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