Home | History | Annotate | Line # | Download | only in lfs
ulfs_quota.c revision 1.6
      1  1.6  dholland /*	$NetBSD: ulfs_quota.c,v 1.6 2013/06/06 00:54:49 dholland Exp $	*/
      2  1.1  dholland /*  from NetBSD: ufs_quota.c,v 1.112 2012/09/09 04:27:49 manu Exp  */
      3  1.1  dholland 
      4  1.1  dholland /*
      5  1.1  dholland  * Copyright (c) 1982, 1986, 1990, 1993, 1995
      6  1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      7  1.1  dholland  *
      8  1.1  dholland  * This code is derived from software contributed to Berkeley by
      9  1.1  dholland  * Robert Elz at The University of Melbourne.
     10  1.1  dholland  *
     11  1.1  dholland  * Redistribution and use in source and binary forms, with or without
     12  1.1  dholland  * modification, are permitted provided that the following conditions
     13  1.1  dholland  * are met:
     14  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     15  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     16  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     19  1.1  dholland  * 3. Neither the name of the University nor the names of its contributors
     20  1.1  dholland  *    may be used to endorse or promote products derived from this software
     21  1.1  dholland  *    without specific prior written permission.
     22  1.1  dholland  *
     23  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  dholland  * SUCH DAMAGE.
     34  1.1  dholland  *
     35  1.1  dholland  *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
     36  1.1  dholland  */
     37  1.1  dholland 
     38  1.1  dholland #include <sys/cdefs.h>
     39  1.6  dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_quota.c,v 1.6 2013/06/06 00:54:49 dholland Exp $");
     40  1.1  dholland 
     41  1.1  dholland #if defined(_KERNEL_OPT)
     42  1.1  dholland #include "opt_quota.h"
     43  1.1  dholland #endif
     44  1.1  dholland #include <sys/param.h>
     45  1.1  dholland #include <sys/kernel.h>
     46  1.1  dholland #include <sys/systm.h>
     47  1.1  dholland #include <sys/namei.h>
     48  1.1  dholland #include <sys/file.h>
     49  1.1  dholland #include <sys/proc.h>
     50  1.1  dholland #include <sys/vnode.h>
     51  1.1  dholland #include <sys/mount.h>
     52  1.1  dholland #include <sys/kauth.h>
     53  1.1  dholland 
     54  1.1  dholland #include <sys/quotactl.h>
     55  1.2  dholland #include <ufs/lfs/ulfs_quotacommon.h>
     56  1.2  dholland #include <ufs/lfs/ulfs_inode.h>
     57  1.2  dholland #include <ufs/lfs/ulfsmount.h>
     58  1.2  dholland #include <ufs/lfs/ulfs_extern.h>
     59  1.2  dholland #include <ufs/lfs/ulfs_quota.h>
     60  1.1  dholland 
     61  1.5  dholland kmutex_t lfs_dqlock;
     62  1.5  dholland kcondvar_t lfs_dqcv;
     63  1.5  dholland const char *lfs_quotatypes[ULFS_MAXQUOTAS] = INITQFNAMES;
     64  1.1  dholland 
     65  1.1  dholland /*
     66  1.1  dholland  * Code pertaining to management of the in-core dquot data structures.
     67  1.1  dholland  */
     68  1.1  dholland #define DQHASH(dqvp, id) \
     69  1.1  dholland 	(((((long)(dqvp)) >> 8) + id) & dqhash)
     70  1.1  dholland static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
     71  1.1  dholland static u_long dqhash;
     72  1.1  dholland static pool_cache_t dquot_cache;
     73  1.1  dholland 
     74  1.1  dholland 
     75  1.1  dholland static int quota_handle_cmd_stat(struct mount *, struct lwp *,
     76  1.1  dholland     struct quotactl_args *args);
     77  1.1  dholland static int quota_handle_cmd_idtypestat(struct mount *, struct lwp *,
     78  1.1  dholland     struct quotactl_args *args);
     79  1.1  dholland static int quota_handle_cmd_objtypestat(struct mount *, struct lwp *,
     80  1.1  dholland     struct quotactl_args *args);
     81  1.1  dholland static int quota_handle_cmd_get(struct mount *, struct lwp *,
     82  1.1  dholland     struct quotactl_args *args);
     83  1.1  dholland static int quota_handle_cmd_put(struct mount *, struct lwp *,
     84  1.1  dholland     struct quotactl_args *args);
     85  1.1  dholland static int quota_handle_cmd_cursorget(struct mount *, struct lwp *,
     86  1.1  dholland     struct quotactl_args *args);
     87  1.1  dholland static int quota_handle_cmd_delete(struct mount *, struct lwp *,
     88  1.1  dholland     struct quotactl_args *args);
     89  1.1  dholland static int quota_handle_cmd_quotaon(struct mount *, struct lwp *,
     90  1.1  dholland     struct quotactl_args *args);
     91  1.1  dholland static int quota_handle_cmd_quotaoff(struct mount *, struct lwp *,
     92  1.1  dholland     struct quotactl_args *args);
     93  1.1  dholland static int quota_handle_cmd_cursoropen(struct mount *, struct lwp *,
     94  1.1  dholland     struct quotactl_args *args);
     95  1.1  dholland static int quota_handle_cmd_cursorclose(struct mount *, struct lwp *,
     96  1.1  dholland     struct quotactl_args *args);
     97  1.1  dholland static int quota_handle_cmd_cursorskipidtype(struct mount *, struct lwp *,
     98  1.1  dholland     struct quotactl_args *args);
     99  1.1  dholland static int quota_handle_cmd_cursoratend(struct mount *, struct lwp *,
    100  1.1  dholland     struct quotactl_args *args);
    101  1.1  dholland static int quota_handle_cmd_cursorrewind(struct mount *, struct lwp *,
    102  1.1  dholland     struct quotactl_args *args);
    103  1.1  dholland 
    104  1.1  dholland /*
    105  1.1  dholland  * Initialize the quota fields of an inode.
    106  1.1  dholland  */
    107  1.1  dholland void
    108  1.4  dholland ulfsquota_init(struct inode *ip)
    109  1.1  dholland {
    110  1.1  dholland 	int i;
    111  1.1  dholland 
    112  1.4  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++)
    113  1.1  dholland 		ip->i_dquot[i] = NODQUOT;
    114  1.1  dholland }
    115  1.1  dholland 
    116  1.1  dholland /*
    117  1.1  dholland  * Release the quota fields from an inode.
    118  1.1  dholland  */
    119  1.1  dholland void
    120  1.4  dholland ulfsquota_free(struct inode *ip)
    121  1.1  dholland {
    122  1.1  dholland 	int i;
    123  1.1  dholland 
    124  1.4  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    125  1.5  dholland 		lfs_dqrele(ITOV(ip), ip->i_dquot[i]);
    126  1.1  dholland 		ip->i_dquot[i] = NODQUOT;
    127  1.1  dholland 	}
    128  1.1  dholland }
    129  1.1  dholland 
    130  1.1  dholland /*
    131  1.1  dholland  * Update disk usage, and take corrective action.
    132  1.1  dholland  */
    133  1.1  dholland int
    134  1.5  dholland lfs_chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
    135  1.1  dholland {
    136  1.1  dholland 	/* do not track snapshot usage, or we will deadlock */
    137  1.1  dholland 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
    138  1.1  dholland 		return 0;
    139  1.1  dholland 
    140  1.3  dholland #ifdef LFS_QUOTA
    141  1.4  dholland 	if (ip->i_ump->um_flags & ULFS_QUOTA)
    142  1.5  dholland 		return lfs_chkdq1(ip, change, cred, flags);
    143  1.1  dholland #endif
    144  1.3  dholland #ifdef LFS_QUOTA2
    145  1.4  dholland 	if (ip->i_ump->um_flags & ULFS_QUOTA2)
    146  1.5  dholland 		return lfs_chkdq2(ip, change, cred, flags);
    147  1.1  dholland #endif
    148  1.1  dholland 	return 0;
    149  1.1  dholland }
    150  1.1  dholland 
    151  1.1  dholland /*
    152  1.1  dholland  * Check the inode limit, applying corrective action.
    153  1.1  dholland  */
    154  1.1  dholland int
    155  1.5  dholland lfs_chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
    156  1.1  dholland {
    157  1.1  dholland 	/* do not track snapshot usage, or we will deadlock */
    158  1.1  dholland 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
    159  1.1  dholland 		return 0;
    160  1.3  dholland #ifdef LFS_QUOTA
    161  1.4  dholland 	if (ip->i_ump->um_flags & ULFS_QUOTA)
    162  1.5  dholland 		return lfs_chkiq1(ip, change, cred, flags);
    163  1.1  dholland #endif
    164  1.3  dholland #ifdef LFS_QUOTA2
    165  1.4  dholland 	if (ip->i_ump->um_flags & ULFS_QUOTA2)
    166  1.5  dholland 		return lfs_chkiq2(ip, change, cred, flags);
    167  1.1  dholland #endif
    168  1.1  dholland 	return 0;
    169  1.1  dholland }
    170  1.1  dholland 
    171  1.1  dholland int
    172  1.5  dholland lfsquota_handle_cmd(struct mount *mp, struct lwp *l,
    173  1.1  dholland 		 struct quotactl_args *args)
    174  1.1  dholland {
    175  1.1  dholland 	int error = 0;
    176  1.1  dholland 
    177  1.1  dholland 	switch (args->qc_op) {
    178  1.1  dholland 	    case QUOTACTL_STAT:
    179  1.1  dholland 		error = quota_handle_cmd_stat(mp, l, args);
    180  1.1  dholland 		break;
    181  1.1  dholland 	    case QUOTACTL_IDTYPESTAT:
    182  1.1  dholland 		error = quota_handle_cmd_idtypestat(mp, l, args);
    183  1.1  dholland 		break;
    184  1.1  dholland 	    case QUOTACTL_OBJTYPESTAT:
    185  1.1  dholland 		error = quota_handle_cmd_objtypestat(mp, l, args);
    186  1.1  dholland 		break;
    187  1.1  dholland 	    case QUOTACTL_QUOTAON:
    188  1.1  dholland 		error = quota_handle_cmd_quotaon(mp, l, args);
    189  1.1  dholland 		break;
    190  1.1  dholland 	    case QUOTACTL_QUOTAOFF:
    191  1.1  dholland 		error = quota_handle_cmd_quotaoff(mp, l, args);
    192  1.1  dholland 		break;
    193  1.1  dholland 	    case QUOTACTL_GET:
    194  1.1  dholland 		error = quota_handle_cmd_get(mp, l, args);
    195  1.1  dholland 		break;
    196  1.1  dholland 	    case QUOTACTL_PUT:
    197  1.1  dholland 		error = quota_handle_cmd_put(mp, l, args);
    198  1.1  dholland 		break;
    199  1.1  dholland 	    case QUOTACTL_CURSORGET:
    200  1.1  dholland 		error = quota_handle_cmd_cursorget(mp, l, args);
    201  1.1  dholland 		break;
    202  1.1  dholland 	    case QUOTACTL_DELETE:
    203  1.1  dholland 		error = quota_handle_cmd_delete(mp, l, args);
    204  1.1  dholland 		break;
    205  1.1  dholland 	    case QUOTACTL_CURSOROPEN:
    206  1.1  dholland 		error = quota_handle_cmd_cursoropen(mp, l, args);
    207  1.1  dholland 		break;
    208  1.1  dholland 	    case QUOTACTL_CURSORCLOSE:
    209  1.1  dholland 		error = quota_handle_cmd_cursorclose(mp, l, args);
    210  1.1  dholland 		break;
    211  1.1  dholland 	    case QUOTACTL_CURSORSKIPIDTYPE:
    212  1.1  dholland 		error = quota_handle_cmd_cursorskipidtype(mp, l, args);
    213  1.1  dholland 		break;
    214  1.1  dholland 	    case QUOTACTL_CURSORATEND:
    215  1.1  dholland 		error = quota_handle_cmd_cursoratend(mp, l, args);
    216  1.1  dholland 		break;
    217  1.1  dholland 	    case QUOTACTL_CURSORREWIND:
    218  1.1  dholland 		error = quota_handle_cmd_cursorrewind(mp, l, args);
    219  1.1  dholland 		break;
    220  1.1  dholland 	    default:
    221  1.1  dholland 		panic("Invalid quotactl operation %d\n", args->qc_op);
    222  1.1  dholland 	}
    223  1.1  dholland 
    224  1.1  dholland 	return error;
    225  1.1  dholland }
    226  1.1  dholland 
    227  1.1  dholland static int
    228  1.1  dholland quota_handle_cmd_stat(struct mount *mp, struct lwp *l,
    229  1.1  dholland     struct quotactl_args *args)
    230  1.1  dholland {
    231  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    232  1.1  dholland 	struct quotastat *info;
    233  1.1  dholland 
    234  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_STAT);
    235  1.1  dholland 	info = args->u.stat.qc_info;
    236  1.1  dholland 
    237  1.4  dholland 	if ((ump->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    238  1.1  dholland 		return EOPNOTSUPP;
    239  1.1  dholland 
    240  1.3  dholland #ifdef LFS_QUOTA
    241  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA) {
    242  1.4  dholland 		strcpy(info->qs_implname, "lfs quota v1");
    243  1.4  dholland 		info->qs_numidtypes = ULFS_MAXQUOTAS;
    244  1.1  dholland 		/* XXX no define for this */
    245  1.1  dholland 		info->qs_numobjtypes = 2;
    246  1.1  dholland 		info->qs_restrictions = 0;
    247  1.1  dholland 		info->qs_restrictions |= QUOTA_RESTRICT_NEEDSQUOTACHECK;
    248  1.1  dholland 		info->qs_restrictions |= QUOTA_RESTRICT_UNIFORMGRACE;
    249  1.1  dholland 		info->qs_restrictions |= QUOTA_RESTRICT_32BIT;
    250  1.1  dholland 	} else
    251  1.1  dholland #endif
    252  1.3  dholland #ifdef LFS_QUOTA2
    253  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    254  1.4  dholland 		strcpy(info->qs_implname, "lfs quota v2");
    255  1.4  dholland 		info->qs_numidtypes = ULFS_MAXQUOTAS;
    256  1.1  dholland 		info->qs_numobjtypes = N_QL;
    257  1.1  dholland 		info->qs_restrictions = 0;
    258  1.1  dholland 	} else
    259  1.1  dholland #endif
    260  1.1  dholland 		return EOPNOTSUPP;
    261  1.1  dholland 
    262  1.1  dholland 	return 0;
    263  1.1  dholland }
    264  1.1  dholland 
    265  1.1  dholland static int
    266  1.1  dholland quota_handle_cmd_idtypestat(struct mount *mp, struct lwp *l,
    267  1.1  dholland     struct quotactl_args *args)
    268  1.1  dholland {
    269  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    270  1.1  dholland 	int idtype;
    271  1.1  dholland 	struct quotaidtypestat *info;
    272  1.1  dholland 	const char *name;
    273  1.1  dholland 
    274  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_IDTYPESTAT);
    275  1.1  dholland 	idtype = args->u.idtypestat.qc_idtype;
    276  1.1  dholland 	info = args->u.idtypestat.qc_info;
    277  1.1  dholland 
    278  1.4  dholland 	if ((ump->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    279  1.1  dholland 		return EOPNOTSUPP;
    280  1.1  dholland 
    281  1.1  dholland 	/*
    282  1.1  dholland 	 * These are the same for both QUOTA and QUOTA2.
    283  1.1  dholland 	 */
    284  1.1  dholland 	switch (idtype) {
    285  1.1  dholland 	    case QUOTA_IDTYPE_USER:
    286  1.1  dholland 		name = "user";
    287  1.1  dholland 		break;
    288  1.1  dholland 	    case QUOTA_IDTYPE_GROUP:
    289  1.1  dholland 		name = "group";
    290  1.1  dholland 		break;
    291  1.1  dholland 	    default:
    292  1.1  dholland 		return EINVAL;
    293  1.1  dholland 	}
    294  1.1  dholland 	strlcpy(info->qis_name, name, sizeof(info->qis_name));
    295  1.1  dholland 	return 0;
    296  1.1  dholland }
    297  1.1  dholland 
    298  1.1  dholland static int
    299  1.1  dholland quota_handle_cmd_objtypestat(struct mount *mp, struct lwp *l,
    300  1.1  dholland     struct quotactl_args *args)
    301  1.1  dholland {
    302  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    303  1.1  dholland 	int objtype;
    304  1.1  dholland 	struct quotaobjtypestat *info;
    305  1.1  dholland 	const char *name;
    306  1.1  dholland 	int isbytes;
    307  1.1  dholland 
    308  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_OBJTYPESTAT);
    309  1.1  dholland 	objtype = args->u.objtypestat.qc_objtype;
    310  1.1  dholland 	info = args->u.objtypestat.qc_info;
    311  1.1  dholland 
    312  1.4  dholland 	if ((ump->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    313  1.1  dholland 		return EOPNOTSUPP;
    314  1.1  dholland 
    315  1.1  dholland 	/*
    316  1.1  dholland 	 * These are the same for both QUOTA and QUOTA2.
    317  1.1  dholland 	 */
    318  1.1  dholland 	switch (objtype) {
    319  1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    320  1.1  dholland 		name = "block";
    321  1.1  dholland 		isbytes = 1;
    322  1.1  dholland 		break;
    323  1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    324  1.1  dholland 		name = "file";
    325  1.1  dholland 		isbytes = 0;
    326  1.1  dholland 		break;
    327  1.1  dholland 	    default:
    328  1.1  dholland 		return EINVAL;
    329  1.1  dholland 	}
    330  1.1  dholland 	strlcpy(info->qos_name, name, sizeof(info->qos_name));
    331  1.1  dholland 	info->qos_isbytes = isbytes;
    332  1.1  dholland 	return 0;
    333  1.1  dholland }
    334  1.1  dholland 
    335  1.1  dholland /* XXX shouldn't all this be in kauth ? */
    336  1.1  dholland static int
    337  1.1  dholland quota_get_auth(struct mount *mp, struct lwp *l, uid_t id) {
    338  1.1  dholland 	/* The user can always query about his own quota. */
    339  1.1  dholland 	if (id == kauth_cred_geteuid(l->l_cred))
    340  1.1  dholland 		return 0;
    341  1.1  dholland 	return kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    342  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(id), NULL);
    343  1.1  dholland }
    344  1.1  dholland 
    345  1.1  dholland static int
    346  1.1  dholland quota_handle_cmd_get(struct mount *mp, struct lwp *l,
    347  1.1  dholland     struct quotactl_args *args)
    348  1.1  dholland {
    349  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    350  1.1  dholland 	int error;
    351  1.1  dholland 	const struct quotakey *qk;
    352  1.1  dholland 	struct quotaval *qv;
    353  1.1  dholland 
    354  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_GET);
    355  1.1  dholland 	qk = args->u.get.qc_key;
    356  1.1  dholland 	qv = args->u.get.qc_val;
    357  1.1  dholland 
    358  1.4  dholland 	if ((ump->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    359  1.1  dholland 		return EOPNOTSUPP;
    360  1.1  dholland 
    361  1.1  dholland 	error = quota_get_auth(mp, l, qk->qk_id);
    362  1.1  dholland 	if (error != 0)
    363  1.1  dholland 		return error;
    364  1.3  dholland #ifdef LFS_QUOTA
    365  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA) {
    366  1.5  dholland 		error = lfsquota1_handle_cmd_get(ump, qk, qv);
    367  1.1  dholland 	} else
    368  1.1  dholland #endif
    369  1.3  dholland #ifdef LFS_QUOTA2
    370  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    371  1.5  dholland 		error = lfsquota2_handle_cmd_get(ump, qk, qv);
    372  1.1  dholland 	} else
    373  1.1  dholland #endif
    374  1.1  dholland 		panic("quota_handle_cmd_get: no support ?");
    375  1.1  dholland 
    376  1.1  dholland 	if (error != 0)
    377  1.1  dholland 		return error;
    378  1.1  dholland 
    379  1.1  dholland 	return error;
    380  1.1  dholland }
    381  1.1  dholland 
    382  1.1  dholland static int
    383  1.1  dholland quota_handle_cmd_put(struct mount *mp, struct lwp *l,
    384  1.1  dholland     struct quotactl_args *args)
    385  1.1  dholland {
    386  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    387  1.1  dholland 	const struct quotakey *qk;
    388  1.1  dholland 	const struct quotaval *qv;
    389  1.1  dholland 	id_t kauth_id;
    390  1.1  dholland 	int error;
    391  1.1  dholland 
    392  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_PUT);
    393  1.1  dholland 	qk = args->u.put.qc_key;
    394  1.1  dholland 	qv = args->u.put.qc_val;
    395  1.1  dholland 
    396  1.4  dholland 	if ((ump->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    397  1.1  dholland 		return EOPNOTSUPP;
    398  1.1  dholland 
    399  1.1  dholland 	kauth_id = qk->qk_id;
    400  1.1  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    401  1.1  dholland 		kauth_id = 0;
    402  1.1  dholland 	}
    403  1.1  dholland 
    404  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    405  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    406  1.1  dholland 	    NULL);
    407  1.1  dholland 	if (error != 0) {
    408  1.1  dholland 		return error;
    409  1.1  dholland 	}
    410  1.1  dholland 
    411  1.3  dholland #ifdef LFS_QUOTA
    412  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA)
    413  1.5  dholland 		error = lfsquota1_handle_cmd_put(ump, qk, qv);
    414  1.1  dholland 	else
    415  1.1  dholland #endif
    416  1.3  dholland #ifdef LFS_QUOTA2
    417  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    418  1.5  dholland 		error = lfsquota2_handle_cmd_put(ump, qk, qv);
    419  1.1  dholland 	} else
    420  1.1  dholland #endif
    421  1.1  dholland 		panic("quota_handle_cmd_get: no support ?");
    422  1.1  dholland 
    423  1.1  dholland 	if (error == ENOENT) {
    424  1.1  dholland 		error = 0;
    425  1.1  dholland 	}
    426  1.1  dholland 
    427  1.1  dholland 	return error;
    428  1.1  dholland }
    429  1.1  dholland 
    430  1.1  dholland static int
    431  1.1  dholland quota_handle_cmd_delete(struct mount *mp, struct lwp *l,
    432  1.1  dholland     struct quotactl_args *args)
    433  1.1  dholland {
    434  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    435  1.1  dholland 	const struct quotakey *qk;
    436  1.1  dholland 	id_t kauth_id;
    437  1.1  dholland 	int error;
    438  1.1  dholland 
    439  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_DELETE);
    440  1.1  dholland 	qk = args->u.delete.qc_key;
    441  1.1  dholland 
    442  1.1  dholland 	kauth_id = qk->qk_id;
    443  1.1  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    444  1.1  dholland 		kauth_id = 0;
    445  1.1  dholland 	}
    446  1.1  dholland 
    447  1.4  dholland 	if ((ump->um_flags & ULFS_QUOTA2) == 0)
    448  1.1  dholland 		return EOPNOTSUPP;
    449  1.1  dholland 
    450  1.1  dholland 	/* avoid whitespace changes */
    451  1.1  dholland 	{
    452  1.1  dholland 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    453  1.1  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    454  1.1  dholland 		    NULL);
    455  1.1  dholland 		if (error != 0)
    456  1.1  dholland 			goto err;
    457  1.3  dholland #ifdef LFS_QUOTA2
    458  1.4  dholland 		if (ump->um_flags & ULFS_QUOTA2) {
    459  1.5  dholland 			error = lfsquota2_handle_cmd_delete(ump, qk);
    460  1.1  dholland 		} else
    461  1.1  dholland #endif
    462  1.1  dholland 			panic("quota_handle_cmd_get: no support ?");
    463  1.1  dholland 
    464  1.1  dholland 		if (error && error != ENOENT)
    465  1.1  dholland 			goto err;
    466  1.1  dholland 	}
    467  1.1  dholland 
    468  1.1  dholland 	return 0;
    469  1.1  dholland  err:
    470  1.1  dholland 	return error;
    471  1.1  dholland }
    472  1.1  dholland 
    473  1.1  dholland static int
    474  1.1  dholland quota_handle_cmd_cursorget(struct mount *mp, struct lwp *l,
    475  1.1  dholland     struct quotactl_args *args)
    476  1.1  dholland {
    477  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    478  1.1  dholland 	struct quotakcursor *cursor;
    479  1.1  dholland 	struct quotakey *keys;
    480  1.1  dholland 	struct quotaval *vals;
    481  1.1  dholland 	unsigned maxnum;
    482  1.1  dholland 	unsigned *ret;
    483  1.1  dholland 	int error;
    484  1.1  dholland 
    485  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORGET);
    486  1.1  dholland 	cursor = args->u.cursorget.qc_cursor;
    487  1.1  dholland 	keys = args->u.cursorget.qc_keys;
    488  1.1  dholland 	vals = args->u.cursorget.qc_vals;
    489  1.1  dholland 	maxnum = args->u.cursorget.qc_maxnum;
    490  1.1  dholland 	ret = args->u.cursorget.qc_ret;
    491  1.1  dholland 
    492  1.4  dholland 	if ((ump->um_flags & ULFS_QUOTA2) == 0)
    493  1.1  dholland 		return EOPNOTSUPP;
    494  1.1  dholland 
    495  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    496  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    497  1.1  dholland 	if (error)
    498  1.1  dholland 		return error;
    499  1.1  dholland 
    500  1.3  dholland #ifdef LFS_QUOTA2
    501  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    502  1.5  dholland 		error = lfsquota2_handle_cmd_cursorget(ump, cursor, keys, vals,
    503  1.1  dholland 						    maxnum, ret);
    504  1.1  dholland 	} else
    505  1.1  dholland #endif
    506  1.1  dholland 		panic("quota_handle_cmd_cursorget: no support ?");
    507  1.1  dholland 
    508  1.1  dholland 	return error;
    509  1.1  dholland }
    510  1.1  dholland 
    511  1.1  dholland static int
    512  1.1  dholland quota_handle_cmd_cursoropen(struct mount *mp, struct lwp *l,
    513  1.1  dholland     struct quotactl_args *args)
    514  1.1  dholland {
    515  1.3  dholland #ifdef LFS_QUOTA2
    516  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    517  1.1  dholland #endif
    518  1.1  dholland 	struct quotakcursor *cursor;
    519  1.1  dholland 	int error;
    520  1.1  dholland 
    521  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSOROPEN);
    522  1.1  dholland 	cursor = args->u.cursoropen.qc_cursor;
    523  1.1  dholland 
    524  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    525  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    526  1.1  dholland 	if (error)
    527  1.1  dholland 		return error;
    528  1.1  dholland 
    529  1.3  dholland #ifdef LFS_QUOTA2
    530  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    531  1.5  dholland 		error = lfsquota2_handle_cmd_cursoropen(ump, cursor);
    532  1.1  dholland 	} else
    533  1.1  dholland #endif
    534  1.1  dholland 		error = EOPNOTSUPP;
    535  1.1  dholland 
    536  1.1  dholland 	return error;
    537  1.1  dholland }
    538  1.1  dholland 
    539  1.1  dholland static int
    540  1.1  dholland quota_handle_cmd_cursorclose(struct mount *mp, struct lwp *l,
    541  1.1  dholland     struct quotactl_args *args)
    542  1.1  dholland {
    543  1.3  dholland #ifdef LFS_QUOTA2
    544  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    545  1.1  dholland #endif
    546  1.1  dholland 	struct quotakcursor *cursor;
    547  1.1  dholland 	int error;
    548  1.1  dholland 
    549  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORCLOSE);
    550  1.1  dholland 	cursor = args->u.cursorclose.qc_cursor;
    551  1.1  dholland 
    552  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    553  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    554  1.1  dholland 	if (error)
    555  1.1  dholland 		return error;
    556  1.1  dholland 
    557  1.3  dholland #ifdef LFS_QUOTA2
    558  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    559  1.5  dholland 		error = lfsquota2_handle_cmd_cursorclose(ump, cursor);
    560  1.1  dholland 	} else
    561  1.1  dholland #endif
    562  1.1  dholland 		error = EOPNOTSUPP;
    563  1.1  dholland 
    564  1.1  dholland 	return error;
    565  1.1  dholland }
    566  1.1  dholland 
    567  1.1  dholland static int
    568  1.1  dholland quota_handle_cmd_cursorskipidtype(struct mount *mp, struct lwp *l,
    569  1.1  dholland     struct quotactl_args *args)
    570  1.1  dholland {
    571  1.3  dholland #ifdef LFS_QUOTA2
    572  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    573  1.1  dholland #endif
    574  1.1  dholland 	struct quotakcursor *cursor;
    575  1.1  dholland 	int idtype;
    576  1.1  dholland 	int error;
    577  1.1  dholland 
    578  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORSKIPIDTYPE);
    579  1.1  dholland 	cursor = args->u.cursorskipidtype.qc_cursor;
    580  1.1  dholland 	idtype = args->u.cursorskipidtype.qc_idtype;
    581  1.1  dholland 
    582  1.3  dholland #ifdef LFS_QUOTA2
    583  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    584  1.5  dholland 		error = lfsquota2_handle_cmd_cursorskipidtype(ump, cursor, idtype);
    585  1.1  dholland 	} else
    586  1.1  dholland #endif
    587  1.1  dholland 		error = EOPNOTSUPP;
    588  1.1  dholland 
    589  1.1  dholland 	return error;
    590  1.1  dholland }
    591  1.1  dholland 
    592  1.1  dholland static int
    593  1.1  dholland quota_handle_cmd_cursoratend(struct mount *mp, struct lwp *l,
    594  1.1  dholland     struct quotactl_args *args)
    595  1.1  dholland {
    596  1.3  dholland #ifdef LFS_QUOTA2
    597  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    598  1.1  dholland #endif
    599  1.1  dholland 	struct quotakcursor *cursor;
    600  1.1  dholland 	int *ret;
    601  1.1  dholland 	int error;
    602  1.1  dholland 
    603  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORATEND);
    604  1.1  dholland 	cursor = args->u.cursoratend.qc_cursor;
    605  1.1  dholland 	ret = args->u.cursoratend.qc_ret;
    606  1.1  dholland 
    607  1.3  dholland #ifdef LFS_QUOTA2
    608  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    609  1.5  dholland 		error = lfsquota2_handle_cmd_cursoratend(ump, cursor, ret);
    610  1.1  dholland 	} else
    611  1.1  dholland #endif
    612  1.1  dholland 		error = EOPNOTSUPP;
    613  1.1  dholland 
    614  1.1  dholland 	return error;
    615  1.1  dholland }
    616  1.1  dholland 
    617  1.1  dholland static int
    618  1.1  dholland quota_handle_cmd_cursorrewind(struct mount *mp, struct lwp *l,
    619  1.1  dholland     struct quotactl_args *args)
    620  1.1  dholland {
    621  1.3  dholland #ifdef LFS_QUOTA2
    622  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    623  1.1  dholland #endif
    624  1.1  dholland 	struct quotakcursor *cursor;
    625  1.1  dholland 	int error;
    626  1.1  dholland 
    627  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORREWIND);
    628  1.1  dholland 	cursor = args->u.cursorrewind.qc_cursor;
    629  1.1  dholland 
    630  1.3  dholland #ifdef LFS_QUOTA2
    631  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    632  1.5  dholland 		error = lfsquota2_handle_cmd_cursorrewind(ump, cursor);
    633  1.1  dholland 	} else
    634  1.1  dholland #endif
    635  1.1  dholland 		error = EOPNOTSUPP;
    636  1.1  dholland 
    637  1.1  dholland 	return error;
    638  1.1  dholland }
    639  1.1  dholland 
    640  1.1  dholland static int
    641  1.1  dholland quota_handle_cmd_quotaon(struct mount *mp, struct lwp *l,
    642  1.1  dholland     struct quotactl_args *args)
    643  1.1  dholland {
    644  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    645  1.1  dholland 	int idtype;
    646  1.1  dholland 	const char *qfile;
    647  1.1  dholland 	int error;
    648  1.1  dholland 
    649  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_QUOTAON);
    650  1.1  dholland 	idtype = args->u.quotaon.qc_idtype;
    651  1.1  dholland 	qfile = args->u.quotaon.qc_quotafile;
    652  1.1  dholland 
    653  1.4  dholland 	if ((ump->um_flags & ULFS_QUOTA2) != 0)
    654  1.1  dholland 		return EBUSY;
    655  1.1  dholland 
    656  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    657  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    658  1.1  dholland 	if (error != 0) {
    659  1.1  dholland 		return error;
    660  1.1  dholland 	}
    661  1.3  dholland #ifdef LFS_QUOTA
    662  1.5  dholland 	error = lfsquota1_handle_cmd_quotaon(l, ump, idtype, qfile);
    663  1.1  dholland #else
    664  1.1  dholland 	error = EOPNOTSUPP;
    665  1.1  dholland #endif
    666  1.1  dholland 
    667  1.1  dholland 	return error;
    668  1.1  dholland }
    669  1.1  dholland 
    670  1.1  dholland static int
    671  1.1  dholland quota_handle_cmd_quotaoff(struct mount *mp, struct lwp *l,
    672  1.1  dholland     struct quotactl_args *args)
    673  1.1  dholland {
    674  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    675  1.1  dholland 	int idtype;
    676  1.1  dholland 	int error;
    677  1.1  dholland 
    678  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_QUOTAOFF);
    679  1.1  dholland 	idtype = args->u.quotaoff.qc_idtype;
    680  1.1  dholland 
    681  1.4  dholland 	if ((ump->um_flags & ULFS_QUOTA2) != 0)
    682  1.1  dholland 		return EOPNOTSUPP;
    683  1.1  dholland 
    684  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    685  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    686  1.1  dholland 	if (error != 0) {
    687  1.1  dholland 		return error;
    688  1.1  dholland 	}
    689  1.3  dholland #ifdef LFS_QUOTA
    690  1.5  dholland 	error = lfsquota1_handle_cmd_quotaoff(l, ump, idtype);
    691  1.1  dholland #else
    692  1.1  dholland 	error = EOPNOTSUPP;
    693  1.1  dholland #endif
    694  1.1  dholland 
    695  1.1  dholland 	return error;
    696  1.1  dholland }
    697  1.1  dholland 
    698  1.1  dholland /*
    699  1.1  dholland  * Initialize the quota system.
    700  1.1  dholland  */
    701  1.1  dholland void
    702  1.5  dholland lfs_dqinit(void)
    703  1.1  dholland {
    704  1.1  dholland 
    705  1.5  dholland 	mutex_init(&lfs_dqlock, MUTEX_DEFAULT, IPL_NONE);
    706  1.5  dholland 	cv_init(&lfs_dqcv, "quota");
    707  1.1  dholland 	dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
    708  1.4  dholland 	dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "lfsdq",
    709  1.1  dholland 	    NULL, IPL_NONE, NULL, NULL, NULL);
    710  1.1  dholland }
    711  1.1  dholland 
    712  1.1  dholland void
    713  1.5  dholland lfs_dqreinit(void)
    714  1.1  dholland {
    715  1.1  dholland 	struct dquot *dq;
    716  1.1  dholland 	struct dqhashhead *oldhash, *hash;
    717  1.1  dholland 	struct vnode *dqvp;
    718  1.1  dholland 	u_long oldmask, mask, hashval;
    719  1.1  dholland 	int i;
    720  1.1  dholland 
    721  1.1  dholland 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    722  1.5  dholland 	mutex_enter(&lfs_dqlock);
    723  1.1  dholland 	oldhash = dqhashtbl;
    724  1.1  dholland 	oldmask = dqhash;
    725  1.1  dholland 	dqhashtbl = hash;
    726  1.1  dholland 	dqhash = mask;
    727  1.1  dholland 	for (i = 0; i <= oldmask; i++) {
    728  1.1  dholland 		while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
    729  1.1  dholland 			dqvp = dq->dq_ump->um_quotas[dq->dq_type];
    730  1.1  dholland 			LIST_REMOVE(dq, dq_hash);
    731  1.1  dholland 			hashval = DQHASH(dqvp, dq->dq_id);
    732  1.1  dholland 			LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
    733  1.1  dholland 		}
    734  1.1  dholland 	}
    735  1.5  dholland 	mutex_exit(&lfs_dqlock);
    736  1.1  dholland 	hashdone(oldhash, HASH_LIST, oldmask);
    737  1.1  dholland }
    738  1.1  dholland 
    739  1.1  dholland /*
    740  1.1  dholland  * Free resources held by quota system.
    741  1.1  dholland  */
    742  1.1  dholland void
    743  1.5  dholland lfs_dqdone(void)
    744  1.1  dholland {
    745  1.1  dholland 
    746  1.1  dholland 	pool_cache_destroy(dquot_cache);
    747  1.1  dholland 	hashdone(dqhashtbl, HASH_LIST, dqhash);
    748  1.5  dholland 	cv_destroy(&lfs_dqcv);
    749  1.5  dholland 	mutex_destroy(&lfs_dqlock);
    750  1.1  dholland }
    751  1.1  dholland 
    752  1.1  dholland /*
    753  1.1  dholland  * Set up the quotas for an inode.
    754  1.1  dholland  *
    755  1.1  dholland  * This routine completely defines the semantics of quotas.
    756  1.4  dholland  * If other criteria want to be used to establish quotas, the
    757  1.4  dholland  * ULFS_MAXQUOTAS value in quotas.h should be increased, and the
    758  1.1  dholland  * additional dquots set up here.
    759  1.1  dholland  */
    760  1.1  dholland int
    761  1.5  dholland lfs_getinoquota(struct inode *ip)
    762  1.1  dholland {
    763  1.4  dholland 	struct ulfsmount *ump = ip->i_ump;
    764  1.1  dholland 	struct vnode *vp = ITOV(ip);
    765  1.1  dholland 	int i, error;
    766  1.4  dholland 	u_int32_t ino_ids[ULFS_MAXQUOTAS];
    767  1.1  dholland 
    768  1.1  dholland 	/*
    769  1.1  dholland 	 * To avoid deadlocks never update quotas for quota files
    770  1.1  dholland 	 * on the same file system
    771  1.1  dholland 	 */
    772  1.4  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++)
    773  1.1  dholland 		if (vp == ump->um_quotas[i])
    774  1.1  dholland 			return 0;
    775  1.1  dholland 
    776  1.4  dholland 	ino_ids[ULFS_USRQUOTA] = ip->i_uid;
    777  1.4  dholland 	ino_ids[ULFS_GRPQUOTA] = ip->i_gid;
    778  1.4  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    779  1.1  dholland 		/*
    780  1.1  dholland 		 * If the file id changed the quota needs update.
    781  1.1  dholland 		 */
    782  1.1  dholland 		if (ip->i_dquot[i] != NODQUOT &&
    783  1.1  dholland 		    ip->i_dquot[i]->dq_id != ino_ids[i]) {
    784  1.5  dholland 			lfs_dqrele(ITOV(ip), ip->i_dquot[i]);
    785  1.1  dholland 			ip->i_dquot[i] = NODQUOT;
    786  1.1  dholland 		}
    787  1.1  dholland 		/*
    788  1.1  dholland 		 * Set up the quota based on file id.
    789  1.1  dholland 		 * ENODEV means that quotas are not enabled.
    790  1.1  dholland 		 */
    791  1.1  dholland 		if (ip->i_dquot[i] == NODQUOT &&
    792  1.5  dholland 		    (error = lfs_dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
    793  1.1  dholland 		    error != ENODEV)
    794  1.1  dholland 			return (error);
    795  1.1  dholland 	}
    796  1.1  dholland 	return 0;
    797  1.1  dholland }
    798  1.1  dholland 
    799  1.1  dholland /*
    800  1.1  dholland  * Obtain a dquot structure for the specified identifier and quota file
    801  1.1  dholland  * reading the information from the file if necessary.
    802  1.1  dholland  */
    803  1.1  dholland int
    804  1.5  dholland lfs_dqget(struct vnode *vp, u_long id, struct ulfsmount *ump, int type,
    805  1.1  dholland     struct dquot **dqp)
    806  1.1  dholland {
    807  1.1  dholland 	struct dquot *dq, *ndq;
    808  1.1  dholland 	struct dqhashhead *dqh;
    809  1.1  dholland 	struct vnode *dqvp;
    810  1.1  dholland 	int error = 0; /* XXX gcc */
    811  1.1  dholland 
    812  1.1  dholland 	/* Lock to see an up to date value for QTF_CLOSING. */
    813  1.5  dholland 	mutex_enter(&lfs_dqlock);
    814  1.4  dholland 	if ((ump->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0) {
    815  1.5  dholland 		mutex_exit(&lfs_dqlock);
    816  1.1  dholland 		*dqp = NODQUOT;
    817  1.1  dholland 		return (ENODEV);
    818  1.1  dholland 	}
    819  1.1  dholland 	dqvp = ump->um_quotas[type];
    820  1.3  dholland #ifdef LFS_QUOTA
    821  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA) {
    822  1.1  dholland 		if (dqvp == NULLVP || (ump->umq1_qflags[type] & QTF_CLOSING)) {
    823  1.5  dholland 			mutex_exit(&lfs_dqlock);
    824  1.1  dholland 			*dqp = NODQUOT;
    825  1.1  dholland 			return (ENODEV);
    826  1.1  dholland 		}
    827  1.1  dholland 	}
    828  1.1  dholland #endif
    829  1.3  dholland #ifdef LFS_QUOTA2
    830  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2) {
    831  1.1  dholland 		if (dqvp == NULLVP) {
    832  1.5  dholland 			mutex_exit(&lfs_dqlock);
    833  1.1  dholland 			*dqp = NODQUOT;
    834  1.1  dholland 			return (ENODEV);
    835  1.1  dholland 		}
    836  1.1  dholland 	}
    837  1.1  dholland #endif
    838  1.1  dholland 	KASSERT(dqvp != vp);
    839  1.1  dholland 	/*
    840  1.1  dholland 	 * Check the cache first.
    841  1.1  dholland 	 */
    842  1.1  dholland 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    843  1.1  dholland 	LIST_FOREACH(dq, dqh, dq_hash) {
    844  1.1  dholland 		if (dq->dq_id != id ||
    845  1.1  dholland 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    846  1.1  dholland 			continue;
    847  1.1  dholland 		KASSERT(dq->dq_cnt > 0);
    848  1.5  dholland 		lfs_dqref(dq);
    849  1.5  dholland 		mutex_exit(&lfs_dqlock);
    850  1.1  dholland 		*dqp = dq;
    851  1.1  dholland 		return (0);
    852  1.1  dholland 	}
    853  1.1  dholland 	/*
    854  1.1  dholland 	 * Not in cache, allocate a new one.
    855  1.1  dholland 	 */
    856  1.5  dholland 	mutex_exit(&lfs_dqlock);
    857  1.1  dholland 	ndq = pool_cache_get(dquot_cache, PR_WAITOK);
    858  1.1  dholland 	/*
    859  1.1  dholland 	 * Initialize the contents of the dquot structure.
    860  1.1  dholland 	 */
    861  1.1  dholland 	memset((char *)ndq, 0, sizeof *ndq);
    862  1.1  dholland 	ndq->dq_flags = 0;
    863  1.1  dholland 	ndq->dq_id = id;
    864  1.1  dholland 	ndq->dq_ump = ump;
    865  1.1  dholland 	ndq->dq_type = type;
    866  1.1  dholland 	mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
    867  1.5  dholland 	mutex_enter(&lfs_dqlock);
    868  1.1  dholland 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    869  1.1  dholland 	LIST_FOREACH(dq, dqh, dq_hash) {
    870  1.1  dholland 		if (dq->dq_id != id ||
    871  1.1  dholland 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    872  1.1  dholland 			continue;
    873  1.1  dholland 		/*
    874  1.1  dholland 		 * Another thread beat us allocating this dquot.
    875  1.1  dholland 		 */
    876  1.1  dholland 		KASSERT(dq->dq_cnt > 0);
    877  1.5  dholland 		lfs_dqref(dq);
    878  1.5  dholland 		mutex_exit(&lfs_dqlock);
    879  1.1  dholland 		mutex_destroy(&ndq->dq_interlock);
    880  1.1  dholland 		pool_cache_put(dquot_cache, ndq);
    881  1.1  dholland 		*dqp = dq;
    882  1.1  dholland 		return 0;
    883  1.1  dholland 	}
    884  1.1  dholland 	dq = ndq;
    885  1.1  dholland 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
    886  1.5  dholland 	lfs_dqref(dq);
    887  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    888  1.5  dholland 	mutex_exit(&lfs_dqlock);
    889  1.3  dholland #ifdef LFS_QUOTA
    890  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA)
    891  1.5  dholland 		error = lfs_dq1get(dqvp, id, ump, type, dq);
    892  1.1  dholland #endif
    893  1.3  dholland #ifdef LFS_QUOTA2
    894  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2)
    895  1.5  dholland 		error = lfs_dq2get(dqvp, id, ump, type, dq);
    896  1.1  dholland #endif
    897  1.1  dholland 	/*
    898  1.1  dholland 	 * I/O error in reading quota file, release
    899  1.1  dholland 	 * quota structure and reflect problem to caller.
    900  1.1  dholland 	 */
    901  1.1  dholland 	if (error) {
    902  1.5  dholland 		mutex_enter(&lfs_dqlock);
    903  1.1  dholland 		LIST_REMOVE(dq, dq_hash);
    904  1.5  dholland 		mutex_exit(&lfs_dqlock);
    905  1.1  dholland 		mutex_exit(&dq->dq_interlock);
    906  1.5  dholland 		lfs_dqrele(vp, dq);
    907  1.1  dholland 		*dqp = NODQUOT;
    908  1.1  dholland 		return (error);
    909  1.1  dholland 	}
    910  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    911  1.1  dholland 	*dqp = dq;
    912  1.1  dholland 	return (0);
    913  1.1  dholland }
    914  1.1  dholland 
    915  1.1  dholland /*
    916  1.1  dholland  * Obtain a reference to a dquot.
    917  1.1  dholland  */
    918  1.1  dholland void
    919  1.5  dholland lfs_dqref(struct dquot *dq)
    920  1.1  dholland {
    921  1.1  dholland 
    922  1.5  dholland 	KASSERT(mutex_owned(&lfs_dqlock));
    923  1.1  dholland 	dq->dq_cnt++;
    924  1.1  dholland 	KASSERT(dq->dq_cnt > 0);
    925  1.1  dholland }
    926  1.1  dholland 
    927  1.1  dholland /*
    928  1.1  dholland  * Release a reference to a dquot.
    929  1.1  dholland  */
    930  1.1  dholland void
    931  1.5  dholland lfs_dqrele(struct vnode *vp, struct dquot *dq)
    932  1.1  dholland {
    933  1.1  dholland 
    934  1.1  dholland 	if (dq == NODQUOT)
    935  1.1  dholland 		return;
    936  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    937  1.1  dholland 	for (;;) {
    938  1.5  dholland 		mutex_enter(&lfs_dqlock);
    939  1.1  dholland 		if (dq->dq_cnt > 1) {
    940  1.1  dholland 			dq->dq_cnt--;
    941  1.5  dholland 			mutex_exit(&lfs_dqlock);
    942  1.1  dholland 			mutex_exit(&dq->dq_interlock);
    943  1.1  dholland 			return;
    944  1.1  dholland 		}
    945  1.1  dholland 		if ((dq->dq_flags & DQ_MOD) == 0)
    946  1.1  dholland 			break;
    947  1.5  dholland 		mutex_exit(&lfs_dqlock);
    948  1.3  dholland #ifdef LFS_QUOTA
    949  1.4  dholland 		if (dq->dq_ump->um_flags & ULFS_QUOTA)
    950  1.5  dholland 			(void) lfs_dq1sync(vp, dq);
    951  1.1  dholland #endif
    952  1.3  dholland #ifdef LFS_QUOTA2
    953  1.4  dholland 		if (dq->dq_ump->um_flags & ULFS_QUOTA2)
    954  1.5  dholland 			(void) lfs_dq2sync(vp, dq);
    955  1.1  dholland #endif
    956  1.1  dholland 	}
    957  1.1  dholland 	KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
    958  1.1  dholland 	LIST_REMOVE(dq, dq_hash);
    959  1.5  dholland 	mutex_exit(&lfs_dqlock);
    960  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    961  1.1  dholland 	mutex_destroy(&dq->dq_interlock);
    962  1.1  dholland 	pool_cache_put(dquot_cache, dq);
    963  1.1  dholland }
    964  1.1  dholland 
    965  1.1  dholland int
    966  1.5  dholland lfs_qsync(struct mount *mp)
    967  1.1  dholland {
    968  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    969  1.6  dholland 
    970  1.6  dholland 	/* avoid compiler warning when quotas aren't enabled */
    971  1.6  dholland 	(void)ump;
    972  1.6  dholland 
    973  1.3  dholland #ifdef LFS_QUOTA
    974  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA)
    975  1.5  dholland 		return lfs_q1sync(mp);
    976  1.1  dholland #endif
    977  1.3  dholland #ifdef LFS_QUOTA2
    978  1.4  dholland 	if (ump->um_flags & ULFS_QUOTA2)
    979  1.5  dholland 		return lfs_q2sync(mp);
    980  1.1  dholland #endif
    981  1.1  dholland 	return 0;
    982  1.1  dholland }
    983  1.1  dholland 
    984  1.1  dholland #ifdef DIAGNOSTIC
    985  1.1  dholland /*
    986  1.1  dholland  * Check the hash chains for stray dquot's.
    987  1.1  dholland  */
    988  1.1  dholland void
    989  1.5  dholland lfs_dqflush(struct vnode *vp)
    990  1.1  dholland {
    991  1.1  dholland 	struct dquot *dq;
    992  1.1  dholland 	int i;
    993  1.1  dholland 
    994  1.5  dholland 	mutex_enter(&lfs_dqlock);
    995  1.1  dholland 	for (i = 0; i <= dqhash; i++)
    996  1.1  dholland 		LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
    997  1.1  dholland 			KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
    998  1.5  dholland 	mutex_exit(&lfs_dqlock);
    999  1.1  dholland }
   1000  1.1  dholland #endif
   1001