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) 2012 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 mutex_enter(&bpo->bpo_lock);
396 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
397 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
398 sizeof (subobj), &subobj, tx);
399 bpo->bpo_phys->bpo_num_subobjs++;
402 * If subobj has only one block of subobjs, then move subobj's
403 * subobjs to bpo's subobj list directly. This reduces
404 * recursion in bpobj_iterate due to nested subobjs.
406 subsubobjs = subbpo.bpo_phys->bpo_subobjs;
407 if (subsubobjs != 0) {
408 dmu_object_info_t doi;
410 VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
411 if (doi.doi_max_offset == doi.doi_data_block_size) {
413 uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
415 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
416 0, FTAG, &subdb, 0));
417 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
418 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
419 numsubsub * sizeof (subobj), subdb->db_data, tx);
420 dmu_buf_rele(subdb, FTAG);
421 bpo->bpo_phys->bpo_num_subobjs += numsubsub;
423 dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
424 subbpo.bpo_phys->bpo_subobjs = 0;
425 VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
429 bpo->bpo_phys->bpo_bytes += used;
430 bpo->bpo_phys->bpo_comp += comp;
431 bpo->bpo_phys->bpo_uncomp += uncomp;
432 mutex_exit(&bpo->bpo_lock);
434 bpobj_close(&subbpo);
438 bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
440 blkptr_t stored_bp = *bp;
445 ASSERT(!BP_IS_HOLE(bp));
446 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
448 /* We never need the fill count. */
449 stored_bp.blk_fill = 0;
451 /* The bpobj will compress better if we can leave off the checksum */
452 if (!BP_GET_DEDUP(bp))
453 bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
455 mutex_enter(&bpo->bpo_lock);
457 offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
458 blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
460 if (bpo->bpo_cached_dbuf == NULL ||
461 offset < bpo->bpo_cached_dbuf->db_offset ||
462 offset >= bpo->bpo_cached_dbuf->db_offset +
463 bpo->bpo_cached_dbuf->db_size) {
464 if (bpo->bpo_cached_dbuf)
465 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
466 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
467 offset, bpo, &bpo->bpo_cached_dbuf, 0));
470 dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
471 bparray = bpo->bpo_cached_dbuf->db_data;
472 bparray[blkoff] = stored_bp;
474 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
475 bpo->bpo_phys->bpo_num_blkptrs++;
476 bpo->bpo_phys->bpo_bytes +=
477 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
478 if (bpo->bpo_havecomp) {
479 bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
480 bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
482 mutex_exit(&bpo->bpo_lock);
485 struct space_range_arg {
496 space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
498 struct space_range_arg *sra = arg;
500 if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
501 if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
502 sra->used += bp_get_dsize_sync(sra->spa, bp);
504 sra->used += bp_get_dsize(sra->spa, bp);
505 sra->comp += BP_GET_PSIZE(bp);
506 sra->uncomp += BP_GET_UCSIZE(bp);
512 bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
514 mutex_enter(&bpo->bpo_lock);
516 *usedp = bpo->bpo_phys->bpo_bytes;
517 if (bpo->bpo_havecomp) {
518 *compp = bpo->bpo_phys->bpo_comp;
519 *uncompp = bpo->bpo_phys->bpo_uncomp;
520 mutex_exit(&bpo->bpo_lock);
523 mutex_exit(&bpo->bpo_lock);
524 return (bpobj_space_range(bpo, 0, UINT64_MAX,
525 usedp, compp, uncompp));
530 * Return the amount of space in the bpobj which is:
531 * mintxg < blk_birth <= maxtxg
534 bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
535 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
537 struct space_range_arg sra = { 0 };
541 * As an optimization, if they want the whole txg range, just
542 * get bpo_bytes rather than iterating over the bps.
544 if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
545 return (bpobj_space(bpo, usedp, compp, uncompp));
547 sra.spa = dmu_objset_spa(bpo->bpo_os);
551 err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
554 *uncompp = sra.uncomp;