Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / xfs / quota / xfs_qm.c
index 9633b15..e23e455 100644 (file)
@@ -1,64 +1,47 @@
 /*
- * Copyright (c) 2000-2002 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_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_ag.h"
-#include "xfs_dir.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_bmap.h"
-#include "xfs_bit.h"
+#include "xfs_btree.h"
+#include "xfs_ialloc.h"
+#include "xfs_itable.h"
 #include "xfs_rtalloc.h"
 #include "xfs_error.h"
-#include "xfs_itable.h"
+#include "xfs_bmap.h"
 #include "xfs_rw.h"
 #include "xfs_acl.h"
 #include "xfs_cap.h"
@@ -67,7 +50,6 @@
 #include "xfs_buf_item.h"
 #include "xfs_trans_space.h"
 #include "xfs_utils.h"
-
 #include "xfs_qm.h"
 
 /*
  * quota functionality, including maintaining the freelist and hash
  * tables of dquots.
  */
-mutex_t xfs_Gqm_lock;
+mutex_t                xfs_Gqm_lock;
 struct xfs_qm  *xfs_Gqm;
+uint           ndquot;
 
 kmem_zone_t    *qm_dqzone;
 kmem_zone_t    *qm_dqtrxzone;
-kmem_shaker_t  xfs_qm_shaker;
+STATIC kmem_shaker_t   xfs_qm_shaker;
+
+STATIC cred_t  xfs_zerocr;
+STATIC xfs_inode_t     xfs_zeroino;
 
 STATIC void    xfs_qm_list_init(xfs_dqlist_t *, char *, int);
 STATIC void    xfs_qm_list_destroy(xfs_dqlist_t *);
-STATIC int     xfs_qm_quotacheck(xfs_mount_t *);
+
+STATIC void    xfs_qm_freelist_init(xfs_frlist_t *);
+STATIC void    xfs_qm_freelist_destroy(xfs_frlist_t *);
+STATIC int     xfs_qm_mplist_nowait(xfs_mount_t *);
+STATIC int     xfs_qm_dqhashlock_nowait(xfs_dquot_t *);
 
 STATIC int     xfs_qm_init_quotainos(xfs_mount_t *);
-STATIC int     xfs_qm_shake(int, unsigned int);
+STATIC int     xfs_qm_init_quotainfo(xfs_mount_t *);
+STATIC int     xfs_qm_shake(int, gfp_t);
 
 #ifdef DEBUG
 extern mutex_t qcheck_lock;
@@ -102,10 +93,10 @@ extern mutex_t     qcheck_lock;
        for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \
                cmn_err(CE_DEBUG, "   %d.  \"%d (%s)\"   " \
                                  "bcnt = %d, icnt = %d, refs = %d", \
-                       ++i, (int) INT_GET(dqp->q_core.d_id, ARCH_CONVERT), \
+                       ++i, (int) be32_to_cpu(dqp->q_core.d_id), \
                        DQFLAGTO_TYPESTR(dqp),       \
-                       (int) INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT), \
-                       (int) INT_GET(dqp->q_core.d_icount, ARCH_CONVERT), \
+                       (int) be64_to_cpu(dqp->q_core.d_bcount), \
+                       (int) be64_to_cpu(dqp->q_core.d_icount), \
                        (int) dqp->q_nrefs);  } \
 }
 #else
@@ -119,25 +110,25 @@ extern mutex_t    qcheck_lock;
 STATIC struct xfs_qm *
 xfs_Gqm_init(void)
 {
-       xfs_qm_t                *xqm;
-       int                     hsize, i;
-
-       xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
-       ASSERT(xqm);
+       xfs_dqhash_t    *udqhash, *gdqhash;
+       xfs_qm_t        *xqm;
+       uint            i, hsize, flags = KM_SLEEP | KM_MAYFAIL;
 
        /*
         * Initialize the dquot hash tables.
         */
-       hsize = (DQUOT_HASH_HEURISTIC < XFS_QM_NCSIZE_THRESHOLD) ?
-               XFS_QM_HASHSIZE_LOW : XFS_QM_HASHSIZE_HIGH;
-       xqm->qm_dqhashmask = hsize - 1;
+       hsize = XFS_QM_HASHSIZE_HIGH;
+       while (!(udqhash = kmem_zalloc(hsize * sizeof(xfs_dqhash_t), flags))) {
+               if ((hsize >>= 1) <= XFS_QM_HASHSIZE_LOW)
+                       flags = KM_SLEEP;
+       }
+       gdqhash = kmem_zalloc(hsize * sizeof(xfs_dqhash_t), KM_SLEEP);
+       ndquot = hsize << 8;
 
-       xqm->qm_usr_dqhtable = (xfs_dqhash_t *)kmem_zalloc(hsize *
-                                                     sizeof(xfs_dqhash_t),
-                                                     KM_SLEEP);
-       xqm->qm_grp_dqhtable = (xfs_dqhash_t *)kmem_zalloc(hsize *
-                                                     sizeof(xfs_dqhash_t),
-                                                     KM_SLEEP);
+       xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
+       xqm->qm_dqhashmask = hsize - 1;
+       xqm->qm_usr_dqhtable = udqhash;
+       xqm->qm_grp_dqhtable = gdqhash;
        ASSERT(xqm->qm_usr_dqhtable != NULL);
        ASSERT(xqm->qm_grp_dqhtable != NULL);
 
@@ -177,7 +168,7 @@ xfs_Gqm_init(void)
        xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
        xqm->qm_nrefs = 0;
 #ifdef DEBUG
-       mutex_init(&qcheck_lock, MUTEX_DEFAULT, "qchk");
+       mutex_init(&qcheck_lock);
 #endif
        return xqm;
 }
@@ -185,7 +176,7 @@ xfs_Gqm_init(void)
 /*
  * Destroy the global quota manager when its reference count goes to zero.
  */
