1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 */
25
26 #include <sys/bpobj.h>
27 #include <sys/zfs_context.h>
28 #include <sys/refcount.h>
29 #include <sys/dsl_pool.h>
30 #include <sys/zfeature.h>
31 #include <sys/zap.h>
32
33 /*
34 * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
35 */
36 uint64_t
bpobj_alloc_empty(objset_t * os,int blocksize,dmu_tx_t * tx)37 bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
38 {
39 spa_t *spa = dmu_objset_spa(os);
40 dsl_pool_t *dp = dmu_objset_pool(os);
41
42 if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
43 if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
44 ASSERT0(dp->dp_empty_bpobj);
45 dp->dp_empty_bpobj =
46 bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx);
47 VERIFY(zap_add(os,
48 DMU_POOL_DIRECTORY_OBJECT,
49 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
50 &dp->dp_empty_bpobj, tx) == 0);
51 }
52 spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx);
53 ASSERT(dp->dp_empty_bpobj != 0);
54 return (dp->dp_empty_bpobj);
55 } else {
56 return (bpobj_alloc(os, blocksize, tx));
57 }
58 }
59
60 void
bpobj_decr_empty(objset_t * os,dmu_tx_t * tx)61 bpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
62 {
63 dsl_pool_t *dp = dmu_objset_pool(os);
64
65 spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx);
66 if (!spa_feature_is_active(dmu_objset_spa(os),
67 SPA_FEATURE_EMPTY_BPOBJ)) {
68 VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
69 DMU_POOL_DIRECTORY_OBJECT,
70 DMU_POOL_EMPTY_BPOBJ, tx));
71 VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
72 dp->dp_empty_bpobj = 0;
73 }
74 }
75
76 uint64_t
bpobj_alloc(objset_t * os,int blocksize,dmu_tx_t * tx)77 bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
78 {
79 int size;
80
81 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
82 size = BPOBJ_SIZE_V0;
83 else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
84 size = BPOBJ_SIZE_V1;
85 else
86 size = sizeof (bpobj_phys_t);
87
88 return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
89 DMU_OT_BPOBJ_HDR, size, tx));
90 }
91
92 void
bpobj_free(objset_t * os,uint64_t obj,dmu_tx_t * tx)93 bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
94 {
95 int64_t i;
96 bpobj_t bpo;
97 dmu_object_info_t doi;
98 int epb;
99 dmu_buf_t *dbuf = NULL;
100
101 ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
102 VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
103
104 mutex_enter(&bpo.bpo_lock);
105
106 if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
107 goto out;
108
109 VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
110 epb = doi.doi_data_block_size / sizeof (uint64_t);
111
112 for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
113 uint64_t *objarray;
114 uint64_t offset, blkoff;
115
116 offset = i * sizeof (uint64_t);
117 blkoff = P2PHASE(i, epb);
118
119 if (dbuf == NULL || dbuf->db_offset > offset) {
120 if (dbuf)
121 dmu_buf_rele(dbuf, FTAG);
122 VERIFY3U(0, ==, dmu_buf_hold(os,
123 bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
124 }
125
126 ASSERT3U(offset, >=, dbuf->db_offset);
127 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
128
129 objarray = dbuf->db_data;
130 bpobj_free(os, objarray[blkoff], tx);
131 }
132 if (dbuf) {
133 dmu_buf_rele(dbuf, FTAG);
134 dbuf = NULL;
135 }
136 VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
137
138 out:
139 mutex_exit(&bpo.bpo_lock);
140 bpobj_close(&bpo);
141
142 VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
143 }
144
145 int
bpobj_open(bpobj_t * bpo,objset_t * os,uint64_t object)146 bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
147 {
148 dmu_object_info_t doi;
149 int err;
150
151 err = dmu_object_info(os, object, &doi);
152 if (err)
153 return (err);
154
155 bzero(bpo, sizeof (*bpo));
156 mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
157
158 ASSERT(bpo->bpo_dbuf == NULL);
159 ASSERT(bpo->bpo_phys == NULL);
160 ASSERT(object != 0);
161 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
162 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
163
164 err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
165 if (err)
166 return (err);
167
168 bpo->bpo_os = os;
169 bpo->bpo_object = object;
170 bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
171 bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
172 bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
173 bpo->bpo_phys = bpo->bpo_dbuf->db_data;
174 return (0);
175 }
176
177 void
bpobj_close(bpobj_t * bpo)178 bpobj_close(bpobj_t *bpo)
179 {
180 /* Lame workaround for closing a bpobj that was never opened. */
181 if (bpo->bpo_object == 0)
182 return;
183
184 dmu_buf_rele(bpo->bpo_dbuf, bpo);
185 if (bpo->bpo_cached_dbuf != NULL)
186 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
187 bpo->bpo_dbuf = NULL;
188 bpo->bpo_phys = NULL;
189 bpo->bpo_cached_dbuf = NULL;
190 bpo->bpo_object = 0;
191
192 mutex_destroy(&bpo->bpo_lock);
193 }
194
195 static boolean_t
bpobj_hasentries(bpobj_t * bpo)196 bpobj_hasentries(bpobj_t *bpo)
197 {
198 return (bpo->bpo_phys->bpo_num_blkptrs != 0 ||
199 (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs != 0));
200 }
201
202 static int
bpobj_iterate_impl(bpobj_t * bpo,bpobj_itor_t func,void * arg,dmu_tx_t * tx,boolean_t free)203 bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
204 boolean_t free)
205 {
206 dmu_object_info_t doi;
207 int epb;
208 int64_t i;
209 int err = 0;
210 dmu_buf_t *dbuf = NULL;
211
212 mutex_enter(&bpo->bpo_lock);
213
214 if (free)
215 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
216
217 for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
218 blkptr_t *bparray;
219 blkptr_t *bp;
220 uint64_t offset, blkoff;
221
222 offset = i * sizeof (blkptr_t);
223 blkoff = P2PHASE(i, bpo->bpo_epb);
224
225 if (dbuf == NULL || dbuf->db_offset > offset) {
226 if (dbuf)
227 dmu_buf_rele(dbuf, FTAG);
228 err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
229 FTAG, &dbuf, 0);
230 if (err)
231 break;
232 }
233
234 ASSERT3U(offset, >=, dbuf->db_offset);
235 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
236
237 bparray = dbuf->db_data;
238 bp = &bparray[blkoff];
239 err = func(arg, bp, tx);
240 if (err)
241 break;
242 if (free) {
243 bpo->bpo_phys->bpo_bytes -=
244 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
245 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
246 if (bpo->bpo_havecomp) {
247 bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
248 bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
249 }
250 bpo->bpo_phys->bpo_num_blkptrs--;
251 ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
252 }
253 }
254 if (dbuf) {
255 dmu_buf_rele(dbuf, FTAG);
256 dbuf = NULL;
257 }
258 if (free) {
259 i++;
260 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
261 i * sizeof (blkptr_t), -1ULL, tx));
262 }
263 if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
264 goto out;
265
266 ASSERT(bpo->bpo_havecomp);
267 err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
268 if (err) {
269 mutex_exit(&bpo->bpo_lock);
270 return (err);
271 }
272 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
273 epb = doi.doi_data_block_size / sizeof (uint64_t);
274
275 for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
276 uint64_t *objarray;
277 uint64_t offset, blkoff;
278 bpobj_t sublist;
279 uint64_t used_before, comp_before, uncomp_before;
280 uint64_t used_after, comp_after, uncomp_after;
281
282 offset = i * sizeof (uint64_t);
283 blkoff = P2PHASE(i, epb);
284
285 if (dbuf == NULL || dbuf->db_offset > offset) {
286 if (dbuf)
287 dmu_buf_rele(dbuf, FTAG);
288 err = dmu_buf_hold(bpo->bpo_os,
289 bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
290 if (err)
291 break;
292 }
293
294 ASSERT3U(offset, >=, dbuf->db_offset);
295 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
296
297 objarray = dbuf->db_data;
298 err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
299 if (err)
300 break;
301 if (free) {
302 err = bpobj_space(&sublist,
303 &used_before, &comp_before, &uncomp_before);
304 if (err)
305 break;
306 }
307 err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
308 if (free) {
309 VERIFY3U(0, ==, bpobj_space(&sublist,
310 &used_after, &comp_after, &uncomp_after));
311 bpo->bpo_phys->bpo_bytes -= used_before - used_after;
312 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
313 bpo->bpo_phys->bpo_comp -= comp_before - comp_after;
314 bpo->bpo_phys->bpo_uncomp -=
315 uncomp_before - uncomp_after;
316 }
317
318 bpobj_close(&sublist);
319 if (err)
320 break;
321 if (free) {
322 err = dmu_object_free(bpo->bpo_os,
323 objarray[blkoff], tx);
324 if (err)
325 break;
326 bpo->bpo_phys->bpo_num_subobjs--;
327 ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
328 }
329 }
330 if (dbuf) {
331 dmu_buf_rele(dbuf, FTAG);
332 dbuf = NULL;
333 }
334 if (free) {
335 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
336 bpo->bpo_phys->bpo_subobjs,
337 (i + 1) * sizeof (uint64_t), -1ULL, tx));
338 }
339
340 out:
341 /* If there are no entries, there should be no bytes. */
342 if (!bpobj_hasentries(bpo)) {
343 ASSERT0(bpo->bpo_phys->bpo_bytes);
344 ASSERT0(bpo->bpo_phys->bpo_comp);
345 ASSERT0(bpo->bpo_phys->bpo_uncomp);
346 }
347
348 mutex_exit(&bpo->bpo_lock);
349 return (err);
350 }
351
352 /*
353 * Iterate and remove the entries. If func returns nonzero, iteration
354 * will stop and that entry will not be removed.
355 */
356 int
bpobj_iterate(bpobj_t * bpo,bpobj_itor_t func,void * arg,dmu_tx_t * tx)357 bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
358 {
359 return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
360 }
361
362 /*
363 * Iterate the entries. If func returns nonzero, iteration will stop.
364 */
365 int
bpobj_iterate_nofree(bpobj_t * bpo,bpobj_itor_t func,void * arg,dmu_tx_t * tx)366 bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
367 {
368 return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE));
369 }
370
371 void
bpobj_enqueue_subobj(bpobj_t * bpo,uint64_t subobj,dmu_tx_t * tx)372 bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
373 {
374 bpobj_t subbpo;
375 uint64_t used, comp, uncomp, subsubobjs;
376
377 ASSERT(bpo->bpo_havesubobj);
378 ASSERT(bpo->bpo_havecomp);
379 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
380
381 if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
382 bpobj_decr_empty(bpo->bpo_os, tx);
383 return;
384 }
385
386 VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
387 VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
388
389 if (!bpobj_hasentries(&subbpo)) {
390 /* No point in having an empty subobj. */
391 bpobj_close(&subbpo);
392 bpobj_free(bpo->bpo_os, subobj, tx);
393 return;
394 }
395
396 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
397 if (bpo->bpo_phys->bpo_subobjs == 0) {
398 bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
399 DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
400 DMU_OT_NONE, 0, tx);
401 }
402
403 dmu_object_info_t doi;
404 ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi));
405 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
406
407 mutex_enter(&bpo->bpo_lock);
408 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
409 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
410 sizeof (subobj), &subobj, tx);
411 bpo->bpo_phys->bpo_num_subobjs++;
412
413 /*
414 * If subobj has only one block of subobjs, then move subobj's
415 * subobjs to bpo's subobj list directly. This reduces
416 * recursion in bpobj_iterate due to nested subobjs.
417 */
418 subsubobjs = subbpo.bpo_phys->bpo_subobjs;
419 if (subsubobjs != 0) {
420 dmu_object_info_t doi;
421
422 VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
423 if (doi.doi_max_offset == doi.doi_data_block_size) {
424 dmu_buf_t *subdb;
425 uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
426
427 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
428 0, FTAG, &subdb, 0));
429 /*
430 * Make sure that we are not asking dmu_write()
431 * to write more data than we have in our buffer.
432 */
433 VERIFY3U(subdb->db_size, >=,
434 numsubsub * sizeof (subobj));
435 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
436 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
437 numsubsub * sizeof (subobj), subdb->db_data, tx);
438 dmu_buf_rele(subdb, FTAG);
439 bpo->bpo_phys->bpo_num_subobjs += numsubsub;
440
441 dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
442 subbpo.bpo_phys->bpo_subobjs = 0;
443 VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
444 subsubobjs, tx));
445 }
446 }
447 bpo->bpo_phys->bpo_bytes += used;
448 bpo->bpo_phys->bpo_comp += comp;
449 bpo->bpo_phys->bpo_uncomp += uncomp;
450 mutex_exit(&bpo->bpo_lock);
451
452 bpobj_close(&subbpo);
453 }
454
455 void
bpobj_enqueue(bpobj_t * bpo,const blkptr_t * bp,dmu_tx_t * tx)456 bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
457 {
458 blkptr_t stored_bp = *bp;
459 uint64_t offset;
460 int blkoff;
461 blkptr_t *bparray;
462
463 ASSERT(!BP_IS_HOLE(bp));
464 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
465
466 if (BP_IS_EMBEDDED(bp)) {
467 /*
468 * The bpobj will compress better without the payload.
469 *
470 * Note that we store EMBEDDED bp's because they have an
471 * uncompressed size, which must be accounted for. An
472 * alternative would be to add their size to bpo_uncomp
473 * without storing the bp, but that would create additional
474 * complications: bpo_uncomp would be inconsistent with the
475 * set of BP's stored, and bpobj_iterate() wouldn't visit
476 * all the space accounted for in the bpobj.
477 */
478 bzero(&stored_bp, sizeof (stored_bp));
479 stored_bp.blk_prop = bp->blk_prop;
480 stored_bp.blk_birth = bp->blk_birth;
481 } else if (!BP_GET_DEDUP(bp)) {
482 /* The bpobj will compress better without the checksum */
483 bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
484 }
485
486 /* We never need the fill count. */
487 stored_bp.blk_fill = 0;
488
489 mutex_enter(&bpo->bpo_lock);
490
491 offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
492 blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
493
494 if (bpo->bpo_cached_dbuf == NULL ||
495 offset < bpo->bpo_cached_dbuf->db_offset ||
496 offset >= bpo->bpo_cached_dbuf->db_offset +
497 bpo->bpo_cached_dbuf->db_size) {
498 if (bpo->bpo_cached_dbuf)
499 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
500 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
501 offset, bpo, &bpo->bpo_cached_dbuf, 0));
502 }
503
504 dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
505 bparray = bpo->bpo_cached_dbuf->db_data;
506 bparray[blkoff] = stored_bp;
507
508 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
509 bpo->bpo_phys->bpo_num_blkptrs++;
510 bpo->bpo_phys->bpo_bytes +=
511 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
512 if (bpo->bpo_havecomp) {
513 bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
514 bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
515 }
516 mutex_exit(&bpo->bpo_lock);
517 }
518
519 struct space_range_arg {
520 spa_t *spa;
521 uint64_t mintxg;
522 uint64_t maxtxg;
523 uint64_t used;
524 uint64_t comp;
525 uint64_t uncomp;
526 };
527
528 /* ARGSUSED */
529 static int
space_range_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)530 space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
531 {
532 struct space_range_arg *sra = arg;
533
534 if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
535 if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
536 sra->used += bp_get_dsize_sync(sra->spa, bp);
537 else
538 sra->used += bp_get_dsize(sra->spa, bp);
539 sra->comp += BP_GET_PSIZE(bp);
540 sra->uncomp += BP_GET_UCSIZE(bp);
541 }
542 return (0);
543 }
544
545 int
bpobj_space(bpobj_t * bpo,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)546 bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
547 {
548 mutex_enter(&bpo->bpo_lock);
549
550 *usedp = bpo->bpo_phys->bpo_bytes;
551 if (bpo->bpo_havecomp) {
552 *compp = bpo->bpo_phys->bpo_comp;
553 *uncompp = bpo->bpo_phys->bpo_uncomp;
554 mutex_exit(&bpo->bpo_lock);
555 return (0);
556 } else {
557 mutex_exit(&bpo->bpo_lock);
558 return (bpobj_space_range(bpo, 0, UINT64_MAX,
559 usedp, compp, uncompp));
560 }
561 }
562
563 /*
564 * Return the amount of space in the bpobj which is:
565 * mintxg < blk_birth <= maxtxg
566 */
567 int
bpobj_space_range(bpobj_t * bpo,uint64_t mintxg,uint64_t maxtxg,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)568 bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
569 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
570 {
571 struct space_range_arg sra = { 0 };
572 int err;
573
574 /*
575 * As an optimization, if they want the whole txg range, just
576 * get bpo_bytes rather than iterating over the bps.
577 */
578 if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
579 return (bpobj_space(bpo, usedp, compp, uncompp));
580
581 sra.spa = dmu_objset_spa(bpo->bpo_os);
582 sra.mintxg = mintxg;
583 sra.maxtxg = maxtxg;
584
585 err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
586 *usedp = sra.used;
587 *compp = sra.comp;
588 *uncompp = sra.uncomp;
589 return (err);
590 }
591