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.
25 #include <sys/dsl_dataset.h>
27 #include <sys/refcount.h>
29 #include <sys/zfs_context.h>
30 #include <sys/dsl_pool.h>
33 dsl_deadlist_compare(const void *arg1, const void *arg2)
35 const dsl_deadlist_entry_t *dle1 = arg1;
36 const dsl_deadlist_entry_t *dle2 = arg2;
38 if (dle1->dle_mintxg < dle2->dle_mintxg)
40 else if (dle1->dle_mintxg > dle2->dle_mintxg)
47 dsl_deadlist_load_tree(dsl_deadlist_t *dl)
52 ASSERT(!dl->dl_oldfmt);
56 avl_create(&dl->dl_tree, dsl_deadlist_compare,
57 sizeof (dsl_deadlist_entry_t),
58 offsetof(dsl_deadlist_entry_t, dle_node));
59 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
60 zap_cursor_retrieve(&zc, &za) == 0;
61 zap_cursor_advance(&zc)) {
62 dsl_deadlist_entry_t *dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
63 dle->dle_mintxg = strtonum(za.za_name, NULL);
64 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os,
65 za.za_first_integer));
66 avl_add(&dl->dl_tree, dle);
69 dl->dl_havetree = B_TRUE;
73 dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
75 dmu_object_info_t doi;
77 mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
79 dl->dl_object = object;
80 VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf));
81 dmu_object_info_from_db(dl->dl_dbuf, &doi);
82 if (doi.doi_type == DMU_OT_BPOBJ) {
83 dmu_buf_rele(dl->dl_dbuf, dl);
85 dl->dl_oldfmt = B_TRUE;
86 VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object));
90 dl->dl_oldfmt = B_FALSE;
91 dl->dl_phys = dl->dl_dbuf->db_data;
92 dl->dl_havetree = B_FALSE;
96 dsl_deadlist_close(dsl_deadlist_t *dl)
99 dsl_deadlist_entry_t *dle;
102 dl->dl_oldfmt = B_FALSE;
103 bpobj_close(&dl->dl_bpobj);
107 if (dl->dl_havetree) {
108 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
110 bpobj_close(&dle->dle_bpobj);
111 kmem_free(dle, sizeof (*dle));
113 avl_destroy(&dl->dl_tree);
115 dmu_buf_rele(dl->dl_dbuf, dl);
116 mutex_destroy(&dl->dl_lock);
122 dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
124 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
125 return (bpobj_alloc(os, SPA_MAXBLOCKSIZE, tx));
126 return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR,
127 sizeof (dsl_deadlist_phys_t), tx));
131 dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
133 dmu_object_info_t doi;
137 VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi));
138 if (doi.doi_type == DMU_OT_BPOBJ) {
139 bpobj_free(os, dlobj, tx);
143 for (zap_cursor_init(&zc, os, dlobj);
144 zap_cursor_retrieve(&zc, &za) == 0;
145 zap_cursor_advance(&zc))
146 bpobj_free(os, za.za_first_integer, tx);
147 zap_cursor_fini(&zc);
148 VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx));
152 dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx)
154 dsl_deadlist_entry_t dle_tofind;
155 dsl_deadlist_entry_t *dle;
159 bpobj_enqueue(&dl->dl_bpobj, bp, tx);
163 dsl_deadlist_load_tree(dl);
165 dmu_buf_will_dirty(dl->dl_dbuf, tx);
166 mutex_enter(&dl->dl_lock);
167 dl->dl_phys->dl_used +=
168 bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
169 dl->dl_phys->dl_comp += BP_GET_PSIZE(bp);
170 dl->dl_phys->dl_uncomp += BP_GET_UCSIZE(bp);
171 mutex_exit(&dl->dl_lock);
173 dle_tofind.dle_mintxg = bp->blk_birth;
174 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
176 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
178 dle = AVL_PREV(&dl->dl_tree, dle);
179 bpobj_enqueue(&dle->dle_bpobj, bp, tx);
183 * Insert new key in deadlist, which must be > all current entries.
184 * mintxg is not inclusive.
187 dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
190 dsl_deadlist_entry_t *dle;
195 dsl_deadlist_load_tree(dl);
197 dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
198 dle->dle_mintxg = mintxg;
199 obj = bpobj_alloc(dl->dl_os, SPA_MAXBLOCKSIZE, tx);
200 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
201 avl_add(&dl->dl_tree, dle);
203 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object,
208 * Remove this key, merging its entries into the previous key.
211 dsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
213 dsl_deadlist_entry_t dle_tofind;
214 dsl_deadlist_entry_t *dle, *dle_prev;
219 dsl_deadlist_load_tree(dl);
221 dle_tofind.dle_mintxg = mintxg;
222 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
223 dle_prev = AVL_PREV(&dl->dl_tree, dle);
225 bpobj_enqueue_subobj(&dle_prev->dle_bpobj,
226 dle->dle_bpobj.bpo_object, tx);
228 avl_remove(&dl->dl_tree, dle);
229 bpobj_close(&dle->dle_bpobj);
230 kmem_free(dle, sizeof (*dle));
232 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
236 * Walk ds's snapshots to regenerate generate ZAP & AVL.
239 dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
240 uint64_t mrs_obj, dmu_tx_t *tx)
243 dsl_pool_t *dp = dmu_objset_pool(os);
245 dsl_deadlist_open(&dl, os, dlobj);
247 dsl_deadlist_close(&dl);
251 while (mrs_obj != 0) {
253 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
254 dsl_deadlist_add_key(&dl, ds->ds_phys->ds_prev_snap_txg, tx);
255 mrs_obj = ds->ds_phys->ds_prev_snap_obj;
256 dsl_dataset_rele(ds, FTAG);
258 dsl_deadlist_close(&dl);
262 dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
263 uint64_t mrs_obj, dmu_tx_t *tx)
265 dsl_deadlist_entry_t *dle;
268 newobj = dsl_deadlist_alloc(dl->dl_os, tx);
271 dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
275 dsl_deadlist_load_tree(dl);
277 for (dle = avl_first(&dl->dl_tree); dle;
278 dle = AVL_NEXT(&dl->dl_tree, dle)) {
281 if (dle->dle_mintxg >= maxtxg)
284 obj = bpobj_alloc(dl->dl_os, SPA_MAXBLOCKSIZE, tx);
285 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, newobj,
286 dle->dle_mintxg, obj, tx));
292 dsl_deadlist_space(dsl_deadlist_t *dl,
293 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
296 VERIFY3U(0, ==, bpobj_space(&dl->dl_bpobj,
297 usedp, compp, uncompp));
301 mutex_enter(&dl->dl_lock);
302 *usedp = dl->dl_phys->dl_used;
303 *compp = dl->dl_phys->dl_comp;
304 *uncompp = dl->dl_phys->dl_uncomp;
305 mutex_exit(&dl->dl_lock);
309 * return space used in the range (mintxg, maxtxg].
310 * Includes maxtxg, does not include mintxg.
311 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
315 dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg,
316 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
318 dsl_deadlist_entry_t dle_tofind;
319 dsl_deadlist_entry_t *dle;
323 VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj,
324 mintxg, maxtxg, usedp, compp, uncompp));
328 dsl_deadlist_load_tree(dl);
329 *usedp = *compp = *uncompp = 0;
331 dle_tofind.dle_mintxg = mintxg;
332 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
334 * If we don't find this mintxg, there shouldn't be anything
337 ASSERT(dle != NULL ||
338 avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL);
339 for (; dle && dle->dle_mintxg < maxtxg;
340 dle = AVL_NEXT(&dl->dl_tree, dle)) {
341 uint64_t used, comp, uncomp;
343 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
344 &used, &comp, &uncomp));
353 dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
356 dsl_deadlist_entry_t dle_tofind;
357 dsl_deadlist_entry_t *dle;
359 uint64_t used, comp, uncomp;
362 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
363 VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp));
366 dsl_deadlist_load_tree(dl);
368 dmu_buf_will_dirty(dl->dl_dbuf, tx);
369 mutex_enter(&dl->dl_lock);
370 dl->dl_phys->dl_used += used;
371 dl->dl_phys->dl_comp += comp;
372 dl->dl_phys->dl_uncomp += uncomp;
373 mutex_exit(&dl->dl_lock);
375 dle_tofind.dle_mintxg = birth;
376 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
378 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
379 bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
383 dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
385 dsl_deadlist_t *dl = arg;
386 dsl_deadlist_insert(dl, bp, tx);
391 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
395 dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
400 dsl_deadlist_phys_t *dlp;
401 dmu_object_info_t doi;
403 VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi));
404 if (doi.doi_type == DMU_OT_BPOBJ) {
406 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
407 VERIFY3U(0, ==, bpobj_iterate(&bpo,
408 dsl_deadlist_insert_cb, dl, tx));
413 for (zap_cursor_init(&zc, dl->dl_os, obj);
414 zap_cursor_retrieve(&zc, &za) == 0;
415 zap_cursor_advance(&zc)) {
416 uint64_t mintxg = strtonum(za.za_name, NULL);
417 dsl_deadlist_insert_bpobj(dl, za.za_first_integer, mintxg, tx);
418 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, obj, mintxg, tx));
420 zap_cursor_fini(&zc);
422 VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
423 dlp = bonus->db_data;
424 dmu_buf_will_dirty(bonus, tx);
425 bzero(dlp, sizeof (*dlp));
426 dmu_buf_rele(bonus, FTAG);
430 * Remove entries on dl that are >= mintxg, and put them on the bpobj.
433 dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
436 dsl_deadlist_entry_t dle_tofind;
437 dsl_deadlist_entry_t *dle;
440 ASSERT(!dl->dl_oldfmt);
441 dmu_buf_will_dirty(dl->dl_dbuf, tx);
442 dsl_deadlist_load_tree(dl);
444 dle_tofind.dle_mintxg = mintxg;
445 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
447 dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
449 uint64_t used, comp, uncomp;
450 dsl_deadlist_entry_t *dle_next;
452 bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
454 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
455 &used, &comp, &uncomp));
456 mutex_enter(&dl->dl_lock);
457 ASSERT3U(dl->dl_phys->dl_used, >=, used);
458 ASSERT3U(dl->dl_phys->dl_comp, >=, comp);
459 ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);
460 dl->dl_phys->dl_used -= used;
461 dl->dl_phys->dl_comp -= comp;
462 dl->dl_phys->dl_uncomp -= uncomp;
463 mutex_exit(&dl->dl_lock);
465 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object,
466 dle->dle_mintxg, tx));
468 dle_next = AVL_NEXT(&dl->dl_tree, dle);
469 avl_remove(&dl->dl_tree, dle);
470 bpobj_close(&dle->dle_bpobj);
471 kmem_free(dle, sizeof (*dle));