Home | History | Annotate | Line # | Download | only in ufs
ufs_quota.c revision 1.92
      1  1.92  dholland /*	$NetBSD: ufs_quota.c,v 1.92 2012/01/29 06:55:44 dholland Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4  1.11      fvdl  * Copyright (c) 1982, 1986, 1990, 1993, 1995
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * This code is derived from software contributed to Berkeley by
      8   1.1   mycroft  * Robert Elz at The University of Melbourne.
      9   1.1   mycroft  *
     10   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.1   mycroft  * modification, are permitted provided that the following conditions
     12   1.1   mycroft  * are met:
     13   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18  1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     20   1.1   mycroft  *    without specific prior written permission.
     21   1.1   mycroft  *
     22   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1   mycroft  * SUCH DAMAGE.
     33   1.1   mycroft  *
     34  1.11      fvdl  *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
     35   1.1   mycroft  */
     36  1.22     lukem 
     37  1.22     lukem #include <sys/cdefs.h>
     38  1.92  dholland __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.92 2012/01/29 06:55:44 dholland Exp $");
     39  1.22     lukem 
     40  1.69    bouyer #if defined(_KERNEL_OPT)
     41  1.69    bouyer #include "opt_quota.h"
     42  1.69    bouyer #endif
     43   1.1   mycroft #include <sys/param.h>
     44   1.1   mycroft #include <sys/kernel.h>
     45   1.1   mycroft #include <sys/systm.h>
     46   1.1   mycroft #include <sys/namei.h>
     47   1.1   mycroft #include <sys/file.h>
     48   1.1   mycroft #include <sys/proc.h>
     49   1.1   mycroft #include <sys/vnode.h>
     50   1.1   mycroft #include <sys/mount.h>
     51  1.39      elad #include <sys/kauth.h>
     52   1.1   mycroft 
     53  1.71  dholland #include <sys/quotactl.h>
     54   1.1   mycroft #include <ufs/ufs/quota.h>
     55   1.1   mycroft #include <ufs/ufs/inode.h>
     56   1.1   mycroft #include <ufs/ufs/ufsmount.h>
     57   1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     58  1.69    bouyer #include <ufs/ufs/ufs_quota.h>
     59  1.70    bouyer #include <quota/quotaprop.h>
     60   1.1   mycroft 
     61  1.69    bouyer kmutex_t dqlock;
     62  1.69    bouyer kcondvar_t dqcv;
     63  1.48   hannken 
     64  1.48   hannken /*
     65  1.69    bouyer  * Code pertaining to management of the in-core dquot data structures.
     66   1.1   mycroft  */
     67  1.69    bouyer #define DQHASH(dqvp, id) \
     68  1.69    bouyer 	(((((long)(dqvp)) >> 8) + id) & dqhash)
     69  1.69    bouyer static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
     70  1.69    bouyer static u_long dqhash;
     71  1.69    bouyer static pool_cache_t dquot_cache;
     72   1.1   mycroft 
     73   1.1   mycroft 
     74  1.69    bouyer static int quota_handle_cmd_get_version(struct mount *, struct lwp *,
     75  1.72  dholland     struct vfs_quotactl_args *args);
     76  1.69    bouyer static int quota_handle_cmd_get(struct mount *, struct lwp *,
     77  1.72  dholland     struct vfs_quotactl_args *args);
     78  1.85  dholland static int quota_handle_cmd_put(struct mount *, struct lwp *,
     79  1.72  dholland     struct vfs_quotactl_args *args);
     80  1.69    bouyer static int quota_handle_cmd_getall(struct mount *, struct lwp *,
     81  1.72  dholland     struct vfs_quotactl_args *args);
     82  1.91  dholland static int quota_handle_cmd_delete(struct mount *, struct lwp *,
     83  1.72  dholland     struct vfs_quotactl_args *args);
     84  1.69    bouyer static int quota_handle_cmd_quotaon(struct mount *, struct lwp *,
     85  1.72  dholland     struct vfs_quotactl_args *args);
     86  1.69    bouyer static int quota_handle_cmd_quotaoff(struct mount *, struct lwp *,
     87  1.72  dholland     struct vfs_quotactl_args *args);
     88  1.72  dholland 
     89   1.1   mycroft /*
     90  1.48   hannken  * Initialize the quota fields of an inode.
     91  1.48   hannken  */
     92  1.48   hannken void
     93  1.48   hannken ufsquota_init(struct inode *ip)
     94  1.48   hannken {
     95  1.48   hannken 	int i;
     96  1.48   hannken 
     97  1.48   hannken 	for (i = 0; i < MAXQUOTAS; i++)
     98  1.48   hannken 		ip->i_dquot[i] = NODQUOT;
     99  1.48   hannken }
    100  1.48   hannken 
    101  1.48   hannken /*
    102  1.48   hannken  * Release the quota fields from an inode.
    103  1.48   hannken  */
    104  1.48   hannken void
    105  1.48   hannken ufsquota_free(struct inode *ip)
    106  1.48   hannken {
    107  1.48   hannken 	int i;
    108  1.48   hannken 
    109  1.48   hannken 	for (i = 0; i < MAXQUOTAS; i++) {
    110  1.48   hannken 		dqrele(ITOV(ip), ip->i_dquot[i]);
    111  1.48   hannken 		ip->i_dquot[i] = NODQUOT;
    112  1.48   hannken 	}
    113  1.48   hannken }
    114  1.48   hannken 
    115  1.48   hannken /*
    116   1.1   mycroft  * Update disk usage, and take corrective action.
    117   1.1   mycroft  */
    118   1.1   mycroft int
    119  1.39      elad chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
    120   1.1   mycroft {
    121  1.69    bouyer 	/* do not track snapshot usage, or we will deadlock */
    122  1.69    bouyer 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
    123  1.69    bouyer 		return 0;
    124   1.1   mycroft 
    125  1.69    bouyer #ifdef QUOTA
    126  1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA)
    127  1.69    bouyer 		return chkdq1(ip, change, cred, flags);
    128  1.69    bouyer #endif
    129  1.69    bouyer #ifdef QUOTA2
    130  1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA2)
    131  1.69    bouyer 		return chkdq2(ip, change, cred, flags);
    132  1.69    bouyer #endif
    133  1.69    bouyer 	return 0;
    134   1.1   mycroft }
    135   1.1   mycroft 
    136   1.1   mycroft /*
    137  1.69    bouyer  * Check the inode limit, applying corrective action.
    138   1.1   mycroft  */
    139  1.69    bouyer int
    140  1.69    bouyer chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
    141   1.1   mycroft {
    142  1.69    bouyer 	/* do not track snapshot usage, or we will deadlock */
    143  1.69    bouyer 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
    144  1.69    bouyer 		return 0;
    145  1.69    bouyer #ifdef QUOTA
    146  1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA)
    147  1.69    bouyer 		return chkiq1(ip, change, cred, flags);
    148  1.69    bouyer #endif
    149  1.69    bouyer #ifdef QUOTA2
    150  1.69    bouyer 	if (ip->i_ump->um_flags & UFS_QUOTA2)
    151  1.69    bouyer 		return chkiq2(ip, change, cred, flags);
    152  1.69    bouyer #endif
    153  1.69    bouyer 	return 0;
    154   1.1   mycroft }
    155   1.1   mycroft 
    156   1.1   mycroft int
    157  1.71  dholland quota_handle_cmd(struct mount *mp, struct lwp *l, int op,
    158  1.72  dholland 		 struct vfs_quotactl_args *args)
    159  1.69    bouyer {
    160  1.69    bouyer 	int error = 0;
    161  1.69    bouyer 
    162  1.71  dholland 	switch (op) {
    163  1.71  dholland 	    case QUOTACTL_GETVERSION:
    164  1.72  dholland 		error = quota_handle_cmd_get_version(mp, l, args);
    165  1.71  dholland 		break;
    166  1.71  dholland 	    case QUOTACTL_QUOTAON:
    167  1.72  dholland 		error = quota_handle_cmd_quotaon(mp, l, args);
    168  1.71  dholland 		break;
    169  1.71  dholland 	    case QUOTACTL_QUOTAOFF:
    170  1.72  dholland 		error = quota_handle_cmd_quotaoff(mp, l, args);
    171  1.71  dholland 		break;
    172  1.71  dholland 	    case QUOTACTL_GET:
    173  1.72  dholland 		error = quota_handle_cmd_get(mp, l, args);
    174  1.71  dholland 		break;
    175  1.85  dholland 	    case QUOTACTL_PUT:
    176  1.85  dholland 		error = quota_handle_cmd_put(mp, l, args);
    177  1.71  dholland 		break;
    178  1.71  dholland 	    case QUOTACTL_GETALL:
    179  1.72  dholland 		error = quota_handle_cmd_getall(mp, l, args);
    180  1.71  dholland 		break;
    181  1.91  dholland 	    case QUOTACTL_DELETE:
    182  1.91  dholland 		error = quota_handle_cmd_delete(mp, l, args);
    183  1.71  dholland 		break;
    184  1.71  dholland 	    default:
    185  1.71  dholland 		panic("Invalid quotactl operation %d\n", op);
    186  1.69    bouyer 	}
    187  1.71  dholland 
    188  1.69    bouyer 	return error;
    189  1.69    bouyer }
    190  1.69    bouyer 
    191  1.69    bouyer static int
    192  1.69    bouyer quota_handle_cmd_get_version(struct mount *mp, struct lwp *l,
    193  1.72  dholland     struct vfs_quotactl_args *args)
    194   1.1   mycroft {
    195  1.69    bouyer 	struct ufsmount *ump = VFSTOUFS(mp);
    196  1.73  dholland 	int *version_ret;
    197  1.72  dholland 
    198  1.73  dholland 	KASSERT(args->qc_type == QCT_GETVERSION);
    199  1.73  dholland 	version_ret = args->u.getversion.qc_version_ret;
    200  1.69    bouyer 
    201  1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    202  1.69    bouyer 		return EOPNOTSUPP;
    203  1.69    bouyer 
    204  1.69    bouyer #ifdef QUOTA
    205  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA) {
    206  1.73  dholland 		*version_ret = 1;
    207  1.69    bouyer 	} else
    208  1.69    bouyer #endif
    209  1.69    bouyer #ifdef QUOTA2
    210  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2) {
    211  1.73  dholland 		*version_ret = 2;
    212  1.69    bouyer 	} else
    213  1.69    bouyer #endif
    214  1.73  dholland 		return EOPNOTSUPP;
    215  1.73  dholland 
    216  1.73  dholland 	return 0;
    217   1.1   mycroft }
    218   1.1   mycroft 
    219  1.69    bouyer /* XXX shouldn't all this be in kauth ? */
    220  1.48   hannken static int
    221  1.69    bouyer quota_get_auth(struct mount *mp, struct lwp *l, uid_t id) {
    222  1.69    bouyer 	/* The user can always query about his own quota. */
    223  1.69    bouyer 	if (id == kauth_cred_getuid(l->l_cred))
    224  1.69    bouyer 		return 0;
    225  1.69    bouyer 	return kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    226  1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(id), NULL);
    227  1.69    bouyer }
    228  1.69    bouyer 
    229  1.69    bouyer static int
    230  1.69    bouyer quota_handle_cmd_get(struct mount *mp, struct lwp *l,
    231  1.72  dholland     struct vfs_quotactl_args *args)
    232  1.69    bouyer {
    233  1.69    bouyer 	struct ufsmount *ump = VFSTOUFS(mp);
    234  1.74  dholland 	int error;
    235  1.78  dholland 	const struct quotakey *qk;
    236  1.77  dholland 	struct quotaval *ret;
    237  1.72  dholland 
    238  1.74  dholland 	KASSERT(args->qc_type == QCT_GET);
    239  1.78  dholland 	qk = args->u.get.qc_key;
    240  1.77  dholland 	ret = args->u.get.qc_ret;
    241  1.69    bouyer 
    242  1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    243  1.69    bouyer 		return EOPNOTSUPP;
    244  1.69    bouyer 
    245  1.79  dholland 	error = quota_get_auth(mp, l, qk->qk_id);
    246  1.79  dholland 	if (error != 0)
    247  1.79  dholland 		return error;
    248  1.69    bouyer #ifdef QUOTA
    249  1.79  dholland 	if (ump->um_flags & UFS_QUOTA) {
    250  1.79  dholland 		error = quota1_handle_cmd_get(ump, qk, ret);
    251  1.79  dholland 	} else
    252  1.69    bouyer #endif
    253  1.69    bouyer #ifdef QUOTA2
    254  1.79  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    255  1.79  dholland 		error = quota2_handle_cmd_get(ump, qk, ret);
    256  1.79  dholland 	} else
    257  1.69    bouyer #endif
    258  1.79  dholland 		panic("quota_handle_cmd_get: no support ?");
    259  1.69    bouyer 
    260  1.79  dholland 	if (error != 0)
    261  1.79  dholland 		return error;
    262  1.74  dholland 
    263  1.69    bouyer 	return error;
    264  1.69    bouyer }
    265  1.69    bouyer 
    266  1.69    bouyer static int
    267  1.85  dholland quota_handle_cmd_put(struct mount *mp, struct lwp *l,
    268  1.72  dholland     struct vfs_quotactl_args *args)
    269  1.69    bouyer {
    270  1.11      fvdl 	struct ufsmount *ump = VFSTOUFS(mp);
    271  1.84  dholland 	const struct quotakey *qk;
    272  1.83  dholland 	const struct quotaval *qv;
    273  1.84  dholland 	id_t kauth_id;
    274  1.80  dholland 	int error;
    275  1.72  dholland 
    276  1.85  dholland 	KASSERT(args->qc_type == QCT_PUT);
    277  1.85  dholland 	qk = args->u.put.qc_key;
    278  1.85  dholland 	qv = args->u.put.qc_val;
    279   1.1   mycroft 
    280  1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0)
    281  1.69    bouyer 		return EOPNOTSUPP;
    282  1.61        ad 
    283  1.84  dholland 	kauth_id = qk->qk_id;
    284  1.84  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    285  1.84  dholland 		kauth_id = 0;
    286  1.84  dholland 	}
    287  1.84  dholland 
    288  1.86  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    289  1.86  dholland 	    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    290  1.86  dholland 	    NULL);
    291  1.87  dholland 	if (error != 0) {
    292  1.87  dholland 		return error;
    293  1.87  dholland 	}
    294  1.87  dholland 
    295  1.69    bouyer #ifdef QUOTA
    296  1.86  dholland 	if (ump->um_flags & UFS_QUOTA)
    297  1.86  dholland 		error = quota1_handle_cmd_put(ump, qk, qv);
    298  1.86  dholland 	else
    299  1.69    bouyer #endif
    300  1.69    bouyer #ifdef QUOTA2
    301  1.86  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    302  1.86  dholland 		error = quota2_handle_cmd_put(ump, qk, qv);
    303  1.86  dholland 	} else
    304  1.69    bouyer #endif
    305  1.86  dholland 		panic("quota_handle_cmd_get: no support ?");
    306  1.69    bouyer 
    307  1.87  dholland 	if (error == ENOENT) {
    308  1.87  dholland 		error = 0;
    309  1.87  dholland 	}
    310  1.80  dholland 
    311  1.69    bouyer 	return error;
    312  1.69    bouyer }
    313  1.69    bouyer 
    314  1.69    bouyer static int
    315  1.91  dholland quota_handle_cmd_delete(struct mount *mp, struct lwp *l,
    316  1.72  dholland     struct vfs_quotactl_args *args)
    317  1.69    bouyer {
    318  1.88  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    319  1.92  dholland 	const struct quotakey *qk;
    320  1.92  dholland 	id_t kauth_id;
    321  1.88  dholland 	int error;
    322  1.72  dholland 
    323  1.91  dholland 	KASSERT(args->qc_type == QCT_DELETE);
    324  1.92  dholland 	qk = args->u.delete.qc_key;
    325  1.92  dholland 
    326  1.92  dholland 	kauth_id = qk->qk_id;
    327  1.92  dholland 	if (kauth_id == QUOTA_DEFAULTID) {
    328  1.92  dholland 		kauth_id = 0;
    329  1.92  dholland 	}
    330  1.33     perry 
    331  1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) == 0)
    332  1.69    bouyer 		return EOPNOTSUPP;
    333  1.53        ad 
    334  1.88  dholland 	/* avoid whitespace changes */
    335  1.88  dholland 	{
    336  1.69    bouyer 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    337  1.92  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(kauth_id),
    338  1.92  dholland 		    NULL);
    339  1.69    bouyer 		if (error != 0)
    340  1.69    bouyer 			goto err;
    341  1.69    bouyer #ifdef QUOTA2
    342  1.69    bouyer 		if (ump->um_flags & UFS_QUOTA2) {
    343  1.92  dholland 			error = quota2_handle_cmd_delete(ump, qk);
    344  1.69    bouyer 		} else
    345  1.69    bouyer #endif
    346  1.69    bouyer 			panic("quota_handle_cmd_get: no support ?");
    347  1.69    bouyer 
    348  1.69    bouyer 		if (error && error != ENOENT)
    349  1.69    bouyer 			goto err;
    350  1.50   hannken 	}
    351  1.88  dholland 
    352  1.88  dholland 	return 0;
    353  1.88  dholland  err:
    354  1.69    bouyer 	return error;
    355   1.1   mycroft }
    356   1.1   mycroft 
    357  1.69    bouyer static int
    358  1.69    bouyer quota_handle_cmd_getall(struct mount *mp, struct lwp *l,
    359  1.72  dholland     struct vfs_quotactl_args *args)
    360   1.1   mycroft {
    361  1.69    bouyer 	prop_array_t replies;
    362   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    363   1.1   mycroft 	int error;
    364  1.72  dholland 	prop_dictionary_t cmddict;
    365  1.72  dholland 	int q2type;
    366  1.72  dholland 	prop_array_t datas;
    367  1.72  dholland 
    368  1.72  dholland 	KASSERT(args->qc_type == QCT_PROPLIB);
    369  1.72  dholland 	cmddict = args->u.proplib.qc_cmddict;
    370  1.72  dholland 	q2type = args->u.proplib.qc_q2type;
    371  1.72  dholland 	datas = args->u.proplib.qc_datas;
    372  1.72  dholland 
    373  1.72  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    374  1.72  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    375   1.1   mycroft 
    376  1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) == 0)
    377  1.69    bouyer 		return EOPNOTSUPP;
    378  1.69    bouyer 
    379  1.69    bouyer 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    380  1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, NULL, NULL);
    381   1.8  christos 	if (error)
    382  1.69    bouyer 		return error;
    383  1.69    bouyer 
    384  1.69    bouyer 	replies = prop_array_create();
    385  1.69    bouyer 	if (replies == NULL)
    386  1.69    bouyer 		return ENOMEM;
    387  1.69    bouyer 
    388  1.69    bouyer #ifdef QUOTA2
    389  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2) {
    390  1.72  dholland 		error = quota2_handle_cmd_getall(ump, q2type, replies);
    391  1.69    bouyer 	} else
    392  1.69    bouyer #endif
    393  1.69    bouyer 		panic("quota_handle_cmd_getall: no support ?");
    394  1.69    bouyer 	if (!prop_dictionary_set_and_rel(cmddict, "data", replies)) {
    395  1.69    bouyer 		error = ENOMEM;
    396  1.69    bouyer 	} else {
    397  1.69    bouyer 		error = 0;
    398  1.69    bouyer 	}
    399  1.69    bouyer 	return error;
    400   1.1   mycroft }
    401   1.1   mycroft 
    402  1.69    bouyer static int
    403  1.69    bouyer quota_handle_cmd_quotaon(struct mount *mp, struct lwp *l,
    404  1.72  dholland     struct vfs_quotactl_args *args)
    405   1.1   mycroft {
    406  1.69    bouyer 	prop_dictionary_t data;
    407   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    408   1.1   mycroft 	int error;
    409  1.69    bouyer 	const char *qfile;
    410  1.72  dholland 	prop_dictionary_t cmddict;
    411  1.72  dholland 	int q2type;
    412  1.72  dholland 	prop_array_t datas;
    413  1.72  dholland 
    414  1.72  dholland 	KASSERT(args->qc_type == QCT_PROPLIB);
    415  1.72  dholland 	cmddict = args->u.proplib.qc_cmddict;
    416  1.72  dholland 	q2type = args->u.proplib.qc_q2type;
    417  1.72  dholland 	datas = args->u.proplib.qc_datas;
    418  1.72  dholland 
    419  1.72  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    420  1.72  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    421  1.69    bouyer 
    422  1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) != 0)
    423  1.69    bouyer 		return EBUSY;
    424  1.69    bouyer 
    425  1.69    bouyer 	if (prop_array_count(datas) != 1)
    426  1.69    bouyer 		return EINVAL;
    427   1.1   mycroft 
    428  1.69    bouyer 	data = prop_array_get(datas, 0);
    429  1.69    bouyer 	if (data == NULL)
    430  1.69    bouyer 		return ENOMEM;
    431  1.69    bouyer 	if (!prop_dictionary_get_cstring_nocopy(data, "quotafile",
    432  1.69    bouyer 	    &qfile))
    433  1.69    bouyer 		return EINVAL;
    434  1.69    bouyer 
    435  1.69    bouyer 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    436  1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    437  1.69    bouyer 	if (error != 0) {
    438  1.69    bouyer 		return error;
    439  1.69    bouyer 	}
    440  1.69    bouyer #ifdef QUOTA
    441  1.72  dholland 	error = quota1_handle_cmd_quotaon(l, ump, q2type, qfile);
    442  1.69    bouyer #else
    443  1.69    bouyer 	error = EOPNOTSUPP;
    444  1.69    bouyer #endif
    445  1.69    bouyer 
    446  1.69    bouyer 	return error;
    447   1.1   mycroft }
    448   1.1   mycroft 
    449  1.69    bouyer static int
    450  1.69    bouyer quota_handle_cmd_quotaoff(struct mount *mp, struct lwp *l,
    451  1.72  dholland     struct vfs_quotactl_args *args)
    452   1.1   mycroft {
    453   1.1   mycroft 	struct ufsmount *ump = VFSTOUFS(mp);
    454  1.69    bouyer 	int error;
    455  1.72  dholland 	prop_dictionary_t cmddict;
    456  1.72  dholland 	int q2type;
    457  1.72  dholland 	prop_array_t datas;
    458  1.72  dholland 
    459  1.72  dholland 	KASSERT(args->qc_type == QCT_PROPLIB);
    460  1.72  dholland 	cmddict = args->u.proplib.qc_cmddict;
    461  1.72  dholland 	q2type = args->u.proplib.qc_q2type;
    462  1.72  dholland 	datas = args->u.proplib.qc_datas;
    463  1.72  dholland 
    464  1.72  dholland 	KASSERT(prop_object_type(cmddict) == PROP_TYPE_DICTIONARY);
    465  1.72  dholland 	KASSERT(prop_object_type(datas) == PROP_TYPE_ARRAY);
    466   1.1   mycroft 
    467  1.69    bouyer 	if ((ump->um_flags & UFS_QUOTA2) != 0)
    468  1.69    bouyer 		return EOPNOTSUPP;
    469  1.69    bouyer 
    470  1.69    bouyer 	if (prop_array_count(datas) != 0)
    471  1.69    bouyer 		return EINVAL;
    472  1.69    bouyer 
    473  1.69    bouyer 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    474  1.69    bouyer 	    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    475  1.69    bouyer 	if (error != 0) {
    476  1.69    bouyer 		return error;
    477   1.1   mycroft 	}
    478  1.69    bouyer #ifdef QUOTA
    479  1.72  dholland 	error = quota1_handle_cmd_quotaoff(l, ump, q2type);
    480  1.69    bouyer #else
    481  1.69    bouyer 	error = EOPNOTSUPP;
    482  1.69    bouyer #endif
    483  1.69    bouyer 
    484  1.69    bouyer 	return error;
    485   1.1   mycroft }
    486   1.1   mycroft 
    487   1.1   mycroft /*
    488   1.1   mycroft  * Initialize the quota system.
    489   1.1   mycroft  */
    490   1.1   mycroft void
    491  1.35   thorpej dqinit(void)
    492   1.1   mycroft {
    493  1.47     pooka 
    494  1.50   hannken 	mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
    495  1.50   hannken 	cv_init(&dqcv, "quota");
    496  1.60        ad 	dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
    497  1.55        ad 	dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "ufsdq",
    498  1.55        ad 	    NULL, IPL_NONE, NULL, NULL, NULL);
    499  1.14  jdolecek }
    500  1.14  jdolecek 
    501  1.21       chs void
    502  1.35   thorpej dqreinit(void)
    503  1.21       chs {
    504  1.21       chs 	struct dquot *dq;
    505  1.21       chs 	struct dqhashhead *oldhash, *hash;
    506  1.21       chs 	struct vnode *dqvp;
    507  1.21       chs 	u_long oldmask, mask, hashval;
    508  1.21       chs 	int i;
    509  1.21       chs 
    510  1.60        ad 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    511  1.50   hannken 	mutex_enter(&dqlock);
    512  1.21       chs 	oldhash = dqhashtbl;
    513  1.21       chs 	oldmask = dqhash;
    514  1.21       chs 	dqhashtbl = hash;
    515  1.21       chs 	dqhash = mask;
    516  1.21       chs 	for (i = 0; i <= oldmask; i++) {
    517  1.21       chs 		while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
    518  1.21       chs 			dqvp = dq->dq_ump->um_quotas[dq->dq_type];
    519  1.21       chs 			LIST_REMOVE(dq, dq_hash);
    520  1.21       chs 			hashval = DQHASH(dqvp, dq->dq_id);
    521  1.21       chs 			LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
    522  1.21       chs 		}
    523  1.21       chs 	}
    524  1.50   hannken 	mutex_exit(&dqlock);
    525  1.60        ad 	hashdone(oldhash, HASH_LIST, oldmask);
    526  1.21       chs }
    527  1.21       chs 
    528  1.14  jdolecek /*
    529  1.14  jdolecek  * Free resources held by quota system.
    530  1.14  jdolecek  */
    531  1.14  jdolecek void
    532  1.35   thorpej dqdone(void)
    533  1.14  jdolecek {
    534  1.47     pooka 
    535  1.55        ad 	pool_cache_destroy(dquot_cache);
    536  1.60        ad 	hashdone(dqhashtbl, HASH_LIST, dqhash);
    537  1.50   hannken 	cv_destroy(&dqcv);
    538  1.50   hannken 	mutex_destroy(&dqlock);
    539   1.1   mycroft }
    540   1.1   mycroft 
    541   1.1   mycroft /*
    542  1.69    bouyer  * Set up the quotas for an inode.
    543  1.69    bouyer  *
    544  1.69    bouyer  * This routine completely defines the semantics of quotas.
    545  1.69    bouyer  * If other criterion want to be used to establish quotas, the
    546  1.69    bouyer  * MAXQUOTAS value in quotas.h should be increased, and the
    547  1.69    bouyer  * additional dquots set up here.
    548  1.69    bouyer  */
    549  1.69    bouyer int
    550  1.69    bouyer getinoquota(struct inode *ip)
    551  1.69    bouyer {
    552  1.69    bouyer 	struct ufsmount *ump = ip->i_ump;
    553  1.69    bouyer 	struct vnode *vp = ITOV(ip);
    554  1.69    bouyer 	int i, error;
    555  1.69    bouyer 	u_int32_t ino_ids[MAXQUOTAS];
    556  1.69    bouyer 
    557  1.69    bouyer 	/*
    558  1.69    bouyer 	 * To avoid deadlocks never update quotas for quota files
    559  1.69    bouyer 	 * on the same file system
    560  1.69    bouyer 	 */
    561  1.69    bouyer 	for (i = 0; i < MAXQUOTAS; i++)
    562  1.69    bouyer 		if (vp == ump->um_quotas[i])
    563  1.69    bouyer 			return 0;
    564  1.69    bouyer 
    565  1.69    bouyer 	ino_ids[USRQUOTA] = ip->i_uid;
    566  1.69    bouyer 	ino_ids[GRPQUOTA] = ip->i_gid;
    567  1.69    bouyer 	for (i = 0; i < MAXQUOTAS; i++) {
    568  1.69    bouyer 		/*
    569  1.69    bouyer 		 * If the file id changed the quota needs update.
    570  1.69    bouyer 		 */
    571  1.69    bouyer 		if (ip->i_dquot[i] != NODQUOT &&
    572  1.69    bouyer 		    ip->i_dquot[i]->dq_id != ino_ids[i]) {
    573  1.69    bouyer 			dqrele(ITOV(ip), ip->i_dquot[i]);
    574  1.69    bouyer 			ip->i_dquot[i] = NODQUOT;
    575  1.69    bouyer 		}
    576  1.69    bouyer 		/*
    577  1.69    bouyer 		 * Set up the quota based on file id.
    578  1.69    bouyer 		 * ENODEV means that quotas are not enabled.
    579  1.69    bouyer 		 */
    580  1.69    bouyer 		if (ip->i_dquot[i] == NODQUOT &&
    581  1.69    bouyer 		    (error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
    582  1.69    bouyer 		    error != ENODEV)
    583  1.69    bouyer 			return (error);
    584  1.69    bouyer 	}
    585  1.69    bouyer 	return 0;
    586  1.69    bouyer }
    587  1.69    bouyer 
    588  1.69    bouyer /*
    589   1.1   mycroft  * Obtain a dquot structure for the specified identifier and quota file
    590   1.1   mycroft  * reading the information from the file if necessary.
    591   1.1   mycroft  */
    592  1.69    bouyer int
    593  1.35   thorpej dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
    594  1.35   thorpej     struct dquot **dqp)
    595   1.1   mycroft {
    596  1.50   hannken 	struct dquot *dq, *ndq;
    597  1.21       chs 	struct dqhashhead *dqh;
    598  1.11      fvdl 	struct vnode *dqvp;
    599  1.69    bouyer 	int error = 0; /* XXX gcc */
    600   1.1   mycroft 
    601  1.50   hannken 	/* Lock to see an up to date value for QTF_CLOSING. */
    602  1.50   hannken 	mutex_enter(&dqlock);
    603  1.69    bouyer 	if ((ump->um_flags & (UFS_QUOTA|UFS_QUOTA2)) == 0) {
    604  1.50   hannken 		mutex_exit(&dqlock);
    605   1.1   mycroft 		*dqp = NODQUOT;
    606  1.69    bouyer 		return (ENODEV);
    607  1.69    bouyer 	}
    608  1.69    bouyer 	dqvp = ump->um_quotas[type];
    609  1.69    bouyer #ifdef QUOTA
    610  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA) {
    611  1.69    bouyer 		if (dqvp == NULLVP || (ump->umq1_qflags[type] & QTF_CLOSING)) {
    612  1.69    bouyer 			mutex_exit(&dqlock);
    613  1.69    bouyer 			*dqp = NODQUOT;
    614  1.69    bouyer 			return (ENODEV);
    615  1.69    bouyer 		}
    616   1.1   mycroft 	}
    617  1.69    bouyer #endif
    618  1.69    bouyer #ifdef QUOTA2
    619  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2) {
    620  1.69    bouyer 		if (dqvp == NULLVP) {
    621  1.69    bouyer 			mutex_exit(&dqlock);
    622  1.69    bouyer 			*dqp = NODQUOT;
    623  1.69    bouyer 			return (ENODEV);
    624  1.69    bouyer 		}
    625  1.69    bouyer 	}
    626  1.69    bouyer #endif
    627  1.50   hannken 	KASSERT(dqvp != vp);
    628   1.1   mycroft 	/*
    629   1.1   mycroft 	 * Check the cache first.
    630   1.1   mycroft 	 */
    631  1.21       chs 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    632  1.21       chs 	LIST_FOREACH(dq, dqh, dq_hash) {
    633   1.1   mycroft 		if (dq->dq_id != id ||
    634   1.1   mycroft 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    635   1.1   mycroft 			continue;
    636  1.50   hannken 		KASSERT(dq->dq_cnt > 0);
    637  1.37       chs 		dqref(dq);
    638  1.50   hannken 		mutex_exit(&dqlock);
    639   1.1   mycroft 		*dqp = dq;
    640   1.1   mycroft 		return (0);
    641   1.1   mycroft 	}
    642   1.1   mycroft 	/*
    643   1.1   mycroft 	 * Not in cache, allocate a new one.
    644   1.1   mycroft 	 */
    645  1.50   hannken 	mutex_exit(&dqlock);
    646  1.55        ad 	ndq = pool_cache_get(dquot_cache, PR_WAITOK);
    647   1.1   mycroft 	/*
    648   1.1   mycroft 	 * Initialize the contents of the dquot structure.
    649   1.1   mycroft 	 */
    650  1.50   hannken 	memset((char *)ndq, 0, sizeof *ndq);
    651  1.50   hannken 	ndq->dq_flags = 0;
    652  1.50   hannken 	ndq->dq_id = id;
    653  1.50   hannken 	ndq->dq_ump = ump;
    654  1.50   hannken 	ndq->dq_type = type;
    655  1.50   hannken 	mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
    656  1.50   hannken 	mutex_enter(&dqlock);
    657  1.49   hannken 	dqh = &dqhashtbl[DQHASH(dqvp, id)];
    658  1.50   hannken 	LIST_FOREACH(dq, dqh, dq_hash) {
    659  1.50   hannken 		if (dq->dq_id != id ||
    660  1.50   hannken 		    dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
    661  1.50   hannken 			continue;
    662  1.50   hannken 		/*
    663  1.50   hannken 		 * Another thread beat us allocating this dquot.
    664  1.50   hannken 		 */
    665  1.50   hannken 		KASSERT(dq->dq_cnt > 0);
    666  1.50   hannken 		dqref(dq);
    667  1.50   hannken 		mutex_exit(&dqlock);
    668  1.64    bouyer 		mutex_destroy(&ndq->dq_interlock);
    669  1.55        ad 		pool_cache_put(dquot_cache, ndq);
    670  1.50   hannken 		*dqp = dq;
    671  1.50   hannken 		return 0;
    672  1.50   hannken 	}
    673  1.50   hannken 	dq = ndq;
    674   1.5   mycroft 	LIST_INSERT_HEAD(dqh, dq, dq_hash);
    675  1.37       chs 	dqref(dq);
    676  1.50   hannken 	mutex_enter(&dq->dq_interlock);
    677  1.50   hannken 	mutex_exit(&dqlock);
    678  1.69    bouyer #ifdef QUOTA
    679  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA)
    680  1.69    bouyer 		error = dq1get(dqvp, id, ump, type, dq);
    681  1.69    bouyer #endif
    682  1.69    bouyer #ifdef QUOTA2
    683  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2)
    684  1.69    bouyer 		error = dq2get(dqvp, id, ump, type, dq);
    685  1.69    bouyer #endif
    686   1.1   mycroft 	/*
    687   1.1   mycroft 	 * I/O error in reading quota file, release
    688   1.1   mycroft 	 * quota structure and reflect problem to caller.
    689   1.1   mycroft 	 */
    690   1.1   mycroft 	if (error) {
    691  1.50   hannken 		mutex_enter(&dqlock);
    692   1.5   mycroft 		LIST_REMOVE(dq, dq_hash);
    693  1.50   hannken 		mutex_exit(&dqlock);
    694  1.49   hannken 		mutex_exit(&dq->dq_interlock);
    695   1.1   mycroft 		dqrele(vp, dq);
    696   1.1   mycroft 		*dqp = NODQUOT;
    697   1.1   mycroft 		return (error);
    698   1.1   mycroft 	}
    699  1.49   hannken 	mutex_exit(&dq->dq_interlock);
    700   1.1   mycroft 	*dqp = dq;
    701   1.1   mycroft 	return (0);
    702   1.1   mycroft }
    703   1.1   mycroft 
    704   1.1   mycroft /*
    705   1.1   mycroft  * Obtain a reference to a dquot.
    706   1.1   mycroft  */
    707  1.69    bouyer void
    708  1.35   thorpej dqref(struct dquot *dq)
    709   1.1   mycroft {
    710   1.1   mycroft 
    711  1.50   hannken 	KASSERT(mutex_owned(&dqlock));
    712   1.1   mycroft 	dq->dq_cnt++;
    713  1.46   hannken 	KASSERT(dq->dq_cnt > 0);
    714   1.1   mycroft }
    715   1.1   mycroft 
    716   1.1   mycroft /*
    717   1.1   mycroft  * Release a reference to a dquot.
    718   1.1   mycroft  */
    719  1.69    bouyer void
    720  1.35   thorpej dqrele(struct vnode *vp, struct dquot *dq)
    721   1.1   mycroft {
    722   1.1   mycroft 
    723   1.1   mycroft 	if (dq == NODQUOT)
    724   1.1   mycroft 		return;
    725  1.50   hannken 	mutex_enter(&dq->dq_interlock);
    726  1.50   hannken 	for (;;) {
    727  1.50   hannken 		mutex_enter(&dqlock);
    728  1.50   hannken 		if (dq->dq_cnt > 1) {
    729  1.50   hannken 			dq->dq_cnt--;
    730  1.50   hannken 			mutex_exit(&dqlock);
    731  1.50   hannken 			mutex_exit(&dq->dq_interlock);
    732  1.50   hannken 			return;
    733  1.50   hannken 		}
    734  1.50   hannken 		if ((dq->dq_flags & DQ_MOD) == 0)
    735  1.50   hannken 			break;
    736  1.50   hannken 		mutex_exit(&dqlock);
    737  1.69    bouyer #ifdef QUOTA
    738  1.69    bouyer 		if (dq->dq_ump->um_flags & UFS_QUOTA)
    739  1.69    bouyer 			(void) dq1sync(vp, dq);
    740  1.69    bouyer #endif
    741  1.69    bouyer #ifdef QUOTA2
    742  1.69    bouyer 		if (dq->dq_ump->um_flags & UFS_QUOTA2)
    743  1.69    bouyer 			(void) dq2sync(vp, dq);
    744  1.69    bouyer #endif
    745   1.1   mycroft 	}
    746  1.50   hannken 	KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
    747  1.50   hannken 	LIST_REMOVE(dq, dq_hash);
    748  1.50   hannken 	mutex_exit(&dqlock);
    749  1.50   hannken 	mutex_exit(&dq->dq_interlock);
    750  1.50   hannken 	mutex_destroy(&dq->dq_interlock);
    751  1.55        ad 	pool_cache_put(dquot_cache, dq);
    752   1.1   mycroft }
    753   1.1   mycroft 
    754  1.69    bouyer int
    755  1.69    bouyer qsync(struct mount *mp)
    756   1.1   mycroft {
    757  1.69    bouyer 	struct ufsmount *ump = VFSTOUFS(mp);
    758  1.69    bouyer #ifdef QUOTA
    759  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA)
    760  1.69    bouyer 		return q1sync(mp);
    761  1.69    bouyer #endif
    762  1.69    bouyer #ifdef QUOTA2
    763  1.69    bouyer 	if (ump->um_flags & UFS_QUOTA2)
    764  1.69    bouyer 		return q2sync(mp);
    765  1.69    bouyer #endif
    766  1.69    bouyer 	return 0;
    767   1.1   mycroft }
    768   1.1   mycroft 
    769  1.50   hannken #ifdef DIAGNOSTIC
    770   1.1   mycroft /*
    771  1.50   hannken  * Check the hash chains for stray dquot's.
    772   1.1   mycroft  */
    773  1.69    bouyer void
    774  1.35   thorpej dqflush(struct vnode *vp)
    775   1.1   mycroft {
    776  1.50   hannken 	struct dquot *dq;
    777  1.50   hannken 	int i;
    778   1.1   mycroft 
    779  1.50   hannken 	mutex_enter(&dqlock);
    780  1.50   hannken 	for (i = 0; i <= dqhash; i++)
    781  1.50   hannken 		LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
    782  1.50   hannken 			KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
    783  1.50   hannken 	mutex_exit(&dqlock);
    784   1.1   mycroft }
    785  1.50   hannken #endif
    786