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