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"
50 #include "xfs_trace.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;
76 static struct lock_class_key xfs_dquot_other_class
;
79 * Allocate and initialize a dquot. We don't always allocate fresh memory;
80 * we try to reclaim a free dquot if the number of incore dquots are above
82 * The only field inside the core that gets initialized at this point
83 * is the d_id field. The idea is to fill in the entire q_core
84 * when we read in the on disk dquot.
93 boolean_t brandnewdquot
;
95 brandnewdquot
= xfs_qm_dqalloc_incore(&dqp
);
97 dqp
->q_core
.d_id
= cpu_to_be32(id
);
101 * No need to re-initialize these if this is a reclaimed dquot.
104 dqp
->dq_flnext
= dqp
->dq_flprev
= dqp
;
105 mutex_init(&dqp
->q_qlock
);
106 init_waitqueue_head(&dqp
->q_pinwait
);
109 * Because we want to use a counting completion, complete
110 * the flush completion once to allow a single access to
111 * the flush completion without blocking.
113 init_completion(&dqp
->q_flush
);
114 complete(&dqp
->q_flush
);
116 trace_xfs_dqinit(dqp
);
119 * Only the q_core portion was zeroed in dqreclaim_one().
120 * So, we need to reset others.
124 dqp
->MPL_NEXT
= dqp
->HL_NEXT
= NULL
;
125 dqp
->HL_PREVP
= dqp
->MPL_PREVP
= NULL
;
126 dqp
->q_bufoffset
= 0;
127 dqp
->q_fileoffset
= 0;
128 dqp
->q_transp
= NULL
;
129 dqp
->q_gdquot
= NULL
;
130 dqp
->q_res_bcount
= 0;
131 dqp
->q_res_icount
= 0;
132 dqp
->q_res_rtbcount
= 0;
133 atomic_set(&dqp
->q_pincount
, 0);
135 ASSERT(dqp
->dq_flnext
== dqp
->dq_flprev
);
137 trace_xfs_dqreuse(dqp
);
141 * In either case we need to make sure group quotas have a different
142 * lock class than user quotas, to make sure lockdep knows we can
143 * locks of one of each at the same time.
145 if (!(type
& XFS_DQ_USER
))
146 lockdep_set_class(&dqp
->q_qlock
, &xfs_dquot_other_class
);
149 * log item gets initialized later
155 * This is called to free all the memory associated with a dquot
161 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp
));
163 mutex_destroy(&dqp
->q_qlock
);
164 sv_destroy(&dqp
->q_pinwait
);
165 kmem_zone_free(xfs_Gqm
->qm_dqzone
, dqp
);
167 atomic_dec(&xfs_Gqm
->qm_totaldquots
);
171 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
180 * Caller has zero'd the entire dquot 'chunk' already.
182 d
->dd_diskdq
.d_magic
= cpu_to_be16(XFS_DQUOT_MAGIC
);
183 d
->dd_diskdq
.d_version
= XFS_DQUOT_VERSION
;
184 d
->dd_diskdq
.d_id
= cpu_to_be32(id
);
185 d
->dd_diskdq
.d_flags
= type
;
189 * If default limits are in force, push them into the dquot now.
190 * We overwrite the dquot limits only if they are zero and this
191 * is not the root dquot.
194 xfs_qm_adjust_dqlimits(
198 xfs_quotainfo_t
*q
= mp
->m_quotainfo
;
202 if (q
->qi_bsoftlimit
&& !d
->d_blk_softlimit
)
203 d
->d_blk_softlimit
= cpu_to_be64(q
->qi_bsoftlimit
);
204 if (q
->qi_bhardlimit
&& !d
->d_blk_hardlimit
)
205 d
->d_blk_hardlimit
= cpu_to_be64(q
->qi_bhardlimit
);
206 if (q
->qi_isoftlimit
&& !d
->d_ino_softlimit
)
207 d
->d_ino_softlimit
= cpu_to_be64(q
->qi_isoftlimit
);
208 if (q
->qi_ihardlimit
&& !d
->d_ino_hardlimit
)
209 d
->d_ino_hardlimit
= cpu_to_be64(q
->qi_ihardlimit
);
210 if (q
->qi_rtbsoftlimit
&& !d
->d_rtb_softlimit
)
211 d
->d_rtb_softlimit
= cpu_to_be64(q
->qi_rtbsoftlimit
);
212 if (q
->qi_rtbhardlimit
&& !d
->d_rtb_hardlimit
)
213 d
->d_rtb_hardlimit
= cpu_to_be64(q
->qi_rtbhardlimit
);
217 * Check the limits and timers of a dquot and start or reset timers
219 * This gets called even when quota enforcement is OFF, which makes our
220 * life a little less complicated. (We just don't reject any quota
221 * reservations in that case, when enforcement is off).
222 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
224 * In contrast, warnings are a little different in that they don't
225 * 'automatically' get started when limits get exceeded. They do
226 * get reset to zero, however, when we find the count to be under
227 * the soft limit (they are only ever set non-zero via userspace).
230 xfs_qm_adjust_dqtimers(
237 if (d
->d_blk_hardlimit
)
238 ASSERT(be64_to_cpu(d
->d_blk_softlimit
) <=
239 be64_to_cpu(d
->d_blk_hardlimit
));
240 if (d
->d_ino_hardlimit
)
241 ASSERT(be64_to_cpu(d
->d_ino_softlimit
) <=
242 be64_to_cpu(d
->d_ino_hardlimit
));
243 if (d
->d_rtb_hardlimit
)
244 ASSERT(be64_to_cpu(d
->d_rtb_softlimit
) <=
245 be64_to_cpu(d
->d_rtb_hardlimit
));
248 if ((d
->d_blk_softlimit
&&
249 (be64_to_cpu(d
->d_bcount
) >=
250 be64_to_cpu(d
->d_blk_softlimit
))) ||
251 (d
->d_blk_hardlimit
&&
252 (be64_to_cpu(d
->d_bcount
) >=
253 be64_to_cpu(d
->d_blk_hardlimit
)))) {
254 d
->d_btimer
= cpu_to_be32(get_seconds() +
255 XFS_QI_BTIMELIMIT(mp
));
260 if ((!d
->d_blk_softlimit
||
261 (be64_to_cpu(d
->d_bcount
) <
262 be64_to_cpu(d
->d_blk_softlimit
))) &&
263 (!d
->d_blk_hardlimit
||
264 (be64_to_cpu(d
->d_bcount
) <
265 be64_to_cpu(d
->d_blk_hardlimit
)))) {
271 if ((d
->d_ino_softlimit
&&
272 (be64_to_cpu(d
->d_icount
) >=
273 be64_to_cpu(d
->d_ino_softlimit
))) ||
274 (d
->d_ino_hardlimit
&&
275 (be64_to_cpu(d
->d_icount
) >=
276 be64_to_cpu(d
->d_ino_hardlimit
)))) {
277 d
->d_itimer
= cpu_to_be32(get_seconds() +
278 XFS_QI_ITIMELIMIT(mp
));
283 if ((!d
->d_ino_softlimit
||
284 (be64_to_cpu(d
->d_icount
) <
285 be64_to_cpu(d
->d_ino_softlimit
))) &&
286 (!d
->d_ino_hardlimit
||
287 (be64_to_cpu(d
->d_icount
) <
288 be64_to_cpu(d
->d_ino_hardlimit
)))) {
293 if (!d
->d_rtbtimer
) {
294 if ((d
->d_rtb_softlimit
&&
295 (be64_to_cpu(d
->d_rtbcount
) >=
296 be64_to_cpu(d
->d_rtb_softlimit
))) ||
297 (d
->d_rtb_hardlimit
&&
298 (be64_to_cpu(d
->d_rtbcount
) >=
299 be64_to_cpu(d
->d_rtb_hardlimit
)))) {
300 d
->d_rtbtimer
= cpu_to_be32(get_seconds() +
301 XFS_QI_RTBTIMELIMIT(mp
));
306 if ((!d
->d_rtb_softlimit
||
307 (be64_to_cpu(d
->d_rtbcount
) <
308 be64_to_cpu(d
->d_rtb_softlimit
))) &&
309 (!d
->d_rtb_hardlimit
||
310 (be64_to_cpu(d
->d_rtbcount
) <
311 be64_to_cpu(d
->d_rtb_hardlimit
)))) {
318 * initialize a buffer full of dquots and log the whole thing
321 xfs_qm_init_dquot_blk(
332 ASSERT(XFS_BUF_ISBUSY(bp
));
333 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
335 d
= (xfs_dqblk_t
*)XFS_BUF_PTR(bp
);
338 * ID of the first dquot in the block - id's are zero based.
340 curid
= id
- (id
% XFS_QM_DQPERBLK(mp
));
342 memset(d
, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp
)));
343 for (i
= 0; i
< XFS_QM_DQPERBLK(mp
); i
++, d
++, curid
++)
344 xfs_qm_dqinit_core(curid
, type
, d
);
345 xfs_trans_dquot_buf(tp
, bp
,
346 (type
& XFS_DQ_USER
? XFS_BLI_UDQUOT_BUF
:
347 ((type
& XFS_DQ_PROJ
) ? XFS_BLI_PDQUOT_BUF
:
348 XFS_BLI_GDQUOT_BUF
)));
349 xfs_trans_log_buf(tp
, bp
, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp
)) - 1);
355 * Allocate a block and fill it with dquots.
356 * This is called when the bmapi finds a hole.
364 xfs_fileoff_t offset_fsb
,
367 xfs_fsblock_t firstblock
;
368 xfs_bmap_free_t flist
;
370 int nmaps
, error
, committed
;
372 xfs_trans_t
*tp
= *tpp
;
376 trace_xfs_dqalloc(dqp
);
379 * Initialize the bmap freelist prior to calling bmapi code.
381 xfs_bmap_init(&flist
, &firstblock
);
382 xfs_ilock(quotip
, XFS_ILOCK_EXCL
);
384 * Return if this type of quotas is turned off while we didn't
387 if (XFS_IS_THIS_QUOTA_OFF(dqp
)) {
388 xfs_iunlock(quotip
, XFS_ILOCK_EXCL
);
393 * xfs_trans_commit normally decrements the vnode ref count
394 * when it unlocks the inode. Since we want to keep the quota
395 * inode around, we bump the vnode ref count now.
399 xfs_trans_ijoin(tp
, quotip
, XFS_ILOCK_EXCL
);
401 if ((error
= xfs_bmapi(tp
, quotip
,
402 offset_fsb
, XFS_DQUOT_CLUSTER_SIZE_FSB
,
403 XFS_BMAPI_METADATA
| XFS_BMAPI_WRITE
,
405 XFS_QM_DQALLOC_SPACE_RES(mp
),
406 &map
, &nmaps
, &flist
, NULL
))) {
409 ASSERT(map
.br_blockcount
== XFS_DQUOT_CLUSTER_SIZE_FSB
);
411 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
412 (map
.br_startblock
!= HOLESTARTBLOCK
));
415 * Keep track of the blkno to save a lookup later
417 dqp
->q_blkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
419 /* now we can just get the buffer (there's nothing to read yet) */
420 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
422 XFS_QI_DQCHUNKLEN(mp
),
424 if (!bp
|| (error
= XFS_BUF_GETERROR(bp
)))
427 * Make a chunk of dquots out of this buffer and log
430 xfs_qm_init_dquot_blk(tp
, mp
, be32_to_cpu(dqp
->q_core
.d_id
),
431 dqp
->dq_flags
& XFS_DQ_ALLTYPES
, bp
);
434 * xfs_bmap_finish() may commit the current transaction and
435 * start a second transaction if the freelist is not empty.
437 * Since we still want to modify this buffer, we need to
438 * ensure that the buffer is not released on commit of
439 * the first transaction and ensure the buffer is added to the
440 * second transaction.
442 * If there is only one transaction then don't stop the buffer
443 * from being released when it commits later on.
446 xfs_trans_bhold(tp
, bp
);
448 if ((error
= xfs_bmap_finish(tpp
, &flist
, &committed
))) {
454 xfs_trans_bjoin(tp
, bp
);
456 xfs_trans_bhold_release(tp
, bp
);
463 xfs_bmap_cancel(&flist
);
465 xfs_iunlock(quotip
, XFS_ILOCK_EXCL
);
471 * Maps a dquot to the buffer containing its on-disk version.
472 * This returns a ptr to the buffer containing the on-disk dquot
473 * in the bpp param, and a ptr to the on-disk dquot within that buffer
479 xfs_disk_dquot_t
**O_ddpp
,
488 xfs_disk_dquot_t
*ddq
;
491 xfs_trans_t
*tp
= (tpp
? *tpp
: NULL
);
494 id
= be32_to_cpu(dqp
->q_core
.d_id
);
499 * If we don't know where the dquot lives, find out.
501 if (dqp
->q_blkno
== (xfs_daddr_t
) 0) {
502 /* We use the id as an index */
503 dqp
->q_fileoffset
= (xfs_fileoff_t
)id
/ XFS_QM_DQPERBLK(mp
);
505 quotip
= XFS_DQ_TO_QIP(dqp
);
506 xfs_ilock(quotip
, XFS_ILOCK_SHARED
);
508 * Return if this type of quotas is turned off while we didn't
511 if (XFS_IS_THIS_QUOTA_OFF(dqp
)) {
512 xfs_iunlock(quotip
, XFS_ILOCK_SHARED
);
516 * Find the block map; no allocations yet
518 error
= xfs_bmapi(NULL
, quotip
, dqp
->q_fileoffset
,
519 XFS_DQUOT_CLUSTER_SIZE_FSB
,
521 NULL
, 0, &map
, &nmaps
, NULL
, NULL
);
523 xfs_iunlock(quotip
, XFS_ILOCK_SHARED
);
527 ASSERT(map
.br_blockcount
== 1);
530 * offset of dquot in the (fixed sized) dquot chunk.
532 dqp
->q_bufoffset
= (id
% XFS_QM_DQPERBLK(mp
)) *
534 if (map
.br_startblock
== HOLESTARTBLOCK
) {
536 * We don't allocate unless we're asked to
538 if (!(flags
& XFS_QMOPT_DQALLOC
))
542 if ((error
= xfs_qm_dqalloc(tpp
, mp
, dqp
, quotip
,
543 dqp
->q_fileoffset
, &bp
)))
549 * store the blkno etc so that we don't have to do the
550 * mapping all the time
552 dqp
->q_blkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
555 ASSERT(dqp
->q_blkno
!= DELAYSTARTBLOCK
);
556 ASSERT(dqp
->q_blkno
!= HOLESTARTBLOCK
);
559 * Read in the buffer, unless we've just done the allocation
560 * (in which case we already have the buf).
563 trace_xfs_dqtobp_read(dqp
);
565 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
567 XFS_QI_DQCHUNKLEN(mp
),
572 return XFS_ERROR(error
);
574 ASSERT(XFS_BUF_ISBUSY(bp
));
575 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
578 * calculate the location of the dquot inside the buffer.
580 ddq
= (xfs_disk_dquot_t
*)((char *)XFS_BUF_PTR(bp
) + dqp
->q_bufoffset
);
583 * A simple sanity check in case we got a corrupted dquot...
585 if (xfs_qm_dqcheck(ddq
, id
, dqp
->dq_flags
& XFS_DQ_ALLTYPES
,
586 flags
& (XFS_QMOPT_DQREPAIR
|XFS_QMOPT_DOWARN
),
588 if (!(flags
& XFS_QMOPT_DQREPAIR
)) {
589 xfs_trans_brelse(tp
, bp
);
590 return XFS_ERROR(EIO
);
592 XFS_BUF_BUSY(bp
); /* We dirtied this */
603 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
604 * and release the buffer immediately.
612 xfs_dquot_t
*dqp
, /* dquot to get filled in */
615 xfs_disk_dquot_t
*ddqp
;
622 trace_xfs_dqread(dqp
);
625 * get a pointer to the on-disk dquot and the buffer containing it
626 * dqp already knows its own type (GROUP/USER).
628 if ((error
= xfs_qm_dqtobp(tpp
, dqp
, &ddqp
, &bp
, flags
))) {
633 /* copy everything from disk dquot to the incore dquot */
634 memcpy(&dqp
->q_core
, ddqp
, sizeof(xfs_disk_dquot_t
));
635 ASSERT(be32_to_cpu(dqp
->q_core
.d_id
) == id
);
636 xfs_qm_dquot_logitem_init(dqp
);
639 * Reservation counters are defined as reservation plus current usage
640 * to avoid having to add everytime.
642 dqp
->q_res_bcount
= be64_to_cpu(ddqp
->d_bcount
);
643 dqp
->q_res_icount
= be64_to_cpu(ddqp
->d_icount
);
644 dqp
->q_res_rtbcount
= be64_to_cpu(ddqp
->d_rtbcount
);
646 /* Mark the buf so that this will stay incore a little longer */
647 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_DQUOT
, XFS_DQUOT_REF
);
650 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
651 * So we need to release with xfs_trans_brelse().
652 * The strategy here is identical to that of inodes; we lock
653 * the dquot in xfs_qm_dqget() before making it accessible to
654 * others. This is because dquots, like inodes, need a good level of
655 * concurrency, and we don't want to take locks on the entire buffers
656 * for dquot accesses.
657 * Note also that the dquot buffer may even be dirty at this point, if
658 * this particular dquot was repaired. We still aren't afraid to
659 * brelse it because we have the changes incore.
661 ASSERT(XFS_BUF_ISBUSY(bp
));
662 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
663 xfs_trans_brelse(tp
, bp
);
670 * allocate an incore dquot from the kernel heap,
671 * and fill its core with quota information kept on disk.
672 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
673 * if it wasn't already allocated.
678 xfs_dqid_t id
, /* gid or uid, depending on type */
679 uint type
, /* UDQUOT or GDQUOT */
680 uint flags
, /* DQALLOC, DQREPAIR */
681 xfs_dquot_t
**O_dqpp
)/* OUT : incore dquot, not locked */
688 dqp
= xfs_qm_dqinit(mp
, id
, type
);
690 if (flags
& XFS_QMOPT_DQALLOC
) {
691 tp
= xfs_trans_alloc(mp
, XFS_TRANS_QM_DQALLOC
);
692 if ((error
= xfs_trans_reserve(tp
,
693 XFS_QM_DQALLOC_SPACE_RES(mp
),
694 XFS_WRITE_LOG_RES(mp
) +
695 BBTOB(XFS_QI_DQCHUNKLEN(mp
)) - 1 +
698 XFS_TRANS_PERM_LOG_RES
,
699 XFS_WRITE_LOG_COUNT
))) {
703 cancelflags
= XFS_TRANS_RELEASE_LOG_RES
;
707 * Read it from disk; xfs_dqread() takes care of
708 * all the necessary initialization of dquot's fields (locks, etc)
710 if ((error
= xfs_qm_dqread(&tp
, id
, dqp
, flags
))) {
712 * This can happen if quotas got turned off (ESRCH),
713 * or if the dquot didn't exist on disk and we ask to
716 trace_xfs_dqread_fail(dqp
);
717 cancelflags
|= XFS_TRANS_ABORT
;
721 if ((error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
)))
731 xfs_trans_cancel(tp
, cancelflags
);
733 xfs_qm_dqdestroy(dqp
);
739 * Lookup a dquot in the incore dquot hashtable. We keep two separate
740 * hashtables for user and group dquots; and, these are global tables
741 * inside the XQM, not per-filesystem tables.
742 * The hash chain must be locked by caller, and it is left locked
743 * on return. Returning dquot is locked.
750 xfs_dquot_t
**O_dqpp
)
756 ASSERT(mutex_is_locked(&qh
->qh_lock
));
758 flist_locked
= B_FALSE
;
761 * Traverse the hashchain looking for a match
763 for (dqp
= qh
->qh_next
; dqp
!= NULL
; dqp
= dqp
->HL_NEXT
) {
765 * We already have the hashlock. We don't need the
766 * dqlock to look at the id field of the dquot, since the
767 * id can't be modified without the hashlock anyway.
769 if (be32_to_cpu(dqp
->q_core
.d_id
) == id
&& dqp
->q_mount
== mp
) {
770 trace_xfs_dqlookup_found(dqp
);
773 * All in core dquots must be on the dqlist of mp
775 ASSERT(dqp
->MPL_PREVP
!= NULL
);
778 if (dqp
->q_nrefs
== 0) {
779 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp
));
780 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm
)) {
781 trace_xfs_dqlookup_want(dqp
);
784 * We may have raced with dqreclaim_one()
785 * (and lost). So, flag that we don't
786 * want the dquot to be reclaimed.
788 dqp
->dq_flags
|= XFS_DQ_WANT
;
790 xfs_qm_freelist_lock(xfs_Gqm
);
792 dqp
->dq_flags
&= ~(XFS_DQ_WANT
);
794 flist_locked
= B_TRUE
;
798 * id couldn't have changed; we had the hashlock all
801 ASSERT(be32_to_cpu(dqp
->q_core
.d_id
) == id
);
804 if (dqp
->q_nrefs
!= 0) {
805 xfs_qm_freelist_unlock(xfs_Gqm
);
806 flist_locked
= B_FALSE
;
809 * take it off the freelist
811 trace_xfs_dqlookup_freelist(dqp
);
812 XQM_FREELIST_REMOVE(dqp
);
813 /* xfs_qm_freelist_print(&(xfs_Gqm->
825 xfs_qm_freelist_unlock(xfs_Gqm
);
827 * move the dquot to the front of the hashchain
829 ASSERT(mutex_is_locked(&qh
->qh_lock
));
830 if (dqp
->HL_PREVP
!= &qh
->qh_next
) {
831 trace_xfs_dqlookup_move(dqp
);
832 if ((d
= dqp
->HL_NEXT
))
833 d
->HL_PREVP
= dqp
->HL_PREVP
;
834 *(dqp
->HL_PREVP
) = d
;
836 d
->HL_PREVP
= &dqp
->HL_NEXT
;
838 dqp
->HL_PREVP
= &qh
->qh_next
;
841 trace_xfs_dqlookup_done(dqp
);
843 ASSERT(mutex_is_locked(&qh
->qh_lock
));
849 ASSERT(mutex_is_locked(&qh
->qh_lock
));
854 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
855 * a locked dquot, doing an allocation (if requested) as needed.
856 * When both an inode and an id are given, the inode's id takes precedence.
857 * That is, if the id changes while we don't hold the ilock inside this
858 * function, the new dquot is returned, not necessarily the one requested
859 * in the id argument.
864 xfs_inode_t
*ip
, /* locked inode (optional) */
865 xfs_dqid_t id
, /* uid/projid/gid depending on type */
866 uint type
, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
867 uint flags
, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
868 xfs_dquot_t
**O_dqpp
) /* OUT : locked incore dquot */
875 ASSERT(XFS_IS_QUOTA_RUNNING(mp
));
876 if ((! XFS_IS_UQUOTA_ON(mp
) && type
== XFS_DQ_USER
) ||
877 (! XFS_IS_PQUOTA_ON(mp
) && type
== XFS_DQ_PROJ
) ||
878 (! XFS_IS_GQUOTA_ON(mp
) && type
== XFS_DQ_GROUP
)) {
881 h
= XFS_DQ_HASH(mp
, id
, type
);
884 if (xfs_do_dqerror
) {
885 if ((xfs_dqerror_target
== mp
->m_ddev_targp
) &&
886 (xfs_dqreq_num
++ % xfs_dqerror_mod
) == 0) {
887 cmn_err(CE_DEBUG
, "Returning error in dqget");
896 ASSERT(type
== XFS_DQ_USER
||
897 type
== XFS_DQ_PROJ
||
898 type
== XFS_DQ_GROUP
);
900 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
901 if (type
== XFS_DQ_USER
)
902 ASSERT(ip
->i_udquot
== NULL
);
904 ASSERT(ip
->i_gdquot
== NULL
);
907 mutex_lock(&h
->qh_lock
);
910 * Look in the cache (hashtable).
911 * The chain is kept locked during lookup.
913 if (xfs_qm_dqlookup(mp
, id
, h
, O_dqpp
) == 0) {
914 XQM_STATS_INC(xqmstats
.xs_qm_dqcachehits
);
916 * The dquot was found, moved to the front of the chain,
917 * taken off the freelist if it was on it, and locked
918 * at this point. Just unlock the hashchain and return.
921 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp
));
922 mutex_unlock(&h
->qh_lock
);
923 trace_xfs_dqget_hit(*O_dqpp
);
924 return (0); /* success */
926 XQM_STATS_INC(xqmstats
.xs_qm_dqcachemisses
);
929 * Dquot cache miss. We don't want to keep the inode lock across
930 * a (potential) disk read. Also we don't want to deal with the lock
931 * ordering between quotainode and this inode. OTOH, dropping the inode
932 * lock here means dealing with a chown that can happen before
933 * we re-acquire the lock.
936 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
938 * Save the hashchain version stamp, and unlock the chain, so that
939 * we don't keep the lock across a disk read
941 version
= h
->qh_version
;
942 mutex_unlock(&h
->qh_lock
);
945 * Allocate the dquot on the kernel heap, and read the ondisk
946 * portion off the disk. Also, do all the necessary initialization
947 * This can return ENOENT if dquot didn't exist on disk and we didn't
948 * ask it to allocate; ESRCH if quotas got turned off suddenly.
950 if ((error
= xfs_qm_idtodq(mp
, id
, type
,
951 flags
& (XFS_QMOPT_DQALLOC
|XFS_QMOPT_DQREPAIR
|
955 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
960 * See if this is mount code calling to look at the overall quota limits
961 * which are stored in the id == 0 user or group's dquot.
962 * Since we may not have done a quotacheck by this point, just return
963 * the dquot without attaching it to any hashtables, lists, etc, or even
964 * taking a reference.
965 * The caller must dqdestroy this once done.
967 if (flags
& XFS_QMOPT_DQSUSER
) {
974 * Dquot lock comes after hashlock in the lock ordering
977 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
978 if (! XFS_IS_DQTYPE_ON(mp
, type
)) {
979 /* inode stays locked on return */
980 xfs_qm_dqdestroy(dqp
);
981 return XFS_ERROR(ESRCH
);
984 * A dquot could be attached to this inode by now, since
985 * we had dropped the ilock.
987 if (type
== XFS_DQ_USER
) {
989 xfs_qm_dqdestroy(dqp
);
996 xfs_qm_dqdestroy(dqp
);
1005 * Hashlock comes after ilock in lock order
1007 mutex_lock(&h
->qh_lock
);
1008 if (version
!= h
->qh_version
) {
1009 xfs_dquot_t
*tmpdqp
;
1011 * Now, see if somebody else put the dquot in the
1012 * hashtable before us. This can happen because we didn't
1013 * keep the hashchain lock. We don't have to worry about
1014 * lock order between the two dquots here since dqp isn't
1015 * on any findable lists yet.
1017 if (xfs_qm_dqlookup(mp
, id
, h
, &tmpdqp
) == 0) {
1019 * Duplicate found. Just throw away the new dquot
1022 xfs_qm_dqput(tmpdqp
);
1023 mutex_unlock(&h
->qh_lock
);
1024 xfs_qm_dqdestroy(dqp
);
1025 XQM_STATS_INC(xqmstats
.xs_qm_dquot_dups
);
1031 * Put the dquot at the beginning of the hash-chain and mp's list
1032 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1034 ASSERT(mutex_is_locked(&h
->qh_lock
));
1036 XQM_HASHLIST_INSERT(h
, dqp
);
1039 * Attach this dquot to this filesystem's list of all dquots,
1040 * kept inside the mount structure in m_quotainfo field
1042 xfs_qm_mplist_lock(mp
);
1045 * We return a locked dquot to the caller, with a reference taken
1050 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp
)), dqp
);
1052 xfs_qm_mplist_unlock(mp
);
1053 mutex_unlock(&h
->qh_lock
);
1055 ASSERT((ip
== NULL
) || xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1056 trace_xfs_dqget_miss(dqp
);
1063 * Release a reference to the dquot (decrement ref-count)
1064 * and unlock it. If there is a group quota attached to this
1065 * dquot, carefully release that too without tripping over
1066 * deadlocks'n'stuff.
1074 ASSERT(dqp
->q_nrefs
> 0);
1075 ASSERT(XFS_DQ_IS_LOCKED(dqp
));
1077 trace_xfs_dqput(dqp
);
1079 if (dqp
->q_nrefs
!= 1) {
1086 * drop the dqlock and acquire the freelist and dqlock
1087 * in the right order; but try to get it out-of-order first
1089 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm
)) {
1090 trace_xfs_dqput_wait(dqp
);
1092 xfs_qm_freelist_lock(xfs_Gqm
);
1099 /* We can't depend on nrefs being == 1 here */
1100 if (--dqp
->q_nrefs
== 0) {
1101 trace_xfs_dqput_free(dqp
);
1104 * insert at end of the freelist.
1106 XQM_FREELIST_INSERT(&(xfs_Gqm
->qm_dqfreelist
), dqp
);
1109 * If we just added a udquot to the freelist, then
1110 * we want to release the gdquot reference that
1111 * it (probably) has. Otherwise it'll keep the
1112 * gdquot from getting reclaimed.
1114 if ((gdqp
= dqp
->q_gdquot
)) {
1116 * Avoid a recursive dqput call
1119 dqp
->q_gdquot
= NULL
;
1122 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1123 "@@@@@++ Free list (after append) @@@@@+");
1129 * If we had a group quota inside the user quota as a hint,
1136 xfs_qm_freelist_unlock(xfs_Gqm
);
1140 * Release a dquot. Flush it if dirty, then dqput() it.
1141 * dquot must not be locked.
1150 trace_xfs_dqrele(dqp
);
1154 * We don't care to flush it if the dquot is dirty here.
1155 * That will create stutters that we want to avoid.
1156 * Instead we do a delayed write when we try to reclaim
1157 * a dirty dquot. Also xfs_sync will take part of the burden...
1164 * Write a modified dquot to disk.
1165 * The dquot must be locked and the flush lock too taken by caller.
1166 * The flush lock will not be unlocked until the dquot reaches the disk,
1167 * but the dquot is free to be unlocked and modified by the caller
1168 * in the interim. Dquot is still locked on return. This behavior is
1169 * identical to that of inodes.
1178 xfs_disk_dquot_t
*ddqp
;
1181 ASSERT(XFS_DQ_IS_LOCKED(dqp
));
1182 ASSERT(!completion_done(&dqp
->q_flush
));
1183 trace_xfs_dqflush(dqp
);
1186 * If not dirty, or it's pinned and we are not supposed to
1189 if (!XFS_DQ_IS_DIRTY(dqp
) ||
1190 (!(flags
& XFS_QMOPT_SYNC
) && atomic_read(&dqp
->q_pincount
) > 0)) {
1194 xfs_qm_dqunpin_wait(dqp
);
1197 * This may have been unpinned because the filesystem is shutting
1198 * down forcibly. If that's the case we must not write this dquot
1199 * to disk, because the log record didn't make it to disk!
1201 if (XFS_FORCED_SHUTDOWN(dqp
->q_mount
)) {
1202 dqp
->dq_flags
&= ~(XFS_DQ_DIRTY
);
1204 return XFS_ERROR(EIO
);
1208 * Get the buffer containing the on-disk dquot
1209 * We don't need a transaction envelope because we know that the
1210 * the ondisk-dquot has already been allocated for.
1212 if ((error
= xfs_qm_dqtobp(NULL
, dqp
, &ddqp
, &bp
, XFS_QMOPT_DOWARN
))) {
1213 ASSERT(error
!= ENOENT
);
1215 * Quotas could have gotten turned off (ESRCH)
1221 if (xfs_qm_dqcheck(&dqp
->q_core
, be32_to_cpu(ddqp
->d_id
),
1222 0, XFS_QMOPT_DOWARN
, "dqflush (incore copy)")) {
1223 xfs_force_shutdown(dqp
->q_mount
, SHUTDOWN_CORRUPT_INCORE
);
1224 return XFS_ERROR(EIO
);
1227 /* This is the only portion of data that needs to persist */
1228 memcpy(ddqp
, &(dqp
->q_core
), sizeof(xfs_disk_dquot_t
));
1231 * Clear the dirty field and remember the flush lsn for later use.
1233 dqp
->dq_flags
&= ~(XFS_DQ_DIRTY
);
1236 xfs_trans_ail_copy_lsn(mp
->m_ail
, &dqp
->q_logitem
.qli_flush_lsn
,
1237 &dqp
->q_logitem
.qli_item
.li_lsn
);
1240 * Attach an iodone routine so that we can remove this dquot from the
1241 * AIL and release the flush lock once the dquot is synced to disk.
1243 xfs_buf_attach_iodone(bp
, (void(*)(xfs_buf_t
*, xfs_log_item_t
*))
1244 xfs_qm_dqflush_done
, &(dqp
->q_logitem
.qli_item
));
1246 * If the buffer is pinned then push on the log so we won't
1247 * get stuck waiting in the write for too long.
1249 if (XFS_BUF_ISPINNED(bp
)) {
1250 trace_xfs_dqflush_force(dqp
);
1251 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
1254 if (flags
& XFS_QMOPT_DELWRI
) {
1255 xfs_bdwrite(mp
, bp
);
1256 } else if (flags
& XFS_QMOPT_ASYNC
) {
1257 error
= xfs_bawrite(mp
, bp
);
1259 error
= xfs_bwrite(mp
, bp
);
1262 trace_xfs_dqflush_done(dqp
);
1265 * dqp is still locked, but caller is free to unlock it now.
1272 * This is the dquot flushing I/O completion routine. It is called
1273 * from interrupt level when the buffer containing the dquot is
1274 * flushed to disk. It is responsible for removing the dquot logitem
1275 * from the AIL if it has not been re-logged, and unlocking the dquot's
1276 * flush lock. This behavior is very similar to that of inodes..
1280 xfs_qm_dqflush_done(
1282 xfs_dq_logitem_t
*qip
)
1285 struct xfs_ail
*ailp
;
1287 dqp
= qip
->qli_dquot
;
1288 ailp
= qip
->qli_item
.li_ailp
;
1291 * We only want to pull the item from the AIL if its
1292 * location in the log has not changed since we started the flush.
1293 * Thus, we only bother if the dquot's lsn has
1294 * not changed. First we check the lsn outside the lock
1295 * since it's cheaper, and then we recheck while
1296 * holding the lock before removing the dquot from the AIL.
1298 if ((qip
->qli_item
.li_flags
& XFS_LI_IN_AIL
) &&
1299 qip
->qli_item
.li_lsn
== qip
->qli_flush_lsn
) {
1301 /* xfs_trans_ail_delete() drops the AIL lock. */
1302 spin_lock(&ailp
->xa_lock
);
1303 if (qip
->qli_item
.li_lsn
== qip
->qli_flush_lsn
)
1304 xfs_trans_ail_delete(ailp
, (xfs_log_item_t
*)qip
);
1306 spin_unlock(&ailp
->xa_lock
);
1310 * Release the dq's flush lock since we're done with it.
1316 xfs_qm_dqlock_nowait(
1319 return mutex_trylock(&dqp
->q_qlock
);
1326 mutex_lock(&dqp
->q_qlock
);
1333 mutex_unlock(&(dqp
->q_qlock
));
1334 if (dqp
->q_logitem
.qli_dquot
== dqp
) {
1335 /* Once was dqp->q_mount, but might just have been cleared */
1336 xfs_trans_unlocked_item(dqp
->q_logitem
.qli_item
.li_ailp
,
1337 (xfs_log_item_t
*)&(dqp
->q_logitem
));
1343 xfs_dqunlock_nonotify(
1346 mutex_unlock(&(dqp
->q_qlock
));
1350 * Lock two xfs_dquot structures.
1352 * To avoid deadlocks we always lock the quota structure with
1353 * the lowerd id first.
1362 if (be32_to_cpu(d1
->q_core
.d_id
) >
1363 be32_to_cpu(d2
->q_core
.d_id
)) {
1364 mutex_lock(&d2
->q_qlock
);
1365 mutex_lock_nested(&d1
->q_qlock
, XFS_QLOCK_NESTED
);
1367 mutex_lock(&d1
->q_qlock
);
1368 mutex_lock_nested(&d2
->q_qlock
, XFS_QLOCK_NESTED
);
1371 mutex_lock(&d1
->q_qlock
);
1373 mutex_lock(&d2
->q_qlock
);
1379 * Take a dquot out of the mount's dqlist as well as the hashlist.
1380 * This is called via unmount as well as quotaoff, and the purge
1381 * will always succeed unless there are soft (temp) references
1384 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1385 * that we're returning! XXXsup - not cool.
1392 xfs_dqhash_t
*thishash
;
1393 xfs_mount_t
*mp
= dqp
->q_mount
;
1395 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp
));
1396 ASSERT(mutex_is_locked(&dqp
->q_hash
->qh_lock
));
1400 * We really can't afford to purge a dquot that is
1401 * referenced, because these are hard refs.
1402 * It shouldn't happen in general because we went thru _all_ inodes in
1403 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1404 * However it is possible that we have dquots with temporary
1405 * references that are not attached to an inode. e.g. see xfs_setattr().
1407 if (dqp
->q_nrefs
!= 0) {
1409 mutex_unlock(&dqp
->q_hash
->qh_lock
);
1413 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp
));
1416 * If we're turning off quotas, we have to make sure that, for
1417 * example, we don't delete quota disk blocks while dquots are
1418 * in the process of getting written to those disk blocks.
1419 * This dquot might well be on AIL, and we can't leave it there
1420 * if we're turning off quotas. Basically, we need this flush
1421 * lock, and are willing to block on it.
1423 if (!xfs_dqflock_nowait(dqp
)) {
1425 * Block on the flush lock after nudging dquot buffer,
1428 xfs_qm_dqflock_pushbuf_wait(dqp
);
1432 * XXXIf we're turning this type of quotas off, we don't care
1433 * about the dirty metadata sitting in this dquot. OTOH, if
1434 * we're unmounting, we do care, so we flush it and wait.
1436 if (XFS_DQ_IS_DIRTY(dqp
)) {
1439 /* dqflush unlocks dqflock */
1441 * Given that dqpurge is a very rare occurrence, it is OK
1442 * that we're holding the hashlist and mplist locks
1443 * across the disk write. But, ... XXXsup
1445 * We don't care about getting disk errors here. We need
1446 * to purge this dquot anyway, so we go ahead regardless.
1448 error
= xfs_qm_dqflush(dqp
, XFS_QMOPT_SYNC
);
1450 xfs_fs_cmn_err(CE_WARN
, mp
,
1451 "xfs_qm_dqpurge: dquot %p flush failed", dqp
);
1454 ASSERT(atomic_read(&dqp
->q_pincount
) == 0);
1455 ASSERT(XFS_FORCED_SHUTDOWN(mp
) ||
1456 !(dqp
->q_logitem
.qli_item
.li_flags
& XFS_LI_IN_AIL
));
1458 thishash
= dqp
->q_hash
;
1459 XQM_HASHLIST_REMOVE(thishash
, dqp
);
1460 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp
)), dqp
);
1462 * XXX Move this to the front of the freelist, if we can get the
1465 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp
));
1467 dqp
->q_mount
= NULL
;
1469 dqp
->dq_flags
= XFS_DQ_INACTIVE
;
1470 memset(&dqp
->q_core
, 0, sizeof(dqp
->q_core
));
1473 mutex_unlock(&thishash
->qh_lock
);
1480 xfs_qm_dqprint(xfs_dquot_t
*dqp
)
1482 cmn_err(CE_DEBUG
, "-----------KERNEL DQUOT----------------");
1483 cmn_err(CE_DEBUG
, "---- dquotID = %d",
1484 (int)be32_to_cpu(dqp
->q_core
.d_id
));
1485 cmn_err(CE_DEBUG
, "---- type = %s", DQFLAGTO_TYPESTR(dqp
));
1486 cmn_err(CE_DEBUG
, "---- fs = 0x%p", dqp
->q_mount
);
1487 cmn_err(CE_DEBUG
, "---- blkno = 0x%x", (int) dqp
->q_blkno
);
1488 cmn_err(CE_DEBUG
, "---- boffset = 0x%x", (int) dqp
->q_bufoffset
);
1489 cmn_err(CE_DEBUG
, "---- blkhlimit = %Lu (0x%x)",
1490 be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
),
1491 (int)be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
));
1492 cmn_err(CE_DEBUG
, "---- blkslimit = %Lu (0x%x)",
1493 be64_to_cpu(dqp
->q_core
.d_blk_softlimit
),
1494 (int)be64_to_cpu(dqp
->q_core
.d_blk_softlimit
));
1495 cmn_err(CE_DEBUG
, "---- inohlimit = %Lu (0x%x)",
1496 be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
),
1497 (int)be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
));
1498 cmn_err(CE_DEBUG
, "---- inoslimit = %Lu (0x%x)",
1499 be64_to_cpu(dqp
->q_core
.d_ino_softlimit
),
1500 (int)be64_to_cpu(dqp
->q_core
.d_ino_softlimit
));
1501 cmn_err(CE_DEBUG
, "---- bcount = %Lu (0x%x)",
1502 be64_to_cpu(dqp
->q_core
.d_bcount
),
1503 (int)be64_to_cpu(dqp
->q_core
.d_bcount
));
1504 cmn_err(CE_DEBUG
, "---- icount = %Lu (0x%x)",
1505 be64_to_cpu(dqp
->q_core
.d_icount
),
1506 (int)be64_to_cpu(dqp
->q_core
.d_icount
));
1507 cmn_err(CE_DEBUG
, "---- btimer = %d",
1508 (int)be32_to_cpu(dqp
->q_core
.d_btimer
));
1509 cmn_err(CE_DEBUG
, "---- itimer = %d",
1510 (int)be32_to_cpu(dqp
->q_core
.d_itimer
));
1511 cmn_err(CE_DEBUG
, "---------------------------");
1516 * Give the buffer a little push if it is incore and
1517 * wait on the flush lock.
1520 xfs_qm_dqflock_pushbuf_wait(
1526 * Check to see if the dquot has been flushed delayed
1527 * write. If so, grab its buffer and send it
1528 * out immediately. We'll be able to acquire
1529 * the flush lock when the I/O completes.
1531 bp
= xfs_incore(dqp
->q_mount
->m_ddev_targp
, dqp
->q_blkno
,
1532 XFS_QI_DQCHUNKLEN(dqp
->q_mount
),
1533 XFS_INCORE_TRYLOCK
);
1535 if (XFS_BUF_ISDELAYWRITE(bp
)) {
1537 if (XFS_BUF_ISPINNED(bp
)) {
1538 xfs_log_force(dqp
->q_mount
,
1542 error
= xfs_bawrite(dqp
->q_mount
, bp
);
1544 xfs_fs_cmn_err(CE_WARN
, dqp
->q_mount
,
1545 "xfs_qm_dqflock_pushbuf_wait: "
1546 "pushbuf error %d on dqp %p, bp %p",