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 $
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/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufsmount.h>
55 static MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
58 * Quota name to error message mapping.
60 static char *quotatypes[] = INITQFNAMES;
62 static int chkdqchg __P((struct inode *, long, struct ucred *, int));
63 static int chkiqchg __P((struct inode *, long, struct ucred *, int));
64 static int dqget __P((struct vnode *,
65 u_long, struct ufsmount *, int, struct dquot **));
66 static int dqsync __P((struct vnode *, struct dquot *));
67 static void dqflush __P((struct vnode *));
70 static void dqref __P((struct dquot *));
71 static void chkdquot __P((struct inode *));
75 * Set up the quotas for an inode.
77 * This routine completely defines the semantics of quotas.
78 * If other criterion want to be used to establish quotas, the
79 * MAXQUOTAS value in quotas.h should be increased, and the
80 * additional dquots set up here.
84 register 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(ip, change, cred, flags)
117 register struct inode *ip;
122 register struct dquot *dq;
124 int ncurblocks, error;
127 if ((flags & CHOWN) == 0)
133 for (i = 0; i < MAXQUOTAS; i++) {
134 if ((dq = ip->i_dquot[i]) == NODQUOT)
136 while (dq->dq_flags & DQ_LOCK) {
137 dq->dq_flags |= DQ_WANT;
138 (void) tsleep((caddr_t)dq, PINOD+1, "chkdq1", 0);
140 ncurblocks = dq->dq_curblocks + change;
142 dq->dq_curblocks = ncurblocks;
144 dq->dq_curblocks = 0;
145 dq->dq_flags &= ~DQ_BLKS;
146 dq->dq_flags |= DQ_MOD;
150 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
151 for (i = 0; i < MAXQUOTAS; i++) {
152 if ((dq = ip->i_dquot[i]) == NODQUOT)
154 error = chkdqchg(ip, change, cred, i);
159 for (i = 0; i < MAXQUOTAS; i++) {
160 if ((dq = ip->i_dquot[i]) == NODQUOT)
162 while (dq->dq_flags & DQ_LOCK) {
163 dq->dq_flags |= DQ_WANT;
164 (void) tsleep((caddr_t)dq, PINOD+1, "chkdq2", 0);
166 /* Reset timer when crossing soft limit */
167 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
168 dq->dq_curblocks < dq->dq_bsoftlimit)
169 dq->dq_btime = time_second +
170 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
171 dq->dq_curblocks += change;
172 dq->dq_flags |= DQ_MOD;
178 * Check for a valid change to a users allocation.
179 * Issue an error message if appropriate.
182 chkdqchg(ip, change, cred, type)
188 register struct dquot *dq = ip->i_dquot[type];
189 long ncurblocks = dq->dq_curblocks + change;
192 * If user would exceed their hard limit, disallow space allocation.
194 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
195 if ((dq->dq_flags & DQ_BLKS) == 0 &&
196 ip->i_uid == cred->cr_uid) {
197 uprintf("\n%s: write failed, %s disk limit reached\n",
198 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
200 dq->dq_flags |= DQ_BLKS;
205 * If user is over their soft limit for too long, disallow space
206 * allocation. Reset time limit as they cross their soft limit.
208 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
209 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
210 dq->dq_btime = time_second +
211 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
212 if (ip->i_uid == cred->cr_uid)
213 uprintf("\n%s: warning, %s %s\n",
214 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
215 quotatypes[type], "disk quota exceeded");
218 if (time_second > dq->dq_btime) {
219 if ((dq->dq_flags & DQ_BLKS) == 0 &&
220 ip->i_uid == cred->cr_uid) {
221 uprintf("\n%s: write failed, %s %s\n",
222 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
224 "disk quota exceeded for too long");
225 dq->dq_flags |= DQ_BLKS;
234 * Check the inode limit, applying corrective action.
237 chkiq(ip, change, cred, flags)
238 register struct inode *ip;
243 register struct dquot *dq;
245 int ncurinodes, error;
248 if ((flags & CHOWN) == 0)
254 for (i = 0; i < MAXQUOTAS; i++) {
255 if ((dq = ip->i_dquot[i]) == NODQUOT)
257 while (dq->dq_flags & DQ_LOCK) {
258 dq->dq_flags |= DQ_WANT;
259 (void) tsleep((caddr_t)dq, PINOD+1, "chkiq1", 0);
261 ncurinodes = dq->dq_curinodes + change;
263 dq->dq_curinodes = ncurinodes;
265 dq->dq_curinodes = 0;
266 dq->dq_flags &= ~DQ_INODS;
267 dq->dq_flags |= DQ_MOD;
271 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
272 for (i = 0; i < MAXQUOTAS; i++) {
273 if ((dq = ip->i_dquot[i]) == NODQUOT)
275 error = chkiqchg(ip, change, cred, i);
280 for (i = 0; i < MAXQUOTAS; i++) {
281 if ((dq = ip->i_dquot[i]) == NODQUOT)
283 while (dq->dq_flags & DQ_LOCK) {
284 dq->dq_flags |= DQ_WANT;
285 (void) tsleep((caddr_t)dq, PINOD+1, "chkiq2", 0);
287 /* Reset timer when crossing soft limit */
288 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
289 dq->dq_curinodes < dq->dq_isoftlimit)
290 dq->dq_itime = time_second +
291 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
292 dq->dq_curinodes += change;
293 dq->dq_flags |= DQ_MOD;
299 * Check for a valid change to a users allocation.
300 * Issue an error message if appropriate.
303 chkiqchg(ip, change, cred, type)
309 register struct dquot *dq = ip->i_dquot[type];
310 long ncurinodes = dq->dq_curinodes + change;
313 * If user would exceed their hard limit, disallow inode allocation.
315 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
316 if ((dq->dq_flags & DQ_INODS) == 0 &&
317 ip->i_uid == cred->cr_uid) {
318 uprintf("\n%s: write failed, %s inode limit reached\n",
319 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
321 dq->dq_flags |= DQ_INODS;
326 * If user is over their soft limit for too long, disallow inode
327 * allocation. Reset time limit as they cross their soft limit.
329 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
330 if (dq->dq_curinodes < dq->dq_isoftlimit) {
331 dq->dq_itime = time_second +
332 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
333 if (ip->i_uid == cred->cr_uid)
334 uprintf("\n%s: warning, %s %s\n",
335 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
336 quotatypes[type], "inode quota exceeded");
339 if (time_second > dq->dq_itime) {
340 if ((dq->dq_flags & DQ_INODS) == 0 &&
341 ip->i_uid == cred->cr_uid) {
342 uprintf("\n%s: write failed, %s %s\n",
343 ITOV(ip)->v_mount->mnt_stat.f_mntonname,
345 "inode quota exceeded for too long");
346 dq->dq_flags |= DQ_INODS;
356 * On filesystems with quotas enabled, it is an error for a file to change
357 * size and not to have a dquot structure associated with it.
361 register struct inode *ip;
363 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
366 for (i = 0; i < MAXQUOTAS; i++) {
367 if (ump->um_quotas[i] == NULLVP ||
368 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
370 if (ip->i_dquot[i] == NODQUOT) {
371 vprint("chkdquot: missing dquot", ITOV(ip));
372 panic("chkdquot: missing dquot");
379 * Code to process quotactl commands.
383 * Q_QUOTAON - set up a quota file for a particular file system.
386 quotaon(p, mp, type, fname)
392 struct ufsmount *ump = VFSTOUFS(mp);
393 struct vnode *vp, **vpp;
394 struct vnode *nextvp;
399 vpp = &ump->um_quotas[type];
400 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, p);
401 error = vn_open(&nd, FREAD|FWRITE, 0);
404 NDFREE(&nd, NDF_ONLY_PNBUF);
406 VOP_UNLOCK(vp, 0, p);
407 if (vp->v_type != VREG) {
408 (void) vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
412 quotaoff(p, mp, type);
413 ump->um_qflags[type] |= QTF_OPENING;
414 mp->mnt_flag |= MNT_QUOTA;
415 vp->v_flag |= VSYSTEM;
418 * Save the credential of the process that turned on quotas.
419 * Set up the time limits for this quota.
422 ump->um_cred[type] = p->p_ucred;
423 ump->um_btime[type] = MAX_DQ_TIME;
424 ump->um_itime[type] = MAX_IQ_TIME;
425 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
426 if (dq->dq_btime > 0)
427 ump->um_btime[type] = dq->dq_btime;
428 if (dq->dq_itime > 0)
429 ump->um_itime[type] = dq->dq_itime;
433 * Search vnodes associated with this mount point,
434 * adding references to quota file being opened.
435 * NB: only need to add dquot's for inodes being modified.
438 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
439 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
440 if (vp->v_type == VNON || vp->v_writecount == 0)
442 if (vget(vp, LK_EXCLUSIVE, p))
444 error = getinoquota(VTOI(vp));
450 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp || vp->v_mount != mp)
453 ump->um_qflags[type] &= ~QTF_OPENING;
455 quotaoff(p, mp, type);
460 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
463 quotaoff(p, mp, type)
469 struct vnode *qvp, *nextvp;
470 struct ufsmount *ump = VFSTOUFS(mp);
475 if ((qvp = ump->um_quotas[type]) == NULLVP)
477 ump->um_qflags[type] |= QTF_CLOSING;
479 * Search vnodes associated with this mount point,
480 * deleting any references to quota file being closed.
483 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
484 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
485 if (vp->v_type == VNON)
487 if (vget(vp, LK_EXCLUSIVE, p))
490 dq = ip->i_dquot[type];
491 ip->i_dquot[type] = NODQUOT;
494 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp || vp->v_mount != mp)
498 qvp->v_flag &= ~VSYSTEM;
499 error = vn_close(qvp, FREAD|FWRITE, p->p_ucred, p);
500 ump->um_quotas[type] = NULLVP;
501 crfree(ump->um_cred[type]);
502 ump->um_cred[type] = NOCRED;
503 ump->um_qflags[type] &= ~QTF_CLOSING;
504 for (type = 0; type < MAXQUOTAS; type++)
505 if (ump->um_quotas[type] != NULLVP)
507 if (type == MAXQUOTAS)
508 mp->mnt_flag &= ~MNT_QUOTA;
513 * Q_GETQUOTA - return current values in a dqblk structure.
516 getquota(mp, id, type, addr)
525 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
528 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
534 * Q_SETQUOTA - assign an entire dqblk structure.
537 setquota(mp, id, type, addr)
543 register struct dquot *dq;
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, PINOD+1, "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(mp, id, type, addr)
604 register struct dquot *dq;
605 struct ufsmount *ump = VFSTOUFS(mp);
610 error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
613 error = dqget(NULLVP, id, ump, type, &ndq);
617 while (dq->dq_flags & DQ_LOCK) {
618 dq->dq_flags |= DQ_WANT;
619 (void) tsleep((caddr_t)dq, PINOD+1, "setuse", 0);
622 * Reset time limit if have a soft limit and were
623 * previously under it, but are now over it.
625 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
626 usage.dqb_curblocks >= dq->dq_bsoftlimit)
627 dq->dq_btime = time_second + ump->um_btime[type];
628 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
629 usage.dqb_curinodes >= dq->dq_isoftlimit)
630 dq->dq_itime = time_second + ump->um_itime[type];
631 dq->dq_curblocks = usage.dqb_curblocks;
632 dq->dq_curinodes = usage.dqb_curinodes;
633 if (dq->dq_curblocks < dq->dq_bsoftlimit)
634 dq->dq_flags &= ~DQ_BLKS;
635 if (dq->dq_curinodes < dq->dq_isoftlimit)
636 dq->dq_flags &= ~DQ_INODS;
637 dq->dq_flags |= DQ_MOD;
643 * Q_SYNC - sync quota files to disk.
649 struct ufsmount *ump = VFSTOUFS(mp);
650 struct proc *p = curproc; /* XXX */
651 struct vnode *vp, *nextvp;
656 * Check if the mount point has any quotas.
657 * If not, simply return.
659 for (i = 0; i < MAXQUOTAS; i++)
660 if (ump->um_quotas[i] != NULLVP)
665 * Search vnodes associated with this mount point,
666 * synchronizing any modified dquot structures.
668 simple_lock(&mntvnode_slock);
670 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nextvp) {
671 if (vp->v_mount != mp)
673 nextvp = TAILQ_NEXT(vp, v_nmntvnodes);
674 if (vp->v_type == VNON)
676 simple_lock(&vp->v_interlock);
677 simple_unlock(&mntvnode_slock);
678 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
680 simple_lock(&mntvnode_slock);
685 for (i = 0; i < MAXQUOTAS; i++) {
686 dq = VTOI(vp)->i_dquot[i];
687 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
691 simple_lock(&mntvnode_slock);
692 if (TAILQ_NEXT(vp, v_nmntvnodes) != nextvp)
695 simple_unlock(&mntvnode_slock);
700 * Code pertaining to management of the in-core dquot data structures.
702 #define DQHASH(dqvp, id) \
703 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
704 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
705 static u_long dqhash;
710 #define DQUOTINC 5 /* minimum free dquots desired */
711 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
712 static long numdquot, desireddquot = DQUOTINC;
715 * Initialize the quota system.
721 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
722 TAILQ_INIT(&dqfreelist);
726 * Obtain a dquot structure for the specified identifier and quota file
727 * reading the information from the file if necessary.
730 dqget(vp, id, ump, type, dqp)
733 register struct ufsmount *ump;
737 struct proc *p = curproc; /* XXX */
745 dqvp = ump->um_quotas[type];
746 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
751 * Check the cache first.
753 dqh = DQHASH(dqvp, id);
754 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
755 if (dq->dq_id != id ||
756 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
759 * Cache hit with no references. Take
760 * the structure off the free list.
763 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
769 * Not in cache, allocate a new one.
771 if (dqfreelist.tqh_first == NODQUOT &&
772 numdquot < MAXQUOTAS * desiredvnodes)
773 desireddquot += DQUOTINC;
774 if (numdquot < desireddquot) {
775 dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
776 bzero((char *)dq, sizeof *dq);
779 if ((dq = dqfreelist.tqh_first) == NULL) {
784 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
785 panic("dqget: free dquot isn't");
786 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
787 if (dq->dq_ump != NULL)
788 LIST_REMOVE(dq, dq_hash);
791 * Initialize the contents of the dquot structure.
794 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, p);
795 LIST_INSERT_HEAD(dqh, dq, dq_hash);
797 dq->dq_flags = DQ_LOCK;
801 auio.uio_iov = &aiov;
803 aiov.iov_base = (caddr_t)&dq->dq_dqb;
804 aiov.iov_len = sizeof (struct dqblk);
805 auio.uio_resid = sizeof (struct dqblk);
806 auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
807 auio.uio_segflg = UIO_SYSSPACE;
808 auio.uio_rw = UIO_READ;
809 auio.uio_procp = (struct proc *)0;
810 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
811 if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
812 bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
814 VOP_UNLOCK(dqvp, 0, p);
815 if (dq->dq_flags & DQ_WANT)
819 * I/O error in reading quota file, release
820 * quota structure and reflect problem to caller.
823 LIST_REMOVE(dq, dq_hash);
829 * Check for no limit to enforce.
830 * Initialize time values if necessary.
832 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
833 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
834 dq->dq_flags |= DQ_FAKE;
835 if (dq->dq_id != 0) {
836 if (dq->dq_btime == 0)
837 dq->dq_btime = time_second + ump->um_btime[type];
838 if (dq->dq_itime == 0)
839 dq->dq_itime = time_second + ump->um_itime[type];
847 * Obtain a reference to a dquot.
859 * Release a reference to a dquot.
864 register struct dquot *dq;
869 if (dq->dq_cnt > 1) {
873 if (dq->dq_flags & DQ_MOD)
874 (void) dqsync(vp, dq);
875 if (--dq->dq_cnt > 0)
877 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
881 * Update the disk quota in the quota file.
888 struct proc *p = curproc; /* XXX */
895 panic("dqsync: dquot");
896 if ((dq->dq_flags & DQ_MOD) == 0)
898 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
899 panic("dqsync: file");
901 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY, p);
902 while (dq->dq_flags & DQ_LOCK) {
903 dq->dq_flags |= DQ_WANT;
904 (void) tsleep((caddr_t)dq, PINOD+2, "dqsync", 0);
905 if ((dq->dq_flags & DQ_MOD) == 0) {
907 VOP_UNLOCK(dqvp, 0, p);
911 dq->dq_flags |= DQ_LOCK;
912 auio.uio_iov = &aiov;
914 aiov.iov_base = (caddr_t)&dq->dq_dqb;
915 aiov.iov_len = sizeof (struct dqblk);
916 auio.uio_resid = sizeof (struct dqblk);
917 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
918 auio.uio_segflg = UIO_SYSSPACE;
919 auio.uio_rw = UIO_WRITE;
920 auio.uio_procp = (struct proc *)0;
921 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
922 if (auio.uio_resid && error == 0)
924 if (dq->dq_flags & DQ_WANT)
926 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
928 VOP_UNLOCK(dqvp, 0, p);
933 * Flush all entries from the cache for a particular vnode.
937 register struct vnode *vp;
939 register struct dquot *dq, *nextdq;
943 * Move all dquot's that used to refer to this quota
944 * file off their hash chains (they will eventually
945 * fall off the head of the free list and be re-used).
947 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
948 for (dq = dqh->lh_first; dq; dq = nextdq) {
949 nextdq = dq->dq_hash.le_next;
950 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
953 panic("dqflush: stray dquot");
954 LIST_REMOVE(dq, dq_hash);
955 dq->dq_ump = (struct ufsmount *)0;