Skip to content

Commit

Permalink
[XFS] implement generic xfs_btree_lshift
Browse files Browse the repository at this point in the history
Make the btree left shift code generic. Based on a patch from David
Chinner with lots of changes to follow the original btree implementations
more closely. While this loses some of the generic helper routines for
inserting/moving/removing records it also solves some of the one off bugs
in the original code and makes it easier to verify.

SGI-PV: 985583

SGI-Modid: xfs-linux-melb:xfs-kern:32197a

Signed-off-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Lachlan McIlroy <lachlan@sgi.com>
Signed-off-by: Bill O'Donnell <billodo@sgi.com>
Signed-off-by: David Chinner <david@fromorbit.com>
  • Loading branch information
Christoph Hellwig authored and Lachlan McIlroy committed Oct 30, 2008
1 parent 9eaead5 commit 687b890
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 425 deletions.
146 changes: 2 additions & 144 deletions fs/xfs/xfs_alloc_btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
Expand Down Expand Up @@ -326,7 +325,7 @@ xfs_alloc_delrec(
*/
if (be16_to_cpu(right->bb_numrecs) - 1 >=
XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
if ((error = xfs_alloc_lshift(tcur, level, &i)))
if ((error = xfs_btree_lshift(tcur, level, &i)))
goto error0;
if (i) {
ASSERT(be16_to_cpu(block->bb_numrecs) >=
Expand Down Expand Up @@ -691,7 +690,7 @@ xfs_alloc_insrec(
* Next, try shifting an entry to the left neighbor.
*/
else {
if ((error = xfs_alloc_lshift(cur, level, &i)))
if ((error = xfs_btree_lshift(cur, level, &i)))
return error;
if (i)
optr = ptr = cur->bc_ptrs[level];
Expand Down Expand Up @@ -935,147 +934,6 @@ xfs_alloc_log_recs(
xfs_trans_log_buf(cur->bc_tp, bp, first, last);
}

/*
* Move 1 record left from cur/level if possible.
* Update cur to reflect the new path.
*/
STATIC int /* error */
xfs_alloc_lshift(
xfs_btree_cur_t *cur, /* btree cursor */
int level, /* level to shift record on */
int *stat) /* success/failure */
{
int error; /* error return value */
#ifdef DEBUG
int i; /* loop index */
#endif
xfs_alloc_key_t key; /* key value for leaf level upward */
xfs_buf_t *lbp; /* buffer for left neighbor block */
xfs_alloc_block_t *left; /* left neighbor btree block */
int nrec; /* new number of left block entries */
xfs_buf_t *rbp; /* buffer for right (current) block */
xfs_alloc_block_t *right; /* right (current) btree block */
xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */

/*
* Set up variables for this block as "right".
*/
rbp = cur->bc_bufs[level];
right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
#ifdef DEBUG
if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
return error;
#endif
/*
* If we've got no left sibling then we can't shift an entry left.
*/
if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
*stat = 0;
return 0;
}
/*
* If the cursor entry is the one that would be moved, don't
* do it... it's too complicated.
*/
if (cur->bc_ptrs[level] <= 1) {
*stat = 0;
return 0;
}
/*
* Set up the left neighbor as "left".
*/
if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
0, &lbp, XFS_ALLOC_BTREE_REF)))
return error;
left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
return error;
/*
* If it's full, it can't take another entry.
*/
if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
*stat = 0;
return 0;
}
nrec = be16_to_cpu(left->bb_numrecs) + 1;
/*
* If non-leaf, copy a key and a ptr to the left block.
*/
if (level > 0) {
xfs_alloc_key_t *lkp; /* key pointer for left block */
xfs_alloc_ptr_t *lpp; /* address pointer for left block */

lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
*lkp = *rkp;
xfs_alloc_log_keys(cur, lbp, nrec, nrec);
lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
#ifdef DEBUG
if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
return error;
#endif
*lpp = *rpp;
xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
}
/*
* If leaf, copy a record to the left block.
*/
else {
xfs_alloc_rec_t *lrp; /* record pointer for left block */

lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
*lrp = *rrp;
xfs_alloc_log_recs(cur, lbp, nrec, nrec);
xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
}
/*
* Bump and log left's numrecs, decrement and log right's numrecs.
*/
be16_add_cpu(&left->bb_numrecs, 1);
xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
be16_add_cpu(&right->bb_numrecs, -1);
xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
/*
* Slide the contents of right down one entry.
*/
if (level > 0) {
#ifdef DEBUG
for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
level)))
return error;
}
#endif
memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
} else {
memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
key.ar_startblock = rrp->ar_startblock;
key.ar_blockcount = rrp->ar_blockcount;
rkp = &key;
}
/*
* Update the parent key values of right.
*/
if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1)))
return error;
/*
* Slide the cursor value left one.
*/
cur->bc_ptrs[level]--;
*stat = 1;
return 0;
}

