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