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. 23*b420f3adSRichard Lowe * Copyright (c) 2011 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> 30cde58dbcSMatthew Ahrens 31cde58dbcSMatthew Ahrens uint64_t 32cde58dbcSMatthew Ahrens bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx) 33cde58dbcSMatthew Ahrens { 34cde58dbcSMatthew Ahrens int size; 35cde58dbcSMatthew Ahrens 36cde58dbcSMatthew Ahrens if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT) 37cde58dbcSMatthew Ahrens size = BPOBJ_SIZE_V0; 38cde58dbcSMatthew Ahrens else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS) 39cde58dbcSMatthew Ahrens size = BPOBJ_SIZE_V1; 40cde58dbcSMatthew Ahrens else 41cde58dbcSMatthew Ahrens size = sizeof (bpobj_phys_t); 42cde58dbcSMatthew Ahrens 43cde58dbcSMatthew Ahrens return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize, 44cde58dbcSMatthew Ahrens DMU_OT_BPOBJ_HDR, size, tx)); 45cde58dbcSMatthew Ahrens } 46cde58dbcSMatthew Ahrens 47cde58dbcSMatthew Ahrens void 48cde58dbcSMatthew Ahrens bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx) 49cde58dbcSMatthew Ahrens { 50cde58dbcSMatthew Ahrens int64_t i; 51cde58dbcSMatthew Ahrens bpobj_t bpo; 52cde58dbcSMatthew Ahrens dmu_object_info_t doi; 53cde58dbcSMatthew Ahrens int epb; 54cde58dbcSMatthew Ahrens dmu_buf_t *dbuf = NULL; 55cde58dbcSMatthew Ahrens 56*b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, os, obj)); 57cde58dbcSMatthew Ahrens 58cde58dbcSMatthew Ahrens mutex_enter(&bpo.bpo_lock); 59cde58dbcSMatthew Ahrens 60cde58dbcSMatthew Ahrens if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0) 61cde58dbcSMatthew Ahrens goto out; 62cde58dbcSMatthew Ahrens 63*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi)); 64cde58dbcSMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t); 65cde58dbcSMatthew Ahrens 66cde58dbcSMatthew Ahrens for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 67cde58dbcSMatthew Ahrens uint64_t *objarray; 68cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 69cde58dbcSMatthew Ahrens 70cde58dbcSMatthew Ahrens offset = i * sizeof (uint64_t); 71cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, epb); 72cde58dbcSMatthew Ahrens 73cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 74cde58dbcSMatthew Ahrens if (dbuf) 75cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 76*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(os, 77cde58dbcSMatthew Ahrens bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0)); 78cde58dbcSMatthew Ahrens } 79cde58dbcSMatthew Ahrens 80cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 81cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 82cde58dbcSMatthew Ahrens 83cde58dbcSMatthew Ahrens objarray = dbuf->db_data; 84cde58dbcSMatthew Ahrens bpobj_free(os, objarray[blkoff], tx); 85cde58dbcSMatthew Ahrens } 86cde58dbcSMatthew Ahrens if (dbuf) { 87cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 88cde58dbcSMatthew Ahrens dbuf = NULL; 89cde58dbcSMatthew Ahrens } 90*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx)); 91cde58dbcSMatthew Ahrens 92cde58dbcSMatthew Ahrens out: 93cde58dbcSMatthew Ahrens mutex_exit(&bpo.bpo_lock); 94cde58dbcSMatthew Ahrens bpobj_close(&bpo); 95cde58dbcSMatthew Ahrens 96*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, obj, tx)); 97cde58dbcSMatthew Ahrens } 98cde58dbcSMatthew Ahrens 99cde58dbcSMatthew Ahrens int 100cde58dbcSMatthew Ahrens bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object) 101cde58dbcSMatthew Ahrens { 102cde58dbcSMatthew Ahrens dmu_object_info_t doi; 103cde58dbcSMatthew Ahrens int err; 104cde58dbcSMatthew Ahrens 105cde58dbcSMatthew Ahrens err = dmu_object_info(os, object, &doi); 106cde58dbcSMatthew Ahrens if (err) 107cde58dbcSMatthew Ahrens return (err); 108cde58dbcSMatthew Ahrens 109cde58dbcSMatthew Ahrens bzero(bpo, sizeof (*bpo)); 110cde58dbcSMatthew Ahrens mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL); 111cde58dbcSMatthew Ahrens 112cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_dbuf == NULL); 113cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_phys == NULL); 114cde58dbcSMatthew Ahrens ASSERT(object != 0); 115cde58dbcSMatthew Ahrens ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ); 116cde58dbcSMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR); 117cde58dbcSMatthew Ahrens 118837b568bSGeorge Wilson err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf); 119837b568bSGeorge Wilson if (err) 120837b568bSGeorge Wilson return (err); 121837b568bSGeorge Wilson 122cde58dbcSMatthew Ahrens bpo->bpo_os = os; 123cde58dbcSMatthew Ahrens bpo->bpo_object = object; 124cde58dbcSMatthew Ahrens bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT; 125cde58dbcSMatthew Ahrens bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0); 126cde58dbcSMatthew Ahrens bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1); 127cde58dbcSMatthew Ahrens bpo->bpo_phys = bpo->bpo_dbuf->db_data; 128cde58dbcSMatthew Ahrens return (0); 129cde58dbcSMatthew Ahrens } 130cde58dbcSMatthew Ahrens 131cde58dbcSMatthew Ahrens void 132cde58dbcSMatthew Ahrens bpobj_close(bpobj_t *bpo) 133cde58dbcSMatthew Ahrens { 134cde58dbcSMatthew Ahrens /* Lame workaround for closing a bpobj that was never opened. */ 135cde58dbcSMatthew Ahrens if (bpo->bpo_object == 0) 136cde58dbcSMatthew Ahrens return; 137cde58dbcSMatthew Ahrens 138cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_dbuf, bpo); 139cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf != NULL) 140cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 141cde58dbcSMatthew Ahrens bpo->bpo_dbuf = NULL; 142cde58dbcSMatthew Ahrens bpo->bpo_phys = NULL; 143cde58dbcSMatthew Ahrens bpo->bpo_cached_dbuf = NULL; 144837b568bSGeorge Wilson bpo->bpo_object = 0; 145cde58dbcSMatthew Ahrens 146cde58dbcSMatthew Ahrens mutex_destroy(&bpo->bpo_lock); 147cde58dbcSMatthew Ahrens } 148cde58dbcSMatthew Ahrens 149cde58dbcSMatthew Ahrens static int 150cde58dbcSMatthew Ahrens bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx, 151cde58dbcSMatthew Ahrens boolean_t free) 152cde58dbcSMatthew Ahrens { 153cde58dbcSMatthew Ahrens dmu_object_info_t doi; 154cde58dbcSMatthew Ahrens int epb; 155cde58dbcSMatthew Ahrens int64_t i; 156cde58dbcSMatthew Ahrens int err = 0; 157cde58dbcSMatthew Ahrens dmu_buf_t *dbuf = NULL; 158cde58dbcSMatthew Ahrens 159cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 160cde58dbcSMatthew Ahrens 161cde58dbcSMatthew Ahrens if (free) 162cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 163cde58dbcSMatthew Ahrens 164cde58dbcSMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) { 165cde58dbcSMatthew Ahrens blkptr_t *bparray; 166cde58dbcSMatthew Ahrens blkptr_t *bp; 167cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 168cde58dbcSMatthew Ahrens 169cde58dbcSMatthew Ahrens offset = i * sizeof (blkptr_t); 170cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, bpo->bpo_epb); 171cde58dbcSMatthew Ahrens 172cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 173cde58dbcSMatthew Ahrens if (dbuf) 174cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 175cde58dbcSMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset, 176cde58dbcSMatthew Ahrens FTAG, &dbuf, 0); 177cde58dbcSMatthew Ahrens if (err) 178cde58dbcSMatthew Ahrens break; 179cde58dbcSMatthew Ahrens } 180cde58dbcSMatthew Ahrens 181cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 182cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 183cde58dbcSMatthew Ahrens 184cde58dbcSMatthew Ahrens bparray = dbuf->db_data; 185cde58dbcSMatthew Ahrens bp = &bparray[blkoff]; 186cde58dbcSMatthew Ahrens err = func(arg, bp, tx); 187cde58dbcSMatthew Ahrens if (err) 188cde58dbcSMatthew Ahrens break; 189cde58dbcSMatthew Ahrens if (free) { 190cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes -= 191cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 192cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 193cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 194cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp); 195cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp); 196cde58dbcSMatthew Ahrens } 197cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs--; 198cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0); 199cde58dbcSMatthew Ahrens } 200cde58dbcSMatthew Ahrens } 201cde58dbcSMatthew Ahrens if (dbuf) { 202cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 203cde58dbcSMatthew Ahrens dbuf = NULL; 204cde58dbcSMatthew Ahrens } 205cde58dbcSMatthew Ahrens if (free) { 206cde58dbcSMatthew Ahrens i++; 207*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object, 208cde58dbcSMatthew Ahrens i * sizeof (blkptr_t), -1ULL, tx)); 209cde58dbcSMatthew Ahrens } 210cde58dbcSMatthew Ahrens if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0) 211cde58dbcSMatthew Ahrens goto out; 212cde58dbcSMatthew Ahrens 213cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havecomp); 214cde58dbcSMatthew Ahrens err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi); 215a0e4757dSLin Ling if (err) { 216a0e4757dSLin Ling mutex_exit(&bpo->bpo_lock); 217cde58dbcSMatthew Ahrens return (err); 218a0e4757dSLin Ling } 219cde58dbcSMatthew Ahrens epb = doi.doi_data_block_size / sizeof (uint64_t); 220cde58dbcSMatthew Ahrens 221cde58dbcSMatthew Ahrens for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 222cde58dbcSMatthew Ahrens uint64_t *objarray; 223cde58dbcSMatthew Ahrens uint64_t offset, blkoff; 224cde58dbcSMatthew Ahrens bpobj_t sublist; 225cde58dbcSMatthew Ahrens uint64_t used_before, comp_before, uncomp_before; 226cde58dbcSMatthew Ahrens uint64_t used_after, comp_after, uncomp_after; 227cde58dbcSMatthew Ahrens 228cde58dbcSMatthew Ahrens offset = i * sizeof (uint64_t); 229cde58dbcSMatthew Ahrens blkoff = P2PHASE(i, epb); 230cde58dbcSMatthew Ahrens 231cde58dbcSMatthew Ahrens if (dbuf == NULL || dbuf->db_offset > offset) { 232cde58dbcSMatthew Ahrens if (dbuf) 233cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 234cde58dbcSMatthew Ahrens err = dmu_buf_hold(bpo->bpo_os, 235cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0); 236cde58dbcSMatthew Ahrens if (err) 237cde58dbcSMatthew Ahrens break; 238cde58dbcSMatthew Ahrens } 239cde58dbcSMatthew Ahrens 240cde58dbcSMatthew Ahrens ASSERT3U(offset, >=, dbuf->db_offset); 241cde58dbcSMatthew Ahrens ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 242cde58dbcSMatthew Ahrens 243cde58dbcSMatthew Ahrens objarray = dbuf->db_data; 244cde58dbcSMatthew Ahrens err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]); 245cde58dbcSMatthew Ahrens if (err) 246cde58dbcSMatthew Ahrens break; 247cde58dbcSMatthew Ahrens if (free) { 248cde58dbcSMatthew Ahrens err = bpobj_space(&sublist, 249cde58dbcSMatthew Ahrens &used_before, &comp_before, &uncomp_before); 250cde58dbcSMatthew Ahrens if (err) 251cde58dbcSMatthew Ahrens break; 252cde58dbcSMatthew Ahrens } 253cde58dbcSMatthew Ahrens err = bpobj_iterate_impl(&sublist, func, arg, tx, free); 254cde58dbcSMatthew Ahrens if (free) { 255*b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&sublist, 256cde58dbcSMatthew Ahrens &used_after, &comp_after, &uncomp_after)); 257cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes -= used_before - used_after; 258cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 2594bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_comp -= comp_before - comp_after; 260cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp -= 261cde58dbcSMatthew Ahrens uncomp_before - uncomp_after; 262cde58dbcSMatthew Ahrens } 263cde58dbcSMatthew Ahrens 264cde58dbcSMatthew Ahrens bpobj_close(&sublist); 265cde58dbcSMatthew Ahrens if (err) 266cde58dbcSMatthew Ahrens break; 267cde58dbcSMatthew Ahrens if (free) { 268cde58dbcSMatthew Ahrens err = dmu_object_free(bpo->bpo_os, 269cde58dbcSMatthew Ahrens objarray[blkoff], tx); 270cde58dbcSMatthew Ahrens if (err) 271cde58dbcSMatthew Ahrens break; 272cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs--; 273cde58dbcSMatthew Ahrens ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0); 274cde58dbcSMatthew Ahrens } 275cde58dbcSMatthew Ahrens } 276cde58dbcSMatthew Ahrens if (dbuf) { 277cde58dbcSMatthew Ahrens dmu_buf_rele(dbuf, FTAG); 278cde58dbcSMatthew Ahrens dbuf = NULL; 279cde58dbcSMatthew Ahrens } 280cde58dbcSMatthew Ahrens if (free) { 281*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, 282cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs, 283cde58dbcSMatthew Ahrens (i + 1) * sizeof (uint64_t), -1ULL, tx)); 284cde58dbcSMatthew Ahrens } 285cde58dbcSMatthew Ahrens 286cde58dbcSMatthew Ahrens out: 287cde58dbcSMatthew Ahrens /* If there are no entries, there should be no bytes. */ 288cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_phys->bpo_num_blkptrs > 0 || 289cde58dbcSMatthew Ahrens (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs > 0) || 290cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes == 0); 291cde58dbcSMatthew Ahrens 292cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 293cde58dbcSMatthew Ahrens return (err); 294cde58dbcSMatthew Ahrens } 295cde58dbcSMatthew Ahrens 296cde58dbcSMatthew Ahrens /* 297cde58dbcSMatthew Ahrens * Iterate and remove the entries. If func returns nonzero, iteration 298cde58dbcSMatthew Ahrens * will stop and that entry will not be removed. 299cde58dbcSMatthew Ahrens */ 300cde58dbcSMatthew Ahrens int 301cde58dbcSMatthew Ahrens bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 302cde58dbcSMatthew Ahrens { 303cde58dbcSMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE)); 304cde58dbcSMatthew Ahrens } 305cde58dbcSMatthew Ahrens 306cde58dbcSMatthew Ahrens /* 307cde58dbcSMatthew Ahrens * Iterate the entries. If func returns nonzero, iteration will stop. 308cde58dbcSMatthew Ahrens */ 309cde58dbcSMatthew Ahrens int 310cde58dbcSMatthew Ahrens bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 311cde58dbcSMatthew Ahrens { 312cde58dbcSMatthew Ahrens return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE)); 313cde58dbcSMatthew Ahrens } 314cde58dbcSMatthew Ahrens 315cde58dbcSMatthew Ahrens void 316cde58dbcSMatthew Ahrens bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx) 317cde58dbcSMatthew Ahrens { 318cde58dbcSMatthew Ahrens bpobj_t subbpo; 3194bc4cb79SMatthew Ahrens uint64_t used, comp, uncomp, subsubobjs; 320cde58dbcSMatthew Ahrens 321cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havesubobj); 322cde58dbcSMatthew Ahrens ASSERT(bpo->bpo_havecomp); 323cde58dbcSMatthew Ahrens 324*b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj)); 325*b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp)); 326cde58dbcSMatthew Ahrens 327cde58dbcSMatthew Ahrens if (used == 0) { 328cde58dbcSMatthew Ahrens /* No point in having an empty subobj. */ 3294bc4cb79SMatthew Ahrens bpobj_close(&subbpo); 330cde58dbcSMatthew Ahrens bpobj_free(bpo->bpo_os, subobj, tx); 331cde58dbcSMatthew Ahrens return; 332cde58dbcSMatthew Ahrens } 333cde58dbcSMatthew Ahrens 334cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 335cde58dbcSMatthew Ahrens if (bpo->bpo_phys->bpo_subobjs == 0) { 336cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os, 337cde58dbcSMatthew Ahrens DMU_OT_BPOBJ_SUBOBJ, SPA_MAXBLOCKSIZE, DMU_OT_NONE, 0, tx); 338cde58dbcSMatthew Ahrens } 339cde58dbcSMatthew Ahrens 340cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 341cde58dbcSMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 342cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 343cde58dbcSMatthew Ahrens sizeof (subobj), &subobj, tx); 344cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs++; 3454bc4cb79SMatthew Ahrens 3464bc4cb79SMatthew Ahrens /* 3474bc4cb79SMatthew Ahrens * If subobj has only one block of subobjs, then move subobj's 3484bc4cb79SMatthew Ahrens * subobjs to bpo's subobj list directly. This reduces 3494bc4cb79SMatthew Ahrens * recursion in bpobj_iterate due to nested subobjs. 3504bc4cb79SMatthew Ahrens */ 3514bc4cb79SMatthew Ahrens subsubobjs = subbpo.bpo_phys->bpo_subobjs; 3524bc4cb79SMatthew Ahrens if (subsubobjs != 0) { 3534bc4cb79SMatthew Ahrens dmu_object_info_t doi; 3544bc4cb79SMatthew Ahrens 355*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi)); 3564bc4cb79SMatthew Ahrens if (doi.doi_max_offset == doi.doi_data_block_size) { 3574bc4cb79SMatthew Ahrens dmu_buf_t *subdb; 3584bc4cb79SMatthew Ahrens uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs; 3594bc4cb79SMatthew Ahrens 360*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs, 3614bc4cb79SMatthew Ahrens 0, FTAG, &subdb, 0)); 3624bc4cb79SMatthew Ahrens dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 3634bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 3644bc4cb79SMatthew Ahrens numsubsub * sizeof (subobj), subdb->db_data, tx); 3654bc4cb79SMatthew Ahrens dmu_buf_rele(subdb, FTAG); 3664bc4cb79SMatthew Ahrens bpo->bpo_phys->bpo_num_subobjs += numsubsub; 3674bc4cb79SMatthew Ahrens 3684bc4cb79SMatthew Ahrens dmu_buf_will_dirty(subbpo.bpo_dbuf, tx); 3694bc4cb79SMatthew Ahrens subbpo.bpo_phys->bpo_subobjs = 0; 370*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os, 3714bc4cb79SMatthew Ahrens subsubobjs, tx)); 3724bc4cb79SMatthew Ahrens } 3734bc4cb79SMatthew Ahrens } 374cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes += used; 375cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp += comp; 376cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp += uncomp; 377cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 3784bc4cb79SMatthew Ahrens 3794bc4cb79SMatthew Ahrens bpobj_close(&subbpo); 380cde58dbcSMatthew Ahrens } 381cde58dbcSMatthew Ahrens 382cde58dbcSMatthew Ahrens void 383cde58dbcSMatthew Ahrens bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx) 384cde58dbcSMatthew Ahrens { 385cde58dbcSMatthew Ahrens blkptr_t stored_bp = *bp; 386cde58dbcSMatthew Ahrens uint64_t offset; 387cde58dbcSMatthew Ahrens int blkoff; 388cde58dbcSMatthew Ahrens blkptr_t *bparray; 389cde58dbcSMatthew Ahrens 390cde58dbcSMatthew Ahrens ASSERT(!BP_IS_HOLE(bp)); 391cde58dbcSMatthew Ahrens 392cde58dbcSMatthew Ahrens /* We never need the fill count. */ 393cde58dbcSMatthew Ahrens stored_bp.blk_fill = 0; 394cde58dbcSMatthew Ahrens 395cde58dbcSMatthew Ahrens /* The bpobj will compress better if we can leave off the checksum */ 396cde58dbcSMatthew Ahrens if (!BP_GET_DEDUP(bp)) 397cde58dbcSMatthew Ahrens bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum)); 398cde58dbcSMatthew Ahrens 399cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 400cde58dbcSMatthew Ahrens 401cde58dbcSMatthew Ahrens offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp); 402cde58dbcSMatthew Ahrens blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb); 403cde58dbcSMatthew Ahrens 404cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf == NULL || 405cde58dbcSMatthew Ahrens offset < bpo->bpo_cached_dbuf->db_offset || 406cde58dbcSMatthew Ahrens offset >= bpo->bpo_cached_dbuf->db_offset + 407cde58dbcSMatthew Ahrens bpo->bpo_cached_dbuf->db_size) { 408cde58dbcSMatthew Ahrens if (bpo->bpo_cached_dbuf) 409cde58dbcSMatthew Ahrens dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 410*b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, 411cde58dbcSMatthew Ahrens offset, bpo, &bpo->bpo_cached_dbuf, 0)); 412cde58dbcSMatthew Ahrens } 413cde58dbcSMatthew Ahrens 414cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx); 415cde58dbcSMatthew Ahrens bparray = bpo->bpo_cached_dbuf->db_data; 416cde58dbcSMatthew Ahrens bparray[blkoff] = stored_bp; 417cde58dbcSMatthew Ahrens 418cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 419cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_num_blkptrs++; 420cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_bytes += 421cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 422cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 423cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp); 424cde58dbcSMatthew Ahrens bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp); 425cde58dbcSMatthew Ahrens } 426cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 427cde58dbcSMatthew Ahrens } 428cde58dbcSMatthew Ahrens 429cde58dbcSMatthew Ahrens struct space_range_arg { 430cde58dbcSMatthew Ahrens spa_t *spa; 431cde58dbcSMatthew Ahrens uint64_t mintxg; 432cde58dbcSMatthew Ahrens uint64_t maxtxg; 433cde58dbcSMatthew Ahrens uint64_t used; 434cde58dbcSMatthew Ahrens uint64_t comp; 435cde58dbcSMatthew Ahrens uint64_t uncomp; 436cde58dbcSMatthew Ahrens }; 437cde58dbcSMatthew Ahrens 438cde58dbcSMatthew Ahrens /* ARGSUSED */ 439cde58dbcSMatthew Ahrens static int 440cde58dbcSMatthew Ahrens space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 441cde58dbcSMatthew Ahrens { 442cde58dbcSMatthew Ahrens struct space_range_arg *sra = arg; 443cde58dbcSMatthew Ahrens 444cde58dbcSMatthew Ahrens if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) { 44519b94df9SMatthew Ahrens if (dsl_pool_sync_context(spa_get_dsl(sra->spa))) 44619b94df9SMatthew Ahrens sra->used += bp_get_dsize_sync(sra->spa, bp); 44719b94df9SMatthew Ahrens else 44819b94df9SMatthew Ahrens sra->used += bp_get_dsize(sra->spa, bp); 449cde58dbcSMatthew Ahrens sra->comp += BP_GET_PSIZE(bp); 450cde58dbcSMatthew Ahrens sra->uncomp += BP_GET_UCSIZE(bp); 451cde58dbcSMatthew Ahrens } 452cde58dbcSMatthew Ahrens return (0); 453cde58dbcSMatthew Ahrens } 454cde58dbcSMatthew Ahrens 455cde58dbcSMatthew Ahrens int 456cde58dbcSMatthew Ahrens bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 457cde58dbcSMatthew Ahrens { 458cde58dbcSMatthew Ahrens mutex_enter(&bpo->bpo_lock); 459cde58dbcSMatthew Ahrens 460cde58dbcSMatthew Ahrens *usedp = bpo->bpo_phys->bpo_bytes; 461cde58dbcSMatthew Ahrens if (bpo->bpo_havecomp) { 462cde58dbcSMatthew Ahrens *compp = bpo->bpo_phys->bpo_comp; 463cde58dbcSMatthew Ahrens *uncompp = bpo->bpo_phys->bpo_uncomp; 464cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 465cde58dbcSMatthew Ahrens return (0); 466cde58dbcSMatthew Ahrens } else { 467cde58dbcSMatthew Ahrens mutex_exit(&bpo->bpo_lock); 468cde58dbcSMatthew Ahrens return (bpobj_space_range(bpo, 0, UINT64_MAX, 469cde58dbcSMatthew Ahrens usedp, compp, uncompp)); 470cde58dbcSMatthew Ahrens } 471cde58dbcSMatthew Ahrens } 472cde58dbcSMatthew Ahrens 473cde58dbcSMatthew Ahrens /* 474cde58dbcSMatthew Ahrens * Return the amount of space in the bpobj which is: 475cde58dbcSMatthew Ahrens * mintxg < blk_birth <= maxtxg 476cde58dbcSMatthew Ahrens */ 477cde58dbcSMatthew Ahrens int 478cde58dbcSMatthew Ahrens bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg, 479cde58dbcSMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 480cde58dbcSMatthew Ahrens { 481cde58dbcSMatthew Ahrens struct space_range_arg sra = { 0 }; 482cde58dbcSMatthew Ahrens int err; 483cde58dbcSMatthew Ahrens 484cde58dbcSMatthew Ahrens /* 485cde58dbcSMatthew Ahrens * As an optimization, if they want the whole txg range, just 486cde58dbcSMatthew Ahrens * get bpo_bytes rather than iterating over the bps. 487cde58dbcSMatthew Ahrens */ 488cde58dbcSMatthew Ahrens if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp) 489cde58dbcSMatthew Ahrens return (bpobj_space(bpo, usedp, compp, uncompp)); 490cde58dbcSMatthew Ahrens 491cde58dbcSMatthew Ahrens sra.spa = dmu_objset_spa(bpo->bpo_os); 492cde58dbcSMatthew Ahrens sra.mintxg = mintxg; 493cde58dbcSMatthew Ahrens sra.maxtxg = maxtxg; 494cde58dbcSMatthew Ahrens 495cde58dbcSMatthew Ahrens err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL); 496cde58dbcSMatthew Ahrens *usedp = sra.used; 497cde58dbcSMatthew Ahrens *compp = sra.comp; 498cde58dbcSMatthew Ahrens *uncompp = sra.uncomp; 499cde58dbcSMatthew Ahrens return (err); 500cde58dbcSMatthew Ahrens } 501