fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / xfs / quota / xfs_qm_bhv.c
index 96b1d58..db8872b 100644 (file)
@@ -1,80 +1,66 @@
 /*
- * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 2000-2006 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_fs.h"
-#include "xfs_inum.h"
+#include "xfs_bit.h"
 #include "xfs_log.h"
+#include "xfs_inum.h"
 #include "xfs_clnt.h"
 #include "xfs_trans.h"
 #include "xfs_sb.h"
-#include "xfs_dir.h"
+#include "xfs_ag.h"
 #include "xfs_dir2.h"
 #include "xfs_alloc.h"
 #include "xfs_dmapi.h"
 #include "xfs_quota.h"
 #include "xfs_mount.h"
-#include "xfs_alloc_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.h"
+#include "xfs_ialloc.h"
+#include "xfs_itable.h"
+#include "xfs_btree.h"
 #include "xfs_bmap.h"
-#include "xfs_bit.h"
 #include "xfs_rtalloc.h"
 #include "xfs_error.h"
-#include "xfs_itable.h"
 #include "xfs_rw.h"
 #include "xfs_acl.h"
 #include "xfs_cap.h"
 #include "xfs_mac.h"
 #include "xfs_attr.h"
 #include "xfs_buf_item.h"
-
 #include "xfs_qm.h"
 
 #define MNTOPT_QUOTA   "quota"         /* disk quotas (user) */
 #define MNTOPT_NOQUOTA "noquota"       /* no quotas */
 #define MNTOPT_USRQUOTA        "usrquota"      /* user quota enabled */
 #define MNTOPT_GRPQUOTA        "grpquota"      /* group quota enabled */
+#define MNTOPT_PRJQUOTA        "prjquota"      /* project quota enabled */
 #define MNTOPT_UQUOTA  "uquota"        /* user quota (IRIX variant) */
 #define MNTOPT_GQUOTA  "gquota"        /* group quota (IRIX variant) */
+#define MNTOPT_PQUOTA  "pquota"        /* project quota (IRIX variant) */
 #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
 #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
+#define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
 #define MNTOPT_QUOTANOENF  "qnoenforce"        /* same as uqnoenforce */
 
 STATIC int
@@ -109,6 +95,14 @@ xfs_qm_parseargs(
                        args->flags |= XFSMNT_UQUOTA;
                        args->flags &= ~XFSMNT_UQUOTAENF;
                        referenced = 1;
+               } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
+                          !strcmp(this_char, MNTOPT_PRJQUOTA)) {
+                       args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
+                       referenced = 1;
+               } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
+                       args->flags |= XFSMNT_PQUOTA;
+                       args->flags &= ~XFSMNT_PQUOTAENF;
+                       referenced = 1;
                } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
                           !strcmp(this_char, MNTOPT_GRPQUOTA)) {
                        args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
@@ -127,7 +121,13 @@ xfs_qm_parseargs(
                        *this_char++ = ',';
        }
 
-       PVFS_PARSEARGS(BHV_NEXT(bhv), options, args, update, error);
+       if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
+               cmn_err(CE_WARN,
+                       "XFS: cannot mount with both project and group quota");
+               return XFS_ERROR(EINVAL);
+       }
+
+       error = bhv_next_vfs_parseargs(BHV_NEXT(bhv), options, args, update);
        if (!error && !referenced)
                bhv_remove_vfsops(bhvtovfs(bhv), VFS_POSITION_QM);
        return error;
@@ -138,9 +138,8 @@ xfs_qm_showargs(
        struct bhv_desc         *bhv,
        struct seq_file         *m)
 {
-       struct vfs              *vfsp = bhvtovfs(bhv);
+       struct bhv_vfs          *vfsp = bhvtovfs(bhv);
        struct xfs_mount        *mp = XFS_VFSTOM(vfsp);
-       int                     error;
 
        if (mp->m_qflags & XFS_UQUOTA_ACCT) {
                (mp->m_qflags & XFS_UQUOTA_ENFD) ?
@@ -148,17 +147,22 @@ xfs_qm_showargs(
                        seq_puts(m, "," MNTOPT_UQUOTANOENF);
        }
 
+       if (mp->m_qflags & XFS_PQUOTA_ACCT) {
+               (mp->m_qflags & XFS_OQUOTA_ENFD) ?
+                       seq_puts(m, "," MNTOPT_PRJQUOTA) :
+                       seq_puts(m, "," MNTOPT_PQUOTANOENF);
+       }
+
        if (mp->m_qflags & XFS_GQUOTA_ACCT) {
-               (mp->m_qflags & XFS_GQUOTA_ENFD) ?
+               (mp->m_qflags & XFS_OQUOTA_ENFD) ?
                        seq_puts(m, "," MNTOPT_GRPQUOTA) :
                        seq_puts(m, "," MNTOPT_GQUOTANOENF);
        }
 
-       if (!(mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT)))
+       if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
                seq_puts(m, "," MNTOPT_NOQUOTA);
 
-       PVFS_SHOWARGS(BHV_NEXT(bhv), m, error);
-       return error;
+       return bhv_next_vfs_showargs(BHV_NEXT(bhv), m);
 }
 
 STATIC int
