Skip to content

Commit 1b236ad

Browse files
author
Darrick J. Wong
committed
xfs: clean up xfs_btree_{calc_size,compute_maxlevels}
During review of the next patch, Dave remarked that he found these two btree geometry calculation functions lacking in documentation and that they performed more work than was really necessary. These functions take the same parameters and have nearly the same logic; the only real difference is in the return values. Reword the function comment to make it clearer what each function does, and move them to be adjacent to reinforce their relation. Clean up both of them to stop opencoding the howmany functions, stop using the uint typedefs, and make them both support computations for more than 2^32 leaf records, since we're going to need all of the above for files with large data forks and large rmap btrees. Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Dave Chinner <[email protected]>
1 parent b74e15d commit 1b236ad

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

fs/xfs/libxfs/xfs_btree.c

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4514,21 +4514,43 @@ xfs_btree_sblock_verify(
45144514
}
45154515

45164516
/*
4517-
* Calculate the number of btree levels needed to store a given number of
4518-
* records in a short-format btree.
4517+
* For the given limits on leaf and keyptr records per block, calculate the
4518+
* height of the tree needed to index the number of leaf records.
45194519
*/
4520-
uint
4520+
unsigned int
45214521
xfs_btree_compute_maxlevels(
4522-
uint *limits,
4523-
unsigned long len)
4522+
const unsigned int *limits,
4523+
unsigned long long records)
45244524
{
4525-
uint level;
4526-
unsigned long maxblocks;
4525+
unsigned long long level_blocks = howmany_64(records, limits[0]);
4526+
unsigned int height = 1;
45274527

4528-
maxblocks = (len + limits[0] - 1) / limits[0];
4529-
for (level = 1; maxblocks > 1; level++)
4530-
maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4531-
return level;
4528+
while (level_blocks > 1) {
4529+
level_blocks = howmany_64(level_blocks, limits[1]);
4530+
height++;
4531+
}
4532+
4533+
return height;
4534+
}
4535+
4536+
/*
4537+
* For the given limits on leaf and keyptr records per block, calculate the
4538+
* number of blocks needed to index the given number of leaf records.
4539+
*/
4540+
unsigned long long
4541+
xfs_btree_calc_size(
4542+
const unsigned int *limits,
4543+
unsigned long long records)
4544+
{
4545+
unsigned long long level_blocks = howmany_64(records, limits[0]);
4546+
unsigned long long blocks = level_blocks;
4547+
4548+
while (level_blocks > 1) {
4549+
level_blocks = howmany_64(level_blocks, limits[1]);
4550+
blocks += level_blocks;
4551+
}
4552+
4553+
return blocks;
45324554
}
45334555

45344556
/*
@@ -4822,29 +4844,6 @@ xfs_btree_query_all(
48224844
return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
48234845
}
48244846

4825-
/*
4826-
* Calculate the number of blocks needed to store a given number of records
4827-
* in a short-format (per-AG metadata) btree.
4828-
*/
4829-
unsigned long long
4830-
xfs_btree_calc_size(
4831-
uint *limits,
4832-
unsigned long long len)
4833-
{
4834-
int level;
4835-
int maxrecs;
4836-
unsigned long long rval;
4837-
4838-
maxrecs = limits[0];
4839-
for (level = 0, rval = 0; len > 1; level++) {
4840-
len += maxrecs - 1;
4841-
do_div(len, maxrecs);
4842-
maxrecs = limits[1];
4843-
rval += len;
4844-
}
4845-
return rval;
4846-
}
4847-
48484847
static int
48494848
xfs_btree_count_blocks_helper(
48504849
struct xfs_btree_cur *cur,

fs/xfs/libxfs/xfs_btree.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,10 @@ xfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
487487
xfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
488488
unsigned int max_recs);
489489

490-
uint xfs_btree_compute_maxlevels(uint *limits, unsigned long len);
491-
unsigned long long xfs_btree_calc_size(uint *limits, unsigned long long len);
490+
unsigned int xfs_btree_compute_maxlevels(const unsigned int *limits,
491+
unsigned long long records);
492+
unsigned long long xfs_btree_calc_size(const unsigned int *limits,
493+
unsigned long long records);
492494

493495
/*
494496
* Return codes for the query range iterator function are 0 to continue

0 commit comments

Comments
 (0)