#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/signal.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_quota.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/session.h>
#include <sys/debug.h>
struct dquot *
getinoquota(struct inode *ip)
{
struct dquot *dqp, *xdqp;
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
ASSERT(RW_WRITE_HELD(&ip->i_contents));
if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
return (NULL);
}
if (ip == ufsvfsp->vfs_qinod) {
return (NULL);
}
ASSERT((ip->i_mode & IFMT) != IFSHAD);
ASSERT((ip->i_mode & IFMT) != IFATTRDIR);
ASSERT(ip->i_mode);
if (getdiskquota((uid_t)ip->i_uid, ufsvfsp, 0, &xdqp)) {
return (NULL);
}
dqp = xdqp;
mutex_enter(&dqp->dq_lock);
ASSERT(ip->i_uid == dqp->dq_uid);
if (dqp->dq_fhardlimit == 0 && dqp->dq_fsoftlimit == 0 &&
dqp->dq_bhardlimit == 0 && dqp->dq_bsoftlimit == 0) {
dqput(dqp);
mutex_exit(&dqp->dq_lock);
dqp = NULL;
} else {
mutex_exit(&dqp->dq_lock);
}
return (dqp);
}
int
chkdq(struct inode *ip, long change, int force, struct cred *cr,
char **uerrp, size_t *lenp)
{
struct dquot *dqp;
uint64_t ncurblocks;
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
int error = 0;
long abs_change;
char *msg1 =
"!quota_ufs: over hard disk limit (pid %d, uid %d, inum %d, fs %s)\n";
char *msg2 =
"!quota_ufs: Warning: over disk limit (pid %d, uid %d, inum %d, fs %s)\n";
char *msg3 =
"!quota_ufs: over disk and time limit (pid %d, uid %d, inum %d, fs %s)\n";
char *msg4 =
"!quota_ufs: Warning: quota overflow (pid %d, uid %d, inum %d, fs %s)\n";
char *errmsg = NULL;
time_t now;
ASSERT((ip->i_mode & IFMT) == IFSHAD ||
RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
ASSERT(RW_WRITE_HELD(&ip->i_contents));
if (change == 0)
return (0);
dqp = ip->i_dquot;
ASSERT(dqp == NULL || ip->i_uid == dqp->dq_uid);
#ifdef DEBUG
if ((ip->i_mode & IFMT) == IFSHAD || (ip->i_mode & IFMT) == IFATTRDIR) {
ASSERT(dqp == NULL);
}
else {
struct dquot *expect_dq;
int mismatch_ok = 0;
expect_dq = getinoquota(ip);
if (expect_dq == NULL && ip != ufsvfsp->vfs_qinod) {
int error;
struct dquot *xdqp;
error = getdiskquota((uid_t)ip->i_uid, ufsvfsp, 0,
&xdqp);
switch (error) {
case 0:
if (xdqp->dq_fhardlimit == 0 &&
xdqp->dq_fsoftlimit == 0 &&
xdqp->dq_bhardlimit == 0 &&
xdqp->dq_bsoftlimit == 0) {
mutex_enter(&xdqp->dq_lock);
dqput(xdqp);
mutex_exit(&xdqp->dq_lock);
} else {
expect_dq = xdqp;
}
break;
case ESRCH:
case EINVAL:
case EUSERS:
case EIO:
mismatch_ok = 1;
break;
}
}
#ifndef CHASE_QUOTA
ASSERT(mismatch_ok || dqp == expect_dq);
#else
if (expect_dq == NULL) {
ASSERT(mismatch_ok || dqp == NULL);
} else {
ASSERT(dqp);
ASSERT(dqp == expect_dq);
}
#endif
if (expect_dq) {
mutex_enter(&expect_dq->dq_lock);
dqput(expect_dq);
mutex_exit(&expect_dq->dq_lock);
}
}
#endif
if (dqp == NULL)
return (0);
if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
return (0);
}
mutex_enter(&dqp->dq_lock);
if (change < 0) {
dqp->dq_flags |= DQ_MOD;
abs_change = -change;
if (dqp->dq_curblocks < abs_change)
dqp->dq_curblocks = 0;
else
dqp->dq_curblocks += change;
if (dqp->dq_curblocks < dqp->dq_bsoftlimit)
dqp->dq_btimelimit = 0;
dqp->dq_flags &= ~DQ_BLKS;
TRANS_QUOTA(dqp);
mutex_exit(&dqp->dq_lock);
return (0);
}
ncurblocks = (uint64_t)dqp->dq_curblocks + change;
if (ip->i_uid == 0)
goto out;
if (dqp->dq_bhardlimit && ncurblocks >= (uint64_t)dqp->dq_bhardlimit &&
!force) {
if ((dqp->dq_flags & DQ_BLKS) == 0 &&
ip->i_uid == crgetruid(cr)) {
errmsg = msg1;
dqp->dq_flags |= DQ_BLKS;
}
error = EDQUOT;
goto out;
}
if (dqp->dq_bsoftlimit && ncurblocks >= (uint64_t)dqp->dq_bsoftlimit) {
now = gethrestime_sec();
if (dqp->dq_curblocks < dqp->dq_bsoftlimit ||
dqp->dq_btimelimit == 0) {
dqp->dq_flags |= DQ_MOD;
dqp->dq_btimelimit = now +
((struct ufsvfs *)ITOV(ip)->v_vfsp->vfs_data)
->vfs_btimelimit;
if (ip->i_uid == crgetruid(cr)) {
errmsg = msg2;
}
} else if (now > dqp->dq_btimelimit && !force) {
if ((dqp->dq_flags & DQ_BLKS) == 0 &&
ip->i_uid == crgetruid(cr)) {
errmsg = msg3;
dqp->dq_flags |= DQ_BLKS;
}
error = EDQUOT;
}
}
out:
if (error == 0) {
dqp->dq_flags |= DQ_MOD;
if (ncurblocks > 0xffffffffLL) {
dqp->dq_curblocks = 0xffffffff;
errmsg = msg4;
} else {
dqp->dq_curblocks = ncurblocks;
}
}
if (dqp->dq_flags & DQ_MOD)
TRANS_QUOTA(dqp);
mutex_exit(&dqp->dq_lock);
if (errmsg != NULL) {
if (uerrp != NULL) {
*lenp = strlen(errmsg) + 20 + 20 +
strlen(ip->i_fs->fs_fsmnt) + 1;
*uerrp = (char *)kmem_alloc(*lenp, KM_NOSLEEP);
if (*uerrp != NULL) {
(void) sprintf(*uerrp, errmsg+1,
(int)ttoproc(curthread)->p_pid,
(int)ip->i_uid, (int)ip->i_number,
ip->i_fs->fs_fsmnt);
}
} else {
cmn_err(CE_NOTE, errmsg,
(int)ttoproc(curthread)->p_pid,
(int)ip->i_uid, (int)ip->i_number,
ip->i_fs->fs_fsmnt);
}
}
return (error);
}
int
chkiq(struct ufsvfs *ufsvfsp, int change, struct inode *ip, uid_t uid,
int force, struct cred *cr, char **uerrp, size_t *lenp)
{
struct dquot *dqp, *xdqp;
unsigned int ncurfiles;
char *errmsg = NULL;
char *err1 =
"!quota_ufs: over file hard limit (pid %d, uid %d, fs %s)\n";
char *err2 =
"!quota_ufs: Warning: too many files (pid %d, uid %d, fs %s)\n";
char *err3 =
"!quota_ufs: over file and time limit (pid %d, uid %d, fs %s)\n";
int error = 0;
time_t now;
ASSERT(RW_READ_HELD(&ufsvfsp->vfs_dqrwlock));
ASSERT(change == 1 || change == -1);
ASSERT(change != 1 || ip == NULL);
if ((ufsvfsp->vfs_qflags & MQ_ENABLED) == 0) {
return (0);
}
if (change == -1 && ip) {
dqp = ip->i_dquot;
if (dqp == NULL)
return (0);
mutex_enter(&dqp->dq_lock);
if (dqp->dq_curfiles) {
dqp->dq_curfiles--;
dqp->dq_flags |= DQ_MOD;
}
if (dqp->dq_curfiles < dqp->dq_fsoftlimit) {
dqp->dq_ftimelimit = 0;
dqp->dq_flags |= DQ_MOD;
}
dqp->dq_flags &= ~DQ_FILES;
if (dqp->dq_flags & DQ_MOD)
TRANS_QUOTA(dqp);
mutex_exit(&dqp->dq_lock);
return (0);
}
if (getdiskquota(uid, ufsvfsp, 0, &xdqp)) {
return (0);
}
dqp = xdqp;
mutex_enter(&dqp->dq_lock);
if (dqp->dq_fsoftlimit == 0 && dqp->dq_fhardlimit == 0) {
dqput(dqp);
mutex_exit(&dqp->dq_lock);
return (0);
}
if (uid == 0)
goto out;
ncurfiles = dqp->dq_curfiles + change;
if (change == 1 && ncurfiles >= dqp->dq_fhardlimit &&
dqp->dq_fhardlimit && !force) {
if ((dqp->dq_flags & DQ_FILES) == 0 && uid == crgetruid(cr)) {
errmsg = err1;
dqp->dq_flags |= DQ_FILES;
}
error = EDQUOT;
} else if (change == 1 && ncurfiles >= dqp->dq_fsoftlimit &&
dqp->dq_fsoftlimit) {
now = gethrestime_sec();
if (ncurfiles == dqp->dq_fsoftlimit ||
dqp->dq_ftimelimit == 0) {
dqp->dq_flags |= DQ_MOD;
dqp->dq_ftimelimit = now + ufsvfsp->vfs_ftimelimit;
if (uid == crgetruid(cr))
errmsg = err2;
} else if (now > dqp->dq_ftimelimit && !force) {
if ((dqp->dq_flags & DQ_FILES) == 0 &&
uid == crgetruid(cr)) {
errmsg = err3;
dqp->dq_flags |= DQ_FILES;
}
error = EDQUOT;
}
}
out:
if (error == 0) {
dqp->dq_flags |= DQ_MOD;
dqp->dq_curfiles += change;
}
if (dqp->dq_flags & DQ_MOD)
TRANS_QUOTA(dqp);
dqput(dqp);
mutex_exit(&dqp->dq_lock);
if (errmsg != NULL) {
if (uerrp != NULL) {
*lenp = strlen(errmsg) + 20 + 20 +
strlen(ufsvfsp->vfs_fs->fs_fsmnt) + 1;
*uerrp = (char *)kmem_alloc(*lenp, KM_NOSLEEP);
if (*uerrp != NULL) {
(void) sprintf(*uerrp, errmsg+1,
(int)ttoproc(curthread)->p_pid,
(int)uid, ufsvfsp->vfs_fs->fs_fsmnt);
}
} else {
cmn_err(CE_NOTE, errmsg,
(int)ttoproc(curthread)->p_pid,
(int)uid, ufsvfsp->vfs_fs->fs_fsmnt);
}
}
return (error);
}
void
dqrele(struct dquot *dqp)
{
if (dqp != NULL) {
mutex_enter(&dqp->dq_lock);
if (dqp->dq_cnt == 1 && dqp->dq_flags & DQ_MOD)
dqupdate(dqp);
dqput(dqp);
mutex_exit(&dqp->dq_lock);
}
}