Skip to content

Commit d88850b

Browse files
committed
xfs: fix high key handling in the rt allocator's query_range function
Fix some off-by-one errors in xfs_rtalloc_query_range. The highest key in the realtime bitmap is always one less than the number of rt extents, which means that the key clamp at the start of the function is wrong. The 4th argument to xfs_rtfind_forw is the highest rt extent that we want to probe, which means that passing 1 less than the high key is wrong. Finally, drop the rem variable that controls the loop because we can compare the iteration point (rtstart) against the high key directly. The sordid history of this function is that the original commit (fb3c3) incorrectly passed (high_rec->ar_startblock - 1) as the 'limit' parameter to xfs_rtfind_forw. This was wrong because the "high key" is supposed to be the largest key for which the caller wants result rows, not the key for the first row that could possibly be outside the range that the caller wants to see. A subsequent attempt (8ad56) to strengthen the parameter checking added incorrect clamping of the parameters to the number of rt blocks in the system (despite the bitmap functions all taking units of rt extents) to avoid querying ranges past the end of rt bitmap file but failed to fix the incorrect _rtfind_forw parameter. The original _rtfind_forw parameter error then survived the conversion of the startblock and blockcount fields to rt extents (a0e5c), and the most recent off-by-one fix (a3a37) thought it was patching a problem when the end of the rt volume is not in use, but none of these fixes actually solved the original problem that the author was confused about the "limit" argument to xfs_rtfind_forw. Sadly, all four of these patches were written by this author and even his own usage of this function and rt testing were inadequate to get this fixed quickly. Original-problem: fb3c3de ("xfs: add a couple of queries to iterate free extents in the rtbitmap") Not-fixed-by: 8ad560d ("xfs: strengthen rtalloc query range checks") Not-fixed-by: a0e5c43 ("xfs: fix xfs_rtalloc_rec units") Fixes: a3a374b ("xfs: fix off-by-one error in xfs_rtalloc_query_range") Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Chandan Babu R <[email protected]>
1 parent ace74e7 commit d88850b

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

fs/xfs/libxfs/xfs_rtbitmap.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,6 @@ xfs_rtalloc_query_range(
10181018
struct xfs_mount *mp = tp->t_mountp;
10191019
xfs_rtblock_t rtstart;
10201020
xfs_rtblock_t rtend;
1021-
xfs_rtblock_t rem;
10221021
int is_free;
10231022
int error = 0;
10241023

@@ -1027,13 +1026,12 @@ xfs_rtalloc_query_range(
10271026
if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
10281027
low_rec->ar_startext == high_rec->ar_startext)
10291028
return 0;
1030-
if (high_rec->ar_startext > mp->m_sb.sb_rextents)
1031-
high_rec->ar_startext = mp->m_sb.sb_rextents;
1029+
high_rec->ar_startext = min(high_rec->ar_startext,
1030+
mp->m_sb.sb_rextents - 1);
10321031

10331032
/* Iterate the bitmap, looking for discrepancies. */
10341033
rtstart = low_rec->ar_startext;
1035-
rem = high_rec->ar_startext - rtstart;
1036-
while (rem) {
1034+
while (rtstart <= high_rec->ar_startext) {
10371035
/* Is the first block free? */
10381036
error = xfs_rtcheck_range(mp, tp, rtstart, 1, 1, &rtend,
10391037
&is_free);
@@ -1042,7 +1040,7 @@ xfs_rtalloc_query_range(
10421040

10431041
/* How long does the extent go for? */
10441042
error = xfs_rtfind_forw(mp, tp, rtstart,
1045-
high_rec->ar_startext - 1, &rtend);
1043+
high_rec->ar_startext, &rtend);
10461044
if (error)
10471045
break;
10481046

@@ -1055,7 +1053,6 @@ xfs_rtalloc_query_range(
10551053
break;
10561054
}
10571055

1058-
rem -= rtend - rtstart + 1;
10591056
rtstart = rtend + 1;
10601057
}
10611058

0 commit comments

Comments
 (0)