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"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_btree.h"
41 #include "xfs_ialloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_trans_priv.h"
61 dquot hash-chain lock (hashlock)
62 xqm dquot freelist lock (freelistlock
63 mount's dquot list lock (mplistlock)
64 user dquot lock - lock ordering among dquots is based on the uid or gid
65 group dquot lock - similar to udquots. Between the two dquots, the udquot
66 has to be locked first.
67 pin lock - the dquot lock must be held to take this lock.
71 STATIC
void xfs_qm_dqflush_done(xfs_buf_t
*, xfs_dq_logitem_t
*);
74 xfs_buftarg_t
*xfs_dqerror_target
;
77 int xfs_dqerror_mod
= 33;
81 * Allocate and initialize a dquot. We don't always allocate fresh memory;
82 * we try to reclaim a free dquot if the number of incore dquots are above
84 * The only field inside the core that gets initialized at this point
85 * is the d_id field. The idea is to fill in the entire q_core
86 * when we read in the on disk dquot.
95 boolean_t brandnewdquot
;
97 brandnewdquot
= xfs_qm_dqalloc_incore(&dqp
);
99 dqp
->q_core
.d_id
= cpu_to_be32(id
);
103 * No need to re-initialize these if this is a reclaimed dquot.
106 dqp
->dq_flnext
= dqp
->dq_flprev
= dqp
;
107 mutex_init(&dqp
->q_qlock
, MUTEX_DEFAULT
, "xdq");
108 initnsema(&dqp
->q_flock
, 1, "fdq");
109 sv_init(&dqp
->q_pinwait
, SV_DEFAULT
, "pdq");
111 #ifdef XFS_DQUOT_TRACE
112 dqp
->q_trace
= ktrace_alloc(DQUOT_TRACE_SIZE
, KM_SLEEP
);
113 xfs_dqtrace_entry(dqp
, "DQINIT");
117 * Only the q_core portion was zeroed in dqreclaim_one().
118 * So, we need to reset others.
122 dqp
->MPL_NEXT
= dqp
->HL_NEXT
= NULL
;
123 dqp
->HL_PREVP
= dqp
->MPL_PREVP
= NULL
;
124 dqp
->q_bufoffset
= 0;
125 dqp
->q_fileoffset
= 0;
126 dqp
->q_transp
= NULL
;
127 dqp
->q_gdquot
= NULL
;
128 dqp
->q_res_bcount
= 0;
129 dqp
->q_res_icount
= 0;
130 dqp
->q_res_rtbcount
= 0;
133 ASSERT(dqp
->dq_flnext
== dqp
->dq_flprev
);
135 #ifdef XFS_DQUOT_TRACE
136 ASSERT(dqp
->q_trace
);
137 xfs_dqtrace_entry(dqp
, "DQRECLAIMED_INIT");
142 * log item gets initialized later
148 * This is called to free all the memory associated with a dquot
154 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp
));
156 mutex_destroy(&dqp
->q_qlock
);
157 freesema(&dqp
->q_flock
);
158 sv_destroy(&dqp
->q_pinwait
);
160 #ifdef XFS_DQUOT_TRACE
162 ktrace_free(dqp
->q_trace
);
165 kmem_zone_free(xfs_Gqm
->qm_dqzone
, dqp
);
166 atomic_dec(&xfs_Gqm
->qm_totaldquots
);
170 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
179 * Caller has zero'd the entire dquot 'chunk' already.
181 d
->dd_diskdq
.d_magic
= cpu_to_be16(XFS_DQUOT_MAGIC
);
182 d
->dd_diskdq
.d_version
= XFS_DQUOT_VERSION
;
183 d
->dd_diskdq
.d_id
= cpu_to_be32(id
);
184 d
->dd_diskdq
.d_flags
= type
;
188 #ifdef XFS_DQUOT_TRACE
190 * Dquot tracing for debugging.
200 xfs_dquot_t
*udqp
= NULL
;
203 ASSERT(dqp
->q_trace
);
208 ktrace_enter(dqp
->q_trace
,
209 (void *)(__psint_t
)DQUOT_KTRACE_ENTRY
,
211 (void *)(__psint_t
)dqp
->q_nrefs
,
212 (void *)(__psint_t
)dqp
->dq_flags
,
213 (void *)(__psint_t
)dqp
->q_res_bcount
,
214 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_bcount
),
215 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_icount
),
216 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
),
217 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_blk_softlimit
),
218 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
),
219 (void *)(__psint_t
)be64_to_cpu(dqp
->q_core
.d_ino_softlimit
),
220 (void *)(__psint_t
)be32_to_cpu(dqp
->q_core
.d_id
),
221 (void *)(__psint_t
)current_pid(),
222 (void *)(__psint_t
)ino
,
223 (void *)(__psint_t
)retaddr
,
224 (void *)(__psint_t
)udqp
);
231 * If default limits are in force, push them into the dquot now.
232 * We overwrite the dquot limits only if they are zero and this
233 * is not the root dquot.
236 xfs_qm_adjust_dqlimits(
240 xfs_quotainfo_t
*q
= mp
->m_quotainfo
;
244 if (q
->qi_bsoftlimit
&& !d
->d_blk_softlimit
)
245 d
->d_blk_softlimit
= cpu_to_be64(q
->qi_bsoftlimit
);
246 if (q
->qi_bhardlimit
&& !d
->d_blk_hardlimit
)
247 d
->d_blk_hardlimit
= cpu_to_be64(q
->qi_bhardlimit
);
248 if (q
->qi_isoftlimit
&& !d
->d_ino_softlimit
)
249 d
->d_ino_softlimit
= cpu_to_be64(q
->qi_isoftlimit
);
250 if (q
->qi_ihardlimit
&& !d
->d_ino_hardlimit
)
251 d
->d_ino_hardlimit
= cpu_to_be64(q
->qi_ihardlimit
);
252 if (q
->qi_rtbsoftlimit
&& !d
->d_rtb_softlimit
)
253 d
->d_rtb_softlimit
= cpu_to_be64(q
->qi_rtbsoftlimit
);
254 if (q
->qi_rtbhardlimit
&& !d
->d_rtb_hardlimit
)
255 d
->d_rtb_hardlimit
= cpu_to_be64(q
->qi_rtbhardlimit
);
259 * Check the limits and timers of a dquot and start or reset timers
261 * This gets called even when quota enforcement is OFF, which makes our
262 * life a little less complicated. (We just don't reject any quota
263 * reservations in that case, when enforcement is off).
264 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
266 * In contrast, warnings are a little different in that they don't
267 * 'automatically' get started when limits get exceeded. They do
268 * get reset to zero, however, when we find the count to be under
269 * the soft limit (they are only ever set non-zero via userspace).
272 xfs_qm_adjust_dqtimers(
279 if (d
->d_blk_hardlimit
)
280 ASSERT(be64_to_cpu(d
->d_blk_softlimit
) <=
281 be64_to_cpu(d
->d_blk_hardlimit
));
282 if (d
->d_ino_hardlimit
)
283 ASSERT(be64_to_cpu(d
->d_ino_softlimit
) <=
284 be64_to_cpu(d
->d_ino_hardlimit
));
285 if (d
->d_rtb_hardlimit
)
286 ASSERT(be64_to_cpu(d
->d_rtb_softlimit
) <=
287 be64_to_cpu(d
->d_rtb_hardlimit
));
290 if ((d
->d_blk_softlimit
&&
291 (be64_to_cpu(d
->d_bcount
) >=
292 be64_to_cpu(d
->d_blk_softlimit
))) ||
293 (d
->d_blk_hardlimit
&&
294 (be64_to_cpu(d
->d_bcount
) >=
295 be64_to_cpu(d
->d_blk_hardlimit
)))) {
296 d
->d_btimer
= cpu_to_be32(get_seconds() +
297 XFS_QI_BTIMELIMIT(mp
));
302 if ((!d
->d_blk_softlimit
||
303 (be64_to_cpu(d
->d_bcount
) <
304 be64_to_cpu(d
->d_blk_softlimit
))) &&
305 (!d
->d_blk_hardlimit
||
306 (be64_to_cpu(d
->d_bcount
) <
307 be64_to_cpu(d
->d_blk_hardlimit
)))) {
313 if ((d
->d_ino_softlimit
&&
314 (be64_to_cpu(d
->d_icount
) >=
315 be64_to_cpu(d
->d_ino_softlimit
))) ||
316 (d
->d_ino_hardlimit
&&
317 (be64_to_cpu(d
->d_icount
) >=
318 be64_to_cpu(d
->d_ino_hardlimit
)))) {
319 d
->d_itimer
= cpu_to_be32(get_seconds() +
320 XFS_QI_ITIMELIMIT(mp
));
325 if ((!d
->d_ino_softlimit
||
326 (be64_to_cpu(d
->d_icount
) <
327 be64_to_cpu(d
->d_ino_softlimit
))) &&
328 (!d
->d_ino_hardlimit
||
329 (be64_to_cpu(d
->d_icount
) <
330 be64_to_cpu(d
->d_ino_hardlimit
)))) {
335 if (!d
->d_rtbtimer
) {
336 if ((d
->d_rtb_softlimit
&&
337 (be64_to_cpu(d
->d_rtbcount
) >=
338 be64_to_cpu(d
->d_rtb_softlimit
))) ||
339 (d
->d_rtb_hardlimit
&&
340 (be64_to_cpu(d
->d_rtbcount
) >=
341 be64_to_cpu(d
->d_rtb_hardlimit
)))) {
342 d
->d_rtbtimer
= cpu_to_be32(get_seconds() +
343 XFS_QI_RTBTIMELIMIT(mp
));
348 if ((!d
->d_rtb_softlimit
||
349 (be64_to_cpu(d
->d_rtbcount
) <
350 be64_to_cpu(d
->d_rtb_softlimit
))) &&
351 (!d
->d_rtb_hardlimit
||
352 (be64_to_cpu(d
->d_rtbcount
) <
353 be64_to_cpu(d
->d_rtb_hardlimit
)))) {
360 * initialize a buffer full of dquots and log the whole thing
363 xfs_qm_init_dquot_blk(
374 ASSERT(XFS_BUF_ISBUSY(bp
));
375 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
377 d
= (xfs_dqblk_t
*)XFS_BUF_PTR(bp
);
380 * ID of the first dquot in the block - id's are zero based.
382 curid
= id
- (id
% XFS_QM_DQPERBLK(mp
));
384 memset(d
, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp
)));
385 for (i
= 0; i
< XFS_QM_DQPERBLK(mp
); i
++, d
++, curid
++)
386 xfs_qm_dqinit_core(curid
, type
, d
);
387 xfs_trans_dquot_buf(tp
, bp
,
388 (type
& XFS_DQ_USER
? XFS_BLI_UDQUOT_BUF
:
389 ((type
& XFS_DQ_PROJ
) ? XFS_BLI_PDQUOT_BUF
:
390 XFS_BLI_GDQUOT_BUF
)));
391 xfs_trans_log_buf(tp
, bp
, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp
)) - 1);
397 * Allocate a block and fill it with dquots.
398 * This is called when the bmapi finds a hole.
406 xfs_fileoff_t offset_fsb
,
409 xfs_fsblock_t firstblock
;
410 xfs_bmap_free_t flist
;
412 int nmaps
, error
, committed
;
414 xfs_trans_t
*tp
= *tpp
;
417 xfs_dqtrace_entry(dqp
, "DQALLOC");
420 * Initialize the bmap freelist prior to calling bmapi code.
422 XFS_BMAP_INIT(&flist
, &firstblock
);
423 xfs_ilock(quotip
, XFS_ILOCK_EXCL
);
425 * Return if this type of quotas is turned off while we didn't
428 if (XFS_IS_THIS_QUOTA_OFF(dqp
)) {
429 xfs_iunlock(quotip
, XFS_ILOCK_EXCL
);
434 * xfs_trans_commit normally decrements the vnode ref count
435 * when it unlocks the inode. Since we want to keep the quota
436 * inode around, we bump the vnode ref count now.
438 VN_HOLD(XFS_ITOV(quotip
));
440 xfs_trans_ijoin(tp
, quotip
, XFS_ILOCK_EXCL
);
442 if ((error
= xfs_bmapi(tp
, quotip
,
443 offset_fsb
, XFS_DQUOT_CLUSTER_SIZE_FSB
,
444 XFS_BMAPI_METADATA
| XFS_BMAPI_WRITE
,
446 XFS_QM_DQALLOC_SPACE_RES(mp
),
447 &map
, &nmaps
, &flist
))) {
450 ASSERT(map
.br_blockcount
== XFS_DQUOT_CLUSTER_SIZE_FSB
);
452 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
453 (map
.br_startblock
!= HOLESTARTBLOCK
));
456 * Keep track of the blkno to save a lookup later
458 dqp
->q_blkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
460 /* now we can just get the buffer (there's nothing to read yet) */
461 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
463 XFS_QI_DQCHUNKLEN(mp
),
465 if (!bp
|| (error
= XFS_BUF_GETERROR(bp
)))
468 * Make a chunk of dquots out of this buffer and log
471 xfs_qm_init_dquot_blk(tp
, mp
, be32_to_cpu(dqp
->q_core
.d_id
),
472 dqp
->dq_flags
& XFS_DQ_ALLTYPES
, bp
);
475 * xfs_bmap_finish() may commit the current transaction and
476 * start a second transaction if the freelist is not empty.
478 * Since we still want to modify this buffer, we need to
479 * ensure that the buffer is not released on commit of
480 * the first transaction and ensure the buffer is added to the
481 * second transaction.
483 * If there is only one transaction then don't stop the buffer
484 * from being released when it commits later on.
487 xfs_trans_bhold(tp
, bp
);
489 if ((error
= xfs_bmap_finish(tpp
, &flist
, firstblock
, &committed
))) {
495 xfs_trans_bjoin(tp
, bp
);
497 xfs_trans_bhold_release(tp
, bp
);
504 xfs_bmap_cancel(&flist
);
506 xfs_iunlock(quotip
, XFS_ILOCK_EXCL
);
512 * Maps a dquot to the buffer containing its on-disk version.
513 * This returns a ptr to the buffer containing the on-disk dquot
514 * in the bpp param, and a ptr to the on-disk dquot within that buffer
520 xfs_disk_dquot_t
**O_ddpp
,
529 xfs_disk_dquot_t
*ddq
;
532 xfs_trans_t
*tp
= (tpp
? *tpp
: NULL
);
535 id
= be32_to_cpu(dqp
->q_core
.d_id
);
540 * If we don't know where the dquot lives, find out.
542 if (dqp
->q_blkno
== (xfs_daddr_t
) 0) {
543 /* We use the id as an index */
544 dqp
->q_fileoffset
= (xfs_fileoff_t
)id
/ XFS_QM_DQPERBLK(mp
);
546 quotip
= XFS_DQ_TO_QIP(dqp
);
547 xfs_ilock(quotip
, XFS_ILOCK_SHARED
);
549 * Return if this type of quotas is turned off while we didn't
552 if (XFS_IS_THIS_QUOTA_OFF(dqp
)) {
553 xfs_iunlock(quotip
, XFS_ILOCK_SHARED
);
557 * Find the block map; no allocations yet
559 error
= xfs_bmapi(NULL
, quotip
, dqp
->q_fileoffset
,
560 XFS_DQUOT_CLUSTER_SIZE_FSB
,
562 NULL
, 0, &map
, &nmaps
, NULL
);
564 xfs_iunlock(quotip
, XFS_ILOCK_SHARED
);
568 ASSERT(map
.br_blockcount
== 1);
571 * offset of dquot in the (fixed sized) dquot chunk.
573 dqp
->q_bufoffset
= (id
% XFS_QM_DQPERBLK(mp
)) *
575 if (map
.br_startblock
== HOLESTARTBLOCK
) {
577 * We don't allocate unless we're asked to
579 if (!(flags
& XFS_QMOPT_DQALLOC
))
583 if ((error
= xfs_qm_dqalloc(tpp
, mp
, dqp
, quotip
,
584 dqp
->q_fileoffset
, &bp
)))
590 * store the blkno etc so that we don't have to do the
591 * mapping all the time
593 dqp
->q_blkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
);
596 ASSERT(dqp
->q_blkno
!= DELAYSTARTBLOCK
);
597 ASSERT(dqp
->q_blkno
!= HOLESTARTBLOCK
);
600 * Read in the buffer, unless we've just done the allocation
601 * (in which case we already have the buf).
604 xfs_dqtrace_entry(dqp
, "DQTOBP READBUF");
605 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
607 XFS_QI_DQCHUNKLEN(mp
),
612 return XFS_ERROR(error
);
614 ASSERT(XFS_BUF_ISBUSY(bp
));
615 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
618 * calculate the location of the dquot inside the buffer.
620 ddq
= (xfs_disk_dquot_t
*)((char *)XFS_BUF_PTR(bp
) + dqp
->q_bufoffset
);
623 * A simple sanity check in case we got a corrupted dquot...
625 if (xfs_qm_dqcheck(ddq
, id
, dqp
->dq_flags
& XFS_DQ_ALLTYPES
,
626 flags
& (XFS_QMOPT_DQREPAIR
|XFS_QMOPT_DOWARN
),
628 if (!(flags
& XFS_QMOPT_DQREPAIR
)) {
629 xfs_trans_brelse(tp
, bp
);
630 return XFS_ERROR(EIO
);
632 XFS_BUF_BUSY(bp
); /* We dirtied this */
643 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
644 * and release the buffer immediately.
652 xfs_dquot_t
*dqp
, /* dquot to get filled in */
655 xfs_disk_dquot_t
*ddqp
;
663 * get a pointer to the on-disk dquot and the buffer containing it
664 * dqp already knows its own type (GROUP/USER).
666 xfs_dqtrace_entry(dqp
, "DQREAD");
667 if ((error
= xfs_qm_dqtobp(tpp
, dqp
, &ddqp
, &bp
, flags
))) {
672 /* copy everything from disk dquot to the incore dquot */
673 memcpy(&dqp
->q_core
, ddqp
, sizeof(xfs_disk_dquot_t
));
674 ASSERT(be32_to_cpu(dqp
->q_core
.d_id
) == id
);
675 xfs_qm_dquot_logitem_init(dqp
);
678 * Reservation counters are defined as reservation plus current usage
679 * to avoid having to add everytime.
681 dqp
->q_res_bcount
= be64_to_cpu(ddqp
->d_bcount
);
682 dqp
->q_res_icount
= be64_to_cpu(ddqp
->d_icount
);
683 dqp
->q_res_rtbcount
= be64_to_cpu(ddqp
->d_rtbcount
);
685 /* Mark the buf so that this will stay incore a little longer */
686 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_DQUOT
, XFS_DQUOT_REF
);
689 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
690 * So we need to release with xfs_trans_brelse().
691 * The strategy here is identical to that of inodes; we lock
692 * the dquot in xfs_qm_dqget() before making it accessible to
693 * others. This is because dquots, like inodes, need a good level of
694 * concurrency, and we don't want to take locks on the entire buffers
695 * for dquot accesses.
696 * Note also that the dquot buffer may even be dirty at this point, if
697 * this particular dquot was repaired. We still aren't afraid to
698 * brelse it because we have the changes incore.
700 ASSERT(XFS_BUF_ISBUSY(bp
));
701 ASSERT(XFS_BUF_VALUSEMA(bp
) <= 0);
702 xfs_trans_brelse(tp
, bp
);
709 * allocate an incore dquot from the kernel heap,
710 * and fill its core with quota information kept on disk.
711 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
712 * if it wasn't already allocated.
717 xfs_dqid_t id
, /* gid or uid, depending on type */
718 uint type
, /* UDQUOT or GDQUOT */
719 uint flags
, /* DQALLOC, DQREPAIR */
720 xfs_dquot_t
**O_dqpp
)/* OUT : incore dquot, not locked */
727 dqp
= xfs_qm_dqinit(mp
, id
, type
);
729 if (flags
& XFS_QMOPT_DQALLOC
) {
730 tp
= xfs_trans_alloc(mp
, XFS_TRANS_QM_DQALLOC
);
731 if ((error
= xfs_trans_reserve(tp
,
732 XFS_QM_DQALLOC_SPACE_RES(mp
),
733 XFS_WRITE_LOG_RES(mp
) +
734 BBTOB(XFS_QI_DQCHUNKLEN(mp
)) - 1 +
737 XFS_TRANS_PERM_LOG_RES
,
738 XFS_WRITE_LOG_COUNT
))) {
742 cancelflags
= XFS_TRANS_RELEASE_LOG_RES
;
746 * Read it from disk; xfs_dqread() takes care of
747 * all the necessary initialization of dquot's fields (locks, etc)
749 if ((error
= xfs_qm_dqread(&tp
, id
, dqp
, flags
))) {
751 * This can happen if quotas got turned off (ESRCH),
752 * or if the dquot didn't exist on disk and we ask to
755 xfs_dqtrace_entry(dqp
, "DQREAD FAIL");
756 cancelflags
|= XFS_TRANS_ABORT
;
760 if ((error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
,
771 xfs_trans_cancel(tp
, cancelflags
);
773 xfs_qm_dqdestroy(dqp
);
779 * Lookup a dquot in the incore dquot hashtable. We keep two separate
780 * hashtables for user and group dquots; and, these are global tables
781 * inside the XQM, not per-filesystem tables.
782 * The hash chain must be locked by caller, and it is left locked
783 * on return. Returning dquot is locked.
790 xfs_dquot_t
**O_dqpp
)
796 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
798 flist_locked
= B_FALSE
;
801 * Traverse the hashchain looking for a match
803 for (dqp
= qh
->qh_next
; dqp
!= NULL
; dqp
= dqp
->HL_NEXT
) {
805 * We already have the hashlock. We don't need the
806 * dqlock to look at the id field of the dquot, since the
807 * id can't be modified without the hashlock anyway.
809 if (be32_to_cpu(dqp
->q_core
.d_id
) == id
&& dqp
->q_mount
== mp
) {
810 xfs_dqtrace_entry(dqp
, "DQFOUND BY LOOKUP");
812 * All in core dquots must be on the dqlist of mp
814 ASSERT(dqp
->MPL_PREVP
!= NULL
);
817 if (dqp
->q_nrefs
== 0) {
818 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp
));
819 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm
)) {
820 xfs_dqtrace_entry(dqp
, "DQLOOKUP: WANT");
823 * We may have raced with dqreclaim_one()
824 * (and lost). So, flag that we don't
825 * want the dquot to be reclaimed.
827 dqp
->dq_flags
|= XFS_DQ_WANT
;
829 xfs_qm_freelist_lock(xfs_Gqm
);
831 dqp
->dq_flags
&= ~(XFS_DQ_WANT
);
833 flist_locked
= B_TRUE
;
837 * id couldn't have changed; we had the hashlock all
840 ASSERT(be32_to_cpu(dqp
->q_core
.d_id
) == id
);
843 if (dqp
->q_nrefs
!= 0) {
844 xfs_qm_freelist_unlock(xfs_Gqm
);
845 flist_locked
= B_FALSE
;
848 * take it off the freelist
850 xfs_dqtrace_entry(dqp
,
851 "DQLOOKUP: TAKEOFF FL");
852 XQM_FREELIST_REMOVE(dqp
);
853 /* xfs_qm_freelist_print(&(xfs_Gqm->
865 xfs_qm_freelist_unlock(xfs_Gqm
);
867 * move the dquot to the front of the hashchain
869 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
870 if (dqp
->HL_PREVP
!= &qh
->qh_next
) {
871 xfs_dqtrace_entry(dqp
,
872 "DQLOOKUP: HASH MOVETOFRONT");
873 if ((d
= dqp
->HL_NEXT
))
874 d
->HL_PREVP
= dqp
->HL_PREVP
;
875 *(dqp
->HL_PREVP
) = d
;
877 d
->HL_PREVP
= &dqp
->HL_NEXT
;
879 dqp
->HL_PREVP
= &qh
->qh_next
;
882 xfs_dqtrace_entry(dqp
, "LOOKUP END");
884 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
890 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh
));
895 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
896 * a locked dquot, doing an allocation (if requested) as needed.
897 * When both an inode and an id are given, the inode's id takes precedence.
898 * That is, if the id changes while we don't hold the ilock inside this
899 * function, the new dquot is returned, not necessarily the one requested
900 * in the id argument.
905 xfs_inode_t
*ip
, /* locked inode (optional) */
906 xfs_dqid_t id
, /* uid/projid/gid depending on type */
907 uint type
, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
908 uint flags
, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
909 xfs_dquot_t
**O_dqpp
) /* OUT : locked incore dquot */
916 ASSERT(XFS_IS_QUOTA_RUNNING(mp
));
917 if ((! XFS_IS_UQUOTA_ON(mp
) && type
== XFS_DQ_USER
) ||
918 (! XFS_IS_PQUOTA_ON(mp
) && type
== XFS_DQ_PROJ
) ||
919 (! XFS_IS_GQUOTA_ON(mp
) && type
== XFS_DQ_GROUP
)) {
922 h
= XFS_DQ_HASH(mp
, id
, type
);
925 if (xfs_do_dqerror
) {
926 if ((xfs_dqerror_target
== mp
->m_ddev_targp
) &&
927 (xfs_dqreq_num
++ % xfs_dqerror_mod
) == 0) {
928 cmn_err(CE_DEBUG
, "Returning error in dqget");
937 ASSERT(type
== XFS_DQ_USER
||
938 type
== XFS_DQ_PROJ
||
939 type
== XFS_DQ_GROUP
);
941 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip
));
942 if (type
== XFS_DQ_USER
)
943 ASSERT(ip
->i_udquot
== NULL
);
945 ASSERT(ip
->i_gdquot
== NULL
);
951 * Look in the cache (hashtable).
952 * The chain is kept locked during lookup.
954 if (xfs_qm_dqlookup(mp
, id
, h
, O_dqpp
) == 0) {
955 XQM_STATS_INC(xqmstats
.xs_qm_dqcachehits
);
957 * The dquot was found, moved to the front of the chain,
958 * taken off the freelist if it was on it, and locked
959 * at this point. Just unlock the hashchain and return.
962 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp
));
963 XFS_DQ_HASH_UNLOCK(h
);
964 xfs_dqtrace_entry(*O_dqpp
, "DQGET DONE (FROM CACHE)");
965 return (0); /* success */
967 XQM_STATS_INC(xqmstats
.xs_qm_dqcachemisses
);
970 * Dquot cache miss. We don't want to keep the inode lock across
971 * a (potential) disk read. Also we don't want to deal with the lock
972 * ordering between quotainode and this inode. OTOH, dropping the inode
973 * lock here means dealing with a chown that can happen before
974 * we re-acquire the lock.
977 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
979 * Save the hashchain version stamp, and unlock the chain, so that
980 * we don't keep the lock across a disk read
982 version
= h
->qh_version
;
983 XFS_DQ_HASH_UNLOCK(h
);
986 * Allocate the dquot on the kernel heap, and read the ondisk
987 * portion off the disk. Also, do all the necessary initialization
988 * This can return ENOENT if dquot didn't exist on disk and we didn't
989 * ask it to allocate; ESRCH if quotas got turned off suddenly.
991 if ((error
= xfs_qm_idtodq(mp
, id
, type
,
992 flags
& (XFS_QMOPT_DQALLOC
|XFS_QMOPT_DQREPAIR
|
996 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1001 * See if this is mount code calling to look at the overall quota limits
1002 * which are stored in the id == 0 user or group's dquot.
1003 * Since we may not have done a quotacheck by this point, just return
1004 * the dquot without attaching it to any hashtables, lists, etc, or even
1005 * taking a reference.
1006 * The caller must dqdestroy this once done.
1008 if (flags
& XFS_QMOPT_DQSUSER
) {
1015 * Dquot lock comes after hashlock in the lock ordering
1018 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1019 if (! XFS_IS_DQTYPE_ON(mp
, type
)) {
1020 /* inode stays locked on return */
1021 xfs_qm_dqdestroy(dqp
);
1022 return XFS_ERROR(ESRCH
);
1025 * A dquot could be attached to this inode by now, since
1026 * we had dropped the ilock.
1028 if (type
== XFS_DQ_USER
) {
1030 xfs_qm_dqdestroy(dqp
);
1037 xfs_qm_dqdestroy(dqp
);
1046 * Hashlock comes after ilock in lock order
1048 XFS_DQ_HASH_LOCK(h
);
1049 if (version
!= h
->qh_version
) {
1050 xfs_dquot_t
*tmpdqp
;
1052 * Now, see if somebody else put the dquot in the
1053 * hashtable before us. This can happen because we didn't
1054 * keep the hashchain lock. We don't have to worry about
1055 * lock order between the two dquots here since dqp isn't
1056 * on any findable lists yet.
1058 if (xfs_qm_dqlookup(mp
, id
, h
, &tmpdqp
) == 0) {
1060 * Duplicate found. Just throw away the new dquot
1063 xfs_qm_dqput(tmpdqp
);
1064 XFS_DQ_HASH_UNLOCK(h
);
1065 xfs_qm_dqdestroy(dqp
);
1066 XQM_STATS_INC(xqmstats
.xs_qm_dquot_dups
);
1072 * Put the dquot at the beginning of the hash-chain and mp's list
1073 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1075 ASSERT(XFS_DQ_IS_HASH_LOCKED(h
));
1077 XQM_HASHLIST_INSERT(h
, dqp
);
1080 * Attach this dquot to this filesystem's list of all dquots,
1081 * kept inside the mount structure in m_quotainfo field
1083 xfs_qm_mplist_lock(mp
);
1086 * We return a locked dquot to the caller, with a reference taken
1091 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp
)), dqp
);
1093 xfs_qm_mplist_unlock(mp
);
1094 XFS_DQ_HASH_UNLOCK(h
);
1096 ASSERT((ip
== NULL
) || XFS_ISLOCKED_INODE_EXCL(ip
));
1097 xfs_dqtrace_entry(dqp
, "DQGET DONE");
1104 * Release a reference to the dquot (decrement ref-count)
1105 * and unlock it. If there is a group quota attached to this
1106 * dquot, carefully release that too without tripping over
1107 * deadlocks'n'stuff.
1115 ASSERT(dqp
->q_nrefs
> 0);
1116 ASSERT(XFS_DQ_IS_LOCKED(dqp
));
1117 xfs_dqtrace_entry(dqp
, "DQPUT");
1119 if (dqp
->q_nrefs
!= 1) {
1126 * drop the dqlock and acquire the freelist and dqlock
1127 * in the right order; but try to get it out-of-order first
1129 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm
)) {
1130 xfs_dqtrace_entry(dqp
, "DQPUT: FLLOCK-WAIT");
1132 xfs_qm_freelist_lock(xfs_Gqm
);
1139 /* We can't depend on nrefs being == 1 here */
1140 if (--dqp
->q_nrefs
== 0) {
1141 xfs_dqtrace_entry(dqp
, "DQPUT: ON FREELIST");
1143 * insert at end of the freelist.
1145 XQM_FREELIST_INSERT(&(xfs_Gqm
->qm_dqfreelist
), dqp
);
1148 * If we just added a udquot to the freelist, then
1149 * we want to release the gdquot reference that
1150 * it (probably) has. Otherwise it'll keep the
1151 * gdquot from getting reclaimed.
1153 if ((gdqp
= dqp
->q_gdquot
)) {
1155 * Avoid a recursive dqput call
1158 dqp
->q_gdquot
= NULL
;
1161 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1162 "@@@@@++ Free list (after append) @@@@@+");
1168 * If we had a group quota inside the user quota as a hint,
1175 xfs_qm_freelist_unlock(xfs_Gqm
);
1179 * Release a dquot. Flush it if dirty, then dqput() it.
1180 * dquot must not be locked.
1187 xfs_dqtrace_entry(dqp
, "DQRELE");
1191 * We don't care to flush it if the dquot is dirty here.
1192 * That will create stutters that we want to avoid.
1193 * Instead we do a delayed write when we try to reclaim
1194 * a dirty dquot. Also xfs_sync will take part of the burden...
1201 * Write a modified dquot to disk.
1202 * The dquot must be locked and the flush lock too taken by caller.
1203 * The flush lock will not be unlocked until the dquot reaches the disk,
1204 * but the dquot is free to be unlocked and modified by the caller
1205 * in the interim. Dquot is still locked on return. This behavior is
1206 * identical to that of inodes.
1215 xfs_disk_dquot_t
*ddqp
;
1219 ASSERT(XFS_DQ_IS_LOCKED(dqp
));
1220 ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp
));
1221 xfs_dqtrace_entry(dqp
, "DQFLUSH");
1224 * If not dirty, nada.
1226 if (!XFS_DQ_IS_DIRTY(dqp
)) {
1232 * Cant flush a pinned dquot. Wait for it.
1234 xfs_qm_dqunpin_wait(dqp
);
1237 * This may have been unpinned because the filesystem is shutting
1238 * down forcibly. If that's the case we must not write this dquot
1239 * to disk, because the log record didn't make it to disk!
1241 if (XFS_FORCED_SHUTDOWN(dqp
->q_mount
)) {
1242 dqp
->dq_flags
&= ~(XFS_DQ_DIRTY
);
1244 return XFS_ERROR(EIO
);
1248 * Get the buffer containing the on-disk dquot
1249 * We don't need a transaction envelope because we know that the
1250 * the ondisk-dquot has already been allocated for.
1252 if ((error
= xfs_qm_dqtobp(NULL
, dqp
, &ddqp
, &bp
, XFS_QMOPT_DOWARN
))) {
1253 xfs_dqtrace_entry(dqp
, "DQTOBP FAIL");
1254 ASSERT(error
!= ENOENT
);
1256 * Quotas could have gotten turned off (ESRCH)
1262 if (xfs_qm_dqcheck(&dqp
->q_core
, be32_to_cpu(ddqp
->d_id
),
1263 0, XFS_QMOPT_DOWARN
, "dqflush (incore copy)")) {
1264 xfs_force_shutdown(dqp
->q_mount
, XFS_CORRUPT_INCORE
);
1265 return XFS_ERROR(EIO
);
1268 /* This is the only portion of data that needs to persist */
1269 memcpy(ddqp
, &(dqp
->q_core
), sizeof(xfs_disk_dquot_t
));
1272 * Clear the dirty field and remember the flush lsn for later use.
1274 dqp
->dq_flags
&= ~(XFS_DQ_DIRTY
);
1277 /* lsn is 64 bits */
1279 dqp
->q_logitem
.qli_flush_lsn
= dqp
->q_logitem
.qli_item
.li_lsn
;
1283 * Attach an iodone routine so that we can remove this dquot from the
1284 * AIL and release the flush lock once the dquot is synced to disk.
1286 xfs_buf_attach_iodone(bp
, (void(*)(xfs_buf_t
*, xfs_log_item_t
*))
1287 xfs_qm_dqflush_done
, &(dqp
->q_logitem
.qli_item
));
1289 * If the buffer is pinned then push on the log so we won't
1290 * get stuck waiting in the write for too long.
1292 if (XFS_BUF_ISPINNED(bp
)) {
1293 xfs_dqtrace_entry(dqp
, "DQFLUSH LOG FORCE");
1294 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
1297 if (flags
& XFS_QMOPT_DELWRI
) {
1298 xfs_bdwrite(mp
, bp
);
1299 } else if (flags
& XFS_QMOPT_ASYNC
) {
1300 xfs_bawrite(mp
, bp
);
1302 error
= xfs_bwrite(mp
, bp
);
1304 xfs_dqtrace_entry(dqp
, "DQFLUSH END");
1306 * dqp is still locked, but caller is free to unlock it now.
1313 * This is the dquot flushing I/O completion routine. It is called
1314 * from interrupt level when the buffer containing the dquot is
1315 * flushed to disk. It is responsible for removing the dquot logitem
1316 * from the AIL if it has not been re-logged, and unlocking the dquot's
1317 * flush lock. This behavior is very similar to that of inodes..
1321 xfs_qm_dqflush_done(
1323 xfs_dq_logitem_t
*qip
)
1328 dqp
= qip
->qli_dquot
;
1331 * We only want to pull the item from the AIL if its
1332 * location in the log has not changed since we started the flush.
1333 * Thus, we only bother if the dquot's lsn has
1334 * not changed. First we check the lsn outside the lock
1335 * since it's cheaper, and then we recheck while
1336 * holding the lock before removing the dquot from the AIL.
1338 if ((qip
->qli_item
.li_flags
& XFS_LI_IN_AIL
) &&
1339 qip
->qli_item
.li_lsn
== qip
->qli_flush_lsn
) {
1341 AIL_LOCK(dqp
->q_mount
, s
);
1343 * xfs_trans_delete_ail() drops the AIL lock.
1345 if (qip
->qli_item
.li_lsn
== qip
->qli_flush_lsn
)
1346 xfs_trans_delete_ail(dqp
->q_mount
,
1347 (xfs_log_item_t
*)qip
, s
);
1349 AIL_UNLOCK(dqp
->q_mount
, s
);
1353 * Release the dq's flush lock since we're done with it.
1360 xfs_qm_dqflock_nowait(
1365 locked
= cpsema(&((dqp
)->q_flock
));
1367 /* XXX ifdef these out */
1369 (dqp
)->dq_flags
|= XFS_DQ_FLOCKED
;
1375 xfs_qm_dqlock_nowait(
1378 return (mutex_trylock(&((dqp
)->q_qlock
)));
1385 mutex_lock(&(dqp
->q_qlock
), PINOD
);
1392 mutex_unlock(&(dqp
->q_qlock
));
1393 if (dqp
->q_logitem
.qli_dquot
== dqp
) {
1394 /* Once was dqp->q_mount, but might just have been cleared */
1395 xfs_trans_unlocked_item(dqp
->q_logitem
.qli_item
.li_mountp
,
1396 (xfs_log_item_t
*)&(dqp
->q_logitem
));
1402 xfs_dqunlock_nonotify(
1405 mutex_unlock(&(dqp
->q_qlock
));
1415 if (be32_to_cpu(d1
->q_core
.d_id
) >
1416 be32_to_cpu(d2
->q_core
.d_id
)) {
1434 * Take a dquot out of the mount's dqlist as well as the hashlist.
1435 * This is called via unmount as well as quotaoff, and the purge
1436 * will always succeed unless there are soft (temp) references
1439 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1440 * that we're returning! XXXsup - not cool.
1448 xfs_dqhash_t
*thishash
;
1453 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp
));
1454 ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp
->q_hash
));
1458 * We really can't afford to purge a dquot that is
1459 * referenced, because these are hard refs.
1460 * It shouldn't happen in general because we went thru _all_ inodes in
1461 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1462 * However it is possible that we have dquots with temporary
1463 * references that are not attached to an inode. e.g. see xfs_setattr().
1465 if (dqp
->q_nrefs
!= 0) {
1467 XFS_DQ_HASH_UNLOCK(dqp
->q_hash
);
1471 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp
));
1474 * If we're turning off quotas, we have to make sure that, for
1475 * example, we don't delete quota disk blocks while dquots are
1476 * in the process of getting written to those disk blocks.
1477 * This dquot might well be on AIL, and we can't leave it there
1478 * if we're turning off quotas. Basically, we need this flush
1479 * lock, and are willing to block on it.
1481 if (! xfs_qm_dqflock_nowait(dqp
)) {
1483 * Block on the flush lock after nudging dquot buffer,
1486 xfs_qm_dqflock_pushbuf_wait(dqp
);
1490 * XXXIf we're turning this type of quotas off, we don't care
1491 * about the dirty metadata sitting in this dquot. OTOH, if
1492 * we're unmounting, we do care, so we flush it and wait.
1494 if (XFS_DQ_IS_DIRTY(dqp
)) {
1495 xfs_dqtrace_entry(dqp
, "DQPURGE ->DQFLUSH: DQDIRTY");
1496 /* dqflush unlocks dqflock */
1498 * Given that dqpurge is a very rare occurrence, it is OK
1499 * that we're holding the hashlist and mplist locks
1500 * across the disk write. But, ... XXXsup
1502 * We don't care about getting disk errors here. We need
1503 * to purge this dquot anyway, so we go ahead regardless.
1505 (void) xfs_qm_dqflush(dqp
, XFS_QMOPT_SYNC
);
1508 ASSERT(dqp
->q_pincount
== 0);
1509 ASSERT(XFS_FORCED_SHUTDOWN(mp
) ||
1510 !(dqp
->q_logitem
.qli_item
.li_flags
& XFS_LI_IN_AIL
));
1512 thishash
= dqp
->q_hash
;
1513 XQM_HASHLIST_REMOVE(thishash
, dqp
);
1514 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp
)), dqp
);
1516 * XXX Move this to the front of the freelist, if we can get the
1519 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp
));
1521 dqp
->q_mount
= NULL
;
1523 dqp
->dq_flags
= XFS_DQ_INACTIVE
;
1524 memset(&dqp
->q_core
, 0, sizeof(dqp
->q_core
));
1527 XFS_DQ_HASH_UNLOCK(thishash
);
1534 xfs_qm_dqprint(xfs_dquot_t
*dqp
)
1536 cmn_err(CE_DEBUG
, "-----------KERNEL DQUOT----------------");
1537 cmn_err(CE_DEBUG
, "---- dquotID = %d",
1538 (int)be32_to_cpu(dqp
->q_core
.d_id
));
1539 cmn_err(CE_DEBUG
, "---- type = %s", DQFLAGTO_TYPESTR(dqp
));
1540 cmn_err(CE_DEBUG
, "---- fs = 0x%p", dqp
->q_mount
);
1541 cmn_err(CE_DEBUG
, "---- blkno = 0x%x", (int) dqp
->q_blkno
);
1542 cmn_err(CE_DEBUG
, "---- boffset = 0x%x", (int) dqp
->q_bufoffset
);
1543 cmn_err(CE_DEBUG
, "---- blkhlimit = %Lu (0x%x)",
1544 be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
),
1545 (int)be64_to_cpu(dqp
->q_core
.d_blk_hardlimit
));
1546 cmn_err(CE_DEBUG
, "---- blkslimit = %Lu (0x%x)",
1547 be64_to_cpu(dqp
->q_core
.d_blk_softlimit
),
1548 (int)be64_to_cpu(dqp
->q_core
.d_blk_softlimit
));
1549 cmn_err(CE_DEBUG
, "---- inohlimit = %Lu (0x%x)",
1550 be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
),
1551 (int)be64_to_cpu(dqp
->q_core
.d_ino_hardlimit
));
1552 cmn_err(CE_DEBUG
, "---- inoslimit = %Lu (0x%x)",
1553 be64_to_cpu(dqp
->q_core
.d_ino_softlimit
),
1554 (int)be64_to_cpu(dqp
->q_core
.d_ino_softlimit
));
1555 cmn_err(CE_DEBUG
, "---- bcount = %Lu (0x%x)",
1556 be64_to_cpu(dqp
->q_core
.d_bcount
),
1557 (int)be64_to_cpu(dqp
->q_core
.d_bcount
));
1558 cmn_err(CE_DEBUG
, "---- icount = %Lu (0x%x)",
1559 be64_to_cpu(dqp
->q_core
.d_icount
),
1560 (int)be64_to_cpu(dqp
->q_core
.d_icount
));
1561 cmn_err(CE_DEBUG
, "---- btimer = %d",
1562 (int)be32_to_cpu(dqp
->q_core
.d_btimer
));
1563 cmn_err(CE_DEBUG
, "---- itimer = %d",
1564 (int)be32_to_cpu(dqp
->q_core
.d_itimer
));
1565 cmn_err(CE_DEBUG
, "---------------------------");
1570 * Give the buffer a little push if it is incore and
1571 * wait on the flush lock.
1574 xfs_qm_dqflock_pushbuf_wait(
1580 * Check to see if the dquot has been flushed delayed
1581 * write. If so, grab its buffer and send it
1582 * out immediately. We'll be able to acquire
1583 * the flush lock when the I/O completes.
1585 bp
= xfs_incore(dqp
->q_mount
->m_ddev_targp
, dqp
->q_blkno
,
1586 XFS_QI_DQCHUNKLEN(dqp
->q_mount
),
1587 XFS_INCORE_TRYLOCK
);
1589 if (XFS_BUF_ISDELAYWRITE(bp
)) {
1590 if (XFS_BUF_ISPINNED(bp
)) {
1591 xfs_log_force(dqp
->q_mount
,
1595 xfs_bawrite(dqp
->q_mount
, bp
);