1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
15 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
16 */
17
18 #include <sys/dmu.h>
19 #include <sys/zap.h>
20 #include <sys/zfs_context.h>
21 #include <sys/dsl_pool.h>
22 #include <sys/dsl_dataset.h>
23
24 /*
25 * Deadlist concurrency:
26 *
27 * Deadlists can only be modified from the syncing thread.
28 *
29 * Except for dsl_deadlist_insert(), it can only be modified with the
30 * dp_config_rwlock held with RW_WRITER.
31 *
32 * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can
33 * be called concurrently, from open context, with the dl_config_rwlock held
34 * with RW_READER.
35 *
36 * Therefore, we only need to provide locking between dsl_deadlist_insert() and
37 * the accessors, protecting:
38 * dl_phys->dl_used,comp,uncomp
39 * and protecting the dl_tree from being loaded.
40 * The locking is provided by dl_lock. Note that locking on the bpobj_t
41 * provides its own locking, and dl_oldfmt is immutable.
42 */
43
44 /*
45 * Livelist Overview
46 * ================
47 *
48 * Livelists use the same 'deadlist_t' struct as deadlists and are also used
49 * to track blkptrs over the lifetime of a dataset. Livelists however, belong
50 * to clones and track the blkptrs that are clone-specific (were born after
51 * the clone's creation). The exception is embedded block pointers which are
52 * not included in livelists because they do not need to be freed.
53 *
54 * When it comes time to delete the clone, the livelist provides a quick
55 * reference as to what needs to be freed. For this reason, livelists also track
56 * when clone-specific blkptrs are freed before deletion to prevent double
57 * frees. Each blkptr in a livelist is marked as a FREE or an ALLOC and the
58 * deletion algorithm iterates backwards over the livelist, matching
59 * FREE/ALLOC pairs and then freeing those ALLOCs which remain. livelists
60 * are also updated in the case when blkptrs are remapped: the old version
61 * of the blkptr is cancelled out with a FREE and the new version is tracked
62 * with an ALLOC.
63 *
64 * To bound the amount of memory required for deletion, livelists over a
65 * certain size are spread over multiple entries. Entries are grouped by
66 * birth txg so we can be sure the ALLOC/FREE pair for a given blkptr will
67 * be in the same entry. This allows us to delete livelists incrementally
68 * over multiple syncs, one entry at a time.
69 *
70 * During the lifetime of the clone, livelists can get extremely large.
71 * Their size is managed by periodic condensing (preemptively cancelling out
72 * FREE/ALLOC pairs). Livelists are disabled when a clone is promoted or when
73 * the shared space between the clone and its origin is so small that it
74 * doesn't make sense to use livelists anymore.
75 */
76
77 /*
78 * The threshold sublist size at which we create a new sub-livelist for the
79 * next txg. However, since blkptrs of the same transaction group must be in
80 * the same sub-list, the actual sublist size may exceed this. When picking the
81 * size we had to balance the fact that larger sublists mean fewer sublists
82 * (decreasing the cost of insertion) against the consideration that sublists
83 * will be loaded into memory and shouldn't take up an inordinate amount of
84 * space. We settled on ~500000 entries, corresponding to roughly 128M.
85 */
86 uint64_t zfs_livelist_max_entries = 500000;
87
88 /*
89 * We can approximate how much of a performance gain a livelist will give us
90 * based on the percentage of blocks shared between the clone and its origin.
91 * 0 percent shared means that the clone has completely diverged and that the
92 * old method is maximally effective: every read from the block tree will
93 * result in lots of frees. Livelists give us gains when they track blocks
94 * scattered across the tree, when one read in the old method might only
95 * result in a few frees. Once the clone has been overwritten enough,
96 * writes are no longer sparse and we'll no longer get much of a benefit from
97 * tracking them with a livelist. We chose a lower limit of 75 percent shared
98 * (25 percent overwritten). This means that 1/4 of all block pointers will be
99 * freed (e.g. each read frees 256, out of a max of 1024) so we expect livelists
100 * to make deletion 4x faster. Once the amount of shared space drops below this
101 * threshold, the clone will revert to the old deletion method.
102 */
103 int zfs_livelist_min_percent_shared = 75;
104
105 static int
dsl_deadlist_compare(const void * arg1,const void * arg2)106 dsl_deadlist_compare(const void *arg1, const void *arg2)
107 {
108 const dsl_deadlist_entry_t *dle1 = arg1;
109 const dsl_deadlist_entry_t *dle2 = arg2;
110
111 return (TREE_CMP(dle1->dle_mintxg, dle2->dle_mintxg));
112 }
113
114 static int
dsl_deadlist_cache_compare(const void * arg1,const void * arg2)115 dsl_deadlist_cache_compare(const void *arg1, const void *arg2)
116 {
117 const dsl_deadlist_cache_entry_t *dlce1 = arg1;
118 const dsl_deadlist_cache_entry_t *dlce2 = arg2;
119
120 return (TREE_CMP(dlce1->dlce_mintxg, dlce2->dlce_mintxg));
121 }
122
123 static void
dsl_deadlist_load_tree(dsl_deadlist_t * dl)124 dsl_deadlist_load_tree(dsl_deadlist_t *dl)
125 {
126 zap_cursor_t zc;
127 zap_attribute_t *za;
128 int error;
129
130 ASSERT(MUTEX_HELD(&dl->dl_lock));
131
132 ASSERT(!dl->dl_oldfmt);
133 if (dl->dl_havecache) {
134 /*
135 * After loading the tree, the caller may modify the tree,
136 * e.g. to add or remove nodes, or to make a node no longer
137 * refer to the empty_bpobj. These changes would make the
138 * dl_cache incorrect. Therefore we discard the cache here,
139 * so that it can't become incorrect.
140 */
141 dsl_deadlist_cache_entry_t *dlce;
142 void *cookie = NULL;
143 while ((dlce = avl_destroy_nodes(&dl->dl_cache, &cookie))
144 != NULL) {
145 kmem_free(dlce, sizeof (*dlce));
146 }
147 avl_destroy(&dl->dl_cache);
148 dl->dl_havecache = B_FALSE;
149 }
150 if (dl->dl_havetree)
151 return;
152
153 za = zap_attribute_alloc();
154 avl_create(&dl->dl_tree, dsl_deadlist_compare,
155 sizeof (dsl_deadlist_entry_t),
156 offsetof(dsl_deadlist_entry_t, dle_node));
157 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
158 (error = zap_cursor_retrieve(&zc, za)) == 0;
159 zap_cursor_advance(&zc)) {
160 dsl_deadlist_entry_t *dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
161 dle->dle_mintxg = zfs_strtonum(za->za_name, NULL);
162
163 /*
164 * Prefetch all the bpobj's so that we do that i/o
165 * in parallel. Then open them all in a second pass.
166 */
167 dle->dle_bpobj.bpo_object = za->za_first_integer;
168 dmu_prefetch_dnode(dl->dl_os, dle->dle_bpobj.bpo_object,
169 ZIO_PRIORITY_SYNC_READ);
170
171 avl_add(&dl->dl_tree, dle);
172 }
173 VERIFY3U(error, ==, ENOENT);
174 zap_cursor_fini(&zc);
175 zap_attribute_free(za);
176
177 for (dsl_deadlist_entry_t *dle = avl_first(&dl->dl_tree);
178 dle != NULL; dle = AVL_NEXT(&dl->dl_tree, dle)) {
179 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os,
180 dle->dle_bpobj.bpo_object));
181 }
182 dl->dl_havetree = B_TRUE;
183 }
184
185 /*
186 * Load only the non-empty bpobj's into the dl_cache. The cache is an analog
187 * of the dl_tree, but contains only non-empty_bpobj nodes from the ZAP. It
188 * is used only for gathering space statistics. The dl_cache has two
189 * advantages over the dl_tree:
190 *
191 * 1. Loading the dl_cache is ~5x faster than loading the dl_tree (if it's
192 * mostly empty_bpobj's), due to less CPU overhead to open the empty_bpobj
193 * many times and to inquire about its (zero) space stats many times.
194 *
195 * 2. The dl_cache uses less memory than the dl_tree. We only need to load
196 * the dl_tree of snapshots when deleting a snapshot, after which we free the
197 * dl_tree with dsl_deadlist_discard_tree
198 */
199 static void
dsl_deadlist_load_cache(dsl_deadlist_t * dl)200 dsl_deadlist_load_cache(dsl_deadlist_t *dl)
201 {
202 zap_cursor_t zc;
203 zap_attribute_t *za;
204 int error;
205
206 ASSERT(MUTEX_HELD(&dl->dl_lock));
207
208 ASSERT(!dl->dl_oldfmt);
209 if (dl->dl_havecache)
210 return;
211
212 uint64_t empty_bpobj = dmu_objset_pool(dl->dl_os)->dp_empty_bpobj;
213
214 avl_create(&dl->dl_cache, dsl_deadlist_cache_compare,
215 sizeof (dsl_deadlist_cache_entry_t),
216 offsetof(dsl_deadlist_cache_entry_t, dlce_node));
217 za = zap_attribute_alloc();
218 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
219 (error = zap_cursor_retrieve(&zc, za)) == 0;
220 zap_cursor_advance(&zc)) {
221 if (za->za_first_integer == empty_bpobj)
222 continue;
223 dsl_deadlist_cache_entry_t *dlce =
224 kmem_zalloc(sizeof (*dlce), KM_SLEEP);
225 dlce->dlce_mintxg = zfs_strtonum(za->za_name, NULL);
226
227 /*
228 * Prefetch all the bpobj's so that we do that i/o
229 * in parallel. Then open them all in a second pass.
230 */
231 dlce->dlce_bpobj = za->za_first_integer;
232 dmu_prefetch_dnode(dl->dl_os, dlce->dlce_bpobj,
233 ZIO_PRIORITY_SYNC_READ);
234 avl_add(&dl->dl_cache, dlce);
235 }
236 VERIFY3U(error, ==, ENOENT);
237 zap_cursor_fini(&zc);
238 zap_attribute_free(za);
239
240 for (dsl_deadlist_cache_entry_t *dlce = avl_first(&dl->dl_cache);
241 dlce != NULL; dlce = AVL_NEXT(&dl->dl_cache, dlce)) {
242 bpobj_t bpo;
243 VERIFY0(bpobj_open(&bpo, dl->dl_os, dlce->dlce_bpobj));
244
245 VERIFY0(bpobj_space(&bpo,
246 &dlce->dlce_bytes, &dlce->dlce_comp, &dlce->dlce_uncomp));
247 bpobj_close(&bpo);
248 }
249 dl->dl_havecache = B_TRUE;
250 }
251
252 /*
253 * Discard the tree to save memory.
254 */
255 void
dsl_deadlist_discard_tree(dsl_deadlist_t * dl)256 dsl_deadlist_discard_tree(dsl_deadlist_t *dl)
257 {
258 mutex_enter(&dl->dl_lock);
259
260 if (!dl->dl_havetree) {
261 mutex_exit(&dl->dl_lock);
262 return;
263 }
264 dsl_deadlist_entry_t *dle;
265 void *cookie = NULL;
266 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie)) != NULL) {
267 bpobj_close(&dle->dle_bpobj);
268 kmem_free(dle, sizeof (*dle));
269 }
270 avl_destroy(&dl->dl_tree);
271
272 dl->dl_havetree = B_FALSE;
273 mutex_exit(&dl->dl_lock);
274 }
275
276 void
dsl_deadlist_iterate(dsl_deadlist_t * dl,deadlist_iter_t func,void * args)277 dsl_deadlist_iterate(dsl_deadlist_t *dl, deadlist_iter_t func, void *args)
278 {
279 dsl_deadlist_entry_t *dle;
280
281 ASSERT(dsl_deadlist_is_open(dl));
282
283 mutex_enter(&dl->dl_lock);
284 dsl_deadlist_load_tree(dl);
285 mutex_exit(&dl->dl_lock);
286 for (dle = avl_first(&dl->dl_tree); dle != NULL;
287 dle = AVL_NEXT(&dl->dl_tree, dle)) {
288 if (func(args, dle) != 0)
289 break;
290 }
291 }
292
293 int
dsl_deadlist_open(dsl_deadlist_t * dl,objset_t * os,uint64_t object)294 dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
295 {
296 dmu_object_info_t doi;
297 int err;
298
299 ASSERT(!dsl_deadlist_is_open(dl));
300
301 mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
302 dl->dl_os = os;
303 dl->dl_object = object;
304 err = dmu_bonus_hold(os, object, dl, &dl->dl_dbuf);
305 if (err != 0)
306 return (err);
307 dmu_object_info_from_db(dl->dl_dbuf, &doi);
308 if (doi.doi_type == DMU_OT_BPOBJ) {
309 dmu_buf_rele(dl->dl_dbuf, dl);
310 dl->dl_dbuf = NULL;
311 dl->dl_oldfmt = B_TRUE;
312 return (bpobj_open(&dl->dl_bpobj, os, object));
313 }
314
315 dl->dl_oldfmt = B_FALSE;
316 dl->dl_phys = dl->dl_dbuf->db_data;
317 dl->dl_havetree = B_FALSE;
318 dl->dl_havecache = B_FALSE;
319 return (0);
320 }
321
322 boolean_t
dsl_deadlist_is_open(dsl_deadlist_t * dl)323 dsl_deadlist_is_open(dsl_deadlist_t *dl)
324 {
325 return (dl->dl_os != NULL);
326 }
327
328 void
dsl_deadlist_close(dsl_deadlist_t * dl)329 dsl_deadlist_close(dsl_deadlist_t *dl)
330 {
331 ASSERT(dsl_deadlist_is_open(dl));
332 mutex_destroy(&dl->dl_lock);
333
334 if (dl->dl_oldfmt) {
335 dl->dl_oldfmt = B_FALSE;
336 bpobj_close(&dl->dl_bpobj);
337 dl->dl_os = NULL;
338 dl->dl_object = 0;
339 return;
340 }
341
342 if (dl->dl_havetree) {
343 dsl_deadlist_entry_t *dle;
344 void *cookie = NULL;
345 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
346 != NULL) {
347 bpobj_close(&dle->dle_bpobj);
348 kmem_free(dle, sizeof (*dle));
349 }
350 avl_destroy(&dl->dl_tree);
351 }
352 if (dl->dl_havecache) {
353 dsl_deadlist_cache_entry_t *dlce;
354 void *cookie = NULL;
355 while ((dlce = avl_destroy_nodes(&dl->dl_cache, &cookie))
356 != NULL) {
357 kmem_free(dlce, sizeof (*dlce));
358 }
359 avl_destroy(&dl->dl_cache);
360 }
361 dmu_buf_rele(dl->dl_dbuf, dl);
362 dl->dl_dbuf = NULL;
363 dl->dl_phys = NULL;
364 dl->dl_os = NULL;
365 dl->dl_object = 0;
366 }
367
368 uint64_t
dsl_deadlist_alloc(objset_t * os,dmu_tx_t * tx)369 dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
370 {
371 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
372 return (bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx));
373 return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR,
374 sizeof (dsl_deadlist_phys_t), tx));
375 }
376
377 void
dsl_deadlist_free(objset_t * os,uint64_t dlobj,dmu_tx_t * tx)378 dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
379 {
380 dmu_object_info_t doi;
381 zap_cursor_t zc;
382 zap_attribute_t *za;
383 int error;
384
385 VERIFY0(dmu_object_info(os, dlobj, &doi));
386 if (doi.doi_type == DMU_OT_BPOBJ) {
387 bpobj_free(os, dlobj, tx);
388 return;
389 }
390
391 za = zap_attribute_alloc();
392 for (zap_cursor_init(&zc, os, dlobj);
393 (error = zap_cursor_retrieve(&zc, za)) == 0;
394 zap_cursor_advance(&zc)) {
395 uint64_t obj = za->za_first_integer;
396 if (obj == dmu_objset_pool(os)->dp_empty_bpobj)
397 bpobj_decr_empty(os, tx);
398 else
399 bpobj_free(os, obj, tx);
400 }
401 VERIFY3U(error, ==, ENOENT);
402 zap_cursor_fini(&zc);
403 zap_attribute_free(za);
404 VERIFY0(dmu_object_free(os, dlobj, tx));
405 }
406
407 static void
dle_enqueue(dsl_deadlist_t * dl,dsl_deadlist_entry_t * dle,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)408 dle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
409 const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx)
410 {
411 ASSERT(MUTEX_HELD(&dl->dl_lock));
412 if (dle->dle_bpobj.bpo_object ==
413 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
414 uint64_t obj = bpobj_alloc(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
415 bpobj_close(&dle->dle_bpobj);
416 bpobj_decr_empty(dl->dl_os, tx);
417 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
418 VERIFY0(zap_update_int_key(dl->dl_os, dl->dl_object,
419 dle->dle_mintxg, obj, tx));
420 }
421 bpobj_enqueue(&dle->dle_bpobj, bp, bp_freed, tx);
422 }
423
424 static void
dle_enqueue_subobj(dsl_deadlist_t * dl,dsl_deadlist_entry_t * dle,uint64_t obj,dmu_tx_t * tx)425 dle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
426 uint64_t obj, dmu_tx_t *tx)
427 {
428 ASSERT(MUTEX_HELD(&dl->dl_lock));
429 if (dle->dle_bpobj.bpo_object !=
430 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
431 bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
432 } else {
433 bpobj_close(&dle->dle_bpobj);
434 bpobj_decr_empty(dl->dl_os, tx);
435 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
436 VERIFY0(zap_update_int_key(dl->dl_os, dl->dl_object,
437 dle->dle_mintxg, obj, tx));
438 }
439 }
440
441 /*
442 * Prefetch metadata required for dle_enqueue_subobj().
443 */
444 static void
dle_prefetch_subobj(dsl_deadlist_t * dl,dsl_deadlist_entry_t * dle,uint64_t obj)445 dle_prefetch_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
446 uint64_t obj)
447 {
448 if (dle->dle_bpobj.bpo_object !=
449 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj)
450 bpobj_prefetch_subobj(&dle->dle_bpobj, obj);
451 }
452
453 void
dsl_deadlist_insert(dsl_deadlist_t * dl,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)454 dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, boolean_t bp_freed,
455 dmu_tx_t *tx)
456 {
457 dsl_deadlist_entry_t dle_tofind;
458 dsl_deadlist_entry_t *dle;
459 avl_index_t where;
460
461 if (dl->dl_oldfmt) {
462 bpobj_enqueue(&dl->dl_bpobj, bp, bp_freed, tx);
463 return;
464 }
465
466 mutex_enter(&dl->dl_lock);
467 dsl_deadlist_load_tree(dl);
468
469 dmu_buf_will_dirty(dl->dl_dbuf, tx);
470
471 int sign = bp_freed ? -1 : +1;
472 dl->dl_phys->dl_used +=
473 sign * bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
474 dl->dl_phys->dl_comp += sign * BP_GET_PSIZE(bp);
475 dl->dl_phys->dl_uncomp += sign * BP_GET_UCSIZE(bp);
476
477 dle_tofind.dle_mintxg = BP_GET_BIRTH(bp);
478 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
479 if (dle == NULL)
480 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
481 else
482 dle = AVL_PREV(&dl->dl_tree, dle);
483
484 if (dle == NULL) {
485 zfs_panic_recover("blkptr at %p has invalid BLK_BIRTH %llu",
486 bp, (longlong_t)BP_GET_BIRTH(bp));
487 dle = avl_first(&dl->dl_tree);
488 }
489
490 ASSERT3P(dle, !=, NULL);
491 dle_enqueue(dl, dle, bp, bp_freed, tx);
492 mutex_exit(&dl->dl_lock);
493 }
494
495 int
dsl_deadlist_insert_alloc_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)496 dsl_deadlist_insert_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
497 {
498 dsl_deadlist_t *dl = arg;
499 dsl_deadlist_insert(dl, bp, B_FALSE, tx);
500 return (0);
501 }
502
503 int
dsl_deadlist_insert_free_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)504 dsl_deadlist_insert_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
505 {
506 dsl_deadlist_t *dl = arg;
507 dsl_deadlist_insert(dl, bp, B_TRUE, tx);
508 return (0);
509 }
510
511 /*
512 * Insert new key in deadlist, which must be > all current entries.
513 * mintxg is not inclusive.
514 */
515 void
dsl_deadlist_add_key(dsl_deadlist_t * dl,uint64_t mintxg,dmu_tx_t * tx)516 dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
517 {
518 uint64_t obj;
519 dsl_deadlist_entry_t *dle;
520
521 if (dl->dl_oldfmt)
522 return;
523
524 dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
525 dle->dle_mintxg = mintxg;
526
527 mutex_enter(&dl->dl_lock);
528 dsl_deadlist_load_tree(dl);
529
530 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
531 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
532 avl_add(&dl->dl_tree, dle);
533
534 VERIFY0(zap_add_int_key(dl->dl_os, dl->dl_object,
535 mintxg, obj, tx));
536 mutex_exit(&dl->dl_lock);
537 }
538
539 /*
540 * Remove this key, merging its entries into the previous key.
541 */
542 void
dsl_deadlist_remove_key(dsl_deadlist_t * dl,uint64_t mintxg,dmu_tx_t * tx)543 dsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
544 {
545 dsl_deadlist_entry_t dle_tofind;
546 dsl_deadlist_entry_t *dle, *dle_prev;
547
548 if (dl->dl_oldfmt)
549 return;
550 mutex_enter(&dl->dl_lock);
551 dsl_deadlist_load_tree(dl);
552
553 dle_tofind.dle_mintxg = mintxg;
554 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
555 ASSERT3P(dle, !=, NULL);
556 dle_prev = AVL_PREV(&dl->dl_tree, dle);
557 ASSERT3P(dle_prev, !=, NULL);
558
559 dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx);
560
561 avl_remove(&dl->dl_tree, dle);
562 bpobj_close(&dle->dle_bpobj);
563 kmem_free(dle, sizeof (*dle));
564
565 VERIFY0(zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
566 mutex_exit(&dl->dl_lock);
567 }
568
569 /*
570 * Remove a deadlist entry and all of its contents by removing the entry from
571 * the deadlist's avl tree, freeing the entry's bpobj and adjusting the
572 * deadlist's space accounting accordingly.
573 */
574 void
dsl_deadlist_remove_entry(dsl_deadlist_t * dl,uint64_t mintxg,dmu_tx_t * tx)575 dsl_deadlist_remove_entry(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
576 {
577 uint64_t used, comp, uncomp;
578 dsl_deadlist_entry_t dle_tofind;
579 dsl_deadlist_entry_t *dle;
580 objset_t *os = dl->dl_os;
581
582 if (dl->dl_oldfmt)
583 return;
584
585 mutex_enter(&dl->dl_lock);
586 dsl_deadlist_load_tree(dl);
587
588 dle_tofind.dle_mintxg = mintxg;
589 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
590 VERIFY3P(dle, !=, NULL);
591
592 avl_remove(&dl->dl_tree, dle);
593 VERIFY0(zap_remove_int(os, dl->dl_object, mintxg, tx));
594 VERIFY0(bpobj_space(&dle->dle_bpobj, &used, &comp, &uncomp));
595 dmu_buf_will_dirty(dl->dl_dbuf, tx);
596 dl->dl_phys->dl_used -= used;
597 dl->dl_phys->dl_comp -= comp;
598 dl->dl_phys->dl_uncomp -= uncomp;
599 if (dle->dle_bpobj.bpo_object == dmu_objset_pool(os)->dp_empty_bpobj) {
600 bpobj_decr_empty(os, tx);
601 } else {
602 bpobj_free(os, dle->dle_bpobj.bpo_object, tx);
603 }
604 bpobj_close(&dle->dle_bpobj);
605 kmem_free(dle, sizeof (*dle));
606 mutex_exit(&dl->dl_lock);
607 }
608
609 /*
610 * Clear out the contents of a deadlist_entry by freeing its bpobj,
611 * replacing it with an empty bpobj and adjusting the deadlist's
612 * space accounting
613 */
614 void
dsl_deadlist_clear_entry(dsl_deadlist_entry_t * dle,dsl_deadlist_t * dl,dmu_tx_t * tx)615 dsl_deadlist_clear_entry(dsl_deadlist_entry_t *dle, dsl_deadlist_t *dl,
616 dmu_tx_t *tx)
617 {
618 uint64_t new_obj, used, comp, uncomp;
619 objset_t *os = dl->dl_os;
620
621 mutex_enter(&dl->dl_lock);
622 VERIFY0(zap_remove_int(os, dl->dl_object, dle->dle_mintxg, tx));
623 VERIFY0(bpobj_space(&dle->dle_bpobj, &used, &comp, &uncomp));
624 dmu_buf_will_dirty(dl->dl_dbuf, tx);
625 dl->dl_phys->dl_used -= used;
626 dl->dl_phys->dl_comp -= comp;
627 dl->dl_phys->dl_uncomp -= uncomp;
628 if (dle->dle_bpobj.bpo_object == dmu_objset_pool(os)->dp_empty_bpobj)
629 bpobj_decr_empty(os, tx);
630 else
631 bpobj_free(os, dle->dle_bpobj.bpo_object, tx);
632 bpobj_close(&dle->dle_bpobj);
633 new_obj = bpobj_alloc_empty(os, SPA_OLD_MAXBLOCKSIZE, tx);
634 VERIFY0(bpobj_open(&dle->dle_bpobj, os, new_obj));
635 VERIFY0(zap_add_int_key(os, dl->dl_object, dle->dle_mintxg,
636 new_obj, tx));
637 ASSERT(bpobj_is_empty(&dle->dle_bpobj));
638 mutex_exit(&dl->dl_lock);
639 }
640
641 /*
642 * Return the first entry in deadlist's avl tree
643 */
644 dsl_deadlist_entry_t *
dsl_deadlist_first(dsl_deadlist_t * dl)645 dsl_deadlist_first(dsl_deadlist_t *dl)
646 {
647 dsl_deadlist_entry_t *dle;
648
649 mutex_enter(&dl->dl_lock);
650 dsl_deadlist_load_tree(dl);
651 dle = avl_first(&dl->dl_tree);
652 mutex_exit(&dl->dl_lock);
653
654 return (dle);
655 }
656
657 /*
658 * Return the last entry in deadlist's avl tree
659 */
660 dsl_deadlist_entry_t *
dsl_deadlist_last(dsl_deadlist_t * dl)661 dsl_deadlist_last(dsl_deadlist_t *dl)
662 {
663 dsl_deadlist_entry_t *dle;
664
665 mutex_enter(&dl->dl_lock);
666 dsl_deadlist_load_tree(dl);
667 dle = avl_last(&dl->dl_tree);
668 mutex_exit(&dl->dl_lock);
669
670 return (dle);
671 }
672
673 /*
674 * Walk ds's snapshots to regenerate generate ZAP & AVL.
675 */
676 static void
dsl_deadlist_regenerate(objset_t * os,uint64_t dlobj,uint64_t mrs_obj,dmu_tx_t * tx)677 dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
678 uint64_t mrs_obj, dmu_tx_t *tx)
679 {
680 dsl_deadlist_t dl = { 0 };
681 dsl_pool_t *dp = dmu_objset_pool(os);
682
683 VERIFY0(dsl_deadlist_open(&dl, os, dlobj));
684 if (dl.dl_oldfmt) {
685 dsl_deadlist_close(&dl);
686 return;
687 }
688
689 while (mrs_obj != 0) {
690 dsl_dataset_t *ds;
691 VERIFY0(dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
692 dsl_deadlist_add_key(&dl,
693 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
694 mrs_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
695 dsl_dataset_rele(ds, FTAG);
696 }
697 dsl_deadlist_close(&dl);
698 }
699
700 uint64_t
dsl_deadlist_clone(dsl_deadlist_t * dl,uint64_t maxtxg,uint64_t mrs_obj,dmu_tx_t * tx)701 dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
702 uint64_t mrs_obj, dmu_tx_t *tx)
703 {
704 dsl_deadlist_entry_t *dle;
705 uint64_t newobj;
706
707 newobj = dsl_deadlist_alloc(dl->dl_os, tx);
708
709 if (dl->dl_oldfmt) {
710 dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
711 return (newobj);
712 }
713
714 mutex_enter(&dl->dl_lock);
715 dsl_deadlist_load_tree(dl);
716
717 for (dle = avl_first(&dl->dl_tree); dle;
718 dle = AVL_NEXT(&dl->dl_tree, dle)) {
719 uint64_t obj;
720
721 if (dle->dle_mintxg >= maxtxg)
722 break;
723
724 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
725 VERIFY0(zap_add_int_key(dl->dl_os, newobj,
726 dle->dle_mintxg, obj, tx));
727 }
728 mutex_exit(&dl->dl_lock);
729 return (newobj);
730 }
731
732 void
dsl_deadlist_space(dsl_deadlist_t * dl,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)733 dsl_deadlist_space(dsl_deadlist_t *dl,
734 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
735 {
736 ASSERT(dsl_deadlist_is_open(dl));
737 if (dl->dl_oldfmt) {
738 VERIFY0(bpobj_space(&dl->dl_bpobj,
739 usedp, compp, uncompp));
740 return;
741 }
742
743 mutex_enter(&dl->dl_lock);
744 *usedp = dl->dl_phys->dl_used;
745 *compp = dl->dl_phys->dl_comp;
746 *uncompp = dl->dl_phys->dl_uncomp;
747 mutex_exit(&dl->dl_lock);
748 }
749
750 /*
751 * return space used in the range (mintxg, maxtxg].
752 * Includes maxtxg, does not include mintxg.
753 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
754 * UINT64_MAX).
755 */
756 void
dsl_deadlist_space_range(dsl_deadlist_t * dl,uint64_t mintxg,uint64_t maxtxg,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)757 dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg,
758 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
759 {
760 dsl_deadlist_cache_entry_t *dlce;
761 dsl_deadlist_cache_entry_t dlce_tofind;
762 avl_index_t where;
763
764 if (dl->dl_oldfmt) {
765 VERIFY0(bpobj_space_range(&dl->dl_bpobj,
766 mintxg, maxtxg, usedp, compp, uncompp));
767 return;
768 }
769
770 *usedp = *compp = *uncompp = 0;
771
772 mutex_enter(&dl->dl_lock);
773 dsl_deadlist_load_cache(dl);
774 dlce_tofind.dlce_mintxg = mintxg;
775 dlce = avl_find(&dl->dl_cache, &dlce_tofind, &where);
776
777 /*
778 * If this mintxg doesn't exist, it may be an empty_bpobj which
779 * is omitted from the sparse tree. Start at the next non-empty
780 * entry.
781 */
782 if (dlce == NULL)
783 dlce = avl_nearest(&dl->dl_cache, where, AVL_AFTER);
784
785 for (; dlce && dlce->dlce_mintxg < maxtxg;
786 dlce = AVL_NEXT(&dl->dl_tree, dlce)) {
787 *usedp += dlce->dlce_bytes;
788 *compp += dlce->dlce_comp;
789 *uncompp += dlce->dlce_uncomp;
790 }
791
792 mutex_exit(&dl->dl_lock);
793 }
794
795 static void
dsl_deadlist_insert_bpobj(dsl_deadlist_t * dl,uint64_t obj,uint64_t birth,dmu_tx_t * tx)796 dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
797 dmu_tx_t *tx)
798 {
799 dsl_deadlist_entry_t dle_tofind;
800 dsl_deadlist_entry_t *dle;
801 avl_index_t where;
802 uint64_t used, comp, uncomp;
803 bpobj_t bpo;
804
805 ASSERT(MUTEX_HELD(&dl->dl_lock));
806
807 VERIFY0(bpobj_open(&bpo, dl->dl_os, obj));
808 VERIFY0(bpobj_space(&bpo, &used, &comp, &uncomp));
809 bpobj_close(&bpo);
810
811 dsl_deadlist_load_tree(dl);
812
813 dmu_buf_will_dirty(dl->dl_dbuf, tx);
814 dl->dl_phys->dl_used += used;
815 dl->dl_phys->dl_comp += comp;
816 dl->dl_phys->dl_uncomp += uncomp;
817
818 dle_tofind.dle_mintxg = birth;
819 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
820 if (dle == NULL)
821 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
822 dle_enqueue_subobj(dl, dle, obj, tx);
823 }
824
825 /*
826 * Prefetch metadata required for dsl_deadlist_insert_bpobj().
827 */
828 static void
dsl_deadlist_prefetch_bpobj(dsl_deadlist_t * dl,uint64_t obj,uint64_t birth)829 dsl_deadlist_prefetch_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth)
830 {
831 dsl_deadlist_entry_t dle_tofind;
832 dsl_deadlist_entry_t *dle;
833 avl_index_t where;
834
835 ASSERT(MUTEX_HELD(&dl->dl_lock));
836
837 dsl_deadlist_load_tree(dl);
838
839 dle_tofind.dle_mintxg = birth;
840 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
841 if (dle == NULL)
842 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
843 dle_prefetch_subobj(dl, dle, obj);
844 }
845
846 static int
dsl_deadlist_insert_cb(void * arg,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)847 dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
848 dmu_tx_t *tx)
849 {
850 dsl_deadlist_t *dl = arg;
851 dsl_deadlist_insert(dl, bp, bp_freed, tx);
852 return (0);
853 }
854
855 /*
856 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
857 * an empty deadlist.
858 */
859 void
dsl_deadlist_merge(dsl_deadlist_t * dl,uint64_t obj,dmu_tx_t * tx)860 dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
861 {
862 zap_cursor_t zc, pzc;
863 zap_attribute_t *za, *pza;
864 dmu_buf_t *bonus;
865 dsl_deadlist_phys_t *dlp;
866 dmu_object_info_t doi;
867 int error, perror, i;
868
869 VERIFY0(dmu_object_info(dl->dl_os, obj, &doi));
870 if (doi.doi_type == DMU_OT_BPOBJ) {
871 bpobj_t bpo;
872 VERIFY0(bpobj_open(&bpo, dl->dl_os, obj));
873 VERIFY0(bpobj_iterate(&bpo, dsl_deadlist_insert_cb, dl, tx));
874 bpobj_close(&bpo);
875 return;
876 }
877
878 za = zap_attribute_alloc();
879 pza = zap_attribute_alloc();
880
881 mutex_enter(&dl->dl_lock);
882 /*
883 * Prefetch up to 128 deadlists first and then more as we progress.
884 * The limit is a balance between ARC use and diminishing returns.
885 */
886 for (zap_cursor_init(&pzc, dl->dl_os, obj), i = 0;
887 (perror = zap_cursor_retrieve(&pzc, pza)) == 0 && i < 128;
888 zap_cursor_advance(&pzc), i++) {
889 dsl_deadlist_prefetch_bpobj(dl, pza->za_first_integer,
890 zfs_strtonum(pza->za_name, NULL));
891 }
892 for (zap_cursor_init(&zc, dl->dl_os, obj);
893 (error = zap_cursor_retrieve(&zc, za)) == 0;
894 zap_cursor_advance(&zc)) {
895 dsl_deadlist_insert_bpobj(dl, za->za_first_integer,
896 zfs_strtonum(za->za_name, NULL), tx);
897 VERIFY0(zap_remove(dl->dl_os, obj, za->za_name, tx));
898 if (perror == 0) {
899 dsl_deadlist_prefetch_bpobj(dl, pza->za_first_integer,
900 zfs_strtonum(pza->za_name, NULL));
901 zap_cursor_advance(&pzc);
902 perror = zap_cursor_retrieve(&pzc, pza);
903 }
904 }
905 VERIFY3U(error, ==, ENOENT);
906 zap_cursor_fini(&zc);
907 zap_cursor_fini(&pzc);
908
909 VERIFY0(dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
910 dlp = bonus->db_data;
911 dmu_buf_will_dirty(bonus, tx);
912 memset(dlp, 0, sizeof (*dlp));
913 dmu_buf_rele(bonus, FTAG);
914 mutex_exit(&dl->dl_lock);
915
916 zap_attribute_free(za);
917 zap_attribute_free(pza);
918 }
919
920 /*
921 * Remove entries on dl that are born > mintxg, and put them on the bpobj.
922 */
923 void
dsl_deadlist_move_bpobj(dsl_deadlist_t * dl,bpobj_t * bpo,uint64_t mintxg,dmu_tx_t * tx)924 dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
925 dmu_tx_t *tx)
926 {
927 dsl_deadlist_entry_t dle_tofind;
928 dsl_deadlist_entry_t *dle, *pdle;
929 avl_index_t where;
930 int i;
931
932 ASSERT(!dl->dl_oldfmt);
933
934 mutex_enter(&dl->dl_lock);
935 dmu_buf_will_dirty(dl->dl_dbuf, tx);
936 dsl_deadlist_load_tree(dl);
937
938 dle_tofind.dle_mintxg = mintxg;
939 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
940 if (dle == NULL)
941 dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
942 /*
943 * Prefetch up to 128 deadlists first and then more as we progress.
944 * The limit is a balance between ARC use and diminishing returns.
945 */
946 for (pdle = dle, i = 0; pdle && i < 128; i++) {
947 bpobj_prefetch_subobj(bpo, pdle->dle_bpobj.bpo_object);
948 pdle = AVL_NEXT(&dl->dl_tree, pdle);
949 }
950 while (dle) {
951 uint64_t used, comp, uncomp;
952 dsl_deadlist_entry_t *dle_next;
953
954 bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
955 if (pdle) {
956 bpobj_prefetch_subobj(bpo, pdle->dle_bpobj.bpo_object);
957 pdle = AVL_NEXT(&dl->dl_tree, pdle);
958 }
959
960 VERIFY0(bpobj_space(&dle->dle_bpobj,
961 &used, &comp, &uncomp));
962 ASSERT3U(dl->dl_phys->dl_used, >=, used);
963 ASSERT3U(dl->dl_phys->dl_comp, >=, comp);
964 ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);
965 dl->dl_phys->dl_used -= used;
966 dl->dl_phys->dl_comp -= comp;
967 dl->dl_phys->dl_uncomp -= uncomp;
968
969 VERIFY0(zap_remove_int(dl->dl_os, dl->dl_object,
970 dle->dle_mintxg, tx));
971
972 dle_next = AVL_NEXT(&dl->dl_tree, dle);
973 avl_remove(&dl->dl_tree, dle);
974 bpobj_close(&dle->dle_bpobj);
975 kmem_free(dle, sizeof (*dle));
976 dle = dle_next;
977 }
978 mutex_exit(&dl->dl_lock);
979 }
980
981 typedef struct livelist_entry {
982 blkptr_t le_bp;
983 uint32_t le_refcnt;
984 avl_node_t le_node;
985 } livelist_entry_t;
986
987 static int
livelist_compare(const void * larg,const void * rarg)988 livelist_compare(const void *larg, const void *rarg)
989 {
990 const blkptr_t *l = &((livelist_entry_t *)larg)->le_bp;
991 const blkptr_t *r = &((livelist_entry_t *)rarg)->le_bp;
992
993 /* Sort them according to dva[0] */
994 int cmp = TREE_CMP(DVA_GET_VDEV(&l->blk_dva[0]),
995 DVA_GET_VDEV(&r->blk_dva[0]));
996 if (cmp != 0)
997 return (cmp);
998
999 /* if vdevs are equal, sort by offsets. */
1000 return (TREE_CMP(DVA_GET_OFFSET(&l->blk_dva[0]),
1001 DVA_GET_OFFSET(&r->blk_dva[0])));
1002 }
1003
1004 struct livelist_iter_arg {
1005 avl_tree_t *avl;
1006 bplist_t *to_free;
1007 zthr_t *t;
1008 };
1009
1010 /*
1011 * Expects an AVL tree which is incrementally filled will FREE blkptrs
1012 * and used to match up ALLOC/FREE pairs. ALLOC'd blkptrs without a
1013 * corresponding FREE are stored in the supplied bplist.
1014 *
1015 * Note that multiple FREE and ALLOC entries for the same blkptr may be
1016 * encountered when dedup or block cloning is involved. For this reason we
1017 * keep a refcount for all the FREE entries of each blkptr and ensure that
1018 * each of those FREE entries has a corresponding ALLOC preceding it.
1019 */
1020 static int
dsl_livelist_iterate(void * arg,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)1021 dsl_livelist_iterate(void *arg, const blkptr_t *bp, boolean_t bp_freed,
1022 dmu_tx_t *tx)
1023 {
1024 struct livelist_iter_arg *lia = arg;
1025 avl_tree_t *avl = lia->avl;
1026 bplist_t *to_free = lia->to_free;
1027 zthr_t *t = lia->t;
1028 ASSERT0P(tx);
1029
1030 if ((t != NULL) && (zthr_has_waiters(t) || zthr_iscancelled(t)))
1031 return (SET_ERROR(EINTR));
1032
1033 livelist_entry_t node;
1034 node.le_bp = *bp;
1035 livelist_entry_t *found = avl_find(avl, &node, NULL);
1036 if (found) {
1037 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(&found->le_bp));
1038 ASSERT3U(BP_GET_CHECKSUM(bp), ==,
1039 BP_GET_CHECKSUM(&found->le_bp));
1040 ASSERT3U(BP_GET_PHYSICAL_BIRTH(bp), ==,
1041 BP_GET_PHYSICAL_BIRTH(&found->le_bp));
1042 }
1043 if (bp_freed) {
1044 if (found == NULL) {
1045 /* first free entry for this blkptr */
1046 livelist_entry_t *e =
1047 kmem_alloc(sizeof (livelist_entry_t), KM_SLEEP);
1048 e->le_bp = *bp;
1049 e->le_refcnt = 1;
1050 avl_add(avl, e);
1051 } else {
1052 /*
1053 * Deduped or cloned block free. We could assert D bit
1054 * for dedup, but there is no such one for cloning.
1055 */
1056 ASSERT3U(found->le_refcnt + 1, >, found->le_refcnt);
1057 found->le_refcnt++;
1058 }
1059 } else {
1060 if (found == NULL) {
1061 /* block is currently marked as allocated */
1062 bplist_append(to_free, bp);
1063 } else {
1064 /* alloc matches a free entry */
1065 ASSERT3U(found->le_refcnt, !=, 0);
1066 found->le_refcnt--;
1067 if (found->le_refcnt == 0) {
1068 /* all tracked free pairs have been matched */
1069 avl_remove(avl, found);
1070 kmem_free(found, sizeof (livelist_entry_t));
1071 }
1072 }
1073 }
1074 return (0);
1075 }
1076
1077 /*
1078 * Accepts a bpobj and a bplist. Will insert into the bplist the blkptrs
1079 * which have an ALLOC entry but no matching FREE
1080 */
1081 int
dsl_process_sub_livelist(bpobj_t * bpobj,bplist_t * to_free,zthr_t * t,uint64_t * size)1082 dsl_process_sub_livelist(bpobj_t *bpobj, bplist_t *to_free, zthr_t *t,
1083 uint64_t *size)
1084 {
1085 avl_tree_t avl;
1086 avl_create(&avl, livelist_compare, sizeof (livelist_entry_t),
1087 offsetof(livelist_entry_t, le_node));
1088
1089 /* process the sublist */
1090 struct livelist_iter_arg arg = {
1091 .avl = &avl,
1092 .to_free = to_free,
1093 .t = t
1094 };
1095 int err = bpobj_iterate_nofree(bpobj, dsl_livelist_iterate, &arg, size);
1096 VERIFY(err != 0 || avl_numnodes(&avl) == 0);
1097
1098 void *cookie = NULL;
1099 livelist_entry_t *le = NULL;
1100 while ((le = avl_destroy_nodes(&avl, &cookie)) != NULL) {
1101 kmem_free(le, sizeof (livelist_entry_t));
1102 }
1103 avl_destroy(&avl);
1104 return (err);
1105 }
1106
1107 ZFS_MODULE_PARAM(zfs_livelist, zfs_livelist_, max_entries, U64, ZMOD_RW,
1108 "Size to start the next sub-livelist in a livelist");
1109
1110 ZFS_MODULE_PARAM(zfs_livelist, zfs_livelist_, min_percent_shared, INT, ZMOD_RW,
1111 "Threshold at which livelist is disabled");
1112