1cde58dbMatthew Ahrens/*
2cde58dbMatthew Ahrens * CDDL HEADER START
3cde58dbMatthew Ahrens *
4cde58dbMatthew Ahrens * The contents of this file are subject to the terms of the
5cde58dbMatthew Ahrens * Common Development and Distribution License (the "License").
6cde58dbMatthew Ahrens * You may not use this file except in compliance with the License.
7cde58dbMatthew Ahrens *
8cde58dbMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9cde58dbMatthew Ahrens * or http://www.opensolaris.org/os/licensing.
10cde58dbMatthew Ahrens * See the License for the specific language governing permissions
11cde58dbMatthew Ahrens * and limitations under the License.
12cde58dbMatthew Ahrens *
13cde58dbMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
14cde58dbMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15cde58dbMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
16cde58dbMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17cde58dbMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18cde58dbMatthew Ahrens *
19cde58dbMatthew Ahrens * CDDL HEADER END
20cde58dbMatthew Ahrens */
21cde58dbMatthew Ahrens/*
22cde58dbMatthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23a3905a4Serapheim Dimitropoulos * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
251702cceAlek Pinchuk * Copyright (c) 2017 Datto Inc.
26cde58dbMatthew Ahrens */
27cde58dbMatthew Ahrens
28cde58dbMatthew Ahrens#include <sys/bpobj.h>
29cde58dbMatthew Ahrens#include <sys/zfs_context.h>
30cde58dbMatthew Ahrens#include <sys/refcount.h>
3119b94dfMatthew Ahrens#include <sys/dsl_pool.h>
32f174573Matthew Ahrens#include <sys/zfeature.h>
33f174573Matthew Ahrens#include <sys/zap.h>
34f174573Matthew Ahrens
35f174573Matthew Ahrens/*
36f174573Matthew Ahrens * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
37f174573Matthew Ahrens */
38f174573Matthew Ahrensuint64_t
39f174573Matthew Ahrensbpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
40f174573Matthew Ahrens{
41f174573Matthew Ahrens	spa_t *spa = dmu_objset_spa(os);
42f174573Matthew Ahrens	dsl_pool_t *dp = dmu_objset_pool(os);
43f174573Matthew Ahrens
442acef22Matthew Ahrens	if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
452acef22Matthew Ahrens		if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
46fb09f5aMadhav Suresh			ASSERT0(dp->dp_empty_bpobj);
47f174573Matthew Ahrens			dp->dp_empty_bpobj =
48b515258Matthew Ahrens			    bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx);
49f174573Matthew Ahrens			VERIFY(zap_add(os,
50f174573Matthew Ahrens			    DMU_POOL_DIRECTORY_OBJECT,
51f174573Matthew Ahrens			    DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
52f174573Matthew Ahrens			    &dp->dp_empty_bpobj, tx) == 0);
53f174573Matthew Ahrens		}
542acef22Matthew Ahrens		spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx);
55f174573Matthew Ahrens		ASSERT(dp->dp_empty_bpobj != 0);
56f174573Matthew Ahrens		return (dp->dp_empty_bpobj);
57f174573Matthew Ahrens	} else {
58f174573Matthew Ahrens		return (bpobj_alloc(os, blocksize, tx));
59f174573Matthew Ahrens	}
60f174573Matthew Ahrens}
61f174573Matthew Ahrens
62f174573Matthew Ahrensvoid
63f174573Matthew Ahrensbpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
64f174573Matthew Ahrens{
65f174573Matthew Ahrens	dsl_pool_t *dp = dmu_objset_pool(os);
66f174573Matthew Ahrens
672acef22Matthew Ahrens	spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx);
682acef22Matthew Ahrens	if (!spa_feature_is_active(dmu_objset_spa(os),
692acef22Matthew Ahrens	    SPA_FEATURE_EMPTY_BPOBJ)) {
70f174573Matthew Ahrens		VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
71f174573Matthew Ahrens		    DMU_POOL_DIRECTORY_OBJECT,
72f174573Matthew Ahrens		    DMU_POOL_EMPTY_BPOBJ, tx));
73f174573Matthew Ahrens		VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
74f174573Matthew Ahrens		dp->dp_empty_bpobj = 0;
75f174573Matthew Ahrens	}
76f174573Matthew Ahrens}
77cde58dbMatthew Ahrens
78cde58dbMatthew Ahrensuint64_t
79cde58dbMatthew Ahrensbpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
80cde58dbMatthew Ahrens{
81cde58dbMatthew Ahrens	int size;
82cde58dbMatthew Ahrens
83cde58dbMatthew Ahrens	if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
84cde58dbMatthew Ahrens		size = BPOBJ_SIZE_V0;
85cde58dbMatthew Ahrens	else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
86cde58dbMatthew Ahrens		size = BPOBJ_SIZE_V1;
87cde58dbMatthew Ahrens	else
88cde58dbMatthew Ahrens		size = sizeof (bpobj_phys_t);
89cde58dbMatthew Ahrens
90cde58dbMatthew Ahrens	return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
91cde58dbMatthew Ahrens	    DMU_OT_BPOBJ_HDR, size, tx));
92cde58dbMatthew Ahrens}
93cde58dbMatthew Ahrens
94cde58dbMatthew Ahrensvoid
95cde58dbMatthew Ahrensbpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
96cde58dbMatthew Ahrens{
97cde58dbMatthew Ahrens	int64_t i;
98cde58dbMatthew Ahrens	bpobj_t bpo;
99cde58dbMatthew Ahrens	dmu_object_info_t doi;
100cde58dbMatthew Ahrens	int epb;
101cde58dbMatthew Ahrens	dmu_buf_t *dbuf = NULL;
102cde58dbMatthew Ahrens
103f174573Matthew Ahrens	ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
104b420f3aRichard Lowe	VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
105cde58dbMatthew Ahrens
106cde58dbMatthew Ahrens	mutex_enter(&bpo.bpo_lock);
107cde58dbMatthew Ahrens
108cde58dbMatthew Ahrens	if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
109cde58dbMatthew Ahrens		goto out;
110cde58dbMatthew Ahrens
111b420f3aRichard Lowe	VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
112cde58dbMatthew Ahrens	epb = doi.doi_data_block_size / sizeof (uint64_t);
113cde58dbMatthew Ahrens
114cde58dbMatthew Ahrens	for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
115cde58dbMatthew Ahrens		uint64_t *objarray;
116cde58dbMatthew Ahrens		uint64_t offset, blkoff;
117cde58dbMatthew Ahrens
118cde58dbMatthew Ahrens		offset = i * sizeof (uint64_t);
119cde58dbMatthew Ahrens		blkoff = P2PHASE(i, epb);
120cde58dbMatthew Ahrens
121cde58dbMatthew Ahrens		if (dbuf == NULL || dbuf->db_offset > offset) {
122cde58dbMatthew Ahrens			if (dbuf)
123cde58dbMatthew Ahrens				dmu_buf_rele(dbuf, FTAG);
124b420f3aRichard Lowe			VERIFY3U(0, ==, dmu_buf_hold(os,
125cde58dbMatthew Ahrens			    bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
126cde58dbMatthew Ahrens		}
127cde58dbMatthew Ahrens
128cde58dbMatthew Ahrens		ASSERT3U(offset, >=, dbuf->db_offset);
129cde58dbMatthew Ahrens		ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
130cde58dbMatthew Ahrens
131cde58dbMatthew Ahrens		objarray = dbuf->db_data;
132cde58dbMatthew Ahrens		bpobj_free(os, objarray[blkoff], tx);
133cde58dbMatthew Ahrens	}
134cde58dbMatthew Ahrens	if (dbuf) {
135cde58dbMatthew Ahrens		dmu_buf_rele(dbuf, FTAG);
136cde58dbMatthew Ahrens		dbuf = NULL;
137cde58dbMatthew Ahrens	}
138b420f3aRichard Lowe	VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
139cde58dbMatthew Ahrens
140cde58dbMatthew Ahrensout:
141cde58dbMatthew Ahrens	mutex_exit(&bpo.bpo_lock);
142cde58dbMatthew Ahrens	bpobj_close(&bpo);
143cde58dbMatthew Ahrens
144b420f3aRichard Lowe	VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
145cde58dbMatthew Ahrens}
146cde58dbMatthew Ahrens
147cde58dbMatthew Ahrensint
148cde58dbMatthew Ahrensbpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
149cde58dbMatthew Ahrens{
150cde58dbMatthew Ahrens	dmu_object_info_t doi;
151cde58dbMatthew Ahrens	int err;
152cde58dbMatthew Ahrens
153cde58dbMatthew Ahrens	err = dmu_object_info(os, object, &doi);
154cde58dbMatthew Ahrens	if (err)
155cde58dbMatthew Ahrens		return (err);
156cde58dbMatthew Ahrens
157cde58dbMatthew Ahrens	bzero(bpo, sizeof (*bpo));
158cde58dbMatthew Ahrens	mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
159cde58dbMatthew Ahrens
160cde58dbMatthew Ahrens	ASSERT(bpo->bpo_dbuf == NULL);
161cde58dbMatthew Ahrens	ASSERT(bpo->bpo_phys == NULL);
162cde58dbMatthew Ahrens	ASSERT(object != 0);
163cde58dbMatthew Ahrens	ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
164cde58dbMatthew Ahrens	ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
165cde58dbMatthew Ahrens
166837b568George Wilson	err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
167837b568George Wilson	if (err)
168837b568George Wilson		return (err);
169837b568George Wilson
170cde58dbMatthew Ahrens	bpo->bpo_os = os;
171cde58dbMatthew Ahrens	bpo->bpo_object = object;
172cde58dbMatthew Ahrens	bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
173cde58dbMatthew Ahrens	bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
174cde58dbMatthew Ahrens	bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
175cde58dbMatthew Ahrens	bpo->bpo_phys = bpo->bpo_dbuf->db_data;
176cde58dbMatthew Ahrens	return (0);
177cde58dbMatthew Ahrens}
178cde58dbMatthew Ahrens
1795cabbc6Prashanth Sreenivasaboolean_t
1805cabbc6Prashanth Sreenivasabpobj_is_open(const bpobj_t *bpo)
1815cabbc6Prashanth Sreenivasa{
1825cabbc6Prashanth Sreenivasa	return (bpo->bpo_object != 0);
1835cabbc6Prashanth Sreenivasa}
1845cabbc6Prashanth Sreenivasa
185cde58dbMatthew Ahrensvoid
186cde58dbMatthew Ahrensbpobj_close(bpobj_t *bpo)
187cde58dbMatthew Ahrens{
188cde58dbMatthew Ahrens	/* Lame workaround for closing a bpobj that was never opened. */
189cde58dbMatthew Ahrens	if (bpo->bpo_object == 0)
190cde58dbMatthew Ahrens		return;
191cde58dbMatthew Ahrens
192cde58dbMatthew Ahrens	dmu_buf_rele(bpo->bpo_dbuf, bpo);
193cde58dbMatthew Ahrens	if (bpo->bpo_cached_dbuf != NULL)
194cde58dbMatthew Ahrens		dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
195cde58dbMatthew Ahrens	bpo->bpo_dbuf = NULL;
196cde58dbMatthew Ahrens	bpo->bpo_phys = NULL;
197cde58dbMatthew Ahrens	bpo->bpo_cached_dbuf = NULL;
198837b568George Wilson	bpo->bpo_object = 0;
199cde58dbMatthew Ahrens
200cde58dbMatthew Ahrens	mutex_destroy(&bpo->bpo_lock);
201cde58dbMatthew Ahrens}
202cde58dbMatthew Ahrens
2035cabbc6Prashanth Sreenivasaboolean_t
2045cabbc6Prashanth Sreenivasabpobj_is_empty(bpobj_t *bpo)
2055d7b4d4Matthew Ahrens{
2065cabbc6Prashanth Sreenivasa	return (bpo->bpo_phys->bpo_num_blkptrs == 0 &&
2075cabbc6Prashanth Sreenivasa	    (!bpo->bpo_havesubobj || bpo->bpo_phys->bpo_num_subobjs == 0));
2085d7b4d4Matthew Ahrens}
2095d7b4d4Matthew Ahrens
210cde58dbMatthew Ahrensstatic int
211cde58dbMatthew Ahrensbpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
212cde58dbMatthew Ahrens    boolean_t free)
213cde58dbMatthew Ahrens{
214cde58dbMatthew Ahrens	dmu_object_info_t doi;
215cde58dbMatthew Ahrens	int epb;
216cde58dbMatthew Ahrens	int64_t i;
217cde58dbMatthew Ahrens	int err = 0;
218cde58dbMatthew Ahrens	dmu_buf_t *dbuf = NULL;
219cde58dbMatthew Ahrens
2205cabbc6Prashanth Sreenivasa	ASSERT(bpobj_is_open(bpo));
221cde58dbMatthew Ahrens	mutex_enter(&bpo->bpo_lock);
222cde58dbMatthew Ahrens
223cde58dbMatthew Ahrens	if (free)
224cde58dbMatthew Ahrens		dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
225cde58dbMatthew Ahrens
226cde58dbMatthew Ahrens	for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
227cde58dbMatthew Ahrens		blkptr_t *bparray;
228cde58dbMatthew Ahrens		blkptr_t *bp;
229cde58dbMatthew Ahrens		uint64_t offset, blkoff;
230cde58dbMatthew Ahrens
231cde58dbMatthew Ahrens		offset = i * sizeof (blkptr_t);
232cde58dbMatthew Ahrens		blkoff = P2PHASE(i, bpo->bpo_epb);
233cde58dbMatthew Ahrens
234cde58dbMatthew Ahrens		if (dbuf == NULL || dbuf->db_offset > offset) {
235cde58dbMatthew Ahrens			if (dbuf)
236cde58dbMatthew Ahrens				dmu_buf_rele(dbuf, FTAG);
237cde58dbMatthew Ahrens			err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
238cde58dbMatthew Ahrens			    FTAG, &dbuf, 0);
239cde58dbMatthew Ahrens			if (err)
240cde58dbMatthew Ahrens				break;
241cde58dbMatthew Ahrens		}
242cde58dbMatthew Ahrens
243cde58dbMatthew Ahrens		ASSERT3U(offset, >=, dbuf->db_offset);
244cde58dbMatthew Ahrens		ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
245cde58dbMatthew Ahrens
246cde58dbMatthew Ahrens		bparray = dbuf->db_data;
247cde58dbMatthew Ahrens		bp = &bparray[blkoff];
248cde58dbMatthew Ahrens		err = func(arg, bp, tx);
249cde58dbMatthew Ahrens		if (err)
250cde58dbMatthew Ahrens			break;
251cde58dbMatthew Ahrens		if (free) {
252cde58dbMatthew Ahrens			bpo->bpo_phys->bpo_bytes -=
253cde58dbMatthew Ahrens			    bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
254cde58dbMatthew Ahrens			ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
255cde58dbMatthew Ahrens			if (bpo->bpo_havecomp) {
256cde58dbMatthew Ahrens				bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
257cde58dbMatthew Ahrens				bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
258cde58dbMatthew Ahrens			}
259cde58dbMatthew Ahrens			bpo->bpo_phys->bpo_num_blkptrs--;
260cde58dbMatthew Ahrens			ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
261cde58dbMatthew Ahrens		}
262cde58dbMatthew Ahrens	}
263cde58dbMatthew Ahrens	if (dbuf) {
264cde58dbMatthew Ahrens		dmu_buf_rele(dbuf, FTAG);
265cde58dbMatthew Ahrens		dbuf = NULL;
266cde58dbMatthew Ahrens	}
267cde58dbMatthew Ahrens	if (free) {
268b420f3aRichard Lowe		VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
269eb63303Tom Caputi		    (i + 1) * sizeof (blkptr_t), DMU_OBJECT_END, tx));
270cde58dbMatthew Ahrens	}
271cde58dbMatthew Ahrens	if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
272cde58dbMatthew Ahrens		goto out;
273cde58dbMatthew Ahrens
274cde58dbMatthew Ahrens	ASSERT(bpo->bpo_havecomp);
275cde58dbMatthew Ahrens	err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
276a0e4757Lin Ling	if (err) {
277a0e4757Lin Ling		mutex_exit(&bpo->bpo_lock);
278cde58dbMatthew Ahrens		return (err);
279a0e4757Lin Ling	}
2802acef22Matthew Ahrens	ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
281cde58dbMatthew Ahrens	epb = doi.doi_data_block_size / sizeof (uint64_t);
282cde58dbMatthew Ahrens
283cde58dbMatthew Ahrens	for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
284cde58dbMatthew Ahrens		uint64_t *objarray;
285cde58dbMatthew Ahrens		uint64_t offset, blkoff;
286cde58dbMatthew Ahrens		bpobj_t sublist;
287cde58dbMatthew Ahrens		uint64_t used_before, comp_before, uncomp_before;
288cde58dbMatthew Ahrens		uint64_t used_after, comp_after, uncomp_after;
289cde58dbMatthew Ahrens
290cde58dbMatthew Ahrens		offset = i * sizeof (uint64_t);
291cde58dbMatthew Ahrens		blkoff = P2PHASE(i, epb);
292cde58dbMatthew Ahrens
293cde58dbMatthew Ahrens		if (dbuf == NULL || dbuf->db_offset > offset) {
294cde58dbMatthew Ahrens			if (dbuf)
295cde58dbMatthew Ahrens				dmu_buf_rele(dbuf, FTAG);
296cde58dbMatthew Ahrens			err = dmu_buf_hold(bpo->bpo_os,
297cde58dbMatthew Ahrens			    bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
298cde58dbMatthew Ahrens			if (err)
299cde58dbMatthew Ahrens				break;
300cde58dbMatthew Ahrens		}
301cde58dbMatthew Ahrens
302cde58dbMatthew Ahrens		ASSERT3U(offset, >=, dbuf->db_offset);
303cde58dbMatthew Ahrens		ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
304cde58dbMatthew Ahrens
305cde58dbMatthew Ahrens		objarray = dbuf->db_data;
306cde58dbMatthew Ahrens		err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
307cde58dbMatthew Ahrens		if (err)
308cde58dbMatthew Ahrens			break;
309cde58dbMatthew Ahrens		if (free) {
310cde58dbMatthew Ahrens			err = bpobj_space(&sublist,
311cde58dbMatthew Ahrens			    &used_before, &comp_before, &uncomp_before);
312b67dde1Will Andrews			if (err != 0) {
313b67dde1Will Andrews				bpobj_close(&sublist);
314cde58dbMatthew Ahrens				break;
315b67dde1Will Andrews			}
316cde58dbMatthew Ahrens		}
317cde58dbMatthew Ahrens		err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
318cde58dbMatthew Ahrens		if (free) {
319b420f3aRichard Lowe			VERIFY3U(0, ==, bpobj_space(&sublist,
320cde58dbMatthew Ahrens			    &used_after, &comp_after, &uncomp_after));
321cde58dbMatthew Ahrens			bpo->bpo_phys->bpo_bytes -= used_before - used_after;
322cde58dbMatthew Ahrens			ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
3234bc4cb7Matthew Ahrens			bpo->bpo_phys->bpo_comp -= comp_before - comp_after;
324cde58dbMatthew Ahrens			bpo->bpo_phys->bpo_uncomp -=
325cde58dbMatthew Ahrens			    uncomp_before - uncomp_after;
326cde58dbMatthew Ahrens		}
327cde58dbMatthew Ahrens
328cde58dbMatthew Ahrens		bpobj_close(&sublist);
329cde58dbMatthew Ahrens		if (err)
330cde58dbMatthew Ahrens			break;
331cde58dbMatthew Ahrens		if (free) {
332cde58dbMatthew Ahrens			err = dmu_object_free(bpo->bpo_os,
333cde58dbMatthew Ahrens			    objarray[blkoff], tx);
334cde58dbMatthew Ahrens			if (err)
335cde58dbMatthew Ahrens				break;
336cde58dbMatthew Ahrens			bpo->bpo_phys->bpo_num_subobjs--;
337cde58dbMatthew Ahrens			ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
338cde58dbMatthew Ahrens		}
339cde58dbMatthew Ahrens	}
340cde58dbMatthew Ahrens	if (dbuf) {
341cde58dbMatthew Ahrens		dmu_buf_rele(dbuf, FTAG);
342cde58dbMatthew Ahrens		dbuf = NULL;
343cde58dbMatthew Ahrens	}
344cde58dbMatthew Ahrens	if (free) {
345b420f3aRichard Lowe		VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
346cde58dbMatthew Ahrens		    bpo->bpo_phys->bpo_subobjs,
347eb63303Tom Caputi		    (i + 1) * sizeof (uint64_t), DMU_OBJECT_END, tx));
348cde58dbMatthew Ahrens	}
349cde58dbMatthew Ahrens
350cde58dbMatthew Ahrensout:
351cde58dbMatthew Ahrens	/* If there are no entries, there should be no bytes. */
3525cabbc6Prashanth Sreenivasa	if (bpobj_is_empty(bpo)) {
3535d7b4d4Matthew Ahrens		ASSERT0(bpo->bpo_phys->bpo_bytes);
3545d7b4d4Matthew Ahrens		ASSERT0(bpo->bpo_phys->bpo_comp);
3555d7b4d4Matthew Ahrens		ASSERT0(bpo->bpo_phys->bpo_uncomp);
3565d7b4d4Matthew Ahrens	}
357cde58dbMatthew Ahrens
358cde58dbMatthew Ahrens	mutex_exit(&bpo->bpo_lock);
359cde58dbMatthew Ahrens	return (err);
360cde58dbMatthew Ahrens}
361cde58dbMatthew Ahrens
362cde58dbMatthew Ahrens/*
363cde58dbMatthew Ahrens * Iterate and remove the entries.  If func returns nonzero, iteration
364cde58dbMatthew Ahrens * will stop and that entry will not be removed.
365cde58dbMatthew Ahrens */
366cde58dbMatthew Ahrensint
367cde58dbMatthew Ahrensbpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
368cde58dbMatthew Ahrens{
369cde58dbMatthew Ahrens	return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
370cde58dbMatthew Ahrens}
371cde58dbMatthew Ahrens
372cde58dbMatthew Ahrens/*
373cde58dbMatthew Ahrens * Iterate the entries.  If func returns nonzero, iteration will stop.
374cde58dbMatthew Ahrens */
375cde58dbMatthew Ahrensint
376cde58dbMatthew Ahrensbpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
377cde58dbMatthew Ahrens{
378cde58dbMatthew Ahrens	return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE));
379cde58dbMatthew Ahrens}
380cde58dbMatthew Ahrens
381cde58dbMatthew Ahrensvoid
382cde58dbMatthew Ahrensbpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
383cde58dbMatthew Ahrens{
384cde58dbMatthew Ahrens	bpobj_t subbpo;
3854bc4cb7Matthew Ahrens	uint64_t used, comp, uncomp, subsubobjs;
386cde58dbMatthew Ahrens
3875cabbc6Prashanth Sreenivasa	ASSERT(bpobj_is_open(bpo));
3885cabbc6Prashanth Sreenivasa	ASSERT(subobj != 0);
389cde58dbMatthew Ahrens	ASSERT(bpo->bpo_havesubobj);
390cde58dbMatthew Ahrens	ASSERT(bpo->bpo_havecomp);
391f174573Matthew Ahrens	ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
392f174573Matthew Ahrens
393f174573Matthew Ahrens	if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
394f174573Matthew Ahrens		bpobj_decr_empty(bpo->bpo_os, tx);
395f174573Matthew Ahrens		return;
396f174573Matthew Ahrens	}
397cde58dbMatthew Ahrens
398b420f3aRichard Lowe	VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
399b420f3aRichard Lowe	VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
400cde58dbMatthew Ahrens
4015cabbc6Prashanth Sreenivasa	if (bpobj_is_empty(&subbpo)) {
402cde58dbMatthew Ahrens		/* No point in having an empty subobj. */
4034bc4cb7Matthew Ahrens		bpobj_close(&subbpo);
404cde58dbMatthew Ahrens		bpobj_free(bpo->bpo_os, subobj, tx);
405cde58dbMatthew Ahrens		return;
406cde58dbMatthew Ahrens	}
407cde58dbMatthew Ahrens
408a3905a4Serapheim Dimitropoulos	mutex_enter(&bpo->bpo_lock);
409cde58dbMatthew Ahrens	dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
410cde58dbMatthew Ahrens	if (bpo->bpo_phys->bpo_subobjs == 0) {
411cde58dbMatthew Ahrens		bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
412b515258Matthew Ahrens		    DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
413b515258Matthew Ahrens		    DMU_OT_NONE, 0, tx);
414cde58dbMatthew Ahrens	}
415cde58dbMatthew Ahrens
4163b2aab1Matthew Ahrens	dmu_object_info_t doi;
4173b2aab1Matthew Ahrens	ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi));
4183b2aab1Matthew Ahrens	ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
4193b2aab1Matthew Ahrens
420cde58dbMatthew Ahrens	dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
421cde58dbMatthew Ahrens	    bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
422cde58dbMatthew Ahrens	    sizeof (subobj), &subobj, tx);
423cde58dbMatthew Ahrens	bpo->bpo_phys->bpo_num_subobjs++;
4244bc4cb7Matthew Ahrens
4254bc4cb7Matthew Ahrens	/*
4264bc4cb7Matthew Ahrens	 * If subobj has only one block of subobjs, then move subobj's
4274bc4cb7Matthew Ahrens	 * subobjs to bpo's subobj list directly.  This reduces
4284bc4cb7Matthew Ahrens	 * recursion in bpobj_iterate due to nested subobjs.
4294bc4cb7Matthew Ahrens	 */
4304bc4cb7Matthew Ahrens	subsubobjs = subbpo.bpo_phys->bpo_subobjs;
4314bc4cb7Matthew Ahrens	if (subsubobjs != 0) {
4324bc4cb7Matthew Ahrens		dmu_object_info_t doi;
4334bc4cb7Matthew Ahrens
434b420f3aRichard Lowe		VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
4354bc4cb7Matthew Ahrens		if (doi.doi_max_offset == doi.doi_data_block_size) {
4364bc4cb7Matthew Ahrens			dmu_buf_t *subdb;
4374bc4cb7Matthew Ahrens			uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
4384bc4cb7Matthew Ahrens
439b420f3aRichard Lowe			VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
4404bc4cb7Matthew Ahrens			    0, FTAG, &subdb, 0));
441d047563Matthew Ahrens			/*
442d047563Matthew Ahrens			 * Make sure that we are not asking dmu_write()
443d047563Matthew Ahrens			 * to write more data than we have in our buffer.
444d047563Matthew Ahrens			 */
445d047563Matthew Ahrens			VERIFY3U(subdb->db_size, >=,
446d047563Matthew Ahrens			    numsubsub * sizeof (subobj));
4474bc4cb7Matthew Ahrens			dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
4484bc4cb7Matthew Ahrens			    bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
4494bc4cb7Matthew Ahrens			    numsubsub * sizeof (subobj), subdb->db_data, tx);
4504bc4cb7Matthew Ahrens			dmu_buf_rele(subdb, FTAG);
4514bc4cb7Matthew Ahrens			bpo->bpo_phys->bpo_num_subobjs += numsubsub;
4524bc4cb7Matthew Ahrens
4534bc4cb7Matthew Ahrens			dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
4544bc4cb7Matthew Ahrens			subbpo.bpo_phys->bpo_subobjs = 0;
455b420f3aRichard Lowe			VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
4564bc4cb7Matthew Ahrens			    subsubobjs, tx));
4574bc4cb7Matthew Ahrens		}
4584bc4cb7Matthew Ahrens	}
459cde58dbMatthew Ahrens	bpo->bpo_phys->bpo_bytes += used;
460cde58dbMatthew Ahrens	bpo->bpo_phys->bpo_comp += comp;
461cde58dbMatthew Ahrens	bpo->bpo_phys->bpo_uncomp += uncomp;
462cde58dbMatthew Ahrens	mutex_exit(&bpo->bpo_lock);
4634bc4cb7Matthew Ahrens
4644bc4cb7Matthew Ahrens	bpobj_close(&subbpo);
465cde58dbMatthew Ahrens}
466cde58dbMatthew Ahrens
467cde58dbMatthew Ahrensvoid
468cde58dbMatthew Ahrensbpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
469cde58dbMatthew Ahrens{
470cde58dbMatthew Ahrens	blkptr_t stored_bp = *bp;
471cde58dbMatthew Ahrens	uint64_t offset;
472cde58dbMatthew Ahrens	int blkoff;
473cde58dbMatthew Ahrens	blkptr_t *bparray;
474cde58dbMatthew Ahrens
4755cabbc6Prashanth Sreenivasa	ASSERT(bpobj_is_open(bpo));
476cde58dbMatthew Ahrens	ASSERT(!BP_IS_HOLE(bp));
477f174573Matthew Ahrens	ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
478cde58dbMatthew Ahrens
4795d7b4d4Matthew Ahrens	if (BP_IS_EMBEDDED(bp)) {
4805d7b4d4Matthew Ahrens		/*
4815d7b4d4Matthew Ahrens		 * The bpobj will compress better without the payload.
4825d7b4d4Matthew Ahrens		 *
4835d7b4d4Matthew Ahrens		 * Note that we store EMBEDDED bp's because they have an
4845d7b4d4Matthew Ahrens		 * uncompressed size, which must be accounted for.  An
4855d7b4d4Matthew Ahrens		 * alternative would be to add their size to bpo_uncomp
4865d7b4d4Matthew Ahrens		 * without storing the bp, but that would create additional
4875d7b4d4Matthew Ahrens		 * complications: bpo_uncomp would be inconsistent with the
4885d7b4d4Matthew Ahrens		 * set of BP's stored, and bpobj_iterate() wouldn't visit
4895d7b4d4Matthew Ahrens		 * all the space accounted for in the bpobj.
4905d7b4d4Matthew Ahrens		 */
4915d7b4d4Matthew Ahrens		bzero(&stored_bp, sizeof (stored_bp));
4925d7b4d4Matthew Ahrens		stored_bp.blk_prop = bp->blk_prop;
4935d7b4d4Matthew Ahrens		stored_bp.blk_birth = bp->blk_birth;
4945d7b4d4Matthew Ahrens	} else if (!BP_GET_DEDUP(bp)) {
4955d7b4d4Matthew Ahrens		/* The bpobj will compress better without the checksum */
4965d7b4d4Matthew Ahrens		bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
4975d7b4d4Matthew Ahrens	}
4985d7b4d4Matthew Ahrens
499cde58dbMatthew Ahrens	/* We never need the fill count. */
500cde58dbMatthew Ahrens	stored_bp.blk_fill = 0;
501cde58dbMatthew Ahrens
502cde58dbMatthew Ahrens	mutex_enter(&bpo->bpo_lock);
503cde58dbMatthew Ahrens
504cde58dbMatthew Ahrens	offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
505cde58dbMatthew Ahrens	blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
506cde58dbMatthew Ahrens
507cde58dbMatthew Ahrens	if (bpo->bpo_cached_dbuf == NULL ||
508cde58dbMatthew Ahrens	    offset < bpo->bpo_cached_dbuf->db_offset ||
509cde58dbMatthew Ahrens	    offset >= bpo->bpo_cached_dbuf->db_offset +
510cde58dbMatthew Ahrens	    bpo->bpo_cached_dbuf->db_size) {
511cde58dbMatthew Ahrens		if (bpo->bpo_cached_dbuf)
512cde58dbMatthew Ahrens			dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
513b420f3aRichard Lowe		VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
514cde58dbMatthew Ahrens		    offset, bpo, &bpo->bpo_cached_dbuf, 0));
515cde58dbMatthew Ahrens	}
516cde58dbMatthew Ahrens
517cde58dbMatthew Ahrens	dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
518cde58dbMatthew Ahrens	bparray = bpo->bpo_cached_dbuf->db_data;
519cde58dbMatthew Ahrens	bparray[blkoff] = stored_bp;
520cde58dbMatthew Ahrens
521cde58dbMatthew Ahrens	dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
522cde58dbMatthew Ahrens	bpo->bpo_phys->bpo_num_blkptrs++;
523cde58dbMatthew Ahrens	bpo->bpo_phys->bpo_bytes +=
524cde58dbMatthew Ahrens	    bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
525cde58dbMatthew Ahrens	if (bpo->bpo_havecomp) {
526cde58dbMatthew Ahrens		bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
527cde58dbMatthew Ahrens		bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
528cde58dbMatthew Ahrens	}
529cde58dbMatthew Ahrens	mutex_exit(&bpo->bpo_lock);
530cde58dbMatthew Ahrens}
531cde58dbMatthew Ahrens
532cde58dbMatthew Ahrensstruct space_range_arg {
533cde58dbMatthew Ahrens	spa_t *spa;
534cde58dbMatthew Ahrens	uint64_t mintxg;
535cde58dbMatthew Ahrens	uint64_t maxtxg;
536cde58dbMatthew Ahrens	uint64_t used;
537cde58dbMatthew Ahrens	uint64_t comp;
538cde58dbMatthew Ahrens	uint64_t uncomp;
539cde58dbMatthew Ahrens};
540cde58dbMatthew Ahrens
541cde58dbMatthew Ahrens/* ARGSUSED */
542cde58dbMatthew Ahrensstatic int
543cde58dbMatthew Ahrensspace_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
544cde58dbMatthew Ahrens{
545cde58dbMatthew Ahrens	struct space_range_arg *sra = arg;
546cde58dbMatthew Ahrens
547cde58dbMatthew Ahrens	if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
54819b94dfMatthew Ahrens		if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
54919b94dfMatthew Ahrens			sra->used += bp_get_dsize_sync(sra->spa, bp);
55019b94dfMatthew Ahrens		else
55119b94dfMatthew Ahrens			sra->used += bp_get_dsize(sra->spa, bp);
552cde58dbMatthew Ahrens		sra->comp += BP_GET_PSIZE(bp);
553cde58dbMatthew Ahrens		sra->uncomp += BP_GET_UCSIZE(bp);
554cde58dbMatthew Ahrens	}
555cde58dbMatthew Ahrens	return (0);
556cde58dbMatthew Ahrens}
557cde58dbMatthew Ahrens
558cde58dbMatthew Ahrensint
559cde58dbMatthew Ahrensbpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
560cde58dbMatthew Ahrens{
5615cabbc6Prashanth Sreenivasa	ASSERT(bpobj_is_open(bpo));
562cde58dbMatthew Ahrens	mutex_enter(&bpo->bpo_lock);
563cde58dbMatthew Ahrens
564cde58dbMatthew Ahrens	*usedp = bpo->bpo_phys->bpo_bytes;
565cde58dbMatthew Ahrens	if (bpo->bpo_havecomp) {
566cde58dbMatthew Ahrens		*compp = bpo->bpo_phys->bpo_comp;
567cde58dbMatthew Ahrens		*uncompp = bpo->bpo_phys->bpo_uncomp;
568cde58dbMatthew Ahrens		mutex_exit(&bpo->bpo_lock);
569cde58dbMatthew Ahrens		return (0);
570cde58dbMatthew Ahrens	} else {
571cde58dbMatthew Ahrens		mutex_exit(&bpo->bpo_lock);
572cde58dbMatthew Ahrens		return (bpobj_space_range(bpo, 0, UINT64_MAX,
573cde58dbMatthew Ahrens		    usedp, compp, uncompp));
574cde58dbMatthew Ahrens	}
575cde58dbMatthew Ahrens}
576cde58dbMatthew Ahrens
577cde58dbMatthew Ahrens/*
578cde58dbMatthew Ahrens * Return the amount of space in the bpobj which is:
579cde58dbMatthew Ahrens * mintxg < blk_birth <= maxtxg
580cde58dbMatthew Ahrens */
581cde58dbMatthew Ahrensint
582cde58dbMatthew Ahrensbpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
583cde58dbMatthew Ahrens    uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
584cde58dbMatthew Ahrens{
585cde58dbMatthew Ahrens	struct space_range_arg sra = { 0 };
586cde58dbMatthew Ahrens	int err;
587cde58dbMatthew Ahrens
5885cabbc6Prashanth Sreenivasa	ASSERT(bpobj_is_open(bpo));
5895cabbc6Prashanth Sreenivasa
590cde58dbMatthew Ahrens	/*
591cde58dbMatthew Ahrens	 * As an optimization, if they want the whole txg range, just
592cde58dbMatthew Ahrens	 * get bpo_bytes rather than iterating over the bps.
593cde58dbMatthew Ahrens	 */
594cde58dbMatthew Ahrens	if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
595cde58dbMatthew Ahrens		return (bpobj_space(bpo, usedp, compp, uncompp));
596cde58dbMatthew Ahrens
597cde58dbMatthew Ahrens	sra.spa = dmu_objset_spa(bpo->bpo_os);
598cde58dbMatthew Ahrens	sra.mintxg = mintxg;
599cde58dbMatthew Ahrens	sra.maxtxg = maxtxg;
600cde58dbMatthew Ahrens
601cde58dbMatthew Ahrens	err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
602cde58dbMatthew Ahrens	*usedp = sra.used;
603cde58dbMatthew Ahrens	*compp = sra.comp;
604cde58dbMatthew Ahrens	*uncompp = sra.uncomp;
605cde58dbMatthew Ahrens	return (err);
606cde58dbMatthew Ahrens}
607