LCOV - code coverage report
Current view: top level - fs/xfs - xfs_dquot.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 580 630 92.1 %
Date: 2023-07-31 20:08:07 Functions: 38 38 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_format.h"
       9             : #include "xfs_log_format.h"
      10             : #include "xfs_shared.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_mount.h"
      14             : #include "xfs_defer.h"
      15             : #include "xfs_inode.h"
      16             : #include "xfs_bmap.h"
      17             : #include "xfs_quota.h"
      18             : #include "xfs_trans.h"
      19             : #include "xfs_buf_item.h"
      20             : #include "xfs_trans_space.h"
      21             : #include "xfs_trans_priv.h"
      22             : #include "xfs_qm.h"
      23             : #include "xfs_trace.h"
      24             : #include "xfs_log.h"
      25             : #include "xfs_bmap_btree.h"
      26             : #include "xfs_error.h"
      27             : #include "xfs_health.h"
      28             : 
      29             : /*
      30             :  * Lock order:
      31             :  *
      32             :  * ip->i_lock
      33             :  *   qi->qi_tree_lock
      34             :  *     dquot->q_qlock (xfs_dqlock() and friends)
      35             :  *       dquot->q_flush (xfs_dqflock() and friends)
      36             :  *       qi->qi_lru_lock
      37             :  *
      38             :  * If two dquots need to be locked the order is user before group/project,
      39             :  * otherwise by the lowest id first, see xfs_dqlock2.
      40             :  */
      41             : 
      42             : struct kmem_cache               *xfs_dqtrx_cache;
      43             : static struct kmem_cache        *xfs_dquot_cache;
      44             : 
      45             : static struct lock_class_key xfs_dquot_group_class;
      46             : static struct lock_class_key xfs_dquot_project_class;
      47             : 
      48             : /* Record observations of quota corruption with the health tracking system. */
      49             : static void
      50           6 : xfs_dquot_mark_sick(
      51             :         struct xfs_dquot        *dqp)
      52             : {
      53           6 :         struct xfs_mount        *mp = dqp->q_mount;
      54             : 
      55           6 :         switch (dqp->q_type) {
      56           2 :         case XFS_DQTYPE_USER:
      57           2 :                 xfs_fs_mark_sick(mp, XFS_SICK_FS_UQUOTA);
      58           2 :                 break;
      59           2 :         case XFS_DQTYPE_GROUP:
      60           2 :                 xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA);
      61           2 :                 break;
      62           2 :         case XFS_DQTYPE_PROJ:
      63           2 :                 xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA);
      64           2 :                 break;
      65           0 :         default:
      66           0 :                 ASSERT(0);
      67           0 :                 break;
      68             :         }
      69           6 : }
      70             : 
      71             : /*
      72             :  * This is called to free all the memory associated with a dquot
      73             :  */
      74             : void
      75    12719617 : xfs_qm_dqdestroy(
      76             :         struct xfs_dquot        *dqp)
      77             : {
      78    12719617 :         ASSERT(list_empty(&dqp->q_lru));
      79             : 
      80    12719617 :         kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
      81    12719566 :         mutex_destroy(&dqp->q_qlock);
      82             : 
      83    12719580 :         XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
      84    12719580 :         kmem_cache_free(xfs_dquot_cache, dqp);
      85    12719644 : }
      86             : 
      87             : /*
      88             :  * If default limits are in force, push them into the dquot now.
      89             :  * We overwrite the dquot limits only if they are zero and this
      90             :  * is not the root dquot.
      91             :  */
      92             : void
      93   146943950 : xfs_qm_adjust_dqlimits(
      94             :         struct xfs_dquot        *dq)
      95             : {
      96   146943950 :         struct xfs_mount        *mp = dq->q_mount;
      97   146943950 :         struct xfs_quotainfo    *q = mp->m_quotainfo;
      98   146943950 :         struct xfs_def_quota    *defq;
      99   146943950 :         int                     prealloc = 0;
     100             : 
     101   146943950 :         ASSERT(dq->q_id);
     102   146943950 :         defq = xfs_get_defquota(q, xfs_dquot_type(dq));
     103             : 
     104   146945101 :         if (!dq->q_blk.softlimit) {
     105   146801460 :                 dq->q_blk.softlimit = defq->blk.soft;
     106   146801460 :                 prealloc = 1;
     107             :         }
     108   146945101 :         if (!dq->q_blk.hardlimit) {
     109   146799184 :                 dq->q_blk.hardlimit = defq->blk.hard;
     110   146799184 :                 prealloc = 1;
     111             :         }
     112   146945101 :         if (!dq->q_ino.softlimit)
     113   146806961 :                 dq->q_ino.softlimit = defq->ino.soft;
     114   146945101 :         if (!dq->q_ino.hardlimit)
     115   146807866 :                 dq->q_ino.hardlimit = defq->ino.hard;
     116   146945101 :         if (!dq->q_rtb.softlimit)
     117   146945954 :                 dq->q_rtb.softlimit = defq->rtb.soft;
     118   146945101 :         if (!dq->q_rtb.hardlimit)
     119   146946085 :                 dq->q_rtb.hardlimit = defq->rtb.hard;
     120             : 
     121   146945101 :         if (prealloc)
     122   146802063 :                 xfs_dquot_set_prealloc_limits(dq);
     123   146945143 : }
     124             : 
     125             : /* Set the expiration time of a quota's grace period. */
     126             : time64_t
     127          74 : xfs_dquot_set_timeout(
     128             :         struct xfs_mount        *mp,
     129             :         time64_t                timeout)
     130             : {
     131         305 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     132             : 
     133         305 :         return clamp_t(time64_t, timeout, qi->qi_expiry_min,
     134             :                                           qi->qi_expiry_max);
     135             : }
     136             : 
     137             : /* Set the length of the default grace period. */
     138             : time64_t
     139        5609 : xfs_dquot_set_grace_period(
     140             :         time64_t                grace)
     141             : {
     142        5609 :         return clamp_t(time64_t, grace, XFS_DQ_GRACE_MIN, XFS_DQ_GRACE_MAX);
     143             : }
     144             : 
     145             : /*
     146             :  * Determine if this quota counter is over either limit and set the quota
     147             :  * timers as appropriate.
     148             :  */
     149             : static inline void
     150   440892367 : xfs_qm_adjust_res_timer(
     151             :         struct xfs_mount        *mp,
     152             :         struct xfs_dquot_res    *res,
     153             :         struct xfs_quota_limits *qlim)
     154             : {
     155   440892367 :         ASSERT(res->hardlimit == 0 || res->softlimit <= res->hardlimit);
     156             : 
     157   440892367 :         if ((res->softlimit && res->count > res->softlimit) ||
     158   440883096 :             (res->hardlimit && res->count > res->hardlimit)) {
     159        9427 :                 if (res->timer == 0)
     160         231 :                         res->timer = xfs_dquot_set_timeout(mp,
     161         231 :                                         ktime_get_real_seconds() + qlim->time);
     162             :         } else {
     163   440882940 :                 res->timer = 0;
     164             :         }
     165   440892367 : }
     166             : 
     167             : /*
     168             :  * Check the limits and timers of a dquot and start or reset timers
     169             :  * if necessary.
     170             :  * This gets called even when quota enforcement is OFF, which makes our
     171             :  * life a little less complicated. (We just don't reject any quota
     172             :  * reservations in that case, when enforcement is off).
     173             :  * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
     174             :  * enforcement's off.
     175             :  * In contrast, warnings are a little different in that they don't
     176             :  * 'automatically' get started when limits get exceeded.  They do
     177             :  * get reset to zero, however, when we find the count to be under
     178             :  * the soft limit (they are only ever set non-zero via userspace).
     179             :  */
     180             : void
     181   146964374 : xfs_qm_adjust_dqtimers(
     182             :         struct xfs_dquot        *dq)
     183             : {
     184   146964374 :         struct xfs_mount        *mp = dq->q_mount;
     185   146964374 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     186   146964374 :         struct xfs_def_quota    *defq;
     187             : 
     188   146964374 :         ASSERT(dq->q_id);
     189   146964374 :         defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
     190             : 
     191   146964739 :         xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_blk, &defq->blk);
     192   146965366 :         xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_ino, &defq->ino);
     193   146965825 :         xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_rtb, &defq->rtb);
     194   146965317 : }
     195             : 
     196             : /*
     197             :  * initialize a buffer full of dquots and log the whole thing
     198             :  */
     199             : STATIC void
     200     2074617 : xfs_qm_init_dquot_blk(
     201             :         struct xfs_trans        *tp,
     202             :         struct xfs_mount        *mp,
     203             :         xfs_dqid_t              id,
     204             :         xfs_dqtype_t            type,
     205             :         struct xfs_buf          *bp)
     206             : {
     207     2074617 :         struct xfs_quotainfo    *q = mp->m_quotainfo;
     208     2074617 :         struct xfs_dqblk        *d;
     209     2074617 :         xfs_dqid_t              curid;
     210     2074617 :         unsigned int            qflag;
     211     2074617 :         unsigned int            blftype;
     212     2074617 :         int                     i;
     213             : 
     214     2074617 :         ASSERT(tp);
     215     2074617 :         ASSERT(xfs_buf_islocked(bp));
     216             : 
     217     2074617 :         switch (type) {
     218             :         case XFS_DQTYPE_USER:
     219             :                 qflag = XFS_UQUOTA_CHKD;
     220             :                 blftype = XFS_BLF_UDQUOT_BUF;
     221             :                 break;
     222       36128 :         case XFS_DQTYPE_PROJ:
     223       36128 :                 qflag = XFS_PQUOTA_CHKD;
     224       36128 :                 blftype = XFS_BLF_PDQUOT_BUF;
     225       36128 :                 break;
     226     1017588 :         case XFS_DQTYPE_GROUP:
     227     1017588 :                 qflag = XFS_GQUOTA_CHKD;
     228     1017588 :                 blftype = XFS_BLF_GDQUOT_BUF;
     229     1017588 :                 break;
     230           0 :         default:
     231           0 :                 ASSERT(0);
     232           0 :                 return;
     233             :         }
     234             : 
     235     2074617 :         d = bp->b_addr;
     236             : 
     237             :         /*
     238             :          * ID of the first dquot in the block - id's are zero based.
     239             :          */
     240     2074617 :         curid = id - (id % q->qi_dqperchunk);
     241     2074617 :         memset(d, 0, BBTOB(q->qi_dqchunklen));
     242    70464072 :         for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
     243    68389479 :                 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
     244    68389479 :                 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
     245    68389479 :                 d->dd_diskdq.d_id = cpu_to_be32(curid);
     246    68389479 :                 d->dd_diskdq.d_type = type;
     247    68389479 :                 if (curid > 0 && xfs_has_bigtime(mp))
     248    68240822 :                         d->dd_diskdq.d_type |= XFS_DQTYPE_BIGTIME;
     249    68389479 :                 if (xfs_has_crc(mp)) {
     250    68386709 :                         uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
     251    68386696 :                         xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
     252             :                                          XFS_DQUOT_CRC_OFF);
     253             :                 }
     254             :         }
     255             : 
     256     2074593 :         xfs_trans_dquot_buf(tp, bp, blftype);
     257             : 
     258             :         /*
     259             :          * quotacheck uses delayed writes to update all the dquots on disk in an
     260             :          * efficient manner instead of logging the individual dquot changes as
     261             :          * they are made. However if we log the buffer allocated here and crash
     262             :          * after quotacheck while the logged initialisation is still in the
     263             :          * active region of the log, log recovery can replay the dquot buffer
     264             :          * initialisation over the top of the checked dquots and corrupt quota
     265             :          * accounting.
     266             :          *
     267             :          * To avoid this problem, quotacheck cannot log the initialised buffer.
     268             :          * We must still dirty the buffer and write it back before the
     269             :          * allocation transaction clears the log. Therefore, mark the buffer as
     270             :          * ordered instead of logging it directly. This is safe for quotacheck
     271             :          * because it detects and repairs allocated but initialized dquot blocks
     272             :          * in the quota inodes.
     273             :          */
     274     2074573 :         if (!(mp->m_qflags & qflag))
     275       20768 :                 xfs_trans_ordered_buf(tp, bp);
     276             :         else
     277     2053805 :                 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
     278             : }
     279             : 
     280             : /*
     281             :  * Initialize the dynamic speculative preallocation thresholds. The lo/hi
     282             :  * watermarks correspond to the soft and hard limits by default. If a soft limit
     283             :  * is not specified, we use 95% of the hard limit.
     284             :  */
     285             : void
     286   159451265 : xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
     287             : {
     288   159451265 :         uint64_t space;
     289             : 
     290   159451265 :         dqp->q_prealloc_hi_wmark = dqp->q_blk.hardlimit;
     291   159451265 :         dqp->q_prealloc_lo_wmark = dqp->q_blk.softlimit;
     292   159451265 :         if (!dqp->q_prealloc_lo_wmark) {
     293   159441345 :                 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
     294   159441345 :                 do_div(dqp->q_prealloc_lo_wmark, 100);
     295   159441345 :                 dqp->q_prealloc_lo_wmark *= 95;
     296             :         }
     297             : 
     298   159451265 :         space = dqp->q_prealloc_hi_wmark;
     299             : 
     300   159451265 :         do_div(space, 100);
     301   159451265 :         dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
     302   159451265 :         dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
     303   159451265 :         dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
     304   159451265 : }
     305             : 
     306             : /*
     307             :  * Ensure that the given in-core dquot has a buffer on disk backing it, and
     308             :  * return the buffer locked and held. This is called when the bmapi finds a
     309             :  * hole.
     310             :  */
     311             : STATIC int
     312     2092439 : xfs_dquot_disk_alloc(
     313             :         struct xfs_dquot        *dqp,
     314             :         struct xfs_buf          **bpp)
     315             : {
     316     2092439 :         struct xfs_bmbt_irec    map;
     317     2092439 :         struct xfs_trans        *tp;
     318     2092439 :         struct xfs_mount        *mp = dqp->q_mount;
     319     2092439 :         struct xfs_buf          *bp;
     320     2092439 :         xfs_dqtype_t            qtype = xfs_dquot_type(dqp);
     321     2092439 :         struct xfs_inode        *quotip = xfs_quota_inode(mp, qtype);
     322     2092421 :         int                     nmaps = 1;
     323     2092421 :         int                     error;
     324             : 
     325     2092421 :         trace_xfs_dqalloc(dqp);
     326             : 
     327     2092369 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
     328     2092369 :                         XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
     329     2092364 :         if (error)
     330             :                 return error;
     331             : 
     332     2076125 :         xfs_ilock(quotip, XFS_ILOCK_EXCL);
     333     2076170 :         xfs_trans_ijoin(tp, quotip, 0);
     334             : 
     335     2076122 :         if (!xfs_this_quota_on(dqp->q_mount, qtype)) {
     336             :                 /*
     337             :                  * Return if this type of quotas is turned off while we didn't
     338             :                  * have an inode lock
     339             :                  */
     340           0 :                 error = -ESRCH;
     341           0 :                 goto err_cancel;
     342             :         }
     343             : 
     344     2076122 :         error = xfs_iext_count_may_overflow(quotip, XFS_DATA_FORK,
     345             :                         XFS_IEXT_ADD_NOSPLIT_CNT);
     346     2076105 :         if (error == -EFBIG)
     347           2 :                 error = xfs_iext_count_upgrade(tp, quotip,
     348             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
     349     2076105 :         if (error)
     350           2 :                 goto err_cancel;
     351             : 
     352             :         /* Create the block mapping. */
     353     2076103 :         error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
     354             :                         XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
     355             :                         &nmaps);
     356     2076206 :         if (error)
     357        1611 :                 goto err_cancel;
     358             : 
     359     2074595 :         ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
     360     2074595 :         ASSERT(nmaps == 1);
     361     2074595 :         ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
     362             :                (map.br_startblock != HOLESTARTBLOCK));
     363             : 
     364             :         /*
     365             :          * Keep track of the blkno to save a lookup later
     366             :          */
     367     2074595 :         dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
     368             : 
     369             :         /* now we can just get the buffer (there's nothing to read yet) */
     370     2074595 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
     371     2074595 :                         mp->m_quotainfo->qi_dqchunklen, 0, &bp);
     372     2074591 :         if (error)
     373           0 :                 goto err_cancel;
     374     2074591 :         bp->b_ops = &xfs_dquot_buf_ops;
     375             : 
     376             :         /*
     377             :          * Make a chunk of dquots out of this buffer and log
     378             :          * the entire thing.
     379             :          */
     380     2074591 :         xfs_qm_init_dquot_blk(tp, mp, dqp->q_id, qtype, bp);
     381     2074602 :         xfs_buf_set_ref(bp, XFS_DQUOT_REF);
     382             : 
     383             :         /*
     384             :          * Hold the buffer and join it to the dfops so that we'll still own
     385             :          * the buffer when we return to the caller.  The buffer disposal on
     386             :          * error must be paid attention to very carefully, as it has been
     387             :          * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
     388             :          * code when allocating a new dquot record" in 2005, and the later
     389             :          * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
     390             :          * the buffer locked across the _defer_finish call.  We can now do
     391             :          * this correctly with xfs_defer_bjoin.
     392             :          *
     393             :          * Above, we allocated a disk block for the dquot information and used
     394             :          * get_buf to initialize the dquot. If the _defer_finish fails, the old
     395             :          * transaction is gone but the new buffer is not joined or held to any
     396             :          * transaction, so we must _buf_relse it.
     397             :          *
     398             :          * If everything succeeds, the caller of this function is returned a
     399             :          * buffer that is locked and held to the transaction.  The caller
     400             :          * is responsible for unlocking any buffer passed back, either
     401             :          * manually or by committing the transaction.  On error, the buffer is
     402             :          * released and not passed back.
     403             :          *
     404             :          * Keep the quota inode ILOCKed until after the transaction commit to
     405             :          * maintain the atomicity of bmap/rmap updates.
     406             :          */
     407     2074599 :         xfs_trans_bhold(tp, bp);
     408     2074547 :         error = xfs_trans_commit(tp);
     409     2074614 :         xfs_iunlock(quotip, XFS_ILOCK_EXCL);
     410     2074603 :         if (error) {
     411         104 :                 xfs_buf_relse(bp);
     412         104 :                 return error;
     413             :         }
     414             : 
     415     2074499 :         *bpp = bp;
     416     2074499 :         return 0;
     417             : 
     418        1613 : err_cancel:
     419        1613 :         xfs_trans_cancel(tp);
     420        1613 :         xfs_iunlock(quotip, XFS_ILOCK_EXCL);
     421        1613 :         return error;
     422             : }
     423             : 
     424             : /*
     425             :  * Read in the in-core dquot's on-disk metadata and return the buffer.
     426             :  * Returns ENOENT to signal a hole.
     427             :  */
     428             : STATIC int
     429    12719559 : xfs_dquot_disk_read(
     430             :         struct xfs_mount        *mp,
     431             :         struct xfs_dquot        *dqp,
     432             :         struct xfs_buf          **bpp)
     433             : {
     434    12719559 :         struct xfs_bmbt_irec    map;
     435    12719559 :         struct xfs_buf          *bp;
     436    12719559 :         xfs_dqtype_t            qtype = xfs_dquot_type(dqp);
     437    12719559 :         struct xfs_inode        *quotip = xfs_quota_inode(mp, qtype);
     438    12719518 :         uint                    lock_mode;
     439    12719518 :         int                     nmaps = 1;
     440    12719518 :         int                     error;
     441             : 
     442    12719518 :         lock_mode = xfs_ilock_data_map_shared(quotip);
     443    12719375 :         if (!xfs_this_quota_on(mp, qtype)) {
     444             :                 /*
     445             :                  * Return if this type of quotas is turned off while we
     446             :                  * didn't have the quota inode lock.
     447             :                  */
     448           0 :                 xfs_iunlock(quotip, lock_mode);
     449           0 :                 return -ESRCH;
     450             :         }
     451             : 
     452             :         /*
     453             :          * Find the block map; no allocations yet
     454             :          */
     455    12719375 :         error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
     456             :                         XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
     457    12719368 :         xfs_iunlock(quotip, lock_mode);
     458    12719517 :         if (error)
     459             :                 return error;
     460             : 
     461    12718884 :         ASSERT(nmaps == 1);
     462    12718884 :         ASSERT(map.br_blockcount >= 1);
     463    12718884 :         ASSERT(map.br_startblock != DELAYSTARTBLOCK);
     464    12718884 :         if (map.br_startblock == HOLESTARTBLOCK)
     465             :                 return -ENOENT;
     466             : 
     467    10565778 :         trace_xfs_dqtobp_read(dqp);
     468             : 
     469             :         /*
     470             :          * store the blkno etc so that we don't have to do the
     471             :          * mapping all the time
     472             :          */
     473    10565777 :         dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
     474             : 
     475    10565777 :         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
     476    10565777 :                         mp->m_quotainfo->qi_dqchunklen, 0, &bp,
     477             :                         &xfs_dquot_buf_ops);
     478    10565717 :         if (xfs_metadata_is_sick(error))
     479           6 :                 xfs_dquot_mark_sick(dqp);
     480    10565717 :         if (error) {
     481        9639 :                 ASSERT(bp == NULL);
     482        9639 :                 return error;
     483             :         }
     484             : 
     485    10556078 :         ASSERT(xfs_buf_islocked(bp));
     486    10556078 :         xfs_buf_set_ref(bp, XFS_DQUOT_REF);
     487    10555873 :         *bpp = bp;
     488             : 
     489    10555873 :         return 0;
     490             : }
     491             : 
     492             : /* Allocate and initialize everything we need for an incore dquot. */
     493             : STATIC struct xfs_dquot *
     494    12719615 : xfs_dquot_alloc(
     495             :         struct xfs_mount        *mp,
     496             :         xfs_dqid_t              id,
     497             :         xfs_dqtype_t            type)
     498             : {
     499    12719615 :         struct xfs_dquot        *dqp;
     500             : 
     501    12719615 :         dqp = kmem_cache_zalloc(xfs_dquot_cache, GFP_KERNEL | __GFP_NOFAIL);
     502             : 
     503    12719585 :         dqp->q_type = type;
     504    12719585 :         dqp->q_id = id;
     505    12719585 :         dqp->q_mount = mp;
     506    12719585 :         INIT_LIST_HEAD(&dqp->q_lru);
     507    12719585 :         mutex_init(&dqp->q_qlock);
     508    12719604 :         init_waitqueue_head(&dqp->q_pinwait);
     509    12719615 :         dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
     510             :         /*
     511             :          * Offset of dquot in the (fixed sized) dquot chunk.
     512             :          */
     513    12719615 :         dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
     514             :                         sizeof(struct xfs_dqblk);
     515             : 
     516             :         /*
     517             :          * Because we want to use a counting completion, complete
     518             :          * the flush completion once to allow a single access to
     519             :          * the flush completion without blocking.
     520             :          */
     521    12719615 :         init_completion(&dqp->q_flush);
     522    12719611 :         complete(&dqp->q_flush);
     523             : 
     524             :         /*
     525             :          * Make sure group quotas have a different lock class than user
     526             :          * quotas.
     527             :          */
     528    12719586 :         switch (type) {
     529             :         case XFS_DQTYPE_USER:
     530             :                 /* uses the default lock class */
     531             :                 break;
     532             :         case XFS_DQTYPE_GROUP:
     533             :                 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
     534             :                 break;
     535             :         case XFS_DQTYPE_PROJ:
     536             :                 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
     537             :                 break;
     538           0 :         default:
     539           0 :                 ASSERT(0);
     540           0 :                 break;
     541             :         }
     542             : 
     543    12719586 :         xfs_qm_dquot_logitem_init(dqp);
     544             : 
     545    12719577 :         XFS_STATS_INC(mp, xs_qm_dquot);
     546    12719577 :         return dqp;
     547             : }
     548             : 
     549             : /* Check the ondisk dquot's id and type match what the incore dquot expects. */
     550             : static bool
     551    12630307 : xfs_dquot_check_type(
     552             :         struct xfs_dquot        *dqp,
     553             :         struct xfs_disk_dquot   *ddqp)
     554             : {
     555    12630307 :         uint8_t                 ddqp_type;
     556    12630307 :         uint8_t                 dqp_type;
     557             : 
     558    12630307 :         ddqp_type = ddqp->d_type & XFS_DQTYPE_REC_MASK;
     559    12630307 :         dqp_type = xfs_dquot_type(dqp);
     560             : 
     561    25260614 :         if (be32_to_cpu(ddqp->d_id) != dqp->q_id)
     562             :                 return false;
     563             : 
     564             :         /*
     565             :          * V5 filesystems always expect an exact type match.  V4 filesystems
     566             :          * expect an exact match for user dquots and for non-root group and
     567             :          * project dquots.
     568             :          */
     569    12630588 :         if (xfs_has_crc(dqp->q_mount) ||
     570          30 :             dqp_type == XFS_DQTYPE_USER || dqp->q_id != 0)
     571    12630558 :                 return ddqp_type == dqp_type;
     572             : 
     573             :         /*
     574             :          * V4 filesystems support either group or project quotas, but not both
     575             :          * at the same time.  The non-user quota file can be switched between
     576             :          * group and project quota uses depending on the mount options, which
     577             :          * means that we can encounter the other type when we try to load quota
     578             :          * defaults.  Quotacheck will soon reset the entire quota file
     579             :          * (including the root dquot) anyway, but don't log scary corruption
     580             :          * reports to dmesg.
     581             :          */
     582          30 :         return ddqp_type == XFS_DQTYPE_GROUP || ddqp_type == XFS_DQTYPE_PROJ;
     583             : }
     584             : 
     585             : /* Copy the in-core quota fields in from the on-disk buffer. */
     586             : STATIC int
     587    12630602 : xfs_dquot_from_disk(
     588             :         struct xfs_dquot        *dqp,
     589             :         struct xfs_buf          *bp)
     590             : {
     591    12630602 :         struct xfs_disk_dquot   *ddqp = bp->b_addr + dqp->q_bufoffset;
     592             : 
     593             :         /*
     594             :          * Ensure that we got the type and ID we were looking for.
     595             :          * Everything else was checked by the dquot buffer verifier.
     596             :          */
     597    12630602 :         if (!xfs_dquot_check_type(dqp, ddqp)) {
     598           0 :                 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR,
     599             :                           "Metadata corruption detected at %pS, quota %u",
     600             :                           __this_address, dqp->q_id);
     601           0 :                 xfs_alert(bp->b_mount, "Unmount and run xfs_repair");
     602           0 :                 xfs_dquot_mark_sick(dqp);
     603           0 :                 return -EFSCORRUPTED;
     604             :         }
     605             : 
     606             :         /* copy everything from disk dquot to the incore dquot */
     607    12630602 :         dqp->q_type = ddqp->d_type;
     608    12630602 :         dqp->q_blk.hardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
     609    12630602 :         dqp->q_blk.softlimit = be64_to_cpu(ddqp->d_blk_softlimit);
     610    12630602 :         dqp->q_ino.hardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
     611    12630602 :         dqp->q_ino.softlimit = be64_to_cpu(ddqp->d_ino_softlimit);
     612    12630602 :         dqp->q_rtb.hardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
     613    12630602 :         dqp->q_rtb.softlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
     614             : 
     615    12630602 :         dqp->q_blk.count = be64_to_cpu(ddqp->d_bcount);
     616    12630602 :         dqp->q_ino.count = be64_to_cpu(ddqp->d_icount);
     617    12630602 :         dqp->q_rtb.count = be64_to_cpu(ddqp->d_rtbcount);
     618             : 
     619    12630602 :         dqp->q_blk.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_btimer);
     620    12630503 :         dqp->q_ino.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_itimer);
     621    12630509 :         dqp->q_rtb.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_rtbtimer);
     622             : 
     623             :         /*
     624             :          * Reservation counters are defined as reservation plus current usage
     625             :          * to avoid having to add every time.
     626             :          */
     627    12630458 :         dqp->q_blk.reserved = dqp->q_blk.count;
     628    12630458 :         dqp->q_ino.reserved = dqp->q_ino.count;
     629    12630458 :         dqp->q_rtb.reserved = dqp->q_rtb.count;
     630             : 
     631             :         /* initialize the dquot speculative prealloc thresholds */
     632    12630458 :         xfs_dquot_set_prealloc_limits(dqp);
     633    12630458 :         return 0;
     634             : }
     635             : 
     636             : /* Copy the in-core quota fields into the on-disk buffer. */
     637             : void
     638  2614936105 : xfs_dquot_to_disk(
     639             :         struct xfs_disk_dquot   *ddqp,
     640             :         struct xfs_dquot        *dqp)
     641             : {
     642  2614936105 :         ddqp->d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
     643  2614936105 :         ddqp->d_version = XFS_DQUOT_VERSION;
     644  2614936105 :         ddqp->d_type = dqp->q_type;
     645  2614936105 :         ddqp->d_id = cpu_to_be32(dqp->q_id);
     646  2614936105 :         ddqp->d_pad0 = 0;
     647  2614936105 :         ddqp->d_pad = 0;
     648             : 
     649  2614936105 :         ddqp->d_blk_hardlimit = cpu_to_be64(dqp->q_blk.hardlimit);
     650  2614936105 :         ddqp->d_blk_softlimit = cpu_to_be64(dqp->q_blk.softlimit);
     651  2614936105 :         ddqp->d_ino_hardlimit = cpu_to_be64(dqp->q_ino.hardlimit);
     652  2614936105 :         ddqp->d_ino_softlimit = cpu_to_be64(dqp->q_ino.softlimit);
     653  2614936105 :         ddqp->d_rtb_hardlimit = cpu_to_be64(dqp->q_rtb.hardlimit);
     654  2614936105 :         ddqp->d_rtb_softlimit = cpu_to_be64(dqp->q_rtb.softlimit);
     655             : 
     656  2614936105 :         ddqp->d_bcount = cpu_to_be64(dqp->q_blk.count);
     657  2614936105 :         ddqp->d_icount = cpu_to_be64(dqp->q_ino.count);
     658  2614936105 :         ddqp->d_rtbcount = cpu_to_be64(dqp->q_rtb.count);
     659             : 
     660  2614936105 :         ddqp->d_bwarns = 0;
     661  2614936105 :         ddqp->d_iwarns = 0;
     662  2614936105 :         ddqp->d_rtbwarns = 0;
     663             : 
     664  2614936105 :         ddqp->d_btimer = xfs_dquot_to_disk_ts(dqp, dqp->q_blk.timer);
     665  2614936748 :         ddqp->d_itimer = xfs_dquot_to_disk_ts(dqp, dqp->q_ino.timer);
     666  2614940013 :         ddqp->d_rtbtimer = xfs_dquot_to_disk_ts(dqp, dqp->q_rtb.timer);
     667  2614942387 : }
     668             : 
     669             : /*
     670             :  * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
     671             :  * and release the buffer immediately.  If @can_alloc is true, fill any
     672             :  * holes in the on-disk metadata.
     673             :  */
     674             : static int
     675    12719636 : xfs_qm_dqread(
     676             :         struct xfs_mount        *mp,
     677             :         xfs_dqid_t              id,
     678             :         xfs_dqtype_t            type,
     679             :         bool                    can_alloc,
     680             :         struct xfs_dquot        **dqpp)
     681             : {
     682    12719636 :         struct xfs_dquot        *dqp;
     683    12719636 :         struct xfs_buf          *bp;
     684    12719636 :         int                     error;
     685             : 
     686    12719636 :         dqp = xfs_dquot_alloc(mp, id, type);
     687    12719583 :         trace_xfs_dqread(dqp);
     688             : 
     689             :         /* Try to read the buffer, allocating if necessary. */
     690    12719561 :         error = xfs_dquot_disk_read(mp, dqp, &bp);
     691    12719345 :         if (error == -ENOENT && can_alloc)
     692     2092441 :                 error = xfs_dquot_disk_alloc(dqp, &bp);
     693    12719372 :         if (error)
     694       88907 :                 goto err;
     695             : 
     696             :         /*
     697             :          * At this point we should have a clean locked buffer.  Copy the data
     698             :          * to the incore dquot and release the buffer since the incore dquot
     699             :          * has its own locking protocol so we needn't tie up the buffer any
     700             :          * further.
     701             :          */
     702    12630465 :         ASSERT(xfs_buf_islocked(bp));
     703    12630465 :         error = xfs_dquot_from_disk(dqp, bp);
     704    12630487 :         xfs_buf_relse(bp);
     705    12630680 :         if (error)
     706           0 :                 goto err;
     707             : 
     708    12630680 :         *dqpp = dqp;
     709    12630680 :         return error;
     710             : 
     711       88907 : err:
     712       88907 :         trace_xfs_dqread_fail(dqp);
     713       88885 :         xfs_qm_dqdestroy(dqp);
     714       88882 :         *dqpp = NULL;
     715       88882 :         return error;
     716             : }
     717             : 
     718             : /*
     719             :  * Advance to the next id in the current chunk, or if at the
     720             :  * end of the chunk, skip ahead to first id in next allocated chunk
     721             :  * using the SEEK_DATA interface.
     722             :  */
     723             : static int
     724     5938859 : xfs_dq_get_next_id(
     725             :         struct xfs_mount        *mp,
     726             :         xfs_dqtype_t            type,
     727             :         xfs_dqid_t              *id)
     728             : {
     729     5938859 :         struct xfs_inode        *quotip = xfs_quota_inode(mp, type);
     730     5938859 :         xfs_dqid_t              next_id = *id + 1; /* simple advance */
     731     5938859 :         uint                    lock_flags;
     732     5938859 :         struct xfs_bmbt_irec    got;
     733     5938859 :         struct xfs_iext_cursor  cur;
     734     5938859 :         xfs_fsblock_t           start;
     735     5938859 :         int                     error = 0;
     736             : 
     737             :         /* If we'd wrap past the max ID, stop */
     738     5938859 :         if (next_id < *id)
     739             :                 return -ENOENT;
     740             : 
     741             :         /* If new ID is within the current chunk, advancing it sufficed */
     742     5938851 :         if (next_id % mp->m_quotainfo->qi_dqperchunk) {
     743     5806721 :                 *id = next_id;
     744     5806721 :                 return 0;
     745             :         }
     746             : 
     747             :         /* Nope, next_id is now past the current chunk, so find the next one */
     748      132130 :         start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
     749             : 
     750      132130 :         lock_flags = xfs_ilock_data_map_shared(quotip);
     751      132130 :         error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
     752      132130 :         if (error)
     753             :                 return error;
     754             : 
     755      132130 :         if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
     756             :                 /* contiguous chunk, bump startoff for the id calculation */
     757      131070 :                 if (got.br_startoff < start)
     758         209 :                         got.br_startoff = start;
     759      131070 :                 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
     760             :         } else {
     761             :                 error = -ENOENT;
     762             :         }
     763             : 
     764      132130 :         xfs_iunlock(quotip, lock_flags);
     765             : 
     766      132130 :         return error;
     767             : }
     768             : 
     769             : /*
     770             :  * Look up the dquot in the in-core cache.  If found, the dquot is returned
     771             :  * locked and ready to go.
     772             :  */
     773             : static struct xfs_dquot *
     774    90221067 : xfs_qm_dqget_cache_lookup(
     775             :         struct xfs_mount        *mp,
     776             :         struct xfs_quotainfo    *qi,
     777             :         struct radix_tree_root  *tree,
     778             :         xfs_dqid_t              id)
     779             : {
     780    90221630 :         struct xfs_dquot        *dqp;
     781             : 
     782    90221630 : restart:
     783    90221630 :         mutex_lock(&qi->qi_tree_lock);
     784    90224858 :         dqp = radix_tree_lookup(tree, id);
     785    90224858 :         if (!dqp) {
     786    12589896 :                 mutex_unlock(&qi->qi_tree_lock);
     787    12589895 :                 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
     788    12589895 :                 return NULL;
     789             :         }
     790             : 
     791    77634962 :         xfs_dqlock(dqp);
     792    77634962 :         if (dqp->q_flags & XFS_DQFLAG_FREEING) {
     793         563 :                 xfs_dqunlock(dqp);
     794         563 :                 mutex_unlock(&qi->qi_tree_lock);
     795         563 :                 trace_xfs_dqget_freeing(dqp);
     796         563 :                 delay(1);
     797         563 :                 goto restart;
     798             :         }
     799             : 
     800    77634399 :         dqp->q_nrefs++;
     801    77634399 :         mutex_unlock(&qi->qi_tree_lock);
     802             : 
     803    77634339 :         trace_xfs_dqget_hit(dqp);
     804    77634350 :         XFS_STATS_INC(mp, xs_qm_dqcachehits);
     805    77634350 :         return dqp;
     806             : }
     807             : 
     808             : /*
     809             :  * Try to insert a new dquot into the in-core cache.  If an error occurs the
     810             :  * caller should throw away the dquot and start over.  Otherwise, the dquot
     811             :  * is returned locked (and held by the cache) as if there had been a cache
     812             :  * hit.
     813             :  */
     814             : static int
     815    12517520 : xfs_qm_dqget_cache_insert(
     816             :         struct xfs_mount        *mp,
     817             :         struct xfs_quotainfo    *qi,
     818             :         struct radix_tree_root  *tree,
     819             :         xfs_dqid_t              id,
     820             :         struct xfs_dquot        *dqp)
     821             : {
     822    12517520 :         int                     error;
     823             : 
     824    12517520 :         mutex_lock(&qi->qi_tree_lock);
     825    12517586 :         error = radix_tree_insert(tree, id, dqp);
     826    12517586 :         if (unlikely(error)) {
     827             :                 /* Duplicate found!  Caller must try again. */
     828         181 :                 mutex_unlock(&qi->qi_tree_lock);
     829         181 :                 trace_xfs_dqget_dup(dqp);
     830         181 :                 return error;
     831             :         }
     832             : 
     833             :         /* Return a locked dquot to the caller, with a reference taken. */
     834    12517405 :         xfs_dqlock(dqp);
     835    12517405 :         dqp->q_nrefs = 1;
     836             : 
     837    12517405 :         qi->qi_dquots++;
     838    12517405 :         mutex_unlock(&qi->qi_tree_lock);
     839             : 
     840    12517405 :         return 0;
     841             : }
     842             : 
     843             : /* Check our input parameters. */
     844             : static int
     845    90354203 : xfs_qm_dqget_checks(
     846             :         struct xfs_mount        *mp,
     847             :         xfs_dqtype_t            type)
     848             : {
     849    90354203 :         switch (type) {
     850    34700402 :         case XFS_DQTYPE_USER:
     851    34700402 :                 if (!XFS_IS_UQUOTA_ON(mp))
     852         328 :                         return -ESRCH;
     853             :                 return 0;
     854    34443131 :         case XFS_DQTYPE_GROUP:
     855    34443131 :                 if (!XFS_IS_GQUOTA_ON(mp))
     856         934 :                         return -ESRCH;
     857             :                 return 0;
     858    21210670 :         case XFS_DQTYPE_PROJ:
     859    21210670 :                 if (!XFS_IS_PQUOTA_ON(mp))
     860         340 :                         return -ESRCH;
     861             :                 return 0;
     862             :         default:
     863             :                 WARN_ON_ONCE(0);
     864             :                 return -EINVAL;
     865             :         }
     866             : }
     867             : 
     868             : /*
     869             :  * Given the file system, id, and type (UDQUOT/GDQUOT/PDQUOT), return a
     870             :  * locked dquot, doing an allocation (if requested) as needed.
     871             :  */
     872             : int
     873    29941166 : xfs_qm_dqget(
     874             :         struct xfs_mount        *mp,
     875             :         xfs_dqid_t              id,
     876             :         xfs_dqtype_t            type,
     877             :         bool                    can_alloc,
     878             :         struct xfs_dquot        **O_dqpp)
     879             : {
     880    29941166 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     881    29941166 :         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
     882    29940827 :         struct xfs_dquot        *dqp;
     883    29940827 :         int                     error;
     884             : 
     885    29940827 :         error = xfs_qm_dqget_checks(mp, type);
     886    29940827 :         if (error)
     887             :                 return error;
     888             : 
     889    29939985 : restart:
     890    29940113 :         dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
     891    29942136 :         if (dqp) {
     892    20511848 :                 *O_dqpp = dqp;
     893    20511848 :                 return 0;
     894             :         }
     895             : 
     896     9430288 :         error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
     897     9430218 :         if (error)
     898       64186 :                 return error;
     899             : 
     900     9366032 :         error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
     901     9366052 :         if (error) {
     902             :                 /*
     903             :                  * Duplicate found. Just throw away the new dquot and start
     904             :                  * over.
     905             :                  */
     906         128 :                 xfs_qm_dqdestroy(dqp);
     907         128 :                 XFS_STATS_INC(mp, xs_qm_dquot_dups);
     908         128 :                 goto restart;
     909             :         }
     910             : 
     911     9365924 :         trace_xfs_dqget_miss(dqp);
     912     9365924 :         *O_dqpp = dqp;
     913     9365924 :         return 0;
     914             : }
     915             : 
     916             : /*
     917             :  * Given a dquot id and type, read and initialize a dquot from the on-disk
     918             :  * metadata.  This function is only for use during quota initialization so
     919             :  * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
     920             :  * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
     921             :  */
     922             : int
     923      130508 : xfs_qm_dqget_uncached(
     924             :         struct xfs_mount        *mp,
     925             :         xfs_dqid_t              id,
     926             :         xfs_dqtype_t            type,
     927             :         struct xfs_dquot        **dqpp)
     928             : {
     929      130508 :         int                     error;
     930             : 
     931      130508 :         error = xfs_qm_dqget_checks(mp, type);
     932      130508 :         if (error)
     933             :                 return error;
     934             : 
     935      129748 :         return xfs_qm_dqread(mp, id, type, 0, dqpp);
     936             : }
     937             : 
     938             : /* Return the quota id for a given inode and type. */
     939             : xfs_dqid_t
     940   214233363 : xfs_qm_id_for_quotatype(
     941             :         struct xfs_inode        *ip,
     942             :         xfs_dqtype_t            type)
     943             : {
     944   214233363 :         switch (type) {
     945             :         case XFS_DQTYPE_USER:
     946    71415669 :                 return i_uid_read(VFS_I(ip));
     947             :         case XFS_DQTYPE_GROUP:
     948    71409400 :                 return i_gid_read(VFS_I(ip));
     949    71408294 :         case XFS_DQTYPE_PROJ:
     950    71408294 :                 return ip->i_projid;
     951             :         }
     952           0 :         ASSERT(0);
     953           0 :         return 0;
     954             : }
     955             : 
     956             : /*
     957             :  * Return the dquot for a given inode and type.  If @can_alloc is true, then
     958             :  * allocate blocks if needed.  The inode's ILOCK must be held and it must not
     959             :  * have already had an inode attached.
     960             :  */
     961             : int
     962    60280716 : xfs_qm_dqget_inode(
     963             :         struct xfs_inode        *ip,
     964             :         xfs_dqtype_t            type,
     965             :         bool                    can_alloc,
     966             :         struct xfs_dquot        **O_dqpp)
     967             : {
     968    60280716 :         struct xfs_mount        *mp = ip->i_mount;
     969    60280716 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     970    60280716 :         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
     971    60281172 :         struct xfs_dquot        *dqp;
     972    60281172 :         xfs_dqid_t              id;
     973    60281172 :         int                     error;
     974             : 
     975    60281172 :         error = xfs_qm_dqget_checks(mp, type);
     976    60281172 :         if (error)
     977             :                 return error;
     978             : 
     979    60281145 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     980   120563496 :         ASSERT(xfs_inode_dquot(ip, type) == NULL);
     981             : 
     982    60281748 :         id = xfs_qm_id_for_quotatype(ip, type);
     983             : 
     984    60280888 : restart:
     985    60280888 :         dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
     986    60282153 :         if (dqp) {
     987    57122546 :                 *O_dqpp = dqp;
     988    57122546 :                 return 0;
     989             :         }
     990             : 
     991             :         /*
     992             :          * Dquot cache miss. We don't want to keep the inode lock across
     993             :          * a (potential) disk read. Also we don't want to deal with the lock
     994             :          * ordering between quotainode and this inode. OTOH, dropping the inode
     995             :          * lock here means dealing with a chown that can happen before
     996             :          * we re-acquire the lock.
     997             :          */
     998     3159607 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     999     3159607 :         error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
    1000     3159594 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1001     3159594 :         if (error)
    1002        8074 :                 return error;
    1003             : 
    1004             :         /*
    1005             :          * A dquot could be attached to this inode by now, since we had
    1006             :          * dropped the ilock.
    1007             :          */
    1008     3151520 :         if (xfs_this_quota_on(mp, type)) {
    1009     3151520 :                 struct xfs_dquot        *dqp1;
    1010             : 
    1011     3151520 :                 dqp1 = xfs_inode_dquot(ip, type);
    1012     3151520 :                 if (dqp1) {
    1013           0 :                         xfs_qm_dqdestroy(dqp);
    1014           0 :                         dqp = dqp1;
    1015           0 :                         xfs_dqlock(dqp);
    1016           0 :                         goto dqret;
    1017             :                 }
    1018             :         } else {
    1019             :                 /* inode stays locked on return */
    1020           0 :                 xfs_qm_dqdestroy(dqp);
    1021           0 :                 return -ESRCH;
    1022             :         }
    1023             : 
    1024     3151520 :         error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
    1025     3151534 :         if (error) {
    1026             :                 /*
    1027             :                  * Duplicate found. Just throw away the new dquot and start
    1028             :                  * over.
    1029             :                  */
    1030          53 :                 xfs_qm_dqdestroy(dqp);
    1031          53 :                 XFS_STATS_INC(mp, xs_qm_dquot_dups);
    1032          53 :                 goto restart;
    1033             :         }
    1034             : 
    1035     3151481 : dqret:
    1036     3151481 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1037     3151480 :         trace_xfs_dqget_miss(dqp);
    1038     3151481 :         *O_dqpp = dqp;
    1039     3151481 :         return 0;
    1040             : }
    1041             : 
    1042             : /*
    1043             :  * Starting at @id and progressing upwards, look for an initialized incore
    1044             :  * dquot, lock it, and return it.
    1045             :  */
    1046             : int
    1047      144836 : xfs_qm_dqget_next(
    1048             :         struct xfs_mount        *mp,
    1049             :         xfs_dqid_t              id,
    1050             :         xfs_dqtype_t            type,
    1051             :         struct xfs_dquot        **dqpp)
    1052             : {
    1053      144836 :         struct xfs_dquot        *dqp;
    1054      144836 :         int                     error = 0;
    1055             : 
    1056      144836 :         *dqpp = NULL;
    1057     6083695 :         for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
    1058     6082627 :                 error = xfs_qm_dqget(mp, id, type, false, &dqp);
    1059     6082627 :                 if (error == -ENOENT)
    1060       36486 :                         continue;
    1061     6046141 :                 else if (error != 0)
    1062             :                         break;
    1063             : 
    1064     6046113 :                 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
    1065      143740 :                         *dqpp = dqp;
    1066      143740 :                         return 0;
    1067             :                 }
    1068             : 
    1069     5902373 :                 xfs_qm_dqput(dqp);
    1070             :         }
    1071             : 
    1072             :         return error;
    1073             : }
    1074             : 
    1075             : /*
    1076             :  * Release a reference to the dquot (decrement ref-count) and unlock it.
    1077             :  *
    1078             :  * If there is a group quota attached to this dquot, carefully release that
    1079             :  * too without tripping over deadlocks'n'stuff.
    1080             :  */
    1081             : void
    1082   331620519 : xfs_qm_dqput(
    1083             :         struct xfs_dquot        *dqp)
    1084             : {
    1085   331620519 :         ASSERT(dqp->q_nrefs > 0);
    1086   331620519 :         ASSERT(XFS_DQ_IS_LOCKED(dqp));
    1087             : 
    1088   331623788 :         trace_xfs_dqput(dqp);
    1089             : 
    1090   331624658 :         if (--dqp->q_nrefs == 0) {
    1091    20910360 :                 struct xfs_quotainfo    *qi = dqp->q_mount->m_quotainfo;
    1092    20910360 :                 trace_xfs_dqput_free(dqp);
    1093             : 
    1094    20910364 :                 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
    1095    14353626 :                         XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
    1096             :         }
    1097   331624814 :         xfs_dqunlock(dqp);
    1098   331517954 : }
    1099             : 
    1100             : /*
    1101             :  * Release a dquot. Flush it if dirty, then dqput() it.
    1102             :  * dquot must not be locked.
    1103             :  */
    1104             : void
    1105   446586097 : xfs_qm_dqrele(
    1106             :         struct xfs_dquot        *dqp)
    1107             : {
    1108   446586097 :         if (!dqp)
    1109             :                 return;
    1110             : 
    1111   313729290 :         trace_xfs_dqrele(dqp);
    1112             : 
    1113   313740171 :         xfs_dqlock(dqp);
    1114             :         /*
    1115             :          * We don't care to flush it if the dquot is dirty here.
    1116             :          * That will create stutters that we want to avoid.
    1117             :          * Instead we do a delayed write when we try to reclaim
    1118             :          * a dirty dquot. Also xfs_sync will take part of the burden...
    1119             :          */
    1120   313680984 :         xfs_qm_dqput(dqp);
    1121             : }
    1122             : 
    1123             : /*
    1124             :  * This is the dquot flushing I/O completion routine.  It is called
    1125             :  * from interrupt level when the buffer containing the dquot is
    1126             :  * flushed to disk.  It is responsible for removing the dquot logitem
    1127             :  * from the AIL if it has not been re-logged, and unlocking the dquot's
    1128             :  * flush lock. This behavior is very similar to that of inodes..
    1129             :  */
    1130             : static void
    1131    11110041 : xfs_qm_dqflush_done(
    1132             :         struct xfs_log_item     *lip)
    1133             : {
    1134    11110041 :         struct xfs_dq_logitem   *qip = (struct xfs_dq_logitem *)lip;
    1135    11110041 :         struct xfs_dquot        *dqp = qip->qli_dquot;
    1136    11110041 :         struct xfs_ail          *ailp = lip->li_ailp;
    1137    11110041 :         xfs_lsn_t               tail_lsn;
    1138             : 
    1139             :         /*
    1140             :          * We only want to pull the item from the AIL if its
    1141             :          * location in the log has not changed since we started the flush.
    1142             :          * Thus, we only bother if the dquot's lsn has
    1143             :          * not changed. First we check the lsn outside the lock
    1144             :          * since it's cheaper, and then we recheck while
    1145             :          * holding the lock before removing the dquot from the AIL.
    1146             :          */
    1147    22237726 :         if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
    1148    11116483 :             ((lip->li_lsn == qip->qli_flush_lsn) ||
    1149           0 :              test_bit(XFS_LI_FAILED, &lip->li_flags))) {
    1150             : 
    1151    11081195 :                 spin_lock(&ailp->ail_lock);
    1152    11081195 :                 xfs_clear_li_failed(lip);
    1153    11081195 :                 if (lip->li_lsn == qip->qli_flush_lsn) {
    1154             :                         /* xfs_ail_update_finish() drops the AIL lock */
    1155    11081195 :                         tail_lsn = xfs_ail_delete_one(ailp, lip);
    1156    11081195 :                         xfs_ail_update_finish(ailp, tail_lsn);
    1157             :                 } else {
    1158           0 :                         spin_unlock(&ailp->ail_lock);
    1159             :                 }
    1160             :         }
    1161             : 
    1162             :         /*
    1163             :          * Release the dq's flush lock since we're done with it.
    1164             :          */
    1165    11110041 :         xfs_dqfunlock(dqp);
    1166    11110041 : }
    1167             : 
    1168             : void
    1169     9413354 : xfs_buf_dquot_iodone(
    1170             :         struct xfs_buf          *bp)
    1171             : {
    1172     9413354 :         struct xfs_log_item     *lip, *n;
    1173             : 
    1174    20523395 :         list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
    1175    11110041 :                 list_del_init(&lip->li_bio_list);
    1176    11110041 :                 xfs_qm_dqflush_done(lip);
    1177             :         }
    1178     9413354 : }
    1179             : 
    1180             : void
    1181          40 : xfs_buf_dquot_io_fail(
    1182             :         struct xfs_buf          *bp)
    1183             : {
    1184          40 :         struct xfs_log_item     *lip;
    1185             : 
    1186          40 :         spin_lock(&bp->b_mount->m_ail->ail_lock);
    1187          80 :         list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
    1188          40 :                 xfs_set_li_failed(lip, bp);
    1189          40 :         spin_unlock(&bp->b_mount->m_ail->ail_lock);
    1190          40 : }
    1191             : 
    1192             : /* Check incore dquot for errors before we flush. */
    1193             : static xfs_failaddr_t
    1194    11109991 : xfs_qm_dqflush_check(
    1195             :         struct xfs_dquot        *dqp)
    1196             : {
    1197    11109991 :         xfs_dqtype_t            type = xfs_dquot_type(dqp);
    1198             : 
    1199    11109991 :         if (type != XFS_DQTYPE_USER &&
    1200    11109991 :             type != XFS_DQTYPE_GROUP &&
    1201             :             type != XFS_DQTYPE_PROJ)
    1202           0 :                 return __this_address;
    1203             : 
    1204    11109991 :         if (dqp->q_id == 0)
    1205             :                 return NULL;
    1206             : 
    1207    10982663 :         if (dqp->q_blk.softlimit && dqp->q_blk.count > dqp->q_blk.softlimit &&
    1208         137 :             !dqp->q_blk.timer)
    1209           0 :                 return __this_address;
    1210             : 
    1211    10982663 :         if (dqp->q_ino.softlimit && dqp->q_ino.count > dqp->q_ino.softlimit &&
    1212         166 :             !dqp->q_ino.timer)
    1213           0 :                 return __this_address;
    1214             : 
    1215    10982663 :         if (dqp->q_rtb.softlimit && dqp->q_rtb.count > dqp->q_rtb.softlimit &&
    1216           0 :             !dqp->q_rtb.timer)
    1217           0 :                 return __this_address;
    1218             : 
    1219             :         /* bigtime flag should never be set on root dquots */
    1220    10982663 :         if (dqp->q_type & XFS_DQTYPE_BIGTIME) {
    1221    10978426 :                 if (!xfs_has_bigtime(dqp->q_mount))
    1222           0 :                         return __this_address;
    1223             :                 if (dqp->q_id == 0)
    1224           0 :                         return __this_address;
    1225             :         }
    1226             : 
    1227             :         return NULL;
    1228             : }
    1229             : 
    1230             : /*
    1231             :  * Write a modified dquot to disk.
    1232             :  * The dquot must be locked and the flush lock too taken by caller.
    1233             :  * The flush lock will not be unlocked until the dquot reaches the disk,
    1234             :  * but the dquot is free to be unlocked and modified by the caller
    1235             :  * in the interim. Dquot is still locked on return. This behavior is
    1236             :  * identical to that of inodes.
    1237             :  */
    1238             : int
    1239    12559851 : xfs_qm_dqflush(
    1240             :         struct xfs_dquot        *dqp,
    1241             :         struct xfs_buf          **bpp)
    1242             : {
    1243    12559851 :         struct xfs_mount        *mp = dqp->q_mount;
    1244    12559851 :         struct xfs_log_item     *lip = &dqp->q_logitem.qli_item;
    1245    12559851 :         struct xfs_buf          *bp;
    1246    12559851 :         struct xfs_dqblk        *dqblk;
    1247    12559851 :         xfs_failaddr_t          fa;
    1248    12559851 :         int                     error;
    1249             : 
    1250    12559851 :         ASSERT(XFS_DQ_IS_LOCKED(dqp));
    1251    12559849 :         ASSERT(!completion_done(&dqp->q_flush));
    1252             : 
    1253    12559844 :         trace_xfs_dqflush(dqp);
    1254             : 
    1255    12559836 :         *bpp = NULL;
    1256             : 
    1257    12559836 :         xfs_qm_dqunpin_wait(dqp);
    1258             : 
    1259             :         /*
    1260             :          * Get the buffer containing the on-disk dquot
    1261             :          */
    1262    12559832 :         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
    1263    12559832 :                                    mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
    1264             :                                    &bp, &xfs_dquot_buf_ops);
    1265    12559734 :         if (error == -EAGAIN)
    1266       19840 :                 goto out_unlock;
    1267    12539894 :         if (xfs_metadata_is_sick(error))
    1268           0 :                 xfs_dquot_mark_sick(dqp);
    1269    12539894 :         if (error)
    1270     1429992 :                 goto out_abort;
    1271             : 
    1272    11109902 :         fa = xfs_qm_dqflush_check(dqp);
    1273    11110005 :         if (fa) {
    1274           0 :                 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
    1275             :                                 dqp->q_id, fa);
    1276           0 :                 xfs_buf_relse(bp);
    1277           0 :                 xfs_dquot_mark_sick(dqp);
    1278           0 :                 error = -EFSCORRUPTED;
    1279           0 :                 goto out_abort;
    1280             :         }
    1281             : 
    1282             :         /* Flush the incore dquot to the ondisk buffer. */
    1283    11110005 :         dqblk = bp->b_addr + dqp->q_bufoffset;
    1284    11110005 :         xfs_dquot_to_disk(&dqblk->dd_diskdq, dqp);
    1285             : 
    1286             :         /*
    1287             :          * Clear the dirty field and remember the flush lsn for later use.
    1288             :          */
    1289    11109908 :         dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
    1290             : 
    1291    11109908 :         xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
    1292             :                                         &dqp->q_logitem.qli_item.li_lsn);
    1293             : 
    1294             :         /*
    1295             :          * copy the lsn into the on-disk dquot now while we have the in memory
    1296             :          * dquot here. This can't be done later in the write verifier as we
    1297             :          * can't get access to the log item at that point in time.
    1298             :          *
    1299             :          * We also calculate the CRC here so that the on-disk dquot in the
    1300             :          * buffer always has a valid CRC. This ensures there is no possibility
    1301             :          * of a dquot without an up-to-date CRC getting to disk.
    1302             :          */
    1303    11109908 :         if (xfs_has_crc(mp)) {
    1304    11109934 :                 dqblk->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
    1305    11109934 :                 xfs_update_cksum((char *)dqblk, sizeof(struct xfs_dqblk),
    1306             :                                  XFS_DQUOT_CRC_OFF);
    1307             :         }
    1308             : 
    1309             :         /*
    1310             :          * Attach the dquot to the buffer so that we can remove this dquot from
    1311             :          * the AIL and release the flush lock once the dquot is synced to disk.
    1312             :          */
    1313    11109944 :         bp->b_flags |= _XBF_DQUOTS;
    1314    11109944 :         list_add_tail(&dqp->q_logitem.qli_item.li_bio_list, &bp->b_li_list);
    1315             : 
    1316             :         /*
    1317             :          * If the buffer is pinned then push on the log so we won't
    1318             :          * get stuck waiting in the write for too long.
    1319             :          */
    1320    11109990 :         if (xfs_buf_ispinned(bp)) {
    1321        9745 :                 trace_xfs_dqflush_force(dqp);
    1322        9745 :                 xfs_log_force(mp, 0);
    1323             :         }
    1324             : 
    1325    11109990 :         trace_xfs_dqflush_done(dqp);
    1326    11109959 :         *bpp = bp;
    1327    11109959 :         return 0;
    1328             : 
    1329     1429992 : out_abort:
    1330     1429992 :         dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
    1331     1429992 :         xfs_trans_ail_delete(lip, 0);
    1332     1429992 :         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
    1333     1449832 : out_unlock:
    1334     1449832 :         xfs_dqfunlock(dqp);
    1335     1449832 :         return error;
    1336             : }
    1337             : 
    1338             : /*
    1339             :  * Lock two xfs_dquot structures.
    1340             :  *
    1341             :  * To avoid deadlocks we always lock the quota structure with
    1342             :  * the lowerd id first.
    1343             :  */
    1344             : void
    1345     9607287 : xfs_dqlock2(
    1346             :         struct xfs_dquot        *d1,
    1347             :         struct xfs_dquot        *d2)
    1348             : {
    1349     9607287 :         if (d1 && d2) {
    1350     9607287 :                 ASSERT(d1 != d2);
    1351     9607287 :                 if (d1->q_id > d2->q_id) {
    1352     7879170 :                         mutex_lock(&d2->q_qlock);
    1353     7879525 :                         mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
    1354             :                 } else {
    1355     1728117 :                         mutex_lock(&d1->q_qlock);
    1356     1728119 :                         mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
    1357             :                 }
    1358           0 :         } else if (d1) {
    1359           0 :                 mutex_lock(&d1->q_qlock);
    1360           0 :         } else if (d2) {
    1361           0 :                 mutex_lock(&d2->q_qlock);
    1362             :         }
    1363     9607646 : }
    1364             : 
    1365             : static int
    1366        6528 : xfs_dqtrx_cmp(
    1367             :         const void              *a,
    1368             :         const void              *b)
    1369             : {
    1370        6528 :         const struct xfs_dqtrx  *qa = a;
    1371        6528 :         const struct xfs_dqtrx  *qb = b;
    1372             : 
    1373        6528 :         if (qa->qt_dquot->q_id > qb->qt_dquot->q_id)
    1374             :                 return 1;
    1375        4007 :         if (qa->qt_dquot->q_id < qb->qt_dquot->q_id)
    1376        4007 :                 return -1;
    1377             :         return 0;
    1378             : }
    1379             : 
    1380             : void
    1381        2176 : xfs_dqlockn(
    1382             :         struct xfs_dqtrx        *q)
    1383             : {
    1384        2176 :         unsigned int            i;
    1385             : 
    1386             :         /* Sort in order of dquot id, do not allow duplicates */
    1387        8704 :         for (i = 0; i < XFS_QM_TRANS_MAXDQS && q[i].qt_dquot != NULL; i++) {
    1388             :                 unsigned int    j;
    1389             : 
    1390       13056 :                 for (j = 0; j < i; j++)
    1391        6528 :                         ASSERT(q[i].qt_dquot != q[j].qt_dquot);
    1392             :         }
    1393        2176 :         if (i == 0)
    1394             :                 return;
    1395             : 
    1396        2176 :         sort(q, i, sizeof(struct xfs_dqtrx), xfs_dqtrx_cmp, NULL);
    1397             : 
    1398        2176 :         mutex_lock(&q[0].qt_dquot->q_qlock);
    1399        8704 :         for (i = 1; i < XFS_QM_TRANS_MAXDQS && q[i].qt_dquot != NULL; i++)
    1400        4352 :                 mutex_lock_nested(&q[i].qt_dquot->q_qlock, XFS_QLOCK_NESTED);
    1401             : }
    1402             : 
    1403             : int __init
    1404          12 : xfs_qm_init(void)
    1405             : {
    1406          12 :         xfs_dquot_cache = kmem_cache_create("xfs_dquot",
    1407             :                                           sizeof(struct xfs_dquot),
    1408             :                                           0, 0, NULL);
    1409          12 :         if (!xfs_dquot_cache)
    1410           0 :                 goto out;
    1411             : 
    1412          12 :         xfs_dqtrx_cache = kmem_cache_create("xfs_dqtrx",
    1413             :                                              sizeof(struct xfs_dquot_acct),
    1414             :                                              0, 0, NULL);
    1415          12 :         if (!xfs_dqtrx_cache)
    1416           0 :                 goto out_free_dquot_cache;
    1417             : 
    1418             :         return 0;
    1419             : 
    1420             : out_free_dquot_cache:
    1421           0 :         kmem_cache_destroy(xfs_dquot_cache);
    1422             : out:
    1423             :         return -ENOMEM;
    1424             : }
    1425             : 
    1426             : void
    1427          12 : xfs_qm_exit(void)
    1428             : {
    1429          12 :         kmem_cache_destroy(xfs_dqtrx_cache);
    1430          12 :         kmem_cache_destroy(xfs_dquot_cache);
    1431          12 : }
    1432             : 
    1433             : /*
    1434             :  * Iterate every dquot of a particular type.  The caller must ensure that the
    1435             :  * particular quota type is active.  iter_fn can return negative error codes,
    1436             :  * or -ECANCELED to indicate that it wants to stop iterating.
    1437             :  */
    1438             : int
    1439       81169 : xfs_qm_dqiterate(
    1440             :         struct xfs_mount        *mp,
    1441             :         xfs_dqtype_t            type,
    1442             :         xfs_qm_dqiterate_fn     iter_fn,
    1443             :         void                    *priv)
    1444             : {
    1445       81169 :         struct xfs_dquot        *dq;
    1446       81169 :         xfs_dqid_t              id = 0;
    1447       81169 :         int                     error;
    1448             : 
    1449       81169 :         do {
    1450       81169 :                 error = xfs_qm_dqget_next(mp, id, type, &dq);
    1451       81169 :                 if (error == -ENOENT)
    1452             :                         return 0;
    1453       81169 :                 if (error)
    1454           0 :                         return error;
    1455             : 
    1456       81169 :                 error = iter_fn(dq, type, priv);
    1457       81169 :                 id = dq->q_id;
    1458       81169 :                 xfs_qm_dqput(dq);
    1459       81169 :         } while (error == 0 && id != 0);
    1460             : 
    1461             :         return error;
    1462             : }

Generated by: LCOV version 1.14