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