]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/quota/xfs_dquot.c
xfs: give xfs_item_ops methods the correct prototypes
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / quota / 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"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4 26#include "xfs_alloc.h"
1da177e4
LT
27#include "xfs_quota.h"
28#include "xfs_mount.h"
1da177e4 29#include "xfs_bmap_btree.h"
1da177e4
LT
30#include "xfs_inode.h"
31#include "xfs_bmap.h"
1da177e4
LT
32#include "xfs_rtalloc.h"
33#include "xfs_error.h"
34#include "xfs_itable.h"
1da177e4
LT
35#include "xfs_attr.h"
36#include "xfs_buf_item.h"
37#include "xfs_trans_space.h"
38#include "xfs_trans_priv.h"
1da177e4 39#include "xfs_qm.h"
0b1b213f 40#include "xfs_trace.h"
1da177e4
LT
41
42
43/*
44 LOCK ORDER
45
46 inode lock (ilock)
47 dquot hash-chain lock (hashlock)
48 xqm dquot freelist lock (freelistlock
49 mount's dquot list lock (mplistlock)
50 user dquot lock - lock ordering among dquots is based on the uid or gid
51 group dquot lock - similar to udquots. Between the two dquots, the udquot
52 has to be locked first.
53 pin lock - the dquot lock must be held to take this lock.
54 flush lock - ditto.
55*/
56
57STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
58
59#ifdef DEBUG
60xfs_buftarg_t *xfs_dqerror_target;
61int xfs_do_dqerror;
62int xfs_dqreq_num;
63int xfs_dqerror_mod = 33;
64#endif
65
98b8c7a0
CH
66static struct lock_class_key xfs_dquot_other_class;
67
1da177e4
LT
68/*
69 * Allocate and initialize a dquot. We don't always allocate fresh memory;
70 * we try to reclaim a free dquot if the number of incore dquots are above
71 * a threshold.
72 * The only field inside the core that gets initialized at this point
73 * is the d_id field. The idea is to fill in the entire q_core
74 * when we read in the on disk dquot.
75 */
ba0f32d4 76STATIC xfs_dquot_t *
1da177e4
LT
77xfs_qm_dqinit(
78 xfs_mount_t *mp,
79 xfs_dqid_t id,
80 uint type)
81{
82 xfs_dquot_t *dqp;
83 boolean_t brandnewdquot;
84
85 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
86 dqp->dq_flags = type;
1149d96a 87 dqp->q_core.d_id = cpu_to_be32(id);
1da177e4
LT
88 dqp->q_mount = mp;
89
90 /*
91 * No need to re-initialize these if this is a reclaimed dquot.
92 */
93 if (brandnewdquot) {
3a8406f6 94 INIT_LIST_HEAD(&dqp->q_freelist);
794ee1ba 95 mutex_init(&dqp->q_qlock);
bc3048e3 96 init_waitqueue_head(&dqp->q_pinwait);
1da177e4 97
e1f49cf2
DC
98 /*
99 * Because we want to use a counting completion, complete
100 * the flush completion once to allow a single access to
101 * the flush completion without blocking.
102 */
103 init_completion(&dqp->q_flush);
104 complete(&dqp->q_flush);
105
0b1b213f 106 trace_xfs_dqinit(dqp);
1da177e4
LT
107 } else {
108 /*
109 * Only the q_core portion was zeroed in dqreclaim_one().
110 * So, we need to reset others.
111 */
3a8406f6
DC
112 dqp->q_nrefs = 0;
113 dqp->q_blkno = 0;
114 INIT_LIST_HEAD(&dqp->q_mplist);
115 INIT_LIST_HEAD(&dqp->q_hashlist);
116 dqp->q_bufoffset = 0;
117 dqp->q_fileoffset = 0;
118 dqp->q_transp = NULL;
119 dqp->q_gdquot = NULL;
120 dqp->q_res_bcount = 0;
121 dqp->q_res_icount = 0;
122 dqp->q_res_rtbcount = 0;
123 atomic_set(&dqp->q_pincount, 0);
124 dqp->q_hash = NULL;
125 ASSERT(list_empty(&dqp->q_freelist));
1da177e4 126
0b1b213f 127 trace_xfs_dqreuse(dqp);
98b8c7a0
CH
128 }
129
130 /*
131 * In either case we need to make sure group quotas have a different
132 * lock class than user quotas, to make sure lockdep knows we can
133 * locks of one of each at the same time.
134 */
135 if (!(type & XFS_DQ_USER))
136 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
1da177e4
LT
137
138 /*
139 * log item gets initialized later
140 */
141 return (dqp);
142}
143
144/*
145 * This is called to free all the memory associated with a dquot
146 */
147void
148xfs_qm_dqdestroy(
149 xfs_dquot_t *dqp)
150{
3a8406f6 151 ASSERT(list_empty(&dqp->q_freelist));
1da177e4
LT
152
153 mutex_destroy(&dqp->q_qlock);
1da177e4 154 sv_destroy(&dqp->q_pinwait);
1da177e4 155 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
0b1b213f 156
1da177e4
LT
157 atomic_dec(&xfs_Gqm->qm_totaldquots);
158}
159
160/*
161 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
162 */
163STATIC void
164xfs_qm_dqinit_core(
165 xfs_dqid_t id,
166 uint type,
167 xfs_dqblk_t *d)
168{
169 /*
170 * Caller has zero'd the entire dquot 'chunk' already.
171 */
1149d96a
CH
172 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
173 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
174 d->dd_diskdq.d_id = cpu_to_be32(id);
175 d->dd_diskdq.d_flags = type;
1da177e4
LT
176}
177
1da177e4
LT
178/*
179 * If default limits are in force, push them into the dquot now.
180 * We overwrite the dquot limits only if they are zero and this
181 * is not the root dquot.
182 */
183void
184xfs_qm_adjust_dqlimits(
185 xfs_mount_t *mp,
186 xfs_disk_dquot_t *d)
187{
188 xfs_quotainfo_t *q = mp->m_quotainfo;
189
190 ASSERT(d->d_id);
191
192 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
1149d96a 193 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
1da177e4 194 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
1149d96a 195 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
1da177e4 196 if (q->qi_isoftlimit && !d->d_ino_softlimit)
1149d96a 197 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
1da177e4 198 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
1149d96a 199 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
1da177e4 200 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
1149d96a 201 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
1da177e4 202 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
1149d96a 203 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
1da177e4
LT
204}
205
206/*
207 * Check the limits and timers of a dquot and start or reset timers
208 * if necessary.
209 * This gets called even when quota enforcement is OFF, which makes our
210 * life a little less complicated. (We just don't reject any quota
211 * reservations in that case, when enforcement is off).
212 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
213 * enforcement's off.
214 * In contrast, warnings are a little different in that they don't
754002b4
NS
215 * 'automatically' get started when limits get exceeded. They do
216 * get reset to zero, however, when we find the count to be under
217 * the soft limit (they are only ever set non-zero via userspace).
1da177e4
LT
218 */
219void
220xfs_qm_adjust_dqtimers(
221 xfs_mount_t *mp,
222 xfs_disk_dquot_t *d)
223{
224 ASSERT(d->d_id);
225
226#ifdef QUOTADEBUG
1149d96a
CH
227 if (d->d_blk_hardlimit)
228 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
229 be64_to_cpu(d->d_blk_hardlimit));
230 if (d->d_ino_hardlimit)
231 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
232 be64_to_cpu(d->d_ino_hardlimit));
233 if (d->d_rtb_hardlimit)
234 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
235 be64_to_cpu(d->d_rtb_hardlimit));
1da177e4
LT
236#endif
237 if (!d->d_btimer) {
1149d96a
CH
238 if ((d->d_blk_softlimit &&
239 (be64_to_cpu(d->d_bcount) >=
240 be64_to_cpu(d->d_blk_softlimit))) ||
241 (d->d_blk_hardlimit &&
242 (be64_to_cpu(d->d_bcount) >=
243 be64_to_cpu(d->d_blk_hardlimit)))) {
244 d->d_btimer = cpu_to_be32(get_seconds() +
8a7b8a89 245 mp->m_quotainfo->qi_btimelimit);
754002b4
NS
246 } else {
247 d->d_bwarns = 0;
1da177e4
LT
248 }
249 } else {
250 if ((!d->d_blk_softlimit ||
1149d96a
CH
251 (be64_to_cpu(d->d_bcount) <
252 be64_to_cpu(d->d_blk_softlimit))) &&
1da177e4 253 (!d->d_blk_hardlimit ||
1149d96a
CH
254 (be64_to_cpu(d->d_bcount) <
255 be64_to_cpu(d->d_blk_hardlimit)))) {
1da177e4
LT
256 d->d_btimer = 0;
257 }
258 }
259
260 if (!d->d_itimer) {
1149d96a
CH
261 if ((d->d_ino_softlimit &&
262 (be64_to_cpu(d->d_icount) >=
263 be64_to_cpu(d->d_ino_softlimit))) ||
264 (d->d_ino_hardlimit &&
265 (be64_to_cpu(d->d_icount) >=
266 be64_to_cpu(d->d_ino_hardlimit)))) {
267 d->d_itimer = cpu_to_be32(get_seconds() +
8a7b8a89 268 mp->m_quotainfo->qi_itimelimit);
754002b4
NS
269 } else {
270 d->d_iwarns = 0;
1da177e4
LT
271 }
272 } else {
273 if ((!d->d_ino_softlimit ||
1149d96a
CH
274 (be64_to_cpu(d->d_icount) <
275 be64_to_cpu(d->d_ino_softlimit))) &&
1da177e4 276 (!d->d_ino_hardlimit ||
1149d96a
CH
277 (be64_to_cpu(d->d_icount) <
278 be64_to_cpu(d->d_ino_hardlimit)))) {
1da177e4
LT
279 d->d_itimer = 0;
280 }
281 }
282
283 if (!d->d_rtbtimer) {
1149d96a
CH
284 if ((d->d_rtb_softlimit &&
285 (be64_to_cpu(d->d_rtbcount) >=
286 be64_to_cpu(d->d_rtb_softlimit))) ||
287 (d->d_rtb_hardlimit &&
288 (be64_to_cpu(d->d_rtbcount) >=
289 be64_to_cpu(d->d_rtb_hardlimit)))) {
290 d->d_rtbtimer = cpu_to_be32(get_seconds() +
8a7b8a89 291 mp->m_quotainfo->qi_rtbtimelimit);
754002b4
NS
292 } else {
293 d->d_rtbwarns = 0;
1da177e4
LT
294 }
295 } else {
296 if ((!d->d_rtb_softlimit ||
1149d96a
CH
297 (be64_to_cpu(d->d_rtbcount) <
298 be64_to_cpu(d->d_rtb_softlimit))) &&
1da177e4 299 (!d->d_rtb_hardlimit ||
1149d96a
CH
300 (be64_to_cpu(d->d_rtbcount) <
301 be64_to_cpu(d->d_rtb_hardlimit)))) {
1da177e4
LT
302 d->d_rtbtimer = 0;
303 }
304 }
305}
306
1da177e4
LT
307/*
308 * initialize a buffer full of dquots and log the whole thing
309 */
310STATIC void
311xfs_qm_init_dquot_blk(
312 xfs_trans_t *tp,
313 xfs_mount_t *mp,
314 xfs_dqid_t id,
315 uint type,
316 xfs_buf_t *bp)
317{
8a7b8a89 318 struct xfs_quotainfo *q = mp->m_quotainfo;
1da177e4
LT
319 xfs_dqblk_t *d;
320 int curid, i;
321
322 ASSERT(tp);
323 ASSERT(XFS_BUF_ISBUSY(bp));
324 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
325
326 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
327
328 /*
329 * ID of the first dquot in the block - id's are zero based.
330 */
8a7b8a89 331 curid = id - (id % q->qi_dqperchunk);
1da177e4 332 ASSERT(curid >= 0);
8a7b8a89
CH
333 memset(d, 0, BBTOB(q->qi_dqchunklen));
334 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
1da177e4
LT
335 xfs_qm_dqinit_core(curid, type, d);
336 xfs_trans_dquot_buf(tp, bp,
c1155410
DC
337 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
338 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
339 XFS_BLF_GDQUOT_BUF)));
8a7b8a89 340 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
1da177e4
LT
341}
342
343
344
345/*
346 * Allocate a block and fill it with dquots.
347 * This is called when the bmapi finds a hole.
348 */
349STATIC int
350xfs_qm_dqalloc(
efa092f3 351 xfs_trans_t **tpp,
1da177e4
LT
352 xfs_mount_t *mp,
353 xfs_dquot_t *dqp,
354 xfs_inode_t *quotip,
355 xfs_fileoff_t offset_fsb,
356 xfs_buf_t **O_bpp)
357{
358 xfs_fsblock_t firstblock;
359 xfs_bmap_free_t flist;
360 xfs_bmbt_irec_t map;
361 int nmaps, error, committed;
362 xfs_buf_t *bp;
efa092f3 363 xfs_trans_t *tp = *tpp;
1da177e4
LT
364
365 ASSERT(tp != NULL);
0b1b213f
CH
366
367 trace_xfs_dqalloc(dqp);
1da177e4
LT
368
369 /*
370 * Initialize the bmap freelist prior to calling bmapi code.
371 */
9d87c319 372 xfs_bmap_init(&flist, &firstblock);
1da177e4
LT
373 xfs_ilock(quotip, XFS_ILOCK_EXCL);
374 /*
375 * Return if this type of quotas is turned off while we didn't
376 * have an inode lock
377 */
378 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
379 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
380 return (ESRCH);
381 }
382
383 /*
384 * xfs_trans_commit normally decrements the vnode ref count
385 * when it unlocks the inode. Since we want to keep the quota
386 * inode around, we bump the vnode ref count now.
387 */
0b1f9177 388 IHOLD(quotip);
1da177e4
LT
389
390 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
391 nmaps = 1;
392 if ((error = xfs_bmapi(tp, quotip,
393 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
394 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
395 &firstblock,
396 XFS_QM_DQALLOC_SPACE_RES(mp),
3e57ecf6 397 &map, &nmaps, &flist, NULL))) {
1da177e4
LT
398 goto error0;
399 }
400 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
401 ASSERT(nmaps == 1);
402 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
403 (map.br_startblock != HOLESTARTBLOCK));
404
405 /*
406 * Keep track of the blkno to save a lookup later
407 */
408 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
409
410 /* now we can just get the buffer (there's nothing to read yet) */
411 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
412 dqp->q_blkno,
8a7b8a89 413 mp->m_quotainfo->qi_dqchunklen,
1da177e4
LT
414 0);
415 if (!bp || (error = XFS_BUF_GETERROR(bp)))
416 goto error1;
417 /*
418 * Make a chunk of dquots out of this buffer and log
419 * the entire thing.
420 */
1149d96a 421 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
c8ad20ff 422 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
1da177e4 423
efa092f3
TS
424 /*
425 * xfs_bmap_finish() may commit the current transaction and
426 * start a second transaction if the freelist is not empty.
427 *
428 * Since we still want to modify this buffer, we need to
429 * ensure that the buffer is not released on commit of
430 * the first transaction and ensure the buffer is added to the
431 * second transaction.
432 *
433 * If there is only one transaction then don't stop the buffer
434 * from being released when it commits later on.
435 */
436
437 xfs_trans_bhold(tp, bp);
438
f7c99b6f 439 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
1da177e4
LT
440 goto error1;
441 }
442
efa092f3
TS
443 if (committed) {
444 tp = *tpp;
445 xfs_trans_bjoin(tp, bp);
446 } else {
447 xfs_trans_bhold_release(tp, bp);
448 }
449
1da177e4
LT
450 *O_bpp = bp;
451 return 0;
452
453 error1:
454 xfs_bmap_cancel(&flist);
455 error0:
456 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
457
458 return (error);
459}
460
461/*
462 * Maps a dquot to the buffer containing its on-disk version.
463 * This returns a ptr to the buffer containing the on-disk dquot
464 * in the bpp param, and a ptr to the on-disk dquot within that buffer
465 */
466STATIC int
467xfs_qm_dqtobp(
efa092f3 468 xfs_trans_t **tpp,
1da177e4
LT
469 xfs_dquot_t *dqp,
470 xfs_disk_dquot_t **O_ddpp,
471 xfs_buf_t **O_bpp,
472 uint flags)
473{
474 xfs_bmbt_irec_t map;
475 int nmaps, error;
476 xfs_buf_t *bp;
477 xfs_inode_t *quotip;
478 xfs_mount_t *mp;
479 xfs_disk_dquot_t *ddq;
480 xfs_dqid_t id;
481 boolean_t newdquot;
efa092f3 482 xfs_trans_t *tp = (tpp ? *tpp : NULL);
1da177e4
LT
483
484 mp = dqp->q_mount;
1149d96a 485 id = be32_to_cpu(dqp->q_core.d_id);
1da177e4
LT
486 nmaps = 1;
487 newdquot = B_FALSE;
488
489 /*
490 * If we don't know where the dquot lives, find out.
491 */
492 if (dqp->q_blkno == (xfs_daddr_t) 0) {
493 /* We use the id as an index */
8a7b8a89
CH
494 dqp->q_fileoffset = (xfs_fileoff_t)id /
495 mp->m_quotainfo->qi_dqperchunk;
1da177e4
LT
496 nmaps = 1;
497 quotip = XFS_DQ_TO_QIP(dqp);
498 xfs_ilock(quotip, XFS_ILOCK_SHARED);
499 /*
500 * Return if this type of quotas is turned off while we didn't
501 * have an inode lock
502 */
503 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
504 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
505 return (ESRCH);
506 }
507 /*
508 * Find the block map; no allocations yet
509 */
510 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
511 XFS_DQUOT_CLUSTER_SIZE_FSB,
512 XFS_BMAPI_METADATA,
3e57ecf6 513 NULL, 0, &map, &nmaps, NULL, NULL);
1da177e4
LT
514
515 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
516 if (error)
517 return (error);
518 ASSERT(nmaps == 1);
519 ASSERT(map.br_blockcount == 1);
520
521 /*
522 * offset of dquot in the (fixed sized) dquot chunk.
523 */
8a7b8a89 524 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
1da177e4
LT
525 sizeof(xfs_dqblk_t);
526 if (map.br_startblock == HOLESTARTBLOCK) {
527 /*
528 * We don't allocate unless we're asked to
529 */
530 if (!(flags & XFS_QMOPT_DQALLOC))
531 return (ENOENT);
532
533 ASSERT(tp);
efa092f3 534 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
1da177e4
LT
535 dqp->q_fileoffset, &bp)))
536 return (error);
efa092f3 537 tp = *tpp;
1da177e4
LT
538 newdquot = B_TRUE;
539 } else {
540 /*
541 * store the blkno etc so that we don't have to do the
542 * mapping all the time
543 */
544 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
545 }
546 }
547 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
548 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
549
550 /*
551 * Read in the buffer, unless we've just done the allocation
552 * (in which case we already have the buf).
553 */
8a7b8a89 554 if (!newdquot) {
0b1b213f
CH
555 trace_xfs_dqtobp_read(dqp);
556
8a7b8a89
CH
557 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
558 dqp->q_blkno,
559 mp->m_quotainfo->qi_dqchunklen,
560 0, &bp);
1da177e4
LT
561 if (error || !bp)
562 return XFS_ERROR(error);
563 }
564 ASSERT(XFS_BUF_ISBUSY(bp));
565 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
566
567 /*
568 * calculate the location of the dquot inside the buffer.
569 */
570 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
571
572 /*
573 * A simple sanity check in case we got a corrupted dquot...
574 */
c8ad20ff 575 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
1da177e4
LT
576 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
577 "dqtobp")) {
578 if (!(flags & XFS_QMOPT_DQREPAIR)) {
579 xfs_trans_brelse(tp, bp);
580 return XFS_ERROR(EIO);
581 }
582 XFS_BUF_BUSY(bp); /* We dirtied this */
583 }
584
585 *O_bpp = bp;
586 *O_ddpp = ddq;
587
588 return (0);
589}
590
591
592/*
593 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
594 * and release the buffer immediately.
595 *
596 */
597/* ARGSUSED */
598STATIC int
599xfs_qm_dqread(
efa092f3 600 xfs_trans_t **tpp,
1da177e4
LT
601 xfs_dqid_t id,
602 xfs_dquot_t *dqp, /* dquot to get filled in */
603 uint flags)
604{
605 xfs_disk_dquot_t *ddqp;
606 xfs_buf_t *bp;
607 int error;
efa092f3
TS
608 xfs_trans_t *tp;
609
610 ASSERT(tpp);
1da177e4 611
0b1b213f
CH
612 trace_xfs_dqread(dqp);
613
1da177e4
LT
614 /*
615 * get a pointer to the on-disk dquot and the buffer containing it
616 * dqp already knows its own type (GROUP/USER).
617 */
efa092f3 618 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
1da177e4
LT
619 return (error);
620 }
efa092f3 621 tp = *tpp;
1da177e4
LT
622
623 /* copy everything from disk dquot to the incore dquot */
624 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
1149d96a 625 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
1da177e4
LT
626 xfs_qm_dquot_logitem_init(dqp);
627
628 /*
629 * Reservation counters are defined as reservation plus current usage
630 * to avoid having to add everytime.
631 */
1149d96a
CH
632 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
633 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
634 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
1da177e4
LT
635
636 /* Mark the buf so that this will stay incore a little longer */
637 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
638
639 /*
640 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
641 * So we need to release with xfs_trans_brelse().
642 * The strategy here is identical to that of inodes; we lock
643 * the dquot in xfs_qm_dqget() before making it accessible to
644 * others. This is because dquots, like inodes, need a good level of
645 * concurrency, and we don't want to take locks on the entire buffers
646 * for dquot accesses.
647 * Note also that the dquot buffer may even be dirty at this point, if
648 * this particular dquot was repaired. We still aren't afraid to
649 * brelse it because we have the changes incore.
650 */
651 ASSERT(XFS_BUF_ISBUSY(bp));
652 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
653 xfs_trans_brelse(tp, bp);
654
655 return (error);
656}
657
658
659/*
660 * allocate an incore dquot from the kernel heap,
661 * and fill its core with quota information kept on disk.
662 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
663 * if it wasn't already allocated.
664 */
665STATIC int
666xfs_qm_idtodq(
667 xfs_mount_t *mp,
668 xfs_dqid_t id, /* gid or uid, depending on type */
669 uint type, /* UDQUOT or GDQUOT */
670 uint flags, /* DQALLOC, DQREPAIR */
671 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
672{
673 xfs_dquot_t *dqp;
674 int error;
675 xfs_trans_t *tp;
676 int cancelflags=0;
677
678 dqp = xfs_qm_dqinit(mp, id, type);
679 tp = NULL;
680 if (flags & XFS_QMOPT_DQALLOC) {
681 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
8a7b8a89
CH
682 error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
683 XFS_WRITE_LOG_RES(mp) +
684 BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
685 128,
686 0,
687 XFS_TRANS_PERM_LOG_RES,
688 XFS_WRITE_LOG_COUNT);
689 if (error) {
1da177e4
LT
690 cancelflags = 0;
691 goto error0;
692 }
693 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
694 }
695
696 /*
697 * Read it from disk; xfs_dqread() takes care of
698 * all the necessary initialization of dquot's fields (locks, etc)
699 */
efa092f3 700 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
1da177e4
LT
701 /*
702 * This can happen if quotas got turned off (ESRCH),
703 * or if the dquot didn't exist on disk and we ask to
704 * allocate (ENOENT).
705 */
0b1b213f 706 trace_xfs_dqread_fail(dqp);
1da177e4
LT
707 cancelflags |= XFS_TRANS_ABORT;
708 goto error0;
709 }
710 if (tp) {
1c72bf90 711 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
1da177e4
LT
712 goto error1;
713 }
714
715 *O_dqpp = dqp;
716 return (0);
717
718 error0:
719 ASSERT(error);
720 if (tp)
721 xfs_trans_cancel(tp, cancelflags);
722 error1:
723 xfs_qm_dqdestroy(dqp);
724 *O_dqpp = NULL;
725 return (error);
726}
727
728/*
729 * Lookup a dquot in the incore dquot hashtable. We keep two separate
730 * hashtables for user and group dquots; and, these are global tables
731 * inside the XQM, not per-filesystem tables.
732 * The hash chain must be locked by caller, and it is left locked
733 * on return. Returning dquot is locked.
734 */
735STATIC int
736xfs_qm_dqlookup(
737 xfs_mount_t *mp,
738 xfs_dqid_t id,
739 xfs_dqhash_t *qh,
740 xfs_dquot_t **O_dqpp)
741{
742 xfs_dquot_t *dqp;
743 uint flist_locked;
1da177e4 744
c9a192dc 745 ASSERT(mutex_is_locked(&qh->qh_lock));
1da177e4
LT
746
747 flist_locked = B_FALSE;
748
749 /*
750 * Traverse the hashchain looking for a match
751 */
e6a81f13 752 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
1da177e4
LT
753 /*
754 * We already have the hashlock. We don't need the
755 * dqlock to look at the id field of the dquot, since the
756 * id can't be modified without the hashlock anyway.
757 */
1149d96a 758 if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
0b1b213f
CH
759 trace_xfs_dqlookup_found(dqp);
760
1da177e4
LT
761 /*
762 * All in core dquots must be on the dqlist of mp
763 */
3a25404b 764 ASSERT(!list_empty(&dqp->q_mplist));
1da177e4
LT
765
766 xfs_dqlock(dqp);
767 if (dqp->q_nrefs == 0) {
3a8406f6
DC
768 ASSERT(!list_empty(&dqp->q_freelist));
769 if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
0b1b213f 770 trace_xfs_dqlookup_want(dqp);
1da177e4
LT
771
772 /*
773 * We may have raced with dqreclaim_one()
774 * (and lost). So, flag that we don't
775 * want the dquot to be reclaimed.
776 */
777 dqp->dq_flags |= XFS_DQ_WANT;
778 xfs_dqunlock(dqp);
3a8406f6 779 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
1da177e4
LT
780 xfs_dqlock(dqp);
781 dqp->dq_flags &= ~(XFS_DQ_WANT);
782 }
783 flist_locked = B_TRUE;
784 }
785
786 /*
787 * id couldn't have changed; we had the hashlock all
788 * along
789 */
1149d96a 790 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
1da177e4
LT
791
792 if (flist_locked) {
793 if (dqp->q_nrefs != 0) {
3a8406f6 794 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
1da177e4
LT
795 flist_locked = B_FALSE;
796 } else {
3a8406f6 797 /* take it off the freelist */
0b1b213f 798 trace_xfs_dqlookup_freelist(dqp);
3a8406f6
DC
799 list_del_init(&dqp->q_freelist);
800 xfs_Gqm->qm_dqfrlist_cnt--;
1da177e4
LT
801 }
802 }
803
1da177e4
LT
804 XFS_DQHOLD(dqp);
805
806 if (flist_locked)
3a8406f6 807 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
1da177e4
LT
808 /*
809 * move the dquot to the front of the hashchain
810 */
c9a192dc 811 ASSERT(mutex_is_locked(&qh->qh_lock));
e6a81f13 812 list_move(&dqp->q_hashlist, &qh->qh_list);
0b1b213f 813 trace_xfs_dqlookup_done(dqp);
1da177e4 814 *O_dqpp = dqp;
e6a81f13 815 return 0;
1da177e4
LT
816 }
817 }
818
819 *O_dqpp = NULL;
c9a192dc 820 ASSERT(mutex_is_locked(&qh->qh_lock));
1da177e4
LT
821 return (1);
822}
823
824/*
825 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
826 * a locked dquot, doing an allocation (if requested) as needed.
827 * When both an inode and an id are given, the inode's id takes precedence.
828 * That is, if the id changes while we don't hold the ilock inside this
829 * function, the new dquot is returned, not necessarily the one requested
830 * in the id argument.
831 */
832int
833xfs_qm_dqget(
834 xfs_mount_t *mp,
835 xfs_inode_t *ip, /* locked inode (optional) */
c8ad20ff
NS
836 xfs_dqid_t id, /* uid/projid/gid depending on type */
837 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
1da177e4
LT
838 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
839 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
840{
841 xfs_dquot_t *dqp;
842 xfs_dqhash_t *h;
843 uint version;
844 int error;
845
846 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
847 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
c8ad20ff 848 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
1da177e4
LT
849 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
850 return (ESRCH);
851 }
852 h = XFS_DQ_HASH(mp, id, type);
853
854#ifdef DEBUG
855 if (xfs_do_dqerror) {
856 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
857 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
858 cmn_err(CE_DEBUG, "Returning error in dqget");
859 return (EIO);
860 }
861 }
862#endif
863
864 again:
865
866#ifdef DEBUG
c8ad20ff
NS
867 ASSERT(type == XFS_DQ_USER ||
868 type == XFS_DQ_PROJ ||
869 type == XFS_DQ_GROUP);
1da177e4 870 if (ip) {
579aa9ca 871 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
872 if (type == XFS_DQ_USER)
873 ASSERT(ip->i_udquot == NULL);
874 else
875 ASSERT(ip->i_gdquot == NULL);
876 }
877#endif
c9a192dc 878 mutex_lock(&h->qh_lock);
1da177e4
LT
879
880 /*
881 * Look in the cache (hashtable).
882 * The chain is kept locked during lookup.
883 */
884 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
885 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
886 /*
887 * The dquot was found, moved to the front of the chain,
888 * taken off the freelist if it was on it, and locked
889 * at this point. Just unlock the hashchain and return.
890 */
891 ASSERT(*O_dqpp);
892 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
c9a192dc 893 mutex_unlock(&h->qh_lock);
0b1b213f 894 trace_xfs_dqget_hit(*O_dqpp);
1da177e4
LT
895 return (0); /* success */
896 }
897 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
898
899 /*
900 * Dquot cache miss. We don't want to keep the inode lock across
901 * a (potential) disk read. Also we don't want to deal with the lock
902 * ordering between quotainode and this inode. OTOH, dropping the inode
903 * lock here means dealing with a chown that can happen before
904 * we re-acquire the lock.
905 */
906 if (ip)
907 xfs_iunlock(ip, XFS_ILOCK_EXCL);
908 /*
909 * Save the hashchain version stamp, and unlock the chain, so that
910 * we don't keep the lock across a disk read
911 */
912 version = h->qh_version;
c9a192dc 913 mutex_unlock(&h->qh_lock);
1da177e4
LT
914
915 /*
916 * Allocate the dquot on the kernel heap, and read the ondisk
917 * portion off the disk. Also, do all the necessary initialization
918 * This can return ENOENT if dquot didn't exist on disk and we didn't
919 * ask it to allocate; ESRCH if quotas got turned off suddenly.
920 */
921 if ((error = xfs_qm_idtodq(mp, id, type,
922 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
923 XFS_QMOPT_DOWARN),
924 &dqp))) {
925 if (ip)
926 xfs_ilock(ip, XFS_ILOCK_EXCL);
927 return (error);
928 }
929
930 /*
931 * See if this is mount code calling to look at the overall quota limits
932 * which are stored in the id == 0 user or group's dquot.
933 * Since we may not have done a quotacheck by this point, just return
934 * the dquot without attaching it to any hashtables, lists, etc, or even
935 * taking a reference.
936 * The caller must dqdestroy this once done.
937 */
938 if (flags & XFS_QMOPT_DQSUSER) {
939 ASSERT(id == 0);
940 ASSERT(! ip);
941 goto dqret;
942 }
943
944 /*
945 * Dquot lock comes after hashlock in the lock ordering
946 */
947 if (ip) {
948 xfs_ilock(ip, XFS_ILOCK_EXCL);
191f8488 949
1da177e4
LT
950 /*
951 * A dquot could be attached to this inode by now, since
952 * we had dropped the ilock.
953 */
954 if (type == XFS_DQ_USER) {
191f8488
CH
955 if (!XFS_IS_UQUOTA_ON(mp)) {
956 /* inode stays locked on return */
957 xfs_qm_dqdestroy(dqp);
958 return XFS_ERROR(ESRCH);
959 }
1da177e4
LT
960 if (ip->i_udquot) {
961 xfs_qm_dqdestroy(dqp);
962 dqp = ip->i_udquot;
963 xfs_dqlock(dqp);
964 goto dqret;
965 }
966 } else {
191f8488
CH
967 if (!XFS_IS_OQUOTA_ON(mp)) {
968 /* inode stays locked on return */
969 xfs_qm_dqdestroy(dqp);
970 return XFS_ERROR(ESRCH);
971 }
1da177e4
LT
972 if (ip->i_gdquot) {
973 xfs_qm_dqdestroy(dqp);
974 dqp = ip->i_gdquot;
975 xfs_dqlock(dqp);
976 goto dqret;
977 }
978 }
979 }
980
981 /*
982 * Hashlock comes after ilock in lock order
983 */
c9a192dc 984 mutex_lock(&h->qh_lock);
1da177e4
LT
985 if (version != h->qh_version) {
986 xfs_dquot_t *tmpdqp;
987 /*
988 * Now, see if somebody else put the dquot in the
989 * hashtable before us. This can happen because we didn't
990 * keep the hashchain lock. We don't have to worry about
991 * lock order between the two dquots here since dqp isn't
992 * on any findable lists yet.
993 */
994 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
995 /*
996 * Duplicate found. Just throw away the new dquot
997 * and start over.
998 */
999 xfs_qm_dqput(tmpdqp);
c9a192dc 1000 mutex_unlock(&h->qh_lock);
1da177e4
LT
1001 xfs_qm_dqdestroy(dqp);
1002 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1003 goto again;
1004 }
1005 }
1006
1007 /*
1008 * Put the dquot at the beginning of the hash-chain and mp's list
1009 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1010 */
c9a192dc 1011 ASSERT(mutex_is_locked(&h->qh_lock));
1da177e4 1012 dqp->q_hash = h;
e6a81f13
DC
1013 list_add(&dqp->q_hashlist, &h->qh_list);
1014 h->qh_version++;
1da177e4
LT
1015
1016 /*
1017 * Attach this dquot to this filesystem's list of all dquots,
1018 * kept inside the mount structure in m_quotainfo field
1019 */
3a25404b 1020 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
1da177e4
LT
1021
1022 /*
1023 * We return a locked dquot to the caller, with a reference taken
1024 */
1025 xfs_dqlock(dqp);
1026 dqp->q_nrefs = 1;
1027
3a25404b
DC
1028 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
1029 mp->m_quotainfo->qi_dquots++;
1030 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
c9a192dc 1031 mutex_unlock(&h->qh_lock);
1da177e4 1032 dqret:
579aa9ca 1033 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
0b1b213f 1034 trace_xfs_dqget_miss(dqp);
1da177e4
LT
1035 *O_dqpp = dqp;
1036 return (0);
1037}
1038
1039
1040/*
1041 * Release a reference to the dquot (decrement ref-count)
1042 * and unlock it. If there is a group quota attached to this
1043 * dquot, carefully release that too without tripping over
1044 * deadlocks'n'stuff.
1045 */
1046void
1047xfs_qm_dqput(
1048 xfs_dquot_t *dqp)
1049{
1050 xfs_dquot_t *gdqp;
1051
1052 ASSERT(dqp->q_nrefs > 0);
1053 ASSERT(XFS_DQ_IS_LOCKED(dqp));
0b1b213f
CH
1054
1055 trace_xfs_dqput(dqp);
1da177e4
LT
1056
1057 if (dqp->q_nrefs != 1) {
1058 dqp->q_nrefs--;
1059 xfs_dqunlock(dqp);
1060 return;
1061 }
1062
1063 /*
1064 * drop the dqlock and acquire the freelist and dqlock
1065 * in the right order; but try to get it out-of-order first
1066 */
3a8406f6 1067 if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
0b1b213f 1068 trace_xfs_dqput_wait(dqp);
1da177e4 1069 xfs_dqunlock(dqp);
3a8406f6 1070 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
1da177e4
LT
1071 xfs_dqlock(dqp);
1072 }
1073
1074 while (1) {
1075 gdqp = NULL;
1076
1077 /* We can't depend on nrefs being == 1 here */
1078 if (--dqp->q_nrefs == 0) {
0b1b213f
CH
1079 trace_xfs_dqput_free(dqp);
1080
3a8406f6
DC
1081 list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
1082 xfs_Gqm->qm_dqfrlist_cnt++;
1da177e4
LT
1083
1084 /*
1085 * If we just added a udquot to the freelist, then
1086 * we want to release the gdquot reference that
1087 * it (probably) has. Otherwise it'll keep the
1088 * gdquot from getting reclaimed.
1089 */
1090 if ((gdqp = dqp->q_gdquot)) {
1091 /*
1092 * Avoid a recursive dqput call
1093 */
1094 xfs_dqlock(gdqp);
1095 dqp->q_gdquot = NULL;
1096 }
1da177e4
LT
1097 }
1098 xfs_dqunlock(dqp);
1099
1100 /*
1101 * If we had a group quota inside the user quota as a hint,
1102 * release it now.
1103 */
1104 if (! gdqp)
1105 break;
1106 dqp = gdqp;
1107 }
3a8406f6 1108 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
1da177e4
LT
1109}
1110
1111/*
1112 * Release a dquot. Flush it if dirty, then dqput() it.
1113 * dquot must not be locked.
1114 */
1115void
1116xfs_qm_dqrele(
1117 xfs_dquot_t *dqp)
1118{
7d095257
CH
1119 if (!dqp)
1120 return;
1121
0b1b213f 1122 trace_xfs_dqrele(dqp);
1da177e4
LT
1123
1124 xfs_dqlock(dqp);
1125 /*
1126 * We don't care to flush it if the dquot is dirty here.
1127 * That will create stutters that we want to avoid.
1128 * Instead we do a delayed write when we try to reclaim
1129 * a dirty dquot. Also xfs_sync will take part of the burden...
1130 */
1131 xfs_qm_dqput(dqp);
1132}
1133
1134
1135/*
1136 * Write a modified dquot to disk.
1137 * The dquot must be locked and the flush lock too taken by caller.
1138 * The flush lock will not be unlocked until the dquot reaches the disk,
1139 * but the dquot is free to be unlocked and modified by the caller
1140 * in the interim. Dquot is still locked on return. This behavior is
1141 * identical to that of inodes.
1142 */
1143int
1144xfs_qm_dqflush(
1145 xfs_dquot_t *dqp,
1146 uint flags)
1147{
1148 xfs_mount_t *mp;
1149 xfs_buf_t *bp;
1150 xfs_disk_dquot_t *ddqp;
1151 int error;
1da177e4
LT
1152
1153 ASSERT(XFS_DQ_IS_LOCKED(dqp));
e1f49cf2 1154 ASSERT(!completion_done(&dqp->q_flush));
0b1b213f 1155 trace_xfs_dqflush(dqp);
1da177e4
LT
1156
1157 /*
2f8a3ce1
DC
1158 * If not dirty, or it's pinned and we are not supposed to
1159 * block, nada.
1da177e4 1160 */
2f8a3ce1 1161 if (!XFS_DQ_IS_DIRTY(dqp) ||
20026d92 1162 (!(flags & SYNC_WAIT) && atomic_read(&dqp->q_pincount) > 0)) {
1da177e4 1163 xfs_dqfunlock(dqp);
2f8a3ce1 1164 return 0;
1da177e4 1165 }
1da177e4
LT
1166 xfs_qm_dqunpin_wait(dqp);
1167
1168 /*
1169 * This may have been unpinned because the filesystem is shutting
1170 * down forcibly. If that's the case we must not write this dquot
1171 * to disk, because the log record didn't make it to disk!
1172 */
1173 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1174 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1175 xfs_dqfunlock(dqp);
1176 return XFS_ERROR(EIO);
1177 }
1178
1179 /*
1180 * Get the buffer containing the on-disk dquot
1181 * We don't need a transaction envelope because we know that the
1182 * the ondisk-dquot has already been allocated for.
1183 */
1184 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1da177e4
LT
1185 ASSERT(error != ENOENT);
1186 /*
1187 * Quotas could have gotten turned off (ESRCH)
1188 */
1189 xfs_dqfunlock(dqp);
1190 return (error);
1191 }
1192
1149d96a 1193 if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
c8ad20ff 1194 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
7d04a335 1195 xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
1da177e4
LT
1196 return XFS_ERROR(EIO);
1197 }
1198
1199 /* This is the only portion of data that needs to persist */
1200 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1201
1202 /*
1203 * Clear the dirty field and remember the flush lsn for later use.
1204 */
1205 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1206 mp = dqp->q_mount;
1207
7b2e2a31
DC
1208 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1209 &dqp->q_logitem.qli_item.li_lsn);
1da177e4
LT
1210
1211 /*
1212 * Attach an iodone routine so that we can remove this dquot from the
1213 * AIL and release the flush lock once the dquot is synced to disk.
1214 */
1215 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1216 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1217 /*
1218 * If the buffer is pinned then push on the log so we won't
1219 * get stuck waiting in the write for too long.
1220 */
1221 if (XFS_BUF_ISPINNED(bp)) {
0b1b213f 1222 trace_xfs_dqflush_force(dqp);
a14a348b 1223 xfs_log_force(mp, 0);
1da177e4
LT
1224 }
1225
20026d92 1226 if (flags & SYNC_WAIT)
1da177e4 1227 error = xfs_bwrite(mp, bp);
20026d92
DC
1228 else
1229 xfs_bdwrite(mp, bp);
0b1b213f
CH
1230
1231 trace_xfs_dqflush_done(dqp);
1232
1da177e4
LT
1233 /*
1234 * dqp is still locked, but caller is free to unlock it now.
1235 */
20026d92 1236 return error;
1da177e4
LT
1237
1238}
1239
1240/*
1241 * This is the dquot flushing I/O completion routine. It is called
1242 * from interrupt level when the buffer containing the dquot is
1243 * flushed to disk. It is responsible for removing the dquot logitem
1244 * from the AIL if it has not been re-logged, and unlocking the dquot's
1245 * flush lock. This behavior is very similar to that of inodes..
1246 */
1247/*ARGSUSED*/
1248STATIC void
1249xfs_qm_dqflush_done(
1250 xfs_buf_t *bp,
1251 xfs_dq_logitem_t *qip)
1252{
1253 xfs_dquot_t *dqp;
783a2f65 1254 struct xfs_ail *ailp;
1da177e4
LT
1255
1256 dqp = qip->qli_dquot;
783a2f65 1257 ailp = qip->qli_item.li_ailp;
1da177e4
LT
1258
1259 /*
1260 * We only want to pull the item from the AIL if its
1261 * location in the log has not changed since we started the flush.
1262 * Thus, we only bother if the dquot's lsn has
1263 * not changed. First we check the lsn outside the lock
1264 * since it's cheaper, and then we recheck while
1265 * holding the lock before removing the dquot from the AIL.
1266 */
1267 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1268 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1269
783a2f65
DC
1270 /* xfs_trans_ail_delete() drops the AIL lock. */
1271 spin_lock(&ailp->xa_lock);
1da177e4 1272 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
783a2f65 1273 xfs_trans_ail_delete(ailp, (xfs_log_item_t*)qip);
1da177e4 1274 else
783a2f65 1275 spin_unlock(&ailp->xa_lock);
1da177e4
LT
1276 }
1277
1278 /*
1279 * Release the dq's flush lock since we're done with it.
1280 */
1281 xfs_dqfunlock(dqp);
1282}
1283
1da177e4
LT
1284int
1285xfs_qm_dqlock_nowait(
1286 xfs_dquot_t *dqp)
1287{
e1f49cf2 1288 return mutex_trylock(&dqp->q_qlock);
1da177e4
LT
1289}
1290
1291void
1292xfs_dqlock(
1293 xfs_dquot_t *dqp)
1294{
e1f49cf2 1295 mutex_lock(&dqp->q_qlock);
1da177e4
LT
1296}
1297
1298void
1299xfs_dqunlock(
1300 xfs_dquot_t *dqp)
1301{
1302 mutex_unlock(&(dqp->q_qlock));
1303 if (dqp->q_logitem.qli_dquot == dqp) {
1304 /* Once was dqp->q_mount, but might just have been cleared */
783a2f65 1305 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
1da177e4
LT
1306 (xfs_log_item_t*)&(dqp->q_logitem));
1307 }
1308}
1309
1310
1311void
1312xfs_dqunlock_nonotify(
1313 xfs_dquot_t *dqp)
1314{
1315 mutex_unlock(&(dqp->q_qlock));
1316}
1317
5bb87a33
CH
1318/*
1319 * Lock two xfs_dquot structures.
1320 *
1321 * To avoid deadlocks we always lock the quota structure with
1322 * the lowerd id first.
1323 */
1da177e4
LT
1324void
1325xfs_dqlock2(
1326 xfs_dquot_t *d1,
1327 xfs_dquot_t *d2)
1328{
1329 if (d1 && d2) {
1330 ASSERT(d1 != d2);
1149d96a
CH
1331 if (be32_to_cpu(d1->q_core.d_id) >
1332 be32_to_cpu(d2->q_core.d_id)) {
5bb87a33
CH
1333 mutex_lock(&d2->q_qlock);
1334 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1335 } else {
5bb87a33
CH
1336 mutex_lock(&d1->q_qlock);
1337 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1338 }
5bb87a33
CH
1339 } else if (d1) {
1340 mutex_lock(&d1->q_qlock);
1341 } else if (d2) {
1342 mutex_lock(&d2->q_qlock);
1da177e4
LT
1343 }
1344}
1345
1346
1347/*
1348 * Take a dquot out of the mount's dqlist as well as the hashlist.
1349 * This is called via unmount as well as quotaoff, and the purge
1350 * will always succeed unless there are soft (temp) references
1351 * outstanding.
1352 *
1353 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1354 * that we're returning! XXXsup - not cool.
1355 */
1356/* ARGSUSED */
1357int
1358xfs_qm_dqpurge(
4f0e8a98 1359 xfs_dquot_t *dqp)
1da177e4 1360{
e6a81f13 1361 xfs_dqhash_t *qh = dqp->q_hash;
3c56836f 1362 xfs_mount_t *mp = dqp->q_mount;
1da177e4 1363
3a25404b 1364 ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
c9a192dc 1365 ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
1da177e4
LT
1366
1367 xfs_dqlock(dqp);
1368 /*
1369 * We really can't afford to purge a dquot that is
1370 * referenced, because these are hard refs.
1371 * It shouldn't happen in general because we went thru _all_ inodes in
1372 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1373 * However it is possible that we have dquots with temporary
1374 * references that are not attached to an inode. e.g. see xfs_setattr().
1375 */
1376 if (dqp->q_nrefs != 0) {
1377 xfs_dqunlock(dqp);
c9a192dc 1378 mutex_unlock(&dqp->q_hash->qh_lock);
1da177e4
LT
1379 return (1);
1380 }
1381
3a8406f6 1382 ASSERT(!list_empty(&dqp->q_freelist));
1da177e4
LT
1383
1384 /*
1385 * If we're turning off quotas, we have to make sure that, for
1386 * example, we don't delete quota disk blocks while dquots are
1387 * in the process of getting written to those disk blocks.
1388 * This dquot might well be on AIL, and we can't leave it there
1389 * if we're turning off quotas. Basically, we need this flush
1390 * lock, and are willing to block on it.
1391 */
e1f49cf2 1392 if (!xfs_dqflock_nowait(dqp)) {
1da177e4
LT
1393 /*
1394 * Block on the flush lock after nudging dquot buffer,
1395 * if it is incore.
1396 */
1397 xfs_qm_dqflock_pushbuf_wait(dqp);
1398 }
1399
1400 /*
1401 * XXXIf we're turning this type of quotas off, we don't care
1402 * about the dirty metadata sitting in this dquot. OTOH, if
1403 * we're unmounting, we do care, so we flush it and wait.
1404 */
1405 if (XFS_DQ_IS_DIRTY(dqp)) {
3c56836f 1406 int error;
0b1b213f 1407
1da177e4
LT
1408 /* dqflush unlocks dqflock */
1409 /*
1410 * Given that dqpurge is a very rare occurrence, it is OK
1411 * that we're holding the hashlist and mplist locks
1412 * across the disk write. But, ... XXXsup
1413 *
1414 * We don't care about getting disk errors here. We need
1415 * to purge this dquot anyway, so we go ahead regardless.
1416 */
20026d92 1417 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
3c56836f
DC
1418 if (error)
1419 xfs_fs_cmn_err(CE_WARN, mp,
1420 "xfs_qm_dqpurge: dquot %p flush failed", dqp);
1da177e4
LT
1421 xfs_dqflock(dqp);
1422 }
bc3048e3 1423 ASSERT(atomic_read(&dqp->q_pincount) == 0);
1da177e4
LT
1424 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1425 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1426
e6a81f13
DC
1427 list_del_init(&dqp->q_hashlist);
1428 qh->qh_version++;
3a25404b
DC
1429 list_del_init(&dqp->q_mplist);
1430 mp->m_quotainfo->qi_dqreclaims++;
1431 mp->m_quotainfo->qi_dquots--;
1da177e4
LT
1432 /*
1433 * XXX Move this to the front of the freelist, if we can get the
1434 * freelist lock.
1435 */
3a8406f6 1436 ASSERT(!list_empty(&dqp->q_freelist));
1da177e4
LT
1437
1438 dqp->q_mount = NULL;
1439 dqp->q_hash = NULL;
1440 dqp->dq_flags = XFS_DQ_INACTIVE;
1441 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1442 xfs_dqfunlock(dqp);
1443 xfs_dqunlock(dqp);
e6a81f13 1444 mutex_unlock(&qh->qh_lock);
1da177e4
LT
1445 return (0);
1446}
1447
1448
1449#ifdef QUOTADEBUG
1450void
1451xfs_qm_dqprint(xfs_dquot_t *dqp)
1452{
1453 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1454 cmn_err(CE_DEBUG, "---- dquotID = %d",
1149d96a 1455 (int)be32_to_cpu(dqp->q_core.d_id));
c8ad20ff 1456 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
1da177e4
LT
1457 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1458 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1459 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1460 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
1149d96a
CH
1461 be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1462 (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
1da177e4 1463 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
1149d96a
CH
1464 be64_to_cpu(dqp->q_core.d_blk_softlimit),
1465 (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
1da177e4 1466 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
1149d96a
CH
1467 be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1468 (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
1da177e4 1469 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
1149d96a
CH
1470 be64_to_cpu(dqp->q_core.d_ino_softlimit),
1471 (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
1da177e4 1472 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1149d96a
CH
1473 be64_to_cpu(dqp->q_core.d_bcount),
1474 (int)be64_to_cpu(dqp->q_core.d_bcount));
1da177e4 1475 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1149d96a
CH
1476 be64_to_cpu(dqp->q_core.d_icount),
1477 (int)be64_to_cpu(dqp->q_core.d_icount));
1da177e4 1478 cmn_err(CE_DEBUG, "---- btimer = %d",
1149d96a 1479 (int)be32_to_cpu(dqp->q_core.d_btimer));
1da177e4 1480 cmn_err(CE_DEBUG, "---- itimer = %d",
1149d96a 1481 (int)be32_to_cpu(dqp->q_core.d_itimer));
1da177e4
LT
1482 cmn_err(CE_DEBUG, "---------------------------");
1483}
1484#endif
1485
1486/*
1487 * Give the buffer a little push if it is incore and
1488 * wait on the flush lock.
1489 */
1490void
1491xfs_qm_dqflock_pushbuf_wait(
1492 xfs_dquot_t *dqp)
1493{
8a7b8a89 1494 xfs_mount_t *mp = dqp->q_mount;
1da177e4
LT
1495 xfs_buf_t *bp;
1496
1497 /*
1498 * Check to see if the dquot has been flushed delayed
1499 * write. If so, grab its buffer and send it
1500 * out immediately. We'll be able to acquire
1501 * the flush lock when the I/O completes.
1502 */
8a7b8a89
CH
1503 bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
1504 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
7d6a7bde
DC
1505 if (!bp)
1506 goto out_lock;
1507
1508 if (XFS_BUF_ISDELAYWRITE(bp)) {
1509 if (XFS_BUF_ISPINNED(bp))
8a7b8a89 1510 xfs_log_force(mp, 0);
7d6a7bde
DC
1511 xfs_buf_delwri_promote(bp);
1512 wake_up_process(bp->b_target->bt_task);
1da177e4 1513 }
7d6a7bde
DC
1514 xfs_buf_relse(bp);
1515out_lock:
1da177e4
LT
1516 xfs_dqflock(dqp);
1517}