aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2017-11-16 22:11:34 -0600
committerEric Sandeen <sandeen@redhat.com>2017-11-16 22:11:34 -0600
commit9788e059ce7c64ae5d7618e6e088625af2e335e2 (patch)
tree4ee7b789d90b3472cf38549f95aeec9a7531bc86
parent62ed733883db194245e78f6eaf4a1d59835a75a4 (diff)
downloadxfsprogs-dev-9788e059ce7c64ae5d7618e6e088625af2e335e2.tar.gz
xfs: introduce the xfs_iext_cursor abstraction
Source kernel commit: b2b1712a640824e7c131bfdd2585d57bf8ccb39a Add a new xfs_iext_cursor structure to hide the direct extent map index manipulations. In addition to the existing lookup/get/insert/ remove and update routines new primitives to get the first and last extent cursor, as well as moving up and down by one extent are provided. Also new are convenience to increment/decrement the cursor and retreive the new extent, as well as to peek into the previous/next extent without updating the cursor and last but not least a macro to iterate over all extents in a fork. [darrick: rename for_each_iext to for_each_xfs_iext] Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Brian Foster <bfoster@redhat.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> [sandeen: use cursor in xfs_repair code as well] Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
-rw-r--r--libxfs/xfs_bmap.c441
-rw-r--r--libxfs/xfs_bmap.h12
-rw-r--r--libxfs/xfs_inode_fork.c75
-rw-r--r--libxfs/xfs_inode_fork.h87
-rw-r--r--libxfs/xfs_types.h3
-rw-r--r--repair/phase6.c4
6 files changed, 344 insertions, 278 deletions
diff --git a/libxfs/xfs_bmap.c b/libxfs/xfs_bmap.c
index 0a1e5fb2da..0821397e48 100644
--- a/libxfs/xfs_bmap.c
+++ b/libxfs/xfs_bmap.c
@@ -663,8 +663,9 @@ xfs_bmap_extents_to_btree(
xfs_bmbt_key_t *kp; /* root block key pointer */
xfs_mount_t *mp; /* mount structure */
xfs_bmbt_ptr_t *pp; /* root block address pointer */
+ struct xfs_iext_cursor icur;
struct xfs_bmbt_irec rec;
- xfs_extnum_t i = 0, cnt = 0;
+ xfs_extnum_t cnt = 0;
mp = ip->i_mount;
ASSERT(whichfork != XFS_COW_FORK);
@@ -743,7 +744,7 @@ xfs_bmap_extents_to_btree(
XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
XFS_BTREE_LONG_PTRS);
- while (xfs_iext_get_extent(ifp, i++, &rec)) {
+ for_each_xfs_iext(ifp, &icur, &rec) {
if (isnullstartblock(rec.br_startblock))
continue;
arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
@@ -819,6 +820,7 @@ xfs_bmap_local_to_extents(
xfs_alloc_arg_t args; /* allocation arguments */
xfs_buf_t *bp; /* buffer for extent block */
struct xfs_bmbt_irec rec;
+ struct xfs_iext_cursor icur;
/*
* We don't want to deal with the case of keeping inode data inline yet.
@@ -885,7 +887,8 @@ xfs_bmap_local_to_extents(
rec.br_startblock = args.fsbno;
rec.br_blockcount = 1;
rec.br_state = XFS_EXT_NORM;
- xfs_iext_insert(ip, 0, 1, &rec, 0);
+ xfs_iext_first(ifp, &icur);
+ xfs_iext_insert(ip, &icur, 1, &rec, 0);
XFS_IFORK_NEXT_SET(ip, whichfork, 1);
ip->i_d.di_nblocks = 1;
@@ -1165,6 +1168,7 @@ xfs_iread_extents(
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
struct xfs_btree_block *block = ifp->if_broot;
+ struct xfs_iext_cursor icur;
xfs_fsblock_t bno;
struct xfs_buf *bp;
xfs_extnum_t i, j;
@@ -1214,6 +1218,7 @@ xfs_iread_extents(
* Here with bp and block set to the leftmost leaf node in the tree.
*/
i = 0;
+ xfs_iext_first(ifp, &icur);
/*
* Loop over all leaf nodes. Copy information to the extent records.
@@ -1255,7 +1260,8 @@ xfs_iread_extents(
}
trp->l0 = be64_to_cpu(frp->l0);
trp->l1 = be64_to_cpu(frp->l1);
- trace_xfs_read_extent(ip, i, state, _THIS_IP_);
+ trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
+ xfs_iext_next(ifp, &icur);
}
xfs_trans_brelse(tp, bp);
bno = nextbno;
@@ -1303,7 +1309,7 @@ xfs_bmap_first_unused(
{
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
struct xfs_bmbt_irec got;
- xfs_extnum_t idx = 0;
+ struct xfs_iext_cursor icur;
xfs_fileoff_t lastaddr = 0;
xfs_fileoff_t lowest, max;
int error;
@@ -1324,7 +1330,7 @@ xfs_bmap_first_unused(
}
lowest = max = *first_unused;
- while (xfs_iext_get_extent(ifp, idx++, &got)) {
+ for_each_xfs_iext(ifp, &icur, &got) {
/*
* See if the hole before this extent will work.
*/
@@ -1354,7 +1360,7 @@ xfs_bmap_last_before(
{
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
struct xfs_bmbt_irec got;
- xfs_extnum_t idx;
+ struct xfs_iext_cursor icur;
int error;
switch (XFS_IFORK_FORMAT(ip, whichfork)) {
@@ -1374,7 +1380,7 @@ xfs_bmap_last_before(
return error;
}
- if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &idx, &got))
+ if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
*last_block = 0;
return 0;
}
@@ -1388,8 +1394,8 @@ xfs_bmap_last_extent(
int *is_empty)
{
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
+ struct xfs_iext_cursor icur;
int error;
- int nextents;
if (!(ifp->if_flags & XFS_IFEXTENTS)) {
error = xfs_iread_extents(tp, ip, whichfork);
@@ -1397,14 +1403,11 @@ xfs_bmap_last_extent(
return error;
}
- nextents = xfs_iext_count(ifp);
- if (nextents == 0) {
+ xfs_iext_last(ifp, &icur);
+ if (!xfs_iext_get_extent(ifp, &icur, rec))
*is_empty = 1;
- return 0;
- }
-
- xfs_iext_get_extent(ifp, nextents - 1, rec);
- *is_empty = 0;
+ else
+ *is_empty = 0;
return 0;
}
@@ -1492,6 +1495,7 @@ xfs_bmap_one_block(
xfs_ifork_t *ifp; /* inode fork pointer */
int rval; /* return value */
xfs_bmbt_irec_t s; /* internal version of extent */
+ struct xfs_iext_cursor icur;
#ifndef DEBUG
if (whichfork == XFS_DATA_FORK)
@@ -1503,7 +1507,8 @@ xfs_bmap_one_block(
return 0;
ifp = XFS_IFORK_PTR(ip, whichfork);
ASSERT(ifp->if_flags & XFS_IFEXTENTS);
- xfs_iext_get_extent(ifp, 0, &s);
+ xfs_iext_first(ifp, &icur);
+ xfs_iext_get_extent(ifp, &icur, &s);
rval = s.br_startoff == 0 && s.br_blockcount == 1;
if (rval && whichfork == XFS_DATA_FORK)
ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
@@ -1545,8 +1550,6 @@ xfs_bmap_add_extent_delay_real(
nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
&bma->ip->i_d.di_nextents);
- ASSERT(bma->idx >= 0);
- ASSERT(bma->idx <= xfs_iext_count(ifp));
ASSERT(!isnullstartblock(new->br_startblock));
ASSERT(!bma->cur ||
(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
@@ -1560,7 +1563,7 @@ xfs_bmap_add_extent_delay_real(
/*
* Set up a bunch of variables to make the tests simpler.
*/
- xfs_iext_get_extent(ifp, bma->idx, &PREV);
+ xfs_iext_get_extent(ifp, &bma->icur, &PREV);
new_endoff = new->br_startoff + new->br_blockcount;
ASSERT(isnullstartblock(PREV.br_startblock));
ASSERT(PREV.br_startoff <= new->br_startoff);
@@ -1582,10 +1585,8 @@ xfs_bmap_add_extent_delay_real(
* Check and set flags if this segment has a left neighbor.
* Don't set contiguous if the combined extent would be too large.
*/
- if (bma->idx > 0) {
+ if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
state |= BMAP_LEFT_VALID;
- xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
-
if (isnullstartblock(LEFT.br_startblock))
state |= BMAP_LEFT_DELAY;
}
@@ -1602,10 +1603,8 @@ xfs_bmap_add_extent_delay_real(
* Don't set contiguous if the combined extent would be too large.
* Also check for all-three-contiguous being too large.
*/
- if (bma->idx < xfs_iext_count(ifp) - 1) {
+ if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
state |= BMAP_RIGHT_VALID;
- xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
-
if (isnullstartblock(RIGHT.br_startblock))
state |= BMAP_RIGHT_DELAY;
}
@@ -1637,9 +1636,9 @@ xfs_bmap_add_extent_delay_real(
*/
LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
- xfs_iext_remove(bma->ip, bma->idx, 2, state);
- bma->idx--;
- xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
+ xfs_iext_remove(bma->ip, &bma->icur, 2, state);
+ xfs_iext_prev(ifp, &bma->icur);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
(*nextents)--;
if (bma->cur == NULL)
@@ -1672,9 +1671,9 @@ xfs_bmap_add_extent_delay_real(
old = LEFT;
LEFT.br_blockcount += PREV.br_blockcount;
- xfs_iext_remove(bma->ip, bma->idx, 1, state);
- bma->idx--;
- xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
+ xfs_iext_remove(bma->ip, &bma->icur, 1, state);
+ xfs_iext_prev(ifp, &bma->icur);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
if (bma->cur == NULL)
rval = XFS_ILOG_DEXT;
@@ -1698,10 +1697,10 @@ xfs_bmap_add_extent_delay_real(
PREV.br_startblock = new->br_startblock;
PREV.br_blockcount += RIGHT.br_blockcount;
- bma->idx++;
- xfs_iext_remove(bma->ip, bma->idx, 1, state);
- bma->idx--;
- xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
+ xfs_iext_next(ifp, &bma->icur);
+ xfs_iext_remove(bma->ip, &bma->icur, 1, state);
+ xfs_iext_prev(ifp, &bma->icur);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
if (bma->cur == NULL)
rval = XFS_ILOG_DEXT;
@@ -1725,7 +1724,7 @@ xfs_bmap_add_extent_delay_real(
*/
PREV.br_startblock = new->br_startblock;
PREV.br_state = new->br_state;
- xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
(*nextents)++;
if (bma->cur == NULL)
@@ -1759,9 +1758,9 @@ xfs_bmap_add_extent_delay_real(
PREV.br_startoff += new->br_blockcount;
PREV.br_startblock = nullstartblock(da_new);
- xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
- bma->idx--;
- xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
+ xfs_iext_prev(ifp, &bma->icur);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
if (bma->cur == NULL)
rval = XFS_ILOG_DEXT;
@@ -1775,7 +1774,6 @@ xfs_bmap_add_extent_delay_real(
if (error)
goto done;
}
-
break;
case BMAP_LEFT_FILLING:
@@ -1783,7 +1781,7 @@ xfs_bmap_add_extent_delay_real(
* Filling in the first part of a previous delayed allocation.
* The left neighbor is not contiguous.
*/
- xfs_iext_update_extent(bma->ip, state, bma->idx, new);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
(*nextents)++;
if (bma->cur == NULL)
rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
@@ -1816,7 +1814,9 @@ xfs_bmap_add_extent_delay_real(
PREV.br_startoff = new_endoff;
PREV.br_blockcount = temp;
PREV.br_startblock = nullstartblock(da_new);
- xfs_iext_insert(bma->ip, bma->idx + 1, 1, &PREV, state);
+ xfs_iext_next(ifp, &bma->icur);
+ xfs_iext_insert(bma->ip, &bma->icur, 1, &PREV, state);
+ xfs_iext_prev(ifp, &bma->icur);
break;
case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
@@ -1849,9 +1849,9 @@ xfs_bmap_add_extent_delay_real(
PREV.br_blockcount = temp;
PREV.br_startblock = nullstartblock(da_new);
- xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
- bma->idx++;
- xfs_iext_update_extent(bma->ip, state, bma->idx, &RIGHT);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
+ xfs_iext_next(ifp, &bma->icur);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
break;
case BMAP_RIGHT_FILLING:
@@ -1859,7 +1859,7 @@ xfs_bmap_add_extent_delay_real(
* Filling in the last part of a previous delayed allocation.
* The right neighbor is not contiguous.
*/
- xfs_iext_update_extent(bma->ip, state, bma->idx, new);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
(*nextents)++;
if (bma->cur == NULL)
rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
@@ -1891,9 +1891,8 @@ xfs_bmap_add_extent_delay_real(
PREV.br_startblock = nullstartblock(da_new);
PREV.br_blockcount = temp;
- xfs_iext_insert(bma->ip, bma->idx, 1, &PREV, state);
-
- bma->idx++;
+ xfs_iext_insert(bma->ip, &bma->icur, 1, &PREV, state);
+ xfs_iext_next(ifp, &bma->icur);
break;
case 0:
@@ -1936,10 +1935,11 @@ xfs_bmap_add_extent_delay_real(
PREV.br_startblock =
nullstartblock(xfs_bmap_worst_indlen(bma->ip,
PREV.br_blockcount));
- xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
+ xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
/* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
- xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
+ xfs_iext_next(ifp, &bma->icur);
+ xfs_iext_insert(bma->ip, &bma->icur, 2, &LEFT, state);
(*nextents)++;
if (bma->cur == NULL)
@@ -1967,7 +1967,6 @@ xfs_bmap_add_extent_delay_real(
da_new = startblockval(PREV.br_startblock) +
startblockval(RIGHT.br_startblock);
- bma->idx++;
break;
case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
@@ -2031,7 +2030,7 @@ xfs_bmap_add_extent_unwritten_real(
struct xfs_trans *tp,
xfs_inode_t *ip, /* incore inode pointer */
int whichfork,
- xfs_extnum_t *idx, /* extent number to update/insert */
+ struct xfs_iext_cursor *icur,
xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
xfs_bmbt_irec_t *new, /* new data to add to file extents */
xfs_fsblock_t *first, /* pointer to firstblock variable */
@@ -2055,8 +2054,6 @@ xfs_bmap_add_extent_unwritten_real(
cur = *curp;
ifp = XFS_IFORK_PTR(ip, whichfork);
- ASSERT(*idx >= 0);
- ASSERT(*idx <= xfs_iext_count(ifp));
ASSERT(!isnullstartblock(new->br_startblock));
XFS_STATS_INC(mp, xs_add_exlist);
@@ -2069,7 +2066,7 @@ xfs_bmap_add_extent_unwritten_real(
* Set up a bunch of variables to make the tests simpler.
*/
error = 0;
- xfs_iext_get_extent(ifp, *idx, &PREV);
+ xfs_iext_get_extent(ifp, icur, &PREV);
ASSERT(new->br_state != PREV.br_state);
new_endoff = new->br_startoff + new->br_blockcount;
ASSERT(PREV.br_startoff <= new->br_startoff);
@@ -2088,10 +2085,8 @@ xfs_bmap_add_extent_unwritten_real(
* Check and set flags if this segment has a left neighbor.
* Don't set contiguous if the combined extent would be too large.
*/
- if (*idx > 0) {
+ if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
state |= BMAP_LEFT_VALID;
- xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
-
if (isnullstartblock(LEFT.br_startblock))
state |= BMAP_LEFT_DELAY;
}
@@ -2108,9 +2103,8 @@ xfs_bmap_add_extent_unwritten_real(
* Don't set contiguous if the combined extent would be too large.
* Also check for all-three-contiguous being too large.
*/
- if (*idx < xfs_iext_count(ifp) - 1) {
+ if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
state |= BMAP_RIGHT_VALID;
- xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
if (isnullstartblock(RIGHT.br_startblock))
state |= BMAP_RIGHT_DELAY;
}
@@ -2141,9 +2135,9 @@ xfs_bmap_add_extent_unwritten_real(
*/
LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
- xfs_iext_remove(ip, *idx, 2, state);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &LEFT);
+ xfs_iext_remove(ip, icur, 2, state);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &LEFT);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
if (cur == NULL)
@@ -2179,9 +2173,9 @@ xfs_bmap_add_extent_unwritten_real(
*/
LEFT.br_blockcount += PREV.br_blockcount;
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &LEFT);
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &LEFT);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
if (cur == NULL)
@@ -2212,10 +2206,10 @@ xfs_bmap_add_extent_unwritten_real(
PREV.br_blockcount += RIGHT.br_blockcount;
PREV.br_state = new->br_state;
- ++*idx;
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
@@ -2246,7 +2240,7 @@ xfs_bmap_add_extent_unwritten_real(
* the new one.
*/
PREV.br_state = new->br_state;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
if (cur == NULL)
rval = XFS_ILOG_DEXT;
@@ -2274,9 +2268,9 @@ xfs_bmap_add_extent_unwritten_real(
PREV.br_startblock += new->br_blockcount;
PREV.br_blockcount -= new->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &LEFT);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &LEFT);
if (cur == NULL)
rval = XFS_ILOG_DEXT;
@@ -2308,8 +2302,8 @@ xfs_bmap_add_extent_unwritten_real(
PREV.br_startblock += new->br_blockcount;
PREV.br_blockcount -= new->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
- xfs_iext_insert(ip, *idx, 1, new, state);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
+ xfs_iext_insert(ip, icur, 1, new, state);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
if (cur == NULL)
@@ -2342,9 +2336,9 @@ xfs_bmap_add_extent_unwritten_real(
RIGHT.br_startblock = new->br_startblock;
RIGHT.br_blockcount += new->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
- ++*idx;
- xfs_iext_update_extent(ip, state, *idx, &RIGHT);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &RIGHT);
if (cur == NULL)
rval = XFS_ILOG_DEXT;
@@ -2374,9 +2368,9 @@ xfs_bmap_add_extent_unwritten_real(
old = PREV;
PREV.br_blockcount -= new->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
- ++*idx;
- xfs_iext_insert(ip, *idx, 1, new, state);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_insert(ip, icur, 1, new, state);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
@@ -2417,9 +2411,9 @@ xfs_bmap_add_extent_unwritten_real(
r[1].br_startblock = new->br_startblock + new->br_blockcount;
r[1].br_state = PREV.br_state;
- xfs_iext_update_extent(ip, state, *idx, &PREV);
- ++*idx;
- xfs_iext_insert(ip, *idx, 2, &r[0], state);
+ xfs_iext_update_extent(ip, state, icur, &PREV);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_insert(ip, icur, 2, &r[0], state);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
@@ -2508,7 +2502,7 @@ STATIC void
xfs_bmap_add_extent_hole_delay(
xfs_inode_t *ip, /* incore inode pointer */
int whichfork,
- xfs_extnum_t *idx, /* extent number to update/insert */
+ struct xfs_iext_cursor *icur,
xfs_bmbt_irec_t *new) /* new data to add to file extents */
{
xfs_ifork_t *ifp; /* inode fork pointer */
@@ -2525,10 +2519,8 @@ xfs_bmap_add_extent_hole_delay(
/*
* Check and set flags if this segment has a left neighbor
*/
- if (*idx > 0) {
+ if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
state |= BMAP_LEFT_VALID;
- xfs_iext_get_extent(ifp, *idx - 1, &left);
-
if (isnullstartblock(left.br_startblock))
state |= BMAP_LEFT_DELAY;
}
@@ -2537,10 +2529,8 @@ xfs_bmap_add_extent_hole_delay(
* Check and set flags if the current (right) segment exists.
* If it doesn't exist, we're converting the hole at end-of-file.
*/
- if (*idx < xfs_iext_count(ifp)) {
+ if (xfs_iext_get_extent(ifp, icur, &right)) {
state |= BMAP_RIGHT_VALID;
- xfs_iext_get_extent(ifp, *idx, &right);
-
if (isnullstartblock(right.br_startblock))
state |= BMAP_RIGHT_DELAY;
}
@@ -2583,9 +2573,9 @@ xfs_bmap_add_extent_hole_delay(
left.br_startblock = nullstartblock(newlen);
left.br_blockcount = temp;
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &left);
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &left);
break;
case BMAP_LEFT_CONTIG:
@@ -2603,8 +2593,8 @@ xfs_bmap_add_extent_hole_delay(
left.br_blockcount = temp;
left.br_startblock = nullstartblock(newlen);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &left);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &left);
break;
case BMAP_RIGHT_CONTIG:
@@ -2621,7 +2611,7 @@ xfs_bmap_add_extent_hole_delay(
right.br_startoff = new->br_startoff;
right.br_startblock = nullstartblock(newlen);
right.br_blockcount = temp;
- xfs_iext_update_extent(ip, state, *idx, &right);
+ xfs_iext_update_extent(ip, state, icur, &right);
break;
case 0:
@@ -2631,7 +2621,7 @@ xfs_bmap_add_extent_hole_delay(
* Insert a new entry.
*/
oldlen = newlen = 0;
- xfs_iext_insert(ip, *idx, 1, new, state);
+ xfs_iext_insert(ip, icur, 1, new, state);
break;
}
if (oldlen != newlen) {
@@ -2652,7 +2642,7 @@ xfs_bmap_add_extent_hole_real(
struct xfs_trans *tp,
struct xfs_inode *ip,
int whichfork,
- xfs_extnum_t *idx,
+ struct xfs_iext_cursor *icur,
struct xfs_btree_cur **curp,
struct xfs_bmbt_irec *new,
xfs_fsblock_t *first,
@@ -2670,8 +2660,6 @@ xfs_bmap_add_extent_hole_real(
int state = xfs_bmap_fork_to_state(whichfork);
struct xfs_bmbt_irec old;
- ASSERT(*idx >= 0);
- ASSERT(*idx <= xfs_iext_count(ifp));
ASSERT(!isnullstartblock(new->br_startblock));
ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
@@ -2680,9 +2668,8 @@ xfs_bmap_add_extent_hole_real(
/*
* Check and set flags if this segment has a left neighbor.
*/
- if (*idx > 0) {
+ if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
state |= BMAP_LEFT_VALID;
- xfs_iext_get_extent(ifp, *idx - 1, &left);
if (isnullstartblock(left.br_startblock))
state |= BMAP_LEFT_DELAY;
}
@@ -2691,9 +2678,8 @@ xfs_bmap_add_extent_hole_real(
* Check and set flags if this segment has a current value.
* Not true if we're inserting into the "hole" at eof.
*/
- if (*idx < xfs_iext_count(ifp)) {
+ if (xfs_iext_get_extent(ifp, icur, &right)) {
state |= BMAP_RIGHT_VALID;
- xfs_iext_get_extent(ifp, *idx, &right);
if (isnullstartblock(right.br_startblock))
state |= BMAP_RIGHT_DELAY;
}
@@ -2732,9 +2718,9 @@ xfs_bmap_add_extent_hole_real(
*/
left.br_blockcount += new->br_blockcount + right.br_blockcount;
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &left);
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &left);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
@@ -2769,8 +2755,8 @@ xfs_bmap_add_extent_hole_real(
old = left;
left.br_blockcount += new->br_blockcount;
- --*idx;
- xfs_iext_update_extent(ip, state, *idx, &left);
+ xfs_iext_prev(ifp, icur);
+ xfs_iext_update_extent(ip, state, icur, &left);
if (cur == NULL) {
rval = xfs_ilog_fext(whichfork);
@@ -2797,7 +2783,7 @@ xfs_bmap_add_extent_hole_real(
right.br_startoff = new->br_startoff;
right.br_startblock = new->br_startblock;
right.br_blockcount += new->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &right);
+ xfs_iext_update_extent(ip, state, icur, &right);
if (cur == NULL) {
rval = xfs_ilog_fext(whichfork);
@@ -2819,7 +2805,7 @@ xfs_bmap_add_extent_hole_real(
* real allocation.
* Insert a new entry.
*/
- xfs_iext_insert(ip, *idx, 1, new, state);
+ xfs_iext_insert(ip, icur, 1, new, state);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
if (cur == NULL) {
@@ -3769,7 +3755,7 @@ xfs_bmapi_read(
struct xfs_bmbt_irec got;
xfs_fileoff_t obno;
xfs_fileoff_t end;
- xfs_extnum_t idx;
+ struct xfs_iext_cursor icur;
int error;
bool eof = false;
int n = 0;
@@ -3811,7 +3797,7 @@ xfs_bmapi_read(
return error;
}
- if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
+ if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
eof = true;
end = bno + len;
obno = bno;
@@ -3843,7 +3829,7 @@ xfs_bmapi_read(
break;
/* Else go on to the next record. */
- if (!xfs_iext_get_extent(ifp, ++idx, &got))
+ if (!xfs_iext_next_extent(ifp, &icur, &got))
eof = true;
}
*nmap = n;
@@ -3871,7 +3857,7 @@ xfs_bmapi_reserve_delalloc(
xfs_filblks_t len,
xfs_filblks_t prealloc,
struct xfs_bmbt_irec *got,
- xfs_extnum_t *lastx,
+ struct xfs_iext_cursor *icur,
int eof)
{
struct xfs_mount *mp = ip->i_mount;
@@ -3901,7 +3887,7 @@ xfs_bmapi_reserve_delalloc(
if (extsz) {
struct xfs_bmbt_irec prev;
- if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
+ if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
prev.br_startoff = NULLFILEOFF;
error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
@@ -3950,7 +3936,7 @@ xfs_bmapi_reserve_delalloc(
got->br_blockcount = alen;
got->br_state = XFS_EXT_NORM;
- xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
+ xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
/*
* Tag the inode if blocks were preallocated. Note that COW fork
@@ -3995,8 +3981,7 @@ xfs_bmapi_allocate(
if (bma->wasdel) {
bma->length = (xfs_extlen_t)bma->got.br_blockcount;
bma->offset = bma->got.br_startoff;
- if (bma->idx)
- xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
+ xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
} else {
bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
if (!bma->eof)
@@ -4081,7 +4066,7 @@ xfs_bmapi_allocate(
error = xfs_bmap_add_extent_delay_real(bma, whichfork);
else
error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
- whichfork, &bma->idx, &bma->cur, &bma->got,
+ whichfork, &bma->icur, &bma->cur, &bma->got,
bma->firstblock, bma->dfops, &bma->logflags);
bma->logflags |= tmp_logflags;
@@ -4093,7 +4078,7 @@ xfs_bmapi_allocate(
* or xfs_bmap_add_extent_hole_real might have merged it into one of
* the neighbouring ones.
*/
- xfs_iext_get_extent(ifp, bma->idx, &bma->got);
+ xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
ASSERT(bma->got.br_startoff <= bma->offset);
ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
@@ -4151,8 +4136,8 @@ xfs_bmapi_convert_unwritten(
}
error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
- &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
- &tmp_logflags);
+ &bma->icur, &bma->cur, mval, bma->firstblock,
+ bma->dfops, &tmp_logflags);
/*
* Log the inode core unconditionally in the unwritten extent conversion
* path because the conversion might not have done so (e.g., if the
@@ -4174,7 +4159,7 @@ xfs_bmapi_convert_unwritten(
* xfs_bmap_add_extent_unwritten_real might have merged it into one
* of the neighbouring ones.
*/
- xfs_iext_get_extent(ifp, bma->idx, &bma->got);
+ xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
/*
* We may have combined previously unwritten space with written space,
@@ -4293,9 +4278,9 @@ xfs_bmapi_write(
end = bno + len;
obno = bno;
- if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
+ if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
eof = true;
- if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
+ if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
bma.prev.br_startoff = NULLFILEOFF;
bma.tp = tp;
bma.ip = ip;
@@ -4400,7 +4385,7 @@ xfs_bmapi_write(
/* Else go on to the next record. */
bma.prev = bma.got;
- if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
+ if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
eof = true;
}
*nmap = n;
@@ -4473,7 +4458,7 @@ xfs_bmapi_remap(
struct xfs_btree_cur *cur = NULL;
xfs_fsblock_t firstblock = NULLFSBLOCK;
struct xfs_bmbt_irec got;
- xfs_extnum_t idx;
+ struct xfs_iext_cursor icur;
int logflags = 0, error;
ASSERT(len > 0);
@@ -4497,7 +4482,7 @@ xfs_bmapi_remap(
return error;
}
- if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
+ if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
/* make sure we only reflink into a hole. */
ASSERT(got.br_startoff > bno);
ASSERT(got.br_startoff - bno >= len);
@@ -4518,8 +4503,8 @@ xfs_bmapi_remap(
got.br_blockcount = len;
got.br_state = XFS_EXT_NORM;
- error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
- &got, &firstblock, dfops, &logflags);
+ error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur,
+ &cur, &got, &firstblock, dfops, &logflags);
if (error)
goto error0;
@@ -4635,7 +4620,7 @@ int
xfs_bmap_del_extent_delay(
struct xfs_inode *ip,
int whichfork,
- xfs_extnum_t *idx,
+ struct xfs_iext_cursor *icur,
struct xfs_bmbt_irec *got,
struct xfs_bmbt_irec *del)
{
@@ -4657,8 +4642,6 @@ xfs_bmap_del_extent_delay(
da_old = startblockval(got->br_startblock);
da_new = 0;
- ASSERT(*idx >= 0);
- ASSERT(*idx <= xfs_iext_count(ifp));
ASSERT(del->br_blockcount > 0);
ASSERT(got->br_startoff <= del->br_startoff);
ASSERT(got_endoff >= del_endoff);
@@ -4692,8 +4675,8 @@ xfs_bmap_del_extent_delay(
/*
* Matches the whole extent. Delete the entry.
*/
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
break;
case BMAP_LEFT_FILLING:
/*
@@ -4704,7 +4687,7 @@ xfs_bmap_del_extent_delay(
da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
got->br_blockcount), da_old);
got->br_startblock = nullstartblock((int)da_new);
- xfs_iext_update_extent(ip, state, *idx, got);
+ xfs_iext_update_extent(ip, state, icur, got);
break;
case BMAP_RIGHT_FILLING:
/*
@@ -4714,7 +4697,7 @@ xfs_bmap_del_extent_delay(
da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
got->br_blockcount), da_old);
got->br_startblock = nullstartblock((int)da_new);
- xfs_iext_update_extent(ip, state, *idx, got);
+ xfs_iext_update_extent(ip, state, icur, got);
break;
case 0:
/*
@@ -4742,9 +4725,9 @@ xfs_bmap_del_extent_delay(
new.br_state = got->br_state;
new.br_startblock = nullstartblock((int)new_indlen);
- xfs_iext_update_extent(ip, state, *idx, got);
- ++*idx;
- xfs_iext_insert(ip, *idx, 1, &new, state);
+ xfs_iext_update_extent(ip, state, icur, got);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_insert(ip, icur, 1, &new, state);
da_new = got_indlen + new_indlen - stolen;
del->br_blockcount -= stolen;
@@ -4763,7 +4746,7 @@ xfs_bmap_del_extent_delay(
void
xfs_bmap_del_extent_cow(
struct xfs_inode *ip,
- xfs_extnum_t *idx,
+ struct xfs_iext_cursor *icur,
struct xfs_bmbt_irec *got,
struct xfs_bmbt_irec *del)
{
@@ -4778,8 +4761,6 @@ xfs_bmap_del_extent_cow(
del_endoff = del->br_startoff + del->br_blockcount;
got_endoff = got->br_startoff + got->br_blockcount;
- ASSERT(*idx >= 0);
- ASSERT(*idx <= xfs_iext_count(ifp));
ASSERT(del->br_blockcount > 0);
ASSERT(got->br_startoff <= del->br_startoff);
ASSERT(got_endoff >= del_endoff);
@@ -4795,8 +4776,8 @@ xfs_bmap_del_extent_cow(
/*
* Matches the whole extent. Delete the entry.
*/
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
break;
case BMAP_LEFT_FILLING:
/*
@@ -4805,14 +4786,14 @@ xfs_bmap_del_extent_cow(
got->br_startoff = del_endoff;
got->br_blockcount -= del->br_blockcount;
got->br_startblock = del->br_startblock + del->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, got);
+ xfs_iext_update_extent(ip, state, icur, got);
break;
case BMAP_RIGHT_FILLING:
/*
* Deleting the last part of the extent.
*/
got->br_blockcount -= del->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, got);
+ xfs_iext_update_extent(ip, state, icur, got);
break;
case 0:
/*
@@ -4825,9 +4806,9 @@ xfs_bmap_del_extent_cow(
new.br_state = got->br_state;
new.br_startblock = del->br_startblock + del->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, got);
- ++*idx;
- xfs_iext_insert(ip, *idx, 1, &new, state);
+ xfs_iext_update_extent(ip, state, icur, got);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_insert(ip, icur, 1, &new, state);
break;
}
}
@@ -4840,7 +4821,7 @@ STATIC int /* error */
xfs_bmap_del_extent_real(
xfs_inode_t *ip, /* incore inode pointer */
xfs_trans_t *tp, /* current transaction pointer */
- xfs_extnum_t *idx, /* extent number to update/delete */
+ struct xfs_iext_cursor *icur,
struct xfs_defer_ops *dfops, /* list of extents to be freed */
xfs_btree_cur_t *cur, /* if null, not a btree */
xfs_bmbt_irec_t *del, /* data to remove from extents */
@@ -4869,9 +4850,8 @@ xfs_bmap_del_extent_real(
XFS_STATS_INC(mp, xs_del_exlist);
ifp = XFS_IFORK_PTR(ip, whichfork);
- ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
ASSERT(del->br_blockcount > 0);
- xfs_iext_get_extent(ifp, *idx, &got);
+ xfs_iext_get_extent(ifp, icur, &got);
ASSERT(got.br_startoff <= del->br_startoff);
del_endoff = del->br_startoff + del->br_blockcount;
got_endoff = got.br_startoff + got.br_blockcount;
@@ -4936,9 +4916,8 @@ xfs_bmap_del_extent_real(
/*
* Matches the whole extent. Delete the entry.
*/
- xfs_iext_remove(ip, *idx, 1, state);
- --*idx;
-
+ xfs_iext_remove(ip, icur, 1, state);
+ xfs_iext_prev(ifp, icur);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
flags |= XFS_ILOG_CORE;
@@ -4957,7 +4936,7 @@ xfs_bmap_del_extent_real(
got.br_startoff = del_endoff;
got.br_startblock = del_endblock;
got.br_blockcount -= del->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &got);
+ xfs_iext_update_extent(ip, state, icur, &got);
if (!cur) {
flags |= xfs_ilog_fext(whichfork);
break;
@@ -4971,7 +4950,7 @@ xfs_bmap_del_extent_real(
* Deleting the last part of the extent.
*/
got.br_blockcount -= del->br_blockcount;
- xfs_iext_update_extent(ip, state, *idx, &got);
+ xfs_iext_update_extent(ip, state, icur, &got);
if (!cur) {
flags |= xfs_ilog_fext(whichfork);
break;
@@ -4987,7 +4966,7 @@ xfs_bmap_del_extent_real(
old = got;
got.br_blockcount = del->br_startoff - got.br_startoff;
- xfs_iext_update_extent(ip, state, *idx, &got);
+ xfs_iext_update_extent(ip, state, icur, &got);
new.br_startoff = del_endoff;
new.br_blockcount = got_endoff - del_endoff;
@@ -5031,7 +5010,7 @@ xfs_bmap_del_extent_real(
* Reset the extent record back
* to the original value.
*/
- xfs_iext_update_extent(ip, state, *idx, &old);
+ xfs_iext_update_extent(ip, state, icur, &old);
flags = 0;
error = -ENOSPC;
goto done;
@@ -5041,8 +5020,8 @@ xfs_bmap_del_extent_real(
flags |= xfs_ilog_fext(whichfork);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
- ++*idx;
- xfs_iext_insert(ip, *idx, 1, &new, state);
+ xfs_iext_next(ifp, icur);
+ xfs_iext_insert(ip, icur, 1, &new, state);
break;
}
@@ -5105,7 +5084,6 @@ __xfs_bunmapi(
xfs_bmbt_irec_t got; /* current extent record */
xfs_ifork_t *ifp; /* inode fork pointer */
int isrt; /* freeing in rt area */
- xfs_extnum_t lastx; /* last extent index used */
int logflags; /* transaction logging flags */
xfs_extlen_t mod; /* rt extent offset */
xfs_mount_t *mp; /* mount structure */
@@ -5117,6 +5095,8 @@ __xfs_bunmapi(
xfs_fileoff_t max_len;
xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
xfs_fileoff_t end;
+ struct xfs_iext_cursor icur;
+ bool done = false;
trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
@@ -5159,7 +5139,7 @@ __xfs_bunmapi(
isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
end = start + len;
- if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &lastx, &got)) {
+ if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
*rlen = 0;
return 0;
}
@@ -5186,16 +5166,16 @@ __xfs_bunmapi(
}
extno = 0;
- while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
+ while (end != (xfs_fileoff_t)-1 && end >= start &&
(nexts == 0 || extno < nexts) && max_len > 0) {
/*
* Is the found extent after a hole in which end lives?
* Just back up to the previous extent, if so.
*/
- if (got.br_startoff > end) {
- if (--lastx < 0)
- break;
- xfs_iext_get_extent(ifp, lastx, &got);
+ if (got.br_startoff > end &&
+ !xfs_iext_prev_extent(ifp, &icur, &got)) {
+ done = true;
+ break;
}
/*
* Is the last block of this extent before the range
@@ -5258,10 +5238,10 @@ __xfs_bunmapi(
ASSERT(end >= mod);
end -= mod > del.br_blockcount ?
del.br_blockcount : mod;
- if (end < got.br_startoff) {
- if (--lastx >= 0)
- xfs_iext_get_extent(ifp, lastx,
- &got);
+ if (end < got.br_startoff &&
+ !xfs_iext_prev_extent(ifp, &icur, &got)) {
+ done = true;
+ break;
}
continue;
}
@@ -5282,7 +5262,7 @@ __xfs_bunmapi(
}
del.br_state = XFS_EXT_UNWRITTEN;
error = xfs_bmap_add_extent_unwritten_real(tp, ip,
- whichfork, &lastx, &cur, &del,
+ whichfork, &icur, &cur, &del,
firstblock, dfops, &logflags);
if (error)
goto error0;
@@ -5309,8 +5289,11 @@ __xfs_bunmapi(
*/
ASSERT(end >= del.br_blockcount);
end -= del.br_blockcount;
- if (got.br_startoff > end && --lastx >= 0)
- xfs_iext_get_extent(ifp, lastx, &got);
+ if (got.br_startoff > end &&
+ !xfs_iext_prev_extent(ifp, &icur, &got)) {
+ done = true;
+ break;
+ }
continue;
} else if (del.br_state == XFS_EXT_UNWRITTEN) {
struct xfs_bmbt_irec prev;
@@ -5321,8 +5304,8 @@ __xfs_bunmapi(
* Unwrite the killed part of that one and
* try again.
*/
- ASSERT(lastx > 0);
- xfs_iext_get_extent(ifp, lastx - 1, &prev);
+ if (!xfs_iext_prev_extent(ifp, &icur, &prev))
+ ASSERT(0);
ASSERT(prev.br_state == XFS_EXT_NORM);
ASSERT(!isnullstartblock(prev.br_startblock));
ASSERT(del.br_startblock ==
@@ -5334,9 +5317,8 @@ __xfs_bunmapi(
prev.br_startoff = start;
}
prev.br_state = XFS_EXT_UNWRITTEN;
- lastx--;
error = xfs_bmap_add_extent_unwritten_real(tp,
- ip, whichfork, &lastx, &cur,
+ ip, whichfork, &icur, &cur,
&prev, firstblock, dfops,
&logflags);
if (error)
@@ -5346,7 +5328,7 @@ __xfs_bunmapi(
ASSERT(del.br_state == XFS_EXT_NORM);
del.br_state = XFS_EXT_UNWRITTEN;
error = xfs_bmap_add_extent_unwritten_real(tp,
- ip, whichfork, &lastx, &cur,
+ ip, whichfork, &icur, &cur,
&del, firstblock, dfops,
&logflags);
if (error)
@@ -5356,10 +5338,10 @@ __xfs_bunmapi(
}
if (wasdel) {
- error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
+ error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
&got, &del);
} else {
- error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
+ error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
cur, &del, &tmp_logflags, whichfork,
flags);
logflags |= tmp_logflags;
@@ -5375,15 +5357,16 @@ nodelete:
* If not done go on to the next (previous) record.
*/
if (end != (xfs_fileoff_t)-1 && end >= start) {
- if (lastx >= 0) {
- xfs_iext_get_extent(ifp, lastx, &got);
- if (got.br_startoff > end && --lastx >= 0)
- xfs_iext_get_extent(ifp, lastx, &got);
+ if (!xfs_iext_get_extent(ifp, &icur, &got) ||
+ (got.br_startoff > end &&
+ !xfs_iext_prev_extent(ifp, &icur, &got))) {
+ done = true;
+ break;
}
extno++;
}
}
- if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
+ if (done || end == (xfs_fileoff_t)-1 || end < start)
*rlen = 0;
else
*rlen = end - start + 1;
@@ -5504,7 +5487,7 @@ xfs_bmse_merge(
struct xfs_inode *ip,
int whichfork,
xfs_fileoff_t shift, /* shift fsb */
- int *current_ext, /* idx of gotp */
+ struct xfs_iext_cursor *icur,
struct xfs_bmbt_irec *got, /* extent to shift */
struct xfs_bmbt_irec *left, /* preceding extent */
struct xfs_btree_cur *cur,
@@ -5559,10 +5542,10 @@ xfs_bmse_merge(
return error;
done:
- xfs_iext_remove(ip, *current_ext, 1, 0);
- --*current_ext;
- xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
- *current_ext, &new);
+ xfs_iext_remove(ip, icur, 1, 0);
+ xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
+ xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
+ &new);
/* update reverse mapping. rmap functions merge the rmaps for us */
error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
@@ -5577,7 +5560,7 @@ static int
xfs_bmap_shift_update_extent(
struct xfs_inode *ip,
int whichfork,
- xfs_extnum_t idx,
+ struct xfs_iext_cursor *icur,
struct xfs_bmbt_irec *got,
struct xfs_btree_cur *cur,
int *logflags,
@@ -5605,7 +5588,8 @@ xfs_bmap_shift_update_extent(
*logflags |= XFS_ILOG_DEXT;
}
- xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got);
+ xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
+ got);
/* update reverse mapping */
error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
@@ -5630,7 +5614,7 @@ xfs_bmap_collapse_extents(
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
struct xfs_btree_cur *cur = NULL;
struct xfs_bmbt_irec got, prev;
- xfs_extnum_t current_ext;
+ struct xfs_iext_cursor icur;
xfs_fileoff_t new_startoff;
int error = 0;
int logflags = 0;
@@ -5661,14 +5645,14 @@ xfs_bmap_collapse_extents(
cur->bc_private.b.flags = 0;
}
- if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
+ if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
*done = true;
goto del_cursor;
}
XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
new_startoff = got.br_startoff - offset_shift_fsb;
- if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) {
+ if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
if (new_startoff < prev.br_startoff + prev.br_blockcount) {
error = -EINVAL;
goto del_cursor;
@@ -5676,8 +5660,8 @@ xfs_bmap_collapse_extents(
if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
- &current_ext, &got, &prev, cur,
- &logflags, dfops);
+ &icur, &got, &prev, cur, &logflags,
+ dfops);
if (error)
goto del_cursor;
goto done;
@@ -5689,15 +5673,15 @@ xfs_bmap_collapse_extents(
}
}
- error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
- cur, &logflags, dfops, new_startoff);
+ error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
+ &logflags, dfops, new_startoff);
if (error)
goto del_cursor;
done:
- if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) {
- *done = true;
- goto del_cursor;
+ if (!xfs_iext_next_extent(ifp, &icur, &got)) {
+ *done = true;
+ goto del_cursor;
}
*next_fsb = got.br_startoff;
@@ -5726,7 +5710,7 @@ xfs_bmap_insert_extents(
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
struct xfs_btree_cur *cur = NULL;
struct xfs_bmbt_irec got, next;
- xfs_extnum_t current_ext;
+ struct xfs_iext_cursor icur;
xfs_fileoff_t new_startoff;
int error = 0;
int logflags = 0;
@@ -5758,15 +5742,14 @@ xfs_bmap_insert_extents(
}
if (*next_fsb == NULLFSBLOCK) {
- current_ext = xfs_iext_count(ifp) - 1;
- if (!xfs_iext_get_extent(ifp, current_ext, &got) ||
+ xfs_iext_last(ifp, &icur);
+ if (!xfs_iext_get_extent(ifp, &icur, &got) ||
stop_fsb > got.br_startoff) {
*done = true;
goto del_cursor;
}
} else {
- if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
- &got)) {
+ if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
*done = true;
goto del_cursor;
}
@@ -5779,7 +5762,7 @@ xfs_bmap_insert_extents(
}
new_startoff = got.br_startoff + offset_shift_fsb;
- if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) {
+ if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
if (new_startoff + got.br_blockcount > next.br_startoff) {
error = -EINVAL;
goto del_cursor;
@@ -5795,12 +5778,12 @@ xfs_bmap_insert_extents(
WARN_ON_ONCE(1);
}
- error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
- cur, &logflags, dfops, new_startoff);
+ error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
+ &logflags, dfops, new_startoff);
if (error)
goto del_cursor;
- if (!xfs_iext_get_extent(ifp, --current_ext, &got) ||
+ if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
stop_fsb >= got.br_startoff + got.br_blockcount) {
*done = true;
goto del_cursor;
@@ -5817,10 +5800,10 @@ del_cursor:
}
/*
- * Splits an extent into two extents at split_fsb block such that it is
- * the first block of the current_ext. @current_ext is a target extent
- * to be split. @split_fsb is a block where the extents is split.
- * If split_fsb lies in a hole or the first block of extents, just return 0.
+ * Splits an extent into two extents at split_fsb block such that it is the
+ * first block of the current_ext. @ext is a target extent to be split.
+ * @split_fsb is a block where the extents is split. If split_fsb lies in a
+ * hole or the first block of extents, just return 0.
*/
STATIC int
xfs_bmap_split_extent_at(
@@ -5837,7 +5820,7 @@ xfs_bmap_split_extent_at(
struct xfs_mount *mp = ip->i_mount;
struct xfs_ifork *ifp;
xfs_fsblock_t gotblkcnt; /* new block count for got */
- xfs_extnum_t current_ext;
+ struct xfs_iext_cursor icur;
int error = 0;
int logflags = 0;
int i = 0;
@@ -5865,7 +5848,7 @@ xfs_bmap_split_extent_at(
/*
* If there are not extents, or split_fsb lies in a hole we are done.
*/
- if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
+ if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
got.br_startoff >= split_fsb)
return 0;
@@ -5887,8 +5870,8 @@ xfs_bmap_split_extent_at(
}
got.br_blockcount = gotblkcnt;
- xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
- current_ext, &got);
+ xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
+ &got);
logflags = XFS_ILOG_CORE;
if (cur) {
@@ -5899,8 +5882,8 @@ xfs_bmap_split_extent_at(
logflags |= XFS_ILOG_DEXT;
/* Add new extent */
- current_ext++;
- xfs_iext_insert(ip, current_ext, 1, &new, 0);
+ xfs_iext_next(ifp, &icur);
+ xfs_iext_insert(ip, &icur, 1, &new, 0);
XFS_IFORK_NEXT_SET(ip, whichfork,
XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
diff --git a/libxfs/xfs_bmap.h b/libxfs/xfs_bmap.h
index a8777682ba..b6a395949d 100644
--- a/libxfs/xfs_bmap.h
+++ b/libxfs/xfs_bmap.h
@@ -43,7 +43,7 @@ struct xfs_bmalloca {
xfs_fsblock_t blkno; /* starting block of new extent */
struct xfs_btree_cur *cur; /* btree cursor */
- xfs_extnum_t idx; /* current extent index */
+ struct xfs_iext_cursor icur; /* incore extent cursor */
int nallocs;/* number of extents alloc'd */
int logflags;/* flags for transaction logging */
@@ -216,10 +216,11 @@ int xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
xfs_extnum_t nexts, xfs_fsblock_t *firstblock,
struct xfs_defer_ops *dfops, int *done);
int xfs_bmap_del_extent_delay(struct xfs_inode *ip, int whichfork,
- xfs_extnum_t *idx, struct xfs_bmbt_irec *got,
+ struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
+ struct xfs_bmbt_irec *del);
+void xfs_bmap_del_extent_cow(struct xfs_inode *ip,
+ struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
struct xfs_bmbt_irec *del);
-void xfs_bmap_del_extent_cow(struct xfs_inode *ip, xfs_extnum_t *idx,
- struct xfs_bmbt_irec *got, struct xfs_bmbt_irec *del);
uint xfs_default_attroffset(struct xfs_inode *ip);
int xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
@@ -232,7 +233,8 @@ int xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
int xfs_bmap_split_extent(struct xfs_inode *ip, xfs_fileoff_t split_offset);
int xfs_bmapi_reserve_delalloc(struct xfs_inode *ip, int whichfork,
xfs_fileoff_t off, xfs_filblks_t len, xfs_filblks_t prealloc,
- struct xfs_bmbt_irec *got, xfs_extnum_t *lastx, int eof);
+ struct xfs_bmbt_irec *got, struct xfs_iext_cursor *cur,
+ int eof);
enum xfs_bmap_intent_type {
XFS_BMAP_MAP = 1,
diff --git a/libxfs/xfs_inode_fork.c b/libxfs/xfs_inode_fork.c
index 55bbbdf5a8..ee59767f86 100644
--- a/libxfs/xfs_inode_fork.c
+++ b/libxfs/xfs_inode_fork.c
@@ -340,6 +340,7 @@ xfs_iformat_extents(
int state = xfs_bmap_fork_to_state(whichfork);
int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
int size = nex * sizeof(xfs_bmbt_rec_t);
+ struct xfs_iext_cursor icur;
struct xfs_bmbt_rec *dp;
int i;
@@ -366,16 +367,21 @@ xfs_iformat_extents(
ifp->if_bytes = size;
if (size) {
dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
+
+ xfs_iext_first(ifp, &icur);
for (i = 0; i < nex; i++, dp++) {
xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
+
if (!xfs_bmbt_validate_extent(mp, whichfork, dp)) {
XFS_ERROR_REPORT("xfs_iformat_extents(2)",
XFS_ERRLEVEL_LOW, mp);
return -EFSCORRUPTED;
}
+
ep->l0 = get_unaligned_be64(&dp->l0);
ep->l1 = get_unaligned_be64(&dp->l1);
- trace_xfs_read_extent(ip, i, state, _THIS_IP_);
+ trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
+ xfs_iext_next(ifp, &icur);
}
}
ifp->if_flags |= XFS_IFEXTENTS;
@@ -736,17 +742,18 @@ xfs_iextents_copy(
{
int state = xfs_bmap_fork_to_state(whichfork);
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
+ struct xfs_iext_cursor icur;
struct xfs_bmbt_irec rec;
- int copied = 0, i = 0;
+ int copied = 0;
ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
ASSERT(ifp->if_bytes > 0);
- while (xfs_iext_get_extent(ifp, i++, &rec)) {
+ for_each_xfs_iext(ifp, &icur, &rec) {
if (isnullstartblock(rec.br_startblock))
continue;
xfs_bmbt_disk_set_all(dp, &rec);
- trace_xfs_write_extent(ip, i, state, _RET_IP_);
+ trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, dp));
copied += sizeof(struct xfs_bmbt_rec);
dp++;
@@ -891,7 +898,7 @@ xfs_iext_state_to_fork(
void
xfs_iext_insert(
xfs_inode_t *ip, /* incore inode pointer */
- xfs_extnum_t idx, /* starting index of new items */
+ struct xfs_iext_cursor *cur,
xfs_extnum_t count, /* number of inserted items */
xfs_bmbt_irec_t *new, /* items to insert */
int state) /* type of extent conversion */
@@ -899,12 +906,12 @@ xfs_iext_insert(
xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
xfs_extnum_t i; /* extent record index */
- trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
+ trace_xfs_iext_insert(ip, cur->idx, new, state, _RET_IP_);
ASSERT(ifp->if_flags & XFS_IFEXTENTS);
- xfs_iext_add(ifp, idx, count);
- for (i = idx; i < idx + count; i++, new++)
- xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
+ xfs_iext_add(ifp, cur->idx, count);
+ for (i = 0; i < count; i++, new++)
+ xfs_bmbt_set_all(xfs_iext_get_ext(ifp, cur->idx + i), new);
}
/*
@@ -1142,7 +1149,7 @@ xfs_iext_add_indirect_multi(
void
xfs_iext_remove(
xfs_inode_t *ip, /* incore inode pointer */
- xfs_extnum_t idx, /* index to begin removing exts */
+ struct xfs_iext_cursor *cur,
int ext_diff, /* number of extents to remove */
int state) /* type of extent conversion */
{
@@ -1150,7 +1157,7 @@ xfs_iext_remove(
xfs_extnum_t nextents; /* number of extents in file */
int new_size; /* size of extents after removal */
- trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
+ trace_xfs_iext_remove(ip, cur, state, _RET_IP_);
ASSERT(ext_diff > 0);
nextents = xfs_iext_count(ifp);
@@ -1159,11 +1166,11 @@ xfs_iext_remove(
if (new_size == 0) {
xfs_iext_destroy(ifp);
} else if (ifp->if_flags & XFS_IFEXTIREC) {
- xfs_iext_remove_indirect(ifp, idx, ext_diff);
+ xfs_iext_remove_indirect(ifp, cur->idx, ext_diff);
} else if (ifp->if_real_bytes) {
- xfs_iext_remove_direct(ifp, idx, ext_diff);
+ xfs_iext_remove_direct(ifp, cur->idx, ext_diff);
} else {
- xfs_iext_remove_inline(ifp, idx, ext_diff);
+ xfs_iext_remove_inline(ifp, cur->idx, ext_diff);
}
ifp->if_bytes = new_size;
}
@@ -1912,26 +1919,26 @@ xfs_ifork_init_cow(
* Lookup the extent covering bno.
*
* If there is an extent covering bno return the extent index, and store the
- * expanded extent structure in *gotp, and the extent index in *idx.
+ * expanded extent structure in *gotp, and the extent cursor in *cur.
* If there is no extent covering bno, but there is an extent after it (e.g.
- * it lies in a hole) return that extent in *gotp and its index in *idx
+ * it lies in a hole) return that extent in *gotp and its cursor in *cur
* instead.
- * If bno is beyond the last extent return false, and return the index after
- * the last valid index in *idxp.
+ * If bno is beyond the last extent return false, and return an invalid
+ * cursor value.
*/
bool
xfs_iext_lookup_extent(
struct xfs_inode *ip,
struct xfs_ifork *ifp,
xfs_fileoff_t bno,
- xfs_extnum_t *idxp,
+ struct xfs_iext_cursor *cur,
struct xfs_bmbt_irec *gotp)
{
struct xfs_bmbt_rec_host *ep;
XFS_STATS_INC(ip->i_mount, xs_look_exlist);
- ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
+ ep = xfs_iext_bno_to_ext(ifp, bno, &cur->idx);
if (!ep)
return false;
xfs_bmbt_get_all(ep, gotp);
@@ -1947,31 +1954,31 @@ xfs_iext_lookup_extent_before(
struct xfs_inode *ip,
struct xfs_ifork *ifp,
xfs_fileoff_t *end,
- xfs_extnum_t *idxp,
+ struct xfs_iext_cursor *cur,
struct xfs_bmbt_irec *gotp)
{
- if (xfs_iext_lookup_extent(ip, ifp, *end - 1, idxp, gotp) &&
+ if (xfs_iext_lookup_extent(ip, ifp, *end - 1, cur, gotp) &&
gotp->br_startoff <= *end - 1)
return true;
- if (!xfs_iext_get_extent(ifp, --*idxp, gotp))
+ if (!xfs_iext_prev_extent(ifp, cur, gotp))
return false;
*end = gotp->br_startoff + gotp->br_blockcount;
return true;
}
/*
- * Return true if there is an extent at index idx, and return the expanded
- * extent structure at idx in that case. Else return false.
+ * Return true if the cursor points at an extent and return the extent structure
+ * in gotp. Else return false.
*/
bool
xfs_iext_get_extent(
struct xfs_ifork *ifp,
- xfs_extnum_t idx,
+ struct xfs_iext_cursor *cur,
struct xfs_bmbt_irec *gotp)
{
- if (idx < 0 || idx >= xfs_iext_count(ifp))
+ if (cur->idx < 0 || cur->idx >= xfs_iext_count(ifp))
return false;
- xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);
+ xfs_bmbt_get_all(xfs_iext_get_ext(ifp, cur->idx), gotp);
return true;
}
@@ -1979,15 +1986,15 @@ void
xfs_iext_update_extent(
struct xfs_inode *ip,
int state,
- xfs_extnum_t idx,
+ struct xfs_iext_cursor *cur,
struct xfs_bmbt_irec *gotp)
{
struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
- ASSERT(idx >= 0);
- ASSERT(idx < xfs_iext_count(ifp));
+ ASSERT(cur->idx >= 0);
+ ASSERT(cur->idx < xfs_iext_count(ifp));
- trace_xfs_bmap_pre_update(ip, idx, state, _RET_IP_);
- xfs_bmbt_set_all(xfs_iext_get_ext(ifp, idx), gotp);
- trace_xfs_bmap_post_update(ip, idx, state, _RET_IP_);
+ trace_xfs_bmap_pre_update(ip, cur, state, _RET_IP_);
+ xfs_bmbt_set_all(xfs_iext_get_ext(ifp, cur->idx), gotp);
+ trace_xfs_bmap_post_update(ip, cur, state, _RET_IP_);
}
diff --git a/libxfs/xfs_inode_fork.h b/libxfs/xfs_inode_fork.h
index 113fd42ec3..d454161793 100644
--- a/libxfs/xfs_inode_fork.h
+++ b/libxfs/xfs_inode_fork.h
@@ -151,12 +151,13 @@ void xfs_init_local_fork(struct xfs_inode *, int, const void *, int);
struct xfs_bmbt_rec_host *
xfs_iext_get_ext(struct xfs_ifork *, xfs_extnum_t);
xfs_extnum_t xfs_iext_count(struct xfs_ifork *);
-void xfs_iext_insert(struct xfs_inode *, xfs_extnum_t, xfs_extnum_t,
- struct xfs_bmbt_irec *, int);
+void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
+ xfs_extnum_t, struct xfs_bmbt_irec *, int);
void xfs_iext_add(struct xfs_ifork *, xfs_extnum_t, int);
void xfs_iext_add_indirect_multi(struct xfs_ifork *, int,
xfs_extnum_t, int);
-void xfs_iext_remove(struct xfs_inode *, xfs_extnum_t, int, int);
+void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
+ int, int);
void xfs_iext_remove_inline(struct xfs_ifork *, xfs_extnum_t, int);
void xfs_iext_remove_direct(struct xfs_ifork *, xfs_extnum_t, int);
void xfs_iext_remove_indirect(struct xfs_ifork *, xfs_extnum_t, int);
@@ -182,15 +183,85 @@ void xfs_iext_irec_update_extoffs(struct xfs_ifork *, int, int);
bool xfs_iext_lookup_extent(struct xfs_inode *ip,
struct xfs_ifork *ifp, xfs_fileoff_t bno,
- xfs_extnum_t *idxp, struct xfs_bmbt_irec *gotp);
+ struct xfs_iext_cursor *cur,
+ struct xfs_bmbt_irec *gotp);
bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
struct xfs_ifork *ifp, xfs_fileoff_t *end,
- xfs_extnum_t *idxp, struct xfs_bmbt_irec *gotp);
-
-bool xfs_iext_get_extent(struct xfs_ifork *ifp, xfs_extnum_t idx,
+ struct xfs_iext_cursor *cur,
+ struct xfs_bmbt_irec *gotp);
+bool xfs_iext_get_extent(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur,
struct xfs_bmbt_irec *gotp);
void xfs_iext_update_extent(struct xfs_inode *ip, int state,
- xfs_extnum_t idx, struct xfs_bmbt_irec *gotp);
+ struct xfs_iext_cursor *cur,
+ struct xfs_bmbt_irec *gotp);
+
+static inline void xfs_iext_first(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur)
+{
+ cur->idx = 0;
+}
+
+static inline void xfs_iext_last(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur)
+{
+ cur->idx = xfs_iext_count(ifp) - 1;
+}
+
+static inline void xfs_iext_next(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur)
+{
+ cur->idx++;
+}
+
+static inline void xfs_iext_prev(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur)
+{
+ cur->idx--;
+}
+
+static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
+{
+ xfs_iext_next(ifp, cur);
+ return xfs_iext_get_extent(ifp, cur, gotp);
+}
+
+static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
+{
+ xfs_iext_prev(ifp, cur);
+ return xfs_iext_get_extent(ifp, cur, gotp);
+}
+
+/*
+ * Return the extent after cur in gotp without updating the cursor.
+ */
+static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
+{
+ struct xfs_iext_cursor ncur = *cur;
+
+ xfs_iext_next(ifp, &ncur);
+ return xfs_iext_get_extent(ifp, &ncur, gotp);
+}
+
+/*
+ * Return the extent before cur in gotp without updating the cursor.
+ */
+static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
+ struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
+{
+ struct xfs_iext_cursor ncur = *cur;
+
+ xfs_iext_prev(ifp, &ncur);
+ return xfs_iext_get_extent(ifp, &ncur, gotp);
+}
+
+#define for_each_xfs_iext(ifp, ext, got) \
+ for (xfs_iext_first((ifp), (ext)); \
+ xfs_iext_get_extent((ifp), (ext), (got)); \
+ xfs_iext_next((ifp), (ext)))
extern struct kmem_zone *xfs_ifork_zone;
diff --git a/libxfs/xfs_types.h b/libxfs/xfs_types.h
index f04dbfb2f5..5da6382bda 100644
--- a/libxfs/xfs_types.h
+++ b/libxfs/xfs_types.h
@@ -142,5 +142,8 @@ typedef uint32_t xfs_dqid_t;
#define XFS_NBWORD (1 << XFS_NBWORDLOG)
#define XFS_WORDMASK ((1 << XFS_WORDLOG) - 1)
+struct xfs_iext_cursor {
+ xfs_extnum_t idx;
+};
#endif /* __XFS_TYPES_H__ */
diff --git a/repair/phase6.c b/repair/phase6.c
index 37505a8904..ad0ce9d43c 100644
--- a/repair/phase6.c
+++ b/repair/phase6.c
@@ -437,7 +437,7 @@ bmap_next_offset(
int error; /* error return value */
xfs_bmbt_irec_t got; /* current extent value */
xfs_ifork_t *ifp; /* inode fork pointer */
- xfs_extnum_t idx; /* last extent used */
+ struct xfs_iext_cursor icur;
if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
@@ -452,7 +452,7 @@ bmap_next_offset(
(error = -libxfs_iread_extents(tp, ip, whichfork)))
return error;
bno = *bnop + 1;
- if (!libxfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
+ if (!libxfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
*bnop = NULLFILEOFF;
else
*bnop = got.br_startoff < bno ? bno : got.br_startoff;