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