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 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(!dl
->dl_oldfmt
);
73 avl_create(&dl
->dl_tree
, dsl_deadlist_compare
,
74 sizeof (dsl_deadlist_entry_t
),
75 offsetof(dsl_deadlist_entry_t
, dle_node
));
76 for (zap_cursor_init(&zc
, dl
->dl_os
, dl
->dl_object
);
77 zap_cursor_retrieve(&zc
, &za
) == 0;
78 zap_cursor_advance(&zc
)) {
79 dsl_deadlist_entry_t
*dle
;
81 dle
= kmem_alloc(sizeof (*dle
), KM_SLEEP
);
82 dle
->dle_mintxg
= 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
;
123 dl
->dl_oldfmt
= B_FALSE
;
124 bpobj_close(&dl
->dl_bpobj
);
128 if (dl
->dl_havetree
) {
129 while ((dle
= avl_destroy_nodes(&dl
->dl_tree
, &cookie
))
131 bpobj_close(&dle
->dle_bpobj
);
132 kmem_free(dle
, sizeof (*dle
));
134 avl_destroy(&dl
->dl_tree
);
136 dmu_buf_rele(dl
->dl_dbuf
, dl
);
137 mutex_destroy(&dl
->dl_lock
);
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 if (dle
->dle_bpobj
.bpo_object
==
182 dmu_objset_pool(dl
->dl_os
)->dp_empty_bpobj
) {
183 uint64_t obj
= bpobj_alloc(dl
->dl_os
, SPA_OLD_MAXBLOCKSIZE
, tx
);
184 bpobj_close(&dle
->dle_bpobj
);
185 bpobj_decr_empty(dl
->dl_os
, tx
);
186 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
187 VERIFY3U(0, ==, zap_update_int_key(dl
->dl_os
, dl
->dl_object
,
188 dle
->dle_mintxg
, obj
, tx
));
190 bpobj_enqueue(&dle
->dle_bpobj
, bp
, tx
);
194 dle_enqueue_subobj(dsl_deadlist_t
*dl
, dsl_deadlist_entry_t
*dle
,
195 uint64_t obj
, dmu_tx_t
*tx
)
197 if (dle
->dle_bpobj
.bpo_object
!=
198 dmu_objset_pool(dl
->dl_os
)->dp_empty_bpobj
) {
199 bpobj_enqueue_subobj(&dle
->dle_bpobj
, obj
, tx
);
201 bpobj_close(&dle
->dle_bpobj
);
202 bpobj_decr_empty(dl
->dl_os
, tx
);
203 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
204 VERIFY3U(0, ==, zap_update_int_key(dl
->dl_os
, dl
->dl_object
,
205 dle
->dle_mintxg
, obj
, tx
));
210 dsl_deadlist_insert(dsl_deadlist_t
*dl
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
212 dsl_deadlist_entry_t dle_tofind
;
213 dsl_deadlist_entry_t
*dle
;
217 bpobj_enqueue(&dl
->dl_bpobj
, bp
, tx
);
221 dsl_deadlist_load_tree(dl
);
223 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
224 mutex_enter(&dl
->dl_lock
);
225 dl
->dl_phys
->dl_used
+=
226 bp_get_dsize_sync(dmu_objset_spa(dl
->dl_os
), bp
);
227 dl
->dl_phys
->dl_comp
+= BP_GET_PSIZE(bp
);
228 dl
->dl_phys
->dl_uncomp
+= BP_GET_UCSIZE(bp
);
229 mutex_exit(&dl
->dl_lock
);
231 dle_tofind
.dle_mintxg
= bp
->blk_birth
;
232 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
234 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_BEFORE
);
236 dle
= AVL_PREV(&dl
->dl_tree
, dle
);
239 zfs_panic_recover("blkptr at %p has invalid BLK_BIRTH %llu",
240 bp
, (longlong_t
)bp
->blk_birth
);
241 dle
= avl_first(&dl
->dl_tree
);
244 ASSERT3P(dle
, !=, NULL
);
245 dle_enqueue(dl
, dle
, bp
, tx
);
249 * Insert new key in deadlist, which must be > all current entries.
250 * mintxg is not inclusive.
253 dsl_deadlist_add_key(dsl_deadlist_t
*dl
, uint64_t mintxg
, dmu_tx_t
*tx
)
256 dsl_deadlist_entry_t
*dle
;
261 dsl_deadlist_load_tree(dl
);
263 dle
= kmem_alloc(sizeof (*dle
), KM_SLEEP
);
264 dle
->dle_mintxg
= mintxg
;
265 obj
= bpobj_alloc_empty(dl
->dl_os
, SPA_OLD_MAXBLOCKSIZE
, tx
);
266 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
267 avl_add(&dl
->dl_tree
, dle
);
269 VERIFY3U(0, ==, zap_add_int_key(dl
->dl_os
, dl
->dl_object
,
274 * Remove this key, merging its entries into the previous key.
277 dsl_deadlist_remove_key(dsl_deadlist_t
*dl
, uint64_t mintxg
, dmu_tx_t
*tx
)
279 dsl_deadlist_entry_t dle_tofind
;
280 dsl_deadlist_entry_t
*dle
, *dle_prev
;
285 dsl_deadlist_load_tree(dl
);
287 dle_tofind
.dle_mintxg
= mintxg
;
288 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, NULL
);
289 dle_prev
= AVL_PREV(&dl
->dl_tree
, dle
);
291 dle_enqueue_subobj(dl
, dle_prev
, dle
->dle_bpobj
.bpo_object
, tx
);
293 avl_remove(&dl
->dl_tree
, dle
);
294 bpobj_close(&dle
->dle_bpobj
);
295 kmem_free(dle
, sizeof (*dle
));
297 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, dl
->dl_object
, mintxg
, tx
));
301 * Walk ds's snapshots to regenerate generate ZAP & AVL.
304 dsl_deadlist_regenerate(objset_t
*os
, uint64_t dlobj
,
305 uint64_t mrs_obj
, dmu_tx_t
*tx
)
308 dsl_pool_t
*dp
= dmu_objset_pool(os
);
310 dsl_deadlist_open(&dl
, os
, dlobj
);
312 dsl_deadlist_close(&dl
);
316 while (mrs_obj
!= 0) {
318 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, mrs_obj
, FTAG
, &ds
));
319 dsl_deadlist_add_key(&dl
,
320 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
);
321 mrs_obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
322 dsl_dataset_rele(ds
, FTAG
);
324 dsl_deadlist_close(&dl
);
328 dsl_deadlist_clone(dsl_deadlist_t
*dl
, uint64_t maxtxg
,
329 uint64_t mrs_obj
, dmu_tx_t
*tx
)
331 dsl_deadlist_entry_t
*dle
;
334 newobj
= dsl_deadlist_alloc(dl
->dl_os
, tx
);
337 dsl_deadlist_regenerate(dl
->dl_os
, newobj
, mrs_obj
, tx
);
341 dsl_deadlist_load_tree(dl
);
343 for (dle
= avl_first(&dl
->dl_tree
); dle
;
344 dle
= AVL_NEXT(&dl
->dl_tree
, dle
)) {
347 if (dle
->dle_mintxg
>= maxtxg
)
350 obj
= bpobj_alloc_empty(dl
->dl_os
, SPA_OLD_MAXBLOCKSIZE
, tx
);
351 VERIFY3U(0, ==, zap_add_int_key(dl
->dl_os
, newobj
,
352 dle
->dle_mintxg
, obj
, tx
));
358 dsl_deadlist_space(dsl_deadlist_t
*dl
,
359 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
362 VERIFY3U(0, ==, bpobj_space(&dl
->dl_bpobj
,
363 usedp
, compp
, uncompp
));
367 mutex_enter(&dl
->dl_lock
);
368 *usedp
= dl
->dl_phys
->dl_used
;
369 *compp
= dl
->dl_phys
->dl_comp
;
370 *uncompp
= dl
->dl_phys
->dl_uncomp
;
371 mutex_exit(&dl
->dl_lock
);
375 * return space used in the range (mintxg, maxtxg].
376 * Includes maxtxg, does not include mintxg.
377 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
378 * larger than any bp in the deadlist (eg. UINT64_MAX)).
381 dsl_deadlist_space_range(dsl_deadlist_t
*dl
, uint64_t mintxg
, uint64_t maxtxg
,
382 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
384 dsl_deadlist_entry_t
*dle
;
385 dsl_deadlist_entry_t dle_tofind
;
389 VERIFY3U(0, ==, bpobj_space_range(&dl
->dl_bpobj
,
390 mintxg
, maxtxg
, usedp
, compp
, uncompp
));
394 *usedp
= *compp
= *uncompp
= 0;
396 mutex_enter(&dl
->dl_lock
);
397 dsl_deadlist_load_tree(dl
);
398 dle_tofind
.dle_mintxg
= mintxg
;
399 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
401 * If we don't find this mintxg, there shouldn't be anything
404 ASSERT(dle
!= NULL
||
405 avl_nearest(&dl
->dl_tree
, where
, AVL_AFTER
) == NULL
);
407 for (; dle
&& dle
->dle_mintxg
< maxtxg
;
408 dle
= AVL_NEXT(&dl
->dl_tree
, dle
)) {
409 uint64_t used
, comp
, uncomp
;
411 VERIFY3U(0, ==, bpobj_space(&dle
->dle_bpobj
,
412 &used
, &comp
, &uncomp
));
418 mutex_exit(&dl
->dl_lock
);
422 dsl_deadlist_insert_bpobj(dsl_deadlist_t
*dl
, uint64_t obj
, uint64_t birth
,
425 dsl_deadlist_entry_t dle_tofind
;
426 dsl_deadlist_entry_t
*dle
;
428 uint64_t used
, comp
, uncomp
;
431 VERIFY3U(0, ==, bpobj_open(&bpo
, dl
->dl_os
, obj
));
432 VERIFY3U(0, ==, bpobj_space(&bpo
, &used
, &comp
, &uncomp
));
435 dsl_deadlist_load_tree(dl
);
437 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
438 mutex_enter(&dl
->dl_lock
);
439 dl
->dl_phys
->dl_used
+= used
;
440 dl
->dl_phys
->dl_comp
+= comp
;
441 dl
->dl_phys
->dl_uncomp
+= uncomp
;
442 mutex_exit(&dl
->dl_lock
);
444 dle_tofind
.dle_mintxg
= birth
;
445 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
447 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_BEFORE
);
448 dle_enqueue_subobj(dl
, dle
, obj
, tx
);
452 dsl_deadlist_insert_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
454 dsl_deadlist_t
*dl
= arg
;
455 dsl_deadlist_insert(dl
, bp
, tx
);
460 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
464 dsl_deadlist_merge(dsl_deadlist_t
*dl
, uint64_t obj
, dmu_tx_t
*tx
)
469 dsl_deadlist_phys_t
*dlp
;
470 dmu_object_info_t doi
;
472 VERIFY3U(0, ==, dmu_object_info(dl
->dl_os
, obj
, &doi
));
473 if (doi
.doi_type
== DMU_OT_BPOBJ
) {
475 VERIFY3U(0, ==, bpobj_open(&bpo
, dl
->dl_os
, obj
));
476 VERIFY3U(0, ==, bpobj_iterate(&bpo
,
477 dsl_deadlist_insert_cb
, dl
, tx
));
482 for (zap_cursor_init(&zc
, dl
->dl_os
, obj
);
483 zap_cursor_retrieve(&zc
, &za
) == 0;
484 zap_cursor_advance(&zc
)) {
485 uint64_t mintxg
= strtonum(za
.za_name
, NULL
);
486 dsl_deadlist_insert_bpobj(dl
, za
.za_first_integer
, mintxg
, tx
);
487 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, obj
, mintxg
, tx
));
489 zap_cursor_fini(&zc
);
491 VERIFY3U(0, ==, dmu_bonus_hold(dl
->dl_os
, obj
, FTAG
, &bonus
));
492 dlp
= bonus
->db_data
;
493 dmu_buf_will_dirty(bonus
, tx
);
494 bzero(dlp
, sizeof (*dlp
));
495 dmu_buf_rele(bonus
, FTAG
);
499 * Remove entries on dl that are >= mintxg, and put them on the bpobj.
502 dsl_deadlist_move_bpobj(dsl_deadlist_t
*dl
, bpobj_t
*bpo
, uint64_t mintxg
,
505 dsl_deadlist_entry_t dle_tofind
;
506 dsl_deadlist_entry_t
*dle
;
509 ASSERT(!dl
->dl_oldfmt
);
510 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
511 dsl_deadlist_load_tree(dl
);
513 dle_tofind
.dle_mintxg
= mintxg
;
514 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
516 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_AFTER
);
518 uint64_t used
, comp
, uncomp
;
519 dsl_deadlist_entry_t
*dle_next
;
521 bpobj_enqueue_subobj(bpo
, dle
->dle_bpobj
.bpo_object
, tx
);
523 VERIFY3U(0, ==, bpobj_space(&dle
->dle_bpobj
,
524 &used
, &comp
, &uncomp
));
525 mutex_enter(&dl
->dl_lock
);
526 ASSERT3U(dl
->dl_phys
->dl_used
, >=, used
);
527 ASSERT3U(dl
->dl_phys
->dl_comp
, >=, comp
);
528 ASSERT3U(dl
->dl_phys
->dl_uncomp
, >=, uncomp
);
529 dl
->dl_phys
->dl_used
-= used
;
530 dl
->dl_phys
->dl_comp
-= comp
;
531 dl
->dl_phys
->dl_uncomp
-= uncomp
;
532 mutex_exit(&dl
->dl_lock
);
534 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, dl
->dl_object
,
535 dle
->dle_mintxg
, tx
));
537 dle_next
= AVL_NEXT(&dl
->dl_tree
, dle
);
538 avl_remove(&dl
->dl_tree
, dle
);
539 bpobj_close(&dle
->dle_bpobj
);
540 kmem_free(dle
, sizeof (*dle
));