blob: 6196f7c52b24239e6b9b8229b32610ffc5e8d0d7 [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 Sandeen3dbb9aa2020-05-21 13:07:00 -0700119 struct xfs_disk_dquot *d = &dq->q_core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 ASSERT(d->d_id);
121
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200122#ifdef DEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100123 if (d->d_blk_hardlimit)
124 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
125 be64_to_cpu(d->d_blk_hardlimit));
126 if (d->d_ino_hardlimit)
127 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
128 be64_to_cpu(d->d_ino_hardlimit));
129 if (d->d_rtb_hardlimit)
130 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
131 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100135 if ((d->d_blk_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000136 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100137 be64_to_cpu(d->d_blk_softlimit))) ||
138 (d->d_blk_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000139 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100140 be64_to_cpu(d->d_blk_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800141 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000142 mp->m_quotainfo->qi_btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000143 } else {
144 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 } else {
147 if ((!d->d_blk_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000148 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100149 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 (!d->d_blk_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000151 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100152 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 d->d_btimer = 0;
154 }
155 }
156
157 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100158 if ((d->d_ino_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000159 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100160 be64_to_cpu(d->d_ino_softlimit))) ||
161 (d->d_ino_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000162 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100163 be64_to_cpu(d->d_ino_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800164 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000165 mp->m_quotainfo->qi_itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000166 } else {
167 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 } else {
170 if ((!d->d_ino_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000171 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100172 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 (!d->d_ino_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000174 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100175 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 d->d_itimer = 0;
177 }
178 }
179
180 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100181 if ((d->d_rtb_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000182 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100183 be64_to_cpu(d->d_rtb_softlimit))) ||
184 (d->d_rtb_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000185 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100186 be64_to_cpu(d->d_rtb_hardlimit)))) {
Arnd Bergmannb8a08802020-01-02 13:27:45 -0800187 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000188 mp->m_quotainfo->qi_rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000189 } else {
190 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 } else {
193 if ((!d->d_rtb_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000194 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100195 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 (!d->d_rtb_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000197 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100198 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 d->d_rtbtimer = 0;
200 }
201 }
202}
203
204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * initialize a buffer full of dquots and log the whole thing
206 */
207STATIC void
208xfs_qm_init_dquot_blk(
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700209 struct xfs_trans *tp,
210 struct xfs_mount *mp,
211 xfs_dqid_t id,
212 uint type,
213 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000215 struct xfs_quotainfo *q = mp->m_quotainfo;
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700216 struct xfs_dqblk *d;
217 xfs_dqid_t curid;
218 unsigned int qflag;
219 unsigned int blftype;
220 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200223 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Chandra Seetharaman62926042011-07-22 23:40:15 +0000225 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /*
228 * ID of the first dquot in the block - id's are zero based.
229 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000230 curid = id - (id % q->qi_dqperchunk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000231 memset(d, 0, BBTOB(q->qi_dqchunklen));
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000232 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
233 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
234 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
235 d->dd_diskdq.d_id = cpu_to_be32(curid);
236 d->dd_diskdq.d_flags = type;
Dave Chinner6fcdc592013-06-03 15:28:46 +1000237 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner92863452015-08-19 10:32:01 +1000238 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000239 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
240 XFS_DQUOT_CRC_OFF);
241 }
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000242 }
243
Darrick J. Wong78bba5c2020-05-13 15:33:27 -0700244 if (type & XFS_DQ_USER) {
245 qflag = XFS_UQUOTA_CHKD;
246 blftype = XFS_BLF_UDQUOT_BUF;
247 } else if (type & XFS_DQ_PROJ) {
248 qflag = XFS_PQUOTA_CHKD;
249 blftype = XFS_BLF_PDQUOT_BUF;
250 } else {
251 qflag = XFS_GQUOTA_CHKD;
252 blftype = XFS_BLF_GDQUOT_BUF;
253 }
254
255 xfs_trans_dquot_buf(tp, bp, blftype);
256
257 /*
258 * quotacheck uses delayed writes to update all the dquots on disk in an
259 * efficient manner instead of logging the individual dquot changes as
260 * they are made. However if we log the buffer allocated here and crash
261 * after quotacheck while the logged initialisation is still in the
262 * active region of the log, log recovery can replay the dquot buffer
263 * initialisation over the top of the checked dquots and corrupt quota
264 * accounting.
265 *
266 * To avoid this problem, quotacheck cannot log the initialised buffer.
267 * We must still dirty the buffer and write it back before the
268 * allocation transaction clears the log. Therefore, mark the buffer as
269 * ordered instead of logging it directly. This is safe for quotacheck
270 * because it detects and repairs allocated but initialized dquot blocks
271 * in the quota inodes.
272 */
273 if (!(mp->m_qflags & qflag))
274 xfs_trans_ordered_buf(tp, bp);
275 else
276 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Brian Fosterb1366452013-03-18 10:51:46 -0400279/*
280 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
281 * watermarks correspond to the soft and hard limits by default. If a soft limit
282 * is not specified, we use 95% of the hard limit.
283 */
284void
285xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
286{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700287 uint64_t space;
Brian Fosterb1366452013-03-18 10:51:46 -0400288
289 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
290 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
291 if (!dqp->q_prealloc_lo_wmark) {
292 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
293 do_div(dqp->q_prealloc_lo_wmark, 100);
294 dqp->q_prealloc_lo_wmark *= 95;
295 }
296
297 space = dqp->q_prealloc_hi_wmark;
298
299 do_div(space, 100);
300 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
301 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
302 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700306 * Ensure that the given in-core dquot has a buffer on disk backing it, and
Darrick J. Wong710d7072019-04-24 09:27:41 -0700307 * return the buffer locked and held. This is called when the bmapi finds a
308 * hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
310STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700311xfs_dquot_disk_alloc(
312 struct xfs_trans **tpp,
313 struct xfs_dquot *dqp,
314 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700316 struct xfs_bmbt_irec map;
Brian Foster2ba13722018-07-11 22:26:11 -0700317 struct xfs_trans *tp = *tpp;
318 struct xfs_mount *mp = tp->t_mountp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700319 struct xfs_buf *bp;
320 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700321 int nmaps = 1;
322 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000323
324 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xfs_ilock(quotip, XFS_ILOCK_EXCL);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000327 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700328 /*
329 * Return if this type of quotas is turned off while we didn't
330 * have an inode lock
331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
Dave Chinner24513372014-06-25 14:58:08 +1000333 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700336 /* Create the block mapping. */
Brian Foster2ba13722018-07-11 22:26:11 -0700337 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
338 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
Brian Fosterda781e62019-10-21 09:26:48 -0700339 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
340 &nmaps);
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000341 if (error)
Brian Foster73971b12018-08-07 10:57:13 -0700342 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
344 ASSERT(nmaps == 1);
345 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
346 (map.br_startblock != HOLESTARTBLOCK));
347
348 /*
349 * Keep track of the blkno to save a lookup later
350 */
351 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
352
353 /* now we can just get the buffer (there's nothing to read yet) */
Darrick J. Wongce924642020-01-23 17:01:18 -0800354 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
355 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
356 if (error)
357 return error;
Dave Chinner1813dd62012-11-14 17:54:40 +1100358 bp->b_ops = &xfs_dquot_buf_ops;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
361 * Make a chunk of dquots out of this buffer and log
362 * the entire thing.
363 */
Brian Foster2ba13722018-07-11 22:26:11 -0700364 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000365 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700366 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Tim Shimminefa092f2005-09-05 08:29:01 +1000368 /*
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700369 * Hold the buffer and join it to the dfops so that we'll still own
370 * the buffer when we return to the caller. The buffer disposal on
371 * error must be paid attention to very carefully, as it has been
372 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
373 * code when allocating a new dquot record" in 2005, and the later
374 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
375 * the buffer locked across the _defer_finish call. We can now do
376 * this correctly with xfs_defer_bjoin.
Tim Shimminefa092f2005-09-05 08:29:01 +1000377 *
Brian Foster73971b12018-08-07 10:57:13 -0700378 * Above, we allocated a disk block for the dquot information and used
379 * get_buf to initialize the dquot. If the _defer_finish fails, the old
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700380 * transaction is gone but the new buffer is not joined or held to any
381 * transaction, so we must _buf_relse it.
Tim Shimminefa092f2005-09-05 08:29:01 +1000382 *
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700383 * If everything succeeds, the caller of this function is returned a
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700384 * buffer that is locked and held to the transaction. The caller
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700385 * is responsible for unlocking any buffer passed back, either
Darrick J. Wong710d7072019-04-24 09:27:41 -0700386 * manually or by committing the transaction. On error, the buffer is
387 * released and not passed back.
Tim Shimminefa092f2005-09-05 08:29:01 +1000388 */
Brian Foster2ba13722018-07-11 22:26:11 -0700389 xfs_trans_bhold(tp, bp);
Brian Foster9e28a242018-07-24 13:43:15 -0700390 error = xfs_defer_finish(tpp);
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700391 if (error) {
Darrick J. Wong710d7072019-04-24 09:27:41 -0700392 xfs_trans_bhold_release(*tpp, bp);
393 xfs_trans_brelse(*tpp, bp);
Brian Foster73971b12018-08-07 10:57:13 -0700394 return error;
Darrick J. Wong7b6b50f2018-05-04 15:30:19 -0700395 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700396 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
Dave Chinner9aede1d2013-10-15 09:17:52 +1100399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700401 * Read in the in-core dquot's on-disk metadata and return the buffer.
402 * Returns ENOENT to signal a hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
404STATIC int
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700405xfs_dquot_disk_read(
406 struct xfs_mount *mp,
407 struct xfs_dquot *dqp,
408 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500410 struct xfs_bmbt_irec map;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500411 struct xfs_buf *bp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700412 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700413 uint lock_mode;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700414 int nmaps = 1;
415 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Christoph Hellwig0891f992017-07-13 12:14:34 -0700417 lock_mode = xfs_ilock_data_map_shared(quotip);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700418 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000420 * Return if this type of quotas is turned off while we
421 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Christoph Hellwig0891f992017-07-13 12:14:34 -0700423 xfs_iunlock(quotip, lock_mode);
Dave Chinner24513372014-06-25 14:58:08 +1000424 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000428 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000430 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700431 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwig0891f992017-07-13 12:14:34 -0700432 xfs_iunlock(quotip, lock_mode);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000433 if (error)
434 return error;
435
436 ASSERT(nmaps == 1);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700437 ASSERT(map.br_blockcount >= 1);
438 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
439 if (map.br_startblock == HOLESTARTBLOCK)
440 return -ENOENT;
441
442 trace_xfs_dqtobp_read(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000443
444 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700445 * store the blkno etc so that we don't have to do the
446 * mapping all the time
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000447 */
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700448 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000449
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700450 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
451 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
452 &xfs_dquot_buf_ops);
453 if (error) {
454 ASSERT(bp == NULL);
455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Dave Chinnerc6319192012-11-14 17:50:13 +1100458 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700459 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
460 *bpp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Eric Sandeend99831f2014-06-22 15:03:54 +1000462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700465/* Allocate and initialize everything we need for an incore dquot. */
466STATIC struct xfs_dquot *
467xfs_dquot_alloc(
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000468 struct xfs_mount *mp,
469 xfs_dqid_t id,
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700470 uint type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000472 struct xfs_dquot *dqp;
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000473
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700474 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000475
476 dqp->dq_flags = type;
477 dqp->q_core.d_id = cpu_to_be32(id);
478 dqp->q_mount = mp;
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000479 INIT_LIST_HEAD(&dqp->q_lru);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000480 mutex_init(&dqp->q_qlock);
481 init_waitqueue_head(&dqp->q_pinwait);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700482 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
483 /*
484 * Offset of dquot in the (fixed sized) dquot chunk.
485 */
486 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
487 sizeof(xfs_dqblk_t);
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000488
489 /*
490 * Because we want to use a counting completion, complete
491 * the flush completion once to allow a single access to
492 * the flush completion without blocking.
493 */
494 init_completion(&dqp->q_flush);
495 complete(&dqp->q_flush);
496
497 /*
498 * Make sure group quotas have a different lock class than user
499 * quotas.
500 */
Dave Chinnerf112a042013-09-30 09:37:03 +1000501 switch (type) {
502 case XFS_DQ_USER:
503 /* uses the default lock class */
504 break;
505 case XFS_DQ_GROUP:
506 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
507 break;
508 case XFS_DQ_PROJ:
509 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
510 break;
511 default:
512 ASSERT(0);
513 break;
514 }
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +0000515
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700516 xfs_qm_dquot_logitem_init(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700518 XFS_STATS_INC(mp, xs_qm_dquot);
519 return dqp;
520}
521
522/* Copy the in-core quota fields in from the on-disk buffer. */
523STATIC void
524xfs_dquot_from_disk(
525 struct xfs_dquot *dqp,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700526 struct xfs_buf *bp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700527{
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700528 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
529
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700530 /* copy everything from disk dquot to the incore dquot */
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800531 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot));
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700532
533 /*
534 * Reservation counters are defined as reservation plus current usage
535 * to avoid having to add every time.
536 */
537 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
538 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
539 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
540
541 /* initialize the dquot speculative prealloc thresholds */
542 xfs_dquot_set_prealloc_limits(dqp);
543}
544
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700545/* Allocate and initialize the dquot buffer for this in-core dquot. */
546static int
547xfs_qm_dqread_alloc(
548 struct xfs_mount *mp,
549 struct xfs_dquot *dqp,
550 struct xfs_buf **bpp)
551{
552 struct xfs_trans *tp;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700553 int error;
554
555 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
556 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
557 if (error)
558 goto err;
559
Darrick J. Wong710d7072019-04-24 09:27:41 -0700560 error = xfs_dquot_disk_alloc(&tp, dqp, bpp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700561 if (error)
562 goto err_cancel;
563
564 error = xfs_trans_commit(tp);
565 if (error) {
566 /*
567 * Buffer was held to the transaction, so we have to unlock it
568 * manually here because we're not passing it back.
569 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700570 xfs_buf_relse(*bpp);
571 *bpp = NULL;
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700572 goto err;
573 }
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700574 return 0;
575
576err_cancel:
577 xfs_trans_cancel(tp);
578err:
579 return error;
580}
581
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700582/*
583 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700584 * and release the buffer immediately. If @can_alloc is true, fill any
585 * holes in the on-disk metadata.
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700586 */
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700587static int
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700588xfs_qm_dqread(
589 struct xfs_mount *mp,
590 xfs_dqid_t id,
591 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700592 bool can_alloc,
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700593 struct xfs_dquot **dqpp)
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700594{
595 struct xfs_dquot *dqp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700596 struct xfs_buf *bp;
Darrick J. Wong617cd5c2018-05-04 15:30:23 -0700597 int error;
598
599 dqp = xfs_dquot_alloc(mp, id, type);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000600 trace_xfs_dqread(dqp);
601
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700602 /* Try to read the buffer, allocating if necessary. */
603 error = xfs_dquot_disk_read(mp, dqp, &bp);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700604 if (error == -ENOENT && can_alloc)
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700605 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
606 if (error)
607 goto err;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700610 * At this point we should have a clean locked buffer. Copy the data
611 * to the incore dquot and release the buffer since the incore dquot
612 * has its own locking protocol so we needn't tie up the buffer any
613 * further.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200615 ASSERT(xfs_buf_islocked(bp));
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700616 xfs_dquot_from_disk(dqp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700618 xfs_buf_relse(bp);
619 *dqpp = dqp;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000620 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700622err:
623 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 xfs_qm_dqdestroy(dqp);
Darrick J. Wongd63192c2018-05-04 15:30:23 -0700625 *dqpp = NULL;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000626 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
Eric Sandeen296c24e2016-02-08 11:27:38 +1100630 * Advance to the next id in the current chunk, or if at the
631 * end of the chunk, skip ahead to first id in next allocated chunk
632 * using the SEEK_DATA interface.
633 */
Eryu Guan6e3e6d52016-04-06 09:47:21 +1000634static int
Eric Sandeen296c24e2016-02-08 11:27:38 +1100635xfs_dq_get_next_id(
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700636 struct xfs_mount *mp,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100637 uint type,
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700638 xfs_dqid_t *id)
Eric Sandeen296c24e2016-02-08 11:27:38 +1100639{
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700640 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
641 xfs_dqid_t next_id = *id + 1; /* simple advance */
642 uint lock_flags;
643 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700644 struct xfs_iext_cursor cur;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100645 xfs_fsblock_t start;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100646 int error = 0;
647
Eric Sandeen657bdfb2017-01-17 11:43:38 -0800648 /* If we'd wrap past the max ID, stop */
649 if (next_id < *id)
650 return -ENOENT;
651
Eric Sandeen296c24e2016-02-08 11:27:38 +1100652 /* If new ID is within the current chunk, advancing it sufficed */
653 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
654 *id = next_id;
655 return 0;
656 }
657
658 /* Nope, next_id is now past the current chunk, so find the next one */
659 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
660
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700661 lock_flags = xfs_ilock_data_map_shared(quotip);
662 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
663 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
664 if (error)
665 return error;
666 }
Eric Sandeen296c24e2016-02-08 11:27:38 +1100667
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700668 if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
Brian Foster2192b0b2017-07-05 12:07:52 -0700669 /* contiguous chunk, bump startoff for the id calculation */
670 if (got.br_startoff < start)
671 got.br_startoff = start;
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700672 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
Brian Foster2192b0b2017-07-05 12:07:52 -0700673 } else {
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700674 error = -ENOENT;
Brian Foster2192b0b2017-07-05 12:07:52 -0700675 }
676
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700677 xfs_iunlock(quotip, lock_flags);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100678
Christoph Hellwigbda250d2017-06-29 12:28:36 -0700679 return error;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100680}
681
682/*
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700683 * Look up the dquot in the in-core cache. If found, the dquot is returned
684 * locked and ready to go.
685 */
686static struct xfs_dquot *
687xfs_qm_dqget_cache_lookup(
688 struct xfs_mount *mp,
689 struct xfs_quotainfo *qi,
690 struct radix_tree_root *tree,
691 xfs_dqid_t id)
692{
693 struct xfs_dquot *dqp;
694
695restart:
696 mutex_lock(&qi->qi_tree_lock);
697 dqp = radix_tree_lookup(tree, id);
698 if (!dqp) {
699 mutex_unlock(&qi->qi_tree_lock);
700 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
701 return NULL;
702 }
703
704 xfs_dqlock(dqp);
705 if (dqp->dq_flags & XFS_DQ_FREEING) {
706 xfs_dqunlock(dqp);
707 mutex_unlock(&qi->qi_tree_lock);
708 trace_xfs_dqget_freeing(dqp);
709 delay(1);
710 goto restart;
711 }
712
713 dqp->q_nrefs++;
714 mutex_unlock(&qi->qi_tree_lock);
715
716 trace_xfs_dqget_hit(dqp);
717 XFS_STATS_INC(mp, xs_qm_dqcachehits);
718 return dqp;
719}
720
721/*
722 * Try to insert a new dquot into the in-core cache. If an error occurs the
723 * caller should throw away the dquot and start over. Otherwise, the dquot
724 * is returned locked (and held by the cache) as if there had been a cache
725 * hit.
726 */
727static int
728xfs_qm_dqget_cache_insert(
729 struct xfs_mount *mp,
730 struct xfs_quotainfo *qi,
731 struct radix_tree_root *tree,
732 xfs_dqid_t id,
733 struct xfs_dquot *dqp)
734{
735 int error;
736
737 mutex_lock(&qi->qi_tree_lock);
738 error = radix_tree_insert(tree, id, dqp);
739 if (unlikely(error)) {
740 /* Duplicate found! Caller must try again. */
741 WARN_ON(error != -EEXIST);
742 mutex_unlock(&qi->qi_tree_lock);
743 trace_xfs_dqget_dup(dqp);
744 return error;
745 }
746
747 /* Return a locked dquot to the caller, with a reference taken. */
748 xfs_dqlock(dqp);
749 dqp->q_nrefs = 1;
750
751 qi->qi_dquots++;
752 mutex_unlock(&qi->qi_tree_lock);
753
754 return 0;
755}
756
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700757/* Check our input parameters. */
758static int
759xfs_qm_dqget_checks(
760 struct xfs_mount *mp,
761 uint type)
762{
763 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
764 return -ESRCH;
765
766 switch (type) {
767 case XFS_DQ_USER:
768 if (!XFS_IS_UQUOTA_ON(mp))
769 return -ESRCH;
770 return 0;
771 case XFS_DQ_GROUP:
772 if (!XFS_IS_GQUOTA_ON(mp))
773 return -ESRCH;
774 return 0;
775 case XFS_DQ_PROJ:
776 if (!XFS_IS_PQUOTA_ON(mp))
777 return -ESRCH;
778 return 0;
779 default:
780 WARN_ON_ONCE(0);
781 return -EINVAL;
782 }
783}
784
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700785/*
Darrick J. Wong4882c192018-05-04 15:30:22 -0700786 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
787 * dquot, doing an allocation (if requested) as needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
789int
790xfs_qm_dqget(
Darrick J. Wong4882c192018-05-04 15:30:22 -0700791 struct xfs_mount *mp,
792 xfs_dqid_t id,
793 uint type,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700794 bool can_alloc,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700795 struct xfs_dquot **O_dqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000797 struct xfs_quotainfo *qi = mp->m_quotainfo;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700798 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000799 struct xfs_dquot *dqp;
800 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Darrick J. Wongd7103ee2018-05-04 15:30:21 -0700802 error = xfs_qm_dqget_checks(mp, type);
803 if (error)
804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Darrick J. Wong4882c192018-05-04 15:30:22 -0700806restart:
807 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
808 if (dqp) {
809 *O_dqpp = dqp;
810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Christoph Hellwig92678552011-12-06 21:58:18 +0000812
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700813 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700814 if (error)
815 return error;
816
817 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
818 if (error) {
819 /*
820 * Duplicate found. Just throw away the new dquot and start
821 * over.
822 */
823 xfs_qm_dqdestroy(dqp);
824 XFS_STATS_INC(mp, xs_qm_dquot_dups);
825 goto restart;
826 }
827
828 trace_xfs_dqget_miss(dqp);
829 *O_dqpp = dqp;
830 return 0;
831}
832
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700833/*
834 * Given a dquot id and type, read and initialize a dquot from the on-disk
835 * metadata. This function is only for use during quota initialization so
836 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
837 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
838 */
839int
840xfs_qm_dqget_uncached(
841 struct xfs_mount *mp,
842 xfs_dqid_t id,
843 uint type,
844 struct xfs_dquot **dqpp)
845{
846 int error;
847
848 error = xfs_qm_dqget_checks(mp, type);
849 if (error)
850 return error;
851
852 return xfs_qm_dqread(mp, id, type, 0, dqpp);
853}
854
Darrick J. Wong4882c192018-05-04 15:30:22 -0700855/* Return the quota id for a given inode and type. */
856xfs_dqid_t
857xfs_qm_id_for_quotatype(
858 struct xfs_inode *ip,
859 uint type)
860{
861 switch (type) {
862 case XFS_DQ_USER:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800863 return i_uid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700864 case XFS_DQ_GROUP:
Christoph Hellwigba8adad2020-02-21 08:31:27 -0800865 return i_gid_read(VFS_I(ip));
Darrick J. Wong4882c192018-05-04 15:30:22 -0700866 case XFS_DQ_PROJ:
Christoph Hellwigde7a8662019-11-12 08:22:54 -0800867 return ip->i_d.di_projid;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700868 }
869 ASSERT(0);
870 return 0;
871}
872
873/*
874 * Return the dquot for a given inode and type. If @can_alloc is true, then
875 * allocate blocks if needed. The inode's ILOCK must be held and it must not
876 * have already had an inode attached.
877 */
878int
879xfs_qm_dqget_inode(
880 struct xfs_inode *ip,
881 uint type,
882 bool can_alloc,
883 struct xfs_dquot **O_dqpp)
884{
885 struct xfs_mount *mp = ip->i_mount;
886 struct xfs_quotainfo *qi = mp->m_quotainfo;
887 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
888 struct xfs_dquot *dqp;
889 xfs_dqid_t id;
Darrick J. Wong4882c192018-05-04 15:30:22 -0700890 int error;
891
892 error = xfs_qm_dqget_checks(mp, type);
893 if (error)
894 return error;
895
Darrick J. Wong4882c192018-05-04 15:30:22 -0700896 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
897 ASSERT(xfs_inode_dquot(ip, type) == NULL);
898
899 id = xfs_qm_id_for_quotatype(ip, type);
900
Christoph Hellwig92678552011-12-06 21:58:18 +0000901restart:
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700902 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000903 if (dqp) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000904 *O_dqpp = dqp;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /*
909 * Dquot cache miss. We don't want to keep the inode lock across
910 * a (potential) disk read. Also we don't want to deal with the lock
911 * ordering between quotainode and this inode. OTOH, dropping the inode
912 * lock here means dealing with a chown that can happen before
913 * we re-acquire the lock.
914 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700915 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700916 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700917 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000918 if (error)
919 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Darrick J. Wong4882c192018-05-04 15:30:22 -0700921 /*
922 * A dquot could be attached to this inode by now, since we had
923 * dropped the ilock.
924 */
925 if (xfs_this_quota_on(mp, type)) {
926 struct xfs_dquot *dqp1;
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000927
Darrick J. Wong4882c192018-05-04 15:30:22 -0700928 dqp1 = xfs_inode_dquot(ip, type);
929 if (dqp1) {
Chandra Seetharaman36731412012-01-23 17:31:30 +0000930 xfs_qm_dqdestroy(dqp);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700931 dqp = dqp1;
932 xfs_dqlock(dqp);
933 goto dqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Darrick J. Wong4882c192018-05-04 15:30:22 -0700935 } else {
936 /* inode stays locked on return */
937 xfs_qm_dqdestroy(dqp);
938 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
Darrick J. Wongcc2047c2018-05-04 15:30:20 -0700941 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
942 if (error) {
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000943 /*
944 * Duplicate found. Just throw away the new dquot and start
945 * over.
946 */
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000947 xfs_qm_dqdestroy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100948 XFS_STATS_INC(mp, xs_qm_dquot_dups);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000949 goto restart;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Darrick J. Wong4882c192018-05-04 15:30:22 -0700952dqret:
953 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000954 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *O_dqpp = dqp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000959/*
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700960 * Starting at @id and progressing upwards, look for an initialized incore
961 * dquot, lock it, and return it.
962 */
963int
964xfs_qm_dqget_next(
965 struct xfs_mount *mp,
966 xfs_dqid_t id,
967 uint type,
968 struct xfs_dquot **dqpp)
969{
970 struct xfs_dquot *dqp;
971 int error = 0;
972
973 *dqpp = NULL;
974 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700975 error = xfs_qm_dqget(mp, id, type, false, &dqp);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700976 if (error == -ENOENT)
977 continue;
978 else if (error != 0)
979 break;
980
981 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
982 *dqpp = dqp;
983 return 0;
984 }
985
986 xfs_qm_dqput(dqp);
987 }
988
989 return error;
990}
991
992/*
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000993 * Release a reference to the dquot (decrement ref-count) and unlock it.
994 *
995 * If there is a group quota attached to this dquot, carefully release that
996 * too without tripping over deadlocks'n'stuff.
997 */
998void
999xfs_qm_dqput(
1000 struct xfs_dquot *dqp)
1001{
1002 ASSERT(dqp->q_nrefs > 0);
1003 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1004
1005 trace_xfs_dqput(dqp);
1006
Dave Chinner3c35337572014-05-05 17:30:15 +10001007 if (--dqp->q_nrefs == 0) {
1008 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1009 trace_xfs_dqput_free(dqp);
1010
1011 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001012 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner3c35337572014-05-05 17:30:15 +10001013 }
1014 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*
1018 * Release a dquot. Flush it if dirty, then dqput() it.
1019 * dquot must not be locked.
1020 */
1021void
1022xfs_qm_dqrele(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001023 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001025 if (!dqp)
1026 return;
1027
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001028 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 xfs_dqlock(dqp);
1031 /*
1032 * We don't care to flush it if the dquot is dirty here.
1033 * That will create stutters that we want to avoid.
1034 * Instead we do a delayed write when we try to reclaim
1035 * a dirty dquot. Also xfs_sync will take part of the burden...
1036 */
1037 xfs_qm_dqput(dqp);
1038}
1039
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001040/*
1041 * This is the dquot flushing I/O completion routine. It is called
1042 * from interrupt level when the buffer containing the dquot is
1043 * flushed to disk. It is responsible for removing the dquot logitem
1044 * from the AIL if it has not been re-logged, and unlocking the dquot's
1045 * flush lock. This behavior is very similar to that of inodes..
1046 */
1047STATIC void
1048xfs_qm_dqflush_done(
1049 struct xfs_buf *bp,
1050 struct xfs_log_item *lip)
1051{
Pavel Reichlfd8b81d2019-11-12 17:04:26 -08001052 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001053 struct xfs_dquot *dqp = qip->qli_dquot;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001054 struct xfs_ail *ailp = lip->li_ailp;
Brian Foster849274c2020-05-06 13:25:23 -07001055 xfs_lsn_t tail_lsn;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001056
1057 /*
1058 * We only want to pull the item from the AIL if its
1059 * location in the log has not changed since we started the flush.
1060 * Thus, we only bother if the dquot's lsn has
1061 * not changed. First we check the lsn outside the lock
1062 * since it's cheaper, and then we recheck while
1063 * holding the lock before removing the dquot from the AIL.
1064 */
Dave Chinner22525c12018-05-09 07:47:34 -07001065 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
Carlos Maiolino373b0582017-11-28 08:54:10 -08001066 ((lip->li_lsn == qip->qli_flush_lsn) ||
Dave Chinner22525c12018-05-09 07:47:34 -07001067 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001068
Matthew Wilcox57e80952018-03-07 14:59:39 -08001069 spin_lock(&ailp->ail_lock);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001070 if (lip->li_lsn == qip->qli_flush_lsn) {
Brian Foster849274c2020-05-06 13:25:23 -07001071 /* xfs_ail_update_finish() drops the AIL lock */
1072 tail_lsn = xfs_ail_delete_one(ailp, lip);
1073 xfs_ail_update_finish(ailp, tail_lsn);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001074 } else {
1075 /*
1076 * Clear the failed state since we are about to drop the
1077 * flush lock
1078 */
Dave Chinner22525c12018-05-09 07:47:34 -07001079 xfs_clear_li_failed(lip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08001080 spin_unlock(&ailp->ail_lock);
Carlos Maiolino373b0582017-11-28 08:54:10 -08001081 }
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001082 }
1083
1084 /*
1085 * Release the dq's flush lock since we're done with it.
1086 */
1087 xfs_dqfunlock(dqp);
1088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090/*
1091 * Write a modified dquot to disk.
1092 * The dquot must be locked and the flush lock too taken by caller.
1093 * The flush lock will not be unlocked until the dquot reaches the disk,
1094 * but the dquot is free to be unlocked and modified by the caller
1095 * in the interim. Dquot is still locked on return. This behavior is
1096 * identical to that of inodes.
1097 */
1098int
1099xfs_qm_dqflush(
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001100 struct xfs_dquot *dqp,
1101 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001103 struct xfs_mount *mp = dqp->q_mount;
Brian Fosterb707fff2020-05-06 13:25:22 -07001104 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001105 struct xfs_buf *bp;
Eric Sandeen7224fa42018-05-07 09:20:18 -07001106 struct xfs_dqblk *dqb;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001107 struct xfs_disk_dquot *ddqp;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001108 xfs_failaddr_t fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001112 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001113
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001114 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001116 *bpp = NULL;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 xfs_qm_dqunpin_wait(dqp);
1119
1120 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001123 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
Brian Foster8d3d7e22020-03-27 08:29:45 -07001124 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1125 &bp, &xfs_dquot_buf_ops);
Brian Fosterb707fff2020-05-06 13:25:22 -07001126 if (error == -EAGAIN)
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001127 goto out_unlock;
Brian Fosterb707fff2020-05-06 13:25:22 -07001128 if (error)
1129 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001131 /*
1132 * Calculate the location of the dquot inside the buffer.
1133 */
Eric Sandeen7224fa42018-05-07 09:20:18 -07001134 dqb = bp->b_addr + dqp->q_bufoffset;
1135 ddqp = &dqb->dd_diskdq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001136
Brian Foster629dcb32020-05-06 13:25:22 -07001137 /* sanity check the in-core structure before we flush */
1138 fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id),
1139 0);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08001140 if (fa) {
1141 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
Brian Foster629dcb32020-05-06 13:25:22 -07001142 be32_to_cpu(dqp->q_core.d_id), fa);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001143 xfs_buf_relse(bp);
Brian Fosterb707fff2020-05-06 13:25:22 -07001144 error = -EFSCORRUPTED;
1145 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 /* This is the only portion of data that needs to persist */
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001149 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /*
1152 * Clear the dirty field and remember the flush lsn for later use.
1153 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001154 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
David Chinner7b2e2a32008-10-30 17:39:12 +11001156 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1157 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /*
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001160 * copy the lsn into the on-disk dquot now while we have the in memory
1161 * dquot here. This can't be done later in the write verifier as we
1162 * can't get access to the log item at that point in time.
Dave Chinner6fcdc592013-06-03 15:28:46 +10001163 *
1164 * We also calculate the CRC here so that the on-disk dquot in the
1165 * buffer always has a valid CRC. This ensures there is no possibility
1166 * of a dquot without an up-to-date CRC getting to disk.
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001167 */
1168 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001169 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
Dave Chinner6fcdc592013-06-03 15:28:46 +10001170 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1171 XFS_DQUOT_CRC_OFF);
Christoph Hellwig3fe58f32013-04-03 16:11:16 +11001172 }
1173
1174 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 * Attach an iodone routine so that we can remove this dquot from the
1176 * AIL and release the flush lock once the dquot is synced to disk.
1177 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001178 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1179 &dqp->q_logitem.qli_item);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * If the buffer is pinned then push on the log so we won't
1183 * get stuck waiting in the write for too long.
1184 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001185 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001186 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001187 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001190 trace_xfs_dqflush_done(dqp);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001191 *bpp = bp;
1192 return 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001193
Brian Fosterb707fff2020-05-06 13:25:22 -07001194out_abort:
1195 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Brian Foster2b3cf092020-05-06 13:27:04 -07001196 xfs_trans_ail_delete(lip, 0);
Brian Fosterb707fff2020-05-06 13:25:22 -07001197 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001198out_unlock:
1199 xfs_dqfunlock(dqp);
Brian Foster8d3d7e22020-03-27 08:29:45 -07001200 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001203/*
1204 * Lock two xfs_dquot structures.
1205 *
1206 * To avoid deadlocks we always lock the quota structure with
1207 * the lowerd id first.
1208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209void
1210xfs_dqlock2(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001211 struct xfs_dquot *d1,
1212 struct xfs_dquot *d2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 if (d1 && d2) {
1215 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001216 if (be32_to_cpu(d1->q_core.d_id) >
1217 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001218 mutex_lock(&d2->q_qlock);
1219 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001221 mutex_lock(&d1->q_qlock);
1222 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001224 } else if (d1) {
1225 mutex_lock(&d1->q_qlock);
1226 } else if (d2) {
1227 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229}
1230
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001231int __init
1232xfs_qm_init(void)
1233{
Carlos Maiolinob1231762019-11-14 12:43:03 -08001234 xfs_qm_dqzone = kmem_cache_create("xfs_dquot",
1235 sizeof(struct xfs_dquot),
1236 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001237 if (!xfs_qm_dqzone)
1238 goto out;
1239
Carlos Maiolinob1231762019-11-14 12:43:03 -08001240 xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx",
1241 sizeof(struct xfs_dquot_acct),
1242 0, 0, NULL);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001243 if (!xfs_qm_dqtrxzone)
1244 goto out_free_dqzone;
1245
1246 return 0;
1247
1248out_free_dqzone:
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001249 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001250out:
1251 return -ENOMEM;
1252}
1253
Gerard Snitselaar1c2ccc62012-03-16 18:36:18 +00001254void
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001255xfs_qm_exit(void)
1256{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08001257 kmem_cache_destroy(xfs_qm_dqtrxzone);
1258 kmem_cache_destroy(xfs_qm_dqzone);
Christoph Hellwiga05931c2012-03-13 08:52:37 +00001259}
Darrick J. Wong554ba962018-05-04 15:31:21 -07001260
1261/*
1262 * Iterate every dquot of a particular type. The caller must ensure that the
1263 * particular quota type is active. iter_fn can return negative error codes,
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07001264 * or -ECANCELED to indicate that it wants to stop iterating.
Darrick J. Wong554ba962018-05-04 15:31:21 -07001265 */
1266int
1267xfs_qm_dqiterate(
1268 struct xfs_mount *mp,
1269 uint dqtype,
1270 xfs_qm_dqiterate_fn iter_fn,
1271 void *priv)
1272{
1273 struct xfs_dquot *dq;
1274 xfs_dqid_t id = 0;
1275 int error;
1276
1277 do {
1278 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1279 if (error == -ENOENT)
1280 return 0;
1281 if (error)
1282 return error;
1283
1284 error = iter_fn(dq, dqtype, priv);
1285 id = be32_to_cpu(dq->q_core.d_id);
1286 xfs_qm_dqput(dq);
1287 id++;
1288 } while (error == 0 && id != 0);
1289
1290 return error;
1291}