// SPDX-License-Identifier: CDDL-1.0 /* * This file and its contents are supplied under the terms of the * Common Development and Distribution License ("CDDL"), version 1.0. * You may only use this file in accordance with the terms of version * 1.0 of the CDDL. * * A full copy of the text of the CDDL should have accompanied this * source. A copy of the CDDL is also available via the Internet at * https://opensource.org/license/CDDL-1.0. */ /* * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2020 by Delphix. All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "zinject.h" static int debug; static void ziprintf(const char *fmt, ...) { va_list ap; if (!debug) return; va_start(ap, fmt); (void) vprintf(fmt, ap); va_end(ap); } static void compress_slashes(const char *src, char *dest) { while (*src != '\0') { *dest = *src++; while (*dest == '/' && *src == '/') ++src; ++dest; } *dest = '\0'; } /* * If 'inpath' points to a zvol block device, copy the zvol dataset path to 'ds' * (like "tank/vol"), and return true. If 'inpath' does not point to a zvol * block device, then return false. */ static boolean_t get_zvol_dataset(const char *inpath, char *ds) { boolean_t rc = B_FALSE; char *buf = realpath(inpath, NULL); if (buf == NULL) return (B_FALSE); int fd = open(buf, O_RDONLY | O_CLOEXEC); free(buf); if (fd == -1) return (B_FALSE); /* * We want to determine if 'inpath' is a zvol device. There's no * universal way to do that, so we use OS-specific zvol ioctls. */ #if defined(__FreeBSD__) if (ioctl(fd, DIOCGPHYSPATH, ds) == 0) rc = B_TRUE; #else if (ioctl(fd, BLKZNAME, ds) == 0) rc = B_TRUE; #endif close(fd); return (rc); } /* * Given a full path to a file or zvol device, translate into a dataset name and * a relative path within the dataset. 'dataset' must be at least MAXNAMELEN * characters, and 'relpath' must be at least MAXPATHLEN characters. We also * pass a stat64 buffer, which we need later to get the object ID. */ static int parse_pathname(const char *inpath, char *dataset, char *relpath, struct stat64 *statbuf) { struct mnttab mp; const char *rel; char fullpath[MAXPATHLEN]; compress_slashes(inpath, fullpath); if (fullpath[0] != '/') { (void) fprintf(stderr, "invalid object '%s': must be full " "path\n", fullpath); usage(); return (-1); } /* special case: inject errors into zvol */ if (get_zvol_dataset(inpath, fullpath)) { char *slash; /* * The zvol's inode does not contain its object number. * However, it has long been the case that the zvol data is * object number 1 (ZVOL_OBJ): * * Object lvl iblk dblk dsize lsize %full type * 0 6 128K 16K 11K 16K 6.25 DMU dnode * 1 2 128K 16K 20.1M 20M 100.00 zvol object * 2 1 128K 512 0 512 100.00 zvol prop * * So we hardcode that in the statbuf inode field as workaround. */ statbuf->st_ino = ZVOL_OBJ; (void) strlcpy(dataset, fullpath, MAXNAMELEN); /* * fullpath contains string like 'tank/zvol'. Strip off the * 'tank' and 'zvol' parts. */ slash = strchr(fullpath, '/'); if (slash == NULL) { (void) fprintf(stderr, "invalid volume name: '%s'\n", fullpath); return (-1); } (void) strlcpy(relpath, slash + 1, MAXPATHLEN); return (0); } if (getextmntent(fullpath, &mp, statbuf) != 0) { (void) fprintf(stderr, "cannot find mountpoint for '%s'\n", fullpath); return (-1); } if (strcmp(mp.mnt_fstype, MNTTYPE_ZFS) != 0) { (void) fprintf(stderr, "invalid path '%s': not a ZFS " "filesystem\n", fullpath); return (-1); } if (strncmp(fullpath, mp.mnt_mountp, strlen(mp.mnt_mountp)) != 0) { (void) fprintf(stderr, "invalid path '%s': mountpoint " "doesn't match path\n", fullpath); return (-1); } (void) strlcpy(dataset, mp.mnt_special, MAXNAMELEN); rel = fullpath + strlen(mp.mnt_mountp); if (rel[0] == '/') rel++; (void) strlcpy(relpath, rel, MAXPATHLEN); return (0); } /* * Convert from a dataset to a objset id. Note that * we grab the object number from the inode number. */ static int object_from_path(const char *dataset, uint64_t object, zinject_record_t *record) { zfs_handle_t *zhp; if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL) return (-1); record->zi_objset = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); record->zi_object = object; zfs_close(zhp); return (0); } /* * Initialize the range based on the type, level, and range given. */ static int initialize_range(err_type_t type, int level, char *range, zinject_record_t *record) { /* * Determine the numeric range from the string. */ if (range == NULL) { /* * If range is unspecified, set the range to [0,-1], which * indicates that the whole object should be treated as an * error. */ record->zi_start = 0; record->zi_end = -1ULL; } else { char *comma; int error; comma = strchr(range, ','); if (comma != NULL) *comma = '\0'; error = zfs_nicestrtonum(g_zfs, range, &record->zi_start); if (comma != NULL) *comma = ','; if (error != 0) goto bad_range; if (comma != NULL) { if (zfs_nicestrtonum(g_zfs, comma + 1, &record->zi_end) != 0) goto bad_range; } else { record->zi_end = record->zi_start + 1; } } switch (type) { default: break; case TYPE_DATA: break; case TYPE_DNODE: /* * If this is a request to inject faults into the dnode, then we * must translate the current (objset,object) pair into an * offset within the metadnode for the objset. Specifying any * kind of range with type 'dnode' is illegal. */ if (range != NULL) { (void) fprintf(stderr, "range cannot be specified when " "type is 'dnode'\n"); return (-1); } record->zi_start = record->zi_object * sizeof (dnode_phys_t); record->zi_end = record->zi_start + sizeof (dnode_phys_t); record->zi_object = 0; break; } record->zi_level = level; return (0); bad_range: (void) fprintf(stderr, "invalid range '%s': must be of the form " "'start[,end]'\n", range); return (-1); } int translate_record(err_type_t type, const char *object, const char *range, int level, zinject_record_t *record, char *poolname, char *dataset) { char path[MAXPATHLEN]; char *slash; struct stat64 statbuf; int ret = -1; debug = (getenv("ZINJECT_DEBUG") != NULL); ziprintf("translating: %s\n", object); if (MOS_TYPE(type)) { /* * MOS objects are treated specially. */ switch (type) { default: break; case TYPE_MOS: record->zi_type = 0; break; case TYPE_MOSDIR: record->zi_type = DMU_OT_OBJECT_DIRECTORY; break; case TYPE_METASLAB: record->zi_type = DMU_OT_OBJECT_ARRAY; break; case TYPE_CONFIG: record->zi_type = DMU_OT_PACKED_NVLIST; break; case TYPE_BPOBJ: record->zi_type = DMU_OT_BPOBJ; break; case TYPE_SPACEMAP: record->zi_type = DMU_OT_SPACE_MAP; break; case TYPE_ERRLOG: record->zi_type = DMU_OT_ERROR_LOG; break; } dataset[0] = '\0'; (void) strlcpy(poolname, object, MAXNAMELEN); return (0); } /* * Convert a full path into a (dataset, file) pair. */ if (parse_pathname(object, dataset, path, &statbuf) != 0) goto err; ziprintf(" dataset: %s\n", dataset); ziprintf(" path: %s\n", path); /* * Convert (dataset, file) into (objset, object) */ if (object_from_path(dataset, statbuf.st_ino, record) != 0) goto err; ziprintf("raw objset: %llu\n", record->zi_objset); ziprintf("raw object: %llu\n", record->zi_object); /* * For the given object, initialize the range in bytes */ if (initialize_range(type, level, (char *)range, record) != 0) goto err; ziprintf(" objset: %llu\n", record->zi_objset); ziprintf(" object: %llu\n", record->zi_object); if (record->zi_start == 0 && record->zi_end == -1ULL) ziprintf(" range: all\n"); else ziprintf(" range: [%llu, %llu]\n", record->zi_start, record->zi_end); /* * Copy the pool name */ (void) strlcpy(poolname, dataset, MAXNAMELEN); if ((slash = strchr(poolname, '/')) != NULL) *slash = '\0'; ret = 0; err: return (ret); } int translate_raw(const char *str, zinject_record_t *record) { /* * A raw bookmark of the form objset:object:level:blkid, where each * number is a hexadecimal value. */ if (sscanf(str, "%llx:%llx:%x:%llx", (u_longlong_t *)&record->zi_objset, (u_longlong_t *)&record->zi_object, &record->zi_level, (u_longlong_t *)&record->zi_start) != 4) { (void) fprintf(stderr, "bad raw spec '%s': must be of the form " "'objset:object:level:blkid'\n", str); return (-1); } record->zi_end = record->zi_start; return (0); } int translate_device(const char *pool, const char *device, err_type_t label_type, zinject_record_t *record) { char *end; zpool_handle_t *zhp; nvlist_t *tgt; boolean_t isspare, iscache; /* * Given a device name or GUID, create an appropriate injection record * with zi_guid set. */ if ((zhp = zpool_open(g_zfs, pool)) == NULL) return (-1); record->zi_guid = strtoull(device, &end, 0); if (record->zi_guid == 0 || *end != '\0') { tgt = zpool_find_vdev(zhp, device, &isspare, &iscache, NULL); if (tgt == NULL) { (void) fprintf(stderr, "cannot find device '%s' in " "pool '%s'\n", device, pool); zpool_close(zhp); return (-1); } verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &record->zi_guid) == 0); } /* * Device faults can take on three different forms: * 1). delayed or hanging I/O * 2). zfs label faults * 3). generic disk faults */ if (record->zi_timer != 0) { record->zi_cmd = ZINJECT_DELAY_IO; } else if (label_type != TYPE_INVAL) { record->zi_cmd = ZINJECT_LABEL_FAULT; } else { record->zi_cmd = ZINJECT_DEVICE_FAULT; } switch (label_type) { default: break; case TYPE_LABEL_UBERBLOCK: record->zi_start = offsetof(vdev_label_t, vl_uberblock[0]); record->zi_end = record->zi_start + VDEV_UBERBLOCK_RING - 1; break; case TYPE_LABEL_NVLIST: record->zi_start = offsetof(vdev_label_t, vl_vdev_phys); record->zi_end = record->zi_start + VDEV_PHYS_SIZE - 1; break; case TYPE_LABEL_PAD1: record->zi_start = offsetof(vdev_label_t, vl_pad1); record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1; break; case TYPE_LABEL_PAD2: record->zi_start = offsetof(vdev_label_t, vl_be); record->zi_end = record->zi_start + VDEV_PAD_SIZE - 1; break; } zpool_close(zhp); return (0); }