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