]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/xfs_dquot.c
xfs: rename on-disk dquot counter zap functions
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / xfs_dquot.c
CommitLineData
1da177e4 1/*
4ce3121f
NS
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
4ce3121f
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
4ce3121f
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
4ce3121f
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4
LT
18#include "xfs.h"
19#include "xfs_fs.h"
6ca1c906 20#include "xfs_format.h"
239880ef 21#include "xfs_log_format.h"
70a9883c 22#include "xfs_shared.h"
239880ef 23#include "xfs_trans_resv.h"
a844f451 24#include "xfs_bit.h"
1da177e4 25#include "xfs_mount.h"
3ab78df2 26#include "xfs_defer.h"
1da177e4
LT
27#include "xfs_inode.h"
28#include "xfs_bmap.h"
68988114 29#include "xfs_bmap_util.h"
239880ef
DC
30#include "xfs_alloc.h"
31#include "xfs_quota.h"
1da177e4 32#include "xfs_error.h"
239880ef 33#include "xfs_trans.h"
1da177e4
LT
34#include "xfs_buf_item.h"
35#include "xfs_trans_space.h"
36#include "xfs_trans_priv.h"
1da177e4 37#include "xfs_qm.h"
3fe58f30 38#include "xfs_cksum.h"
0b1b213f 39#include "xfs_trace.h"
239880ef 40#include "xfs_log.h"
a4fbe6ab 41#include "xfs_bmap_btree.h"
1da177e4 42
1da177e4 43/*
bf72de31
CH
44 * Lock order:
45 *
46 * ip->i_lock
9f920f11 47 * qi->qi_tree_lock
b84a3a96
CH
48 * dquot->q_qlock (xfs_dqlock() and friends)
49 * dquot->q_flush (xfs_dqflock() and friends)
50 * qi->qi_lru_lock
bf72de31
CH
51 *
52 * If two dquots need to be locked the order is user before group/project,
53 * otherwise by the lowest id first, see xfs_dqlock2.
54 */
1da177e4 55
a05931ce
CH
56struct kmem_zone *xfs_qm_dqtrxzone;
57static struct kmem_zone *xfs_qm_dqzone;
58
f112a049
DC
59static struct lock_class_key xfs_dquot_group_class;
60static struct lock_class_key xfs_dquot_project_class;
98b8c7a0 61
1da177e4
LT
62/*
63 * This is called to free all the memory associated with a dquot
64 */
65void
66xfs_qm_dqdestroy(
67 xfs_dquot_t *dqp)
68{
f8739c3c 69 ASSERT(list_empty(&dqp->q_lru));
1da177e4 70
b1c5ebb2 71 kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
1da177e4 72 mutex_destroy(&dqp->q_qlock);
0b1b213f 73
ff6d6af2
BD
74 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
75 kmem_zone_free(xfs_qm_dqzone, dqp);
1da177e4
LT
76}
77
1da177e4
LT
78/*
79 * If default limits are in force, push them into the dquot now.
80 * We overwrite the dquot limits only if they are zero and this
81 * is not the root dquot.
82 */
83void
84xfs_qm_adjust_dqlimits(
4b6eae2e
BF
85 struct xfs_mount *mp,
86 struct xfs_dquot *dq)
1da177e4 87{
4b6eae2e
BF
88 struct xfs_quotainfo *q = mp->m_quotainfo;
89 struct xfs_disk_dquot *d = &dq->q_core;
be607946 90 struct xfs_def_quota *defq;
b1366451 91 int prealloc = 0;
1da177e4
LT
92
93 ASSERT(d->d_id);
be607946 94 defq = xfs_get_defquota(dq, q);
1da177e4 95
be607946
CM
96 if (defq->bsoftlimit && !d->d_blk_softlimit) {
97 d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
b1366451
BF
98 prealloc = 1;
99 }
be607946
CM
100 if (defq->bhardlimit && !d->d_blk_hardlimit) {
101 d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
b1366451
BF
102 prealloc = 1;
103 }
be607946
CM
104 if (defq->isoftlimit && !d->d_ino_softlimit)
105 d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
106 if (defq->ihardlimit && !d->d_ino_hardlimit)
107 d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
108 if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
109 d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
110 if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
111 d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
b1366451
BF
112
113 if (prealloc)
114 xfs_dquot_set_prealloc_limits(dq);
1da177e4
LT
115}
116
117/*
118 * Check the limits and timers of a dquot and start or reset timers
119 * if necessary.
120 * This gets called even when quota enforcement is OFF, which makes our
121 * life a little less complicated. (We just don't reject any quota
122 * reservations in that case, when enforcement is off).
123 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
124 * enforcement's off.
125 * In contrast, warnings are a little different in that they don't
754002b4
NS
126 * 'automatically' get started when limits get exceeded. They do
127 * get reset to zero, however, when we find the count to be under
128 * the soft limit (they are only ever set non-zero via userspace).
1da177e4
LT
129 */
130void
131xfs_qm_adjust_dqtimers(
132 xfs_mount_t *mp,
133 xfs_disk_dquot_t *d)
134{
135 ASSERT(d->d_id);
136
ea15ab3c 137#ifdef DEBUG
1149d96a
CH
138 if (d->d_blk_hardlimit)
139 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
140 be64_to_cpu(d->d_blk_hardlimit));
141 if (d->d_ino_hardlimit)
142 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
143 be64_to_cpu(d->d_ino_hardlimit));
144 if (d->d_rtb_hardlimit)
145 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
146 be64_to_cpu(d->d_rtb_hardlimit));
1da177e4 147#endif
ea15ab3c 148
1da177e4 149 if (!d->d_btimer) {
1149d96a 150 if ((d->d_blk_softlimit &&
d0a3fe67 151 (be64_to_cpu(d->d_bcount) >
1149d96a
CH
152 be64_to_cpu(d->d_blk_softlimit))) ||
153 (d->d_blk_hardlimit &&
d0a3fe67 154 (be64_to_cpu(d->d_bcount) >
1149d96a
CH
155 be64_to_cpu(d->d_blk_hardlimit)))) {
156 d->d_btimer = cpu_to_be32(get_seconds() +
8a7b8a89 157 mp->m_quotainfo->qi_btimelimit);
754002b4
NS
158 } else {
159 d->d_bwarns = 0;
1da177e4
LT
160 }
161 } else {
162 if ((!d->d_blk_softlimit ||
d0a3fe67 163 (be64_to_cpu(d->d_bcount) <=
1149d96a 164 be64_to_cpu(d->d_blk_softlimit))) &&
1da177e4 165 (!d->d_blk_hardlimit ||
d0a3fe67 166 (be64_to_cpu(d->d_bcount) <=
1149d96a 167 be64_to_cpu(d->d_blk_hardlimit)))) {
1da177e4
LT
168 d->d_btimer = 0;
169 }
170 }
171
172 if (!d->d_itimer) {
1149d96a 173 if ((d->d_ino_softlimit &&
d0a3fe67 174 (be64_to_cpu(d->d_icount) >
1149d96a
CH
175 be64_to_cpu(d->d_ino_softlimit))) ||
176 (d->d_ino_hardlimit &&
d0a3fe67 177 (be64_to_cpu(d->d_icount) >
1149d96a
CH
178 be64_to_cpu(d->d_ino_hardlimit)))) {
179 d->d_itimer = cpu_to_be32(get_seconds() +
8a7b8a89 180 mp->m_quotainfo->qi_itimelimit);
754002b4
NS
181 } else {
182 d->d_iwarns = 0;
1da177e4
LT
183 }
184 } else {
185 if ((!d->d_ino_softlimit ||
d0a3fe67 186 (be64_to_cpu(d->d_icount) <=
1149d96a 187 be64_to_cpu(d->d_ino_softlimit))) &&
1da177e4 188 (!d->d_ino_hardlimit ||
d0a3fe67 189 (be64_to_cpu(d->d_icount) <=
1149d96a 190 be64_to_cpu(d->d_ino_hardlimit)))) {
1da177e4
LT
191 d->d_itimer = 0;
192 }
193 }
194
195 if (!d->d_rtbtimer) {
1149d96a 196 if ((d->d_rtb_softlimit &&
d0a3fe67 197 (be64_to_cpu(d->d_rtbcount) >
1149d96a
CH
198 be64_to_cpu(d->d_rtb_softlimit))) ||
199 (d->d_rtb_hardlimit &&
d0a3fe67 200 (be64_to_cpu(d->d_rtbcount) >
1149d96a
CH
201 be64_to_cpu(d->d_rtb_hardlimit)))) {
202 d->d_rtbtimer = cpu_to_be32(get_seconds() +
8a7b8a89 203 mp->m_quotainfo->qi_rtbtimelimit);
754002b4
NS
204 } else {
205 d->d_rtbwarns = 0;
1da177e4
LT
206 }
207 } else {
208 if ((!d->d_rtb_softlimit ||
d0a3fe67 209 (be64_to_cpu(d->d_rtbcount) <=
1149d96a 210 be64_to_cpu(d->d_rtb_softlimit))) &&
1da177e4 211 (!d->d_rtb_hardlimit ||
d0a3fe67 212 (be64_to_cpu(d->d_rtbcount) <=
1149d96a 213 be64_to_cpu(d->d_rtb_hardlimit)))) {
1da177e4
LT
214 d->d_rtbtimer = 0;
215 }
216 }
217}
218
1da177e4
LT
219/*
220 * initialize a buffer full of dquots and log the whole thing
221 */
222STATIC void
223xfs_qm_init_dquot_blk(
224 xfs_trans_t *tp,
225 xfs_mount_t *mp,
226 xfs_dqid_t id,
227 uint type,
228 xfs_buf_t *bp)
229{
8a7b8a89 230 struct xfs_quotainfo *q = mp->m_quotainfo;
1da177e4 231 xfs_dqblk_t *d;
a484bcdd
ES
232 xfs_dqid_t curid;
233 int i;
1da177e4
LT
234
235 ASSERT(tp);
0c842ad4 236 ASSERT(xfs_buf_islocked(bp));
1da177e4 237
62926044 238 d = bp->b_addr;
1da177e4
LT
239
240 /*
241 * ID of the first dquot in the block - id's are zero based.
242 */
8a7b8a89 243 curid = id - (id % q->qi_dqperchunk);
8a7b8a89 244 memset(d, 0, BBTOB(q->qi_dqchunklen));
49d35a5c
CH
245 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
246 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
247 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
248 d->dd_diskdq.d_id = cpu_to_be32(curid);
249 d->dd_diskdq.d_flags = type;
6fcdc59d 250 if (xfs_sb_version_hascrc(&mp->m_sb)) {
92863451 251 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
6fcdc59d
DC
252 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
253 XFS_DQUOT_CRC_OFF);
254 }
49d35a5c
CH
255 }
256
1da177e4 257 xfs_trans_dquot_buf(tp, bp,
c1155410
DC
258 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
259 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
260 XFS_BLF_GDQUOT_BUF)));
8a7b8a89 261 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
1da177e4
LT
262}
263
b1366451
BF
264/*
265 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
266 * watermarks correspond to the soft and hard limits by default. If a soft limit
267 * is not specified, we use 95% of the hard limit.
268 */
269void
270xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
271{
c8ce540d 272 uint64_t space;
b1366451
BF
273
274 dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
275 dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
276 if (!dqp->q_prealloc_lo_wmark) {
277 dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
278 do_div(dqp->q_prealloc_lo_wmark, 100);
279 dqp->q_prealloc_lo_wmark *= 95;
280 }
281
282 space = dqp->q_prealloc_hi_wmark;
283
284 do_div(space, 100);
285 dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
286 dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
287 dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
288}
289
1da177e4 290/*
d63192c8
DW
291 * Ensure that the given in-core dquot has a buffer on disk backing it, and
292 * return the buffer. This is called when the bmapi finds a hole.
1da177e4
LT
293 */
294STATIC int
d63192c8
DW
295xfs_dquot_disk_alloc(
296 struct xfs_trans **tpp,
297 struct xfs_dquot *dqp,
298 struct xfs_buf **bpp)
1da177e4 299{
d63192c8
DW
300 struct xfs_bmbt_irec map;
301 struct xfs_defer_ops dfops;
302 struct xfs_mount *mp = (*tpp)->t_mountp;
303 struct xfs_buf *bp;
304 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
305 xfs_fsblock_t firstblock;
306 int nmaps = 1;
307 int error;
0b1b213f
CH
308
309 trace_xfs_dqalloc(dqp);
1da177e4 310
2c3234d1 311 xfs_defer_init(&dfops, &firstblock);
1da177e4 312 xfs_ilock(quotip, XFS_ILOCK_EXCL);
6967b964 313 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
d63192c8
DW
314 /*
315 * Return if this type of quotas is turned off while we didn't
316 * have an inode lock
317 */
1da177e4 318 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
2451337d 319 return -ESRCH;
1da177e4
LT
320 }
321
d63192c8
DW
322 /* Create the block mapping. */
323 xfs_trans_ijoin(*tpp, quotip, XFS_ILOCK_EXCL);
324 error = xfs_bmapi_write(*tpp, quotip, dqp->q_fileoffset,
325 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
326 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
327 &map, &nmaps, &dfops);
c0dc7828 328 if (error)
1da177e4 329 goto error0;
1da177e4
LT
330 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
331 ASSERT(nmaps == 1);
332 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
333 (map.br_startblock != HOLESTARTBLOCK));
334
335 /*
336 * Keep track of the blkno to save a lookup later
337 */
338 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
339
340 /* now we can just get the buffer (there's nothing to read yet) */
d63192c8
DW
341 bp = xfs_trans_get_buf(*tpp, mp->m_ddev_targp, dqp->q_blkno,
342 mp->m_quotainfo->qi_dqchunklen, 0);
36de9556 343 if (!bp) {
2451337d 344 error = -ENOMEM;
1da177e4 345 goto error1;
36de9556 346 }
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 */
d63192c8 353 xfs_qm_init_dquot_blk(*tpp, mp, be32_to_cpu(dqp->q_core.d_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 *
7b6b50f5
DW
367 * Above, we allocated a disk block for the dquot information and
368 * used get_buf to initialize the dquot. If the _defer_bjoin fails,
369 * the buffer is still locked to *tpp, so we must _bhold_release and
370 * then _trans_brelse the buffer. If the _defer_finish fails, the old
371 * transaction is gone but the new buffer is not joined or held to any
372 * transaction, so we must _buf_relse it.
efa092f3 373 *
7b6b50f5 374 * If everything succeeds, the caller of this function is returned a
d63192c8 375 * buffer that is locked and held to the transaction. The caller
7b6b50f5
DW
376 * is responsible for unlocking any buffer passed back, either
377 * manually or by committing the transaction.
efa092f3 378 */
7b6b50f5
DW
379 xfs_trans_bhold(*tpp, bp);
380 error = xfs_defer_bjoin(&dfops, bp);
381 if (error) {
382 xfs_trans_bhold_release(*tpp, bp);
383 xfs_trans_brelse(*tpp, bp);
384 goto error1;
385 }
8ad7c629 386 error = xfs_defer_finish(tpp, &dfops);
7b6b50f5
DW
387 if (error) {
388 xfs_buf_relse(bp);
1da177e4 389 goto error1;
efa092f3 390 }
d63192c8 391 *bpp = bp;
1da177e4
LT
392 return 0;
393
f6106efa 394error1:
2c3234d1 395 xfs_defer_cancel(&dfops);
f6106efa 396error0:
d99831ff 397 return error;
1da177e4 398}
9aede1d8 399
1da177e4 400/*
d63192c8
DW
401 * Read in the in-core dquot's on-disk metadata and return the buffer.
402 * Returns ENOENT to signal a hole.
1da177e4
LT
403 */
404STATIC int
d63192c8
DW
405xfs_dquot_disk_read(
406 struct xfs_mount *mp,
407 struct xfs_dquot *dqp,
408 struct xfs_buf **bpp)
1da177e4 409{
113a5683 410 struct xfs_bmbt_irec map;
113a5683 411 struct xfs_buf *bp;
d63192c8 412 struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
0891f997 413 uint lock_mode;
d63192c8
DW
414 int nmaps = 1;
415 int error;
1da177e4 416
0891f997 417 lock_mode = xfs_ilock_data_map_shared(quotip);
d63192c8 418 if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
1da177e4 419 /*
acecf1b5
CH
420 * Return if this type of quotas is turned off while we
421 * didn't have the quota inode lock.
1da177e4 422 */
0891f997 423 xfs_iunlock(quotip, lock_mode);
2451337d 424 return -ESRCH;
acecf1b5
CH
425 }
426
427 /*
428 * Find the block map; no allocations yet
429 */
5c8ed202 430 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
d63192c8 431 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
0891f997 432 xfs_iunlock(quotip, lock_mode);
acecf1b5
CH
433 if (error)
434 return error;
435
436 ASSERT(nmaps == 1);
d63192c8
DW
437 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);
acecf1b5
CH
443
444 /*
d63192c8
DW
445 * store the blkno etc so that we don't have to do the
446 * mapping all the time
acecf1b5 447 */
d63192c8 448 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
1da177e4 449
d63192c8
DW
450 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;
1da177e4
LT
456 }
457
c6319198 458 ASSERT(xfs_buf_islocked(bp));
d63192c8
DW
459 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
460 *bpp = bp;
1da177e4 461
d99831ff 462 return 0;
1da177e4
LT
463}
464
617cd5c1
DW
465/* Allocate and initialize everything we need for an incore dquot. */
466STATIC struct xfs_dquot *
467xfs_dquot_alloc(
97e7ade5
CH
468 struct xfs_mount *mp,
469 xfs_dqid_t id,
617cd5c1 470 uint type)
1da177e4 471{
97e7ade5 472 struct xfs_dquot *dqp;
92b2e5b3 473
a05931ce 474 dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
92b2e5b3
CH
475
476 dqp->dq_flags = type;
477 dqp->q_core.d_id = cpu_to_be32(id);
478 dqp->q_mount = mp;
f8739c3c 479 INIT_LIST_HEAD(&dqp->q_lru);
92b2e5b3
CH
480 mutex_init(&dqp->q_qlock);
481 init_waitqueue_head(&dqp->q_pinwait);
d63192c8
DW
482 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);
92b2e5b3
CH
488
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 */
f112a049
DC
501 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 }
92b2e5b3 515
617cd5c1
DW
516 xfs_qm_dquot_logitem_init(dqp);
517
ff6d6af2 518 XFS_STATS_INC(mp, xs_qm_dquot);
617cd5c1
DW
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,
d63192c8 526 struct xfs_buf *bp)
617cd5c1 527{
d63192c8
DW
528 struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
529
617cd5c1
DW
530 /* copy everything from disk dquot to the incore dquot */
531 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
532
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}
1da177e4 544
d63192c8
DW
545/* 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;
553 struct xfs_buf *bp;
554 int error;
555
556 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
557 XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
558 if (error)
559 goto err;
560
561 error = xfs_dquot_disk_alloc(&tp, dqp, &bp);
562 if (error)
563 goto err_cancel;
564
565 error = xfs_trans_commit(tp);
566 if (error) {
567 /*
568 * Buffer was held to the transaction, so we have to unlock it
569 * manually here because we're not passing it back.
570 */
571 xfs_buf_relse(bp);
572 goto err;
573 }
574 *bpp = bp;
575 return 0;
576
577err_cancel:
578 xfs_trans_cancel(tp);
579err:
580 return error;
581}
582
617cd5c1
DW
583/*
584 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
30ab2dcf
DW
585 * and release the buffer immediately. If @can_alloc is true, fill any
586 * holes in the on-disk metadata.
617cd5c1 587 */
114e73cc 588static int
617cd5c1
DW
589xfs_qm_dqread(
590 struct xfs_mount *mp,
591 xfs_dqid_t id,
592 uint type,
30ab2dcf 593 bool can_alloc,
d63192c8 594 struct xfs_dquot **dqpp)
617cd5c1
DW
595{
596 struct xfs_dquot *dqp;
617cd5c1 597 struct xfs_buf *bp;
617cd5c1
DW
598 int error;
599
600 dqp = xfs_dquot_alloc(mp, id, type);
0b1b213f
CH
601 trace_xfs_dqread(dqp);
602
d63192c8
DW
603 /* Try to read the buffer, allocating if necessary. */
604 error = xfs_dquot_disk_read(mp, dqp, &bp);
30ab2dcf 605 if (error == -ENOENT && can_alloc)
d63192c8
DW
606 error = xfs_qm_dqread_alloc(mp, dqp, &bp);
607 if (error)
608 goto err;
1da177e4
LT
609
610 /*
d63192c8
DW
611 * At this point we should have a clean locked buffer. Copy the data
612 * to the incore dquot and release the buffer since the incore dquot
613 * has its own locking protocol so we needn't tie up the buffer any
614 * further.
1da177e4 615 */
0c842ad4 616 ASSERT(xfs_buf_islocked(bp));
d63192c8 617 xfs_dquot_from_disk(dqp, bp);
1da177e4 618
d63192c8
DW
619 xfs_buf_relse(bp);
620 *dqpp = dqp;
97e7ade5 621 return error;
1da177e4 622
d63192c8
DW
623err:
624 trace_xfs_dqread_fail(dqp);
1da177e4 625 xfs_qm_dqdestroy(dqp);
d63192c8 626 *dqpp = NULL;
97e7ade5 627 return error;
1da177e4
LT
628}
629
296c24e2
ES
630/*
631 * Advance to the next id in the current chunk, or if at the
632 * end of the chunk, skip ahead to first id in next allocated chunk
633 * using the SEEK_DATA interface.
634 */
6e3e6d55 635static int
296c24e2 636xfs_dq_get_next_id(
bda250db 637 struct xfs_mount *mp,
296c24e2 638 uint type,
bda250db 639 xfs_dqid_t *id)
296c24e2 640{
bda250db
CH
641 struct xfs_inode *quotip = xfs_quota_inode(mp, type);
642 xfs_dqid_t next_id = *id + 1; /* simple advance */
643 uint lock_flags;
644 struct xfs_bmbt_irec got;
b2b1712a 645 struct xfs_iext_cursor cur;
296c24e2 646 xfs_fsblock_t start;
296c24e2
ES
647 int error = 0;
648
657bdfb7
ES
649 /* If we'd wrap past the max ID, stop */
650 if (next_id < *id)
651 return -ENOENT;
652
296c24e2
ES
653 /* If new ID is within the current chunk, advancing it sufficed */
654 if (next_id % mp->m_quotainfo->qi_dqperchunk) {
655 *id = next_id;
656 return 0;
657 }
658
659 /* Nope, next_id is now past the current chunk, so find the next one */
660 start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
661
bda250db
CH
662 lock_flags = xfs_ilock_data_map_shared(quotip);
663 if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
664 error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
665 if (error)
666 return error;
667 }
296c24e2 668
b2b1712a 669 if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
2192b0ba
BF
670 /* contiguous chunk, bump startoff for the id calculation */
671 if (got.br_startoff < start)
672 got.br_startoff = start;
bda250db 673 *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
2192b0ba 674 } else {
bda250db 675 error = -ENOENT;
2192b0ba
BF
676 }
677
bda250db 678 xfs_iunlock(quotip, lock_flags);
296c24e2 679
bda250db 680 return error;
296c24e2
ES
681}
682
cc2047c4
DW
683/*
684 * Look up the dquot in the in-core cache. If found, the dquot is returned
685 * locked and ready to go.
686 */
687static struct xfs_dquot *
688xfs_qm_dqget_cache_lookup(
689 struct xfs_mount *mp,
690 struct xfs_quotainfo *qi,
691 struct radix_tree_root *tree,
692 xfs_dqid_t id)
693{
694 struct xfs_dquot *dqp;
695
696restart:
697 mutex_lock(&qi->qi_tree_lock);
698 dqp = radix_tree_lookup(tree, id);
699 if (!dqp) {
700 mutex_unlock(&qi->qi_tree_lock);
701 XFS_STATS_INC(mp, xs_qm_dqcachemisses);
702 return NULL;
703 }
704
705 xfs_dqlock(dqp);
706 if (dqp->dq_flags & XFS_DQ_FREEING) {
707 xfs_dqunlock(dqp);
708 mutex_unlock(&qi->qi_tree_lock);
709 trace_xfs_dqget_freeing(dqp);
710 delay(1);
711 goto restart;
712 }
713
714 dqp->q_nrefs++;
715 mutex_unlock(&qi->qi_tree_lock);
716
717 trace_xfs_dqget_hit(dqp);
718 XFS_STATS_INC(mp, xs_qm_dqcachehits);
719 return dqp;
720}
721
722/*
723 * Try to insert a new dquot into the in-core cache. If an error occurs the
724 * caller should throw away the dquot and start over. Otherwise, the dquot
725 * is returned locked (and held by the cache) as if there had been a cache
726 * hit.
727 */
728static int
729xfs_qm_dqget_cache_insert(
730 struct xfs_mount *mp,
731 struct xfs_quotainfo *qi,
732 struct radix_tree_root *tree,
733 xfs_dqid_t id,
734 struct xfs_dquot *dqp)
735{
736 int error;
737
738 mutex_lock(&qi->qi_tree_lock);
739 error = radix_tree_insert(tree, id, dqp);
740 if (unlikely(error)) {
741 /* Duplicate found! Caller must try again. */
742 WARN_ON(error != -EEXIST);
743 mutex_unlock(&qi->qi_tree_lock);
744 trace_xfs_dqget_dup(dqp);
745 return error;
746 }
747
748 /* Return a locked dquot to the caller, with a reference taken. */
749 xfs_dqlock(dqp);
750 dqp->q_nrefs = 1;
751
752 qi->qi_dquots++;
753 mutex_unlock(&qi->qi_tree_lock);
754
755 return 0;
756}
757
d7103eeb
DW
758/* Check our input parameters. */
759static int
760xfs_qm_dqget_checks(
761 struct xfs_mount *mp,
762 uint type)
763{
764 if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
765 return -ESRCH;
766
767 switch (type) {
768 case XFS_DQ_USER:
769 if (!XFS_IS_UQUOTA_ON(mp))
770 return -ESRCH;
771 return 0;
772 case XFS_DQ_GROUP:
773 if (!XFS_IS_GQUOTA_ON(mp))
774 return -ESRCH;
775 return 0;
776 case XFS_DQ_PROJ:
777 if (!XFS_IS_PQUOTA_ON(mp))
778 return -ESRCH;
779 return 0;
780 default:
781 WARN_ON_ONCE(0);
782 return -EINVAL;
783 }
784}
785
1da177e4 786/*
4882c19d
DW
787 * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
788 * dquot, doing an allocation (if requested) as needed.
1da177e4
LT
789 */
790int
791xfs_qm_dqget(
4882c19d
DW
792 struct xfs_mount *mp,
793 xfs_dqid_t id,
794 uint type,
30ab2dcf 795 bool can_alloc,
4882c19d 796 struct xfs_dquot **O_dqpp)
1da177e4 797{
9f920f11 798 struct xfs_quotainfo *qi = mp->m_quotainfo;
4882c19d 799 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
9f920f11
CH
800 struct xfs_dquot *dqp;
801 int error;
1da177e4 802
d7103eeb
DW
803 error = xfs_qm_dqget_checks(mp, type);
804 if (error)
805 return error;
1da177e4 806
4882c19d
DW
807restart:
808 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
809 if (dqp) {
810 *O_dqpp = dqp;
811 return 0;
812 }
813
30ab2dcf 814 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
4882c19d
DW
815 if (error)
816 return error;
817
818 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
819 if (error) {
820 /*
821 * Duplicate found. Just throw away the new dquot and start
822 * over.
823 */
824 xfs_qm_dqdestroy(dqp);
825 XFS_STATS_INC(mp, xs_qm_dquot_dups);
826 goto restart;
827 }
828
829 trace_xfs_dqget_miss(dqp);
830 *O_dqpp = dqp;
831 return 0;
832}
833
114e73cc
DW
834/*
835 * Given a dquot id and type, read and initialize a dquot from the on-disk
836 * metadata. This function is only for use during quota initialization so
837 * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
838 * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
839 */
840int
841xfs_qm_dqget_uncached(
842 struct xfs_mount *mp,
843 xfs_dqid_t id,
844 uint type,
845 struct xfs_dquot **dqpp)
846{
847 int error;
848
849 error = xfs_qm_dqget_checks(mp, type);
850 if (error)
851 return error;
852
853 return xfs_qm_dqread(mp, id, type, 0, dqpp);
854}
855
4882c19d
DW
856/* Return the quota id for a given inode and type. */
857xfs_dqid_t
858xfs_qm_id_for_quotatype(
859 struct xfs_inode *ip,
860 uint type)
861{
862 switch (type) {
863 case XFS_DQ_USER:
864 return ip->i_d.di_uid;
865 case XFS_DQ_GROUP:
866 return ip->i_d.di_gid;
867 case XFS_DQ_PROJ:
868 return xfs_get_projid(ip);
1da177e4 869 }
4882c19d
DW
870 ASSERT(0);
871 return 0;
872}
873
874/*
875 * Return the dquot for a given inode and type. If @can_alloc is true, then
876 * allocate blocks if needed. The inode's ILOCK must be held and it must not
877 * have already had an inode attached.
878 */
879int
880xfs_qm_dqget_inode(
881 struct xfs_inode *ip,
882 uint type,
883 bool can_alloc,
884 struct xfs_dquot **O_dqpp)
885{
886 struct xfs_mount *mp = ip->i_mount;
887 struct xfs_quotainfo *qi = mp->m_quotainfo;
888 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
889 struct xfs_dquot *dqp;
890 xfs_dqid_t id;
4882c19d
DW
891 int error;
892
893 error = xfs_qm_dqget_checks(mp, type);
894 if (error)
895 return error;
896
4882c19d
DW
897 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
898 ASSERT(xfs_inode_dquot(ip, type) == NULL);
899
900 id = xfs_qm_id_for_quotatype(ip, type);
92678554
CH
901
902restart:
cc2047c4 903 dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
9f920f11 904 if (dqp) {
9f920f11
CH
905 *O_dqpp = dqp;
906 return 0;
1da177e4 907 }
1da177e4
LT
908
909 /*
910 * Dquot cache miss. We don't want to keep the inode lock across
911 * a (potential) disk read. Also we don't want to deal with the lock
912 * ordering between quotainode and this inode. OTOH, dropping the inode
913 * lock here means dealing with a chown that can happen before
914 * we re-acquire the lock.
915 */
4882c19d 916 xfs_iunlock(ip, XFS_ILOCK_EXCL);
30ab2dcf 917 error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
4882c19d 918 xfs_ilock(ip, XFS_ILOCK_EXCL);
7ae44407
CH
919 if (error)
920 return error;
1da177e4 921
4882c19d
DW
922 /*
923 * A dquot could be attached to this inode by now, since we had
924 * dropped the ilock.
925 */
926 if (xfs_this_quota_on(mp, type)) {
927 struct xfs_dquot *dqp1;
928
929 dqp1 = xfs_inode_dquot(ip, type);
930 if (dqp1) {
36731410 931 xfs_qm_dqdestroy(dqp);
4882c19d
DW
932 dqp = dqp1;
933 xfs_dqlock(dqp);
934 goto dqret;
1da177e4 935 }
4882c19d
DW
936 } else {
937 /* inode stays locked on return */
938 xfs_qm_dqdestroy(dqp);
939 return -ESRCH;
1da177e4
LT
940 }
941
cc2047c4
DW
942 error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
943 if (error) {
1da177e4 944 /*
9f920f11
CH
945 * Duplicate found. Just throw away the new dquot and start
946 * over.
1da177e4 947 */
9f920f11 948 xfs_qm_dqdestroy(dqp);
ff6d6af2 949 XFS_STATS_INC(mp, xs_qm_dquot_dups);
9f920f11 950 goto restart;
1da177e4
LT
951 }
952
4882c19d
DW
953dqret:
954 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
0b1b213f 955 trace_xfs_dqget_miss(dqp);
1da177e4 956 *O_dqpp = dqp;
d99831ff 957 return 0;
1da177e4
LT
958}
959
2e330e76
DW
960/*
961 * Starting at @id and progressing upwards, look for an initialized incore
962 * dquot, lock it, and return it.
963 */
964int
965xfs_qm_dqget_next(
966 struct xfs_mount *mp,
967 xfs_dqid_t id,
968 uint type,
969 struct xfs_dquot **dqpp)
970{
971 struct xfs_dquot *dqp;
972 int error = 0;
973
974 *dqpp = NULL;
975 for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
30ab2dcf 976 error = xfs_qm_dqget(mp, id, type, false, &dqp);
2e330e76
DW
977 if (error == -ENOENT)
978 continue;
979 else if (error != 0)
980 break;
981
982 if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
983 *dqpp = dqp;
984 return 0;
985 }
986
987 xfs_qm_dqput(dqp);
988 }
989
990 return error;
991}
992
f8739c3c
CH
993/*
994 * Release a reference to the dquot (decrement ref-count) and unlock it.
995 *
996 * If there is a group quota attached to this dquot, carefully release that
997 * too without tripping over deadlocks'n'stuff.
998 */
999void
1000xfs_qm_dqput(
1001 struct xfs_dquot *dqp)
1002{
1003 ASSERT(dqp->q_nrefs > 0);
1004 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1005
1006 trace_xfs_dqput(dqp);
1007
3c353375
DC
1008 if (--dqp->q_nrefs == 0) {
1009 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
1010 trace_xfs_dqput_free(dqp);
1011
1012 if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
ff6d6af2 1013 XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
3c353375
DC
1014 }
1015 xfs_dqunlock(dqp);
1da177e4
LT
1016}
1017
1018/*
1019 * Release a dquot. Flush it if dirty, then dqput() it.
1020 * dquot must not be locked.
1021 */
1022void
1023xfs_qm_dqrele(
1024 xfs_dquot_t *dqp)
1025{
7d095257
CH
1026 if (!dqp)
1027 return;
1028
0b1b213f 1029 trace_xfs_dqrele(dqp);
1da177e4
LT
1030
1031 xfs_dqlock(dqp);
1032 /*
1033 * We don't care to flush it if the dquot is dirty here.
1034 * That will create stutters that we want to avoid.
1035 * Instead we do a delayed write when we try to reclaim
1036 * a dirty dquot. Also xfs_sync will take part of the burden...
1037 */
1038 xfs_qm_dqput(dqp);
1039}
1040
ca30b2a7
CH
1041/*
1042 * This is the dquot flushing I/O completion routine. It is called
1043 * from interrupt level when the buffer containing the dquot is
1044 * flushed to disk. It is responsible for removing the dquot logitem
1045 * from the AIL if it has not been re-logged, and unlocking the dquot's
1046 * flush lock. This behavior is very similar to that of inodes..
1047 */
1048STATIC void
1049xfs_qm_dqflush_done(
1050 struct xfs_buf *bp,
1051 struct xfs_log_item *lip)
1052{
1053 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
1054 xfs_dquot_t *dqp = qip->qli_dquot;
1055 struct xfs_ail *ailp = lip->li_ailp;
1056
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 */
22525c17 1065 if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
373b0589 1066 ((lip->li_lsn == qip->qli_flush_lsn) ||
22525c17 1067 test_bit(XFS_LI_FAILED, &lip->li_flags))) {
ca30b2a7
CH
1068
1069 /* xfs_trans_ail_delete() drops the AIL lock. */
57e80956 1070 spin_lock(&ailp->ail_lock);
373b0589 1071 if (lip->li_lsn == qip->qli_flush_lsn) {
04913fdd 1072 xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
373b0589
CM
1073 } else {
1074 /*
1075 * Clear the failed state since we are about to drop the
1076 * flush lock
1077 */
22525c17 1078 xfs_clear_li_failed(lip);
57e80956 1079 spin_unlock(&ailp->ail_lock);
373b0589 1080 }
ca30b2a7
CH
1081 }
1082
1083 /*
1084 * Release the dq's flush lock since we're done with it.
1085 */
1086 xfs_dqfunlock(dqp);
1087}
1da177e4
LT
1088
1089/*
1090 * Write a modified dquot to disk.
1091 * The dquot must be locked and the flush lock too taken by caller.
1092 * The flush lock will not be unlocked until the dquot reaches the disk,
1093 * but the dquot is free to be unlocked and modified by the caller
1094 * in the interim. Dquot is still locked on return. This behavior is
1095 * identical to that of inodes.
1096 */
1097int
1098xfs_qm_dqflush(
fe7257fd
CH
1099 struct xfs_dquot *dqp,
1100 struct xfs_buf **bpp)
1da177e4 1101{
acecf1b5
CH
1102 struct xfs_mount *mp = dqp->q_mount;
1103 struct xfs_buf *bp;
7224fa48 1104 struct xfs_dqblk *dqb;
acecf1b5 1105 struct xfs_disk_dquot *ddqp;
eebf3cab 1106 xfs_failaddr_t fa;
1da177e4 1107 int error;
1da177e4
LT
1108
1109 ASSERT(XFS_DQ_IS_LOCKED(dqp));
e1f49cf2 1110 ASSERT(!completion_done(&dqp->q_flush));
acecf1b5 1111
0b1b213f 1112 trace_xfs_dqflush(dqp);
1da177e4 1113
fe7257fd
CH
1114 *bpp = NULL;
1115
1da177e4
LT
1116 xfs_qm_dqunpin_wait(dqp);
1117
1118 /*
1119 * This may have been unpinned because the filesystem is shutting
1120 * down forcibly. If that's the case we must not write this dquot
dea96095
CH
1121 * to disk, because the log record didn't make it to disk.
1122 *
1123 * We also have to remove the log item from the AIL in this case,
1124 * as we wait for an emptry AIL as part of the unmount process.
1da177e4 1125 */
acecf1b5 1126 if (XFS_FORCED_SHUTDOWN(mp)) {
dea96095 1127 struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
acecf1b5 1128 dqp->dq_flags &= ~XFS_DQ_DIRTY;
dea96095 1129
146e54b7
BF
1130 xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);
1131
2451337d 1132 error = -EIO;
fe7257fd 1133 goto out_unlock;
1da177e4
LT
1134 }
1135
1136 /*
1137 * Get the buffer containing the on-disk dquot
1da177e4 1138 */
acecf1b5 1139 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
5fd364fe
DC
1140 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
1141 &xfs_dquot_buf_ops);
fe7257fd
CH
1142 if (error)
1143 goto out_unlock;
1da177e4 1144
acecf1b5
CH
1145 /*
1146 * Calculate the location of the dquot inside the buffer.
1147 */
7224fa48
ES
1148 dqb = bp->b_addr + dqp->q_bufoffset;
1149 ddqp = &dqb->dd_diskdq;
acecf1b5
CH
1150
1151 /*
7224fa48 1152 * A simple sanity check in case we got a corrupted dquot.
acecf1b5 1153 */
7224fa48 1154 fa = xfs_dqblk_verify(mp, dqb, be32_to_cpu(ddqp->d_id), 0);
eebf3cab
DW
1155 if (fa) {
1156 xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
1157 be32_to_cpu(ddqp->d_id), fa);
acecf1b5
CH
1158 xfs_buf_relse(bp);
1159 xfs_dqfunlock(dqp);
1160 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2451337d 1161 return -EIO;
1da177e4
LT
1162 }
1163
1164 /* This is the only portion of data that needs to persist */
acecf1b5 1165 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
1da177e4
LT
1166
1167 /*
1168 * Clear the dirty field and remember the flush lsn for later use.
1169 */
acecf1b5 1170 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1da177e4 1171
7b2e2a31
DC
1172 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1173 &dqp->q_logitem.qli_item.li_lsn);
1da177e4 1174
3fe58f30
CH
1175 /*
1176 * copy the lsn into the on-disk dquot now while we have the in memory
1177 * dquot here. This can't be done later in the write verifier as we
1178 * can't get access to the log item at that point in time.
6fcdc59d
DC
1179 *
1180 * We also calculate the CRC here so that the on-disk dquot in the
1181 * buffer always has a valid CRC. This ensures there is no possibility
1182 * of a dquot without an up-to-date CRC getting to disk.
3fe58f30
CH
1183 */
1184 if (xfs_sb_version_hascrc(&mp->m_sb)) {
3fe58f30 1185 dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
6fcdc59d
DC
1186 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
1187 XFS_DQUOT_CRC_OFF);
3fe58f30
CH
1188 }
1189
1da177e4
LT
1190 /*
1191 * Attach an iodone routine so that we can remove this dquot from the
1192 * AIL and release the flush lock once the dquot is synced to disk.
1193 */
ca30b2a7
CH
1194 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1195 &dqp->q_logitem.qli_item);
1196
1da177e4
LT
1197 /*
1198 * If the buffer is pinned then push on the log so we won't
1199 * get stuck waiting in the write for too long.
1200 */
811e64c7 1201 if (xfs_buf_ispinned(bp)) {
0b1b213f 1202 trace_xfs_dqflush_force(dqp);
a14a348b 1203 xfs_log_force(mp, 0);
1da177e4
LT
1204 }
1205
0b1b213f 1206 trace_xfs_dqflush_done(dqp);
fe7257fd
CH
1207 *bpp = bp;
1208 return 0;
0b1b213f 1209
fe7257fd
CH
1210out_unlock:
1211 xfs_dqfunlock(dqp);
2451337d 1212 return -EIO;
1da177e4
LT
1213}
1214
5bb87a33
CH
1215/*
1216 * Lock two xfs_dquot structures.
1217 *
1218 * To avoid deadlocks we always lock the quota structure with
1219 * the lowerd id first.
1220 */
1da177e4
LT
1221void
1222xfs_dqlock2(
1223 xfs_dquot_t *d1,
1224 xfs_dquot_t *d2)
1225{
1226 if (d1 && d2) {
1227 ASSERT(d1 != d2);
1149d96a
CH
1228 if (be32_to_cpu(d1->q_core.d_id) >
1229 be32_to_cpu(d2->q_core.d_id)) {
5bb87a33
CH
1230 mutex_lock(&d2->q_qlock);
1231 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1232 } else {
5bb87a33
CH
1233 mutex_lock(&d1->q_qlock);
1234 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1235 }
5bb87a33
CH
1236 } else if (d1) {
1237 mutex_lock(&d1->q_qlock);
1238 } else if (d2) {
1239 mutex_lock(&d2->q_qlock);
1da177e4
LT
1240 }
1241}
1242
a05931ce
CH
1243int __init
1244xfs_qm_init(void)
1245{
1246 xfs_qm_dqzone =
1247 kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
1248 if (!xfs_qm_dqzone)
1249 goto out;
1250
1251 xfs_qm_dqtrxzone =
1252 kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
1253 if (!xfs_qm_dqtrxzone)
1254 goto out_free_dqzone;
1255
1256 return 0;
1257
1258out_free_dqzone:
1259 kmem_zone_destroy(xfs_qm_dqzone);
1260out:
1261 return -ENOMEM;
1262}
1263
1c2ccc66 1264void
a05931ce
CH
1265xfs_qm_exit(void)
1266{
1267 kmem_zone_destroy(xfs_qm_dqtrxzone);
1268 kmem_zone_destroy(xfs_qm_dqzone);
1269}