2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
37 #include "xfs_trans.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_trans_space.h"
68 #include "xfs_trans_priv.h"
77 dquot hash-chain lock (hashlock)
78 xqm dquot freelist lock (freelistlock
79 mount's dquot list lock (mplistlock)
80 user dquot lock - lock ordering among dquots is based on the uid or gid
81 group dquot lock - similar to udquots. Between the two dquots, the udquot
82 has to be locked first.
83 pin lock - the dquot lock must be held to take this lock.
87 STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
90 xfs_buftarg_t *xfs_dqerror_target;
93 int xfs_dqerror_mod = 33;
97 * Allocate and initialize a dquot. We don't always allocate fresh memory;
98 * we try to reclaim a free dquot if the number of incore dquots are above
100 * The only field inside the core that gets initialized at this point
101 * is the d_id field. The idea is to fill in the entire q_core
102 * when we read in the on disk dquot.
111 boolean_t brandnewdquot;
113 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
114 dqp->dq_flags = type;
115 INT_SET(dqp->q_core.d_id, ARCH_CONVERT, id);
119 * No need to re-initialize these if this is a reclaimed dquot.
122 dqp->dq_flnext = dqp->dq_flprev = dqp;
123 mutex_init(&dqp->q_qlock, MUTEX_DEFAULT, "xdq");
124 initnsema(&dqp->q_flock, 1, "fdq");
125 sv_init(&dqp->q_pinwait, SV_DEFAULT, "pdq");
127 #ifdef XFS_DQUOT_TRACE
128 dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_SLEEP);
129 xfs_dqtrace_entry(dqp, "DQINIT");
133 * Only the q_core portion was zeroed in dqreclaim_one().
134 * So, we need to reset others.
138 dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
139 dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
140 dqp->q_bufoffset = 0;
141 dqp->q_fileoffset = 0;
142 dqp->q_transp = NULL;
143 dqp->q_gdquot = NULL;
144 dqp->q_res_bcount = 0;
145 dqp->q_res_icount = 0;
146 dqp->q_res_rtbcount = 0;
149 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
151 #ifdef XFS_DQUOT_TRACE
152 ASSERT(dqp->q_trace);
153 xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
158 * log item gets initialized later
164 * This is called to free all the memory associated with a dquot
170 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
172 mutex_destroy(&dqp->q_qlock);
173 freesema(&dqp->q_flock);
174 sv_destroy(&dqp->q_pinwait);
176 #ifdef XFS_DQUOT_TRACE
178 ktrace_free(dqp->q_trace);
181 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
182 atomic_dec(&xfs_Gqm->qm_totaldquots);
186 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
195 * Caller has zero'd the entire dquot 'chunk' already.
197 INT_SET(d->dd_diskdq.d_magic, ARCH_CONVERT, XFS_DQUOT_MAGIC);
198 INT_SET(d->dd_diskdq.d_version, ARCH_CONVERT, XFS_DQUOT_VERSION);
199 INT_SET(d->dd_diskdq.d_id, ARCH_CONVERT, id);
200 INT_SET(d->dd_diskdq.d_flags, ARCH_CONVERT, type);
204 #ifdef XFS_DQUOT_TRACE
206 * Dquot tracing for debugging.
216 xfs_dquot_t *udqp = NULL;
219 ASSERT(dqp->q_trace);
224 ktrace_enter(dqp->q_trace,
225 (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
227 (void *)(__psint_t)dqp->q_nrefs,
228 (void *)(__psint_t)dqp->dq_flags,
229 (void *)(__psint_t)dqp->q_res_bcount,
230 (void *)(__psint_t)INT_GET(dqp->q_core.d_bcount,
232 (void *)(__psint_t)INT_GET(dqp->q_core.d_icount,
234 (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_hardlimit,
236 (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_softlimit,
238 (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_hardlimit,
240 (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_softlimit,
242 (void *)(__psint_t)INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
243 (void *)(__psint_t)current_pid(),
244 (void *)(__psint_t)ino,
245 (void *)(__psint_t)retaddr,
246 (void *)(__psint_t)udqp);
253 * If default limits are in force, push them into the dquot now.
254 * We overwrite the dquot limits only if they are zero and this
255 * is not the root dquot.
258 xfs_qm_adjust_dqlimits(
262 xfs_quotainfo_t *q = mp->m_quotainfo;
266 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
267 INT_SET(d->d_blk_softlimit, ARCH_CONVERT, q->qi_bsoftlimit);
268 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
269 INT_SET(d->d_blk_hardlimit, ARCH_CONVERT, q->qi_bhardlimit);
270 if (q->qi_isoftlimit && !d->d_ino_softlimit)
271 INT_SET(d->d_ino_softlimit, ARCH_CONVERT, q->qi_isoftlimit);
272 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
273 INT_SET(d->d_ino_hardlimit, ARCH_CONVERT, q->qi_ihardlimit);
274 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
275 INT_SET(d->d_rtb_softlimit, ARCH_CONVERT, q->qi_rtbsoftlimit);
276 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
277 INT_SET(d->d_rtb_hardlimit, ARCH_CONVERT, q->qi_rtbhardlimit);
281 * Check the limits and timers of a dquot and start or reset timers
283 * This gets called even when quota enforcement is OFF, which makes our
284 * life a little less complicated. (We just don't reject any quota
285 * reservations in that case, when enforcement is off).
286 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
288 * In contrast, warnings are a little different in that they don't
289 * 'automatically' get started when limits get exceeded. They do
290 * get reset to zero, however, when we find the count to be under
291 * the soft limit (they are only ever set non-zero via userspace).
294 xfs_qm_adjust_dqtimers(
301 if (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT))
302 ASSERT(INT_GET(d->d_blk_softlimit, ARCH_CONVERT) <=
303 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT));
304 if (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT))
305 ASSERT(INT_GET(d->d_ino_softlimit, ARCH_CONVERT) <=
306 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT));
307 if (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT))
308 ASSERT(INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) <=
309 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT));
312 if ((INT_GET(d->d_blk_softlimit, ARCH_CONVERT) &&
313 (INT_GET(d->d_bcount, ARCH_CONVERT) >=
314 INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) ||
315 (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT) &&
316 (INT_GET(d->d_bcount, ARCH_CONVERT) >=
317 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
318 INT_SET(d->d_btimer, ARCH_CONVERT,
319 get_seconds() + XFS_QI_BTIMELIMIT(mp));
324 if ((!d->d_blk_softlimit ||
325 (INT_GET(d->d_bcount, ARCH_CONVERT) <
326 INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) &&
327 (!d->d_blk_hardlimit ||
328 (INT_GET(d->d_bcount, ARCH_CONVERT) <
329 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
335 if ((INT_GET(d->d_ino_softlimit, ARCH_CONVERT) &&
336 (INT_GET(d->d_icount, ARCH_CONVERT) >=
337 INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) ||
338 (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT) &&
339 (INT_GET(d->d_icount, ARCH_CONVERT) >=
340 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
341 INT_SET(d->d_itimer, ARCH_CONVERT,
342 get_seconds() + XFS_QI_ITIMELIMIT(mp));
347 if ((!d->d_ino_softlimit ||
348 (INT_GET(d->d_icount, ARCH_CONVERT) <
349 INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) &&
350 (!d->d_ino_hardlimit ||
351 (INT_GET(d->d_icount, ARCH_CONVERT) <
352 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
357 if (!d->d_rtbtimer) {
358 if ((INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) &&
359 (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
360 INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) ||
361 (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT) &&
362 (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
363 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
364 INT_SET(d->d_rtbtimer, ARCH_CONVERT,
365 get_seconds() + XFS_QI_RTBTIMELIMIT(mp));
370 if ((!d->d_rtb_softlimit ||
371 (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
372 INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) &&
373 (!d->d_rtb_hardlimit ||
374 (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
375 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
382 * initialize a buffer full of dquots and log the whole thing
385 xfs_qm_init_dquot_blk(
396 ASSERT(XFS_BUF_ISBUSY(bp));
397 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
399 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
402 * ID of the first dquot in the block - id's are zero based.
404 curid = id - (id % XFS_QM_DQPERBLK(mp));
406 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
407 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
408 xfs_qm_dqinit_core(curid, type, d);
409 xfs_trans_dquot_buf(tp, bp,
410 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
411 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
412 XFS_BLI_GDQUOT_BUF)));
413 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
419 * Allocate a block and fill it with dquots.
420 * This is called when the bmapi finds a hole.
428 xfs_fileoff_t offset_fsb,
431 xfs_fsblock_t firstblock;
432 xfs_bmap_free_t flist;
434 int nmaps, error, committed;
436 xfs_trans_t *tp = *tpp;
439 xfs_dqtrace_entry(dqp, "DQALLOC");
442 * Initialize the bmap freelist prior to calling bmapi code.
444 XFS_BMAP_INIT(&flist, &firstblock);
445 xfs_ilock(quotip, XFS_ILOCK_EXCL);
447 * Return if this type of quotas is turned off while we didn't
450 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
451 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
456 * xfs_trans_commit normally decrements the vnode ref count
457 * when it unlocks the inode. Since we want to keep the quota
458 * inode around, we bump the vnode ref count now.
460 VN_HOLD(XFS_ITOV(quotip));
462 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
464 if ((error = xfs_bmapi(tp, quotip,
465 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
466 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
468 XFS_QM_DQALLOC_SPACE_RES(mp),
469 &map, &nmaps, &flist))) {
472 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
474 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
475 (map.br_startblock != HOLESTARTBLOCK));
478 * Keep track of the blkno to save a lookup later
480 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
482 /* now we can just get the buffer (there's nothing to read yet) */
483 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
485 XFS_QI_DQCHUNKLEN(mp),
487 if (!bp || (error = XFS_BUF_GETERROR(bp)))
490 * Make a chunk of dquots out of this buffer and log
493 xfs_qm_init_dquot_blk(tp, mp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
494 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
497 * xfs_bmap_finish() may commit the current transaction and
498 * start a second transaction if the freelist is not empty.
500 * Since we still want to modify this buffer, we need to
501 * ensure that the buffer is not released on commit of
502 * the first transaction and ensure the buffer is added to the
503 * second transaction.
505 * If there is only one transaction then don't stop the buffer
506 * from being released when it commits later on.
509 xfs_trans_bhold(tp, bp);
511 if ((error = xfs_bmap_finish(tpp, &flist, firstblock, &committed))) {
517 xfs_trans_bjoin(tp, bp);
519 xfs_trans_bhold_release(tp, bp);
526 xfs_bmap_cancel(&flist);
528 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
534 * Maps a dquot to the buffer containing its on-disk version.
535 * This returns a ptr to the buffer containing the on-disk dquot
536 * in the bpp param, and a ptr to the on-disk dquot within that buffer
542 xfs_disk_dquot_t **O_ddpp,
551 xfs_disk_dquot_t *ddq;
554 xfs_trans_t *tp = (tpp ? *tpp : NULL);
557 id = INT_GET(dqp->q_core.d_id, ARCH_CONVERT);
562 * If we don't know where the dquot lives, find out.
564 if (dqp->q_blkno == (xfs_daddr_t) 0) {
565 /* We use the id as an index */
566 dqp->q_fileoffset = (xfs_fileoff_t) ((uint)id /
567 XFS_QM_DQPERBLK(mp));
569 quotip = XFS_DQ_TO_QIP(dqp);
570 xfs_ilock(quotip, XFS_ILOCK_SHARED);
572 * Return if this type of quotas is turned off while we didn't
575 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
576 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
580 * Find the block map; no allocations yet
582 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
583 XFS_DQUOT_CLUSTER_SIZE_FSB,
585 NULL, 0, &map, &nmaps, NULL);
587 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
591 ASSERT(map.br_blockcount == 1);
594 * offset of dquot in the (fixed sized) dquot chunk.
596 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
598 if (map.br_startblock == HOLESTARTBLOCK) {
600 * We don't allocate unless we're asked to
602 if (!(flags & XFS_QMOPT_DQALLOC))
606 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
607 dqp->q_fileoffset, &bp)))
613 * store the blkno etc so that we don't have to do the
614 * mapping all the time
616 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
619 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
620 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
623 * Read in the buffer, unless we've just done the allocation
624 * (in which case we already have the buf).
627 xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
628 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
630 XFS_QI_DQCHUNKLEN(mp),
635 return XFS_ERROR(error);
637 ASSERT(XFS_BUF_ISBUSY(bp));
638 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
641 * calculate the location of the dquot inside the buffer.
643 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
646 * A simple sanity check in case we got a corrupted dquot...
648 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
649 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
651 if (!(flags & XFS_QMOPT_DQREPAIR)) {
652 xfs_trans_brelse(tp, bp);
653 return XFS_ERROR(EIO);
655 XFS_BUF_BUSY(bp); /* We dirtied this */
666 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
667 * and release the buffer immediately.
675 xfs_dquot_t *dqp, /* dquot to get filled in */
678 xfs_disk_dquot_t *ddqp;
686 * get a pointer to the on-disk dquot and the buffer containing it
687 * dqp already knows its own type (GROUP/USER).
689 xfs_dqtrace_entry(dqp, "DQREAD");
690 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
695 /* copy everything from disk dquot to the incore dquot */
696 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
697 ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
698 xfs_qm_dquot_logitem_init(dqp);
701 * Reservation counters are defined as reservation plus current usage
702 * to avoid having to add everytime.
704 dqp->q_res_bcount = INT_GET(ddqp->d_bcount, ARCH_CONVERT);
705 dqp->q_res_icount = INT_GET(ddqp->d_icount, ARCH_CONVERT);
706 dqp->q_res_rtbcount = INT_GET(ddqp->d_rtbcount, ARCH_CONVERT);
708 /* Mark the buf so that this will stay incore a little longer */
709 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
712 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
713 * So we need to release with xfs_trans_brelse().
714 * The strategy here is identical to that of inodes; we lock
715 * the dquot in xfs_qm_dqget() before making it accessible to
716 * others. This is because dquots, like inodes, need a good level of
717 * concurrency, and we don't want to take locks on the entire buffers
718 * for dquot accesses.
719 * Note also that the dquot buffer may even be dirty at this point, if
720 * this particular dquot was repaired. We still aren't afraid to
721 * brelse it because we have the changes incore.
723 ASSERT(XFS_BUF_ISBUSY(bp));
724 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
725 xfs_trans_brelse(tp, bp);
732 * allocate an incore dquot from the kernel heap,
733 * and fill its core with quota information kept on disk.
734 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
735 * if it wasn't already allocated.
740 xfs_dqid_t id, /* gid or uid, depending on type */
741 uint type, /* UDQUOT or GDQUOT */
742 uint flags, /* DQALLOC, DQREPAIR */
743 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
750 dqp = xfs_qm_dqinit(mp, id, type);
752 if (flags & XFS_QMOPT_DQALLOC) {
753 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
754 if ((error = xfs_trans_reserve(tp,
755 XFS_QM_DQALLOC_SPACE_RES(mp),
756 XFS_WRITE_LOG_RES(mp) +
757 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
760 XFS_TRANS_PERM_LOG_RES,
761 XFS_WRITE_LOG_COUNT))) {
765 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
769 * Read it from disk; xfs_dqread() takes care of
770 * all the necessary initialization of dquot's fields (locks, etc)
772 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
774 * This can happen if quotas got turned off (ESRCH),
775 * or if the dquot didn't exist on disk and we ask to
778 xfs_dqtrace_entry(dqp, "DQREAD FAIL");
779 cancelflags |= XFS_TRANS_ABORT;
783 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
794 xfs_trans_cancel(tp, cancelflags);
796 xfs_qm_dqdestroy(dqp);
802 * Lookup a dquot in the incore dquot hashtable. We keep two separate
803 * hashtables for user and group dquots; and, these are global tables
804 * inside the XQM, not per-filesystem tables.
805 * The hash chain must be locked by caller, and it is left locked
806 * on return. Returning dquot is locked.
813 xfs_dquot_t **O_dqpp)
819 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
821 flist_locked = B_FALSE;
824 * Traverse the hashchain looking for a match
826 for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
828 * We already have the hashlock. We don't need the
829 * dqlock to look at the id field of the dquot, since the
830 * id can't be modified without the hashlock anyway.
832 if (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id && dqp->q_mount == mp) {
833 xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
835 * All in core dquots must be on the dqlist of mp
837 ASSERT(dqp->MPL_PREVP != NULL);
840 if (dqp->q_nrefs == 0) {
841 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
842 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
843 xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
846 * We may have raced with dqreclaim_one()
847 * (and lost). So, flag that we don't
848 * want the dquot to be reclaimed.
850 dqp->dq_flags |= XFS_DQ_WANT;
852 xfs_qm_freelist_lock(xfs_Gqm);
854 dqp->dq_flags &= ~(XFS_DQ_WANT);
856 flist_locked = B_TRUE;
860 * id couldn't have changed; we had the hashlock all
863 ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
866 if (dqp->q_nrefs != 0) {
867 xfs_qm_freelist_unlock(xfs_Gqm);
868 flist_locked = B_FALSE;
871 * take it off the freelist
873 xfs_dqtrace_entry(dqp,
874 "DQLOOKUP: TAKEOFF FL");
875 XQM_FREELIST_REMOVE(dqp);
876 /* xfs_qm_freelist_print(&(xfs_Gqm->
888 xfs_qm_freelist_unlock(xfs_Gqm);
890 * move the dquot to the front of the hashchain
892 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
893 if (dqp->HL_PREVP != &qh->qh_next) {
894 xfs_dqtrace_entry(dqp,
895 "DQLOOKUP: HASH MOVETOFRONT");
896 if ((d = dqp->HL_NEXT))
897 d->HL_PREVP = dqp->HL_PREVP;
898 *(dqp->HL_PREVP) = d;
900 d->HL_PREVP = &dqp->HL_NEXT;
902 dqp->HL_PREVP = &qh->qh_next;
905 xfs_dqtrace_entry(dqp, "LOOKUP END");
907 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
913 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
918 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
919 * a locked dquot, doing an allocation (if requested) as needed.
920 * When both an inode and an id are given, the inode's id takes precedence.
921 * That is, if the id changes while we don't hold the ilock inside this
922 * function, the new dquot is returned, not necessarily the one requested
923 * in the id argument.
928 xfs_inode_t *ip, /* locked inode (optional) */
929 xfs_dqid_t id, /* uid/projid/gid depending on type */
930 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
931 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
932 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
939 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
940 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
941 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
942 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
945 h = XFS_DQ_HASH(mp, id, type);
948 if (xfs_do_dqerror) {
949 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
950 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
951 cmn_err(CE_DEBUG, "Returning error in dqget");
960 ASSERT(type == XFS_DQ_USER ||
961 type == XFS_DQ_PROJ ||
962 type == XFS_DQ_GROUP);
964 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
965 if (type == XFS_DQ_USER)
966 ASSERT(ip->i_udquot == NULL);
968 ASSERT(ip->i_gdquot == NULL);
974 * Look in the cache (hashtable).
975 * The chain is kept locked during lookup.
977 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
978 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
980 * The dquot was found, moved to the front of the chain,
981 * taken off the freelist if it was on it, and locked
982 * at this point. Just unlock the hashchain and return.
985 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
986 XFS_DQ_HASH_UNLOCK(h);
987 xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
988 return (0); /* success */
990 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
993 * Dquot cache miss. We don't want to keep the inode lock across
994 * a (potential) disk read. Also we don't want to deal with the lock
995 * ordering between quotainode and this inode. OTOH, dropping the inode
996 * lock here means dealing with a chown that can happen before
997 * we re-acquire the lock.
1000 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1002 * Save the hashchain version stamp, and unlock the chain, so that
1003 * we don't keep the lock across a disk read
1005 version = h->qh_version;
1006 XFS_DQ_HASH_UNLOCK(h);
1009 * Allocate the dquot on the kernel heap, and read the ondisk
1010 * portion off the disk. Also, do all the necessary initialization
1011 * This can return ENOENT if dquot didn't exist on disk and we didn't
1012 * ask it to allocate; ESRCH if quotas got turned off suddenly.
1014 if ((error = xfs_qm_idtodq(mp, id, type,
1015 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
1019 xfs_ilock(ip, XFS_ILOCK_EXCL);
1024 * See if this is mount code calling to look at the overall quota limits
1025 * which are stored in the id == 0 user or group's dquot.
1026 * Since we may not have done a quotacheck by this point, just return
1027 * the dquot without attaching it to any hashtables, lists, etc, or even
1028 * taking a reference.
1029 * The caller must dqdestroy this once done.
1031 if (flags & XFS_QMOPT_DQSUSER) {
1038 * Dquot lock comes after hashlock in the lock ordering
1041 xfs_ilock(ip, XFS_ILOCK_EXCL);
1042 if (! XFS_IS_DQTYPE_ON(mp, type)) {
1043 /* inode stays locked on return */
1044 xfs_qm_dqdestroy(dqp);
1045 return XFS_ERROR(ESRCH);
1048 * A dquot could be attached to this inode by now, since
1049 * we had dropped the ilock.
1051 if (type == XFS_DQ_USER) {
1053 xfs_qm_dqdestroy(dqp);
1060 xfs_qm_dqdestroy(dqp);
1069 * Hashlock comes after ilock in lock order
1071 XFS_DQ_HASH_LOCK(h);
1072 if (version != h->qh_version) {
1073 xfs_dquot_t *tmpdqp;
1075 * Now, see if somebody else put the dquot in the
1076 * hashtable before us. This can happen because we didn't
1077 * keep the hashchain lock. We don't have to worry about
1078 * lock order between the two dquots here since dqp isn't
1079 * on any findable lists yet.
1081 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1083 * Duplicate found. Just throw away the new dquot
1086 xfs_qm_dqput(tmpdqp);
1087 XFS_DQ_HASH_UNLOCK(h);
1088 xfs_qm_dqdestroy(dqp);
1089 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1095 * Put the dquot at the beginning of the hash-chain and mp's list
1096 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1098 ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
1100 XQM_HASHLIST_INSERT(h, dqp);
1103 * Attach this dquot to this filesystem's list of all dquots,
1104 * kept inside the mount structure in m_quotainfo field
1106 xfs_qm_mplist_lock(mp);
1109 * We return a locked dquot to the caller, with a reference taken
1114 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
1116 xfs_qm_mplist_unlock(mp);
1117 XFS_DQ_HASH_UNLOCK(h);
1119 ASSERT((ip == NULL) || XFS_ISLOCKED_INODE_EXCL(ip));
1120 xfs_dqtrace_entry(dqp, "DQGET DONE");
1127 * Release a reference to the dquot (decrement ref-count)
1128 * and unlock it. If there is a group quota attached to this
1129 * dquot, carefully release that too without tripping over
1130 * deadlocks'n'stuff.
1138 ASSERT(dqp->q_nrefs > 0);
1139 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1140 xfs_dqtrace_entry(dqp, "DQPUT");
1142 if (dqp->q_nrefs != 1) {
1149 * drop the dqlock and acquire the freelist and dqlock
1150 * in the right order; but try to get it out-of-order first
1152 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
1153 xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
1155 xfs_qm_freelist_lock(xfs_Gqm);
1162 /* We can't depend on nrefs being == 1 here */
1163 if (--dqp->q_nrefs == 0) {
1164 xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
1166 * insert at end of the freelist.
1168 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1171 * If we just added a udquot to the freelist, then
1172 * we want to release the gdquot reference that
1173 * it (probably) has. Otherwise it'll keep the
1174 * gdquot from getting reclaimed.
1176 if ((gdqp = dqp->q_gdquot)) {
1178 * Avoid a recursive dqput call
1181 dqp->q_gdquot = NULL;
1184 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1185 "@@@@@++ Free list (after append) @@@@@+");
1191 * If we had a group quota inside the user quota as a hint,
1198 xfs_qm_freelist_unlock(xfs_Gqm);
1202 * Release a dquot. Flush it if dirty, then dqput() it.
1203 * dquot must not be locked.
1210 xfs_dqtrace_entry(dqp, "DQRELE");
1214 * We don't care to flush it if the dquot is dirty here.
1215 * That will create stutters that we want to avoid.
1216 * Instead we do a delayed write when we try to reclaim
1217 * a dirty dquot. Also xfs_sync will take part of the burden...
1224 * Write a modified dquot to disk.
1225 * The dquot must be locked and the flush lock too taken by caller.
1226 * The flush lock will not be unlocked until the dquot reaches the disk,
1227 * but the dquot is free to be unlocked and modified by the caller
1228 * in the interim. Dquot is still locked on return. This behavior is
1229 * identical to that of inodes.
1238 xfs_disk_dquot_t *ddqp;
1242 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1243 ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp));
1244 xfs_dqtrace_entry(dqp, "DQFLUSH");
1247 * If not dirty, nada.
1249 if (!XFS_DQ_IS_DIRTY(dqp)) {
1255 * Cant flush a pinned dquot. Wait for it.
1257 xfs_qm_dqunpin_wait(dqp);
1260 * This may have been unpinned because the filesystem is shutting
1261 * down forcibly. If that's the case we must not write this dquot
1262 * to disk, because the log record didn't make it to disk!
1264 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1265 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1267 return XFS_ERROR(EIO);
1271 * Get the buffer containing the on-disk dquot
1272 * We don't need a transaction envelope because we know that the
1273 * the ondisk-dquot has already been allocated for.
1275 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1276 xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
1277 ASSERT(error != ENOENT);
1279 * Quotas could have gotten turned off (ESRCH)
1285 if (xfs_qm_dqcheck(&dqp->q_core, INT_GET(ddqp->d_id, ARCH_CONVERT),
1286 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
1287 xfs_force_shutdown(dqp->q_mount, XFS_CORRUPT_INCORE);
1288 return XFS_ERROR(EIO);
1291 /* This is the only portion of data that needs to persist */
1292 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1295 * Clear the dirty field and remember the flush lsn for later use.
1297 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1300 /* lsn is 64 bits */
1302 dqp->q_logitem.qli_flush_lsn = dqp->q_logitem.qli_item.li_lsn;
1306 * Attach an iodone routine so that we can remove this dquot from the
1307 * AIL and release the flush lock once the dquot is synced to disk.
1309 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1310 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1312 * If the buffer is pinned then push on the log so we won't
1313 * get stuck waiting in the write for too long.
1315 if (XFS_BUF_ISPINNED(bp)) {
1316 xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
1317 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1320 if (flags & XFS_QMOPT_DELWRI) {
1321 xfs_bdwrite(mp, bp);
1322 } else if (flags & XFS_QMOPT_ASYNC) {
1323 xfs_bawrite(mp, bp);
1325 error = xfs_bwrite(mp, bp);
1327 xfs_dqtrace_entry(dqp, "DQFLUSH END");
1329 * dqp is still locked, but caller is free to unlock it now.
1336 * This is the dquot flushing I/O completion routine. It is called
1337 * from interrupt level when the buffer containing the dquot is
1338 * flushed to disk. It is responsible for removing the dquot logitem
1339 * from the AIL if it has not been re-logged, and unlocking the dquot's
1340 * flush lock. This behavior is very similar to that of inodes..
1344 xfs_qm_dqflush_done(
1346 xfs_dq_logitem_t *qip)
1351 dqp = qip->qli_dquot;
1354 * We only want to pull the item from the AIL if its
1355 * location in the log has not changed since we started the flush.
1356 * Thus, we only bother if the dquot's lsn has
1357 * not changed. First we check the lsn outside the lock
1358 * since it's cheaper, and then we recheck while
1359 * holding the lock before removing the dquot from the AIL.
1361 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1362 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1364 AIL_LOCK(dqp->q_mount, s);
1366 * xfs_trans_delete_ail() drops the AIL lock.
1368 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
1369 xfs_trans_delete_ail(dqp->q_mount,
1370 (xfs_log_item_t*)qip, s);
1372 AIL_UNLOCK(dqp->q_mount, s);
1376 * Release the dq's flush lock since we're done with it.
1383 xfs_qm_dqflock_nowait(
1388 locked = cpsema(&((dqp)->q_flock));
1390 /* XXX ifdef these out */
1392 (dqp)->dq_flags |= XFS_DQ_FLOCKED;
1398 xfs_qm_dqlock_nowait(
1401 return (mutex_trylock(&((dqp)->q_qlock)));
1408 mutex_lock(&(dqp->q_qlock), PINOD);
1415 mutex_unlock(&(dqp->q_qlock));
1416 if (dqp->q_logitem.qli_dquot == dqp) {
1417 /* Once was dqp->q_mount, but might just have been cleared */
1418 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_mountp,
1419 (xfs_log_item_t*)&(dqp->q_logitem));
1425 xfs_dqunlock_nonotify(
1428 mutex_unlock(&(dqp->q_qlock));
1438 if (INT_GET(d1->q_core.d_id, ARCH_CONVERT) >
1439 INT_GET(d2->q_core.d_id, ARCH_CONVERT)) {
1457 * Take a dquot out of the mount's dqlist as well as the hashlist.
1458 * This is called via unmount as well as quotaoff, and the purge
1459 * will always succeed unless there are soft (temp) references
1462 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1463 * that we're returning! XXXsup - not cool.
1471 xfs_dqhash_t *thishash;
1476 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
1477 ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
1481 * We really can't afford to purge a dquot that is
1482 * referenced, because these are hard refs.
1483 * It shouldn't happen in general because we went thru _all_ inodes in
1484 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1485 * However it is possible that we have dquots with temporary
1486 * references that are not attached to an inode. e.g. see xfs_setattr().
1488 if (dqp->q_nrefs != 0) {
1490 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
1494 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1497 * If we're turning off quotas, we have to make sure that, for
1498 * example, we don't delete quota disk blocks while dquots are
1499 * in the process of getting written to those disk blocks.
1500 * This dquot might well be on AIL, and we can't leave it there
1501 * if we're turning off quotas. Basically, we need this flush
1502 * lock, and are willing to block on it.
1504 if (! xfs_qm_dqflock_nowait(dqp)) {
1506 * Block on the flush lock after nudging dquot buffer,
1509 xfs_qm_dqflock_pushbuf_wait(dqp);
1513 * XXXIf we're turning this type of quotas off, we don't care
1514 * about the dirty metadata sitting in this dquot. OTOH, if
1515 * we're unmounting, we do care, so we flush it and wait.
1517 if (XFS_DQ_IS_DIRTY(dqp)) {
1518 xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
1519 /* dqflush unlocks dqflock */
1521 * Given that dqpurge is a very rare occurrence, it is OK
1522 * that we're holding the hashlist and mplist locks
1523 * across the disk write. But, ... XXXsup
1525 * We don't care about getting disk errors here. We need
1526 * to purge this dquot anyway, so we go ahead regardless.
1528 (void) xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
1531 ASSERT(dqp->q_pincount == 0);
1532 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1533 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1535 thishash = dqp->q_hash;
1536 XQM_HASHLIST_REMOVE(thishash, dqp);
1537 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
1539 * XXX Move this to the front of the freelist, if we can get the
1542 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1544 dqp->q_mount = NULL;
1546 dqp->dq_flags = XFS_DQ_INACTIVE;
1547 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1550 XFS_DQ_HASH_UNLOCK(thishash);
1557 xfs_qm_dqprint(xfs_dquot_t *dqp)
1559 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1560 cmn_err(CE_DEBUG, "---- dquotID = %d",
1561 (int)INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
1562 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
1563 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1564 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1565 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1566 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
1567 INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT),
1568 (int) INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT));
1569 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
1570 INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT),
1571 (int)INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT));
1572 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
1573 INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT),
1574 (int)INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT));
1575 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
1576 INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT),
1577 (int)INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT));
1578 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1579 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
1580 (int)INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
1581 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1582 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
1583 (int)INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
1584 cmn_err(CE_DEBUG, "---- btimer = %d",
1585 (int)INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT));
1586 cmn_err(CE_DEBUG, "---- itimer = %d",
1587 (int)INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT));
1588 cmn_err(CE_DEBUG, "---------------------------");
1593 * Give the buffer a little push if it is incore and
1594 * wait on the flush lock.
1597 xfs_qm_dqflock_pushbuf_wait(
1603 * Check to see if the dquot has been flushed delayed
1604 * write. If so, grab its buffer and send it
1605 * out immediately. We'll be able to acquire
1606 * the flush lock when the I/O completes.
1608 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
1609 XFS_QI_DQCHUNKLEN(dqp->q_mount),
1610 XFS_INCORE_TRYLOCK);
1612 if (XFS_BUF_ISDELAYWRITE(bp)) {
1613 if (XFS_BUF_ISPINNED(bp)) {
1614 xfs_log_force(dqp->q_mount,
1618 xfs_bawrite(dqp->q_mount, bp);