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