panic in bpobj_space(): null pointer dereference
[zfs.git] / module / zfs / bpobj.c
blob5f2aff45349f490146b79a12fde1250344347990
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 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>
31 #include <sys/zap.h>
34 * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
36 uint64_t
37 bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
39 spa_t *spa = dmu_objset_spa(os);
40 dsl_pool_t *dp = dmu_objset_pool(os);
42 if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
43 if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
44 ASSERT0(dp->dp_empty_bpobj);
45 dp->dp_empty_bpobj =
46 bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx);
47 VERIFY(zap_add(os,
48 DMU_POOL_DIRECTORY_OBJECT,
49 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
50 &dp->dp_empty_bpobj, tx) == 0);
52 spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx);
53 ASSERT(dp->dp_empty_bpobj != 0);
54 return (dp->dp_empty_bpobj);
55 } else {
56 return (bpobj_alloc(os, blocksize, tx));
60 void
61 bpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
63 dsl_pool_t *dp = dmu_objset_pool(os);
65 spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx);
66 if (!spa_feature_is_active(dmu_objset_spa(os),
67 SPA_FEATURE_EMPTY_BPOBJ)) {
68 VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
69 DMU_POOL_DIRECTORY_OBJECT,
70 DMU_POOL_EMPTY_BPOBJ, tx));
71 VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
72 dp->dp_empty_bpobj = 0;
76 uint64_t
77 bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
79 int size;
81 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
82 size = BPOBJ_SIZE_V0;
83 else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
84 size = BPOBJ_SIZE_V1;
85 else
86 size = sizeof (bpobj_phys_t);
88 return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
89 DMU_OT_BPOBJ_HDR, size, tx));
92 void
93 bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
95 int64_t i;
96 bpobj_t bpo;
97 dmu_object_info_t doi;
98 int epb;
99 dmu_buf_t *dbuf = NULL;
101 ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
102 VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
104 mutex_enter(&bpo.bpo_lock);
106 if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
107 goto out;
109 VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
110 epb = doi.doi_data_block_size / sizeof (uint64_t);
112 for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
113 uint64_t *objarray;
114 uint64_t offset, blkoff;
116 offset = i * sizeof (uint64_t);
117 blkoff = P2PHASE(i, epb);
119 if (dbuf == NULL || dbuf->db_offset > offset) {
120 if (dbuf)
121 dmu_buf_rele(dbuf, FTAG);
122 VERIFY3U(0, ==, dmu_buf_hold(os,
123 bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
126 ASSERT3U(offset, >=, dbuf->db_offset);
127 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
129 objarray = dbuf->db_data;
130 bpobj_free(os, objarray[blkoff], tx);
132 if (dbuf) {
133 dmu_buf_rele(dbuf, FTAG);
134 dbuf = NULL;
136 VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
138 out:
139 mutex_exit(&bpo.bpo_lock);
140 bpobj_close(&bpo);
142 VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
146 bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
148 dmu_object_info_t doi;
149 int err;
151 err = dmu_object_info(os, object, &doi);
152 if (err)
153 return (err);
155 bzero(bpo, sizeof (*bpo));
156 mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
158 ASSERT(bpo->bpo_dbuf == NULL);
159 ASSERT(bpo->bpo_phys == NULL);
160 ASSERT(object != 0);
161 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
162 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
164 err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
165 if (err)
166 return (err);
168 bpo->bpo_os = os;
169 bpo->bpo_object = object;
170 bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
171 bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
172 bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
173 bpo->bpo_phys = bpo->bpo_dbuf->db_data;
174 return (0);
177 void
178 bpobj_close(bpobj_t *bpo)
180 /* Lame workaround for closing a bpobj that was never opened. */
181 if (bpo->bpo_object == 0)
182 return;
184 dmu_buf_rele(bpo->bpo_dbuf, bpo);
185 if (bpo->bpo_cached_dbuf != NULL)
186 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
187 bpo->bpo_dbuf = NULL;
188 bpo->bpo_phys = NULL;
189 bpo->bpo_cached_dbuf = NULL;
190 bpo->bpo_object = 0;
192 mutex_destroy(&bpo->bpo_lock);
195 static boolean_t
196 bpobj_hasentries(bpobj_t *bpo)
198 return (bpo->bpo_phys->bpo_num_blkptrs != 0 ||
199 (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_num_subobjs != 0));
202 static int
203 bpobj_iterate_impl(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx,
204 boolean_t free)
206 dmu_object_info_t doi;
207 int epb;
208 int64_t i;
209 int err = 0;
210 dmu_buf_t *dbuf = NULL;
212 mutex_enter(&bpo->bpo_lock);
214 if (free)
215 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
217 for (i = bpo->bpo_phys->bpo_num_blkptrs - 1; i >= 0; i--) {
218 blkptr_t *bparray;
219 blkptr_t *bp;
220 uint64_t offset, blkoff;
222 offset = i * sizeof (blkptr_t);
223 blkoff = P2PHASE(i, bpo->bpo_epb);
225 if (dbuf == NULL || dbuf->db_offset > offset) {
226 if (dbuf)
227 dmu_buf_rele(dbuf, FTAG);
228 err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object, offset,
229 FTAG, &dbuf, 0);
230 if (err)
231 break;
234 ASSERT3U(offset, >=, dbuf->db_offset);
235 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
237 bparray = dbuf->db_data;
238 bp = &bparray[blkoff];
239 err = func(arg, bp, tx);
240 if (err)
241 break;
242 if (free) {
243 bpo->bpo_phys->bpo_bytes -=
244 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
245 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
246 if (bpo->bpo_havecomp) {
247 bpo->bpo_phys->bpo_comp -= BP_GET_PSIZE(bp);
248 bpo->bpo_phys->bpo_uncomp -= BP_GET_UCSIZE(bp);
250 bpo->bpo_phys->bpo_num_blkptrs--;
251 ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
254 if (dbuf) {
255 dmu_buf_rele(dbuf, FTAG);
256 dbuf = NULL;
258 if (free) {
259 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os, bpo->bpo_object,
260 (i + 1) * sizeof (blkptr_t), -1ULL, tx));
262 if (err || !bpo->bpo_havesubobj || bpo->bpo_phys->bpo_subobjs == 0)
263 goto out;
265 ASSERT(bpo->bpo_havecomp);
266 err = dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi);
267 if (err) {
268 mutex_exit(&bpo->bpo_lock);
269 return (err);
271 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
272 epb = doi.doi_data_block_size / sizeof (uint64_t);
274 for (i = bpo->bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
275 uint64_t *objarray;
276 uint64_t offset, blkoff;
277 bpobj_t sublist;
278 uint64_t used_before, comp_before, uncomp_before;
279 uint64_t used_after, comp_after, uncomp_after;
281 offset = i * sizeof (uint64_t);
282 blkoff = P2PHASE(i, epb);
284 if (dbuf == NULL || dbuf->db_offset > offset) {
285 if (dbuf)
286 dmu_buf_rele(dbuf, FTAG);
287 err = dmu_buf_hold(bpo->bpo_os,
288 bpo->bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0);
289 if (err)
290 break;
293 ASSERT3U(offset, >=, dbuf->db_offset);
294 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
296 objarray = dbuf->db_data;
297 err = bpobj_open(&sublist, bpo->bpo_os, objarray[blkoff]);
298 if (err)
299 break;
300 if (free) {
301 err = bpobj_space(&sublist,
302 &used_before, &comp_before, &uncomp_before);
303 if (err != 0) {
304 bpobj_close(&sublist);
305 break;
308 err = bpobj_iterate_impl(&sublist, func, arg, tx, free);
309 if (free) {
310 VERIFY3U(0, ==, bpobj_space(&sublist,
311 &used_after, &comp_after, &uncomp_after));
312 bpo->bpo_phys->bpo_bytes -= used_before - used_after;
313 ASSERT3S(bpo->bpo_phys->bpo_bytes, >=, 0);
314 bpo->bpo_phys->bpo_comp -= comp_before - comp_after;
315 bpo->bpo_phys->bpo_uncomp -=
316 uncomp_before - uncomp_after;
319 bpobj_close(&sublist);
320 if (err)
321 break;
322 if (free) {
323 err = dmu_object_free(bpo->bpo_os,
324 objarray[blkoff], tx);
325 if (err)
326 break;
327 bpo->bpo_phys->bpo_num_subobjs--;
328 ASSERT3S(bpo->bpo_phys->bpo_num_subobjs, >=, 0);
331 if (dbuf) {
332 dmu_buf_rele(dbuf, FTAG);
333 dbuf = NULL;
335 if (free) {
336 VERIFY3U(0, ==, dmu_free_range(bpo->bpo_os,
337 bpo->bpo_phys->bpo_subobjs,
338 (i + 1) * sizeof (uint64_t), -1ULL, tx));
341 out:
342 /* If there are no entries, there should be no bytes. */
343 if (!bpobj_hasentries(bpo)) {
344 ASSERT0(bpo->bpo_phys->bpo_bytes);
345 ASSERT0(bpo->bpo_phys->bpo_comp);
346 ASSERT0(bpo->bpo_phys->bpo_uncomp);
349 mutex_exit(&bpo->bpo_lock);
350 return (err);
354 * Iterate and remove the entries. If func returns nonzero, iteration
355 * will stop and that entry will not be removed.
358 bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
360 return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE));
364 * Iterate the entries. If func returns nonzero, iteration will stop.
367 bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
369 return (bpobj_iterate_impl(bpo, func, arg, tx, B_FALSE));
372 void
373 bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
375 bpobj_t subbpo;
376 uint64_t used, comp, uncomp, subsubobjs;
377 ASSERTV(dmu_object_info_t doi);
379 ASSERT(bpo->bpo_havesubobj);
380 ASSERT(bpo->bpo_havecomp);
381 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
383 if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
384 bpobj_decr_empty(bpo->bpo_os, tx);
385 return;
388 VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
389 VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
391 if (!bpobj_hasentries(&subbpo)) {
392 /* No point in having an empty subobj. */
393 bpobj_close(&subbpo);
394 bpobj_free(bpo->bpo_os, subobj, tx);
395 return;
398 mutex_enter(&bpo->bpo_lock);
399 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
400 if (bpo->bpo_phys->bpo_subobjs == 0) {
401 bpo->bpo_phys->bpo_subobjs = dmu_object_alloc(bpo->bpo_os,
402 DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
403 DMU_OT_NONE, 0, tx);
406 ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, &doi));
407 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
409 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
410 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
411 sizeof (subobj), &subobj, tx);
412 bpo->bpo_phys->bpo_num_subobjs++;
415 * If subobj has only one block of subobjs, then move subobj's
416 * subobjs to bpo's subobj list directly. This reduces
417 * recursion in bpobj_iterate due to nested subobjs.
419 subsubobjs = subbpo.bpo_phys->bpo_subobjs;
420 if (subsubobjs != 0) {
421 dmu_object_info_t doi;
423 VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
424 if (doi.doi_max_offset == doi.doi_data_block_size) {
425 dmu_buf_t *subdb;
426 uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
428 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, subsubobjs,
429 0, FTAG, &subdb, 0));
431 * Make sure that we are not asking dmu_write()
432 * to write more data than we have in our buffer.
434 VERIFY3U(subdb->db_size, >=,
435 numsubsub * sizeof (subobj));
436 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
437 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
438 numsubsub * sizeof (subobj), subdb->db_data, tx);
439 dmu_buf_rele(subdb, FTAG);
440 bpo->bpo_phys->bpo_num_subobjs += numsubsub;
442 dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
443 subbpo.bpo_phys->bpo_subobjs = 0;
444 VERIFY3U(0, ==, dmu_object_free(bpo->bpo_os,
445 subsubobjs, tx));
448 bpo->bpo_phys->bpo_bytes += used;
449 bpo->bpo_phys->bpo_comp += comp;
450 bpo->bpo_phys->bpo_uncomp += uncomp;
451 mutex_exit(&bpo->bpo_lock);
453 bpobj_close(&subbpo);
456 void
457 bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, dmu_tx_t *tx)
459 blkptr_t stored_bp = *bp;
460 uint64_t offset;
461 int blkoff;
462 blkptr_t *bparray;
464 ASSERT(!BP_IS_HOLE(bp));
465 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
467 if (BP_IS_EMBEDDED(bp)) {
469 * The bpobj will compress better without the payload.
471 * Note that we store EMBEDDED bp's because they have an
472 * uncompressed size, which must be accounted for. An
473 * alternative would be to add their size to bpo_uncomp
474 * without storing the bp, but that would create additional
475 * complications: bpo_uncomp would be inconsistent with the
476 * set of BP's stored, and bpobj_iterate() wouldn't visit
477 * all the space accounted for in the bpobj.
479 bzero(&stored_bp, sizeof (stored_bp));
480 stored_bp.blk_prop = bp->blk_prop;
481 stored_bp.blk_birth = bp->blk_birth;
482 } else if (!BP_GET_DEDUP(bp)) {
483 /* The bpobj will compress better without the checksum */
484 bzero(&stored_bp.blk_cksum, sizeof (stored_bp.blk_cksum));
487 /* We never need the fill count. */
488 stored_bp.blk_fill = 0;
490 mutex_enter(&bpo->bpo_lock);
492 offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
493 blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
495 if (bpo->bpo_cached_dbuf == NULL ||
496 offset < bpo->bpo_cached_dbuf->db_offset ||
497 offset >= bpo->bpo_cached_dbuf->db_offset +
498 bpo->bpo_cached_dbuf->db_size) {
499 if (bpo->bpo_cached_dbuf)
500 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
501 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
502 offset, bpo, &bpo->bpo_cached_dbuf, 0));
505 dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
506 bparray = bpo->bpo_cached_dbuf->db_data;
507 bparray[blkoff] = stored_bp;
509 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
510 bpo->bpo_phys->bpo_num_blkptrs++;
511 bpo->bpo_phys->bpo_bytes +=
512 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
513 if (bpo->bpo_havecomp) {
514 bpo->bpo_phys->bpo_comp += BP_GET_PSIZE(bp);
515 bpo->bpo_phys->bpo_uncomp += BP_GET_UCSIZE(bp);
517 mutex_exit(&bpo->bpo_lock);
520 struct space_range_arg {
521 spa_t *spa;
522 uint64_t mintxg;
523 uint64_t maxtxg;
524 uint64_t used;
525 uint64_t comp;
526 uint64_t uncomp;
529 /* ARGSUSED */
530 static int
531 space_range_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
533 struct space_range_arg *sra = arg;
535 if (bp->blk_birth > sra->mintxg && bp->blk_birth <= sra->maxtxg) {
536 if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
537 sra->used += bp_get_dsize_sync(sra->spa, bp);
538 else
539 sra->used += bp_get_dsize(sra->spa, bp);
540 sra->comp += BP_GET_PSIZE(bp);
541 sra->uncomp += BP_GET_UCSIZE(bp);
543 return (0);
547 bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
549 mutex_enter(&bpo->bpo_lock);
551 *usedp = bpo->bpo_phys->bpo_bytes;
552 if (bpo->bpo_havecomp) {
553 *compp = bpo->bpo_phys->bpo_comp;
554 *uncompp = bpo->bpo_phys->bpo_uncomp;
555 mutex_exit(&bpo->bpo_lock);
556 return (0);
557 } else {
558 mutex_exit(&bpo->bpo_lock);
559 return (bpobj_space_range(bpo, 0, UINT64_MAX,
560 usedp, compp, uncompp));
565 * Return the amount of space in the bpobj which is:
566 * mintxg < blk_birth <= maxtxg
569 bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
570 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
572 struct space_range_arg sra = { 0 };
573 int err;
576 * As an optimization, if they want the whole txg range, just
577 * get bpo_bytes rather than iterating over the bps.
579 if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
580 return (bpobj_space(bpo, usedp, compp, uncompp));
582 sra.spa = dmu_objset_spa(bpo->bpo_os);
583 sra.mintxg = mintxg;
584 sra.maxtxg = maxtxg;
586 err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
587 *usedp = sra.used;
588 *compp = sra.comp;
589 *uncompp = sra.uncomp;
590 return (err);