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
37 * $FreeBSD: src/sys/ufs/ufs/ufs_quota.c,v 1.27.2.3 2002/01/15 10:33:32 phk Exp $
38 * $DragonFly: src/sys/vfs/ufs/ufs_quota.c,v 1.16 2004/10/12 19:21:12 dillon Exp $
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/fcntl.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <vm/vm_zone.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 (struct inode *, long, struct ucred *, int);
64 static int chkiqchg (struct inode *, long, struct ucred *, int);
65 static int dqget (struct vnode *,
66 u_long, struct ufsmount *, int, struct dquot **);
67 static int dqsync (struct vnode *, struct dquot *);
68 static void dqflush (struct vnode *);
71 static void dqref (struct dquot *);
72 static void chkdquot (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.
84 getinoquota(struct inode *ip)
87 struct vnode *vp = ITOV(ip);
90 ump = VFSTOUFS(vp->v_mount);
92 * Set up the user quota based on file uid.
93 * EINVAL means that quotas are not enabled.
95 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
97 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
101 * Set up the group quota based on file gid.
102 * EINVAL means that quotas are not enabled.
104 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
106 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
113 * Update disk usage, and take corrective action.
116 chkdq(struct inode *ip, long change, struct ucred *cred, int flags)
120 int ncurblocks, error;
123 if ((flags & CHOWN) == 0)
129 for (i = 0; i < MAXQUOTAS; i++) {
130 if ((dq = ip->i_dquot[i]) == NODQUOT)
132 while (dq->dq_flags & DQ_LOCK) {
133 dq->dq_flags |= DQ_WANT;
134 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
136 ncurblocks = dq->dq_curblocks + change;
138 dq->dq_curblocks = ncurblocks;
140 dq->dq_curblocks = 0;
141 dq->dq_flags &= ~DQ_BLKS;
142 dq->dq_flags |= DQ_MOD;
146 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
147 for (i = 0; i < MAXQUOTAS; i++) {
148 if ((dq = ip->i_dquot[i]) == NODQUOT)
150 error = chkdqchg(ip, change, cred, i);
155 for (i = 0; i < MAXQUOTAS; i++) {
156 if ((dq = ip->i_dquot[i]) == NODQUOT)
158 while (dq->dq_flags & DQ_LOCK) {
159 dq->dq_flags |= DQ_WANT;
160 (void) tsleep((caddr_t)dq, 0, "chkdq2", 0);
162 /* Reset timer when crossing soft limit */
163 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
164 dq->dq_curblocks < dq->dq_bsoftlimit)
165 dq->dq_btime = time_second +
166 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
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(struct inode *ip, long change, struct ucred *cred, int type)
180 struct dquot *dq = ip->i_dquot[type];
181 long ncurblocks = dq->dq_curblocks + change;
184 * If user would exceed their hard limit, disallow space allocation.
186 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
187 if ((dq->dq_flags & DQ_BLKS) == 0 &&
188 ip->i_uid == cred->cr_uid) {
189 uprintf("\n%s: write failed, %s disk limit reached\n",
190 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
192 dq->dq_flags |= DQ_BLKS;
197 * If user is over their soft limit for too long, disallow space
198 * allocation. Reset time limit as they cross their soft limit.
200 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
201 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
202 dq->dq_btime = time_second +
203 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
204 if (ip->i_uid == cred->cr_uid)
205 uprintf("\n%s: warning, %s %s\n",
206 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
207 quotatypes[type], "disk quota exceeded");
210 if (time_second > dq->dq_btime) {
211 if ((dq->dq_flags & DQ_BLKS) == 0 &&
212 ip->i_uid == cred->cr_uid) {
213 uprintf("\n%s: write failed, %s %s\n",
214 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
216 "disk quota exceeded for too long");
217 dq->dq_flags |= DQ_BLKS;
226 * Check the inode limit, applying corrective action.
229 chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
233 int ncurinodes, error;
236 if ((flags & CHOWN) == 0)
242 for (i = 0; i < MAXQUOTAS; i++) {
243 if ((dq = ip->i_dquot[i]) == NODQUOT)
245 while (dq->dq_flags & DQ_LOCK) {
246 dq->dq_flags |= DQ_WANT;
247 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
249 ncurinodes = dq->dq_curinodes + change;
251 dq->dq_curinodes = ncurinodes;
253 dq->dq_curinodes = 0;
254 dq->dq_flags &= ~DQ_INODS;
255 dq->dq_flags |= DQ_MOD;
259 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
260 for (i = 0; i < MAXQUOTAS; i++) {
261 if ((dq = ip->i_dquot[i]) == NODQUOT)
263 error = chkiqchg(ip, change, cred, i);
268 for (i = 0; i < MAXQUOTAS; i++) {
269 if ((dq = ip->i_dquot[i]) == NODQUOT)
271 while (dq->dq_flags & DQ_LOCK) {
272 dq->dq_flags |= DQ_WANT;
273 (void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
275 /* Reset timer when crossing soft limit */
276 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
277 dq->dq_curinodes < dq->dq_isoftlimit)
278 dq->dq_itime = time_second +
279 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
280 dq->dq_curinodes += change;
281 dq->dq_flags |= DQ_MOD;
287 * Check for a valid change to a users allocation.
288 * Issue an error message if appropriate.
291 chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
293 struct dquot *dq = ip->i_dquot[type];
294 long ncurinodes = dq->dq_curinodes + change;
297 * If user would exceed their hard limit, disallow inode allocation.
299 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
300 if ((dq->dq_flags & DQ_INODS) == 0 &&
301 ip->i_uid == cred->cr_uid) {
302 uprintf("\n%s: write failed, %s inode limit reached\n",
303 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
305 dq->dq_flags |= DQ_INODS;
310 * If user is over their soft limit for too long, disallow inode
311 * allocation. Reset time limit as they cross their soft limit.
313 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
314 if (dq->dq_curinodes < dq->dq_isoftlimit) {
315 dq->dq_itime = time_second +
316 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
317 if (ip->i_uid == cred->cr_uid)
318 uprintf("\n%s: warning, %s %s\n",
319 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
320 quotatypes[type], "inode quota exceeded");
323 if (time_second > dq->dq_itime) {
324 if ((dq->dq_flags & DQ_INODS) == 0 &&
325 ip->i_uid == cred->cr_uid) {
326 uprintf("\n%s: write failed, %s %s\n",
327 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
329 "inode quota exceeded for too long");
330 dq->dq_flags |= DQ_INODS;
340 * On filesystems with quotas enabled, it is an error for a file to change
341 * size and not to have a dquot structure associated with it.
344 chkdquot(struct inode *ip)
346 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
349 for (i = 0; i < MAXQUOTAS; i++) {
350 if (ump->um_quotas[i] == NULLVP ||
351 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
353 if (ip->i_dquot[i] == NODQUOT) {
354 vprint("chkdquot: missing dquot", ITOV(ip));
355 panic("chkdquot: missing dquot");
362 * Code to process quotactl commands.
372 * Q_QUOTAON - set up a quota file for a particular filesystem.
374 static int quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
377 quotaon(struct thread *td, struct mount *mp, int type, caddr_t fname)
379 struct ufsmount *ump = VFSTOUFS(mp);
380 struct vnode *vp, **vpp;
385 struct scaninfo scaninfo;
387 KKASSERT(td->td_proc);
388 cred = td->td_proc->p_ucred;
390 vpp = &ump->um_quotas[type];
391 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, fname, td);
392 error = vn_open(&nd, FREAD|FWRITE, 0);
395 NDFREE(&nd, NDF_ONLY_PNBUF);
397 VOP_UNLOCK(vp, 0, td);
398 if (vp->v_type != VREG) {
399 (void) vn_close(vp, FREAD|FWRITE, td);
403 quotaoff(td, 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.
412 ump->um_cred[type] = crhold(cred);
413 ump->um_btime[type] = MAX_DQ_TIME;
414 ump->um_itime[type] = MAX_IQ_TIME;
415 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
416 if (dq->dq_btime > 0)
417 ump->um_btime[type] = dq->dq_btime;
418 if (dq->dq_itime > 0)
419 ump->um_itime[type] = dq->dq_itime;
423 * Search vnodes associated with this mount point,
424 * adding references to quota file being opened.
425 * NB: only need to add dquot's for inodes being modified.
429 while (scaninfo.rescan) {
431 error = vmntvnodescan(mp, VMSC_GETVP,
432 NULL, quotaon_scan, &scaninfo);
436 ump->um_qflags[type] &= ~QTF_OPENING;
438 quotaoff(td, mp, type);
443 quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
446 /*struct scaninfo *info = data;*/
448 if (vp->v_writecount == 0)
450 error = getinoquota(VTOI(vp));
455 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
458 static int quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
461 quotaoff(struct thread *td, struct mount *mp, int type)
464 struct ufsmount *ump = VFSTOUFS(mp);
467 struct scaninfo scaninfo;
469 KKASSERT(td->td_proc);
470 cred = td->td_proc->p_ucred;
472 if ((qvp = ump->um_quotas[type]) == NULLVP)
474 ump->um_qflags[type] |= QTF_CLOSING;
477 * Search vnodes associated with this mount point,
478 * deleting any references to quota file being closed.
482 scaninfo.type = type;
483 while (scaninfo.rescan) {
485 vmntvnodescan(mp, VMSC_GETVP, NULL, quotaoff_scan, &scaninfo);
488 qvp->v_flag &= ~VSYSTEM;
489 error = vn_close(qvp, FREAD|FWRITE, td);
490 ump->um_quotas[type] = NULLVP;
491 crfree(ump->um_cred[type]);
492 ump->um_cred[type] = NOCRED;
493 ump->um_qflags[type] &= ~QTF_CLOSING;
494 for (type = 0; type < MAXQUOTAS; type++) {
495 if (ump->um_quotas[type] != NULLVP)
498 if (type == MAXQUOTAS)
499 mp->mnt_flag &= ~MNT_QUOTA;
504 quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
506 struct scaninfo *info = data;
510 if (vp->v_type == VNON) {
514 dq = ip->i_dquot[info->type];
515 ip->i_dquot[info->type] = NODQUOT;
521 * Q_GETQUOTA - return current values in a dqblk structure.
524 getquota(struct mount *mp, u_long id, int type, caddr_t addr)
529 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
532 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
538 * Q_SETQUOTA - assign an entire dqblk structure.
541 setquota(struct mount *mp, u_long id, int type, caddr_t addr)
545 struct ufsmount *ump = VFSTOUFS(mp);
549 error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
552 error = dqget(NULLVP, id, ump, type, &ndq);
556 while (dq->dq_flags & DQ_LOCK) {
557 dq->dq_flags |= DQ_WANT;
558 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
561 * Copy all but the current values.
562 * Reset time limit if previously had no soft limit or were
563 * under it, but now have a soft limit and are over it.
565 newlim.dqb_curblocks = dq->dq_curblocks;
566 newlim.dqb_curinodes = dq->dq_curinodes;
567 if (dq->dq_id != 0) {
568 newlim.dqb_btime = dq->dq_btime;
569 newlim.dqb_itime = dq->dq_itime;
571 if (newlim.dqb_bsoftlimit &&
572 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
573 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
574 newlim.dqb_btime = time_second + ump->um_btime[type];
575 if (newlim.dqb_isoftlimit &&
576 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
577 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
578 newlim.dqb_itime = time_second + ump->um_itime[type];
580 if (dq->dq_curblocks < dq->dq_bsoftlimit)
581 dq->dq_flags &= ~DQ_BLKS;
582 if (dq->dq_curinodes < dq->dq_isoftlimit)
583 dq->dq_flags &= ~DQ_INODS;
584 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
585 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
586 dq->dq_flags |= DQ_FAKE;
588 dq->dq_flags &= ~DQ_FAKE;
589 dq->dq_flags |= DQ_MOD;
595 * Q_SETUSE - set current inode and block usage.
598 setuse(struct mount *mp, u_long id, int type, caddr_t addr)
601 struct ufsmount *ump = VFSTOUFS(mp);
606 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
609 error = dqget(NULLVP, id, ump, type, &ndq);
613 while (dq->dq_flags & DQ_LOCK) {
614 dq->dq_flags |= DQ_WANT;
615 (void) tsleep((caddr_t)dq, 0, "setuse", 0);
618 * Reset time limit if have a soft limit and were
619 * previously under it, but are now over it.
621 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
622 usage.dqb_curblocks >= dq->dq_bsoftlimit)
623 dq->dq_btime = time_second + ump->um_btime[type];
624 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
625 usage.dqb_curinodes >= dq->dq_isoftlimit)
626 dq->dq_itime = time_second + ump->um_itime[type];
627 dq->dq_curblocks = usage.dqb_curblocks;
628 dq->dq_curinodes = usage.dqb_curinodes;
629 if (dq->dq_curblocks < dq->dq_bsoftlimit)
630 dq->dq_flags &= ~DQ_BLKS;
631 if (dq->dq_curinodes < dq->dq_isoftlimit)
632 dq->dq_flags &= ~DQ_INODS;
633 dq->dq_flags |= DQ_MOD;
639 * Q_SYNC - sync quota files to disk.
642 static int qsync_scan(struct mount *mp, struct vnode *vp, void *data);
645 qsync(struct mount *mp)
647 struct ufsmount *ump = VFSTOUFS(mp);
648 struct thread *td = curthread; /* XXX */
649 struct scaninfo scaninfo;
653 * Check if the mount point has any quotas.
654 * If not, simply return.
656 for (i = 0; i < MAXQUOTAS; i++)
657 if (ump->um_quotas[i] != NULLVP)
662 * Search vnodes associated with this mount point,
663 * synchronizing any modified dquot structures.
667 while (scaninfo.rescan) {
669 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
670 NULL, qsync_scan, &scaninfo);
676 qsync_scan(struct mount *mp, struct vnode *vp, void *data)
678 /*struct scaninfo *info = data;*/
683 for (i = 0; i < MAXQUOTAS; i++) {
684 dq = VTOI(vp)->i_dquot[i];
685 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
692 * Code pertaining to management of the in-core dquot data structures.
694 #define DQHASH(dqvp, id) \
695 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
696 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
697 static u_long dqhash;
702 #define DQUOTINC 5 /* minimum free dquots desired */
703 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
704 static long numdquot, desireddquot = DQUOTINC;
707 * Initialize the quota system.
713 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
714 TAILQ_INIT(&dqfreelist);
718 * Obtain a dquot structure for the specified identifier and quota file
719 * reading the information from the file if necessary.
722 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
725 struct thread *td = curthread; /* XXX */
733 dqvp = ump->um_quotas[type];
734 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
739 * Check the cache first.
741 dqh = DQHASH(dqvp, id);
742 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
743 if (dq->dq_id != id ||
744 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
747 * Cache hit with no references. Take
748 * the structure off the free list.
751 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
757 * Not in cache, allocate a new one.
759 if (TAILQ_EMPTY(&dqfreelist) && numdquot < MAXQUOTAS * desiredvnodes)
760 desireddquot += DQUOTINC;
761 if (numdquot < desireddquot) {
762 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
763 bzero((char *)dq, sizeof *dq);
766 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
771 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
772 panic("dqget: free dquot isn't");
773 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
774 if (dq->dq_ump != NULL)
775 LIST_REMOVE(dq, dq_hash);
778 * Initialize the contents of the dquot structure.
781 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
782 LIST_INSERT_HEAD(dqh, dq, dq_hash);
784 dq->dq_flags = DQ_LOCK;
788 auio.uio_iov = &aiov;
790 aiov.iov_base = (caddr_t)&dq->dq_dqb;
791 aiov.iov_len = sizeof (struct dqblk);
792 auio.uio_resid = sizeof (struct dqblk);
793 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
794 auio.uio_segflg = UIO_SYSSPACE;
795 auio.uio_rw = UIO_READ;
797 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
798 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
799 bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
801 VOP_UNLOCK(dqvp, 0, td);
802 if (dq->dq_flags & DQ_WANT)
806 * I/O error in reading quota file, release
807 * quota structure and reflect problem to caller.
810 LIST_REMOVE(dq, dq_hash);
816 * Check for no limit to enforce.
817 * Initialize time values if necessary.
819 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
820 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
821 dq->dq_flags |= DQ_FAKE;
822 if (dq->dq_id != 0) {
823 if (dq->dq_btime == 0)
824 dq->dq_btime = time_second + ump->um_btime[type];
825 if (dq->dq_itime == 0)
826 dq->dq_itime = time_second + ump->um_itime[type];
834 * Obtain a reference to a dquot.
837 dqref(struct dquot *dq)
844 * Release a reference to a dquot.
847 dqrele(struct vnode *vp, struct dquot *dq)
851 if (dq->dq_cnt > 1) {
855 if (dq->dq_flags & DQ_MOD)
856 (void) dqsync(vp, dq);
857 if (--dq->dq_cnt > 0)
859 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
863 * Update the disk quota in the quota file.
866 dqsync(struct vnode *vp, struct dquot *dq)
868 struct thread *td = curthread; /* XXX */
875 panic("dqsync: dquot");
876 if ((dq->dq_flags & DQ_MOD) == 0)
878 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
879 panic("dqsync: file");
881 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
882 while (dq->dq_flags & DQ_LOCK) {
883 dq->dq_flags |= DQ_WANT;
884 (void) tsleep((caddr_t)dq, 0, "dqsync", 0);
885 if ((dq->dq_flags & DQ_MOD) == 0) {
887 VOP_UNLOCK(dqvp, 0, td);
891 dq->dq_flags |= DQ_LOCK;
892 auio.uio_iov = &aiov;
894 aiov.iov_base = (caddr_t)&dq->dq_dqb;
895 aiov.iov_len = sizeof (struct dqblk);
896 auio.uio_resid = sizeof (struct dqblk);
897 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
898 auio.uio_segflg = UIO_SYSSPACE;
899 auio.uio_rw = UIO_WRITE;
901 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
902 if (auio.uio_resid && error == 0)
904 if (dq->dq_flags & DQ_WANT)
906 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
908 VOP_UNLOCK(dqvp, 0, td);
913 * Flush all entries from the cache for a particular vnode.
916 dqflush(struct vnode *vp)
918 struct dquot *dq, *nextdq;
922 * Move all dquot's that used to refer to this quota
923 * file off their hash chains (they will eventually
924 * fall off the head of the free list and be re-used).
926 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
927 for (dq = dqh->lh_first; dq; dq = nextdq) {
928 nextdq = dq->dq_hash.le_next;
929 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
932 panic("dqflush: stray dquot");
933 LIST_REMOVE(dq, dq_hash);
934 dq->dq_ump = (struct ufsmount *)0;