/*
* Allocate a new root block, fill it in.
*/
Expand Down
138 changes: 2 additions & 136 deletions fs/xfs/xfs_bmap_btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
STATIC int xfs_bmbt_killroot(xfs_btree_cur_t *);
STATIC void xfs_bmbt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
STATIC void xfs_bmbt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
STATIC int xfs_bmbt_lshift(xfs_btree_cur_t *, int, int *);
STATIC int xfs_bmbt_split(xfs_btree_cur_t *, int, xfs_fsblock_t *,
__uint64_t *, xfs_btree_cur_t **, int *);

Expand Down Expand Up @@ -270,7 +269,7 @@ xfs_bmbt_delrec(
bno = be64_to_cpu(right->bb_leftsib);
if (be16_to_cpu(right->bb_numrecs) - 1 >=
XFS_BMAP_BLOCK_IMINRECS(level, cur)) {
if ((error = xfs_bmbt_lshift(tcur, level, &i))) {
if ((error = xfs_btree_lshift(tcur, level, &i))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
goto error0;
}
Expand Down Expand Up @@ -544,7 +543,7 @@ xfs_bmbt_insrec(
if (i) {
/* nothing */
} else {
if ((error = xfs_bmbt_lshift(cur, level, &i))) {
if ((error = xfs_btree_lshift(cur, level, &i))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
Expand Down Expand Up @@ -810,139 +809,6 @@ xfs_bmbt_log_ptrs(
XFS_BMBT_TRACE_CURSOR(cur, EXIT);
}

/*
* Move 1 record left from cur/level if possible.
* Update cur to reflect the new path.
*/
STATIC int /* error */
xfs_bmbt_lshift(
xfs_btree_cur_t *cur,
int level,
int *stat) /* success/failure */
{
int error; /* error return value */
#ifdef DEBUG
int i; /* loop counter */
#endif
xfs_bmbt_key_t key; /* bmap btree key */
xfs_buf_t *lbp; /* left buffer pointer */
xfs_bmbt_block_t *left; /* left btree block */
xfs_bmbt_key_t *lkp=NULL; /* left btree key */
xfs_bmbt_ptr_t *lpp; /* left address pointer */
int lrecs; /* left record count */
xfs_bmbt_rec_t *lrp=NULL; /* left record pointer */
xfs_mount_t *mp; /* file system mount point */
xfs_buf_t *rbp; /* right buffer pointer */
xfs_bmbt_block_t *right; /* right btree block */
xfs_bmbt_key_t *rkp=NULL; /* right btree key */
xfs_bmbt_ptr_t *rpp=NULL; /* right address pointer */
xfs_bmbt_rec_t *rrp=NULL; /* right record pointer */
int rrecs; /* right record count */

XFS_BMBT_TRACE_CURSOR(cur, ENTRY);
XFS_BMBT_TRACE_ARGI(cur, level);
if (level == cur->bc_nlevels - 1) {
XFS_BMBT_TRACE_CURSOR(cur, EXIT);
*stat = 0;
return 0;
}
rbp = cur->bc_bufs[level];
right = XFS_BUF_TO_BMBT_BLOCK(rbp);
#ifdef DEBUG
if ((error = xfs_btree_check_lblock(cur, right, level, rbp))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
#endif
if (be64_to_cpu(right->bb_leftsib) == NULLDFSBNO) {
XFS_BMBT_TRACE_CURSOR(cur, EXIT);
*stat = 0;
return 0;
}
if (cur->bc_ptrs[level] <= 1) {
XFS_BMBT_TRACE_CURSOR(cur, EXIT);
*stat = 0;
return 0;
}
mp = cur->bc_mp;
if ((error = xfs_btree_read_bufl(mp, cur->bc_tp, be64_to_cpu(right->bb_leftsib), 0,
&lbp, XFS_BMAP_BTREE_REF))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
left = XFS_BUF_TO_BMBT_BLOCK(lbp);
if ((error = xfs_btree_check_lblock(cur, left, level, lbp))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
if (be16_to_cpu(left->bb_numrecs) == XFS_BMAP_BLOCK_IMAXRECS(level, cur)) {
XFS_BMBT_TRACE_CURSOR(cur, EXIT);
*stat = 0;
return 0;
}
lrecs = be16_to_cpu(left->bb_numrecs) + 1;
if (level > 0) {
lkp = XFS_BMAP_KEY_IADDR(left, lrecs, cur);
rkp = XFS_BMAP_KEY_IADDR(right, 1, cur);
*lkp = *rkp;
xfs_bmbt_log_keys(cur, lbp, lrecs, lrecs);
lpp = XFS_BMAP_PTR_IADDR(left, lrecs, cur);
rpp = XFS_BMAP_PTR_IADDR(right, 1, cur);
#ifdef DEBUG
if ((error = xfs_btree_check_lptr_disk(cur, *rpp, level))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
#endif
*lpp = *rpp;
xfs_bmbt_log_ptrs(cur, lbp, lrecs, lrecs);
} else {
lrp = XFS_BMAP_REC_IADDR(left, lrecs, cur);
rrp = XFS_BMAP_REC_IADDR(right, 1, cur);
*lrp = *rrp;
xfs_bmbt_log_recs(cur, lbp, lrecs, lrecs);
}
left->bb_numrecs = cpu_to_be16(lrecs);
xfs_bmbt_log_block(cur, lbp, XFS_BB_NUMRECS);
#ifdef DEBUG
if (level > 0)
xfs_btree_check_key(XFS_BTNUM_BMAP, lkp - 1, lkp);
else
xfs_btree_check_rec(XFS_BTNUM_BMAP, lrp - 1, lrp);
#endif
rrecs = be16_to_cpu(right->bb_numrecs) - 1;
right->bb_numrecs = cpu_to_be16(rrecs);
xfs_bmbt_log_block(cur, rbp, XFS_BB_NUMRECS);
if (level > 0) {
#ifdef DEBUG
for (i = 0; i < rrecs; i++) {
if ((error = xfs_btree_check_lptr_disk(cur, rpp[i + 1],
level))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
}
#endif
memmove(rkp, rkp + 1, rrecs * sizeof(*rkp));
memmove(rpp, rpp + 1, rrecs * sizeof(*rpp));
xfs_bmbt_log_keys(cur, rbp, 1, rrecs);
xfs_bmbt_log_ptrs(cur, rbp, 1, rrecs);
} else {
memmove(rrp, rrp + 1, rrecs * sizeof(*rrp));
xfs_bmbt_log_recs(cur, rbp, 1, rrecs);
key.br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(rrp));
rkp = &key;
}
if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1))) {
XFS_BMBT_TRACE_CURSOR(cur, ERROR);
return error;
}
cur->bc_ptrs[level]--;
XFS_BMBT_TRACE_CURSOR(cur, EXIT);
*stat = 1;
return 0;
}

/*
* Determine the extent state.
*/
Expand Down
Loading

0 comments on commit 687b890

Please sign in to comment.