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