]>
git.proxmox.com Git - mirror_zfs.git/blob - zfs/zcmd/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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)translate.c 1.3 07/11/09 SMI"
30 #undef verify /* both libzfs.h and zfs_context.h want to define this */
32 #include <sys/zfs_context.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
44 #include <sys/param.h>
48 #include <sys/dmu_objset.h>
49 #include <sys/dnode.h>
51 #include <sys/mkdev.h>
55 extern void kernel_init(int);
56 extern void kernel_fini(void);
61 ziprintf(const char *fmt
, ...)
69 (void) vprintf(fmt
, ap
);
74 * Given a full path to a file, translate into a dataset name and a relative
75 * path within the dataset. 'dataset' must be at least MAXNAMELEN characters,
76 * and 'relpath' must be at least MAXPATHLEN characters. We also pass a stat64
77 * buffer, which we need later to get the object ID.
80 parse_pathname(const char *fullpath
, char *dataset
, char *relpath
,
81 struct stat64
*statbuf
)
88 if (fullpath
[0] != '/') {
89 (void) fprintf(stderr
, "invalid object '%s': must be full "
95 if (strlen(fullpath
) >= MAXPATHLEN
) {
96 (void) fprintf(stderr
, "invalid object; pathname too long\n");
100 if (stat64(fullpath
, statbuf
) != 0) {
101 (void) fprintf(stderr
, "cannot open '%s': %s\n",
102 fullpath
, strerror(errno
));
106 if ((fp
= fopen(MNTTAB
, "r")) == NULL
) {
107 (void) fprintf(stderr
, "cannot open /etc/mnttab\n");
112 while (getextmntent(fp
, &mp
, sizeof (mp
)) == 0) {
113 if (makedev(mp
.mnt_major
, mp
.mnt_minor
) == statbuf
->st_dev
) {
120 (void) fprintf(stderr
, "cannot find mountpoint for '%s'\n",
125 if (strcmp(mp
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
126 (void) fprintf(stderr
, "invalid path '%s': not a ZFS "
127 "filesystem\n", fullpath
);
131 if (strncmp(fullpath
, mp
.mnt_mountp
, strlen(mp
.mnt_mountp
)) != 0) {
132 (void) fprintf(stderr
, "invalid path '%s': mountpoint "
133 "doesn't match path\n", fullpath
);
137 (void) strcpy(dataset
, mp
.mnt_special
);
139 rel
= fullpath
+ strlen(mp
.mnt_mountp
);
142 (void) strcpy(relpath
, rel
);
148 * Convert from a (dataset, path) pair into a (objset, object) pair. Note that
149 * we grab the object number from the inode number, since looking this up via
150 * libzpool is a real pain.
154 object_from_path(const char *dataset
, const char *path
, struct stat64
*statbuf
,
155 zinject_record_t
*record
)
161 * Before doing any libzpool operations, call sync() to ensure that the
162 * on-disk state is consistent with the in-core state.
166 if ((err
= dmu_objset_open(dataset
, DMU_OST_ZFS
,
167 DS_MODE_STANDARD
| DS_MODE_READONLY
, &os
)) != 0) {
168 (void) fprintf(stderr
, "cannot open dataset '%s': %s\n",
169 dataset
, strerror(err
));
173 record
->zi_objset
= dmu_objset_id(os
);
174 record
->zi_object
= statbuf
->st_ino
;
176 dmu_objset_close(os
);
182 * Calculate the real range based on the type, level, and range given.
185 calculate_range(const char *dataset
, err_type_t type
, int level
, char *range
,
186 zinject_record_t
*record
)
194 * Determine the numeric range from the string.
198 * If range is unspecified, set the range to [0,-1], which
199 * indicates that the whole object should be treated as an
202 record
->zi_start
= 0;
203 record
->zi_end
= -1ULL;
207 /* XXX add support for suffixes */
208 record
->zi_start
= strtoull(range
, &end
, 10);
212 record
->zi_end
= record
->zi_start
+ 1;
213 else if (*end
== ',')
214 record
->zi_end
= strtoull(end
+ 1, &end
, 10);
217 (void) fprintf(stderr
, "invalid range '%s': must be "
218 "a numeric range of the form 'start[,end]'\n",
230 * If this is a request to inject faults into the dnode, then we
231 * must translate the current (objset,object) pair into an
232 * offset within the metadnode for the objset. Specifying any
233 * kind of range with type 'dnode' is illegal.
236 (void) fprintf(stderr
, "range cannot be specified when "
237 "type is 'dnode'\n");
241 record
->zi_start
= record
->zi_object
* sizeof (dnode_phys_t
);
242 record
->zi_end
= record
->zi_start
+ sizeof (dnode_phys_t
);
243 record
->zi_object
= 0;
248 * Get the dnode associated with object, so we can calculate the block
251 if ((err
= dmu_objset_open(dataset
, DMU_OST_ANY
,
252 DS_MODE_STANDARD
| DS_MODE_READONLY
, &os
)) != 0) {
253 (void) fprintf(stderr
, "cannot open dataset '%s': %s\n",
254 dataset
, strerror(err
));
258 if (record
->zi_object
== 0) {
259 dn
= os
->os
->os_meta_dnode
;
261 err
= dnode_hold(os
->os
, record
->zi_object
, FTAG
, &dn
);
263 (void) fprintf(stderr
, "failed to hold dnode "
265 (u_longlong_t
)record
->zi_object
);
271 ziprintf("data shift: %d\n", (int)dn
->dn_datablkshift
);
272 ziprintf(" ind shift: %d\n", (int)dn
->dn_indblkshift
);
275 * Translate range into block IDs.
277 if (record
->zi_start
!= 0 || record
->zi_end
!= -1ULL) {
278 record
->zi_start
>>= dn
->dn_datablkshift
;
279 record
->zi_end
>>= dn
->dn_datablkshift
;
283 * Check level, and then translate level 0 blkids into ranges
284 * appropriate for level of indirection.
286 record
->zi_level
= level
;
288 ziprintf("level 0 blkid range: [%llu, %llu]\n",
289 record
->zi_start
, record
->zi_end
);
291 if (level
>= dn
->dn_nlevels
) {
292 (void) fprintf(stderr
, "level %d exceeds max level "
293 "of object (%d)\n", level
, dn
->dn_nlevels
- 1);
297 if (record
->zi_start
!= 0 || record
->zi_end
!= 0) {
298 int shift
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
300 for (; level
> 0; level
--) {
301 record
->zi_start
>>= shift
;
302 record
->zi_end
>>= shift
;
310 if (dn
!= os
->os
->os_meta_dnode
)
311 dnode_rele(dn
, FTAG
);
314 dmu_objset_close(os
);
320 translate_record(err_type_t type
, const char *object
, const char *range
,
321 int level
, zinject_record_t
*record
, char *poolname
, char *dataset
)
323 char path
[MAXPATHLEN
];
325 struct stat64 statbuf
;
330 debug
= (getenv("ZINJECT_DEBUG") != NULL
);
332 ziprintf("translating: %s\n", object
);
334 if (MOS_TYPE(type
)) {
336 * MOS objects are treated specially.
343 record
->zi_type
= DMU_OT_OBJECT_DIRECTORY
;
346 record
->zi_type
= DMU_OT_OBJECT_ARRAY
;
349 record
->zi_type
= DMU_OT_PACKED_NVLIST
;
352 record
->zi_type
= DMU_OT_BPLIST
;
355 record
->zi_type
= DMU_OT_SPACE_MAP
;
358 record
->zi_type
= DMU_OT_ERROR_LOG
;
363 (void) strcpy(poolname
, object
);
368 * Convert a full path into a (dataset, file) pair.
370 if (parse_pathname(object
, dataset
, path
, &statbuf
) != 0)
373 ziprintf(" dataset: %s\n", dataset
);
374 ziprintf(" path: %s\n", path
);
377 * Convert (dataset, file) into (objset, object)
379 if (object_from_path(dataset
, path
, &statbuf
, record
) != 0)
382 ziprintf("raw objset: %llu\n", record
->zi_objset
);
383 ziprintf("raw object: %llu\n", record
->zi_object
);
386 * For the given object, calculate the real (type, level, range)
388 if (calculate_range(dataset
, type
, level
, (char *)range
, record
) != 0)
391 ziprintf(" objset: %llu\n", record
->zi_objset
);
392 ziprintf(" object: %llu\n", record
->zi_object
);
393 if (record
->zi_start
== 0 &&
394 record
->zi_end
== -1ULL)
395 ziprintf(" range: all\n");
397 ziprintf(" range: [%llu, %llu]\n", record
->zi_start
,
403 (void) strcpy(poolname
, dataset
);
404 if ((slash
= strchr(poolname
, '/')) != NULL
)
415 translate_raw(const char *str
, zinject_record_t
*record
)
418 * A raw bookmark of the form objset:object:level:blkid, where each
419 * number is a hexidecimal value.
421 if (sscanf(str
, "%llx:%llx:%x:%llx", (u_longlong_t
*)&record
->zi_objset
,
422 (u_longlong_t
*)&record
->zi_object
, &record
->zi_level
,
423 (u_longlong_t
*)&record
->zi_start
) != 4) {
424 (void) fprintf(stderr
, "bad raw spec '%s': must be of the form "
425 "'objset:object:level:blkid'\n", str
);
429 record
->zi_end
= record
->zi_start
;
435 translate_device(const char *pool
, const char *device
, zinject_record_t
*record
)
440 boolean_t isspare
, iscache
;
443 * Given a device name or GUID, create an appropriate injection record
446 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
)
449 record
->zi_guid
= strtoull(device
, &end
, 16);
450 if (record
->zi_guid
== 0 || *end
!= '\0') {
451 tgt
= zpool_find_vdev(zhp
, device
, &isspare
, &iscache
);
454 (void) fprintf(stderr
, "cannot find device '%s' in "
455 "pool '%s'\n", device
, pool
);
459 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
460 &record
->zi_guid
) == 0);