blob: 5408ec82e0d9fe16dfb9a3f90cc8105afd4d8bf4 [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;
74 struct xfs_disk_dquot *d = &dq->q_core;
Carlos Maiolinobe607942016-02-08 11:27:55 +110075 struct xfs_def_quota *defq;
Brian Fosterb1366452013-03-18 10:51:46 -040076 int prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 ASSERT(d->d_id);
Eric Sandeence6e7e79c2020-05-21 13:07:00 -070079 defq = xfs_get_defquota(q, xfs_dquot_type(dq));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Carlos Maiolinobe607942016-02-08 11:27:55 +110081 if (defq->bsoftlimit && !d->d_blk_softlimit) {
82 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
Brian Fosterb1366452013-03-18 10:51:46 -040083 prealloc = 1;
84 }
Carlos Maiolinobe607942016-02-08 11:27:55 +110085 if (defq->bhardlimit && !d->d_blk_hardlimit) {
86 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
Brian Fosterb1366452013-03-18 10:51:46 -040087 prealloc = 1;
88 }
Carlos Maiolinobe607942016-02-08 11:27:55 +110089 if (defq->isoftlimit && !d->d_ino_softlimit)
90 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
91 if (defq->ihardlimit && !d->d_ino_hardlimit)
92 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
93 if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
94 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
95 if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
96 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
Brian Fosterb1366452013-03-18 10:51:46 -040097
98 if (prealloc)
99 xfs_dquot_set_prealloc_limits(dq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/*
103 * Check the limits and timers of a dquot and start or reset timers
104 * if necessary.
105 * This gets called even when quota enforcement is OFF, which makes our
106 * life a little less complicated. (We just don't reject any quota
107 * reservations in that case, when enforcement is off).
108 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
109 * enforcement's off.
110 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000111 * 'automatically' get started when limits get exceeded. They do
112 * get reset to zero, however, when we find the count to be under
113 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115void
116xfs_qm_adjust_dqtimers(
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800117 struct xfs_mount *mp,
Eric Sandeen3dbb9aa2020-05-21 13:07:00 -0700118 struct xfs_dquot *dq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Eric Sandeene8503012020-05-21 13:07:01 -0700120 struct xfs_quotainfo *qi = mp->m_quotainfo;
Eric Sandeen3dbb9aa2020-05-21 13:07:00 -0700121 struct xfs_disk_dquot *d = &dq->q_core;
Eric Sandeene8503012020-05-21 13:07:01 -0700122 struct xfs_def_quota *defq;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ASSERT(d->d_id);
Eric Sandeene8503012020-05-21 13:07:01 -0700125 defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200127#ifdef DEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100128 if (d->d_blk_hardlimit)
129 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
130 be64_to_cpu(d->d_blk_hardlimit));
131 if (d->d_ino_hardlimit)
132 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
133 be64_to_cpu(d->d_ino_hardlimit));
134 if (d->d_rtb_hardlimit)
135 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
136 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100140 if ((d->d_blk_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000141 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100142 be64_to_cpu(d->d_blk_softlimit))) ||
143 (d->d_blk_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000144 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100145 be64_to_cpu(d->d_blk_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800146 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700147 defq->btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000148 } else {
149 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 } else {
152 if ((!d->d_blk_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000153 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100154 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 (!d->d_blk_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000156 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100157 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 d->d_btimer = 0;
159 }
160 }
161
162 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100163 if ((d->d_ino_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000164 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100165 be64_to_cpu(d->d_ino_softlimit))) ||
166 (d->d_ino_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000167 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100168 be64_to_cpu(d->d_ino_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800169 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700170 defq->itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000171 } else {
172 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 } else {
175 if ((!d->d_ino_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000176 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100177 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 (!d->d_ino_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000179 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100180 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 d->d_itimer = 0;
182 }
183 }
184
185 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100186 if ((d->d_rtb_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000187 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100188 be64_to_cpu(d->d_rtb_softlimit))) ||
189 (d->d_rtb_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000190 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100191 be64_to_cpu(d->d_rtb_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800192 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700193 defq->rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000194 } else {
195 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 } else {
198 if ((!d->d_rtb_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000199 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100200 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 (!d->d_rtb_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000202 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100203 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 d->d_rtbtimer = 0;
205 }
206 }
207}
208
209/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * initialize a buffer full of dquots and log the whole thing
211 */
212STATIC void
213xfs_qm_init_dquot_blk(
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700214 struct xfs_trans *tp,
215 struct xfs_mount *mp,
216 xfs_dqid_t id,
217 uint type,
218 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000220 struct xfs_quotainfo *q = mp->m_quotainfo;
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700221 struct xfs_dqblk *d;
222 xfs_dqid_t curid;
223 unsigned int qflag;
224 unsigned int blftype;
225 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200228 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Chandra Seetharaman62926042011-07-22 23:40:15 +0000230 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
233 * ID of the first dquot in the block - id's are zero based.
234 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000235 curid = id - (id % q->qi_dqperchunk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000236 memset(d, 0, BBTOB(q->qi_dqchunklen));
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000237 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
238 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
239 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
240 d->dd_diskdq.d_id = cpu_to_be32(curid);
241 d->dd_diskdq.d_flags = type;
Dave Chinner6fcdc592013-06-03 15:28:46 +1000242 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner92863452015-08-19 10:32:01 +1000243 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000244 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
245 XFS_DQUOT_CRC_OFF);
246 }
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000247 }
248
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700249 if (type & XFS_DQ_USER) {
250 qflag = XFS_UQUOTA_CHKD;
251 blftype = XFS_BLF_UDQUOT_BUF;
252 } else if (type & XFS_DQ_PROJ) {
253 qflag = XFS_PQUOTA_CHKD;
254 blftype = XFS_BLF_PDQUOT_BUF;
255 } else {
256 qflag = XFS_GQUOTA_CHKD;
257 blftype = XFS_BLF_GDQUOT_BUF;
258 }
259
260 xfs_trans_dquot_buf(tp, bp, blftype);
261
262 /*
263 * quotacheck uses delayed writes to update all the dquots on disk in an
264 * efficient manner instead of logging the individual dquot changes as
265 * they are made. However if we log the buffer allocated here and crash
266 * after quotacheck while the logged initialisation is still in the
267 * active region of the log, log recovery can replay the dquot buffer
268 * initialisation over the top of the checked dquots and corrupt quota
269 * accounting.
270 *
271 * To avoid this problem, quotacheck cannot log the initialised buffer.
272 * We must still dirty the buffer and write it back before the
273 * allocation transaction clears the log. Therefore, mark the buffer as
274 * ordered instead of logging it directly. This is safe for quotacheck
275 * because it detects and repairs allocated but initialized dquot blocks
276 * in the quota inodes.
277 */
278 if (!(mp->m_qflags & qflag))
279 xfs_trans_ordered_buf(tp, bp);
280 else
281 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Brian Fosterb1366452013-03-18 10:51:46 -0400284/*
285 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
286 * watermarks correspond to the soft and hard limits by default. If a soft limit
287 * is not specified, we use 95% of the hard limit.
288 */
289void
290xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
291{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700292 uint64_t space;
Brian Fosterb1366452013-03-18 10:51:46 -0400293
294 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
295 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
296 if (!dqp->q_prealloc_lo_wmark) {
297 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
298 do_div(dqp->q_prealloc_lo_wmark, 100);
299 dqp->q_prealloc_lo_wmark *= 95;
300 }
301
302 space = dqp->q_prealloc_hi_wmark;
303
304 do_div(space, 100);
305 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
306 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
307 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700311 * Ensure that the given in-core dquot has a buffer on disk backing it, and
Darrick J. Wong710d7072019-04-24 09:27:41 -0700312 * return the buffer locked and held. This is called when the bmapi finds a
313 * hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700316xfs_dquot_disk_alloc(
317 struct xfs_trans **tpp,
318 struct xfs_dquot *dqp,
319 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700321 struct xfs_bmbt_irec map;
Brian Foster2ba13722018-07-11 22:26:11 -0700322 struct xfs_trans *tp = *tpp;
323 struct xfs_mount *mp = tp->t_mountp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700324 struct xfs_buf *bp;
325 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700326 int nmaps = 1;
327 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000328
329 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 xfs_ilock(quotip, XFS_ILOCK_EXCL);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000332 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700333 /*
334 * Return if this type of quotas is turned off while we didn't
335 * have an inode lock
336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
Dave Chinner24513372014-06-25 14:58:08 +1000338 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700341 /* Create the block mapping. */
Brian Foster2ba13722018-07-11 22:26:11 -0700342 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
343 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
Brian Fosterda781e62019-10-21 09:26:48 -0700344 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
345 &nmaps);
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000346 if (error)
Brian Foster73971b12018-08-07 10:57:13 -0700347 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
349 ASSERT(nmaps == 1);
350 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
351 (map.br_startblock != HOLESTARTBLOCK));
352
353 /*
354 * Keep track of the blkno to save a lookup later
355 */
356 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
357
358 /* now we can just get the buffer (there's nothing to read yet) */
Darrick J. Wongce924642020-01-23 17:01:18 -0800359 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
360 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
361 if (error)
362 return error;
Dave Chinner1813dd62012-11-14 17:54:40 +1100363 bp->b_ops = &xfs_dquot_buf_ops;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * Make a chunk of dquots out of this buffer and log
367 * the entire thing.
368 */
Brian Foster2ba13722018-07-11 22:26:11 -0700369 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000370 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700371 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Tim Shimminefa092f2005-09-05 08:29:01 +1000373 /*
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700374 * Hold the buffer and join it to the dfops so that we'll still own
375 * the buffer when we return to the caller. The buffer disposal on
376 * error must be paid attention to very carefully, as it has been
377 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
378 * code when allocating a new dquot record" in 2005, and the later
379 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
380 * the buffer locked across the _defer_finish call. We can now do
381 * this correctly with xfs_defer_bjoin.
Tim Shimminefa092f2005-09-05 08:29:01 +1000382 *
Brian Foster73971b12018-08-07 10:57:13 -0700383 * Above, we allocated a disk block for the dquot information and used
384 * get_buf to initialize the dquot. If the _defer_finish fails, the old
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700385 * transaction is gone but the new buffer is not joined or held to any
386 * transaction, so we must _buf_relse it.
Tim Shimminefa092f2005-09-05 08:29:01 +1000387 *
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700388 * If everything succeeds, the caller of this function is returned a
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700389 * buffer that is locked and held to the transaction. The caller
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700390 * is responsible for unlocking any buffer passed back, either
Darrick J. Wong710d7072019-04-24 09:27:41 -0700391 * manually or by committing the transaction. On error, the buffer is
392 * released and not passed back.
Tim Shimminefa092f2005-09-05 08:29:01 +1000393 */
Brian Foster2ba13722018-07-11 22:26:11 -0700394 xfs_trans_bhold(tp, bp);
Brian Foster9e28a242018-07-24 13:43:15 -0700395 error = xfs_defer_finish(tpp);
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700396 if (error) {
Darrick J. Wong710d7072019-04-24 09:27:41 -0700397 xfs_trans_bhold_release(*tpp, bp);
398 xfs_trans_brelse(*tpp, bp);
Brian Foster73971b12018-08-07 10:57:13 -0700399 return error;
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700400 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700401 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
Dave Chinner9aede1d2013-10-15 09:17:52 +1100404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700406 * Read in the in-core dquot's on-disk metadata and return the buffer.
407 * Returns ENOENT to signal a hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
409STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700410xfs_dquot_disk_read(
411 struct xfs_mount *mp,
412 struct xfs_dquot *dqp,
413 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500415 struct xfs_bmbt_irec map;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500416 struct xfs_buf *bp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700417 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700418 uint lock_mode;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700419 int nmaps = 1;
420 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christoph Hellwig0891f992017-07-13 12:14:34 -0700422 lock_mode = xfs_ilock_data_map_shared(quotip);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700423 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000425 * Return if this type of quotas is turned off while we
426 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Christoph Hellwig0891f992017-07-13 12:14:34 -0700428 xfs_iunlock(quotip, lock_mode);
Dave Chinner24513372014-06-25 14:58:08 +1000429 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000433 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000435 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700436 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700437 xfs_iunlock(quotip, lock_mode);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000438 if (error)
439 return error;
440
441 ASSERT(nmaps == 1);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700442 ASSERT(map.br_blockcount >= 1);
443 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
444 if (map.br_startblock == HOLESTARTBLOCK)
445 return -ENOENT;
446
447 trace_xfs_dqtobp_read(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000448
449 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700450 * store the blkno etc so that we don't have to do the
451 * mapping all the time
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000452 */
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700453 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000454
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700455 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
456 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
457 &xfs_dquot_buf_ops);
458 if (error) {
459 ASSERT(bp == NULL);
460 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
Dave Chinnerc6319192012-11-14 17:50:13 +1100463 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700464 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
465 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Eric Sandeend99831f2014-06-22 15:03:54 +1000467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700470/* Allocate and initialize everything we need for an incore dquot. */
471STATIC struct xfs_dquot *
472xfs_dquot_alloc(
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000473 struct xfs_mount *mp,
474 xfs_dqid_t id,
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700475 uint type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000477 struct xfs_dquot *dqp;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000478
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700479 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000480
481 dqp->dq_flags = type;
482 dqp->q_core.d_id = cpu_to_be32(id);
483 dqp->q_mount = mp;
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000484 INIT_LIST_HEAD(&dqp->q_lru);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000485 mutex_init(&dqp->q_qlock);
486 init_waitqueue_head(&dqp->q_pinwait);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700487 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
488 /*
489 * Offset of dquot in the (fixed sized) dquot chunk.
490 */
491 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
492 sizeof(xfs_dqblk_t);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000493
494 /*
495 * Because we want to use a counting completion, complete
496 * the flush completion once to allow a single access to
497 * the flush completion without blocking.
498 */
499 init_completion(&dqp->q_flush);
500 complete(&dqp->q_flush);
501
502 /*
503 * Make sure group quotas have a different lock class than user
504 * quotas.
505 */
Dave Chinnerf112a042013-09-30 09:37:03 +1000506 switch (type) {
507 case XFS_DQ_USER:
508 /* uses the default lock class */
509 break;
510 case XFS_DQ_GROUP:
511 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
512 break;
513 case XFS_DQ_PROJ:
514 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
515 break;
516 default:
517 ASSERT(0);
518 break;
519 }
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000520
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700521 xfs_qm_dquot_logitem_init(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700523 XFS_STATS_INC(mp, xs_qm_dquot);
524 return dqp;
525}
526
527/* Copy the in-core quota fields in from the on-disk buffer. */
Darrick J. Wongafeda602020-07-14 10:36:09 -0700528STATIC int
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700529xfs_dquot_from_disk(
530 struct xfs_dquot *dqp,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700531 struct xfs_buf *bp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700532{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700533 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
534
Darrick J. Wongafeda602020-07-14 10:36:09 -0700535 /*
536 * Ensure that we got the type and ID we were looking for.
537 * Everything else was checked by the dquot buffer verifier.
538 */
539 if ((ddqp->d_flags & XFS_DQ_ALLTYPES) != dqp->dq_flags ||
540 ddqp->d_id != dqp->q_core.d_id) {
541 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR,
542 "Metadata corruption detected at %pS, quota %u",
543 __this_address, be32_to_cpu(dqp->q_core.d_id));
544 xfs_alert(bp->b_mount, "Unmount and run xfs_repair");
545 return -EFSCORRUPTED;
546 }
547
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700548 /* copy everything from disk dquot to the incore dquot */
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800549 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot));
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700550
551 /*
552 * Reservation counters are defined as reservation plus current usage
553 * to avoid having to add every time.
554 */
555 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
556 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
557 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
558
559 /* initialize the dquot speculative prealloc thresholds */
560 xfs_dquot_set_prealloc_limits(dqp);
Darrick J. Wongafeda602020-07-14 10:36:09 -0700561 return 0;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700562}
563
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700564/* Allocate and initialize the dquot buffer for this in-core dquot. */
565static int
566xfs_qm_dqread_alloc(
567 struct xfs_mount *mp,
568 struct xfs_dquot *dqp,
569 struct xfs_buf **bpp)
570{
571 struct xfs_trans *tp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700572 int error;
573
574 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
575 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
576 if (error)
577 goto err;
578
Darrick J. Wong710d7072019-04-24 09:27:41 -0700579 error = xfs_dquot_disk_alloc(&tp, dqp, bpp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700580 if (error)
581 goto err_cancel;
582
583 error = xfs_trans_commit(tp);
584 if (error) {
585 /*
586 * Buffer was held to the transaction, so we have to unlock it
587 * manually here because we're not passing it back.
588 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700589 xfs_buf_relse(*bpp);
590 *bpp = NULL;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700591 goto err;
592 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700593 return 0;
594
595err_cancel:
596 xfs_trans_cancel(tp);
597err:
598 return error;
599}
600
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700601/*
602 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700603 * and release the buffer immediately. If @can_alloc is true, fill any
604 * holes in the on-disk metadata.
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700605 */
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700606static int
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700607xfs_qm_dqread(
608 struct xfs_mount *mp,
609 xfs_dqid_t id,
610 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700611 bool can_alloc,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700612 struct xfs_dquot **dqpp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700613{
614 struct xfs_dquot *dqp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700615 struct xfs_buf *bp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700616 int error;
617
618 dqp = xfs_dquot_alloc(mp, id, type);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000619 trace_xfs_dqread(dqp);
620
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700621 /* Try to read the buffer, allocating if necessary. */
622 error = xfs_dquot_disk_read(mp, dqp, &bp);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700623 if (error == -ENOENT && can_alloc)
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700624 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
625 if (error)
626 goto err;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700629 * At this point we should have a clean locked buffer. Copy the data
630 * to the incore dquot and release the buffer since the incore dquot
631 * has its own locking protocol so we needn't tie up the buffer any
632 * further.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200634 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongafeda602020-07-14 10:36:09 -0700635 error = xfs_dquot_from_disk(dqp, bp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700636 xfs_buf_relse(bp);
Darrick J. Wongafeda602020-07-14 10:36:09 -0700637 if (error)
638 goto err;
639
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700640 *dqpp = dqp;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000641 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700643err:
644 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xfs_qm_dqdestroy(dqp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700646 *dqpp = NULL;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
Eric Sandeen296c24e2016-02-08 11:27:38 +1100651 * Advance to the next id in the current chunk, or if at the
652 * end of the chunk, skip ahead to first id in next allocated chunk
653 * using the SEEK_DATA interface.
654 */
Eryu Guan6e3e6d52016-04-06 09:47:21 +1000655static int
Eric Sandeen296c24e2016-02-08 11:27:38 +1100656xfs_dq_get_next_id(
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700657 struct xfs_mount *mp,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100658 uint type,
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700659 xfs_dqid_t *id)
Eric Sandeen296c24e2016-02-08 11:27:38 +1100660{
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700661 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
662 xfs_dqid_t next_id = *id + 1; /* simple advance */
663 uint lock_flags;
664 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700665 struct xfs_iext_cursor cur;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100666 xfs_fsblock_t start;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100667 int error = 0;
668
Eric Sandeen657bdfb2017-01-17 11:43:38 -0800669 /* If we'd wrap past the max ID, stop */
670 if (next_id < *id)
671 return -ENOENT;
672
Eric Sandeen296c24e2016-02-08 11:27:38 +1100673 /* If new ID is within the current chunk, advancing it sufficed */
674 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
675 *id = next_id;
676 return 0;
677 }
678
679 /* Nope, next_id is now past the current chunk, so find the next one */
680 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
681
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700682 lock_flags = xfs_ilock_data_map_shared(quotip);
683 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
684 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
685 if (error)
686 return error;
687 }
Eric Sandeen296c24e2016-02-08 11:27:38 +1100688
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700689 if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
Brian Foster2192b0b2017-07-05 12:07:52 -0700690 /* contiguous chunk, bump startoff for the id calculation */
691 if (got.br_startoff < start)
692 got.br_startoff = start;
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700693 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
Brian Foster2192b0b2017-07-05 12:07:52 -0700694 } else {
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700695 error = -ENOENT;
Brian Foster2192b0b2017-07-05 12:07:52 -0700696 }
697
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700698 xfs_iunlock(quotip, lock_flags);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100699
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700700 return error;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100701}
702
703/*
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700704 * Look up the dquot in the in-core cache. If found, the dquot is returned
705 * locked and ready to go.
706 */
707static struct xfs_dquot *
708xfs_qm_dqget_cache_lookup(
709 struct xfs_mount *mp,
710 struct xfs_quotainfo *qi,
711 struct radix_tree_root *tree,
712 xfs_dqid_t id)
713{
714 struct xfs_dquot *dqp;
715
716restart:
717 mutex_lock(&qi->qi_tree_lock);
718 dqp = radix_tree_lookup(tree, id);
719 if (!dqp) {
720 mutex_unlock(&qi->qi_tree_lock);
721 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
722 return NULL;
723 }
724
725 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700726 if (dqp->q_flags & XFS_DQFLAG_FREEING) {
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700727 xfs_dqunlock(dqp);
728 mutex_unlock(&qi->qi_tree_lock);
729 trace_xfs_dqget_freeing(dqp);
730 delay(1);
731 goto restart;
732 }
733
734 dqp->q_nrefs++;
735 mutex_unlock(&qi->qi_tree_lock);
736
737 trace_xfs_dqget_hit(dqp);
738 XFS_STATS_INC(mp, xs_qm_dqcachehits);
739 return dqp;
740}
741
742/*
743 * Try to insert a new dquot into the in-core cache. If an error occurs the
744 * caller should throw away the dquot and start over. Otherwise, the dquot
745 * is returned locked (and held by the cache) as if there had been a cache
746 * hit.
747 */
748static int
749xfs_qm_dqget_cache_insert(
750 struct xfs_mount *mp,
751 struct xfs_quotainfo *qi,
752 struct radix_tree_root *tree,
753 xfs_dqid_t id,
754 struct xfs_dquot *dqp)
755{
756 int error;
757
758 mutex_lock(&qi->qi_tree_lock);
759 error = radix_tree_insert(tree, id, dqp);
760 if (unlikely(error)) {
761 /* Duplicate found! Caller must try again. */
762 WARN_ON(error != -EEXIST);
763 mutex_unlock(&qi->qi_tree_lock);
764 trace_xfs_dqget_dup(dqp);
765 return error;
766 }
767
768 /* Return a locked dquot to the caller, with a reference taken. */
769 xfs_dqlock(dqp);
770 dqp->q_nrefs = 1;
771
772 qi->qi_dquots++;
773 mutex_unlock(&qi->qi_tree_lock);
774
775 return 0;
776}
777
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700778/* Check our input parameters. */
779static int
780xfs_qm_dqget_checks(
781 struct xfs_mount *mp,
782 uint type)
783{
784 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
785 return -ESRCH;
786
787 switch (type) {
788 case XFS_DQ_USER:
789 if (!XFS_IS_UQUOTA_ON(mp))
790 return -ESRCH;
791 return 0;
792 case XFS_DQ_GROUP:
793 if (!XFS_IS_GQUOTA_ON(mp))
794 return -ESRCH;
795 return 0;
796 case XFS_DQ_PROJ:
797 if (!XFS_IS_PQUOTA_ON(mp))
798 return -ESRCH;
799 return 0;
800 default:
801 WARN_ON_ONCE(0);
802 return -EINVAL;
803 }
804}
805
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700806/*
Darrick J. Wong4882c192018-05-04 15:30:22 -0700807 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
808 * dquot, doing an allocation (if requested) as needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
810int
811xfs_qm_dqget(
Darrick J. Wong4882c192018-05-04 15:30:22 -0700812 struct xfs_mount *mp,
813 xfs_dqid_t id,
814 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700815 bool can_alloc,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700816 struct xfs_dquot **O_dqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000818 struct xfs_quotainfo *qi = mp->m_quotainfo;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700819 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000820 struct xfs_dquot *dqp;
821 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700823 error = xfs_qm_dqget_checks(mp, type);
824 if (error)
825 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Darrick J. Wong4882c192018-05-04 15:30:22 -0700827restart:
828 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
829 if (dqp) {
830 *O_dqpp = dqp;
831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Christoph Hellwig92678552011-12-06 21:58:18 +0000833
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700834 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700835 if (error)
836 return error;
837
838 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
839 if (error) {
840 /*
841 * Duplicate found. Just throw away the new dquot and start
842 * over.
843 */
844 xfs_qm_dqdestroy(dqp);
845 XFS_STATS_INC(mp, xs_qm_dquot_dups);
846 goto restart;
847 }
848
849 trace_xfs_dqget_miss(dqp);
850 *O_dqpp = dqp;
851 return 0;
852}
853
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700854/*
855 * Given a dquot id and type, read and initialize a dquot from the on-disk
856 * metadata. This function is only for use during quota initialization so
857 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
858 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
859 */
860int
861xfs_qm_dqget_uncached(
862 struct xfs_mount *mp,
863 xfs_dqid_t id,
864 uint type,
865 struct xfs_dquot **dqpp)
866{
867 int error;
868
869 error = xfs_qm_dqget_checks(mp, type);
870 if (error)
871 return error;
872
873 return xfs_qm_dqread(mp, id, type, 0, dqpp);
874}
875
Darrick J. Wong4882c192018-05-04 15:30:22 -0700876/* Return the quota id for a given inode and type. */
877xfs_dqid_t
878xfs_qm_id_for_quotatype(
879 struct xfs_inode *ip,
880 uint type)
881{
882 switch (type) {
883 case XFS_DQ_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800884 return i_uid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700885 case XFS_DQ_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800886 return i_gid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700887 case XFS_DQ_PROJ:
Christoph Hellwigde7a8662019-11-12 08:22:54 -0800888 return ip->i_d.di_projid;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700889 }
890 ASSERT(0);
891 return 0;
892}
893
894/*
895 * Return the dquot for a given inode and type. If @can_alloc is true, then
896 * allocate blocks if needed. The inode's ILOCK must be held and it must not
897 * have already had an inode attached.
898 */
899int
900xfs_qm_dqget_inode(
901 struct xfs_inode *ip,
902 uint type,
903 bool can_alloc,
904 struct xfs_dquot **O_dqpp)
905{
906 struct xfs_mount *mp = ip->i_mount;
907 struct xfs_quotainfo *qi = mp->m_quotainfo;
908 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
909 struct xfs_dquot *dqp;
910 xfs_dqid_t id;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700911 int error;
912
913 error = xfs_qm_dqget_checks(mp, type);
914 if (error)
915 return error;
916
Darrick J. Wong4882c192018-05-04 15:30:22 -0700917 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
918 ASSERT(xfs_inode_dquot(ip, type) == NULL);
919
920 id = xfs_qm_id_for_quotatype(ip, type);
921
Christoph Hellwig92678552011-12-06 21:58:18 +0000922restart:
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700923 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000924 if (dqp) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000925 *O_dqpp = dqp;
926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /*
930 * Dquot cache miss. We don't want to keep the inode lock across
931 * a (potential) disk read. Also we don't want to deal with the lock
932 * ordering between quotainode and this inode. OTOH, dropping the inode
933 * lock here means dealing with a chown that can happen before
934 * we re-acquire the lock.
935 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700937 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700938 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000939 if (error)
940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Darrick J. Wong4882c192018-05-04 15:30:22 -0700942 /*
943 * A dquot could be attached to this inode by now, since we had
944 * dropped the ilock.
945 */
946 if (xfs_this_quota_on(mp, type)) {
947 struct xfs_dquot *dqp1;
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000948
Darrick J. Wong4882c192018-05-04 15:30:22 -0700949 dqp1 = xfs_inode_dquot(ip, type);
950 if (dqp1) {
Chandra Seetharaman36731412012-01-23 17:31:30 +0000951 xfs_qm_dqdestroy(dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700952 dqp = dqp1;
953 xfs_dqlock(dqp);
954 goto dqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Darrick J. Wong4882c192018-05-04 15:30:22 -0700956 } else {
957 /* inode stays locked on return */
958 xfs_qm_dqdestroy(dqp);
959 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700962 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
963 if (error) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000964 /*
965 * Duplicate found. Just throw away the new dquot and start
966 * over.
967 */
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000968 xfs_qm_dqdestroy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100969 XFS_STATS_INC(mp, xs_qm_dquot_dups);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000970 goto restart;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Darrick J. Wong4882c192018-05-04 15:30:22 -0700973dqret:
974 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000975 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 *O_dqpp = dqp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000980/*
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700981 * Starting at @id and progressing upwards, look for an initialized incore
982 * dquot, lock it, and return it.
983 */
984int
985xfs_qm_dqget_next(
986 struct xfs_mount *mp,
987 xfs_dqid_t id,
988 uint type,
989 struct xfs_dquot **dqpp)
990{
991 struct xfs_dquot *dqp;
992 int error = 0;
993
994 *dqpp = NULL;
995 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700996 error = xfs_qm_dqget(mp, id, type, false, &dqp);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700997 if (error == -ENOENT)
998 continue;
999 else if (error != 0)
1000 break;
1001
1002 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
1003 *dqpp = dqp;
1004 return 0;
1005 }
1006
1007 xfs_qm_dqput(dqp);
1008 }
1009
1010 return error;
1011}
1012
1013/*
Christoph Hellwigf8739c32012-03-13 08:52:34 +00001014 * Release a reference to the dquot (decrement ref-count) and unlock it.
1015 *
1016 * If there is a group quota attached to this dquot, carefully release that
1017 * too without tripping over deadlocks'n'stuff.
1018 */
1019void
1020xfs_qm_dqput(
1021 struct xfs_dquot *dqp)
1022{
1023 ASSERT(dqp->q_nrefs > 0);
1024 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1025
1026 trace_xfs_dqput(dqp);
1027
Dave Chinner3c35337572014-05-05 17:30:15 +10001028 if (--dqp->q_nrefs == 0) {
1029 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1030 trace_xfs_dqput_free(dqp);
1031
1032 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001033 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner3c35337572014-05-05 17:30:15 +10001034 }
1035 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
1039 * Release a dquot. Flush it if dirty, then dqput() it.
1040 * dquot must not be locked.
1041 */
1042void
1043xfs_qm_dqrele(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001044 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001046 if (!dqp)
1047 return;
1048
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001049 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 xfs_dqlock(dqp);
1052 /*
1053 * We don't care to flush it if the dquot is dirty here.
1054 * That will create stutters that we want to avoid.
1055 * Instead we do a delayed write when we try to reclaim
1056 * a dirty dquot. Also xfs_sync will take part of the burden...
1057 */
1058 xfs_qm_dqput(dqp);
1059}
1060
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001061/*
1062 * This is the dquot flushing I/O completion routine. It is called
1063 * from interrupt level when the buffer containing the dquot is
1064 * flushed to disk. It is responsible for removing the dquot logitem
1065 * from the AIL if it has not been re-logged, and unlocking the dquot's
1066 * flush lock. This behavior is very similar to that of inodes..
1067 */
Dave Chinner6f5de182020-06-29 14:48:59 -07001068static void
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001069xfs_qm_dqflush_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001070 struct xfs_log_item *lip)
1071{
Pavel Reichlfd8b81d2019-11-12 17:04:26 -08001072 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001073 struct xfs_dquot *dqp = qip->qli_dquot;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001074 struct xfs_ail *ailp = lip->li_ailp;
Brian Foster849274c2020-05-06 13:25:23 -07001075 xfs_lsn_t tail_lsn;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001076
1077 /*
1078 * We only want to pull the item from the AIL if its
1079 * location in the log has not changed since we started the flush.
1080 * Thus, we only bother if the dquot's lsn has
1081 * not changed. First we check the lsn outside the lock
1082 * since it's cheaper, and then we recheck while
1083 * holding the lock before removing the dquot from the AIL.
1084 */
Dave Chinner22525c12018-05-09 07:47:34 -07001085 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
Carlos Maiolino373b0582017-11-28 08:54:10 -08001086 ((lip->li_lsn == qip->qli_flush_lsn) ||
Dave Chinner22525c12018-05-09 07:47:34 -07001087 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001088
Matthew Wilcox57e80952018-03-07 14:59:39 -08001089 spin_lock(&ailp->ail_lock);
Dave Chinnere98084b2020-06-29 14:49:15 -07001090 xfs_clear_li_failed(lip);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001091 if (lip->li_lsn == qip->qli_flush_lsn) {
Brian Foster849274c2020-05-06 13:25:23 -07001092 /* xfs_ail_update_finish() drops the AIL lock */
1093 tail_lsn = xfs_ail_delete_one(ailp, lip);
1094 xfs_ail_update_finish(ailp, tail_lsn);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001095 } else {
Matthew Wilcox57e80952018-03-07 14:59:39 -08001096 spin_unlock(&ailp->ail_lock);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001097 }
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001098 }
1099
1100 /*
1101 * Release the dq's flush lock since we're done with it.
1102 */
1103 xfs_dqfunlock(dqp);
1104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Dave Chinner6f5de182020-06-29 14:48:59 -07001106void
1107xfs_dquot_done(
1108 struct xfs_buf *bp)
1109{
1110 struct xfs_log_item *lip, *n;
1111
1112 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1113 list_del_init(&lip->li_bio_list);
1114 xfs_qm_dqflush_done(lip);
1115 }
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * Write a modified dquot to disk.
1120 * The dquot must be locked and the flush lock too taken by caller.
1121 * The flush lock will not be unlocked until the dquot reaches the disk,
1122 * but the dquot is free to be unlocked and modified by the caller
1123 * in the interim. Dquot is still locked on return. This behavior is
1124 * identical to that of inodes.
1125 */
1126int
1127xfs_qm_dqflush(
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001128 struct xfs_dquot *dqp,
1129 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001131 struct xfs_mount *mp = dqp->q_mount;
Brian Fosterb707fff2020-05-06 13:25:22 -07001132 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001133 struct xfs_buf *bp;
Eric Sandeen7224fa42018-05-07 09:20:18 -07001134 struct xfs_dqblk *dqb;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001135 struct xfs_disk_dquot *ddqp;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001136 xfs_failaddr_t fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001140 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001141
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001142 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001144 *bpp = NULL;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 xfs_qm_dqunpin_wait(dqp);
1147
1148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001151 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
Brian Foster8d3d7e22020-03-27 08:29:45 -07001152 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1153 &bp, &xfs_dquot_buf_ops);
Brian Fosterb707fff2020-05-06 13:25:22 -07001154 if (error == -EAGAIN)
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001155 goto out_unlock;
Brian Fosterb707fff2020-05-06 13:25:22 -07001156 if (error)
1157 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001159 /*
1160 * Calculate the location of the dquot inside the buffer.
1161 */
Eric Sandeen7224fa42018-05-07 09:20:18 -07001162 dqb = bp->b_addr + dqp->q_bufoffset;
1163 ddqp = &dqb->dd_diskdq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001164
Brian Foster629dcb32020-05-06 13:25:22 -07001165 /* sanity check the in-core structure before we flush */
1166 fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id),
1167 0);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001168 if (fa) {
1169 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
Brian Foster629dcb32020-05-06 13:25:22 -07001170 be32_to_cpu(dqp->q_core.d_id), fa);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001171 xfs_buf_relse(bp);
Brian Fosterb707fff2020-05-06 13:25:22 -07001172 error = -EFSCORRUPTED;
1173 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175
1176 /* This is the only portion of data that needs to persist */
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001177 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
1180 * Clear the dirty field and remember the flush lsn for later use.
1181 */
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001182 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
David Chinner7b2e2a32008-10-30 17:39:12 +11001184 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1185 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /*
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001188 * copy the lsn into the on-disk dquot now while we have the in memory
1189 * dquot here. This can't be done later in the write verifier as we
1190 * can't get access to the log item at that point in time.
Dave Chinner6fcdc592013-06-03 15:28:46 +10001191 *
1192 * We also calculate the CRC here so that the on-disk dquot in the
1193 * buffer always has a valid CRC. This ensures there is no possibility
1194 * of a dquot without an up-to-date CRC getting to disk.
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001195 */
1196 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001197 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
Dave Chinner6fcdc592013-06-03 15:28:46 +10001198 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1199 XFS_DQUOT_CRC_OFF);
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001200 }
1201
1202 /*
Dave Chinner2ef3f7f2020-06-29 14:49:14 -07001203 * Attach the dquot to the buffer so that we can remove this dquot from
1204 * the AIL and release the flush lock once the dquot is synced to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
Dave Chinner0c7e5af2020-06-29 14:48:46 -07001206 bp->b_flags |= _XBF_DQUOTS;
Dave Chinner2ef3f7f2020-06-29 14:49:14 -07001207 list_add_tail(&dqp->q_logitem.qli_item.li_bio_list, &bp->b_li_list);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * If the buffer is pinned then push on the log so we won't
1211 * get stuck waiting in the write for too long.
1212 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001213 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001214 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001215 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001218 trace_xfs_dqflush_done(dqp);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001219 *bpp = bp;
1220 return 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001221
Brian Fosterb707fff2020-05-06 13:25:22 -07001222out_abort:
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001223 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
Brian Foster2b3cf092020-05-06 13:27:04 -07001224 xfs_trans_ail_delete(lip, 0);
Brian Fosterb707fff2020-05-06 13:25:22 -07001225 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001226out_unlock:
1227 xfs_dqfunlock(dqp);
Brian Foster8d3d7e22020-03-27 08:29:45 -07001228 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001231/*
1232 * Lock two xfs_dquot structures.
1233 *
1234 * To avoid deadlocks we always lock the quota structure with
1235 * the lowerd id first.
1236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237void
1238xfs_dqlock2(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001239 struct xfs_dquot *d1,
1240 struct xfs_dquot *d2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 if (d1 && d2) {
1243 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001244 if (be32_to_cpu(d1->q_core.d_id) >
1245 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001246 mutex_lock(&d2->q_qlock);
1247 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001249 mutex_lock(&d1->q_qlock);
1250 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001252 } else if (d1) {
1253 mutex_lock(&d1->q_qlock);
1254 } else if (d2) {
1255 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257}
1258
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001259int __init
1260xfs_qm_init(void)
1261{
Carlos Maiolinob1231762019-11-14 12:43:03 -08001262 xfs_qm_dqzone = kmem_cache_create("xfs_dquot",
1263 sizeof(struct xfs_dquot),
1264 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001265 if (!xfs_qm_dqzone)
1266 goto out;
1267
Carlos Maiolinob1231762019-11-14 12:43:03 -08001268 xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx",
1269 sizeof(struct xfs_dquot_acct),
1270 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001271 if (!xfs_qm_dqtrxzone)
1272 goto out_free_dqzone;
1273
1274 return 0;
1275
1276out_free_dqzone:
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001277 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001278out:
1279 return -ENOMEM;
1280}
1281
Gerard Snitselaar1c2ccc62012-03-16 18:36:18 +00001282void
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001283xfs_qm_exit(void)
1284{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001285 kmem_cache_destroy(xfs_qm_dqtrxzone);
1286 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001287}
Darrick J. Wong554ba962018-05-04 15:31:21 -07001288
1289/*
1290 * Iterate every dquot of a particular type. The caller must ensure that the
1291 * particular quota type is active. iter_fn can return negative error codes,
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001292 * or -ECANCELED to indicate that it wants to stop iterating.
Darrick J. Wong554ba962018-05-04 15:31:21 -07001293 */
1294int
1295xfs_qm_dqiterate(
1296 struct xfs_mount *mp,
1297 uint dqtype,
1298 xfs_qm_dqiterate_fn iter_fn,
1299 void *priv)
1300{
1301 struct xfs_dquot *dq;
1302 xfs_dqid_t id = 0;
1303 int error;
1304
1305 do {
1306 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1307 if (error == -ENOENT)
1308 return 0;
1309 if (error)
1310 return error;
1311
1312 error = iter_fn(dq, dqtype, priv);
1313 id = be32_to_cpu(dq->q_core.d_id);
1314 xfs_qm_dqput(dq);
1315 id++;
1316 } while (error == 0 && id != 0);
1317
1318 return error;
1319}