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 https://opensource.org/licenses/CDDL-1.0.
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.
23 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
36 #include <sys/mntent.h>
37 #include <sys/mnttab.h>
38 #include <sys/param.h>
42 #include <sys/dmu_objset.h>
43 #include <sys/dnode.h>
44 #include <sys/vdev_impl.h>
46 #include <sys/mkdev.h>
53 ziprintf(const char *fmt
, ...)
61 (void) vprintf(fmt
, ap
);
66 compress_slashes(const char *src
, char *dest
)
68 while (*src
!= '\0') {
70 while (*dest
== '/' && *src
== '/')
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.
84 parse_pathname(const char *inpath
, char *dataset
, char *relpath
,
85 struct stat64
*statbuf
)
89 char fullpath
[MAXPATHLEN
];
91 compress_slashes(inpath
, fullpath
);
93 if (fullpath
[0] != '/') {
94 (void) fprintf(stderr
, "invalid object '%s': must be full "
100 if (getextmntent(fullpath
, &mp
, statbuf
) != 0) {
101 (void) fprintf(stderr
, "cannot find mountpoint for '%s'\n",
106 if (strcmp(mp
.mnt_fstype
, MNTTYPE_ZFS
) != 0) {
107 (void) fprintf(stderr
, "invalid path '%s': not a ZFS "
108 "filesystem\n", fullpath
);
112 if (strncmp(fullpath
, mp
.mnt_mountp
, strlen(mp
.mnt_mountp
)) != 0) {
113 (void) fprintf(stderr
, "invalid path '%s': mountpoint "
114 "doesn't match path\n", fullpath
);
118 (void) strlcpy(dataset
, mp
.mnt_special
, MAXNAMELEN
);
120 rel
= fullpath
+ strlen(mp
.mnt_mountp
);
123 (void) strlcpy(relpath
, rel
, MAXPATHLEN
);
129 * Convert from a dataset to a objset id. Note that
130 * we grab the object number from the inode number.
133 object_from_path(const char *dataset
, uint64_t object
, zinject_record_t
*record
)
137 if ((zhp
= zfs_open(g_zfs
, dataset
, ZFS_TYPE_DATASET
)) == NULL
)
140 record
->zi_objset
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
141 record
->zi_object
= object
;
149 * Initialize the range based on the type, level, and range given.
152 initialize_range(err_type_t type
, int level
, char *range
,
153 zinject_record_t
*record
)
156 * Determine the numeric range from the string.
160 * If range is unspecified, set the range to [0,-1], which
161 * indicates that the whole object should be treated as an
164 record
->zi_start
= 0;
165 record
->zi_end
= -1ULL;
169 /* XXX add support for suffixes */
170 record
->zi_start
= strtoull(range
, &end
, 10);
174 record
->zi_end
= record
->zi_start
+ 1;
175 else if (*end
== ',')
176 record
->zi_end
= strtoull(end
+ 1, &end
, 10);
179 (void) fprintf(stderr
, "invalid range '%s': must be "
180 "a numeric range of the form 'start[,end]'\n",
195 * If this is a request to inject faults into the dnode, then we
196 * must translate the current (objset,object) pair into an
197 * offset within the metadnode for the objset. Specifying any
198 * kind of range with type 'dnode' is illegal.
201 (void) fprintf(stderr
, "range cannot be specified when "
202 "type is 'dnode'\n");
206 record
->zi_start
= record
->zi_object
* sizeof (dnode_phys_t
);
207 record
->zi_end
= record
->zi_start
+ sizeof (dnode_phys_t
);
208 record
->zi_object
= 0;
212 record
->zi_level
= level
;
218 translate_record(err_type_t type
, const char *object
, const char *range
,
219 int level
, zinject_record_t
*record
, char *poolname
, char *dataset
)
221 char path
[MAXPATHLEN
];
223 struct stat64 statbuf
;
226 debug
= (getenv("ZINJECT_DEBUG") != NULL
);
228 ziprintf("translating: %s\n", object
);
230 if (MOS_TYPE(type
)) {
232 * MOS objects are treated specially.
241 record
->zi_type
= DMU_OT_OBJECT_DIRECTORY
;
244 record
->zi_type
= DMU_OT_OBJECT_ARRAY
;
247 record
->zi_type
= DMU_OT_PACKED_NVLIST
;
250 record
->zi_type
= DMU_OT_BPOBJ
;
253 record
->zi_type
= DMU_OT_SPACE_MAP
;
256 record
->zi_type
= DMU_OT_ERROR_LOG
;
261 (void) strlcpy(poolname
, object
, MAXNAMELEN
);
266 * Convert a full path into a (dataset, file) pair.
268 if (parse_pathname(object
, dataset
, path
, &statbuf
) != 0)
271 ziprintf(" dataset: %s\n", dataset
);
272 ziprintf(" path: %s\n", path
);
275 * Convert (dataset, file) into (objset, object)
277 if (object_from_path(dataset
, statbuf
.st_ino
, record
) != 0)
280 ziprintf("raw objset: %llu\n", record
->zi_objset
);
281 ziprintf("raw object: %llu\n", record
->zi_object
);
284 * For the given object, initialize the range in bytes
286 if (initialize_range(type
, level
, (char *)range
, record
) != 0)
289 ziprintf(" objset: %llu\n", record
->zi_objset
);
290 ziprintf(" object: %llu\n", record
->zi_object
);
291 if (record
->zi_start
== 0 &&
292 record
->zi_end
== -1ULL)
293 ziprintf(" range: all\n");
295 ziprintf(" range: [%llu, %llu]\n", record
->zi_start
,
301 (void) strlcpy(poolname
, dataset
, MAXNAMELEN
);
302 if ((slash
= strchr(poolname
, '/')) != NULL
)
312 translate_raw(const char *str
, zinject_record_t
*record
)
315 * A raw bookmark of the form objset:object:level:blkid, where each
316 * number is a hexadecimal value.
318 if (sscanf(str
, "%llx:%llx:%x:%llx", (u_longlong_t
*)&record
->zi_objset
,
319 (u_longlong_t
*)&record
->zi_object
, &record
->zi_level
,
320 (u_longlong_t
*)&record
->zi_start
) != 4) {
321 (void) fprintf(stderr
, "bad raw spec '%s': must be of the form "
322 "'objset:object:level:blkid'\n", str
);
326 record
->zi_end
= record
->zi_start
;
332 translate_device(const char *pool
, const char *device
, err_type_t label_type
,
333 zinject_record_t
*record
)
338 boolean_t isspare
, iscache
;
341 * Given a device name or GUID, create an appropriate injection record
344 if ((zhp
= zpool_open(g_zfs
, pool
)) == NULL
)
347 record
->zi_guid
= strtoull(device
, &end
, 0);
348 if (record
->zi_guid
== 0 || *end
!= '\0') {
349 tgt
= zpool_find_vdev(zhp
, device
, &isspare
, &iscache
, NULL
);
352 (void) fprintf(stderr
, "cannot find device '%s' in "
353 "pool '%s'\n", device
, pool
);
358 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
359 &record
->zi_guid
) == 0);
363 * Device faults can take on three different forms:
364 * 1). delayed or hanging I/O
365 * 2). zfs label faults
366 * 3). generic disk faults
368 if (record
->zi_timer
!= 0) {
369 record
->zi_cmd
= ZINJECT_DELAY_IO
;
370 } else if (label_type
!= TYPE_INVAL
) {
371 record
->zi_cmd
= ZINJECT_LABEL_FAULT
;
373 record
->zi_cmd
= ZINJECT_DEVICE_FAULT
;
376 switch (label_type
) {
379 case TYPE_LABEL_UBERBLOCK
:
380 record
->zi_start
= offsetof(vdev_label_t
, vl_uberblock
[0]);
381 record
->zi_end
= record
->zi_start
+ VDEV_UBERBLOCK_RING
- 1;
383 case TYPE_LABEL_NVLIST
:
384 record
->zi_start
= offsetof(vdev_label_t
, vl_vdev_phys
);
385 record
->zi_end
= record
->zi_start
+ VDEV_PHYS_SIZE
- 1;
387 case TYPE_LABEL_PAD1
:
388 record
->zi_start
= offsetof(vdev_label_t
, vl_pad1
);
389 record
->zi_end
= record
->zi_start
+ VDEV_PAD_SIZE
- 1;
391 case TYPE_LABEL_PAD2
:
392 record
->zi_start
= offsetof(vdev_label_t
, vl_be
);
393 record
->zi_end
= record
->zi_start
+ VDEV_PAD_SIZE
- 1;