]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/xfs_dquot.c
xfs: stop using q_core warning counters in the quota code
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / xfs_dquot.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
4ce3121f
NS
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4
LT
6#include "xfs.h"
7#include "xfs_fs.h"
6ca1c906 8#include "xfs_format.h"
239880ef 9#include "xfs_log_format.h"
70a9883c 10#include "xfs_shared.h"
239880ef 11#include "xfs_trans_resv.h"
a844f451 12#include "xfs_bit.h"
1da177e4 13#include "xfs_mount.h"
3ab78df2 14#include "xfs_defer.h"
1da177e4
LT
15#include "xfs_inode.h"
16#include "xfs_bmap.h"
239880ef 17#include "xfs_quota.h"
239880ef 18#include "xfs_trans.h"
1da177e4
LT
19#include "xfs_buf_item.h"
20#include "xfs_trans_space.h"
21#include "xfs_trans_priv.h"
1da177e4 22#include "xfs_qm.h"
0b1b213f 23#include "xfs_trace.h"
239880ef 24#include "xfs_log.h"
a4fbe6ab 25#include "xfs_bmap_btree.h"
afeda600 26#include "xfs_error.h"
1da177e4 27
1da177e4 28/*
bf72de31
CH
29 * Lock order:
30 *
31 * ip->i_lock
9f920f11 32 * qi->qi_tree_lock
b84a3a96
CH
33 * dquot->q_qlock (xfs_dqlock() and friends)
34 * dquot->q_flush (xfs_dqflock() and friends)
35 * qi->qi_lru_lock
bf72de31
CH
36 *
37 * If two dquots need to be locked the order is user before group/project,
38 * otherwise by the lowest id first, see xfs_dqlock2.
39 */
1da177e4 40
a05931ce
CH
41struct kmem_zone *xfs_qm_dqtrxzone;
42static struct kmem_zone *xfs_qm_dqzone;
43
f112a049
DC
44static struct lock_class_key xfs_dquot_group_class;
45static struct lock_class_key xfs_dquot_project_class;
98b8c7a0 46
1da177e4
LT
47/*
48 * This is called to free all the memory associated with a dquot
49 */
50void
51xfs_qm_dqdestroy(
aefe69a4 52 struct xfs_dquot *dqp)
1da177e4 53{
f8739c3c 54 ASSERT(list_empty(&dqp->q_lru));
1da177e4 55
b1c5ebb2 56 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
1da177e4 57 mutex_destroy(&dqp->q_qlock);
0b1b213f 58
ff6d6af2 59 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
377bcd5f 60 kmem_cache_free(xfs_qm_dqzone, dqp);
1da177e4
LT
61}
62
1da177e4
LT
63/*
64 * If default limits are in force, push them into the dquot now.
65 * We overwrite the dquot limits only if they are zero and this
66 * is not the root dquot.
67 */
68void
69xfs_qm_adjust_dqlimits(
4b6eae2e
BF
70 struct xfs_mount *mp,
71 struct xfs_dquot *dq)
1da177e4 72{
4b6eae2e 73 struct xfs_quotainfo *q = mp->m_quotainfo;
be607946 74 struct xfs_def_quota *defq;
b1366451 75 int prealloc = 0;
1da177e4 76
c51df733 77 ASSERT(dq->q_id);
ce6e7e79 78 defq = xfs_get_defquota(q, xfs_dquot_type(dq));
1da177e4 79
d3537cf9
DW
80 if (defq->bsoftlimit && !dq->q_blk.softlimit) {
81 dq->q_blk.softlimit = defq->bsoftlimit;
b1366451
BF
82 prealloc = 1;
83 }
d3537cf9
DW
84 if (defq->bhardlimit && !dq->q_blk.hardlimit) {
85 dq->q_blk.hardlimit = defq->bhardlimit;
b1366451
BF
86 prealloc = 1;
87 }
d3537cf9
DW
88 if (defq->isoftlimit && !dq->q_ino.softlimit)
89 dq->q_ino.softlimit = defq->isoftlimit;
90 if (defq->ihardlimit && !dq->q_ino.hardlimit)
91 dq->q_ino.hardlimit = defq->ihardlimit;
92 if (defq->rtbsoftlimit && !dq->q_rtb.softlimit)
93 dq->q_rtb.softlimit = defq->rtbsoftlimit;
94 if (defq->rtbhardlimit && !dq->q_rtb.hardlimit)
95 dq->q_rtb.hardlimit = defq->rtbhardlimit;
b1366451
BF
96
97 if (prealloc)
98 xfs_dquot_set_prealloc_limits(dq);
1da177e4
LT
99}
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
754002b4
NS
110 * '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).
1da177e4
LT
113 */
114void
115xfs_qm_adjust_dqtimers(
aefe69a4 116 struct xfs_mount *mp,
3dbb9aa3 117 struct xfs_dquot *dq)
1da177e4 118{
e850301f 119 struct xfs_quotainfo *qi = mp->m_quotainfo;
3dbb9aa3 120 struct xfs_disk_dquot *d = &dq->q_core;
e850301f
ES
121 struct xfs_def_quota *defq;
122
c51df733 123 ASSERT(dq->q_id);
e850301f 124 defq = xfs_get_defquota(qi, xfs_dquot_type(dq));
1da177e4 125
ea15ab3c 126#ifdef DEBUG
d3537cf9
DW
127 if (dq->q_blk.hardlimit)
128 ASSERT(dq->q_blk.softlimit <= dq->q_blk.hardlimit);
129 if (dq->q_ino.hardlimit)
130 ASSERT(dq->q_ino.softlimit <= dq->q_ino.hardlimit);
131 if (dq->q_rtb.hardlimit)
132 ASSERT(dq->q_rtb.softlimit <= dq->q_rtb.hardlimit);
1da177e4 133#endif
ea15ab3c 134
1da177e4 135 if (!d->d_btimer) {
d3537cf9 136 if ((dq->q_blk.softlimit &&
be37d40c 137 (dq->q_blk.count > dq->q_blk.softlimit)) ||
d3537cf9 138 (dq->q_blk.hardlimit &&
be37d40c 139 (dq->q_blk.count > dq->q_blk.hardlimit))) {
b8a0880a 140 d->d_btimer = cpu_to_be32(ktime_get_real_seconds() +
e850301f 141 defq->btimelimit);
754002b4 142 } else {
c8c45fb2 143 dq->q_blk.warnings = 0;
1da177e4
LT
144 }
145 } else {
d3537cf9 146 if ((!dq->q_blk.softlimit ||
be37d40c 147 (dq->q_blk.count <= dq->q_blk.softlimit)) &&
d3537cf9 148 (!dq->q_blk.hardlimit ||
be37d40c 149 (dq->q_blk.count <= dq->q_blk.hardlimit))) {
1da177e4
LT
150 d->d_btimer = 0;
151 }
152 }
153
154 if (!d->d_itimer) {
d3537cf9 155 if ((dq->q_ino.softlimit &&
be37d40c 156 (dq->q_ino.count > dq->q_ino.softlimit)) ||
d3537cf9 157 (dq->q_ino.hardlimit &&
be37d40c 158 (dq->q_ino.count > dq->q_ino.hardlimit))) {
b8a0880a 159 d->d_itimer = cpu_to_be32(ktime_get_real_seconds() +
e850301f 160 defq->itimelimit);
754002b4 161 } else {
c8c45fb2 162 dq->q_ino.warnings = 0;
1da177e4
LT
163 }
164 } else {
d3537cf9 165 if ((!dq->q_ino.softlimit ||
be37d40c 166 (dq->q_ino.count <= dq->q_ino.softlimit)) &&
d3537cf9 167 (!dq->q_ino.hardlimit ||
be37d40c 168 (dq->q_ino.count <= dq->q_ino.hardlimit))) {
1da177e4
LT
169 d->d_itimer = 0;
170 }
171 }
172
173 if (!d->d_rtbtimer) {
d3537cf9 174 if ((dq->q_rtb.softlimit &&
be37d40c 175 (dq->q_rtb.count > dq->q_rtb.softlimit)) ||
d3537cf9 176 (dq->q_rtb.hardlimit &&
be37d40c 177 (dq->q_rtb.count > dq->q_rtb.hardlimit))) {
b8a0880a 178 d->d_rtbtimer = cpu_to_be32(ktime_get_real_seconds() +
e850301f 179 defq->rtbtimelimit);
754002b4 180 } else {
c8c45fb2 181 dq->q_rtb.warnings = 0;
1da177e4
LT
182 }
183 } else {
d3537cf9 184 if ((!dq->q_rtb.softlimit ||
be37d40c 185 (dq->q_rtb.count <= dq->q_rtb.softlimit)) &&
d3537cf9 186 (!dq->q_rtb.hardlimit ||
be37d40c 187 (dq->q_rtb.count <= dq->q_rtb.hardlimit))) {
1da177e4
LT
188 d->d_rtbtimer = 0;
189 }
190 }
191}
192
1da177e4
LT
193/*
194 * initialize a buffer full of dquots and log the whole thing
195 */
196STATIC void
197xfs_qm_init_dquot_blk(
78bba5c8
DW
198 struct xfs_trans *tp,
199 struct xfs_mount *mp,
200 xfs_dqid_t id,
201 uint type,
202 struct xfs_buf *bp)
1da177e4 203{
8a7b8a89 204 struct xfs_quotainfo *q = mp->m_quotainfo;
78bba5c8
DW
205 struct xfs_dqblk *d;
206 xfs_dqid_t curid;
207 unsigned int qflag;
208 unsigned int blftype;
209 int i;
1da177e4
LT
210
211 ASSERT(tp);
0c842ad4 212 ASSERT(xfs_buf_islocked(bp));
1da177e4 213
62926044 214 d = bp->b_addr;
1da177e4
LT
215
216 /*
217 * ID of the first dquot in the block - id's are zero based.
218 */
8a7b8a89 219 curid = id - (id % q->qi_dqperchunk);
8a7b8a89 220 memset(d, 0, BBTOB(q->qi_dqchunklen));
49d35a5c
CH
221 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
222 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
223 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
224 d->dd_diskdq.d_id = cpu_to_be32(curid);
225 d->dd_diskdq.d_flags = type;
6fcdc59d 226 if (xfs_sb_version_hascrc(&mp->m_sb)) {
92863451 227 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
6fcdc59d
DC
228 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
229 XFS_DQUOT_CRC_OFF);
230 }
49d35a5c
CH
231 }
232
78bba5c8
DW
233 if (type & XFS_DQ_USER) {
234 qflag = XFS_UQUOTA_CHKD;
235 blftype = XFS_BLF_UDQUOT_BUF;
236 } else if (type & XFS_DQ_PROJ) {
237 qflag = XFS_PQUOTA_CHKD;
238 blftype = XFS_BLF_PDQUOT_BUF;
239 } else {
240 qflag = XFS_GQUOTA_CHKD;
241 blftype = XFS_BLF_GDQUOT_BUF;
242 }
243
244 xfs_trans_dquot_buf(tp, bp, blftype);
245
246 /*
247 * quotacheck uses delayed writes to update all the dquots on disk in an
248 * efficient manner instead of logging the individual dquot changes as
249 * they are made. However if we log the buffer allocated here and crash
250 * after quotacheck while the logged initialisation is still in the
251 * active region of the log, log recovery can replay the dquot buffer
252 * initialisation over the top of the checked dquots and corrupt quota
253 * accounting.
254 *
255 * To avoid this problem, quotacheck cannot log the initialised buffer.
256 * We must still dirty the buffer and write it back before the
257 * allocation transaction clears the log. Therefore, mark the buffer as
258 * ordered instead of logging it directly. This is safe for quotacheck
259 * because it detects and repairs allocated but initialized dquot blocks
260 * in the quota inodes.
261 */
262 if (!(mp->m_qflags & qflag))
263 xfs_trans_ordered_buf(tp, bp);
264 else
265 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
1da177e4
LT
266}
267
b1366451
BF
268/*
269 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
270 * watermarks correspond to the soft and hard limits by default. If a soft limit
271 * is not specified, we use 95% of the hard limit.
272 */
273void
274xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
275{
c8ce540d 276 uint64_t space;
b1366451 277
d3537cf9
DW
278 dqp->q_prealloc_hi_wmark = dqp->q_blk.hardlimit;
279 dqp->q_prealloc_lo_wmark = dqp->q_blk.softlimit;
b1366451
BF
280 if (!dqp->q_prealloc_lo_wmark) {
281 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
282 do_div(dqp->q_prealloc_lo_wmark, 100);
283 dqp->q_prealloc_lo_wmark *= 95;
284 }
285
286 space = dqp->q_prealloc_hi_wmark;
287
288 do_div(space, 100);
289 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
290 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
291 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
292}
293
1da177e4 294/*
d63192c8 295 * Ensure that the given in-core dquot has a buffer on disk backing it, and
710d707d
DW
296 * return the buffer locked and held. This is called when the bmapi finds a
297 * hole.
1da177e4
LT
298 */
299STATIC int
d63192c8
DW
300xfs_dquot_disk_alloc(
301 struct xfs_trans **tpp,
302 struct xfs_dquot *dqp,
303 struct xfs_buf **bpp)
1da177e4 304{
d63192c8 305 struct xfs_bmbt_irec map;
2ba13721
BF
306 struct xfs_trans *tp = *tpp;
307 struct xfs_mount *mp = tp->t_mountp;
d63192c8
DW
308 struct xfs_buf *bp;
309 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
d63192c8
DW
310 int nmaps = 1;
311 int error;
0b1b213f
CH
312
313 trace_xfs_dqalloc(dqp);
1da177e4 314
1da177e4 315 xfs_ilock(quotip, XFS_ILOCK_EXCL);
6967b964 316 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
d63192c8
DW
317 /*
318 * Return if this type of quotas is turned off while we didn't
319 * have an inode lock
320 */
1da177e4 321 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
2451337d 322 return -ESRCH;
1da177e4
LT
323 }
324
d63192c8 325 /* Create the block mapping. */
2ba13721
BF
326 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
327 error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
da781e64
BF
328 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0, &map,
329 &nmaps);
c0dc7828 330 if (error)
73971b17 331 return error;
1da177e4
LT
332 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
333 ASSERT(nmaps == 1);
334 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
335 (map.br_startblock != HOLESTARTBLOCK));
336
337 /*
338 * Keep track of the blkno to save a lookup later
339 */
340 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
341
342 /* now we can just get the buffer (there's nothing to read yet) */
ce92464c
DW
343 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
344 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
345 if (error)
346 return error;
1813dd64 347 bp->b_ops = &xfs_dquot_buf_ops;
2a30f36d 348
1da177e4
LT
349 /*
350 * Make a chunk of dquots out of this buffer and log
351 * the entire thing.
352 */
c51df733 353 xfs_qm_init_dquot_blk(tp, mp, dqp->q_id,
c8ad20ff 354 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
d63192c8 355 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
1da177e4 356
efa092f3 357 /*
7b6b50f5
DW
358 * Hold the buffer and join it to the dfops so that we'll still own
359 * the buffer when we return to the caller. The buffer disposal on
360 * error must be paid attention to very carefully, as it has been
361 * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
362 * code when allocating a new dquot record" in 2005, and the later
363 * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
364 * the buffer locked across the _defer_finish call. We can now do
365 * this correctly with xfs_defer_bjoin.
efa092f3 366 *
73971b17
BF
367 * Above, we allocated a disk block for the dquot information and used
368 * get_buf to initialize the dquot. If the _defer_finish fails, the old
7b6b50f5
DW
369 * transaction is gone but the new buffer is not joined or held to any
370 * transaction, so we must _buf_relse it.
efa092f3 371 *
7b6b50f5 372 * If everything succeeds, the caller of this function is returned a
d63192c8 373 * buffer that is locked and held to the transaction. The caller
7b6b50f5 374 * is responsible for unlocking any buffer passed back, either
710d707d
DW
375 * manually or by committing the transaction. On error, the buffer is
376 * released and not passed back.
efa092f3 377 */
2ba13721 378 xfs_trans_bhold(tp, bp);
9e28a242 379 error = xfs_defer_finish(tpp);
7b6b50f5 380 if (error) {
710d707d
DW
381 xfs_trans_bhold_release(*tpp, bp);
382 xfs_trans_brelse(*tpp, bp);
73971b17 383 return error;
efa092f3 384 }
d63192c8 385 *bpp = bp;
1da177e4 386 return 0;
1da177e4 387}
9aede1d8 388
1da177e4 389/*
d63192c8
DW
390 * Read in the in-core dquot's on-disk metadata and return the buffer.
391 * Returns ENOENT to signal a hole.
1da177e4
LT
392 */
393STATIC int
d63192c8
DW
394xfs_dquot_disk_read(
395 struct xfs_mount *mp,
396 struct xfs_dquot *dqp,
397 struct xfs_buf **bpp)
1da177e4 398{
113a5683 399 struct xfs_bmbt_irec map;
113a5683 400 struct xfs_buf *bp;
d63192c8 401 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
0891f997 402 uint lock_mode;
d63192c8
DW
403 int nmaps = 1;
404 int error;
1da177e4 405
0891f997 406 lock_mode = xfs_ilock_data_map_shared(quotip);
d63192c8 407 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
1da177e4 408 /*
acecf1b5
CH
409 * Return if this type of quotas is turned off while we
410 * didn't have the quota inode lock.
1da177e4 411 */
0891f997 412 xfs_iunlock(quotip, lock_mode);
2451337d 413 return -ESRCH;
acecf1b5
CH
414 }
415
416 /*
417 * Find the block map; no allocations yet
418 */
5c8ed202 419 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
d63192c8 420 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
0891f997 421 xfs_iunlock(quotip, lock_mode);
acecf1b5
CH
422 if (error)
423 return error;
424
425 ASSERT(nmaps == 1);
d63192c8
DW
426 ASSERT(map.br_blockcount >= 1);
427 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
428 if (map.br_startblock == HOLESTARTBLOCK)
429 return -ENOENT;
430
431 trace_xfs_dqtobp_read(dqp);
acecf1b5
CH
432
433 /*
d63192c8
DW
434 * store the blkno etc so that we don't have to do the
435 * mapping all the time
acecf1b5 436 */
d63192c8 437 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
1da177e4 438
d63192c8
DW
439 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
440 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
441 &xfs_dquot_buf_ops);
442 if (error) {
443 ASSERT(bp == NULL);
444 return error;
1da177e4
LT
445 }
446
c6319198 447 ASSERT(xfs_buf_islocked(bp));
d63192c8
DW
448 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
449 *bpp = bp;
1da177e4 450
d99831ff 451 return 0;
1da177e4
LT
452}
453
617cd5c1
DW
454/* Allocate and initialize everything we need for an incore dquot. */
455STATIC struct xfs_dquot *
456xfs_dquot_alloc(
97e7ade5
CH
457 struct xfs_mount *mp,
458 xfs_dqid_t id,
617cd5c1 459 uint type)
1da177e4 460{
97e7ade5 461 struct xfs_dquot *dqp;
92b2e5b3 462
707e0dda 463 dqp = kmem_zone_zalloc(xfs_qm_dqzone, 0);
92b2e5b3
CH
464
465 dqp->dq_flags = type;
c51df733 466 dqp->q_id = id;
92b2e5b3 467 dqp->q_mount = mp;
f8739c3c 468 INIT_LIST_HEAD(&dqp->q_lru);
92b2e5b3
CH
469 mutex_init(&dqp->q_qlock);
470 init_waitqueue_head(&dqp->q_pinwait);
d63192c8
DW
471 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
472 /*
473 * Offset of dquot in the (fixed sized) dquot chunk.
474 */
475 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
476 sizeof(xfs_dqblk_t);
92b2e5b3
CH
477
478 /*
479 * Because we want to use a counting completion, complete
480 * the flush completion once to allow a single access to
481 * the flush completion without blocking.
482 */
483 init_completion(&dqp->q_flush);
484 complete(&dqp->q_flush);
485
486 /*
487 * Make sure group quotas have a different lock class than user
488 * quotas.
489 */
f112a049
DC
490 switch (type) {
491 case XFS_DQ_USER:
492 /* uses the default lock class */
493 break;
494 case XFS_DQ_GROUP:
495 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
496 break;
497 case XFS_DQ_PROJ:
498 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
499 break;
500 default:
501 ASSERT(0);
502 break;
503 }
92b2e5b3 504
617cd5c1
DW
505 xfs_qm_dquot_logitem_init(dqp);
506
ff6d6af2 507 XFS_STATS_INC(mp, xs_qm_dquot);
617cd5c1
DW
508 return dqp;
509}
510
511/* Copy the in-core quota fields in from the on-disk buffer. */
afeda600 512STATIC int
617cd5c1
DW
513xfs_dquot_from_disk(
514 struct xfs_dquot *dqp,
d63192c8 515 struct xfs_buf *bp)
617cd5c1 516{
d63192c8
DW
517 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
518
afeda600
DW
519 /*
520 * Ensure that we got the type and ID we were looking for.
521 * Everything else was checked by the dquot buffer verifier.
522 */
523 if ((ddqp->d_flags & XFS_DQ_ALLTYPES) != dqp->dq_flags ||
c51df733 524 be32_to_cpu(ddqp->d_id) != dqp->q_id) {
afeda600
DW
525 xfs_alert_tag(bp->b_mount, XFS_PTAG_VERIFIER_ERROR,
526 "Metadata corruption detected at %pS, quota %u",
c51df733 527 __this_address, dqp->q_id);
afeda600
DW
528 xfs_alert(bp->b_mount, "Unmount and run xfs_repair");
529 return -EFSCORRUPTED;
530 }
531
617cd5c1 532 /* copy everything from disk dquot to the incore dquot */
aefe69a4 533 memcpy(&dqp->q_core, ddqp, sizeof(struct xfs_disk_dquot));
d3537cf9
DW
534 dqp->q_blk.hardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
535 dqp->q_blk.softlimit = be64_to_cpu(ddqp->d_blk_softlimit);
536 dqp->q_ino.hardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
537 dqp->q_ino.softlimit = be64_to_cpu(ddqp->d_ino_softlimit);
538 dqp->q_rtb.hardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
539 dqp->q_rtb.softlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
617cd5c1 540
be37d40c
DW
541 dqp->q_blk.count = be64_to_cpu(ddqp->d_bcount);
542 dqp->q_ino.count = be64_to_cpu(ddqp->d_icount);
543 dqp->q_rtb.count = be64_to_cpu(ddqp->d_rtbcount);
544
c8c45fb2
DW
545 dqp->q_blk.warnings = be16_to_cpu(ddqp->d_bwarns);
546 dqp->q_ino.warnings = be16_to_cpu(ddqp->d_iwarns);
547 dqp->q_rtb.warnings = be16_to_cpu(ddqp->d_rtbwarns);
548
617cd5c1
DW
549 /*
550 * Reservation counters are defined as reservation plus current usage
551 * to avoid having to add every time.
552 */
be37d40c
DW
553 dqp->q_blk.reserved = dqp->q_blk.count;
554 dqp->q_ino.reserved = dqp->q_ino.count;
555 dqp->q_rtb.reserved = dqp->q_rtb.count;
617cd5c1
DW
556
557 /* initialize the dquot speculative prealloc thresholds */
558 xfs_dquot_set_prealloc_limits(dqp);
afeda600 559 return 0;
617cd5c1 560}
1da177e4 561
0b0fa1d1
DW
562/* Copy the in-core quota fields into the on-disk buffer. */
563void
564xfs_dquot_to_disk(
565 struct xfs_disk_dquot *ddqp,
566 struct xfs_dquot *dqp)
567{
568 memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
d3537cf9
DW
569 ddqp->d_blk_hardlimit = cpu_to_be64(dqp->q_blk.hardlimit);
570 ddqp->d_blk_softlimit = cpu_to_be64(dqp->q_blk.softlimit);
571 ddqp->d_ino_hardlimit = cpu_to_be64(dqp->q_ino.hardlimit);
572 ddqp->d_ino_softlimit = cpu_to_be64(dqp->q_ino.softlimit);
573 ddqp->d_rtb_hardlimit = cpu_to_be64(dqp->q_rtb.hardlimit);
574 ddqp->d_rtb_softlimit = cpu_to_be64(dqp->q_rtb.softlimit);
be37d40c
DW
575
576 ddqp->d_bcount = cpu_to_be64(dqp->q_blk.count);
577 ddqp->d_icount = cpu_to_be64(dqp->q_ino.count);
578 ddqp->d_rtbcount = cpu_to_be64(dqp->q_rtb.count);
c8c45fb2
DW
579
580 ddqp->d_bwarns = cpu_to_be16(dqp->q_blk.warnings);
581 ddqp->d_iwarns = cpu_to_be16(dqp->q_ino.warnings);
582 ddqp->d_rtbwarns = cpu_to_be16(dqp->q_rtb.warnings);
0b0fa1d1
DW
583}
584
d63192c8
DW
585/* Allocate and initialize the dquot buffer for this in-core dquot. */
586static int
587xfs_qm_dqread_alloc(
588 struct xfs_mount *mp,
589 struct xfs_dquot *dqp,
590 struct xfs_buf **bpp)
591{
592 struct xfs_trans *tp;
d63192c8
DW
593 int error;
594
595 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
596 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
597 if (error)
598 goto err;
599
710d707d 600 error = xfs_dquot_disk_alloc(&tp, dqp, bpp);
d63192c8
DW
601 if (error)
602 goto err_cancel;
603
604 error = xfs_trans_commit(tp);
605 if (error) {
606 /*
607 * Buffer was held to the transaction, so we have to unlock it
608 * manually here because we're not passing it back.
609 */
710d707d
DW
610 xfs_buf_relse(*bpp);
611 *bpp = NULL;
d63192c8
DW
612 goto err;
613 }
d63192c8
DW
614 return 0;
615
616err_cancel:
617 xfs_trans_cancel(tp);
618err:
619 return error;
620}
621
617cd5c1
DW
622/*
623 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
30ab2dcf
DW
624 * and release the buffer immediately. If @can_alloc is true, fill any
625 * holes in the on-disk metadata.
617cd5c1 626 */
114e73cc 627static int
617cd5c1
DW
628xfs_qm_dqread(
629 struct xfs_mount *mp,
630 xfs_dqid_t id,
631 uint type,
30ab2dcf 632 bool can_alloc,
d63192c8 633 struct xfs_dquot **dqpp)
617cd5c1
DW
634{
635 struct xfs_dquot *dqp;
617cd5c1 636 struct xfs_buf *bp;
617cd5c1
DW
637 int error;
638
639 dqp = xfs_dquot_alloc(mp, id, type);
0b1b213f
CH
640 trace_xfs_dqread(dqp);
641
d63192c8
DW
642 /* Try to read the buffer, allocating if necessary. */
643 error = xfs_dquot_disk_read(mp, dqp, &bp);
30ab2dcf 644 if (error == -ENOENT && can_alloc)
d63192c8
DW
645 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
646 if (error)
647 goto err;
1da177e4
LT
648
649 /*
d63192c8
DW
650 * At this point we should have a clean locked buffer. Copy the data
651 * to the incore dquot and release the buffer since the incore dquot
652 * has its own locking protocol so we needn't tie up the buffer any
653 * further.
1da177e4 654 */
0c842ad4 655 ASSERT(xfs_buf_islocked(bp));
afeda600 656 error = xfs_dquot_from_disk(dqp, bp);
d63192c8 657 xfs_buf_relse(bp);
afeda600
DW
658 if (error)
659 goto err;
660
d63192c8 661 *dqpp = dqp;
97e7ade5 662 return error;
1da177e4 663
d63192c8
DW
664err:
665 trace_xfs_dqread_fail(dqp);
1da177e4 666 xfs_qm_dqdestroy(dqp);
d63192c8 667 *dqpp = NULL;
97e7ade5 668 return error;
1da177e4
LT
669}
670
296c24e2
ES
671/*
672 * Advance to the next id in the current chunk, or if at the
673 * end of the chunk, skip ahead to first id in next allocated chunk
674 * using the SEEK_DATA interface.
675 */
6e3e6d55 676static int
296c24e2 677xfs_dq_get_next_id(
bda250db 678 struct xfs_mount *mp,
296c24e2 679 uint type,
bda250db 680 xfs_dqid_t *id)
296c24e2 681{
bda250db
CH
682 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
683 xfs_dqid_t next_id = *id + 1; /* simple advance */
684 uint lock_flags;
685 struct xfs_bmbt_irec got;
b2b1712a 686 struct xfs_iext_cursor cur;
296c24e2 687 xfs_fsblock_t start;
296c24e2
ES
688 int error = 0;
689
657bdfb7
ES
690 /* If we'd wrap past the max ID, stop */
691 if (next_id < *id)
692 return -ENOENT;
693
296c24e2
ES
694 /* If new ID is within the current chunk, advancing it sufficed */
695 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
696 *id = next_id;
697 return 0;
698 }
699
700 /* Nope, next_id is now past the current chunk, so find the next one */
701 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
702
bda250db
CH
703 lock_flags = xfs_ilock_data_map_shared(quotip);
704 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
705 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
706 if (error)
707 return error;
708 }
296c24e2 709
b2b1712a 710 if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
2192b0ba
BF
711 /* contiguous chunk, bump startoff for the id calculation */
712 if (got.br_startoff < start)
713 got.br_startoff = start;
bda250db 714 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
2192b0ba 715 } else {
bda250db 716 error = -ENOENT;
2192b0ba
BF
717 }
718
bda250db 719 xfs_iunlock(quotip, lock_flags);
296c24e2 720
bda250db 721 return error;
296c24e2
ES
722}
723
cc2047c4
DW
724/*
725 * Look up the dquot in the in-core cache. If found, the dquot is returned
726 * locked and ready to go.
727 */
728static struct xfs_dquot *
729xfs_qm_dqget_cache_lookup(
730 struct xfs_mount *mp,
731 struct xfs_quotainfo *qi,
732 struct radix_tree_root *tree,
733 xfs_dqid_t id)
734{
735 struct xfs_dquot *dqp;
736
737restart:
738 mutex_lock(&qi->qi_tree_lock);
739 dqp = radix_tree_lookup(tree, id);
740 if (!dqp) {
741 mutex_unlock(&qi->qi_tree_lock);
742 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
743 return NULL;
744 }
745
746 xfs_dqlock(dqp);
985a78fd 747 if (dqp->q_flags & XFS_DQFLAG_FREEING) {
cc2047c4
DW
748 xfs_dqunlock(dqp);
749 mutex_unlock(&qi->qi_tree_lock);
750 trace_xfs_dqget_freeing(dqp);
751 delay(1);
752 goto restart;
753 }
754
755 dqp->q_nrefs++;
756 mutex_unlock(&qi->qi_tree_lock);
757
758 trace_xfs_dqget_hit(dqp);
759 XFS_STATS_INC(mp, xs_qm_dqcachehits);
760 return dqp;
761}
762
763/*
764 * Try to insert a new dquot into the in-core cache. If an error occurs the
765 * caller should throw away the dquot and start over. Otherwise, the dquot
766 * is returned locked (and held by the cache) as if there had been a cache
767 * hit.
768 */
769static int
770xfs_qm_dqget_cache_insert(
771 struct xfs_mount *mp,
772 struct xfs_quotainfo *qi,
773 struct radix_tree_root *tree,
774 xfs_dqid_t id,
775 struct xfs_dquot *dqp)
776{
777 int error;
778
779 mutex_lock(&qi->qi_tree_lock);
780 error = radix_tree_insert(tree, id, dqp);
781 if (unlikely(error)) {
782 /* Duplicate found! Caller must try again. */
783 WARN_ON(error != -EEXIST);
784 mutex_unlock(&qi->qi_tree_lock);
785 trace_xfs_dqget_dup(dqp);
786 return error;
787 }
788
789 /* Return a locked dquot to the caller, with a reference taken. */
790 xfs_dqlock(dqp);
791 dqp->q_nrefs = 1;
792
793 qi->qi_dquots++;
794 mutex_unlock(&qi->qi_tree_lock);
795
796 return 0;
797}
798
d7103eeb
DW
799/* Check our input parameters. */
800static int
801xfs_qm_dqget_checks(
802 struct xfs_mount *mp,
803 uint type)
804{
805 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
806 return -ESRCH;
807
808 switch (type) {
809 case XFS_DQ_USER:
810 if (!XFS_IS_UQUOTA_ON(mp))
811 return -ESRCH;
812 return 0;
813 case XFS_DQ_GROUP:
814 if (!XFS_IS_GQUOTA_ON(mp))
815 return -ESRCH;
816 return 0;
817 case XFS_DQ_PROJ:
818 if (!XFS_IS_PQUOTA_ON(mp))
819 return -ESRCH;
820 return 0;
821 default:
822 WARN_ON_ONCE(0);
823 return -EINVAL;
824 }
825}
826
1da177e4 827/*
4882c19d
DW
828 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
829 * dquot, doing an allocation (if requested) as needed.
1da177e4
LT
830 */
831int
832xfs_qm_dqget(
4882c19d
DW
833 struct xfs_mount *mp,
834 xfs_dqid_t id,
835 uint type,
30ab2dcf 836 bool can_alloc,
4882c19d 837 struct xfs_dquot **O_dqpp)
1da177e4 838{
9f920f11 839 struct xfs_quotainfo *qi = mp->m_quotainfo;
4882c19d 840 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
9f920f11
CH
841 struct xfs_dquot *dqp;
842 int error;
1da177e4 843
d7103eeb
DW
844 error = xfs_qm_dqget_checks(mp, type);
845 if (error)
846 return error;
1da177e4 847
4882c19d
DW
848restart:
849 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
850 if (dqp) {
851 *O_dqpp = dqp;
852 return 0;
853 }
854
30ab2dcf 855 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
4882c19d
DW
856 if (error)
857 return error;
858
859 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
860 if (error) {
861 /*
862 * Duplicate found. Just throw away the new dquot and start
863 * over.
864 */
865 xfs_qm_dqdestroy(dqp);
866 XFS_STATS_INC(mp, xs_qm_dquot_dups);
867 goto restart;
868 }
869
870 trace_xfs_dqget_miss(dqp);
871 *O_dqpp = dqp;
872 return 0;
873}
874
114e73cc
DW
875/*
876 * Given a dquot id and type, read and initialize a dquot from the on-disk
877 * metadata. This function is only for use during quota initialization so
878 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
879 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
880 */
881int
882xfs_qm_dqget_uncached(
883 struct xfs_mount *mp,
884 xfs_dqid_t id,
885 uint type,
886 struct xfs_dquot **dqpp)
887{
888 int error;
889
890 error = xfs_qm_dqget_checks(mp, type);
891 if (error)
892 return error;
893
894 return xfs_qm_dqread(mp, id, type, 0, dqpp);
895}
896
4882c19d
DW
897/* Return the quota id for a given inode and type. */
898xfs_dqid_t
899xfs_qm_id_for_quotatype(
900 struct xfs_inode *ip,
901 uint type)
902{
903 switch (type) {
904 case XFS_DQ_USER:
ba8adad5 905 return i_uid_read(VFS_I(ip));
4882c19d 906 case XFS_DQ_GROUP:
ba8adad5 907 return i_gid_read(VFS_I(ip));
4882c19d 908 case XFS_DQ_PROJ:
de7a866f 909 return ip->i_d.di_projid;
1da177e4 910 }
4882c19d
DW
911 ASSERT(0);
912 return 0;
913}
914
915/*
916 * Return the dquot for a given inode and type. If @can_alloc is true, then
917 * allocate blocks if needed. The inode's ILOCK must be held and it must not
918 * have already had an inode attached.
919 */
920int
921xfs_qm_dqget_inode(
922 struct xfs_inode *ip,
923 uint type,
924 bool can_alloc,
925 struct xfs_dquot **O_dqpp)
926{
927 struct xfs_mount *mp = ip->i_mount;
928 struct xfs_quotainfo *qi = mp->m_quotainfo;
929 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
930 struct xfs_dquot *dqp;
931 xfs_dqid_t id;
4882c19d
DW
932 int error;
933
934 error = xfs_qm_dqget_checks(mp, type);
935 if (error)
936 return error;
937
4882c19d
DW
938 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
939 ASSERT(xfs_inode_dquot(ip, type) == NULL);
940
941 id = xfs_qm_id_for_quotatype(ip, type);
92678554
CH
942
943restart:
cc2047c4 944 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
9f920f11 945 if (dqp) {
9f920f11
CH
946 *O_dqpp = dqp;
947 return 0;
1da177e4 948 }
1da177e4
LT
949
950 /*
951 * Dquot cache miss. We don't want to keep the inode lock across
952 * a (potential) disk read. Also we don't want to deal with the lock
953 * ordering between quotainode and this inode. OTOH, dropping the inode
954 * lock here means dealing with a chown that can happen before
955 * we re-acquire the lock.
956 */
4882c19d 957 xfs_iunlock(ip, XFS_ILOCK_EXCL);
30ab2dcf 958 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
4882c19d 959 xfs_ilock(ip, XFS_ILOCK_EXCL);
7ae44407
CH
960 if (error)
961 return error;
1da177e4 962
4882c19d
DW
963 /*
964 * A dquot could be attached to this inode by now, since we had
965 * dropped the ilock.
966 */
967 if (xfs_this_quota_on(mp, type)) {
968 struct xfs_dquot *dqp1;
969
970 dqp1 = xfs_inode_dquot(ip, type);
971 if (dqp1) {
36731410 972 xfs_qm_dqdestroy(dqp);
4882c19d
DW
973 dqp = dqp1;
974 xfs_dqlock(dqp);
975 goto dqret;
1da177e4 976 }
4882c19d
DW
977 } else {
978 /* inode stays locked on return */
979 xfs_qm_dqdestroy(dqp);
980 return -ESRCH;
1da177e4
LT
981 }
982
cc2047c4
DW
983 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
984 if (error) {
1da177e4 985 /*
9f920f11
CH
986 * Duplicate found. Just throw away the new dquot and start
987 * over.
1da177e4 988 */
9f920f11 989 xfs_qm_dqdestroy(dqp);
ff6d6af2 990 XFS_STATS_INC(mp, xs_qm_dquot_dups);
9f920f11 991 goto restart;
1da177e4
LT
992 }
993
4882c19d
DW
994dqret:
995 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
0b1b213f 996 trace_xfs_dqget_miss(dqp);
1da177e4 997 *O_dqpp = dqp;
d99831ff 998 return 0;
1da177e4
LT
999}
1000
2e330e76
DW
1001/*
1002 * Starting at @id and progressing upwards, look for an initialized incore
1003 * dquot, lock it, and return it.
1004 */
1005int
1006xfs_qm_dqget_next(
1007 struct xfs_mount *mp,
1008 xfs_dqid_t id,
1009 uint type,
1010 struct xfs_dquot **dqpp)
1011{
1012 struct xfs_dquot *dqp;
1013 int error = 0;
1014
1015 *dqpp = NULL;
1016 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
30ab2dcf 1017 error = xfs_qm_dqget(mp, id, type, false, &dqp);
2e330e76
DW
1018 if (error == -ENOENT)
1019 continue;
1020 else if (error != 0)
1021 break;
1022
1023 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
1024 *dqpp = dqp;
1025 return 0;
1026 }
1027
1028 xfs_qm_dqput(dqp);
1029 }
1030
1031 return error;
1032}
1033
f8739c3c
CH
1034/*
1035 * Release a reference to the dquot (decrement ref-count) and unlock it.
1036 *
1037 * If there is a group quota attached to this dquot, carefully release that
1038 * too without tripping over deadlocks'n'stuff.
1039 */
1040void
1041xfs_qm_dqput(
1042 struct xfs_dquot *dqp)
1043{
1044 ASSERT(dqp->q_nrefs > 0);
1045 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1046
1047 trace_xfs_dqput(dqp);
1048
3c353375
DC
1049 if (--dqp->q_nrefs == 0) {
1050 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1051 trace_xfs_dqput_free(dqp);
1052
1053 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
ff6d6af2 1054 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
3c353375
DC
1055 }
1056 xfs_dqunlock(dqp);
1da177e4
LT
1057}
1058
1059/*
1060 * Release a dquot. Flush it if dirty, then dqput() it.
1061 * dquot must not be locked.
1062 */
1063void
1064xfs_qm_dqrele(
aefe69a4 1065 struct xfs_dquot *dqp)
1da177e4 1066{
7d095257
CH
1067 if (!dqp)
1068 return;
1069
0b1b213f 1070 trace_xfs_dqrele(dqp);
1da177e4
LT
1071
1072 xfs_dqlock(dqp);
1073 /*
1074 * We don't care to flush it if the dquot is dirty here.
1075 * That will create stutters that we want to avoid.
1076 * Instead we do a delayed write when we try to reclaim
1077 * a dirty dquot. Also xfs_sync will take part of the burden...
1078 */
1079 xfs_qm_dqput(dqp);
1080}
1081
ca30b2a7
CH
1082/*
1083 * This is the dquot flushing I/O completion routine. It is called
1084 * from interrupt level when the buffer containing the dquot is
1085 * flushed to disk. It is responsible for removing the dquot logitem
1086 * from the AIL if it has not been re-logged, and unlocking the dquot's
1087 * flush lock. This behavior is very similar to that of inodes..
1088 */
6f5de180 1089static void
ca30b2a7 1090xfs_qm_dqflush_done(
ca30b2a7
CH
1091 struct xfs_log_item *lip)
1092{
fd8b81db 1093 struct xfs_dq_logitem *qip = (struct xfs_dq_logitem *)lip;
aefe69a4 1094 struct xfs_dquot *dqp = qip->qli_dquot;
ca30b2a7 1095 struct xfs_ail *ailp = lip->li_ailp;
849274c1 1096 xfs_lsn_t tail_lsn;
ca30b2a7
CH
1097
1098 /*
1099 * We only want to pull the item from the AIL if its
1100 * location in the log has not changed since we started the flush.
1101 * Thus, we only bother if the dquot's lsn has
1102 * not changed. First we check the lsn outside the lock
1103 * since it's cheaper, and then we recheck while
1104 * holding the lock before removing the dquot from the AIL.
1105 */
22525c17 1106 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
373b0589 1107 ((lip->li_lsn == qip->qli_flush_lsn) ||
22525c17 1108 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
ca30b2a7 1109
57e80956 1110 spin_lock(&ailp->ail_lock);
e98084b8 1111 xfs_clear_li_failed(lip);
373b0589 1112 if (lip->li_lsn == qip->qli_flush_lsn) {
849274c1
BF
1113 /* xfs_ail_update_finish() drops the AIL lock */
1114 tail_lsn = xfs_ail_delete_one(ailp, lip);
1115 xfs_ail_update_finish(ailp, tail_lsn);
373b0589 1116 } else {
57e80956 1117 spin_unlock(&ailp->ail_lock);
373b0589 1118 }
ca30b2a7
CH
1119 }
1120
1121 /*
1122 * Release the dq's flush lock since we're done with it.
1123 */
1124 xfs_dqfunlock(dqp);
1125}
1da177e4 1126
6f5de180
DC
1127void
1128xfs_dquot_done(
1129 struct xfs_buf *bp)
1130{
1131 struct xfs_log_item *lip, *n;
1132
1133 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1134 list_del_init(&lip->li_bio_list);
1135 xfs_qm_dqflush_done(lip);
1136 }
1137}
1138
0b0fa1d1
DW
1139/* Check incore dquot for errors before we flush. */
1140static xfs_failaddr_t
1141xfs_qm_dqflush_check(
1142 struct xfs_dquot *dqp)
1143{
d3537cf9 1144 struct xfs_disk_dquot *ddq = &dqp->q_core;
0b0fa1d1
DW
1145 __u8 type = dqp->dq_flags & XFS_DQ_ALLTYPES;
1146
1147 if (type != XFS_DQ_USER &&
1148 type != XFS_DQ_GROUP &&
1149 type != XFS_DQ_PROJ)
1150 return __this_address;
1151
d3537cf9
DW
1152 if (dqp->q_id == 0)
1153 return NULL;
1154
be37d40c 1155 if (dqp->q_blk.softlimit && dqp->q_blk.count > dqp->q_blk.softlimit &&
d3537cf9
DW
1156 !ddq->d_btimer)
1157 return __this_address;
1158
be37d40c 1159 if (dqp->q_ino.softlimit && dqp->q_ino.count > dqp->q_ino.softlimit &&
d3537cf9
DW
1160 !ddq->d_itimer)
1161 return __this_address;
1162
be37d40c 1163 if (dqp->q_rtb.softlimit && dqp->q_rtb.count > dqp->q_rtb.softlimit &&
d3537cf9
DW
1164 !ddq->d_rtbtimer)
1165 return __this_address;
1166
0b0fa1d1
DW
1167 return NULL;
1168}
1169
1da177e4
LT
1170/*
1171 * Write a modified dquot to disk.
1172 * The dquot must be locked and the flush lock too taken by caller.
1173 * The flush lock will not be unlocked until the dquot reaches the disk,
1174 * but the dquot is free to be unlocked and modified by the caller
1175 * in the interim. Dquot is still locked on return. This behavior is
1176 * identical to that of inodes.
1177 */
1178int
1179xfs_qm_dqflush(
fe7257fd
CH
1180 struct xfs_dquot *dqp,
1181 struct xfs_buf **bpp)
1da177e4 1182{
acecf1b5 1183 struct xfs_mount *mp = dqp->q_mount;
b707fffd 1184 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
acecf1b5 1185 struct xfs_buf *bp;
7224fa48 1186 struct xfs_dqblk *dqb;
acecf1b5 1187 struct xfs_disk_dquot *ddqp;
eebf3cab 1188 xfs_failaddr_t fa;
1da177e4 1189 int error;
1da177e4
LT
1190
1191 ASSERT(XFS_DQ_IS_LOCKED(dqp));
e1f49cf2 1192 ASSERT(!completion_done(&dqp->q_flush));
acecf1b5 1193
0b1b213f 1194 trace_xfs_dqflush(dqp);
1da177e4 1195
fe7257fd
CH
1196 *bpp = NULL;
1197
1da177e4
LT
1198 xfs_qm_dqunpin_wait(dqp);
1199
1da177e4
LT
1200 /*
1201 * Get the buffer containing the on-disk dquot
1da177e4 1202 */
acecf1b5 1203 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
8d3d7e2b
BF
1204 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK,
1205 &bp, &xfs_dquot_buf_ops);
b707fffd 1206 if (error == -EAGAIN)
fe7257fd 1207 goto out_unlock;
b707fffd
BF
1208 if (error)
1209 goto out_abort;
1da177e4 1210
acecf1b5
CH
1211 /*
1212 * Calculate the location of the dquot inside the buffer.
1213 */
7224fa48
ES
1214 dqb = bp->b_addr + dqp->q_bufoffset;
1215 ddqp = &dqb->dd_diskdq;
acecf1b5 1216
629dcb38 1217 /* sanity check the in-core structure before we flush */
c51df733 1218 fa = xfs_dquot_verify(mp, &dqp->q_core, dqp->q_id, 0);
eebf3cab
DW
1219 if (fa) {
1220 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
c51df733 1221 dqp->q_id, fa);
acecf1b5 1222 xfs_buf_relse(bp);
b707fffd
BF
1223 error = -EFSCORRUPTED;
1224 goto out_abort;
1da177e4
LT
1225 }
1226
0b0fa1d1
DW
1227 fa = xfs_qm_dqflush_check(dqp);
1228 if (fa) {
1229 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
c51df733 1230 dqp->q_id, fa);
0b0fa1d1
DW
1231 xfs_buf_relse(bp);
1232 error = -EFSCORRUPTED;
1233 goto out_abort;
1234 }
1235
1236 xfs_dquot_to_disk(ddqp, dqp);
1da177e4
LT
1237
1238 /*
1239 * Clear the dirty field and remember the flush lsn for later use.
1240 */
985a78fd 1241 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
1da177e4 1242
7b2e2a31
DC
1243 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1244 &dqp->q_logitem.qli_item.li_lsn);
1da177e4 1245
3fe58f30
CH
1246 /*
1247 * copy the lsn into the on-disk dquot now while we have the in memory
1248 * dquot here. This can't be done later in the write verifier as we
1249 * can't get access to the log item at that point in time.
6fcdc59d
DC
1250 *
1251 * We also calculate the CRC here so that the on-disk dquot in the
1252 * buffer always has a valid CRC. This ensures there is no possibility
1253 * of a dquot without an up-to-date CRC getting to disk.
3fe58f30
CH
1254 */
1255 if (xfs_sb_version_hascrc(&mp->m_sb)) {
3fe58f30 1256 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
6fcdc59d
DC
1257 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1258 XFS_DQUOT_CRC_OFF);
3fe58f30
CH
1259 }
1260
1da177e4 1261 /*
2ef3f7f5
DC
1262 * Attach the dquot to the buffer so that we can remove this dquot from
1263 * the AIL and release the flush lock once the dquot is synced to disk.
1da177e4 1264 */
0c7e5afb 1265 bp->b_flags |= _XBF_DQUOTS;
2ef3f7f5 1266 list_add_tail(&dqp->q_logitem.qli_item.li_bio_list, &bp->b_li_list);
ca30b2a7 1267
1da177e4
LT
1268 /*
1269 * If the buffer is pinned then push on the log so we won't
1270 * get stuck waiting in the write for too long.
1271 */
811e64c7 1272 if (xfs_buf_ispinned(bp)) {
0b1b213f 1273 trace_xfs_dqflush_force(dqp);
a14a348b 1274 xfs_log_force(mp, 0);
1da177e4
LT
1275 }
1276
0b1b213f 1277 trace_xfs_dqflush_done(dqp);
fe7257fd
CH
1278 *bpp = bp;
1279 return 0;
0b1b213f 1280
b707fffd 1281out_abort:
985a78fd 1282 dqp->q_flags &= ~XFS_DQFLAG_DIRTY;
2b3cf093 1283 xfs_trans_ail_delete(lip, 0);
b707fffd 1284 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
fe7257fd
CH
1285out_unlock:
1286 xfs_dqfunlock(dqp);
8d3d7e2b 1287 return error;
1da177e4
LT
1288}
1289
5bb87a33
CH
1290/*
1291 * Lock two xfs_dquot structures.
1292 *
1293 * To avoid deadlocks we always lock the quota structure with
1294 * the lowerd id first.
1295 */
1da177e4
LT
1296void
1297xfs_dqlock2(
aefe69a4
PR
1298 struct xfs_dquot *d1,
1299 struct xfs_dquot *d2)
1da177e4
LT
1300{
1301 if (d1 && d2) {
1302 ASSERT(d1 != d2);
c51df733 1303 if (d1->q_id > d2->q_id) {
5bb87a33
CH
1304 mutex_lock(&d2->q_qlock);
1305 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1306 } else {
5bb87a33
CH
1307 mutex_lock(&d1->q_qlock);
1308 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1309 }
5bb87a33
CH
1310 } else if (d1) {
1311 mutex_lock(&d1->q_qlock);
1312 } else if (d2) {
1313 mutex_lock(&d2->q_qlock);
1da177e4
LT
1314 }
1315}
1316
a05931ce
CH
1317int __init
1318xfs_qm_init(void)
1319{
b1231760
CM
1320 xfs_qm_dqzone = kmem_cache_create("xfs_dquot",
1321 sizeof(struct xfs_dquot),
1322 0, 0, NULL);
a05931ce
CH
1323 if (!xfs_qm_dqzone)
1324 goto out;
1325
b1231760
CM
1326 xfs_qm_dqtrxzone = kmem_cache_create("xfs_dqtrx",
1327 sizeof(struct xfs_dquot_acct),
1328 0, 0, NULL);
a05931ce
CH
1329 if (!xfs_qm_dqtrxzone)
1330 goto out_free_dqzone;
1331
1332 return 0;
1333
1334out_free_dqzone:
aaf54eb8 1335 kmem_cache_destroy(xfs_qm_dqzone);
a05931ce
CH
1336out:
1337 return -ENOMEM;
1338}
1339
1c2ccc66 1340void
a05931ce
CH
1341xfs_qm_exit(void)
1342{
aaf54eb8
CM
1343 kmem_cache_destroy(xfs_qm_dqtrxzone);
1344 kmem_cache_destroy(xfs_qm_dqzone);
a05931ce 1345}
554ba965
DW
1346
1347/*
1348 * Iterate every dquot of a particular type. The caller must ensure that the
1349 * particular quota type is active. iter_fn can return negative error codes,
e7ee96df 1350 * or -ECANCELED to indicate that it wants to stop iterating.
554ba965
DW
1351 */
1352int
1353xfs_qm_dqiterate(
1354 struct xfs_mount *mp,
1355 uint dqtype,
1356 xfs_qm_dqiterate_fn iter_fn,
1357 void *priv)
1358{
1359 struct xfs_dquot *dq;
1360 xfs_dqid_t id = 0;
1361 int error;
1362
1363 do {
1364 error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
1365 if (error == -ENOENT)
1366 return 0;
1367 if (error)
1368 return error;
1369
1370 error = iter_fn(dq, dqtype, priv);
c51df733 1371 id = dq->q_id;
554ba965 1372 xfs_qm_dqput(dq);
554ba965
DW
1373 } while (error == 0 && id != 0);
1374
1375 return error;
1376}