1cde58dbcSMatthew Ahrens /* 2cde58dbcSMatthew Ahrens * CDDL HEADER START 3cde58dbcSMatthew Ahrens * 4cde58dbcSMatthew Ahrens * The contents of this file are subject to the terms of the 5cde58dbcSMatthew Ahrens * Common Development and Distribution License (the "License"). 6cde58dbcSMatthew Ahrens * You may not use this file except in compliance with the License. 7cde58dbcSMatthew Ahrens * 8cde58dbcSMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9cde58dbcSMatthew Ahrens * or http://www.opensolaris.org/os/licensing. 10cde58dbcSMatthew Ahrens * See the License for the specific language governing permissions 11cde58dbcSMatthew Ahrens * and limitations under the License. 12cde58dbcSMatthew Ahrens * 13cde58dbcSMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each 14cde58dbcSMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15cde58dbcSMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the 16cde58dbcSMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying 17cde58dbcSMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18cde58dbcSMatthew Ahrens * 19cde58dbcSMatthew Ahrens * CDDL HEADER END 20cde58dbcSMatthew Ahrens */ 21cde58dbcSMatthew Ahrens /* 22cde58dbcSMatthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23a3905a45SSerapheim Dimitropoulos * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 24c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 251702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 26cde58dbcSMatthew Ahrens */ 27cde58dbcSMatthew Ahrens 28cde58dbcSMatthew Ahrens #include <sys/bpobj.h> 29cde58dbcSMatthew Ahrens #include <sys/zfs_context.h> 30cde58dbcSMatthew Ahrens #include <sys/refcount.h> 3119b94df9SMatthew Ahrens #include <sys/dsl_pool.h> 32f1745736SMatthew Ahrens #include <sys/zfeature.h> 33f1745736SMatthew Ahrens #include <sys/zap.h> 34f1745736SMatthew Ahrens 35f1745736SMatthew Ahrens /* 36f1745736SMatthew Ahrens * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj). 37f1745736SMatthew Ahrens */ 38f1745736SMatthew Ahrens uint64_t 39f1745736SMatthew Ahrens bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx) 40f1745736SMatthew Ahrens { 41f1745736SMatthew Ahrens spa_t *spa = dmu_objset_spa(os); 42f1745736SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 43f1745736SMatthew Ahrens 442acef22dSMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) { 452acef22dSMatthew Ahrens if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) { 46fb09f5aaSMadhav Suresh ASSERT0(dp->dp_empty_bpobj); 47f1745736SMatthew Ahrens dp->dp_empty_bpobj = 48b5152584SMatthew Ahrens bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx); 49f1745736SMatthew Ahrens VERIFY(zap_add(os, 50f1745736SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, 51f1745736SMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 52f1745736SMatthew Ahrens &dp->dp_empty_bpobj, tx) == 0); 53f1745736SMatthew Ahrens } 542acef22dSMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx); 55f1745736SMatthew Ahrens ASSERT(dp->dp_empty_bpobj != 0); 56f1745736SMatthew Ahrens return (dp->dp_empty_bpobj); 57f1745736SMatthew Ahrens } else { 58f1745736SMatthew Ahrens return (bpobj_alloc(os, blocksize, tx)); 59f1745736SMatthew Ahrens } 60f1745736SMatthew Ahrens } 61f1745736SMatthew Ahrens 62f1745736SMatthew Ahrens void 63f1745736SMatthew Ahrens bpobj_decr_empty(objset_t *os, dmu_tx_t *tx) 64f1745736SMatthew Ahrens { 65f1745736SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 66f1745736SMatthew Ahrens 672acef22dSMatthew Ahrens spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx); 682acef22dSMatthew Ahrens if (!spa_feature_is_active(dmu_objset_spa(os), 692acef22dSMatthew Ahrens SPA_FEATURE_EMPTY_BPOBJ)) { 70f1745736SMatthew Ahrens VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset, 71f1745736SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, 72f1745736SMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, tx)); 73f1745736SMatthew Ahrens VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx)); 74f1745736SMatthew Ahrens dp->dp_empty_bpobj = 0; 75f1745736SMatthew Ahrens } 76f1745736SMatthew Ahrens } 77cde58dbcSMatthew Ahrens 78cde58dbcSMatthew Ahrens uint64_t 79cde58dbcSMatthew Ahrens bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx) 80cde58dbcSMatthew Ahrens { 81cde58dbcSMatthew Ahrens int size; 82cde58dbcSMatthew Ahrens 83cde58dbcSMatthew Ahrens if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT) 84cde58dbcSMatthew Ahrens size = BPOBJ_SIZE_V0; 85cde58dbcSMatthew Ahrens else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS) 86cde58dbcSMatthew Ahrens size = BPOBJ_SIZE_V1; 87cde58dbcSMatthew Ahrens else 88cde58dbcSMatthew Ahrens size = sizeof (bpobj_phys_t); 89cde58dbcSMatthew Ahrens 90cde58dbcSMatthew Ahrens return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize, 91cde58dbcSMatthew Ahrens DMU_OT_BPOBJ_HDR, size, tx)); 92cde58dbcSMatthew Ahrens } 93cde58dbcSMatthew Ahrens 94cde58dbcSMatthew Ahrens void 95cde58dbcSMatthew Ahrens bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx) 96cde58dbcSMatthew Ahrens { 97cde58dbcSMatthew Ahrens int64_t i; 98cde58dbcSMatthew Ahrens bpobj_t bpo; 99cde58dbcSMatthew Ahrens dmu_object_info_t doi; 100cde58dbcSMatthew Ahrens int epb; 101cde58dbcSMatthew Ahrens dmu_buf_t *dbuf = NULL; 102cde58dbcSMatthew Ahrens 103f1745736SMatthew Ahrens ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj); 104b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, os, obj)); 105cde58dbcSMatthew Ahrens 106cde58dbcSMatthew Ahrens mutex_enter(&bpo.bpo_lock); 107cde58dbcSMatthew Ahrens 108cde58dbcSMatthew Ahrens if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0) 109cde58dbcSMatthew Ahrens goto out; 110cde58dbcSMatthew Ahrens 111b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi)); 112cde58dbcSMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t); 113cde58dbcSMatthew Ahrens 114cde58dbcSMatthew Ahrens for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 115cde58dbcSMatthew Ahrens uint64_t *objarray; 116cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 117cde58dbcSMatthew Ahrens 118cde58dbcSMatthew Ahrens offset = i * sizeof (uint64_t); 119cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, epb); 120cde58dbcSMatthew Ahrens 121cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 122cde58dbcSMatthew Ahrens if (dbuf) 123cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 124b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(os, 125cde58dbcSMatthew Ahrens bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0)); 126cde58dbcSMatthew Ahrens } 127cde58dbcSMatthew Ahrens 128cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 129cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 130cde58dbcSMatthew Ahrens 131cde58dbcSMatthew Ahrens objarray = dbuf->db_data; 132cde58dbcSMatthew Ahrens bpobj_free(os, objarray[blkoff], tx); 133cde58dbcSMatthew Ahrens } 134cde58dbcSMatthew Ahrens if (dbuf) { 135cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 136cde58dbcSMatthew Ahrens dbuf = NULL; 137cde58dbcSMatthew Ahrens } 138b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx)); 139cde58dbcSMatthew Ahrens 140cde58dbcSMatthew Ahrens out: 141cde58dbcSMatthew Ahrens mutex_exit(&bpo.bpo_lock); 142cde58dbcSMatthew Ahrens bpobj_close(&bpo); 143cde58dbcSMatthew Ahrens 144b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, obj, tx)); 145cde58dbcSMatthew Ahrens } 146cde58dbcSMatthew Ahrens 147cde58dbcSMatthew Ahrens int 148cde58dbcSMatthew Ahrens bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object) 149cde58dbcSMatthew Ahrens { 150cde58dbcSMatthew Ahrens dmu_object_info_t doi; 151cde58dbcSMatthew Ahrens int err; 152cde58dbcSMatthew Ahrens 153cde58dbcSMatthew Ahrens err = dmu_object_info(os, object, &doi); 154cde58dbcSMatthew Ahrens if (err) 155cde58dbcSMatthew Ahrens return (err); 156cde58dbcSMatthew Ahrens 157cde58dbcSMatthew Ahrens bzero(bpo, sizeof (*bpo)); 158cde58dbcSMatthew Ahrens mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL); 159cde58dbcSMatthew Ahrens 160cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_dbuf == NULL); 161cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_phys == NULL); 162cde58dbcSMatthew Ahrens ASSERT(object != 0); 163cde58dbcSMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ); 164cde58dbcSMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR); 165cde58dbcSMatthew Ahrens 166837b568bSGeorge Wilson err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf); 167837b568bSGeorge Wilson if (err) 168837b568bSGeorge Wilson return (err); 169837b568bSGeorge Wilson 170cde58dbcSMatthew Ahrens bpo->bpo_os = os; 171cde58dbcSMatthew Ahrens bpo->bpo_object = object; 172cde58dbcSMatthew Ahrens bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT; 173cde58dbcSMatthew Ahrens bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0); 174cde58dbcSMatthew Ahrens bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1); 175cde58dbcSMatthew Ahrens bpo->bpo_phys = bpo->bpo_dbuf->db_data; 176cde58dbcSMatthew Ahrens return (0); 177cde58dbcSMatthew Ahrens } 178cde58dbcSMatthew Ahrens 179*5cabbc6bSPrashanth Sreenivasa boolean_t 180*5cabbc6bSPrashanth Sreenivasa bpobj_is_open(const bpobj_t *bpo) 181*5cabbc6bSPrashanth Sreenivasa { 182*5cabbc6bSPrashanth Sreenivasa return (bpo->bpo_object != 0); 183*5cabbc6bSPrashanth Sreenivasa } 184*5cabbc6bSPrashanth Sreenivasa 185cde58dbcSMatthew Ahrens void 186cde58dbcSMatthew Ahrens bpobj_close(bpobj_t *bpo) 187cde58dbcSMatthew Ahrens { 188cde58dbcSMatthew Ahrens /* Lame workaround for closing a bpobj that was never opened. */ 189cde58dbcSMatthew Ahrens if (bpo->bpo_object == 0) 190cde58dbcSMatthew Ahrens return; 191cde58dbcSMatthew Ahrens 192cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_dbuf, bpo); 193cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf != NULL) 194cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 195cde58dbcSMatthew Ahrens bpo->bpo_dbuf = NULL; 196cde58dbcSMatthew Ahrens bpo->bpo_phys = NULL; 197cde58dbcSMatthew Ahrens bpo->bpo_cached_dbuf = NULL; 198837b568bSGeorge Wilson bpo->bpo_object = 0; 199cde58dbcSMatthew Ahrens 200cde58dbcSMatthew Ahrens mutex_destroy(&bpo->bpo_lock); 201cde58dbcSMatthew Ahrens } 202cde58dbcSMatthew Ahrens 203*5cabbc6bSPrashanth Sreenivasa boolean_t 204*5cabbc6bSPrashanth Sreenivasa bpobj_is_empty(bpobj_t *bpo) 2055d7b4d43SMatthew Ahrens { 206*5cabbc6bSPrashanth Sreenivasa return (bpo->bpo_phys->bpo_num_blkptrs == 0 && 207*5cabbc6bSPrashanth Sreenivasa (!bpo->bpo_havesubobj || bpo->bpo_phys->bpo_num_subobjs == 0)); 2085d7b4d43SMatthew Ahrens } 2095d7b4d43SMatthew Ahrens 210cde58dbcSMatthew Ahrens static int 211cde58dbcSMatthew Ahrens bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx, 212cde58dbcSMatthew Ahrens boolean_t free) 213cde58dbcSMatthew Ahrens { 214cde58dbcSMatthew Ahrens dmu_object_info_t doi; 215cde58dbcSMatthew Ahrens int epb; 216cde58dbcSMatthew Ahrens int64_t i; 217cde58dbcSMatthew Ahrens int err = 0; 218cde58dbcSMatthew Ahrens dmu_buf_t *dbuf = NULL; 219cde58dbcSMatthew Ahrens 220*5cabbc6bSPrashanth Sreenivasa ASSERT(bpobj_is_open(bpo)); 221cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 222cde58dbcSMatthew Ahrens 223cde58dbcSMatthew Ahrens if (free) 224cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 225cde58dbcSMatthew Ahrens 226cde58dbcSMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) { 227cde58dbcSMatthew Ahrens blkptr_t *bparray; 228cde58dbcSMatthew Ahrens blkptr_t *bp; 229cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 230cde58dbcSMatthew Ahrens 231cde58dbcSMatthew Ahrens offset = i * sizeof (blkptr_t); 232cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, bpo->bpo_epb); 233cde58dbcSMatthew Ahrens 234cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 235cde58dbcSMatthew Ahrens if (dbuf) 236cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 237cde58dbcSMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset, 238cde58dbcSMatthew Ahrens FTAG, &dbuf, 0); 239cde58dbcSMatthew Ahrens if (err) 240cde58dbcSMatthew Ahrens break; 241cde58dbcSMatthew Ahrens } 242cde58dbcSMatthew Ahrens 243cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 244cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 245cde58dbcSMatthew Ahrens 246cde58dbcSMatthew Ahrens bparray = dbuf->db_data; 247cde58dbcSMatthew Ahrens bp = &bparray[blkoff]; 248cde58dbcSMatthew Ahrens err = func(arg, bp, tx); 249cde58dbcSMatthew Ahrens if (err) 250cde58dbcSMatthew Ahrens break; 251cde58dbcSMatthew Ahrens if (free) { 252cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes -= 253cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 254cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 255cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 256cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp); 257cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp); 258cde58dbcSMatthew Ahrens } 259cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs--; 260cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0); 261cde58dbcSMatthew Ahrens } 262cde58dbcSMatthew Ahrens } 263cde58dbcSMatthew Ahrens if (dbuf) { 264cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 265cde58dbcSMatthew Ahrens dbuf = NULL; 266cde58dbcSMatthew Ahrens } 267cde58dbcSMatthew Ahrens if (free) { 268b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object, 269732885fcSMatthew Ahrens (i + 1) * sizeof (blkptr_t), -1ULL, tx)); 270cde58dbcSMatthew Ahrens } 271cde58dbcSMatthew Ahrens if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0) 272cde58dbcSMatthew Ahrens goto out; 273cde58dbcSMatthew Ahrens 274cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havecomp); 275cde58dbcSMatthew Ahrens err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi); 276a0e4757dSLin Ling if (err) { 277a0e4757dSLin Ling mutex_exit(&bpo->bpo_lock); 278cde58dbcSMatthew Ahrens return (err); 279a0e4757dSLin Ling } 2802acef22dSMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ); 281cde58dbcSMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t); 282cde58dbcSMatthew Ahrens 283cde58dbcSMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 284cde58dbcSMatthew Ahrens uint64_t *objarray; 285cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 286cde58dbcSMatthew Ahrens bpobj_t sublist; 287cde58dbcSMatthew Ahrens uint64_t used_before, comp_before, uncomp_before; 288cde58dbcSMatthew Ahrens uint64_t used_after, comp_after, uncomp_after; 289cde58dbcSMatthew Ahrens 290cde58dbcSMatthew Ahrens offset = i * sizeof (uint64_t); 291cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, epb); 292cde58dbcSMatthew Ahrens 293cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 294cde58dbcSMatthew Ahrens if (dbuf) 295cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 296cde58dbcSMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, 297cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0); 298cde58dbcSMatthew Ahrens if (err) 299cde58dbcSMatthew Ahrens break; 300cde58dbcSMatthew Ahrens } 301cde58dbcSMatthew Ahrens 302cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 303cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 304cde58dbcSMatthew Ahrens 305cde58dbcSMatthew Ahrens objarray = dbuf->db_data; 306cde58dbcSMatthew Ahrens err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]); 307cde58dbcSMatthew Ahrens if (err) 308cde58dbcSMatthew Ahrens break; 309cde58dbcSMatthew Ahrens if (free) { 310cde58dbcSMatthew Ahrens err = bpobj_space(&sublist, 311cde58dbcSMatthew Ahrens &used_before, &comp_before, &uncomp_before); 312b67dde11SWill Andrews if (err != 0) { 313b67dde11SWill Andrews bpobj_close(&sublist); 314cde58dbcSMatthew Ahrens break; 315b67dde11SWill Andrews } 316cde58dbcSMatthew Ahrens } 317cde58dbcSMatthew Ahrens err = bpobj_iterate_impl(&sublist, func, arg, tx, free); 318cde58dbcSMatthew Ahrens if (free) { 319b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&sublist, 320cde58dbcSMatthew Ahrens &used_after, &comp_after, &uncomp_after)); 321cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes -= used_before - used_after; 322cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 3234bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_comp -= comp_before - comp_after; 324cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= 325cde58dbcSMatthew Ahrens uncomp_before - uncomp_after; 326cde58dbcSMatthew Ahrens } 327cde58dbcSMatthew Ahrens 328cde58dbcSMatthew Ahrens bpobj_close(&sublist); 329cde58dbcSMatthew Ahrens if (err) 330cde58dbcSMatthew Ahrens break; 331cde58dbcSMatthew Ahrens if (free) { 332cde58dbcSMatthew Ahrens err = dmu_object_free(bpo->bpo_os, 333cde58dbcSMatthew Ahrens objarray[blkoff], tx); 334cde58dbcSMatthew Ahrens if (err) 335cde58dbcSMatthew Ahrens break; 336cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs--; 337cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0); 338cde58dbcSMatthew Ahrens } 339cde58dbcSMatthew Ahrens } 340cde58dbcSMatthew Ahrens if (dbuf) { 341cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 342cde58dbcSMatthew Ahrens dbuf = NULL; 343cde58dbcSMatthew Ahrens } 344cde58dbcSMatthew Ahrens if (free) { 345b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, 346cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs, 347cde58dbcSMatthew Ahrens (i + 1) * sizeof (uint64_t), -1ULL, tx)); 348cde58dbcSMatthew Ahrens } 349cde58dbcSMatthew Ahrens 350cde58dbcSMatthew Ahrens out: 351cde58dbcSMatthew Ahrens /* If there are no entries, there should be no bytes. */ 352*5cabbc6bSPrashanth Sreenivasa if (bpobj_is_empty(bpo)) { 3535d7b4d43SMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_bytes); 3545d7b4d43SMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_comp); 3555d7b4d43SMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_uncomp); 3565d7b4d43SMatthew Ahrens } 357cde58dbcSMatthew Ahrens 358cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 359cde58dbcSMatthew Ahrens return (err); 360cde58dbcSMatthew Ahrens } 361cde58dbcSMatthew Ahrens 362cde58dbcSMatthew Ahrens /* 363cde58dbcSMatthew Ahrens * Iterate and remove the entries. If func returns nonzero, iteration 364cde58dbcSMatthew Ahrens * will stop and that entry will not be removed. 365cde58dbcSMatthew Ahrens */ 366cde58dbcSMatthew Ahrens int 367cde58dbcSMatthew Ahrens bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 368cde58dbcSMatthew Ahrens { 369cde58dbcSMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE)); 370cde58dbcSMatthew Ahrens } 371cde58dbcSMatthew Ahrens 372cde58dbcSMatthew Ahrens /* 373cde58dbcSMatthew Ahrens * Iterate the entries. If func returns nonzero, iteration will stop. 374cde58dbcSMatthew Ahrens */ 375cde58dbcSMatthew Ahrens int 376cde58dbcSMatthew Ahrens bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 377cde58dbcSMatthew Ahrens { 378cde58dbcSMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE)); 379cde58dbcSMatthew Ahrens } 380cde58dbcSMatthew Ahrens 381cde58dbcSMatthew Ahrens void 382cde58dbcSMatthew Ahrens bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx) 383cde58dbcSMatthew Ahrens { 384cde58dbcSMatthew Ahrens bpobj_t subbpo; 3854bc4cb79SMatthew Ahrens uint64_t used, comp, uncomp, subsubobjs; 386cde58dbcSMatthew Ahrens 387*5cabbc6bSPrashanth Sreenivasa ASSERT(bpobj_is_open(bpo)); 388*5cabbc6bSPrashanth Sreenivasa ASSERT(subobj != 0); 389cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havesubobj); 390cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havecomp); 391f1745736SMatthew Ahrens ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj); 392f1745736SMatthew Ahrens 393f1745736SMatthew Ahrens if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) { 394f1745736SMatthew Ahrens bpobj_decr_empty(bpo->bpo_os, tx); 395f1745736SMatthew Ahrens return; 396f1745736SMatthew Ahrens } 397cde58dbcSMatthew Ahrens 398b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj)); 399b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp)); 400cde58dbcSMatthew Ahrens 401*5cabbc6bSPrashanth Sreenivasa if (bpobj_is_empty(&subbpo)) { 402cde58dbcSMatthew Ahrens /* No point in having an empty subobj. */ 4034bc4cb79SMatthew Ahrens bpobj_close(&subbpo); 404cde58dbcSMatthew Ahrens bpobj_free(bpo->bpo_os, subobj, tx); 405cde58dbcSMatthew Ahrens return; 406cde58dbcSMatthew Ahrens } 407cde58dbcSMatthew Ahrens 408a3905a45SSerapheim Dimitropoulos mutex_enter(&bpo->bpo_lock); 409cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 410cde58dbcSMatthew Ahrens if (bpo->bpo_phys->bpo_subobjs == 0) { 411cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os, 412b5152584SMatthew Ahrens DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE, 413b5152584SMatthew Ahrens DMU_OT_NONE, 0, tx); 414cde58dbcSMatthew Ahrens } 415cde58dbcSMatthew Ahrens 4163b2aab18SMatthew Ahrens dmu_object_info_t doi; 4173b2aab18SMatthew Ahrens ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi)); 4183b2aab18SMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ); 4193b2aab18SMatthew Ahrens 420cde58dbcSMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 421cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 422cde58dbcSMatthew Ahrens sizeof (subobj), &subobj, tx); 423cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs++; 4244bc4cb79SMatthew Ahrens 4254bc4cb79SMatthew Ahrens /* 4264bc4cb79SMatthew Ahrens * If subobj has only one block of subobjs, then move subobj's 4274bc4cb79SMatthew Ahrens * subobjs to bpo's subobj list directly. This reduces 4284bc4cb79SMatthew Ahrens * recursion in bpobj_iterate due to nested subobjs. 4294bc4cb79SMatthew Ahrens */ 4304bc4cb79SMatthew Ahrens subsubobjs = subbpo.bpo_phys->bpo_subobjs; 4314bc4cb79SMatthew Ahrens if (subsubobjs != 0) { 4324bc4cb79SMatthew Ahrens dmu_object_info_t doi; 4334bc4cb79SMatthew Ahrens 434b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi)); 4354bc4cb79SMatthew Ahrens if (doi.doi_max_offset == doi.doi_data_block_size) { 4364bc4cb79SMatthew Ahrens dmu_buf_t *subdb; 4374bc4cb79SMatthew Ahrens uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs; 4384bc4cb79SMatthew Ahrens 439b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs, 4404bc4cb79SMatthew Ahrens 0, FTAG, &subdb, 0)); 441d0475637SMatthew Ahrens /* 442d0475637SMatthew Ahrens * Make sure that we are not asking dmu_write() 443d0475637SMatthew Ahrens * to write more data than we have in our buffer. 444d0475637SMatthew Ahrens */ 445d0475637SMatthew Ahrens VERIFY3U(subdb->db_size, >=, 446d0475637SMatthew Ahrens numsubsub * sizeof (subobj)); 4474bc4cb79SMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 4484bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 4494bc4cb79SMatthew Ahrens numsubsub * sizeof (subobj), subdb->db_data, tx); 4504bc4cb79SMatthew Ahrens dmu_buf_rele(subdb, FTAG); 4514bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs += numsubsub; 4524bc4cb79SMatthew Ahrens 4534bc4cb79SMatthew Ahrens dmu_buf_will_dirty(subbpo.bpo_dbuf, tx); 4544bc4cb79SMatthew Ahrens subbpo.bpo_phys->bpo_subobjs = 0; 455b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os, 4564bc4cb79SMatthew Ahrens subsubobjs, tx)); 4574bc4cb79SMatthew Ahrens } 4584bc4cb79SMatthew Ahrens } 459cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes += used; 460cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp += comp; 461cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp += uncomp; 462cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 4634bc4cb79SMatthew Ahrens 4644bc4cb79SMatthew Ahrens bpobj_close(&subbpo); 465cde58dbcSMatthew Ahrens } 466cde58dbcSMatthew Ahrens 467cde58dbcSMatthew Ahrens void 468cde58dbcSMatthew Ahrens bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx) 469cde58dbcSMatthew Ahrens { 470cde58dbcSMatthew Ahrens blkptr_t stored_bp = *bp; 471cde58dbcSMatthew Ahrens uint64_t offset; 472cde58dbcSMatthew Ahrens int blkoff; 473cde58dbcSMatthew Ahrens blkptr_t *bparray; 474cde58dbcSMatthew Ahrens 475*5cabbc6bSPrashanth Sreenivasa ASSERT(bpobj_is_open(bpo)); 476cde58dbcSMatthew Ahrens ASSERT(!BP_IS_HOLE(bp)); 477f1745736SMatthew Ahrens ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj); 478cde58dbcSMatthew Ahrens 4795d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) { 4805d7b4d43SMatthew Ahrens /* 4815d7b4d43SMatthew Ahrens * The bpobj will compress better without the payload. 4825d7b4d43SMatthew Ahrens * 4835d7b4d43SMatthew Ahrens * Note that we store EMBEDDED bp's because they have an 4845d7b4d43SMatthew Ahrens * uncompressed size, which must be accounted for. An 4855d7b4d43SMatthew Ahrens * alternative would be to add their size to bpo_uncomp 4865d7b4d43SMatthew Ahrens * without storing the bp, but that would create additional 4875d7b4d43SMatthew Ahrens * complications: bpo_uncomp would be inconsistent with the 4885d7b4d43SMatthew Ahrens * set of BP's stored, and bpobj_iterate() wouldn't visit 4895d7b4d43SMatthew Ahrens * all the space accounted for in the bpobj. 4905d7b4d43SMatthew Ahrens */ 4915d7b4d43SMatthew Ahrens bzero(&stored_bp, sizeof (stored_bp)); 4925d7b4d43SMatthew Ahrens stored_bp.blk_prop = bp->blk_prop; 4935d7b4d43SMatthew Ahrens stored_bp.blk_birth = bp->blk_birth; 4945d7b4d43SMatthew Ahrens } else if (!BP_GET_DEDUP(bp)) { 4955d7b4d43SMatthew Ahrens /* The bpobj will compress better without the checksum */ 4965d7b4d43SMatthew Ahrens bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum)); 4975d7b4d43SMatthew Ahrens } 4985d7b4d43SMatthew Ahrens 499cde58dbcSMatthew Ahrens /* We never need the fill count. */ 500cde58dbcSMatthew Ahrens stored_bp.blk_fill = 0; 501cde58dbcSMatthew Ahrens 502cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 503cde58dbcSMatthew Ahrens 504cde58dbcSMatthew Ahrens offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp); 505cde58dbcSMatthew Ahrens blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb); 506cde58dbcSMatthew Ahrens 507cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf == NULL || 508cde58dbcSMatthew Ahrens offset < bpo->bpo_cached_dbuf->db_offset || 509cde58dbcSMatthew Ahrens offset >= bpo->bpo_cached_dbuf->db_offset + 510cde58dbcSMatthew Ahrens bpo->bpo_cached_dbuf->db_size) { 511cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf) 512cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 513b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, 514cde58dbcSMatthew Ahrens offset, bpo, &bpo->bpo_cached_dbuf, 0)); 515cde58dbcSMatthew Ahrens } 516cde58dbcSMatthew Ahrens 517cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx); 518cde58dbcSMatthew Ahrens bparray = bpo->bpo_cached_dbuf->db_data; 519cde58dbcSMatthew Ahrens bparray[blkoff] = stored_bp; 520cde58dbcSMatthew Ahrens 521cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 522cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs++; 523cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes += 524cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 525cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 526cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp); 527cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp); 528cde58dbcSMatthew Ahrens } 529cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 530cde58dbcSMatthew Ahrens } 531cde58dbcSMatthew Ahrens 532cde58dbcSMatthew Ahrens struct space_range_arg { 533cde58dbcSMatthew Ahrens spa_t *spa; 534cde58dbcSMatthew Ahrens uint64_t mintxg; 535cde58dbcSMatthew Ahrens uint64_t maxtxg; 536cde58dbcSMatthew Ahrens uint64_t used; 537cde58dbcSMatthew Ahrens uint64_t comp; 538cde58dbcSMatthew Ahrens uint64_t uncomp; 539cde58dbcSMatthew Ahrens }; 540cde58dbcSMatthew Ahrens 541cde58dbcSMatthew Ahrens /* ARGSUSED */ 542cde58dbcSMatthew Ahrens static int 543cde58dbcSMatthew Ahrens space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 544cde58dbcSMatthew Ahrens { 545cde58dbcSMatthew Ahrens struct space_range_arg *sra = arg; 546cde58dbcSMatthew Ahrens 547cde58dbcSMatthew Ahrens if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) { 54819b94df9SMatthew Ahrens if (dsl_pool_sync_context(spa_get_dsl(sra->spa))) 54919b94df9SMatthew Ahrens sra->used += bp_get_dsize_sync(sra->spa, bp); 55019b94df9SMatthew Ahrens else 55119b94df9SMatthew Ahrens sra->used += bp_get_dsize(sra->spa, bp); 552cde58dbcSMatthew Ahrens sra->comp += BP_GET_PSIZE(bp); 553cde58dbcSMatthew Ahrens sra->uncomp += BP_GET_UCSIZE(bp); 554cde58dbcSMatthew Ahrens } 555cde58dbcSMatthew Ahrens return (0); 556cde58dbcSMatthew Ahrens } 557cde58dbcSMatthew Ahrens 558cde58dbcSMatthew Ahrens int 559cde58dbcSMatthew Ahrens bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 560cde58dbcSMatthew Ahrens { 561*5cabbc6bSPrashanth Sreenivasa ASSERT(bpobj_is_open(bpo)); 562cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 563cde58dbcSMatthew Ahrens 564cde58dbcSMatthew Ahrens *usedp = bpo->bpo_phys->bpo_bytes; 565cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 566cde58dbcSMatthew Ahrens *compp = bpo->bpo_phys->bpo_comp; 567cde58dbcSMatthew Ahrens *uncompp = bpo->bpo_phys->bpo_uncomp; 568cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 569cde58dbcSMatthew Ahrens return (0); 570cde58dbcSMatthew Ahrens } else { 571cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 572cde58dbcSMatthew Ahrens return (bpobj_space_range(bpo, 0, UINT64_MAX, 573cde58dbcSMatthew Ahrens usedp, compp, uncompp)); 574cde58dbcSMatthew Ahrens } 575cde58dbcSMatthew Ahrens } 576cde58dbcSMatthew Ahrens 577cde58dbcSMatthew Ahrens /* 578cde58dbcSMatthew Ahrens * Return the amount of space in the bpobj which is: 579cde58dbcSMatthew Ahrens * mintxg < blk_birth <= maxtxg 580cde58dbcSMatthew Ahrens */ 581cde58dbcSMatthew Ahrens int 582cde58dbcSMatthew Ahrens bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg, 583cde58dbcSMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 584cde58dbcSMatthew Ahrens { 585cde58dbcSMatthew Ahrens struct space_range_arg sra = { 0 }; 586cde58dbcSMatthew Ahrens int err; 587cde58dbcSMatthew Ahrens 588*5cabbc6bSPrashanth Sreenivasa ASSERT(bpobj_is_open(bpo)); 589*5cabbc6bSPrashanth Sreenivasa 590cde58dbcSMatthew Ahrens /* 591cde58dbcSMatthew Ahrens * As an optimization, if they want the whole txg range, just 592cde58dbcSMatthew Ahrens * get bpo_bytes rather than iterating over the bps. 593cde58dbcSMatthew Ahrens */ 594cde58dbcSMatthew Ahrens if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp) 595cde58dbcSMatthew Ahrens return (bpobj_space(bpo, usedp, compp, uncompp)); 596cde58dbcSMatthew Ahrens 597cde58dbcSMatthew Ahrens sra.spa = dmu_objset_spa(bpo->bpo_os); 598cde58dbcSMatthew Ahrens sra.mintxg = mintxg; 599cde58dbcSMatthew Ahrens sra.maxtxg = maxtxg; 600cde58dbcSMatthew Ahrens 601cde58dbcSMatthew Ahrens err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL); 602cde58dbcSMatthew Ahrens *usedp = sra.used; 603cde58dbcSMatthew Ahrens *compp = sra.comp; 604cde58dbcSMatthew Ahrens *uncompp = sra.uncomp; 605cde58dbcSMatthew Ahrens return (err); 606cde58dbcSMatthew Ahrens } 607