]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - cmd/zinject/translate.c
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.
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.
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]
22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
27 #include <sys/zfs_context.h>
37 #include <sys/mntent.h>
38 #include <sys/mnttab.h>
39 #include <sys/param.h>
43 #include <sys/dmu_objset.h>
44 #include <sys/dnode.h>
45 #include <sys/vdev_impl.h>
47 #include <sys/mkdev.h>
51 extern void kernel_init(int);
52 extern void kernel_fini(void);
57 ziprintf(const char *fmt
, ...)
65 (void) vprintf(fmt
, ap
);
70 compress_slashes(const char *src
, char *dest
)
72 while (*src
!= '\0') {
74 while (*dest
== '/' && *src
== '/')
82 * Given a full path to a file, translate into a dataset name and a relative
83 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
84 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
85 * buffer, which we need later to get the object ID.
88 parse_pathname(const char *inpath
, char *dataset
, char *relpath
,
89 struct stat64
*statbuf
)
95 char fullpath
[MAXPATHLEN
];
97 compress_slashes(inpath
, fullpath
);
99 if (fullpath
[0] != '/') {
100 (void) fprintf(stderr
, "invalid object '%s': must be full "
106 if (strlen(fullpath
) >= MAXPATHLEN
) {
107 (void) fprintf(stderr
, "invalid object; pathname too long\n");
111 if (stat64(fullpath
, statbuf
) != 0) {
112 (void) fprintf(stderr
, "cannot open '%s': %s\n",
113 fullpath
, strerror(errno
));
117 if ((fp
= fopen(MNTTAB
, "r")) == NULL
) {
118 (void) fprintf(stderr
, "cannot open /etc/mtab\n");
123 while (getextmntent(fp
, &mp
, sizeof (mp
)) == 0) {
124 if (makedev(mp
.mnt_major
, mp
.mnt_minor
) == statbuf
->st_dev
) {
131 (void) fprintf(stderr
, "cannot find mountpoint for '%s'\n",
136 if (strcmp(mp
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
137 (void) fprintf(stderr
, "invalid path '%s': not a ZFS "
138 "filesystem\n", fullpath
);
142 if (strncmp(fullpath
, mp
.mnt_mountp
, strlen(mp
.mnt_mountp
)) != 0) {
143 (void) fprintf(stderr
, "invalid path '%s': mountpoint "
144 "doesn't match path\n", fullpath
);
148 (void) strcpy(dataset
, mp
.mnt_special
);
150 rel
= fullpath
+ strlen(mp
.mnt_mountp
);
153 (void) strcpy(relpath
, rel
);
159 * Convert from a (dataset, path) pair into a (objset, object) pair. Note that
160 * we grab the object number from the inode number, since looking this up via
161 * libzpool is a real pain.
165 object_from_path(const char *dataset
, const char *path
, struct stat64
*statbuf
,
166 zinject_record_t
*record
)
172 * Before doing any libzpool operations, call sync() to ensure that the
173 * on-disk state is consistent with the in-core state.
177 err
= dmu_objset_own(dataset
, DMU_OST_ZFS
, B_TRUE
, FTAG
, &os
);
179 (void) fprintf(stderr
, "cannot open dataset '%s': %s\n",
180 dataset
, strerror(err
));
184 record
->zi_objset
= dmu_objset_id(os
);
185 record
->zi_object
= statbuf
->st_ino
;
187 dmu_objset_disown(os
, FTAG
);
193 * Calculate the real range based on the type, level, and range given.
196 calculate_range(const char *dataset
, err_type_t type
, int level
, char *range
,
197 zinject_record_t
*record
)
205 * Determine the numeric range from the string.
209 * If range is unspecified, set the range to [0,-1], which
210 * indicates that the whole object should be treated as an
213 record
->zi_start
= 0;
214 record
->zi_end
= -1ULL;
218 /* XXX add support for suffixes */
219 record
->zi_start
= strtoull(range
, &end
, 10);
223 record
->zi_end
= record
->zi_start
+ 1;
224 else if (*end
== ',')
225 record
->zi_end
= strtoull(end
+ 1, &end
, 10);
228 (void) fprintf(stderr
, "invalid range '%s': must be "
229 "a numeric range of the form 'start[,end]'\n",
243 * If this is a request to inject faults into the dnode, then we
244 * must translate the current (objset,object) pair into an
245 * offset within the metadnode for the objset. Specifying any
246 * kind of range with type 'dnode' is illegal.
249 (void) fprintf(stderr
, "range cannot be specified when "
250 "type is 'dnode'\n");
254 record
->zi_start
= record
->zi_object
* sizeof (dnode_phys_t
);
255 record
->zi_end
= record
->zi_start
+ sizeof (dnode_phys_t
);
256 record
->zi_object
= 0;
261 * Get the dnode associated with object, so we can calculate the block
264 if ((err
= dmu_objset_own(dataset
, DMU_OST_ANY
,
265 B_TRUE
, FTAG
, &os
)) != 0) {
266 (void) fprintf(stderr
, "cannot open dataset '%s': %s\n",
267 dataset
, strerror(err
));
271 if (record
->zi_object
== 0) {
272 dn
= DMU_META_DNODE(os
);
274 err
= dnode_hold(os
, record
->zi_object
, FTAG
, &dn
);
276 (void) fprintf(stderr
, "failed to hold dnode "
278 (u_longlong_t
)record
->zi_object
);
284 ziprintf("data shift: %d\n", (int)dn
->dn_datablkshift
);
285 ziprintf(" ind shift: %d\n", (int)dn
->dn_indblkshift
);
288 * Translate range into block IDs.
290 if (record
->zi_start
!= 0 || record
->zi_end
!= -1ULL) {
291 record
->zi_start
>>= dn
->dn_datablkshift
;
292 record
->zi_end
>>= dn
->dn_datablkshift
;
296 * Check level, and then translate level 0 blkids into ranges
297 * appropriate for level of indirection.
299 record
->zi_level
= level
;
301 ziprintf("level 0 blkid range: [%llu, %llu]\n",
302 record
->zi_start
, record
->zi_end
);
304 if (level
>= dn
->dn_nlevels
) {
305 (void) fprintf(stderr
, "level %d exceeds max level "
306 "of object (%d)\n", level
, dn
->dn_nlevels
- 1);
310 if (record
->zi_start
!= 0 || record
->zi_end
!= 0) {
311 int shift
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
313 for (; level
> 0; level
--) {
314 record
->zi_start
>>= shift
;
315 record
->zi_end
>>= shift
;
323 if (dn
!= DMU_META_DNODE(os
))
324 dnode_rele(dn
, FTAG
);
327 dmu_objset_disown(os
, FTAG
);
333 translate_record(err_type_t type
, const char *object
, const char *range
,
334 int level
, zinject_record_t
*record
, char *poolname
, char *dataset
)
336 char path
[MAXPATHLEN
];
338 struct stat64 statbuf
;
343 debug
= (getenv("ZINJECT_DEBUG") != NULL
);
345 ziprintf("translating: %s\n", object
);
347 if (MOS_TYPE(type
)) {
349 * MOS objects are treated specially.
358 record
->zi_type
= DMU_OT_OBJECT_DIRECTORY
;
361 record
->zi_type
= DMU_OT_OBJECT_ARRAY
;
364 record
->zi_type
= DMU_OT_PACKED_NVLIST
;
367 record
->zi_type
= DMU_OT_BPOBJ
;
370 record
->zi_type
= DMU_OT_SPACE_MAP
;
373 record
->zi_type
= DMU_OT_ERROR_LOG
;
378 (void) strcpy(poolname
, object
);
383 * Convert a full path into a (dataset, file) pair.
385 if (parse_pathname(object
, dataset
, path
, &statbuf
) != 0)
388 ziprintf(" dataset: %s\n", dataset
);
389 ziprintf(" path: %s\n", path
);
392 * Convert (dataset, file) into (objset, object)
394 if (object_from_path(dataset
, path
, &statbuf
, record
) != 0)
397 ziprintf("raw objset: %llu\n", record
->zi_objset
);
398 ziprintf("raw object: %llu\n", record
->zi_object
);
401 * For the given object, calculate the real (type, level, range)
403 if (calculate_range(dataset
, type
, level
, (char *)range
, record
) != 0)
406 ziprintf(" objset: %llu\n", record
->zi_objset
);
407 ziprintf(" object: %llu\n", record
->zi_object
);
408 if (record
->zi_start
== 0 &&
409 record
->zi_end
== -1ULL)
410 ziprintf(" range: all\n");
412 ziprintf(" range: [%llu, %llu]\n", record
->zi_start
,
418 (void) strcpy(poolname
, dataset
);
419 if ((slash
= strchr(poolname
, '/')) != NULL
)
430 translate_raw(const char *str
, zinject_record_t
*record
)
433 * A raw bookmark of the form objset:object:level:blkid, where each
434 * number is a hexidecimal value.
436 if (sscanf(str
, "%llx:%llx:%x:%llx", (u_longlong_t
*)&record
->zi_objset
,
437 (u_longlong_t
*)&record
->zi_object
, &record
->zi_level
,
438 (u_longlong_t
*)&record
->zi_start
) != 4) {
439 (void) fprintf(stderr
, "bad raw spec '%s': must be of the form "
440 "'objset:object:level:blkid'\n", str
);
444 record
->zi_end
= record
->zi_start
;
450 translate_device(const char *pool
, const char *device
, err_type_t label_type
,
451 zinject_record_t
*record
)
456 boolean_t isspare
, iscache
;
459 * Given a device name or GUID, create an appropriate injection record
462 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
)
465 record
->zi_guid
= strtoull(device
, &end
, 16);
466 if (record
->zi_guid
== 0 || *end
!= '\0') {
467 tgt
= zpool_find_vdev(zhp
, device
, &isspare
, &iscache
, NULL
);
470 (void) fprintf(stderr
, "cannot find device '%s' in "
471 "pool '%s'\n", device
, pool
);
475 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
476 &record
->zi_guid
) == 0);
479 switch (label_type
) {
482 case TYPE_LABEL_UBERBLOCK
:
483 record
->zi_start
= offsetof(vdev_label_t
, vl_uberblock
[0]);
484 record
->zi_end
= record
->zi_start
+ VDEV_UBERBLOCK_RING
- 1;
486 case TYPE_LABEL_NVLIST
:
487 record
->zi_start
= offsetof(vdev_label_t
, vl_vdev_phys
);
488 record
->zi_end
= record
->zi_start
+ VDEV_PHYS_SIZE
- 1;
490 case TYPE_LABEL_PAD1
:
491 record
->zi_start
= offsetof(vdev_label_t
, vl_pad1
);
492 record
->zi_end
= record
->zi_start
+ VDEV_PAD_SIZE
- 1;
494 case TYPE_LABEL_PAD2
:
495 record
->zi_start
= offsetof(vdev_label_t
, vl_pad2
);
496 record
->zi_end
= record
->zi_start
+ VDEV_PAD_SIZE
- 1;