Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / fs / xfs / quota / xfs_qm.c
index efde16e..7a2beb6 100644 (file)
@@ -1,73 +1,52 @@
 /*
- * Copyright (c) 2000-2005 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_clnt.h"
+#include "xfs_inum.h"
 #include "xfs_trans.h"
 #include "xfs_sb.h"
 #include "xfs_ag.h"
-#include "xfs_dir.h"
 #include "xfs_dir2.h"
 #include "xfs_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"
-#include "xfs_mac.h"
 #include "xfs_attr.h"
 #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;
-STATIC kmem_shaker_t   xfs_qm_shaker;
+
+static cred_t  xfs_zerocr;
 
 STATIC void    xfs_qm_list_init(xfs_dqlist_t *, char *, int);
 STATIC void    xfs_qm_list_destroy(xfs_dqlist_t *);
@@ -93,7 +74,12 @@ STATIC int   xfs_qm_dqhashlock_nowait(xfs_dquot_t *);
 
 STATIC int     xfs_qm_init_quotainos(xfs_mount_t *);
 STATIC int     xfs_qm_init_quotainfo(xfs_mount_t *);
-STATIC int     xfs_qm_shake(int, unsigned int);
+STATIC int     xfs_qm_shake(int, gfp_t);
+
+static struct shrinker xfs_qm_shaker = {
+       .shrink = xfs_qm_shake,
+       .seeks = DEFAULT_SEEKS,
+};
 
 #ifdef DEBUG
 extern mutex_t qcheck_lock;
@@ -107,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
@@ -124,25 +110,26 @@ 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;
+       size_t          hsize;
+       uint            i;
 
        /*
         * 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;
+       udqhash = kmem_zalloc_greedy(&hsize,
+                                    XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
+                                    XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t),
+                                    KM_SLEEP | KM_MAYFAIL | KM_LARGE);
+       gdqhash = kmem_zalloc(hsize, KM_SLEEP | KM_LARGE);
+       hsize /= sizeof(xfs_dqhash_t);
+       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);
 
@@ -166,7 +153,7 @@ xfs_Gqm_init(void)
        } else
                xqm->qm_dqzone = qm_dqzone;
 
-       xfs_qm_shaker = kmem_shake_register(xfs_qm_shake);
+       register_shrinker(&xfs_qm_shaker);
 
        /*
         * The t_dqinfo portion of transactions.
@@ -182,7 +169,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;
 }
@@ -198,14 +185,14 @@ xfs_qm_destroy(
 
        ASSERT(xqm != NULL);
        ASSERT(xqm->qm_nrefs == 0);
-       kmem_shake_deregister(xfs_qm_shaker);
+       unregister_shrinker(&xfs_qm_shaker);
        hsize = xqm->qm_dqhashmask + 1;
        for (i = 0; i < hsize; i++) {
                xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
                xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
        }
-       kmem_free(xqm->qm_usr_dqhtable, hsize * sizeof(xfs_dqhash_t));
-       kmem_free(xqm->qm_grp_dqhtable, hsize * sizeof(xfs_dqhash_t));
+       kmem_free(xqm->qm_usr_dqhtable);
+       kmem_free(xqm->qm_grp_dqhtable);
        xqm->qm_usr_dqhtable = NULL;
        xqm->qm_grp_dqhtable = NULL;
        xqm->qm_dqhashmask = 0;
@@ -213,7 +200,7 @@ xfs_qm_destroy(
 #ifdef DEBUG
        mutex_destroy(&qcheck_lock);
 #endif
-       kmem_free(xqm, sizeof(xfs_qm_t));
+       kmem_free(xqm);
 }
 
 /*
@@ -300,45 +287,6 @@ 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
- * so already. Note that the superblock has not been read in yet.
- */
-void
-xfs_qm_mount_quotainit(
-       xfs_mount_t     *mp,
-       uint            flags)
-{
-       /*
-        * User, projects or group quotas has to be on.
-        */
-       ASSERT(flags & (XFSMNT_UQUOTA | XFSMNT_PQUOTA | XFSMNT_GQUOTA));
-
-       /*
-        * Initialize the flags in the mount structure. From this point
-        * onwards we look at m_qflags to figure out if quotas's ON/OFF, etc.
-        * Note that we enforce nothing if accounting is off.
-        * ie.  XFSMNT_*QUOTA must be ON for XFSMNT_*QUOTAENF.
-        * It isn't necessary to take the quotaoff lock to do this; this is
-        * called from mount.
-        */
-       if (flags & XFSMNT_UQUOTA) {
-               mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
-               if (flags & XFSMNT_UQUOTAENF)
-                       mp->m_qflags |= XFS_UQUOTA_ENFD;
-       }
-       if (flags & XFSMNT_GQUOTA) {
-               mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
-               if (flags & XFSMNT_GQUOTAENF)
-                       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;
-       }
-}
-
-/*
  * Just destroy the quotainfo structure.
  */
 void
