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