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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
732885fca09e11183dd0ea69aaaab5588fb7dbffMatthew Ahrens * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/bpobj.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/zfs_context.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/refcount.h>
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens#include <sys/dsl_pool.h>
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens#include <sys/zfeature.h>
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens#include <sys/zap.h>
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens/*
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens */
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensuint64_t
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensbpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens{
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens spa_t *spa = dmu_objset_spa(os);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(dp->dp_empty_bpobj);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dp->dp_empty_bpobj =
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY(zap_add(os,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens &dp->dp_empty_bpobj, tx) == 0);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens ASSERT(dp->dp_empty_bpobj != 0);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens return (dp->dp_empty_bpobj);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens } else {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens return (bpobj_alloc(os, blocksize, tx));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens}
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensvoid
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrensbpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens{
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (!spa_feature_is_active(dmu_objset_spa(os),
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens SPA_FEATURE_EMPTY_BPOBJ)) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, tx));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens dp->dp_empty_bpobj = 0;
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensuint64_t
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int size;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens size = BPOBJ_SIZE_V0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens size = BPOBJ_SIZE_V1;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens else
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens size = sizeof (bpobj_phys_t);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_OT_BPOBJ_HDR, size, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int64_t i;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_t bpo;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_t doi;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int epb;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_t *dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&bpo.bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens goto out;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t *objarray;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t offset, blkoff;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset = i * sizeof (uint64_t);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkoff = P2PHASE(i, epb);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dbuf, FTAG);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_buf_hold(os,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens objarray = dbuf->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_free(os, objarray[blkoff], tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dbuf, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensout:
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&bpo.bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&bpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensint
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_t doi;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int err;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dmu_object_info(os, object, &doi);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (err);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bzero(bpo, sizeof (*bpo));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_dbuf == NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_phys == NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(object != 0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson if (err)
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson return (err);
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_os = os;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_object = object;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys = bpo->bpo_dbuf->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_close(bpobj_t *bpo)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* Lame workaround for closing a bpobj that was never opened. */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_object == 0)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(bpo->bpo_dbuf, bpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_cached_dbuf != NULL)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_cached_dbuf = NULL;
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson bpo->bpo_object = 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_destroy(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensstatic boolean_t
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensbpobj_hasentries(bpobj_t *bpo)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (bpo->bpo_phys->bpo_num_blkptrs != 0 ||
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs != 0));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens}
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic int
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens boolean_t free)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_object_info_t doi;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int epb;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int64_t i;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int err = 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_t *dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkptr_t *bparray;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkptr_t *bp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t offset, blkoff;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset = i * sizeof (blkptr_t);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkoff = P2PHASE(i, bpo->bpo_epb);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dbuf, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens FTAG, &dbuf, 0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bparray = dbuf->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bp = &bparray[blkoff];
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = func(arg, bp, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_bytes -=
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_havecomp) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs--;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dbuf, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
732885fca09e11183dd0ea69aaaab5588fb7dbffMatthew Ahrens (i + 1) * sizeof (blkptr_t), -1ULL, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens goto out;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_havecomp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
a0e4757d0a6af35e4f970785eae9598f525e5a12Lin Ling if (err) {
a0e4757d0a6af35e4f970785eae9598f525e5a12Lin Ling mutex_exit(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (err);
a0e4757d0a6af35e4f970785eae9598f525e5a12Lin Ling }
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t *objarray;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t offset, blkoff;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_t sublist;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used_before, comp_before, uncomp_before;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used_after, comp_after, uncomp_after;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset = i * sizeof (uint64_t);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkoff = P2PHASE(i, epb);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dbuf, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens objarray = dbuf->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = bpobj_space(&sublist,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens &used_before, &comp_before, &uncomp_before);
b67dde11a73a9455d641403cbbb65ec2add41b41Will Andrews if (err != 0) {
b67dde11a73a9455d641403cbbb65ec2add41b41Will Andrews bpobj_close(&sublist);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
b67dde11a73a9455d641403cbbb65ec2add41b41Will Andrews }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&sublist,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens &used_after, &comp_after, &uncomp_after));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_bytes -= used_before - used_after;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens bpo->bpo_phys->bpo_comp -= comp_before - comp_after;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_uncomp -=
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uncomp_before - uncomp_after;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&sublist);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dmu_object_free(bpo->bpo_os,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens objarray[blkoff], tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs--;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dbuf) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(dbuf, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dbuf = NULL;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_subobjs,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (i + 1) * sizeof (uint64_t), -1ULL, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensout:
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* If there are no entries, there should be no bytes. */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (!bpobj_hasentries(bpo)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_bytes);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_comp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_uncomp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (err);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Iterate and remove the entries. If func returns nonzero, iteration
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * will stop and that entry will not be removed.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensint
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Iterate the entries. If func returns nonzero, iteration will stop.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensint
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_t subbpo;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens uint64_t used, comp, uncomp, subsubobjs;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_havesubobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_havecomp);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens bpobj_decr_empty(bpo->bpo_os, tx);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens return;
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (!bpobj_hasentries(&subbpo)) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* No point in having an empty subobj. */
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens bpobj_close(&subbpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_free(bpo->bpo_os, subobj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_phys->bpo_subobjs == 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens DMU_OT_NONE, 0, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_object_info_t doi;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sizeof (subobj), &subobj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs++;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens /*
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens * If subobj has only one block of subobjs, then move subobj's
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens * subobjs to bpo's subobj list directly. This reduces
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens * recursion in bpobj_iterate due to nested subobjs.
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens */
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens subsubobjs = subbpo.bpo_phys->bpo_subobjs;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens if (subsubobjs != 0) {
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens dmu_object_info_t doi;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens if (doi.doi_max_offset == doi.doi_data_block_size) {
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens dmu_buf_t *subdb;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens 0, FTAG, &subdb, 0));
d04756377ddd1cf28ebcf652541094e17b03c889Matthew Ahrens /*
d04756377ddd1cf28ebcf652541094e17b03c889Matthew Ahrens * Make sure that we are not asking dmu_write()
d04756377ddd1cf28ebcf652541094e17b03c889Matthew Ahrens * to write more data than we have in our buffer.
d04756377ddd1cf28ebcf652541094e17b03c889Matthew Ahrens */
d04756377ddd1cf28ebcf652541094e17b03c889Matthew Ahrens VERIFY3U(subdb->db_size, >=,
d04756377ddd1cf28ebcf652541094e17b03c889Matthew Ahrens numsubsub * sizeof (subobj));
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens numsubsub * sizeof (subobj), subdb->db_data, tx);
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens dmu_buf_rele(subdb, FTAG);
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs += numsubsub;
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens subbpo.bpo_phys->bpo_subobjs = 0;
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens subsubobjs, tx));
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens }
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_bytes += used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_comp += comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_uncomp += uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&bpo->bpo_lock);
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens
4bc4cb79b47ef763700720f01652c6ed7c56b95dMatthew Ahrens bpobj_close(&subbpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkptr_t stored_bp = *bp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t offset;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int blkoff;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkptr_t *bparray;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(!BP_IS_HOLE(bp));
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (BP_IS_EMBEDDED(bp)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * The bpobj will compress better without the payload.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Note that we store EMBEDDED bp's because they have an
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * uncompressed size, which must be accounted for. An
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * alternative would be to add their size to bpo_uncomp
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * without storing the bp, but that would create additional
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * complications: bpo_uncomp would be inconsistent with the
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * set of BP's stored, and bpobj_iterate() wouldn't visit
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * all the space accounted for in the bpobj.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bzero(&stored_bp, sizeof (stored_bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens stored_bp.blk_prop = bp->blk_prop;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens stored_bp.blk_birth = bp->blk_birth;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens } else if (!BP_GET_DEDUP(bp)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* The bpobj will compress better without the checksum */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* We never need the fill count. */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens stored_bp.blk_fill = 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_cached_dbuf == NULL ||
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset < bpo->bpo_cached_dbuf->db_offset ||
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset >= bpo->bpo_cached_dbuf->db_offset +
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_cached_dbuf->db_size) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_cached_dbuf)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens offset, bpo, &bpo->bpo_cached_dbuf, 0));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bparray = bpo->bpo_cached_dbuf->db_data;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bparray[blkoff] = stored_bp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs++;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_bytes +=
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_havecomp) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstruct space_range_arg {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens spa_t *spa;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t mintxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t maxtxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens};
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/* ARGSUSED */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic int
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensspace_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens struct space_range_arg *sra = arg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens sra->used += bp_get_dsize_sync(sra->spa, bp);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens else
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens sra->used += bp_get_dsize(sra->spa, bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sra->comp += BP_GET_PSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sra->uncomp += BP_GET_UCSIZE(bp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensint
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_enter(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *usedp = bpo->bpo_phys->bpo_bytes;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (bpo->bpo_havecomp) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *compp = bpo->bpo_phys->bpo_comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *uncompp = bpo->bpo_phys->bpo_uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens } else {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_exit(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (bpobj_space_range(bpo, 0, UINT64_MAX,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens usedp, compp, uncompp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * Return the amount of space in the bpobj which is:
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * mintxg < blk_birth <= maxtxg
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensint
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensbpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens struct space_range_arg sra = { 0 };
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens int err;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * As an optimization, if they want the whole txg range, just
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * get bpo_bytes rather than iterating over the bps.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (bpobj_space(bpo, usedp, compp, uncompp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sra.spa = dmu_objset_spa(bpo->bpo_os);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sra.mintxg = mintxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sra.maxtxg = maxtxg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *usedp = sra.used;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *compp = sra.comp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens *uncompp = sra.uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (err);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}