xref: /titanic_52/usr/src/cmd/zinject/translate.c (revision 503ad85c168c7992ccc310af845a581cff3c72b5)
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 /*
22*503ad85cSMatthew Ahrens  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23ea8dc4b6Seschrock  * Use is subject to license terms.
24ea8dc4b6Seschrock  */
25ea8dc4b6Seschrock 
26ea8dc4b6Seschrock #include <libzfs.h>
27ea8dc4b6Seschrock 
28ea8dc4b6Seschrock #undef verify	/* both libzfs.h and zfs_context.h want to define this */
29ea8dc4b6Seschrock 
30ea8dc4b6Seschrock #include <sys/zfs_context.h>
31ea8dc4b6Seschrock 
32ea8dc4b6Seschrock #include <errno.h>
33ea8dc4b6Seschrock #include <fcntl.h>
34ea8dc4b6Seschrock #include <stdarg.h>
35ea8dc4b6Seschrock #include <stddef.h>
36ea8dc4b6Seschrock #include <stdio.h>
37ea8dc4b6Seschrock #include <stdlib.h>
38ea8dc4b6Seschrock #include <strings.h>
39ea8dc4b6Seschrock #include <sys/file.h>
40ea8dc4b6Seschrock #include <sys/mntent.h>
41ea8dc4b6Seschrock #include <sys/mnttab.h>
42ea8dc4b6Seschrock #include <sys/param.h>
43ea8dc4b6Seschrock #include <sys/stat.h>
44ea8dc4b6Seschrock 
45ea8dc4b6Seschrock #include <sys/dmu.h>
46ea8dc4b6Seschrock #include <sys/dmu_objset.h>
47ea8dc4b6Seschrock #include <sys/dnode.h>
4821bf64a7Sgw25295 #include <sys/vdev_impl.h>
49ea8dc4b6Seschrock 
50ea8dc4b6Seschrock #include <sys/mkdev.h>
51ea8dc4b6Seschrock 
52ea8dc4b6Seschrock #include "zinject.h"
53ea8dc4b6Seschrock 
54ea8dc4b6Seschrock extern void kernel_init(int);
55ea8dc4b6Seschrock extern void kernel_fini(void);
56ea8dc4b6Seschrock 
57ea8dc4b6Seschrock static int debug;
58ea8dc4b6Seschrock 
59ea8dc4b6Seschrock static void
60ea8dc4b6Seschrock ziprintf(const char *fmt, ...)
61ea8dc4b6Seschrock {
62ea8dc4b6Seschrock 	va_list ap;
63ea8dc4b6Seschrock 
64ea8dc4b6Seschrock 	if (!debug)
65ea8dc4b6Seschrock 		return;
66ea8dc4b6Seschrock 
67ea8dc4b6Seschrock 	va_start(ap, fmt);
68ea8dc4b6Seschrock 	(void) vprintf(fmt, ap);
69ea8dc4b6Seschrock 	va_end(ap);
70ea8dc4b6Seschrock }
71ea8dc4b6Seschrock 
72ea8dc4b6Seschrock /*
73ea8dc4b6Seschrock  * Given a full path to a file, translate into a dataset name and a relative
74ea8dc4b6Seschrock  * path within the dataset.  'dataset' must be at least MAXNAMELEN characters,
75ea8dc4b6Seschrock  * and 'relpath' must be at least MAXPATHLEN characters.  We also pass a stat64
76ea8dc4b6Seschrock  * buffer, which we need later to get the object ID.
77ea8dc4b6Seschrock  */
78ea8dc4b6Seschrock static int
79ea8dc4b6Seschrock parse_pathname(const char *fullpath, char *dataset, char *relpath,
80ea8dc4b6Seschrock     struct stat64 *statbuf)
81ea8dc4b6Seschrock {
82ea8dc4b6Seschrock 	struct extmnttab mp;
83ea8dc4b6Seschrock 	FILE *fp;
84ea8dc4b6Seschrock 	int match;
85ea8dc4b6Seschrock 	const char *rel;
86ea8dc4b6Seschrock 
87ea8dc4b6Seschrock 	if (fullpath[0] != '/') {
88ea8dc4b6Seschrock 		(void) fprintf(stderr, "invalid object '%s': must be full "
89ea8dc4b6Seschrock 		    "path\n", fullpath);
90ea8dc4b6Seschrock 		usage();
91ea8dc4b6Seschrock 		return (-1);
92ea8dc4b6Seschrock 	}
93ea8dc4b6Seschrock 
94ea8dc4b6Seschrock 	if (strlen(fullpath) >= MAXPATHLEN) {
95ea8dc4b6Seschrock 		(void) fprintf(stderr, "invalid object; pathname too long\n");
96ea8dc4b6Seschrock 		return (-1);
97ea8dc4b6Seschrock 	}
98ea8dc4b6Seschrock 
99ea8dc4b6Seschrock 	if (stat64(fullpath, statbuf) != 0) {
100ea8dc4b6Seschrock 		(void) fprintf(stderr, "cannot open '%s': %s\n",
101ea8dc4b6Seschrock 		    fullpath, strerror(errno));
102ea8dc4b6Seschrock 		return (-1);
103ea8dc4b6Seschrock 	}
104ea8dc4b6Seschrock 
105ea8dc4b6Seschrock 	if ((fp = fopen(MNTTAB, "r")) == NULL) {
106ea8dc4b6Seschrock 		(void) fprintf(stderr, "cannot open /etc/mnttab\n");
107ea8dc4b6Seschrock 		return (-1);
108ea8dc4b6Seschrock 	}
109ea8dc4b6Seschrock 
110ea8dc4b6Seschrock 	match = 0;
111ea8dc4b6Seschrock 	while (getextmntent(fp, &mp, sizeof (mp)) == 0) {
112ea8dc4b6Seschrock 		if (makedev(mp.mnt_major, mp.mnt_minor) == statbuf->st_dev) {
113ea8dc4b6Seschrock 			match = 1;
114ea8dc4b6Seschrock 			break;
115ea8dc4b6Seschrock 		}
116ea8dc4b6Seschrock 	}
117ea8dc4b6Seschrock 
118ea8dc4b6Seschrock 	if (!match) {
119ea8dc4b6Seschrock 		(void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
120ea8dc4b6Seschrock 		    fullpath);
121ea8dc4b6Seschrock 		return (-1);
122ea8dc4b6Seschrock 	}
123ea8dc4b6Seschrock 
124ea8dc4b6Seschrock 	if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) {
125ea8dc4b6Seschrock 		(void) fprintf(stderr, "invalid path '%s': not a ZFS "
126ea8dc4b6Seschrock 		    "filesystem\n", fullpath);
127ea8dc4b6Seschrock 		return (-1);
128ea8dc4b6Seschrock 	}
129ea8dc4b6Seschrock 
130ea8dc4b6Seschrock 	if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) {
131ea8dc4b6Seschrock 		(void) fprintf(stderr, "invalid path '%s': mountpoint "
132ea8dc4b6Seschrock 		    "doesn't match path\n", fullpath);
133ea8dc4b6Seschrock 		return (-1);
134ea8dc4b6Seschrock 	}
135ea8dc4b6Seschrock 
136ea8dc4b6Seschrock 	(void) strcpy(dataset, mp.mnt_special);
137ea8dc4b6Seschrock 
138ea8dc4b6Seschrock 	rel = fullpath + strlen(mp.mnt_mountp);
139ea8dc4b6Seschrock 	if (rel[0] == '/')
140ea8dc4b6Seschrock 		rel++;
141ea8dc4b6Seschrock 	(void) strcpy(relpath, rel);
142ea8dc4b6Seschrock 
143ea8dc4b6Seschrock 	return (0);
144ea8dc4b6Seschrock }
145ea8dc4b6Seschrock 
146ea8dc4b6Seschrock /*
147ea8dc4b6Seschrock  * Convert from a (dataset, path) pair into a (objset, object) pair.  Note that
148ea8dc4b6Seschrock  * we grab the object number from the inode number, since looking this up via
149ea8dc4b6Seschrock  * libzpool is a real pain.
150ea8dc4b6Seschrock  */
151ea8dc4b6Seschrock /* ARGSUSED */
152ea8dc4b6Seschrock static int
153ea8dc4b6Seschrock object_from_path(const char *dataset, const char *path, struct stat64 *statbuf,
154ea8dc4b6Seschrock     zinject_record_t *record)
155ea8dc4b6Seschrock {
156ea8dc4b6Seschrock 	objset_t *os;
157ea8dc4b6Seschrock 	int err;
158ea8dc4b6Seschrock 
159ea8dc4b6Seschrock 	/*
160ea8dc4b6Seschrock 	 * Before doing any libzpool operations, call sync() to ensure that the
161ea8dc4b6Seschrock 	 * on-disk state is consistent with the in-core state.
162ea8dc4b6Seschrock 	 */
163ea8dc4b6Seschrock 	sync();
164ea8dc4b6Seschrock 
165*503ad85cSMatthew Ahrens 	err = dmu_objset_own(dataset, DMU_OST_ZFS, B_TRUE, FTAG, &os);
166*503ad85cSMatthew Ahrens 	if (err != 0) {
167ea8dc4b6Seschrock 		(void) fprintf(stderr, "cannot open dataset '%s': %s\n",
168ea8dc4b6Seschrock 		    dataset, strerror(err));
169ea8dc4b6Seschrock 		return (-1);
170ea8dc4b6Seschrock 	}
171ea8dc4b6Seschrock 
172ea8dc4b6Seschrock 	record->zi_objset = dmu_objset_id(os);
173ea8dc4b6Seschrock 	record->zi_object = statbuf->st_ino;
174ea8dc4b6Seschrock 
175*503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
176ea8dc4b6Seschrock 
177ea8dc4b6Seschrock 	return (0);
178ea8dc4b6Seschrock }
179ea8dc4b6Seschrock 
180ea8dc4b6Seschrock /*
181ea8dc4b6Seschrock  * Calculate the real range based on the type, level, and range given.
182ea8dc4b6Seschrock  */
183ea8dc4b6Seschrock static int
184ea8dc4b6Seschrock calculate_range(const char *dataset, err_type_t type, int level, char *range,
185ea8dc4b6Seschrock     zinject_record_t *record)
186ea8dc4b6Seschrock {
187ea8dc4b6Seschrock 	objset_t *os = NULL;
188ea8dc4b6Seschrock 	dnode_t *dn = NULL;
189ea8dc4b6Seschrock 	int err;
190ea8dc4b6Seschrock 	int ret = -1;
191ea8dc4b6Seschrock 
192ea8dc4b6Seschrock 	/*
193ea8dc4b6Seschrock 	 * Determine the numeric range from the string.
194ea8dc4b6Seschrock 	 */
195ea8dc4b6Seschrock 	if (range == NULL) {
196ea8dc4b6Seschrock 		/*
197ea8dc4b6Seschrock 		 * If range is unspecified, set the range to [0,-1], which
198ea8dc4b6Seschrock 		 * indicates that the whole object should be treated as an
199ea8dc4b6Seschrock 		 * error.
200ea8dc4b6Seschrock 		 */
201ea8dc4b6Seschrock 		record->zi_start = 0;
202ea8dc4b6Seschrock 		record->zi_end = -1ULL;
203ea8dc4b6Seschrock 	} else {
204ea8dc4b6Seschrock 		char *end;
205ea8dc4b6Seschrock 
206ea8dc4b6Seschrock 		/* XXX add support for suffixes */
207ea8dc4b6Seschrock 		record->zi_start = strtoull(range, &end, 10);
208ea8dc4b6Seschrock 
209ea8dc4b6Seschrock 
210ea8dc4b6Seschrock 		if (*end == '\0')
211ea8dc4b6Seschrock 			record->zi_end = record->zi_start + 1;
212ea8dc4b6Seschrock 		else if (*end == ',')
213ea8dc4b6Seschrock 			record->zi_end = strtoull(end + 1, &end, 10);
214ea8dc4b6Seschrock 
215ea8dc4b6Seschrock 		if (*end != '\0') {
216ea8dc4b6Seschrock 			(void) fprintf(stderr, "invalid range '%s': must be "
217ea8dc4b6Seschrock 			    "a numeric range of the form 'start[,end]'\n",
218ea8dc4b6Seschrock 			    range);
219ea8dc4b6Seschrock 			goto out;
220ea8dc4b6Seschrock 		}
221ea8dc4b6Seschrock 	}
222ea8dc4b6Seschrock 
223ea8dc4b6Seschrock 	switch (type) {
224ea8dc4b6Seschrock 	case TYPE_DATA:
225ea8dc4b6Seschrock 		break;
226ea8dc4b6Seschrock 
227ea8dc4b6Seschrock 	case TYPE_DNODE:
228ea8dc4b6Seschrock 		/*
229ea8dc4b6Seschrock 		 * If this is a request to inject faults into the dnode, then we
230ea8dc4b6Seschrock 		 * must translate the current (objset,object) pair into an
231ea8dc4b6Seschrock 		 * offset within the metadnode for the objset.  Specifying any
232ea8dc4b6Seschrock 		 * kind of range with type 'dnode' is illegal.
233ea8dc4b6Seschrock 		 */
234ea8dc4b6Seschrock 		if (range != NULL) {
235ea8dc4b6Seschrock 			(void) fprintf(stderr, "range cannot be specified when "
236ea8dc4b6Seschrock 			    "type is 'dnode'\n");
237ea8dc4b6Seschrock 			goto out;
238ea8dc4b6Seschrock 		}
239ea8dc4b6Seschrock 
240ea8dc4b6Seschrock 		record->zi_start = record->zi_object * sizeof (dnode_phys_t);
241ea8dc4b6Seschrock 		record->zi_end = record->zi_start + sizeof (dnode_phys_t);
242ea8dc4b6Seschrock 		record->zi_object = 0;
243ea8dc4b6Seschrock 		break;
244ea8dc4b6Seschrock 	}
245ea8dc4b6Seschrock 
246ea8dc4b6Seschrock 	/*
247ea8dc4b6Seschrock 	 * Get the dnode associated with object, so we can calculate the block
248ea8dc4b6Seschrock 	 * size.
249ea8dc4b6Seschrock 	 */
250*503ad85cSMatthew Ahrens 	if ((err = dmu_objset_own(dataset, DMU_OST_ANY,
251*503ad85cSMatthew Ahrens 	    B_TRUE, FTAG, &os)) != 0) {
252ea8dc4b6Seschrock 		(void) fprintf(stderr, "cannot open dataset '%s': %s\n",
253ea8dc4b6Seschrock 		    dataset, strerror(err));
254ea8dc4b6Seschrock 		goto out;
255ea8dc4b6Seschrock 	}
256ea8dc4b6Seschrock 
257ea8dc4b6Seschrock 	if (record->zi_object == 0) {
258*503ad85cSMatthew Ahrens 		dn = os->os_meta_dnode;
259ea8dc4b6Seschrock 	} else {
260*503ad85cSMatthew Ahrens 		err = dnode_hold(os, record->zi_object, FTAG, &dn);
261ea8dc4b6Seschrock 		if (err != 0) {
262ea8dc4b6Seschrock 			(void) fprintf(stderr, "failed to hold dnode "
263ea8dc4b6Seschrock 			    "for object %llu\n",
264ea8dc4b6Seschrock 			    (u_longlong_t)record->zi_object);
265ea8dc4b6Seschrock 			goto out;
266ea8dc4b6Seschrock 		}
267ea8dc4b6Seschrock 	}
268ea8dc4b6Seschrock 
269ea8dc4b6Seschrock 
270ea8dc4b6Seschrock 	ziprintf("data shift: %d\n", (int)dn->dn_datablkshift);
271ea8dc4b6Seschrock 	ziprintf(" ind shift: %d\n", (int)dn->dn_indblkshift);
272ea8dc4b6Seschrock 
273ea8dc4b6Seschrock 	/*
274ea8dc4b6Seschrock 	 * Translate range into block IDs.
275ea8dc4b6Seschrock 	 */
276ea8dc4b6Seschrock 	if (record->zi_start != 0 || record->zi_end != -1ULL) {
277ea8dc4b6Seschrock 		record->zi_start >>= dn->dn_datablkshift;
278ea8dc4b6Seschrock 		record->zi_end >>= dn->dn_datablkshift;
279ea8dc4b6Seschrock 	}
280ea8dc4b6Seschrock 
281ea8dc4b6Seschrock 	/*
282ea8dc4b6Seschrock 	 * Check level, and then translate level 0 blkids into ranges
283ea8dc4b6Seschrock 	 * appropriate for level of indirection.
284ea8dc4b6Seschrock 	 */
285ea8dc4b6Seschrock 	record->zi_level = level;
286ea8dc4b6Seschrock 	if (level > 0) {
287ea8dc4b6Seschrock 		ziprintf("level 0 blkid range: [%llu, %llu]\n",
288ea8dc4b6Seschrock 		    record->zi_start, record->zi_end);
289ea8dc4b6Seschrock 
290ea8dc4b6Seschrock 		if (level >= dn->dn_nlevels) {
291ea8dc4b6Seschrock 			(void) fprintf(stderr, "level %d exceeds max level "
292ea8dc4b6Seschrock 			    "of object (%d)\n", level, dn->dn_nlevels - 1);
293ea8dc4b6Seschrock 			goto out;
294ea8dc4b6Seschrock 		}
295ea8dc4b6Seschrock 
296ea8dc4b6Seschrock 		if (record->zi_start != 0 || record->zi_end != 0) {
297ea8dc4b6Seschrock 			int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
298ea8dc4b6Seschrock 
299ea8dc4b6Seschrock 			for (; level > 0; level--) {
300ea8dc4b6Seschrock 				record->zi_start >>= shift;
301ea8dc4b6Seschrock 				record->zi_end >>= shift;
302ea8dc4b6Seschrock 			}
303ea8dc4b6Seschrock 		}
304ea8dc4b6Seschrock 	}
305ea8dc4b6Seschrock 
306ea8dc4b6Seschrock 	ret = 0;
307ea8dc4b6Seschrock out:
308ea8dc4b6Seschrock 	if (dn) {
309*503ad85cSMatthew Ahrens 		if (dn != os->os_meta_dnode)
310ea8dc4b6Seschrock 			dnode_rele(dn, FTAG);
311ea8dc4b6Seschrock 	}
312ea8dc4b6Seschrock 	if (os)
313*503ad85cSMatthew Ahrens 		dmu_objset_disown(os, FTAG);
314ea8dc4b6Seschrock 
315ea8dc4b6Seschrock 	return (ret);
316ea8dc4b6Seschrock }
317ea8dc4b6Seschrock 
318ea8dc4b6Seschrock int
319ea8dc4b6Seschrock translate_record(err_type_t type, const char *object, const char *range,
320ea8dc4b6Seschrock     int level, zinject_record_t *record, char *poolname, char *dataset)
321ea8dc4b6Seschrock {
322ea8dc4b6Seschrock 	char path[MAXPATHLEN];
323ea8dc4b6Seschrock 	char *slash;
324ea8dc4b6Seschrock 	struct stat64 statbuf;
325ea8dc4b6Seschrock 	int ret = -1;
326ea8dc4b6Seschrock 
327ea8dc4b6Seschrock 	kernel_init(FREAD);
328ea8dc4b6Seschrock 
329ea8dc4b6Seschrock 	debug = (getenv("ZINJECT_DEBUG") != NULL);
330ea8dc4b6Seschrock 
331ea8dc4b6Seschrock 	ziprintf("translating: %s\n", object);
332ea8dc4b6Seschrock 
333ea8dc4b6Seschrock 	if (MOS_TYPE(type)) {
334ea8dc4b6Seschrock 		/*
335ea8dc4b6Seschrock 		 * MOS objects are treated specially.
336ea8dc4b6Seschrock 		 */
337ea8dc4b6Seschrock 		switch (type) {
338ea8dc4b6Seschrock 		case TYPE_MOS:
339ea8dc4b6Seschrock 			record->zi_type = 0;
340ea8dc4b6Seschrock 			break;
341ea8dc4b6Seschrock 		case TYPE_MOSDIR:
342ea8dc4b6Seschrock 			record->zi_type = DMU_OT_OBJECT_DIRECTORY;
343ea8dc4b6Seschrock 			break;
344ea8dc4b6Seschrock 		case TYPE_METASLAB:
345ea8dc4b6Seschrock 			record->zi_type = DMU_OT_OBJECT_ARRAY;
346ea8dc4b6Seschrock 			break;
347ea8dc4b6Seschrock 		case TYPE_CONFIG:
348ea8dc4b6Seschrock 			record->zi_type = DMU_OT_PACKED_NVLIST;
349ea8dc4b6Seschrock 			break;
350ea8dc4b6Seschrock 		case TYPE_BPLIST:
351ea8dc4b6Seschrock 			record->zi_type = DMU_OT_BPLIST;
352ea8dc4b6Seschrock 			break;
353ea8dc4b6Seschrock 		case TYPE_SPACEMAP:
354ea8dc4b6Seschrock 			record->zi_type = DMU_OT_SPACE_MAP;
355ea8dc4b6Seschrock 			break;
356ea8dc4b6Seschrock 		case TYPE_ERRLOG:
357ea8dc4b6Seschrock 			record->zi_type = DMU_OT_ERROR_LOG;
358ea8dc4b6Seschrock 			break;
359ea8dc4b6Seschrock 		}
360ea8dc4b6Seschrock 
361ea8dc4b6Seschrock 		dataset[0] = '\0';
362ea8dc4b6Seschrock 		(void) strcpy(poolname, object);
363ea8dc4b6Seschrock 		return (0);
364ea8dc4b6Seschrock 	}
365ea8dc4b6Seschrock 
366ea8dc4b6Seschrock 	/*
367ea8dc4b6Seschrock 	 * Convert a full path into a (dataset, file) pair.
368ea8dc4b6Seschrock 	 */
369ea8dc4b6Seschrock 	if (parse_pathname(object, dataset, path, &statbuf) != 0)
370ea8dc4b6Seschrock 		goto err;
371ea8dc4b6Seschrock 
372ea8dc4b6Seschrock 	ziprintf("   dataset: %s\n", dataset);
373ea8dc4b6Seschrock 	ziprintf("      path: %s\n", path);
374ea8dc4b6Seschrock 
375ea8dc4b6Seschrock 	/*
376ea8dc4b6Seschrock 	 * Convert (dataset, file) into (objset, object)
377ea8dc4b6Seschrock 	 */
378ea8dc4b6Seschrock 	if (object_from_path(dataset, path, &statbuf, record) != 0)
379ea8dc4b6Seschrock 		goto err;
380ea8dc4b6Seschrock 
381ea8dc4b6Seschrock 	ziprintf("raw objset: %llu\n", record->zi_objset);
382ea8dc4b6Seschrock 	ziprintf("raw object: %llu\n", record->zi_object);
383ea8dc4b6Seschrock 
384ea8dc4b6Seschrock 	/*
385ea8dc4b6Seschrock 	 * For the given object, calculate the real (type, level, range)
386ea8dc4b6Seschrock 	 */
387ea8dc4b6Seschrock 	if (calculate_range(dataset, type, level, (char *)range, record) != 0)
388ea8dc4b6Seschrock 		goto err;
389ea8dc4b6Seschrock 
390ea8dc4b6Seschrock 	ziprintf("    objset: %llu\n", record->zi_objset);
391ea8dc4b6Seschrock 	ziprintf("    object: %llu\n", record->zi_object);
392ea8dc4b6Seschrock 	if (record->zi_start == 0 &&
393ea8dc4b6Seschrock 	    record->zi_end == -1ULL)
394ea8dc4b6Seschrock 		ziprintf("     range: all\n");
395ea8dc4b6Seschrock 	else
396ea8dc4b6Seschrock 		ziprintf("     range: [%llu, %llu]\n", record->zi_start,
397ea8dc4b6Seschrock 		    record->zi_end);
398ea8dc4b6Seschrock 
399ea8dc4b6Seschrock 	/*
400ea8dc4b6Seschrock 	 * Copy the pool name
401ea8dc4b6Seschrock 	 */
402ea8dc4b6Seschrock 	(void) strcpy(poolname, dataset);
403ea8dc4b6Seschrock 	if ((slash = strchr(poolname, '/')) != NULL)
404ea8dc4b6Seschrock 		*slash = '\0';
405ea8dc4b6Seschrock 
406ea8dc4b6Seschrock 	ret = 0;
407ea8dc4b6Seschrock 
408ea8dc4b6Seschrock err:
409ea8dc4b6Seschrock 	kernel_fini();
410ea8dc4b6Seschrock 	return (ret);
411ea8dc4b6Seschrock }
412ea8dc4b6Seschrock 
413ea8dc4b6Seschrock int
414ea8dc4b6Seschrock translate_raw(const char *str, zinject_record_t *record)
415ea8dc4b6Seschrock {
416ea8dc4b6Seschrock 	/*
417ea8dc4b6Seschrock 	 * A raw bookmark of the form objset:object:level:blkid, where each
418ea8dc4b6Seschrock 	 * number is a hexidecimal value.
419ea8dc4b6Seschrock 	 */
420ea8dc4b6Seschrock 	if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
421ea8dc4b6Seschrock 	    (u_longlong_t *)&record->zi_object, &record->zi_level,
422ea8dc4b6Seschrock 	    (u_longlong_t *)&record->zi_start) != 4) {
423ea8dc4b6Seschrock 		(void) fprintf(stderr, "bad raw spec '%s': must be of the form "
424ea8dc4b6Seschrock 		    "'objset:object:level:blkid'\n", str);
425ea8dc4b6Seschrock 		return (-1);
426ea8dc4b6Seschrock 	}
427ea8dc4b6Seschrock 
428ea8dc4b6Seschrock 	record->zi_end = record->zi_start;
429ea8dc4b6Seschrock 
430ea8dc4b6Seschrock 	return (0);
431ea8dc4b6Seschrock }
432ea8dc4b6Seschrock 
433ea8dc4b6Seschrock int
43421bf64a7Sgw25295 translate_device(const char *pool, const char *device, err_type_t label_type,
43521bf64a7Sgw25295     zinject_record_t *record)
436ea8dc4b6Seschrock {
437ea8dc4b6Seschrock 	char *end;
438ea8dc4b6Seschrock 	zpool_handle_t *zhp;
43999653d4eSeschrock 	nvlist_t *tgt;
440fa94a07fSbrendan 	boolean_t isspare, iscache;
441ea8dc4b6Seschrock 
442ea8dc4b6Seschrock 	/*
443ea8dc4b6Seschrock 	 * Given a device name or GUID, create an appropriate injection record
444ea8dc4b6Seschrock 	 * with zi_guid set.
445ea8dc4b6Seschrock 	 */
44699653d4eSeschrock 	if ((zhp = zpool_open(g_zfs, pool)) == NULL)
447ea8dc4b6Seschrock 		return (-1);
448ea8dc4b6Seschrock 
449ea8dc4b6Seschrock 	record->zi_guid = strtoull(device, &end, 16);
45099653d4eSeschrock 	if (record->zi_guid == 0 || *end != '\0') {
451ee0eb9f2SEric Schrock 		tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
452ea8dc4b6Seschrock 
45399653d4eSeschrock 		if (tgt == NULL) {
45499653d4eSeschrock 			(void) fprintf(stderr, "cannot find device '%s' in "
45599653d4eSeschrock 			    "pool '%s'\n", device, pool);
456ea8dc4b6Seschrock 			return (-1);
457ea8dc4b6Seschrock 		}
458ea8dc4b6Seschrock 
45999653d4eSeschrock 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
46099653d4eSeschrock 		    &record->zi_guid) == 0);
46199653d4eSeschrock 	}
46299653d4eSeschrock 
46321bf64a7Sgw25295 	switch (label_type) {
46421bf64a7Sgw25295 	case TYPE_LABEL_UBERBLOCK:
46521bf64a7Sgw25295 		record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
46621bf64a7Sgw25295 		record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
46721bf64a7Sgw25295 		break;
46821bf64a7Sgw25295 	case TYPE_LABEL_NVLIST:
46921bf64a7Sgw25295 		record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
47021bf64a7Sgw25295 		record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;
47121bf64a7Sgw25295 		break;
47221bf64a7Sgw25295 	}
473ea8dc4b6Seschrock 	return (0);
474ea8dc4b6Seschrock }
475