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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/namei.h>
44 #include <sys/malloc.h>
45 #include <sys/fcntl.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <vm/vm_zone.h>
51 #include <ufs/ufs/extattr.h>
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/ufsmount.h>
56 static MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
59 * Quota name to error message mapping.
61 static char *quotatypes[] = INITQFNAMES;
63 static int chkdqchg __P((struct inode *, long, struct ucred *, int));
64 static int chkiqchg __P((struct inode *, long, struct ucred *, int));
65 static int dqget __P((struct vnode *,
66 u_long, struct ufsmount *, int, struct dquot **));
67 static int dqsync __P((struct vnode *, struct dquot *));
68 static void dqflush __P((struct vnode *));
71 static void dqref __P((struct dquot *));
72 static void chkdquot __P((struct inode *));
76 * Set up the quotas for an inode.
78 * This routine completely defines the semantics of quotas.
79 * If other criterion want to be used to establish quotas, the
80 * MAXQUOTAS value in quotas.h should be increased, and the
81 * additional dquots set up here.
85 register struct inode *ip;
88 struct vnode *vp = ITOV(ip);
91 ump = VFSTOUFS(vp->v_mount);
93 * Set up the user quota based on file uid.
94 * EINVAL means that quotas are not enabled.
96 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
98 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
102 * Set up the group quota based on file gid.
103 * EINVAL means that quotas are not enabled.
105 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
107 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
114 * Update disk usage, and take corrective action.
117 chkdq(ip, change, cred, flags)
118 register struct inode *ip;
123 register struct dquot *dq;
125 int ncurblocks, error;
128 if ((flags & CHOWN) == 0)
134 for (i = 0; i < MAXQUOTAS; i++) {
135 if ((dq = ip->i_dquot[i]) == NODQUOT)
137 while (dq->dq_flags & DQ_LOCK) {
138 dq->dq_flags |= DQ_WANT;
139 (void) tsleep((caddr_t)dq, PINOD+1, "chkdq1", 0);
141 ncurblocks = dq->dq_curblocks + change;
143 dq->dq_curblocks = ncurblocks;
145 dq->dq_curblocks = 0;
146 dq->dq_flags &= ~DQ_BLKS;
147 dq->dq_flags |= DQ_MOD;
151 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
152 for (i = 0; i < MAXQUOTAS; i++) {
153 if ((dq = ip->i_dquot[i]) == NODQUOT)
155 error = chkdqchg(ip, change, cred, i);
160 for (i = 0; i < MAXQUOTAS; i++) {
161 if ((dq = ip->i_dquot[i]) == NODQUOT)
163 while (dq->dq_flags & DQ_LOCK) {
164 dq->dq_flags |= DQ_WANT;
165 (void) tsleep((caddr_t)dq, PINOD+1, "chkdq2", 0);
167 dq->dq_curblocks += change;
168 dq->dq_flags |= DQ_MOD;
174 * Check for a valid change to a users allocation.
175 * Issue an error message if appropriate.
178 chkdqchg(ip, change, cred, type)
184 register struct dquot *dq = ip->i_dquot[type];
185 long ncurblocks = dq->dq_curblocks + change;
188 * If user would exceed their hard limit, disallow space allocation.
190 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
191 if ((dq->dq_flags & DQ_BLKS) == 0 &&
192 ip->i_uid == cred->cr_uid) {
193 uprintf("\n%s: write failed, %s disk limit reached\n",
194 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
196 dq->dq_flags |= DQ_BLKS;
201 * If user is over their soft limit for too long, disallow space
202 * allocation. Reset time limit as they cross their soft limit.
204 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
205 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
206 dq->dq_btime = time_second +
207 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
208 if (ip->i_uid == cred->cr_uid)
209 uprintf("\n%s: warning, %s %s\n",
210 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
211 quotatypes[type], "disk quota exceeded");
214 if (time_second > dq->dq_btime) {
215 if ((dq->dq_flags & DQ_BLKS) == 0 &&
216 ip->i_uid == cred->cr_uid) {
217 uprintf("\n%s: write failed, %s %s\n",
218 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
220 "disk quota exceeded for too long");
221 dq->dq_flags |= DQ_BLKS;
230 * Check the inode limit, applying corrective action.
233 chkiq(ip, change, cred, flags)
234 register struct inode *ip;
239 register struct dquot *dq;
241 int ncurinodes, error;
244 if ((flags & CHOWN) == 0)
250 for (i = 0; i < MAXQUOTAS; i++) {
251 if ((dq = ip->i_dquot[i]) == NODQUOT)
253 while (dq->dq_flags & DQ_LOCK) {
254 dq->dq_flags |= DQ_WANT;
255 (void) tsleep((caddr_t)dq, PINOD+1, "chkiq1", 0);
257 ncurinodes = dq->dq_curinodes + change;
259 dq->dq_curinodes = ncurinodes;
261 dq->dq_curinodes = 0;
262 dq->dq_flags &= ~DQ_INODS;
263 dq->dq_flags |= DQ_MOD;
267 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
268 for (i = 0; i < MAXQUOTAS; i++) {
269 if ((dq = ip->i_dquot[i]) == NODQUOT)
271 error = chkiqchg(ip, change, cred, i);
276 for (i = 0; i < MAXQUOTAS; i++) {
277 if ((dq = ip->i_dquot[i]) == NODQUOT)
279 while (dq->dq_flags & DQ_LOCK) {
280 dq->dq_flags |= DQ_WANT;
281 (void) tsleep((caddr_t)dq, PINOD+1, "chkiq2", 0);
283 dq->dq_curinodes += change;
284 dq->dq_flags |= DQ_MOD;
290 * Check for a valid change to a users allocation.
291 * Issue an error message if appropriate.
294 chkiqchg(ip, change, cred, type)
300 register struct dquot *dq = ip->i_dquot[type];
301 long ncurinodes = dq->dq_curinodes + change;
304 * If user would exceed their hard limit, disallow inode allocation.
306 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
307 if ((dq->dq_flags & DQ_INODS) == 0 &&
308 ip->i_uid == cred->cr_uid) {
309 uprintf("\n%s: write failed, %s inode limit reached\n",
310 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
312 dq->dq_flags |= DQ_INODS;
317 * If user is over their soft limit for too long, disallow inode
318 * allocation. Reset time limit as they cross their soft limit.
320 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
321 if (dq->dq_curinodes < dq->dq_isoftlimit) {
322 dq->dq_itime = time_second +
323 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
324 if (ip->i_uid == cred->cr_uid)
325 uprintf("\n%s: warning, %s %s\n",
326 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
327 quotatypes[type], "inode quota exceeded");
330 if (time_second > dq->dq_itime) {
331 if ((dq->dq_flags & DQ_INODS) == 0 &&
332 ip->i_uid == cred->cr_uid) {
333 uprintf("\n%s: write failed, %s %s\n",
334 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
336 "inode quota exceeded for too long");
337 dq->dq_flags |= DQ_INODS;
347 * On filesystems with quotas enabled, it is an error for a file to change
348 * size and not to have a dquot structure associated with it.
352 register struct inode *ip;
354 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
357 for (i = 0; i < MAXQUOTAS; i++) {
358 if (ump->um_quotas[i] == NULLVP ||
359 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
361 if (ip->i_dquot[i] == NODQUOT) {
362 vprint("chkdquot: missing dquot", ITOV(ip));
363 panic("chkdquot: missing dquot");
370 * Code to process quotactl commands.
374 * Q_QUOTAON - set up a quota file for a particular file system.
377 quotaon(p, mp, type, fname)
383 struct ufsmount *ump = VFSTOUFS(mp);
384 struct vnode *vp, **vpp;
385 struct vnode *nextvp;
390 vpp = &ump->um_quotas[type];
391 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, p);
392 error = vn_open(&nd, FREAD|FWRITE, 0);
395 NDFREE(&nd, NDF_ONLY_PNBUF);
397 VOP_UNLOCK(vp, 0, p);
398 if (vp->v_type != VREG) {
399 (void) vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
403 quotaoff(p, mp, type);
404 ump->um_qflags[type] |= QTF_OPENING;
405 mp->mnt_flag |= MNT_QUOTA;
406 vp->v_flag |= VSYSTEM;
409 * Save the credential of the process that turned on quotas.
410 * Set up the time limits for this quota.
413 ump->um_cred[type] = p->p_ucred;
414 ump->um_btime[type] = MAX_DQ_TIME;
415 ump->um_itime[type] = MAX_IQ_TIME;
416 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
417 if (dq->dq_btime > 0)
418 ump->um_btime[type] = dq->dq_btime;
419 if (dq->dq_itime > 0)
420 ump->um_itime[type] = dq->dq_itime;
424 * Search vnodes associated with this mount point,
425 * adding references to quota file being opened.
426 * NB: only need to add dquot's for inodes being modified.
429 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
430 nextvp = vp->v_mntvnodes.le_next;
431 if (vp->v_type == VNON || vp->v_writecount == 0)
433 if (vget(vp, LK_EXCLUSIVE, p))
435 error = getinoquota(VTOI(vp));
441 if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
444 ump->um_qflags[type] &= ~QTF_OPENING;
446 quotaoff(p, mp, type);
451 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
454 quotaoff(p, mp, type)
460 struct vnode *qvp, *nextvp;
461 struct ufsmount *ump = VFSTOUFS(mp);
466 if ((qvp = ump->um_quotas[type]) == NULLVP)
468 ump->um_qflags[type] |= QTF_CLOSING;
470 * Search vnodes associated with this mount point,
471 * deleting any references to quota file being closed.
474 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
475 nextvp = vp->v_mntvnodes.le_next;
476 if (vp->v_type == VNON)
478 if (vget(vp, LK_EXCLUSIVE, p))
481 dq = ip->i_dquot[type];
482 ip->i_dquot[type] = NODQUOT;
485 if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
489 qvp->v_flag &= ~VSYSTEM;
490 error = vn_close(qvp, FREAD|FWRITE, p->p_ucred, p);
491 ump->um_quotas[type] = NULLVP;
492 crfree(ump->um_cred[type]);
493 ump->um_cred[type] = NOCRED;
494 ump->um_qflags[type] &= ~QTF_CLOSING;
495 for (type = 0; type < MAXQUOTAS; type++)
496 if (ump->um_quotas[type] != NULLVP)
498 if (type == MAXQUOTAS)
499 mp->mnt_flag &= ~MNT_QUOTA;
504 * Q_GETQUOTA - return current values in a dqblk structure.
507 getquota(mp, id, type, addr)
516 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
519 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
525 * Q_SETQUOTA - assign an entire dqblk structure.
528 setquota(mp, id, type, addr)
534 register struct dquot *dq;
536 struct ufsmount *ump = VFSTOUFS(mp);
540 error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
543 error = dqget(NULLVP, id, ump, type, &ndq);
547 while (dq->dq_flags & DQ_LOCK) {
548 dq->dq_flags |= DQ_WANT;
549 (void) tsleep((caddr_t)dq, PINOD+1, "setqta", 0);
552 * Copy all but the current values.
553 * Reset time limit if previously had no soft limit or were
554 * under it, but now have a soft limit and are over it.
556 newlim.dqb_curblocks = dq->dq_curblocks;
557 newlim.dqb_curinodes = dq->dq_curinodes;
558 if (dq->dq_id != 0) {
559 newlim.dqb_btime = dq->dq_btime;
560 newlim.dqb_itime = dq->dq_itime;
562 if (newlim.dqb_bsoftlimit &&
563 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
564 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
565 newlim.dqb_btime = time_second + ump->um_btime[type];
566 if (newlim.dqb_isoftlimit &&
567 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
568 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
569 newlim.dqb_itime = time_second + ump->um_itime[type];
571 if (dq->dq_curblocks < dq->dq_bsoftlimit)
572 dq->dq_flags &= ~DQ_BLKS;
573 if (dq->dq_curinodes < dq->dq_isoftlimit)
574 dq->dq_flags &= ~DQ_INODS;
575 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
576 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
577 dq->dq_flags |= DQ_FAKE;
579 dq->dq_flags &= ~DQ_FAKE;
580 dq->dq_flags |= DQ_MOD;
586 * Q_SETUSE - set current inode and block usage.
589 setuse(mp, id, type, addr)
595 register struct dquot *dq;
596 struct ufsmount *ump = VFSTOUFS(mp);
601 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
604 error = dqget(NULLVP, id, ump, type, &ndq);
608 while (dq->dq_flags & DQ_LOCK) {
609 dq->dq_flags |= DQ_WANT;
610 (void) tsleep((caddr_t)dq, PINOD+1, "setuse", 0);
613 * Reset time limit if have a soft limit and were
614 * previously under it, but are now over it.
616 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
617 usage.dqb_curblocks >= dq->dq_bsoftlimit)
618 dq->dq_btime = time_second + ump->um_btime[type];
619 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
620 usage.dqb_curinodes >= dq->dq_isoftlimit)
621 dq->dq_itime = time_second + ump->um_itime[type];
622 dq->dq_curblocks = usage.dqb_curblocks;
623 dq->dq_curinodes = usage.dqb_curinodes;
624 if (dq->dq_curblocks < dq->dq_bsoftlimit)
625 dq->dq_flags &= ~DQ_BLKS;
626 if (dq->dq_curinodes < dq->dq_isoftlimit)
627 dq->dq_flags &= ~DQ_INODS;
628 dq->dq_flags |= DQ_MOD;
634 * Q_SYNC - sync quota files to disk.
640 struct ufsmount *ump = VFSTOUFS(mp);
641 struct proc *p = curproc; /* XXX */
642 struct vnode *vp, *nextvp;
647 * Check if the mount point has any quotas.
648 * If not, simply return.
650 for (i = 0; i < MAXQUOTAS; i++)
651 if (ump->um_quotas[i] != NULLVP)
656 * Search vnodes associated with this mount point,
657 * synchronizing any modified dquot structures.
659 simple_lock(&mntvnode_slock);
661 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
662 if (vp->v_mount != mp)
664 nextvp = vp->v_mntvnodes.le_next;
665 if (vp->v_type == VNON)
667 simple_lock(&vp->v_interlock);
668 simple_unlock(&mntvnode_slock);
669 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
671 simple_lock(&mntvnode_slock);
676 for (i = 0; i < MAXQUOTAS; i++) {
677 dq = VTOI(vp)->i_dquot[i];
678 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
682 simple_lock(&mntvnode_slock);
683 if (vp->v_mntvnodes.le_next != nextvp)
686 simple_unlock(&mntvnode_slock);
691 * Code pertaining to management of the in-core dquot data structures.
693 #define DQHASH(dqvp, id) \
694 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
695 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
696 static u_long dqhash;
701 #define DQUOTINC 5 /* minimum free dquots desired */
702 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
703 static long numdquot, desireddquot = DQUOTINC;
706 * Initialize the quota system.
712 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
713 TAILQ_INIT(&dqfreelist);
717 * Obtain a dquot structure for the specified identifier and quota file
718 * reading the information from the file if necessary.
721 dqget(vp, id, ump, type, dqp)
724 register struct ufsmount *ump;
728 struct proc *p = curproc; /* XXX */
736 dqvp = ump->um_quotas[type];
737 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
742 * Check the cache first.
744 dqh = DQHASH(dqvp, id);
745 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
746 if (dq->dq_id != id ||
747 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
750 * Cache hit with no references. Take
751 * the structure off the free list.
754 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
760 * Not in cache, allocate a new one.
762 if (dqfreelist.tqh_first == NODQUOT &&
763 numdquot < MAXQUOTAS * desiredvnodes)
764 desireddquot += DQUOTINC;
765 if (numdquot < desireddquot) {
766 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
767 bzero((char *)dq, sizeof *dq);
770 if ((dq = dqfreelist.tqh_first) == NULL) {
775 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
776 panic("dqget: free dquot isn't");
777 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
778 LIST_REMOVE(dq, dq_hash);
781 * Initialize the contents of the dquot structure.
784 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, p);
785 LIST_INSERT_HEAD(dqh, dq, dq_hash);
787 dq->dq_flags = DQ_LOCK;
791 auio.uio_iov = &aiov;
793 aiov.iov_base = (caddr_t)&dq->dq_dqb;
794 aiov.iov_len = sizeof (struct dqblk);
795 auio.uio_resid = sizeof (struct dqblk);
796 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
797 auio.uio_segflg = UIO_SYSSPACE;
798 auio.uio_rw = UIO_READ;
799 auio.uio_procp = (struct proc *)0;
800 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
801 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
802 bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
804 VOP_UNLOCK(dqvp, 0, p);
805 if (dq->dq_flags & DQ_WANT)
809 * I/O error in reading quota file, release
810 * quota structure and reflect problem to caller.
813 LIST_REMOVE(dq, dq_hash);
819 * Check for no limit to enforce.
820 * Initialize time values if necessary.
822 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
823 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
824 dq->dq_flags |= DQ_FAKE;
825 if (dq->dq_id != 0) {
826 if (dq->dq_btime == 0)
827 dq->dq_btime = time_second + ump->um_btime[type];
828 if (dq->dq_itime == 0)
829 dq->dq_itime = time_second + ump->um_itime[type];
837 * Obtain a reference to a dquot.
849 * Release a reference to a dquot.
854 register struct dquot *dq;
859 if (dq->dq_cnt > 1) {
863 if (dq->dq_flags & DQ_MOD)
864 (void) dqsync(vp, dq);
865 if (--dq->dq_cnt > 0)
867 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
871 * Update the disk quota in the quota file.
878 struct proc *p = curproc; /* XXX */
885 panic("dqsync: dquot");
886 if ((dq->dq_flags & DQ_MOD) == 0)
888 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
889 panic("dqsync: file");
891 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, p);
892 while (dq->dq_flags & DQ_LOCK) {
893 dq->dq_flags |= DQ_WANT;
894 (void) tsleep((caddr_t)dq, PINOD+2, "dqsync", 0);
895 if ((dq->dq_flags & DQ_MOD) == 0) {
897 VOP_UNLOCK(dqvp, 0, p);
901 dq->dq_flags |= DQ_LOCK;
902 auio.uio_iov = &aiov;
904 aiov.iov_base = (caddr_t)&dq->dq_dqb;
905 aiov.iov_len = sizeof (struct dqblk);
906 auio.uio_resid = sizeof (struct dqblk);
907 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
908 auio.uio_segflg = UIO_SYSSPACE;
909 auio.uio_rw = UIO_WRITE;
910 auio.uio_procp = (struct proc *)0;
911 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
912 if (auio.uio_resid && error == 0)
914 if (dq->dq_flags & DQ_WANT)
916 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
918 VOP_UNLOCK(dqvp, 0, p);
923 * Flush all entries from the cache for a particular vnode.
927 register struct vnode *vp;
929 register struct dquot *dq, *nextdq;
933 * Move all dquot's that used to refer to this quota
934 * file off their hash chains (they will eventually
935 * fall off the head of the free list and be re-used).
937 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
938 for (dq = dqh->lh_first; dq; dq = nextdq) {
939 nextdq = dq->dq_hash.le_next;
940 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
943 panic("dqflush: stray dquot");
944 LIST_REMOVE(dq, dq_hash);
945 dq->dq_ump = (struct ufsmount *)0;