1*12470SMatthew.Ahrens@Sun.COM /* 2*12470SMatthew.Ahrens@Sun.COM * CDDL HEADER START 3*12470SMatthew.Ahrens@Sun.COM * 4*12470SMatthew.Ahrens@Sun.COM * The contents of this file are subject to the terms of the 5*12470SMatthew.Ahrens@Sun.COM * Common Development and Distribution License (the "License"). 6*12470SMatthew.Ahrens@Sun.COM * You may not use this file except in compliance with the License. 7*12470SMatthew.Ahrens@Sun.COM * 8*12470SMatthew.Ahrens@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*12470SMatthew.Ahrens@Sun.COM * or http://www.opensolaris.org/os/licensing. 10*12470SMatthew.Ahrens@Sun.COM * See the License for the specific language governing permissions 11*12470SMatthew.Ahrens@Sun.COM * and limitations under the License. 12*12470SMatthew.Ahrens@Sun.COM * 13*12470SMatthew.Ahrens@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 14*12470SMatthew.Ahrens@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*12470SMatthew.Ahrens@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 16*12470SMatthew.Ahrens@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 17*12470SMatthew.Ahrens@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 18*12470SMatthew.Ahrens@Sun.COM * 19*12470SMatthew.Ahrens@Sun.COM * CDDL HEADER END 20*12470SMatthew.Ahrens@Sun.COM */ 21*12470SMatthew.Ahrens@Sun.COM /* 22*12470SMatthew.Ahrens@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23*12470SMatthew.Ahrens@Sun.COM */ 24*12470SMatthew.Ahrens@Sun.COM 25*12470SMatthew.Ahrens@Sun.COM #include <sys/bpobj.h> 26*12470SMatthew.Ahrens@Sun.COM #include <sys/zfs_context.h> 27*12470SMatthew.Ahrens@Sun.COM #include <sys/refcount.h> 28*12470SMatthew.Ahrens@Sun.COM 29*12470SMatthew.Ahrens@Sun.COM uint64_t 30*12470SMatthew.Ahrens@Sun.COM bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx) 31*12470SMatthew.Ahrens@Sun.COM { 32*12470SMatthew.Ahrens@Sun.COM int size; 33*12470SMatthew.Ahrens@Sun.COM 34*12470SMatthew.Ahrens@Sun.COM if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT) 35*12470SMatthew.Ahrens@Sun.COM size = BPOBJ_SIZE_V0; 36*12470SMatthew.Ahrens@Sun.COM else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS) 37*12470SMatthew.Ahrens@Sun.COM size = BPOBJ_SIZE_V1; 38*12470SMatthew.Ahrens@Sun.COM else 39*12470SMatthew.Ahrens@Sun.COM size = sizeof (bpobj_phys_t); 40*12470SMatthew.Ahrens@Sun.COM 41*12470SMatthew.Ahrens@Sun.COM return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize, 42*12470SMatthew.Ahrens@Sun.COM DMU_OT_BPOBJ_HDR, size, tx)); 43*12470SMatthew.Ahrens@Sun.COM } 44*12470SMatthew.Ahrens@Sun.COM 45*12470SMatthew.Ahrens@Sun.COM void 46*12470SMatthew.Ahrens@Sun.COM bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx) 47*12470SMatthew.Ahrens@Sun.COM { 48*12470SMatthew.Ahrens@Sun.COM int64_t i; 49*12470SMatthew.Ahrens@Sun.COM bpobj_t bpo; 50*12470SMatthew.Ahrens@Sun.COM dmu_object_info_t doi; 51*12470SMatthew.Ahrens@Sun.COM int epb; 52*12470SMatthew.Ahrens@Sun.COM dmu_buf_t *dbuf = NULL; 53*12470SMatthew.Ahrens@Sun.COM 54*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&bpo, os, obj)); 55*12470SMatthew.Ahrens@Sun.COM 56*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpo.bpo_lock); 57*12470SMatthew.Ahrens@Sun.COM 58*12470SMatthew.Ahrens@Sun.COM if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0) 59*12470SMatthew.Ahrens@Sun.COM goto out; 60*12470SMatthew.Ahrens@Sun.COM 61*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi)); 62*12470SMatthew.Ahrens@Sun.COM epb = doi.doi_data_block_size / sizeof (uint64_t); 63*12470SMatthew.Ahrens@Sun.COM 64*12470SMatthew.Ahrens@Sun.COM for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 65*12470SMatthew.Ahrens@Sun.COM uint64_t *objarray; 66*12470SMatthew.Ahrens@Sun.COM uint64_t offset, blkoff; 67*12470SMatthew.Ahrens@Sun.COM 68*12470SMatthew.Ahrens@Sun.COM offset = i * sizeof (uint64_t); 69*12470SMatthew.Ahrens@Sun.COM blkoff = P2PHASE(i, epb); 70*12470SMatthew.Ahrens@Sun.COM 71*12470SMatthew.Ahrens@Sun.COM if (dbuf == NULL || dbuf->db_offset > offset) { 72*12470SMatthew.Ahrens@Sun.COM if (dbuf) 73*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, FTAG); 74*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_buf_hold(os, 75*12470SMatthew.Ahrens@Sun.COM bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0)); 76*12470SMatthew.Ahrens@Sun.COM } 77*12470SMatthew.Ahrens@Sun.COM 78*12470SMatthew.Ahrens@Sun.COM ASSERT3U(offset, >=, dbuf->db_offset); 79*12470SMatthew.Ahrens@Sun.COM ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 80*12470SMatthew.Ahrens@Sun.COM 81*12470SMatthew.Ahrens@Sun.COM objarray = dbuf->db_data; 82*12470SMatthew.Ahrens@Sun.COM bpobj_free(os, objarray[blkoff], tx); 83*12470SMatthew.Ahrens@Sun.COM } 84*12470SMatthew.Ahrens@Sun.COM if (dbuf) { 85*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, FTAG); 86*12470SMatthew.Ahrens@Sun.COM dbuf = NULL; 87*12470SMatthew.Ahrens@Sun.COM } 88*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx)); 89*12470SMatthew.Ahrens@Sun.COM 90*12470SMatthew.Ahrens@Sun.COM out: 91*12470SMatthew.Ahrens@Sun.COM mutex_exit(&bpo.bpo_lock); 92*12470SMatthew.Ahrens@Sun.COM bpobj_close(&bpo); 93*12470SMatthew.Ahrens@Sun.COM 94*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_object_free(os, obj, tx)); 95*12470SMatthew.Ahrens@Sun.COM } 96*12470SMatthew.Ahrens@Sun.COM 97*12470SMatthew.Ahrens@Sun.COM int 98*12470SMatthew.Ahrens@Sun.COM bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object) 99*12470SMatthew.Ahrens@Sun.COM { 100*12470SMatthew.Ahrens@Sun.COM dmu_object_info_t doi; 101*12470SMatthew.Ahrens@Sun.COM int err; 102*12470SMatthew.Ahrens@Sun.COM 103*12470SMatthew.Ahrens@Sun.COM err = dmu_object_info(os, object, &doi); 104*12470SMatthew.Ahrens@Sun.COM if (err) 105*12470SMatthew.Ahrens@Sun.COM return (err); 106*12470SMatthew.Ahrens@Sun.COM 107*12470SMatthew.Ahrens@Sun.COM bzero(bpo, sizeof (*bpo)); 108*12470SMatthew.Ahrens@Sun.COM mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL); 109*12470SMatthew.Ahrens@Sun.COM 110*12470SMatthew.Ahrens@Sun.COM ASSERT(bpo->bpo_dbuf == NULL); 111*12470SMatthew.Ahrens@Sun.COM ASSERT(bpo->bpo_phys == NULL); 112*12470SMatthew.Ahrens@Sun.COM ASSERT(object != 0); 113*12470SMatthew.Ahrens@Sun.COM ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ); 114*12470SMatthew.Ahrens@Sun.COM ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR); 115*12470SMatthew.Ahrens@Sun.COM 116*12470SMatthew.Ahrens@Sun.COM bpo->bpo_os = os; 117*12470SMatthew.Ahrens@Sun.COM bpo->bpo_object = object; 118*12470SMatthew.Ahrens@Sun.COM bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT; 119*12470SMatthew.Ahrens@Sun.COM bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0); 120*12470SMatthew.Ahrens@Sun.COM bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1); 121*12470SMatthew.Ahrens@Sun.COM 122*12470SMatthew.Ahrens@Sun.COM err = dmu_bonus_hold(bpo->bpo_os, 123*12470SMatthew.Ahrens@Sun.COM bpo->bpo_object, bpo, &bpo->bpo_dbuf); 124*12470SMatthew.Ahrens@Sun.COM if (err) 125*12470SMatthew.Ahrens@Sun.COM return (err); 126*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys = bpo->bpo_dbuf->db_data; 127*12470SMatthew.Ahrens@Sun.COM return (0); 128*12470SMatthew.Ahrens@Sun.COM } 129*12470SMatthew.Ahrens@Sun.COM 130*12470SMatthew.Ahrens@Sun.COM void 131*12470SMatthew.Ahrens@Sun.COM bpobj_close(bpobj_t *bpo) 132*12470SMatthew.Ahrens@Sun.COM { 133*12470SMatthew.Ahrens@Sun.COM /* Lame workaround for closing a bpobj that was never opened. */ 134*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_object == 0) 135*12470SMatthew.Ahrens@Sun.COM return; 136*12470SMatthew.Ahrens@Sun.COM 137*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(bpo->bpo_dbuf, bpo); 138*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_cached_dbuf != NULL) 139*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 140*12470SMatthew.Ahrens@Sun.COM bpo->bpo_dbuf = NULL; 141*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys = NULL; 142*12470SMatthew.Ahrens@Sun.COM bpo->bpo_cached_dbuf = NULL; 143*12470SMatthew.Ahrens@Sun.COM 144*12470SMatthew.Ahrens@Sun.COM mutex_destroy(&bpo->bpo_lock); 145*12470SMatthew.Ahrens@Sun.COM } 146*12470SMatthew.Ahrens@Sun.COM 147*12470SMatthew.Ahrens@Sun.COM static int 148*12470SMatthew.Ahrens@Sun.COM bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx, 149*12470SMatthew.Ahrens@Sun.COM boolean_t free) 150*12470SMatthew.Ahrens@Sun.COM { 151*12470SMatthew.Ahrens@Sun.COM dmu_object_info_t doi; 152*12470SMatthew.Ahrens@Sun.COM int epb; 153*12470SMatthew.Ahrens@Sun.COM int64_t i; 154*12470SMatthew.Ahrens@Sun.COM int err = 0; 155*12470SMatthew.Ahrens@Sun.COM dmu_buf_t *dbuf = NULL; 156*12470SMatthew.Ahrens@Sun.COM 157*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpo->bpo_lock); 158*12470SMatthew.Ahrens@Sun.COM 159*12470SMatthew.Ahrens@Sun.COM if (free) 160*12470SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 161*12470SMatthew.Ahrens@Sun.COM 162*12470SMatthew.Ahrens@Sun.COM for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) { 163*12470SMatthew.Ahrens@Sun.COM blkptr_t *bparray; 164*12470SMatthew.Ahrens@Sun.COM blkptr_t *bp; 165*12470SMatthew.Ahrens@Sun.COM uint64_t offset, blkoff; 166*12470SMatthew.Ahrens@Sun.COM 167*12470SMatthew.Ahrens@Sun.COM offset = i * sizeof (blkptr_t); 168*12470SMatthew.Ahrens@Sun.COM blkoff = P2PHASE(i, bpo->bpo_epb); 169*12470SMatthew.Ahrens@Sun.COM 170*12470SMatthew.Ahrens@Sun.COM if (dbuf == NULL || dbuf->db_offset > offset) { 171*12470SMatthew.Ahrens@Sun.COM if (dbuf) 172*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, FTAG); 173*12470SMatthew.Ahrens@Sun.COM err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset, 174*12470SMatthew.Ahrens@Sun.COM FTAG, &dbuf, 0); 175*12470SMatthew.Ahrens@Sun.COM if (err) 176*12470SMatthew.Ahrens@Sun.COM break; 177*12470SMatthew.Ahrens@Sun.COM } 178*12470SMatthew.Ahrens@Sun.COM 179*12470SMatthew.Ahrens@Sun.COM ASSERT3U(offset, >=, dbuf->db_offset); 180*12470SMatthew.Ahrens@Sun.COM ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 181*12470SMatthew.Ahrens@Sun.COM 182*12470SMatthew.Ahrens@Sun.COM bparray = dbuf->db_data; 183*12470SMatthew.Ahrens@Sun.COM bp = &bparray[blkoff]; 184*12470SMatthew.Ahrens@Sun.COM err = func(arg, bp, tx); 185*12470SMatthew.Ahrens@Sun.COM if (err) 186*12470SMatthew.Ahrens@Sun.COM break; 187*12470SMatthew.Ahrens@Sun.COM if (free) { 188*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_bytes -= 189*12470SMatthew.Ahrens@Sun.COM bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 190*12470SMatthew.Ahrens@Sun.COM ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 191*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_havecomp) { 192*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp); 193*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp); 194*12470SMatthew.Ahrens@Sun.COM } 195*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_num_blkptrs--; 196*12470SMatthew.Ahrens@Sun.COM ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0); 197*12470SMatthew.Ahrens@Sun.COM } 198*12470SMatthew.Ahrens@Sun.COM } 199*12470SMatthew.Ahrens@Sun.COM if (dbuf) { 200*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, FTAG); 201*12470SMatthew.Ahrens@Sun.COM dbuf = NULL; 202*12470SMatthew.Ahrens@Sun.COM } 203*12470SMatthew.Ahrens@Sun.COM if (free) { 204*12470SMatthew.Ahrens@Sun.COM i++; 205*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object, 206*12470SMatthew.Ahrens@Sun.COM i * sizeof (blkptr_t), -1ULL, tx)); 207*12470SMatthew.Ahrens@Sun.COM } 208*12470SMatthew.Ahrens@Sun.COM if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0) 209*12470SMatthew.Ahrens@Sun.COM goto out; 210*12470SMatthew.Ahrens@Sun.COM 211*12470SMatthew.Ahrens@Sun.COM ASSERT(bpo->bpo_havecomp); 212*12470SMatthew.Ahrens@Sun.COM err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi); 213*12470SMatthew.Ahrens@Sun.COM if (err) 214*12470SMatthew.Ahrens@Sun.COM return (err); 215*12470SMatthew.Ahrens@Sun.COM epb = doi.doi_data_block_size / sizeof (uint64_t); 216*12470SMatthew.Ahrens@Sun.COM 217*12470SMatthew.Ahrens@Sun.COM for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) { 218*12470SMatthew.Ahrens@Sun.COM uint64_t *objarray; 219*12470SMatthew.Ahrens@Sun.COM uint64_t offset, blkoff; 220*12470SMatthew.Ahrens@Sun.COM bpobj_t sublist; 221*12470SMatthew.Ahrens@Sun.COM uint64_t used_before, comp_before, uncomp_before; 222*12470SMatthew.Ahrens@Sun.COM uint64_t used_after, comp_after, uncomp_after; 223*12470SMatthew.Ahrens@Sun.COM 224*12470SMatthew.Ahrens@Sun.COM offset = i * sizeof (uint64_t); 225*12470SMatthew.Ahrens@Sun.COM blkoff = P2PHASE(i, epb); 226*12470SMatthew.Ahrens@Sun.COM 227*12470SMatthew.Ahrens@Sun.COM if (dbuf == NULL || dbuf->db_offset > offset) { 228*12470SMatthew.Ahrens@Sun.COM if (dbuf) 229*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, FTAG); 230*12470SMatthew.Ahrens@Sun.COM err = dmu_buf_hold(bpo->bpo_os, 231*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0); 232*12470SMatthew.Ahrens@Sun.COM if (err) 233*12470SMatthew.Ahrens@Sun.COM break; 234*12470SMatthew.Ahrens@Sun.COM } 235*12470SMatthew.Ahrens@Sun.COM 236*12470SMatthew.Ahrens@Sun.COM ASSERT3U(offset, >=, dbuf->db_offset); 237*12470SMatthew.Ahrens@Sun.COM ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size); 238*12470SMatthew.Ahrens@Sun.COM 239*12470SMatthew.Ahrens@Sun.COM objarray = dbuf->db_data; 240*12470SMatthew.Ahrens@Sun.COM err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]); 241*12470SMatthew.Ahrens@Sun.COM if (err) 242*12470SMatthew.Ahrens@Sun.COM break; 243*12470SMatthew.Ahrens@Sun.COM if (free) { 244*12470SMatthew.Ahrens@Sun.COM err = bpobj_space(&sublist, 245*12470SMatthew.Ahrens@Sun.COM &used_before, &comp_before, &uncomp_before); 246*12470SMatthew.Ahrens@Sun.COM if (err) 247*12470SMatthew.Ahrens@Sun.COM break; 248*12470SMatthew.Ahrens@Sun.COM } 249*12470SMatthew.Ahrens@Sun.COM err = bpobj_iterate_impl(&sublist, func, arg, tx, free); 250*12470SMatthew.Ahrens@Sun.COM if (free) { 251*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_space(&sublist, 252*12470SMatthew.Ahrens@Sun.COM &used_after, &comp_after, &uncomp_after)); 253*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_bytes -= used_before - used_after; 254*12470SMatthew.Ahrens@Sun.COM ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0); 255*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_comp -= comp_before - used_after; 256*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_uncomp -= 257*12470SMatthew.Ahrens@Sun.COM uncomp_before - uncomp_after; 258*12470SMatthew.Ahrens@Sun.COM } 259*12470SMatthew.Ahrens@Sun.COM 260*12470SMatthew.Ahrens@Sun.COM bpobj_close(&sublist); 261*12470SMatthew.Ahrens@Sun.COM if (err) 262*12470SMatthew.Ahrens@Sun.COM break; 263*12470SMatthew.Ahrens@Sun.COM if (free) { 264*12470SMatthew.Ahrens@Sun.COM err = dmu_object_free(bpo->bpo_os, 265*12470SMatthew.Ahrens@Sun.COM objarray[blkoff], tx); 266*12470SMatthew.Ahrens@Sun.COM if (err) 267*12470SMatthew.Ahrens@Sun.COM break; 268*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_num_subobjs--; 269*12470SMatthew.Ahrens@Sun.COM ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0); 270*12470SMatthew.Ahrens@Sun.COM } 271*12470SMatthew.Ahrens@Sun.COM } 272*12470SMatthew.Ahrens@Sun.COM if (dbuf) { 273*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, FTAG); 274*12470SMatthew.Ahrens@Sun.COM dbuf = NULL; 275*12470SMatthew.Ahrens@Sun.COM } 276*12470SMatthew.Ahrens@Sun.COM if (free) { 277*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, 278*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_subobjs, 279*12470SMatthew.Ahrens@Sun.COM (i + 1) * sizeof (uint64_t), -1ULL, tx)); 280*12470SMatthew.Ahrens@Sun.COM } 281*12470SMatthew.Ahrens@Sun.COM 282*12470SMatthew.Ahrens@Sun.COM out: 283*12470SMatthew.Ahrens@Sun.COM /* If there are no entries, there should be no bytes. */ 284*12470SMatthew.Ahrens@Sun.COM ASSERT(bpo->bpo_phys->bpo_num_blkptrs > 0 || 285*12470SMatthew.Ahrens@Sun.COM (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs > 0) || 286*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_bytes == 0); 287*12470SMatthew.Ahrens@Sun.COM 288*12470SMatthew.Ahrens@Sun.COM mutex_exit(&bpo->bpo_lock); 289*12470SMatthew.Ahrens@Sun.COM return (err); 290*12470SMatthew.Ahrens@Sun.COM } 291*12470SMatthew.Ahrens@Sun.COM 292*12470SMatthew.Ahrens@Sun.COM /* 293*12470SMatthew.Ahrens@Sun.COM * Iterate and remove the entries. If func returns nonzero, iteration 294*12470SMatthew.Ahrens@Sun.COM * will stop and that entry will not be removed. 295*12470SMatthew.Ahrens@Sun.COM */ 296*12470SMatthew.Ahrens@Sun.COM int 297*12470SMatthew.Ahrens@Sun.COM bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 298*12470SMatthew.Ahrens@Sun.COM { 299*12470SMatthew.Ahrens@Sun.COM return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE)); 300*12470SMatthew.Ahrens@Sun.COM } 301*12470SMatthew.Ahrens@Sun.COM 302*12470SMatthew.Ahrens@Sun.COM /* 303*12470SMatthew.Ahrens@Sun.COM * Iterate the entries. If func returns nonzero, iteration will stop. 304*12470SMatthew.Ahrens@Sun.COM */ 305*12470SMatthew.Ahrens@Sun.COM int 306*12470SMatthew.Ahrens@Sun.COM bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx) 307*12470SMatthew.Ahrens@Sun.COM { 308*12470SMatthew.Ahrens@Sun.COM return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE)); 309*12470SMatthew.Ahrens@Sun.COM } 310*12470SMatthew.Ahrens@Sun.COM 311*12470SMatthew.Ahrens@Sun.COM void 312*12470SMatthew.Ahrens@Sun.COM bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx) 313*12470SMatthew.Ahrens@Sun.COM { 314*12470SMatthew.Ahrens@Sun.COM bpobj_t subbpo; 315*12470SMatthew.Ahrens@Sun.COM uint64_t used, comp, uncomp; 316*12470SMatthew.Ahrens@Sun.COM 317*12470SMatthew.Ahrens@Sun.COM ASSERT(bpo->bpo_havesubobj); 318*12470SMatthew.Ahrens@Sun.COM ASSERT(bpo->bpo_havecomp); 319*12470SMatthew.Ahrens@Sun.COM 320*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj)); 321*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp)); 322*12470SMatthew.Ahrens@Sun.COM bpobj_close(&subbpo); 323*12470SMatthew.Ahrens@Sun.COM 324*12470SMatthew.Ahrens@Sun.COM if (used == 0) { 325*12470SMatthew.Ahrens@Sun.COM /* No point in having an empty subobj. */ 326*12470SMatthew.Ahrens@Sun.COM bpobj_free(bpo->bpo_os, subobj, tx); 327*12470SMatthew.Ahrens@Sun.COM return; 328*12470SMatthew.Ahrens@Sun.COM } 329*12470SMatthew.Ahrens@Sun.COM 330*12470SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 331*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_phys->bpo_subobjs == 0) { 332*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os, 333*12470SMatthew.Ahrens@Sun.COM DMU_OT_BPOBJ_SUBOBJ, SPA_MAXBLOCKSIZE, DMU_OT_NONE, 0, tx); 334*12470SMatthew.Ahrens@Sun.COM } 335*12470SMatthew.Ahrens@Sun.COM 336*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpo->bpo_lock); 337*12470SMatthew.Ahrens@Sun.COM dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 338*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 339*12470SMatthew.Ahrens@Sun.COM sizeof (subobj), &subobj, tx); 340*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_num_subobjs++; 341*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_bytes += used; 342*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_comp += comp; 343*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_uncomp += uncomp; 344*12470SMatthew.Ahrens@Sun.COM mutex_exit(&bpo->bpo_lock); 345*12470SMatthew.Ahrens@Sun.COM } 346*12470SMatthew.Ahrens@Sun.COM 347*12470SMatthew.Ahrens@Sun.COM void 348*12470SMatthew.Ahrens@Sun.COM bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx) 349*12470SMatthew.Ahrens@Sun.COM { 350*12470SMatthew.Ahrens@Sun.COM blkptr_t stored_bp = *bp; 351*12470SMatthew.Ahrens@Sun.COM uint64_t offset; 352*12470SMatthew.Ahrens@Sun.COM int blkoff; 353*12470SMatthew.Ahrens@Sun.COM blkptr_t *bparray; 354*12470SMatthew.Ahrens@Sun.COM 355*12470SMatthew.Ahrens@Sun.COM ASSERT(!BP_IS_HOLE(bp)); 356*12470SMatthew.Ahrens@Sun.COM 357*12470SMatthew.Ahrens@Sun.COM /* We never need the fill count. */ 358*12470SMatthew.Ahrens@Sun.COM stored_bp.blk_fill = 0; 359*12470SMatthew.Ahrens@Sun.COM 360*12470SMatthew.Ahrens@Sun.COM /* The bpobj will compress better if we can leave off the checksum */ 361*12470SMatthew.Ahrens@Sun.COM if (!BP_GET_DEDUP(bp)) 362*12470SMatthew.Ahrens@Sun.COM bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum)); 363*12470SMatthew.Ahrens@Sun.COM 364*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpo->bpo_lock); 365*12470SMatthew.Ahrens@Sun.COM 366*12470SMatthew.Ahrens@Sun.COM offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp); 367*12470SMatthew.Ahrens@Sun.COM blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb); 368*12470SMatthew.Ahrens@Sun.COM 369*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_cached_dbuf == NULL || 370*12470SMatthew.Ahrens@Sun.COM offset < bpo->bpo_cached_dbuf->db_offset || 371*12470SMatthew.Ahrens@Sun.COM offset >= bpo->bpo_cached_dbuf->db_offset + 372*12470SMatthew.Ahrens@Sun.COM bpo->bpo_cached_dbuf->db_size) { 373*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_cached_dbuf) 374*12470SMatthew.Ahrens@Sun.COM dmu_buf_rele(bpo->bpo_cached_dbuf, bpo); 375*12470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, 376*12470SMatthew.Ahrens@Sun.COM offset, bpo, &bpo->bpo_cached_dbuf, 0)); 377*12470SMatthew.Ahrens@Sun.COM } 378*12470SMatthew.Ahrens@Sun.COM 379*12470SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx); 380*12470SMatthew.Ahrens@Sun.COM bparray = bpo->bpo_cached_dbuf->db_data; 381*12470SMatthew.Ahrens@Sun.COM bparray[blkoff] = stored_bp; 382*12470SMatthew.Ahrens@Sun.COM 383*12470SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(bpo->bpo_dbuf, tx); 384*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_num_blkptrs++; 385*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_bytes += 386*12470SMatthew.Ahrens@Sun.COM bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp); 387*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_havecomp) { 388*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp); 389*12470SMatthew.Ahrens@Sun.COM bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp); 390*12470SMatthew.Ahrens@Sun.COM } 391*12470SMatthew.Ahrens@Sun.COM mutex_exit(&bpo->bpo_lock); 392*12470SMatthew.Ahrens@Sun.COM } 393*12470SMatthew.Ahrens@Sun.COM 394*12470SMatthew.Ahrens@Sun.COM struct space_range_arg { 395*12470SMatthew.Ahrens@Sun.COM spa_t *spa; 396*12470SMatthew.Ahrens@Sun.COM uint64_t mintxg; 397*12470SMatthew.Ahrens@Sun.COM uint64_t maxtxg; 398*12470SMatthew.Ahrens@Sun.COM uint64_t used; 399*12470SMatthew.Ahrens@Sun.COM uint64_t comp; 400*12470SMatthew.Ahrens@Sun.COM uint64_t uncomp; 401*12470SMatthew.Ahrens@Sun.COM }; 402*12470SMatthew.Ahrens@Sun.COM 403*12470SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 404*12470SMatthew.Ahrens@Sun.COM static int 405*12470SMatthew.Ahrens@Sun.COM space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 406*12470SMatthew.Ahrens@Sun.COM { 407*12470SMatthew.Ahrens@Sun.COM struct space_range_arg *sra = arg; 408*12470SMatthew.Ahrens@Sun.COM 409*12470SMatthew.Ahrens@Sun.COM if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) { 410*12470SMatthew.Ahrens@Sun.COM sra->used += bp_get_dsize_sync(sra->spa, bp); 411*12470SMatthew.Ahrens@Sun.COM sra->comp += BP_GET_PSIZE(bp); 412*12470SMatthew.Ahrens@Sun.COM sra->uncomp += BP_GET_UCSIZE(bp); 413*12470SMatthew.Ahrens@Sun.COM } 414*12470SMatthew.Ahrens@Sun.COM return (0); 415*12470SMatthew.Ahrens@Sun.COM } 416*12470SMatthew.Ahrens@Sun.COM 417*12470SMatthew.Ahrens@Sun.COM int 418*12470SMatthew.Ahrens@Sun.COM bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 419*12470SMatthew.Ahrens@Sun.COM { 420*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpo->bpo_lock); 421*12470SMatthew.Ahrens@Sun.COM 422*12470SMatthew.Ahrens@Sun.COM *usedp = bpo->bpo_phys->bpo_bytes; 423*12470SMatthew.Ahrens@Sun.COM if (bpo->bpo_havecomp) { 424*12470SMatthew.Ahrens@Sun.COM *compp = bpo->bpo_phys->bpo_comp; 425*12470SMatthew.Ahrens@Sun.COM *uncompp = bpo->bpo_phys->bpo_uncomp; 426*12470SMatthew.Ahrens@Sun.COM mutex_exit(&bpo->bpo_lock); 427*12470SMatthew.Ahrens@Sun.COM return (0); 428*12470SMatthew.Ahrens@Sun.COM } else { 429*12470SMatthew.Ahrens@Sun.COM mutex_exit(&bpo->bpo_lock); 430*12470SMatthew.Ahrens@Sun.COM return (bpobj_space_range(bpo, 0, UINT64_MAX, 431*12470SMatthew.Ahrens@Sun.COM usedp, compp, uncompp)); 432*12470SMatthew.Ahrens@Sun.COM } 433*12470SMatthew.Ahrens@Sun.COM } 434*12470SMatthew.Ahrens@Sun.COM 435*12470SMatthew.Ahrens@Sun.COM /* 436*12470SMatthew.Ahrens@Sun.COM * Return the amount of space in the bpobj which is: 437*12470SMatthew.Ahrens@Sun.COM * mintxg < blk_birth <= maxtxg 438*12470SMatthew.Ahrens@Sun.COM */ 439*12470SMatthew.Ahrens@Sun.COM int 440*12470SMatthew.Ahrens@Sun.COM bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg, 441*12470SMatthew.Ahrens@Sun.COM uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 442*12470SMatthew.Ahrens@Sun.COM { 443*12470SMatthew.Ahrens@Sun.COM struct space_range_arg sra = { 0 }; 444*12470SMatthew.Ahrens@Sun.COM int err; 445*12470SMatthew.Ahrens@Sun.COM 446*12470SMatthew.Ahrens@Sun.COM /* 447*12470SMatthew.Ahrens@Sun.COM * As an optimization, if they want the whole txg range, just 448*12470SMatthew.Ahrens@Sun.COM * get bpo_bytes rather than iterating over the bps. 449*12470SMatthew.Ahrens@Sun.COM */ 450*12470SMatthew.Ahrens@Sun.COM if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp) 451*12470SMatthew.Ahrens@Sun.COM return (bpobj_space(bpo, usedp, compp, uncompp)); 452*12470SMatthew.Ahrens@Sun.COM 453*12470SMatthew.Ahrens@Sun.COM sra.spa = dmu_objset_spa(bpo->bpo_os); 454*12470SMatthew.Ahrens@Sun.COM sra.mintxg = mintxg; 455*12470SMatthew.Ahrens@Sun.COM sra.maxtxg = maxtxg; 456*12470SMatthew.Ahrens@Sun.COM 457*12470SMatthew.Ahrens@Sun.COM err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL); 458*12470SMatthew.Ahrens@Sun.COM *usedp = sra.used; 459*12470SMatthew.Ahrens@Sun.COM *compp = sra.comp; 460*12470SMatthew.Ahrens@Sun.COM *uncompp = sra.uncomp; 461*12470SMatthew.Ahrens@Sun.COM return (err); 462*12470SMatthew.Ahrens@Sun.COM } 463