1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25 */
26
27 #include <libzfs.h>
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdarg.h>
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/file.h>
37 #include <sys/mntent.h>
38 #include <sys/mnttab.h>
39 #include <sys/param.h>
40 #include <sys/stat.h>
41
42 #include <sys/dmu.h>
43 #include <sys/dmu_objset.h>
44 #include <sys/dnode.h>
45 #include <sys/vdev_impl.h>
46
47 #include <sys/mkdev.h>
48
49 #include "zinject.h"
50
51 static int debug;
52
53 static void
ziprintf(const char * fmt,...)54 ziprintf(const char *fmt, ...)
55 {
56 va_list ap;
57
58 if (!debug)
59 return;
60
61 va_start(ap, fmt);
62 (void) vprintf(fmt, ap);
63 va_end(ap);
64 }
65
66 static void
compress_slashes(const char * src,char * dest)67 compress_slashes(const char *src, char *dest)
68 {
69 while (*src != '\0') {
70 *dest = *src++;
71 while (*dest == '/' && *src == '/')
72 ++src;
73 ++dest;
74 }
75 *dest = '\0';
76 }
77
78 /*
79 * Given a full path to a file, translate into a dataset name and a relative
80 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
81 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
82 * buffer, which we need later to get the object ID.
83 */
84 static int
parse_pathname(const char * inpath,char * dataset,char * relpath,struct stat64 * statbuf)85 parse_pathname(const char *inpath, char *dataset, char *relpath,
86 struct stat64 *statbuf)
87 {
88 struct mnttab mp;
89 const char *rel;
90 char fullpath[MAXPATHLEN];
91
92 compress_slashes(inpath, fullpath);
93
94 if (fullpath[0] != '/') {
95 (void) fprintf(stderr, "invalid object '%s': must be full "
96 "path\n", fullpath);
97 usage();
98 return (-1);
99 }
100
101 if (getextmntent(fullpath, &mp, statbuf) != 0) {
102 (void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
103 fullpath);
104 return (-1);
105 }
106
107 if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) {
108 (void) fprintf(stderr, "invalid path '%s': not a ZFS "
109 "filesystem\n", fullpath);
110 return (-1);
111 }
112
113 if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) {
114 (void) fprintf(stderr, "invalid path '%s': mountpoint "
115 "doesn't match path\n", fullpath);
116 return (-1);
117 }
118
119 (void) strlcpy(dataset, mp.mnt_special, MAXNAMELEN);
120
121 rel = fullpath + strlen(mp.mnt_mountp);
122 if (rel[0] == '/')
123 rel++;
124 (void) strlcpy(relpath, rel, MAXPATHLEN);
125
126 return (0);
127 }
128
129 /*
130 * Convert from a dataset to a objset id. Note that
131 * we grab the object number from the inode number.
132 */
133 static int
object_from_path(const char * dataset,uint64_t object,zinject_record_t * record)134 object_from_path(const char *dataset, uint64_t object, zinject_record_t *record)
135 {
136 zfs_handle_t *zhp;
137
138 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
139 return (-1);
140
141 record->zi_objset = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
142 record->zi_object = object;
143
144 zfs_close(zhp);
145
146 return (0);
147 }
148
149 /*
150 * Initialize the range based on the type, level, and range given.
151 */
152 static int
initialize_range(err_type_t type,int level,char * range,zinject_record_t * record)153 initialize_range(err_type_t type, int level, char *range,
154 zinject_record_t *record)
155 {
156 /*
157 * Determine the numeric range from the string.
158 */
159 if (range == NULL) {
160 /*
161 * If range is unspecified, set the range to [0,-1], which
162 * indicates that the whole object should be treated as an
163 * error.
164 */
165 record->zi_start = 0;
166 record->zi_end = -1ULL;
167 } else {
168 char *comma;
169 int error;
170
171 comma = strchr(range, ',');
172 if (comma != NULL)
173 *comma = '\0';
174
175 error = zfs_nicestrtonum(g_zfs, range,
176 &record->zi_start);
177
178 if (comma != NULL)
179 *comma = ',';
180
181 if (error != 0)
182 goto bad_range;
183
184 if (comma != NULL) {
185 if (zfs_nicestrtonum(g_zfs, comma + 1,
186 &record->zi_end) != 0)
187 goto bad_range;
188 } else {
189 record->zi_end = record->zi_start + 1;
190 }
191 }
192
193 switch (type) {
194 default:
195 break;
196
197 case TYPE_DATA:
198 break;
199
200 case TYPE_DNODE:
201 /*
202 * If this is a request to inject faults into the dnode, then we
203 * must translate the current (objset,object) pair into an
204 * offset within the metadnode for the objset. Specifying any
205 * kind of range with type 'dnode' is illegal.
206 */
207 if (range != NULL) {
208 (void) fprintf(stderr, "range cannot be specified when "
209 "type is 'dnode'\n");
210 return (-1);
211 }
212
213 record->zi_start = record->zi_object * sizeof (dnode_phys_t);
214 record->zi_end = record->zi_start + sizeof (dnode_phys_t);
215 record->zi_object = 0;
216 break;
217 }
218
219 record->zi_level = level;
220
221 return (0);
222
223 bad_range:
224 (void) fprintf(stderr, "invalid range '%s': must be of the form "
225 "'start[,end]'\n", range);
226 return (-1);
227 }
228
229 int
translate_record(err_type_t type,const char * object,const char * range,int level,zinject_record_t * record,char * poolname,char * dataset)230 translate_record(err_type_t type, const char *object, const char *range,
231 int level, zinject_record_t *record, char *poolname, char *dataset)
232 {
233 char path[MAXPATHLEN];
234 char *slash;
235 struct stat64 statbuf;
236 int ret = -1;
237
238 debug = (getenv("ZINJECT_DEBUG") != NULL);
239
240 ziprintf("translating: %s\n", object);
241
242 if (MOS_TYPE(type)) {
243 /*
244 * MOS objects are treated specially.
245 */
246 switch (type) {
247 default:
248 break;
249 case TYPE_MOS:
250 record->zi_type = 0;
251 break;
252 case TYPE_MOSDIR:
253 record->zi_type = DMU_OT_OBJECT_DIRECTORY;
254 break;
255 case TYPE_METASLAB:
256 record->zi_type = DMU_OT_OBJECT_ARRAY;
257 break;
258 case TYPE_CONFIG:
259 record->zi_type = DMU_OT_PACKED_NVLIST;
260 break;
261 case TYPE_BPOBJ:
262 record->zi_type = DMU_OT_BPOBJ;
263 break;
264 case TYPE_SPACEMAP:
265 record->zi_type = DMU_OT_SPACE_MAP;
266 break;
267 case TYPE_ERRLOG:
268 record->zi_type = DMU_OT_ERROR_LOG;
269 break;
270 }
271
272 dataset[0] = '\0';
273 (void) strlcpy(poolname, object, MAXNAMELEN);
274 return (0);
275 }
276
277 /*
278 * Convert a full path into a (dataset, file) pair.
279 */
280 if (parse_pathname(object, dataset, path, &statbuf) != 0)
281 goto err;
282
283 ziprintf(" dataset: %s\n", dataset);
284 ziprintf(" path: %s\n", path);
285
286 /*
287 * Convert (dataset, file) into (objset, object)
288 */
289 if (object_from_path(dataset, statbuf.st_ino, record) != 0)
290 goto err;
291
292 ziprintf("raw objset: %llu\n", record->zi_objset);
293 ziprintf("raw object: %llu\n", record->zi_object);
294
295 /*
296 * For the given object, initialize the range in bytes
297 */
298 if (initialize_range(type, level, (char *)range, record) != 0)
299 goto err;
300
301 ziprintf(" objset: %llu\n", record->zi_objset);
302 ziprintf(" object: %llu\n", record->zi_object);
303 if (record->zi_start == 0 &&
304 record->zi_end == -1ULL)
305 ziprintf(" range: all\n");
306 else
307 ziprintf(" range: [%llu, %llu]\n", record->zi_start,
308 record->zi_end);
309
310 /*
311 * Copy the pool name
312 */
313 (void) strlcpy(poolname, dataset, MAXNAMELEN);
314 if ((slash = strchr(poolname, '/')) != NULL)
315 *slash = '\0';
316
317 ret = 0;
318
319 err:
320 return (ret);
321 }
322
323 int
translate_raw(const char * str,zinject_record_t * record)324 translate_raw(const char *str, zinject_record_t *record)
325 {
326 /*
327 * A raw bookmark of the form objset:object:level:blkid, where each
328 * number is a hexadecimal value.
329 */
330 if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
331 (u_longlong_t *)&record->zi_object, &record->zi_level,
332 (u_longlong_t *)&record->zi_start) != 4) {
333 (void) fprintf(stderr, "bad raw spec '%s': must be of the form "
334 "'objset:object:level:blkid'\n", str);
335 return (-1);
336 }
337
338 record->zi_end = record->zi_start;
339
340 return (0);
341 }
342
343 int
translate_device(const char * pool,const char * device,err_type_t label_type,zinject_record_t * record)344 translate_device(const char *pool, const char *device, err_type_t label_type,
345 zinject_record_t *record)
346 {
347 char *end;
348 zpool_handle_t *zhp;
349 nvlist_t *tgt;
350 boolean_t isspare, iscache;
351
352 /*
353 * Given a device name or GUID, create an appropriate injection record
354 * with zi_guid set.
355 */
356 if ((zhp = zpool_open(g_zfs, pool)) == NULL)
357 return (-1);
358
359 record->zi_guid = strtoull(device, &end, 0);
360 if (record->zi_guid == 0 || *end != '\0') {
361 tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
362
363 if (tgt == NULL) {
364 (void) fprintf(stderr, "cannot find device '%s' in "
365 "pool '%s'\n", device, pool);
366 zpool_close(zhp);
367 return (-1);
368 }
369
370 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
371 &record->zi_guid) == 0);
372 }
373
374 /*
375 * Device faults can take on three different forms:
376 * 1). delayed or hanging I/O
377 * 2). zfs label faults
378 * 3). generic disk faults
379 */
380 if (record->zi_timer != 0) {
381 record->zi_cmd = ZINJECT_DELAY_IO;
382 } else if (label_type != TYPE_INVAL) {
383 record->zi_cmd = ZINJECT_LABEL_FAULT;
384 } else {
385 record->zi_cmd = ZINJECT_DEVICE_FAULT;
386 }
387
388 switch (label_type) {
389 default:
390 break;
391 case TYPE_LABEL_UBERBLOCK:
392 record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
393 record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
394 break;
395 case TYPE_LABEL_NVLIST:
396 record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
397 record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;
398 break;
399 case TYPE_LABEL_PAD1:
400 record->zi_start = offsetof(vdev_label_t, vl_pad1);
401 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
402 break;
403 case TYPE_LABEL_PAD2:
404 record->zi_start = offsetof(vdev_label_t, vl_be);
405 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
406 break;
407 }
408 zpool_close(zhp);
409 return (0);
410 }
411