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.
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.
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]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 #include <sys/dsl_dataset.h>
29 #include <sys/refcount.h>
31 #include <sys/zfs_context.h>
32 #include <sys/dsl_pool.h>
35 * Deadlist concurrency:
37 * Deadlists can only be modified from the syncing thread.
39 * Except for dsl_deadlist_insert(), it can only be modified with the
40 * dp_config_rwlock held with RW_WRITER.
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
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.
55 dsl_deadlist_compare(const void *arg1
, const void *arg2
)
57 const dsl_deadlist_entry_t
*dle1
= (const dsl_deadlist_entry_t
*)arg1
;
58 const dsl_deadlist_entry_t
*dle2
= (const dsl_deadlist_entry_t
*)arg2
;
60 return (AVL_CMP(dle1
->dle_mintxg
, dle2
->dle_mintxg
));
64 dsl_deadlist_load_tree(dsl_deadlist_t
*dl
)
69 ASSERT(MUTEX_HELD(&dl
->dl_lock
));
71 ASSERT(!dl
->dl_oldfmt
);
75 avl_create(&dl
->dl_tree
, dsl_deadlist_compare
,
76 sizeof (dsl_deadlist_entry_t
),
77 offsetof(dsl_deadlist_entry_t
, dle_node
));
78 for (zap_cursor_init(&zc
, dl
->dl_os
, dl
->dl_object
);
79 zap_cursor_retrieve(&zc
, &za
) == 0;
80 zap_cursor_advance(&zc
)) {
81 dsl_deadlist_entry_t
*dle
= kmem_alloc(sizeof (*dle
), KM_SLEEP
);
82 dle
->dle_mintxg
= zfs_strtonum(za
.za_name
, NULL
);
83 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
,
84 za
.za_first_integer
));
85 avl_add(&dl
->dl_tree
, dle
);
88 dl
->dl_havetree
= B_TRUE
;
92 dsl_deadlist_open(dsl_deadlist_t
*dl
, objset_t
*os
, uint64_t object
)
94 dmu_object_info_t doi
;
96 mutex_init(&dl
->dl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
98 dl
->dl_object
= object
;
99 VERIFY3U(0, ==, dmu_bonus_hold(os
, object
, dl
, &dl
->dl_dbuf
));
100 dmu_object_info_from_db(dl
->dl_dbuf
, &doi
);
101 if (doi
.doi_type
== DMU_OT_BPOBJ
) {
102 dmu_buf_rele(dl
->dl_dbuf
, dl
);
104 dl
->dl_oldfmt
= B_TRUE
;
105 VERIFY3U(0, ==, bpobj_open(&dl
->dl_bpobj
, os
, object
));
109 dl
->dl_oldfmt
= B_FALSE
;
110 dl
->dl_phys
= dl
->dl_dbuf
->db_data
;
111 dl
->dl_havetree
= B_FALSE
;
115 dsl_deadlist_close(dsl_deadlist_t
*dl
)
118 dsl_deadlist_entry_t
*dle
;
121 mutex_destroy(&dl
->dl_lock
);
124 dl
->dl_oldfmt
= B_FALSE
;
125 bpobj_close(&dl
->dl_bpobj
);
129 if (dl
->dl_havetree
) {
130 while ((dle
= avl_destroy_nodes(&dl
->dl_tree
, &cookie
))
132 bpobj_close(&dle
->dle_bpobj
);
133 kmem_free(dle
, sizeof (*dle
));
135 avl_destroy(&dl
->dl_tree
);
137 dmu_buf_rele(dl
->dl_dbuf
, dl
);
143 dsl_deadlist_alloc(objset_t
*os
, dmu_tx_t
*tx
)
145 if (spa_version(dmu_objset_spa(os
)) < SPA_VERSION_DEADLISTS
)
146 return (bpobj_alloc(os
, SPA_OLD_MAXBLOCKSIZE
, tx
));
147 return (zap_create(os
, DMU_OT_DEADLIST
, DMU_OT_DEADLIST_HDR
,
148 sizeof (dsl_deadlist_phys_t
), tx
));
152 dsl_deadlist_free(objset_t
*os
, uint64_t dlobj
, dmu_tx_t
*tx
)
154 dmu_object_info_t doi
;
158 VERIFY3U(0, ==, dmu_object_info(os
, dlobj
, &doi
));
159 if (doi
.doi_type
== DMU_OT_BPOBJ
) {
160 bpobj_free(os
, dlobj
, tx
);
164 for (zap_cursor_init(&zc
, os
, dlobj
);
165 zap_cursor_retrieve(&zc
, &za
) == 0;
166 zap_cursor_advance(&zc
)) {
167 uint64_t obj
= za
.za_first_integer
;
168 if (obj
== dmu_objset_pool(os
)->dp_empty_bpobj
)
169 bpobj_decr_empty(os
, tx
);
171 bpobj_free(os
, obj
, tx
);
173 zap_cursor_fini(&zc
);
174 VERIFY3U(0, ==, dmu_object_free(os
, dlobj
, tx
));
178 dle_enqueue(dsl_deadlist_t
*dl
, dsl_deadlist_entry_t
*dle
,
179 const blkptr_t
*bp
, dmu_tx_t
*tx
)
181 ASSERT(MUTEX_HELD(&dl
->dl_lock
));
182 if (dle
->dle_bpobj
.bpo_object
==
183 dmu_objset_pool(dl
->dl_os
)->dp_empty_bpobj
) {
184 uint64_t obj
= bpobj_alloc(dl
->dl_os
, SPA_OLD_MAXBLOCKSIZE
, tx
);
185 bpobj_close(&dle
->dle_bpobj
);
186 bpobj_decr_empty(dl
->dl_os
, tx
);
187 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
188 VERIFY3U(0, ==, zap_update_int_key(dl
->dl_os
, dl
->dl_object
,
189 dle
->dle_mintxg
, obj
, tx
));
191 bpobj_enqueue(&dle
->dle_bpobj
, bp
, tx
);
195 dle_enqueue_subobj(dsl_deadlist_t
*dl
, dsl_deadlist_entry_t
*dle
,
196 uint64_t obj
, dmu_tx_t
*tx
)
198 ASSERT(MUTEX_HELD(&dl
->dl_lock
));
199 if (dle
->dle_bpobj
.bpo_object
!=
200 dmu_objset_pool(dl
->dl_os
)->dp_empty_bpobj
) {
201 bpobj_enqueue_subobj(&dle
->dle_bpobj
, obj
, tx
);
203 bpobj_close(&dle
->dle_bpobj
);
204 bpobj_decr_empty(dl
->dl_os
, tx
);
205 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
206 VERIFY3U(0, ==, zap_update_int_key(dl
->dl_os
, dl
->dl_object
,
207 dle
->dle_mintxg
, obj
, tx
));
212 dsl_deadlist_insert(dsl_deadlist_t
*dl
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
214 dsl_deadlist_entry_t dle_tofind
;
215 dsl_deadlist_entry_t
*dle
;
219 bpobj_enqueue(&dl
->dl_bpobj
, bp
, tx
);
223 mutex_enter(&dl
->dl_lock
);
224 dsl_deadlist_load_tree(dl
);
226 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
227 dl
->dl_phys
->dl_used
+=
228 bp_get_dsize_sync(dmu_objset_spa(dl
->dl_os
), bp
);
229 dl
->dl_phys
->dl_comp
+= BP_GET_PSIZE(bp
);
230 dl
->dl_phys
->dl_uncomp
+= BP_GET_UCSIZE(bp
);
232 dle_tofind
.dle_mintxg
= bp
->blk_birth
;
233 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
235 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_BEFORE
);
237 dle
= AVL_PREV(&dl
->dl_tree
, dle
);
240 zfs_panic_recover("blkptr at %p has invalid BLK_BIRTH %llu",
241 bp
, (longlong_t
)bp
->blk_birth
);
242 dle
= avl_first(&dl
->dl_tree
);
245 ASSERT3P(dle
, !=, NULL
);
246 dle_enqueue(dl
, dle
, bp
, tx
);
247 mutex_exit(&dl
->dl_lock
);
251 * Insert new key in deadlist, which must be > all current entries.
252 * mintxg is not inclusive.
255 dsl_deadlist_add_key(dsl_deadlist_t
*dl
, uint64_t mintxg
, dmu_tx_t
*tx
)
258 dsl_deadlist_entry_t
*dle
;
263 dle
= kmem_alloc(sizeof (*dle
), KM_SLEEP
);
264 dle
->dle_mintxg
= mintxg
;
266 mutex_enter(&dl
->dl_lock
);
267 dsl_deadlist_load_tree(dl
);
269 obj
= bpobj_alloc_empty(dl
->dl_os
, SPA_OLD_MAXBLOCKSIZE
, tx
);
270 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
271 avl_add(&dl
->dl_tree
, dle
);
273 VERIFY3U(0, ==, zap_add_int_key(dl
->dl_os
, dl
->dl_object
,
275 mutex_exit(&dl
->dl_lock
);
279 * Remove this key, merging its entries into the previous key.
282 dsl_deadlist_remove_key(dsl_deadlist_t
*dl
, uint64_t mintxg
, dmu_tx_t
*tx
)
284 dsl_deadlist_entry_t dle_tofind
;
285 dsl_deadlist_entry_t
*dle
, *dle_prev
;
290 mutex_enter(&dl
->dl_lock
);
291 dsl_deadlist_load_tree(dl
);
293 dle_tofind
.dle_mintxg
= mintxg
;
294 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, NULL
);
295 dle_prev
= AVL_PREV(&dl
->dl_tree
, dle
);
297 dle_enqueue_subobj(dl
, dle_prev
, dle
->dle_bpobj
.bpo_object
, tx
);
299 avl_remove(&dl
->dl_tree
, dle
);
300 bpobj_close(&dle
->dle_bpobj
);
301 kmem_free(dle
, sizeof (*dle
));
303 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, dl
->dl_object
, mintxg
, tx
));
304 mutex_exit(&dl
->dl_lock
);
308 * Walk ds's snapshots to regenerate generate ZAP & AVL.
311 dsl_deadlist_regenerate(objset_t
*os
, uint64_t dlobj
,
312 uint64_t mrs_obj
, dmu_tx_t
*tx
)
315 dsl_pool_t
*dp
= dmu_objset_pool(os
);
317 dsl_deadlist_open(&dl
, os
, dlobj
);
319 dsl_deadlist_close(&dl
);
323 while (mrs_obj
!= 0) {
325 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, mrs_obj
, FTAG
, &ds
));
326 dsl_deadlist_add_key(&dl
,
327 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
);
328 mrs_obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
329 dsl_dataset_rele(ds
, FTAG
);
331 dsl_deadlist_close(&dl
);
335 dsl_deadlist_clone(dsl_deadlist_t
*dl
, uint64_t maxtxg
,
336 uint64_t mrs_obj
, dmu_tx_t
*tx
)
338 dsl_deadlist_entry_t
*dle
;
341 newobj
= dsl_deadlist_alloc(dl
->dl_os
, tx
);
344 dsl_deadlist_regenerate(dl
->dl_os
, newobj
, mrs_obj
, tx
);
348 mutex_enter(&dl
->dl_lock
);
349 dsl_deadlist_load_tree(dl
);
351 for (dle
= avl_first(&dl
->dl_tree
); dle
;
352 dle
= AVL_NEXT(&dl
->dl_tree
, dle
)) {
355 if (dle
->dle_mintxg
>= maxtxg
)
358 obj
= bpobj_alloc_empty(dl
->dl_os
, SPA_OLD_MAXBLOCKSIZE
, tx
);
359 VERIFY3U(0, ==, zap_add_int_key(dl
->dl_os
, newobj
,
360 dle
->dle_mintxg
, obj
, tx
));
362 mutex_exit(&dl
->dl_lock
);
367 dsl_deadlist_space(dsl_deadlist_t
*dl
,
368 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
371 VERIFY3U(0, ==, bpobj_space(&dl
->dl_bpobj
,
372 usedp
, compp
, uncompp
));
376 mutex_enter(&dl
->dl_lock
);
377 *usedp
= dl
->dl_phys
->dl_used
;
378 *compp
= dl
->dl_phys
->dl_comp
;
379 *uncompp
= dl
->dl_phys
->dl_uncomp
;
380 mutex_exit(&dl
->dl_lock
);
384 * return space used in the range (mintxg, maxtxg].
385 * Includes maxtxg, does not include mintxg.
386 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
387 * larger than any bp in the deadlist (eg. UINT64_MAX)).
390 dsl_deadlist_space_range(dsl_deadlist_t
*dl
, uint64_t mintxg
, uint64_t maxtxg
,
391 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
393 dsl_deadlist_entry_t
*dle
;
394 dsl_deadlist_entry_t dle_tofind
;
398 VERIFY3U(0, ==, bpobj_space_range(&dl
->dl_bpobj
,
399 mintxg
, maxtxg
, usedp
, compp
, uncompp
));
403 *usedp
= *compp
= *uncompp
= 0;
405 mutex_enter(&dl
->dl_lock
);
406 dsl_deadlist_load_tree(dl
);
407 dle_tofind
.dle_mintxg
= mintxg
;
408 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
410 * If we don't find this mintxg, there shouldn't be anything
413 ASSERT(dle
!= NULL
||
414 avl_nearest(&dl
->dl_tree
, where
, AVL_AFTER
) == NULL
);
416 for (; dle
&& dle
->dle_mintxg
< maxtxg
;
417 dle
= AVL_NEXT(&dl
->dl_tree
, dle
)) {
418 uint64_t used
, comp
, uncomp
;
420 VERIFY3U(0, ==, bpobj_space(&dle
->dle_bpobj
,
421 &used
, &comp
, &uncomp
));
427 mutex_exit(&dl
->dl_lock
);
431 dsl_deadlist_insert_bpobj(dsl_deadlist_t
*dl
, uint64_t obj
, uint64_t birth
,
434 dsl_deadlist_entry_t dle_tofind
;
435 dsl_deadlist_entry_t
*dle
;
437 uint64_t used
, comp
, uncomp
;
440 ASSERT(MUTEX_HELD(&dl
->dl_lock
));
442 VERIFY3U(0, ==, bpobj_open(&bpo
, dl
->dl_os
, obj
));
443 VERIFY3U(0, ==, bpobj_space(&bpo
, &used
, &comp
, &uncomp
));
446 dsl_deadlist_load_tree(dl
);
448 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
449 dl
->dl_phys
->dl_used
+= used
;
450 dl
->dl_phys
->dl_comp
+= comp
;
451 dl
->dl_phys
->dl_uncomp
+= uncomp
;
453 dle_tofind
.dle_mintxg
= birth
;
454 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
456 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_BEFORE
);
457 dle_enqueue_subobj(dl
, dle
, obj
, tx
);
461 dsl_deadlist_insert_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
463 dsl_deadlist_t
*dl
= arg
;
464 dsl_deadlist_insert(dl
, bp
, tx
);
469 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
473 dsl_deadlist_merge(dsl_deadlist_t
*dl
, uint64_t obj
, dmu_tx_t
*tx
)
478 dsl_deadlist_phys_t
*dlp
;
479 dmu_object_info_t doi
;
481 VERIFY3U(0, ==, dmu_object_info(dl
->dl_os
, obj
, &doi
));
482 if (doi
.doi_type
== DMU_OT_BPOBJ
) {
484 VERIFY3U(0, ==, bpobj_open(&bpo
, dl
->dl_os
, obj
));
485 VERIFY3U(0, ==, bpobj_iterate(&bpo
,
486 dsl_deadlist_insert_cb
, dl
, tx
));
491 mutex_enter(&dl
->dl_lock
);
492 for (zap_cursor_init(&zc
, dl
->dl_os
, obj
);
493 zap_cursor_retrieve(&zc
, &za
) == 0;
494 zap_cursor_advance(&zc
)) {
495 uint64_t mintxg
= zfs_strtonum(za
.za_name
, NULL
);
496 dsl_deadlist_insert_bpobj(dl
, za
.za_first_integer
, mintxg
, tx
);
497 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, obj
, mintxg
, tx
));
499 zap_cursor_fini(&zc
);
501 VERIFY3U(0, ==, dmu_bonus_hold(dl
->dl_os
, obj
, FTAG
, &bonus
));
502 dlp
= bonus
->db_data
;
503 dmu_buf_will_dirty(bonus
, tx
);
504 bzero(dlp
, sizeof (*dlp
));
505 dmu_buf_rele(bonus
, FTAG
);
506 mutex_exit(&dl
->dl_lock
);
510 * Remove entries on dl that are >= mintxg, and put them on the bpobj.
513 dsl_deadlist_move_bpobj(dsl_deadlist_t
*dl
, bpobj_t
*bpo
, uint64_t mintxg
,
516 dsl_deadlist_entry_t dle_tofind
;
517 dsl_deadlist_entry_t
*dle
;
520 ASSERT(!dl
->dl_oldfmt
);
522 mutex_enter(&dl
->dl_lock
);
523 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
524 dsl_deadlist_load_tree(dl
);
526 dle_tofind
.dle_mintxg
= mintxg
;
527 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
529 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_AFTER
);
531 uint64_t used
, comp
, uncomp
;
532 dsl_deadlist_entry_t
*dle_next
;
534 bpobj_enqueue_subobj(bpo
, dle
->dle_bpobj
.bpo_object
, tx
);
536 VERIFY3U(0, ==, bpobj_space(&dle
->dle_bpobj
,
537 &used
, &comp
, &uncomp
));
538 ASSERT3U(dl
->dl_phys
->dl_used
, >=, used
);
539 ASSERT3U(dl
->dl_phys
->dl_comp
, >=, comp
);
540 ASSERT3U(dl
->dl_phys
->dl_uncomp
, >=, uncomp
);
541 dl
->dl_phys
->dl_used
-= used
;
542 dl
->dl_phys
->dl_comp
-= comp
;
543 dl
->dl_phys
->dl_uncomp
-= uncomp
;
545 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, dl
->dl_object
,
546 dle
->dle_mintxg
, tx
));
548 dle_next
= AVL_NEXT(&dl
->dl_tree
, dle
);
549 avl_remove(&dl
->dl_tree
, dle
);
550 bpobj_close(&dle
->dle_bpobj
);
551 kmem_free(dle
, sizeof (*dle
));
554 mutex_exit(&dl
->dl_lock
);