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