Home | History | Annotate | Line # | Download | only in lfs
ulfs_quota1.c revision 1.2
      1  1.2  dholland /*	$NetBSD: ulfs_quota1.c,v 1.2 2013/06/06 00:44:40 dholland Exp $	*/
      2  1.1  dholland /*  from NetBSD: ufs_quota1.c,v 1.18 2012/02/02 03:00:48 matt Exp  */
      3  1.1  dholland 
      4  1.1  dholland /*
      5  1.1  dholland  * Copyright (c) 1982, 1986, 1990, 1993, 1995
      6  1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      7  1.1  dholland  *
      8  1.1  dholland  * This code is derived from software contributed to Berkeley by
      9  1.1  dholland  * Robert Elz at The University of Melbourne.
     10  1.1  dholland  *
     11  1.1  dholland  * Redistribution and use in source and binary forms, with or without
     12  1.1  dholland  * modification, are permitted provided that the following conditions
     13  1.1  dholland  * are met:
     14  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     15  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     16  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     19  1.1  dholland  * 3. Neither the name of the University nor the names of its contributors
     20  1.1  dholland  *    may be used to endorse or promote products derived from this software
     21  1.1  dholland  *    without specific prior written permission.
     22  1.1  dholland  *
     23  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  dholland  * SUCH DAMAGE.
     34  1.1  dholland  *
     35  1.1  dholland  *	@(#)ufs_quota.c	8.5 (Berkeley) 5/20/95
     36  1.1  dholland  */
     37  1.1  dholland 
     38  1.1  dholland #include <sys/cdefs.h>
     39  1.2  dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_quota1.c,v 1.2 2013/06/06 00:44:40 dholland Exp $");
     40  1.1  dholland 
     41  1.1  dholland #include <sys/param.h>
     42  1.1  dholland #include <sys/kernel.h>
     43  1.1  dholland #include <sys/systm.h>
     44  1.1  dholland #include <sys/namei.h>
     45  1.1  dholland #include <sys/file.h>
     46  1.1  dholland #include <sys/proc.h>
     47  1.1  dholland #include <sys/vnode.h>
     48  1.1  dholland #include <sys/mount.h>
     49  1.1  dholland #include <sys/kauth.h>
     50  1.1  dholland 
     51  1.2  dholland #include <ufs/lfs/ulfs_quota1.h>
     52  1.2  dholland #include <ufs/lfs/ulfs_inode.h>
     53  1.2  dholland #include <ufs/lfs/ulfsmount.h>
     54  1.2  dholland #include <ufs/lfs/ulfs_extern.h>
     55  1.2  dholland #include <ufs/lfs/ulfs_quota.h>
     56  1.1  dholland 
     57  1.1  dholland static int chkdqchg(struct inode *, int64_t, kauth_cred_t, int);
     58  1.1  dholland static int chkiqchg(struct inode *, int32_t, kauth_cred_t, int);
     59  1.1  dholland 
     60  1.1  dholland /*
     61  1.1  dholland  * Update disk usage, and take corrective action.
     62  1.1  dholland  */
     63  1.1  dholland int
     64  1.1  dholland chkdq1(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
     65  1.1  dholland {
     66  1.1  dholland 	struct dquot *dq;
     67  1.1  dholland 	int i;
     68  1.1  dholland 	int ncurblocks, error;
     69  1.1  dholland 
     70  1.1  dholland 	if ((error = getinoquota(ip)) != 0)
     71  1.1  dholland 		return error;
     72  1.1  dholland 	if (change == 0)
     73  1.1  dholland 		return (0);
     74  1.1  dholland 	if (change < 0) {
     75  1.1  dholland 		for (i = 0; i < MAXQUOTAS; i++) {
     76  1.1  dholland 			if ((dq = ip->i_dquot[i]) == NODQUOT)
     77  1.1  dholland 				continue;
     78  1.1  dholland 			mutex_enter(&dq->dq_interlock);
     79  1.1  dholland 			ncurblocks = dq->dq_curblocks + change;
     80  1.1  dholland 			if (ncurblocks >= 0)
     81  1.1  dholland 				dq->dq_curblocks = ncurblocks;
     82  1.1  dholland 			else
     83  1.1  dholland 				dq->dq_curblocks = 0;
     84  1.1  dholland 			dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
     85  1.1  dholland 			dq->dq_flags |= DQ_MOD;
     86  1.1  dholland 			mutex_exit(&dq->dq_interlock);
     87  1.1  dholland 		}
     88  1.1  dholland 		return (0);
     89  1.1  dholland 	}
     90  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++) {
     91  1.1  dholland 		if ((dq = ip->i_dquot[i]) == NODQUOT)
     92  1.1  dholland 			continue;
     93  1.1  dholland 		if ((flags & FORCE) == 0 &&
     94  1.1  dholland 		    kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA,
     95  1.1  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, KAUTH_ARG(i),
     96  1.1  dholland 		    KAUTH_ARG(QL_BLOCK), NULL) != 0) {
     97  1.1  dholland 			mutex_enter(&dq->dq_interlock);
     98  1.1  dholland 			error = chkdqchg(ip, change, cred, i);
     99  1.1  dholland 			mutex_exit(&dq->dq_interlock);
    100  1.1  dholland 			if (error != 0)
    101  1.1  dholland 				return (error);
    102  1.1  dholland 		}
    103  1.1  dholland 	}
    104  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++) {
    105  1.1  dholland 		if ((dq = ip->i_dquot[i]) == NODQUOT)
    106  1.1  dholland 			continue;
    107  1.1  dholland 		mutex_enter(&dq->dq_interlock);
    108  1.1  dholland 		dq->dq_curblocks += change;
    109  1.1  dholland 		dq->dq_flags |= DQ_MOD;
    110  1.1  dholland 		mutex_exit(&dq->dq_interlock);
    111  1.1  dholland 	}
    112  1.1  dholland 	return (0);
    113  1.1  dholland }
    114  1.1  dholland 
    115  1.1  dholland /*
    116  1.1  dholland  * Check for a valid change to a users allocation.
    117  1.1  dholland  * Issue an error message if appropriate.
    118  1.1  dholland  */
    119  1.1  dholland static int
    120  1.1  dholland chkdqchg(struct inode *ip, int64_t change, kauth_cred_t cred, int type)
    121  1.1  dholland {
    122  1.1  dholland 	struct dquot *dq = ip->i_dquot[type];
    123  1.1  dholland 	long ncurblocks = dq->dq_curblocks + change;
    124  1.1  dholland 
    125  1.1  dholland 	KASSERT(mutex_owned(&dq->dq_interlock));
    126  1.1  dholland 	/*
    127  1.1  dholland 	 * If user would exceed their hard limit, disallow space allocation.
    128  1.1  dholland 	 */
    129  1.1  dholland 	if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
    130  1.1  dholland 		if ((dq->dq_flags & DQ_WARN(QL_BLOCK)) == 0 &&
    131  1.1  dholland 		    ip->i_uid == kauth_cred_geteuid(cred)) {
    132  1.1  dholland 			uprintf("\n%s: write failed, %s disk limit reached\n",
    133  1.1  dholland 			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    134  1.1  dholland 			    quotatypes[type]);
    135  1.1  dholland 			dq->dq_flags |= DQ_WARN(QL_BLOCK);
    136  1.1  dholland 		}
    137  1.1  dholland 		return (EDQUOT);
    138  1.1  dholland 	}
    139  1.1  dholland 	/*
    140  1.1  dholland 	 * If user is over their soft limit for too long, disallow space
    141  1.1  dholland 	 * allocation. Reset time limit as they cross their soft limit.
    142  1.1  dholland 	 */
    143  1.1  dholland 	if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
    144  1.1  dholland 		if (dq->dq_curblocks < dq->dq_bsoftlimit) {
    145  1.1  dholland 			dq->dq_btime =
    146  1.1  dholland 			    time_second + ip->i_ump->umq1_btime[type];
    147  1.1  dholland 			if (ip->i_uid == kauth_cred_geteuid(cred))
    148  1.1  dholland 				uprintf("\n%s: warning, %s %s\n",
    149  1.1  dholland 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    150  1.1  dholland 				    quotatypes[type], "disk quota exceeded");
    151  1.1  dholland 			return (0);
    152  1.1  dholland 		}
    153  1.1  dholland 		if (time_second > dq->dq_btime) {
    154  1.1  dholland 			if ((dq->dq_flags & DQ_WARN(QL_BLOCK)) == 0 &&
    155  1.1  dholland 			    ip->i_uid == kauth_cred_geteuid(cred)) {
    156  1.1  dholland 				uprintf("\n%s: write failed, %s %s\n",
    157  1.1  dholland 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    158  1.1  dholland 				    quotatypes[type],
    159  1.1  dholland 				    "disk quota exceeded for too long");
    160  1.1  dholland 				dq->dq_flags |= DQ_WARN(QL_BLOCK);
    161  1.1  dholland 			}
    162  1.1  dholland 			return (EDQUOT);
    163  1.1  dholland 		}
    164  1.1  dholland 	}
    165  1.1  dholland 	return (0);
    166  1.1  dholland }
    167  1.1  dholland 
    168  1.1  dholland /*
    169  1.1  dholland  * Check the inode limit, applying corrective action.
    170  1.1  dholland  */
    171  1.1  dholland int
    172  1.1  dholland chkiq1(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
    173  1.1  dholland {
    174  1.1  dholland 	struct dquot *dq;
    175  1.1  dholland 	int i;
    176  1.1  dholland 	int ncurinodes, error;
    177  1.1  dholland 
    178  1.1  dholland 	if ((error = getinoquota(ip)) != 0)
    179  1.1  dholland 		return error;
    180  1.1  dholland 	if (change == 0)
    181  1.1  dholland 		return (0);
    182  1.1  dholland 	if (change < 0) {
    183  1.1  dholland 		for (i = 0; i < MAXQUOTAS; i++) {
    184  1.1  dholland 			if ((dq = ip->i_dquot[i]) == NODQUOT)
    185  1.1  dholland 				continue;
    186  1.1  dholland 			mutex_enter(&dq->dq_interlock);
    187  1.1  dholland 			ncurinodes = dq->dq_curinodes + change;
    188  1.1  dholland 			if (ncurinodes >= 0)
    189  1.1  dholland 				dq->dq_curinodes = ncurinodes;
    190  1.1  dholland 			else
    191  1.1  dholland 				dq->dq_curinodes = 0;
    192  1.1  dholland 			dq->dq_flags &= ~DQ_WARN(QL_FILE);
    193  1.1  dholland 			dq->dq_flags |= DQ_MOD;
    194  1.1  dholland 			mutex_exit(&dq->dq_interlock);
    195  1.1  dholland 		}
    196  1.1  dholland 		return (0);
    197  1.1  dholland 	}
    198  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++) {
    199  1.1  dholland 		if ((dq = ip->i_dquot[i]) == NODQUOT)
    200  1.1  dholland 			continue;
    201  1.1  dholland 		if ((flags & FORCE) == 0 && kauth_authorize_system(cred,
    202  1.1  dholland 		    KAUTH_SYSTEM_FS_QUOTA, KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT,
    203  1.1  dholland 		    KAUTH_ARG(i), KAUTH_ARG(QL_FILE), NULL) != 0) {
    204  1.1  dholland 			mutex_enter(&dq->dq_interlock);
    205  1.1  dholland 			error = chkiqchg(ip, change, cred, i);
    206  1.1  dholland 			mutex_exit(&dq->dq_interlock);
    207  1.1  dholland 			if (error != 0)
    208  1.1  dholland 				return (error);
    209  1.1  dholland 		}
    210  1.1  dholland 	}
    211  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++) {
    212  1.1  dholland 		if ((dq = ip->i_dquot[i]) == NODQUOT)
    213  1.1  dholland 			continue;
    214  1.1  dholland 		mutex_enter(&dq->dq_interlock);
    215  1.1  dholland 		dq->dq_curinodes += change;
    216  1.1  dholland 		dq->dq_flags |= DQ_MOD;
    217  1.1  dholland 		mutex_exit(&dq->dq_interlock);
    218  1.1  dholland 	}
    219  1.1  dholland 	return (0);
    220  1.1  dholland }
    221  1.1  dholland 
    222  1.1  dholland /*
    223  1.1  dholland  * Check for a valid change to a users allocation.
    224  1.1  dholland  * Issue an error message if appropriate.
    225  1.1  dholland  */
    226  1.1  dholland static int
    227  1.1  dholland chkiqchg(struct inode *ip, int32_t change, kauth_cred_t cred, int type)
    228  1.1  dholland {
    229  1.1  dholland 	struct dquot *dq = ip->i_dquot[type];
    230  1.1  dholland 	long ncurinodes = dq->dq_curinodes + change;
    231  1.1  dholland 
    232  1.1  dholland 	KASSERT(mutex_owned(&dq->dq_interlock));
    233  1.1  dholland 	/*
    234  1.1  dholland 	 * If user would exceed their hard limit, disallow inode allocation.
    235  1.1  dholland 	 */
    236  1.1  dholland 	if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
    237  1.1  dholland 		if ((dq->dq_flags & DQ_WARN(QL_FILE)) == 0 &&
    238  1.1  dholland 		    ip->i_uid == kauth_cred_geteuid(cred)) {
    239  1.1  dholland 			uprintf("\n%s: write failed, %s inode limit reached\n",
    240  1.1  dholland 			    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    241  1.1  dholland 			    quotatypes[type]);
    242  1.1  dholland 			dq->dq_flags |= DQ_WARN(QL_FILE);
    243  1.1  dholland 		}
    244  1.1  dholland 		return (EDQUOT);
    245  1.1  dholland 	}
    246  1.1  dholland 	/*
    247  1.1  dholland 	 * If user is over their soft limit for too long, disallow inode
    248  1.1  dholland 	 * allocation. Reset time limit as they cross their soft limit.
    249  1.1  dholland 	 */
    250  1.1  dholland 	if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
    251  1.1  dholland 		if (dq->dq_curinodes < dq->dq_isoftlimit) {
    252  1.1  dholland 			dq->dq_itime =
    253  1.1  dholland 			    time_second + ip->i_ump->umq1_itime[type];
    254  1.1  dholland 			if (ip->i_uid == kauth_cred_geteuid(cred))
    255  1.1  dholland 				uprintf("\n%s: warning, %s %s\n",
    256  1.1  dholland 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    257  1.1  dholland 				    quotatypes[type], "inode quota exceeded");
    258  1.1  dholland 			return (0);
    259  1.1  dholland 		}
    260  1.1  dholland 		if (time_second > dq->dq_itime) {
    261  1.1  dholland 			if ((dq->dq_flags & DQ_WARN(QL_FILE)) == 0 &&
    262  1.1  dholland 			    ip->i_uid == kauth_cred_geteuid(cred)) {
    263  1.1  dholland 				uprintf("\n%s: write failed, %s %s\n",
    264  1.1  dholland 				    ITOV(ip)->v_mount->mnt_stat.f_mntonname,
    265  1.1  dholland 				    quotatypes[type],
    266  1.1  dholland 				    "inode quota exceeded for too long");
    267  1.1  dholland 				dq->dq_flags |= DQ_WARN(QL_FILE);
    268  1.1  dholland 			}
    269  1.1  dholland 			return (EDQUOT);
    270  1.1  dholland 		}
    271  1.1  dholland 	}
    272  1.1  dholland 	return (0);
    273  1.1  dholland }
    274  1.1  dholland 
    275  1.1  dholland int
    276  1.1  dholland quota1_umount(struct mount *mp, int flags)
    277  1.1  dholland {
    278  1.1  dholland 	int i, error;
    279  1.1  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    280  1.1  dholland 	struct lwp *l = curlwp;
    281  1.1  dholland 
    282  1.1  dholland 	if ((ump->um_flags & UFS_QUOTA) == 0)
    283  1.1  dholland 		return 0;
    284  1.1  dholland 
    285  1.1  dholland 	if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
    286  1.1  dholland 		return (error);
    287  1.1  dholland 
    288  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++) {
    289  1.1  dholland 		if (ump->um_quotas[i] != NULLVP) {
    290  1.1  dholland 			quota1_handle_cmd_quotaoff(l, ump, i);
    291  1.1  dholland 		}
    292  1.1  dholland 	}
    293  1.1  dholland 	return 0;
    294  1.1  dholland }
    295  1.1  dholland 
    296  1.1  dholland /*
    297  1.1  dholland  * Code to process quotactl commands.
    298  1.1  dholland  */
    299  1.1  dholland 
    300  1.1  dholland /*
    301  1.1  dholland  * set up a quota file for a particular file system.
    302  1.1  dholland  */
    303  1.1  dholland int
    304  1.1  dholland quota1_handle_cmd_quotaon(struct lwp *l, struct ufsmount *ump, int type,
    305  1.1  dholland     const char *fname)
    306  1.1  dholland {
    307  1.1  dholland 	struct mount *mp = ump->um_mountp;
    308  1.1  dholland 	struct vnode *vp, **vpp, *mvp;
    309  1.1  dholland 	struct dquot *dq;
    310  1.1  dholland 	int error;
    311  1.1  dholland 	struct pathbuf *pb;
    312  1.1  dholland 	struct nameidata nd;
    313  1.1  dholland 
    314  1.1  dholland 	if (ump->um_flags & UFS_QUOTA2) {
    315  1.1  dholland 		uprintf("%s: quotas v2 already enabled\n",
    316  1.1  dholland 		    mp->mnt_stat.f_mntonname);
    317  1.1  dholland 		return (EBUSY);
    318  1.1  dholland 	}
    319  1.1  dholland 
    320  1.1  dholland 	if (mp->mnt_wapbl != NULL) {
    321  1.1  dholland 		printf("%s: quota v1 cannot be used with -o log\n",
    322  1.1  dholland 		    mp->mnt_stat.f_mntonname);
    323  1.1  dholland 		return (EOPNOTSUPP);
    324  1.1  dholland 	}
    325  1.1  dholland 
    326  1.1  dholland 	vpp = &ump->um_quotas[type];
    327  1.1  dholland 
    328  1.1  dholland 	pb = pathbuf_create(fname);
    329  1.1  dholland 	if (pb == NULL) {
    330  1.1  dholland 		return ENOMEM;
    331  1.1  dholland 	}
    332  1.1  dholland 	NDINIT(&nd, LOOKUP, FOLLOW, pb);
    333  1.1  dholland 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    334  1.1  dholland 		pathbuf_destroy(pb);
    335  1.1  dholland 		return error;
    336  1.1  dholland 	}
    337  1.1  dholland 	vp = nd.ni_vp;
    338  1.1  dholland 	pathbuf_destroy(pb);
    339  1.1  dholland 
    340  1.1  dholland 	VOP_UNLOCK(vp);
    341  1.1  dholland 	if (vp->v_type != VREG) {
    342  1.1  dholland 		(void) vn_close(vp, FREAD|FWRITE, l->l_cred);
    343  1.1  dholland 		return (EACCES);
    344  1.1  dholland 	}
    345  1.1  dholland 	if (*vpp != vp)
    346  1.1  dholland 		quota1_handle_cmd_quotaoff(l, ump, type);
    347  1.1  dholland 	mutex_enter(&dqlock);
    348  1.1  dholland 	while ((ump->umq1_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
    349  1.1  dholland 		cv_wait(&dqcv, &dqlock);
    350  1.1  dholland 	ump->umq1_qflags[type] |= QTF_OPENING;
    351  1.1  dholland 	mutex_exit(&dqlock);
    352  1.1  dholland 	mp->mnt_flag |= MNT_QUOTA;
    353  1.1  dholland 	vp->v_vflag |= VV_SYSTEM;	/* XXXSMP */
    354  1.1  dholland 	*vpp = vp;
    355  1.1  dholland 	/*
    356  1.1  dholland 	 * Save the credential of the process that turned on quotas.
    357  1.1  dholland 	 * Set up the time limits for this quota.
    358  1.1  dholland 	 */
    359  1.1  dholland 	kauth_cred_hold(l->l_cred);
    360  1.1  dholland 	ump->um_cred[type] = l->l_cred;
    361  1.1  dholland 	ump->umq1_btime[type] = MAX_DQ_TIME;
    362  1.1  dholland 	ump->umq1_itime[type] = MAX_IQ_TIME;
    363  1.1  dholland 	if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
    364  1.1  dholland 		if (dq->dq_btime > 0)
    365  1.1  dholland 			ump->umq1_btime[type] = dq->dq_btime;
    366  1.1  dholland 		if (dq->dq_itime > 0)
    367  1.1  dholland 			ump->umq1_itime[type] = dq->dq_itime;
    368  1.1  dholland 		dqrele(NULLVP, dq);
    369  1.1  dholland 	}
    370  1.1  dholland 	/* Allocate a marker vnode. */
    371  1.1  dholland 	mvp = vnalloc(mp);
    372  1.1  dholland 	/*
    373  1.1  dholland 	 * Search vnodes associated with this mount point,
    374  1.1  dholland 	 * adding references to quota file being opened.
    375  1.1  dholland 	 * NB: only need to add dquot's for inodes being modified.
    376  1.1  dholland 	 */
    377  1.1  dholland 	mutex_enter(&mntvnode_lock);
    378  1.1  dholland again:
    379  1.1  dholland 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    380  1.1  dholland 		vmark(mvp, vp);
    381  1.1  dholland 		mutex_enter(vp->v_interlock);
    382  1.1  dholland 		if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
    383  1.1  dholland 		    vp->v_type == VNON || vp->v_writecount == 0 ||
    384  1.1  dholland 		    (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
    385  1.1  dholland 			mutex_exit(vp->v_interlock);
    386  1.1  dholland 			continue;
    387  1.1  dholland 		}
    388  1.1  dholland 		mutex_exit(&mntvnode_lock);
    389  1.1  dholland 		if (vget(vp, LK_EXCLUSIVE)) {
    390  1.1  dholland 			mutex_enter(&mntvnode_lock);
    391  1.1  dholland 			(void)vunmark(mvp);
    392  1.1  dholland 			goto again;
    393  1.1  dholland 		}
    394  1.1  dholland 		if ((error = getinoquota(VTOI(vp))) != 0) {
    395  1.1  dholland 			vput(vp);
    396  1.1  dholland 			mutex_enter(&mntvnode_lock);
    397  1.1  dholland 			(void)vunmark(mvp);
    398  1.1  dholland 			break;
    399  1.1  dholland 		}
    400  1.1  dholland 		vput(vp);
    401  1.1  dholland 		mutex_enter(&mntvnode_lock);
    402  1.1  dholland 	}
    403  1.1  dholland 	mutex_exit(&mntvnode_lock);
    404  1.1  dholland 	vnfree(mvp);
    405  1.1  dholland 
    406  1.1  dholland 	mutex_enter(&dqlock);
    407  1.1  dholland 	ump->umq1_qflags[type] &= ~QTF_OPENING;
    408  1.1  dholland 	cv_broadcast(&dqcv);
    409  1.1  dholland 	if (error == 0)
    410  1.1  dholland 		ump->um_flags |= UFS_QUOTA;
    411  1.1  dholland 	mutex_exit(&dqlock);
    412  1.1  dholland 	if (error)
    413  1.1  dholland 		quota1_handle_cmd_quotaoff(l, ump, type);
    414  1.1  dholland 	return (error);
    415  1.1  dholland }
    416  1.1  dholland 
    417  1.1  dholland /*
    418  1.1  dholland  * turn off disk quotas for a filesystem.
    419  1.1  dholland  */
    420  1.1  dholland int
    421  1.1  dholland quota1_handle_cmd_quotaoff(struct lwp *l, struct ufsmount *ump, int type)
    422  1.1  dholland {
    423  1.1  dholland 	struct mount *mp = ump->um_mountp;
    424  1.1  dholland 	struct vnode *vp;
    425  1.1  dholland 	struct vnode *qvp, *mvp;
    426  1.1  dholland 	struct dquot *dq;
    427  1.1  dholland 	struct inode *ip;
    428  1.1  dholland 	kauth_cred_t cred;
    429  1.1  dholland 	int i, error;
    430  1.1  dholland 
    431  1.1  dholland 	/* Allocate a marker vnode. */
    432  1.1  dholland 	mvp = vnalloc(mp);
    433  1.1  dholland 
    434  1.1  dholland 	mutex_enter(&dqlock);
    435  1.1  dholland 	while ((ump->umq1_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
    436  1.1  dholland 		cv_wait(&dqcv, &dqlock);
    437  1.1  dholland 	if ((qvp = ump->um_quotas[type]) == NULLVP) {
    438  1.1  dholland 		mutex_exit(&dqlock);
    439  1.1  dholland 		vnfree(mvp);
    440  1.1  dholland 		return (0);
    441  1.1  dholland 	}
    442  1.1  dholland 	ump->umq1_qflags[type] |= QTF_CLOSING;
    443  1.1  dholland 	ump->um_flags &= ~UFS_QUOTA;
    444  1.1  dholland 	mutex_exit(&dqlock);
    445  1.1  dholland 	/*
    446  1.1  dholland 	 * Search vnodes associated with this mount point,
    447  1.1  dholland 	 * deleting any references to quota file being closed.
    448  1.1  dholland 	 */
    449  1.1  dholland 	mutex_enter(&mntvnode_lock);
    450  1.1  dholland again:
    451  1.1  dholland 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    452  1.1  dholland 		vmark(mvp, vp);
    453  1.1  dholland 		mutex_enter(vp->v_interlock);
    454  1.1  dholland 		if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
    455  1.1  dholland 		    vp->v_type == VNON ||
    456  1.1  dholland 		    (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
    457  1.1  dholland 			mutex_exit(vp->v_interlock);
    458  1.1  dholland 			continue;
    459  1.1  dholland 		}
    460  1.1  dholland 		mutex_exit(&mntvnode_lock);
    461  1.1  dholland 		if (vget(vp, LK_EXCLUSIVE)) {
    462  1.1  dholland 			mutex_enter(&mntvnode_lock);
    463  1.1  dholland 			(void)vunmark(mvp);
    464  1.1  dholland 			goto again;
    465  1.1  dholland 		}
    466  1.1  dholland 		ip = VTOI(vp);
    467  1.1  dholland 		dq = ip->i_dquot[type];
    468  1.1  dholland 		ip->i_dquot[type] = NODQUOT;
    469  1.1  dholland 		dqrele(vp, dq);
    470  1.1  dholland 		vput(vp);
    471  1.1  dholland 		mutex_enter(&mntvnode_lock);
    472  1.1  dholland 	}
    473  1.1  dholland 	mutex_exit(&mntvnode_lock);
    474  1.1  dholland #ifdef DIAGNOSTIC
    475  1.1  dholland 	dqflush(qvp);
    476  1.1  dholland #endif
    477  1.1  dholland 	qvp->v_vflag &= ~VV_SYSTEM;
    478  1.1  dholland 	error = vn_close(qvp, FREAD|FWRITE, l->l_cred);
    479  1.1  dholland 	mutex_enter(&dqlock);
    480  1.1  dholland 	ump->um_quotas[type] = NULLVP;
    481  1.1  dholland 	cred = ump->um_cred[type];
    482  1.1  dholland 	ump->um_cred[type] = NOCRED;
    483  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++)
    484  1.1  dholland 		if (ump->um_quotas[i] != NULLVP)
    485  1.1  dholland 			break;
    486  1.1  dholland 	ump->umq1_qflags[type] &= ~QTF_CLOSING;
    487  1.1  dholland 	cv_broadcast(&dqcv);
    488  1.1  dholland 	mutex_exit(&dqlock);
    489  1.1  dholland 	kauth_cred_free(cred);
    490  1.1  dholland 	if (i == MAXQUOTAS)
    491  1.1  dholland 		mp->mnt_flag &= ~MNT_QUOTA;
    492  1.1  dholland 	return (error);
    493  1.1  dholland }
    494  1.1  dholland 
    495  1.1  dholland int
    496  1.1  dholland quota1_handle_cmd_get(struct ufsmount *ump, const struct quotakey *qk,
    497  1.1  dholland     struct quotaval *qv)
    498  1.1  dholland {
    499  1.1  dholland 	struct dquot *dq;
    500  1.1  dholland 	int error;
    501  1.1  dholland 	struct quotaval blocks, files;
    502  1.1  dholland 	int idtype;
    503  1.1  dholland 	id_t id;
    504  1.1  dholland 
    505  1.1  dholland 	idtype = qk->qk_idtype;
    506  1.1  dholland 	id = qk->qk_id;
    507  1.1  dholland 
    508  1.1  dholland 	if (ump->um_quotas[idtype] == NULLVP)
    509  1.1  dholland 		return ENODEV;
    510  1.1  dholland 
    511  1.1  dholland 	if (id == QUOTA_DEFAULTID) { /* we want the grace period of id 0 */
    512  1.1  dholland 		if ((error = dqget(NULLVP, 0, ump, idtype, &dq)) != 0)
    513  1.1  dholland 			return error;
    514  1.1  dholland 
    515  1.1  dholland 	} else {
    516  1.1  dholland 		if ((error = dqget(NULLVP, id, ump, idtype, &dq)) != 0)
    517  1.1  dholland 			return error;
    518  1.1  dholland 	}
    519  1.1  dholland 	dqblk_to_quotavals(&dq->dq_un.dq1_dqb, &blocks, &files);
    520  1.1  dholland 	dqrele(NULLVP, dq);
    521  1.1  dholland 	if (id == QUOTA_DEFAULTID) {
    522  1.1  dholland 		if (blocks.qv_expiretime > 0)
    523  1.1  dholland 			blocks.qv_grace = blocks.qv_expiretime;
    524  1.1  dholland 		else
    525  1.1  dholland 			blocks.qv_grace = MAX_DQ_TIME;
    526  1.1  dholland 		if (files.qv_expiretime > 0)
    527  1.1  dholland 			files.qv_grace = files.qv_expiretime;
    528  1.1  dholland 		else
    529  1.1  dholland 			files.qv_grace = MAX_DQ_TIME;
    530  1.1  dholland 	}
    531  1.1  dholland 
    532  1.1  dholland 	switch (qk->qk_objtype) {
    533  1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    534  1.1  dholland 		*qv = blocks;
    535  1.1  dholland 		break;
    536  1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    537  1.1  dholland 		*qv = files;
    538  1.1  dholland 		break;
    539  1.1  dholland 	    default:
    540  1.1  dholland 		return EINVAL;
    541  1.1  dholland 	}
    542  1.1  dholland 
    543  1.1  dholland 	return 0;
    544  1.1  dholland }
    545  1.1  dholland 
    546  1.1  dholland static uint32_t
    547  1.1  dholland quota1_encode_limit(uint64_t lim)
    548  1.1  dholland {
    549  1.1  dholland 	if (lim == QUOTA_NOLIMIT || lim >= 0xffffffff) {
    550  1.1  dholland 		return 0;
    551  1.1  dholland 	}
    552  1.1  dholland 	return lim;
    553  1.1  dholland }
    554  1.1  dholland 
    555  1.1  dholland int
    556  1.1  dholland quota1_handle_cmd_put(struct ufsmount *ump, const struct quotakey *key,
    557  1.1  dholland     const struct quotaval *val)
    558  1.1  dholland {
    559  1.1  dholland 	struct dquot *dq;
    560  1.1  dholland 	struct dqblk dqb;
    561  1.1  dholland 	int error;
    562  1.1  dholland 
    563  1.1  dholland 	switch (key->qk_idtype) {
    564  1.1  dholland 	    case QUOTA_IDTYPE_USER:
    565  1.1  dholland 	    case QUOTA_IDTYPE_GROUP:
    566  1.1  dholland 		break;
    567  1.1  dholland 	    default:
    568  1.1  dholland 		return EINVAL;
    569  1.1  dholland 	}
    570  1.1  dholland 
    571  1.1  dholland 	switch (key->qk_objtype) {
    572  1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    573  1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    574  1.1  dholland 		break;
    575  1.1  dholland 	    default:
    576  1.1  dholland 		return EINVAL;
    577  1.1  dholland 	}
    578  1.1  dholland 
    579  1.1  dholland 	if (ump->um_quotas[key->qk_idtype] == NULLVP)
    580  1.1  dholland 		return ENODEV;
    581  1.1  dholland 
    582  1.1  dholland 	if (key->qk_id == QUOTA_DEFAULTID) {
    583  1.1  dholland 		/* just update grace times */
    584  1.1  dholland 		id_t id = 0;
    585  1.1  dholland 
    586  1.1  dholland 		if ((error = dqget(NULLVP, id, ump, key->qk_idtype, &dq)) != 0)
    587  1.1  dholland 			return error;
    588  1.1  dholland 		mutex_enter(&dq->dq_interlock);
    589  1.1  dholland 		if (val->qv_grace != QUOTA_NOTIME) {
    590  1.1  dholland 			if (key->qk_objtype == QUOTA_OBJTYPE_BLOCKS)
    591  1.1  dholland 				ump->umq1_btime[key->qk_idtype] = dq->dq_btime =
    592  1.1  dholland 					val->qv_grace;
    593  1.1  dholland 			if (key->qk_objtype == QUOTA_OBJTYPE_FILES)
    594  1.1  dholland 				ump->umq1_itime[key->qk_idtype] = dq->dq_itime =
    595  1.1  dholland 					val->qv_grace;
    596  1.1  dholland 		}
    597  1.1  dholland 		dq->dq_flags |= DQ_MOD;
    598  1.1  dholland 		mutex_exit(&dq->dq_interlock);
    599  1.1  dholland 		dqrele(NULLVP, dq);
    600  1.1  dholland 		return 0;
    601  1.1  dholland 	}
    602  1.1  dholland 
    603  1.1  dholland 	if ((error = dqget(NULLVP, key->qk_id, ump, key->qk_idtype, &dq)) != 0)
    604  1.1  dholland 		return (error);
    605  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    606  1.1  dholland 	/*
    607  1.1  dholland 	 * Copy all but the current values.
    608  1.1  dholland 	 * Reset time limit if previously had no soft limit or were
    609  1.1  dholland 	 * under it, but now have a soft limit and are over it.
    610  1.1  dholland 	 */
    611  1.1  dholland 	dqb.dqb_curblocks = dq->dq_curblocks;
    612  1.1  dholland 	dqb.dqb_curinodes = dq->dq_curinodes;
    613  1.1  dholland 	dqb.dqb_btime = dq->dq_btime;
    614  1.1  dholland 	dqb.dqb_itime = dq->dq_itime;
    615  1.1  dholland 	if (key->qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
    616  1.1  dholland 		dqb.dqb_bsoftlimit = quota1_encode_limit(val->qv_softlimit);
    617  1.1  dholland 		dqb.dqb_bhardlimit = quota1_encode_limit(val->qv_hardlimit);
    618  1.1  dholland 		dqb.dqb_isoftlimit = dq->dq_isoftlimit;
    619  1.1  dholland 		dqb.dqb_ihardlimit = dq->dq_ihardlimit;
    620  1.1  dholland 	} else {
    621  1.1  dholland 		KASSERT(key->qk_objtype == QUOTA_OBJTYPE_FILES);
    622  1.1  dholland 		dqb.dqb_bsoftlimit = dq->dq_bsoftlimit;
    623  1.1  dholland 		dqb.dqb_bhardlimit = dq->dq_bhardlimit;
    624  1.1  dholland 		dqb.dqb_isoftlimit = quota1_encode_limit(val->qv_softlimit);
    625  1.1  dholland 		dqb.dqb_ihardlimit = quota1_encode_limit(val->qv_hardlimit);
    626  1.1  dholland 	}
    627  1.1  dholland 	if (dq->dq_id == 0 && val->qv_grace != QUOTA_NOTIME) {
    628  1.1  dholland 		/* also update grace time if available */
    629  1.1  dholland 		if (key->qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
    630  1.1  dholland 			ump->umq1_btime[key->qk_idtype] = dqb.dqb_btime =
    631  1.1  dholland 				val->qv_grace;
    632  1.1  dholland 		}
    633  1.1  dholland 		if (key->qk_objtype == QUOTA_OBJTYPE_FILES) {
    634  1.1  dholland 			ump->umq1_itime[key->qk_idtype] = dqb.dqb_itime =
    635  1.1  dholland 				val->qv_grace;
    636  1.1  dholland 		}
    637  1.1  dholland 	}
    638  1.1  dholland 	if (dqb.dqb_bsoftlimit &&
    639  1.1  dholland 	    dq->dq_curblocks >= dqb.dqb_bsoftlimit &&
    640  1.1  dholland 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
    641  1.1  dholland 		dqb.dqb_btime = time_second + ump->umq1_btime[key->qk_idtype];
    642  1.1  dholland 	if (dqb.dqb_isoftlimit &&
    643  1.1  dholland 	    dq->dq_curinodes >= dqb.dqb_isoftlimit &&
    644  1.1  dholland 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
    645  1.1  dholland 		dqb.dqb_itime = time_second + ump->umq1_itime[key->qk_idtype];
    646  1.1  dholland 	dq->dq_un.dq1_dqb = dqb;
    647  1.1  dholland 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    648  1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
    649  1.1  dholland 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    650  1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_FILE);
    651  1.1  dholland 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    652  1.1  dholland 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    653  1.1  dholland 		dq->dq_flags |= DQ_FAKE;
    654  1.1  dholland 	else
    655  1.1  dholland 		dq->dq_flags &= ~DQ_FAKE;
    656  1.1  dholland 	dq->dq_flags |= DQ_MOD;
    657  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    658  1.1  dholland 	dqrele(NULLVP, dq);
    659  1.1  dholland 	return (0);
    660  1.1  dholland }
    661  1.1  dholland 
    662  1.1  dholland 
    663  1.1  dholland #if 0
    664  1.1  dholland /*
    665  1.1  dholland  * Q_SETQUOTA - assign an entire dqblk structure.
    666  1.1  dholland  */
    667  1.1  dholland int
    668  1.1  dholland setquota1(struct mount *mp, u_long id, int type, struct dqblk *dqb)
    669  1.1  dholland {
    670  1.1  dholland 	struct dquot *dq;
    671  1.1  dholland 	struct dquot *ndq;
    672  1.1  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    673  1.1  dholland 
    674  1.1  dholland 
    675  1.1  dholland 	if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
    676  1.1  dholland 		return (error);
    677  1.1  dholland 	dq = ndq;
    678  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    679  1.1  dholland 	/*
    680  1.1  dholland 	 * Copy all but the current values.
    681  1.1  dholland 	 * Reset time limit if previously had no soft limit or were
    682  1.1  dholland 	 * under it, but now have a soft limit and are over it.
    683  1.1  dholland 	 */
    684  1.1  dholland 	dqb->dqb_curblocks = dq->dq_curblocks;
    685  1.1  dholland 	dqb->dqb_curinodes = dq->dq_curinodes;
    686  1.1  dholland 	if (dq->dq_id != 0) {
    687  1.1  dholland 		dqb->dqb_btime = dq->dq_btime;
    688  1.1  dholland 		dqb->dqb_itime = dq->dq_itime;
    689  1.1  dholland 	}
    690  1.1  dholland 	if (dqb->dqb_bsoftlimit &&
    691  1.1  dholland 	    dq->dq_curblocks >= dqb->dqb_bsoftlimit &&
    692  1.1  dholland 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
    693  1.1  dholland 		dqb->dqb_btime = time_second + ump->umq1_btime[type];
    694  1.1  dholland 	if (dqb->dqb_isoftlimit &&
    695  1.1  dholland 	    dq->dq_curinodes >= dqb->dqb_isoftlimit &&
    696  1.1  dholland 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
    697  1.1  dholland 		dqb->dqb_itime = time_second + ump->umq1_itime[type];
    698  1.1  dholland 	dq->dq_un.dq1_dqb = *dqb;
    699  1.1  dholland 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    700  1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
    701  1.1  dholland 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    702  1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_FILE);
    703  1.1  dholland 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    704  1.1  dholland 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    705  1.1  dholland 		dq->dq_flags |= DQ_FAKE;
    706  1.1  dholland 	else
    707  1.1  dholland 		dq->dq_flags &= ~DQ_FAKE;
    708  1.1  dholland 	dq->dq_flags |= DQ_MOD;
    709  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    710  1.1  dholland 	dqrele(NULLVP, dq);
    711  1.1  dholland 	return (0);
    712  1.1  dholland }
    713  1.1  dholland 
    714  1.1  dholland /*
    715  1.1  dholland  * Q_SETUSE - set current inode and block usage.
    716  1.1  dholland  */
    717  1.1  dholland int
    718  1.1  dholland setuse(struct mount *mp, u_long id, int type, void *addr)
    719  1.1  dholland {
    720  1.1  dholland 	struct dquot *dq;
    721  1.1  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    722  1.1  dholland 	struct dquot *ndq;
    723  1.1  dholland 	struct dqblk usage;
    724  1.1  dholland 	int error;
    725  1.1  dholland 
    726  1.1  dholland 	error = copyin(addr, (void *)&usage, sizeof (struct dqblk));
    727  1.1  dholland 	if (error)
    728  1.1  dholland 		return (error);
    729  1.1  dholland 	if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
    730  1.1  dholland 		return (error);
    731  1.1  dholland 	dq = ndq;
    732  1.1  dholland 	mutex_enter(&dq->dq_interlock);
    733  1.1  dholland 	/*
    734  1.1  dholland 	 * Reset time limit if have a soft limit and were
    735  1.1  dholland 	 * previously under it, but are now over it.
    736  1.1  dholland 	 */
    737  1.1  dholland 	if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
    738  1.1  dholland 	    usage.dqb_curblocks >= dq->dq_bsoftlimit)
    739  1.1  dholland 		dq->dq_btime = time_second + ump->umq1_btime[type];
    740  1.1  dholland 	if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
    741  1.1  dholland 	    usage.dqb_curinodes >= dq->dq_isoftlimit)
    742  1.1  dholland 		dq->dq_itime = time_second + ump->umq1_itime[type];
    743  1.1  dholland 	dq->dq_curblocks = usage.dqb_curblocks;
    744  1.1  dholland 	dq->dq_curinodes = usage.dqb_curinodes;
    745  1.1  dholland 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    746  1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
    747  1.1  dholland 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    748  1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_FILE);
    749  1.1  dholland 	dq->dq_flags |= DQ_MOD;
    750  1.1  dholland 	mutex_exit(&dq->dq_interlock);
    751  1.1  dholland 	dqrele(NULLVP, dq);
    752  1.1  dholland 	return (0);
    753  1.1  dholland }
    754  1.1  dholland #endif
    755  1.1  dholland 
    756  1.1  dholland /*
    757  1.1  dholland  * Q_SYNC - sync quota files to disk.
    758  1.1  dholland  */
    759  1.1  dholland int
    760  1.1  dholland q1sync(struct mount *mp)
    761  1.1  dholland {
    762  1.1  dholland 	struct ufsmount *ump = VFSTOUFS(mp);
    763  1.1  dholland 	struct vnode *vp, *mvp;
    764  1.1  dholland 	struct dquot *dq;
    765  1.1  dholland 	int i, error;
    766  1.1  dholland 
    767  1.1  dholland 	/*
    768  1.1  dholland 	 * Check if the mount point has any quotas.
    769  1.1  dholland 	 * If not, simply return.
    770  1.1  dholland 	 */
    771  1.1  dholland 	for (i = 0; i < MAXQUOTAS; i++)
    772  1.1  dholland 		if (ump->um_quotas[i] != NULLVP)
    773  1.1  dholland 			break;
    774  1.1  dholland 	if (i == MAXQUOTAS)
    775  1.1  dholland 		return (0);
    776  1.1  dholland 
    777  1.1  dholland 	/* Allocate a marker vnode. */
    778  1.1  dholland 	mvp = vnalloc(mp);
    779  1.1  dholland 
    780  1.1  dholland 	/*
    781  1.1  dholland 	 * Search vnodes associated with this mount point,
    782  1.1  dholland 	 * synchronizing any modified dquot structures.
    783  1.1  dholland 	 */
    784  1.1  dholland 	mutex_enter(&mntvnode_lock);
    785  1.1  dholland  again:
    786  1.1  dholland 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    787  1.1  dholland 		vmark(mvp, vp);
    788  1.1  dholland 		mutex_enter(vp->v_interlock);
    789  1.1  dholland 		if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
    790  1.1  dholland 		    vp->v_type == VNON ||
    791  1.1  dholland 		    (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
    792  1.1  dholland 			mutex_exit(vp->v_interlock);
    793  1.1  dholland 			continue;
    794  1.1  dholland 		}
    795  1.1  dholland 		mutex_exit(&mntvnode_lock);
    796  1.1  dholland 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
    797  1.1  dholland 		if (error) {
    798  1.1  dholland 			mutex_enter(&mntvnode_lock);
    799  1.1  dholland 			if (error == ENOENT) {
    800  1.1  dholland 				(void)vunmark(mvp);
    801  1.1  dholland 				goto again;
    802  1.1  dholland 			}
    803  1.1  dholland 			continue;
    804  1.1  dholland 		}
    805  1.1  dholland 		for (i = 0; i < MAXQUOTAS; i++) {
    806  1.1  dholland 			dq = VTOI(vp)->i_dquot[i];
    807  1.1  dholland 			if (dq == NODQUOT)
    808  1.1  dholland 				continue;
    809  1.1  dholland 			mutex_enter(&dq->dq_interlock);
    810  1.1  dholland 			if (dq->dq_flags & DQ_MOD)
    811  1.1  dholland 				dq1sync(vp, dq);
    812  1.1  dholland 			mutex_exit(&dq->dq_interlock);
    813  1.1  dholland 		}
    814  1.1  dholland 		vput(vp);
    815  1.1  dholland 		mutex_enter(&mntvnode_lock);
    816  1.1  dholland 	}
    817  1.1  dholland 	mutex_exit(&mntvnode_lock);
    818  1.1  dholland 	vnfree(mvp);
    819  1.1  dholland 	return (0);
    820  1.1  dholland }
    821  1.1  dholland 
    822  1.1  dholland /*
    823  1.1  dholland  * Obtain a dquot structure for the specified identifier and quota file
    824  1.1  dholland  * reading the information from the file if necessary.
    825  1.1  dholland  */
    826  1.1  dholland int
    827  1.1  dholland dq1get(struct vnode *dqvp, u_long id, struct ufsmount *ump, int type,
    828  1.1  dholland     struct dquot *dq)
    829  1.1  dholland {
    830  1.1  dholland 	struct iovec aiov;
    831  1.1  dholland 	struct uio auio;
    832  1.1  dholland 	int error;
    833  1.1  dholland 
    834  1.1  dholland 	KASSERT(mutex_owned(&dq->dq_interlock));
    835  1.1  dholland 	vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
    836  1.1  dholland 	auio.uio_iov = &aiov;
    837  1.1  dholland 	auio.uio_iovcnt = 1;
    838  1.1  dholland 	aiov.iov_base = (void *)&dq->dq_un.dq1_dqb;
    839  1.1  dholland 	aiov.iov_len = sizeof (struct dqblk);
    840  1.1  dholland 	auio.uio_resid = sizeof (struct dqblk);
    841  1.1  dholland 	auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
    842  1.1  dholland 	auio.uio_rw = UIO_READ;
    843  1.1  dholland 	UIO_SETUP_SYSSPACE(&auio);
    844  1.1  dholland 	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
    845  1.1  dholland 	if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
    846  1.1  dholland 		memset((void *)&dq->dq_un.dq1_dqb, 0, sizeof(struct dqblk));
    847  1.1  dholland 	VOP_UNLOCK(dqvp);
    848  1.1  dholland 	/*
    849  1.1  dholland 	 * I/O error in reading quota file, release
    850  1.1  dholland 	 * quota structure and reflect problem to caller.
    851  1.1  dholland 	 */
    852  1.1  dholland 	if (error)
    853  1.1  dholland 		return (error);
    854  1.1  dholland 	/*
    855  1.1  dholland 	 * Check for no limit to enforce.
    856  1.1  dholland 	 * Initialize time values if necessary.
    857  1.1  dholland 	 */
    858  1.1  dholland 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    859  1.1  dholland 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    860  1.1  dholland 		dq->dq_flags |= DQ_FAKE;
    861  1.1  dholland 	if (dq->dq_id != 0) {
    862  1.1  dholland 		if (dq->dq_btime == 0)
    863  1.1  dholland 			dq->dq_btime = time_second + ump->umq1_btime[type];
    864  1.1  dholland 		if (dq->dq_itime == 0)
    865  1.1  dholland 			dq->dq_itime = time_second + ump->umq1_itime[type];
    866  1.1  dholland 	}
    867  1.1  dholland 	return (0);
    868  1.1  dholland }
    869  1.1  dholland 
    870  1.1  dholland /*
    871  1.1  dholland  * Update the disk quota in the quota file.
    872  1.1  dholland  */
    873  1.1  dholland int
    874  1.1  dholland dq1sync(struct vnode *vp, struct dquot *dq)
    875  1.1  dholland {
    876  1.1  dholland 	struct vnode *dqvp;
    877  1.1  dholland 	struct iovec aiov;
    878  1.1  dholland 	struct uio auio;
    879  1.1  dholland 	int error;
    880  1.1  dholland 
    881  1.1  dholland 	if (dq == NODQUOT)
    882  1.1  dholland 		panic("dq1sync: dquot");
    883  1.1  dholland 	KASSERT(mutex_owned(&dq->dq_interlock));
    884  1.1  dholland 	if ((dq->dq_flags & DQ_MOD) == 0)
    885  1.1  dholland 		return (0);
    886  1.1  dholland 	if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
    887  1.1  dholland 		panic("dq1sync: file");
    888  1.1  dholland 	KASSERT(dqvp != vp);
    889  1.1  dholland 	vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
    890  1.1  dholland 	auio.uio_iov = &aiov;
    891  1.1  dholland 	auio.uio_iovcnt = 1;
    892  1.1  dholland 	aiov.iov_base = (void *)&dq->dq_un.dq1_dqb;
    893  1.1  dholland 	aiov.iov_len = sizeof (struct dqblk);
    894  1.1  dholland 	auio.uio_resid = sizeof (struct dqblk);
    895  1.1  dholland 	auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
    896  1.1  dholland 	auio.uio_rw = UIO_WRITE;
    897  1.1  dholland 	UIO_SETUP_SYSSPACE(&auio);
    898  1.1  dholland 	error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
    899  1.1  dholland 	if (auio.uio_resid && error == 0)
    900  1.1  dholland 		error = EIO;
    901  1.1  dholland 	dq->dq_flags &= ~DQ_MOD;
    902  1.1  dholland 	VOP_UNLOCK(dqvp);
    903  1.1  dholland 	return (error);
    904  1.1  dholland }
    905