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