]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/ztest/ztest.c
Fix minor acl issue
[mirror_zfs.git] / cmd / ztest / ztest.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
34dc7c2f
BB
23 */
24
34dc7c2f
BB
25/*
26 * The objective of this program is to provide a DMU/ZAP/SPA stress test
27 * that runs entirely in userland, is easy to use, and easy to extend.
28 *
29 * The overall design of the ztest program is as follows:
30 *
31 * (1) For each major functional area (e.g. adding vdevs to a pool,
32 * creating and destroying datasets, reading and writing objects, etc)
33 * we have a simple routine to test that functionality. These
34 * individual routines do not have to do anything "stressful".
35 *
36 * (2) We turn these simple functionality tests into a stress test by
37 * running them all in parallel, with as many threads as desired,
38 * and spread across as many datasets, objects, and vdevs as desired.
39 *
40 * (3) While all this is happening, we inject faults into the pool to
41 * verify that self-healing data really works.
42 *
43 * (4) Every time we open a dataset, we change its checksum and compression
44 * functions. Thus even individual objects vary from block to block
45 * in which checksum they use and whether they're compressed.
46 *
47 * (5) To verify that we never lose on-disk consistency after a crash,
48 * we run the entire test in a child of the main process.
49 * At random times, the child self-immolates with a SIGKILL.
50 * This is the software equivalent of pulling the power cord.
51 * The parent then runs the test again, using the existing
52 * storage pool, as many times as desired.
53 *
54 * (6) To verify that we don't have future leaks or temporal incursions,
55 * many of the functional tests record the transaction group number
56 * as part of their data. When reading old data, they verify that
57 * the transaction group number is less than the current, open txg.
58 * If you add a new test, please do this if applicable.
59 *
60 * When run with no arguments, ztest runs for about five minutes and
61 * produces no output if successful. To get a little bit of information,
62 * specify -V. To get more information, specify -VV, and so on.
63 *
64 * To turn this into an overnight stress test, use -T to specify run time.
65 *
66 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
67 * to increase the pool capacity, fanout, and overall stress level.
68 *
69 * The -N(okill) option will suppress kills, so each child runs to completion.
70 * This can be useful when you're trying to distinguish temporal incursions
71 * from plain old race conditions.
72 */
73
74#include <sys/zfs_context.h>
75#include <sys/spa.h>
76#include <sys/dmu.h>
77#include <sys/txg.h>
9babb374 78#include <sys/dbuf.h>
34dc7c2f 79#include <sys/zap.h>
34dc7c2f
BB
80#include <sys/dmu_objset.h>
81#include <sys/poll.h>
82#include <sys/stat.h>
83#include <sys/time.h>
84#include <sys/wait.h>
85#include <sys/mman.h>
86#include <sys/resource.h>
87#include <sys/zio.h>
34dc7c2f 88#include <sys/zil.h>
428870ff 89#include <sys/zil_impl.h>
34dc7c2f 90#include <sys/vdev_impl.h>
b128c09f 91#include <sys/vdev_file.h>
34dc7c2f 92#include <sys/spa_impl.h>
428870ff 93#include <sys/metaslab_impl.h>
34dc7c2f 94#include <sys/dsl_prop.h>
9babb374 95#include <sys/dsl_dataset.h>
428870ff
BB
96#include <sys/dsl_scan.h>
97#include <sys/zio_checksum.h>
34dc7c2f
BB
98#include <sys/refcount.h>
99#include <stdio.h>
100#include <stdio_ext.h>
101#include <stdlib.h>
102#include <unistd.h>
103#include <signal.h>
104#include <umem.h>
105#include <dlfcn.h>
106#include <ctype.h>
107#include <math.h>
108#include <sys/fs/zfs.h>
428870ff 109#include <libnvpair.h>
34dc7c2f
BB
110
111static char cmdname[] = "ztest";
112static char *zopt_pool = cmdname;
113
114static uint64_t zopt_vdevs = 5;
115static uint64_t zopt_vdevtime;
116static int zopt_ashift = SPA_MINBLOCKSHIFT;
117static int zopt_mirrors = 2;
118static int zopt_raidz = 4;
119static int zopt_raidz_parity = 1;
120static size_t zopt_vdev_size = SPA_MINDEVSIZE;
121static int zopt_datasets = 7;
122static int zopt_threads = 23;
123static uint64_t zopt_passtime = 60; /* 60 seconds */
124static uint64_t zopt_killrate = 70; /* 70% kill rate */
125static int zopt_verbose = 0;
126static int zopt_init = 1;
127static char *zopt_dir = "/tmp";
128static uint64_t zopt_time = 300; /* 5 minutes */
428870ff
BB
129static uint64_t zopt_maxloops = 50; /* max loops during spa_freeze() */
130
131#define BT_MAGIC 0x123456789abcdefULL
132#define MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
133
134enum ztest_io_type {
135 ZTEST_IO_WRITE_TAG,
136 ZTEST_IO_WRITE_PATTERN,
137 ZTEST_IO_WRITE_ZEROES,
138 ZTEST_IO_TRUNCATE,
139 ZTEST_IO_SETATTR,
140 ZTEST_IO_TYPES
141};
34dc7c2f
BB
142
143typedef struct ztest_block_tag {
428870ff 144 uint64_t bt_magic;
34dc7c2f
BB
145 uint64_t bt_objset;
146 uint64_t bt_object;
147 uint64_t bt_offset;
428870ff 148 uint64_t bt_gen;
34dc7c2f 149 uint64_t bt_txg;
428870ff 150 uint64_t bt_crtxg;
34dc7c2f
BB
151} ztest_block_tag_t;
152
428870ff
BB
153typedef struct bufwad {
154 uint64_t bw_index;
155 uint64_t bw_txg;
156 uint64_t bw_data;
157} bufwad_t;
158
159/*
160 * XXX -- fix zfs range locks to be generic so we can use them here.
161 */
162typedef enum {
163 RL_READER,
164 RL_WRITER,
165 RL_APPEND
166} rl_type_t;
167
168typedef struct rll {
169 void *rll_writer;
170 int rll_readers;
171 mutex_t rll_lock;
172 cond_t rll_cv;
173} rll_t;
174
175typedef struct rl {
176 uint64_t rl_object;
177 uint64_t rl_offset;
178 uint64_t rl_size;
179 rll_t *rl_lock;
180} rl_t;
181
182#define ZTEST_RANGE_LOCKS 64
183#define ZTEST_OBJECT_LOCKS 64
184
185/*
186 * Object descriptor. Used as a template for object lookup/create/remove.
187 */
188typedef struct ztest_od {
189 uint64_t od_dir;
190 uint64_t od_object;
191 dmu_object_type_t od_type;
192 dmu_object_type_t od_crtype;
193 uint64_t od_blocksize;
194 uint64_t od_crblocksize;
195 uint64_t od_gen;
196 uint64_t od_crgen;
197 char od_name[MAXNAMELEN];
198} ztest_od_t;
34dc7c2f 199
428870ff
BB
200/*
201 * Per-dataset state.
202 */
203typedef struct ztest_ds {
204 objset_t *zd_os;
205 zilog_t *zd_zilog;
206 uint64_t zd_seq;
207 ztest_od_t *zd_od; /* debugging aid */
208 char zd_name[MAXNAMELEN];
209 mutex_t zd_dirobj_lock;
210 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
211 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
212} ztest_ds_t;
213
214/*
215 * Per-iteration state.
216 */
217typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
218
219typedef struct ztest_info {
220 ztest_func_t *zi_func; /* test function */
221 uint64_t zi_iters; /* iterations per execution */
222 uint64_t *zi_interval; /* execute every <interval> seconds */
223 uint64_t zi_call_count; /* per-pass count */
224 uint64_t zi_call_time; /* per-pass time */
225 uint64_t zi_call_next; /* next time to call this function */
226} ztest_info_t;
34dc7c2f
BB
227
228/*
229 * Note: these aren't static because we want dladdr() to work.
230 */
231ztest_func_t ztest_dmu_read_write;
232ztest_func_t ztest_dmu_write_parallel;
233ztest_func_t ztest_dmu_object_alloc_free;
428870ff 234ztest_func_t ztest_dmu_commit_callbacks;
34dc7c2f
BB
235ztest_func_t ztest_zap;
236ztest_func_t ztest_zap_parallel;
428870ff
BB
237ztest_func_t ztest_zil_commit;
238ztest_func_t ztest_dmu_read_write_zcopy;
34dc7c2f 239ztest_func_t ztest_dmu_objset_create_destroy;
428870ff
BB
240ztest_func_t ztest_dmu_prealloc;
241ztest_func_t ztest_fzap;
34dc7c2f 242ztest_func_t ztest_dmu_snapshot_create_destroy;
428870ff
BB
243ztest_func_t ztest_dsl_prop_get_set;
244ztest_func_t ztest_spa_prop_get_set;
34dc7c2f
BB
245ztest_func_t ztest_spa_create_destroy;
246ztest_func_t ztest_fault_inject;
428870ff
BB
247ztest_func_t ztest_ddt_repair;
248ztest_func_t ztest_dmu_snapshot_hold;
b128c09f 249ztest_func_t ztest_spa_rename;
428870ff
BB
250ztest_func_t ztest_scrub;
251ztest_func_t ztest_dsl_dataset_promote_busy;
34dc7c2f
BB
252ztest_func_t ztest_vdev_attach_detach;
253ztest_func_t ztest_vdev_LUN_growth;
254ztest_func_t ztest_vdev_add_remove;
b128c09f 255ztest_func_t ztest_vdev_aux_add_remove;
428870ff 256ztest_func_t ztest_split_pool;
34dc7c2f 257
428870ff
BB
258uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
259uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
260uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
261uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
262uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
34dc7c2f
BB
263
264ztest_info_t ztest_info[] = {
265 { ztest_dmu_read_write, 1, &zopt_always },
428870ff 266 { ztest_dmu_write_parallel, 10, &zopt_always },
34dc7c2f 267 { ztest_dmu_object_alloc_free, 1, &zopt_always },
428870ff 268 { ztest_dmu_commit_callbacks, 1, &zopt_always },
34dc7c2f
BB
269 { ztest_zap, 30, &zopt_always },
270 { ztest_zap_parallel, 100, &zopt_always },
428870ff
BB
271 { ztest_split_pool, 1, &zopt_always },
272 { ztest_zil_commit, 1, &zopt_incessant },
273 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
274 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
275 { ztest_dsl_prop_get_set, 1, &zopt_often },
276 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
277#if 0
278 { ztest_dmu_prealloc, 1, &zopt_sometimes },
279#endif
280 { ztest_fzap, 1, &zopt_sometimes },
281 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
282 { ztest_spa_create_destroy, 1, &zopt_sometimes },
34dc7c2f 283 { ztest_fault_inject, 1, &zopt_sometimes },
428870ff
BB
284 { ztest_ddt_repair, 1, &zopt_sometimes },
285 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
34dc7c2f 286 { ztest_spa_rename, 1, &zopt_rarely },
428870ff 287 { ztest_scrub, 1, &zopt_rarely },
9babb374 288 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
428870ff
BB
289 { ztest_vdev_attach_detach, 1, &zopt_rarely },
290 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
291 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
b128c09f 292 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
34dc7c2f
BB
293};
294
295#define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
296
428870ff
BB
297/*
298 * The following struct is used to hold a list of uncalled commit callbacks.
299 * The callbacks are ordered by txg number.
300 */
301typedef struct ztest_cb_list {
302 mutex_t zcl_callbacks_lock;
303 list_t zcl_callbacks;
304} ztest_cb_list_t;
34dc7c2f
BB
305
306/*
307 * Stuff we need to share writably between parent and child.
308 */
309typedef struct ztest_shared {
428870ff
BB
310 char *zs_pool;
311 spa_t *zs_spa;
312 hrtime_t zs_proc_start;
313 hrtime_t zs_proc_stop;
314 hrtime_t zs_thread_start;
315 hrtime_t zs_thread_stop;
316 hrtime_t zs_thread_kill;
34dc7c2f 317 uint64_t zs_enospc_count;
428870ff
BB
318 uint64_t zs_vdev_next_leaf;
319 uint64_t zs_vdev_aux;
34dc7c2f
BB
320 uint64_t zs_alloc;
321 uint64_t zs_space;
428870ff
BB
322 mutex_t zs_vdev_lock;
323 rwlock_t zs_name_lock;
34dc7c2f 324 ztest_info_t zs_info[ZTEST_FUNCS];
428870ff
BB
325 uint64_t zs_splits;
326 uint64_t zs_mirrors;
327 ztest_ds_t zs_zd[];
34dc7c2f
BB
328} ztest_shared_t;
329
428870ff
BB
330#define ID_PARALLEL -1ULL
331
34dc7c2f 332static char ztest_dev_template[] = "%s/%s.%llua";
b128c09f 333static char ztest_aux_template[] = "%s/%s.%s.%llu";
428870ff
BB
334ztest_shared_t *ztest_shared;
335uint64_t *ztest_seq;
34dc7c2f
BB
336
337static int ztest_random_fd;
338static int ztest_dump_core = 1;
339
b128c09f 340static boolean_t ztest_exiting;
34dc7c2f 341
428870ff
BB
342/* Global commit callback list */
343static ztest_cb_list_t zcl;
344
34dc7c2f 345extern uint64_t metaslab_gang_bang;
9babb374 346extern uint64_t metaslab_df_alloc_threshold;
428870ff 347static uint64_t metaslab_sz;
34dc7c2f 348
428870ff
BB
349enum ztest_object {
350 ZTEST_META_DNODE = 0,
351 ZTEST_DIROBJ,
352 ZTEST_OBJECTS
353};
34dc7c2f
BB
354
355static void usage(boolean_t) __NORETURN;
356
357/*
358 * These libumem hooks provide a reasonable set of defaults for the allocator's
359 * debugging facilities.
360 */
361const char *
0bc8fd78 362_umem_debug_init(void)
34dc7c2f
BB
363{
364 return ("default,verbose"); /* $UMEM_DEBUG setting */
365}
366
367const char *
368_umem_logging_init(void)
369{
370 return ("fail,contents"); /* $UMEM_LOGGING setting */
371}
372
373#define FATAL_MSG_SZ 1024
374
375char *fatal_msg;
376
377static void
378fatal(int do_perror, char *message, ...)
379{
380 va_list args;
381 int save_errno = errno;
382 char buf[FATAL_MSG_SZ];
383
384 (void) fflush(stdout);
385
386 va_start(args, message);
387 (void) sprintf(buf, "ztest: ");
388 /* LINTED */
389 (void) vsprintf(buf + strlen(buf), message, args);
390 va_end(args);
391 if (do_perror) {
392 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
393 ": %s", strerror(save_errno));
394 }
395 (void) fprintf(stderr, "%s\n", buf);
396 fatal_msg = buf; /* to ease debugging */
397 if (ztest_dump_core)
398 abort();
399 exit(3);
400}
401
402static int
403str2shift(const char *buf)
404{
405 const char *ends = "BKMGTPEZ";
406 int i;
407
408 if (buf[0] == '\0')
409 return (0);
410 for (i = 0; i < strlen(ends); i++) {
411 if (toupper(buf[0]) == ends[i])
412 break;
413 }
414 if (i == strlen(ends)) {
415 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
416 buf);
417 usage(B_FALSE);
418 }
419 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
420 return (10*i);
421 }
422 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
423 usage(B_FALSE);
424 /* NOTREACHED */
425}
426
427static uint64_t
428nicenumtoull(const char *buf)
429{
430 char *end;
431 uint64_t val;
432
433 val = strtoull(buf, &end, 0);
434 if (end == buf) {
435 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
436 usage(B_FALSE);
437 } else if (end[0] == '.') {
438 double fval = strtod(buf, &end);
439 fval *= pow(2, str2shift(end));
440 if (fval > UINT64_MAX) {
441 (void) fprintf(stderr, "ztest: value too large: %s\n",
442 buf);
443 usage(B_FALSE);
444 }
445 val = (uint64_t)fval;
446 } else {
447 int shift = str2shift(end);
448 if (shift >= 64 || (val << shift) >> shift != val) {
449 (void) fprintf(stderr, "ztest: value too large: %s\n",
450 buf);
451 usage(B_FALSE);
452 }
453 val <<= shift;
454 }
455 return (val);
456}
457
458static void
459usage(boolean_t requested)
460{
461 char nice_vdev_size[10];
462 char nice_gang_bang[10];
463 FILE *fp = requested ? stdout : stderr;
464
465 nicenum(zopt_vdev_size, nice_vdev_size);
466 nicenum(metaslab_gang_bang, nice_gang_bang);
467
468 (void) fprintf(fp, "Usage: %s\n"
469 "\t[-v vdevs (default: %llu)]\n"
470 "\t[-s size_of_each_vdev (default: %s)]\n"
428870ff 471 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
34dc7c2f
BB
472 "\t[-m mirror_copies (default: %d)]\n"
473 "\t[-r raidz_disks (default: %d)]\n"
474 "\t[-R raidz_parity (default: %d)]\n"
475 "\t[-d datasets (default: %d)]\n"
476 "\t[-t threads (default: %d)]\n"
477 "\t[-g gang_block_threshold (default: %s)]\n"
428870ff
BB
478 "\t[-i init_count (default: %d)] initialize pool i times\n"
479 "\t[-k kill_percentage (default: %llu%%)]\n"
34dc7c2f 480 "\t[-p pool_name (default: %s)]\n"
428870ff
BB
481 "\t[-f dir (default: %s)] file directory for vdev files\n"
482 "\t[-V] verbose (use multiple times for ever more blather)\n"
483 "\t[-E] use existing pool instead of creating new one\n"
484 "\t[-T time (default: %llu sec)] total run time\n"
485 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
486 "\t[-P passtime (default: %llu sec)] time per pass\n"
34dc7c2f
BB
487 "\t[-h] (print help)\n"
488 "",
489 cmdname,
490 (u_longlong_t)zopt_vdevs, /* -v */
491 nice_vdev_size, /* -s */
492 zopt_ashift, /* -a */
493 zopt_mirrors, /* -m */
494 zopt_raidz, /* -r */
495 zopt_raidz_parity, /* -R */
496 zopt_datasets, /* -d */
497 zopt_threads, /* -t */
498 nice_gang_bang, /* -g */
499 zopt_init, /* -i */
500 (u_longlong_t)zopt_killrate, /* -k */
501 zopt_pool, /* -p */
502 zopt_dir, /* -f */
503 (u_longlong_t)zopt_time, /* -T */
428870ff 504 (u_longlong_t)zopt_maxloops, /* -F */
b128c09f 505 (u_longlong_t)zopt_passtime); /* -P */
34dc7c2f
BB
506 exit(requested ? 0 : 1);
507}
508
34dc7c2f
BB
509static void
510process_options(int argc, char **argv)
511{
512 int opt;
513 uint64_t value;
514
515 /* By default, test gang blocks for blocks 32K and greater */
516 metaslab_gang_bang = 32 << 10;
517
34dc7c2f 518 while ((opt = getopt(argc, argv,
428870ff 519 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
34dc7c2f
BB
520 value = 0;
521 switch (opt) {
522 case 'v':
523 case 's':
524 case 'a':
525 case 'm':
526 case 'r':
527 case 'R':
528 case 'd':
529 case 't':
530 case 'g':
531 case 'i':
532 case 'k':
533 case 'T':
534 case 'P':
428870ff 535 case 'F':
34dc7c2f
BB
536 value = nicenumtoull(optarg);
537 }
538 switch (opt) {
539 case 'v':
540 zopt_vdevs = value;
541 break;
542 case 's':
543 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
544 break;
545 case 'a':
546 zopt_ashift = value;
547 break;
548 case 'm':
549 zopt_mirrors = value;
550 break;
551 case 'r':
552 zopt_raidz = MAX(1, value);
553 break;
554 case 'R':
45d1cae3 555 zopt_raidz_parity = MIN(MAX(value, 1), 3);
34dc7c2f
BB
556 break;
557 case 'd':
558 zopt_datasets = MAX(1, value);
559 break;
560 case 't':
561 zopt_threads = MAX(1, value);
562 break;
563 case 'g':
564 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
565 break;
566 case 'i':
567 zopt_init = value;
568 break;
569 case 'k':
570 zopt_killrate = value;
571 break;
572 case 'p':
573 zopt_pool = strdup(optarg);
574 break;
575 case 'f':
576 zopt_dir = strdup(optarg);
577 break;
578 case 'V':
579 zopt_verbose++;
580 break;
581 case 'E':
582 zopt_init = 0;
583 break;
584 case 'T':
585 zopt_time = value;
586 break;
587 case 'P':
588 zopt_passtime = MAX(1, value);
589 break;
428870ff
BB
590 case 'F':
591 zopt_maxloops = MAX(1, value);
592 break;
34dc7c2f
BB
593 case 'h':
594 usage(B_TRUE);
595 break;
596 case '?':
597 default:
598 usage(B_FALSE);
599 break;
600 }
601 }
602
603 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
604
428870ff
BB
605 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
606 UINT64_MAX >> 2);
607}
608
609static void
610ztest_kill(ztest_shared_t *zs)
611{
612 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
613 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
614 (void) kill(getpid(), SIGKILL);
615}
616
617static uint64_t
618ztest_random(uint64_t range)
619{
620 uint64_t r;
621
622 if (range == 0)
623 return (0);
624
625 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
626 fatal(1, "short read from /dev/urandom");
627
628 return (r % range);
629}
630
631/* ARGSUSED */
632static void
633ztest_record_enospc(const char *s)
634{
635 ztest_shared->zs_enospc_count++;
34dc7c2f
BB
636}
637
638static uint64_t
639ztest_get_ashift(void)
640{
641 if (zopt_ashift == 0)
642 return (SPA_MINBLOCKSHIFT + ztest_random(3));
643 return (zopt_ashift);
644}
645
646static nvlist_t *
b128c09f 647make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
34dc7c2f 648{
b128c09f 649 char pathbuf[MAXPATHLEN];
34dc7c2f 650 uint64_t vdev;
34dc7c2f
BB
651 nvlist_t *file;
652
b128c09f
BB
653 if (ashift == 0)
654 ashift = ztest_get_ashift();
655
656 if (path == NULL) {
657 path = pathbuf;
658
659 if (aux != NULL) {
660 vdev = ztest_shared->zs_vdev_aux;
661 (void) sprintf(path, ztest_aux_template,
662 zopt_dir, zopt_pool, aux, vdev);
663 } else {
428870ff 664 vdev = ztest_shared->zs_vdev_next_leaf++;
b128c09f
BB
665 (void) sprintf(path, ztest_dev_template,
666 zopt_dir, zopt_pool, vdev);
667 }
668 }
34dc7c2f 669
b128c09f
BB
670 if (size != 0) {
671 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
34dc7c2f 672 if (fd == -1)
b128c09f 673 fatal(1, "can't open %s", path);
34dc7c2f 674 if (ftruncate(fd, size) != 0)
b128c09f 675 fatal(1, "can't ftruncate %s", path);
34dc7c2f
BB
676 (void) close(fd);
677 }
678
679 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
680 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
b128c09f 681 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
34dc7c2f
BB
682 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
683
684 return (file);
685}
686
687static nvlist_t *
b128c09f 688make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
34dc7c2f
BB
689{
690 nvlist_t *raidz, **child;
691 int c;
692
693 if (r < 2)
b128c09f 694 return (make_vdev_file(path, aux, size, ashift));
34dc7c2f
BB
695 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
696
697 for (c = 0; c < r; c++)
b128c09f 698 child[c] = make_vdev_file(path, aux, size, ashift);
34dc7c2f
BB
699
700 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
701 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
702 VDEV_TYPE_RAIDZ) == 0);
703 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
704 zopt_raidz_parity) == 0);
705 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
706 child, r) == 0);
707
708 for (c = 0; c < r; c++)
709 nvlist_free(child[c]);
710
711 umem_free(child, r * sizeof (nvlist_t *));
712
713 return (raidz);
714}
715
716static nvlist_t *
b128c09f
BB
717make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
718 int r, int m)
34dc7c2f
BB
719{
720 nvlist_t *mirror, **child;
721 int c;
722
723 if (m < 1)
b128c09f 724 return (make_vdev_raidz(path, aux, size, ashift, r));
34dc7c2f
BB
725
726 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
727
728 for (c = 0; c < m; c++)
b128c09f 729 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
34dc7c2f
BB
730
731 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
732 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
733 VDEV_TYPE_MIRROR) == 0);
734 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
735 child, m) == 0);
34dc7c2f
BB
736
737 for (c = 0; c < m; c++)
738 nvlist_free(child[c]);
739
740 umem_free(child, m * sizeof (nvlist_t *));
741
742 return (mirror);
743}
744
745static nvlist_t *
b128c09f
BB
746make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
747 int log, int r, int m, int t)
34dc7c2f
BB
748{
749 nvlist_t *root, **child;
750 int c;
751
752 ASSERT(t > 0);
753
754 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
755
b128c09f
BB
756 for (c = 0; c < t; c++) {
757 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
758 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
759 log) == 0);
760 }
34dc7c2f
BB
761
762 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
763 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
b128c09f 764 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
34dc7c2f
BB
765 child, t) == 0);
766
767 for (c = 0; c < t; c++)
768 nvlist_free(child[c]);
769
770 umem_free(child, t * sizeof (nvlist_t *));
771
772 return (root);
773}
774
428870ff
BB
775static int
776ztest_random_blocksize(void)
34dc7c2f 777{
428870ff
BB
778 return (1 << (SPA_MINBLOCKSHIFT +
779 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
780}
34dc7c2f 781
428870ff
BB
782static int
783ztest_random_ibshift(void)
784{
785 return (DN_MIN_INDBLKSHIFT +
786 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
34dc7c2f
BB
787}
788
428870ff
BB
789static uint64_t
790ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
34dc7c2f 791{
428870ff
BB
792 uint64_t top;
793 vdev_t *rvd = spa->spa_root_vdev;
794 vdev_t *tvd;
34dc7c2f 795
428870ff 796 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 797
428870ff
BB
798 do {
799 top = ztest_random(rvd->vdev_children);
800 tvd = rvd->vdev_child[top];
801 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
802 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
34dc7c2f 803
428870ff 804 return (top);
34dc7c2f
BB
805}
806
428870ff
BB
807static uint64_t
808ztest_random_dsl_prop(zfs_prop_t prop)
34dc7c2f 809{
428870ff
BB
810 uint64_t value;
811
812 do {
813 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
814 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
815
816 return (value);
34dc7c2f
BB
817}
818
34dc7c2f 819static int
428870ff
BB
820ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
821 boolean_t inherit)
34dc7c2f 822{
428870ff
BB
823 const char *propname = zfs_prop_to_name(prop);
824 const char *valname;
825 char setpoint[MAXPATHLEN];
826 uint64_t curval;
34dc7c2f
BB
827 int error;
828
428870ff
BB
829 error = dsl_prop_set(osname, propname,
830 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
831 sizeof (value), 1, &value);
34dc7c2f 832
428870ff
BB
833 if (error == ENOSPC) {
834 ztest_record_enospc(FTAG);
34dc7c2f
BB
835 return (error);
836 }
34dc7c2f 837 ASSERT3U(error, ==, 0);
34dc7c2f 838
428870ff
BB
839 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
840 1, &curval, setpoint), ==, 0);
841
842 if (zopt_verbose >= 6) {
843 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
844 (void) printf("%s %s = %s at '%s'\n",
845 osname, propname, valname, setpoint);
34dc7c2f
BB
846 }
847
848 return (error);
849}
850
851static int
428870ff 852ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
34dc7c2f 853{
428870ff
BB
854 spa_t *spa = zs->zs_spa;
855 nvlist_t *props = NULL;
34dc7c2f
BB
856 int error;
857
428870ff
BB
858 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
859 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
34dc7c2f 860
428870ff
BB
861 error = spa_prop_set(spa, props);
862
863 nvlist_free(props);
864
865 if (error == ENOSPC) {
866 ztest_record_enospc(FTAG);
34dc7c2f
BB
867 return (error);
868 }
428870ff 869 ASSERT3U(error, ==, 0);
34dc7c2f
BB
870
871 return (error);
872}
873
428870ff
BB
874static void
875ztest_rll_init(rll_t *rll)
876{
877 rll->rll_writer = NULL;
878 rll->rll_readers = 0;
879 VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
880 VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
881}
34dc7c2f 882
428870ff
BB
883static void
884ztest_rll_destroy(rll_t *rll)
34dc7c2f 885{
428870ff
BB
886 ASSERT(rll->rll_writer == NULL);
887 ASSERT(rll->rll_readers == 0);
888 VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
889 VERIFY(cond_destroy(&rll->rll_cv) == 0);
890}
34dc7c2f 891
428870ff
BB
892static void
893ztest_rll_lock(rll_t *rll, rl_type_t type)
894{
895 VERIFY(mutex_lock(&rll->rll_lock) == 0);
34dc7c2f 896
428870ff
BB
897 if (type == RL_READER) {
898 while (rll->rll_writer != NULL)
899 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
900 rll->rll_readers++;
901 } else {
902 while (rll->rll_writer != NULL || rll->rll_readers)
903 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
904 rll->rll_writer = curthread;
905 }
34dc7c2f 906
428870ff
BB
907 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
908}
34dc7c2f 909
428870ff
BB
910static void
911ztest_rll_unlock(rll_t *rll)
912{
913 VERIFY(mutex_lock(&rll->rll_lock) == 0);
34dc7c2f 914
428870ff
BB
915 if (rll->rll_writer) {
916 ASSERT(rll->rll_readers == 0);
917 rll->rll_writer = NULL;
918 } else {
919 ASSERT(rll->rll_readers != 0);
920 ASSERT(rll->rll_writer == NULL);
921 rll->rll_readers--;
922 }
34dc7c2f 923
428870ff
BB
924 if (rll->rll_writer == NULL && rll->rll_readers == 0)
925 VERIFY(cond_broadcast(&rll->rll_cv) == 0);
926
927 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
34dc7c2f
BB
928}
929
428870ff
BB
930static void
931ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
b128c09f 932{
428870ff 933 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
b128c09f 934
428870ff
BB
935 ztest_rll_lock(rll, type);
936}
b128c09f 937
428870ff
BB
938static void
939ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
940{
941 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
b128c09f 942
428870ff 943 ztest_rll_unlock(rll);
b128c09f
BB
944}
945
428870ff
BB
946static rl_t *
947ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
948 uint64_t size, rl_type_t type)
34dc7c2f 949{
428870ff
BB
950 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
951 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
952 rl_t *rl;
34dc7c2f 953
428870ff
BB
954 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
955 rl->rl_object = object;
956 rl->rl_offset = offset;
957 rl->rl_size = size;
958 rl->rl_lock = rll;
34dc7c2f 959
428870ff
BB
960 ztest_rll_lock(rll, type);
961
962 return (rl);
963}
34dc7c2f 964
428870ff
BB
965static void
966ztest_range_unlock(rl_t *rl)
967{
968 rll_t *rll = rl->rl_lock;
34dc7c2f 969
428870ff 970 ztest_rll_unlock(rll);
34dc7c2f 971
428870ff
BB
972 umem_free(rl, sizeof (*rl));
973}
34dc7c2f 974
428870ff
BB
975static void
976ztest_zd_init(ztest_ds_t *zd, objset_t *os)
977{
978 zd->zd_os = os;
979 zd->zd_zilog = dmu_objset_zil(os);
980 zd->zd_seq = 0;
981 dmu_objset_name(os, zd->zd_name);
d6320ddb 982 int l;
428870ff
BB
983
984 VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f 985
d6320ddb 986 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
428870ff 987 ztest_rll_init(&zd->zd_object_lock[l]);
34dc7c2f 988
d6320ddb 989 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
428870ff 990 ztest_rll_init(&zd->zd_range_lock[l]);
34dc7c2f
BB
991}
992
428870ff
BB
993static void
994ztest_zd_fini(ztest_ds_t *zd)
34dc7c2f 995{
d6320ddb
BB
996 int l;
997
428870ff 998 VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
34dc7c2f 999
d6320ddb 1000 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
428870ff 1001 ztest_rll_destroy(&zd->zd_object_lock[l]);
b128c09f 1002
d6320ddb 1003 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
428870ff
BB
1004 ztest_rll_destroy(&zd->zd_range_lock[l]);
1005}
b128c09f 1006
428870ff 1007#define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
b128c09f 1008
428870ff
BB
1009static uint64_t
1010ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1011{
1012 uint64_t txg;
1013 int error;
1014
1015 /*
1016 * Attempt to assign tx to some transaction group.
1017 */
1018 error = dmu_tx_assign(tx, txg_how);
1019 if (error) {
1020 if (error == ERESTART) {
1021 ASSERT(txg_how == TXG_NOWAIT);
1022 dmu_tx_wait(tx);
1023 } else {
1024 ASSERT3U(error, ==, ENOSPC);
1025 ztest_record_enospc(tag);
1026 }
1027 dmu_tx_abort(tx);
1028 return (0);
1029 }
1030 txg = dmu_tx_get_txg(tx);
1031 ASSERT(txg != 0);
1032 return (txg);
1033}
1034
1035static void
1036ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1037{
1038 uint64_t *ip = buf;
1039 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1040
1041 while (ip < ip_end)
1042 *ip++ = value;
1043}
1044
1fde1e37 1045#ifndef NDEBUG
428870ff
BB
1046static boolean_t
1047ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1048{
1049 uint64_t *ip = buf;
1050 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1051 uint64_t diff = 0;
1052
1053 while (ip < ip_end)
1054 diff |= (value - *ip++);
1055
1056 return (diff == 0);
1057}
1fde1e37 1058#endif
428870ff
BB
1059
1060static void
1061ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1062 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1063{
1064 bt->bt_magic = BT_MAGIC;
1065 bt->bt_objset = dmu_objset_id(os);
1066 bt->bt_object = object;
1067 bt->bt_offset = offset;
1068 bt->bt_gen = gen;
1069 bt->bt_txg = txg;
1070 bt->bt_crtxg = crtxg;
1071}
1072
1073static void
1074ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1075 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1076{
1077 ASSERT(bt->bt_magic == BT_MAGIC);
1078 ASSERT(bt->bt_objset == dmu_objset_id(os));
1079 ASSERT(bt->bt_object == object);
1080 ASSERT(bt->bt_offset == offset);
1081 ASSERT(bt->bt_gen <= gen);
1082 ASSERT(bt->bt_txg <= txg);
1083 ASSERT(bt->bt_crtxg == crtxg);
1084}
1085
1086static ztest_block_tag_t *
1087ztest_bt_bonus(dmu_buf_t *db)
1088{
1089 dmu_object_info_t doi;
1090 ztest_block_tag_t *bt;
1091
1092 dmu_object_info_from_db(db, &doi);
1093 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1094 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1095 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1096
1097 return (bt);
1098}
1099
1100/*
1101 * ZIL logging ops
1102 */
1103
1104#define lrz_type lr_mode
1105#define lrz_blocksize lr_uid
1106#define lrz_ibshift lr_gid
1107#define lrz_bonustype lr_rdev
1108#define lrz_bonuslen lr_crtime[1]
1109
572e2857 1110static void
428870ff
BB
1111ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1112{
1113 char *name = (void *)(lr + 1); /* name follows lr */
1114 size_t namesize = strlen(name) + 1;
1115 itx_t *itx;
1116
1117 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1118 return;
428870ff
BB
1119
1120 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1121 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1122 sizeof (*lr) + namesize - sizeof (lr_t));
1123
572e2857 1124 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1125}
1126
572e2857
BB
1127static void
1128ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
428870ff
BB
1129{
1130 char *name = (void *)(lr + 1); /* name follows lr */
1131 size_t namesize = strlen(name) + 1;
1132 itx_t *itx;
1133
1134 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1135 return;
428870ff
BB
1136
1137 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1138 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1139 sizeof (*lr) + namesize - sizeof (lr_t));
1140
572e2857
BB
1141 itx->itx_oid = object;
1142 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1143}
1144
572e2857 1145static void
428870ff
BB
1146ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1147{
1148 itx_t *itx;
1149 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1150
1151 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1152 return;
428870ff
BB
1153
1154 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1155 write_state = WR_INDIRECT;
1156
1157 itx = zil_itx_create(TX_WRITE,
1158 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1159
1160 if (write_state == WR_COPIED &&
1161 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1162 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1163 zil_itx_destroy(itx);
1164 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1165 write_state = WR_NEED_COPY;
1166 }
1167 itx->itx_private = zd;
1168 itx->itx_wr_state = write_state;
1169 itx->itx_sync = (ztest_random(8) == 0);
1170 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1171
1172 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1173 sizeof (*lr) - sizeof (lr_t));
1174
572e2857 1175 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1176}
1177
572e2857 1178static void
428870ff
BB
1179ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1180{
1181 itx_t *itx;
1182
1183 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1184 return;
428870ff
BB
1185
1186 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1187 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1188 sizeof (*lr) - sizeof (lr_t));
1189
572e2857
BB
1190 itx->itx_sync = B_FALSE;
1191 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1192}
1193
572e2857 1194static void
428870ff
BB
1195ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1196{
1197 itx_t *itx;
1198
1199 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1200 return;
428870ff
BB
1201
1202 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1203 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1204 sizeof (*lr) - sizeof (lr_t));
1205
572e2857
BB
1206 itx->itx_sync = B_FALSE;
1207 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1208}
1209
1210/*
1211 * ZIL replay ops
1212 */
1213static int
1214ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1215{
1216 char *name = (void *)(lr + 1); /* name follows lr */
1217 objset_t *os = zd->zd_os;
1218 ztest_block_tag_t *bbt;
1219 dmu_buf_t *db;
1220 dmu_tx_t *tx;
1221 uint64_t txg;
1222 int error = 0;
1223
1224 if (byteswap)
1225 byteswap_uint64_array(lr, sizeof (*lr));
1226
1227 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1228 ASSERT(name[0] != '\0');
1229
1230 tx = dmu_tx_create(os);
1231
1232 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1233
1234 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1235 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1236 } else {
1237 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1238 }
1239
1240 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1241 if (txg == 0)
1242 return (ENOSPC);
1243
1244 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1245
1246 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1247 if (lr->lr_foid == 0) {
1248 lr->lr_foid = zap_create(os,
1249 lr->lrz_type, lr->lrz_bonustype,
1250 lr->lrz_bonuslen, tx);
1251 } else {
1252 error = zap_create_claim(os, lr->lr_foid,
1253 lr->lrz_type, lr->lrz_bonustype,
1254 lr->lrz_bonuslen, tx);
1255 }
1256 } else {
1257 if (lr->lr_foid == 0) {
1258 lr->lr_foid = dmu_object_alloc(os,
1259 lr->lrz_type, 0, lr->lrz_bonustype,
1260 lr->lrz_bonuslen, tx);
1261 } else {
1262 error = dmu_object_claim(os, lr->lr_foid,
1263 lr->lrz_type, 0, lr->lrz_bonustype,
1264 lr->lrz_bonuslen, tx);
1265 }
1266 }
1267
1268 if (error) {
1269 ASSERT3U(error, ==, EEXIST);
1270 ASSERT(zd->zd_zilog->zl_replay);
1271 dmu_tx_commit(tx);
1272 return (error);
1273 }
1274
1275 ASSERT(lr->lr_foid != 0);
1276
1277 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1278 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1279 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1280
1281 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1282 bbt = ztest_bt_bonus(db);
1283 dmu_buf_will_dirty(db, tx);
1284 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1285 dmu_buf_rele(db, FTAG);
1286
1287 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1288 &lr->lr_foid, tx));
1289
1290 (void) ztest_log_create(zd, tx, lr);
1291
1292 dmu_tx_commit(tx);
1293
1294 return (0);
1295}
1296
1297static int
1298ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1299{
1300 char *name = (void *)(lr + 1); /* name follows lr */
1301 objset_t *os = zd->zd_os;
1302 dmu_object_info_t doi;
1303 dmu_tx_t *tx;
1304 uint64_t object, txg;
1305
1306 if (byteswap)
1307 byteswap_uint64_array(lr, sizeof (*lr));
1308
1309 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1310 ASSERT(name[0] != '\0');
1311
1312 VERIFY3U(0, ==,
1313 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1314 ASSERT(object != 0);
1315
1316 ztest_object_lock(zd, object, RL_WRITER);
1317
1318 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1319
1320 tx = dmu_tx_create(os);
1321
1322 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1323 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1324
1325 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1326 if (txg == 0) {
1327 ztest_object_unlock(zd, object);
1328 return (ENOSPC);
1329 }
1330
1331 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1332 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1333 } else {
1334 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1335 }
1336
1337 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1338
572e2857 1339 (void) ztest_log_remove(zd, tx, lr, object);
428870ff
BB
1340
1341 dmu_tx_commit(tx);
1342
1343 ztest_object_unlock(zd, object);
1344
1345 return (0);
1346}
1347
1348static int
1349ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1350{
1351 objset_t *os = zd->zd_os;
1352 void *data = lr + 1; /* data follows lr */
1353 uint64_t offset, length;
1354 ztest_block_tag_t *bt = data;
1355 ztest_block_tag_t *bbt;
1356 uint64_t gen, txg, lrtxg, crtxg;
1357 dmu_object_info_t doi;
1358 dmu_tx_t *tx;
1359 dmu_buf_t *db;
1360 arc_buf_t *abuf = NULL;
1361 rl_t *rl;
1362
1363 if (byteswap)
1364 byteswap_uint64_array(lr, sizeof (*lr));
1365
1366 offset = lr->lr_offset;
1367 length = lr->lr_length;
1368
1369 /* If it's a dmu_sync() block, write the whole block */
1370 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1371 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1372 if (length < blocksize) {
1373 offset -= offset % blocksize;
1374 length = blocksize;
1375 }
1376 }
1377
1378 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1379 byteswap_uint64_array(bt, sizeof (*bt));
1380
1381 if (bt->bt_magic != BT_MAGIC)
1382 bt = NULL;
1383
1384 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1385 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1386
1387 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1388
1389 dmu_object_info_from_db(db, &doi);
1390
1391 bbt = ztest_bt_bonus(db);
1392 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1393 gen = bbt->bt_gen;
1394 crtxg = bbt->bt_crtxg;
1395 lrtxg = lr->lr_common.lrc_txg;
1396
1397 tx = dmu_tx_create(os);
1398
1399 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1400
1401 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1402 P2PHASE(offset, length) == 0)
1403 abuf = dmu_request_arcbuf(db, length);
1404
1405 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1406 if (txg == 0) {
1407 if (abuf != NULL)
1408 dmu_return_arcbuf(abuf);
1409 dmu_buf_rele(db, FTAG);
1410 ztest_range_unlock(rl);
1411 ztest_object_unlock(zd, lr->lr_foid);
1412 return (ENOSPC);
1413 }
1414
1415 if (bt != NULL) {
1416 /*
1417 * Usually, verify the old data before writing new data --
1418 * but not always, because we also want to verify correct
1419 * behavior when the data was not recently read into cache.
1420 */
1421 ASSERT(offset % doi.doi_data_block_size == 0);
1422 if (ztest_random(4) != 0) {
1423 int prefetch = ztest_random(2) ?
1424 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1425 ztest_block_tag_t rbt;
1426
1427 VERIFY(dmu_read(os, lr->lr_foid, offset,
1428 sizeof (rbt), &rbt, prefetch) == 0);
1429 if (rbt.bt_magic == BT_MAGIC) {
1430 ztest_bt_verify(&rbt, os, lr->lr_foid,
1431 offset, gen, txg, crtxg);
1432 }
1433 }
1434
1435 /*
1436 * Writes can appear to be newer than the bonus buffer because
1437 * the ztest_get_data() callback does a dmu_read() of the
1438 * open-context data, which may be different than the data
1439 * as it was when the write was generated.
1440 */
1441 if (zd->zd_zilog->zl_replay) {
1442 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1443 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1444 bt->bt_crtxg);
1445 }
1446
1447 /*
1448 * Set the bt's gen/txg to the bonus buffer's gen/txg
1449 * so that all of the usual ASSERTs will work.
1450 */
1451 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1452 }
1453
1454 if (abuf == NULL) {
1455 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1456 } else {
1457 bcopy(data, abuf->b_data, length);
1458 dmu_assign_arcbuf(db, offset, abuf, tx);
1459 }
1460
1461 (void) ztest_log_write(zd, tx, lr);
1462
1463 dmu_buf_rele(db, FTAG);
1464
1465 dmu_tx_commit(tx);
1466
1467 ztest_range_unlock(rl);
1468 ztest_object_unlock(zd, lr->lr_foid);
1469
1470 return (0);
1471}
1472
1473static int
1474ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1475{
1476 objset_t *os = zd->zd_os;
1477 dmu_tx_t *tx;
1478 uint64_t txg;
1479 rl_t *rl;
1480
1481 if (byteswap)
1482 byteswap_uint64_array(lr, sizeof (*lr));
1483
1484 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1485 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1486 RL_WRITER);
1487
1488 tx = dmu_tx_create(os);
1489
1490 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1491
1492 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1493 if (txg == 0) {
1494 ztest_range_unlock(rl);
1495 ztest_object_unlock(zd, lr->lr_foid);
1496 return (ENOSPC);
1497 }
1498
1499 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1500 lr->lr_length, tx) == 0);
1501
1502 (void) ztest_log_truncate(zd, tx, lr);
1503
1504 dmu_tx_commit(tx);
1505
1506 ztest_range_unlock(rl);
1507 ztest_object_unlock(zd, lr->lr_foid);
1508
1509 return (0);
1510}
1511
1512static int
1513ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1514{
1515 objset_t *os = zd->zd_os;
1516 dmu_tx_t *tx;
1517 dmu_buf_t *db;
1518 ztest_block_tag_t *bbt;
1519 uint64_t txg, lrtxg, crtxg;
1520
1521 if (byteswap)
1522 byteswap_uint64_array(lr, sizeof (*lr));
1523
1524 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1525
1526 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1527
1528 tx = dmu_tx_create(os);
1529 dmu_tx_hold_bonus(tx, lr->lr_foid);
1530
1531 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1532 if (txg == 0) {
1533 dmu_buf_rele(db, FTAG);
1534 ztest_object_unlock(zd, lr->lr_foid);
1535 return (ENOSPC);
1536 }
1537
1538 bbt = ztest_bt_bonus(db);
1539 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1540 crtxg = bbt->bt_crtxg;
1541 lrtxg = lr->lr_common.lrc_txg;
1542
1543 if (zd->zd_zilog->zl_replay) {
1544 ASSERT(lr->lr_size != 0);
1545 ASSERT(lr->lr_mode != 0);
1546 ASSERT(lrtxg != 0);
1547 } else {
1548 /*
1549 * Randomly change the size and increment the generation.
1550 */
1551 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1552 sizeof (*bbt);
1553 lr->lr_mode = bbt->bt_gen + 1;
1554 ASSERT(lrtxg == 0);
1555 }
1556
1557 /*
1558 * Verify that the current bonus buffer is not newer than our txg.
1559 */
1560 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1561 MAX(txg, lrtxg), crtxg);
1562
1563 dmu_buf_will_dirty(db, tx);
1564
1565 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1566 ASSERT3U(lr->lr_size, <=, db->db_size);
1567 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1568 bbt = ztest_bt_bonus(db);
1569
1570 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1571
1572 dmu_buf_rele(db, FTAG);
1573
1574 (void) ztest_log_setattr(zd, tx, lr);
1575
1576 dmu_tx_commit(tx);
1577
1578 ztest_object_unlock(zd, lr->lr_foid);
1579
1580 return (0);
1581}
1582
1583zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
0bc8fd78
BB
1584 NULL, /* 0 no such transaction type */
1585 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1586 NULL, /* TX_MKDIR */
1587 NULL, /* TX_MKXATTR */
1588 NULL, /* TX_SYMLINK */
1589 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1590 NULL, /* TX_RMDIR */
1591 NULL, /* TX_LINK */
1592 NULL, /* TX_RENAME */
1593 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1594 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1595 (zil_replay_func_t *)ztest_replay_setattr, /* TX_SETATTR */
1596 NULL, /* TX_ACL */
1597 NULL, /* TX_CREATE_ACL */
1598 NULL, /* TX_CREATE_ATTR */
1599 NULL, /* TX_CREATE_ACL_ATTR */
1600 NULL, /* TX_MKDIR_ACL */
1601 NULL, /* TX_MKDIR_ATTR */
1602 NULL, /* TX_MKDIR_ACL_ATTR */
1603 NULL, /* TX_WRITE2 */
428870ff
BB
1604};
1605
1606/*
1607 * ZIL get_data callbacks
1608 */
1609
1610static void
1611ztest_get_done(zgd_t *zgd, int error)
1612{
1613 ztest_ds_t *zd = zgd->zgd_private;
1614 uint64_t object = zgd->zgd_rl->rl_object;
1615
1616 if (zgd->zgd_db)
1617 dmu_buf_rele(zgd->zgd_db, zgd);
1618
1619 ztest_range_unlock(zgd->zgd_rl);
1620 ztest_object_unlock(zd, object);
1621
1622 if (error == 0 && zgd->zgd_bp)
1623 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1624
1625 umem_free(zgd, sizeof (*zgd));
1626}
1627
1628static int
1629ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1630{
1631 ztest_ds_t *zd = arg;
1632 objset_t *os = zd->zd_os;
1633 uint64_t object = lr->lr_foid;
1634 uint64_t offset = lr->lr_offset;
1635 uint64_t size = lr->lr_length;
1636 blkptr_t *bp = &lr->lr_blkptr;
1637 uint64_t txg = lr->lr_common.lrc_txg;
1638 uint64_t crtxg;
1639 dmu_object_info_t doi;
1640 dmu_buf_t *db;
1641 zgd_t *zgd;
1642 int error;
1643
1644 ztest_object_lock(zd, object, RL_READER);
1645 error = dmu_bonus_hold(os, object, FTAG, &db);
1646 if (error) {
1647 ztest_object_unlock(zd, object);
1648 return (error);
1649 }
1650
1651 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1652
1653 if (crtxg == 0 || crtxg > txg) {
1654 dmu_buf_rele(db, FTAG);
1655 ztest_object_unlock(zd, object);
1656 return (ENOENT);
1657 }
1658
1659 dmu_object_info_from_db(db, &doi);
1660 dmu_buf_rele(db, FTAG);
1661 db = NULL;
1662
1663 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1664 zgd->zgd_zilog = zd->zd_zilog;
1665 zgd->zgd_private = zd;
1666
1667 if (buf != NULL) { /* immediate write */
1668 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1669 RL_READER);
1670
1671 error = dmu_read(os, object, offset, size, buf,
1672 DMU_READ_NO_PREFETCH);
1673 ASSERT(error == 0);
1674 } else {
1675 size = doi.doi_data_block_size;
1676 if (ISP2(size)) {
1677 offset = P2ALIGN(offset, size);
1678 } else {
1679 ASSERT(offset < size);
1680 offset = 0;
1681 }
1682
1683 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1684 RL_READER);
1685
1686 error = dmu_buf_hold(os, object, offset, zgd, &db,
1687 DMU_READ_NO_PREFETCH);
1688
1689 if (error == 0) {
1690 zgd->zgd_db = db;
1691 zgd->zgd_bp = bp;
1692
1693 ASSERT(db->db_offset == offset);
1694 ASSERT(db->db_size == size);
1695
1696 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1697 ztest_get_done, zgd);
1698
1699 if (error == 0)
1700 return (0);
1701 }
1702 }
1703
1704 ztest_get_done(zgd, error);
1705
1706 return (error);
1707}
1708
1709static void *
1710ztest_lr_alloc(size_t lrsize, char *name)
1711{
1712 char *lr;
1713 size_t namesize = name ? strlen(name) + 1 : 0;
1714
1715 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1716
1717 if (name)
1718 bcopy(name, lr + lrsize, namesize);
1719
1720 return (lr);
1721}
1722
1723void
1724ztest_lr_free(void *lr, size_t lrsize, char *name)
1725{
1726 size_t namesize = name ? strlen(name) + 1 : 0;
1727
1728 umem_free(lr, lrsize + namesize);
1729}
1730
1731/*
1732 * Lookup a bunch of objects. Returns the number of objects not found.
1733 */
1734static int
1735ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1736{
1737 int missing = 0;
1738 int error;
d6320ddb 1739 int i;
428870ff
BB
1740
1741 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1742
d6320ddb 1743 for (i = 0; i < count; i++, od++) {
428870ff
BB
1744 od->od_object = 0;
1745 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1746 sizeof (uint64_t), 1, &od->od_object);
1747 if (error) {
1748 ASSERT(error == ENOENT);
1749 ASSERT(od->od_object == 0);
1750 missing++;
1751 } else {
1752 dmu_buf_t *db;
1753 ztest_block_tag_t *bbt;
1754 dmu_object_info_t doi;
1755
1756 ASSERT(od->od_object != 0);
1757 ASSERT(missing == 0); /* there should be no gaps */
1758
1759 ztest_object_lock(zd, od->od_object, RL_READER);
1760 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1761 od->od_object, FTAG, &db));
1762 dmu_object_info_from_db(db, &doi);
1763 bbt = ztest_bt_bonus(db);
1764 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1765 od->od_type = doi.doi_type;
1766 od->od_blocksize = doi.doi_data_block_size;
1767 od->od_gen = bbt->bt_gen;
1768 dmu_buf_rele(db, FTAG);
1769 ztest_object_unlock(zd, od->od_object);
1770 }
1771 }
1772
1773 return (missing);
1774}
1775
1776static int
1777ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1778{
1779 int missing = 0;
d6320ddb 1780 int i;
428870ff
BB
1781
1782 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1783
d6320ddb 1784 for (i = 0; i < count; i++, od++) {
428870ff
BB
1785 if (missing) {
1786 od->od_object = 0;
1787 missing++;
1788 continue;
1789 }
1790
1791 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1792
1793 lr->lr_doid = od->od_dir;
1794 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1795 lr->lrz_type = od->od_crtype;
1796 lr->lrz_blocksize = od->od_crblocksize;
1797 lr->lrz_ibshift = ztest_random_ibshift();
1798 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1799 lr->lrz_bonuslen = dmu_bonus_max();
1800 lr->lr_gen = od->od_crgen;
1801 lr->lr_crtime[0] = time(NULL);
1802
1803 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1804 ASSERT(missing == 0);
1805 od->od_object = 0;
1806 missing++;
1807 } else {
1808 od->od_object = lr->lr_foid;
1809 od->od_type = od->od_crtype;
1810 od->od_blocksize = od->od_crblocksize;
1811 od->od_gen = od->od_crgen;
1812 ASSERT(od->od_object != 0);
1813 }
1814
1815 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1816 }
1817
1818 return (missing);
1819}
1820
1821static int
1822ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1823{
1824 int missing = 0;
1825 int error;
d6320ddb 1826 int i;
428870ff
BB
1827
1828 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1829
1830 od += count - 1;
1831
d6320ddb 1832 for (i = count - 1; i >= 0; i--, od--) {
428870ff
BB
1833 if (missing) {
1834 missing++;
1835 continue;
1836 }
1837
1838 if (od->od_object == 0)
1839 continue;
1840
1841 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1842
1843 lr->lr_doid = od->od_dir;
1844
1845 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1846 ASSERT3U(error, ==, ENOSPC);
1847 missing++;
1848 } else {
1849 od->od_object = 0;
1850 }
1851 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1852 }
1853
1854 return (missing);
1855}
1856
1857static int
1858ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1859 void *data)
1860{
1861 lr_write_t *lr;
1862 int error;
1863
1864 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1865
1866 lr->lr_foid = object;
1867 lr->lr_offset = offset;
1868 lr->lr_length = size;
1869 lr->lr_blkoff = 0;
1870 BP_ZERO(&lr->lr_blkptr);
1871
1872 bcopy(data, lr + 1, size);
1873
1874 error = ztest_replay_write(zd, lr, B_FALSE);
1875
1876 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1877
1878 return (error);
1879}
1880
1881static int
1882ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1883{
1884 lr_truncate_t *lr;
1885 int error;
1886
1887 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1888
1889 lr->lr_foid = object;
1890 lr->lr_offset = offset;
1891 lr->lr_length = size;
1892
1893 error = ztest_replay_truncate(zd, lr, B_FALSE);
1894
1895 ztest_lr_free(lr, sizeof (*lr), NULL);
1896
1897 return (error);
1898}
1899
1900static int
1901ztest_setattr(ztest_ds_t *zd, uint64_t object)
1902{
1903 lr_setattr_t *lr;
1904 int error;
1905
1906 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1907
1908 lr->lr_foid = object;
1909 lr->lr_size = 0;
1910 lr->lr_mode = 0;
1911
1912 error = ztest_replay_setattr(zd, lr, B_FALSE);
1913
1914 ztest_lr_free(lr, sizeof (*lr), NULL);
1915
1916 return (error);
1917}
1918
1919static void
1920ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1921{
1922 objset_t *os = zd->zd_os;
1923 dmu_tx_t *tx;
1924 uint64_t txg;
1925 rl_t *rl;
1926
1927 txg_wait_synced(dmu_objset_pool(os), 0);
1928
1929 ztest_object_lock(zd, object, RL_READER);
1930 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1931
1932 tx = dmu_tx_create(os);
1933
1934 dmu_tx_hold_write(tx, object, offset, size);
1935
1936 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1937
1938 if (txg != 0) {
1939 dmu_prealloc(os, object, offset, size, tx);
1940 dmu_tx_commit(tx);
1941 txg_wait_synced(dmu_objset_pool(os), txg);
1942 } else {
1943 (void) dmu_free_long_range(os, object, offset, size);
1944 }
1945
1946 ztest_range_unlock(rl);
1947 ztest_object_unlock(zd, object);
1948}
1949
1950static void
1951ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1952{
1953 ztest_block_tag_t wbt;
1954 dmu_object_info_t doi;
1955 enum ztest_io_type io_type;
1956 uint64_t blocksize;
1957 void *data;
1958
1959 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1960 blocksize = doi.doi_data_block_size;
1961 data = umem_alloc(blocksize, UMEM_NOFAIL);
1962
1963 /*
1964 * Pick an i/o type at random, biased toward writing block tags.
1965 */
1966 io_type = ztest_random(ZTEST_IO_TYPES);
1967 if (ztest_random(2) == 0)
1968 io_type = ZTEST_IO_WRITE_TAG;
1969
1970 switch (io_type) {
1971
1972 case ZTEST_IO_WRITE_TAG:
1973 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
1974 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
1975 break;
1976
1977 case ZTEST_IO_WRITE_PATTERN:
1978 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
1979 if (ztest_random(2) == 0) {
1980 /*
1981 * Induce fletcher2 collisions to ensure that
1982 * zio_ddt_collision() detects and resolves them
1983 * when using fletcher2-verify for deduplication.
1984 */
1985 ((uint64_t *)data)[0] ^= 1ULL << 63;
1986 ((uint64_t *)data)[4] ^= 1ULL << 63;
1987 }
1988 (void) ztest_write(zd, object, offset, blocksize, data);
1989 break;
1990
1991 case ZTEST_IO_WRITE_ZEROES:
1992 bzero(data, blocksize);
1993 (void) ztest_write(zd, object, offset, blocksize, data);
1994 break;
1995
1996 case ZTEST_IO_TRUNCATE:
1997 (void) ztest_truncate(zd, object, offset, blocksize);
1998 break;
1999
2000 case ZTEST_IO_SETATTR:
2001 (void) ztest_setattr(zd, object);
2002 break;
e75c13c3
BB
2003 default:
2004 break;
428870ff
BB
2005 }
2006
2007 umem_free(data, blocksize);
2008}
2009
2010/*
2011 * Initialize an object description template.
2012 */
2013static void
2014ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2015 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2016{
2017 od->od_dir = ZTEST_DIROBJ;
2018 od->od_object = 0;
2019
2020 od->od_crtype = type;
2021 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2022 od->od_crgen = gen;
2023
2024 od->od_type = DMU_OT_NONE;
2025 od->od_blocksize = 0;
2026 od->od_gen = 0;
2027
2028 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
b8864a23 2029 tag, (longlong_t)id, (u_longlong_t)index);
428870ff
BB
2030}
2031
2032/*
2033 * Lookup or create the objects for a test using the od template.
2034 * If the objects do not all exist, or if 'remove' is specified,
2035 * remove any existing objects and create new ones. Otherwise,
2036 * use the existing objects.
2037 */
2038static int
2039ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2040{
2041 int count = size / sizeof (*od);
2042 int rv = 0;
2043
2044 VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2045 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2046 (ztest_remove(zd, od, count) != 0 ||
2047 ztest_create(zd, od, count) != 0))
2048 rv = -1;
2049 zd->zd_od = od;
2050 VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2051
2052 return (rv);
2053}
2054
2055/* ARGSUSED */
2056void
2057ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2058{
2059 zilog_t *zilog = zd->zd_zilog;
2060
572e2857 2061 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
428870ff
BB
2062
2063 /*
2064 * Remember the committed values in zd, which is in parent/child
2065 * shared memory. If we die, the next iteration of ztest_run()
2066 * will verify that the log really does contain this record.
2067 */
2068 mutex_enter(&zilog->zl_lock);
2069 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2070 zd->zd_seq = zilog->zl_commit_lr_seq;
2071 mutex_exit(&zilog->zl_lock);
2072}
2073
2074/*
2075 * Verify that we can't destroy an active pool, create an existing pool,
2076 * or create a pool with a bad vdev spec.
2077 */
2078/* ARGSUSED */
2079void
2080ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2081{
2082 ztest_shared_t *zs = ztest_shared;
2083 spa_t *spa;
2084 nvlist_t *nvroot;
2085
2086 /*
2087 * Attempt to create using a bad file.
2088 */
2089 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2090 VERIFY3U(ENOENT, ==,
2091 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2092 nvlist_free(nvroot);
2093
2094 /*
2095 * Attempt to create using a bad mirror.
2096 */
2097 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2098 VERIFY3U(ENOENT, ==,
2099 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2100 nvlist_free(nvroot);
2101
2102 /*
2103 * Attempt to create an existing pool. It shouldn't matter
2104 * what's in the nvroot; we should fail with EEXIST.
2105 */
2106 (void) rw_rdlock(&zs->zs_name_lock);
2107 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2108 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2109 nvlist_free(nvroot);
2110 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2111 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2112 spa_close(spa, FTAG);
2113
2114 (void) rw_unlock(&zs->zs_name_lock);
2115}
2116
2117static vdev_t *
2118vdev_lookup_by_path(vdev_t *vd, const char *path)
2119{
2120 vdev_t *mvd;
d6320ddb 2121 int c;
428870ff
BB
2122
2123 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2124 return (vd);
2125
d6320ddb 2126 for (c = 0; c < vd->vdev_children; c++)
428870ff
BB
2127 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2128 NULL)
2129 return (mvd);
2130
2131 return (NULL);
2132}
2133
2134/*
2135 * Find the first available hole which can be used as a top-level.
2136 */
2137int
2138find_vdev_hole(spa_t *spa)
2139{
2140 vdev_t *rvd = spa->spa_root_vdev;
2141 int c;
2142
2143 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2144
2145 for (c = 0; c < rvd->vdev_children; c++) {
2146 vdev_t *cvd = rvd->vdev_child[c];
2147
2148 if (cvd->vdev_ishole)
2149 break;
2150 }
2151 return (c);
2152}
2153
2154/*
2155 * Verify that vdev_add() works as expected.
2156 */
2157/* ARGSUSED */
2158void
2159ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2160{
2161 ztest_shared_t *zs = ztest_shared;
2162 spa_t *spa = zs->zs_spa;
2163 uint64_t leaves;
2164 uint64_t guid;
2165 nvlist_t *nvroot;
2166 int error;
2167
2168 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2169 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2170
2171 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2172
2173 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2174
2175 /*
2176 * If we have slogs then remove them 1/4 of the time.
2177 */
2178 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2179 /*
2180 * Grab the guid from the head of the log class rotor.
2181 */
2182 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2183
2184 spa_config_exit(spa, SCL_VDEV, FTAG);
2185
2186 /*
2187 * We have to grab the zs_name_lock as writer to
2188 * prevent a race between removing a slog (dmu_objset_find)
2189 * and destroying a dataset. Removing the slog will
2190 * grab a reference on the dataset which may cause
2191 * dmu_objset_destroy() to fail with EBUSY thus
2192 * leaving the dataset in an inconsistent state.
2193 */
2194 VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
2195 error = spa_vdev_remove(spa, guid, B_FALSE);
2196 VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
2197
2198 if (error && error != EEXIST)
2199 fatal(0, "spa_vdev_remove() = %d", error);
2200 } else {
2201 spa_config_exit(spa, SCL_VDEV, FTAG);
2202
2203 /*
2204 * Make 1/4 of the devices be log devices.
2205 */
2206 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2207 ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2208
2209 error = spa_vdev_add(spa, nvroot);
2210 nvlist_free(nvroot);
2211
2212 if (error == ENOSPC)
2213 ztest_record_enospc("spa_vdev_add");
2214 else if (error != 0)
2215 fatal(0, "spa_vdev_add() = %d", error);
2216 }
2217
2218 VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
2219}
2220
2221/*
2222 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2223 */
2224/* ARGSUSED */
2225void
2226ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2227{
2228 ztest_shared_t *zs = ztest_shared;
2229 spa_t *spa = zs->zs_spa;
2230 vdev_t *rvd = spa->spa_root_vdev;
2231 spa_aux_vdev_t *sav;
2232 char *aux;
2233 uint64_t guid = 0;
2234 int error;
2235
2236 if (ztest_random(2) == 0) {
2237 sav = &spa->spa_spares;
2238 aux = ZPOOL_CONFIG_SPARES;
2239 } else {
2240 sav = &spa->spa_l2cache;
2241 aux = ZPOOL_CONFIG_L2CACHE;
2242 }
2243
2244 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2245
2246 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2247
2248 if (sav->sav_count != 0 && ztest_random(4) == 0) {
b128c09f
BB
2249 /*
2250 * Pick a random device to remove.
2251 */
2252 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2253 } else {
2254 /*
2255 * Find an unused device we can add.
2256 */
428870ff 2257 zs->zs_vdev_aux = 0;
b128c09f
BB
2258 for (;;) {
2259 char path[MAXPATHLEN];
2260 int c;
2261 (void) sprintf(path, ztest_aux_template, zopt_dir,
428870ff 2262 zopt_pool, aux, zs->zs_vdev_aux);
b128c09f
BB
2263 for (c = 0; c < sav->sav_count; c++)
2264 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2265 path) == 0)
2266 break;
2267 if (c == sav->sav_count &&
2268 vdev_lookup_by_path(rvd, path) == NULL)
2269 break;
428870ff 2270 zs->zs_vdev_aux++;
34dc7c2f
BB
2271 }
2272 }
2273
b128c09f 2274 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f 2275
b128c09f
BB
2276 if (guid == 0) {
2277 /*
2278 * Add a new device.
2279 */
2280 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2281 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2282 error = spa_vdev_add(spa, nvroot);
2283 if (error != 0)
2284 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2285 nvlist_free(nvroot);
2286 } else {
2287 /*
2288 * Remove an existing device. Sometimes, dirty its
2289 * vdev state first to make sure we handle removal
2290 * of devices that have pending state changes.
2291 */
2292 if (ztest_random(2) == 0)
9babb374 2293 (void) vdev_online(spa, guid, 0, NULL);
b128c09f
BB
2294
2295 error = spa_vdev_remove(spa, guid, B_FALSE);
2296 if (error != 0 && error != EBUSY)
2297 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2298 }
2299
428870ff
BB
2300 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2301}
2302
2303/*
2304 * split a pool if it has mirror tlvdevs
2305 */
2306/* ARGSUSED */
2307void
2308ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2309{
2310 ztest_shared_t *zs = ztest_shared;
2311 spa_t *spa = zs->zs_spa;
2312 vdev_t *rvd = spa->spa_root_vdev;
2313 nvlist_t *tree, **child, *config, *split, **schild;
2314 uint_t c, children, schildren = 0, lastlogid = 0;
2315 int error = 0;
2316
2317 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2318
2319 /* ensure we have a useable config; mirrors of raidz aren't supported */
2320 if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2321 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2322 return;
2323 }
2324
2325 /* clean up the old pool, if any */
2326 (void) spa_destroy("splitp");
2327
2328 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2329
2330 /* generate a config from the existing config */
2331 mutex_enter(&spa->spa_props_lock);
2332 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2333 &tree) == 0);
2334 mutex_exit(&spa->spa_props_lock);
2335
2336 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2337 &children) == 0);
2338
2339 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2340 for (c = 0; c < children; c++) {
2341 vdev_t *tvd = rvd->vdev_child[c];
2342 nvlist_t **mchild;
2343 uint_t mchildren;
2344
2345 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2346 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2347 0) == 0);
2348 VERIFY(nvlist_add_string(schild[schildren],
2349 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2350 VERIFY(nvlist_add_uint64(schild[schildren],
2351 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2352 if (lastlogid == 0)
2353 lastlogid = schildren;
2354 ++schildren;
2355 continue;
2356 }
2357 lastlogid = 0;
2358 VERIFY(nvlist_lookup_nvlist_array(child[c],
2359 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2360 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2361 }
2362
2363 /* OK, create a config that can be used to split */
2364 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2365 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2366 VDEV_TYPE_ROOT) == 0);
2367 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2368 lastlogid != 0 ? lastlogid : schildren) == 0);
2369
2370 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2371 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2372
2373 for (c = 0; c < schildren; c++)
2374 nvlist_free(schild[c]);
2375 free(schild);
2376 nvlist_free(split);
2377
2378 spa_config_exit(spa, SCL_VDEV, FTAG);
2379
2380 (void) rw_wrlock(&zs->zs_name_lock);
2381 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2382 (void) rw_unlock(&zs->zs_name_lock);
2383
2384 nvlist_free(config);
2385
2386 if (error == 0) {
2387 (void) printf("successful split - results:\n");
2388 mutex_enter(&spa_namespace_lock);
2389 show_pool_stats(spa);
2390 show_pool_stats(spa_lookup("splitp"));
2391 mutex_exit(&spa_namespace_lock);
2392 ++zs->zs_splits;
2393 --zs->zs_mirrors;
2394 }
2395 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2396
34dc7c2f
BB
2397}
2398
2399/*
2400 * Verify that we can attach and detach devices.
2401 */
428870ff 2402/* ARGSUSED */
34dc7c2f 2403void
428870ff 2404ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
34dc7c2f 2405{
428870ff
BB
2406 ztest_shared_t *zs = ztest_shared;
2407 spa_t *spa = zs->zs_spa;
b128c09f 2408 spa_aux_vdev_t *sav = &spa->spa_spares;
34dc7c2f
BB
2409 vdev_t *rvd = spa->spa_root_vdev;
2410 vdev_t *oldvd, *newvd, *pvd;
b128c09f 2411 nvlist_t *root;
428870ff 2412 uint64_t leaves;
34dc7c2f
BB
2413 uint64_t leaf, top;
2414 uint64_t ashift = ztest_get_ashift();
fb5f0bc8 2415 uint64_t oldguid, pguid;
34dc7c2f
BB
2416 size_t oldsize, newsize;
2417 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2418 int replacing;
b128c09f
BB
2419 int oldvd_has_siblings = B_FALSE;
2420 int newvd_is_spare = B_FALSE;
2421 int oldvd_is_log;
34dc7c2f 2422 int error, expected_error;
34dc7c2f 2423
428870ff
BB
2424 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2425 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
34dc7c2f 2426
b128c09f 2427 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34dc7c2f
BB
2428
2429 /*
2430 * Decide whether to do an attach or a replace.
2431 */
2432 replacing = ztest_random(2);
2433
2434 /*
2435 * Pick a random top-level vdev.
2436 */
428870ff 2437 top = ztest_random_vdev_top(spa, B_TRUE);
34dc7c2f
BB
2438
2439 /*
2440 * Pick a random leaf within it.
2441 */
2442 leaf = ztest_random(leaves);
2443
2444 /*
b128c09f 2445 * Locate this vdev.
34dc7c2f 2446 */
b128c09f 2447 oldvd = rvd->vdev_child[top];
428870ff 2448 if (zs->zs_mirrors >= 1) {
fb5f0bc8 2449 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
428870ff 2450 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
b128c09f 2451 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
fb5f0bc8
BB
2452 }
2453 if (zopt_raidz > 1) {
2454 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2455 ASSERT(oldvd->vdev_children == zopt_raidz);
b128c09f 2456 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
fb5f0bc8 2457 }
34dc7c2f
BB
2458
2459 /*
b128c09f
BB
2460 * If we're already doing an attach or replace, oldvd may be a
2461 * mirror vdev -- in which case, pick a random child.
34dc7c2f 2462 */
b128c09f
BB
2463 while (oldvd->vdev_children != 0) {
2464 oldvd_has_siblings = B_TRUE;
fb5f0bc8
BB
2465 ASSERT(oldvd->vdev_children >= 2);
2466 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
b128c09f
BB
2467 }
2468
2469 oldguid = oldvd->vdev_guid;
9babb374 2470 oldsize = vdev_get_min_asize(oldvd);
b128c09f
BB
2471 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2472 (void) strcpy(oldpath, oldvd->vdev_path);
2473 pvd = oldvd->vdev_parent;
fb5f0bc8 2474 pguid = pvd->vdev_guid;
34dc7c2f
BB
2475
2476 /*
b128c09f 2477 * If oldvd has siblings, then half of the time, detach it.
34dc7c2f 2478 */
b128c09f
BB
2479 if (oldvd_has_siblings && ztest_random(2) == 0) {
2480 spa_config_exit(spa, SCL_VDEV, FTAG);
fb5f0bc8
BB
2481 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2482 if (error != 0 && error != ENODEV && error != EBUSY &&
2483 error != ENOTSUP)
2484 fatal(0, "detach (%s) returned %d", oldpath, error);
428870ff 2485 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
b128c09f
BB
2486 return;
2487 }
34dc7c2f
BB
2488
2489 /*
b128c09f
BB
2490 * For the new vdev, choose with equal probability between the two
2491 * standard paths (ending in either 'a' or 'b') or a random hot spare.
34dc7c2f 2492 */
b128c09f
BB
2493 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2494 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2495 newvd_is_spare = B_TRUE;
2496 (void) strcpy(newpath, newvd->vdev_path);
2497 } else {
2498 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2499 zopt_dir, zopt_pool, top * leaves + leaf);
2500 if (ztest_random(2) == 0)
2501 newpath[strlen(newpath) - 1] = 'b';
2502 newvd = vdev_lookup_by_path(rvd, newpath);
2503 }
2504
2505 if (newvd) {
9babb374 2506 newsize = vdev_get_min_asize(newvd);
b128c09f
BB
2507 } else {
2508 /*
2509 * Make newsize a little bigger or smaller than oldsize.
2510 * If it's smaller, the attach should fail.
2511 * If it's larger, and we're doing a replace,
2512 * we should get dynamic LUN growth when we're done.
2513 */
2514 newsize = 10 * oldsize / (9 + ztest_random(3));
2515 }
34dc7c2f
BB
2516
2517 /*
2518 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2519 * unless it's a replace; in that case any non-replacing parent is OK.
2520 *
2521 * If newvd is already part of the pool, it should fail with EBUSY.
2522 *
2523 * If newvd is too small, it should fail with EOVERFLOW.
2524 */
b128c09f
BB
2525 if (pvd->vdev_ops != &vdev_mirror_ops &&
2526 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2527 pvd->vdev_ops == &vdev_replacing_ops ||
2528 pvd->vdev_ops == &vdev_spare_ops))
34dc7c2f 2529 expected_error = ENOTSUP;
b128c09f
BB
2530 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2531 expected_error = ENOTSUP;
2532 else if (newvd == oldvd)
2533 expected_error = replacing ? 0 : EBUSY;
2534 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2535 expected_error = EBUSY;
34dc7c2f
BB
2536 else if (newsize < oldsize)
2537 expected_error = EOVERFLOW;
2538 else if (ashift > oldvd->vdev_top->vdev_ashift)
2539 expected_error = EDOM;
2540 else
2541 expected_error = 0;
2542
b128c09f 2543 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f
BB
2544
2545 /*
2546 * Build the nvlist describing newpath.
2547 */
b128c09f
BB
2548 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2549 ashift, 0, 0, 0, 1);
34dc7c2f 2550
b128c09f 2551 error = spa_vdev_attach(spa, oldguid, root, replacing);
34dc7c2f 2552
34dc7c2f
BB
2553 nvlist_free(root);
2554
2555 /*
2556 * If our parent was the replacing vdev, but the replace completed,
2557 * then instead of failing with ENOTSUP we may either succeed,
2558 * fail with ENODEV, or fail with EOVERFLOW.
2559 */
2560 if (expected_error == ENOTSUP &&
2561 (error == 0 || error == ENODEV || error == EOVERFLOW))
2562 expected_error = error;
2563
2564 /*
2565 * If someone grew the LUN, the replacement may be too small.
2566 */
b128c09f 2567 if (error == EOVERFLOW || error == EBUSY)
34dc7c2f
BB
2568 expected_error = error;
2569
b128c09f
BB
2570 /* XXX workaround 6690467 */
2571 if (error != expected_error && expected_error != EBUSY) {
2572 fatal(0, "attach (%s %llu, %s %llu, %d) "
2573 "returned %d, expected %d",
2574 oldpath, (longlong_t)oldsize, newpath,
2575 (longlong_t)newsize, replacing, error, expected_error);
34dc7c2f
BB
2576 }
2577
428870ff 2578 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
34dc7c2f
BB
2579}
2580
9babb374
BB
2581/*
2582 * Callback function which expands the physical size of the vdev.
2583 */
2584vdev_t *
2585grow_vdev(vdev_t *vd, void *arg)
2586{
1fde1e37 2587 ASSERTV(spa_t *spa = vd->vdev_spa);
9babb374
BB
2588 size_t *newsize = arg;
2589 size_t fsize;
2590 int fd;
2591
2592 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2593 ASSERT(vd->vdev_ops->vdev_op_leaf);
2594
2595 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2596 return (vd);
2597
2598 fsize = lseek(fd, 0, SEEK_END);
0e5b68e0 2599 VERIFY(ftruncate(fd, *newsize) == 0);
9babb374
BB
2600
2601 if (zopt_verbose >= 6) {
2602 (void) printf("%s grew from %lu to %lu bytes\n",
2603 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2604 }
2605 (void) close(fd);
2606 return (NULL);
2607}
2608
2609/*
2610 * Callback function which expands a given vdev by calling vdev_online().
2611 */
2612/* ARGSUSED */
2613vdev_t *
2614online_vdev(vdev_t *vd, void *arg)
2615{
2616 spa_t *spa = vd->vdev_spa;
2617 vdev_t *tvd = vd->vdev_top;
9babb374 2618 uint64_t guid = vd->vdev_guid;
428870ff
BB
2619 uint64_t generation = spa->spa_config_generation + 1;
2620 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2621 int error;
9babb374
BB
2622
2623 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2624 ASSERT(vd->vdev_ops->vdev_op_leaf);
2625
2626 /* Calling vdev_online will initialize the new metaslabs */
2627 spa_config_exit(spa, SCL_STATE, spa);
428870ff 2628 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
9babb374
BB
2629 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2630
428870ff
BB
2631 /*
2632 * If vdev_online returned an error or the underlying vdev_open
2633 * failed then we abort the expand. The only way to know that
2634 * vdev_open fails is by checking the returned newstate.
2635 */
2636 if (error || newstate != VDEV_STATE_HEALTHY) {
2637 if (zopt_verbose >= 5) {
2638 (void) printf("Unable to expand vdev, state %llu, "
2639 "error %d\n", (u_longlong_t)newstate, error);
2640 }
2641 return (vd);
2642 }
2643 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2644
9babb374
BB
2645 /*
2646 * Since we dropped the lock we need to ensure that we're
2647 * still talking to the original vdev. It's possible this
2648 * vdev may have been detached/replaced while we were
2649 * trying to online it.
2650 */
428870ff
BB
2651 if (generation != spa->spa_config_generation) {
2652 if (zopt_verbose >= 5) {
2653 (void) printf("vdev configuration has changed, "
2654 "guid %llu, state %llu, expected gen %llu, "
2655 "got gen %llu\n",
2656 (u_longlong_t)guid,
2657 (u_longlong_t)tvd->vdev_state,
2658 (u_longlong_t)generation,
2659 (u_longlong_t)spa->spa_config_generation);
9babb374
BB
2660 }
2661 return (vd);
2662 }
2663 return (NULL);
2664}
2665
2666/*
2667 * Traverse the vdev tree calling the supplied function.
2668 * We continue to walk the tree until we either have walked all
2669 * children or we receive a non-NULL return from the callback.
2670 * If a NULL callback is passed, then we just return back the first
2671 * leaf vdev we encounter.
2672 */
2673vdev_t *
2674vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2675{
d6320ddb
BB
2676 uint_t c;
2677
9babb374
BB
2678 if (vd->vdev_ops->vdev_op_leaf) {
2679 if (func == NULL)
2680 return (vd);
2681 else
2682 return (func(vd, arg));
2683 }
2684
d6320ddb 2685 for (c = 0; c < vd->vdev_children; c++) {
9babb374
BB
2686 vdev_t *cvd = vd->vdev_child[c];
2687 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2688 return (cvd);
2689 }
2690 return (NULL);
2691}
2692
34dc7c2f
BB
2693/*
2694 * Verify that dynamic LUN growth works as expected.
2695 */
428870ff 2696/* ARGSUSED */
34dc7c2f 2697void
428870ff 2698ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
34dc7c2f 2699{
428870ff
BB
2700 ztest_shared_t *zs = ztest_shared;
2701 spa_t *spa = zs->zs_spa;
2702 vdev_t *vd, *tvd;
2703 metaslab_class_t *mc;
2704 metaslab_group_t *mg;
9babb374 2705 size_t psize, newsize;
428870ff
BB
2706 uint64_t top;
2707 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
34dc7c2f 2708
428870ff 2709 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
9babb374
BB
2710 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2711
428870ff 2712 top = ztest_random_vdev_top(spa, B_TRUE);
9babb374 2713
428870ff
BB
2714 tvd = spa->spa_root_vdev->vdev_child[top];
2715 mg = tvd->vdev_mg;
2716 mc = mg->mg_class;
2717 old_ms_count = tvd->vdev_ms_count;
2718 old_class_space = metaslab_class_get_space(mc);
34dc7c2f
BB
2719
2720 /*
9babb374
BB
2721 * Determine the size of the first leaf vdev associated with
2722 * our top-level device.
34dc7c2f 2723 */
9babb374
BB
2724 vd = vdev_walk_tree(tvd, NULL, NULL);
2725 ASSERT3P(vd, !=, NULL);
2726 ASSERT(vd->vdev_ops->vdev_op_leaf);
34dc7c2f 2727
9babb374 2728 psize = vd->vdev_psize;
34dc7c2f 2729
9babb374 2730 /*
428870ff
BB
2731 * We only try to expand the vdev if it's healthy, less than 4x its
2732 * original size, and it has a valid psize.
9babb374 2733 */
428870ff
BB
2734 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2735 psize == 0 || psize >= 4 * zopt_vdev_size) {
9babb374 2736 spa_config_exit(spa, SCL_STATE, spa);
428870ff 2737 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
9babb374
BB
2738 return;
2739 }
2740 ASSERT(psize > 0);
2741 newsize = psize + psize / 8;
2742 ASSERT3U(newsize, >, psize);
34dc7c2f 2743
9babb374 2744 if (zopt_verbose >= 6) {
428870ff 2745 (void) printf("Expanding LUN %s from %lu to %lu\n",
9babb374
BB
2746 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2747 }
2748
9babb374
BB
2749 /*
2750 * Growing the vdev is a two step process:
2751 * 1). expand the physical size (i.e. relabel)
2752 * 2). online the vdev to create the new metaslabs
2753 */
2754 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2755 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2756 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2757 if (zopt_verbose >= 5) {
2758 (void) printf("Could not expand LUN because "
428870ff 2759 "the vdev configuration changed.\n");
34dc7c2f 2760 }
428870ff
BB
2761 spa_config_exit(spa, SCL_STATE, spa);
2762 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
9babb374 2763 return;
34dc7c2f
BB
2764 }
2765
428870ff 2766 spa_config_exit(spa, SCL_STATE, spa);
9babb374
BB
2767
2768 /*
2769 * Expanding the LUN will update the config asynchronously,
2770 * thus we must wait for the async thread to complete any
2771 * pending tasks before proceeding.
2772 */
428870ff
BB
2773 for (;;) {
2774 boolean_t done;
2775 mutex_enter(&spa->spa_async_lock);
2776 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2777 mutex_exit(&spa->spa_async_lock);
2778 if (done)
2779 break;
2780 txg_wait_synced(spa_get_dsl(spa), 0);
2781 (void) poll(NULL, 0, 100);
2782 }
9babb374
BB
2783
2784 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
428870ff
BB
2785
2786 tvd = spa->spa_root_vdev->vdev_child[top];
2787 new_ms_count = tvd->vdev_ms_count;
2788 new_class_space = metaslab_class_get_space(mc);
2789
2790 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2791 if (zopt_verbose >= 5) {
2792 (void) printf("Could not verify LUN expansion due to "
2793 "intervening vdev offline or remove.\n");
2794 }
2795 spa_config_exit(spa, SCL_STATE, spa);
2796 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2797 return;
2798 }
2799
2800 /*
2801 * Make sure we were able to grow the vdev.
2802 */
2803 if (new_ms_count <= old_ms_count)
2804 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2805 old_ms_count, new_ms_count);
9babb374
BB
2806
2807 /*
2808 * Make sure we were able to grow the pool.
2809 */
428870ff
BB
2810 if (new_class_space <= old_class_space)
2811 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2812 old_class_space, new_class_space);
2813
2814 if (zopt_verbose >= 5) {
9babb374
BB
2815 char oldnumbuf[6], newnumbuf[6];
2816
428870ff
BB
2817 nicenum(old_class_space, oldnumbuf);
2818 nicenum(new_class_space, newnumbuf);
9babb374
BB
2819 (void) printf("%s grew from %s to %s\n",
2820 spa->spa_name, oldnumbuf, newnumbuf);
2821 }
428870ff 2822
9babb374 2823 spa_config_exit(spa, SCL_STATE, spa);
428870ff 2824 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
34dc7c2f
BB
2825}
2826
428870ff
BB
2827/*
2828 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2829 */
34dc7c2f
BB
2830/* ARGSUSED */
2831static void
428870ff 2832ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
34dc7c2f
BB
2833{
2834 /*
428870ff 2835 * Create the objects common to all ztest datasets.
34dc7c2f 2836 */
428870ff 2837 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
34dc7c2f 2838 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
428870ff 2839}
34dc7c2f 2840
428870ff
BB
2841static int
2842ztest_dataset_create(char *dsname)
2843{
2844 uint64_t zilset = ztest_random(100);
2845 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2846 ztest_objset_create_cb, NULL);
2847
2848 if (err || zilset < 80)
2849 return (err);
2850
2851 (void) printf("Setting dataset %s to sync always\n", dsname);
2852 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2853 ZFS_SYNC_ALWAYS, B_FALSE));
34dc7c2f
BB
2854}
2855
428870ff 2856/* ARGSUSED */
34dc7c2f 2857static int
428870ff 2858ztest_objset_destroy_cb(const char *name, void *arg)
34dc7c2f 2859{
34dc7c2f 2860 objset_t *os;
428870ff 2861 dmu_object_info_t doi;
34dc7c2f
BB
2862 int error;
2863
2864 /*
2865 * Verify that the dataset contains a directory object.
2866 */
428870ff
BB
2867 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2868 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
34dc7c2f
BB
2869 if (error != ENOENT) {
2870 /* We could have crashed in the middle of destroying it */
2871 ASSERT3U(error, ==, 0);
428870ff
BB
2872 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2873 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
34dc7c2f 2874 }
428870ff 2875 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
2876
2877 /*
2878 * Destroy the dataset.
2879 */
428870ff 2880 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
34dc7c2f
BB
2881 return (0);
2882}
2883
428870ff
BB
2884static boolean_t
2885ztest_snapshot_create(char *osname, uint64_t id)
34dc7c2f 2886{
428870ff
BB
2887 char snapname[MAXNAMELEN];
2888 int error;
2889
2890 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2891 (u_longlong_t)id);
2892
2893 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
572e2857 2894 NULL, NULL, B_FALSE, B_FALSE, -1);
428870ff
BB
2895 if (error == ENOSPC) {
2896 ztest_record_enospc(FTAG);
2897 return (B_FALSE);
2898 }
2899 if (error != 0 && error != EEXIST)
2900 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2901 return (B_TRUE);
2902}
2903
2904static boolean_t
2905ztest_snapshot_destroy(char *osname, uint64_t id)
2906{
2907 char snapname[MAXNAMELEN];
2908 int error;
2909
2910 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2911 (u_longlong_t)id);
2912
2913 error = dmu_objset_destroy(snapname, B_FALSE);
2914 if (error != 0 && error != ENOENT)
2915 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2916 return (B_TRUE);
34dc7c2f
BB
2917}
2918
428870ff 2919/* ARGSUSED */
34dc7c2f 2920void
428870ff 2921ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 2922{
428870ff
BB
2923 ztest_shared_t *zs = ztest_shared;
2924 ztest_ds_t zdtmp;
2925 int iters;
34dc7c2f 2926 int error;
b128c09f 2927 objset_t *os, *os2;
428870ff 2928 char name[MAXNAMELEN];
34dc7c2f 2929 zilog_t *zilog;
d6320ddb 2930 int i;
34dc7c2f 2931
428870ff 2932 (void) rw_rdlock(&zs->zs_name_lock);
34dc7c2f 2933
428870ff
BB
2934 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2935 zs->zs_pool, (u_longlong_t)id);
34dc7c2f
BB
2936
2937 /*
2938 * If this dataset exists from a previous run, process its replay log
2939 * half of the time. If we don't replay it, then dmu_objset_destroy()
428870ff 2940 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
34dc7c2f
BB
2941 */
2942 if (ztest_random(2) == 0 &&
428870ff
BB
2943 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2944 ztest_zd_init(&zdtmp, os);
2945 zil_replay(os, &zdtmp, ztest_replay_vector);
2946 ztest_zd_fini(&zdtmp);
2947 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
2948 }
2949
2950 /*
2951 * There may be an old instance of the dataset we're about to
2952 * create lying around from a previous run. If so, destroy it
2953 * and all of its snapshots.
2954 */
428870ff 2955 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
34dc7c2f
BB
2956 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2957
2958 /*
2959 * Verify that the destroyed dataset is no longer in the namespace.
2960 */
428870ff 2961 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
34dc7c2f
BB
2962
2963 /*
2964 * Verify that we can create a new dataset.
2965 */
428870ff 2966 error = ztest_dataset_create(name);
34dc7c2f
BB
2967 if (error) {
2968 if (error == ENOSPC) {
428870ff
BB
2969 ztest_record_enospc(FTAG);
2970 (void) rw_unlock(&zs->zs_name_lock);
34dc7c2f
BB
2971 return;
2972 }
2973 fatal(0, "dmu_objset_create(%s) = %d", name, error);
2974 }
2975
428870ff
BB
2976 VERIFY3U(0, ==,
2977 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
2978
2979 ztest_zd_init(&zdtmp, os);
34dc7c2f
BB
2980
2981 /*
2982 * Open the intent log for it.
2983 */
428870ff 2984 zilog = zil_open(os, ztest_get_data);
34dc7c2f
BB
2985
2986 /*
428870ff
BB
2987 * Put some objects in there, do a little I/O to them,
2988 * and randomly take a couple of snapshots along the way.
34dc7c2f 2989 */
428870ff 2990 iters = ztest_random(5);
d6320ddb 2991 for (i = 0; i < iters; i++) {
428870ff
BB
2992 ztest_dmu_object_alloc_free(&zdtmp, id);
2993 if (ztest_random(iters) == 0)
2994 (void) ztest_snapshot_create(name, i);
34dc7c2f
BB
2995 }
2996
2997 /*
2998 * Verify that we cannot create an existing dataset.
2999 */
428870ff
BB
3000 VERIFY3U(EEXIST, ==,
3001 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
34dc7c2f
BB
3002
3003 /*
428870ff 3004 * Verify that we can hold an objset that is also owned.
b128c09f 3005 */
428870ff
BB
3006 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3007 dmu_objset_rele(os2, FTAG);
34dc7c2f 3008
428870ff
BB
3009 /*
3010 * Verify that we cannot own an objset that is already owned.
3011 */
3012 VERIFY3U(EBUSY, ==,
3013 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
34dc7c2f 3014
428870ff
BB
3015 zil_close(zilog);
3016 dmu_objset_disown(os, FTAG);
3017 ztest_zd_fini(&zdtmp);
34dc7c2f 3018
428870ff 3019 (void) rw_unlock(&zs->zs_name_lock);
34dc7c2f
BB
3020}
3021
3022/*
3023 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3024 */
3025void
428870ff 3026ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3027{
428870ff 3028 ztest_shared_t *zs = ztest_shared;
34dc7c2f 3029
428870ff
BB
3030 (void) rw_rdlock(&zs->zs_name_lock);
3031 (void) ztest_snapshot_destroy(zd->zd_name, id);
3032 (void) ztest_snapshot_create(zd->zd_name, id);
3033 (void) rw_unlock(&zs->zs_name_lock);
34dc7c2f
BB
3034}
3035
9babb374
BB
3036/*
3037 * Cleanup non-standard snapshots and clones.
3038 */
3039void
428870ff 3040ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
9babb374 3041{
428870ff
BB
3042 char snap1name[MAXNAMELEN];
3043 char clone1name[MAXNAMELEN];
3044 char snap2name[MAXNAMELEN];
3045 char clone2name[MAXNAMELEN];
3046 char snap3name[MAXNAMELEN];
9babb374
BB
3047 int error;
3048
b8864a23
BB
3049 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3050 osname, (u_longlong_t)id);
3051 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3052 osname, (u_longlong_t)id);
3053 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3054 clone1name, (u_longlong_t)id);
3055 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3056 osname, (u_longlong_t)id);
3057 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3058 clone1name, (u_longlong_t)id);
9babb374 3059
45d1cae3 3060 error = dmu_objset_destroy(clone2name, B_FALSE);
9babb374
BB
3061 if (error && error != ENOENT)
3062 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
45d1cae3 3063 error = dmu_objset_destroy(snap3name, B_FALSE);
9babb374
BB
3064 if (error && error != ENOENT)
3065 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
45d1cae3 3066 error = dmu_objset_destroy(snap2name, B_FALSE);
9babb374
BB
3067 if (error && error != ENOENT)
3068 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
45d1cae3 3069 error = dmu_objset_destroy(clone1name, B_FALSE);
9babb374
BB
3070 if (error && error != ENOENT)
3071 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
45d1cae3 3072 error = dmu_objset_destroy(snap1name, B_FALSE);
9babb374
BB
3073 if (error && error != ENOENT)
3074 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3075}
3076
3077/*
3078 * Verify dsl_dataset_promote handles EBUSY
3079 */
3080void
428870ff 3081ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
9babb374 3082{
428870ff 3083 ztest_shared_t *zs = ztest_shared;
9babb374
BB
3084 objset_t *clone;
3085 dsl_dataset_t *ds;
428870ff
BB
3086 char snap1name[MAXNAMELEN];
3087 char clone1name[MAXNAMELEN];
3088 char snap2name[MAXNAMELEN];
3089 char clone2name[MAXNAMELEN];
3090 char snap3name[MAXNAMELEN];
3091 char *osname = zd->zd_name;
3092 int error;
9babb374 3093
428870ff 3094 (void) rw_rdlock(&zs->zs_name_lock);
9babb374 3095
428870ff 3096 ztest_dsl_dataset_cleanup(osname, id);
9babb374 3097
b8864a23
BB
3098 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3099 osname, (u_longlong_t)id);
3100 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3101 osname, (u_longlong_t)id);
3102 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3103 clone1name, (u_longlong_t)id);
3104 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3105 osname, (u_longlong_t)id);
3106 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3107 clone1name, (u_longlong_t)id);
9babb374
BB
3108
3109 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
572e2857 3110 NULL, NULL, B_FALSE, B_FALSE, -1);
9babb374
BB
3111 if (error && error != EEXIST) {
3112 if (error == ENOSPC) {
428870ff 3113 ztest_record_enospc(FTAG);
9babb374
BB
3114 goto out;
3115 }
3116 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3117 }
3118
428870ff 3119 error = dmu_objset_hold(snap1name, FTAG, &clone);
9babb374
BB
3120 if (error)
3121 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3122
428870ff
BB
3123 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3124 dmu_objset_rele(clone, FTAG);
9babb374
BB
3125 if (error) {
3126 if (error == ENOSPC) {
428870ff 3127 ztest_record_enospc(FTAG);
9babb374
BB
3128 goto out;
3129 }
3130 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3131 }
3132
428870ff 3133 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
572e2857 3134 NULL, NULL, B_FALSE, B_FALSE, -1);
428870ff
BB
3135 if (error && error != EEXIST) {
3136 if (error == ENOSPC) {
3137 ztest_record_enospc(FTAG);
3138 goto out;
34dc7c2f 3139 }
428870ff
BB
3140 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3141 }
34dc7c2f 3142
428870ff 3143 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
572e2857 3144 NULL, NULL, B_FALSE, B_FALSE, -1);
428870ff
BB
3145 if (error && error != EEXIST) {
3146 if (error == ENOSPC) {
3147 ztest_record_enospc(FTAG);
3148 goto out;
3149 }
3150 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3151 }
34dc7c2f 3152
428870ff
BB
3153 error = dmu_objset_hold(snap3name, FTAG, &clone);
3154 if (error)
3155 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
34dc7c2f 3156
428870ff
BB
3157 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3158 dmu_objset_rele(clone, FTAG);
3159 if (error) {
3160 if (error == ENOSPC) {
3161 ztest_record_enospc(FTAG);
3162 goto out;
3163 }
3164 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3165 }
34dc7c2f 3166
428870ff
BB
3167 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3168 if (error)
3169 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3170 error = dsl_dataset_promote(clone2name, NULL);
3171 if (error != EBUSY)
3172 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3173 error);
3174 dsl_dataset_disown(ds, FTAG);
34dc7c2f 3175
428870ff
BB
3176out:
3177 ztest_dsl_dataset_cleanup(osname, id);
34dc7c2f 3178
428870ff
BB
3179 (void) rw_unlock(&zs->zs_name_lock);
3180}
34dc7c2f 3181
428870ff
BB
3182/*
3183 * Verify that dmu_object_{alloc,free} work as expected.
3184 */
3185void
3186ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3187{
3188 ztest_od_t od[4];
3189 int batchsize = sizeof (od) / sizeof (od[0]);
d6320ddb 3190 int b;
34dc7c2f 3191
d6320ddb 3192 for (b = 0; b < batchsize; b++)
428870ff 3193 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
34dc7c2f 3194
428870ff
BB
3195 /*
3196 * Destroy the previous batch of objects, create a new batch,
3197 * and do some I/O on the new objects.
3198 */
3199 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3200 return;
34dc7c2f 3201
428870ff
BB
3202 while (ztest_random(4 * batchsize) != 0)
3203 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3204 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
34dc7c2f
BB
3205}
3206
3207/*
3208 * Verify that dmu_{read,write} work as expected.
3209 */
34dc7c2f 3210void
428870ff 3211ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3212{
428870ff
BB
3213 objset_t *os = zd->zd_os;
3214 ztest_od_t od[2];
34dc7c2f
BB
3215 dmu_tx_t *tx;
3216 int i, freeit, error;
3217 uint64_t n, s, txg;
3218 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
428870ff
BB
3219 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3220 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
34dc7c2f
BB
3221 uint64_t regions = 997;
3222 uint64_t stride = 123456789ULL;
3223 uint64_t width = 40;
3224 int free_percent = 5;
3225
3226 /*
3227 * This test uses two objects, packobj and bigobj, that are always
3228 * updated together (i.e. in the same tx) so that their contents are
3229 * in sync and can be compared. Their contents relate to each other
3230 * in a simple way: packobj is a dense array of 'bufwad' structures,
3231 * while bigobj is a sparse array of the same bufwads. Specifically,
3232 * for any index n, there are three bufwads that should be identical:
3233 *
3234 * packobj, at offset n * sizeof (bufwad_t)
3235 * bigobj, at the head of the nth chunk
3236 * bigobj, at the tail of the nth chunk
3237 *
3238 * The chunk size is arbitrary. It doesn't have to be a power of two,
3239 * and it doesn't have any relation to the object blocksize.
3240 * The only requirement is that it can hold at least two bufwads.
3241 *
3242 * Normally, we write the bufwad to each of these locations.
3243 * However, free_percent of the time we instead write zeroes to
3244 * packobj and perform a dmu_free_range() on bigobj. By comparing
3245 * bigobj to packobj, we can verify that the DMU is correctly
3246 * tracking which parts of an object are allocated and free,
3247 * and that the contents of the allocated blocks are correct.
3248 */
3249
3250 /*
3251 * Read the directory info. If it's the first time, set things up.
3252 */
428870ff
BB
3253 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3254 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
34dc7c2f 3255
428870ff
BB
3256 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3257 return;
34dc7c2f 3258
428870ff
BB
3259 bigobj = od[0].od_object;
3260 packobj = od[1].od_object;
3261 chunksize = od[0].od_gen;
3262 ASSERT(chunksize == od[1].od_gen);
34dc7c2f
BB
3263
3264 /*
3265 * Prefetch a random chunk of the big object.
3266 * Our aim here is to get some async reads in flight
3267 * for blocks that we may free below; the DMU should
3268 * handle this race correctly.
3269 */
3270 n = ztest_random(regions) * stride + ztest_random(width);
3271 s = 1 + ztest_random(2 * width - 1);
428870ff 3272 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
34dc7c2f
BB
3273
3274 /*
3275 * Pick a random index and compute the offsets into packobj and bigobj.
3276 */
3277 n = ztest_random(regions) * stride + ztest_random(width);
3278 s = 1 + ztest_random(width - 1);
3279
3280 packoff = n * sizeof (bufwad_t);
3281 packsize = s * sizeof (bufwad_t);
3282
428870ff
BB
3283 bigoff = n * chunksize;
3284 bigsize = s * chunksize;
34dc7c2f
BB
3285
3286 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3287 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3288
3289 /*
3290 * free_percent of the time, free a range of bigobj rather than
3291 * overwriting it.
3292 */
3293 freeit = (ztest_random(100) < free_percent);
3294
3295 /*
3296 * Read the current contents of our objects.
3297 */
428870ff 3298 error = dmu_read(os, packobj, packoff, packsize, packbuf,
9babb374 3299 DMU_READ_PREFETCH);
34dc7c2f 3300 ASSERT3U(error, ==, 0);
428870ff 3301 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
9babb374 3302 DMU_READ_PREFETCH);
34dc7c2f
BB
3303 ASSERT3U(error, ==, 0);
3304
3305 /*
3306 * Get a tx for the mods to both packobj and bigobj.
3307 */
3308 tx = dmu_tx_create(os);
3309
428870ff 3310 dmu_tx_hold_write(tx, packobj, packoff, packsize);
34dc7c2f
BB
3311
3312 if (freeit)
428870ff 3313 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
34dc7c2f 3314 else
428870ff 3315 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
34dc7c2f 3316
428870ff
BB
3317 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3318 if (txg == 0) {
34dc7c2f
BB
3319 umem_free(packbuf, packsize);
3320 umem_free(bigbuf, bigsize);
3321 return;
3322 }
3323
428870ff
BB
3324 dmu_object_set_checksum(os, bigobj,
3325 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3326
3327 dmu_object_set_compress(os, bigobj,
3328 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
34dc7c2f
BB
3329
3330 /*
3331 * For each index from n to n + s, verify that the existing bufwad
3332 * in packobj matches the bufwads at the head and tail of the
3333 * corresponding chunk in bigobj. Then update all three bufwads
3334 * with the new values we want to write out.
3335 */
3336 for (i = 0; i < s; i++) {
3337 /* LINTED */
3338 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3339 /* LINTED */
428870ff 3340 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34dc7c2f 3341 /* LINTED */
428870ff 3342 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34dc7c2f
BB
3343
3344 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3345 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3346
3347 if (pack->bw_txg > txg)
3348 fatal(0, "future leak: got %llx, open txg is %llx",
3349 pack->bw_txg, txg);
3350
3351 if (pack->bw_data != 0 && pack->bw_index != n + i)
3352 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3353 pack->bw_index, n, i);
3354
3355 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3356 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3357
3358 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3359 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3360
3361 if (freeit) {
3362 bzero(pack, sizeof (bufwad_t));
3363 } else {
3364 pack->bw_index = n + i;
3365 pack->bw_txg = txg;
3366 pack->bw_data = 1 + ztest_random(-2ULL);
3367 }
3368 *bigH = *pack;
3369 *bigT = *pack;
3370 }
3371
3372 /*
3373 * We've verified all the old bufwads, and made new ones.
3374 * Now write them out.
3375 */
428870ff 3376 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
34dc7c2f
BB
3377
3378 if (freeit) {
428870ff 3379 if (zopt_verbose >= 7) {
34dc7c2f
BB
3380 (void) printf("freeing offset %llx size %llx"
3381 " txg %llx\n",
3382 (u_longlong_t)bigoff,
3383 (u_longlong_t)bigsize,
3384 (u_longlong_t)txg);
3385 }
428870ff 3386 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
34dc7c2f 3387 } else {
428870ff 3388 if (zopt_verbose >= 7) {
34dc7c2f
BB
3389 (void) printf("writing offset %llx size %llx"
3390 " txg %llx\n",
3391 (u_longlong_t)bigoff,
3392 (u_longlong_t)bigsize,
3393 (u_longlong_t)txg);
3394 }
428870ff 3395 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
34dc7c2f
BB
3396 }
3397
3398 dmu_tx_commit(tx);
3399
3400 /*
3401 * Sanity check the stuff we just wrote.
3402 */
3403 {
3404 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3405 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3406
428870ff 3407 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 3408 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 3409 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374 3410 bigsize, bigcheck, DMU_READ_PREFETCH));
34dc7c2f
BB
3411
3412 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3413 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3414
3415 umem_free(packcheck, packsize);
3416 umem_free(bigcheck, bigsize);
3417 }
3418
3419 umem_free(packbuf, packsize);
3420 umem_free(bigbuf, bigsize);
3421}
3422
9babb374
BB
3423void
3424compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
428870ff 3425 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
9babb374
BB
3426{
3427 uint64_t i;
3428 bufwad_t *pack;
3429 bufwad_t *bigH;
3430 bufwad_t *bigT;
3431
3432 /*
3433 * For each index from n to n + s, verify that the existing bufwad
3434 * in packobj matches the bufwads at the head and tail of the
3435 * corresponding chunk in bigobj. Then update all three bufwads
3436 * with the new values we want to write out.
3437 */
3438 for (i = 0; i < s; i++) {
3439 /* LINTED */
3440 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3441 /* LINTED */
428870ff 3442 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
9babb374 3443 /* LINTED */
428870ff 3444 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
9babb374
BB
3445
3446 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3447 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3448
3449 if (pack->bw_txg > txg)
3450 fatal(0, "future leak: got %llx, open txg is %llx",
3451 pack->bw_txg, txg);
3452
3453 if (pack->bw_data != 0 && pack->bw_index != n + i)
3454 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3455 pack->bw_index, n, i);
3456
3457 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3458 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3459
3460 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3461 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3462
3463 pack->bw_index = n + i;
3464 pack->bw_txg = txg;
3465 pack->bw_data = 1 + ztest_random(-2ULL);
3466
3467 *bigH = *pack;
3468 *bigT = *pack;
3469 }
3470}
3471
3472void
428870ff 3473ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
9babb374 3474{
428870ff
BB
3475 objset_t *os = zd->zd_os;
3476 ztest_od_t od[2];
9babb374
BB
3477 dmu_tx_t *tx;
3478 uint64_t i;
3479 int error;
3480 uint64_t n, s, txg;
3481 bufwad_t *packbuf, *bigbuf;
428870ff
BB
3482 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3483 uint64_t blocksize = ztest_random_blocksize();
3484 uint64_t chunksize = blocksize;
9babb374
BB
3485 uint64_t regions = 997;
3486 uint64_t stride = 123456789ULL;
3487 uint64_t width = 9;
3488 dmu_buf_t *bonus_db;
3489 arc_buf_t **bigbuf_arcbufs;
428870ff 3490 dmu_object_info_t doi;
9babb374
BB
3491
3492 /*
3493 * This test uses two objects, packobj and bigobj, that are always
3494 * updated together (i.e. in the same tx) so that their contents are
3495 * in sync and can be compared. Their contents relate to each other
3496 * in a simple way: packobj is a dense array of 'bufwad' structures,
3497 * while bigobj is a sparse array of the same bufwads. Specifically,
3498 * for any index n, there are three bufwads that should be identical:
3499 *
3500 * packobj, at offset n * sizeof (bufwad_t)
3501 * bigobj, at the head of the nth chunk
3502 * bigobj, at the tail of the nth chunk
3503 *
3504 * The chunk size is set equal to bigobj block size so that
3505 * dmu_assign_arcbuf() can be tested for object updates.
3506 */
3507
3508 /*
3509 * Read the directory info. If it's the first time, set things up.
3510 */
428870ff
BB
3511 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3512 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
9babb374 3513
428870ff
BB
3514 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3515 return;
9babb374 3516
428870ff
BB
3517 bigobj = od[0].od_object;
3518 packobj = od[1].od_object;
3519 blocksize = od[0].od_blocksize;
3520 chunksize = blocksize;
3521 ASSERT(chunksize == od[1].od_gen);
9babb374 3522
428870ff
BB
3523 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3524 VERIFY(ISP2(doi.doi_data_block_size));
3525 VERIFY(chunksize == doi.doi_data_block_size);
3526 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
9babb374
BB
3527
3528 /*
3529 * Pick a random index and compute the offsets into packobj and bigobj.
3530 */
3531 n = ztest_random(regions) * stride + ztest_random(width);
3532 s = 1 + ztest_random(width - 1);
3533
3534 packoff = n * sizeof (bufwad_t);
3535 packsize = s * sizeof (bufwad_t);
3536
428870ff
BB
3537 bigoff = n * chunksize;
3538 bigsize = s * chunksize;
9babb374
BB
3539
3540 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3541 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3542
428870ff 3543 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
9babb374
BB
3544
3545 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3546
3547 /*
3548 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3549 * Iteration 1 test zcopy to already referenced dbufs.
3550 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3551 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3552 * Iteration 4 test zcopy when dbuf is no longer dirty.
3553 * Iteration 5 test zcopy when it can't be done.
3554 * Iteration 6 one more zcopy write.
3555 */
3556 for (i = 0; i < 7; i++) {
3557 uint64_t j;
3558 uint64_t off;
3559
3560 /*
3561 * In iteration 5 (i == 5) use arcbufs
3562 * that don't match bigobj blksz to test
3563 * dmu_assign_arcbuf() when it can't directly
3564 * assign an arcbuf to a dbuf.
3565 */
3566 for (j = 0; j < s; j++) {
3567 if (i != 5) {
3568 bigbuf_arcbufs[j] =
428870ff 3569 dmu_request_arcbuf(bonus_db, chunksize);
9babb374
BB
3570 } else {
3571 bigbuf_arcbufs[2 * j] =
428870ff 3572 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374 3573 bigbuf_arcbufs[2 * j + 1] =
428870ff 3574 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374
BB
3575 }
3576 }
3577
3578 /*
3579 * Get a tx for the mods to both packobj and bigobj.
3580 */
3581 tx = dmu_tx_create(os);
3582
428870ff
BB
3583 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3584 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
9babb374 3585
428870ff
BB
3586 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3587 if (txg == 0) {
9babb374
BB
3588 umem_free(packbuf, packsize);
3589 umem_free(bigbuf, bigsize);
3590 for (j = 0; j < s; j++) {
3591 if (i != 5) {
3592 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3593 } else {
3594 dmu_return_arcbuf(
3595 bigbuf_arcbufs[2 * j]);
3596 dmu_return_arcbuf(
3597 bigbuf_arcbufs[2 * j + 1]);
3598 }
3599 }
3600 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3601 dmu_buf_rele(bonus_db, FTAG);
3602 return;
3603 }
3604
9babb374
BB
3605 /*
3606 * 50% of the time don't read objects in the 1st iteration to
3607 * test dmu_assign_arcbuf() for the case when there're no
3608 * existing dbufs for the specified offsets.
3609 */
3610 if (i != 0 || ztest_random(2) != 0) {
428870ff 3611 error = dmu_read(os, packobj, packoff,
9babb374
BB
3612 packsize, packbuf, DMU_READ_PREFETCH);
3613 ASSERT3U(error, ==, 0);
428870ff 3614 error = dmu_read(os, bigobj, bigoff, bigsize,
9babb374
BB
3615 bigbuf, DMU_READ_PREFETCH);
3616 ASSERT3U(error, ==, 0);
3617 }
3618 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
428870ff 3619 n, chunksize, txg);
9babb374
BB
3620
3621 /*
3622 * We've verified all the old bufwads, and made new ones.
3623 * Now write them out.
3624 */
428870ff
BB
3625 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3626 if (zopt_verbose >= 7) {
9babb374
BB
3627 (void) printf("writing offset %llx size %llx"
3628 " txg %llx\n",
3629 (u_longlong_t)bigoff,
3630 (u_longlong_t)bigsize,
3631 (u_longlong_t)txg);
3632 }
428870ff 3633 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
9babb374
BB
3634 dmu_buf_t *dbt;
3635 if (i != 5) {
3636 bcopy((caddr_t)bigbuf + (off - bigoff),
428870ff 3637 bigbuf_arcbufs[j]->b_data, chunksize);
9babb374
BB
3638 } else {
3639 bcopy((caddr_t)bigbuf + (off - bigoff),
3640 bigbuf_arcbufs[2 * j]->b_data,
428870ff 3641 chunksize / 2);
9babb374 3642 bcopy((caddr_t)bigbuf + (off - bigoff) +
428870ff 3643 chunksize / 2,
9babb374 3644 bigbuf_arcbufs[2 * j + 1]->b_data,
428870ff 3645 chunksize / 2);
9babb374
BB
3646 }
3647
3648 if (i == 1) {
428870ff
BB
3649 VERIFY(dmu_buf_hold(os, bigobj, off,
3650 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
9babb374
BB
3651 }
3652 if (i != 5) {
3653 dmu_assign_arcbuf(bonus_db, off,
3654 bigbuf_arcbufs[j], tx);
3655 } else {
3656 dmu_assign_arcbuf(bonus_db, off,
3657 bigbuf_arcbufs[2 * j], tx);
3658 dmu_assign_arcbuf(bonus_db,
428870ff 3659 off + chunksize / 2,
9babb374
BB
3660 bigbuf_arcbufs[2 * j + 1], tx);
3661 }
3662 if (i == 1) {
3663 dmu_buf_rele(dbt, FTAG);
3664 }
3665 }
3666 dmu_tx_commit(tx);
3667
3668 /*
3669 * Sanity check the stuff we just wrote.
3670 */
3671 {
3672 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3673 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3674
428870ff 3675 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 3676 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 3677 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374
BB
3678 bigsize, bigcheck, DMU_READ_PREFETCH));
3679
3680 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3681 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3682
3683 umem_free(packcheck, packsize);
3684 umem_free(bigcheck, bigsize);
3685 }
3686 if (i == 2) {
3687 txg_wait_open(dmu_objset_pool(os), 0);
3688 } else if (i == 3) {
3689 txg_wait_synced(dmu_objset_pool(os), 0);
3690 }
3691 }
3692
3693 dmu_buf_rele(bonus_db, FTAG);
3694 umem_free(packbuf, packsize);
3695 umem_free(bigbuf, bigsize);
3696 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3697}
3698
428870ff 3699/* ARGSUSED */
34dc7c2f 3700void
428870ff 3701ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3702{
428870ff
BB
3703 ztest_od_t od[1];
3704 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3705 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
34dc7c2f
BB
3706
3707 /*
428870ff
BB
3708 * Have multiple threads write to large offsets in an object
3709 * to verify that parallel writes to an object -- even to the
3710 * same blocks within the object -- doesn't cause any trouble.
34dc7c2f 3711 */
428870ff 3712 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
9babb374 3713
428870ff 3714 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
34dc7c2f 3715 return;
34dc7c2f 3716
428870ff
BB
3717 while (ztest_random(10) != 0)
3718 ztest_io(zd, od[0].od_object, offset);
3719}
34dc7c2f 3720
428870ff
BB
3721void
3722ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3723{
3724 ztest_od_t od[1];
3725 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3726 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3727 uint64_t count = ztest_random(20) + 1;
3728 uint64_t blocksize = ztest_random_blocksize();
3729 void *data;
34dc7c2f 3730
428870ff 3731 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
34dc7c2f 3732
428870ff 3733 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
34dc7c2f
BB
3734 return;
3735
428870ff 3736 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
34dc7c2f
BB
3737 return;
3738
428870ff 3739 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
34dc7c2f 3740
428870ff 3741 data = umem_zalloc(blocksize, UMEM_NOFAIL);
34dc7c2f 3742
428870ff
BB
3743 while (ztest_random(count) != 0) {
3744 uint64_t randoff = offset + (ztest_random(count) * blocksize);
3745 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
3746 data) != 0)
3747 break;
3748 while (ztest_random(4) != 0)
3749 ztest_io(zd, od[0].od_object, randoff);
9babb374 3750 }
34dc7c2f 3751
428870ff 3752 umem_free(data, blocksize);
34dc7c2f
BB
3753}
3754
3755/*
3756 * Verify that zap_{create,destroy,add,remove,update} work as expected.
3757 */
3758#define ZTEST_ZAP_MIN_INTS 1
3759#define ZTEST_ZAP_MAX_INTS 4
3760#define ZTEST_ZAP_MAX_PROPS 1000
3761
3762void
428870ff 3763ztest_zap(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3764{
428870ff
BB
3765 objset_t *os = zd->zd_os;
3766 ztest_od_t od[1];
34dc7c2f
BB
3767 uint64_t object;
3768 uint64_t txg, last_txg;
3769 uint64_t value[ZTEST_ZAP_MAX_INTS];
3770 uint64_t zl_ints, zl_intsize, prop;
3771 int i, ints;
3772 dmu_tx_t *tx;
3773 char propname[100], txgname[100];
3774 int error;
34dc7c2f
BB
3775 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3776
428870ff 3777 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
34dc7c2f 3778
428870ff
BB
3779 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3780 return;
34dc7c2f 3781
428870ff 3782 object = od[0].od_object;
34dc7c2f 3783
428870ff
BB
3784 /*
3785 * Generate a known hash collision, and verify that
3786 * we can lookup and remove both entries.
3787 */
3788 tx = dmu_tx_create(os);
3789 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3790 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3791 if (txg == 0)
3792 return;
3793 for (i = 0; i < 2; i++) {
3794 value[i] = i;
3795 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3796 1, &value[i], tx));
3797 }
3798 for (i = 0; i < 2; i++) {
3799 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3800 sizeof (uint64_t), 1, &value[i], tx));
3801 VERIFY3U(0, ==,
3802 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3803 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3804 ASSERT3U(zl_ints, ==, 1);
3805 }
3806 for (i = 0; i < 2; i++) {
3807 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
34dc7c2f 3808 }
428870ff 3809 dmu_tx_commit(tx);
34dc7c2f 3810
428870ff
BB
3811 /*
3812 * Generate a buch of random entries.
3813 */
34dc7c2f
BB
3814 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3815
3816 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3817 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3818 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3819 bzero(value, sizeof (value));
3820 last_txg = 0;
3821
3822 /*
3823 * If these zap entries already exist, validate their contents.
3824 */
3825 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3826 if (error == 0) {
3827 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3828 ASSERT3U(zl_ints, ==, 1);
3829
3830 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3831 zl_ints, &last_txg) == 0);
3832
3833 VERIFY(zap_length(os, object, propname, &zl_intsize,
3834 &zl_ints) == 0);
3835
3836 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3837 ASSERT3U(zl_ints, ==, ints);
3838
3839 VERIFY(zap_lookup(os, object, propname, zl_intsize,
3840 zl_ints, value) == 0);
3841
3842 for (i = 0; i < ints; i++) {
3843 ASSERT3U(value[i], ==, last_txg + object + i);
3844 }
3845 } else {
3846 ASSERT3U(error, ==, ENOENT);
3847 }
3848
3849 /*
3850 * Atomically update two entries in our zap object.
3851 * The first is named txg_%llu, and contains the txg
3852 * in which the property was last updated. The second
3853 * is named prop_%llu, and the nth element of its value
3854 * should be txg + object + n.
3855 */
3856 tx = dmu_tx_create(os);
428870ff
BB
3857 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3858 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3859 if (txg == 0)
34dc7c2f 3860 return;
34dc7c2f
BB
3861
3862 if (last_txg > txg)
3863 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
3864
3865 for (i = 0; i < ints; i++)
3866 value[i] = txg + object + i;
3867
428870ff
BB
3868 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3869 1, &txg, tx));
3870 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3871 ints, value, tx));
34dc7c2f
BB
3872
3873 dmu_tx_commit(tx);
3874
3875 /*
3876 * Remove a random pair of entries.
3877 */
3878 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3879 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3880 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3881
3882 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3883
3884 if (error == ENOENT)
3885 return;
3886
3887 ASSERT3U(error, ==, 0);
3888
3889 tx = dmu_tx_create(os);
428870ff
BB
3890 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3891 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3892 if (txg == 0)
34dc7c2f 3893 return;
428870ff
BB
3894 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
3895 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3896 dmu_tx_commit(tx);
3897}
34dc7c2f 3898
428870ff
BB
3899/*
3900 * Testcase to test the upgrading of a microzap to fatzap.
3901 */
3902void
3903ztest_fzap(ztest_ds_t *zd, uint64_t id)
3904{
3905 objset_t *os = zd->zd_os;
3906 ztest_od_t od[1];
3907 uint64_t object, txg;
d6320ddb 3908 int i;
34dc7c2f 3909
428870ff
BB
3910 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3911
3912 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3913 return;
3914
3915 object = od[0].od_object;
34dc7c2f
BB
3916
3917 /*
428870ff
BB
3918 * Add entries to this ZAP and make sure it spills over
3919 * and gets upgraded to a fatzap. Also, since we are adding
3920 * 2050 entries we should see ptrtbl growth and leaf-block split.
34dc7c2f 3921 */
d6320ddb 3922 for (i = 0; i < 2050; i++) {
428870ff
BB
3923 char name[MAXNAMELEN];
3924 uint64_t value = i;
3925 dmu_tx_t *tx;
3926 int error;
34dc7c2f 3927
428870ff 3928 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
b8864a23 3929 (u_longlong_t)id, (u_longlong_t)value);
428870ff
BB
3930
3931 tx = dmu_tx_create(os);
3932 dmu_tx_hold_zap(tx, object, B_TRUE, name);
3933 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3934 if (txg == 0)
3935 return;
3936 error = zap_add(os, object, name, sizeof (uint64_t), 1,
3937 &value, tx);
3938 ASSERT(error == 0 || error == EEXIST);
3939 dmu_tx_commit(tx);
34dc7c2f 3940 }
34dc7c2f
BB
3941}
3942
428870ff 3943/* ARGSUSED */
34dc7c2f 3944void
428870ff 3945ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3946{
428870ff
BB
3947 objset_t *os = zd->zd_os;
3948 ztest_od_t od[1];
34dc7c2f
BB
3949 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3950 dmu_tx_t *tx;
3951 int i, namelen, error;
428870ff 3952 int micro = ztest_random(2);
34dc7c2f
BB
3953 char name[20], string_value[20];
3954 void *data;
3955
428870ff
BB
3956 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
3957
3958 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3959 return;
3960
3961 object = od[0].od_object;
3962
34dc7c2f
BB
3963 /*
3964 * Generate a random name of the form 'xxx.....' where each
3965 * x is a random printable character and the dots are dots.
3966 * There are 94 such characters, and the name length goes from
3967 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
3968 */
3969 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
3970
3971 for (i = 0; i < 3; i++)
3972 name[i] = '!' + ztest_random('~' - '!' + 1);
3973 for (; i < namelen - 1; i++)
3974 name[i] = '.';
3975 name[i] = '\0';
3976
428870ff 3977 if ((namelen & 1) || micro) {
34dc7c2f
BB
3978 wsize = sizeof (txg);
3979 wc = 1;
3980 data = &txg;
3981 } else {
3982 wsize = 1;
3983 wc = namelen;
3984 data = string_value;
3985 }
3986
3987 count = -1ULL;
3988 VERIFY(zap_count(os, object, &count) == 0);
3989 ASSERT(count != -1ULL);
3990
3991 /*
3992 * Select an operation: length, lookup, add, update, remove.
3993 */
3994 i = ztest_random(5);
3995
3996 if (i >= 2) {
3997 tx = dmu_tx_create(os);
428870ff
BB
3998 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3999 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4000 if (txg == 0)
34dc7c2f 4001 return;
34dc7c2f
BB
4002 bcopy(name, string_value, namelen);
4003 } else {
4004 tx = NULL;
4005 txg = 0;
4006 bzero(string_value, namelen);
4007 }
4008
4009 switch (i) {
4010
4011 case 0:
4012 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4013 if (error == 0) {
4014 ASSERT3U(wsize, ==, zl_wsize);
4015 ASSERT3U(wc, ==, zl_wc);
4016 } else {
4017 ASSERT3U(error, ==, ENOENT);
4018 }
4019 break;
4020
4021 case 1:
4022 error = zap_lookup(os, object, name, wsize, wc, data);
4023 if (error == 0) {
4024 if (data == string_value &&
4025 bcmp(name, data, namelen) != 0)
4026 fatal(0, "name '%s' != val '%s' len %d",
4027 name, data, namelen);
4028 } else {
4029 ASSERT3U(error, ==, ENOENT);
4030 }
4031 break;
4032
4033 case 2:
4034 error = zap_add(os, object, name, wsize, wc, data, tx);
4035 ASSERT(error == 0 || error == EEXIST);
4036 break;
4037
4038 case 3:
4039 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4040 break;
4041
4042 case 4:
4043 error = zap_remove(os, object, name, tx);
4044 ASSERT(error == 0 || error == ENOENT);
4045 break;
4046 }
4047
4048 if (tx != NULL)
4049 dmu_tx_commit(tx);
4050}
4051
428870ff
BB
4052/*
4053 * Commit callback data.
4054 */
4055typedef struct ztest_cb_data {
4056 list_node_t zcd_node;
4057 uint64_t zcd_txg;
4058 int zcd_expected_err;
4059 boolean_t zcd_added;
4060 boolean_t zcd_called;
4061 spa_t *zcd_spa;
4062} ztest_cb_data_t;
4063
4064/* This is the actual commit callback function */
4065static void
4066ztest_commit_callback(void *arg, int error)
4067{
4068 ztest_cb_data_t *data = arg;
4069 uint64_t synced_txg;
4070
4071 VERIFY(data != NULL);
4072 VERIFY3S(data->zcd_expected_err, ==, error);
4073 VERIFY(!data->zcd_called);
4074
4075 synced_txg = spa_last_synced_txg(data->zcd_spa);
4076 if (data->zcd_txg > synced_txg)
4077 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4078 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4079 synced_txg);
4080
4081 data->zcd_called = B_TRUE;
4082
4083 if (error == ECANCELED) {
4084 ASSERT3U(data->zcd_txg, ==, 0);
4085 ASSERT(!data->zcd_added);
4086
4087 /*
4088 * The private callback data should be destroyed here, but
4089 * since we are going to check the zcd_called field after
4090 * dmu_tx_abort(), we will destroy it there.
4091 */
4092 return;
4093 }
4094
4095 /* Was this callback added to the global callback list? */
4096 if (!data->zcd_added)
4097 goto out;
4098
4099 ASSERT3U(data->zcd_txg, !=, 0);
4100
4101 /* Remove our callback from the list */
4102 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4103 list_remove(&zcl.zcl_callbacks, data);
4104 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4105
4106out:
4107 umem_free(data, sizeof (ztest_cb_data_t));
4108}
4109
4110/* Allocate and initialize callback data structure */
4111static ztest_cb_data_t *
4112ztest_create_cb_data(objset_t *os, uint64_t txg)
4113{
4114 ztest_cb_data_t *cb_data;
4115
4116 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4117
4118 cb_data->zcd_txg = txg;
4119 cb_data->zcd_spa = dmu_objset_spa(os);
4120
4121 return (cb_data);
4122}
4123
4124/*
4125 * If a number of txgs equal to this threshold have been created after a commit
4126 * callback has been registered but not called, then we assume there is an
4127 * implementation bug.
4128 */
4129#define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4130
4131/*
4132 * Commit callback test.
4133 */
34dc7c2f 4134void
428870ff
BB
4135ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4136{
4137 objset_t *os = zd->zd_os;
4138 ztest_od_t od[1];
4139 dmu_tx_t *tx;
4140 ztest_cb_data_t *cb_data[3], *tmp_cb;
4141 uint64_t old_txg, txg;
4142 int i, error;
4143
4144 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4145
4146 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4147 return;
4148
4149 tx = dmu_tx_create(os);
4150
4151 cb_data[0] = ztest_create_cb_data(os, 0);
4152 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4153
4154 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4155
4156 /* Every once in a while, abort the transaction on purpose */
4157 if (ztest_random(100) == 0)
4158 error = -1;
4159
4160 if (!error)
4161 error = dmu_tx_assign(tx, TXG_NOWAIT);
4162
4163 txg = error ? 0 : dmu_tx_get_txg(tx);
4164
4165 cb_data[0]->zcd_txg = txg;
4166 cb_data[1] = ztest_create_cb_data(os, txg);
4167 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4168
4169 if (error) {
4170 /*
4171 * It's not a strict requirement to call the registered
4172 * callbacks from inside dmu_tx_abort(), but that's what
4173 * it's supposed to happen in the current implementation
4174 * so we will check for that.
4175 */
4176 for (i = 0; i < 2; i++) {
4177 cb_data[i]->zcd_expected_err = ECANCELED;
4178 VERIFY(!cb_data[i]->zcd_called);
4179 }
4180
4181 dmu_tx_abort(tx);
4182
4183 for (i = 0; i < 2; i++) {
4184 VERIFY(cb_data[i]->zcd_called);
4185 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4186 }
4187
4188 return;
4189 }
4190
4191 cb_data[2] = ztest_create_cb_data(os, txg);
4192 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4193
4194 /*
4195 * Read existing data to make sure there isn't a future leak.
4196 */
4197 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4198 &old_txg, DMU_READ_PREFETCH));
4199
4200 if (old_txg > txg)
4201 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4202 old_txg, txg);
4203
4204 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4205
4206 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4207
4208 /*
4209 * Since commit callbacks don't have any ordering requirement and since
4210 * it is theoretically possible for a commit callback to be called
4211 * after an arbitrary amount of time has elapsed since its txg has been
4212 * synced, it is difficult to reliably determine whether a commit
4213 * callback hasn't been called due to high load or due to a flawed
4214 * implementation.
4215 *
4216 * In practice, we will assume that if after a certain number of txgs a
4217 * commit callback hasn't been called, then most likely there's an
4218 * implementation bug..
4219 */
4220 tmp_cb = list_head(&zcl.zcl_callbacks);
4221 if (tmp_cb != NULL &&
4222 tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
4223 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4224 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4225 }
4226
4227 /*
4228 * Let's find the place to insert our callbacks.
4229 *
4230 * Even though the list is ordered by txg, it is possible for the
4231 * insertion point to not be the end because our txg may already be
4232 * quiescing at this point and other callbacks in the open txg
4233 * (from other objsets) may have sneaked in.
4234 */
4235 tmp_cb = list_tail(&zcl.zcl_callbacks);
4236 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4237 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4238
4239 /* Add the 3 callbacks to the list */
4240 for (i = 0; i < 3; i++) {
4241 if (tmp_cb == NULL)
4242 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4243 else
4244 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4245 cb_data[i]);
4246
4247 cb_data[i]->zcd_added = B_TRUE;
4248 VERIFY(!cb_data[i]->zcd_called);
4249
4250 tmp_cb = cb_data[i];
4251 }
4252
4253 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4254
4255 dmu_tx_commit(tx);
4256}
4257
4258/* ARGSUSED */
4259void
4260ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4261{
4262 zfs_prop_t proplist[] = {
4263 ZFS_PROP_CHECKSUM,
4264 ZFS_PROP_COMPRESSION,
4265 ZFS_PROP_COPIES,
4266 ZFS_PROP_DEDUP
4267 };
4268 ztest_shared_t *zs = ztest_shared;
d6320ddb 4269 int p;
428870ff
BB
4270
4271 (void) rw_rdlock(&zs->zs_name_lock);
4272
d6320ddb 4273 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
428870ff
BB
4274 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4275 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4276
4277 (void) rw_unlock(&zs->zs_name_lock);
4278}
4279
4280/* ARGSUSED */
4281void
4282ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4283{
4284 ztest_shared_t *zs = ztest_shared;
4285 nvlist_t *props = NULL;
4286
4287 (void) rw_rdlock(&zs->zs_name_lock);
4288
4289 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4290 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4291
4292 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4293
4294 if (zopt_verbose >= 6)
4295 dump_nvlist(props, 4);
4296
4297 nvlist_free(props);
4298
4299 (void) rw_unlock(&zs->zs_name_lock);
4300}
4301
4302/*
4303 * Test snapshot hold/release and deferred destroy.
4304 */
4305void
4306ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4307{
34dc7c2f 4308 int error;
428870ff
BB
4309 objset_t *os = zd->zd_os;
4310 objset_t *origin;
4311 char snapname[100];
4312 char fullname[100];
4313 char clonename[100];
4314 char tag[100];
4315 char osname[MAXNAMELEN];
34dc7c2f
BB
4316
4317 (void) rw_rdlock(&ztest_shared->zs_name_lock);
4318
4319 dmu_objset_name(os, osname);
4320
428870ff
BB
4321 (void) snprintf(snapname, 100, "sh1_%llu", id);
4322 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4323 (void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
4324 (void) snprintf(tag, 100, "%tag_%llu", id);
4325
4326 /*
4327 * Clean up from any previous run.
4328 */
4329 (void) dmu_objset_destroy(clonename, B_FALSE);
4330 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4331 (void) dmu_objset_destroy(fullname, B_FALSE);
4332
4333 /*
4334 * Create snapshot, clone it, mark snap for deferred destroy,
4335 * destroy clone, verify snap was also destroyed.
4336 */
572e2857
BB
4337 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4338 FALSE, -1);
428870ff
BB
4339 if (error) {
4340 if (error == ENOSPC) {
4341 ztest_record_enospc("dmu_objset_snapshot");
4342 goto out;
34dc7c2f 4343 }
428870ff
BB
4344 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4345 }
34dc7c2f 4346
428870ff
BB
4347 error = dmu_objset_hold(fullname, FTAG, &origin);
4348 if (error)
4349 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
34dc7c2f 4350
428870ff
BB
4351 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4352 dmu_objset_rele(origin, FTAG);
4353 if (error) {
34dc7c2f 4354 if (error == ENOSPC) {
428870ff
BB
4355 ztest_record_enospc("dmu_objset_clone");
4356 goto out;
34dc7c2f 4357 }
428870ff
BB
4358 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4359 }
34dc7c2f 4360
428870ff
BB
4361 error = dmu_objset_destroy(fullname, B_TRUE);
4362 if (error) {
4363 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4364 fullname, error);
4365 }
34dc7c2f 4366
428870ff
BB
4367 error = dmu_objset_destroy(clonename, B_FALSE);
4368 if (error)
4369 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
34dc7c2f 4370
428870ff
BB
4371 error = dmu_objset_hold(fullname, FTAG, &origin);
4372 if (error != ENOENT)
4373 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
34dc7c2f 4374
428870ff
BB
4375 /*
4376 * Create snapshot, add temporary hold, verify that we can't
4377 * destroy a held snapshot, mark for deferred destroy,
4378 * release hold, verify snapshot was destroyed.
4379 */
572e2857
BB
4380 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4381 FALSE, -1);
428870ff
BB
4382 if (error) {
4383 if (error == ENOSPC) {
4384 ztest_record_enospc("dmu_objset_snapshot");
4385 goto out;
34dc7c2f 4386 }
428870ff
BB
4387 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4388 }
4389
572e2857
BB
4390 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4391 B_TRUE, -1);
428870ff
BB
4392 if (error)
4393 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4394
4395 error = dmu_objset_destroy(fullname, B_FALSE);
4396 if (error != EBUSY) {
4397 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4398 fullname, error);
4399 }
4400
4401 error = dmu_objset_destroy(fullname, B_TRUE);
4402 if (error) {
4403 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4404 fullname, error);
34dc7c2f
BB
4405 }
4406
428870ff
BB
4407 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4408 if (error)
4409 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4410
4411 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4412
4413out:
34dc7c2f
BB
4414 (void) rw_unlock(&ztest_shared->zs_name_lock);
4415}
4416
34dc7c2f
BB
4417/*
4418 * Inject random faults into the on-disk data.
4419 */
428870ff 4420/* ARGSUSED */
34dc7c2f 4421void
428870ff 4422ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4423{
428870ff
BB
4424 ztest_shared_t *zs = ztest_shared;
4425 spa_t *spa = zs->zs_spa;
34dc7c2f
BB
4426 int fd;
4427 uint64_t offset;
428870ff 4428 uint64_t leaves;
c5b3a7bb 4429 uint64_t bad = 0x1990c0ffeedecadeull;
34dc7c2f
BB
4430 uint64_t top, leaf;
4431 char path0[MAXPATHLEN];
4432 char pathrand[MAXPATHLEN];
4433 size_t fsize;
34dc7c2f
BB
4434 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4435 int iters = 1000;
428870ff
BB
4436 int maxfaults;
4437 int mirror_save;
b128c09f 4438 vdev_t *vd0 = NULL;
34dc7c2f 4439 uint64_t guid0 = 0;
428870ff
BB
4440 boolean_t islog = B_FALSE;
4441
4442 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4443 maxfaults = MAXFAULTS();
4444 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4445 mirror_save = zs->zs_mirrors;
4446 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
34dc7c2f 4447
b128c09f 4448 ASSERT(leaves >= 1);
34dc7c2f
BB
4449
4450 /*
b128c09f 4451 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
34dc7c2f 4452 */
b128c09f 4453 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 4454
b128c09f
BB
4455 if (ztest_random(2) == 0) {
4456 /*
428870ff 4457 * Inject errors on a normal data device or slog device.
b128c09f 4458 */
428870ff
BB
4459 top = ztest_random_vdev_top(spa, B_TRUE);
4460 leaf = ztest_random(leaves) + zs->zs_splits;
34dc7c2f 4461
b128c09f
BB
4462 /*
4463 * Generate paths to the first leaf in this top-level vdev,
4464 * and to the random leaf we selected. We'll induce transient
4465 * write failures and random online/offline activity on leaf 0,
4466 * and we'll write random garbage to the randomly chosen leaf.
4467 */
4468 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
428870ff 4469 zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
b128c09f
BB
4470 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4471 zopt_dir, zopt_pool, top * leaves + leaf);
34dc7c2f 4472
b128c09f 4473 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
428870ff
BB
4474 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4475 islog = B_TRUE;
4476
b128c09f
BB
4477 if (vd0 != NULL && maxfaults != 1) {
4478 /*
4479 * Make vd0 explicitly claim to be unreadable,
4480 * or unwriteable, or reach behind its back
4481 * and close the underlying fd. We can do this if
4482 * maxfaults == 0 because we'll fail and reexecute,
4483 * and we can do it if maxfaults >= 2 because we'll
4484 * have enough redundancy. If maxfaults == 1, the
4485 * combination of this with injection of random data
4486 * corruption below exceeds the pool's fault tolerance.
4487 */
4488 vdev_file_t *vf = vd0->vdev_tsd;
4489
4490 if (vf != NULL && ztest_random(3) == 0) {
4491 (void) close(vf->vf_vnode->v_fd);
4492 vf->vf_vnode->v_fd = -1;
4493 } else if (ztest_random(2) == 0) {
4494 vd0->vdev_cant_read = B_TRUE;
4495 } else {
4496 vd0->vdev_cant_write = B_TRUE;
4497 }
4498 guid0 = vd0->vdev_guid;
4499 }
4500 } else {
4501 /*
4502 * Inject errors on an l2cache device.
4503 */
4504 spa_aux_vdev_t *sav = &spa->spa_l2cache;
34dc7c2f 4505
b128c09f
BB
4506 if (sav->sav_count == 0) {
4507 spa_config_exit(spa, SCL_STATE, FTAG);
4508 return;
4509 }
4510 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
34dc7c2f 4511 guid0 = vd0->vdev_guid;
b128c09f
BB
4512 (void) strcpy(path0, vd0->vdev_path);
4513 (void) strcpy(pathrand, vd0->vdev_path);
4514
4515 leaf = 0;
4516 leaves = 1;
4517 maxfaults = INT_MAX; /* no limit on cache devices */
34dc7c2f
BB
4518 }
4519
b128c09f
BB
4520 spa_config_exit(spa, SCL_STATE, FTAG);
4521
34dc7c2f 4522 /*
428870ff
BB
4523 * If we can tolerate two or more faults, or we're dealing
4524 * with a slog, randomly online/offline vd0.
34dc7c2f 4525 */
428870ff 4526 if ((maxfaults >= 2 || islog) && guid0 != 0) {
fb5f0bc8
BB
4527 if (ztest_random(10) < 6) {
4528 int flags = (ztest_random(2) == 0 ?
4529 ZFS_OFFLINE_TEMPORARY : 0);
428870ff
BB
4530
4531 /*
4532 * We have to grab the zs_name_lock as writer to
4533 * prevent a race between offlining a slog and
4534 * destroying a dataset. Offlining the slog will
4535 * grab a reference on the dataset which may cause
4536 * dmu_objset_destroy() to fail with EBUSY thus
4537 * leaving the dataset in an inconsistent state.
4538 */
4539 if (islog)
4540 (void) rw_wrlock(&ztest_shared->zs_name_lock);
4541
fb5f0bc8 4542 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
428870ff
BB
4543
4544 if (islog)
4545 (void) rw_unlock(&ztest_shared->zs_name_lock);
fb5f0bc8
BB
4546 } else {
4547 (void) vdev_online(spa, guid0, 0, NULL);
4548 }
34dc7c2f
BB
4549 }
4550
428870ff
BB
4551 if (maxfaults == 0)
4552 return;
4553
34dc7c2f
BB
4554 /*
4555 * We have at least single-fault tolerance, so inject data corruption.
4556 */
4557 fd = open(pathrand, O_RDWR);
4558
4559 if (fd == -1) /* we hit a gap in the device namespace */
4560 return;
4561
4562 fsize = lseek(fd, 0, SEEK_END);
4563
4564 while (--iters != 0) {
4565 offset = ztest_random(fsize / (leaves << bshift)) *
4566 (leaves << bshift) + (leaf << bshift) +
4567 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4568
4569 if (offset >= fsize)
4570 continue;
4571
428870ff
BB
4572 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4573 if (mirror_save != zs->zs_mirrors) {
4574 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4575 (void) close(fd);
4576 return;
4577 }
34dc7c2f
BB
4578
4579 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4580 fatal(1, "can't inject bad word at 0x%llx in %s",
4581 offset, pathrand);
428870ff
BB
4582
4583 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4584
4585 if (zopt_verbose >= 7)
4586 (void) printf("injected bad word into %s,"
4587 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
34dc7c2f
BB
4588 }
4589
4590 (void) close(fd);
4591}
4592
4593/*
428870ff 4594 * Verify that DDT repair works as expected.
34dc7c2f
BB
4595 */
4596void
428870ff 4597ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4598{
428870ff
BB
4599 ztest_shared_t *zs = ztest_shared;
4600 spa_t *spa = zs->zs_spa;
4601 objset_t *os = zd->zd_os;
4602 ztest_od_t od[1];
4603 uint64_t object, blocksize, txg, pattern, psize;
4604 enum zio_checksum checksum = spa_dedup_checksum(spa);
4605 dmu_buf_t *db;
4606 dmu_tx_t *tx;
4607 void *buf;
4608 blkptr_t blk;
4609 int copies = 2 * ZIO_DEDUPDITTO_MIN;
d6320ddb 4610 int i;
34dc7c2f 4611
428870ff
BB
4612 blocksize = ztest_random_blocksize();
4613 blocksize = MIN(blocksize, 2048); /* because we write so many */
34dc7c2f 4614
428870ff 4615 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
34dc7c2f 4616
428870ff
BB
4617 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4618 return;
34dc7c2f
BB
4619
4620 /*
428870ff
BB
4621 * Take the name lock as writer to prevent anyone else from changing
4622 * the pool and dataset properies we need to maintain during this test.
34dc7c2f 4623 */
428870ff 4624 (void) rw_wrlock(&zs->zs_name_lock);
34dc7c2f 4625
428870ff
BB
4626 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4627 B_FALSE) != 0 ||
4628 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4629 B_FALSE) != 0) {
4630 (void) rw_unlock(&zs->zs_name_lock);
4631 return;
4632 }
4633
4634 object = od[0].od_object;
4635 blocksize = od[0].od_blocksize;
4636 pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
4637
4638 ASSERT(object != 0);
4639
4640 tx = dmu_tx_create(os);
4641 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4642 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4643 if (txg == 0) {
4644 (void) rw_unlock(&zs->zs_name_lock);
4645 return;
4646 }
34dc7c2f
BB
4647
4648 /*
428870ff 4649 * Write all the copies of our block.
34dc7c2f 4650 */
d6320ddb 4651 for (i = 0; i < copies; i++) {
428870ff
BB
4652 uint64_t offset = i * blocksize;
4653 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4654 DMU_READ_NO_PREFETCH) == 0);
4655 ASSERT(db->db_offset == offset);
4656 ASSERT(db->db_size == blocksize);
4657 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4658 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4659 dmu_buf_will_fill(db, tx);
4660 ztest_pattern_set(db->db_data, db->db_size, pattern);
4661 dmu_buf_rele(db, FTAG);
4662 }
34dc7c2f 4663
428870ff
BB
4664 dmu_tx_commit(tx);
4665 txg_wait_synced(spa_get_dsl(spa), txg);
34dc7c2f
BB
4666
4667 /*
428870ff 4668 * Find out what block we got.
34dc7c2f 4669 */
428870ff
BB
4670 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4671 DMU_READ_NO_PREFETCH) == 0);
4672 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4673 dmu_buf_rele(db, FTAG);
34dc7c2f
BB
4674
4675 /*
428870ff 4676 * Damage the block. Dedup-ditto will save us when we read it later.
34dc7c2f 4677 */
428870ff
BB
4678 psize = BP_GET_PSIZE(&blk);
4679 buf = zio_buf_alloc(psize);
4680 ztest_pattern_set(buf, psize, ~pattern);
34dc7c2f 4681
428870ff
BB
4682 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4683 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4684 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
34dc7c2f 4685
428870ff 4686 zio_buf_free(buf, psize);
34dc7c2f 4687
428870ff 4688 (void) rw_unlock(&zs->zs_name_lock);
34dc7c2f
BB
4689}
4690
34dc7c2f 4691/*
428870ff 4692 * Scrub the pool.
34dc7c2f 4693 */
428870ff
BB
4694/* ARGSUSED */
4695void
4696ztest_scrub(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4697{
428870ff
BB
4698 ztest_shared_t *zs = ztest_shared;
4699 spa_t *spa = zs->zs_spa;
34dc7c2f 4700
428870ff
BB
4701 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4702 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4703 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4704}
34dc7c2f 4705
428870ff
BB
4706/*
4707 * Rename the pool to a different name and then rename it back.
4708 */
4709/* ARGSUSED */
4710void
4711ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4712{
4713 ztest_shared_t *zs = ztest_shared;
4714 char *oldname, *newname;
4715 spa_t *spa;
34dc7c2f 4716
428870ff 4717 (void) rw_wrlock(&zs->zs_name_lock);
34dc7c2f 4718
428870ff
BB
4719 oldname = zs->zs_pool;
4720 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4721 (void) strcpy(newname, oldname);
4722 (void) strcat(newname, "_tmp");
34dc7c2f
BB
4723
4724 /*
428870ff 4725 * Do the rename
34dc7c2f 4726 */
428870ff 4727 VERIFY3U(0, ==, spa_rename(oldname, newname));
34dc7c2f
BB
4728
4729 /*
428870ff 4730 * Try to open it under the old name, which shouldn't exist
34dc7c2f 4731 */
428870ff 4732 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
4733
4734 /*
428870ff
BB
4735 * Open it under the new name and make sure it's still the same spa_t.
4736 */
4737 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f 4738
428870ff
BB
4739 ASSERT(spa == zs->zs_spa);
4740 spa_close(spa, FTAG);
34dc7c2f
BB
4741
4742 /*
428870ff 4743 * Rename it back to the original
34dc7c2f 4744 */
428870ff 4745 VERIFY3U(0, ==, spa_rename(newname, oldname));
34dc7c2f 4746
428870ff
BB
4747 /*
4748 * Make sure it can still be opened
4749 */
4750 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 4751
428870ff
BB
4752 ASSERT(spa == zs->zs_spa);
4753 spa_close(spa, FTAG);
4754
4755 umem_free(newname, strlen(newname) + 1);
4756
4757 (void) rw_unlock(&zs->zs_name_lock);
34dc7c2f
BB
4758}
4759
428870ff
BB
4760/*
4761 * Verify pool integrity by running zdb.
4762 */
34dc7c2f 4763static void
428870ff 4764ztest_run_zdb(char *pool)
34dc7c2f
BB
4765{
4766 int status;
4767 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4768 char zbuf[1024];
4769 char *bin;
4770 char *ztest;
4771 char *isa;
4772 int isalen;
4773 FILE *fp;
4774
4775 (void) realpath(getexecname(), zdb);
4776
4777 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4778 bin = strstr(zdb, "/usr/bin/");
4779 ztest = strstr(bin, "/ztest");
4780 isa = bin + 8;
4781 isalen = ztest - isa;
4782 isa = strdup(isa);
4783 /* LINTED */
4784 (void) sprintf(bin,
428870ff 4785 "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
34dc7c2f
BB
4786 isalen,
4787 isa,
4788 zopt_verbose >= 3 ? "s" : "",
4789 zopt_verbose >= 4 ? "v" : "",
428870ff 4790 spa_config_path,
b128c09f 4791 pool);
34dc7c2f
BB
4792 free(isa);
4793
4794 if (zopt_verbose >= 5)
4795 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
4796
4797 fp = popen(zdb, "r");
4798
4799 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4800 if (zopt_verbose >= 3)
4801 (void) printf("%s", zbuf);
4802
4803 status = pclose(fp);
4804
4805 if (status == 0)
4806 return;
4807
4808 ztest_dump_core = 0;
4809 if (WIFEXITED(status))
4810 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4811 else
4812 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4813}
4814
4815static void
4816ztest_walk_pool_directory(char *header)
4817{
4818 spa_t *spa = NULL;
4819
4820 if (zopt_verbose >= 6)
4821 (void) printf("%s\n", header);
4822
4823 mutex_enter(&spa_namespace_lock);
4824 while ((spa = spa_next(spa)) != NULL)
4825 if (zopt_verbose >= 6)
4826 (void) printf("\t%s\n", spa_name(spa));
4827 mutex_exit(&spa_namespace_lock);
4828}
4829
4830static void
4831ztest_spa_import_export(char *oldname, char *newname)
4832{
fb5f0bc8 4833 nvlist_t *config, *newconfig;
34dc7c2f
BB
4834 uint64_t pool_guid;
4835 spa_t *spa;
34dc7c2f
BB
4836
4837 if (zopt_verbose >= 4) {
4838 (void) printf("import/export: old = %s, new = %s\n",
4839 oldname, newname);
4840 }
4841
4842 /*
4843 * Clean up from previous runs.
4844 */
4845 (void) spa_destroy(newname);
4846
4847 /*
4848 * Get the pool's configuration and guid.
4849 */
428870ff 4850 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 4851
fb5f0bc8
BB
4852 /*
4853 * Kick off a scrub to tickle scrub/export races.
4854 */
4855 if (ztest_random(2) == 0)
428870ff 4856 (void) spa_scan(spa, POOL_SCAN_SCRUB);
fb5f0bc8 4857
34dc7c2f
BB
4858 pool_guid = spa_guid(spa);
4859 spa_close(spa, FTAG);
4860
4861 ztest_walk_pool_directory("pools before export");
4862
4863 /*
4864 * Export it.
4865 */
428870ff 4866 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
34dc7c2f
BB
4867
4868 ztest_walk_pool_directory("pools after export");
4869
fb5f0bc8
BB
4870 /*
4871 * Try to import it.
4872 */
4873 newconfig = spa_tryimport(config);
4874 ASSERT(newconfig != NULL);
4875 nvlist_free(newconfig);
4876
34dc7c2f
BB
4877 /*
4878 * Import it under the new name.
4879 */
572e2857 4880 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
34dc7c2f
BB
4881
4882 ztest_walk_pool_directory("pools after import");
4883
4884 /*
4885 * Try to import it again -- should fail with EEXIST.
4886 */
572e2857 4887 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
34dc7c2f
BB
4888
4889 /*
4890 * Try to import it under a different name -- should fail with EEXIST.
4891 */
572e2857 4892 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
34dc7c2f
BB
4893
4894 /*
4895 * Verify that the pool is no longer visible under the old name.
4896 */
428870ff 4897 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
4898
4899 /*
4900 * Verify that we can open and close the pool using the new name.
4901 */
428870ff 4902 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f
BB
4903 ASSERT(pool_guid == spa_guid(spa));
4904 spa_close(spa, FTAG);
4905
4906 nvlist_free(config);
4907}
4908
fb5f0bc8
BB
4909static void
4910ztest_resume(spa_t *spa)
4911{
428870ff
BB
4912 if (spa_suspended(spa) && zopt_verbose >= 6)
4913 (void) printf("resuming from suspended state\n");
4914 spa_vdev_state_enter(spa, SCL_NONE);
4915 vdev_clear(spa, NULL);
4916 (void) spa_vdev_state_exit(spa, NULL, 0);
4917 (void) zio_resume(spa);
fb5f0bc8
BB
4918}
4919
34dc7c2f 4920static void *
fb5f0bc8 4921ztest_resume_thread(void *arg)
34dc7c2f 4922{
b128c09f 4923 spa_t *spa = arg;
34dc7c2f
BB
4924
4925 while (!ztest_exiting) {
428870ff
BB
4926 if (spa_suspended(spa))
4927 ztest_resume(spa);
4928 (void) poll(NULL, 0, 100);
34dc7c2f 4929 }
34dc7c2f
BB
4930 return (NULL);
4931}
4932
428870ff
BB
4933static void *
4934ztest_deadman_thread(void *arg)
4935{
4936 ztest_shared_t *zs = arg;
4937 int grace = 300;
4938 hrtime_t delta;
4939
4940 delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
4941
4942 (void) poll(NULL, 0, (int)(1000 * delta));
4943
4944 fatal(0, "failed to complete within %d seconds of deadline", grace);
4945
4946 return (NULL);
4947}
4948
4949static void
4950ztest_execute(ztest_info_t *zi, uint64_t id)
4951{
4952 ztest_shared_t *zs = ztest_shared;
4953 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
4954 hrtime_t functime = gethrtime();
d6320ddb 4955 int i;
428870ff 4956
d6320ddb 4957 for (i = 0; i < zi->zi_iters; i++)
428870ff
BB
4958 zi->zi_func(zd, id);
4959
4960 functime = gethrtime() - functime;
4961
4962 atomic_add_64(&zi->zi_call_count, 1);
4963 atomic_add_64(&zi->zi_call_time, functime);
4964
4965 if (zopt_verbose >= 4) {
4966 Dl_info dli;
4967 (void) dladdr((void *)zi->zi_func, &dli);
4968 (void) printf("%6.2f sec in %s\n",
4969 (double)functime / NANOSEC, dli.dli_sname);
4970 }
4971}
4972
34dc7c2f
BB
4973static void *
4974ztest_thread(void *arg)
4975{
428870ff 4976 uint64_t id = (uintptr_t)arg;
34dc7c2f 4977 ztest_shared_t *zs = ztest_shared;
428870ff
BB
4978 uint64_t call_next;
4979 hrtime_t now;
34dc7c2f 4980 ztest_info_t *zi;
34dc7c2f 4981
428870ff 4982 while ((now = gethrtime()) < zs->zs_thread_stop) {
34dc7c2f
BB
4983 /*
4984 * See if it's time to force a crash.
4985 */
428870ff
BB
4986 if (now > zs->zs_thread_kill)
4987 ztest_kill(zs);
34dc7c2f
BB
4988
4989 /*
428870ff 4990 * If we're getting ENOSPC with some regularity, stop.
34dc7c2f 4991 */
428870ff
BB
4992 if (zs->zs_enospc_count > 10)
4993 break;
34dc7c2f
BB
4994
4995 /*
428870ff 4996 * Pick a random function to execute.
34dc7c2f 4997 */
428870ff
BB
4998 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
4999 call_next = zi->zi_call_next;
34dc7c2f 5000
428870ff
BB
5001 if (now >= call_next &&
5002 atomic_cas_64(&zi->zi_call_next, call_next, call_next +
5003 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
5004 ztest_execute(zi, id);
5005 }
34dc7c2f 5006
428870ff
BB
5007 return (NULL);
5008}
34dc7c2f 5009
428870ff
BB
5010static void
5011ztest_dataset_name(char *dsname, char *pool, int d)
5012{
5013 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5014}
34dc7c2f 5015
428870ff
BB
5016static void
5017ztest_dataset_destroy(ztest_shared_t *zs, int d)
5018{
5019 char name[MAXNAMELEN];
d6320ddb 5020 int t;
34dc7c2f 5021
428870ff 5022 ztest_dataset_name(name, zs->zs_pool, d);
34dc7c2f 5023
428870ff
BB
5024 if (zopt_verbose >= 3)
5025 (void) printf("Destroying %s to free up space\n", name);
34dc7c2f 5026
428870ff
BB
5027 /*
5028 * Cleanup any non-standard clones and snapshots. In general,
5029 * ztest thread t operates on dataset (t % zopt_datasets),
5030 * so there may be more than one thing to clean up.
5031 */
d6320ddb 5032 for (t = d; t < zopt_threads; t += zopt_datasets)
428870ff
BB
5033 ztest_dsl_dataset_cleanup(name, t);
5034
5035 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5036 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5037}
5038
5039static void
5040ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5041{
5042 uint64_t usedobjs, dirobjs, scratch;
5043
5044 /*
5045 * ZTEST_DIROBJ is the object directory for the entire dataset.
5046 * Therefore, the number of objects in use should equal the
5047 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5048 * If not, we have an object leak.
5049 *
5050 * Note that we can only check this in ztest_dataset_open(),
5051 * when the open-context and syncing-context values agree.
5052 * That's because zap_count() returns the open-context value,
5053 * while dmu_objset_space() returns the rootbp fill count.
5054 */
5055 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5056 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5057 ASSERT3U(dirobjs + 1, ==, usedobjs);
5058}
5059
5060static int
5061ztest_dataset_open(ztest_shared_t *zs, int d)
5062{
5063 ztest_ds_t *zd = &zs->zs_zd[d];
5064 uint64_t committed_seq = zd->zd_seq;
5065 objset_t *os;
5066 zilog_t *zilog;
5067 char name[MAXNAMELEN];
5068 int error;
5069
5070 ztest_dataset_name(name, zs->zs_pool, d);
5071
5072 (void) rw_rdlock(&zs->zs_name_lock);
5073
5074 error = ztest_dataset_create(name);
5075 if (error == ENOSPC) {
5076 (void) rw_unlock(&zs->zs_name_lock);
5077 ztest_record_enospc(FTAG);
5078 return (error);
34dc7c2f 5079 }
428870ff 5080 ASSERT(error == 0 || error == EEXIST);
34dc7c2f 5081
428870ff
BB
5082 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5083 (void) rw_unlock(&zs->zs_name_lock);
5084
5085 ztest_zd_init(zd, os);
5086
5087 zilog = zd->zd_zilog;
5088
5089 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5090 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5091 fatal(0, "missing log records: claimed %llu < committed %llu",
5092 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5093
5094 ztest_dataset_dirobj_verify(zd);
5095
5096 zil_replay(os, zd, ztest_replay_vector);
5097
5098 ztest_dataset_dirobj_verify(zd);
5099
5100 if (zopt_verbose >= 6)
5101 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5102 zd->zd_name,
5103 (u_longlong_t)zilog->zl_parse_blk_count,
5104 (u_longlong_t)zilog->zl_parse_lr_count,
5105 (u_longlong_t)zilog->zl_replaying_seq);
5106
5107 zilog = zil_open(os, ztest_get_data);
5108
5109 if (zilog->zl_replaying_seq != 0 &&
5110 zilog->zl_replaying_seq < committed_seq)
5111 fatal(0, "missing log records: replayed %llu < committed %llu",
5112 zilog->zl_replaying_seq, committed_seq);
5113
5114 return (0);
5115}
5116
5117static void
5118ztest_dataset_close(ztest_shared_t *zs, int d)
5119{
5120 ztest_ds_t *zd = &zs->zs_zd[d];
5121
5122 zil_close(zd->zd_zilog);
5123 dmu_objset_rele(zd->zd_os, zd);
5124
5125 ztest_zd_fini(zd);
34dc7c2f
BB
5126}
5127
5128/*
5129 * Kick off threads to run tests on all datasets in parallel.
5130 */
5131static void
428870ff 5132ztest_run(ztest_shared_t *zs)
34dc7c2f 5133{
428870ff 5134 thread_t *tid;
34dc7c2f 5135 spa_t *spa;
b128c09f 5136 thread_t resume_tid;
428870ff 5137 int error;
d6320ddb 5138 int t, d;
b128c09f
BB
5139
5140 ztest_exiting = B_FALSE;
34dc7c2f 5141
34dc7c2f 5142 /*
428870ff 5143 * Initialize parent/child shared state.
34dc7c2f 5144 */
428870ff
BB
5145 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5146 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f 5147
428870ff
BB
5148 zs->zs_thread_start = gethrtime();
5149 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5150 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5151 zs->zs_thread_kill = zs->zs_thread_stop;
5152 if (ztest_random(100) < zopt_killrate)
5153 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
34dc7c2f 5154
428870ff 5155 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
34dc7c2f 5156
428870ff
BB
5157 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5158 offsetof(ztest_cb_data_t, zcd_node));
34dc7c2f
BB
5159
5160 /*
b128c09f 5161 * Open our pool.
34dc7c2f 5162 */
428870ff
BB
5163 kernel_init(FREAD | FWRITE);
5164 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5165 zs->zs_spa = spa;
5166
5167 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
34dc7c2f 5168
fb5f0bc8
BB
5169 /*
5170 * We don't expect the pool to suspend unless maxfaults == 0,
5171 * in which case ztest_fault_inject() temporarily takes away
5172 * the only valid replica.
5173 */
428870ff 5174 if (MAXFAULTS() == 0)
fb5f0bc8
BB
5175 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5176 else
5177 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5178
34dc7c2f 5179 /*
b128c09f 5180 * Create a thread to periodically resume suspended I/O.
34dc7c2f 5181 */
fb5f0bc8 5182 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
b128c09f 5183 &resume_tid) == 0);
34dc7c2f 5184
428870ff
BB
5185 /*
5186 * Create a deadman thread to abort() if we hang.
5187 */
5188 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5189 NULL) == 0);
5190
34dc7c2f
BB
5191 /*
5192 * Verify that we can safely inquire about about any object,
5193 * whether it's allocated or not. To make it interesting,
5194 * we probe a 5-wide window around each power of two.
5195 * This hits all edge cases, including zero and the max.
5196 */
d6320ddb
BB
5197 for (t = 0; t < 64; t++) {
5198 for (d = -5; d <= 5; d++) {
34dc7c2f
BB
5199 error = dmu_object_info(spa->spa_meta_objset,
5200 (1ULL << t) + d, NULL);
5201 ASSERT(error == 0 || error == ENOENT ||
5202 error == EINVAL);
5203 }
5204 }
5205
5206 /*
428870ff 5207 * If we got any ENOSPC errors on the previous run, destroy something.
34dc7c2f 5208 */
428870ff
BB
5209 if (zs->zs_enospc_count != 0) {
5210 int d = ztest_random(zopt_datasets);
5211 ztest_dataset_destroy(zs, d);
5212 }
34dc7c2f
BB
5213 zs->zs_enospc_count = 0;
5214
428870ff 5215 tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
34dc7c2f
BB
5216
5217 if (zopt_verbose >= 4)
5218 (void) printf("starting main threads...\n");
5219
428870ff
BB
5220 /*
5221 * Kick off all the tests that run in parallel.
5222 */
d6320ddb 5223 for (t = 0; t < zopt_threads; t++) {
428870ff
BB
5224 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5225 return;
5226 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5227 THR_BOUND, &tid[t]) == 0);
34dc7c2f
BB
5228 }
5229
428870ff
BB
5230 /*
5231 * Wait for all of the tests to complete. We go in reverse order
5232 * so we don't close datasets while threads are still using them.
5233 */
d6320ddb 5234 for (t = zopt_threads - 1; t >= 0; t--) {
428870ff
BB
5235 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5236 if (t < zopt_datasets)
5237 ztest_dataset_close(zs, t);
34dc7c2f
BB
5238 }
5239
34dc7c2f
BB
5240 txg_wait_synced(spa_get_dsl(spa), 0);
5241
428870ff
BB
5242 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5243 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5244
5245 umem_free(tid, zopt_threads * sizeof (thread_t));
5246
5247 /* Kill the resume thread */
5248 ztest_exiting = B_TRUE;
5249 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5250 ztest_resume(spa);
34dc7c2f
BB
5251
5252 /*
428870ff
BB
5253 * Right before closing the pool, kick off a bunch of async I/O;
5254 * spa_close() should wait for it to complete.
34dc7c2f 5255 */
428870ff
BB
5256 for (uint64_t object = 1; object < 50; object++)
5257 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5258
5259 spa_close(spa, FTAG);
5260
5261 /*
5262 * Verify that we can loop over all pools.
5263 */
5264 mutex_enter(&spa_namespace_lock);
5265 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5266 if (zopt_verbose > 3)
5267 (void) printf("spa_next: found %s\n", spa_name(spa));
5268 mutex_exit(&spa_namespace_lock);
5269
5270 /*
5271 * Verify that we can export the pool and reimport it under a
5272 * different name.
5273 */
5274 if (ztest_random(2) == 0) {
5275 char name[MAXNAMELEN];
5276 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5277 ztest_spa_import_export(zs->zs_pool, name);
5278 ztest_spa_import_export(name, zs->zs_pool);
5279 }
5280
5281 kernel_fini();
572e2857
BB
5282
5283 list_destroy(&zcl.zcl_callbacks);
5284
5285 (void) _mutex_destroy(&zcl.zcl_callbacks_lock);
5286
5287 (void) rwlock_destroy(&zs->zs_name_lock);
5288 (void) _mutex_destroy(&zs->zs_vdev_lock);
428870ff
BB
5289}
5290
5291static void
5292ztest_freeze(ztest_shared_t *zs)
5293{
5294 ztest_ds_t *zd = &zs->zs_zd[0];
5295 spa_t *spa;
5296 int numloops = 0;
5297
5298 if (zopt_verbose >= 3)
5299 (void) printf("testing spa_freeze()...\n");
9babb374 5300
428870ff
BB
5301 kernel_init(FREAD | FWRITE);
5302 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5303 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
9babb374 5304
428870ff
BB
5305 /*
5306 * Force the first log block to be transactionally allocated.
5307 * We have to do this before we freeze the pool -- otherwise
5308 * the log chain won't be anchored.
5309 */
5310 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5311 ztest_dmu_object_alloc_free(zd, 0);
572e2857 5312 zil_commit(zd->zd_zilog, 0);
34dc7c2f
BB
5313 }
5314
5315 txg_wait_synced(spa_get_dsl(spa), 0);
5316
428870ff
BB
5317 /*
5318 * Freeze the pool. This stops spa_sync() from doing anything,
5319 * so that the only way to record changes from now on is the ZIL.
5320 */
5321 spa_freeze(spa);
b128c09f 5322
428870ff
BB
5323 /*
5324 * Run tests that generate log records but don't alter the pool config
5325 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5326 * We do a txg_wait_synced() after each iteration to force the txg
5327 * to increase well beyond the last synced value in the uberblock.
5328 * The ZIL should be OK with that.
5329 */
5330 while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5331 ztest_dmu_write_parallel(zd, 0);
5332 ztest_dmu_object_alloc_free(zd, 0);
5333 txg_wait_synced(spa_get_dsl(spa), 0);
5334 }
b128c09f 5335
34dc7c2f 5336 /*
428870ff 5337 * Commit all of the changes we just generated.
34dc7c2f 5338 */
572e2857 5339 zil_commit(zd->zd_zilog, 0);
428870ff 5340 txg_wait_synced(spa_get_dsl(spa), 0);
34dc7c2f 5341
428870ff
BB
5342 /*
5343 * Close our dataset and close the pool.
5344 */
5345 ztest_dataset_close(zs, 0);
34dc7c2f 5346 spa_close(spa, FTAG);
428870ff 5347 kernel_fini();
34dc7c2f 5348
428870ff
BB
5349 /*
5350 * Open and close the pool and dataset to induce log replay.
5351 */
5352 kernel_init(FREAD | FWRITE);
5353 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5354 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5355 ztest_dataset_close(zs, 0);
5356 spa_close(spa, FTAG);
34dc7c2f
BB
5357 kernel_fini();
5358}
5359
5360void
5361print_time(hrtime_t t, char *timebuf)
5362{
5363 hrtime_t s = t / NANOSEC;
5364 hrtime_t m = s / 60;
5365 hrtime_t h = m / 60;
5366 hrtime_t d = h / 24;
5367
5368 s -= m * 60;
5369 m -= h * 60;
5370 h -= d * 24;
5371
5372 timebuf[0] = '\0';
5373
5374 if (d)
5375 (void) sprintf(timebuf,
5376 "%llud%02lluh%02llum%02llus", d, h, m, s);
5377 else if (h)
5378 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5379 else if (m)
5380 (void) sprintf(timebuf, "%llum%02llus", m, s);
5381 else
5382 (void) sprintf(timebuf, "%llus", s);
5383}
5384
428870ff 5385static nvlist_t *
0bc8fd78 5386make_random_props(void)
428870ff
BB
5387{
5388 nvlist_t *props;
5389
5390 if (ztest_random(2) == 0)
5391 return (NULL);
5392
5393 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5394 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5395
5396 (void) printf("props:\n");
5397 dump_nvlist(props, 4);
5398
5399 return (props);
5400}
5401
34dc7c2f
BB
5402/*
5403 * Create a storage pool with the given name and initial vdev size.
428870ff 5404 * Then test spa_freeze() functionality.
34dc7c2f
BB
5405 */
5406static void
428870ff 5407ztest_init(ztest_shared_t *zs)
34dc7c2f
BB
5408{
5409 spa_t *spa;
428870ff
BB
5410 nvlist_t *nvroot, *props;
5411
5412 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5413 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f
BB
5414
5415 kernel_init(FREAD | FWRITE);
5416
5417 /*
5418 * Create the storage pool.
5419 */
428870ff
BB
5420 (void) spa_destroy(zs->zs_pool);
5421 ztest_shared->zs_vdev_next_leaf = 0;
5422 zs->zs_splits = 0;
5423 zs->zs_mirrors = zopt_mirrors;
b128c09f 5424 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
428870ff
BB
5425 0, zopt_raidz, zs->zs_mirrors, 1);
5426 props = make_random_props();
5427 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
34dc7c2f
BB
5428 nvlist_free(nvroot);
5429
428870ff 5430 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
9babb374 5431 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
34dc7c2f
BB
5432 spa_close(spa, FTAG);
5433
5434 kernel_fini();
428870ff
BB
5435
5436 ztest_run_zdb(zs->zs_pool);
5437
5438 ztest_freeze(zs);
5439
5440 ztest_run_zdb(zs->zs_pool);
572e2857
BB
5441
5442 (void) rwlock_destroy(&zs->zs_name_lock);
5443 (void) _mutex_destroy(&zs->zs_vdev_lock);
34dc7c2f
BB
5444}
5445
5446int
5447main(int argc, char **argv)
5448{
5449 int kills = 0;
5450 int iters = 0;
34dc7c2f 5451 ztest_shared_t *zs;
428870ff 5452 size_t shared_size;
34dc7c2f
BB
5453 ztest_info_t *zi;
5454 char timebuf[100];
5455 char numbuf[6];
428870ff 5456 spa_t *spa;
d6320ddb 5457 int i, f;
34dc7c2f
BB
5458
5459 (void) setvbuf(stdout, NULL, _IOLBF, 0);
5460
34dc7c2f
BB
5461 ztest_random_fd = open("/dev/urandom", O_RDONLY);
5462
5463 process_options(argc, argv);
5464
428870ff 5465 /* Override location of zpool.cache */
0e5b68e0
BB
5466 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
5467 zopt_dir) != -1);
428870ff 5468
34dc7c2f
BB
5469 /*
5470 * Blow away any existing copy of zpool.cache
5471 */
5472 if (zopt_init != 0)
428870ff
BB
5473 (void) remove(spa_config_path);
5474
5475 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
34dc7c2f
BB
5476
5477 zs = ztest_shared = (void *)mmap(0,
428870ff 5478 P2ROUNDUP(shared_size, getpagesize()),
34dc7c2f
BB
5479 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5480
5481 if (zopt_verbose >= 1) {
5482 (void) printf("%llu vdevs, %d datasets, %d threads,"
5483 " %llu seconds...\n",
5484 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5485 (u_longlong_t)zopt_time);
5486 }
5487
5488 /*
5489 * Create and initialize our storage pool.
5490 */
d6320ddb 5491 for (i = 1; i <= zopt_init; i++) {
34dc7c2f
BB
5492 bzero(zs, sizeof (ztest_shared_t));
5493 if (zopt_verbose >= 3 && zopt_init != 1)
5494 (void) printf("ztest_init(), pass %d\n", i);
428870ff
BB
5495 zs->zs_pool = zopt_pool;
5496 ztest_init(zs);
34dc7c2f
BB
5497 }
5498
428870ff
BB
5499 zs->zs_pool = zopt_pool;
5500 zs->zs_proc_start = gethrtime();
5501 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
34dc7c2f 5502
d6320ddb 5503 for (f = 0; f < ZTEST_FUNCS; f++) {
428870ff 5504 zi = &zs->zs_info[f];
34dc7c2f 5505 *zi = ztest_info[f];
428870ff
BB
5506 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5507 zi->zi_call_next = UINT64_MAX;
34dc7c2f 5508 else
428870ff
BB
5509 zi->zi_call_next = zs->zs_proc_start +
5510 ztest_random(2 * zi->zi_interval[0] + 1);
34dc7c2f
BB
5511 }
5512
34dc7c2f
BB
5513 /*
5514 * Run the tests in a loop. These tests include fault injection
5515 * to verify that self-healing data works, and forced crashes
5516 * to verify that we never lose on-disk consistency.
5517 */
428870ff 5518 while (gethrtime() < zs->zs_proc_stop) {
34dc7c2f
BB
5519 int status;
5520 pid_t pid;
34dc7c2f
BB
5521
5522 /*
5523 * Initialize the workload counters for each function.
5524 */
d6320ddb 5525 for (f = 0; f < ZTEST_FUNCS; f++) {
34dc7c2f 5526 zi = &zs->zs_info[f];
428870ff 5527 zi->zi_call_count = 0;
34dc7c2f
BB
5528 zi->zi_call_time = 0;
5529 }
5530
9babb374
BB
5531 /* Set the allocation switch size */
5532 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5533
34dc7c2f
BB
5534 pid = fork();
5535
5536 if (pid == -1)
5537 fatal(1, "fork failed");
5538
5539 if (pid == 0) { /* child */
5540 struct rlimit rl = { 1024, 1024 };
5541 (void) setrlimit(RLIMIT_NOFILE, &rl);
5542 (void) enable_extended_FILE_stdio(-1, -1);
428870ff 5543 ztest_run(zs);
34dc7c2f
BB
5544 exit(0);
5545 }
5546
5547 while (waitpid(pid, &status, 0) != pid)
5548 continue;
5549
5550 if (WIFEXITED(status)) {
5551 if (WEXITSTATUS(status) != 0) {
5552 (void) fprintf(stderr,
5553 "child exited with code %d\n",
5554 WEXITSTATUS(status));
5555 exit(2);
5556 }
5557 } else if (WIFSIGNALED(status)) {
5558 if (WTERMSIG(status) != SIGKILL) {
5559 (void) fprintf(stderr,
5560 "child died with signal %d\n",
5561 WTERMSIG(status));
5562 exit(3);
5563 }
5564 kills++;
5565 } else {
5566 (void) fprintf(stderr, "something strange happened "
5567 "to child\n");
5568 exit(4);
5569 }
5570
5571 iters++;
5572
5573 if (zopt_verbose >= 1) {
5574 hrtime_t now = gethrtime();
5575
428870ff
BB
5576 now = MIN(now, zs->zs_proc_stop);
5577 print_time(zs->zs_proc_stop - now, timebuf);
34dc7c2f
BB
5578 nicenum(zs->zs_space, numbuf);
5579
5580 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5581 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5582 iters,
5583 WIFEXITED(status) ? "Complete" : "SIGKILL",
5584 (u_longlong_t)zs->zs_enospc_count,
5585 100.0 * zs->zs_alloc / zs->zs_space,
5586 numbuf,
428870ff 5587 100.0 * (now - zs->zs_proc_start) /
34dc7c2f
BB
5588 (zopt_time * NANOSEC), timebuf);
5589 }
5590
5591 if (zopt_verbose >= 2) {
5592 (void) printf("\nWorkload summary:\n\n");
5593 (void) printf("%7s %9s %s\n",
5594 "Calls", "Time", "Function");
5595 (void) printf("%7s %9s %s\n",
5596 "-----", "----", "--------");
d6320ddb 5597 for (f = 0; f < ZTEST_FUNCS; f++) {
34dc7c2f
BB
5598 Dl_info dli;
5599
5600 zi = &zs->zs_info[f];
5601 print_time(zi->zi_call_time, timebuf);
5602 (void) dladdr((void *)zi->zi_func, &dli);
5603 (void) printf("%7llu %9s %s\n",
428870ff 5604 (u_longlong_t)zi->zi_call_count, timebuf,
34dc7c2f
BB
5605 dli.dli_sname);
5606 }
5607 (void) printf("\n");
5608 }
5609
5610 /*
428870ff
BB
5611 * It's possible that we killed a child during a rename test,
5612 * in which case we'll have a 'ztest_tmp' pool lying around
5613 * instead of 'ztest'. Do a blind rename in case this happened.
34dc7c2f 5614 */
428870ff
BB
5615 kernel_init(FREAD);
5616 if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5617 spa_close(spa, FTAG);
5618 } else {
5619 char tmpname[MAXNAMELEN];
5620 kernel_fini();
5621 kernel_init(FREAD | FWRITE);
5622 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5623 zopt_pool);
5624 (void) spa_rename(tmpname, zopt_pool);
5625 }
34dc7c2f 5626 kernel_fini();
34dc7c2f 5627
428870ff
BB
5628 ztest_run_zdb(zopt_pool);
5629 }
34dc7c2f
BB
5630
5631 if (zopt_verbose >= 1) {
5632 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5633 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5634 }
5635
5636 return (0);
5637}