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-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 580 630 92.1 %
Date: 2023-07-31 20:08:12 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    16171656 : xfs_qm_dqdestroy(
      76             :         struct xfs_dquot        *dqp)
      77             : {
      78    16171656 :         ASSERT(list_empty(&dqp->q_lru));
      79             : 
      80    16171656 :         kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
      81    16171633 :         mutex_destroy(&dqp->q_qlock);
      82             : 
      83    16171642 :         XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
      84    16171657 :         kmem_cache_free(xfs_dquot_cache, dqp);
      85    16171699 : }
      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   256458281 : xfs_qm_adjust_dqlimits(
      94             :         struct xfs_dquot        *dq)
      95             : {
      96   256458281 :         struct xfs_mount        *mp = dq->q_mount;
      97   256458281 :         struct xfs_quotainfo    *q = mp->m_quotainfo;
      98   256458281 :         struct xfs_def_quota    *defq;
      99   256458281 :         int                     prealloc = 0;
     100             : 
     101   256458281 :         ASSERT(dq->q_id);
     102   256458281 :         defq = xfs_get_defquota(q, xfs_dquot_type(dq));
     103             : 
     104   256454608 :         if (!dq->q_blk.softlimit) {
     105   256022323 :                 dq->q_blk.softlimit = defq->blk.soft;
     106   256022323 :                 prealloc = 1;
     107             :         }
     108   256454608 :         if (!dq->q_blk.hardlimit) {
     109   256008315 :                 dq->q_blk.hardlimit = defq->blk.hard;
     110   256008315 :                 prealloc = 1;
     111             :         }
     112   256454608 :         if (!dq->q_ino.softlimit)
     113   256040182 :                 dq->q_ino.softlimit = defq->ino.soft;
     114   256454608 :         if (!dq->q_ino.hardlimit)
     115   256040124 :                 dq->q_ino.hardlimit = defq->ino.hard;
     116   256454608 :         if (!dq->q_rtb.softlimit)
     117   256454958 :                 dq->q_rtb.softlimit = defq->rtb.soft;
     118   256454608 :         if (!dq->q_rtb.hardlimit)
     119   256454724 :                 dq->q_rtb.hardlimit = defq->rtb.hard;
     120             : 
     121   256454608 :         if (prealloc)
     122   256021057 :                 xfs_dquot_set_prealloc_limits(dq);
     123   256449409 : }
     124             : 
     125             : /* Set the expiration time of a quota's grace period. */
     126             : time64_t
     127         222 : xfs_dquot_set_timeout(
     128             :         struct xfs_mount        *mp,
     129             :         time64_t                timeout)
     130             : {
     131         906 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     132             : 
     133         906 :         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       16896 : xfs_dquot_set_grace_period(
     140             :         time64_t                grace)
     141             : {
     142       16896 :         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   769484020 : xfs_qm_adjust_res_timer(
     151             :         struct xfs_mount        *mp,
     152             :         struct xfs_dquot_res    *res,
     153             :         struct xfs_quota_limits *qlim)
     154             : {
     155   769484020 :         ASSERT(res->hardlimit == 0 || res->softlimit <= res->hardlimit);
     156             : 
     157   769484020 :         if ((res->softlimit && res->count > res->softlimit) ||
     158   769456643 :             (res->hardlimit && res->count > res->hardlimit)) {
     159       28373 :                 if (res->timer == 0)
     160         684 :                         res->timer = xfs_dquot_set_timeout(mp,
     161         684 :                                         ktime_get_real_seconds() + qlim->time);
     162             :         } else {
     163   769455647 :                 res->timer = 0;
     164             :         }
     165   769484020 : }
     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   256509405 : xfs_qm_adjust_dqtimers(
     182             :         struct xfs_dquot        *dq)
     183             : {
     184   256509405 :         struct xfs_mount        *mp = dq->q_mount;
     185   256509405 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     186   256509405 :         struct xfs_def_quota    *defq;
     187             : 
     188   256509405 :         ASSERT(dq->q_id);
     189   256509405 :         defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
     190             : 
     191   256500948 :         xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_blk, &defq->blk);
     192   256497971 :         xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_ino, &defq->ino);
     193   256499584 :         xfs_qm_adjust_res_timer(dq->q_mount, &dq->q_rtb, &defq->rtb);
     194   256500536 : }
     195             : 
     196             : /*
     197             :  * initialize a buffer full of dquots and log the whole thing
     198             :  */
     199             : STATIC void
     200     2927324 : 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     2927324 :         struct xfs_quotainfo    *q = mp->m_quotainfo;
     208     2927324 :         struct xfs_dqblk        *d;
     209     2927324 :         xfs_dqid_t              curid;
     210     2927324 :         unsigned int            qflag;
     211     2927324 :         unsigned int            blftype;
     212     2927324 :         int                     i;
     213             : 
     214     2927324 :         ASSERT(tp);
     215     2927324 :         ASSERT(xfs_buf_islocked(bp));
     216             : 
     217     2927324 :         switch (type) {
     218             :         case XFS_DQTYPE_USER:
     219             :                 qflag = XFS_UQUOTA_CHKD;
     220             :                 blftype = XFS_BLF_UDQUOT_BUF;
     221             :                 break;
     222       61343 :         case XFS_DQTYPE_PROJ:
     223       61343 :                 qflag = XFS_PQUOTA_CHKD;
     224       61343 :                 blftype = XFS_BLF_PDQUOT_BUF;
     225       61343 :                 break;
     226     1430648 :         case XFS_DQTYPE_GROUP:
     227     1430648 :                 qflag = XFS_GQUOTA_CHKD;
     228     1430648 :                 blftype = XFS_BLF_GDQUOT_BUF;
     229     1430648 :                 break;
     230           0 :         default:
     231           0 :                 ASSERT(0);
     232           0 :                 return;
     233             :         }
     234             : 
     235     2927324 :         d = bp->b_addr;
     236             : 
     237             :         /*
     238             :          * ID of the first dquot in the block - id's are zero based.
     239             :          */
     240     2927324 :         curid = id - (id % q->qi_dqperchunk);
     241     2927324 :         memset(d, 0, BBTOB(q->qi_dqchunklen));
     242    90728804 :         for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
     243    87801556 :                 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
     244    87801556 :                 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
     245    87801556 :                 d->dd_diskdq.d_id = cpu_to_be32(curid);
     246    87801556 :                 d->dd_diskdq.d_type = type;
     247    87801556 :                 if (curid > 0 && xfs_has_bigtime(mp))
     248    87516615 :                         d->dd_diskdq.d_type |= XFS_DQTYPE_BIGTIME;
     249    87801556 :                 if (xfs_has_crc(mp)) {
     250    87798556 :                         uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
     251    87796576 :                         xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
     252             :                                          XFS_DQUOT_CRC_OFF);
     253             :                 }
     254             :         }
     255             : 
     256     2927248 :         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     2926720 :         if (!(mp->m_qflags & qflag))
     275       37029 :                 xfs_trans_ordered_buf(tp, bp);
     276             :         else
     277     2889691 :                 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   271994346 : xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
     287             : {
     288   271994346 :         uint64_t space;
     289             : 
     290   271994346 :         dqp->q_prealloc_hi_wmark = dqp->q_blk.hardlimit;
     291   271994346 :         dqp->q_prealloc_lo_wmark = dqp->q_blk.softlimit;
     292   271994346 :         if (!dqp->q_prealloc_lo_wmark) {
     293   271959973 :                 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
     294   271959973 :                 do_div(dqp->q_prealloc_lo_wmark, 100);
     295   271959973 :                 dqp->q_prealloc_lo_wmark *= 95;
     296             :         }
     297             : 
     298   271994346 :         space = dqp->q_prealloc_hi_wmark;
     299             : 
     300   271994346 :         do_div(space, 100);
     301   271994346 :         dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
     302   271994346 :         dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
     303   271994346 :         dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
     304   271994346 : }
     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     2955101 : xfs_dquot_disk_alloc(
     313             :         struct xfs_dquot        *dqp,
     314             :         struct xfs_buf          **bpp)
     315             : {
     316     2955101 :         struct xfs_bmbt_irec    map;
     317     2955101 :         struct xfs_trans        *tp;
     318     2955101 :         struct xfs_mount        *mp = dqp->q_mount;
     319     2955101 :         struct xfs_buf          *bp;
     320     2955101 :         xfs_dqtype_t            qtype = xfs_dquot_type(dqp);
     321     2955101 :         struct xfs_inode        *quotip = xfs_quota_inode(mp, qtype);
     322     2955051 :         int                     nmaps = 1;
     323     2955051 :         int                     error;
     324             : 
     325     2955051 :         trace_xfs_dqalloc(dqp);
     326             : 
     327     2955001 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
     328     2955001 :                         XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
     329     2954697 :         if (error)
     330             :                 return error;
     331             : 
     332     2928182 :         xfs_ilock(quotip, XFS_ILOCK_EXCL);
     333     2928774 :         xfs_trans_ijoin(tp, quotip, 0);
     334             : 
     335     2928851 :         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     2928851 :         error = xfs_iext_count_may_overflow(quotip, XFS_DATA_FORK,
     345             :                         XFS_IEXT_ADD_NOSPLIT_CNT);
     346     2928748 :         if (error == -EFBIG)
     347           5 :                 error = xfs_iext_count_upgrade(tp, quotip,
     348             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
     349     2928748 :         if (error)
     350           5 :                 goto err_cancel;
     351             : 
     352             :         /* Create the block mapping. */
     353     2928743 :         error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
     354             :                         XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
     355             :                         &nmaps);
     356     2928669 :         if (error)
     357        1606 :                 goto err_cancel;
     358             : 
     359     2927063 :         ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
     360     2927063 :         ASSERT(nmaps == 1);
     361     2927063 :         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     2927063 :         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     2926857 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
     371     2926857 :                         mp->m_quotainfo->qi_dqchunklen, 0, &bp);
     372     2926954 :         if (error)
     373           0 :                 goto err_cancel;
     374     2926954 :         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     2926954 :         xfs_qm_init_dquot_blk(tp, mp, dqp->q_id, qtype, bp);
     381     2926762 :         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     2926761 :         xfs_trans_bhold(tp, bp);
     408     2926413 :         error = xfs_trans_commit(tp);
     409     2927081 :         xfs_iunlock(quotip, XFS_ILOCK_EXCL);
     410     2927086 :         if (error) {
     411         221 :                 xfs_buf_relse(bp);
     412         221 :                 return error;
     413             :         }
     414             : 
     415     2926865 :         *bpp = bp;
     416     2926865 :         return 0;
     417             : 
     418        1611 : err_cancel:
     419        1611 :         xfs_trans_cancel(tp);
     420        1611 :         xfs_iunlock(quotip, XFS_ILOCK_EXCL);
     421        1611 :         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    16171293 : xfs_dquot_disk_read(
     430             :         struct xfs_mount        *mp,
     431             :         struct xfs_dquot        *dqp,
     432             :         struct xfs_buf          **bpp)
     433             : {
     434    16171293 :         struct xfs_bmbt_irec    map;
     435    16171293 :         struct xfs_buf          *bp;
     436    16171293 :         xfs_dqtype_t            qtype = xfs_dquot_type(dqp);
     437    16171293 :         struct xfs_inode        *quotip = xfs_quota_inode(mp, qtype);
     438    16171047 :         uint                    lock_mode;
     439    16171047 :         int                     nmaps = 1;
     440    16171047 :         int                     error;
     441             : 
     442    16171047 :         lock_mode = xfs_ilock_data_map_shared(quotip);
     443    16171184 :         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    16171184 :         error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
     456             :                         XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
     457    16170978 :         xfs_iunlock(quotip, lock_mode);
     458    16171202 :         if (error)
     459             :                 return error;
     460             : 
     461    16170588 :         ASSERT(nmaps == 1);
     462    16170588 :         ASSERT(map.br_blockcount >= 1);
     463    16170588 :         ASSERT(map.br_startblock != DELAYSTARTBLOCK);
     464    16170588 :         if (map.br_startblock == HOLESTARTBLOCK)
     465             :                 return -ENOENT;
     466             : 
     467    12989784 :         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    12989708 :         dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
     474             : 
     475    12989716 :         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
     476    12989716 :                         mp->m_quotainfo->qi_dqchunklen, 0, &bp,
     477             :                         &xfs_dquot_buf_ops);
     478    12989846 :         if (xfs_metadata_is_sick(error))
     479           6 :                 xfs_dquot_mark_sick(dqp);
     480    12989846 :         if (error) {
     481        5718 :                 ASSERT(bp == NULL);
     482        5718 :                 return error;
     483             :         }
     484             : 
     485    12984128 :         ASSERT(xfs_buf_islocked(bp));
     486    12984128 :         xfs_buf_set_ref(bp, XFS_DQUOT_REF);
     487    12984059 :         *bpp = bp;
     488             : 
     489    12984059 :         return 0;
     490             : }
     491             : 
     492             : /* Allocate and initialize everything we need for an incore dquot. */
     493             : STATIC struct xfs_dquot *
     494    16171673 : xfs_dquot_alloc(
     495             :         struct xfs_mount        *mp,
     496             :         xfs_dqid_t              id,
     497             :         xfs_dqtype_t            type)
     498             : {
     499    16171673 :         struct xfs_dquot        *dqp;
     500             : 
     501    16171673 :         dqp = kmem_cache_zalloc(xfs_dquot_cache, GFP_KERNEL | __GFP_NOFAIL);
     502             : 
     503    16171657 :         dqp->q_type = type;
     504    16171657 :         dqp->q_id = id;
     505    16171657 :         dqp->q_mount = mp;
     506    16171657 :         INIT_LIST_HEAD(&dqp->q_lru);
     507    16171657 :         mutex_init(&dqp->q_qlock);
     508    16171569 :         init_waitqueue_head(&dqp->q_pinwait);
     509    16171555 :         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    16171555 :         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    16171555 :         init_completion(&dqp->q_flush);
     522    16171459 :         complete(&dqp->q_flush);
     523             : 
     524             :         /*
     525             :          * Make sure group quotas have a different lock class than user
     526             :          * quotas.
     527             :          */
     528    16171402 :         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    16171402 :         xfs_qm_dquot_logitem_init(dqp);
     544             : 
     545    16171279 :         XFS_STATS_INC(mp, xs_qm_dquot);
     546    16171357 :         return dqp;
     547             : }
     548             : 
     549             : /* Check the ondisk dquot's id and type match what the incore dquot expects. */
     550             : static bool
     551    15910807 : xfs_dquot_check_type(
     552             :         struct xfs_dquot        *dqp,
     553             :         struct xfs_disk_dquot   *ddqp)
     554             : {
     555    15910807 :         uint8_t                 ddqp_type;
     556    15910807 :         uint8_t                 dqp_type;
     557             : 
     558    15910807 :         ddqp_type = ddqp->d_type & XFS_DQTYPE_REC_MASK;
     559    15910807 :         dqp_type = xfs_dquot_type(dqp);
     560             : 
     561    15910807 :         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    15910947 :         if (xfs_has_crc(dqp->q_mount) ||
     570          85 :             dqp_type == XFS_DQTYPE_USER || dqp->q_id != 0)
     571    15910862 :                 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          85 :         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    15910904 : xfs_dquot_from_disk(
     588             :         struct xfs_dquot        *dqp,
     589             :         struct xfs_buf          *bp)
     590             : {
     591    15910904 :         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    15910904 :         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    15910904 :         dqp->q_type = ddqp->d_type;
     608    15910904 :         dqp->q_blk.hardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
     609    15910904 :         dqp->q_blk.softlimit = be64_to_cpu(ddqp->d_blk_softlimit);
     610    15910904 :         dqp->q_ino.hardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
     611    15910904 :         dqp->q_ino.softlimit = be64_to_cpu(ddqp->d_ino_softlimit);
     612    15910904 :         dqp->q_rtb.hardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
     613    15910904 :         dqp->q_rtb.softlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
     614             : 
     615    15910904 :         dqp->q_blk.count = be64_to_cpu(ddqp->d_bcount);
     616    15910904 :         dqp->q_ino.count = be64_to_cpu(ddqp->d_icount);
     617    15910904 :         dqp->q_rtb.count = be64_to_cpu(ddqp->d_rtbcount);
     618             : 
     619    15910904 :         dqp->q_blk.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_btimer);
     620    15910824 :         dqp->q_ino.timer = xfs_dquot_from_disk_ts(ddqp, ddqp->d_itimer);
     621    15910863 :         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    15910947 :         dqp->q_blk.reserved = dqp->q_blk.count;
     628    15910947 :         dqp->q_ino.reserved = dqp->q_ino.count;
     629    15910947 :         dqp->q_rtb.reserved = dqp->q_rtb.count;
     630             : 
     631             :         /* initialize the dquot speculative prealloc thresholds */
     632    15910947 :         xfs_dquot_set_prealloc_limits(dqp);
     633    15910947 :         return 0;
     634             : }
     635             : 
     636             : /* Copy the in-core quota fields into the on-disk buffer. */
     637             : void
     638  4005211771 : xfs_dquot_to_disk(
     639             :         struct xfs_disk_dquot   *ddqp,
     640             :         struct xfs_dquot        *dqp)
     641             : {
     642  4005211771 :         ddqp->d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
     643  4005211771 :         ddqp->d_version = XFS_DQUOT_VERSION;
     644  4005211771 :         ddqp->d_type = dqp->q_type;
     645  4005211771 :         ddqp->d_id = cpu_to_be32(dqp->q_id);
     646  4005211771 :         ddqp->d_pad0 = 0;
     647  4005211771 :         ddqp->d_pad = 0;
     648             : 
     649  4005211771 :         ddqp->d_blk_hardlimit = cpu_to_be64(dqp->q_blk.hardlimit);
     650  4005211771 :         ddqp->d_blk_softlimit = cpu_to_be64(dqp->q_blk.softlimit);
     651  4005211771 :         ddqp->d_ino_hardlimit = cpu_to_be64(dqp->q_ino.hardlimit);
     652  4005211771 :         ddqp->d_ino_softlimit = cpu_to_be64(dqp->q_ino.softlimit);
     653  4005211771 :         ddqp->d_rtb_hardlimit = cpu_to_be64(dqp->q_rtb.hardlimit);
     654  4005211771 :         ddqp->d_rtb_softlimit = cpu_to_be64(dqp->q_rtb.softlimit);
     655             : 
     656  4005211771 :         ddqp->d_bcount = cpu_to_be64(dqp->q_blk.count);
     657  4005211771 :         ddqp->d_icount = cpu_to_be64(dqp->q_ino.count);
     658  4005211771 :         ddqp->d_rtbcount = cpu_to_be64(dqp->q_rtb.count);
     659             : 
     660  4005211771 :         ddqp->d_bwarns = 0;
     661  4005211771 :         ddqp->d_iwarns = 0;
     662  4005211771 :         ddqp->d_rtbwarns = 0;
     663             : 
     664  4005211771 :         ddqp->d_btimer = xfs_dquot_to_disk_ts(dqp, dqp->q_blk.timer);
     665  4005202465 :         ddqp->d_itimer = xfs_dquot_to_disk_ts(dqp, dqp->q_ino.timer);
     666  4005217513 :         ddqp->d_rtbtimer = xfs_dquot_to_disk_ts(dqp, dqp->q_rtb.timer);
     667  4005220283 : }
     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    16171672 : 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    16171672 :         struct xfs_dquot        *dqp;
     683    16171672 :         struct xfs_buf          *bp;
     684    16171672 :         int                     error;
     685             : 
     686    16171672 :         dqp = xfs_dquot_alloc(mp, id, type);
     687    16171422 :         trace_xfs_dqread(dqp);
     688             : 
     689             :         /* Try to read the buffer, allocating if necessary. */
     690    16171363 :         error = xfs_dquot_disk_read(mp, dqp, &bp);
     691    16171037 :         if (error == -ENOENT && can_alloc)
     692     2955180 :                 error = xfs_dquot_disk_alloc(dqp, &bp);
     693    16171077 :         if (error)
     694      260209 :                 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    15910868 :         ASSERT(xfs_buf_islocked(bp));
     703    15910868 :         error = xfs_dquot_from_disk(dqp, bp);
     704    15910679 :         xfs_buf_relse(bp);
     705    15911313 :         if (error)
     706           0 :                 goto err;
     707             : 
     708    15911313 :         *dqpp = dqp;
     709    15911313 :         return error;
     710             : 
     711      260209 : err:
     712      260209 :         trace_xfs_dqread_fail(dqp);
     713      260185 :         xfs_qm_dqdestroy(dqp);
     714      260244 :         *dqpp = NULL;
     715      260244 :         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    12935383 : xfs_dq_get_next_id(
     725             :         struct xfs_mount        *mp,
     726             :         xfs_dqtype_t            type,
     727             :         xfs_dqid_t              *id)
     728             : {
     729    12935383 :         struct xfs_inode        *quotip = xfs_quota_inode(mp, type);
     730    12935383 :         xfs_dqid_t              next_id = *id + 1; /* simple advance */
     731    12935383 :         uint                    lock_flags;
     732    12935383 :         struct xfs_bmbt_irec    got;
     733    12935383 :         struct xfs_iext_cursor  cur;
     734    12935383 :         xfs_fsblock_t           start;
     735    12935383 :         int                     error = 0;
     736             : 
     737             :         /* If we'd wrap past the max ID, stop */
     738    12935383 :         if (next_id < *id)
     739             :                 return -ENOENT;
     740             : 
     741             :         /* If new ID is within the current chunk, advancing it sufficed */
     742    12935343 :         if (next_id % mp->m_quotainfo->qi_dqperchunk) {
     743    12503882 :                 *id = next_id;
     744    12503882 :                 return 0;
     745             :         }
     746             : 
     747             :         /* Nope, next_id is now past the current chunk, so find the next one */
     748      431461 :         start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
     749             : 
     750      431461 :         lock_flags = xfs_ilock_data_map_shared(quotip);
     751      431461 :         error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
     752      431461 :         if (error)
     753             :                 return error;
     754             : 
     755      431461 :         if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
     756             :                 /* contiguous chunk, bump startoff for the id calculation */
     757      428508 :                 if (got.br_startoff < start)
     758         641 :                         got.br_startoff = start;
     759      428508 :                 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
     760             :         } else {
     761             :                 error = -ENOENT;
     762             :         }
     763             : 
     764      431461 :         xfs_iunlock(quotip, lock_flags);
     765             : 
     766      431461 :         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   155204669 : 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   155204673 :         struct xfs_dquot        *dqp;
     781             : 
     782   155204673 : restart:
     783   155204673 :         mutex_lock(&qi->qi_tree_lock);
     784   155320305 :         dqp = radix_tree_lookup(tree, id);
     785   155320305 :         if (!dqp) {
     786    15959644 :                 mutex_unlock(&qi->qi_tree_lock);
     787    15959643 :                 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
     788    15959641 :                 return NULL;
     789             :         }
     790             : 
     791   139360661 :         xfs_dqlock(dqp);
     792   139360661 :         if (dqp->q_flags & XFS_DQFLAG_FREEING) {
     793           4 :                 xfs_dqunlock(dqp);
     794           4 :                 mutex_unlock(&qi->qi_tree_lock);
     795           4 :                 trace_xfs_dqget_freeing(dqp);
     796           4 :                 delay(1);
     797           4 :                 goto restart;
     798             :         }
     799             : 
     800   139360657 :         dqp->q_nrefs++;
     801   139360657 :         mutex_unlock(&qi->qi_tree_lock);
     802             : 
     803   139360640 :         trace_xfs_dqget_hit(dqp);
     804   139360624 :         XFS_STATS_INC(mp, xs_qm_dqcachehits);
     805   139360598 :         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    15739538 : 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    15739538 :         int                     error;
     823             : 
     824    15739538 :         mutex_lock(&qi->qi_tree_lock);
     825    15739957 :         error = radix_tree_insert(tree, id, dqp);
     826    15739957 :         if (unlikely(error)) {
     827             :                 /* Duplicate found!  Caller must try again. */
     828         385 :                 mutex_unlock(&qi->qi_tree_lock);
     829         385 :                 trace_xfs_dqget_dup(dqp);
     830         385 :                 return error;
     831             :         }
     832             : 
     833             :         /* Return a locked dquot to the caller, with a reference taken. */
     834    15739572 :         xfs_dqlock(dqp);
     835    15739572 :         dqp->q_nrefs = 1;
     836             : 
     837    15739572 :         qi->qi_dquots++;
     838    15739572 :         mutex_unlock(&qi->qi_tree_lock);
     839             : 
     840    15739572 :         return 0;
     841             : }
     842             : 
     843             : /* Check our input parameters. */
     844             : static int
     845   155411186 : xfs_qm_dqget_checks(
     846             :         struct xfs_mount        *mp,
     847             :         xfs_dqtype_t            type)
     848             : {
     849   155411186 :         switch (type) {
     850    66990468 :         case XFS_DQTYPE_USER:
     851    66990468 :                 if (!XFS_IS_UQUOTA_ON(mp))
     852        1008 :                         return -ESRCH;
     853             :                 return 0;
     854    66745374 :         case XFS_DQTYPE_GROUP:
     855    66745374 :                 if (!XFS_IS_GQUOTA_ON(mp))
     856        2822 :                         return -ESRCH;
     857             :                 return 0;
     858    21675344 :         case XFS_DQTYPE_PROJ:
     859    21675344 :                 if (!XFS_IS_PQUOTA_ON(mp))
     860        1095 :                         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   102482299 : 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   102482299 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     881   102482299 :         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
     882   102406592 :         struct xfs_dquot        *dqp;
     883   102406592 :         int                     error;
     884             : 
     885   102406592 :         error = xfs_qm_dqget_checks(mp, type);
     886   102406592 :         if (error)
     887             :                 return error;
     888             : 
     889   102404066 : restart:
     890   102404304 :         dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
     891   102550022 :         if (dqp) {
     892    87711016 :                 *O_dqpp = dqp;
     893    87711016 :                 return 0;
     894             :         }
     895             : 
     896    14839006 :         error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
     897    14838817 :         if (error)
     898      215587 :                 return error;
     899             : 
     900    14623230 :         error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
     901    14623392 :         if (error) {
     902             :                 /*
     903             :                  * Duplicate found. Just throw away the new dquot and start
     904             :                  * over.
     905             :                  */
     906         238 :                 xfs_qm_dqdestroy(dqp);
     907         238 :                 XFS_STATS_INC(mp, xs_qm_dquot_dups);
     908         238 :                 goto restart;
     909             :         }
     910             : 
     911    14623154 :         trace_xfs_dqget_miss(dqp);
     912    14623150 :         *O_dqpp = dqp;
     913    14623150 :         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      214453 : 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      214453 :         int                     error;
     930             : 
     931      214453 :         error = xfs_qm_dqget_checks(mp, type);
     932      214453 :         if (error)
     933             :                 return error;
     934             : 
     935      212054 :         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   741859425 : xfs_qm_id_for_quotatype(
     941             :         struct xfs_inode        *ip,
     942             :         xfs_dqtype_t            type)
     943             : {
     944   741859425 :         switch (type) {
     945             :         case XFS_DQTYPE_USER:
     946   247289332 :                 return i_uid_read(VFS_I(ip));
     947             :         case XFS_DQTYPE_GROUP:
     948   247285490 :                 return i_gid_read(VFS_I(ip));
     949   247284603 :         case XFS_DQTYPE_PROJ:
     950   247284603 :                 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    52769475 : 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    52769475 :         struct xfs_mount        *mp = ip->i_mount;
     969    52769475 :         struct xfs_quotainfo    *qi = mp->m_quotainfo;
     970    52769475 :         struct radix_tree_root  *tree = xfs_dquot_tree(qi, type);
     971    52769112 :         struct xfs_dquot        *dqp;
     972    52769112 :         xfs_dqid_t              id;
     973    52769112 :         int                     error;
     974             : 
     975    52769112 :         error = xfs_qm_dqget_checks(mp, type);
     976    52769112 :         if (error)
     977             :                 return error;
     978             : 
     979    52768886 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     980   105537512 :         ASSERT(xfs_inode_dquot(ip, type) == NULL);
     981             : 
     982    52768756 :         id = xfs_qm_id_for_quotatype(ip, type);
     983             : 
     984    52768376 : restart:
     985    52768376 :         dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
     986    52770169 :         if (dqp) {
     987    51649537 :                 *O_dqpp = dqp;
     988    51649537 :                 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     1120632 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     999     1120619 :         error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
    1000     1120600 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1001     1120585 :         if (error)
    1002        4071 :                 return error;
    1003             : 
    1004             :         /*
    1005             :          * A dquot could be attached to this inode by now, since we had
    1006             :          * dropped the ilock.
    1007             :          */
    1008     1116514 :         if (xfs_this_quota_on(mp, type)) {
    1009     1116514 :                 struct xfs_dquot        *dqp1;
    1010             : 
    1011     1116514 :                 dqp1 = xfs_inode_dquot(ip, type);
    1012     1116514 :                 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     1116514 :         error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
    1025     1116565 :         if (error) {
    1026             :                 /*
    1027             :                  * Duplicate found. Just throw away the new dquot and start
    1028             :                  * over.
    1029             :                  */
    1030         147 :                 xfs_qm_dqdestroy(dqp);
    1031         147 :                 XFS_STATS_INC(mp, xs_qm_dquot_dups);
    1032         147 :                 goto restart;
    1033             :         }
    1034             : 
    1035     1116418 : dqret:
    1036     1116418 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
    1037     1116418 :         trace_xfs_dqget_miss(dqp);
    1038     1116418 :         *O_dqpp = dqp;
    1039     1116418 :         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      416323 : 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      416323 :         struct xfs_dquot        *dqp;
    1054      416323 :         int                     error = 0;
    1055             : 
    1056      416323 :         *dqpp = NULL;
    1057    13351706 :         for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
    1058    13348713 :                 error = xfs_qm_dqget(mp, id, type, false, &dqp);
    1059    13348709 :                 if (error == -ENOENT)
    1060      160668 :                         continue;
    1061    13188041 :                 else if (error != 0)
    1062             :                         break;
    1063             : 
    1064    13187957 :                 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
    1065      413242 :                         *dqpp = dqp;
    1066      413242 :                         return 0;
    1067             :                 }
    1068             : 
    1069    12774715 :                 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   576205481 : xfs_qm_dqput(
    1083             :         struct xfs_dquot        *dqp)
    1084             : {
    1085   576205481 :         ASSERT(dqp->q_nrefs > 0);
    1086   576205481 :         ASSERT(XFS_DQ_IS_LOCKED(dqp));
    1087             : 
    1088   576254014 :         trace_xfs_dqput(dqp);
    1089             : 
    1090   576231712 :         if (--dqp->q_nrefs == 0) {
    1091    29397476 :                 struct xfs_quotainfo    *qi = dqp->q_mount->m_quotainfo;
    1092    29397476 :                 trace_xfs_dqput_free(dqp);
    1093             : 
    1094    29397456 :                 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
    1095    15787215 :                         XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
    1096             :         }
    1097   576231830 :         xfs_dqunlock(dqp);
    1098   576566080 : }
    1099             : 
    1100             : /*
    1101             :  * Release a dquot. Flush it if dirty, then dqput() it.
    1102             :  * dquot must not be locked.
    1103             :  */
    1104             : void
    1105   847894491 : xfs_qm_dqrele(
    1106             :         struct xfs_dquot        *dqp)
    1107             : {
    1108   847894491 :         if (!dqp)
    1109             :                 return;
    1110             : 
    1111   494670082 :         trace_xfs_dqrele(dqp);
    1112             : 
    1113   494482242 :         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   494700536 :         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    16581762 : xfs_qm_dqflush_done(
    1132             :         struct xfs_log_item     *lip)
    1133             : {
    1134    16581762 :         struct xfs_dq_logitem   *qip = (struct xfs_dq_logitem *)lip;
    1135    16581762 :         struct xfs_dquot        *dqp = qip->qli_dquot;
    1136    16581762 :         struct xfs_ail          *ailp = lip->li_ailp;
    1137    16581762 :         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    33170043 :         if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
    1148    16560140 :             ((lip->li_lsn == qip->qli_flush_lsn) ||
    1149           0 :              test_bit(XFS_LI_FAILED, &lip->li_flags))) {
    1150             : 
    1151    16547102 :                 spin_lock(&ailp->ail_lock);
    1152    16547102 :                 xfs_clear_li_failed(lip);
    1153    16547102 :                 if (lip->li_lsn == qip->qli_flush_lsn) {
    1154             :                         /* xfs_ail_update_finish() drops the AIL lock */
    1155    16547102 :                         tail_lsn = xfs_ail_delete_one(ailp, lip);
    1156    16547102 :                         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    16581762 :         xfs_dqfunlock(dqp);
    1166    16581762 : }
    1167             : 
    1168             : void
    1169    14679302 : xfs_buf_dquot_iodone(
    1170             :         struct xfs_buf          *bp)
    1171             : {
    1172    14679302 :         struct xfs_log_item     *lip, *n;
    1173             : 
    1174    31261064 :         list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
    1175    16581762 :                 list_del_init(&lip->li_bio_list);
    1176    16581762 :                 xfs_qm_dqflush_done(lip);
    1177             :         }
    1178    14679302 : }
    1179             : 
    1180             : void
    1181         107 : xfs_buf_dquot_io_fail(
    1182             :         struct xfs_buf          *bp)
    1183             : {
    1184         107 :         struct xfs_log_item     *lip;
    1185             : 
    1186         107 :         spin_lock(&bp->b_mount->m_ail->ail_lock);
    1187         214 :         list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
    1188         107 :                 xfs_set_li_failed(lip, bp);
    1189         107 :         spin_unlock(&bp->b_mount->m_ail->ail_lock);
    1190         107 : }
    1191             : 
    1192             : /* Check incore dquot for errors before we flush. */
    1193             : static xfs_failaddr_t
    1194    16581762 : xfs_qm_dqflush_check(
    1195             :         struct xfs_dquot        *dqp)
    1196             : {
    1197    16581762 :         xfs_dqtype_t            type = xfs_dquot_type(dqp);
    1198             : 
    1199    16581762 :         if (type != XFS_DQTYPE_USER &&
    1200    16581762 :             type != XFS_DQTYPE_GROUP &&
    1201             :             type != XFS_DQTYPE_PROJ)
    1202           0 :                 return __this_address;
    1203             : 
    1204    16581762 :         if (dqp->q_id == 0)
    1205             :                 return NULL;
    1206             : 
    1207    16399708 :         if (dqp->q_blk.softlimit && dqp->q_blk.count > dqp->q_blk.softlimit &&
    1208         408 :             !dqp->q_blk.timer)
    1209           0 :                 return __this_address;
    1210             : 
    1211    16399708 :         if (dqp->q_ino.softlimit && dqp->q_ino.count > dqp->q_ino.softlimit &&
    1212         498 :             !dqp->q_ino.timer)
    1213           0 :                 return __this_address;
    1214             : 
    1215    16399708 :         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    16399708 :         if (dqp->q_type & XFS_DQTYPE_BIGTIME) {
    1221    16387018 :                 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    18251818 : xfs_qm_dqflush(
    1240             :         struct xfs_dquot        *dqp,
    1241             :         struct xfs_buf          **bpp)
    1242             : {
    1243    18251818 :         struct xfs_mount        *mp = dqp->q_mount;
    1244    18251818 :         struct xfs_log_item     *lip = &dqp->q_logitem.qli_item;
    1245    18251818 :         struct xfs_buf          *bp;
    1246    18251818 :         struct xfs_dqblk        *dqblk;
    1247    18251818 :         xfs_failaddr_t          fa;
    1248    18251818 :         int                     error;
    1249             : 
    1250    18251818 :         ASSERT(XFS_DQ_IS_LOCKED(dqp));
    1251    18251818 :         ASSERT(!completion_done(&dqp->q_flush));
    1252             : 
    1253    18251818 :         trace_xfs_dqflush(dqp);
    1254             : 
    1255    18251818 :         *bpp = NULL;
    1256             : 
    1257    18251818 :         xfs_qm_dqunpin_wait(dqp);
    1258             : 
    1259             :         /*
    1260             :          * Get the buffer containing the on-disk dquot
    1261             :          */
    1262    18251818 :         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
    1263    18251818 :                                    mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
    1264             :                                    &bp, &xfs_dquot_buf_ops);
    1265    18251818 :         if (error == -EAGAIN)
    1266        4569 :                 goto out_unlock;
    1267    18247249 :         if (xfs_metadata_is_sick(error))
    1268           0 :                 xfs_dquot_mark_sick(dqp);
    1269    18247249 :         if (error)
    1270     1665487 :                 goto out_abort;
    1271             : 
    1272    16581762 :         fa = xfs_qm_dqflush_check(dqp);
    1273    16581762 :         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    16581762 :         dqblk = bp->b_addr + dqp->q_bufoffset;
    1284    16581762 :         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    16581762 :         dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
    1290             : 
    1291    16581762 :         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    16581762 :         if (xfs_has_crc(mp)) {
    1304    16581680 :                 dqblk->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
    1305    16581680 :                 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    16581762 :         bp->b_flags |= _XBF_DQUOTS;
    1314    16581762 :         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    16581762 :         if (xfs_buf_ispinned(bp)) {
    1321       22631 :                 trace_xfs_dqflush_force(dqp);
    1322       22631 :                 xfs_log_force(mp, 0);
    1323             :         }
    1324             : 
    1325    16581762 :         trace_xfs_dqflush_done(dqp);
    1326    16581762 :         *bpp = bp;
    1327    16581762 :         return 0;
    1328             : 
    1329     1665487 : out_abort:
    1330     1665487 :         dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
    1331     1665487 :         xfs_trans_ail_delete(lip, 0);
    1332     1665487 :         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
    1333     1670056 : out_unlock:
    1334     1670056 :         xfs_dqfunlock(dqp);
    1335     1670056 :         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    17969083 : xfs_dqlock2(
    1346             :         struct xfs_dquot        *d1,
    1347             :         struct xfs_dquot        *d2)
    1348             : {
    1349    17969083 :         if (d1 && d2) {
    1350    17969083 :                 ASSERT(d1 != d2);
    1351    17969083 :                 if (d1->q_id > d2->q_id) {
    1352    14871863 :                         mutex_lock(&d2->q_qlock);
    1353    14954430 :                         mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
    1354             :                 } else {
    1355     3097220 :                         mutex_lock(&d1->q_qlock);
    1356     3097290 :                         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    18051743 : }
    1364             : 
    1365             : static int
    1366        8799 : xfs_dqtrx_cmp(
    1367             :         const void              *a,
    1368             :         const void              *b)
    1369             : {
    1370        8799 :         const struct xfs_dqtrx  *qa = a;
    1371        8799 :         const struct xfs_dqtrx  *qb = b;
    1372             : 
    1373        8799 :         if (qa->qt_dquot->q_id > qb->qt_dquot->q_id)
    1374             :                 return 1;
    1375        5520 :         if (qa->qt_dquot->q_id < qb->qt_dquot->q_id)
    1376        5520 :                 return -1;
    1377             :         return 0;
    1378             : }
    1379             : 
    1380             : void
    1381        2933 : xfs_dqlockn(
    1382             :         struct xfs_dqtrx        *q)
    1383             : {
    1384        2933 :         unsigned int            i;
    1385             : 
    1386             :         /* Sort in order of dquot id, do not allow duplicates */
    1387       11732 :         for (i = 0; i < XFS_QM_TRANS_MAXDQS && q[i].qt_dquot != NULL; i++) {
    1388             :                 unsigned int    j;
    1389             : 
    1390       17598 :                 for (j = 0; j < i; j++)
    1391        8799 :                         ASSERT(q[i].qt_dquot != q[j].qt_dquot);
    1392             :         }
    1393        2933 :         if (i == 0)
    1394             :                 return;
    1395             : 
    1396        2933 :         sort(q, i, sizeof(struct xfs_dqtrx), xfs_dqtrx_cmp, NULL);
    1397             : 
    1398        2933 :         mutex_lock(&q[0].qt_dquot->q_qlock);
    1399       11732 :         for (i = 1; i < XFS_QM_TRANS_MAXDQS && q[i].qt_dquot != NULL; i++)
    1400        5866 :                 mutex_lock_nested(&q[i].qt_dquot->q_qlock, XFS_QLOCK_NESTED);
    1401             : }
    1402             : 
    1403             : int __init
    1404          50 : xfs_qm_init(void)
    1405             : {
    1406          50 :         xfs_dquot_cache = kmem_cache_create("xfs_dquot",
    1407             :                                           sizeof(struct xfs_dquot),
    1408             :                                           0, 0, NULL);
    1409          50 :         if (!xfs_dquot_cache)
    1410           0 :                 goto out;
    1411             : 
    1412          50 :         xfs_dqtrx_cache = kmem_cache_create("xfs_dqtrx",
    1413             :                                              sizeof(struct xfs_dquot_acct),
    1414             :                                              0, 0, NULL);
    1415          50 :         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          49 : xfs_qm_exit(void)
    1428             : {
    1429          49 :         kmem_cache_destroy(xfs_dqtrx_cache);
    1430          49 :         kmem_cache_destroy(xfs_dquot_cache);
    1431          49 : }
    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      144310 : 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      144310 :         struct xfs_dquot        *dq;
    1446      144310 :         xfs_dqid_t              id = 0;
    1447      144310 :         int                     error;
    1448             : 
    1449      144310 :         do {
    1450      144310 :                 error = xfs_qm_dqget_next(mp, id, type, &dq);
    1451      144303 :                 if (error == -ENOENT)
    1452             :                         return 0;
    1453      144303 :                 if (error)
    1454           0 :                         return error;
    1455             : 
    1456      144303 :                 error = iter_fn(dq, type, priv);
    1457      144309 :                 id = dq->q_id;
    1458      144309 :                 xfs_qm_dqput(dq);
    1459      144310 :         } while (error == 0 && id != 0);
    1460             : 
    1461             :         return error;
    1462             : }

Generated by: LCOV version 1.14