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