2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/endian.h>
45 #include <sys/fcntl.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
54 #include <sys/socket.h>
56 #include <sys/sysctl.h>
57 #include <sys/vnode.h>
59 #include <ufs/ufs/extattr.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufsmount.h>
63 #include <ufs/ufs/ufs_extern.h>
65 CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64));
67 static int unprivileged_get_quota = 0;
68 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
69 &unprivileged_get_quota, 0,
70 "Unprivileged processes may retrieve quotas for other uids and gids");
72 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
75 * Quota name to error message mapping.
77 static char *quotatypes[] = INITQFNAMES;
79 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
80 static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
81 static int dqopen(struct vnode *, struct ufsmount *, int);
82 static int dqget(struct vnode *,
83 u_long, struct ufsmount *, int, struct dquot **);
84 static int dqsync(struct vnode *, struct dquot *);
85 static int dqflush(struct vnode *);
86 static int quotaoff1(struct thread *td, struct mount *mp, int type);
87 static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
89 /* conversion functions - from_to() */
90 static void dqb32_dq(const struct dqblk32 *, struct dquot *);
91 static void dqb64_dq(const struct dqblk64 *, struct dquot *);
92 static void dq_dqb32(const struct dquot *, struct dqblk32 *);
93 static void dq_dqb64(const struct dquot *, struct dqblk64 *);
94 static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *);
95 static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *);
98 static void dqref(struct dquot *);
99 static void chkdquot(struct inode *);
103 * Set up the quotas for an inode.
105 * This routine completely defines the semantics of quotas.
106 * If other criterion want to be used to establish quotas, the
107 * MAXQUOTAS value in quota.h should be increased, and the
108 * additional dquots set up here.
111 getinoquota(struct inode *ip)
113 struct ufsmount *ump;
120 * Disk quotas must be turned off for system files. Currently
121 * snapshot and quota files.
123 if ((vp->v_vflag & VV_SYSTEM) != 0)
126 * XXX: Turn off quotas for files with a negative UID or GID.
127 * This prevents the creation of 100GB+ quota files.
129 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
131 ump = VFSTOUFS(vp->v_mount);
133 * Set up the user quota based on file uid.
134 * EINVAL means that quotas are not enabled.
137 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
141 * Set up the group quota based on file gid.
142 * EINVAL means that quotas are not enabled.
145 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
152 * Update disk usage, and take corrective action.
155 chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
158 ufs2_daddr_t ncurblocks;
159 struct vnode *vp = ITOV(ip);
160 int i, error, warn, do_check;
162 MPASS(cred != NOCRED || (flags & FORCE) != 0);
164 * Disk quotas must be turned off for system files. Currently
165 * snapshot and quota files.
167 if ((vp->v_vflag & VV_SYSTEM) != 0)
170 * XXX: Turn off quotas for files with a negative UID or GID.
171 * This prevents the creation of 100GB+ quota files.
173 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
176 if ((flags & CHOWN) == 0)
182 for (i = 0; i < MAXQUOTAS; i++) {
183 if ((dq = ip->i_dquot[i]) == NODQUOT)
186 DQI_WAIT(dq, PINOD+1, "chkdq1");
187 ncurblocks = dq->dq_curblocks + change;
189 dq->dq_curblocks = ncurblocks;
191 dq->dq_curblocks = 0;
192 dq->dq_flags &= ~DQ_BLKS;
193 dq->dq_flags |= DQ_MOD;
198 if ((flags & FORCE) == 0 &&
199 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA))
203 for (i = 0; i < MAXQUOTAS; i++) {
204 if ((dq = ip->i_dquot[i]) == NODQUOT)
208 DQI_WAIT(dq, PINOD+1, "chkdq2");
210 error = chkdqchg(ip, change, cred, i, &warn);
213 * Roll back user quota changes when
214 * group quota failed.
222 DQI_WAIT(dq, PINOD+1, "chkdq3");
223 ncurblocks = dq->dq_curblocks - change;
225 dq->dq_curblocks = ncurblocks;
227 dq->dq_curblocks = 0;
228 dq->dq_flags &= ~DQ_BLKS;
229 dq->dq_flags |= DQ_MOD;
235 /* Reset timer when crossing soft limit */
236 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
237 dq->dq_curblocks < dq->dq_bsoftlimit)
238 dq->dq_btime = time_second + ITOUMP(ip)->um_btime[i];
239 dq->dq_curblocks += change;
240 dq->dq_flags |= DQ_MOD;
243 uprintf("\n%s: warning, %s disk quota exceeded\n",
244 ITOVFS(ip)->mnt_stat.f_mntonname,
251 * Check for a valid change to a users allocation.
252 * Issue an error message if appropriate.
255 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
258 struct dquot *dq = ip->i_dquot[type];
259 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
262 * If user would exceed their hard limit, disallow space allocation.
264 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
265 if ((dq->dq_flags & DQ_BLKS) == 0 &&
266 ip->i_uid == cred->cr_uid) {
267 dq->dq_flags |= DQ_BLKS;
269 uprintf("\n%s: write failed, %s disk limit reached\n",
270 ITOVFS(ip)->mnt_stat.f_mntonname,
278 * If user is over their soft limit for too long, disallow space
279 * allocation. Reset time limit as they cross their soft limit.
281 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
282 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
283 dq->dq_btime = time_second + ITOUMP(ip)->um_btime[type];
284 if (ip->i_uid == cred->cr_uid)
288 if (time_second > dq->dq_btime) {
289 if ((dq->dq_flags & DQ_BLKS) == 0 &&
290 ip->i_uid == cred->cr_uid) {
291 dq->dq_flags |= DQ_BLKS;
293 uprintf("\n%s: write failed, %s "
294 "disk quota exceeded for too long\n",
295 ITOVFS(ip)->mnt_stat.f_mntonname,
307 * Check the inode limit, applying corrective action.
310 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
313 int i, error, warn, do_check;
315 MPASS(cred != NOCRED || (flags & FORCE) != 0);
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 if (dq->dq_curinodes >= -change)
329 dq->dq_curinodes += change;
331 dq->dq_curinodes = 0;
332 dq->dq_flags &= ~DQ_INODS;
333 dq->dq_flags |= DQ_MOD;
338 if ((flags & FORCE) == 0 &&
339 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA))
343 for (i = 0; i < MAXQUOTAS; i++) {
344 if ((dq = ip->i_dquot[i]) == NODQUOT)
348 DQI_WAIT(dq, PINOD+1, "chkiq2");
350 error = chkiqchg(ip, change, cred, i, &warn);
353 * Roll back user quota changes when
354 * group quota failed.
362 DQI_WAIT(dq, PINOD+1, "chkiq3");
363 if (dq->dq_curinodes >= change)
364 dq->dq_curinodes -= change;
366 dq->dq_curinodes = 0;
367 dq->dq_flags &= ~DQ_INODS;
368 dq->dq_flags |= DQ_MOD;
374 /* Reset timer when crossing soft limit */
375 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
376 dq->dq_curinodes < dq->dq_isoftlimit)
377 dq->dq_itime = time_second + ITOUMP(ip)->um_itime[i];
378 dq->dq_curinodes += change;
379 dq->dq_flags |= DQ_MOD;
382 uprintf("\n%s: warning, %s inode quota exceeded\n",
383 ITOVFS(ip)->mnt_stat.f_mntonname,
390 * Check for a valid change to a users allocation.
391 * Issue an error message if appropriate.
394 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
396 struct dquot *dq = ip->i_dquot[type];
397 ino_t ncurinodes = dq->dq_curinodes + change;
400 * If user would exceed their hard limit, disallow inode allocation.
402 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
403 if ((dq->dq_flags & DQ_INODS) == 0 &&
404 ip->i_uid == cred->cr_uid) {
405 dq->dq_flags |= DQ_INODS;
407 uprintf("\n%s: write failed, %s inode limit reached\n",
408 ITOVFS(ip)->mnt_stat.f_mntonname,
416 * If user is over their soft limit for too long, disallow inode
417 * allocation. Reset time limit as they cross their soft limit.
419 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
420 if (dq->dq_curinodes < dq->dq_isoftlimit) {
421 dq->dq_itime = time_second + ITOUMP(ip)->um_itime[type];
422 if (ip->i_uid == cred->cr_uid)
426 if (time_second > dq->dq_itime) {
427 if ((dq->dq_flags & DQ_INODS) == 0 &&
428 ip->i_uid == cred->cr_uid) {
429 dq->dq_flags |= DQ_INODS;
431 uprintf("\n%s: write failed, %s "
432 "inode quota exceeded for too long\n",
433 ITOVFS(ip)->mnt_stat.f_mntonname,
446 * On filesystems with quotas enabled, it is an error for a file to change
447 * size and not to have a dquot structure associated with it.
450 chkdquot(struct inode *ip)
452 struct ufsmount *ump;
460 * Disk quotas must be turned off for system files. Currently
461 * these are snapshots and quota files.
463 if ((vp->v_vflag & VV_SYSTEM) != 0)
466 * XXX: Turn off quotas for files with a negative UID or GID.
467 * This prevents the creation of 100GB+ quota files.
469 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
473 for (i = 0; i < MAXQUOTAS; i++) {
474 if (ump->um_quotas[i] == NULLVP ||
475 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
477 if (ip->i_dquot[i] == NODQUOT) {
479 vn_printf(ITOV(ip), "chkdquot: missing dquot ");
480 panic("chkdquot: missing dquot");
488 * Code to process quotactl commands.
492 * Q_QUOTAON - set up a quota file for a particular filesystem.
495 quotaon(struct thread *td, struct mount *mp, int type, void *fname,
498 struct ufsmount *ump;
499 struct vnode *vp, **vpp;
505 error = priv_check(td, PRIV_UFS_QUOTAON);
509 if ((mp->mnt_flag & MNT_RDONLY) != 0)
515 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname);
516 flags = FREAD | FWRITE;
518 KASSERT(*mp_busy, ("%s called without busied mount", __func__));
521 error = vn_open(&nd, &flags, 0, NULL);
528 error = vfs_busy(mp, MBF_NOWAIT);
532 if (vp->v_type != VREG)
537 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
542 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
545 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
548 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
550 if ((error = dqopen(vp, ump, type)) != 0) {
553 ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
555 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
560 mp->mnt_flag |= MNT_QUOTA;
561 mp->mnt_stat.f_flags |= MNT_QUOTA;
564 vpp = &ump->um_quotas[type];
566 quotaoff1(td, mp, type);
569 * When the directory vnode containing the quota file is
570 * inactivated, due to the shared lookup of the quota file
571 * vput()ing the dvp, the qsyncvp() call for the containing
572 * directory would try to acquire the quota lock exclusive.
573 * At the same time, lookup already locked the quota vnode
574 * shared. Mark the quota vnode lock as allowing recursion
575 * and automatically converting shared locks to exclusive.
577 * Also mark quota vnode as system.
579 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
580 vp->v_vflag |= VV_SYSTEM;
586 * Save the credential of the process that turned on quotas.
587 * Set up the time limits for this quota.
589 ump->um_cred[type] = crhold(td->td_ucred);
590 ump->um_btime[type] = MAX_DQ_TIME;
591 ump->um_itime[type] = MAX_IQ_TIME;
592 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
593 if (dq->dq_btime > 0)
594 ump->um_btime[type] = dq->dq_btime;
595 if (dq->dq_itime > 0)
596 ump->um_itime[type] = dq->dq_itime;
600 * Allow the getdq from getinoquota below to read the quota
604 ump->um_qflags[type] &= ~QTF_CLOSING;
607 * Search vnodes associated with this mount point,
608 * adding references to quota file being opened.
609 * NB: only need to add dquot's for inodes being modified.
612 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
613 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
614 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
617 if (vp->v_type == VNON || vp->v_writecount <= 0) {
621 error = getinoquota(VTOI(vp));
624 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
630 quotaoff_inchange(td, mp, type);
632 ump->um_qflags[type] &= ~QTF_OPENING;
633 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
634 ("quotaon: leaking flags"));
641 * Main code to turn off disk quotas for a filesystem. Does not change
645 quotaoff1(struct thread *td, struct mount *mp, int type)
648 struct vnode *qvp, *mvp;
649 struct ufsmount *ump;
658 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
659 ("quotaoff1: flags are invalid"));
660 if ((qvp = ump->um_quotas[type]) == NULLVP) {
664 cr = ump->um_cred[type];
668 * Search vnodes associated with this mount point,
669 * deleting any references to quota file being closed.
672 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
673 if (vp->v_type == VNON) {
677 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
678 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
682 dq = ip->i_dquot[type];
683 ip->i_dquot[type] = NODQUOT;
688 error = dqflush(qvp);
693 * Clear um_quotas before closing the quota vnode to prevent
694 * access to the closed vnode from dqget/dqsync
697 ump->um_quotas[type] = NULLVP;
698 ump->um_cred[type] = NOCRED;
701 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
702 qvp->v_vflag &= ~VV_SYSTEM;
704 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
711 quotaoff_inchange1(struct thread *td, struct mount *mp, int type)
717 * mp is already suspended on unmount. If not, suspend it, to
718 * avoid the situation where quotaoff operation eventually
719 * failing due to SU structures still keeping references on
720 * dquots, but vnode's references are already clean. This
721 * would cause quota accounting leak and asserts otherwise.
722 * Note that the thread has already called vn_start_write().
724 if (mp->mnt_susp_owner == td) {
727 error = vfs_write_suspend_umnt(mp);
732 error = quotaoff1(td, mp, type);
734 vfs_write_resume(mp, VR_START_WRITE);
739 * Turns off quotas, assumes that ump->um_qflags are already checked
740 * and QTF_CLOSING is set to indicate operation in progress. Fixes
741 * ump->um_qflags and mp->mnt_flag after.
744 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
746 struct ufsmount *ump;
749 error = quotaoff_inchange1(td, mp, type);
753 ump->um_qflags[type] &= ~QTF_CLOSING;
754 for (i = 0; i < MAXQUOTAS; i++)
755 if (ump->um_quotas[i] != NULLVP)
757 if (i == MAXQUOTAS) {
759 mp->mnt_flag &= ~MNT_QUOTA;
760 mp->mnt_stat.f_flags &= ~MNT_QUOTA;
768 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
771 quotaoff(struct thread *td, struct mount *mp, int type)
773 struct ufsmount *ump;
776 error = priv_check(td, PRIV_UFS_QUOTAOFF);
782 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
786 ump->um_qflags[type] |= QTF_CLOSING;
789 return (quotaoff_inchange(td, mp, type));
793 * Q_GETQUOTA - return current values in a dqblk structure.
796 _getquota(struct thread *td, struct mount *mp, u_long id, int type,
804 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
805 error = priv_check(td, PRIV_VFS_GETQUOTA);
812 if (!groupmember(id, td->td_ucred) &&
813 !unprivileged_get_quota) {
814 error = priv_check(td, PRIV_VFS_GETQUOTA);
825 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
834 * Q_SETQUOTA - assign an entire dqblk structure.
837 _setquota(struct thread *td, struct mount *mp, u_long id, int type,
842 struct ufsmount *ump;
843 struct dqblk64 newlim;
846 error = priv_check(td, PRIV_VFS_SETQUOTA);
855 error = dqget(NULLVP, id, ump, type, &ndq);
860 DQI_WAIT(dq, PINOD+1, "setqta");
862 * Copy all but the current values.
863 * Reset time limit if previously had no soft limit or were
864 * under it, but now have a soft limit and are over it.
866 newlim.dqb_curblocks = dq->dq_curblocks;
867 newlim.dqb_curinodes = dq->dq_curinodes;
868 if (dq->dq_id != 0) {
869 newlim.dqb_btime = dq->dq_btime;
870 newlim.dqb_itime = dq->dq_itime;
872 if (newlim.dqb_bsoftlimit &&
873 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
874 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
875 newlim.dqb_btime = time_second + ump->um_btime[type];
876 if (newlim.dqb_isoftlimit &&
877 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
878 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
879 newlim.dqb_itime = time_second + ump->um_itime[type];
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 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
886 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
887 dq->dq_flags |= DQ_FAKE;
889 dq->dq_flags &= ~DQ_FAKE;
890 dq->dq_flags |= DQ_MOD;
897 * Q_SETUSE - set current inode and block usage.
900 _setuse(struct thread *td, struct mount *mp, u_long id, int type,
904 struct ufsmount *ump;
906 struct dqblk64 usage;
909 error = priv_check(td, PRIV_UFS_SETUSE);
918 error = dqget(NULLVP, id, ump, type, &ndq);
923 DQI_WAIT(dq, PINOD+1, "setuse");
925 * Reset time limit if have a soft limit and were
926 * previously under it, but are now over it.
928 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
929 usage.dqb_curblocks >= dq->dq_bsoftlimit)
930 dq->dq_btime = time_second + ump->um_btime[type];
931 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
932 usage.dqb_curinodes >= dq->dq_isoftlimit)
933 dq->dq_itime = time_second + ump->um_itime[type];
934 dq->dq_curblocks = usage.dqb_curblocks;
935 dq->dq_curinodes = usage.dqb_curinodes;
936 if (dq->dq_curblocks < dq->dq_bsoftlimit)
937 dq->dq_flags &= ~DQ_BLKS;
938 if (dq->dq_curinodes < dq->dq_isoftlimit)
939 dq->dq_flags &= ~DQ_INODS;
940 dq->dq_flags |= DQ_MOD;
947 getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
949 struct dqblk32 dqb32;
950 struct dqblk64 dqb64;
953 error = _getquota(td, mp, id, type, &dqb64);
956 dqb64_dqb32(&dqb64, &dqb32);
957 error = copyout(&dqb32, addr, sizeof(dqb32));
962 setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
964 struct dqblk32 dqb32;
965 struct dqblk64 dqb64;
968 error = copyin(addr, &dqb32, sizeof(dqb32));
971 dqb32_dqb64(&dqb32, &dqb64);
972 error = _setquota(td, mp, id, type, &dqb64);
977 setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
979 struct dqblk32 dqb32;
980 struct dqblk64 dqb64;
983 error = copyin(addr, &dqb32, sizeof(dqb32));
986 dqb32_dqb64(&dqb32, &dqb64);
987 error = _setuse(td, mp, id, type, &dqb64);
992 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
994 struct dqblk64 dqb64;
997 error = _getquota(td, mp, id, type, &dqb64);
1000 error = copyout(&dqb64, addr, sizeof(dqb64));
1005 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
1007 struct dqblk64 dqb64;
1010 error = copyin(addr, &dqb64, sizeof(dqb64));
1013 error = _setquota(td, mp, id, type, &dqb64);
1018 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
1020 struct dqblk64 dqb64;
1023 error = copyin(addr, &dqb64, sizeof(dqb64));
1026 error = _setuse(td, mp, id, type, &dqb64);
1031 * Q_GETQUOTASIZE - get bit-size of quota file fields
1034 getquotasize(struct thread *td, struct mount *mp, u_long id, int type,
1037 struct ufsmount *ump = VFSTOUFS(mp);
1041 if (ump->um_quotas[type] == NULLVP ||
1042 (ump->um_qflags[type] & QTF_CLOSING)) {
1046 if ((ump->um_qflags[type] & QTF_64BIT) != 0)
1051 return (copyout(&bitsize, sizep, sizeof(int)));
1055 * Q_SYNC - sync quota files to disk.
1058 qsync(struct mount *mp)
1060 struct ufsmount *ump = VFSTOUFS(mp);
1061 struct vnode *vp, *mvp;
1066 * Check if the mount point has any quotas.
1067 * If not, simply return.
1069 for (i = 0; i < MAXQUOTAS; i++)
1070 if (ump->um_quotas[i] != NULLVP)
1075 * Search vnodes associated with this mount point,
1076 * synchronizing any modified dquot structures.
1079 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1080 if (vp->v_type == VNON) {
1084 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK);
1086 if (error == ENOENT) {
1087 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1092 for (i = 0; i < MAXQUOTAS; i++) {
1093 dq = VTOI(vp)->i_dquot[i];
1103 * Sync quota file for given vnode to disk.
1106 qsyncvp(struct vnode *vp)
1108 struct ufsmount *ump = VFSTOUFS(vp->v_mount);
1113 * Check if the mount point has any quotas.
1114 * If not, simply return.
1116 for (i = 0; i < MAXQUOTAS; i++)
1117 if (ump->um_quotas[i] != NULLVP)
1122 * Search quotas associated with this vnode
1123 * synchronizing any modified dquot structures.
1125 for (i = 0; i < MAXQUOTAS; i++) {
1126 dq = VTOI(vp)->i_dquot[i];
1134 * Code pertaining to management of the in-core dquot data structures.
1136 #define DQHASH(dqvp, id) \
1137 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
1138 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
1139 static u_long dqhash;
1144 #define DQUOTINC 5 /* minimum free dquots desired */
1145 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
1146 static long numdquot, desireddquot = DQUOTINC;
1149 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
1154 #define DQH_LOCK() mtx_lock(&dqhlock)
1155 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
1157 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
1158 struct vnode *dqvp);
1161 * Initialize the quota system.
1167 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
1168 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
1169 TAILQ_INIT(&dqfreelist);
1173 * Shut down the quota system.
1180 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
1181 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
1182 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1183 mtx_destroy(&dq->dq_lock);
1186 mtx_destroy(&dqhlock);
1189 static struct dquot *
1190 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1194 mtx_assert(&dqhlock, MA_OWNED);
1195 LIST_FOREACH(dq, dqh, dq_hash) {
1196 if (dq->dq_id != id ||
1197 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1200 * Cache hit with no references. Take
1201 * the structure off the free list.
1203 if (dq->dq_cnt == 0)
1204 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1212 * Determine the quota file type.
1214 * A 32-bit quota file is simply an array of struct dqblk32.
1216 * A 64-bit quota file is a struct dqhdr64 followed by an array of struct
1217 * dqblk64. The header contains various magic bits which allow us to be
1218 * reasonably confident that it is indeeda 64-bit quota file and not just
1219 * a 32-bit quota file that just happens to "look right".
1223 dqopen(struct vnode *vp, struct ufsmount *ump, int type)
1230 ASSERT_VOP_LOCKED(vp, "dqopen");
1231 auio.uio_iov = &aiov;
1232 auio.uio_iovcnt = 1;
1233 aiov.iov_base = &dqh;
1234 aiov.iov_len = sizeof(dqh);
1235 auio.uio_resid = sizeof(dqh);
1236 auio.uio_offset = 0;
1237 auio.uio_segflg = UIO_SYSSPACE;
1238 auio.uio_rw = UIO_READ;
1239 auio.uio_td = (struct thread *)0;
1240 error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
1244 if (auio.uio_resid > 0) {
1245 /* assume 32 bits */
1250 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
1251 be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
1252 be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
1253 be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) {
1254 /* XXX: what if the magic matches, but the sizes are wrong? */
1255 ump->um_qflags[type] |= QTF_64BIT;
1257 ump->um_qflags[type] &= ~QTF_64BIT;
1265 * Obtain a dquot structure for the specified identifier and quota file
1266 * reading the information from the file if necessary.
1269 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1272 uint8_t buf[sizeof(struct dqblk64)];
1273 off_t base, recsize;
1274 struct dquot *dq, *dq1;
1279 int dqvplocked, error;
1281 #ifdef DEBUG_VFS_LOCKS
1283 ASSERT_VOP_ELOCKED(vp, "dqget");
1286 if (vp != NULLVP && *dqp != NODQUOT) {
1290 /* XXX: Disallow negative id values to prevent the
1291 * creation of 100GB+ quota data files.
1297 dqvp = ump->um_quotas[type];
1298 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1309 * Check the cache first.
1311 dqh = DQHASH(dqvp, id);
1313 dq = dqhashfind(dqh, id, dqvp);
1316 hfound: DQI_LOCK(dq);
1317 DQI_WAIT(dq, PINOD+1, "dqget");
1319 if (dq->dq_ump == NULL) {
1333 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1334 * since new dq will appear on the hash chain DQ_LOCKed.
1338 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1342 * Recheck the cache after sleep for quota vnode lock.
1344 dq = dqhashfind(dqh, id, dqvp);
1352 * Not in cache, allocate a new one or take it from the
1355 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1356 numdquot < MAXQUOTAS * desiredvnodes)
1357 desireddquot += DQUOTINC;
1358 if (numdquot < desireddquot) {
1361 dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
1362 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1365 * Recheck the cache after sleep for memory.
1367 dq = dqhashfind(dqh, id, dqvp);
1371 mtx_destroy(&dq1->dq_lock);
1377 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1387 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1388 panic("dqget: free dquot isn't %p", dq);
1389 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1390 if (dq->dq_ump != NULL)
1391 LIST_REMOVE(dq, dq_hash);
1395 * Dq is put into hash already locked to prevent parallel
1396 * usage while it is being read from file.
1398 dq->dq_flags = DQ_LOCK;
1402 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1407 * Read the requested quota record from the quota file, performing
1408 * any necessary conversions.
1410 if (ump->um_qflags[type] & QTF_64BIT) {
1411 recsize = sizeof(struct dqblk64);
1412 base = sizeof(struct dqhdr64);
1414 recsize = sizeof(struct dqblk32);
1417 auio.uio_iov = &aiov;
1418 auio.uio_iovcnt = 1;
1419 aiov.iov_base = buf;
1420 aiov.iov_len = recsize;
1421 auio.uio_resid = recsize;
1422 auio.uio_offset = base + id * recsize;
1423 auio.uio_segflg = UIO_SYSSPACE;
1424 auio.uio_rw = UIO_READ;
1425 auio.uio_td = (struct thread *)0;
1427 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1428 if (auio.uio_resid == recsize && error == 0) {
1429 bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
1431 if (ump->um_qflags[type] & QTF_64BIT)
1432 dqb64_dq((struct dqblk64 *)buf, dq);
1434 dqb32_dq((struct dqblk32 *)buf, dq);
1441 * I/O error in reading quota file, release
1442 * quota structure and reflect problem to caller.
1447 LIST_REMOVE(dq, dq_hash);
1450 if (dq->dq_flags & DQ_WANT)
1460 * Check for no limit to enforce.
1461 * Initialize time values if necessary.
1463 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1464 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1465 dq->dq_flags |= DQ_FAKE;
1466 if (dq->dq_id != 0) {
1467 if (dq->dq_btime == 0) {
1468 dq->dq_btime = time_second + ump->um_btime[type];
1469 if (dq->dq_bsoftlimit &&
1470 dq->dq_curblocks >= dq->dq_bsoftlimit)
1471 dq->dq_flags |= DQ_MOD;
1473 if (dq->dq_itime == 0) {
1474 dq->dq_itime = time_second + ump->um_itime[type];
1475 if (dq->dq_isoftlimit &&
1476 dq->dq_curinodes >= dq->dq_isoftlimit)
1477 dq->dq_flags |= DQ_MOD;
1488 * Obtain a reference to a dquot.
1491 dqref(struct dquot *dq)
1499 * Release a reference to a dquot.
1502 dqrele(struct vnode *vp, struct dquot *dq)
1508 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
1509 if (dq->dq_cnt > 1) {
1516 (void) dqsync(vp, dq);
1519 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
1520 if (--dq->dq_cnt > 0)
1527 * The dq may become dirty after it is synced but before it is
1528 * put to the free list. Checking the DQ_MOD there without
1529 * locking dq should be safe since no other references to the
1532 if ((dq->dq_flags & DQ_MOD) != 0) {
1537 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1542 * Update the disk quota in the quota file.
1545 dqsync(struct vnode *vp, struct dquot *dq)
1547 uint8_t buf[sizeof(struct dqblk64)];
1548 off_t base, recsize;
1554 struct ufsmount *ump;
1556 #ifdef DEBUG_VFS_LOCKS
1558 ASSERT_VOP_ELOCKED(vp, "dqsync");
1564 panic("dqsync: dquot");
1565 if ((ump = dq->dq_ump) == NULL)
1568 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP) {
1573 panic("dqsync: file");
1579 if ((dq->dq_flags & DQ_MOD) == 0) {
1586 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1588 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1591 DQI_WAIT(dq, PINOD+2, "dqsync");
1592 if ((dq->dq_flags & DQ_MOD) == 0)
1594 dq->dq_flags |= DQ_LOCK;
1598 * Write the quota record to the quota file, performing any
1599 * necessary conversions. See dqget() for additional details.
1601 if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
1602 dq_dqb64(dq, (struct dqblk64 *)buf);
1603 recsize = sizeof(struct dqblk64);
1604 base = sizeof(struct dqhdr64);
1606 dq_dqb32(dq, (struct dqblk32 *)buf);
1607 recsize = sizeof(struct dqblk32);
1611 auio.uio_iov = &aiov;
1612 auio.uio_iovcnt = 1;
1613 aiov.iov_base = buf;
1614 aiov.iov_len = recsize;
1615 auio.uio_resid = recsize;
1616 auio.uio_offset = base + dq->dq_id * recsize;
1617 auio.uio_segflg = UIO_SYSSPACE;
1618 auio.uio_rw = UIO_WRITE;
1619 auio.uio_td = (struct thread *)0;
1620 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1621 if (auio.uio_resid && error == 0)
1626 dq->dq_flags &= ~DQ_MOD;
1633 vn_finished_secondary_write(mp);
1638 * Flush all entries from the cache for a particular vnode.
1641 dqflush(struct vnode *vp)
1643 struct dquot *dq, *nextdq;
1648 * Move all dquot's that used to refer to this quota
1649 * file off their hash chains (they will eventually
1650 * fall off the head of the free list and be re-used).
1654 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1655 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1656 nextdq = LIST_NEXT(dq, dq_hash);
1657 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1662 LIST_REMOVE(dq, dq_hash);
1672 * The following three functions are provided for the adjustment of
1673 * quotas by the soft updates code.
1677 * Acquire a reference to the quota structures associated with a vnode.
1678 * Return count of number of quota structures found.
1681 quotaref(struct vnode *vp, struct dquot **qrp)
1687 for (i = 0; i < MAXQUOTAS; i++)
1690 * Disk quotas must be turned off for system files. Currently
1691 * snapshot and quota files.
1693 if ((vp->v_vflag & VV_SYSTEM) != 0)
1696 * Iterate through and copy active quotas.
1701 for (i = 0; i < MAXQUOTAS; i++) {
1702 if ((dq = ip->i_dquot[i]) == NODQUOT)
1708 mtx_unlock(&dqhlock);
1713 * Release a set of quota structures obtained from a vnode.
1716 quotarele(struct dquot **qrp)
1721 for (i = 0; i < MAXQUOTAS; i++) {
1722 if ((dq = qrp[i]) == NODQUOT)
1729 * Adjust the number of blocks associated with a quota.
1730 * Positive numbers when adding blocks; negative numbers when freeing blocks.
1733 quotaadj(struct dquot **qrp, struct ufsmount *ump, int64_t blkcount)
1736 ufs2_daddr_t ncurblocks;
1741 for (i = 0; i < MAXQUOTAS; i++) {
1742 if ((dq = qrp[i]) == NODQUOT)
1745 DQI_WAIT(dq, PINOD+1, "adjqta");
1746 ncurblocks = dq->dq_curblocks + blkcount;
1747 if (ncurblocks >= 0)
1748 dq->dq_curblocks = ncurblocks;
1750 dq->dq_curblocks = 0;
1752 dq->dq_flags &= ~DQ_BLKS;
1753 else if (dq->dq_curblocks + blkcount >= dq->dq_bsoftlimit &&
1754 dq->dq_curblocks < dq->dq_bsoftlimit)
1755 dq->dq_btime = time_second + ump->um_btime[i];
1756 dq->dq_flags |= DQ_MOD;
1760 #endif /* SOFTUPDATES */
1763 * 32-bit / 64-bit conversion functions.
1765 * 32-bit quota records are stored in native byte order. Attention must
1766 * be paid to overflow issues.
1768 * 64-bit quota records are stored in network byte order.
1771 #define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
1774 * Convert 32-bit host-order structure to dquot.
1777 dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
1780 dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
1781 dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
1782 dq->dq_curblocks = dqb32->dqb_curblocks;
1783 dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
1784 dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
1785 dq->dq_curinodes = dqb32->dqb_curinodes;
1786 dq->dq_btime = dqb32->dqb_btime;
1787 dq->dq_itime = dqb32->dqb_itime;
1791 * Convert 64-bit network-order structure to dquot.
1794 dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
1797 dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
1798 dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
1799 dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
1800 dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
1801 dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
1802 dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
1803 dq->dq_btime = be64toh(dqb64->dqb_btime);
1804 dq->dq_itime = be64toh(dqb64->dqb_itime);
1808 * Convert dquot to 32-bit host-order structure.
1811 dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
1814 dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
1815 dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
1816 dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
1817 dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
1818 dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
1819 dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
1820 dqb32->dqb_btime = CLIP32(dq->dq_btime);
1821 dqb32->dqb_itime = CLIP32(dq->dq_itime);
1825 * Convert dquot to 64-bit network-order structure.
1828 dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
1831 dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
1832 dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
1833 dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
1834 dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
1835 dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
1836 dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
1837 dqb64->dqb_btime = htobe64(dq->dq_btime);
1838 dqb64->dqb_itime = htobe64(dq->dq_itime);
1842 * Convert 64-bit host-order structure to 32-bit host-order structure.
1845 dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
1848 dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
1849 dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
1850 dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
1851 dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
1852 dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
1853 dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
1854 dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
1855 dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
1859 * Convert 32-bit host-order structure to 64-bit host-order structure.
1862 dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
1865 dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
1866 dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
1867 dqb64->dqb_curblocks = dqb32->dqb_curblocks;
1868 dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
1869 dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
1870 dqb64->dqb_curinodes = dqb32->dqb_curinodes;
1871 dqb64->dqb_btime = dqb32->dqb_btime;
1872 dqb64->dqb_itime = dqb32->dqb_itime;