]> err.no Git - linux-2.6/blobdiff - fs/xfs/xfs_bmap.c
[XFS] Fixed a bug in reporting extent list for attribute fork running
[linux-2.6] / fs / xfs / xfs_bmap.c
index f6f5ad35734c597ee368b0949e886c06489fa133..e52387b3abef805f6f2879f287240d5f607724f8 100644 (file)
@@ -1,68 +1,53 @@
 /*
- * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 2000-2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
  * published by the Free Software Foundation.
  *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
-
 #include "xfs.h"
-
-#include "xfs_macros.h"
+#include "xfs_fs.h"
 #include "xfs_types.h"
-#include "xfs_inum.h"
+#include "xfs_bit.h"
 #include "xfs_log.h"
+#include "xfs_inum.h"
 #include "xfs_trans.h"
 #include "xfs_sb.h"
 #include "xfs_ag.h"
 #include "xfs_dir.h"
 #include "xfs_dir2.h"
-#include "xfs_dmapi.h"
-#include "xfs_mount.h"
-#include "xfs_alloc_btree.h"
+#include "xfs_da_btree.h"
 #include "xfs_bmap_btree.h"
+#include "xfs_alloc_btree.h"
 #include "xfs_ialloc_btree.h"
-#include "xfs_btree.h"
-#include "xfs_ialloc.h"
-#include "xfs_attr_sf.h"
 #include "xfs_dir_sf.h"
 #include "xfs_dir2_sf.h"
+#include "xfs_attr_sf.h"
 #include "xfs_dinode.h"
-#include "xfs_inode_item.h"
 #include "xfs_inode.h"
+#include "xfs_btree.h"
+#include "xfs_dmapi.h"
+#include "xfs_mount.h"
+#include "xfs_ialloc.h"
 #include "xfs_itable.h"
+#include "xfs_inode_item.h"
 #include "xfs_extfree_item.h"
 #include "xfs_alloc.h"
 #include "xfs_bmap.h"
 #include "xfs_rtalloc.h"
 #include "xfs_error.h"
-#include "xfs_da_btree.h"
 #include "xfs_dir_leaf.h"
-#include "xfs_bit.h"
+#include "xfs_attr_leaf.h"
 #include "xfs_rw.h"
 #include "xfs_quota.h"
 #include "xfs_trans_space.h"
@@ -3336,6 +3321,29 @@ xfs_bmap_insert_exlist(
                xfs_bmbt_set_all(&base[to], new);
 }
 
+/*
+ * Helper routine to reset inode di_forkoff field when switching
+ * attribute fork from local to extent format - we reset it where
+ * possible to make space available for inline data fork extents.
+ */
+STATIC void
+xfs_bmap_forkoff_reset(
+       xfs_mount_t     *mp,
+       xfs_inode_t     *ip,
+       int             whichfork)
+{
+       if (whichfork == XFS_ATTR_FORK &&
+           (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
+           (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
+           ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
+               ip->i_d.di_forkoff = mp->m_attroffset >> 3;
+               ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
+                                       (uint)sizeof(xfs_bmbt_rec_t);
+               ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
+                                       (uint)sizeof(xfs_bmbt_rec_t);
+       }
+}
+
 /*
  * Convert a local file to an extents file.
  * This code is out of bounds for data forks of regular files,
@@ -3403,6 +3411,7 @@ xfs_bmap_local_to_extents(
                memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
                        ifp->if_bytes);
                xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
+               xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
                xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
                xfs_iext_realloc(ip, 1, whichfork);
                ep = ifp->if_u1.if_extents;
@@ -3413,8 +3422,10 @@ xfs_bmap_local_to_extents(
                XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
                        XFS_TRANS_DQ_BCOUNT, 1L);
                flags |= XFS_ILOG_FEXT(whichfork);
-       } else
+       } else {
                ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
+               xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
+       }
        ifp->if_flags &= ~XFS_IFINLINE;
        ifp->if_flags |= XFS_IFEXTENTS;
        XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
@@ -3796,22 +3807,24 @@ xfs_bunmap_trace(
 int                                            /* error code */
 xfs_bmap_add_attrfork(
        xfs_inode_t             *ip,            /* incore inode pointer */
-       int                     rsvd)           /* OK to allocated reserved blocks in trans */
+       int                     size,           /* space new attribute needs */
+       int                     rsvd)           /* xact may use reserved blks */
 {
-       int                     blks;           /* space reservation */
-       int                     committed;      /* xaction was committed */
-       int                     error;          /* error return value */
        xfs_fsblock_t           firstblock;     /* 1st block/ag allocated */
        xfs_bmap_free_t         flist;          /* freed extent list */
-       int                     logflags;       /* logging flags */
        xfs_mount_t             *mp;            /* mount structure */
-       unsigned long           s;              /* spinlock spl value */
        xfs_trans_t             *tp;            /* transaction pointer */
+       unsigned long           s;              /* spinlock spl value */
+       int                     blks;           /* space reservation */
+       int                     version = 1;    /* superblock attr version */
+       int                     committed;      /* xaction was committed */
+       int                     logflags;       /* logging flags */
+       int                     error;          /* error return value */
 
+       ASSERT(XFS_IFORK_Q(ip) == 0);
        ASSERT(ip->i_df.if_ext_max ==
               XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
-       if (XFS_IFORK_Q(ip))
-               return 0;
+
        mp = ip->i_mount;
        ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
        tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
@@ -3853,7 +3866,11 @@ xfs_bmap_add_attrfork(
        case XFS_DINODE_FMT_LOCAL:
        case XFS_DINODE_FMT_EXTENTS:
        case XFS_DINODE_FMT_BTREE:
-               ip->i_d.di_forkoff = mp->m_attroffset >> 3;
+               ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
+               if (!ip->i_d.di_forkoff)
+                       ip->i_d.di_forkoff = mp->m_attroffset >> 3;
+               else if (!(mp->m_flags & XFS_MOUNT_COMPAT_ATTR))
+                       version = 2;
                break;
        default:
                ASSERT(0);
@@ -3890,12 +3907,22 @@ xfs_bmap_add_attrfork(
                xfs_trans_log_inode(tp, ip, logflags);
        if (error)
                goto error2;
-       if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
+       if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
+          (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
+               __int64_t sbfields = 0;
+
                s = XFS_SB_LOCK(mp);
                if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
                        XFS_SB_VERSION_ADDATTR(&mp->m_sb);
+                       sbfields |= XFS_SB_VERSIONNUM;
+               }
+               if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
+                       XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
+                       sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
+               }
+               if (sbfields) {
                        XFS_SB_UNLOCK(mp, s);
-                       xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
+                       xfs_mod_sb(tp, sbfields);
                } else
                        XFS_SB_UNLOCK(mp, s);
        }
@@ -3988,13 +4015,19 @@ xfs_bmap_compute_maxlevels(
         * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
         * (a signed 16-bit number, xfs_aextnum_t).
         */
-       maxleafents = (whichfork == XFS_DATA_FORK) ? MAXEXTNUM : MAXAEXTNUM;
+       if (whichfork == XFS_DATA_FORK) {
+               maxleafents = MAXEXTNUM;
+               sz = (mp->m_flags & XFS_MOUNT_COMPAT_ATTR) ?
+                       mp->m_attroffset : XFS_BMDR_SPACE_CALC(MINDBTPTRS);
+       } else {
+               maxleafents = MAXAEXTNUM;
+               sz = (mp->m_flags & XFS_MOUNT_COMPAT_ATTR) ?
+                       mp->m_sb.sb_inodesize - mp->m_attroffset :
+                       XFS_BMDR_SPACE_CALC(MINABTPTRS);
+       }
+       maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
        minleafrecs = mp->m_bmap_dmnr[0];
        minnoderecs = mp->m_bmap_dmnr[1];
-       sz = (whichfork == XFS_DATA_FORK) ?
-               mp->m_attroffset :
-               mp->m_sb.sb_inodesize - mp->m_attroffset;
-       maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
        maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
        for (level = 1; maxblocks > 1; level++) {
                if (maxblocks <= maxrootrecs)
@@ -4545,18 +4578,17 @@ xfs_bmapi(
        xfs_extlen_t    alen;           /* allocated extent length */
        xfs_fileoff_t   aoff;           /* allocated file offset */
        xfs_bmalloca_t  bma;            /* args for xfs_bmap_alloc */
-       char            contig;         /* allocation must be one extent */
        xfs_btree_cur_t *cur;           /* bmap btree cursor */
-       char            delay;          /* this request is for delayed alloc */
        xfs_fileoff_t   end;            /* end of mapped file region */
        int             eof;            /* we've hit the end of extent list */
+       char            contig;         /* allocation must be one extent */
+       char            delay;          /* this request is for delayed alloc */
+       char            exact;          /* don't do all of wasdelayed extent */
        xfs_bmbt_rec_t  *ep;            /* extent list entry pointer */
        int             error;          /* error return */
-       char            exact;          /* don't do all of wasdelayed extent */
        xfs_bmbt_irec_t got;            /* current extent list record */
        xfs_ifork_t     *ifp;           /* inode fork pointer */
        xfs_extlen_t    indlen;         /* indirect blocks length */
-       char            inhole;         /* current location is hole in file */
        xfs_extnum_t    lastx;          /* last useful extent number */
        int             logflags;       /* flags for transaction logging */
        xfs_extlen_t    minleft;        /* min blocks left after allocation */
@@ -4567,13 +4599,15 @@ xfs_bmapi(
        xfs_extnum_t    nextents;       /* number of extents in file */
        xfs_fileoff_t   obno;           /* old block number (offset) */
        xfs_bmbt_irec_t prev;           /* previous extent list record */
-       char            stateless;      /* ignore state flag set */
        int             tmp_logflags;   /* temp flags holder */
+       int             whichfork;      /* data or attr fork */
+       char            inhole;         /* current location is hole in file */
+       char            stateless;      /* ignore state flag set */
        char            trim;           /* output trimmed to match range */
        char            userdata;       /* allocating non-metadata */
        char            wasdelay;       /* old extent was delayed */
-       int             whichfork;      /* data or attr fork */
        char            wr;             /* this is a write request */
+       char            rt;             /* this is a realtime file */
        char            rsvd;           /* OK to allocate reserved blocks */
 #ifdef DEBUG
        xfs_fileoff_t   orig_bno;       /* original block number value */
@@ -4603,6 +4637,7 @@ xfs_bmapi(
        }
        if (XFS_FORCED_SHUTDOWN(mp))
                return XFS_ERROR(EIO);
+       rt = XFS_IS_REALTIME_INODE(ip);
        ifp = XFS_IFORK_PTR(ip, whichfork);
        ASSERT(ifp->if_ext_max ==
               XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
@@ -4707,9 +4742,16 @@ xfs_bmapi(
                        }
                        minlen = contig ? alen : 1;
                        if (delay) {
-                               indlen = (xfs_extlen_t)
-                                       xfs_bmap_worst_indlen(ip, alen);
-                               ASSERT(indlen > 0);
+                               xfs_extlen_t    extsz = 0;
+
+                               /* Figure out the extent size, adjust alen */
+                               if (rt) {
+                                       if (!(extsz = ip->i_d.di_extsize))
+                                               extsz = mp->m_sb.sb_rextsize;
+                                       alen = roundup(alen, extsz);
+                                       extsz = alen / mp->m_sb.sb_rextsize;
+                               }
+
                                /*
                                 * Make a transaction-less quota reservation for
                                 * delayed allocation blocks. This number gets
@@ -4717,8 +4759,10 @@ xfs_bmapi(
                                 * We return EDQUOT if we haven't allocated
                                 * blks already inside this loop;
                                 */
-                               if (XFS_TRANS_RESERVE_BLKQUOTA(
-                                               mp, NULL, ip, (long)alen)) {
+                               if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
+                                               mp, NULL, ip, (long)alen, 0,
+                                               rt ? XFS_QMOPT_RES_RTBLKS :
+                                                    XFS_QMOPT_RES_REGBLKS)) {
                                        if (n == 0) {
                                                *nmap = 0;
                                                ASSERT(cur == NULL);
@@ -4731,40 +4775,44 @@ xfs_bmapi(
                                 * Split changing sb for alen and indlen since
                                 * they could be coming from different places.
                                 */
-                               if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
-                                       xfs_extlen_t    extsz;
-                                       xfs_extlen_t    ralen;
-                                       if (!(extsz = ip->i_d.di_extsize))
-                                               extsz = mp->m_sb.sb_rextsize;
-                                       ralen = roundup(alen, extsz);
-                                       ralen = ralen / mp->m_sb.sb_rextsize;
-                                       if (xfs_mod_incore_sb(mp,
-                                               XFS_SBS_FREXTENTS,
-                                               -(ralen), rsvd)) {
-                                               if (XFS_IS_QUOTA_ON(ip->i_mount))
-                                                       XFS_TRANS_UNRESERVE_BLKQUOTA(
-                                                               mp, NULL, ip,
-                                                               (long)alen);
-                                               break;
-                                       }
-                               } else {
-                                       if (xfs_mod_incore_sb(mp,
-                                                             XFS_SBS_FDBLOCKS,
-                                                             -(alen), rsvd)) {
-                                               if (XFS_IS_QUOTA_ON(ip->i_mount))
-                                                       XFS_TRANS_UNRESERVE_BLKQUOTA(
-                                                               mp, NULL, ip,
-                                                               (long)alen);
-                                               break;
+                               indlen = (xfs_extlen_t)
+                                       xfs_bmap_worst_indlen(ip, alen);
+                               ASSERT(indlen > 0);
+
+                               if (rt)
+                                       error = xfs_mod_incore_sb(mp,
+                                                       XFS_SBS_FREXTENTS,
+                                                       -(extsz), rsvd);
+                               else
+                                       error = xfs_mod_incore_sb(mp,
+                                                       XFS_SBS_FDBLOCKS,
+                                                       -(alen), rsvd);
+                               if (!error) {
+                                       error = xfs_mod_incore_sb(mp,
+                                                       XFS_SBS_FDBLOCKS,
+                                                       -(indlen), rsvd);
+                                       if (error && rt) {
+                                               xfs_mod_incore_sb(ip->i_mount,
+                                                       XFS_SBS_FREXTENTS,
+                                                       extsz, rsvd);
+                                       } else if (error) {
+                                               xfs_mod_incore_sb(ip->i_mount,
+                                                       XFS_SBS_FDBLOCKS,
+                                                       alen, rsvd);
                                        }
                                }
 
-                               if (xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
-                                               -(indlen), rsvd)) {
-                                       XFS_TRANS_UNRESERVE_BLKQUOTA(
-                                               mp, NULL, ip, (long)alen);
+                               if (error) {
+                                       if (XFS_IS_QUOTA_ON(ip->i_mount))
+                                               /* unreserve the blocks now */
+                                               XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
+                                                       mp, NULL, ip,
+                                                       (long)alen, 0, rt ?
+                                                       XFS_QMOPT_RES_RTBLKS :
+                                                       XFS_QMOPT_RES_REGBLKS);
                                        break;
                                }
+
                                ip->i_delayed_blks += alen;
                                abno = NULLSTARTBLOCK(indlen);
                        } else {
@@ -5389,13 +5437,24 @@ xfs_bunmapi(
                }
                if (wasdel) {
                        ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
-                       xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
-                               (int)del.br_blockcount, rsvd);
-                       /* Unreserve our quota space */
-                       XFS_TRANS_RESERVE_QUOTA_NBLKS(
-                               mp, NULL, ip, -((long)del.br_blockcount), 0,
-                               isrt ?  XFS_QMOPT_RES_RTBLKS :
+                       /* Update realtim/data freespace, unreserve quota */
+                       if (isrt) {
+                               xfs_filblks_t rtexts;
+
+                               rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
+                               do_div(rtexts, mp->m_sb.sb_rextsize);
+                               xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
+                                               (int)rtexts, rsvd);
+                               XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, NULL, ip,
+                                       -((long)del.br_blockcount), 0,
+                                       XFS_QMOPT_RES_RTBLKS);
+                       } else {
+                               xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
+                                               (int)del.br_blockcount, rsvd);
+                               XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, NULL, ip,
+                                       -((long)del.br_blockcount), 0,
                                        XFS_QMOPT_RES_REGBLKS);
+                       }
                        ip->i_delayed_blks -= del.br_blockcount;
                        if (cur)
                                cur->bc_private.b.flags |=
@@ -5666,12 +5725,13 @@ xfs_getbmap(
                        out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
                        out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
                        ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
-                       if (prealloced &&
-                           map[i].br_startblock == HOLESTARTBLOCK &&
-                           out.bmv_offset + out.bmv_length == bmvend) {
-                               /*
-                                * came to hole at end of file
-                                */
+                        if (map[i].br_startblock == HOLESTARTBLOCK &&
+                           ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
+                             whichfork == XFS_ATTR_FORK )) {
+                                /*
+                                 * came to hole at end of file or the end of
+                                   attribute fork
+                                 */
                                goto unlock_and_return;
                        } else {
                                out.bmv_block =
@@ -6105,7 +6165,7 @@ error0:
                xfs_trans_brelse(NULL, bp);
 error_norelse:
        cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
-               i, __FUNCTION__);
+               __FUNCTION__, i);
        panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
        return;
 }