xfs_quotaops.c 6.5 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
/*
 * Copyright (c) 2008, Christoph Hellwig
 * All Rights Reserved.
 */
#include "xfs.h"
7
#include "xfs_shared.h"
8
#include "xfs_format.h"
9
#include "xfs_log_format.h"
10
#include "xfs_trans_resv.h"
11
#include "xfs_mount.h"
12
#include "xfs_inode.h"
13 14
#include "xfs_quota.h"
#include "xfs_trans.h"
15
#include "xfs_icache.h"
16
#include "xfs_qm.h"
17 18


19 20 21 22 23 24
static void
xfs_qm_fill_state(
	struct qc_type_state	*tstate,
	struct xfs_mount	*mp,
	struct xfs_inode	*ip,
	xfs_ino_t		ino)
25
{
26 27 28 29 30 31 32 33 34 35
	struct xfs_quotainfo *q = mp->m_quotainfo;
	bool tempqip = false;

	tstate->ino = ino;
	if (!ip && ino == NULLFSINO)
		return;
	if (!ip) {
		if (xfs_iget(mp, NULL, ino, 0, 0, &ip))
			return;
		tempqip = true;
36
	}
37 38 39 40 41 42 43 44 45 46
	tstate->flags |= QCI_SYSFILE;
	tstate->blocks = ip->i_d.di_nblocks;
	tstate->nextents = ip->i_d.di_nextents;
	tstate->spc_timelimit = q->qi_btimelimit;
	tstate->ino_timelimit = q->qi_itimelimit;
	tstate->rt_spc_timelimit = q->qi_rtbtimelimit;
	tstate->spc_warnlimit = q->qi_bwarnlimit;
	tstate->ino_warnlimit = q->qi_iwarnlimit;
	tstate->rt_spc_warnlimit = q->qi_rtbwarnlimit;
	if (tempqip)
47
		xfs_irele(ip);
48 49
}

50 51 52 53 54 55
/*
 * Return quota status information, such as enforcements, quota file inode
 * numbers etc.
 */
static int
xfs_fs_get_quota_state(
56
	struct super_block	*sb,
57
	struct qc_state		*state)
58
{
59 60
	struct xfs_mount *mp = XFS_M(sb);
	struct xfs_quotainfo *q = mp->m_quotainfo;
61

62
	memset(state, 0, sizeof(*state));
63
	if (!XFS_IS_QUOTA_RUNNING(mp))
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		return 0;
	state->s_incoredqs = q->qi_dquots;
	if (XFS_IS_UQUOTA_RUNNING(mp))
		state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED;
	if (XFS_IS_UQUOTA_ENFORCED(mp))
		state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
	if (XFS_IS_GQUOTA_RUNNING(mp))
		state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED;
	if (XFS_IS_GQUOTA_ENFORCED(mp))
		state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
	if (XFS_IS_PQUOTA_RUNNING(mp))
		state->s_state[PRJQUOTA].flags |= QCI_ACCT_ENABLED;
	if (XFS_IS_PQUOTA_ENFORCED(mp))
		state->s_state[PRJQUOTA].flags |= QCI_LIMITS_ENFORCED;

	xfs_qm_fill_state(&state->s_state[USRQUOTA], mp, q->qi_uquotaip,
			  mp->m_sb.sb_uquotino);
	xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp, q->qi_gquotaip,
			  mp->m_sb.sb_gquotino);
	xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp, q->qi_pquotaip,
			  mp->m_sb.sb_pquotino);
	return 0;
86 87
}

88
STATIC int
89
xfs_quota_type(int type)
90
{
91 92 93 94 95 96 97 98
	switch (type) {
	case USRQUOTA:
		return XFS_DQ_USER;
	case GRPQUOTA:
		return XFS_DQ_GROUP;
	default:
		return XFS_DQ_PROJ;
	}
99 100
}

J
Jan Kara 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114
#define XFS_QC_SETINFO_MASK (QC_TIMER_MASK | QC_WARNS_MASK)

/*
 * Adjust quota timers & warnings
 */
static int
xfs_fs_set_info(
	struct super_block	*sb,
	int			type,
	struct qc_info		*info)
{
	struct xfs_mount *mp = XFS_M(sb);
	struct qc_dqblk newlim;

115
	if (sb_rdonly(sb))
J
Jan Kara 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		return -EROFS;
	if (!XFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!XFS_IS_QUOTA_ON(mp))
		return -ESRCH;
	if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
		return -EINVAL;
	if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
		return 0;

	newlim.d_fieldmask = info->i_fieldmask;
	newlim.d_spc_timer = info->i_spc_timelimit;
	newlim.d_ino_timer = info->i_ino_timelimit;
	newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
	newlim.d_ino_warns = info->i_ino_warnlimit;
	newlim.d_spc_warns = info->i_spc_warnlimit;
	newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;

	return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
}

