Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / xfs / quota / xfs_qm.c
index 89f2cd6..7fb5eca 100644 (file)
@@ -1,39 +1,25 @@
 /*
- * 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_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_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 +52,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 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;
@@ -101,10 +95,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
@@ -118,25 +112,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);
 
@@ -176,7 +170,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;
 }
@@ -184,7 +178,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)
 {
@@ -295,7 +289,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
@@ -304,9 +298,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
@@ -324,7 +318,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;
        }
 }
 
@@ -355,16 +353,6 @@ xfs_qm_mount_quotas(
        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
@@ -378,11 +366,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.
@@ -400,19 +385,14 @@ xfs_qm_mount_quotas(
         */
        if (XFS_QM_NEED_QUOTACHECK(mp) &&
                !(mfsi_flags & XFS_MFSI_NO_QUOTACHECK)) {
-#ifdef DEBUG
-               cmn_err(CE_NOTE, "Doing a quotacheck. Please wait.");
-#endif
                if ((error = xfs_qm_quotacheck(mp))) {
                        /* Quotacheck has failed and quotas have
                         * been disabled.
                         */
                        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.
@@ -509,7 +489,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)
@@ -520,7 +500,7 @@ xfs_qm_dqflush_all(
        int             error;
 
        if (mp->m_quotainfo == NULL)
-               return (0);
+               return 0;
        niters = 0;
 again:
        xfs_qm_mplist_lock(mp);
@@ -551,7 +531,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)) {
@@ -563,7 +543,7 @@ again:
 
        xfs_qm_mplist_unlock(mp);
        /* return ! busy */
-       return (0);
+       return 0;
 }
 /*
  * Release the group dquot pointers the user dquots may be
@@ -613,7 +593,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;
@@ -622,9 +602,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);
@@ -734,11 +715,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);
 
@@ -750,7 +731,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);
@@ -818,7 +799,7 @@ xfs_qm_dqattach_one(
                        ASSERT(XFS_DQ_IS_LOCKED(dqp));
        }
 #endif
-       return (error);
+       return error;
 }
 
 
@@ -826,7 +807,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.
  */
@@ -897,8 +878,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.
@@ -919,7 +900,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));
@@ -937,8 +918,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);
@@ -989,7 +975,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
@@ -1001,7 +987,7 @@ xfs_qm_dqattach(
        else
                ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
 #endif
-       return (error);
+       return error;
 }
 
 /*
@@ -1018,13 +1004,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;
        }
@@ -1066,7 +1052,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) {
                /*
@@ -1108,10 +1094,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);
@@ -1126,9 +1112,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)) {
@@ -1141,7 +1127,7 @@ xfs_qm_sync(
        }
 
        xfs_qm_mplist_unlock(mp);
-       return (0);
+       return 0;
 }
 
 
@@ -1149,7 +1135,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)
 {
@@ -1163,7 +1149,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);
@@ -1175,7 +1161,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");
@@ -1183,7 +1169,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);
@@ -1202,8 +1188,9 @@ 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) {
@@ -1214,38 +1201,24 @@ xfs_qm_init_quotainfo(
                 * 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(ddqp->d_btimer, ARCH_CONVERT) ?
-                               INT_GET(ddqp->d_btimer, ARCH_CONVERT) :
-                               XFS_QM_BTIMELIMIT;
-               qinf->qi_itimelimit =
-                               INT_GET(ddqp->d_itimer, ARCH_CONVERT) ?
-                               INT_GET(ddqp->d_itimer, ARCH_CONVERT) :
-                               XFS_QM_ITIMELIMIT;
-               qinf->qi_rtbtimelimit =
-                               INT_GET(ddqp->d_rtbtimer, ARCH_CONVERT) ?
-                               INT_GET(ddqp->d_rtbtimer, ARCH_CONVERT) :
-                               XFS_QM_RTBTIMELIMIT;
-               qinf->qi_bwarnlimit =
-                               INT_GET(ddqp->d_bwarns, ARCH_CONVERT) ?
-                               INT_GET(ddqp->d_bwarns, ARCH_CONVERT) :
-                               XFS_QM_BWARNLIMIT;
-               qinf->qi_iwarnlimit =
-                               INT_GET(ddqp->d_iwarns, ARCH_CONVERT) ?
-                               INT_GET(ddqp->d_iwarns, ARCH_CONVERT) :
-                               XFS_QM_IWARNLIMIT;
-               qinf->qi_bhardlimit =
-                               INT_GET(ddqp->d_blk_hardlimit, ARCH_CONVERT);
-               qinf->qi_bsoftlimit =
-                               INT_GET(ddqp->d_blk_softlimit, ARCH_CONVERT);
-               qinf->qi_ihardlimit =
-                               INT_GET(ddqp->d_ino_hardlimit, ARCH_CONVERT);
-               qinf->qi_isoftlimit =
-                               INT_GET(ddqp->d_ino_softlimit, ARCH_CONVERT);
-               qinf->qi_rtbhardlimit =
-                               INT_GET(ddqp->d_rtb_hardlimit, ARCH_CONVERT);
-               qinf->qi_rtbsoftlimit =
-                               INT_GET(ddqp->d_rtb_softlimit, ARCH_CONVERT);
+               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
@@ -1259,9 +1232,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;
 }
 
 
@@ -1314,7 +1288,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;
@@ -1361,23 +1335,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);
 
@@ -1398,7 +1379,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;
 }
 
 /*
@@ -1414,26 +1395,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;
        }
 
        /*
@@ -1481,9 +1460,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;
 }
 
 
@@ -1512,21 +1491,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
@@ -1541,11 +1522,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;
 
        /*
@@ -1564,9 +1548,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.
@@ -1574,11 +1556,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
@@ -1598,12 +1580,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);
 
@@ -1672,7 +1654,7 @@ xfs_qm_dqiterate(
 
        kmem_free(map, XFS_DQITER_MAP_SIZE * sizeof(*map));
 
-       return (error);
+       return error;
 }
 
 /*
@@ -1694,14 +1676,14 @@ 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;
        }
 
@@ -1722,9 +1704,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;
 
@@ -1732,15 +1714,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;
 }
 
 /*
@@ -1784,7 +1767,7 @@ xfs_qm_dqusage_adjust(
         */
        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) {
@@ -1802,7 +1785,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;
@@ -1819,7 +1802,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;
        }
