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