Skip to content

Commit 26b0f56

Browse files
authored
dnode_next_offset: backtrack if lower level does not match
This changes the basic search algorithm from a single search up and down the tree to a full depth-first traversal to handle conditions where the tree matches at a higher level but not a lower level. Normally higher level blocks always point to matching blocks, but there are cases where this does not happen: 1. Racing block pointer updates from dbuf_write_ready. Before f664f1e (#8946), both dbuf_write_ready and dnode_next_offset held dn_struct_rwlock which protected against pointer writes from concurrent syncs. This no longer applies, so sync context can f.e. clear or fill all L1->L0 BPs before the L2->L1 BP and higher BP's are updated. dnode_free_range in particular can reach this case and skip over L1 blocks that need to be dirtied. Later, sync will panic in free_children when trying to clear a non-dirty indirect block. This case was found with ztest. 2. txg > 0, non-hole case. This is #11196. Freeing blocks/dnodes breaks the assumption that a match at a higher level implies a match at a lower level when filtering txg > 0. Whenever some but not all L0 blocks are freed, the parent L1 block is rewritten. Its updated L2->L1 BP reflects a newer birth txg. Later when searching by txg, if the L1 block matches since the txg is newer, it is possible that none of the remaining L1->L0 BPs match if none have been updated. The same behavior is possible with dnode search at L0. This is reachable from dsl_destroy_head for synchronous freeing. When this happens open context fails to free objects leaving sync context stuck freeing potentially many objects. This is also reachable from traverse_pool for extreme rewind where it is theoretically possible that datasets not dirtied after txg are skipped if the MOS has high enough indirection to trigger this case. In both of these cases, without backtracking the search ends prematurely as ESRCH result implies no more matches in the entire object. Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Akash B <[email protected]> Signed-off-by: Robert Evans <[email protected]> Closes #16025 Closes #11196
1 parent c722bf8 commit 26b0f56

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

module/zfs/dnode.c

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,32 @@ dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
26552655
return (error);
26562656
}
26572657

2658+
/*
2659+
* Adjust *offset to the next (or previous) block byte offset at lvl.
2660+
* Returns FALSE if *offset would overflow or underflow.
2661+
*/
2662+
static boolean_t
2663+
dnode_next_block(dnode_t *dn, int flags, uint64_t *offset, int lvl)
2664+
{
2665+
int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2666+
int span = lvl * epbs + dn->dn_datablkshift;
2667+
uint64_t blkid, maxblkid;
2668+
2669+
if (span >= 8 * sizeof (uint64_t))
2670+
return (B_FALSE);
2671+
2672+
blkid = *offset >> span;
2673+
maxblkid = 1ULL << (8 * sizeof (*offset) - span);
2674+
if (!(flags & DNODE_FIND_BACKWARDS) && blkid + 1 < maxblkid)
2675+
*offset = (blkid + 1) << span;
2676+
else if ((flags & DNODE_FIND_BACKWARDS) && blkid > 0)
2677+
*offset = (blkid << span) - 1;
2678+
else
2679+
return (B_FALSE);
2680+
2681+
return (B_TRUE);
2682+
}
2683+
26582684
/*
26592685
* Find the next hole, data, or sparse region at or after *offset.
26602686
* The value 'blkfill' tells us how many items we expect to find
@@ -2682,7 +2708,7 @@ int
26822708
dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
26832709
int minlvl, uint64_t blkfill, uint64_t txg)
26842710
{
2685-
uint64_t initial_offset = *offset;
2711+
uint64_t matched = *offset;
26862712
int lvl, maxlvl;
26872713
int error = 0;
26882714

@@ -2706,16 +2732,36 @@ dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
27062732

27072733
maxlvl = dn->dn_phys->dn_nlevels;
27082734

2709-
for (lvl = minlvl; lvl <= maxlvl; lvl++) {
2735+
for (lvl = minlvl; lvl <= maxlvl; ) {
27102736
error = dnode_next_offset_level(dn,
27112737
flags, offset, lvl, blkfill, txg);
2712-
if (error != ESRCH)
2738+
if (error == 0 && lvl > minlvl) {
2739+
--lvl;
2740+
matched = *offset;
2741+
} else if (error == ESRCH && lvl < maxlvl &&
2742+
dnode_next_block(dn, flags, &matched, lvl)) {
2743+
/*
2744+
* Continue search at next/prev offset in lvl+1 block.
2745+
*
2746+
* Usually we only search upwards at the start of the
2747+
* search as higher level blocks point at a matching
2748+
* minlvl block in most cases, but we backtrack if not.
2749+
*
2750+
* This can happen for txg > 0 searches if the block
2751+
* contains only BPs/dnodes freed at that txg. It also
2752+
* happens if we are still syncing out the tree, and
2753+
* some BP's at higher levels are not updated yet.
2754+
*
2755+
* We must adjust offset to avoid coming back to the
2756+
* same offset and getting stuck looping forever. This
2757+
* also deals with the case where offset is already at
2758+
* the beginning or end of the object.
2759+
*/
2760+
++lvl;
2761+
*offset = matched;
2762+
} else {
27132763
break;
2714-
}
2715-
2716-
while (error == 0 && --lvl >= minlvl) {
2717-
error = dnode_next_offset_level(dn,
2718-
flags, offset, lvl, blkfill, txg);
2764+
}
27192765
}
27202766

27212767
/*
@@ -2727,9 +2773,6 @@ dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
27272773
error = 0;
27282774
}
27292775

2730-
if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
2731-
initial_offset < *offset : initial_offset > *offset))
2732-
error = SET_ERROR(ESRCH);
27332776
out:
27342777
if (!(flags & DNODE_FIND_HAVELOCK))
27352778
rw_exit(&dn->dn_struct_rwlock);

0 commit comments

Comments
 (0)