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