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