diff --git a/cmd/zinject/zinject.c b/cmd/zinject/zinject.c index 8be659278616..113797c878b9 100644 --- a/cmd/zinject/zinject.c +++ b/cmd/zinject/zinject.c @@ -1,1465 +1,1468 @@ // SPDX-License-Identifier: CDDL-1.0 /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or https://opensource.org/licenses/CDDL-1.0. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2015 by Delphix. All rights reserved. * Copyright (c) 2017, Intel Corporation. * Copyright (c) 2023-2025, Klara, Inc. */ /* * ZFS Fault Injector * * This userland component takes a set of options and uses libzpool to translate * from a user-visible object type and name to an internal representation. * There are two basic types of faults: device faults and data faults. * * * DEVICE FAULTS * * Errors can be injected into a particular vdev using the '-d' option. This * option takes a path or vdev GUID to uniquely identify the device within a * pool. There are four types of errors that can be injected, IO, ENXIO, * ECHILD, and EILSEQ. These can be controlled through the '-e' option and the * default is ENXIO. For EIO failures, any attempt to read data from the device * will return EIO, but a subsequent attempt to reopen the device will succeed. * For ENXIO failures, any attempt to read from the device will return EIO, but * any attempt to reopen the device will also return ENXIO. The EILSEQ failures * only apply to read operations (-T read) and will flip a bit after the device * has read the original data. * * For label faults, the -L option must be specified. This allows faults * to be injected into either the nvlist, uberblock, pad1, or pad2 region * of all the labels for the specified device. * * This form of the command looks like: * * zinject -d device [-e errno] [-L ] pool * * * DATA FAULTS * * We begin with a tuple of the form: * * * * type A string describing the type of data to target. Each type * implicitly describes how to interpret 'object'. Currently, * the following values are supported: * * data User data for a file * dnode Dnode for a file or directory * * The following MOS objects are special. Instead of injecting * errors on a particular object or blkid, we inject errors across * all objects of the given type. * * mos Any data in the MOS * mosdir object directory * config pool configuration * bpobj blkptr list * spacemap spacemap * metaslab metaslab * errlog persistent error log * * level Object level. Defaults to '0', not applicable to all types. If * a range is given, this corresponds to the indirect block * corresponding to the specific range. * * range A numerical range [start,end) within the object. Defaults to * the full size of the file. * * object A string describing the logical location of the object. For * files and directories (currently the only supported types), * this is the path of the object on disk. * * This is translated, via libzpool, into the following internal representation: * * * * These types should be self-explanatory. This tuple is then passed to the * kernel via a special ioctl() to initiate fault injection for the given * object. Note that 'type' is not strictly necessary for fault injection, but * is used when translating existing faults into a human-readable string. * * * The command itself takes one of the forms: * * zinject * zinject <-a | -u pool> * zinject -c * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level] * [-r range] * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool * * With no arguments, the command prints all currently registered injection * handlers, with their numeric identifiers. * * The '-c' option will clear the given handler, or all handlers if 'all' is * specified. * * The '-e' option takes a string describing the errno to simulate. This must * be one of 'io', 'checksum', 'decompress', or 'decrypt'. In most cases this * will result in the same behavior, but RAID-Z will produce a different set of * ereports for this situation. * * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is * specified, then the ARC cache is flushed appropriately. If '-u' is * specified, then the underlying SPA is unloaded. Either of these flags can be * specified independently of any other handlers. The '-m' flag automatically * does an unmount and remount of the underlying dataset to aid in flushing the * cache. * * The '-f' flag controls the frequency of errors injected, expressed as a * real number percentage between 0.0001 and 100. The default is 100. * * The this form is responsible for actually injecting the handler into the * framework. It takes the arguments described above, translates them to the * internal tuple using libzpool, and then issues an ioctl() to register the * handler. * * The final form can target a specific bookmark, regardless of whether a * human-readable interface has been designed. It allows developers to specify * a particular block by number. */ #include #include #include #include #include #include #include #include #include #include #undef verify /* both libzfs.h and zfs_context.h want to define this */ #include "zinject.h" libzfs_handle_t *g_zfs; int zfs_fd; static const char *const errtable[TYPE_INVAL] = { "data", "dnode", "mos", "mosdir", "metaslab", "config", "bpobj", "spacemap", "errlog", "uber", "nvlist", "pad1", "pad2" }; static err_type_t name_to_type(const char *arg) { int i; for (i = 0; i < TYPE_INVAL; i++) if (strcmp(errtable[i], arg) == 0) return (i); return (TYPE_INVAL); } static const char * type_to_name(uint64_t type) { switch (type) { case DMU_OT_OBJECT_DIRECTORY: return ("mosdir"); case DMU_OT_OBJECT_ARRAY: return ("metaslab"); case DMU_OT_PACKED_NVLIST: return ("config"); case DMU_OT_BPOBJ: return ("bpobj"); case DMU_OT_SPACE_MAP: return ("spacemap"); case DMU_OT_ERROR_LOG: return ("errlog"); default: return ("-"); } } struct errstr { int err; const char *str; }; static const struct errstr errstrtable[] = { { EIO, "io" }, { ECKSUM, "checksum" }, { EINVAL, "decompress" }, { EACCES, "decrypt" }, { ENXIO, "nxio" }, { ECHILD, "dtl" }, { EILSEQ, "corrupt" }, { ENOSYS, "noop" }, { 0, NULL }, }; static int str_to_err(const char *str) { for (int i = 0; errstrtable[i].str != NULL; i++) if (strcasecmp(errstrtable[i].str, str) == 0) return (errstrtable[i].err); return (-1); } static const char * err_to_str(int err) { for (int i = 0; errstrtable[i].str != NULL; i++) if (errstrtable[i].err == err) return (errstrtable[i].str); return ("[unknown]"); } static const char *const iotypestrtable[ZINJECT_IOTYPES] = { [ZINJECT_IOTYPE_NULL] = "null", [ZINJECT_IOTYPE_READ] = "read", [ZINJECT_IOTYPE_WRITE] = "write", [ZINJECT_IOTYPE_FREE] = "free", [ZINJECT_IOTYPE_CLAIM] = "claim", [ZINJECT_IOTYPE_FLUSH] = "flush", [ZINJECT_IOTYPE_TRIM] = "trim", [ZINJECT_IOTYPE_ALL] = "all", [ZINJECT_IOTYPE_PROBE] = "probe", }; static zinject_iotype_t str_to_iotype(const char *arg) { for (uint_t iotype = 0; iotype < ZINJECT_IOTYPES; iotype++) if (iotypestrtable[iotype] != NULL && strcasecmp(iotypestrtable[iotype], arg) == 0) return (iotype); return (ZINJECT_IOTYPES); } static const char * iotype_to_str(zinject_iotype_t iotype) { if (iotype >= ZINJECT_IOTYPES || iotypestrtable[iotype] == NULL) return ("[unknown]"); return (iotypestrtable[iotype]); } /* * Print usage message. */ void usage(void) { (void) printf( "usage:\n" "\n" "\tzinject\n" "\n" "\t\tList all active injection records.\n" "\n" "\tzinject -c \n" "\n" "\t\tClear the particular record (if given a numeric ID), or\n" "\t\tall records if 'all' is specified.\n" "\n" "\tzinject -p pool\n" "\t\tInject a panic fault at the specified function. Only \n" "\t\tfunctions which call spa_vdev_config_exit(), or \n" "\t\tspa_vdev_exit() will trigger a panic.\n" "\n" "\tzinject -d device [-e errno] [-L ] [-F]\n" "\t\t[-T ] [-f frequency] pool\n\n" "\t\tInject a fault into a particular device or the device's\n" "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n " "\t\t'pad1', or 'pad2'.\n" "\t\t'errno' can be 'nxio' (the default), 'io', 'dtl',\n" "\t\t'corrupt' (bit flip), or 'noop' (successfully do nothing).\n" "\t\t'frequency' is a value between 0.0001 and 100.0 that limits\n" "\t\tdevice error injection to a percentage of the IOs.\n" "\n" "\tzinject -d device -A -D pool\n" "\t\tPerform a specific action on a particular device.\n" "\n" "\tzinject -d device -D latency:lanes pool\n" "\n" "\t\tAdd an artificial delay to IO requests on a particular\n" "\t\tdevice, such that the requests take a minimum of 'latency'\n" "\t\tmilliseconds to complete. Each delay has an associated\n" "\t\tnumber of 'lanes' which defines the number of concurrent\n" "\t\tIO requests that can be processed.\n" "\n" "\t\tFor example, with a single lane delay of 10 ms (-D 10:1),\n" "\t\tthe device will only be able to service a single IO request\n" "\t\tat a time with each request taking 10 ms to complete. So,\n" "\t\tif only a single request is submitted every 10 ms, the\n" "\t\taverage latency will be 10 ms; but if more than one request\n" "\t\tis submitted every 10 ms, the average latency will be more\n" "\t\tthan 10 ms.\n" "\n" "\t\tSimilarly, if a delay of 10 ms is specified to have two\n" "\t\tlanes (-D 10:2), then the device will be able to service\n" "\t\ttwo requests at a time, each with a minimum latency of\n" "\t\t10 ms. So, if two requests are submitted every 10 ms, then\n" "\t\tthe average latency will be 10 ms; but if more than two\n" "\t\trequests are submitted every 10 ms, the average latency\n" "\t\twill be more than 10 ms.\n" "\n" "\t\tAlso note, these delays are additive. So two invocations\n" "\t\tof '-D 10:1', is roughly equivalent to a single invocation\n" "\t\tof '-D 10:2'. This also means, one can specify multiple\n" "\t\tlanes with differing target latencies. For example, an\n" "\t\tinvocation of '-D 10:1' followed by '-D 25:2' will\n" "\t\tcreate 3 lanes on the device; one lane with a latency\n" "\t\tof 10 ms and two lanes with a 25 ms latency.\n" "\n" "\tzinject -P import|export -s pool\n" "\t\tAdd an artificial delay to a future pool import or export,\n" "\t\tsuch that the operation takes a minimum of supplied seconds\n" "\t\tto complete.\n" "\n" "\tzinject -I [-s | -g ] pool\n" "\t\tCause the pool to stop writing blocks yet not\n" "\t\treport errors for a duration. Simulates buggy hardware\n" "\t\tthat fails to honor cache flush requests.\n" "\t\tDefault duration is 30 seconds. The machine is panicked\n" "\t\tat the end of the duration.\n" "\n" "\tzinject -b objset:object:level:blkid pool\n" "\n" "\t\tInject an error into pool 'pool' with the numeric bookmark\n" "\t\tspecified by the remaining tuple. Each number is in\n" "\t\thexadecimal, and only one block can be specified.\n" "\n" "\tzinject [-q] <-t type> [-C dvas] [-e errno] [-l level]\n" "\t\t[-r range] [-a] [-m] [-u] [-f freq] \n" "\n" "\t\tInject an error into the object specified by the '-t' option\n" "\t\tand the object descriptor. The 'object' parameter is\n" "\t\tinterpreted depending on the '-t' option.\n" "\n" "\t\t-q\tQuiet mode. Only print out the handler number added.\n" "\t\t-e\tInject a specific error. Must be one of 'io',\n" "\t\t\t'checksum', 'decompress', or 'decrypt'. Default is 'io'.\n" "\t\t-C\tInject the given error only into specific DVAs. The\n" "\t\t\tDVAs should be specified as a list of 0-indexed DVAs\n" "\t\t\tseparated by commas (ex. '0,2').\n" "\t\t-l\tInject error at a particular block level. Default is " "0.\n" "\t\t-m\tAutomatically remount underlying filesystem.\n" "\t\t-r\tInject error over a particular logical range of an\n" "\t\t\tobject. Will be translated to the appropriate blkid\n" "\t\t\trange according to the object's properties.\n" "\t\t-a\tFlush the ARC cache. Can be specified without any\n" "\t\t\tassociated object.\n" "\t\t-u\tUnload the associated pool. Can be specified with only\n" "\t\t\ta pool object.\n" "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n" "\t\t\ta percentage between 0.0001 and 100.\n" "\n" "\t-t data\t\tInject an error into the plain file contents of a\n" "\t\t\tfile. The object must be specified as a complete path\n" "\t\t\tto a file on a ZFS filesystem.\n" "\n" "\t-t dnode\tInject an error into the metadnode in the block\n" "\t\t\tcorresponding to the dnode for a file or directory. The\n" "\t\t\t'-r' option is incompatible with this mode. The object\n" "\t\t\tis specified as a complete path to a file or directory\n" "\t\t\ton a ZFS filesystem.\n" "\n" "\t-t \tInject errors into the MOS for objects of the given\n" "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n" "\t\t\tspacemap, metaslab, errlog. The only valid is\n" "\t\t\tthe poolname.\n"); } static int iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *), void *data) { zfs_cmd_t zc = {"\0"}; int ret; while (zfs_ioctl(g_zfs, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0) if ((ret = func((int)zc.zc_guid, zc.zc_name, &zc.zc_inject_record, data)) != 0) return (ret); if (errno != ENOENT) { (void) fprintf(stderr, "Unable to list handlers: %s\n", strerror(errno)); return (-1); } return (0); } static int print_data_handler(int id, const char *pool, zinject_record_t *record, void *data) { int *count = data; if (record->zi_guid != 0 || record->zi_func[0] != '\0' || record->zi_duration != 0) { return (0); } if (*count == 0) { (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-4s " "%-15s %-6s %-15s\n", "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL", "DVAs", "RANGE", "MATCH", "INJECT"); (void) printf("--- --------------- ------ " "------ -------- --- ---- --------------- " "------ ------\n"); } *count += 1; char rangebuf[32]; if (record->zi_start == 0 && record->zi_end == -1ULL) snprintf(rangebuf, sizeof (rangebuf), "all"); else snprintf(rangebuf, sizeof (rangebuf), "[%llu, %llu]", (u_longlong_t)record->zi_start, (u_longlong_t)record->zi_end); (void) printf("%3d %-15s %-6llu %-6llu %-8s %-3d 0x%02x %-15s " - "%6lu %6lu\n", id, pool, (u_longlong_t)record->zi_objset, + "%6" PRIu64 " %6" PRIu64 "\n", id, pool, + (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object, type_to_name(record->zi_type), record->zi_level, record->zi_dvas, rangebuf, record->zi_match_count, record->zi_inject_count); return (0); } static int print_device_handler(int id, const char *pool, zinject_record_t *record, void *data) { int *count = data; if (record->zi_guid == 0 || record->zi_func[0] != '\0') return (0); if (record->zi_cmd == ZINJECT_DELAY_IO) return (0); if (*count == 0) { (void) printf("%3s %-15s %-16s %-5s %-10s %-9s " "%-6s %-6s\n", "ID", "POOL", "GUID", "TYPE", "ERROR", "FREQ", "MATCH", "INJECT"); (void) printf( "--- --------------- ---------------- " "----- ---------- --------- " "------ ------\n"); } *count += 1; double freq = record->zi_freq == 0 ? 100.0f : (((double)record->zi_freq) / ZI_PERCENTAGE_MAX) * 100.0f; (void) printf("%3d %-15s %llx %-5s %-10s %8.4f%% " - "%6lu %6lu\n", id, pool, (u_longlong_t)record->zi_guid, + "%6" PRIu64 " %6" PRIu64 "\n", id, pool, + (u_longlong_t)record->zi_guid, iotype_to_str(record->zi_iotype), err_to_str(record->zi_error), freq, record->zi_match_count, record->zi_inject_count); return (0); } static int print_delay_handler(int id, const char *pool, zinject_record_t *record, void *data) { int *count = data; if (record->zi_guid == 0 || record->zi_func[0] != '\0') return (0); if (record->zi_cmd != ZINJECT_DELAY_IO) return (0); if (*count == 0) { (void) printf("%3s %-15s %-16s %-10s %-5s %-9s " "%-6s %-6s\n", "ID", "POOL", "GUID", "DELAY (ms)", "LANES", "FREQ", "MATCH", "INJECT"); (void) printf("--- --------------- ---------------- " "---------- ----- --------- " "------ ------\n"); } *count += 1; double freq = record->zi_freq == 0 ? 100.0f : (((double)record->zi_freq) / ZI_PERCENTAGE_MAX) * 100.0f; (void) printf("%3d %-15s %llx %10llu %5llu %8.4f%% " - "%6lu %6lu\n", id, pool, (u_longlong_t)record->zi_guid, + "%6" PRIu64 " %6" PRIu64 "\n", id, pool, + (u_longlong_t)record->zi_guid, (u_longlong_t)NSEC2MSEC(record->zi_timer), (u_longlong_t)record->zi_nlanes, freq, record->zi_match_count, record->zi_inject_count); return (0); } static int print_panic_handler(int id, const char *pool, zinject_record_t *record, void *data) { int *count = data; if (record->zi_func[0] == '\0') return (0); if (*count == 0) { (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION"); (void) printf("--- --------------- ----------------\n"); } *count += 1; (void) printf("%3d %-15s %s\n", id, pool, record->zi_func); return (0); } static int print_pool_delay_handler(int id, const char *pool, zinject_record_t *record, void *data) { int *count = data; if (record->zi_cmd != ZINJECT_DELAY_IMPORT && record->zi_cmd != ZINJECT_DELAY_EXPORT) { return (0); } if (*count == 0) { (void) printf("%3s %-19s %-11s %s\n", "ID", "POOL", "DELAY (sec)", "COMMAND"); (void) printf("--- ------------------- -----------" " -------\n"); } *count += 1; (void) printf("%3d %-19s %-11llu %s\n", id, pool, (u_longlong_t)record->zi_duration, record->zi_cmd == ZINJECT_DELAY_IMPORT ? "import": "export"); return (0); } /* * Print all registered error handlers. Returns the number of handlers * registered. */ static int print_all_handlers(void) { int count = 0, total = 0; (void) iter_handlers(print_device_handler, &count); if (count > 0) { total += count; (void) printf("\n"); count = 0; } (void) iter_handlers(print_delay_handler, &count); if (count > 0) { total += count; (void) printf("\n"); count = 0; } (void) iter_handlers(print_data_handler, &count); if (count > 0) { total += count; (void) printf("\n"); count = 0; } (void) iter_handlers(print_pool_delay_handler, &count); if (count > 0) { total += count; (void) printf("\n"); count = 0; } (void) iter_handlers(print_panic_handler, &count); return (count + total); } static int cancel_one_handler(int id, const char *pool, zinject_record_t *record, void *data) { (void) pool, (void) record, (void) data; zfs_cmd_t zc = {"\0"}; zc.zc_guid = (uint64_t)id; if (zfs_ioctl(g_zfs, ZFS_IOC_CLEAR_FAULT, &zc) != 0) { (void) fprintf(stderr, "failed to remove handler %d: %s\n", id, strerror(errno)); return (1); } return (0); } /* * Remove all fault injection handlers. */ static int cancel_all_handlers(void) { int ret = iter_handlers(cancel_one_handler, NULL); if (ret == 0) (void) printf("removed all registered handlers\n"); return (ret); } /* * Remove a specific fault injection handler. */ static int cancel_handler(int id) { zfs_cmd_t zc = {"\0"}; zc.zc_guid = (uint64_t)id; if (zfs_ioctl(g_zfs, ZFS_IOC_CLEAR_FAULT, &zc) != 0) { (void) fprintf(stderr, "failed to remove handler %d: %s\n", id, strerror(errno)); return (1); } (void) printf("removed handler %d\n", id); return (0); } /* * Register a new fault injection handler. */ static int register_handler(const char *pool, int flags, zinject_record_t *record, int quiet) { zfs_cmd_t zc = {"\0"}; (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); zc.zc_inject_record = *record; zc.zc_guid = flags; if (zfs_ioctl(g_zfs, ZFS_IOC_INJECT_FAULT, &zc) != 0) { const char *errmsg = strerror(errno); switch (errno) { case EDOM: errmsg = "block level exceeds max level of object"; break; case EEXIST: if (record->zi_cmd == ZINJECT_DELAY_IMPORT) errmsg = "pool already imported"; if (record->zi_cmd == ZINJECT_DELAY_EXPORT) errmsg = "a handler already exists"; break; case ENOENT: /* import delay injector running on older zfs module */ if (record->zi_cmd == ZINJECT_DELAY_IMPORT) errmsg = "import delay injector not supported"; break; default: break; } (void) fprintf(stderr, "failed to add handler: %s\n", errmsg); return (1); } if (flags & ZINJECT_NULL) return (0); if (quiet) { (void) printf("%llu\n", (u_longlong_t)zc.zc_guid); } else { (void) printf("Added handler %llu with the following " "properties:\n", (u_longlong_t)zc.zc_guid); (void) printf(" pool: %s\n", pool); if (record->zi_guid) { (void) printf(" vdev: %llx\n", (u_longlong_t)record->zi_guid); } else if (record->zi_func[0] != '\0') { (void) printf(" panic function: %s\n", record->zi_func); } else if (record->zi_duration > 0) { (void) printf(" time: %lld seconds\n", (u_longlong_t)record->zi_duration); } else if (record->zi_duration < 0) { (void) printf(" txgs: %lld \n", (u_longlong_t)-record->zi_duration); } else if (record->zi_timer > 0) { (void) printf(" timer: %lld ms\n", (u_longlong_t)NSEC2MSEC(record->zi_timer)); } else { (void) printf("objset: %llu\n", (u_longlong_t)record->zi_objset); (void) printf("object: %llu\n", (u_longlong_t)record->zi_object); (void) printf(" type: %llu\n", (u_longlong_t)record->zi_type); (void) printf(" level: %d\n", record->zi_level); if (record->zi_start == 0 && record->zi_end == -1ULL) (void) printf(" range: all\n"); else (void) printf(" range: [%llu, %llu)\n", (u_longlong_t)record->zi_start, (u_longlong_t)record->zi_end); (void) printf(" dvas: 0x%x\n", record->zi_dvas); } } return (0); } static int perform_action(const char *pool, zinject_record_t *record, int cmd) { zfs_cmd_t zc = {"\0"}; ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED); (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); zc.zc_guid = record->zi_guid; zc.zc_cookie = cmd; if (zfs_ioctl(g_zfs, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) return (0); return (1); } static int parse_delay(char *str, uint64_t *delay, uint64_t *nlanes) { unsigned long scan_delay; unsigned long scan_nlanes; if (sscanf(str, "%lu:%lu", &scan_delay, &scan_nlanes) != 2) return (1); /* * We explicitly disallow a delay of zero here, because we key * off this value being non-zero in translate_device(), to * determine if the fault is a ZINJECT_DELAY_IO fault or not. */ if (scan_delay == 0) return (1); /* * The units for the CLI delay parameter is milliseconds, but * the data passed to the kernel is interpreted as nanoseconds. * Thus we scale the milliseconds to nanoseconds here, and this * nanosecond value is used to pass the delay to the kernel. */ *delay = MSEC2NSEC(scan_delay); *nlanes = scan_nlanes; return (0); } static int parse_frequency(const char *str, uint32_t *percent) { double val; char *post; val = strtod(str, &post); if (post == NULL || *post != '\0') return (EINVAL); /* valid range is [0.0001, 100.0] */ val /= 100.0f; if (val < 0.000001f || val > 1.0f) return (ERANGE); /* convert to an integer for use by kernel */ *percent = ((uint32_t)(val * ZI_PERCENTAGE_MAX)); return (0); } /* * This function converts a string specifier for DVAs into a bit mask. * The dva's provided by the user should be 0 indexed and separated by * a comma. For example: * "1" -> 0b0010 (0x2) * "0,1" -> 0b0011 (0x3) * "0,1,2" -> 0b0111 (0x7) */ static int parse_dvas(const char *str, uint32_t *dvas_out) { const char *c = str; uint32_t mask = 0; boolean_t need_delim = B_FALSE; /* max string length is 5 ("0,1,2") */ if (strlen(str) > 5 || strlen(str) == 0) return (EINVAL); while (*c != '\0') { switch (*c) { case '0': case '1': case '2': /* check for pipe between DVAs */ if (need_delim) return (EINVAL); /* check if this DVA has been set already */ if (mask & (1 << ((*c) - '0'))) return (EINVAL); mask |= (1 << ((*c) - '0')); need_delim = B_TRUE; break; case ',': need_delim = B_FALSE; break; default: /* check for invalid character */ return (EINVAL); } c++; } /* check for dangling delimiter */ if (!need_delim) return (EINVAL); *dvas_out = mask; return (0); } int main(int argc, char **argv) { int c; char *range = NULL; char *cancel = NULL; char *end; char *raw = NULL; char *device = NULL; int level = 0; int quiet = 0; int error = 0; int domount = 0; int io_type = ZINJECT_IOTYPE_ALL; int action = VDEV_STATE_UNKNOWN; err_type_t type = TYPE_INVAL; err_type_t label = TYPE_INVAL; zinject_record_t record = { 0 }; char pool[MAXNAMELEN] = ""; char dataset[MAXNAMELEN] = ""; zfs_handle_t *zhp = NULL; int nowrites = 0; int dur_txg = 0; int dur_secs = 0; int ret; int flags = 0; uint32_t dvas = 0; if ((g_zfs = libzfs_init()) == NULL) { (void) fprintf(stderr, "%s\n", libzfs_error_init(errno)); return (1); } libzfs_print_on_error(g_zfs, B_TRUE); if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { (void) fprintf(stderr, "failed to open ZFS device\n"); libzfs_fini(g_zfs); return (1); } if (argc == 1) { /* * No arguments. Print the available handlers. If there are no * available handlers, direct the user to '-h' for help * information. */ if (print_all_handlers() == 0) { (void) printf("No handlers registered.\n"); (void) printf("Run 'zinject -h' for usage " "information.\n"); } libzfs_fini(g_zfs); return (0); } while ((c = getopt(argc, argv, ":aA:b:C:d:D:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:P:")) != -1) { switch (c) { case 'a': flags |= ZINJECT_FLUSH_ARC; break; case 'A': if (strcasecmp(optarg, "degrade") == 0) { action = VDEV_STATE_DEGRADED; } else if (strcasecmp(optarg, "fault") == 0) { action = VDEV_STATE_FAULTED; } else { (void) fprintf(stderr, "invalid action '%s': " "must be 'degrade' or 'fault'\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'b': raw = optarg; break; case 'c': cancel = optarg; break; case 'C': ret = parse_dvas(optarg, &dvas); if (ret != 0) { (void) fprintf(stderr, "invalid DVA list '%s': " "DVAs should be 0 indexed and separated by " "commas.\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'd': device = optarg; break; case 'D': errno = 0; ret = parse_delay(optarg, &record.zi_timer, &record.zi_nlanes); if (ret != 0) { (void) fprintf(stderr, "invalid i/o delay " "value: '%s'\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'e': error = str_to_err(optarg); if (error < 0) { (void) fprintf(stderr, "invalid error type " "'%s': must be one of: io decompress " "decrypt nxio dtl corrupt noop\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'f': ret = parse_frequency(optarg, &record.zi_freq); if (ret != 0) { (void) fprintf(stderr, "%sfrequency value must " "be in the range [0.0001, 100.0]\n", ret == EINVAL ? "invalid value: " : ret == ERANGE ? "out of range: " : ""); libzfs_fini(g_zfs); return (1); } break; case 'F': record.zi_failfast = B_TRUE; break; case 'g': dur_txg = 1; record.zi_duration = (int)strtol(optarg, &end, 10); if (record.zi_duration <= 0 || *end != '\0') { (void) fprintf(stderr, "invalid duration '%s': " "must be a positive integer\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } /* store duration of txgs as its negative */ record.zi_duration *= -1; break; case 'h': usage(); libzfs_fini(g_zfs); return (0); case 'I': /* default duration, if one hasn't yet been defined */ nowrites = 1; if (dur_secs == 0 && dur_txg == 0) record.zi_duration = 30; break; case 'l': level = (int)strtol(optarg, &end, 10); if (*end != '\0') { (void) fprintf(stderr, "invalid level '%s': " "must be an integer\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'm': domount = 1; break; case 'p': (void) strlcpy(record.zi_func, optarg, sizeof (record.zi_func)); record.zi_cmd = ZINJECT_PANIC; break; case 'P': if (strcasecmp(optarg, "import") == 0) { record.zi_cmd = ZINJECT_DELAY_IMPORT; } else if (strcasecmp(optarg, "export") == 0) { record.zi_cmd = ZINJECT_DELAY_EXPORT; } else { (void) fprintf(stderr, "invalid command '%s': " "must be 'import' or 'export'\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'q': quiet = 1; break; case 'r': range = optarg; flags |= ZINJECT_CALC_RANGE; break; case 's': dur_secs = 1; record.zi_duration = (int)strtol(optarg, &end, 10); if (record.zi_duration <= 0 || *end != '\0') { (void) fprintf(stderr, "invalid duration '%s': " "must be a positive integer\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'T': io_type = str_to_iotype(optarg); if (io_type == ZINJECT_IOTYPES) { (void) fprintf(stderr, "invalid I/O type " "'%s': must be 'read', 'write', 'free', " "'claim', 'flush' or 'all'\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 't': if ((type = name_to_type(optarg)) == TYPE_INVAL && !MOS_TYPE(type)) { (void) fprintf(stderr, "invalid type '%s'\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case 'u': flags |= ZINJECT_UNLOAD_SPA; break; case 'L': if ((label = name_to_type(optarg)) == TYPE_INVAL && !LABEL_TYPE(type)) { (void) fprintf(stderr, "invalid label type " "'%s'\n", optarg); usage(); libzfs_fini(g_zfs); return (1); } break; case ':': (void) fprintf(stderr, "option -%c requires an " "operand\n", optopt); usage(); libzfs_fini(g_zfs); return (1); case '?': (void) fprintf(stderr, "invalid option '%c'\n", optopt); usage(); libzfs_fini(g_zfs); return (2); } } argc -= optind; argv += optind; if (record.zi_duration != 0 && record.zi_cmd == 0) record.zi_cmd = ZINJECT_IGNORED_WRITES; if (cancel != NULL) { /* * '-c' is invalid with any other options. */ if (raw != NULL || range != NULL || type != TYPE_INVAL || level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED || record.zi_freq > 0 || dvas != 0) { (void) fprintf(stderr, "cancel (-c) incompatible with " "any other options\n"); usage(); libzfs_fini(g_zfs); return (2); } if (argc != 0) { (void) fprintf(stderr, "extraneous argument to '-c'\n"); usage(); libzfs_fini(g_zfs); return (2); } if (strcmp(cancel, "all") == 0) { return (cancel_all_handlers()); } else { int id = (int)strtol(cancel, &end, 10); if (*end != '\0') { (void) fprintf(stderr, "invalid handle id '%s':" " must be an integer or 'all'\n", cancel); usage(); libzfs_fini(g_zfs); return (1); } return (cancel_handler(id)); } } if (device != NULL) { /* * Device (-d) injection uses a completely different mechanism * for doing injection, so handle it separately here. */ if (raw != NULL || range != NULL || type != TYPE_INVAL || level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED || dvas != 0) { (void) fprintf(stderr, "device (-d) incompatible with " "data error injection\n"); usage(); libzfs_fini(g_zfs); return (2); } if (argc != 1) { (void) fprintf(stderr, "device (-d) injection requires " "a single pool name\n"); usage(); libzfs_fini(g_zfs); return (2); } (void) strlcpy(pool, argv[0], sizeof (pool)); dataset[0] = '\0'; if (error == ECKSUM) { (void) fprintf(stderr, "device error type must be " "'io', 'nxio' or 'corrupt'\n"); libzfs_fini(g_zfs); return (1); } if (error == EILSEQ && (record.zi_freq == 0 || io_type != ZINJECT_IOTYPE_READ)) { (void) fprintf(stderr, "device corrupt errors require " "io type read and a frequency value\n"); libzfs_fini(g_zfs); return (1); } record.zi_iotype = io_type; if (translate_device(pool, device, label, &record) != 0) { libzfs_fini(g_zfs); return (1); } if (record.zi_nlanes) { switch (io_type) { case ZINJECT_IOTYPE_READ: case ZINJECT_IOTYPE_WRITE: case ZINJECT_IOTYPE_ALL: break; default: (void) fprintf(stderr, "I/O type for a delay " "must be 'read' or 'write'\n"); usage(); libzfs_fini(g_zfs); return (1); } } if (!error) error = ENXIO; if (action != VDEV_STATE_UNKNOWN) return (perform_action(pool, &record, action)); } else if (raw != NULL) { if (range != NULL || type != TYPE_INVAL || level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED || record.zi_freq > 0 || dvas != 0) { (void) fprintf(stderr, "raw (-b) format with " "any other options\n"); usage(); libzfs_fini(g_zfs); return (2); } if (argc != 1) { (void) fprintf(stderr, "raw (-b) format expects a " "single pool name\n"); usage(); libzfs_fini(g_zfs); return (2); } (void) strlcpy(pool, argv[0], sizeof (pool)); dataset[0] = '\0'; if (error == ENXIO) { (void) fprintf(stderr, "data error type must be " "'checksum' or 'io'\n"); libzfs_fini(g_zfs); return (1); } record.zi_cmd = ZINJECT_DATA_FAULT; if (translate_raw(raw, &record) != 0) { libzfs_fini(g_zfs); return (1); } if (!error) error = EIO; } else if (record.zi_cmd == ZINJECT_PANIC) { if (raw != NULL || range != NULL || type != TYPE_INVAL || level != 0 || device != NULL || record.zi_freq > 0 || dvas != 0) { (void) fprintf(stderr, "%s incompatible with other " "options\n", "import|export delay (-P)"); usage(); libzfs_fini(g_zfs); return (2); } if (argc < 1 || argc > 2) { (void) fprintf(stderr, "panic (-p) injection requires " "a single pool name and an optional id\n"); usage(); libzfs_fini(g_zfs); return (2); } (void) strlcpy(pool, argv[0], sizeof (pool)); if (argv[1] != NULL) record.zi_type = atoi(argv[1]); dataset[0] = '\0'; } else if (record.zi_cmd == ZINJECT_DELAY_IMPORT || record.zi_cmd == ZINJECT_DELAY_EXPORT) { if (raw != NULL || range != NULL || type != TYPE_INVAL || level != 0 || device != NULL || record.zi_freq > 0 || dvas != 0) { (void) fprintf(stderr, "%s incompatible with other " "options\n", "import|export delay (-P)"); usage(); libzfs_fini(g_zfs); return (2); } if (argc != 1 || record.zi_duration <= 0) { (void) fprintf(stderr, "import|export delay (-P) " "injection requires a duration (-s) and a single " "pool name\n"); usage(); libzfs_fini(g_zfs); return (2); } (void) strlcpy(pool, argv[0], sizeof (pool)); } else if (record.zi_cmd == ZINJECT_IGNORED_WRITES) { if (raw != NULL || range != NULL || type != TYPE_INVAL || level != 0 || record.zi_freq > 0 || dvas != 0) { (void) fprintf(stderr, "hardware failure (-I) " "incompatible with other options\n"); usage(); libzfs_fini(g_zfs); return (2); } if (nowrites == 0) { (void) fprintf(stderr, "-s or -g meaningless " "without -I (ignore writes)\n"); usage(); libzfs_fini(g_zfs); return (2); } else if (dur_secs && dur_txg) { (void) fprintf(stderr, "choose a duration either " "in seconds (-s) or a number of txgs (-g) " "but not both\n"); usage(); libzfs_fini(g_zfs); return (2); } else if (argc != 1) { (void) fprintf(stderr, "ignore writes (-I) " "injection requires a single pool name\n"); usage(); libzfs_fini(g_zfs); return (2); } (void) strlcpy(pool, argv[0], sizeof (pool)); dataset[0] = '\0'; } else if (type == TYPE_INVAL) { if (flags == 0) { (void) fprintf(stderr, "at least one of '-b', '-d', " "'-t', '-a', '-p', '-I' or '-u' " "must be specified\n"); usage(); libzfs_fini(g_zfs); return (2); } if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) { (void) strlcpy(pool, argv[0], sizeof (pool)); dataset[0] = '\0'; } else if (argc != 0) { (void) fprintf(stderr, "extraneous argument for " "'-f'\n"); usage(); libzfs_fini(g_zfs); return (2); } flags |= ZINJECT_NULL; } else { if (argc != 1) { (void) fprintf(stderr, "missing object\n"); usage(); libzfs_fini(g_zfs); return (2); } if (error == ENXIO || error == EILSEQ) { (void) fprintf(stderr, "data error type must be " "'checksum' or 'io'\n"); libzfs_fini(g_zfs); return (1); } if (dvas != 0) { if (error == EACCES || error == EINVAL) { (void) fprintf(stderr, "the '-C' option may " "not be used with logical data errors " "'decrypt' and 'decompress'\n"); libzfs_fini(g_zfs); return (1); } record.zi_dvas = dvas; } if (error == EACCES) { if (type != TYPE_DATA) { (void) fprintf(stderr, "decryption errors " "may only be injected for 'data' types\n"); libzfs_fini(g_zfs); return (1); } record.zi_cmd = ZINJECT_DECRYPT_FAULT; /* * Internally, ZFS actually uses ECKSUM for decryption * errors since EACCES is used to indicate the key was * not found. */ error = ECKSUM; } else { record.zi_cmd = ZINJECT_DATA_FAULT; } if (translate_record(type, argv[0], range, level, &record, pool, dataset) != 0) { libzfs_fini(g_zfs); return (1); } if (!error) error = EIO; } /* * If this is pool-wide metadata, unmount everything. The ioctl() will * unload the pool, so that we trigger spa-wide reopen of metadata next * time we access the pool. */ if (dataset[0] != '\0' && domount) { if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL) { libzfs_fini(g_zfs); return (1); } if (zfs_unmount(zhp, NULL, 0) != 0) { libzfs_fini(g_zfs); return (1); } } record.zi_error = error; ret = register_handler(pool, flags, &record, quiet); if (dataset[0] != '\0' && domount) ret = (zfs_mount(zhp, NULL, 0) != 0); libzfs_fini(g_zfs); return (ret); } diff --git a/tests/zfs-tests/cmd/clonefile.c b/tests/zfs-tests/cmd/clonefile.c index bc30bb7798e9..809a33d2fb99 100644 --- a/tests/zfs-tests/cmd/clonefile.c +++ b/tests/zfs-tests/cmd/clonefile.c @@ -1,373 +1,376 @@ /* * SPDX-License-Identifier: MIT * * Copyright (c) 2023, Rob Norris * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ /* * This program is to test the availability and behaviour of copy_file_range, * FICLONE, FICLONERANGE and FIDEDUPERANGE in the Linux kernel. It should * compile and run even if these features aren't exposed through the libc. */ #include #include #include #include #include #include #include #include #include #include #include #include #ifndef __NR_copy_file_range #if defined(__x86_64__) #define __NR_copy_file_range (326) #elif defined(__i386__) #define __NR_copy_file_range (377) #elif defined(__s390__) #define __NR_copy_file_range (375) #elif defined(__arm__) #define __NR_copy_file_range (391) #elif defined(__aarch64__) #define __NR_copy_file_range (285) #elif defined(__powerpc__) #define __NR_copy_file_range (379) #else #error "no definition of __NR_copy_file_range for this platform" #endif #endif /* __NR_copy_file_range */ #ifdef __FreeBSD__ #define loff_t off_t #endif ssize_t copy_file_range(int, loff_t *, int, loff_t *, size_t, unsigned int) __attribute__((weak)); static inline ssize_t cf_copy_file_range(int sfd, loff_t *soff, int dfd, loff_t *doff, size_t len, unsigned int flags) { if (copy_file_range) return (copy_file_range(sfd, soff, dfd, doff, len, flags)); return ( syscall(__NR_copy_file_range, sfd, soff, dfd, doff, len, flags)); } /* Define missing FICLONE */ #ifdef FICLONE #define CF_FICLONE FICLONE #else #define CF_FICLONE _IOW(0x94, 9, int) #endif /* Define missing FICLONERANGE and support structs */ #ifdef FICLONERANGE #define CF_FICLONERANGE FICLONERANGE typedef struct file_clone_range cf_file_clone_range_t; #else typedef struct { int64_t src_fd; uint64_t src_offset; uint64_t src_length; uint64_t dest_offset; } cf_file_clone_range_t; #define CF_FICLONERANGE _IOW(0x94, 13, cf_file_clone_range_t) #endif /* Define missing FIDEDUPERANGE and support structs */ #ifdef FIDEDUPERANGE #define CF_FIDEDUPERANGE FIDEDUPERANGE #define CF_FILE_DEDUPE_RANGE_SAME FILE_DEDUPE_RANGE_SAME #define CF_FILE_DEDUPE_RANGE_DIFFERS FILE_DEDUPE_RANGE_DIFFERS typedef struct file_dedupe_range_info cf_file_dedupe_range_info_t; typedef struct file_dedupe_range cf_file_dedupe_range_t; #else typedef struct { int64_t dest_fd; uint64_t dest_offset; uint64_t bytes_deduped; int32_t status; uint32_t reserved; } cf_file_dedupe_range_info_t; typedef struct { uint64_t src_offset; uint64_t src_length; uint16_t dest_count; uint16_t reserved1; uint32_t reserved2; cf_file_dedupe_range_info_t info[0]; } cf_file_dedupe_range_t; #define CF_FIDEDUPERANGE _IOWR(0x94, 54, cf_file_dedupe_range_t) #define CF_FILE_DEDUPE_RANGE_SAME (0) #define CF_FILE_DEDUPE_RANGE_DIFFERS (1) #endif typedef enum { CF_MODE_NONE, CF_MODE_CLONE, CF_MODE_CLONERANGE, CF_MODE_COPYFILERANGE, CF_MODE_DEDUPERANGE, } cf_mode_t; static int usage(void) { printf( "usage:\n" " FICLONE:\n" " clonefile -c \n" " FICLONERANGE:\n" " clonefile -r \n" " copy_file_range:\n" " clonefile -f [ ]\n" " FIDEDUPERANGE:\n" " clonefile -d \n"); return (1); } int do_clone(int sfd, int dfd); int do_clonerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len); int do_copyfilerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len); int do_deduperange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len); int quiet = 0; int main(int argc, char **argv) { cf_mode_t mode = CF_MODE_NONE; int c; while ((c = getopt(argc, argv, "crfdq")) != -1) { switch (c) { case 'c': mode = CF_MODE_CLONE; break; case 'r': mode = CF_MODE_CLONERANGE; break; case 'f': mode = CF_MODE_COPYFILERANGE; break; case 'd': mode = CF_MODE_DEDUPERANGE; break; case 'q': quiet = 1; break; } } switch (mode) { case CF_MODE_NONE: return (usage()); case CF_MODE_CLONE: if ((argc-optind) != 2) return (usage()); break; case CF_MODE_CLONERANGE: case CF_MODE_DEDUPERANGE: if ((argc-optind) != 5) return (usage()); break; case CF_MODE_COPYFILERANGE: if ((argc-optind) != 2 && (argc-optind) != 5) return (usage()); break; default: abort(); } loff_t soff = 0, doff = 0; size_t len = SSIZE_MAX; + unsigned long long len2; if ((argc-optind) == 5) { soff = strtoull(argv[optind+2], NULL, 10); if (soff == ULLONG_MAX) { fprintf(stderr, "invalid source offset"); return (1); } doff = strtoull(argv[optind+3], NULL, 10); if (doff == ULLONG_MAX) { fprintf(stderr, "invalid dest offset"); return (1); } if (mode == CF_MODE_COPYFILERANGE && strcmp(argv[optind+4], "all") == 0) { len = SSIZE_MAX; } else { - len = strtoull(argv[optind+4], NULL, 10); - if (len == ULLONG_MAX) { + len2 = strtoull(argv[optind+4], NULL, 10); + if (len2 == ULLONG_MAX) { fprintf(stderr, "invalid length"); return (1); } + if (len2 < SSIZE_MAX) + len = (size_t)len2; } } int sfd = open(argv[optind], O_RDONLY); if (sfd < 0) { fprintf(stderr, "open: %s: %s\n", argv[optind], strerror(errno)); return (1); } int dfd = open(argv[optind+1], O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (dfd < 0) { fprintf(stderr, "open: %s: %s\n", argv[optind+1], strerror(errno)); close(sfd); return (1); } int err; switch (mode) { case CF_MODE_CLONE: err = do_clone(sfd, dfd); break; case CF_MODE_CLONERANGE: err = do_clonerange(sfd, dfd, soff, doff, len); break; case CF_MODE_COPYFILERANGE: err = do_copyfilerange(sfd, dfd, soff, doff, len); break; case CF_MODE_DEDUPERANGE: err = do_deduperange(sfd, dfd, soff, doff, len); break; default: abort(); } if (!quiet) { off_t spos = lseek(sfd, 0, SEEK_CUR); off_t slen = lseek(sfd, 0, SEEK_END); off_t dpos = lseek(dfd, 0, SEEK_CUR); off_t dlen = lseek(dfd, 0, SEEK_END); - fprintf(stderr, "file offsets: src=%lu/%lu; dst=%lu/%lu\n", + fprintf(stderr, "file offsets: src=%jd/%jd; dst=%jd/%jd\n", spos, slen, dpos, dlen); } close(dfd); close(sfd); return (err == 0 ? 0 : 1); } int do_clone(int sfd, int dfd) { if (!quiet) fprintf(stderr, "using FICLONE\n"); int err = ioctl(dfd, CF_FICLONE, sfd); if (err < 0) { fprintf(stderr, "ioctl(FICLONE): %s\n", strerror(errno)); return (err); } return (0); } int do_clonerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len) { if (!quiet) fprintf(stderr, "using FICLONERANGE\n"); cf_file_clone_range_t fcr = { .src_fd = sfd, .src_offset = soff, .src_length = len, .dest_offset = doff, }; int err = ioctl(dfd, CF_FICLONERANGE, &fcr); if (err < 0) { fprintf(stderr, "ioctl(FICLONERANGE): %s\n", strerror(errno)); return (err); } return (0); } int do_copyfilerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len) { if (!quiet) fprintf(stderr, "using copy_file_range\n"); ssize_t copied = cf_copy_file_range(sfd, &soff, dfd, &doff, len, 0); if (copied < 0) { fprintf(stderr, "copy_file_range: %s\n", strerror(errno)); return (1); } if (len == SSIZE_MAX) { struct stat sb; if (fstat(sfd, &sb) < 0) { fprintf(stderr, "fstat(sfd): %s\n", strerror(errno)); return (1); } len = sb.st_size; } if (copied != len) { fprintf(stderr, "copy_file_range: copied less than requested: " - "requested=%lu; copied=%lu\n", len, copied); + "requested=%zu; copied=%zd\n", len, copied); return (1); } return (0); } int do_deduperange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len) { if (!quiet) fprintf(stderr, "using FIDEDUPERANGE\n"); char buf[sizeof (cf_file_dedupe_range_t)+ sizeof (cf_file_dedupe_range_info_t)] = {0}; cf_file_dedupe_range_t *fdr = (cf_file_dedupe_range_t *)&buf[0]; cf_file_dedupe_range_info_t *fdri = (cf_file_dedupe_range_info_t *) &buf[sizeof (cf_file_dedupe_range_t)]; fdr->src_offset = soff; fdr->src_length = len; fdr->dest_count = 1; fdri->dest_fd = dfd; fdri->dest_offset = doff; int err = ioctl(sfd, CF_FIDEDUPERANGE, fdr); if (err != 0) fprintf(stderr, "ioctl(FIDEDUPERANGE): %s\n", strerror(errno)); if (fdri->status < 0) { fprintf(stderr, "dedup failed: %s\n", strerror(-fdri->status)); err = -1; } else if (fdri->status == CF_FILE_DEDUPE_RANGE_DIFFERS) { fprintf(stderr, "dedup failed: range differs\n"); err = -1; } return (err); } diff --git a/tests/zfs-tests/tests/functional/vdev_disk/page_alignment.c b/tests/zfs-tests/tests/functional/vdev_disk/page_alignment.c index 2d8dad8ef986..8189c32cc380 100644 --- a/tests/zfs-tests/tests/functional/vdev_disk/page_alignment.c +++ b/tests/zfs-tests/tests/functional/vdev_disk/page_alignment.c @@ -1,476 +1,476 @@ // SPDX-License-Identifier: CDDL-1.0 /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or https://opensource.org/licenses/CDDL-1.0. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2023, 2024, Klara Inc. */ #include #include #include #include #include /* * This tests the vdev_disk page alignment check callback * vdev_disk_check_alignment_cb(). For now, this test includes a copy of that * function from module/os/linux/zfs/vdev_disk.c. If you change it here, * remember to change it there too, and add tests data here to validate the * change you're making. */ struct page; /* * This is spl_pagesize() in userspace, which requires linking libspl, but * would also then use the platform page size, which isn't what we want for * a test. To keep the check callback the same as the real one, we just * redefine it. */ #undef PAGESIZE #define PAGESIZE (4096) typedef struct { size_t blocksize; int seen_first; int seen_last; } vdev_disk_check_alignment_t; static int vdev_disk_check_alignment_cb(struct page *page, size_t off, size_t len, void *priv) { (void) page; vdev_disk_check_alignment_t *s = priv; /* * The cardinal rule: a single on-disk block must never cross an * physical (order-0) page boundary, as the kernel expects to be able * to split at both LBS and page boundaries. * * This implies various alignment rules for the blocks in this * (possibly compound) page, which we can check for. */ /* * If the previous page did not end on a page boundary, then we * can't proceed without creating a hole. */ if (s->seen_last) return (1); /* This page must contain only whole LBS-sized blocks. */ if (!IS_P2ALIGNED(len, s->blocksize)) return (1); /* * If this is not the first page in the ABD, then the data must start * on a page-aligned boundary (so the kernel can split on page * boundaries without having to deal with a hole). If it is, then * it can start on LBS-alignment. */ if (s->seen_first) { if (!IS_P2ALIGNED(off, PAGESIZE)) return (1); } else { if (!IS_P2ALIGNED(off, s->blocksize)) return (1); s->seen_first = 1; } /* * If this data does not end on a page-aligned boundary, then this * must be the last page in the ABD, for the same reason. */ s->seen_last = !IS_P2ALIGNED(off+len, PAGESIZE); return (0); } typedef struct { /* test name */ const char *name; /* stored block size */ uint32_t blocksize; /* amount of data to take */ size_t size; /* [start offset in page, len to end of page or size] */ size_t pages[16][2]; } page_test_t; static const page_test_t valid_tests[] = { /* 512B block tests */ { "512B blocks, 4K single page", 512, 0x1000, { { 0x0, 0x1000 }, }, }, { "512B blocks, 1K at start of page", 512, 0x400, { { 0x0, 0x1000 }, }, }, { "512B blocks, 1K at end of page", 512, 0x400, { { 0x0c00, 0x0400 }, }, }, { "512B blocks, 1K within page, 512B start offset", 512, 0x400, { { 0x0200, 0x0e00 }, }, }, { "512B blocks, 8K across 2x4K pages", 512, 0x2000, { { 0x0, 0x1000 }, { 0x0, 0x1000 }, }, }, { "512B blocks, 4K across two pages, 2K start offset", 512, 0x1000, { { 0x0800, 0x0800 }, { 0x0, 0x0800 }, }, }, { "512B blocks, 16K across 5x4K pages, 512B start offset", 512, 0x4000, { { 0x0200, 0x0e00 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x0200 }, }, }, { "512B blocks, 64K data, 8x8K compound pages", 512, 0x10000, { { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, }, }, { "512B blocks, 64K data, 9x8K compound pages, 512B start offset", 512, 0x10000, { { 0x0200, 0x1e00 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x0200 }, }, }, { "512B blocks, 64K data, 2x16K compound pages, 8x4K pages", 512, 0x10000, { { 0x0, 0x8000 }, { 0x0, 0x8000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, }, }, { "512B blocks, 64K data, mixed 4K/8K/16K pages", 512, 0x10000, { { 0x0, 0x1000 }, { 0x0, 0x2000 }, { 0x0, 0x1000 }, { 0x0, 0x8000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x2000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x2000 }, }, }, { "512B blocks, 64K data, mixed 4K/8K/16K pages, 1K start offset", 512, 0x10000, { { 0x0400, 0x0c00 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x1000 }, { 0x0, 0x8000 }, { 0x0, 0x1000 }, { 0x0, 0x0400 }, }, }, /* 4K block tests */ { "4K blocks, 4K single page", 4096, 0x1000, { { 0x0, 0x1000 }, }, }, { "4K blocks, 8K across 2x4K pages", 4096, 0x2000, { { 0x0, 0x1000 }, { 0x0, 0x1000 }, }, }, { "4K blocks, 64K data, 8x8K compound pages", 4096, 0x10000, { { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, }, }, { "4K blocks, 64K data, 2x16K compound pages, 8x4K pages", 4096, 0x10000, { { 0x0, 0x8000 }, { 0x0, 0x8000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, }, }, { "4K blocks, 64K data, mixed 4K/8K/16K pages", 4096, 0x10000, { { 0x0, 0x1000 }, { 0x0, 0x2000 }, { 0x0, 0x1000 }, { 0x0, 0x8000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x2000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x2000 }, }, }, { 0 }, }; static const page_test_t invalid_tests[] = { /* * Gang tests. Composed of lots of smaller allocations, rarely properly * aligned. */ { "512B blocks, 16K data, 512 leader (gang block simulation)", 512, 0x8000, { { 0x0, 0x0200 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x0c00 }, }, }, { "4K blocks, 32K data, 2 incompatible spans " "(gang abd simulation)", 4096, 0x8000, { { 0x0800, 0x0800 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x0800 }, { 0x0800, 0x0800 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x0800 }, }, }, /* * Blocks must not span multiple physical pages. These tests used to * be considered valid, but were since found to be invalid and were * moved here. */ { "4K blocks, 4K across two pages, 2K start offset", 4096, 0x1000, { { 0x0800, 0x0800 }, { 0x0, 0x0800 }, }, }, { "4K blocks, 16K across 5x4K pages, 512B start offset", 4096, 0x4000, { { 0x0200, 0x0e00 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x0200 }, }, }, { "4K blocks, 64K data, 9x8K compound pages, 512B start offset", 4096, 0x10000, { { 0x0200, 0x1e00 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x0200 }, }, }, { "4K blocks, 64K data, mixed 4K/8K/16K pages, 1K start offset", 4096, 0x10000, { { 0x0400, 0x0c00 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x1000 }, { 0x0, 0x2000 }, { 0x0, 0x2000 }, { 0x0, 0x1000 }, { 0x0, 0x8000 }, { 0x0, 0x1000 }, { 0x0, 0x0400 }, }, }, /* * This is the very typical case of a 4K block being allocated from * the middle of a mixed-used slab backed by a higher-order compound * page. */ { "4K blocks, 4K data from compound slab, 2K-align offset", 4096, 0x1000, { { 0x1800, 0x6800 } } }, /* * Blocks smaller than LBS should never be possible, but used to be by * accident (see GH#16990). We test for and reject them just to be * sure. */ { "4K blocks, 1K at end of page", 4096, 0x400, { { 0x0c00, 0x0400 }, }, }, { "4K blocks, 1K at start of page", 4096, 0x400, { { 0x0, 0x1000 }, }, }, { "4K blocks, 1K within page, 512B start offset", 4096, 0x400, { { 0x0200, 0x0e00 }, }, }, { 0 }, }; static bool run_test(const page_test_t *test, bool verbose) { size_t rem = test->size; vdev_disk_check_alignment_t s = { .blocksize = test->blocksize, }; for (int i = 0; test->pages[i][1] > 0; i++) { size_t off = test->pages[i][0]; size_t len = test->pages[i][1]; size_t take = MIN(rem, len); if (verbose) - printf(" page %d [off %lx len %lx], " - "rem %lx, take %lx\n", + printf(" page %d [off %zx len %zx], " + "rem %zx, take %zx\n", i, off, len, rem, take); if (vdev_disk_check_alignment_cb(NULL, off, take, &s)) { if (verbose) printf(" ABORT: misalignment detected, " - "rem %lx\n", rem); + "rem %zx\n", rem); return (false); } rem -= take; if (rem == 0) break; } if (rem > 0) { if (verbose) - printf(" ABORT: ran out of pages, rem %lx\n", rem); + printf(" ABORT: ran out of pages, rem %zx\n", rem); return (false); } return (true); } static void run_test_set(const page_test_t *tests, bool want, int *ntests, int *npassed) { for (const page_test_t *test = &tests[0]; test->name; test++) { bool pass = (run_test(test, false) == want); if (pass) { printf("%c %s: PASS\n", want ? '+' : '-', test->name); (*npassed)++; } else { printf("%s: FAIL [expected %s, got %s]\n", test->name, want ? "VALID" : "INVALID", want ? "INVALID" : "VALID"); run_test(test, true); } (*ntests)++; } } int main(void) { int ntests = 0, npassed = 0; run_test_set(valid_tests, true, &ntests, &npassed); run_test_set(invalid_tests, false, &ntests, &npassed); printf("\n%d/%d tests passed\n", npassed, ntests); return (ntests == npassed ? 0 : 1); }