137 138
static unsigned int
xfs_quota_flags(unsigned int uflags)
139
{
140
	unsigned int flags = 0;
141

142
	if (uflags & FS_QUOTA_UDQ_ACCT)
143
		flags |= XFS_UQUOTA_ACCT;
144
	if (uflags & FS_QUOTA_PDQ_ACCT)
145
		flags |= XFS_PQUOTA_ACCT;
146
	if (uflags & FS_QUOTA_GDQ_ACCT)
147
		flags |= XFS_GQUOTA_ACCT;
148
	if (uflags & FS_QUOTA_UDQ_ENFD)
149
		flags |= XFS_UQUOTA_ENFD;
150 151 152 153
	if (uflags & FS_QUOTA_GDQ_ENFD)
		flags |= XFS_GQUOTA_ENFD;
	if (uflags & FS_QUOTA_PDQ_ENFD)
		flags |= XFS_PQUOTA_ENFD;
154

155 156 157 158 159 160 161 162 163 164
	return flags;
}

STATIC int
xfs_quota_enable(
	struct super_block	*sb,
	unsigned int		uflags)
{
	struct xfs_mount	*mp = XFS_M(sb);

165
	if (sb_rdonly(sb))
166 167 168 169 170 171 172 173 174 175 176 177 178 179
		return -EROFS;
	if (!XFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;

	return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
}

STATIC int
xfs_quota_disable(
	struct super_block	*sb,
	unsigned int		uflags)
{
	struct xfs_mount	*mp = XFS_M(sb);

180
	if (sb_rdonly(sb))
181 182 183 184 185
		return -EROFS;
	if (!XFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!XFS_IS_QUOTA_ON(mp))
		return -EINVAL;
186

187
	return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
188 189
}

E
Eric Sandeen 已提交
190 191 192 193 194 195 196
STATIC int
xfs_fs_rm_xquota(
	struct super_block	*sb,
	unsigned int		uflags)
{
	struct xfs_mount	*mp = XFS_M(sb);
	unsigned int		flags = 0;
D
Dave Chinner 已提交
197

198
	if (sb_rdonly(sb))
E
Eric Sandeen 已提交
199 200 201 202 203
		return -EROFS;

	if (XFS_IS_QUOTA_ON(mp))
		return -EINVAL;

204 205 206
	if (uflags & ~(FS_USER_QUOTA | FS_GROUP_QUOTA | FS_PROJ_QUOTA))
		return -EINVAL;

E
Eric Sandeen 已提交
207 208 209 210
	if (uflags & FS_USER_QUOTA)
		flags |= XFS_DQ_USER;
	if (uflags & FS_GROUP_QUOTA)
		flags |= XFS_DQ_GROUP;
211
	if (uflags & FS_PROJ_QUOTA)
E
Eric Sandeen 已提交
212 213
		flags |= XFS_DQ_PROJ;

D
Dave Chinner 已提交
214 215
	return xfs_qm_scall_trunc_qfiles(mp, flags);
}
E
Eric Sandeen 已提交
216

217
STATIC int
C
Christoph Hellwig 已提交
218
xfs_fs_get_dqblk(
219
	struct super_block	*sb,
E
Eric W. Biederman 已提交
220
	struct kqid		qid,
221
	struct qc_dqblk		*qdq)
222 223
{
	struct xfs_mount	*mp = XFS_M(sb);
224
	xfs_dqid_t		id;
225 226 227 228 229 230

	if (!XFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!XFS_IS_QUOTA_ON(mp))
		return -ESRCH;

231
	id = from_kqid(&init_user_ns, qid);
232
	return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

/* Return quota info for active quota >= this qid */
STATIC int
xfs_fs_get_nextdqblk(
	struct super_block	*sb,
	struct kqid		*qid,
	struct qc_dqblk		*qdq)
{
	int			ret;
	struct xfs_mount	*mp = XFS_M(sb);
	xfs_dqid_t		id;

	if (!XFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!XFS_IS_QUOTA_ON(mp))
		return -ESRCH;

	id = from_kqid(&init_user_ns, *qid);
252 253
	ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
			qdq);
254 255 256 257 258 259
	if (ret)
		return ret;

	/* ID may be different, so convert back what we got */
	*qid = make_kqid(current_user_ns(), qid->type, id);
	return 0;
260 261 262
}

STATIC int
C
Christoph Hellwig 已提交
263
xfs_fs_set_dqblk(
264
	struct super_block	*sb,
E
Eric W. Biederman 已提交
265
	struct kqid		qid,
266
	struct qc_dqblk		*qdq)
267 268 269
{
	struct xfs_mount	*mp = XFS_M(sb);

270
	if (sb_rdonly(sb))
271 272 273 274 275 276
		return -EROFS;
	if (!XFS_IS_QUOTA_RUNNING(mp))
		return -ENOSYS;
	if (!XFS_IS_QUOTA_ON(mp))
		return -ESRCH;

D
Dave Chinner 已提交
277
	return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
278
				     xfs_quota_type(qid.type), qdq);
279 280
}

281
const struct quotactl_ops xfs_quotactl_operations = {
282
	.get_state		= xfs_fs_get_quota_state,
J
Jan Kara 已提交
283
	.set_info		= xfs_fs_set_info,
284 285
	.quota_enable		= xfs_quota_enable,
	.quota_disable		= xfs_quota_disable,
E
Eric Sandeen 已提交
286
	.rm_xquota		= xfs_fs_rm_xquota,
C
Christoph Hellwig 已提交
287
	.get_dqblk		= xfs_fs_get_dqblk,
288
	.get_nextdqblk		= xfs_fs_get_nextdqblk,
C
Christoph Hellwig 已提交
289
	.set_dqblk		= xfs_fs_set_dqblk,
290
};