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>
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 ufs_chkdqchg (struct inode *, long, struct ucred *, int);
63 static int ufs_chkiqchg (struct inode *, long, struct ucred *, int);
64 static int ufs_dqget (struct vnode *,
65 u_long, struct ufsmount *, int, struct ufs_dquot **);
66 static int ufs_dqsync (struct vnode *, struct ufs_dquot *);
67 static void ufs_dqflush (struct vnode *);
68 static void ufs_quotawarn(struct ufs_dquot *dq);
71 static void ufs_dqref (struct ufs_dquot *);
72 static void ufs_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 ufs_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 &&
96 (error = ufs_dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
100 * Set up the group quota based on file gid.
101 * EINVAL means that quotas are not enabled.
103 if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
104 (error = ufs_dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
111 * Update disk usage, and take corrective action.
114 ufs_chkdq(struct inode *ip, long change, struct ucred *cred, int flags)
116 struct ufs_dquot *dq;
118 int ncurblocks, error;
121 if ((flags & CHOWN) == 0)
127 for (i = 0; i < MAXQUOTAS; i++) {
128 if ((dq = ip->i_dquot[i]) == NODQUOT)
130 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
134 while (dq->dq_flags & DQ_LOCK) {
135 dq->dq_flags |= DQ_WANT;
136 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
138 ncurblocks = dq->dq_curblocks + change;
140 dq->dq_curblocks = ncurblocks;
142 dq->dq_curblocks = 0;
143 dq->dq_flags &= ~DQ_BLKS;
144 dq->dq_flags |= DQ_MOD;
148 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
149 for (i = 0; i < MAXQUOTAS; i++) {
150 if ((dq = ip->i_dquot[i]) == NODQUOT)
152 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
156 error = ufs_chkdqchg(ip, change, cred, i);
161 for (i = 0; i < MAXQUOTAS; i++) {
162 if ((dq = ip->i_dquot[i]) == NODQUOT)
164 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
168 while (dq->dq_flags & DQ_LOCK) {
169 dq->dq_flags |= DQ_WANT;
170 (void) tsleep((caddr_t)dq, 0, "chkdq2", 0);
172 /* Reset timer when crossing soft limit */
173 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
174 dq->dq_curblocks < dq->dq_bsoftlimit)
175 dq->dq_btime = time_second +
176 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
177 dq->dq_curblocks += change;
178 dq->dq_flags |= DQ_MOD;
184 * Check for a valid change to a users allocation.
185 * Issue an error message if appropriate.
188 ufs_chkdqchg(struct inode *ip, long change, struct ucred *cred, int type)
190 struct ufs_dquot *dq = ip->i_dquot[type];
191 long ncurblocks = dq->dq_curblocks + change;
194 * If user would exceed their hard limit, disallow space allocation.
196 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
197 if ((dq->dq_flags & DQ_BLKS) == 0 &&
198 ip->i_uid == cred->cr_uid) {
199 uprintf("\n%s: write failed, %s disk limit reached\n",
200 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
202 dq->dq_flags |= DQ_BLKS;
207 * If user is over their soft limit for too long, disallow space
208 * allocation. Reset time limit as they cross their soft limit.
210 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
211 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
212 dq->dq_btime = time_second +
213 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
214 if (ip->i_uid == cred->cr_uid)
215 uprintf("\n%s: warning, %s %s\n",
216 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
217 quotatypes[type], "disk quota exceeded");
220 if (time_second > dq->dq_btime) {
221 if ((dq->dq_flags & DQ_BLKS) == 0 &&
222 ip->i_uid == cred->cr_uid) {
223 uprintf("\n%s: write failed, %s %s\n",
224 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
226 "disk quota exceeded for too long");
227 dq->dq_flags |= DQ_BLKS;
236 * Check the inode limit, applying corrective action.
239 ufs_chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
241 struct ufs_dquot *dq;
243 int ncurinodes, error;
246 if ((flags & CHOWN) == 0)
252 for (i = 0; i < MAXQUOTAS; i++) {
253 if ((dq = ip->i_dquot[i]) == NODQUOT)
255 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
259 while (dq->dq_flags & DQ_LOCK) {
260 dq->dq_flags |= DQ_WANT;
261 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
263 ncurinodes = dq->dq_curinodes + change;
265 dq->dq_curinodes = ncurinodes;
267 dq->dq_curinodes = 0;
268 dq->dq_flags &= ~DQ_INODS;
269 dq->dq_flags |= DQ_MOD;
273 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
274 for (i = 0; i < MAXQUOTAS; i++) {
275 if ((dq = ip->i_dquot[i]) == NODQUOT)
277 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
281 error = ufs_chkiqchg(ip, change, cred, i);
286 for (i = 0; i < MAXQUOTAS; i++) {
287 if ((dq = ip->i_dquot[i]) == NODQUOT)
289 if (dq->dq_ump->um_quotas[dq->dq_type] == ip->i_vnode) {
293 while (dq->dq_flags & DQ_LOCK) {
294 dq->dq_flags |= DQ_WANT;
295 (void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
297 /* Reset timer when crossing soft limit */
298 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
299 dq->dq_curinodes < dq->dq_isoftlimit)
300 dq->dq_itime = time_second +
301 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
302 dq->dq_curinodes += change;
303 dq->dq_flags |= DQ_MOD;
309 * Check for a valid change to a users allocation.
310 * Issue an error message if appropriate.
313 ufs_chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
315 struct ufs_dquot *dq = ip->i_dquot[type];
316 long ncurinodes = dq->dq_curinodes + change;
319 * If user would exceed their hard limit, disallow inode allocation.
321 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
322 if ((dq->dq_flags & DQ_INODS) == 0 &&
323 ip->i_uid == cred->cr_uid) {
324 uprintf("\n%s: write failed, %s inode limit reached\n",
325 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
327 dq->dq_flags |= DQ_INODS;
332 * If user is over their soft limit for too long, disallow inode
333 * allocation. Reset time limit as they cross their soft limit.
335 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
336 if (dq->dq_curinodes < dq->dq_isoftlimit) {
337 dq->dq_itime = time_second +
338 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
339 if (ip->i_uid == cred->cr_uid)
340 uprintf("\n%s: warning, %s %s\n",
341 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
342 quotatypes[type], "inode quota exceeded");
345 if (time_second > dq->dq_itime) {
346 if ((dq->dq_flags & DQ_INODS) == 0 &&
347 ip->i_uid == cred->cr_uid) {
348 uprintf("\n%s: write failed, %s %s\n",
349 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
351 "inode quota exceeded for too long");
352 dq->dq_flags |= DQ_INODS;
361 * To avoid a deadlock we disallow quota operations on the quota file itself.
362 * This generally means that quotacheck was not run on the filesystem.
366 ufs_quotawarn(struct ufs_dquot *dq)
370 if (dqticks != ticks / hz) {
371 dqticks = ticks / hz;
372 uprintf("%s: warning, quota file expanded, quotacheck "
374 dq->dq_ump->um_mountp->mnt_stat.f_mntfromname);
380 * On filesystems with quotas enabled, it is an error for a file to change
381 * size and not to have a dquot structure associated with it.
384 ufs_chkdquot(struct inode *ip)
386 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
389 for (i = 0; i < MAXQUOTAS; i++) {
390 if (ump->um_quotas[i] == NULLVP ||
391 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
393 if (ip->i_dquot[i] == NODQUOT) {
394 vprint("chkdquot: missing dquot", ITOV(ip));
395 panic("chkdquot: missing dquot");
402 * Code to process quotactl commands.
411 * Q_QUOTAON - set up a quota file for a particular filesystem.
413 static int ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
416 ufs_quotaon(struct ucred *cred, struct mount *mp, int type, caddr_t fname)
418 struct ufsmount *ump = VFSTOUFS(mp);
419 struct vnode *vp, **vpp;
420 struct ufs_dquot *dq;
422 struct nlookupdata nd;
423 struct scaninfo scaninfo;
425 vpp = &ump->um_quotas[type];
426 error = nlookup_init(&nd, fname, UIO_USERSPACE, NLC_FOLLOW|NLC_LOCKVP);
428 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
429 if (error == 0 && nd.nl_open_vp->v_type != VREG)
436 nd.nl_open_vp = NULL;
441 ufs_quotaoff(mp, type);
442 ump->um_qflags[type] |= QTF_OPENING;
443 mp->mnt_flag |= MNT_QUOTA;
444 vsetflags(vp, VSYSTEM);
446 /* XXX release duplicate vp if *vpp == vp? */
448 * Save the credential of the process that turned on quotas.
449 * Set up the time limits for this quota.
451 ump->um_cred[type] = crhold(cred);
452 ump->um_btime[type] = MAX_DQ_TIME;
453 ump->um_itime[type] = MAX_IQ_TIME;
454 if (ufs_dqget(NULLVP, 0, ump, type, &dq) == 0) {
455 if (dq->dq_btime > 0)
456 ump->um_btime[type] = dq->dq_btime;
457 if (dq->dq_itime > 0)
458 ump->um_itime[type] = dq->dq_itime;
459 ufs_dqrele(NULLVP, dq);
462 * Search vnodes associated with this mount point,
463 * adding references to quota file being opened.
464 * NB: only need to add dquot's for inodes being modified.
467 while (scaninfo.rescan) {
469 error = vmntvnodescan(mp, VMSC_GETVP,
470 NULL, ufs_quotaon_scan, &scaninfo);
474 ump->um_qflags[type] &= ~QTF_OPENING;
476 ufs_quotaoff(mp, type);
481 ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
484 /*struct scaninfo *info = data;*/
486 if (vp->v_writecount == 0)
488 error = ufs_getinoquota(VTOI(vp));
493 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
496 static int ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
499 ufs_quotaoff(struct mount *mp, int type)
502 struct ufsmount *ump = VFSTOUFS(mp);
504 struct scaninfo scaninfo;
506 if ((qvp = ump->um_quotas[type]) == NULLVP)
508 ump->um_qflags[type] |= QTF_CLOSING;
511 * Search vnodes associated with this mount point,
512 * deleting any references to quota file being closed.
515 scaninfo.type = type;
516 while (scaninfo.rescan) {
518 vmntvnodescan(mp, VMSC_GETVP, NULL, ufs_quotaoff_scan, &scaninfo);
521 vclrflags(qvp, VSYSTEM);
522 error = vn_close(qvp, FREAD|FWRITE);
523 ump->um_quotas[type] = NULLVP;
524 crfree(ump->um_cred[type]);
525 ump->um_cred[type] = NOCRED;
526 ump->um_qflags[type] &= ~QTF_CLOSING;
527 for (type = 0; type < MAXQUOTAS; type++) {
528 if (ump->um_quotas[type] != NULLVP)
531 if (type == MAXQUOTAS)
532 mp->mnt_flag &= ~MNT_QUOTA;
537 ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
539 struct scaninfo *info = data;
540 struct ufs_dquot *dq;
543 if (vp->v_type == VNON) {
547 dq = ip->i_dquot[info->type];
548 ip->i_dquot[info->type] = NODQUOT;
554 * Q_GETQUOTA - return current values in a dqblk structure.
557 ufs_getquota(struct mount *mp, u_long id, int type, caddr_t addr)
559 struct ufs_dquot *dq;
562 error = ufs_dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
565 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct ufs_dqblk));
566 ufs_dqrele(NULLVP, dq);
571 * Q_SETQUOTA - assign an entire dqblk structure.
574 ufs_setquota(struct mount *mp, u_long id, int type, caddr_t addr)
576 struct ufs_dquot *dq;
577 struct ufs_dquot *ndq;
578 struct ufsmount *ump = VFSTOUFS(mp);
579 struct ufs_dqblk newlim;
582 error = copyin(addr, (caddr_t)&newlim, sizeof (struct ufs_dqblk));
585 error = ufs_dqget(NULLVP, id, ump, type, &ndq);
589 while (dq->dq_flags & DQ_LOCK) {
590 dq->dq_flags |= DQ_WANT;
591 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
594 * Copy all but the current values.
595 * Reset time limit if previously had no soft limit or were
596 * under it, but now have a soft limit and are over it.
598 newlim.dqb_curblocks = dq->dq_curblocks;
599 newlim.dqb_curinodes = dq->dq_curinodes;
600 if (dq->dq_id != 0) {
601 newlim.dqb_btime = dq->dq_btime;
602 newlim.dqb_itime = dq->dq_itime;
604 if (newlim.dqb_bsoftlimit &&
605 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
606 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
607 newlim.dqb_btime = time_second + ump->um_btime[type];
608 if (newlim.dqb_isoftlimit &&
609 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
610 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
611 newlim.dqb_itime = time_second + ump->um_itime[type];
613 if (dq->dq_curblocks < dq->dq_bsoftlimit)
614 dq->dq_flags &= ~DQ_BLKS;
615 if (dq->dq_curinodes < dq->dq_isoftlimit)
616 dq->dq_flags &= ~DQ_INODS;
617 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
618 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
619 dq->dq_flags |= DQ_FAKE;
621 dq->dq_flags &= ~DQ_FAKE;
622 dq->dq_flags |= DQ_MOD;
623 ufs_dqrele(NULLVP, dq);
628 * Q_SETUSE - set current inode and block usage.
631 ufs_setuse(struct mount *mp, u_long id, int type, caddr_t addr)
633 struct ufs_dquot *dq;
634 struct ufsmount *ump = VFSTOUFS(mp);
635 struct ufs_dquot *ndq;
636 struct ufs_dqblk usage;
639 error = copyin(addr, (caddr_t)&usage, sizeof (struct ufs_dqblk));
642 error = ufs_dqget(NULLVP, id, ump, type, &ndq);
646 while (dq->dq_flags & DQ_LOCK) {
647 dq->dq_flags |= DQ_WANT;
648 (void) tsleep((caddr_t)dq, 0, "setuse", 0);
651 * Reset time limit if have a soft limit and were
652 * previously under it, but are now over it.
654 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
655 usage.dqb_curblocks >= dq->dq_bsoftlimit)
656 dq->dq_btime = time_second + ump->um_btime[type];
657 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
658 usage.dqb_curinodes >= dq->dq_isoftlimit)
659 dq->dq_itime = time_second + ump->um_itime[type];
660 dq->dq_curblocks = usage.dqb_curblocks;
661 dq->dq_curinodes = usage.dqb_curinodes;
662 if (dq->dq_curblocks < dq->dq_bsoftlimit)
663 dq->dq_flags &= ~DQ_BLKS;
664 if (dq->dq_curinodes < dq->dq_isoftlimit)
665 dq->dq_flags &= ~DQ_INODS;
666 dq->dq_flags |= DQ_MOD;
667 ufs_dqrele(NULLVP, dq);
672 * Q_SYNC - sync quota files to disk.
675 static int ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data);
678 ufs_qsync(struct mount *mp)
680 struct ufsmount *ump = VFSTOUFS(mp);
681 struct scaninfo scaninfo;
685 * Check if the mount point has any quotas.
686 * If not, simply return.
688 for (i = 0; i < MAXQUOTAS; i++)
689 if (ump->um_quotas[i] != NULLVP)
694 * Search vnodes associated with this mount point,
695 * synchronizing any modified ufs_dquot structures.
698 while (scaninfo.rescan) {
700 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
701 NULL, ufs_qsync_scan, &scaninfo);
707 ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data)
709 /*struct scaninfo *info = data;*/
710 struct ufs_dquot *dq;
714 for (i = 0; i < MAXQUOTAS; i++) {
715 dq = VTOI(vp)->i_dquot[i];
716 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
723 * Code pertaining to management of the in-core dquot data structures.
725 #define DQHASH(dqvp, id) \
726 (&ufs_dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & ufs_dqhash])
727 static LIST_HEAD(ufs_dqhash, ufs_dquot) *ufs_dqhashtbl;
728 static u_long ufs_dqhash;
733 #define DQUOTINC 5 /* minimum free dquots desired */
734 static TAILQ_HEAD(ufs_dqfreelist, ufs_dquot) ufs_dqfreelist;
735 static long ufs_numdquot, ufs_desireddquot = DQUOTINC;
738 * Initialize the quota system.
743 ufs_dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &ufs_dqhash);
744 TAILQ_INIT(&ufs_dqfreelist);
748 * Obtain a dquot structure for the specified identifier and quota file
749 * reading the information from the file if necessary.
752 ufs_dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
753 struct ufs_dquot **dqp)
755 struct ufs_dquot *dq;
756 struct ufs_dqhash *dqh;
762 dqvp = ump->um_quotas[type];
763 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
768 * Check the cache first.
770 dqh = DQHASH(dqvp, id);
771 LIST_FOREACH(dq, dqh, dq_hash) {
772 if (dq->dq_id != id ||
773 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
776 * Cache hit with no references. Take
777 * the structure off the free list.
780 TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
786 * Not in cache, allocate a new one.
788 if (TAILQ_EMPTY(&ufs_dqfreelist) && ufs_numdquot < MAXQUOTAS * desiredvnodes)
789 ufs_desireddquot += DQUOTINC;
790 if (ufs_numdquot < ufs_desireddquot) {
791 dq = (struct ufs_dquot *)
792 kmalloc(sizeof *dq, M_DQUOT, M_WAITOK | M_ZERO);
795 if ((dq = TAILQ_FIRST(&ufs_dqfreelist)) == NULL) {
800 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
801 panic("dqget: free dquot isn't");
802 TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
803 if (dq->dq_ump != NULL)
804 LIST_REMOVE(dq, dq_hash);
807 * Initialize the contents of the dquot structure.
810 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
811 LIST_INSERT_HEAD(dqh, dq, dq_hash);
813 dq->dq_flags = DQ_LOCK;
817 auio.uio_iov = &aiov;
819 aiov.iov_base = (caddr_t)&dq->dq_dqb;
820 aiov.iov_len = sizeof (struct ufs_dqblk);
821 auio.uio_resid = sizeof (struct ufs_dqblk);
822 auio.uio_offset = (off_t)(id * sizeof (struct ufs_dqblk));
823 auio.uio_segflg = UIO_SYSSPACE;
824 auio.uio_rw = UIO_READ;
826 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
827 if (auio.uio_resid == sizeof(struct ufs_dqblk) && error == 0)
828 bzero((caddr_t)&dq->dq_dqb, sizeof(struct ufs_dqblk));
831 if (dq->dq_flags & DQ_WANT)
835 * I/O error in reading quota file, release
836 * quota structure and reflect problem to caller.
839 LIST_REMOVE(dq, dq_hash);
845 * Check for no limit to enforce.
846 * Initialize time values if necessary.
848 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
849 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
850 dq->dq_flags |= DQ_FAKE;
851 if (dq->dq_id != 0) {
852 if (dq->dq_btime == 0)
853 dq->dq_btime = time_second + ump->um_btime[type];
854 if (dq->dq_itime == 0)
855 dq->dq_itime = time_second + ump->um_itime[type];
863 * Obtain a reference to a dquot.
866 ufs_dqref(struct ufs_dquot *dq)
873 * Release a reference to a dquot.
876 ufs_dqrele(struct vnode *vp, struct ufs_dquot *dq)
880 if (dq->dq_cnt > 1) {
884 if (dq->dq_flags & DQ_MOD)
885 (void)ufs_dqsync(vp, dq);
886 if (--dq->dq_cnt > 0)
888 TAILQ_INSERT_TAIL(&ufs_dqfreelist, dq, dq_freelist);
892 * Update the disk quota in the quota file.
895 ufs_dqsync(struct vnode *vp, struct ufs_dquot *dq)
903 panic("dqsync: dquot");
904 if ((dq->dq_flags & DQ_MOD) == 0)
906 if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
907 panic("dqsync: file");
909 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
910 while (dq->dq_flags & DQ_LOCK) {
911 dq->dq_flags |= DQ_WANT;
912 (void) tsleep((caddr_t)dq, 0, "dqsync", 0);
913 if ((dq->dq_flags & DQ_MOD) == 0) {
919 dq->dq_flags |= DQ_LOCK;
920 auio.uio_iov = &aiov;
922 aiov.iov_base = (caddr_t)&dq->dq_dqb;
923 aiov.iov_len = sizeof (struct ufs_dqblk);
924 auio.uio_resid = sizeof (struct ufs_dqblk);
925 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct ufs_dqblk));
926 auio.uio_segflg = UIO_SYSSPACE;
927 auio.uio_rw = UIO_WRITE;
929 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
930 if (auio.uio_resid && error == 0)
932 if (dq->dq_flags & DQ_WANT)
934 dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
941 * Flush all entries from the cache for a particular vnode.
944 ufs_dqflush(struct vnode *vp)
946 struct ufs_dquot *dq, *nextdq;
947 struct ufs_dqhash *dqh;
950 * Move all dquot's that used to refer to this quota
951 * file off their hash chains (they will eventually
952 * fall off the head of the free list and be re-used).
954 for (dqh = &ufs_dqhashtbl[ufs_dqhash]; dqh >= ufs_dqhashtbl; dqh--) {
955 for (dq = dqh->lh_first; dq; dq = nextdq) {
956 nextdq = dq->dq_hash.le_next;
957 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
960 panic("dqflush: stray dquot");
961 LIST_REMOVE(dq, dq_hash);