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.
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.
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]
22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
28 #include <sys/zfs_context.h>
38 #include <sys/mntent.h>
39 #include <sys/mnttab.h>
40 #include <sys/param.h>
44 #include <sys/dmu_objset.h>
45 #include <sys/dnode.h>
46 #include <sys/vdev_impl.h>
50 extern void kernel_init(int);
51 extern void kernel_fini(void);
56 ziprintf(const char *fmt, ...)
64 (void) vprintf(fmt, ap);
69 compress_slashes(const char *src, char *dest)
71 while (*src != '\0') {
73 while (*dest == '/' && *src == '/')
81 * Given a full path to a file, translate into a dataset name and a relative
82 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
83 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
84 * buffer, which we need later to get the object ID.
87 parse_pathname(const char *inpath, char *dataset, char *relpath,
88 struct stat64 *statbuf)
92 char fullpath[MAXPATHLEN];
94 compress_slashes(inpath, fullpath);
96 if (fullpath[0] != '/') {
97 (void) fprintf(stderr, "invalid object '%s': must be full "
103 if (strlen(fullpath) >= MAXPATHLEN) {
104 (void) fprintf(stderr, "invalid object; pathname too long\n");
108 if (stat64(fullpath, statbuf) != 0) {
109 (void) fprintf(stderr, "cannot open '%s': %s\n",
110 fullpath, strerror(errno));
114 if (statfs(fullpath, &sfs) == -1) {
115 (void) fprintf(stderr, "cannot find mountpoint for '%s': %s\n",
116 fullpath, strerror(errno));
120 if (strcmp(sfs.f_fstypename, MNTTYPE_ZFS) != 0) {
121 (void) fprintf(stderr, "invalid path '%s': not a ZFS "
122 "filesystem\n", fullpath);
126 if (strncmp(fullpath, sfs.f_mntonname, strlen(sfs.f_mntonname)) != 0) {
127 (void) fprintf(stderr, "invalid path '%s': mountpoint "
128 "doesn't match path\n", fullpath);
132 (void) strcpy(dataset, sfs.f_mntfromname);
134 rel = fullpath + strlen(sfs.f_mntonname);
137 (void) strcpy(relpath, rel);
143 * Convert from a (dataset, path) pair into a (objset, object) pair. Note that
144 * we grab the object number from the inode number, since looking this up via
145 * libzpool is a real pain.
149 object_from_path(const char *dataset, const char *path, struct stat64 *statbuf,
150 zinject_record_t *record)
156 * Before doing any libzpool operations, call sync() to ensure that the
157 * on-disk state is consistent with the in-core state.
161 err = dmu_objset_own(dataset, DMU_OST_ZFS, B_TRUE, FTAG, &os);
163 (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
164 dataset, strerror(err));
168 record->zi_objset = dmu_objset_id(os);
169 record->zi_object = statbuf->st_ino;
171 dmu_objset_disown(os, FTAG);
177 * Calculate the real range based on the type, level, and range given.
180 calculate_range(const char *dataset, err_type_t type, int level, char *range,
181 zinject_record_t *record)
189 * Determine the numeric range from the string.
193 * If range is unspecified, set the range to [0,-1], which
194 * indicates that the whole object should be treated as an
197 record->zi_start = 0;
198 record->zi_end = -1ULL;
202 /* XXX add support for suffixes */
203 record->zi_start = strtoull(range, &end, 10);
207 record->zi_end = record->zi_start + 1;
208 else if (*end == ',')
209 record->zi_end = strtoull(end + 1, &end, 10);
212 (void) fprintf(stderr, "invalid range '%s': must be "
213 "a numeric range of the form 'start[,end]'\n",
225 * If this is a request to inject faults into the dnode, then we
226 * must translate the current (objset,object) pair into an
227 * offset within the metadnode for the objset. Specifying any
228 * kind of range with type 'dnode' is illegal.
231 (void) fprintf(stderr, "range cannot be specified when "
232 "type is 'dnode'\n");
236 record->zi_start = record->zi_object * sizeof (dnode_phys_t);
237 record->zi_end = record->zi_start + sizeof (dnode_phys_t);
238 record->zi_object = 0;
243 * Get the dnode associated with object, so we can calculate the block
246 if ((err = dmu_objset_own(dataset, DMU_OST_ANY,
247 B_TRUE, FTAG, &os)) != 0) {
248 (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
249 dataset, strerror(err));
253 if (record->zi_object == 0) {
254 dn = DMU_META_DNODE(os);
256 err = dnode_hold(os, record->zi_object, FTAG, &dn);
258 (void) fprintf(stderr, "failed to hold dnode "
260 (u_longlong_t)record->zi_object);
266 ziprintf("data shift: %d\n", (int)dn->dn_datablkshift);
267 ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift);
270 * Translate range into block IDs.
272 if (record->zi_start != 0 || record->zi_end != -1ULL) {
273 record->zi_start >>= dn->dn_datablkshift;
274 record->zi_end >>= dn->dn_datablkshift;
278 * Check level, and then translate level 0 blkids into ranges
279 * appropriate for level of indirection.
281 record->zi_level = level;
283 ziprintf("level 0 blkid range: [%llu, %llu]\n",
284 record->zi_start, record->zi_end);
286 if (level >= dn->dn_nlevels) {
287 (void) fprintf(stderr, "level %d exceeds max level "
288 "of object (%d)\n", level, dn->dn_nlevels - 1);
292 if (record->zi_start != 0 || record->zi_end != 0) {
293 int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
295 for (; level > 0; level--) {
296 record->zi_start >>= shift;
297 record->zi_end >>= shift;
305 if (dn != DMU_META_DNODE(os))
306 dnode_rele(dn, FTAG);
309 dmu_objset_disown(os, FTAG);
315 translate_record(err_type_t type, const char *object, const char *range,
316 int level, zinject_record_t *record, char *poolname, char *dataset)
318 char path[MAXPATHLEN];
320 struct stat64 statbuf;
325 debug = (getenv("ZINJECT_DEBUG") != NULL);
327 ziprintf("translating: %s\n", object);
329 if (MOS_TYPE(type)) {
331 * MOS objects are treated specially.
338 record->zi_type = DMU_OT_OBJECT_DIRECTORY;
341 record->zi_type = DMU_OT_OBJECT_ARRAY;
344 record->zi_type = DMU_OT_PACKED_NVLIST;
347 record->zi_type = DMU_OT_BPOBJ;
350 record->zi_type = DMU_OT_SPACE_MAP;
353 record->zi_type = DMU_OT_ERROR_LOG;
358 (void) strcpy(poolname, object);
363 * Convert a full path into a (dataset, file) pair.
365 if (parse_pathname(object, dataset, path, &statbuf) != 0)
368 ziprintf(" dataset: %s\n", dataset);
369 ziprintf(" path: %s\n", path);
372 * Convert (dataset, file) into (objset, object)
374 if (object_from_path(dataset, path, &statbuf, record) != 0)
377 ziprintf("raw objset: %llu\n", record->zi_objset);
378 ziprintf("raw object: %llu\n", record->zi_object);
381 * For the given object, calculate the real (type, level, range)
383 if (calculate_range(dataset, type, level, (char *)range, record) != 0)
386 ziprintf(" objset: %llu\n", record->zi_objset);
387 ziprintf(" object: %llu\n", record->zi_object);
388 if (record->zi_start == 0 &&
389 record->zi_end == -1ULL)
390 ziprintf(" range: all\n");
392 ziprintf(" range: [%llu, %llu]\n", record->zi_start,
398 (void) strcpy(poolname, dataset);
399 if ((slash = strchr(poolname, '/')) != NULL)
410 translate_raw(const char *str, zinject_record_t *record)
413 * A raw bookmark of the form objset:object:level:blkid, where each
414 * number is a hexidecimal value.
416 if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
417 (u_longlong_t *)&record->zi_object, &record->zi_level,
418 (u_longlong_t *)&record->zi_start) != 4) {
419 (void) fprintf(stderr, "bad raw spec '%s': must be of the form "
420 "'objset:object:level:blkid'\n", str);
424 record->zi_end = record->zi_start;
430 translate_device(const char *pool, const char *device, err_type_t label_type,
431 zinject_record_t *record)
436 boolean_t isspare, iscache;
439 * Given a device name or GUID, create an appropriate injection record
442 if ((zhp = zpool_open(g_zfs, pool)) == NULL)
445 record->zi_guid = strtoull(device, &end, 16);
446 if (record->zi_guid == 0 || *end != '\0') {
447 tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
450 (void) fprintf(stderr, "cannot find device '%s' in "
451 "pool '%s'\n", device, pool);
455 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
456 &record->zi_guid) == 0);
460 * Device faults can take on three different forms:
461 * 1). delayed or hanging I/O
462 * 2). zfs label faults
463 * 3). generic disk faults
465 if (record->zi_timer != 0) {
466 record->zi_cmd = ZINJECT_DELAY_IO;
467 } else if (label_type != TYPE_INVAL) {
468 record->zi_cmd = ZINJECT_LABEL_FAULT;
470 record->zi_cmd = ZINJECT_DEVICE_FAULT;
473 switch (label_type) {
474 case TYPE_LABEL_UBERBLOCK:
475 record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
476 record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
478 case TYPE_LABEL_NVLIST:
479 record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
480 record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;
482 case TYPE_LABEL_PAD1:
483 record->zi_start = offsetof(vdev_label_t, vl_pad1);
484 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
486 case TYPE_LABEL_PAD2:
487 record->zi_start = offsetof(vdev_label_t, vl_pad2);
488 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;