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.
36 #include <sys/mntent.h>
37 #include <sys/mnttab.h>
38 #include <sys/param.h>
42 #include <sys/dmu_objset.h>
43 #include <sys/dnode.h>
44 #include <sys/vdev_impl.h>
46 #include <sys/mkdev.h>
53 ziprintf(const char *fmt, ...)
61 (void) vprintf(fmt, ap);
66 compress_slashes(const char *src, char *dest)
68 while (*src != '\0') {
70 while (*dest == '/' && *src == '/')
78 * Given a full path to a file, translate into a dataset name and a relative
79 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
80 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
81 * buffer, which we need later to get the object ID.
84 parse_pathname(const char *inpath, char *dataset, char *relpath,
85 struct stat64 *statbuf)
89 char fullpath[MAXPATHLEN];
91 compress_slashes(inpath, fullpath);
93 if (fullpath[0] != '/') {
94 (void) fprintf(stderr, "invalid object '%s': must be full "
100 if (getextmntent(fullpath, &mp, statbuf) != 0) {
101 (void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
106 if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) {
107 (void) fprintf(stderr, "invalid path '%s': not a ZFS "
108 "filesystem\n", fullpath);
112 if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) {
113 (void) fprintf(stderr, "invalid path '%s': mountpoint "
114 "doesn't match path\n", fullpath);
118 (void) strcpy(dataset, mp.mnt_special);
120 rel = fullpath + strlen(mp.mnt_mountp);
123 (void) strcpy(relpath, rel);
129 * Convert from a dataset to a objset id. Note that
130 * we grab the object number from the inode number.
133 object_from_path(const char *dataset, uint64_t object, zinject_record_t *record)
137 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
140 record->zi_objset = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
141 record->zi_object = object;
149 * Initialize the range based on the type, level, and range given.
152 initialize_range(err_type_t type, int level, char *range,
153 zinject_record_t *record)
156 * Determine the numeric range from the string.
160 * If range is unspecified, set the range to [0,-1], which
161 * indicates that the whole object should be treated as an
164 record->zi_start = 0;
165 record->zi_end = -1ULL;
169 /* XXX add support for suffixes */
170 record->zi_start = strtoull(range, &end, 10);
174 record->zi_end = record->zi_start + 1;
175 else if (*end == ',')
176 record->zi_end = strtoull(end + 1, &end, 10);
179 (void) fprintf(stderr, "invalid range '%s': must be "
180 "a numeric range of the form 'start[,end]'\n",
194 * If this is a request to inject faults into the dnode, then we
195 * must translate the current (objset,object) pair into an
196 * offset within the metadnode for the objset. Specifying any
197 * kind of range with type 'dnode' is illegal.
200 (void) fprintf(stderr, "range cannot be specified when "
201 "type is 'dnode'\n");
205 record->zi_start = record->zi_object * sizeof (dnode_phys_t);
206 record->zi_end = record->zi_start + sizeof (dnode_phys_t);
207 record->zi_object = 0;
211 record->zi_level = level;
217 translate_record(err_type_t type, const char *object, const char *range,
218 int level, zinject_record_t *record, char *poolname, char *dataset)
220 char path[MAXPATHLEN];
222 struct stat64 statbuf;
225 debug = (getenv("ZINJECT_DEBUG") != NULL);
227 ziprintf("translating: %s\n", object);
229 if (MOS_TYPE(type)) {
231 * MOS objects are treated specially.
240 record->zi_type = DMU_OT_OBJECT_DIRECTORY;
243 record->zi_type = DMU_OT_OBJECT_ARRAY;
246 record->zi_type = DMU_OT_PACKED_NVLIST;
249 record->zi_type = DMU_OT_BPOBJ;
252 record->zi_type = DMU_OT_SPACE_MAP;
255 record->zi_type = DMU_OT_ERROR_LOG;
260 (void) strcpy(poolname, object);
265 * Convert a full path into a (dataset, file) pair.
267 if (parse_pathname(object, dataset, path, &statbuf) != 0)
270 ziprintf(" dataset: %s\n", dataset);
271 ziprintf(" path: %s\n", path);
274 * Convert (dataset, file) into (objset, object)
276 if (object_from_path(dataset, statbuf.st_ino, record) != 0)
279 ziprintf("raw objset: %llu\n", record->zi_objset);
280 ziprintf("raw object: %llu\n", record->zi_object);
283 * For the given object, initialize the range in bytes
285 if (initialize_range(type, level, (char *)range, record) != 0)
288 ziprintf(" objset: %llu\n", record->zi_objset);
289 ziprintf(" object: %llu\n", record->zi_object);
290 if (record->zi_start == 0 &&
291 record->zi_end == -1ULL)
292 ziprintf(" range: all\n");
294 ziprintf(" range: [%llu, %llu]\n", record->zi_start,
300 (void) strcpy(poolname, dataset);
301 if ((slash = strchr(poolname, '/')) != NULL)
311 translate_raw(const char *str, zinject_record_t *record)
314 * A raw bookmark of the form objset:object:level:blkid, where each
315 * number is a hexadecimal value.
317 if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
318 (u_longlong_t *)&record->zi_object, &record->zi_level,
319 (u_longlong_t *)&record->zi_start) != 4) {
320 (void) fprintf(stderr, "bad raw spec '%s': must be of the form "
321 "'objset:object:level:blkid'\n", str);
325 record->zi_end = record->zi_start;
331 translate_device(const char *pool, const char *device, err_type_t label_type,
332 zinject_record_t *record)
337 boolean_t isspare, iscache;
340 * Given a device name or GUID, create an appropriate injection record
343 if ((zhp = zpool_open(g_zfs, pool)) == NULL)
346 record->zi_guid = strtoull(device, &end, 0);
347 if (record->zi_guid == 0 || *end != '\0') {
348 tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
351 (void) fprintf(stderr, "cannot find device '%s' in "
352 "pool '%s'\n", device, pool);
357 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
358 &record->zi_guid) == 0);
362 * Device faults can take on three different forms:
363 * 1). delayed or hanging I/O
364 * 2). zfs label faults
365 * 3). generic disk faults
367 if (record->zi_timer != 0) {
368 record->zi_cmd = ZINJECT_DELAY_IO;
369 } else if (label_type != TYPE_INVAL) {
370 record->zi_cmd = ZINJECT_LABEL_FAULT;
372 record->zi_cmd = ZINJECT_DEVICE_FAULT;
375 switch (label_type) {
378 case TYPE_LABEL_UBERBLOCK:
379 record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
380 record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
382 case TYPE_LABEL_NVLIST:
383 record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
384 record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;
386 case TYPE_LABEL_PAD1:
387 record->zi_start = offsetof(vdev_label_t, vl_pad1);
388 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
390 case TYPE_LABEL_PAD2:
391 record->zi_start = offsetof(vdev_label_t, vl_pad2);
392 record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;