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.
25 #include <sys/bpobj.h>
26 #include <sys/zfs_context.h>
27 #include <sys/refcount.h>
30 bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
34 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
36 else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
39 size = sizeof (bpobj_phys_t);
41 return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
42 DMU_OT_BPOBJ_HDR, size, tx));
46 bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
50 dmu_object_info_t doi;
52 dmu_buf_t *dbuf = NULL;
54 VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
56 mutex_enter(&bpo.bpo_lock);
58 if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
61 VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
62 epb = doi.doi_data_block_size / sizeof (uint64_t);
64 for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
66 uint64_t offset, blkoff;
68 offset = i * sizeof (uint64_t);
69 blkoff = P2PHASE(i, epb);
71 if (dbuf == NULL || dbuf->db_offset > offset) {
73 dmu_buf_rele(dbuf, FTAG);
74 VERIFY3U(0, ==, dmu_buf_hold(os,
75 bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
78 ASSERT3U(offset, >=, dbuf->db_offset);
79 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
81 objarray = dbuf->db_data;
82 bpobj_free(os, objarray[blkoff], tx);
85 dmu_buf_rele(dbuf, FTAG);
88 VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
91 mutex_exit(&bpo.bpo_lock);
94 VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
98 bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
100 dmu_object_info_t doi;
103 err = dmu_object_info(os, object, &doi);
107 bzero(bpo, sizeof (*bpo));
108 mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
110 ASSERT(bpo->bpo_dbuf == NULL);
111 ASSERT(bpo->bpo_phys == NULL);
113 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
114 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
117 bpo->bpo_object = object;
118 bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
119 bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
120 bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
122 err = dmu_bonus_hold(bpo->bpo_os,
123 bpo->bpo_object, bpo, &bpo->bpo_dbuf);
126 bpo->bpo_phys = bpo->bpo_dbuf->db_data;
131 bpobj_close(bpobj_t *bpo)
133 /* Lame workaround for closing a bpobj that was never opened. */
134 if (bpo->bpo_object == 0)
137 dmu_buf_rele(bpo->bpo_dbuf, bpo);
138 if (bpo->bpo_cached_dbuf != NULL)
139 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
140 bpo->bpo_dbuf = NULL;
141 bpo->bpo_phys = NULL;
142 bpo->bpo_cached_dbuf = NULL;
144 mutex_destroy(&bpo->bpo_lock);
148 bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
151 dmu_object_info_t doi;
155 dmu_buf_t *dbuf = NULL;
157 mutex_enter(&bpo->bpo_lock);
160 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
162 for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
165 uint64_t offset, blkoff;
167 offset = i * sizeof (blkptr_t);
168 blkoff = P2PHASE(i, bpo->bpo_epb);
170 if (dbuf == NULL || dbuf->db_offset > offset) {
172 dmu_buf_rele(dbuf, FTAG);
173 err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
179 ASSERT3U(offset, >=, dbuf->db_offset);
180 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
182 bparray = dbuf->db_data;
183 bp = &bparray[blkoff];
184 err = func(arg, bp, tx);
188 bpo->bpo_phys->bpo_bytes -=
189 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
190 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
191 if (bpo->bpo_havecomp) {
192 bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
193 bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
195 bpo->bpo_phys->bpo_num_blkptrs--;
196 ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
200 dmu_buf_rele(dbuf, FTAG);
205 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
206 i * sizeof (blkptr_t), -1ULL, tx));
208 if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
211 ASSERT(bpo->bpo_havecomp);
212 err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
215 epb = doi.doi_data_block_size / sizeof (uint64_t);
217 for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
219 uint64_t offset, blkoff;
221 uint64_t used_before, comp_before, uncomp_before;
222 uint64_t used_after, comp_after, uncomp_after;
224 offset = i * sizeof (uint64_t);
225 blkoff = P2PHASE(i, epb);
227 if (dbuf == NULL || dbuf->db_offset > offset) {
229 dmu_buf_rele(dbuf, FTAG);
230 err = dmu_buf_hold(bpo->bpo_os,
231 bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
236 ASSERT3U(offset, >=, dbuf->db_offset);
237 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
239 objarray = dbuf->db_data;
240 err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
244 err = bpobj_space(&sublist,
245 &used_before, &comp_before, &uncomp_before);
249 err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
251 VERIFY3U(0, ==, bpobj_space(&sublist,
252 &used_after, &comp_after, &uncomp_after));
253 bpo->bpo_phys->bpo_bytes -= used_before - used_after;
254 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
255 bpo->bpo_phys->bpo_comp -= comp_before - used_after;
256 bpo->bpo_phys->bpo_uncomp -=
257 uncomp_before - uncomp_after;
260 bpobj_close(&sublist);
264 err = dmu_object_free(bpo->bpo_os,
265 objarray[blkoff], tx);
268 bpo->bpo_phys->bpo_num_subobjs--;
269 ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
273 dmu_buf_rele(dbuf, FTAG);
277 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
278 bpo->bpo_phys->bpo_subobjs,
279 (i + 1) * sizeof (uint64_t), -1ULL, tx));
283 /* If there are no entries, there should be no bytes. */
284 ASSERT(bpo->bpo_phys->bpo_num_blkptrs > 0 ||
285 (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs > 0) ||
286 bpo->bpo_phys->bpo_bytes == 0);
288 mutex_exit(&bpo->bpo_lock);
293 * Iterate and remove the entries. If func returns nonzero, iteration
294 * will stop and that entry will not be removed.
297 bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
299 return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
303 * Iterate the entries. If func returns nonzero, iteration will stop.
306 bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
308 return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE));
312 bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
315 uint64_t used, comp, uncomp;
317 ASSERT(bpo->bpo_havesubobj);
318 ASSERT(bpo->bpo_havecomp);
320 VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
321 VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
322 bpobj_close(&subbpo);
325 /* No point in having an empty subobj. */
326 bpobj_free(bpo->bpo_os, subobj, tx);
330 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
331 if (bpo->bpo_phys->bpo_subobjs == 0) {
332 bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
333 DMU_OT_BPOBJ_SUBOBJ, SPA_MAXBLOCKSIZE, DMU_OT_NONE, 0, tx);
336 mutex_enter(&bpo->bpo_lock);
337 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
338 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
339 sizeof (subobj), &subobj, tx);
340 bpo->bpo_phys->bpo_num_subobjs++;
341 bpo->bpo_phys->bpo_bytes += used;
342 bpo->bpo_phys->bpo_comp += comp;
343 bpo->bpo_phys->bpo_uncomp += uncomp;
344 mutex_exit(&bpo->bpo_lock);
348 bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
350 blkptr_t stored_bp = *bp;
355 ASSERT(!BP_IS_HOLE(bp));
357 /* We never need the fill count. */
358 stored_bp.blk_fill = 0;
360 /* The bpobj will compress better if we can leave off the checksum */
361 if (!BP_GET_DEDUP(bp))
362 bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
364 mutex_enter(&bpo->bpo_lock);
366 offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
367 blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
369 if (bpo->bpo_cached_dbuf == NULL ||
370 offset < bpo->bpo_cached_dbuf->db_offset ||
371 offset >= bpo->bpo_cached_dbuf->db_offset +
372 bpo->bpo_cached_dbuf->db_size) {
373 if (bpo->bpo_cached_dbuf)
374 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
375 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
376 offset, bpo, &bpo->bpo_cached_dbuf, 0));
379 dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
380 bparray = bpo->bpo_cached_dbuf->db_data;
381 bparray[blkoff] = stored_bp;
383 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
384 bpo->bpo_phys->bpo_num_blkptrs++;
385 bpo->bpo_phys->bpo_bytes +=
386 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
387 if (bpo->bpo_havecomp) {
388 bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
389 bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
391 mutex_exit(&bpo->bpo_lock);
394 struct space_range_arg {
405 space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
407 struct space_range_arg *sra = arg;
409 if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
410 sra->used += bp_get_dsize_sync(sra->spa, bp);
411 sra->comp += BP_GET_PSIZE(bp);
412 sra->uncomp += BP_GET_UCSIZE(bp);
418 bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
420 mutex_enter(&bpo->bpo_lock);
422 *usedp = bpo->bpo_phys->bpo_bytes;
423 if (bpo->bpo_havecomp) {
424 *compp = bpo->bpo_phys->bpo_comp;
425 *uncompp = bpo->bpo_phys->bpo_uncomp;
426 mutex_exit(&bpo->bpo_lock);
429 mutex_exit(&bpo->bpo_lock);
430 return (bpobj_space_range(bpo, 0, UINT64_MAX,
431 usedp, compp, uncompp));
436 * Return the amount of space in the bpobj which is:
437 * mintxg < blk_birth <= maxtxg
440 bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
441 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
443 struct space_range_arg sra = { 0 };
447 * As an optimization, if they want the whole txg range, just
448 * get bpo_bytes rather than iterating over the bps.
450 if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
451 return (bpobj_space(bpo, usedp, compp, uncompp));
453 sra.spa = dmu_objset_spa(bpo->bpo_os);
457 err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
460 *uncompp = sra.uncomp;