LCOV - code coverage report
Current view: top level - fs/xfs - xfs_qm.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 817 909 89.9 %
Date: 2023-07-31 20:08:34 Functions: 35 35 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_sb.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_inode.h"
      16             : #include "xfs_iwalk.h"
      17             : #include "xfs_quota.h"
      18             : #include "xfs_bmap.h"
      19             : #include "xfs_bmap_util.h"
      20             : #include "xfs_trans.h"
      21             : #include "xfs_trans_space.h"
      22             : #include "xfs_qm.h"
      23             : #include "xfs_trace.h"
      24             : #include "xfs_icache.h"
      25             : #include "xfs_error.h"
      26             : #include "xfs_ag.h"
      27             : #include "xfs_ialloc.h"
      28             : #include "xfs_log_priv.h"
      29             : #include "xfs_health.h"
      30             : #include "xfs_imeta.h"
      31             : #include "xfs_imeta_utils.h"
      32             : #include "xfs_da_format.h"
      33             : #include "xfs_rtalloc.h"
      34             : 
      35             : /*
      36             :  * The global quota manager. There is only one of these for the entire
      37             :  * system, _not_ one per file system. XQM keeps track of the overall
      38             :  * quota functionality, including maintaining the freelist and hash
      39             :  * tables of dquots.
      40             :  */
      41             : STATIC int      xfs_qm_init_quotainos(struct xfs_mount *mp);
      42             : STATIC int      xfs_qm_init_quotainfo(struct xfs_mount *mp);
      43             : 
      44             : STATIC void     xfs_qm_destroy_quotainos(struct xfs_quotainfo *qi);
      45             : STATIC void     xfs_qm_dqfree_one(struct xfs_dquot *dqp);
      46             : /*
      47             :  * We use the batch lookup interface to iterate over the dquots as it
      48             :  * currently is the only interface into the radix tree code that allows
      49             :  * fuzzy lookups instead of exact matches.  Holding the lock over multiple
      50             :  * operations is fine as all callers are used either during mount/umount
      51             :  * or quotaoff.
      52             :  */
      53             : #define XFS_DQ_LOOKUP_BATCH     32
      54             : 
      55             : STATIC int
      56      213080 : xfs_qm_dquot_walk(
      57             :         struct xfs_mount        *mp,
      58             :         xfs_dqtype_t            type,
      59             :         int                     (*execute)(struct xfs_dquot *dqp, void *data),
      60             :         void                    *data)
      61             : {
      62      213080 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
      63      213080 :         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
      64      213080 :         uint32_t                next_index;
      65      213080 :         int                     last_error = 0;
      66      213080 :         int                     skipped;
      67      213080 :         int                     nr_found;
      68             : 
      69      213080 : restart:
      70      213080 :         skipped = 0;
      71      213080 :         next_index = 0;
      72      213080 :         nr_found = 0;
      73             : 
      74     1120577 :         while (1) {
      75     1333657 :                 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
      76     1333657 :                 int             error;
      77     1333657 :                 int             i;
      78             : 
      79     1333657 :                 mutex_lock(&qi->qi_tree_lock);
      80     1333657 :                 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
      81             :                                         next_index, XFS_DQ_LOOKUP_BATCH);
      82     1333657 :                 if (!nr_found) {
      83      213024 :                         mutex_unlock(&qi->qi_tree_lock);
      84      213024 :                         break;
      85             :                 }
      86             : 
      87    32245989 :                 for (i = 0; i < nr_found; i++) {
      88    31125356 :                         struct xfs_dquot *dqp = batch[i];
      89             : 
      90    31125356 :                         next_index = dqp->q_id + 1;
      91             : 
      92    31125356 :                         error = execute(batch[i], data);
      93    31125356 :                         if (error == -EAGAIN) {
      94           0 :                                 skipped++;
      95           0 :                                 continue;
      96             :                         }
      97    31125356 :                         if (error && last_error != -EFSCORRUPTED)
      98           0 :                                 last_error = error;
      99             :                 }
     100             : 
     101     1120633 :                 mutex_unlock(&qi->qi_tree_lock);
     102             : 
     103             :                 /* bail out if the filesystem is corrupted.  */
     104     1120633 :                 if (last_error == -EFSCORRUPTED) {
     105             :                         skipped = 0;
     106             :                         break;
     107             :                 }
     108             :                 /* we're done if id overflows back to zero */
     109     1120633 :                 if (!next_index)
     110             :                         break;
     111             :         }
     112             : 
     113      213080 :         if (skipped) {
     114           0 :                 delay(1);
     115           0 :                 goto restart;
     116             :         }
     117             : 
     118      213080 :         return last_error;
     119             : }
     120             : 
     121             : 
     122             : /*
     123             :  * Purge a dquot from all tracking data structures and free it.
     124             :  */
     125             : STATIC int
     126    31061087 : xfs_qm_dqpurge(
     127             :         struct xfs_dquot        *dqp,
     128             :         void                    *data)
     129             : {
     130    31061087 :         struct xfs_quotainfo    *qi = dqp->q_mount->m_quotainfo;
     131    31061087 :         int                     error = -EAGAIN;
     132             : 
     133    31061087 :         xfs_dqlock(dqp);
     134    31061087 :         if ((dqp->q_flags & XFS_DQFLAG_FREEING) || dqp->q_nrefs != 0)
     135           0 :                 goto out_unlock;
     136             : 
     137    31061087 :         dqp->q_flags |= XFS_DQFLAG_FREEING;
     138             : 
     139    31061087 :         xfs_dqflock(dqp);
     140             : 
     141             :         /*
     142             :          * If we are turning this type of quotas off, we don't care
     143             :          * about the dirty metadata sitting in this dquot. OTOH, if
     144             :          * we're unmounting, we do care, so we flush it and wait.
     145             :          */
     146    31061087 :         if (XFS_DQ_IS_DIRTY(dqp)) {
     147       46302 :                 struct xfs_buf  *bp = NULL;
     148             : 
     149             :                 /*
     150             :                  * We don't care about getting disk errors here. We need
     151             :                  * to purge this dquot anyway, so we go ahead regardless.
     152             :                  */
     153       46302 :                 error = xfs_qm_dqflush(dqp, &bp);
     154       46302 :                 if (!error) {
     155           0 :                         error = xfs_bwrite(bp);
     156           0 :                         xfs_buf_relse(bp);
     157       46302 :                 } else if (error == -EAGAIN) {
     158           0 :                         dqp->q_flags &= ~XFS_DQFLAG_FREEING;
     159           0 :                         goto out_unlock;
     160             :                 }
     161       46302 :                 xfs_dqflock(dqp);
     162             :         }
     163             : 
     164    31061087 :         ASSERT(atomic_read(&dqp->q_pincount) == 0);
     165    62122174 :         ASSERT(xlog_is_shutdown(dqp->q_logitem.qli_item.li_log) ||
     166             :                 !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags));
     167             : 
     168    31061087 :         xfs_dqfunlock(dqp);
     169    31061087 :         xfs_dqunlock(dqp);
     170             : 
     171    31061087 :         radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
     172    31061087 :         qi->qi_dquots--;
     173             : 
     174             :         /*
     175             :          * We move dquots to the freelist as soon as their reference count
     176             :          * hits zero, so it really should be on the freelist here.
     177             :          */
     178    31061087 :         ASSERT(!list_empty(&dqp->q_lru));
     179    31061087 :         list_lru_del(&qi->qi_lru, &dqp->q_lru);
     180    31061087 :         XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
     181             : 
     182    31061087 :         xfs_qm_dqdestroy(dqp);
     183    31061087 :         return 0;
     184             : 
     185           0 : out_unlock:
     186           0 :         xfs_dqunlock(dqp);
     187           0 :         return error;
     188             : }
     189             : 
     190             : /*
     191             :  * Purge the dquot cache.
     192             :  */
     193             : static void
     194       57562 : xfs_qm_dqpurge_all(
     195             :         struct xfs_mount        *mp)
     196             : {
     197       57562 :         xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_dqpurge, NULL);
     198       57562 :         xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_dqpurge, NULL);
     199       57562 :         xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_dqpurge, NULL);
     200       57562 : }
     201             : 
     202             : /*
     203             :  * Just destroy the quotainfo structure.
     204             :  */
     205             : void
     206       66800 : xfs_qm_unmount(
     207             :         struct xfs_mount        *mp)
     208             : {
     209       66800 :         if (mp->m_quotainfo) {
     210       57562 :                 xfs_qm_dqpurge_all(mp);
     211       57562 :                 xfs_qm_destroy_quotainfo(mp);
     212             :         }
     213       66800 : }
     214             : 
     215             : /*
     216             :  * Called from the vfsops layer.
     217             :  */
     218             : void
     219       66752 : xfs_qm_unmount_quotas(
     220             :         xfs_mount_t     *mp)
     221             : {
     222             :         /*
     223             :          * Release the dquots that root inode, et al might be holding,
     224             :          * before we flush quotas and blow away the quotainfo structure.
     225             :          */
     226       66752 :         ASSERT(mp->m_rootip);
     227       66752 :         xfs_qm_dqdetach(mp->m_rootip);
     228       66752 :         if (mp->m_rbmip)
     229       66752 :                 xfs_qm_dqdetach(mp->m_rbmip);
     230       66752 :         if (mp->m_rsumip)
     231       66752 :                 xfs_qm_dqdetach(mp->m_rsumip);
     232             : 
     233             :         /*
     234             :          * Release the quota inodes.
     235             :          */
     236       66752 :         if (mp->m_quotainfo) {
     237       57552 :                 if (mp->m_quotainfo->qi_uquotaip) {
     238       56656 :                         xfs_imeta_irele(mp->m_quotainfo->qi_uquotaip);
     239       56656 :                         mp->m_quotainfo->qi_uquotaip = NULL;
     240             :                 }
     241       57552 :                 if (mp->m_quotainfo->qi_gquotaip) {
     242       56010 :                         xfs_imeta_irele(mp->m_quotainfo->qi_gquotaip);
     243       56010 :                         mp->m_quotainfo->qi_gquotaip = NULL;
     244             :                 }
     245       57552 :                 if (mp->m_quotainfo->qi_pquotaip) {
     246       55801 :                         xfs_imeta_irele(mp->m_quotainfo->qi_pquotaip);
     247       55801 :                         mp->m_quotainfo->qi_pquotaip = NULL;
     248             :                 }
     249             :         }
     250       66752 : }
     251             : 
     252             : STATIC int
     253    58922572 : xfs_qm_dqattach_one(
     254             :         struct xfs_inode        *ip,
     255             :         xfs_dqtype_t            type,
     256             :         bool                    doalloc,
     257             :         struct xfs_dquot        **IO_idqpp)
     258             : {
     259    58922572 :         struct xfs_dquot        *dqp;
     260    58922572 :         int                     error;
     261             : 
     262    58922572 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     263    58921739 :         error = 0;
     264             : 
     265             :         /*
     266             :          * See if we already have it in the inode itself. IO_idqpp is &i_udquot
     267             :          * or &i_gdquot. This made the code look weird, but made the logic a lot
     268             :          * simpler.
     269             :          */
     270    58921739 :         dqp = *IO_idqpp;
     271    58921739 :         if (dqp) {
     272           0 :                 trace_xfs_dqattach_found(dqp);
     273           0 :                 return 0;
     274             :         }
     275             : 
     276             :         /*
     277             :          * Find the dquot from somewhere. This bumps the reference count of
     278             :          * dquot and returns it locked.  This can return ENOENT if dquot didn't
     279             :          * exist on disk and we didn't ask it to allocate; ESRCH if quotas got
     280             :          * turned off suddenly.
     281             :          */
     282    58921739 :         error = xfs_qm_dqget_inode(ip, type, doalloc, &dqp);
     283    58923661 :         if (error)
     284             :                 return error;
     285             : 
     286    58920150 :         trace_xfs_dqattach_get(dqp);
     287             : 
     288             :         /*
     289             :          * dqget may have dropped and re-acquired the ilock, but it guarantees
     290             :          * that the dquot returned is the one that should go in the inode.
     291             :          */
     292    58920147 :         *IO_idqpp = dqp;
     293    58920147 :         xfs_dqunlock(dqp);
     294    58920147 :         return 0;
     295             : }
     296             : 
     297             : static bool
     298  2639345672 : xfs_qm_need_dqattach(
     299             :         struct xfs_inode        *ip)
     300             : {
     301  2639345672 :         struct xfs_mount        *mp = ip->i_mount;
     302             : 
     303  2639345672 :         if (!XFS_IS_QUOTA_ON(mp))
     304             :                 return false;
     305  2514837168 :         if (!XFS_NOT_DQATTACHED(mp, ip))
     306             :                 return false;
     307    77590534 :         if (xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
     308             :                 return false;
     309    38795272 :         if (xfs_is_metadir_inode(ip))
     310      174506 :                 return false;
     311             :         return true;
     312             : }
     313             : 
     314             : /*
     315             :  * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
     316             :  * into account.
     317             :  * If @doalloc is true, the dquot(s) will be allocated if needed.
     318             :  * Inode may get unlocked and relocked in here, and the caller must deal with
     319             :  * the consequences.
     320             :  */
     321             : int
     322  1245075885 : xfs_qm_dqattach_locked(
     323             :         xfs_inode_t     *ip,
     324             :         bool            doalloc)
     325             : {
     326  1245075885 :         xfs_mount_t     *mp = ip->i_mount;
     327  1245075885 :         int             error = 0;
     328             : 
     329  1245075885 :         if (!xfs_qm_need_dqattach(ip))
     330             :                 return 0;
     331             : 
     332    19644666 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     333    19644630 :         ASSERT(!xfs_is_metadir_inode(ip));
     334             : 
     335    19644630 :         if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) {
     336    19642593 :                 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_USER,
     337             :                                 doalloc, &ip->i_udquot);
     338    19643972 :                 if (error)
     339        2552 :                         goto done;
     340    19641420 :                 ASSERT(ip->i_udquot);
     341             :         }
     342             : 
     343    19643457 :         if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) {
     344    19640481 :                 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_GROUP,
     345             :                                 doalloc, &ip->i_gdquot);
     346    19640483 :                 if (error)
     347         665 :                         goto done;
     348    19639818 :                 ASSERT(ip->i_gdquot);
     349             :         }
     350             : 
     351    19642794 :         if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) {
     352    19639233 :                 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_PROJ,
     353             :                                 doalloc, &ip->i_pdquot);
     354    19639233 :                 if (error)
     355         293 :                         goto done;
     356    19638940 :                 ASSERT(ip->i_pdquot);
     357             :         }
     358             : 
     359    19642501 : done:
     360             :         /*
     361             :          * Don't worry about the dquots that we may have attached before any
     362             :          * error - they'll get detached later if it has not already been done.
     363             :          */
     364    19646011 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     365             :         return error;
     366             : }
     367             : 
     368             : int
     369  1394722985 : xfs_qm_dqattach(
     370             :         struct xfs_inode        *ip)
     371             : {
     372  1394722985 :         int                     error;
     373             : 
     374  1394722985 :         if (!xfs_qm_need_dqattach(ip))
     375             :                 return 0;
     376             : 
     377    18976016 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     378    18976193 :         error = xfs_qm_dqattach_locked(ip, false);
     379    18977194 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     380             : 
     381    18977194 :         return error;
     382             : }
     383             : 
     384             : /*
     385             :  * Release dquots (and their references) if any.
     386             :  * The inode should be locked EXCL except when this's called by
     387             :  * xfs_ireclaim.
     388             :  */
     389             : void
     390   879869265 : xfs_qm_dqdetach(
     391             :         xfs_inode_t     *ip)
     392             : {
     393   879869265 :         if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot))
     394             :                 return;
     395             : 
     396   137779894 :         trace_xfs_dquot_dqdetach(ip);
     397             : 
     398   275490100 :         ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, ip->i_ino));
     399   137745050 :         if (ip->i_udquot) {
     400   137738928 :                 xfs_qm_dqrele(ip->i_udquot);
     401   137737902 :                 ip->i_udquot = NULL;
     402             :         }
     403   137744024 :         if (ip->i_gdquot) {
     404   137146432 :                 xfs_qm_dqrele(ip->i_gdquot);
     405   137132289 :                 ip->i_gdquot = NULL;
     406             :         }
     407   137729881 :         if (ip->i_pdquot) {
     408   137113270 :                 xfs_qm_dqrele(ip->i_pdquot);
     409   137122223 :                 ip->i_pdquot = NULL;
     410             :         }
     411             : }
     412             : 
     413             : struct xfs_qm_isolate {
     414             :         struct list_head        buffers;
     415             :         struct list_head        dispose;
     416             : };
     417             : 
     418             : static enum lru_status
     419     1261941 : xfs_qm_dquot_isolate(
     420             :         struct list_head        *item,
     421             :         struct list_lru_one     *lru,
     422             :         spinlock_t              *lru_lock,
     423             :         void                    *arg)
     424             :                 __releases(lru_lock) __acquires(lru_lock)
     425             : {
     426     1261941 :         struct xfs_dquot        *dqp = container_of(item,
     427             :                                                 struct xfs_dquot, q_lru);
     428     1261941 :         struct xfs_qm_isolate   *isol = arg;
     429             : 
     430     1261941 :         if (!xfs_dqlock_nowait(dqp))
     431          46 :                 goto out_miss_busy;
     432             : 
     433             :         /*
     434             :          * If something else is freeing this dquot and hasn't yet removed it
     435             :          * from the LRU, leave it for the freeing task to complete the freeing
     436             :          * process rather than risk it being free from under us here.
     437             :          */
     438     1261895 :         if (dqp->q_flags & XFS_DQFLAG_FREEING)
     439           0 :                 goto out_miss_unlock;
     440             : 
     441             :         /*
     442             :          * This dquot has acquired a reference in the meantime remove it from
     443             :          * the freelist and try again.
     444             :          */
     445     1261895 :         if (dqp->q_nrefs) {
     446       41276 :                 xfs_dqunlock(dqp);
     447       41276 :                 XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
     448             : 
     449       41276 :                 trace_xfs_dqreclaim_want(dqp);
     450       41276 :                 list_lru_isolate(lru, &dqp->q_lru);
     451       41276 :                 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
     452       41276 :                 return LRU_REMOVED;
     453             :         }
     454             : 
     455             :         /*
     456             :          * If the dquot is dirty, flush it. If it's already being flushed, just
     457             :          * skip it so there is time for the IO to complete before we try to
     458             :          * reclaim it again on the next LRU pass.
     459             :          */
     460     1220619 :         if (!xfs_dqflock_nowait(dqp))
     461      301254 :                 goto out_miss_unlock;
     462             : 
     463      919365 :         if (XFS_DQ_IS_DIRTY(dqp)) {
     464         956 :                 struct xfs_buf  *bp = NULL;
     465         956 :                 int             error;
     466             : 
     467         956 :                 trace_xfs_dqreclaim_dirty(dqp);
     468             : 
     469             :                 /* we have to drop the LRU lock to flush the dquot */
     470         956 :                 spin_unlock(lru_lock);
     471             : 
     472         956 :                 error = xfs_qm_dqflush(dqp, &bp);
     473         956 :                 if (error)
     474           0 :                         goto out_unlock_dirty;
     475             : 
     476         956 :                 xfs_buf_delwri_queue(bp, &isol->buffers);
     477         956 :                 xfs_buf_relse(bp);
     478         956 :                 goto out_unlock_dirty;
     479             :         }
     480      918409 :         xfs_dqfunlock(dqp);
     481             : 
     482             :         /*
     483             :          * Prevent lookups now that we are past the point of no return.
     484             :          */
     485      918409 :         dqp->q_flags |= XFS_DQFLAG_FREEING;
     486      918409 :         xfs_dqunlock(dqp);
     487             : 
     488      918409 :         ASSERT(dqp->q_nrefs == 0);
     489      918409 :         list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
     490      918409 :         XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
     491      918409 :         trace_xfs_dqreclaim_done(dqp);
     492      918409 :         XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
     493      918409 :         return LRU_REMOVED;
     494             : 
     495      301254 : out_miss_unlock:
     496      301254 :         xfs_dqunlock(dqp);
     497      301300 : out_miss_busy:
     498      301300 :         trace_xfs_dqreclaim_busy(dqp);
     499      301300 :         XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
     500      301300 :         return LRU_SKIP;
     501             : 
     502             : out_unlock_dirty:
     503         956 :         trace_xfs_dqreclaim_busy(dqp);
     504         956 :         XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
     505         956 :         xfs_dqunlock(dqp);
     506         956 :         spin_lock(lru_lock);
     507         956 :         return LRU_RETRY;
     508             : }
     509             : 
     510             : static unsigned long
     511       16357 : xfs_qm_shrink_scan(
     512             :         struct shrinker         *shrink,
     513             :         struct shrink_control   *sc)
     514             : {
     515       16357 :         struct xfs_quotainfo    *qi = container_of(shrink,
     516             :                                         struct xfs_quotainfo, qi_shrinker);
     517       16357 :         struct xfs_qm_isolate   isol;
     518       16357 :         unsigned long           freed;
     519       16357 :         int                     error;
     520             : 
     521       16357 :         if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
     522             :                 return 0;
     523             : 
     524       16357 :         INIT_LIST_HEAD(&isol.buffers);
     525       16357 :         INIT_LIST_HEAD(&isol.dispose);
     526             : 
     527       16357 :         freed = list_lru_shrink_walk(&qi->qi_lru, sc,
     528             :                                      xfs_qm_dquot_isolate, &isol);
     529             : 
     530       16357 :         error = xfs_buf_delwri_submit(&isol.buffers);
     531       16357 :         if (error)
     532           0 :                 xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
     533             : 
     534      934766 :         while (!list_empty(&isol.dispose)) {
     535      918409 :                 struct xfs_dquot        *dqp;
     536             : 
     537      918409 :                 dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
     538      918409 :                 list_del_init(&dqp->q_lru);
     539      918409 :                 xfs_qm_dqfree_one(dqp);
     540             :         }
     541             : 
     542             :         return freed;
     543             : }
     544             : 
     545             : static unsigned long
     546        6983 : xfs_qm_shrink_count(
     547             :         struct shrinker         *shrink,
     548             :         struct shrink_control   *sc)
     549             : {
     550        6983 :         struct xfs_quotainfo    *qi = container_of(shrink,
     551             :                                         struct xfs_quotainfo, qi_shrinker);
     552             : 
     553        6983 :         return list_lru_shrink_count(&qi->qi_lru, sc);
     554             : }
     555             : 
     556             : STATIC void
     557      168467 : xfs_qm_set_defquota(
     558             :         struct xfs_mount        *mp,
     559             :         xfs_dqtype_t            type,
     560             :         struct xfs_quotainfo    *qinf)
     561             : {
     562      168467 :         struct xfs_dquot        *dqp;
     563      168467 :         struct xfs_def_quota    *defq;
     564      168467 :         int                     error;
     565             : 
     566      168467 :         error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
     567      168467 :         if (error)
     568       39351 :                 return;
     569             : 
     570      129116 :         defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp));
     571             : 
     572             :         /*
     573             :          * Timers and warnings have been already set, let's just set the
     574             :          * default limits for this quota type
     575             :          */
     576      129116 :         defq->blk.hard = dqp->q_blk.hardlimit;
     577      129116 :         defq->blk.soft = dqp->q_blk.softlimit;
     578      129116 :         defq->ino.hard = dqp->q_ino.hardlimit;
     579      129116 :         defq->ino.soft = dqp->q_ino.softlimit;
     580      129116 :         defq->rtb.hard = dqp->q_rtb.hardlimit;
     581      129116 :         defq->rtb.soft = dqp->q_rtb.softlimit;
     582      129116 :         xfs_qm_dqdestroy(dqp);
     583             : }
     584             : 
     585             : /* Initialize quota time limits from the root dquot. */
     586             : static void
     587      172656 : xfs_qm_init_timelimits(
     588             :         struct xfs_mount        *mp,
     589             :         xfs_dqtype_t            type)
     590             : {
     591      172656 :         struct xfs_quotainfo    *qinf = mp->m_quotainfo;
     592      172656 :         struct xfs_def_quota    *defq;
     593      172656 :         struct xfs_dquot        *dqp;
     594      172656 :         int                     error;
     595             : 
     596      172656 :         defq = xfs_get_defquota(qinf, type);
     597             : 
     598      172656 :         defq->blk.time = XFS_QM_BTIMELIMIT;
     599      172656 :         defq->ino.time = XFS_QM_ITIMELIMIT;
     600      172656 :         defq->rtb.time = XFS_QM_RTBTIMELIMIT;
     601             : 
     602             :         /*
     603             :          * We try to get the limits from the superuser's limits fields.
     604             :          * This is quite hacky, but it is standard quota practice.
     605             :          *
     606             :          * Since we may not have done a quotacheck by this point, just read
     607             :          * the dquot without attaching it to any hashtables or lists.
     608             :          */
     609      172656 :         error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
     610      172656 :         if (error)
     611       43540 :                 return;
     612             : 
     613             :         /*
     614             :          * The warnings and timers set the grace period given to
     615             :          * a user or group before he or she can not perform any
     616             :          * more writing. If it is zero, a default is used.
     617             :          */
     618      129116 :         if (dqp->q_blk.timer)
     619         284 :                 defq->blk.time = dqp->q_blk.timer;
     620      129116 :         if (dqp->q_ino.timer)
     621         274 :                 defq->ino.time = dqp->q_ino.timer;
     622      129116 :         if (dqp->q_rtb.timer)
     623          51 :                 defq->rtb.time = dqp->q_rtb.timer;
     624             : 
     625      129116 :         xfs_qm_dqdestroy(dqp);
     626             : }
     627             : 
     628             : /*
     629             :  * This initializes all the quota information that's kept in the
     630             :  * mount structure
     631             :  */
     632             : STATIC int
     633       57626 : xfs_qm_init_quotainfo(
     634             :         struct xfs_mount        *mp)
     635             : {
     636       57626 :         struct xfs_quotainfo    *qinf;
     637       57626 :         int                     error;
     638             : 
     639       57626 :         ASSERT(XFS_IS_QUOTA_ON(mp));
     640             : 
     641       57626 :         qinf = mp->m_quotainfo = kmem_zalloc(sizeof(struct xfs_quotainfo), 0);
     642             : 
     643       57626 :         error = list_lru_init(&qinf->qi_lru);
     644       57626 :         if (error)
     645           0 :                 goto out_free_qinf;
     646             : 
     647             :         /*
     648             :          * See if quotainodes are setup, and if not, allocate them,
     649             :          * and change the superblock accordingly.
     650             :          */
     651       57626 :         error = xfs_qm_init_quotainos(mp);
     652       57626 :         if (error)
     653          74 :                 goto out_free_lru;
     654             : 
     655       57552 :         INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
     656       57552 :         INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
     657       57552 :         INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_NOFS);
     658       57552 :         mutex_init(&qinf->qi_tree_lock);
     659             : 
     660             :         /* mutex used to serialize quotaoffs */
     661       57552 :         mutex_init(&qinf->qi_quotaofflock);
     662             : 
     663             :         /* Precalc some constants */
     664       57552 :         qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
     665       57552 :         qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
     666       57552 :         if (xfs_has_bigtime(mp)) {
     667       57294 :                 qinf->qi_expiry_min =
     668             :                         xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN);
     669       57294 :                 qinf->qi_expiry_max =
     670             :                         xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX);
     671             :         } else {
     672         258 :                 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN;
     673         258 :                 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX;
     674             :         }
     675       57552 :         trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min,
     676             :                         qinf->qi_expiry_max);
     677             : 
     678       57552 :         mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
     679             : 
     680       57552 :         xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER);
     681       57552 :         xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP);
     682       57552 :         xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ);
     683             : 
     684       57552 :         if (XFS_IS_UQUOTA_ON(mp))
     685       56656 :                 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf);
     686       57552 :         if (XFS_IS_GQUOTA_ON(mp))
     687       56010 :                 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf);
     688       57552 :         if (XFS_IS_PQUOTA_ON(mp))
     689       55801 :                 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf);
     690             : 
     691       57552 :         qinf->qi_shrinker.count_objects = xfs_qm_shrink_count;
     692       57552 :         qinf->qi_shrinker.scan_objects = xfs_qm_shrink_scan;
     693       57552 :         qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
     694       57552 :         qinf->qi_shrinker.flags = SHRINKER_NUMA_AWARE;
     695             : 
     696       57552 :         error = register_shrinker(&qinf->qi_shrinker, "xfs-qm:%s",
     697       57552 :                                   mp->m_super->s_id);
     698       57552 :         if (error)
     699           0 :                 goto out_free_inos;
     700             : 
     701       57552 :         xfs_hooks_init(&qinf->qi_mod_ino_dqtrx_hooks);
     702       57552 :         xfs_hooks_init(&qinf->qi_apply_dqtrx_hooks);
     703             : 
     704       57552 :         return 0;
     705             : 
     706             : out_free_inos:
     707           0 :         mutex_destroy(&qinf->qi_quotaofflock);
     708           0 :         mutex_destroy(&qinf->qi_tree_lock);
     709           0 :         xfs_qm_destroy_quotainos(qinf);
     710          74 : out_free_lru:
     711          74 :         list_lru_destroy(&qinf->qi_lru);
     712          74 : out_free_qinf:
     713          74 :         kmem_free(qinf);
     714          74 :         mp->m_quotainfo = NULL;
     715          74 :         return error;
     716             : }
     717             : 
     718             : /*
     719             :  * Gets called when unmounting a filesystem or when all quotas get
     720             :  * turned off.
     721             :  * This purges the quota inodes, destroys locks and frees itself.
     722             :  */
     723             : void
     724       57562 : xfs_qm_destroy_quotainfo(
     725             :         struct xfs_mount        *mp)
     726             : {
     727       57562 :         struct xfs_quotainfo    *qi;
     728             : 
     729       57562 :         qi = mp->m_quotainfo;
     730       57562 :         ASSERT(qi != NULL);
     731             : 
     732       57562 :         unregister_shrinker(&qi->qi_shrinker);
     733       57562 :         list_lru_destroy(&qi->qi_lru);
     734       57562 :         xfs_qm_destroy_quotainos(qi);
     735       57562 :         mutex_destroy(&qi->qi_tree_lock);
     736       57562 :         mutex_destroy(&qi->qi_quotaofflock);
     737       57562 :         kmem_free(qi);
     738       57562 :         mp->m_quotainfo = NULL;
     739       57562 : }
     740             : 
     741             : static inline const struct xfs_imeta_path *
     742             : xfs_qflags_to_imeta_path(
     743             :         unsigned int    qflags)
     744             : {
     745       39345 :         if (qflags & XFS_QMOPT_UQUOTA)
     746             :                 return &XFS_IMETA_USRQUOTA;
     747       26011 :         else if (qflags & XFS_QMOPT_GQUOTA)
     748             :                 return &XFS_IMETA_GRPQUOTA;
     749             :         else
     750       12996 :                 return &XFS_IMETA_PRJQUOTA;
     751             : }
     752             : 
     753             : /*
     754             :  * Switch the group and project quota in-core inode pointers if needed.
     755             :  *
     756             :  * On v4 superblocks that don't have separate pquotino, we share an inode
     757             :  * between gquota and pquota. If the on-disk superblock has GQUOTA and the
     758             :  * filesystem is now mounted with PQUOTA, just use sb_gquotino for sb_pquotino
     759             :  * and vice-versa.
     760             :  *
     761             :  * We tolerate the direct manipulation of the in-core sb quota inode pointers
     762             :  * here because calling xfs_imeta_ functions is only really required for
     763             :  * filesystems with the metadata directory feature.  That feature requires a v5
     764             :  * superblock, which always supports simultaneous group and project quotas, so
     765             :  * we'll never get here.
     766             :  */
     767             : STATIC int
     768       39345 : xfs_qm_qino_switch(
     769             :         struct xfs_mount        *mp,
     770             :         struct xfs_inode        **ipp,
     771             :         unsigned int            flags,
     772             :         bool                    *need_alloc)
     773             : {
     774       39345 :         xfs_ino_t               ino = NULLFSINO;
     775       39345 :         int                     error;
     776             : 
     777       39345 :         if (xfs_has_pquotino(mp) ||
     778          93 :             !(flags & (XFS_QMOPT_PQUOTA | XFS_QMOPT_GQUOTA)))
     779             :                 return 0;
     780             : 
     781          52 :         if ((flags & XFS_QMOPT_PQUOTA) &&
     782          41 :             (mp->m_sb.sb_gquotino != NULLFSINO)) {
     783          11 :                 ino = mp->m_sb.sb_gquotino;
     784          11 :                 if (XFS_IS_CORRUPT(mp, mp->m_sb.sb_pquotino != NULLFSINO)) {
     785           0 :                         xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA);
     786           0 :                         return -EFSCORRUPTED;
     787             :                 }
     788          41 :         } else if ((flags & XFS_QMOPT_GQUOTA) &&
     789          11 :                    (mp->m_sb.sb_pquotino != NULLFSINO)) {
     790           0 :                 ino = mp->m_sb.sb_pquotino;
     791           0 :                 if (XFS_IS_CORRUPT(mp, mp->m_sb.sb_gquotino != NULLFSINO)) {
     792           0 :                         xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA);
     793           0 :                         return -EFSCORRUPTED;
     794             :                 }
     795             :         }
     796             : 
     797          11 :         if (ino == NULLFSINO)
     798             :                 return 0;
     799             : 
     800          11 :         error = xfs_imeta_iget(mp, ino, XFS_DIR3_FT_REG_FILE, ipp);
     801          11 :         if (error)
     802             :                 return error;
     803             : 
     804          11 :         if (flags & XFS_QMOPT_PQUOTA) {
     805          11 :                 mp->m_sb.sb_gquotino = NULLFSINO;
     806          11 :                 mp->m_sb.sb_pquotino = ino;
     807           0 :         } else if (flags & XFS_QMOPT_GQUOTA) {
     808           0 :                 mp->m_sb.sb_gquotino = ino;
     809           0 :                 mp->m_sb.sb_pquotino = NULLFSINO;
     810             :         }
     811          11 :         *need_alloc = false;
     812          11 :         return 0;
     813             : }
     814             : 
     815             : /*
     816             :  * Create an inode and return with a reference already taken, but unlocked
     817             :  * This is how we create quota inodes
     818             :  */
     819             : STATIC int
     820       39345 : xfs_qm_qino_alloc(
     821             :         struct xfs_mount        *mp,
     822             :         struct xfs_inode        **ipp,
     823             :         unsigned int            flags)
     824             : {
     825       39345 :         struct xfs_imeta_update upd = { };
     826       39345 :         const struct xfs_imeta_path *path = xfs_qflags_to_imeta_path(flags);
     827       39345 :         int                     error;
     828       39345 :         bool                    need_alloc = true;
     829             : 
     830       39345 :         *ipp = NULL;
     831             : 
     832       39345 :         error = xfs_qm_qino_switch(mp, ipp, flags, &need_alloc);
     833       39345 :         if (error)
     834             :                 return error;
     835             : 
     836       39345 :         if (need_alloc) {
     837             :                 /*
     838             :                  * Ensure the quota directory exists, being careful to disable
     839             :                  * quotas while we do this.  We'll have to quotacheck anyway,
     840             :                  * so the temporary undercount of the directory tree shouldn't
     841             :                  * affect the quota count.
     842             :                  */
     843       39334 :                 if (xfs_has_metadir(mp)) {
     844       35230 :                         unsigned int    old_qflags;
     845             : 
     846       35230 :                         old_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
     847       35230 :                         mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
     848       35230 :                         error = xfs_imeta_ensure_dirpath(mp, path);
     849       35230 :                         mp->m_qflags |= old_qflags;
     850       35230 :                         if (error)
     851             :                                 return error;
     852             :                 }
     853             : 
     854       39267 :                 error = xfs_imeta_start_create(mp, path, &upd);
     855             :         } else {
     856          11 :                 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create, 0, 0, 0,
     857             :                                 &upd.tp);
     858             :         }
     859       39278 :         if (error)
     860           7 :                 goto out_end;
     861             : 
     862             :         /*
     863             :          * Make the changes in the superblock, and log those too.
     864             :          * sbfields arg may contain fields other than *QUOTINO;
     865             :          * VERSIONNUM for example.
     866             :          */
     867       39271 :         spin_lock(&mp->m_sb_lock);
     868       39271 :         if (flags & XFS_QMOPT_SBVERSION) {
     869       13480 :                 ASSERT(!xfs_has_quota(mp));
     870             : 
     871       13480 :                 xfs_add_quota(mp);
     872       13480 :                 mp->m_sb.sb_uquotino = NULLFSINO;
     873       13480 :                 mp->m_sb.sb_gquotino = NULLFSINO;
     874       13480 :                 mp->m_sb.sb_pquotino = NULLFSINO;
     875             : 
     876             :                 /* qflags will get updated fully _after_ quotacheck */
     877       13480 :                 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
     878             :         }
     879       39271 :         spin_unlock(&mp->m_sb_lock);
     880       39271 :         xfs_log_sb(upd.tp);
     881             : 
     882       39271 :         if (need_alloc) {
     883       39260 :                 error = xfs_imeta_create(&upd, S_IFREG, ipp);
     884       39260 :                 if (error)
     885           0 :                         goto out_cancel;
     886             :         }
     887             : 
     888       39271 :         error = xfs_imeta_commit_update(&upd);
     889       39271 :         if (error) {
     890           0 :                 ASSERT(xfs_is_shutdown(mp));
     891           0 :                 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
     892           0 :                 goto out_end;
     893             :         }
     894             : 
     895       39271 :         if (need_alloc)
     896       39260 :                 xfs_finish_inode_setup(*ipp);
     897             : 
     898             :         return 0;
     899             : 
     900             : out_cancel:
     901           0 :         xfs_imeta_cancel_update(&upd, error);
     902           7 : out_end:
     903             :         /* Have to finish setting up the inode to ensure it's deleted. */
     904           7 :         if (*ipp) {
     905           0 :                 xfs_finish_inode_setup(*ipp);
     906           0 :                 xfs_irele(*ipp);
     907           0 :                 *ipp = NULL;
     908             :         }
     909             :         return error;
     910             : }
     911             : 
     912             : 
     913             : STATIC void
     914        6153 : xfs_qm_reset_dqcounts(
     915             :         struct xfs_mount        *mp,
     916             :         struct xfs_buf          *bp,
     917             :         xfs_dqid_t              id,
     918             :         xfs_dqtype_t            type)
     919             : {
     920        6153 :         struct xfs_dqblk        *dqb;
     921        6153 :         int                     j;
     922             : 
     923        6153 :         trace_xfs_reset_dqcounts(bp, _RET_IP_);
     924             : 
     925             :         /*
     926             :          * Reset all counters and timers. They'll be
     927             :          * started afresh by xfs_qm_quotacheck.
     928             :          */
     929             : #ifdef DEBUG
     930        6153 :         j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
     931             :                 sizeof(struct xfs_dqblk);
     932        6153 :         ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
     933             : #endif
     934        6153 :         dqb = bp->b_addr;
     935      190743 :         for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
     936      184590 :                 struct xfs_disk_dquot   *ddq;
     937             : 
     938      184590 :                 ddq = (struct xfs_disk_dquot *)&dqb[j];
     939             : 
     940             :                 /*
     941             :                  * Do a sanity check, and if needed, repair the dqblk. Don't
     942             :                  * output any warnings because it's perfectly possible to
     943             :                  * find uninitialised dquot blks. See comment in
     944             :                  * xfs_dquot_verify.
     945             :                  */
     946      184590 :                 if (xfs_dqblk_verify(mp, &dqb[j], id + j) ||
     947      184500 :                     (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type)
     948         420 :                         xfs_dqblk_repair(mp, &dqb[j], id + j, type);
     949             : 
     950             :                 /*
     951             :                  * Reset type in case we are reusing group quota file for
     952             :                  * project quotas or vice versa
     953             :                  */
     954      184590 :                 ddq->d_type = type;
     955      184590 :                 ddq->d_bcount = 0;
     956      184590 :                 ddq->d_icount = 0;
     957      184590 :                 ddq->d_rtbcount = 0;
     958             : 
     959             :                 /*
     960             :                  * dquot id 0 stores the default grace period and the maximum
     961             :                  * warning limit that were set by the administrator, so we
     962             :                  * should not reset them.
     963             :                  */
     964      184590 :                 if (ddq->d_id != 0) {
     965      183534 :                         ddq->d_btimer = 0;
     966      183534 :                         ddq->d_itimer = 0;
     967      183534 :                         ddq->d_rtbtimer = 0;
     968      183534 :                         ddq->d_bwarns = 0;
     969      183534 :                         ddq->d_iwarns = 0;
     970      183534 :                         ddq->d_rtbwarns = 0;
     971      183534 :                         if (xfs_has_bigtime(mp))
     972      181939 :                                 ddq->d_type |= XFS_DQTYPE_BIGTIME;
     973             :                 }
     974             : 
     975      184590 :                 if (xfs_has_crc(mp)) {
     976      182940 :                         xfs_update_cksum((char *)&dqb[j],
     977             :                                          sizeof(struct xfs_dqblk),
     978             :                                          XFS_DQUOT_CRC_OFF);
     979             :                 }
     980             :         }
     981        6153 : }
     982             : 
     983             : STATIC int
     984        5836 : xfs_qm_reset_dqcounts_all(
     985             :         struct xfs_mount        *mp,
     986             :         xfs_dqid_t              firstid,
     987             :         xfs_fsblock_t           bno,
     988             :         xfs_filblks_t           blkcnt,
     989             :         xfs_dqtype_t            type,
     990             :         struct list_head        *buffer_list)
     991             : {
     992        5836 :         struct xfs_buf          *bp;
     993        5836 :         int                     error = 0;
     994             : 
     995        5836 :         ASSERT(blkcnt > 0);
     996             : 
     997             :         /*
     998             :          * Blkcnt arg can be a very big number, and might even be
     999             :          * larger than the log itself. So, we have to break it up into
    1000             :          * manageable-sized transactions.
    1001             :          * Note that we don't start a permanent transaction here; we might
    1002             :          * not be able to get a log reservation for the whole thing up front,
    1003             :          * and we don't really care to either, because we just discard
    1004             :          * everything if we were to crash in the middle of this loop.
    1005             :          */
    1006       11989 :         while (blkcnt--) {
    1007       18459 :                 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
    1008        6153 :                               XFS_FSB_TO_DADDR(mp, bno),
    1009        6153 :                               mp->m_quotainfo->qi_dqchunklen, 0, &bp,
    1010             :                               &xfs_dquot_buf_ops);
    1011             : 
    1012             :                 /*
    1013             :                  * CRC and validation errors will return a EFSCORRUPTED here. If
    1014             :                  * this occurs, re-read without CRC validation so that we can
    1015             :                  * repair the damage via xfs_qm_reset_dqcounts(). This process
    1016             :                  * will leave a trace in the log indicating corruption has
    1017             :                  * been detected.
    1018             :                  */
    1019        6153 :                 if (error == -EFSCORRUPTED) {
    1020           9 :                         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
    1021           3 :                                       XFS_FSB_TO_DADDR(mp, bno),
    1022           3 :                                       mp->m_quotainfo->qi_dqchunklen, 0, &bp,
    1023             :                                       NULL);
    1024             :                 }
    1025             : 
    1026        6153 :                 if (error)
    1027             :                         break;
    1028             : 
    1029             :                 /*
    1030             :                  * A corrupt buffer might not have a verifier attached, so
    1031             :                  * make sure we have the correct one attached before writeback
    1032             :                  * occurs.
    1033             :                  */
    1034        6153 :                 bp->b_ops = &xfs_dquot_buf_ops;
    1035        6153 :                 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
    1036        6153 :                 xfs_buf_delwri_queue(bp, buffer_list);
    1037        6153 :                 xfs_buf_relse(bp);
    1038             : 
    1039             :                 /* goto the next block. */
    1040        6153 :                 bno++;
    1041        6153 :                 firstid += mp->m_quotainfo->qi_dqperchunk;
    1042             :         }
    1043             : 
    1044        5836 :         return error;
    1045             : }
    1046             : 
    1047             : /*
    1048             :  * Iterate over all allocated dquot blocks in this quota inode, zeroing all
    1049             :  * counters for every chunk of dquots that we find.
    1050             :  */
    1051             : STATIC int
    1052       40394 : xfs_qm_reset_dqcounts_buf(
    1053             :         struct xfs_mount        *mp,
    1054             :         struct xfs_inode        *qip,
    1055             :         xfs_dqtype_t            type,
    1056             :         struct list_head        *buffer_list)
    1057             : {
    1058       40394 :         struct xfs_bmbt_irec    *map;
    1059       40394 :         int                     i, nmaps;       /* number of map entries */
    1060       40394 :         int                     error;          /* return value */
    1061       40394 :         xfs_fileoff_t           lblkno;
    1062       40394 :         xfs_filblks_t           maxlblkcnt;
    1063       40394 :         xfs_dqid_t              firstid;
    1064       40394 :         xfs_fsblock_t           rablkno;
    1065       40394 :         xfs_filblks_t           rablkcnt;
    1066             : 
    1067       40394 :         error = 0;
    1068             :         /*
    1069             :          * This looks racy, but we can't keep an inode lock across a
    1070             :          * trans_reserve. But, this gets called during quotacheck, and that
    1071             :          * happens only at mount time which is single threaded.
    1072             :          */
    1073       40394 :         if (qip->i_nblocks == 0)
    1074             :                 return 0;
    1075             : 
    1076        1046 :         map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
    1077             : 
    1078        1046 :         lblkno = 0;
    1079        1046 :         maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
    1080        2969 :         do {
    1081        2969 :                 uint            lock_mode;
    1082             : 
    1083        2969 :                 nmaps = XFS_DQITER_MAP_SIZE;
    1084             :                 /*
    1085             :                  * We aren't changing the inode itself. Just changing
    1086             :                  * some of its data. No new blocks are added here, and
    1087             :                  * the inode is never added to the transaction.
    1088             :                  */
    1089        2969 :                 lock_mode = xfs_ilock_data_map_shared(qip);
    1090        2969 :                 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
    1091             :                                        map, &nmaps, 0);
    1092        2969 :                 xfs_iunlock(qip, lock_mode);
    1093        2969 :                 if (error)
    1094             :                         break;
    1095             : 
    1096        2969 :                 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
    1097       14386 :                 for (i = 0; i < nmaps; i++) {
    1098       11417 :                         ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
    1099       11417 :                         ASSERT(map[i].br_blockcount);
    1100             : 
    1101             : 
    1102       11417 :                         lblkno += map[i].br_blockcount;
    1103             : 
    1104       11417 :                         if (map[i].br_startblock == HOLESTARTBLOCK)
    1105        5581 :                                 continue;
    1106             : 
    1107        5836 :                         firstid = (xfs_dqid_t) map[i].br_startoff *
    1108        5836 :                                 mp->m_quotainfo->qi_dqperchunk;
    1109             :                         /*
    1110             :                          * Do a read-ahead on the next extent.
    1111             :                          */
    1112        5836 :                         if ((i+1 < nmaps) &&
    1113        5588 :                             (map[i+1].br_startblock != HOLESTARTBLOCK)) {
    1114         246 :                                 rablkcnt =  map[i+1].br_blockcount;
    1115         246 :                                 rablkno = map[i+1].br_startblock;
    1116         508 :                                 while (rablkcnt--) {
    1117         786 :                                         xfs_buf_readahead(mp->m_ddev_targp,
    1118         262 :                                                XFS_FSB_TO_DADDR(mp, rablkno),
    1119         262 :                                                mp->m_quotainfo->qi_dqchunklen,
    1120             :                                                &xfs_dquot_buf_ops);
    1121         262 :                                         rablkno++;
    1122             :                                 }
    1123             :                         }
    1124             :                         /*
    1125             :                          * Iterate thru all the blks in the extent and
    1126             :                          * reset the counters of all the dquots inside them.
    1127             :                          */
    1128        5836 :                         error = xfs_qm_reset_dqcounts_all(mp, firstid,
    1129             :                                                    map[i].br_startblock,
    1130             :                                                    map[i].br_blockcount,
    1131             :                                                    type, buffer_list);
    1132        5836 :                         if (error)
    1133           0 :                                 goto out;
    1134             :                 }
    1135        2969 :         } while (nmaps > 0);
    1136             : 
    1137        1046 : out:
    1138        1046 :         kmem_free(map);
    1139        1046 :         return error;
    1140             : }
    1141             : 
    1142             : /*
    1143             :  * Called by dqusage_adjust in doing a quotacheck.
    1144             :  *
    1145             :  * Given the inode, and a dquot id this updates both the incore dqout as well
    1146             :  * as the buffer copy. This is so that once the quotacheck is done, we can
    1147             :  * just log all the buffers, as opposed to logging numerous updates to
    1148             :  * individual dquots.
    1149             :  */
    1150             : STATIC int
    1151      702088 : xfs_qm_quotacheck_dqadjust(
    1152             :         struct xfs_inode        *ip,
    1153             :         xfs_dqtype_t            type,
    1154             :         xfs_qcnt_t              nblks,
    1155             :         xfs_qcnt_t              rtblks)
    1156             : {
    1157      702088 :         struct xfs_mount        *mp = ip->i_mount;
    1158      702088 :         struct xfs_dquot        *dqp;
    1159      702088 :         xfs_dqid_t              id;
    1160      702088 :         int                     error;
    1161             : 
    1162      702088 :         id = xfs_qm_id_for_quotatype(ip, type);
    1163      702100 :         error = xfs_qm_dqget(mp, id, type, true, &dqp);
    1164      702601 :         if (error) {
    1165             :                 /*
    1166             :                  * Shouldn't be able to turn off quotas here.
    1167             :                  */
    1168           0 :                 ASSERT(error != -ESRCH);
    1169           0 :                 ASSERT(error != -ENOENT);
    1170           0 :                 return error;
    1171             :         }
    1172             : 
    1173      702601 :         trace_xfs_dqadjust(dqp);
    1174             : 
    1175             :         /*
    1176             :          * Adjust the inode count and the block count to reflect this inode's
    1177             :          * resource usage.
    1178             :          */
    1179      702598 :         dqp->q_ino.count++;
    1180      702598 :         dqp->q_ino.reserved++;
    1181      702598 :         if (nblks) {
    1182       69432 :                 dqp->q_blk.count += nblks;
    1183       69432 :                 dqp->q_blk.reserved += nblks;
    1184             :         }
    1185      702598 :         if (rtblks) {
    1186        4893 :                 dqp->q_rtb.count += rtblks;
    1187        4893 :                 dqp->q_rtb.reserved += rtblks;
    1188             :         }
    1189             : 
    1190             :         /*
    1191             :          * Set default limits, adjust timers (since we changed usages)
    1192             :          *
    1193             :          * There are no timers for the default values set in the root dquot.
    1194             :          */
    1195      702598 :         if (dqp->q_id) {
    1196       32852 :                 xfs_qm_adjust_dqlimits(dqp);
    1197       32852 :                 xfs_qm_adjust_dqtimers(dqp);
    1198             :         }
    1199             : 
    1200      702598 :         dqp->q_flags |= XFS_DQFLAG_DIRTY;
    1201      702598 :         xfs_qm_dqput(dqp);
    1202      702598 :         return 0;
    1203             : }
    1204             : 
    1205             : /*
    1206             :  * callback routine supplied to bulkstat(). Given an inumber, find its
    1207             :  * dquots and update them to account for resources taken by that inode.
    1208             :  */
    1209             : /* ARGSUSED */
    1210             : STATIC int
    1211      393262 : xfs_qm_dqusage_adjust(
    1212             :         struct xfs_mount        *mp,
    1213             :         struct xfs_trans        *tp,
    1214             :         xfs_ino_t               ino,
    1215             :         void                    *data)
    1216             : {
    1217      393262 :         struct xfs_inode        *ip;
    1218      393262 :         xfs_filblks_t           nblks, rtblks;
    1219      393262 :         unsigned int            lock_mode;
    1220      393262 :         int                     error;
    1221             : 
    1222      393262 :         ASSERT(XFS_IS_QUOTA_ON(mp));
    1223             : 
    1224             :         /*
    1225             :          * rootino must have its resources accounted for, not so with the quota
    1226             :          * inodes.
    1227             :          */
    1228      772643 :         if (xfs_is_quota_inode(&mp->m_sb, ino))
    1229             :                 return 0;
    1230             : 
    1231             :         /*
    1232             :          * We don't _need_ to take the ilock EXCL here because quotacheck runs
    1233             :          * at mount time and therefore nobody will be racing chown/chproj.
    1234             :          */
    1235      352588 :         error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip);
    1236      352849 :         if (error == -EINVAL || error == -ENOENT)
    1237             :                 return 0;
    1238      352849 :         if (error)
    1239             :                 return error;
    1240             : 
    1241             :         /* Metadata directory files are not accounted to user-visible quotas. */
    1242      352849 :         if (xfs_is_metadir_inode(ip))
    1243      116800 :                 goto error0;
    1244             : 
    1245      236049 :         ASSERT(ip->i_delayed_blks == 0);
    1246             : 
    1247      236049 :         lock_mode = xfs_ilock_data_map_shared(ip);
    1248      235895 :         if (XFS_IS_REALTIME_INODE(ip)) {
    1249       74055 :                 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
    1250       74055 :                 if (error) {
    1251           0 :                         xfs_iunlock(ip, lock_mode);
    1252           0 :                         goto error0;
    1253             :                 }
    1254             :         }
    1255      235895 :         xfs_inode_count_blocks(tp, ip, &nblks, &rtblks);
    1256      235637 :         xfs_iunlock(ip, lock_mode);
    1257             : 
    1258             :         /*
    1259             :          * Add the (disk blocks and inode) resources occupied by this
    1260             :          * inode to its dquots. We do this adjustment in the incore dquot,
    1261             :          * and also copy the changes to its buffer.
    1262             :          * We don't care about putting these changes in a transaction
    1263             :          * envelope because if we crash in the middle of a 'quotacheck'
    1264             :          * we have to start from the beginning anyway.
    1265             :          * Once we're done, we'll log all the dquot bufs.
    1266             :          *
    1267             :          * The *QUOTA_ON checks below may look pretty racy, but quotachecks
    1268             :          * and quotaoffs don't race. (Quotachecks happen at mount time only).
    1269             :          */
    1270      235864 :         if (XFS_IS_UQUOTA_ON(mp)) {
    1271      234384 :                 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks,
    1272             :                                 rtblks);
    1273      234751 :                 if (error)
    1274           0 :                         goto error0;
    1275             :         }
    1276             : 
    1277      236231 :         if (XFS_IS_GQUOTA_ON(mp)) {
    1278      234152 :                 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks,
    1279             :                                 rtblks);
    1280      234155 :                 if (error)
    1281           0 :                         goto error0;
    1282             :         }
    1283             : 
    1284      236234 :         if (XFS_IS_PQUOTA_ON(mp)) {
    1285      233682 :                 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks,
    1286             :                                 rtblks);
    1287      233684 :                 if (error)
    1288           0 :                         goto error0;
    1289             :         }
    1290             : 
    1291      236236 : error0:
    1292      353036 :         xfs_irele(ip);
    1293      353036 :         return error;
    1294             : }
    1295             : 
    1296             : STATIC int
    1297       64269 : xfs_qm_flush_one(
    1298             :         struct xfs_dquot        *dqp,
    1299             :         void                    *data)
    1300             : {
    1301       64269 :         struct xfs_mount        *mp = dqp->q_mount;
    1302       64269 :         struct list_head        *buffer_list = data;
    1303       64269 :         struct xfs_buf          *bp = NULL;
    1304       64269 :         int                     error = 0;
    1305             : 
    1306       64269 :         xfs_dqlock(dqp);
    1307       64269 :         if (dqp->q_flags & XFS_DQFLAG_FREEING)
    1308           0 :                 goto out_unlock;
    1309       64269 :         if (!XFS_DQ_IS_DIRTY(dqp))
    1310           0 :                 goto out_unlock;
    1311             : 
    1312             :         /*
    1313             :          * The only way the dquot is already flush locked by the time quotacheck
    1314             :          * gets here is if reclaim flushed it before the dqadjust walk dirtied
    1315             :          * it for the final time. Quotacheck collects all dquot bufs in the
    1316             :          * local delwri queue before dquots are dirtied, so reclaim can't have
    1317             :          * possibly queued it for I/O. The only way out is to push the buffer to
    1318             :          * cycle the flush lock.
    1319             :          */
    1320       64269 :         if (!xfs_dqflock_nowait(dqp)) {
    1321             :                 /* buf is pinned in-core by delwri list */
    1322           0 :                 error = xfs_buf_incore(mp->m_ddev_targp, dqp->q_blkno,
    1323           0 :                                 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
    1324           0 :                 if (error)
    1325           0 :                         goto out_unlock;
    1326             : 
    1327           0 :                 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
    1328           0 :                         error = -EAGAIN;
    1329           0 :                         xfs_buf_relse(bp);
    1330           0 :                         goto out_unlock;
    1331             :                 }
    1332           0 :                 xfs_buf_unlock(bp);
    1333             : 
    1334           0 :                 xfs_buf_delwri_pushbuf(bp, buffer_list);
    1335           0 :                 xfs_buf_rele(bp);
    1336             : 
    1337           0 :                 error = -EAGAIN;
    1338           0 :                 goto out_unlock;
    1339             :         }
    1340             : 
    1341       64269 :         error = xfs_qm_dqflush(dqp, &bp);
    1342       64269 :         if (error)
    1343           0 :                 goto out_unlock;
    1344             : 
    1345       64269 :         xfs_buf_delwri_queue(bp, buffer_list);
    1346       64269 :         xfs_buf_relse(bp);
    1347       64269 : out_unlock:
    1348       64269 :         xfs_dqunlock(dqp);
    1349       64269 :         return error;
    1350             : }
    1351             : 
    1352             : /*
    1353             :  * Walk thru all the filesystem inodes and construct a consistent view
    1354             :  * of the disk quota world. If the quotacheck fails, disable quotas.
    1355             :  */
    1356             : STATIC int
    1357       14200 : xfs_qm_quotacheck(
    1358             :         xfs_mount_t     *mp)
    1359             : {
    1360       14200 :         int                     error, error2;
    1361       14200 :         uint                    flags;
    1362       14200 :         LIST_HEAD               (buffer_list);
    1363       14200 :         struct xfs_inode        *uip = mp->m_quotainfo->qi_uquotaip;
    1364       14200 :         struct xfs_inode        *gip = mp->m_quotainfo->qi_gquotaip;
    1365       14200 :         struct xfs_inode        *pip = mp->m_quotainfo->qi_pquotaip;
    1366             : 
    1367       14200 :         flags = 0;
    1368             : 
    1369       14200 :         ASSERT(uip || gip || pip);
    1370       14200 :         ASSERT(XFS_IS_QUOTA_ON(mp));
    1371             : 
    1372       14200 :         xfs_notice(mp, "Quotacheck needed: Please wait.");
    1373             : 
    1374             :         /*
    1375             :          * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
    1376             :          * their counters to zero. We need a clean slate.
    1377             :          * We don't log our changes till later.
    1378             :          */
    1379       14200 :         if (uip) {
    1380       13718 :                 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER,
    1381             :                                          &buffer_list);
    1382       13718 :                 if (error)
    1383           0 :                         goto error_return;
    1384             :                 flags |= XFS_UQUOTA_CHKD;
    1385             :         }
    1386             : 
    1387       14200 :         if (gip) {
    1388       13382 :                 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP,
    1389             :                                          &buffer_list);
    1390       13382 :                 if (error)
    1391           0 :                         goto error_return;
    1392       13382 :                 flags |= XFS_GQUOTA_CHKD;
    1393             :         }
    1394             : 
    1395       14200 :         if (pip) {
    1396       13294 :                 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ,
    1397             :                                          &buffer_list);
    1398       13294 :                 if (error)
    1399           0 :                         goto error_return;
    1400       13294 :                 flags |= XFS_PQUOTA_CHKD;
    1401             :         }
    1402             : 
    1403       14200 :         error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true,
    1404             :                         NULL);
    1405             : 
    1406             :         /*
    1407             :          * On error, the inode walk may have partially populated the dquot
    1408             :          * caches.  We must purge them before disabling quota and tearing down
    1409             :          * the quotainfo, or else the dquots will leak.
    1410             :          */
    1411       14200 :         if (error)
    1412           0 :                 goto error_purge;
    1413             : 
    1414             :         /*
    1415             :          * We've made all the changes that we need to make incore.  Flush them
    1416             :          * down to disk buffers if everything was updated successfully.
    1417             :          */
    1418       14200 :         if (XFS_IS_UQUOTA_ON(mp)) {
    1419       13718 :                 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one,
    1420             :                                           &buffer_list);
    1421             :         }
    1422       14200 :         if (XFS_IS_GQUOTA_ON(mp)) {
    1423       13382 :                 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one,
    1424             :                                            &buffer_list);
    1425       13382 :                 if (!error)
    1426       13382 :                         error = error2;
    1427             :         }
    1428       14200 :         if (XFS_IS_PQUOTA_ON(mp)) {
    1429       13294 :                 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one,
    1430             :                                            &buffer_list);
    1431       13294 :                 if (!error)
    1432       13294 :                         error = error2;
    1433             :         }
    1434             : 
    1435       14200 :         error2 = xfs_buf_delwri_submit(&buffer_list);
    1436       14200 :         if (!error)
    1437       14200 :                 error = error2;
    1438             : 
    1439             :         /*
    1440             :          * We can get this error if we couldn't do a dquot allocation inside
    1441             :          * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
    1442             :          * dirty dquots that might be cached, we just want to get rid of them
    1443             :          * and turn quotaoff. The dquots won't be attached to any of the inodes
    1444             :          * at this point (because we intentionally didn't in dqget_noattach).
    1445             :          */
    1446       14200 :         if (error)
    1447           0 :                 goto error_purge;
    1448             : 
    1449             :         /*
    1450             :          * If one type of quotas is off, then it will lose its
    1451             :          * quotachecked status, since we won't be doing accounting for
    1452             :          * that type anymore.
    1453             :          */
    1454       14200 :         mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
    1455       14200 :         mp->m_qflags |= flags;
    1456             : 
    1457       14200 : error_return:
    1458       14200 :         xfs_buf_delwri_cancel(&buffer_list);
    1459             : 
    1460       14200 :         if (error) {
    1461           0 :                 xfs_warn(mp,
    1462             :         "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
    1463             :                         error);
    1464             :                 /*
    1465             :                  * We must turn off quotas.
    1466             :                  */
    1467           0 :                 ASSERT(mp->m_quotainfo != NULL);
    1468           0 :                 xfs_qm_destroy_quotainfo(mp);
    1469           0 :                 if (xfs_mount_reset_sbqflags(mp)) {
    1470           0 :                         xfs_warn(mp,
    1471             :                                 "Quotacheck: Failed to reset quota flags.");
    1472             :                 }
    1473           0 :                 xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK);
    1474             :         } else {
    1475       14200 :                 xfs_notice(mp, "Quotacheck: Done.");
    1476       14200 :                 xfs_fs_mark_healthy(mp, XFS_SICK_FS_QUOTACHECK);
    1477             :         }
    1478             : 
    1479       14200 :         return error;
    1480             : 
    1481           0 : error_purge:
    1482             :         /*
    1483             :          * On error, we may have inodes queued for inactivation. This may try
    1484             :          * to attach dquots to the inode before running cleanup operations on
    1485             :          * the inode and this can race with the xfs_qm_destroy_quotainfo() call
    1486             :          * below that frees mp->m_quotainfo. To avoid this race, flush all the
    1487             :          * pending inodegc operations before we purge the dquots from memory,
    1488             :          * ensuring that background inactivation is idle whilst we turn off
    1489             :          * quotas.
    1490             :          */
    1491           0 :         xfs_inodegc_flush(mp);
    1492           0 :         xfs_qm_dqpurge_all(mp);
    1493           0 :         goto error_return;
    1494             : 
    1495             : }
    1496             : 
    1497             : /*
    1498             :  * This is called from xfs_mountfs to start quotas and initialize all
    1499             :  * necessary data structures like quotainfo.  This is also responsible for
    1500             :  * running a quotacheck as necessary.  We are guaranteed that the superblock
    1501             :  * is consistently read in at this point.
    1502             :  *
    1503             :  * If we fail here, the mount will continue with quota turned off. We don't
    1504             :  * need to inidicate success or failure at all.
    1505             :  */
    1506             : int
    1507       57626 : xfs_qm_mount_quotas(
    1508             :         struct xfs_mount        *mp)
    1509             : {
    1510       57626 :         int                     error = 0;
    1511       57626 :         uint                    sbf;
    1512             : 
    1513       57626 :         if (mp->m_sb.sb_rextents)
    1514       21981 :                 xfs_warn(mp,
    1515             :         "EXPERIMENTAL realtime quota feature in use. Use at your own risk!");
    1516             : 
    1517       57626 :         ASSERT(XFS_IS_QUOTA_ON(mp));
    1518             : 
    1519             :         /*
    1520             :          * Allocate the quotainfo structure inside the mount struct, and
    1521             :          * create quotainode(s), and change/rev superblock if necessary.
    1522             :          */
    1523       57626 :         error = xfs_qm_init_quotainfo(mp);
    1524       57626 :         if (error) {
    1525             :                 /*
    1526             :                  * We must turn off quotas.
    1527             :                  */
    1528          74 :                 ASSERT(mp->m_quotainfo == NULL);
    1529          74 :                 mp->m_qflags = 0;
    1530          74 :                 goto write_changes;
    1531             :         }
    1532             :         /*
    1533             :          * If any of the quotas are not consistent, do a quotacheck.
    1534             :          */
    1535       57552 :         if (XFS_QM_NEED_QUOTACHECK(mp)) {
    1536       14200 :                 error = xfs_qm_quotacheck(mp);
    1537       14200 :                 if (error) {
    1538             :                         /* Quotacheck failed and disabled quotas. */
    1539             :                         return 0;
    1540             :                 }
    1541             :         }
    1542             :         /*
    1543             :          * If one type of quotas is off, then it will lose its
    1544             :          * quotachecked status, since we won't be doing accounting for
    1545             :          * that type anymore.
    1546             :          */
    1547       57552 :         if (!XFS_IS_UQUOTA_ON(mp))
    1548         896 :                 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
    1549       57552 :         if (!XFS_IS_GQUOTA_ON(mp))
    1550        1542 :                 mp->m_qflags &= ~XFS_GQUOTA_CHKD;
    1551       57552 :         if (!XFS_IS_PQUOTA_ON(mp))
    1552        1751 :                 mp->m_qflags &= ~XFS_PQUOTA_CHKD;
    1553             : 
    1554       55801 :  write_changes:
    1555             :         /*
    1556             :          * We actually don't have to acquire the m_sb_lock at all.
    1557             :          * This can only be called from mount, and that's single threaded. XXX
    1558             :          */
    1559       57626 :         spin_lock(&mp->m_sb_lock);
    1560       57626 :         sbf = mp->m_sb.sb_qflags;
    1561       57626 :         mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
    1562       57626 :         spin_unlock(&mp->m_sb_lock);
    1563             : 
    1564       57626 :         if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
    1565       14414 :                 if (xfs_sync_sb(mp, false)) {
    1566             :                         /*
    1567             :                          * We could only have been turning quotas off.
    1568             :                          * We aren't in very good shape actually because
    1569             :                          * the incore structures are convinced that quotas are
    1570             :                          * off, but the on disk superblock doesn't know that !
    1571             :                          */
    1572           0 :                         ASSERT(!(XFS_IS_QUOTA_ON(mp)));
    1573           0 :                         xfs_alert(mp, "%s: Superblock update failed!",
    1574             :                                 __func__);
    1575             :                 }
    1576             :         }
    1577             : 
    1578       57626 :         if (error) {
    1579          74 :                 xfs_warn(mp, "Failed to initialize disk quotas.");
    1580          74 :                 return 0;
    1581             :         }
    1582             : 
    1583             :         /*
    1584             :          * Attach dquots to realtime metadata files before we do anything that
    1585             :          * could alter the resource usage of rt metadata (log recovery, normal
    1586             :          * operation, etc).
    1587             :          */
    1588       57552 :         error = xfs_rtmount_dqattach(mp);
    1589       57552 :         if (error) {
    1590           0 :                 xfs_qm_unmount_quotas(mp);
    1591           0 :                 return error;
    1592             :         }
    1593             : 
    1594             :         return 0;
    1595             : }
    1596             : 
    1597             : /*
    1598             :  * This is called after the superblock has been read in and we're ready to
    1599             :  * iget the quota inodes.
    1600             :  */
    1601             : STATIC int
    1602       57626 : xfs_qm_init_quotainos(
    1603             :         xfs_mount_t     *mp)
    1604             : {
    1605       57626 :         struct xfs_inode        *uip = NULL;
    1606       57626 :         struct xfs_inode        *gip = NULL;
    1607       57626 :         struct xfs_inode        *pip = NULL;
    1608       57626 :         int                     error;
    1609       57626 :         uint                    flags = 0;
    1610             : 
    1611       57626 :         ASSERT(mp->m_quotainfo);
    1612             : 
    1613             :         /*
    1614             :          * Get the uquota and gquota inodes
    1615             :          */
    1616       57626 :         if (xfs_has_quota(mp)) {
    1617       44072 :                 if (XFS_IS_UQUOTA_ON(mp) &&
    1618       43451 :                     mp->m_sb.sb_uquotino != NULLFSINO) {
    1619       43396 :                         ASSERT(mp->m_sb.sb_uquotino > 0);
    1620       43396 :                         error = xfs_imeta_iget(mp, mp->m_sb.sb_uquotino,
    1621             :                                         XFS_DIR3_FT_REG_FILE, &uip);
    1622       43396 :                         if (error)
    1623             :                                 return error;
    1624             :                 }
    1625       44072 :                 if (XFS_IS_GQUOTA_ON(mp) &&
    1626       43206 :                     mp->m_sb.sb_gquotino != NULLFSINO) {
    1627       42995 :                         ASSERT(mp->m_sb.sb_gquotino > 0);
    1628       42995 :                         error = xfs_imeta_iget(mp, mp->m_sb.sb_gquotino,
    1629             :                                         XFS_DIR3_FT_REG_FILE, &gip);
    1630       42995 :                         if (error)
    1631           0 :                                 goto error_rele;
    1632             :                 }
    1633       44072 :                 if (XFS_IS_PQUOTA_ON(mp) &&
    1634       43030 :                     mp->m_sb.sb_pquotino != NULLFSINO) {
    1635       42805 :                         ASSERT(mp->m_sb.sb_pquotino > 0);
    1636       42805 :                         error = xfs_imeta_iget(mp, mp->m_sb.sb_pquotino,
    1637             :                                         XFS_DIR3_FT_REG_FILE, &pip);
    1638       42805 :                         if (error)
    1639           0 :                                 goto error_rele;
    1640             :                 }
    1641             :         } else {
    1642             :                 flags |= XFS_QMOPT_SBVERSION;
    1643             :         }
    1644             : 
    1645             :         /*
    1646             :          * Create the three inodes, if they don't exist already. The changes
    1647             :          * made above will get added to a transaction and logged in one of
    1648             :          * the qino_alloc calls below.  If the device is readonly,
    1649             :          * temporarily switch to read-write to do this.
    1650             :          */
    1651       57626 :         if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
    1652       13334 :                 error = xfs_qm_qino_alloc(mp, &uip,
    1653             :                                               flags | XFS_QMOPT_UQUOTA);
    1654       13334 :                 if (error)
    1655          74 :                         goto error_rele;
    1656             : 
    1657             :                 flags &= ~XFS_QMOPT_SBVERSION;
    1658             :         }
    1659       57552 :         if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
    1660       13015 :                 error = xfs_qm_qino_alloc(mp, &gip,
    1661             :                                           flags | XFS_QMOPT_GQUOTA);
    1662       13015 :                 if (error)
    1663           0 :                         goto error_rele;
    1664             : 
    1665             :                 flags &= ~XFS_QMOPT_SBVERSION;
    1666             :         }
    1667       57552 :         if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
    1668       12996 :                 error = xfs_qm_qino_alloc(mp, &pip,
    1669             :                                           flags | XFS_QMOPT_PQUOTA);
    1670       12996 :                 if (error)
    1671           0 :                         goto error_rele;
    1672             :         }
    1673             : 
    1674       57552 :         mp->m_quotainfo->qi_uquotaip = uip;
    1675       57552 :         mp->m_quotainfo->qi_gquotaip = gip;
    1676       57552 :         mp->m_quotainfo->qi_pquotaip = pip;
    1677             : 
    1678       57552 :         return 0;
    1679             : 
    1680          74 : error_rele:
    1681          74 :         if (uip)
    1682           0 :                 xfs_imeta_irele(uip);
    1683          74 :         if (gip)
    1684           0 :                 xfs_imeta_irele(gip);
    1685          74 :         if (pip)
    1686           0 :                 xfs_imeta_irele(pip);
    1687             :         return error;
    1688             : }
    1689             : 
    1690             : STATIC void
    1691       57562 : xfs_qm_destroy_quotainos(
    1692             :         struct xfs_quotainfo    *qi)
    1693             : {
    1694       57562 :         if (qi->qi_uquotaip) {
    1695          10 :                 xfs_imeta_irele(qi->qi_uquotaip);
    1696          10 :                 qi->qi_uquotaip = NULL; /* paranoia */
    1697             :         }
    1698       57562 :         if (qi->qi_gquotaip) {
    1699          10 :                 xfs_imeta_irele(qi->qi_gquotaip);
    1700          10 :                 qi->qi_gquotaip = NULL;
    1701             :         }
    1702       57562 :         if (qi->qi_pquotaip) {
    1703          10 :                 xfs_imeta_irele(qi->qi_pquotaip);
    1704          10 :                 qi->qi_pquotaip = NULL;
    1705             :         }
    1706       57562 : }
    1707             : 
    1708             : STATIC void
    1709      918409 : xfs_qm_dqfree_one(
    1710             :         struct xfs_dquot        *dqp)
    1711             : {
    1712      918409 :         struct xfs_mount        *mp = dqp->q_mount;
    1713      918409 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
    1714             : 
    1715      918409 :         mutex_lock(&qi->qi_tree_lock);
    1716      918409 :         radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
    1717             : 
    1718      918409 :         qi->qi_dquots--;
    1719      918409 :         mutex_unlock(&qi->qi_tree_lock);
    1720             : 
    1721      918409 :         xfs_qm_dqdestroy(dqp);
    1722      918409 : }
    1723             : 
    1724             : /* --------------- utility functions for vnodeops ---------------- */
    1725             : 
    1726             : 
    1727             : /*
    1728             :  * Given an inode, a uid, gid and prid make sure that we have
    1729             :  * allocated relevant dquot(s) on disk, and that we won't exceed inode
    1730             :  * quotas by creating this file.
    1731             :  * This also attaches dquot(s) to the given inode after locking it,
    1732             :  * and returns the dquots corresponding to the uid and/or gid.
    1733             :  *
    1734             :  * in   : inode (unlocked)
    1735             :  * out  : udquot, gdquot with references taken and unlocked
    1736             :  */
    1737             : int
    1738   153945188 : xfs_qm_vop_dqalloc(
    1739             :         struct xfs_inode        *ip,
    1740             :         kuid_t                  uid,
    1741             :         kgid_t                  gid,
    1742             :         prid_t                  prid,
    1743             :         uint                    flags,
    1744             :         struct xfs_dquot        **O_udqpp,
    1745             :         struct xfs_dquot        **O_gdqpp,
    1746             :         struct xfs_dquot        **O_pdqpp)
    1747             : {
    1748   153945188 :         struct xfs_mount        *mp = ip->i_mount;
    1749   153945188 :         struct inode            *inode = VFS_I(ip);
    1750   153945188 :         struct user_namespace   *user_ns = inode->i_sb->s_user_ns;
    1751   153945188 :         struct xfs_dquot        *uq = NULL;
    1752   153945188 :         struct xfs_dquot        *gq = NULL;
    1753   153945188 :         struct xfs_dquot        *pq = NULL;
    1754   153945188 :         int                     error;
    1755   153945188 :         uint                    lockflags;
    1756             : 
    1757   153945188 :         if (!XFS_IS_QUOTA_ON(mp))
    1758             :                 return 0;
    1759             : 
    1760   147824621 :         ASSERT(!xfs_is_metadir_inode(ip));
    1761             : 
    1762   147824621 :         lockflags = XFS_ILOCK_EXCL;
    1763   147824621 :         xfs_ilock(ip, lockflags);
    1764             : 
    1765   147403590 :         if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
    1766         870 :                 gid = inode->i_gid;
    1767             : 
    1768             :         /*
    1769             :          * Attach the dquot(s) to this inode, doing a dquot allocation
    1770             :          * if necessary. The dquot(s) will not be locked.
    1771             :          */
    1772   147403590 :         if (XFS_NOT_DQATTACHED(mp, ip)) {
    1773      366129 :                 error = xfs_qm_dqattach_locked(ip, true);
    1774      366136 :                 if (error) {
    1775         961 :                         xfs_iunlock(ip, lockflags);
    1776         961 :                         return error;
    1777             :                 }
    1778             :         }
    1779             : 
    1780   147402636 :         if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
    1781   146527062 :                 ASSERT(O_udqpp);
    1782   146527062 :                 if (!uid_eq(inode->i_uid, uid)) {
    1783             :                         /*
    1784             :                          * What we need is the dquot that has this uid, and
    1785             :                          * if we send the inode to dqget, the uid of the inode
    1786             :                          * takes priority over what's sent in the uid argument.
    1787             :                          * We must unlock inode here before calling dqget if
    1788             :                          * we're not sending the inode, because otherwise
    1789             :                          * we'll deadlock by doing trans_reserve while
    1790             :                          * holding ilock.
    1791             :                          */
    1792    20228435 :                         xfs_iunlock(ip, lockflags);
    1793    20238893 :                         error = xfs_qm_dqget(mp, from_kuid(user_ns, uid),
    1794             :                                         XFS_DQTYPE_USER, true, &uq);
    1795    20293544 :                         if (error) {
    1796       38876 :                                 ASSERT(error != -ENOENT);
    1797       38876 :                                 return error;
    1798             :                         }
    1799             :                         /*
    1800             :                          * Get the ilock in the right order.
    1801             :                          */
    1802    20254668 :                         xfs_dqunlock(uq);
    1803    20253977 :                         lockflags = XFS_ILOCK_SHARED;
    1804    20253977 :                         xfs_ilock(ip, lockflags);
    1805             :                 } else {
    1806             :                         /*
    1807             :                          * Take an extra reference, because we'll return
    1808             :                          * this to caller
    1809             :                          */
    1810   126298627 :                         ASSERT(ip->i_udquot);
    1811   126298627 :                         uq = xfs_qm_dqhold(ip->i_udquot);
    1812             :                 }
    1813             :         }
    1814   148631518 :         if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
    1815   147150336 :                 ASSERT(O_gdqpp);
    1816   147150336 :                 if (!gid_eq(inode->i_gid, gid)) {
    1817    20236917 :                         xfs_iunlock(ip, lockflags);
    1818    20228389 :                         error = xfs_qm_dqget(mp, from_kgid(user_ns, gid),
    1819             :                                         XFS_DQTYPE_GROUP, true, &gq);
    1820    20239621 :                         if (error) {
    1821       10161 :                                 ASSERT(error != -ENOENT);
    1822       10161 :                                 goto error_rele;
    1823             :                         }
    1824    20229460 :                         xfs_dqunlock(gq);
    1825    20229431 :                         lockflags = XFS_ILOCK_SHARED;
    1826    20229431 :                         xfs_ilock(ip, lockflags);
    1827             :                 } else {
    1828   126913419 :                         ASSERT(ip->i_gdquot);
    1829   126913419 :                         gq = xfs_qm_dqhold(ip->i_gdquot);
    1830             :                 }
    1831             :         }
    1832   148571495 :         if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
    1833   131563612 :                 ASSERT(O_pdqpp);
    1834   131563612 :                 if (ip->i_projid != prid) {
    1835      732541 :                         xfs_iunlock(ip, lockflags);
    1836      732543 :                         error = xfs_qm_dqget(mp, prid,
    1837             :                                         XFS_DQTYPE_PROJ, true, &pq);
    1838      732548 :                         if (error) {
    1839        1820 :                                 ASSERT(error != -ENOENT);
    1840        1820 :                                 goto error_rele;
    1841             :                         }
    1842      730728 :                         xfs_dqunlock(pq);
    1843      730728 :                         lockflags = XFS_ILOCK_SHARED;
    1844      730728 :                         xfs_ilock(ip, lockflags);
    1845             :                 } else {
    1846   130831071 :                         ASSERT(ip->i_pdquot);
    1847   130831071 :                         pq = xfs_qm_dqhold(ip->i_pdquot);
    1848             :                 }
    1849             :         }
    1850   148617837 :         trace_xfs_dquot_dqalloc(ip);
    1851             : 
    1852   148526657 :         xfs_iunlock(ip, lockflags);
    1853   148415902 :         if (O_udqpp)
    1854   147545430 :                 *O_udqpp = uq;
    1855             :         else
    1856      870472 :                 xfs_qm_dqrele(uq);
    1857   148415898 :         if (O_gdqpp)
    1858   147545431 :                 *O_gdqpp = gq;
    1859             :         else
    1860      870467 :                 xfs_qm_dqrele(gq);
    1861   148415900 :         if (O_pdqpp)
    1862   132117694 :                 *O_pdqpp = pq;
    1863             :         else
    1864    16298206 :                 xfs_qm_dqrele(pq);
    1865             :         return 0;
    1866             : 
    1867       11981 : error_rele:
    1868       11981 :         xfs_qm_dqrele(gq);
    1869       11981 :         xfs_qm_dqrele(uq);
    1870       11981 :         return error;
    1871             : }
    1872             : 
    1873             : /*
    1874             :  * Actually transfer ownership, and do dquot modifications.
    1875             :  * These were already reserved.
    1876             :  */
    1877             : struct xfs_dquot *
    1878    31771952 : xfs_qm_vop_chown(
    1879             :         struct xfs_trans        *tp,
    1880             :         struct xfs_inode        *ip,
    1881             :         struct xfs_dquot        **IO_olddq,
    1882             :         struct xfs_dquot        *newdq)
    1883             : {
    1884    31771952 :         struct xfs_dquot        *prevdq;
    1885    31771952 :         xfs_filblks_t           dblocks, rblocks;
    1886    31771952 :         bool                    isrt = XFS_IS_REALTIME_INODE(ip);
    1887             : 
    1888    31771952 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1889    31761089 :         ASSERT(XFS_IS_QUOTA_ON(ip->i_mount));
    1890    31761089 :         ASSERT(!xfs_is_metadir_inode(ip));
    1891             : 
    1892             :         /* old dquot */
    1893    31761089 :         prevdq = *IO_olddq;
    1894    31761089 :         ASSERT(prevdq);
    1895    31761089 :         ASSERT(prevdq != newdq);
    1896             : 
    1897    31761089 :         xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks);
    1898             : 
    1899    31767218 :         xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_BCOUNT,
    1900    31767218 :                         -(xfs_qcnt_t)dblocks);
    1901    31750295 :         xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_RTBCOUNT,
    1902    31750295 :                         -(xfs_qcnt_t)rblocks);
    1903    31763483 :         xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
    1904             : 
    1905             :         /* the sparkling new dquot */
    1906    31740509 :         xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_BCOUNT, dblocks);
    1907    31770456 :         xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_RTBCOUNT, rblocks);
    1908    31777485 :         xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_ICOUNT, 1);
    1909             : 
    1910             :         /*
    1911             :          * Back when we made quota reservations for the chown, we reserved the
    1912             :          * ondisk blocks + delalloc blocks with the new dquot.  Now that we've
    1913             :          * switched the dquots, decrease the new dquot's block reservation
    1914             :          * (having already bumped up the real counter) so that we don't have
    1915             :          * any reservation to give back when we commit.
    1916             :          */
    1917    31829385 :         xfs_trans_mod_dquot(tp, newdq,
    1918             :                         isrt ? XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
    1919    31829385 :                         -ip->i_delayed_blks);
    1920             : 
    1921             :         /*
    1922             :          * Give the incore reservation for delalloc blocks back to the old
    1923             :          * dquot.  We don't normally handle delalloc quota reservations
    1924             :          * transactionally, so just lock the dquot and subtract from the
    1925             :          * reservation.  Dirty the transaction because it's too late to turn
    1926             :          * back now.
    1927             :          */
    1928    31785653 :         tp->t_flags |= XFS_TRANS_DIRTY;
    1929    31785653 :         xfs_dqlock(prevdq);
    1930    31889503 :         if (isrt) {
    1931    12177525 :                 ASSERT(prevdq->q_rtb.reserved >= ip->i_delayed_blks);
    1932    12177525 :                 prevdq->q_rtb.reserved -= ip->i_delayed_blks;
    1933             :         } else {
    1934    19711978 :                 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks);
    1935    19711978 :                 prevdq->q_blk.reserved -= ip->i_delayed_blks;
    1936             :         }
    1937    31889503 :         xfs_dqunlock(prevdq);
    1938             : 
    1939             :         /*
    1940             :          * Take an extra reference, because the inode is going to keep
    1941             :          * this dquot pointer even after the trans_commit.
    1942             :          */
    1943    31888416 :         *IO_olddq = xfs_qm_dqhold(newdq);
    1944             : 
    1945    31878880 :         return prevdq;
    1946             : }
    1947             : 
    1948             : int
    1949    50654601 : xfs_qm_vop_rename_dqattach(
    1950             :         struct xfs_inode        **i_tab)
    1951             : {
    1952    50654601 :         struct xfs_mount        *mp = i_tab[0]->i_mount;
    1953    50654601 :         int                     i;
    1954             : 
    1955    50654601 :         if (!XFS_IS_QUOTA_ON(mp))
    1956             :                 return 0;
    1957             : 
    1958   212464109 :         for (i = 0; (i < 4 && i_tab[i]); i++) {
    1959   163734004 :                 struct xfs_inode        *ip = i_tab[i];
    1960   163734004 :                 int                     error;
    1961             : 
    1962             :                 /*
    1963             :                  * Watch out for duplicate entries in the table.
    1964             :                  */
    1965   163734004 :                 if (i == 0 || ip != i_tab[i-1]) {
    1966   160514989 :                         if (XFS_NOT_DQATTACHED(mp, ip)) {
    1967      735882 :                                 error = xfs_qm_dqattach(ip);
    1968      735887 :                                 if (error)
    1969         793 :                                         return error;
    1970             :                         }
    1971             :                 }
    1972             :         }
    1973             :         return 0;
    1974             : }
    1975             : 
    1976             : void
    1977   123354008 : xfs_qm_vop_create_dqattach(
    1978             :         struct xfs_trans        *tp,
    1979             :         struct xfs_inode        *ip,
    1980             :         struct xfs_dquot        *udqp,
    1981             :         struct xfs_dquot        *gdqp,
    1982             :         struct xfs_dquot        *pdqp)
    1983             : {
    1984   123354008 :         struct xfs_mount        *mp = tp->t_mountp;
    1985             : 
    1986   123354008 :         if (!XFS_IS_QUOTA_ON(mp))
    1987             :                 return;
    1988             : 
    1989   117216262 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1990   116849718 :         ASSERT(!xfs_is_metadir_inode(ip));
    1991             : 
    1992   116849718 :         if (udqp && XFS_IS_UQUOTA_ON(mp)) {
    1993   116954154 :                 ASSERT(ip->i_udquot == NULL);
    1994   116954154 :                 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id);
    1995             : 
    1996   116999173 :                 ip->i_udquot = xfs_qm_dqhold(udqp);
    1997             :         }
    1998   117980354 :         if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
    1999   117462345 :                 ASSERT(ip->i_gdquot == NULL);
    2000   117462345 :                 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id);
    2001             : 
    2002   117450709 :                 ip->i_gdquot = xfs_qm_dqhold(gdqp);
    2003             :         }
    2004   117979815 :         if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
    2005   117425737 :                 ASSERT(ip->i_pdquot == NULL);
    2006   117425737 :                 ASSERT(ip->i_projid == pdqp->q_id);
    2007             : 
    2008   117425737 :                 ip->i_pdquot = xfs_qm_dqhold(pdqp);
    2009             :         }
    2010             : 
    2011   117983948 :         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, 1);
    2012             : }
    2013             : 
    2014             : /* Decide if this inode's dquot is near an enforcement boundary. */
    2015             : bool
    2016   178934196 : xfs_inode_near_dquot_enforcement(
    2017             :         struct xfs_inode        *ip,
    2018             :         xfs_dqtype_t            type)
    2019             : {
    2020   178934196 :         struct xfs_dquot        *dqp;
    2021   178934196 :         int64_t                 freesp;
    2022             : 
    2023             :         /* We only care for quotas that are enabled and enforced. */
    2024   178934196 :         dqp = xfs_inode_dquot(ip, type);
    2025   178934196 :         if (!dqp || !xfs_dquot_is_enforced(dqp))
    2026    10438149 :                 return false;
    2027             : 
    2028   168463143 :         if (xfs_dquot_res_over_limits(&dqp->q_ino) ||
    2029   168462310 :             xfs_dquot_res_over_limits(&dqp->q_rtb))
    2030             :                 return true;
    2031             : 
    2032             :         /* For space on the data device, check the various thresholds. */
    2033   168462284 :         if (!dqp->q_prealloc_hi_wmark)
    2034             :                 return false;
    2035             : 
    2036       50792 :         if (dqp->q_blk.reserved < dqp->q_prealloc_lo_wmark)
    2037             :                 return false;
    2038             : 
    2039         575 :         if (dqp->q_blk.reserved >= dqp->q_prealloc_hi_wmark)
    2040             :                 return true;
    2041             : 
    2042         440 :         freesp = dqp->q_prealloc_hi_wmark - dqp->q_blk.reserved;
    2043         440 :         if (freesp < dqp->q_low_space[XFS_QLOWSP_5_PCNT])
    2044           0 :                 return true;
    2045             : 
    2046             :         return false;
    2047             : }

Generated by: LCOV version 1.14