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) 2011 by Delphix. All rights reserved.
26 #include <sys/dsl_dataset.h>
28 #include <sys/refcount.h>
30 #include <sys/zfs_context.h>
31 #include <sys/dsl_pool.h>
34 * Deadlist concurrency:
36 * Deadlists can only be modified from the syncing thread.
38 * Except for dsl_deadlist_insert(), it can only be modified with the
39 * dp_config_rwlock held with RW_WRITER.
41 * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can
42 * be called concurrently, from open context, with the dl_config_rwlock held
45 * Therefore, we only need to provide locking between dsl_deadlist_insert() and
46 * the accessors, protecting:
47 * dl_phys->dl_used,comp,uncomp
48 * and protecting the dl_tree from being loaded.
49 * The locking is provided by dl_lock. Note that locking on the bpobj_t
50 * provides its own locking, and dl_oldfmt is immutable.
54 dsl_deadlist_compare(const void *arg1
, const void *arg2
)
56 const dsl_deadlist_entry_t
*dle1
= arg1
;
57 const dsl_deadlist_entry_t
*dle2
= arg2
;
59 if (dle1
->dle_mintxg
< dle2
->dle_mintxg
)
61 else if (dle1
->dle_mintxg
> dle2
->dle_mintxg
)
68 dsl_deadlist_load_tree(dsl_deadlist_t
*dl
)
73 ASSERT(!dl
->dl_oldfmt
);
77 avl_create(&dl
->dl_tree
, dsl_deadlist_compare
,
78 sizeof (dsl_deadlist_entry_t
),
79 offsetof(dsl_deadlist_entry_t
, dle_node
));
80 for (zap_cursor_init(&zc
, dl
->dl_os
, dl
->dl_object
);
81 zap_cursor_retrieve(&zc
, &za
) == 0;
82 zap_cursor_advance(&zc
)) {
83 dsl_deadlist_entry_t
*dle
= kmem_alloc(sizeof (*dle
), KM_SLEEP
);
84 dle
->dle_mintxg
= strtonum(za
.za_name
, NULL
);
85 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
,
86 za
.za_first_integer
));
87 avl_add(&dl
->dl_tree
, dle
);
90 dl
->dl_havetree
= B_TRUE
;
94 dsl_deadlist_open(dsl_deadlist_t
*dl
, objset_t
*os
, uint64_t object
)
96 dmu_object_info_t doi
;
98 mutex_init(&dl
->dl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
100 dl
->dl_object
= object
;
101 VERIFY3U(0, ==, dmu_bonus_hold(os
, object
, dl
, &dl
->dl_dbuf
));
102 dmu_object_info_from_db(dl
->dl_dbuf
, &doi
);
103 if (doi
.doi_type
== DMU_OT_BPOBJ
) {
104 dmu_buf_rele(dl
->dl_dbuf
, dl
);
106 dl
->dl_oldfmt
= B_TRUE
;
107 VERIFY3U(0, ==, bpobj_open(&dl
->dl_bpobj
, os
, object
));
111 dl
->dl_oldfmt
= B_FALSE
;
112 dl
->dl_phys
= dl
->dl_dbuf
->db_data
;
113 dl
->dl_havetree
= B_FALSE
;
117 dsl_deadlist_close(dsl_deadlist_t
*dl
)
120 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_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 bpobj_free(os
, za
.za_first_integer
, tx
);
168 zap_cursor_fini(&zc
);
169 VERIFY3U(0, ==, dmu_object_free(os
, dlobj
, tx
));
173 dsl_deadlist_insert(dsl_deadlist_t
*dl
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
175 dsl_deadlist_entry_t dle_tofind
;
176 dsl_deadlist_entry_t
*dle
;
180 bpobj_enqueue(&dl
->dl_bpobj
, bp
, tx
);
184 dsl_deadlist_load_tree(dl
);
186 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
187 mutex_enter(&dl
->dl_lock
);
188 dl
->dl_phys
->dl_used
+=
189 bp_get_dsize_sync(dmu_objset_spa(dl
->dl_os
), bp
);
190 dl
->dl_phys
->dl_comp
+= BP_GET_PSIZE(bp
);
191 dl
->dl_phys
->dl_uncomp
+= BP_GET_UCSIZE(bp
);
192 mutex_exit(&dl
->dl_lock
);
194 dle_tofind
.dle_mintxg
= bp
->blk_birth
;
195 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
197 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_BEFORE
);
199 dle
= AVL_PREV(&dl
->dl_tree
, dle
);
200 bpobj_enqueue(&dle
->dle_bpobj
, bp
, tx
);
204 * Insert new key in deadlist, which must be > all current entries.
205 * mintxg is not inclusive.
208 dsl_deadlist_add_key(dsl_deadlist_t
*dl
, uint64_t mintxg
, dmu_tx_t
*tx
)
211 dsl_deadlist_entry_t
*dle
;
216 dsl_deadlist_load_tree(dl
);
218 dle
= kmem_alloc(sizeof (*dle
), KM_SLEEP
);
219 dle
->dle_mintxg
= mintxg
;
220 obj
= bpobj_alloc(dl
->dl_os
, SPA_MAXBLOCKSIZE
, tx
);
221 VERIFY3U(0, ==, bpobj_open(&dle
->dle_bpobj
, dl
->dl_os
, obj
));
222 avl_add(&dl
->dl_tree
, dle
);
224 VERIFY3U(0, ==, zap_add_int_key(dl
->dl_os
, dl
->dl_object
,
229 * Remove this key, merging its entries into the previous key.
232 dsl_deadlist_remove_key(dsl_deadlist_t
*dl
, uint64_t mintxg
, dmu_tx_t
*tx
)
234 dsl_deadlist_entry_t dle_tofind
;
235 dsl_deadlist_entry_t
*dle
, *dle_prev
;
240 dsl_deadlist_load_tree(dl
);
242 dle_tofind
.dle_mintxg
= mintxg
;
243 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, NULL
);
244 dle_prev
= AVL_PREV(&dl
->dl_tree
, dle
);
246 bpobj_enqueue_subobj(&dle_prev
->dle_bpobj
,
247 dle
->dle_bpobj
.bpo_object
, tx
);
249 avl_remove(&dl
->dl_tree
, dle
);
250 bpobj_close(&dle
->dle_bpobj
);
251 kmem_free(dle
, sizeof (*dle
));
253 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, dl
->dl_object
, mintxg
, tx
));
257 * Walk ds's snapshots to regenerate generate ZAP & AVL.
260 dsl_deadlist_regenerate(objset_t
*os
, uint64_t dlobj
,
261 uint64_t mrs_obj
, dmu_tx_t
*tx
)
264 dsl_pool_t
*dp
= dmu_objset_pool(os
);
266 dsl_deadlist_open(&dl
, os
, dlobj
);
268 dsl_deadlist_close(&dl
);
272 while (mrs_obj
!= 0) {
274 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, mrs_obj
, FTAG
, &ds
));
275 dsl_deadlist_add_key(&dl
, ds
->ds_phys
->ds_prev_snap_txg
, tx
);
276 mrs_obj
= ds
->ds_phys
->ds_prev_snap_obj
;
277 dsl_dataset_rele(ds
, FTAG
);
279 dsl_deadlist_close(&dl
);
283 dsl_deadlist_clone(dsl_deadlist_t
*dl
, uint64_t maxtxg
,
284 uint64_t mrs_obj
, dmu_tx_t
*tx
)
286 dsl_deadlist_entry_t
*dle
;
289 newobj
= dsl_deadlist_alloc(dl
->dl_os
, tx
);
292 dsl_deadlist_regenerate(dl
->dl_os
, newobj
, mrs_obj
, tx
);
296 dsl_deadlist_load_tree(dl
);
298 for (dle
= avl_first(&dl
->dl_tree
); dle
;
299 dle
= AVL_NEXT(&dl
->dl_tree
, dle
)) {
302 if (dle
->dle_mintxg
>= maxtxg
)
305 obj
= bpobj_alloc(dl
->dl_os
, SPA_MAXBLOCKSIZE
, tx
);
306 VERIFY3U(0, ==, zap_add_int_key(dl
->dl_os
, newobj
,
307 dle
->dle_mintxg
, obj
, tx
));
313 dsl_deadlist_space(dsl_deadlist_t
*dl
,
314 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
317 VERIFY3U(0, ==, bpobj_space(&dl
->dl_bpobj
,
318 usedp
, compp
, uncompp
));
322 mutex_enter(&dl
->dl_lock
);
323 *usedp
= dl
->dl_phys
->dl_used
;
324 *compp
= dl
->dl_phys
->dl_comp
;
325 *uncompp
= dl
->dl_phys
->dl_uncomp
;
326 mutex_exit(&dl
->dl_lock
);
330 * return space used in the range (mintxg, maxtxg].
331 * Includes maxtxg, does not include mintxg.
332 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
333 * larger than any bp in the deadlist (eg. UINT64_MAX)).
336 dsl_deadlist_space_range(dsl_deadlist_t
*dl
, uint64_t mintxg
, uint64_t maxtxg
,
337 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
339 dsl_deadlist_entry_t
*dle
;
340 dsl_deadlist_entry_t dle_tofind
;
344 VERIFY3U(0, ==, bpobj_space_range(&dl
->dl_bpobj
,
345 mintxg
, maxtxg
, usedp
, compp
, uncompp
));
349 *usedp
= *compp
= *uncompp
= 0;
351 mutex_enter(&dl
->dl_lock
);
352 dsl_deadlist_load_tree(dl
);
353 dle_tofind
.dle_mintxg
= mintxg
;
354 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
356 * If we don't find this mintxg, there shouldn't be anything
359 ASSERT(dle
!= NULL
||
360 avl_nearest(&dl
->dl_tree
, where
, AVL_AFTER
) == NULL
);
362 for (; dle
&& dle
->dle_mintxg
< maxtxg
;
363 dle
= AVL_NEXT(&dl
->dl_tree
, dle
)) {
364 uint64_t used
, comp
, uncomp
;
366 VERIFY3U(0, ==, bpobj_space(&dle
->dle_bpobj
,
367 &used
, &comp
, &uncomp
));
373 mutex_exit(&dl
->dl_lock
);
377 dsl_deadlist_insert_bpobj(dsl_deadlist_t
*dl
, uint64_t obj
, uint64_t birth
,
380 dsl_deadlist_entry_t dle_tofind
;
381 dsl_deadlist_entry_t
*dle
;
383 uint64_t used
, comp
, uncomp
;
386 VERIFY3U(0, ==, bpobj_open(&bpo
, dl
->dl_os
, obj
));
387 VERIFY3U(0, ==, bpobj_space(&bpo
, &used
, &comp
, &uncomp
));
390 dsl_deadlist_load_tree(dl
);
392 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
393 mutex_enter(&dl
->dl_lock
);
394 dl
->dl_phys
->dl_used
+= used
;
395 dl
->dl_phys
->dl_comp
+= comp
;
396 dl
->dl_phys
->dl_uncomp
+= uncomp
;
397 mutex_exit(&dl
->dl_lock
);
399 dle_tofind
.dle_mintxg
= birth
;
400 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
402 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_BEFORE
);
403 bpobj_enqueue_subobj(&dle
->dle_bpobj
, obj
, tx
);
407 dsl_deadlist_insert_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
409 dsl_deadlist_t
*dl
= arg
;
410 dsl_deadlist_insert(dl
, bp
, tx
);
415 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
419 dsl_deadlist_merge(dsl_deadlist_t
*dl
, uint64_t obj
, dmu_tx_t
*tx
)
424 dsl_deadlist_phys_t
*dlp
;
425 dmu_object_info_t doi
;
427 VERIFY3U(0, ==, dmu_object_info(dl
->dl_os
, obj
, &doi
));
428 if (doi
.doi_type
== DMU_OT_BPOBJ
) {
430 VERIFY3U(0, ==, bpobj_open(&bpo
, dl
->dl_os
, obj
));
431 VERIFY3U(0, ==, bpobj_iterate(&bpo
,
432 dsl_deadlist_insert_cb
, dl
, tx
));
437 for (zap_cursor_init(&zc
, dl
->dl_os
, obj
);
438 zap_cursor_retrieve(&zc
, &za
) == 0;
439 zap_cursor_advance(&zc
)) {
440 uint64_t mintxg
= strtonum(za
.za_name
, NULL
);
441 dsl_deadlist_insert_bpobj(dl
, za
.za_first_integer
, mintxg
, tx
);
442 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, obj
, mintxg
, tx
));
444 zap_cursor_fini(&zc
);
446 VERIFY3U(0, ==, dmu_bonus_hold(dl
->dl_os
, obj
, FTAG
, &bonus
));
447 dlp
= bonus
->db_data
;
448 dmu_buf_will_dirty(bonus
, tx
);
449 bzero(dlp
, sizeof (*dlp
));
450 dmu_buf_rele(bonus
, FTAG
);
454 * Remove entries on dl that are >= mintxg, and put them on the bpobj.
457 dsl_deadlist_move_bpobj(dsl_deadlist_t
*dl
, bpobj_t
*bpo
, uint64_t mintxg
,
460 dsl_deadlist_entry_t dle_tofind
;
461 dsl_deadlist_entry_t
*dle
;
464 ASSERT(!dl
->dl_oldfmt
);
465 dmu_buf_will_dirty(dl
->dl_dbuf
, tx
);
466 dsl_deadlist_load_tree(dl
);
468 dle_tofind
.dle_mintxg
= mintxg
;
469 dle
= avl_find(&dl
->dl_tree
, &dle_tofind
, &where
);
471 dle
= avl_nearest(&dl
->dl_tree
, where
, AVL_AFTER
);
473 uint64_t used
, comp
, uncomp
;
474 dsl_deadlist_entry_t
*dle_next
;
476 bpobj_enqueue_subobj(bpo
, dle
->dle_bpobj
.bpo_object
, tx
);
478 VERIFY3U(0, ==, bpobj_space(&dle
->dle_bpobj
,
479 &used
, &comp
, &uncomp
));
480 mutex_enter(&dl
->dl_lock
);
481 ASSERT3U(dl
->dl_phys
->dl_used
, >=, used
);
482 ASSERT3U(dl
->dl_phys
->dl_comp
, >=, comp
);
483 ASSERT3U(dl
->dl_phys
->dl_uncomp
, >=, uncomp
);
484 dl
->dl_phys
->dl_used
-= used
;
485 dl
->dl_phys
->dl_comp
-= comp
;
486 dl
->dl_phys
->dl_uncomp
-= uncomp
;
487 mutex_exit(&dl
->dl_lock
);
489 VERIFY3U(0, ==, zap_remove_int(dl
->dl_os
, dl
->dl_object
,
490 dle
->dle_mintxg
, tx
));
492 dle_next
= AVL_NEXT(&dl
->dl_tree
, dle
);
493 avl_remove(&dl
->dl_tree
, dle
);
494 bpobj_close(&dle
->dle_bpobj
);
495 kmem_free(dle
, sizeof (*dle
));