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. 23732885fcSMatthew Ahrens * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24cde58dbcSMatthew Ahrens */ 25cde58dbcSMatthew Ahrens 26cde58dbcSMatthew Ahrens #include <sys/bpobj.h> 27cde58dbcSMatthew Ahrens #include <sys/zfs_context.h> 28cde58dbcSMatthew Ahrens #include <sys/refcount.h> 2919b94df9SMatthew Ahrens #include <sys/dsl_pool.h> 30f1745736SMatthew Ahrens #include <sys/zfeature.h> 31f1745736SMatthew Ahrens #include <sys/zap.h> 32f1745736SMatthew Ahrens 33f1745736SMatthew Ahrens /* 34f1745736SMatthew Ahrens * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj). 35f1745736SMatthew Ahrens */ 36f1745736SMatthew Ahrens uint64_t 37f1745736SMatthew Ahrens bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx) 38f1745736SMatthew Ahrens { 39f1745736SMatthew Ahrens spa_t *spa = dmu_objset_spa(os); 40f1745736SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 41f1745736SMatthew Ahrens 422acef22dSMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) { 432acef22dSMatthew Ahrens if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) { 44fb09f5aaSMadhav Suresh ASSERT0(dp->dp_empty_bpobj); 45f1745736SMatthew Ahrens dp->dp_empty_bpobj = 46b5152584SMatthew Ahrens bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx); 47f1745736SMatthew Ahrens VERIFY(zap_add(os, 48f1745736SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, 49f1745736SMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 50f1745736SMatthew Ahrens &dp->dp_empty_bpobj, tx) == 0); 51f1745736SMatthew Ahrens } 522acef22dSMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx); 53f1745736SMatthew Ahrens ASSERT(dp->dp_empty_bpobj != 0); 54f1745736SMatthew Ahrens return (dp->dp_empty_bpobj); 55f1745736SMatthew Ahrens } else { 56f1745736SMatthew Ahrens return (bpobj_alloc(os, blocksize, tx)); 57f1745736SMatthew Ahrens } 58f1745736SMatthew Ahrens } 59f1745736SMatthew Ahrens 60f1745736SMatthew Ahrens void 61f1745736SMatthew Ahrens bpobj_decr_empty(objset_t *os, dmu_tx_t *tx) 62f1745736SMatthew Ahrens { 63f1745736SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 64f1745736SMatthew Ahrens 652acef22dSMatthew Ahrens spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx); 662acef22dSMatthew Ahrens if (!spa_feature_is_active(dmu_objset_spa(os), 672acef22dSMatthew Ahrens SPA_FEATURE_EMPTY_BPOBJ)) { 68f1745736SMatthew Ahrens VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset, 69f1745736SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, 70f1745736SMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, tx)); 71f1745736SMatthew Ahrens VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx)); 72f1745736SMatthew Ahrens dp->dp_empty_bpobj = 0; 73f1745736SMatthew Ahrens } 74f1745736SMatthew Ahrens } 75cde58dbcSMatthew Ahrens 76cde58dbcSMatthew Ahrens uint64_t 77cde58dbcSMatthew Ahrens bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx) 78cde58dbcSMatthew Ahrens { 79cde58dbcSMatthew Ahrens int size; 80cde58dbcSMatthew Ahrens 81cde58dbcSMatthew Ahrens if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT) 82cde58dbcSMatthew Ahrens size = BPOBJ_SIZE_V0; 83cde58dbcSMatthew Ahrens else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS) 84cde58dbcSMatthew Ahrens size = BPOBJ_SIZE_V1; 85cde58dbcSMatthew Ahrens else 86cde58dbcSMatthew Ahrens size = sizeof (bpobj_phys_t); 87cde58dbcSMatthew Ahrens 88cde58dbcSMatthew Ahrens return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize, 89cde58dbcSMatthew Ahrens DMU_OT_BPOBJ_HDR, size, tx)); 90cde58dbcSMatthew Ahrens } 91cde58dbcSMatthew Ahrens 92cde58dbcSMatthew Ahrens void 93cde58dbcSMatthew Ahrens bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx) 94cde58dbcSMatthew Ahrens { 95cde58dbcSMatthew Ahrens int64_t i; 96cde58dbcSMatthew Ahrens bpobj_t bpo; 97cde58dbcSMatthew Ahrens dmu_object_info_t doi; 98cde58dbcSMatthew Ahrens int epb; 99cde58dbcSMatthew Ahrens dmu_buf_t *dbuf = NULL; 100cde58dbcSMatthew Ahrens 101f1745736SMatthew Ahrens ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj); 102b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, os, obj)); 103cde58dbcSMatthew Ahrens 104cde58dbcSMatthew Ahrens mutex_enter(&bpo.bpo_lock); 105cde58dbcSMatthew Ahrens 106cde58dbcSMatthew Ahrens if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0) 107cde58dbcSMatthew Ahrens goto out; 108cde58dbcSMatthew Ahrens 109b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi)); 110cde58dbcSMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t); 111cde58dbcSMatthew Ahrens 112cde58dbcSMatthew Ahrens for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 113cde58dbcSMatthew Ahrens uint64_t *objarray; 114cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 115cde58dbcSMatthew Ahrens 116cde58dbcSMatthew Ahrens offset = i * sizeof (uint64_t); 117cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, epb); 118cde58dbcSMatthew Ahrens 119cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 120cde58dbcSMatthew Ahrens if (dbuf) 121cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 122b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(os, 123cde58dbcSMatthew Ahrens bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0)); 124cde58dbcSMatthew Ahrens } 125cde58dbcSMatthew Ahrens 126cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 127cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 128cde58dbcSMatthew Ahrens 129cde58dbcSMatthew Ahrens objarray = dbuf->db_data; 130cde58dbcSMatthew Ahrens bpobj_free(os, objarray[blkoff], tx); 131cde58dbcSMatthew Ahrens } 132cde58dbcSMatthew Ahrens if (dbuf) { 133cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 134cde58dbcSMatthew Ahrens dbuf = NULL; 135cde58dbcSMatthew Ahrens } 136b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx)); 137cde58dbcSMatthew Ahrens 138cde58dbcSMatthew Ahrens out: 139cde58dbcSMatthew Ahrens mutex_exit(&bpo.bpo_lock); 140cde58dbcSMatthew Ahrens bpobj_close(&bpo); 141cde58dbcSMatthew Ahrens 142b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, obj, tx)); 143cde58dbcSMatthew Ahrens } 144cde58dbcSMatthew Ahrens 145cde58dbcSMatthew Ahrens int 146cde58dbcSMatthew Ahrens bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object) 147cde58dbcSMatthew Ahrens { 148cde58dbcSMatthew Ahrens dmu_object_info_t doi; 149cde58dbcSMatthew Ahrens int err; 150cde58dbcSMatthew Ahrens 151cde58dbcSMatthew Ahrens err = dmu_object_info(os, object, &doi); 152cde58dbcSMatthew Ahrens if (err) 153cde58dbcSMatthew Ahrens return (err); 154cde58dbcSMatthew Ahrens 155cde58dbcSMatthew Ahrens bzero(bpo, sizeof (*bpo)); 156cde58dbcSMatthew Ahrens mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL); 157cde58dbcSMatthew Ahrens 158cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_dbuf == NULL); 159cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_phys == NULL); 160cde58dbcSMatthew Ahrens ASSERT(object != 0); 161cde58dbcSMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ); 162cde58dbcSMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR); 163cde58dbcSMatthew Ahrens 164837b568bSGeorge Wilson err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf); 165837b568bSGeorge Wilson if (err) 166837b568bSGeorge Wilson return (err); 167837b568bSGeorge Wilson 168cde58dbcSMatthew Ahrens bpo->bpo_os = os; 169cde58dbcSMatthew Ahrens bpo->bpo_object = object; 170cde58dbcSMatthew Ahrens bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT; 171cde58dbcSMatthew Ahrens bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0); 172cde58dbcSMatthew Ahrens bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1); 173cde58dbcSMatthew Ahrens bpo->bpo_phys = bpo->bpo_dbuf->db_data; 174cde58dbcSMatthew Ahrens return (0); 175cde58dbcSMatthew Ahrens } 176cde58dbcSMatthew Ahrens 177cde58dbcSMatthew Ahrens void 178cde58dbcSMatthew Ahrens bpobj_close(bpobj_t *bpo) 179cde58dbcSMatthew Ahrens { 180cde58dbcSMatthew Ahrens /* Lame workaround for closing a bpobj that was never opened. */ 181cde58dbcSMatthew Ahrens if (bpo->bpo_object == 0) 182cde58dbcSMatthew Ahrens return; 183cde58dbcSMatthew Ahrens 184cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_dbuf, bpo); 185cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf != NULL) 186cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 187cde58dbcSMatthew Ahrens bpo->bpo_dbuf = NULL; 188cde58dbcSMatthew Ahrens bpo->bpo_phys = NULL; 189cde58dbcSMatthew Ahrens bpo->bpo_cached_dbuf = NULL; 190837b568bSGeorge Wilson bpo->bpo_object = 0; 191cde58dbcSMatthew Ahrens 192cde58dbcSMatthew Ahrens mutex_destroy(&bpo->bpo_lock); 193cde58dbcSMatthew Ahrens } 194cde58dbcSMatthew Ahrens 1955d7b4d43SMatthew Ahrens static boolean_t 1965d7b4d43SMatthew Ahrens bpobj_hasentries(bpobj_t *bpo) 1975d7b4d43SMatthew Ahrens { 1985d7b4d43SMatthew Ahrens return (bpo->bpo_phys->bpo_num_blkptrs != 0 || 1995d7b4d43SMatthew Ahrens (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs != 0)); 2005d7b4d43SMatthew Ahrens } 2015d7b4d43SMatthew Ahrens 202cde58dbcSMatthew Ahrens static int 203cde58dbcSMatthew Ahrens bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx, 204cde58dbcSMatthew Ahrens boolean_t free) 205cde58dbcSMatthew Ahrens { 206cde58dbcSMatthew Ahrens dmu_object_info_t doi; 207cde58dbcSMatthew Ahrens int epb; 208cde58dbcSMatthew Ahrens int64_t i; 209cde58dbcSMatthew Ahrens int err = 0; 210cde58dbcSMatthew Ahrens dmu_buf_t *dbuf = NULL; 211cde58dbcSMatthew Ahrens 212cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 213cde58dbcSMatthew Ahrens 214cde58dbcSMatthew Ahrens if (free) 215cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 216cde58dbcSMatthew Ahrens 217cde58dbcSMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) { 218cde58dbcSMatthew Ahrens blkptr_t *bparray; 219cde58dbcSMatthew Ahrens blkptr_t *bp; 220cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 221cde58dbcSMatthew Ahrens 222cde58dbcSMatthew Ahrens offset = i * sizeof (blkptr_t); 223cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, bpo->bpo_epb); 224cde58dbcSMatthew Ahrens 225cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 226cde58dbcSMatthew Ahrens if (dbuf) 227cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 228cde58dbcSMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset, 229cde58dbcSMatthew Ahrens FTAG, &dbuf, 0); 230cde58dbcSMatthew Ahrens if (err) 231cde58dbcSMatthew Ahrens break; 232cde58dbcSMatthew Ahrens } 233cde58dbcSMatthew Ahrens 234cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 235cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 236cde58dbcSMatthew Ahrens 237cde58dbcSMatthew Ahrens bparray = dbuf->db_data; 238cde58dbcSMatthew Ahrens bp = &bparray[blkoff]; 239cde58dbcSMatthew Ahrens err = func(arg, bp, tx); 240cde58dbcSMatthew Ahrens if (err) 241cde58dbcSMatthew Ahrens break; 242cde58dbcSMatthew Ahrens if (free) { 243cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes -= 244cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 245cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 246cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 247cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp); 248cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp); 249cde58dbcSMatthew Ahrens } 250cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs--; 251cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0); 252cde58dbcSMatthew Ahrens } 253cde58dbcSMatthew Ahrens } 254cde58dbcSMatthew Ahrens if (dbuf) { 255cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 256cde58dbcSMatthew Ahrens dbuf = NULL; 257cde58dbcSMatthew Ahrens } 258cde58dbcSMatthew Ahrens if (free) { 259b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object, 260732885fcSMatthew Ahrens (i + 1) * sizeof (blkptr_t), -1ULL, tx)); 261cde58dbcSMatthew Ahrens } 262cde58dbcSMatthew Ahrens if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0) 263cde58dbcSMatthew Ahrens goto out; 264cde58dbcSMatthew Ahrens 265cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havecomp); 266cde58dbcSMatthew Ahrens err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi); 267a0e4757dSLin Ling if (err) { 268a0e4757dSLin Ling mutex_exit(&bpo->bpo_lock); 269cde58dbcSMatthew Ahrens return (err); 270a0e4757dSLin Ling } 2712acef22dSMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ); 272cde58dbcSMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t); 273cde58dbcSMatthew Ahrens 274cde58dbcSMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 275cde58dbcSMatthew Ahrens uint64_t *objarray; 276cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 277cde58dbcSMatthew Ahrens bpobj_t sublist; 278cde58dbcSMatthew Ahrens uint64_t used_before, comp_before, uncomp_before; 279cde58dbcSMatthew Ahrens uint64_t used_after, comp_after, uncomp_after; 280cde58dbcSMatthew Ahrens 281cde58dbcSMatthew Ahrens offset = i * sizeof (uint64_t); 282cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, epb); 283cde58dbcSMatthew Ahrens 284cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 285cde58dbcSMatthew Ahrens if (dbuf) 286cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 287cde58dbcSMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, 288cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0); 289cde58dbcSMatthew Ahrens if (err) 290cde58dbcSMatthew Ahrens break; 291cde58dbcSMatthew Ahrens } 292cde58dbcSMatthew Ahrens 293cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 294cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 295cde58dbcSMatthew Ahrens 296cde58dbcSMatthew Ahrens objarray = dbuf->db_data; 297cde58dbcSMatthew Ahrens err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]); 298cde58dbcSMatthew Ahrens if (err) 299cde58dbcSMatthew Ahrens break; 300cde58dbcSMatthew Ahrens if (free) { 301cde58dbcSMatthew Ahrens err = bpobj_space(&sublist, 302cde58dbcSMatthew Ahrens &used_before, &comp_before, &uncomp_before); 303*b67dde11SWill Andrews if (err != 0) { 304*b67dde11SWill Andrews bpobj_close(&sublist); 305cde58dbcSMatthew Ahrens break; 306*b67dde11SWill Andrews } 307cde58dbcSMatthew Ahrens } 308cde58dbcSMatthew Ahrens err = bpobj_iterate_impl(&sublist, func, arg, tx, free); 309cde58dbcSMatthew Ahrens if (free) { 310b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&sublist, 311cde58dbcSMatthew Ahrens &used_after, &comp_after, &uncomp_after)); 312cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes -= used_before - used_after; 313cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 3144bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_comp -= comp_before - comp_after; 315cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= 316cde58dbcSMatthew Ahrens uncomp_before - uncomp_after; 317cde58dbcSMatthew Ahrens } 318cde58dbcSMatthew Ahrens 319cde58dbcSMatthew Ahrens bpobj_close(&sublist); 320cde58dbcSMatthew Ahrens if (err) 321cde58dbcSMatthew Ahrens break; 322cde58dbcSMatthew Ahrens if (free) { 323cde58dbcSMatthew Ahrens err = dmu_object_free(bpo->bpo_os, 324cde58dbcSMatthew Ahrens objarray[blkoff], tx); 325cde58dbcSMatthew Ahrens if (err) 326cde58dbcSMatthew Ahrens break; 327cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs--; 328cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0); 329cde58dbcSMatthew Ahrens } 330cde58dbcSMatthew Ahrens } 331cde58dbcSMatthew Ahrens if (dbuf) { 332cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 333cde58dbcSMatthew Ahrens dbuf = NULL; 334cde58dbcSMatthew Ahrens } 335cde58dbcSMatthew Ahrens if (free) { 336b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, 337cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs, 338cde58dbcSMatthew Ahrens (i + 1) * sizeof (uint64_t), -1ULL, tx)); 339cde58dbcSMatthew Ahrens } 340cde58dbcSMatthew Ahrens 341cde58dbcSMatthew Ahrens out: 342cde58dbcSMatthew Ahrens /* If there are no entries, there should be no bytes. */ 3435d7b4d43SMatthew Ahrens if (!bpobj_hasentries(bpo)) { 3445d7b4d43SMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_bytes); 3455d7b4d43SMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_comp); 3465d7b4d43SMatthew Ahrens ASSERT0(bpo->bpo_phys->bpo_uncomp); 3475d7b4d43SMatthew Ahrens } 348cde58dbcSMatthew Ahrens 349cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 350cde58dbcSMatthew Ahrens return (err); 351cde58dbcSMatthew Ahrens } 352cde58dbcSMatthew Ahrens 353cde58dbcSMatthew Ahrens /* 354cde58dbcSMatthew Ahrens * Iterate and remove the entries. If func returns nonzero, iteration 355cde58dbcSMatthew Ahrens * will stop and that entry will not be removed. 356cde58dbcSMatthew Ahrens */ 357cde58dbcSMatthew Ahrens int 358cde58dbcSMatthew Ahrens bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 359cde58dbcSMatthew Ahrens { 360cde58dbcSMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE)); 361cde58dbcSMatthew Ahrens } 362cde58dbcSMatthew Ahrens 363cde58dbcSMatthew Ahrens /* 364cde58dbcSMatthew Ahrens * Iterate the entries. If func returns nonzero, iteration will stop. 365cde58dbcSMatthew Ahrens */ 366cde58dbcSMatthew Ahrens int 367cde58dbcSMatthew Ahrens bpobj_iterate_nofree(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_FALSE)); 370cde58dbcSMatthew Ahrens } 371cde58dbcSMatthew Ahrens 372cde58dbcSMatthew Ahrens void 373cde58dbcSMatthew Ahrens bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx) 374cde58dbcSMatthew Ahrens { 375cde58dbcSMatthew Ahrens bpobj_t subbpo; 3764bc4cb79SMatthew Ahrens uint64_t used, comp, uncomp, subsubobjs; 377cde58dbcSMatthew Ahrens 378cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havesubobj); 379cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havecomp); 380f1745736SMatthew Ahrens ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj); 381f1745736SMatthew Ahrens 382f1745736SMatthew Ahrens if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) { 383f1745736SMatthew Ahrens bpobj_decr_empty(bpo->bpo_os, tx); 384f1745736SMatthew Ahrens return; 385f1745736SMatthew Ahrens } 386cde58dbcSMatthew Ahrens 387b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj)); 388b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp)); 389cde58dbcSMatthew Ahrens 3905d7b4d43SMatthew Ahrens if (!bpobj_hasentries(&subbpo)) { 391cde58dbcSMatthew Ahrens /* No point in having an empty subobj. */ 3924bc4cb79SMatthew Ahrens bpobj_close(&subbpo); 393cde58dbcSMatthew Ahrens bpobj_free(bpo->bpo_os, subobj, tx); 394cde58dbcSMatthew Ahrens return; 395cde58dbcSMatthew Ahrens } 396cde58dbcSMatthew Ahrens 397cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 398cde58dbcSMatthew Ahrens if (bpo->bpo_phys->bpo_subobjs == 0) { 399cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os, 400b5152584SMatthew Ahrens DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE, 401b5152584SMatthew Ahrens DMU_OT_NONE, 0, tx); 402cde58dbcSMatthew Ahrens } 403cde58dbcSMatthew Ahrens 4043b2aab18SMatthew Ahrens dmu_object_info_t doi; 4053b2aab18SMatthew Ahrens ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi)); 4063b2aab18SMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ); 4073b2aab18SMatthew Ahrens 408cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 409cde58dbcSMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 410cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 411cde58dbcSMatthew Ahrens sizeof (subobj), &subobj, tx); 412cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs++; 4134bc4cb79SMatthew Ahrens 4144bc4cb79SMatthew Ahrens /* 4154bc4cb79SMatthew Ahrens * If subobj has only one block of subobjs, then move subobj's 4164bc4cb79SMatthew Ahrens * subobjs to bpo's subobj list directly. This reduces 4174bc4cb79SMatthew Ahrens * recursion in bpobj_iterate due to nested subobjs. 4184bc4cb79SMatthew Ahrens */ 4194bc4cb79SMatthew Ahrens subsubobjs = subbpo.bpo_phys->bpo_subobjs; 4204bc4cb79SMatthew Ahrens if (subsubobjs != 0) { 4214bc4cb79SMatthew Ahrens dmu_object_info_t doi; 4224bc4cb79SMatthew Ahrens 423b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi)); 4244bc4cb79SMatthew Ahrens if (doi.doi_max_offset == doi.doi_data_block_size) { 4254bc4cb79SMatthew Ahrens dmu_buf_t *subdb; 4264bc4cb79SMatthew Ahrens uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs; 4274bc4cb79SMatthew Ahrens 428b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs, 4294bc4cb79SMatthew Ahrens 0, FTAG, &subdb, 0)); 430d0475637SMatthew Ahrens /* 431d0475637SMatthew Ahrens * Make sure that we are not asking dmu_write() 432d0475637SMatthew Ahrens * to write more data than we have in our buffer. 433d0475637SMatthew Ahrens */ 434d0475637SMatthew Ahrens VERIFY3U(subdb->db_size, >=, 435d0475637SMatthew Ahrens numsubsub * sizeof (subobj)); 4364bc4cb79SMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 4374bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 4384bc4cb79SMatthew Ahrens numsubsub * sizeof (subobj), subdb->db_data, tx); 4394bc4cb79SMatthew Ahrens dmu_buf_rele(subdb, FTAG); 4404bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs += numsubsub; 4414bc4cb79SMatthew Ahrens 4424bc4cb79SMatthew Ahrens dmu_buf_will_dirty(subbpo.bpo_dbuf, tx); 4434bc4cb79SMatthew Ahrens subbpo.bpo_phys->bpo_subobjs = 0; 444b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os, 4454bc4cb79SMatthew Ahrens subsubobjs, tx)); 4464bc4cb79SMatthew Ahrens } 4474bc4cb79SMatthew Ahrens } 448cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes += used; 449cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp += comp; 450cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp += uncomp; 451cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 4524bc4cb79SMatthew Ahrens 4534bc4cb79SMatthew Ahrens bpobj_close(&subbpo); 454cde58dbcSMatthew Ahrens } 455cde58dbcSMatthew Ahrens 456cde58dbcSMatthew Ahrens void 457cde58dbcSMatthew Ahrens bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx) 458cde58dbcSMatthew Ahrens { 459cde58dbcSMatthew Ahrens blkptr_t stored_bp = *bp; 460cde58dbcSMatthew Ahrens uint64_t offset; 461cde58dbcSMatthew Ahrens int blkoff; 462cde58dbcSMatthew Ahrens blkptr_t *bparray; 463cde58dbcSMatthew Ahrens 464cde58dbcSMatthew Ahrens ASSERT(!BP_IS_HOLE(bp)); 465f1745736SMatthew Ahrens ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj); 466cde58dbcSMatthew Ahrens 4675d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) { 4685d7b4d43SMatthew Ahrens /* 4695d7b4d43SMatthew Ahrens * The bpobj will compress better without the payload. 4705d7b4d43SMatthew Ahrens * 4715d7b4d43SMatthew Ahrens * Note that we store EMBEDDED bp's because they have an 4725d7b4d43SMatthew Ahrens * uncompressed size, which must be accounted for. An 4735d7b4d43SMatthew Ahrens * alternative would be to add their size to bpo_uncomp 4745d7b4d43SMatthew Ahrens * without storing the bp, but that would create additional 4755d7b4d43SMatthew Ahrens * complications: bpo_uncomp would be inconsistent with the 4765d7b4d43SMatthew Ahrens * set of BP's stored, and bpobj_iterate() wouldn't visit 4775d7b4d43SMatthew Ahrens * all the space accounted for in the bpobj. 4785d7b4d43SMatthew Ahrens */ 4795d7b4d43SMatthew Ahrens bzero(&stored_bp, sizeof (stored_bp)); 4805d7b4d43SMatthew Ahrens stored_bp.blk_prop = bp->blk_prop; 4815d7b4d43SMatthew Ahrens stored_bp.blk_birth = bp->blk_birth; 4825d7b4d43SMatthew Ahrens } else if (!BP_GET_DEDUP(bp)) { 4835d7b4d43SMatthew Ahrens /* The bpobj will compress better without the checksum */ 4845d7b4d43SMatthew Ahrens bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum)); 4855d7b4d43SMatthew Ahrens } 4865d7b4d43SMatthew Ahrens 487cde58dbcSMatthew Ahrens /* We never need the fill count. */ 488cde58dbcSMatthew Ahrens stored_bp.blk_fill = 0; 489cde58dbcSMatthew Ahrens 490cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 491cde58dbcSMatthew Ahrens 492cde58dbcSMatthew Ahrens offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp); 493cde58dbcSMatthew Ahrens blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb); 494cde58dbcSMatthew Ahrens 495cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf == NULL || 496cde58dbcSMatthew Ahrens offset < bpo->bpo_cached_dbuf->db_offset || 497cde58dbcSMatthew Ahrens offset >= bpo->bpo_cached_dbuf->db_offset + 498cde58dbcSMatthew Ahrens bpo->bpo_cached_dbuf->db_size) { 499cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf) 500cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 501b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, 502cde58dbcSMatthew Ahrens offset, bpo, &bpo->bpo_cached_dbuf, 0)); 503cde58dbcSMatthew Ahrens } 504cde58dbcSMatthew Ahrens 505cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx); 506cde58dbcSMatthew Ahrens bparray = bpo->bpo_cached_dbuf->db_data; 507cde58dbcSMatthew Ahrens bparray[blkoff] = stored_bp; 508cde58dbcSMatthew Ahrens 509cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 510cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs++; 511cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes += 512cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 513cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 514cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp); 515cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp); 516cde58dbcSMatthew Ahrens } 517cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 518cde58dbcSMatthew Ahrens } 519cde58dbcSMatthew Ahrens 520cde58dbcSMatthew Ahrens struct space_range_arg { 521cde58dbcSMatthew Ahrens spa_t *spa; 522cde58dbcSMatthew Ahrens uint64_t mintxg; 523cde58dbcSMatthew Ahrens uint64_t maxtxg; 524cde58dbcSMatthew Ahrens uint64_t used; 525cde58dbcSMatthew Ahrens uint64_t comp; 526cde58dbcSMatthew Ahrens uint64_t uncomp; 527cde58dbcSMatthew Ahrens }; 528cde58dbcSMatthew Ahrens 529cde58dbcSMatthew Ahrens /* ARGSUSED */ 530cde58dbcSMatthew Ahrens static int 531cde58dbcSMatthew Ahrens space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 532cde58dbcSMatthew Ahrens { 533cde58dbcSMatthew Ahrens struct space_range_arg *sra = arg; 534cde58dbcSMatthew Ahrens 535cde58dbcSMatthew Ahrens if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) { 53619b94df9SMatthew Ahrens if (dsl_pool_sync_context(spa_get_dsl(sra->spa))) 53719b94df9SMatthew Ahrens sra->used += bp_get_dsize_sync(sra->spa, bp); 53819b94df9SMatthew Ahrens else 53919b94df9SMatthew Ahrens sra->used += bp_get_dsize(sra->spa, bp); 540cde58dbcSMatthew Ahrens sra->comp += BP_GET_PSIZE(bp); 541cde58dbcSMatthew Ahrens sra->uncomp += BP_GET_UCSIZE(bp); 542cde58dbcSMatthew Ahrens } 543cde58dbcSMatthew Ahrens return (0); 544cde58dbcSMatthew Ahrens } 545cde58dbcSMatthew Ahrens 546cde58dbcSMatthew Ahrens int 547cde58dbcSMatthew Ahrens bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 548cde58dbcSMatthew Ahrens { 549cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 550cde58dbcSMatthew Ahrens 551cde58dbcSMatthew Ahrens *usedp = bpo->bpo_phys->bpo_bytes; 552cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 553cde58dbcSMatthew Ahrens *compp = bpo->bpo_phys->bpo_comp; 554cde58dbcSMatthew Ahrens *uncompp = bpo->bpo_phys->bpo_uncomp; 555cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 556cde58dbcSMatthew Ahrens return (0); 557cde58dbcSMatthew Ahrens } else { 558cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 559cde58dbcSMatthew Ahrens return (bpobj_space_range(bpo, 0, UINT64_MAX, 560cde58dbcSMatthew Ahrens usedp, compp, uncompp)); 561cde58dbcSMatthew Ahrens } 562cde58dbcSMatthew Ahrens } 563cde58dbcSMatthew Ahrens 564cde58dbcSMatthew Ahrens /* 565cde58dbcSMatthew Ahrens * Return the amount of space in the bpobj which is: 566cde58dbcSMatthew Ahrens * mintxg < blk_birth <= maxtxg 567cde58dbcSMatthew Ahrens */ 568cde58dbcSMatthew Ahrens int 569cde58dbcSMatthew Ahrens bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg, 570cde58dbcSMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 571cde58dbcSMatthew Ahrens { 572cde58dbcSMatthew Ahrens struct space_range_arg sra = { 0 }; 573cde58dbcSMatthew Ahrens int err; 574cde58dbcSMatthew Ahrens 575cde58dbcSMatthew Ahrens /* 576cde58dbcSMatthew Ahrens * As an optimization, if they want the whole txg range, just 577cde58dbcSMatthew Ahrens * get bpo_bytes rather than iterating over the bps. 578cde58dbcSMatthew Ahrens */ 579cde58dbcSMatthew Ahrens if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp) 580cde58dbcSMatthew Ahrens return (bpobj_space(bpo, usedp, compp, uncompp)); 581cde58dbcSMatthew Ahrens 582cde58dbcSMatthew Ahrens sra.spa = dmu_objset_spa(bpo->bpo_os); 583cde58dbcSMatthew Ahrens sra.mintxg = mintxg; 584cde58dbcSMatthew Ahrens sra.maxtxg = maxtxg; 585cde58dbcSMatthew Ahrens 586cde58dbcSMatthew Ahrens err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL); 587cde58dbcSMatthew Ahrens *usedp = sra.used; 588cde58dbcSMatthew Ahrens *compp = sra.comp; 589cde58dbcSMatthew Ahrens *uncompp = sra.uncomp; 590cde58dbcSMatthew Ahrens return (err); 591cde58dbcSMatthew Ahrens } 592