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>
55 extern void kernel_init(int);
56 extern void kernel_fini(void);
61 ziprintf(const char *fmt, ...)
69 (void) vprintf(fmt, ap);
74 * Given a full path to a file, translate into a dataset name and a relative
75 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
76 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
77 * buffer, which we need later to get the object ID.
80 parse_pathname(const char *fullpath, char *dataset, char *relpath,
81 struct stat64 *statbuf)
86 if (fullpath[0] != '/') {
87 (void) fprintf(stderr, "invalid object '%s': must be full "
93 if (strlen(fullpath) >= MAXPATHLEN) {
94 (void) fprintf(stderr, "invalid object; pathname too long\n");
98 if (stat64(fullpath, statbuf) != 0) {
99 (void) fprintf(stderr, "cannot open '%s': %s\n",
100 fullpath, strerror(errno));
104 if (statfs(fullpath, &sfs) == -1) {
105 (void) fprintf(stderr, "cannot find mountpoint for '%s': %s\n",
106 fullpath, strerror(errno));
110 if (strcmp(sfs.f_fstypename, MNTTYPE_ZFS) != 0) {
111 (void) fprintf(stderr, "invalid path '%s': not a ZFS "
112 "filesystem\n", fullpath);
116 if (strncmp(fullpath, sfs.f_mntonname, strlen(sfs.f_mntonname)) != 0) {
117 (void) fprintf(stderr, "invalid path '%s': mountpoint "
118 "doesn't match path\n", fullpath);
122 (void) strcpy(dataset, sfs.f_mntfromname);
124 rel = fullpath + strlen(sfs.f_mntonname);
127 (void) strcpy(relpath, rel);
133 * Convert from a (dataset, path) pair into a (objset, object) pair. Note that
134 * we grab the object number from the inode number, since looking this up via
135 * libzpool is a real pain.
139 object_from_path(const char *dataset, const char *path, struct stat64 *statbuf,
140 zinject_record_t *record)
146 * Before doing any libzpool operations, call sync() to ensure that the
147 * on-disk state is consistent with the in-core state.
151 if ((err = dmu_objset_open(dataset, DMU_OST_ZFS,
152 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) {
153 (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
154 dataset, strerror(err));
158 record->zi_objset = dmu_objset_id(os);
159 record->zi_object = statbuf->st_ino;
161 dmu_objset_close(os);
167 * Calculate the real range based on the type, level, and range given.
170 calculate_range(const char *dataset, err_type_t type, int level, char *range,
171 zinject_record_t *record)
179 * Determine the numeric range from the string.
183 * If range is unspecified, set the range to [0,-1], which
184 * indicates that the whole object should be treated as an
187 record->zi_start = 0;
188 record->zi_end = -1ULL;
192 /* XXX add support for suffixes */
193 record->zi_start = strtoull(range, &end, 10);
197 record->zi_end = record->zi_start + 1;
198 else if (*end == ',')
199 record->zi_end = strtoull(end + 1, &end, 10);
202 (void) fprintf(stderr, "invalid range '%s': must be "
203 "a numeric range of the form 'start[,end]'\n",
215 * If this is a request to inject faults into the dnode, then we
216 * must translate the current (objset,object) pair into an
217 * offset within the metadnode for the objset. Specifying any
218 * kind of range with type 'dnode' is illegal.
221 (void) fprintf(stderr, "range cannot be specified when "
222 "type is 'dnode'\n");
226 record->zi_start = record->zi_object * sizeof (dnode_phys_t);
227 record->zi_end = record->zi_start + sizeof (dnode_phys_t);
228 record->zi_object = 0;
233 * Get the dnode associated with object, so we can calculate the block
236 if ((err = dmu_objset_open(dataset, DMU_OST_ANY,
237 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) {
238 (void) fprintf(stderr, "cannot open dataset '%s': %s\n",
239 dataset, strerror(err));
243 if (record->zi_object == 0) {
244 dn = os->os->os_meta_dnode;
246 err = dnode_hold(os->os, record->zi_object, FTAG, &dn);
248 (void) fprintf(stderr, "failed to hold dnode "
250 (u_longlong_t)record->zi_object);
256 ziprintf("data shift: %d\n", (int)dn->dn_datablkshift);
257 ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift);
260 * Translate range into block IDs.
262 if (record->zi_start != 0 || record->zi_end != -1ULL) {
263 record->zi_start >>= dn->dn_datablkshift;
264 record->zi_end >>= dn->dn_datablkshift;
268 * Check level, and then translate level 0 blkids into ranges
269 * appropriate for level of indirection.
271 record->zi_level = level;
273 ziprintf("level 0 blkid range: [%llu, %llu]\n",
274 record->zi_start, record->zi_end);
276 if (level >= dn->dn_nlevels) {
277 (void) fprintf(stderr, "level %d exceeds max level "
278 "of object (%d)\n", level, dn->dn_nlevels - 1);
282 if (record->zi_start != 0 || record->zi_end != 0) {
283 int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
285 for (; level > 0; level--) {
286 record->zi_start >>= shift;
287 record->zi_end >>= shift;
295 if (dn != os->os->os_meta_dnode)
296 dnode_rele(dn, FTAG);
299 dmu_objset_close(os);
305 translate_record(err_type_t type, const char *object, const char *range,
306 int level, zinject_record_t *record, char *poolname, char *dataset)
308 char path[MAXPATHLEN];
310 struct stat64 statbuf;
315 debug = (getenv("ZINJECT_DEBUG") != NULL);
317 ziprintf("translating: %s\n", object);
319 if (MOS_TYPE(type)) {
321 * MOS objects are treated specially.
328 record->zi_type = DMU_OT_OBJECT_DIRECTORY;
331 record->zi_type = DMU_OT_OBJECT_ARRAY;
334 record->zi_type = DMU_OT_PACKED_NVLIST;
337 record->zi_type = DMU_OT_BPLIST;
340 record->zi_type = DMU_OT_SPACE_MAP;
343 record->zi_type = DMU_OT_ERROR_LOG;
348 (void) strcpy(poolname, object);
353 * Convert a full path into a (dataset, file) pair.
355 if (parse_pathname(object, dataset, path, &statbuf) != 0)
358 ziprintf(" dataset: %s\n", dataset);
359 ziprintf(" path: %s\n", path);
362 * Convert (dataset, file) into (objset, object)
364 if (object_from_path(dataset, path, &statbuf, record) != 0)
367 ziprintf("raw objset: %llu\n", record->zi_objset);
368 ziprintf("raw object: %llu\n", record->zi_object);
371 * For the given object, calculate the real (type, level, range)
373 if (calculate_range(dataset, type, level, (char *)range, record) != 0)
376 ziprintf(" objset: %llu\n", record->zi_objset);
377 ziprintf(" object: %llu\n", record->zi_object);
378 if (record->zi_start == 0 &&
379 record->zi_end == -1ULL)
380 ziprintf(" range: all\n");
382 ziprintf(" range: [%llu, %llu]\n", record->zi_start,
388 (void) strcpy(poolname, dataset);
389 if ((slash = strchr(poolname, '/')) != NULL)
400 translate_raw(const char *str, zinject_record_t *record)
403 * A raw bookmark of the form objset:object:level:blkid, where each
404 * number is a hexidecimal value.
406 if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
407 (u_longlong_t *)&record->zi_object, &record->zi_level,
408 (u_longlong_t *)&record->zi_start) != 4) {
409 (void) fprintf(stderr, "bad raw spec '%s': must be of the form "
410 "'objset:object:level:blkid'\n", str);
414 record->zi_end = record->zi_start;
420 translate_device(const char *pool, const char *device, err_type_t label_type,
421 zinject_record_t *record)
426 boolean_t isspare, iscache;
429 * Given a device name or GUID, create an appropriate injection record
432 if ((zhp = zpool_open(g_zfs, pool)) == NULL)
435 record->zi_guid = strtoull(device, &end, 16);
436 if (record->zi_guid == 0 || *end != '\0') {
437 tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
440 (void) fprintf(stderr, "cannot find device '%s' in "
441 "pool '%s'\n", device, pool);
445 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
446 &record->zi_guid) == 0);
449 switch (label_type) {
450 case TYPE_LABEL_UBERBLOCK:
451 record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
452 record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
454 case TYPE_LABEL_NVLIST:
455 record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
456 record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;