@@ -1841,7 +1824,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)) {
@@ -1849,7 +1832,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);
@@ -1864,7 +1847,7 @@ xfs_qm_dqusage_adjust(
         * Goto next inode.
         */
        *res = BULKSTAT_RV_DIDONE;
-       return (0);
+       return 0;
 }
 
 /*
@@ -1898,7 +1881,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.
         */
@@ -1909,9 +1892,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 {
@@ -1936,9 +1920,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;
        }
        /*
@@ -1961,7 +1943,7 @@ 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 +++");
@@ -1977,7 +1959,7 @@ xfs_qm_quotacheck(
                ASSERT(mp->m_quotainfo != NULL);
                ASSERT(xfs_Gqm != NULL);
                xfs_qm_destroy_quotainfo(mp);
-               xfs_mount_reset_sbqflags(mp);
+               (void)xfs_mount_reset_sbqflags(mp);
        } else {
                cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
        }
@@ -2013,7 +1995,7 @@ xfs_qm_init_quotainos(
                                             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,
@@ -2043,10 +2025,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));
 
@@ -2057,7 +2041,7 @@ xfs_qm_init_quotainos(
        XFS_QI_UQIP(mp) = uip;
        XFS_QI_GQIP(mp) = gip;
 
-       return (0);
+       return 0;
 }
 
 
@@ -2078,7 +2062,7 @@ xfs_qm_shake_freelist(
        int             nflushes;
 
        if (howmany <= 0)
-               return (0);
+               return 0;
 
        nreclaimed = 0;
        restarts = 0;
@@ -2104,7 +2088,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;
                }
@@ -2179,13 +2163,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;
@@ -2204,7 +2188,7 @@ xfs_qm_shake_freelist(
                dqp = nextdqp;
        }
        xfs_qm_freelist_unlock(xfs_Gqm);
-       return (nreclaimed);
+       return nreclaimed;
 }
 
 
@@ -2213,14 +2197,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 */
@@ -2229,7 +2213,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 */
@@ -2273,7 +2257,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;
                }
@@ -2349,7 +2333,7 @@ xfs_qm_dqreclaim_one(void)
        }
 
        xfs_qm_freelist_unlock(xfs_Gqm);
-       return (dqpout);
+       return dqpout;
 }
 
 
@@ -2385,7 +2369,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);
        }
@@ -2398,7 +2382,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;
 }
 
 
@@ -2423,13 +2407,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;
 }
 
 
@@ -2452,6 +2436,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)
@@ -2478,13 +2463,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
@@ -2502,7 +2486,7 @@ xfs_qm_vop_dqalloc(
                                                 XFS_QMOPT_DOWARN,
                                                 &uq))) {
                                ASSERT(error != ENOENT);
-                               return (error);
+                               return error;
                        }
                        /*
                         * Get the ilock in the right order.
@@ -2522,8 +2506,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,
@@ -2534,6 +2517,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);
@@ -2559,7 +2565,7 @@ xfs_qm_vop_dqalloc(
                *O_gdqpp = gq;
        else if (gq)
                xfs_qm_dqrele(gq);
-       return (0);
+       return 0;
 }
 
 /*
@@ -2574,6 +2580,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));
 
@@ -2582,20 +2591,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
@@ -2607,11 +2608,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(
@@ -2623,7 +2624,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));
@@ -2632,9 +2633,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
@@ -2646,18 +2649,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);
 
        /*
@@ -2674,11 +2684,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);
@@ -2695,12 +2705,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++) {
                /*
@@ -2710,11 +2720,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
@@ -2736,7 +2746,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) {
@@ -2745,27 +2756,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);
@@ -2778,15 +2792,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;
@@ -2816,14 +2828,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
@@ -2833,10 +2845,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)
 {
@@ -2844,5 +2856,5 @@ xfs_qm_mplist_nowait(
 
        ASSERT(mp->m_quotainfo);
        locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
-       return (locked);
+       return locked;
 }