@@ -355,17 +303,17 @@ xfs_qm_unmount_quotadestroy(
  * necessary data structures like quotainfo.  This is also responsible for
  * running a quotacheck as necessary.  We are guaranteed that the superblock
  * is consistently read in at this point.
+ *
+ * If we fail here, the mount will continue with quota turned off. We don't
+ * need to inidicate success or failure at all.
  */
-int
+void
 xfs_qm_mount_quotas(
-       xfs_mount_t     *mp,
-       int             mfsi_flags)
+       xfs_mount_t     *mp)
 {
-       unsigned long   s;
        int             error = 0;
        uint            sbf;
 
-
        /*
         * If quotas on realtime volumes is not supported, we disable
         * quotas immediately.
@@ -384,7 +332,8 @@ xfs_qm_mount_quotas(
         * Allocate the quotainfo structure inside the mount struct, and
         * create quotainode(s), and change/rev superblock if necessary.
         */
-       if ((error = xfs_qm_init_quotainfo(mp))) {
+       error = xfs_qm_init_quotainfo(mp);
+       if (error) {
                /*
                 * We must turn off quotas.
                 */
@@ -395,25 +344,32 @@ xfs_qm_mount_quotas(
        /*
         * If any of the quotas are not consistent, do a quotacheck.
         */
-       if (XFS_QM_NEED_QUOTACHECK(mp) &&
-               !(mfsi_flags & XFS_MFSI_NO_QUOTACHECK)) {
-               if ((error = xfs_qm_quotacheck(mp))) {
-                       /* Quotacheck has failed and quotas have
-                        * been disabled.
-                        */
-                       return XFS_ERROR(error);
+       if (XFS_QM_NEED_QUOTACHECK(mp)) {
+               error = xfs_qm_quotacheck(mp);
+               if (error) {
+                       /* Quotacheck failed and disabled quotas. */
+                       return;
                }
        }
+       /* 
+        * If one type of quotas is off, then it will lose its
+        * quotachecked status, since we won't be doing accounting for
+        * that type anymore.
+        */
+       if (!XFS_IS_UQUOTA_ON(mp))
+               mp->m_qflags &= ~XFS_UQUOTA_CHKD;
+       if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
+               mp->m_qflags &= ~XFS_OQUOTA_CHKD;
 
  write_changes:
        /*
-        * We actually don't have to acquire the SB_LOCK at all.
+        * We actually don't have to acquire the m_sb_lock at all.
         * This can only be called from mount, and that's single threaded. XXX
         */
-       s = XFS_SB_LOCK(mp);
+       spin_lock(&mp->m_sb_lock);
        sbf = mp->m_sb.sb_qflags;
        mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
-       XFS_SB_UNLOCK(mp, s);
+       spin_unlock(&mp->m_sb_lock);
 
        if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
                if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
@@ -433,19 +389,16 @@ xfs_qm_mount_quotas(
                xfs_fs_cmn_err(CE_WARN, mp,
                        "Failed to initialize disk quotas.");
        }
-       return XFS_ERROR(error);
+       return;
 }
 
 /*
  * Called from the vfsops layer.
  */
-int
+void
 xfs_qm_unmount_quotas(
        xfs_mount_t     *mp)
 {
-       xfs_inode_t     *uqp, *gqp;
-       int             error = 0;
-
        /*
         * Release the dquots that root inode, et al might be holding,
         * before we flush quotas and blow away the quotainfo structure.
@@ -458,43 +411,18 @@ xfs_qm_unmount_quotas(
                xfs_qm_dqdetach(mp->m_rsumip);
 
        /*
-        * Flush out the quota inodes.
+        * Release the quota inodes.
         */
-       uqp = gqp = NULL;
        if (mp->m_quotainfo) {
-               if ((uqp = mp->m_quotainfo->qi_uquotaip) != NULL) {
-                       xfs_ilock(uqp, XFS_ILOCK_EXCL);
-                       xfs_iflock(uqp);
-                       error = xfs_iflush(uqp, XFS_IFLUSH_SYNC);
-                       xfs_iunlock(uqp, XFS_ILOCK_EXCL);
-                       if (unlikely(error == EFSCORRUPTED)) {
-                               XFS_ERROR_REPORT("xfs_qm_unmount_quotas(1)",
-                                                XFS_ERRLEVEL_LOW, mp);
-                               goto out;
-                       }
+               if (mp->m_quotainfo->qi_uquotaip) {
+                       IRELE(mp->m_quotainfo->qi_uquotaip);
+                       mp->m_quotainfo->qi_uquotaip = NULL;
                }
-               if ((gqp = mp->m_quotainfo->qi_gquotaip) != NULL) {
-                       xfs_ilock(gqp, XFS_ILOCK_EXCL);
-                       xfs_iflock(gqp);
-                       error = xfs_iflush(gqp, XFS_IFLUSH_SYNC);
-                       xfs_iunlock(gqp, XFS_ILOCK_EXCL);
-                       if (unlikely(error == EFSCORRUPTED)) {
-                               XFS_ERROR_REPORT("xfs_qm_unmount_quotas(2)",
-                                                XFS_ERRLEVEL_LOW, mp);
-                               goto out;
-                       }
+               if (mp->m_quotainfo->qi_gquotaip) {
+                       IRELE(mp->m_quotainfo->qi_gquotaip);
+                       mp->m_quotainfo->qi_gquotaip = NULL;
                }
        }
-       if (uqp) {
-                XFS_PURGE_INODE(uqp);
-                mp->m_quotainfo->qi_uquotaip = NULL;
-       }
-       if (gqp) {
-               XFS_PURGE_INODE(gqp);
-               mp->m_quotainfo->qi_gquotaip = NULL;
-       }
-out:
-       return XFS_ERROR(error);
 }
 
 /*
@@ -512,7 +440,7 @@ xfs_qm_dqflush_all(
        int             error;
 
        if (mp->m_quotainfo == NULL)
-               return (0);
+               return 0;
        niters = 0;
 again:
        xfs_qm_mplist_lock(mp);
@@ -525,7 +453,7 @@ again:
                xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY");
                /* XXX a sentinel would be better */
                recl = XFS_QI_MPLRECLAIMS(mp);
-               if (! xfs_qm_dqflock_nowait(dqp)) {
+               if (!xfs_dqflock_nowait(dqp)) {
                        /*
                         * If we can't grab the flush lock then check
                         * to see if the dquot has been flushed delayed
@@ -543,7 +471,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)) {
@@ -555,7 +483,7 @@ again:
 
        xfs_qm_mplist_unlock(mp);
        /* return ! busy */
-       return (0);
+       return 0;
 }
 /*
  * Release the group dquot pointers the user dquots may be
@@ -614,7 +542,7 @@ 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;
@@ -672,7 +600,7 @@ xfs_qm_dqpurge_int(
                 * freelist in INACTIVE state.
                 */
                nextdqp = dqp->MPL_NEXT;
-               nmisses += xfs_qm_dqpurge(dqp, flags);
+               nmisses += xfs_qm_dqpurge(dqp);
                dqp = nextdqp;
        }
        xfs_qm_mplist_unlock(mp);
@@ -711,7 +639,7 @@ xfs_qm_dqattach_one(
        xfs_dquot_t     *dqp;
        int             error;
 
-       ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
        error = 0;
        /*
         * See if we already have it in the inode itself. IO_idqpp is
@@ -743,7 +671,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);
@@ -811,7 +739,7 @@ xfs_qm_dqattach_one(
                        ASSERT(XFS_DQ_IS_LOCKED(dqp));
        }
 #endif
-       return (error);
+       return error;
 }
 
 
@@ -819,7 +747,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.
  */
@@ -912,10 +840,10 @@ 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));
+              xfs_isilocked(ip, XFS_ILOCK_EXCL));
 
        if (! (flags & XFS_QMOPT_ILOCKED))
                xfs_ilock(ip, XFS_ILOCK_EXCL);
@@ -929,7 +857,8 @@ xfs_qm_dqattach(
                        goto done;
                nquotas++;
        }
-       ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
        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,
@@ -954,7 +883,7 @@ xfs_qm_dqattach(
         * This WON'T, in general, result in a thrash.
         */
        if (nquotas == 2) {
-               ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+               ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
                ASSERT(ip->i_udquot);
                ASSERT(ip->i_gdquot);
 
@@ -997,9 +926,9 @@ xfs_qm_dqattach(
 
 #ifdef QUOTADEBUG
        else
-               ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+               ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
 #endif
-       return (error);
+       return error;
 }
 
 /*
@@ -1029,18 +958,14 @@ xfs_qm_dqdetach(
 }
 
 /*
- * This is called by VFS_SYNC and flags arg determines the caller,
- * and its motives, as done in xfs_sync.
- *
- * vfs_sync: SYNC_FSDATA|SYNC_ATTR|SYNC_BDFLUSH 0x31
- * syscall sync: SYNC_FSDATA|SYNC_ATTR|SYNC_DELWRI 0x25
- * umountroot : SYNC_WAIT | SYNC_CLOSE | SYNC_ATTR | SYNC_FSDATA
+ * This is called to sync quotas. We can be told to use non-blocking
+ * semantics by either the SYNC_BDFLUSH flag or the absence of the
+ * SYNC_WAIT flag.
  */
-
 int
 xfs_qm_sync(
        xfs_mount_t     *mp,
-       short           flags)
+       int             flags)
 {
        int             recl, restarts;
        xfs_dquot_t     *dqp;
@@ -1048,6 +973,9 @@ xfs_qm_sync(
        boolean_t       nowait;
        int             error;
 
+       if (! XFS_IS_QUOTA_ON(mp))
+               return 0;
+
        restarts = 0;
        /*
         * We won't block unless we are asked to.
@@ -1064,7 +992,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) {
                /*
@@ -1099,17 +1027,17 @@ xfs_qm_sync(
 
                /* XXX a sentinel would be better */
                recl = XFS_QI_MPLRECLAIMS(mp);
-               if (! xfs_qm_dqflock_nowait(dqp)) {
+               if (!xfs_dqflock_nowait(dqp)) {
                        if (nowait) {
                                xfs_dqunlock(dqp);
                                continue;
                        }
                        /*
                         * 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);
@@ -1124,9 +1052,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)) {
@@ -1139,9 +1067,16 @@ xfs_qm_sync(
        }
 
        xfs_qm_mplist_unlock(mp);
-       return (0);
+       return 0;
 }
 
+/*
+ * The hash chains and the mplist use the same xfs_dqhash structure as
+ * their list head, but we can take the mplist qh_lock and one of the
+ * hash qh_locks at the same time without any problem as they aren't
+ * related.
+ */
+static struct lock_class_key xfs_quota_mplist_class;
 
 /*
  * This initializes all the quota information that's kept in the
@@ -1161,7 +1096,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);
@@ -1171,17 +1106,18 @@ xfs_qm_init_quotainfo(
         * and change the superblock accordingly.
         */
        if ((error = xfs_qm_init_quotainos(mp))) {
-               kmem_free(qinf, sizeof(xfs_quotainfo_t));
+               kmem_free(qinf);
                mp->m_quotainfo = NULL;
-               return (error);
+               return error;
        }
 
-       spinlock_init(&qinf->qi_pinlock, "xfs_qinf_pin");
        xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
+       lockdep_set_class(&qinf->qi_dqlist.qh_lock, &xfs_quota_mplist_class);
+
        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);
@@ -1213,42 +1149,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_rtbwarnlimit =
-                               INT_GET(ddqp->d_rtbwarns, ARCH_CONVERT) ?
-                               INT_GET(ddqp->d_rtbwarns, ARCH_CONVERT) :
-                               XFS_QM_RTBWARNLIMIT;
-               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
@@ -1265,7 +1183,7 @@ xfs_qm_init_quotainfo(
                qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
        }
 
-       return (0);
+       return 0;
 }
 
 
@@ -1291,19 +1209,18 @@ xfs_qm_destroy_quotainfo(
         */
        xfs_qm_rele_quotafs_ref(mp);
 
-       spinlock_destroy(&qi->qi_pinlock);
        xfs_qm_list_destroy(&qi->qi_dqlist);
 
        if (qi->qi_uquotaip) {
-               XFS_PURGE_INODE(qi->qi_uquotaip);
+               IRELE(qi->qi_uquotaip);
                qi->qi_uquotaip = NULL; /* paranoia */
        }
        if (qi->qi_gquotaip) {
-               XFS_PURGE_INODE(qi->qi_gquotaip);
+               IRELE(qi->qi_gquotaip);
                qi->qi_gquotaip = NULL;
        }
        mutex_destroy(&qi->qi_quotaofflock);
-       kmem_free(qi, sizeof(xfs_quotainfo_t));
+       kmem_free(qi);
        mp->m_quotainfo = NULL;
 }
 
@@ -1318,7 +1235,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;
@@ -1347,7 +1264,7 @@ xfs_qm_dqget_noattach(
        xfs_mount_t     *mp;
        xfs_dquot_t     *udqp, *gdqp;
 
-       ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
        mp = ip->i_mount;
        udqp = NULL;
        gdqp = NULL;
@@ -1365,7 +1282,7 @@ xfs_qm_dqget_noattach(
                         */
                        ASSERT(error != ESRCH);
                        ASSERT(error != ENOENT);
-                       return (error);
+                       return error;
                }
                ASSERT(udqp);
        }
@@ -1388,7 +1305,7 @@ xfs_qm_dqget_noattach(
                                xfs_qm_dqrele(udqp);
                        ASSERT(error != ESRCH);
                        ASSERT(error != ENOENT);
-                       return (error);
+                       return error;
                }
                ASSERT(gdqp);
 
@@ -1409,7 +1326,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;
 }
 
 /*
@@ -1425,52 +1342,49 @@ xfs_qm_qino_alloc(
 {
        xfs_trans_t     *tp;
        int             error;
-       unsigned long s;
-       cred_t          zerocr;
        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, NULL, 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;
        }
 
        /*
         * Keep an extra reference to this quota inode. This inode is
         * locked exclusively and joined to the transaction already.
         */
-       ASSERT(XFS_ISLOCKED_INODE_EXCL(*ip));
-       VN_HOLD(XFS_ITOV((*ip)));
+       ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL));
+       IHOLD(*ip);
 
        /*
         * Make the changes in the superblock, and log those too.
         * sbfields arg may contain fields other than *QUOTINO;
         * VERSIONNUM for example.
         */
-       s = XFS_SB_LOCK(mp);
+       spin_lock(&mp->m_sb_lock);
        if (flags & XFS_QMOPT_SBVERSION) {
 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
                unsigned oldv = mp->m_sb.sb_versionnum;
 #endif
-               ASSERT(!XFS_SB_VERSION_HASQUOTA(&mp->m_sb));
+               ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
                ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
                                   XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
                       (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
                        XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
 
-               XFS_SB_VERSION_ADDQUOTA(&mp->m_sb);
+               xfs_sb_version_addquota(&mp->m_sb);
                mp->m_sb.sb_uquotino = NULLFSINO;
                mp->m_sb.sb_gquotino = NULLFSINO;
 
@@ -1486,19 +1400,18 @@ xfs_qm_qino_alloc(
                mp->m_sb.sb_uquotino = (*ip)->i_ino;
        else
                mp->m_sb.sb_gquotino = (*ip)->i_ino;
-       XFS_SB_UNLOCK(mp, s);
+       spin_unlock(&mp->m_sb_lock);
        xfs_mod_sb(tp, sbfields);
 
-       if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
-                                    NULL))) {
+       if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
                xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
-               return (error);
+               return error;
        }
-       return (0);
+       return 0;
 }
 
 
-STATIC int
+STATIC void
 xfs_qm_reset_dqcounts(
        xfs_mount_t     *mp,
        xfs_buf_t       *bp,
@@ -1523,23 +1436,21 @@ 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_rtbtimer, ARCH_CONVERT, (time_t)0);
-               INT_SET(ddq->d_bwarns, ARCH_CONVERT, 0UL);
-               INT_SET(ddq->d_iwarns, ARCH_CONVERT, 0UL);
-               INT_SET(ddq->d_rtbwarns, 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);
 }
 
 STATIC int
@@ -1580,7 +1491,7 @@ xfs_qm_dqiter_bufs(
                if (error)
                        break;
 
-               (void) xfs_qm_reset_dqcounts(mp, bp, firstid, type);
+               xfs_qm_reset_dqcounts(mp, bp, firstid, type);
                xfs_bdwrite(mp, bp);
                /*
                 * goto the next block.
@@ -1588,7 +1499,7 @@ xfs_qm_dqiter_bufs(
                bno++;
                firstid += XFS_QM_DQPERBLK(mp);
        }
-       return (error);
+       return error;
 }
 
 /*
@@ -1612,12 +1523,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);
 
@@ -1635,7 +1546,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;
@@ -1684,9 +1595,9 @@ xfs_qm_dqiterate(
                        break;
        } while (nmaps > 0);
 
-       kmem_free(map, XFS_DQITER_MAP_SIZE * sizeof(*map));
+       kmem_free(map);
 
-       return (error);
+       return error;
 }
 
 /*
@@ -1708,14 +1619,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_cpu(&dqp->q_core.d_icount, 1);
        dqp->q_res_icount++;
        if (nblks) {
-               INT_MOD(dqp->q_core.d_bcount, ARCH_CONVERT, nblks);
+               be64_add_cpu(&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_cpu(&dqp->q_core.d_rtbcount, rtblks);
                dqp->q_res_rtbcount += rtblks;
        }
 
@@ -1736,25 +1647,23 @@ 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;
 
        ASSERT(XFS_IS_REALTIME_INODE(ip));
        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++)
-               rtblks += xfs_bmbt_get_blockcount(ep);
+       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
+       for (idx = 0; idx < nextents; idx++)
+               rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
        *O_rtblks = (xfs_qcnt_t)rtblks;
-       return (0);
+       return 0;
 }
 
 /*
@@ -1798,13 +1707,7 @@ xfs_qm_dqusage_adjust(
         */
        if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
                *res = BULKSTAT_RV_NOTHING;
-               return (error);
-       }
-
-       if (ip->i_d.di_mode == 0) {
-               xfs_iput_new(ip, XFS_ILOCK_EXCL);
-               *res = BULKSTAT_RV_NOTHING;
-               return XFS_ERROR(ENOENT);
+               return error;
        }
 
        /*
@@ -1816,7 +1719,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;
@@ -1833,7 +1736,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;
        }
@@ -1855,7 +1758,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)) {
@@ -1872,13 +1775,13 @@ xfs_qm_dqusage_adjust(
         * Now release the inode. This will send it to 'inactive', and
         * possibly even free blocks.
         */
-       VN_RELE(XFS_ITOV(ip));
+       IRELE(ip);
 
        /*
         * Goto next inode.
         */
        *res = BULKSTAT_RV_DIDONE;
-       return (0);
+       return 0;
 }
 
 /*
@@ -1936,14 +1839,20 @@ xfs_qm_quotacheck(
                 */
                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);
 
        /*
+        * We've made all the changes that we need to make incore.
+        * Flush them down to disk buffers if everything was updated
+        * successfully.
+        */
+       if (!error)
+               error = xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
+
+       /*
         * We can get this error if we couldn't do a dquot allocation inside
         * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
         * dirty dquots that might be cached, we just want to get rid of them
@@ -1951,16 +1860,9 @@ 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_PQUOTA|XFS_QMOPT_QUOTAOFF);
+               xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF);
                goto error_return;
        }
-       /*
-        * We've made all the changes that we need to make incore.
-        * Now flush_them down to disk buffers.
-        */
-       xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
 
        /*
         * We didn't log anything, because if we crashed, we'll have to
@@ -1992,7 +1894,10 @@ xfs_qm_quotacheck(
                ASSERT(mp->m_quotainfo != NULL);
                ASSERT(xfs_Gqm != NULL);
                xfs_qm_destroy_quotainfo(mp);
-               (void)xfs_mount_reset_sbqflags(mp);
+               if (xfs_mount_reset_sbqflags(mp)) {
+                       cmn_err(CE_WARN, "XFS quotacheck %s: "
+                               "Failed to reset quota flags.", mp->m_fsname);
+               }
        } else {
                cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
        }
@@ -2020,7 +1925,7 @@ xfs_qm_init_quotainos(
        /*
         * Get the uquota and gquota inodes
         */
-       if (XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
+       if (xfs_sb_version_hasquota(&mp->m_sb)) {
                if (XFS_IS_UQUOTA_ON(mp) &&
                    mp->m_sb.sb_uquotino != NULLFSINO) {
                        ASSERT(mp->m_sb.sb_uquotino > 0);
@@ -2034,7 +1939,7 @@ xfs_qm_init_quotainos(
                        if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
                                             0, 0, &gip, 0))) {
                                if (uip)
-                                       VN_RELE(XFS_ITOV(uip));
+                                       IRELE(uip);
                                return XFS_ERROR(error);
                        }
                }
@@ -2065,7 +1970,7 @@ xfs_qm_init_quotainos(
                                          sbflags | XFS_SB_GQUOTINO, flags);
                if (error) {
                        if (uip)
-                               VN_RELE(XFS_ITOV(uip));
+                               IRELE(uip);
 
                        return XFS_ERROR(error);
                }
@@ -2074,7 +1979,7 @@ xfs_qm_init_quotainos(
        XFS_QI_UQIP(mp) = uip;
        XFS_QI_GQIP(mp) = gip;
 
-       return (0);
+       return 0;
 }
 
 
@@ -2095,7 +2000,7 @@ xfs_qm_shake_freelist(
        int             nflushes;
 
        if (howmany <= 0)
-               return (0);
+               return 0;
 
        nreclaimed = 0;
        restarts = 0;
@@ -2121,7 +2026,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;
                }
@@ -2146,7 +2051,7 @@ xfs_qm_shake_freelist(
                 * Try to grab the flush lock. If this dquot is in the process of
                 * getting flushed to disk, we don't want to reclaim it.
                 */
-               if (! xfs_qm_dqflock_nowait(dqp)) {
+               if (!xfs_dqflock_nowait(dqp)) {
                        xfs_dqunlock(dqp);
                        dqp = dqp->dq_flnext;
                        continue;
@@ -2159,12 +2064,17 @@ xfs_qm_shake_freelist(
                 * dirty dquots.
                 */
                if (XFS_DQ_IS_DIRTY(dqp)) {
+                       int     error;
                        xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY");
                        /*
                         * We flush it delayed write, so don't bother
                         * releasing the mplock.
                         */
-                       (void) xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
+                       error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
+                       if (error) {
+                               xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
+                       "xfs_qm_dqflush_all: dquot %p flush failed", dqp);
+                       }
                        xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
                        dqp = dqp->dq_flnext;
                        continue;
@@ -2196,13 +2106,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;
@@ -2221,7 +2131,7 @@ xfs_qm_shake_freelist(
                dqp = nextdqp;
        }
        xfs_qm_freelist_unlock(xfs_Gqm);
-       return (nreclaimed);
+       return nreclaimed;
 }
 
 
@@ -2230,14 +2140,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 */
@@ -2246,7 +2156,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 */
@@ -2290,7 +2200,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;
                }
@@ -2319,7 +2229,7 @@ xfs_qm_dqreclaim_one(void)
                 * Try to grab the flush lock. If this dquot is in the process of
                 * getting flushed to disk, we don't want to reclaim it.
                 */
-               if (! xfs_qm_dqflock_nowait(dqp)) {
+               if (!xfs_dqflock_nowait(dqp)) {
                        xfs_dqunlock(dqp);
                        continue;
                }
@@ -2331,12 +2241,17 @@ xfs_qm_dqreclaim_one(void)
                 * dirty dquots.
                 */
                if (XFS_DQ_IS_DIRTY(dqp)) {
+                       int     error;
                        xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY");
                        /*
                         * We flush it delayed write, so don't bother
                         * releasing the freelist lock.
                         */
-                       (void) xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
+                       error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
+                       if (error) {
+                               xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
+                       "xfs_qm_dqreclaim: dquot %p flush failed", dqp);
+                       }
                        xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
                        continue;
                }
@@ -2366,7 +2281,7 @@ xfs_qm_dqreclaim_one(void)
        }
 
        xfs_qm_freelist_unlock(xfs_Gqm);
-       return (dqpout);
+       return dqpout;
 }
 
 
@@ -2402,7 +2317,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);
        }
