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
54662Sfrankho * Common Development and Distribution License (the "License").
64662Sfrankho * 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 /*
224662Sfrankho * 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 * Quota system calls.
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/time.h>
490Sstevel@tonic-gate #include <sys/systm.h>
500Sstevel@tonic-gate #include <sys/signal.h>
510Sstevel@tonic-gate #include <sys/cred.h>
520Sstevel@tonic-gate #include <sys/proc.h>
530Sstevel@tonic-gate #include <sys/user.h>
540Sstevel@tonic-gate #include <sys/proc.h>
550Sstevel@tonic-gate #include <sys/vfs.h>
560Sstevel@tonic-gate #include <sys/vnode.h>
570Sstevel@tonic-gate #include <sys/uio.h>
580Sstevel@tonic-gate #include <sys/buf.h>
590Sstevel@tonic-gate #include <sys/file.h>
600Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
610Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
620Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
630Sstevel@tonic-gate #include <sys/errno.h>
640Sstevel@tonic-gate #include <sys/debug.h>
650Sstevel@tonic-gate #include <sys/cmn_err.h>
660Sstevel@tonic-gate #include <sys/pathname.h>
670Sstevel@tonic-gate #include <sys/mntent.h>
680Sstevel@tonic-gate #include <sys/policy.h>
690Sstevel@tonic-gate
700Sstevel@tonic-gate static int opendq();
710Sstevel@tonic-gate static int setquota();
720Sstevel@tonic-gate static int getquota();
730Sstevel@tonic-gate static int quotasync();
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Quota sub-system init flag.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate int quotas_initialized = 0;
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Sys call to allow users to find out
820Sstevel@tonic-gate * their current position wrt quota's
830Sstevel@tonic-gate * and to allow privileged users to alter it.
840Sstevel@tonic-gate */
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*ARGSUSED*/
870Sstevel@tonic-gate int
quotactl(struct vnode * vp,intptr_t arg,int flag,struct cred * cr)880Sstevel@tonic-gate quotactl(struct vnode *vp, intptr_t arg, int flag, struct cred *cr)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate struct quotctl quot;
910Sstevel@tonic-gate struct ufsvfs *ufsvfsp;
920Sstevel@tonic-gate int error = 0;
930Sstevel@tonic-gate
940Sstevel@tonic-gate if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
950Sstevel@tonic-gate if (copyin((caddr_t)arg, ", sizeof (struct quotctl)))
960Sstevel@tonic-gate return (EFAULT);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
990Sstevel@tonic-gate else {
1000Sstevel@tonic-gate /* quotctl struct from ILP32 callers */
1010Sstevel@tonic-gate struct quotctl32 quot32;
1020Sstevel@tonic-gate if (copyin((caddr_t)arg, "32, sizeof (struct quotctl32)))
1030Sstevel@tonic-gate return (EFAULT);
1040Sstevel@tonic-gate quot.op = quot32.op;
1050Sstevel@tonic-gate quot.uid = quot32.uid;
1060Sstevel@tonic-gate quot.addr = (caddr_t)(uintptr_t)quot32.addr;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (quot.uid < 0)
1110Sstevel@tonic-gate quot.uid = crgetruid(cr);
1120Sstevel@tonic-gate if (quot.op == Q_SYNC && vp == NULL) {
1130Sstevel@tonic-gate ufsvfsp = NULL;
1140Sstevel@tonic-gate } else if (quot.op != Q_ALLSYNC) {
1150Sstevel@tonic-gate ufsvfsp = (struct ufsvfs *)(vp->v_vfsp->vfs_data);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate switch (quot.op) {
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate case Q_QUOTAON:
1200Sstevel@tonic-gate rw_enter(&dq_rwlock, RW_WRITER);
1210Sstevel@tonic-gate if (quotas_initialized == 0) {
1220Sstevel@tonic-gate qtinit2();
1230Sstevel@tonic-gate quotas_initialized = 1;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate rw_exit(&dq_rwlock);
1260Sstevel@tonic-gate error = opendq(ufsvfsp, vp, cr);
1270Sstevel@tonic-gate break;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate case Q_QUOTAOFF:
1300Sstevel@tonic-gate error = closedq(ufsvfsp, cr);
1310Sstevel@tonic-gate if (!error) {
1320Sstevel@tonic-gate invalidatedq(ufsvfsp);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate break;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate case Q_SETQUOTA:
1370Sstevel@tonic-gate case Q_SETQLIM:
1380Sstevel@tonic-gate error = setquota(quot.op, (uid_t)quot.uid, ufsvfsp,
1390Sstevel@tonic-gate quot.addr, cr);
1400Sstevel@tonic-gate break;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate case Q_GETQUOTA:
1430Sstevel@tonic-gate error = getquota((uid_t)quot.uid, ufsvfsp, (caddr_t)quot.addr,
1440Sstevel@tonic-gate cr);
1450Sstevel@tonic-gate break;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate case Q_SYNC:
1480Sstevel@tonic-gate error = qsync(ufsvfsp);
1490Sstevel@tonic-gate break;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate case Q_ALLSYNC:
1520Sstevel@tonic-gate (void) qsync(NULL);
1530Sstevel@tonic-gate break;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate default:
1560Sstevel@tonic-gate error = EINVAL;
1570Sstevel@tonic-gate break;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate return (error);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate static int
opendq_scan_inode(struct inode * ip,void * arg)1630Sstevel@tonic-gate opendq_scan_inode(struct inode *ip, void *arg)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * wrong file system or this is the quota inode; keep looking
1690Sstevel@tonic-gate */
1700Sstevel@tonic-gate if (ufsvfsp != (struct ufsvfs *)arg || ip == ip->i_ufsvfs->vfs_qinod) {
1710Sstevel@tonic-gate return (0);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock));
1750Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_WRITER);
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * This inode is in the cache (by definition), is still valid,
1780Sstevel@tonic-gate * and is not a shadow inode or extended attribute directory inode,
1790Sstevel@tonic-gate * but does not have a quota so get the quota information.
1800Sstevel@tonic-gate */
1810Sstevel@tonic-gate if (ip->i_mode && (ip->i_mode & IFMT) != IFSHAD &&
1820Sstevel@tonic-gate (ip->i_mode & IFMT) != IFATTRDIR && ip->i_dquot == NULL) {
1830Sstevel@tonic-gate ip->i_dquot = getinoquota(ip);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate rw_exit(&ip->i_contents);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * Set the quota file up for a particular file system.
1920Sstevel@tonic-gate * Called as the result of a quotaon (Q_QUOTAON) ioctl.
1930Sstevel@tonic-gate */
1940Sstevel@tonic-gate static int
opendq(struct ufsvfs * ufsvfsp,struct vnode * vp,struct cred * cr)1950Sstevel@tonic-gate opendq(
1960Sstevel@tonic-gate struct ufsvfs *ufsvfsp,
1970Sstevel@tonic-gate struct vnode *vp, /* quota file */
1980Sstevel@tonic-gate struct cred *cr)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate struct inode *qip;
2010Sstevel@tonic-gate struct dquot *dqp;
2020Sstevel@tonic-gate int error;
2030Sstevel@tonic-gate int quotaon = 0;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (secpolicy_fs_quota(cr, ufsvfsp->vfs_vfs) != 0)
2060Sstevel@tonic-gate return (EPERM);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate VN_HOLD(vp);
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate * Check to be sure its a regular file.
2120Sstevel@tonic-gate */
2130Sstevel@tonic-gate if (vp->v_type != VREG) {
2140Sstevel@tonic-gate VN_RELE(vp);
2150Sstevel@tonic-gate return (EACCES);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate rw_enter(&ufsvfsp->vfs_dqrwlock, RW_WRITER);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * We have vfs_dqrwlock as writer, so if quotas are disabled,
2220Sstevel@tonic-gate * then vfs_qinod should be NULL or we have a race somewhere.
2230Sstevel@tonic-gate */
2240Sstevel@tonic-gate ASSERT((ufsvfsp->vfs_qflags & MQ_ENABLED) || (ufsvfsp->vfs_qinod == 0));
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if ((ufsvfsp->vfs_qflags & MQ_ENABLED) != 0) {
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate * Quotas are already enabled on this file system.
2290Sstevel@tonic-gate *
2300Sstevel@tonic-gate * If the "quotas" file was replaced (different inode)
2310Sstevel@tonic-gate * while quotas were enabled we don't want to re-enable
2320Sstevel@tonic-gate * them with a new "quotas" file. Simply print a warning
2330Sstevel@tonic-gate * message to the console, release the new vnode, and
2340Sstevel@tonic-gate * return.
2350Sstevel@tonic-gate * XXX - The right way to fix this is to return EBUSY
2360Sstevel@tonic-gate * for the ioctl() issued by 'quotaon'.
2370Sstevel@tonic-gate */
2380Sstevel@tonic-gate if (VTOI(vp) != ufsvfsp->vfs_qinod) {
2390Sstevel@tonic-gate cmn_err(CE_WARN, "Previous quota file still in use."
2404662Sfrankho " Disable quotas on %s before enabling.\n",
2414662Sfrankho VTOI(vp)->i_fs->fs_fsmnt);
2420Sstevel@tonic-gate VN_RELE(vp);
2430Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
2440Sstevel@tonic-gate return (0);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate (void) quotasync(ufsvfsp, /* do_lock */ 0);
2470Sstevel@tonic-gate /* remove extra hold on quota file */
2480Sstevel@tonic-gate VN_RELE(vp);
2490Sstevel@tonic-gate quotaon++;
2500Sstevel@tonic-gate qip = ufsvfsp->vfs_qinod;
2510Sstevel@tonic-gate } else {
2520Sstevel@tonic-gate int qlen;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate ufsvfsp->vfs_qinod = VTOI(vp);
2550Sstevel@tonic-gate qip = ufsvfsp->vfs_qinod;
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate * Force the file to have no partially allocated blocks
2580Sstevel@tonic-gate * to prevent a realloc from changing the location of
2590Sstevel@tonic-gate * the data. We must do this even if not logging in
2600Sstevel@tonic-gate * case we later remount to logging.
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate qlen = qip->i_fs->fs_bsize * NDADDR;
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate * Largefiles: i_size needs to be atomically accessed now.
2660Sstevel@tonic-gate */
2670Sstevel@tonic-gate rw_enter(&qip->i_contents, RW_WRITER);
2680Sstevel@tonic-gate if (qip->i_size < qlen) {
2690Sstevel@tonic-gate if (ufs_itrunc(qip, (u_offset_t)qlen, (int)0, cr) != 0)
2700Sstevel@tonic-gate cmn_err(CE_WARN, "opendq failed to remove frags"
2710Sstevel@tonic-gate " from quota file\n");
2720Sstevel@tonic-gate rw_exit(&qip->i_contents);
2730Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)qip->i_size,
274*5331Samw B_INVAL, kcred, NULL);
2750Sstevel@tonic-gate } else {
2760Sstevel@tonic-gate rw_exit(&qip->i_contents);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate TRANS_MATA_IGET(ufsvfsp, qip);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * The file system time limits are in the dquot for uid 0.
2830Sstevel@tonic-gate * The time limits set the relative time the other users
2840Sstevel@tonic-gate * can be over quota for this file system.
2850Sstevel@tonic-gate * If it is zero a default is used (see quota.h).
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate error = getdiskquota((uid_t)0, ufsvfsp, 1, &dqp);
2880Sstevel@tonic-gate if (error == 0) {
2890Sstevel@tonic-gate mutex_enter(&dqp->dq_lock);
2900Sstevel@tonic-gate ufsvfsp->vfs_btimelimit =
2910Sstevel@tonic-gate (dqp->dq_btimelimit? dqp->dq_btimelimit: DQ_BTIMELIMIT);
2920Sstevel@tonic-gate ufsvfsp->vfs_ftimelimit =
2930Sstevel@tonic-gate (dqp->dq_ftimelimit? dqp->dq_ftimelimit: DQ_FTIMELIMIT);
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate ufsvfsp->vfs_qflags = MQ_ENABLED; /* enable quotas */
2960Sstevel@tonic-gate vfs_setmntopt(ufsvfsp->vfs_vfs, MNTOPT_QUOTA, NULL, 0);
2970Sstevel@tonic-gate dqput(dqp);
2980Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
2990Sstevel@tonic-gate } else if (!quotaon) {
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * Some sort of I/O error on the quota file, and quotas were
3020Sstevel@tonic-gate * not already on when we got here so clean up.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate ufsvfsp->vfs_qflags = 0;
3050Sstevel@tonic-gate ufsvfsp->vfs_qinod = NULL;
3060Sstevel@tonic-gate VN_RELE(ITOV(qip));
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * If quotas are enabled update all valid inodes in the
3110Sstevel@tonic-gate * cache with quota information.
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate if (ufsvfsp->vfs_qflags & MQ_ENABLED) {
3140Sstevel@tonic-gate (void) ufs_scan_inodes(0, opendq_scan_inode, ufsvfsp, ufsvfsp);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
3180Sstevel@tonic-gate return (error);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate static int
closedq_scan_inode(struct inode * ip,void * arg)3220Sstevel@tonic-gate closedq_scan_inode(struct inode *ip, void *arg)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate struct dquot *dqp;
3250Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate * wrong file system; keep looking
3290Sstevel@tonic-gate */
3300Sstevel@tonic-gate if (ufsvfsp != (struct ufsvfs *)arg)
3310Sstevel@tonic-gate return (0);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock));
3340Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_WRITER);
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate * Shadow inodes and extended attribute directories
3380Sstevel@tonic-gate * do not have quota info records.
3390Sstevel@tonic-gate */
3400Sstevel@tonic-gate if ((dqp = ip->i_dquot) != NULL) {
3410Sstevel@tonic-gate ASSERT((ip->i_mode & IFMT) != IFSHAD);
3420Sstevel@tonic-gate ASSERT((ip->i_mode & IFMT) != IFATTRDIR);
3430Sstevel@tonic-gate ip->i_dquot = NULL;
3440Sstevel@tonic-gate mutex_enter(&dqp->dq_lock);
3450Sstevel@tonic-gate dqput(dqp);
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate * If we have a pending logging file system quota
3490Sstevel@tonic-gate * transaction, then cancel it. Clear the flag to
3500Sstevel@tonic-gate * prevent ufs_trans_push_quota() from trying to
3510Sstevel@tonic-gate * deal with this transaction just in case it is
3520Sstevel@tonic-gate * waiting for the mutex. We decrement the counter
3530Sstevel@tonic-gate * since the transaction won't be needing the quota
3540Sstevel@tonic-gate * info record anymore.
3550Sstevel@tonic-gate */
3560Sstevel@tonic-gate if (dqp->dq_flags & DQ_TRANS) {
3570Sstevel@tonic-gate dqp->dq_flags &= ~DQ_TRANS;
3580Sstevel@tonic-gate dqput(dqp);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate rw_exit(&ip->i_contents);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate return (0);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate * Close off disk quotas for a file system.
3690Sstevel@tonic-gate */
3700Sstevel@tonic-gate int
closedq(struct ufsvfs * ufsvfsp,struct cred * cr)3710Sstevel@tonic-gate closedq(struct ufsvfs *ufsvfsp, struct cred *cr)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate struct inode *qip;
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate if (secpolicy_fs_quota(cr, ufsvfsp->vfs_vfs) != 0)
3760Sstevel@tonic-gate return (EPERM);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate rw_enter(&ufsvfsp->vfs_dqrwlock, RW_WRITER);
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate * Quotas are not enabled on this file system so there is
3820Sstevel@tonic-gate * nothing more to do.
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
3850Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
3860Sstevel@tonic-gate return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * At this point, the quota subsystem is quiescent on this file
3910Sstevel@tonic-gate * system so we can do all the work necessary to dismantle the
3920Sstevel@tonic-gate * quota stuff.
3930Sstevel@tonic-gate */
3940Sstevel@tonic-gate qip = ufsvfsp->vfs_qinod;
3950Sstevel@tonic-gate if (!qip)
3960Sstevel@tonic-gate return (ufs_fault(ufsvfsp->vfs_root, "closedq: NULL qip"));
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate ufsvfsp->vfs_qflags = 0; /* disable quotas */
3990Sstevel@tonic-gate vfs_setmntopt(ufsvfsp->vfs_vfs, MNTOPT_NOQUOTA, NULL, 0);
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate * ufs_scan_inodes() depends on vfs_qinod, so we can't
4030Sstevel@tonic-gate * clear it until afterwards.
4040Sstevel@tonic-gate */
4050Sstevel@tonic-gate (void) ufs_scan_inodes(0, closedq_scan_inode, ufsvfsp, ufsvfsp);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate ufsvfsp->vfs_qinod = NULL;
4080Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate * Sync and release the quota file inode. Since we have a private
4120Sstevel@tonic-gate * pointer to the quota inode and vfs_qinod is now clear we do not
4130Sstevel@tonic-gate * need to hold vfs_dqrwlock.
4140Sstevel@tonic-gate */
4150Sstevel@tonic-gate (void) TRANS_SYNCIP(qip, 0, I_SYNC, TOP_SYNCIP_CLOSEDQ);
4160Sstevel@tonic-gate VN_RELE(ITOV(qip));
4170Sstevel@tonic-gate return (0);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate * Private data between setquota() and setquota_scan_inode().
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate struct setquota_data {
4240Sstevel@tonic-gate #define SQD_TYPE_NONE 0
4250Sstevel@tonic-gate #define SQD_TYPE_LIMIT 1
4260Sstevel@tonic-gate #define SQD_TYPE_NO_LIMIT 2
4270Sstevel@tonic-gate int sqd_type;
4280Sstevel@tonic-gate struct ufsvfs *sqd_ufsvfsp;
4290Sstevel@tonic-gate uid_t sqd_uid;
4300Sstevel@tonic-gate };
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate static int
setquota_scan_inode(struct inode * ip,void * arg)4330Sstevel@tonic-gate setquota_scan_inode(struct inode *ip, void *arg)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate struct setquota_data *sqdp = (struct setquota_data *)arg;
4360Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate * wrong file system; keep looking
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate if (ufsvfsp != sqdp->sqd_ufsvfsp)
4420Sstevel@tonic-gate return (0);
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock));
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate * The file system does not have quotas enabled or this is the
4480Sstevel@tonic-gate * file system's quota inode; keep looking.
4490Sstevel@tonic-gate */
4500Sstevel@tonic-gate if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0 ||
4510Sstevel@tonic-gate ip == ufsvfsp->vfs_qinod) {
4520Sstevel@tonic-gate return (0);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_WRITER);
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate * This inode is in the cache (by definition), is still valid,
4580Sstevel@tonic-gate * is not a shadow inode or extended attribute directory inode
4590Sstevel@tonic-gate * and has the right uid.
4600Sstevel@tonic-gate */
4610Sstevel@tonic-gate if (ip->i_mode && (ip->i_mode & IFMT) != IFSHAD &&
4620Sstevel@tonic-gate (ip->i_mode & IFMT) != IFATTRDIR && ip->i_uid == sqdp->sqd_uid) {
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * Transition is "no limit" to "at least one limit":
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate if (sqdp->sqd_type == SQD_TYPE_LIMIT &&
4670Sstevel@tonic-gate ip->i_dquot == NULL) {
4680Sstevel@tonic-gate ip->i_dquot = getinoquota(ip);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate * Transition is "at least one limit" to "no limit":
4720Sstevel@tonic-gate */
4730Sstevel@tonic-gate else if (sqdp->sqd_type == SQD_TYPE_NO_LIMIT && ip->i_dquot) {
4740Sstevel@tonic-gate mutex_enter(&ip->i_dquot->dq_lock);
4750Sstevel@tonic-gate dqput(ip->i_dquot);
4760Sstevel@tonic-gate mutex_exit(&ip->i_dquot->dq_lock);
4770Sstevel@tonic-gate ip->i_dquot = NULL;
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate rw_exit(&ip->i_contents);
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate return (0);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * Set various fields of the dqblk according to the command.
4870Sstevel@tonic-gate * Q_SETQUOTA - assign an entire dqblk structure.
4880Sstevel@tonic-gate * Q_SETQLIM - assign a dqblk structure except for the usage.
4890Sstevel@tonic-gate */
4900Sstevel@tonic-gate static int
setquota(int cmd,uid_t uid,struct ufsvfs * ufsvfsp,caddr_t addr,struct cred * cr)4910Sstevel@tonic-gate setquota(int cmd, uid_t uid, struct ufsvfs *ufsvfsp,
4920Sstevel@tonic-gate caddr_t addr, struct cred *cr)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate struct dquot *dqp;
4950Sstevel@tonic-gate struct inode *qip;
4960Sstevel@tonic-gate struct dquot *xdqp;
4970Sstevel@tonic-gate struct dqblk newlim;
4980Sstevel@tonic-gate int error;
4990Sstevel@tonic-gate int scan_type = SQD_TYPE_NONE;
5000Sstevel@tonic-gate daddr_t bn;
5010Sstevel@tonic-gate int contig;
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate if (secpolicy_fs_quota(cr, ufsvfsp->vfs_vfs) != 0)
5040Sstevel@tonic-gate return (EPERM);
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate rw_enter(&ufsvfsp->vfs_dqrwlock, RW_WRITER);
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate * Quotas are not enabled on this file system so there is
5100Sstevel@tonic-gate * nothing more to do.
5110Sstevel@tonic-gate */
5120Sstevel@tonic-gate if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
5130Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
5140Sstevel@tonic-gate return (ESRCH);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate * At this point, the quota subsystem is quiescent on this file
5190Sstevel@tonic-gate * system so we can do all the work necessary to modify the quota
5200Sstevel@tonic-gate * information for this user.
5210Sstevel@tonic-gate */
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate if (copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk)) != 0) {
5240Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
5250Sstevel@tonic-gate return (EFAULT);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate error = getdiskquota(uid, ufsvfsp, 0, &xdqp);
5280Sstevel@tonic-gate if (error) {
5290Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
5300Sstevel@tonic-gate return (error);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate dqp = xdqp;
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate * Don't change disk usage on Q_SETQLIM
5350Sstevel@tonic-gate */
5360Sstevel@tonic-gate mutex_enter(&dqp->dq_lock);
5370Sstevel@tonic-gate if (cmd == Q_SETQLIM) {
5380Sstevel@tonic-gate newlim.dqb_curblocks = dqp->dq_curblocks;
5390Sstevel@tonic-gate newlim.dqb_curfiles = dqp->dq_curfiles;
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate if (uid == 0) {
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate * Timelimits for uid 0 set the relative time
5440Sstevel@tonic-gate * the other users can be over quota for this file system.
5450Sstevel@tonic-gate * If it is zero a default is used (see quota.h).
5460Sstevel@tonic-gate */
5470Sstevel@tonic-gate ufsvfsp->vfs_btimelimit =
5480Sstevel@tonic-gate newlim.dqb_btimelimit? newlim.dqb_btimelimit: DQ_BTIMELIMIT;
5490Sstevel@tonic-gate ufsvfsp->vfs_ftimelimit =
5500Sstevel@tonic-gate newlim.dqb_ftimelimit? newlim.dqb_ftimelimit: DQ_FTIMELIMIT;
5510Sstevel@tonic-gate } else {
5520Sstevel@tonic-gate if (newlim.dqb_bsoftlimit &&
5530Sstevel@tonic-gate newlim.dqb_curblocks >= newlim.dqb_bsoftlimit) {
5540Sstevel@tonic-gate if (dqp->dq_bsoftlimit == 0 ||
5550Sstevel@tonic-gate dqp->dq_curblocks < dqp->dq_bsoftlimit) {
5560Sstevel@tonic-gate /* If we're suddenly over the limit(s), */
5570Sstevel@tonic-gate /* start the timer(s) */
5580Sstevel@tonic-gate newlim.dqb_btimelimit =
5590Sstevel@tonic-gate (uint32_t)gethrestime_sec() +
5600Sstevel@tonic-gate ufsvfsp->vfs_btimelimit;
5610Sstevel@tonic-gate dqp->dq_flags &= ~DQ_BLKS;
5620Sstevel@tonic-gate } else {
5630Sstevel@tonic-gate /* If we're currently over the soft */
5640Sstevel@tonic-gate /* limit and were previously over the */
5650Sstevel@tonic-gate /* soft limit then preserve the old */
5660Sstevel@tonic-gate /* time limit but make sure the DQ_BLKS */
5670Sstevel@tonic-gate /* flag is set since we must have been */
5680Sstevel@tonic-gate /* previously warned. */
5690Sstevel@tonic-gate newlim.dqb_btimelimit = dqp->dq_btimelimit;
5700Sstevel@tonic-gate dqp->dq_flags |= DQ_BLKS;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate } else {
5730Sstevel@tonic-gate /* Either no quota or under quota, clear time limit */
5740Sstevel@tonic-gate newlim.dqb_btimelimit = 0;
5750Sstevel@tonic-gate dqp->dq_flags &= ~DQ_BLKS;
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate if (newlim.dqb_fsoftlimit &&
5790Sstevel@tonic-gate newlim.dqb_curfiles >= newlim.dqb_fsoftlimit) {
5800Sstevel@tonic-gate if (dqp->dq_fsoftlimit == 0 ||
5810Sstevel@tonic-gate dqp->dq_curfiles < dqp->dq_fsoftlimit) {
5820Sstevel@tonic-gate /* If we're suddenly over the limit(s), */
5830Sstevel@tonic-gate /* start the timer(s) */
5840Sstevel@tonic-gate newlim.dqb_ftimelimit =
5850Sstevel@tonic-gate (uint32_t)gethrestime_sec() +
5860Sstevel@tonic-gate ufsvfsp->vfs_ftimelimit;
5870Sstevel@tonic-gate dqp->dq_flags &= ~DQ_FILES;
5880Sstevel@tonic-gate } else {
5890Sstevel@tonic-gate /* If we're currently over the soft */
5900Sstevel@tonic-gate /* limit and were previously over the */
5910Sstevel@tonic-gate /* soft limit then preserve the old */
5920Sstevel@tonic-gate /* time limit but make sure the */
5930Sstevel@tonic-gate /* DQ_FILES flag is set since we must */
5940Sstevel@tonic-gate /* have been previously warned. */
5950Sstevel@tonic-gate newlim.dqb_ftimelimit = dqp->dq_ftimelimit;
5960Sstevel@tonic-gate dqp->dq_flags |= DQ_FILES;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate } else {
5990Sstevel@tonic-gate /* Either no quota or under quota, clear time limit */
6000Sstevel@tonic-gate newlim.dqb_ftimelimit = 0;
6010Sstevel@tonic-gate dqp->dq_flags &= ~DQ_FILES;
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate * If there was previously no limit and there is now at least
6070Sstevel@tonic-gate * one limit, then any inodes in the cache have NULL d_iquot
6080Sstevel@tonic-gate * fields (getinoquota() returns NULL when there are no limits).
6090Sstevel@tonic-gate */
6100Sstevel@tonic-gate if ((dqp->dq_fhardlimit == 0 && dqp->dq_fsoftlimit == 0 &&
6110Sstevel@tonic-gate dqp->dq_bhardlimit == 0 && dqp->dq_bsoftlimit == 0) &&
6120Sstevel@tonic-gate (newlim.dqb_fhardlimit || newlim.dqb_fsoftlimit ||
6130Sstevel@tonic-gate newlim.dqb_bhardlimit || newlim.dqb_bsoftlimit)) {
6140Sstevel@tonic-gate scan_type = SQD_TYPE_LIMIT;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate * If there was previously at least one limit and there is now
6190Sstevel@tonic-gate * no limit, then any inodes in the cache have non-NULL d_iquot
6200Sstevel@tonic-gate * fields need to be reset to NULL.
6210Sstevel@tonic-gate */
6220Sstevel@tonic-gate else if ((dqp->dq_fhardlimit || dqp->dq_fsoftlimit ||
6230Sstevel@tonic-gate dqp->dq_bhardlimit || dqp->dq_bsoftlimit) &&
6240Sstevel@tonic-gate (newlim.dqb_fhardlimit == 0 && newlim.dqb_fsoftlimit == 0 &&
6250Sstevel@tonic-gate newlim.dqb_bhardlimit == 0 && newlim.dqb_bsoftlimit == 0)) {
6260Sstevel@tonic-gate scan_type = SQD_TYPE_NO_LIMIT;
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate dqp->dq_dqb = newlim;
6300Sstevel@tonic-gate dqp->dq_flags |= DQ_MOD;
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate * push the new quota to disk now. If this is a trans device
6340Sstevel@tonic-gate * then force the page out with ufs_putpage so it will be deltaed
6350Sstevel@tonic-gate * by ufs_startio.
6360Sstevel@tonic-gate */
6370Sstevel@tonic-gate qip = ufsvfsp->vfs_qinod;
6380Sstevel@tonic-gate rw_enter(&qip->i_contents, RW_WRITER);
6390Sstevel@tonic-gate (void) ufs_rdwri(UIO_WRITE, FWRITE | FSYNC, qip, (caddr_t)&dqp->dq_dqb,
6404662Sfrankho sizeof (struct dqblk), dqoff(uid), UIO_SYSSPACE,
6414662Sfrankho (int *)NULL, kcred);
6420Sstevel@tonic-gate rw_exit(&qip->i_contents);
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate (void) VOP_PUTPAGE(ITOV(qip), dqoff(dqp->dq_uid) & ~qip->i_fs->fs_bmask,
645*5331Samw qip->i_fs->fs_bsize, B_INVAL, kcred, NULL);
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate * We must set the dq_mof even if not we are not logging in case
6490Sstevel@tonic-gate * we are later remount to logging.
6500Sstevel@tonic-gate */
6510Sstevel@tonic-gate contig = 0;
6520Sstevel@tonic-gate rw_enter(&qip->i_contents, RW_WRITER);
6530Sstevel@tonic-gate error = bmap_read(qip, dqoff(dqp->dq_uid), &bn, &contig);
6540Sstevel@tonic-gate rw_exit(&qip->i_contents);
6550Sstevel@tonic-gate if (error || (bn == UFS_HOLE)) {
6560Sstevel@tonic-gate dqp->dq_mof = UFS_HOLE;
6570Sstevel@tonic-gate } else {
6580Sstevel@tonic-gate dqp->dq_mof = ldbtob(bn) +
6590Sstevel@tonic-gate (offset_t)((dqoff(dqp->dq_uid)) & (DEV_BSIZE - 1));
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate dqp->dq_flags &= ~DQ_MOD;
6630Sstevel@tonic-gate dqput(dqp);
6640Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
6650Sstevel@tonic-gate if (scan_type) {
6660Sstevel@tonic-gate struct setquota_data sqd;
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate sqd.sqd_type = scan_type;
6690Sstevel@tonic-gate sqd.sqd_ufsvfsp = ufsvfsp;
6700Sstevel@tonic-gate sqd.sqd_uid = uid;
6710Sstevel@tonic-gate (void) ufs_scan_inodes(0, setquota_scan_inode, &sqd, ufsvfsp);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
6740Sstevel@tonic-gate return (0);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate * Q_GETQUOTA - return current values in a dqblk structure.
6790Sstevel@tonic-gate */
6800Sstevel@tonic-gate static int
getquota(uid_t uid,struct ufsvfs * ufsvfsp,caddr_t addr,cred_t * cr)6810Sstevel@tonic-gate getquota(uid_t uid, struct ufsvfs *ufsvfsp, caddr_t addr, cred_t *cr)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate struct dquot *dqp;
6840Sstevel@tonic-gate struct dquot *xdqp;
6850Sstevel@tonic-gate struct dqblk dqb;
6860Sstevel@tonic-gate int error = 0;
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate if (uid != crgetruid(cr) &&
6890Sstevel@tonic-gate secpolicy_fs_quota(cr, ufsvfsp->vfs_vfs) != 0)
6900Sstevel@tonic-gate return (EPERM);
6910Sstevel@tonic-gate rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
6920Sstevel@tonic-gate if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
6930Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
6940Sstevel@tonic-gate return (ESRCH);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate error = getdiskquota(uid, ufsvfsp, 0, &xdqp);
6970Sstevel@tonic-gate if (error) {
6980Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
6990Sstevel@tonic-gate return (error);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate dqp = xdqp;
7020Sstevel@tonic-gate mutex_enter(&dqp->dq_lock);
7030Sstevel@tonic-gate if (dqp->dq_fhardlimit == 0 && dqp->dq_fsoftlimit == 0 &&
7040Sstevel@tonic-gate dqp->dq_bhardlimit == 0 && dqp->dq_bsoftlimit == 0) {
7050Sstevel@tonic-gate error = ESRCH;
7060Sstevel@tonic-gate } else {
7070Sstevel@tonic-gate bcopy(&dqp->dq_dqb, &dqb, sizeof (struct dqblk));
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate dqput(dqp);
7100Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
7110Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
7120Sstevel@tonic-gate if (error == 0 && copyout(&dqb, addr, sizeof (struct dqblk)) != 0)
7130Sstevel@tonic-gate error = EFAULT;
7140Sstevel@tonic-gate return (error);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate * Q_SYNC - sync quota files to disk.
7190Sstevel@tonic-gate */
7200Sstevel@tonic-gate int
qsync(struct ufsvfs * ufsvfsp)7210Sstevel@tonic-gate qsync(struct ufsvfs *ufsvfsp)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate return (quotasync(ufsvfsp, /* do_lock */ 1));
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate /*
7270Sstevel@tonic-gate * Sync quota information records to disk for the specified file system
7280Sstevel@tonic-gate * or all file systems with quotas if ufsvfsp == NULL. Grabs a reader
7290Sstevel@tonic-gate * lock on vfs_dqrwlock if it is needed.
7300Sstevel@tonic-gate *
7310Sstevel@tonic-gate * Currently, if ufsvfsp is NULL, then do_lock is always true, but the
7320Sstevel@tonic-gate * routine is coded to account for either do_lock value. This seemed
7330Sstevel@tonic-gate * to be the safer thing to do.
7340Sstevel@tonic-gate */
7350Sstevel@tonic-gate int
quotasync(struct ufsvfs * ufsvfsp,int do_lock)7360Sstevel@tonic-gate quotasync(struct ufsvfs *ufsvfsp, int do_lock)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate struct dquot *dqp;
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate rw_enter(&dq_rwlock, RW_READER);
7410Sstevel@tonic-gate if (!quotas_initialized) {
7420Sstevel@tonic-gate rw_exit(&dq_rwlock);
7430Sstevel@tonic-gate return (ESRCH);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate rw_exit(&dq_rwlock);
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate * The operation applies to a specific file system only.
7490Sstevel@tonic-gate */
7500Sstevel@tonic-gate if (ufsvfsp) {
7510Sstevel@tonic-gate if (do_lock) {
7520Sstevel@tonic-gate rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate * Quotas are not enabled on this file system so bail.
7570Sstevel@tonic-gate */
7580Sstevel@tonic-gate if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
7590Sstevel@tonic-gate if (do_lock) {
7600Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate return (ESRCH);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate * This operation is a no-op on a logging file system because
7670Sstevel@tonic-gate * quota information is treated as metadata and is in the log.
7680Sstevel@tonic-gate * This code path treats quota information as user data which
7690Sstevel@tonic-gate * is not necessary on a logging file system.
7700Sstevel@tonic-gate */
7710Sstevel@tonic-gate if (TRANS_ISTRANS(ufsvfsp)) {
7720Sstevel@tonic-gate if (do_lock) {
7730Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate return (0);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate /*
7790Sstevel@tonic-gate * Try to sync all the quota info records for this
7800Sstevel@tonic-gate * file system:
7810Sstevel@tonic-gate */
7820Sstevel@tonic-gate for (dqp = dquot; dqp < dquotNDQUOT; dqp++) {
7830Sstevel@tonic-gate /*
7840Sstevel@tonic-gate * If someone else has it, then ignore it.
7850Sstevel@tonic-gate */
7860Sstevel@tonic-gate if (!mutex_tryenter(&dqp->dq_lock)) {
7870Sstevel@tonic-gate continue;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate * The quota info record is for this file system
7920Sstevel@tonic-gate * and it has changes.
7930Sstevel@tonic-gate */
7940Sstevel@tonic-gate if (dqp->dq_ufsvfsp == ufsvfsp &&
7950Sstevel@tonic-gate (dqp->dq_flags & DQ_MOD)) {
7960Sstevel@tonic-gate ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
7970Sstevel@tonic-gate dqupdate(dqp);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate if (do_lock) {
8030Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate return (0);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate * Try to sync all the quota info records for *all* file systems
8110Sstevel@tonic-gate * for which quotas are enabled.
8120Sstevel@tonic-gate */
8130Sstevel@tonic-gate for (dqp = dquot; dqp < dquotNDQUOT; dqp++) {
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate * If someone else has it, then ignore it.
8160Sstevel@tonic-gate */
8170Sstevel@tonic-gate if (!mutex_tryenter(&dqp->dq_lock)) {
8180Sstevel@tonic-gate continue;
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate ufsvfsp = dqp->dq_ufsvfsp; /* shorthand */
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate * This quota info record has no changes or is
8250Sstevel@tonic-gate * not a valid quota info record yet.
8260Sstevel@tonic-gate */
8270Sstevel@tonic-gate if ((dqp->dq_flags & DQ_MOD) == 0 || ufsvfsp == NULL) {
8280Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
8290Sstevel@tonic-gate continue;
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate * Now we have a potential lock order problem:
8340Sstevel@tonic-gate *
8350Sstevel@tonic-gate * vfs_dqrwlock > dq_lock
8360Sstevel@tonic-gate *
8370Sstevel@tonic-gate * so if we have to get vfs_dqrwlock, then go thru hoops
8380Sstevel@tonic-gate * to avoid deadlock. If we cannot get the order right,
8390Sstevel@tonic-gate * then we ignore this quota info record.
8400Sstevel@tonic-gate */
8410Sstevel@tonic-gate if (do_lock) {
8420Sstevel@tonic-gate /*
8430Sstevel@tonic-gate * If we can't grab vfs_dqrwlock, then we don't
8440Sstevel@tonic-gate * want to wait to avoid deadlock.
8450Sstevel@tonic-gate */
8460Sstevel@tonic-gate if (rw_tryenter(&ufsvfsp->vfs_dqrwlock,
8474662Sfrankho RW_READER) == 0) {
8480Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
8490Sstevel@tonic-gate continue;
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate * Okay, now we have both dq_lock and vfs_dqrwlock.
8530Sstevel@tonic-gate * We should not deadlock for the following reasons:
8540Sstevel@tonic-gate * - If another thread has a reader lock on
8550Sstevel@tonic-gate * vfs_dqrwlock and is waiting for dq_lock,
8560Sstevel@tonic-gate * there is no conflict because we can also have
8570Sstevel@tonic-gate * a reader lock on vfs_dqrwlock.
8580Sstevel@tonic-gate * - If another thread has a writer lock on
8590Sstevel@tonic-gate * vfs_dqrwlock and is waiting for dq_lock,
8600Sstevel@tonic-gate * we would have failed the rw_tryenter() above
8610Sstevel@tonic-gate * and given up dq_lock.
8620Sstevel@tonic-gate * - Since we have dq_lock another thread cannot
8630Sstevel@tonic-gate * have it and be waiting for vfs_dqrwlock.
8640Sstevel@tonic-gate */
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate * Since we got to this file system via a quota info
8690Sstevel@tonic-gate * record and we have vfs_dqrwlock this is paranoia
8700Sstevel@tonic-gate * to make sure that quotas are enabled.
8710Sstevel@tonic-gate */
8720Sstevel@tonic-gate ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate * We are not logging. See above logging file system
8760Sstevel@tonic-gate * comment.
8770Sstevel@tonic-gate */
8780Sstevel@tonic-gate if (!TRANS_ISTRANS(ufsvfsp)) {
8790Sstevel@tonic-gate dqupdate(dqp);
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate * Since we have a private copy of dqp->dq_ufsvfsp,
8840Sstevel@tonic-gate * we can drop dq_lock now.
8850Sstevel@tonic-gate */
8860Sstevel@tonic-gate mutex_exit(&dqp->dq_lock);
8870Sstevel@tonic-gate
8880Sstevel@tonic-gate if (do_lock) {
8890Sstevel@tonic-gate rw_exit(&ufsvfsp->vfs_dqrwlock);
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate return (0);
8940Sstevel@tonic-gate }
895