2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
38 #include "xfs_trans.h"
43 #include "xfs_alloc.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_quota.h"
46 #include "xfs_mount.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_bmap_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_btree.h"
51 #include "xfs_ialloc.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode.h"
59 #include "xfs_rtalloc.h"
60 #include "xfs_error.h"
61 #include "xfs_itable.h"
67 #include "xfs_buf_item.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_utils.h"
74 * The global quota manager. There is only one of these for the entire
75 * system, _not_ one per file system. XQM keeps track of the overall
76 * quota functionality, including maintaining the freelist and hash
80 struct xfs_qm *xfs_Gqm;
82 kmem_zone_t *qm_dqzone;
83 kmem_zone_t *qm_dqtrxzone;
84 kmem_shaker_t xfs_qm_shaker;
86 STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int);
87 STATIC void xfs_qm_list_destroy(xfs_dqlist_t *);
89 STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
90 STATIC int xfs_qm_shake(int, unsigned int);
93 extern mutex_t qcheck_lock;
97 #define XQM_LIST_PRINT(l, NXT, title) \
99 xfs_dquot_t *dqp; int i = 0; \
100 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
101 for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \
102 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \
103 "bcnt = %d, icnt = %d, refs = %d", \
104 ++i, (int) INT_GET(dqp->q_core.d_id, ARCH_CONVERT), \
105 DQFLAGTO_TYPESTR(dqp), \
106 (int) INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT), \
107 (int) INT_GET(dqp->q_core.d_icount, ARCH_CONVERT), \
108 (int) dqp->q_nrefs); } \
111 #define XQM_LIST_PRINT(l, NXT, title) do { } while (0)
115 * Initialize the XQM structure.
116 * Note that there is not one quota manager per file system.
118 STATIC struct xfs_qm *
124 xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
128 * Initialize the dquot hash tables.
130 hsize = (DQUOT_HASH_HEURISTIC < XFS_QM_NCSIZE_THRESHOLD) ?
131 XFS_QM_HASHSIZE_LOW : XFS_QM_HASHSIZE_HIGH;
132 xqm->qm_dqhashmask = hsize - 1;
134 xqm->qm_usr_dqhtable = (xfs_dqhash_t *)kmem_zalloc(hsize *
135 sizeof(xfs_dqhash_t),
137 xqm->qm_grp_dqhtable = (xfs_dqhash_t *)kmem_zalloc(hsize *
138 sizeof(xfs_dqhash_t),
140 ASSERT(xqm->qm_usr_dqhtable != NULL);
141 ASSERT(xqm->qm_grp_dqhtable != NULL);
143 for (i = 0; i < hsize; i++) {
144 xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
145 xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
149 * Freelist of all dquots of all file systems
151 xfs_qm_freelist_init(&(xqm->qm_dqfreelist));
154 * dquot zone. we register our own low-memory callback.
157 xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
159 qm_dqzone = xqm->qm_dqzone;
161 xqm->qm_dqzone = qm_dqzone;
163 xfs_qm_shaker = kmem_shake_register(xfs_qm_shake);
166 * The t_dqinfo portion of transactions.
169 xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
171 qm_dqtrxzone = xqm->qm_dqtrxzone;
173 xqm->qm_dqtrxzone = qm_dqtrxzone;
175 atomic_set(&xqm->qm_totaldquots, 0);
176 xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
179 mutex_init(&qcheck_lock, MUTEX_DEFAULT, "qchk");
185 * Destroy the global quota manager when its reference count goes to zero.
194 ASSERT(xqm->qm_nrefs == 0);
195 kmem_shake_deregister(xfs_qm_shaker);
196 hsize = xqm->qm_dqhashmask + 1;
197 for (i = 0; i < hsize; i++) {
198 xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
199 xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
201 kmem_free(xqm->qm_usr_dqhtable, hsize * sizeof(xfs_dqhash_t));
202 kmem_free(xqm->qm_grp_dqhtable, hsize * sizeof(xfs_dqhash_t));
203 xqm->qm_usr_dqhtable = NULL;
204 xqm->qm_grp_dqhtable = NULL;
205 xqm->qm_dqhashmask = 0;
206 xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist));
208 mutex_destroy(&qcheck_lock);
210 kmem_free(xqm, sizeof(xfs_qm_t));
214 * Called at mount time to let XQM know that another file system is
215 * starting quotas. This isn't crucial information as the individual mount
216 * structures are pretty independent, but it helps the XQM keep a
217 * global view of what's going on.
221 xfs_qm_hold_quotafs_ref(
222 struct xfs_mount *mp)
225 * Need to lock the xfs_Gqm structure for things like this. For example,
226 * the structure could disappear between the entry to this routine and
227 * a HOLD operation if not locked.
229 XFS_QM_LOCK(xfs_Gqm);
232 xfs_Gqm = xfs_Gqm_init();
234 * We can keep a list of all filesystems with quotas mounted for
235 * debugging and statistical purposes, but ...
236 * Just take a reference and get out.
238 XFS_QM_HOLD(xfs_Gqm);
239 XFS_QM_UNLOCK(xfs_Gqm);
246 * Release the reference that a filesystem took at mount time,
247 * so that we know when we need to destroy the entire quota manager.
251 xfs_qm_rele_quotafs_ref(
252 struct xfs_mount *mp)
254 xfs_dquot_t *dqp, *nextdqp;
257 ASSERT(xfs_Gqm->qm_nrefs > 0);
260 * Go thru the freelist and destroy all inactive dquots.
262 xfs_qm_freelist_lock(xfs_Gqm);
264 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
265 dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) {
267 nextdqp = dqp->dq_flnext;
268 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
269 ASSERT(dqp->q_mount == NULL);
270 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
271 ASSERT(dqp->HL_PREVP == NULL);
272 ASSERT(dqp->MPL_PREVP == NULL);
273 XQM_FREELIST_REMOVE(dqp);
275 xfs_qm_dqdestroy(dqp);
281 xfs_qm_freelist_unlock(xfs_Gqm);
284 * Destroy the entire XQM. If somebody mounts with quotaon, this'll
287 XFS_QM_LOCK(xfs_Gqm);
288 XFS_QM_RELE(xfs_Gqm);
289 if (xfs_Gqm->qm_nrefs == 0) {
290 xfs_qm_destroy(xfs_Gqm);
293 XFS_QM_UNLOCK(xfs_Gqm);
297 * This is called at mount time from xfs_mountfs to initialize the quotainfo
298 * structure and start the global quotamanager (xfs_Gqm) if it hasn't done
299 * so already. Note that the superblock has not been read in yet.
302 xfs_qm_mount_quotainit(
307 * User or group quotas has to be on.
309 ASSERT(flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA));
312 * Initialize the flags in the mount structure. From this point
313 * onwards we look at m_qflags to figure out if quotas's ON/OFF, etc.
314 * Note that we enforce nothing if accounting is off.
315 * ie. XFSMNT_*QUOTA must be ON for XFSMNT_*QUOTAENF.
316 * It isn't necessary to take the quotaoff lock to do this; this is
319 if (flags & XFSMNT_UQUOTA) {
320 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
321 if (flags & XFSMNT_UQUOTAENF)
322 mp->m_qflags |= XFS_UQUOTA_ENFD;
324 if (flags & XFSMNT_GQUOTA) {
325 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
326 if (flags & XFSMNT_GQUOTAENF)
327 mp->m_qflags |= XFS_GQUOTA_ENFD;
332 * Just destroy the quotainfo structure.
335 xfs_qm_unmount_quotadestroy(
339 xfs_qm_destroy_quotainfo(mp);
344 * This is called from xfs_mountfs to start quotas and initialize all
345 * necessary data structures like quotainfo. This is also responsible for
346 * running a quotacheck as necessary. We are guaranteed that the superblock
347 * is consistently read in at this point.
359 * If a file system had quotas running earlier, but decided to
360 * mount without -o quota/uquota/gquota options, revoke the
361 * quotachecked license, and bail out.
363 if (! XFS_IS_QUOTA_ON(mp) &&
364 (mp->m_sb.sb_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT))) {
370 * If quotas on realtime volumes is not supported, we disable
371 * quotas immediately.
373 if (mp->m_sb.sb_rextents) {
375 "Cannot turn on quotas for realtime filesystem %s",
381 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
382 cmn_err(CE_NOTE, "Attempting to turn on disk quotas.");
385 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
387 * Allocate the quotainfo structure inside the mount struct, and
388 * create quotainode(s), and change/rev superblock if necessary.
390 if ((error = xfs_qm_init_quotainfo(mp))) {
392 * We must turn off quotas.
394 ASSERT(mp->m_quotainfo == NULL);
399 * If any of the quotas are not consistent, do a quotacheck.
401 if (XFS_QM_NEED_QUOTACHECK(mp) &&
402 !(mfsi_flags & XFS_MFSI_NO_QUOTACHECK)) {
404 cmn_err(CE_NOTE, "Doing a quotacheck. Please wait.");
406 if ((error = xfs_qm_quotacheck(mp))) {
407 /* Quotacheck has failed and quotas have
410 return XFS_ERROR(error);
413 cmn_err(CE_NOTE, "Done quotacheck.");
418 * We actually don't have to acquire the SB_LOCK at all.
419 * This can only be called from mount, and that's single threaded. XXX
422 sbf = mp->m_sb.sb_qflags;
423 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
424 XFS_SB_UNLOCK(mp, s);
426 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
427 if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
429 * We could only have been turning quotas off.
430 * We aren't in very good shape actually because
431 * the incore structures are convinced that quotas are
432 * off, but the on disk superblock doesn't know that !
434 ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
435 xfs_fs_cmn_err(CE_ALERT, mp,
436 "XFS mount_quotas: Superblock update failed!");
441 xfs_fs_cmn_err(CE_WARN, mp,
442 "Failed to initialize disk quotas.");
444 return XFS_ERROR(error);
448 * Called from the vfsops layer.
451 xfs_qm_unmount_quotas(
454 xfs_inode_t *uqp, *gqp;
458 * Release the dquots that root inode, et al might be holding,
459 * before we flush quotas and blow away the quotainfo structure.
461 ASSERT(mp->m_rootip);
462 xfs_qm_dqdetach(mp->m_rootip);
464 xfs_qm_dqdetach(mp->m_rbmip);
466 xfs_qm_dqdetach(mp->m_rsumip);
469 * Flush out the quota inodes.
472 if (mp->m_quotainfo) {
473 if ((uqp = mp->m_quotainfo->qi_uquotaip) != NULL) {
474 xfs_ilock(uqp, XFS_ILOCK_EXCL);
476 error = xfs_iflush(uqp, XFS_IFLUSH_SYNC);
477 xfs_iunlock(uqp, XFS_ILOCK_EXCL);
478 if (unlikely(error == EFSCORRUPTED)) {
479 XFS_ERROR_REPORT("xfs_qm_unmount_quotas(1)",
480 XFS_ERRLEVEL_LOW, mp);
484 if ((gqp = mp->m_quotainfo->qi_gquotaip) != NULL) {
485 xfs_ilock(gqp, XFS_ILOCK_EXCL);
487 error = xfs_iflush(gqp, XFS_IFLUSH_SYNC);
488 xfs_iunlock(gqp, XFS_ILOCK_EXCL);
489 if (unlikely(error == EFSCORRUPTED)) {
490 XFS_ERROR_REPORT("xfs_qm_unmount_quotas(2)",
491 XFS_ERRLEVEL_LOW, mp);
497 XFS_PURGE_INODE(uqp);
498 mp->m_quotainfo->qi_uquotaip = NULL;
501 XFS_PURGE_INODE(gqp);
502 mp->m_quotainfo->qi_gquotaip = NULL;
505 return XFS_ERROR(error);
509 * Flush all dquots of the given file system to disk. The dquots are
510 * _not_ purged from memory here, just their data written to disk.
522 if (mp->m_quotainfo == NULL)
526 xfs_qm_mplist_lock(mp);
527 FOREACH_DQUOT_IN_MP(dqp, mp) {
529 if (! XFS_DQ_IS_DIRTY(dqp)) {
533 xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY");
534 /* XXX a sentinel would be better */
535 recl = XFS_QI_MPLRECLAIMS(mp);
536 if (! xfs_qm_dqflock_nowait(dqp)) {
538 * If we can't grab the flush lock then check
539 * to see if the dquot has been flushed delayed
540 * write. If so, grab its buffer and send it
541 * out immediately. We'll be able to acquire
542 * the flush lock when the I/O completes.
544 xfs_qm_dqflock_pushbuf_wait(dqp);
547 * Let go of the mplist lock. We don't want to hold it
548 * across a disk write.
550 xfs_qm_mplist_unlock(mp);
551 error = xfs_qm_dqflush(dqp, flags);
556 xfs_qm_mplist_lock(mp);
557 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
558 xfs_qm_mplist_unlock(mp);
559 /* XXX restart limit */
564 xfs_qm_mplist_unlock(mp);
569 * Release the group dquot pointers the user dquots may be
570 * carrying around as a hint. mplist is locked on entry and exit.
573 xfs_qm_detach_gdquots(
576 xfs_dquot_t *dqp, *gdqp;
580 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
581 dqp = XFS_QI_MPLNEXT(mp);
584 if ((gdqp = dqp->q_gdquot)) {
586 dqp->q_gdquot = NULL;
592 * Can't hold the mplist lock across a dqput.
593 * XXXmust convert to marker based iterations here.
595 nrecl = XFS_QI_MPLRECLAIMS(mp);
596 xfs_qm_mplist_unlock(mp);
599 xfs_qm_mplist_lock(mp);
600 if (nrecl != XFS_QI_MPLRECLAIMS(mp))
608 * Go through all the incore dquots of this file system and take them
609 * off the mplist and hashlist, if the dquot type matches the dqtype
610 * parameter. This is used when turning off quota accounting for
611 * users and/or groups, as well as when the filesystem is unmounting.
616 uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/GQUOTA */
621 xfs_dquot_t *nextdqp;
624 if (mp->m_quotainfo == NULL)
627 dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
628 dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
630 xfs_qm_mplist_lock(mp);
633 * In the first pass through all incore dquots of this filesystem,
634 * we release the group dquot pointers the user dquots may be
635 * carrying around as a hint. We need to do this irrespective of
636 * what's being turned off.
638 xfs_qm_detach_gdquots(mp);
642 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
644 * Try to get rid of all of the unwanted dquots. The idea is to
645 * get them off mplist and hashlist, but leave them on freelist.
647 dqp = XFS_QI_MPLNEXT(mp);
650 * It's OK to look at the type without taking dqlock here.
651 * We're holding the mplist lock here, and that's needed for
654 if ((dqp->dq_flags & dqtype) == 0) {
659 if (! xfs_qm_dqhashlock_nowait(dqp)) {
660 nrecl = XFS_QI_MPLRECLAIMS(mp);
661 xfs_qm_mplist_unlock(mp);
662 XFS_DQ_HASH_LOCK(dqp->q_hash);
663 xfs_qm_mplist_lock(mp);
666 * XXXTheoretically, we can get into a very long
667 * ping pong game here.
668 * No one can be adding dquots to the mplist at
669 * this point, but somebody might be taking things off.
671 if (nrecl != XFS_QI_MPLRECLAIMS(mp)) {
672 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
678 * Take the dquot off the mplist and hashlist. It may remain on
679 * freelist in INACTIVE state.
681 nextdqp = dqp->MPL_NEXT;
682 nmisses += xfs_qm_dqpurge(dqp, flags);
685 xfs_qm_mplist_unlock(mp);
697 * Purge the dquot cache.
698 * None of the dquots should really be busy at this point.
700 if (mp->m_quotainfo) {
701 while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
715 xfs_dquot_t *udqhint, /* hint */
716 xfs_dquot_t **IO_idqpp)
721 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
724 * See if we already have it in the inode itself. IO_idqpp is
725 * &i_udquot or &i_gdquot. This made the code look weird, but
726 * made the logic a lot simpler.
728 if ((dqp = *IO_idqpp)) {
731 xfs_dqtrace_entry(dqp, "DQATTACH: found in ip");
736 * udqhint is the i_udquot field in inode, and is non-NULL only
737 * when the type arg is XFS_DQ_GROUP. Its purpose is to save a
738 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
741 ASSERT(!udqhint || type == XFS_DQ_GROUP);
742 if (udqhint && !dolock)
746 * No need to take dqlock to look at the id.
747 * The ID can't change until it gets reclaimed, and it won't
748 * be reclaimed as long as we have a ref from inode and we hold
752 (dqp = udqhint->q_gdquot) &&
753 (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id)) {
754 ASSERT(XFS_DQ_IS_LOCKED(udqhint));
757 ASSERT(*IO_idqpp == NULL);
761 xfs_dqunlock(udqhint);
766 * We can't hold a dquot lock when we call the dqget code.
767 * We'll deadlock in no time, because of (not conforming to)
768 * lock ordering - the inodelock comes before any dquot lock,
769 * and we may drop and reacquire the ilock in xfs_qm_dqget().
772 xfs_dqunlock(udqhint);
774 * Find the dquot from somewhere. This bumps the
775 * reference count of dquot and returns it locked.
776 * This can return ENOENT if dquot didn't exist on
777 * disk and we didn't ask it to allocate;
778 * ESRCH if quotas got turned off suddenly.
780 if ((error = xfs_qm_dqget(ip->i_mount, ip, id, type,
781 doalloc|XFS_QMOPT_DOWARN, &dqp))) {
782 if (udqhint && dolock)
787 xfs_dqtrace_entry(dqp, "DQATTACH: found by dqget");
789 * dqget may have dropped and re-acquired the ilock, but it guarantees
790 * that the dquot returned is the one that should go in the inode.
794 ASSERT(XFS_DQ_IS_LOCKED(dqp));
804 ASSERT(XFS_DQ_IS_LOCKED(dqp));
805 if (! xfs_qm_dqlock_nowait(udqhint)) {
814 ASSERT(XFS_DQ_IS_LOCKED(udqhint));
818 ASSERT(XFS_DQ_IS_LOCKED(dqp));
826 * Given a udquot and gdquot, attach a ptr to the group dquot in the
827 * udquot as a hint for future lookups. The idea sounds simple, but the
828 * execution isn't, because the udquot might have a group dquot attached
829 * already and getting rid of that gets us into lock ordering contraints.
830 * The process is complicated more by the fact that the dquots may or may not
831 * be locked on entry.
834 xfs_qm_dqattach_grouphint(
843 ASSERT(XFS_DQ_IS_LOCKED(udq));
844 ASSERT(XFS_DQ_IS_LOCKED(gdq));
850 if ((tmp = udq->q_gdquot)) {
857 udq->q_gdquot = NULL;
859 * We can't keep any dqlocks when calling dqrele,
860 * because the freelist lock comes before dqlocks.
866 * we took a hard reference once upon a time in dqget,
867 * so give it back when the udquot no longer points at it
868 * dqput() does the unlocking of the dquot.
876 ASSERT(XFS_DQ_IS_LOCKED(udq));
882 ASSERT(XFS_DQ_IS_LOCKED(udq));
883 ASSERT(XFS_DQ_IS_LOCKED(gdq));
885 * Somebody could have attached a gdquot here,
886 * when we dropped the uqlock. If so, just do nothing.
888 if (udq->q_gdquot == NULL) {
900 * Given a locked inode, attach dquot(s) to it, taking UQUOTAON / GQUOTAON
902 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
903 * If XFS_QMOPT_DQLOCK, the dquot(s) will be returned locked. This option pretty
904 * much made this code a complete mess, but it has been pretty useful.
905 * If XFS_QMOPT_ILOCKED, then inode sent is already locked EXCL.
906 * Inode may get unlocked and relocked in here, and the caller must deal with
914 xfs_mount_t *mp = ip->i_mount;
918 if ((! XFS_IS_QUOTA_ON(mp)) ||
919 (! XFS_NOT_DQATTACHED(mp, ip)) ||
920 (ip->i_ino == mp->m_sb.sb_uquotino) ||
921 (ip->i_ino == mp->m_sb.sb_gquotino))
924 ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 ||
925 XFS_ISLOCKED_INODE_EXCL(ip));
927 if (! (flags & XFS_QMOPT_ILOCKED))
928 xfs_ilock(ip, XFS_ILOCK_EXCL);
930 if (XFS_IS_UQUOTA_ON(mp)) {
931 error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
932 flags & XFS_QMOPT_DQALLOC,
933 flags & XFS_QMOPT_DQLOCK,
934 NULL, &ip->i_udquot);
939 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
940 if (XFS_IS_GQUOTA_ON(mp)) {
941 error = xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
942 flags & XFS_QMOPT_DQALLOC,
943 flags & XFS_QMOPT_DQLOCK,
944 ip->i_udquot, &ip->i_gdquot);
946 * Don't worry about the udquot that we may have
947 * attached above. It'll get detached, if not already.
955 * Attach this group quota to the user quota as a hint.
956 * This WON'T, in general, result in a thrash.
959 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
960 ASSERT(ip->i_udquot);
961 ASSERT(ip->i_gdquot);
964 * We may or may not have the i_udquot locked at this point,
965 * but this check is OK since we don't depend on the i_gdquot to
966 * be accurate 100% all the time. It is just a hint, and this
967 * will succeed in general.
969 if (ip->i_udquot->q_gdquot == ip->i_gdquot)
972 * Attach i_gdquot to the gdquot hint inside the i_udquot.
974 xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot,
975 flags & XFS_QMOPT_DQLOCK);
983 if (flags & XFS_QMOPT_DQLOCK)
984 ASSERT(XFS_DQ_IS_LOCKED(ip->i_udquot));
987 if (flags & XFS_QMOPT_DQLOCK)
988 ASSERT(XFS_DQ_IS_LOCKED(ip->i_gdquot));
990 if (XFS_IS_UQUOTA_ON(mp))
991 ASSERT(ip->i_udquot);
992 if (XFS_IS_GQUOTA_ON(mp))
993 ASSERT(ip->i_gdquot);
997 if (! (flags & XFS_QMOPT_ILOCKED))
998 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1002 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
1008 * Release dquots (and their references) if any.
1009 * The inode should be locked EXCL except when this's called by
1016 if (!(ip->i_udquot || ip->i_gdquot))
1019 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
1020 ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
1022 xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
1024 xfs_qm_dqrele(ip->i_udquot);
1025 ip->i_udquot = NULL;
1028 xfs_qm_dqrele(ip->i_gdquot);
1029 ip->i_gdquot = NULL;
1034 * This is called by VFS_SYNC and flags arg determines the caller,
1035 * and its motives, as done in xfs_sync.
1037 * vfs_sync: SYNC_FSDATA|SYNC_ATTR|SYNC_BDFLUSH 0x31
1038 * syscall sync: SYNC_FSDATA|SYNC_ATTR|SYNC_DELWRI 0x25
1039 * umountroot : SYNC_WAIT | SYNC_CLOSE | SYNC_ATTR | SYNC_FSDATA
1055 * We won't block unless we are asked to.
1057 nowait = (boolean_t)(flags & SYNC_BDFLUSH || (flags & SYNC_WAIT) == 0);
1060 xfs_qm_mplist_lock(mp);
1062 * dqpurge_all() also takes the mplist lock and iterate thru all dquots
1063 * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
1064 * when we have the mplist lock, we know that dquots will be consistent
1065 * as long as we have it locked.
1067 if (! XFS_IS_QUOTA_ON(mp)) {
1068 xfs_qm_mplist_unlock(mp);
1071 FOREACH_DQUOT_IN_MP(dqp, mp) {
1073 * If this is vfs_sync calling, then skip the dquots that
1074 * don't 'seem' to be dirty. ie. don't acquire dqlock.
1075 * This is very similar to what xfs_sync does with inodes.
1077 if (flags & SYNC_BDFLUSH) {
1078 if (! XFS_DQ_IS_DIRTY(dqp))
1084 * Try to acquire the dquot lock. We are NOT out of
1085 * lock order, but we just don't want to wait for this
1086 * lock, unless somebody wanted us to.
1088 if (! xfs_qm_dqlock_nowait(dqp))
1095 * Now, find out for sure if this dquot is dirty or not.
1097 if (! XFS_DQ_IS_DIRTY(dqp)) {
1102 /* XXX a sentinel would be better */
1103 recl = XFS_QI_MPLRECLAIMS(mp);
1104 if (! xfs_qm_dqflock_nowait(dqp)) {
1110 * If we can't grab the flush lock then if the caller
1111 * really wanted us to give this our best shot,
1112 * see if we can give a push to the buffer before we wait
1113 * on the flush lock. At this point, we know that
1114 * eventhough the dquot is being flushed,
1115 * it has (new) dirty data.
1117 xfs_qm_dqflock_pushbuf_wait(dqp);
1120 * Let go of the mplist lock. We don't want to hold it
1121 * across a disk write
1123 flush_flags = (nowait) ? XFS_QMOPT_DELWRI : XFS_QMOPT_SYNC;
1124 xfs_qm_mplist_unlock(mp);
1125 xfs_dqtrace_entry(dqp, "XQM_SYNC: DQFLUSH");
1126 error = xfs_qm_dqflush(dqp, flush_flags);
1128 if (error && XFS_FORCED_SHUTDOWN(mp))
1129 return(0); /* Need to prevent umount failure */
1133 xfs_qm_mplist_lock(mp);
1134 if (recl != XFS_QI_MPLRECLAIMS(mp)) {
1135 if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
1138 xfs_qm_mplist_unlock(mp);
1143 xfs_qm_mplist_unlock(mp);
1149 * This initializes all the quota information that's kept in the
1153 xfs_qm_init_quotainfo(
1156 xfs_quotainfo_t *qinf;
1160 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1163 * Tell XQM that we exist as soon as possible.
1165 if ((error = xfs_qm_hold_quotafs_ref(mp))) {
1169 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
1172 * See if quotainodes are setup, and if not, allocate them,
1173 * and change the superblock accordingly.
1175 if ((error = xfs_qm_init_quotainos(mp))) {
1176 kmem_free(qinf, sizeof(xfs_quotainfo_t));
1177 mp->m_quotainfo = NULL;
1181 spinlock_init(&qinf->qi_pinlock, "xfs_qinf_pin");
1182 xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
1183 qinf->qi_dqreclaims = 0;
1185 /* mutex used to serialize quotaoffs */
1186 mutex_init(&qinf->qi_quotaofflock, MUTEX_DEFAULT, "qoff");
1188 /* Precalc some constants */
1189 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1190 ASSERT(qinf->qi_dqchunklen);
1191 qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
1192 do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
1194 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
1197 * We try to get the limits from the superuser's limits fields.
1198 * This is quite hacky, but it is standard quota practice.
1199 * We look at the USR dquot with id == 0 first, but if user quotas
1200 * are not enabled we goto the GRP dquot with id == 0.
1201 * We don't really care to keep separate default limits for user
1202 * and group quotas, at least not at this point.
1204 error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
1205 (XFS_IS_UQUOTA_RUNNING(mp)) ?
1206 XFS_DQ_USER : XFS_DQ_GROUP,
1207 XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
1210 xfs_disk_dquot_t *ddqp = &dqp->q_core;
1213 * The warnings and timers set the grace period given to
1214 * a user or group before he or she can not perform any
1215 * more writing. If it is zero, a default is used.
1217 qinf->qi_btimelimit =
1218 INT_GET(ddqp->d_btimer, ARCH_CONVERT) ?
1219 INT_GET(ddqp->d_btimer, ARCH_CONVERT) :
1221 qinf->qi_itimelimit =
1222 INT_GET(ddqp->d_itimer, ARCH_CONVERT) ?
1223 INT_GET(ddqp->d_itimer, ARCH_CONVERT) :
1225 qinf->qi_rtbtimelimit =
1226 INT_GET(ddqp->d_rtbtimer, ARCH_CONVERT) ?
1227 INT_GET(ddqp->d_rtbtimer, ARCH_CONVERT) :
1228 XFS_QM_RTBTIMELIMIT;
1229 qinf->qi_bwarnlimit =
1230 INT_GET(ddqp->d_bwarns, ARCH_CONVERT) ?
1231 INT_GET(ddqp->d_bwarns, ARCH_CONVERT) :
1233 qinf->qi_iwarnlimit =
1234 INT_GET(ddqp->d_iwarns, ARCH_CONVERT) ?
1235 INT_GET(ddqp->d_iwarns, ARCH_CONVERT) :
1237 qinf->qi_bhardlimit =
1238 INT_GET(ddqp->d_blk_hardlimit, ARCH_CONVERT);
1239 qinf->qi_bsoftlimit =
1240 INT_GET(ddqp->d_blk_softlimit, ARCH_CONVERT);
1241 qinf->qi_ihardlimit =
1242 INT_GET(ddqp->d_ino_hardlimit, ARCH_CONVERT);
1243 qinf->qi_isoftlimit =
1244 INT_GET(ddqp->d_ino_softlimit, ARCH_CONVERT);
1245 qinf->qi_rtbhardlimit =
1246 INT_GET(ddqp->d_rtb_hardlimit, ARCH_CONVERT);
1247 qinf->qi_rtbsoftlimit =
1248 INT_GET(ddqp->d_rtb_softlimit, ARCH_CONVERT);
1251 * We sent the XFS_QMOPT_DQSUSER flag to dqget because
1252 * we don't want this dquot cached. We haven't done a
1253 * quotacheck yet, and quotacheck doesn't like incore dquots.
1255 xfs_qm_dqdestroy(dqp);
1257 qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
1258 qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
1259 qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
1260 qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
1261 qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
1269 * Gets called when unmounting a filesystem or when all quotas get
1271 * This purges the quota inodes, destroys locks and frees itself.
1274 xfs_qm_destroy_quotainfo(
1277 xfs_quotainfo_t *qi;
1279 qi = mp->m_quotainfo;
1281 ASSERT(xfs_Gqm != NULL);
1284 * Release the reference that XQM kept, so that we know
1285 * when the XQM structure should be freed. We cannot assume
1286 * that xfs_Gqm is non-null after this point.
1288 xfs_qm_rele_quotafs_ref(mp);
1290 spinlock_destroy(&qi->qi_pinlock);
1291 xfs_qm_list_destroy(&qi->qi_dqlist);
1293 if (qi->qi_uquotaip) {
1294 XFS_PURGE_INODE(qi->qi_uquotaip);
1295 qi->qi_uquotaip = NULL; /* paranoia */
1297 if (qi->qi_gquotaip) {
1298 XFS_PURGE_INODE(qi->qi_gquotaip);
1299 qi->qi_gquotaip = NULL;
1301 mutex_destroy(&qi->qi_quotaofflock);
1302 kmem_free(qi, sizeof(xfs_quotainfo_t));
1303 mp->m_quotainfo = NULL;
1308 /* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
1317 mutex_init(&list->qh_lock, MUTEX_DEFAULT, str);
1318 list->qh_next = NULL;
1319 list->qh_version = 0;
1320 list->qh_nelems = 0;
1324 xfs_qm_list_destroy(
1327 mutex_destroy(&(list->qh_lock));
1332 * Stripped down version of dqattach. This doesn't attach, or even look at the
1333 * dquots attached to the inode. The rationale is that there won't be any
1334 * attached at the time this is called from quotacheck.
1337 xfs_qm_dqget_noattach(
1339 xfs_dquot_t **O_udqpp,
1340 xfs_dquot_t **O_gdqpp)
1344 xfs_dquot_t *udqp, *gdqp;
1346 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
1351 if (XFS_IS_UQUOTA_ON(mp)) {
1352 ASSERT(ip->i_udquot == NULL);
1354 * We want the dquot allocated if it doesn't exist.
1356 if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER,
1357 XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN,
1360 * Shouldn't be able to turn off quotas here.
1362 ASSERT(error != ESRCH);
1363 ASSERT(error != ENOENT);
1369 if (XFS_IS_GQUOTA_ON(mp)) {
1370 ASSERT(ip->i_gdquot == NULL);
1373 if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_gid, XFS_DQ_GROUP,
1374 XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
1377 xfs_qm_dqrele(udqp);
1378 ASSERT(error != ESRCH);
1379 ASSERT(error != ENOENT);
1384 /* Reacquire the locks in the right order */
1386 if (! xfs_qm_dqlock_nowait(udqp)) {
1398 if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
1399 if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
1405 * Create an inode and return with a reference already taken, but unlocked
1406 * This is how we create quota inodes
1421 tp = xfs_trans_alloc(mp,XFS_TRANS_QM_QINOCREATE);
1422 if ((error = xfs_trans_reserve(tp,
1423 XFS_QM_QINOCREATE_SPACE_RES(mp),
1424 XFS_CREATE_LOG_RES(mp), 0,
1425 XFS_TRANS_PERM_LOG_RES,
1426 XFS_CREATE_LOG_COUNT))) {
1427 xfs_trans_cancel(tp, 0);
1430 memset(&zerocr, 0, sizeof(zerocr));
1432 if ((error = xfs_dir_ialloc(&tp, mp->m_rootip, S_IFREG, 1, 0,
1433 &zerocr, 0, 1, ip, &committed))) {
1434 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
1440 * Keep an extra reference to this quota inode. This inode is
1441 * locked exclusively and joined to the transaction already.
1443 ASSERT(XFS_ISLOCKED_INODE_EXCL(*ip));
1444 VN_HOLD(XFS_ITOV((*ip)));
1447 * Make the changes in the superblock, and log those too.
1448 * sbfields arg may contain fields other than *QUOTINO;
1449 * VERSIONNUM for example.
1451 s = XFS_SB_LOCK(mp);
1452 if (flags & XFS_QMOPT_SBVERSION) {
1453 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1454 unsigned oldv = mp->m_sb.sb_versionnum;
1456 ASSERT(!XFS_SB_VERSION_HASQUOTA(&mp->m_sb));
1457 ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1458 XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
1459 (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
1460 XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
1462 XFS_SB_VERSION_ADDQUOTA(&mp->m_sb);
1463 mp->m_sb.sb_uquotino = NULLFSINO;
1464 mp->m_sb.sb_gquotino = NULLFSINO;
1466 /* qflags will get updated _after_ quotacheck */
1467 mp->m_sb.sb_qflags = 0;
1468 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1470 "Old superblock version %x, converting to %x.",
1471 oldv, mp->m_sb.sb_versionnum);
1474 if (flags & XFS_QMOPT_UQUOTA)
1475 mp->m_sb.sb_uquotino = (*ip)->i_ino;
1477 mp->m_sb.sb_gquotino = (*ip)->i_ino;
1478 XFS_SB_UNLOCK(mp, s);
1479 xfs_mod_sb(tp, sbfields);
1481 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
1483 xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
1491 xfs_qm_reset_dqcounts(
1497 xfs_disk_dquot_t *ddq;
1500 xfs_buftrace("RESET DQUOTS", bp);
1502 * Reset all counters and timers. They'll be
1503 * started afresh by xfs_qm_quotacheck.
1506 j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1507 do_div(j, sizeof(xfs_dqblk_t));
1508 ASSERT(XFS_QM_DQPERBLK(mp) == j);
1510 ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp);
1511 for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) {
1513 * Do a sanity check, and if needed, repair the dqblk. Don't
1514 * output any warnings because it's perfectly possible to
1515 * find unitialized dquot blks. See comment in xfs_qm_dqcheck.
1517 (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR,
1519 INT_SET(ddq->d_bcount, ARCH_CONVERT, 0ULL);
1520 INT_SET(ddq->d_icount, ARCH_CONVERT, 0ULL);
1521 INT_SET(ddq->d_rtbcount, ARCH_CONVERT, 0ULL);
1522 INT_SET(ddq->d_btimer, ARCH_CONVERT, (time_t)0);
1523 INT_SET(ddq->d_itimer, ARCH_CONVERT, (time_t)0);
1524 INT_SET(ddq->d_bwarns, ARCH_CONVERT, 0UL);
1525 INT_SET(ddq->d_iwarns, ARCH_CONVERT, 0UL);
1526 ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
1537 xfs_filblks_t blkcnt,
1547 incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
1548 XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
1552 * Blkcnt arg can be a very big number, and might even be
1553 * larger than the log itself. So, we have to break it up into
1554 * manageable-sized transactions.
1555 * Note that we don't start a permanent transaction here; we might
1556 * not be able to get a log reservation for the whole thing up front,
1557 * and we don't really care to either, because we just discard
1558 * everything if we were to crash in the middle of this loop.
1561 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1562 XFS_FSB_TO_DADDR(mp, bno),
1563 (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp);
1567 (void) xfs_qm_reset_dqcounts(mp, bp, firstid,
1568 flags & XFS_QMOPT_UQUOTA ?
1569 XFS_DQ_USER : XFS_DQ_GROUP);
1570 xfs_bdwrite(mp, bp);
1572 * goto the next block.
1575 firstid += XFS_QM_DQPERBLK(mp);
1581 * Iterate over all allocated USR/GRP dquots in the system, calling a
1582 * caller supplied function for every chunk of dquots that we find.
1590 xfs_bmbt_irec_t *map;
1591 int i, nmaps; /* number of map entries */
1592 int error; /* return value */
1593 xfs_fileoff_t lblkno;
1594 xfs_filblks_t maxlblkcnt;
1596 xfs_fsblock_t rablkno;
1597 xfs_filblks_t rablkcnt;
1601 * This looks racey, but we can't keep an inode lock across a
1602 * trans_reserve. But, this gets called during quotacheck, and that
1603 * happens only at mount time which is single threaded.
1605 if (qip->i_d.di_nblocks == 0)
1608 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
1611 maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1613 nmaps = XFS_DQITER_MAP_SIZE;
1615 * We aren't changing the inode itself. Just changing
1616 * some of its data. No new blocks are added here, and
1617 * the inode is never added to the transaction.
1619 xfs_ilock(qip, XFS_ILOCK_SHARED);
1620 error = xfs_bmapi(NULL, qip, lblkno,
1621 maxlblkcnt - lblkno,
1624 0, map, &nmaps, NULL);
1625 xfs_iunlock(qip, XFS_ILOCK_SHARED);
1629 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1630 for (i = 0; i < nmaps; i++) {
1631 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1632 ASSERT(map[i].br_blockcount);
1635 lblkno += map[i].br_blockcount;
1637 if (map[i].br_startblock == HOLESTARTBLOCK)
1640 firstid = (xfs_dqid_t) map[i].br_startoff *
1641 XFS_QM_DQPERBLK(mp);
1643 * Do a read-ahead on the next extent.
1645 if ((i+1 < nmaps) &&
1646 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1647 rablkcnt = map[i+1].br_blockcount;
1648 rablkno = map[i+1].br_startblock;
1649 while (rablkcnt--) {
1650 xfs_baread(mp->m_ddev_targp,
1651 XFS_FSB_TO_DADDR(mp, rablkno),
1652 (int)XFS_QI_DQCHUNKLEN(mp));
1657 * Iterate thru all the blks in the extent and
1658 * reset the counters of all the dquots inside them.
1660 if ((error = xfs_qm_dqiter_bufs(mp,
1662 map[i].br_startblock,
1663 map[i].br_blockcount,
1671 } while (nmaps > 0);
1673 kmem_free(map, XFS_DQITER_MAP_SIZE * sizeof(*map));
1679 * Called by dqusage_adjust in doing a quotacheck.
1680 * Given the inode, and a dquot (either USR or GRP, doesn't matter),
1681 * this updates its incore copy as well as the buffer copy. This is
1682 * so that once the quotacheck is done, we can just log all the buffers,
1683 * as opposed to logging numerous updates to individual dquots.
1686 xfs_qm_quotacheck_dqadjust(
1691 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1692 xfs_dqtrace_entry(dqp, "QCHECK DQADJUST");
1694 * Adjust the inode count and the block count to reflect this inode's
1697 INT_MOD(dqp->q_core.d_icount, ARCH_CONVERT, +1);
1698 dqp->q_res_icount++;
1700 INT_MOD(dqp->q_core.d_bcount, ARCH_CONVERT, nblks);
1701 dqp->q_res_bcount += nblks;
1704 INT_MOD(dqp->q_core.d_rtbcount, ARCH_CONVERT, rtblks);
1705 dqp->q_res_rtbcount += rtblks;
1709 * Set default limits, adjust timers (since we changed usages)
1711 if (! XFS_IS_SUSER_DQUOT(dqp)) {
1712 xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
1713 xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
1716 dqp->dq_flags |= XFS_DQ_DIRTY;
1722 xfs_qcnt_t *O_rtblks)
1724 xfs_filblks_t rtblks; /* total rt blks */
1725 xfs_ifork_t *ifp; /* inode fork pointer */
1726 xfs_extnum_t nextents; /* number of extent entries */
1727 xfs_bmbt_rec_t *base; /* base of extent array */
1728 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
1731 ASSERT(XFS_IS_REALTIME_INODE(ip));
1732 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1733 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1734 if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
1738 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1739 base = &ifp->if_u1.if_extents[0];
1740 for (ep = base; ep < &base[nextents]; ep++)
1741 rtblks += xfs_bmbt_get_blockcount(ep);
1742 *O_rtblks = (xfs_qcnt_t)rtblks;
1747 * callback routine supplied to bulkstat(). Given an inumber, find its
1748 * dquots and update them to account for resources taken by that inode.
1752 xfs_qm_dqusage_adjust(
1753 xfs_mount_t *mp, /* mount point for filesystem */
1754 xfs_ino_t ino, /* inode number to get data for */
1755 void __user *buffer, /* not used */
1756 int ubsize, /* not used */
1757 void *private_data, /* not used */
1758 xfs_daddr_t bno, /* starting block of inode cluster */
1759 int *ubused, /* not used */
1760 void *dip, /* on-disk inode pointer (not used) */
1761 int *res) /* result code value */
1764 xfs_dquot_t *udqp, *gdqp;
1765 xfs_qcnt_t nblks, rtblks;
1768 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1771 * rootino must have its resources accounted for, not so with the quota
1774 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1775 *res = BULKSTAT_RV_NOTHING;
1776 return XFS_ERROR(EINVAL);
1780 * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
1781 * interface expects the inode to be exclusively locked because that's
1782 * the case in all other instances. It's OK that we do this because
1783 * quotacheck is done only at mount time.
1785 if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
1786 *res = BULKSTAT_RV_NOTHING;
1790 if (ip->i_d.di_mode == 0) {
1791 xfs_iput_new(ip, XFS_ILOCK_EXCL);
1792 *res = BULKSTAT_RV_NOTHING;
1793 return XFS_ERROR(ENOENT);
1797 * Obtain the locked dquots. In case of an error (eg. allocation
1798 * fails for ENOSPC), we return the negative of the error number
1799 * to bulkstat, so that it can get propagated to quotacheck() and
1800 * making us disable quotas for the file system.
1802 if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
1803 xfs_iput(ip, XFS_ILOCK_EXCL);
1804 *res = BULKSTAT_RV_GIVEUP;
1809 if (! XFS_IS_REALTIME_INODE(ip)) {
1810 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks;
1813 * Walk thru the extent list and count the realtime blocks.
1815 if ((error = xfs_qm_get_rtblks(ip, &rtblks))) {
1816 xfs_iput(ip, XFS_ILOCK_EXCL);
1821 *res = BULKSTAT_RV_GIVEUP;
1824 nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
1826 ASSERT(ip->i_delayed_blks == 0);
1829 * We can't release the inode while holding its dquot locks.
1830 * The inode can go into inactive and might try to acquire the dquotlocks.
1831 * So, just unlock here and do a vn_rele at the end.
1833 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1836 * Add the (disk blocks and inode) resources occupied by this
1837 * inode to its dquots. We do this adjustment in the incore dquot,
1838 * and also copy the changes to its buffer.
1839 * We don't care about putting these changes in a transaction
1840 * envelope because if we crash in the middle of a 'quotacheck'
1841 * we have to start from the beginning anyway.
1842 * Once we're done, we'll log all the dquot bufs.
1844 * The *QUOTA_ON checks below may look pretty racey, but quotachecks
1845 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1847 if (XFS_IS_UQUOTA_ON(mp)) {
1849 xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
1852 if (XFS_IS_GQUOTA_ON(mp)) {
1854 xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
1858 * Now release the inode. This will send it to 'inactive', and
1859 * possibly even free blocks.
1861 VN_RELE(XFS_ITOV(ip));
1866 *res = BULKSTAT_RV_DIDONE;
1871 * Walk thru all the filesystem inodes and construct a consistent view
1872 * of the disk quota world. If the quotacheck fails, disable quotas.
1878 int done, count, error;
1881 xfs_inode_t *uip, *gip;
1889 ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp));
1890 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1893 * There should be no cached dquots. The (simplistic) quotacheck
1894 * algorithm doesn't like that.
1896 ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0);
1898 cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
1901 * First we go thru all the dquots on disk, USR and GRP, and reset
1902 * their counters to zero. We need a clean slate.
1903 * We don't log our changes till later.
1905 if ((uip = XFS_QI_UQIP(mp))) {
1906 if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA)))
1908 flags |= XFS_UQUOTA_CHKD;
1911 if ((gip = XFS_QI_GQIP(mp))) {
1912 if ((error = xfs_qm_dqiterate(mp, gip, XFS_QMOPT_GQUOTA)))
1914 flags |= XFS_GQUOTA_CHKD;
1919 * Iterate thru all the inodes in the file system,
1920 * adjusting the corresponding dquot counters in core.
1922 if ((error = xfs_bulkstat(mp, &lastino, &count,
1923 xfs_qm_dqusage_adjust, NULL,
1925 BULKSTAT_FG_IGET|BULKSTAT_FG_VFSLOCKED,
1932 * We can get this error if we couldn't do a dquot allocation inside
1933 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1934 * dirty dquots that might be cached, we just want to get rid of them
1935 * and turn quotaoff. The dquots won't be attached to any of the inodes
1936 * at this point (because we intentionally didn't in dqget_noattach).
1939 xfs_qm_dqpurge_all(mp,
1940 XFS_QMOPT_UQUOTA|XFS_QMOPT_GQUOTA|
1941 XFS_QMOPT_QUOTAOFF);
1945 * We've made all the changes that we need to make incore.
1946 * Now flush_them down to disk buffers.
1948 xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
1951 * We didn't log anything, because if we crashed, we'll have to
1952 * start the quotacheck from scratch anyway. However, we must make
1953 * sure that our dquot changes are secure before we put the
1954 * quotacheck'd stamp on the superblock. So, here we do a synchronous
1957 XFS_bflush(mp->m_ddev_targp);
1960 * If one type of quotas is off, then it will lose its
1961 * quotachecked status, since we won't be doing accounting for
1962 * that type anymore.
1964 mp->m_qflags &= ~(XFS_GQUOTA_CHKD | XFS_UQUOTA_CHKD);
1965 mp->m_qflags |= flags;
1967 XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
1971 cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
1972 "Disabling quotas.",
1973 mp->m_fsname, error);
1975 * We must turn off quotas.
1977 ASSERT(mp->m_quotainfo != NULL);
1978 ASSERT(xfs_Gqm != NULL);
1979 xfs_qm_destroy_quotainfo(mp);
1980 xfs_mount_reset_sbqflags(mp);
1982 cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
1988 * This is called after the superblock has been read in and we're ready to
1989 * iget the quota inodes.
1992 xfs_qm_init_quotainos(
1995 xfs_inode_t *uip, *gip;
2000 ASSERT(mp->m_quotainfo);
2006 * Get the uquota and gquota inodes
2008 if (XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
2009 if (XFS_IS_UQUOTA_ON(mp) &&
2010 mp->m_sb.sb_uquotino != NULLFSINO) {
2011 ASSERT(mp->m_sb.sb_uquotino > 0);
2012 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
2014 return XFS_ERROR(error);
2016 if (XFS_IS_GQUOTA_ON(mp) &&
2017 mp->m_sb.sb_gquotino != NULLFSINO) {
2018 ASSERT(mp->m_sb.sb_gquotino > 0);
2019 if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
2022 VN_RELE(XFS_ITOV(uip));
2023 return XFS_ERROR(error);
2027 flags |= XFS_QMOPT_SBVERSION;
2028 sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
2029 XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
2033 * Create the two inodes, if they don't exist already. The changes
2034 * made above will get added to a transaction and logged in one of
2035 * the qino_alloc calls below. If the device is readonly,
2036 * temporarily switch to read-write to do this.
2038 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
2039 if ((error = xfs_qm_qino_alloc(mp, &uip,
2040 sbflags | XFS_SB_UQUOTINO,
2041 flags | XFS_QMOPT_UQUOTA)))
2042 return XFS_ERROR(error);
2044 flags &= ~XFS_QMOPT_SBVERSION;
2046 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
2047 if ((error = xfs_qm_qino_alloc(mp, &gip,
2048 sbflags | XFS_SB_GQUOTINO,
2049 flags | XFS_QMOPT_GQUOTA))) {
2051 VN_RELE(XFS_ITOV(uip));
2053 return XFS_ERROR(error);
2057 XFS_QI_UQIP(mp) = uip;
2058 XFS_QI_GQIP(mp) = gip;
2065 * Traverse the freelist of dquots and attempt to reclaim a maximum of
2066 * 'howmany' dquots. This operation races with dqlookup(), and attempts to
2067 * favor the lookup function ...
2068 * XXXsup merge this with qm_reclaim_one().
2071 xfs_qm_shake_freelist(
2076 xfs_dquot_t *dqp, *nextdqp;
2088 cmn_err(CE_DEBUG, "Shake free 0x%x", howmany);
2090 /* lock order is : hashchainlock, freelistlock, mplistlock */
2092 xfs_qm_freelist_lock(xfs_Gqm);
2094 for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
2095 ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) &&
2096 nreclaimed < howmany); ) {
2100 * We are racing with dqlookup here. Naturally we don't
2101 * want to reclaim a dquot that lookup wants.
2103 if (dqp->dq_flags & XFS_DQ_WANT) {
2105 xfs_qm_freelist_unlock(xfs_Gqm);
2106 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
2107 return (nreclaimed);
2108 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
2113 * If the dquot is inactive, we are assured that it is
2114 * not on the mplist or the hashlist, and that makes our
2117 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
2118 ASSERT(dqp->q_mount == NULL);
2119 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
2120 ASSERT(dqp->HL_PREVP == NULL);
2121 ASSERT(dqp->MPL_PREVP == NULL);
2122 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
2123 nextdqp = dqp->dq_flnext;
2127 ASSERT(dqp->MPL_PREVP);
2129 * Try to grab the flush lock. If this dquot is in the process of
2130 * getting flushed to disk, we don't want to reclaim it.
2132 if (! xfs_qm_dqflock_nowait(dqp)) {
2134 dqp = dqp->dq_flnext;
2139 * We have the flush lock so we know that this is not in the
2140 * process of being flushed. So, if this is dirty, flush it
2141 * DELWRI so that we don't get a freelist infested with
2144 if (XFS_DQ_IS_DIRTY(dqp)) {
2145 xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY");
2147 * We flush it delayed write, so don't bother
2148 * releasing the mplock.
2150 (void) xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
2151 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2152 dqp = dqp->dq_flnext;
2156 * We're trying to get the hashlock out of order. This races
2157 * with dqlookup; so, we giveup and goto the next dquot if
2158 * we couldn't get the hashlock. This way, we won't starve
2159 * a dqlookup process that holds the hashlock that is
2160 * waiting for the freelist lock.
2162 if (! xfs_qm_dqhashlock_nowait(dqp)) {
2165 dqp = dqp->dq_flnext;
2169 * This races with dquot allocation code as well as dqflush_all
2170 * and reclaim code. So, if we failed to grab the mplist lock,
2171 * giveup everything and start over.
2175 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2176 /* XXX put a sentinel so that we can come back here */
2179 XFS_DQ_HASH_UNLOCK(hash);
2180 xfs_qm_freelist_unlock(xfs_Gqm);
2181 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
2182 return (nreclaimed);
2185 xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING");
2187 cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
2188 dqp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
2190 ASSERT(dqp->q_nrefs == 0);
2191 nextdqp = dqp->dq_flnext;
2192 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2193 XQM_HASHLIST_REMOVE(hash, dqp);
2195 xfs_qm_mplist_unlock(dqp->q_mount);
2196 XFS_DQ_HASH_UNLOCK(hash);
2199 XQM_FREELIST_REMOVE(dqp);
2202 XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims);
2203 xfs_qm_dqdestroy(dqp);
2206 xfs_qm_freelist_unlock(xfs_Gqm);
2207 return (nreclaimed);
2212 * The kmem_shake interface is invoked when memory is running low.
2216 xfs_qm_shake(int nr_to_scan, unsigned int gfp_mask)
2218 int ndqused, nfree, n;
2220 if (!kmem_shake_allow(gfp_mask))
2225 nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
2226 /* incore dquots in all f/s's */
2227 ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
2229 ASSERT(ndqused >= 0);
2231 if (nfree <= ndqused && nfree < ndquot)
2234 ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
2235 n = nfree - ndqused - ndquot; /* # over target */
2237 return xfs_qm_shake_freelist(MAX(nfree, n));
2242 * Just pop the least recently used dquot off the freelist and
2243 * recycle it. The returned dquot is locked.
2245 STATIC xfs_dquot_t *
2246 xfs_qm_dqreclaim_one(void)
2248 xfs_dquot_t *dqpout;
2257 /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
2259 xfs_qm_freelist_lock(xfs_Gqm);
2261 FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) {
2265 * We are racing with dqlookup here. Naturally we don't
2266 * want to reclaim a dquot that lookup wants. We release the
2267 * freelist lock and start over, so that lookup will grab
2268 * both the dquot and the freelistlock.
2270 if (dqp->dq_flags & XFS_DQ_WANT) {
2271 ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
2272 xfs_dqtrace_entry(dqp, "DQRECLAIM: DQWANT");
2274 xfs_qm_freelist_unlock(xfs_Gqm);
2275 if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
2277 XQM_STATS_INC(xqmstats.xs_qm_dqwants);
2282 * If the dquot is inactive, we are assured that it is
2283 * not on the mplist or the hashlist, and that makes our
2286 if (dqp->dq_flags & XFS_DQ_INACTIVE) {
2287 ASSERT(dqp->q_mount == NULL);
2288 ASSERT(! XFS_DQ_IS_DIRTY(dqp));
2289 ASSERT(dqp->HL_PREVP == NULL);
2290 ASSERT(dqp->MPL_PREVP == NULL);
2291 XQM_FREELIST_REMOVE(dqp);
2294 XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
2298 ASSERT(dqp->q_hash);
2299 ASSERT(dqp->MPL_PREVP);
2302 * Try to grab the flush lock. If this dquot is in the process of
2303 * getting flushed to disk, we don't want to reclaim it.
2305 if (! xfs_qm_dqflock_nowait(dqp)) {
2311 * We have the flush lock so we know that this is not in the
2312 * process of being flushed. So, if this is dirty, flush it
2313 * DELWRI so that we don't get a freelist infested with
2316 if (XFS_DQ_IS_DIRTY(dqp)) {
2317 xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY");
2319 * We flush it delayed write, so don't bother
2320 * releasing the freelist lock.
2322 (void) xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
2323 xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
2327 if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
2333 if (! xfs_qm_dqhashlock_nowait(dqp))
2336 ASSERT(dqp->q_nrefs == 0);
2337 xfs_dqtrace_entry(dqp, "DQRECLAIM: UNLINKING");
2338 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
2339 XQM_HASHLIST_REMOVE(dqp->q_hash, dqp);
2340 XQM_FREELIST_REMOVE(dqp);
2342 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
2344 xfs_qm_mplist_unlock(dqp->q_mount);
2351 xfs_qm_freelist_unlock(xfs_Gqm);
2356 /*------------------------------------------------------------------*/
2359 * Return a new incore dquot. Depending on the number of
2360 * dquots in the system, we either allocate a new one on the kernel heap,
2361 * or reclaim a free one.
2362 * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
2363 * to reclaim an existing one from the freelist.
2366 xfs_qm_dqalloc_incore(
2367 xfs_dquot_t **O_dqpp)
2372 * Check against high water mark to see if we want to pop
2373 * a nincompoop dquot off the freelist.
2375 if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
2377 * Try to recycle a dquot from the freelist.
2379 if ((dqp = xfs_qm_dqreclaim_one())) {
2380 XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
2382 * Just zero the core here. The rest will get
2383 * reinitialized by caller. XXX we shouldn't even
2386 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
2390 XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
2394 * Allocate a brand new dquot on the kernel heap and return it
2395 * to the caller to initialize.
2397 ASSERT(xfs_Gqm->qm_dqzone != NULL);
2398 *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
2399 atomic_inc(&xfs_Gqm->qm_totaldquots);
2406 * Start a transaction and write the incore superblock changes to
2407 * disk. flags parameter indicates which fields have changed.
2410 xfs_qm_write_sb_changes(
2418 cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname);
2420 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
2421 if ((error = xfs_trans_reserve(tp, 0,
2422 mp->m_sb.sb_sectsize + 128, 0,
2424 XFS_DEFAULT_LOG_COUNT))) {
2425 xfs_trans_cancel(tp, 0);
2429 xfs_mod_sb(tp, flags);
2430 (void) xfs_trans_commit(tp, 0, NULL);
2436 /* --------------- utility functions for vnodeops ---------------- */
2440 * Given an inode, a uid and gid (from cred_t) make sure that we have
2441 * allocated relevant dquot(s) on disk, and that we won't exceed inode
2442 * quotas by creating this file.
2443 * This also attaches dquot(s) to the given inode after locking it,
2444 * and returns the dquots corresponding to the uid and/or gid.
2446 * in : inode (unlocked)
2447 * out : udquot, gdquot with references taken and unlocked
2456 xfs_dquot_t **O_udqpp,
2457 xfs_dquot_t **O_gdqpp)
2460 xfs_dquot_t *uq, *gq;
2463 if (!XFS_IS_QUOTA_ON(mp))
2466 lockflags = XFS_ILOCK_EXCL;
2467 xfs_ilock(ip, lockflags);
2469 if ((flags & XFS_QMOPT_INHERIT) &&
2470 XFS_INHERIT_GID(ip, XFS_MTOVFS(mp)))
2471 gid = ip->i_d.di_gid;
2474 * Attach the dquot(s) to this inode, doing a dquot allocation
2475 * if necessary. The dquot(s) will not be locked.
2477 if (XFS_NOT_DQATTACHED(mp, ip)) {
2478 if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
2479 XFS_QMOPT_ILOCKED))) {
2480 xfs_iunlock(ip, lockflags);
2486 if ((flags & XFS_QMOPT_UQUOTA) &&
2487 XFS_IS_UQUOTA_ON(mp)) {
2488 if (ip->i_d.di_uid != uid) {
2490 * What we need is the dquot that has this uid, and
2491 * if we send the inode to dqget, the uid of the inode
2492 * takes priority over what's sent in the uid argument.
2493 * We must unlock inode here before calling dqget if
2494 * we're not sending the inode, because otherwise
2495 * we'll deadlock by doing trans_reserve while
2498 xfs_iunlock(ip, lockflags);
2499 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
2504 ASSERT(error != ENOENT);
2508 * Get the ilock in the right order.
2511 lockflags = XFS_ILOCK_SHARED;
2512 xfs_ilock(ip, lockflags);
2515 * Take an extra reference, because we'll return
2518 ASSERT(ip->i_udquot);
2525 if ((flags & XFS_QMOPT_GQUOTA) &&
2526 XFS_IS_GQUOTA_ON(mp)) {
2527 if (ip->i_d.di_gid != gid) {
2528 xfs_iunlock(ip, lockflags);
2529 if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
2536 ASSERT(error != ENOENT);
2540 lockflags = XFS_ILOCK_SHARED;
2541 xfs_ilock(ip, lockflags);
2543 ASSERT(ip->i_gdquot);
2551 xfs_dqtrace_entry_ino(uq, "DQALLOC", ip);
2553 xfs_iunlock(ip, lockflags);
2566 * Actually transfer ownership, and do dquot modifications.
2567 * These were already reserved.
2573 xfs_dquot_t **IO_olddq,
2576 xfs_dquot_t *prevdq;
2577 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
2578 ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
2583 ASSERT(prevdq != newdq);
2585 xfs_trans_mod_dquot(tp, prevdq,
2586 XFS_TRANS_DQ_BCOUNT,
2587 -(ip->i_d.di_nblocks));
2588 xfs_trans_mod_dquot(tp, prevdq,
2589 XFS_TRANS_DQ_ICOUNT,
2592 /* the sparkling new dquot */
2593 xfs_trans_mod_dquot(tp, newdq,
2594 XFS_TRANS_DQ_BCOUNT,
2595 ip->i_d.di_nblocks);
2596 xfs_trans_mod_dquot(tp, newdq,
2597 XFS_TRANS_DQ_ICOUNT,
2601 * Take an extra reference, because the inode
2602 * is going to keep this dquot pointer even
2603 * after the trans_commit.
2607 xfs_dqunlock(newdq);
2614 * Quota reservations for setattr(AT_UID|AT_GID).
2617 xfs_qm_vop_chown_reserve(
2627 xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
2629 ASSERT(XFS_ISLOCKED_INODE(ip));
2631 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
2633 delblks = ip->i_delayed_blks;
2634 delblksudq = delblksgdq = unresudq = unresgdq = NULL;
2636 if (XFS_IS_UQUOTA_ON(mp) && udqp &&
2637 ip->i_d.di_uid != (uid_t)INT_GET(udqp->q_core.d_id, ARCH_CONVERT)) {
2640 * If there are delayed allocation blocks, then we have to
2641 * unreserve those from the old dquot, and add them to the
2645 ASSERT(ip->i_udquot);
2646 unresudq = ip->i_udquot;
2649 if (XFS_IS_GQUOTA_ON(ip->i_mount) && gdqp &&
2650 ip->i_d.di_gid != INT_GET(gdqp->q_core.d_id, ARCH_CONVERT)) {
2653 ASSERT(ip->i_gdquot);
2654 unresgdq = ip->i_gdquot;
2658 if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
2659 delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
2660 flags | XFS_QMOPT_RES_REGBLKS)))
2664 * Do the delayed blks reservations/unreservations now. Since, these
2665 * are done without the help of a transaction, if a reservation fails
2666 * its previous reservations won't be automatically undone by trans
2667 * code. So, we have to do it manually here.
2671 * Do the reservations first. Unreservation can't fail.
2673 ASSERT(delblksudq || delblksgdq);
2674 ASSERT(unresudq || unresgdq);
2675 if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2676 delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
2677 flags | XFS_QMOPT_RES_REGBLKS)))
2679 xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
2680 unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
2681 XFS_QMOPT_RES_REGBLKS);
2688 xfs_qm_vop_rename_dqattach(
2689 xfs_inode_t **i_tab)
2697 if (! XFS_IS_QUOTA_ON(ip->i_mount))
2700 if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
2701 error = xfs_qm_dqattach(ip, 0);
2705 for (i = 1; (i < 4 && i_tab[i]); i++) {
2707 * Watch out for duplicate entries in the table.
2709 if ((ip = i_tab[i]) != i_tab[i-1]) {
2710 if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
2711 error = xfs_qm_dqattach(ip, 0);
2721 xfs_qm_vop_dqattach_and_dqmod_newinode(
2727 if (!XFS_IS_QUOTA_ON(tp->t_mountp))
2730 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
2731 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
2737 ASSERT(ip->i_udquot == NULL);
2738 ip->i_udquot = udqp;
2739 ASSERT(ip->i_d.di_uid == INT_GET(udqp->q_core.d_id, ARCH_CONVERT));
2740 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
2746 ASSERT(ip->i_gdquot == NULL);
2747 ip->i_gdquot = gdqp;
2748 ASSERT(ip->i_d.di_gid == INT_GET(gdqp->q_core.d_id, ARCH_CONVERT));
2749 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
2753 /* ------------- list stuff -----------------*/
2755 xfs_qm_freelist_init(xfs_frlist_t *ql)
2757 ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
2758 mutex_init(&ql->qh_lock, MUTEX_DEFAULT, "dqf");
2764 xfs_qm_freelist_destroy(xfs_frlist_t *ql)
2766 xfs_dquot_t *dqp, *nextdqp;
2768 mutex_lock(&ql->qh_lock, PINOD);
2769 for (dqp = ql->qh_next;
2770 dqp != (xfs_dquot_t *)ql; ) {
2772 nextdqp = dqp->dq_flnext;
2774 cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
2776 XQM_FREELIST_REMOVE(dqp);
2778 xfs_qm_dqdestroy(dqp);
2782 * Don't bother about unlocking.
2784 mutex_destroy(&ql->qh_lock);
2786 ASSERT(ql->qh_nelems == 0);
2790 xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
2792 dq->dq_flnext = ql->qh_next;
2793 dq->dq_flprev = (xfs_dquot_t *)ql;
2795 dq->dq_flnext->dq_flprev = dq;
2796 xfs_Gqm->qm_dqfreelist.qh_nelems++;
2797 xfs_Gqm->qm_dqfreelist.qh_version++;
2801 xfs_qm_freelist_unlink(xfs_dquot_t *dq)
2803 xfs_dquot_t *next = dq->dq_flnext;
2804 xfs_dquot_t *prev = dq->dq_flprev;
2806 next->dq_flprev = prev;
2807 prev->dq_flnext = next;
2808 dq->dq_flnext = dq->dq_flprev = dq;
2809 xfs_Gqm->qm_dqfreelist.qh_nelems--;
2810 xfs_Gqm->qm_dqfreelist.qh_version++;
2814 xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
2816 xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
2820 xfs_qm_dqhashlock_nowait(
2825 locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
2830 xfs_qm_freelist_lock_nowait(
2835 locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
2840 xfs_qm_mplist_nowait(
2845 ASSERT(mp->m_quotainfo);
2846 locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));