@@ -2415,7 +2330,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;
 }
 
 
@@ -2440,13 +2355,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);
+       error = xfs_trans_commit(tp, 0);
 
-       return (0);
+       return error;
 }
 
 
@@ -2484,8 +2399,7 @@ xfs_qm_vop_dqalloc(
        lockflags = XFS_ILOCK_EXCL;
        xfs_ilock(ip, lockflags);
 
-       if ((flags & XFS_QMOPT_INHERIT) &&
-           XFS_INHERIT_GID(ip, XFS_MTOVFS(mp)))
+       if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
                gid = ip->i_d.di_gid;
 
        /*
@@ -2496,7 +2410,7 @@ xfs_qm_vop_dqalloc(
                if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
                                            XFS_QMOPT_ILOCKED))) {
                        xfs_iunlock(ip, lockflags);
-                       return (error);
+                       return error;
                }
        }
 
@@ -2519,7 +2433,7 @@ xfs_qm_vop_dqalloc(
                                                 XFS_QMOPT_DOWARN,
                                                 &uq))) {
                                ASSERT(error != ENOENT);
-                               return (error);
+                               return error;
                        }
                        /*
                         * Get the ilock in the right order.
@@ -2550,7 +2464,7 @@ xfs_qm_vop_dqalloc(
                                if (uq)
                                        xfs_qm_dqrele(uq);
                                ASSERT(error != ENOENT);
-                               return (error);
+                               return error;
                        }
                        xfs_dqunlock(gq);
                        lockflags = XFS_ILOCK_SHARED;
@@ -2598,7 +2512,7 @@ xfs_qm_vop_dqalloc(
                *O_gdqpp = gq;
        else if (gq)
                xfs_qm_dqrele(gq);
-       return (0);
+       return 0;
 }
 
 /*
@@ -2616,7 +2530,7 @@ xfs_qm_vop_chown(
        uint            bfield = XFS_IS_REALTIME_INODE(ip) ?
                                 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
 
-       ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
        ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
 
        /* old dquot */
@@ -2641,7 +2555,7 @@ xfs_qm_vop_chown(
        xfs_dqunlock(newdq);
        *IO_olddq = newdq;
 
-       return (prevdq);
+       return prevdq;
 }
 
 /*
@@ -2657,10 +2571,10 @@ xfs_qm_vop_chown_reserve(
 {
        int             error;
        xfs_mount_t     *mp;
-       uint            delblks, blkflags;
+       uint            delblks, blkflags, prjflags = 0;
        xfs_dquot_t     *unresudq, *unresgdq, *delblksudq, *delblksgdq;
 
-       ASSERT(XFS_ISLOCKED_INODE(ip));
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
        mp = ip->i_mount;
        ASSERT(XFS_IS_QUOTA_RUNNING(mp));
 
@@ -2670,7 +2584,7 @@ xfs_qm_vop_chown_reserve(
                        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
@@ -2683,10 +2597,13 @@ xfs_qm_vop_chown_reserve(
                }
        }
        if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
-               if ((XFS_IS_GQUOTA_ON(ip->i_mount) && ip->i_d.di_gid !=
-                               INT_GET(gdqp->q_core.d_id, ARCH_CONVERT)) ||
-                   (XFS_IS_PQUOTA_ON(ip->i_mount) && ip->i_d.di_projid !=
-                               INT_GET(gdqp->q_core.d_id, ARCH_CONVERT))) {
+               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);
@@ -2697,7 +2614,7 @@ xfs_qm_vop_chown_reserve(
 
        if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
                                delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
-                               flags | blkflags)))
+                               flags | blkflags | prjflags)))
                return (error);
 
        /*
@@ -2714,7 +2631,7 @@ 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 | blkflags)))
+                               flags | blkflags | prjflags)))
                        return (error);
                xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
                                unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
@@ -2735,12 +2652,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++) {
                /*
@@ -2750,11 +2667,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
@@ -2767,7 +2684,7 @@ xfs_qm_vop_dqattach_and_dqmod_newinode(
        if (!XFS_IS_QUOTA_ON(tp->t_mountp))
                return;
 
-       ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
        ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
 
        if (udqp) {
@@ -2776,7 +2693,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) {
@@ -2785,7 +2703,10 @@ 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);
        }
 }
@@ -2795,7 +2716,7 @@ 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;
 }
@@ -2805,7 +2726,7 @@ 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);
@@ -2818,9 +2739,7 @@ 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);
@@ -2863,7 +2782,7 @@ xfs_qm_dqhashlock_nowait(
        int locked;
 
        locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
-       return (locked);
+       return locked;
 }
 
 int
@@ -2873,7 +2792,7 @@ xfs_qm_freelist_lock_nowait(
        int locked;
 
        locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
-       return (locked);
+       return locked;
 }
 
 STATIC int
@@ -2884,5 +2803,5 @@ xfs_qm_mplist_nowait(
 
        ASSERT(mp->m_quotainfo);
        locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
-       return (locked);
+       return locked;
 }