cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * CDDL HEADER START
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *
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 *
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 *
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 *
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * CDDL HEADER END
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
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]
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/dsl_dataset.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/dmu.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/refcount.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/zap.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/zfs_context.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/dsl_pool.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens/*
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Deadlist concurrency:
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Deadlists can only be modified from the syncing thread.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *
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 *
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 *
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.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic int
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_compare(const void *arg1, const void *arg2)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens const dsl_deadlist_entry_t *dle1 = arg1;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens const dsl_deadlist_entry_t *dle2 = arg2;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dle1->dle_mintxg < dle2->dle_mintxg)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (-1);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens else if (dle1->dle_mintxg > dle2->dle_mintxg)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (+1);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens else
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic void
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_load_tree(dsl_deadlist_t *dl)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_t zc;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_attribute_t za;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(!dl->dl_oldfmt);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_havetree)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_create(&dl->dl_tree, dsl_deadlist_compare,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sizeof (dsl_deadlist_entry_t),
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offsetof(dsl_deadlist_entry_t, dle_node));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_advance(&zc)) {
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 Ahrens za.za_first_integer));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_add(&dl->dl_tree, dle);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_fini(&zc);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_havetree = B_TRUE;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_t doi;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_os = os;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_object = object;
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_from_db(dl->dl_dbuf, &doi);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (doi.doi_type == DMU_OT_BPOBJ) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dl->dl_dbuf, dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_oldfmt = B_TRUE;
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_oldfmt = B_FALSE;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys = dl->dl_dbuf->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_havetree = B_FALSE;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_close(dsl_deadlist_t *dl)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens void *cookie = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs dl->dl_os = NULL;
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_oldfmt = B_FALSE;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&dl->dl_bpobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_havetree) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens != NULL) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&dle->dle_bpobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens kmem_free(dle, sizeof (*dle));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_destroy(&dl->dl_tree);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dl->dl_dbuf, dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_destroy(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensuint64_t
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
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 Ahrens sizeof (dsl_deadlist_phys_t), tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_t doi;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_t zc;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_attribute_t za;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (doi.doi_type == DMU_OT_BPOBJ) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_free(os, dlobj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (zap_cursor_init(&zc, os, dlobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0;
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens zap_cursor_advance(&zc)) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens uint64_t obj = za.za_first_integer;
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (obj == dmu_objset_pool(os)->dp_empty_bpobj)
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_decr_empty(os, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens else
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_free(os, obj, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_fini(&zc);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensstatic void
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensdle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens const blkptr_t *bp, dmu_tx_t *tx)
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens{
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (dle->dle_bpobj.bpo_object ==
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 bpobj_close(&dle->dle_bpobj);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_decr_empty(dl->dl_os, 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 Ahrens dle->dle_mintxg, obj, tx));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_enqueue(&dle->dle_bpobj, bp, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens}
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensstatic void
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensdle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens uint64_t obj, dmu_tx_t *tx)
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens{
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (dle->dle_bpobj.bpo_object !=
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens } else {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_close(&dle->dle_bpobj);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_decr_empty(dl->dl_os, 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 Ahrens dle->dle_mintxg, obj, tx));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens}
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t dle_tofind;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_index_t where;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_enqueue(&dl->dl_bpobj, bp, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(dl->dl_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_used +=
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_comp += BP_GET_PSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_uncomp += BP_GET_UCSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_tofind.dle_mintxg = bp->blk_birth;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dle == NULL)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens else
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = AVL_PREV(&dl->dl_tree, dle);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dle_enqueue(dl, dle, bp, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Insert new key in deadlist, which must be > all current entries.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * mintxg is not inclusive.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t obj;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle->dle_mintxg = mintxg;
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));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_add(&dl->dl_tree, dle);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mintxg, obj, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Remove this key, merging its entries into the previous key.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t dle_tofind;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle, *dle_prev;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_tofind.dle_mintxg = mintxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_prev = AVL_PREV(&dl->dl_tree, dle);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_remove(&dl->dl_tree, dle);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&dle->dle_bpobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens kmem_free(dle, sizeof (*dle));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Walk ds's snapshots to regenerate generate ZAP & AVL.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic void
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t mrs_obj, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_t dl;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_open(&dl, os, dlobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl.dl_oldfmt) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_close(&dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens while (mrs_obj != 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_dataset_t *ds;
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_deadlist_add_key(&dl,
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 Ahrens dsl_dataset_rele(ds, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_close(&dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensuint64_t
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t mrs_obj, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t newobj;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens newobj = dsl_deadlist_alloc(dl->dl_os, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (newobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (dle = avl_first(&dl->dl_tree); dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = AVL_NEXT(&dl->dl_tree, dle)) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t obj;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dle->dle_mintxg >= maxtxg)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
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 dle->dle_mintxg, obj, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (newobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_space(dsl_deadlist_t *dl,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt) {
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&dl->dl_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens usedp, compp, uncompp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *usedp = dl->dl_phys->dl_used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *compp = dl->dl_phys->dl_comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *uncompp = dl->dl_phys->dl_uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
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 Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
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)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
8ac09fcebf848c31516b15ce92861de4b2f514e8Richard Lowe dsl_deadlist_entry_t *dle;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_deadlist_entry_t dle_tofind;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_index_t where;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dl->dl_oldfmt) {
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mintxg, maxtxg, usedp, compp, uncompp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *usedp = *compp = *uncompp = 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens mutex_enter(&dl->dl_lock);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_tofind.dle_mintxg = mintxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * If we don't find this mintxg, there shouldn't be anything
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * after it either.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(dle != NULL ||
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (; dle && dle->dle_mintxg < maxtxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = AVL_NEXT(&dl->dl_tree, dle)) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used, comp, uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens &used, &comp, &uncomp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *usedp += used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *compp += comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *uncompp += uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens mutex_exit(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic void
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t dle_tofind;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_index_t where;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used, comp, uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_t bpo;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&bpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(dl->dl_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_used += used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_comp += comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_uncomp += uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_tofind.dle_mintxg = birth;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dle == NULL)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dle_enqueue_subobj(dl, dle, obj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic int
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_t *dl = arg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_insert(dl, bp, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * an empty deadlist.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_t zc;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_attribute_t za;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_t *bonus;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_phys_t *dlp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_t doi;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (doi.doi_type == DMU_OT_BPOBJ) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_t bpo;
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_iterate(&bpo,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_insert_cb, dl, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&bpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (zap_cursor_init(&zc, dl->dl_os, obj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_advance(&zc)) {
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));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_fini(&zc);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dlp = bonus->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(bonus, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bzero(dlp, sizeof (*dlp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(bonus, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Remove entries on dl that are >= mintxg, and put them on the bpobj.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t dle_tofind;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_index_t where;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(!dl->dl_oldfmt);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(dl->dl_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_load_tree(dl);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_tofind.dle_mintxg = mintxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dle == NULL)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens while (dle) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used, comp, uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_entry_t *dle_next;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens &used, &comp, &uncomp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(dl->dl_phys->dl_used, >=, used);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(dl->dl_phys->dl_comp, >=, comp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_used -= used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_comp -= comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dl->dl_phys->dl_uncomp -= uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&dl->dl_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle->dle_mintxg, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle_next = AVL_NEXT(&dl->dl_tree, dle);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens avl_remove(&dl->dl_tree, dle);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&dle->dle_bpobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens kmem_free(dle, sizeof (*dle));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dle = dle_next;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}