blob: 9ba21523a481720869400a78d91951cbcf23483c [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott4ce31212005-11-02 14:59:41 +11003 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
7#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +10008#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_log_format.h"
Dave Chinner70a98832013-10-23 10:36:05 +110010#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100014#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_inode.h"
16#include "xfs_bmap.h"
Dave Chinner239880e2013-10-23 10:50:10 +110017#include "xfs_quota.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_buf_item.h"
20#include "xfs_trans_space.h"
21#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000023#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110024#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110025#include "xfs_bmap_btree.h"
Darrick J. Wongafeda602020-07-14 10:36:09 -070026#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
Christoph Hellwigbf72de32011-12-06 21:58:19 +000029 * Lock order:
30 *
31 * ip->i_lock
Christoph Hellwig9f920f12012-03-13 08:52:35 +000032 * qi->qi_tree_lock
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050033 * dquot->q_qlock (xfs_dqlock() and friends)
34 * dquot->q_flush (xfs_dqflock() and friends)
35 * qi->qi_lru_lock
Christoph Hellwigbf72de32011-12-06 21:58:19 +000036 *
37 * If two dquots need to be locked the order is user before group/project,
38 * otherwise by the lowest id first, see xfs_dqlock2.
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Christoph Hellwiga05931c2012-03-13 08:52:37 +000041struct kmem_zone *xfs_qm_dqtrxzone;
42static struct kmem_zone *xfs_qm_dqzone;
43
Dave Chinnerf112a042013-09-30 09:37:03 +100044static struct lock_class_key xfs_dquot_group_class;
45static struct lock_class_key xfs_dquot_project_class;
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * This is called to free all the memory associated with a dquot
49 */
50void
51xfs_qm_dqdestroy(
Pavel Reichlaefe69a2019-11-12 17:04:02 -080052 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Christoph Hellwigf8739c32012-03-13 08:52:34 +000054 ASSERT(list_empty(&dqp->q_lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100056 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 mutex_destroy(&dqp->q_qlock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000058
Bill O'Donnellff6d6af2015-10-12 18:21:22 +110059 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
Carlos Maiolino377bcd52019-11-14 12:43:04 -080060 kmem_cache_free(xfs_qm_dqzone, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * If default limits are in force, push them into the dquot now.
65 * We overwrite the dquot limits only if they are zero and this
66 * is not the root dquot.
67 */
68void
69xfs_qm_adjust_dqlimits(
Brian Foster4b6eae2e2013-03-18 10:51:45 -040070 struct xfs_mount *mp,
71 struct xfs_dquot *dq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Brian Foster4b6eae2e2013-03-18 10:51:45 -040073 struct xfs_quotainfo *q = mp->m_quotainfo;
Carlos Maiolinobe607942016-02-08 11:27:55 +110074 struct xfs_def_quota *defq;
Brian Fosterb1366452013-03-18 10:51:46 -040075 int prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Darrick J. Wongc51df732020-07-14 10:37:30 -070077 ASSERT(dq->q_id);
Eric Sandeence6e7e79c2020-05-21 13:07:00 -070078 defq = xfs_get_defquota(q, xfs_dquot_type(dq));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Darrick J. Wongd3537cf2020-07-14 10:37:31 -070080 if (defq->bsoftlimit && !dq->q_blk.softlimit) {
81 dq->q_blk.softlimit = defq->bsoftlimit;
Brian Fosterb1366452013-03-18 10:51:46 -040082 prealloc = 1;
83 }
Darrick J. Wongd3537cf2020-07-14 10:37:31 -070084 if (defq->bhardlimit && !dq->q_blk.hardlimit) {
85 dq->q_blk.hardlimit = defq->bhardlimit;
Brian Fosterb1366452013-03-18 10:51:46 -040086 prealloc = 1;
87 }
Darrick J. Wongd3537cf2020-07-14 10:37:31 -070088 if (defq->isoftlimit && !dq->q_ino.softlimit)
89 dq->q_ino.softlimit = defq->isoftlimit;
90 if (defq->ihardlimit && !dq->q_ino.hardlimit)
91 dq->q_ino.hardlimit = defq->ihardlimit;
92 if (defq->rtbsoftlimit && !dq->q_rtb.softlimit)
93 dq->q_rtb.softlimit = defq->rtbsoftlimit;
94 if (defq->rtbhardlimit && !dq->q_rtb.hardlimit)
95 dq->q_rtb.hardlimit = defq->rtbhardlimit;
Brian Fosterb1366452013-03-18 10:51:46 -040096
97 if (prealloc)
98 xfs_dquot_set_prealloc_limits(dq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/*
102 * Check the limits and timers of a dquot and start or reset timers
103 * if necessary.
104 * This gets called even when quota enforcement is OFF, which makes our
105 * life a little less complicated. (We just don't reject any quota
106 * reservations in that case, when enforcement is off).
107 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
108 * enforcement's off.
109 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000110 * 'automatically' get started when limits get exceeded. They do
111 * get reset to zero, however, when we find the count to be under
112 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114void
115xfs_qm_adjust_dqtimers(
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800116 struct xfs_mount *mp,
Eric Sandeen3dbb9aa2020-05-21 13:07:00 -0700117 struct xfs_dquot *dq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Eric Sandeene8503012020-05-21 13:07:01 -0700119 struct xfs_quotainfo *qi = mp->m_quotainfo;
Eric Sandeen3dbb9aa2020-05-21 13:07:00 -0700120 struct xfs_disk_dquot *d = &dq->q_core;
Eric Sandeene8503012020-05-21 13:07:01 -0700121 struct xfs_def_quota *defq;
122
Darrick J. Wongc51df732020-07-14 10:37:30 -0700123 ASSERT(dq->q_id);
Eric Sandeene8503012020-05-21 13:07:01 -0700124 defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200126#ifdef DEBUG
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700127 if (dq->q_blk.hardlimit)
128 ASSERT(dq->q_blk.softlimit <= dq->q_blk.hardlimit);
129 if (dq->q_ino.hardlimit)
130 ASSERT(dq->q_ino.softlimit <= dq->q_ino.hardlimit);
131 if (dq->q_rtb.hardlimit)
132 ASSERT(dq->q_rtb.softlimit <= dq->q_rtb.hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!d->d_btimer) {
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700136 if ((dq->q_blk.softlimit &&
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700137 (dq->q_blk.count > dq->q_blk.softlimit)) ||
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700138 (dq->q_blk.hardlimit &&
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700139 (dq->q_blk.count > dq->q_blk.hardlimit))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800140 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700141 defq->btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000142 } else {
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700143 dq->q_blk.warnings = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 } else {
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700146 if ((!dq->q_blk.softlimit ||
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700147 (dq->q_blk.count <= dq->q_blk.softlimit)) &&
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700148 (!dq->q_blk.hardlimit ||
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700149 (dq->q_blk.count <= dq->q_blk.hardlimit))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 d->d_btimer = 0;
151 }
152 }
153
154 if (!d->d_itimer) {
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700155 if ((dq->q_ino.softlimit &&
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700156 (dq->q_ino.count > dq->q_ino.softlimit)) ||
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700157 (dq->q_ino.hardlimit &&
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700158 (dq->q_ino.count > dq->q_ino.hardlimit))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800159 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700160 defq->itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000161 } else {
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700162 dq->q_ino.warnings = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 } else {
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700165 if ((!dq->q_ino.softlimit ||
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700166 (dq->q_ino.count <= dq->q_ino.softlimit)) &&
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700167 (!dq->q_ino.hardlimit ||
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700168 (dq->q_ino.count <= dq->q_ino.hardlimit))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 d->d_itimer = 0;
170 }
171 }
172
173 if (!d->d_rtbtimer) {
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700174 if ((dq->q_rtb.softlimit &&
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700175 (dq->q_rtb.count > dq->q_rtb.softlimit)) ||
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700176 (dq->q_rtb.hardlimit &&
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700177 (dq->q_rtb.count > dq->q_rtb.hardlimit))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800178 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700179 defq->rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000180 } else {
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700181 dq->q_rtb.warnings = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 } else {
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700184 if ((!dq->q_rtb.softlimit ||
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700185 (dq->q_rtb.count <= dq->q_rtb.softlimit)) &&
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700186 (!dq->q_rtb.hardlimit ||
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700187 (dq->q_rtb.count <= dq->q_rtb.hardlimit))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 d->d_rtbtimer = 0;
189 }
190 }
191}
192
193/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * initialize a buffer full of dquots and log the whole thing
195 */
196STATIC void
197xfs_qm_init_dquot_blk(
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700198 struct xfs_trans *tp,
199 struct xfs_mount *mp,
200 xfs_dqid_t id,
201 uint type,
202 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000204 struct xfs_quotainfo *q = mp->m_quotainfo;
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700205 struct xfs_dqblk *d;
206 xfs_dqid_t curid;
207 unsigned int qflag;
208 unsigned int blftype;
209 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200212 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Chandra Seetharaman62926042011-07-22 23:40:15 +0000214 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /*
217 * ID of the first dquot in the block - id's are zero based.
218 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000219 curid = id - (id % q->qi_dqperchunk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000220 memset(d, 0, BBTOB(q->qi_dqchunklen));
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000221 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
222 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
223 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
224 d->dd_diskdq.d_id = cpu_to_be32(curid);
225 d->dd_diskdq.d_flags = type;
Dave Chinner6fcdc592013-06-03 15:28:46 +1000226 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner92863452015-08-19 10:32:01 +1000227 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000228 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
229 XFS_DQUOT_CRC_OFF);
230 }
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000231 }
232
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700233 if (type & XFS_DQ_USER) {
234 qflag = XFS_UQUOTA_CHKD;
235 blftype = XFS_BLF_UDQUOT_BUF;
236 } else if (type & XFS_DQ_PROJ) {
237 qflag = XFS_PQUOTA_CHKD;
238 blftype = XFS_BLF_PDQUOT_BUF;
239 } else {
240 qflag = XFS_GQUOTA_CHKD;
241 blftype = XFS_BLF_GDQUOT_BUF;
242 }
243
244 xfs_trans_dquot_buf(tp, bp, blftype);
245
246 /*
247 * quotacheck uses delayed writes to update all the dquots on disk in an
248 * efficient manner instead of logging the individual dquot changes as
249 * they are made. However if we log the buffer allocated here and crash
250 * after quotacheck while the logged initialisation is still in the
251 * active region of the log, log recovery can replay the dquot buffer
252 * initialisation over the top of the checked dquots and corrupt quota
253 * accounting.
254 *
255 * To avoid this problem, quotacheck cannot log the initialised buffer.
256 * We must still dirty the buffer and write it back before the
257 * allocation transaction clears the log. Therefore, mark the buffer as
258 * ordered instead of logging it directly. This is safe for quotacheck
259 * because it detects and repairs allocated but initialized dquot blocks
260 * in the quota inodes.
261 */
262 if (!(mp->m_qflags & qflag))
263 xfs_trans_ordered_buf(tp, bp);
264 else
265 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Brian Fosterb1366452013-03-18 10:51:46 -0400268/*
269 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
270 * watermarks correspond to the soft and hard limits by default. If a soft limit
271 * is not specified, we use 95% of the hard limit.
272 */
273void
274xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
275{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700276 uint64_t space;
Brian Fosterb1366452013-03-18 10:51:46 -0400277
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700278 dqp->q_prealloc_hi_wmark = dqp->q_blk.hardlimit;
279 dqp->q_prealloc_lo_wmark = dqp->q_blk.softlimit;
Brian Fosterb1366452013-03-18 10:51:46 -0400280 if (!dqp->q_prealloc_lo_wmark) {
281 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
282 do_div(dqp->q_prealloc_lo_wmark, 100);
283 dqp->q_prealloc_lo_wmark *= 95;
284 }
285
286 space = dqp->q_prealloc_hi_wmark;
287
288 do_div(space, 100);
289 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
290 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
291 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700295 * Ensure that the given in-core dquot has a buffer on disk backing it, and
Darrick J. Wong710d7072019-04-24 09:27:41 -0700296 * return the buffer locked and held. This is called when the bmapi finds a
297 * hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700300xfs_dquot_disk_alloc(
301 struct xfs_trans **tpp,
302 struct xfs_dquot *dqp,
303 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700305 struct xfs_bmbt_irec map;
Brian Foster2ba13722018-07-11 22:26:11 -0700306 struct xfs_trans *tp = *tpp;
307 struct xfs_mount *mp = tp->t_mountp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700308 struct xfs_buf *bp;
309 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700310 int nmaps = 1;
311 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000312
313 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 xfs_ilock(quotip, XFS_ILOCK_EXCL);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000316 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700317 /*
318 * Return if this type of quotas is turned off while we didn't
319 * have an inode lock
320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
Dave Chinner24513372014-06-25 14:58:08 +1000322 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700325 /* Create the block mapping. */
Brian Foster2ba13722018-07-11 22:26:11 -0700326 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
327 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
Brian Fosterda781e62019-10-21 09:26:48 -0700328 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
329 &nmaps);
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000330 if (error)
Brian Foster73971b12018-08-07 10:57:13 -0700331 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
333 ASSERT(nmaps == 1);
334 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
335 (map.br_startblock != HOLESTARTBLOCK));
336
337 /*
338 * Keep track of the blkno to save a lookup later
339 */
340 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
341
342 /* now we can just get the buffer (there's nothing to read yet) */
Darrick J. Wongce924642020-01-23 17:01:18 -0800343 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
344 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
345 if (error)
346 return error;
Dave Chinner1813dd62012-11-14 17:54:40 +1100347 bp->b_ops = &xfs_dquot_buf_ops;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * Make a chunk of dquots out of this buffer and log
351 * the entire thing.
352 */
Darrick J. Wongc51df732020-07-14 10:37:30 -0700353 xfs_qm_init_dquot_blk(tp, mp, dqp->q_id,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000354 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700355 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Tim Shimminefa092f2005-09-05 08:29:01 +1000357 /*
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700358 * Hold the buffer and join it to the dfops so that we'll still own
359 * the buffer when we return to the caller. The buffer disposal on
360 * error must be paid attention to very carefully, as it has been
361 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
362 * code when allocating a new dquot record" in 2005, and the later
363 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
364 * the buffer locked across the _defer_finish call. We can now do
365 * this correctly with xfs_defer_bjoin.
Tim Shimminefa092f2005-09-05 08:29:01 +1000366 *
Brian Foster73971b12018-08-07 10:57:13 -0700367 * Above, we allocated a disk block for the dquot information and used
368 * get_buf to initialize the dquot. If the _defer_finish fails, the old
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700369 * transaction is gone but the new buffer is not joined or held to any
370 * transaction, so we must _buf_relse it.
Tim Shimminefa092f2005-09-05 08:29:01 +1000371 *
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700372 * If everything succeeds, the caller of this function is returned a
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700373 * buffer that is locked and held to the transaction. The caller
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700374 * is responsible for unlocking any buffer passed back, either
Darrick J. Wong710d7072019-04-24 09:27:41 -0700375 * manually or by committing the transaction. On error, the buffer is
376 * released and not passed back.
Tim Shimminefa092f2005-09-05 08:29:01 +1000377 */
Brian Foster2ba13722018-07-11 22:26:11 -0700378 xfs_trans_bhold(tp, bp);
Brian Foster9e28a242018-07-24 13:43:15 -0700379 error = xfs_defer_finish(tpp);
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700380 if (error) {
Darrick J. Wong710d7072019-04-24 09:27:41 -0700381 xfs_trans_bhold_release(*tpp, bp);
382 xfs_trans_brelse(*tpp, bp);
Brian Foster73971b12018-08-07 10:57:13 -0700383 return error;
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700384 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700385 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Dave Chinner9aede1d2013-10-15 09:17:52 +1100388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700390 * Read in the in-core dquot's on-disk metadata and return the buffer.
391 * Returns ENOENT to signal a hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
393STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700394xfs_dquot_disk_read(
395 struct xfs_mount *mp,
396 struct xfs_dquot *dqp,
397 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500399 struct xfs_bmbt_irec map;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500400 struct xfs_buf *bp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700401 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700402 uint lock_mode;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700403 int nmaps = 1;
404 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Christoph Hellwig0891f992017-07-13 12:14:34 -0700406 lock_mode = xfs_ilock_data_map_shared(quotip);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700407 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000409 * Return if this type of quotas is turned off while we
410 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
Christoph Hellwig0891f992017-07-13 12:14:34 -0700412 xfs_iunlock(quotip, lock_mode);
Dave Chinner24513372014-06-25 14:58:08 +1000413 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000417 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000419 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700420 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700421 xfs_iunlock(quotip, lock_mode);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000422 if (error)
423 return error;
424
425 ASSERT(nmaps == 1);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700426 ASSERT(map.br_blockcount >= 1);
427 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
428 if (map.br_startblock == HOLESTARTBLOCK)
429 return -ENOENT;
430
431 trace_xfs_dqtobp_read(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000432
433 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700434 * store the blkno etc so that we don't have to do the
435 * mapping all the time
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000436 */
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700437 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000438
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700439 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
440 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
441 &xfs_dquot_buf_ops);
442 if (error) {
443 ASSERT(bp == NULL);
444 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Dave Chinnerc6319192012-11-14 17:50:13 +1100447 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700448 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
449 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Eric Sandeend99831f2014-06-22 15:03:54 +1000451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700454/* Allocate and initialize everything we need for an incore dquot. */
455STATIC struct xfs_dquot *
456xfs_dquot_alloc(
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000457 struct xfs_mount *mp,
458 xfs_dqid_t id,
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700459 uint type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000461 struct xfs_dquot *dqp;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000462
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700463 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000464
465 dqp->dq_flags = type;
Darrick J. Wongc51df732020-07-14 10:37:30 -0700466 dqp->q_id = id;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000467 dqp->q_mount = mp;
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000468 INIT_LIST_HEAD(&dqp->q_lru);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000469 mutex_init(&dqp->q_qlock);
470 init_waitqueue_head(&dqp->q_pinwait);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700471 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
472 /*
473 * Offset of dquot in the (fixed sized) dquot chunk.
474 */
475 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
476 sizeof(xfs_dqblk_t);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000477
478 /*
479 * Because we want to use a counting completion, complete
480 * the flush completion once to allow a single access to
481 * the flush completion without blocking.
482 */
483 init_completion(&dqp->q_flush);
484 complete(&dqp->q_flush);
485
486 /*
487 * Make sure group quotas have a different lock class than user
488 * quotas.
489 */
Dave Chinnerf112a042013-09-30 09:37:03 +1000490 switch (type) {
491 case XFS_DQ_USER:
492 /* uses the default lock class */
493 break;
494 case XFS_DQ_GROUP:
495 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
496 break;
497 case XFS_DQ_PROJ:
498 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
499 break;
500 default:
501 ASSERT(0);
502 break;
503 }
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000504
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700505 xfs_qm_dquot_logitem_init(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700507 XFS_STATS_INC(mp, xs_qm_dquot);
508 return dqp;
509}
510
511/* Copy the in-core quota fields in from the on-disk buffer. */
Darrick J. Wongafeda602020-07-14 10:36:09 -0700512STATIC int
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700513xfs_dquot_from_disk(
514 struct xfs_dquot *dqp,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700515 struct xfs_buf *bp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700516{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700517 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
518
Darrick J. Wongafeda602020-07-14 10:36:09 -0700519 /*
520 * Ensure that we got the type and ID we were looking for.
521 * Everything else was checked by the dquot buffer verifier.
522 */
523 if ((ddqp->d_flags & XFS_DQ_ALLTYPES) != dqp->dq_flags ||
Darrick J. Wongc51df732020-07-14 10:37:30 -0700524 be32_to_cpu(ddqp->d_id) != dqp->q_id) {
Darrick J. Wongafeda602020-07-14 10:36:09 -0700525 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR,
526 "Metadata corruption detected at %pS, quota %u",
Darrick J. Wongc51df732020-07-14 10:37:30 -0700527 __this_address, dqp->q_id);
Darrick J. Wongafeda602020-07-14 10:36:09 -0700528 xfs_alert(bp->b_mount, "Unmount and run xfs_repair");
529 return -EFSCORRUPTED;
530 }
531
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700532 /* copy everything from disk dquot to the incore dquot */
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800533 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot));
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700534 dqp->q_blk.hardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
535 dqp->q_blk.softlimit = be64_to_cpu(ddqp->d_blk_softlimit);
536 dqp->q_ino.hardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
537 dqp->q_ino.softlimit = be64_to_cpu(ddqp->d_ino_softlimit);
538 dqp->q_rtb.hardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
539 dqp->q_rtb.softlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700540
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700541 dqp->q_blk.count = be64_to_cpu(ddqp->d_bcount);
542 dqp->q_ino.count = be64_to_cpu(ddqp->d_icount);
543 dqp->q_rtb.count = be64_to_cpu(ddqp->d_rtbcount);
544
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700545 dqp->q_blk.warnings = be16_to_cpu(ddqp->d_bwarns);
546 dqp->q_ino.warnings = be16_to_cpu(ddqp->d_iwarns);
547 dqp->q_rtb.warnings = be16_to_cpu(ddqp->d_rtbwarns);
548
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700549 /*
550 * Reservation counters are defined as reservation plus current usage
551 * to avoid having to add every time.
552 */
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700553 dqp->q_blk.reserved = dqp->q_blk.count;
554 dqp->q_ino.reserved = dqp->q_ino.count;
555 dqp->q_rtb.reserved = dqp->q_rtb.count;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700556
557 /* initialize the dquot speculative prealloc thresholds */
558 xfs_dquot_set_prealloc_limits(dqp);
Darrick J. Wongafeda602020-07-14 10:36:09 -0700559 return 0;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700560}
561
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -0700562/* Copy the in-core quota fields into the on-disk buffer. */
563void
564xfs_dquot_to_disk(
565 struct xfs_disk_dquot *ddqp,
566 struct xfs_dquot *dqp)
567{
568 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700569 ddqp->d_blk_hardlimit = cpu_to_be64(dqp->q_blk.hardlimit);
570 ddqp->d_blk_softlimit = cpu_to_be64(dqp->q_blk.softlimit);
571 ddqp->d_ino_hardlimit = cpu_to_be64(dqp->q_ino.hardlimit);
572 ddqp->d_ino_softlimit = cpu_to_be64(dqp->q_ino.softlimit);
573 ddqp->d_rtb_hardlimit = cpu_to_be64(dqp->q_rtb.hardlimit);
574 ddqp->d_rtb_softlimit = cpu_to_be64(dqp->q_rtb.softlimit);
Darrick J. Wongbe37d402020-07-14 10:37:31 -0700575
576 ddqp->d_bcount = cpu_to_be64(dqp->q_blk.count);
577 ddqp->d_icount = cpu_to_be64(dqp->q_ino.count);
578 ddqp->d_rtbcount = cpu_to_be64(dqp->q_rtb.count);
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700579
580 ddqp->d_bwarns = cpu_to_be16(dqp->q_blk.warnings);
581 ddqp->d_iwarns = cpu_to_be16(dqp->q_ino.warnings);
582 ddqp->d_rtbwarns = cpu_to_be16(dqp->q_rtb.warnings);
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -0700583}
584
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700585/* Allocate and initialize the dquot buffer for this in-core dquot. */
586static int
587xfs_qm_dqread_alloc(
588 struct xfs_mount *mp,
589 struct xfs_dquot *dqp,
590 struct xfs_buf **bpp)
591{
592 struct xfs_trans *tp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700593 int error;
594
595 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
596 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
597 if (error)
598 goto err;
599
Darrick J. Wong710d7072019-04-24 09:27:41 -0700600 error = xfs_dquot_disk_alloc(&tp, dqp, bpp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700601 if (error)
602 goto err_cancel;
603
604 error = xfs_trans_commit(tp);
605 if (error) {
606 /*
607 * Buffer was held to the transaction, so we have to unlock it
608 * manually here because we're not passing it back.
609 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700610 xfs_buf_relse(*bpp);
611 *bpp = NULL;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700612 goto err;
613 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700614 return 0;
615
616err_cancel:
617 xfs_trans_cancel(tp);
618err:
619 return error;
620}
621
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700622/*
623 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700624 * and release the buffer immediately. If @can_alloc is true, fill any
625 * holes in the on-disk metadata.
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700626 */
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700627static int
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700628xfs_qm_dqread(
629 struct xfs_mount *mp,
630 xfs_dqid_t id,
631 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700632 bool can_alloc,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700633 struct xfs_dquot **dqpp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700634{
635 struct xfs_dquot *dqp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700636 struct xfs_buf *bp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700637 int error;
638
639 dqp = xfs_dquot_alloc(mp, id, type);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000640 trace_xfs_dqread(dqp);
641
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700642 /* Try to read the buffer, allocating if necessary. */
643 error = xfs_dquot_disk_read(mp, dqp, &bp);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700644 if (error == -ENOENT && can_alloc)
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700645 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
646 if (error)
647 goto err;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700650 * At this point we should have a clean locked buffer. Copy the data
651 * to the incore dquot and release the buffer since the incore dquot
652 * has its own locking protocol so we needn't tie up the buffer any
653 * further.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200655 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongafeda602020-07-14 10:36:09 -0700656 error = xfs_dquot_from_disk(dqp, bp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700657 xfs_buf_relse(bp);
Darrick J. Wongafeda602020-07-14 10:36:09 -0700658 if (error)
659 goto err;
660
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700661 *dqpp = dqp;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000662 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700664err:
665 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 xfs_qm_dqdestroy(dqp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700667 *dqpp = NULL;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000668 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/*
Eric Sandeen296c24e2016-02-08 11:27:38 +1100672 * Advance to the next id in the current chunk, or if at the
673 * end of the chunk, skip ahead to first id in next allocated chunk
674 * using the SEEK_DATA interface.
675 */
Eryu Guan6e3e6d52016-04-06 09:47:21 +1000676static int
Eric Sandeen296c24e2016-02-08 11:27:38 +1100677xfs_dq_get_next_id(
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700678 struct xfs_mount *mp,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100679 uint type,
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700680 xfs_dqid_t *id)
Eric Sandeen296c24e2016-02-08 11:27:38 +1100681{
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700682 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
683 xfs_dqid_t next_id = *id + 1; /* simple advance */
684 uint lock_flags;
685 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700686 struct xfs_iext_cursor cur;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100687 xfs_fsblock_t start;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100688 int error = 0;
689
Eric Sandeen657bdfb2017-01-17 11:43:38 -0800690 /* If we'd wrap past the max ID, stop */
691 if (next_id < *id)
692 return -ENOENT;
693
Eric Sandeen296c24e2016-02-08 11:27:38 +1100694 /* If new ID is within the current chunk, advancing it sufficed */
695 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
696 *id = next_id;
697 return 0;
698 }
699
700 /* Nope, next_id is now past the current chunk, so find the next one */
701 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
702
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700703 lock_flags = xfs_ilock_data_map_shared(quotip);
704 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
705 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
706 if (error)
707 return error;
708 }
Eric Sandeen296c24e2016-02-08 11:27:38 +1100709
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700710 if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
Brian Foster2192b0b2017-07-05 12:07:52 -0700711 /* contiguous chunk, bump startoff for the id calculation */
712 if (got.br_startoff < start)
713 got.br_startoff = start;
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700714 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
Brian Foster2192b0b2017-07-05 12:07:52 -0700715 } else {
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700716 error = -ENOENT;
Brian Foster2192b0b2017-07-05 12:07:52 -0700717 }
718
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700719 xfs_iunlock(quotip, lock_flags);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100720
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700721 return error;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100722}
723
724/*
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700725 * Look up the dquot in the in-core cache. If found, the dquot is returned
726 * locked and ready to go.
727 */
728static struct xfs_dquot *
729xfs_qm_dqget_cache_lookup(
730 struct xfs_mount *mp,
731 struct xfs_quotainfo *qi,
732 struct radix_tree_root *tree,
733 xfs_dqid_t id)
734{
735 struct xfs_dquot *dqp;
736
737restart:
738 mutex_lock(&qi->qi_tree_lock);
739 dqp = radix_tree_lookup(tree, id);
740 if (!dqp) {
741 mutex_unlock(&qi->qi_tree_lock);
742 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
743 return NULL;
744 }
745
746 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700747 if (dqp->q_flags & XFS_DQFLAG_FREEING) {
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700748 xfs_dqunlock(dqp);
749 mutex_unlock(&qi->qi_tree_lock);
750 trace_xfs_dqget_freeing(dqp);
751 delay(1);
752 goto restart;
753 }
754
755 dqp->q_nrefs++;
756 mutex_unlock(&qi->qi_tree_lock);
757
758 trace_xfs_dqget_hit(dqp);
759 XFS_STATS_INC(mp, xs_qm_dqcachehits);
760 return dqp;
761}
762
763/*
764 * Try to insert a new dquot into the in-core cache. If an error occurs the
765 * caller should throw away the dquot and start over. Otherwise, the dquot
766 * is returned locked (and held by the cache) as if there had been a cache
767 * hit.
768 */
769static int
770xfs_qm_dqget_cache_insert(
771 struct xfs_mount *mp,
772 struct xfs_quotainfo *qi,
773 struct radix_tree_root *tree,
774 xfs_dqid_t id,
775 struct xfs_dquot *dqp)
776{
777 int error;
778
779 mutex_lock(&qi->qi_tree_lock);
780 error = radix_tree_insert(tree, id, dqp);
781 if (unlikely(error)) {
782 /* Duplicate found! Caller must try again. */
783 WARN_ON(error != -EEXIST);
784 mutex_unlock(&qi->qi_tree_lock);
785 trace_xfs_dqget_dup(dqp);
786 return error;
787 }
788
789 /* Return a locked dquot to the caller, with a reference taken. */
790 xfs_dqlock(dqp);
791 dqp->q_nrefs = 1;
792
793 qi->qi_dquots++;
794 mutex_unlock(&qi->qi_tree_lock);
795
796 return 0;
797}
798
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700799/* Check our input parameters. */
800static int
801xfs_qm_dqget_checks(
802 struct xfs_mount *mp,
803 uint type)
804{
805 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
806 return -ESRCH;
807
808 switch (type) {
809 case XFS_DQ_USER:
810 if (!XFS_IS_UQUOTA_ON(mp))
811 return -ESRCH;
812 return 0;
813 case XFS_DQ_GROUP:
814 if (!XFS_IS_GQUOTA_ON(mp))
815 return -ESRCH;
816 return 0;
817 case XFS_DQ_PROJ:
818 if (!XFS_IS_PQUOTA_ON(mp))
819 return -ESRCH;
820 return 0;
821 default:
822 WARN_ON_ONCE(0);
823 return -EINVAL;
824 }
825}
826
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700827/*
Darrick J. Wong4882c192018-05-04 15:30:22 -0700828 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
829 * dquot, doing an allocation (if requested) as needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
831int
832xfs_qm_dqget(
Darrick J. Wong4882c192018-05-04 15:30:22 -0700833 struct xfs_mount *mp,
834 xfs_dqid_t id,
835 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700836 bool can_alloc,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700837 struct xfs_dquot **O_dqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000839 struct xfs_quotainfo *qi = mp->m_quotainfo;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700840 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000841 struct xfs_dquot *dqp;
842 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700844 error = xfs_qm_dqget_checks(mp, type);
845 if (error)
846 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Darrick J. Wong4882c192018-05-04 15:30:22 -0700848restart:
849 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
850 if (dqp) {
851 *O_dqpp = dqp;
852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Christoph Hellwig92678552011-12-06 21:58:18 +0000854
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700855 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700856 if (error)
857 return error;
858
859 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
860 if (error) {
861 /*
862 * Duplicate found. Just throw away the new dquot and start
863 * over.
864 */
865 xfs_qm_dqdestroy(dqp);
866 XFS_STATS_INC(mp, xs_qm_dquot_dups);
867 goto restart;
868 }
869
870 trace_xfs_dqget_miss(dqp);
871 *O_dqpp = dqp;
872 return 0;
873}
874
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700875/*
876 * Given a dquot id and type, read and initialize a dquot from the on-disk
877 * metadata. This function is only for use during quota initialization so
878 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
879 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
880 */
881int
882xfs_qm_dqget_uncached(
883 struct xfs_mount *mp,
884 xfs_dqid_t id,
885 uint type,
886 struct xfs_dquot **dqpp)
887{
888 int error;
889
890 error = xfs_qm_dqget_checks(mp, type);
891 if (error)
892 return error;
893
894 return xfs_qm_dqread(mp, id, type, 0, dqpp);
895}
896
Darrick J. Wong4882c192018-05-04 15:30:22 -0700897/* Return the quota id for a given inode and type. */
898xfs_dqid_t
899xfs_qm_id_for_quotatype(
900 struct xfs_inode *ip,
901 uint type)
902{
903 switch (type) {
904 case XFS_DQ_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800905 return i_uid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700906 case XFS_DQ_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800907 return i_gid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700908 case XFS_DQ_PROJ:
Christoph Hellwigde7a8662019-11-12 08:22:54 -0800909 return ip->i_d.di_projid;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700910 }
911 ASSERT(0);
912 return 0;
913}
914
915/*
916 * Return the dquot for a given inode and type. If @can_alloc is true, then
917 * allocate blocks if needed. The inode's ILOCK must be held and it must not
918 * have already had an inode attached.
919 */
920int
921xfs_qm_dqget_inode(
922 struct xfs_inode *ip,
923 uint type,
924 bool can_alloc,
925 struct xfs_dquot **O_dqpp)
926{
927 struct xfs_mount *mp = ip->i_mount;
928 struct xfs_quotainfo *qi = mp->m_quotainfo;
929 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
930 struct xfs_dquot *dqp;
931 xfs_dqid_t id;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700932 int error;
933
934 error = xfs_qm_dqget_checks(mp, type);
935 if (error)
936 return error;
937
Darrick J. Wong4882c192018-05-04 15:30:22 -0700938 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
939 ASSERT(xfs_inode_dquot(ip, type) == NULL);
940
941 id = xfs_qm_id_for_quotatype(ip, type);
942
Christoph Hellwig92678552011-12-06 21:58:18 +0000943restart:
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700944 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000945 if (dqp) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000946 *O_dqpp = dqp;
947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
951 * Dquot cache miss. We don't want to keep the inode lock across
952 * a (potential) disk read. Also we don't want to deal with the lock
953 * ordering between quotainode and this inode. OTOH, dropping the inode
954 * lock here means dealing with a chown that can happen before
955 * we re-acquire the lock.
956 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700957 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700958 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700959 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000960 if (error)
961 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Darrick J. Wong4882c192018-05-04 15:30:22 -0700963 /*
964 * A dquot could be attached to this inode by now, since we had
965 * dropped the ilock.
966 */
967 if (xfs_this_quota_on(mp, type)) {
968 struct xfs_dquot *dqp1;
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000969
Darrick J. Wong4882c192018-05-04 15:30:22 -0700970 dqp1 = xfs_inode_dquot(ip, type);
971 if (dqp1) {
Chandra Seetharaman36731412012-01-23 17:31:30 +0000972 xfs_qm_dqdestroy(dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700973 dqp = dqp1;
974 xfs_dqlock(dqp);
975 goto dqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Darrick J. Wong4882c192018-05-04 15:30:22 -0700977 } else {
978 /* inode stays locked on return */
979 xfs_qm_dqdestroy(dqp);
980 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700983 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
984 if (error) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000985 /*
986 * Duplicate found. Just throw away the new dquot and start
987 * over.
988 */
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000989 xfs_qm_dqdestroy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100990 XFS_STATS_INC(mp, xs_qm_dquot_dups);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000991 goto restart;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Darrick J. Wong4882c192018-05-04 15:30:22 -0700994dqret:
995 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000996 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 *O_dqpp = dqp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001001/*
Darrick J. Wong2e330e72018-05-04 15:30:20 -07001002 * Starting at @id and progressing upwards, look for an initialized incore
1003 * dquot, lock it, and return it.
1004 */
1005int
1006xfs_qm_dqget_next(
1007 struct xfs_mount *mp,
1008 xfs_dqid_t id,
1009 uint type,
1010 struct xfs_dquot **dqpp)
1011{
1012 struct xfs_dquot *dqp;
1013 int error = 0;
1014
1015 *dqpp = NULL;
1016 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -07001017 error = xfs_qm_dqget(mp, id, type, false, &dqp);
Darrick J. Wong2e330e72018-05-04 15:30:20 -07001018 if (error == -ENOENT)
1019 continue;
1020 else if (error != 0)
1021 break;
1022
1023 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
1024 *dqpp = dqp;
1025 return 0;
1026 }
1027
1028 xfs_qm_dqput(dqp);
1029 }
1030
1031 return error;
1032}
1033
1034/*
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001035 * Release a reference to the dquot (decrement ref-count) and unlock it.
1036 *
1037 * If there is a group quota attached to this dquot, carefully release that
1038 * too without tripping over deadlocks'n'stuff.
1039 */
1040void
1041xfs_qm_dqput(
1042 struct xfs_dquot *dqp)
1043{
1044 ASSERT(dqp->q_nrefs > 0);
1045 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1046
1047 trace_xfs_dqput(dqp);
1048
Dave Chinner3c35337572014-05-05 17:30:15 +10001049 if (--dqp->q_nrefs == 0) {
1050 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1051 trace_xfs_dqput_free(dqp);
1052
1053 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001054 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner3c35337572014-05-05 17:30:15 +10001055 }
1056 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059/*
1060 * Release a dquot. Flush it if dirty, then dqput() it.
1061 * dquot must not be locked.
1062 */
1063void
1064xfs_qm_dqrele(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001065 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001067 if (!dqp)
1068 return;
1069
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001070 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 xfs_dqlock(dqp);
1073 /*
1074 * We don't care to flush it if the dquot is dirty here.
1075 * That will create stutters that we want to avoid.
1076 * Instead we do a delayed write when we try to reclaim
1077 * a dirty dquot. Also xfs_sync will take part of the burden...
1078 */
1079 xfs_qm_dqput(dqp);
1080}
1081
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001082/*
1083 * This is the dquot flushing I/O completion routine. It is called
1084 * from interrupt level when the buffer containing the dquot is
1085 * flushed to disk. It is responsible for removing the dquot logitem
1086 * from the AIL if it has not been re-logged, and unlocking the dquot's
1087 * flush lock. This behavior is very similar to that of inodes..
1088 */
Dave Chinner6f5de182020-06-29 14:48:59 -07001089static void
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001090xfs_qm_dqflush_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001091 struct xfs_log_item *lip)
1092{
Pavel Reichlfd8b81d2019-11-12 17:04:26 -08001093 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001094 struct xfs_dquot *dqp = qip->qli_dquot;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001095 struct xfs_ail *ailp = lip->li_ailp;
Brian Foster849274c2020-05-06 13:25:23 -07001096 xfs_lsn_t tail_lsn;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001097
1098 /*
1099 * We only want to pull the item from the AIL if its
1100 * location in the log has not changed since we started the flush.
1101 * Thus, we only bother if the dquot's lsn has
1102 * not changed. First we check the lsn outside the lock
1103 * since it's cheaper, and then we recheck while
1104 * holding the lock before removing the dquot from the AIL.
1105 */
Dave Chinner22525c12018-05-09 07:47:34 -07001106 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
Carlos Maiolino373b0582017-11-28 08:54:10 -08001107 ((lip->li_lsn == qip->qli_flush_lsn) ||
Dave Chinner22525c12018-05-09 07:47:34 -07001108 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001109
Matthew Wilcox57e80952018-03-07 14:59:39 -08001110 spin_lock(&ailp->ail_lock);
Dave Chinnere98084b2020-06-29 14:49:15 -07001111 xfs_clear_li_failed(lip);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001112 if (lip->li_lsn == qip->qli_flush_lsn) {
Brian Foster849274c2020-05-06 13:25:23 -07001113 /* xfs_ail_update_finish() drops the AIL lock */
1114 tail_lsn = xfs_ail_delete_one(ailp, lip);
1115 xfs_ail_update_finish(ailp, tail_lsn);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001116 } else {
Matthew Wilcox57e80952018-03-07 14:59:39 -08001117 spin_unlock(&ailp->ail_lock);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001118 }
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001119 }
1120
1121 /*
1122 * Release the dq's flush lock since we're done with it.
1123 */
1124 xfs_dqfunlock(dqp);
1125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Dave Chinner6f5de182020-06-29 14:48:59 -07001127void
1128xfs_dquot_done(
1129 struct xfs_buf *bp)
1130{
1131 struct xfs_log_item *lip, *n;
1132
1133 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1134 list_del_init(&lip->li_bio_list);
1135 xfs_qm_dqflush_done(lip);
1136 }
1137}
1138
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -07001139/* Check incore dquot for errors before we flush. */
1140static xfs_failaddr_t
1141xfs_qm_dqflush_check(
1142 struct xfs_dquot *dqp)
1143{
Darrick J. Wongd3537cf2020-07-14 10:37:31 -07001144 struct xfs_disk_dquot *ddq = &dqp->q_core;
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -07001145 __u8 type = dqp->dq_flags & XFS_DQ_ALLTYPES;
1146
1147 if (type != XFS_DQ_USER &&
1148 type != XFS_DQ_GROUP &&
1149 type != XFS_DQ_PROJ)
1150 return __this_address;
1151
Darrick J. Wongd3537cf2020-07-14 10:37:31 -07001152 if (dqp->q_id == 0)
1153 return NULL;
1154
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001155 if (dqp->q_blk.softlimit && dqp->q_blk.count > dqp->q_blk.softlimit &&
Darrick J. Wongd3537cf2020-07-14 10:37:31 -07001156 !ddq->d_btimer)
1157 return __this_address;
1158
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001159 if (dqp->q_ino.softlimit && dqp->q_ino.count > dqp->q_ino.softlimit &&
Darrick J. Wongd3537cf2020-07-14 10:37:31 -07001160 !ddq->d_itimer)
1161 return __this_address;
1162
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001163 if (dqp->q_rtb.softlimit && dqp->q_rtb.count > dqp->q_rtb.softlimit &&
Darrick J. Wongd3537cf2020-07-14 10:37:31 -07001164 !ddq->d_rtbtimer)
1165 return __this_address;
1166
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -07001167 return NULL;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * Write a modified dquot to disk.
1172 * The dquot must be locked and the flush lock too taken by caller.
1173 * The flush lock will not be unlocked until the dquot reaches the disk,
1174 * but the dquot is free to be unlocked and modified by the caller
1175 * in the interim. Dquot is still locked on return. This behavior is
1176 * identical to that of inodes.
1177 */
1178int
1179xfs_qm_dqflush(
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001180 struct xfs_dquot *dqp,
1181 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001183 struct xfs_mount *mp = dqp->q_mount;
Brian Fosterb707fff2020-05-06 13:25:22 -07001184 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001185 struct xfs_buf *bp;
Eric Sandeen7224fa42018-05-07 09:20:18 -07001186 struct xfs_dqblk *dqb;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001187 struct xfs_disk_dquot *ddqp;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001188 xfs_failaddr_t fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001192 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001193
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001194 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001196 *bpp = NULL;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 xfs_qm_dqunpin_wait(dqp);
1199
1200 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001203 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
Brian Foster8d3d7e22020-03-27 08:29:45 -07001204 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1205 &bp, &xfs_dquot_buf_ops);
Brian Fosterb707fff2020-05-06 13:25:22 -07001206 if (error == -EAGAIN)
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001207 goto out_unlock;
Brian Fosterb707fff2020-05-06 13:25:22 -07001208 if (error)
1209 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001211 /*
1212 * Calculate the location of the dquot inside the buffer.
1213 */
Eric Sandeen7224fa42018-05-07 09:20:18 -07001214 dqb = bp->b_addr + dqp->q_bufoffset;
1215 ddqp = &dqb->dd_diskdq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001216
Brian Foster629dcb32020-05-06 13:25:22 -07001217 /* sanity check the in-core structure before we flush */
Darrick J. Wongc51df732020-07-14 10:37:30 -07001218 fa = xfs_dquot_verify(mp, &dqp->q_core, dqp->q_id, 0);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001219 if (fa) {
1220 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
Darrick J. Wongc51df732020-07-14 10:37:30 -07001221 dqp->q_id, fa);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001222 xfs_buf_relse(bp);
Brian Fosterb707fff2020-05-06 13:25:22 -07001223 error = -EFSCORRUPTED;
1224 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -07001227 fa = xfs_qm_dqflush_check(dqp);
1228 if (fa) {
1229 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
Darrick J. Wongc51df732020-07-14 10:37:30 -07001230 dqp->q_id, fa);
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -07001231 xfs_buf_relse(bp);
1232 error = -EFSCORRUPTED;
1233 goto out_abort;
1234 }
1235
1236 xfs_dquot_to_disk(ddqp, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /*
1239 * Clear the dirty field and remember the flush lsn for later use.
1240 */
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001241 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
David Chinner7b2e2a32008-10-30 17:39:12 +11001243 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1244 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /*
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001247 * copy the lsn into the on-disk dquot now while we have the in memory
1248 * dquot here. This can't be done later in the write verifier as we
1249 * can't get access to the log item at that point in time.
Dave Chinner6fcdc592013-06-03 15:28:46 +10001250 *
1251 * We also calculate the CRC here so that the on-disk dquot in the
1252 * buffer always has a valid CRC. This ensures there is no possibility
1253 * of a dquot without an up-to-date CRC getting to disk.
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001254 */
1255 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001256 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
Dave Chinner6fcdc592013-06-03 15:28:46 +10001257 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1258 XFS_DQUOT_CRC_OFF);
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001259 }
1260
1261 /*
Dave Chinner2ef3f7f2020-06-29 14:49:14 -07001262 * Attach the dquot to the buffer so that we can remove this dquot from
1263 * the AIL and release the flush lock once the dquot is synced to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Dave Chinner0c7e5af2020-06-29 14:48:46 -07001265 bp->b_flags |= _XBF_DQUOTS;
Dave Chinner2ef3f7f2020-06-29 14:49:14 -07001266 list_add_tail(&dqp->q_logitem.qli_item.li_bio_list, &bp->b_li_list);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /*
1269 * If the buffer is pinned then push on the log so we won't
1270 * get stuck waiting in the write for too long.
1271 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001272 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001273 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001274 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001277 trace_xfs_dqflush_done(dqp);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001278 *bpp = bp;
1279 return 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001280
Brian Fosterb707fff2020-05-06 13:25:22 -07001281out_abort:
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001282 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
Brian Foster2b3cf092020-05-06 13:27:04 -07001283 xfs_trans_ail_delete(lip, 0);
Brian Fosterb707fff2020-05-06 13:25:22 -07001284 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001285out_unlock:
1286 xfs_dqfunlock(dqp);
Brian Foster8d3d7e22020-03-27 08:29:45 -07001287 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001290/*
1291 * Lock two xfs_dquot structures.
1292 *
1293 * To avoid deadlocks we always lock the quota structure with
1294 * the lowerd id first.
1295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296void
1297xfs_dqlock2(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001298 struct xfs_dquot *d1,
1299 struct xfs_dquot *d2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 if (d1 && d2) {
1302 ASSERT(d1 != d2);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001303 if (d1->q_id > d2->q_id) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001304 mutex_lock(&d2->q_qlock);
1305 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001307 mutex_lock(&d1->q_qlock);
1308 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001310 } else if (d1) {
1311 mutex_lock(&d1->q_qlock);
1312 } else if (d2) {
1313 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315}
1316
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001317int __init
1318xfs_qm_init(void)
1319{
Carlos Maiolinob1231762019-11-14 12:43:03 -08001320 xfs_qm_dqzone = kmem_cache_create("xfs_dquot",
1321 sizeof(struct xfs_dquot),
1322 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001323 if (!xfs_qm_dqzone)
1324 goto out;
1325
Carlos Maiolinob1231762019-11-14 12:43:03 -08001326 xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx",
1327 sizeof(struct xfs_dquot_acct),
1328 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001329 if (!xfs_qm_dqtrxzone)
1330 goto out_free_dqzone;
1331
1332 return 0;
1333
1334out_free_dqzone:
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001335 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001336out:
1337 return -ENOMEM;
1338}
1339
Gerard Snitselaar1c2ccc62012-03-16 18:36:18 +00001340void
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001341xfs_qm_exit(void)
1342{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001343 kmem_cache_destroy(xfs_qm_dqtrxzone);
1344 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001345}
Darrick J. Wong554ba962018-05-04 15:31:21 -07001346
1347/*
1348 * Iterate every dquot of a particular type. The caller must ensure that the
1349 * particular quota type is active. iter_fn can return negative error codes,
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001350 * or -ECANCELED to indicate that it wants to stop iterating.
Darrick J. Wong554ba962018-05-04 15:31:21 -07001351 */
1352int
1353xfs_qm_dqiterate(
1354 struct xfs_mount *mp,
1355 uint dqtype,
1356 xfs_qm_dqiterate_fn iter_fn,
1357 void *priv)
1358{
1359 struct xfs_dquot *dq;
1360 xfs_dqid_t id = 0;
1361 int error;
1362
1363 do {
1364 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1365 if (error == -ENOENT)
1366 return 0;
1367 if (error)
1368 return error;
1369
1370 error = iter_fn(dq, dqtype, priv);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001371 id = dq->q_id;
Darrick J. Wong554ba962018-05-04 15:31:21 -07001372 xfs_qm_dqput(dq);
Darrick J. Wong554ba962018-05-04 15:31:21 -07001373 } while (error == 0 && id != 0);
1374
1375 return error;
1376}