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