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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
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>
34 * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
37 bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
39 zfeature_info_t *empty_bpobj_feat =
40 &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ];
41 spa_t *spa = dmu_objset_spa(os);
42 dsl_pool_t *dp = dmu_objset_pool(os);
44 if (spa_feature_is_enabled(spa, empty_bpobj_feat)) {
45 if (!spa_feature_is_active(spa, empty_bpobj_feat)) {
46 ASSERT0(dp->dp_empty_bpobj);
48 bpobj_alloc(os, SPA_MAXBLOCKSIZE, tx);
50 DMU_POOL_DIRECTORY_OBJECT,
51 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
52 &dp->dp_empty_bpobj, tx) == 0);
54 spa_feature_incr(spa, empty_bpobj_feat, tx);
55 ASSERT(dp->dp_empty_bpobj != 0);
56 return (dp->dp_empty_bpobj);
58 return (bpobj_alloc(os, blocksize, tx));
63 bpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
65 zfeature_info_t *empty_bpobj_feat =
66 &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ];
67 dsl_pool_t *dp = dmu_objset_pool(os);
69 spa_feature_decr(dmu_objset_spa(os), empty_bpobj_feat, tx);
70 if (!spa_feature_is_active(dmu_objset_spa(os), empty_bpobj_feat)) {
71 VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
72 DMU_POOL_DIRECTORY_OBJECT,
73 DMU_POOL_EMPTY_BPOBJ, tx));
74 VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
75 dp->dp_empty_bpobj = 0;
80 bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
84 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
86 else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
89 size = sizeof (bpobj_phys_t);
91 return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
92 DMU_OT_BPOBJ_HDR, size, tx));
96 bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
100 dmu_object_info_t doi;
102 dmu_buf_t *dbuf = NULL;
104 ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
105 VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
107 mutex_enter(&bpo.bpo_lock);
109 if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
112 VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
113 epb = doi.doi_data_block_size / sizeof (uint64_t);
115 for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
117 uint64_t offset, blkoff;
119 offset = i * sizeof (uint64_t);
120 blkoff = P2PHASE(i, epb);
122 if (dbuf == NULL || dbuf->db_offset > offset) {
124 dmu_buf_rele(dbuf, FTAG);
125 VERIFY3U(0, ==, dmu_buf_hold(os,
126 bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
129 ASSERT3U(offset, >=, dbuf->db_offset);
130 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
132 objarray = dbuf->db_data;
133 bpobj_free(os, objarray[blkoff], tx);
136 dmu_buf_rele(dbuf, FTAG);
139 VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
142 mutex_exit(&bpo.bpo_lock);
145 VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
149 bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
151 dmu_object_info_t doi;
154 err = dmu_object_info(os, object, &doi);
158 bzero(bpo, sizeof (*bpo));
159 mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
161 ASSERT(bpo->bpo_dbuf == NULL);
162 ASSERT(bpo->bpo_phys == NULL);
164 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
165 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
167 err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
172 bpo->bpo_object = object;
173 bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
174 bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
175 bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
176 bpo->bpo_phys = bpo->bpo_dbuf->db_data;
181 bpobj_close(bpobj_t *bpo)
183 /* Lame workaround for closing a bpobj that was never opened. */
184 if (bpo->bpo_object == 0)
187 dmu_buf_rele(bpo->bpo_dbuf, bpo);
188 if (bpo->bpo_cached_dbuf != NULL)
189 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
190 bpo->bpo_dbuf = NULL;
191 bpo->bpo_phys = NULL;
192 bpo->bpo_cached_dbuf = NULL;
195 mutex_destroy(&bpo->bpo_lock);
199 bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
202 dmu_object_info_t doi;
206 dmu_buf_t *dbuf = NULL;
208 mutex_enter(&bpo->bpo_lock);
211 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
213 for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
216 uint64_t offset, blkoff;
218 offset = i * sizeof (blkptr_t);
219 blkoff = P2PHASE(i, bpo->bpo_epb);
221 if (dbuf == NULL || dbuf->db_offset > offset) {
223 dmu_buf_rele(dbuf, FTAG);
224 err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
230 ASSERT3U(offset, >=, dbuf->db_offset);
231 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
233 bparray = dbuf->db_data;
234 bp = &bparray[blkoff];
235 err = func(arg, bp, tx);
239 bpo->bpo_phys->bpo_bytes -=
240 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
241 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
242 if (bpo->bpo_havecomp) {
243 bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
244 bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
246 bpo->bpo_phys->bpo_num_blkptrs--;
247 ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
251 dmu_buf_rele(dbuf, FTAG);
256 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
257 i * sizeof (blkptr_t), -1ULL, tx));
259 if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
262 ASSERT(bpo->bpo_havecomp);
263 err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
265 mutex_exit(&bpo->bpo_lock);
268 epb = doi.doi_data_block_size / sizeof (uint64_t);
270 for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
272 uint64_t offset, blkoff;
274 uint64_t used_before, comp_before, uncomp_before;
275 uint64_t used_after, comp_after, uncomp_after;
277 offset = i * sizeof (uint64_t);
278 blkoff = P2PHASE(i, epb);
280 if (dbuf == NULL || dbuf->db_offset > offset) {
282 dmu_buf_rele(dbuf, FTAG);
283 err = dmu_buf_hold(bpo->bpo_os,
284 bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
289 ASSERT3U(offset, >=, dbuf->db_offset);
290 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
292 objarray = dbuf->db_data;
293 err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
297 err = bpobj_space(&sublist,
298 &used_before, &comp_before, &uncomp_before);
302 err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
304 VERIFY3U(0, ==, bpobj_space(&sublist,
305 &used_after, &comp_after, &uncomp_after));
306 bpo->bpo_phys->bpo_bytes -= used_before - used_after;
307 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
308 bpo->bpo_phys->bpo_comp -= comp_before - comp_after;
309 bpo->bpo_phys->bpo_uncomp -=
310 uncomp_before - uncomp_after;
313 bpobj_close(&sublist);
317 err = dmu_object_free(bpo->bpo_os,
318 objarray[blkoff], tx);
321 bpo->bpo_phys->bpo_num_subobjs--;
322 ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
326 dmu_buf_rele(dbuf, FTAG);
330 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
331 bpo->bpo_phys->bpo_subobjs,
332 (i + 1) * sizeof (uint64_t), -1ULL, tx));
336 /* If there are no entries, there should be no bytes. */
337 ASSERT(bpo->bpo_phys->bpo_num_blkptrs > 0 ||
338 (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs > 0) ||
339 bpo->bpo_phys->bpo_bytes == 0);
341 mutex_exit(&bpo->bpo_lock);
346 * Iterate and remove the entries. If func returns nonzero, iteration
347 * will stop and that entry will not be removed.
350 bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
352 return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
356 * Iterate the entries. If func returns nonzero, iteration will stop.
359 bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
361 return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE));
365 bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
368 uint64_t used, comp, uncomp, subsubobjs;
370 ASSERT(bpo->bpo_havesubobj);
371 ASSERT(bpo->bpo_havecomp);
372 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
374 if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
375 bpobj_decr_empty(bpo->bpo_os, tx);
379 VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
380 VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
383 /* No point in having an empty subobj. */
384 bpobj_close(&subbpo);
385 bpobj_free(bpo->bpo_os, subobj, tx);
389 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
390 if (bpo->bpo_phys->bpo_subobjs == 0) {
391 bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
392 DMU_OT_BPOBJ_SUBOBJ, SPA_MAXBLOCKSIZE, DMU_OT_NONE, 0, tx);
395 dmu_object_info_t doi;
396 ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi));
397 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
399 mutex_enter(&bpo->bpo_lock);
400 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
401 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
402 sizeof (subobj), &subobj, tx);
403 bpo->bpo_phys->bpo_num_subobjs++;
406 * If subobj has only one block of subobjs, then move subobj's
407 * subobjs to bpo's subobj list directly. This reduces
408 * recursion in bpobj_iterate due to nested subobjs.
410 subsubobjs = subbpo.bpo_phys->bpo_subobjs;
411 if (subsubobjs != 0) {
412 dmu_object_info_t doi;
414 VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
415 if (doi.doi_max_offset == doi.doi_data_block_size) {
417 uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
419 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
420 0, FTAG, &subdb, 0));
422 * Make sure that we are not asking dmu_write()
423 * to write more data than we have in our buffer.
425 VERIFY3U(subdb->db_size, >=,
426 numsubsub * sizeof (subobj));
427 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
428 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
429 numsubsub * sizeof (subobj), subdb->db_data, tx);
430 dmu_buf_rele(subdb, FTAG);
431 bpo->bpo_phys->bpo_num_subobjs += numsubsub;
433 dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
434 subbpo.bpo_phys->bpo_subobjs = 0;
435 VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
439 bpo->bpo_phys->bpo_bytes += used;
440 bpo->bpo_phys->bpo_comp += comp;
441 bpo->bpo_phys->bpo_uncomp += uncomp;
442 mutex_exit(&bpo->bpo_lock);
444 bpobj_close(&subbpo);
448 bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
450 blkptr_t stored_bp = *bp;
455 ASSERT(!BP_IS_HOLE(bp));
456 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
458 /* We never need the fill count. */
459 stored_bp.blk_fill = 0;
461 /* The bpobj will compress better if we can leave off the checksum */
462 if (!BP_GET_DEDUP(bp))
463 bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
465 mutex_enter(&bpo->bpo_lock);
467 offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
468 blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
470 if (bpo->bpo_cached_dbuf == NULL ||
471 offset < bpo->bpo_cached_dbuf->db_offset ||
472 offset >= bpo->bpo_cached_dbuf->db_offset +
473 bpo->bpo_cached_dbuf->db_size) {
474 if (bpo->bpo_cached_dbuf)
475 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
476 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
477 offset, bpo, &bpo->bpo_cached_dbuf, 0));
480 dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
481 bparray = bpo->bpo_cached_dbuf->db_data;
482 bparray[blkoff] = stored_bp;
484 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
485 bpo->bpo_phys->bpo_num_blkptrs++;
486 bpo->bpo_phys->bpo_bytes +=
487 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
488 if (bpo->bpo_havecomp) {
489 bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
490 bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
492 mutex_exit(&bpo->bpo_lock);
495 struct space_range_arg {
506 space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
508 struct space_range_arg *sra = arg;
510 if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
511 if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
512 sra->used += bp_get_dsize_sync(sra->spa, bp);
514 sra->used += bp_get_dsize(sra->spa, bp);
515 sra->comp += BP_GET_PSIZE(bp);
516 sra->uncomp += BP_GET_UCSIZE(bp);
522 bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
524 mutex_enter(&bpo->bpo_lock);
526 *usedp = bpo->bpo_phys->bpo_bytes;
527 if (bpo->bpo_havecomp) {
528 *compp = bpo->bpo_phys->bpo_comp;
529 *uncompp = bpo->bpo_phys->bpo_uncomp;
530 mutex_exit(&bpo->bpo_lock);
533 mutex_exit(&bpo->bpo_lock);
534 return (bpobj_space_range(bpo, 0, UINT64_MAX,
535 usedp, compp, uncompp));
540 * Return the amount of space in the bpobj which is:
541 * mintxg < blk_birth <= maxtxg
544 bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
545 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
547 struct space_range_arg sra = { 0 };
551 * As an optimization, if they want the whole txg range, just
552 * get bpo_bytes rather than iterating over the bps.
554 if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
555 return (bpobj_space(bpo, usedp, compp, uncompp));
557 sra.spa = dmu_objset_spa(bpo->bpo_os);
561 err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
564 *uncompp = sra.uncomp;