Home | History | Annotate | Line # | Download | only in lfs
ulfs_quota1.c revision 1.11
      1  1.11  dholland /*	$NetBSD: ulfs_quota1.c,v 1.11 2016/06/20 00:53:10 dholland Exp $	*/
      2  1.11  dholland /*  from NetBSD: ufs_quota1.c,v 1.22 2016/06/20 00:52:04 dholland 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.11  dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_quota1.c,v 1.11 2016/06/20 00:53:10 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.4  dholland lfs_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.4  dholland 	if ((error = lfs_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.3  dholland 		for (i = 0; i < ULFS_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.3  dholland 	for (i = 0; i < ULFS_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.3  dholland 	for (i = 0; i < ULFS_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.4  dholland 			    lfs_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.4  dholland 				    lfs_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.4  dholland 				    lfs_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.4  dholland lfs_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.4  dholland 	if ((error = lfs_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.3  dholland 		for (i = 0; i < ULFS_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.3  dholland 	for (i = 0; i < ULFS_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.3  dholland 	for (i = 0; i < ULFS_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.4  dholland 			    lfs_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.4  dholland 				    lfs_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.4  dholland 				    lfs_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.4  dholland lfsquota1_umount(struct mount *mp, int flags)
    277   1.1  dholland {
    278   1.1  dholland 	int i, error;
    279   1.3  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    280   1.6  dholland 	struct lfs *fs = ump->um_lfs;
    281   1.1  dholland 	struct lwp *l = curlwp;
    282   1.1  dholland 
    283   1.6  dholland 	if ((fs->um_flags & ULFS_QUOTA) == 0)
    284   1.1  dholland 		return 0;
    285   1.1  dholland 
    286   1.1  dholland 	if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
    287   1.1  dholland 		return (error);
    288   1.1  dholland 
    289   1.3  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    290   1.1  dholland 		if (ump->um_quotas[i] != NULLVP) {
    291   1.4  dholland 			lfsquota1_handle_cmd_quotaoff(l, ump, i);
    292   1.1  dholland 		}
    293   1.1  dholland 	}
    294   1.1  dholland 	return 0;
    295   1.1  dholland }
    296   1.1  dholland 
    297   1.1  dholland /*
    298   1.1  dholland  * Code to process quotactl commands.
    299   1.1  dholland  */
    300   1.1  dholland 
    301   1.1  dholland /*
    302   1.1  dholland  * set up a quota file for a particular file system.
    303   1.1  dholland  */
    304   1.1  dholland int
    305   1.4  dholland lfsquota1_handle_cmd_quotaon(struct lwp *l, struct ulfsmount *ump, int type,
    306   1.1  dholland     const char *fname)
    307   1.1  dholland {
    308   1.1  dholland 	struct mount *mp = ump->um_mountp;
    309   1.6  dholland 	struct lfs *fs = ump->um_lfs;
    310   1.7   hannken 	struct vnode *vp, **vpp;
    311   1.7   hannken 	struct vnode_iterator *marker;
    312   1.1  dholland 	struct dquot *dq;
    313   1.1  dholland 	int error;
    314   1.1  dholland 	struct pathbuf *pb;
    315   1.1  dholland 	struct nameidata nd;
    316   1.1  dholland 
    317   1.6  dholland 	if (fs->um_flags & ULFS_QUOTA2) {
    318   1.1  dholland 		uprintf("%s: quotas v2 already enabled\n",
    319   1.1  dholland 		    mp->mnt_stat.f_mntonname);
    320   1.1  dholland 		return (EBUSY);
    321   1.1  dholland 	}
    322   1.1  dholland 
    323   1.1  dholland 	vpp = &ump->um_quotas[type];
    324   1.1  dholland 
    325   1.1  dholland 	pb = pathbuf_create(fname);
    326   1.1  dholland 	if (pb == NULL) {
    327   1.1  dholland 		return ENOMEM;
    328   1.1  dholland 	}
    329   1.1  dholland 	NDINIT(&nd, LOOKUP, FOLLOW, pb);
    330   1.1  dholland 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    331   1.1  dholland 		pathbuf_destroy(pb);
    332   1.1  dholland 		return error;
    333   1.1  dholland 	}
    334   1.1  dholland 	vp = nd.ni_vp;
    335   1.1  dholland 	pathbuf_destroy(pb);
    336   1.1  dholland 
    337   1.1  dholland 	VOP_UNLOCK(vp);
    338   1.1  dholland 	if (vp->v_type != VREG) {
    339   1.1  dholland 		(void) vn_close(vp, FREAD|FWRITE, l->l_cred);
    340   1.1  dholland 		return (EACCES);
    341   1.1  dholland 	}
    342   1.1  dholland 	if (*vpp != vp)
    343   1.4  dholland 		lfsquota1_handle_cmd_quotaoff(l, ump, type);
    344   1.4  dholland 	mutex_enter(&lfs_dqlock);
    345   1.1  dholland 	while ((ump->umq1_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
    346   1.4  dholland 		cv_wait(&lfs_dqcv, &lfs_dqlock);
    347   1.1  dholland 	ump->umq1_qflags[type] |= QTF_OPENING;
    348   1.4  dholland 	mutex_exit(&lfs_dqlock);
    349   1.1  dholland 	mp->mnt_flag |= MNT_QUOTA;
    350   1.1  dholland 	vp->v_vflag |= VV_SYSTEM;	/* XXXSMP */
    351   1.1  dholland 	*vpp = vp;
    352   1.1  dholland 	/*
    353   1.1  dholland 	 * Save the credential of the process that turned on quotas.
    354   1.1  dholland 	 * Set up the time limits for this quota.
    355   1.1  dholland 	 */
    356   1.1  dholland 	kauth_cred_hold(l->l_cred);
    357   1.1  dholland 	ump->um_cred[type] = l->l_cred;
    358   1.1  dholland 	ump->umq1_btime[type] = MAX_DQ_TIME;
    359   1.1  dholland 	ump->umq1_itime[type] = MAX_IQ_TIME;
    360   1.4  dholland 	if (lfs_dqget(NULLVP, 0, ump, type, &dq) == 0) {
    361   1.1  dholland 		if (dq->dq_btime > 0)
    362   1.1  dholland 			ump->umq1_btime[type] = dq->dq_btime;
    363   1.1  dholland 		if (dq->dq_itime > 0)
    364   1.1  dholland 			ump->umq1_itime[type] = dq->dq_itime;
    365   1.4  dholland 		lfs_dqrele(NULLVP, dq);
    366   1.1  dholland 	}
    367   1.1  dholland 	/*
    368   1.1  dholland 	 * Search vnodes associated with this mount point,
    369   1.1  dholland 	 * adding references to quota file being opened.
    370   1.1  dholland 	 * NB: only need to add dquot's for inodes being modified.
    371   1.1  dholland 	 */
    372   1.7   hannken 	vfs_vnode_iterator_init(mp, &marker);
    373   1.8  christos 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    374   1.7   hannken 		error = vn_lock(vp, LK_EXCLUSIVE);
    375   1.7   hannken 		if (error) {
    376   1.7   hannken 			vrele(vp);
    377   1.7   hannken 			continue;
    378   1.7   hannken 		}
    379   1.1  dholland 		mutex_enter(vp->v_interlock);
    380   1.7   hannken 		if (VTOI(vp) == NULL || vp->v_type == VNON ||
    381   1.7   hannken 		    vp->v_writecount == 0) {
    382   1.1  dholland 			mutex_exit(vp->v_interlock);
    383   1.7   hannken 			vput(vp);
    384   1.1  dholland 			continue;
    385   1.1  dholland 		}
    386   1.7   hannken 		mutex_exit(vp->v_interlock);
    387   1.4  dholland 		if ((error = lfs_getinoquota(VTOI(vp))) != 0) {
    388   1.1  dholland 			vput(vp);
    389   1.1  dholland 			break;
    390   1.1  dholland 		}
    391   1.1  dholland 		vput(vp);
    392   1.1  dholland 	}
    393   1.7   hannken 	vfs_vnode_iterator_destroy(marker);
    394   1.1  dholland 
    395   1.4  dholland 	mutex_enter(&lfs_dqlock);
    396   1.1  dholland 	ump->umq1_qflags[type] &= ~QTF_OPENING;
    397   1.4  dholland 	cv_broadcast(&lfs_dqcv);
    398   1.1  dholland 	if (error == 0)
    399   1.6  dholland 		fs->um_flags |= ULFS_QUOTA;
    400   1.4  dholland 	mutex_exit(&lfs_dqlock);
    401   1.1  dholland 	if (error)
    402   1.4  dholland 		lfsquota1_handle_cmd_quotaoff(l, ump, type);
    403   1.1  dholland 	return (error);
    404   1.1  dholland }
    405   1.1  dholland 
    406   1.1  dholland /*
    407   1.1  dholland  * turn off disk quotas for a filesystem.
    408   1.1  dholland  */
    409   1.1  dholland int
    410   1.4  dholland lfsquota1_handle_cmd_quotaoff(struct lwp *l, struct ulfsmount *ump, int type)
    411   1.1  dholland {
    412   1.1  dholland 	struct mount *mp = ump->um_mountp;
    413   1.6  dholland 	struct lfs *fs = ump->um_lfs;
    414   1.1  dholland 	struct vnode *vp;
    415   1.7   hannken 	struct vnode *qvp;
    416   1.7   hannken 	struct vnode_iterator *marker;
    417   1.1  dholland 	struct dquot *dq;
    418   1.1  dholland 	struct inode *ip;
    419   1.1  dholland 	kauth_cred_t cred;
    420   1.1  dholland 	int i, error;
    421   1.1  dholland 
    422   1.4  dholland 	mutex_enter(&lfs_dqlock);
    423   1.1  dholland 	while ((ump->umq1_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
    424   1.4  dholland 		cv_wait(&lfs_dqcv, &lfs_dqlock);
    425   1.1  dholland 	if ((qvp = ump->um_quotas[type]) == NULLVP) {
    426   1.4  dholland 		mutex_exit(&lfs_dqlock);
    427   1.1  dholland 		return (0);
    428   1.1  dholland 	}
    429   1.1  dholland 	ump->umq1_qflags[type] |= QTF_CLOSING;
    430   1.6  dholland 	fs->um_flags &= ~ULFS_QUOTA;
    431   1.4  dholland 	mutex_exit(&lfs_dqlock);
    432   1.1  dholland 	/*
    433   1.1  dholland 	 * Search vnodes associated with this mount point,
    434   1.1  dholland 	 * deleting any references to quota file being closed.
    435   1.1  dholland 	 */
    436   1.7   hannken 	vfs_vnode_iterator_init(mp, &marker);
    437   1.8  christos 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    438   1.7   hannken 		error = vn_lock(vp, LK_EXCLUSIVE);
    439   1.7   hannken 		if (error) {
    440   1.7   hannken 			vrele(vp);
    441   1.1  dholland 			continue;
    442   1.1  dholland 		}
    443   1.7   hannken 		ip = VTOI(vp);
    444   1.7   hannken 		if (ip == NULL || vp->v_type == VNON) {
    445   1.7   hannken 			vput(vp);
    446   1.7   hannken 			continue;
    447   1.1  dholland 		}
    448   1.1  dholland 		dq = ip->i_dquot[type];
    449   1.1  dholland 		ip->i_dquot[type] = NODQUOT;
    450   1.4  dholland 		lfs_dqrele(vp, dq);
    451   1.1  dholland 		vput(vp);
    452   1.1  dholland 	}
    453   1.7   hannken 	vfs_vnode_iterator_destroy(marker);
    454   1.1  dholland #ifdef DIAGNOSTIC
    455   1.4  dholland 	lfs_dqflush(qvp);
    456   1.1  dholland #endif
    457   1.1  dholland 	qvp->v_vflag &= ~VV_SYSTEM;
    458   1.1  dholland 	error = vn_close(qvp, FREAD|FWRITE, l->l_cred);
    459   1.4  dholland 	mutex_enter(&lfs_dqlock);
    460   1.1  dholland 	ump->um_quotas[type] = NULLVP;
    461   1.1  dholland 	cred = ump->um_cred[type];
    462   1.1  dholland 	ump->um_cred[type] = NOCRED;
    463   1.3  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++)
    464   1.1  dholland 		if (ump->um_quotas[i] != NULLVP)
    465   1.1  dholland 			break;
    466   1.1  dholland 	ump->umq1_qflags[type] &= ~QTF_CLOSING;
    467   1.4  dholland 	cv_broadcast(&lfs_dqcv);
    468   1.4  dholland 	mutex_exit(&lfs_dqlock);
    469   1.1  dholland 	kauth_cred_free(cred);
    470   1.3  dholland 	if (i == ULFS_MAXQUOTAS)
    471   1.1  dholland 		mp->mnt_flag &= ~MNT_QUOTA;
    472   1.1  dholland 	return (error);
    473   1.1  dholland }
    474   1.1  dholland 
    475   1.1  dholland int
    476   1.4  dholland lfsquota1_handle_cmd_get(struct ulfsmount *ump, const struct quotakey *qk,
    477   1.1  dholland     struct quotaval *qv)
    478   1.1  dholland {
    479   1.1  dholland 	struct dquot *dq;
    480   1.1  dholland 	int error;
    481   1.1  dholland 	struct quotaval blocks, files;
    482   1.1  dholland 	int idtype;
    483   1.1  dholland 	id_t id;
    484   1.1  dholland 
    485   1.1  dholland 	idtype = qk->qk_idtype;
    486   1.1  dholland 	id = qk->qk_id;
    487   1.1  dholland 
    488   1.1  dholland 	if (ump->um_quotas[idtype] == NULLVP)
    489   1.1  dholland 		return ENODEV;
    490   1.1  dholland 
    491   1.1  dholland 	if (id == QUOTA_DEFAULTID) { /* we want the grace period of id 0 */
    492   1.4  dholland 		if ((error = lfs_dqget(NULLVP, 0, ump, idtype, &dq)) != 0)
    493   1.1  dholland 			return error;
    494   1.1  dholland 
    495   1.1  dholland 	} else {
    496   1.4  dholland 		if ((error = lfs_dqget(NULLVP, id, ump, idtype, &dq)) != 0)
    497   1.1  dholland 			return error;
    498   1.1  dholland 	}
    499   1.4  dholland 	lfs_dqblk_to_quotavals(&dq->dq_un.dq1_dqb, &blocks, &files);
    500   1.4  dholland 	lfs_dqrele(NULLVP, dq);
    501   1.1  dholland 	if (id == QUOTA_DEFAULTID) {
    502   1.1  dholland 		if (blocks.qv_expiretime > 0)
    503   1.1  dholland 			blocks.qv_grace = blocks.qv_expiretime;
    504   1.1  dholland 		else
    505   1.1  dholland 			blocks.qv_grace = MAX_DQ_TIME;
    506   1.1  dholland 		if (files.qv_expiretime > 0)
    507   1.1  dholland 			files.qv_grace = files.qv_expiretime;
    508   1.1  dholland 		else
    509   1.1  dholland 			files.qv_grace = MAX_DQ_TIME;
    510   1.1  dholland 	}
    511   1.1  dholland 
    512   1.1  dholland 	switch (qk->qk_objtype) {
    513   1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    514   1.1  dholland 		*qv = blocks;
    515   1.1  dholland 		break;
    516   1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    517   1.1  dholland 		*qv = files;
    518   1.1  dholland 		break;
    519   1.1  dholland 	    default:
    520   1.1  dholland 		return EINVAL;
    521   1.1  dholland 	}
    522   1.1  dholland 
    523   1.1  dholland 	return 0;
    524   1.1  dholland }
    525   1.1  dholland 
    526   1.1  dholland static uint32_t
    527   1.1  dholland quota1_encode_limit(uint64_t lim)
    528   1.1  dholland {
    529   1.1  dholland 	if (lim == QUOTA_NOLIMIT || lim >= 0xffffffff) {
    530   1.1  dholland 		return 0;
    531   1.1  dholland 	}
    532   1.1  dholland 	return lim;
    533   1.1  dholland }
    534   1.1  dholland 
    535   1.1  dholland int
    536   1.4  dholland lfsquota1_handle_cmd_put(struct ulfsmount *ump, const struct quotakey *key,
    537   1.1  dholland     const struct quotaval *val)
    538   1.1  dholland {
    539   1.1  dholland 	struct dquot *dq;
    540   1.1  dholland 	struct dqblk dqb;
    541   1.1  dholland 	int error;
    542   1.1  dholland 
    543   1.1  dholland 	switch (key->qk_idtype) {
    544   1.1  dholland 	    case QUOTA_IDTYPE_USER:
    545   1.1  dholland 	    case QUOTA_IDTYPE_GROUP:
    546   1.1  dholland 		break;
    547   1.1  dholland 	    default:
    548   1.1  dholland 		return EINVAL;
    549   1.1  dholland 	}
    550   1.1  dholland 
    551   1.1  dholland 	switch (key->qk_objtype) {
    552   1.1  dholland 	    case QUOTA_OBJTYPE_BLOCKS:
    553   1.1  dholland 	    case QUOTA_OBJTYPE_FILES:
    554   1.1  dholland 		break;
    555   1.1  dholland 	    default:
    556   1.1  dholland 		return EINVAL;
    557   1.1  dholland 	}
    558   1.1  dholland 
    559   1.1  dholland 	if (ump->um_quotas[key->qk_idtype] == NULLVP)
    560   1.1  dholland 		return ENODEV;
    561   1.1  dholland 
    562   1.1  dholland 	if (key->qk_id == QUOTA_DEFAULTID) {
    563   1.1  dholland 		/* just update grace times */
    564   1.1  dholland 		id_t id = 0;
    565   1.1  dholland 
    566   1.4  dholland 		if ((error = lfs_dqget(NULLVP, id, ump, key->qk_idtype, &dq)) != 0)
    567   1.1  dholland 			return error;
    568   1.1  dholland 		mutex_enter(&dq->dq_interlock);
    569   1.1  dholland 		if (val->qv_grace != QUOTA_NOTIME) {
    570   1.1  dholland 			if (key->qk_objtype == QUOTA_OBJTYPE_BLOCKS)
    571   1.1  dholland 				ump->umq1_btime[key->qk_idtype] = dq->dq_btime =
    572   1.1  dholland 					val->qv_grace;
    573   1.1  dholland 			if (key->qk_objtype == QUOTA_OBJTYPE_FILES)
    574   1.1  dholland 				ump->umq1_itime[key->qk_idtype] = dq->dq_itime =
    575   1.1  dholland 					val->qv_grace;
    576   1.1  dholland 		}
    577   1.1  dholland 		dq->dq_flags |= DQ_MOD;
    578   1.1  dholland 		mutex_exit(&dq->dq_interlock);
    579   1.4  dholland 		lfs_dqrele(NULLVP, dq);
    580   1.1  dholland 		return 0;
    581   1.1  dholland 	}
    582   1.1  dholland 
    583   1.4  dholland 	if ((error = lfs_dqget(NULLVP, key->qk_id, ump, key->qk_idtype, &dq)) != 0)
    584   1.1  dholland 		return (error);
    585   1.1  dholland 	mutex_enter(&dq->dq_interlock);
    586   1.1  dholland 	/*
    587   1.1  dholland 	 * Copy all but the current values.
    588   1.1  dholland 	 * Reset time limit if previously had no soft limit or were
    589   1.1  dholland 	 * under it, but now have a soft limit and are over it.
    590   1.1  dholland 	 */
    591   1.1  dholland 	dqb.dqb_curblocks = dq->dq_curblocks;
    592   1.1  dholland 	dqb.dqb_curinodes = dq->dq_curinodes;
    593   1.1  dholland 	dqb.dqb_btime = dq->dq_btime;
    594   1.1  dholland 	dqb.dqb_itime = dq->dq_itime;
    595   1.1  dholland 	if (key->qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
    596   1.1  dholland 		dqb.dqb_bsoftlimit = quota1_encode_limit(val->qv_softlimit);
    597   1.1  dholland 		dqb.dqb_bhardlimit = quota1_encode_limit(val->qv_hardlimit);
    598   1.1  dholland 		dqb.dqb_isoftlimit = dq->dq_isoftlimit;
    599   1.1  dholland 		dqb.dqb_ihardlimit = dq->dq_ihardlimit;
    600   1.1  dholland 	} else {
    601   1.1  dholland 		KASSERT(key->qk_objtype == QUOTA_OBJTYPE_FILES);
    602   1.1  dholland 		dqb.dqb_bsoftlimit = dq->dq_bsoftlimit;
    603   1.1  dholland 		dqb.dqb_bhardlimit = dq->dq_bhardlimit;
    604   1.1  dholland 		dqb.dqb_isoftlimit = quota1_encode_limit(val->qv_softlimit);
    605   1.1  dholland 		dqb.dqb_ihardlimit = quota1_encode_limit(val->qv_hardlimit);
    606   1.1  dholland 	}
    607   1.1  dholland 	if (dq->dq_id == 0 && val->qv_grace != QUOTA_NOTIME) {
    608   1.1  dholland 		/* also update grace time if available */
    609   1.1  dholland 		if (key->qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
    610   1.1  dholland 			ump->umq1_btime[key->qk_idtype] = dqb.dqb_btime =
    611   1.1  dholland 				val->qv_grace;
    612   1.1  dholland 		}
    613   1.1  dholland 		if (key->qk_objtype == QUOTA_OBJTYPE_FILES) {
    614   1.1  dholland 			ump->umq1_itime[key->qk_idtype] = dqb.dqb_itime =
    615   1.1  dholland 				val->qv_grace;
    616   1.1  dholland 		}
    617   1.1  dholland 	}
    618   1.1  dholland 	if (dqb.dqb_bsoftlimit &&
    619   1.1  dholland 	    dq->dq_curblocks >= dqb.dqb_bsoftlimit &&
    620   1.1  dholland 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
    621   1.1  dholland 		dqb.dqb_btime = time_second + ump->umq1_btime[key->qk_idtype];
    622   1.1  dholland 	if (dqb.dqb_isoftlimit &&
    623   1.1  dholland 	    dq->dq_curinodes >= dqb.dqb_isoftlimit &&
    624   1.1  dholland 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
    625   1.1  dholland 		dqb.dqb_itime = time_second + ump->umq1_itime[key->qk_idtype];
    626   1.1  dholland 	dq->dq_un.dq1_dqb = dqb;
    627   1.1  dholland 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    628   1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
    629   1.1  dholland 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    630   1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_FILE);
    631   1.1  dholland 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    632   1.1  dholland 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    633   1.1  dholland 		dq->dq_flags |= DQ_FAKE;
    634   1.1  dholland 	else
    635   1.1  dholland 		dq->dq_flags &= ~DQ_FAKE;
    636   1.1  dholland 	dq->dq_flags |= DQ_MOD;
    637   1.1  dholland 	mutex_exit(&dq->dq_interlock);
    638   1.4  dholland 	lfs_dqrele(NULLVP, dq);
    639   1.1  dholland 	return (0);
    640   1.1  dholland }
    641   1.1  dholland 
    642   1.1  dholland 
    643   1.1  dholland #if 0
    644   1.1  dholland /*
    645   1.1  dholland  * Q_SETQUOTA - assign an entire dqblk structure.
    646   1.1  dholland  */
    647   1.1  dholland int
    648   1.1  dholland setquota1(struct mount *mp, u_long id, int type, struct dqblk *dqb)
    649   1.1  dholland {
    650   1.1  dholland 	struct dquot *dq;
    651   1.1  dholland 	struct dquot *ndq;
    652   1.3  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    653   1.1  dholland 
    654   1.1  dholland 
    655   1.4  dholland 	if ((error = lfs_dqget(NULLVP, id, ump, type, &ndq)) != 0)
    656   1.1  dholland 		return (error);
    657   1.1  dholland 	dq = ndq;
    658   1.1  dholland 	mutex_enter(&dq->dq_interlock);
    659   1.1  dholland 	/*
    660   1.1  dholland 	 * Copy all but the current values.
    661   1.1  dholland 	 * Reset time limit if previously had no soft limit or were
    662   1.1  dholland 	 * under it, but now have a soft limit and are over it.
    663   1.1  dholland 	 */
    664   1.1  dholland 	dqb->dqb_curblocks = dq->dq_curblocks;
    665   1.1  dholland 	dqb->dqb_curinodes = dq->dq_curinodes;
    666   1.1  dholland 	if (dq->dq_id != 0) {
    667   1.1  dholland 		dqb->dqb_btime = dq->dq_btime;
    668   1.1  dholland 		dqb->dqb_itime = dq->dq_itime;
    669   1.1  dholland 	}
    670   1.1  dholland 	if (dqb->dqb_bsoftlimit &&
    671   1.1  dholland 	    dq->dq_curblocks >= dqb->dqb_bsoftlimit &&
    672   1.1  dholland 	    (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
    673   1.1  dholland 		dqb->dqb_btime = time_second + ump->umq1_btime[type];
    674   1.1  dholland 	if (dqb->dqb_isoftlimit &&
    675   1.1  dholland 	    dq->dq_curinodes >= dqb->dqb_isoftlimit &&
    676   1.1  dholland 	    (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
    677   1.1  dholland 		dqb->dqb_itime = time_second + ump->umq1_itime[type];
    678   1.1  dholland 	dq->dq_un.dq1_dqb = *dqb;
    679   1.1  dholland 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    680   1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
    681   1.1  dholland 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    682   1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_FILE);
    683   1.1  dholland 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    684   1.1  dholland 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    685   1.1  dholland 		dq->dq_flags |= DQ_FAKE;
    686   1.1  dholland 	else
    687   1.1  dholland 		dq->dq_flags &= ~DQ_FAKE;
    688   1.1  dholland 	dq->dq_flags |= DQ_MOD;
    689   1.1  dholland 	mutex_exit(&dq->dq_interlock);
    690   1.4  dholland 	lfs_dqrele(NULLVP, dq);
    691   1.1  dholland 	return (0);
    692   1.1  dholland }
    693   1.1  dholland 
    694   1.1  dholland /*
    695   1.1  dholland  * Q_SETUSE - set current inode and block usage.
    696   1.1  dholland  */
    697   1.1  dholland int
    698   1.1  dholland setuse(struct mount *mp, u_long id, int type, void *addr)
    699   1.1  dholland {
    700   1.1  dholland 	struct dquot *dq;
    701   1.3  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    702   1.1  dholland 	struct dquot *ndq;
    703   1.1  dholland 	struct dqblk usage;
    704   1.1  dholland 	int error;
    705   1.1  dholland 
    706   1.1  dholland 	error = copyin(addr, (void *)&usage, sizeof (struct dqblk));
    707   1.1  dholland 	if (error)
    708   1.1  dholland 		return (error);
    709   1.4  dholland 	if ((error = lfs_dqget(NULLVP, id, ump, type, &ndq)) != 0)
    710   1.1  dholland 		return (error);
    711   1.1  dholland 	dq = ndq;
    712   1.1  dholland 	mutex_enter(&dq->dq_interlock);
    713   1.1  dholland 	/*
    714   1.1  dholland 	 * Reset time limit if have a soft limit and were
    715   1.1  dholland 	 * previously under it, but are now over it.
    716   1.1  dholland 	 */
    717   1.1  dholland 	if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
    718   1.1  dholland 	    usage.dqb_curblocks >= dq->dq_bsoftlimit)
    719   1.1  dholland 		dq->dq_btime = time_second + ump->umq1_btime[type];
    720   1.1  dholland 	if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
    721   1.1  dholland 	    usage.dqb_curinodes >= dq->dq_isoftlimit)
    722   1.1  dholland 		dq->dq_itime = time_second + ump->umq1_itime[type];
    723   1.1  dholland 	dq->dq_curblocks = usage.dqb_curblocks;
    724   1.1  dholland 	dq->dq_curinodes = usage.dqb_curinodes;
    725   1.1  dholland 	if (dq->dq_curblocks < dq->dq_bsoftlimit)
    726   1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_BLOCK);
    727   1.1  dholland 	if (dq->dq_curinodes < dq->dq_isoftlimit)
    728   1.1  dholland 		dq->dq_flags &= ~DQ_WARN(QL_FILE);
    729   1.1  dholland 	dq->dq_flags |= DQ_MOD;
    730   1.1  dholland 	mutex_exit(&dq->dq_interlock);
    731   1.4  dholland 	lfs_dqrele(NULLVP, dq);
    732   1.1  dholland 	return (0);
    733   1.1  dholland }
    734   1.1  dholland #endif
    735   1.1  dholland 
    736   1.1  dholland /*
    737   1.1  dholland  * Q_SYNC - sync quota files to disk.
    738   1.1  dholland  */
    739   1.1  dholland int
    740   1.4  dholland lfs_q1sync(struct mount *mp)
    741   1.1  dholland {
    742   1.3  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    743   1.7   hannken 	struct vnode *vp;
    744   1.7   hannken 	struct vnode_iterator *marker;
    745   1.1  dholland 	struct dquot *dq;
    746   1.1  dholland 	int i, error;
    747   1.1  dholland 
    748   1.1  dholland 	/*
    749   1.1  dholland 	 * Check if the mount point has any quotas.
    750   1.1  dholland 	 * If not, simply return.
    751   1.1  dholland 	 */
    752   1.3  dholland 	for (i = 0; i < ULFS_MAXQUOTAS; i++)
    753   1.1  dholland 		if (ump->um_quotas[i] != NULLVP)
    754   1.1  dholland 			break;
    755   1.3  dholland 	if (i == ULFS_MAXQUOTAS)
    756   1.1  dholland 		return (0);
    757   1.1  dholland 
    758   1.1  dholland 	/*
    759   1.1  dholland 	 * Search vnodes associated with this mount point,
    760   1.1  dholland 	 * synchronizing any modified dquot structures.
    761   1.1  dholland 	 */
    762   1.7   hannken 	vfs_vnode_iterator_init(mp, &marker);
    763   1.8  christos 	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    764   1.7   hannken 		error = vn_lock(vp, LK_EXCLUSIVE);
    765   1.7   hannken 		if (error) {
    766   1.7   hannken 			vrele(vp);
    767   1.1  dholland 			continue;
    768   1.1  dholland 		}
    769   1.7   hannken 		if (VTOI(vp) == NULL || vp->v_type == VNON) {
    770   1.7   hannken 			vput(vp);
    771   1.1  dholland 			continue;
    772   1.1  dholland 		}
    773   1.3  dholland 		for (i = 0; i < ULFS_MAXQUOTAS; i++) {
    774   1.1  dholland 			dq = VTOI(vp)->i_dquot[i];
    775   1.1  dholland 			if (dq == NODQUOT)
    776   1.1  dholland 				continue;
    777   1.1  dholland 			mutex_enter(&dq->dq_interlock);
    778   1.1  dholland 			if (dq->dq_flags & DQ_MOD)
    779   1.4  dholland 				lfs_dq1sync(vp, dq);
    780   1.1  dholland 			mutex_exit(&dq->dq_interlock);
    781   1.1  dholland 		}
    782   1.1  dholland 		vput(vp);
    783   1.1  dholland 	}
    784   1.7   hannken 	vfs_vnode_iterator_destroy(marker);
    785   1.1  dholland 	return (0);
    786   1.1  dholland }
    787   1.1  dholland 
    788   1.1  dholland /*
    789   1.1  dholland  * Obtain a dquot structure for the specified identifier and quota file
    790   1.1  dholland  * reading the information from the file if necessary.
    791   1.1  dholland  */
    792   1.1  dholland int
    793   1.4  dholland lfs_dq1get(struct vnode *dqvp, u_long id, struct ulfsmount *ump, int type,
    794   1.1  dholland     struct dquot *dq)
    795   1.1  dholland {
    796   1.1  dholland 	struct iovec aiov;
    797   1.1  dholland 	struct uio auio;
    798   1.1  dholland 	int error;
    799   1.1  dholland 
    800   1.1  dholland 	KASSERT(mutex_owned(&dq->dq_interlock));
    801   1.1  dholland 	vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
    802   1.1  dholland 	auio.uio_iov = &aiov;
    803   1.1  dholland 	auio.uio_iovcnt = 1;
    804   1.1  dholland 	aiov.iov_base = (void *)&dq->dq_un.dq1_dqb;
    805   1.1  dholland 	aiov.iov_len = sizeof (struct dqblk);
    806   1.1  dholland 	auio.uio_resid = sizeof (struct dqblk);
    807  1.11  dholland 	auio.uio_offset = (off_t)id * sizeof (struct dqblk);
    808   1.1  dholland 	auio.uio_rw = UIO_READ;
    809   1.1  dholland 	UIO_SETUP_SYSSPACE(&auio);
    810   1.1  dholland 	error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
    811   1.1  dholland 	if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
    812   1.1  dholland 		memset((void *)&dq->dq_un.dq1_dqb, 0, sizeof(struct dqblk));
    813   1.1  dholland 	VOP_UNLOCK(dqvp);
    814   1.1  dholland 	/*
    815   1.1  dholland 	 * I/O error in reading quota file, release
    816   1.1  dholland 	 * quota structure and reflect problem to caller.
    817   1.1  dholland 	 */
    818   1.1  dholland 	if (error)
    819   1.1  dholland 		return (error);
    820   1.1  dholland 	/*
    821   1.1  dholland 	 * Check for no limit to enforce.
    822   1.1  dholland 	 * Initialize time values if necessary.
    823   1.1  dholland 	 */
    824   1.1  dholland 	if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
    825   1.1  dholland 	    dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
    826   1.1  dholland 		dq->dq_flags |= DQ_FAKE;
    827   1.1  dholland 	if (dq->dq_id != 0) {
    828   1.1  dholland 		if (dq->dq_btime == 0)
    829   1.1  dholland 			dq->dq_btime = time_second + ump->umq1_btime[type];
    830   1.1  dholland 		if (dq->dq_itime == 0)
    831   1.1  dholland 			dq->dq_itime = time_second + ump->umq1_itime[type];
    832   1.1  dholland 	}
    833   1.1  dholland 	return (0);
    834   1.1  dholland }
    835   1.1  dholland 
    836   1.1  dholland /*
    837   1.1  dholland  * Update the disk quota in the quota file.
    838   1.1  dholland  */
    839   1.1  dholland int
    840   1.4  dholland lfs_dq1sync(struct vnode *vp, struct dquot *dq)
    841   1.1  dholland {
    842   1.1  dholland 	struct vnode *dqvp;
    843   1.1  dholland 	struct iovec aiov;
    844   1.1  dholland 	struct uio auio;
    845   1.1  dholland 	int error;
    846   1.1  dholland 
    847   1.1  dholland 	if (dq == NODQUOT)
    848   1.1  dholland 		panic("dq1sync: dquot");
    849   1.1  dholland 	KASSERT(mutex_owned(&dq->dq_interlock));
    850   1.1  dholland 	if ((dq->dq_flags & DQ_MOD) == 0)
    851   1.1  dholland 		return (0);
    852   1.1  dholland 	if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
    853   1.1  dholland 		panic("dq1sync: file");
    854   1.1  dholland 	KASSERT(dqvp != vp);
    855   1.1  dholland 	vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
    856   1.1  dholland 	auio.uio_iov = &aiov;
    857   1.1  dholland 	auio.uio_iovcnt = 1;
    858   1.1  dholland 	aiov.iov_base = (void *)&dq->dq_un.dq1_dqb;
    859   1.1  dholland 	aiov.iov_len = sizeof (struct dqblk);
    860   1.1  dholland 	auio.uio_resid = sizeof (struct dqblk);
    861  1.11  dholland 	auio.uio_offset = (off_t)dq->dq_id * sizeof (struct dqblk);
    862   1.1  dholland 	auio.uio_rw = UIO_WRITE;
    863   1.1  dholland 	UIO_SETUP_SYSSPACE(&auio);
    864   1.1  dholland 	error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
    865   1.1  dholland 	if (auio.uio_resid && error == 0)
    866   1.1  dholland 		error = EIO;
    867   1.1  dholland 	dq->dq_flags &= ~DQ_MOD;
    868   1.1  dholland 	VOP_UNLOCK(dqvp);
    869   1.1  dholland 	return (error);
    870   1.1  dholland }
    871