cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * CDDL HEADER START
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * The contents of this file are subject to the terms of the
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Common Development and Distribution License (the "License").
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * You may not use this file except in compliance with the License.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * or http://www.opensolaris.org/os/licensing.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * See the License for the specific language governing permissions
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * and limitations under the License.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * CDDL HEADER END
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved.
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Deadlist concurrency:
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Deadlists can only be modified from the syncing thread.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Except for dsl_deadlist_insert(), it can only be modified with the
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * dp_config_rwlock held with RW_WRITER.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * be called concurrently, from open context, with the dl_config_rwlock held
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * with RW_READER.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Therefore, we only need to provide locking between dsl_deadlist_insert() and
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * the accessors, protecting:
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * dl_phys->dl_used,comp,uncomp
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * and protecting the dl_tree from being loaded.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * The locking is provided by dl_lock. Note that locking on the bpobj_t
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * provides its own locking, and dl_oldfmt is immutable.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_compare(const void *arg1, const void *arg2)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens else if (dle1->dle_mintxg > dle2->dle_mintxg)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_create(&dl->dl_tree, dsl_deadlist_compare,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle->dle_mintxg = strtonum(za.za_name, NULL);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens return (bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (obj == dmu_objset_pool(os)->dp_empty_bpobj)
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensdle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens uint64_t obj = bpobj_alloc(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensdle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Insert new key in deadlist, which must be > all current entries.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * mintxg is not inclusive.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Remove this key, merging its entries into the previous key.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Walk ds's snapshots to regenerate generate ZAP & AVL.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens while (mrs_obj != 0) {
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs mrs_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, newobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * return space used in the range (mintxg, maxtxg].
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Includes maxtxg, does not include mintxg.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * larger than any bp in the deadlist (eg. UINT64_MAX)).
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * If we don't find this mintxg, there shouldn't be anything
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * after it either.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * an empty deadlist.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t mintxg = strtonum(za.za_name, NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_insert_bpobj(dl, za.za_first_integer, mintxg, tx);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, obj, mintxg, tx));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Remove entries on dl that are >= mintxg, and put them on the bpobj.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);