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$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mutex.h>
46 #include <sys/namei.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
52 #include <ufs/ufs/extattr.h>
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/inode.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/ufs_extern.h>
58 SYSCTL_DECL(_security_bsd);
60 static int unprivileged_get_quota = 0;
61 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
62 &unprivileged_get_quota, 0,
63 "Unprivileged processes may retrieve quotas for other uids and gids");
65 static MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
68 * Quota name to error message mapping.
70 static char *quotatypes[] = INITQFNAMES;
72 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int);
73 static int chkiqchg(struct inode *, ino_t, struct ucred *, int);
74 static int dqget(struct vnode *,
75 u_long, struct ufsmount *, int, struct dquot **);
76 static int dqsync(struct vnode *, struct dquot *);
77 static void dqflush(struct vnode *);
80 static void dqref(struct dquot *);
81 static void chkdquot(struct inode *);
85 * Set up the quotas for an inode.
87 * This routine completely defines the semantics of quotas.
88 * If other criterion want to be used to establish quotas, the
89 * MAXQUOTAS value in quotas.h should be increased, and the
90 * additional dquots set up here.
97 struct vnode *vp = ITOV(ip);
100 ump = VFSTOUFS(vp->v_mount);
102 * Set up the user quota based on file uid.
103 * EINVAL means that quotas are not enabled.
105 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
107 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
111 * Set up the group quota based on file gid.
112 * EINVAL means that quotas are not enabled.
114 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
116 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
123 * Update disk usage, and take corrective action.
126 chkdq(ip, change, cred, flags)
133 ufs2_daddr_t ncurblocks;
137 if ((flags & CHOWN) == 0)
143 for (i = 0; i < MAXQUOTAS; i++) {
144 if ((dq = ip->i_dquot[i]) == NODQUOT)
146 while (dq->dq_flags & DQ_LOCK) {
147 dq->dq_flags |= DQ_WANT;
148 (void) tsleep(dq, PINOD+1, "chkdq1", 0);
150 ncurblocks = dq->dq_curblocks + change;
152 dq->dq_curblocks = ncurblocks;
154 dq->dq_curblocks = 0;
155 dq->dq_flags &= ~DQ_BLKS;
156 dq->dq_flags |= DQ_MOD;
160 if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
161 for (i = 0; i < MAXQUOTAS; i++) {
162 if ((dq = ip->i_dquot[i]) == NODQUOT)
164 error = chkdqchg(ip, change, cred, i);
169 for (i = 0; i < MAXQUOTAS; i++) {
170 if ((dq = ip->i_dquot[i]) == NODQUOT)
172 while (dq->dq_flags & DQ_LOCK) {
173 dq->dq_flags |= DQ_WANT;
174 (void) tsleep(dq, PINOD+1, "chkdq2", 0);
176 /* Reset timer when crossing soft limit */
177 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
178 dq->dq_curblocks < dq->dq_bsoftlimit)
179 dq->dq_btime = time_second +
180 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
181 dq->dq_curblocks += change;
182 dq->dq_flags |= DQ_MOD;
188 * Check for a valid change to a users allocation.
189 * Issue an error message if appropriate.
192 chkdqchg(ip, change, cred, type)
198 struct dquot *dq = ip->i_dquot[type];
199 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
202 * If user would exceed their hard limit, disallow space allocation.
204 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
205 if ((dq->dq_flags & DQ_BLKS) == 0 &&
206 ip->i_uid == cred->cr_uid) {
207 uprintf("\n%s: write failed, %s disk limit reached\n",
208 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
210 dq->dq_flags |= DQ_BLKS;
215 * If user is over their soft limit for too long, disallow space
216 * allocation. Reset time limit as they cross their soft limit.
218 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
219 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
220 dq->dq_btime = time_second +
221 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
222 if (ip->i_uid == cred->cr_uid)
223 uprintf("\n%s: warning, %s %s\n",
224 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
225 quotatypes[type], "disk quota exceeded");
228 if (time_second > dq->dq_btime) {
229 if ((dq->dq_flags & DQ_BLKS) == 0 &&
230 ip->i_uid == cred->cr_uid) {
231 uprintf("\n%s: write failed, %s %s\n",
232 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
234 "disk quota exceeded for too long");
235 dq->dq_flags |= DQ_BLKS;
244 * Check the inode limit, applying corrective action.
247 chkiq(ip, change, cred, flags)
258 if ((flags & CHOWN) == 0)
263 /* XXX: change is unsigned */
265 for (i = 0; i < MAXQUOTAS; i++) {
266 if ((dq = ip->i_dquot[i]) == NODQUOT)
268 while (dq->dq_flags & DQ_LOCK) {
269 dq->dq_flags |= DQ_WANT;
270 (void) tsleep(dq, PINOD+1, "chkiq1", 0);
272 ncurinodes = dq->dq_curinodes + change;
273 /* XXX: ncurinodes is unsigned */
275 dq->dq_curinodes = ncurinodes;
277 dq->dq_curinodes = 0;
278 dq->dq_flags &= ~DQ_INODS;
279 dq->dq_flags |= DQ_MOD;
283 if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
284 for (i = 0; i < MAXQUOTAS; i++) {
285 if ((dq = ip->i_dquot[i]) == NODQUOT)
287 error = chkiqchg(ip, change, cred, i);
292 for (i = 0; i < MAXQUOTAS; i++) {
293 if ((dq = ip->i_dquot[i]) == NODQUOT)
295 while (dq->dq_flags & DQ_LOCK) {
296 dq->dq_flags |= DQ_WANT;
297 (void) tsleep(dq, PINOD+1, "chkiq2", 0);
299 /* Reset timer when crossing soft limit */
300 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
301 dq->dq_curinodes < dq->dq_isoftlimit)
302 dq->dq_itime = time_second +
303 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
304 dq->dq_curinodes += change;
305 dq->dq_flags |= DQ_MOD;
311 * Check for a valid change to a users allocation.
312 * Issue an error message if appropriate.
315 chkiqchg(ip, change, cred, type)
321 struct dquot *dq = ip->i_dquot[type];
322 ino_t ncurinodes = dq->dq_curinodes + change;
325 * If user would exceed their hard limit, disallow inode allocation.
327 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
328 if ((dq->dq_flags & DQ_INODS) == 0 &&
329 ip->i_uid == cred->cr_uid) {
330 uprintf("\n%s: write failed, %s inode limit reached\n",
331 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
333 dq->dq_flags |= DQ_INODS;
338 * If user is over their soft limit for too long, disallow inode
339 * allocation. Reset time limit as they cross their soft limit.
341 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
342 if (dq->dq_curinodes < dq->dq_isoftlimit) {
343 dq->dq_itime = time_second +
344 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
345 if (ip->i_uid == cred->cr_uid)
346 uprintf("\n%s: warning, %s %s\n",
347 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
348 quotatypes[type], "inode quota exceeded");
351 if (time_second > dq->dq_itime) {
352 if ((dq->dq_flags & DQ_INODS) == 0 &&
353 ip->i_uid == cred->cr_uid) {
354 uprintf("\n%s: write failed, %s %s\n",
355 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
357 "inode quota exceeded for too long");
358 dq->dq_flags |= DQ_INODS;
368 * On filesystems with quotas enabled, it is an error for a file to change
369 * size and not to have a dquot structure associated with it.
375 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
378 for (i = 0; i < MAXQUOTAS; i++) {
379 if (ump->um_quotas[i] == NULLVP ||
380 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
382 if (ip->i_dquot[i] == NODQUOT) {
383 vprint("chkdquot: missing dquot", ITOV(ip));
384 panic("chkdquot: missing dquot");
391 * Code to process quotactl commands.
395 * Q_QUOTAON - set up a quota file for a particular filesystem.
398 quotaon(td, mp, type, fname)
404 struct ufsmount *ump = VFSTOUFS(mp);
405 struct vnode *vp, **vpp;
406 struct vnode *nextvp;
411 error = suser_cred(td->td_ucred, PRISON_ROOT);
415 vpp = &ump->um_quotas[type];
416 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
417 flags = FREAD | FWRITE;
418 error = vn_open(&nd, &flags, 0, -1);
421 NDFREE(&nd, NDF_ONLY_PNBUF);
423 VOP_UNLOCK(vp, 0, td);
424 if (vp->v_type != VREG) {
425 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
429 quotaoff(td, mp, type);
430 ump->um_qflags[type] |= QTF_OPENING;
431 mp->mnt_flag |= MNT_QUOTA;
432 ASSERT_VOP_LOCKED(vp, "quotaon");
433 vp->v_vflag |= VV_SYSTEM;
436 * Save the credential of the process that turned on quotas.
437 * Set up the time limits for this quota.
439 ump->um_cred[type] = crhold(td->td_ucred);
440 ump->um_btime[type] = MAX_DQ_TIME;
441 ump->um_itime[type] = MAX_IQ_TIME;
442 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
443 if (dq->dq_btime > 0)
444 ump->um_btime[type] = dq->dq_btime;
445 if (dq->dq_itime > 0)
446 ump->um_itime[type] = dq->dq_itime;
450 * Search vnodes associated with this mount point,
451 * adding references to quota file being opened.
452 * NB: only need to add dquot's for inodes being modified.
456 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
457 if (vp->v_mount != mp)
459 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
462 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
466 if (vp->v_type == VNON || vp->v_writecount == 0) {
467 VOP_UNLOCK(vp, 0, td);
472 error = getinoquota(VTOI(vp));
473 VOP_UNLOCK(vp, 0, td);
478 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp)
482 ump->um_qflags[type] &= ~QTF_OPENING;
484 quotaoff(td, mp, type);
489 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
492 quotaoff(td, mp, type)
498 struct vnode *qvp, *nextvp;
499 struct ufsmount *ump = VFSTOUFS(mp);
504 error = suser_cred(td->td_ucred, PRISON_ROOT);
508 if ((qvp = ump->um_quotas[type]) == NULLVP)
510 ump->um_qflags[type] |= QTF_CLOSING;
512 * Search vnodes associated with this mount point,
513 * deleting any references to quota file being closed.
517 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
518 if (vp->v_mount != mp)
520 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
524 if (vp->v_type == VNON) {
529 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
534 dq = ip->i_dquot[type];
535 ip->i_dquot[type] = NODQUOT;
537 VOP_UNLOCK(vp, 0, td);
540 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp)
545 ASSERT_VOP_LOCKED(qvp, "quotaoff");
546 qvp->v_vflag &= ~VV_SYSTEM;
547 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
548 ump->um_quotas[type] = NULLVP;
549 crfree(ump->um_cred[type]);
550 ump->um_cred[type] = NOCRED;
551 ump->um_qflags[type] &= ~QTF_CLOSING;
552 for (type = 0; type < MAXQUOTAS; type++)
553 if (ump->um_quotas[type] != NULLVP)
555 if (type == MAXQUOTAS)
556 mp->mnt_flag &= ~MNT_QUOTA;
561 * Q_GETQUOTA - return current values in a dqblk structure.
564 getquota(td, mp, id, type, addr)
576 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
577 error = suser_cred(td->td_ucred, PRISON_ROOT);
584 if (!groupmember(id, td->td_ucred) && !unprivileged_get_quota) {
585 error = suser_cred(td->td_ucred, PRISON_ROOT);
595 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
598 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
604 * Q_SETQUOTA - assign an entire dqblk structure.
607 setquota(td, mp, id, type, addr)
616 struct ufsmount *ump = VFSTOUFS(mp);
620 error = suser_cred(td->td_ucred, PRISON_ROOT);
624 error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
627 error = dqget(NULLVP, id, ump, type, &ndq);
631 while (dq->dq_flags & DQ_LOCK) {
632 dq->dq_flags |= DQ_WANT;
633 (void) tsleep(dq, PINOD+1, "setqta", 0);
636 * Copy all but the current values.
637 * Reset time limit if previously had no soft limit or were
638 * under it, but now have a soft limit and are over it.
640 newlim.dqb_curblocks = dq->dq_curblocks;
641 newlim.dqb_curinodes = dq->dq_curinodes;
642 if (dq->dq_id != 0) {
643 newlim.dqb_btime = dq->dq_btime;
644 newlim.dqb_itime = dq->dq_itime;
646 if (newlim.dqb_bsoftlimit &&
647 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
648 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
649 newlim.dqb_btime = time_second + ump->um_btime[type];
650 if (newlim.dqb_isoftlimit &&
651 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
652 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
653 newlim.dqb_itime = time_second + ump->um_itime[type];
655 if (dq->dq_curblocks < dq->dq_bsoftlimit)
656 dq->dq_flags &= ~DQ_BLKS;
657 if (dq->dq_curinodes < dq->dq_isoftlimit)
658 dq->dq_flags &= ~DQ_INODS;
659 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
660 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
661 dq->dq_flags |= DQ_FAKE;
663 dq->dq_flags &= ~DQ_FAKE;
664 dq->dq_flags |= DQ_MOD;
670 * Q_SETUSE - set current inode and block usage.
673 setuse(td, mp, id, type, addr)
681 struct ufsmount *ump = VFSTOUFS(mp);
686 error = suser_cred(td->td_ucred, PRISON_ROOT);
690 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
693 error = dqget(NULLVP, id, ump, type, &ndq);
697 while (dq->dq_flags & DQ_LOCK) {
698 dq->dq_flags |= DQ_WANT;
699 (void) tsleep(dq, PINOD+1, "setuse", 0);
702 * Reset time limit if have a soft limit and were
703 * previously under it, but are now over it.
705 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
706 usage.dqb_curblocks >= dq->dq_bsoftlimit)
707 dq->dq_btime = time_second + ump->um_btime[type];
708 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
709 usage.dqb_curinodes >= dq->dq_isoftlimit)
710 dq->dq_itime = time_second + ump->um_itime[type];
711 dq->dq_curblocks = usage.dqb_curblocks;
712 dq->dq_curinodes = usage.dqb_curinodes;
713 if (dq->dq_curblocks < dq->dq_bsoftlimit)
714 dq->dq_flags &= ~DQ_BLKS;
715 if (dq->dq_curinodes < dq->dq_isoftlimit)
716 dq->dq_flags &= ~DQ_INODS;
717 dq->dq_flags |= DQ_MOD;
723 * Q_SYNC - sync quota files to disk.
729 struct ufsmount *ump = VFSTOUFS(mp);
730 struct thread *td = curthread; /* XXX */
731 struct vnode *vp, *nextvp;
736 * Check if the mount point has any quotas.
737 * If not, simply return.
739 for (i = 0; i < MAXQUOTAS; i++)
740 if (ump->um_quotas[i] != NULLVP)
745 * Search vnodes associated with this mount point,
746 * synchronizing any modified dquot structures.
750 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
751 if (vp->v_mount != mp)
753 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
756 if (vp->v_type == VNON) {
761 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
768 for (i = 0; i < MAXQUOTAS; i++) {
769 dq = VTOI(vp)->i_dquot[i];
770 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
775 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp)
783 * Code pertaining to management of the in-core dquot data structures.
785 #define DQHASH(dqvp, id) \
786 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
787 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
788 static u_long dqhash;
793 #define DQUOTINC 5 /* minimum free dquots desired */
794 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
795 static long numdquot, desireddquot = DQUOTINC;
798 * Initialize the quota system.
804 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
805 TAILQ_INIT(&dqfreelist);
809 * Shut down the quota system.
816 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
817 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
818 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
824 * Obtain a dquot structure for the specified identifier and quota file
825 * reading the information from the file if necessary.
828 dqget(vp, id, ump, type, dqp)
831 struct ufsmount *ump;
835 struct thread *td = curthread; /* XXX */
843 dqvp = ump->um_quotas[type];
844 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
849 * Check the cache first.
851 dqh = DQHASH(dqvp, id);
852 LIST_FOREACH(dq, dqh, dq_hash) {
853 if (dq->dq_id != id ||
854 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
857 * Cache hit with no references. Take
858 * the structure off the free list.
861 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
867 * Not in cache, allocate a new one.
869 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
870 numdquot < MAXQUOTAS * desiredvnodes)
871 desireddquot += DQUOTINC;
872 if (numdquot < desireddquot) {
873 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
877 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
882 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
883 panic("dqget: free dquot isn't");
884 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
885 if (dq->dq_ump != NULL)
886 LIST_REMOVE(dq, dq_hash);
889 * Initialize the contents of the dquot structure.
892 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
893 LIST_INSERT_HEAD(dqh, dq, dq_hash);
895 dq->dq_flags = DQ_LOCK;
899 auio.uio_iov = &aiov;
901 aiov.iov_base = (caddr_t)&dq->dq_dqb;
902 aiov.iov_len = sizeof (struct dqblk);
903 auio.uio_resid = sizeof (struct dqblk);
904 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
905 auio.uio_segflg = UIO_SYSSPACE;
906 auio.uio_rw = UIO_READ;
907 auio.uio_td = (struct thread *)0;
908 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
909 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
910 bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
912 VOP_UNLOCK(dqvp, 0, td);
913 if (dq->dq_flags & DQ_WANT)
917 * I/O error in reading quota file, release
918 * quota structure and reflect problem to caller.
921 LIST_REMOVE(dq, dq_hash);
927 * Check for no limit to enforce.
928 * Initialize time values if necessary.
930 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
931 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
932 dq->dq_flags |= DQ_FAKE;
933 if (dq->dq_id != 0) {
934 if (dq->dq_btime == 0)
935 dq->dq_btime = time_second + ump->um_btime[type];
936 if (dq->dq_itime == 0)
937 dq->dq_itime = time_second + ump->um_itime[type];
945 * Obtain a reference to a dquot.
957 * Release a reference to a dquot.
967 if (dq->dq_cnt > 1) {
971 if (dq->dq_flags & DQ_MOD)
972 (void) dqsync(vp, dq);
973 if (--dq->dq_cnt > 0)
975 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
979 * Update the disk quota in the quota file.
986 struct thread *td = curthread; /* XXX */
993 panic("dqsync: dquot");
994 if ((dq->dq_flags & DQ_MOD) == 0)
996 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
997 panic("dqsync: file");
998 (void) vn_write_suspend_wait(dqvp, NULL, V_WAIT);
1000 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
1001 while (dq->dq_flags & DQ_LOCK) {
1002 dq->dq_flags |= DQ_WANT;
1003 (void) tsleep(dq, PINOD+2, "dqsync", 0);
1004 if ((dq->dq_flags & DQ_MOD) == 0) {
1006 VOP_UNLOCK(dqvp, 0, td);
1010 dq->dq_flags |= DQ_LOCK;
1011 auio.uio_iov = &aiov;
1012 auio.uio_iovcnt = 1;
1013 aiov.iov_base = (caddr_t)&dq->dq_dqb;
1014 aiov.iov_len = sizeof (struct dqblk);
1015 auio.uio_resid = sizeof (struct dqblk);
1016 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
1017 auio.uio_segflg = UIO_SYSSPACE;
1018 auio.uio_rw = UIO_WRITE;
1019 auio.uio_td = (struct thread *)0;
1020 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1021 if (auio.uio_resid && error == 0)
1023 if (dq->dq_flags & DQ_WANT)
1025 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
1027 VOP_UNLOCK(dqvp, 0, td);
1032 * Flush all entries from the cache for a particular vnode.
1038 struct dquot *dq, *nextdq;
1042 * Move all dquot's that used to refer to this quota
1043 * file off their hash chains (they will eventually
1044 * fall off the head of the free list and be re-used).
1046 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1047 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1048 nextdq = LIST_NEXT(dq, dq_hash);
1049 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1052 panic("dqflush: stray dquot");
1053 LIST_REMOVE(dq, dq_hash);
1054 dq->dq_ump = (struct ufsmount *)0;