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;
163 * Disk quotas must be turned off for system files. Currently
164 * snapshot and quota files.
166 if ((vp->v_vflag & VV_SYSTEM) != 0)
169 * XXX: Turn off quotas for files with a negative UID or GID.
170 * This prevents the creation of 100GB+ quota files.
172 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
175 if ((flags & CHOWN) == 0)
181 for (i = 0; i < MAXQUOTAS; i++) {
182 if ((dq = ip->i_dquot[i]) == NODQUOT)
185 DQI_WAIT(dq, PINOD+1, "chkdq1");
186 ncurblocks = dq->dq_curblocks + change;
188 dq->dq_curblocks = ncurblocks;
190 dq->dq_curblocks = 0;
191 dq->dq_flags &= ~DQ_BLKS;
192 dq->dq_flags |= DQ_MOD;
197 if ((flags & FORCE) == 0 &&
198 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
202 for (i = 0; i < MAXQUOTAS; i++) {
203 if ((dq = ip->i_dquot[i]) == NODQUOT)
207 DQI_WAIT(dq, PINOD+1, "chkdq2");
209 error = chkdqchg(ip, change, cred, i, &warn);
212 * Roll back user quota changes when
213 * group quota failed.
221 DQI_WAIT(dq, PINOD+1, "chkdq3");
222 ncurblocks = dq->dq_curblocks - change;
224 dq->dq_curblocks = ncurblocks;
226 dq->dq_curblocks = 0;
227 dq->dq_flags &= ~DQ_BLKS;
228 dq->dq_flags |= DQ_MOD;
234 /* Reset timer when crossing soft limit */
235 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
236 dq->dq_curblocks < dq->dq_bsoftlimit)
237 dq->dq_btime = time_second + ITOUMP(ip)->um_btime[i];
238 dq->dq_curblocks += change;
239 dq->dq_flags |= DQ_MOD;
242 uprintf("\n%s: warning, %s disk quota exceeded\n",
243 ITOVFS(ip)->mnt_stat.f_mntonname,
250 * Check for a valid change to a users allocation.
251 * Issue an error message if appropriate.
254 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
257 struct dquot *dq = ip->i_dquot[type];
258 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
261 * If user would exceed their hard limit, disallow space allocation.
263 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
264 if ((dq->dq_flags & DQ_BLKS) == 0 &&
265 ip->i_uid == cred->cr_uid) {
266 dq->dq_flags |= DQ_BLKS;
268 uprintf("\n%s: write failed, %s disk limit reached\n",
269 ITOVFS(ip)->mnt_stat.f_mntonname,
277 * If user is over their soft limit for too long, disallow space
278 * allocation. Reset time limit as they cross their soft limit.
280 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
281 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
282 dq->dq_btime = time_second + ITOUMP(ip)->um_btime[type];
283 if (ip->i_uid == cred->cr_uid)
287 if (time_second > dq->dq_btime) {
288 if ((dq->dq_flags & DQ_BLKS) == 0 &&
289 ip->i_uid == cred->cr_uid) {
290 dq->dq_flags |= DQ_BLKS;
292 uprintf("\n%s: write failed, %s "
293 "disk quota exceeded for too long\n",
294 ITOVFS(ip)->mnt_stat.f_mntonname,
306 * Check the inode limit, applying corrective action.
309 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
312 int i, error, warn, do_check;
315 if ((flags & CHOWN) == 0)
321 for (i = 0; i < MAXQUOTAS; i++) {
322 if ((dq = ip->i_dquot[i]) == NODQUOT)
325 DQI_WAIT(dq, PINOD+1, "chkiq1");
326 if (dq->dq_curinodes >= -change)
327 dq->dq_curinodes += change;
329 dq->dq_curinodes = 0;
330 dq->dq_flags &= ~DQ_INODS;
331 dq->dq_flags |= DQ_MOD;
336 if ((flags & FORCE) == 0 &&
337 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
341 for (i = 0; i < MAXQUOTAS; i++) {
342 if ((dq = ip->i_dquot[i]) == NODQUOT)
346 DQI_WAIT(dq, PINOD+1, "chkiq2");
348 error = chkiqchg(ip, change, cred, i, &warn);
351 * Roll back user quota changes when
352 * group quota failed.
360 DQI_WAIT(dq, PINOD+1, "chkiq3");
361 if (dq->dq_curinodes >= change)
362 dq->dq_curinodes -= change;
364 dq->dq_curinodes = 0;
365 dq->dq_flags &= ~DQ_INODS;
366 dq->dq_flags |= DQ_MOD;
372 /* Reset timer when crossing soft limit */
373 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
374 dq->dq_curinodes < dq->dq_isoftlimit)
375 dq->dq_itime = time_second + ITOUMP(ip)->um_itime[i];
376 dq->dq_curinodes += change;
377 dq->dq_flags |= DQ_MOD;
380 uprintf("\n%s: warning, %s inode quota exceeded\n",
381 ITOVFS(ip)->mnt_stat.f_mntonname,
388 * Check for a valid change to a users allocation.
389 * Issue an error message if appropriate.
392 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
394 struct dquot *dq = ip->i_dquot[type];
395 ino_t ncurinodes = dq->dq_curinodes + change;
398 * If user would exceed their hard limit, disallow inode allocation.
400 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
401 if ((dq->dq_flags & DQ_INODS) == 0 &&
402 ip->i_uid == cred->cr_uid) {
403 dq->dq_flags |= DQ_INODS;
405 uprintf("\n%s: write failed, %s inode limit reached\n",
406 ITOVFS(ip)->mnt_stat.f_mntonname,
414 * If user is over their soft limit for too long, disallow inode
415 * allocation. Reset time limit as they cross their soft limit.
417 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
418 if (dq->dq_curinodes < dq->dq_isoftlimit) {
419 dq->dq_itime = time_second + ITOUMP(ip)->um_itime[type];
420 if (ip->i_uid == cred->cr_uid)
424 if (time_second > dq->dq_itime) {
425 if ((dq->dq_flags & DQ_INODS) == 0 &&
426 ip->i_uid == cred->cr_uid) {
427 dq->dq_flags |= DQ_INODS;
429 uprintf("\n%s: write failed, %s "
430 "inode quota exceeded for too long\n",
431 ITOVFS(ip)->mnt_stat.f_mntonname,
444 * On filesystems with quotas enabled, it is an error for a file to change
445 * size and not to have a dquot structure associated with it.
448 chkdquot(struct inode *ip)
450 struct ufsmount *ump;
458 * Disk quotas must be turned off for system files. Currently
459 * these are snapshots and quota files.
461 if ((vp->v_vflag & VV_SYSTEM) != 0)
464 * XXX: Turn off quotas for files with a negative UID or GID.
465 * This prevents the creation of 100GB+ quota files.
467 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
471 for (i = 0; i < MAXQUOTAS; i++) {
472 if (ump->um_quotas[i] == NULLVP ||
473 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
475 if (ip->i_dquot[i] == NODQUOT) {
477 vn_printf(ITOV(ip), "chkdquot: missing dquot ");
478 panic("chkdquot: missing dquot");
486 * Code to process quotactl commands.
490 * Q_QUOTAON - set up a quota file for a particular filesystem.
493 quotaon(struct thread *td, struct mount *mp, int type, void *fname)
495 struct ufsmount *ump;
496 struct vnode *vp, **vpp;
502 error = priv_check(td, PRIV_UFS_QUOTAON);
508 if ((mp->mnt_flag & MNT_RDONLY) != 0) {
516 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
517 flags = FREAD | FWRITE;
520 error = vn_open(&nd, &flags, 0, NULL);
525 NDFREE(&nd, NDF_ONLY_PNBUF);
527 error = vfs_busy(mp, MBF_NOWAIT);
530 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);
549 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
551 if ((error = dqopen(vp, ump, type)) != 0) {
554 ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
556 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
562 mp->mnt_flag |= MNT_QUOTA;
565 vpp = &ump->um_quotas[type];
567 quotaoff1(td, mp, type);
570 * When the directory vnode containing the quota file is
571 * inactivated, due to the shared lookup of the quota file
572 * vput()ing the dvp, the qsyncvp() call for the containing
573 * directory would try to acquire the quota lock exclusive.
574 * At the same time, lookup already locked the quota vnode
575 * shared. Mark the quota vnode lock as allowing recursion
576 * and automatically converting shared locks to exclusive.
578 * Also mark quota vnode as system.
580 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
581 vp->v_vflag |= VV_SYSTEM;
587 * Save the credential of the process that turned on quotas.
588 * Set up the time limits for this quota.
590 ump->um_cred[type] = crhold(td->td_ucred);
591 ump->um_btime[type] = MAX_DQ_TIME;
592 ump->um_itime[type] = MAX_IQ_TIME;
593 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
594 if (dq->dq_btime > 0)
595 ump->um_btime[type] = dq->dq_btime;
596 if (dq->dq_itime > 0)
597 ump->um_itime[type] = dq->dq_itime;
601 * Allow the getdq from getinoquota below to read the quota
605 ump->um_qflags[type] &= ~QTF_CLOSING;
608 * Search vnodes associated with this mount point,
609 * adding references to quota file being opened.
610 * NB: only need to add dquot's for inodes being modified.
613 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
614 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
615 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
618 if (vp->v_type == VNON || vp->v_writecount == 0) {
623 error = getinoquota(VTOI(vp));
627 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
633 quotaoff_inchange(td, mp, type);
635 ump->um_qflags[type] &= ~QTF_OPENING;
636 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
637 ("quotaon: leaking flags"));
645 * Main code to turn off disk quotas for a filesystem. Does not change
649 quotaoff1(struct thread *td, struct mount *mp, int type)
652 struct vnode *qvp, *mvp;
653 struct ufsmount *ump;
662 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
663 ("quotaoff1: flags are invalid"));
664 if ((qvp = ump->um_quotas[type]) == NULLVP) {
668 cr = ump->um_cred[type];
672 * Search vnodes associated with this mount point,
673 * deleting any references to quota file being closed.
676 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
677 if (vp->v_type == VNON) {
681 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
682 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
686 dq = ip->i_dquot[type];
687 ip->i_dquot[type] = NODQUOT;
693 error = dqflush(qvp);
698 * Clear um_quotas before closing the quota vnode to prevent
699 * access to the closed vnode from dqget/dqsync
702 ump->um_quotas[type] = NULLVP;
703 ump->um_cred[type] = NOCRED;
706 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
707 qvp->v_vflag &= ~VV_SYSTEM;
709 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
716 * Turns off quotas, assumes that ump->um_qflags are already checked
717 * and QTF_CLOSING is set to indicate operation in progress. Fixes
718 * ump->um_qflags and mp->mnt_flag after.
721 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
723 struct ufsmount *ump;
727 error = quotaoff1(td, mp, type);
731 ump->um_qflags[type] &= ~QTF_CLOSING;
732 for (i = 0; i < MAXQUOTAS; i++)
733 if (ump->um_quotas[i] != NULLVP)
735 if (i == MAXQUOTAS) {
737 mp->mnt_flag &= ~MNT_QUOTA;
745 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
748 quotaoff(struct thread *td, struct mount *mp, int type)
750 struct ufsmount *ump;
753 error = priv_check(td, PRIV_UFS_QUOTAOFF);
759 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
763 ump->um_qflags[type] |= QTF_CLOSING;
766 return (quotaoff_inchange(td, mp, type));
770 * Q_GETQUOTA - return current values in a dqblk structure.
773 _getquota(struct thread *td, struct mount *mp, u_long id, int type,
781 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
782 error = priv_check(td, PRIV_VFS_GETQUOTA);
789 if (!groupmember(id, td->td_ucred) &&
790 !unprivileged_get_quota) {
791 error = priv_check(td, PRIV_VFS_GETQUOTA);
802 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
811 * Q_SETQUOTA - assign an entire dqblk structure.
814 _setquota(struct thread *td, struct mount *mp, u_long id, int type,
819 struct ufsmount *ump;
820 struct dqblk64 newlim;
823 error = priv_check(td, PRIV_VFS_SETQUOTA);
832 error = dqget(NULLVP, id, ump, type, &ndq);
837 DQI_WAIT(dq, PINOD+1, "setqta");
839 * Copy all but the current values.
840 * Reset time limit if previously had no soft limit or were
841 * under it, but now have a soft limit and are over it.
843 newlim.dqb_curblocks = dq->dq_curblocks;
844 newlim.dqb_curinodes = dq->dq_curinodes;
845 if (dq->dq_id != 0) {
846 newlim.dqb_btime = dq->dq_btime;
847 newlim.dqb_itime = dq->dq_itime;
849 if (newlim.dqb_bsoftlimit &&
850 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
851 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
852 newlim.dqb_btime = time_second + ump->um_btime[type];
853 if (newlim.dqb_isoftlimit &&
854 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
855 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
856 newlim.dqb_itime = time_second + ump->um_itime[type];
858 if (dq->dq_curblocks < dq->dq_bsoftlimit)
859 dq->dq_flags &= ~DQ_BLKS;
860 if (dq->dq_curinodes < dq->dq_isoftlimit)
861 dq->dq_flags &= ~DQ_INODS;
862 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
863 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
864 dq->dq_flags |= DQ_FAKE;
866 dq->dq_flags &= ~DQ_FAKE;
867 dq->dq_flags |= DQ_MOD;
874 * Q_SETUSE - set current inode and block usage.
877 _setuse(struct thread *td, struct mount *mp, u_long id, int type,
881 struct ufsmount *ump;
883 struct dqblk64 usage;
886 error = priv_check(td, PRIV_UFS_SETUSE);
895 error = dqget(NULLVP, id, ump, type, &ndq);
900 DQI_WAIT(dq, PINOD+1, "setuse");
902 * Reset time limit if have a soft limit and were
903 * previously under it, but are now over it.
905 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
906 usage.dqb_curblocks >= dq->dq_bsoftlimit)
907 dq->dq_btime = time_second + ump->um_btime[type];
908 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
909 usage.dqb_curinodes >= dq->dq_isoftlimit)
910 dq->dq_itime = time_second + ump->um_itime[type];
911 dq->dq_curblocks = usage.dqb_curblocks;
912 dq->dq_curinodes = usage.dqb_curinodes;
913 if (dq->dq_curblocks < dq->dq_bsoftlimit)
914 dq->dq_flags &= ~DQ_BLKS;
915 if (dq->dq_curinodes < dq->dq_isoftlimit)
916 dq->dq_flags &= ~DQ_INODS;
917 dq->dq_flags |= DQ_MOD;
924 getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
926 struct dqblk32 dqb32;
927 struct dqblk64 dqb64;
930 error = _getquota(td, mp, id, type, &dqb64);
933 dqb64_dqb32(&dqb64, &dqb32);
934 error = copyout(&dqb32, addr, sizeof(dqb32));
939 setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
941 struct dqblk32 dqb32;
942 struct dqblk64 dqb64;
945 error = copyin(addr, &dqb32, sizeof(dqb32));
948 dqb32_dqb64(&dqb32, &dqb64);
949 error = _setquota(td, mp, id, type, &dqb64);
954 setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
956 struct dqblk32 dqb32;
957 struct dqblk64 dqb64;
960 error = copyin(addr, &dqb32, sizeof(dqb32));
963 dqb32_dqb64(&dqb32, &dqb64);
964 error = _setuse(td, mp, id, type, &dqb64);
969 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
971 struct dqblk64 dqb64;
974 error = _getquota(td, mp, id, type, &dqb64);
977 error = copyout(&dqb64, addr, sizeof(dqb64));
982 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
984 struct dqblk64 dqb64;
987 error = copyin(addr, &dqb64, sizeof(dqb64));
990 error = _setquota(td, mp, id, type, &dqb64);
995 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
997 struct dqblk64 dqb64;
1000 error = copyin(addr, &dqb64, sizeof(dqb64));
1003 error = _setuse(td, mp, id, type, &dqb64);
1008 * Q_GETQUOTASIZE - get bit-size of quota file fields
1011 getquotasize(struct thread *td, struct mount *mp, u_long id, int type,
1014 struct ufsmount *ump = VFSTOUFS(mp);
1018 if (ump->um_quotas[type] == NULLVP ||
1019 (ump->um_qflags[type] & QTF_CLOSING)) {
1023 if ((ump->um_qflags[type] & QTF_64BIT) != 0)
1028 return (copyout(&bitsize, sizep, sizeof(int)));
1032 * Q_SYNC - sync quota files to disk.
1035 qsync(struct mount *mp)
1037 struct ufsmount *ump = VFSTOUFS(mp);
1038 struct thread *td = curthread; /* XXX */
1039 struct vnode *vp, *mvp;
1044 * Check if the mount point has any quotas.
1045 * If not, simply return.
1047 for (i = 0; i < MAXQUOTAS; i++)
1048 if (ump->um_quotas[i] != NULLVP)
1053 * Search vnodes associated with this mount point,
1054 * synchronizing any modified dquot structures.
1057 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1058 if (vp->v_type == VNON) {
1062 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
1064 if (error == ENOENT) {
1065 MNT_VNODE_FOREACH_ACTIVE_ABORT(mp, mvp);
1070 for (i = 0; i < MAXQUOTAS; i++) {
1071 dq = VTOI(vp)->i_dquot[i];
1081 * Sync quota file for given vnode to disk.
1084 qsyncvp(struct vnode *vp)
1086 struct ufsmount *ump = VFSTOUFS(vp->v_mount);
1091 * Check if the mount point has any quotas.
1092 * If not, simply return.
1094 for (i = 0; i < MAXQUOTAS; i++)
1095 if (ump->um_quotas[i] != NULLVP)
1100 * Search quotas associated with this vnode
1101 * synchronizing any modified dquot structures.
1103 for (i = 0; i < MAXQUOTAS; i++) {
1104 dq = VTOI(vp)->i_dquot[i];
1112 * Code pertaining to management of the in-core dquot data structures.
1114 #define DQHASH(dqvp, id) \
1115 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
1116 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
1117 static u_long dqhash;
1122 #define DQUOTINC 5 /* minimum free dquots desired */
1123 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
1124 static long numdquot, desireddquot = DQUOTINC;
1127 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
1132 #define DQH_LOCK() mtx_lock(&dqhlock)
1133 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
1135 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
1136 struct vnode *dqvp);
1139 * Initialize the quota system.
1145 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
1146 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
1147 TAILQ_INIT(&dqfreelist);
1151 * Shut down the quota system.
1158 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
1159 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
1160 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1161 mtx_destroy(&dq->dq_lock);
1164 mtx_destroy(&dqhlock);
1167 static struct dquot *
1168 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1172 mtx_assert(&dqhlock, MA_OWNED);
1173 LIST_FOREACH(dq, dqh, dq_hash) {
1174 if (dq->dq_id != id ||
1175 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1178 * Cache hit with no references. Take
1179 * the structure off the free list.
1181 if (dq->dq_cnt == 0)
1182 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1190 * Determine the quota file type.
1192 * A 32-bit quota file is simply an array of struct dqblk32.
1194 * A 64-bit quota file is a struct dqhdr64 followed by an array of struct
1195 * dqblk64. The header contains various magic bits which allow us to be
1196 * reasonably confident that it is indeeda 64-bit quota file and not just
1197 * a 32-bit quota file that just happens to "look right".
1201 dqopen(struct vnode *vp, struct ufsmount *ump, int type)
1208 ASSERT_VOP_LOCKED(vp, "dqopen");
1209 auio.uio_iov = &aiov;
1210 auio.uio_iovcnt = 1;
1211 aiov.iov_base = &dqh;
1212 aiov.iov_len = sizeof(dqh);
1213 auio.uio_resid = sizeof(dqh);
1214 auio.uio_offset = 0;
1215 auio.uio_segflg = UIO_SYSSPACE;
1216 auio.uio_rw = UIO_READ;
1217 auio.uio_td = (struct thread *)0;
1218 error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
1222 if (auio.uio_resid > 0) {
1223 /* assume 32 bits */
1228 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
1229 be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
1230 be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
1231 be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) {
1232 /* XXX: what if the magic matches, but the sizes are wrong? */
1233 ump->um_qflags[type] |= QTF_64BIT;
1235 ump->um_qflags[type] &= ~QTF_64BIT;
1243 * Obtain a dquot structure for the specified identifier and quota file
1244 * reading the information from the file if necessary.
1247 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1250 uint8_t buf[sizeof(struct dqblk64)];
1251 off_t base, recsize;
1252 struct dquot *dq, *dq1;
1257 int dqvplocked, error;
1259 #ifdef DEBUG_VFS_LOCKS
1261 ASSERT_VOP_ELOCKED(vp, "dqget");
1264 if (vp != NULLVP && *dqp != NODQUOT) {
1268 /* XXX: Disallow negative id values to prevent the
1269 * creation of 100GB+ quota data files.
1275 dqvp = ump->um_quotas[type];
1276 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1287 * Check the cache first.
1289 dqh = DQHASH(dqvp, id);
1291 dq = dqhashfind(dqh, id, dqvp);
1294 hfound: DQI_LOCK(dq);
1295 DQI_WAIT(dq, PINOD+1, "dqget");
1297 if (dq->dq_ump == NULL) {
1311 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1312 * since new dq will appear on the hash chain DQ_LOCKed.
1316 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1320 * Recheck the cache after sleep for quota vnode lock.
1322 dq = dqhashfind(dqh, id, dqvp);
1330 * Not in cache, allocate a new one or take it from the
1333 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1334 numdquot < MAXQUOTAS * desiredvnodes)
1335 desireddquot += DQUOTINC;
1336 if (numdquot < desireddquot) {
1339 dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
1340 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1343 * Recheck the cache after sleep for memory.
1345 dq = dqhashfind(dqh, id, dqvp);
1349 mtx_destroy(&dq1->dq_lock);
1355 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1365 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1366 panic("dqget: free dquot isn't %p", dq);
1367 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1368 if (dq->dq_ump != NULL)
1369 LIST_REMOVE(dq, dq_hash);
1373 * Dq is put into hash already locked to prevent parallel
1374 * usage while it is being read from file.
1376 dq->dq_flags = DQ_LOCK;
1380 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1385 * Read the requested quota record from the quota file, performing
1386 * any necessary conversions.
1388 if (ump->um_qflags[type] & QTF_64BIT) {
1389 recsize = sizeof(struct dqblk64);
1390 base = sizeof(struct dqhdr64);
1392 recsize = sizeof(struct dqblk32);
1395 auio.uio_iov = &aiov;
1396 auio.uio_iovcnt = 1;
1397 aiov.iov_base = buf;
1398 aiov.iov_len = recsize;
1399 auio.uio_resid = recsize;
1400 auio.uio_offset = base + id * recsize;
1401 auio.uio_segflg = UIO_SYSSPACE;
1402 auio.uio_rw = UIO_READ;
1403 auio.uio_td = (struct thread *)0;
1405 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1406 if (auio.uio_resid == recsize && error == 0) {
1407 bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
1409 if (ump->um_qflags[type] & QTF_64BIT)
1410 dqb64_dq((struct dqblk64 *)buf, dq);
1412 dqb32_dq((struct dqblk32 *)buf, dq);
1419 * I/O error in reading quota file, release
1420 * quota structure and reflect problem to caller.
1425 LIST_REMOVE(dq, dq_hash);
1428 if (dq->dq_flags & DQ_WANT)
1438 * Check for no limit to enforce.
1439 * Initialize time values if necessary.
1441 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1442 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1443 dq->dq_flags |= DQ_FAKE;
1444 if (dq->dq_id != 0) {
1445 if (dq->dq_btime == 0) {
1446 dq->dq_btime = time_second + ump->um_btime[type];
1447 if (dq->dq_bsoftlimit &&
1448 dq->dq_curblocks >= dq->dq_bsoftlimit)
1449 dq->dq_flags |= DQ_MOD;
1451 if (dq->dq_itime == 0) {
1452 dq->dq_itime = time_second + ump->um_itime[type];
1453 if (dq->dq_isoftlimit &&
1454 dq->dq_curinodes >= dq->dq_isoftlimit)
1455 dq->dq_flags |= DQ_MOD;
1466 * Obtain a reference to a dquot.
1469 dqref(struct dquot *dq)
1477 * Release a reference to a dquot.
1480 dqrele(struct vnode *vp, struct dquot *dq)
1486 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
1487 if (dq->dq_cnt > 1) {
1494 (void) dqsync(vp, dq);
1497 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
1498 if (--dq->dq_cnt > 0)
1505 * The dq may become dirty after it is synced but before it is
1506 * put to the free list. Checking the DQ_MOD there without
1507 * locking dq should be safe since no other references to the
1510 if ((dq->dq_flags & DQ_MOD) != 0) {
1515 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1520 * Update the disk quota in the quota file.
1523 dqsync(struct vnode *vp, struct dquot *dq)
1525 uint8_t buf[sizeof(struct dqblk64)];
1526 off_t base, recsize;
1532 struct ufsmount *ump;
1534 #ifdef DEBUG_VFS_LOCKS
1536 ASSERT_VOP_ELOCKED(vp, "dqsync");
1542 panic("dqsync: dquot");
1543 if ((ump = dq->dq_ump) == NULL)
1546 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP) {
1551 panic("dqsync: file");
1557 if ((dq->dq_flags & DQ_MOD) == 0) {
1564 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1566 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1569 DQI_WAIT(dq, PINOD+2, "dqsync");
1570 if ((dq->dq_flags & DQ_MOD) == 0)
1572 dq->dq_flags |= DQ_LOCK;
1576 * Write the quota record to the quota file, performing any
1577 * necessary conversions. See dqget() for additional details.
1579 if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
1580 dq_dqb64(dq, (struct dqblk64 *)buf);
1581 recsize = sizeof(struct dqblk64);
1582 base = sizeof(struct dqhdr64);
1584 dq_dqb32(dq, (struct dqblk32 *)buf);
1585 recsize = sizeof(struct dqblk32);
1589 auio.uio_iov = &aiov;
1590 auio.uio_iovcnt = 1;
1591 aiov.iov_base = buf;
1592 aiov.iov_len = recsize;
1593 auio.uio_resid = recsize;
1594 auio.uio_offset = base + dq->dq_id * recsize;
1595 auio.uio_segflg = UIO_SYSSPACE;
1596 auio.uio_rw = UIO_WRITE;
1597 auio.uio_td = (struct thread *)0;
1598 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1599 if (auio.uio_resid && error == 0)
1604 dq->dq_flags &= ~DQ_MOD;
1611 vn_finished_secondary_write(mp);
1616 * Flush all entries from the cache for a particular vnode.
1619 dqflush(struct vnode *vp)
1621 struct dquot *dq, *nextdq;
1626 * Move all dquot's that used to refer to this quota
1627 * file off their hash chains (they will eventually
1628 * fall off the head of the free list and be re-used).
1632 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1633 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1634 nextdq = LIST_NEXT(dq, dq_hash);
1635 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1640 LIST_REMOVE(dq, dq_hash);
1650 * The following three functions are provided for the adjustment of
1651 * quotas by the soft updates code.
1655 * Acquire a reference to the quota structures associated with a vnode.
1656 * Return count of number of quota structures found.
1667 for (i = 0; i < MAXQUOTAS; i++)
1670 * Disk quotas must be turned off for system files. Currently
1671 * snapshot and quota files.
1673 if ((vp->v_vflag & VV_SYSTEM) != 0)
1676 * Iterate through and copy active quotas.
1681 for (i = 0; i < MAXQUOTAS; i++) {
1682 if ((dq = ip->i_dquot[i]) == NODQUOT)
1688 mtx_unlock(&dqhlock);
1693 * Release a set of quota structures obtained from a vnode.
1702 for (i = 0; i < MAXQUOTAS; i++) {
1703 if ((dq = qrp[i]) == NODQUOT)
1710 * Adjust the number of blocks associated with a quota.
1711 * Positive numbers when adding blocks; negative numbers when freeing blocks.
1714 quotaadj(qrp, ump, blkcount)
1716 struct ufsmount *ump;
1720 ufs2_daddr_t ncurblocks;
1725 for (i = 0; i < MAXQUOTAS; i++) {
1726 if ((dq = qrp[i]) == NODQUOT)
1729 DQI_WAIT(dq, PINOD+1, "adjqta");
1730 ncurblocks = dq->dq_curblocks + blkcount;
1731 if (ncurblocks >= 0)
1732 dq->dq_curblocks = ncurblocks;
1734 dq->dq_curblocks = 0;
1736 dq->dq_flags &= ~DQ_BLKS;
1737 else if (dq->dq_curblocks + blkcount >= dq->dq_bsoftlimit &&
1738 dq->dq_curblocks < dq->dq_bsoftlimit)
1739 dq->dq_btime = time_second + ump->um_btime[i];
1740 dq->dq_flags |= DQ_MOD;
1744 #endif /* SOFTUPDATES */
1747 * 32-bit / 64-bit conversion functions.
1749 * 32-bit quota records are stored in native byte order. Attention must
1750 * be paid to overflow issues.
1752 * 64-bit quota records are stored in network byte order.
1755 #define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
1758 * Convert 32-bit host-order structure to dquot.
1761 dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
1764 dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
1765 dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
1766 dq->dq_curblocks = dqb32->dqb_curblocks;
1767 dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
1768 dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
1769 dq->dq_curinodes = dqb32->dqb_curinodes;
1770 dq->dq_btime = dqb32->dqb_btime;
1771 dq->dq_itime = dqb32->dqb_itime;
1775 * Convert 64-bit network-order structure to dquot.
1778 dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
1781 dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
1782 dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
1783 dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
1784 dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
1785 dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
1786 dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
1787 dq->dq_btime = be64toh(dqb64->dqb_btime);
1788 dq->dq_itime = be64toh(dqb64->dqb_itime);
1792 * Convert dquot to 32-bit host-order structure.
1795 dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
1798 dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
1799 dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
1800 dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
1801 dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
1802 dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
1803 dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
1804 dqb32->dqb_btime = CLIP32(dq->dq_btime);
1805 dqb32->dqb_itime = CLIP32(dq->dq_itime);
1809 * Convert dquot to 64-bit network-order structure.
1812 dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
1815 dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
1816 dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
1817 dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
1818 dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
1819 dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
1820 dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
1821 dqb64->dqb_btime = htobe64(dq->dq_btime);
1822 dqb64->dqb_itime = htobe64(dq->dq_itime);
1826 * Convert 64-bit host-order structure to 32-bit host-order structure.
1829 dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
1832 dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
1833 dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
1834 dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
1835 dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
1836 dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
1837 dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
1838 dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
1839 dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
1843 * Convert 32-bit host-order structure to 64-bit host-order structure.
1846 dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
1849 dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
1850 dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
1851 dqb64->dqb_curblocks = dqb32->dqb_curblocks;
1852 dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
1853 dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
1854 dqb64->dqb_curinodes = dqb32->dqb_curinodes;
1855 dqb64->dqb_btime = dqb32->dqb_btime;
1856 dqb64->dqb_itime = dqb32->dqb_itime;