-void
+STATIC void
 xfs_qm_destroy(
        struct xfs_qm   *xqm)
 {
@@ -229,11 +220,8 @@ xfs_qm_hold_quotafs_ref(
         */
        XFS_QM_LOCK(xfs_Gqm);
 
-       if (xfs_Gqm == NULL) {
-               if ((xfs_Gqm = xfs_Gqm_init()) == NULL) {
-                       return (XFS_ERROR(EINVAL));
-               }
-       }
+       if (xfs_Gqm == NULL)
+               xfs_Gqm = xfs_Gqm_init();
        /*
         * We can keep a list of all filesystems with quotas mounted for
         * debugging and statistical purposes, but ...
@@ -299,7 +287,7 @@ xfs_qm_rele_quotafs_ref(
 
 /*
  * This is called at mount time from xfs_mountfs to initialize the quotainfo
- * structure and start the global quotamanager (xfs_Gqm) if it hasn't done
+ * structure and start the global quota manager (xfs_Gqm) if it hasn't done
  * so already. Note that the superblock has not been read in yet.
  */
 void
@@ -308,9 +296,9 @@ xfs_qm_mount_quotainit(
        uint            flags)
 {
        /*
-        * User or group quotas has to be on.
+        * User, projects or group quotas has to be on.
         */
-       ASSERT(flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA));
+       ASSERT(flags & (XFSMNT_UQUOTA | XFSMNT_PQUOTA | XFSMNT_GQUOTA));
 
        /*
         * Initialize the flags in the mount structure. From this point
@@ -328,7 +316,11 @@ xfs_qm_mount_quotainit(
        if (flags & XFSMNT_GQUOTA) {
                mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
                if (flags & XFSMNT_GQUOTAENF)
-                       mp->m_qflags |= XFS_GQUOTA_ENFD;
+                       mp->m_qflags |= XFS_OQUOTA_ENFD;
+       } else if (flags & XFSMNT_PQUOTA) {
+               mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
+               if (flags & XFSMNT_PQUOTAENF)
+                       mp->m_qflags |= XFS_OQUOTA_ENFD;
        }
 }
 
@@ -352,22 +344,13 @@ xfs_qm_unmount_quotadestroy(
  */
 int
 xfs_qm_mount_quotas(
-       xfs_mount_t     *mp)
+       xfs_mount_t     *mp,
+       int             mfsi_flags)
 {
        unsigned long   s;
        int             error = 0;
        uint            sbf;
 
-       /*
-        * If a file system had quotas running earlier, but decided to
-        * mount without -o quota/uquota/gquota options, revoke the
-        * quotachecked license, and bail out.
-        */
-       if (! XFS_IS_QUOTA_ON(mp) &&
-           (mp->m_sb.sb_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT))) {
-               mp->m_qflags = 0;
-               goto write_changes;
-       }
 
        /*
         * If quotas on realtime volumes is not supported, we disable
@@ -381,11 +364,8 @@ xfs_qm_mount_quotas(
                goto write_changes;
        }
 
-#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
-       cmn_err(CE_NOTE, "Attempting to turn on disk quotas.");
-#endif
-
        ASSERT(XFS_IS_QUOTA_RUNNING(mp));
+
        /*
         * Allocate the quotainfo structure inside the mount struct, and
         * create quotainode(s), and change/rev superblock if necessary.
@@ -401,27 +381,16 @@ xfs_qm_mount_quotas(
        /*
         * If any of the quotas are not consistent, do a quotacheck.
         */
-       if (XFS_QM_NEED_QUOTACHECK(mp)) {
-#ifdef DEBUG
-               cmn_err(CE_NOTE, "Doing a quotacheck. Please wait.");
-#endif
+       if (XFS_QM_NEED_QUOTACHECK(mp) &&
+               !(mfsi_flags & XFS_MFSI_NO_QUOTACHECK)) {
                if ((error = xfs_qm_quotacheck(mp))) {
-                       cmn_err(CE_WARN, "Quotacheck unsuccessful (Error %d): "
-                               "Disabling quotas.",
-                               error);
-                       /*
-                        * We must turn off quotas.
+                       /* Quotacheck has failed and quotas have
+                        * been disabled.
                         */
-                       ASSERT(mp->m_quotainfo != NULL);
-                       ASSERT(xfs_Gqm != NULL);
-                       xfs_qm_destroy_quotainfo(mp);
-                       mp->m_qflags = 0;
-                       goto write_changes;
+                       return XFS_ERROR(error);
                }
-#ifdef DEBUG
-               cmn_err(CE_NOTE, "Done quotacheck.");
-#endif
        }
+
  write_changes:
        /*
         * We actually don't have to acquire the SB_LOCK at all.
@@ -518,7 +487,7 @@ out:
  * Flush all dquots of the given file system to disk. The dquots are
  * _not_ purged from memory here, just their data written to disk.
  */
-int
+STATIC int
 xfs_qm_dqflush_all(
        xfs_mount_t     *mp,
        int             flags)
@@ -529,7 +498,7 @@ xfs_qm_dqflush_all(
        int             error;
 
        if (mp->m_quotainfo == NULL)
-               return (0);
+               return 0;
        niters = 0;
 again:
        xfs_qm_mplist_lock(mp);
@@ -560,7 +529,7 @@ again:
                error = xfs_qm_dqflush(dqp, flags);
                xfs_dqunlock(dqp);
                if (error)
-                       return (error);
+                       return error;
 
                xfs_qm_mplist_lock(mp);
                if (recl != XFS_QI_MPLRECLAIMS(mp)) {
@@ -572,7 +541,7 @@ again:
 
        xfs_qm_mplist_unlock(mp);
        /* return ! busy */
-       return (0);
+       return 0;
 }
 /*
  * Release the group dquot pointers the user dquots may be
@@ -622,7 +591,7 @@ xfs_qm_detach_gdquots(
 STATIC int
 xfs_qm_dqpurge_int(
        xfs_mount_t     *mp,
-       uint            flags) /* QUOTAOFF/UMOUNTING/UQUOTA/GQUOTA */
+       uint            flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */
 {
        xfs_dquot_t     *dqp;
        uint            dqtype;
@@ -631,9 +600,10 @@ xfs_qm_dqpurge_int(
        int             nmisses;
 
        if (mp->m_quotainfo == NULL)
-               return (0);
+               return 0;
 
        dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
+       dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
        dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
 
        xfs_qm_mplist_lock(mp);
@@ -743,11 +713,11 @@ xfs_qm_dqattach_one(
 
        /*
         * udqhint is the i_udquot field in inode, and is non-NULL only
-        * when the type arg is XFS_DQ_GROUP. Its purpose is to save a
+        * when the type arg is group/project. Its purpose is to save a
         * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
         * the user dquot.
         */
-       ASSERT(!udqhint || type == XFS_DQ_GROUP);
+       ASSERT(!udqhint || type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
        if (udqhint && !dolock)
                xfs_dqlock(udqhint);
 
@@ -759,7 +729,7 @@ xfs_qm_dqattach_one(
         */
        if (udqhint &&
            (dqp = udqhint->q_gdquot) &&
-           (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id)) {
+           (be32_to_cpu(dqp->q_core.d_id) == id)) {
                ASSERT(XFS_DQ_IS_LOCKED(udqhint));
                xfs_dqlock(dqp);
                XFS_DQHOLD(dqp);
@@ -827,7 +797,7 @@ xfs_qm_dqattach_one(
                        ASSERT(XFS_DQ_IS_LOCKED(dqp));
        }
 #endif
-       return (error);
+       return error;
 }
 
 
@@ -835,7 +805,7 @@ xfs_qm_dqattach_one(
  * Given a udquot and gdquot, attach a ptr to the group dquot in the
  * udquot as a hint for future lookups. The idea sounds simple, but the
  * execution isn't, because the udquot might have a group dquot attached
- * already and getting rid of that gets us into lock ordering contraints.
+ * already and getting rid of that gets us into lock ordering constraints.
  * The process is complicated more by the fact that the dquots may or may not
  * be locked on entry.
  */
@@ -906,8 +876,8 @@ xfs_qm_dqattach_grouphint(
 
 
 /*
- * Given a locked inode, attach dquot(s) to it, taking UQUOTAON / GQUOTAON
- * in to account.
+ * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
+ * into account.
  * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
  * If XFS_QMOPT_DQLOCK, the dquot(s) will be returned locked. This option pretty
  * much made this code a complete mess, but it has been pretty useful.
@@ -928,7 +898,7 @@ xfs_qm_dqattach(
            (! XFS_NOT_DQATTACHED(mp, ip)) ||
            (ip->i_ino == mp->m_sb.sb_uquotino) ||
            (ip->i_ino == mp->m_sb.sb_gquotino))
-               return (0);
+               return 0;
 
        ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 ||
               XFS_ISLOCKED_INODE_EXCL(ip));
@@ -946,8 +916,13 @@ xfs_qm_dqattach(
                nquotas++;
        }
        ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
-       if (XFS_IS_GQUOTA_ON(mp)) {
-               error = xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
+       if (XFS_IS_OQUOTA_ON(mp)) {
+               error = XFS_IS_GQUOTA_ON(mp) ?
+                       xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
+                                               flags & XFS_QMOPT_DQALLOC,
+                                               flags & XFS_QMOPT_DQLOCK,
+                                               ip->i_udquot, &ip->i_gdquot) :
+                       xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ,
                                                flags & XFS_QMOPT_DQALLOC,
                                                flags & XFS_QMOPT_DQLOCK,
                                                ip->i_udquot, &ip->i_gdquot);
@@ -998,7 +973,7 @@ xfs_qm_dqattach(
                }
                if (XFS_IS_UQUOTA_ON(mp))
                        ASSERT(ip->i_udquot);
-               if (XFS_IS_GQUOTA_ON(mp))
+               if (XFS_IS_OQUOTA_ON(mp))
                        ASSERT(ip->i_gdquot);
        }
 #endif
@@ -1010,7 +985,7 @@ xfs_qm_dqattach(
        else
                ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
 #endif
-       return (error);
+       return error;
 }
 
 /*
@@ -1027,13 +1002,13 @@ xfs_qm_dqdetach(
 
        ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
        ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
-       if (ip->i_udquot)
-               xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
        if (ip->i_udquot) {
+               xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
                xfs_qm_dqrele(ip->i_udquot);
                ip->i_udquot = NULL;
        }
        if (ip->i_gdquot) {
+               xfs_dqtrace_entry_ino(ip->i_gdquot, "DQDETTACH", ip);
                xfs_qm_dqrele(ip->i_gdquot);
                ip->i_gdquot = NULL;
        }
@@ -1075,7 +1050,7 @@ xfs_qm_sync(
         */
        if (! XFS_IS_QUOTA_ON(mp)) {
                xfs_qm_mplist_unlock(mp);
-               return (0);
+               return 0;
        }
        FOREACH_DQUOT_IN_MP(dqp, mp) {
                /*
@@ -1117,10 +1092,10 @@ xfs_qm_sync(
                        }
                        /*
                         * If we can't grab the flush lock then if the caller
-                        * really wanted us to give this our best shot,
+                        * really wanted us to give this our best shot, so
                         * see if we can give a push to the buffer before we wait
                         * on the flush lock. At this point, we know that
-                        * eventhough the dquot is being flushed,
+                        * even though the dquot is being flushed,
                         * it has (new) dirty data.
                         */
                        xfs_qm_dqflock_pushbuf_wait(dqp);
@@ -1135,9 +1110,9 @@ xfs_qm_sync(
                error = xfs_qm_dqflush(dqp, flush_flags);
                xfs_dqunlock(dqp);
                if (error && XFS_FORCED_SHUTDOWN(mp))
-                       return(0);      /* Need to prevent umount failure */
+                       return 0;       /* Need to prevent umount failure */
                else if (error)
-                       return (error);
+                       return error;
 
                xfs_qm_mplist_lock(mp);
                if (recl != XFS_QI_MPLRECLAIMS(mp)) {
@@ -1150,7 +1125,7 @@ xfs_qm_sync(
        }
 
        xfs_qm_mplist_unlock(mp);
-       return (0);
+       return 0;
 }
 
 
@@ -1158,7 +1133,7 @@ xfs_qm_sync(
  * This initializes all the quota information that's kept in the
  * mount structure
  */
-int
+STATIC int
 xfs_qm_init_quotainfo(
        xfs_mount_t     *mp)
 {
@@ -1172,7 +1147,7 @@ xfs_qm_init_quotainfo(
         * Tell XQM that we exist as soon as possible.
         */
        if ((error = xfs_qm_hold_quotafs_ref(mp))) {
-               return (error);
+               return error;
        }
 
        qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
@@ -1184,7 +1159,7 @@ xfs_qm_init_quotainfo(
        if ((error = xfs_qm_init_quotainos(mp))) {
                kmem_free(qinf, sizeof(xfs_quotainfo_t));
                mp->m_quotainfo = NULL;
-               return (error);
+               return error;
        }
 
        spinlock_init(&qinf->qi_pinlock, "xfs_qinf_pin");
@@ -1192,7 +1167,7 @@ xfs_qm_init_quotainfo(
        qinf->qi_dqreclaims = 0;
 
        /* mutex used to serialize quotaoffs */
-       mutex_init(&qinf->qi_quotaofflock, MUTEX_DEFAULT, "qoff");
+       mutex_init(&qinf->qi_quotaofflock);
 
        /* Precalc some constants */
        qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
@@ -1211,27 +1186,38 @@ xfs_qm_init_quotainfo(
         * and group quotas, at least not at this point.
         */
        error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
-                            (XFS_IS_UQUOTA_RUNNING(mp)) ?
-                            XFS_DQ_USER : XFS_DQ_GROUP,
+                            XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER : 
+                            (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
+                               XFS_DQ_PROJ),
                             XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
                             &dqp);
        if (! error) {
+               xfs_disk_dquot_t        *ddqp = &dqp->q_core;
+
                /*
                 * The warnings and timers set the grace period given to
                 * a user or group before he or she can not perform any
                 * more writing. If it is zero, a default is used.
                 */
-               qinf->qi_btimelimit = INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT) ?
-                       INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT) : XFS_QM_BTIMELIMIT;
-               qinf->qi_itimelimit = INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT) ?
-                       INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT) : XFS_QM_ITIMELIMIT;
-               qinf->qi_rtbtimelimit = INT_GET(dqp->q_core.d_rtbtimer, ARCH_CONVERT) ?
-                       INT_GET(dqp->q_core.d_rtbtimer, ARCH_CONVERT) : XFS_QM_RTBTIMELIMIT;
-               qinf->qi_bwarnlimit = INT_GET(dqp->q_core.d_bwarns, ARCH_CONVERT) ?
-                       INT_GET(dqp->q_core.d_bwarns, ARCH_CONVERT) : XFS_QM_BWARNLIMIT;
-               qinf->qi_iwarnlimit = INT_GET(dqp->q_core.d_iwarns, ARCH_CONVERT) ?
-                       INT_GET(dqp->q_core.d_iwarns, ARCH_CONVERT) : XFS_QM_IWARNLIMIT;
-
+               qinf->qi_btimelimit = ddqp->d_btimer ?
+                       be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
+               qinf->qi_itimelimit = ddqp->d_itimer ?
+                       be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
+               qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
+                       be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
+               qinf->qi_bwarnlimit = ddqp->d_bwarns ?
+                       be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
+               qinf->qi_iwarnlimit = ddqp->d_iwarns ?
+                       be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
+               qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
+                       be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
+               qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
+               qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
+               qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
+               qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
+               qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
+               qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
                /*
                 * We sent the XFS_QMOPT_DQSUSER flag to dqget because
                 * we don't want this dquot cached. We haven't done a
@@ -1244,9 +1230,10 @@ xfs_qm_init_quotainfo(
                qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
                qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
                qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
+               qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
        }
 
-       return (0);
+       return 0;
 }
 
 
@@ -1299,7 +1286,7 @@ xfs_qm_list_init(
        char            *str,
        int             n)
 {
-       mutex_init(&list->qh_lock, MUTEX_DEFAULT, str);
+       mutex_init(&list->qh_lock);
        list->qh_next = NULL;
        list->qh_version = 0;
        list->qh_nelems = 0;
@@ -1346,23 +1333,30 @@ xfs_qm_dqget_noattach(
                         */
                        ASSERT(error != ESRCH);
                        ASSERT(error != ENOENT);
-                       return (error);
+                       return error;
                }
                ASSERT(udqp);
        }
 
-       if (XFS_IS_GQUOTA_ON(mp)) {
+       if (XFS_IS_OQUOTA_ON(mp)) {
                ASSERT(ip->i_gdquot == NULL);
                if (udqp)
                        xfs_dqunlock(udqp);
-               if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_gid, XFS_DQ_GROUP,
-                                        XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
-                                        &gdqp))) {
+               error = XFS_IS_GQUOTA_ON(mp) ?
+                               xfs_qm_dqget(mp, ip,
+                                            ip->i_d.di_gid, XFS_DQ_GROUP,
+                                            XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
+                                            &gdqp) :
+                               xfs_qm_dqget(mp, ip,
+                                            ip->i_d.di_projid, XFS_DQ_PROJ,
+                                            XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
+                                            &gdqp);
+               if (error) {
                        if (udqp)
                                xfs_qm_dqrele(udqp);
                        ASSERT(error != ESRCH);
                        ASSERT(error != ENOENT);
-                       return (error);
+                       return error;
                }
                ASSERT(gdqp);
 
@@ -1383,7 +1377,7 @@ xfs_qm_dqget_noattach(
        if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
        if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
 #endif
-       return (0);
+       return 0;
 }
 
 /*
@@ -1399,26 +1393,24 @@ xfs_qm_qino_alloc(
 {
        xfs_trans_t     *tp;
        int             error;
-       unsigned long s;
-       cred_t          zerocr;
+       unsigned long   s;
        int             committed;
 
-       tp = xfs_trans_alloc(mp,XFS_TRANS_QM_QINOCREATE);
+       tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
        if ((error = xfs_trans_reserve(tp,
                                      XFS_QM_QINOCREATE_SPACE_RES(mp),
                                      XFS_CREATE_LOG_RES(mp), 0,
                                      XFS_TRANS_PERM_LOG_RES,
                                      XFS_CREATE_LOG_COUNT))) {
                xfs_trans_cancel(tp, 0);
-               return (error);
+               return error;
        }
-       memset(&zerocr, 0, sizeof(zerocr));
 
-       if ((error = xfs_dir_ialloc(&tp, mp->m_rootip, S_IFREG, 1, 0,
-                                  &zerocr, 0, 1, ip, &committed))) {
+       if ((error = xfs_dir_ialloc(&tp, &xfs_zeroino, S_IFREG, 1, 0,
+                                  &xfs_zerocr, 0, 1, ip, &committed))) {
                xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
                                 XFS_TRANS_ABORT);
-               return (error);
+               return error;
        }
 
        /*
@@ -1466,9 +1458,9 @@ xfs_qm_qino_alloc(
        if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
                                     NULL))) {
                xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
-               return (error);
+               return error;
        }
-       return (0);
+       return 0;
 }
 
 
@@ -1497,21 +1489,23 @@ xfs_qm_reset_dqcounts(
                /*
                 * Do a sanity check, and if needed, repair the dqblk. Don't
                 * output any warnings because it's perfectly possible to
-                * find unitialized dquot blks. See comment in xfs_qm_dqcheck.
+                * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
                 */
                (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR,
                                      "xfs_quotacheck");
-               INT_SET(ddq->d_bcount, ARCH_CONVERT, 0ULL);
-               INT_SET(ddq->d_icount, ARCH_CONVERT, 0ULL);
-               INT_SET(ddq->d_rtbcount, ARCH_CONVERT, 0ULL);
-               INT_SET(ddq->d_btimer, ARCH_CONVERT, (time_t)0);
-               INT_SET(ddq->d_itimer, ARCH_CONVERT, (time_t)0);
-               INT_SET(ddq->d_bwarns, ARCH_CONVERT, 0UL);
-               INT_SET(ddq->d_iwarns, ARCH_CONVERT, 0UL);
+               ddq->d_bcount = 0;
+               ddq->d_icount = 0;
+               ddq->d_rtbcount = 0;
+               ddq->d_btimer = 0;
+               ddq->d_itimer = 0;
+               ddq->d_rtbtimer = 0;
+               ddq->d_bwarns = 0;
+               ddq->d_iwarns = 0;
+               ddq->d_rtbwarns = 0;
                ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
        }
 
-       return (0);
+       return 0;
 }
 
 STATIC int
@@ -1526,11 +1520,14 @@ xfs_qm_dqiter_bufs(
        int             error;
        int             notcommitted;
        int             incr;
+       int             type;
 
        ASSERT(blkcnt > 0);
        notcommitted = 0;
        incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
                XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
+       type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
+               (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
        error = 0;
 
        /*
@@ -1549,9 +1546,7 @@ xfs_qm_dqiter_bufs(
                if (error)
                        break;
 
-               (void) xfs_qm_reset_dqcounts(mp, bp, firstid,
-                                            flags & XFS_QMOPT_UQUOTA ?
-                                            XFS_DQ_USER : XFS_DQ_GROUP);
+               (void) xfs_qm_reset_dqcounts(mp, bp, firstid, type);
                xfs_bdwrite(mp, bp);
                /*
                 * goto the next block.
@@ -1559,11 +1554,11 @@ xfs_qm_dqiter_bufs(
                bno++;
                firstid += XFS_QM_DQPERBLK(mp);
        }
-       return (error);
+       return error;
 }
 
 /*
- * Iterate over all allocated USR/GRP dquots in the system, calling a
+ * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
  * caller supplied function for every chunk of dquots that we find.
  */
 STATIC int
@@ -1583,12 +1578,12 @@ xfs_qm_dqiterate(
 
        error = 0;
        /*
-        * This looks racey, but we can't keep an inode lock across a
+        * This looks racy, but we can't keep an inode lock across a
         * trans_reserve. But, this gets called during quotacheck, and that
         * happens only at mount time which is single threaded.
         */
        if (qip->i_d.di_nblocks == 0)
-               return (0);
+               return 0;
 
        map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
 
@@ -1606,7 +1601,7 @@ xfs_qm_dqiterate(
                                  maxlblkcnt - lblkno,
                                  XFS_BMAPI_METADATA,
                                  NULL,
-                                 0, map, &nmaps, NULL);
+                                 0, map, &nmaps, NULL, NULL);
                xfs_iunlock(qip, XFS_ILOCK_SHARED);
                if (error)
                        break;
@@ -1657,7 +1652,7 @@ xfs_qm_dqiterate(
 
        kmem_free(map, XFS_DQITER_MAP_SIZE * sizeof(*map));
 
-       return (error);
+       return error;
 }
 
 /*
@@ -1679,22 +1674,24 @@ xfs_qm_quotacheck_dqadjust(
         * Adjust the inode count and the block count to reflect this inode's
         * resource usage.
         */
-       INT_MOD(dqp->q_core.d_icount, ARCH_CONVERT, +1);
+       be64_add(&dqp->q_core.d_icount, 1);
        dqp->q_res_icount++;
        if (nblks) {
-               INT_MOD(dqp->q_core.d_bcount, ARCH_CONVERT, nblks);
+               be64_add(&dqp->q_core.d_bcount, nblks);
                dqp->q_res_bcount += nblks;
        }
        if (rtblks) {
-               INT_MOD(dqp->q_core.d_rtbcount, ARCH_CONVERT, rtblks);
+               be64_add(&dqp->q_core.d_rtbcount, rtblks);
                dqp->q_res_rtbcount += rtblks;
        }
 
        /*
-        * Adjust the timers since we just changed usages
+        * Set default limits, adjust timers (since we changed usages)
         */
-       if (! XFS_IS_SUSER_DQUOT(dqp))
+       if (! XFS_IS_SUSER_DQUOT(dqp)) {
+               xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
                xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
+       }
 
        dqp->dq_flags |= XFS_DQ_DIRTY;
 }
@@ -1705,9 +1702,9 @@ xfs_qm_get_rtblks(
        xfs_qcnt_t      *O_rtblks)
 {
        xfs_filblks_t   rtblks;                 /* total rt blks */
+       xfs_extnum_t    idx;                    /* extent record index */
        xfs_ifork_t     *ifp;                   /* inode fork pointer */
        xfs_extnum_t    nextents;               /* number of extent entries */
-       xfs_bmbt_rec_t  *base;                  /* base of extent array */
        xfs_bmbt_rec_t  *ep;                    /* pointer to an extent entry */
        int             error;
 
@@ -1715,15 +1712,16 @@ xfs_qm_get_rtblks(
        ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
        if (!(ifp->if_flags & XFS_IFEXTENTS)) {
                if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
-                       return (error);
+                       return error;
        }
        rtblks = 0;
-       nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
-       base = &ifp->if_u1.if_extents[0];
-       for (ep = base; ep < &base[nextents]; ep++)
+       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       for (idx = 0; idx < nextents; idx++) {
+               ep = xfs_iext_get_ext(ifp, idx);
                rtblks += xfs_bmbt_get_blockcount(ep);
+       }
        *O_rtblks = (xfs_qcnt_t)rtblks;
-       return (0);
+       return 0;
 }
 
 /*
@@ -1734,9 +1732,8 @@ xfs_qm_get_rtblks(
 STATIC int
 xfs_qm_dqusage_adjust(
        xfs_mount_t     *mp,            /* mount point for filesystem */
-       xfs_trans_t     *tp,            /* transaction pointer - NULL */
        xfs_ino_t       ino,            /* inode number to get data for */
-       void            *buffer,        /* not used */
+       void            __user *buffer, /* not used */
        int             ubsize,         /* not used */
        void            *private_data,  /* not used */
        xfs_daddr_t     bno,            /* starting block of inode cluster */
@@ -1766,9 +1763,9 @@ xfs_qm_dqusage_adjust(
         * the case in all other instances. It's OK that we do this because
         * quotacheck is done only at mount time.
         */
-       if ((error = xfs_iget(mp, tp, ino, XFS_ILOCK_EXCL, &ip, bno))) {
+       if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
                *res = BULKSTAT_RV_NOTHING;
-               return (error);
+               return error;
        }
 
        if (ip->i_d.di_mode == 0) {
@@ -1786,7 +1783,7 @@ xfs_qm_dqusage_adjust(
        if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
                xfs_iput(ip, XFS_ILOCK_EXCL);
                *res = BULKSTAT_RV_GIVEUP;
-               return (error);
+               return error;
        }
 
        rtblks = 0;
@@ -1803,7 +1800,7 @@ xfs_qm_dqusage_adjust(
                        if (gdqp)
                                xfs_qm_dqput(gdqp);
                        *res = BULKSTAT_RV_GIVEUP;
-                       return (error);
+                       return error;
                }
                nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
        }
@@ -1825,7 +1822,7 @@ xfs_qm_dqusage_adjust(
         * we have to start from the beginning anyway.
         * Once we're done, we'll log all the dquot bufs.
         *
-        * The *QUOTA_ON checks below may look pretty racey, but quotachecks
+        * The *QUOTA_ON checks below may look pretty racy, but quotachecks
         * and quotaoffs don't race. (Quotachecks happen at mount time only).
         */
        if (XFS_IS_UQUOTA_ON(mp)) {
@@ -1833,7 +1830,7 @@ xfs_qm_dqusage_adjust(
                xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
                xfs_qm_dqput(udqp);
        }
-       if (XFS_IS_GQUOTA_ON(mp)) {
+       if (XFS_IS_OQUOTA_ON(mp)) {
                ASSERT(gdqp);
                xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
                xfs_qm_dqput(gdqp);
@@ -1848,14 +1845,14 @@ xfs_qm_dqusage_adjust(
         * Goto next inode.
         */
        *res = BULKSTAT_RV_DIDONE;
-       return (0);
+       return 0;
 }
 
 /*
  * Walk thru all the filesystem inodes and construct a consistent view
- * of the disk quota world.
+ * of the disk quota world. If the quotacheck fails, disable quotas.
  */
-STATIC int
+int
 xfs_qm_quotacheck(
        xfs_mount_t     *mp)
 {
@@ -1882,7 +1879,7 @@ xfs_qm_quotacheck(
        cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
 
        /*
-        * First we go thru all the dquots on disk, USR and GRP, and reset
+        * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
         * their counters to zero. We need a clean slate.
         * We don't log our changes till later.
         */
@@ -1893,9 +1890,10 @@ xfs_qm_quotacheck(
        }
 
        if ((gip = XFS_QI_GQIP(mp))) {
-               if ((error = xfs_qm_dqiterate(mp, gip, XFS_QMOPT_GQUOTA)))
+               if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
+                                       XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA)))
                        goto error_return;
-               flags |= XFS_GQUOTA_CHKD;
+               flags |= XFS_OQUOTA_CHKD;
        }
 
        do {
@@ -1903,11 +1901,9 @@ xfs_qm_quotacheck(
                 * Iterate thru all the inodes in the file system,
                 * adjusting the corresponding dquot counters in core.
                 */
-               if ((error = xfs_bulkstat(mp, NULL, &lastino, &count,
+               if ((error = xfs_bulkstat(mp, &lastino, &count,
                                     xfs_qm_dqusage_adjust, NULL,
-                                    structsz, NULL,
-                                    BULKSTAT_FG_IGET|BULKSTAT_FG_VFSLOCKED,
-                                    &done)))
+                                    structsz, NULL, BULKSTAT_FG_IGET, &done)))
                        break;
 
        } while (! done);
@@ -1920,9 +1916,7 @@ xfs_qm_quotacheck(
         * at this point (because we intentionally didn't in dqget_noattach).
         */
        if (error) {
-               xfs_qm_dqpurge_all(mp,
-                                  XFS_QMOPT_UQUOTA|XFS_QMOPT_GQUOTA|
-                                  XFS_QMOPT_QUOTAOFF);
+               xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF);
                goto error_return;
        }
        /*
@@ -1945,13 +1939,26 @@ xfs_qm_quotacheck(
         * quotachecked status, since we won't be doing accounting for
         * that type anymore.
         */
-       mp->m_qflags &= ~(XFS_GQUOTA_CHKD | XFS_UQUOTA_CHKD);
+       mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD);
        mp->m_qflags |= flags;
 
        XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
 
  error_return:
-       cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
+       if (error) {
+               cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
+                       "Disabling quotas.",
+                       mp->m_fsname, error);
+               /*
+                * We must turn off quotas.
+                */
+               ASSERT(mp->m_quotainfo != NULL);
+               ASSERT(xfs_Gqm != NULL);
+               xfs_qm_destroy_quotainfo(mp);
+               (void)xfs_mount_reset_sbqflags(mp);
+       } else {
+               cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
+       }
        return (error);
 }
 
@@ -1981,14 +1988,14 @@ xfs_qm_init_quotainos(
                    mp->m_sb.sb_uquotino != NULLFSINO) {
                        ASSERT(mp->m_sb.sb_uquotino > 0);
                        if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
-                                            0, &uip, 0)))
+                                            0, 0, &uip, 0)))
                                return XFS_ERROR(error);
                }
-               if (XFS_IS_GQUOTA_ON(mp) &&
+               if (XFS_IS_OQUOTA_ON(mp) &&
                    mp->m_sb.sb_gquotino != NULLFSINO) {
                        ASSERT(mp->m_sb.sb_gquotino > 0);
                        if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
-                                            0, &gip, 0))) {
+                                            0, 0, &gip, 0))) {
                                if (uip)
                                        VN_RELE(XFS_ITOV(uip));
                                return XFS_ERROR(error);
@@ -2014,10 +2021,12 @@ xfs_qm_init_quotainos(
 
                flags &= ~XFS_QMOPT_SBVERSION;
        }
-       if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
-               if ((error = xfs_qm_qino_alloc(mp, &gip,
-                                             sbflags | XFS_SB_GQUOTINO,
-                                             flags | XFS_QMOPT_GQUOTA))) {
+       if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
+               flags |= (XFS_IS_GQUOTA_ON(mp) ?
+                               XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
+               error = xfs_qm_qino_alloc(mp, &gip,
+                                         sbflags | XFS_SB_GQUOTINO, flags);
+               if (error) {
                        if (uip)
                                VN_RELE(XFS_ITOV(uip));
 
@@ -2028,7 +2037,7 @@ xfs_qm_init_quotainos(
        XFS_QI_UQIP(mp) = uip;
        XFS_QI_GQIP(mp) = gip;
 
-       return (0);
+       return 0;
 }
 
 
@@ -2049,7 +2058,7 @@ xfs_qm_shake_freelist(
        int             nflushes;
 
        if (howmany <= 0)
-               return (0);
+               return 0;
 
        nreclaimed = 0;
        restarts = 0;
@@ -2075,7 +2084,7 @@ xfs_qm_shake_freelist(
                        xfs_dqunlock(dqp);
                        xfs_qm_freelist_unlock(xfs_Gqm);
                        if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
-                               return (nreclaimed);
+                               return nreclaimed;
                        XQM_STATS_INC(xqmstats.xs_qm_dqwants);
                        goto tryagain;
                }
@@ -2150,13 +2159,13 @@ xfs_qm_shake_freelist(
                        XFS_DQ_HASH_UNLOCK(hash);
                        xfs_qm_freelist_unlock(xfs_Gqm);
                        if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
-                               return (nreclaimed);
+                               return nreclaimed;
                        goto tryagain;
                }
                xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING");
 #ifdef QUOTADEBUG
                cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
-                       dqp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
+                       dqp, be32_to_cpu(dqp->q_core.d_id));
 #endif
                ASSERT(dqp->q_nrefs == 0);
                nextdqp = dqp->dq_flnext;
@@ -2175,7 +2184,7 @@ xfs_qm_shake_freelist(
                dqp = nextdqp;
        }
        xfs_qm_freelist_unlock(xfs_Gqm);
-       return (nreclaimed);
+       return nreclaimed;
 }
 
 
@@ -2184,14 +2193,14 @@ xfs_qm_shake_freelist(
  */
 /* ARGSUSED */
 STATIC int
-xfs_qm_shake(int nr_to_scan, unsigned int gfp_mask)
+xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask)
 {
        int     ndqused, nfree, n;
 
        if (!kmem_shake_allow(gfp_mask))
-               return (0);
+               return 0;
        if (!xfs_Gqm)
-               return (0);
+               return 0;
 
        nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
        /* incore dquots in all f/s's */
@@ -2200,7 +2209,7 @@ xfs_qm_shake(int nr_to_scan, unsigned int gfp_mask)
        ASSERT(ndqused >= 0);
 
        if (nfree <= ndqused && nfree < ndquot)
-               return (0);
+               return 0;
 
        ndqused *= xfs_Gqm->qm_dqfree_ratio;    /* target # of free dquots */
        n = nfree - ndqused - ndquot;           /* # over target */
@@ -2244,7 +2253,7 @@ xfs_qm_dqreclaim_one(void)
                        xfs_dqunlock(dqp);
                        xfs_qm_freelist_unlock(xfs_Gqm);
                        if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
-                               return (NULL);
+                               return NULL;
                        XQM_STATS_INC(xqmstats.xs_qm_dqwants);
                        goto startagain;
                }
@@ -2320,7 +2329,7 @@ xfs_qm_dqreclaim_one(void)
        }
 
        xfs_qm_freelist_unlock(xfs_Gqm);
-       return (dqpout);
+       return dqpout;
 }
 
 
@@ -2356,7 +2365,7 @@ xfs_qm_dqalloc_incore(
                         */
                        memset(&dqp->q_core, 0, sizeof(dqp->q_core));
                        *O_dqpp = dqp;
-                       return (B_FALSE);
+                       return B_FALSE;
                }
                XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
        }
@@ -2369,7 +2378,7 @@ xfs_qm_dqalloc_incore(
        *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
        atomic_inc(&xfs_Gqm->qm_totaldquots);
 
-       return (B_TRUE);
+       return B_TRUE;
 }
 
 
@@ -2394,13 +2403,13 @@ xfs_qm_write_sb_changes(
                                      0,
                                      XFS_DEFAULT_LOG_COUNT))) {
                xfs_trans_cancel(tp, 0);
-               return (error);
+               return error;
        }
 
        xfs_mod_sb(tp, flags);
        (void) xfs_trans_commit(tp, 0, NULL);
 
-       return (0);
+       return 0;
 }
 
 
@@ -2423,6 +2432,7 @@ xfs_qm_vop_dqalloc(
        xfs_inode_t     *ip,
        uid_t           uid,
        gid_t           gid,
+       prid_t          prid,
        uint            flags,
        xfs_dquot_t     **O_udqpp,
        xfs_dquot_t     **O_gdqpp)
@@ -2449,13 +2459,12 @@ xfs_qm_vop_dqalloc(
                if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
                                            XFS_QMOPT_ILOCKED))) {
                        xfs_iunlock(ip, lockflags);
-                       return (error);
+                       return error;
                }
        }
 
        uq = gq = NULL;
