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 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/bplist.h>
29 #include <sys/zfs_context.h>
32 bplist_hold(bplist_t *bpl)
34 ASSERT(MUTEX_HELD(&bpl->bpl_lock));
35 if (bpl->bpl_dbuf == NULL) {
36 int err = dmu_bonus_hold(bpl->bpl_mos,
37 bpl->bpl_object, bpl, &bpl->bpl_dbuf);
40 bpl->bpl_phys = bpl->bpl_dbuf->db_data;
46 bplist_create(objset_t *mos, int blocksize, dmu_tx_t *tx)
50 size = spa_version(dmu_objset_spa(mos)) < ZFS_VERSION_BPLIST_ACCOUNT ?
51 BPLIST_SIZE_V0 : sizeof (bplist_phys_t);
53 return (dmu_object_alloc(mos, DMU_OT_BPLIST, blocksize,
54 DMU_OT_BPLIST_HDR, size, tx));
58 bplist_destroy(objset_t *mos, uint64_t object, dmu_tx_t *tx)
60 VERIFY(dmu_object_free(mos, object, tx) == 0);
64 bplist_open(bplist_t *bpl, objset_t *mos, uint64_t object)
66 dmu_object_info_t doi;
69 err = dmu_object_info(mos, object, &doi);
73 mutex_enter(&bpl->bpl_lock);
75 ASSERT(bpl->bpl_dbuf == NULL);
76 ASSERT(bpl->bpl_phys == NULL);
77 ASSERT(bpl->bpl_cached_dbuf == NULL);
78 ASSERT(bpl->bpl_queue == NULL);
80 ASSERT3U(doi.doi_type, ==, DMU_OT_BPLIST);
81 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPLIST_HDR);
84 bpl->bpl_object = object;
85 bpl->bpl_blockshift = highbit(doi.doi_data_block_size - 1);
86 bpl->bpl_bpshift = bpl->bpl_blockshift - SPA_BLKPTRSHIFT;
87 bpl->bpl_havecomp = (doi.doi_bonus_size == sizeof (bplist_phys_t));
89 mutex_exit(&bpl->bpl_lock);
94 bplist_close(bplist_t *bpl)
96 mutex_enter(&bpl->bpl_lock);
98 ASSERT(bpl->bpl_queue == NULL);
100 if (bpl->bpl_cached_dbuf) {
101 dmu_buf_rele(bpl->bpl_cached_dbuf, bpl);
102 bpl->bpl_cached_dbuf = NULL;
105 dmu_buf_rele(bpl->bpl_dbuf, bpl);
106 bpl->bpl_dbuf = NULL;
107 bpl->bpl_phys = NULL;
110 mutex_exit(&bpl->bpl_lock);
114 bplist_empty(bplist_t *bpl)
118 if (bpl->bpl_object == 0)
121 mutex_enter(&bpl->bpl_lock);
122 VERIFY(0 == bplist_hold(bpl)); /* XXX */
123 rv = (bpl->bpl_phys->bpl_entries == 0);
124 mutex_exit(&bpl->bpl_lock);
130 bplist_cache(bplist_t *bpl, uint64_t blkid)
134 if (bpl->bpl_cached_dbuf == NULL ||
135 bpl->bpl_cached_dbuf->db_offset != (blkid << bpl->bpl_blockshift)) {
136 if (bpl->bpl_cached_dbuf != NULL)
137 dmu_buf_rele(bpl->bpl_cached_dbuf, bpl);
138 err = dmu_buf_hold(bpl->bpl_mos,
139 bpl->bpl_object, blkid << bpl->bpl_blockshift,
140 bpl, &bpl->bpl_cached_dbuf);
141 ASSERT(err || bpl->bpl_cached_dbuf->db_size ==
142 1ULL << bpl->bpl_blockshift);
148 bplist_iterate(bplist_t *bpl, uint64_t *itorp, blkptr_t *bp)
154 mutex_enter(&bpl->bpl_lock);
156 err = bplist_hold(bpl);
158 mutex_exit(&bpl->bpl_lock);
162 if (*itorp >= bpl->bpl_phys->bpl_entries) {
163 mutex_exit(&bpl->bpl_lock);
167 blk = *itorp >> bpl->bpl_bpshift;
168 off = P2PHASE(*itorp, 1ULL << bpl->bpl_bpshift);
170 err = bplist_cache(bpl, blk);
172 mutex_exit(&bpl->bpl_lock);
176 bparray = bpl->bpl_cached_dbuf->db_data;
179 mutex_exit(&bpl->bpl_lock);
184 bplist_enqueue(bplist_t *bpl, blkptr_t *bp, dmu_tx_t *tx)
190 ASSERT(!BP_IS_HOLE(bp));
191 mutex_enter(&bpl->bpl_lock);
192 err = bplist_hold(bpl);
196 blk = bpl->bpl_phys->bpl_entries >> bpl->bpl_bpshift;
197 off = P2PHASE(bpl->bpl_phys->bpl_entries, 1ULL << bpl->bpl_bpshift);
199 err = bplist_cache(bpl, blk);
201 mutex_exit(&bpl->bpl_lock);
205 dmu_buf_will_dirty(bpl->bpl_cached_dbuf, tx);
206 bparray = bpl->bpl_cached_dbuf->db_data;
209 /* We never need the fill count. */
210 bparray[off].blk_fill = 0;
212 /* The bplist will compress better if we can leave off the checksum */
213 bzero(&bparray[off].blk_cksum, sizeof (bparray[off].blk_cksum));
215 dmu_buf_will_dirty(bpl->bpl_dbuf, tx);
216 bpl->bpl_phys->bpl_entries++;
217 bpl->bpl_phys->bpl_bytes +=
218 bp_get_dasize(dmu_objset_spa(bpl->bpl_mos), bp);
219 if (bpl->bpl_havecomp) {
220 bpl->bpl_phys->bpl_comp += BP_GET_PSIZE(bp);
221 bpl->bpl_phys->bpl_uncomp += BP_GET_UCSIZE(bp);
223 mutex_exit(&bpl->bpl_lock);
229 * Deferred entry; will be written later by bplist_sync().
232 bplist_enqueue_deferred(bplist_t *bpl, blkptr_t *bp)
234 bplist_q_t *bpq = kmem_alloc(sizeof (*bpq), KM_SLEEP);
236 ASSERT(!BP_IS_HOLE(bp));
237 mutex_enter(&bpl->bpl_lock);
239 bpq->bpq_next = bpl->bpl_queue;
240 bpl->bpl_queue = bpq;
241 mutex_exit(&bpl->bpl_lock);
245 bplist_sync(bplist_t *bpl, dmu_tx_t *tx)
249 mutex_enter(&bpl->bpl_lock);
250 while ((bpq = bpl->bpl_queue) != NULL) {
251 bpl->bpl_queue = bpq->bpq_next;
252 mutex_exit(&bpl->bpl_lock);
253 VERIFY(0 == bplist_enqueue(bpl, &bpq->bpq_blk, tx));
254 kmem_free(bpq, sizeof (*bpq));
255 mutex_enter(&bpl->bpl_lock);
257 mutex_exit(&bpl->bpl_lock);
261 bplist_vacate(bplist_t *bpl, dmu_tx_t *tx)
263 mutex_enter(&bpl->bpl_lock);
264 ASSERT3P(bpl->bpl_queue, ==, NULL);
265 VERIFY(0 == bplist_hold(bpl));
266 dmu_buf_will_dirty(bpl->bpl_dbuf, tx);
267 VERIFY(0 == dmu_free_range(bpl->bpl_mos,
268 bpl->bpl_object, 0, -1ULL, tx));
269 bpl->bpl_phys->bpl_entries = 0;
270 bpl->bpl_phys->bpl_bytes = 0;
271 if (bpl->bpl_havecomp) {
272 bpl->bpl_phys->bpl_comp = 0;
273 bpl->bpl_phys->bpl_uncomp = 0;
275 mutex_exit(&bpl->bpl_lock);
279 bplist_space(bplist_t *bpl, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
281 uint64_t itor = 0, comp = 0, uncomp = 0;
285 mutex_enter(&bpl->bpl_lock);
287 err = bplist_hold(bpl);
289 mutex_exit(&bpl->bpl_lock);
293 *usedp = bpl->bpl_phys->bpl_bytes;
294 if (bpl->bpl_havecomp) {
295 *compp = bpl->bpl_phys->bpl_comp;
296 *uncompp = bpl->bpl_phys->bpl_uncomp;
298 mutex_exit(&bpl->bpl_lock);
300 if (!bpl->bpl_havecomp) {
301 while ((err = bplist_iterate(bpl, &itor, &bp)) == 0) {
302 comp += BP_GET_PSIZE(&bp);
303 uncomp += BP_GET_UCSIZE(&bp);