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