@@ -167,14 +171,74 @@ xfs_qm_mount(
        struct xfs_mount_args   *args,
        struct cred             *cr)
 {
-       struct vfs              *vfsp = bhvtovfs(bhv);
+       struct bhv_vfs          *vfsp = bhvtovfs(bhv);
        struct xfs_mount        *mp = XFS_VFSTOM(vfsp);
-       int                     error;
 
-       if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA))
+       if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA | XFSMNT_PQUOTA))
                xfs_qm_mount_quotainit(mp, args->flags);
-       PVFS_MOUNT(BHV_NEXT(bhv), args, cr, error);
-       return error;
+       return bhv_next_vfs_mount(BHV_NEXT(bhv), args, cr);
+}
+
+/*
+ * Directory tree accounting is implemented using project quotas, where
+ * the project identifier is inherited from parent directories.
+ * A statvfs (df, etc.) of a directory that is using project quota should
+ * return a statvfs of the project, not the entire filesystem.
+ * This makes such trees appear as if they are filesystems in themselves.
+ */
+STATIC int
+xfs_qm_statvfs(
+       struct bhv_desc         *bhv,
+       bhv_statvfs_t           *statp,
+       struct bhv_vnode        *vnode)
+{
+       xfs_mount_t             *mp;
+       xfs_inode_t             *ip;
+       xfs_dquot_t             *dqp;
+       xfs_disk_dquot_t        *dp;
+       __uint64_t              limit;
+       int                     error;
+
+       error = bhv_next_vfs_statvfs(BHV_NEXT(bhv), statp, vnode);
+       if (error || !vnode)
+               return error;
+
+       mp = xfs_vfstom(bhvtovfs(bhv));
+       ip = xfs_vtoi(vnode);
+
+       if (!(ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
+               return 0;
+       if (!(mp->m_qflags & XFS_PQUOTA_ACCT))
+               return 0;
+       if (!(mp->m_qflags & XFS_OQUOTA_ENFD))
+               return 0;
+
+       if (xfs_qm_dqget(mp, NULL, ip->i_d.di_projid, XFS_DQ_PROJ, 0, &dqp))
+               return 0;
+       dp = &dqp->q_core;
+
+       limit = dp->d_blk_softlimit ?
+               be64_to_cpu(dp->d_blk_softlimit) :
+               be64_to_cpu(dp->d_blk_hardlimit);
+       if (limit && statp->f_blocks > limit) {
+               statp->f_blocks = limit;
+               statp->f_bfree =
+                       (statp->f_blocks > be64_to_cpu(dp->d_bcount)) ?
+                        (statp->f_blocks - be64_to_cpu(dp->d_bcount)) : 0;
+       }
+
+       limit = dp->d_ino_softlimit ?
+               be64_to_cpu(dp->d_ino_softlimit) :
+               be64_to_cpu(dp->d_ino_hardlimit);
+       if (limit && statp->f_files > limit) {
+               statp->f_files = limit;
+               statp->f_ffree =
+                       (statp->f_files > be64_to_cpu(dp->d_icount)) ?
+                        (statp->f_ffree - be64_to_cpu(dp->d_icount)) : 0;
+       }
+
+       xfs_qm_dqput(dqp);
+       return 0;
 }
 
 STATIC int
@@ -183,7 +247,7 @@ xfs_qm_syncall(
        int                     flags,
        cred_t                  *credp)
 {
-       struct vfs              *vfsp = bhvtovfs(bhv);
+       struct bhv_vfs          *vfsp = bhvtovfs(bhv);
        struct xfs_mount        *mp = XFS_VFSTOM(vfsp);
        int                     error;
 
@@ -202,49 +266,7 @@ xfs_qm_syncall(
                        }
                }
        }
-       PVFS_SYNC(BHV_NEXT(bhv), flags, credp, error);
-       return error;
-}
-
-/*
- * When xfsquotas isn't installed and the superblock had quotas, we need to
- * clear the quotaflags from superblock.
- */
-STATIC void
-xfs_mount_reset_sbqflags(
-       xfs_mount_t             *mp)
-{
-       xfs_trans_t             *tp;
-       unsigned long           s;
-
-       mp->m_qflags = 0;
-       /*
-        * It is OK to look at sb_qflags here in mount path,
-        * without SB_LOCK.
-        */
-       if (mp->m_sb.sb_qflags == 0)
-               return;
-       s = XFS_SB_LOCK(mp);
-       mp->m_sb.sb_qflags = 0;
-       XFS_SB_UNLOCK(mp, s);
-
-       /*
-        * if the fs is readonly, let the incore superblock run
-        * with quotas off but don't flush the update out to disk
-        */
-       if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
-               return;
-#ifdef QUOTADEBUG
-       xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
-#endif
-       tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
-       if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
-                                     XFS_DEFAULT_LOG_COUNT)) {
-               xfs_trans_cancel(tp, 0);
-               return;
-       }
-       xfs_mod_sb(tp, XFS_SB_QFLAGS);
-       xfs_trans_commit(tp, 0, NULL);
+       return bhv_next_vfs_sync(BHV_NEXT(bhv), flags, credp);
 }
 
 STATIC int
