Home | History | Annotate | Line # | Download | only in ufs
ufs_quota.c revision 1.6
      1 /*	$NetBSD: ufs_quota.c,v 1.6 1994/12/14 19:03:13 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Robert Elz at The University of Melbourne.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)ufs_quota.c	8.3 (Berkeley) 8/19/94
     39  */
     40 #include <sys/param.h>
     41 #include <sys/kernel.h>
     42 #include <sys/systm.h>
     43 #include <sys/namei.h>
     44 #include <sys/malloc.h>
     45 #include <sys/file.h>
     46 #include <sys/proc.h>
     47 #include <sys/vnode.h>
     48 #include <sys/mount.h>
     49 
     50 #include <ufs/ufs/quota.h>
     51 #include <ufs/ufs/inode.h>
     52 #include <ufs/ufs/ufsmount.h>
     53 #include <ufs/ufs/ufs_extern.h>
     54 
     55 /*
     56  * Quota name to error message mapping.
     57  */
     58 static char *quotatypes[] = INITQFNAMES;
     59 
     60 /*
     61  * Set up the quotas for an inode.
     62  *
     63  * This routine completely defines the semantics of quotas.
     64  * If other criterion want to be used to establish quotas, the
     65  * MAXQUOTAS value in quotas.h should be increased, and the
     66  * additional dquots set up here.
     67  */
     68 int
     69 getinoquota(ip)
     70 	register struct inode *ip;
     71 {
     72 	struct ufsmount *ump;
     73 	struct vnode *vp = ITOV(ip);
     74 	int error;
     75 
     76 	ump = VFSTOUFS(vp->v_mount);
     77 	/*
     78 	 * Set up the user quota based on file uid.
     79 	 * EINVAL means that quotas are not enabled.
     80 	 */
     81 	if (ip->i_dquot[USRQUOTA] == NODQUOT &&
     82 	    (error =
     83 		dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
     84 	    error != EINVAL)
     85 		return (error);
     86 	/*
     87 	 * Set up the group quota based on file gid.
     88 	 * EINVAL means that quotas are not enabled.
     89 	 */
     90 	if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
     91 	    (error =
     92 		dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
     93 	    error != EINVAL)
     94 		return (error);
     95 	return (0);
     96 }
     97 
     98 /*
     99  * Update disk usage, and take corrective action.
    100  */
    101 int
    102 chkdq(ip, change, cred, flags)
    103 	register struct inode *ip;
    104 	long change;
    105 	struct ucred *cred;
    106 	int flags;
    107 {
    108 	register struct dquot *dq;
    109 	register int i;
    110 	int ncurblocks, error;
    111 
    112 #ifdef DIAGNOSTIC
    113 	if ((flags & CHOWN) == 0)
    114 		chkdquot(ip);
    115 #endif
    116 	if (change == 0)
    117 		return (0);
    118 	if (change < 0) {
    119 		for (i = 0; i < MAXQUOTAS; i++) {
    120 			if ((dq = ip->i_dquot[i]) == NODQUOT)
    121 				continue;
    122 			while (dq->dq_flags & DQ_LOCK) {
    123 				dq->dq_flags |= DQ_WANT;
    124 				sleep((caddr_t)dq, PINOD+1);
    125 			}
    126 			ncurblocks = dq->dq_curblocks + change;
    127 			if (ncurblocks >= 0)
    128 				dq->dq_curblocks = ncurblocks;
    129 			else
    130 				dq->dq_curblocks = 0;
    131 			dq->dq_flags &= ~DQ_BLKS;
    132 			dq->dq_flags |= DQ_MOD;
    133 		}
    134 		return (0);
    135 	}
    136 	if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
    137 		for (i = 0; i < MAXQUOTAS; i++) {
    138 			if ((dq = ip->i_dquot[i]) == NODQUOT)
    139 				continue;
    140 			if (error = chkdqchg(ip, change, cred, i))
    141 				return (error);
    142 		}
    143 	}
    144 	for (i = 0; i < MAXQUOTAS; i++) {
    145 		if ((dq = ip->i_dquot[i]) == NODQUOT)
    146 			continue;
    147 		while (dq->dq_flags & DQ_LOCK) {
    148 			dq->dq_flags |= DQ_WANT;
    149 			sleep((caddr_t)dq, PINOD+1);
    150 		}
    151 		dq->dq_curblocks += change;
    152 		dq->dq_flags |= DQ_MOD;
    153 	}
    154 	return (0);
    155 }
    156 
    157 /*
    158  * Check for a valid change to a users allocation.
    159  * Issue an error message if appropriate.
    160  */
    161 int
    162 chkdqchg(ip, change, cred, type)
    163 	struct inode *ip;
    164 	long change;
    165 	struct ucred *cred;
    166 	int type;
    167 {
    168 	register struct dquot *dq = ip->i_dquot[type];
    169 	long ncurblocks = dq->dq_curblocks + change;
    170 
    171 	/*
    172 	 * If user would exceed their hard limit, disallow space allocation.
    173 	 */
    174 	if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
    175 		if ((dq->dq_flags & DQ_BLKS) == 0 &&
    176 		    ip->i_uid == cred->cr_uid) {
    177 			uprintf("\n%s: write failed, %s disk limit reached\n",
    178 			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    179 			    quotatypes[type]);
    180 			dq->dq_flags |= DQ_BLKS;
    181 		}
    182 		return (EDQUOT);
    183 	}
    184 	/*
    185 	 * If user is over their soft limit for too long, disallow space
    186 	 * allocation. Reset time limit as they cross their soft limit.
    187 	 */
    188 	if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
    189 		if (dq->dq_curblocks < dq->dq_bsoftlimit) {
    190 			dq->dq_btime = time.tv_sec +
    191 			    VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
    192 			if (ip->i_uid == cred->cr_uid)
    193 				uprintf("\n%s: warning, %s %s\n",
    194 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    195 				    quotatypes[type], "disk quota exceeded");
    196 			return (0);
    197 		}
    198 		if (time.tv_sec > dq->dq_btime) {
    199 			if ((dq->dq_flags & DQ_BLKS) == 0 &&
    200 			    ip->i_uid == cred->cr_uid) {
    201 				uprintf("\n%s: write failed, %s %s\n",
    202 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    203 				    quotatypes[type],
    204 				    "disk quota exceeded for too long");
    205 				dq->dq_flags |= DQ_BLKS;
    206 			}
    207 			return (EDQUOT);
    208 		}
    209 	}
    210 	return (0);
    211 }
    212 
    213 /*
    214  * Check the inode limit, applying corrective action.
    215  */
    216 int
    217 chkiq(ip, change, cred, flags)
    218 	register struct inode *ip;
    219 	long change;
    220 	struct ucred *cred;
    221 	int flags;
    222 {
    223 	register struct dquot *dq;
    224 	register int i;
    225 	int ncurinodes, error;
    226 
    227 #ifdef DIAGNOSTIC
    228 	if ((flags & CHOWN) == 0)
    229 		chkdquot(ip);
    230 #endif
    231 	if (change == 0)
    232 		return (0);
    233 	if (change < 0) {
    234 		for (i = 0; i < MAXQUOTAS; i++) {
    235 			if ((dq = ip->i_dquot[i]) == NODQUOT)
    236 				continue;
    237 			while (dq->dq_flags & DQ_LOCK) {
    238 				dq->dq_flags |= DQ_WANT;
    239 				sleep((caddr_t)dq, PINOD+1);
    240 			}
    241 			ncurinodes = dq->dq_curinodes + change;
    242 			if (ncurinodes >= 0)
    243 				dq->dq_curinodes = ncurinodes;
    244 			else
    245 				dq->dq_curinodes = 0;
    246 			dq->dq_flags &= ~DQ_INODS;
    247 			dq->dq_flags |= DQ_MOD;
    248 		}
    249 		return (0);
    250 	}
    251 	if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
    252 		for (i = 0; i < MAXQUOTAS; i++) {
    253 			if ((dq = ip->i_dquot[i]) == NODQUOT)
    254 				continue;
    255 			if (error = chkiqchg(ip, change, cred, i))
    256 				return (error);
    257 		}
    258 	}
    259 	for (i = 0; i < MAXQUOTAS; i++) {
    260 		if ((dq = ip->i_dquot[i]) == NODQUOT)
    261 			continue;
    262 		while (dq->dq_flags & DQ_LOCK) {
    263 			dq->dq_flags |= DQ_WANT;
    264 			sleep((caddr_t)dq, PINOD+1);
    265 		}
    266 		dq->dq_curinodes += change;
    267 		dq->dq_flags |= DQ_MOD;
    268 	}
    269 	return (0);
    270 }
    271 
    272 /*
    273  * Check for a valid change to a users allocation.
    274  * Issue an error message if appropriate.
    275  */
    276 int
    277 chkiqchg(ip, change, cred, type)
    278 	struct inode *ip;
    279 	long change;
    280 	struct ucred *cred;
    281 	int type;
    282 {
    283 	register struct dquot *dq = ip->i_dquot[type];
    284 	long ncurinodes = dq->dq_curinodes + change;
    285 
    286 	/*
    287 	 * If user would exceed their hard limit, disallow inode allocation.
    288 	 */
    289 	if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
    290 		if ((dq->dq_flags & DQ_INODS) == 0 &&
    291 		    ip->i_uid == cred->cr_uid) {
    292 			uprintf("\n%s: write failed, %s inode limit reached\n",
    293 			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    294 			    quotatypes[type]);
    295 			dq->dq_flags |= DQ_INODS;
    296 		}
    297 		return (EDQUOT);
    298 	}
    299 	/*
    300 	 * If user is over their soft limit for too long, disallow inode
    301 	 * allocation. Reset time limit as they cross their soft limit.
    302 	 */
    303 	if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
    304 		if (dq->dq_curinodes < dq->dq_isoftlimit) {
    305 			dq->dq_itime = time.tv_sec +
    306 			    VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
    307 			if (ip->i_uid == cred->cr_uid)
    308 				uprintf("\n%s: warning, %s %s\n",
    309 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    310 				    quotatypes[type], "inode quota exceeded");
    311 			return (0);
    312 		}
    313 		if (time.tv_sec > dq->dq_itime) {
    314 			if ((dq->dq_flags & DQ_INODS) == 0 &&
    315 			    ip->i_uid == cred->cr_uid) {
    316 				uprintf("\n%s: write failed, %s %s\n",
    317 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    318 				    quotatypes[type],
    319 				    "inode quota exceeded for too long");
    320 				dq->dq_flags |= DQ_INODS;
    321 			}
    322 			return (EDQUOT);
    323 		}
    324 	}
    325 	return (0);
    326 }
    327 
    328 #ifdef DIAGNOSTIC
    329 /*
    330  * On filesystems with quotas enabled, it is an error for a file to change
    331  * size and not to have a dquot structure associated with it.
    332  */
    333 void
    334 chkdquot(ip)
    335 	register struct inode *ip;
    336 {
    337 	struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
    338 	register int i;
    339 
    340 	for (i = 0; i < MAXQUOTAS; i++) {
    341 		if (ump->um_quotas[i] == NULLVP ||
    342 		    (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
    343 			continue;
    344 		if (ip->i_dquot[i] == NODQUOT) {
    345 			vprint("chkdquot: missing dquot", ITOV(ip));
    346 			panic("missing dquot");
    347 		}
    348 	}
    349 }
    350 #endif
    351 
    352 /*
    353  * Code to process quotactl commands.
    354  */
    355 
    356 /*
    357  * Q_QUOTAON - set up a quota file for a particular file system.
    358  */
    359 int
    360 quotaon(p, mp, type, fname)
    361 	struct proc *p;
    362 	struct mount *mp;
    363 	register int type;
    364 	caddr_t fname;
    365 {
    366 	register struct ufsmount *ump = VFSTOUFS(mp);
    367 	register struct vnode *vp, **vpp;
    368 	struct vnode *nextvp;
    369 	struct dquot *dq;
    370 	int error;
    371 	struct nameidata nd;
    372 
    373 	vpp = &ump->um_quotas[type];
    374 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, p);
    375 	if (error = vn_open(&nd, FREAD|FWRITE, 0))
    376 		return (error);
    377 	vp = nd.ni_vp;
    378 	VOP_UNLOCK(vp);
    379 	if (vp->v_type != VREG) {
    380 		(void) vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    381 		return (EACCES);
    382 	}
    383 	if (vfs_busy(mp)) {
    384 		(void) vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
    385 		return (EBUSY);
    386 	}
    387 	if (*vpp != vp)
    388 		quotaoff(p, mp, type);
    389 	ump->um_qflags[type] |= QTF_OPENING;
    390 	mp->mnt_flag |= MNT_QUOTA;
    391 	vp->v_flag |= VSYSTEM;
    392 	*vpp = vp;
    393 	/*
    394 	 * Save the credential of the process that turned on quotas.
    395 	 * Set up the time limits for this quota.
    396 	 */
    397 	crhold(p->p_ucred);
    398 	ump->um_cred[type] = p->p_ucred;
    399 	ump->um_btime[type] = MAX_DQ_TIME;
    400 	ump->um_itime[type] = MAX_IQ_TIME;
    401 	if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
    402 		if (dq->dq_btime > 0)
    403 			ump->um_btime[type] = dq->dq_btime;
    404 		if (dq->dq_itime > 0)
    405 			ump->um_itime[type] = dq->dq_itime;
    406 		dqrele(NULLVP, dq);
    407 	}
    408 	/*
    409 	 * Search vnodes associated with this mount point,
    410 	 * adding references to quota file being opened.
    411 	 * NB: only need to add dquot's for inodes being modified.
    412 	 */
    413 again:
    414 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
    415 		nextvp = vp->v_mntvnodes.le_next;
    416 		if (vp->v_writecount == 0)
    417 			continue;
    418 		if (vget(vp, 1))
    419 			goto again;
    420 		if (error = getinoquota(VTOI(vp))) {
    421 			vput(vp);
    422 			break;
    423 		}
    424 		vput(vp);
    425 		if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
    426 			goto again;
    427 	}
    428 	ump->um_qflags[type] &= ~QTF_OPENING;
    429 	if (error)
    430 		quotaoff(p, mp, type);
    431 	vfs_unbusy(mp);
    432 	return (error);
    433 }
    434 
    435 /*
    436  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
    437  */
    438 int
    439 quotaoff(p, mp, type)
    440 	struct proc *p;
    441 	struct mount *mp;
    442 	register int type;
    443 {
    444 	register struct vnode *vp;
    445 	struct vnode *qvp, *nextvp;
    446 	struct ufsmount *ump = VFSTOUFS(mp);
    447 	register struct dquot *dq;
    448 	register struct inode *ip;
    449 	int error;
    450 
    451 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
    452 		panic("quotaoff: not busy");
    453 	if ((qvp = ump->um_quotas[type]) == NULLVP)
    454 		return (0);
    455 	ump->um_qflags[type] |= QTF_CLOSING;
    456 	/*
    457 	 * Search vnodes associated with this mount point,
    458 	 * deleting any references to quota file being closed.
    459 	 */
    460 again:
    461 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
    462 		nextvp = vp->v_mntvnodes.le_next;
    463 		if (vget(vp, 1))
    464 			goto again;
    465 		ip = VTOI(vp);
    466 		dq = ip->i_dquot[type];
    467 		ip->i_dquot[type] = NODQUOT;
    468 		dqrele(vp, dq);
    469 		vput(vp);
    470 		if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
    471 			goto again;
    472 	}
    473 	dqflush(qvp);
    474 	qvp->v_flag &= ~VSYSTEM;
    475 	error = vn_close(qvp, FREAD|FWRITE, p->p_ucred, p);
    476 	ump->um_quotas[type] = NULLVP;
    477 	crfree(ump->um_cred[type]);
    478 	ump->um_cred[type] = NOCRED;
    479 	ump->um_qflags[type] &= ~QTF_CLOSING;
    480 	for (type = 0; type < MAXQUOTAS; type++)
    481 		if (ump->um_quotas[type] != NULLVP)
    482 			break;
    483 	if (type == MAXQUOTAS)
    484 		mp->mnt_flag &= ~MNT_QUOTA;
    485 	return (error);
    486 }
    487 
    488 /*
    489  * Q_GETQUOTA - return current values in a dqblk structure.
    490  */
    491 int
    492 getquota(mp, id, type, addr)
    493 	struct mount *mp;
    494 	u_long id;
    495 	int type;
    496 	caddr_t addr;
    497 {
    498 	struct dquot *dq;
    499 	int error;
    500 
    501 	if (error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq))
    502 		return (error);
    503 	error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
    504 	dqrele(NULLVP, dq);
    505 	return (error);
    506 }
    507 
    508 /*
    509  * Q_SETQUOTA - assign an entire dqblk structure.
    510  */
    511 int
    512 setquota(mp, id, type, addr)
    513 	struct mount *mp;
    514 	u_long id;
    515 	int type;
    516 	caddr_t addr;
    517 {
    518 	register struct dquot *dq;
    519 	struct dquot *ndq;
    520 	struct ufsmount *ump = VFSTOUFS(mp);
    521 	struct dqblk newlim;
    522 	int error;
    523 
    524 	if (error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk)))
    525 		return (error);
    526 	if (error = dqget(NULLVP, id, ump, type, &ndq))
    527 		return (error);
    528 	dq = ndq;
    529 	while (dq->dq_flags & DQ_LOCK) {
    530 		dq->dq_flags |= DQ_WANT;
    531 		sleep((caddr_t)dq, PINOD+1);
    532 	}
    533 	/*
    534 	 * Copy all but the current values.
    535 	 * Reset time limit if previously had no soft limit or were
    536 	 * under it, but now have a soft limit and are over it.
    537 	 */
    538 	newlim.dqb_curblocks = dq->dq_curblocks;
    539 	newlim.dqb_curinodes = dq->dq_curinodes;
    540 	if (dq->dq_id != 0) {
    541 		newlim.dqb_btime = dq->dq_btime;
    542 		newlim.dqb_itime = dq->dq_itime;
    543 	}
    544 	if (newlim.dqb_bsoftlimit &&
    545 	    dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
    546 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
    547 		newlim.dqb_btime = time.tv_sec + ump->um_btime[type];
    548 	if (newlim.dqb_isoftlimit &&
    549 	    dq->dq_curinodes >= newlim.dqb_isoftlimit &&
    550 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
    551 		newlim.dqb_itime = time.tv_sec + ump->um_itime[type];
    552 	dq->dq_dqb = newlim;
    553 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    554 		dq->dq_flags &= ~DQ_BLKS;
    555 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    556 		dq->dq_flags &= ~DQ_INODS;
    557 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    558 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    559 		dq->dq_flags |= DQ_FAKE;
    560 	else
    561 		dq->dq_flags &= ~DQ_FAKE;
    562 	dq->dq_flags |= DQ_MOD;
    563 	dqrele(NULLVP, dq);
    564 	return (0);
    565 }
    566 
    567 /*
    568  * Q_SETUSE - set current inode and block usage.
    569  */
    570 int
    571 setuse(mp, id, type, addr)
    572 	struct mount *mp;
    573 	u_long id;
    574 	int type;
    575 	caddr_t addr;
    576 {
    577 	register struct dquot *dq;
    578 	struct ufsmount *ump = VFSTOUFS(mp);
    579 	struct dquot *ndq;
    580 	struct dqblk usage;
    581 	int error;
    582 
    583 	if (error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk)))
    584 		return (error);
    585 	if (error = dqget(NULLVP, id, ump, type, &ndq))
    586 		return (error);
    587 	dq = ndq;
    588 	while (dq->dq_flags & DQ_LOCK) {
    589 		dq->dq_flags |= DQ_WANT;
    590 		sleep((caddr_t)dq, PINOD+1);
    591 	}
    592 	/*
    593 	 * Reset time limit if have a soft limit and were
    594 	 * previously under it, but are now over it.
    595 	 */
    596 	if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
    597 	    usage.dqb_curblocks >= dq->dq_bsoftlimit)
    598 		dq->dq_btime = time.tv_sec + ump->um_btime[type];
    599 	if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
    600 	    usage.dqb_curinodes >= dq->dq_isoftlimit)
    601 		dq->dq_itime = time.tv_sec + ump->um_itime[type];
    602 	dq->dq_curblocks = usage.dqb_curblocks;
    603 	dq->dq_curinodes = usage.dqb_curinodes;
    604 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    605 		dq->dq_flags &= ~DQ_BLKS;
    606 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    607 		dq->dq_flags &= ~DQ_INODS;
    608 	dq->dq_flags |= DQ_MOD;
    609 	dqrele(NULLVP, dq);
    610 	return (0);
    611 }
    612 
    613 /*
    614  * Q_SYNC - sync quota files to disk.
    615  */
    616 int
    617 qsync(mp)
    618 	struct mount *mp;
    619 {
    620 	struct ufsmount *ump = VFSTOUFS(mp);
    621 	register struct vnode *vp, *nextvp;
    622 	register struct dquot *dq;
    623 	register int i;
    624 
    625 	/*
    626 	 * Check if the mount point has any quotas.
    627 	 * If not, simply return.
    628 	 */
    629 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
    630 		panic("qsync: not busy");
    631 	for (i = 0; i < MAXQUOTAS; i++)
    632 		if (ump->um_quotas[i] != NULLVP)
    633 			break;
    634 	if (i == MAXQUOTAS)
    635 		return (0);
    636 	/*
    637 	 * Search vnodes associated with this mount point,
    638 	 * synchronizing any modified dquot structures.
    639 	 */
    640 again:
    641 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
    642 		nextvp = vp->v_mntvnodes.le_next;
    643 		if (VOP_ISLOCKED(vp))
    644 			continue;
    645 		if (vget(vp, 1))
    646 			goto again;
    647 		for (i = 0; i < MAXQUOTAS; i++) {
    648 			dq = VTOI(vp)->i_dquot[i];
    649 			if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
    650 				dqsync(vp, dq);
    651 		}
    652 		vput(vp);
    653 		if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
    654 			goto again;
    655 	}
    656 	return (0);
    657 }
    658 
    659 /*
    660  * Code pertaining to management of the in-core dquot data structures.
    661  */
    662 #define DQHASH(dqvp, id) \
    663 	(&dqhashtbl[((((int)(dqvp)) >> 8) + id) & dqhash])
    664 LIST_HEAD(dqhash, dquot) *dqhashtbl;
    665 u_long dqhash;
    666 
    667 /*
    668  * Dquot free list.
    669  */
    670 #define	DQUOTINC	5	/* minimum free dquots desired */
    671 TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
    672 long numdquot, desireddquot = DQUOTINC;
    673 
    674 /*
    675  * Initialize the quota system.
    676  */
    677 void
    678 dqinit()
    679 {
    680 
    681 	dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
    682 	TAILQ_INIT(&dqfreelist);
    683 }
    684 
    685 /*
    686  * Obtain a dquot structure for the specified identifier and quota file
    687  * reading the information from the file if necessary.
    688  */
    689 int
    690 dqget(vp, id, ump, type, dqp)
    691 	struct vnode *vp;
    692 	u_long id;
    693 	register struct ufsmount *ump;
    694 	register int type;
    695 	struct dquot **dqp;
    696 {
    697 	register struct dquot *dq;
    698 	struct dqhash *dqh;
    699 	register struct vnode *dqvp;
    700 	struct iovec aiov;
    701 	struct uio auio;
    702 	int error;
    703 
    704 	dqvp = ump->um_quotas[type];
    705 	if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
    706 		*dqp = NODQUOT;
    707 		return (EINVAL);
    708 	}
    709 	/*
    710 	 * Check the cache first.
    711 	 */
    712 	dqh = DQHASH(dqvp, id);
    713 	for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
    714 		if (dq->dq_id != id ||
    715 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    716 			continue;
    717 		/*
    718 		 * Cache hit with no references.  Take
    719 		 * the structure off the free list.
    720 		 */
    721 		if (dq->dq_cnt == 0)
    722 			TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
    723 		DQREF(dq);
    724 		*dqp = dq;
    725 		return (0);
    726 	}
    727 	/*
    728 	 * Not in cache, allocate a new one.
    729 	 */
    730 	if (dqfreelist.tqh_first == NODQUOT &&
    731 	    numdquot < MAXQUOTAS * desiredvnodes)
    732 		desireddquot += DQUOTINC;
    733 	if (numdquot < desireddquot) {
    734 		dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
    735 		bzero((char *)dq, sizeof *dq);
    736 		numdquot++;
    737 	} else {
    738 		if ((dq = dqfreelist.tqh_first) == NULL) {
    739 			tablefull("dquot");
    740 			*dqp = NODQUOT;
    741 			return (EUSERS);
    742 		}
    743 		if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
    744 			panic("free dquot isn't");
    745 		TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
    746 		LIST_REMOVE(dq, dq_hash);
    747 	}
    748 	/*
    749 	 * Initialize the contents of the dquot structure.
    750 	 */
    751 	if (vp != dqvp)
    752 		VOP_LOCK(dqvp);
    753 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
    754 	DQREF(dq);
    755 	dq->dq_flags = DQ_LOCK;
    756 	dq->dq_id = id;
    757 	dq->dq_ump = ump;
    758 	dq->dq_type = type;
    759 	auio.uio_iov = &aiov;
    760 	auio.uio_iovcnt = 1;
    761 	aiov.iov_base = (caddr_t)&dq->dq_dqb;
    762 	aiov.iov_len = sizeof (struct dqblk);
    763 	auio.uio_resid = sizeof (struct dqblk);
    764 	auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
    765 	auio.uio_segflg = UIO_SYSSPACE;
    766 	auio.uio_rw = UIO_READ;
    767 	auio.uio_procp = (struct proc *)0;
    768 	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
    769 	if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
    770 		bzero((caddr_t)&dq->dq_dqb, sizeof(struct dqblk));
    771 	if (vp != dqvp)
    772 		VOP_UNLOCK(dqvp);
    773 	if (dq->dq_flags & DQ_WANT)
    774 		wakeup((caddr_t)dq);
    775 	dq->dq_flags = 0;
    776 	/*
    777 	 * I/O error in reading quota file, release
    778 	 * quota structure and reflect problem to caller.
    779 	 */
    780 	if (error) {
    781 		LIST_REMOVE(dq, dq_hash);
    782 		dqrele(vp, dq);
    783 		*dqp = NODQUOT;
    784 		return (error);
    785 	}
    786 	/*
    787 	 * Check for no limit to enforce.
    788 	 * Initialize time values if necessary.
    789 	 */
    790 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    791 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    792 		dq->dq_flags |= DQ_FAKE;
    793 	if (dq->dq_id != 0) {
    794 		if (dq->dq_btime == 0)
    795 			dq->dq_btime = time.tv_sec + ump->um_btime[type];
    796 		if (dq->dq_itime == 0)
    797 			dq->dq_itime = time.tv_sec + ump->um_itime[type];
    798 	}
    799 	*dqp = dq;
    800 	return (0);
    801 }
    802 
    803 /*
    804  * Obtain a reference to a dquot.
    805  */
    806 void
    807 dqref(dq)
    808 	struct dquot *dq;
    809 {
    810 
    811 	dq->dq_cnt++;
    812 }
    813 
    814 /*
    815  * Release a reference to a dquot.
    816  */
    817 void
    818 dqrele(vp, dq)
    819 	struct vnode *vp;
    820 	register struct dquot *dq;
    821 {
    822 
    823 	if (dq == NODQUOT)
    824 		return;
    825 	if (dq->dq_cnt > 1) {
    826 		dq->dq_cnt--;
    827 		return;
    828 	}
    829 	if (dq->dq_flags & DQ_MOD)
    830 		(void) dqsync(vp, dq);
    831 	if (--dq->dq_cnt > 0)
    832 		return;
    833 	TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
    834 }
    835 
    836 /*
    837  * Update the disk quota in the quota file.
    838  */
    839 int
    840 dqsync(vp, dq)
    841 	struct vnode *vp;
    842 	register struct dquot *dq;
    843 {
    844 	struct vnode *dqvp;
    845 	struct iovec aiov;
    846 	struct uio auio;
    847 	int error;
    848 
    849 	if (dq == NODQUOT)
    850 		panic("dqsync: dquot");
    851 	if ((dq->dq_flags & DQ_MOD) == 0)
    852 		return (0);
    853 	if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
    854 		panic("dqsync: file");
    855 	if (vp != dqvp)
    856 		VOP_LOCK(dqvp);
    857 	while (dq->dq_flags & DQ_LOCK) {
    858 		dq->dq_flags |= DQ_WANT;
    859 		sleep((caddr_t)dq, PINOD+2);
    860 		if ((dq->dq_flags & DQ_MOD) == 0) {
    861 			if (vp != dqvp)
    862 				VOP_UNLOCK(dqvp);
    863 			return (0);
    864 		}
    865 	}
    866 	dq->dq_flags |= DQ_LOCK;
    867 	auio.uio_iov = &aiov;
    868 	auio.uio_iovcnt = 1;
    869 	aiov.iov_base = (caddr_t)&dq->dq_dqb;
    870 	aiov.iov_len = sizeof (struct dqblk);
    871 	auio.uio_resid = sizeof (struct dqblk);
    872 	auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
    873 	auio.uio_segflg = UIO_SYSSPACE;
    874 	auio.uio_rw = UIO_WRITE;
    875 	auio.uio_procp = (struct proc *)0;
    876 	error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
    877 	if (auio.uio_resid && error == 0)
    878 		error = EIO;
    879 	if (dq->dq_flags & DQ_WANT)
    880 		wakeup((caddr_t)dq);
    881 	dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
    882 	if (vp != dqvp)
    883 		VOP_UNLOCK(dqvp);
    884 	return (error);
    885 }
    886 
    887 /*
    888  * Flush all entries from the cache for a particular vnode.
    889  */
    890 void
    891 dqflush(vp)
    892 	register struct vnode *vp;
    893 {
    894 	register struct dquot *dq, *nextdq;
    895 	struct dqhash *dqh;
    896 
    897 	/*
    898 	 * Move all dquot's that used to refer to this quota
    899 	 * file off their hash chains (they will eventually
    900 	 * fall off the head of the free list and be re-used).
    901 	 */
    902 	for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
    903 		for (dq = dqh->lh_first; dq; dq = nextdq) {
    904 			nextdq = dq->dq_hash.le_next;
    905 			if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
    906 				continue;
    907 			if (dq->dq_cnt)
    908 				panic("dqflush: stray dquot");
    909 			LIST_REMOVE(dq, dq_hash);
    910 			dq->dq_ump = (struct ufsmount *)0;
    911 		}
    912 	}
    913 }
    914