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