]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dmu_traverse.c
vdev_disk: reorganise vdev_disk_io_start
[mirror_zfs.git] / module / zfs / dmu_traverse.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24 */
25
26 #include <sys/zfs_context.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_traverse.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dnode.h>
33 #include <sys/spa.h>
34 #include <sys/spa_impl.h>
35 #include <sys/zio.h>
36 #include <sys/dmu_impl.h>
37 #include <sys/sa.h>
38 #include <sys/sa_impl.h>
39 #include <sys/callb.h>
40 #include <sys/zfeature.h>
41
42 static int32_t zfs_pd_bytes_max = 50 * 1024 * 1024; /* 50MB */
43 static int32_t send_holes_without_birth_time = 1;
44 static uint_t zfs_traverse_indirect_prefetch_limit = 32;
45
46 typedef struct prefetch_data {
47 kmutex_t pd_mtx;
48 kcondvar_t pd_cv;
49 int32_t pd_bytes_fetched;
50 int pd_flags;
51 boolean_t pd_cancel;
52 boolean_t pd_exited;
53 zbookmark_phys_t pd_resume;
54 } prefetch_data_t;
55
56 typedef struct traverse_data {
57 spa_t *td_spa;
58 uint64_t td_objset;
59 blkptr_t *td_rootbp;
60 uint64_t td_min_txg;
61 zbookmark_phys_t *td_resume;
62 int td_flags;
63 prefetch_data_t *td_pfd;
64 boolean_t td_paused;
65 uint64_t td_hole_birth_enabled_txg;
66 blkptr_cb_t *td_func;
67 void *td_arg;
68 boolean_t td_realloc_possible;
69 } traverse_data_t;
70
71 static int traverse_dnode(traverse_data_t *td, const blkptr_t *bp,
72 const dnode_phys_t *dnp, uint64_t objset, uint64_t object);
73 static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
74 uint64_t objset, uint64_t object);
75
76 static int
77 traverse_zil_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
78 uint64_t claim_txg)
79 {
80 traverse_data_t *td = arg;
81 zbookmark_phys_t zb;
82
83 if (BP_IS_HOLE(bp))
84 return (0);
85
86 if (claim_txg == 0 &&
87 BP_GET_LOGICAL_BIRTH(bp) >= spa_min_claim_txg(td->td_spa))
88 return (-1);
89
90 SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
91 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
92
93 (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
94
95 return (0);
96 }
97
98 static int
99 traverse_zil_record(zilog_t *zilog, const lr_t *lrc, void *arg,
100 uint64_t claim_txg)
101 {
102 traverse_data_t *td = arg;
103
104 if (lrc->lrc_txtype == TX_WRITE) {
105 lr_write_t *lr = (lr_write_t *)lrc;
106 blkptr_t *bp = &lr->lr_blkptr;
107 zbookmark_phys_t zb;
108
109 if (BP_IS_HOLE(bp))
110 return (0);
111
112 if (claim_txg == 0 || BP_GET_LOGICAL_BIRTH(bp) < claim_txg)
113 return (0);
114
115 ASSERT3U(BP_GET_LSIZE(bp), !=, 0);
116 SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
117 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
118
119 (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
120 td->td_arg);
121 }
122 return (0);
123 }
124
125 static void
126 traverse_zil(traverse_data_t *td, zil_header_t *zh)
127 {
128 uint64_t claim_txg = zh->zh_claim_txg;
129
130 /*
131 * We only want to visit blocks that have been claimed but not yet
132 * replayed; plus blocks that are already stable in read-only mode.
133 */
134 if (claim_txg == 0 && spa_writeable(td->td_spa))
135 return;
136
137 zilog_t *zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
138 (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
139 claim_txg, !(td->td_flags & TRAVERSE_NO_DECRYPT));
140 zil_free(zilog);
141 }
142
143 typedef enum resume_skip {
144 RESUME_SKIP_ALL,
145 RESUME_SKIP_NONE,
146 RESUME_SKIP_CHILDREN
147 } resume_skip_t;
148
149 /*
150 * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
151 * the block indicated by zb does not need to be visited at all. Returns
152 * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
153 * resume point. This indicates that this block should be visited but not its
154 * children (since they must have been visited in a previous traversal).
155 * Otherwise returns RESUME_SKIP_NONE.
156 */
157 static resume_skip_t
158 resume_skip_check(const traverse_data_t *td, const dnode_phys_t *dnp,
159 const zbookmark_phys_t *zb)
160 {
161 if (td->td_resume != NULL) {
162 /*
163 * If we already visited this bp & everything below,
164 * don't bother doing it again.
165 */
166 if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
167 return (RESUME_SKIP_ALL);
168
169 if (memcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
170 if (td->td_flags & TRAVERSE_POST)
171 return (RESUME_SKIP_CHILDREN);
172 }
173 }
174 return (RESUME_SKIP_NONE);
175 }
176
177 /*
178 * Returns B_TRUE, if prefetch read is issued, otherwise B_FALSE.
179 */
180 static boolean_t
181 traverse_prefetch_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
182 const blkptr_t *bp, const zbookmark_phys_t *zb)
183 {
184 arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
185 ARC_FLAG_PRESCIENT_PREFETCH;
186 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
187
188 if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
189 return (B_FALSE);
190 /*
191 * If this bp is before the resume point, it may have already been
192 * freed.
193 */
194 if (resume_skip_check(td, dnp, zb) != RESUME_SKIP_NONE)
195 return (B_FALSE);
196 if (BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) <= td->td_min_txg)
197 return (B_FALSE);
198 if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
199 return (B_FALSE);
200 ASSERT(!BP_IS_REDACTED(bp));
201
202 if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
203 zio_flags |= ZIO_FLAG_RAW;
204
205 (void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
206 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
207 return (B_TRUE);
208 }
209
210 static boolean_t
211 prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
212 {
213 ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
214 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
215 BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG || BP_IS_REDACTED(bp))
216 return (B_FALSE);
217 return (B_TRUE);
218 }
219
220 static int
221 traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
222 const blkptr_t *bp, const zbookmark_phys_t *zb)
223 {
224 int err = 0;
225 arc_buf_t *buf = NULL;
226 prefetch_data_t *pd = td->td_pfd;
227
228 switch (resume_skip_check(td, dnp, zb)) {
229 case RESUME_SKIP_ALL:
230 return (0);
231 case RESUME_SKIP_CHILDREN:
232 goto post;
233 case RESUME_SKIP_NONE:
234 break;
235 default:
236 ASSERT(0);
237 }
238
239 if (BP_GET_LOGICAL_BIRTH(bp) == 0) {
240 /*
241 * Since this block has a birth time of 0 it must be one of
242 * two things: a hole created before the
243 * SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole
244 * which has always been a hole in an object.
245 *
246 * If a file is written sparsely, then the unwritten parts of
247 * the file were "always holes" -- that is, they have been
248 * holes since this object was allocated. However, we (and
249 * our callers) can not necessarily tell when an object was
250 * allocated. Therefore, if it's possible that this object
251 * was freed and then its object number reused, we need to
252 * visit all the holes with birth==0.
253 *
254 * If it isn't possible that the object number was reused,
255 * then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote
256 * all the blocks we will visit as part of this traversal,
257 * then this hole must have always existed, so we can skip
258 * it. We visit blocks born after (exclusive) td_min_txg.
259 *
260 * Note that the meta-dnode cannot be reallocated.
261 */
262 if (!send_holes_without_birth_time &&
263 (!td->td_realloc_possible ||
264 zb->zb_object == DMU_META_DNODE_OBJECT) &&
265 td->td_hole_birth_enabled_txg <= td->td_min_txg)
266 return (0);
267 } else if (BP_GET_LOGICAL_BIRTH(bp) <= td->td_min_txg) {
268 return (0);
269 }
270
271 if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
272 uint64_t size = BP_GET_LSIZE(bp);
273 mutex_enter(&pd->pd_mtx);
274 ASSERT(pd->pd_bytes_fetched >= 0);
275 while (pd->pd_bytes_fetched < size && !pd->pd_exited)
276 cv_wait_sig(&pd->pd_cv, &pd->pd_mtx);
277 pd->pd_bytes_fetched -= size;
278 cv_broadcast(&pd->pd_cv);
279 mutex_exit(&pd->pd_mtx);
280 }
281
282 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) {
283 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
284 if (err != 0)
285 goto post;
286 return (0);
287 }
288
289 if (td->td_flags & TRAVERSE_PRE) {
290 err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
291 td->td_arg);
292 if (err == TRAVERSE_VISIT_NO_CHILDREN)
293 return (0);
294 if (err != 0)
295 goto post;
296 }
297
298 if (BP_GET_LEVEL(bp) > 0) {
299 uint32_t flags = ARC_FLAG_WAIT;
300 int32_t i, ptidx, pidx;
301 uint32_t prefetchlimit;
302 int32_t epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
303 zbookmark_phys_t *czb;
304
305 ASSERT(!BP_IS_PROTECTED(bp));
306
307 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
308 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
309 if (err != 0)
310 goto post;
311
312 czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
313
314 /*
315 * When performing a traversal it is beneficial to
316 * asynchronously read-ahead the upcoming indirect
317 * blocks since they will be needed shortly. However,
318 * since a 128k indirect (non-L0) block may contain up
319 * to 1024 128-byte block pointers, its preferable to not
320 * prefetch them all at once. Issuing a large number of
321 * async reads may effect performance, and the earlier
322 * the indirect blocks are prefetched the less likely
323 * they are to still be resident in the ARC when needed.
324 * Therefore, prefetching indirect blocks is limited to
325 * zfs_traverse_indirect_prefetch_limit=32 blocks by
326 * default.
327 *
328 * pidx: Index for which next prefetch to be issued.
329 * ptidx: Index at which next prefetch to be triggered.
330 */
331 ptidx = 0;
332 pidx = 1;
333 prefetchlimit = zfs_traverse_indirect_prefetch_limit;
334 for (i = 0; i < epb; i++) {
335 if (prefetchlimit && i == ptidx) {
336 ASSERT3S(ptidx, <=, pidx);
337 for (uint32_t prefetched = 0; pidx < epb &&
338 prefetched < prefetchlimit; pidx++) {
339 SET_BOOKMARK(czb, zb->zb_objset,
340 zb->zb_object, zb->zb_level - 1,
341 zb->zb_blkid * epb + pidx);
342 if (traverse_prefetch_metadata(td, dnp,
343 &((blkptr_t *)buf->b_data)[pidx],
344 czb) == B_TRUE) {
345 prefetched++;
346 if (prefetched ==
347 MAX(prefetchlimit / 2, 1))
348 ptidx = pidx;
349 }
350 }
351 }
352
353 /* recursively visitbp() blocks below this */
354 SET_BOOKMARK(czb, zb->zb_objset, zb->zb_object,
355 zb->zb_level - 1,
356 zb->zb_blkid * epb + i);
357 err = traverse_visitbp(td, dnp,
358 &((blkptr_t *)buf->b_data)[i], czb);
359 if (err != 0)
360 break;
361 }
362
363 kmem_free(czb, sizeof (zbookmark_phys_t));
364
365 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
366 uint32_t flags = ARC_FLAG_WAIT;
367 uint32_t zio_flags = ZIO_FLAG_CANFAIL;
368 int32_t i;
369 int32_t epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
370 dnode_phys_t *child_dnp;
371
372 /*
373 * dnode blocks might have their bonus buffers encrypted, so
374 * we must be careful to honor TRAVERSE_NO_DECRYPT
375 */
376 if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
377 zio_flags |= ZIO_FLAG_RAW;
378
379 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
380 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
381 if (err != 0)
382 goto post;
383
384 child_dnp = buf->b_data;
385
386 for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
387 prefetch_dnode_metadata(td, &child_dnp[i],
388 zb->zb_objset, zb->zb_blkid * epb + i);
389 }
390
391 /* recursively visitbp() blocks below this */
392 for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
393 err = traverse_dnode(td, bp, &child_dnp[i],
394 zb->zb_objset, zb->zb_blkid * epb + i);
395 if (err != 0)
396 break;
397 }
398 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
399 uint32_t zio_flags = ZIO_FLAG_CANFAIL;
400 arc_flags_t flags = ARC_FLAG_WAIT;
401 objset_phys_t *osp;
402
403 if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
404 zio_flags |= ZIO_FLAG_RAW;
405
406 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
407 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
408 if (err != 0)
409 goto post;
410
411 osp = buf->b_data;
412 prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
413 DMU_META_DNODE_OBJECT);
414 /*
415 * See the block comment above for the goal of this variable.
416 * If the maxblkid of the meta-dnode is 0, then we know that
417 * we've never had more than DNODES_PER_BLOCK objects in the
418 * dataset, which means we can't have reused any object ids.
419 */
420 if (osp->os_meta_dnode.dn_maxblkid == 0)
421 td->td_realloc_possible = B_FALSE;
422
423 if (OBJSET_BUF_HAS_USERUSED(buf)) {
424 if (OBJSET_BUF_HAS_PROJECTUSED(buf))
425 prefetch_dnode_metadata(td,
426 &osp->os_projectused_dnode,
427 zb->zb_objset, DMU_PROJECTUSED_OBJECT);
428 prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
429 zb->zb_objset, DMU_GROUPUSED_OBJECT);
430 prefetch_dnode_metadata(td, &osp->os_userused_dnode,
431 zb->zb_objset, DMU_USERUSED_OBJECT);
432 }
433
434 err = traverse_dnode(td, bp, &osp->os_meta_dnode, zb->zb_objset,
435 DMU_META_DNODE_OBJECT);
436 if (err == 0 && OBJSET_BUF_HAS_USERUSED(buf)) {
437 if (OBJSET_BUF_HAS_PROJECTUSED(buf))
438 err = traverse_dnode(td, bp,
439 &osp->os_projectused_dnode, zb->zb_objset,
440 DMU_PROJECTUSED_OBJECT);
441 if (err == 0)
442 err = traverse_dnode(td, bp,
443 &osp->os_groupused_dnode, zb->zb_objset,
444 DMU_GROUPUSED_OBJECT);
445 if (err == 0)
446 err = traverse_dnode(td, bp,
447 &osp->os_userused_dnode, zb->zb_objset,
448 DMU_USERUSED_OBJECT);
449 }
450 }
451
452 if (buf)
453 arc_buf_destroy(buf, &buf);
454
455 post:
456 if (err == 0 && (td->td_flags & TRAVERSE_POST))
457 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
458
459 if ((td->td_flags & TRAVERSE_HARD) && (err == EIO || err == ECKSUM)) {
460 /*
461 * Ignore this disk error as requested by the HARD flag,
462 * and continue traversal.
463 */
464 err = 0;
465 }
466
467 /*
468 * If we are stopping here, set td_resume.
469 */
470 if (td->td_resume != NULL && err != 0 && !td->td_paused) {
471 td->td_resume->zb_objset = zb->zb_objset;
472 td->td_resume->zb_object = zb->zb_object;
473 td->td_resume->zb_level = 0;
474 /*
475 * If we have stopped on an indirect block (e.g. due to
476 * i/o error), we have not visited anything below it.
477 * Set the bookmark to the first level-0 block that we need
478 * to visit. This way, the resuming code does not need to
479 * deal with resuming from indirect blocks.
480 *
481 * Note, if zb_level <= 0, dnp may be NULL, so we don't want
482 * to dereference it.
483 */
484 td->td_resume->zb_blkid = zb->zb_blkid;
485 if (zb->zb_level > 0) {
486 td->td_resume->zb_blkid <<= zb->zb_level *
487 (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
488 }
489 td->td_paused = B_TRUE;
490 }
491
492 return (err);
493 }
494
495 static void
496 prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
497 uint64_t objset, uint64_t object)
498 {
499 int j;
500 zbookmark_phys_t czb;
501
502 for (j = 0; j < dnp->dn_nblkptr; j++) {
503 SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
504 traverse_prefetch_metadata(td, dnp, &dnp->dn_blkptr[j], &czb);
505 }
506
507 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
508 SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
509 traverse_prefetch_metadata(td, dnp, DN_SPILL_BLKPTR(dnp), &czb);
510 }
511 }
512
513 static int
514 traverse_dnode(traverse_data_t *td, const blkptr_t *bp, const dnode_phys_t *dnp,
515 uint64_t objset, uint64_t object)
516 {
517 int j, err = 0;
518 zbookmark_phys_t czb;
519
520 if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
521 object < td->td_resume->zb_object)
522 return (0);
523
524 if (td->td_flags & TRAVERSE_PRE) {
525 SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
526 ZB_DNODE_BLKID);
527 err = td->td_func(td->td_spa, NULL, bp, &czb, dnp,
528 td->td_arg);
529 if (err == TRAVERSE_VISIT_NO_CHILDREN)
530 return (0);
531 if (err != 0)
532 return (err);
533 }
534
535 for (j = 0; j < dnp->dn_nblkptr; j++) {
536 SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
537 err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
538 if (err != 0)
539 break;
540 }
541
542 if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
543 SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
544 err = traverse_visitbp(td, dnp, DN_SPILL_BLKPTR(dnp), &czb);
545 }
546
547 if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
548 SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
549 ZB_DNODE_BLKID);
550 err = td->td_func(td->td_spa, NULL, bp, &czb, dnp,
551 td->td_arg);
552 if (err == TRAVERSE_VISIT_NO_CHILDREN)
553 return (0);
554 if (err != 0)
555 return (err);
556 }
557 return (err);
558 }
559
560 static int
561 traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
562 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
563 {
564 (void) zilog, (void) dnp;
565 prefetch_data_t *pfd = arg;
566 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
567 arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
568 ARC_FLAG_PRESCIENT_PREFETCH;
569
570 ASSERT(pfd->pd_bytes_fetched >= 0);
571 if (zb->zb_level == ZB_DNODE_LEVEL)
572 return (0);
573 if (pfd->pd_cancel)
574 return (SET_ERROR(EINTR));
575
576 if (!prefetch_needed(pfd, bp))
577 return (0);
578
579 mutex_enter(&pfd->pd_mtx);
580 while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
581 cv_wait_sig(&pfd->pd_cv, &pfd->pd_mtx);
582 pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
583 cv_broadcast(&pfd->pd_cv);
584 mutex_exit(&pfd->pd_mtx);
585
586 if ((pfd->pd_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
587 zio_flags |= ZIO_FLAG_RAW;
588
589 (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
590 zio_flags, &aflags, zb);
591
592 return (0);
593 }
594
595 static void
596 traverse_prefetch_thread(void *arg)
597 {
598 traverse_data_t *td_main = arg;
599 traverse_data_t td = *td_main;
600 zbookmark_phys_t czb;
601 fstrans_cookie_t cookie = spl_fstrans_mark();
602
603 td.td_func = traverse_prefetcher;
604 td.td_arg = td_main->td_pfd;
605 td.td_pfd = NULL;
606 td.td_resume = &td_main->td_pfd->pd_resume;
607
608 SET_BOOKMARK(&czb, td.td_objset,
609 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
610 (void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
611
612 mutex_enter(&td_main->td_pfd->pd_mtx);
613 td_main->td_pfd->pd_exited = B_TRUE;
614 cv_broadcast(&td_main->td_pfd->pd_cv);
615 mutex_exit(&td_main->td_pfd->pd_mtx);
616 spl_fstrans_unmark(cookie);
617 }
618
619 /*
620 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
621 * in syncing context).
622 */
623 static int
624 traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
625 uint64_t txg_start, zbookmark_phys_t *resume, int flags,
626 blkptr_cb_t func, void *arg)
627 {
628 traverse_data_t *td;
629 prefetch_data_t *pd;
630 zbookmark_phys_t *czb;
631 int err;
632
633 ASSERT(ds == NULL || objset == ds->ds_object);
634 ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
635
636 td = kmem_alloc(sizeof (traverse_data_t), KM_SLEEP);
637 pd = kmem_zalloc(sizeof (prefetch_data_t), KM_SLEEP);
638 czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
639
640 td->td_spa = spa;
641 td->td_objset = objset;
642 td->td_rootbp = rootbp;
643 td->td_min_txg = txg_start;
644 td->td_resume = resume;
645 td->td_func = func;
646 td->td_arg = arg;
647 td->td_pfd = pd;
648 td->td_flags = flags;
649 td->td_paused = B_FALSE;
650 td->td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
651
652 if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
653 VERIFY(spa_feature_enabled_txg(spa,
654 SPA_FEATURE_HOLE_BIRTH, &td->td_hole_birth_enabled_txg));
655 } else {
656 td->td_hole_birth_enabled_txg = UINT64_MAX;
657 }
658
659 pd->pd_flags = flags;
660 if (resume != NULL)
661 pd->pd_resume = *resume;
662 mutex_init(&pd->pd_mtx, NULL, MUTEX_DEFAULT, NULL);
663 cv_init(&pd->pd_cv, NULL, CV_DEFAULT, NULL);
664
665 SET_BOOKMARK(czb, td->td_objset,
666 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
667
668 /* See comment on ZIL traversal in dsl_scan_visitds. */
669 if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
670 zio_flag_t zio_flags = ZIO_FLAG_CANFAIL;
671 uint32_t flags = ARC_FLAG_WAIT;
672 objset_phys_t *osp;
673 arc_buf_t *buf;
674 ASSERT(!BP_IS_REDACTED(rootbp));
675
676 if ((td->td_flags & TRAVERSE_NO_DECRYPT) &&
677 BP_IS_PROTECTED(rootbp))
678 zio_flags |= ZIO_FLAG_RAW;
679
680 err = arc_read(NULL, td->td_spa, rootbp, arc_getbuf_func,
681 &buf, ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, czb);
682 if (err != 0) {
683 /*
684 * If both TRAVERSE_HARD and TRAVERSE_PRE are set,
685 * continue to visitbp so that td_func can be called
686 * in pre stage, and err will reset to zero.
687 */
688 if (!(td->td_flags & TRAVERSE_HARD) ||
689 !(td->td_flags & TRAVERSE_PRE))
690 goto out;
691 } else {
692 osp = buf->b_data;
693 traverse_zil(td, &osp->os_zil_header);
694 arc_buf_destroy(buf, &buf);
695 }
696 }
697
698 if (!(flags & TRAVERSE_PREFETCH_DATA) ||
699 taskq_dispatch(spa->spa_prefetch_taskq, traverse_prefetch_thread,
700 td, TQ_NOQUEUE) == TASKQID_INVALID)
701 pd->pd_exited = B_TRUE;
702
703 err = traverse_visitbp(td, NULL, rootbp, czb);
704
705 mutex_enter(&pd->pd_mtx);
706 pd->pd_cancel = B_TRUE;
707 cv_broadcast(&pd->pd_cv);
708 while (!pd->pd_exited)
709 cv_wait_sig(&pd->pd_cv, &pd->pd_mtx);
710 mutex_exit(&pd->pd_mtx);
711 out:
712 mutex_destroy(&pd->pd_mtx);
713 cv_destroy(&pd->pd_cv);
714
715 kmem_free(czb, sizeof (zbookmark_phys_t));
716 kmem_free(pd, sizeof (struct prefetch_data));
717 kmem_free(td, sizeof (struct traverse_data));
718
719 return (err);
720 }
721
722 /*
723 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
724 * in syncing context).
725 */
726 int
727 traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
728 zbookmark_phys_t *resume,
729 int flags, blkptr_cb_t func, void *arg)
730 {
731 return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
732 &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
733 }
734
735 int
736 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
737 int flags, blkptr_cb_t func, void *arg)
738 {
739 return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
740 }
741
742 int
743 traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
744 uint64_t txg_start, zbookmark_phys_t *resume, int flags,
745 blkptr_cb_t func, void *arg)
746 {
747 return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
748 blkptr, txg_start, resume, flags, func, arg));
749 }
750
751 /*
752 * NB: pool must not be changing on-disk (eg, from zdb or sync context).
753 */
754 int
755 traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
756 blkptr_cb_t func, void *arg)
757 {
758 int err;
759 dsl_pool_t *dp = spa_get_dsl(spa);
760 objset_t *mos = dp->dp_meta_objset;
761 boolean_t hard = (flags & TRAVERSE_HARD);
762
763 /* visit the MOS */
764 err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
765 txg_start, NULL, flags, func, arg);
766 if (err != 0)
767 return (err);
768
769 /* visit each dataset */
770 for (uint64_t obj = 1; err == 0;
771 err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
772 dmu_object_info_t doi;
773
774 err = dmu_object_info(mos, obj, &doi);
775 if (err != 0) {
776 if (hard)
777 continue;
778 break;
779 }
780
781 if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
782 dsl_dataset_t *ds;
783 uint64_t txg = txg_start;
784
785 dsl_pool_config_enter(dp, FTAG);
786 err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
787 dsl_pool_config_exit(dp, FTAG);
788 if (err != 0) {
789 if (hard)
790 continue;
791 break;
792 }
793 if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
794 txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
795 err = traverse_dataset(ds, txg, flags, func, arg);
796 dsl_dataset_rele(ds, FTAG);
797 if (err != 0)
798 break;
799 }
800 }
801 if (err == ESRCH)
802 err = 0;
803 return (err);
804 }
805
806 EXPORT_SYMBOL(traverse_dataset);
807 EXPORT_SYMBOL(traverse_pool);
808
809 ZFS_MODULE_PARAM(zfs, zfs_, pd_bytes_max, INT, ZMOD_RW,
810 "Max number of bytes to prefetch");
811
812 ZFS_MODULE_PARAM(zfs, zfs_, traverse_indirect_prefetch_limit, UINT, ZMOD_RW,
813 "Traverse prefetch number of blocks pointed by indirect block");
814
815 #if defined(_KERNEL)
816 module_param_named(ignore_hole_birth, send_holes_without_birth_time, int, 0644);
817 MODULE_PARM_DESC(ignore_hole_birth,
818 "Alias for send_holes_without_birth_time");
819 #endif
820
821 /* CSTYLED */
822 ZFS_MODULE_PARAM(zfs, , send_holes_without_birth_time, INT, ZMOD_RW,
823 "Ignore hole_birth txg for zfs send");