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/gnu/ext2fs/ext2_quota.c,v 1.8 2008/01/05 14:02:41 swildner 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/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <vm/vm_zone.h>
55 #include "ext2mount.h"
56 #include "ext2_extern.h"
58 static MALLOC_DEFINE(M_EXT2DQUOT, "EXT2 quota", "EXT2 quota entries");
61 * Quota name to error message mapping.
63 static char *quotatypes[] = INITQFNAMES;
65 static int ext2_chkdqchg (struct inode *, long, struct ucred *, int);
66 static int ext2_chkiqchg (struct inode *, long, struct ucred *, int);
67 static int ext2_dqget (struct vnode *,
68 u_long, struct ext2mount *, int, struct ext2_dquot **);
69 static int ext2_dqsync (struct vnode *, struct ext2_dquot *);
70 static void ext2_dqflush (struct vnode *);
73 static void ext2_dqref (struct ext2_dquot *);
74 static void ext2_chkdquot (struct inode *);
78 * Set up the quotas for an inode.
80 * This routine completely defines the semantics of quotas.
81 * If other criterion want to be used to establish quotas, the
82 * MAXQUOTAS value in quotas.h should be increased, and the
83 * additional dquots set up here.
86 ext2_getinoquota(struct inode *ip)
88 struct ext2mount *ump;
89 struct vnode *vp = ITOV(ip);
92 ump = VFSTOEXT2(vp->v_mount);
94 * Set up the user quota based on file uid.
95 * EINVAL means that quotas are not enabled.
97 if (ip->i_dquot[USRQUOTA] == NODQUOT &&
98 (error = ext2_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 &&
106 (error = ext2_dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
113 * Update disk usage, and take corrective action.
116 ext2_chkdq(struct inode *ip, long change, struct ucred *cred, int flags)
118 struct ext2_dquot *dq;
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 = ext2_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 VFSTOEXT2(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 ext2_chkdqchg(struct inode *ip, long change, struct ucred *cred, int type)
180 struct ext2_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_mntfromname,
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 VFSTOEXT2(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_mntfromname,
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_mntfromname,
216 "disk quota exceeded for too long");
217 dq->dq_flags |= DQ_BLKS;
226 * Check the inode limit, applying corrective action.
229 ext2_chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
231 struct ext2_dquot *dq;
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 = ext2_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 VFSTOEXT2(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 ext2_chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
293 struct ext2_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_mntfromname,
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 VFSTOEXT2(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_mntfromname,
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_mntfromname,
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 ext2_chkdquot(struct inode *ip)
346 struct ext2mount *ump = VFSTOEXT2(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.
371 * Q_QUOTAON - set up a quota file for a particular filesystem.
373 static int ext2_quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
376 ext2_quotaon(struct ucred *cred, struct mount *mp, int type, caddr_t fname)
378 struct ext2mount *ump = VFSTOEXT2(mp);
379 struct vnode *vp, **vpp;
380 struct ext2_dquot *dq;
382 struct nlookupdata nd;
383 struct scaninfo scaninfo;
385 vpp = &ump->um_quotas[type];
386 error = nlookup_init(&nd, fname, UIO_USERSPACE, NLC_FOLLOW|NLC_LOCKVP);
388 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
389 if (error == 0 && nd.nl_open_vp->v_type != VREG)
396 nd.nl_open_vp = NULL;
401 ext2_quotaoff(mp, type);
402 ump->um_qflags[type] |= QTF_OPENING;
403 mp->mnt_flag |= MNT_QUOTA;
404 vsetflags(vp, VSYSTEM);
406 /* XXX release duplicate vp if *vpp == vp? */
408 * Save the credential of the process that turned on quotas.
409 * Set up the time limits for this quota.
411 ump->um_cred[type] = crhold(cred);
412 ump->um_btime[type] = MAX_DQ_TIME;
413 ump->um_itime[type] = MAX_IQ_TIME;
414 if (ext2_dqget(NULLVP, 0, ump, type, &dq) == 0) {
415 if (dq->dq_btime > 0)
416 ump->um_btime[type] = dq->dq_btime;
417 if (dq->dq_itime > 0)
418 ump->um_itime[type] = dq->dq_itime;
419 ext2_dqrele(NULLVP, dq);
422 * Search vnodes associated with this mount point,
423 * adding references to quota file being opened.
424 * NB: only need to add dquot's for inodes being modified.
427 while (scaninfo.rescan) {
429 error = vmntvnodescan(mp, VMSC_GETVP,
430 NULL, ext2_quotaon_scan, &scaninfo);
434 ump->um_qflags[type] &= ~QTF_OPENING;
436 ext2_quotaoff(mp, type);
441 ext2_quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
444 /*struct scaninfo *info = data;*/
446 if (vp->v_writecount == 0)
448 error = ext2_getinoquota(VTOI(vp));
453 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
456 static int ext2_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
459 ext2_quotaoff(struct mount *mp, int type)
462 struct ext2mount *ump = VFSTOEXT2(mp);
464 struct scaninfo scaninfo;
466 if ((qvp = ump->um_quotas[type]) == NULLVP)
468 ump->um_qflags[type] |= QTF_CLOSING;
471 * Search vnodes associated with this mount point,
472 * deleting any references to quota file being closed.
475 scaninfo.type = type;
476 while (scaninfo.rescan) {
478 vmntvnodescan(mp, VMSC_GETVP, NULL, ext2_quotaoff_scan, &scaninfo);
481 vclrflags(qvp, VSYSTEM);
482 error = vn_close(qvp, FREAD|FWRITE);
483 ump->um_quotas[type] = NULLVP;
484 crfree(ump->um_cred[type]);
485 ump->um_cred[type] = NOCRED;
486 ump->um_qflags[type] &= ~QTF_CLOSING;
487 for (type = 0; type < MAXQUOTAS; type++) {
488 if (ump->um_quotas[type] != NULLVP)
491 if (type == MAXQUOTAS)
492 mp->mnt_flag &= ~MNT_QUOTA;
497 ext2_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
499 struct scaninfo *info = data;
500 struct ext2_dquot *dq;
503 if (vp->v_type == VNON) {
507 dq = ip->i_dquot[info->type];
508 ip->i_dquot[info->type] = NODQUOT;
514 * Q_GETQUOTA - return current values in a dqblk structure.
517 ext2_getquota(struct mount *mp, u_long id, int type, caddr_t addr)
519 struct ext2_dquot *dq;
522 error = ext2_dqget(NULLVP, id, VFSTOEXT2(mp), type, &dq);
525 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct ext2_dqblk));
526 ext2_dqrele(NULLVP, dq);
531 * Q_SETQUOTA - assign an entire dqblk structure.
534 ext2_setquota(struct mount *mp, u_long id, int type, caddr_t addr)
536 struct ext2_dquot *dq;
537 struct ext2_dquot *ndq;
538 struct ext2mount *ump = VFSTOEXT2(mp);
539 struct ext2_dqblk newlim;
542 error = copyin(addr, (caddr_t)&newlim, sizeof (struct ext2_dqblk));
545 error = ext2_dqget(NULLVP, id, ump, type, &ndq);
549 while (dq->dq_flags & DQ_LOCK) {
550 dq->dq_flags |= DQ_WANT;
551 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
554 * Copy all but the current values.
555 * Reset time limit if previously had no soft limit or were
556 * under it, but now have a soft limit and are over it.
558 newlim.dqb_curblocks = dq->dq_curblocks;
559 newlim.dqb_curinodes = dq->dq_curinodes;
560 if (dq->dq_id != 0) {
561 newlim.dqb_btime = dq->dq_btime;
562 newlim.dqb_itime = dq->dq_itime;
564 if (newlim.dqb_bsoftlimit &&
565 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
566 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
567 newlim.dqb_btime = time_second + ump->um_btime[type];
568 if (newlim.dqb_isoftlimit &&
569 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
570 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
571 newlim.dqb_itime = time_second + ump->um_itime[type];
573 if (dq->dq_curblocks < dq->dq_bsoftlimit)
574 dq->dq_flags &= ~DQ_BLKS;
575 if (dq->dq_curinodes < dq->dq_isoftlimit)
576 dq->dq_flags &= ~DQ_INODS;
577 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
578 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
579 dq->dq_flags |= DQ_FAKE;
581 dq->dq_flags &= ~DQ_FAKE;
582 dq->dq_flags |= DQ_MOD;
583 ext2_dqrele(NULLVP, dq);
588 * Q_SETUSE - set current inode and block usage.
591 ext2_setuse(struct mount *mp, u_long id, int type, caddr_t addr)
593 struct ext2_dquot *dq;
594 struct ext2mount *ump = VFSTOEXT2(mp);
595 struct ext2_dquot *ndq;
596 struct ext2_dqblk usage;
599 error = copyin(addr, (caddr_t)&usage, sizeof (struct ext2_dqblk));
602 error = ext2_dqget(NULLVP, id, ump, type, &ndq);
606 while (dq->dq_flags & DQ_LOCK) {
607 dq->dq_flags |= DQ_WANT;
608 (void) tsleep((caddr_t)dq, 0, "setuse", 0);
611 * Reset time limit if have a soft limit and were
612 * previously under it, but are now over it.
614 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
615 usage.dqb_curblocks >= dq->dq_bsoftlimit)
616 dq->dq_btime = time_second + ump->um_btime[type];
617 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
618 usage.dqb_curinodes >= dq->dq_isoftlimit)
619 dq->dq_itime = time_second + ump->um_itime[type];
620 dq->dq_curblocks = usage.dqb_curblocks;
621 dq->dq_curinodes = usage.dqb_curinodes;
622 if (dq->dq_curblocks < dq->dq_bsoftlimit)
623 dq->dq_flags &= ~DQ_BLKS;
624 if (dq->dq_curinodes < dq->dq_isoftlimit)
625 dq->dq_flags &= ~DQ_INODS;
626 dq->dq_flags |= DQ_MOD;
627 ext2_dqrele(NULLVP, dq);
632 * Q_SYNC - sync quota files to disk.
635 static int ext2_qsync_scan(struct mount *mp, struct vnode *vp, void *data);
638 ext2_qsync(struct mount *mp)
640 struct ext2mount *ump = VFSTOEXT2(mp);
641 struct scaninfo scaninfo;
645 * Check if the mount point has any quotas.
646 * If not, simply return.
648 for (i = 0; i < MAXQUOTAS; i++)
649 if (ump->um_quotas[i] != NULLVP)
654 * Search vnodes associated with this mount point,
655 * synchronizing any modified ext2_dquot structures.
658 while (scaninfo.rescan) {
660 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
661 NULL, ext2_qsync_scan, &scaninfo);
667 ext2_qsync_scan(struct mount *mp, struct vnode *vp, void *data)
669 /*struct scaninfo *info = data;*/
670 struct ext2_dquot *dq;
674 for (i = 0; i < MAXQUOTAS; i++) {
675 dq = VTOI(vp)->i_dquot[i];
676 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
683 * Code pertaining to management of the in-core dquot data structures.
685 #define DQHASH(dqvp, id) \
686 (&ext2_dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & ext2_dqhash])
687 static LIST_HEAD(ext2_dqhash, ext2_dquot) *ext2_dqhashtbl;
688 static u_long ext2_dqhash;
693 #define DQUOTINC 5 /* minimum free dquots desired */
694 static TAILQ_HEAD(ext2_dqfreelist, ext2_dquot) ext2_dqfreelist;
695 static long ext2_numdquot, ext2_desireddquot = DQUOTINC;
698 * Initialize the quota system.
703 ext2_dqhashtbl = hashinit(desiredvnodes, M_EXT2DQUOT, &ext2_dqhash);
704 TAILQ_INIT(&ext2_dqfreelist);
708 * Obtain a dquot structure for the specified identifier and quota file
709 * reading the information from the file if necessary.
712 ext2_dqget(struct vnode *vp, u_long id, struct ext2mount *ump, int type,
713 struct ext2_dquot **dqp)
715 struct ext2_dquot *dq;
716 struct ext2_dqhash *dqh;
722 dqvp = ump->um_quotas[type];
723 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
728 * Check the cache first.
730 dqh = DQHASH(dqvp, id);
731 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
732 if (dq->dq_id != id ||
733 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
736 * Cache hit with no references. Take
737 * the structure off the free list.
740 TAILQ_REMOVE(&ext2_dqfreelist, dq, dq_freelist);
746 * Not in cache, allocate a new one.
748 if (TAILQ_EMPTY(&ext2_dqfreelist) && ext2_numdquot < MAXQUOTAS * desiredvnodes)
749 ext2_desireddquot += DQUOTINC;
750 if (ext2_numdquot < ext2_desireddquot) {
751 dq = (struct ext2_dquot *)kmalloc(sizeof *dq, M_EXT2DQUOT, M_WAITOK | M_ZERO);
754 if ((dq = TAILQ_FIRST(&ext2_dqfreelist)) == NULL) {
759 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
760 panic("dqget: free dquot isn't");
761 TAILQ_REMOVE(&ext2_dqfreelist, dq, dq_freelist);
762 if (dq->dq_ump != NULL)
763 LIST_REMOVE(dq, dq_hash);
766 * Initialize the contents of the dquot structure.
769 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
770 LIST_INSERT_HEAD(dqh, dq, dq_hash);
772 dq->dq_flags = DQ_LOCK;
776 auio.uio_iov = &aiov;
778 aiov.iov_base = (caddr_t)&dq->dq_dqb;
779 aiov.iov_len = sizeof (struct ext2_dqblk);
780 auio.uio_resid = sizeof (struct ext2_dqblk);
781 auio.uio_offset = (off_t)(id * sizeof (struct ext2_dqblk));
782 auio.uio_segflg = UIO_SYSSPACE;
783 auio.uio_rw = UIO_READ;
785 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
786 if (auio.uio_resid == sizeof(struct ext2_dqblk) && error == 0)
787 bzero((caddr_t)&dq->dq_dqb, sizeof(struct ext2_dqblk));
790 if (dq->dq_flags & DQ_WANT)
794 * I/O error in reading quota file, release
795 * quota structure and reflect problem to caller.
798 LIST_REMOVE(dq, dq_hash);
804 * Check for no limit to enforce.
805 * Initialize time values if necessary.
807 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
808 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
809 dq->dq_flags |= DQ_FAKE;
810 if (dq->dq_id != 0) {
811 if (dq->dq_btime == 0)
812 dq->dq_btime = time_second + ump->um_btime[type];
813 if (dq->dq_itime == 0)
814 dq->dq_itime = time_second + ump->um_itime[type];
822 * Obtain a reference to a dquot.
825 ext2_dqref(struct ext2_dquot *dq)
832 * Release a reference to a dquot.
835 ext2_dqrele(struct vnode *vp, struct ext2_dquot *dq)
839 if (dq->dq_cnt > 1) {
843 if (dq->dq_flags & DQ_MOD)
844 (void)ext2_dqsync(vp, dq);
845 if (--dq->dq_cnt > 0)
847 TAILQ_INSERT_TAIL(&ext2_dqfreelist, dq, dq_freelist);
851 * Update the disk quota in the quota file.
854 ext2_dqsync(struct vnode *vp, struct ext2_dquot *dq)
862 panic("dqsync: dquot");
863 if ((dq->dq_flags & DQ_MOD) == 0)
865 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
866 panic("dqsync: file");
868 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
869 while (dq->dq_flags & DQ_LOCK) {
870 dq->dq_flags |= DQ_WANT;
871 (void) tsleep((caddr_t)dq, 0, "dqsync", 0);
872 if ((dq->dq_flags & DQ_MOD) == 0) {
878 dq->dq_flags |= DQ_LOCK;
879 auio.uio_iov = &aiov;
881 aiov.iov_base = (caddr_t)&dq->dq_dqb;
882 aiov.iov_len = sizeof (struct ext2_dqblk);
883 auio.uio_resid = sizeof (struct ext2_dqblk);
884 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct ext2_dqblk));
885 auio.uio_segflg = UIO_SYSSPACE;
886 auio.uio_rw = UIO_WRITE;
888 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
889 if (auio.uio_resid && error == 0)
891 if (dq->dq_flags & DQ_WANT)
893 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
900 * Flush all entries from the cache for a particular vnode.
903 ext2_dqflush(struct vnode *vp)
905 struct ext2_dquot *dq, *nextdq;
906 struct ext2_dqhash *dqh;
909 * Move all dquot's that used to refer to this quota
910 * file off their hash chains (they will eventually
911 * fall off the head of the free list and be re-used).
913 for (dqh = &ext2_dqhashtbl[ext2_dqhash]; dqh >= ext2_dqhashtbl; dqh--) {
914 for (dq = dqh->lh_first; dq; dq = nextdq) {
915 nextdq = dq->dq_hash.le_next;
916 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
919 panic("dqflush: stray dquot");
920 LIST_REMOVE(dq, dq_hash);