xref: /onnv-gate/usr/src/uts/common/fs/ufs/quota.c (revision 4662:9c48274ded8b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54321Scasper  * Common Development and Distribution License (the "License").
64321Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Code pertaining to management of the in-core data structures.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/t_lock.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/systm.h>
490Sstevel@tonic-gate #include <sys/signal.h>
500Sstevel@tonic-gate #include <sys/errno.h>
510Sstevel@tonic-gate #include <sys/user.h>
520Sstevel@tonic-gate #include <sys/proc.h>
530Sstevel@tonic-gate #include <sys/vfs.h>
540Sstevel@tonic-gate #include <sys/vnode.h>
550Sstevel@tonic-gate #include <sys/uio.h>
560Sstevel@tonic-gate #include <sys/buf.h>
570Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
580Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
590Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
600Sstevel@tonic-gate #include <sys/cmn_err.h>
610Sstevel@tonic-gate #include <sys/kmem.h>
620Sstevel@tonic-gate #include <sys/debug.h>
630Sstevel@tonic-gate #include <sys/file.h>
640Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
650Sstevel@tonic-gate #include <sys/var.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Dquot in core hash chain headers
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate struct	dqhead	dqhead[NDQHASH];
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static kmutex_t dq_cachelock;
740Sstevel@tonic-gate static kmutex_t dq_freelock;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate krwlock_t dq_rwlock;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Dquot free list.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate struct dquot dqfreelist;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	dqinsheadfree(DQP) { \
840Sstevel@tonic-gate 	mutex_enter(&dq_freelock); \
850Sstevel@tonic-gate 	(DQP)->dq_freef = dqfreelist.dq_freef; \
860Sstevel@tonic-gate 	(DQP)->dq_freeb = &dqfreelist; \
870Sstevel@tonic-gate 	dqfreelist.dq_freef->dq_freeb = (DQP); \
880Sstevel@tonic-gate 	dqfreelist.dq_freef = (DQP); \
890Sstevel@tonic-gate 	mutex_exit(&dq_freelock); \
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	dqinstailfree(DQP) { \
930Sstevel@tonic-gate 	mutex_enter(&dq_freelock); \
940Sstevel@tonic-gate 	(DQP)->dq_freeb = dqfreelist.dq_freeb; \
950Sstevel@tonic-gate 	(DQP)->dq_freef = &dqfreelist; \
960Sstevel@tonic-gate 	dqfreelist.dq_freeb->dq_freef = (DQP); \
970Sstevel@tonic-gate 	dqfreelist.dq_freeb = (DQP); \
980Sstevel@tonic-gate 	mutex_exit(&dq_freelock); \
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* (clear pointers to make sure we don't use them; catch problems early) */
1020Sstevel@tonic-gate #define	dqremfree(DQP) { \
1030Sstevel@tonic-gate 	(DQP)->dq_freeb->dq_freef = (DQP)->dq_freef; \
1040Sstevel@tonic-gate 	(DQP)->dq_freef->dq_freeb = (DQP)->dq_freeb; \
1050Sstevel@tonic-gate 	(DQP)->dq_freef = (DQP)->dq_freeb = NULL; \
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate typedef	struct dquot *DQptr;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * Initialize quota sub-system init lock.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate void
qtinit()1140Sstevel@tonic-gate qtinit()
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	rw_init(&dq_rwlock, NULL, RW_DEFAULT, NULL);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * qtinit2 allocated space for the quota structures.  Only do this if
1210Sstevel@tonic-gate  * if quotas are going to be used so that we can save the space if quotas
1220Sstevel@tonic-gate  * aren't used.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate void
qtinit2(void)1250Sstevel@tonic-gate qtinit2(void)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	register struct dqhead *dhp;
1280Sstevel@tonic-gate 	register struct dquot *dqp;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dq_rwlock));
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (ndquot == 0)
1330Sstevel@tonic-gate 		ndquot = ((maxusers * NMOUNT) / 4) + v.v_proc;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	dquot = kmem_zalloc(ndquot * sizeof (struct dquot), KM_SLEEP);
1360Sstevel@tonic-gate 	dquotNDQUOT = dquot + ndquot;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * Initialize the cache between the in-core structures
1400Sstevel@tonic-gate 	 * and the per-file system quota files on disk.
1410Sstevel@tonic-gate 	 */
1420Sstevel@tonic-gate 	for (dhp = &dqhead[0]; dhp < &dqhead[NDQHASH]; dhp++) {
1430Sstevel@tonic-gate 		dhp->dqh_forw = dhp->dqh_back = (DQptr)dhp;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	dqfreelist.dq_freef = dqfreelist.dq_freeb = (DQptr)&dqfreelist;
1460Sstevel@tonic-gate 	for (dqp = dquot; dqp < dquotNDQUOT; dqp++) {
1470Sstevel@tonic-gate 		mutex_init(&dqp->dq_lock, NULL, MUTEX_DEFAULT, NULL);
1480Sstevel@tonic-gate 		dqp->dq_forw = dqp->dq_back = dqp;
1490Sstevel@tonic-gate 		dqinsheadfree(dqp);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * Obtain the user's on-disk quota limit for file system specified.
1550Sstevel@tonic-gate  * dqpp is returned locked.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate int
getdiskquota(uid_t uid,struct ufsvfs * ufsvfsp,int force,struct dquot ** dqpp)1580Sstevel@tonic-gate getdiskquota(
1590Sstevel@tonic-gate 	uid_t uid,
1600Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp,
1610Sstevel@tonic-gate 	int force,			/* don't do enable checks */
1620Sstevel@tonic-gate 	struct dquot **dqpp)		/* resulting dquot ptr */
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	struct dquot *dqp;
1650Sstevel@tonic-gate 	struct dqhead *dhp;
1660Sstevel@tonic-gate 	struct inode *qip;
1670Sstevel@tonic-gate 	int error;
1680Sstevel@tonic-gate 	extern struct cred *kcred;
1690Sstevel@tonic-gate 	daddr_t	bn;
1700Sstevel@tonic-gate 	int contig;
1710Sstevel@tonic-gate 	int err;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	dhp = &dqhead[DQHASH(uid, ufsvfsp)];
1760Sstevel@tonic-gate loop:
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Check for quotas enabled.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0 && !force)
1810Sstevel@tonic-gate 		return (ESRCH);
1820Sstevel@tonic-gate 	qip = ufsvfsp->vfs_qinod;
1830Sstevel@tonic-gate 	if (!qip)
1840Sstevel@tonic-gate 		return (ufs_fault(ufsvfsp->vfs_root, "getdiskquota: NULL qip"));
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Check the cache first.
1870Sstevel@tonic-gate 	 */
1880Sstevel@tonic-gate 	mutex_enter(&dq_cachelock);
1890Sstevel@tonic-gate 	for (dqp = dhp->dqh_forw; dqp != (DQptr)dhp; dqp = dqp->dq_forw) {
1900Sstevel@tonic-gate 		if (dqp->dq_uid != uid || dqp->dq_ufsvfsp != ufsvfsp)
1910Sstevel@tonic-gate 			continue;
1920Sstevel@tonic-gate 		mutex_exit(&dq_cachelock);
1930Sstevel@tonic-gate 		mutex_enter(&dqp->dq_lock);
1940Sstevel@tonic-gate 		/*
1950Sstevel@tonic-gate 		 * I may have slept in the mutex_enter.  Make sure this is
1960Sstevel@tonic-gate 		 * still the one I want.
1970Sstevel@tonic-gate 		 */
1980Sstevel@tonic-gate 		if (dqp->dq_uid != uid || dqp->dq_ufsvfsp != ufsvfsp) {
1990Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2000Sstevel@tonic-gate 			goto loop;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 		if (dqp->dq_flags & DQ_ERROR) {
2030Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2040Sstevel@tonic-gate 			return (EINVAL);
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		/*
2070Sstevel@tonic-gate 		 * Cache hit with no references.
2080Sstevel@tonic-gate 		 * Take the structure off the free list.
2090Sstevel@tonic-gate 		 */
2100Sstevel@tonic-gate 		if (dqp->dq_cnt == 0) {
2110Sstevel@tonic-gate 			mutex_enter(&dq_freelock);
2120Sstevel@tonic-gate 			dqremfree(dqp);
2130Sstevel@tonic-gate 			mutex_exit(&dq_freelock);
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 		dqp->dq_cnt++;
2160Sstevel@tonic-gate 		mutex_exit(&dqp->dq_lock);
2170Sstevel@tonic-gate 		*dqpp = dqp;
2180Sstevel@tonic-gate 		return (0);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	/*
2210Sstevel@tonic-gate 	 * Not in cache.
2220Sstevel@tonic-gate 	 * Get dquot at head of free list.
2230Sstevel@tonic-gate 	 */
2240Sstevel@tonic-gate 	mutex_enter(&dq_freelock);
2250Sstevel@tonic-gate 	if ((dqp = dqfreelist.dq_freef) == &dqfreelist) {
2260Sstevel@tonic-gate 		mutex_exit(&dq_freelock);
2270Sstevel@tonic-gate 		mutex_exit(&dq_cachelock);
2280Sstevel@tonic-gate 		cmn_err(CE_WARN, "dquot table full");
2290Sstevel@tonic-gate 		return (EUSERS);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (dqp->dq_cnt != 0 || dqp->dq_flags != 0) {
2330Sstevel@tonic-gate 		panic("getdiskquota: dqp->dq_cnt: "
2340Sstevel@tonic-gate 		    "%ld != 0 || dqp->dq_flags: 0x%x != 0 (%s)",
2350Sstevel@tonic-gate 		    dqp->dq_cnt, dqp->dq_flags, qip->i_fs->fs_fsmnt);
2360Sstevel@tonic-gate 		/*NOTREACHED*/
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * Take it off the free list, and off the hash chain it was on.
2400Sstevel@tonic-gate 	 * Then put it on the new hash chain.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	dqremfree(dqp);
2430Sstevel@tonic-gate 	mutex_exit(&dq_freelock);
2440Sstevel@tonic-gate 	remque(dqp);
2450Sstevel@tonic-gate 	dqp->dq_cnt = 1;
2460Sstevel@tonic-gate 	dqp->dq_uid = uid;
2470Sstevel@tonic-gate 	dqp->dq_ufsvfsp = ufsvfsp;
2480Sstevel@tonic-gate 	dqp->dq_mof = UFS_HOLE;
2490Sstevel@tonic-gate 	mutex_enter(&dqp->dq_lock);
2500Sstevel@tonic-gate 	insque(dqp, dhp);
2510Sstevel@tonic-gate 	mutex_exit(&dq_cachelock);
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Check the uid in case it's too large to fit into the 2Gbyte
2540Sstevel@tonic-gate 	 * 'quotas' file (higher than 67 million or so).
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Large Files: i_size need to be accessed atomically now.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	rw_enter(&qip->i_contents, RW_READER);
2614321Scasper 	if (uid <= MAXUID && dqoff(uid) >= 0 && dqoff(uid) < qip->i_size) {
2620Sstevel@tonic-gate 		/*
2630Sstevel@tonic-gate 		 * Read quota info off disk.
2640Sstevel@tonic-gate 		 */
2650Sstevel@tonic-gate 		error = ufs_rdwri(UIO_READ, FREAD, qip, (caddr_t)&dqp->dq_dqb,
2660Sstevel@tonic-gate 		    sizeof (struct dqblk), dqoff(uid), UIO_SYSSPACE,
2670Sstevel@tonic-gate 		    (int *)NULL, kcred);
2680Sstevel@tonic-gate 		/*
2690Sstevel@tonic-gate 		 * We must set the dq_mof even if not we are not logging in case
2700Sstevel@tonic-gate 		 * we are later remount to logging.
2710Sstevel@tonic-gate 		 */
2720Sstevel@tonic-gate 		err = bmap_read(qip, dqoff(uid), &bn, &contig);
2730Sstevel@tonic-gate 		rw_exit(&qip->i_contents);
2740Sstevel@tonic-gate 		if ((bn != UFS_HOLE) && !err) {
2750Sstevel@tonic-gate 			dqp->dq_mof = ldbtob(bn) +
276*4662Sfrankho 			    (offset_t)(dqoff(uid) & (DEV_BSIZE - 1));
2770Sstevel@tonic-gate 		} else {
2780Sstevel@tonic-gate 			dqp->dq_mof = UFS_HOLE;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 		if (error) {
2810Sstevel@tonic-gate 			/*
2820Sstevel@tonic-gate 			 * I/O error in reading quota file.
2830Sstevel@tonic-gate 			 * Put dquot on a private, unfindable hash list,
2840Sstevel@tonic-gate 			 * put dquot at the head of the free list and
2850Sstevel@tonic-gate 			 * reflect the problem to caller.
2860Sstevel@tonic-gate 			 */
2870Sstevel@tonic-gate 			dqp->dq_flags = DQ_ERROR;
2880Sstevel@tonic-gate 			/*
2890Sstevel@tonic-gate 			 * I must exit the dq_lock so that I can acquire the
2900Sstevel@tonic-gate 			 * dq_cachelock.  If another thread finds dqp before
2910Sstevel@tonic-gate 			 * I remove it from the cache it will see the
2920Sstevel@tonic-gate 			 * DQ_ERROR and just return EIO.
2930Sstevel@tonic-gate 			 */
2940Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2950Sstevel@tonic-gate 			mutex_enter(&dq_cachelock);
2960Sstevel@tonic-gate 			mutex_enter(&dqp->dq_lock);
2970Sstevel@tonic-gate 			remque(dqp);
2980Sstevel@tonic-gate 			mutex_exit(&dqp->dq_lock);
2990Sstevel@tonic-gate 			mutex_exit(&dq_cachelock);
3000Sstevel@tonic-gate 			/*
3010Sstevel@tonic-gate 			 * Don't bother reacquiring dq_lock because the dq is
3020Sstevel@tonic-gate 			 * not on the freelist or in the cache so only I have
3030Sstevel@tonic-gate 			 * access to it.
3040Sstevel@tonic-gate 			 */
3050Sstevel@tonic-gate 			dqp->dq_cnt = 0;
3060Sstevel@tonic-gate 			dqp->dq_ufsvfsp = NULL;
3070Sstevel@tonic-gate 			dqp->dq_forw = dqp;
3080Sstevel@tonic-gate 			dqp->dq_back = dqp;
3090Sstevel@tonic-gate 			dqp->dq_mof = UFS_HOLE;
3100Sstevel@tonic-gate 			dqp->dq_flags = 0;
3110Sstevel@tonic-gate 			dqinsheadfree(dqp);
3120Sstevel@tonic-gate 			return (EIO);
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 	} else {
3150Sstevel@tonic-gate 		rw_exit(&qip->i_contents);	/* done with i_size */
3160Sstevel@tonic-gate 		bzero(&dqp->dq_dqb, sizeof (struct dqblk));
3170Sstevel@tonic-gate 		dqp->dq_mof = UFS_HOLE;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 	mutex_exit(&dqp->dq_lock);
3200Sstevel@tonic-gate 	*dqpp = dqp;
3210Sstevel@tonic-gate 	return (0);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Release dquot.
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate void
dqput(dqp)3280Sstevel@tonic-gate dqput(dqp)
3290Sstevel@tonic-gate 	register struct dquot *dqp;
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	ASSERT(dqp->dq_ufsvfsp == NULL ||
3330Sstevel@tonic-gate 		RW_LOCK_HELD(&dqp->dq_ufsvfsp->vfs_dqrwlock));
3340Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dqp->dq_lock));
3350Sstevel@tonic-gate 	if (dqp->dq_cnt == 0) {
3360Sstevel@tonic-gate 		(void) ufs_fault(
3370Sstevel@tonic-gate 			dqp->dq_ufsvfsp && dqp->dq_ufsvfsp->vfs_root?
3380Sstevel@tonic-gate 			dqp->dq_ufsvfsp->vfs_root: NULL,
3390Sstevel@tonic-gate 						    "dqput: dqp->dq_cnt == 0");
3400Sstevel@tonic-gate 		return;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	if (--dqp->dq_cnt == 0) {
3430Sstevel@tonic-gate 		if (dqp->dq_flags & DQ_MOD)
3440Sstevel@tonic-gate 			dqupdate(dqp);
3450Sstevel@tonic-gate 		/*
3460Sstevel@tonic-gate 		 * DQ_MOD was cleared by dqupdate().
3470Sstevel@tonic-gate 		 * DQ_ERROR shouldn't be set if this dquot was being used.
3480Sstevel@tonic-gate 		 * DQ_FILES/DQ_BLKS don't matter at this point.
3490Sstevel@tonic-gate 		 */
3500Sstevel@tonic-gate 		dqp->dq_flags = 0;
3510Sstevel@tonic-gate 		if (dqp->dq_ufsvfsp == NULL ||
3520Sstevel@tonic-gate 		    dqp->dq_ufsvfsp->vfs_qflags == 0) {
3530Sstevel@tonic-gate 			/* quotas are disabled, discard this dquot struct */
3540Sstevel@tonic-gate 			dqinval(dqp);
3550Sstevel@tonic-gate 		} else
3560Sstevel@tonic-gate 			dqinstailfree(dqp);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * Update on disk quota info.
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate void
dqupdate(dqp)3640Sstevel@tonic-gate dqupdate(dqp)
3650Sstevel@tonic-gate 	register struct dquot *dqp;
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	register struct inode *qip;
3680Sstevel@tonic-gate 	extern struct cred *kcred;
3690Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
3700Sstevel@tonic-gate 	int		newtrans	= 0;
3710Sstevel@tonic-gate 	struct vnode	*vfs_root;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dqp->dq_lock));
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (!dqp->dq_ufsvfsp) {
3760Sstevel@tonic-gate 		(void) ufs_fault(NULL, "dqupdate: NULL dq_ufsvfsp");
3770Sstevel@tonic-gate 		return;
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	vfs_root = dqp->dq_ufsvfsp->vfs_root;
3800Sstevel@tonic-gate 	if (!vfs_root) {
3810Sstevel@tonic-gate 		(void) ufs_fault(NULL, "dqupdate: NULL vfs_root");
3820Sstevel@tonic-gate 		return;
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	/*
3850Sstevel@tonic-gate 	 * I don't need to hold dq_rwlock when looking at vfs_qinod here
3860Sstevel@tonic-gate 	 * because vfs_qinod is only cleared by closedq after it has called
3870Sstevel@tonic-gate 	 * dqput on all dq's.  Since I am holding dq_lock on this dq, closedq
3880Sstevel@tonic-gate 	 * will have to wait until I am done before it can call dqput on
3890Sstevel@tonic-gate 	 * this dq so vfs_qinod will not change value until after I return.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	qip = dqp->dq_ufsvfsp->vfs_qinod;
3920Sstevel@tonic-gate 	if (!qip) {
3930Sstevel@tonic-gate 		(void) ufs_fault(vfs_root, "dqupdate: NULL vfs_qinod");
3940Sstevel@tonic-gate 		return;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 	ufsvfsp = qip->i_ufsvfs;
3970Sstevel@tonic-gate 	if (!ufsvfsp) {
3980Sstevel@tonic-gate 		(void) ufs_fault(vfs_root,
3990Sstevel@tonic-gate 				    "dqupdate: NULL vfs_qinod->i_ufsvfs");
4000Sstevel@tonic-gate 		return;
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 	if (ufsvfsp != dqp->dq_ufsvfsp) {
4030Sstevel@tonic-gate 		(void) ufs_fault(vfs_root,
4040Sstevel@tonic-gate 			    "dqupdate: vfs_qinod->i_ufsvfs != dqp->dq_ufsvfsp");
4050Sstevel@tonic-gate 		return;
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	if (!(dqp->dq_flags & DQ_MOD)) {
4080Sstevel@tonic-gate 		(void) ufs_fault(vfs_root,
4090Sstevel@tonic-gate 				    "dqupdate: !(dqp->dq_flags & DQ_MOD)");
4100Sstevel@tonic-gate 		return;
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (!(curthread->t_flag & T_DONTBLOCK)) {
4140Sstevel@tonic-gate 		newtrans++;
4150Sstevel@tonic-gate 		curthread->t_flag |= T_DONTBLOCK;
4160Sstevel@tonic-gate 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA, TOP_QUOTA_SIZE);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
4190Sstevel@tonic-gate 		TRANS_DELTA(ufsvfsp, dqp->dq_mof, sizeof (struct dqblk),
4200Sstevel@tonic-gate 		    DT_QR, 0, 0);
4210Sstevel@tonic-gate 		TRANS_LOG(ufsvfsp, (caddr_t)&dqp->dq_dqb, dqp->dq_mof,
4220Sstevel@tonic-gate 		    (int)(sizeof (struct dqblk)), NULL, 0);
4230Sstevel@tonic-gate 	} else {
4240Sstevel@tonic-gate 		/*
4250Sstevel@tonic-gate 		 * Locknest gets very confused when I lock the quota inode.
4260Sstevel@tonic-gate 		 * It thinks that qip and ip (the inode that caused the
4270Sstevel@tonic-gate 		 * quota routines to get called) are the same inode.
4280Sstevel@tonic-gate 		 */
4290Sstevel@tonic-gate 		rw_enter(&qip->i_contents, RW_WRITER);
4300Sstevel@tonic-gate 		/*
4310Sstevel@tonic-gate 		 * refuse to push if offset would be illegal
4320Sstevel@tonic-gate 		 */
4330Sstevel@tonic-gate 		if (dqoff(dqp->dq_uid) >= 0) {
4340Sstevel@tonic-gate 			(void) ufs_rdwri(UIO_WRITE, FWRITE, qip,
4350Sstevel@tonic-gate 					(caddr_t)&dqp->dq_dqb,
4360Sstevel@tonic-gate 					sizeof (struct dqblk),
4370Sstevel@tonic-gate 					dqoff(dqp->dq_uid), UIO_SYSSPACE,
4380Sstevel@tonic-gate 					(int *)NULL, kcred);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		rw_exit(&qip->i_contents);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	dqp->dq_flags &= ~DQ_MOD;
4440Sstevel@tonic-gate 	if (newtrans) {
4450Sstevel@tonic-gate 		TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA, TOP_QUOTA_SIZE);
4460Sstevel@tonic-gate 		curthread->t_flag &= ~T_DONTBLOCK;
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * Invalidate a dquot.  This function is called when quotas are disabled
4520Sstevel@tonic-gate  * for a specific file system via closedq() or when we unmount the file
4530Sstevel@tonic-gate  * system and invalidate the quota cache via invalidatedq().
4540Sstevel@tonic-gate  *
4550Sstevel@tonic-gate  * Take the dquot off its hash list and put it on a private, unfindable
4560Sstevel@tonic-gate  * hash list (refers to itself). Also, put it at the head of the free list.
4570Sstevel@tonic-gate  * Note that even though dq_cnt is zero, this dquot is NOT yet on the
4580Sstevel@tonic-gate  * freelist.
4590Sstevel@tonic-gate  */
4600Sstevel@tonic-gate void
dqinval(dqp)4610Sstevel@tonic-gate dqinval(dqp)
4620Sstevel@tonic-gate 	register struct dquot *dqp;
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dqp->dq_lock));
4650Sstevel@tonic-gate 	ASSERT(dqp->dq_cnt == 0);
4660Sstevel@tonic-gate 	ASSERT(dqp->dq_flags == 0);
4670Sstevel@tonic-gate 	ASSERT(dqp->dq_freef == NULL && dqp->dq_freeb == NULL);
4680Sstevel@tonic-gate 	ASSERT(dqp->dq_ufsvfsp &&
4690Sstevel@tonic-gate 		(dqp->dq_ufsvfsp->vfs_qflags & MQ_ENABLED) == 0);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/*
4720Sstevel@tonic-gate 	 * To preserve lock order, we have to drop dq_lock in order to
4730Sstevel@tonic-gate 	 * grab dq_cachelock.  To prevent someone from grabbing this
4740Sstevel@tonic-gate 	 * dquot from the quota cache via getdiskquota() while we are
4750Sstevel@tonic-gate 	 * "unsafe", we clear dq_ufsvfsp so it won't match anything.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	dqp->dq_ufsvfsp = NULL;
4780Sstevel@tonic-gate 	mutex_exit(&dqp->dq_lock);
4790Sstevel@tonic-gate 	mutex_enter(&dq_cachelock);
4800Sstevel@tonic-gate 	mutex_enter(&dqp->dq_lock);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * The following paranoia is to make sure that getdiskquota()
4840Sstevel@tonic-gate 	 * has not been broken:
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	ASSERT(dqp->dq_cnt == 0);
4870Sstevel@tonic-gate 	ASSERT(dqp->dq_flags == 0);
4880Sstevel@tonic-gate 	ASSERT(dqp->dq_freef == NULL && dqp->dq_freeb == NULL);
4890Sstevel@tonic-gate 	ASSERT(dqp->dq_ufsvfsp == NULL);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Now we have the locks in the right order so we can do the
4930Sstevel@tonic-gate 	 * rest of the work.
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	remque(dqp);
4960Sstevel@tonic-gate 	mutex_exit(&dq_cachelock);
4970Sstevel@tonic-gate 	dqp->dq_forw = dqp;
4980Sstevel@tonic-gate 	dqp->dq_back = dqp;
4990Sstevel@tonic-gate 	dqinsheadfree(dqp);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate  * Invalidate all quota information records for the specified file system.
5040Sstevel@tonic-gate  */
5050Sstevel@tonic-gate void
invalidatedq(ufsvfsp)5060Sstevel@tonic-gate invalidatedq(ufsvfsp)
5070Sstevel@tonic-gate 	register struct ufsvfs *ufsvfsp;
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	register struct dquot *dqp;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * If quotas are not initialized, then there is nothing to do.
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	rw_enter(&dq_rwlock, RW_READER);
5160Sstevel@tonic-gate 	if (!quotas_initialized) {
5170Sstevel@tonic-gate 		rw_exit(&dq_rwlock);
5180Sstevel@tonic-gate 		return;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	rw_exit(&dq_rwlock);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_WRITER);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	ASSERT((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * Invalidate all the quota info records for this file system
5290Sstevel@tonic-gate 	 * that are in the quota cache:
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 	for (dqp = dquot; dqp < dquotNDQUOT; dqp++) {
5320Sstevel@tonic-gate 		/*
5330Sstevel@tonic-gate 		 * If someone else has it, then ignore it. For the target
5340Sstevel@tonic-gate 		 * file system, this is okay for three reasons:
5350Sstevel@tonic-gate 		 *
5360Sstevel@tonic-gate 		 * 1) This routine is called after closedq() so the quota
5370Sstevel@tonic-gate 		 *    sub-system is disabled for this file system.
5380Sstevel@tonic-gate 		 * 2) We have made the quota sub-system quiescent for
5390Sstevel@tonic-gate 		 *    this file system.
5400Sstevel@tonic-gate 		 * 3) We are in the process of unmounting this file
5410Sstevel@tonic-gate 		 *    system so the quota sub-system can't be enabled
5420Sstevel@tonic-gate 		 *    for it.
5430Sstevel@tonic-gate 		 */
5440Sstevel@tonic-gate 		if (!mutex_tryenter(&dqp->dq_lock)) {
5450Sstevel@tonic-gate 			continue;
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		/*
5500Sstevel@tonic-gate 		 * At this point, any quota info records that are
5510Sstevel@tonic-gate 		 * associated with the target file system, should have a
5520Sstevel@tonic-gate 		 * reference count of zero and be on the free list.
5530Sstevel@tonic-gate 		 * Why? Because these quota info records went to a zero
5540Sstevel@tonic-gate 		 * dq_cnt (via dqput()) before the file system was
5550Sstevel@tonic-gate 		 * unmounted and are waiting to be found in the quota
5560Sstevel@tonic-gate 		 * cache and reused (via getdiskquota()). The exception
5570Sstevel@tonic-gate 		 * is when a quota transaction is sitting in the deltamap,
5580Sstevel@tonic-gate 		 * indicated by DQ_TRANS being set in dq_flags.
5590Sstevel@tonic-gate 		 * This causes a reference to be held on the quota
5600Sstevel@tonic-gate 		 * information record and it will only be cleared once
5610Sstevel@tonic-gate 		 * the transaction has reached the log. If we find
5620Sstevel@tonic-gate 		 * any of these - we ignore them and let logging do
5630Sstevel@tonic-gate 		 * the right thing.
5640Sstevel@tonic-gate 		 */
5650Sstevel@tonic-gate 		if (dqp->dq_ufsvfsp == ufsvfsp) {
5660Sstevel@tonic-gate 			ASSERT(dqp->dq_cnt == 0 || (dqp->dq_cnt == 1 &&
5670Sstevel@tonic-gate 			    (dqp->dq_flags & DQ_TRANS)));
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 			/* Cope with those orphaned dquots. */
5700Sstevel@tonic-gate 			if (dqp->dq_cnt == 1 && (dqp->dq_flags & DQ_TRANS)) {
5710Sstevel@tonic-gate 				mutex_exit(&dqp->dq_lock);
5720Sstevel@tonic-gate 				continue;
5730Sstevel@tonic-gate 			}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 			ASSERT(dqp->dq_cnt == 0);
5760Sstevel@tonic-gate 			ASSERT(dqp->dq_freef && dqp->dq_freeb);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 			/*
5790Sstevel@tonic-gate 			 * Take the quota info record off the free list
5800Sstevel@tonic-gate 			 * so dqinval() can do its job (and put it on the
5810Sstevel@tonic-gate 			 * front of the free list).
5820Sstevel@tonic-gate 			 */
5830Sstevel@tonic-gate 			mutex_enter(&dq_freelock);
5840Sstevel@tonic-gate 			dqremfree(dqp);
5850Sstevel@tonic-gate 			mutex_exit(&dq_freelock);
5860Sstevel@tonic-gate 			dqinval(dqp);
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		mutex_exit(&dqp->dq_lock);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
5920Sstevel@tonic-gate }
593