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