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/malloc.h>
44 #include <sys/fcntl.h>
46 #include <sys/nlookup.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <vm/vm_zone.h>
54 #include "ext2mount.h"
55 #include "ext2_extern.h"
57 static MALLOC_DEFINE(M_EXT2DQUOT, "EXT2 quota", "EXT2 quota entries");
60 * Quota name to error message mapping.
62 static char *quotatypes[] = INITQFNAMES;
64 static int ext2_chkdqchg (struct inode *, long, struct ucred *, int);
65 static int ext2_chkiqchg (struct inode *, long, struct ucred *, int);
66 static int ext2_dqget (struct vnode *,
67 u_long, struct ext2mount *, int, struct ext2_dquot **);
68 static int ext2_dqsync (struct vnode *, struct ext2_dquot *);
69 static void ext2_dqflush (struct vnode *);
72 static void ext2_dqref (struct ext2_dquot *);
73 static void ext2_chkdquot (struct inode *);
77 * Set up the quotas for an inode.
79 * This routine completely defines the semantics of quotas.
80 * If other criterion want to be used to establish quotas, the
81 * MAXQUOTAS value in quotas.h should be increased, and the
82 * additional dquots set up here.
85 ext2_getinoquota(struct inode *ip)
87 struct ext2mount *ump;
88 struct vnode *vp = ITOV(ip);
91 ump = VFSTOEXT2(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 &&
97 (error = ext2_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 &&
105 (error = ext2_dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
112 * Update disk usage, and take corrective action.
115 ext2_chkdq(struct inode *ip, long change, struct ucred *cred, int flags)
117 struct ext2_dquot *dq;
119 int ncurblocks, error;
122 if ((flags & CHOWN) == 0)
128 for (i = 0; i < MAXQUOTAS; i++) {
129 if ((dq = ip->i_dquot[i]) == NODQUOT)
131 while (dq->dq_flags & DQ_LOCK) {
132 dq->dq_flags |= DQ_WANT;
133 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
135 ncurblocks = dq->dq_curblocks + change;
137 dq->dq_curblocks = ncurblocks;
139 dq->dq_curblocks = 0;
140 dq->dq_flags &= ~DQ_BLKS;
141 dq->dq_flags |= DQ_MOD;
145 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
146 for (i = 0; i < MAXQUOTAS; i++) {
147 if ((dq = ip->i_dquot[i]) == NODQUOT)
149 error = ext2_chkdqchg(ip, change, cred, i);
154 for (i = 0; i < MAXQUOTAS; i++) {
155 if ((dq = ip->i_dquot[i]) == NODQUOT)
157 while (dq->dq_flags & DQ_LOCK) {
158 dq->dq_flags |= DQ_WANT;
159 (void) tsleep((caddr_t)dq, 0, "chkdq2", 0);
161 /* Reset timer when crossing soft limit */
162 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
163 dq->dq_curblocks < dq->dq_bsoftlimit)
164 dq->dq_btime = time_second +
165 VFSTOEXT2(ITOV(ip)->v_mount)->um_btime[i];
166 dq->dq_curblocks += change;
167 dq->dq_flags |= DQ_MOD;
173 * Check for a valid change to a users allocation.
174 * Issue an error message if appropriate.
177 ext2_chkdqchg(struct inode *ip, long change, struct ucred *cred, int type)
179 struct ext2_dquot *dq = ip->i_dquot[type];
180 long ncurblocks = dq->dq_curblocks + change;
183 * If user would exceed their hard limit, disallow space allocation.
185 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
186 if ((dq->dq_flags & DQ_BLKS) == 0 &&
187 ip->i_uid == cred->cr_uid) {
188 uprintf("\n%s: write failed, %s disk limit reached\n",
189 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
191 dq->dq_flags |= DQ_BLKS;
196 * If user is over their soft limit for too long, disallow space
197 * allocation. Reset time limit as they cross their soft limit.
199 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
200 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
201 dq->dq_btime = time_second +
202 VFSTOEXT2(ITOV(ip)->v_mount)->um_btime[type];
203 if (ip->i_uid == cred->cr_uid)
204 uprintf("\n%s: warning, %s %s\n",
205 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
206 quotatypes[type], "disk quota exceeded");
209 if (time_second > dq->dq_btime) {
210 if ((dq->dq_flags & DQ_BLKS) == 0 &&
211 ip->i_uid == cred->cr_uid) {
212 uprintf("\n%s: write failed, %s %s\n",
213 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
215 "disk quota exceeded for too long");
216 dq->dq_flags |= DQ_BLKS;
225 * Check the inode limit, applying corrective action.
228 ext2_chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
230 struct ext2_dquot *dq;
232 int ncurinodes, error;
235 if ((flags & CHOWN) == 0)
241 for (i = 0; i < MAXQUOTAS; i++) {
242 if ((dq = ip->i_dquot[i]) == NODQUOT)
244 while (dq->dq_flags & DQ_LOCK) {
245 dq->dq_flags |= DQ_WANT;
246 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
248 ncurinodes = dq->dq_curinodes + change;
250 dq->dq_curinodes = ncurinodes;
252 dq->dq_curinodes = 0;
253 dq->dq_flags &= ~DQ_INODS;
254 dq->dq_flags |= DQ_MOD;
258 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
259 for (i = 0; i < MAXQUOTAS; i++) {
260 if ((dq = ip->i_dquot[i]) == NODQUOT)
262 error = ext2_chkiqchg(ip, change, cred, i);
267 for (i = 0; i < MAXQUOTAS; i++) {
268 if ((dq = ip->i_dquot[i]) == NODQUOT)
270 while (dq->dq_flags & DQ_LOCK) {
271 dq->dq_flags |= DQ_WANT;
272 (void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
274 /* Reset timer when crossing soft limit */
275 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
276 dq->dq_curinodes < dq->dq_isoftlimit)
277 dq->dq_itime = time_second +
278 VFSTOEXT2(ITOV(ip)->v_mount)->um_itime[i];
279 dq->dq_curinodes += change;
280 dq->dq_flags |= DQ_MOD;
286 * Check for a valid change to a users allocation.
287 * Issue an error message if appropriate.
290 ext2_chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
292 struct ext2_dquot *dq = ip->i_dquot[type];
293 long ncurinodes = dq->dq_curinodes + change;
296 * If user would exceed their hard limit, disallow inode allocation.
298 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
299 if ((dq->dq_flags & DQ_INODS) == 0 &&
300 ip->i_uid == cred->cr_uid) {
301 uprintf("\n%s: write failed, %s inode limit reached\n",
302 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
304 dq->dq_flags |= DQ_INODS;
309 * If user is over their soft limit for too long, disallow inode
310 * allocation. Reset time limit as they cross their soft limit.
312 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
313 if (dq->dq_curinodes < dq->dq_isoftlimit) {
314 dq->dq_itime = time_second +
315 VFSTOEXT2(ITOV(ip)->v_mount)->um_itime[type];
316 if (ip->i_uid == cred->cr_uid)
317 uprintf("\n%s: warning, %s %s\n",
318 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
319 quotatypes[type], "inode quota exceeded");
322 if (time_second > dq->dq_itime) {
323 if ((dq->dq_flags & DQ_INODS) == 0 &&
324 ip->i_uid == cred->cr_uid) {
325 uprintf("\n%s: write failed, %s %s\n",
326 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
328 "inode quota exceeded for too long");
329 dq->dq_flags |= DQ_INODS;
339 * On filesystems with quotas enabled, it is an error for a file to change
340 * size and not to have a dquot structure associated with it.
343 ext2_chkdquot(struct inode *ip)
345 struct ext2mount *ump = VFSTOEXT2(ITOV(ip)->v_mount);
348 for (i = 0; i < MAXQUOTAS; i++) {
349 if (ump->um_quotas[i] == NULLVP ||
350 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
352 if (ip->i_dquot[i] == NODQUOT) {
353 vprint("chkdquot: missing dquot", ITOV(ip));
354 panic("chkdquot: missing dquot");
361 * Code to process quotactl commands.
370 * Q_QUOTAON - set up a quota file for a particular filesystem.
372 static int ext2_quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
375 ext2_quotaon(struct ucred *cred, struct mount *mp, int type, caddr_t fname)
377 struct ext2mount *ump = VFSTOEXT2(mp);
378 struct vnode *vp, **vpp;
379 struct ext2_dquot *dq;
381 struct nlookupdata nd;
382 struct scaninfo scaninfo;
384 vpp = &ump->um_quotas[type];
385 error = nlookup_init(&nd, fname, UIO_USERSPACE, NLC_FOLLOW|NLC_LOCKVP);
387 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
388 if (error == 0 && nd.nl_open_vp->v_type != VREG)
395 nd.nl_open_vp = NULL;
400 ext2_quotaoff(mp, type);
401 ump->um_qflags[type] |= QTF_OPENING;
402 mp->mnt_flag |= MNT_QUOTA;
403 vsetflags(vp, VSYSTEM);
405 /* XXX release duplicate vp if *vpp == vp? */
407 * Save the credential of the process that turned on quotas.
408 * Set up the time limits for this quota.
410 ump->um_cred[type] = crhold(cred);
411 ump->um_btime[type] = MAX_DQ_TIME;
412 ump->um_itime[type] = MAX_IQ_TIME;
413 if (ext2_dqget(NULLVP, 0, ump, type, &dq) == 0) {
414 if (dq->dq_btime > 0)
415 ump->um_btime[type] = dq->dq_btime;
416 if (dq->dq_itime > 0)
417 ump->um_itime[type] = dq->dq_itime;
418 ext2_dqrele(NULLVP, dq);
421 * Search vnodes associated with this mount point,
422 * adding references to quota file being opened.
423 * NB: only need to add dquot's for inodes being modified.
426 while (scaninfo.rescan) {
428 error = vmntvnodescan(mp, VMSC_GETVP,
429 NULL, ext2_quotaon_scan, &scaninfo);
433 ump->um_qflags[type] &= ~QTF_OPENING;
435 ext2_quotaoff(mp, type);
440 ext2_quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
443 /*struct scaninfo *info = data;*/
445 if (vp->v_writecount == 0)
447 error = ext2_getinoquota(VTOI(vp));
452 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
455 static int ext2_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
458 ext2_quotaoff(struct mount *mp, int type)
461 struct ext2mount *ump = VFSTOEXT2(mp);
463 struct scaninfo scaninfo;
465 if ((qvp = ump->um_quotas[type]) == NULLVP)
467 ump->um_qflags[type] |= QTF_CLOSING;
470 * Search vnodes associated with this mount point,
471 * deleting any references to quota file being closed.
474 scaninfo.type = type;
475 while (scaninfo.rescan) {
477 vmntvnodescan(mp, VMSC_GETVP, NULL, ext2_quotaoff_scan, &scaninfo);
480 vclrflags(qvp, VSYSTEM);
481 error = vn_close(qvp, FREAD|FWRITE, NULL);
482 ump->um_quotas[type] = NULLVP;
483 crfree(ump->um_cred[type]);
484 ump->um_cred[type] = NOCRED;
485 ump->um_qflags[type] &= ~QTF_CLOSING;
486 for (type = 0; type < MAXQUOTAS; type++) {
487 if (ump->um_quotas[type] != NULLVP)
490 if (type == MAXQUOTAS)
491 mp->mnt_flag &= ~MNT_QUOTA;
496 ext2_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
498 struct scaninfo *info = data;
499 struct ext2_dquot *dq;
502 if (vp->v_type == VNON) {
506 dq = ip->i_dquot[info->type];
507 ip->i_dquot[info->type] = NODQUOT;
513 * Q_GETQUOTA - return current values in a dqblk structure.
516 ext2_getquota(struct mount *mp, u_long id, int type, caddr_t addr)
518 struct ext2_dquot *dq;
521 error = ext2_dqget(NULLVP, id, VFSTOEXT2(mp), type, &dq);
524 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct ext2_dqblk));
525 ext2_dqrele(NULLVP, dq);
530 * Q_SETQUOTA - assign an entire dqblk structure.
533 ext2_setquota(struct mount *mp, u_long id, int type, caddr_t addr)
535 struct ext2_dquot *dq;
536 struct ext2_dquot *ndq;
537 struct ext2mount *ump = VFSTOEXT2(mp);
538 struct ext2_dqblk newlim;
541 error = copyin(addr, (caddr_t)&newlim, sizeof (struct ext2_dqblk));
544 error = ext2_dqget(NULLVP, id, ump, type, &ndq);
548 while (dq->dq_flags & DQ_LOCK) {
549 dq->dq_flags |= DQ_WANT;
550 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
553 * Copy all but the current values.
554 * Reset time limit if previously had no soft limit or were
555 * under it, but now have a soft limit and are over it.
557 newlim.dqb_curblocks = dq->dq_curblocks;
558 newlim.dqb_curinodes = dq->dq_curinodes;
559 if (dq->dq_id != 0) {
560 newlim.dqb_btime = dq->dq_btime;
561 newlim.dqb_itime = dq->dq_itime;
563 if (newlim.dqb_bsoftlimit &&
564 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
565 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
566 newlim.dqb_btime = time_second + ump->um_btime[type];
567 if (newlim.dqb_isoftlimit &&
568 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
569 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
570 newlim.dqb_itime = time_second + ump->um_itime[type];
572 if (dq->dq_curblocks < dq->dq_bsoftlimit)
573 dq->dq_flags &= ~DQ_BLKS;
574 if (dq->dq_curinodes < dq->dq_isoftlimit)
575 dq->dq_flags &= ~DQ_INODS;
576 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
577 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
578 dq->dq_flags |= DQ_FAKE;
580 dq->dq_flags &= ~DQ_FAKE;
581 dq->dq_flags |= DQ_MOD;
582 ext2_dqrele(NULLVP, dq);
587 * Q_SETUSE - set current inode and block usage.
590 ext2_setuse(struct mount *mp, u_long id, int type, caddr_t addr)
592 struct ext2_dquot *dq;
593 struct ext2mount *ump = VFSTOEXT2(mp);
594 struct ext2_dquot *ndq;
595 struct ext2_dqblk usage;
598 error = copyin(addr, (caddr_t)&usage, sizeof (struct ext2_dqblk));
601 error = ext2_dqget(NULLVP, id, ump, type, &ndq);
605 while (dq->dq_flags & DQ_LOCK) {
606 dq->dq_flags |= DQ_WANT;
607 (void) tsleep((caddr_t)dq, 0, "setuse", 0);
610 * Reset time limit if have a soft limit and were
611 * previously under it, but are now over it.
613 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
614 usage.dqb_curblocks >= dq->dq_bsoftlimit)
615 dq->dq_btime = time_second + ump->um_btime[type];
616 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
617 usage.dqb_curinodes >= dq->dq_isoftlimit)
618 dq->dq_itime = time_second + ump->um_itime[type];
619 dq->dq_curblocks = usage.dqb_curblocks;
620 dq->dq_curinodes = usage.dqb_curinodes;
621 if (dq->dq_curblocks < dq->dq_bsoftlimit)
622 dq->dq_flags &= ~DQ_BLKS;
623 if (dq->dq_curinodes < dq->dq_isoftlimit)
624 dq->dq_flags &= ~DQ_INODS;
625 dq->dq_flags |= DQ_MOD;
626 ext2_dqrele(NULLVP, dq);
631 * Q_SYNC - sync quota files to disk.
634 static int ext2_qsync_scan(struct mount *mp, struct vnode *vp, void *data);
637 ext2_qsync(struct mount *mp)
639 struct ext2mount *ump = VFSTOEXT2(mp);
640 struct scaninfo scaninfo;
644 * Check if the mount point has any quotas.
645 * If not, simply return.
647 for (i = 0; i < MAXQUOTAS; i++)
648 if (ump->um_quotas[i] != NULLVP)
653 * Search vnodes associated with this mount point,
654 * synchronizing any modified ext2_dquot structures.
657 while (scaninfo.rescan) {
659 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
660 NULL, ext2_qsync_scan, &scaninfo);
666 ext2_qsync_scan(struct mount *mp, struct vnode *vp, void *data)
668 /*struct scaninfo *info = data;*/
669 struct ext2_dquot *dq;
673 for (i = 0; i < MAXQUOTAS; i++) {
674 dq = VTOI(vp)->i_dquot[i];
675 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
682 * Code pertaining to management of the in-core dquot data structures.
684 #define DQHASH(dqvp, id) \
685 (&ext2_dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & ext2_dqhash])
686 static LIST_HEAD(ext2_dqhash, ext2_dquot) *ext2_dqhashtbl;
687 static u_long ext2_dqhash;
692 #define DQUOTINC 5 /* minimum free dquots desired */
693 static TAILQ_HEAD(ext2_dqfreelist, ext2_dquot) ext2_dqfreelist;
694 static long ext2_numdquot, ext2_desireddquot = DQUOTINC;
697 * Initialize the quota system.
702 ext2_dqhashtbl = hashinit(desiredvnodes, M_EXT2DQUOT, &ext2_dqhash);
703 TAILQ_INIT(&ext2_dqfreelist);
707 * Obtain a dquot structure for the specified identifier and quota file
708 * reading the information from the file if necessary.
711 ext2_dqget(struct vnode *vp, u_long id, struct ext2mount *ump, int type,
712 struct ext2_dquot **dqp)
714 struct ext2_dquot *dq;
715 struct ext2_dqhash *dqh;
721 dqvp = ump->um_quotas[type];
722 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
727 * Check the cache first.
729 dqh = DQHASH(dqvp, id);
730 LIST_FOREACH(dq, dqh, dq_hash) {
731 if (dq->dq_id != id ||
732 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
735 * Cache hit with no references. Take
736 * the structure off the free list.
739 TAILQ_REMOVE(&ext2_dqfreelist, dq, dq_freelist);
745 * Not in cache, allocate a new one.
747 if (TAILQ_EMPTY(&ext2_dqfreelist) && ext2_numdquot < MAXQUOTAS * desiredvnodes)
748 ext2_desireddquot += DQUOTINC;
749 if (ext2_numdquot < ext2_desireddquot) {
750 dq = (struct ext2_dquot *)kmalloc(sizeof *dq, M_EXT2DQUOT, M_WAITOK | M_ZERO);
753 if ((dq = TAILQ_FIRST(&ext2_dqfreelist)) == NULL) {
758 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
759 panic("dqget: free dquot isn't");
760 TAILQ_REMOVE(&ext2_dqfreelist, dq, dq_freelist);
761 if (dq->dq_ump != NULL)
762 LIST_REMOVE(dq, dq_hash);
765 * Initialize the contents of the dquot structure.
768 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
769 LIST_INSERT_HEAD(dqh, dq, dq_hash);
771 dq->dq_flags = DQ_LOCK;
775 auio.uio_iov = &aiov;
777 aiov.iov_base = (caddr_t)&dq->dq_dqb;
778 aiov.iov_len = sizeof (struct ext2_dqblk);
779 auio.uio_resid = sizeof (struct ext2_dqblk);
780 auio.uio_offset = (off_t)(id * sizeof (struct ext2_dqblk));
781 auio.uio_segflg = UIO_SYSSPACE;
782 auio.uio_rw = UIO_READ;
784 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
785 if (auio.uio_resid == sizeof(struct ext2_dqblk) && error == 0)
786 bzero((caddr_t)&dq->dq_dqb, sizeof(struct ext2_dqblk));
789 if (dq->dq_flags & DQ_WANT)
793 * I/O error in reading quota file, release
794 * quota structure and reflect problem to caller.
797 LIST_REMOVE(dq, dq_hash);
803 * Check for no limit to enforce.
804 * Initialize time values if necessary.
806 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
807 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
808 dq->dq_flags |= DQ_FAKE;
809 if (dq->dq_id != 0) {
810 if (dq->dq_btime == 0)
811 dq->dq_btime = time_second + ump->um_btime[type];
812 if (dq->dq_itime == 0)
813 dq->dq_itime = time_second + ump->um_itime[type];
821 * Obtain a reference to a dquot.
824 ext2_dqref(struct ext2_dquot *dq)
831 * Release a reference to a dquot.
834 ext2_dqrele(struct vnode *vp, struct ext2_dquot *dq)
838 if (dq->dq_cnt > 1) {
842 if (dq->dq_flags & DQ_MOD)
843 (void)ext2_dqsync(vp, dq);
844 if (--dq->dq_cnt > 0)
846 TAILQ_INSERT_TAIL(&ext2_dqfreelist, dq, dq_freelist);
850 * Update the disk quota in the quota file.
853 ext2_dqsync(struct vnode *vp, struct ext2_dquot *dq)
861 panic("dqsync: dquot");
862 if ((dq->dq_flags & DQ_MOD) == 0)
864 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
865 panic("dqsync: file");
867 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
868 while (dq->dq_flags & DQ_LOCK) {
869 dq->dq_flags |= DQ_WANT;
870 (void) tsleep((caddr_t)dq, 0, "dqsync", 0);
871 if ((dq->dq_flags & DQ_MOD) == 0) {
877 dq->dq_flags |= DQ_LOCK;
878 auio.uio_iov = &aiov;
880 aiov.iov_base = (caddr_t)&dq->dq_dqb;
881 aiov.iov_len = sizeof (struct ext2_dqblk);
882 auio.uio_resid = sizeof (struct ext2_dqblk);
883 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct ext2_dqblk));
884 auio.uio_segflg = UIO_SYSSPACE;
885 auio.uio_rw = UIO_WRITE;
887 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
888 if (auio.uio_resid && error == 0)
890 if (dq->dq_flags & DQ_WANT)
892 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
899 * Flush all entries from the cache for a particular vnode.
902 ext2_dqflush(struct vnode *vp)
904 struct ext2_dquot *dq, *nextdq;
905 struct ext2_dqhash *dqh;
908 * Move all dquot's that used to refer to this quota
909 * file off their hash chains (they will eventually
910 * fall off the head of the free list and be re-used).
912 for (dqh = &ext2_dqhashtbl[ext2_dqhash]; dqh >= ext2_dqhashtbl; dqh--) {
913 for (dq = dqh->lh_first; dq; dq = nextdq) {
914 nextdq = dq->dq_hash.le_next;
915 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
918 panic("dqflush: stray dquot");
919 LIST_REMOVE(dq, dq_hash);