bpobj.c revision a0e4757d0a6af35e4f970785eae9598f525e5a12
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.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/bpobj.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/zfs_context.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/refcount.h>
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
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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 }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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
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
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dmu_bonus_hold(bpo->bpo_os,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_object, bpo, &bpo->bpo_dbuf);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (err);
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;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens mutex_destroy(&bpo->bpo_lock);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew 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) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens i++;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens i * 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 }
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);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens break;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (free) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_comp -= comp_before - used_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) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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. */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_phys->bpo_num_blkptrs > 0 ||
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs > 0) ||
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpo->bpo_phys->bpo_bytes == 0);
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;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t used, comp, uncomp;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_havesubobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(bpo->bpo_havecomp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&subbpo);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (used == 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* No point in having an empty subobj. */
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,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_OT_BPOBJ_SUBOBJ, SPA_MAXBLOCKSIZE, DMU_OT_NONE, 0, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew 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++;
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);
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));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* We never need the fill count. */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens stored_bp.blk_fill = 0;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* The bpobj will compress better if we can leave off the checksum */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (!BP_GET_DEDUP(bp))
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
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);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens 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) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens sra->used += bp_get_dsize_sync(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}