#include <sys/cdefs.h>
#include "opt_ffs.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/endian.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>
#include <ufs/ufs/extattr.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64));
static int unprivileged_get_quota = 0;
SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
&unprivileged_get_quota, 0,
"Unprivileged processes may retrieve quotas for other uids and gids");
static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
static char *quotatypes[] = INITQFNAMES;
static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
static int dqopen(struct vnode *, struct ufsmount *, int);
static int dqget(struct vnode *,
uint64_t, struct ufsmount *, int, struct dquot **);
static int dqsync(struct vnode *, struct dquot *);
static int dqflush(struct vnode *);
static int quotaoff1(struct thread *td, struct mount *mp, int type);
static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
static void dqb32_dq(const struct dqblk32 *, struct dquot *);
static void dqb64_dq(const struct dqblk64 *, struct dquot *);
static void dq_dqb32(const struct dquot *, struct dqblk32 *);
static void dq_dqb64(const struct dquot *, struct dqblk64 *);
static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *);
static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *);
#ifdef DIAGNOSTIC
static void dqref(struct dquot *);
static void chkdquot(struct inode *);
#endif
int
getinoquota(struct inode *ip)
{
struct ufsmount *ump;
struct vnode *vp;
int error;
vp = ITOV(ip);
if ((vp->v_vflag & VV_SYSTEM) != 0)
return (0);
if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
return (0);
ump = VFSTOUFS(vp->v_mount);
if ((error =
dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
error != EINVAL)
return (error);
if ((error =
dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
error != EINVAL)
return (error);
return (0);
}
int
chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
{
struct dquot *dq;
ufs2_daddr_t ncurblocks;
struct vnode *vp = ITOV(ip);
int i, error, warn, do_check;
MPASS(cred != NOCRED || (flags & FORCE) != 0);
if ((vp->v_vflag & VV_SYSTEM) != 0)
return (0);
if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
return (0);
#ifdef DIAGNOSTIC
if ((flags & CHOWN) == 0)
chkdquot(ip);
#endif
if (change == 0)
return (0);
if (change < 0) {
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = ip->i_dquot[i]) == NODQUOT)
continue;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "chkdq1");
ncurblocks = dq->dq_curblocks + change;
if (ncurblocks >= 0)
dq->dq_curblocks = ncurblocks;
else
dq->dq_curblocks = 0;
dq->dq_flags &= ~DQ_BLKS;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
}
return (0);
}
if ((flags & FORCE) == 0 &&
priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA))
do_check = 1;
else
do_check = 0;
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = ip->i_dquot[i]) == NODQUOT)
continue;
warn = 0;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "chkdq2");
if (do_check) {
error = chkdqchg(ip, change, cred, i, &warn);
if (error) {
while (i > 0) {
--i;
dq = ip->i_dquot[i];
if (dq == NODQUOT)
continue;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "chkdq3");
ncurblocks = dq->dq_curblocks - change;
if (ncurblocks >= 0)
dq->dq_curblocks = ncurblocks;
else
dq->dq_curblocks = 0;
dq->dq_flags &= ~DQ_BLKS;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
}
return (error);
}
}
if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
dq->dq_curblocks < dq->dq_bsoftlimit)
dq->dq_btime = time_second + ITOUMP(ip)->um_btime[i];
dq->dq_curblocks += change;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
if (warn)
uprintf("\n%s: warning, %s disk quota exceeded\n",
ITOVFS(ip)->mnt_stat.f_mntonname,
quotatypes[i]);
}
return (0);
}
static int
chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
int type, int *warn)
{
struct dquot *dq = ip->i_dquot[type];
ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
if ((dq->dq_flags & DQ_BLKS) == 0 &&
ip->i_uid == cred->cr_uid) {
dq->dq_flags |= DQ_BLKS;
DQI_UNLOCK(dq);
uprintf("\n%s: write failed, %s disk limit reached\n",
ITOVFS(ip)->mnt_stat.f_mntonname,
quotatypes[type]);
return (EDQUOT);
}
DQI_UNLOCK(dq);
return (EDQUOT);
}
if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
if (dq->dq_curblocks < dq->dq_bsoftlimit) {
dq->dq_btime = time_second + ITOUMP(ip)->um_btime[type];
if (ip->i_uid == cred->cr_uid)
*warn = 1;
return (0);
}
if (time_second > dq->dq_btime) {
if ((dq->dq_flags & DQ_BLKS) == 0 &&
ip->i_uid == cred->cr_uid) {
dq->dq_flags |= DQ_BLKS;
DQI_UNLOCK(dq);
uprintf("\n%s: write failed, %s "
"disk quota exceeded for too long\n",
ITOVFS(ip)->mnt_stat.f_mntonname,
quotatypes[type]);
return (EDQUOT);
}
DQI_UNLOCK(dq);
return (EDQUOT);
}
}
return (0);
}
int
chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
{
struct dquot *dq;
int i, error, warn, do_check;
MPASS(cred != NOCRED || (flags & FORCE) != 0);
#ifdef DIAGNOSTIC
if ((flags & CHOWN) == 0)
chkdquot(ip);
#endif
if (change == 0)
return (0);
if (change < 0) {
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = ip->i_dquot[i]) == NODQUOT)
continue;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "chkiq1");
if (dq->dq_curinodes >= -change)
dq->dq_curinodes += change;
else
dq->dq_curinodes = 0;
dq->dq_flags &= ~DQ_INODS;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
}
return (0);
}
if ((flags & FORCE) == 0 &&
priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA))
do_check = 1;
else
do_check = 0;
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = ip->i_dquot[i]) == NODQUOT)
continue;
warn = 0;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "chkiq2");
if (do_check) {
error = chkiqchg(ip, change, cred, i, &warn);
if (error) {
while (i > 0) {
--i;
dq = ip->i_dquot[i];
if (dq == NODQUOT)
continue;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "chkiq3");
if (dq->dq_curinodes >= change)
dq->dq_curinodes -= change;
else
dq->dq_curinodes = 0;
dq->dq_flags &= ~DQ_INODS;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
}
return (error);
}
}
if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
dq->dq_curinodes < dq->dq_isoftlimit)
dq->dq_itime = time_second + ITOUMP(ip)->um_itime[i];
dq->dq_curinodes += change;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
if (warn)
uprintf("\n%s: warning, %s inode quota exceeded\n",
ITOVFS(ip)->mnt_stat.f_mntonname,
quotatypes[i]);
}
return (0);
}
static int
chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
{
struct dquot *dq = ip->i_dquot[type];
ino_t ncurinodes = dq->dq_curinodes + change;
if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
if ((dq->dq_flags & DQ_INODS) == 0 &&
ip->i_uid == cred->cr_uid) {
dq->dq_flags |= DQ_INODS;
DQI_UNLOCK(dq);
uprintf("\n%s: write failed, %s inode limit reached\n",
ITOVFS(ip)->mnt_stat.f_mntonname,
quotatypes[type]);
return (EDQUOT);
}
DQI_UNLOCK(dq);
return (EDQUOT);
}
if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
if (dq->dq_curinodes < dq->dq_isoftlimit) {
dq->dq_itime = time_second + ITOUMP(ip)->um_itime[type];
if (ip->i_uid == cred->cr_uid)
*warn = 1;
return (0);
}
if (time_second > dq->dq_itime) {
if ((dq->dq_flags & DQ_INODS) == 0 &&
ip->i_uid == cred->cr_uid) {
dq->dq_flags |= DQ_INODS;
DQI_UNLOCK(dq);
uprintf("\n%s: write failed, %s "
"inode quota exceeded for too long\n",
ITOVFS(ip)->mnt_stat.f_mntonname,
quotatypes[type]);
return (EDQUOT);
}
DQI_UNLOCK(dq);
return (EDQUOT);
}
}
return (0);
}
#ifdef DIAGNOSTIC
static void
chkdquot(struct inode *ip)
{
struct ufsmount *ump;
struct vnode *vp;
int i;
ump = ITOUMP(ip);
vp = ITOV(ip);
if ((vp->v_vflag & VV_SYSTEM) != 0)
return;
if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
return;
UFS_LOCK(ump);
for (i = 0; i < MAXQUOTAS; i++) {
if (ump->um_quotas[i] == NULL ||
(ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
continue;
if (ip->i_dquot[i] == NODQUOT) {
UFS_UNLOCK(ump);
vn_printf(ITOV(ip), "chkdquot: missing dquot ");
panic("chkdquot: missing dquot");
}
}
UFS_UNLOCK(ump);
}
#endif
int
quotaon(struct thread *td, struct mount *mp, int type, void *fname,
bool *mp_busy)
{
struct ufsmount *ump;
struct vnode *vp, **vpp;
struct vnode *mvp;
struct dquot *dq;
int error, flags;
struct nameidata nd;
error = priv_check(td, PRIV_UFS_QUOTAON);
if (error != 0)
return (error);
if ((mp->mnt_flag & MNT_RDONLY) != 0)
return (EROFS);
ump = VFSTOUFS(mp);
dq = NODQUOT;
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname);
flags = FREAD | FWRITE;
vfs_ref(mp);
KASSERT(*mp_busy, ("%s called without busied mount", __func__));
vfs_unbusy(mp);
*mp_busy = false;
error = vn_open(&nd, &flags, 0, NULL);
if (error != 0) {
vfs_rel(mp);
return (error);
}
NDFREE_PNBUF(&nd);
vp = nd.ni_vp;
error = vfs_busy(mp, MBF_NOWAIT);
vfs_rel(mp);
if (error == 0) {
*mp_busy = true;
if (vp->v_type != VREG)
error = EACCES;
}
if (error != 0) {
VOP_UNLOCK(vp);
(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
return (error);
}
UFS_LOCK(ump);
if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
UFS_UNLOCK(ump);
VOP_UNLOCK(vp);
(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
return (EALREADY);
}
ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
UFS_UNLOCK(ump);
if ((error = dqopen(vp, ump, type)) != 0) {
VOP_UNLOCK(vp);
UFS_LOCK(ump);
ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
UFS_UNLOCK(ump);
(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
return (error);
}
VOP_UNLOCK(vp);
MNT_ILOCK(mp);
mp->mnt_flag |= MNT_QUOTA;
mp->mnt_stat.f_flags |= MNT_QUOTA;
MNT_IUNLOCK(mp);
vpp = &ump->um_quotas[type];
if (*vpp != vp)
quotaoff1(td, mp, type);
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
vp->v_vflag |= VV_SYSTEM;
VN_LOCK_AREC(vp);
VN_LOCK_DSHARE(vp);
VOP_UNLOCK(vp);
*vpp = vp;
UFS_LOCK(ump);
ump->um_qflags[type] &= ~QTF_CLOSING;
UFS_UNLOCK(ump);
ump->um_cred[type] = crhold(td->td_ucred);
ump->um_btime[type] = MAX_DQ_TIME;
ump->um_itime[type] = MAX_IQ_TIME;
if (dqget(NULL, 0, ump, type, &dq) == 0) {
if (dq->dq_btime > 0)
ump->um_btime[type] = dq->dq_btime;
if (dq->dq_itime > 0)
ump->um_itime[type] = dq->dq_itime;
dqrele(NULL, dq);
}
again:
MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
goto again;
}
if (vp->v_type == VNON || vp->v_writecount <= 0) {
vput(vp);
continue;
}
error = getinoquota(VTOI(vp));
vput(vp);
if (error) {
MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
break;
}
}
if (error)
quotaoff_inchange(td, mp, type);
UFS_LOCK(ump);
ump->um_qflags[type] &= ~QTF_OPENING;
KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
("quotaon: leaking flags"));
UFS_UNLOCK(ump);
return (error);
}
static int
quotaoff1(struct thread *td, struct mount *mp, int type)
{
struct vnode *vp;
struct vnode *qvp, *mvp;
struct ufsmount *ump;
struct dquot *dq;
struct inode *ip;
struct ucred *cr;
int error;
ump = VFSTOUFS(mp);
UFS_LOCK(ump);
KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
("quotaoff1: flags are invalid"));
if ((qvp = ump->um_quotas[type]) == NULL) {
UFS_UNLOCK(ump);
return (0);
}
cr = ump->um_cred[type];
UFS_UNLOCK(ump);
again:
MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
if (vp->v_type == VNON) {
VI_UNLOCK(vp);
continue;
}
if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
goto again;
}
ip = VTOI(vp);
dq = ip->i_dquot[type];
ip->i_dquot[type] = NODQUOT;
dqrele(vp, dq);
vput(vp);
}
error = dqflush(qvp);
if (error != 0)
return (error);
UFS_LOCK(ump);
ump->um_quotas[type] = NULL;
ump->um_cred[type] = NOCRED;
UFS_UNLOCK(ump);
vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
qvp->v_vflag &= ~VV_SYSTEM;
VOP_UNLOCK(qvp);
error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
crfree(cr);
return (error);
}
static int
quotaoff_inchange1(struct thread *td, struct mount *mp, int type)
{
int error;
bool need_resume;
if (mp->mnt_susp_owner == td) {
need_resume = false;
} else {
error = vfs_write_suspend_umnt(mp);
if (error != 0)
return (error);
need_resume = true;
}
error = quotaoff1(td, mp, type);
if (need_resume)
vfs_write_resume(mp, VR_START_WRITE);
return (error);
}
int
quotaoff_inchange(struct thread *td, struct mount *mp, int type)
{
struct ufsmount *ump;
int error, i;
error = quotaoff_inchange1(td, mp, type);
ump = VFSTOUFS(mp);
UFS_LOCK(ump);
ump->um_qflags[type] &= ~QTF_CLOSING;
for (i = 0; i < MAXQUOTAS; i++)
if (ump->um_quotas[i] != NULL)
break;
if (i == MAXQUOTAS) {
MNT_ILOCK(mp);
mp->mnt_flag &= ~MNT_QUOTA;
mp->mnt_stat.f_flags &= ~MNT_QUOTA;
MNT_IUNLOCK(mp);
}
UFS_UNLOCK(ump);
return (error);
}
int
quotaoff(struct thread *td, struct mount *mp, int type)
{
struct ufsmount *ump;
int error;
error = priv_check(td, PRIV_UFS_QUOTAOFF);
if (error)
return (error);
ump = VFSTOUFS(mp);
UFS_LOCK(ump);
if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
UFS_UNLOCK(ump);
return (EALREADY);
}
ump->um_qflags[type] |= QTF_CLOSING;
UFS_UNLOCK(ump);
return (quotaoff_inchange(td, mp, type));
}
static int
_getquota(struct thread *td, struct mount *mp, uint64_t id, int type,
struct dqblk64 *dqb)
{
struct dquot *dq;
int error;
switch (type) {
case USRQUOTA:
if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
error = priv_check(td, PRIV_VFS_GETQUOTA);
if (error)
return (error);
}
break;
case GRPQUOTA:
if (!groupmember(id, td->td_ucred) &&
!unprivileged_get_quota) {
error = priv_check(td, PRIV_VFS_GETQUOTA);
if (error)
return (error);
}
break;
default:
return (EINVAL);
}
dq = NODQUOT;
error = dqget(NULL, id, VFSTOUFS(mp), type, &dq);
if (error)
return (error);
*dqb = dq->dq_dqb;
dqrele(NULL, dq);
return (error);
}
static int
_setquota(struct thread *td, struct mount *mp, uint64_t id, int type,
struct dqblk64 *dqb)
{
struct dquot *dq;
struct dquot *ndq;
struct ufsmount *ump;
struct dqblk64 newlim;
int error;
error = priv_check(td, PRIV_VFS_SETQUOTA);
if (error)
return (error);
newlim = *dqb;
ndq = NODQUOT;
ump = VFSTOUFS(mp);
error = dqget(NULL, id, ump, type, &ndq);
if (error)
return (error);
dq = ndq;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "setqta");
newlim.dqb_curblocks = dq->dq_curblocks;
newlim.dqb_curinodes = dq->dq_curinodes;
if (dq->dq_id != 0) {
newlim.dqb_btime = dq->dq_btime;
newlim.dqb_itime = dq->dq_itime;
}
if (newlim.dqb_bsoftlimit &&
dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
(dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
newlim.dqb_btime = time_second + ump->um_btime[type];
if (newlim.dqb_isoftlimit &&
dq->dq_curinodes >= newlim.dqb_isoftlimit &&
(dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
newlim.dqb_itime = time_second + ump->um_itime[type];
dq->dq_dqb = newlim;
if (dq->dq_curblocks < dq->dq_bsoftlimit)
dq->dq_flags &= ~DQ_BLKS;
if (dq->dq_curinodes < dq->dq_isoftlimit)
dq->dq_flags &= ~DQ_INODS;
if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
dq->dq_flags |= DQ_FAKE;
else
dq->dq_flags &= ~DQ_FAKE;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
dqrele(NULL, dq);
return (0);
}
static int
_setuse(struct thread *td, struct mount *mp, uint64_t id, int type,
struct dqblk64 *dqb)
{
struct dquot *dq;
struct ufsmount *ump;
struct dquot *ndq;
struct dqblk64 usage;
int error;
error = priv_check(td, PRIV_UFS_SETUSE);
if (error)
return (error);
usage = *dqb;
ump = VFSTOUFS(mp);
ndq = NODQUOT;
error = dqget(NULL, id, ump, type, &ndq);
if (error)
return (error);
dq = ndq;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "setuse");
if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
usage.dqb_curblocks >= dq->dq_bsoftlimit)
dq->dq_btime = time_second + ump->um_btime[type];
if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
usage.dqb_curinodes >= dq->dq_isoftlimit)
dq->dq_itime = time_second + ump->um_itime[type];
dq->dq_curblocks = usage.dqb_curblocks;
dq->dq_curinodes = usage.dqb_curinodes;
if (dq->dq_curblocks < dq->dq_bsoftlimit)
dq->dq_flags &= ~DQ_BLKS;
if (dq->dq_curinodes < dq->dq_isoftlimit)
dq->dq_flags &= ~DQ_INODS;
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
dqrele(NULL, dq);
return (0);
}
int
getquota32(struct thread *td, struct mount *mp, uint64_t id, int type,
void *addr)
{
struct dqblk32 dqb32;
struct dqblk64 dqb64;
int error;
error = _getquota(td, mp, id, type, &dqb64);
if (error)
return (error);
dqb64_dqb32(&dqb64, &dqb32);
error = copyout(&dqb32, addr, sizeof(dqb32));
return (error);
}
int
setquota32(struct thread *td, struct mount *mp, uint64_t id, int type,
void *addr)
{
struct dqblk32 dqb32;
struct dqblk64 dqb64;
int error;
error = copyin(addr, &dqb32, sizeof(dqb32));
if (error)
return (error);
dqb32_dqb64(&dqb32, &dqb64);
error = _setquota(td, mp, id, type, &dqb64);
return (error);
}
int
setuse32(struct thread *td, struct mount *mp, uint64_t id, int type, void *addr)
{
struct dqblk32 dqb32;
struct dqblk64 dqb64;
int error;
error = copyin(addr, &dqb32, sizeof(dqb32));
if (error)
return (error);
dqb32_dqb64(&dqb32, &dqb64);
error = _setuse(td, mp, id, type, &dqb64);
return (error);
}
int
getquota(struct thread *td, struct mount *mp, uint64_t id, int type, void *addr)
{
struct dqblk64 dqb64;
int error;
error = _getquota(td, mp, id, type, &dqb64);
if (error)
return (error);
error = copyout(&dqb64, addr, sizeof(dqb64));
return (error);
}
int
setquota(struct thread *td, struct mount *mp, uint64_t id, int type, void *addr)
{
struct dqblk64 dqb64;
int error;
error = copyin(addr, &dqb64, sizeof(dqb64));
if (error)
return (error);
error = _setquota(td, mp, id, type, &dqb64);
return (error);
}
int
setuse(struct thread *td, struct mount *mp, uint64_t id, int type, void *addr)
{
struct dqblk64 dqb64;
int error;
error = copyin(addr, &dqb64, sizeof(dqb64));
if (error)
return (error);
error = _setuse(td, mp, id, type, &dqb64);
return (error);
}
int
getquotasize(struct thread *td, struct mount *mp, uint64_t id, int type,
void *sizep)
{
struct ufsmount *ump = VFSTOUFS(mp);
int bitsize;
UFS_LOCK(ump);
if (ump->um_quotas[type] == NULL ||
(ump->um_qflags[type] & QTF_CLOSING)) {
UFS_UNLOCK(ump);
return (EINVAL);
}
if ((ump->um_qflags[type] & QTF_64BIT) != 0)
bitsize = 64;
else
bitsize = 32;
UFS_UNLOCK(ump);
return (copyout(&bitsize, sizep, sizeof(int)));
}
int
qsync(struct mount *mp)
{
struct ufsmount *ump = VFSTOUFS(mp);
struct vnode *vp, *mvp;
struct dquot *dq;
int i, error;
for (i = 0; i < MAXQUOTAS; i++)
if (ump->um_quotas[i] != NULL)
break;
if (i == MAXQUOTAS)
return (0);
again:
MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
if (vp->v_type == VNON) {
VI_UNLOCK(vp);
continue;
}
error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK);
if (error) {
if (error == ENOENT) {
MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
goto again;
}
continue;
}
for (i = 0; i < MAXQUOTAS; i++) {
dq = VTOI(vp)->i_dquot[i];
if (dq != NODQUOT)
dqsync(vp, dq);
}
vput(vp);
}
return (0);
}
int
qsyncvp(struct vnode *vp)
{
struct ufsmount *ump = VFSTOUFS(vp->v_mount);
struct dquot *dq;
int i;
for (i = 0; i < MAXQUOTAS; i++)
if (ump->um_quotas[i] != NULL)
break;
if (i == MAXQUOTAS)
return (0);
for (i = 0; i < MAXQUOTAS; i++) {
dq = VTOI(vp)->i_dquot[i];
if (dq != NODQUOT)
dqsync(vp, dq);
}
return (0);
}
#define DQHASH(dqvp, id) \
(&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
static LIST_HEAD(dqhash, dquot) *dqhashtbl;
static u_long dqhash;
#define DQUOTINC 5
static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
static long numdquot, desireddquot = DQUOTINC;
struct mtx dqhlock;
#define DQH_LOCK() mtx_lock(&dqhlock)
#define DQH_UNLOCK() mtx_unlock(&dqhlock)
static struct dquot *dqhashfind(struct dqhash *dqh, uint64_t id,
struct vnode *dqvp);
void
dqinit(void)
{
mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
TAILQ_INIT(&dqfreelist);
}
void
dquninit(void)
{
struct dquot *dq;
hashdestroy(dqhashtbl, M_DQUOT, dqhash);
while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
mtx_destroy(&dq->dq_lock);
free(dq, M_DQUOT);
}
mtx_destroy(&dqhlock);
}
static struct dquot *
dqhashfind(struct dqhash *dqh, uint64_t id, struct vnode *dqvp)
{
struct dquot *dq;
mtx_assert(&dqhlock, MA_OWNED);
LIST_FOREACH(dq, dqh, dq_hash) {
if (dq->dq_id != id ||
dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
continue;
if (dq->dq_cnt == 0)
TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
DQREF(dq);
return (dq);
}
return (NODQUOT);
}
static int
dqopen(struct vnode *vp, struct ufsmount *ump, int type)
{
struct dqhdr64 dqh;
struct iovec aiov;
struct uio auio;
int error;
ASSERT_VOP_LOCKED(vp, "dqopen");
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
aiov.iov_base = &dqh;
aiov.iov_len = sizeof(dqh);
auio.uio_resid = sizeof(dqh);
auio.uio_offset = 0;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_READ;
auio.uio_td = (struct thread *)0;
error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
if (error != 0)
return (error);
if (auio.uio_resid > 0) {
return (0);
}
UFS_LOCK(ump);
if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) {
ump->um_qflags[type] |= QTF_64BIT;
} else {
ump->um_qflags[type] &= ~QTF_64BIT;
}
UFS_UNLOCK(ump);
return (0);
}
static int
dqget(struct vnode *vp, uint64_t id, struct ufsmount *ump, int type,
struct dquot **dqp)
{
uint8_t buf[sizeof(struct dqblk64)];
off_t base, recsize;
struct dquot *dq, *dq1;
struct dqhash *dqh;
struct vnode *dqvp;
struct iovec aiov;
struct uio auio;
int dqvplocked, error;
if (vp != NULL)
ASSERT_VOP_ELOCKED(vp, "dqget");
if (vp != NULL && *dqp != NODQUOT) {
return (0);
}
if ((int)id < 0)
return (EINVAL);
UFS_LOCK(ump);
dqvp = ump->um_quotas[type];
if (dqvp == NULL || (ump->um_qflags[type] & QTF_CLOSING)) {
*dqp = NODQUOT;
UFS_UNLOCK(ump);
return (EINVAL);
}
vref(dqvp);
UFS_UNLOCK(ump);
error = 0;
dqvplocked = 0;
dqh = DQHASH(dqvp, id);
DQH_LOCK();
dq = dqhashfind(dqh, id, dqvp);
if (dq != NULL) {
DQH_UNLOCK();
hfound: DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "dqget");
DQI_UNLOCK(dq);
if (dq->dq_ump == NULL) {
dqrele(vp, dq);
dq = NODQUOT;
error = EIO;
}
*dqp = dq;
if (dqvplocked)
vput(dqvp);
else
vrele(dqvp);
return (error);
}
if (vp != dqvp) {
DQH_UNLOCK();
vn_lock(dqvp, LK_SHARED | LK_RETRY);
dqvplocked = 1;
DQH_LOCK();
dq = dqhashfind(dqh, id, dqvp);
if (dq != NULL) {
DQH_UNLOCK();
goto hfound;
}
}
if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
numdquot < MAXQUOTAS * desiredvnodes)
desireddquot += DQUOTINC;
if (numdquot < desireddquot) {
numdquot++;
DQH_UNLOCK();
dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
DQH_LOCK();
dq = dqhashfind(dqh, id, dqvp);
if (dq != NULL) {
numdquot--;
DQH_UNLOCK();
mtx_destroy(&dq1->dq_lock);
free(dq1, M_DQUOT);
goto hfound;
}
dq = dq1;
} else {
if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
DQH_UNLOCK();
tablefull("dquot");
*dqp = NODQUOT;
if (dqvplocked)
vput(dqvp);
else
vrele(dqvp);
return (EUSERS);
}
if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
panic("dqget: free dquot isn't %p", dq);
TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
if (dq->dq_ump != NULL)
LIST_REMOVE(dq, dq_hash);
}
dq->dq_flags = DQ_LOCK;
dq->dq_id = id;
dq->dq_type = type;
dq->dq_ump = ump;
LIST_INSERT_HEAD(dqh, dq, dq_hash);
DQREF(dq);
DQH_UNLOCK();
if (ump->um_qflags[type] & QTF_64BIT) {
recsize = sizeof(struct dqblk64);
base = sizeof(struct dqhdr64);
} else {
recsize = sizeof(struct dqblk32);
base = 0;
}
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
aiov.iov_base = buf;
aiov.iov_len = recsize;
auio.uio_resid = recsize;
auio.uio_offset = base + id * recsize;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_READ;
auio.uio_td = (struct thread *)0;
error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
if (auio.uio_resid == recsize && error == 0) {
bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
} else {
if (ump->um_qflags[type] & QTF_64BIT)
dqb64_dq((struct dqblk64 *)buf, dq);
else
dqb32_dq((struct dqblk32 *)buf, dq);
}
if (dqvplocked)
vput(dqvp);
else
vrele(dqvp);
if (error) {
DQH_LOCK();
dq->dq_ump = NULL;
LIST_REMOVE(dq, dq_hash);
DQH_UNLOCK();
DQI_LOCK(dq);
if (dq->dq_flags & DQ_WANT)
wakeup(dq);
dq->dq_flags = 0;
DQI_UNLOCK(dq);
dqrele(vp, dq);
*dqp = NODQUOT;
return (error);
}
DQI_LOCK(dq);
if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
dq->dq_flags |= DQ_FAKE;
if (dq->dq_id != 0) {
if (dq->dq_btime == 0) {
dq->dq_btime = time_second + ump->um_btime[type];
if (dq->dq_bsoftlimit &&
dq->dq_curblocks >= dq->dq_bsoftlimit)
dq->dq_flags |= DQ_MOD;
}
if (dq->dq_itime == 0) {
dq->dq_itime = time_second + ump->um_itime[type];
if (dq->dq_isoftlimit &&
dq->dq_curinodes >= dq->dq_isoftlimit)
dq->dq_flags |= DQ_MOD;
}
}
DQI_WAKEUP(dq);
DQI_UNLOCK(dq);
*dqp = dq;
return (0);
}
#ifdef DIAGNOSTIC
static void
dqref(struct dquot *dq)
{
dq->dq_cnt++;
}
#endif
void
dqrele(struct vnode *vp, struct dquot *dq)
{
if (dq == NODQUOT)
return;
DQH_LOCK();
KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
if (dq->dq_cnt > 1) {
dq->dq_cnt--;
DQH_UNLOCK();
return;
}
DQH_UNLOCK();
sync:
(void) dqsync(vp, dq);
DQH_LOCK();
KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
if (--dq->dq_cnt > 0)
{
DQH_UNLOCK();
return;
}
if ((dq->dq_flags & DQ_MOD) != 0) {
dq->dq_cnt++;
DQH_UNLOCK();
goto sync;
}
TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
DQH_UNLOCK();
}
static int
dqsync(struct vnode *vp, struct dquot *dq)
{
uint8_t buf[sizeof(struct dqblk64)];
off_t base, recsize;
struct vnode *dqvp;
struct iovec aiov;
struct uio auio;
int error;
struct mount *mp;
struct ufsmount *ump;
if (vp != NULL)
ASSERT_VOP_ELOCKED(vp, "dqsync");
mp = NULL;
error = 0;
if (dq == NODQUOT)
panic("dqsync: dquot");
if ((ump = dq->dq_ump) == NULL)
return (0);
UFS_LOCK(ump);
if ((dqvp = ump->um_quotas[dq->dq_type]) == NULL) {
if (vp == NULL) {
UFS_UNLOCK(ump);
return (0);
} else
panic("dqsync: file");
}
vref(dqvp);
UFS_UNLOCK(ump);
DQI_LOCK(dq);
if ((dq->dq_flags & DQ_MOD) == 0) {
DQI_UNLOCK(dq);
vrele(dqvp);
return (0);
}
DQI_UNLOCK(dq);
(void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
if (vp != dqvp)
vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "dqsync");
if ((dq->dq_flags & DQ_MOD) == 0)
goto out;
dq->dq_flags |= DQ_LOCK;
DQI_UNLOCK(dq);
if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
dq_dqb64(dq, (struct dqblk64 *)buf);
recsize = sizeof(struct dqblk64);
base = sizeof(struct dqhdr64);
} else {
dq_dqb32(dq, (struct dqblk32 *)buf);
recsize = sizeof(struct dqblk32);
base = 0;
}
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
aiov.iov_base = buf;
aiov.iov_len = recsize;
auio.uio_resid = recsize;
auio.uio_offset = base + dq->dq_id * recsize;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_td = (struct thread *)0;
error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
if (auio.uio_resid && error == 0)
error = EIO;
DQI_LOCK(dq);
DQI_WAKEUP(dq);
dq->dq_flags &= ~DQ_MOD;
out:
DQI_UNLOCK(dq);
if (vp != dqvp)
vput(dqvp);
else
vrele(dqvp);
vn_finished_secondary_write(mp);
return (error);
}
static int
dqflush(struct vnode *vp)
{
struct dquot *dq, *nextdq;
struct dqhash *dqh;
int error;
error = 0;
DQH_LOCK();
for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
nextdq = LIST_NEXT(dq, dq_hash);
if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
continue;
if (dq->dq_cnt)
error = EBUSY;
else {
LIST_REMOVE(dq, dq_hash);
dq->dq_ump = NULL;
}
}
}
DQH_UNLOCK();
return (error);
}
#ifdef SOFTUPDATES
int
quotaref(struct vnode *vp, struct dquot **qrp)
{
struct inode *ip;
struct dquot *dq;
int i, found;
for (i = 0; i < MAXQUOTAS; i++)
qrp[i] = NODQUOT;
if ((vp->v_vflag & VV_SYSTEM) != 0)
return (0);
found = 0;
ip = VTOI(vp);
mtx_lock(&dqhlock);
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = ip->i_dquot[i]) == NODQUOT)
continue;
DQREF(dq);
qrp[i] = dq;
found++;
}
mtx_unlock(&dqhlock);
return (found);
}
void
quotarele(struct dquot **qrp)
{
struct dquot *dq;
int i;
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = qrp[i]) == NODQUOT)
continue;
dqrele(NULL, dq);
}
}
void
quotaadj(struct dquot **qrp, struct ufsmount *ump, int64_t blkcount)
{
struct dquot *dq;
ufs2_daddr_t ncurblocks;
int i;
if (blkcount == 0)
return;
for (i = 0; i < MAXQUOTAS; i++) {
if ((dq = qrp[i]) == NODQUOT)
continue;
DQI_LOCK(dq);
DQI_WAIT(dq, PINOD, "adjqta");
ncurblocks = dq->dq_curblocks + blkcount;
if (ncurblocks >= 0)
dq->dq_curblocks = ncurblocks;
else
dq->dq_curblocks = 0;
if (blkcount < 0)
dq->dq_flags &= ~DQ_BLKS;
else if (dq->dq_curblocks + blkcount >= dq->dq_bsoftlimit &&
dq->dq_curblocks < dq->dq_bsoftlimit)
dq->dq_btime = time_second + ump->um_btime[i];
dq->dq_flags |= DQ_MOD;
DQI_UNLOCK(dq);
}
}
#endif
#define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
static void
dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
{
dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
dq->dq_curblocks = dqb32->dqb_curblocks;
dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
dq->dq_curinodes = dqb32->dqb_curinodes;
dq->dq_btime = dqb32->dqb_btime;
dq->dq_itime = dqb32->dqb_itime;
}
static void
dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
{
dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
dq->dq_btime = be64toh(dqb64->dqb_btime);
dq->dq_itime = be64toh(dqb64->dqb_itime);
}
static void
dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
{
dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
dqb32->dqb_btime = CLIP32(dq->dq_btime);
dqb32->dqb_itime = CLIP32(dq->dq_itime);
}
static void
dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
{
dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
dqb64->dqb_btime = htobe64(dq->dq_btime);
dqb64->dqb_itime = htobe64(dq->dq_itime);
}
static void
dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
{
dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
}
static void
dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
{
dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
dqb64->dqb_curblocks = dqb32->dqb_curblocks;
dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
dqb64->dqb_curinodes = dqb32->dqb_curinodes;
dqb64->dqb_btime = dqb32->dqb_btime;
dqb64->dqb_itime = dqb32->dqb_itime;
}