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