1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
15 * Copyright (c) 2017 Datto Inc.
16 */
17
18 #include <sys/bpobj.h>
19 #include <sys/zfs_context.h>
20 #include <sys/zfs_refcount.h>
21 #include <sys/dsl_pool.h>
22 #include <sys/zfeature.h>
23 #include <sys/zap.h>
24
25 /*
26 * Return an empty bpobj, preferably the empty dummy one (dp_empty_bpobj).
27 */
28 uint64_t
bpobj_alloc_empty(objset_t * os,int blocksize,dmu_tx_t * tx)29 bpobj_alloc_empty(objset_t *os, int blocksize, dmu_tx_t *tx)
30 {
31 spa_t *spa = dmu_objset_spa(os);
32 dsl_pool_t *dp = dmu_objset_pool(os);
33
34 if (spa_feature_is_enabled(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
35 if (!spa_feature_is_active(spa, SPA_FEATURE_EMPTY_BPOBJ)) {
36 ASSERT0(dp->dp_empty_bpobj);
37 dp->dp_empty_bpobj =
38 bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx);
39 VERIFY(zap_add(os,
40 DMU_POOL_DIRECTORY_OBJECT,
41 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
42 &dp->dp_empty_bpobj, tx) == 0);
43 }
44 spa_feature_incr(spa, SPA_FEATURE_EMPTY_BPOBJ, tx);
45 ASSERT(dp->dp_empty_bpobj != 0);
46 return (dp->dp_empty_bpobj);
47 } else {
48 return (bpobj_alloc(os, blocksize, tx));
49 }
50 }
51
52 void
bpobj_decr_empty(objset_t * os,dmu_tx_t * tx)53 bpobj_decr_empty(objset_t *os, dmu_tx_t *tx)
54 {
55 dsl_pool_t *dp = dmu_objset_pool(os);
56
57 spa_feature_decr(dmu_objset_spa(os), SPA_FEATURE_EMPTY_BPOBJ, tx);
58 if (!spa_feature_is_active(dmu_objset_spa(os),
59 SPA_FEATURE_EMPTY_BPOBJ)) {
60 VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
61 DMU_POOL_DIRECTORY_OBJECT,
62 DMU_POOL_EMPTY_BPOBJ, tx));
63 VERIFY3U(0, ==, dmu_object_free(os, dp->dp_empty_bpobj, tx));
64 dp->dp_empty_bpobj = 0;
65 }
66 }
67
68 uint64_t
bpobj_alloc(objset_t * os,int blocksize,dmu_tx_t * tx)69 bpobj_alloc(objset_t *os, int blocksize, dmu_tx_t *tx)
70 {
71 int size;
72
73 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_BPOBJ_ACCOUNT)
74 size = BPOBJ_SIZE_V0;
75 else if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
76 size = BPOBJ_SIZE_V1;
77 else if (!spa_feature_is_active(dmu_objset_spa(os),
78 SPA_FEATURE_LIVELIST))
79 size = BPOBJ_SIZE_V2;
80 else
81 size = sizeof (bpobj_phys_t);
82
83 return (dmu_object_alloc(os, DMU_OT_BPOBJ, blocksize,
84 DMU_OT_BPOBJ_HDR, size, tx));
85 }
86
87 void
bpobj_free(objset_t * os,uint64_t obj,dmu_tx_t * tx)88 bpobj_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
89 {
90 int64_t i;
91 bpobj_t bpo;
92 dmu_object_info_t doi;
93 int epb;
94 dmu_buf_t *dbuf = NULL;
95
96 ASSERT(obj != dmu_objset_pool(os)->dp_empty_bpobj);
97 VERIFY3U(0, ==, bpobj_open(&bpo, os, obj));
98
99 mutex_enter(&bpo.bpo_lock);
100
101 if (!bpo.bpo_havesubobj || bpo.bpo_phys->bpo_subobjs == 0)
102 goto out;
103
104 VERIFY3U(0, ==, dmu_object_info(os, bpo.bpo_phys->bpo_subobjs, &doi));
105 epb = doi.doi_data_block_size / sizeof (uint64_t);
106
107 for (i = bpo.bpo_phys->bpo_num_subobjs - 1; i >= 0; i--) {
108 uint64_t *objarray;
109 uint64_t offset, blkoff;
110
111 offset = i * sizeof (uint64_t);
112 blkoff = P2PHASE(i, epb);
113
114 if (dbuf == NULL || dbuf->db_offset > offset) {
115 if (dbuf)
116 dmu_buf_rele(dbuf, FTAG);
117 VERIFY3U(0, ==, dmu_buf_hold(os,
118 bpo.bpo_phys->bpo_subobjs, offset, FTAG, &dbuf, 0));
119 }
120
121 ASSERT3U(offset, >=, dbuf->db_offset);
122 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
123
124 objarray = dbuf->db_data;
125 bpobj_free(os, objarray[blkoff], tx);
126 }
127 if (dbuf) {
128 dmu_buf_rele(dbuf, FTAG);
129 dbuf = NULL;
130 }
131 VERIFY3U(0, ==, dmu_object_free(os, bpo.bpo_phys->bpo_subobjs, tx));
132
133 out:
134 mutex_exit(&bpo.bpo_lock);
135 bpobj_close(&bpo);
136
137 VERIFY3U(0, ==, dmu_object_free(os, obj, tx));
138 }
139
140 int
bpobj_open(bpobj_t * bpo,objset_t * os,uint64_t object)141 bpobj_open(bpobj_t *bpo, objset_t *os, uint64_t object)
142 {
143 dmu_object_info_t doi;
144 int err;
145
146 err = dmu_object_info(os, object, &doi);
147 if (err)
148 return (err);
149
150 memset(bpo, 0, sizeof (*bpo));
151 mutex_init(&bpo->bpo_lock, NULL, MUTEX_DEFAULT, NULL);
152
153 ASSERT0P(bpo->bpo_dbuf);
154 ASSERT0P(bpo->bpo_phys);
155 ASSERT(object != 0);
156 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ);
157 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_BPOBJ_HDR);
158
159 err = dmu_bonus_hold(os, object, bpo, &bpo->bpo_dbuf);
160 if (err)
161 return (err);
162
163 bpo->bpo_os = os;
164 bpo->bpo_object = object;
165 bpo->bpo_epb = doi.doi_data_block_size >> SPA_BLKPTRSHIFT;
166 bpo->bpo_havecomp = (doi.doi_bonus_size > BPOBJ_SIZE_V0);
167 bpo->bpo_havesubobj = (doi.doi_bonus_size > BPOBJ_SIZE_V1);
168 bpo->bpo_havefreed = (doi.doi_bonus_size > BPOBJ_SIZE_V2);
169 bpo->bpo_phys = bpo->bpo_dbuf->db_data;
170 return (0);
171 }
172
173 boolean_t
bpobj_is_open(const bpobj_t * bpo)174 bpobj_is_open(const bpobj_t *bpo)
175 {
176 return (bpo->bpo_object != 0);
177 }
178
179 void
bpobj_close(bpobj_t * bpo)180 bpobj_close(bpobj_t *bpo)
181 {
182 /* Lame workaround for closing a bpobj that was never opened. */
183 if (bpo->bpo_object == 0)
184 return;
185
186 dmu_buf_rele(bpo->bpo_dbuf, bpo);
187 if (bpo->bpo_cached_dbuf != NULL)
188 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
189 bpo->bpo_dbuf = NULL;
190 bpo->bpo_phys = NULL;
191 bpo->bpo_cached_dbuf = NULL;
192 bpo->bpo_object = 0;
193
194 mutex_destroy(&bpo->bpo_lock);
195 }
196
197 static boolean_t
bpobj_is_empty_impl(bpobj_t * bpo)198 bpobj_is_empty_impl(bpobj_t *bpo)
199 {
200 ASSERT(MUTEX_HELD(&bpo->bpo_lock));
201 return (bpo->bpo_phys->bpo_num_blkptrs == 0 &&
202 (!bpo->bpo_havesubobj || bpo->bpo_phys->bpo_num_subobjs == 0));
203 }
204
205 boolean_t
bpobj_is_empty(bpobj_t * bpo)206 bpobj_is_empty(bpobj_t *bpo)
207 {
208 mutex_enter(&bpo->bpo_lock);
209 boolean_t is_empty = bpobj_is_empty_impl(bpo);
210 mutex_exit(&bpo->bpo_lock);
211 return (is_empty);
212 }
213
214 /*
215 * A recursive iteration of the bpobjs would be nice here but we run the risk
216 * of overflowing function stack space. Instead, find each subobj and add it
217 * to the head of our list so it can be scanned for subjobjs. Like a
218 * recursive implementation, the "deepest" subobjs will be freed first.
219 * When a subobj is found to have no additional subojs, free it.
220 */
221 typedef struct bpobj_info {
222 bpobj_t *bpi_bpo;
223 /*
224 * This object is a subobj of bpi_parent,
225 * at bpi_index in its subobj array.
226 */
227 struct bpobj_info *bpi_parent;
228 uint64_t bpi_index;
229 /* How many of our subobj's are left to process. */
230 uint64_t bpi_unprocessed_subobjs;
231 /* True after having visited this bpo's directly referenced BPs. */
232 boolean_t bpi_visited;
233 list_node_t bpi_node;
234 } bpobj_info_t;
235
236 static bpobj_info_t *
bpi_alloc(bpobj_t * bpo,bpobj_info_t * parent,uint64_t index)237 bpi_alloc(bpobj_t *bpo, bpobj_info_t *parent, uint64_t index)
238 {
239 bpobj_info_t *bpi = kmem_zalloc(sizeof (bpobj_info_t), KM_SLEEP);
240 bpi->bpi_bpo = bpo;
241 bpi->bpi_parent = parent;
242 bpi->bpi_index = index;
243 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
244 bpi->bpi_unprocessed_subobjs = bpo->bpo_phys->bpo_num_subobjs;
245 }
246 return (bpi);
247 }
248
249 /*
250 * Update bpobj and all of its parents with new space accounting.
251 */
252 static void
propagate_space_reduction(bpobj_info_t * bpi,int64_t freed,int64_t comp_freed,int64_t uncomp_freed,dmu_tx_t * tx)253 propagate_space_reduction(bpobj_info_t *bpi, int64_t freed,
254 int64_t comp_freed, int64_t uncomp_freed, dmu_tx_t *tx)
255 {
256
257 for (; bpi != NULL; bpi = bpi->bpi_parent) {
258 bpobj_t *p = bpi->bpi_bpo;
259 ASSERT(dmu_buf_is_dirty(p->bpo_dbuf, tx));
260 p->bpo_phys->bpo_bytes -= freed;
261 ASSERT3S(p->bpo_phys->bpo_bytes, >=, 0);
262 if (p->bpo_havecomp) {
263 p->bpo_phys->bpo_comp -= comp_freed;
264 p->bpo_phys->bpo_uncomp -= uncomp_freed;
265 }
266 }
267 }
268
269 static int
bpobj_iterate_blkptrs(bpobj_info_t * bpi,bpobj_itor_t func,void * arg,int64_t start,dmu_tx_t * tx,boolean_t free)270 bpobj_iterate_blkptrs(bpobj_info_t *bpi, bpobj_itor_t func, void *arg,
271 int64_t start, dmu_tx_t *tx, boolean_t free)
272 {
273 int err = 0;
274 int64_t freed = 0, comp_freed = 0, uncomp_freed = 0;
275 dmu_buf_t *dbuf = NULL;
276 bpobj_t *bpo = bpi->bpi_bpo;
277
278 int64_t i = bpo->bpo_phys->bpo_num_blkptrs - 1;
279 uint64_t pe = P2ALIGN_TYPED(i, bpo->bpo_epb, uint64_t) *
280 sizeof (blkptr_t);
281 uint64_t ps = start * sizeof (blkptr_t);
282 uint64_t pb = MAX((pe > dmu_prefetch_max) ? pe - dmu_prefetch_max : 0,
283 ps);
284 if (pe > pb) {
285 dmu_prefetch(bpo->bpo_os, bpo->bpo_object, 0, pb, pe - pb,
286 ZIO_PRIORITY_ASYNC_READ);
287 }
288 for (; i >= start; i--) {
289 uint64_t offset = i * sizeof (blkptr_t);
290 uint64_t blkoff = P2PHASE(i, bpo->bpo_epb);
291
292 if (dbuf == NULL || dbuf->db_offset > offset) {
293 if (dbuf)
294 dmu_buf_rele(dbuf, FTAG);
295 err = dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
296 offset, FTAG, &dbuf, DMU_READ_NO_PREFETCH);
297 if (err)
298 break;
299 pe = pb;
300 pb = MAX((dbuf->db_offset > dmu_prefetch_max) ?
301 dbuf->db_offset - dmu_prefetch_max : 0, ps);
302 if (pe > pb) {
303 dmu_prefetch(bpo->bpo_os, bpo->bpo_object, 0,
304 pb, pe - pb, ZIO_PRIORITY_ASYNC_READ);
305 }
306 }
307
308 ASSERT3U(offset, >=, dbuf->db_offset);
309 ASSERT3U(offset, <, dbuf->db_offset + dbuf->db_size);
310
311 blkptr_t *bparray = dbuf->db_data;
312 blkptr_t *bp = &bparray[blkoff];
313
314 boolean_t bp_freed = BP_GET_FREE(bp);
315 err = func(arg, bp, bp_freed, tx);
316 if (err)
317 break;
318
319 if (free) {
320 int sign = bp_freed ? -1 : +1;
321 spa_t *spa = dmu_objset_spa(bpo->bpo_os);
322 freed += sign * bp_get_dsize_sync(spa, bp);
323 comp_freed += sign * BP_GET_PSIZE(bp);
324 uncomp_freed += sign * BP_GET_UCSIZE(bp);
325 ASSERT(dmu_buf_is_dirty(bpo->bpo_dbuf, tx));
326 bpo->bpo_phys->bpo_num_blkptrs--;
327 ASSERT3S(bpo->bpo_phys->bpo_num_blkptrs, >=, 0);
328 if (bp_freed) {
329 ASSERT(bpo->bpo_havefreed);
330 bpo->bpo_phys->bpo_num_freed--;
331 ASSERT3S(bpo->bpo_phys->bpo_num_freed, >=, 0);
332 }
333 }
334 }
335 if (free) {
336 propagate_space_reduction(bpi, freed, comp_freed,
337 uncomp_freed, tx);
338 VERIFY0(dmu_free_range(bpo->bpo_os,
339 bpo->bpo_object,
340 bpo->bpo_phys->bpo_num_blkptrs * sizeof (blkptr_t),
341 DMU_OBJECT_END, tx));
342 }
343 if (dbuf) {
344 dmu_buf_rele(dbuf, FTAG);
345 dbuf = NULL;
346 }
347 return (err);
348 }
349
350 /*
351 * Given an initial bpo, start by freeing the BPs that are directly referenced
352 * by that bpo. If the bpo has subobjs, read in its last subobj and push the
353 * subobj to our stack. By popping items off our stack, eventually we will
354 * encounter a bpo that has no subobjs. We can free its bpobj_info_t, and if
355 * requested also free the now-empty bpo from disk and decrement
356 * its parent's subobj count. We continue popping each subobj from our stack,
357 * visiting its last subobj until they too have no more subobjs, and so on.
358 */
359 static int
bpobj_iterate_impl(bpobj_t * initial_bpo,bpobj_itor_t func,void * arg,dmu_tx_t * tx,boolean_t free,uint64_t * bpobj_size)360 bpobj_iterate_impl(bpobj_t *initial_bpo, bpobj_itor_t func, void *arg,
361 dmu_tx_t *tx, boolean_t free, uint64_t *bpobj_size)
362 {
363 list_t stack;
364 bpobj_info_t *bpi;
365 int err = 0;
366
367 /*
368 * Create a "stack" for us to work with without worrying about
369 * stack overflows. Initialize it with the initial_bpo.
370 */
371 list_create(&stack, sizeof (bpobj_info_t),
372 offsetof(bpobj_info_t, bpi_node));
373 mutex_enter(&initial_bpo->bpo_lock);
374
375 if (bpobj_size != NULL)
376 *bpobj_size = initial_bpo->bpo_phys->bpo_num_blkptrs;
377
378 list_insert_head(&stack, bpi_alloc(initial_bpo, NULL, 0));
379
380 while ((bpi = list_head(&stack)) != NULL) {
381 bpobj_t *bpo = bpi->bpi_bpo;
382
383 ASSERT3P(bpo, !=, NULL);
384 ASSERT(MUTEX_HELD(&bpo->bpo_lock));
385 ASSERT(bpobj_is_open(bpo));
386
387 if (free)
388 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
389
390 if (bpi->bpi_visited == B_FALSE) {
391 err = bpobj_iterate_blkptrs(bpi, func, arg, 0, tx,
392 free);
393 bpi->bpi_visited = B_TRUE;
394 if (err != 0)
395 break;
396 }
397 /*
398 * We've finished with this bpo's directly-referenced BP's and
399 * it has no more unprocessed subobjs. We can free its
400 * bpobj_info_t (unless it is the topmost, initial_bpo).
401 * If we are freeing from disk, we can also do that.
402 */
403 if (bpi->bpi_unprocessed_subobjs == 0) {
404 /*
405 * If there are no entries, there should
406 * be no bytes.
407 */
408 if (bpobj_is_empty_impl(bpo)) {
409 ASSERT0(bpo->bpo_phys->bpo_bytes);
410 ASSERT0(bpo->bpo_phys->bpo_comp);
411 ASSERT0(bpo->bpo_phys->bpo_uncomp);
412 }
413
414 /* The initial_bpo has no parent and is not closed. */
415 if (bpi->bpi_parent != NULL) {
416 if (free) {
417 bpobj_t *p = bpi->bpi_parent->bpi_bpo;
418
419 ASSERT0(bpo->bpo_phys->bpo_num_blkptrs);
420 ASSERT3U(p->bpo_phys->bpo_num_subobjs,
421 >, 0);
422 ASSERT3U(bpi->bpi_index, ==,
423 p->bpo_phys->bpo_num_subobjs - 1);
424 ASSERT(dmu_buf_is_dirty(bpo->bpo_dbuf,
425 tx));
426
427 p->bpo_phys->bpo_num_subobjs--;
428
429 VERIFY0(dmu_free_range(p->bpo_os,
430 p->bpo_phys->bpo_subobjs,
431 bpi->bpi_index * sizeof (uint64_t),
432 sizeof (uint64_t), tx));
433
434 /* eliminate the empty subobj list */
435 if (bpo->bpo_havesubobj &&
436 bpo->bpo_phys->bpo_subobjs != 0) {
437 ASSERT0(bpo->bpo_phys->
438 bpo_num_subobjs);
439 err = dmu_object_free(
440 bpo->bpo_os,
441 bpo->bpo_phys->bpo_subobjs,
442 tx);
443 if (err)
444 break;
445 bpo->bpo_phys->bpo_subobjs = 0;
446 }
447 err = dmu_object_free(p->bpo_os,
448 bpo->bpo_object, tx);
449 if (err)
450 break;
451 }
452
453 mutex_exit(&bpo->bpo_lock);
454 bpobj_close(bpo);
455 kmem_free(bpo, sizeof (bpobj_t));
456 } else {
457 mutex_exit(&bpo->bpo_lock);
458 }
459
460 /*
461 * Finished processing this bpo. Unlock, and free
462 * our "stack" info.
463 */
464 list_remove_head(&stack);
465 kmem_free(bpi, sizeof (bpobj_info_t));
466 } else {
467 /*
468 * We have unprocessed subobjs. Process the next one.
469 */
470 ASSERT(bpo->bpo_havecomp);
471 ASSERT0P(bpobj_size);
472
473 /* Add the last subobj to stack. */
474 int64_t i = bpi->bpi_unprocessed_subobjs - 1;
475 uint64_t offset = i * sizeof (uint64_t);
476
477 uint64_t subobj;
478 err = dmu_read(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
479 offset, sizeof (uint64_t), &subobj,
480 DMU_READ_NO_PREFETCH);
481 if (err)
482 break;
483
484 bpobj_t *subbpo = kmem_alloc(sizeof (bpobj_t),
485 KM_SLEEP);
486 err = bpobj_open(subbpo, bpo->bpo_os, subobj);
487 if (err) {
488 kmem_free(subbpo, sizeof (bpobj_t));
489 break;
490 }
491
492 if (subbpo->bpo_havesubobj &&
493 subbpo->bpo_phys->bpo_subobjs != 0) {
494 dmu_prefetch(subbpo->bpo_os,
495 subbpo->bpo_phys->bpo_subobjs, 0, 0, 0,
496 ZIO_PRIORITY_ASYNC_READ);
497 }
498
499 list_insert_head(&stack, bpi_alloc(subbpo, bpi, i));
500 mutex_enter(&subbpo->bpo_lock);
501 bpi->bpi_unprocessed_subobjs--;
502 }
503 }
504 /*
505 * Cleanup anything left on the "stack" after we left the loop.
506 * Every bpo on the stack is locked so we must remember to undo
507 * that now (in LIFO order).
508 */
509 while ((bpi = list_remove_head(&stack)) != NULL) {
510 bpobj_t *bpo = bpi->bpi_bpo;
511 ASSERT(err != 0);
512 ASSERT3P(bpo, !=, NULL);
513
514 mutex_exit(&bpo->bpo_lock);
515
516 /* do not free the initial_bpo */
517 if (bpi->bpi_parent != NULL) {
518 bpobj_close(bpi->bpi_bpo);
519 kmem_free(bpi->bpi_bpo, sizeof (bpobj_t));
520 }
521 kmem_free(bpi, sizeof (bpobj_info_t));
522 }
523
524 list_destroy(&stack);
525
526 return (err);
527 }
528
529 /*
530 * Iterate and remove the entries. If func returns nonzero, iteration
531 * will stop and that entry will not be removed.
532 */
533 int
bpobj_iterate(bpobj_t * bpo,bpobj_itor_t func,void * arg,dmu_tx_t * tx)534 bpobj_iterate(bpobj_t *bpo, bpobj_itor_t func, void *arg, dmu_tx_t *tx)
535 {
536 return (bpobj_iterate_impl(bpo, func, arg, tx, B_TRUE, NULL));
537 }
538
539 /*
540 * Iterate the entries. If func returns nonzero, iteration will stop.
541 *
542 * If there are no subobjs:
543 *
544 * *bpobj_size can be used to return the number of block pointers in the
545 * bpobj. Note that this may be different from the number of block pointers
546 * that are iterated over, if iteration is terminated early (e.g. by the func
547 * returning nonzero).
548 *
549 * If there are concurrent (or subsequent) modifications to the bpobj then the
550 * returned *bpobj_size can be passed as "start" to
551 * livelist_bpobj_iterate_from_nofree() to iterate the newly added entries.
552 */
553 int
bpobj_iterate_nofree(bpobj_t * bpo,bpobj_itor_t func,void * arg,uint64_t * bpobj_size)554 bpobj_iterate_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg,
555 uint64_t *bpobj_size)
556 {
557 return (bpobj_iterate_impl(bpo, func, arg, NULL, B_FALSE, bpobj_size));
558 }
559
560 /*
561 * Iterate over the blkptrs in the bpobj beginning at index start. If func
562 * returns nonzero, iteration will stop. This is a livelist specific function
563 * since it assumes that there are no subobjs present.
564 */
565 int
livelist_bpobj_iterate_from_nofree(bpobj_t * bpo,bpobj_itor_t func,void * arg,int64_t start)566 livelist_bpobj_iterate_from_nofree(bpobj_t *bpo, bpobj_itor_t func, void *arg,
567 int64_t start)
568 {
569 if (bpo->bpo_havesubobj)
570 VERIFY0(bpo->bpo_phys->bpo_subobjs);
571 bpobj_info_t *bpi = bpi_alloc(bpo, NULL, 0);
572 int err = bpobj_iterate_blkptrs(bpi, func, arg, start, NULL, B_FALSE);
573 kmem_free(bpi, sizeof (bpobj_info_t));
574 return (err);
575 }
576
577 /*
578 * Logically add subobj's contents to the parent bpobj.
579 *
580 * In the most general case, this is accomplished in constant time by adding
581 * a reference to subobj. This case is used when enqueuing a large subobj:
582 * +--------------+ +--------------+
583 * | bpobj |----------------------->| subobj list |
584 * +----+----+----+----+----+ +-----+-----+--+--+
585 * | bp | bp | bp | bp | bp | | obj | obj | obj |
586 * +----+----+----+----+----+ +-----+-----+-----+
587 *
588 * +--------------+ +--------------+
589 * | sub-bpobj |----------------------> | subsubobj |
590 * +----+----+----+----+---------+----+ +-----+-----+--+--------+-----+
591 * | bp | bp | bp | bp | ... | bp | | obj | obj | ... | obj |
592 * +----+----+----+----+---------+----+ +-----+-----+-----------+-----+
593 *
594 * Result: sub-bpobj added to parent's subobj list.
595 * +--------------+ +--------------+
596 * | bpobj |----------------------->| subobj list |
597 * +----+----+----+----+----+ +-----+-----+--+--+-----+
598 * | bp | bp | bp | bp | bp | | obj | obj | obj | OBJ |
599 * +----+----+----+----+----+ +-----+-----+-----+--|--+
600 * |
601 * /-----------------------------------------------------/
602 * v
603 * +--------------+ +--------------+
604 * | sub-bpobj |----------------------> | subsubobj |
605 * +----+----+----+----+---------+----+ +-----+-----+--+--------+-----+
606 * | bp | bp | bp | bp | ... | bp | | obj | obj | ... | obj |
607 * +----+----+----+----+---------+----+ +-----+-----+-----------+-----+
608 *
609 *
610 * In a common case, the subobj is small: its bp's and its list of subobj's
611 * are each stored in a single block. In this case we copy the subobj's
612 * contents to the parent:
613 * +--------------+ +--------------+
614 * | bpobj |----------------------->| subobj list |
615 * +----+----+----+----+----+ +-----+-----+--+--+
616 * | bp | bp | bp | bp | bp | | obj | obj | obj |
617 * +----+----+----+----+----+ +-----+-----+-----+
618 * ^ ^
619 * +--------------+ | +--------------+ |
620 * | sub-bpobj |---------^------------> | subsubobj | ^
621 * +----+----+----+ | +-----+-----+--+ |
622 * | BP | BP |-->-->-->-->-/ | OBJ | OBJ |-->-/
623 * +----+----+ +-----+-----+
624 *
625 * Result: subobj destroyed, contents copied to parent:
626 * +--------------+ +--------------+
627 * | bpobj |----------------------->| subobj list |
628 * +----+----+----+----+----+----+----+ +-----+-----+--+--+-----+-----+
629 * | bp | bp | bp | bp | bp | BP | BP | | obj | obj | obj | OBJ | OBJ |
630 * +----+----+----+----+----+----+----+ +-----+-----+-----+-----+-----+
631 *
632 *
633 * If the subobj has many BP's but few subobj's, we can copy the sub-subobj's
634 * but retain the sub-bpobj:
635 * +--------------+ +--------------+
636 * | bpobj |----------------------->| subobj list |
637 * +----+----+----+----+----+ +-----+-----+--+--+
638 * | bp | bp | bp | bp | bp | | obj | obj | obj |
639 * +----+----+----+----+----+ +-----+-----+-----+
640 * ^
641 * +--------------+ +--------------+ |
642 * | sub-bpobj |----------------------> | subsubobj | ^
643 * +----+----+----+----+---------+----+ +-----+-----+--+ |
644 * | bp | bp | bp | bp | ... | bp | | OBJ | OBJ |-->-/
645 * +----+----+----+----+---------+----+ +-----+-----+
646 *
647 * Result: sub-sub-bpobjs and subobj added to parent's subobj list.
648 * +--------------+ +--------------+
649 * | bpobj |-------------------->| subobj list |
650 * +----+----+----+----+----+ +-----+-----+--+--+-----+-----+------+
651 * | bp | bp | bp | bp | bp | | obj | obj | obj | OBJ | OBJ | OBJ* |
652 * +----+----+----+----+----+ +-----+-----+-----+-----+-----+--|---+
653 * |
654 * /--------------------------------------------------------------/
655 * v
656 * +--------------+
657 * | sub-bpobj |
658 * +----+----+----+----+---------+----+
659 * | bp | bp | bp | bp | ... | bp |
660 * +----+----+----+----+---------+----+
661 */
662 void
bpobj_enqueue_subobj(bpobj_t * bpo,uint64_t subobj,dmu_tx_t * tx)663 bpobj_enqueue_subobj(bpobj_t *bpo, uint64_t subobj, dmu_tx_t *tx)
664 {
665 bpobj_t subbpo;
666 uint64_t used, comp, uncomp, subsubobjs;
667 boolean_t copy_subsub = B_TRUE;
668 boolean_t copy_bps = B_TRUE;
669
670 ASSERT(bpobj_is_open(bpo));
671 ASSERT(subobj != 0);
672 ASSERT(bpo->bpo_havesubobj);
673 ASSERT(bpo->bpo_havecomp);
674 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
675
676 if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj) {
677 bpobj_decr_empty(bpo->bpo_os, tx);
678 return;
679 }
680
681 VERIFY3U(0, ==, bpobj_open(&subbpo, bpo->bpo_os, subobj));
682 if (bpobj_is_empty(&subbpo)) {
683 /* No point in having an empty subobj. */
684 bpobj_close(&subbpo);
685 bpobj_free(bpo->bpo_os, subobj, tx);
686 return;
687 }
688 VERIFY3U(0, ==, bpobj_space(&subbpo, &used, &comp, &uncomp));
689
690 mutex_enter(&bpo->bpo_lock);
691 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
692
693 dmu_object_info_t doi;
694
695 if (bpo->bpo_phys->bpo_subobjs != 0) {
696 ASSERT0(dmu_object_info(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
697 &doi));
698 ASSERT3U(doi.doi_type, ==, DMU_OT_BPOBJ_SUBOBJ);
699 }
700
701 /*
702 * If subobj has only one block of subobjs, then move subobj's
703 * subobjs to bpo's subobj list directly. This reduces recursion in
704 * bpobj_iterate due to nested subobjs.
705 */
706 subsubobjs = subbpo.bpo_phys->bpo_subobjs;
707 if (subsubobjs != 0) {
708 VERIFY0(dmu_object_info(bpo->bpo_os, subsubobjs, &doi));
709 if (doi.doi_max_offset > doi.doi_data_block_size) {
710 copy_subsub = B_FALSE;
711 }
712 }
713
714 /*
715 * If, in addition to having only one block of subobj's, subobj has
716 * only one block of bp's, then move subobj's bp's to bpo's bp list
717 * directly. This reduces recursion in bpobj_iterate due to nested
718 * subobjs.
719 */
720 VERIFY3U(0, ==, dmu_object_info(bpo->bpo_os, subobj, &doi));
721 if (doi.doi_max_offset > doi.doi_data_block_size || !copy_subsub) {
722 copy_bps = B_FALSE;
723 }
724
725 if (copy_subsub && subsubobjs != 0) {
726 dmu_buf_t *subdb;
727 uint64_t numsubsub = subbpo.bpo_phys->bpo_num_subobjs;
728
729 VERIFY0(dmu_buf_hold(bpo->bpo_os, subsubobjs,
730 0, FTAG, &subdb, 0));
731 /*
732 * Make sure that we are not asking dmu_write()
733 * to write more data than we have in our buffer.
734 */
735 VERIFY3U(subdb->db_size, >=,
736 numsubsub * sizeof (subobj));
737 if (bpo->bpo_phys->bpo_subobjs == 0) {
738 bpo->bpo_phys->bpo_subobjs =
739 dmu_object_alloc(bpo->bpo_os,
740 DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
741 DMU_OT_NONE, 0, tx);
742 }
743 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
744 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
745 numsubsub * sizeof (subobj), subdb->db_data, tx,
746 DMU_READ_NO_PREFETCH);
747 dmu_buf_rele(subdb, FTAG);
748 bpo->bpo_phys->bpo_num_subobjs += numsubsub;
749
750 dmu_buf_will_dirty(subbpo.bpo_dbuf, tx);
751 subbpo.bpo_phys->bpo_subobjs = 0;
752 VERIFY0(dmu_object_free(bpo->bpo_os, subsubobjs, tx));
753 }
754
755 if (copy_bps) {
756 dmu_buf_t *bps;
757 uint64_t numbps = subbpo.bpo_phys->bpo_num_blkptrs;
758
759 ASSERT(copy_subsub);
760 VERIFY0(dmu_buf_hold(bpo->bpo_os, subobj,
761 0, FTAG, &bps, 0));
762
763 /*
764 * Make sure that we are not asking dmu_write()
765 * to write more data than we have in our buffer.
766 */
767 VERIFY3U(bps->db_size, >=, numbps * sizeof (blkptr_t));
768 dmu_write(bpo->bpo_os, bpo->bpo_object,
769 bpo->bpo_phys->bpo_num_blkptrs * sizeof (blkptr_t),
770 numbps * sizeof (blkptr_t),
771 bps->db_data, tx, DMU_READ_NO_PREFETCH);
772 dmu_buf_rele(bps, FTAG);
773 bpo->bpo_phys->bpo_num_blkptrs += numbps;
774
775 bpobj_close(&subbpo);
776 VERIFY0(dmu_object_free(bpo->bpo_os, subobj, tx));
777 } else {
778 bpobj_close(&subbpo);
779 if (bpo->bpo_phys->bpo_subobjs == 0) {
780 bpo->bpo_phys->bpo_subobjs =
781 dmu_object_alloc(bpo->bpo_os,
782 DMU_OT_BPOBJ_SUBOBJ, SPA_OLD_MAXBLOCKSIZE,
783 DMU_OT_NONE, 0, tx);
784 }
785
786 dmu_write(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs,
787 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj),
788 sizeof (subobj), &subobj, tx, DMU_READ_NO_PREFETCH);
789 bpo->bpo_phys->bpo_num_subobjs++;
790 }
791
792 bpo->bpo_phys->bpo_bytes += used;
793 bpo->bpo_phys->bpo_comp += comp;
794 bpo->bpo_phys->bpo_uncomp += uncomp;
795 mutex_exit(&bpo->bpo_lock);
796
797 }
798
799 /*
800 * Prefetch metadata required for bpobj_enqueue_subobj().
801 */
802 void
bpobj_prefetch_subobj(bpobj_t * bpo,uint64_t subobj)803 bpobj_prefetch_subobj(bpobj_t *bpo, uint64_t subobj)
804 {
805 dmu_object_info_t doi;
806 bpobj_t subbpo;
807 uint64_t subsubobjs;
808 boolean_t copy_subsub = B_TRUE;
809 boolean_t copy_bps = B_TRUE;
810
811 ASSERT(bpobj_is_open(bpo));
812 ASSERT(subobj != 0);
813
814 if (subobj == dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj)
815 return;
816
817 if (bpobj_open(&subbpo, bpo->bpo_os, subobj) != 0)
818 return;
819 if (bpobj_is_empty(&subbpo)) {
820 bpobj_close(&subbpo);
821 return;
822 }
823 subsubobjs = subbpo.bpo_phys->bpo_subobjs;
824 bpobj_close(&subbpo);
825
826 if (subsubobjs != 0) {
827 if (dmu_object_info(bpo->bpo_os, subsubobjs, &doi) != 0)
828 return;
829 if (doi.doi_max_offset > doi.doi_data_block_size)
830 copy_subsub = B_FALSE;
831 }
832
833 if (dmu_object_info(bpo->bpo_os, subobj, &doi) != 0)
834 return;
835 if (doi.doi_max_offset > doi.doi_data_block_size || !copy_subsub)
836 copy_bps = B_FALSE;
837
838 if (copy_subsub && subsubobjs != 0) {
839 if (bpo->bpo_phys->bpo_subobjs) {
840 dmu_prefetch(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 0,
841 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 1,
842 ZIO_PRIORITY_ASYNC_READ);
843 }
844 dmu_prefetch(bpo->bpo_os, subsubobjs, 0, 0, 1,
845 ZIO_PRIORITY_ASYNC_READ);
846 }
847
848 if (copy_bps) {
849 dmu_prefetch(bpo->bpo_os, bpo->bpo_object, 0,
850 bpo->bpo_phys->bpo_num_blkptrs * sizeof (blkptr_t), 1,
851 ZIO_PRIORITY_ASYNC_READ);
852 dmu_prefetch(bpo->bpo_os, subobj, 0, 0, 1,
853 ZIO_PRIORITY_ASYNC_READ);
854 } else if (bpo->bpo_phys->bpo_subobjs) {
855 dmu_prefetch(bpo->bpo_os, bpo->bpo_phys->bpo_subobjs, 0,
856 bpo->bpo_phys->bpo_num_subobjs * sizeof (subobj), 1,
857 ZIO_PRIORITY_ASYNC_READ);
858 }
859 }
860
861 void
bpobj_enqueue(bpobj_t * bpo,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)862 bpobj_enqueue(bpobj_t *bpo, const blkptr_t *bp, boolean_t bp_freed,
863 dmu_tx_t *tx)
864 {
865 blkptr_t stored_bp = *bp;
866 uint64_t offset;
867 int blkoff;
868 blkptr_t *bparray;
869
870 ASSERT(bpobj_is_open(bpo));
871 ASSERT(!BP_IS_HOLE(bp));
872 ASSERT(bpo->bpo_object != dmu_objset_pool(bpo->bpo_os)->dp_empty_bpobj);
873
874 if (BP_IS_EMBEDDED(bp)) {
875 /*
876 * The bpobj will compress better without the payload.
877 *
878 * Note that we store EMBEDDED bp's because they have an
879 * uncompressed size, which must be accounted for. An
880 * alternative would be to add their size to bpo_uncomp
881 * without storing the bp, but that would create additional
882 * complications: bpo_uncomp would be inconsistent with the
883 * set of BP's stored, and bpobj_iterate() wouldn't visit
884 * all the space accounted for in the bpobj.
885 */
886 memset(&stored_bp, 0, sizeof (stored_bp));
887 stored_bp.blk_prop = bp->blk_prop;
888 BP_SET_LOGICAL_BIRTH(&stored_bp, BP_GET_LOGICAL_BIRTH(bp));
889 } else if (!BP_GET_DEDUP(bp)) {
890 /* The bpobj will compress better without the checksum */
891 memset(&stored_bp.blk_cksum, 0, sizeof (stored_bp.blk_cksum));
892 }
893
894 stored_bp.blk_fill = 0;
895 BP_SET_FREE(&stored_bp, bp_freed);
896
897 mutex_enter(&bpo->bpo_lock);
898
899 offset = bpo->bpo_phys->bpo_num_blkptrs * sizeof (stored_bp);
900 blkoff = P2PHASE(bpo->bpo_phys->bpo_num_blkptrs, bpo->bpo_epb);
901
902 if (bpo->bpo_cached_dbuf == NULL ||
903 offset < bpo->bpo_cached_dbuf->db_offset ||
904 offset >= bpo->bpo_cached_dbuf->db_offset +
905 bpo->bpo_cached_dbuf->db_size) {
906 if (bpo->bpo_cached_dbuf)
907 dmu_buf_rele(bpo->bpo_cached_dbuf, bpo);
908 VERIFY3U(0, ==, dmu_buf_hold(bpo->bpo_os, bpo->bpo_object,
909 offset, bpo, &bpo->bpo_cached_dbuf, 0));
910 ASSERT3P(bpo->bpo_cached_dbuf, !=, NULL);
911 }
912
913 dmu_buf_will_dirty(bpo->bpo_cached_dbuf, tx);
914 bparray = bpo->bpo_cached_dbuf->db_data;
915 bparray[blkoff] = stored_bp;
916
917 dmu_buf_will_dirty(bpo->bpo_dbuf, tx);
918 bpo->bpo_phys->bpo_num_blkptrs++;
919 int sign = bp_freed ? -1 : +1;
920 bpo->bpo_phys->bpo_bytes += sign *
921 bp_get_dsize_sync(dmu_objset_spa(bpo->bpo_os), bp);
922 if (bpo->bpo_havecomp) {
923 bpo->bpo_phys->bpo_comp += sign * BP_GET_PSIZE(bp);
924 bpo->bpo_phys->bpo_uncomp += sign * BP_GET_UCSIZE(bp);
925 }
926 if (bp_freed) {
927 ASSERT(bpo->bpo_havefreed);
928 bpo->bpo_phys->bpo_num_freed++;
929 }
930 mutex_exit(&bpo->bpo_lock);
931 }
932
933 struct space_range_arg {
934 spa_t *spa;
935 uint64_t mintxg;
936 uint64_t maxtxg;
937 uint64_t used;
938 uint64_t comp;
939 uint64_t uncomp;
940 };
941
942 static int
space_range_cb(void * arg,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)943 space_range_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx)
944 {
945 (void) bp_freed, (void) tx;
946 struct space_range_arg *sra = arg;
947
948 if (BP_GET_BIRTH(bp) > sra->mintxg &&
949 BP_GET_BIRTH(bp) <= sra->maxtxg) {
950 if (dsl_pool_sync_context(spa_get_dsl(sra->spa)))
951 sra->used += bp_get_dsize_sync(sra->spa, bp);
952 else
953 sra->used += bp_get_dsize(sra->spa, bp);
954 sra->comp += BP_GET_PSIZE(bp);
955 sra->uncomp += BP_GET_UCSIZE(bp);
956 }
957 return (0);
958 }
959
960 int
bpobj_space(bpobj_t * bpo,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)961 bpobj_space(bpobj_t *bpo, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
962 {
963 ASSERT(bpobj_is_open(bpo));
964 mutex_enter(&bpo->bpo_lock);
965
966 *usedp = bpo->bpo_phys->bpo_bytes;
967 if (bpo->bpo_havecomp) {
968 *compp = bpo->bpo_phys->bpo_comp;
969 *uncompp = bpo->bpo_phys->bpo_uncomp;
970 mutex_exit(&bpo->bpo_lock);
971 return (0);
972 } else {
973 mutex_exit(&bpo->bpo_lock);
974 return (bpobj_space_range(bpo, 0, UINT64_MAX,
975 usedp, compp, uncompp));
976 }
977 }
978
979 /*
980 * Return the amount of space in the bpobj which is:
981 * mintxg < logical birth <= maxtxg
982 */
983 int
bpobj_space_range(bpobj_t * bpo,uint64_t mintxg,uint64_t maxtxg,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)984 bpobj_space_range(bpobj_t *bpo, uint64_t mintxg, uint64_t maxtxg,
985 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
986 {
987 struct space_range_arg sra = { 0 };
988 int err;
989
990 ASSERT(bpobj_is_open(bpo));
991
992 /*
993 * As an optimization, if they want the whole txg range, just
994 * get bpo_bytes rather than iterating over the bps.
995 */
996 if (mintxg < TXG_INITIAL && maxtxg == UINT64_MAX && bpo->bpo_havecomp)
997 return (bpobj_space(bpo, usedp, compp, uncompp));
998
999 sra.spa = dmu_objset_spa(bpo->bpo_os);
1000 sra.mintxg = mintxg;
1001 sra.maxtxg = maxtxg;
1002
1003 err = bpobj_iterate_nofree(bpo, space_range_cb, &sra, NULL);
1004 *usedp = sra.used;
1005 *compp = sra.comp;
1006 *uncompp = sra.uncomp;
1007 return (err);
1008 }
1009
1010 /*
1011 * A bpobj_itor_t to append blkptrs to a bplist. Note that while blkptrs in a
1012 * bpobj are designated as free or allocated that information is not preserved
1013 * in bplists.
1014 */
1015 int
bplist_append_cb(void * arg,const blkptr_t * bp,boolean_t bp_freed,dmu_tx_t * tx)1016 bplist_append_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
1017 dmu_tx_t *tx)
1018 {
1019 (void) bp_freed, (void) tx;
1020 bplist_t *bpl = arg;
1021 bplist_append(bpl, bp);
1022 return (0);
1023 }
1024