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