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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 #undef verify /* both libzfs.h and zfs_context.h want to define this */
30 #include <sys/zfs_context.h>
40 #include <sys/mntent.h>
41 #include <sys/mnttab.h>
42 #include <sys/param.h>
46 #include <sys/dmu_objset.h>
47 #include <sys/dnode.h>
48 #include <sys/vdev_impl.h>
50 #include <sys/mkdev.h>
54 extern void kernel_init(int);
55 extern void kernel_fini(void);
60 ziprintf(const char *fmt, ...)
68 (void) vprintf(fmt, ap);
73 * Given a full path to a file, translate into a dataset name and a relative
74 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
75 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
76 * buffer, which we need later to get the object ID.
79 parse_pathname(const char *fullpath, char *dataset, char *relpath,
80 struct stat64 *statbuf)
87 if (fullpath[0] != '/') {
88 (void) fprintf(stderr, "invalid object '%s': must be full "
94 if (strlen(fullpath) >= MAXPATHLEN) {
95 (void) fprintf(stderr, "invalid object; pathname too long\n");
99 if (stat64(fullpath, statbuf) != 0) {
100 (void) fprintf(stderr, "cannot open '%s': %s\n",
101 fullpath, strerror(errno));
105 if ((fp = fopen(MNTTAB, "r")) == NULL) {
106 (void) fprintf(stderr, "cannot open /etc/mnttab\n");
111 while (getextmntent(fp, &mp, sizeof (mp)) == 0) {
112 if (makedev(mp.mnt_major, mp.mnt_minor) == statbuf->st_dev) {
119 (void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
124 if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) {
125 (void) fprintf(stderr, "invalid path '%s': not a ZFS "
126 "filesystem\n", fullpath);
130 if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) {
131 (void) fprintf(stderr, "invalid path '%s': mountpoint "
132 "doesn't match path\n", fullpath);
136 (void) strcpy(dataset, mp.mnt_special);
138 rel = fullpath + strlen(mp.mnt_mountp);
141 (void) strcpy(relpath, rel);
147 * Convert from a (dataset, path) pair into a (objset, object) pair. Note that
148 * we grab the object number from the inode number, since looking this up via
149 * libzpool is a real pain.
153 object_from_path(const char *dataset, const char *path, struct stat64 *statbuf,
154 zinject_record_t *record)
160 * Before doing any libzpool operations, call sync() to ensure that the
161 * on-disk state is consistent with the in-core state.
165 if ((err = dmu_objset_open(dataset, DMU_OST_ZFS,
166 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) {
167 (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
168 dataset, strerror(err));
172 record->zi_objset = dmu_objset_id(os);
173 record->zi_object = statbuf->st_ino;
175 dmu_objset_close(os);
181 * Calculate the real range based on the type, level, and range given.
184 calculate_range(const char *dataset, err_type_t type, int level, char *range,
185 zinject_record_t *record)
193 * Determine the numeric range from the string.
197 * If range is unspecified, set the range to [0,-1], which
198 * indicates that the whole object should be treated as an
201 record->zi_start = 0;
202 record->zi_end = -1ULL;
206 /* XXX add support for suffixes */
207 record->zi_start = strtoull(range, &end, 10);
211 record->zi_end = record->zi_start + 1;
212 else if (*end == ',')
213 record->zi_end = strtoull(end + 1, &end, 10);
216 (void) fprintf(stderr, "invalid range '%s': must be "
217 "a numeric range of the form 'start[,end]'\n",
229 * If this is a request to inject faults into the dnode, then we
230 * must translate the current (objset,object) pair into an
231 * offset within the metadnode for the objset. Specifying any
232 * kind of range with type 'dnode' is illegal.
235 (void) fprintf(stderr, "range cannot be specified when "
236 "type is 'dnode'\n");
240 record->zi_start = record->zi_object * sizeof (dnode_phys_t);
241 record->zi_end = record->zi_start + sizeof (dnode_phys_t);
242 record->zi_object = 0;
247 * Get the dnode associated with object, so we can calculate the block
250 if ((err = dmu_objset_open(dataset, DMU_OST_ANY,
251 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) {
252 (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
253 dataset, strerror(err));
257 if (record->zi_object == 0) {
258 dn = os->os->os_meta_dnode;
260 err = dnode_hold(os->os, record->zi_object, FTAG, &dn);
262 (void) fprintf(stderr, "failed to hold dnode "
264 (u_longlong_t)record->zi_object);
270 ziprintf("data shift: %d\n", (int)dn->dn_datablkshift);
271 ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift);
274 * Translate range into block IDs.
276 if (record->zi_start != 0 || record->zi_end != -1ULL) {
277 record->zi_start >>= dn->dn_datablkshift;
278 record->zi_end >>= dn->dn_datablkshift;
282 * Check level, and then translate level 0 blkids into ranges
283 * appropriate for level of indirection.
285 record->zi_level = level;
287 ziprintf("level 0 blkid range: [%llu, %llu]\n",
288 record->zi_start, record->zi_end);
290 if (level >= dn->dn_nlevels) {
291 (void) fprintf(stderr, "level %d exceeds max level "
292 "of object (%d)\n", level, dn->dn_nlevels - 1);
296 if (record->zi_start != 0 || record->zi_end != 0) {
297 int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
299 for (; level > 0; level--) {
300 record->zi_start >>= shift;
301 record->zi_end >>= shift;
309 if (dn != os->os->os_meta_dnode)
310 dnode_rele(dn, FTAG);
313 dmu_objset_close(os);
319 translate_record(err_type_t type, const char *object, const char *range,
320 int level, zinject_record_t *record, char *poolname, char *dataset)
322 char path[MAXPATHLEN];
324 struct stat64 statbuf;
329 debug = (getenv("ZINJECT_DEBUG") != NULL);
331 ziprintf("translating: %s\n", object);
333 if (MOS_TYPE(type)) {
335 * MOS objects are treated specially.
342 record->zi_type = DMU_OT_OBJECT_DIRECTORY;
345 record->zi_type = DMU_OT_OBJECT_ARRAY;
348 record->zi_type = DMU_OT_PACKED_NVLIST;
351 record->zi_type = DMU_OT_BPLIST;
354 record->zi_type = DMU_OT_SPACE_MAP;
357 record->zi_type = DMU_OT_ERROR_LOG;
362 (void) strcpy(poolname, object);
367 * Convert a full path into a (dataset, file) pair.
369 if (parse_pathname(object, dataset, path, &statbuf) != 0)
372 ziprintf(" dataset: %s\n", dataset);
373 ziprintf(" path: %s\n", path);
376 * Convert (dataset, file) into (objset, object)
378 if (object_from_path(dataset, path, &statbuf, record) != 0)
381 ziprintf("raw objset: %llu\n", record->zi_objset);
382 ziprintf("raw object: %llu\n", record->zi_object);
385 * For the given object, calculate the real (type, level, range)
387 if (calculate_range(dataset, type, level, (char *)range, record) != 0)
390 ziprintf(" objset: %llu\n", record->zi_objset);
391 ziprintf(" object: %llu\n", record->zi_object);
392 if (record->zi_start == 0 &&
393 record->zi_end == -1ULL)
394 ziprintf(" range: all\n");
396 ziprintf(" range: [%llu, %llu]\n", record->zi_start,
402 (void) strcpy(poolname, dataset);
403 if ((slash = strchr(poolname, '/')) != NULL)
414 translate_raw(const char *str, zinject_record_t *record)
417 * A raw bookmark of the form objset:object:level:blkid, where each
418 * number is a hexidecimal value.
420 if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
421 (u_longlong_t *)&record->zi_object, &record->zi_level,
422 (u_longlong_t *)&record->zi_start) != 4) {
423 (void) fprintf(stderr, "bad raw spec '%s': must be of the form "
424 "'objset:object:level:blkid'\n", str);
428 record->zi_end = record->zi_start;
434 translate_device(const char *pool, const char *device, err_type_t label_type,
435 zinject_record_t *record)
440 boolean_t isspare, iscache;
443 * Given a device name or GUID, create an appropriate injection record
446 if ((zhp = zpool_open(g_zfs, pool)) == NULL)
449 record->zi_guid = strtoull(device, &end, 16);
450 if (record->zi_guid == 0 || *end != '\0') {
451 tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
454 (void) fprintf(stderr, "cannot find device '%s' in "
455 "pool '%s'\n", device, pool);
459 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
460 &record->zi_guid) == 0);
463 switch (label_type) {
464 case TYPE_LABEL_UBERBLOCK:
465 record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
466 record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
468 case TYPE_LABEL_NVLIST:
469 record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
470 record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;