xref: /freebsd/sys/contrib/openzfs/cmd/zinject/translate.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25  */
26 
27 #include <libzfs.h>
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdarg.h>
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/file.h>
37 #include <sys/mntent.h>
38 #include <sys/mnttab.h>
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 
42 #include <sys/dmu.h>
43 #include <sys/dmu_objset.h>
44 #include <sys/dnode.h>
45 #include <sys/vdev_impl.h>
46 
47 #include <sys/mkdev.h>
48 
49 #include "zinject.h"
50 
51 static int debug;
52 
53 static void
ziprintf(const char * fmt,...)54 ziprintf(const char *fmt, ...)
55 {
56 	va_list ap;
57 
58 	if (!debug)
59 		return;
60 
61 	va_start(ap, fmt);
62 	(void) vprintf(fmt, ap);
63 	va_end(ap);
64 }
65 
66 static void
compress_slashes(const char * src,char * dest)67 compress_slashes(const char *src, char *dest)
68 {
69 	while (*src != '\0') {
70 		*dest = *src++;
71 		while (*dest == '/' && *src == '/')
72 			++src;
73 		++dest;
74 	}
75 	*dest = '\0';
76 }
77 
78 /*
79  * Given a full path to a file, translate into a dataset name and a relative
80  * path within the dataset.  'dataset' must be at least MAXNAMELEN characters,
81  * and 'relpath' must be at least MAXPATHLEN characters.  We also pass a stat64
82  * buffer, which we need later to get the object ID.
83  */
84 static int
parse_pathname(const char * inpath,char * dataset,char * relpath,struct stat64 * statbuf)85 parse_pathname(const char *inpath, char *dataset, char *relpath,
86     struct stat64 *statbuf)
87 {
88 	struct extmnttab mp;
89 	const char *rel;
90 	char fullpath[MAXPATHLEN];
91 
92 	compress_slashes(inpath, fullpath);
93 
94 	if (fullpath[0] != '/') {
95 		(void) fprintf(stderr, "invalid object '%s': must be full "
96 		    "path\n", fullpath);
97 		usage();
98 		return (-1);
99 	}
100 
101 	if (getextmntent(fullpath, &mp, statbuf) != 0) {
102 		(void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
103 		    fullpath);
104 		return (-1);
105 	}
106 
107 	if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) {
108 		(void) fprintf(stderr, "invalid path '%s': not a ZFS "
109 		    "filesystem\n", fullpath);
110 		return (-1);
111 	}
112 
113 	if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) {
114 		(void) fprintf(stderr, "invalid path '%s': mountpoint "
115 		    "doesn't match path\n", fullpath);
116 		return (-1);
117 	}
118 
119 	(void) strlcpy(dataset, mp.mnt_special, MAXNAMELEN);
120 
121 	rel = fullpath + strlen(mp.mnt_mountp);
122 	if (rel[0] == '/')
123 		rel++;
124 	(void) strlcpy(relpath, rel, MAXPATHLEN);
125 
126 	return (0);
127 }
128 
129 /*
130  * Convert from a dataset to a objset id. Note that
131  * we grab the object number from the inode number.
132  */
133 static int
object_from_path(const char * dataset,uint64_t object,zinject_record_t * record)134 object_from_path(const char *dataset, uint64_t object, zinject_record_t *record)
135 {
136 	zfs_handle_t *zhp;
137 
138 	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL)
139 		return (-1);
140 
141 	record->zi_objset = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
142 	record->zi_object = object;
143 
144 	zfs_close(zhp);
145 
146 	return (0);
147 }
148 
149 /*
150  * Initialize the range based on the type, level, and range given.
151  */
152 static int
initialize_range(err_type_t type,int level,char * range,zinject_record_t * record)153 initialize_range(err_type_t type, int level, char *range,
154     zinject_record_t *record)
155 {
156 	/*
157 	 * Determine the numeric range from the string.
158 	 */
159 	if (range == NULL) {
160 		/*
161 		 * If range is unspecified, set the range to [0,-1], which
162 		 * indicates that the whole object should be treated as an
163 		 * error.
164 		 */
165 		record->zi_start = 0;
166 		record->zi_end = -1ULL;
167 	} else {
168 		char *end;
169 
170 		/* XXX add support for suffixes */
171 		record->zi_start = strtoull(range, &end, 10);
172 
173 
174 		if (*end == '\0')
175 			record->zi_end = record->zi_start + 1;
176 		else if (*end == ',')
177 			record->zi_end = strtoull(end + 1, &end, 10);
178 
179 		if (*end != '\0') {
180 			(void) fprintf(stderr, "invalid range '%s': must be "
181 			    "a numeric range of the form 'start[,end]'\n",
182 			    range);
183 			return (-1);
184 		}
185 	}
186 
187 	switch (type) {
188 	default:
189 		break;
190 
191 	case TYPE_DATA:
192 		break;
193 
194 	case TYPE_DNODE:
195 		/*
196 		 * If this is a request to inject faults into the dnode, then we
197 		 * must translate the current (objset,object) pair into an
198 		 * offset within the metadnode for the objset.  Specifying any
199 		 * kind of range with type 'dnode' is illegal.
200 		 */
201 		if (range != NULL) {
202 			(void) fprintf(stderr, "range cannot be specified when "
203 			    "type is 'dnode'\n");
204 			return (-1);
205 		}
206 
207 		record->zi_start = record->zi_object * sizeof (dnode_phys_t);
208 		record->zi_end = record->zi_start + sizeof (dnode_phys_t);
209 		record->zi_object = 0;
210 		break;
211 	}
212 
213 	record->zi_level = level;
214 
215 	return (0);
216 }
217 
218 int
translate_record(err_type_t type,const char * object,const char * range,int level,zinject_record_t * record,char * poolname,char * dataset)219 translate_record(err_type_t type, const char *object, const char *range,
220     int level, zinject_record_t *record, char *poolname, char *dataset)
221 {
222 	char path[MAXPATHLEN];
223 	char *slash;
224 	struct stat64 statbuf;
225 	int ret = -1;
226 
227 	debug = (getenv("ZINJECT_DEBUG") != NULL);
228 
229 	ziprintf("translating: %s\n", object);
230 
231 	if (MOS_TYPE(type)) {
232 		/*
233 		 * MOS objects are treated specially.
234 		 */
235 		switch (type) {
236 		default:
237 			break;
238 		case TYPE_MOS:
239 			record->zi_type = 0;
240 			break;
241 		case TYPE_MOSDIR:
242 			record->zi_type = DMU_OT_OBJECT_DIRECTORY;
243 			break;
244 		case TYPE_METASLAB:
245 			record->zi_type = DMU_OT_OBJECT_ARRAY;
246 			break;
247 		case TYPE_CONFIG:
248 			record->zi_type = DMU_OT_PACKED_NVLIST;
249 			break;
250 		case TYPE_BPOBJ:
251 			record->zi_type = DMU_OT_BPOBJ;
252 			break;
253 		case TYPE_SPACEMAP:
254 			record->zi_type = DMU_OT_SPACE_MAP;
255 			break;
256 		case TYPE_ERRLOG:
257 			record->zi_type = DMU_OT_ERROR_LOG;
258 			break;
259 		}
260 
261 		dataset[0] = '\0';
262 		(void) strlcpy(poolname, object, MAXNAMELEN);
263 		return (0);
264 	}
265 
266 	/*
267 	 * Convert a full path into a (dataset, file) pair.
268 	 */
269 	if (parse_pathname(object, dataset, path, &statbuf) != 0)
270 		goto err;
271 
272 	ziprintf("   dataset: %s\n", dataset);
273 	ziprintf("      path: %s\n", path);
274 
275 	/*
276 	 * Convert (dataset, file) into (objset, object)
277 	 */
278 	if (object_from_path(dataset, statbuf.st_ino, record) != 0)
279 		goto err;
280 
281 	ziprintf("raw objset: %llu\n", record->zi_objset);
282 	ziprintf("raw object: %llu\n", record->zi_object);
283 
284 	/*
285 	 * For the given object, initialize the range in bytes
286 	 */
287 	if (initialize_range(type, level, (char *)range, record) != 0)
288 		goto err;
289 
290 	ziprintf("    objset: %llu\n", record->zi_objset);
291 	ziprintf("    object: %llu\n", record->zi_object);
292 	if (record->zi_start == 0 &&
293 	    record->zi_end == -1ULL)
294 		ziprintf("     range: all\n");
295 	else
296 		ziprintf("     range: [%llu, %llu]\n", record->zi_start,
297 		    record->zi_end);
298 
299 	/*
300 	 * Copy the pool name
301 	 */
302 	(void) strlcpy(poolname, dataset, MAXNAMELEN);
303 	if ((slash = strchr(poolname, '/')) != NULL)
304 		*slash = '\0';
305 
306 	ret = 0;
307 
308 err:
309 	return (ret);
310 }
311 
312 int
translate_raw(const char * str,zinject_record_t * record)313 translate_raw(const char *str, zinject_record_t *record)
314 {
315 	/*
316 	 * A raw bookmark of the form objset:object:level:blkid, where each
317 	 * number is a hexadecimal value.
318 	 */
319 	if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset,
320 	    (u_longlong_t *)&record->zi_object, &record->zi_level,
321 	    (u_longlong_t *)&record->zi_start) != 4) {
322 		(void) fprintf(stderr, "bad raw spec '%s': must be of the form "
323 		    "'objset:object:level:blkid'\n", str);
324 		return (-1);
325 	}
326 
327 	record->zi_end = record->zi_start;
328 
329 	return (0);
330 }
331 
332 int
translate_device(const char * pool,const char * device,err_type_t label_type,zinject_record_t * record)333 translate_device(const char *pool, const char *device, err_type_t label_type,
334     zinject_record_t *record)
335 {
336 	char *end;
337 	zpool_handle_t *zhp;
338 	nvlist_t *tgt;
339 	boolean_t isspare, iscache;
340 
341 	/*
342 	 * Given a device name or GUID, create an appropriate injection record
343 	 * with zi_guid set.
344 	 */
345 	if ((zhp = zpool_open(g_zfs, pool)) == NULL)
346 		return (-1);
347 
348 	record->zi_guid = strtoull(device, &end, 0);
349 	if (record->zi_guid == 0 || *end != '\0') {
350 		tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL);
351 
352 		if (tgt == NULL) {
353 			(void) fprintf(stderr, "cannot find device '%s' in "
354 			    "pool '%s'\n", device, pool);
355 			zpool_close(zhp);
356 			return (-1);
357 		}
358 
359 		verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
360 		    &record->zi_guid) == 0);
361 	}
362 
363 	/*
364 	 * Device faults can take on three different forms:
365 	 * 1). delayed or hanging I/O
366 	 * 2). zfs label faults
367 	 * 3). generic disk faults
368 	 */
369 	if (record->zi_timer != 0) {
370 		record->zi_cmd = ZINJECT_DELAY_IO;
371 	} else if (label_type != TYPE_INVAL) {
372 		record->zi_cmd = ZINJECT_LABEL_FAULT;
373 	} else {
374 		record->zi_cmd = ZINJECT_DEVICE_FAULT;
375 	}
376 
377 	switch (label_type) {
378 	default:
379 		break;
380 	case TYPE_LABEL_UBERBLOCK:
381 		record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]);
382 		record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1;
383 		break;
384 	case TYPE_LABEL_NVLIST:
385 		record->zi_start = offsetof(vdev_label_t, vl_vdev_phys);
386 		record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1;
387 		break;
388 	case TYPE_LABEL_PAD1:
389 		record->zi_start = offsetof(vdev_label_t, vl_pad1);
390 		record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
391 		break;
392 	case TYPE_LABEL_PAD2:
393 		record->zi_start = offsetof(vdev_label_t, vl_be);
394 		record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1;
395 		break;
396 	}
397 	zpool_close(zhp);
398 	return (0);
399 }
400