blob: d5b7f03e93c8db1ccf5c3355530f79998eeeef79 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
Christoph Hellwigbf72de32011-12-06 21:58:19 +000028 * Lock order:
29 *
30 * ip->i_lock
Christoph Hellwig9f920f12012-03-13 08:52:35 +000031 * qi->qi_tree_lock
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050032 * dquot->q_qlock (xfs_dqlock() and friends)
33 * dquot->q_flush (xfs_dqflock() and friends)
34 * qi->qi_lru_lock
Christoph Hellwigbf72de32011-12-06 21:58:19 +000035 *
36 * If two dquots need to be locked the order is user before group/project,
37 * otherwise by the lowest id first, see xfs_dqlock2.
38 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Christoph Hellwiga05931c2012-03-13 08:52:37 +000040struct kmem_zone *xfs_qm_dqtrxzone;
41static struct kmem_zone *xfs_qm_dqzone;
42
Dave Chinnerf112a042013-09-30 09:37:03 +100043static struct lock_class_key xfs_dquot_group_class;
44static struct lock_class_key xfs_dquot_project_class;
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * This is called to free all the memory associated with a dquot
48 */
49void
50xfs_qm_dqdestroy(
Pavel Reichlaefe69a2019-11-12 17:04:02 -080051 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Hellwigf8739c32012-03-13 08:52:34 +000053 ASSERT(list_empty(&dqp->q_lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100055 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 mutex_destroy(&dqp->q_qlock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000057
Bill O'Donnellff6d6af2015-10-12 18:21:22 +110058 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
Carlos Maiolino377bcd52019-11-14 12:43:04 -080059 kmem_cache_free(xfs_qm_dqzone, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * If default limits are in force, push them into the dquot now.
64 * We overwrite the dquot limits only if they are zero and this
65 * is not the root dquot.
66 */
67void
68xfs_qm_adjust_dqlimits(
Brian Foster4b6eae2e2013-03-18 10:51:45 -040069 struct xfs_mount *mp,
70 struct xfs_dquot *dq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Brian Foster4b6eae2e2013-03-18 10:51:45 -040072 struct xfs_quotainfo *q = mp->m_quotainfo;
73 struct xfs_disk_dquot *d = &dq->q_core;
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
77 ASSERT(d->d_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
Carlos Maiolinobe607942016-02-08 11:27:55 +110080 if (defq->bsoftlimit && !d->d_blk_softlimit) {
81 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
Brian Fosterb1366452013-03-18 10:51:46 -040082 prealloc = 1;
83 }
Carlos Maiolinobe607942016-02-08 11:27:55 +110084 if (defq->bhardlimit && !d->d_blk_hardlimit) {
85 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
Brian Fosterb1366452013-03-18 10:51:46 -040086 prealloc = 1;
87 }
Carlos Maiolinobe607942016-02-08 11:27:55 +110088 if (defq->isoftlimit && !d->d_ino_softlimit)
89 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
90 if (defq->ihardlimit && !d->d_ino_hardlimit)
91 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
92 if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
93 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
94 if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
95 d->d_rtb_hardlimit = cpu_to_be64(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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 ASSERT(d->d_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
Christoph Hellwig1149d962005-11-02 15:01:12 +1100127 if (d->d_blk_hardlimit)
128 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
129 be64_to_cpu(d->d_blk_hardlimit));
130 if (d->d_ino_hardlimit)
131 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
132 be64_to_cpu(d->d_ino_hardlimit));
133 if (d->d_rtb_hardlimit)
134 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
135 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100139 if ((d->d_blk_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000140 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100141 be64_to_cpu(d->d_blk_softlimit))) ||
142 (d->d_blk_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000143 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100144 be64_to_cpu(d->d_blk_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800145 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700146 defq->btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000147 } else {
148 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 } else {
151 if ((!d->d_blk_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000152 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100153 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 (!d->d_blk_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000155 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100156 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 d->d_btimer = 0;
158 }
159 }
160
161 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100162 if ((d->d_ino_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000163 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100164 be64_to_cpu(d->d_ino_softlimit))) ||
165 (d->d_ino_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000166 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100167 be64_to_cpu(d->d_ino_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800168 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700169 defq->itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000170 } else {
171 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 } else {
174 if ((!d->d_ino_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000175 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100176 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 (!d->d_ino_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000178 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100179 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 d->d_itimer = 0;
181 }
182 }
183
184 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100185 if ((d->d_rtb_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000186 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100187 be64_to_cpu(d->d_rtb_softlimit))) ||
188 (d->d_rtb_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000189 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100190 be64_to_cpu(d->d_rtb_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800191 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() +
Eric Sandeene8503012020-05-21 13:07:01 -0700192 defq->rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000193 } else {
194 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 } else {
197 if ((!d->d_rtb_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000198 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100199 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 (!d->d_rtb_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000201 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100202 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 d->d_rtbtimer = 0;
204 }
205 }
206}
207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * initialize a buffer full of dquots and log the whole thing
210 */
211STATIC void
212xfs_qm_init_dquot_blk(
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700213 struct xfs_trans *tp,
214 struct xfs_mount *mp,
215 xfs_dqid_t id,
216 uint type,
217 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000219 struct xfs_quotainfo *q = mp->m_quotainfo;
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700220 struct xfs_dqblk *d;
221 xfs_dqid_t curid;
222 unsigned int qflag;
223 unsigned int blftype;
224 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200227 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Chandra Seetharaman62926042011-07-22 23:40:15 +0000229 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /*
232 * ID of the first dquot in the block - id's are zero based.
233 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000234 curid = id - (id % q->qi_dqperchunk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000235 memset(d, 0, BBTOB(q->qi_dqchunklen));
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000236 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
237 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
238 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
239 d->dd_diskdq.d_id = cpu_to_be32(curid);
240 d->dd_diskdq.d_flags = type;
Dave Chinner6fcdc592013-06-03 15:28:46 +1000241 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner92863452015-08-19 10:32:01 +1000242 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000243 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
244 XFS_DQUOT_CRC_OFF);
245 }
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000246 }
247
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700248 if (type & XFS_DQ_USER) {
249 qflag = XFS_UQUOTA_CHKD;
250 blftype = XFS_BLF_UDQUOT_BUF;
251 } else if (type & XFS_DQ_PROJ) {
252 qflag = XFS_PQUOTA_CHKD;
253 blftype = XFS_BLF_PDQUOT_BUF;
254 } else {
255 qflag = XFS_GQUOTA_CHKD;
256 blftype = XFS_BLF_GDQUOT_BUF;
257 }
258
259 xfs_trans_dquot_buf(tp, bp, blftype);
260
261 /*
262 * quotacheck uses delayed writes to update all the dquots on disk in an
263 * efficient manner instead of logging the individual dquot changes as
264 * they are made. However if we log the buffer allocated here and crash
265 * after quotacheck while the logged initialisation is still in the
266 * active region of the log, log recovery can replay the dquot buffer
267 * initialisation over the top of the checked dquots and corrupt quota
268 * accounting.
269 *
270 * To avoid this problem, quotacheck cannot log the initialised buffer.
271 * We must still dirty the buffer and write it back before the
272 * allocation transaction clears the log. Therefore, mark the buffer as
273 * ordered instead of logging it directly. This is safe for quotacheck
274 * because it detects and repairs allocated but initialized dquot blocks
275 * in the quota inodes.
276 */
277 if (!(mp->m_qflags & qflag))
278 xfs_trans_ordered_buf(tp, bp);
279 else
280 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Brian Fosterb1366452013-03-18 10:51:46 -0400283/*
284 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
285 * watermarks correspond to the soft and hard limits by default. If a soft limit
286 * is not specified, we use 95% of the hard limit.
287 */
288void
289xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
290{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700291 uint64_t space;
Brian Fosterb1366452013-03-18 10:51:46 -0400292
293 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
294 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
295 if (!dqp->q_prealloc_lo_wmark) {
296 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
297 do_div(dqp->q_prealloc_lo_wmark, 100);
298 dqp->q_prealloc_lo_wmark *= 95;
299 }
300
301 space = dqp->q_prealloc_hi_wmark;
302
303 do_div(space, 100);
304 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
305 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
306 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700310 * Ensure that the given in-core dquot has a buffer on disk backing it, and
Darrick J. Wong710d7072019-04-24 09:27:41 -0700311 * return the buffer locked and held. This is called when the bmapi finds a
312 * hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700315xfs_dquot_disk_alloc(
316 struct xfs_trans **tpp,
317 struct xfs_dquot *dqp,
318 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700320 struct xfs_bmbt_irec map;
Brian Foster2ba13722018-07-11 22:26:11 -0700321 struct xfs_trans *tp = *tpp;
322 struct xfs_mount *mp = tp->t_mountp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700323 struct xfs_buf *bp;
324 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700325 int nmaps = 1;
326 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000327
328 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xfs_ilock(quotip, XFS_ILOCK_EXCL);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000331 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700332 /*
333 * Return if this type of quotas is turned off while we didn't
334 * have an inode lock
335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
Dave Chinner24513372014-06-25 14:58:08 +1000337 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700340 /* Create the block mapping. */
Brian Foster2ba13722018-07-11 22:26:11 -0700341 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
342 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
Brian Fosterda781e62019-10-21 09:26:48 -0700343 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
344 &nmaps);
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000345 if (error)
Brian Foster73971b12018-08-07 10:57:13 -0700346 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
348 ASSERT(nmaps == 1);
349 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
350 (map.br_startblock != HOLESTARTBLOCK));
351
352 /*
353 * Keep track of the blkno to save a lookup later
354 */
355 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
356
357 /* now we can just get the buffer (there's nothing to read yet) */
Darrick J. Wongce924642020-01-23 17:01:18 -0800358 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
359 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
360 if (error)
361 return error;
Dave Chinner1813dd62012-11-14 17:54:40 +1100362 bp->b_ops = &xfs_dquot_buf_ops;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Make a chunk of dquots out of this buffer and log
366 * the entire thing.
367 */
Brian Foster2ba13722018-07-11 22:26:11 -0700368 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000369 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700370 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Tim Shimminefa092f2005-09-05 08:29:01 +1000372 /*
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700373 * Hold the buffer and join it to the dfops so that we'll still own
374 * the buffer when we return to the caller. The buffer disposal on
375 * error must be paid attention to very carefully, as it has been
376 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
377 * code when allocating a new dquot record" in 2005, and the later
378 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
379 * the buffer locked across the _defer_finish call. We can now do
380 * this correctly with xfs_defer_bjoin.
Tim Shimminefa092f2005-09-05 08:29:01 +1000381 *
Brian Foster73971b12018-08-07 10:57:13 -0700382 * Above, we allocated a disk block for the dquot information and used
383 * get_buf to initialize the dquot. If the _defer_finish fails, the old
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700384 * transaction is gone but the new buffer is not joined or held to any
385 * transaction, so we must _buf_relse it.
Tim Shimminefa092f2005-09-05 08:29:01 +1000386 *
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700387 * If everything succeeds, the caller of this function is returned a
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700388 * buffer that is locked and held to the transaction. The caller
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700389 * is responsible for unlocking any buffer passed back, either
Darrick J. Wong710d7072019-04-24 09:27:41 -0700390 * manually or by committing the transaction. On error, the buffer is
391 * released and not passed back.
Tim Shimminefa092f2005-09-05 08:29:01 +1000392 */
Brian Foster2ba13722018-07-11 22:26:11 -0700393 xfs_trans_bhold(tp, bp);
Brian Foster9e28a242018-07-24 13:43:15 -0700394 error = xfs_defer_finish(tpp);
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700395 if (error) {
Darrick J. Wong710d7072019-04-24 09:27:41 -0700396 xfs_trans_bhold_release(*tpp, bp);
397 xfs_trans_brelse(*tpp, bp);
Brian Foster73971b12018-08-07 10:57:13 -0700398 return error;
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700399 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700400 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Dave Chinner9aede1d2013-10-15 09:17:52 +1100403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700405 * Read in the in-core dquot's on-disk metadata and return the buffer.
406 * Returns ENOENT to signal a hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
408STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700409xfs_dquot_disk_read(
410 struct xfs_mount *mp,
411 struct xfs_dquot *dqp,
412 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500414 struct xfs_bmbt_irec map;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500415 struct xfs_buf *bp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700416 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700417 uint lock_mode;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700418 int nmaps = 1;
419 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Christoph Hellwig0891f992017-07-13 12:14:34 -0700421 lock_mode = xfs_ilock_data_map_shared(quotip);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700422 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000424 * Return if this type of quotas is turned off while we
425 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Christoph Hellwig0891f992017-07-13 12:14:34 -0700427 xfs_iunlock(quotip, lock_mode);
Dave Chinner24513372014-06-25 14:58:08 +1000428 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000432 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000434 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700435 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700436 xfs_iunlock(quotip, lock_mode);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000437 if (error)
438 return error;
439
440 ASSERT(nmaps == 1);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700441 ASSERT(map.br_blockcount >= 1);
442 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
443 if (map.br_startblock == HOLESTARTBLOCK)
444 return -ENOENT;
445
446 trace_xfs_dqtobp_read(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000447
448 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700449 * store the blkno etc so that we don't have to do the
450 * mapping all the time
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000451 */
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700452 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000453
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700454 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
455 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
456 &xfs_dquot_buf_ops);
457 if (error) {
458 ASSERT(bp == NULL);
459 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Dave Chinnerc6319192012-11-14 17:50:13 +1100462 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700463 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
464 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Eric Sandeend99831f2014-06-22 15:03:54 +1000466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700469/* Allocate and initialize everything we need for an incore dquot. */
470STATIC struct xfs_dquot *
471xfs_dquot_alloc(
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000472 struct xfs_mount *mp,
473 xfs_dqid_t id,
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700474 uint type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000476 struct xfs_dquot *dqp;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000477
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700478 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000479
480 dqp->dq_flags = type;
481 dqp->q_core.d_id = cpu_to_be32(id);
482 dqp->q_mount = mp;
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000483 INIT_LIST_HEAD(&dqp->q_lru);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000484 mutex_init(&dqp->q_qlock);
485 init_waitqueue_head(&dqp->q_pinwait);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700486 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
487 /*
488 * Offset of dquot in the (fixed sized) dquot chunk.
489 */
490 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
491 sizeof(xfs_dqblk_t);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000492
493 /*
494 * Because we want to use a counting completion, complete
495 * the flush completion once to allow a single access to
496 * the flush completion without blocking.
497 */
498 init_completion(&dqp->q_flush);
499 complete(&dqp->q_flush);
500
501 /*
502 * Make sure group quotas have a different lock class than user
503 * quotas.
504 */
Dave Chinnerf112a042013-09-30 09:37:03 +1000505 switch (type) {
506 case XFS_DQ_USER:
507 /* uses the default lock class */
508 break;
509 case XFS_DQ_GROUP:
510 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
511 break;
512 case XFS_DQ_PROJ:
513 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
514 break;
515 default:
516 ASSERT(0);
517 break;
518 }
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000519
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700520 xfs_qm_dquot_logitem_init(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700522 XFS_STATS_INC(mp, xs_qm_dquot);
523 return dqp;
524}
525
526/* Copy the in-core quota fields in from the on-disk buffer. */
527STATIC void
528xfs_dquot_from_disk(
529 struct xfs_dquot *dqp,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700530 struct xfs_buf *bp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700531{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700532 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
533
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700534 /* copy everything from disk dquot to the incore dquot */
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800535 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot));
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700536
537 /*
538 * Reservation counters are defined as reservation plus current usage
539 * to avoid having to add every time.
540 */
541 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
542 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
543 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
544
545 /* initialize the dquot speculative prealloc thresholds */
546 xfs_dquot_set_prealloc_limits(dqp);
547}
548
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700549/* Allocate and initialize the dquot buffer for this in-core dquot. */
550static int
551xfs_qm_dqread_alloc(
552 struct xfs_mount *mp,
553 struct xfs_dquot *dqp,
554 struct xfs_buf **bpp)
555{
556 struct xfs_trans *tp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700557 int error;
558
559 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
560 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
561 if (error)
562 goto err;
563
Darrick J. Wong710d7072019-04-24 09:27:41 -0700564 error = xfs_dquot_disk_alloc(&tp, dqp, bpp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700565 if (error)
566 goto err_cancel;
567
568 error = xfs_trans_commit(tp);
569 if (error) {
570 /*
571 * Buffer was held to the transaction, so we have to unlock it
572 * manually here because we're not passing it back.
573 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700574 xfs_buf_relse(*bpp);
575 *bpp = NULL;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700576 goto err;
577 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700578 return 0;
579
580err_cancel:
581 xfs_trans_cancel(tp);
582err:
583 return error;
584}
585
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700586/*
587 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700588 * and release the buffer immediately. If @can_alloc is true, fill any
589 * holes in the on-disk metadata.
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700590 */
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700591static int
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700592xfs_qm_dqread(
593 struct xfs_mount *mp,
594 xfs_dqid_t id,
595 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700596 bool can_alloc,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700597 struct xfs_dquot **dqpp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700598{
599 struct xfs_dquot *dqp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700600 struct xfs_buf *bp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700601 int error;
602
603 dqp = xfs_dquot_alloc(mp, id, type);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000604 trace_xfs_dqread(dqp);
605
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700606 /* Try to read the buffer, allocating if necessary. */
607 error = xfs_dquot_disk_read(mp, dqp, &bp);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700608 if (error == -ENOENT && can_alloc)
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700609 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
610 if (error)
611 goto err;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700614 * At this point we should have a clean locked buffer. Copy the data
615 * to the incore dquot and release the buffer since the incore dquot
616 * has its own locking protocol so we needn't tie up the buffer any
617 * further.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200619 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700620 xfs_dquot_from_disk(dqp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700622 xfs_buf_relse(bp);
623 *dqpp = dqp;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000624 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700626err:
627 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 xfs_qm_dqdestroy(dqp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700629 *dqpp = NULL;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000630 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
Eric Sandeen296c24e2016-02-08 11:27:38 +1100634 * Advance to the next id in the current chunk, or if at the
635 * end of the chunk, skip ahead to first id in next allocated chunk
636 * using the SEEK_DATA interface.
637 */
Eryu Guan6e3e6d52016-04-06 09:47:21 +1000638static int
Eric Sandeen296c24e2016-02-08 11:27:38 +1100639xfs_dq_get_next_id(
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700640 struct xfs_mount *mp,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100641 uint type,
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700642 xfs_dqid_t *id)
Eric Sandeen296c24e2016-02-08 11:27:38 +1100643{
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700644 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
645 xfs_dqid_t next_id = *id + 1; /* simple advance */
646 uint lock_flags;
647 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700648 struct xfs_iext_cursor cur;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100649 xfs_fsblock_t start;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100650 int error = 0;
651
Eric Sandeen657bdfb2017-01-17 11:43:38 -0800652 /* If we'd wrap past the max ID, stop */
653 if (next_id < *id)
654 return -ENOENT;
655
Eric Sandeen296c24e2016-02-08 11:27:38 +1100656 /* If new ID is within the current chunk, advancing it sufficed */
657 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
658 *id = next_id;
659 return 0;
660 }
661
662 /* Nope, next_id is now past the current chunk, so find the next one */
663 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
664
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700665 lock_flags = xfs_ilock_data_map_shared(quotip);
666 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
667 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
668 if (error)
669 return error;
670 }
Eric Sandeen296c24e2016-02-08 11:27:38 +1100671
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700672 if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
Brian Foster2192b0b2017-07-05 12:07:52 -0700673 /* contiguous chunk, bump startoff for the id calculation */
674 if (got.br_startoff < start)
675 got.br_startoff = start;
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700676 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
Brian Foster2192b0b2017-07-05 12:07:52 -0700677 } else {
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700678 error = -ENOENT;
Brian Foster2192b0b2017-07-05 12:07:52 -0700679 }
680
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700681 xfs_iunlock(quotip, lock_flags);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100682
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700683 return error;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100684}
685
686/*
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700687 * Look up the dquot in the in-core cache. If found, the dquot is returned
688 * locked and ready to go.
689 */
690static struct xfs_dquot *
691xfs_qm_dqget_cache_lookup(
692 struct xfs_mount *mp,
693 struct xfs_quotainfo *qi,
694 struct radix_tree_root *tree,
695 xfs_dqid_t id)
696{
697 struct xfs_dquot *dqp;
698
699restart:
700 mutex_lock(&qi->qi_tree_lock);
701 dqp = radix_tree_lookup(tree, id);
702 if (!dqp) {
703 mutex_unlock(&qi->qi_tree_lock);
704 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
705 return NULL;
706 }
707
708 xfs_dqlock(dqp);
709 if (dqp->dq_flags & XFS_DQ_FREEING) {
710 xfs_dqunlock(dqp);
711 mutex_unlock(&qi->qi_tree_lock);
712 trace_xfs_dqget_freeing(dqp);
713 delay(1);
714 goto restart;
715 }
716
717 dqp->q_nrefs++;
718 mutex_unlock(&qi->qi_tree_lock);
719
720 trace_xfs_dqget_hit(dqp);
721 XFS_STATS_INC(mp, xs_qm_dqcachehits);
722 return dqp;
723}
724
725/*
726 * Try to insert a new dquot into the in-core cache. If an error occurs the
727 * caller should throw away the dquot and start over. Otherwise, the dquot
728 * is returned locked (and held by the cache) as if there had been a cache
729 * hit.
730 */
731static int
732xfs_qm_dqget_cache_insert(
733 struct xfs_mount *mp,
734 struct xfs_quotainfo *qi,
735 struct radix_tree_root *tree,
736 xfs_dqid_t id,
737 struct xfs_dquot *dqp)
738{
739 int error;
740
741 mutex_lock(&qi->qi_tree_lock);
742 error = radix_tree_insert(tree, id, dqp);
743 if (unlikely(error)) {
744 /* Duplicate found! Caller must try again. */
745 WARN_ON(error != -EEXIST);
746 mutex_unlock(&qi->qi_tree_lock);
747 trace_xfs_dqget_dup(dqp);
748 return error;
749 }
750
751 /* Return a locked dquot to the caller, with a reference taken. */
752 xfs_dqlock(dqp);
753 dqp->q_nrefs = 1;
754
755 qi->qi_dquots++;
756 mutex_unlock(&qi->qi_tree_lock);
757
758 return 0;
759}
760
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700761/* Check our input parameters. */
762static int
763xfs_qm_dqget_checks(
764 struct xfs_mount *mp,
765 uint type)
766{
767 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
768 return -ESRCH;
769
770 switch (type) {
771 case XFS_DQ_USER:
772 if (!XFS_IS_UQUOTA_ON(mp))
773 return -ESRCH;
774 return 0;
775 case XFS_DQ_GROUP:
776 if (!XFS_IS_GQUOTA_ON(mp))
777 return -ESRCH;
778 return 0;
779 case XFS_DQ_PROJ:
780 if (!XFS_IS_PQUOTA_ON(mp))
781 return -ESRCH;
782 return 0;
783 default:
784 WARN_ON_ONCE(0);
785 return -EINVAL;
786 }
787}
788
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700789/*
Darrick J. Wong4882c192018-05-04 15:30:22 -0700790 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
791 * dquot, doing an allocation (if requested) as needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
793int
794xfs_qm_dqget(
Darrick J. Wong4882c192018-05-04 15:30:22 -0700795 struct xfs_mount *mp,
796 xfs_dqid_t id,
797 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700798 bool can_alloc,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700799 struct xfs_dquot **O_dqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000801 struct xfs_quotainfo *qi = mp->m_quotainfo;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700802 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000803 struct xfs_dquot *dqp;
804 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700806 error = xfs_qm_dqget_checks(mp, type);
807 if (error)
808 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Darrick J. Wong4882c192018-05-04 15:30:22 -0700810restart:
811 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
812 if (dqp) {
813 *O_dqpp = dqp;
814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Christoph Hellwig92678552011-12-06 21:58:18 +0000816
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700817 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700818 if (error)
819 return error;
820
821 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
822 if (error) {
823 /*
824 * Duplicate found. Just throw away the new dquot and start
825 * over.
826 */
827 xfs_qm_dqdestroy(dqp);
828 XFS_STATS_INC(mp, xs_qm_dquot_dups);
829 goto restart;
830 }
831
832 trace_xfs_dqget_miss(dqp);
833 *O_dqpp = dqp;
834 return 0;
835}
836
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700837/*
838 * Given a dquot id and type, read and initialize a dquot from the on-disk
839 * metadata. This function is only for use during quota initialization so
840 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
841 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
842 */
843int
844xfs_qm_dqget_uncached(
845 struct xfs_mount *mp,
846 xfs_dqid_t id,
847 uint type,
848 struct xfs_dquot **dqpp)
849{
850 int error;
851
852 error = xfs_qm_dqget_checks(mp, type);
853 if (error)
854 return error;
855
856 return xfs_qm_dqread(mp, id, type, 0, dqpp);
857}
858
Darrick J. Wong4882c192018-05-04 15:30:22 -0700859/* Return the quota id for a given inode and type. */
860xfs_dqid_t
861xfs_qm_id_for_quotatype(
862 struct xfs_inode *ip,
863 uint type)
864{
865 switch (type) {
866 case XFS_DQ_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800867 return i_uid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700868 case XFS_DQ_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800869 return i_gid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700870 case XFS_DQ_PROJ:
Christoph Hellwigde7a8662019-11-12 08:22:54 -0800871 return ip->i_d.di_projid;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700872 }
873 ASSERT(0);
874 return 0;
875}
876
877/*
878 * Return the dquot for a given inode and type. If @can_alloc is true, then
879 * allocate blocks if needed. The inode's ILOCK must be held and it must not
880 * have already had an inode attached.
881 */
882int
883xfs_qm_dqget_inode(
884 struct xfs_inode *ip,
885 uint type,
886 bool can_alloc,
887 struct xfs_dquot **O_dqpp)
888{
889 struct xfs_mount *mp = ip->i_mount;
890 struct xfs_quotainfo *qi = mp->m_quotainfo;
891 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
892 struct xfs_dquot *dqp;
893 xfs_dqid_t id;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700894 int error;
895
896 error = xfs_qm_dqget_checks(mp, type);
897 if (error)
898 return error;
899
Darrick J. Wong4882c192018-05-04 15:30:22 -0700900 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
901 ASSERT(xfs_inode_dquot(ip, type) == NULL);
902
903 id = xfs_qm_id_for_quotatype(ip, type);
904
Christoph Hellwig92678552011-12-06 21:58:18 +0000905restart:
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700906 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000907 if (dqp) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000908 *O_dqpp = dqp;
909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /*
913 * Dquot cache miss. We don't want to keep the inode lock across
914 * a (potential) disk read. Also we don't want to deal with the lock
915 * ordering between quotainode and this inode. OTOH, dropping the inode
916 * lock here means dealing with a chown that can happen before
917 * we re-acquire the lock.
918 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700920 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700921 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000922 if (error)
923 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Darrick J. Wong4882c192018-05-04 15:30:22 -0700925 /*
926 * A dquot could be attached to this inode by now, since we had
927 * dropped the ilock.
928 */
929 if (xfs_this_quota_on(mp, type)) {
930 struct xfs_dquot *dqp1;
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000931
Darrick J. Wong4882c192018-05-04 15:30:22 -0700932 dqp1 = xfs_inode_dquot(ip, type);
933 if (dqp1) {
Chandra Seetharaman36731412012-01-23 17:31:30 +0000934 xfs_qm_dqdestroy(dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700935 dqp = dqp1;
936 xfs_dqlock(dqp);
937 goto dqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Darrick J. Wong4882c192018-05-04 15:30:22 -0700939 } else {
940 /* inode stays locked on return */
941 xfs_qm_dqdestroy(dqp);
942 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700945 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
946 if (error) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000947 /*
948 * Duplicate found. Just throw away the new dquot and start
949 * over.
950 */
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000951 xfs_qm_dqdestroy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100952 XFS_STATS_INC(mp, xs_qm_dquot_dups);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000953 goto restart;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Darrick J. Wong4882c192018-05-04 15:30:22 -0700956dqret:
957 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000958 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 *O_dqpp = dqp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000963/*
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700964 * Starting at @id and progressing upwards, look for an initialized incore
965 * dquot, lock it, and return it.
966 */
967int
968xfs_qm_dqget_next(
969 struct xfs_mount *mp,
970 xfs_dqid_t id,
971 uint type,
972 struct xfs_dquot **dqpp)
973{
974 struct xfs_dquot *dqp;
975 int error = 0;
976
977 *dqpp = NULL;
978 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700979 error = xfs_qm_dqget(mp, id, type, false, &dqp);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700980 if (error == -ENOENT)
981 continue;
982 else if (error != 0)
983 break;
984
985 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
986 *dqpp = dqp;
987 return 0;
988 }
989
990 xfs_qm_dqput(dqp);
991 }
992
993 return error;
994}
995
996/*
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000997 * Release a reference to the dquot (decrement ref-count) and unlock it.
998 *
999 * If there is a group quota attached to this dquot, carefully release that
1000 * too without tripping over deadlocks'n'stuff.
1001 */
1002void
1003xfs_qm_dqput(
1004 struct xfs_dquot *dqp)
1005{
1006 ASSERT(dqp->q_nrefs > 0);
1007 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1008
1009 trace_xfs_dqput(dqp);
1010
Dave Chinner3c35337572014-05-05 17:30:15 +10001011 if (--dqp->q_nrefs == 0) {
1012 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1013 trace_xfs_dqput_free(dqp);
1014
1015 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001016 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner3c35337572014-05-05 17:30:15 +10001017 }
1018 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
1021/*
1022 * Release a dquot. Flush it if dirty, then dqput() it.
1023 * dquot must not be locked.
1024 */
1025void
1026xfs_qm_dqrele(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001027 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001029 if (!dqp)
1030 return;
1031
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001032 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 xfs_dqlock(dqp);
1035 /*
1036 * We don't care to flush it if the dquot is dirty here.
1037 * That will create stutters that we want to avoid.
1038 * Instead we do a delayed write when we try to reclaim
1039 * a dirty dquot. Also xfs_sync will take part of the burden...
1040 */
1041 xfs_qm_dqput(dqp);
1042}
1043
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001044/*
1045 * This is the dquot flushing I/O completion routine. It is called
1046 * from interrupt level when the buffer containing the dquot is
1047 * flushed to disk. It is responsible for removing the dquot logitem
1048 * from the AIL if it has not been re-logged, and unlocking the dquot's
1049 * flush lock. This behavior is very similar to that of inodes..
1050 */
1051STATIC void
1052xfs_qm_dqflush_done(
1053 struct xfs_buf *bp,
1054 struct xfs_log_item *lip)
1055{
Pavel Reichlfd8b81d2019-11-12 17:04:26 -08001056 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001057 struct xfs_dquot *dqp = qip->qli_dquot;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001058 struct xfs_ail *ailp = lip->li_ailp;
Brian Foster849274c2020-05-06 13:25:23 -07001059 xfs_lsn_t tail_lsn;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001060
1061 /*
1062 * We only want to pull the item from the AIL if its
1063 * location in the log has not changed since we started the flush.
1064 * Thus, we only bother if the dquot's lsn has
1065 * not changed. First we check the lsn outside the lock
1066 * since it's cheaper, and then we recheck while
1067 * holding the lock before removing the dquot from the AIL.
1068 */
Dave Chinner22525c12018-05-09 07:47:34 -07001069 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
Carlos Maiolino373b0582017-11-28 08:54:10 -08001070 ((lip->li_lsn == qip->qli_flush_lsn) ||
Dave Chinner22525c12018-05-09 07:47:34 -07001071 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001072
Matthew Wilcox57e80952018-03-07 14:59:39 -08001073 spin_lock(&ailp->ail_lock);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001074 if (lip->li_lsn == qip->qli_flush_lsn) {
Brian Foster849274c2020-05-06 13:25:23 -07001075 /* xfs_ail_update_finish() drops the AIL lock */
1076 tail_lsn = xfs_ail_delete_one(ailp, lip);
1077 xfs_ail_update_finish(ailp, tail_lsn);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001078 } else {
1079 /*
1080 * Clear the failed state since we are about to drop the
1081 * flush lock
1082 */
Dave Chinner22525c12018-05-09 07:47:34 -07001083 xfs_clear_li_failed(lip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08001084 spin_unlock(&ailp->ail_lock);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001085 }
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001086 }
1087
1088 /*
1089 * Release the dq's flush lock since we're done with it.
1090 */
1091 xfs_dqfunlock(dqp);
1092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094/*
1095 * Write a modified dquot to disk.
1096 * The dquot must be locked and the flush lock too taken by caller.
1097 * The flush lock will not be unlocked until the dquot reaches the disk,
1098 * but the dquot is free to be unlocked and modified by the caller
1099 * in the interim. Dquot is still locked on return. This behavior is
1100 * identical to that of inodes.
1101 */
1102int
1103xfs_qm_dqflush(
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001104 struct xfs_dquot *dqp,
1105 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001107 struct xfs_mount *mp = dqp->q_mount;
Brian Fosterb707fff2020-05-06 13:25:22 -07001108 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001109 struct xfs_buf *bp;
Eric Sandeen7224fa42018-05-07 09:20:18 -07001110 struct xfs_dqblk *dqb;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001111 struct xfs_disk_dquot *ddqp;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001112 xfs_failaddr_t fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001116 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001117
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001118 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001120 *bpp = NULL;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 xfs_qm_dqunpin_wait(dqp);
1123
1124 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001127 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
Brian Foster8d3d7e22020-03-27 08:29:45 -07001128 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1129 &bp, &xfs_dquot_buf_ops);
Brian Fosterb707fff2020-05-06 13:25:22 -07001130 if (error == -EAGAIN)
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001131 goto out_unlock;
Brian Fosterb707fff2020-05-06 13:25:22 -07001132 if (error)
1133 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001135 /*
1136 * Calculate the location of the dquot inside the buffer.
1137 */
Eric Sandeen7224fa42018-05-07 09:20:18 -07001138 dqb = bp->b_addr + dqp->q_bufoffset;
1139 ddqp = &dqb->dd_diskdq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001140
Brian Foster629dcb32020-05-06 13:25:22 -07001141 /* sanity check the in-core structure before we flush */
1142 fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id),
1143 0);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001144 if (fa) {
1145 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
Brian Foster629dcb32020-05-06 13:25:22 -07001146 be32_to_cpu(dqp->q_core.d_id), fa);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001147 xfs_buf_relse(bp);
Brian Fosterb707fff2020-05-06 13:25:22 -07001148 error = -EFSCORRUPTED;
1149 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
1152 /* This is the only portion of data that needs to persist */
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001153 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /*
1156 * Clear the dirty field and remember the flush lsn for later use.
1157 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001158 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
David Chinner7b2e2a32008-10-30 17:39:12 +11001160 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1161 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /*
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001164 * copy the lsn into the on-disk dquot now while we have the in memory
1165 * dquot here. This can't be done later in the write verifier as we
1166 * can't get access to the log item at that point in time.
Dave Chinner6fcdc592013-06-03 15:28:46 +10001167 *
1168 * We also calculate the CRC here so that the on-disk dquot in the
1169 * buffer always has a valid CRC. This ensures there is no possibility
1170 * of a dquot without an up-to-date CRC getting to disk.
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001171 */
1172 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001173 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
Dave Chinner6fcdc592013-06-03 15:28:46 +10001174 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1175 XFS_DQUOT_CRC_OFF);
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001176 }
1177
1178 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * Attach an iodone routine so that we can remove this dquot from the
1180 * AIL and release the flush lock once the dquot is synced to disk.
1181 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001182 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1183 &dqp->q_logitem.qli_item);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * If the buffer is pinned then push on the log so we won't
1187 * get stuck waiting in the write for too long.
1188 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001189 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001190 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001191 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001194 trace_xfs_dqflush_done(dqp);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001195 *bpp = bp;
1196 return 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001197
Brian Fosterb707fff2020-05-06 13:25:22 -07001198out_abort:
1199 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Brian Foster2b3cf092020-05-06 13:27:04 -07001200 xfs_trans_ail_delete(lip, 0);
Brian Fosterb707fff2020-05-06 13:25:22 -07001201 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001202out_unlock:
1203 xfs_dqfunlock(dqp);
Brian Foster8d3d7e22020-03-27 08:29:45 -07001204 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001207/*
1208 * Lock two xfs_dquot structures.
1209 *
1210 * To avoid deadlocks we always lock the quota structure with
1211 * the lowerd id first.
1212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213void
1214xfs_dqlock2(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001215 struct xfs_dquot *d1,
1216 struct xfs_dquot *d2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 if (d1 && d2) {
1219 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001220 if (be32_to_cpu(d1->q_core.d_id) >
1221 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001222 mutex_lock(&d2->q_qlock);
1223 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001225 mutex_lock(&d1->q_qlock);
1226 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001228 } else if (d1) {
1229 mutex_lock(&d1->q_qlock);
1230 } else if (d2) {
1231 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233}
1234
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001235int __init
1236xfs_qm_init(void)
1237{
Carlos Maiolinob1231762019-11-14 12:43:03 -08001238 xfs_qm_dqzone = kmem_cache_create("xfs_dquot",
1239 sizeof(struct xfs_dquot),
1240 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001241 if (!xfs_qm_dqzone)
1242 goto out;
1243
Carlos Maiolinob1231762019-11-14 12:43:03 -08001244 xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx",
1245 sizeof(struct xfs_dquot_acct),
1246 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001247 if (!xfs_qm_dqtrxzone)
1248 goto out_free_dqzone;
1249
1250 return 0;
1251
1252out_free_dqzone:
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001253 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001254out:
1255 return -ENOMEM;
1256}
1257
Gerard Snitselaar1c2ccc62012-03-16 18:36:18 +00001258void
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001259xfs_qm_exit(void)
1260{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001261 kmem_cache_destroy(xfs_qm_dqtrxzone);
1262 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001263}
Darrick J. Wong554ba962018-05-04 15:31:21 -07001264
1265/*
1266 * Iterate every dquot of a particular type. The caller must ensure that the
1267 * particular quota type is active. iter_fn can return negative error codes,
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001268 * or -ECANCELED to indicate that it wants to stop iterating.
Darrick J. Wong554ba962018-05-04 15:31:21 -07001269 */
1270int
1271xfs_qm_dqiterate(
1272 struct xfs_mount *mp,
1273 uint dqtype,
1274 xfs_qm_dqiterate_fn iter_fn,
1275 void *priv)
1276{
1277 struct xfs_dquot *dq;
1278 xfs_dqid_t id = 0;
1279 int error;
1280
1281 do {
1282 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1283 if (error == -ENOENT)
1284 return 0;
1285 if (error)
1286 return error;
1287
1288 error = iter_fn(dq, dqtype, priv);
1289 id = be32_to_cpu(dq->q_core.d_id);
1290 xfs_qm_dqput(dq);
1291 id++;
1292 } while (error == 0 && id != 0);
1293
1294 return error;
1295}