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