@@ -254,16 +276,17 @@ xfs_qm_newmount(
        uint            *quotaflags)
 {
        uint            quotaondisk;
-       uint            uquotaondisk = 0, gquotaondisk = 0;
+       uint            uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
 
        *quotaflags = 0;
        *needquotamount = B_FALSE;
 
        quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
-               mp->m_sb.sb_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT);
+                               (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
 
        if (quotaondisk) {
                uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
+               pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
                gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
        }
 
@@ -276,13 +299,16 @@ xfs_qm_newmount(
 
        if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
            (!uquotaondisk &&  XFS_IS_UQUOTA_ON(mp)) ||
+            (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
+           (!pquotaondisk &&  XFS_IS_PQUOTA_ON(mp)) ||
             (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
-           (!gquotaondisk &&  XFS_IS_GQUOTA_ON(mp)))  &&
+           (!gquotaondisk &&  XFS_IS_OQUOTA_ON(mp)))  &&
            xfs_dev_is_read_only(mp, "changing quota state")) {
                cmn_err(CE_WARN,
-                       "XFS: please mount with%s%s%s.",
+                       "XFS: please mount with%s%s%s%s.",
                        (!quotaondisk ? "out quota" : ""),
                        (uquotaondisk ? " usrquota" : ""),
+                       (pquotaondisk ? " prjquota" : ""),
                        (gquotaondisk ? " grpquota" : ""));
                return XFS_ERROR(EPERM);
        }
@@ -294,15 +320,12 @@ xfs_qm_newmount(
                 */
                if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
                        /*
-                        * If the xfs quota code isn't installed,
-                        * we have to reset the quotachk'd bit.
                         * If an error occured, qm_mount_quotas code
                         * has already disabled quotas. So, just finish
                         * mounting, and get on with the boring life
                         * without disk quotas.
                         */
-                       if (xfs_qm_mount_quotas(mp))
-                               xfs_mount_reset_sbqflags(mp);
+                       xfs_qm_mount_quotas(mp, 0);
                } else {
                        /*
                         * Clear the quota flags, but remember them. This
@@ -324,13 +347,13 @@ STATIC int
 xfs_qm_endmount(
        xfs_mount_t     *mp,
        uint            needquotamount,
-       uint            quotaflags)
+       uint            quotaflags,
+       int             mfsi_flags)
 {
        if (needquotamount) {
                ASSERT(mp->m_qflags == 0);
                mp->m_qflags = quotaflags;
-               if (xfs_qm_mount_quotas(mp))
-                       xfs_mount_reset_sbqflags(mp);
+               xfs_qm_mount_quotas(mp, mfsi_flags);
        }
 
 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
@@ -361,7 +384,7 @@ xfs_qm_dqrele_null(
 }
 
 
-struct xfs_qmops xfs_qmcore_xfs = {
+STATIC struct xfs_qmops xfs_qmcore_xfs = {
        .xfs_qminit             = xfs_qm_newmount,
        .xfs_qmdone             = xfs_qm_unmount_quotadestroy,
        .xfs_qmmount            = xfs_qm_endmount,
@@ -378,11 +401,12 @@ struct xfs_qmops xfs_qmcore_xfs = {
        .xfs_dqtrxops           = &xfs_trans_dquot_ops,
 };
 
-struct bhv_vfsops xfs_qmops = { {
+struct bhv_module_vfsops xfs_qmops = { {
        BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
        .vfs_parseargs          = xfs_qm_parseargs,
        .vfs_showargs           = xfs_qm_showargs,
        .vfs_mount              = xfs_qm_mount,
+       .vfs_statvfs            = xfs_qm_statvfs,
        .vfs_sync               = xfs_qm_syncall,
        .vfs_quotactl           = xfs_qm_quotactl, },
 };
@@ -395,7 +419,7 @@ xfs_qm_init(void)
                KERN_INFO "SGI XFS Quota Management subsystem\n";
 
        printk(message);
-       mutex_init(&xfs_Gqm_lock, MUTEX_DEFAULT, "xfs_qmlock");
+       mutex_init(&xfs_Gqm_lock);
        vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
        xfs_qm_init_procfs();
 }
@@ -406,7 +430,7 @@ xfs_qm_exit(void)
        vfs_bhv_clr_custom(&xfs_qmops);
        xfs_qm_cleanup_procfs();
        if (qm_dqzone)
-               kmem_cache_destroy(qm_dqzone);
+               kmem_zone_destroy(qm_dqzone);
        if (qm_dqtrxzone)
-               kmem_cache_destroy(qm_dqtrxzone);
+               kmem_zone_destroy(qm_dqtrxzone);
 }