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"
46 #include "xfs_buf_item.h"
47 #include "xfs_trans_space.h"
48 #include "xfs_trans_priv.h"
56 dquot hash-chain lock (hashlock)
57 xqm dquot freelist lock (freelistlock
58 mount's dquot list lock (mplistlock)
59 user dquot lock - lock ordering among dquots is based on the uid or gid
60 group dquot lock - similar to udquots. Between the two dquots, the udquot
61 has to be locked first.
62 pin lock - the dquot lock must be held to take this lock.
66 STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
69 xfs_buftarg_t *xfs_dqerror_target;
72 int xfs_dqerror_mod = 33;
75 static struct lock_class_key xfs_dquot_other_class;
78 * Allocate and initialize a dquot. We don't always allocate fresh memory;
79 * we try to reclaim a free dquot if the number of incore dquots are above
81 * The only field inside the core that gets initialized at this point
82 * is the d_id field. The idea is to fill in the entire q_core
83 * when we read in the on disk dquot.
92 boolean_t brandnewdquot;
94 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
96 dqp->q_core.d_id = cpu_to_be32(id);
100 * No need to re-initialize these if this is a reclaimed dquot.
103 dqp->dq_flnext = dqp->dq_flprev = dqp;
104 mutex_init(&dqp->q_qlock);
105 init_waitqueue_head(&dqp->q_pinwait);
108 * Because we want to use a counting completion, complete
109 * the flush completion once to allow a single access to
110 * the flush completion without blocking.
112 init_completion(&dqp->q_flush);
113 complete(&dqp->q_flush);
115 #ifdef XFS_DQUOT_TRACE
116 dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_NOFS);
117 xfs_dqtrace_entry(dqp, "DQINIT");
121 * Only the q_core portion was zeroed in dqreclaim_one().
122 * So, we need to reset others.
126 dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
127 dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
128 dqp->q_bufoffset = 0;
129 dqp->q_fileoffset = 0;
130 dqp->q_transp = NULL;
131 dqp->q_gdquot = NULL;
132 dqp->q_res_bcount = 0;
133 dqp->q_res_icount = 0;
134 dqp->q_res_rtbcount = 0;
135 atomic_set(&dqp->q_pincount, 0);
137 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
139 #ifdef XFS_DQUOT_TRACE
140 ASSERT(dqp->q_trace);
141 xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
146 * In either case we need to make sure group quotas have a different
147 * lock class than user quotas, to make sure lockdep knows we can
148 * locks of one of each at the same time.
150 if (!(type & XFS_DQ_USER))
151 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
154 * log item gets initialized later
160 * This is called to free all the memory associated with a dquot
166 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
168 mutex_destroy(&dqp->q_qlock);
169 sv_destroy(&dqp->q_pinwait);
171 #ifdef XFS_DQUOT_TRACE
173 ktrace_free(dqp->q_trace);
176 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
177 atomic_dec(&xfs_Gqm->qm_totaldquots);
181 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
190 * Caller has zero'd the entire dquot 'chunk' already.
192 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
193 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
194 d->dd_diskdq.d_id = cpu_to_be32(id);
195 d->dd_diskdq.d_flags = type;
199 #ifdef XFS_DQUOT_TRACE
201 * Dquot tracing for debugging.
211 xfs_dquot_t *udqp = NULL;
214 ASSERT(dqp->q_trace);
219 ktrace_enter(dqp->q_trace,
220 (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
222 (void *)(__psint_t)dqp->q_nrefs,
223 (void *)(__psint_t)dqp->dq_flags,
224 (void *)(__psint_t)dqp->q_res_bcount,
225 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_bcount),
226 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_icount),
227 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_blk_hardlimit),
228 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_blk_softlimit),
229 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_ino_hardlimit),
230 (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_ino_softlimit),
231 (void *)(__psint_t)be32_to_cpu(dqp->q_core.d_id),
232 (void *)(__psint_t)current_pid(),
233 (void *)(__psint_t)ino,
234 (void *)(__psint_t)retaddr,
235 (void *)(__psint_t)udqp);
242 * If default limits are in force, push them into the dquot now.
243 * We overwrite the dquot limits only if they are zero and this
244 * is not the root dquot.
247 xfs_qm_adjust_dqlimits(
251 xfs_quotainfo_t *q = mp->m_quotainfo;
255 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
256 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
257 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
258 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
259 if (q->qi_isoftlimit && !d->d_ino_softlimit)
260 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
261 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
262 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
263 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
264 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
265 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
266 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
270 * Check the limits and timers of a dquot and start or reset timers
272 * This gets called even when quota enforcement is OFF, which makes our
273 * life a little less complicated. (We just don't reject any quota
274 * reservations in that case, when enforcement is off).
275 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
277 * In contrast, warnings are a little different in that they don't
278 * 'automatically' get started when limits get exceeded. They do
279 * get reset to zero, however, when we find the count to be under
280 * the soft limit (they are only ever set non-zero via userspace).
283 xfs_qm_adjust_dqtimers(
290 if (d->d_blk_hardlimit)
291 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
292 be64_to_cpu(d->d_blk_hardlimit));
293 if (d->d_ino_hardlimit)
294 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
295 be64_to_cpu(d->d_ino_hardlimit));
296 if (d->d_rtb_hardlimit)
297 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
298 be64_to_cpu(d->d_rtb_hardlimit));
301 if ((d->d_blk_softlimit &&
302 (be64_to_cpu(d->d_bcount) >=
303 be64_to_cpu(d->d_blk_softlimit))) ||
304 (d->d_blk_hardlimit &&
305 (be64_to_cpu(d->d_bcount) >=
306 be64_to_cpu(d->d_blk_hardlimit)))) {
307 d->d_btimer = cpu_to_be32(get_seconds() +
308 XFS_QI_BTIMELIMIT(mp));
313 if ((!d->d_blk_softlimit ||
314 (be64_to_cpu(d->d_bcount) <
315 be64_to_cpu(d->d_blk_softlimit))) &&
316 (!d->d_blk_hardlimit ||
317 (be64_to_cpu(d->d_bcount) <
318 be64_to_cpu(d->d_blk_hardlimit)))) {
324 if ((d->d_ino_softlimit &&
325 (be64_to_cpu(d->d_icount) >=
326 be64_to_cpu(d->d_ino_softlimit))) ||
327 (d->d_ino_hardlimit &&
328 (be64_to_cpu(d->d_icount) >=
329 be64_to_cpu(d->d_ino_hardlimit)))) {
330 d->d_itimer = cpu_to_be32(get_seconds() +
331 XFS_QI_ITIMELIMIT(mp));
336 if ((!d->d_ino_softlimit ||
337 (be64_to_cpu(d->d_icount) <
338 be64_to_cpu(d->d_ino_softlimit))) &&
339 (!d->d_ino_hardlimit ||
340 (be64_to_cpu(d->d_icount) <
341 be64_to_cpu(d->d_ino_hardlimit)))) {
346 if (!d->d_rtbtimer) {
347 if ((d->d_rtb_softlimit &&
348 (be64_to_cpu(d->d_rtbcount) >=
349 be64_to_cpu(d->d_rtb_softlimit))) ||
350 (d->d_rtb_hardlimit &&
351 (be64_to_cpu(d->d_rtbcount) >=
352 be64_to_cpu(d->d_rtb_hardlimit)))) {
353 d->d_rtbtimer = cpu_to_be32(get_seconds() +
354 XFS_QI_RTBTIMELIMIT(mp));
359 if ((!d->d_rtb_softlimit ||
360 (be64_to_cpu(d->d_rtbcount) <
361 be64_to_cpu(d->d_rtb_softlimit))) &&
362 (!d->d_rtb_hardlimit ||
363 (be64_to_cpu(d->d_rtbcount) <
364 be64_to_cpu(d->d_rtb_hardlimit)))) {
371 * initialize a buffer full of dquots and log the whole thing
374 xfs_qm_init_dquot_blk(
385 ASSERT(XFS_BUF_ISBUSY(bp));
386 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
388 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
391 * ID of the first dquot in the block - id's are zero based.
393 curid = id - (id % XFS_QM_DQPERBLK(mp));
395 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
396 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
397 xfs_qm_dqinit_core(curid, type, d);
398 xfs_trans_dquot_buf(tp, bp,
399 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
400 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
401 XFS_BLI_GDQUOT_BUF)));
402 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
408 * Allocate a block and fill it with dquots.
409 * This is called when the bmapi finds a hole.
417 xfs_fileoff_t offset_fsb,
420 xfs_fsblock_t firstblock;
421 xfs_bmap_free_t flist;
423 int nmaps, error, committed;
425 xfs_trans_t *tp = *tpp;
428 xfs_dqtrace_entry(dqp, "DQALLOC");
431 * Initialize the bmap freelist prior to calling bmapi code.
433 xfs_bmap_init(&flist, &firstblock);
434 xfs_ilock(quotip, XFS_ILOCK_EXCL);
436 * Return if this type of quotas is turned off while we didn't
439 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
440 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
445 * xfs_trans_commit normally decrements the vnode ref count
446 * when it unlocks the inode. Since we want to keep the quota
447 * inode around, we bump the vnode ref count now.
451 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
453 if ((error = xfs_bmapi(tp, quotip,
454 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
455 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
457 XFS_QM_DQALLOC_SPACE_RES(mp),
458 &map, &nmaps, &flist, NULL))) {
461 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
463 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
464 (map.br_startblock != HOLESTARTBLOCK));
467 * Keep track of the blkno to save a lookup later
469 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
471 /* now we can just get the buffer (there's nothing to read yet) */
472 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
474 XFS_QI_DQCHUNKLEN(mp),
476 if (!bp || (error = XFS_BUF_GETERROR(bp)))
479 * Make a chunk of dquots out of this buffer and log
482 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
483 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
486 * xfs_bmap_finish() may commit the current transaction and
487 * start a second transaction if the freelist is not empty.
489 * Since we still want to modify this buffer, we need to
490 * ensure that the buffer is not released on commit of
491 * the first transaction and ensure the buffer is added to the
492 * second transaction.
494 * If there is only one transaction then don't stop the buffer
495 * from being released when it commits later on.
498 xfs_trans_bhold(tp, bp);
500 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
506 xfs_trans_bjoin(tp, bp);
508 xfs_trans_bhold_release(tp, bp);
515 xfs_bmap_cancel(&flist);
517 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
523 * Maps a dquot to the buffer containing its on-disk version.
524 * This returns a ptr to the buffer containing the on-disk dquot
525 * in the bpp param, and a ptr to the on-disk dquot within that buffer
531 xfs_disk_dquot_t **O_ddpp,
540 xfs_disk_dquot_t *ddq;
543 xfs_trans_t *tp = (tpp ? *tpp : NULL);
546 id = be32_to_cpu(dqp->q_core.d_id);
551 * If we don't know where the dquot lives, find out.
553 if (dqp->q_blkno == (xfs_daddr_t) 0) {
554 /* We use the id as an index */
555 dqp->q_fileoffset = (xfs_fileoff_t)id / XFS_QM_DQPERBLK(mp);
557 quotip = XFS_DQ_TO_QIP(dqp);
558 xfs_ilock(quotip, XFS_ILOCK_SHARED);
560 * Return if this type of quotas is turned off while we didn't
563 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
564 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
568 * Find the block map; no allocations yet
570 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
571 XFS_DQUOT_CLUSTER_SIZE_FSB,
573 NULL, 0, &map, &nmaps, NULL, NULL);
575 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
579 ASSERT(map.br_blockcount == 1);
582 * offset of dquot in the (fixed sized) dquot chunk.
584 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
586 if (map.br_startblock == HOLESTARTBLOCK) {
588 * We don't allocate unless we're asked to
590 if (!(flags & XFS_QMOPT_DQALLOC))
594 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
595 dqp->q_fileoffset, &bp)))
601 * store the blkno etc so that we don't have to do the
602 * mapping all the time
604 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
607 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
608 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
611 * Read in the buffer, unless we've just done the allocation
612 * (in which case we already have the buf).
615 xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
616 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
618 XFS_QI_DQCHUNKLEN(mp),
623 return XFS_ERROR(error);
625 ASSERT(XFS_BUF_ISBUSY(bp));
626 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
629 * calculate the location of the dquot inside the buffer.
631 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
634 * A simple sanity check in case we got a corrupted dquot...
636 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
637 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
639 if (!(flags & XFS_QMOPT_DQREPAIR)) {
640 xfs_trans_brelse(tp, bp);
641 return XFS_ERROR(EIO);
643 XFS_BUF_BUSY(bp); /* We dirtied this */
654 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
655 * and release the buffer immediately.
663 xfs_dquot_t *dqp, /* dquot to get filled in */
666 xfs_disk_dquot_t *ddqp;
674 * get a pointer to the on-disk dquot and the buffer containing it
675 * dqp already knows its own type (GROUP/USER).
677 xfs_dqtrace_entry(dqp, "DQREAD");
678 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
683 /* copy everything from disk dquot to the incore dquot */
684 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
685 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
686 xfs_qm_dquot_logitem_init(dqp);
689 * Reservation counters are defined as reservation plus current usage
690 * to avoid having to add everytime.
692 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
693 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
694 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
696 /* Mark the buf so that this will stay incore a little longer */
697 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
700 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
701 * So we need to release with xfs_trans_brelse().
702 * The strategy here is identical to that of inodes; we lock
703 * the dquot in xfs_qm_dqget() before making it accessible to
704 * others. This is because dquots, like inodes, need a good level of
705 * concurrency, and we don't want to take locks on the entire buffers
706 * for dquot accesses.
707 * Note also that the dquot buffer may even be dirty at this point, if
708 * this particular dquot was repaired. We still aren't afraid to
709 * brelse it because we have the changes incore.
711 ASSERT(XFS_BUF_ISBUSY(bp));
712 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
713 xfs_trans_brelse(tp, bp);
720 * allocate an incore dquot from the kernel heap,
721 * and fill its core with quota information kept on disk.
722 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
723 * if it wasn't already allocated.
728 xfs_dqid_t id, /* gid or uid, depending on type */
729 uint type, /* UDQUOT or GDQUOT */
730 uint flags, /* DQALLOC, DQREPAIR */
731 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
738 dqp = xfs_qm_dqinit(mp, id, type);
740 if (flags & XFS_QMOPT_DQALLOC) {
741 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
742 if ((error = xfs_trans_reserve(tp,
743 XFS_QM_DQALLOC_SPACE_RES(mp),
744 XFS_WRITE_LOG_RES(mp) +
745 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
748 XFS_TRANS_PERM_LOG_RES,
749 XFS_WRITE_LOG_COUNT))) {
753 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
757 * Read it from disk; xfs_dqread() takes care of
758 * all the necessary initialization of dquot's fields (locks, etc)
760 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
762 * This can happen if quotas got turned off (ESRCH),
763 * or if the dquot didn't exist on disk and we ask to
766 xfs_dqtrace_entry(dqp, "DQREAD FAIL");
767 cancelflags |= XFS_TRANS_ABORT;
771 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
781 xfs_trans_cancel(tp, cancelflags);
783 xfs_qm_dqdestroy(dqp);
789 * Lookup a dquot in the incore dquot hashtable. We keep two separate
790 * hashtables for user and group dquots; and, these are global tables
791 * inside the XQM, not per-filesystem tables.
792 * The hash chain must be locked by caller, and it is left locked
793 * on return. Returning dquot is locked.
800 xfs_dquot_t **O_dqpp)
806 ASSERT(mutex_is_locked(&qh->qh_lock));
808 flist_locked = B_FALSE;
811 * Traverse the hashchain looking for a match
813 for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
815 * We already have the hashlock. We don't need the
816 * dqlock to look at the id field of the dquot, since the
817 * id can't be modified without the hashlock anyway.
819 if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
820 xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
822 * All in core dquots must be on the dqlist of mp
824 ASSERT(dqp->MPL_PREVP != NULL);
827 if (dqp->q_nrefs == 0) {
828 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
829 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
830 xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
833 * We may have raced with dqreclaim_one()
834 * (and lost). So, flag that we don't
835 * want the dquot to be reclaimed.
837 dqp->dq_flags |= XFS_DQ_WANT;
839 xfs_qm_freelist_lock(xfs_Gqm);
841 dqp->dq_flags &= ~(XFS_DQ_WANT);
843 flist_locked = B_TRUE;
847 * id couldn't have changed; we had the hashlock all
850 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
853 if (dqp->q_nrefs != 0) {
854 xfs_qm_freelist_unlock(xfs_Gqm);
855 flist_locked = B_FALSE;
858 * take it off the freelist
860 xfs_dqtrace_entry(dqp,
861 "DQLOOKUP: TAKEOFF FL");
862 XQM_FREELIST_REMOVE(dqp);
863 /* xfs_qm_freelist_print(&(xfs_Gqm->
875 xfs_qm_freelist_unlock(xfs_Gqm);
877 * move the dquot to the front of the hashchain
879 ASSERT(mutex_is_locked(&qh->qh_lock));
880 if (dqp->HL_PREVP != &qh->qh_next) {
881 xfs_dqtrace_entry(dqp,
882 "DQLOOKUP: HASH MOVETOFRONT");
883 if ((d = dqp->HL_NEXT))
884 d->HL_PREVP = dqp->HL_PREVP;
885 *(dqp->HL_PREVP) = d;
887 d->HL_PREVP = &dqp->HL_NEXT;
889 dqp->HL_PREVP = &qh->qh_next;
892 xfs_dqtrace_entry(dqp, "LOOKUP END");
894 ASSERT(mutex_is_locked(&qh->qh_lock));
900 ASSERT(mutex_is_locked(&qh->qh_lock));
905 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
906 * a locked dquot, doing an allocation (if requested) as needed.
907 * When both an inode and an id are given, the inode's id takes precedence.
908 * That is, if the id changes while we don't hold the ilock inside this
909 * function, the new dquot is returned, not necessarily the one requested
910 * in the id argument.
915 xfs_inode_t *ip, /* locked inode (optional) */
916 xfs_dqid_t id, /* uid/projid/gid depending on type */
917 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
918 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
919 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
926 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
927 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
928 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
929 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
932 h = XFS_DQ_HASH(mp, id, type);
935 if (xfs_do_dqerror) {
936 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
937 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
938 cmn_err(CE_DEBUG, "Returning error in dqget");
947 ASSERT(type == XFS_DQ_USER ||
948 type == XFS_DQ_PROJ ||
949 type == XFS_DQ_GROUP);
951 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
952 if (type == XFS_DQ_USER)
953 ASSERT(ip->i_udquot == NULL);
955 ASSERT(ip->i_gdquot == NULL);
958 mutex_lock(&h->qh_lock);
961 * Look in the cache (hashtable).
962 * The chain is kept locked during lookup.
964 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
965 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
967 * The dquot was found, moved to the front of the chain,
968 * taken off the freelist if it was on it, and locked
969 * at this point. Just unlock the hashchain and return.
972 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
973 mutex_unlock(&h->qh_lock);
974 xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
975 return (0); /* success */
977 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
980 * Dquot cache miss. We don't want to keep the inode lock across
981 * a (potential) disk read. Also we don't want to deal with the lock
982 * ordering between quotainode and this inode. OTOH, dropping the inode
983 * lock here means dealing with a chown that can happen before
984 * we re-acquire the lock.
987 xfs_iunlock(ip, XFS_ILOCK_EXCL);
989 * Save the hashchain version stamp, and unlock the chain, so that
990 * we don't keep the lock across a disk read
992 version = h->qh_version;
993 mutex_unlock(&h->qh_lock);
996 * Allocate the dquot on the kernel heap, and read the ondisk
997 * portion off the disk. Also, do all the necessary initialization
998 * This can return ENOENT if dquot didn't exist on disk and we didn't
999 * ask it to allocate; ESRCH if quotas got turned off suddenly.
1001 if ((error = xfs_qm_idtodq(mp, id, type,
1002 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
1006 xfs_ilock(ip, XFS_ILOCK_EXCL);
1011 * See if this is mount code calling to look at the overall quota limits
1012 * which are stored in the id == 0 user or group's dquot.
1013 * Since we may not have done a quotacheck by this point, just return
1014 * the dquot without attaching it to any hashtables, lists, etc, or even
1015 * taking a reference.
1016 * The caller must dqdestroy this once done.
1018 if (flags & XFS_QMOPT_DQSUSER) {
1025 * Dquot lock comes after hashlock in the lock ordering
1028 xfs_ilock(ip, XFS_ILOCK_EXCL);
1029 if (! XFS_IS_DQTYPE_ON(mp, type)) {
1030 /* inode stays locked on return */
1031 xfs_qm_dqdestroy(dqp);
1032 return XFS_ERROR(ESRCH);
1035 * A dquot could be attached to this inode by now, since
1036 * we had dropped the ilock.
1038 if (type == XFS_DQ_USER) {
1040 xfs_qm_dqdestroy(dqp);
1047 xfs_qm_dqdestroy(dqp);
1056 * Hashlock comes after ilock in lock order
1058 mutex_lock(&h->qh_lock);
1059 if (version != h->qh_version) {
1060 xfs_dquot_t *tmpdqp;
1062 * Now, see if somebody else put the dquot in the
1063 * hashtable before us. This can happen because we didn't
1064 * keep the hashchain lock. We don't have to worry about
1065 * lock order between the two dquots here since dqp isn't
1066 * on any findable lists yet.
1068 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1070 * Duplicate found. Just throw away the new dquot
1073 xfs_qm_dqput(tmpdqp);
1074 mutex_unlock(&h->qh_lock);
1075 xfs_qm_dqdestroy(dqp);
1076 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1082 * Put the dquot at the beginning of the hash-chain and mp's list
1083 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1085 ASSERT(mutex_is_locked(&h->qh_lock));
1087 XQM_HASHLIST_INSERT(h, dqp);
1090 * Attach this dquot to this filesystem's list of all dquots,
1091 * kept inside the mount structure in m_quotainfo field
1093 xfs_qm_mplist_lock(mp);
1096 * We return a locked dquot to the caller, with a reference taken
1101 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
1103 xfs_qm_mplist_unlock(mp);
1104 mutex_unlock(&h->qh_lock);
1106 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
1107 xfs_dqtrace_entry(dqp, "DQGET DONE");
1114 * Release a reference to the dquot (decrement ref-count)
1115 * and unlock it. If there is a group quota attached to this
1116 * dquot, carefully release that too without tripping over
1117 * deadlocks'n'stuff.
1125 ASSERT(dqp->q_nrefs > 0);
1126 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1127 xfs_dqtrace_entry(dqp, "DQPUT");
1129 if (dqp->q_nrefs != 1) {
1136 * drop the dqlock and acquire the freelist and dqlock
1137 * in the right order; but try to get it out-of-order first
1139 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
1140 xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
1142 xfs_qm_freelist_lock(xfs_Gqm);
1149 /* We can't depend on nrefs being == 1 here */
1150 if (--dqp->q_nrefs == 0) {
1151 xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
1153 * insert at end of the freelist.
1155 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1158 * If we just added a udquot to the freelist, then
1159 * we want to release the gdquot reference that
1160 * it (probably) has. Otherwise it'll keep the
1161 * gdquot from getting reclaimed.
1163 if ((gdqp = dqp->q_gdquot)) {
1165 * Avoid a recursive dqput call
1168 dqp->q_gdquot = NULL;
1171 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1172 "@@@@@++ Free list (after append) @@@@@+");
1178 * If we had a group quota inside the user quota as a hint,
1185 xfs_qm_freelist_unlock(xfs_Gqm);
1189 * Release a dquot. Flush it if dirty, then dqput() it.
1190 * dquot must not be locked.
1199 xfs_dqtrace_entry(dqp, "DQRELE");
1203 * We don't care to flush it if the dquot is dirty here.
1204 * That will create stutters that we want to avoid.
1205 * Instead we do a delayed write when we try to reclaim
1206 * a dirty dquot. Also xfs_sync will take part of the burden...
1213 * Write a modified dquot to disk.
1214 * The dquot must be locked and the flush lock too taken by caller.
1215 * The flush lock will not be unlocked until the dquot reaches the disk,
1216 * but the dquot is free to be unlocked and modified by the caller
1217 * in the interim. Dquot is still locked on return. This behavior is
1218 * identical to that of inodes.
1227 xfs_disk_dquot_t *ddqp;
1230 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1231 ASSERT(!completion_done(&dqp->q_flush));
1232 xfs_dqtrace_entry(dqp, "DQFLUSH");
1235 * If not dirty, or it's pinned and we are not supposed to
1238 if (!XFS_DQ_IS_DIRTY(dqp) ||
1239 (!(flags & XFS_QMOPT_SYNC) && atomic_read(&dqp->q_pincount) > 0)) {
1243 xfs_qm_dqunpin_wait(dqp);
1246 * This may have been unpinned because the filesystem is shutting
1247 * down forcibly. If that's the case we must not write this dquot
1248 * to disk, because the log record didn't make it to disk!
1250 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1251 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1253 return XFS_ERROR(EIO);
1257 * Get the buffer containing the on-disk dquot
1258 * We don't need a transaction envelope because we know that the
1259 * the ondisk-dquot has already been allocated for.
1261 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1262 xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
1263 ASSERT(error != ENOENT);
1265 * Quotas could have gotten turned off (ESRCH)
1271 if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
1272 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
1273 xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
1274 return XFS_ERROR(EIO);
1277 /* This is the only portion of data that needs to persist */
1278 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1281 * Clear the dirty field and remember the flush lsn for later use.
1283 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1286 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1287 &dqp->q_logitem.qli_item.li_lsn);
1290 * Attach an iodone routine so that we can remove this dquot from the
1291 * AIL and release the flush lock once the dquot is synced to disk.
1293 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1294 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1296 * If the buffer is pinned then push on the log so we won't
1297 * get stuck waiting in the write for too long.
1299 if (XFS_BUF_ISPINNED(bp)) {
1300 xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
1301 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1304 if (flags & XFS_QMOPT_DELWRI) {
1305 xfs_bdwrite(mp, bp);
1306 } else if (flags & XFS_QMOPT_ASYNC) {
1307 error = xfs_bawrite(mp, bp);
1309 error = xfs_bwrite(mp, bp);
1311 xfs_dqtrace_entry(dqp, "DQFLUSH END");
1313 * dqp is still locked, but caller is free to unlock it now.
1320 * This is the dquot flushing I/O completion routine. It is called
1321 * from interrupt level when the buffer containing the dquot is
1322 * flushed to disk. It is responsible for removing the dquot logitem
1323 * from the AIL if it has not been re-logged, and unlocking the dquot's
1324 * flush lock. This behavior is very similar to that of inodes..
1328 xfs_qm_dqflush_done(
1330 xfs_dq_logitem_t *qip)
1333 struct xfs_ail *ailp;
1335 dqp = qip->qli_dquot;
1336 ailp = qip->qli_item.li_ailp;
1339 * We only want to pull the item from the AIL if its
1340 * location in the log has not changed since we started the flush.
1341 * Thus, we only bother if the dquot's lsn has
1342 * not changed. First we check the lsn outside the lock
1343 * since it's cheaper, and then we recheck while
1344 * holding the lock before removing the dquot from the AIL.
1346 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1347 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1349 /* xfs_trans_ail_delete() drops the AIL lock. */
1350 spin_lock(&ailp->xa_lock);
1351 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
1352 xfs_trans_ail_delete(ailp, (xfs_log_item_t*)qip);
1354 spin_unlock(&ailp->xa_lock);
1358 * Release the dq's flush lock since we're done with it.
1364 xfs_qm_dqlock_nowait(
1367 return mutex_trylock(&dqp->q_qlock);
1374 mutex_lock(&dqp->q_qlock);
1381 mutex_unlock(&(dqp->q_qlock));
1382 if (dqp->q_logitem.qli_dquot == dqp) {
1383 /* Once was dqp->q_mount, but might just have been cleared */
1384 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
1385 (xfs_log_item_t*)&(dqp->q_logitem));
1391 xfs_dqunlock_nonotify(
1394 mutex_unlock(&(dqp->q_qlock));
1398 * Lock two xfs_dquot structures.
1400 * To avoid deadlocks we always lock the quota structure with
1401 * the lowerd id first.
1410 if (be32_to_cpu(d1->q_core.d_id) >
1411 be32_to_cpu(d2->q_core.d_id)) {
1412 mutex_lock(&d2->q_qlock);
1413 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1415 mutex_lock(&d1->q_qlock);
1416 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1419 mutex_lock(&d1->q_qlock);
1421 mutex_lock(&d2->q_qlock);
1427 * Take a dquot out of the mount's dqlist as well as the hashlist.
1428 * This is called via unmount as well as quotaoff, and the purge
1429 * will always succeed unless there are soft (temp) references
1432 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1433 * that we're returning! XXXsup - not cool.
1440 xfs_dqhash_t *thishash;
1441 xfs_mount_t *mp = dqp->q_mount;
1443 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
1444 ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
1448 * We really can't afford to purge a dquot that is
1449 * referenced, because these are hard refs.
1450 * It shouldn't happen in general because we went thru _all_ inodes in
1451 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1452 * However it is possible that we have dquots with temporary
1453 * references that are not attached to an inode. e.g. see xfs_setattr().
1455 if (dqp->q_nrefs != 0) {
1457 mutex_unlock(&dqp->q_hash->qh_lock);
1461 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1464 * If we're turning off quotas, we have to make sure that, for
1465 * example, we don't delete quota disk blocks while dquots are
1466 * in the process of getting written to those disk blocks.
1467 * This dquot might well be on AIL, and we can't leave it there
1468 * if we're turning off quotas. Basically, we need this flush
1469 * lock, and are willing to block on it.
1471 if (!xfs_dqflock_nowait(dqp)) {
1473 * Block on the flush lock after nudging dquot buffer,
1476 xfs_qm_dqflock_pushbuf_wait(dqp);
1480 * XXXIf we're turning this type of quotas off, we don't care
1481 * about the dirty metadata sitting in this dquot. OTOH, if
1482 * we're unmounting, we do care, so we flush it and wait.
1484 if (XFS_DQ_IS_DIRTY(dqp)) {
1486 xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
1487 /* dqflush unlocks dqflock */
1489 * Given that dqpurge is a very rare occurrence, it is OK
1490 * that we're holding the hashlist and mplist locks
1491 * across the disk write. But, ... XXXsup
1493 * We don't care about getting disk errors here. We need
1494 * to purge this dquot anyway, so we go ahead regardless.
1496 error = xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
1498 xfs_fs_cmn_err(CE_WARN, mp,
1499 "xfs_qm_dqpurge: dquot %p flush failed", dqp);
1502 ASSERT(atomic_read(&dqp->q_pincount) == 0);
1503 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1504 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1506 thishash = dqp->q_hash;
1507 XQM_HASHLIST_REMOVE(thishash, dqp);
1508 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
1510 * XXX Move this to the front of the freelist, if we can get the
1513 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1515 dqp->q_mount = NULL;
1517 dqp->dq_flags = XFS_DQ_INACTIVE;
1518 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1521 mutex_unlock(&thishash->qh_lock);
1528 xfs_qm_dqprint(xfs_dquot_t *dqp)
1530 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1531 cmn_err(CE_DEBUG, "---- dquotID = %d",
1532 (int)be32_to_cpu(dqp->q_core.d_id));
1533 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
1534 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1535 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1536 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1537 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
1538 be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1539 (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
1540 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
1541 be64_to_cpu(dqp->q_core.d_blk_softlimit),
1542 (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
1543 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
1544 be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1545 (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
1546 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
1547 be64_to_cpu(dqp->q_core.d_ino_softlimit),
1548 (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
1549 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1550 be64_to_cpu(dqp->q_core.d_bcount),
1551 (int)be64_to_cpu(dqp->q_core.d_bcount));
1552 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1553 be64_to_cpu(dqp->q_core.d_icount),
1554 (int)be64_to_cpu(dqp->q_core.d_icount));
1555 cmn_err(CE_DEBUG, "---- btimer = %d",
1556 (int)be32_to_cpu(dqp->q_core.d_btimer));
1557 cmn_err(CE_DEBUG, "---- itimer = %d",
1558 (int)be32_to_cpu(dqp->q_core.d_itimer));
1559 cmn_err(CE_DEBUG, "---------------------------");
1564 * Give the buffer a little push if it is incore and
1565 * wait on the flush lock.
1568 xfs_qm_dqflock_pushbuf_wait(
1574 * Check to see if the dquot has been flushed delayed
1575 * write. If so, grab its buffer and send it
1576 * out immediately. We'll be able to acquire
1577 * the flush lock when the I/O completes.
1579 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
1580 XFS_QI_DQCHUNKLEN(dqp->q_mount),
1581 XFS_INCORE_TRYLOCK);
1583 if (XFS_BUF_ISDELAYWRITE(bp)) {
1585 if (XFS_BUF_ISPINNED(bp)) {
1586 xfs_log_force(dqp->q_mount,
1590 error = xfs_bawrite(dqp->q_mount, bp);
1592 xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
1593 "xfs_qm_dqflock_pushbuf_wait: "
1594 "pushbuf error %d on dqp %p, bp %p",