Files
mirror_zfs/module/zfs/dmu_traverse.c
T

827 lines
23 KiB
C
Raw Normal View History

2025-01-04 11:04:27 +11:00
// SPDX-License-Identifier: CDDL-1.0
2008-11-20 12:01:55 -08:00
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2022-07-11 23:16:13 +02:00
* or https://opensource.org/licenses/CDDL-1.0.
2008-11-20 12:01:55 -08:00
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2016-12-16 14:11:29 -08:00
* Copyright (c) 2012, 2018 by Delphix. All rights reserved.
2008-11-20 12:01:55 -08:00
*/
#include <sys/zfs_context.h>
#include <sys/dmu_objset.h>
#include <sys/dmu_traverse.h>
#include <sys/dsl_dataset.h>
#include <sys/dsl_dir.h>
#include <sys/dsl_pool.h>
#include <sys/dnode.h>
#include <sys/spa.h>
2018-03-30 21:10:01 +02:00
#include <sys/spa_impl.h>
2008-11-20 12:01:55 -08:00
#include <sys/zio.h>
#include <sys/dmu_impl.h>
#include <sys/sa.h>
#include <sys/sa_impl.h>
#include <sys/callb.h>
2013-12-09 10:37:51 -08:00
#include <sys/zfeature.h>
2022-01-15 00:37:55 +01:00
static int32_t zfs_pd_bytes_max = 50 * 1024 * 1024; /* 50MB */
static int32_t send_holes_without_birth_time = 1;
static uint_t zfs_traverse_indirect_prefetch_limit = 32;
2010-08-26 14:24:34 -07:00
typedef struct prefetch_data {
kmutex_t pd_mtx;
kcondvar_t pd_cv;
int32_t pd_bytes_fetched;
int pd_flags;
boolean_t pd_cancel;
boolean_t pd_exited;
2016-01-06 22:22:48 +01:00
zbookmark_phys_t pd_resume;
2010-08-26 14:24:34 -07:00
} prefetch_data_t;
2010-08-26 14:24:34 -07:00
typedef struct traverse_data {
spa_t *td_spa;
uint64_t td_objset;
blkptr_t *td_rootbp;
uint64_t td_min_txg;
zbookmark_phys_t *td_resume;
int td_flags;
2010-08-26 14:24:34 -07:00
prefetch_data_t *td_pfd;
boolean_t td_paused;
2015-05-14 17:41:29 -06:00
uint64_t td_hole_birth_enabled_txg;
blkptr_cb_t *td_func;
void *td_arg;
boolean_t td_realloc_possible;
2010-08-26 14:24:34 -07:00
} traverse_data_t;
2008-11-20 12:01:55 -08:00
2019-06-19 09:48:13 -07:00
static int traverse_dnode(traverse_data_t *td, const blkptr_t *bp,
const dnode_phys_t *dnp, uint64_t objset, uint64_t object);
static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
2013-07-02 13:26:24 -07:00
uint64_t objset, uint64_t object);
2009-07-02 15:44:48 -07:00
2025-07-17 12:50:54 -04:00
static inline uint64_t
get_birth_time(traverse_data_t *td, const blkptr_t *bp)
{
if (td->td_flags & TRAVERSE_LOGICAL)
return (BP_GET_LOGICAL_BIRTH(bp));
else
return (BP_GET_BIRTH(bp));
}
static int
2020-10-09 18:34:54 +02:00
traverse_zil_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
uint64_t claim_txg)
2008-11-20 12:01:55 -08:00
{
2010-08-26 14:24:34 -07:00
traverse_data_t *td = arg;
zbookmark_phys_t zb;
2008-11-20 12:01:55 -08:00
2013-12-09 10:37:51 -08:00
if (BP_IS_HOLE(bp))
return (0);
2008-11-20 12:01:55 -08:00
if (claim_txg == 0 &&
2025-07-17 12:50:54 -04:00
get_birth_time(td, bp) >= spa_min_claim_txg(td->td_spa))
2016-12-16 14:11:29 -08:00
return (-1);
SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
2013-07-02 13:26:24 -07:00
(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
return (0);
2008-11-20 12:01:55 -08:00
}
static int
2020-10-09 18:34:54 +02:00
traverse_zil_record(zilog_t *zilog, const lr_t *lrc, void *arg,
uint64_t claim_txg)
2008-11-20 12:01:55 -08:00
{
2010-08-26 14:24:34 -07:00
traverse_data_t *td = arg;
2008-11-20 12:01:55 -08:00
if (lrc->lrc_txtype == TX_WRITE) {
lr_write_t *lr = (lr_write_t *)lrc;
blkptr_t *bp = &lr->lr_blkptr;
zbookmark_phys_t zb;
2008-11-20 12:01:55 -08:00
2013-12-09 10:37:51 -08:00
if (BP_IS_HOLE(bp))
return (0);
2008-11-20 12:01:55 -08:00
2025-07-17 12:50:54 -04:00
if (claim_txg == 0 || get_birth_time(td, bp) < claim_txg)
return (0);
2022-10-12 14:25:18 -04:00
ASSERT3U(BP_GET_LSIZE(bp), !=, 0);
2010-08-26 14:24:34 -07:00
SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
2013-07-02 13:26:24 -07:00
(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
td->td_arg);
2008-11-20 12:01:55 -08:00
}
return (0);
2008-11-20 12:01:55 -08:00
}
static void
2010-08-26 14:24:34 -07:00
traverse_zil(traverse_data_t *td, zil_header_t *zh)
2008-11-20 12:01:55 -08:00
{
uint64_t claim_txg = zh->zh_claim_txg;
/*
* We only want to visit blocks that have been claimed but not yet
2016-12-16 14:11:29 -08:00
* replayed; plus blocks that are already stable in read-only mode.
2008-11-20 12:01:55 -08:00
*/
2009-01-15 13:59:39 -08:00
if (claim_txg == 0 && spa_writeable(td->td_spa))
2008-11-20 12:01:55 -08:00
return;
2016-12-16 14:11:29 -08:00
zilog_t *zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
2017-08-14 13:36:48 -04:00
claim_txg, !(td->td_flags & TRAVERSE_NO_DECRYPT));
2008-11-20 12:01:55 -08:00
zil_free(zilog);
}
2012-12-13 15:24:15 -08:00
typedef enum resume_skip {
RESUME_SKIP_ALL,
RESUME_SKIP_NONE,
RESUME_SKIP_CHILDREN
} resume_skip_t;
/*
* Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
* the block indicated by zb does not need to be visited at all. Returns
* RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
* resume point. This indicates that this block should be visited but not its
* children (since they must have been visited in a previous traversal).
* Otherwise returns RESUME_SKIP_NONE.
*/
static resume_skip_t
resume_skip_check(const traverse_data_t *td, const dnode_phys_t *dnp,
const zbookmark_phys_t *zb)
2012-12-13 15:24:15 -08:00
{
if (td->td_resume != NULL) {
2012-12-13 15:24:15 -08:00
/*
* If we already visited this bp & everything below,
* don't bother doing it again.
*/
2015-12-22 02:31:57 +01:00
if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
2012-12-13 15:24:15 -08:00
return (RESUME_SKIP_ALL);
2022-02-25 14:26:54 +01:00
if (memcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
2012-12-13 15:24:15 -08:00
if (td->td_flags & TRAVERSE_POST)
return (RESUME_SKIP_CHILDREN);
}
}
return (RESUME_SKIP_NONE);
}
/*
* Returns B_TRUE, if prefetch read is issued, otherwise B_FALSE.
*/
static boolean_t
traverse_prefetch_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
const blkptr_t *bp, const zbookmark_phys_t *zb)
{
arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
ARC_FLAG_PRESCIENT_PREFETCH;
2017-09-28 11:49:13 -04:00
int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
return (B_FALSE);
/*
* If this bp is before the resume point, it may have already been
* freed.
*/
if (resume_skip_check(td, dnp, zb) != RESUME_SKIP_NONE)
return (B_FALSE);
2025-07-17 12:50:54 -04:00
if (BP_IS_HOLE(bp) || get_birth_time(td, bp) <= td->td_min_txg)
return (B_FALSE);
if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
return (B_FALSE);
2019-06-19 09:48:13 -07:00
ASSERT(!BP_IS_REDACTED(bp));
2017-08-14 13:36:48 -04:00
if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
zio_flags |= ZIO_FLAG_RAW;
2013-07-02 13:26:24 -07:00
(void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
2017-08-14 13:36:48 -04:00
ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
return (B_TRUE);
}
static boolean_t
prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
{
ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
2019-06-19 09:48:13 -07:00
BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG || BP_IS_REDACTED(bp))
return (B_FALSE);
return (B_TRUE);
}
2011-05-25 16:09:57 -07:00
static int
traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
const blkptr_t *bp, const zbookmark_phys_t *zb)
2010-08-27 16:48:18 -07:00
{
int err = 0;
2011-05-25 16:09:57 -07:00
arc_buf_t *buf = NULL;
2015-03-27 15:04:12 +11:00
prefetch_data_t *pd = td->td_pfd;
2012-12-13 15:24:15 -08:00
switch (resume_skip_check(td, dnp, zb)) {
case RESUME_SKIP_ALL:
return (0);
case RESUME_SKIP_CHILDREN:
goto post;
case RESUME_SKIP_NONE:
break;
default:
ASSERT(0);
}
2010-08-27 16:48:18 -07:00
if (BP_GET_LOGICAL_BIRTH(bp) == 0) {
2015-05-14 17:41:29 -06:00
/*
* Since this block has a birth time of 0 it must be one of
* two things: a hole created before the
* SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole
* which has always been a hole in an object.
*
* If a file is written sparsely, then the unwritten parts of
* the file were "always holes" -- that is, they have been
* holes since this object was allocated. However, we (and
* our callers) can not necessarily tell when an object was
* allocated. Therefore, if it's possible that this object
* was freed and then its object number reused, we need to
* visit all the holes with birth==0.
*
* If it isn't possible that the object number was reused,
* then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote
* all the blocks we will visit as part of this traversal,
* then this hole must have always existed, so we can skip
* it. We visit blocks born after (exclusive) td_min_txg.
*
* Note that the meta-dnode cannot be reallocated.
2015-05-14 17:41:29 -06:00
*/
2016-10-07 21:02:24 -07:00
if (!send_holes_without_birth_time &&
(!td->td_realloc_possible ||
zb->zb_object == DMU_META_DNODE_OBJECT) &&
td->td_hole_birth_enabled_txg <= td->td_min_txg)
2015-05-14 17:41:29 -06:00
return (0);
2025-07-17 12:50:54 -04:00
} else if (get_birth_time(td, bp) <= td->td_min_txg) {
2013-12-09 10:37:51 -08:00
return (0);
}
2015-03-27 15:04:12 +11:00
if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
uint64_t size = BP_GET_LSIZE(bp);
2015-03-27 15:04:12 +11:00
mutex_enter(&pd->pd_mtx);
ASSERT(pd->pd_bytes_fetched >= 0);
while (pd->pd_bytes_fetched < size && !pd->pd_exited)
2015-06-09 16:39:25 -07:00
cv_wait_sig(&pd->pd_cv, &pd->pd_mtx);
pd->pd_bytes_fetched -= size;
2015-03-27 15:04:12 +11:00
cv_broadcast(&pd->pd_cv);
mutex_exit(&pd->pd_mtx);
2008-11-20 12:01:55 -08:00
}
2019-06-19 09:48:13 -07:00
if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) {
err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
if (err != 0)
goto post;
return (0);
}
2011-05-25 16:09:57 -07:00
if (td->td_flags & TRAVERSE_PRE) {
2013-07-02 13:26:24 -07:00
err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
2011-05-25 16:09:57 -07:00
td->td_arg);
if (err == TRAVERSE_VISIT_NO_CHILDREN)
2010-08-26 14:24:34 -07:00
return (0);
2012-12-13 15:24:15 -08:00
if (err != 0)
goto post;
2008-11-20 12:01:55 -08:00
}
2011-05-25 16:09:57 -07:00
if (BP_GET_LEVEL(bp) > 0) {
2025-05-13 17:26:55 -04:00
arc_flags_t flags = ARC_FLAG_WAIT;
int32_t i, ptidx, pidx;
uint32_t prefetchlimit;
int32_t epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
zbookmark_phys_t *czb;
2017-08-14 13:36:48 -04:00
ASSERT(!BP_IS_PROTECTED(bp));
2013-07-02 13:26:24 -07:00
err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
2011-05-25 16:09:57 -07:00
ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
2013-09-04 07:00:57 -05:00
if (err != 0)
goto post;
2014-11-20 19:09:39 -05:00
czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
/*
* When performing a traversal it is beneficial to
* asynchronously read-ahead the upcoming indirect
* blocks since they will be needed shortly. However,
* since a 128k indirect (non-L0) block may contain up
* to 1024 128-byte block pointers, its preferable to not
* prefetch them all at once. Issuing a large number of
* async reads may effect performance, and the earlier
* the indirect blocks are prefetched the less likely
* they are to still be resident in the ARC when needed.
* Therefore, prefetching indirect blocks is limited to
* zfs_traverse_indirect_prefetch_limit=32 blocks by
* default.
*
* pidx: Index for which next prefetch to be issued.
* ptidx: Index at which next prefetch to be triggered.
*/
ptidx = 0;
pidx = 1;
prefetchlimit = zfs_traverse_indirect_prefetch_limit;
for (i = 0; i < epb; i++) {
if (prefetchlimit && i == ptidx) {
ASSERT3S(ptidx, <=, pidx);
for (uint32_t prefetched = 0; pidx < epb &&
prefetched < prefetchlimit; pidx++) {
SET_BOOKMARK(czb, zb->zb_objset,
zb->zb_object, zb->zb_level - 1,
zb->zb_blkid * epb + pidx);
if (traverse_prefetch_metadata(td, dnp,
&((blkptr_t *)buf->b_data)[pidx],
czb) == B_TRUE) {
prefetched++;
if (prefetched ==
MAX(prefetchlimit / 2, 1))
ptidx = pidx;
}
}
}
/* recursively visitbp() blocks below this */
SET_BOOKMARK(czb, zb->zb_objset, zb->zb_object,
2011-05-25 16:09:57 -07:00
zb->zb_level - 1,
zb->zb_blkid * epb + i);
err = traverse_visitbp(td, dnp,
&((blkptr_t *)buf->b_data)[i], czb);
if (err != 0)
break;
}
kmem_free(czb, sizeof (zbookmark_phys_t));
2011-05-25 16:09:57 -07:00
} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
2025-05-13 17:26:55 -04:00
arc_flags_t flags = ARC_FLAG_WAIT;
zio_flag_t zio_flags = ZIO_FLAG_CANFAIL;
int32_t i;
int32_t epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
2016-01-06 22:22:48 +01:00
dnode_phys_t *child_dnp;
2017-08-14 13:36:48 -04:00
/*
* dnode blocks might have their bonus buffers encrypted, so
* we must be careful to honor TRAVERSE_NO_DECRYPT
*/
if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
zio_flags |= ZIO_FLAG_RAW;
2013-07-02 13:26:24 -07:00
err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
2017-08-14 13:36:48 -04:00
ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
2013-09-04 07:00:57 -05:00
if (err != 0)
goto post;
2017-08-14 13:36:48 -04:00
2016-01-06 22:22:48 +01:00
child_dnp = buf->b_data;
2016-01-06 22:22:48 +01:00
for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
prefetch_dnode_metadata(td, &child_dnp[i],
zb->zb_objset, zb->zb_blkid * epb + i);
}
/* recursively visitbp() blocks below this */
2016-01-06 22:22:48 +01:00
for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
2019-06-19 09:48:13 -07:00
err = traverse_dnode(td, bp, &child_dnp[i],
2016-01-06 22:22:48 +01:00
zb->zb_objset, zb->zb_blkid * epb + i);
if (err != 0)
break;
2008-11-20 12:01:55 -08:00
}
2011-05-25 16:09:57 -07:00
} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
2025-05-13 17:26:55 -04:00
zio_flag_t zio_flags = ZIO_FLAG_CANFAIL;
2014-12-06 09:24:32 -08:00
arc_flags_t flags = ARC_FLAG_WAIT;
2011-05-25 16:09:57 -07:00
objset_phys_t *osp;
2017-08-14 13:36:48 -04:00
if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
zio_flags |= ZIO_FLAG_RAW;
2013-07-02 13:26:24 -07:00
err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
2017-08-14 13:36:48 -04:00
ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
2013-09-04 07:00:57 -05:00
if (err != 0)
goto post;
2011-05-25 16:09:57 -07:00
osp = buf->b_data;
2016-01-06 22:22:48 +01:00
prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
DMU_META_DNODE_OBJECT);
/*
* See the block comment above for the goal of this variable.
* If the maxblkid of the meta-dnode is 0, then we know that
* we've never had more than DNODES_PER_BLOCK objects in the
* dataset, which means we can't have reused any object ids.
*/
if (osp->os_meta_dnode.dn_maxblkid == 0)
td->td_realloc_possible = B_FALSE;
2018-02-14 06:54:54 +08:00
if (OBJSET_BUF_HAS_USERUSED(buf)) {
if (OBJSET_BUF_HAS_PROJECTUSED(buf))
prefetch_dnode_metadata(td,
&osp->os_projectused_dnode,
zb->zb_objset, DMU_PROJECTUSED_OBJECT);
2016-01-06 22:22:48 +01:00
prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
zb->zb_objset, DMU_GROUPUSED_OBJECT);
prefetch_dnode_metadata(td, &osp->os_userused_dnode,
zb->zb_objset, DMU_USERUSED_OBJECT);
}
2019-06-19 09:48:13 -07:00
err = traverse_dnode(td, bp, &osp->os_meta_dnode, zb->zb_objset,
2011-05-25 16:09:57 -07:00
DMU_META_DNODE_OBJECT);
2018-02-14 06:54:54 +08:00
if (err == 0 && OBJSET_BUF_HAS_USERUSED(buf)) {
if (OBJSET_BUF_HAS_PROJECTUSED(buf))
2019-06-19 09:48:13 -07:00
err = traverse_dnode(td, bp,
2018-02-14 06:54:54 +08:00
&osp->os_projectused_dnode, zb->zb_objset,
DMU_PROJECTUSED_OBJECT);
if (err == 0)
2019-06-19 09:48:13 -07:00
err = traverse_dnode(td, bp,
2018-02-14 06:54:54 +08:00
&osp->os_groupused_dnode, zb->zb_objset,
DMU_GROUPUSED_OBJECT);
if (err == 0)
2019-06-19 09:48:13 -07:00
err = traverse_dnode(td, bp,
2018-02-14 06:54:54 +08:00
&osp->os_userused_dnode, zb->zb_objset,
DMU_USERUSED_OBJECT);
2008-11-20 12:01:55 -08:00
}
}
2011-05-25 16:09:57 -07:00
if (buf)
arc_buf_destroy(buf, &buf);
2008-11-20 12:01:55 -08:00
2012-12-13 15:24:15 -08:00
post:
if (err == 0 && (td->td_flags & TRAVERSE_POST))
2013-07-02 13:26:24 -07:00
err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
if ((td->td_flags & TRAVERSE_HARD) && (err == EIO || err == ECKSUM)) {
/*
* Ignore this disk error as requested by the HARD flag,
* and continue traversal.
*/
err = 0;
2012-12-13 15:24:15 -08:00
}
/*
* If we are stopping here, set td_resume.
*/
if (td->td_resume != NULL && err != 0 && !td->td_paused) {
td->td_resume->zb_objset = zb->zb_objset;
td->td_resume->zb_object = zb->zb_object;
td->td_resume->zb_level = 0;
/*
* If we have stopped on an indirect block (e.g. due to
* i/o error), we have not visited anything below it.
* Set the bookmark to the first level-0 block that we need
* to visit. This way, the resuming code does not need to
* deal with resuming from indirect blocks.
2016-01-06 22:22:48 +01:00
*
* Note, if zb_level <= 0, dnp may be NULL, so we don't want
* to dereference it.
*/
2016-01-06 22:22:48 +01:00
td->td_resume->zb_blkid = zb->zb_blkid;
if (zb->zb_level > 0) {
td->td_resume->zb_blkid <<= zb->zb_level *
(dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
}
td->td_paused = B_TRUE;
}
2008-11-20 12:01:55 -08:00
return (err);
2008-11-20 12:01:55 -08:00
}
static void
prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
2013-07-02 13:26:24 -07:00
uint64_t objset, uint64_t object)
{
int j;
zbookmark_phys_t czb;
for (j = 0; j < dnp->dn_nblkptr; j++) {
SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
traverse_prefetch_metadata(td, dnp, &dnp->dn_blkptr[j], &czb);
}
if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
traverse_prefetch_metadata(td, dnp, DN_SPILL_BLKPTR(dnp), &czb);
}
}
2009-07-02 15:44:48 -07:00
static int
2019-06-19 09:48:13 -07:00
traverse_dnode(traverse_data_t *td, const blkptr_t *bp, const dnode_phys_t *dnp,
2013-07-02 13:26:24 -07:00
uint64_t objset, uint64_t object)
2009-07-02 15:44:48 -07:00
{
int j, err = 0;
zbookmark_phys_t czb;
2009-07-02 15:44:48 -07:00
2016-01-06 22:22:48 +01:00
if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
object < td->td_resume->zb_object)
return (0);
2015-12-22 02:31:57 +01:00
if (td->td_flags & TRAVERSE_PRE) {
SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
ZB_DNODE_BLKID);
2019-06-19 09:48:13 -07:00
err = td->td_func(td->td_spa, NULL, bp, &czb, dnp,
2015-12-22 02:31:57 +01:00
td->td_arg);
if (err == TRAVERSE_VISIT_NO_CHILDREN)
return (0);
if (err != 0)
return (err);
}
2009-07-02 15:44:48 -07:00
for (j = 0; j < dnp->dn_nblkptr; j++) {
SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
2013-07-02 13:26:24 -07:00
err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
if (err != 0)
break;
2009-07-02 15:44:48 -07:00
}
2015-12-22 02:31:57 +01:00
if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
2016-03-16 18:25:34 -07:00
err = traverse_visitbp(td, dnp, DN_SPILL_BLKPTR(dnp), &czb);
}
2015-12-22 02:31:57 +01:00
if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
ZB_DNODE_BLKID);
2019-06-19 09:48:13 -07:00
err = td->td_func(td->td_spa, NULL, bp, &czb, dnp,
2015-12-22 02:31:57 +01:00
td->td_arg);
if (err == TRAVERSE_VISIT_NO_CHILDREN)
return (0);
if (err != 0)
return (err);
}
return (err);
2009-07-02 15:44:48 -07:00
}
static int
traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2008-11-20 12:01:55 -08:00
{
2021-12-12 16:06:44 +01:00
(void) zilog, (void) dnp;
2010-08-26 14:24:34 -07:00
prefetch_data_t *pfd = arg;
2017-08-14 13:36:48 -04:00
int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2017-11-15 20:27:01 -05:00
arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
ARC_FLAG_PRESCIENT_PREFETCH;
2008-11-20 12:01:55 -08:00
ASSERT(pfd->pd_bytes_fetched >= 0);
2019-06-19 09:48:13 -07:00
if (zb->zb_level == ZB_DNODE_LEVEL)
2015-12-22 02:31:57 +01:00
return (0);
if (pfd->pd_cancel)
2013-03-08 10:41:28 -08:00
return (SET_ERROR(EINTR));
2008-11-20 12:01:55 -08:00
if (!prefetch_needed(pfd, bp))
2008-11-20 12:01:55 -08:00
return (0);
mutex_enter(&pfd->pd_mtx);
while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
2015-06-09 16:39:25 -07:00
cv_wait_sig(&pfd->pd_cv, &pfd->pd_mtx);
pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
cv_broadcast(&pfd->pd_cv);
mutex_exit(&pfd->pd_mtx);
2008-11-20 12:01:55 -08:00
2017-08-14 13:36:48 -04:00
if ((pfd->pd_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
zio_flags |= ZIO_FLAG_RAW;
2013-07-02 13:26:24 -07:00
(void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
2017-08-14 13:36:48 -04:00
zio_flags, &aflags, zb);
2008-11-20 12:01:55 -08:00
return (0);
2008-11-20 12:01:55 -08:00
}
static void
traverse_prefetch_thread(void *arg)
2008-11-20 12:01:55 -08:00
{
2010-08-26 14:24:34 -07:00
traverse_data_t *td_main = arg;
traverse_data_t td = *td_main;
zbookmark_phys_t czb;
fstrans_cookie_t cookie = spl_fstrans_mark();
2008-11-20 12:01:55 -08:00
td.td_func = traverse_prefetcher;
td.td_arg = td_main->td_pfd;
td.td_pfd = NULL;
2016-01-06 22:22:48 +01:00
td.td_resume = &td_main->td_pfd->pd_resume;
2008-11-20 12:01:55 -08:00
SET_BOOKMARK(&czb, td.td_objset,
ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
2013-07-02 13:26:24 -07:00
(void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
2008-11-20 12:01:55 -08:00
mutex_enter(&td_main->td_pfd->pd_mtx);
td_main->td_pfd->pd_exited = B_TRUE;
cv_broadcast(&td_main->td_pfd->pd_cv);
mutex_exit(&td_main->td_pfd->pd_mtx);
spl_fstrans_unmark(cookie);
2008-11-20 12:01:55 -08:00
}
/*
* NB: dataset must not be changing on-disk (eg, is a snapshot or we are
* in syncing context).
*/
static int
2012-12-13 15:24:15 -08:00
traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
uint64_t txg_start, zbookmark_phys_t *resume, int flags,
2012-12-13 15:24:15 -08:00
blkptr_cb_t func, void *arg)
2008-11-20 12:01:55 -08:00
{
2010-08-26 10:58:47 -07:00
traverse_data_t *td;
prefetch_data_t *pd;
zbookmark_phys_t *czb;
int err;
2008-11-20 12:01:55 -08:00
2012-12-13 15:24:15 -08:00
ASSERT(ds == NULL || objset == ds->ds_object);
ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
2014-11-20 19:09:39 -05:00
td = kmem_alloc(sizeof (traverse_data_t), KM_SLEEP);
pd = kmem_zalloc(sizeof (prefetch_data_t), KM_SLEEP);
czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
2010-08-26 10:58:47 -07:00
td->td_spa = spa;
2012-12-13 15:24:15 -08:00
td->td_objset = objset;
2010-08-26 10:58:47 -07:00
td->td_rootbp = rootbp;
td->td_min_txg = txg_start;
2012-12-13 15:24:15 -08:00
td->td_resume = resume;
2010-08-26 10:58:47 -07:00
td->td_func = func;
td->td_arg = arg;
td->td_pfd = pd;
td->td_flags = flags;
td->td_paused = B_FALSE;
td->td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
2015-05-14 17:41:29 -06:00
if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
VERIFY(spa_feature_enabled_txg(spa,
SPA_FEATURE_HOLE_BIRTH, &td->td_hole_birth_enabled_txg));
} else {
td->td_hole_birth_enabled_txg = UINT64_MAX;
2015-05-14 17:41:29 -06:00
}
2010-08-26 10:58:47 -07:00
pd->pd_flags = flags;
2016-01-06 22:22:48 +01:00
if (resume != NULL)
pd->pd_resume = *resume;
2010-08-26 10:58:47 -07:00
mutex_init(&pd->pd_mtx, NULL, MUTEX_DEFAULT, NULL);
cv_init(&pd->pd_cv, NULL, CV_DEFAULT, NULL);
2013-09-06 16:09:05 -07:00
SET_BOOKMARK(czb, td->td_objset,
ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
2010-08-26 14:24:34 -07:00
/* See comment on ZIL traversal in dsl_scan_visitds. */
if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
2022-10-27 09:54:54 -07:00
zio_flag_t zio_flags = ZIO_FLAG_CANFAIL;
2025-05-13 17:26:55 -04:00
arc_flags_t flags = ARC_FLAG_WAIT;
2013-09-04 07:00:57 -05:00
objset_phys_t *osp;
arc_buf_t *buf;
2019-06-19 09:48:13 -07:00
ASSERT(!BP_IS_REDACTED(rootbp));
2010-08-26 14:24:34 -07:00
2017-08-14 13:36:48 -04:00
if ((td->td_flags & TRAVERSE_NO_DECRYPT) &&
BP_IS_PROTECTED(rootbp))
zio_flags |= ZIO_FLAG_RAW;
err = arc_read(NULL, td->td_spa, rootbp, arc_getbuf_func,
&buf, ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, czb);
if (err != 0) {
/*
* If both TRAVERSE_HARD and TRAVERSE_PRE are set,
* continue to visitbp so that td_func can be called
* in pre stage, and err will reset to zero.
*/
if (!(td->td_flags & TRAVERSE_HARD) ||
!(td->td_flags & TRAVERSE_PRE))
2018-08-15 09:53:44 -07:00
goto out;
} else {
osp = buf->b_data;
traverse_zil(td, &osp->os_zil_header);
arc_buf_destroy(buf, &buf);
}
2010-08-26 14:24:34 -07:00
}
if (!(flags & TRAVERSE_PREFETCH_DATA) ||
2018-03-30 21:10:01 +02:00
taskq_dispatch(spa->spa_prefetch_taskq, traverse_prefetch_thread,
2016-10-28 22:40:14 +00:00
td, TQ_NOQUEUE) == TASKQID_INVALID)
2010-08-26 10:58:47 -07:00
pd->pd_exited = B_TRUE;
2013-07-02 13:26:24 -07:00
err = traverse_visitbp(td, NULL, rootbp, czb);
2010-08-26 10:58:47 -07:00
mutex_enter(&pd->pd_mtx);
pd->pd_cancel = B_TRUE;
cv_broadcast(&pd->pd_cv);
while (!pd->pd_exited)
2015-06-09 16:39:25 -07:00
cv_wait_sig(&pd->pd_cv, &pd->pd_mtx);
2010-08-26 10:58:47 -07:00
mutex_exit(&pd->pd_mtx);
2018-08-15 09:53:44 -07:00
out:
2010-08-26 10:58:47 -07:00
mutex_destroy(&pd->pd_mtx);
cv_destroy(&pd->pd_cv);
kmem_free(czb, sizeof (zbookmark_phys_t));
kmem_free(pd, sizeof (struct prefetch_data));
kmem_free(td, sizeof (struct traverse_data));
2008-11-20 12:01:55 -08:00
return (err);
2008-11-20 12:01:55 -08:00
}
/*
* NB: dataset must not be changing on-disk (eg, is a snapshot or we are
* in syncing context).
*/
int
2016-01-06 22:22:48 +01:00
traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
zbookmark_phys_t *resume,
int flags, blkptr_cb_t func, void *arg)
2008-11-20 12:01:55 -08:00
{
2012-12-13 15:24:15 -08:00
return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
2016-01-06 22:22:48 +01:00
&dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
}
int
traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
int flags, blkptr_cb_t func, void *arg)
{
return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
2012-12-13 15:24:15 -08:00
}
int
traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
uint64_t txg_start, zbookmark_phys_t *resume, int flags,
2012-12-13 15:24:15 -08:00
blkptr_cb_t func, void *arg)
{
return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
blkptr, txg_start, resume, flags, func, arg));
2008-11-20 12:01:55 -08:00
}
/*
* NB: pool must not be changing on-disk (eg, from zdb or sync context).
*/
int
traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
blkptr_cb_t func, void *arg)
2008-11-20 12:01:55 -08:00
{
int err;
dsl_pool_t *dp = spa_get_dsl(spa);
objset_t *mos = dp->dp_meta_objset;
boolean_t hard = (flags & TRAVERSE_HARD);
/* visit the MOS */
2012-12-13 15:24:15 -08:00
err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
txg_start, NULL, flags, func, arg);
2013-09-04 07:00:57 -05:00
if (err != 0)
return (err);
/* visit each dataset */
2017-11-04 14:25:13 -06:00
for (uint64_t obj = 1; err == 0;
2016-01-06 22:22:48 +01:00
err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
dmu_object_info_t doi;
err = dmu_object_info(mos, obj, &doi);
2013-09-04 07:00:57 -05:00
if (err != 0) {
if (hard)
continue;
break;
}
2013-10-08 09:13:05 -08:00
if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
dsl_dataset_t *ds;
uint64_t txg = txg_start;
2013-09-04 07:00:57 -05:00
dsl_pool_config_enter(dp, FTAG);
err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2013-09-04 07:00:57 -05:00
dsl_pool_config_exit(dp, FTAG);
if (err != 0) {
if (hard)
continue;
break;
}
if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
err = traverse_dataset(ds, txg, flags, func, arg);
dsl_dataset_rele(ds, FTAG);
if (err != 0)
break;
}
2008-11-20 12:01:55 -08:00
}
if (err == ESRCH)
err = 0;
return (err);
2008-11-20 12:01:55 -08:00
}
2010-08-26 11:49:16 -07:00
EXPORT_SYMBOL(traverse_dataset);
EXPORT_SYMBOL(traverse_pool);
2011-05-03 15:09:28 -07:00
2019-09-05 14:49:49 -07:00
ZFS_MODULE_PARAM(zfs, zfs_, pd_bytes_max, INT, ZMOD_RW,
"Max number of bytes to prefetch");
2016-07-08 16:51:50 -04:00
ZFS_MODULE_PARAM(zfs, zfs_, traverse_indirect_prefetch_limit, UINT, ZMOD_RW,
"Traverse prefetch number of blocks pointed by indirect block");
2019-09-05 14:49:49 -07:00
ZFS_MODULE_PARAM(zfs, , send_holes_without_birth_time, INT, ZMOD_RW,
2016-10-07 21:02:24 -07:00
"Ignore hole_birth txg for zfs send");