xfs: per-filesystem dquot LRU lists
[deliverable/linux.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"
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 41
1da177e4 42/*
bf72de31
CH
43 * Lock order:
44 *
45 * ip->i_lock
46 * qh->qh_lock
47 * qi->qi_dqlist_lock
48 * dquot->q_qlock (xfs_dqlock() and friends)
49 * dquot->q_flush (xfs_dqflock() and friends)
f8739c3c 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
1da177e4
LT
56#ifdef DEBUG
57xfs_buftarg_t *xfs_dqerror_target;
58int xfs_do_dqerror;
59int xfs_dqreq_num;
60int xfs_dqerror_mod = 33;
61#endif
62
98b8c7a0
CH
63static struct lock_class_key xfs_dquot_other_class;
64
1da177e4
LT
65/*
66 * This is called to free all the memory associated with a dquot
67 */
68void
69xfs_qm_dqdestroy(
70 xfs_dquot_t *dqp)
71{
f8739c3c 72 ASSERT(list_empty(&dqp->q_lru));
1da177e4
LT
73
74 mutex_destroy(&dqp->q_qlock);
1da177e4 75 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
0b1b213f 76
48776fd2 77 XFS_STATS_DEC(xs_qm_dquot);
1da177e4
LT
78}
79
1da177e4
LT
80/*
81 * If default limits are in force, push them into the dquot now.
82 * We overwrite the dquot limits only if they are zero and this
83 * is not the root dquot.
84 */
85void
86xfs_qm_adjust_dqlimits(
87 xfs_mount_t *mp,
88 xfs_disk_dquot_t *d)
89{
90 xfs_quotainfo_t *q = mp->m_quotainfo;
91
92 ASSERT(d->d_id);
93
94 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
1149d96a 95 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
1da177e4 96 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
1149d96a 97 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
1da177e4 98 if (q->qi_isoftlimit && !d->d_ino_softlimit)
1149d96a 99 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
1da177e4 100 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
1149d96a 101 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
1da177e4 102 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
1149d96a 103 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
1da177e4 104 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
1149d96a 105 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
1da177e4
LT
106}
107
108/*
109 * Check the limits and timers of a dquot and start or reset timers
110 * if necessary.
111 * This gets called even when quota enforcement is OFF, which makes our
112 * life a little less complicated. (We just don't reject any quota
113 * reservations in that case, when enforcement is off).
114 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
115 * enforcement's off.
116 * In contrast, warnings are a little different in that they don't
754002b4
NS
117 * 'automatically' get started when limits get exceeded. They do
118 * get reset to zero, however, when we find the count to be under
119 * the soft limit (they are only ever set non-zero via userspace).
1da177e4
LT
120 */
121void
122xfs_qm_adjust_dqtimers(
123 xfs_mount_t *mp,
124 xfs_disk_dquot_t *d)
125{
126 ASSERT(d->d_id);
127
ea15ab3c 128#ifdef DEBUG
1149d96a
CH
129 if (d->d_blk_hardlimit)
130 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
131 be64_to_cpu(d->d_blk_hardlimit));
132 if (d->d_ino_hardlimit)
133 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
134 be64_to_cpu(d->d_ino_hardlimit));
135 if (d->d_rtb_hardlimit)
136 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
137 be64_to_cpu(d->d_rtb_hardlimit));
1da177e4 138#endif
ea15ab3c 139
1da177e4 140 if (!d->d_btimer) {
1149d96a 141 if ((d->d_blk_softlimit &&
d0a3fe67 142 (be64_to_cpu(d->d_bcount) >
1149d96a
CH
143 be64_to_cpu(d->d_blk_softlimit))) ||
144 (d->d_blk_hardlimit &&
d0a3fe67 145 (be64_to_cpu(d->d_bcount) >
1149d96a
CH
146 be64_to_cpu(d->d_blk_hardlimit)))) {
147 d->d_btimer = cpu_to_be32(get_seconds() +
8a7b8a89 148 mp->m_quotainfo->qi_btimelimit);
754002b4
NS
149 } else {
150 d->d_bwarns = 0;
1da177e4
LT
151 }
152 } else {
153 if ((!d->d_blk_softlimit ||
d0a3fe67 154 (be64_to_cpu(d->d_bcount) <=
1149d96a 155 be64_to_cpu(d->d_blk_softlimit))) &&
1da177e4 156 (!d->d_blk_hardlimit ||
d0a3fe67 157 (be64_to_cpu(d->d_bcount) <=
1149d96a 158 be64_to_cpu(d->d_blk_hardlimit)))) {
1da177e4
LT
159 d->d_btimer = 0;
160 }
161 }
162
163 if (!d->d_itimer) {
1149d96a 164 if ((d->d_ino_softlimit &&
d0a3fe67 165 (be64_to_cpu(d->d_icount) >
1149d96a
CH
166 be64_to_cpu(d->d_ino_softlimit))) ||
167 (d->d_ino_hardlimit &&
d0a3fe67 168 (be64_to_cpu(d->d_icount) >
1149d96a
CH
169 be64_to_cpu(d->d_ino_hardlimit)))) {
170 d->d_itimer = cpu_to_be32(get_seconds() +
8a7b8a89 171 mp->m_quotainfo->qi_itimelimit);
754002b4
NS
172 } else {
173 d->d_iwarns = 0;
1da177e4
LT
174 }
175 } else {
176 if ((!d->d_ino_softlimit ||
d0a3fe67 177 (be64_to_cpu(d->d_icount) <=
1149d96a 178 be64_to_cpu(d->d_ino_softlimit))) &&
1da177e4 179 (!d->d_ino_hardlimit ||
d0a3fe67 180 (be64_to_cpu(d->d_icount) <=
1149d96a 181 be64_to_cpu(d->d_ino_hardlimit)))) {
1da177e4
LT
182 d->d_itimer = 0;
183 }
184 }
185
186 if (!d->d_rtbtimer) {
1149d96a 187 if ((d->d_rtb_softlimit &&
d0a3fe67 188 (be64_to_cpu(d->d_rtbcount) >
1149d96a
CH
189 be64_to_cpu(d->d_rtb_softlimit))) ||
190 (d->d_rtb_hardlimit &&
d0a3fe67 191 (be64_to_cpu(d->d_rtbcount) >
1149d96a
CH
192 be64_to_cpu(d->d_rtb_hardlimit)))) {
193 d->d_rtbtimer = cpu_to_be32(get_seconds() +
8a7b8a89 194 mp->m_quotainfo->qi_rtbtimelimit);
754002b4
NS
195 } else {
196 d->d_rtbwarns = 0;
1da177e4
LT
197 }
198 } else {
199 if ((!d->d_rtb_softlimit ||
d0a3fe67 200 (be64_to_cpu(d->d_rtbcount) <=
1149d96a 201 be64_to_cpu(d->d_rtb_softlimit))) &&
1da177e4 202 (!d->d_rtb_hardlimit ||
d0a3fe67 203 (be64_to_cpu(d->d_rtbcount) <=
1149d96a 204 be64_to_cpu(d->d_rtb_hardlimit)))) {
1da177e4
LT
205 d->d_rtbtimer = 0;
206 }
207 }
208}
209
1da177e4
LT
210/*
211 * initialize a buffer full of dquots and log the whole thing
212 */
213STATIC void
214xfs_qm_init_dquot_blk(
215 xfs_trans_t *tp,
216 xfs_mount_t *mp,
217 xfs_dqid_t id,
218 uint type,
219 xfs_buf_t *bp)
220{
8a7b8a89 221 struct xfs_quotainfo *q = mp->m_quotainfo;
1da177e4
LT
222 xfs_dqblk_t *d;
223 int curid, i;
224
225 ASSERT(tp);
0c842ad4 226 ASSERT(xfs_buf_islocked(bp));
1da177e4 227
62926044 228 d = bp->b_addr;
1da177e4
LT
229
230 /*
231 * ID of the first dquot in the block - id's are zero based.
232 */
8a7b8a89 233 curid = id - (id % q->qi_dqperchunk);
1da177e4 234 ASSERT(curid >= 0);
8a7b8a89 235 memset(d, 0, BBTOB(q->qi_dqchunklen));
49d35a5c
CH
236 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
237 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
238 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
239 d->dd_diskdq.d_id = cpu_to_be32(curid);
240 d->dd_diskdq.d_flags = type;
241 }
242
1da177e4 243 xfs_trans_dquot_buf(tp, bp,
c1155410
DC
244 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
245 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
246 XFS_BLF_GDQUOT_BUF)));
8a7b8a89 247 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
1da177e4
LT
248}
249
250
251
252/*
253 * Allocate a block and fill it with dquots.
254 * This is called when the bmapi finds a hole.
255 */
256STATIC int
257xfs_qm_dqalloc(
efa092f3 258 xfs_trans_t **tpp,
1da177e4
LT
259 xfs_mount_t *mp,
260 xfs_dquot_t *dqp,
261 xfs_inode_t *quotip,
262 xfs_fileoff_t offset_fsb,
263 xfs_buf_t **O_bpp)
264{
265 xfs_fsblock_t firstblock;
266 xfs_bmap_free_t flist;
267 xfs_bmbt_irec_t map;
268 int nmaps, error, committed;
269 xfs_buf_t *bp;
efa092f3 270 xfs_trans_t *tp = *tpp;
1da177e4
LT
271
272 ASSERT(tp != NULL);
0b1b213f
CH
273
274 trace_xfs_dqalloc(dqp);
1da177e4
LT
275
276 /*
277 * Initialize the bmap freelist prior to calling bmapi code.
278 */
9d87c319 279 xfs_bmap_init(&flist, &firstblock);
1da177e4
LT
280 xfs_ilock(quotip, XFS_ILOCK_EXCL);
281 /*
282 * Return if this type of quotas is turned off while we didn't
283 * have an inode lock
284 */
6967b964 285 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
1da177e4
LT
286 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
287 return (ESRCH);
288 }
289
ddc3415a 290 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
1da177e4 291 nmaps = 1;
c0dc7828
DC
292 error = xfs_bmapi_write(tp, quotip, offset_fsb,
293 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
294 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
295 &map, &nmaps, &flist);
296 if (error)
1da177e4 297 goto error0;
1da177e4
LT
298 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
299 ASSERT(nmaps == 1);
300 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
301 (map.br_startblock != HOLESTARTBLOCK));
302
303 /*
304 * Keep track of the blkno to save a lookup later
305 */
306 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
307
308 /* now we can just get the buffer (there's nothing to read yet) */
309 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
310 dqp->q_blkno,
8a7b8a89 311 mp->m_quotainfo->qi_dqchunklen,
1da177e4 312 0);
2a30f36d
CS
313
314 error = xfs_buf_geterror(bp);
315 if (error)
1da177e4 316 goto error1;
2a30f36d 317
1da177e4
LT
318 /*
319 * Make a chunk of dquots out of this buffer and log
320 * the entire thing.
321 */
1149d96a 322 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
c8ad20ff 323 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
1da177e4 324
efa092f3
TS
325 /*
326 * xfs_bmap_finish() may commit the current transaction and
327 * start a second transaction if the freelist is not empty.
328 *
329 * Since we still want to modify this buffer, we need to
330 * ensure that the buffer is not released on commit of
331 * the first transaction and ensure the buffer is added to the
332 * second transaction.
333 *
334 * If there is only one transaction then don't stop the buffer
335 * from being released when it commits later on.
336 */
337
338 xfs_trans_bhold(tp, bp);
339
f7c99b6f 340 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
1da177e4
LT
341 goto error1;
342 }
343
efa092f3
TS
344 if (committed) {
345 tp = *tpp;
346 xfs_trans_bjoin(tp, bp);
347 } else {
348 xfs_trans_bhold_release(tp, bp);
349 }
350
1da177e4
LT
351 *O_bpp = bp;
352 return 0;
353
354 error1:
355 xfs_bmap_cancel(&flist);
356 error0:
357 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
358
359 return (error);
360}
361
362/*
363 * Maps a dquot to the buffer containing its on-disk version.
364 * This returns a ptr to the buffer containing the on-disk dquot
365 * in the bpp param, and a ptr to the on-disk dquot within that buffer
366 */
367STATIC int
368xfs_qm_dqtobp(
efa092f3 369 xfs_trans_t **tpp,
1da177e4
LT
370 xfs_dquot_t *dqp,
371 xfs_disk_dquot_t **O_ddpp,
372 xfs_buf_t **O_bpp,
373 uint flags)
374{
375 xfs_bmbt_irec_t map;
acecf1b5 376 int nmaps = 1, error;
1da177e4 377 xfs_buf_t *bp;
acecf1b5
CH
378 xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
379 xfs_mount_t *mp = dqp->q_mount;
1da177e4 380 xfs_disk_dquot_t *ddq;
acecf1b5 381 xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
efa092f3 382 xfs_trans_t *tp = (tpp ? *tpp : NULL);
1da177e4 383
acecf1b5 384 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
1da177e4 385
acecf1b5 386 xfs_ilock(quotip, XFS_ILOCK_SHARED);
6967b964 387 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
1da177e4 388 /*
acecf1b5
CH
389 * Return if this type of quotas is turned off while we
390 * didn't have the quota inode lock.
1da177e4 391 */
acecf1b5
CH
392 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
393 return ESRCH;
394 }
395
396 /*
397 * Find the block map; no allocations yet
398 */
5c8ed202
DC
399 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
400 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
acecf1b5
CH
401
402 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
403 if (error)
404 return error;
405
406 ASSERT(nmaps == 1);
407 ASSERT(map.br_blockcount == 1);
408
409 /*
410 * Offset of dquot in the (fixed sized) dquot chunk.
411 */
412 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
413 sizeof(xfs_dqblk_t);
414
415 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
416 if (map.br_startblock == HOLESTARTBLOCK) {
1da177e4 417 /*
acecf1b5 418 * We don't allocate unless we're asked to
1da177e4 419 */
acecf1b5
CH
420 if (!(flags & XFS_QMOPT_DQALLOC))
421 return ENOENT;
1da177e4 422
acecf1b5
CH
423 ASSERT(tp);
424 error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
425 dqp->q_fileoffset, &bp);
1da177e4 426 if (error)
acecf1b5
CH
427 return error;
428 tp = *tpp;
429 } else {
430 trace_xfs_dqtobp_read(dqp);
1da177e4
LT
431
432 /*
acecf1b5
CH
433 * store the blkno etc so that we don't have to do the
434 * mapping all the time
1da177e4 435 */
acecf1b5 436 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
0b1b213f 437
8a7b8a89
CH
438 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
439 dqp->q_blkno,
440 mp->m_quotainfo->qi_dqchunklen,
441 0, &bp);
1da177e4
LT
442 if (error || !bp)
443 return XFS_ERROR(error);
444 }
acecf1b5 445
0c842ad4 446 ASSERT(xfs_buf_islocked(bp));
1da177e4
LT
447
448 /*
449 * calculate the location of the dquot inside the buffer.
450 */
62926044 451 ddq = bp->b_addr + dqp->q_bufoffset;
1da177e4
LT
452
453 /*
454 * A simple sanity check in case we got a corrupted dquot...
455 */
a0fa2b67 456 error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
1da177e4 457 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
a0fa2b67
DC
458 "dqtobp");
459 if (error) {
1da177e4
LT
460 if (!(flags & XFS_QMOPT_DQREPAIR)) {
461 xfs_trans_brelse(tp, bp);
462 return XFS_ERROR(EIO);
463 }
1da177e4
LT
464 }
465
466 *O_bpp = bp;
467 *O_ddpp = ddq;
468
469 return (0);
470}
471
472
473/*
474 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
475 * and release the buffer immediately.
476 *
97e7ade5 477 * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
1da177e4 478 */
7ae44407 479int
1da177e4 480xfs_qm_dqread(
97e7ade5
CH
481 struct xfs_mount *mp,
482 xfs_dqid_t id,
483 uint type,
484 uint flags,
485 struct xfs_dquot **O_dqpp)
1da177e4 486{
97e7ade5
CH
487 struct xfs_dquot *dqp;
488 struct xfs_disk_dquot *ddqp;
489 struct xfs_buf *bp;
490 struct xfs_trans *tp = NULL;
491 int error;
492 int cancelflags = 0;
efa092f3 493
92b2e5b3
CH
494
495 dqp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
496
497 dqp->dq_flags = type;
498 dqp->q_core.d_id = cpu_to_be32(id);
499 dqp->q_mount = mp;
f8739c3c 500 INIT_LIST_HEAD(&dqp->q_lru);
92b2e5b3
CH
501 mutex_init(&dqp->q_qlock);
502 init_waitqueue_head(&dqp->q_pinwait);
503
504 /*
505 * Because we want to use a counting completion, complete
506 * the flush completion once to allow a single access to
507 * the flush completion without blocking.
508 */
509 init_completion(&dqp->q_flush);
510 complete(&dqp->q_flush);
511
512 /*
513 * Make sure group quotas have a different lock class than user
514 * quotas.
515 */
516 if (!(type & XFS_DQ_USER))
517 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
518
48776fd2 519 XFS_STATS_INC(xs_qm_dquot);
1da177e4 520
0b1b213f
CH
521 trace_xfs_dqread(dqp);
522
97e7ade5
CH
523 if (flags & XFS_QMOPT_DQALLOC) {
524 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
525 error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
526 XFS_WRITE_LOG_RES(mp) +
527 /*
528 * Round the chunklen up to the next multiple
529 * of 128 (buf log item chunk size)).
530 */
531 BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 + 128,
532 0,
533 XFS_TRANS_PERM_LOG_RES,
534 XFS_WRITE_LOG_COUNT);
535 if (error)
536 goto error1;
537 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
538 }
539
1da177e4
LT
540 /*
541 * get a pointer to the on-disk dquot and the buffer containing it
542 * dqp already knows its own type (GROUP/USER).
543 */
97e7ade5
CH
544 error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
545 if (error) {
546 /*
547 * This can happen if quotas got turned off (ESRCH),
548 * or if the dquot didn't exist on disk and we ask to
549 * allocate (ENOENT).
550 */
551 trace_xfs_dqread_fail(dqp);
552 cancelflags |= XFS_TRANS_ABORT;
553 goto error1;
1da177e4
LT
554 }
555
556 /* copy everything from disk dquot to the incore dquot */
557 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
1da177e4
LT
558 xfs_qm_dquot_logitem_init(dqp);
559
560 /*
561 * Reservation counters are defined as reservation plus current usage
25985edc 562 * to avoid having to add every time.
1da177e4 563 */
1149d96a
CH
564 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
565 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
566 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
1da177e4
LT
567
568 /* Mark the buf so that this will stay incore a little longer */
38f23232 569 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
1da177e4
LT
570
571 /*
572 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
573 * So we need to release with xfs_trans_brelse().
574 * The strategy here is identical to that of inodes; we lock
575 * the dquot in xfs_qm_dqget() before making it accessible to
576 * others. This is because dquots, like inodes, need a good level of
577 * concurrency, and we don't want to take locks on the entire buffers
578 * for dquot accesses.
579 * Note also that the dquot buffer may even be dirty at this point, if
580 * this particular dquot was repaired. We still aren't afraid to
581 * brelse it because we have the changes incore.
582 */
0c842ad4 583 ASSERT(xfs_buf_islocked(bp));
1da177e4
LT
584 xfs_trans_brelse(tp, bp);
585
1da177e4 586 if (tp) {
97e7ade5
CH
587 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
588 if (error)
589 goto error0;
1da177e4
LT
590 }
591
592 *O_dqpp = dqp;
97e7ade5 593 return error;
1da177e4 594
97e7ade5 595error1:
1da177e4
LT
596 if (tp)
597 xfs_trans_cancel(tp, cancelflags);
97e7ade5 598error0:
1da177e4
LT
599 xfs_qm_dqdestroy(dqp);
600 *O_dqpp = NULL;
97e7ade5 601 return error;
1da177e4
LT
602}
603
604/*
605 * Lookup a dquot in the incore dquot hashtable. We keep two separate
606 * hashtables for user and group dquots; and, these are global tables
607 * inside the XQM, not per-filesystem tables.
608 * The hash chain must be locked by caller, and it is left locked
609 * on return. Returning dquot is locked.
610 */
611STATIC int
612xfs_qm_dqlookup(
613 xfs_mount_t *mp,
614 xfs_dqid_t id,
615 xfs_dqhash_t *qh,
616 xfs_dquot_t **O_dqpp)
617{
618 xfs_dquot_t *dqp;
1da177e4 619
c9a192dc 620 ASSERT(mutex_is_locked(&qh->qh_lock));
1da177e4 621
1da177e4
LT
622 /*
623 * Traverse the hashchain looking for a match
624 */
e6a81f13 625 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
1da177e4
LT
626 /*
627 * We already have the hashlock. We don't need the
628 * dqlock to look at the id field of the dquot, since the
629 * id can't be modified without the hashlock anyway.
630 */
be7ffc38
CH
631 if (be32_to_cpu(dqp->q_core.d_id) != id || dqp->q_mount != mp)
632 continue;
0b1b213f 633
be7ffc38 634 trace_xfs_dqlookup_found(dqp);
1da177e4 635
be7ffc38 636 xfs_dqlock(dqp);
92678554
CH
637 if (dqp->dq_flags & XFS_DQ_FREEING) {
638 *O_dqpp = NULL;
639 xfs_dqunlock(dqp);
640 return -1;
641 }
642
78e55892 643 dqp->q_nrefs++;
be7ffc38
CH
644
645 /*
646 * move the dquot to the front of the hashchain
647 */
648 list_move(&dqp->q_hashlist, &qh->qh_list);
649 trace_xfs_dqlookup_done(dqp);
650 *O_dqpp = dqp;
651 return 0;
1da177e4
LT
652 }
653
654 *O_dqpp = NULL;
be7ffc38 655 return 1;
1da177e4
LT
656}
657
658/*
659 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
660 * a locked dquot, doing an allocation (if requested) as needed.
661 * When both an inode and an id are given, the inode's id takes precedence.
662 * That is, if the id changes while we don't hold the ilock inside this
663 * function, the new dquot is returned, not necessarily the one requested
664 * in the id argument.
665 */
666int
667xfs_qm_dqget(
668 xfs_mount_t *mp,
669 xfs_inode_t *ip, /* locked inode (optional) */
c8ad20ff
NS
670 xfs_dqid_t id, /* uid/projid/gid depending on type */
671 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
1da177e4
LT
672 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
673 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
674{
36731410 675 xfs_dquot_t *dqp, *dqp1;
1da177e4
LT
676 xfs_dqhash_t *h;
677 uint version;
678 int error;
679
680 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
681 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
c8ad20ff 682 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
1da177e4
LT
683 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
684 return (ESRCH);
685 }
686 h = XFS_DQ_HASH(mp, id, type);
687
688#ifdef DEBUG
689 if (xfs_do_dqerror) {
690 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
691 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
0b932ccc 692 xfs_debug(mp, "Returning error in dqget");
1da177e4
LT
693 return (EIO);
694 }
695 }
1da177e4 696
c8ad20ff
NS
697 ASSERT(type == XFS_DQ_USER ||
698 type == XFS_DQ_PROJ ||
699 type == XFS_DQ_GROUP);
1da177e4 700 if (ip) {
579aa9ca 701 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
36731410 702 ASSERT(xfs_inode_dquot(ip, type) == NULL);
1da177e4
LT
703 }
704#endif
92678554
CH
705
706restart:
c9a192dc 707 mutex_lock(&h->qh_lock);
1da177e4
LT
708
709 /*
710 * Look in the cache (hashtable).
711 * The chain is kept locked during lookup.
712 */
92678554
CH
713 switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) {
714 case -1:
48776fd2 715 XFS_STATS_INC(xs_qm_dquot_dups);
92678554
CH
716 mutex_unlock(&h->qh_lock);
717 delay(1);
718 goto restart;
719 case 0:
48776fd2 720 XFS_STATS_INC(xs_qm_dqcachehits);
1da177e4
LT
721 /*
722 * The dquot was found, moved to the front of the chain,
723 * taken off the freelist if it was on it, and locked
724 * at this point. Just unlock the hashchain and return.
725 */
726 ASSERT(*O_dqpp);
727 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
c9a192dc 728 mutex_unlock(&h->qh_lock);
0b1b213f 729 trace_xfs_dqget_hit(*O_dqpp);
92678554
CH
730 return 0; /* success */
731 default:
48776fd2 732 XFS_STATS_INC(xs_qm_dqcachemisses);
92678554 733 break;
1da177e4 734 }
1da177e4
LT
735
736 /*
737 * Dquot cache miss. We don't want to keep the inode lock across
738 * a (potential) disk read. Also we don't want to deal with the lock
739 * ordering between quotainode and this inode. OTOH, dropping the inode
740 * lock here means dealing with a chown that can happen before
741 * we re-acquire the lock.
742 */
743 if (ip)
744 xfs_iunlock(ip, XFS_ILOCK_EXCL);
745 /*
746 * Save the hashchain version stamp, and unlock the chain, so that
747 * we don't keep the lock across a disk read
748 */
749 version = h->qh_version;
c9a192dc 750 mutex_unlock(&h->qh_lock);
1da177e4 751
97e7ade5 752 error = xfs_qm_dqread(mp, id, type, flags, &dqp);
1da177e4 753
7ae44407
CH
754 if (ip)
755 xfs_ilock(ip, XFS_ILOCK_EXCL);
756
757 if (error)
758 return error;
1da177e4
LT
759
760 /*
761 * Dquot lock comes after hashlock in the lock ordering
762 */
763 if (ip) {
1da177e4
LT
764 /*
765 * A dquot could be attached to this inode by now, since
766 * we had dropped the ilock.
767 */
36731410
CS
768 if (xfs_this_quota_on(mp, type)) {
769 dqp1 = xfs_inode_dquot(ip, type);
770 if (dqp1) {
1da177e4 771 xfs_qm_dqdestroy(dqp);
36731410 772 dqp = dqp1;
1da177e4
LT
773 xfs_dqlock(dqp);
774 goto dqret;
775 }
776 } else {
36731410
CS
777 /* inode stays locked on return */
778 xfs_qm_dqdestroy(dqp);
779 return XFS_ERROR(ESRCH);
1da177e4
LT
780 }
781 }
782
783 /*
784 * Hashlock comes after ilock in lock order
785 */
c9a192dc 786 mutex_lock(&h->qh_lock);
1da177e4
LT
787 if (version != h->qh_version) {
788 xfs_dquot_t *tmpdqp;
789 /*
790 * Now, see if somebody else put the dquot in the
791 * hashtable before us. This can happen because we didn't
792 * keep the hashchain lock. We don't have to worry about
793 * lock order between the two dquots here since dqp isn't
794 * on any findable lists yet.
795 */
92678554
CH
796 switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) {
797 case 0:
798 case -1:
1da177e4 799 /*
92678554
CH
800 * Duplicate found, either in cache or on its way out.
801 * Just throw away the new dquot and start over.
1da177e4 802 */
92678554
CH
803 if (tmpdqp)
804 xfs_qm_dqput(tmpdqp);
c9a192dc 805 mutex_unlock(&h->qh_lock);
1da177e4 806 xfs_qm_dqdestroy(dqp);
48776fd2 807 XFS_STATS_INC(xs_qm_dquot_dups);
92678554
CH
808 goto restart;
809 default:
810 break;
1da177e4
LT
811 }
812 }
813
814 /*
815 * Put the dquot at the beginning of the hash-chain and mp's list
816 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
817 */
c9a192dc 818 ASSERT(mutex_is_locked(&h->qh_lock));
1da177e4 819 dqp->q_hash = h;
e6a81f13
DC
820 list_add(&dqp->q_hashlist, &h->qh_list);
821 h->qh_version++;
1da177e4
LT
822
823 /*
824 * Attach this dquot to this filesystem's list of all dquots,
825 * kept inside the mount structure in m_quotainfo field
826 */
3a25404b 827 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
1da177e4
LT
828
829 /*
830 * We return a locked dquot to the caller, with a reference taken
831 */
832 xfs_dqlock(dqp);
833 dqp->q_nrefs = 1;
834
3a25404b
DC
835 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
836 mp->m_quotainfo->qi_dquots++;
837 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
c9a192dc 838 mutex_unlock(&h->qh_lock);
1da177e4 839 dqret:
579aa9ca 840 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
0b1b213f 841 trace_xfs_dqget_miss(dqp);
1da177e4
LT
842 *O_dqpp = dqp;
843 return (0);
844}
845
846
f8739c3c
CH
847STATIC void
848xfs_qm_dqput_final(
bf72de31 849 struct xfs_dquot *dqp)
1da177e4 850{
f8739c3c 851 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
bf72de31 852 struct xfs_dquot *gdqp;
1da177e4 853
bf72de31
CH
854 trace_xfs_dqput_free(dqp);
855
f8739c3c
CH
856 mutex_lock(&qi->qi_lru_lock);
857 if (list_empty(&dqp->q_lru)) {
858 list_add_tail(&dqp->q_lru, &qi->qi_lru_list);
859 qi->qi_lru_count++;
48776fd2 860 XFS_STATS_INC(xs_qm_dquot_unused);
bf72de31 861 }
f8739c3c 862 mutex_unlock(&qi->qi_lru_lock);
bf72de31 863
1da177e4 864 /*
bf72de31
CH
865 * If we just added a udquot to the freelist, then we want to release
866 * the gdquot reference that it (probably) has. Otherwise it'll keep
867 * the gdquot from getting reclaimed.
1da177e4 868 */
bf72de31
CH
869 gdqp = dqp->q_gdquot;
870 if (gdqp) {
871 xfs_dqlock(gdqp);
872 dqp->q_gdquot = NULL;
1da177e4 873 }
bf72de31 874 xfs_dqunlock(dqp);
1da177e4 875
bf72de31
CH
876 /*
877 * If we had a group quota hint, release it now.
878 */
f8739c3c
CH
879 if (gdqp)
880 xfs_qm_dqput(gdqp);
881}
882
883/*
884 * Release a reference to the dquot (decrement ref-count) and unlock it.
885 *
886 * If there is a group quota attached to this dquot, carefully release that
887 * too without tripping over deadlocks'n'stuff.
888 */
889void
890xfs_qm_dqput(
891 struct xfs_dquot *dqp)
892{
893 ASSERT(dqp->q_nrefs > 0);
894 ASSERT(XFS_DQ_IS_LOCKED(dqp));
895
896 trace_xfs_dqput(dqp);
897
898 if (--dqp->q_nrefs > 0)
899 xfs_dqunlock(dqp);
900 else
901 xfs_qm_dqput_final(dqp);
1da177e4
LT
902}
903
904/*
905 * Release a dquot. Flush it if dirty, then dqput() it.
906 * dquot must not be locked.
907 */
908void
909xfs_qm_dqrele(
910 xfs_dquot_t *dqp)
911{
7d095257
CH
912 if (!dqp)
913 return;
914
0b1b213f 915 trace_xfs_dqrele(dqp);
1da177e4
LT
916
917 xfs_dqlock(dqp);
918 /*
919 * We don't care to flush it if the dquot is dirty here.
920 * That will create stutters that we want to avoid.
921 * Instead we do a delayed write when we try to reclaim
922 * a dirty dquot. Also xfs_sync will take part of the burden...
923 */
924 xfs_qm_dqput(dqp);
925}
926
ca30b2a7
CH
927/*
928 * This is the dquot flushing I/O completion routine. It is called
929 * from interrupt level when the buffer containing the dquot is
930 * flushed to disk. It is responsible for removing the dquot logitem
931 * from the AIL if it has not been re-logged, and unlocking the dquot's
932 * flush lock. This behavior is very similar to that of inodes..
933 */
934STATIC void
935xfs_qm_dqflush_done(
936 struct xfs_buf *bp,
937 struct xfs_log_item *lip)
938{
939 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
940 xfs_dquot_t *dqp = qip->qli_dquot;
941 struct xfs_ail *ailp = lip->li_ailp;
942
943 /*
944 * We only want to pull the item from the AIL if its
945 * location in the log has not changed since we started the flush.
946 * Thus, we only bother if the dquot's lsn has
947 * not changed. First we check the lsn outside the lock
948 * since it's cheaper, and then we recheck while
949 * holding the lock before removing the dquot from the AIL.
950 */
951 if ((lip->li_flags & XFS_LI_IN_AIL) &&
952 lip->li_lsn == qip->qli_flush_lsn) {
953
954 /* xfs_trans_ail_delete() drops the AIL lock. */
955 spin_lock(&ailp->xa_lock);
956 if (lip->li_lsn == qip->qli_flush_lsn)
957 xfs_trans_ail_delete(ailp, lip);
958 else
959 spin_unlock(&ailp->xa_lock);
960 }
961
962 /*
963 * Release the dq's flush lock since we're done with it.
964 */
965 xfs_dqfunlock(dqp);
966}
1da177e4
LT
967
968/*
969 * Write a modified dquot to disk.
970 * The dquot must be locked and the flush lock too taken by caller.
971 * The flush lock will not be unlocked until the dquot reaches the disk,
972 * but the dquot is free to be unlocked and modified by the caller
973 * in the interim. Dquot is still locked on return. This behavior is
974 * identical to that of inodes.
975 */
976int
977xfs_qm_dqflush(
978 xfs_dquot_t *dqp,
979 uint flags)
980{
acecf1b5
CH
981 struct xfs_mount *mp = dqp->q_mount;
982 struct xfs_buf *bp;
983 struct xfs_disk_dquot *ddqp;
1da177e4 984 int error;
1da177e4
LT
985
986 ASSERT(XFS_DQ_IS_LOCKED(dqp));
e1f49cf2 987 ASSERT(!completion_done(&dqp->q_flush));
acecf1b5 988
0b1b213f 989 trace_xfs_dqflush(dqp);
1da177e4
LT
990
991 /*
acecf1b5 992 * If not dirty, or it's pinned and we are not supposed to block, nada.
1da177e4 993 */
2f8a3ce1 994 if (!XFS_DQ_IS_DIRTY(dqp) ||
fdedf28b 995 ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
1da177e4 996 xfs_dqfunlock(dqp);
2f8a3ce1 997 return 0;
1da177e4 998 }
1da177e4
LT
999 xfs_qm_dqunpin_wait(dqp);
1000
1001 /*
1002 * This may have been unpinned because the filesystem is shutting
1003 * down forcibly. If that's the case we must not write this dquot
1004 * to disk, because the log record didn't make it to disk!
1005 */
acecf1b5
CH
1006 if (XFS_FORCED_SHUTDOWN(mp)) {
1007 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1da177e4
LT
1008 xfs_dqfunlock(dqp);
1009 return XFS_ERROR(EIO);
1010 }
1011
1012 /*
1013 * Get the buffer containing the on-disk dquot
1da177e4 1014 */
acecf1b5
CH
1015 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1016 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
1017 if (error) {
1da177e4 1018 ASSERT(error != ENOENT);
1da177e4 1019 xfs_dqfunlock(dqp);
acecf1b5 1020 return error;
1da177e4
LT
1021 }
1022
acecf1b5
CH
1023 /*
1024 * Calculate the location of the dquot inside the buffer.
1025 */
62926044 1026 ddqp = bp->b_addr + dqp->q_bufoffset;
acecf1b5
CH
1027
1028 /*
1029 * A simple sanity check in case we got a corrupted dquot..
1030 */
a0fa2b67
DC
1031 error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
1032 XFS_QMOPT_DOWARN, "dqflush (incore copy)");
1033 if (error) {
acecf1b5
CH
1034 xfs_buf_relse(bp);
1035 xfs_dqfunlock(dqp);
1036 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1da177e4
LT
1037 return XFS_ERROR(EIO);
1038 }
1039
1040 /* This is the only portion of data that needs to persist */
acecf1b5 1041 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
1da177e4
LT
1042
1043 /*
1044 * Clear the dirty field and remember the flush lsn for later use.
1045 */
acecf1b5 1046 dqp->dq_flags &= ~XFS_DQ_DIRTY;
1da177e4 1047
7b2e2a31
DC
1048 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1049 &dqp->q_logitem.qli_item.li_lsn);
1da177e4
LT
1050
1051 /*
1052 * Attach an iodone routine so that we can remove this dquot from the
1053 * AIL and release the flush lock once the dquot is synced to disk.
1054 */
ca30b2a7
CH
1055 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1056 &dqp->q_logitem.qli_item);
1057
1da177e4
LT
1058 /*
1059 * If the buffer is pinned then push on the log so we won't
1060 * get stuck waiting in the write for too long.
1061 */
811e64c7 1062 if (xfs_buf_ispinned(bp)) {
0b1b213f 1063 trace_xfs_dqflush_force(dqp);
a14a348b 1064 xfs_log_force(mp, 0);
1da177e4
LT
1065 }
1066
20026d92 1067 if (flags & SYNC_WAIT)
c2b006c1
CH
1068 error = xfs_bwrite(bp);
1069 else
61551f1e 1070 xfs_buf_delwri_queue(bp);
c2b006c1
CH
1071
1072 xfs_buf_relse(bp);
0b1b213f
CH
1073
1074 trace_xfs_dqflush_done(dqp);
1075
1da177e4
LT
1076 /*
1077 * dqp is still locked, but caller is free to unlock it now.
1078 */
20026d92 1079 return error;
1da177e4
LT
1080
1081}
1082
5bb87a33
CH
1083/*
1084 * Lock two xfs_dquot structures.
1085 *
1086 * To avoid deadlocks we always lock the quota structure with
1087 * the lowerd id first.
1088 */
1da177e4
LT
1089void
1090xfs_dqlock2(
1091 xfs_dquot_t *d1,
1092 xfs_dquot_t *d2)
1093{
1094 if (d1 && d2) {
1095 ASSERT(d1 != d2);
1149d96a
CH
1096 if (be32_to_cpu(d1->q_core.d_id) >
1097 be32_to_cpu(d2->q_core.d_id)) {
5bb87a33
CH
1098 mutex_lock(&d2->q_qlock);
1099 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1100 } else {
5bb87a33
CH
1101 mutex_lock(&d1->q_qlock);
1102 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1da177e4 1103 }
5bb87a33
CH
1104 } else if (d1) {
1105 mutex_lock(&d1->q_qlock);
1106 } else if (d2) {
1107 mutex_lock(&d2->q_qlock);
1da177e4
LT
1108 }
1109}
1110
1da177e4 1111/*
92678554
CH
1112 * Take a dquot out of the mount's dqlist as well as the hashlist. This is
1113 * called via unmount as well as quotaoff, and the purge will always succeed.
1da177e4 1114 */
92678554 1115void
1da177e4 1116xfs_qm_dqpurge(
92678554 1117 struct xfs_dquot *dqp)
1da177e4 1118{
92678554
CH
1119 struct xfs_mount *mp = dqp->q_mount;
1120 struct xfs_dqhash *qh = dqp->q_hash;
f8739c3c 1121 struct xfs_quotainfo *qi = mp->m_quotainfo;
80a376bf 1122
1da177e4 1123 xfs_dqlock(dqp);
1da177e4
LT
1124
1125 /*
1126 * If we're turning off quotas, we have to make sure that, for
1127 * example, we don't delete quota disk blocks while dquots are
1128 * in the process of getting written to those disk blocks.
1129 * This dquot might well be on AIL, and we can't leave it there
1130 * if we're turning off quotas. Basically, we need this flush
1131 * lock, and are willing to block on it.
1132 */
e1f49cf2 1133 if (!xfs_dqflock_nowait(dqp)) {
1da177e4
LT
1134 /*
1135 * Block on the flush lock after nudging dquot buffer,
1136 * if it is incore.
1137 */
800b484e 1138 xfs_dqflock_pushbuf_wait(dqp);
1da177e4
LT
1139 }
1140
1141 /*
92678554 1142 * If we are turning this type of quotas off, we don't care
1da177e4
LT
1143 * about the dirty metadata sitting in this dquot. OTOH, if
1144 * we're unmounting, we do care, so we flush it and wait.
1145 */
1146 if (XFS_DQ_IS_DIRTY(dqp)) {
3c56836f 1147 int error;
0b1b213f 1148
1da177e4 1149 /*
1da177e4
LT
1150 * We don't care about getting disk errors here. We need
1151 * to purge this dquot anyway, so we go ahead regardless.
1152 */
20026d92 1153 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
3c56836f 1154 if (error)
53487786
DC
1155 xfs_warn(mp, "%s: dquot %p flush failed",
1156 __func__, dqp);
1da177e4
LT
1157 xfs_dqflock(dqp);
1158 }
92678554 1159
bc3048e3 1160 ASSERT(atomic_read(&dqp->q_pincount) == 0);
1da177e4
LT
1161 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1162 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1163
92678554
CH
1164 xfs_dqfunlock(dqp);
1165 xfs_dqunlock(dqp);
1166
1167 mutex_lock(&qh->qh_lock);
e6a81f13
DC
1168 list_del_init(&dqp->q_hashlist);
1169 qh->qh_version++;
92678554 1170 mutex_unlock(&qh->qh_lock);
80a376bf 1171
f8739c3c 1172 mutex_lock(&qi->qi_dqlist_lock);
3a25404b 1173 list_del_init(&dqp->q_mplist);
f8739c3c
CH
1174 qi->qi_dqreclaims++;
1175 qi->qi_dquots--;
1176 mutex_unlock(&qi->qi_dqlist_lock);
1da177e4 1177
92678554
CH
1178 /*
1179 * We move dquots to the freelist as soon as their reference count
1180 * hits zero, so it really should be on the freelist here.
1181 */
f8739c3c
CH
1182 mutex_lock(&qi->qi_lru_lock);
1183 ASSERT(!list_empty(&dqp->q_lru));
1184 list_del_init(&dqp->q_lru);
1185 qi->qi_lru_count--;
48776fd2 1186 XFS_STATS_DEC(xs_qm_dquot_unused);
f8739c3c 1187 mutex_unlock(&qi->qi_lru_lock);
1da177e4 1188
80a376bf 1189 xfs_qm_dqdestroy(dqp);
80a376bf 1190}
1da177e4 1191
1da177e4
LT
1192/*
1193 * Give the buffer a little push if it is incore and
1194 * wait on the flush lock.
1195 */
1196void
800b484e 1197xfs_dqflock_pushbuf_wait(
1da177e4
LT
1198 xfs_dquot_t *dqp)
1199{
8a7b8a89 1200 xfs_mount_t *mp = dqp->q_mount;
1da177e4
LT
1201 xfs_buf_t *bp;
1202
1203 /*
1204 * Check to see if the dquot has been flushed delayed
1205 * write. If so, grab its buffer and send it
1206 * out immediately. We'll be able to acquire
1207 * the flush lock when the I/O completes.
1208 */
8a7b8a89
CH
1209 bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
1210 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
7d6a7bde
DC
1211 if (!bp)
1212 goto out_lock;
1213
1214 if (XFS_BUF_ISDELAYWRITE(bp)) {
811e64c7 1215 if (xfs_buf_ispinned(bp))
8a7b8a89 1216 xfs_log_force(mp, 0);
7d6a7bde
DC
1217 xfs_buf_delwri_promote(bp);
1218 wake_up_process(bp->b_target->bt_task);
1da177e4 1219 }
7d6a7bde
DC
1220 xfs_buf_relse(bp);
1221out_lock:
1da177e4
LT
1222 xfs_dqflock(dqp);
1223}
This page took 0.841156 seconds and 5 git commands to generate.