2 * Copyright (c) 1982, 1986, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/mutex.h>
48 #include <sys/namei.h>
50 #include <sys/socket.h>
52 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
61 SYSCTL_DECL(_security_bsd);
63 static int unprivileged_get_quota = 0;
64 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
65 &unprivileged_get_quota, 0,
66 "Unprivileged processes may retrieve quotas for other uids and gids");
68 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
71 * Quota name to error message mapping.
73 static char *quotatypes[] = INITQFNAMES;
75 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int);
76 static int chkiqchg(struct inode *, ino_t, struct ucred *, int);
77 static int dqget(struct vnode *,
78 u_long, struct ufsmount *, int, struct dquot **);
79 static int dqsync(struct vnode *, struct dquot *);
80 static void dqflush(struct vnode *);
83 static void dqref(struct dquot *);
84 static void chkdquot(struct inode *);
88 * Set up the quotas for an inode.
90 * This routine completely defines the semantics of quotas.
91 * If other criterion want to be used to establish quotas, the
92 * MAXQUOTAS value in quotas.h should be increased, and the
93 * additional dquots set up here.
100 struct vnode *vp = ITOV(ip);
103 #ifndef NO_FFS_SNAPSHOT
105 * Disk quotas must be turned off for snapshot files.
107 if ((ip->i_flags & SF_SNAPSHOT) != 0)
110 ump = VFSTOUFS(vp->v_mount);
112 * Set up the user quota based on file uid.
113 * EINVAL means that quotas are not enabled.
115 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
117 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
121 * Set up the group quota based on file gid.
122 * EINVAL means that quotas are not enabled.
124 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
126 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
133 * Update disk usage, and take corrective action.
136 chkdq(ip, change, cred, flags)
143 ufs2_daddr_t ncurblocks;
147 if ((flags & CHOWN) == 0)
153 for (i = 0; i < MAXQUOTAS; i++) {
154 if ((dq = ip->i_dquot[i]) == NODQUOT)
156 while (dq->dq_flags & DQ_LOCK) {
157 dq->dq_flags |= DQ_WANT;
158 (void) tsleep(dq, PINOD+1, "chkdq1", 0);
160 ncurblocks = dq->dq_curblocks + change;
162 dq->dq_curblocks = ncurblocks;
164 dq->dq_curblocks = 0;
165 dq->dq_flags &= ~DQ_BLKS;
166 dq->dq_flags |= DQ_MOD;
170 if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
171 for (i = 0; i < MAXQUOTAS; i++) {
172 if ((dq = ip->i_dquot[i]) == NODQUOT)
174 error = chkdqchg(ip, change, cred, i);
179 for (i = 0; i < MAXQUOTAS; i++) {
180 if ((dq = ip->i_dquot[i]) == NODQUOT)
182 while (dq->dq_flags & DQ_LOCK) {
183 dq->dq_flags |= DQ_WANT;
184 (void) tsleep(dq, PINOD+1, "chkdq2", 0);
186 /* Reset timer when crossing soft limit */
187 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
188 dq->dq_curblocks < dq->dq_bsoftlimit)
189 dq->dq_btime = time_second +
190 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
191 dq->dq_curblocks += change;
192 dq->dq_flags |= DQ_MOD;
198 * Check for a valid change to a users allocation.
199 * Issue an error message if appropriate.
202 chkdqchg(ip, change, cred, type)
208 struct dquot *dq = ip->i_dquot[type];
209 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
212 * If user would exceed their hard limit, disallow space allocation.
214 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
215 if ((dq->dq_flags & DQ_BLKS) == 0 &&
216 ip->i_uid == cred->cr_uid) {
217 uprintf("\n%s: write failed, %s disk limit reached\n",
218 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
220 dq->dq_flags |= DQ_BLKS;
225 * If user is over their soft limit for too long, disallow space
226 * allocation. Reset time limit as they cross their soft limit.
228 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
229 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
230 dq->dq_btime = time_second +
231 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
232 if (ip->i_uid == cred->cr_uid)
233 uprintf("\n%s: warning, %s %s\n",
234 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
235 quotatypes[type], "disk quota exceeded");
238 if (time_second > dq->dq_btime) {
239 if ((dq->dq_flags & DQ_BLKS) == 0 &&
240 ip->i_uid == cred->cr_uid) {
241 uprintf("\n%s: write failed, %s %s\n",
242 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
244 "disk quota exceeded for too long");
245 dq->dq_flags |= DQ_BLKS;
254 * Check the inode limit, applying corrective action.
257 chkiq(ip, change, cred, flags)
268 if ((flags & CHOWN) == 0)
273 /* XXX: change is unsigned */
275 for (i = 0; i < MAXQUOTAS; i++) {
276 if ((dq = ip->i_dquot[i]) == NODQUOT)
278 while (dq->dq_flags & DQ_LOCK) {
279 dq->dq_flags |= DQ_WANT;
280 (void) tsleep(dq, PINOD+1, "chkiq1", 0);
282 ncurinodes = dq->dq_curinodes + change;
283 /* XXX: ncurinodes is unsigned */
285 dq->dq_curinodes = ncurinodes;
287 dq->dq_curinodes = 0;
288 dq->dq_flags &= ~DQ_INODS;
289 dq->dq_flags |= DQ_MOD;
293 if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
294 for (i = 0; i < MAXQUOTAS; i++) {
295 if ((dq = ip->i_dquot[i]) == NODQUOT)
297 error = chkiqchg(ip, change, cred, i);
302 for (i = 0; i < MAXQUOTAS; i++) {
303 if ((dq = ip->i_dquot[i]) == NODQUOT)
305 while (dq->dq_flags & DQ_LOCK) {
306 dq->dq_flags |= DQ_WANT;
307 (void) tsleep(dq, PINOD+1, "chkiq2", 0);
309 /* Reset timer when crossing soft limit */
310 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
311 dq->dq_curinodes < dq->dq_isoftlimit)
312 dq->dq_itime = time_second +
313 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
314 dq->dq_curinodes += change;
315 dq->dq_flags |= DQ_MOD;
321 * Check for a valid change to a users allocation.
322 * Issue an error message if appropriate.
325 chkiqchg(ip, change, cred, type)
331 struct dquot *dq = ip->i_dquot[type];
332 ino_t ncurinodes = dq->dq_curinodes + change;
335 * If user would exceed their hard limit, disallow inode allocation.
337 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
338 if ((dq->dq_flags & DQ_INODS) == 0 &&
339 ip->i_uid == cred->cr_uid) {
340 uprintf("\n%s: write failed, %s inode limit reached\n",
341 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
343 dq->dq_flags |= DQ_INODS;
348 * If user is over their soft limit for too long, disallow inode
349 * allocation. Reset time limit as they cross their soft limit.
351 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
352 if (dq->dq_curinodes < dq->dq_isoftlimit) {
353 dq->dq_itime = time_second +
354 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
355 if (ip->i_uid == cred->cr_uid)
356 uprintf("\n%s: warning, %s %s\n",
357 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
358 quotatypes[type], "inode quota exceeded");
361 if (time_second > dq->dq_itime) {
362 if ((dq->dq_flags & DQ_INODS) == 0 &&
363 ip->i_uid == cred->cr_uid) {
364 uprintf("\n%s: write failed, %s %s\n",
365 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
367 "inode quota exceeded for too long");
368 dq->dq_flags |= DQ_INODS;
378 * On filesystems with quotas enabled, it is an error for a file to change
379 * size and not to have a dquot structure associated with it.
385 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
388 #ifndef NO_FFS_SNAPSHOT
390 * Disk quotas must be turned off for snapshot files.
392 if ((ip->i_flags & SF_SNAPSHOT) != 0)
395 for (i = 0; i < MAXQUOTAS; i++) {
396 if (ump->um_quotas[i] == NULLVP ||
397 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
399 if (ip->i_dquot[i] == NODQUOT) {
400 vprint("chkdquot: missing dquot", ITOV(ip));
401 panic("chkdquot: missing dquot");
408 * Code to process quotactl commands.
412 * Q_QUOTAON - set up a quota file for a particular filesystem.
415 quotaon(td, mp, type, fname)
421 struct ufsmount *ump = VFSTOUFS(mp);
422 struct vnode *vp, **vpp;
428 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
432 vpp = &ump->um_quotas[type];
433 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
434 flags = FREAD | FWRITE;
435 error = vn_open(&nd, &flags, 0, -1);
438 NDFREE(&nd, NDF_ONLY_PNBUF);
440 VOP_UNLOCK(vp, 0, td);
441 if (vp->v_type != VREG) {
442 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
446 quotaoff(td, mp, type);
447 ump->um_qflags[type] |= QTF_OPENING;
448 mp->mnt_flag |= MNT_QUOTA;
449 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
450 vp->v_vflag |= VV_SYSTEM;
451 VOP_UNLOCK(vp, 0, td);
454 * Save the credential of the process that turned on quotas.
455 * Set up the time limits for this quota.
457 ump->um_cred[type] = crhold(td->td_ucred);
458 ump->um_btime[type] = MAX_DQ_TIME;
459 ump->um_itime[type] = MAX_IQ_TIME;
460 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
461 if (dq->dq_btime > 0)
462 ump->um_btime[type] = dq->dq_btime;
463 if (dq->dq_itime > 0)
464 ump->um_itime[type] = dq->dq_itime;
468 * Search vnodes associated with this mount point,
469 * adding references to quota file being opened.
470 * NB: only need to add dquot's for inodes being modified.
474 MNT_VNODE_FOREACH(vp, mp, mvp) {
477 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
479 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
482 if (vp->v_type == VNON || vp->v_writecount == 0) {
483 VOP_UNLOCK(vp, 0, td);
488 error = getinoquota(VTOI(vp));
489 VOP_UNLOCK(vp, 0, td);
493 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
498 ump->um_qflags[type] &= ~QTF_OPENING;
500 quotaoff(td, mp, type);
505 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
508 quotaoff(td, mp, type)
514 struct vnode *qvp, *mvp;
515 struct ufsmount *ump = VFSTOUFS(mp);
520 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
524 if ((qvp = ump->um_quotas[type]) == NULLVP)
526 ump->um_qflags[type] |= QTF_CLOSING;
528 * Search vnodes associated with this mount point,
529 * deleting any references to quota file being closed.
533 MNT_VNODE_FOREACH(vp, mp, mvp) {
536 if (vp->v_type == VNON) {
541 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
543 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
547 dq = ip->i_dquot[type];
548 ip->i_dquot[type] = NODQUOT;
550 VOP_UNLOCK(vp, 0, td);
556 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY, td);
557 qvp->v_vflag &= ~VV_SYSTEM;
558 VOP_UNLOCK(qvp, 0, td);
559 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
560 ump->um_quotas[type] = NULLVP;
561 crfree(ump->um_cred[type]);
562 ump->um_cred[type] = NOCRED;
563 ump->um_qflags[type] &= ~QTF_CLOSING;
564 for (type = 0; type < MAXQUOTAS; type++)
565 if (ump->um_quotas[type] != NULLVP)
567 if (type == MAXQUOTAS)
568 mp->mnt_flag &= ~MNT_QUOTA;
573 * Q_GETQUOTA - return current values in a dqblk structure.
576 getquota(td, mp, id, type, addr)
588 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
589 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
596 if (!groupmember(id, td->td_ucred) && !unprivileged_get_quota) {
597 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
607 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
610 error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
616 * Q_SETQUOTA - assign an entire dqblk structure.
619 setquota(td, mp, id, type, addr)
628 struct ufsmount *ump = VFSTOUFS(mp);
632 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
636 error = copyin(addr, &newlim, sizeof (struct dqblk));
639 error = dqget(NULLVP, id, ump, type, &ndq);
643 while (dq->dq_flags & DQ_LOCK) {
644 dq->dq_flags |= DQ_WANT;
645 (void) tsleep(dq, PINOD+1, "setqta", 0);
648 * Copy all but the current values.
649 * Reset time limit if previously had no soft limit or were
650 * under it, but now have a soft limit and are over it.
652 newlim.dqb_curblocks = dq->dq_curblocks;
653 newlim.dqb_curinodes = dq->dq_curinodes;
654 if (dq->dq_id != 0) {
655 newlim.dqb_btime = dq->dq_btime;
656 newlim.dqb_itime = dq->dq_itime;
658 if (newlim.dqb_bsoftlimit &&
659 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
660 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
661 newlim.dqb_btime = time_second + ump->um_btime[type];
662 if (newlim.dqb_isoftlimit &&
663 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
664 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
665 newlim.dqb_itime = time_second + ump->um_itime[type];
667 if (dq->dq_curblocks < dq->dq_bsoftlimit)
668 dq->dq_flags &= ~DQ_BLKS;
669 if (dq->dq_curinodes < dq->dq_isoftlimit)
670 dq->dq_flags &= ~DQ_INODS;
671 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
672 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
673 dq->dq_flags |= DQ_FAKE;
675 dq->dq_flags &= ~DQ_FAKE;
676 dq->dq_flags |= DQ_MOD;
682 * Q_SETUSE - set current inode and block usage.
685 setuse(td, mp, id, type, addr)
693 struct ufsmount *ump = VFSTOUFS(mp);
698 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
702 error = copyin(addr, &usage, sizeof (struct dqblk));
705 error = dqget(NULLVP, id, ump, type, &ndq);
709 while (dq->dq_flags & DQ_LOCK) {
710 dq->dq_flags |= DQ_WANT;
711 (void) tsleep(dq, PINOD+1, "setuse", 0);
714 * Reset time limit if have a soft limit and were
715 * previously under it, but are now over it.
717 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
718 usage.dqb_curblocks >= dq->dq_bsoftlimit)
719 dq->dq_btime = time_second + ump->um_btime[type];
720 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
721 usage.dqb_curinodes >= dq->dq_isoftlimit)
722 dq->dq_itime = time_second + ump->um_itime[type];
723 dq->dq_curblocks = usage.dqb_curblocks;
724 dq->dq_curinodes = usage.dqb_curinodes;
725 if (dq->dq_curblocks < dq->dq_bsoftlimit)
726 dq->dq_flags &= ~DQ_BLKS;
727 if (dq->dq_curinodes < dq->dq_isoftlimit)
728 dq->dq_flags &= ~DQ_INODS;
729 dq->dq_flags |= DQ_MOD;
735 * Q_SYNC - sync quota files to disk.
741 struct ufsmount *ump = VFSTOUFS(mp);
742 struct thread *td = curthread; /* XXX */
743 struct vnode *vp, *mvp;
748 * Check if the mount point has any quotas.
749 * If not, simply return.
751 for (i = 0; i < MAXQUOTAS; i++)
752 if (ump->um_quotas[i] != NULLVP)
757 * Search vnodes associated with this mount point,
758 * synchronizing any modified dquot structures.
762 MNT_VNODE_FOREACH(vp, mp, mvp) {
765 if (vp->v_type == VNON) {
770 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
773 if (error == ENOENT) {
774 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
779 for (i = 0; i < MAXQUOTAS; i++) {
780 dq = VTOI(vp)->i_dquot[i];
781 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
792 * Code pertaining to management of the in-core dquot data structures.
794 #define DQHASH(dqvp, id) \
795 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
796 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
797 static u_long dqhash;
802 #define DQUOTINC 5 /* minimum free dquots desired */
803 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
804 static long numdquot, desireddquot = DQUOTINC;
807 * Initialize the quota system.
813 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
814 TAILQ_INIT(&dqfreelist);
818 * Shut down the quota system.
825 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
826 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
827 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
833 * Obtain a dquot structure for the specified identifier and quota file
834 * reading the information from the file if necessary.
837 dqget(vp, id, ump, type, dqp)
840 struct ufsmount *ump;
844 struct thread *td = curthread; /* XXX */
852 dqvp = ump->um_quotas[type];
853 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
858 * Check the cache first.
860 dqh = DQHASH(dqvp, id);
861 LIST_FOREACH(dq, dqh, dq_hash) {
862 if (dq->dq_id != id ||
863 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
866 * Cache hit with no references. Take
867 * the structure off the free list.
870 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
876 * Not in cache, allocate a new one.
878 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
879 numdquot < MAXQUOTAS * desiredvnodes)
880 desireddquot += DQUOTINC;
881 if (numdquot < desireddquot) {
882 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
886 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
891 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
892 panic("dqget: free dquot isn't");
893 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
894 if (dq->dq_ump != NULL)
895 LIST_REMOVE(dq, dq_hash);
898 * Initialize the contents of the dquot structure.
901 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
902 LIST_INSERT_HEAD(dqh, dq, dq_hash);
904 dq->dq_flags = DQ_LOCK;
908 auio.uio_iov = &aiov;
910 aiov.iov_base = &dq->dq_dqb;
911 aiov.iov_len = sizeof (struct dqblk);
912 auio.uio_resid = sizeof (struct dqblk);
913 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
914 auio.uio_segflg = UIO_SYSSPACE;
915 auio.uio_rw = UIO_READ;
916 auio.uio_td = (struct thread *)0;
917 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
918 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
919 bzero(&dq->dq_dqb, sizeof(struct dqblk));
921 VOP_UNLOCK(dqvp, 0, td);
922 if (dq->dq_flags & DQ_WANT)
926 * I/O error in reading quota file, release
927 * quota structure and reflect problem to caller.
930 LIST_REMOVE(dq, dq_hash);
936 * Check for no limit to enforce.
937 * Initialize time values if necessary.
939 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
940 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
941 dq->dq_flags |= DQ_FAKE;
942 if (dq->dq_id != 0) {
943 if (dq->dq_btime == 0)
944 dq->dq_btime = time_second + ump->um_btime[type];
945 if (dq->dq_itime == 0)
946 dq->dq_itime = time_second + ump->um_itime[type];
954 * Obtain a reference to a dquot.
966 * Release a reference to a dquot.
976 if (dq->dq_cnt > 1) {
980 if (dq->dq_flags & DQ_MOD)
981 (void) dqsync(vp, dq);
982 if (--dq->dq_cnt > 0)
984 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
988 * Update the disk quota in the quota file.
995 struct thread *td = curthread; /* XXX */
1004 panic("dqsync: dquot");
1005 if ((dq->dq_flags & DQ_MOD) == 0)
1007 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
1008 panic("dqsync: file");
1009 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1011 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
1012 while (dq->dq_flags & DQ_LOCK) {
1013 dq->dq_flags |= DQ_WANT;
1014 (void) tsleep(dq, PINOD+2, "dqsync", 0);
1015 if ((dq->dq_flags & DQ_MOD) == 0) {
1017 VOP_UNLOCK(dqvp, 0, td);
1018 vn_finished_secondary_write(mp);
1022 dq->dq_flags |= DQ_LOCK;
1023 auio.uio_iov = &aiov;
1024 auio.uio_iovcnt = 1;
1025 aiov.iov_base = &dq->dq_dqb;
1026 aiov.iov_len = sizeof (struct dqblk);
1027 auio.uio_resid = sizeof (struct dqblk);
1028 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
1029 auio.uio_segflg = UIO_SYSSPACE;
1030 auio.uio_rw = UIO_WRITE;
1031 auio.uio_td = (struct thread *)0;
1032 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1033 if (auio.uio_resid && error == 0)
1035 if (dq->dq_flags & DQ_WANT)
1037 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
1039 VOP_UNLOCK(dqvp, 0, td);
1040 vn_finished_secondary_write(mp);
1045 * Flush all entries from the cache for a particular vnode.
1051 struct dquot *dq, *nextdq;
1055 * Move all dquot's that used to refer to this quota
1056 * file off their hash chains (they will eventually
1057 * fall off the head of the free list and be re-used).
1059 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1060 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1061 nextdq = LIST_NEXT(dq, dq_hash);
1062 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1065 panic("dqflush: stray dquot");
1066 LIST_REMOVE(dq, dq_hash);
1067 dq->dq_ump = (struct ufsmount *)0;