Home | History | Annotate | Line # | Download | only in lfs
ulfs_quota.c revision 1.7
      1  1.7  dholland /*	$NetBSD: ulfs_quota.c,v 1.7 2013/07/28 01:10: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.7  dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_quota.c,v 1.7 2013/07/28 01:10: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.7  dholland 	if (ip->i_lfs->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.7  dholland 	if (ip->i_lfs->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.7  dholland 	if (ip->i_lfs->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.7  dholland 	if (ip->i_lfs->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.7  dholland 	struct lfs *fs = ump->um_lfs;
    233  1.1  dholland 	struct quotastat *info;
    234  1.1  dholland 
    235  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_STAT);
    236  1.1  dholland 	info = args->u.stat.qc_info;
    237  1.1  dholland 
    238  1.7  dholland 	if ((fs->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    239  1.1  dholland 		return EOPNOTSUPP;
    240  1.1  dholland 
    241  1.3  dholland #ifdef LFS_QUOTA
    242  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA) {
    243  1.4  dholland 		strcpy(info->qs_implname, "lfs quota v1");
    244  1.4  dholland 		info->qs_numidtypes = ULFS_MAXQUOTAS;
    245  1.1  dholland 		/* XXX no define for this */
    246  1.1  dholland 		info->qs_numobjtypes = 2;
    247  1.1  dholland 		info->qs_restrictions = 0;
    248  1.1  dholland 		info->qs_restrictions |= QUOTA_RESTRICT_NEEDSQUOTACHECK;
    249  1.1  dholland 		info->qs_restrictions |= QUOTA_RESTRICT_UNIFORMGRACE;
    250  1.1  dholland 		info->qs_restrictions |= QUOTA_RESTRICT_32BIT;
    251  1.1  dholland 	} else
    252  1.1  dholland #endif
    253  1.3  dholland #ifdef LFS_QUOTA2
    254  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    255  1.4  dholland 		strcpy(info->qs_implname, "lfs quota v2");
    256  1.4  dholland 		info->qs_numidtypes = ULFS_MAXQUOTAS;
    257  1.1  dholland 		info->qs_numobjtypes = N_QL;
    258  1.1  dholland 		info->qs_restrictions = 0;
    259  1.1  dholland 	} else
    260  1.1  dholland #endif
    261  1.1  dholland 		return EOPNOTSUPP;
    262  1.1  dholland 
    263  1.1  dholland 	return 0;
    264  1.1  dholland }
    265  1.1  dholland 
    266  1.1  dholland static int
    267  1.1  dholland quota_handle_cmd_idtypestat(struct mount *mp, struct lwp *l,
    268  1.1  dholland     struct quotactl_args *args)
    269  1.1  dholland {
    270  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    271  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    272  1.1  dholland 	int idtype;
    273  1.1  dholland 	struct quotaidtypestat *info;
    274  1.1  dholland 	const char *name;
    275  1.1  dholland 
    276  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_IDTYPESTAT);
    277  1.1  dholland 	idtype = args->u.idtypestat.qc_idtype;
    278  1.1  dholland 	info = args->u.idtypestat.qc_info;
    279  1.1  dholland 
    280  1.7  dholland 	if ((fs->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    281  1.1  dholland 		return EOPNOTSUPP;
    282  1.1  dholland 
    283  1.1  dholland 	/*
    284  1.1  dholland 	 * These are the same for both QUOTA and QUOTA2.
    285  1.1  dholland 	 */
    286  1.1  dholland 	switch (idtype) {
    287  1.1  dholland 	    case QUOTA_IDTYPE_USER:
    288  1.1  dholland 		name = "user";
    289  1.1  dholland 		break;
    290  1.1  dholland 	    case QUOTA_IDTYPE_GROUP:
    291  1.1  dholland 		name = "group";
    292  1.1  dholland 		break;
    293  1.1  dholland 	    default:
    294  1.1  dholland 		return EINVAL;
    295  1.1  dholland 	}
    296  1.1  dholland 	strlcpy(info->qis_name, name, sizeof(info->qis_name));
    297  1.1  dholland 	return 0;
    298  1.1  dholland }
    299  1.1  dholland 
    300  1.1  dholland static int
    301  1.1  dholland quota_handle_cmd_objtypestat(struct mount *mp, struct lwp *l,
    302  1.1  dholland     struct quotactl_args *args)
    303  1.1  dholland {
    304  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    305  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    306  1.1  dholland 	int objtype;
    307  1.1  dholland 	struct quotaobjtypestat *info;
    308  1.1  dholland 	const char *name;
    309  1.1  dholland 	int isbytes;
    310  1.1  dholland 
    311  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_OBJTYPESTAT);
    312  1.1  dholland 	objtype = args->u.objtypestat.qc_objtype;
    313  1.1  dholland 	info = args->u.objtypestat.qc_info;
    314  1.1  dholland 
    315  1.7  dholland 	if ((fs->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    316  1.1  dholland 		return EOPNOTSUPP;
    317  1.1  dholland 
    318  1.1  dholland 	/*
    319  1.1  dholland 	 * These are the same for both QUOTA and QUOTA2.
    320  1.1  dholland 	 */
    321  1.1  dholland 	switch (objtype) {
    322  1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    323  1.1  dholland 		name = "block";
    324  1.1  dholland 		isbytes = 1;
    325  1.1  dholland 		break;
    326  1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    327  1.1  dholland 		name = "file";
    328  1.1  dholland 		isbytes = 0;
    329  1.1  dholland 		break;
    330  1.1  dholland 	    default:
    331  1.1  dholland 		return EINVAL;
    332  1.1  dholland 	}
    333  1.1  dholland 	strlcpy(info->qos_name, name, sizeof(info->qos_name));
    334  1.1  dholland 	info->qos_isbytes = isbytes;
    335  1.1  dholland 	return 0;
    336  1.1  dholland }
    337  1.1  dholland 
    338  1.1  dholland /* XXX shouldn't all this be in kauth ? */
    339  1.1  dholland static int
    340  1.1  dholland quota_get_auth(struct mount *mp, struct lwp *l, uid_t id) {
    341  1.1  dholland 	/* The user can always query about his own quota. */
    342  1.1  dholland 	if (id == kauth_cred_geteuid(l->l_cred))
    343  1.1  dholland 		return 0;
    344  1.1  dholland 	return kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    345  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(id), NULL);
    346  1.1  dholland }
    347  1.1  dholland 
    348  1.1  dholland static int
    349  1.1  dholland quota_handle_cmd_get(struct mount *mp, struct lwp *l,
    350  1.1  dholland     struct quotactl_args *args)
    351  1.1  dholland {
    352  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    353  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    354  1.1  dholland 	int error;
    355  1.1  dholland 	const struct quotakey *qk;
    356  1.1  dholland 	struct quotaval *qv;
    357  1.1  dholland 
    358  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_GET);
    359  1.1  dholland 	qk = args->u.get.qc_key;
    360  1.1  dholland 	qv = args->u.get.qc_val;
    361  1.1  dholland 
    362  1.7  dholland 	if ((fs->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    363  1.1  dholland 		return EOPNOTSUPP;
    364  1.1  dholland 
    365  1.1  dholland 	error = quota_get_auth(mp, l, qk->qk_id);
    366  1.1  dholland 	if (error != 0)
    367  1.1  dholland 		return error;
    368  1.3  dholland #ifdef LFS_QUOTA
    369  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA) {
    370  1.5  dholland 		error = lfsquota1_handle_cmd_get(ump, qk, qv);
    371  1.1  dholland 	} else
    372  1.1  dholland #endif
    373  1.3  dholland #ifdef LFS_QUOTA2
    374  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    375  1.5  dholland 		error = lfsquota2_handle_cmd_get(ump, qk, qv);
    376  1.1  dholland 	} else
    377  1.1  dholland #endif
    378  1.1  dholland 		panic("quota_handle_cmd_get: no support ?");
    379  1.1  dholland 
    380  1.1  dholland 	if (error != 0)
    381  1.1  dholland 		return error;
    382  1.1  dholland 
    383  1.1  dholland 	return error;
    384  1.1  dholland }
    385  1.1  dholland 
    386  1.1  dholland static int
    387  1.1  dholland quota_handle_cmd_put(struct mount *mp, struct lwp *l,
    388  1.1  dholland     struct quotactl_args *args)
    389  1.1  dholland {
    390  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    391  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    392  1.1  dholland 	const struct quotakey *qk;
    393  1.1  dholland 	const struct quotaval *qv;
    394  1.1  dholland 	id_t kauth_id;
    395  1.1  dholland 	int error;
    396  1.1  dholland 
    397  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_PUT);
    398  1.1  dholland 	qk = args->u.put.qc_key;
    399  1.1  dholland 	qv = args->u.put.qc_val;
    400  1.1  dholland 
    401  1.7  dholland 	if ((fs->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0)
    402  1.1  dholland 		return EOPNOTSUPP;
    403  1.1  dholland 
    404  1.1  dholland 	kauth_id = qk->qk_id;
    405  1.1  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    406  1.1  dholland 		kauth_id = 0;
    407  1.1  dholland 	}
    408  1.1  dholland 
    409  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    410  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    411  1.1  dholland 	    NULL);
    412  1.1  dholland 	if (error != 0) {
    413  1.1  dholland 		return error;
    414  1.1  dholland 	}
    415  1.1  dholland 
    416  1.3  dholland #ifdef LFS_QUOTA
    417  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA)
    418  1.5  dholland 		error = lfsquota1_handle_cmd_put(ump, qk, qv);
    419  1.1  dholland 	else
    420  1.1  dholland #endif
    421  1.3  dholland #ifdef LFS_QUOTA2
    422  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    423  1.5  dholland 		error = lfsquota2_handle_cmd_put(ump, qk, qv);
    424  1.1  dholland 	} else
    425  1.1  dholland #endif
    426  1.1  dholland 		panic("quota_handle_cmd_get: no support ?");
    427  1.1  dholland 
    428  1.1  dholland 	if (error == ENOENT) {
    429  1.1  dholland 		error = 0;
    430  1.1  dholland 	}
    431  1.1  dholland 
    432  1.1  dholland 	return error;
    433  1.1  dholland }
    434  1.1  dholland 
    435  1.1  dholland static int
    436  1.1  dholland quota_handle_cmd_delete(struct mount *mp, struct lwp *l,
    437  1.1  dholland     struct quotactl_args *args)
    438  1.1  dholland {
    439  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    440  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    441  1.1  dholland 	const struct quotakey *qk;
    442  1.1  dholland 	id_t kauth_id;
    443  1.1  dholland 	int error;
    444  1.1  dholland 
    445  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_DELETE);
    446  1.1  dholland 	qk = args->u.delete.qc_key;
    447  1.1  dholland 
    448  1.1  dholland 	kauth_id = qk->qk_id;
    449  1.1  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    450  1.1  dholland 		kauth_id = 0;
    451  1.1  dholland 	}
    452  1.1  dholland 
    453  1.7  dholland 	if ((fs->um_flags & ULFS_QUOTA2) == 0)
    454  1.1  dholland 		return EOPNOTSUPP;
    455  1.1  dholland 
    456  1.1  dholland 	/* avoid whitespace changes */
    457  1.1  dholland 	{
    458  1.1  dholland 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    459  1.1  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    460  1.1  dholland 		    NULL);
    461  1.1  dholland 		if (error != 0)
    462  1.1  dholland 			goto err;
    463  1.3  dholland #ifdef LFS_QUOTA2
    464  1.7  dholland 		if (fs->um_flags & ULFS_QUOTA2) {
    465  1.5  dholland 			error = lfsquota2_handle_cmd_delete(ump, qk);
    466  1.1  dholland 		} else
    467  1.1  dholland #endif
    468  1.1  dholland 			panic("quota_handle_cmd_get: no support ?");
    469  1.1  dholland 
    470  1.1  dholland 		if (error && error != ENOENT)
    471  1.1  dholland 			goto err;
    472  1.1  dholland 	}
    473  1.1  dholland 
    474  1.1  dholland 	return 0;
    475  1.1  dholland  err:
    476  1.1  dholland 	return error;
    477  1.1  dholland }
    478  1.1  dholland 
    479  1.1  dholland static int
    480  1.1  dholland quota_handle_cmd_cursorget(struct mount *mp, struct lwp *l,
    481  1.1  dholland     struct quotactl_args *args)
    482  1.1  dholland {
    483  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    484  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    485  1.1  dholland 	struct quotakcursor *cursor;
    486  1.1  dholland 	struct quotakey *keys;
    487  1.1  dholland 	struct quotaval *vals;
    488  1.1  dholland 	unsigned maxnum;
    489  1.1  dholland 	unsigned *ret;
    490  1.1  dholland 	int error;
    491  1.1  dholland 
    492  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORGET);
    493  1.1  dholland 	cursor = args->u.cursorget.qc_cursor;
    494  1.1  dholland 	keys = args->u.cursorget.qc_keys;
    495  1.1  dholland 	vals = args->u.cursorget.qc_vals;
    496  1.1  dholland 	maxnum = args->u.cursorget.qc_maxnum;
    497  1.1  dholland 	ret = args->u.cursorget.qc_ret;
    498  1.1  dholland 
    499  1.7  dholland 	if ((fs->um_flags & ULFS_QUOTA2) == 0)
    500  1.1  dholland 		return EOPNOTSUPP;
    501  1.1  dholland 
    502  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    503  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    504  1.1  dholland 	if (error)
    505  1.1  dholland 		return error;
    506  1.1  dholland 
    507  1.3  dholland #ifdef LFS_QUOTA2
    508  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    509  1.5  dholland 		error = lfsquota2_handle_cmd_cursorget(ump, cursor, keys, vals,
    510  1.1  dholland 						    maxnum, ret);
    511  1.1  dholland 	} else
    512  1.1  dholland #endif
    513  1.1  dholland 		panic("quota_handle_cmd_cursorget: no support ?");
    514  1.1  dholland 
    515  1.1  dholland 	return error;
    516  1.1  dholland }
    517  1.1  dholland 
    518  1.1  dholland static int
    519  1.1  dholland quota_handle_cmd_cursoropen(struct mount *mp, struct lwp *l,
    520  1.1  dholland     struct quotactl_args *args)
    521  1.1  dholland {
    522  1.3  dholland #ifdef LFS_QUOTA2
    523  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    524  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    525  1.1  dholland #endif
    526  1.1  dholland 	struct quotakcursor *cursor;
    527  1.1  dholland 	int error;
    528  1.1  dholland 
    529  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSOROPEN);
    530  1.1  dholland 	cursor = args->u.cursoropen.qc_cursor;
    531  1.1  dholland 
    532  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    533  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    534  1.1  dholland 	if (error)
    535  1.1  dholland 		return error;
    536  1.1  dholland 
    537  1.3  dholland #ifdef LFS_QUOTA2
    538  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    539  1.5  dholland 		error = lfsquota2_handle_cmd_cursoropen(ump, cursor);
    540  1.1  dholland 	} else
    541  1.1  dholland #endif
    542  1.1  dholland 		error = EOPNOTSUPP;
    543  1.1  dholland 
    544  1.1  dholland 	return error;
    545  1.1  dholland }
    546  1.1  dholland 
    547  1.1  dholland static int
    548  1.1  dholland quota_handle_cmd_cursorclose(struct mount *mp, struct lwp *l,
    549  1.1  dholland     struct quotactl_args *args)
    550  1.1  dholland {
    551  1.3  dholland #ifdef LFS_QUOTA2
    552  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    553  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    554  1.1  dholland #endif
    555  1.1  dholland 	struct quotakcursor *cursor;
    556  1.1  dholland 	int error;
    557  1.1  dholland 
    558  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORCLOSE);
    559  1.1  dholland 	cursor = args->u.cursorclose.qc_cursor;
    560  1.1  dholland 
    561  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    562  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    563  1.1  dholland 	if (error)
    564  1.1  dholland 		return error;
    565  1.1  dholland 
    566  1.3  dholland #ifdef LFS_QUOTA2
    567  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    568  1.5  dholland 		error = lfsquota2_handle_cmd_cursorclose(ump, cursor);
    569  1.1  dholland 	} else
    570  1.1  dholland #endif
    571  1.1  dholland 		error = EOPNOTSUPP;
    572  1.1  dholland 
    573  1.1  dholland 	return error;
    574  1.1  dholland }
    575  1.1  dholland 
    576  1.1  dholland static int
    577  1.1  dholland quota_handle_cmd_cursorskipidtype(struct mount *mp, struct lwp *l,
    578  1.1  dholland     struct quotactl_args *args)
    579  1.1  dholland {
    580  1.3  dholland #ifdef LFS_QUOTA2
    581  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    582  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    583  1.1  dholland #endif
    584  1.1  dholland 	struct quotakcursor *cursor;
    585  1.1  dholland 	int idtype;
    586  1.1  dholland 	int error;
    587  1.1  dholland 
    588  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORSKIPIDTYPE);
    589  1.1  dholland 	cursor = args->u.cursorskipidtype.qc_cursor;
    590  1.1  dholland 	idtype = args->u.cursorskipidtype.qc_idtype;
    591  1.1  dholland 
    592  1.3  dholland #ifdef LFS_QUOTA2
    593  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    594  1.5  dholland 		error = lfsquota2_handle_cmd_cursorskipidtype(ump, cursor, idtype);
    595  1.1  dholland 	} else
    596  1.1  dholland #endif
    597  1.1  dholland 		error = EOPNOTSUPP;
    598  1.1  dholland 
    599  1.1  dholland 	return error;
    600  1.1  dholland }
    601  1.1  dholland 
    602  1.1  dholland static int
    603  1.1  dholland quota_handle_cmd_cursoratend(struct mount *mp, struct lwp *l,
    604  1.1  dholland     struct quotactl_args *args)
    605  1.1  dholland {
    606  1.3  dholland #ifdef LFS_QUOTA2
    607  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    608  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    609  1.1  dholland #endif
    610  1.1  dholland 	struct quotakcursor *cursor;
    611  1.1  dholland 	int *ret;
    612  1.1  dholland 	int error;
    613  1.1  dholland 
    614  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORATEND);
    615  1.1  dholland 	cursor = args->u.cursoratend.qc_cursor;
    616  1.1  dholland 	ret = args->u.cursoratend.qc_ret;
    617  1.1  dholland 
    618  1.3  dholland #ifdef LFS_QUOTA2
    619  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    620  1.5  dholland 		error = lfsquota2_handle_cmd_cursoratend(ump, cursor, ret);
    621  1.1  dholland 	} else
    622  1.1  dholland #endif
    623  1.1  dholland 		error = EOPNOTSUPP;
    624  1.1  dholland 
    625  1.1  dholland 	return error;
    626  1.1  dholland }
    627  1.1  dholland 
    628  1.1  dholland static int
    629  1.1  dholland quota_handle_cmd_cursorrewind(struct mount *mp, struct lwp *l,
    630  1.1  dholland     struct quotactl_args *args)
    631  1.1  dholland {
    632  1.3  dholland #ifdef LFS_QUOTA2
    633  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    634  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    635  1.1  dholland #endif
    636  1.1  dholland 	struct quotakcursor *cursor;
    637  1.1  dholland 	int error;
    638  1.1  dholland 
    639  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_CURSORREWIND);
    640  1.1  dholland 	cursor = args->u.cursorrewind.qc_cursor;
    641  1.1  dholland 
    642  1.3  dholland #ifdef LFS_QUOTA2
    643  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    644  1.5  dholland 		error = lfsquota2_handle_cmd_cursorrewind(ump, cursor);
    645  1.1  dholland 	} else
    646  1.1  dholland #endif
    647  1.1  dholland 		error = EOPNOTSUPP;
    648  1.1  dholland 
    649  1.1  dholland 	return error;
    650  1.1  dholland }
    651  1.1  dholland 
    652  1.1  dholland static int
    653  1.1  dholland quota_handle_cmd_quotaon(struct mount *mp, struct lwp *l,
    654  1.1  dholland     struct quotactl_args *args)
    655  1.1  dholland {
    656  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    657  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    658  1.1  dholland 	int idtype;
    659  1.1  dholland 	const char *qfile;
    660  1.1  dholland 	int error;
    661  1.1  dholland 
    662  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_QUOTAON);
    663  1.1  dholland 	idtype = args->u.quotaon.qc_idtype;
    664  1.1  dholland 	qfile = args->u.quotaon.qc_quotafile;
    665  1.1  dholland 
    666  1.7  dholland 	if ((fs->um_flags & ULFS_QUOTA2) != 0)
    667  1.1  dholland 		return EBUSY;
    668  1.1  dholland 
    669  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    670  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    671  1.1  dholland 	if (error != 0) {
    672  1.1  dholland 		return error;
    673  1.1  dholland 	}
    674  1.3  dholland #ifdef LFS_QUOTA
    675  1.5  dholland 	error = lfsquota1_handle_cmd_quotaon(l, ump, idtype, qfile);
    676  1.1  dholland #else
    677  1.1  dholland 	error = EOPNOTSUPP;
    678  1.1  dholland #endif
    679  1.1  dholland 
    680  1.1  dholland 	return error;
    681  1.1  dholland }
    682  1.1  dholland 
    683  1.1  dholland static int
    684  1.1  dholland quota_handle_cmd_quotaoff(struct mount *mp, struct lwp *l,
    685  1.1  dholland     struct quotactl_args *args)
    686  1.1  dholland {
    687  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    688  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    689  1.1  dholland 	int idtype;
    690  1.1  dholland 	int error;
    691  1.1  dholland 
    692  1.1  dholland 	KASSERT(args->qc_op == QUOTACTL_QUOTAOFF);
    693  1.1  dholland 	idtype = args->u.quotaoff.qc_idtype;
    694  1.1  dholland 
    695  1.7  dholland 	if ((fs->um_flags & ULFS_QUOTA2) != 0)
    696  1.1  dholland 		return EOPNOTSUPP;
    697  1.1  dholland 
    698  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    699  1.1  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    700  1.1  dholland 	if (error != 0) {
    701  1.1  dholland 		return error;
    702  1.1  dholland 	}
    703  1.3  dholland #ifdef LFS_QUOTA
    704  1.5  dholland 	error = lfsquota1_handle_cmd_quotaoff(l, ump, idtype);
    705  1.1  dholland #else
    706  1.1  dholland 	error = EOPNOTSUPP;
    707  1.1  dholland #endif
    708  1.1  dholland 
    709  1.1  dholland 	return error;
    710  1.1  dholland }
    711  1.1  dholland 
    712  1.1  dholland /*
    713  1.1  dholland  * Initialize the quota system.
    714  1.1  dholland  */
    715  1.1  dholland void
    716  1.5  dholland lfs_dqinit(void)
    717  1.1  dholland {
    718  1.1  dholland 
    719  1.5  dholland 	mutex_init(&lfs_dqlock, MUTEX_DEFAULT, IPL_NONE);
    720  1.5  dholland 	cv_init(&lfs_dqcv, "quota");
    721  1.1  dholland 	dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
    722  1.4  dholland 	dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "lfsdq",
    723  1.1  dholland 	    NULL, IPL_NONE, NULL, NULL, NULL);
    724  1.1  dholland }
    725  1.1  dholland 
    726  1.1  dholland void
    727  1.5  dholland lfs_dqreinit(void)
    728  1.1  dholland {
    729  1.1  dholland 	struct dquot *dq;
    730  1.1  dholland 	struct dqhashhead *oldhash, *hash;
    731  1.1  dholland 	struct vnode *dqvp;
    732  1.1  dholland 	u_long oldmask, mask, hashval;
    733  1.1  dholland 	int i;
    734  1.1  dholland 
    735  1.1  dholland 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    736  1.5  dholland 	mutex_enter(&lfs_dqlock);
    737  1.1  dholland 	oldhash = dqhashtbl;
    738  1.1  dholland 	oldmask = dqhash;
    739  1.1  dholland 	dqhashtbl = hash;
    740  1.1  dholland 	dqhash = mask;
    741  1.1  dholland 	for (i = 0; i <= oldmask; i++) {
    742  1.1  dholland 		while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
    743  1.1  dholland 			dqvp = dq->dq_ump->um_quotas[dq->dq_type];
    744  1.1  dholland 			LIST_REMOVE(dq, dq_hash);
    745  1.1  dholland 			hashval = DQHASH(dqvp, dq->dq_id);
    746  1.1  dholland 			LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
    747  1.1  dholland 		}
    748  1.1  dholland 	}
    749  1.5  dholland 	mutex_exit(&lfs_dqlock);
    750  1.1  dholland 	hashdone(oldhash, HASH_LIST, oldmask);
    751  1.1  dholland }
    752  1.1  dholland 
    753  1.1  dholland /*
    754  1.1  dholland  * Free resources held by quota system.
    755  1.1  dholland  */
    756  1.1  dholland void
    757  1.5  dholland lfs_dqdone(void)
    758  1.1  dholland {
    759  1.1  dholland 
    760  1.1  dholland 	pool_cache_destroy(dquot_cache);
    761  1.1  dholland 	hashdone(dqhashtbl, HASH_LIST, dqhash);
    762  1.5  dholland 	cv_destroy(&lfs_dqcv);
    763  1.5  dholland 	mutex_destroy(&lfs_dqlock);
    764  1.1  dholland }
    765  1.1  dholland 
    766  1.1  dholland /*
    767  1.1  dholland  * Set up the quotas for an inode.
    768  1.1  dholland  *
    769  1.1  dholland  * This routine completely defines the semantics of quotas.
    770  1.4  dholland  * If other criteria want to be used to establish quotas, the
    771  1.4  dholland  * ULFS_MAXQUOTAS value in quotas.h should be increased, and the
    772  1.1  dholland  * additional dquots set up here.
    773  1.1  dholland  */
    774  1.1  dholland int
    775  1.5  dholland lfs_getinoquota(struct inode *ip)
    776  1.1  dholland {
    777  1.4  dholland 	struct ulfsmount *ump = ip->i_ump;
    778  1.7  dholland 	//struct lfs *fs = ump->um_lfs; // notyet
    779  1.1  dholland 	struct vnode *vp = ITOV(ip);
    780  1.1  dholland 	int i, error;
    781  1.4  dholland 	u_int32_t ino_ids[ULFS_MAXQUOTAS];
    782  1.1  dholland 
    783  1.1  dholland 	/*
    784  1.1  dholland 	 * To avoid deadlocks never update quotas for quota files
    785  1.1  dholland 	 * on the same file system
    786  1.1  dholland 	 */
    787  1.4  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++)
    788  1.1  dholland 		if (vp == ump->um_quotas[i])
    789  1.1  dholland 			return 0;
    790  1.1  dholland 
    791  1.4  dholland 	ino_ids[ULFS_USRQUOTA] = ip->i_uid;
    792  1.4  dholland 	ino_ids[ULFS_GRPQUOTA] = ip->i_gid;
    793  1.4  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    794  1.1  dholland 		/*
    795  1.1  dholland 		 * If the file id changed the quota needs update.
    796  1.1  dholland 		 */
    797  1.1  dholland 		if (ip->i_dquot[i] != NODQUOT &&
    798  1.1  dholland 		    ip->i_dquot[i]->dq_id != ino_ids[i]) {
    799  1.5  dholland 			lfs_dqrele(ITOV(ip), ip->i_dquot[i]);
    800  1.1  dholland 			ip->i_dquot[i] = NODQUOT;
    801  1.1  dholland 		}
    802  1.1  dholland 		/*
    803  1.1  dholland 		 * Set up the quota based on file id.
    804  1.1  dholland 		 * ENODEV means that quotas are not enabled.
    805  1.1  dholland 		 */
    806  1.1  dholland 		if (ip->i_dquot[i] == NODQUOT &&
    807  1.5  dholland 		    (error = lfs_dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
    808  1.1  dholland 		    error != ENODEV)
    809  1.1  dholland 			return (error);
    810  1.1  dholland 	}
    811  1.1  dholland 	return 0;
    812  1.1  dholland }
    813  1.1  dholland 
    814  1.1  dholland /*
    815  1.1  dholland  * Obtain a dquot structure for the specified identifier and quota file
    816  1.1  dholland  * reading the information from the file if necessary.
    817  1.1  dholland  */
    818  1.1  dholland int
    819  1.5  dholland lfs_dqget(struct vnode *vp, u_long id, struct ulfsmount *ump, int type,
    820  1.1  dholland     struct dquot **dqp)
    821  1.1  dholland {
    822  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    823  1.1  dholland 	struct dquot *dq, *ndq;
    824  1.1  dholland 	struct dqhashhead *dqh;
    825  1.1  dholland 	struct vnode *dqvp;
    826  1.1  dholland 	int error = 0; /* XXX gcc */
    827  1.1  dholland 
    828  1.1  dholland 	/* Lock to see an up to date value for QTF_CLOSING. */
    829  1.5  dholland 	mutex_enter(&lfs_dqlock);
    830  1.7  dholland 	if ((fs->um_flags & (ULFS_QUOTA|ULFS_QUOTA2)) == 0) {
    831  1.5  dholland 		mutex_exit(&lfs_dqlock);
    832  1.1  dholland 		*dqp = NODQUOT;
    833  1.1  dholland 		return (ENODEV);
    834  1.1  dholland 	}
    835  1.1  dholland 	dqvp = ump->um_quotas[type];
    836  1.3  dholland #ifdef LFS_QUOTA
    837  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA) {
    838  1.1  dholland 		if (dqvp == NULLVP || (ump->umq1_qflags[type] & QTF_CLOSING)) {
    839  1.5  dholland 			mutex_exit(&lfs_dqlock);
    840  1.1  dholland 			*dqp = NODQUOT;
    841  1.1  dholland 			return (ENODEV);
    842  1.1  dholland 		}
    843  1.1  dholland 	}
    844  1.1  dholland #endif
    845  1.3  dholland #ifdef LFS_QUOTA2
    846  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    847  1.1  dholland 		if (dqvp == NULLVP) {
    848  1.5  dholland 			mutex_exit(&lfs_dqlock);
    849  1.1  dholland 			*dqp = NODQUOT;
    850  1.1  dholland 			return (ENODEV);
    851  1.1  dholland 		}
    852  1.1  dholland 	}
    853  1.1  dholland #endif
    854  1.1  dholland 	KASSERT(dqvp != vp);
    855  1.1  dholland 	/*
    856  1.1  dholland 	 * Check the cache first.
    857  1.1  dholland 	 */
    858  1.1  dholland 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    859  1.1  dholland 	LIST_FOREACH(dq, dqh, dq_hash) {
    860  1.1  dholland 		if (dq->dq_id != id ||
    861  1.1  dholland 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    862  1.1  dholland 			continue;
    863  1.1  dholland 		KASSERT(dq->dq_cnt > 0);
    864  1.5  dholland 		lfs_dqref(dq);
    865  1.5  dholland 		mutex_exit(&lfs_dqlock);
    866  1.1  dholland 		*dqp = dq;
    867  1.1  dholland 		return (0);
    868  1.1  dholland 	}
    869  1.1  dholland 	/*
    870  1.1  dholland 	 * Not in cache, allocate a new one.
    871  1.1  dholland 	 */
    872  1.5  dholland 	mutex_exit(&lfs_dqlock);
    873  1.1  dholland 	ndq = pool_cache_get(dquot_cache, PR_WAITOK);
    874  1.1  dholland 	/*
    875  1.1  dholland 	 * Initialize the contents of the dquot structure.
    876  1.1  dholland 	 */
    877  1.1  dholland 	memset((char *)ndq, 0, sizeof *ndq);
    878  1.1  dholland 	ndq->dq_flags = 0;
    879  1.1  dholland 	ndq->dq_id = id;
    880  1.1  dholland 	ndq->dq_ump = ump;
    881  1.1  dholland 	ndq->dq_type = type;
    882  1.1  dholland 	mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
    883  1.5  dholland 	mutex_enter(&lfs_dqlock);
    884  1.1  dholland 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    885  1.1  dholland 	LIST_FOREACH(dq, dqh, dq_hash) {
    886  1.1  dholland 		if (dq->dq_id != id ||
    887  1.1  dholland 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    888  1.1  dholland 			continue;
    889  1.1  dholland 		/*
    890  1.1  dholland 		 * Another thread beat us allocating this dquot.
    891  1.1  dholland 		 */
    892  1.1  dholland 		KASSERT(dq->dq_cnt > 0);
    893  1.5  dholland 		lfs_dqref(dq);
    894  1.5  dholland 		mutex_exit(&lfs_dqlock);
    895  1.1  dholland 		mutex_destroy(&ndq->dq_interlock);
    896  1.1  dholland 		pool_cache_put(dquot_cache, ndq);
    897  1.1  dholland 		*dqp = dq;
    898  1.1  dholland 		return 0;
    899  1.1  dholland 	}
    900  1.1  dholland 	dq = ndq;
    901  1.1  dholland 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
    902  1.5  dholland 	lfs_dqref(dq);
    903  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    904  1.5  dholland 	mutex_exit(&lfs_dqlock);
    905  1.3  dholland #ifdef LFS_QUOTA
    906  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA)
    907  1.5  dholland 		error = lfs_dq1get(dqvp, id, ump, type, dq);
    908  1.1  dholland #endif
    909  1.3  dholland #ifdef LFS_QUOTA2
    910  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2)
    911  1.5  dholland 		error = lfs_dq2get(dqvp, id, ump, type, dq);
    912  1.1  dholland #endif
    913  1.1  dholland 	/*
    914  1.1  dholland 	 * I/O error in reading quota file, release
    915  1.1  dholland 	 * quota structure and reflect problem to caller.
    916  1.1  dholland 	 */
    917  1.1  dholland 	if (error) {
    918  1.5  dholland 		mutex_enter(&lfs_dqlock);
    919  1.1  dholland 		LIST_REMOVE(dq, dq_hash);
    920  1.5  dholland 		mutex_exit(&lfs_dqlock);
    921  1.1  dholland 		mutex_exit(&dq->dq_interlock);
    922  1.5  dholland 		lfs_dqrele(vp, dq);
    923  1.1  dholland 		*dqp = NODQUOT;
    924  1.1  dholland 		return (error);
    925  1.1  dholland 	}
    926  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    927  1.1  dholland 	*dqp = dq;
    928  1.1  dholland 	return (0);
    929  1.1  dholland }
    930  1.1  dholland 
    931  1.1  dholland /*
    932  1.1  dholland  * Obtain a reference to a dquot.
    933  1.1  dholland  */
    934  1.1  dholland void
    935  1.5  dholland lfs_dqref(struct dquot *dq)
    936  1.1  dholland {
    937  1.1  dholland 
    938  1.5  dholland 	KASSERT(mutex_owned(&lfs_dqlock));
    939  1.1  dholland 	dq->dq_cnt++;
    940  1.1  dholland 	KASSERT(dq->dq_cnt > 0);
    941  1.1  dholland }
    942  1.1  dholland 
    943  1.1  dholland /*
    944  1.1  dholland  * Release a reference to a dquot.
    945  1.1  dholland  */
    946  1.1  dholland void
    947  1.5  dholland lfs_dqrele(struct vnode *vp, struct dquot *dq)
    948  1.1  dholland {
    949  1.1  dholland 
    950  1.1  dholland 	if (dq == NODQUOT)
    951  1.1  dholland 		return;
    952  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    953  1.1  dholland 	for (;;) {
    954  1.5  dholland 		mutex_enter(&lfs_dqlock);
    955  1.1  dholland 		if (dq->dq_cnt > 1) {
    956  1.1  dholland 			dq->dq_cnt--;
    957  1.5  dholland 			mutex_exit(&lfs_dqlock);
    958  1.1  dholland 			mutex_exit(&dq->dq_interlock);
    959  1.1  dholland 			return;
    960  1.1  dholland 		}
    961  1.1  dholland 		if ((dq->dq_flags & DQ_MOD) == 0)
    962  1.1  dholland 			break;
    963  1.5  dholland 		mutex_exit(&lfs_dqlock);
    964  1.3  dholland #ifdef LFS_QUOTA
    965  1.7  dholland 		if (dq->dq_ump->um_lfs->um_flags & ULFS_QUOTA)
    966  1.5  dholland 			(void) lfs_dq1sync(vp, dq);
    967  1.1  dholland #endif
    968  1.3  dholland #ifdef LFS_QUOTA2
    969  1.7  dholland 		if (dq->dq_ump->um_lfs->um_flags & ULFS_QUOTA2)
    970  1.5  dholland 			(void) lfs_dq2sync(vp, dq);
    971  1.1  dholland #endif
    972  1.1  dholland 	}
    973  1.1  dholland 	KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
    974  1.1  dholland 	LIST_REMOVE(dq, dq_hash);
    975  1.5  dholland 	mutex_exit(&lfs_dqlock);
    976  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    977  1.1  dholland 	mutex_destroy(&dq->dq_interlock);
    978  1.1  dholland 	pool_cache_put(dquot_cache, dq);
    979  1.1  dholland }
    980  1.1  dholland 
    981  1.1  dholland int
    982  1.5  dholland lfs_qsync(struct mount *mp)
    983  1.1  dholland {
    984  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    985  1.7  dholland 	struct lfs *fs = ump->um_lfs;
    986  1.6  dholland 
    987  1.6  dholland 	/* avoid compiler warning when quotas aren't enabled */
    988  1.6  dholland 	(void)ump;
    989  1.7  dholland 	(void)fs;
    990  1.6  dholland 
    991  1.3  dholland #ifdef LFS_QUOTA
    992  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA)
    993  1.5  dholland 		return lfs_q1sync(mp);
    994  1.1  dholland #endif
    995  1.3  dholland #ifdef LFS_QUOTA2
    996  1.7  dholland 	if (fs->um_flags & ULFS_QUOTA2)
    997  1.5  dholland 		return lfs_q2sync(mp);
    998  1.1  dholland #endif
    999  1.1  dholland 	return 0;
   1000  1.1  dholland }
   1001  1.1  dholland 
   1002  1.1  dholland #ifdef DIAGNOSTIC
   1003  1.1  dholland /*
   1004  1.1  dholland  * Check the hash chains for stray dquot's.
   1005  1.1  dholland  */
   1006  1.1  dholland void
   1007  1.5  dholland lfs_dqflush(struct vnode *vp)
   1008  1.1  dholland {
   1009  1.1  dholland 	struct dquot *dq;
   1010  1.1  dholland 	int i;
   1011  1.1  dholland 
   1012  1.5  dholland 	mutex_enter(&lfs_dqlock);
   1013  1.1  dholland 	for (i = 0; i <= dqhash; i++)
   1014  1.1  dholland 		LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
   1015  1.1  dholland 			KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
   1016  1.5  dholland 	mutex_exit(&lfs_dqlock);
   1017  1.1  dholland }
   1018  1.1  dholland #endif
   1019