Home | History | Annotate | Line # | Download | only in ufs
ufs_quota.c revision 1.107
      1  1.107  dholland /*	$NetBSD: ufs_quota.c,v 1.107 2012/02/01 05:34:43 dholland 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.107  dholland __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.107 2012/02/01 05:34:43 dholland Exp $");
     39   1.22     lukem 
     40   1.69    bouyer #if defined(_KERNEL_OPT)
     41   1.69    bouyer #include "opt_quota.h"
     42   1.69    bouyer #endif
     43    1.1   mycroft #include <sys/param.h>
     44    1.1   mycroft #include <sys/kernel.h>
     45    1.1   mycroft #include <sys/systm.h>
     46    1.1   mycroft #include <sys/namei.h>
     47    1.1   mycroft #include <sys/file.h>
     48    1.1   mycroft #include <sys/proc.h>
     49    1.1   mycroft #include <sys/vnode.h>
     50    1.1   mycroft #include <sys/mount.h>
     51   1.39      elad #include <sys/kauth.h>
     52    1.1   mycroft 
     53   1.71  dholland #include <sys/quotactl.h>
     54    1.1   mycroft #include <ufs/ufs/quota.h>
     55    1.1   mycroft #include <ufs/ufs/inode.h>
     56    1.1   mycroft #include <ufs/ufs/ufsmount.h>
     57    1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     58   1.69    bouyer #include <ufs/ufs/ufs_quota.h>
     59    1.1   mycroft 
     60   1.69    bouyer kmutex_t dqlock;
     61   1.69    bouyer kcondvar_t dqcv;
     62   1.48   hannken 
     63   1.48   hannken /*
     64   1.69    bouyer  * Code pertaining to management of the in-core dquot data structures.
     65    1.1   mycroft  */
     66   1.69    bouyer #define DQHASH(dqvp, id) \
     67   1.69    bouyer 	(((((long)(dqvp)) >> 8) + id) & dqhash)
     68   1.69    bouyer static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
     69   1.69    bouyer static u_long dqhash;
     70   1.69    bouyer static pool_cache_t dquot_cache;
     71    1.1   mycroft 
     72    1.1   mycroft 
     73  1.101  dholland static int quota_handle_cmd_stat(struct mount *, struct lwp *,
     74  1.107  dholland     struct quotactl_args *args);
     75  1.106  dholland static int quota_handle_cmd_idtypestat(struct mount *, struct lwp *,
     76  1.107  dholland     struct quotactl_args *args);
     77  1.106  dholland static int quota_handle_cmd_objtypestat(struct mount *, struct lwp *,
     78  1.107  dholland     struct quotactl_args *args);
     79   1.69    bouyer static int quota_handle_cmd_get(struct mount *, struct lwp *,
     80  1.107  dholland     struct quotactl_args *args);
     81   1.85  dholland static int quota_handle_cmd_put(struct mount *, struct lwp *,
     82  1.107  dholland     struct quotactl_args *args);
     83   1.98  dholland static int quota_handle_cmd_cursorget(struct mount *, struct lwp *,
     84  1.107  dholland     struct quotactl_args *args);
     85   1.91  dholland static int quota_handle_cmd_delete(struct mount *, struct lwp *,
     86  1.107  dholland     struct quotactl_args *args);
     87   1.69    bouyer static int quota_handle_cmd_quotaon(struct mount *, struct lwp *,
     88  1.107  dholland     struct quotactl_args *args);
     89   1.69    bouyer static int quota_handle_cmd_quotaoff(struct mount *, struct lwp *,
     90  1.107  dholland     struct quotactl_args *args);
     91   1.93  dholland static int quota_handle_cmd_cursoropen(struct mount *, struct lwp *,
     92  1.107  dholland     struct quotactl_args *args);
     93   1.93  dholland static int quota_handle_cmd_cursorclose(struct mount *, struct lwp *,
     94  1.107  dholland     struct quotactl_args *args);
     95   1.98  dholland static int quota_handle_cmd_cursorskipidtype(struct mount *, struct lwp *,
     96  1.107  dholland     struct quotactl_args *args);
     97   1.98  dholland static int quota_handle_cmd_cursoratend(struct mount *, struct lwp *,
     98  1.107  dholland     struct quotactl_args *args);
     99   1.98  dholland static int quota_handle_cmd_cursorrewind(struct mount *, struct lwp *,
    100  1.107  dholland     struct quotactl_args *args);
    101   1.72  dholland 
    102    1.1   mycroft /*
    103   1.48   hannken  * Initialize the quota fields of an inode.
    104   1.48   hannken  */
    105   1.48   hannken void
    106   1.48   hannken ufsquota_init(struct inode *ip)
    107   1.48   hannken {
    108   1.48   hannken 	int i;
    109   1.48   hannken 
    110   1.48   hannken 	for (i = 0; i < MAXQUOTAS; i++)
    111   1.48   hannken 		ip->i_dquot[i] = NODQUOT;
    112   1.48   hannken }
    113   1.48   hannken 
    114   1.48   hannken /*
    115   1.48   hannken  * Release the quota fields from an inode.
    116   1.48   hannken  */
    117   1.48   hannken void
    118   1.48   hannken ufsquota_free(struct inode *ip)
    119   1.48   hannken {
    120   1.48   hannken 	int i;
    121   1.48   hannken 
    122   1.48   hannken 	for (i = 0; i < MAXQUOTAS; i++) {
    123   1.48   hannken 		dqrele(ITOV(ip), ip->i_dquot[i]);
    124   1.48   hannken 		ip->i_dquot[i] = NODQUOT;
    125   1.48   hannken 	}
    126   1.48   hannken }
    127   1.48   hannken 
    128   1.48   hannken /*
    129    1.1   mycroft  * Update disk usage, and take corrective action.
    130    1.1   mycroft  */
    131    1.1   mycroft int
    132   1.39      elad chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
    133    1.1   mycroft {
    134   1.69    bouyer 	/* do not track snapshot usage, or we will deadlock */
    135   1.69    bouyer 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
    136   1.69    bouyer 		return 0;
    137    1.1   mycroft 
    138   1.69    bouyer #ifdef QUOTA
    139   1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA)
    140   1.69    bouyer 		return chkdq1(ip, change, cred, flags);
    141   1.69    bouyer #endif
    142   1.69    bouyer #ifdef QUOTA2
    143   1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA2)
    144   1.69    bouyer 		return chkdq2(ip, change, cred, flags);
    145   1.69    bouyer #endif
    146   1.69    bouyer 	return 0;
    147    1.1   mycroft }
    148    1.1   mycroft 
    149    1.1   mycroft /*
    150   1.69    bouyer  * Check the inode limit, applying corrective action.
    151    1.1   mycroft  */
    152   1.69    bouyer int
    153   1.69    bouyer chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
    154    1.1   mycroft {
    155   1.69    bouyer 	/* do not track snapshot usage, or we will deadlock */
    156   1.69    bouyer 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
    157   1.69    bouyer 		return 0;
    158   1.69    bouyer #ifdef QUOTA
    159   1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA)
    160   1.69    bouyer 		return chkiq1(ip, change, cred, flags);
    161   1.69    bouyer #endif
    162   1.69    bouyer #ifdef QUOTA2
    163   1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA2)
    164   1.69    bouyer 		return chkiq2(ip, change, cred, flags);
    165   1.69    bouyer #endif
    166   1.69    bouyer 	return 0;
    167    1.1   mycroft }
    168    1.1   mycroft 
    169    1.1   mycroft int
    170  1.103  dholland quota_handle_cmd(struct mount *mp, struct lwp *l,
    171  1.107  dholland 		 struct quotactl_args *args)
    172   1.69    bouyer {
    173   1.69    bouyer 	int error = 0;
    174   1.69    bouyer 
    175  1.103  dholland 	switch (args->qc_op) {
    176  1.101  dholland 	    case QUOTACTL_STAT:
    177  1.101  dholland 		error = quota_handle_cmd_stat(mp, l, args);
    178   1.71  dholland 		break;
    179  1.106  dholland 	    case QUOTACTL_IDTYPESTAT:
    180  1.106  dholland 		error = quota_handle_cmd_idtypestat(mp, l, args);
    181  1.106  dholland 		break;
    182  1.106  dholland 	    case QUOTACTL_OBJTYPESTAT:
    183  1.106  dholland 		error = quota_handle_cmd_objtypestat(mp, l, args);
    184  1.106  dholland 		break;
    185   1.71  dholland 	    case QUOTACTL_QUOTAON:
    186   1.72  dholland 		error = quota_handle_cmd_quotaon(mp, l, args);
    187   1.71  dholland 		break;
    188   1.71  dholland 	    case QUOTACTL_QUOTAOFF:
    189   1.72  dholland 		error = quota_handle_cmd_quotaoff(mp, l, args);
    190   1.71  dholland 		break;
    191   1.71  dholland 	    case QUOTACTL_GET:
    192   1.72  dholland 		error = quota_handle_cmd_get(mp, l, args);
    193   1.71  dholland 		break;
    194   1.85  dholland 	    case QUOTACTL_PUT:
    195   1.85  dholland 		error = quota_handle_cmd_put(mp, l, args);
    196   1.71  dholland 		break;
    197   1.98  dholland 	    case QUOTACTL_CURSORGET:
    198   1.98  dholland 		error = quota_handle_cmd_cursorget(mp, l, args);
    199   1.71  dholland 		break;
    200   1.91  dholland 	    case QUOTACTL_DELETE:
    201   1.91  dholland 		error = quota_handle_cmd_delete(mp, l, args);
    202   1.71  dholland 		break;
    203   1.93  dholland 	    case QUOTACTL_CURSOROPEN:
    204   1.93  dholland 		error = quota_handle_cmd_cursoropen(mp, l, args);
    205   1.93  dholland 		break;
    206   1.93  dholland 	    case QUOTACTL_CURSORCLOSE:
    207   1.93  dholland 		error = quota_handle_cmd_cursorclose(mp, l, args);
    208   1.93  dholland 		break;
    209   1.98  dholland 	    case QUOTACTL_CURSORSKIPIDTYPE:
    210   1.98  dholland 		error = quota_handle_cmd_cursorskipidtype(mp, l, args);
    211   1.98  dholland 		break;
    212   1.98  dholland 	    case QUOTACTL_CURSORATEND:
    213   1.98  dholland 		error = quota_handle_cmd_cursoratend(mp, l, args);
    214   1.98  dholland 		break;
    215   1.98  dholland 	    case QUOTACTL_CURSORREWIND:
    216   1.98  dholland 		error = quota_handle_cmd_cursorrewind(mp, l, args);
    217   1.98  dholland 		break;
    218   1.71  dholland 	    default:
    219  1.103  dholland 		panic("Invalid quotactl operation %d\n", args->qc_op);
    220   1.69    bouyer 	}
    221   1.71  dholland 
    222   1.69    bouyer 	return error;
    223   1.69    bouyer }
    224   1.69    bouyer 
    225   1.69    bouyer static int
    226  1.101  dholland quota_handle_cmd_stat(struct mount *mp, struct lwp *l,
    227  1.107  dholland     struct quotactl_args *args)
    228    1.1   mycroft {
    229   1.69    bouyer 	struct ufsmount *ump = VFSTOUFS(mp);
    230  1.101  dholland 	struct quotastat *ret;
    231   1.72  dholland 
    232  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_STAT);
    233  1.101  dholland 	ret = args->u.stat.qc_ret;
    234   1.69    bouyer 
    235   1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    236   1.69    bouyer 		return EOPNOTSUPP;
    237   1.69    bouyer 
    238   1.69    bouyer #ifdef QUOTA
    239   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA) {
    240  1.101  dholland 		strcpy(ret->qs_implname, "ufs/ffs quota v1");
    241  1.101  dholland 		ret->qs_numidtypes = MAXQUOTAS;
    242  1.101  dholland 		/* XXX no define for this */
    243  1.101  dholland 		ret->qs_numobjtypes = 2;
    244  1.101  dholland 		ret->qs_restrictions = 0;
    245  1.101  dholland 		ret->qs_restrictions |= QUOTA_RESTRICT_NEEDSQUOTACHECK;
    246  1.101  dholland 		ret->qs_restrictions |= QUOTA_RESTRICT_UNIFORMGRACE;
    247  1.101  dholland 		ret->qs_restrictions |= QUOTA_RESTRICT_32BIT;
    248   1.69    bouyer 	} else
    249   1.69    bouyer #endif
    250   1.69    bouyer #ifdef QUOTA2
    251   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2) {
    252  1.101  dholland 		strcpy(ret->qs_implname, "ufs/ffs quota v2");
    253  1.101  dholland 		ret->qs_numidtypes = MAXQUOTAS;
    254  1.101  dholland 		ret->qs_numobjtypes = N_QL;
    255  1.101  dholland 		ret->qs_restrictions = 0;
    256   1.69    bouyer 	} else
    257   1.69    bouyer #endif
    258   1.73  dholland 		return EOPNOTSUPP;
    259   1.73  dholland 
    260   1.73  dholland 	return 0;
    261    1.1   mycroft }
    262    1.1   mycroft 
    263  1.106  dholland static int
    264  1.106  dholland quota_handle_cmd_idtypestat(struct mount *mp, struct lwp *l,
    265  1.107  dholland     struct quotactl_args *args)
    266  1.106  dholland {
    267  1.106  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    268  1.106  dholland 	int idtype;
    269  1.106  dholland 	struct quotaidtypestat *info;
    270  1.106  dholland 	const char *name;
    271  1.106  dholland 
    272  1.106  dholland 	KASSERT(args->qc_op == QUOTACTL_IDTYPESTAT);
    273  1.106  dholland 	idtype = args->u.idtypestat.qc_idtype;
    274  1.106  dholland 	info = args->u.idtypestat.qc_info;
    275  1.106  dholland 
    276  1.106  dholland 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    277  1.106  dholland 		return EOPNOTSUPP;
    278  1.106  dholland 
    279  1.106  dholland 	/*
    280  1.106  dholland 	 * These are the same for both QUOTA and QUOTA2.
    281  1.106  dholland 	 */
    282  1.106  dholland 	switch (idtype) {
    283  1.106  dholland 	    case QUOTA_IDTYPE_USER:
    284  1.106  dholland 		name = "user";
    285  1.106  dholland 		break;
    286  1.106  dholland 	    case QUOTA_IDTYPE_GROUP:
    287  1.106  dholland 		name = "group";
    288  1.106  dholland 		break;
    289  1.106  dholland 	    default:
    290  1.106  dholland 		return EINVAL;
    291  1.106  dholland 	}
    292  1.106  dholland 	strlcpy(info->qis_name, name, sizeof(info->qis_name));
    293  1.106  dholland 	return 0;
    294  1.106  dholland }
    295  1.106  dholland 
    296  1.106  dholland static int
    297  1.106  dholland quota_handle_cmd_objtypestat(struct mount *mp, struct lwp *l,
    298  1.107  dholland     struct quotactl_args *args)
    299  1.106  dholland {
    300  1.106  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    301  1.106  dholland 	int objtype;
    302  1.106  dholland 	struct quotaobjtypestat *info;
    303  1.106  dholland 	const char *name;
    304  1.106  dholland 	int isbytes;
    305  1.106  dholland 
    306  1.106  dholland 	KASSERT(args->qc_op == QUOTACTL_OBJTYPESTAT);
    307  1.106  dholland 	objtype = args->u.objtypestat.qc_objtype;
    308  1.106  dholland 	info = args->u.objtypestat.qc_info;
    309  1.106  dholland 
    310  1.106  dholland 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    311  1.106  dholland 		return EOPNOTSUPP;
    312  1.106  dholland 
    313  1.106  dholland 	/*
    314  1.106  dholland 	 * These are the same for both QUOTA and QUOTA2.
    315  1.106  dholland 	 */
    316  1.106  dholland 	switch (objtype) {
    317  1.106  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    318  1.106  dholland 		name = "block";
    319  1.106  dholland 		isbytes = 1;
    320  1.106  dholland 		break;
    321  1.106  dholland 	    case QUOTA_OBJTYPE_FILES:
    322  1.106  dholland 		name = "file";
    323  1.106  dholland 		isbytes = 0;
    324  1.106  dholland 		break;
    325  1.106  dholland 	    default:
    326  1.106  dholland 		return EINVAL;
    327  1.106  dholland 	}
    328  1.106  dholland 	strlcpy(info->qos_name, name, sizeof(info->qos_name));
    329  1.106  dholland 	info->qos_isbytes = isbytes;
    330  1.106  dholland 	return 0;
    331  1.106  dholland }
    332  1.106  dholland 
    333   1.69    bouyer /* XXX shouldn't all this be in kauth ? */
    334   1.48   hannken static int
    335   1.69    bouyer quota_get_auth(struct mount *mp, struct lwp *l, uid_t id) {
    336   1.69    bouyer 	/* The user can always query about his own quota. */
    337   1.69    bouyer 	if (id == kauth_cred_getuid(l->l_cred))
    338   1.69    bouyer 		return 0;
    339   1.69    bouyer 	return kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    340   1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(id), NULL);
    341   1.69    bouyer }
    342   1.69    bouyer 
    343   1.69    bouyer static int
    344   1.69    bouyer quota_handle_cmd_get(struct mount *mp, struct lwp *l,
    345  1.107  dholland     struct quotactl_args *args)
    346   1.69    bouyer {
    347   1.69    bouyer 	struct ufsmount *ump = VFSTOUFS(mp);
    348   1.74  dholland 	int error;
    349   1.78  dholland 	const struct quotakey *qk;
    350   1.77  dholland 	struct quotaval *ret;
    351   1.72  dholland 
    352  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_GET);
    353   1.78  dholland 	qk = args->u.get.qc_key;
    354   1.77  dholland 	ret = args->u.get.qc_ret;
    355   1.69    bouyer 
    356   1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    357   1.69    bouyer 		return EOPNOTSUPP;
    358   1.69    bouyer 
    359   1.79  dholland 	error = quota_get_auth(mp, l, qk->qk_id);
    360   1.79  dholland 	if (error != 0)
    361   1.79  dholland 		return error;
    362   1.69    bouyer #ifdef QUOTA
    363   1.79  dholland 	if (ump->um_flags & UFS_QUOTA) {
    364   1.79  dholland 		error = quota1_handle_cmd_get(ump, qk, ret);
    365   1.79  dholland 	} else
    366   1.69    bouyer #endif
    367   1.69    bouyer #ifdef QUOTA2
    368   1.79  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    369   1.79  dholland 		error = quota2_handle_cmd_get(ump, qk, ret);
    370   1.79  dholland 	} else
    371   1.69    bouyer #endif
    372   1.79  dholland 		panic("quota_handle_cmd_get: no support ?");
    373   1.69    bouyer 
    374   1.79  dholland 	if (error != 0)
    375   1.79  dholland 		return error;
    376   1.74  dholland 
    377   1.69    bouyer 	return error;
    378   1.69    bouyer }
    379   1.69    bouyer 
    380   1.69    bouyer static int
    381   1.85  dholland quota_handle_cmd_put(struct mount *mp, struct lwp *l,
    382  1.107  dholland     struct quotactl_args *args)
    383   1.69    bouyer {
    384   1.11      fvdl 	struct ufsmount *ump = VFSTOUFS(mp);
    385   1.84  dholland 	const struct quotakey *qk;
    386   1.83  dholland 	const struct quotaval *qv;
    387   1.84  dholland 	id_t kauth_id;
    388   1.80  dholland 	int error;
    389   1.72  dholland 
    390  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_PUT);
    391   1.85  dholland 	qk = args->u.put.qc_key;
    392   1.85  dholland 	qv = args->u.put.qc_val;
    393    1.1   mycroft 
    394   1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    395   1.69    bouyer 		return EOPNOTSUPP;
    396   1.61        ad 
    397   1.84  dholland 	kauth_id = qk->qk_id;
    398   1.84  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    399   1.84  dholland 		kauth_id = 0;
    400   1.84  dholland 	}
    401   1.84  dholland 
    402   1.86  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    403   1.86  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    404   1.86  dholland 	    NULL);
    405   1.87  dholland 	if (error != 0) {
    406   1.87  dholland 		return error;
    407   1.87  dholland 	}
    408   1.87  dholland 
    409   1.69    bouyer #ifdef QUOTA
    410   1.86  dholland 	if (ump->um_flags & UFS_QUOTA)
    411   1.86  dholland 		error = quota1_handle_cmd_put(ump, qk, qv);
    412   1.86  dholland 	else
    413   1.69    bouyer #endif
    414   1.69    bouyer #ifdef QUOTA2
    415   1.86  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    416   1.86  dholland 		error = quota2_handle_cmd_put(ump, qk, qv);
    417   1.86  dholland 	} else
    418   1.69    bouyer #endif
    419   1.86  dholland 		panic("quota_handle_cmd_get: no support ?");
    420   1.69    bouyer 
    421   1.87  dholland 	if (error == ENOENT) {
    422   1.87  dholland 		error = 0;
    423   1.87  dholland 	}
    424   1.80  dholland 
    425   1.69    bouyer 	return error;
    426   1.69    bouyer }
    427   1.69    bouyer 
    428   1.69    bouyer static int
    429   1.91  dholland quota_handle_cmd_delete(struct mount *mp, struct lwp *l,
    430  1.107  dholland     struct quotactl_args *args)
    431   1.69    bouyer {
    432   1.88  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    433   1.92  dholland 	const struct quotakey *qk;
    434   1.92  dholland 	id_t kauth_id;
    435   1.88  dholland 	int error;
    436   1.72  dholland 
    437  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_DELETE);
    438   1.92  dholland 	qk = args->u.delete.qc_key;
    439   1.92  dholland 
    440   1.92  dholland 	kauth_id = qk->qk_id;
    441   1.92  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    442   1.92  dholland 		kauth_id = 0;
    443   1.92  dholland 	}
    444   1.33     perry 
    445   1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) == 0)
    446   1.69    bouyer 		return EOPNOTSUPP;
    447   1.53        ad 
    448   1.88  dholland 	/* avoid whitespace changes */
    449   1.88  dholland 	{
    450   1.69    bouyer 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    451   1.92  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    452   1.92  dholland 		    NULL);
    453   1.69    bouyer 		if (error != 0)
    454   1.69    bouyer 			goto err;
    455   1.69    bouyer #ifdef QUOTA2
    456   1.69    bouyer 		if (ump->um_flags & UFS_QUOTA2) {
    457   1.92  dholland 			error = quota2_handle_cmd_delete(ump, qk);
    458   1.69    bouyer 		} else
    459   1.69    bouyer #endif
    460   1.69    bouyer 			panic("quota_handle_cmd_get: no support ?");
    461   1.69    bouyer 
    462   1.69    bouyer 		if (error && error != ENOENT)
    463   1.69    bouyer 			goto err;
    464   1.50   hannken 	}
    465   1.88  dholland 
    466   1.88  dholland 	return 0;
    467   1.88  dholland  err:
    468   1.69    bouyer 	return error;
    469    1.1   mycroft }
    470    1.1   mycroft 
    471   1.69    bouyer static int
    472   1.98  dholland quota_handle_cmd_cursorget(struct mount *mp, struct lwp *l,
    473  1.107  dholland     struct quotactl_args *args)
    474    1.1   mycroft {
    475    1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    476   1.94  dholland 	struct quotakcursor *cursor;
    477   1.96  dholland 	struct quotakey *keys;
    478   1.96  dholland 	struct quotaval *vals;
    479   1.96  dholland 	unsigned maxnum;
    480   1.96  dholland 	unsigned *ret;
    481    1.1   mycroft 	int error;
    482   1.72  dholland 
    483  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORGET);
    484   1.98  dholland 	cursor = args->u.cursorget.qc_cursor;
    485   1.98  dholland 	keys = args->u.cursorget.qc_keys;
    486   1.98  dholland 	vals = args->u.cursorget.qc_vals;
    487   1.98  dholland 	maxnum = args->u.cursorget.qc_maxnum;
    488   1.98  dholland 	ret = args->u.cursorget.qc_ret;
    489    1.1   mycroft 
    490   1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) == 0)
    491   1.69    bouyer 		return EOPNOTSUPP;
    492   1.69    bouyer 
    493   1.69    bouyer 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    494   1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    495    1.8  christos 	if (error)
    496   1.69    bouyer 		return error;
    497   1.69    bouyer 
    498   1.69    bouyer #ifdef QUOTA2
    499   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2) {
    500   1.98  dholland 		error = quota2_handle_cmd_cursorget(ump, cursor, keys, vals,
    501   1.98  dholland 						    maxnum, ret);
    502   1.69    bouyer 	} else
    503   1.69    bouyer #endif
    504   1.98  dholland 		panic("quota_handle_cmd_cursorget: no support ?");
    505   1.95  dholland 
    506   1.69    bouyer 	return error;
    507    1.1   mycroft }
    508    1.1   mycroft 
    509   1.69    bouyer static int
    510   1.93  dholland quota_handle_cmd_cursoropen(struct mount *mp, struct lwp *l,
    511  1.107  dholland     struct quotactl_args *args)
    512   1.93  dholland {
    513  1.105      para #ifdef QUOTA2
    514   1.93  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    515  1.105      para #endif
    516   1.93  dholland 	struct quotakcursor *cursor;
    517   1.93  dholland 	int error;
    518   1.93  dholland 
    519  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSOROPEN);
    520   1.93  dholland 	cursor = args->u.cursoropen.qc_cursor;
    521   1.93  dholland 
    522   1.93  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    523   1.93  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    524   1.93  dholland 	if (error)
    525   1.93  dholland 		return error;
    526   1.93  dholland 
    527   1.93  dholland #ifdef QUOTA2
    528   1.93  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    529   1.93  dholland 		error = quota2_handle_cmd_cursoropen(ump, cursor);
    530   1.93  dholland 	} else
    531   1.93  dholland #endif
    532   1.93  dholland 		error = EOPNOTSUPP;
    533   1.93  dholland 
    534   1.93  dholland 	return error;
    535   1.93  dholland }
    536   1.93  dholland 
    537   1.93  dholland static int
    538   1.93  dholland quota_handle_cmd_cursorclose(struct mount *mp, struct lwp *l,
    539  1.107  dholland     struct quotactl_args *args)
    540   1.93  dholland {
    541  1.105      para #ifdef QUOTA2
    542   1.93  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    543  1.105      para #endif
    544   1.93  dholland 	struct quotakcursor *cursor;
    545   1.93  dholland 	int error;
    546   1.93  dholland 
    547  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORCLOSE);
    548   1.93  dholland 	cursor = args->u.cursorclose.qc_cursor;
    549   1.93  dholland 
    550   1.93  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    551   1.93  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    552   1.93  dholland 	if (error)
    553   1.93  dholland 		return error;
    554   1.93  dholland 
    555   1.93  dholland #ifdef QUOTA2
    556   1.93  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    557   1.93  dholland 		error = quota2_handle_cmd_cursorclose(ump, cursor);
    558   1.93  dholland 	} else
    559   1.93  dholland #endif
    560   1.93  dholland 		error = EOPNOTSUPP;
    561   1.93  dholland 
    562   1.93  dholland 	return error;
    563   1.93  dholland }
    564   1.93  dholland 
    565   1.93  dholland static int
    566   1.98  dholland quota_handle_cmd_cursorskipidtype(struct mount *mp, struct lwp *l,
    567  1.107  dholland     struct quotactl_args *args)
    568   1.98  dholland {
    569  1.105      para #ifdef QUOTA2
    570   1.98  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    571  1.105      para #endif
    572   1.98  dholland 	struct quotakcursor *cursor;
    573   1.98  dholland 	int idtype;
    574   1.98  dholland 	int error;
    575   1.98  dholland 
    576  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORSKIPIDTYPE);
    577   1.98  dholland 	cursor = args->u.cursorskipidtype.qc_cursor;
    578   1.98  dholland 	idtype = args->u.cursorskipidtype.qc_idtype;
    579   1.98  dholland 
    580   1.98  dholland #ifdef QUOTA2
    581   1.98  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    582   1.98  dholland 		error = quota2_handle_cmd_cursorskipidtype(ump, cursor, idtype);
    583   1.98  dholland 	} else
    584   1.98  dholland #endif
    585   1.98  dholland 		error = EOPNOTSUPP;
    586   1.98  dholland 
    587   1.98  dholland 	return error;
    588   1.98  dholland }
    589   1.98  dholland 
    590   1.98  dholland static int
    591   1.98  dholland quota_handle_cmd_cursoratend(struct mount *mp, struct lwp *l,
    592  1.107  dholland     struct quotactl_args *args)
    593   1.98  dholland {
    594  1.105      para #ifdef QUOTA2
    595   1.98  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    596  1.105      para #endif
    597   1.98  dholland 	struct quotakcursor *cursor;
    598   1.98  dholland 	int *ret;
    599   1.98  dholland 	int error;
    600   1.98  dholland 
    601  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORATEND);
    602   1.98  dholland 	cursor = args->u.cursoratend.qc_cursor;
    603   1.98  dholland 	ret = args->u.cursoratend.qc_ret;
    604   1.98  dholland 
    605   1.98  dholland #ifdef QUOTA2
    606   1.98  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    607   1.98  dholland 		error = quota2_handle_cmd_cursoratend(ump, cursor, ret);
    608   1.98  dholland 	} else
    609   1.98  dholland #endif
    610   1.98  dholland 		error = EOPNOTSUPP;
    611   1.98  dholland 
    612   1.98  dholland 	return error;
    613   1.98  dholland }
    614   1.98  dholland 
    615   1.98  dholland static int
    616   1.98  dholland quota_handle_cmd_cursorrewind(struct mount *mp, struct lwp *l,
    617  1.107  dholland     struct quotactl_args *args)
    618   1.98  dholland {
    619  1.105      para #ifdef QUOTA2
    620   1.98  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    621  1.105      para #endif
    622   1.98  dholland 	struct quotakcursor *cursor;
    623   1.98  dholland 	int error;
    624   1.98  dholland 
    625  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORREWIND);
    626   1.98  dholland 	cursor = args->u.cursorrewind.qc_cursor;
    627   1.98  dholland 
    628   1.98  dholland #ifdef QUOTA2
    629   1.98  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    630   1.98  dholland 		error = quota2_handle_cmd_cursorrewind(ump, cursor);
    631   1.98  dholland 	} else
    632   1.98  dholland #endif
    633   1.98  dholland 		error = EOPNOTSUPP;
    634   1.98  dholland 
    635   1.98  dholland 	return error;
    636   1.98  dholland }
    637   1.98  dholland 
    638   1.98  dholland static int
    639   1.69    bouyer quota_handle_cmd_quotaon(struct mount *mp, struct lwp *l,
    640  1.107  dholland     struct quotactl_args *args)
    641    1.1   mycroft {
    642    1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    643   1.99  dholland 	int idtype;
    644   1.99  dholland 	const char *qfile;
    645    1.1   mycroft 	int error;
    646   1.72  dholland 
    647  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_QUOTAON);
    648   1.99  dholland 	idtype = args->u.quotaon.qc_idtype;
    649   1.99  dholland 	qfile = args->u.quotaon.qc_quotafile;
    650   1.69    bouyer 
    651   1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) != 0)
    652   1.69    bouyer 		return EBUSY;
    653   1.69    bouyer 
    654   1.69    bouyer 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    655   1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    656   1.69    bouyer 	if (error != 0) {
    657   1.69    bouyer 		return error;
    658   1.69    bouyer 	}
    659   1.69    bouyer #ifdef QUOTA
    660   1.99  dholland 	error = quota1_handle_cmd_quotaon(l, ump, idtype, qfile);
    661   1.69    bouyer #else
    662   1.69    bouyer 	error = EOPNOTSUPP;
    663   1.69    bouyer #endif
    664   1.69    bouyer 
    665   1.69    bouyer 	return error;
    666    1.1   mycroft }
    667    1.1   mycroft 
    668   1.69    bouyer static int
    669   1.69    bouyer quota_handle_cmd_quotaoff(struct mount *mp, struct lwp *l,
    670  1.107  dholland     struct quotactl_args *args)
    671    1.1   mycroft {
    672    1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    673  1.100  dholland 	int idtype;
    674   1.69    bouyer 	int error;
    675   1.72  dholland 
    676  1.102  dholland 	KASSERT(args->qc_op == QUOTACTL_QUOTAOFF);
    677  1.100  dholland 	idtype = args->u.quotaoff.qc_idtype;
    678    1.1   mycroft 
    679   1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) != 0)
    680   1.69    bouyer 		return EOPNOTSUPP;
    681   1.69    bouyer 
    682   1.69    bouyer 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    683   1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    684   1.69    bouyer 	if (error != 0) {
    685   1.69    bouyer 		return error;
    686    1.1   mycroft 	}
    687   1.69    bouyer #ifdef QUOTA
    688  1.100  dholland 	error = quota1_handle_cmd_quotaoff(l, ump, idtype);
    689   1.69    bouyer #else
    690   1.69    bouyer 	error = EOPNOTSUPP;
    691   1.69    bouyer #endif
    692   1.69    bouyer 
    693   1.69    bouyer 	return error;
    694    1.1   mycroft }
    695    1.1   mycroft 
    696    1.1   mycroft /*
    697    1.1   mycroft  * Initialize the quota system.
    698    1.1   mycroft  */
    699    1.1   mycroft void
    700   1.35   thorpej dqinit(void)
    701    1.1   mycroft {
    702   1.47     pooka 
    703   1.50   hannken 	mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
    704   1.50   hannken 	cv_init(&dqcv, "quota");
    705   1.60        ad 	dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
    706   1.55        ad 	dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "ufsdq",
    707   1.55        ad 	    NULL, IPL_NONE, NULL, NULL, NULL);
    708   1.14  jdolecek }
    709   1.14  jdolecek 
    710   1.21       chs void
    711   1.35   thorpej dqreinit(void)
    712   1.21       chs {
    713   1.21       chs 	struct dquot *dq;
    714   1.21       chs 	struct dqhashhead *oldhash, *hash;
    715   1.21       chs 	struct vnode *dqvp;
    716   1.21       chs 	u_long oldmask, mask, hashval;
    717   1.21       chs 	int i;
    718   1.21       chs 
    719   1.60        ad 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    720   1.50   hannken 	mutex_enter(&dqlock);
    721   1.21       chs 	oldhash = dqhashtbl;
    722   1.21       chs 	oldmask = dqhash;
    723   1.21       chs 	dqhashtbl = hash;
    724   1.21       chs 	dqhash = mask;
    725   1.21       chs 	for (i = 0; i <= oldmask; i++) {
    726   1.21       chs 		while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
    727   1.21       chs 			dqvp = dq->dq_ump->um_quotas[dq->dq_type];
    728   1.21       chs 			LIST_REMOVE(dq, dq_hash);
    729   1.21       chs 			hashval = DQHASH(dqvp, dq->dq_id);
    730   1.21       chs 			LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
    731   1.21       chs 		}
    732   1.21       chs 	}
    733   1.50   hannken 	mutex_exit(&dqlock);
    734   1.60        ad 	hashdone(oldhash, HASH_LIST, oldmask);
    735   1.21       chs }
    736   1.21       chs 
    737   1.14  jdolecek /*
    738   1.14  jdolecek  * Free resources held by quota system.
    739   1.14  jdolecek  */
    740   1.14  jdolecek void
    741   1.35   thorpej dqdone(void)
    742   1.14  jdolecek {
    743   1.47     pooka 
    744   1.55        ad 	pool_cache_destroy(dquot_cache);
    745   1.60        ad 	hashdone(dqhashtbl, HASH_LIST, dqhash);
    746   1.50   hannken 	cv_destroy(&dqcv);
    747   1.50   hannken 	mutex_destroy(&dqlock);
    748    1.1   mycroft }
    749    1.1   mycroft 
    750    1.1   mycroft /*
    751   1.69    bouyer  * Set up the quotas for an inode.
    752   1.69    bouyer  *
    753   1.69    bouyer  * This routine completely defines the semantics of quotas.
    754   1.69    bouyer  * If other criterion want to be used to establish quotas, the
    755   1.69    bouyer  * MAXQUOTAS value in quotas.h should be increased, and the
    756   1.69    bouyer  * additional dquots set up here.
    757   1.69    bouyer  */
    758   1.69    bouyer int
    759   1.69    bouyer getinoquota(struct inode *ip)
    760   1.69    bouyer {
    761   1.69    bouyer 	struct ufsmount *ump = ip->i_ump;
    762   1.69    bouyer 	struct vnode *vp = ITOV(ip);
    763   1.69    bouyer 	int i, error;
    764   1.69    bouyer 	u_int32_t ino_ids[MAXQUOTAS];
    765   1.69    bouyer 
    766   1.69    bouyer 	/*
    767   1.69    bouyer 	 * To avoid deadlocks never update quotas for quota files
    768   1.69    bouyer 	 * on the same file system
    769   1.69    bouyer 	 */
    770   1.69    bouyer 	for (i = 0; i < MAXQUOTAS; i++)
    771   1.69    bouyer 		if (vp == ump->um_quotas[i])
    772   1.69    bouyer 			return 0;
    773   1.69    bouyer 
    774   1.69    bouyer 	ino_ids[USRQUOTA] = ip->i_uid;
    775   1.69    bouyer 	ino_ids[GRPQUOTA] = ip->i_gid;
    776   1.69    bouyer 	for (i = 0; i < MAXQUOTAS; i++) {
    777   1.69    bouyer 		/*
    778   1.69    bouyer 		 * If the file id changed the quota needs update.
    779   1.69    bouyer 		 */
    780   1.69    bouyer 		if (ip->i_dquot[i] != NODQUOT &&
    781   1.69    bouyer 		    ip->i_dquot[i]->dq_id != ino_ids[i]) {
    782   1.69    bouyer 			dqrele(ITOV(ip), ip->i_dquot[i]);
    783   1.69    bouyer 			ip->i_dquot[i] = NODQUOT;
    784   1.69    bouyer 		}
    785   1.69    bouyer 		/*
    786   1.69    bouyer 		 * Set up the quota based on file id.
    787   1.69    bouyer 		 * ENODEV means that quotas are not enabled.
    788   1.69    bouyer 		 */
    789   1.69    bouyer 		if (ip->i_dquot[i] == NODQUOT &&
    790   1.69    bouyer 		    (error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
    791   1.69    bouyer 		    error != ENODEV)
    792   1.69    bouyer 			return (error);
    793   1.69    bouyer 	}
    794   1.69    bouyer 	return 0;
    795   1.69    bouyer }
    796   1.69    bouyer 
    797   1.69    bouyer /*
    798    1.1   mycroft  * Obtain a dquot structure for the specified identifier and quota file
    799    1.1   mycroft  * reading the information from the file if necessary.
    800    1.1   mycroft  */
    801   1.69    bouyer int
    802   1.35   thorpej dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
    803   1.35   thorpej     struct dquot **dqp)
    804    1.1   mycroft {
    805   1.50   hannken 	struct dquot *dq, *ndq;
    806   1.21       chs 	struct dqhashhead *dqh;
    807   1.11      fvdl 	struct vnode *dqvp;
    808   1.69    bouyer 	int error = 0; /* XXX gcc */
    809    1.1   mycroft 
    810   1.50   hannken 	/* Lock to see an up to date value for QTF_CLOSING. */
    811   1.50   hannken 	mutex_enter(&dqlock);
    812   1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0) {
    813   1.50   hannken 		mutex_exit(&dqlock);
    814    1.1   mycroft 		*dqp = NODQUOT;
    815   1.69    bouyer 		return (ENODEV);
    816   1.69    bouyer 	}
    817   1.69    bouyer 	dqvp = ump->um_quotas[type];
    818   1.69    bouyer #ifdef QUOTA
    819   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA) {
    820   1.69    bouyer 		if (dqvp == NULLVP || (ump->umq1_qflags[type] & QTF_CLOSING)) {
    821   1.69    bouyer 			mutex_exit(&dqlock);
    822   1.69    bouyer 			*dqp = NODQUOT;
    823   1.69    bouyer 			return (ENODEV);
    824   1.69    bouyer 		}
    825    1.1   mycroft 	}
    826   1.69    bouyer #endif
    827   1.69    bouyer #ifdef QUOTA2
    828   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2) {
    829   1.69    bouyer 		if (dqvp == NULLVP) {
    830   1.69    bouyer 			mutex_exit(&dqlock);
    831   1.69    bouyer 			*dqp = NODQUOT;
    832   1.69    bouyer 			return (ENODEV);
    833   1.69    bouyer 		}
    834   1.69    bouyer 	}
    835   1.69    bouyer #endif
    836   1.50   hannken 	KASSERT(dqvp != vp);
    837    1.1   mycroft 	/*
    838    1.1   mycroft 	 * Check the cache first.
    839    1.1   mycroft 	 */
    840   1.21       chs 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    841   1.21       chs 	LIST_FOREACH(dq, dqh, dq_hash) {
    842    1.1   mycroft 		if (dq->dq_id != id ||
    843    1.1   mycroft 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    844    1.1   mycroft 			continue;
    845   1.50   hannken 		KASSERT(dq->dq_cnt > 0);
    846   1.37       chs 		dqref(dq);
    847   1.50   hannken 		mutex_exit(&dqlock);
    848    1.1   mycroft 		*dqp = dq;
    849    1.1   mycroft 		return (0);
    850    1.1   mycroft 	}
    851    1.1   mycroft 	/*
    852    1.1   mycroft 	 * Not in cache, allocate a new one.
    853    1.1   mycroft 	 */
    854   1.50   hannken 	mutex_exit(&dqlock);
    855   1.55        ad 	ndq = pool_cache_get(dquot_cache, PR_WAITOK);
    856    1.1   mycroft 	/*
    857    1.1   mycroft 	 * Initialize the contents of the dquot structure.
    858    1.1   mycroft 	 */
    859   1.50   hannken 	memset((char *)ndq, 0, sizeof *ndq);
    860   1.50   hannken 	ndq->dq_flags = 0;
    861   1.50   hannken 	ndq->dq_id = id;
    862   1.50   hannken 	ndq->dq_ump = ump;
    863   1.50   hannken 	ndq->dq_type = type;
    864   1.50   hannken 	mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
    865   1.50   hannken 	mutex_enter(&dqlock);
    866   1.49   hannken 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    867   1.50   hannken 	LIST_FOREACH(dq, dqh, dq_hash) {
    868   1.50   hannken 		if (dq->dq_id != id ||
    869   1.50   hannken 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    870   1.50   hannken 			continue;
    871   1.50   hannken 		/*
    872   1.50   hannken 		 * Another thread beat us allocating this dquot.
    873   1.50   hannken 		 */
    874   1.50   hannken 		KASSERT(dq->dq_cnt > 0);
    875   1.50   hannken 		dqref(dq);
    876   1.50   hannken 		mutex_exit(&dqlock);
    877   1.64    bouyer 		mutex_destroy(&ndq->dq_interlock);
    878   1.55        ad 		pool_cache_put(dquot_cache, ndq);
    879   1.50   hannken 		*dqp = dq;
    880   1.50   hannken 		return 0;
    881   1.50   hannken 	}
    882   1.50   hannken 	dq = ndq;
    883    1.5   mycroft 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
    884   1.37       chs 	dqref(dq);
    885   1.50   hannken 	mutex_enter(&dq->dq_interlock);
    886   1.50   hannken 	mutex_exit(&dqlock);
    887   1.69    bouyer #ifdef QUOTA
    888   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA)
    889   1.69    bouyer 		error = dq1get(dqvp, id, ump, type, dq);
    890   1.69    bouyer #endif
    891   1.69    bouyer #ifdef QUOTA2
    892   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2)
    893   1.69    bouyer 		error = dq2get(dqvp, id, ump, type, dq);
    894   1.69    bouyer #endif
    895    1.1   mycroft 	/*
    896    1.1   mycroft 	 * I/O error in reading quota file, release
    897    1.1   mycroft 	 * quota structure and reflect problem to caller.
    898    1.1   mycroft 	 */
    899    1.1   mycroft 	if (error) {
    900   1.50   hannken 		mutex_enter(&dqlock);
    901    1.5   mycroft 		LIST_REMOVE(dq, dq_hash);
    902   1.50   hannken 		mutex_exit(&dqlock);
    903   1.49   hannken 		mutex_exit(&dq->dq_interlock);
    904    1.1   mycroft 		dqrele(vp, dq);
    905    1.1   mycroft 		*dqp = NODQUOT;
    906    1.1   mycroft 		return (error);
    907    1.1   mycroft 	}
    908   1.49   hannken 	mutex_exit(&dq->dq_interlock);
    909    1.1   mycroft 	*dqp = dq;
    910    1.1   mycroft 	return (0);
    911    1.1   mycroft }
    912    1.1   mycroft 
    913    1.1   mycroft /*
    914    1.1   mycroft  * Obtain a reference to a dquot.
    915    1.1   mycroft  */
    916   1.69    bouyer void
    917   1.35   thorpej dqref(struct dquot *dq)
    918    1.1   mycroft {
    919    1.1   mycroft 
    920   1.50   hannken 	KASSERT(mutex_owned(&dqlock));
    921    1.1   mycroft 	dq->dq_cnt++;
    922   1.46   hannken 	KASSERT(dq->dq_cnt > 0);
    923    1.1   mycroft }
    924    1.1   mycroft 
    925    1.1   mycroft /*
    926    1.1   mycroft  * Release a reference to a dquot.
    927    1.1   mycroft  */
    928   1.69    bouyer void
    929   1.35   thorpej dqrele(struct vnode *vp, struct dquot *dq)
    930    1.1   mycroft {
    931    1.1   mycroft 
    932    1.1   mycroft 	if (dq == NODQUOT)
    933    1.1   mycroft 		return;
    934   1.50   hannken 	mutex_enter(&dq->dq_interlock);
    935   1.50   hannken 	for (;;) {
    936   1.50   hannken 		mutex_enter(&dqlock);
    937   1.50   hannken 		if (dq->dq_cnt > 1) {
    938   1.50   hannken 			dq->dq_cnt--;
    939   1.50   hannken 			mutex_exit(&dqlock);
    940   1.50   hannken 			mutex_exit(&dq->dq_interlock);
    941   1.50   hannken 			return;
    942   1.50   hannken 		}
    943   1.50   hannken 		if ((dq->dq_flags & DQ_MOD) == 0)
    944   1.50   hannken 			break;
    945   1.50   hannken 		mutex_exit(&dqlock);
    946   1.69    bouyer #ifdef QUOTA
    947   1.69    bouyer 		if (dq->dq_ump->um_flags & UFS_QUOTA)
    948   1.69    bouyer 			(void) dq1sync(vp, dq);
    949   1.69    bouyer #endif
    950   1.69    bouyer #ifdef QUOTA2
    951   1.69    bouyer 		if (dq->dq_ump->um_flags & UFS_QUOTA2)
    952   1.69    bouyer 			(void) dq2sync(vp, dq);
    953   1.69    bouyer #endif
    954    1.1   mycroft 	}
    955   1.50   hannken 	KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
    956   1.50   hannken 	LIST_REMOVE(dq, dq_hash);
    957   1.50   hannken 	mutex_exit(&dqlock);
    958   1.50   hannken 	mutex_exit(&dq->dq_interlock);
    959   1.50   hannken 	mutex_destroy(&dq->dq_interlock);
    960   1.55        ad 	pool_cache_put(dquot_cache, dq);
    961    1.1   mycroft }
    962    1.1   mycroft 
    963   1.69    bouyer int
    964   1.69    bouyer qsync(struct mount *mp)
    965    1.1   mycroft {
    966   1.69    bouyer 	struct ufsmount *ump = VFSTOUFS(mp);
    967   1.69    bouyer #ifdef QUOTA
    968   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA)
    969   1.69    bouyer 		return q1sync(mp);
    970   1.69    bouyer #endif
    971   1.69    bouyer #ifdef QUOTA2
    972   1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2)
    973   1.69    bouyer 		return q2sync(mp);
    974   1.69    bouyer #endif
    975   1.69    bouyer 	return 0;
    976    1.1   mycroft }
    977    1.1   mycroft 
    978   1.50   hannken #ifdef DIAGNOSTIC
    979    1.1   mycroft /*
    980   1.50   hannken  * Check the hash chains for stray dquot's.
    981    1.1   mycroft  */
    982   1.69    bouyer void
    983   1.35   thorpej dqflush(struct vnode *vp)
    984    1.1   mycroft {
    985   1.50   hannken 	struct dquot *dq;
    986   1.50   hannken 	int i;
    987    1.1   mycroft 
    988   1.50   hannken 	mutex_enter(&dqlock);
    989   1.50   hannken 	for (i = 0; i <= dqhash; i++)
    990   1.50   hannken 		LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
    991   1.50   hannken 			KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
    992   1.50   hannken 	mutex_exit(&dqlock);
    993    1.1   mycroft }
    994   1.50   hannken #endif
    995