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/endian.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
52 #include <sys/socket.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/ufs_extern.h>
63 CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64));
65 static int unprivileged_get_quota = 0;
66 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
67 &unprivileged_get_quota, 0,
68 "Unprivileged processes may retrieve quotas for other uids and gids");
70 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
73 * Quota name to error message mapping.
75 static char *quotatypes[] = INITQFNAMES;
77 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
78 static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
79 static int dqopen(struct vnode *, struct ufsmount *, int);
80 static int dqget(struct vnode *,
81 u_long, struct ufsmount *, int, struct dquot **);
82 static int dqsync(struct vnode *, struct dquot *);
83 static int dqflush(struct vnode *);
84 static int quotaoff1(struct thread *td, struct mount *mp, int type);
85 static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
87 /* conversion functions - from_to() */
88 static void dqb32_dq(const struct dqblk32 *, struct dquot *);
89 static void dqb64_dq(const struct dqblk64 *, struct dquot *);
90 static void dq_dqb32(const struct dquot *, struct dqblk32 *);
91 static void dq_dqb64(const struct dquot *, struct dqblk64 *);
92 static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *);
93 static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *);
96 static void dqref(struct dquot *);
97 static void chkdquot(struct inode *);
101 * Set up the quotas for an inode.
103 * This routine completely defines the semantics of quotas.
104 * If other criterion want to be used to establish quotas, the
105 * MAXQUOTAS value in quota.h should be increased, and the
106 * additional dquots set up here.
109 getinoquota(struct inode *ip)
111 struct ufsmount *ump;
118 * Disk quotas must be turned off for system files. Currently
119 * snapshot and quota files.
121 if ((vp->v_vflag & VV_SYSTEM) != 0)
124 * XXX: Turn off quotas for files with a negative UID or GID.
125 * This prevents the creation of 100GB+ quota files.
127 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
129 ump = VFSTOUFS(vp->v_mount);
131 * Set up the user quota based on file uid.
132 * EINVAL means that quotas are not enabled.
135 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
139 * Set up the group quota based on file gid.
140 * EINVAL means that quotas are not enabled.
143 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
150 * Update disk usage, and take corrective action.
153 chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
156 ufs2_daddr_t ncurblocks;
157 struct vnode *vp = ITOV(ip);
158 int i, error, warn, do_check;
161 * Disk quotas must be turned off for system files. Currently
162 * snapshot and quota files.
164 if ((vp->v_vflag & VV_SYSTEM) != 0)
167 * XXX: Turn off quotas for files with a negative UID or GID.
168 * This prevents the creation of 100GB+ quota files.
170 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
173 if ((flags & CHOWN) == 0)
179 for (i = 0; i < MAXQUOTAS; i++) {
180 if ((dq = ip->i_dquot[i]) == NODQUOT)
183 DQI_WAIT(dq, PINOD+1, "chkdq1");
184 ncurblocks = dq->dq_curblocks + change;
186 dq->dq_curblocks = ncurblocks;
188 dq->dq_curblocks = 0;
189 dq->dq_flags &= ~DQ_BLKS;
190 dq->dq_flags |= DQ_MOD;
195 if ((flags & FORCE) == 0 &&
196 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
200 for (i = 0; i < MAXQUOTAS; i++) {
201 if ((dq = ip->i_dquot[i]) == NODQUOT)
205 DQI_WAIT(dq, PINOD+1, "chkdq2");
207 error = chkdqchg(ip, change, cred, i, &warn);
210 * Roll back user quota changes when
211 * group quota failed.
219 DQI_WAIT(dq, PINOD+1, "chkdq3");
220 ncurblocks = dq->dq_curblocks - change;
222 dq->dq_curblocks = ncurblocks;
224 dq->dq_curblocks = 0;
225 dq->dq_flags &= ~DQ_BLKS;
226 dq->dq_flags |= DQ_MOD;
232 /* Reset timer when crossing soft limit */
233 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
234 dq->dq_curblocks < dq->dq_bsoftlimit)
235 dq->dq_btime = time_second + ip->i_ump->um_btime[i];
236 dq->dq_curblocks += change;
237 dq->dq_flags |= DQ_MOD;
240 uprintf("\n%s: warning, %s disk quota exceeded\n",
241 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
248 * Check for a valid change to a users allocation.
249 * Issue an error message if appropriate.
252 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
255 struct dquot *dq = ip->i_dquot[type];
256 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
259 * If user would exceed their hard limit, disallow space allocation.
261 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
262 if ((dq->dq_flags & DQ_BLKS) == 0 &&
263 ip->i_uid == cred->cr_uid) {
264 dq->dq_flags |= DQ_BLKS;
266 uprintf("\n%s: write failed, %s disk limit reached\n",
267 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
275 * If user is over their soft limit for too long, disallow space
276 * allocation. Reset time limit as they cross their soft limit.
278 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
279 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
280 dq->dq_btime = time_second + ip->i_ump->um_btime[type];
281 if (ip->i_uid == cred->cr_uid)
285 if (time_second > dq->dq_btime) {
286 if ((dq->dq_flags & DQ_BLKS) == 0 &&
287 ip->i_uid == cred->cr_uid) {
288 dq->dq_flags |= DQ_BLKS;
290 uprintf("\n%s: write failed, %s "
291 "disk quota exceeded for too long\n",
292 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
304 * Check the inode limit, applying corrective action.
307 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
310 int i, error, warn, do_check;
313 if ((flags & CHOWN) == 0)
319 for (i = 0; i < MAXQUOTAS; i++) {
320 if ((dq = ip->i_dquot[i]) == NODQUOT)
323 DQI_WAIT(dq, PINOD+1, "chkiq1");
324 if (dq->dq_curinodes >= -change)
325 dq->dq_curinodes += change;
327 dq->dq_curinodes = 0;
328 dq->dq_flags &= ~DQ_INODS;
329 dq->dq_flags |= DQ_MOD;
334 if ((flags & FORCE) == 0 &&
335 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
339 for (i = 0; i < MAXQUOTAS; i++) {
340 if ((dq = ip->i_dquot[i]) == NODQUOT)
344 DQI_WAIT(dq, PINOD+1, "chkiq2");
346 error = chkiqchg(ip, change, cred, i, &warn);
349 * Roll back user quota changes when
350 * group quota failed.
358 DQI_WAIT(dq, PINOD+1, "chkiq3");
359 if (dq->dq_curinodes >= change)
360 dq->dq_curinodes -= change;
362 dq->dq_curinodes = 0;
363 dq->dq_flags &= ~DQ_INODS;
364 dq->dq_flags |= DQ_MOD;
370 /* Reset timer when crossing soft limit */
371 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
372 dq->dq_curinodes < dq->dq_isoftlimit)
373 dq->dq_itime = time_second + ip->i_ump->um_itime[i];
374 dq->dq_curinodes += change;
375 dq->dq_flags |= DQ_MOD;
378 uprintf("\n%s: warning, %s inode quota exceeded\n",
379 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
386 * Check for a valid change to a users allocation.
387 * Issue an error message if appropriate.
390 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
392 struct dquot *dq = ip->i_dquot[type];
393 ino_t ncurinodes = dq->dq_curinodes + change;
396 * If user would exceed their hard limit, disallow inode allocation.
398 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
399 if ((dq->dq_flags & DQ_INODS) == 0 &&
400 ip->i_uid == cred->cr_uid) {
401 dq->dq_flags |= DQ_INODS;
403 uprintf("\n%s: write failed, %s inode limit reached\n",
404 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
412 * If user is over their soft limit for too long, disallow inode
413 * allocation. Reset time limit as they cross their soft limit.
415 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
416 if (dq->dq_curinodes < dq->dq_isoftlimit) {
417 dq->dq_itime = time_second + ip->i_ump->um_itime[type];
418 if (ip->i_uid == cred->cr_uid)
422 if (time_second > dq->dq_itime) {
423 if ((dq->dq_flags & DQ_INODS) == 0 &&
424 ip->i_uid == cred->cr_uid) {
425 dq->dq_flags |= DQ_INODS;
427 uprintf("\n%s: write failed, %s "
428 "inode quota exceeded for too long\n",
429 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
442 * On filesystems with quotas enabled, it is an error for a file to change
443 * size and not to have a dquot structure associated with it.
446 chkdquot(struct inode *ip)
448 struct ufsmount *ump = ip->i_ump;
449 struct vnode *vp = ITOV(ip);
453 * Disk quotas must be turned off for system files. Currently
454 * these are snapshots and quota files.
456 if ((vp->v_vflag & VV_SYSTEM) != 0)
459 * XXX: Turn off quotas for files with a negative UID or GID.
460 * This prevents the creation of 100GB+ quota files.
462 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
466 for (i = 0; i < MAXQUOTAS; i++) {
467 if (ump->um_quotas[i] == NULLVP ||
468 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
470 if (ip->i_dquot[i] == NODQUOT) {
472 vprint("chkdquot: missing dquot", ITOV(ip));
473 panic("chkdquot: missing dquot");
481 * Code to process quotactl commands.
485 * Q_QUOTAON - set up a quota file for a particular filesystem.
488 quotaon(struct thread *td, struct mount *mp, int type, void *fname)
490 struct ufsmount *ump;
491 struct vnode *vp, **vpp;
497 error = priv_check(td, PRIV_UFS_QUOTAON);
501 if (mp->mnt_flag & MNT_RDONLY)
507 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
508 flags = FREAD | FWRITE;
511 error = vn_open(&nd, &flags, 0, NULL);
516 NDFREE(&nd, NDF_ONLY_PNBUF);
518 error = vfs_busy(mp, MBF_NOWAIT);
521 if (vp->v_type != VREG) {
528 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
533 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
536 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
540 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
542 if ((error = dqopen(vp, ump, type)) != 0) {
545 ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
547 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
553 mp->mnt_flag |= MNT_QUOTA;
556 vpp = &ump->um_quotas[type];
558 quotaoff1(td, mp, type);
560 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
561 vp->v_vflag |= VV_SYSTEM;
565 * Save the credential of the process that turned on quotas.
566 * Set up the time limits for this quota.
568 ump->um_cred[type] = crhold(td->td_ucred);
569 ump->um_btime[type] = MAX_DQ_TIME;
570 ump->um_itime[type] = MAX_IQ_TIME;
571 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
572 if (dq->dq_btime > 0)
573 ump->um_btime[type] = dq->dq_btime;
574 if (dq->dq_itime > 0)
575 ump->um_itime[type] = dq->dq_itime;
579 * Allow the getdq from getinoquota below to read the quota
583 ump->um_qflags[type] &= ~QTF_CLOSING;
586 * Search vnodes associated with this mount point,
587 * adding references to quota file being opened.
588 * NB: only need to add dquot's for inodes being modified.
591 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
592 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
593 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
596 if (vp->v_type == VNON || vp->v_writecount == 0) {
601 error = getinoquota(VTOI(vp));
605 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
611 quotaoff_inchange(td, mp, type);
613 ump->um_qflags[type] &= ~QTF_OPENING;
614 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
615 ("quotaon: leaking flags"));
623 * Main code to turn off disk quotas for a filesystem. Does not change
627 quotaoff1(struct thread *td, struct mount *mp, int type)
630 struct vnode *qvp, *mvp;
631 struct ufsmount *ump;
640 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
641 ("quotaoff1: flags are invalid"));
642 if ((qvp = ump->um_quotas[type]) == NULLVP) {
646 cr = ump->um_cred[type];
650 * Search vnodes associated with this mount point,
651 * deleting any references to quota file being closed.
654 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
655 if (vp->v_type == VNON) {
659 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
660 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
664 dq = ip->i_dquot[type];
665 ip->i_dquot[type] = NODQUOT;
671 error = dqflush(qvp);
676 * Clear um_quotas before closing the quota vnode to prevent
677 * access to the closed vnode from dqget/dqsync
680 ump->um_quotas[type] = NULLVP;
681 ump->um_cred[type] = NOCRED;
684 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
685 qvp->v_vflag &= ~VV_SYSTEM;
687 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
694 * Turns off quotas, assumes that ump->um_qflags are already checked
695 * and QTF_CLOSING is set to indicate operation in progress. Fixes
696 * ump->um_qflags and mp->mnt_flag after.
699 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
701 struct ufsmount *ump;
705 error = quotaoff1(td, mp, type);
709 ump->um_qflags[type] &= ~QTF_CLOSING;
710 for (i = 0; i < MAXQUOTAS; i++)
711 if (ump->um_quotas[i] != NULLVP)
713 if (i == MAXQUOTAS) {
715 mp->mnt_flag &= ~MNT_QUOTA;
723 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
726 quotaoff(struct thread *td, struct mount *mp, int type)
728 struct ufsmount *ump;
731 error = priv_check(td, PRIV_UFS_QUOTAOFF);
737 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
741 ump->um_qflags[type] |= QTF_CLOSING;
744 return (quotaoff_inchange(td, mp, type));
748 * Q_GETQUOTA - return current values in a dqblk structure.
751 _getquota(struct thread *td, struct mount *mp, u_long id, int type,
759 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
760 error = priv_check(td, PRIV_VFS_GETQUOTA);
767 if (!groupmember(id, td->td_ucred) &&
768 !unprivileged_get_quota) {
769 error = priv_check(td, PRIV_VFS_GETQUOTA);
780 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
789 * Q_SETQUOTA - assign an entire dqblk structure.
792 _setquota(struct thread *td, struct mount *mp, u_long id, int type,
797 struct ufsmount *ump;
798 struct dqblk64 newlim;
801 error = priv_check(td, PRIV_VFS_SETQUOTA);
810 error = dqget(NULLVP, id, ump, type, &ndq);
815 DQI_WAIT(dq, PINOD+1, "setqta");
817 * Copy all but the current values.
818 * Reset time limit if previously had no soft limit or were
819 * under it, but now have a soft limit and are over it.
821 newlim.dqb_curblocks = dq->dq_curblocks;
822 newlim.dqb_curinodes = dq->dq_curinodes;
823 if (dq->dq_id != 0) {
824 newlim.dqb_btime = dq->dq_btime;
825 newlim.dqb_itime = dq->dq_itime;
827 if (newlim.dqb_bsoftlimit &&
828 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
829 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
830 newlim.dqb_btime = time_second + ump->um_btime[type];
831 if (newlim.dqb_isoftlimit &&
832 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
833 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
834 newlim.dqb_itime = time_second + ump->um_itime[type];
836 if (dq->dq_curblocks < dq->dq_bsoftlimit)
837 dq->dq_flags &= ~DQ_BLKS;
838 if (dq->dq_curinodes < dq->dq_isoftlimit)
839 dq->dq_flags &= ~DQ_INODS;
840 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
841 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
842 dq->dq_flags |= DQ_FAKE;
844 dq->dq_flags &= ~DQ_FAKE;
845 dq->dq_flags |= DQ_MOD;
852 * Q_SETUSE - set current inode and block usage.
855 _setuse(struct thread *td, struct mount *mp, u_long id, int type,
859 struct ufsmount *ump;
861 struct dqblk64 usage;
864 error = priv_check(td, PRIV_UFS_SETUSE);
873 error = dqget(NULLVP, id, ump, type, &ndq);
878 DQI_WAIT(dq, PINOD+1, "setuse");
880 * Reset time limit if have a soft limit and were
881 * previously under it, but are now over it.
883 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
884 usage.dqb_curblocks >= dq->dq_bsoftlimit)
885 dq->dq_btime = time_second + ump->um_btime[type];
886 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
887 usage.dqb_curinodes >= dq->dq_isoftlimit)
888 dq->dq_itime = time_second + ump->um_itime[type];
889 dq->dq_curblocks = usage.dqb_curblocks;
890 dq->dq_curinodes = usage.dqb_curinodes;
891 if (dq->dq_curblocks < dq->dq_bsoftlimit)
892 dq->dq_flags &= ~DQ_BLKS;
893 if (dq->dq_curinodes < dq->dq_isoftlimit)
894 dq->dq_flags &= ~DQ_INODS;
895 dq->dq_flags |= DQ_MOD;
902 getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
904 struct dqblk32 dqb32;
905 struct dqblk64 dqb64;
908 error = _getquota(td, mp, id, type, &dqb64);
911 dqb64_dqb32(&dqb64, &dqb32);
912 error = copyout(&dqb32, addr, sizeof(dqb32));
917 setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
919 struct dqblk32 dqb32;
920 struct dqblk64 dqb64;
923 error = copyin(addr, &dqb32, sizeof(dqb32));
926 dqb32_dqb64(&dqb32, &dqb64);
927 error = _setquota(td, mp, id, type, &dqb64);
932 setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
934 struct dqblk32 dqb32;
935 struct dqblk64 dqb64;
938 error = copyin(addr, &dqb32, sizeof(dqb32));
941 dqb32_dqb64(&dqb32, &dqb64);
942 error = _setuse(td, mp, id, type, &dqb64);
947 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
949 struct dqblk64 dqb64;
952 error = _getquota(td, mp, id, type, &dqb64);
955 error = copyout(&dqb64, addr, sizeof(dqb64));
960 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
962 struct dqblk64 dqb64;
965 error = copyin(addr, &dqb64, sizeof(dqb64));
968 error = _setquota(td, mp, id, type, &dqb64);
973 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
975 struct dqblk64 dqb64;
978 error = copyin(addr, &dqb64, sizeof(dqb64));
981 error = _setuse(td, mp, id, type, &dqb64);
986 * Q_GETQUOTASIZE - get bit-size of quota file fields
989 getquotasize(struct thread *td, struct mount *mp, u_long id, int type,
992 struct ufsmount *ump = VFSTOUFS(mp);
996 if (ump->um_quotas[type] == NULLVP ||
997 (ump->um_qflags[type] & QTF_CLOSING)) {
1001 if ((ump->um_qflags[type] & QTF_64BIT) != 0)
1006 return (copyout(&bitsize, sizep, sizeof(int)));
1010 * Q_SYNC - sync quota files to disk.
1013 qsync(struct mount *mp)
1015 struct ufsmount *ump = VFSTOUFS(mp);
1016 struct thread *td = curthread; /* XXX */
1017 struct vnode *vp, *mvp;
1022 * Check if the mount point has any quotas.
1023 * If not, simply return.
1026 for (i = 0; i < MAXQUOTAS; i++)
1027 if (ump->um_quotas[i] != NULLVP)
1033 * Search vnodes associated with this mount point,
1034 * synchronizing any modified dquot structures.
1037 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1038 if (vp->v_type == VNON) {
1042 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
1044 if (error == ENOENT) {
1045 MNT_VNODE_FOREACH_ACTIVE_ABORT(mp, mvp);
1050 for (i = 0; i < MAXQUOTAS; i++) {
1051 dq = VTOI(vp)->i_dquot[i];
1061 * Sync quota file for given vnode to disk.
1064 qsyncvp(struct vnode *vp)
1066 struct ufsmount *ump = VFSTOUFS(vp->v_mount);
1071 * Check if the mount point has any quotas.
1072 * If not, simply return.
1075 for (i = 0; i < MAXQUOTAS; i++)
1076 if (ump->um_quotas[i] != NULLVP)
1082 * Search quotas associated with this vnode
1083 * synchronizing any modified dquot structures.
1085 for (i = 0; i < MAXQUOTAS; i++) {
1086 dq = VTOI(vp)->i_dquot[i];
1094 * Code pertaining to management of the in-core dquot data structures.
1096 #define DQHASH(dqvp, id) \
1097 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
1098 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
1099 static u_long dqhash;
1104 #define DQUOTINC 5 /* minimum free dquots desired */
1105 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
1106 static long numdquot, desireddquot = DQUOTINC;
1109 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
1114 #define DQH_LOCK() mtx_lock(&dqhlock)
1115 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
1117 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
1118 struct vnode *dqvp);
1121 * Initialize the quota system.
1127 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
1128 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
1129 TAILQ_INIT(&dqfreelist);
1133 * Shut down the quota system.
1140 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
1141 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
1142 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1143 mtx_destroy(&dq->dq_lock);
1146 mtx_destroy(&dqhlock);
1149 static struct dquot *
1150 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1154 mtx_assert(&dqhlock, MA_OWNED);
1155 LIST_FOREACH(dq, dqh, dq_hash) {
1156 if (dq->dq_id != id ||
1157 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1160 * Cache hit with no references. Take
1161 * the structure off the free list.
1163 if (dq->dq_cnt == 0)
1164 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1172 * Determine the quota file type.
1174 * A 32-bit quota file is simply an array of struct dqblk32.
1176 * A 64-bit quota file is a struct dqhdr64 followed by an array of struct
1177 * dqblk64. The header contains various magic bits which allow us to be
1178 * reasonably confident that it is indeeda 64-bit quota file and not just
1179 * a 32-bit quota file that just happens to "look right".
1183 dqopen(struct vnode *vp, struct ufsmount *ump, int type)
1190 ASSERT_VOP_LOCKED(vp, "dqopen");
1191 auio.uio_iov = &aiov;
1192 auio.uio_iovcnt = 1;
1193 aiov.iov_base = &dqh;
1194 aiov.iov_len = sizeof(dqh);
1195 auio.uio_resid = sizeof(dqh);
1196 auio.uio_offset = 0;
1197 auio.uio_segflg = UIO_SYSSPACE;
1198 auio.uio_rw = UIO_READ;
1199 auio.uio_td = (struct thread *)0;
1200 error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
1204 if (auio.uio_resid > 0) {
1205 /* assume 32 bits */
1210 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
1211 be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
1212 be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
1213 be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) {
1214 /* XXX: what if the magic matches, but the sizes are wrong? */
1215 ump->um_qflags[type] |= QTF_64BIT;
1217 ump->um_qflags[type] &= ~QTF_64BIT;
1225 * Obtain a dquot structure for the specified identifier and quota file
1226 * reading the information from the file if necessary.
1229 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1232 uint8_t buf[sizeof(struct dqblk64)];
1233 off_t base, recsize;
1234 struct dquot *dq, *dq1;
1239 int dqvplocked, error;
1241 #ifdef DEBUG_VFS_LOCKS
1243 ASSERT_VOP_ELOCKED(vp, "dqget");
1246 if (vp != NULLVP && *dqp != NODQUOT) {
1250 /* XXX: Disallow negative id values to prevent the
1251 * creation of 100GB+ quota data files.
1257 dqvp = ump->um_quotas[type];
1258 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1269 * Check the cache first.
1271 dqh = DQHASH(dqvp, id);
1273 dq = dqhashfind(dqh, id, dqvp);
1276 hfound: DQI_LOCK(dq);
1277 DQI_WAIT(dq, PINOD+1, "dqget");
1279 if (dq->dq_ump == NULL) {
1293 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1294 * since new dq will appear on the hash chain DQ_LOCKed.
1298 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1302 * Recheck the cache after sleep for quota vnode lock.
1304 dq = dqhashfind(dqh, id, dqvp);
1312 * Not in cache, allocate a new one or take it from the
1315 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1316 numdquot < MAXQUOTAS * desiredvnodes)
1317 desireddquot += DQUOTINC;
1318 if (numdquot < desireddquot) {
1321 dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
1322 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1325 * Recheck the cache after sleep for memory.
1327 dq = dqhashfind(dqh, id, dqvp);
1331 mtx_destroy(&dq1->dq_lock);
1337 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1347 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1348 panic("dqget: free dquot isn't %p", dq);
1349 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1350 if (dq->dq_ump != NULL)
1351 LIST_REMOVE(dq, dq_hash);
1355 * Dq is put into hash already locked to prevent parallel
1356 * usage while it is being read from file.
1358 dq->dq_flags = DQ_LOCK;
1362 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1367 * Read the requested quota record from the quota file, performing
1368 * any necessary conversions.
1370 if (ump->um_qflags[type] & QTF_64BIT) {
1371 recsize = sizeof(struct dqblk64);
1372 base = sizeof(struct dqhdr64);
1374 recsize = sizeof(struct dqblk32);
1377 auio.uio_iov = &aiov;
1378 auio.uio_iovcnt = 1;
1379 aiov.iov_base = buf;
1380 aiov.iov_len = recsize;
1381 auio.uio_resid = recsize;
1382 auio.uio_offset = base + id * recsize;
1383 auio.uio_segflg = UIO_SYSSPACE;
1384 auio.uio_rw = UIO_READ;
1385 auio.uio_td = (struct thread *)0;
1387 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1388 if (auio.uio_resid == recsize && error == 0) {
1389 bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
1391 if (ump->um_qflags[type] & QTF_64BIT)
1392 dqb64_dq((struct dqblk64 *)buf, dq);
1394 dqb32_dq((struct dqblk32 *)buf, dq);
1401 * I/O error in reading quota file, release
1402 * quota structure and reflect problem to caller.
1407 LIST_REMOVE(dq, dq_hash);
1410 if (dq->dq_flags & DQ_WANT)
1420 * Check for no limit to enforce.
1421 * Initialize time values if necessary.
1423 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1424 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1425 dq->dq_flags |= DQ_FAKE;
1426 if (dq->dq_id != 0) {
1427 if (dq->dq_btime == 0) {
1428 dq->dq_btime = time_second + ump->um_btime[type];
1429 if (dq->dq_bsoftlimit &&
1430 dq->dq_curblocks >= dq->dq_bsoftlimit)
1431 dq->dq_flags |= DQ_MOD;
1433 if (dq->dq_itime == 0) {
1434 dq->dq_itime = time_second + ump->um_itime[type];
1435 if (dq->dq_isoftlimit &&
1436 dq->dq_curinodes >= dq->dq_isoftlimit)
1437 dq->dq_flags |= DQ_MOD;
1448 * Obtain a reference to a dquot.
1451 dqref(struct dquot *dq)
1459 * Release a reference to a dquot.
1462 dqrele(struct vnode *vp, struct dquot *dq)
1468 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
1469 if (dq->dq_cnt > 1) {
1476 (void) dqsync(vp, dq);
1479 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
1480 if (--dq->dq_cnt > 0)
1487 * The dq may become dirty after it is synced but before it is
1488 * put to the free list. Checking the DQ_MOD there without
1489 * locking dq should be safe since no other references to the
1492 if ((dq->dq_flags & DQ_MOD) != 0) {
1497 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1502 * Update the disk quota in the quota file.
1505 dqsync(struct vnode *vp, struct dquot *dq)
1507 uint8_t buf[sizeof(struct dqblk64)];
1508 off_t base, recsize;
1514 struct ufsmount *ump;
1516 #ifdef DEBUG_VFS_LOCKS
1518 ASSERT_VOP_ELOCKED(vp, "dqsync");
1524 panic("dqsync: dquot");
1525 if ((ump = dq->dq_ump) == NULL)
1528 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP)
1529 panic("dqsync: file");
1534 if ((dq->dq_flags & DQ_MOD) == 0) {
1541 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1543 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1546 DQI_WAIT(dq, PINOD+2, "dqsync");
1547 if ((dq->dq_flags & DQ_MOD) == 0)
1549 dq->dq_flags |= DQ_LOCK;
1553 * Write the quota record to the quota file, performing any
1554 * necessary conversions. See dqget() for additional details.
1556 if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
1557 dq_dqb64(dq, (struct dqblk64 *)buf);
1558 recsize = sizeof(struct dqblk64);
1559 base = sizeof(struct dqhdr64);
1561 dq_dqb32(dq, (struct dqblk32 *)buf);
1562 recsize = sizeof(struct dqblk32);
1566 auio.uio_iov = &aiov;
1567 auio.uio_iovcnt = 1;
1568 aiov.iov_base = buf;
1569 aiov.iov_len = recsize;
1570 auio.uio_resid = recsize;
1571 auio.uio_offset = base + dq->dq_id * recsize;
1572 auio.uio_segflg = UIO_SYSSPACE;
1573 auio.uio_rw = UIO_WRITE;
1574 auio.uio_td = (struct thread *)0;
1575 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1576 if (auio.uio_resid && error == 0)
1581 dq->dq_flags &= ~DQ_MOD;
1588 vn_finished_secondary_write(mp);
1593 * Flush all entries from the cache for a particular vnode.
1596 dqflush(struct vnode *vp)
1598 struct dquot *dq, *nextdq;
1603 * Move all dquot's that used to refer to this quota
1604 * file off their hash chains (they will eventually
1605 * fall off the head of the free list and be re-used).
1609 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1610 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1611 nextdq = LIST_NEXT(dq, dq_hash);
1612 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1617 LIST_REMOVE(dq, dq_hash);
1627 * The following three functions are provided for the adjustment of
1628 * quotas by the soft updates code.
1632 * Acquire a reference to the quota structures associated with a vnode.
1633 * Return count of number of quota structures found.
1644 for (i = 0; i < MAXQUOTAS; i++)
1647 * Disk quotas must be turned off for system files. Currently
1648 * snapshot and quota files.
1650 if ((vp->v_vflag & VV_SYSTEM) != 0)
1653 * Iterate through and copy active quotas.
1658 for (i = 0; i < MAXQUOTAS; i++) {
1659 if ((dq = ip->i_dquot[i]) == NODQUOT)
1665 mtx_unlock(&dqhlock);
1670 * Release a set of quota structures obtained from a vnode.
1679 for (i = 0; i < MAXQUOTAS; i++) {
1680 if ((dq = qrp[i]) == NODQUOT)
1687 * Adjust the number of blocks associated with a quota.
1688 * Positive numbers when adding blocks; negative numbers when freeing blocks.
1691 quotaadj(qrp, ump, blkcount)
1693 struct ufsmount *ump;
1697 ufs2_daddr_t ncurblocks;
1702 for (i = 0; i < MAXQUOTAS; i++) {
1703 if ((dq = qrp[i]) == NODQUOT)
1706 DQI_WAIT(dq, PINOD+1, "adjqta");
1707 ncurblocks = dq->dq_curblocks + blkcount;
1708 if (ncurblocks >= 0)
1709 dq->dq_curblocks = ncurblocks;
1711 dq->dq_curblocks = 0;
1713 dq->dq_flags &= ~DQ_BLKS;
1714 else if (dq->dq_curblocks + blkcount >= dq->dq_bsoftlimit &&
1715 dq->dq_curblocks < dq->dq_bsoftlimit)
1716 dq->dq_btime = time_second + ump->um_btime[i];
1717 dq->dq_flags |= DQ_MOD;
1721 #endif /* SOFTUPDATES */
1724 * 32-bit / 64-bit conversion functions.
1726 * 32-bit quota records are stored in native byte order. Attention must
1727 * be paid to overflow issues.
1729 * 64-bit quota records are stored in network byte order.
1732 #define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
1735 * Convert 32-bit host-order structure to dquot.
1738 dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
1741 dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
1742 dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
1743 dq->dq_curblocks = dqb32->dqb_curblocks;
1744 dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
1745 dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
1746 dq->dq_curinodes = dqb32->dqb_curinodes;
1747 dq->dq_btime = dqb32->dqb_btime;
1748 dq->dq_itime = dqb32->dqb_itime;
1752 * Convert 64-bit network-order structure to dquot.
1755 dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
1758 dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
1759 dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
1760 dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
1761 dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
1762 dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
1763 dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
1764 dq->dq_btime = be64toh(dqb64->dqb_btime);
1765 dq->dq_itime = be64toh(dqb64->dqb_itime);
1769 * Convert dquot to 32-bit host-order structure.
1772 dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
1775 dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
1776 dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
1777 dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
1778 dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
1779 dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
1780 dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
1781 dqb32->dqb_btime = CLIP32(dq->dq_btime);
1782 dqb32->dqb_itime = CLIP32(dq->dq_itime);
1786 * Convert dquot to 64-bit network-order structure.
1789 dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
1792 dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
1793 dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
1794 dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
1795 dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
1796 dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
1797 dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
1798 dqb64->dqb_btime = htobe64(dq->dq_btime);
1799 dqb64->dqb_itime = htobe64(dq->dq_itime);
1803 * Convert 64-bit host-order structure to 32-bit host-order structure.
1806 dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
1809 dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
1810 dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
1811 dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
1812 dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
1813 dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
1814 dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
1815 dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
1816 dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
1820 * Convert 32-bit host-order structure to 64-bit host-order structure.
1823 dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
1826 dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
1827 dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
1828 dqb64->dqb_curblocks = dqb32->dqb_curblocks;
1829 dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
1830 dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
1831 dqb64->dqb_curinodes = dqb32->dqb_curinodes;
1832 dqb64->dqb_btime = dqb32->dqb_btime;
1833 dqb64->dqb_itime = dqb32->dqb_itime;