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.24 2006/09/05 00:55:51 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/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <vm/vm_zone.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 ufs_chkdqchg (struct inode *, long, struct ucred *, int);
64 static int ufs_chkiqchg (struct inode *, long, struct ucred *, int);
65 static int ufs_dqget (struct vnode *,
66 u_long, struct ufsmount *, int, struct ufs_dquot **);
67 static int ufs_dqsync (struct vnode *, struct ufs_dquot *);
68 static void ufs_dqflush (struct vnode *);
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 while (dq->dq_flags & DQ_LOCK) {
131 dq->dq_flags |= DQ_WANT;
132 (void) tsleep((caddr_t)dq, 0, "chkdq1", 0);
134 ncurblocks = dq->dq_curblocks + change;
136 dq->dq_curblocks = ncurblocks;
138 dq->dq_curblocks = 0;
139 dq->dq_flags &= ~DQ_BLKS;
140 dq->dq_flags |= DQ_MOD;
144 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
145 for (i = 0; i < MAXQUOTAS; i++) {
146 if ((dq = ip->i_dquot[i]) == NODQUOT)
148 error = ufs_chkdqchg(ip, change, cred, i);
153 for (i = 0; i < MAXQUOTAS; i++) {
154 if ((dq = ip->i_dquot[i]) == NODQUOT)
156 while (dq->dq_flags & DQ_LOCK) {
157 dq->dq_flags |= DQ_WANT;
158 (void) tsleep((caddr_t)dq, 0, "chkdq2", 0);
160 /* Reset timer when crossing soft limit */
161 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
162 dq->dq_curblocks < dq->dq_bsoftlimit)
163 dq->dq_btime = time_second +
164 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[i];
165 dq->dq_curblocks += change;
166 dq->dq_flags |= DQ_MOD;
172 * Check for a valid change to a users allocation.
173 * Issue an error message if appropriate.
176 ufs_chkdqchg(struct inode *ip, long change, struct ucred *cred, int type)
178 struct ufs_dquot *dq = ip->i_dquot[type];
179 long ncurblocks = dq->dq_curblocks + change;
182 * If user would exceed their hard limit, disallow space allocation.
184 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
185 if ((dq->dq_flags & DQ_BLKS) == 0 &&
186 ip->i_uid == cred->cr_uid) {
187 uprintf("\n%s: write failed, %s disk limit reached\n",
188 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
190 dq->dq_flags |= DQ_BLKS;
195 * If user is over their soft limit for too long, disallow space
196 * allocation. Reset time limit as they cross their soft limit.
198 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
199 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
200 dq->dq_btime = time_second +
201 VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
202 if (ip->i_uid == cred->cr_uid)
203 uprintf("\n%s: warning, %s %s\n",
204 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
205 quotatypes[type], "disk quota exceeded");
208 if (time_second > dq->dq_btime) {
209 if ((dq->dq_flags & DQ_BLKS) == 0 &&
210 ip->i_uid == cred->cr_uid) {
211 uprintf("\n%s: write failed, %s %s\n",
212 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
214 "disk quota exceeded for too long");
215 dq->dq_flags |= DQ_BLKS;
224 * Check the inode limit, applying corrective action.
227 ufs_chkiq(struct inode *ip, long change, struct ucred *cred, int flags)
229 struct ufs_dquot *dq;
231 int ncurinodes, error;
234 if ((flags & CHOWN) == 0)
240 for (i = 0; i < MAXQUOTAS; i++) {
241 if ((dq = ip->i_dquot[i]) == NODQUOT)
243 while (dq->dq_flags & DQ_LOCK) {
244 dq->dq_flags |= DQ_WANT;
245 (void) tsleep((caddr_t)dq, 0, "chkiq1", 0);
247 ncurinodes = dq->dq_curinodes + change;
249 dq->dq_curinodes = ncurinodes;
251 dq->dq_curinodes = 0;
252 dq->dq_flags &= ~DQ_INODS;
253 dq->dq_flags |= DQ_MOD;
257 if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
258 for (i = 0; i < MAXQUOTAS; i++) {
259 if ((dq = ip->i_dquot[i]) == NODQUOT)
261 error = ufs_chkiqchg(ip, change, cred, i);
266 for (i = 0; i < MAXQUOTAS; i++) {
267 if ((dq = ip->i_dquot[i]) == NODQUOT)
269 while (dq->dq_flags & DQ_LOCK) {
270 dq->dq_flags |= DQ_WANT;
271 (void) tsleep((caddr_t)dq, 0, "chkiq2", 0);
273 /* Reset timer when crossing soft limit */
274 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
275 dq->dq_curinodes < dq->dq_isoftlimit)
276 dq->dq_itime = time_second +
277 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[i];
278 dq->dq_curinodes += change;
279 dq->dq_flags |= DQ_MOD;
285 * Check for a valid change to a users allocation.
286 * Issue an error message if appropriate.
289 ufs_chkiqchg(struct inode *ip, long change, struct ucred *cred, int type)
291 struct ufs_dquot *dq = ip->i_dquot[type];
292 long ncurinodes = dq->dq_curinodes + change;
295 * If user would exceed their hard limit, disallow inode allocation.
297 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
298 if ((dq->dq_flags & DQ_INODS) == 0 &&
299 ip->i_uid == cred->cr_uid) {
300 uprintf("\n%s: write failed, %s inode limit reached\n",
301 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
303 dq->dq_flags |= DQ_INODS;
308 * If user is over their soft limit for too long, disallow inode
309 * allocation. Reset time limit as they cross their soft limit.
311 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
312 if (dq->dq_curinodes < dq->dq_isoftlimit) {
313 dq->dq_itime = time_second +
314 VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
315 if (ip->i_uid == cred->cr_uid)
316 uprintf("\n%s: warning, %s %s\n",
317 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
318 quotatypes[type], "inode quota exceeded");
321 if (time_second > dq->dq_itime) {
322 if ((dq->dq_flags & DQ_INODS) == 0 &&
323 ip->i_uid == cred->cr_uid) {
324 uprintf("\n%s: write failed, %s %s\n",
325 ITOV(ip)->v_mount->mnt_stat.f_mntfromname,
327 "inode quota exceeded for too long");
328 dq->dq_flags |= DQ_INODS;
338 * On filesystems with quotas enabled, it is an error for a file to change
339 * size and not to have a dquot structure associated with it.
342 ufs_chkdquot(struct inode *ip)
344 struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
347 for (i = 0; i < MAXQUOTAS; i++) {
348 if (ump->um_quotas[i] == NULLVP ||
349 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
351 if (ip->i_dquot[i] == NODQUOT) {
352 vprint("chkdquot: missing dquot", ITOV(ip));
353 panic("chkdquot: missing dquot");
360 * Code to process quotactl commands.
369 * Q_QUOTAON - set up a quota file for a particular filesystem.
371 static int ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data);
374 ufs_quotaon(struct ucred *cred, struct mount *mp, int type, caddr_t fname)
376 struct ufsmount *ump = VFSTOUFS(mp);
377 struct vnode *vp, **vpp;
378 struct ufs_dquot *dq;
380 struct nlookupdata nd;
381 struct scaninfo scaninfo;
383 vpp = &ump->um_quotas[type];
384 error = nlookup_init(&nd, fname, UIO_USERSPACE, NLC_FOLLOW|NLC_LOCKVP);
386 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
387 if (error == 0 && nd.nl_open_vp->v_type != VREG)
394 nd.nl_open_vp = NULL;
399 ufs_quotaoff(mp, type);
400 ump->um_qflags[type] |= QTF_OPENING;
401 mp->mnt_flag |= MNT_QUOTA;
402 vp->v_flag |= VSYSTEM;
404 /* XXX release duplicate vp if *vpp == vp? */
406 * Save the credential of the process that turned on quotas.
407 * Set up the time limits for this quota.
409 ump->um_cred[type] = crhold(cred);
410 ump->um_btime[type] = MAX_DQ_TIME;
411 ump->um_itime[type] = MAX_IQ_TIME;
412 if (ufs_dqget(NULLVP, 0, ump, type, &dq) == 0) {
413 if (dq->dq_btime > 0)
414 ump->um_btime[type] = dq->dq_btime;
415 if (dq->dq_itime > 0)
416 ump->um_itime[type] = dq->dq_itime;
417 ufs_dqrele(NULLVP, dq);
420 * Search vnodes associated with this mount point,
421 * adding references to quota file being opened.
422 * NB: only need to add dquot's for inodes being modified.
425 while (scaninfo.rescan) {
427 error = vmntvnodescan(mp, VMSC_GETVP,
428 NULL, ufs_quotaon_scan, &scaninfo);
432 ump->um_qflags[type] &= ~QTF_OPENING;
434 ufs_quotaoff(mp, type);
439 ufs_quotaon_scan(struct mount *mp, struct vnode *vp, void *data)
442 /*struct scaninfo *info = data;*/
444 if (vp->v_writecount == 0)
446 error = ufs_getinoquota(VTOI(vp));
451 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
454 static int ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data);
457 ufs_quotaoff(struct mount *mp, int type)
460 struct ufsmount *ump = VFSTOUFS(mp);
462 struct scaninfo scaninfo;
464 if ((qvp = ump->um_quotas[type]) == NULLVP)
466 ump->um_qflags[type] |= QTF_CLOSING;
469 * Search vnodes associated with this mount point,
470 * deleting any references to quota file being closed.
473 scaninfo.type = type;
474 while (scaninfo.rescan) {
476 vmntvnodescan(mp, VMSC_GETVP, NULL, ufs_quotaoff_scan, &scaninfo);
479 qvp->v_flag &= ~VSYSTEM;
480 error = vn_close(qvp, FREAD|FWRITE);
481 ump->um_quotas[type] = NULLVP;
482 crfree(ump->um_cred[type]);
483 ump->um_cred[type] = NOCRED;
484 ump->um_qflags[type] &= ~QTF_CLOSING;
485 for (type = 0; type < MAXQUOTAS; type++) {
486 if (ump->um_quotas[type] != NULLVP)
489 if (type == MAXQUOTAS)
490 mp->mnt_flag &= ~MNT_QUOTA;
495 ufs_quotaoff_scan(struct mount *mp, struct vnode *vp, void *data)
497 struct scaninfo *info = data;
498 struct ufs_dquot *dq;
501 if (vp->v_type == VNON) {
505 dq = ip->i_dquot[info->type];
506 ip->i_dquot[info->type] = NODQUOT;
512 * Q_GETQUOTA - return current values in a dqblk structure.
515 ufs_getquota(struct mount *mp, u_long id, int type, caddr_t addr)
517 struct ufs_dquot *dq;
520 error = ufs_dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
523 error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct ufs_dqblk));
524 ufs_dqrele(NULLVP, dq);
529 * Q_SETQUOTA - assign an entire dqblk structure.
532 ufs_setquota(struct mount *mp, u_long id, int type, caddr_t addr)
534 struct ufs_dquot *dq;
535 struct ufs_dquot *ndq;
536 struct ufsmount *ump = VFSTOUFS(mp);
537 struct ufs_dqblk newlim;
540 error = copyin(addr, (caddr_t)&newlim, sizeof (struct ufs_dqblk));
543 error = ufs_dqget(NULLVP, id, ump, type, &ndq);
547 while (dq->dq_flags & DQ_LOCK) {
548 dq->dq_flags |= DQ_WANT;
549 (void) tsleep((caddr_t)dq, 0, "setqta", 0);
552 * Copy all but the current values.
553 * Reset time limit if previously had no soft limit or were
554 * under it, but now have a soft limit and are over it.
556 newlim.dqb_curblocks = dq->dq_curblocks;
557 newlim.dqb_curinodes = dq->dq_curinodes;
558 if (dq->dq_id != 0) {
559 newlim.dqb_btime = dq->dq_btime;
560 newlim.dqb_itime = dq->dq_itime;
562 if (newlim.dqb_bsoftlimit &&
563 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
564 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
565 newlim.dqb_btime = time_second + ump->um_btime[type];
566 if (newlim.dqb_isoftlimit &&
567 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
568 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
569 newlim.dqb_itime = time_second + ump->um_itime[type];
571 if (dq->dq_curblocks < dq->dq_bsoftlimit)
572 dq->dq_flags &= ~DQ_BLKS;
573 if (dq->dq_curinodes < dq->dq_isoftlimit)
574 dq->dq_flags &= ~DQ_INODS;
575 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
576 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
577 dq->dq_flags |= DQ_FAKE;
579 dq->dq_flags &= ~DQ_FAKE;
580 dq->dq_flags |= DQ_MOD;
581 ufs_dqrele(NULLVP, dq);
586 * Q_SETUSE - set current inode and block usage.
589 ufs_setuse(struct mount *mp, u_long id, int type, caddr_t addr)
591 struct ufs_dquot *dq;
592 struct ufsmount *ump = VFSTOUFS(mp);
593 struct ufs_dquot *ndq;
594 struct ufs_dqblk usage;
597 error = copyin(addr, (caddr_t)&usage, sizeof (struct ufs_dqblk));
600 error = ufs_dqget(NULLVP, id, ump, type, &ndq);
604 while (dq->dq_flags & DQ_LOCK) {
605 dq->dq_flags |= DQ_WANT;
606 (void) tsleep((caddr_t)dq, 0, "setuse", 0);
609 * Reset time limit if have a soft limit and were
610 * previously under it, but are now over it.
612 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
613 usage.dqb_curblocks >= dq->dq_bsoftlimit)
614 dq->dq_btime = time_second + ump->um_btime[type];
615 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
616 usage.dqb_curinodes >= dq->dq_isoftlimit)
617 dq->dq_itime = time_second + ump->um_itime[type];
618 dq->dq_curblocks = usage.dqb_curblocks;
619 dq->dq_curinodes = usage.dqb_curinodes;
620 if (dq->dq_curblocks < dq->dq_bsoftlimit)
621 dq->dq_flags &= ~DQ_BLKS;
622 if (dq->dq_curinodes < dq->dq_isoftlimit)
623 dq->dq_flags &= ~DQ_INODS;
624 dq->dq_flags |= DQ_MOD;
625 ufs_dqrele(NULLVP, dq);
630 * Q_SYNC - sync quota files to disk.
633 static int ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data);
636 ufs_qsync(struct mount *mp)
638 struct ufsmount *ump = VFSTOUFS(mp);
639 struct scaninfo scaninfo;
643 * Check if the mount point has any quotas.
644 * If not, simply return.
646 for (i = 0; i < MAXQUOTAS; i++)
647 if (ump->um_quotas[i] != NULLVP)
652 * Search vnodes associated with this mount point,
653 * synchronizing any modified ufs_dquot structures.
656 while (scaninfo.rescan) {
658 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
659 NULL, ufs_qsync_scan, &scaninfo);
665 ufs_qsync_scan(struct mount *mp, struct vnode *vp, void *data)
667 /*struct scaninfo *info = data;*/
668 struct ufs_dquot *dq;
672 for (i = 0; i < MAXQUOTAS; i++) {
673 dq = VTOI(vp)->i_dquot[i];
674 if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
681 * Code pertaining to management of the in-core dquot data structures.
683 #define DQHASH(dqvp, id) \
684 (&ufs_dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & ufs_dqhash])
685 static LIST_HEAD(ufs_dqhash, ufs_dquot) *ufs_dqhashtbl;
686 static u_long ufs_dqhash;
691 #define DQUOTINC 5 /* minimum free dquots desired */
692 static TAILQ_HEAD(ufs_dqfreelist, ufs_dquot) ufs_dqfreelist;
693 static long ufs_numdquot, ufs_desireddquot = DQUOTINC;
696 * Initialize the quota system.
701 ufs_dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &ufs_dqhash);
702 TAILQ_INIT(&ufs_dqfreelist);
706 * Obtain a dquot structure for the specified identifier and quota file
707 * reading the information from the file if necessary.
710 ufs_dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
711 struct ufs_dquot **dqp)
713 struct ufs_dquot *dq;
714 struct ufs_dqhash *dqh;
720 dqvp = ump->um_quotas[type];
721 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
726 * Check the cache first.
728 dqh = DQHASH(dqvp, id);
729 for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
730 if (dq->dq_id != id ||
731 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
734 * Cache hit with no references. Take
735 * the structure off the free list.
738 TAILQ_REMOVE(&ufs_dqfreelist, dq, dq_freelist);
744 * Not in cache, allocate a new one.
746 if (TAILQ_EMPTY(&ufs_dqfreelist) && ufs_numdquot < MAXQUOTAS * desiredvnodes)
747 ufs_desireddquot += DQUOTINC;
748 if (ufs_numdquot < ufs_desireddquot) {
749 dq = (struct ufs_dquot *)kmalloc(sizeof *dq, M_DQUOT, M_WAITOK);
750 bzero((char *)dq, sizeof *dq);
753 if ((dq = TAILQ_FIRST(&ufs_dqfreelist)) == NULL) {
758 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
759 panic("dqget: free dquot isn't");
760 TAILQ_REMOVE(&ufs_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 ufs_dqblk);
779 auio.uio_resid = sizeof (struct ufs_dqblk);
780 auio.uio_offset = (off_t)(id * sizeof (struct ufs_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 ufs_dqblk) && error == 0)
786 bzero((caddr_t)&dq->dq_dqb, sizeof(struct ufs_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 ufs_dqref(struct ufs_dquot *dq)
831 * Release a reference to a dquot.
834 ufs_dqrele(struct vnode *vp, struct ufs_dquot *dq)
838 if (dq->dq_cnt > 1) {
842 if (dq->dq_flags & DQ_MOD)
843 (void)ufs_dqsync(vp, dq);
844 if (--dq->dq_cnt > 0)
846 TAILQ_INSERT_TAIL(&ufs_dqfreelist, dq, dq_freelist);
850 * Update the disk quota in the quota file.
853 ufs_dqsync(struct vnode *vp, struct ufs_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 ufs_dqblk);
882 auio.uio_resid = sizeof (struct ufs_dqblk);
883 auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct ufs_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 ufs_dqflush(struct vnode *vp)
904 struct ufs_dquot *dq, *nextdq;
905 struct ufs_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 = &ufs_dqhashtbl[ufs_dqhash]; dqh >= ufs_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);
920 dq->dq_ump = (struct ufsmount *)0;