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