2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
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
7 * published by the Free Software Foundation.
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.
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
23 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_trans_priv.h"
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.
67 STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
70 xfs_buftarg_t *xfs_dqerror_target;
73 int xfs_dqerror_mod = 33;
77 * Allocate and initialize a dquot. We don't always allocate fresh memory;
78 * we try to reclaim a free dquot if the number of incore dquots are above
80 * The only field inside the core that gets initialized at this point
81 * is the d_id field. The idea is to fill in the entire q_core
82 * when we read in the on disk dquot.
91 boolean_t brandnewdquot;
93 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
95 dqp->q_core.d_id = cpu_to_be32(id);
99 * No need to re-initialize these if this is a reclaimed dquot.
102 dqp->dq_flnext = dqp->dq_flprev = dqp;
103 mutex_init(&dqp->q_qlock);
104 sv_init(&dqp->q_pinwait, SV_DEFAULT, "pdq");
107 * Because we want to use a counting completion, complete
108 * the flush completion once to allow a single access to
109 * the flush completion without blocking.
111 init_completion(&dqp->q_flush);
112 complete(&dqp->q_flush);
114 #ifdef XFS_DQUOT_TRACE
115 dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_NOFS);
116 xfs_dqtrace_entry(dqp, "DQINIT");
120 * Only the q_core portion was zeroed in dqreclaim_one().
121 * So, we need to reset others.
125 dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
126 dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
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;
136 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
138 #ifdef XFS_DQUOT_TRACE
139 ASSERT(dqp->q_trace);
140 xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
145 * log item gets initialized later
151 * This is called to free all the memory associated with a dquot
157 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
159 mutex_destroy(&dqp->q_qlock);
160 sv_destroy(&dqp->q_pinwait);
162 #ifdef XFS_DQUOT_TRACE
164 ktrace_free(dqp->q_trace);
167 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
168 atomic_dec(&xfs_Gqm->qm_totaldquots);
172 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
181 * Caller has zero'd the entire dquot 'chunk' already.
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;
190 #ifdef XFS_DQUOT_TRACE
192 * Dquot tracing for debugging.
202 xfs_dquot_t *udqp = NULL;
205 ASSERT(dqp->q_trace);
210 ktrace_enter(dqp->q_trace,
211 (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
213 (void *)(__psint_t)dqp->q_nrefs,
214 (void *)(__psint_t)dqp->dq_flags,
215 (void *)(__psint_t)dqp->q_res_bcount,
216 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_bcount),
217 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_icount),
218 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_blk_hardlimit),
219 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_blk_softlimit),
220 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_ino_hardlimit),
221 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_ino_softlimit),
222 (void *)(__psint_t)be32_to_cpu(dqp->q_core.d_id),
223 (void *)(__psint_t)current_pid(),
224 (void *)(__psint_t)ino,
225 (void *)(__psint_t)retaddr,
226 (void *)(__psint_t)udqp);
233 * If default limits are in force, push them into the dquot now.
234 * We overwrite the dquot limits only if they are zero and this
235 * is not the root dquot.
238 xfs_qm_adjust_dqlimits(
242 xfs_quotainfo_t *q = mp->m_quotainfo;
246 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
247 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
248 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
249 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
250 if (q->qi_isoftlimit && !d->d_ino_softlimit)
251 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
252 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
253 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
254 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
255 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
256 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
257 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
261 * Check the limits and timers of a dquot and start or reset timers
263 * This gets called even when quota enforcement is OFF, which makes our
264 * life a little less complicated. (We just don't reject any quota
265 * reservations in that case, when enforcement is off).
266 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
268 * In contrast, warnings are a little different in that they don't
269 * 'automatically' get started when limits get exceeded. They do
270 * get reset to zero, however, when we find the count to be under
271 * the soft limit (they are only ever set non-zero via userspace).
274 xfs_qm_adjust_dqtimers(
281 if (d->d_blk_hardlimit)
282 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
283 be64_to_cpu(d->d_blk_hardlimit));
284 if (d->d_ino_hardlimit)
285 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
286 be64_to_cpu(d->d_ino_hardlimit));
287 if (d->d_rtb_hardlimit)
288 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
289 be64_to_cpu(d->d_rtb_hardlimit));
292 if ((d->d_blk_softlimit &&
293 (be64_to_cpu(d->d_bcount) >=
294 be64_to_cpu(d->d_blk_softlimit))) ||
295 (d->d_blk_hardlimit &&
296 (be64_to_cpu(d->d_bcount) >=
297 be64_to_cpu(d->d_blk_hardlimit)))) {
298 d->d_btimer = cpu_to_be32(get_seconds() +
299 XFS_QI_BTIMELIMIT(mp));
304 if ((!d->d_blk_softlimit ||
305 (be64_to_cpu(d->d_bcount) <
306 be64_to_cpu(d->d_blk_softlimit))) &&
307 (!d->d_blk_hardlimit ||
308 (be64_to_cpu(d->d_bcount) <
309 be64_to_cpu(d->d_blk_hardlimit)))) {
315 if ((d->d_ino_softlimit &&
316 (be64_to_cpu(d->d_icount) >=
317 be64_to_cpu(d->d_ino_softlimit))) ||
318 (d->d_ino_hardlimit &&
319 (be64_to_cpu(d->d_icount) >=
320 be64_to_cpu(d->d_ino_hardlimit)))) {
321 d->d_itimer = cpu_to_be32(get_seconds() +
322 XFS_QI_ITIMELIMIT(mp));
327 if ((!d->d_ino_softlimit ||
328 (be64_to_cpu(d->d_icount) <
329 be64_to_cpu(d->d_ino_softlimit))) &&
330 (!d->d_ino_hardlimit ||
331 (be64_to_cpu(d->d_icount) <
332 be64_to_cpu(d->d_ino_hardlimit)))) {
337 if (!d->d_rtbtimer) {
338 if ((d->d_rtb_softlimit &&
339 (be64_to_cpu(d->d_rtbcount) >=
340 be64_to_cpu(d->d_rtb_softlimit))) ||
341 (d->d_rtb_hardlimit &&
342 (be64_to_cpu(d->d_rtbcount) >=
343 be64_to_cpu(d->d_rtb_hardlimit)))) {
344 d->d_rtbtimer = cpu_to_be32(get_seconds() +
345 XFS_QI_RTBTIMELIMIT(mp));
350 if ((!d->d_rtb_softlimit ||
351 (be64_to_cpu(d->d_rtbcount) <
352 be64_to_cpu(d->d_rtb_softlimit))) &&
353 (!d->d_rtb_hardlimit ||
354 (be64_to_cpu(d->d_rtbcount) <
355 be64_to_cpu(d->d_rtb_hardlimit)))) {
362 * initialize a buffer full of dquots and log the whole thing
365 xfs_qm_init_dquot_blk(
376 ASSERT(XFS_BUF_ISBUSY(bp));
377 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
379 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
382 * ID of the first dquot in the block - id's are zero based.
384 curid = id - (id % XFS_QM_DQPERBLK(mp));
386 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
387 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
388 xfs_qm_dqinit_core(curid, type, d);
389 xfs_trans_dquot_buf(tp, bp,
390 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
391 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
392 XFS_BLI_GDQUOT_BUF)));
393 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
399 * Allocate a block and fill it with dquots.
400 * This is called when the bmapi finds a hole.
408 xfs_fileoff_t offset_fsb,
411 xfs_fsblock_t firstblock;
412 xfs_bmap_free_t flist;
414 int nmaps, error, committed;
416 xfs_trans_t *tp = *tpp;
419 xfs_dqtrace_entry(dqp, "DQALLOC");
422 * Initialize the bmap freelist prior to calling bmapi code.
424 XFS_BMAP_INIT(&flist, &firstblock);
425 xfs_ilock(quotip, XFS_ILOCK_EXCL);
427 * Return if this type of quotas is turned off while we didn't
430 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
431 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
436 * xfs_trans_commit normally decrements the vnode ref count
437 * when it unlocks the inode. Since we want to keep the quota
438 * inode around, we bump the vnode ref count now.
442 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
444 if ((error = xfs_bmapi(tp, quotip,
445 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
446 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
448 XFS_QM_DQALLOC_SPACE_RES(mp),
449 &map, &nmaps, &flist, NULL))) {
452 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
454 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
455 (map.br_startblock != HOLESTARTBLOCK));
458 * Keep track of the blkno to save a lookup later
460 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
462 /* now we can just get the buffer (there's nothing to read yet) */
463 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
465 XFS_QI_DQCHUNKLEN(mp),
467 if (!bp || (error = XFS_BUF_GETERROR(bp)))
470 * Make a chunk of dquots out of this buffer and log
473 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
474 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
477 * xfs_bmap_finish() may commit the current transaction and
478 * start a second transaction if the freelist is not empty.
480 * Since we still want to modify this buffer, we need to
481 * ensure that the buffer is not released on commit of
482 * the first transaction and ensure the buffer is added to the
483 * second transaction.
485 * If there is only one transaction then don't stop the buffer
486 * from being released when it commits later on.
489 xfs_trans_bhold(tp, bp);
491 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
497 xfs_trans_bjoin(tp, bp);
499 xfs_trans_bhold_release(tp, bp);
506 xfs_bmap_cancel(&flist);
508 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
514 * Maps a dquot to the buffer containing its on-disk version.
515 * This returns a ptr to the buffer containing the on-disk dquot
516 * in the bpp param, and a ptr to the on-disk dquot within that buffer
522 xfs_disk_dquot_t **O_ddpp,
531 xfs_disk_dquot_t *ddq;
534 xfs_trans_t *tp = (tpp ? *tpp : NULL);
537 id = be32_to_cpu(dqp->q_core.d_id);
542 * If we don't know where the dquot lives, find out.
544 if (dqp->q_blkno == (xfs_daddr_t) 0) {
545 /* We use the id as an index */
546 dqp->q_fileoffset = (xfs_fileoff_t)id / XFS_QM_DQPERBLK(mp);
548 quotip = XFS_DQ_TO_QIP(dqp);
549 xfs_ilock(quotip, XFS_ILOCK_SHARED);
551 * Return if this type of quotas is turned off while we didn't
554 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
555 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
559 * Find the block map; no allocations yet
561 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
562 XFS_DQUOT_CLUSTER_SIZE_FSB,
564 NULL, 0, &map, &nmaps, NULL, NULL);
566 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
570 ASSERT(map.br_blockcount == 1);
573 * offset of dquot in the (fixed sized) dquot chunk.
575 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
577 if (map.br_startblock == HOLESTARTBLOCK) {
579 * We don't allocate unless we're asked to
581 if (!(flags & XFS_QMOPT_DQALLOC))
585 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
586 dqp->q_fileoffset, &bp)))
592 * store the blkno etc so that we don't have to do the
593 * mapping all the time
595 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
598 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
599 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
602 * Read in the buffer, unless we've just done the allocation
603 * (in which case we already have the buf).
606 xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
607 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
609 XFS_QI_DQCHUNKLEN(mp),
614 return XFS_ERROR(error);
616 ASSERT(XFS_BUF_ISBUSY(bp));
617 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
620 * calculate the location of the dquot inside the buffer.
622 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
625 * A simple sanity check in case we got a corrupted dquot...
627 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
628 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
630 if (!(flags & XFS_QMOPT_DQREPAIR)) {
631 xfs_trans_brelse(tp, bp);
632 return XFS_ERROR(EIO);
634 XFS_BUF_BUSY(bp); /* We dirtied this */
645 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
646 * and release the buffer immediately.
654 xfs_dquot_t *dqp, /* dquot to get filled in */
657 xfs_disk_dquot_t *ddqp;
665 * get a pointer to the on-disk dquot and the buffer containing it
666 * dqp already knows its own type (GROUP/USER).
668 xfs_dqtrace_entry(dqp, "DQREAD");
669 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
674 /* copy everything from disk dquot to the incore dquot */
675 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
676 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
677 xfs_qm_dquot_logitem_init(dqp);
680 * Reservation counters are defined as reservation plus current usage
681 * to avoid having to add everytime.
683 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
684 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
685 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
687 /* Mark the buf so that this will stay incore a little longer */
688 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
691 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
692 * So we need to release with xfs_trans_brelse().
693 * The strategy here is identical to that of inodes; we lock
694 * the dquot in xfs_qm_dqget() before making it accessible to
695 * others. This is because dquots, like inodes, need a good level of
696 * concurrency, and we don't want to take locks on the entire buffers
697 * for dquot accesses.
698 * Note also that the dquot buffer may even be dirty at this point, if
699 * this particular dquot was repaired. We still aren't afraid to
700 * brelse it because we have the changes incore.
702 ASSERT(XFS_BUF_ISBUSY(bp));
703 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
704 xfs_trans_brelse(tp, bp);
711 * allocate an incore dquot from the kernel heap,
712 * and fill its core with quota information kept on disk.
713 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
714 * if it wasn't already allocated.
719 xfs_dqid_t id, /* gid or uid, depending on type */
720 uint type, /* UDQUOT or GDQUOT */
721 uint flags, /* DQALLOC, DQREPAIR */
722 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
729 dqp = xfs_qm_dqinit(mp, id, type);
731 if (flags & XFS_QMOPT_DQALLOC) {
732 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
733 if ((error = xfs_trans_reserve(tp,
734 XFS_QM_DQALLOC_SPACE_RES(mp),
735 XFS_WRITE_LOG_RES(mp) +
736 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
739 XFS_TRANS_PERM_LOG_RES,
740 XFS_WRITE_LOG_COUNT))) {
744 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
748 * Read it from disk; xfs_dqread() takes care of
749 * all the necessary initialization of dquot's fields (locks, etc)
751 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
753 * This can happen if quotas got turned off (ESRCH),
754 * or if the dquot didn't exist on disk and we ask to
757 xfs_dqtrace_entry(dqp, "DQREAD FAIL");
758 cancelflags |= XFS_TRANS_ABORT;
762 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
772 xfs_trans_cancel(tp, cancelflags);
774 xfs_qm_dqdestroy(dqp);
780 * Lookup a dquot in the incore dquot hashtable. We keep two separate
781 * hashtables for user and group dquots; and, these are global tables
782 * inside the XQM, not per-filesystem tables.
783 * The hash chain must be locked by caller, and it is left locked
784 * on return. Returning dquot is locked.
791 xfs_dquot_t **O_dqpp)
797 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
799 flist_locked = B_FALSE;
802 * Traverse the hashchain looking for a match
804 for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
806 * We already have the hashlock. We don't need the
807 * dqlock to look at the id field of the dquot, since the
808 * id can't be modified without the hashlock anyway.
810 if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
811 xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
813 * All in core dquots must be on the dqlist of mp
815 ASSERT(dqp->MPL_PREVP != NULL);
818 if (dqp->q_nrefs == 0) {
819 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
820 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
821 xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
824 * We may have raced with dqreclaim_one()
825 * (and lost). So, flag that we don't
826 * want the dquot to be reclaimed.
828 dqp->dq_flags |= XFS_DQ_WANT;
830 xfs_qm_freelist_lock(xfs_Gqm);
832 dqp->dq_flags &= ~(XFS_DQ_WANT);
834 flist_locked = B_TRUE;
838 * id couldn't have changed; we had the hashlock all
841 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
844 if (dqp->q_nrefs != 0) {
845 xfs_qm_freelist_unlock(xfs_Gqm);
846 flist_locked = B_FALSE;
849 * take it off the freelist
851 xfs_dqtrace_entry(dqp,
852 "DQLOOKUP: TAKEOFF FL");
853 XQM_FREELIST_REMOVE(dqp);
854 /* xfs_qm_freelist_print(&(xfs_Gqm->
866 xfs_qm_freelist_unlock(xfs_Gqm);
868 * move the dquot to the front of the hashchain
870 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
871 if (dqp->HL_PREVP != &qh->qh_next) {
872 xfs_dqtrace_entry(dqp,
873 "DQLOOKUP: HASH MOVETOFRONT");
874 if ((d = dqp->HL_NEXT))
875 d->HL_PREVP = dqp->HL_PREVP;
876 *(dqp->HL_PREVP) = d;
878 d->HL_PREVP = &dqp->HL_NEXT;
880 dqp->HL_PREVP = &qh->qh_next;
883 xfs_dqtrace_entry(dqp, "LOOKUP END");
885 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
891 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
896 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
897 * a locked dquot, doing an allocation (if requested) as needed.
898 * When both an inode and an id are given, the inode's id takes precedence.
899 * That is, if the id changes while we don't hold the ilock inside this
900 * function, the new dquot is returned, not necessarily the one requested
901 * in the id argument.
906 xfs_inode_t *ip, /* locked inode (optional) */
907 xfs_dqid_t id, /* uid/projid/gid depending on type */
908 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
909 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
910 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
917 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
918 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
919 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
920 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
923 h = XFS_DQ_HASH(mp, id, type);
926 if (xfs_do_dqerror) {
927 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
928 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
929 cmn_err(CE_DEBUG, "Returning error in dqget");
938 ASSERT(type == XFS_DQ_USER ||
939 type == XFS_DQ_PROJ ||
940 type == XFS_DQ_GROUP);
942 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
943 if (type == XFS_DQ_USER)
944 ASSERT(ip->i_udquot == NULL);
946 ASSERT(ip->i_gdquot == NULL);
952 * Look in the cache (hashtable).
953 * The chain is kept locked during lookup.
955 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
956 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
958 * The dquot was found, moved to the front of the chain,
959 * taken off the freelist if it was on it, and locked
960 * at this point. Just unlock the hashchain and return.
963 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
964 XFS_DQ_HASH_UNLOCK(h);
965 xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
966 return (0); /* success */
968 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
971 * Dquot cache miss. We don't want to keep the inode lock across
972 * a (potential) disk read. Also we don't want to deal with the lock
973 * ordering between quotainode and this inode. OTOH, dropping the inode
974 * lock here means dealing with a chown that can happen before
975 * we re-acquire the lock.
978 xfs_iunlock(ip, XFS_ILOCK_EXCL);
980 * Save the hashchain version stamp, and unlock the chain, so that
981 * we don't keep the lock across a disk read
983 version = h->qh_version;
984 XFS_DQ_HASH_UNLOCK(h);
987 * Allocate the dquot on the kernel heap, and read the ondisk
988 * portion off the disk. Also, do all the necessary initialization
989 * This can return ENOENT if dquot didn't exist on disk and we didn't
990 * ask it to allocate; ESRCH if quotas got turned off suddenly.
992 if ((error = xfs_qm_idtodq(mp, id, type,
993 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
997 xfs_ilock(ip, XFS_ILOCK_EXCL);
1002 * See if this is mount code calling to look at the overall quota limits
1003 * which are stored in the id == 0 user or group's dquot.
1004 * Since we may not have done a quotacheck by this point, just return
1005 * the dquot without attaching it to any hashtables, lists, etc, or even
1006 * taking a reference.
1007 * The caller must dqdestroy this once done.
1009 if (flags & XFS_QMOPT_DQSUSER) {
1016 * Dquot lock comes after hashlock in the lock ordering
1019 xfs_ilock(ip, XFS_ILOCK_EXCL);
1020 if (! XFS_IS_DQTYPE_ON(mp, type)) {
1021 /* inode stays locked on return */
1022 xfs_qm_dqdestroy(dqp);
1023 return XFS_ERROR(ESRCH);
1026 * A dquot could be attached to this inode by now, since
1027 * we had dropped the ilock.
1029 if (type == XFS_DQ_USER) {
1031 xfs_qm_dqdestroy(dqp);
1038 xfs_qm_dqdestroy(dqp);
1047 * Hashlock comes after ilock in lock order
1049 XFS_DQ_HASH_LOCK(h);
1050 if (version != h->qh_version) {
1051 xfs_dquot_t *tmpdqp;
1053 * Now, see if somebody else put the dquot in the
1054 * hashtable before us. This can happen because we didn't
1055 * keep the hashchain lock. We don't have to worry about
1056 * lock order between the two dquots here since dqp isn't
1057 * on any findable lists yet.
1059 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1061 * Duplicate found. Just throw away the new dquot
1064 xfs_qm_dqput(tmpdqp);
1065 XFS_DQ_HASH_UNLOCK(h);
1066 xfs_qm_dqdestroy(dqp);
1067 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1073 * Put the dquot at the beginning of the hash-chain and mp's list
1074 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1076 ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
1078 XQM_HASHLIST_INSERT(h, dqp);
1081 * Attach this dquot to this filesystem's list of all dquots,
1082 * kept inside the mount structure in m_quotainfo field
1084 xfs_qm_mplist_lock(mp);
1087 * We return a locked dquot to the caller, with a reference taken
1092 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
1094 xfs_qm_mplist_unlock(mp);
1095 XFS_DQ_HASH_UNLOCK(h);
1097 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
1098 xfs_dqtrace_entry(dqp, "DQGET DONE");
1105 * Release a reference to the dquot (decrement ref-count)
1106 * and unlock it. If there is a group quota attached to this
1107 * dquot, carefully release that too without tripping over
1108 * deadlocks'n'stuff.
1116 ASSERT(dqp->q_nrefs > 0);
1117 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1118 xfs_dqtrace_entry(dqp, "DQPUT");
1120 if (dqp->q_nrefs != 1) {
1127 * drop the dqlock and acquire the freelist and dqlock
1128 * in the right order; but try to get it out-of-order first
1130 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
1131 xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
1133 xfs_qm_freelist_lock(xfs_Gqm);
1140 /* We can't depend on nrefs being == 1 here */
1141 if (--dqp->q_nrefs == 0) {
1142 xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
1144 * insert at end of the freelist.
1146 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1149 * If we just added a udquot to the freelist, then
1150 * we want to release the gdquot reference that
1151 * it (probably) has. Otherwise it'll keep the
1152 * gdquot from getting reclaimed.
1154 if ((gdqp = dqp->q_gdquot)) {
1156 * Avoid a recursive dqput call
1159 dqp->q_gdquot = NULL;
1162 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1163 "@@@@@++ Free list (after append) @@@@@+");
1169 * If we had a group quota inside the user quota as a hint,
1176 xfs_qm_freelist_unlock(xfs_Gqm);
1180 * Release a dquot. Flush it if dirty, then dqput() it.
1181 * dquot must not be locked.
1188 xfs_dqtrace_entry(dqp, "DQRELE");
1192 * We don't care to flush it if the dquot is dirty here.
1193 * That will create stutters that we want to avoid.
1194 * Instead we do a delayed write when we try to reclaim
1195 * a dirty dquot. Also xfs_sync will take part of the burden...
1202 * Write a modified dquot to disk.
1203 * The dquot must be locked and the flush lock too taken by caller.
1204 * The flush lock will not be unlocked until the dquot reaches the disk,
1205 * but the dquot is free to be unlocked and modified by the caller
1206 * in the interim. Dquot is still locked on return. This behavior is
1207 * identical to that of inodes.
1216 xfs_disk_dquot_t *ddqp;
1219 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1220 ASSERT(!completion_done(&dqp->q_flush));
1221 xfs_dqtrace_entry(dqp, "DQFLUSH");
1224 * If not dirty, nada.
1226 if (!XFS_DQ_IS_DIRTY(dqp)) {
1232 * Cant flush a pinned dquot. Wait for it.
1234 xfs_qm_dqunpin_wait(dqp);
1237 * This may have been unpinned because the filesystem is shutting
1238 * down forcibly. If that's the case we must not write this dquot
1239 * to disk, because the log record didn't make it to disk!
1241 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1242 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1244 return XFS_ERROR(EIO);
1248 * Get the buffer containing the on-disk dquot
1249 * We don't need a transaction envelope because we know that the
1250 * the ondisk-dquot has already been allocated for.
1252 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1253 xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
1254 ASSERT(error != ENOENT);
1256 * Quotas could have gotten turned off (ESRCH)
1262 if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
1263 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
1264 xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
1265 return XFS_ERROR(EIO);
1268 /* This is the only portion of data that needs to persist */
1269 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1272 * Clear the dirty field and remember the flush lsn for later use.
1274 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1277 /* lsn is 64 bits */
1278 spin_lock(&mp->m_ail_lock);
1279 dqp->q_logitem.qli_flush_lsn = dqp->q_logitem.qli_item.li_lsn;
1280 spin_unlock(&mp->m_ail_lock);
1283 * Attach an iodone routine so that we can remove this dquot from the
1284 * AIL and release the flush lock once the dquot is synced to disk.
1286 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1287 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1289 * If the buffer is pinned then push on the log so we won't
1290 * get stuck waiting in the write for too long.
1292 if (XFS_BUF_ISPINNED(bp)) {
1293 xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
1294 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1297 if (flags & XFS_QMOPT_DELWRI) {
1298 xfs_bdwrite(mp, bp);
1299 } else if (flags & XFS_QMOPT_ASYNC) {
1300 error = xfs_bawrite(mp, bp);
1302 error = xfs_bwrite(mp, bp);
1304 xfs_dqtrace_entry(dqp, "DQFLUSH END");
1306 * dqp is still locked, but caller is free to unlock it now.
1313 * This is the dquot flushing I/O completion routine. It is called
1314 * from interrupt level when the buffer containing the dquot is
1315 * flushed to disk. It is responsible for removing the dquot logitem
1316 * from the AIL if it has not been re-logged, and unlocking the dquot's
1317 * flush lock. This behavior is very similar to that of inodes..
1321 xfs_qm_dqflush_done(
1323 xfs_dq_logitem_t *qip)
1327 dqp = qip->qli_dquot;
1330 * We only want to pull the item from the AIL if its
1331 * location in the log has not changed since we started the flush.
1332 * Thus, we only bother if the dquot's lsn has
1333 * not changed. First we check the lsn outside the lock
1334 * since it's cheaper, and then we recheck while
1335 * holding the lock before removing the dquot from the AIL.
1337 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1338 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1340 spin_lock(&dqp->q_mount->m_ail_lock);
1342 * xfs_trans_delete_ail() drops the AIL lock.
1344 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
1345 xfs_trans_delete_ail(dqp->q_mount,
1346 (xfs_log_item_t*)qip);
1348 spin_unlock(&dqp->q_mount->m_ail_lock);
1352 * Release the dq's flush lock since we're done with it.
1358 xfs_qm_dqlock_nowait(
1361 return mutex_trylock(&dqp->q_qlock);
1368 mutex_lock(&dqp->q_qlock);
1375 mutex_unlock(&(dqp->q_qlock));
1376 if (dqp->q_logitem.qli_dquot == dqp) {
1377 /* Once was dqp->q_mount, but might just have been cleared */
1378 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_mountp,
1379 (xfs_log_item_t*)&(dqp->q_logitem));
1385 xfs_dqunlock_nonotify(
1388 mutex_unlock(&(dqp->q_qlock));
1398 if (be32_to_cpu(d1->q_core.d_id) >
1399 be32_to_cpu(d2->q_core.d_id)) {
1417 * Take a dquot out of the mount's dqlist as well as the hashlist.
1418 * This is called via unmount as well as quotaoff, and the purge
1419 * will always succeed unless there are soft (temp) references
1422 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1423 * that we're returning! XXXsup - not cool.
1430 xfs_dqhash_t *thishash;
1431 xfs_mount_t *mp = dqp->q_mount;
1433 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
1434 ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
1438 * We really can't afford to purge a dquot that is
1439 * referenced, because these are hard refs.
1440 * It shouldn't happen in general because we went thru _all_ inodes in
1441 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1442 * However it is possible that we have dquots with temporary
1443 * references that are not attached to an inode. e.g. see xfs_setattr().
1445 if (dqp->q_nrefs != 0) {
1447 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
1451 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1454 * If we're turning off quotas, we have to make sure that, for
1455 * example, we don't delete quota disk blocks while dquots are
1456 * in the process of getting written to those disk blocks.
1457 * This dquot might well be on AIL, and we can't leave it there
1458 * if we're turning off quotas. Basically, we need this flush
1459 * lock, and are willing to block on it.
1461 if (!xfs_dqflock_nowait(dqp)) {
1463 * Block on the flush lock after nudging dquot buffer,
1466 xfs_qm_dqflock_pushbuf_wait(dqp);
1470 * XXXIf we're turning this type of quotas off, we don't care
1471 * about the dirty metadata sitting in this dquot. OTOH, if
1472 * we're unmounting, we do care, so we flush it and wait.
1474 if (XFS_DQ_IS_DIRTY(dqp)) {
1476 xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
1477 /* dqflush unlocks dqflock */
1479 * Given that dqpurge is a very rare occurrence, it is OK
1480 * that we're holding the hashlist and mplist locks
1481 * across the disk write. But, ... XXXsup
1483 * We don't care about getting disk errors here. We need
1484 * to purge this dquot anyway, so we go ahead regardless.
1486 error = xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
1488 xfs_fs_cmn_err(CE_WARN, mp,
1489 "xfs_qm_dqpurge: dquot %p flush failed", dqp);
1492 ASSERT(dqp->q_pincount == 0);
1493 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1494 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1496 thishash = dqp->q_hash;
1497 XQM_HASHLIST_REMOVE(thishash, dqp);
1498 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
1500 * XXX Move this to the front of the freelist, if we can get the
1503 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1505 dqp->q_mount = NULL;
1507 dqp->dq_flags = XFS_DQ_INACTIVE;
1508 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1511 XFS_DQ_HASH_UNLOCK(thishash);
1518 xfs_qm_dqprint(xfs_dquot_t *dqp)
1520 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1521 cmn_err(CE_DEBUG, "---- dquotID = %d",
1522 (int)be32_to_cpu(dqp->q_core.d_id));
1523 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
1524 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1525 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1526 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1527 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
1528 be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1529 (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
1530 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
1531 be64_to_cpu(dqp->q_core.d_blk_softlimit),
1532 (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
1533 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
1534 be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1535 (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
1536 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
1537 be64_to_cpu(dqp->q_core.d_ino_softlimit),
1538 (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
1539 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1540 be64_to_cpu(dqp->q_core.d_bcount),
1541 (int)be64_to_cpu(dqp->q_core.d_bcount));
1542 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1543 be64_to_cpu(dqp->q_core.d_icount),
1544 (int)be64_to_cpu(dqp->q_core.d_icount));
1545 cmn_err(CE_DEBUG, "---- btimer = %d",
1546 (int)be32_to_cpu(dqp->q_core.d_btimer));
1547 cmn_err(CE_DEBUG, "---- itimer = %d",
1548 (int)be32_to_cpu(dqp->q_core.d_itimer));
1549 cmn_err(CE_DEBUG, "---------------------------");
1554 * Give the buffer a little push if it is incore and
1555 * wait on the flush lock.
1558 xfs_qm_dqflock_pushbuf_wait(
1564 * Check to see if the dquot has been flushed delayed
1565 * write. If so, grab its buffer and send it
1566 * out immediately. We'll be able to acquire
1567 * the flush lock when the I/O completes.
1569 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
1570 XFS_QI_DQCHUNKLEN(dqp->q_mount),
1571 XFS_INCORE_TRYLOCK);
1573 if (XFS_BUF_ISDELAYWRITE(bp)) {
1575 if (XFS_BUF_ISPINNED(bp)) {
1576 xfs_log_force(dqp->q_mount,
1580 error = xfs_bawrite(dqp->q_mount, bp);
1582 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
1583 "xfs_qm_dqflock_pushbuf_wait: "
1584 "pushbuf error %d on dqp %p, bp %p",