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