]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: provide helper for counting extents from if_bytes
authorEric Sandeen <sandeen@sandeen.net>
Tue, 10 Jan 2017 02:16:39 +0000 (20:16 -0600)
committerEric Sandeen <sandeen@redhat.com>
Tue, 10 Jan 2017 02:16:39 +0000 (20:16 -0600)
Source kernel commit: 5d829300bee000980a09ac2ccb761cb25867b67c

The open-coded pattern:

ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)

is all over the xfs code; provide a new helper
xfs_iext_count(ifp) to count the number of inline extents
in an inode fork.

[dchinner: pick up several missed conversions]

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Dave Chinner <david@fromorbit.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
libxfs/xfs_bmap.c
libxfs/xfs_inode_fork.c
libxfs/xfs_inode_fork.h

index 0e2f450495253519f4fbf0f267d18dfd4556d367..6185d9d2087c003bff7321b2c99b62314c74528b 100644 (file)
@@ -507,7 +507,7 @@ xfs_bmap_trace_exlist(
                state |= BMAP_ATTRFORK;
 
        ifp = XFS_IFORK_PTR(ip, whichfork);
-       ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
+       ASSERT(cnt == xfs_iext_count(ifp));
        for (idx = 0; idx < cnt; idx++)
                trace_xfs_extlist(ip, idx, whichfork, caller_ip);
 }
