xref: /illumos-gate/usr/src/uts/common/fs/zfs/bpobj.c (revision f17457368189aa911f774c38c1f21875a568bdca)
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*f1745736SMatthew Ahrens  * Copyright (c) 2012 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>
30*f1745736SMatthew Ahrens #include <sys/zfeature.h>
31*f1745736SMatthew Ahrens #include <sys/zap.h>
32*f1745736SMatthew Ahrens 
33*f1745736SMatthew Ahrens /*
34*f1745736SMatthew Ahrens  * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
35*f1745736SMatthew Ahrens  */
36*f1745736SMatthew Ahrens uint64_t
37*f1745736SMatthew Ahrens bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
38*f1745736SMatthew Ahrens {
39*f1745736SMatthew Ahrens 	zfeature_info_t *empty_bpobj_feat =
40*f1745736SMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ];
41*f1745736SMatthew Ahrens 	spa_t *spa = dmu_objset_spa(os);
42*f1745736SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
43*f1745736SMatthew Ahrens 
44*f1745736SMatthew Ahrens 	if (spa_feature_is_enabled(spa, empty_bpobj_feat)) {
45*f1745736SMatthew Ahrens 		if (!spa_feature_is_active(spa, empty_bpobj_feat)) {
46*f1745736SMatthew Ahrens 			ASSERT3U(dp->dp_empty_bpobj, ==, 0);
47*f1745736SMatthew Ahrens 			dp->dp_empty_bpobj =
48*f1745736SMatthew Ahrens 			    bpobj_alloc(os, SPA_MAXBLOCKSIZE, tx);
49*f1745736SMatthew Ahrens 			VERIFY(zap_add(os,
50*f1745736SMatthew Ahrens 			    DMU_POOL_DIRECTORY_OBJECT,
51*f1745736SMatthew Ahrens 			    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
52*f1745736SMatthew Ahrens 			    &dp->dp_empty_bpobj, tx) == 0);
53*f1745736SMatthew Ahrens 		}
54*f1745736SMatthew Ahrens 		spa_feature_incr(spa, empty_bpobj_feat, tx);
55*f1745736SMatthew Ahrens 		ASSERT(dp->dp_empty_bpobj != 0);
56*f1745736SMatthew Ahrens 		return (dp->dp_empty_bpobj);
57*f1745736SMatthew Ahrens 	} else {
58*f1745736SMatthew Ahrens 		return (bpobj_alloc(os, blocksize, tx));
59*f1745736SMatthew Ahrens 	}
60*f1745736SMatthew Ahrens }
61*f1745736SMatthew Ahrens 
62*f1745736SMatthew Ahrens void
63*f1745736SMatthew Ahrens bpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
64*f1745736SMatthew Ahrens {
65*f1745736SMatthew Ahrens 	zfeature_info_t *empty_bpobj_feat =
66*f1745736SMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ];
67*f1745736SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
68*f1745736SMatthew Ahrens 
69*f1745736SMatthew Ahrens 	spa_feature_decr(dmu_objset_spa(os), empty_bpobj_feat, tx);
70*f1745736SMatthew Ahrens 	if (!spa_feature_is_active(dmu_objset_spa(os), empty_bpobj_feat)) {
71*f1745736SMatthew Ahrens 		VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
72*f1745736SMatthew Ahrens 		    DMU_POOL_DIRECTORY_OBJECT,
73*f1745736SMatthew Ahrens 		    DMU_POOL_EMPTY_BPOBJ, tx));
74*f1745736SMatthew Ahrens 		VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
75*f1745736SMatthew Ahrens 		dp->dp_empty_bpobj = 0;
76*f1745736SMatthew Ahrens 	}
77*f1745736SMatthew Ahrens }
78cde58dbcSMatthew Ahrens 
79cde58dbcSMatthew Ahrens uint64_t
80cde58dbcSMatthew Ahrens bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
81cde58dbcSMatthew Ahrens {
82cde58dbcSMatthew Ahrens 	int size;
83cde58dbcSMatthew Ahrens 
84cde58dbcSMatthew Ahrens 	if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
85cde58dbcSMatthew Ahrens 		size = BPOBJ_SIZE_V0;
86cde58dbcSMatthew Ahrens 	else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
87cde58dbcSMatthew Ahrens 		size = BPOBJ_SIZE_V1;
88cde58dbcSMatthew Ahrens 	else
89cde58dbcSMatthew Ahrens 		size = sizeof (bpobj_phys_t);
90cde58dbcSMatthew Ahrens 
91cde58dbcSMatthew Ahrens 	return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
92cde58dbcSMatthew Ahrens 	    DMU_OT_BPOBJ_HDR, size, tx));
93cde58dbcSMatthew Ahrens }
94cde58dbcSMatthew Ahrens 
95cde58dbcSMatthew Ahrens void
96cde58dbcSMatthew Ahrens bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
97cde58dbcSMatthew Ahrens {
98cde58dbcSMatthew Ahrens 	int64_t i;
99cde58dbcSMatthew Ahrens 	bpobj_t bpo;
100cde58dbcSMatthew Ahrens 	dmu_object_info_t doi;
101cde58dbcSMatthew Ahrens 	int epb;
102cde58dbcSMatthew Ahrens 	dmu_buf_t *dbuf = NULL;
103cde58dbcSMatthew Ahrens 
104*f1745736SMatthew Ahrens 	ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
105b420f3adSRichard Lowe 	VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
106cde58dbcSMatthew Ahrens 
107cde58dbcSMatthew Ahrens 	mutex_enter(&bpo.bpo_lock);
108cde58dbcSMatthew Ahrens 
109cde58dbcSMatthew Ahrens 	if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
110cde58dbcSMatthew Ahrens 		goto out;
111cde58dbcSMatthew Ahrens 
112b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
113cde58dbcSMatthew Ahrens 	epb = doi.doi_data_block_size / sizeof (uint64_t);
114cde58dbcSMatthew Ahrens 
115cde58dbcSMatthew Ahrens 	for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
116cde58dbcSMatthew Ahrens 		uint64_t *objarray;
117cde58dbcSMatthew Ahrens 		uint64_t offset, blkoff;
118cde58dbcSMatthew Ahrens 
119cde58dbcSMatthew Ahrens 		offset = i * sizeof (uint64_t);
120cde58dbcSMatthew Ahrens 		blkoff = P2PHASE(i, epb);
121cde58dbcSMatthew Ahrens 
122cde58dbcSMatthew Ahrens 		if (dbuf == NULL || dbuf->db_offset > offset) {
123cde58dbcSMatthew Ahrens 			if (dbuf)
124cde58dbcSMatthew Ahrens 				dmu_buf_rele(dbuf, FTAG);
125b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_buf_hold(os,
126cde58dbcSMatthew Ahrens 			    bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
127cde58dbcSMatthew Ahrens 		}
128cde58dbcSMatthew Ahrens 
129cde58dbcSMatthew Ahrens 		ASSERT3U(offset, >=, dbuf->db_offset);
130cde58dbcSMatthew Ahrens 		ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
131cde58dbcSMatthew Ahrens 
132cde58dbcSMatthew Ahrens 		objarray = dbuf->db_data;
133cde58dbcSMatthew Ahrens 		bpobj_free(os, objarray[blkoff], tx);
134cde58dbcSMatthew Ahrens 	}
135cde58dbcSMatthew Ahrens 	if (dbuf) {
136cde58dbcSMatthew Ahrens 		dmu_buf_rele(dbuf, FTAG);
137cde58dbcSMatthew Ahrens 		dbuf = NULL;
138cde58dbcSMatthew Ahrens 	}
139b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
140cde58dbcSMatthew Ahrens 
141cde58dbcSMatthew Ahrens out:
142cde58dbcSMatthew Ahrens 	mutex_exit(&bpo.bpo_lock);
143cde58dbcSMatthew Ahrens 	bpobj_close(&bpo);
144cde58dbcSMatthew Ahrens 
145b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
146cde58dbcSMatthew Ahrens }
147cde58dbcSMatthew Ahrens 
148cde58dbcSMatthew Ahrens int
149cde58dbcSMatthew Ahrens bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
150cde58dbcSMatthew Ahrens {
151cde58dbcSMatthew Ahrens 	dmu_object_info_t doi;
152cde58dbcSMatthew Ahrens 	int err;
153cde58dbcSMatthew Ahrens 
154cde58dbcSMatthew Ahrens 	err = dmu_object_info(os, object, &doi);
155cde58dbcSMatthew Ahrens 	if (err)
156cde58dbcSMatthew Ahrens 		return (err);
157cde58dbcSMatthew Ahrens 
158cde58dbcSMatthew Ahrens 	bzero(bpo, sizeof (*bpo));
159cde58dbcSMatthew Ahrens 	mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
160cde58dbcSMatthew Ahrens 
161cde58dbcSMatthew Ahrens 	ASSERT(bpo->bpo_dbuf == NULL);
162cde58dbcSMatthew Ahrens 	ASSERT(bpo->bpo_phys == NULL);
163cde58dbcSMatthew Ahrens 	ASSERT(object != 0);
164cde58dbcSMatthew Ahrens 	ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
165cde58dbcSMatthew Ahrens 	ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
166cde58dbcSMatthew Ahrens 
167837b568bSGeorge Wilson 	err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
168837b568bSGeorge Wilson 	if (err)
169837b568bSGeorge Wilson 		return (err);
170837b568bSGeorge Wilson 
171cde58dbcSMatthew Ahrens 	bpo->bpo_os = os;
172cde58dbcSMatthew Ahrens 	bpo->bpo_object = object;
173cde58dbcSMatthew Ahrens 	bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
174cde58dbcSMatthew Ahrens 	bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
175cde58dbcSMatthew Ahrens 	bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
176cde58dbcSMatthew Ahrens 	bpo->bpo_phys = bpo->bpo_dbuf->db_data;
177cde58dbcSMatthew Ahrens 	return (0);
178cde58dbcSMatthew Ahrens }
179cde58dbcSMatthew Ahrens 
180cde58dbcSMatthew Ahrens void
181cde58dbcSMatthew Ahrens bpobj_close(bpobj_t *bpo)
182cde58dbcSMatthew Ahrens {
183cde58dbcSMatthew Ahrens 	/* Lame workaround for closing a bpobj that was never opened. */
184cde58dbcSMatthew Ahrens 	if (bpo->bpo_object == 0)
185cde58dbcSMatthew Ahrens 		return;
186cde58dbcSMatthew Ahrens 
187cde58dbcSMatthew Ahrens 	dmu_buf_rele(bpo->bpo_dbuf, bpo);
188cde58dbcSMatthew Ahrens 	if (bpo->bpo_cached_dbuf != NULL)
189cde58dbcSMatthew Ahrens 		dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
190cde58dbcSMatthew Ahrens 	bpo->bpo_dbuf = NULL;
191cde58dbcSMatthew Ahrens 	bpo->bpo_phys = NULL;
192cde58dbcSMatthew Ahrens 	bpo->bpo_cached_dbuf = NULL;
193837b568bSGeorge Wilson 	bpo->bpo_object = 0;
194cde58dbcSMatthew Ahrens 
195cde58dbcSMatthew Ahrens 	mutex_destroy(&bpo->bpo_lock);
196cde58dbcSMatthew Ahrens }
197cde58dbcSMatthew Ahrens 
198cde58dbcSMatthew Ahrens static int
199cde58dbcSMatthew Ahrens bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
200cde58dbcSMatthew Ahrens     boolean_t free)
201cde58dbcSMatthew Ahrens {
202cde58dbcSMatthew Ahrens 	dmu_object_info_t doi;
203cde58dbcSMatthew Ahrens 	int epb;
204cde58dbcSMatthew Ahrens 	int64_t i;
205cde58dbcSMatthew Ahrens 	int err = 0;
206cde58dbcSMatthew Ahrens 	dmu_buf_t *dbuf = NULL;
207cde58dbcSMatthew Ahrens 
208cde58dbcSMatthew Ahrens 	mutex_enter(&bpo->bpo_lock);
209cde58dbcSMatthew Ahrens 
210cde58dbcSMatthew Ahrens 	if (free)
211cde58dbcSMatthew Ahrens 		dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
212cde58dbcSMatthew Ahrens 
213cde58dbcSMatthew Ahrens 	for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
214cde58dbcSMatthew Ahrens 		blkptr_t *bparray;
215cde58dbcSMatthew Ahrens 		blkptr_t *bp;
216cde58dbcSMatthew Ahrens 		uint64_t offset, blkoff;
217cde58dbcSMatthew Ahrens 
218cde58dbcSMatthew Ahrens 		offset = i * sizeof (blkptr_t);
219cde58dbcSMatthew Ahrens 		blkoff = P2PHASE(i, bpo->bpo_epb);
220cde58dbcSMatthew Ahrens 
221cde58dbcSMatthew Ahrens 		if (dbuf == NULL || dbuf->db_offset > offset) {
222cde58dbcSMatthew Ahrens 			if (dbuf)
223cde58dbcSMatthew Ahrens 				dmu_buf_rele(dbuf, FTAG);
224cde58dbcSMatthew Ahrens 			err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
225cde58dbcSMatthew Ahrens 			    FTAG, &dbuf, 0);
226cde58dbcSMatthew Ahrens 			if (err)
227cde58dbcSMatthew Ahrens 				break;
228cde58dbcSMatthew Ahrens 		}
229cde58dbcSMatthew Ahrens 
230cde58dbcSMatthew Ahrens 		ASSERT3U(offset, >=, dbuf->db_offset);
231cde58dbcSMatthew Ahrens 		ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
232cde58dbcSMatthew Ahrens 
233cde58dbcSMatthew Ahrens 		bparray = dbuf->db_data;
234cde58dbcSMatthew Ahrens 		bp = &bparray[blkoff];
235cde58dbcSMatthew Ahrens 		err = func(arg, bp, tx);
236cde58dbcSMatthew Ahrens 		if (err)
237cde58dbcSMatthew Ahrens 			break;
238cde58dbcSMatthew Ahrens 		if (free) {
239cde58dbcSMatthew Ahrens 			bpo->bpo_phys->bpo_bytes -=
240cde58dbcSMatthew Ahrens 			    bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
241cde58dbcSMatthew Ahrens 			ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
242cde58dbcSMatthew Ahrens 			if (bpo->bpo_havecomp) {
243cde58dbcSMatthew Ahrens 				bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
244cde58dbcSMatthew Ahrens 				bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
245cde58dbcSMatthew Ahrens 			}
246cde58dbcSMatthew Ahrens 			bpo->bpo_phys->bpo_num_blkptrs--;
247cde58dbcSMatthew Ahrens 			ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
248cde58dbcSMatthew Ahrens 		}
249cde58dbcSMatthew Ahrens 	}
250cde58dbcSMatthew Ahrens 	if (dbuf) {
251cde58dbcSMatthew Ahrens 		dmu_buf_rele(dbuf, FTAG);
252cde58dbcSMatthew Ahrens 		dbuf = NULL;
253cde58dbcSMatthew Ahrens 	}
254cde58dbcSMatthew Ahrens 	if (free) {
255cde58dbcSMatthew Ahrens 		i++;
256b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
257cde58dbcSMatthew Ahrens 		    i * sizeof (blkptr_t), -1ULL, tx));
258cde58dbcSMatthew Ahrens 	}
259cde58dbcSMatthew Ahrens 	if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
260cde58dbcSMatthew Ahrens 		goto out;
261cde58dbcSMatthew Ahrens 
262cde58dbcSMatthew Ahrens 	ASSERT(bpo->bpo_havecomp);
263cde58dbcSMatthew Ahrens 	err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
264a0e4757dSLin Ling 	if (err) {
265a0e4757dSLin Ling 		mutex_exit(&bpo->bpo_lock);
266cde58dbcSMatthew Ahrens 		return (err);
267a0e4757dSLin Ling 	}
268cde58dbcSMatthew Ahrens 	epb = doi.doi_data_block_size / sizeof (uint64_t);
269cde58dbcSMatthew Ahrens 
270cde58dbcSMatthew Ahrens 	for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
271cde58dbcSMatthew Ahrens 		uint64_t *objarray;
272cde58dbcSMatthew Ahrens 		uint64_t offset, blkoff;
273cde58dbcSMatthew Ahrens 		bpobj_t sublist;
274cde58dbcSMatthew Ahrens 		uint64_t used_before, comp_before, uncomp_before;
275cde58dbcSMatthew Ahrens 		uint64_t used_after, comp_after, uncomp_after;
276cde58dbcSMatthew Ahrens 
277cde58dbcSMatthew Ahrens 		offset = i * sizeof (uint64_t);
278cde58dbcSMatthew Ahrens 		blkoff = P2PHASE(i, epb);
279cde58dbcSMatthew Ahrens 
280cde58dbcSMatthew Ahrens 		if (dbuf == NULL || dbuf->db_offset > offset) {
281cde58dbcSMatthew Ahrens 			if (dbuf)
282cde58dbcSMatthew Ahrens 				dmu_buf_rele(dbuf, FTAG);
283cde58dbcSMatthew Ahrens 			err = dmu_buf_hold(bpo->bpo_os,
284cde58dbcSMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
285cde58dbcSMatthew Ahrens 			if (err)
286cde58dbcSMatthew Ahrens 				break;
287cde58dbcSMatthew Ahrens 		}
288cde58dbcSMatthew Ahrens 
289cde58dbcSMatthew Ahrens 		ASSERT3U(offset, >=, dbuf->db_offset);
290cde58dbcSMatthew Ahrens 		ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
291cde58dbcSMatthew Ahrens 
292cde58dbcSMatthew Ahrens 		objarray = dbuf->db_data;
293cde58dbcSMatthew Ahrens 		err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
294cde58dbcSMatthew Ahrens 		if (err)
295cde58dbcSMatthew Ahrens 			break;
296cde58dbcSMatthew Ahrens 		if (free) {
297cde58dbcSMatthew Ahrens 			err = bpobj_space(&sublist,
298cde58dbcSMatthew Ahrens 			    &used_before, &comp_before, &uncomp_before);
299cde58dbcSMatthew Ahrens 			if (err)
300cde58dbcSMatthew Ahrens 				break;
301cde58dbcSMatthew Ahrens 		}
302cde58dbcSMatthew Ahrens 		err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
303cde58dbcSMatthew Ahrens 		if (free) {
304b420f3adSRichard Lowe 			VERIFY3U(0, ==, bpobj_space(&sublist,
305cde58dbcSMatthew Ahrens 			    &used_after, &comp_after, &uncomp_after));
306cde58dbcSMatthew Ahrens 			bpo->bpo_phys->bpo_bytes -= used_before - used_after;
307cde58dbcSMatthew Ahrens 			ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
3084bc4cb79SMatthew Ahrens 			bpo->bpo_phys->bpo_comp -= comp_before - comp_after;
309cde58dbcSMatthew Ahrens 			bpo->bpo_phys->bpo_uncomp -=
310cde58dbcSMatthew Ahrens 			    uncomp_before - uncomp_after;
311cde58dbcSMatthew Ahrens 		}
312cde58dbcSMatthew Ahrens 
313cde58dbcSMatthew Ahrens 		bpobj_close(&sublist);
314cde58dbcSMatthew Ahrens 		if (err)
315cde58dbcSMatthew Ahrens 			break;
316cde58dbcSMatthew Ahrens 		if (free) {
317cde58dbcSMatthew Ahrens 			err = dmu_object_free(bpo->bpo_os,
318cde58dbcSMatthew Ahrens 			    objarray[blkoff], tx);
319cde58dbcSMatthew Ahrens 			if (err)
320cde58dbcSMatthew Ahrens 				break;
321cde58dbcSMatthew Ahrens 			bpo->bpo_phys->bpo_num_subobjs--;
322cde58dbcSMatthew Ahrens 			ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
323cde58dbcSMatthew Ahrens 		}
324cde58dbcSMatthew Ahrens 	}
325cde58dbcSMatthew Ahrens 	if (dbuf) {
326cde58dbcSMatthew Ahrens 		dmu_buf_rele(dbuf, FTAG);
327cde58dbcSMatthew Ahrens 		dbuf = NULL;
328cde58dbcSMatthew Ahrens 	}
329cde58dbcSMatthew Ahrens 	if (free) {
330b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
331cde58dbcSMatthew Ahrens 		    bpo->bpo_phys->bpo_subobjs,
332cde58dbcSMatthew Ahrens 		    (i + 1) * sizeof (uint64_t), -1ULL, tx));
333cde58dbcSMatthew Ahrens 	}
334cde58dbcSMatthew Ahrens 
335cde58dbcSMatthew Ahrens out:
336cde58dbcSMatthew Ahrens 	/* If there are no entries, there should be no bytes. */
337cde58dbcSMatthew Ahrens 	ASSERT(bpo->bpo_phys->bpo_num_blkptrs > 0 ||
338cde58dbcSMatthew Ahrens 	    (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs > 0) ||
339cde58dbcSMatthew Ahrens 	    bpo->bpo_phys->bpo_bytes == 0);
340cde58dbcSMatthew Ahrens 
341cde58dbcSMatthew Ahrens 	mutex_exit(&bpo->bpo_lock);
342cde58dbcSMatthew Ahrens 	return (err);
343cde58dbcSMatthew Ahrens }
344cde58dbcSMatthew Ahrens 
345cde58dbcSMatthew Ahrens /*
346cde58dbcSMatthew Ahrens  * Iterate and remove the entries.  If func returns nonzero, iteration
347cde58dbcSMatthew Ahrens  * will stop and that entry will not be removed.
348cde58dbcSMatthew Ahrens  */
349cde58dbcSMatthew Ahrens int
350cde58dbcSMatthew Ahrens bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
351cde58dbcSMatthew Ahrens {
352cde58dbcSMatthew Ahrens 	return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
353cde58dbcSMatthew Ahrens }
354cde58dbcSMatthew Ahrens 
355cde58dbcSMatthew Ahrens /*
356cde58dbcSMatthew Ahrens  * Iterate the entries.  If func returns nonzero, iteration will stop.
357cde58dbcSMatthew Ahrens  */
358cde58dbcSMatthew Ahrens int
359cde58dbcSMatthew Ahrens bpobj_iterate_nofree(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_FALSE));
362cde58dbcSMatthew Ahrens }
363cde58dbcSMatthew Ahrens 
364cde58dbcSMatthew Ahrens void
365cde58dbcSMatthew Ahrens bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
366cde58dbcSMatthew Ahrens {
367cde58dbcSMatthew Ahrens 	bpobj_t subbpo;
3684bc4cb79SMatthew Ahrens 	uint64_t used, comp, uncomp, subsubobjs;
369cde58dbcSMatthew Ahrens 
370cde58dbcSMatthew Ahrens 	ASSERT(bpo->bpo_havesubobj);
371cde58dbcSMatthew Ahrens 	ASSERT(bpo->bpo_havecomp);
372*f1745736SMatthew Ahrens 	ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
373*f1745736SMatthew Ahrens 
374*f1745736SMatthew Ahrens 	if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
375*f1745736SMatthew Ahrens 		bpobj_decr_empty(bpo->bpo_os, tx);
376*f1745736SMatthew Ahrens 		return;
377*f1745736SMatthew Ahrens 	}
378cde58dbcSMatthew Ahrens 
379b420f3adSRichard Lowe 	VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
380b420f3adSRichard Lowe 	VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
381cde58dbcSMatthew Ahrens 
382cde58dbcSMatthew Ahrens 	if (used == 0) {
383cde58dbcSMatthew Ahrens 		/* No point in having an empty subobj. */
3844bc4cb79SMatthew Ahrens 		bpobj_close(&subbpo);
385cde58dbcSMatthew Ahrens 		bpobj_free(bpo->bpo_os, subobj, tx);
386cde58dbcSMatthew Ahrens 		return;
387cde58dbcSMatthew Ahrens 	}
388cde58dbcSMatthew Ahrens 
389cde58dbcSMatthew Ahrens 	dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
390cde58dbcSMatthew Ahrens 	if (bpo->bpo_phys->bpo_subobjs == 0) {
391cde58dbcSMatthew Ahrens 		bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
392cde58dbcSMatthew Ahrens 		    DMU_OT_BPOBJ_SUBOBJ, SPA_MAXBLOCKSIZE, DMU_OT_NONE, 0, tx);
393cde58dbcSMatthew Ahrens 	}
394cde58dbcSMatthew Ahrens 
395cde58dbcSMatthew Ahrens 	mutex_enter(&bpo->bpo_lock);
396cde58dbcSMatthew Ahrens 	dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
397cde58dbcSMatthew Ahrens 	    bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
398cde58dbcSMatthew Ahrens 	    sizeof (subobj), &subobj, tx);
399cde58dbcSMatthew Ahrens 	bpo->bpo_phys->bpo_num_subobjs++;
4004bc4cb79SMatthew Ahrens 
4014bc4cb79SMatthew Ahrens 	/*
4024bc4cb79SMatthew Ahrens 	 * If subobj has only one block of subobjs, then move subobj's
4034bc4cb79SMatthew Ahrens 	 * subobjs to bpo's subobj list directly.  This reduces
4044bc4cb79SMatthew Ahrens 	 * recursion in bpobj_iterate due to nested subobjs.
4054bc4cb79SMatthew Ahrens 	 */
4064bc4cb79SMatthew Ahrens 	subsubobjs = subbpo.bpo_phys->bpo_subobjs;
4074bc4cb79SMatthew Ahrens 	if (subsubobjs != 0) {
4084bc4cb79SMatthew Ahrens 		dmu_object_info_t doi;
4094bc4cb79SMatthew Ahrens 
410b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
4114bc4cb79SMatthew Ahrens 		if (doi.doi_max_offset == doi.doi_data_block_size) {
4124bc4cb79SMatthew Ahrens 			dmu_buf_t *subdb;
4134bc4cb79SMatthew Ahrens 			uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
4144bc4cb79SMatthew Ahrens 
415b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
4164bc4cb79SMatthew Ahrens 			    0, FTAG, &subdb, 0));
4174bc4cb79SMatthew Ahrens 			dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
4184bc4cb79SMatthew Ahrens 			    bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
4194bc4cb79SMatthew Ahrens 			    numsubsub * sizeof (subobj), subdb->db_data, tx);
4204bc4cb79SMatthew Ahrens 			dmu_buf_rele(subdb, FTAG);
4214bc4cb79SMatthew Ahrens 			bpo->bpo_phys->bpo_num_subobjs += numsubsub;
4224bc4cb79SMatthew Ahrens 
4234bc4cb79SMatthew Ahrens 			dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
4244bc4cb79SMatthew Ahrens 			subbpo.bpo_phys->bpo_subobjs = 0;
425b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
4264bc4cb79SMatthew Ahrens 			    subsubobjs, tx));
4274bc4cb79SMatthew Ahrens 		}
4284bc4cb79SMatthew Ahrens 	}
429cde58dbcSMatthew Ahrens 	bpo->bpo_phys->bpo_bytes += used;
430cde58dbcSMatthew Ahrens 	bpo->bpo_phys->bpo_comp += comp;
431cde58dbcSMatthew Ahrens 	bpo->bpo_phys->bpo_uncomp += uncomp;
432cde58dbcSMatthew Ahrens 	mutex_exit(&bpo->bpo_lock);
4334bc4cb79SMatthew Ahrens 
4344bc4cb79SMatthew Ahrens 	bpobj_close(&subbpo);
435cde58dbcSMatthew Ahrens }
436cde58dbcSMatthew Ahrens 
437cde58dbcSMatthew Ahrens void
438cde58dbcSMatthew Ahrens bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
439cde58dbcSMatthew Ahrens {
440cde58dbcSMatthew Ahrens 	blkptr_t stored_bp = *bp;
441cde58dbcSMatthew Ahrens 	uint64_t offset;
442cde58dbcSMatthew Ahrens 	int blkoff;
443cde58dbcSMatthew Ahrens 	blkptr_t *bparray;
444cde58dbcSMatthew Ahrens 
445cde58dbcSMatthew Ahrens 	ASSERT(!BP_IS_HOLE(bp));
446*f1745736SMatthew Ahrens 	ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
447cde58dbcSMatthew Ahrens 
448cde58dbcSMatthew Ahrens 	/* We never need the fill count. */
449cde58dbcSMatthew Ahrens 	stored_bp.blk_fill = 0;
450cde58dbcSMatthew Ahrens 
451cde58dbcSMatthew Ahrens 	/* The bpobj will compress better if we can leave off the checksum */
452cde58dbcSMatthew Ahrens 	if (!BP_GET_DEDUP(bp))
453cde58dbcSMatthew Ahrens 		bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
454cde58dbcSMatthew Ahrens 
455cde58dbcSMatthew Ahrens 	mutex_enter(&bpo->bpo_lock);
456cde58dbcSMatthew Ahrens 
457cde58dbcSMatthew Ahrens 	offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
458cde58dbcSMatthew Ahrens 	blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
459cde58dbcSMatthew Ahrens 
460cde58dbcSMatthew Ahrens 	if (bpo->bpo_cached_dbuf == NULL ||
461cde58dbcSMatthew Ahrens 	    offset < bpo->bpo_cached_dbuf->db_offset ||
462cde58dbcSMatthew Ahrens 	    offset >= bpo->bpo_cached_dbuf->db_offset +
463cde58dbcSMatthew Ahrens 	    bpo->bpo_cached_dbuf->db_size) {
464cde58dbcSMatthew Ahrens 		if (bpo->bpo_cached_dbuf)
465cde58dbcSMatthew Ahrens 			dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
466b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
467cde58dbcSMatthew Ahrens 		    offset, bpo, &bpo->bpo_cached_dbuf, 0));
468cde58dbcSMatthew Ahrens 	}
469cde58dbcSMatthew Ahrens 
470cde58dbcSMatthew Ahrens 	dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
471cde58dbcSMatthew Ahrens 	bparray = bpo->bpo_cached_dbuf->db_data;
472cde58dbcSMatthew Ahrens 	bparray[blkoff] = stored_bp;
473cde58dbcSMatthew Ahrens 
474cde58dbcSMatthew Ahrens 	dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
475cde58dbcSMatthew Ahrens 	bpo->bpo_phys->bpo_num_blkptrs++;
476cde58dbcSMatthew Ahrens 	bpo->bpo_phys->bpo_bytes +=
477cde58dbcSMatthew Ahrens 	    bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
478cde58dbcSMatthew Ahrens 	if (bpo->bpo_havecomp) {
479cde58dbcSMatthew Ahrens 		bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
480cde58dbcSMatthew Ahrens 		bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
481cde58dbcSMatthew Ahrens 	}
482cde58dbcSMatthew Ahrens 	mutex_exit(&bpo->bpo_lock);
483cde58dbcSMatthew Ahrens }
484cde58dbcSMatthew Ahrens 
485cde58dbcSMatthew Ahrens struct space_range_arg {
486cde58dbcSMatthew Ahrens 	spa_t *spa;
487cde58dbcSMatthew Ahrens 	uint64_t mintxg;
488cde58dbcSMatthew Ahrens 	uint64_t maxtxg;
489cde58dbcSMatthew Ahrens 	uint64_t used;
490cde58dbcSMatthew Ahrens 	uint64_t comp;
491cde58dbcSMatthew Ahrens 	uint64_t uncomp;
492cde58dbcSMatthew Ahrens };
493cde58dbcSMatthew Ahrens 
494cde58dbcSMatthew Ahrens /* ARGSUSED */
495cde58dbcSMatthew Ahrens static int
496cde58dbcSMatthew Ahrens space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
497cde58dbcSMatthew Ahrens {
498cde58dbcSMatthew Ahrens 	struct space_range_arg *sra = arg;
499cde58dbcSMatthew Ahrens 
500cde58dbcSMatthew Ahrens 	if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
50119b94df9SMatthew Ahrens 		if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
50219b94df9SMatthew Ahrens 			sra->used += bp_get_dsize_sync(sra->spa, bp);
50319b94df9SMatthew Ahrens 		else
50419b94df9SMatthew Ahrens 			sra->used += bp_get_dsize(sra->spa, bp);
505cde58dbcSMatthew Ahrens 		sra->comp += BP_GET_PSIZE(bp);
506cde58dbcSMatthew Ahrens 		sra->uncomp += BP_GET_UCSIZE(bp);
507cde58dbcSMatthew Ahrens 	}
508cde58dbcSMatthew Ahrens 	return (0);
509cde58dbcSMatthew Ahrens }
510cde58dbcSMatthew Ahrens 
511cde58dbcSMatthew Ahrens int
512cde58dbcSMatthew Ahrens bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
513cde58dbcSMatthew Ahrens {
514cde58dbcSMatthew Ahrens 	mutex_enter(&bpo->bpo_lock);
515cde58dbcSMatthew Ahrens 
516cde58dbcSMatthew Ahrens 	*usedp = bpo->bpo_phys->bpo_bytes;
517cde58dbcSMatthew Ahrens 	if (bpo->bpo_havecomp) {
518cde58dbcSMatthew Ahrens 		*compp = bpo->bpo_phys->bpo_comp;
519cde58dbcSMatthew Ahrens 		*uncompp = bpo->bpo_phys->bpo_uncomp;
520cde58dbcSMatthew Ahrens 		mutex_exit(&bpo->bpo_lock);
521cde58dbcSMatthew Ahrens 		return (0);
522cde58dbcSMatthew Ahrens 	} else {
523cde58dbcSMatthew Ahrens 		mutex_exit(&bpo->bpo_lock);
524cde58dbcSMatthew Ahrens 		return (bpobj_space_range(bpo, 0, UINT64_MAX,
525cde58dbcSMatthew Ahrens 		    usedp, compp, uncompp));
526cde58dbcSMatthew Ahrens 	}
527cde58dbcSMatthew Ahrens }
528cde58dbcSMatthew Ahrens 
529cde58dbcSMatthew Ahrens /*
530cde58dbcSMatthew Ahrens  * Return the amount of space in the bpobj which is:
531cde58dbcSMatthew Ahrens  * mintxg < blk_birth <= maxtxg
532cde58dbcSMatthew Ahrens  */
533cde58dbcSMatthew Ahrens int
534cde58dbcSMatthew Ahrens bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
535cde58dbcSMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
536cde58dbcSMatthew Ahrens {
537cde58dbcSMatthew Ahrens 	struct space_range_arg sra = { 0 };
538cde58dbcSMatthew Ahrens 	int err;
539cde58dbcSMatthew Ahrens 
540cde58dbcSMatthew Ahrens 	/*
541cde58dbcSMatthew Ahrens 	 * As an optimization, if they want the whole txg range, just
542cde58dbcSMatthew Ahrens 	 * get bpo_bytes rather than iterating over the bps.
543cde58dbcSMatthew Ahrens 	 */
544cde58dbcSMatthew Ahrens 	if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
545cde58dbcSMatthew Ahrens 		return (bpobj_space(bpo, usedp, compp, uncompp));
546cde58dbcSMatthew Ahrens 
547cde58dbcSMatthew Ahrens 	sra.spa = dmu_objset_spa(bpo->bpo_os);
548cde58dbcSMatthew Ahrens 	sra.mintxg = mintxg;
549cde58dbcSMatthew Ahrens 	sra.maxtxg = maxtxg;
550cde58dbcSMatthew Ahrens 
551cde58dbcSMatthew Ahrens 	err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
552cde58dbcSMatthew Ahrens 	*usedp = sra.used;
553cde58dbcSMatthew Ahrens 	*compp = sra.comp;
554cde58dbcSMatthew Ahrens 	*uncompp = sra.uncomp;
555cde58dbcSMatthew Ahrens 	return (err);
556cde58dbcSMatthew Ahrens }
557