Home | History | Annotate | Line # | Download | only in ufs
ufs_quota.c revision 1.14
      1  1.14  jdolecek /*	$NetBSD: ufs_quota.c,v 1.14 2000/03/16 18:26:49 jdolecek Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4  1.11      fvdl  * Copyright (c) 1982, 1986, 1990, 1993, 1995
      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.11      fvdl  *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
     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.9    bouyer 		dqget(vp, ip->i_ffs_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.9    bouyer 		dqget(vp, ip->i_ffs_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.8  christos 			if ((error = chkdqchg(ip, change, cred, i)) != 0)
    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.9    bouyer 		    ip->i_ffs_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.9    bouyer 			if (ip->i_ffs_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.9    bouyer 			    ip->i_ffs_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.8  christos 			if ((error = chkiqchg(ip, change, cred, i)) != 0)
    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.9    bouyer 		    ip->i_ffs_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.9    bouyer 			if (ip->i_ffs_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.9    bouyer 			    ip->i_ffs_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.11      fvdl 	struct ufsmount *ump = VFSTOUFS(mp);
    367  1.11      fvdl 	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.8  christos 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
    376   1.1   mycroft 		return (error);
    377   1.1   mycroft 	vp = nd.ni_vp;
    378  1.11      fvdl 	VOP_UNLOCK(vp, 0);
    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 (*vpp != vp)
    384   1.1   mycroft 		quotaoff(p, mp, type);
    385   1.1   mycroft 	ump->um_qflags[type] |= QTF_OPENING;
    386   1.1   mycroft 	mp->mnt_flag |= MNT_QUOTA;
    387   1.1   mycroft 	vp->v_flag |= VSYSTEM;
    388   1.1   mycroft 	*vpp = vp;
    389   1.1   mycroft 	/*
    390   1.1   mycroft 	 * Save the credential of the process that turned on quotas.
    391   1.1   mycroft 	 * Set up the time limits for this quota.
    392   1.1   mycroft 	 */
    393   1.1   mycroft 	crhold(p->p_ucred);
    394   1.1   mycroft 	ump->um_cred[type] = p->p_ucred;
    395   1.1   mycroft 	ump->um_btime[type] = MAX_DQ_TIME;
    396   1.1   mycroft 	ump->um_itime[type] = MAX_IQ_TIME;
    397   1.1   mycroft 	if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
    398   1.1   mycroft 		if (dq->dq_btime > 0)
    399   1.1   mycroft 			ump->um_btime[type] = dq->dq_btime;
    400   1.1   mycroft 		if (dq->dq_itime > 0)
    401   1.1   mycroft 			ump->um_itime[type] = dq->dq_itime;
    402   1.1   mycroft 		dqrele(NULLVP, dq);
    403   1.1   mycroft 	}
    404   1.1   mycroft 	/*
    405   1.1   mycroft 	 * Search vnodes associated with this mount point,
    406   1.1   mycroft 	 * adding references to quota file being opened.
    407   1.1   mycroft 	 * NB: only need to add dquot's for inodes being modified.
    408   1.1   mycroft 	 */
    409   1.1   mycroft again:
    410   1.1   mycroft 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
    411   1.1   mycroft 		nextvp = vp->v_mntvnodes.le_next;
    412  1.13      fvdl 		if (vp->v_type == VNON ||vp->v_writecount == 0)
    413   1.1   mycroft 			continue;
    414  1.11      fvdl 		if (vget(vp, LK_EXCLUSIVE))
    415   1.1   mycroft 			goto again;
    416   1.8  christos 		if ((error = getinoquota(VTOI(vp))) != 0) {
    417   1.1   mycroft 			vput(vp);
    418   1.1   mycroft 			break;
    419   1.1   mycroft 		}
    420   1.1   mycroft 		vput(vp);
    421   1.1   mycroft 		if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
    422   1.1   mycroft 			goto again;
    423   1.1   mycroft 	}
    424   1.1   mycroft 	ump->um_qflags[type] &= ~QTF_OPENING;
    425   1.1   mycroft 	if (error)
    426   1.1   mycroft 		quotaoff(p, mp, type);
    427   1.1   mycroft 	return (error);
    428   1.1   mycroft }
    429   1.1   mycroft 
    430   1.1   mycroft /*
    431   1.1   mycroft  * Q_QUOTAOFF - turn off disk quotas for a filesystem.
    432   1.1   mycroft  */
    433   1.1   mycroft int
    434   1.1   mycroft quotaoff(p, mp, type)
    435   1.1   mycroft 	struct proc *p;
    436   1.1   mycroft 	struct mount *mp;
    437   1.1   mycroft 	register int type;
    438   1.1   mycroft {
    439  1.11      fvdl 	struct vnode *vp;
    440   1.1   mycroft 	struct vnode *qvp, *nextvp;
    441   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    442  1.11      fvdl 	struct dquot *dq;
    443  1.11      fvdl 	struct inode *ip;
    444   1.1   mycroft 	int error;
    445   1.1   mycroft 
    446   1.1   mycroft 	if ((qvp = ump->um_quotas[type]) == NULLVP)
    447   1.1   mycroft 		return (0);
    448   1.1   mycroft 	ump->um_qflags[type] |= QTF_CLOSING;
    449   1.1   mycroft 	/*
    450   1.1   mycroft 	 * Search vnodes associated with this mount point,
    451   1.1   mycroft 	 * deleting any references to quota file being closed.
    452   1.1   mycroft 	 */
    453   1.1   mycroft again:
    454   1.1   mycroft 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
    455   1.1   mycroft 		nextvp = vp->v_mntvnodes.le_next;
    456  1.13      fvdl 		if (vp->v_type == VNON)
    457  1.13      fvdl 			continue;
    458  1.11      fvdl 		if (vget(vp, LK_EXCLUSIVE))
    459   1.1   mycroft 			goto again;
    460   1.1   mycroft 		ip = VTOI(vp);
    461   1.1   mycroft 		dq = ip->i_dquot[type];
    462   1.1   mycroft 		ip->i_dquot[type] = NODQUOT;
    463   1.1   mycroft 		dqrele(vp, dq);
    464   1.1   mycroft 		vput(vp);
    465   1.1   mycroft 		if (vp->v_mntvnodes.le_next != nextvp || vp->v_mount != mp)
    466   1.1   mycroft 			goto again;
    467   1.1   mycroft 	}
    468   1.1   mycroft 	dqflush(qvp);
    469   1.1   mycroft 	qvp->v_flag &= ~VSYSTEM;
    470   1.1   mycroft 	error = vn_close(qvp, FREAD|FWRITE, p->p_ucred, p);
    471   1.1   mycroft 	ump->um_quotas[type] = NULLVP;
    472   1.1   mycroft 	crfree(ump->um_cred[type]);
    473   1.1   mycroft 	ump->um_cred[type] = NOCRED;
    474   1.1   mycroft 	ump->um_qflags[type] &= ~QTF_CLOSING;
    475   1.1   mycroft 	for (type = 0; type < MAXQUOTAS; type++)
    476   1.1   mycroft 		if (ump->um_quotas[type] != NULLVP)
    477   1.1   mycroft 			break;
    478   1.1   mycroft 	if (type == MAXQUOTAS)
    479   1.1   mycroft 		mp->mnt_flag &= ~MNT_QUOTA;
    480   1.1   mycroft 	return (error);
    481   1.1   mycroft }
    482   1.1   mycroft 
    483   1.1   mycroft /*
    484   1.1   mycroft  * Q_GETQUOTA - return current values in a dqblk structure.
    485   1.1   mycroft  */
    486   1.1   mycroft int
    487   1.1   mycroft getquota(mp, id, type, addr)
    488   1.1   mycroft 	struct mount *mp;
    489   1.1   mycroft 	u_long id;
    490   1.1   mycroft 	int type;
    491   1.1   mycroft 	caddr_t addr;
    492   1.1   mycroft {
    493   1.1   mycroft 	struct dquot *dq;
    494   1.1   mycroft 	int error;
    495   1.1   mycroft 
    496   1.8  christos 	if ((error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq)) != 0)
    497   1.1   mycroft 		return (error);
    498   1.1   mycroft 	error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
    499   1.1   mycroft 	dqrele(NULLVP, dq);
    500   1.1   mycroft 	return (error);
    501   1.1   mycroft }
    502   1.1   mycroft 
    503   1.1   mycroft /*
    504   1.1   mycroft  * Q_SETQUOTA - assign an entire dqblk structure.
    505   1.1   mycroft  */
    506   1.1   mycroft int
    507   1.1   mycroft setquota(mp, id, type, addr)
    508   1.1   mycroft 	struct mount *mp;
    509   1.1   mycroft 	u_long id;
    510   1.1   mycroft 	int type;
    511   1.1   mycroft 	caddr_t addr;
    512   1.1   mycroft {
    513   1.1   mycroft 	register struct dquot *dq;
    514   1.1   mycroft 	struct dquot *ndq;
    515   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    516   1.1   mycroft 	struct dqblk newlim;
    517   1.1   mycroft 	int error;
    518   1.1   mycroft 
    519   1.8  christos 	error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
    520   1.8  christos 	if (error)
    521   1.1   mycroft 		return (error);
    522   1.8  christos 	if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
    523   1.1   mycroft 		return (error);
    524   1.1   mycroft 	dq = ndq;
    525   1.1   mycroft 	while (dq->dq_flags & DQ_LOCK) {
    526   1.1   mycroft 		dq->dq_flags |= DQ_WANT;
    527   1.1   mycroft 		sleep((caddr_t)dq, PINOD+1);
    528   1.1   mycroft 	}
    529   1.1   mycroft 	/*
    530   1.1   mycroft 	 * Copy all but the current values.
    531   1.1   mycroft 	 * Reset time limit if previously had no soft limit or were
    532   1.1   mycroft 	 * under it, but now have a soft limit and are over it.
    533   1.1   mycroft 	 */
    534   1.1   mycroft 	newlim.dqb_curblocks = dq->dq_curblocks;
    535   1.1   mycroft 	newlim.dqb_curinodes = dq->dq_curinodes;
    536   1.1   mycroft 	if (dq->dq_id != 0) {
    537   1.1   mycroft 		newlim.dqb_btime = dq->dq_btime;
    538   1.1   mycroft 		newlim.dqb_itime = dq->dq_itime;
    539   1.1   mycroft 	}
    540   1.1   mycroft 	if (newlim.dqb_bsoftlimit &&
    541   1.1   mycroft 	    dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
    542   1.1   mycroft 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
    543   1.1   mycroft 		newlim.dqb_btime = time.tv_sec + ump->um_btime[type];
    544   1.1   mycroft 	if (newlim.dqb_isoftlimit &&
    545   1.1   mycroft 	    dq->dq_curinodes >= newlim.dqb_isoftlimit &&
    546   1.1   mycroft 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
    547   1.1   mycroft 		newlim.dqb_itime = time.tv_sec + ump->um_itime[type];
    548   1.1   mycroft 	dq->dq_dqb = newlim;
    549   1.1   mycroft 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    550   1.1   mycroft 		dq->dq_flags &= ~DQ_BLKS;
    551   1.1   mycroft 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    552   1.1   mycroft 		dq->dq_flags &= ~DQ_INODS;
    553   1.1   mycroft 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    554   1.1   mycroft 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    555   1.1   mycroft 		dq->dq_flags |= DQ_FAKE;
    556   1.1   mycroft 	else
    557   1.1   mycroft 		dq->dq_flags &= ~DQ_FAKE;
    558   1.1   mycroft 	dq->dq_flags |= DQ_MOD;
    559   1.1   mycroft 	dqrele(NULLVP, dq);
    560   1.1   mycroft 	return (0);
    561   1.1   mycroft }
    562   1.1   mycroft 
    563   1.1   mycroft /*
    564   1.1   mycroft  * Q_SETUSE - set current inode and block usage.
    565   1.1   mycroft  */
    566   1.1   mycroft int
    567   1.1   mycroft setuse(mp, id, type, addr)
    568   1.1   mycroft 	struct mount *mp;
    569   1.1   mycroft 	u_long id;
    570   1.1   mycroft 	int type;
    571   1.1   mycroft 	caddr_t addr;
    572   1.1   mycroft {
    573   1.1   mycroft 	register struct dquot *dq;
    574   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    575   1.1   mycroft 	struct dquot *ndq;
    576   1.1   mycroft 	struct dqblk usage;
    577   1.1   mycroft 	int error;
    578   1.1   mycroft 
    579   1.8  christos 	error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
    580   1.8  christos 	if (error)
    581   1.1   mycroft 		return (error);
    582   1.8  christos 	if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
    583   1.1   mycroft 		return (error);
    584   1.1   mycroft 	dq = ndq;
    585   1.1   mycroft 	while (dq->dq_flags & DQ_LOCK) {
    586   1.1   mycroft 		dq->dq_flags |= DQ_WANT;
    587   1.1   mycroft 		sleep((caddr_t)dq, PINOD+1);
    588   1.1   mycroft 	}
    589   1.1   mycroft 	/*
    590   1.1   mycroft 	 * Reset time limit if have a soft limit and were
    591   1.1   mycroft 	 * previously under it, but are now over it.
    592   1.1   mycroft 	 */
    593   1.1   mycroft 	if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
    594   1.1   mycroft 	    usage.dqb_curblocks >= dq->dq_bsoftlimit)
    595   1.1   mycroft 		dq->dq_btime = time.tv_sec + ump->um_btime[type];
    596   1.1   mycroft 	if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
    597   1.1   mycroft 	    usage.dqb_curinodes >= dq->dq_isoftlimit)
    598   1.1   mycroft 		dq->dq_itime = time.tv_sec + ump->um_itime[type];
    599   1.1   mycroft 	dq->dq_curblocks = usage.dqb_curblocks;
    600   1.1   mycroft 	dq->dq_curinodes = usage.dqb_curinodes;
    601   1.1   mycroft 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    602   1.1   mycroft 		dq->dq_flags &= ~DQ_BLKS;
    603   1.1   mycroft 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    604   1.1   mycroft 		dq->dq_flags &= ~DQ_INODS;
    605   1.1   mycroft 	dq->dq_flags |= DQ_MOD;
    606   1.1   mycroft 	dqrele(NULLVP, dq);
    607   1.1   mycroft 	return (0);
    608   1.1   mycroft }
    609   1.1   mycroft 
    610   1.1   mycroft /*
    611   1.1   mycroft  * Q_SYNC - sync quota files to disk.
    612   1.1   mycroft  */
    613   1.1   mycroft int
    614   1.1   mycroft qsync(mp)
    615   1.1   mycroft 	struct mount *mp;
    616   1.1   mycroft {
    617   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    618  1.11      fvdl 	struct vnode *vp, *nextvp;
    619  1.11      fvdl 	struct dquot *dq;
    620  1.11      fvdl 	int i, error;
    621   1.1   mycroft 
    622   1.1   mycroft 	/*
    623   1.1   mycroft 	 * Check if the mount point has any quotas.
    624   1.1   mycroft 	 * If not, simply return.
    625   1.1   mycroft 	 */
    626   1.1   mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    627   1.1   mycroft 		if (ump->um_quotas[i] != NULLVP)
    628   1.1   mycroft 			break;
    629   1.1   mycroft 	if (i == MAXQUOTAS)
    630   1.1   mycroft 		return (0);
    631   1.1   mycroft 	/*
    632   1.1   mycroft 	 * Search vnodes associated with this mount point,
    633   1.1   mycroft 	 * synchronizing any modified dquot structures.
    634   1.1   mycroft 	 */
    635  1.11      fvdl 	simple_lock(&mntvnode_slock);
    636   1.1   mycroft again:
    637   1.1   mycroft 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nextvp) {
    638  1.11      fvdl 		if (vp->v_mount != mp)
    639  1.11      fvdl 			goto again;
    640   1.1   mycroft 		nextvp = vp->v_mntvnodes.le_next;
    641  1.13      fvdl 		if (vp->v_type == VNON)
    642  1.13      fvdl 			continue;
    643  1.11      fvdl 		simple_lock(&vp->v_interlock);
    644  1.11      fvdl 		simple_unlock(&mntvnode_slock);
    645  1.11      fvdl 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    646  1.11      fvdl 		if (error) {
    647  1.11      fvdl 			simple_lock(&mntvnode_slock);
    648  1.11      fvdl 			if (error == ENOENT)
    649  1.11      fvdl 				goto again;
    650   1.1   mycroft 			continue;
    651  1.11      fvdl 		}
    652   1.1   mycroft 		for (i = 0; i < MAXQUOTAS; i++) {
    653   1.1   mycroft 			dq = VTOI(vp)->i_dquot[i];
    654   1.1   mycroft 			if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
    655   1.1   mycroft 				dqsync(vp, dq);
    656   1.1   mycroft 		}
    657   1.1   mycroft 		vput(vp);
    658  1.11      fvdl 		simple_lock(&mntvnode_slock);
    659  1.11      fvdl 		if (vp->v_mntvnodes.le_next != nextvp)
    660   1.1   mycroft 			goto again;
    661   1.1   mycroft 	}
    662  1.11      fvdl 	simple_unlock(&mntvnode_slock);
    663   1.1   mycroft 	return (0);
    664   1.1   mycroft }
    665   1.1   mycroft 
    666   1.1   mycroft /*
    667   1.1   mycroft  * Code pertaining to management of the in-core dquot data structures.
    668   1.1   mycroft  */
    669   1.5   mycroft #define DQHASH(dqvp, id) \
    670   1.7       cgd 	(&dqhashtbl[((((long)(dqvp)) >> 8) + id) & dqhash])
    671   1.5   mycroft LIST_HEAD(dqhash, dquot) *dqhashtbl;
    672   1.1   mycroft u_long dqhash;
    673   1.1   mycroft 
    674   1.1   mycroft /*
    675   1.1   mycroft  * Dquot free list.
    676   1.1   mycroft  */
    677   1.1   mycroft #define	DQUOTINC	5	/* minimum free dquots desired */
    678   1.5   mycroft TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
    679   1.1   mycroft long numdquot, desireddquot = DQUOTINC;
    680   1.1   mycroft 
    681   1.1   mycroft /*
    682   1.1   mycroft  * Initialize the quota system.
    683   1.1   mycroft  */
    684   1.1   mycroft void
    685   1.1   mycroft dqinit()
    686   1.1   mycroft {
    687  1.10       chs 	dqhashtbl = hashinit(desiredvnodes, M_DQUOT, M_WAITOK, &dqhash);
    688   1.5   mycroft 	TAILQ_INIT(&dqfreelist);
    689  1.14  jdolecek }
    690  1.14  jdolecek 
    691  1.14  jdolecek /*
    692  1.14  jdolecek  * Free resources held by quota system.
    693  1.14  jdolecek  */
    694  1.14  jdolecek void
    695  1.14  jdolecek dqdone()
    696  1.14  jdolecek {
    697  1.14  jdolecek 	hashdone(dqhashtbl, M_DQUOT);
    698   1.1   mycroft }
    699   1.1   mycroft 
    700   1.1   mycroft /*
    701   1.1   mycroft  * Obtain a dquot structure for the specified identifier and quota file
    702   1.1   mycroft  * reading the information from the file if necessary.
    703   1.1   mycroft  */
    704   1.1   mycroft int
    705   1.1   mycroft dqget(vp, id, ump, type, dqp)
    706   1.1   mycroft 	struct vnode *vp;
    707   1.1   mycroft 	u_long id;
    708   1.1   mycroft 	register struct ufsmount *ump;
    709   1.1   mycroft 	register int type;
    710   1.1   mycroft 	struct dquot **dqp;
    711   1.1   mycroft {
    712  1.11      fvdl 	struct dquot *dq;
    713   1.5   mycroft 	struct dqhash *dqh;
    714  1.11      fvdl 	struct vnode *dqvp;
    715   1.1   mycroft 	struct iovec aiov;
    716   1.1   mycroft 	struct uio auio;
    717   1.1   mycroft 	int error;
    718   1.1   mycroft 
    719   1.1   mycroft 	dqvp = ump->um_quotas[type];
    720   1.1   mycroft 	if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
    721   1.1   mycroft 		*dqp = NODQUOT;
    722   1.1   mycroft 		return (EINVAL);
    723   1.1   mycroft 	}
    724   1.1   mycroft 	/*
    725   1.1   mycroft 	 * Check the cache first.
    726   1.1   mycroft 	 */
    727   1.5   mycroft 	dqh = DQHASH(dqvp, id);
    728   1.5   mycroft 	for (dq = dqh->lh_first; dq; dq = dq->dq_hash.le_next) {
    729   1.1   mycroft 		if (dq->dq_id != id ||
    730   1.1   mycroft 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    731   1.1   mycroft 			continue;
    732   1.1   mycroft 		/*
    733   1.1   mycroft 		 * Cache hit with no references.  Take
    734   1.1   mycroft 		 * the structure off the free list.
    735   1.1   mycroft 		 */
    736   1.5   mycroft 		if (dq->dq_cnt == 0)
    737   1.5   mycroft 			TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
    738   1.1   mycroft 		DQREF(dq);
    739   1.1   mycroft 		*dqp = dq;
    740   1.1   mycroft 		return (0);
    741   1.1   mycroft 	}
    742   1.1   mycroft 	/*
    743   1.1   mycroft 	 * Not in cache, allocate a new one.
    744   1.1   mycroft 	 */
    745   1.5   mycroft 	if (dqfreelist.tqh_first == NODQUOT &&
    746   1.5   mycroft 	    numdquot < MAXQUOTAS * desiredvnodes)
    747   1.1   mycroft 		desireddquot += DQUOTINC;
    748   1.1   mycroft 	if (numdquot < desireddquot) {
    749   1.1   mycroft 		dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
    750  1.12     perry 		memset((char *)dq, 0, sizeof *dq);
    751   1.1   mycroft 		numdquot++;
    752   1.1   mycroft 	} else {
    753   1.5   mycroft 		if ((dq = dqfreelist.tqh_first) == NULL) {
    754   1.1   mycroft 			tablefull("dquot");
    755   1.1   mycroft 			*dqp = NODQUOT;
    756   1.1   mycroft 			return (EUSERS);
    757   1.1   mycroft 		}
    758   1.1   mycroft 		if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
    759   1.1   mycroft 			panic("free dquot isn't");
    760   1.5   mycroft 		TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
    761   1.5   mycroft 		LIST_REMOVE(dq, dq_hash);
    762   1.1   mycroft 	}
    763   1.1   mycroft 	/*
    764   1.1   mycroft 	 * Initialize the contents of the dquot structure.
    765   1.1   mycroft 	 */
    766   1.1   mycroft 	if (vp != dqvp)
    767  1.11      fvdl 		vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
    768   1.5   mycroft 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
    769   1.1   mycroft 	DQREF(dq);
    770   1.1   mycroft 	dq->dq_flags = DQ_LOCK;
    771   1.1   mycroft 	dq->dq_id = id;
    772   1.1   mycroft 	dq->dq_ump = ump;
    773   1.1   mycroft 	dq->dq_type = type;
    774   1.1   mycroft 	auio.uio_iov = &aiov;
    775   1.1   mycroft 	auio.uio_iovcnt = 1;
    776   1.1   mycroft 	aiov.iov_base = (caddr_t)&dq->dq_dqb;
    777   1.1   mycroft 	aiov.iov_len = sizeof (struct dqblk);
    778   1.1   mycroft 	auio.uio_resid = sizeof (struct dqblk);
    779   1.1   mycroft 	auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
    780   1.1   mycroft 	auio.uio_segflg = UIO_SYSSPACE;
    781   1.1   mycroft 	auio.uio_rw = UIO_READ;
    782   1.1   mycroft 	auio.uio_procp = (struct proc *)0;
    783   1.1   mycroft 	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
    784   1.1   mycroft 	if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
    785  1.12     perry 		memset((caddr_t)&dq->dq_dqb, 0, sizeof(struct dqblk));
    786   1.1   mycroft 	if (vp != dqvp)
    787  1.11      fvdl 		VOP_UNLOCK(dqvp, 0);
    788   1.1   mycroft 	if (dq->dq_flags & DQ_WANT)
    789   1.1   mycroft 		wakeup((caddr_t)dq);
    790   1.1   mycroft 	dq->dq_flags = 0;
    791   1.1   mycroft 	/*
    792   1.1   mycroft 	 * I/O error in reading quota file, release
    793   1.1   mycroft 	 * quota structure and reflect problem to caller.
    794   1.1   mycroft 	 */
    795   1.1   mycroft 	if (error) {
    796   1.5   mycroft 		LIST_REMOVE(dq, dq_hash);
    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.5   mycroft 	TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
    849   1.1   mycroft }
    850   1.1   mycroft 
    851   1.1   mycroft /*
    852   1.1   mycroft  * Update the disk quota in the quota file.
    853   1.1   mycroft  */
    854   1.1   mycroft int
    855   1.1   mycroft dqsync(vp, dq)
    856   1.1   mycroft 	struct vnode *vp;
    857   1.1   mycroft 	register struct dquot *dq;
    858   1.1   mycroft {
    859   1.1   mycroft 	struct vnode *dqvp;
    860   1.1   mycroft 	struct iovec aiov;
    861   1.1   mycroft 	struct uio auio;
    862   1.1   mycroft 	int error;
    863   1.1   mycroft 
    864   1.1   mycroft 	if (dq == NODQUOT)
    865   1.1   mycroft 		panic("dqsync: dquot");
    866   1.1   mycroft 	if ((dq->dq_flags & DQ_MOD) == 0)
    867   1.1   mycroft 		return (0);
    868   1.1   mycroft 	if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
    869   1.1   mycroft 		panic("dqsync: file");
    870   1.1   mycroft 	if (vp != dqvp)
    871  1.11      fvdl 		vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
    872   1.1   mycroft 	while (dq->dq_flags & DQ_LOCK) {
    873   1.1   mycroft 		dq->dq_flags |= DQ_WANT;
    874   1.1   mycroft 		sleep((caddr_t)dq, PINOD+2);
    875   1.1   mycroft 		if ((dq->dq_flags & DQ_MOD) == 0) {
    876   1.1   mycroft 			if (vp != dqvp)
    877  1.11      fvdl 				VOP_UNLOCK(dqvp, 0);
    878   1.1   mycroft 			return (0);
    879   1.1   mycroft 		}
    880   1.1   mycroft 	}
    881   1.1   mycroft 	dq->dq_flags |= DQ_LOCK;
    882   1.1   mycroft 	auio.uio_iov = &aiov;
    883   1.1   mycroft 	auio.uio_iovcnt = 1;
    884   1.1   mycroft 	aiov.iov_base = (caddr_t)&dq->dq_dqb;
    885   1.1   mycroft 	aiov.iov_len = sizeof (struct dqblk);
    886   1.1   mycroft 	auio.uio_resid = sizeof (struct dqblk);
    887   1.1   mycroft 	auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
    888   1.1   mycroft 	auio.uio_segflg = UIO_SYSSPACE;
    889   1.1   mycroft 	auio.uio_rw = UIO_WRITE;
    890   1.1   mycroft 	auio.uio_procp = (struct proc *)0;
    891   1.1   mycroft 	error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
    892   1.1   mycroft 	if (auio.uio_resid && error == 0)
    893   1.1   mycroft 		error = EIO;
    894   1.1   mycroft 	if (dq->dq_flags & DQ_WANT)
    895   1.1   mycroft 		wakeup((caddr_t)dq);
    896   1.1   mycroft 	dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
    897   1.1   mycroft 	if (vp != dqvp)
    898  1.11      fvdl 		VOP_UNLOCK(dqvp, 0);
    899   1.1   mycroft 	return (error);
    900   1.1   mycroft }
    901   1.1   mycroft 
    902   1.1   mycroft /*
    903   1.1   mycroft  * Flush all entries from the cache for a particular vnode.
    904   1.1   mycroft  */
    905   1.1   mycroft void
    906   1.1   mycroft dqflush(vp)
    907   1.1   mycroft 	register struct vnode *vp;
    908   1.1   mycroft {
    909   1.5   mycroft 	register struct dquot *dq, *nextdq;
    910   1.5   mycroft 	struct dqhash *dqh;
    911   1.1   mycroft 
    912   1.1   mycroft 	/*
    913   1.1   mycroft 	 * Move all dquot's that used to refer to this quota
    914   1.1   mycroft 	 * file off their hash chains (they will eventually
    915   1.1   mycroft 	 * fall off the head of the free list and be re-used).
    916   1.1   mycroft 	 */
    917   1.5   mycroft 	for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
    918   1.5   mycroft 		for (dq = dqh->lh_first; dq; dq = nextdq) {
    919   1.5   mycroft 			nextdq = dq->dq_hash.le_next;
    920   1.1   mycroft 			if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
    921   1.1   mycroft 				continue;
    922   1.1   mycroft 			if (dq->dq_cnt)
    923   1.1   mycroft 				panic("dqflush: stray dquot");
    924   1.5   mycroft 			LIST_REMOVE(dq, dq_hash);
    925   1.1   mycroft 			dq->dq_ump = (struct ufsmount *)0;
    926   1.1   mycroft 		}
    927   1.1   mycroft 	}
    928   1.1   mycroft }
    929