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