]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. |
cc92e9d0 | 23 | * Copyright (c) 2012 by Delphix. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
34dc7c2f BB |
26 | #include <libzfs.h> |
27 | ||
34dc7c2f BB |
28 | #include <sys/zfs_context.h> |
29 | ||
30 | #include <errno.h> | |
31 | #include <fcntl.h> | |
32 | #include <stdarg.h> | |
33 | #include <stddef.h> | |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <strings.h> | |
37 | #include <sys/file.h> | |
38 | #include <sys/mntent.h> | |
39 | #include <sys/mnttab.h> | |
40 | #include <sys/param.h> | |
41 | #include <sys/stat.h> | |
42 | ||
43 | #include <sys/dmu.h> | |
44 | #include <sys/dmu_objset.h> | |
45 | #include <sys/dnode.h> | |
b128c09f | 46 | #include <sys/vdev_impl.h> |
34dc7c2f BB |
47 | |
48 | #include <sys/mkdev.h> | |
49 | ||
50 | #include "zinject.h" | |
51 | ||
52 | extern void kernel_init(int); | |
53 | extern void kernel_fini(void); | |
54 | ||
55 | static int debug; | |
56 | ||
57 | static void | |
58 | ziprintf(const char *fmt, ...) | |
59 | { | |
60 | va_list ap; | |
61 | ||
62 | if (!debug) | |
63 | return; | |
64 | ||
65 | va_start(ap, fmt); | |
66 | (void) vprintf(fmt, ap); | |
67 | va_end(ap); | |
68 | } | |
69 | ||
428870ff BB |
70 | static void |
71 | compress_slashes(const char *src, char *dest) | |
72 | { | |
73 | while (*src != '\0') { | |
74 | *dest = *src++; | |
75 | while (*dest == '/' && *src == '/') | |
76 | ++src; | |
77 | ++dest; | |
78 | } | |
79 | *dest = '\0'; | |
80 | } | |
81 | ||
34dc7c2f BB |
82 | /* |
83 | * Given a full path to a file, translate into a dataset name and a relative | |
84 | * path within the dataset. 'dataset' must be at least MAXNAMELEN characters, | |
85 | * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64 | |
86 | * buffer, which we need later to get the object ID. | |
87 | */ | |
88 | static int | |
428870ff | 89 | parse_pathname(const char *inpath, char *dataset, char *relpath, |
34dc7c2f BB |
90 | struct stat64 *statbuf) |
91 | { | |
92 | struct extmnttab mp; | |
93 | FILE *fp; | |
94 | int match; | |
95 | const char *rel; | |
428870ff BB |
96 | char fullpath[MAXPATHLEN]; |
97 | ||
98 | compress_slashes(inpath, fullpath); | |
34dc7c2f BB |
99 | |
100 | if (fullpath[0] != '/') { | |
101 | (void) fprintf(stderr, "invalid object '%s': must be full " | |
102 | "path\n", fullpath); | |
103 | usage(); | |
104 | return (-1); | |
105 | } | |
106 | ||
107 | if (strlen(fullpath) >= MAXPATHLEN) { | |
108 | (void) fprintf(stderr, "invalid object; pathname too long\n"); | |
109 | return (-1); | |
110 | } | |
111 | ||
112 | if (stat64(fullpath, statbuf) != 0) { | |
113 | (void) fprintf(stderr, "cannot open '%s': %s\n", | |
114 | fullpath, strerror(errno)); | |
115 | return (-1); | |
116 | } | |
117 | ||
f9e459d1 TF |
118 | #ifdef HAVE_SETMNTENT |
119 | if ((fp = setmntent(MNTTAB, "r")) == NULL) { | |
120 | #else | |
34dc7c2f | 121 | if ((fp = fopen(MNTTAB, "r")) == NULL) { |
f9e459d1 | 122 | #endif |
79251738 | 123 | (void) fprintf(stderr, "cannot open %s\n", MNTTAB); |
34dc7c2f BB |
124 | return (-1); |
125 | } | |
126 | ||
127 | match = 0; | |
128 | while (getextmntent(fp, &mp, sizeof (mp)) == 0) { | |
129 | if (makedev(mp.mnt_major, mp.mnt_minor) == statbuf->st_dev) { | |
130 | match = 1; | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | if (!match) { | |
136 | (void) fprintf(stderr, "cannot find mountpoint for '%s'\n", | |
137 | fullpath); | |
138 | return (-1); | |
139 | } | |
140 | ||
141 | if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) { | |
142 | (void) fprintf(stderr, "invalid path '%s': not a ZFS " | |
143 | "filesystem\n", fullpath); | |
144 | return (-1); | |
145 | } | |
146 | ||
147 | if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) { | |
148 | (void) fprintf(stderr, "invalid path '%s': mountpoint " | |
149 | "doesn't match path\n", fullpath); | |
150 | return (-1); | |
151 | } | |
152 | ||
153 | (void) strcpy(dataset, mp.mnt_special); | |
154 | ||
155 | rel = fullpath + strlen(mp.mnt_mountp); | |
156 | if (rel[0] == '/') | |
157 | rel++; | |
158 | (void) strcpy(relpath, rel); | |
159 | ||
160 | return (0); | |
161 | } | |
162 | ||
163 | /* | |
164 | * Convert from a (dataset, path) pair into a (objset, object) pair. Note that | |
165 | * we grab the object number from the inode number, since looking this up via | |
166 | * libzpool is a real pain. | |
167 | */ | |
168 | /* ARGSUSED */ | |
169 | static int | |
170 | object_from_path(const char *dataset, const char *path, struct stat64 *statbuf, | |
171 | zinject_record_t *record) | |
172 | { | |
173 | objset_t *os; | |
174 | int err; | |
175 | ||
176 | /* | |
177 | * Before doing any libzpool operations, call sync() to ensure that the | |
178 | * on-disk state is consistent with the in-core state. | |
179 | */ | |
180 | sync(); | |
181 | ||
b5256303 | 182 | err = dmu_objset_own(dataset, DMU_OST_ZFS, B_TRUE, B_FALSE, FTAG, &os); |
428870ff | 183 | if (err != 0) { |
34dc7c2f BB |
184 | (void) fprintf(stderr, "cannot open dataset '%s': %s\n", |
185 | dataset, strerror(err)); | |
186 | return (-1); | |
187 | } | |
188 | ||
189 | record->zi_objset = dmu_objset_id(os); | |
190 | record->zi_object = statbuf->st_ino; | |
191 | ||
b5256303 | 192 | dmu_objset_disown(os, B_FALSE, FTAG); |
34dc7c2f BB |
193 | |
194 | return (0); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Calculate the real range based on the type, level, and range given. | |
199 | */ | |
200 | static int | |
201 | calculate_range(const char *dataset, err_type_t type, int level, char *range, | |
202 | zinject_record_t *record) | |
203 | { | |
204 | objset_t *os = NULL; | |
205 | dnode_t *dn = NULL; | |
206 | int err; | |
207 | int ret = -1; | |
208 | ||
209 | /* | |
210 | * Determine the numeric range from the string. | |
211 | */ | |
212 | if (range == NULL) { | |
213 | /* | |
214 | * If range is unspecified, set the range to [0,-1], which | |
215 | * indicates that the whole object should be treated as an | |
216 | * error. | |
217 | */ | |
218 | record->zi_start = 0; | |
219 | record->zi_end = -1ULL; | |
220 | } else { | |
221 | char *end; | |
222 | ||
223 | /* XXX add support for suffixes */ | |
224 | record->zi_start = strtoull(range, &end, 10); | |
225 | ||
226 | ||
227 | if (*end == '\0') | |
228 | record->zi_end = record->zi_start + 1; | |
229 | else if (*end == ',') | |
230 | record->zi_end = strtoull(end + 1, &end, 10); | |
231 | ||
232 | if (*end != '\0') { | |
233 | (void) fprintf(stderr, "invalid range '%s': must be " | |
234 | "a numeric range of the form 'start[,end]'\n", | |
235 | range); | |
236 | goto out; | |
237 | } | |
238 | } | |
239 | ||
240 | switch (type) { | |
e75c13c3 BB |
241 | default: |
242 | break; | |
34dc7c2f BB |
243 | case TYPE_DATA: |
244 | break; | |
245 | ||
246 | case TYPE_DNODE: | |
247 | /* | |
248 | * If this is a request to inject faults into the dnode, then we | |
249 | * must translate the current (objset,object) pair into an | |
250 | * offset within the metadnode for the objset. Specifying any | |
251 | * kind of range with type 'dnode' is illegal. | |
252 | */ | |
253 | if (range != NULL) { | |
254 | (void) fprintf(stderr, "range cannot be specified when " | |
255 | "type is 'dnode'\n"); | |
256 | goto out; | |
257 | } | |
258 | ||
259 | record->zi_start = record->zi_object * sizeof (dnode_phys_t); | |
260 | record->zi_end = record->zi_start + sizeof (dnode_phys_t); | |
261 | record->zi_object = 0; | |
262 | break; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Get the dnode associated with object, so we can calculate the block | |
267 | * size. | |
268 | */ | |
428870ff | 269 | if ((err = dmu_objset_own(dataset, DMU_OST_ANY, |
b5256303 | 270 | B_TRUE, B_FALSE, FTAG, &os)) != 0) { |
34dc7c2f BB |
271 | (void) fprintf(stderr, "cannot open dataset '%s': %s\n", |
272 | dataset, strerror(err)); | |
273 | goto out; | |
274 | } | |
275 | ||
276 | if (record->zi_object == 0) { | |
572e2857 | 277 | dn = DMU_META_DNODE(os); |
34dc7c2f | 278 | } else { |
428870ff | 279 | err = dnode_hold(os, record->zi_object, FTAG, &dn); |
34dc7c2f BB |
280 | if (err != 0) { |
281 | (void) fprintf(stderr, "failed to hold dnode " | |
282 | "for object %llu\n", | |
283 | (u_longlong_t)record->zi_object); | |
284 | goto out; | |
285 | } | |
286 | } | |
287 | ||
288 | ||
289 | ziprintf("data shift: %d\n", (int)dn->dn_datablkshift); | |
290 | ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift); | |
291 | ||
292 | /* | |
293 | * Translate range into block IDs. | |
294 | */ | |
295 | if (record->zi_start != 0 || record->zi_end != -1ULL) { | |
296 | record->zi_start >>= dn->dn_datablkshift; | |
297 | record->zi_end >>= dn->dn_datablkshift; | |
298 | } | |
299 | ||
300 | /* | |
301 | * Check level, and then translate level 0 blkids into ranges | |
302 | * appropriate for level of indirection. | |
303 | */ | |
304 | record->zi_level = level; | |
305 | if (level > 0) { | |
306 | ziprintf("level 0 blkid range: [%llu, %llu]\n", | |
307 | record->zi_start, record->zi_end); | |
308 | ||
309 | if (level >= dn->dn_nlevels) { | |
310 | (void) fprintf(stderr, "level %d exceeds max level " | |
311 | "of object (%d)\n", level, dn->dn_nlevels - 1); | |
312 | goto out; | |
313 | } | |
314 | ||
315 | if (record->zi_start != 0 || record->zi_end != 0) { | |
316 | int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT; | |
317 | ||
318 | for (; level > 0; level--) { | |
319 | record->zi_start >>= shift; | |
320 | record->zi_end >>= shift; | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | ret = 0; | |
326 | out: | |
327 | if (dn) { | |
572e2857 | 328 | if (dn != DMU_META_DNODE(os)) |
34dc7c2f BB |
329 | dnode_rele(dn, FTAG); |
330 | } | |
331 | if (os) | |
b5256303 | 332 | dmu_objset_disown(os, B_FALSE, FTAG); |
34dc7c2f BB |
333 | |
334 | return (ret); | |
335 | } | |
336 | ||
337 | int | |
338 | translate_record(err_type_t type, const char *object, const char *range, | |
339 | int level, zinject_record_t *record, char *poolname, char *dataset) | |
340 | { | |
341 | char path[MAXPATHLEN]; | |
342 | char *slash; | |
343 | struct stat64 statbuf; | |
344 | int ret = -1; | |
345 | ||
346 | kernel_init(FREAD); | |
347 | ||
348 | debug = (getenv("ZINJECT_DEBUG") != NULL); | |
349 | ||
350 | ziprintf("translating: %s\n", object); | |
351 | ||
352 | if (MOS_TYPE(type)) { | |
353 | /* | |
354 | * MOS objects are treated specially. | |
355 | */ | |
356 | switch (type) { | |
e75c13c3 BB |
357 | default: |
358 | break; | |
34dc7c2f BB |
359 | case TYPE_MOS: |
360 | record->zi_type = 0; | |
361 | break; | |
362 | case TYPE_MOSDIR: | |
363 | record->zi_type = DMU_OT_OBJECT_DIRECTORY; | |
364 | break; | |
365 | case TYPE_METASLAB: | |
366 | record->zi_type = DMU_OT_OBJECT_ARRAY; | |
367 | break; | |
368 | case TYPE_CONFIG: | |
369 | record->zi_type = DMU_OT_PACKED_NVLIST; | |
370 | break; | |
428870ff BB |
371 | case TYPE_BPOBJ: |
372 | record->zi_type = DMU_OT_BPOBJ; | |
34dc7c2f BB |
373 | break; |
374 | case TYPE_SPACEMAP: | |
375 | record->zi_type = DMU_OT_SPACE_MAP; | |
376 | break; | |
377 | case TYPE_ERRLOG: | |
378 | record->zi_type = DMU_OT_ERROR_LOG; | |
379 | break; | |
380 | } | |
381 | ||
382 | dataset[0] = '\0'; | |
383 | (void) strcpy(poolname, object); | |
384 | return (0); | |
385 | } | |
386 | ||
387 | /* | |
388 | * Convert a full path into a (dataset, file) pair. | |
389 | */ | |
390 | if (parse_pathname(object, dataset, path, &statbuf) != 0) | |
391 | goto err; | |
392 | ||
393 | ziprintf(" dataset: %s\n", dataset); | |
394 | ziprintf(" path: %s\n", path); | |
395 | ||
396 | /* | |
397 | * Convert (dataset, file) into (objset, object) | |
398 | */ | |
399 | if (object_from_path(dataset, path, &statbuf, record) != 0) | |
400 | goto err; | |
401 | ||
402 | ziprintf("raw objset: %llu\n", record->zi_objset); | |
403 | ziprintf("raw object: %llu\n", record->zi_object); | |
404 | ||
405 | /* | |
406 | * For the given object, calculate the real (type, level, range) | |
407 | */ | |
408 | if (calculate_range(dataset, type, level, (char *)range, record) != 0) | |
409 | goto err; | |
410 | ||
411 | ziprintf(" objset: %llu\n", record->zi_objset); | |
412 | ziprintf(" object: %llu\n", record->zi_object); | |
413 | if (record->zi_start == 0 && | |
414 | record->zi_end == -1ULL) | |
415 | ziprintf(" range: all\n"); | |
416 | else | |
417 | ziprintf(" range: [%llu, %llu]\n", record->zi_start, | |
418 | record->zi_end); | |
419 | ||
420 | /* | |
421 | * Copy the pool name | |
422 | */ | |
423 | (void) strcpy(poolname, dataset); | |
424 | if ((slash = strchr(poolname, '/')) != NULL) | |
425 | *slash = '\0'; | |
426 | ||
427 | ret = 0; | |
428 | ||
429 | err: | |
430 | kernel_fini(); | |
431 | return (ret); | |
432 | } | |
433 | ||
434 | int | |
435 | translate_raw(const char *str, zinject_record_t *record) | |
436 | { | |
437 | /* | |
438 | * A raw bookmark of the form objset:object:level:blkid, where each | |
4e33ba4c | 439 | * number is a hexadecimal value. |
34dc7c2f BB |
440 | */ |
441 | if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset, | |
442 | (u_longlong_t *)&record->zi_object, &record->zi_level, | |
443 | (u_longlong_t *)&record->zi_start) != 4) { | |
444 | (void) fprintf(stderr, "bad raw spec '%s': must be of the form " | |
445 | "'objset:object:level:blkid'\n", str); | |
446 | return (-1); | |
447 | } | |
448 | ||
449 | record->zi_end = record->zi_start; | |
450 | ||
451 | return (0); | |
452 | } | |
453 | ||
454 | int | |
b128c09f BB |
455 | translate_device(const char *pool, const char *device, err_type_t label_type, |
456 | zinject_record_t *record) | |
34dc7c2f BB |
457 | { |
458 | char *end; | |
459 | zpool_handle_t *zhp; | |
460 | nvlist_t *tgt; | |
461 | boolean_t isspare, iscache; | |
462 | ||
463 | /* | |
464 | * Given a device name or GUID, create an appropriate injection record | |
465 | * with zi_guid set. | |
466 | */ | |
467 | if ((zhp = zpool_open(g_zfs, pool)) == NULL) | |
468 | return (-1); | |
469 | ||
1a5c611a | 470 | record->zi_guid = strtoull(device, &end, 0); |
34dc7c2f | 471 | if (record->zi_guid == 0 || *end != '\0') { |
b128c09f | 472 | tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL); |
34dc7c2f BB |
473 | |
474 | if (tgt == NULL) { | |
475 | (void) fprintf(stderr, "cannot find device '%s' in " | |
476 | "pool '%s'\n", device, pool); | |
884385a0 | 477 | zpool_close(zhp); |
34dc7c2f BB |
478 | return (-1); |
479 | } | |
480 | ||
481 | verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, | |
482 | &record->zi_guid) == 0); | |
483 | } | |
484 | ||
cc92e9d0 GW |
485 | /* |
486 | * Device faults can take on three different forms: | |
487 | * 1). delayed or hanging I/O | |
488 | * 2). zfs label faults | |
489 | * 3). generic disk faults | |
490 | */ | |
491 | if (record->zi_timer != 0) { | |
492 | record->zi_cmd = ZINJECT_DELAY_IO; | |
493 | } else if (label_type != TYPE_INVAL) { | |
494 | record->zi_cmd = ZINJECT_LABEL_FAULT; | |
495 | } else { | |
496 | record->zi_cmd = ZINJECT_DEVICE_FAULT; | |
497 | } | |
498 | ||
b128c09f | 499 | switch (label_type) { |
e75c13c3 BB |
500 | default: |
501 | break; | |
b128c09f BB |
502 | case TYPE_LABEL_UBERBLOCK: |
503 | record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]); | |
504 | record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1; | |
505 | break; | |
506 | case TYPE_LABEL_NVLIST: | |
507 | record->zi_start = offsetof(vdev_label_t, vl_vdev_phys); | |
508 | record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1; | |
509 | break; | |
428870ff BB |
510 | case TYPE_LABEL_PAD1: |
511 | record->zi_start = offsetof(vdev_label_t, vl_pad1); | |
512 | record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1; | |
513 | break; | |
514 | case TYPE_LABEL_PAD2: | |
515 | record->zi_start = offsetof(vdev_label_t, vl_pad2); | |
516 | record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1; | |
517 | break; | |
b128c09f | 518 | } |
884385a0 | 519 | zpool_close(zhp); |
34dc7c2f BB |
520 | return (0); |
521 | } |