2 * Copyright (c) 1982, 1986, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/mutex.h>
48 #include <sys/namei.h>
51 #include <sys/socket.h>
53 #include <sys/sysctl.h>
54 #include <sys/vnode.h>
56 #include <ufs/ufs/extattr.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/inode.h>
59 #include <ufs/ufs/ufsmount.h>
60 #include <ufs/ufs/ufs_extern.h>
62 static int unprivileged_get_quota = 0;
63 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
64 &unprivileged_get_quota, 0,
65 "Unprivileged processes may retrieve quotas for other uids and gids");
67 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
70 * Quota name to error message mapping.
72 static char *quotatypes[] = INITQFNAMES;
74 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
75 static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
76 static int dqget(struct vnode *,
77 u_long, struct ufsmount *, int, struct dquot **);
78 static int dqsync(struct vnode *, struct dquot *);
79 static void dqflush(struct vnode *);
80 static int quotaoff1(struct thread *td, struct mount *mp, int type);
81 static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
84 static void dqref(struct dquot *);
85 static void chkdquot(struct inode *);
89 * Set up the quotas for an inode.
91 * This routine completely defines the semantics of quotas.
92 * If other criterion want to be used to establish quotas, the
93 * MAXQUOTAS value in quotas.h should be increased, and the
94 * additional dquots set up here.
100 struct ufsmount *ump;
107 * Disk quotas must be turned off for system files. Currently
108 * snapshot and quota files.
110 if ((vp->v_vflag & VV_SYSTEM) != 0)
113 * XXX: Turn off quotas for files with a negative UID or GID.
114 * This prevents the creation of 100GB+ quota files.
116 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
118 ump = VFSTOUFS(vp->v_mount);
120 * Set up the user quota based on file uid.
121 * EINVAL means that quotas are not enabled.
124 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
128 * Set up the group quota based on file gid.
129 * EINVAL means that quotas are not enabled.
132 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
139 * Update disk usage, and take corrective action.
142 chkdq(ip, change, cred, flags)
149 ufs2_daddr_t ncurblocks;
150 struct vnode *vp = ITOV(ip);
151 int i, error, warn, do_check;
154 * Disk quotas must be turned off for system files. Currently
155 * snapshot and quota files.
157 if ((vp->v_vflag & VV_SYSTEM) != 0)
160 * XXX: Turn off quotas for files with a negative UID or GID.
161 * This prevents the creation of 100GB+ quota files.
163 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
166 if ((flags & CHOWN) == 0)
172 for (i = 0; i < MAXQUOTAS; i++) {
173 if ((dq = ip->i_dquot[i]) == NODQUOT)
176 DQI_WAIT(dq, PINOD+1, "chkdq1");
177 ncurblocks = dq->dq_curblocks + change;
179 dq->dq_curblocks = ncurblocks;
181 dq->dq_curblocks = 0;
182 dq->dq_flags &= ~DQ_BLKS;
183 dq->dq_flags |= DQ_MOD;
188 if ((flags & FORCE) == 0 &&
189 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
193 for (i = 0; i < MAXQUOTAS; i++) {
194 if ((dq = ip->i_dquot[i]) == NODQUOT)
198 DQI_WAIT(dq, PINOD+1, "chkdq2");
200 error = chkdqchg(ip, change, cred, i, &warn);
203 * Roll back user quota changes when
204 * group quota failed.
212 DQI_WAIT(dq, PINOD+1, "chkdq3");
213 ncurblocks = dq->dq_curblocks - change;
215 dq->dq_curblocks = ncurblocks;
217 dq->dq_curblocks = 0;
218 dq->dq_flags &= ~DQ_BLKS;
219 dq->dq_flags |= DQ_MOD;
225 /* Reset timer when crossing soft limit */
226 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
227 dq->dq_curblocks < dq->dq_bsoftlimit)
228 dq->dq_btime = time_second +
229 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
230 dq->dq_curblocks += change;
231 dq->dq_flags |= DQ_MOD;
234 uprintf("\n%s: warning, %s %s\n",
235 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
236 quotatypes[i], "disk quota exceeded");
242 * Check for a valid change to a users allocation.
243 * Issue an error message if appropriate.
246 chkdqchg(ip, change, cred, type, warn)
253 struct dquot *dq = ip->i_dquot[type];
254 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
257 * If user would exceed their hard limit, disallow space allocation.
259 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
260 if ((dq->dq_flags & DQ_BLKS) == 0 &&
261 ip->i_uid == cred->cr_uid) {
262 dq->dq_flags |= DQ_BLKS;
264 uprintf("\n%s: write failed, %s disk limit reached\n",
265 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
273 * If user is over their soft limit for too long, disallow space
274 * allocation. Reset time limit as they cross their soft limit.
276 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
277 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
278 dq->dq_btime = time_second +
279 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
280 if (ip->i_uid == cred->cr_uid)
284 if (time_second > dq->dq_btime) {
285 if ((dq->dq_flags & DQ_BLKS) == 0 &&
286 ip->i_uid == cred->cr_uid) {
287 dq->dq_flags |= DQ_BLKS;
289 uprintf("\n%s: write failed, %s %s\n",
290 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
292 "disk quota exceeded for too long");
303 * Check the inode limit, applying corrective action.
306 chkiq(ip, change, cred, flags)
314 int i, error, warn, do_check;
317 if ((flags & CHOWN) == 0)
323 for (i = 0; i < MAXQUOTAS; i++) {
324 if ((dq = ip->i_dquot[i]) == NODQUOT)
327 DQI_WAIT(dq, PINOD+1, "chkiq1");
328 ncurinodes = dq->dq_curinodes + change;
329 /* XXX: ncurinodes is unsigned */
330 if (dq->dq_curinodes != 0 && ncurinodes >= 0)
331 dq->dq_curinodes = ncurinodes;
333 dq->dq_curinodes = 0;
334 dq->dq_flags &= ~DQ_INODS;
335 dq->dq_flags |= DQ_MOD;
340 if ((flags & FORCE) == 0 &&
341 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
345 for (i = 0; i < MAXQUOTAS; i++) {
346 if ((dq = ip->i_dquot[i]) == NODQUOT)
350 DQI_WAIT(dq, PINOD+1, "chkiq2");
352 error = chkiqchg(ip, change, cred, i, &warn);
355 * Roll back user quota changes when
356 * group quota failed.
364 DQI_WAIT(dq, PINOD+1, "chkiq3");
365 ncurinodes = dq->dq_curinodes - change;
366 /* XXX: ncurinodes is unsigned */
367 if (dq->dq_curinodes != 0 &&
369 dq->dq_curinodes = ncurinodes;
371 dq->dq_curinodes = 0;
372 dq->dq_flags &= ~DQ_INODS;
373 dq->dq_flags |= DQ_MOD;
379 /* Reset timer when crossing soft limit */
380 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
381 dq->dq_curinodes < dq->dq_isoftlimit)
382 dq->dq_itime = time_second +
383 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
384 dq->dq_curinodes += change;
385 dq->dq_flags |= DQ_MOD;
388 uprintf("\n%s: warning, %s %s\n",
389 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
390 quotatypes[i], "inode quota exceeded");
396 * Check for a valid change to a users allocation.
397 * Issue an error message if appropriate.
400 chkiqchg(ip, change, cred, type, warn)
407 struct dquot *dq = ip->i_dquot[type];
408 ino_t ncurinodes = dq->dq_curinodes + change;
411 * If user would exceed their hard limit, disallow inode allocation.
413 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
414 if ((dq->dq_flags & DQ_INODS) == 0 &&
415 ip->i_uid == cred->cr_uid) {
416 dq->dq_flags |= DQ_INODS;
418 uprintf("\n%s: write failed, %s inode limit reached\n",
419 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
427 * If user is over their soft limit for too long, disallow inode
428 * allocation. Reset time limit as they cross their soft limit.
430 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
431 if (dq->dq_curinodes < dq->dq_isoftlimit) {
432 dq->dq_itime = time_second +
433 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
434 if (ip->i_uid == cred->cr_uid)
438 if (time_second > dq->dq_itime) {
439 if ((dq->dq_flags & DQ_INODS) == 0 &&
440 ip->i_uid == cred->cr_uid) {
441 dq->dq_flags |= DQ_INODS;
443 uprintf("\n%s: write failed, %s %s\n",
444 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
446 "inode quota exceeded for too long");
458 * On filesystems with quotas enabled, it is an error for a file to change
459 * size and not to have a dquot structure associated with it.
465 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
466 struct vnode *vp = ITOV(ip);
470 * Disk quotas must be turned off for system files. Currently
471 * these are snapshots and quota files.
473 if ((vp->v_vflag & VV_SYSTEM) != 0)
476 * XXX: Turn off quotas for files with a negative UID or GID.
477 * This prevents the creation of 100GB+ quota files.
479 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
483 for (i = 0; i < MAXQUOTAS; i++) {
484 if (ump->um_quotas[i] == NULLVP ||
485 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
487 if (ip->i_dquot[i] == NODQUOT) {
489 vprint("chkdquot: missing dquot", ITOV(ip));
490 panic("chkdquot: missing dquot");
498 * Code to process quotactl commands.
502 * Q_QUOTAON - set up a quota file for a particular filesystem.
505 quotaon(td, mp, type, fname)
511 struct ufsmount *ump;
512 struct vnode *vp, **vpp;
515 int error, flags, vfslocked;
518 error = priv_check(td, PRIV_UFS_QUOTAON);
525 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, fname, td);
526 flags = FREAD | FWRITE;
527 error = vn_open(&nd, &flags, 0, NULL);
530 vfslocked = NDHASGIANT(&nd);
531 NDFREE(&nd, NDF_ONLY_PNBUF);
533 VOP_UNLOCK(vp, 0, td);
534 if (vp->v_type != VREG) {
535 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
536 VFS_UNLOCK_GIANT(vfslocked);
541 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
543 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
544 VFS_UNLOCK_GIANT(vfslocked);
547 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
549 mp->mnt_flag |= MNT_QUOTA;
553 vpp = &ump->um_quotas[type];
555 quotaoff1(td, mp, type);
557 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
558 vp->v_vflag |= VV_SYSTEM;
559 VOP_UNLOCK(vp, 0, td);
561 VFS_UNLOCK_GIANT(vfslocked);
563 * Save the credential of the process that turned on quotas.
564 * Set up the time limits for this quota.
566 ump->um_cred[type] = crhold(td->td_ucred);
567 ump->um_btime[type] = MAX_DQ_TIME;
568 ump->um_itime[type] = MAX_IQ_TIME;
569 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
570 if (dq->dq_btime > 0)
571 ump->um_btime[type] = dq->dq_btime;
572 if (dq->dq_itime > 0)
573 ump->um_itime[type] = dq->dq_itime;
577 * Allow the getdq from getinoquota below to read the quota
581 ump->um_qflags[type] &= ~QTF_CLOSING;
584 * Search vnodes associated with this mount point,
585 * adding references to quota file being opened.
586 * NB: only need to add dquot's for inodes being modified.
590 MNT_VNODE_FOREACH(vp, mp, mvp) {
593 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
595 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
598 if (vp->v_type == VNON || vp->v_writecount == 0) {
599 VOP_UNLOCK(vp, 0, td);
604 error = getinoquota(VTOI(vp));
605 VOP_UNLOCK(vp, 0, td);
609 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
616 quotaoff_inchange(td, mp, type);
618 ump->um_qflags[type] &= ~QTF_OPENING;
619 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
620 ("quotaon: leaking flags"));
627 * Main code to turn off disk quotas for a filesystem. Does not change
631 quotaoff1(td, mp, type)
637 struct vnode *qvp, *mvp;
638 struct ufsmount *ump;
648 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
649 ("quotaoff1: flags are invalid"));
650 if ((qvp = ump->um_quotas[type]) == NULLVP) {
654 cr = ump->um_cred[type];
658 * Search vnodes associated with this mount point,
659 * deleting any references to quota file being closed.
663 MNT_VNODE_FOREACH(vp, mp, mvp) {
666 if (vp->v_type == VNON) {
671 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
673 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
677 dq = ip->i_dquot[type];
678 ip->i_dquot[type] = NODQUOT;
680 VOP_UNLOCK(vp, 0, td);
687 /* Clear um_quotas before closing the quota vnode to prevent
688 * access to the closed vnode from dqget/dqsync
691 ump->um_quotas[type] = NULLVP;
692 ump->um_cred[type] = NOCRED;
695 vfslocked = VFS_LOCK_GIANT(qvp->v_mount);
696 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY, td);
697 qvp->v_vflag &= ~VV_SYSTEM;
698 VOP_UNLOCK(qvp, 0, td);
699 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
700 VFS_UNLOCK_GIANT(vfslocked);
707 * Turns off quotas, assumes that ump->um_qflags are already checked
708 * and QTF_CLOSING is set to indicate operation in progress. Fixes
709 * ump->um_qflags and mp->mnt_flag after.
712 quotaoff_inchange(td, mp, type)
717 struct ufsmount *ump;
721 error = quotaoff1(td, mp, type);
725 ump->um_qflags[type] &= ~QTF_CLOSING;
726 for (i = 0; i < MAXQUOTAS; i++)
727 if (ump->um_quotas[i] != NULLVP)
729 if (i == MAXQUOTAS) {
731 mp->mnt_flag &= ~MNT_QUOTA;
739 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
742 quotaoff(td, mp, type)
747 struct ufsmount *ump;
750 error = priv_check(td, PRIV_UFS_QUOTAOFF);
756 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
760 ump->um_qflags[type] |= QTF_CLOSING;
763 return (quotaoff_inchange(td, mp, type));
767 * Q_GETQUOTA - return current values in a dqblk structure.
770 getquota(td, mp, id, type, addr)
782 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
783 error = priv_check(td, PRIV_VFS_GETQUOTA);
790 if (!groupmember(id, td->td_ucred) &&
791 !unprivileged_get_quota) {
792 error = priv_check(td, PRIV_VFS_GETQUOTA);
803 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
806 error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
812 * Q_SETQUOTA - assign an entire dqblk structure.
815 setquota(td, mp, id, type, addr)
824 struct ufsmount *ump;
828 error = priv_check(td, PRIV_VFS_SETQUOTA);
833 error = copyin(addr, &newlim, sizeof (struct dqblk));
840 error = dqget(NULLVP, id, ump, type, &ndq);
845 DQI_WAIT(dq, PINOD+1, "setqta");
847 * Copy all but the current values.
848 * Reset time limit if previously had no soft limit or were
849 * under it, but now have a soft limit and are over it.
851 newlim.dqb_curblocks = dq->dq_curblocks;
852 newlim.dqb_curinodes = dq->dq_curinodes;
853 if (dq->dq_id != 0) {
854 newlim.dqb_btime = dq->dq_btime;
855 newlim.dqb_itime = dq->dq_itime;
857 if (newlim.dqb_bsoftlimit &&
858 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
859 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
860 newlim.dqb_btime = time_second + ump->um_btime[type];
861 if (newlim.dqb_isoftlimit &&
862 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
863 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
864 newlim.dqb_itime = time_second + ump->um_itime[type];
866 if (dq->dq_curblocks < dq->dq_bsoftlimit)
867 dq->dq_flags &= ~DQ_BLKS;
868 if (dq->dq_curinodes < dq->dq_isoftlimit)
869 dq->dq_flags &= ~DQ_INODS;
870 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
871 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
872 dq->dq_flags |= DQ_FAKE;
874 dq->dq_flags &= ~DQ_FAKE;
875 dq->dq_flags |= DQ_MOD;
882 * Q_SETUSE - set current inode and block usage.
885 setuse(td, mp, id, type, addr)
893 struct ufsmount *ump;
898 error = priv_check(td, PRIV_UFS_SETUSE);
903 error = copyin(addr, &usage, sizeof (struct dqblk));
910 error = dqget(NULLVP, id, ump, type, &ndq);
915 DQI_WAIT(dq, PINOD+1, "setuse");
917 * Reset time limit if have a soft limit and were
918 * previously under it, but are now over it.
920 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
921 usage.dqb_curblocks >= dq->dq_bsoftlimit)
922 dq->dq_btime = time_second + ump->um_btime[type];
923 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
924 usage.dqb_curinodes >= dq->dq_isoftlimit)
925 dq->dq_itime = time_second + ump->um_itime[type];
926 dq->dq_curblocks = usage.dqb_curblocks;
927 dq->dq_curinodes = usage.dqb_curinodes;
928 if (dq->dq_curblocks < dq->dq_bsoftlimit)
929 dq->dq_flags &= ~DQ_BLKS;
930 if (dq->dq_curinodes < dq->dq_isoftlimit)
931 dq->dq_flags &= ~DQ_INODS;
932 dq->dq_flags |= DQ_MOD;
939 * Q_SYNC - sync quota files to disk.
945 struct ufsmount *ump = VFSTOUFS(mp);
946 struct thread *td = curthread; /* XXX */
947 struct vnode *vp, *mvp;
952 * Check if the mount point has any quotas.
953 * If not, simply return.
956 for (i = 0; i < MAXQUOTAS; i++)
957 if (ump->um_quotas[i] != NULLVP)
963 * Search vnodes associated with this mount point,
964 * synchronizing any modified dquot structures.
968 MNT_VNODE_FOREACH(vp, mp, mvp) {
971 if (vp->v_type == VNON) {
976 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
979 if (error == ENOENT) {
980 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
985 for (i = 0; i < MAXQUOTAS; i++) {
986 dq = VTOI(vp)->i_dquot[i];
998 * Code pertaining to management of the in-core dquot data structures.
1000 #define DQHASH(dqvp, id) \
1001 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
1002 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
1003 static u_long dqhash;
1008 #define DQUOTINC 5 /* minimum free dquots desired */
1009 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
1010 static long numdquot, desireddquot = DQUOTINC;
1013 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
1018 #define DQH_LOCK() mtx_lock(&dqhlock)
1019 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
1021 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
1022 struct vnode *dqvp);
1025 * Initialize the quota system.
1031 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
1032 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
1033 TAILQ_INIT(&dqfreelist);
1037 * Shut down the quota system.
1044 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
1045 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
1046 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1047 mtx_destroy(&dq->dq_lock);
1050 mtx_destroy(&dqhlock);
1053 static struct dquot *
1054 dqhashfind(dqh, id, dqvp)
1061 mtx_assert(&dqhlock, MA_OWNED);
1062 LIST_FOREACH(dq, dqh, dq_hash) {
1063 if (dq->dq_id != id ||
1064 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1067 * Cache hit with no references. Take
1068 * the structure off the free list.
1070 if (dq->dq_cnt == 0)
1071 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1079 * Obtain a dquot structure for the specified identifier and quota file
1080 * reading the information from the file if necessary.
1083 dqget(vp, id, ump, type, dqp)
1086 struct ufsmount *ump;
1090 struct thread *td = curthread; /* XXX */
1091 struct dquot *dq, *dq1;
1096 int vfslocked, dqvplocked, error;
1098 #ifdef DEBUG_VFS_LOCKS
1100 ASSERT_VOP_ELOCKED(vp, "dqget");
1103 if (vp != NULLVP && *dqp != NODQUOT) {
1107 /* XXX: Disallow negative id values to prevent the
1108 * creation of 100GB+ quota data files.
1114 dqvp = ump->um_quotas[type];
1115 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1126 * Check the cache first.
1128 dqh = DQHASH(dqvp, id);
1130 dq = dqhashfind(dqh, id, dqvp);
1133 hfound: DQI_LOCK(dq);
1134 DQI_WAIT(dq, PINOD+1, "dqget");
1136 if (dq->dq_ump == NULL) {
1142 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1147 VFS_UNLOCK_GIANT(vfslocked);
1152 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1153 * since new dq will appear on the hash chain DQ_LOCKed.
1157 vn_lock(dqvp, LK_SHARED | LK_RETRY, td);
1161 * Recheck the cache after sleep for quota vnode lock.
1163 dq = dqhashfind(dqh, id, dqvp);
1171 * Not in cache, allocate a new one or take it from the
1174 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1175 numdquot < MAXQUOTAS * desiredvnodes)
1176 desireddquot += DQUOTINC;
1177 if (numdquot < desireddquot) {
1180 dq1 = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
1182 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1185 * Recheck the cache after sleep for memory.
1187 dq = dqhashfind(dqh, id, dqvp);
1191 mtx_destroy(&dq1->dq_lock);
1197 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1201 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1206 VFS_UNLOCK_GIANT(vfslocked);
1209 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1210 panic("dqget: free dquot isn't %p", dq);
1211 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1212 if (dq->dq_ump != NULL)
1213 LIST_REMOVE(dq, dq_hash);
1217 * Dq is put into hash already locked to prevent parallel
1218 * usage while it is being read from file.
1220 dq->dq_flags = DQ_LOCK;
1224 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1228 auio.uio_iov = &aiov;
1229 auio.uio_iovcnt = 1;
1230 aiov.iov_base = &dq->dq_dqb;
1231 aiov.iov_len = sizeof (struct dqblk);
1232 auio.uio_resid = sizeof (struct dqblk);
1233 auio.uio_offset = (off_t)id * sizeof (struct dqblk);
1234 auio.uio_segflg = UIO_SYSSPACE;
1235 auio.uio_rw = UIO_READ;
1236 auio.uio_td = (struct thread *)0;
1238 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1239 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1240 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
1241 bzero(&dq->dq_dqb, sizeof(struct dqblk));
1246 VFS_UNLOCK_GIANT(vfslocked);
1248 * I/O error in reading quota file, release
1249 * quota structure and reflect problem to caller.
1254 LIST_REMOVE(dq, dq_hash);
1257 if (dq->dq_flags & DQ_WANT)
1267 * Check for no limit to enforce.
1268 * Initialize time values if necessary.
1270 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1271 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1272 dq->dq_flags |= DQ_FAKE;
1273 if (dq->dq_id != 0) {
1274 if (dq->dq_btime == 0) {
1275 dq->dq_btime = time_second + ump->um_btime[type];
1276 if (dq->dq_bsoftlimit &&
1277 dq->dq_curblocks >= dq->dq_bsoftlimit)
1278 dq->dq_flags |= DQ_MOD;
1280 if (dq->dq_itime == 0) {
1281 dq->dq_itime = time_second + ump->um_itime[type];
1282 if (dq->dq_isoftlimit &&
1283 dq->dq_curinodes >= dq->dq_isoftlimit)
1284 dq->dq_flags |= DQ_MOD;
1295 * Obtain a reference to a dquot.
1307 * Release a reference to a dquot.
1318 if (dq->dq_cnt > 1) {
1325 (void) dqsync(vp, dq);
1328 if (--dq->dq_cnt > 0)
1335 * The dq may become dirty after it is synced but before it is
1336 * put to the free list. Checking the DQ_MOD there without
1337 * locking dq should be safe since no other references to the
1340 if ((dq->dq_flags & DQ_MOD) != 0) {
1345 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1350 * Update the disk quota in the quota file.
1357 struct thread *td = curthread; /* XXX */
1361 int vfslocked, error;
1363 struct ufsmount *ump;
1365 #ifdef DEBUG_VFS_LOCKS
1367 ASSERT_VOP_ELOCKED(vp, "dqsync");
1373 panic("dqsync: dquot");
1374 if ((ump = dq->dq_ump) == NULL)
1377 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP)
1378 panic("dqsync: file");
1382 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1384 if ((dq->dq_flags & DQ_MOD) == 0) {
1387 VFS_UNLOCK_GIANT(vfslocked);
1392 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1394 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
1396 VFS_UNLOCK_GIANT(vfslocked);
1398 DQI_WAIT(dq, PINOD+2, "dqsync");
1399 if ((dq->dq_flags & DQ_MOD) == 0)
1401 dq->dq_flags |= DQ_LOCK;
1404 auio.uio_iov = &aiov;
1405 auio.uio_iovcnt = 1;
1406 aiov.iov_base = &dq->dq_dqb;
1407 aiov.iov_len = sizeof (struct dqblk);
1408 auio.uio_resid = sizeof (struct dqblk);
1409 auio.uio_offset = (off_t)dq->dq_id * sizeof (struct dqblk);
1410 auio.uio_segflg = UIO_SYSSPACE;
1411 auio.uio_rw = UIO_WRITE;
1412 auio.uio_td = (struct thread *)0;
1413 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1414 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1415 VFS_UNLOCK_GIANT(vfslocked);
1416 if (auio.uio_resid && error == 0)
1421 dq->dq_flags &= ~DQ_MOD;
1422 out: DQI_UNLOCK(dq);
1423 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1428 vn_finished_secondary_write(mp);
1429 VFS_UNLOCK_GIANT(vfslocked);
1434 * Flush all entries from the cache for a particular vnode.
1440 struct dquot *dq, *nextdq;
1444 * Move all dquot's that used to refer to this quota
1445 * file off their hash chains (they will eventually
1446 * fall off the head of the free list and be re-used).
1449 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1450 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1451 nextdq = LIST_NEXT(dq, dq_hash);
1452 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1455 panic("dqflush: stray dquot");
1456 LIST_REMOVE(dq, dq_hash);
1457 dq->dq_ump = (struct ufsmount *)0;