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.3 2003/06/25 03:56: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>
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 /* Reset timer when crossing soft limit */
168 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
169 dq->dq_curblocks < dq->dq_bsoftlimit)
170 dq->dq_btime = time_second +
171 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
172 dq->dq_curblocks += change;
173 dq->dq_flags |= DQ_MOD;
179 * Check for a valid change to a users allocation.
180 * Issue an error message if appropriate.
183 chkdqchg(ip, change, cred, type)
189 register struct dquot *dq = ip->i_dquot[type];
190 long ncurblocks = dq->dq_curblocks + change;
193 * If user would exceed their hard limit, disallow space allocation.
195 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
196 if ((dq->dq_flags & DQ_BLKS) == 0 &&
197 ip->i_uid == cred->cr_uid) {
198 uprintf("\n%s: write failed, %s disk limit reached\n",
199 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
201 dq->dq_flags |= DQ_BLKS;
206 * If user is over their soft limit for too long, disallow space
207 * allocation. Reset time limit as they cross their soft limit.
209 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
210 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
211 dq->dq_btime = time_second +
212 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
213 if (ip->i_uid == cred->cr_uid)
214 uprintf("\n%s: warning, %s %s\n",
215 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
216 quotatypes[type], "disk quota exceeded");
219 if (time_second > dq->dq_btime) {
220 if ((dq->dq_flags & DQ_BLKS) == 0 &&
221 ip->i_uid == cred->cr_uid) {
222 uprintf("\n%s: write failed, %s %s\n",
223 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
225 "disk quota exceeded for too long");
226 dq->dq_flags |= DQ_BLKS;
235 * Check the inode limit, applying corrective action.
238 chkiq(ip, change, cred, flags)
239 register struct inode *ip;
244 register struct dquot *dq;
246 int ncurinodes, error;
249 if ((flags & CHOWN) == 0)
255 for (i = 0; i < MAXQUOTAS; i++) {
256 if ((dq = ip->i_dquot[i]) == NODQUOT)
258 while (dq->dq_flags & DQ_LOCK) {
259 dq->dq_flags |= DQ_WANT;
260 (void) tsleep((caddr_t)dq, PINOD+1, "chkiq1", 0);
262 ncurinodes = dq->dq_curinodes + change;
264 dq->dq_curinodes = ncurinodes;
266 dq->dq_curinodes = 0;
267 dq->dq_flags &= ~DQ_INODS;
268 dq->dq_flags |= DQ_MOD;
272 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
273 for (i = 0; i < MAXQUOTAS; i++) {
274 if ((dq = ip->i_dquot[i]) == NODQUOT)
276 error = chkiqchg(ip, change, cred, i);
281 for (i = 0; i < MAXQUOTAS; i++) {
282 if ((dq = ip->i_dquot[i]) == NODQUOT)
284 while (dq->dq_flags & DQ_LOCK) {
285 dq->dq_flags |= DQ_WANT;
286 (void) tsleep((caddr_t)dq, PINOD+1, "chkiq2", 0);
288 /* Reset timer when crossing soft limit */
289 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
290 dq->dq_curinodes < dq->dq_isoftlimit)
291 dq->dq_itime = time_second +
292 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
293 dq->dq_curinodes += change;
294 dq->dq_flags |= DQ_MOD;
300 * Check for a valid change to a users allocation.
301 * Issue an error message if appropriate.
304 chkiqchg(ip, change, cred, type)
310 register struct dquot *dq = ip->i_dquot[type];
311 long ncurinodes = dq->dq_curinodes + change;
314 * If user would exceed their hard limit, disallow inode allocation.
316 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
317 if ((dq->dq_flags & DQ_INODS) == 0 &&
318 ip->i_uid == cred->cr_uid) {
319 uprintf("\n%s: write failed, %s inode limit reached\n",
320 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
322 dq->dq_flags |= DQ_INODS;
327 * If user is over their soft limit for too long, disallow inode
328 * allocation. Reset time limit as they cross their soft limit.
330 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
331 if (dq->dq_curinodes < dq->dq_isoftlimit) {
332 dq->dq_itime = time_second +
333 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
334 if (ip->i_uid == cred->cr_uid)
335 uprintf("\n%s: warning, %s %s\n",
336 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
337 quotatypes[type], "inode quota exceeded");
340 if (time_second > dq->dq_itime) {
341 if ((dq->dq_flags & DQ_INODS) == 0 &&
342 ip->i_uid == cred->cr_uid) {
343 uprintf("\n%s: write failed, %s %s\n",
344 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
346 "inode quota exceeded for too long");
347 dq->dq_flags |= DQ_INODS;
357 * On filesystems with quotas enabled, it is an error for a file to change
358 * size and not to have a dquot structure associated with it.
362 register struct inode *ip;
364 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
367 for (i = 0; i < MAXQUOTAS; i++) {
368 if (ump->um_quotas[i] == NULLVP ||
369 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
371 if (ip->i_dquot[i] == NODQUOT) {
372 vprint("chkdquot: missing dquot", ITOV(ip));
373 panic("chkdquot: missing dquot");
380 * Code to process quotactl commands.
384 * Q_QUOTAON - set up a quota file for a particular file system.
387 quotaon(td, mp, type, fname)
393 struct ufsmount *ump = VFSTOUFS(mp);
394 struct vnode *vp, **vpp;
395 struct vnode *nextvp;
401 KKASSERT(td->td_proc);
402 cred = td->td_proc->p_ucred;
404 vpp = &ump->um_quotas[type];
405 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
406 error = vn_open(&nd, FREAD|FWRITE, 0);
409 NDFREE(&nd, NDF_ONLY_PNBUF);
411 VOP_UNLOCK(vp, 0, td);
412 if (vp->v_type != VREG) {
413 (void) vn_close(vp, FREAD|FWRITE, cred, td);
417 quotaoff(td, mp, type);
418 ump->um_qflags[type] |= QTF_OPENING;
419 mp->mnt_flag |= MNT_QUOTA;
420 vp->v_flag |= VSYSTEM;
423 * Save the credential of the process that turned on quotas.
424 * Set up the time limits for this quota.
427 ump->um_cred[type] = cred;
428 ump->um_btime[type] = MAX_DQ_TIME;
429 ump->um_itime[type] = MAX_IQ_TIME;
430 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
431 if (dq->dq_btime > 0)
432 ump->um_btime[type] = dq->dq_btime;
433 if (dq->dq_itime > 0)
434 ump->um_itime[type] = dq->dq_itime;
438 * Search vnodes associated with this mount point,
439 * adding references to quota file being opened.
440 * NB: only need to add dquot's for inodes being modified.
443 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
444 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
445 if (vp->v_type == VNON || vp->v_writecount == 0)
447 if (vget(vp, LK_EXCLUSIVE, td))
449 error = getinoquota(VTOI(vp));
455 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp || vp->v_mount != mp)
458 ump->um_qflags[type] &= ~QTF_OPENING;
460 quotaoff(td, mp, type);
465 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
468 quotaoff(struct thread *td, struct mount *mp, int type)
471 struct vnode *qvp, *nextvp;
472 struct ufsmount *ump = VFSTOUFS(mp);
478 KKASSERT(td->td_proc);
479 cred = td->td_proc->p_ucred;
481 if ((qvp = ump->um_quotas[type]) == NULLVP)
483 ump->um_qflags[type] |= QTF_CLOSING;
485 * Search vnodes associated with this mount point,
486 * deleting any references to quota file being closed.
489 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
490 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
491 if (vp->v_type == VNON)
493 if (vget(vp, LK_EXCLUSIVE, td))
496 dq = ip->i_dquot[type];
497 ip->i_dquot[type] = NODQUOT;
500 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp || vp->v_mount != mp)
504 qvp->v_flag &= ~VSYSTEM;
505 error = vn_close(qvp, FREAD|FWRITE, cred, td);
506 ump->um_quotas[type] = NULLVP;
507 crfree(ump->um_cred[type]);
508 ump->um_cred[type] = NOCRED;
509 ump->um_qflags[type] &= ~QTF_CLOSING;
510 for (type = 0; type < MAXQUOTAS; type++)
511 if (ump->um_quotas[type] != NULLVP)
513 if (type == MAXQUOTAS)
514 mp->mnt_flag &= ~MNT_QUOTA;
519 * Q_GETQUOTA - return current values in a dqblk structure.
522 getquota(mp, id, type, addr)
531 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
534 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
540 * Q_SETQUOTA - assign an entire dqblk structure.
543 setquota(mp, id, type, addr)
549 register struct dquot *dq;
551 struct ufsmount *ump = VFSTOUFS(mp);
555 error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
558 error = dqget(NULLVP, id, ump, type, &ndq);
562 while (dq->dq_flags & DQ_LOCK) {
563 dq->dq_flags |= DQ_WANT;
564 (void) tsleep((caddr_t)dq, PINOD+1, "setqta", 0);
567 * Copy all but the current values.
568 * Reset time limit if previously had no soft limit or were
569 * under it, but now have a soft limit and are over it.
571 newlim.dqb_curblocks = dq->dq_curblocks;
572 newlim.dqb_curinodes = dq->dq_curinodes;
573 if (dq->dq_id != 0) {
574 newlim.dqb_btime = dq->dq_btime;
575 newlim.dqb_itime = dq->dq_itime;
577 if (newlim.dqb_bsoftlimit &&
578 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
579 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
580 newlim.dqb_btime = time_second + ump->um_btime[type];
581 if (newlim.dqb_isoftlimit &&
582 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
583 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
584 newlim.dqb_itime = time_second + ump->um_itime[type];
586 if (dq->dq_curblocks < dq->dq_bsoftlimit)
587 dq->dq_flags &= ~DQ_BLKS;
588 if (dq->dq_curinodes < dq->dq_isoftlimit)
589 dq->dq_flags &= ~DQ_INODS;
590 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
591 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
592 dq->dq_flags |= DQ_FAKE;
594 dq->dq_flags &= ~DQ_FAKE;
595 dq->dq_flags |= DQ_MOD;
601 * Q_SETUSE - set current inode and block usage.
604 setuse(mp, id, type, addr)
610 register struct dquot *dq;
611 struct ufsmount *ump = VFSTOUFS(mp);
616 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
619 error = dqget(NULLVP, id, ump, type, &ndq);
623 while (dq->dq_flags & DQ_LOCK) {
624 dq->dq_flags |= DQ_WANT;
625 (void) tsleep((caddr_t)dq, PINOD+1, "setuse", 0);
628 * Reset time limit if have a soft limit and were
629 * previously under it, but are now over it.
631 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
632 usage.dqb_curblocks >= dq->dq_bsoftlimit)
633 dq->dq_btime = time_second + ump->um_btime[type];
634 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
635 usage.dqb_curinodes >= dq->dq_isoftlimit)
636 dq->dq_itime = time_second + ump->um_itime[type];
637 dq->dq_curblocks = usage.dqb_curblocks;
638 dq->dq_curinodes = usage.dqb_curinodes;
639 if (dq->dq_curblocks < dq->dq_bsoftlimit)
640 dq->dq_flags &= ~DQ_BLKS;
641 if (dq->dq_curinodes < dq->dq_isoftlimit)
642 dq->dq_flags &= ~DQ_INODS;
643 dq->dq_flags |= DQ_MOD;
649 * Q_SYNC - sync quota files to disk.
652 qsync(struct mount *mp)
654 struct ufsmount *ump = VFSTOUFS(mp);
655 struct thread *td = curthread; /* XXX */
656 struct vnode *vp, *nextvp;
661 * Check if the mount point has any quotas.
662 * If not, simply return.
664 for (i = 0; i < MAXQUOTAS; i++)
665 if (ump->um_quotas[i] != NULLVP)
670 * Search vnodes associated with this mount point,
671 * synchronizing any modified dquot structures.
673 simple_lock(&mntvnode_slock);
675 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
676 if (vp->v_mount != mp)
678 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
679 if (vp->v_type == VNON)
681 simple_lock(&vp->v_interlock);
682 simple_unlock(&mntvnode_slock);
683 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
685 simple_lock(&mntvnode_slock);
690 for (i = 0; i < MAXQUOTAS; i++) {
691 dq = VTOI(vp)->i_dquot[i];
692 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
696 simple_lock(&mntvnode_slock);
697 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp)
700 simple_unlock(&mntvnode_slock);
705 * Code pertaining to management of the in-core dquot data structures.
707 #define DQHASH(dqvp, id) \
708 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
709 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
710 static u_long dqhash;
715 #define DQUOTINC 5 /* minimum free dquots desired */
716 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
717 static long numdquot, desireddquot = DQUOTINC;
720 * Initialize the quota system.
726 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
727 TAILQ_INIT(&dqfreelist);
731 * Obtain a dquot structure for the specified identifier and quota file
732 * reading the information from the file if necessary.
735 dqget(vp, id, ump, type, dqp)
738 register struct ufsmount *ump;
742 struct thread *td = curthread; /* XXX */
750 dqvp = ump->um_quotas[type];
751 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
756 * Check the cache first.
758 dqh = DQHASH(dqvp, id);
759 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
760 if (dq->dq_id != id ||
761 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
764 * Cache hit with no references. Take
765 * the structure off the free list.
768 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
774 * Not in cache, allocate a new one.
776 if (dqfreelist.tqh_first == NODQUOT &&
777 numdquot < MAXQUOTAS * desiredvnodes)
778 desireddquot += DQUOTINC;
779 if (numdquot < desireddquot) {
780 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
781 bzero((char *)dq, sizeof *dq);
784 if ((dq = dqfreelist.tqh_first) == NULL) {
789 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
790 panic("dqget: free dquot isn't");
791 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
792 if (dq->dq_ump != NULL)
793 LIST_REMOVE(dq, dq_hash);
796 * Initialize the contents of the dquot structure.
799 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
800 LIST_INSERT_HEAD(dqh, dq, dq_hash);
802 dq->dq_flags = DQ_LOCK;
806 auio.uio_iov = &aiov;
808 aiov.iov_base = (caddr_t)&dq->dq_dqb;
809 aiov.iov_len = sizeof (struct dqblk);
810 auio.uio_resid = sizeof (struct dqblk);
811 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
812 auio.uio_segflg = UIO_SYSSPACE;
813 auio.uio_rw = UIO_READ;
815 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
816 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
817 bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
819 VOP_UNLOCK(dqvp, 0, td);
820 if (dq->dq_flags & DQ_WANT)
824 * I/O error in reading quota file, release
825 * quota structure and reflect problem to caller.
828 LIST_REMOVE(dq, dq_hash);
834 * Check for no limit to enforce.
835 * Initialize time values if necessary.
837 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
838 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
839 dq->dq_flags |= DQ_FAKE;
840 if (dq->dq_id != 0) {
841 if (dq->dq_btime == 0)
842 dq->dq_btime = time_second + ump->um_btime[type];
843 if (dq->dq_itime == 0)
844 dq->dq_itime = time_second + ump->um_itime[type];
852 * Obtain a reference to a dquot.
864 * Release a reference to a dquot.
869 register struct dquot *dq;
874 if (dq->dq_cnt > 1) {
878 if (dq->dq_flags & DQ_MOD)
879 (void) dqsync(vp, dq);
880 if (--dq->dq_cnt > 0)
882 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
886 * Update the disk quota in the quota file.
889 dqsync(struct vnode *vp, struct dquot *dq)
891 struct thread *td = curthread; /* XXX */
898 panic("dqsync: dquot");
899 if ((dq->dq_flags & DQ_MOD) == 0)
901 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
902 panic("dqsync: file");
904 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, td);
905 while (dq->dq_flags & DQ_LOCK) {
906 dq->dq_flags |= DQ_WANT;
907 (void) tsleep((caddr_t)dq, PINOD+2, "dqsync", 0);
908 if ((dq->dq_flags & DQ_MOD) == 0) {
910 VOP_UNLOCK(dqvp, 0, td);
914 dq->dq_flags |= DQ_LOCK;
915 auio.uio_iov = &aiov;
917 aiov.iov_base = (caddr_t)&dq->dq_dqb;
918 aiov.iov_len = sizeof (struct dqblk);
919 auio.uio_resid = sizeof (struct dqblk);
920 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
921 auio.uio_segflg = UIO_SYSSPACE;
922 auio.uio_rw = UIO_WRITE;
924 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
925 if (auio.uio_resid && error == 0)
927 if (dq->dq_flags & DQ_WANT)
929 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
931 VOP_UNLOCK(dqvp, 0, td);
936 * Flush all entries from the cache for a particular vnode.
940 register struct vnode *vp;
942 register struct dquot *dq, *nextdq;
946 * Move all dquot's that used to refer to this quota
947 * file off their hash chains (they will eventually
948 * fall off the head of the free list and be re-used).
950 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
951 for (dq = dqh->lh_first; dq; dq = nextdq) {
952 nextdq = dq->dq_hash.le_next;
953 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
956 panic("dqflush: stray dquot");
957 LIST_REMOVE(dq, dq_hash);
958 dq->dq_ump = (struct ufsmount *)0;