@@ -803,7 +803,7 @@ try_another_ag:
                                XFS_BTREE_LONG_PTRS);
 
        arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents =  xfs_iext_count(ifp);
        for (cnt = i = 0; i < nextents; i++) {
                ep = xfs_iext_get_ext(ifp, i);
                if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
@@ -1288,7 +1288,7 @@ xfs_bmap_read_extents(
        /*
         * Here with bp and block set to the leftmost leaf node in the tree.
         */
-       room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       room = xfs_iext_count(ifp);
        i = 0;
        /*
         * Loop over all leaf nodes.  Copy information to the extent records.
@@ -1353,7 +1353,7 @@ xfs_bmap_read_extents(
                        return error;
                block = XFS_BUF_TO_BLOCK(bp);
        }
-       ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
+       ASSERT(i == xfs_iext_count(ifp));
        ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
        XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
        return 0;
@@ -1396,7 +1396,7 @@ xfs_bmap_search_multi_extents(
        if (lastx > 0) {
                xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
        }
-       if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
+       if (lastx < xfs_iext_count(ifp)) {
                xfs_bmbt_get_all(ep, gotp);
                *eofp = 0;
        } else {
@@ -1489,7 +1489,7 @@ xfs_bmap_first_unused(
            (error = xfs_iread_extents(tp, ip, whichfork)))
                return error;
        lowest = *first_unused;
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
                xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
                off = xfs_bmbt_get_startoff(ep);
@@ -1574,7 +1574,7 @@ xfs_bmap_last_extent(
                        return error;
        }
 
-       nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        if (nextents == 0) {
                *is_empty = 1;
                return 0;
@@ -1727,7 +1727,7 @@ xfs_bmap_add_extent_delay_real(
                                                &bma->ip->i_d.di_nextents);
 
        ASSERT(bma->idx >= 0);
-       ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
+       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));
@@ -1786,7 +1786,7 @@ 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 < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
+       if (bma->idx < xfs_iext_count(ifp) - 1) {
                state |= BMAP_RIGHT_VALID;
                xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
 
@@ -2292,7 +2292,7 @@ xfs_bmap_add_extent_unwritten_real(
        ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
 
        ASSERT(*idx >= 0);
-       ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
+       ASSERT(*idx <= xfs_iext_count(ifp));
        ASSERT(!isnullstartblock(new->br_startblock));
 
        XFS_STATS_INC(mp, xs_add_exlist);
@@ -2348,7 +2348,7 @@ 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 < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
+       if (*idx < xfs_iext_count(&ip->i_df) - 1) {
                state |= BMAP_RIGHT_VALID;
                xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
                if (isnullstartblock(RIGHT.br_startblock))
@@ -2828,7 +2828,7 @@ 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 < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
+       if (*idx < xfs_iext_count(ifp)) {
                state |= BMAP_RIGHT_VALID;
                xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
 
@@ -2958,7 +2958,7 @@ xfs_bmap_add_extent_hole_real(
        ifp = XFS_IFORK_PTR(bma->ip, whichfork);
 
        ASSERT(bma->idx >= 0);
-       ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
+       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));
@@ -2984,7 +2984,7 @@ 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 (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
+       if (bma->idx < xfs_iext_count(ifp)) {
                state |= BMAP_RIGHT_VALID;
                xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
                if (isnullstartblock(right.br_startblock))
@@ -4213,7 +4213,7 @@ xfs_bmapi_read(
                        break;
 
                /* Else go on to the next record. */
-               if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
+               if (++lastx < xfs_iext_count(ifp))
                        xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
                else
                        eof = 1;
@@ -4725,7 +4725,7 @@ xfs_bmapi_write(
 
                /* Else go on to the next record. */
                bma.prev = bma.got;
-               if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
+               if (++bma.idx < xfs_iext_count(ifp)) {
                        xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
                                         &bma.got);
                } else
@@ -4877,7 +4877,7 @@ xfs_bmap_del_extent_delay(
        da_new = 0;
 
        ASSERT(*idx >= 0);
-       ASSERT(*idx < ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
+       ASSERT(*idx <= xfs_iext_count(ifp));
        ASSERT(del->br_blockcount > 0);
        ASSERT(got->br_startoff <= del->br_startoff);
        ASSERT(got_endoff >= del_endoff);
@@ -5008,7 +5008,7 @@ xfs_bmap_del_extent_cow(
        got_endoff = got->br_startoff + got->br_blockcount;
 
        ASSERT(*idx >= 0);
-       ASSERT(*idx < ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
+       ASSERT(*idx <= xfs_iext_count(ifp));
        ASSERT(del->br_blockcount > 0);
        ASSERT(got->br_startoff <= del->br_startoff);
        ASSERT(got_endoff >= del_endoff);
@@ -5114,8 +5114,7 @@ xfs_bmap_del_extent(
                state |= BMAP_COWFORK;
 
        ifp = XFS_IFORK_PTR(ip, whichfork);
-       ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
-               (uint)sizeof(xfs_bmbt_rec_t)));
+       ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
        ASSERT(del->br_blockcount > 0);
        ep = xfs_iext_get_ext(ifp, *idx);
        xfs_bmbt_get_all(ep, &got);
@@ -5440,7 +5439,6 @@ __xfs_bunmapi(
        int                     logflags;       /* transaction logging flags */
        xfs_extlen_t            mod;            /* rt extent offset */
        xfs_mount_t             *mp;            /* mount structure */
-       xfs_extnum_t            nextents;       /* number of file extents */
        xfs_bmbt_irec_t         prev;           /* previous extent record */
        xfs_fileoff_t           start;          /* first file offset deleted */
        int                     tmp_logflags;   /* partial logging flags */
@@ -5472,8 +5470,7 @@ __xfs_bunmapi(
        if (!(ifp->if_flags & XFS_IFEXTENTS) &&
            (error = xfs_iread_extents(tp, ip, whichfork)))
                return error;
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
-       if (nextents == 0) {
+       if (xfs_iext_count(ifp) == 0) {
                *rlen = 0;
                return 0;
        }
@@ -5958,7 +5955,7 @@ xfs_bmse_shift_one(
 
        mp = ip->i_mount;
        ifp = XFS_IFORK_PTR(ip, whichfork);
-       total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
+       total_extents = xfs_iext_count(ifp);
 
        xfs_bmbt_get_all(gotp, &got);
 
@@ -6135,7 +6132,7 @@ xfs_bmap_shift_extents(
         * are collapsing out, so we cannot use the count of real extents here.
         * Instead we have to calculate it from the incore fork.
         */
-       total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
+       total_extents = xfs_iext_count(ifp);
        if (total_extents == 0) {
                *done = 1;
                goto del_cursor;
@@ -6195,7 +6192,7 @@ xfs_bmap_shift_extents(
                 * count can change. Update the total and grade the next record.
                 */
                if (direction == SHIFT_LEFT) {
-                       total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
+                       total_extents = xfs_iext_count(ifp);
                        stop_extent = total_extents;
                }
 
index 2799e2da9d17b1c648b37d545b11c1d05aea35ad..7519e43eb16cc803833b3accc995506ff09d0a71 100644 (file)
@@ -772,6 +772,13 @@ xfs_idestroy_fork(
        }
 }
 
+/* Count number of incore extents based on if_bytes */
+xfs_extnum_t
+xfs_iext_count(struct xfs_ifork *ifp)
+{
+       return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+}
+
 /*
  * Convert in-core extents to on-disk form
  *
@@ -800,7 +807,7 @@ xfs_iextents_copy(
        ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
        ASSERT(ifp->if_bytes > 0);
 
-       nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nrecs = xfs_iext_count(ifp);
        XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
        ASSERT(nrecs > 0);
 
@@ -938,7 +945,7 @@ xfs_iext_get_ext(
        xfs_extnum_t    idx)            /* index of target extent */
 {
        ASSERT(idx >= 0);
-       ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
+       ASSERT(idx < xfs_iext_count(ifp));
 
        if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
                return ifp->if_u1.if_ext_irec->er_extbuf;
@@ -1014,7 +1021,7 @@ xfs_iext_add(
        int             new_size;       /* size of extents after adding */
        xfs_extnum_t    nextents;       /* number of extents in file */
 
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        ASSERT((idx >= 0) && (idx <= nextents));
        byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
        new_size = ifp->if_bytes + byte_diff;
@@ -1238,7 +1245,7 @@ xfs_iext_remove(
        trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
 
        ASSERT(ext_diff > 0);
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
 
        if (new_size == 0) {
@@ -1267,7 +1274,7 @@ xfs_iext_remove_inline(
 
        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
        ASSERT(idx < XFS_INLINE_EXTS);
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        ASSERT(((nextents - ext_diff) > 0) &&
                (nextents - ext_diff) < XFS_INLINE_EXTS);
 
@@ -1306,7 +1313,7 @@ xfs_iext_remove_direct(
        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
        new_size = ifp->if_bytes -
                (ext_diff * sizeof(xfs_bmbt_rec_t));
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
 
        if (new_size == 0) {
                xfs_iext_destroy(ifp);
@@ -1545,7 +1552,7 @@ xfs_iext_indirect_to_direct(
        int             size;           /* size of file extents */
 
        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        ASSERT(nextents <= XFS_LINEAR_EXTS);
        size = nextents * sizeof(xfs_bmbt_rec_t);
 
@@ -1619,7 +1626,7 @@ xfs_iext_bno_to_ext(
        xfs_extnum_t    nextents;       /* number of file extents */
        xfs_fileoff_t   startoff = 0;   /* start offset of extent */
 
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        if (nextents == 0) {
                *idxp = 0;
                return NULL;
@@ -1732,8 +1739,8 @@ xfs_iext_idx_to_irec(
 
        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
        ASSERT(page_idx >= 0);
-       ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
-       ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
+       ASSERT(page_idx <= xfs_iext_count(ifp));
+       ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
 
        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
        erp_idx = 0;
@@ -1781,7 +1788,7 @@ xfs_iext_irec_init(
        xfs_extnum_t    nextents;       /* number of extents in file */
 
        ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
        ASSERT(nextents <= XFS_LINEAR_EXTS);
 
        erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
@@ -1905,7 +1912,7 @@ xfs_iext_irec_compact(
 
        ASSERT(ifp->if_flags & XFS_IFEXTIREC);
        nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       nextents = xfs_iext_count(ifp);
 
        if (nextents == 0) {
                xfs_iext_destroy(ifp);
index c9476f50e32d116109d1f71dad3895c5659496b8..8bf112e29aa1c8a61262d7c75f91bcb9a7c3055a 100644 (file)
@@ -152,6 +152,7 @@ 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_add(struct xfs_ifork *, xfs_extnum_t, int);