diff --git a/tests/zfs-tests/cmd/draid.c b/tests/zfs-tests/cmd/draid.c index 76fdb4e8417f..46d7b4dcc69d 100644 --- a/tests/zfs-tests/cmd/draid.c +++ b/tests/zfs-tests/cmd/draid.c @@ -1,1407 +1,1409 @@ /* * 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) 2018 Intel Corporation. * Copyright (c) 2020 by Lawrence Livermore National Security, LLC. */ #include #include #include #include #include #include /* * The number of rows to generate for new permutation maps. */ #define MAP_ROWS_DEFAULT 256 /* * Key values for dRAID maps when stored as nvlists. */ #define MAP_SEED "seed" #define MAP_CHECKSUM "checksum" #define MAP_WORST_RATIO "worst_ratio" #define MAP_AVG_RATIO "avg_ratio" #define MAP_CHILDREN "children" #define MAP_NPERMS "nperms" #define MAP_PERMS "perms" static void draid_usage(void) { (void) fprintf(stderr, "usage: draid command args ...\n" "Available commands are:\n" "\n" "\tdraid generate [-cv] [-m min] [-n max] [-p passes] FILE\n" "\tdraid verify [-rv] FILE\n" "\tdraid dump [-v] [-m min] [-n max] FILE\n" "\tdraid table FILE\n" "\tdraid merge FILE SRC SRC...\n"); exit(1); } static int read_map(const char *filename, nvlist_t **allcfgs) { int block_size = 131072; int buf_size = 131072; int tmp_size, error; char *tmp_buf; struct stat64 stat; if (lstat64(filename, &stat) != 0) return (errno); if (stat.st_size == 0 || !(S_ISREG(stat.st_mode) || S_ISLNK(stat.st_mode))) { return (EINVAL); } gzFile fp = gzopen(filename, "rb"); if (fp == Z_NULL) return (errno); char *buf = malloc(buf_size); if (buf == NULL) { (void) gzclose(fp); return (ENOMEM); } ssize_t rc, bytes = 0; while (!gzeof(fp)) { rc = gzread(fp, buf + bytes, block_size); if ((rc < 0) || (rc == 0 && !gzeof(fp))) { free(buf); - (void) gzclose(fp); (void) gzerror(fp, &error); + (void) gzclose(fp); return (error); } else { bytes += rc; if (bytes + block_size >= buf_size) { tmp_size = 2 * buf_size; tmp_buf = malloc(tmp_size); if (tmp_buf == NULL) { free(buf); (void) gzclose(fp); return (ENOMEM); } memcpy(tmp_buf, buf, bytes); free(buf); buf = tmp_buf; buf_size = tmp_size; } } } (void) gzclose(fp); error = nvlist_unpack(buf, bytes, allcfgs, 0); free(buf); return (error); } /* * Read a map from the specified filename. A file contains multiple maps * which are indexed by the number of children. The caller is responsible * for freeing the configuration returned. */ static int read_map_key(const char *filename, const char *key, nvlist_t **cfg) { nvlist_t *allcfgs, *foundcfg = NULL; int error; error = read_map(filename, &allcfgs); if (error != 0) return (error); (void) nvlist_lookup_nvlist(allcfgs, key, &foundcfg); if (foundcfg != NULL) { nvlist_dup(foundcfg, cfg, KM_SLEEP); error = 0; } else { error = ENOENT; } nvlist_free(allcfgs); return (error); } /* * Write all mappings to the map file. */ static int write_map(const char *filename, nvlist_t *allcfgs) { size_t buflen = 0; int error; error = nvlist_size(allcfgs, &buflen, NV_ENCODE_XDR); if (error) return (error); char *buf = malloc(buflen); if (buf == NULL) return (ENOMEM); error = nvlist_pack(allcfgs, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP); if (error) { free(buf); return (error); } /* * Atomically update the file using a temporary file and the * traditional unlink then rename steps. This code provides * no locking, it only guarantees the packed nvlist on disk * is updated atomically and is internally consistent. */ char *tmpname = calloc(1, MAXPATHLEN); if (tmpname == NULL) { free(buf); return (ENOMEM); } snprintf(tmpname, MAXPATHLEN - 1, "%s.XXXXXX", filename); int fd = mkstemp(tmpname); if (fd < 0) { error = errno; free(buf); free(tmpname); return (error); } (void) close(fd); gzFile fp = gzopen(tmpname, "w9b"); if (fp == Z_NULL) { error = errno; free(buf); free(tmpname); return (errno); } ssize_t rc, bytes = 0; while (bytes < buflen) { size_t size = MIN(buflen - bytes, 131072); rc = gzwrite(fp, buf + bytes, size); if (rc < 0) { free(buf); (void) gzerror(fp, &error); (void) gzclose(fp); (void) unlink(tmpname); free(tmpname); return (error); } else if (rc == 0) { break; } else { bytes += rc; } } free(buf); (void) gzclose(fp); if (bytes != buflen) { (void) unlink(tmpname); free(tmpname); return (EIO); } /* * Unlink the previous config file and replace it with the updated * version. If we're able to unlink the file then directory is * writable by us and the subsequent rename should never fail. */ error = unlink(filename); if (error != 0 && errno != ENOENT) { error = errno; (void) unlink(tmpname); free(tmpname); return (error); } error = rename(tmpname, filename); if (error != 0) { error = errno; (void) unlink(tmpname); free(tmpname); return (error); } free(tmpname); return (0); } /* * Add the dRAID map to the file and write it out. */ static int write_map_key(const char *filename, char *key, draid_map_t *map, double worst_ratio, double avg_ratio) { nvlist_t *nv_cfg, *allcfgs; int error; /* * Add the configuration to an existing or new file. The new * configuration will replace an existing configuration with the * same key if it has a lower ratio and is therefore better. */ error = read_map(filename, &allcfgs); if (error == ENOENT) { allcfgs = fnvlist_alloc(); } else if (error != 0) { return (error); } error = nvlist_lookup_nvlist(allcfgs, key, &nv_cfg); if (error == 0) { uint64_t nv_cfg_worst_ratio = fnvlist_lookup_uint64(nv_cfg, MAP_WORST_RATIO); double nv_worst_ratio = (double)nv_cfg_worst_ratio / 1000.0; if (worst_ratio < nv_worst_ratio) { /* Replace old map with the more balanced new map. */ fnvlist_remove(allcfgs, key); } else { /* The old map is preferable, keep it. */ nvlist_free(allcfgs); return (EEXIST); } } nvlist_t *cfg = fnvlist_alloc(); fnvlist_add_uint64(cfg, MAP_SEED, map->dm_seed); fnvlist_add_uint64(cfg, MAP_CHECKSUM, map->dm_checksum); fnvlist_add_uint64(cfg, MAP_CHILDREN, map->dm_children); fnvlist_add_uint64(cfg, MAP_NPERMS, map->dm_nperms); fnvlist_add_uint8_array(cfg, MAP_PERMS, map->dm_perms, map->dm_children * map->dm_nperms * sizeof (uint8_t)); fnvlist_add_uint64(cfg, MAP_WORST_RATIO, (uint64_t)(worst_ratio * 1000.0)); fnvlist_add_uint64(cfg, MAP_AVG_RATIO, (uint64_t)(avg_ratio * 1000.0)); error = nvlist_add_nvlist(allcfgs, key, cfg); if (error == 0) error = write_map(filename, allcfgs); nvlist_free(cfg); nvlist_free(allcfgs); return (error); } static void dump_map(draid_map_t *map, const char *key, double worst_ratio, double avg_ratio, int verbose) { if (verbose == 0) { return; } else if (verbose == 1) { printf(" \"%s\": seed: 0x%016llx worst_ratio: %2.03f " "avg_ratio: %2.03f\n", key, (u_longlong_t)map->dm_seed, worst_ratio, avg_ratio); return; } else { printf(" \"%s\":\n" " seed: 0x%016llx\n" " checksum: 0x%016llx\n" " worst_ratio: %2.03f\n" " avg_ratio: %2.03f\n" " children: %llu\n" " nperms: %llu\n", key, (u_longlong_t)map->dm_seed, (u_longlong_t)map->dm_checksum, worst_ratio, avg_ratio, (u_longlong_t)map->dm_children, (u_longlong_t)map->dm_nperms); if (verbose > 2) { printf(" perms = {\n"); for (int i = 0; i < map->dm_nperms; i++) { printf(" { "); for (int j = 0; j < map->dm_children; j++) { printf("%3d%s ", map->dm_perms[ i * map->dm_children + j], j < map->dm_children - 1 ? "," : ""); } printf(" },\n"); } printf(" }\n"); } else if (verbose == 2) { printf(" draid_perms = \n"); } } } static void dump_map_nv(const char *key, nvlist_t *cfg, int verbose) { draid_map_t map; uint_t c; uint64_t worst_ratio = fnvlist_lookup_uint64(cfg, MAP_WORST_RATIO); uint64_t avg_ratio = fnvlist_lookup_uint64(cfg, MAP_AVG_RATIO); map.dm_seed = fnvlist_lookup_uint64(cfg, MAP_SEED); map.dm_checksum = fnvlist_lookup_uint64(cfg, MAP_CHECKSUM); map.dm_children = fnvlist_lookup_uint64(cfg, MAP_CHILDREN); map.dm_nperms = fnvlist_lookup_uint64(cfg, MAP_NPERMS); map.dm_perms = fnvlist_lookup_uint8_array(cfg, MAP_PERMS, &c); dump_map(&map, key, (double)worst_ratio / 1000.0, avg_ratio / 1000.0, verbose); } /* * Print a summary of the mapping. */ static int dump_map_key(const char *filename, const char *key, int verbose) { nvlist_t *cfg; int error; error = read_map_key(filename, key, &cfg); if (error != 0) return (error); dump_map_nv(key, cfg, verbose); return (0); } /* * Allocate a new permutation map for evaluation. */ static int alloc_new_map(uint64_t children, uint64_t nperms, uint64_t seed, draid_map_t **mapp) { draid_map_t *map; int error; map = malloc(sizeof (draid_map_t)); if (map == NULL) return (ENOMEM); map->dm_children = children; map->dm_nperms = nperms; map->dm_seed = seed; map->dm_checksum = 0; error = vdev_draid_generate_perms(map, &map->dm_perms); if (error) { free(map); return (error); } *mapp = map; return (0); } /* * Allocate the fixed permutation map for N children. */ static int alloc_fixed_map(uint64_t children, draid_map_t **mapp) { const draid_map_t *fixed_map; draid_map_t *map; int error; error = vdev_draid_lookup_map(children, &fixed_map); if (error) return (error); map = malloc(sizeof (draid_map_t)); if (map == NULL) return (ENOMEM); memcpy(map, fixed_map, sizeof (draid_map_t)); VERIFY3U(map->dm_checksum, !=, 0); error = vdev_draid_generate_perms(map, &map->dm_perms); if (error) { free(map); return (error); } *mapp = map; return (0); } /* * Free a permutation map. */ static void free_map(draid_map_t *map) { free(map->dm_perms); free(map); } /* * Check if dev is in the provided list of faulted devices. */ static inline boolean_t is_faulted(int *faulted_devs, int nfaulted, int dev) { for (int i = 0; i < nfaulted; i++) if (faulted_devs[i] == dev) return (B_TRUE); return (B_FALSE); } /* * Evaluate how resilvering I/O will be distributed given a list of faulted * vdevs. As a simplification we assume one IO is sufficient to repair each * damaged device in a group. */ static double eval_resilver(draid_map_t *map, uint64_t groupwidth, uint64_t nspares, int *faulted_devs, int nfaulted, int *min_child_ios, int *max_child_ios) { uint64_t children = map->dm_children; uint64_t ngroups = 1; uint64_t ndisks = children - nspares; /* * Calculate the minimum number of groups required to fill a slice. */ while (ngroups * (groupwidth) % (children - nspares) != 0) ngroups++; int *ios = calloc(map->dm_children, sizeof (uint64_t)); + ASSERT3P(ios, !=, NULL); + /* Resilver all rows */ for (int i = 0; i < map->dm_nperms; i++) { uint8_t *row = &map->dm_perms[i * map->dm_children]; /* Resilver all groups with faulted drives */ for (int j = 0; j < ngroups; j++) { uint64_t spareidx = map->dm_children - nspares; boolean_t repair_needed = B_FALSE; /* See if any devices in this group are faulted */ uint64_t groupstart = (j * groupwidth) % ndisks; for (int k = 0; k < groupwidth; k++) { uint64_t groupidx = (groupstart + k) % ndisks; repair_needed = is_faulted(faulted_devs, nfaulted, row[groupidx]); if (repair_needed) break; } if (repair_needed == B_FALSE) continue; /* * This group is degraded. Calculate the number of * reads the non-faulted drives require and the number * of writes to the distributed hot spare for this row. */ for (int k = 0; k < groupwidth; k++) { uint64_t groupidx = (groupstart + k) % ndisks; if (!is_faulted(faulted_devs, nfaulted, row[groupidx])) { ios[row[groupidx]]++; } else if (nspares > 0) { while (is_faulted(faulted_devs, nfaulted, row[spareidx])) { spareidx++; } ASSERT3U(spareidx, <, map->dm_children); ios[row[spareidx]]++; spareidx++; } } } } *min_child_ios = INT_MAX; *max_child_ios = 0; /* * Find the drives with fewest and most required I/O. These values * are used to calculate the imbalance ratio. To avoid returning an * infinite value for permutations which have children that perform * no IO a floor of 1 IO per child is set. This ensures a meaningful * ratio is returned for comparison and it is not an uncommon when * there are a large number of children. */ for (int i = 0; i < map->dm_children; i++) { if (is_faulted(faulted_devs, nfaulted, i)) { ASSERT0(ios[i]); continue; } if (ios[i] == 0) ios[i] = 1; if (ios[i] < *min_child_ios) *min_child_ios = ios[i]; if (ios[i] > *max_child_ios) *max_child_ios = ios[i]; } ASSERT3S(*min_child_ios, !=, INT_MAX); ASSERT3S(*max_child_ios, !=, 0); double ratio = (double)(*max_child_ios) / (double)(*min_child_ios); free(ios); return (ratio); } /* * Evaluate the quality of the permutation mapping by considering possible * device failures. Returns the imbalance ratio for the worst mapping which * is defined to be the largest number of child IOs over the fewest number * child IOs. A value of 1.0 indicates the mapping is perfectly balance and * all children perform an equal amount of work during reconstruction. */ static void eval_decluster(draid_map_t *map, double *worst_ratiop, double *avg_ratiop) { uint64_t children = map->dm_children; double worst_ratio = 1.0; double sum = 0; int worst_min_ios = 0, worst_max_ios = 0; int n = 0; /* * When there are only 2 children there can be no distributed * spare and no resilver to evaluate. Default to a ratio of 1.0 * for this degenerate case. */ if (children == VDEV_DRAID_MIN_CHILDREN) { *worst_ratiop = 1.0; *avg_ratiop = 1.0; return; } /* * Score the mapping as if it had either 1 or 2 distributed spares. */ for (int nspares = 1; nspares <= 2; nspares++) { uint64_t faults = nspares; /* * Score groupwidths up to 19. This value was chosen as the * largest reasonable width (16d+3p). dRAID pools may be still * be created with wider stripes but they are not considered in * this analysis in order to optimize for the most common cases. */ for (uint64_t groupwidth = 2; groupwidth <= MIN(children - nspares, 19); groupwidth++) { int faulted_devs[2]; int min_ios, max_ios; /* * Score possible devices faults. This is limited * to exactly one fault per distributed spare for * the purposes of this similation. */ for (int f1 = 0; f1 < children; f1++) { faulted_devs[0] = f1; double ratio; if (faults == 1) { ratio = eval_resilver(map, groupwidth, nspares, faulted_devs, faults, &min_ios, &max_ios); if (ratio > worst_ratio) { worst_ratio = ratio; worst_min_ios = min_ios; worst_max_ios = max_ios; } sum += ratio; n++; } else if (faults == 2) { for (int f2 = f1 + 1; f2 < children; f2++) { faulted_devs[1] = f2; ratio = eval_resilver(map, groupwidth, nspares, faulted_devs, faults, &min_ios, &max_ios); if (ratio > worst_ratio) { worst_ratio = ratio; worst_min_ios = min_ios; worst_max_ios = max_ios; } sum += ratio; n++; } } } } } *worst_ratiop = worst_ratio; *avg_ratiop = sum / n; /* * Log the min/max io values for particularly unbalanced maps. * Since the maps are generated entirely randomly these are possible * be exceedingly unlikely. We log it for possible investigation. */ if (worst_ratio > 100.0) { dump_map(map, "DEBUG", worst_ratio, *avg_ratiop, 2); printf("worst_min_ios=%d worst_max_ios=%d\n", worst_min_ios, worst_max_ios); } } static int eval_maps(uint64_t children, int passes, uint64_t *map_seed, draid_map_t **best_mapp, double *best_ratiop, double *avg_ratiop) { draid_map_t *best_map = NULL; double best_worst_ratio = 1000.0; double best_avg_ratio = 1000.0; /* * Perform the requested number of passes evaluating randomly * generated permutation maps. Only the best version is kept. */ for (int i = 0; i < passes; i++) { double worst_ratio, avg_ratio; draid_map_t *map; int error; /* * Calculate the next seed and generate a new candidate map. */ error = alloc_new_map(children, MAP_ROWS_DEFAULT, vdev_draid_rand(map_seed), &map); if (error) { if (best_map != NULL) free_map(best_map); return (error); } /* * Consider maps with a lower worst_ratio to be of higher * quality. Some maps may have a lower avg_ratio but they * are discarded since they might include some particularly * imbalanced permutations. The average is tracked to in * order to get a sense of the average permutation quality. */ eval_decluster(map, &worst_ratio, &avg_ratio); if (best_map == NULL || worst_ratio < best_worst_ratio) { if (best_map != NULL) free_map(best_map); best_map = map; best_worst_ratio = worst_ratio; best_avg_ratio = avg_ratio; } else { free_map(map); } } /* * After determining the best map generate a checksum over the full * permutation array. This checksum is verified when opening a dRAID * pool to ensure the generated in memory permutations are correct. */ zio_cksum_t cksum; fletcher_4_native_varsize(best_map->dm_perms, sizeof (uint8_t) * best_map->dm_children * best_map->dm_nperms, &cksum); best_map->dm_checksum = cksum.zc_word[0]; *best_mapp = best_map; *best_ratiop = best_worst_ratio; *avg_ratiop = best_avg_ratio; return (0); } static int draid_generate(int argc, char *argv[]) { char filename[MAXPATHLEN] = {0}; - uint64_t map_seed; + uint64_t map_seed[2]; int c, fd, error, verbose = 0, passes = 1, continuous = 0; int min_children = VDEV_DRAID_MIN_CHILDREN; int max_children = VDEV_DRAID_MAX_CHILDREN; int restarts = 0; while ((c = getopt(argc, argv, ":cm:n:p:v")) != -1) { switch (c) { case 'c': continuous++; break; case 'm': min_children = (int)strtol(optarg, NULL, 0); if (min_children < VDEV_DRAID_MIN_CHILDREN) { (void) fprintf(stderr, "A minimum of 2 " "children are required.\n"); return (1); } break; case 'n': max_children = (int)strtol(optarg, NULL, 0); if (max_children > VDEV_DRAID_MAX_CHILDREN) { (void) fprintf(stderr, "A maximum of %d " "children are allowed.\n", VDEV_DRAID_MAX_CHILDREN); return (1); } break; case 'p': passes = (int)strtol(optarg, NULL, 0); break; case 'v': /* * 0 - Only log when a better map is added to the file. * 1 - Log the current best map for each child count. * Minimal output on a single summary line. * 2 - Log the current best map for each child count. * More verbose includes most map fields. * 3 - Log the current best map for each child count. * Very verbose all fields including the full map. */ verbose++; break; case ':': (void) fprintf(stderr, "missing argument for '%c' option\n", optopt); draid_usage(); break; case '?': (void) fprintf(stderr, "invalid option '%c'\n", optopt); draid_usage(); break; } } if (argc > optind) strlcpy(filename, argv[optind], sizeof (filename)); else { (void) fprintf(stderr, "A FILE must be specified.\n"); return (1); } restart: /* * Start with a fresh seed from /dev/urandom. */ fd = open("/dev/urandom", O_RDONLY); if (fd < 0) { printf("Unable to open /dev/urandom: %s\n:", strerror(errno)); return (1); } else { ssize_t bytes = sizeof (map_seed); ssize_t bytes_read = 0; while (bytes_read < bytes) { - ssize_t rc = read(fd, ((char *)&map_seed) + bytes_read, + ssize_t rc = read(fd, ((char *)map_seed) + bytes_read, bytes - bytes_read); if (rc < 0) { printf("Unable to read /dev/urandom: %s\n:", strerror(errno)); close(fd); return (1); } bytes_read += rc; } (void) close(fd); } if (restarts == 0) printf("Writing generated mappings to '%s':\n", filename); /* * Generate maps for all requested child counts. The best map for * each child count is written out to the specified file. If the file * already contains a better mapping this map will not be added. */ for (uint64_t children = min_children; children <= max_children; children++) { char key[8] = { 0 }; draid_map_t *map; double worst_ratio = 1000.0; double avg_ratio = 1000.0; - error = eval_maps(children, passes, &map_seed, &map, + error = eval_maps(children, passes, map_seed, &map, &worst_ratio, &avg_ratio); if (error) { printf("Error eval_maps(): %s\n", strerror(error)); return (1); } if (worst_ratio < 1.0 || avg_ratio < 1.0) { printf("Error ratio < 1.0: worst_ratio = %2.03f " "avg_ratio = %2.03f\n", worst_ratio, avg_ratio); return (1); } snprintf(key, 7, "%llu", (u_longlong_t)children); error = write_map_key(filename, key, map, worst_ratio, avg_ratio); if (error == 0) { /* The new map was added to the file. */ dump_map(map, key, worst_ratio, avg_ratio, MAX(verbose, 1)); } else if (error == EEXIST) { /* The existing map was preferable and kept. */ if (verbose > 0) dump_map_key(filename, key, verbose); } else { printf("Error write_map_key(): %s\n", strerror(error)); return (1); } free_map(map); } /* * When the continuous option is set restart at the minimum number of * children instead of exiting. This option is useful as a mechanism * to continuous try and refine the discovered permutations. */ if (continuous) { restarts++; printf("Restarting by request (-c): %d\n", restarts); goto restart; } return (0); } /* * Verify each map in the file by generating its in-memory permutation array * and comfirming its checksum is correct. */ static int draid_verify(int argc, char *argv[]) { char filename[MAXPATHLEN] = {0}; int n = 0, c, error, verbose = 1; int check_ratios = 0; while ((c = getopt(argc, argv, ":rv")) != -1) { switch (c) { case 'r': check_ratios++; break; case 'v': verbose++; break; case ':': (void) fprintf(stderr, "missing argument for '%c' option\n", optopt); draid_usage(); break; case '?': (void) fprintf(stderr, "invalid option '%c'\n", optopt); draid_usage(); break; } } if (argc > optind) { char *abspath = malloc(MAXPATHLEN); if (abspath == NULL) return (ENOMEM); if (realpath(argv[optind], abspath) != NULL) strlcpy(filename, abspath, sizeof (filename)); else strlcpy(filename, argv[optind], sizeof (filename)); free(abspath); } else { (void) fprintf(stderr, "A FILE must be specified.\n"); return (1); } printf("Verifying permutation maps: '%s'\n", filename); /* * Lookup hardcoded permutation map for each valid number of children * and verify a generated map has the correct checksum. Then compare * the generated map values with the nvlist map values read from the * reference file to cross-check the permutation. */ for (uint64_t children = VDEV_DRAID_MIN_CHILDREN; children <= VDEV_DRAID_MAX_CHILDREN; children++) { draid_map_t *map; char key[8] = {0}; snprintf(key, 8, "%llu", (u_longlong_t)children); error = alloc_fixed_map(children, &map); if (error) { printf("Error alloc_fixed_map() failed: %s\n", error == ECKSUM ? "Invalid checksum" : strerror(error)); return (1); } uint64_t nv_seed, nv_checksum, nv_children, nv_nperms; uint8_t *nv_perms; nvlist_t *cfg; uint_t c; error = read_map_key(filename, key, &cfg); if (error != 0) { printf("Error read_map_key() failed: %s\n", strerror(error)); free_map(map); return (1); } nv_seed = fnvlist_lookup_uint64(cfg, MAP_SEED); nv_checksum = fnvlist_lookup_uint64(cfg, MAP_CHECKSUM); nv_children = fnvlist_lookup_uint64(cfg, MAP_CHILDREN); nv_nperms = fnvlist_lookup_uint64(cfg, MAP_NPERMS); nvlist_lookup_uint8_array(cfg, MAP_PERMS, &nv_perms, &c); /* * Compare draid_map_t and nvlist reference values. */ if (map->dm_seed != nv_seed) { printf("Error different seeds: 0x%016llx != " "0x%016llx\n", (u_longlong_t)map->dm_seed, (u_longlong_t)nv_seed); error = EINVAL; } if (map->dm_checksum != nv_checksum) { printf("Error different checksums: 0x%016llx " "!= 0x%016llx\n", (u_longlong_t)map->dm_checksum, (u_longlong_t)nv_checksum); error = EINVAL; } if (map->dm_children != nv_children) { printf("Error different children: %llu " "!= %llu\n", (u_longlong_t)map->dm_children, (u_longlong_t)nv_children); error = EINVAL; } if (map->dm_nperms != nv_nperms) { printf("Error different nperms: %llu " "!= %llu\n", (u_longlong_t)map->dm_nperms, (u_longlong_t)nv_nperms); error = EINVAL; } for (uint64_t i = 0; i < nv_children * nv_nperms; i++) { if (map->dm_perms[i] != nv_perms[i]) { printf("Error different perms[%llu]: " "%d != %d\n", (u_longlong_t)i, (int)map->dm_perms[i], (int)nv_perms[i]); error = EINVAL; break; } } /* * For good measure recalculate the worst and average * ratios and confirm they match the nvlist values. */ if (check_ratios) { uint64_t nv_worst_ratio, nv_avg_ratio; double worst_ratio, avg_ratio; eval_decluster(map, &worst_ratio, &avg_ratio); nv_worst_ratio = fnvlist_lookup_uint64(cfg, MAP_WORST_RATIO); nv_avg_ratio = fnvlist_lookup_uint64(cfg, MAP_AVG_RATIO); if (worst_ratio < 1.0 || avg_ratio < 1.0) { printf("Error ratio out of range %2.03f, " "%2.03f\n", worst_ratio, avg_ratio); error = EINVAL; } if ((uint64_t)(worst_ratio * 1000.0) != nv_worst_ratio) { printf("Error different worst_ratio %2.03f " "!= %2.03f\n", (double)nv_worst_ratio / 1000.0, worst_ratio); error = EINVAL; } if ((uint64_t)(avg_ratio * 1000.0) != nv_avg_ratio) { printf("Error different average_ratio %2.03f " "!= %2.03f\n", (double)nv_avg_ratio / 1000.0, avg_ratio); error = EINVAL; } } if (error) { free_map(map); nvlist_free(cfg); return (1); } if (verbose > 0) { printf("- %llu children: good\n", (u_longlong_t)children); } n++; free_map(map); nvlist_free(cfg); } if (n != (VDEV_DRAID_MAX_CHILDREN - 1)) { printf("Error permutation maps missing: %d / %d checked\n", n, VDEV_DRAID_MAX_CHILDREN - 1); return (1); } printf("Successfully verified %d / %d permutation maps\n", n, VDEV_DRAID_MAX_CHILDREN - 1); return (0); } /* * Dump the contents of the specified mapping(s) for inspection. */ static int draid_dump(int argc, char *argv[]) { char filename[MAXPATHLEN] = {0}; int c, error, verbose = 1; int min_children = VDEV_DRAID_MIN_CHILDREN; int max_children = VDEV_DRAID_MAX_CHILDREN; while ((c = getopt(argc, argv, ":vm:n:")) != -1) { switch (c) { case 'm': min_children = (int)strtol(optarg, NULL, 0); if (min_children < 2) { (void) fprintf(stderr, "A minimum of 2 " "children are required.\n"); return (1); } break; case 'n': max_children = (int)strtol(optarg, NULL, 0); if (max_children > VDEV_DRAID_MAX_CHILDREN) { (void) fprintf(stderr, "A maximum of %d " "children are allowed.\n", VDEV_DRAID_MAX_CHILDREN); return (1); } break; case 'v': verbose++; break; case ':': (void) fprintf(stderr, "missing argument for '%c' option\n", optopt); draid_usage(); break; case '?': (void) fprintf(stderr, "invalid option '%c'\n", optopt); draid_usage(); break; } } if (argc > optind) strlcpy(filename, argv[optind], sizeof (filename)); else { (void) fprintf(stderr, "A FILE must be specified.\n"); return (1); } /* * Dump maps for the requested child counts. */ for (uint64_t children = min_children; children <= max_children; children++) { char key[8] = { 0 }; snprintf(key, 7, "%llu", (u_longlong_t)children); error = dump_map_key(filename, key, verbose); if (error) { printf("Error dump_map_key(): %s\n", strerror(error)); return (1); } } return (0); } /* * Print all of the mappings as a C formatted draid_map_t array. This table * is found in the module/zcommon/zfs_draid.c file and is the definitive * source for all mapping used by dRAID. It cannot be updated without * changing the dRAID on disk format. */ static int draid_table(int argc, char *argv[]) { char filename[MAXPATHLEN] = {0}; int error; if (argc > optind) strlcpy(filename, argv[optind], sizeof (filename)); else { (void) fprintf(stderr, "A FILE must be specified.\n"); return (1); } printf("static const draid_map_t " "draid_maps[VDEV_DRAID_MAX_MAPS] = {\n"); for (uint64_t children = VDEV_DRAID_MIN_CHILDREN; children <= VDEV_DRAID_MAX_CHILDREN; children++) { uint64_t seed, checksum, nperms, avg_ratio; nvlist_t *cfg; char key[8] = {0}; snprintf(key, 8, "%llu", (u_longlong_t)children); error = read_map_key(filename, key, &cfg); if (error != 0) { printf("Error read_map_key() failed: %s\n", strerror(error)); return (1); } seed = fnvlist_lookup_uint64(cfg, MAP_SEED); checksum = fnvlist_lookup_uint64(cfg, MAP_CHECKSUM); children = fnvlist_lookup_uint64(cfg, MAP_CHILDREN); nperms = fnvlist_lookup_uint64(cfg, MAP_NPERMS); avg_ratio = fnvlist_lookup_uint64(cfg, MAP_AVG_RATIO); printf("\t{ %3llu, %3llu, 0x%016llx, 0x%016llx },\t" "/* %2.03f */\n", (u_longlong_t)children, (u_longlong_t)nperms, (u_longlong_t)seed, (u_longlong_t)checksum, (double)avg_ratio / 1000.0); nvlist_free(cfg); } printf("};\n"); return (0); } static int draid_merge_impl(nvlist_t *allcfgs, const char *srcfilename, int *mergedp) { nvlist_t *srccfgs; nvpair_t *elem = NULL; int error, merged = 0; error = read_map(srcfilename, &srccfgs); if (error != 0) return (error); while ((elem = nvlist_next_nvpair(srccfgs, elem)) != NULL) { uint64_t nv_worst_ratio; uint64_t allcfg_worst_ratio; nvlist_t *cfg, *allcfg; char *key; switch (nvpair_type(elem)) { case DATA_TYPE_NVLIST: (void) nvpair_value_nvlist(elem, &cfg); key = nvpair_name(elem); nv_worst_ratio = fnvlist_lookup_uint64(cfg, MAP_WORST_RATIO); error = nvlist_lookup_nvlist(allcfgs, key, &allcfg); if (error == 0) { allcfg_worst_ratio = fnvlist_lookup_uint64( allcfg, MAP_WORST_RATIO); if (nv_worst_ratio < allcfg_worst_ratio) { fnvlist_remove(allcfgs, key); error = nvlist_add_nvlist(allcfgs, key, cfg); merged++; } } else if (error == ENOENT) { error = nvlist_add_nvlist(allcfgs, key, cfg); merged++; } else { return (error); } break; default: continue; } } nvlist_free(srccfgs); *mergedp = merged; return (0); } /* * Merge the best map for each child count found in the listed files into * a new file. This allows 'draid generate' to be run in parallel and for * the results maps to be combined. */ static int draid_merge(int argc, char *argv[]) { char filename[MAXPATHLEN] = {0}; int c, error, total_merged = 0; nvlist_t *allcfgs; while ((c = getopt(argc, argv, ":")) != -1) { switch (c) { case ':': (void) fprintf(stderr, "missing argument for '%c' option\n", optopt); draid_usage(); break; case '?': (void) fprintf(stderr, "invalid option '%c'\n", optopt); draid_usage(); break; } } if (argc < 4) { (void) fprintf(stderr, "A FILE and multiple SRCs must be specified.\n"); return (1); } strlcpy(filename, argv[optind], sizeof (filename)); optind++; error = read_map(filename, &allcfgs); if (error == ENOENT) { allcfgs = fnvlist_alloc(); } else if (error != 0) { printf("Error read_map(): %s\n", strerror(error)); return (error); } while (optind < argc) { char srcfilename[MAXPATHLEN] = {0}; int merged = 0; strlcpy(srcfilename, argv[optind], sizeof (srcfilename)); error = draid_merge_impl(allcfgs, srcfilename, &merged); if (error) { printf("Error draid_merge_impl(): %s\n", strerror(error)); nvlist_free(allcfgs); return (1); } total_merged += merged; printf("Merged %d key(s) from '%s' into '%s'\n", merged, srcfilename, filename); optind++; } if (total_merged > 0) write_map(filename, allcfgs); printf("Merged a total of %d key(s) into '%s'\n", total_merged, filename); nvlist_free(allcfgs); return (0); } int main(int argc, char *argv[]) { if (argc < 2) draid_usage(); char *subcommand = argv[1]; if (strcmp(subcommand, "generate") == 0) { return (draid_generate(argc - 1, argv + 1)); } else if (strcmp(subcommand, "verify") == 0) { return (draid_verify(argc - 1, argv + 1)); } else if (strcmp(subcommand, "dump") == 0) { return (draid_dump(argc - 1, argv + 1)); } else if (strcmp(subcommand, "table") == 0) { return (draid_table(argc - 1, argv + 1)); } else if (strcmp(subcommand, "merge") == 0) { return (draid_merge(argc - 1, argv + 1)); } else { draid_usage(); } } diff --git a/tests/zfs-tests/cmd/file/file_trunc.c b/tests/zfs-tests/cmd/file/file_trunc.c index 2085f5955ea1..9e3bba24afdf 100644 --- a/tests/zfs-tests/cmd/file/file_trunc.c +++ b/tests/zfs-tests/cmd/file/file_trunc.c @@ -1,240 +1,240 @@ /* * 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 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ /* * Copyright (c) 2012, 2014 by Delphix. All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define FSIZE 256*1024*1024 #define BSIZE 512 /* Initialize Globals */ static long fsize = FSIZE; static size_t bsize = BSIZE; static int count = 0; static int rflag = 0; -static int seed = 0; +static uint_t seed = 0; static int vflag = 0; static int errflag = 0; static off_t offset = 0; static char *filename = NULL; static void usage(char *execname); static void parse_options(int argc, char *argv[]); static void do_write(int fd); static void do_trunc(int fd); static void usage(char *execname) { (void) fprintf(stderr, "usage: %s [-b blocksize] [-c count] [-f filesize]" " [-o offset] [-s seed] [-r] [-v] filename\n", execname); (void) exit(1); } int main(int argc, char *argv[]) { int i = 0; int fd = -1; parse_options(argc, argv); fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666); if (fd < 0) { perror("open"); exit(3); } for (i = 0; count == 0 || i < count; i++) { (void) do_write(fd); (void) do_trunc(fd); } (void) close(fd); return (0); } static void parse_options(int argc, char *argv[]) { int c; extern char *optarg; extern int optind, optopt; count = fsize / bsize; - seed = time(NULL); + seed = (uint_t)time(NULL); while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) { switch (c) { case 'b': bsize = atoi(optarg); break; case 'c': count = atoi(optarg); break; case 'f': fsize = atoi(optarg); break; case 'o': offset = atoi(optarg); break; case 'r': rflag++; break; case 's': seed = atoi(optarg); break; case 'v': vflag++; break; case ':': (void) fprintf(stderr, "Option -%c requires an operand\n", optopt); errflag++; break; case '?': (void) fprintf(stderr, "Unrecognized option: -%c\n", optopt); errflag++; break; } if (errflag) { (void) usage(argv[0]); } } if (argc <= optind) { (void) fprintf(stderr, "No filename specified\n"); usage(argv[0]); } filename = argv[optind]; if (vflag) { (void) fprintf(stderr, "Seed = %d\n", seed); } srandom(seed); } static void do_write(int fd) { off_t roffset = 0; char *buf = NULL; char *rbuf = NULL; buf = (char *)calloc(1, bsize); rbuf = (char *)calloc(1, bsize); if (buf == NULL || rbuf == NULL) { perror("malloc"); exit(4); } roffset = random() % fsize; if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) { perror("lseek"); exit(5); } (void) strcpy(buf, "ZFS Test Suite Truncation Test"); if (write(fd, buf, bsize) < bsize) { perror("write"); exit(6); } if (rflag) { if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) { perror("lseek"); exit(7); } if (read(fd, rbuf, bsize) < bsize) { perror("read"); exit(8); } if (memcmp(buf, rbuf, bsize) != 0) { perror("memcmp"); exit(9); } } if (vflag) { (void) fprintf(stderr, "Wrote to offset %" PRId64 "\n", (offset + roffset)); if (rflag) { (void) fprintf(stderr, "Read back from offset %" PRId64 "\n", (offset + roffset)); } } (void) free(buf); (void) free(rbuf); } static void do_trunc(int fd) { off_t roffset = 0; roffset = random() % fsize; if (ftruncate64(fd, (offset + roffset)) < 0) { perror("truncate"); exit(7); } if (vflag) { (void) fprintf(stderr, "Truncated at offset %" PRId64 "\n", (offset + roffset)); } } diff --git a/tests/zfs-tests/cmd/file/randwritecomp.c b/tests/zfs-tests/cmd/file/randwritecomp.c index cc70d1212f84..2d5c0ec8643c 100644 --- a/tests/zfs-tests/cmd/file/randwritecomp.c +++ b/tests/zfs-tests/cmd/file/randwritecomp.c @@ -1,187 +1,192 @@ /* * 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 * http://www.illumos.org/license/CDDL. */ /* * Copyright (c) 2017 by Delphix. All rights reserved. */ #include #include #include "file_common.h" /* * The following sample was derived from real-world data * of a production Oracle database. */ static const uint64_t size_distribution[] = { 0, 1499018, 352084, 1503485, 4206227, 5626657, 5387001, 3733756, 2233094, 874652, 238635, 81434, 33357, 13106, 2009, 1, 23660, }; static uint64_t distribution_n; static uint8_t randbuf[BLOCKSZ]; static void rwc_pwrite(int fd, const void *buf, size_t nbytes, off_t offset) { size_t nleft = nbytes; ssize_t nwrite = 0; nwrite = pwrite(fd, buf, nbytes, offset); if (nwrite < 0) { perror("pwrite"); exit(EXIT_FAILURE); } nleft -= nwrite; if (nleft != 0) { (void) fprintf(stderr, "warning: pwrite: " "wrote %zu out of %zu bytes\n", (nbytes - nleft), nbytes); } } static void fillbuf(char *buf) { uint64_t rv = lrand48() % distribution_n; uint64_t sum = 0; uint64_t i; for (i = 0; i < sizeof (size_distribution) / sizeof (size_distribution[0]); i++) { sum += size_distribution[i]; if (rv < sum) break; } memcpy(buf, randbuf, BLOCKSZ); if (i == 0) memset(buf, 0, BLOCKSZ - 10); else if (i < 16) memset(buf, 0, BLOCKSZ - i * 512 + 256); /*LINTED: E_BAD_PTR_CAST_ALIGN*/ ((uint32_t *)buf)[0] = lrand48(); } static void exit_usage(void) { (void) puts("usage: randwritecomp [-s] file [nwrites]"); exit(EXIT_FAILURE); } static void sequential_writes(int fd, char *buf, uint64_t nblocks, int64_t n) { for (int64_t i = 0; n == -1 || i < n; i++) { fillbuf(buf); static uint64_t j = 0; if (j == 0) j = lrand48() % nblocks; rwc_pwrite(fd, buf, BLOCKSZ, j * BLOCKSZ); j++; if (j >= nblocks) j = 0; } } static void random_writes(int fd, char *buf, uint64_t nblocks, int64_t n) { for (int64_t i = 0; n == -1 || i < n; i++) { fillbuf(buf); rwc_pwrite(fd, buf, BLOCKSZ, (lrand48() % nblocks) * BLOCKSZ); } } int main(int argc, char *argv[]) { int fd, err; char *filename = NULL; char buf[BLOCKSZ]; struct stat ss; uint64_t nblocks; int64_t n = -1; int sequential = 0; if (argc < 2) exit_usage(); argv++; if (strcmp("-s", argv[0]) == 0) { sequential = 1; argv++; } if (argv[0] == NULL) exit_usage(); else filename = argv[0]; argv++; if (argv[0] != NULL) n = strtoull(argv[0], NULL, 0); fd = open(filename, O_RDWR|O_CREAT, 0666); + if (fd == -1) { + (void) fprintf(stderr, "open(%s) failed: %s\n", filename, + strerror(errno)); + exit(EXIT_FAILURE); + } err = fstat(fd, &ss); if (err != 0) { (void) fprintf(stderr, "error: fstat returned error code %d\n", err); exit(EXIT_FAILURE); } nblocks = ss.st_size / BLOCKSZ; if (nblocks == 0) { (void) fprintf(stderr, "error: " "file is too small (min allowed size is %d bytes)\n", BLOCKSZ); exit(EXIT_FAILURE); } srand48(getpid()); for (int i = 0; i < BLOCKSZ; i++) randbuf[i] = lrand48(); distribution_n = 0; for (uint64_t i = 0; i < sizeof (size_distribution) / sizeof (size_distribution[0]); i++) { distribution_n += size_distribution[i]; } if (sequential) sequential_writes(fd, buf, nblocks, n); else random_writes(fd, buf, nblocks, n); return (0); } diff --git a/tests/zfs-tests/cmd/get_diff.c b/tests/zfs-tests/cmd/get_diff.c index 3f8fe787f7b9..61467f25014c 100644 --- a/tests/zfs-tests/cmd/get_diff.c +++ b/tests/zfs-tests/cmd/get_diff.c @@ -1,108 +1,108 @@ /* * 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 * http://www.illumos.org/license/CDDL. */ /* * Copyright (c) 2018 by Delphix. All rights reserved. */ #include #include #include #include #include #include #include #include #include #include static void usage(const char *msg, int exit_value) { (void) fprintf(stderr, "usage: get_diff file redacted_file\n%s\n", msg); exit(exit_value); } /* * This utility compares two files, an original and its redacted counterpart * (in that order). It compares the files 512 bytes at a time, printing out * any ranges (as offset and length) where the redacted file does not match * the original. This output is used to verify that the expected ranges of * a redacted file do not contain the original data. */ int main(int argc, char *argv[]) { off_t diff_off = 0, diff_len = 0, off = 0; int fd1, fd2; char *fname1, *fname2; char buf1[DEV_BSIZE], buf2[DEV_BSIZE]; ssize_t bytes; if (argc != 3) usage("Incorrect number of arguments.", 1); if ((fname1 = argv[1]) == NULL) usage("Filename missing.", 1); if ((fd1 = open(fname1, O_LARGEFILE | O_RDONLY)) < 0) { perror("open1 failed"); exit(1); } if ((fname2 = argv[2]) == NULL) usage("Redacted filename missing.", 1); if ((fd2 = open(fname2, O_LARGEFILE | O_RDONLY)) < 0) { perror("open2 failed"); exit(1); } while ((bytes = pread(fd1, buf1, DEV_BSIZE, off)) > 0) { if (pread(fd2, buf2, DEV_BSIZE, off) < 0) { if (errno == EIO) { /* * A read in a redacted section of a file will * fail with EIO. If we get EIO, continue on * but ensure that a comparison of buf1 and * buf2 will fail, indicating a redacted block. */ buf2[0] = ~buf1[0]; } else { perror("pread failed"); exit(1); } } if (memcmp(buf1, buf2, bytes) == 0) { if (diff_len != 0) { (void) fprintf(stdout, "%lld,%lld\n", (long long)diff_off, (long long)diff_len); assert(off == diff_off + diff_len); diff_len = 0; } diff_off = 0; } else { if (diff_len == 0) diff_off = off; assert(off == diff_off + diff_len); diff_len += bytes; } off += bytes; } - if (diff_len != 0 && diff_len != 0) { + if (diff_len != 0) { (void) fprintf(stdout, "%lld,%lld\n", (long long)diff_off, (long long)diff_len); } (void) close(fd1); (void) close(fd2); return (0); } diff --git a/tests/zfs-tests/cmd/libzfs_input_check.c b/tests/zfs-tests/cmd/libzfs_input_check.c index 434cc863f36c..2e1859b1eef0 100644 --- a/tests/zfs-tests/cmd/libzfs_input_check.c +++ b/tests/zfs-tests/cmd/libzfs_input_check.c @@ -1,1063 +1,1067 @@ /* * CDDL HEADER START * * 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 * http://www.illumos.org/license/CDDL. * * CDDL HEADER END */ /* * Copyright (c) 2018 by Delphix. All rights reserved. */ #include #include #include #include #include #include #include #include #include /* * Test the nvpair inputs for the non-legacy zfs ioctl commands. */ static boolean_t unexpected_failures; static int zfs_fd; static const char *active_test; /* * Tracks which zfs_ioc_t commands were tested */ static boolean_t ioc_tested[ZFS_IOC_LAST - ZFS_IOC_FIRST]; /* * Legacy ioctls that are skipped (for now) */ static const zfs_ioc_t ioc_skip[] = { ZFS_IOC_POOL_CREATE, ZFS_IOC_POOL_DESTROY, ZFS_IOC_POOL_IMPORT, ZFS_IOC_POOL_EXPORT, ZFS_IOC_POOL_CONFIGS, ZFS_IOC_POOL_STATS, ZFS_IOC_POOL_TRYIMPORT, ZFS_IOC_POOL_SCAN, ZFS_IOC_POOL_FREEZE, ZFS_IOC_POOL_UPGRADE, ZFS_IOC_POOL_GET_HISTORY, ZFS_IOC_VDEV_ADD, ZFS_IOC_VDEV_REMOVE, ZFS_IOC_VDEV_SET_STATE, ZFS_IOC_VDEV_ATTACH, ZFS_IOC_VDEV_DETACH, ZFS_IOC_VDEV_SETPATH, ZFS_IOC_VDEV_SETFRU, ZFS_IOC_OBJSET_STATS, ZFS_IOC_OBJSET_ZPLPROPS, ZFS_IOC_DATASET_LIST_NEXT, ZFS_IOC_SNAPSHOT_LIST_NEXT, ZFS_IOC_SET_PROP, ZFS_IOC_DESTROY, ZFS_IOC_RENAME, ZFS_IOC_RECV, ZFS_IOC_SEND, ZFS_IOC_INJECT_FAULT, ZFS_IOC_CLEAR_FAULT, ZFS_IOC_INJECT_LIST_NEXT, ZFS_IOC_ERROR_LOG, ZFS_IOC_CLEAR, ZFS_IOC_PROMOTE, ZFS_IOC_DSOBJ_TO_DSNAME, ZFS_IOC_OBJ_TO_PATH, ZFS_IOC_POOL_SET_PROPS, ZFS_IOC_POOL_GET_PROPS, ZFS_IOC_SET_FSACL, ZFS_IOC_GET_FSACL, ZFS_IOC_SHARE, ZFS_IOC_INHERIT_PROP, ZFS_IOC_SMB_ACL, ZFS_IOC_USERSPACE_ONE, ZFS_IOC_USERSPACE_MANY, ZFS_IOC_USERSPACE_UPGRADE, ZFS_IOC_OBJSET_RECVD_PROPS, ZFS_IOC_VDEV_SPLIT, ZFS_IOC_NEXT_OBJ, ZFS_IOC_DIFF, ZFS_IOC_TMP_SNAPSHOT, ZFS_IOC_OBJ_TO_STATS, ZFS_IOC_SPACE_WRITTEN, ZFS_IOC_POOL_REGUID, ZFS_IOC_SEND_PROGRESS, ZFS_IOC_EVENTS_NEXT, ZFS_IOC_EVENTS_CLEAR, ZFS_IOC_EVENTS_SEEK, ZFS_IOC_NEXTBOOT, ZFS_IOC_JAIL, ZFS_IOC_UNJAIL, }; #define IOC_INPUT_TEST(ioc, name, req, opt, err) \ IOC_INPUT_TEST_IMPL(ioc, name, req, opt, err, B_FALSE) #define IOC_INPUT_TEST_WILD(ioc, name, req, opt, err) \ IOC_INPUT_TEST_IMPL(ioc, name, req, opt, err, B_TRUE) #define IOC_INPUT_TEST_IMPL(ioc, name, req, opt, err, wild) \ do { \ active_test = __func__ + 5; \ ioc_tested[ioc - ZFS_IOC_FIRST] = B_TRUE; \ lzc_ioctl_test(ioc, name, req, opt, err, wild); \ } while (0) /* * run a zfs ioctl command, verify expected results and log failures */ static void lzc_ioctl_run(zfs_ioc_t ioc, const char *name, nvlist_t *innvl, int expected) { zfs_cmd_t zc = {"\0"}; char *packed = NULL; const char *variant; size_t size = 0; int error = 0; switch (expected) { case ZFS_ERR_IOC_ARG_UNAVAIL: variant = "unsupported input"; break; case ZFS_ERR_IOC_ARG_REQUIRED: variant = "missing input"; break; case ZFS_ERR_IOC_ARG_BADTYPE: variant = "invalid input type"; break; default: variant = "valid input"; break; } packed = fnvlist_pack(innvl, &size); (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); zc.zc_name[sizeof (zc.zc_name) - 1] = '\0'; zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed; zc.zc_nvlist_src_size = size; zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024); zc.zc_nvlist_dst = (uint64_t)(uintptr_t)malloc(zc.zc_nvlist_dst_size); if (lzc_ioctl_fd(zfs_fd, ioc, &zc) != 0) error = errno; if (error != expected) { unexpected_failures = B_TRUE; (void) fprintf(stderr, "%s: Unexpected result with %s, " "error %d (expecting %d)\n", active_test, variant, error, expected); } fnvlist_pack_free(packed, size); free((void *)(uintptr_t)zc.zc_nvlist_dst); } /* * Test each ioc for the following ioctl input errors: * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type */ static int lzc_ioctl_test(zfs_ioc_t ioc, const char *name, nvlist_t *required, nvlist_t *optional, int expected_error, boolean_t wildcard) { nvlist_t *input = fnvlist_alloc(); nvlist_t *future = fnvlist_alloc(); int error = 0; if (required != NULL) { for (nvpair_t *pair = nvlist_next_nvpair(required, NULL); pair != NULL; pair = nvlist_next_nvpair(required, pair)) { fnvlist_add_nvpair(input, pair); } } if (optional != NULL) { for (nvpair_t *pair = nvlist_next_nvpair(optional, NULL); pair != NULL; pair = nvlist_next_nvpair(optional, pair)) { fnvlist_add_nvpair(input, pair); } } /* * Generic input run with 'optional' nvlist pair */ if (!wildcard) fnvlist_add_nvlist(input, "optional", future); lzc_ioctl_run(ioc, name, input, expected_error); if (!wildcard) fnvlist_remove(input, "optional"); /* * Bogus input value */ if (!wildcard) { fnvlist_add_string(input, "bogus_input", "bogus"); lzc_ioctl_run(ioc, name, input, ZFS_ERR_IOC_ARG_UNAVAIL); fnvlist_remove(input, "bogus_input"); } /* * Missing required inputs */ if (required != NULL) { nvlist_t *empty = fnvlist_alloc(); lzc_ioctl_run(ioc, name, empty, ZFS_ERR_IOC_ARG_REQUIRED); nvlist_free(empty); } /* * Wrong nvpair type */ if (required != NULL || optional != NULL) { /* * switch the type of one of the input pairs */ for (nvpair_t *pair = nvlist_next_nvpair(input, NULL); pair != NULL; pair = nvlist_next_nvpair(input, pair)) { char pname[MAXNAMELEN]; data_type_t ptype; strlcpy(pname, nvpair_name(pair), sizeof (pname)); pname[sizeof (pname) - 1] = '\0'; ptype = nvpair_type(pair); fnvlist_remove_nvpair(input, pair); switch (ptype) { case DATA_TYPE_STRING: fnvlist_add_uint64(input, pname, 42); break; default: fnvlist_add_string(input, pname, "bogus"); break; } } lzc_ioctl_run(ioc, name, input, ZFS_ERR_IOC_ARG_BADTYPE); } nvlist_free(future); nvlist_free(input); return (error); } static void test_pool_sync(const char *pool) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_boolean_value(required, "force", B_TRUE); IOC_INPUT_TEST(ZFS_IOC_POOL_SYNC, pool, required, NULL, 0); nvlist_free(required); } static void test_pool_reopen(const char *pool) { nvlist_t *optional = fnvlist_alloc(); fnvlist_add_boolean_value(optional, "scrub_restart", B_FALSE); IOC_INPUT_TEST(ZFS_IOC_POOL_REOPEN, pool, NULL, optional, 0); nvlist_free(optional); } static void test_pool_checkpoint(const char *pool) { IOC_INPUT_TEST(ZFS_IOC_POOL_CHECKPOINT, pool, NULL, NULL, 0); } static void test_pool_discard_checkpoint(const char *pool) { int err = lzc_pool_checkpoint(pool); if (err == 0 || err == ZFS_ERR_CHECKPOINT_EXISTS) IOC_INPUT_TEST(ZFS_IOC_POOL_DISCARD_CHECKPOINT, pool, NULL, NULL, 0); } static void test_log_history(const char *pool) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_string(required, "message", "input check"); IOC_INPUT_TEST(ZFS_IOC_LOG_HISTORY, pool, required, NULL, 0); nvlist_free(required); } static void test_create(const char *pool) { char dataset[MAXNAMELEN + 32]; (void) snprintf(dataset, sizeof (dataset), "%s/create-fs", pool); nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *props = fnvlist_alloc(); fnvlist_add_int32(required, "type", DMU_OST_ZFS); fnvlist_add_uint64(props, "recordsize", 8192); fnvlist_add_nvlist(optional, "props", props); IOC_INPUT_TEST(ZFS_IOC_CREATE, dataset, required, optional, 0); nvlist_free(required); nvlist_free(optional); } static void test_snapshot(const char *pool, const char *snapshot) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *snaps = fnvlist_alloc(); nvlist_t *props = fnvlist_alloc(); fnvlist_add_boolean(snaps, snapshot); fnvlist_add_nvlist(required, "snaps", snaps); fnvlist_add_string(props, "org.openzfs:launch", "September 17th, 2013"); fnvlist_add_nvlist(optional, "props", props); IOC_INPUT_TEST(ZFS_IOC_SNAPSHOT, pool, required, optional, 0); nvlist_free(props); nvlist_free(snaps); nvlist_free(optional); nvlist_free(required); } static void test_space_snaps(const char *snapshot) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_string(required, "firstsnap", snapshot); IOC_INPUT_TEST(ZFS_IOC_SPACE_SNAPS, snapshot, required, NULL, 0); nvlist_free(required); } static void test_destroy_snaps(const char *pool, const char *snapshot) { nvlist_t *required = fnvlist_alloc(); nvlist_t *snaps = fnvlist_alloc(); fnvlist_add_boolean(snaps, snapshot); fnvlist_add_nvlist(required, "snaps", snaps); IOC_INPUT_TEST(ZFS_IOC_DESTROY_SNAPS, pool, required, NULL, 0); nvlist_free(snaps); nvlist_free(required); } static void test_bookmark(const char *pool, const char *snapshot, const char *bookmark) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_string(required, bookmark, snapshot); IOC_INPUT_TEST_WILD(ZFS_IOC_BOOKMARK, pool, required, NULL, 0); nvlist_free(required); } static void test_get_bookmarks(const char *dataset) { nvlist_t *optional = fnvlist_alloc(); fnvlist_add_boolean(optional, "guid"); fnvlist_add_boolean(optional, "createtxg"); fnvlist_add_boolean(optional, "creation"); IOC_INPUT_TEST_WILD(ZFS_IOC_GET_BOOKMARKS, dataset, NULL, optional, 0); nvlist_free(optional); } static void test_destroy_bookmarks(const char *pool, const char *bookmark) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_boolean(required, bookmark); IOC_INPUT_TEST_WILD(ZFS_IOC_DESTROY_BOOKMARKS, pool, required, NULL, 0); nvlist_free(required); } static void test_clone(const char *snapshot, const char *clone) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *props = fnvlist_alloc(); fnvlist_add_string(required, "origin", snapshot); IOC_INPUT_TEST(ZFS_IOC_CLONE, clone, required, NULL, 0); nvlist_free(props); nvlist_free(optional); nvlist_free(required); } static void test_rollback(const char *dataset, const char *snapshot) { nvlist_t *optional = fnvlist_alloc(); fnvlist_add_string(optional, "target", snapshot); IOC_INPUT_TEST(ZFS_IOC_ROLLBACK, dataset, NULL, optional, B_FALSE); nvlist_free(optional); } static void test_hold(const char *pool, const char *snapshot) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *holds = fnvlist_alloc(); fnvlist_add_string(holds, snapshot, "libzfs_check_hold"); fnvlist_add_nvlist(required, "holds", holds); fnvlist_add_int32(optional, "cleanup_fd", zfs_fd); IOC_INPUT_TEST(ZFS_IOC_HOLD, pool, required, optional, 0); nvlist_free(holds); nvlist_free(optional); nvlist_free(required); } static void test_get_holds(const char *snapshot) { IOC_INPUT_TEST(ZFS_IOC_GET_HOLDS, snapshot, NULL, NULL, 0); } static void test_release(const char *pool, const char *snapshot) { nvlist_t *required = fnvlist_alloc(); nvlist_t *release = fnvlist_alloc(); fnvlist_add_boolean(release, "libzfs_check_hold"); fnvlist_add_nvlist(required, snapshot, release); IOC_INPUT_TEST_WILD(ZFS_IOC_RELEASE, pool, required, NULL, 0); nvlist_free(release); nvlist_free(required); } static void test_send_new(const char *snapshot, int fd) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); fnvlist_add_int32(required, "fd", fd); fnvlist_add_boolean(optional, "largeblockok"); fnvlist_add_boolean(optional, "embedok"); fnvlist_add_boolean(optional, "compressok"); fnvlist_add_boolean(optional, "rawok"); /* * TODO - Resumable send is harder to set up. So we currently * ignore testing for that variant. */ #if 0 fnvlist_add_string(optional, "fromsnap", from); fnvlist_add_uint64(optional, "resume_object", resumeobj); fnvlist_add_uint64(optional, "resume_offset", offset); fnvlist_add_boolean(optional, "savedok"); #endif IOC_INPUT_TEST(ZFS_IOC_SEND_NEW, snapshot, required, optional, 0); nvlist_free(optional); nvlist_free(required); } static void test_recv_new(const char *dataset, int fd) { dmu_replay_record_t drr = { 0 }; nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *props = fnvlist_alloc(); char snapshot[MAXNAMELEN + 32]; ssize_t count; int cleanup_fd = open(ZFS_DEV, O_RDWR); - + if (cleanup_fd == -1) { + (void) fprintf(stderr, "open(%s) failed: %s\n", ZFS_DEV, + strerror(errno)); + exit(EXIT_FAILURE); + } (void) snprintf(snapshot, sizeof (snapshot), "%s@replicant", dataset); count = pread(fd, &drr, sizeof (drr), 0); if (count != sizeof (drr)) { (void) fprintf(stderr, "could not read stream: %s\n", strerror(errno)); } fnvlist_add_string(required, "snapname", snapshot); fnvlist_add_byte_array(required, "begin_record", (uchar_t *)&drr, sizeof (drr)); fnvlist_add_int32(required, "input_fd", fd); fnvlist_add_string(props, "org.openzfs:launch", "September 17th, 2013"); fnvlist_add_nvlist(optional, "localprops", props); fnvlist_add_boolean(optional, "force"); fnvlist_add_boolean(optional, "heal"); fnvlist_add_int32(optional, "cleanup_fd", cleanup_fd); /* * TODO - Resumable receive is harder to set up. So we currently * ignore testing for one. */ #if 0 fnvlist_add_nvlist(optional, "props", recvdprops); fnvlist_add_string(optional, "origin", origin); fnvlist_add_boolean(optional, "resumable"); fnvlist_add_uint64(optional, "action_handle", *action_handle); #endif IOC_INPUT_TEST(ZFS_IOC_RECV_NEW, dataset, required, optional, ZFS_ERR_STREAM_TRUNCATED); nvlist_free(props); nvlist_free(optional); nvlist_free(required); (void) close(cleanup_fd); } static void test_send_space(const char *snapshot1, const char *snapshot2) { nvlist_t *optional = fnvlist_alloc(); fnvlist_add_string(optional, "from", snapshot1); fnvlist_add_boolean(optional, "largeblockok"); fnvlist_add_boolean(optional, "embedok"); fnvlist_add_boolean(optional, "compressok"); fnvlist_add_boolean(optional, "rawok"); IOC_INPUT_TEST(ZFS_IOC_SEND_SPACE, snapshot2, NULL, optional, 0); nvlist_free(optional); } static void test_remap(const char *dataset) { IOC_INPUT_TEST(ZFS_IOC_REMAP, dataset, NULL, NULL, 0); } static void test_channel_program(const char *pool) { const char *program = "arg = ...\n" "argv = arg[\"argv\"]\n" "return argv[1]"; const char *const argv[1] = { "Hello World!" }; nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *args = fnvlist_alloc(); fnvlist_add_string(required, "program", program); fnvlist_add_string_array(args, "argv", argv, 1); fnvlist_add_nvlist(required, "arg", args); fnvlist_add_boolean_value(optional, "sync", B_TRUE); fnvlist_add_uint64(optional, "instrlimit", 1000 * 1000); fnvlist_add_uint64(optional, "memlimit", 8192 * 1024); IOC_INPUT_TEST(ZFS_IOC_CHANNEL_PROGRAM, pool, required, optional, 0); nvlist_free(args); nvlist_free(optional); nvlist_free(required); } #define WRAPPING_KEY_LEN 32 static void test_load_key(const char *dataset) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *hidden = fnvlist_alloc(); uint8_t keydata[WRAPPING_KEY_LEN] = {0}; fnvlist_add_uint8_array(hidden, "wkeydata", keydata, sizeof (keydata)); fnvlist_add_nvlist(required, "hidden_args", hidden); fnvlist_add_boolean(optional, "noop"); IOC_INPUT_TEST(ZFS_IOC_LOAD_KEY, dataset, required, optional, EINVAL); nvlist_free(hidden); nvlist_free(optional); nvlist_free(required); } static void test_change_key(const char *dataset) { IOC_INPUT_TEST(ZFS_IOC_CHANGE_KEY, dataset, NULL, NULL, EINVAL); } static void test_unload_key(const char *dataset) { IOC_INPUT_TEST(ZFS_IOC_UNLOAD_KEY, dataset, NULL, NULL, EACCES); } static void test_vdev_initialize(const char *pool) { nvlist_t *required = fnvlist_alloc(); nvlist_t *vdev_guids = fnvlist_alloc(); fnvlist_add_uint64(vdev_guids, "path", 0xdeadbeefdeadbeef); fnvlist_add_uint64(required, ZPOOL_INITIALIZE_COMMAND, POOL_INITIALIZE_START); fnvlist_add_nvlist(required, ZPOOL_INITIALIZE_VDEVS, vdev_guids); IOC_INPUT_TEST(ZFS_IOC_POOL_INITIALIZE, pool, required, NULL, EINVAL); nvlist_free(vdev_guids); nvlist_free(required); } static void test_vdev_trim(const char *pool) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); nvlist_t *vdev_guids = fnvlist_alloc(); fnvlist_add_uint64(vdev_guids, "path", 0xdeadbeefdeadbeef); fnvlist_add_uint64(required, ZPOOL_TRIM_COMMAND, POOL_TRIM_START); fnvlist_add_nvlist(required, ZPOOL_TRIM_VDEVS, vdev_guids); fnvlist_add_uint64(optional, ZPOOL_TRIM_RATE, 1ULL << 30); fnvlist_add_boolean_value(optional, ZPOOL_TRIM_SECURE, B_TRUE); IOC_INPUT_TEST(ZFS_IOC_POOL_TRIM, pool, required, optional, EINVAL); nvlist_free(vdev_guids); nvlist_free(optional); nvlist_free(required); } static int zfs_destroy(const char *dataset) { zfs_cmd_t zc = {"\0"}; int err; (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); zc.zc_name[sizeof (zc.zc_name) - 1] = '\0'; err = lzc_ioctl_fd(zfs_fd, ZFS_IOC_DESTROY, &zc); return (err == 0 ? 0 : errno); } static void test_redact(const char *snapshot1, const char *snapshot2) { nvlist_t *required = fnvlist_alloc(); nvlist_t *snapnv = fnvlist_alloc(); char bookmark[MAXNAMELEN + 32]; fnvlist_add_string(required, "bookname", "testbookmark"); fnvlist_add_boolean(snapnv, snapshot2); fnvlist_add_nvlist(required, "snapnv", snapnv); IOC_INPUT_TEST(ZFS_IOC_REDACT, snapshot1, required, NULL, 0); nvlist_free(snapnv); nvlist_free(required); strlcpy(bookmark, snapshot1, sizeof (bookmark)); *strchr(bookmark, '@') = '\0'; strlcat(bookmark, "#testbookmark", sizeof (bookmark) - strlen(bookmark)); zfs_destroy(bookmark); } static void test_get_bookmark_props(const char *bookmark) { IOC_INPUT_TEST(ZFS_IOC_GET_BOOKMARK_PROPS, bookmark, NULL, NULL, 0); } static void test_wait(const char *pool) { nvlist_t *required = fnvlist_alloc(); nvlist_t *optional = fnvlist_alloc(); fnvlist_add_int32(required, "wait_activity", 2); fnvlist_add_uint64(optional, "wait_tag", 0xdeadbeefdeadbeef); IOC_INPUT_TEST(ZFS_IOC_WAIT, pool, required, optional, EINVAL); nvlist_free(required); nvlist_free(optional); } static void test_wait_fs(const char *dataset) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_int32(required, "wait_activity", 2); IOC_INPUT_TEST(ZFS_IOC_WAIT_FS, dataset, required, NULL, EINVAL); nvlist_free(required); } static void test_get_bootenv(const char *pool) { IOC_INPUT_TEST(ZFS_IOC_GET_BOOTENV, pool, NULL, NULL, 0); } static void test_set_bootenv(const char *pool) { nvlist_t *required = fnvlist_alloc(); fnvlist_add_uint64(required, "version", VB_RAW); fnvlist_add_string(required, GRUB_ENVMAP, "test"); IOC_INPUT_TEST_WILD(ZFS_IOC_SET_BOOTENV, pool, required, NULL, 0); nvlist_free(required); } static void zfs_ioc_input_tests(const char *pool) { char filepath[] = "/tmp/ioc_test_file_XXXXXX"; char dataset[ZFS_MAX_DATASET_NAME_LEN]; char snapbase[ZFS_MAX_DATASET_NAME_LEN + 32]; char snapshot[ZFS_MAX_DATASET_NAME_LEN + 32]; char bookmark[ZFS_MAX_DATASET_NAME_LEN + 32]; char backup[ZFS_MAX_DATASET_NAME_LEN]; char clone[ZFS_MAX_DATASET_NAME_LEN]; char clonesnap[ZFS_MAX_DATASET_NAME_LEN + 32]; int tmpfd, err; /* * Setup names and create a working dataset */ (void) snprintf(dataset, sizeof (dataset), "%s/test-fs", pool); (void) snprintf(snapbase, sizeof (snapbase), "%s@snapbase", dataset); (void) snprintf(snapshot, sizeof (snapshot), "%s@snapshot", dataset); (void) snprintf(bookmark, sizeof (bookmark), "%s#bookmark", dataset); (void) snprintf(clone, sizeof (clone), "%s/test-fs-clone", pool); (void) snprintf(clonesnap, sizeof (clonesnap), "%s@snap", clone); (void) snprintf(backup, sizeof (backup), "%s/backup", pool); err = lzc_create(dataset, LZC_DATSET_TYPE_ZFS, NULL, NULL, -1); if (err) { (void) fprintf(stderr, "could not create '%s': %s\n", dataset, strerror(errno)); exit(2); } tmpfd = mkstemp(filepath); if (tmpfd < 0) { (void) fprintf(stderr, "could not create '%s': %s\n", filepath, strerror(errno)); exit(2); } /* * run a test for each ioctl * Note that some test build on previous test operations */ test_pool_sync(pool); test_pool_reopen(pool); test_pool_checkpoint(pool); test_pool_discard_checkpoint(pool); test_log_history(pool); test_create(dataset); test_snapshot(pool, snapbase); test_snapshot(pool, snapshot); test_space_snaps(snapshot); test_send_space(snapbase, snapshot); test_send_new(snapshot, tmpfd); test_recv_new(backup, tmpfd); test_bookmark(pool, snapshot, bookmark); test_get_bookmarks(dataset); test_get_bookmark_props(bookmark); test_destroy_bookmarks(pool, bookmark); test_hold(pool, snapshot); test_get_holds(snapshot); test_release(pool, snapshot); test_clone(snapshot, clone); test_snapshot(pool, clonesnap); test_redact(snapshot, clonesnap); zfs_destroy(clonesnap); zfs_destroy(clone); test_rollback(dataset, snapshot); test_destroy_snaps(pool, snapshot); test_destroy_snaps(pool, snapbase); test_remap(dataset); test_channel_program(pool); test_load_key(dataset); test_change_key(dataset); test_unload_key(dataset); test_vdev_initialize(pool); test_vdev_trim(pool); test_wait(pool); test_wait_fs(dataset); test_set_bootenv(pool); test_get_bootenv(pool); /* * cleanup */ zfs_cmd_t zc = {"\0"}; nvlist_t *snaps = fnvlist_alloc(); fnvlist_add_boolean(snaps, snapshot); (void) lzc_destroy_snaps(snaps, B_FALSE, NULL); nvlist_free(snaps); (void) zfs_destroy(dataset); (void) zfs_destroy(backup); (void) close(tmpfd); (void) unlink(filepath); /* * All the unused slots should yield ZFS_ERR_IOC_CMD_UNAVAIL */ for (int i = 0; i < ARRAY_SIZE(ioc_skip); i++) { if (ioc_tested[ioc_skip[i] - ZFS_IOC_FIRST]) (void) fprintf(stderr, "cmd %d tested, not skipped!\n", (int)(ioc_skip[i] - ZFS_IOC_FIRST)); ioc_tested[ioc_skip[i] - ZFS_IOC_FIRST] = B_TRUE; } (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); zc.zc_name[sizeof (zc.zc_name) - 1] = '\0'; for (unsigned ioc = ZFS_IOC_FIRST; ioc < ZFS_IOC_LAST; ioc++) { unsigned cmd = ioc - ZFS_IOC_FIRST; if (ioc_tested[cmd]) continue; if (lzc_ioctl_fd(zfs_fd, ioc, &zc) != 0 && errno != ZFS_ERR_IOC_CMD_UNAVAIL) { (void) fprintf(stderr, "cmd %d is missing a test case " "(%d)\n", cmd, errno); } } } enum zfs_ioc_ref { #ifdef __FreeBSD__ ZFS_IOC_BASE = 0, #else ZFS_IOC_BASE = ('Z' << 8), #endif ZFS_IOC_PLATFORM_BASE = ZFS_IOC_BASE + 0x80, }; /* * Canonical reference check of /dev/zfs ioctl numbers. * These cannot change and new ioctl numbers must be appended. */ static boolean_t validate_ioc_values(void) { boolean_t result = B_TRUE; #define CHECK(expr) do { \ if (!(expr)) { \ result = B_FALSE; \ fprintf(stderr, "(%s) === FALSE\n", #expr); \ } \ } while (0) CHECK(ZFS_IOC_BASE + 0 == ZFS_IOC_POOL_CREATE); CHECK(ZFS_IOC_BASE + 1 == ZFS_IOC_POOL_DESTROY); CHECK(ZFS_IOC_BASE + 2 == ZFS_IOC_POOL_IMPORT); CHECK(ZFS_IOC_BASE + 3 == ZFS_IOC_POOL_EXPORT); CHECK(ZFS_IOC_BASE + 4 == ZFS_IOC_POOL_CONFIGS); CHECK(ZFS_IOC_BASE + 5 == ZFS_IOC_POOL_STATS); CHECK(ZFS_IOC_BASE + 6 == ZFS_IOC_POOL_TRYIMPORT); CHECK(ZFS_IOC_BASE + 7 == ZFS_IOC_POOL_SCAN); CHECK(ZFS_IOC_BASE + 8 == ZFS_IOC_POOL_FREEZE); CHECK(ZFS_IOC_BASE + 9 == ZFS_IOC_POOL_UPGRADE); CHECK(ZFS_IOC_BASE + 10 == ZFS_IOC_POOL_GET_HISTORY); CHECK(ZFS_IOC_BASE + 11 == ZFS_IOC_VDEV_ADD); CHECK(ZFS_IOC_BASE + 12 == ZFS_IOC_VDEV_REMOVE); CHECK(ZFS_IOC_BASE + 13 == ZFS_IOC_VDEV_SET_STATE); CHECK(ZFS_IOC_BASE + 14 == ZFS_IOC_VDEV_ATTACH); CHECK(ZFS_IOC_BASE + 15 == ZFS_IOC_VDEV_DETACH); CHECK(ZFS_IOC_BASE + 16 == ZFS_IOC_VDEV_SETPATH); CHECK(ZFS_IOC_BASE + 17 == ZFS_IOC_VDEV_SETFRU); CHECK(ZFS_IOC_BASE + 18 == ZFS_IOC_OBJSET_STATS); CHECK(ZFS_IOC_BASE + 19 == ZFS_IOC_OBJSET_ZPLPROPS); CHECK(ZFS_IOC_BASE + 20 == ZFS_IOC_DATASET_LIST_NEXT); CHECK(ZFS_IOC_BASE + 21 == ZFS_IOC_SNAPSHOT_LIST_NEXT); CHECK(ZFS_IOC_BASE + 22 == ZFS_IOC_SET_PROP); CHECK(ZFS_IOC_BASE + 23 == ZFS_IOC_CREATE); CHECK(ZFS_IOC_BASE + 24 == ZFS_IOC_DESTROY); CHECK(ZFS_IOC_BASE + 25 == ZFS_IOC_ROLLBACK); CHECK(ZFS_IOC_BASE + 26 == ZFS_IOC_RENAME); CHECK(ZFS_IOC_BASE + 27 == ZFS_IOC_RECV); CHECK(ZFS_IOC_BASE + 28 == ZFS_IOC_SEND); CHECK(ZFS_IOC_BASE + 29 == ZFS_IOC_INJECT_FAULT); CHECK(ZFS_IOC_BASE + 30 == ZFS_IOC_CLEAR_FAULT); CHECK(ZFS_IOC_BASE + 31 == ZFS_IOC_INJECT_LIST_NEXT); CHECK(ZFS_IOC_BASE + 32 == ZFS_IOC_ERROR_LOG); CHECK(ZFS_IOC_BASE + 33 == ZFS_IOC_CLEAR); CHECK(ZFS_IOC_BASE + 34 == ZFS_IOC_PROMOTE); CHECK(ZFS_IOC_BASE + 35 == ZFS_IOC_SNAPSHOT); CHECK(ZFS_IOC_BASE + 36 == ZFS_IOC_DSOBJ_TO_DSNAME); CHECK(ZFS_IOC_BASE + 37 == ZFS_IOC_OBJ_TO_PATH); CHECK(ZFS_IOC_BASE + 38 == ZFS_IOC_POOL_SET_PROPS); CHECK(ZFS_IOC_BASE + 39 == ZFS_IOC_POOL_GET_PROPS); CHECK(ZFS_IOC_BASE + 40 == ZFS_IOC_SET_FSACL); CHECK(ZFS_IOC_BASE + 41 == ZFS_IOC_GET_FSACL); CHECK(ZFS_IOC_BASE + 42 == ZFS_IOC_SHARE); CHECK(ZFS_IOC_BASE + 43 == ZFS_IOC_INHERIT_PROP); CHECK(ZFS_IOC_BASE + 44 == ZFS_IOC_SMB_ACL); CHECK(ZFS_IOC_BASE + 45 == ZFS_IOC_USERSPACE_ONE); CHECK(ZFS_IOC_BASE + 46 == ZFS_IOC_USERSPACE_MANY); CHECK(ZFS_IOC_BASE + 47 == ZFS_IOC_USERSPACE_UPGRADE); CHECK(ZFS_IOC_BASE + 48 == ZFS_IOC_HOLD); CHECK(ZFS_IOC_BASE + 49 == ZFS_IOC_RELEASE); CHECK(ZFS_IOC_BASE + 50 == ZFS_IOC_GET_HOLDS); CHECK(ZFS_IOC_BASE + 51 == ZFS_IOC_OBJSET_RECVD_PROPS); CHECK(ZFS_IOC_BASE + 52 == ZFS_IOC_VDEV_SPLIT); CHECK(ZFS_IOC_BASE + 53 == ZFS_IOC_NEXT_OBJ); CHECK(ZFS_IOC_BASE + 54 == ZFS_IOC_DIFF); CHECK(ZFS_IOC_BASE + 55 == ZFS_IOC_TMP_SNAPSHOT); CHECK(ZFS_IOC_BASE + 56 == ZFS_IOC_OBJ_TO_STATS); CHECK(ZFS_IOC_BASE + 57 == ZFS_IOC_SPACE_WRITTEN); CHECK(ZFS_IOC_BASE + 58 == ZFS_IOC_SPACE_SNAPS); CHECK(ZFS_IOC_BASE + 59 == ZFS_IOC_DESTROY_SNAPS); CHECK(ZFS_IOC_BASE + 60 == ZFS_IOC_POOL_REGUID); CHECK(ZFS_IOC_BASE + 61 == ZFS_IOC_POOL_REOPEN); CHECK(ZFS_IOC_BASE + 62 == ZFS_IOC_SEND_PROGRESS); CHECK(ZFS_IOC_BASE + 63 == ZFS_IOC_LOG_HISTORY); CHECK(ZFS_IOC_BASE + 64 == ZFS_IOC_SEND_NEW); CHECK(ZFS_IOC_BASE + 65 == ZFS_IOC_SEND_SPACE); CHECK(ZFS_IOC_BASE + 66 == ZFS_IOC_CLONE); CHECK(ZFS_IOC_BASE + 67 == ZFS_IOC_BOOKMARK); CHECK(ZFS_IOC_BASE + 68 == ZFS_IOC_GET_BOOKMARKS); CHECK(ZFS_IOC_BASE + 69 == ZFS_IOC_DESTROY_BOOKMARKS); CHECK(ZFS_IOC_BASE + 70 == ZFS_IOC_RECV_NEW); CHECK(ZFS_IOC_BASE + 71 == ZFS_IOC_POOL_SYNC); CHECK(ZFS_IOC_BASE + 72 == ZFS_IOC_CHANNEL_PROGRAM); CHECK(ZFS_IOC_BASE + 73 == ZFS_IOC_LOAD_KEY); CHECK(ZFS_IOC_BASE + 74 == ZFS_IOC_UNLOAD_KEY); CHECK(ZFS_IOC_BASE + 75 == ZFS_IOC_CHANGE_KEY); CHECK(ZFS_IOC_BASE + 76 == ZFS_IOC_REMAP); CHECK(ZFS_IOC_BASE + 77 == ZFS_IOC_POOL_CHECKPOINT); CHECK(ZFS_IOC_BASE + 78 == ZFS_IOC_POOL_DISCARD_CHECKPOINT); CHECK(ZFS_IOC_BASE + 79 == ZFS_IOC_POOL_INITIALIZE); CHECK(ZFS_IOC_BASE + 80 == ZFS_IOC_POOL_TRIM); CHECK(ZFS_IOC_BASE + 81 == ZFS_IOC_REDACT); CHECK(ZFS_IOC_BASE + 82 == ZFS_IOC_GET_BOOKMARK_PROPS); CHECK(ZFS_IOC_BASE + 83 == ZFS_IOC_WAIT); CHECK(ZFS_IOC_BASE + 84 == ZFS_IOC_WAIT_FS); CHECK(ZFS_IOC_PLATFORM_BASE + 1 == ZFS_IOC_EVENTS_NEXT); CHECK(ZFS_IOC_PLATFORM_BASE + 2 == ZFS_IOC_EVENTS_CLEAR); CHECK(ZFS_IOC_PLATFORM_BASE + 3 == ZFS_IOC_EVENTS_SEEK); CHECK(ZFS_IOC_PLATFORM_BASE + 4 == ZFS_IOC_NEXTBOOT); CHECK(ZFS_IOC_PLATFORM_BASE + 5 == ZFS_IOC_JAIL); CHECK(ZFS_IOC_PLATFORM_BASE + 6 == ZFS_IOC_UNJAIL); CHECK(ZFS_IOC_PLATFORM_BASE + 7 == ZFS_IOC_SET_BOOTENV); CHECK(ZFS_IOC_PLATFORM_BASE + 8 == ZFS_IOC_GET_BOOTENV); #undef CHECK return (result); } int main(int argc, const char *argv[]) { if (argc != 2) { (void) fprintf(stderr, "usage: %s \n", argv[0]); exit(2); } if (!validate_ioc_values()) { (void) fprintf(stderr, "WARNING: zfs_ioc_t has binary " "incompatible command values\n"); exit(3); } (void) libzfs_core_init(); zfs_fd = open(ZFS_DEV, O_RDWR); if (zfs_fd < 0) { (void) fprintf(stderr, "open: %s\n", strerror(errno)); libzfs_core_fini(); exit(2); } zfs_ioc_input_tests(argv[1]); (void) close(zfs_fd); libzfs_core_fini(); return (unexpected_failures); } diff --git a/tests/zfs-tests/cmd/mmapwrite.c b/tests/zfs-tests/cmd/mmapwrite.c index 8534db339666..ca55d730fd34 100644 --- a/tests/zfs-tests/cmd/mmapwrite.c +++ b/tests/zfs-tests/cmd/mmapwrite.c @@ -1,161 +1,161 @@ /* * 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 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include #include #include #include /* * -------------------------------------------------------------------- * Bug Issue Id: #7512 * The bug time sequence: * 1. context #1, zfs_write assign a txg "n". * 2. In the same process, context #2, mmap page fault (which means the mm_sem * is hold) occurred, zfs_dirty_inode open a txg failed, and wait previous * txg "n" completed. * 3. context #1 call zfs_uiomove to write, however page fault is occurred in * zfs_uiomove, which means it needs mm_sem, but mm_sem is hold by * context #2, so it stuck and can't complete, then txg "n" will not * complete. * * So context #1 and context #2 trap into the "dead lock". * -------------------------------------------------------------------- */ #define NORMAL_WRITE_TH_NUM 2 static void * normal_writer(void *filename) { char *file_path = filename; int fd = -1; ssize_t write_num = 0; int page_size = getpagesize(); fd = open(file_path, O_RDWR | O_CREAT, 0777); if (fd == -1) { err(1, "failed to open %s", file_path); } - char buf; + char buf = 'z'; while (1) { write_num = write(fd, &buf, 1); if (write_num == 0) { err(1, "write failed!"); break; } if (lseek(fd, page_size, SEEK_CUR) == -1) { err(1, "lseek failed on %s: %s", file_path, strerror(errno)); break; } } } static void * map_writer(void *filename) { int fd = -1; int ret = 0; char *buf = NULL; int page_size = getpagesize(); int op_errno = 0; char *file_path = filename; while (1) { ret = access(file_path, F_OK); if (ret) { op_errno = errno; if (op_errno == ENOENT) { fd = open(file_path, O_RDWR | O_CREAT, 0777); if (fd == -1) { err(1, "open file failed"); } ret = ftruncate(fd, page_size); if (ret == -1) { err(1, "truncate file failed"); } } else { err(1, "access file failed!"); } } else { fd = open(file_path, O_RDWR, 0777); if (fd == -1) { err(1, "open file failed"); } } if ((buf = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { err(1, "map file failed"); } if (fd != -1) close(fd); char s[10] = {0, }; memcpy(buf, s, 10); ret = munmap(buf, page_size); if (ret != 0) { err(1, "unmap file failed"); } } } int main(int argc, char **argv) { pthread_t map_write_tid; pthread_t normal_write_tid[NORMAL_WRITE_TH_NUM]; int i = 0; if (argc != 3) { (void) printf("usage: %s " "\n", argv[0]); exit(1); } for (i = 0; i < NORMAL_WRITE_TH_NUM; i++) { if (pthread_create(&normal_write_tid[i], NULL, normal_writer, argv[1])) { err(1, "pthread_create normal_writer failed."); } } if (pthread_create(&map_write_tid, NULL, map_writer, argv[2])) { err(1, "pthread_create map_writer failed."); } pthread_join(map_write_tid, NULL); return (0); } diff --git a/tests/zfs-tests/cmd/readmmap.c b/tests/zfs-tests/cmd/readmmap.c index c4812b4a259d..704ffd55c8a5 100644 --- a/tests/zfs-tests/cmd/readmmap.c +++ b/tests/zfs-tests/cmd/readmmap.c @@ -1,138 +1,138 @@ /* * 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 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ /* * -------------------------------------------------------------- * BugId 5047993 : Getting bad read data. * * Usage: readmmap * * where: * filename is an absolute path to the file name. * * Return values: * 1 : error * 0 : no errors * -------------------------------------------------------------- */ #include #include #include #include #include #include #include int main(int argc, char **argv) { const char *filename = "badfile"; size_t size = 4395; size_t idx = 0; char *buf = NULL; char *map = NULL; int fd = -1, bytes, retval = 0; - unsigned seed; + uint_t seed; if (argc < 2 || optind == argc) { (void) fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } if ((buf = calloc(1, size)) == NULL) { perror("calloc"); exit(1); } filename = argv[optind]; (void) remove(filename); fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666); if (fd == -1) { perror("open to create"); retval = 1; goto end; } bytes = write(fd, buf, size); if (bytes != size) { (void) printf("short write: %d != %zd\n", bytes, size); retval = 1; goto end; } map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { perror("mmap"); retval = 1; goto end; } - seed = time(NULL); + seed = (uint_t)time(NULL); srandom(seed); idx = random() % size; map[idx] = 1; if (msync(map, size, MS_SYNC) != 0) { perror("msync"); retval = 1; goto end; } if (munmap(map, size) != 0) { perror("munmap"); retval = 1; goto end; } bytes = pread(fd, buf, size, 0); if (bytes != size) { (void) printf("short read: %d != %zd\n", bytes, size); retval = 1; goto end; } if (buf[idx] != 1) { (void) printf( "bad data from read! got buf[%zd]=%d, expected 1\n", idx, buf[idx]); retval = 1; goto end; } (void) printf("good data from read: buf[%zd]=1\n", idx); end: if (fd != -1) { (void) close(fd); } if (buf != NULL) { free(buf); } return (retval); } diff --git a/tests/zfs-tests/cmd/zed_fd_spill-zedlet.c b/tests/zfs-tests/cmd/zed_fd_spill-zedlet.c index c072f906d23e..b248579abb7c 100644 --- a/tests/zfs-tests/cmd/zed_fd_spill-zedlet.c +++ b/tests/zfs-tests/cmd/zed_fd_spill-zedlet.c @@ -1,36 +1,49 @@ /* * Permission to use, copy, modify, and/or distribute this software for * any purpose with or without fee is hereby granted. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include +#include #include #include #include #include #include int main(void) { if (fork()) { int err; wait(&err); return (err); } char buf[64]; sprintf(buf, "/tmp/zts-zed_fd_spill-logdir/%d", getppid()); - dup2(creat(buf, 0644), STDOUT_FILENO); + int fd = creat(buf, 0644); + if (fd == -1) { + (void) fprintf(stderr, "creat(%s) failed: %s\n", buf, + strerror(errno)); + exit(EXIT_FAILURE); + } + + if (dup2(fd, STDOUT_FILENO) == -1) { + close(fd); + (void) fprintf(stderr, "dup2(%s, STDOUT_FILENO) failed: %s\n", + buf, strerror(errno)); + exit(EXIT_FAILURE); + } snprintf(buf, sizeof (buf), "/proc/%d/fd", getppid()); execlp("ls", "ls", buf, NULL); _exit(127); }