-       if ((flags & XFS_QMOPT_UQUOTA) &&
-           XFS_IS_UQUOTA_ON(mp)) {
+       if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
                if (ip->i_d.di_uid != uid) {
                        /*
                         * What we need is the dquot that has this uid, and
@@ -2473,7 +2482,7 @@ xfs_qm_vop_dqalloc(
                                                 XFS_QMOPT_DOWARN,
                                                 &uq))) {
                                ASSERT(error != ENOENT);
-                               return (error);
+                               return error;
                        }
                        /*
                         * Get the ilock in the right order.
@@ -2493,8 +2502,7 @@ xfs_qm_vop_dqalloc(
                        xfs_dqunlock(uq);
                }
        }
-       if ((flags & XFS_QMOPT_GQUOTA) &&
-           XFS_IS_GQUOTA_ON(mp)) {
+       if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
                if (ip->i_d.di_gid != gid) {
                        xfs_iunlock(ip, lockflags);
                        if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
@@ -2505,6 +2513,29 @@ xfs_qm_vop_dqalloc(
                                if (uq)
                                        xfs_qm_dqrele(uq);
                                ASSERT(error != ENOENT);
+                               return error;
+                       }
+                       xfs_dqunlock(gq);
+                       lockflags = XFS_ILOCK_SHARED;
+                       xfs_ilock(ip, lockflags);
+               } else {
+                       ASSERT(ip->i_gdquot);
+                       gq = ip->i_gdquot;
+                       xfs_dqlock(gq);
+                       XFS_DQHOLD(gq);
+                       xfs_dqunlock(gq);
+               }
+       } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
+               if (ip->i_d.di_projid != prid) {
+                       xfs_iunlock(ip, lockflags);
+                       if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
+                                                XFS_DQ_PROJ,
+                                                XFS_QMOPT_DQALLOC |
+                                                XFS_QMOPT_DOWARN,
+                                                &gq))) {
+                               if (uq)
+                                       xfs_qm_dqrele(uq);
+                               ASSERT(error != ENOENT);
                                return (error);
                        }
                        xfs_dqunlock(gq);
@@ -2530,7 +2561,7 @@ xfs_qm_vop_dqalloc(
                *O_gdqpp = gq;
        else if (gq)
                xfs_qm_dqrele(gq);
-       return (0);
+       return 0;
 }
 
 /*
@@ -2545,6 +2576,9 @@ xfs_qm_vop_chown(
        xfs_dquot_t     *newdq)
 {
        xfs_dquot_t     *prevdq;
+       uint            bfield = XFS_IS_REALTIME_INODE(ip) ?
+                                XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
+
        ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
        ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
 
@@ -2553,20 +2587,12 @@ xfs_qm_vop_chown(
        ASSERT(prevdq);
        ASSERT(prevdq != newdq);
 
-       xfs_trans_mod_dquot(tp, prevdq,
-                           XFS_TRANS_DQ_BCOUNT,
-                           -(ip->i_d.di_nblocks));
-       xfs_trans_mod_dquot(tp, prevdq,
-                           XFS_TRANS_DQ_ICOUNT,
-                           -1);
+       xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
+       xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
 
        /* the sparkling new dquot */
-       xfs_trans_mod_dquot(tp, newdq,
-                           XFS_TRANS_DQ_BCOUNT,
-                           ip->i_d.di_nblocks);
-       xfs_trans_mod_dquot(tp, newdq,
-                           XFS_TRANS_DQ_ICOUNT,
-                           1);
+       xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
+       xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
 
        /*
         * Take an extra reference, because the inode
@@ -2578,11 +2604,11 @@ xfs_qm_vop_chown(
        xfs_dqunlock(newdq);
        *IO_olddq = newdq;
 
-       return (prevdq);
+       return prevdq;
 }
 
 /*
- * Quota reservations for setattr(AT_UID|AT_GID).
+ * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
  */
 int
 xfs_qm_vop_chown_reserve(
@@ -2594,7 +2620,7 @@ xfs_qm_vop_chown_reserve(
 {
        int             error;
        xfs_mount_t     *mp;
-       uint            delblks;
+       uint            delblks, blkflags, prjflags = 0;
        xfs_dquot_t     *unresudq, *unresgdq, *delblksudq, *delblksgdq;
 
        ASSERT(XFS_ISLOCKED_INODE(ip));
@@ -2603,9 +2629,11 @@ xfs_qm_vop_chown_reserve(
 
        delblks = ip->i_delayed_blks;
        delblksudq = delblksgdq = unresudq = unresgdq = NULL;
+       blkflags = XFS_IS_REALTIME_INODE(ip) ?
+                       XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
 
        if (XFS_IS_UQUOTA_ON(mp) && udqp &&
-           ip->i_d.di_uid != (uid_t)INT_GET(udqp->q_core.d_id, ARCH_CONVERT)) {
+           ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
                delblksudq = udqp;
                /*
                 * If there are delayed allocation blocks, then we have to
@@ -2617,18 +2645,25 @@ xfs_qm_vop_chown_reserve(
                        unresudq = ip->i_udquot;
                }
        }
-       if (XFS_IS_GQUOTA_ON(ip->i_mount) && gdqp &&
-           ip->i_d.di_gid != INT_GET(gdqp->q_core.d_id, ARCH_CONVERT)) {
-               delblksgdq = gdqp;
-               if (delblks) {
-                       ASSERT(ip->i_gdquot);
-                       unresgdq = ip->i_gdquot;
+       if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
+               if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
+                    ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id))
+                       prjflags = XFS_QMOPT_ENOSPC;
+
+               if (prjflags ||
+                   (XFS_IS_GQUOTA_ON(ip->i_mount) &&
+                    ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
+                       delblksgdq = gdqp;
+                       if (delblks) {
+                               ASSERT(ip->i_gdquot);
+                               unresgdq = ip->i_gdquot;
+                       }
                }
        }
 
        if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
                                delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
-                               flags | XFS_QMOPT_RES_REGBLKS)))
+                               flags | blkflags | prjflags)))
                return (error);
 
        /*
@@ -2645,11 +2680,11 @@ xfs_qm_vop_chown_reserve(
                ASSERT(unresudq || unresgdq);
                if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
                                delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
-                               flags | XFS_QMOPT_RES_REGBLKS)))
+                               flags | blkflags | prjflags)))
                        return (error);
                xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
                                unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
-                               XFS_QMOPT_RES_REGBLKS);
+                               blkflags);
        }
 
        return (0);
@@ -2666,12 +2701,12 @@ xfs_qm_vop_rename_dqattach(
        ip = i_tab[0];
 
        if (! XFS_IS_QUOTA_ON(ip->i_mount))
-               return (0);
+               return 0;
 
        if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
                error = xfs_qm_dqattach(ip, 0);
                if (error)
-                       return (error);
+                       return error;
        }
        for (i = 1; (i < 4 && i_tab[i]); i++) {
                /*
@@ -2681,11 +2716,11 @@ xfs_qm_vop_rename_dqattach(
                        if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
                                error = xfs_qm_dqattach(ip, 0);
                                if (error)
-                                       return (error);
+                                       return error;
                        }
                }
        }
-       return (0);
+       return 0;
 }
 
 void
@@ -2707,7 +2742,8 @@ xfs_qm_vop_dqattach_and_dqmod_newinode(
                xfs_dqunlock(udqp);
                ASSERT(ip->i_udquot == NULL);
                ip->i_udquot = udqp;
-               ASSERT(ip->i_d.di_uid == INT_GET(udqp->q_core.d_id, ARCH_CONVERT));
+               ASSERT(XFS_IS_UQUOTA_ON(tp->t_mountp));
+               ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
                xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
        }
        if (gdqp) {
@@ -2716,27 +2752,30 @@ xfs_qm_vop_dqattach_and_dqmod_newinode(
                xfs_dqunlock(gdqp);
                ASSERT(ip->i_gdquot == NULL);
                ip->i_gdquot = gdqp;
-               ASSERT(ip->i_d.di_gid == INT_GET(gdqp->q_core.d_id, ARCH_CONVERT));
+               ASSERT(XFS_IS_OQUOTA_ON(tp->t_mountp));
+               ASSERT((XFS_IS_GQUOTA_ON(tp->t_mountp) ?
+                       ip->i_d.di_gid : ip->i_d.di_projid) ==
+                               be32_to_cpu(gdqp->q_core.d_id));
                xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
        }
 }
 
 /* ------------- list stuff -----------------*/
-void
+STATIC void
 xfs_qm_freelist_init(xfs_frlist_t *ql)
 {
        ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
-       mutex_init(&ql->qh_lock, MUTEX_DEFAULT, "dqf");
+       mutex_init(&ql->qh_lock);
        ql->qh_version = 0;
        ql->qh_nelems = 0;
 }
 
-void
+STATIC void
 xfs_qm_freelist_destroy(xfs_frlist_t *ql)
 {
        xfs_dquot_t     *dqp, *nextdqp;
 
-       mutex_lock(&ql->qh_lock, PINOD);
+       mutex_lock(&ql->qh_lock);
        for (dqp = ql->qh_next;
             dqp != (xfs_dquot_t *)ql; ) {
                xfs_dqlock(dqp);
@@ -2749,15 +2788,13 @@ xfs_qm_freelist_destroy(xfs_frlist_t *ql)
                xfs_qm_dqdestroy(dqp);
                dqp = nextdqp;
        }
-       /*
-        * Don't bother about unlocking.
-        */
+       mutex_unlock(&ql->qh_lock);
        mutex_destroy(&ql->qh_lock);
 
        ASSERT(ql->qh_nelems == 0);
 }
 
-void
+STATIC void
 xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
 {
        dq->dq_flnext = ql->qh_next;
@@ -2787,14 +2824,14 @@ xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
        xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
 }
 
-int
+STATIC int
 xfs_qm_dqhashlock_nowait(
        xfs_dquot_t *dqp)
 {
        int locked;
 
        locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
-       return (locked);
+       return locked;
 }
 
 int
@@ -2804,10 +2841,10 @@ xfs_qm_freelist_lock_nowait(
        int locked;
 
        locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
-       return (locked);
+       return locked;
 }
 
-int
+STATIC int
 xfs_qm_mplist_nowait(
        xfs_mount_t     *mp)
 {
@@ -2815,5 +2852,5 @@ xfs_qm_mplist_nowait(
 
        ASSERT(mp->m_quotainfo);
        locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
-       return (locked);
+       return locked;
 }