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.
97 getinoquota(struct inode *ip)
106 * Disk quotas must be turned off for system files. Currently
107 * snapshot and quota files.
109 if ((vp->v_vflag & VV_SYSTEM) != 0)
112 * XXX: Turn off quotas for files with a negative UID or GID.
113 * This prevents the creation of 100GB+ quota files.
115 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
117 ump = VFSTOUFS(vp->v_mount);
119 * Set up the user quota based on file uid.
120 * EINVAL means that quotas are not enabled.
123 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
127 * Set up the group quota based on file gid.
128 * EINVAL means that quotas are not enabled.
131 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
138 * Update disk usage, and take corrective action.
141 chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
144 ufs2_daddr_t ncurblocks;
145 struct vnode *vp = ITOV(ip);
146 int i, error, warn, do_check;
149 * Disk quotas must be turned off for system files. Currently
150 * snapshot and quota files.
152 if ((vp->v_vflag & VV_SYSTEM) != 0)
155 * XXX: Turn off quotas for files with a negative UID or GID.
156 * This prevents the creation of 100GB+ quota files.
158 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
161 if ((flags & CHOWN) == 0)
167 for (i = 0; i < MAXQUOTAS; i++) {
168 if ((dq = ip->i_dquot[i]) == NODQUOT)
171 DQI_WAIT(dq, PINOD+1, "chkdq1");
172 ncurblocks = dq->dq_curblocks + change;
174 dq->dq_curblocks = ncurblocks;
176 dq->dq_curblocks = 0;
177 dq->dq_flags &= ~DQ_BLKS;
178 dq->dq_flags |= DQ_MOD;
183 if ((flags & FORCE) == 0 &&
184 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
188 for (i = 0; i < MAXQUOTAS; i++) {
189 if ((dq = ip->i_dquot[i]) == NODQUOT)
193 DQI_WAIT(dq, PINOD+1, "chkdq2");
195 error = chkdqchg(ip, change, cred, i, &warn);
198 * Roll back user quota changes when
199 * group quota failed.
207 DQI_WAIT(dq, PINOD+1, "chkdq3");
208 ncurblocks = dq->dq_curblocks - change;
210 dq->dq_curblocks = ncurblocks;
212 dq->dq_curblocks = 0;
213 dq->dq_flags &= ~DQ_BLKS;
214 dq->dq_flags |= DQ_MOD;
220 /* Reset timer when crossing soft limit */
221 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
222 dq->dq_curblocks < dq->dq_bsoftlimit)
223 dq->dq_btime = time_second +
224 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
225 dq->dq_curblocks += change;
226 dq->dq_flags |= DQ_MOD;
229 uprintf("\n%s: warning, %s %s\n",
230 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
231 quotatypes[i], "disk quota exceeded");
237 * Check for a valid change to a users allocation.
238 * Issue an error message if appropriate.
241 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
244 struct dquot *dq = ip->i_dquot[type];
245 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
248 * If user would exceed their hard limit, disallow space allocation.
250 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
251 if ((dq->dq_flags & DQ_BLKS) == 0 &&
252 ip->i_uid == cred->cr_uid) {
253 dq->dq_flags |= DQ_BLKS;
255 uprintf("\n%s: write failed, %s disk limit reached\n",
256 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
264 * If user is over their soft limit for too long, disallow space
265 * allocation. Reset time limit as they cross their soft limit.
267 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
268 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
269 dq->dq_btime = time_second +
270 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
271 if (ip->i_uid == cred->cr_uid)
275 if (time_second > dq->dq_btime) {
276 if ((dq->dq_flags & DQ_BLKS) == 0 &&
277 ip->i_uid == cred->cr_uid) {
278 dq->dq_flags |= DQ_BLKS;
280 uprintf("\n%s: write failed, %s %s\n",
281 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
283 "disk quota exceeded for too long");
294 * Check the inode limit, applying corrective action.
297 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
301 int i, error, warn, do_check;
304 if ((flags & CHOWN) == 0)
310 for (i = 0; i < MAXQUOTAS; i++) {
311 if ((dq = ip->i_dquot[i]) == NODQUOT)
314 DQI_WAIT(dq, PINOD+1, "chkiq1");
315 ncurinodes = dq->dq_curinodes + change;
316 /* XXX: ncurinodes is unsigned */
317 if (dq->dq_curinodes != 0 && ncurinodes >= 0)
318 dq->dq_curinodes = ncurinodes;
320 dq->dq_curinodes = 0;
321 dq->dq_flags &= ~DQ_INODS;
322 dq->dq_flags |= DQ_MOD;
327 if ((flags & FORCE) == 0 &&
328 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
332 for (i = 0; i < MAXQUOTAS; i++) {
333 if ((dq = ip->i_dquot[i]) == NODQUOT)
337 DQI_WAIT(dq, PINOD+1, "chkiq2");
339 error = chkiqchg(ip, change, cred, i, &warn);
342 * Roll back user quota changes when
343 * group quota failed.
351 DQI_WAIT(dq, PINOD+1, "chkiq3");
352 ncurinodes = dq->dq_curinodes - change;
353 /* XXX: ncurinodes is unsigned */
354 if (dq->dq_curinodes != 0 &&
356 dq->dq_curinodes = ncurinodes;
358 dq->dq_curinodes = 0;
359 dq->dq_flags &= ~DQ_INODS;
360 dq->dq_flags |= DQ_MOD;
366 /* Reset timer when crossing soft limit */
367 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
368 dq->dq_curinodes < dq->dq_isoftlimit)
369 dq->dq_itime = time_second +
370 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
371 dq->dq_curinodes += change;
372 dq->dq_flags |= DQ_MOD;
375 uprintf("\n%s: warning, %s %s\n",
376 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
377 quotatypes[i], "inode quota exceeded");
383 * Check for a valid change to a users allocation.
384 * Issue an error message if appropriate.
387 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
389 struct dquot *dq = ip->i_dquot[type];
390 ino_t ncurinodes = dq->dq_curinodes + change;
393 * If user would exceed their hard limit, disallow inode allocation.
395 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
396 if ((dq->dq_flags & DQ_INODS) == 0 &&
397 ip->i_uid == cred->cr_uid) {
398 dq->dq_flags |= DQ_INODS;
400 uprintf("\n%s: write failed, %s inode limit reached\n",
401 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
409 * If user is over their soft limit for too long, disallow inode
410 * allocation. Reset time limit as they cross their soft limit.
412 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
413 if (dq->dq_curinodes < dq->dq_isoftlimit) {
414 dq->dq_itime = time_second +
415 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
416 if (ip->i_uid == cred->cr_uid)
420 if (time_second > dq->dq_itime) {
421 if ((dq->dq_flags & DQ_INODS) == 0 &&
422 ip->i_uid == cred->cr_uid) {
423 dq->dq_flags |= DQ_INODS;
425 uprintf("\n%s: write failed, %s %s\n",
426 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
428 "inode quota exceeded for too long");
440 * On filesystems with quotas enabled, it is an error for a file to change
441 * size and not to have a dquot structure associated with it.
444 chkdquot(struct inode *ip)
446 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
447 struct vnode *vp = ITOV(ip);
451 * Disk quotas must be turned off for system files. Currently
452 * these are snapshots and quota files.
454 if ((vp->v_vflag & VV_SYSTEM) != 0)
457 * XXX: Turn off quotas for files with a negative UID or GID.
458 * This prevents the creation of 100GB+ quota files.
460 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
464 for (i = 0; i < MAXQUOTAS; i++) {
465 if (ump->um_quotas[i] == NULLVP ||
466 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
468 if (ip->i_dquot[i] == NODQUOT) {
470 vprint("chkdquot: missing dquot", ITOV(ip));
471 panic("chkdquot: missing dquot");
479 * Code to process quotactl commands.
483 * Q_QUOTAON - set up a quota file for a particular filesystem.
486 quotaon(struct thread *td, struct mount *mp, int type, void *fname)
488 struct ufsmount *ump;
489 struct vnode *vp, **vpp;
492 int error, flags, vfslocked;
495 error = priv_check(td, PRIV_UFS_QUOTAON);
502 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, fname, td);
503 flags = FREAD | FWRITE;
504 error = vn_open(&nd, &flags, 0, NULL);
507 vfslocked = NDHASGIANT(&nd);
508 NDFREE(&nd, NDF_ONLY_PNBUF);
511 if (vp->v_type != VREG) {
512 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
513 VFS_UNLOCK_GIANT(vfslocked);
518 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
520 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
521 VFS_UNLOCK_GIANT(vfslocked);
524 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
526 mp->mnt_flag |= MNT_QUOTA;
530 vpp = &ump->um_quotas[type];
532 quotaoff1(td, mp, type);
534 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
535 vp->v_vflag |= VV_SYSTEM;
538 VFS_UNLOCK_GIANT(vfslocked);
540 * Save the credential of the process that turned on quotas.
541 * Set up the time limits for this quota.
543 ump->um_cred[type] = crhold(td->td_ucred);
544 ump->um_btime[type] = MAX_DQ_TIME;
545 ump->um_itime[type] = MAX_IQ_TIME;
546 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
547 if (dq->dq_btime > 0)
548 ump->um_btime[type] = dq->dq_btime;
549 if (dq->dq_itime > 0)
550 ump->um_itime[type] = dq->dq_itime;
554 * Allow the getdq from getinoquota below to read the quota
558 ump->um_qflags[type] &= ~QTF_CLOSING;
561 * Search vnodes associated with this mount point,
562 * adding references to quota file being opened.
563 * NB: only need to add dquot's for inodes being modified.
567 MNT_VNODE_FOREACH(vp, mp, mvp) {
570 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
572 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
575 if (vp->v_type == VNON || vp->v_writecount == 0) {
581 error = getinoquota(VTOI(vp));
586 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
593 quotaoff_inchange(td, mp, type);
595 ump->um_qflags[type] &= ~QTF_OPENING;
596 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
597 ("quotaon: leaking flags"));
604 * Main code to turn off disk quotas for a filesystem. Does not change
608 quotaoff1(struct thread *td, struct mount *mp, int type)
611 struct vnode *qvp, *mvp;
612 struct ufsmount *ump;
622 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
623 ("quotaoff1: flags are invalid"));
624 if ((qvp = ump->um_quotas[type]) == NULLVP) {
628 cr = ump->um_cred[type];
632 * Search vnodes associated with this mount point,
633 * deleting any references to quota file being closed.
637 MNT_VNODE_FOREACH(vp, mp, mvp) {
640 if (vp->v_type == VNON) {
645 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
647 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
651 dq = ip->i_dquot[type];
652 ip->i_dquot[type] = NODQUOT;
661 /* Clear um_quotas before closing the quota vnode to prevent
662 * access to the closed vnode from dqget/dqsync
665 ump->um_quotas[type] = NULLVP;
666 ump->um_cred[type] = NOCRED;
669 vfslocked = VFS_LOCK_GIANT(qvp->v_mount);
670 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
671 qvp->v_vflag &= ~VV_SYSTEM;
673 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
674 VFS_UNLOCK_GIANT(vfslocked);
681 * Turns off quotas, assumes that ump->um_qflags are already checked
682 * and QTF_CLOSING is set to indicate operation in progress. Fixes
683 * ump->um_qflags and mp->mnt_flag after.
686 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
688 struct ufsmount *ump;
692 error = quotaoff1(td, mp, type);
696 ump->um_qflags[type] &= ~QTF_CLOSING;
697 for (i = 0; i < MAXQUOTAS; i++)
698 if (ump->um_quotas[i] != NULLVP)
700 if (i == MAXQUOTAS) {
702 mp->mnt_flag &= ~MNT_QUOTA;
710 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
713 quotaoff(struct thread *td, struct mount *mp, int type)
715 struct ufsmount *ump;
718 error = priv_check(td, PRIV_UFS_QUOTAOFF);
724 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
728 ump->um_qflags[type] |= QTF_CLOSING;
731 return (quotaoff_inchange(td, mp, type));
735 * Q_GETQUOTA - return current values in a dqblk structure.
738 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
745 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
746 error = priv_check(td, PRIV_VFS_GETQUOTA);
753 if (!groupmember(id, td->td_ucred) &&
754 !unprivileged_get_quota) {
755 error = priv_check(td, PRIV_VFS_GETQUOTA);
766 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
769 error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
775 * Q_SETQUOTA - assign an entire dqblk structure.
778 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
782 struct ufsmount *ump;
786 error = priv_check(td, PRIV_VFS_SETQUOTA);
791 error = copyin(addr, &newlim, sizeof (struct dqblk));
798 error = dqget(NULLVP, id, ump, type, &ndq);
803 DQI_WAIT(dq, PINOD+1, "setqta");
805 * Copy all but the current values.
806 * Reset time limit if previously had no soft limit or were
807 * under it, but now have a soft limit and are over it.
809 newlim.dqb_curblocks = dq->dq_curblocks;
810 newlim.dqb_curinodes = dq->dq_curinodes;
811 if (dq->dq_id != 0) {
812 newlim.dqb_btime = dq->dq_btime;
813 newlim.dqb_itime = dq->dq_itime;
815 if (newlim.dqb_bsoftlimit &&
816 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
817 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
818 newlim.dqb_btime = time_second + ump->um_btime[type];
819 if (newlim.dqb_isoftlimit &&
820 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
821 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
822 newlim.dqb_itime = time_second + ump->um_itime[type];
824 if (dq->dq_curblocks < dq->dq_bsoftlimit)
825 dq->dq_flags &= ~DQ_BLKS;
826 if (dq->dq_curinodes < dq->dq_isoftlimit)
827 dq->dq_flags &= ~DQ_INODS;
828 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
829 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
830 dq->dq_flags |= DQ_FAKE;
832 dq->dq_flags &= ~DQ_FAKE;
833 dq->dq_flags |= DQ_MOD;
840 * Q_SETUSE - set current inode and block usage.
843 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
846 struct ufsmount *ump;
851 error = priv_check(td, PRIV_UFS_SETUSE);
856 error = copyin(addr, &usage, sizeof (struct dqblk));
863 error = dqget(NULLVP, id, ump, type, &ndq);
868 DQI_WAIT(dq, PINOD+1, "setuse");
870 * Reset time limit if have a soft limit and were
871 * previously under it, but are now over it.
873 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
874 usage.dqb_curblocks >= dq->dq_bsoftlimit)
875 dq->dq_btime = time_second + ump->um_btime[type];
876 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
877 usage.dqb_curinodes >= dq->dq_isoftlimit)
878 dq->dq_itime = time_second + ump->um_itime[type];
879 dq->dq_curblocks = usage.dqb_curblocks;
880 dq->dq_curinodes = usage.dqb_curinodes;
881 if (dq->dq_curblocks < dq->dq_bsoftlimit)
882 dq->dq_flags &= ~DQ_BLKS;
883 if (dq->dq_curinodes < dq->dq_isoftlimit)
884 dq->dq_flags &= ~DQ_INODS;
885 dq->dq_flags |= DQ_MOD;
892 * Q_SYNC - sync quota files to disk.
895 qsync(struct mount *mp)
897 struct ufsmount *ump = VFSTOUFS(mp);
898 struct thread *td = curthread; /* XXX */
899 struct vnode *vp, *mvp;
904 * Check if the mount point has any quotas.
905 * If not, simply return.
908 for (i = 0; i < MAXQUOTAS; i++)
909 if (ump->um_quotas[i] != NULLVP)
915 * Search vnodes associated with this mount point,
916 * synchronizing any modified dquot structures.
920 MNT_VNODE_FOREACH(vp, mp, mvp) {
923 if (vp->v_type == VNON) {
928 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
931 if (error == ENOENT) {
932 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
937 for (i = 0; i < MAXQUOTAS; i++) {
938 dq = VTOI(vp)->i_dquot[i];
950 * Code pertaining to management of the in-core dquot data structures.
952 #define DQHASH(dqvp, id) \
953 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
954 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
955 static u_long dqhash;
960 #define DQUOTINC 5 /* minimum free dquots desired */
961 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
962 static long numdquot, desireddquot = DQUOTINC;
965 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
970 #define DQH_LOCK() mtx_lock(&dqhlock)
971 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
973 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
977 * Initialize the quota system.
983 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
984 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
985 TAILQ_INIT(&dqfreelist);
989 * Shut down the quota system.
996 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
997 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
998 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
999 mtx_destroy(&dq->dq_lock);
1002 mtx_destroy(&dqhlock);
1005 static struct dquot *
1006 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1010 mtx_assert(&dqhlock, MA_OWNED);
1011 LIST_FOREACH(dq, dqh, dq_hash) {
1012 if (dq->dq_id != id ||
1013 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1016 * Cache hit with no references. Take
1017 * the structure off the free list.
1019 if (dq->dq_cnt == 0)
1020 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1028 * Obtain a dquot structure for the specified identifier and quota file
1029 * reading the information from the file if necessary.
1032 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1035 struct dquot *dq, *dq1;
1040 int vfslocked, dqvplocked, error;
1042 #ifdef DEBUG_VFS_LOCKS
1044 ASSERT_VOP_ELOCKED(vp, "dqget");
1047 if (vp != NULLVP && *dqp != NODQUOT) {
1051 /* XXX: Disallow negative id values to prevent the
1052 * creation of 100GB+ quota data files.
1058 dqvp = ump->um_quotas[type];
1059 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1070 * Check the cache first.
1072 dqh = DQHASH(dqvp, id);
1074 dq = dqhashfind(dqh, id, dqvp);
1077 hfound: DQI_LOCK(dq);
1078 DQI_WAIT(dq, PINOD+1, "dqget");
1080 if (dq->dq_ump == NULL) {
1086 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1091 VFS_UNLOCK_GIANT(vfslocked);
1096 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1097 * since new dq will appear on the hash chain DQ_LOCKed.
1101 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1105 * Recheck the cache after sleep for quota vnode lock.
1107 dq = dqhashfind(dqh, id, dqvp);
1115 * Not in cache, allocate a new one or take it from the
1118 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1119 numdquot < MAXQUOTAS * desiredvnodes)
1120 desireddquot += DQUOTINC;
1121 if (numdquot < desireddquot) {
1124 dq1 = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
1126 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1129 * Recheck the cache after sleep for memory.
1131 dq = dqhashfind(dqh, id, dqvp);
1135 mtx_destroy(&dq1->dq_lock);
1141 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1145 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1150 VFS_UNLOCK_GIANT(vfslocked);
1153 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1154 panic("dqget: free dquot isn't %p", dq);
1155 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1156 if (dq->dq_ump != NULL)
1157 LIST_REMOVE(dq, dq_hash);
1161 * Dq is put into hash already locked to prevent parallel
1162 * usage while it is being read from file.
1164 dq->dq_flags = DQ_LOCK;
1168 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1172 auio.uio_iov = &aiov;
1173 auio.uio_iovcnt = 1;
1174 aiov.iov_base = &dq->dq_dqb;
1175 aiov.iov_len = sizeof (struct dqblk);
1176 auio.uio_resid = sizeof (struct dqblk);
1177 auio.uio_offset = (off_t)id * sizeof (struct dqblk);
1178 auio.uio_segflg = UIO_SYSSPACE;
1179 auio.uio_rw = UIO_READ;
1180 auio.uio_td = (struct thread *)0;
1182 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1183 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1184 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
1185 bzero(&dq->dq_dqb, sizeof(struct dqblk));
1190 VFS_UNLOCK_GIANT(vfslocked);
1192 * I/O error in reading quota file, release
1193 * quota structure and reflect problem to caller.
1198 LIST_REMOVE(dq, dq_hash);
1201 if (dq->dq_flags & DQ_WANT)
1211 * Check for no limit to enforce.
1212 * Initialize time values if necessary.
1214 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1215 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1216 dq->dq_flags |= DQ_FAKE;
1217 if (dq->dq_id != 0) {
1218 if (dq->dq_btime == 0) {
1219 dq->dq_btime = time_second + ump->um_btime[type];
1220 if (dq->dq_bsoftlimit &&
1221 dq->dq_curblocks >= dq->dq_bsoftlimit)
1222 dq->dq_flags |= DQ_MOD;
1224 if (dq->dq_itime == 0) {
1225 dq->dq_itime = time_second + ump->um_itime[type];
1226 if (dq->dq_isoftlimit &&
1227 dq->dq_curinodes >= dq->dq_isoftlimit)
1228 dq->dq_flags |= DQ_MOD;
1239 * Obtain a reference to a dquot.
1242 dqref(struct dquot *dq)
1250 * Release a reference to a dquot.
1253 dqrele(struct vnode *vp, struct dquot *dq)
1259 if (dq->dq_cnt > 1) {
1266 (void) dqsync(vp, dq);
1269 if (--dq->dq_cnt > 0)
1276 * The dq may become dirty after it is synced but before it is
1277 * put to the free list. Checking the DQ_MOD there without
1278 * locking dq should be safe since no other references to the
1281 if ((dq->dq_flags & DQ_MOD) != 0) {
1286 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1291 * Update the disk quota in the quota file.
1294 dqsync(struct vnode *vp, struct dquot *dq)
1299 int vfslocked, error;
1301 struct ufsmount *ump;
1303 #ifdef DEBUG_VFS_LOCKS
1305 ASSERT_VOP_ELOCKED(vp, "dqsync");
1311 panic("dqsync: dquot");
1312 if ((ump = dq->dq_ump) == NULL)
1315 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP)
1316 panic("dqsync: file");
1320 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1322 if ((dq->dq_flags & DQ_MOD) == 0) {
1325 VFS_UNLOCK_GIANT(vfslocked);
1330 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1332 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1334 VFS_UNLOCK_GIANT(vfslocked);
1336 DQI_WAIT(dq, PINOD+2, "dqsync");
1337 if ((dq->dq_flags & DQ_MOD) == 0)
1339 dq->dq_flags |= DQ_LOCK;
1342 auio.uio_iov = &aiov;
1343 auio.uio_iovcnt = 1;
1344 aiov.iov_base = &dq->dq_dqb;
1345 aiov.iov_len = sizeof (struct dqblk);
1346 auio.uio_resid = sizeof (struct dqblk);
1347 auio.uio_offset = (off_t)dq->dq_id * sizeof (struct dqblk);
1348 auio.uio_segflg = UIO_SYSSPACE;
1349 auio.uio_rw = UIO_WRITE;
1350 auio.uio_td = (struct thread *)0;
1351 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1352 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1353 VFS_UNLOCK_GIANT(vfslocked);
1354 if (auio.uio_resid && error == 0)
1359 dq->dq_flags &= ~DQ_MOD;
1360 out: DQI_UNLOCK(dq);
1361 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1366 vn_finished_secondary_write(mp);
1367 VFS_UNLOCK_GIANT(vfslocked);
1372 * Flush all entries from the cache for a particular vnode.
1375 dqflush(struct vnode *vp)
1377 struct dquot *dq, *nextdq;
1381 * Move all dquot's that used to refer to this quota
1382 * file off their hash chains (they will eventually
1383 * fall off the head of the free list and be re-used).
1386 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1387 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1388 nextdq = LIST_NEXT(dq, dq_hash);
1389 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1392 panic("dqflush: stray dquot");
1393 LIST_REMOVE(dq, dq_hash);
1394 dq->dq_ump = (struct ufsmount *)0;