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 + ip->i_ump->um_btime[i];
224 dq->dq_curblocks += change;
225 dq->dq_flags |= DQ_MOD;
228 uprintf("\n%s: warning, %s %s\n",
229 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
230 quotatypes[i], "disk quota exceeded");
236 * Check for a valid change to a users allocation.
237 * Issue an error message if appropriate.
240 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
243 struct dquot *dq = ip->i_dquot[type];
244 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
247 * If user would exceed their hard limit, disallow space allocation.
249 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
250 if ((dq->dq_flags & DQ_BLKS) == 0 &&
251 ip->i_uid == cred->cr_uid) {
252 dq->dq_flags |= DQ_BLKS;
254 uprintf("\n%s: write failed, %s disk limit reached\n",
255 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
263 * If user is over their soft limit for too long, disallow space
264 * allocation. Reset time limit as they cross their soft limit.
266 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
267 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
268 dq->dq_btime = time_second + ip->i_ump->um_btime[type];
269 if (ip->i_uid == cred->cr_uid)
273 if (time_second > dq->dq_btime) {
274 if ((dq->dq_flags & DQ_BLKS) == 0 &&
275 ip->i_uid == cred->cr_uid) {
276 dq->dq_flags |= DQ_BLKS;
278 uprintf("\n%s: write failed, %s %s\n",
279 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
281 "disk quota exceeded for too long");
292 * Check the inode limit, applying corrective action.
295 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
299 int i, error, warn, do_check;
302 if ((flags & CHOWN) == 0)
308 for (i = 0; i < MAXQUOTAS; i++) {
309 if ((dq = ip->i_dquot[i]) == NODQUOT)
312 DQI_WAIT(dq, PINOD+1, "chkiq1");
313 ncurinodes = dq->dq_curinodes + change;
314 /* XXX: ncurinodes is unsigned */
315 if (dq->dq_curinodes != 0 && ncurinodes >= 0)
316 dq->dq_curinodes = ncurinodes;
318 dq->dq_curinodes = 0;
319 dq->dq_flags &= ~DQ_INODS;
320 dq->dq_flags |= DQ_MOD;
325 if ((flags & FORCE) == 0 &&
326 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
330 for (i = 0; i < MAXQUOTAS; i++) {
331 if ((dq = ip->i_dquot[i]) == NODQUOT)
335 DQI_WAIT(dq, PINOD+1, "chkiq2");
337 error = chkiqchg(ip, change, cred, i, &warn);
340 * Roll back user quota changes when
341 * group quota failed.
349 DQI_WAIT(dq, PINOD+1, "chkiq3");
350 ncurinodes = dq->dq_curinodes - change;
351 /* XXX: ncurinodes is unsigned */
352 if (dq->dq_curinodes != 0 &&
354 dq->dq_curinodes = ncurinodes;
356 dq->dq_curinodes = 0;
357 dq->dq_flags &= ~DQ_INODS;
358 dq->dq_flags |= DQ_MOD;
364 /* Reset timer when crossing soft limit */
365 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
366 dq->dq_curinodes < dq->dq_isoftlimit)
367 dq->dq_itime = time_second + ip->i_ump->um_itime[i];
368 dq->dq_curinodes += change;
369 dq->dq_flags |= DQ_MOD;
372 uprintf("\n%s: warning, %s %s\n",
373 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
374 quotatypes[i], "inode quota exceeded");
380 * Check for a valid change to a users allocation.
381 * Issue an error message if appropriate.
384 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
386 struct dquot *dq = ip->i_dquot[type];
387 ino_t ncurinodes = dq->dq_curinodes + change;
390 * If user would exceed their hard limit, disallow inode allocation.
392 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
393 if ((dq->dq_flags & DQ_INODS) == 0 &&
394 ip->i_uid == cred->cr_uid) {
395 dq->dq_flags |= DQ_INODS;
397 uprintf("\n%s: write failed, %s inode limit reached\n",
398 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
406 * If user is over their soft limit for too long, disallow inode
407 * allocation. Reset time limit as they cross their soft limit.
409 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
410 if (dq->dq_curinodes < dq->dq_isoftlimit) {
411 dq->dq_itime = time_second + ip->i_ump->um_itime[type];
412 if (ip->i_uid == cred->cr_uid)
416 if (time_second > dq->dq_itime) {
417 if ((dq->dq_flags & DQ_INODS) == 0 &&
418 ip->i_uid == cred->cr_uid) {
419 dq->dq_flags |= DQ_INODS;
421 uprintf("\n%s: write failed, %s %s\n",
422 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
424 "inode quota exceeded for too long");
436 * On filesystems with quotas enabled, it is an error for a file to change
437 * size and not to have a dquot structure associated with it.
440 chkdquot(struct inode *ip)
442 struct ufsmount *ump = ip->i_ump;
443 struct vnode *vp = ITOV(ip);
447 * Disk quotas must be turned off for system files. Currently
448 * these are snapshots and quota files.
450 if ((vp->v_vflag & VV_SYSTEM) != 0)
453 * XXX: Turn off quotas for files with a negative UID or GID.
454 * This prevents the creation of 100GB+ quota files.
456 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
460 for (i = 0; i < MAXQUOTAS; i++) {
461 if (ump->um_quotas[i] == NULLVP ||
462 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
464 if (ip->i_dquot[i] == NODQUOT) {
466 vprint("chkdquot: missing dquot", ITOV(ip));
467 panic("chkdquot: missing dquot");
475 * Code to process quotactl commands.
479 * Q_QUOTAON - set up a quota file for a particular filesystem.
482 quotaon(struct thread *td, struct mount *mp, int type, void *fname)
484 struct ufsmount *ump;
485 struct vnode *vp, **vpp;
488 int error, flags, vfslocked;
491 error = priv_check(td, PRIV_UFS_QUOTAON);
498 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, fname, td);
499 flags = FREAD | FWRITE;
500 error = vn_open(&nd, &flags, 0, NULL);
503 vfslocked = NDHASGIANT(&nd);
504 NDFREE(&nd, NDF_ONLY_PNBUF);
507 if (vp->v_type != VREG) {
508 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
509 VFS_UNLOCK_GIANT(vfslocked);
514 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
516 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
517 VFS_UNLOCK_GIANT(vfslocked);
520 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
522 mp->mnt_flag |= MNT_QUOTA;
526 vpp = &ump->um_quotas[type];
528 quotaoff1(td, mp, type);
530 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
531 vp->v_vflag |= VV_SYSTEM;
534 VFS_UNLOCK_GIANT(vfslocked);
536 * Save the credential of the process that turned on quotas.
537 * Set up the time limits for this quota.
539 ump->um_cred[type] = crhold(td->td_ucred);
540 ump->um_btime[type] = MAX_DQ_TIME;
541 ump->um_itime[type] = MAX_IQ_TIME;
542 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
543 if (dq->dq_btime > 0)
544 ump->um_btime[type] = dq->dq_btime;
545 if (dq->dq_itime > 0)
546 ump->um_itime[type] = dq->dq_itime;
550 * Allow the getdq from getinoquota below to read the quota
554 ump->um_qflags[type] &= ~QTF_CLOSING;
557 * Search vnodes associated with this mount point,
558 * adding references to quota file being opened.
559 * NB: only need to add dquot's for inodes being modified.
563 MNT_VNODE_FOREACH(vp, mp, mvp) {
566 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
568 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
571 if (vp->v_type == VNON || vp->v_writecount == 0) {
577 error = getinoquota(VTOI(vp));
582 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
589 quotaoff_inchange(td, mp, type);
591 ump->um_qflags[type] &= ~QTF_OPENING;
592 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
593 ("quotaon: leaking flags"));
600 * Main code to turn off disk quotas for a filesystem. Does not change
604 quotaoff1(struct thread *td, struct mount *mp, int type)
607 struct vnode *qvp, *mvp;
608 struct ufsmount *ump;
618 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
619 ("quotaoff1: flags are invalid"));
620 if ((qvp = ump->um_quotas[type]) == NULLVP) {
624 cr = ump->um_cred[type];
628 * Search vnodes associated with this mount point,
629 * deleting any references to quota file being closed.
633 MNT_VNODE_FOREACH(vp, mp, mvp) {
636 if (vp->v_type == VNON) {
641 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
643 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
647 dq = ip->i_dquot[type];
648 ip->i_dquot[type] = NODQUOT;
657 /* Clear um_quotas before closing the quota vnode to prevent
658 * access to the closed vnode from dqget/dqsync
661 ump->um_quotas[type] = NULLVP;
662 ump->um_cred[type] = NOCRED;
665 vfslocked = VFS_LOCK_GIANT(qvp->v_mount);
666 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
667 qvp->v_vflag &= ~VV_SYSTEM;
669 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
670 VFS_UNLOCK_GIANT(vfslocked);
677 * Turns off quotas, assumes that ump->um_qflags are already checked
678 * and QTF_CLOSING is set to indicate operation in progress. Fixes
679 * ump->um_qflags and mp->mnt_flag after.
682 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
684 struct ufsmount *ump;
688 error = quotaoff1(td, mp, type);
692 ump->um_qflags[type] &= ~QTF_CLOSING;
693 for (i = 0; i < MAXQUOTAS; i++)
694 if (ump->um_quotas[i] != NULLVP)
696 if (i == MAXQUOTAS) {
698 mp->mnt_flag &= ~MNT_QUOTA;
706 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
709 quotaoff(struct thread *td, struct mount *mp, int type)
711 struct ufsmount *ump;
714 error = priv_check(td, PRIV_UFS_QUOTAOFF);
720 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
724 ump->um_qflags[type] |= QTF_CLOSING;
727 return (quotaoff_inchange(td, mp, type));
731 * Q_GETQUOTA - return current values in a dqblk structure.
734 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
741 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
742 error = priv_check(td, PRIV_VFS_GETQUOTA);
749 if (!groupmember(id, td->td_ucred) &&
750 !unprivileged_get_quota) {
751 error = priv_check(td, PRIV_VFS_GETQUOTA);
762 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
765 error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
771 * Q_SETQUOTA - assign an entire dqblk structure.
774 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
778 struct ufsmount *ump;
782 error = priv_check(td, PRIV_VFS_SETQUOTA);
787 error = copyin(addr, &newlim, sizeof (struct dqblk));
794 error = dqget(NULLVP, id, ump, type, &ndq);
799 DQI_WAIT(dq, PINOD+1, "setqta");
801 * Copy all but the current values.
802 * Reset time limit if previously had no soft limit or were
803 * under it, but now have a soft limit and are over it.
805 newlim.dqb_curblocks = dq->dq_curblocks;
806 newlim.dqb_curinodes = dq->dq_curinodes;
807 if (dq->dq_id != 0) {
808 newlim.dqb_btime = dq->dq_btime;
809 newlim.dqb_itime = dq->dq_itime;
811 if (newlim.dqb_bsoftlimit &&
812 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
813 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
814 newlim.dqb_btime = time_second + ump->um_btime[type];
815 if (newlim.dqb_isoftlimit &&
816 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
817 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
818 newlim.dqb_itime = time_second + ump->um_itime[type];
820 if (dq->dq_curblocks < dq->dq_bsoftlimit)
821 dq->dq_flags &= ~DQ_BLKS;
822 if (dq->dq_curinodes < dq->dq_isoftlimit)
823 dq->dq_flags &= ~DQ_INODS;
824 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
825 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
826 dq->dq_flags |= DQ_FAKE;
828 dq->dq_flags &= ~DQ_FAKE;
829 dq->dq_flags |= DQ_MOD;
836 * Q_SETUSE - set current inode and block usage.
839 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
842 struct ufsmount *ump;
847 error = priv_check(td, PRIV_UFS_SETUSE);
852 error = copyin(addr, &usage, sizeof (struct dqblk));
859 error = dqget(NULLVP, id, ump, type, &ndq);
864 DQI_WAIT(dq, PINOD+1, "setuse");
866 * Reset time limit if have a soft limit and were
867 * previously under it, but are now over it.
869 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
870 usage.dqb_curblocks >= dq->dq_bsoftlimit)
871 dq->dq_btime = time_second + ump->um_btime[type];
872 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
873 usage.dqb_curinodes >= dq->dq_isoftlimit)
874 dq->dq_itime = time_second + ump->um_itime[type];
875 dq->dq_curblocks = usage.dqb_curblocks;
876 dq->dq_curinodes = usage.dqb_curinodes;
877 if (dq->dq_curblocks < dq->dq_bsoftlimit)
878 dq->dq_flags &= ~DQ_BLKS;
879 if (dq->dq_curinodes < dq->dq_isoftlimit)
880 dq->dq_flags &= ~DQ_INODS;
881 dq->dq_flags |= DQ_MOD;
888 * Q_SYNC - sync quota files to disk.
891 qsync(struct mount *mp)
893 struct ufsmount *ump = VFSTOUFS(mp);
894 struct thread *td = curthread; /* XXX */
895 struct vnode *vp, *mvp;
900 * Check if the mount point has any quotas.
901 * If not, simply return.
904 for (i = 0; i < MAXQUOTAS; i++)
905 if (ump->um_quotas[i] != NULLVP)
911 * Search vnodes associated with this mount point,
912 * synchronizing any modified dquot structures.
916 MNT_VNODE_FOREACH(vp, mp, mvp) {
918 if (vp->v_type == VNON) {
923 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
926 if (error == ENOENT) {
927 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
932 for (i = 0; i < MAXQUOTAS; i++) {
933 dq = VTOI(vp)->i_dquot[i];
945 * Code pertaining to management of the in-core dquot data structures.
947 #define DQHASH(dqvp, id) \
948 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
949 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
950 static u_long dqhash;
955 #define DQUOTINC 5 /* minimum free dquots desired */
956 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
957 static long numdquot, desireddquot = DQUOTINC;
960 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
965 #define DQH_LOCK() mtx_lock(&dqhlock)
966 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
968 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
972 * Initialize the quota system.
978 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
979 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
980 TAILQ_INIT(&dqfreelist);
984 * Shut down the quota system.
991 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
992 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
993 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
994 mtx_destroy(&dq->dq_lock);
997 mtx_destroy(&dqhlock);
1000 static struct dquot *
1001 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1005 mtx_assert(&dqhlock, MA_OWNED);
1006 LIST_FOREACH(dq, dqh, dq_hash) {
1007 if (dq->dq_id != id ||
1008 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1011 * Cache hit with no references. Take
1012 * the structure off the free list.
1014 if (dq->dq_cnt == 0)
1015 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1023 * Obtain a dquot structure for the specified identifier and quota file
1024 * reading the information from the file if necessary.
1027 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1030 struct dquot *dq, *dq1;
1035 int vfslocked, dqvplocked, error;
1037 #ifdef DEBUG_VFS_LOCKS
1039 ASSERT_VOP_ELOCKED(vp, "dqget");
1042 if (vp != NULLVP && *dqp != NODQUOT) {
1046 /* XXX: Disallow negative id values to prevent the
1047 * creation of 100GB+ quota data files.
1053 dqvp = ump->um_quotas[type];
1054 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1065 * Check the cache first.
1067 dqh = DQHASH(dqvp, id);
1069 dq = dqhashfind(dqh, id, dqvp);
1072 hfound: DQI_LOCK(dq);
1073 DQI_WAIT(dq, PINOD+1, "dqget");
1075 if (dq->dq_ump == NULL) {
1081 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1086 VFS_UNLOCK_GIANT(vfslocked);
1091 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1092 * since new dq will appear on the hash chain DQ_LOCKed.
1096 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1100 * Recheck the cache after sleep for quota vnode lock.
1102 dq = dqhashfind(dqh, id, dqvp);
1110 * Not in cache, allocate a new one or take it from the
1113 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1114 numdquot < MAXQUOTAS * desiredvnodes)
1115 desireddquot += DQUOTINC;
1116 if (numdquot < desireddquot) {
1119 dq1 = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
1121 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1124 * Recheck the cache after sleep for memory.
1126 dq = dqhashfind(dqh, id, dqvp);
1130 mtx_destroy(&dq1->dq_lock);
1136 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1140 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1145 VFS_UNLOCK_GIANT(vfslocked);
1148 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1149 panic("dqget: free dquot isn't %p", dq);
1150 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1151 if (dq->dq_ump != NULL)
1152 LIST_REMOVE(dq, dq_hash);
1156 * Dq is put into hash already locked to prevent parallel
1157 * usage while it is being read from file.
1159 dq->dq_flags = DQ_LOCK;
1163 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1167 auio.uio_iov = &aiov;
1168 auio.uio_iovcnt = 1;
1169 aiov.iov_base = &dq->dq_dqb;
1170 aiov.iov_len = sizeof (struct dqblk);
1171 auio.uio_resid = sizeof (struct dqblk);
1172 auio.uio_offset = (off_t)id * sizeof (struct dqblk);
1173 auio.uio_segflg = UIO_SYSSPACE;
1174 auio.uio_rw = UIO_READ;
1175 auio.uio_td = (struct thread *)0;
1177 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1178 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1179 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
1180 bzero(&dq->dq_dqb, sizeof(struct dqblk));
1185 VFS_UNLOCK_GIANT(vfslocked);
1187 * I/O error in reading quota file, release
1188 * quota structure and reflect problem to caller.
1193 LIST_REMOVE(dq, dq_hash);
1196 if (dq->dq_flags & DQ_WANT)
1206 * Check for no limit to enforce.
1207 * Initialize time values if necessary.
1209 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1210 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1211 dq->dq_flags |= DQ_FAKE;
1212 if (dq->dq_id != 0) {
1213 if (dq->dq_btime == 0) {
1214 dq->dq_btime = time_second + ump->um_btime[type];
1215 if (dq->dq_bsoftlimit &&
1216 dq->dq_curblocks >= dq->dq_bsoftlimit)
1217 dq->dq_flags |= DQ_MOD;
1219 if (dq->dq_itime == 0) {
1220 dq->dq_itime = time_second + ump->um_itime[type];
1221 if (dq->dq_isoftlimit &&
1222 dq->dq_curinodes >= dq->dq_isoftlimit)
1223 dq->dq_flags |= DQ_MOD;
1234 * Obtain a reference to a dquot.
1237 dqref(struct dquot *dq)
1245 * Release a reference to a dquot.
1248 dqrele(struct vnode *vp, struct dquot *dq)
1254 if (dq->dq_cnt > 1) {
1261 (void) dqsync(vp, dq);
1264 if (--dq->dq_cnt > 0)
1271 * The dq may become dirty after it is synced but before it is
1272 * put to the free list. Checking the DQ_MOD there without
1273 * locking dq should be safe since no other references to the
1276 if ((dq->dq_flags & DQ_MOD) != 0) {
1281 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1286 * Update the disk quota in the quota file.
1289 dqsync(struct vnode *vp, struct dquot *dq)
1294 int vfslocked, error;
1296 struct ufsmount *ump;
1298 #ifdef DEBUG_VFS_LOCKS
1300 ASSERT_VOP_ELOCKED(vp, "dqsync");
1306 panic("dqsync: dquot");
1307 if ((ump = dq->dq_ump) == NULL)
1310 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP)
1311 panic("dqsync: file");
1315 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1317 if ((dq->dq_flags & DQ_MOD) == 0) {
1320 VFS_UNLOCK_GIANT(vfslocked);
1325 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1327 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1329 VFS_UNLOCK_GIANT(vfslocked);
1331 DQI_WAIT(dq, PINOD+2, "dqsync");
1332 if ((dq->dq_flags & DQ_MOD) == 0)
1334 dq->dq_flags |= DQ_LOCK;
1337 auio.uio_iov = &aiov;
1338 auio.uio_iovcnt = 1;
1339 aiov.iov_base = &dq->dq_dqb;
1340 aiov.iov_len = sizeof (struct dqblk);
1341 auio.uio_resid = sizeof (struct dqblk);
1342 auio.uio_offset = (off_t)dq->dq_id * sizeof (struct dqblk);
1343 auio.uio_segflg = UIO_SYSSPACE;
1344 auio.uio_rw = UIO_WRITE;
1345 auio.uio_td = (struct thread *)0;
1346 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1347 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1348 VFS_UNLOCK_GIANT(vfslocked);
1349 if (auio.uio_resid && error == 0)
1354 dq->dq_flags &= ~DQ_MOD;
1355 out: DQI_UNLOCK(dq);
1356 vfslocked = VFS_LOCK_GIANT(dqvp->v_mount);
1361 vn_finished_secondary_write(mp);
1362 VFS_UNLOCK_GIANT(vfslocked);
1367 * Flush all entries from the cache for a particular vnode.
1370 dqflush(struct vnode *vp)
1372 struct dquot *dq, *nextdq;
1376 * Move all dquot's that used to refer to this quota
1377 * file off their hash chains (they will eventually
1378 * fall off the head of the free list and be re-used).
1381 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1382 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1383 nextdq = LIST_NEXT(dq, dq_hash);
1384 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1387 panic("dqflush: stray dquot");
1388 LIST_REMOVE(dq, dq_hash);
1389 dq->dq_ump = (struct ufsmount *)0;