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 = arg1;
58 const dsl_deadlist_entry_t *dle2 = arg2;
60 if (dle1->dle_mintxg < dle2->dle_mintxg)
62 else if (dle1->dle_mintxg > dle2->dle_mintxg)
69 dsl_deadlist_load_tree(dsl_deadlist_t *dl)
74 ASSERT(!dl->dl_oldfmt);
78 avl_create(&dl->dl_tree, dsl_deadlist_compare,
79 sizeof (dsl_deadlist_entry_t),
80 offsetof(dsl_deadlist_entry_t, dle_node));
81 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
82 zap_cursor_retrieve(&zc, &za) == 0;
83 zap_cursor_advance(&zc)) {
84 dsl_deadlist_entry_t *dle;
86 dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
87 dle->dle_mintxg = strtonum(za.za_name, NULL);
88 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os,
89 za.za_first_integer));
90 avl_add(&dl->dl_tree, dle);
93 dl->dl_havetree = B_TRUE;
97 dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
99 dmu_object_info_t doi;
101 mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
103 dl->dl_object = object;
104 VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf));
105 dmu_object_info_from_db(dl->dl_dbuf, &doi);
106 if (doi.doi_type == DMU_OT_BPOBJ) {
107 dmu_buf_rele(dl->dl_dbuf, dl);
109 dl->dl_oldfmt = B_TRUE;
110 VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object));
114 dl->dl_oldfmt = B_FALSE;
115 dl->dl_phys = dl->dl_dbuf->db_data;
116 dl->dl_havetree = B_FALSE;
120 dsl_deadlist_close(dsl_deadlist_t *dl)
123 dsl_deadlist_entry_t *dle;
128 dl->dl_oldfmt = B_FALSE;
129 bpobj_close(&dl->dl_bpobj);
133 if (dl->dl_havetree) {
134 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
136 bpobj_close(&dle->dle_bpobj);
137 kmem_free(dle, sizeof (*dle));
139 avl_destroy(&dl->dl_tree);
141 dmu_buf_rele(dl->dl_dbuf, dl);
142 mutex_destroy(&dl->dl_lock);
148 dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
150 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
151 return (bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx));
152 return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR,
153 sizeof (dsl_deadlist_phys_t), tx));
157 dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
159 dmu_object_info_t doi;
163 VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi));
164 if (doi.doi_type == DMU_OT_BPOBJ) {
165 bpobj_free(os, dlobj, tx);
169 for (zap_cursor_init(&zc, os, dlobj);
170 zap_cursor_retrieve(&zc, &za) == 0;
171 zap_cursor_advance(&zc)) {
172 uint64_t obj = za.za_first_integer;
173 if (obj == dmu_objset_pool(os)->dp_empty_bpobj)
174 bpobj_decr_empty(os, tx);
176 bpobj_free(os, obj, tx);
178 zap_cursor_fini(&zc);
179 VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx));
183 dle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
184 const blkptr_t *bp, dmu_tx_t *tx)
186 if (dle->dle_bpobj.bpo_object ==
187 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
188 uint64_t obj = bpobj_alloc(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
189 bpobj_close(&dle->dle_bpobj);
190 bpobj_decr_empty(dl->dl_os, tx);
191 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
192 VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
193 dle->dle_mintxg, obj, tx));
195 bpobj_enqueue(&dle->dle_bpobj, bp, tx);
199 dle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
200 uint64_t obj, dmu_tx_t *tx)
202 if (dle->dle_bpobj.bpo_object !=
203 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
204 bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
206 bpobj_close(&dle->dle_bpobj);
207 bpobj_decr_empty(dl->dl_os, tx);
208 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
209 VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
210 dle->dle_mintxg, obj, tx));
215 dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx)
217 dsl_deadlist_entry_t dle_tofind;
218 dsl_deadlist_entry_t *dle;
222 bpobj_enqueue(&dl->dl_bpobj, bp, tx);
226 dsl_deadlist_load_tree(dl);
228 dmu_buf_will_dirty(dl->dl_dbuf, tx);
229 mutex_enter(&dl->dl_lock);
230 dl->dl_phys->dl_used +=
231 bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
232 dl->dl_phys->dl_comp += BP_GET_PSIZE(bp);
233 dl->dl_phys->dl_uncomp += BP_GET_UCSIZE(bp);
234 mutex_exit(&dl->dl_lock);
236 dle_tofind.dle_mintxg = bp->blk_birth;
237 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
239 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
241 dle = AVL_PREV(&dl->dl_tree, dle);
244 zfs_panic_recover("blkptr at %p has invalid BLK_BIRTH %llu",
245 bp, (longlong_t)bp->blk_birth);
246 dle = avl_first(&dl->dl_tree);
249 ASSERT3P(dle, !=, NULL);
250 dle_enqueue(dl, dle, bp, tx);
254 * Insert new key in deadlist, which must be > all current entries.
255 * mintxg is not inclusive.
258 dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
261 dsl_deadlist_entry_t *dle;
266 dsl_deadlist_load_tree(dl);
268 dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
269 dle->dle_mintxg = mintxg;
270 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
271 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
272 avl_add(&dl->dl_tree, dle);
274 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object,
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 dsl_deadlist_load_tree(dl);
292 dle_tofind.dle_mintxg = mintxg;
293 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
294 dle_prev = AVL_PREV(&dl->dl_tree, dle);
296 dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx);
298 avl_remove(&dl->dl_tree, dle);
299 bpobj_close(&dle->dle_bpobj);
300 kmem_free(dle, sizeof (*dle));
302 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
306 * Walk ds's snapshots to regenerate generate ZAP & AVL.
309 dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
310 uint64_t mrs_obj, dmu_tx_t *tx)
313 dsl_pool_t *dp = dmu_objset_pool(os);
315 dsl_deadlist_open(&dl, os, dlobj);
317 dsl_deadlist_close(&dl);
321 while (mrs_obj != 0) {
323 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
324 dsl_deadlist_add_key(&dl,
325 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
326 mrs_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
327 dsl_dataset_rele(ds, FTAG);
329 dsl_deadlist_close(&dl);
333 dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
334 uint64_t mrs_obj, dmu_tx_t *tx)
336 dsl_deadlist_entry_t *dle;
339 newobj = dsl_deadlist_alloc(dl->dl_os, tx);
342 dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
346 dsl_deadlist_load_tree(dl);
348 for (dle = avl_first(&dl->dl_tree); dle;
349 dle = AVL_NEXT(&dl->dl_tree, dle)) {
352 if (dle->dle_mintxg >= maxtxg)
355 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
356 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, newobj,
357 dle->dle_mintxg, obj, tx));
363 dsl_deadlist_space(dsl_deadlist_t *dl,
364 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
367 VERIFY3U(0, ==, bpobj_space(&dl->dl_bpobj,
368 usedp, compp, uncompp));
372 mutex_enter(&dl->dl_lock);
373 *usedp = dl->dl_phys->dl_used;
374 *compp = dl->dl_phys->dl_comp;
375 *uncompp = dl->dl_phys->dl_uncomp;
376 mutex_exit(&dl->dl_lock);
380 * return space used in the range (mintxg, maxtxg].
381 * Includes maxtxg, does not include mintxg.
382 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
383 * larger than any bp in the deadlist (eg. UINT64_MAX)).
386 dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg,
387 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
389 dsl_deadlist_entry_t *dle;
390 dsl_deadlist_entry_t dle_tofind;
394 VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj,
395 mintxg, maxtxg, usedp, compp, uncompp));
399 *usedp = *compp = *uncompp = 0;
401 mutex_enter(&dl->dl_lock);
402 dsl_deadlist_load_tree(dl);
403 dle_tofind.dle_mintxg = mintxg;
404 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
406 * If we don't find this mintxg, there shouldn't be anything
409 ASSERT(dle != NULL ||
410 avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL);
412 for (; dle && dle->dle_mintxg < maxtxg;
413 dle = AVL_NEXT(&dl->dl_tree, dle)) {
414 uint64_t used, comp, uncomp;
416 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
417 &used, &comp, &uncomp));
423 mutex_exit(&dl->dl_lock);
427 dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
430 dsl_deadlist_entry_t dle_tofind;
431 dsl_deadlist_entry_t *dle;
433 uint64_t used, comp, uncomp;
436 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
437 VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp));
440 dsl_deadlist_load_tree(dl);
442 dmu_buf_will_dirty(dl->dl_dbuf, tx);
443 mutex_enter(&dl->dl_lock);
444 dl->dl_phys->dl_used += used;
445 dl->dl_phys->dl_comp += comp;
446 dl->dl_phys->dl_uncomp += uncomp;
447 mutex_exit(&dl->dl_lock);
449 dle_tofind.dle_mintxg = birth;
450 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
452 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
453 dle_enqueue_subobj(dl, dle, obj, tx);
457 dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
459 dsl_deadlist_t *dl = arg;
460 dsl_deadlist_insert(dl, bp, tx);
465 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
469 dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
474 dsl_deadlist_phys_t *dlp;
475 dmu_object_info_t doi;
477 VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi));
478 if (doi.doi_type == DMU_OT_BPOBJ) {
480 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
481 VERIFY3U(0, ==, bpobj_iterate(&bpo,
482 dsl_deadlist_insert_cb, dl, tx));
487 for (zap_cursor_init(&zc, dl->dl_os, obj);
488 zap_cursor_retrieve(&zc, &za) == 0;
489 zap_cursor_advance(&zc)) {
490 uint64_t mintxg = strtonum(za.za_name, NULL);
491 dsl_deadlist_insert_bpobj(dl, za.za_first_integer, mintxg, tx);
492 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, obj, mintxg, tx));
494 zap_cursor_fini(&zc);
496 VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
497 dlp = bonus->db_data;
498 dmu_buf_will_dirty(bonus, tx);
499 bzero(dlp, sizeof (*dlp));
500 dmu_buf_rele(bonus, FTAG);
504 * Remove entries on dl that are >= mintxg, and put them on the bpobj.
507 dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
510 dsl_deadlist_entry_t dle_tofind;
511 dsl_deadlist_entry_t *dle;
514 ASSERT(!dl->dl_oldfmt);
515 dmu_buf_will_dirty(dl->dl_dbuf, tx);
516 dsl_deadlist_load_tree(dl);
518 dle_tofind.dle_mintxg = mintxg;
519 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
521 dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
523 uint64_t used, comp, uncomp;
524 dsl_deadlist_entry_t *dle_next;
526 bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
528 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
529 &used, &comp, &uncomp));
530 mutex_enter(&dl->dl_lock);
531 ASSERT3U(dl->dl_phys->dl_used, >=, used);
532 ASSERT3U(dl->dl_phys->dl_comp, >=, comp);
533 ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);
534 dl->dl_phys->dl_used -= used;
535 dl->dl_phys->dl_comp -= comp;
536 dl->dl_phys->dl_uncomp -= uncomp;
537 mutex_exit(&dl->dl_lock);
539 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object,
540 dle->dle_mintxg, tx));
542 dle_next = AVL_NEXT(&dl->dl_tree, dle);
543 avl_remove(&dl->dl_tree, dle);
544 bpobj_close(&dle->dle_bpobj);
545 kmem_free(dle, sizeof (*dle));