XFS: Free buffer pages array unconditionally
[safe/jmp/linux-2.6] / fs / xfs / xfs_rtalloc.c
index e050987..9e15a11 100644 (file)
@@ -44,6 +44,8 @@
 #include "xfs_rw.h"
 #include "xfs_inode_item.h"
 #include "xfs_trans_space.h"
+#include "xfs_utils.h"
+#include "xfs_trace.h"
 
 
 /*
@@ -73,18 +75,6 @@ STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
  */
 
 /*
- * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
- */
-STATIC int
-xfs_lowbit32(
-       __uint32_t      v)
-{
-       if (v)
-               return ffs(v) - 1;
-       return -1;
-}
-
-/*
  * Allocate space to the bitmap or summary file, and zero it, for growfs.
  */
 STATIC int                             /* error */
@@ -96,7 +86,6 @@ xfs_growfs_rt_alloc(
 {
        xfs_fileoff_t   bno;            /* block number in file */
        xfs_buf_t       *bp;            /* temporary buffer for zeroing */
-       int             cancelflags;    /* flags for xfs_trans_cancel */
        int             committed;      /* transaction committed flag */
        xfs_daddr_t     d;              /* disk block address */
        int             error;          /* error return value */
@@ -107,15 +96,16 @@ xfs_growfs_rt_alloc(
        xfs_bmbt_irec_t map;            /* block map output */
        int             nmap;           /* number of block maps */
        int             resblks;        /* space reservation */
-       xfs_trans_t     *tp;            /* transaction pointer */
 
        /*
         * Allocate space to the file, as necessary.
         */
        while (oblocks < nblocks) {
+               int             cancelflags = 0;
+               xfs_trans_t     *tp;
+
                tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
                resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
-               cancelflags = 0;
                /*
                 * Reserve space & log for one extent added to the file.
                 */
@@ -123,15 +113,15 @@ xfs_growfs_rt_alloc(
                                XFS_GROWRTALLOC_LOG_RES(mp), 0,
                                XFS_TRANS_PERM_LOG_RES,
                                XFS_DEFAULT_PERM_LOG_COUNT)))
-                       goto error_exit;
+                       goto error_cancel;
                cancelflags = XFS_TRANS_RELEASE_LOG_RES;
                /*
                 * Lock the inode.
                 */
                if ((error = xfs_trans_iget(mp, tp, ino, 0,
                                                XFS_ILOCK_EXCL, &ip)))
-                       goto error_exit;
-               XFS_BMAP_INIT(&flist, &firstblock);
+                       goto error_cancel;
+               xfs_bmap_init(&flist, &firstblock);
                /*
                 * Allocate blocks to the bitmap file.
                 */
@@ -143,14 +133,16 @@ xfs_growfs_rt_alloc(
                if (!error && nmap < 1)
                        error = XFS_ERROR(ENOSPC);
                if (error)
-                       goto error_exit;
+                       goto error_cancel;
                /*
                 * Free any blocks freed up in the transaction, then commit.
                 */
-               error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
+               error = xfs_bmap_finish(&tp, &flist, &committed);
+               if (error)
+                       goto error_cancel;
+               error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
                if (error)
-                       goto error_exit;
-               xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
+                       goto error;
                /*
                 * Now we need to clear the allocated blocks.
                 * Do this one block per transaction, to keep it simple.
@@ -165,13 +157,13 @@ xfs_growfs_rt_alloc(
                         */
                        if ((error = xfs_trans_reserve(tp, 0,
                                        XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
-                               goto error_exit;
+                               goto error_cancel;
                        /*
                         * Lock the bitmap inode.
                         */
                        if ((error = xfs_trans_iget(mp, tp, ino, 0,
                                                        XFS_ILOCK_EXCL, &ip)))
-                               goto error_exit;
+                               goto error_cancel;
                        /*
                         * Get a buffer for the block.
                         */
@@ -180,14 +172,18 @@ xfs_growfs_rt_alloc(
                                mp->m_bsize, 0);
                        if (bp == NULL) {
                                error = XFS_ERROR(EIO);
-                               goto error_exit;
+error_cancel:
+                               xfs_trans_cancel(tp, cancelflags);
+                               goto error;
                        }
                        memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
                        xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
                        /*
                         * Commit the transaction.
                         */
-                       xfs_trans_commit(tp, 0, NULL);
+                       error = xfs_trans_commit(tp, 0);
+                       if (error)
+                               goto error;
                }
                /*
                 * Go on to the next extent, if any.
@@ -195,8 +191,7 @@ xfs_growfs_rt_alloc(
                oblocks = map.br_startoff + map.br_blockcount;
        }
        return 0;
-error_exit:
-       xfs_trans_cancel(tp, cancelflags);
+error:
        return error;
 }
 
@@ -444,6 +439,7 @@ xfs_rtallocate_extent_near(
        }
        bbno = XFS_BITTOBLOCK(mp, bno);
        i = 0;
+       ASSERT(minlen != 0);
        log2len = xfs_highbit32(minlen);
        /*
         * Loop over all bitmap blocks (bbno + i is current block).
@@ -612,6 +608,8 @@ xfs_rtallocate_extent_size(
        xfs_suminfo_t   sum;            /* summary information for extents */
 
        ASSERT(minlen % prod == 0 && maxlen % prod == 0);
+       ASSERT(maxlen != 0);
+
        /*
         * Loop over all the levels starting with maxlen.
         * At each level, look at all the bitmap blocks, to see if there
@@ -669,6 +667,9 @@ xfs_rtallocate_extent_size(
                *rtblock = NULLRTBLOCK;
                return 0;
        }
+       ASSERT(minlen != 0);
+       ASSERT(maxlen != 0);
+
        /*
         * Loop over sizes, from maxlen down to minlen.
         * This time, when we do the allocations, allow smaller ones
@@ -913,57 +914,6 @@ xfs_rtcheck_alloc_range(
 }
 #endif
 
-#ifdef DEBUG
-/*
- * Check whether the given block in the bitmap has the given value.
- */
-STATIC int                             /* 1 for matches, 0 for not */
-xfs_rtcheck_bit(
-       xfs_mount_t     *mp,            /* file system mount structure */
-       xfs_trans_t     *tp,            /* transaction pointer */
-       xfs_rtblock_t   start,          /* bit (block) to check */
-       int             val)            /* 1 for free, 0 for allocated */
-{
-       int             bit;            /* bit number in the word */
-       xfs_rtblock_t   block;          /* bitmap block number */
-       xfs_buf_t       *bp;            /* buf for the block */
-       xfs_rtword_t    *bufp;          /* pointer into the buffer */
-       /* REFERENCED */
-       int             error;          /* error value */
-       xfs_rtword_t    wdiff;          /* difference between bit & expected */
-       int             word;           /* word number in the buffer */
-       xfs_rtword_t    wval;           /* word value from buffer */
-
-       block = XFS_BITTOBLOCK(mp, start);
-       error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
-       bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
-       word = XFS_BITTOWORD(mp, start);
-       bit = (int)(start & (XFS_NBWORD - 1));
-       wval = bufp[word];
-       xfs_trans_brelse(tp, bp);
-       wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
-       return !wdiff;
-}
-#endif /* DEBUG */
-
-#if 0
-/*
- * Check that the given extent (block range) is free already.
- */
-STATIC int                             /* error */
-xfs_rtcheck_free_range(
-       xfs_mount_t     *mp,            /* file system mount point */
-       xfs_trans_t     *tp,            /* transaction pointer */
-       xfs_rtblock_t   bno,            /* starting block number of extent */
-       xfs_extlen_t    len,            /* length of extent */
-       int             *stat)          /* out: 1 for free, 0 for not */
-{
-       xfs_rtblock_t   new;            /* dummy for xfs_rtcheck_range */
-
-       return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
-}
-#endif
-
 /*
  * Check that the given range is either all allocated (val = 0) or
  * all free (val = 1).
@@ -1907,7 +1857,6 @@ xfs_growfs_rt(
 {
        xfs_rtblock_t   bmbno;          /* bitmap block number */
        xfs_buf_t       *bp;            /* temporary buffer */
-       int             cancelflags;    /* flags for xfs_trans_cancel */
        int             error;          /* error return value */
        xfs_inode_t     *ip;            /* bitmap inode, used as lock */
        xfs_mount_t     *nmp;           /* new (fake) mount structure */
@@ -1923,21 +1872,24 @@ xfs_growfs_rt(
        xfs_extlen_t    rsumblocks;     /* current number of rt summary blks */
        xfs_sb_t        *sbp;           /* old superblock */
        xfs_fsblock_t   sumbno;         /* summary block number */
-       xfs_trans_t     *tp;            /* transaction pointer */
 
        sbp = &mp->m_sb;
        /*
         * Initial error checking.
         */
+       if (!capable(CAP_SYS_ADMIN))
+               return XFS_ERROR(EPERM);
        if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
            (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
            (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
                return XFS_ERROR(EINVAL);
+       if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
+               return error;
        /*
         * Read in the last block of the device, make sure it exists.
         */
        error = xfs_read_buf(mp, mp->m_rtdev_targp,
-                       XFS_FSB_TO_BB(mp, in->newblocks - 1),
+                       XFS_FSB_TO_BB(mp, nrblocks - 1),
                        XFS_FSB_TO_BB(mp, 1), 0, &bp);
        if (error)
                return error;
@@ -1948,7 +1900,7 @@ xfs_growfs_rt(
         */
        nrextents = nrblocks;
        do_div(nrextents, in->extsize);
-       nrbmblocks = roundup_64(nrextents, NBBY * sbp->sb_blocksize);
+       nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
        nrextslog = xfs_highbit32(nrextents);
        nrsumlevels = nrextslog + 1;
        nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
@@ -1990,6 +1942,9 @@ xfs_growfs_rt(
                     ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
             bmbno < nrbmblocks;
             bmbno++) {
+               xfs_trans_t     *tp;
+               int             cancelflags = 0;
+
                *nmp = *mp;
                nsbp = &nmp->m_sb;
                /*
@@ -2003,6 +1958,7 @@ xfs_growfs_rt(
                                  nsbp->sb_blocksize * nsbp->sb_rextsize);
                nsbp->sb_rextents = nsbp->sb_rblocks;
                do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
+               ASSERT(nsbp->sb_rextents != 0);
                nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
                nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
                nrsumsize =
@@ -2014,16 +1970,15 @@ xfs_growfs_rt(
                 * Start a transaction, get the log reservation.
                 */
                tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
-               cancelflags = 0;
                if ((error = xfs_trans_reserve(tp, 0,
                                XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
-                       break;
+                       goto error_cancel;
                /*
                 * Lock out other callers by grabbing the bitmap inode lock.
                 */
                if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
                                                XFS_ILOCK_EXCL, &ip)))
-                       break;
+                       goto error_cancel;
                ASSERT(ip == mp->m_rbmip);
                /*
                 * Update the bitmap inode's size.
@@ -2037,7 +1992,7 @@ xfs_growfs_rt(
                 */
                if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
                                                XFS_ILOCK_EXCL, &ip)))
-                       break;
+                       goto error_cancel;
                ASSERT(ip == mp->m_rsumip);
                /*
                 * Update the summary inode's size.
@@ -2052,7 +2007,7 @@ xfs_growfs_rt(
                    mp->m_rsumlevels != nmp->m_rsumlevels) {
                        error = xfs_rtcopy_summary(mp, nmp, tp);
                        if (error)
-                               break;
+                               goto error_cancel;
                }
                /*
                 * Update superblock fields.
@@ -2078,8 +2033,11 @@ xfs_growfs_rt(
                bp = NULL;
                error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
                        nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
-               if (error)
+               if (error) {
+error_cancel:
+                       xfs_trans_cancel(tp, cancelflags);
                        break;
+               }
                /*
                 * Mark more blocks free in the superblock.
                 */
@@ -2090,19 +2048,16 @@ xfs_growfs_rt(
                 */
                mp->m_rsumlevels = nrsumlevels;
                mp->m_rsumsize = nrsumsize;
-               /*
-                * Commit the transaction.
-                */
-               xfs_trans_commit(tp, 0, NULL);
-       }
 
-       if (error)
-               xfs_trans_cancel(tp, cancelflags);
+               error = xfs_trans_commit(tp, 0);
+               if (error)
+                       break;
+       }
 
        /*
         * Free the fake mp structure.
         */
-       kmem_free(nmp, sizeof(*nmp));
+       kmem_free(nmp);
 
        return error;
 }
@@ -2327,13 +2282,23 @@ xfs_rtmount_inodes(
        ASSERT(sbp->sb_rsumino != NULLFSINO);
        error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
        if (error) {
-               VN_RELE(XFS_ITOV(mp->m_rbmip));
+               IRELE(mp->m_rbmip);
                return error;
        }
        ASSERT(mp->m_rsumip != NULL);
        return 0;
 }
 
+void
+xfs_rtunmount_inodes(
+       struct xfs_mount        *mp)
+{
+       if (mp->m_rbmip)
+               IRELE(mp->m_rbmip);
+       if (mp->m_rsumip)
+               IRELE(mp->m_rsumip);
+}
+
 /*
  * Pick an extent for allocation at the start of a new realtime file.
  * Use the sequence number stored in the atime field of the bitmap inode.
@@ -2382,60 +2347,3 @@ xfs_rtpick_extent(
        *pick = b;
        return 0;
 }
-
-#ifdef DEBUG
-/*
- * Debug code: print out the value of a range in the bitmap.
- */
-void
-xfs_rtprint_range(
-       xfs_mount_t     *mp,            /* file system mount structure */
-       xfs_trans_t     *tp,            /* transaction pointer */
-       xfs_rtblock_t   start,          /* starting block to print */
-       xfs_extlen_t    len)            /* length to print */
-{
-       xfs_extlen_t    i;              /* block number in the extent */
-
-       cmn_err(CE_DEBUG, "%Ld: ", (long long)start);
-       for (i = 0; i < len; i++)
-               cmn_err(CE_DEBUG, "%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
-       cmn_err(CE_DEBUG, "\n");
-}
-
-/*
- * Debug code: print the summary file.
- */
-void
-xfs_rtprint_summary(
-       xfs_mount_t     *mp,            /* file system mount structure */
-       xfs_trans_t     *tp)            /* transaction pointer */
-{
-       xfs_suminfo_t   c;              /* summary data */
-       xfs_rtblock_t   i;              /* bitmap block number */
-       int             l;              /* summary information level */
-       int             p;              /* flag for printed anything */
-       xfs_fsblock_t   sb;             /* summary block number */
-       xfs_buf_t       *sumbp;         /* summary block buffer */
-
-       sumbp = NULL;
-       for (l = 0; l < mp->m_rsumlevels; l++) {
-               for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
-                       (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
-                       if (c) {
-                               if (!p) {
-                                       cmn_err(CE_DEBUG, "%Ld-%Ld:", 1LL << l,
-                                               XFS_RTMIN((1LL << l) +
-                                                         ((1LL << l) - 1LL),
-                                                        mp->m_sb.sb_rextents));
-                                       p = 1;
-                               }
-                               cmn_err(CE_DEBUG, " %Ld:%d", (long long)i, c);
-                       }
-               }
-               if (p)
-                       cmn_err(CE_DEBUG, "\n");
-       }
-       if (sumbp)
-               xfs_trans_brelse(tp, sumbp);
-}
-#endif /* DEBUG */