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 static int unprivileged_get_quota = 0;
62 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
63 &unprivileged_get_quota, 0,
64 "Unprivileged processes may retrieve quotas for other uids and gids");
66 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
69 * Quota name to error message mapping.
71 static char *quotatypes[] = INITQFNAMES;
73 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int);
74 static int chkiqchg(struct inode *, ino_t, struct ucred *, int);
75 static int dqget(struct vnode *,
76 u_long, struct ufsmount *, int, struct dquot **);
77 static int dqsync(struct vnode *, struct dquot *);
78 static void dqflush(struct vnode *);
81 static void dqref(struct dquot *);
82 static void chkdquot(struct inode *);
86 * Set up the quotas for an inode.
88 * This routine completely defines the semantics of quotas.
89 * If other criterion want to be used to establish quotas, the
90 * MAXQUOTAS value in quotas.h should be increased, and the
91 * additional dquots set up here.
98 struct vnode *vp = ITOV(ip);
101 #ifndef NO_FFS_SNAPSHOT
103 * Disk quotas must be turned off for snapshot files.
105 if ((ip->i_flags & SF_SNAPSHOT) != 0)
108 ump = VFSTOUFS(vp->v_mount);
110 * Set up the user quota based on file uid.
111 * EINVAL means that quotas are not enabled.
113 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
115 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
119 * Set up the group quota based on file gid.
120 * EINVAL means that quotas are not enabled.
122 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
124 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
131 * Update disk usage, and take corrective action.
134 chkdq(ip, change, cred, flags)
141 ufs2_daddr_t ncurblocks;
145 if ((flags & CHOWN) == 0)
151 for (i = 0; i < MAXQUOTAS; i++) {
152 if ((dq = ip->i_dquot[i]) == NODQUOT)
154 while (dq->dq_flags & DQ_LOCK) {
155 dq->dq_flags |= DQ_WANT;
156 (void) tsleep(dq, PINOD+1, "chkdq1", 0);
158 ncurblocks = dq->dq_curblocks + change;
160 dq->dq_curblocks = ncurblocks;
162 dq->dq_curblocks = 0;
163 dq->dq_flags &= ~DQ_BLKS;
164 dq->dq_flags |= DQ_MOD;
168 if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
169 for (i = 0; i < MAXQUOTAS; i++) {
170 if ((dq = ip->i_dquot[i]) == NODQUOT)
172 error = chkdqchg(ip, change, cred, i);
177 for (i = 0; i < MAXQUOTAS; i++) {
178 if ((dq = ip->i_dquot[i]) == NODQUOT)
180 while (dq->dq_flags & DQ_LOCK) {
181 dq->dq_flags |= DQ_WANT;
182 (void) tsleep(dq, PINOD+1, "chkdq2", 0);
184 /* Reset timer when crossing soft limit */
185 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
186 dq->dq_curblocks < dq->dq_bsoftlimit)
187 dq->dq_btime = time_second +
188 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
189 dq->dq_curblocks += change;
190 dq->dq_flags |= DQ_MOD;
196 * Check for a valid change to a users allocation.
197 * Issue an error message if appropriate.
200 chkdqchg(ip, change, cred, type)
206 struct dquot *dq = ip->i_dquot[type];
207 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
210 * If user would exceed their hard limit, disallow space allocation.
212 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
213 if ((dq->dq_flags & DQ_BLKS) == 0 &&
214 ip->i_uid == cred->cr_uid) {
215 uprintf("\n%s: write failed, %s disk limit reached\n",
216 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
218 dq->dq_flags |= DQ_BLKS;
223 * If user is over their soft limit for too long, disallow space
224 * allocation. Reset time limit as they cross their soft limit.
226 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
227 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
228 dq->dq_btime = time_second +
229 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
230 if (ip->i_uid == cred->cr_uid)
231 uprintf("\n%s: warning, %s %s\n",
232 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
233 quotatypes[type], "disk quota exceeded");
236 if (time_second > dq->dq_btime) {
237 if ((dq->dq_flags & DQ_BLKS) == 0 &&
238 ip->i_uid == cred->cr_uid) {
239 uprintf("\n%s: write failed, %s %s\n",
240 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
242 "disk quota exceeded for too long");
243 dq->dq_flags |= DQ_BLKS;
252 * Check the inode limit, applying corrective action.
255 chkiq(ip, change, cred, flags)
266 if ((flags & CHOWN) == 0)
271 /* XXX: change is unsigned */
273 for (i = 0; i < MAXQUOTAS; i++) {
274 if ((dq = ip->i_dquot[i]) == NODQUOT)
276 while (dq->dq_flags & DQ_LOCK) {
277 dq->dq_flags |= DQ_WANT;
278 (void) tsleep(dq, PINOD+1, "chkiq1", 0);
280 ncurinodes = dq->dq_curinodes + change;
281 /* XXX: ncurinodes is unsigned */
283 dq->dq_curinodes = ncurinodes;
285 dq->dq_curinodes = 0;
286 dq->dq_flags &= ~DQ_INODS;
287 dq->dq_flags |= DQ_MOD;
291 if ((flags & FORCE) == 0 && suser_cred(cred, 0)) {
292 for (i = 0; i < MAXQUOTAS; i++) {
293 if ((dq = ip->i_dquot[i]) == NODQUOT)
295 error = chkiqchg(ip, change, cred, i);
300 for (i = 0; i < MAXQUOTAS; i++) {
301 if ((dq = ip->i_dquot[i]) == NODQUOT)
303 while (dq->dq_flags & DQ_LOCK) {
304 dq->dq_flags |= DQ_WANT;
305 (void) tsleep(dq, PINOD+1, "chkiq2", 0);
307 /* Reset timer when crossing soft limit */
308 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
309 dq->dq_curinodes < dq->dq_isoftlimit)
310 dq->dq_itime = time_second +
311 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
312 dq->dq_curinodes += change;
313 dq->dq_flags |= DQ_MOD;
319 * Check for a valid change to a users allocation.
320 * Issue an error message if appropriate.
323 chkiqchg(ip, change, cred, type)
329 struct dquot *dq = ip->i_dquot[type];
330 ino_t ncurinodes = dq->dq_curinodes + change;
333 * If user would exceed their hard limit, disallow inode allocation.
335 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
336 if ((dq->dq_flags & DQ_INODS) == 0 &&
337 ip->i_uid == cred->cr_uid) {
338 uprintf("\n%s: write failed, %s inode limit reached\n",
339 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
341 dq->dq_flags |= DQ_INODS;
346 * If user is over their soft limit for too long, disallow inode
347 * allocation. Reset time limit as they cross their soft limit.
349 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
350 if (dq->dq_curinodes < dq->dq_isoftlimit) {
351 dq->dq_itime = time_second +
352 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
353 if (ip->i_uid == cred->cr_uid)
354 uprintf("\n%s: warning, %s %s\n",
355 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
356 quotatypes[type], "inode quota exceeded");
359 if (time_second > dq->dq_itime) {
360 if ((dq->dq_flags & DQ_INODS) == 0 &&
361 ip->i_uid == cred->cr_uid) {
362 uprintf("\n%s: write failed, %s %s\n",
363 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
365 "inode quota exceeded for too long");
366 dq->dq_flags |= DQ_INODS;
376 * On filesystems with quotas enabled, it is an error for a file to change
377 * size and not to have a dquot structure associated with it.
383 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
386 #ifndef NO_FFS_SNAPSHOT
388 * Disk quotas must be turned off for snapshot files.
390 if ((ip->i_flags & SF_SNAPSHOT) != 0)
393 for (i = 0; i < MAXQUOTAS; i++) {
394 if (ump->um_quotas[i] == NULLVP ||
395 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
397 if (ip->i_dquot[i] == NODQUOT) {
398 vprint("chkdquot: missing dquot", ITOV(ip));
399 panic("chkdquot: missing dquot");
406 * Code to process quotactl commands.
410 * Q_QUOTAON - set up a quota file for a particular filesystem.
413 quotaon(td, mp, type, fname)
419 struct ufsmount *ump = VFSTOUFS(mp);
420 struct vnode *vp, **vpp;
426 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
430 vpp = &ump->um_quotas[type];
431 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
432 flags = FREAD | FWRITE;
433 error = vn_open(&nd, &flags, 0, -1);
436 NDFREE(&nd, NDF_ONLY_PNBUF);
438 VOP_UNLOCK(vp, 0, td);
439 if (vp->v_type != VREG) {
440 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
444 quotaoff(td, mp, type);
445 ump->um_qflags[type] |= QTF_OPENING;
447 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) {
569 mp->mnt_flag &= ~MNT_QUOTA;
576 * Q_GETQUOTA - return current values in a dqblk structure.
579 getquota(td, mp, id, type, addr)
591 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
592 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
599 if (!groupmember(id, td->td_ucred) && !unprivileged_get_quota) {
600 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
610 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
613 error = copyout(&dq->dq_dqb, addr, sizeof (struct dqblk));
619 * Q_SETQUOTA - assign an entire dqblk structure.
622 setquota(td, mp, id, type, addr)
631 struct ufsmount *ump = VFSTOUFS(mp);
635 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
639 error = copyin(addr, &newlim, sizeof (struct dqblk));
642 error = dqget(NULLVP, id, ump, type, &ndq);
646 while (dq->dq_flags & DQ_LOCK) {
647 dq->dq_flags |= DQ_WANT;
648 (void) tsleep(dq, PINOD+1, "setqta", 0);
651 * Copy all but the current values.
652 * Reset time limit if previously had no soft limit or were
653 * under it, but now have a soft limit and are over it.
655 newlim.dqb_curblocks = dq->dq_curblocks;
656 newlim.dqb_curinodes = dq->dq_curinodes;
657 if (dq->dq_id != 0) {
658 newlim.dqb_btime = dq->dq_btime;
659 newlim.dqb_itime = dq->dq_itime;
661 if (newlim.dqb_bsoftlimit &&
662 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
663 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
664 newlim.dqb_btime = time_second + ump->um_btime[type];
665 if (newlim.dqb_isoftlimit &&
666 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
667 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
668 newlim.dqb_itime = time_second + ump->um_itime[type];
670 if (dq->dq_curblocks < dq->dq_bsoftlimit)
671 dq->dq_flags &= ~DQ_BLKS;
672 if (dq->dq_curinodes < dq->dq_isoftlimit)
673 dq->dq_flags &= ~DQ_INODS;
674 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
675 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
676 dq->dq_flags |= DQ_FAKE;
678 dq->dq_flags &= ~DQ_FAKE;
679 dq->dq_flags |= DQ_MOD;
685 * Q_SETUSE - set current inode and block usage.
688 setuse(td, mp, id, type, addr)
696 struct ufsmount *ump = VFSTOUFS(mp);
701 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
705 error = copyin(addr, &usage, sizeof (struct dqblk));
708 error = dqget(NULLVP, id, ump, type, &ndq);
712 while (dq->dq_flags & DQ_LOCK) {
713 dq->dq_flags |= DQ_WANT;
714 (void) tsleep(dq, PINOD+1, "setuse", 0);
717 * Reset time limit if have a soft limit and were
718 * previously under it, but are now over it.
720 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
721 usage.dqb_curblocks >= dq->dq_bsoftlimit)
722 dq->dq_btime = time_second + ump->um_btime[type];
723 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
724 usage.dqb_curinodes >= dq->dq_isoftlimit)
725 dq->dq_itime = time_second + ump->um_itime[type];
726 dq->dq_curblocks = usage.dqb_curblocks;
727 dq->dq_curinodes = usage.dqb_curinodes;
728 if (dq->dq_curblocks < dq->dq_bsoftlimit)
729 dq->dq_flags &= ~DQ_BLKS;
730 if (dq->dq_curinodes < dq->dq_isoftlimit)
731 dq->dq_flags &= ~DQ_INODS;
732 dq->dq_flags |= DQ_MOD;
738 * Q_SYNC - sync quota files to disk.
744 struct ufsmount *ump = VFSTOUFS(mp);
745 struct thread *td = curthread; /* XXX */
746 struct vnode *vp, *mvp;
751 * Check if the mount point has any quotas.
752 * If not, simply return.
754 for (i = 0; i < MAXQUOTAS; i++)
755 if (ump->um_quotas[i] != NULLVP)
760 * Search vnodes associated with this mount point,
761 * synchronizing any modified dquot structures.
765 MNT_VNODE_FOREACH(vp, mp, mvp) {
768 if (vp->v_type == VNON) {
773 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
776 if (error == ENOENT) {
777 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
782 for (i = 0; i < MAXQUOTAS; i++) {
783 dq = VTOI(vp)->i_dquot[i];
784 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
795 * Code pertaining to management of the in-core dquot data structures.
797 #define DQHASH(dqvp, id) \
798 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
799 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
800 static u_long dqhash;
805 #define DQUOTINC 5 /* minimum free dquots desired */
806 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
807 static long numdquot, desireddquot = DQUOTINC;
810 * Initialize the quota system.
816 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
817 TAILQ_INIT(&dqfreelist);
821 * Shut down the quota system.
828 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
829 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
830 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
836 * Obtain a dquot structure for the specified identifier and quota file
837 * reading the information from the file if necessary.
840 dqget(vp, id, ump, type, dqp)
843 struct ufsmount *ump;
847 struct thread *td = curthread; /* XXX */
855 dqvp = ump->um_quotas[type];
856 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
861 * Check the cache first.
863 dqh = DQHASH(dqvp, id);
864 LIST_FOREACH(dq, dqh, dq_hash) {
865 if (dq->dq_id != id ||
866 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
869 * Cache hit with no references. Take
870 * the structure off the free list.
873 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
879 * Not in cache, allocate a new one.
881 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
882 numdquot < MAXQUOTAS * desiredvnodes)
883 desireddquot += DQUOTINC;
884 if (numdquot < desireddquot) {
885 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT,
889 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
894 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
895 panic("dqget: free dquot isn't");
896 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
897 if (dq->dq_ump != NULL)
898 LIST_REMOVE(dq, dq_hash);
901 * Initialize the contents of the dquot structure.
904 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
905 LIST_INSERT_HEAD(dqh, dq, dq_hash);
907 dq->dq_flags = DQ_LOCK;
911 auio.uio_iov = &aiov;
913 aiov.iov_base = &dq->dq_dqb;
914 aiov.iov_len = sizeof (struct dqblk);
915 auio.uio_resid = sizeof (struct dqblk);
916 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
917 auio.uio_segflg = UIO_SYSSPACE;
918 auio.uio_rw = UIO_READ;
919 auio.uio_td = (struct thread *)0;
920 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
921 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
922 bzero(&dq->dq_dqb, sizeof(struct dqblk));
924 VOP_UNLOCK(dqvp, 0, td);
925 if (dq->dq_flags & DQ_WANT)
929 * I/O error in reading quota file, release
930 * quota structure and reflect problem to caller.
933 LIST_REMOVE(dq, dq_hash);
939 * Check for no limit to enforce.
940 * Initialize time values if necessary.
942 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
943 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
944 dq->dq_flags |= DQ_FAKE;
945 if (dq->dq_id != 0) {
946 if (dq->dq_btime == 0)
947 dq->dq_btime = time_second + ump->um_btime[type];
948 if (dq->dq_itime == 0)
949 dq->dq_itime = time_second + ump->um_itime[type];
957 * Obtain a reference to a dquot.
969 * Release a reference to a dquot.
979 if (dq->dq_cnt > 1) {
983 if (dq->dq_flags & DQ_MOD)
984 (void) dqsync(vp, dq);
985 if (--dq->dq_cnt > 0)
987 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
991 * Update the disk quota in the quota file.
998 struct thread *td = curthread; /* XXX */
1007 panic("dqsync: dquot");
1008 if ((dq->dq_flags & DQ_MOD) == 0)
1010 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
1011 panic("dqsync: file");
1012 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1014 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
1015 while (dq->dq_flags & DQ_LOCK) {
1016 dq->dq_flags |= DQ_WANT;
1017 (void) tsleep(dq, PINOD+2, "dqsync", 0);
1018 if ((dq->dq_flags & DQ_MOD) == 0) {
1020 VOP_UNLOCK(dqvp, 0, td);
1021 vn_finished_secondary_write(mp);
1025 dq->dq_flags |= DQ_LOCK;
1026 auio.uio_iov = &aiov;
1027 auio.uio_iovcnt = 1;
1028 aiov.iov_base = &dq->dq_dqb;
1029 aiov.iov_len = sizeof (struct dqblk);
1030 auio.uio_resid = sizeof (struct dqblk);
1031 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
1032 auio.uio_segflg = UIO_SYSSPACE;
1033 auio.uio_rw = UIO_WRITE;
1034 auio.uio_td = (struct thread *)0;
1035 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1036 if (auio.uio_resid && error == 0)
1038 if (dq->dq_flags & DQ_WANT)
1040 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
1042 VOP_UNLOCK(dqvp, 0, td);
1043 vn_finished_secondary_write(mp);
1048 * Flush all entries from the cache for a particular vnode.
1054 struct dquot *dq, *nextdq;
1058 * Move all dquot's that used to refer to this quota
1059 * file off their hash chains (they will eventually
1060 * fall off the head of the free list and be re-used).
1062 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1063 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1064 nextdq = LIST_NEXT(dq, dq_hash);
1065 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1068 panic("dqflush: stray dquot");
1069 LIST_REMOVE(dq, dq_hash);
1070 dq->dq_ump = (struct ufsmount *)0;