2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_trans_priv.h"
57 dquot hash-chain lock (hashlock)
58 xqm dquot freelist lock (freelistlock
59 mount's dquot list lock (mplistlock)
60 user dquot lock - lock ordering among dquots is based on the uid or gid
61 group dquot lock - similar to udquots. Between the two dquots, the udquot
62 has to be locked first.
63 pin lock - the dquot lock must be held to take this lock.
67 STATIC
void xfs_qm_dqflush_done(xfs_buf_t
*, xfs_dq_logitem_t
*);
70 xfs_buftarg_t
*xfs_dqerror_target
;
73 int xfs_dqerror_mod
= 33;
77 * Allocate and initialize a dquot. We don't always allocate fresh memory;
78 * we try to reclaim a free dquot if the number of incore dquots are above
80 * The only field inside the core that gets initialized at this point
81 * is the d_id field. The idea is to fill in the entire q_core
82 * when we read in the on disk dquot.
91 boolean_t brandnewdquot
;
93 brandnewdquot
= xfs_qm_dqalloc_incore(&dqp
);
95 dqp
->q_core
.d_id
= cpu_to_be32(id
);
99 * No need to re-initialize these if this is a reclaimed dquot.
102 dqp
->dq_flnext
= dqp
->dq_flprev
= dqp
;
103 mutex_init(&dqp
->q_qlock
);
104 initnsema(&dqp
->q_flock
, 1, "fdq");
105 sv_init(&dqp
->q_pinwait
, SV_DEFAULT
, "pdq");
107 #ifdef XFS_DQUOT_TRACE
108 dqp
->q_trace
= ktrace_alloc(DQUOT_TRACE_SIZE
, KM_SLEEP
);
109 xfs_dqtrace_entry(dqp
, "DQINIT");
113 * Only the q_core portion was zeroed in dqreclaim_one().
114 * So, we need to reset others.
118 dqp
->MPL_NEXT
= dqp
->HL_NEXT
= NULL
;
119 dqp
->HL_PREVP
= dqp
->MPL_PREVP
= NULL
;
120 dqp
->q_bufoffset
= 0;
121 dqp
->q_fileoffset
= 0;
122 dqp
->q_transp
= NULL
;
123 dqp
->q_gdquot
= NULL
;
124 dqp
->q_res_bcount
= 0;
125 dqp
->q_res_icount
= 0;
126 dqp
->q_res_rtbcount
= 0;
129 ASSERT(dqp
->dq_flnext
== dqp
->dq_flprev
);
131 #ifdef XFS_DQUOT_TRACE
132 ASSERT(dqp
->q_trace
);
133 xfs_dqtrace_entry(dqp
, "DQRECLAIMED_INIT");
138 * log item gets initialized later
144 * This is called to free all the memory associated with a dquot
150 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp
));
152 mutex_destroy(&dqp
->q_qlock
);
153 freesema(&dqp
->q_flock
);
154 sv_destroy(&dqp
->q_pinwait
);
156 #ifdef XFS_DQUOT_TRACE
158 ktrace_free(dqp
->q_trace
);
161 kmem_zone_free(xfs_Gqm
->qm_dqzone
, dqp
);
162 atomic_dec(&xfs_Gqm
->qm_totaldquots
);
166 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
175 * Caller has zero'd the entire dquot 'chunk' already.
177 d
->dd_diskdq
.d_magic
= cpu_to_be16(XFS_DQUOT_MAGIC
);
178 d
->dd_diskdq
.d_version
= XFS_DQUOT_VERSION
;
179 d
->dd_diskdq
.d_id
= cpu_to_be32(id
);
180 d
->dd_diskdq
.d_flags
= type
;
184 #ifdef XFS_DQUOT_TRACE
186 * Dquot tracing for debugging.
196 xfs_dquot_t
*udqp
= NULL
;
199 ASSERT(dqp
->q_trace
);
204 ktrace_enter(dqp
->q_trace
,
205 (void *)(__psint_t
)DQUOT_KTRACE_ENTRY
,
207 (void *)(__psint_t
)dqp
->q_nrefs
,
208 (void *)(__psint_t
)dqp
->dq_flags
,
209 (void *)(__psint_t
)dqp
->q_res_bcount
,
210 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_bcount
),
211 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_icount
),
212 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
),
213 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_blk_softlimit
),
214 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
),
215 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_ino_softlimit
),
216 (void *)(__psint_t
)be32_to_cpu(dqp
->q_core
.d_id
),
217 (void *)(__psint_t
)current_pid(),
218 (void *)(__psint_t
)ino
,
219 (void *)(__psint_t
)retaddr
,
220 (void *)(__psint_t
)udqp
);
227 * If default limits are in force, push them into the dquot now.
228 * We overwrite the dquot limits only if they are zero and this
229 * is not the root dquot.
232 xfs_qm_adjust_dqlimits(
236 xfs_quotainfo_t
*q
= mp
->m_quotainfo
;
240 if (q
->qi_bsoftlimit
&& !d
->d_blk_softlimit
)
241 d
->d_blk_softlimit
= cpu_to_be64(q
->qi_bsoftlimit
);
242 if (q
->qi_bhardlimit
&& !d
->d_blk_hardlimit
)
243 d
->d_blk_hardlimit
= cpu_to_be64(q
->qi_bhardlimit
);
244 if (q
->qi_isoftlimit
&& !d
->d_ino_softlimit
)
245 d
->d_ino_softlimit
= cpu_to_be64(q
->qi_isoftlimit
);
246 if (q
->qi_ihardlimit
&& !d
->d_ino_hardlimit
)
247 d
->d_ino_hardlimit
= cpu_to_be64(q
->qi_ihardlimit
);
248 if (q
->qi_rtbsoftlimit
&& !d
->d_rtb_softlimit
)
249 d
->d_rtb_softlimit
= cpu_to_be64(q
->qi_rtbsoftlimit
);
250 if (q
->qi_rtbhardlimit
&& !d
->d_rtb_hardlimit
)
251 d
->d_rtb_hardlimit
= cpu_to_be64(q
->qi_rtbhardlimit
);
255 * Check the limits and timers of a dquot and start or reset timers
257 * This gets called even when quota enforcement is OFF, which makes our
258 * life a little less complicated. (We just don't reject any quota
259 * reservations in that case, when enforcement is off).
260 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
262 * In contrast, warnings are a little different in that they don't
263 * 'automatically' get started when limits get exceeded. They do
264 * get reset to zero, however, when we find the count to be under
265 * the soft limit (they are only ever set non-zero via userspace).
268 xfs_qm_adjust_dqtimers(
275 if (d
->d_blk_hardlimit
)
276 ASSERT(be64_to_cpu(d
->d_blk_softlimit
) <=
277 be64_to_cpu(d
->d_blk_hardlimit
));
278 if (d
->d_ino_hardlimit
)
279 ASSERT(be64_to_cpu(d
->d_ino_softlimit
) <=
280 be64_to_cpu(d
->d_ino_hardlimit
));
281 if (d
->d_rtb_hardlimit
)
282 ASSERT(be64_to_cpu(d
->d_rtb_softlimit
) <=
283 be64_to_cpu(d
->d_rtb_hardlimit
));
286 if ((d
->d_blk_softlimit
&&
287 (be64_to_cpu(d
->d_bcount
) >=
288 be64_to_cpu(d
->d_blk_softlimit
))) ||
289 (d
->d_blk_hardlimit
&&
290 (be64_to_cpu(d
->d_bcount
) >=
291 be64_to_cpu(d
->d_blk_hardlimit
)))) {
292 d
->d_btimer
= cpu_to_be32(get_seconds() +
293 XFS_QI_BTIMELIMIT(mp
));
298 if ((!d
->d_blk_softlimit
||
299 (be64_to_cpu(d
->d_bcount
) <
300 be64_to_cpu(d
->d_blk_softlimit
))) &&
301 (!d
->d_blk_hardlimit
||
302 (be64_to_cpu(d
->d_bcount
) <
303 be64_to_cpu(d
->d_blk_hardlimit
)))) {
309 if ((d
->d_ino_softlimit
&&
310 (be64_to_cpu(d
->d_icount
) >=
311 be64_to_cpu(d
->d_ino_softlimit
))) ||
312 (d
->d_ino_hardlimit
&&
313 (be64_to_cpu(d
->d_icount
) >=
314 be64_to_cpu(d
->d_ino_hardlimit
)))) {
315 d
->d_itimer
= cpu_to_be32(get_seconds() +
316 XFS_QI_ITIMELIMIT(mp
));
321 if ((!d
->d_ino_softlimit
||
322 (be64_to_cpu(d
->d_icount
) <
323 be64_to_cpu(d
->d_ino_softlimit
))) &&
324 (!d
->d_ino_hardlimit
||
325 (be64_to_cpu(d
->d_icount
) <
326 be64_to_cpu(d
->d_ino_hardlimit
)))) {
331 if (!d
->d_rtbtimer
) {
332 if ((d
->d_rtb_softlimit
&&
333 (be64_to_cpu(d
->d_rtbcount
) >=
334 be64_to_cpu(d
->d_rtb_softlimit
))) ||
335 (d
->d_rtb_hardlimit
&&
336 (be64_to_cpu(d
->d_rtbcount
) >=
337 be64_to_cpu(d
->d_rtb_hardlimit
)))) {
338 d
->d_rtbtimer
= cpu_to_be32(get_seconds() +
339 XFS_QI_RTBTIMELIMIT(mp
));
344 if ((!d
->d_rtb_softlimit
||
345 (be64_to_cpu(d
->d_rtbcount
) <
346 be64_to_cpu(d
->d_rtb_softlimit
))) &&
347 (!d
->d_rtb_hardlimit
||
348 (be64_to_cpu(d
->d_rtbcount
) <
349 be64_to_cpu(d
->d_rtb_hardlimit
)))) {
356 * initialize a buffer full of dquots and log the whole thing
359 xfs_qm_init_dquot_blk(
370 ASSERT(XFS_BUF_ISBUSY(bp
));
371 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
373 d
= (xfs_dqblk_t
*)XFS_BUF_PTR(bp
);
376 * ID of the first dquot in the block - id's are zero based.
378 curid
= id
- (id
% XFS_QM_DQPERBLK(mp
));
380 memset(d
, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp
)));
381 for (i
= 0; i
< XFS_QM_DQPERBLK(mp
); i
++, d
++, curid
++)
382 xfs_qm_dqinit_core(curid
, type
, d
);
383 xfs_trans_dquot_buf(tp
, bp
,
384 (type
& XFS_DQ_USER
? XFS_BLI_UDQUOT_BUF
:
385 ((type
& XFS_DQ_PROJ
) ? XFS_BLI_PDQUOT_BUF
:
386 XFS_BLI_GDQUOT_BUF
)));
387 xfs_trans_log_buf(tp
, bp
, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp
)) - 1);
393 * Allocate a block and fill it with dquots.
394 * This is called when the bmapi finds a hole.
402 xfs_fileoff_t offset_fsb
,
405 xfs_fsblock_t firstblock
;
406 xfs_bmap_free_t flist
;
408 int nmaps
, error
, committed
;
410 xfs_trans_t
*tp
= *tpp
;
413 xfs_dqtrace_entry(dqp
, "DQALLOC");
416 * Initialize the bmap freelist prior to calling bmapi code.
418 XFS_BMAP_INIT(&flist
, &firstblock
);
419 xfs_ilock(quotip
, XFS_ILOCK_EXCL
);
421 * Return if this type of quotas is turned off while we didn't
424 if (XFS_IS_THIS_QUOTA_OFF(dqp
)) {
425 xfs_iunlock(quotip
, XFS_ILOCK_EXCL
);
430 * xfs_trans_commit normally decrements the vnode ref count
431 * when it unlocks the inode. Since we want to keep the quota
432 * inode around, we bump the vnode ref count now.
434 VN_HOLD(XFS_ITOV(quotip
));
436 xfs_trans_ijoin(tp
, quotip
, XFS_ILOCK_EXCL
);
438 if ((error
= xfs_bmapi(tp
, quotip
,
439 offset_fsb
, XFS_DQUOT_CLUSTER_SIZE_FSB
,
440 XFS_BMAPI_METADATA
| XFS_BMAPI_WRITE
,
442 XFS_QM_DQALLOC_SPACE_RES(mp
),
443 &map
, &nmaps
, &flist
, NULL
))) {
446 ASSERT(map
.br_blockcount
== XFS_DQUOT_CLUSTER_SIZE_FSB
);
448 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
449 (map
.br_startblock
!= HOLESTARTBLOCK
));
452 * Keep track of the blkno to save a lookup later
454 dqp
->q_blkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
456 /* now we can just get the buffer (there's nothing to read yet) */
457 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
459 XFS_QI_DQCHUNKLEN(mp
),
461 if (!bp
|| (error
= XFS_BUF_GETERROR(bp
)))
464 * Make a chunk of dquots out of this buffer and log
467 xfs_qm_init_dquot_blk(tp
, mp
, be32_to_cpu(dqp
->q_core
.d_id
),
468 dqp
->dq_flags
& XFS_DQ_ALLTYPES
, bp
);
471 * xfs_bmap_finish() may commit the current transaction and
472 * start a second transaction if the freelist is not empty.
474 * Since we still want to modify this buffer, we need to
475 * ensure that the buffer is not released on commit of
476 * the first transaction and ensure the buffer is added to the
477 * second transaction.
479 * If there is only one transaction then don't stop the buffer
480 * from being released when it commits later on.
483 xfs_trans_bhold(tp
, bp
);
485 if ((error
= xfs_bmap_finish(tpp
, &flist
, &committed
))) {
491 xfs_trans_bjoin(tp
, bp
);
493 xfs_trans_bhold_release(tp
, bp
);
500 xfs_bmap_cancel(&flist
);
502 xfs_iunlock(quotip
, XFS_ILOCK_EXCL
);
508 * Maps a dquot to the buffer containing its on-disk version.
509 * This returns a ptr to the buffer containing the on-disk dquot
510 * in the bpp param, and a ptr to the on-disk dquot within that buffer
516 xfs_disk_dquot_t
**O_ddpp
,
525 xfs_disk_dquot_t
*ddq
;
528 xfs_trans_t
*tp
= (tpp
? *tpp
: NULL
);
531 id
= be32_to_cpu(dqp
->q_core
.d_id
);
536 * If we don't know where the dquot lives, find out.
538 if (dqp
->q_blkno
== (xfs_daddr_t
) 0) {
539 /* We use the id as an index */
540 dqp
->q_fileoffset
= (xfs_fileoff_t
)id
/ XFS_QM_DQPERBLK(mp
);
542 quotip
= XFS_DQ_TO_QIP(dqp
);
543 xfs_ilock(quotip
, XFS_ILOCK_SHARED
);
545 * Return if this type of quotas is turned off while we didn't
548 if (XFS_IS_THIS_QUOTA_OFF(dqp
)) {
549 xfs_iunlock(quotip
, XFS_ILOCK_SHARED
);
553 * Find the block map; no allocations yet
555 error
= xfs_bmapi(NULL
, quotip
, dqp
->q_fileoffset
,
556 XFS_DQUOT_CLUSTER_SIZE_FSB
,
558 NULL
, 0, &map
, &nmaps
, NULL
, NULL
);
560 xfs_iunlock(quotip
, XFS_ILOCK_SHARED
);
564 ASSERT(map
.br_blockcount
== 1);
567 * offset of dquot in the (fixed sized) dquot chunk.
569 dqp
->q_bufoffset
= (id
% XFS_QM_DQPERBLK(mp
)) *
571 if (map
.br_startblock
== HOLESTARTBLOCK
) {
573 * We don't allocate unless we're asked to
575 if (!(flags
& XFS_QMOPT_DQALLOC
))
579 if ((error
= xfs_qm_dqalloc(tpp
, mp
, dqp
, quotip
,
580 dqp
->q_fileoffset
, &bp
)))
586 * store the blkno etc so that we don't have to do the
587 * mapping all the time
589 dqp
->q_blkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
592 ASSERT(dqp
->q_blkno
!= DELAYSTARTBLOCK
);
593 ASSERT(dqp
->q_blkno
!= HOLESTARTBLOCK
);
596 * Read in the buffer, unless we've just done the allocation
597 * (in which case we already have the buf).
600 xfs_dqtrace_entry(dqp
, "DQTOBP READBUF");
601 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
603 XFS_QI_DQCHUNKLEN(mp
),
608 return XFS_ERROR(error
);
610 ASSERT(XFS_BUF_ISBUSY(bp
));
611 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
614 * calculate the location of the dquot inside the buffer.
616 ddq
= (xfs_disk_dquot_t
*)((char *)XFS_BUF_PTR(bp
) + dqp
->q_bufoffset
);
619 * A simple sanity check in case we got a corrupted dquot...
621 if (xfs_qm_dqcheck(ddq
, id
, dqp
->dq_flags
& XFS_DQ_ALLTYPES
,
622 flags
& (XFS_QMOPT_DQREPAIR
|XFS_QMOPT_DOWARN
),
624 if (!(flags
& XFS_QMOPT_DQREPAIR
)) {
625 xfs_trans_brelse(tp
, bp
);
626 return XFS_ERROR(EIO
);
628 XFS_BUF_BUSY(bp
); /* We dirtied this */
639 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
640 * and release the buffer immediately.
648 xfs_dquot_t
*dqp
, /* dquot to get filled in */
651 xfs_disk_dquot_t
*ddqp
;
659 * get a pointer to the on-disk dquot and the buffer containing it
660 * dqp already knows its own type (GROUP/USER).
662 xfs_dqtrace_entry(dqp
, "DQREAD");
663 if ((error
= xfs_qm_dqtobp(tpp
, dqp
, &ddqp
, &bp
, flags
))) {
668 /* copy everything from disk dquot to the incore dquot */
669 memcpy(&dqp
->q_core
, ddqp
, sizeof(xfs_disk_dquot_t
));
670 ASSERT(be32_to_cpu(dqp
->q_core
.d_id
) == id
);
671 xfs_qm_dquot_logitem_init(dqp
);
674 * Reservation counters are defined as reservation plus current usage
675 * to avoid having to add everytime.
677 dqp
->q_res_bcount
= be64_to_cpu(ddqp
->d_bcount
);
678 dqp
->q_res_icount
= be64_to_cpu(ddqp
->d_icount
);
679 dqp
->q_res_rtbcount
= be64_to_cpu(ddqp
->d_rtbcount
);
681 /* Mark the buf so that this will stay incore a little longer */
682 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_DQUOT
, XFS_DQUOT_REF
);
685 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
686 * So we need to release with xfs_trans_brelse().
687 * The strategy here is identical to that of inodes; we lock
688 * the dquot in xfs_qm_dqget() before making it accessible to
689 * others. This is because dquots, like inodes, need a good level of
690 * concurrency, and we don't want to take locks on the entire buffers
691 * for dquot accesses.
692 * Note also that the dquot buffer may even be dirty at this point, if
693 * this particular dquot was repaired. We still aren't afraid to
694 * brelse it because we have the changes incore.
696 ASSERT(XFS_BUF_ISBUSY(bp
));
697 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
698 xfs_trans_brelse(tp
, bp
);
705 * allocate an incore dquot from the kernel heap,
706 * and fill its core with quota information kept on disk.
707 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
708 * if it wasn't already allocated.
713 xfs_dqid_t id
, /* gid or uid, depending on type */
714 uint type
, /* UDQUOT or GDQUOT */
715 uint flags
, /* DQALLOC, DQREPAIR */
716 xfs_dquot_t
**O_dqpp
)/* OUT : incore dquot, not locked */
723 dqp
= xfs_qm_dqinit(mp
, id
, type
);
725 if (flags
& XFS_QMOPT_DQALLOC
) {
726 tp
= xfs_trans_alloc(mp
, XFS_TRANS_QM_DQALLOC
);
727 if ((error
= xfs_trans_reserve(tp
,
728 XFS_QM_DQALLOC_SPACE_RES(mp
),
729 XFS_WRITE_LOG_RES(mp
) +
730 BBTOB(XFS_QI_DQCHUNKLEN(mp
)) - 1 +
733 XFS_TRANS_PERM_LOG_RES
,
734 XFS_WRITE_LOG_COUNT
))) {
738 cancelflags
= XFS_TRANS_RELEASE_LOG_RES
;
742 * Read it from disk; xfs_dqread() takes care of
743 * all the necessary initialization of dquot's fields (locks, etc)
745 if ((error
= xfs_qm_dqread(&tp
, id
, dqp
, flags
))) {
747 * This can happen if quotas got turned off (ESRCH),
748 * or if the dquot didn't exist on disk and we ask to
751 xfs_dqtrace_entry(dqp
, "DQREAD FAIL");
752 cancelflags
|= XFS_TRANS_ABORT
;
756 if ((error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
,
767 xfs_trans_cancel(tp
, cancelflags
);
769 xfs_qm_dqdestroy(dqp
);
775 * Lookup a dquot in the incore dquot hashtable. We keep two separate
776 * hashtables for user and group dquots; and, these are global tables
777 * inside the XQM, not per-filesystem tables.
778 * The hash chain must be locked by caller, and it is left locked
779 * on return. Returning dquot is locked.
786 xfs_dquot_t
**O_dqpp
)
792 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
794 flist_locked
= B_FALSE
;
797 * Traverse the hashchain looking for a match
799 for (dqp
= qh
->qh_next
; dqp
!= NULL
; dqp
= dqp
->HL_NEXT
) {
801 * We already have the hashlock. We don't need the
802 * dqlock to look at the id field of the dquot, since the
803 * id can't be modified without the hashlock anyway.
805 if (be32_to_cpu(dqp
->q_core
.d_id
) == id
&& dqp
->q_mount
== mp
) {
806 xfs_dqtrace_entry(dqp
, "DQFOUND BY LOOKUP");
808 * All in core dquots must be on the dqlist of mp
810 ASSERT(dqp
->MPL_PREVP
!= NULL
);
813 if (dqp
->q_nrefs
== 0) {
814 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp
));
815 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm
)) {
816 xfs_dqtrace_entry(dqp
, "DQLOOKUP: WANT");
819 * We may have raced with dqreclaim_one()
820 * (and lost). So, flag that we don't
821 * want the dquot to be reclaimed.
823 dqp
->dq_flags
|= XFS_DQ_WANT
;
825 xfs_qm_freelist_lock(xfs_Gqm
);
827 dqp
->dq_flags
&= ~(XFS_DQ_WANT
);
829 flist_locked
= B_TRUE
;
833 * id couldn't have changed; we had the hashlock all
836 ASSERT(be32_to_cpu(dqp
->q_core
.d_id
) == id
);
839 if (dqp
->q_nrefs
!= 0) {
840 xfs_qm_freelist_unlock(xfs_Gqm
);
841 flist_locked
= B_FALSE
;
844 * take it off the freelist
846 xfs_dqtrace_entry(dqp
,
847 "DQLOOKUP: TAKEOFF FL");
848 XQM_FREELIST_REMOVE(dqp
);
849 /* xfs_qm_freelist_print(&(xfs_Gqm->
861 xfs_qm_freelist_unlock(xfs_Gqm
);
863 * move the dquot to the front of the hashchain
865 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
866 if (dqp
->HL_PREVP
!= &qh
->qh_next
) {
867 xfs_dqtrace_entry(dqp
,
868 "DQLOOKUP: HASH MOVETOFRONT");
869 if ((d
= dqp
->HL_NEXT
))
870 d
->HL_PREVP
= dqp
->HL_PREVP
;
871 *(dqp
->HL_PREVP
) = d
;
873 d
->HL_PREVP
= &dqp
->HL_NEXT
;
875 dqp
->HL_PREVP
= &qh
->qh_next
;
878 xfs_dqtrace_entry(dqp
, "LOOKUP END");
880 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
886 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
891 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
892 * a locked dquot, doing an allocation (if requested) as needed.
893 * When both an inode and an id are given, the inode's id takes precedence.
894 * That is, if the id changes while we don't hold the ilock inside this
895 * function, the new dquot is returned, not necessarily the one requested
896 * in the id argument.
901 xfs_inode_t
*ip
, /* locked inode (optional) */
902 xfs_dqid_t id
, /* uid/projid/gid depending on type */
903 uint type
, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
904 uint flags
, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
905 xfs_dquot_t
**O_dqpp
) /* OUT : locked incore dquot */
912 ASSERT(XFS_IS_QUOTA_RUNNING(mp
));
913 if ((! XFS_IS_UQUOTA_ON(mp
) && type
== XFS_DQ_USER
) ||
914 (! XFS_IS_PQUOTA_ON(mp
) && type
== XFS_DQ_PROJ
) ||
915 (! XFS_IS_GQUOTA_ON(mp
) && type
== XFS_DQ_GROUP
)) {
918 h
= XFS_DQ_HASH(mp
, id
, type
);
921 if (xfs_do_dqerror
) {
922 if ((xfs_dqerror_target
== mp
->m_ddev_targp
) &&
923 (xfs_dqreq_num
++ % xfs_dqerror_mod
) == 0) {
924 cmn_err(CE_DEBUG
, "Returning error in dqget");
933 ASSERT(type
== XFS_DQ_USER
||
934 type
== XFS_DQ_PROJ
||
935 type
== XFS_DQ_GROUP
);
937 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip
));
938 if (type
== XFS_DQ_USER
)
939 ASSERT(ip
->i_udquot
== NULL
);
941 ASSERT(ip
->i_gdquot
== NULL
);
947 * Look in the cache (hashtable).
948 * The chain is kept locked during lookup.
950 if (xfs_qm_dqlookup(mp
, id
, h
, O_dqpp
) == 0) {
951 XQM_STATS_INC(xqmstats
.xs_qm_dqcachehits
);
953 * The dquot was found, moved to the front of the chain,
954 * taken off the freelist if it was on it, and locked
955 * at this point. Just unlock the hashchain and return.
958 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp
));
959 XFS_DQ_HASH_UNLOCK(h
);
960 xfs_dqtrace_entry(*O_dqpp
, "DQGET DONE (FROM CACHE)");
961 return (0); /* success */
963 XQM_STATS_INC(xqmstats
.xs_qm_dqcachemisses
);
966 * Dquot cache miss. We don't want to keep the inode lock across
967 * a (potential) disk read. Also we don't want to deal with the lock
968 * ordering between quotainode and this inode. OTOH, dropping the inode
969 * lock here means dealing with a chown that can happen before
970 * we re-acquire the lock.
973 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
975 * Save the hashchain version stamp, and unlock the chain, so that
976 * we don't keep the lock across a disk read
978 version
= h
->qh_version
;
979 XFS_DQ_HASH_UNLOCK(h
);
982 * Allocate the dquot on the kernel heap, and read the ondisk
983 * portion off the disk. Also, do all the necessary initialization
984 * This can return ENOENT if dquot didn't exist on disk and we didn't
985 * ask it to allocate; ESRCH if quotas got turned off suddenly.
987 if ((error
= xfs_qm_idtodq(mp
, id
, type
,
988 flags
& (XFS_QMOPT_DQALLOC
|XFS_QMOPT_DQREPAIR
|
992 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
997 * See if this is mount code calling to look at the overall quota limits
998 * which are stored in the id == 0 user or group's dquot.
999 * Since we may not have done a quotacheck by this point, just return
1000 * the dquot without attaching it to any hashtables, lists, etc, or even
1001 * taking a reference.
1002 * The caller must dqdestroy this once done.
1004 if (flags
& XFS_QMOPT_DQSUSER
) {
1011 * Dquot lock comes after hashlock in the lock ordering
1014 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1015 if (! XFS_IS_DQTYPE_ON(mp
, type
)) {
1016 /* inode stays locked on return */
1017 xfs_qm_dqdestroy(dqp
);
1018 return XFS_ERROR(ESRCH
);
1021 * A dquot could be attached to this inode by now, since
1022 * we had dropped the ilock.
1024 if (type
== XFS_DQ_USER
) {
1026 xfs_qm_dqdestroy(dqp
);
1033 xfs_qm_dqdestroy(dqp
);
1042 * Hashlock comes after ilock in lock order
1044 XFS_DQ_HASH_LOCK(h
);
1045 if (version
!= h
->qh_version
) {
1046 xfs_dquot_t
*tmpdqp
;
1048 * Now, see if somebody else put the dquot in the
1049 * hashtable before us. This can happen because we didn't
1050 * keep the hashchain lock. We don't have to worry about
1051 * lock order between the two dquots here since dqp isn't
1052 * on any findable lists yet.
1054 if (xfs_qm_dqlookup(mp
, id
, h
, &tmpdqp
) == 0) {
1056 * Duplicate found. Just throw away the new dquot
1059 xfs_qm_dqput(tmpdqp
);
1060 XFS_DQ_HASH_UNLOCK(h
);
1061 xfs_qm_dqdestroy(dqp
);
1062 XQM_STATS_INC(xqmstats
.xs_qm_dquot_dups
);
1068 * Put the dquot at the beginning of the hash-chain and mp's list
1069 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1071 ASSERT(XFS_DQ_IS_HASH_LOCKED(h
));
1073 XQM_HASHLIST_INSERT(h
, dqp
);
1076 * Attach this dquot to this filesystem's list of all dquots,
1077 * kept inside the mount structure in m_quotainfo field
1079 xfs_qm_mplist_lock(mp
);
1082 * We return a locked dquot to the caller, with a reference taken
1087 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp
)), dqp
);
1089 xfs_qm_mplist_unlock(mp
);
1090 XFS_DQ_HASH_UNLOCK(h
);
1092 ASSERT((ip
== NULL
) || XFS_ISLOCKED_INODE_EXCL(ip
));
1093 xfs_dqtrace_entry(dqp
, "DQGET DONE");
1100 * Release a reference to the dquot (decrement ref-count)
1101 * and unlock it. If there is a group quota attached to this
1102 * dquot, carefully release that too without tripping over
1103 * deadlocks'n'stuff.
1111 ASSERT(dqp
->q_nrefs
> 0);
1112 ASSERT(XFS_DQ_IS_LOCKED(dqp
));
1113 xfs_dqtrace_entry(dqp
, "DQPUT");
1115 if (dqp
->q_nrefs
!= 1) {
1122 * drop the dqlock and acquire the freelist and dqlock
1123 * in the right order; but try to get it out-of-order first
1125 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm
)) {
1126 xfs_dqtrace_entry(dqp
, "DQPUT: FLLOCK-WAIT");
1128 xfs_qm_freelist_lock(xfs_Gqm
);
1135 /* We can't depend on nrefs being == 1 here */
1136 if (--dqp
->q_nrefs
== 0) {
1137 xfs_dqtrace_entry(dqp
, "DQPUT: ON FREELIST");
1139 * insert at end of the freelist.
1141 XQM_FREELIST_INSERT(&(xfs_Gqm
->qm_dqfreelist
), dqp
);
1144 * If we just added a udquot to the freelist, then
1145 * we want to release the gdquot reference that
1146 * it (probably) has. Otherwise it'll keep the
1147 * gdquot from getting reclaimed.
1149 if ((gdqp
= dqp
->q_gdquot
)) {
1151 * Avoid a recursive dqput call
1154 dqp
->q_gdquot
= NULL
;
1157 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1158 "@@@@@++ Free list (after append) @@@@@+");
1164 * If we had a group quota inside the user quota as a hint,
1171 xfs_qm_freelist_unlock(xfs_Gqm
);
1175 * Release a dquot. Flush it if dirty, then dqput() it.
1176 * dquot must not be locked.
1183 xfs_dqtrace_entry(dqp
, "DQRELE");
1187 * We don't care to flush it if the dquot is dirty here.
1188 * That will create stutters that we want to avoid.
1189 * Instead we do a delayed write when we try to reclaim
1190 * a dirty dquot. Also xfs_sync will take part of the burden...
1197 * Write a modified dquot to disk.
1198 * The dquot must be locked and the flush lock too taken by caller.
1199 * The flush lock will not be unlocked until the dquot reaches the disk,
1200 * but the dquot is free to be unlocked and modified by the caller
1201 * in the interim. Dquot is still locked on return. This behavior is
1202 * identical to that of inodes.
1211 xfs_disk_dquot_t
*ddqp
;
1215 ASSERT(XFS_DQ_IS_LOCKED(dqp
));
1216 ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp
));
1217 xfs_dqtrace_entry(dqp
, "DQFLUSH");
1220 * If not dirty, nada.
1222 if (!XFS_DQ_IS_DIRTY(dqp
)) {
1228 * Cant flush a pinned dquot. Wait for it.
1230 xfs_qm_dqunpin_wait(dqp
);
1233 * This may have been unpinned because the filesystem is shutting
1234 * down forcibly. If that's the case we must not write this dquot
1235 * to disk, because the log record didn't make it to disk!
1237 if (XFS_FORCED_SHUTDOWN(dqp
->q_mount
)) {
1238 dqp
->dq_flags
&= ~(XFS_DQ_DIRTY
);
1240 return XFS_ERROR(EIO
);
1244 * Get the buffer containing the on-disk dquot
1245 * We don't need a transaction envelope because we know that the
1246 * the ondisk-dquot has already been allocated for.
1248 if ((error
= xfs_qm_dqtobp(NULL
, dqp
, &ddqp
, &bp
, XFS_QMOPT_DOWARN
))) {
1249 xfs_dqtrace_entry(dqp
, "DQTOBP FAIL");
1250 ASSERT(error
!= ENOENT
);
1252 * Quotas could have gotten turned off (ESRCH)
1258 if (xfs_qm_dqcheck(&dqp
->q_core
, be32_to_cpu(ddqp
->d_id
),
1259 0, XFS_QMOPT_DOWARN
, "dqflush (incore copy)")) {
1260 xfs_force_shutdown(dqp
->q_mount
, SHUTDOWN_CORRUPT_INCORE
);
1261 return XFS_ERROR(EIO
);
1264 /* This is the only portion of data that needs to persist */
1265 memcpy(ddqp
, &(dqp
->q_core
), sizeof(xfs_disk_dquot_t
));
1268 * Clear the dirty field and remember the flush lsn for later use.
1270 dqp
->dq_flags
&= ~(XFS_DQ_DIRTY
);
1273 /* lsn is 64 bits */
1275 dqp
->q_logitem
.qli_flush_lsn
= dqp
->q_logitem
.qli_item
.li_lsn
;
1279 * Attach an iodone routine so that we can remove this dquot from the
1280 * AIL and release the flush lock once the dquot is synced to disk.
1282 xfs_buf_attach_iodone(bp
, (void(*)(xfs_buf_t
*, xfs_log_item_t
*))
1283 xfs_qm_dqflush_done
, &(dqp
->q_logitem
.qli_item
));
1285 * If the buffer is pinned then push on the log so we won't
1286 * get stuck waiting in the write for too long.
1288 if (XFS_BUF_ISPINNED(bp
)) {
1289 xfs_dqtrace_entry(dqp
, "DQFLUSH LOG FORCE");
1290 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
1293 if (flags
& XFS_QMOPT_DELWRI
) {
1294 xfs_bdwrite(mp
, bp
);
1295 } else if (flags
& XFS_QMOPT_ASYNC
) {
1296 xfs_bawrite(mp
, bp
);
1298 error
= xfs_bwrite(mp
, bp
);
1300 xfs_dqtrace_entry(dqp
, "DQFLUSH END");
1302 * dqp is still locked, but caller is free to unlock it now.
1309 * This is the dquot flushing I/O completion routine. It is called
1310 * from interrupt level when the buffer containing the dquot is
1311 * flushed to disk. It is responsible for removing the dquot logitem
1312 * from the AIL if it has not been re-logged, and unlocking the dquot's
1313 * flush lock. This behavior is very similar to that of inodes..
1317 xfs_qm_dqflush_done(
1319 xfs_dq_logitem_t
*qip
)
1324 dqp
= qip
->qli_dquot
;
1327 * We only want to pull the item from the AIL if its
1328 * location in the log has not changed since we started the flush.
1329 * Thus, we only bother if the dquot's lsn has
1330 * not changed. First we check the lsn outside the lock
1331 * since it's cheaper, and then we recheck while
1332 * holding the lock before removing the dquot from the AIL.
1334 if ((qip
->qli_item
.li_flags
& XFS_LI_IN_AIL
) &&
1335 qip
->qli_item
.li_lsn
== qip
->qli_flush_lsn
) {
1337 AIL_LOCK(dqp
->q_mount
, s
);
1339 * xfs_trans_delete_ail() drops the AIL lock.
1341 if (qip
->qli_item
.li_lsn
== qip
->qli_flush_lsn
)
1342 xfs_trans_delete_ail(dqp
->q_mount
,
1343 (xfs_log_item_t
*)qip
, s
);
1345 AIL_UNLOCK(dqp
->q_mount
, s
);
1349 * Release the dq's flush lock since we're done with it.
1356 xfs_qm_dqflock_nowait(
1361 locked
= cpsema(&((dqp
)->q_flock
));
1363 /* XXX ifdef these out */
1365 (dqp
)->dq_flags
|= XFS_DQ_FLOCKED
;
1371 xfs_qm_dqlock_nowait(
1374 return (mutex_trylock(&((dqp
)->q_qlock
)));
1381 mutex_lock(&(dqp
->q_qlock
));
1388 mutex_unlock(&(dqp
->q_qlock
));
1389 if (dqp
->q_logitem
.qli_dquot
== dqp
) {
1390 /* Once was dqp->q_mount, but might just have been cleared */
1391 xfs_trans_unlocked_item(dqp
->q_logitem
.qli_item
.li_mountp
,
1392 (xfs_log_item_t
*)&(dqp
->q_logitem
));
1398 xfs_dqunlock_nonotify(
1401 mutex_unlock(&(dqp
->q_qlock
));
1411 if (be32_to_cpu(d1
->q_core
.d_id
) >
1412 be32_to_cpu(d2
->q_core
.d_id
)) {
1430 * Take a dquot out of the mount's dqlist as well as the hashlist.
1431 * This is called via unmount as well as quotaoff, and the purge
1432 * will always succeed unless there are soft (temp) references
1435 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1436 * that we're returning! XXXsup - not cool.
1444 xfs_dqhash_t
*thishash
;
1449 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp
));
1450 ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp
->q_hash
));
1454 * We really can't afford to purge a dquot that is
1455 * referenced, because these are hard refs.
1456 * It shouldn't happen in general because we went thru _all_ inodes in
1457 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1458 * However it is possible that we have dquots with temporary
1459 * references that are not attached to an inode. e.g. see xfs_setattr().
1461 if (dqp
->q_nrefs
!= 0) {
1463 XFS_DQ_HASH_UNLOCK(dqp
->q_hash
);
1467 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp
));
1470 * If we're turning off quotas, we have to make sure that, for
1471 * example, we don't delete quota disk blocks while dquots are
1472 * in the process of getting written to those disk blocks.
1473 * This dquot might well be on AIL, and we can't leave it there
1474 * if we're turning off quotas. Basically, we need this flush
1475 * lock, and are willing to block on it.
1477 if (! xfs_qm_dqflock_nowait(dqp
)) {
1479 * Block on the flush lock after nudging dquot buffer,
1482 xfs_qm_dqflock_pushbuf_wait(dqp
);
1486 * XXXIf we're turning this type of quotas off, we don't care
1487 * about the dirty metadata sitting in this dquot. OTOH, if
1488 * we're unmounting, we do care, so we flush it and wait.
1490 if (XFS_DQ_IS_DIRTY(dqp
)) {
1491 xfs_dqtrace_entry(dqp
, "DQPURGE ->DQFLUSH: DQDIRTY");
1492 /* dqflush unlocks dqflock */
1494 * Given that dqpurge is a very rare occurrence, it is OK
1495 * that we're holding the hashlist and mplist locks
1496 * across the disk write. But, ... XXXsup
1498 * We don't care about getting disk errors here. We need
1499 * to purge this dquot anyway, so we go ahead regardless.
1501 (void) xfs_qm_dqflush(dqp
, XFS_QMOPT_SYNC
);
1504 ASSERT(dqp
->q_pincount
== 0);
1505 ASSERT(XFS_FORCED_SHUTDOWN(mp
) ||
1506 !(dqp
->q_logitem
.qli_item
.li_flags
& XFS_LI_IN_AIL
));
1508 thishash
= dqp
->q_hash
;
1509 XQM_HASHLIST_REMOVE(thishash
, dqp
);
1510 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp
)), dqp
);
1512 * XXX Move this to the front of the freelist, if we can get the
1515 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp
));
1517 dqp
->q_mount
= NULL
;
1519 dqp
->dq_flags
= XFS_DQ_INACTIVE
;
1520 memset(&dqp
->q_core
, 0, sizeof(dqp
->q_core
));
1523 XFS_DQ_HASH_UNLOCK(thishash
);
1530 xfs_qm_dqprint(xfs_dquot_t
*dqp
)
1532 cmn_err(CE_DEBUG
, "-----------KERNEL DQUOT----------------");
1533 cmn_err(CE_DEBUG
, "---- dquotID = %d",
1534 (int)be32_to_cpu(dqp
->q_core
.d_id
));
1535 cmn_err(CE_DEBUG
, "---- type = %s", DQFLAGTO_TYPESTR(dqp
));
1536 cmn_err(CE_DEBUG
, "---- fs = 0x%p", dqp
->q_mount
);
1537 cmn_err(CE_DEBUG
, "---- blkno = 0x%x", (int) dqp
->q_blkno
);
1538 cmn_err(CE_DEBUG
, "---- boffset = 0x%x", (int) dqp
->q_bufoffset
);
1539 cmn_err(CE_DEBUG
, "---- blkhlimit = %Lu (0x%x)",
1540 be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
),
1541 (int)be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
));
1542 cmn_err(CE_DEBUG
, "---- blkslimit = %Lu (0x%x)",
1543 be64_to_cpu(dqp
->q_core
.d_blk_softlimit
),
1544 (int)be64_to_cpu(dqp
->q_core
.d_blk_softlimit
));
1545 cmn_err(CE_DEBUG
, "---- inohlimit = %Lu (0x%x)",
1546 be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
),
1547 (int)be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
));
1548 cmn_err(CE_DEBUG
, "---- inoslimit = %Lu (0x%x)",
1549 be64_to_cpu(dqp
->q_core
.d_ino_softlimit
),
1550 (int)be64_to_cpu(dqp
->q_core
.d_ino_softlimit
));
1551 cmn_err(CE_DEBUG
, "---- bcount = %Lu (0x%x)",
1552 be64_to_cpu(dqp
->q_core
.d_bcount
),
1553 (int)be64_to_cpu(dqp
->q_core
.d_bcount
));
1554 cmn_err(CE_DEBUG
, "---- icount = %Lu (0x%x)",
1555 be64_to_cpu(dqp
->q_core
.d_icount
),
1556 (int)be64_to_cpu(dqp
->q_core
.d_icount
));
1557 cmn_err(CE_DEBUG
, "---- btimer = %d",
1558 (int)be32_to_cpu(dqp
->q_core
.d_btimer
));
1559 cmn_err(CE_DEBUG
, "---- itimer = %d",
1560 (int)be32_to_cpu(dqp
->q_core
.d_itimer
));
1561 cmn_err(CE_DEBUG
, "---------------------------");
1566 * Give the buffer a little push if it is incore and
1567 * wait on the flush lock.
1570 xfs_qm_dqflock_pushbuf_wait(
1576 * Check to see if the dquot has been flushed delayed
1577 * write. If so, grab its buffer and send it
1578 * out immediately. We'll be able to acquire
1579 * the flush lock when the I/O completes.
1581 bp
= xfs_incore(dqp
->q_mount
->m_ddev_targp
, dqp
->q_blkno
,
1582 XFS_QI_DQCHUNKLEN(dqp
->q_mount
),
1583 XFS_INCORE_TRYLOCK
);
1585 if (XFS_BUF_ISDELAYWRITE(bp
)) {
1586 if (XFS_BUF_ISPINNED(bp
)) {
1587 xfs_log_force(dqp
->q_mount
,
1591 xfs_bawrite(dqp
->q_mount
, bp
);