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