xref: /illumos-gate/usr/src/uts/common/fs/ufs/quota.c (revision 2d6eb4a5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Code pertaining to management of the in-core data structures.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/signal.h>
477c478bd9Sstevel@tonic-gate #include <sys/errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/user.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
517c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
527c478bd9Sstevel@tonic-gate #include <sys/uio.h>
537c478bd9Sstevel@tonic-gate #include <sys/buf.h>
547c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
557c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
587c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
597c478bd9Sstevel@tonic-gate #include <sys/debug.h>
607c478bd9Sstevel@tonic-gate #include <sys/file.h>
617c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
627c478bd9Sstevel@tonic-gate #include <sys/var.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Dquot in core hash chain headers
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate struct	dqhead	dqhead[NDQHASH];
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static kmutex_t dq_cachelock;
717c478bd9Sstevel@tonic-gate static kmutex_t dq_freelock;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate krwlock_t dq_rwlock;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Dquot free list.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate struct dquot dqfreelist;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	dqinsheadfree(DQP) { \
817c478bd9Sstevel@tonic-gate 	mutex_enter(&dq_freelock); \
827c478bd9Sstevel@tonic-gate 	(DQP)->dq_freef = dqfreelist.dq_freef; \
837c478bd9Sstevel@tonic-gate 	(DQP)->dq_freeb = &dqfreelist; \
847c478bd9Sstevel@tonic-gate 	dqfreelist.dq_freef->dq_freeb = (DQP); \
857c478bd9Sstevel@tonic-gate 	dqfreelist.dq_freef = (DQP); \
867c478bd9Sstevel@tonic-gate 	mutex_exit(&dq_freelock); \
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	dqinstailfree(DQP) { \
907c478bd9Sstevel@tonic-gate 	mutex_enter(&dq_freelock); \
917c478bd9Sstevel@tonic-gate 	(DQP)->dq_freeb = dqfreelist.dq_freeb; \
927c478bd9Sstevel@tonic-gate 	(DQP)->dq_freef = &dqfreelist; \
937c478bd9Sstevel@tonic-gate 	dqfreelist.dq_freeb->dq_freef = (DQP); \
947c478bd9Sstevel@tonic-gate 	dqfreelist.dq_freeb = (DQP); \
957c478bd9Sstevel@tonic-gate 	mutex_exit(&dq_freelock); \
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* (clear pointers to make sure we don't use them; catch problems early) */
997c478bd9Sstevel@tonic-gate #define	dqremfree(DQP) { \
1007c478bd9Sstevel@tonic-gate 	(DQP)->dq_freeb->dq_freef = (DQP)->dq_freef; \
1017c478bd9Sstevel@tonic-gate 	(DQP)->dq_freef->dq_freeb = (DQP)->dq_freeb; \
1027c478bd9Sstevel@tonic-gate 	(DQP)->dq_freef = (DQP)->dq_freeb = NULL; \
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate typedef	struct dquot *DQptr;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Initialize quota sub-system init lock.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate void
qtinit()1117c478bd9Sstevel@tonic-gate qtinit()
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	rw_init(&dq_rwlock, NULL, RW_DEFAULT, NULL);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * qtinit2 allocated space for the quota structures.  Only do this if
1187c478bd9Sstevel@tonic-gate  * if quotas are going to be used so that we can save the space if quotas
1197c478bd9Sstevel@tonic-gate  * aren't used.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate void
qtinit2(void)1227c478bd9Sstevel@tonic-gate qtinit2(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	register struct dqhead *dhp;
1257c478bd9Sstevel@tonic-gate 	register struct dquot *dqp;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dq_rwlock));
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (ndquot == 0)
1307c478bd9Sstevel@tonic-gate 		ndquot = ((maxusers * NMOUNT) / 4) + v.v_proc;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	dquot = kmem_zalloc(ndquot * sizeof (struct dquot), KM_SLEEP);
1337c478bd9Sstevel@tonic-gate 	dquotNDQUOT = dquot + ndquot;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * Initialize the cache between the in-core structures
1377c478bd9Sstevel@tonic-gate 	 * and the per-file system quota files on disk.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	for (dhp = &dqhead[0]; dhp < &dqhead[NDQHASH]; dhp++) {
1407c478bd9Sstevel@tonic-gate 		dhp->dqh_forw = dhp->dqh_back = (DQptr)dhp;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	dqfreelist.dq_freef = dqfreelist.dq_freeb = (DQptr)&dqfreelist;
1437c478bd9Sstevel@tonic-gate 	for (dqp = dquot; dqp < dquotNDQUOT; dqp++) {
1447c478bd9Sstevel@tonic-gate 		mutex_init(&dqp->dq_lock, NULL, MUTEX_DEFAULT, NULL);
1457c478bd9Sstevel@tonic-gate 		dqp->dq_forw = dqp->dq_back = dqp;
1467c478bd9Sstevel@tonic-gate 		dqinsheadfree(dqp);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Obtain the user's on-disk quota limit for file system specified.
1527c478bd9Sstevel@tonic-gate  * dqpp is returned locked.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate int
getdiskquota(uid_t uid,struct ufsvfs * ufsvfsp,int force,struct dquot ** dqpp)1557c478bd9Sstevel@tonic-gate getdiskquota(
1567c478bd9Sstevel@tonic-gate 	uid_t uid,
1577c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp,
1587c478bd9Sstevel@tonic-gate 	int force,			/* don't do enable checks */
1597c478bd9Sstevel@tonic-gate 	struct dquot **dqpp)		/* resulting dquot ptr */
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct dquot *dqp;
1627c478bd9Sstevel@tonic-gate 	struct dqhead *dhp;
1637c478bd9Sstevel@tonic-gate 	struct inode *qip;
1647c478bd9Sstevel@tonic-gate 	int error;
1657c478bd9Sstevel@tonic-gate 	extern struct cred *kcred;
1667c478bd9Sstevel@tonic-gate 	daddr_t	bn;
1677c478bd9Sstevel@tonic-gate 	int contig;
1687c478bd9Sstevel@tonic-gate 	int err;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	dhp = &dqhead[DQHASH(uid, ufsvfsp)];
1737c478bd9Sstevel@tonic-gate loop:
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Check for quotas enabled.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0 && !force)
1787c478bd9Sstevel@tonic-gate 		return (ESRCH);
1797c478bd9Sstevel@tonic-gate 	qip = ufsvfsp->vfs_qinod;
1807c478bd9Sstevel@tonic-gate 	if (!qip)
1817c478bd9Sstevel@tonic-gate 		return (ufs_fault(ufsvfsp->vfs_root, "getdiskquota: NULL qip"));
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Check the cache first.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	mutex_enter(&dq_cachelock);
1867c478bd9Sstevel@tonic-gate 	for (dqp = dhp->dqh_forw; dqp != (DQptr)dhp; dqp = dqp->dq_forw) {
1877c478bd9Sstevel@tonic-gate 		if (dqp->dq_uid != uid || dqp->dq_ufsvfsp != ufsvfsp)
1887c478bd9Sstevel@tonic-gate 			continue;
1897c478bd9Sstevel@tonic-gate 		mutex_exit(&dq_cachelock);
1907c478bd9Sstevel@tonic-gate 		mutex_enter(&dqp->dq_lock);
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * I may have slept in the mutex_enter.  Make sure this is
1937c478bd9Sstevel@tonic-gate 		 * still the one I want.
1947c478bd9Sstevel@tonic-gate 		 */
1957c478bd9Sstevel@tonic-gate 		if (dqp->dq_uid != uid || dqp->dq_ufsvfsp != ufsvfsp) {
1967c478bd9Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
1977c478bd9Sstevel@tonic-gate 			goto loop;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		if (dqp->dq_flags & DQ_ERROR) {
2007c478bd9Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2017c478bd9Sstevel@tonic-gate 			return (EINVAL);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		/*
2047c478bd9Sstevel@tonic-gate 		 * Cache hit with no references.
2057c478bd9Sstevel@tonic-gate 		 * Take the structure off the free list.
2067c478bd9Sstevel@tonic-gate 		 */
2077c478bd9Sstevel@tonic-gate 		if (dqp->dq_cnt == 0) {
2087c478bd9Sstevel@tonic-gate 			mutex_enter(&dq_freelock);
2097c478bd9Sstevel@tonic-gate 			dqremfree(dqp);
2107c478bd9Sstevel@tonic-gate 			mutex_exit(&dq_freelock);
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 		dqp->dq_cnt++;
2137c478bd9Sstevel@tonic-gate 		mutex_exit(&dqp->dq_lock);
2147c478bd9Sstevel@tonic-gate 		*dqpp = dqp;
2157c478bd9Sstevel@tonic-gate 		return (0);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * Not in cache.
2197c478bd9Sstevel@tonic-gate 	 * Get dquot at head of free list.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	mutex_enter(&dq_freelock);
2227c478bd9Sstevel@tonic-gate 	if ((dqp = dqfreelist.dq_freef) == &dqfreelist) {
2237c478bd9Sstevel@tonic-gate 		mutex_exit(&dq_freelock);
2247c478bd9Sstevel@tonic-gate 		mutex_exit(&dq_cachelock);
2257c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "dquot table full");
2267c478bd9Sstevel@tonic-gate 		return (EUSERS);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (dqp->dq_cnt != 0 || dqp->dq_flags != 0) {
2307c478bd9Sstevel@tonic-gate 		panic("getdiskquota: dqp->dq_cnt: "
2317c478bd9Sstevel@tonic-gate 		    "%ld != 0 || dqp->dq_flags: 0x%x != 0 (%s)",
2327c478bd9Sstevel@tonic-gate 		    dqp->dq_cnt, dqp->dq_flags, qip->i_fs->fs_fsmnt);
2337c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Take it off the free list, and off the hash chain it was on.
2377c478bd9Sstevel@tonic-gate 	 * Then put it on the new hash chain.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	dqremfree(dqp);
2407c478bd9Sstevel@tonic-gate 	mutex_exit(&dq_freelock);
2417c478bd9Sstevel@tonic-gate 	remque(dqp);
2427c478bd9Sstevel@tonic-gate 	dqp->dq_cnt = 1;
2437c478bd9Sstevel@tonic-gate 	dqp->dq_uid = uid;
2447c478bd9Sstevel@tonic-gate 	dqp->dq_ufsvfsp = ufsvfsp;
2457c478bd9Sstevel@tonic-gate 	dqp->dq_mof = UFS_HOLE;
2467c478bd9Sstevel@tonic-gate 	mutex_enter(&dqp->dq_lock);
2477c478bd9Sstevel@tonic-gate 	insque(dqp, dhp);
2487c478bd9Sstevel@tonic-gate 	mutex_exit(&dq_cachelock);
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Check the uid in case it's too large to fit into the 2Gbyte
2517c478bd9Sstevel@tonic-gate 	 * 'quotas' file (higher than 67 million or so).
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Large Files: i_size need to be accessed atomically now.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	rw_enter(&qip->i_contents, RW_READER);
258f48205beScasper 	if (uid <= MAXUID && dqoff(uid) >= 0 && dqoff(uid) < qip->i_size) {
2597c478bd9Sstevel@tonic-gate 		/*
2607c478bd9Sstevel@tonic-gate 		 * Read quota info off disk.
2617c478bd9Sstevel@tonic-gate 		 */
2627c478bd9Sstevel@tonic-gate 		error = ufs_rdwri(UIO_READ, FREAD, qip, (caddr_t)&dqp->dq_dqb,
2637c478bd9Sstevel@tonic-gate 		    sizeof (struct dqblk), dqoff(uid), UIO_SYSSPACE,
2647c478bd9Sstevel@tonic-gate 		    (int *)NULL, kcred);
2657c478bd9Sstevel@tonic-gate 		/*
2667c478bd9Sstevel@tonic-gate 		 * We must set the dq_mof even if not we are not logging in case
2677c478bd9Sstevel@tonic-gate 		 * we are later remount to logging.
2687c478bd9Sstevel@tonic-gate 		 */
2697c478bd9Sstevel@tonic-gate 		err = bmap_read(qip, dqoff(uid), &bn, &contig);
2707c478bd9Sstevel@tonic-gate 		rw_exit(&qip->i_contents);
2717c478bd9Sstevel@tonic-gate 		if ((bn != UFS_HOLE) && !err) {
2727c478bd9Sstevel@tonic-gate 			dqp->dq_mof = ldbtob(bn) +
273*80d34432Sfrankho 			    (offset_t)(dqoff(uid) & (DEV_BSIZE - 1));
2747c478bd9Sstevel@tonic-gate 		} else {
2757c478bd9Sstevel@tonic-gate 			dqp->dq_mof = UFS_HOLE;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		if (error) {
2787c478bd9Sstevel@tonic-gate 			/*
2797c478bd9Sstevel@tonic-gate 			 * I/O error in reading quota file.
2807c478bd9Sstevel@tonic-gate 			 * Put dquot on a private, unfindable hash list,
2817c478bd9Sstevel@tonic-gate 			 * put dquot at the head of the free list and
2827c478bd9Sstevel@tonic-gate 			 * reflect the problem to caller.
2837c478bd9Sstevel@tonic-gate 			 */
2847c478bd9Sstevel@tonic-gate 			dqp->dq_flags = DQ_ERROR;
2857c478bd9Sstevel@tonic-gate 			/*
2867c478bd9Sstevel@tonic-gate 			 * I must exit the dq_lock so that I can acquire the
2877c478bd9Sstevel@tonic-gate 			 * dq_cachelock.  If another thread finds dqp before
2887c478bd9Sstevel@tonic-gate 			 * I remove it from the cache it will see the
2897c478bd9Sstevel@tonic-gate 			 * DQ_ERROR and just return EIO.
2907c478bd9Sstevel@tonic-gate 			 */
2917c478bd9Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2927c478bd9Sstevel@tonic-gate 			mutex_enter(&dq_cachelock);
2937c478bd9Sstevel@tonic-gate 			mutex_enter(&dqp->dq_lock);
2947c478bd9Sstevel@tonic-gate 			remque(dqp);
2957c478bd9Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2967c478bd9Sstevel@tonic-gate 			mutex_exit(&dq_cachelock);
2977c478bd9Sstevel@tonic-gate 			/*
2987c478bd9Sstevel@tonic-gate 			 * Don't bother reacquiring dq_lock because the dq is
2997c478bd9Sstevel@tonic-gate 			 * not on the freelist or in the cache so only I have
3007c478bd9Sstevel@tonic-gate 			 * access to it.
3017c478bd9Sstevel@tonic-gate 			 */
3027c478bd9Sstevel@tonic-gate 			dqp->dq_cnt = 0;
3037c478bd9Sstevel@tonic-gate 			dqp->dq_ufsvfsp = NULL;
3047c478bd9Sstevel@tonic-gate 			dqp->dq_forw = dqp;
3057c478bd9Sstevel@tonic-gate 			dqp->dq_back = dqp;
3067c478bd9Sstevel@tonic-gate 			dqp->dq_mof = UFS_HOLE;
3077c478bd9Sstevel@tonic-gate 			dqp->dq_flags = 0;
3087c478bd9Sstevel@tonic-gate 			dqinsheadfree(dqp);
3097c478bd9Sstevel@tonic-gate 			return (EIO);
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	} else {
3127c478bd9Sstevel@tonic-gate 		rw_exit(&qip->i_contents);	/* done with i_size */
3137c478bd9Sstevel@tonic-gate 		bzero(&dqp->dq_dqb, sizeof (struct dqblk));
3147c478bd9Sstevel@tonic-gate 		dqp->dq_mof = UFS_HOLE;
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	mutex_exit(&dqp->dq_lock);
3177c478bd9Sstevel@tonic-gate 	*dqpp = dqp;
3187c478bd9Sstevel@tonic-gate 	return (0);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Release dquot.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate void
dqput(dqp)3257c478bd9Sstevel@tonic-gate dqput(dqp)
3267c478bd9Sstevel@tonic-gate 	register struct dquot *dqp;
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_ufsvfsp == NULL ||
3307c478bd9Sstevel@tonic-gate 		RW_LOCK_HELD(&dqp->dq_ufsvfsp->vfs_dqrwlock));
3317c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dqp->dq_lock));
3327c478bd9Sstevel@tonic-gate 	if (dqp->dq_cnt == 0) {
3337c478bd9Sstevel@tonic-gate 		(void) ufs_fault(
3347c478bd9Sstevel@tonic-gate 			dqp->dq_ufsvfsp && dqp->dq_ufsvfsp->vfs_root?
3357c478bd9Sstevel@tonic-gate 			dqp->dq_ufsvfsp->vfs_root: NULL,
3367c478bd9Sstevel@tonic-gate 						    "dqput: dqp->dq_cnt == 0");
3377c478bd9Sstevel@tonic-gate 		return;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	if (--dqp->dq_cnt == 0) {
3407c478bd9Sstevel@tonic-gate 		if (dqp->dq_flags & DQ_MOD)
3417c478bd9Sstevel@tonic-gate 			dqupdate(dqp);
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * DQ_MOD was cleared by dqupdate().
3447c478bd9Sstevel@tonic-gate 		 * DQ_ERROR shouldn't be set if this dquot was being used.
3457c478bd9Sstevel@tonic-gate 		 * DQ_FILES/DQ_BLKS don't matter at this point.
3467c478bd9Sstevel@tonic-gate 		 */
3477c478bd9Sstevel@tonic-gate 		dqp->dq_flags = 0;
3487c478bd9Sstevel@tonic-gate 		if (dqp->dq_ufsvfsp == NULL ||
3497c478bd9Sstevel@tonic-gate 		    dqp->dq_ufsvfsp->vfs_qflags == 0) {
3507c478bd9Sstevel@tonic-gate 			/* quotas are disabled, discard this dquot struct */
3517c478bd9Sstevel@tonic-gate 			dqinval(dqp);
3527c478bd9Sstevel@tonic-gate 		} else
3537c478bd9Sstevel@tonic-gate 			dqinstailfree(dqp);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * Update on disk quota info.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate void
dqupdate(dqp)3617c478bd9Sstevel@tonic-gate dqupdate(dqp)
3627c478bd9Sstevel@tonic-gate 	register struct dquot *dqp;
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	register struct inode *qip;
3657c478bd9Sstevel@tonic-gate 	extern struct cred *kcred;
3667c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
3677c478bd9Sstevel@tonic-gate 	int		newtrans	= 0;
3687c478bd9Sstevel@tonic-gate 	struct vnode	*vfs_root;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dqp->dq_lock));
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (!dqp->dq_ufsvfsp) {
3737c478bd9Sstevel@tonic-gate 		(void) ufs_fault(NULL, "dqupdate: NULL dq_ufsvfsp");
3747c478bd9Sstevel@tonic-gate 		return;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	vfs_root = dqp->dq_ufsvfsp->vfs_root;
3777c478bd9Sstevel@tonic-gate 	if (!vfs_root) {
3787c478bd9Sstevel@tonic-gate 		(void) ufs_fault(NULL, "dqupdate: NULL vfs_root");
3797c478bd9Sstevel@tonic-gate 		return;
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * I don't need to hold dq_rwlock when looking at vfs_qinod here
3837c478bd9Sstevel@tonic-gate 	 * because vfs_qinod is only cleared by closedq after it has called
3847c478bd9Sstevel@tonic-gate 	 * dqput on all dq's.  Since I am holding dq_lock on this dq, closedq
3857c478bd9Sstevel@tonic-gate 	 * will have to wait until I am done before it can call dqput on
3867c478bd9Sstevel@tonic-gate 	 * this dq so vfs_qinod will not change value until after I return.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	qip = dqp->dq_ufsvfsp->vfs_qinod;
3897c478bd9Sstevel@tonic-gate 	if (!qip) {
3907c478bd9Sstevel@tonic-gate 		(void) ufs_fault(vfs_root, "dqupdate: NULL vfs_qinod");
3917c478bd9Sstevel@tonic-gate 		return;
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	ufsvfsp = qip->i_ufsvfs;
3947c478bd9Sstevel@tonic-gate 	if (!ufsvfsp) {
3957c478bd9Sstevel@tonic-gate 		(void) ufs_fault(vfs_root,
3967c478bd9Sstevel@tonic-gate 				    "dqupdate: NULL vfs_qinod->i_ufsvfs");
3977c478bd9Sstevel@tonic-gate 		return;
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 	if (ufsvfsp != dqp->dq_ufsvfsp) {
4007c478bd9Sstevel@tonic-gate 		(void) ufs_fault(vfs_root,
4017c478bd9Sstevel@tonic-gate 			    "dqupdate: vfs_qinod->i_ufsvfs != dqp->dq_ufsvfsp");
4027c478bd9Sstevel@tonic-gate 		return;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	if (!(dqp->dq_flags & DQ_MOD)) {
4057c478bd9Sstevel@tonic-gate 		(void) ufs_fault(vfs_root,
4067c478bd9Sstevel@tonic-gate 				    "dqupdate: !(dqp->dq_flags & DQ_MOD)");
4077c478bd9Sstevel@tonic-gate 		return;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (!(curthread->t_flag & T_DONTBLOCK)) {
4117c478bd9Sstevel@tonic-gate 		newtrans++;
4127c478bd9Sstevel@tonic-gate 		curthread->t_flag |= T_DONTBLOCK;
4137c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA, TOP_QUOTA_SIZE);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
4167c478bd9Sstevel@tonic-gate 		TRANS_DELTA(ufsvfsp, dqp->dq_mof, sizeof (struct dqblk),
4177c478bd9Sstevel@tonic-gate 		    DT_QR, 0, 0);
4187c478bd9Sstevel@tonic-gate 		TRANS_LOG(ufsvfsp, (caddr_t)&dqp->dq_dqb, dqp->dq_mof,
4197c478bd9Sstevel@tonic-gate 		    (int)(sizeof (struct dqblk)), NULL, 0);
4207c478bd9Sstevel@tonic-gate 	} else {
4217c478bd9Sstevel@tonic-gate 		/*
4227c478bd9Sstevel@tonic-gate 		 * Locknest gets very confused when I lock the quota inode.
4237c478bd9Sstevel@tonic-gate 		 * It thinks that qip and ip (the inode that caused the
4247c478bd9Sstevel@tonic-gate 		 * quota routines to get called) are the same inode.
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		rw_enter(&qip->i_contents, RW_WRITER);
4277c478bd9Sstevel@tonic-gate 		/*
4287c478bd9Sstevel@tonic-gate 		 * refuse to push if offset would be illegal
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if (dqoff(dqp->dq_uid) >= 0) {
4317c478bd9Sstevel@tonic-gate 			(void) ufs_rdwri(UIO_WRITE, FWRITE, qip,
4327c478bd9Sstevel@tonic-gate 					(caddr_t)&dqp->dq_dqb,
4337c478bd9Sstevel@tonic-gate 					sizeof (struct dqblk),
4347c478bd9Sstevel@tonic-gate 					dqoff(dqp->dq_uid), UIO_SYSSPACE,
4357c478bd9Sstevel@tonic-gate 					(int *)NULL, kcred);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		rw_exit(&qip->i_contents);
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	dqp->dq_flags &= ~DQ_MOD;
4417c478bd9Sstevel@tonic-gate 	if (newtrans) {
4427c478bd9Sstevel@tonic-gate 		TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA, TOP_QUOTA_SIZE);
4437c478bd9Sstevel@tonic-gate 		curthread->t_flag &= ~T_DONTBLOCK;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate  * Invalidate a dquot.  This function is called when quotas are disabled
4497c478bd9Sstevel@tonic-gate  * for a specific file system via closedq() or when we unmount the file
4507c478bd9Sstevel@tonic-gate  * system and invalidate the quota cache via invalidatedq().
4517c478bd9Sstevel@tonic-gate  *
4527c478bd9Sstevel@tonic-gate  * Take the dquot off its hash list and put it on a private, unfindable
4537c478bd9Sstevel@tonic-gate  * hash list (refers to itself). Also, put it at the head of the free list.
4547c478bd9Sstevel@tonic-gate  * Note that even though dq_cnt is zero, this dquot is NOT yet on the
4557c478bd9Sstevel@tonic-gate  * freelist.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate void
dqinval(dqp)4587c478bd9Sstevel@tonic-gate dqinval(dqp)
4597c478bd9Sstevel@tonic-gate 	register struct dquot *dqp;
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dqp->dq_lock));
4627c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_cnt == 0);
4637c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_flags == 0);
4647c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_freef == NULL && dqp->dq_freeb == NULL);
4657c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_ufsvfsp &&
4667c478bd9Sstevel@tonic-gate 		(dqp->dq_ufsvfsp->vfs_qflags & MQ_ENABLED) == 0);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * To preserve lock order, we have to drop dq_lock in order to
4707c478bd9Sstevel@tonic-gate 	 * grab dq_cachelock.  To prevent someone from grabbing this
4717c478bd9Sstevel@tonic-gate 	 * dquot from the quota cache via getdiskquota() while we are
4727c478bd9Sstevel@tonic-gate 	 * "unsafe", we clear dq_ufsvfsp so it won't match anything.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	dqp->dq_ufsvfsp = NULL;
4757c478bd9Sstevel@tonic-gate 	mutex_exit(&dqp->dq_lock);
4767c478bd9Sstevel@tonic-gate 	mutex_enter(&dq_cachelock);
4777c478bd9Sstevel@tonic-gate 	mutex_enter(&dqp->dq_lock);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * The following paranoia is to make sure that getdiskquota()
4817c478bd9Sstevel@tonic-gate 	 * has not been broken:
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_cnt == 0);
4847c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_flags == 0);
4857c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_freef == NULL && dqp->dq_freeb == NULL);
4867c478bd9Sstevel@tonic-gate 	ASSERT(dqp->dq_ufsvfsp == NULL);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Now we have the locks in the right order so we can do the
4907c478bd9Sstevel@tonic-gate 	 * rest of the work.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	remque(dqp);
4937c478bd9Sstevel@tonic-gate 	mutex_exit(&dq_cachelock);
4947c478bd9Sstevel@tonic-gate 	dqp->dq_forw = dqp;
4957c478bd9Sstevel@tonic-gate 	dqp->dq_back = dqp;
4967c478bd9Sstevel@tonic-gate 	dqinsheadfree(dqp);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * Invalidate all quota information records for the specified file system.
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate void
invalidatedq(ufsvfsp)5037c478bd9Sstevel@tonic-gate invalidatedq(ufsvfsp)
5047c478bd9Sstevel@tonic-gate 	register struct ufsvfs *ufsvfsp;
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	register struct dquot *dqp;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * If quotas are not initialized, then there is nothing to do.
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 	rw_enter(&dq_rwlock, RW_READER);
5137c478bd9Sstevel@tonic-gate 	if (!quotas_initialized) {
5147c478bd9Sstevel@tonic-gate 		rw_exit(&dq_rwlock);
5157c478bd9Sstevel@tonic-gate 		return;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	rw_exit(&dq_rwlock);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_WRITER);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	ASSERT((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * Invalidate all the quota info records for this file system
5267c478bd9Sstevel@tonic-gate 	 * that are in the quota cache:
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	for (dqp = dquot; dqp < dquotNDQUOT; dqp++) {
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * If someone else has it, then ignore it. For the target
5317c478bd9Sstevel@tonic-gate 		 * file system, this is okay for three reasons:
5327c478bd9Sstevel@tonic-gate 		 *
5337c478bd9Sstevel@tonic-gate 		 * 1) This routine is called after closedq() so the quota
5347c478bd9Sstevel@tonic-gate 		 *    sub-system is disabled for this file system.
5357c478bd9Sstevel@tonic-gate 		 * 2) We have made the quota sub-system quiescent for
5367c478bd9Sstevel@tonic-gate 		 *    this file system.
5377c478bd9Sstevel@tonic-gate 		 * 3) We are in the process of unmounting this file
5387c478bd9Sstevel@tonic-gate 		 *    system so the quota sub-system can't be enabled
5397c478bd9Sstevel@tonic-gate 		 *    for it.
5407c478bd9Sstevel@tonic-gate 		 */
5417c478bd9Sstevel@tonic-gate 		if (!mutex_tryenter(&dqp->dq_lock)) {
5427c478bd9Sstevel@tonic-gate 			continue;
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		/*
5477c478bd9Sstevel@tonic-gate 		 * At this point, any quota info records that are
5487c478bd9Sstevel@tonic-gate 		 * associated with the target file system, should have a
5497c478bd9Sstevel@tonic-gate 		 * reference count of zero and be on the free list.
5507c478bd9Sstevel@tonic-gate 		 * Why? Because these quota info records went to a zero
5517c478bd9Sstevel@tonic-gate 		 * dq_cnt (via dqput()) before the file system was
5527c478bd9Sstevel@tonic-gate 		 * unmounted and are waiting to be found in the quota
5537c478bd9Sstevel@tonic-gate 		 * cache and reused (via getdiskquota()). The exception
5547c478bd9Sstevel@tonic-gate 		 * is when a quota transaction is sitting in the deltamap,
5557c478bd9Sstevel@tonic-gate 		 * indicated by DQ_TRANS being set in dq_flags.
5567c478bd9Sstevel@tonic-gate 		 * This causes a reference to be held on the quota
5577c478bd9Sstevel@tonic-gate 		 * information record and it will only be cleared once
5587c478bd9Sstevel@tonic-gate 		 * the transaction has reached the log. If we find
5597c478bd9Sstevel@tonic-gate 		 * any of these - we ignore them and let logging do
5607c478bd9Sstevel@tonic-gate 		 * the right thing.
5617c478bd9Sstevel@tonic-gate 		 */
5627c478bd9Sstevel@tonic-gate 		if (dqp->dq_ufsvfsp == ufsvfsp) {
5637c478bd9Sstevel@tonic-gate 			ASSERT(dqp->dq_cnt == 0 || (dqp->dq_cnt == 1 &&
5647c478bd9Sstevel@tonic-gate 			    (dqp->dq_flags & DQ_TRANS)));
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			/* Cope with those orphaned dquots. */
5677c478bd9Sstevel@tonic-gate 			if (dqp->dq_cnt == 1 && (dqp->dq_flags & DQ_TRANS)) {
5687c478bd9Sstevel@tonic-gate 				mutex_exit(&dqp->dq_lock);
5697c478bd9Sstevel@tonic-gate 				continue;
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 			ASSERT(dqp->dq_cnt == 0);
5737c478bd9Sstevel@tonic-gate 			ASSERT(dqp->dq_freef && dqp->dq_freeb);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 			/*
5767c478bd9Sstevel@tonic-gate 			 * Take the quota info record off the free list
5777c478bd9Sstevel@tonic-gate 			 * so dqinval() can do its job (and put it on the
5787c478bd9Sstevel@tonic-gate 			 * front of the free list).
5797c478bd9Sstevel@tonic-gate 			 */
5807c478bd9Sstevel@tonic-gate 			mutex_enter(&dq_freelock);
5817c478bd9Sstevel@tonic-gate 			dqremfree(dqp);
5827c478bd9Sstevel@tonic-gate 			mutex_exit(&dq_freelock);
5837c478bd9Sstevel@tonic-gate 			dqinval(dqp);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		mutex_exit(&dqp->dq_lock);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
5897c478bd9Sstevel@tonic-gate }
590