xref: /openbmc/linux/fs/quota/quota.c (revision 3218a3ec87f7d0063b48b10cf3d649ce75966223)
1884d179dSJan Kara /*
2884d179dSJan Kara  * Quota code necessary even when VFS quota support is not compiled
3884d179dSJan Kara  * into the kernel.  The interesting stuff is over in dquot.c, here
4884d179dSJan Kara  * we have symbols for initial quotactl(2) handling, the sysctl(2)
5884d179dSJan Kara  * variables, etc - things needed even when quota support disabled.
6884d179dSJan Kara  */
7884d179dSJan Kara 
8884d179dSJan Kara #include <linux/fs.h>
9884d179dSJan Kara #include <linux/namei.h>
10884d179dSJan Kara #include <linux/slab.h>
11884d179dSJan Kara #include <asm/current.h>
12f3da9310SJeff Liu #include <linux/uaccess.h>
13884d179dSJan Kara #include <linux/kernel.h>
14884d179dSJan Kara #include <linux/security.h>
15884d179dSJan Kara #include <linux/syscalls.h>
16884d179dSJan Kara #include <linux/capability.h>
17884d179dSJan Kara #include <linux/quotaops.h>
18884d179dSJan Kara #include <linux/types.h>
198c4e4acdSChristoph Hellwig #include <linux/writeback.h>
20884d179dSJan Kara 
21c988afb5SChristoph Hellwig static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
22268157baSJan Kara 				     qid_t id)
23884d179dSJan Kara {
24c988afb5SChristoph Hellwig 	switch (cmd) {
25c988afb5SChristoph Hellwig 	/* these commands do not require any special privilegues */
26c988afb5SChristoph Hellwig 	case Q_GETFMT:
27c988afb5SChristoph Hellwig 	case Q_SYNC:
28c988afb5SChristoph Hellwig 	case Q_GETINFO:
29c988afb5SChristoph Hellwig 	case Q_XGETQSTAT:
30af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
31c988afb5SChristoph Hellwig 	case Q_XQUOTASYNC:
32c988afb5SChristoph Hellwig 		break;
33c988afb5SChristoph Hellwig 	/* allow to query information for dquots we "own" */
34c988afb5SChristoph Hellwig 	case Q_GETQUOTA:
35c988afb5SChristoph Hellwig 	case Q_XGETQUOTA:
3674a8a103SEric W. Biederman 		if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
3774a8a103SEric W. Biederman 		    (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
38c988afb5SChristoph Hellwig 			break;
39c988afb5SChristoph Hellwig 		/*FALLTHROUGH*/
40c988afb5SChristoph Hellwig 	default:
41884d179dSJan Kara 		if (!capable(CAP_SYS_ADMIN))
42884d179dSJan Kara 			return -EPERM;
43884d179dSJan Kara 	}
44884d179dSJan Kara 
45c988afb5SChristoph Hellwig 	return security_quotactl(cmd, type, id, sb);
46884d179dSJan Kara }
47884d179dSJan Kara 
4801a05b33SAl Viro static void quota_sync_one(struct super_block *sb, void *arg)
4901a05b33SAl Viro {
502c5f648aSJan Kara 	int type = *(int *)arg;
512c5f648aSJan Kara 
522c5f648aSJan Kara 	if (sb->s_qcop && sb->s_qcop->quota_sync &&
532c5f648aSJan Kara 	    (sb->s_quota_types & (1 << type)))
542c5f648aSJan Kara 		sb->s_qcop->quota_sync(sb, type);
5501a05b33SAl Viro }
5601a05b33SAl Viro 
576ae09575SChristoph Hellwig static int quota_sync_all(int type)
58884d179dSJan Kara {
596ae09575SChristoph Hellwig 	int ret;
606ae09575SChristoph Hellwig 
616ae09575SChristoph Hellwig 	if (type >= MAXQUOTAS)
626ae09575SChristoph Hellwig 		return -EINVAL;
636ae09575SChristoph Hellwig 	ret = security_quotactl(Q_SYNC, type, 0, NULL);
6401a05b33SAl Viro 	if (!ret)
6501a05b33SAl Viro 		iterate_supers(quota_sync_one, &type);
666ae09575SChristoph Hellwig 	return ret;
67884d179dSJan Kara }
68884d179dSJan Kara 
69d3b86324SJan Kara unsigned int qtype_enforce_flag(int type)
70d3b86324SJan Kara {
71d3b86324SJan Kara 	switch (type) {
72d3b86324SJan Kara 	case USRQUOTA:
73d3b86324SJan Kara 		return FS_QUOTA_UDQ_ENFD;
74d3b86324SJan Kara 	case GRPQUOTA:
75d3b86324SJan Kara 		return FS_QUOTA_GDQ_ENFD;
76d3b86324SJan Kara 	case PRJQUOTA:
77d3b86324SJan Kara 		return FS_QUOTA_PDQ_ENFD;
78d3b86324SJan Kara 	}
79d3b86324SJan Kara 	return 0;
80d3b86324SJan Kara }
81d3b86324SJan Kara 
82*3218a3ecSEric Sandeen static int quota_quotaon(struct super_block *sb, int type, qid_t id,
83f00c9e44SJan Kara 		         struct path *path)
84884d179dSJan Kara {
85aaa3daedSJan Kara 	if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
86f00c9e44SJan Kara 		return -ENOSYS;
87d3b86324SJan Kara 	if (sb->s_qcop->quota_enable)
88d3b86324SJan Kara 		return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
89f00c9e44SJan Kara 	if (IS_ERR(path))
90f00c9e44SJan Kara 		return PTR_ERR(path);
91f00c9e44SJan Kara 	return sb->s_qcop->quota_on(sb, type, id, path);
92884d179dSJan Kara }
93884d179dSJan Kara 
94d3b86324SJan Kara static int quota_quotaoff(struct super_block *sb, int type)
95d3b86324SJan Kara {
96d3b86324SJan Kara 	if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
97d3b86324SJan Kara 		return -ENOSYS;
98d3b86324SJan Kara 	if (sb->s_qcop->quota_disable)
99d3b86324SJan Kara 		return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type));
100d3b86324SJan Kara 	return sb->s_qcop->quota_off(sb, type);
101d3b86324SJan Kara }
102d3b86324SJan Kara 
103c411e5f6SChristoph Hellwig static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
104c411e5f6SChristoph Hellwig {
105884d179dSJan Kara 	__u32 fmt;
106884d179dSJan Kara 
107606cdccaSNiu Yawei 	mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
108884d179dSJan Kara 	if (!sb_has_quota_active(sb, type)) {
109606cdccaSNiu Yawei 		mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
110884d179dSJan Kara 		return -ESRCH;
111884d179dSJan Kara 	}
112884d179dSJan Kara 	fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
113606cdccaSNiu Yawei 	mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
114884d179dSJan Kara 	if (copy_to_user(addr, &fmt, sizeof(fmt)))
115884d179dSJan Kara 		return -EFAULT;
116884d179dSJan Kara 	return 0;
117884d179dSJan Kara }
118c411e5f6SChristoph Hellwig 
119c411e5f6SChristoph Hellwig static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
120c411e5f6SChristoph Hellwig {
1210a240339SJan Kara 	struct qc_state state;
1220a240339SJan Kara 	struct qc_type_state *tstate;
1230a240339SJan Kara 	struct if_dqinfo uinfo;
124c411e5f6SChristoph Hellwig 	int ret;
125884d179dSJan Kara 
1260a240339SJan Kara 	/* This checks whether qc_state has enough entries... */
1270a240339SJan Kara 	BUILD_BUG_ON(MAXQUOTAS > XQM_MAXQUOTAS);
1280a240339SJan Kara 	if (!sb->s_qcop->get_state)
129f450d4feSChristoph Hellwig 		return -ENOSYS;
1300a240339SJan Kara 	ret = sb->s_qcop->get_state(sb, &state);
1310a240339SJan Kara 	if (ret)
1320a240339SJan Kara 		return ret;
1330a240339SJan Kara 	tstate = state.s_state + type;
1340a240339SJan Kara 	if (!(tstate->flags & QCI_ACCT_ENABLED))
1350a240339SJan Kara 		return -ESRCH;
1360a240339SJan Kara 	memset(&uinfo, 0, sizeof(uinfo));
1370a240339SJan Kara 	uinfo.dqi_bgrace = tstate->spc_timelimit;
1380a240339SJan Kara 	uinfo.dqi_igrace = tstate->ino_timelimit;
1390a240339SJan Kara 	if (tstate->flags & QCI_SYSFILE)
1400a240339SJan Kara 		uinfo.dqi_flags |= DQF_SYS_FILE;
1410a240339SJan Kara 	if (tstate->flags & QCI_ROOT_SQUASH)
1420a240339SJan Kara 		uinfo.dqi_flags |= DQF_ROOT_SQUASH;
1430a240339SJan Kara 	uinfo.dqi_valid = IIF_ALL;
14472d4d0e4SDan Carpenter 	if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
145884d179dSJan Kara 		return -EFAULT;
14672d4d0e4SDan Carpenter 	return 0;
147884d179dSJan Kara }
148c411e5f6SChristoph Hellwig 
149c411e5f6SChristoph Hellwig static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
150c411e5f6SChristoph Hellwig {
151884d179dSJan Kara 	struct if_dqinfo info;
1525eacb2acSJan Kara 	struct qc_info qinfo;
153884d179dSJan Kara 
154884d179dSJan Kara 	if (copy_from_user(&info, addr, sizeof(info)))
155884d179dSJan Kara 		return -EFAULT;
156f450d4feSChristoph Hellwig 	if (!sb->s_qcop->set_info)
157f450d4feSChristoph Hellwig 		return -ENOSYS;
1585eacb2acSJan Kara 	if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
1595eacb2acSJan Kara 		return -EINVAL;
1605eacb2acSJan Kara 	memset(&qinfo, 0, sizeof(qinfo));
1615eacb2acSJan Kara 	if (info.dqi_valid & IIF_FLAGS) {
1625eacb2acSJan Kara 		if (info.dqi_flags & ~DQF_SETINFO_MASK)
1635eacb2acSJan Kara 			return -EINVAL;
1645eacb2acSJan Kara 		if (info.dqi_flags & DQF_ROOT_SQUASH)
1655eacb2acSJan Kara 			qinfo.i_flags |= QCI_ROOT_SQUASH;
1665eacb2acSJan Kara 		qinfo.i_fieldmask |= QC_FLAGS;
1675eacb2acSJan Kara 	}
1685eacb2acSJan Kara 	if (info.dqi_valid & IIF_BGRACE) {
1695eacb2acSJan Kara 		qinfo.i_spc_timelimit = info.dqi_bgrace;
1705eacb2acSJan Kara 		qinfo.i_fieldmask |= QC_SPC_TIMER;
1715eacb2acSJan Kara 	}
1725eacb2acSJan Kara 	if (info.dqi_valid & IIF_IGRACE) {
1735eacb2acSJan Kara 		qinfo.i_ino_timelimit = info.dqi_igrace;
1745eacb2acSJan Kara 		qinfo.i_fieldmask |= QC_INO_TIMER;
1755eacb2acSJan Kara 	}
1765eacb2acSJan Kara 	return sb->s_qcop->set_info(sb, type, &qinfo);
177884d179dSJan Kara }
178c411e5f6SChristoph Hellwig 
17914bf61ffSJan Kara static inline qsize_t qbtos(qsize_t blocks)
18014bf61ffSJan Kara {
18114bf61ffSJan Kara 	return blocks << QIF_DQBLKSIZE_BITS;
18214bf61ffSJan Kara }
18314bf61ffSJan Kara 
18414bf61ffSJan Kara static inline qsize_t stoqb(qsize_t space)
18514bf61ffSJan Kara {
18614bf61ffSJan Kara 	return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
18714bf61ffSJan Kara }
18814bf61ffSJan Kara 
18914bf61ffSJan Kara static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
190b9b2dd36SChristoph Hellwig {
19118da65e7SDan Carpenter 	memset(dst, 0, sizeof(*dst));
19214bf61ffSJan Kara 	dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
19314bf61ffSJan Kara 	dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
19414bf61ffSJan Kara 	dst->dqb_curspace = src->d_space;
195b9b2dd36SChristoph Hellwig 	dst->dqb_ihardlimit = src->d_ino_hardlimit;
196b9b2dd36SChristoph Hellwig 	dst->dqb_isoftlimit = src->d_ino_softlimit;
19714bf61ffSJan Kara 	dst->dqb_curinodes = src->d_ino_count;
19814bf61ffSJan Kara 	dst->dqb_btime = src->d_spc_timer;
19914bf61ffSJan Kara 	dst->dqb_itime = src->d_ino_timer;
200b9b2dd36SChristoph Hellwig 	dst->dqb_valid = QIF_ALL;
201b9b2dd36SChristoph Hellwig }
202b9b2dd36SChristoph Hellwig 
203c411e5f6SChristoph Hellwig static int quota_getquota(struct super_block *sb, int type, qid_t id,
204c411e5f6SChristoph Hellwig 			  void __user *addr)
205c411e5f6SChristoph Hellwig {
20674a8a103SEric W. Biederman 	struct kqid qid;
20714bf61ffSJan Kara 	struct qc_dqblk fdq;
208884d179dSJan Kara 	struct if_dqblk idq;
209c411e5f6SChristoph Hellwig 	int ret;
210884d179dSJan Kara 
211f450d4feSChristoph Hellwig 	if (!sb->s_qcop->get_dqblk)
212f450d4feSChristoph Hellwig 		return -ENOSYS;
21374a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
21474a8a103SEric W. Biederman 	if (!qid_valid(qid))
21574a8a103SEric W. Biederman 		return -EINVAL;
21674a8a103SEric W. Biederman 	ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
217268157baSJan Kara 	if (ret)
218884d179dSJan Kara 		return ret;
219b9b2dd36SChristoph Hellwig 	copy_to_if_dqblk(&idq, &fdq);
220884d179dSJan Kara 	if (copy_to_user(addr, &idq, sizeof(idq)))
221884d179dSJan Kara 		return -EFAULT;
222884d179dSJan Kara 	return 0;
223884d179dSJan Kara }
224c411e5f6SChristoph Hellwig 
22514bf61ffSJan Kara static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
226c472b432SChristoph Hellwig {
22714bf61ffSJan Kara 	dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
22814bf61ffSJan Kara 	dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
22914bf61ffSJan Kara 	dst->d_space = src->dqb_curspace;
230c472b432SChristoph Hellwig 	dst->d_ino_hardlimit = src->dqb_ihardlimit;
231c472b432SChristoph Hellwig 	dst->d_ino_softlimit = src->dqb_isoftlimit;
23214bf61ffSJan Kara 	dst->d_ino_count = src->dqb_curinodes;
23314bf61ffSJan Kara 	dst->d_spc_timer = src->dqb_btime;
23414bf61ffSJan Kara 	dst->d_ino_timer = src->dqb_itime;
235c472b432SChristoph Hellwig 
236c472b432SChristoph Hellwig 	dst->d_fieldmask = 0;
237c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_BLIMITS)
23814bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
239c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_SPACE)
24014bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPACE;
241c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_ILIMITS)
24214bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
243c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_INODES)
24414bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_COUNT;
245c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_BTIME)
24614bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_TIMER;
247c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_ITIME)
24814bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_TIMER;
249c472b432SChristoph Hellwig }
250c472b432SChristoph Hellwig 
251c411e5f6SChristoph Hellwig static int quota_setquota(struct super_block *sb, int type, qid_t id,
252c411e5f6SChristoph Hellwig 			  void __user *addr)
253c411e5f6SChristoph Hellwig {
25414bf61ffSJan Kara 	struct qc_dqblk fdq;
255884d179dSJan Kara 	struct if_dqblk idq;
25674a8a103SEric W. Biederman 	struct kqid qid;
257884d179dSJan Kara 
258884d179dSJan Kara 	if (copy_from_user(&idq, addr, sizeof(idq)))
259884d179dSJan Kara 		return -EFAULT;
260f450d4feSChristoph Hellwig 	if (!sb->s_qcop->set_dqblk)
261f450d4feSChristoph Hellwig 		return -ENOSYS;
26274a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
26374a8a103SEric W. Biederman 	if (!qid_valid(qid))
26474a8a103SEric W. Biederman 		return -EINVAL;
265c472b432SChristoph Hellwig 	copy_from_if_dqblk(&fdq, &idq);
26674a8a103SEric W. Biederman 	return sb->s_qcop->set_dqblk(sb, qid, &fdq);
267884d179dSJan Kara }
268884d179dSJan Kara 
26938e478c4SJan Kara static int quota_enable(struct super_block *sb, void __user *addr)
270c411e5f6SChristoph Hellwig {
271884d179dSJan Kara 	__u32 flags;
272884d179dSJan Kara 
273884d179dSJan Kara 	if (copy_from_user(&flags, addr, sizeof(flags)))
274884d179dSJan Kara 		return -EFAULT;
27538e478c4SJan Kara 	if (!sb->s_qcop->quota_enable)
276f450d4feSChristoph Hellwig 		return -ENOSYS;
27738e478c4SJan Kara 	return sb->s_qcop->quota_enable(sb, flags);
27838e478c4SJan Kara }
27938e478c4SJan Kara 
28038e478c4SJan Kara static int quota_disable(struct super_block *sb, void __user *addr)
28138e478c4SJan Kara {
28238e478c4SJan Kara 	__u32 flags;
28338e478c4SJan Kara 
28438e478c4SJan Kara 	if (copy_from_user(&flags, addr, sizeof(flags)))
28538e478c4SJan Kara 		return -EFAULT;
28638e478c4SJan Kara 	if (!sb->s_qcop->quota_disable)
28738e478c4SJan Kara 		return -ENOSYS;
28838e478c4SJan Kara 	return sb->s_qcop->quota_disable(sb, flags);
289884d179dSJan Kara }
290884d179dSJan Kara 
291bc230e4aSJan Kara static int quota_state_to_flags(struct qc_state *state)
292bc230e4aSJan Kara {
293bc230e4aSJan Kara 	int flags = 0;
294bc230e4aSJan Kara 
295bc230e4aSJan Kara 	if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
296bc230e4aSJan Kara 		flags |= FS_QUOTA_UDQ_ACCT;
297bc230e4aSJan Kara 	if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
298bc230e4aSJan Kara 		flags |= FS_QUOTA_UDQ_ENFD;
299bc230e4aSJan Kara 	if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
300bc230e4aSJan Kara 		flags |= FS_QUOTA_GDQ_ACCT;
301bc230e4aSJan Kara 	if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
302bc230e4aSJan Kara 		flags |= FS_QUOTA_GDQ_ENFD;
303bc230e4aSJan Kara 	if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
304bc230e4aSJan Kara 		flags |= FS_QUOTA_PDQ_ACCT;
305bc230e4aSJan Kara 	if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
306bc230e4aSJan Kara 		flags |= FS_QUOTA_PDQ_ENFD;
307bc230e4aSJan Kara 	return flags;
308bc230e4aSJan Kara }
309bc230e4aSJan Kara 
310bc230e4aSJan Kara static int quota_getstate(struct super_block *sb, struct fs_quota_stat *fqs)
311bc230e4aSJan Kara {
312bc230e4aSJan Kara 	int type;
313bc230e4aSJan Kara 	struct qc_state state;
314bc230e4aSJan Kara 	int ret;
315bc230e4aSJan Kara 
316bc230e4aSJan Kara 	ret = sb->s_qcop->get_state(sb, &state);
317bc230e4aSJan Kara 	if (ret < 0)
318bc230e4aSJan Kara 		return ret;
319bc230e4aSJan Kara 
320bc230e4aSJan Kara 	memset(fqs, 0, sizeof(*fqs));
321bc230e4aSJan Kara 	fqs->qs_version = FS_QSTAT_VERSION;
322bc230e4aSJan Kara 	fqs->qs_flags = quota_state_to_flags(&state);
323bc230e4aSJan Kara 	/* No quota enabled? */
324bc230e4aSJan Kara 	if (!fqs->qs_flags)
325bc230e4aSJan Kara 		return -ENOSYS;
326bc230e4aSJan Kara 	fqs->qs_incoredqs = state.s_incoredqs;
327bc230e4aSJan Kara 	/*
328bc230e4aSJan Kara 	 * GETXSTATE quotactl has space for just one set of time limits so
329bc230e4aSJan Kara 	 * report them for the first enabled quota type
330bc230e4aSJan Kara 	 */
331bc230e4aSJan Kara 	for (type = 0; type < XQM_MAXQUOTAS; type++)
332bc230e4aSJan Kara 		if (state.s_state[type].flags & QCI_ACCT_ENABLED)
333bc230e4aSJan Kara 			break;
334bc230e4aSJan Kara 	BUG_ON(type == XQM_MAXQUOTAS);
335bc230e4aSJan Kara 	fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
336bc230e4aSJan Kara 	fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
337bc230e4aSJan Kara 	fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
338bc230e4aSJan Kara 	fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
339bc230e4aSJan Kara 	fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
340bc230e4aSJan Kara 	if (state.s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) {
341bc230e4aSJan Kara 		fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
342bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
343bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
344bc230e4aSJan Kara 	}
345bc230e4aSJan Kara 	if (state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) {
346bc230e4aSJan Kara 		fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
347bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
348bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
349bc230e4aSJan Kara 	}
350bc230e4aSJan Kara 	if (state.s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) {
351bc230e4aSJan Kara 		/*
352bc230e4aSJan Kara 		 * Q_XGETQSTAT doesn't have room for both group and project
353bc230e4aSJan Kara 		 * quotas.  So, allow the project quota values to be copied out
354bc230e4aSJan Kara 		 * only if there is no group quota information available.
355bc230e4aSJan Kara 		 */
356bc230e4aSJan Kara 		if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
357bc230e4aSJan Kara 			fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
358bc230e4aSJan Kara 			fqs->qs_gquota.qfs_nblks =
359bc230e4aSJan Kara 					state.s_state[PRJQUOTA].blocks;
360bc230e4aSJan Kara 			fqs->qs_gquota.qfs_nextents =
361bc230e4aSJan Kara 					state.s_state[PRJQUOTA].nextents;
362bc230e4aSJan Kara 		}
363bc230e4aSJan Kara 	}
364bc230e4aSJan Kara 	return 0;
365bc230e4aSJan Kara }
366bc230e4aSJan Kara 
367c411e5f6SChristoph Hellwig static int quota_getxstate(struct super_block *sb, void __user *addr)
368c411e5f6SChristoph Hellwig {
369c411e5f6SChristoph Hellwig 	struct fs_quota_stat fqs;
370c411e5f6SChristoph Hellwig 	int ret;
371c411e5f6SChristoph Hellwig 
37259b6ba69SJan Kara 	if (!sb->s_qcop->get_state)
373f450d4feSChristoph Hellwig 		return -ENOSYS;
374bc230e4aSJan Kara 	ret = quota_getstate(sb, &fqs);
375c411e5f6SChristoph Hellwig 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
376884d179dSJan Kara 		return -EFAULT;
377c411e5f6SChristoph Hellwig 	return ret;
378884d179dSJan Kara }
379c411e5f6SChristoph Hellwig 
380bc230e4aSJan Kara static int quota_getstatev(struct super_block *sb, struct fs_quota_statv *fqs)
381bc230e4aSJan Kara {
382bc230e4aSJan Kara 	int type;
383bc230e4aSJan Kara 	struct qc_state state;
384bc230e4aSJan Kara 	int ret;
385bc230e4aSJan Kara 
386bc230e4aSJan Kara 	ret = sb->s_qcop->get_state(sb, &state);
387bc230e4aSJan Kara 	if (ret < 0)
388bc230e4aSJan Kara 		return ret;
389bc230e4aSJan Kara 
390bc230e4aSJan Kara 	memset(fqs, 0, sizeof(*fqs));
391bc230e4aSJan Kara 	fqs->qs_version = FS_QSTAT_VERSION;
392bc230e4aSJan Kara 	fqs->qs_flags = quota_state_to_flags(&state);
393bc230e4aSJan Kara 	/* No quota enabled? */
394bc230e4aSJan Kara 	if (!fqs->qs_flags)
395bc230e4aSJan Kara 		return -ENOSYS;
396bc230e4aSJan Kara 	fqs->qs_incoredqs = state.s_incoredqs;
397bc230e4aSJan Kara 	/*
398bc230e4aSJan Kara 	 * GETXSTATV quotactl has space for just one set of time limits so
399bc230e4aSJan Kara 	 * report them for the first enabled quota type
400bc230e4aSJan Kara 	 */
401bc230e4aSJan Kara 	for (type = 0; type < XQM_MAXQUOTAS; type++)
402bc230e4aSJan Kara 		if (state.s_state[type].flags & QCI_ACCT_ENABLED)
403bc230e4aSJan Kara 			break;
404bc230e4aSJan Kara 	BUG_ON(type == XQM_MAXQUOTAS);
405bc230e4aSJan Kara 	fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
406bc230e4aSJan Kara 	fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
407bc230e4aSJan Kara 	fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
408bc230e4aSJan Kara 	fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
409bc230e4aSJan Kara 	fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
410bc230e4aSJan Kara 	if (state.s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) {
411bc230e4aSJan Kara 		fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
412bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
413bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
414bc230e4aSJan Kara 	}
415bc230e4aSJan Kara 	if (state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) {
416bc230e4aSJan Kara 		fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
417bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
418bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
419bc230e4aSJan Kara 	}
420bc230e4aSJan Kara 	if (state.s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) {
421bc230e4aSJan Kara 		fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
422bc230e4aSJan Kara 		fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
423bc230e4aSJan Kara 		fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
424bc230e4aSJan Kara 	}
425bc230e4aSJan Kara 	return 0;
426bc230e4aSJan Kara }
427bc230e4aSJan Kara 
428af30cb44SChandra Seetharaman static int quota_getxstatev(struct super_block *sb, void __user *addr)
429af30cb44SChandra Seetharaman {
430af30cb44SChandra Seetharaman 	struct fs_quota_statv fqs;
431af30cb44SChandra Seetharaman 	int ret;
432af30cb44SChandra Seetharaman 
43359b6ba69SJan Kara 	if (!sb->s_qcop->get_state)
434af30cb44SChandra Seetharaman 		return -ENOSYS;
435af30cb44SChandra Seetharaman 
436af30cb44SChandra Seetharaman 	memset(&fqs, 0, sizeof(fqs));
437af30cb44SChandra Seetharaman 	if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
438af30cb44SChandra Seetharaman 		return -EFAULT;
439af30cb44SChandra Seetharaman 
440af30cb44SChandra Seetharaman 	/* If this kernel doesn't support user specified version, fail */
441af30cb44SChandra Seetharaman 	switch (fqs.qs_version) {
442af30cb44SChandra Seetharaman 	case FS_QSTATV_VERSION1:
443af30cb44SChandra Seetharaman 		break;
444af30cb44SChandra Seetharaman 	default:
445af30cb44SChandra Seetharaman 		return -EINVAL;
446af30cb44SChandra Seetharaman 	}
447bc230e4aSJan Kara 	ret = quota_getstatev(sb, &fqs);
448af30cb44SChandra Seetharaman 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
449af30cb44SChandra Seetharaman 		return -EFAULT;
450af30cb44SChandra Seetharaman 	return ret;
451af30cb44SChandra Seetharaman }
452af30cb44SChandra Seetharaman 
45314bf61ffSJan Kara /*
45414bf61ffSJan Kara  * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
45514bf61ffSJan Kara  * out of there as xfsprogs rely on definitions being in that header file. So
45614bf61ffSJan Kara  * just define same functions here for quota purposes.
45714bf61ffSJan Kara  */
45814bf61ffSJan Kara #define XFS_BB_SHIFT 9
45914bf61ffSJan Kara 
46014bf61ffSJan Kara static inline u64 quota_bbtob(u64 blocks)
46114bf61ffSJan Kara {
46214bf61ffSJan Kara 	return blocks << XFS_BB_SHIFT;
46314bf61ffSJan Kara }
46414bf61ffSJan Kara 
46514bf61ffSJan Kara static inline u64 quota_btobb(u64 bytes)
46614bf61ffSJan Kara {
46714bf61ffSJan Kara 	return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
46814bf61ffSJan Kara }
46914bf61ffSJan Kara 
47014bf61ffSJan Kara static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
47114bf61ffSJan Kara {
47214bf61ffSJan Kara 	dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
47314bf61ffSJan Kara 	dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
47414bf61ffSJan Kara 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
47514bf61ffSJan Kara 	dst->d_ino_softlimit = src->d_ino_softlimit;
47614bf61ffSJan Kara 	dst->d_space = quota_bbtob(src->d_bcount);
47714bf61ffSJan Kara 	dst->d_ino_count = src->d_icount;
47814bf61ffSJan Kara 	dst->d_ino_timer = src->d_itimer;
47914bf61ffSJan Kara 	dst->d_spc_timer = src->d_btimer;
48014bf61ffSJan Kara 	dst->d_ino_warns = src->d_iwarns;
48114bf61ffSJan Kara 	dst->d_spc_warns = src->d_bwarns;
48214bf61ffSJan Kara 	dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
48314bf61ffSJan Kara 	dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
48414bf61ffSJan Kara 	dst->d_rt_space = quota_bbtob(src->d_rtbcount);
48514bf61ffSJan Kara 	dst->d_rt_spc_timer = src->d_rtbtimer;
48614bf61ffSJan Kara 	dst->d_rt_spc_warns = src->d_rtbwarns;
48714bf61ffSJan Kara 	dst->d_fieldmask = 0;
48814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ISOFT)
48914bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_SOFT;
49014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_IHARD)
49114bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_HARD;
49214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BSOFT)
49314bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_SOFT;
49414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BHARD)
49514bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_HARD;
49614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBSOFT)
49714bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_SOFT;
49814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBHARD)
49914bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_HARD;
50014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BTIMER)
50114bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_TIMER;
50214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ITIMER)
50314bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_TIMER;
50414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
50514bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_TIMER;
50614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BWARNS)
50714bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_WARNS;
50814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_IWARNS)
50914bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_WARNS;
51014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
51114bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_WARNS;
51214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BCOUNT)
51314bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPACE;
51414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ICOUNT)
51514bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_COUNT;
51614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBCOUNT)
51714bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPACE;
51814bf61ffSJan Kara }
51914bf61ffSJan Kara 
520c39fb53bSJan Kara static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
521c39fb53bSJan Kara 				       struct fs_disk_quota *src)
522c39fb53bSJan Kara {
523c39fb53bSJan Kara 	memset(dst, 0, sizeof(*dst));
524c39fb53bSJan Kara 	dst->i_spc_timelimit = src->d_btimer;
525c39fb53bSJan Kara 	dst->i_ino_timelimit = src->d_itimer;
526c39fb53bSJan Kara 	dst->i_rt_spc_timelimit = src->d_rtbtimer;
527c39fb53bSJan Kara 	dst->i_ino_warnlimit = src->d_iwarns;
528c39fb53bSJan Kara 	dst->i_spc_warnlimit = src->d_bwarns;
529c39fb53bSJan Kara 	dst->i_rt_spc_warnlimit = src->d_rtbwarns;
530c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_BWARNS)
531c39fb53bSJan Kara 		dst->i_fieldmask |= QC_SPC_WARNS;
532c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_IWARNS)
533c39fb53bSJan Kara 		dst->i_fieldmask |= QC_INO_WARNS;
534c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
535c39fb53bSJan Kara 		dst->i_fieldmask |= QC_RT_SPC_WARNS;
536c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_BTIMER)
537c39fb53bSJan Kara 		dst->i_fieldmask |= QC_SPC_TIMER;
538c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_ITIMER)
539c39fb53bSJan Kara 		dst->i_fieldmask |= QC_INO_TIMER;
540c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
541c39fb53bSJan Kara 		dst->i_fieldmask |= QC_RT_SPC_TIMER;
542c39fb53bSJan Kara }
543c39fb53bSJan Kara 
544c411e5f6SChristoph Hellwig static int quota_setxquota(struct super_block *sb, int type, qid_t id,
545c411e5f6SChristoph Hellwig 			   void __user *addr)
546c411e5f6SChristoph Hellwig {
547884d179dSJan Kara 	struct fs_disk_quota fdq;
54814bf61ffSJan Kara 	struct qc_dqblk qdq;
54974a8a103SEric W. Biederman 	struct kqid qid;
550884d179dSJan Kara 
551884d179dSJan Kara 	if (copy_from_user(&fdq, addr, sizeof(fdq)))
552884d179dSJan Kara 		return -EFAULT;
553c472b432SChristoph Hellwig 	if (!sb->s_qcop->set_dqblk)
554f450d4feSChristoph Hellwig 		return -ENOSYS;
55574a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
55674a8a103SEric W. Biederman 	if (!qid_valid(qid))
55774a8a103SEric W. Biederman 		return -EINVAL;
558c39fb53bSJan Kara 	/* Are we actually setting timer / warning limits for all users? */
559c39fb53bSJan Kara 	if (from_kqid(&init_user_ns, qid) == 0 &&
560c39fb53bSJan Kara 	    fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
561c39fb53bSJan Kara 		struct qc_info qinfo;
562c39fb53bSJan Kara 		int ret;
563c39fb53bSJan Kara 
564c39fb53bSJan Kara 		if (!sb->s_qcop->set_info)
565c39fb53bSJan Kara 			return -EINVAL;
566c39fb53bSJan Kara 		copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
567c39fb53bSJan Kara 		ret = sb->s_qcop->set_info(sb, type, &qinfo);
568c39fb53bSJan Kara 		if (ret)
569c39fb53bSJan Kara 			return ret;
570c39fb53bSJan Kara 		/* These are already done */
571c39fb53bSJan Kara 		fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
572c39fb53bSJan Kara 	}
57314bf61ffSJan Kara 	copy_from_xfs_dqblk(&qdq, &fdq);
57414bf61ffSJan Kara 	return sb->s_qcop->set_dqblk(sb, qid, &qdq);
57514bf61ffSJan Kara }
57614bf61ffSJan Kara 
57714bf61ffSJan Kara static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
57814bf61ffSJan Kara 			      int type, qid_t id)
57914bf61ffSJan Kara {
58014bf61ffSJan Kara 	memset(dst, 0, sizeof(*dst));
58114bf61ffSJan Kara 	dst->d_version = FS_DQUOT_VERSION;
58214bf61ffSJan Kara 	dst->d_id = id;
58314bf61ffSJan Kara 	if (type == USRQUOTA)
58414bf61ffSJan Kara 		dst->d_flags = FS_USER_QUOTA;
58514bf61ffSJan Kara 	else if (type == PRJQUOTA)
58614bf61ffSJan Kara 		dst->d_flags = FS_PROJ_QUOTA;
58714bf61ffSJan Kara 	else
58814bf61ffSJan Kara 		dst->d_flags = FS_GROUP_QUOTA;
58914bf61ffSJan Kara 	dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
59014bf61ffSJan Kara 	dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
59114bf61ffSJan Kara 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
59214bf61ffSJan Kara 	dst->d_ino_softlimit = src->d_ino_softlimit;
59314bf61ffSJan Kara 	dst->d_bcount = quota_btobb(src->d_space);
59414bf61ffSJan Kara 	dst->d_icount = src->d_ino_count;
59514bf61ffSJan Kara 	dst->d_itimer = src->d_ino_timer;
59614bf61ffSJan Kara 	dst->d_btimer = src->d_spc_timer;
59714bf61ffSJan Kara 	dst->d_iwarns = src->d_ino_warns;
59814bf61ffSJan Kara 	dst->d_bwarns = src->d_spc_warns;
59914bf61ffSJan Kara 	dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
60014bf61ffSJan Kara 	dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
60114bf61ffSJan Kara 	dst->d_rtbcount = quota_btobb(src->d_rt_space);
60214bf61ffSJan Kara 	dst->d_rtbtimer = src->d_rt_spc_timer;
60314bf61ffSJan Kara 	dst->d_rtbwarns = src->d_rt_spc_warns;
604884d179dSJan Kara }
605c411e5f6SChristoph Hellwig 
606c411e5f6SChristoph Hellwig static int quota_getxquota(struct super_block *sb, int type, qid_t id,
607c411e5f6SChristoph Hellwig 			   void __user *addr)
608c411e5f6SChristoph Hellwig {
609884d179dSJan Kara 	struct fs_disk_quota fdq;
61014bf61ffSJan Kara 	struct qc_dqblk qdq;
61174a8a103SEric W. Biederman 	struct kqid qid;
612c411e5f6SChristoph Hellwig 	int ret;
613884d179dSJan Kara 
614b9b2dd36SChristoph Hellwig 	if (!sb->s_qcop->get_dqblk)
615f450d4feSChristoph Hellwig 		return -ENOSYS;
61674a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
61774a8a103SEric W. Biederman 	if (!qid_valid(qid))
61874a8a103SEric W. Biederman 		return -EINVAL;
61914bf61ffSJan Kara 	ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
62014bf61ffSJan Kara 	if (ret)
62114bf61ffSJan Kara 		return ret;
62214bf61ffSJan Kara 	copy_to_xfs_dqblk(&fdq, &qdq, type, id);
62314bf61ffSJan Kara 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
624884d179dSJan Kara 		return -EFAULT;
625c411e5f6SChristoph Hellwig 	return ret;
626884d179dSJan Kara }
627c411e5f6SChristoph Hellwig 
6289da93f9bSEric Sandeen static int quota_rmxquota(struct super_block *sb, void __user *addr)
6299da93f9bSEric Sandeen {
6309da93f9bSEric Sandeen 	__u32 flags;
6319da93f9bSEric Sandeen 
6329da93f9bSEric Sandeen 	if (copy_from_user(&flags, addr, sizeof(flags)))
6339da93f9bSEric Sandeen 		return -EFAULT;
6349da93f9bSEric Sandeen 	if (!sb->s_qcop->rm_xquota)
6359da93f9bSEric Sandeen 		return -ENOSYS;
6369da93f9bSEric Sandeen 	return sb->s_qcop->rm_xquota(sb, flags);
6379da93f9bSEric Sandeen }
6389da93f9bSEric Sandeen 
639c411e5f6SChristoph Hellwig /* Copy parameters and call proper function */
640c411e5f6SChristoph Hellwig static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
641f00c9e44SJan Kara 		       void __user *addr, struct path *path)
642c411e5f6SChristoph Hellwig {
643c988afb5SChristoph Hellwig 	int ret;
644c988afb5SChristoph Hellwig 
645c988afb5SChristoph Hellwig 	if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
646c988afb5SChristoph Hellwig 		return -EINVAL;
6472c5f648aSJan Kara 	/*
6482c5f648aSJan Kara 	 * Quota not supported on this fs? Check this before s_quota_types
6492c5f648aSJan Kara 	 * since they needn't be set if quota is not supported at all.
6502c5f648aSJan Kara 	 */
651c988afb5SChristoph Hellwig 	if (!sb->s_qcop)
652c988afb5SChristoph Hellwig 		return -ENOSYS;
6532c5f648aSJan Kara 	if (!(sb->s_quota_types & (1 << type)))
6542c5f648aSJan Kara 		return -EINVAL;
655c988afb5SChristoph Hellwig 
656c988afb5SChristoph Hellwig 	ret = check_quotactl_permission(sb, type, cmd, id);
657c988afb5SChristoph Hellwig 	if (ret < 0)
658c988afb5SChristoph Hellwig 		return ret;
659c988afb5SChristoph Hellwig 
660c411e5f6SChristoph Hellwig 	switch (cmd) {
661c411e5f6SChristoph Hellwig 	case Q_QUOTAON:
662*3218a3ecSEric Sandeen 		return quota_quotaon(sb, type, id, path);
663c411e5f6SChristoph Hellwig 	case Q_QUOTAOFF:
664d3b86324SJan Kara 		return quota_quotaoff(sb, type);
665c411e5f6SChristoph Hellwig 	case Q_GETFMT:
666c411e5f6SChristoph Hellwig 		return quota_getfmt(sb, type, addr);
667c411e5f6SChristoph Hellwig 	case Q_GETINFO:
668c411e5f6SChristoph Hellwig 		return quota_getinfo(sb, type, addr);
669c411e5f6SChristoph Hellwig 	case Q_SETINFO:
670c411e5f6SChristoph Hellwig 		return quota_setinfo(sb, type, addr);
671c411e5f6SChristoph Hellwig 	case Q_GETQUOTA:
672c411e5f6SChristoph Hellwig 		return quota_getquota(sb, type, id, addr);
673c411e5f6SChristoph Hellwig 	case Q_SETQUOTA:
674c411e5f6SChristoph Hellwig 		return quota_setquota(sb, type, id, addr);
675c411e5f6SChristoph Hellwig 	case Q_SYNC:
676f450d4feSChristoph Hellwig 		if (!sb->s_qcop->quota_sync)
677f450d4feSChristoph Hellwig 			return -ENOSYS;
678ceed1723SJan Kara 		return sb->s_qcop->quota_sync(sb, type);
679c411e5f6SChristoph Hellwig 	case Q_XQUOTAON:
68038e478c4SJan Kara 		return quota_enable(sb, addr);
681c411e5f6SChristoph Hellwig 	case Q_XQUOTAOFF:
68238e478c4SJan Kara 		return quota_disable(sb, addr);
6839da93f9bSEric Sandeen 	case Q_XQUOTARM:
6849da93f9bSEric Sandeen 		return quota_rmxquota(sb, addr);
685c411e5f6SChristoph Hellwig 	case Q_XGETQSTAT:
686c411e5f6SChristoph Hellwig 		return quota_getxstate(sb, addr);
687af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
688af30cb44SChandra Seetharaman 		return quota_getxstatev(sb, addr);
689c411e5f6SChristoph Hellwig 	case Q_XSETQLIM:
690c411e5f6SChristoph Hellwig 		return quota_setxquota(sb, type, id, addr);
691c411e5f6SChristoph Hellwig 	case Q_XGETQUOTA:
692c411e5f6SChristoph Hellwig 		return quota_getxquota(sb, type, id, addr);
693884d179dSJan Kara 	case Q_XQUOTASYNC:
6948c4e4acdSChristoph Hellwig 		if (sb->s_flags & MS_RDONLY)
6958c4e4acdSChristoph Hellwig 			return -EROFS;
6964b217ed9SChristoph Hellwig 		/* XFS quotas are fully coherent now, making this call a noop */
6978c4e4acdSChristoph Hellwig 		return 0;
698884d179dSJan Kara 	default:
699f450d4feSChristoph Hellwig 		return -EINVAL;
700884d179dSJan Kara 	}
701884d179dSJan Kara }
702884d179dSJan Kara 
70356df1278SLee Jones #ifdef CONFIG_BLOCK
70456df1278SLee Jones 
705dcdbed85SJan Kara /* Return 1 if 'cmd' will block on frozen filesystem */
706dcdbed85SJan Kara static int quotactl_cmd_write(int cmd)
707dcdbed85SJan Kara {
708dcdbed85SJan Kara 	switch (cmd) {
709dcdbed85SJan Kara 	case Q_GETFMT:
710dcdbed85SJan Kara 	case Q_GETINFO:
711dcdbed85SJan Kara 	case Q_SYNC:
712dcdbed85SJan Kara 	case Q_XGETQSTAT:
713af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
714dcdbed85SJan Kara 	case Q_XGETQUOTA:
715dcdbed85SJan Kara 	case Q_XQUOTASYNC:
716dcdbed85SJan Kara 		return 0;
717dcdbed85SJan Kara 	}
718dcdbed85SJan Kara 	return 1;
719dcdbed85SJan Kara }
720dcdbed85SJan Kara 
72156df1278SLee Jones #endif /* CONFIG_BLOCK */
72256df1278SLee Jones 
723884d179dSJan Kara /*
724884d179dSJan Kara  * look up a superblock on which quota ops will be performed
725884d179dSJan Kara  * - use the name of a block device to find the superblock thereon
726884d179dSJan Kara  */
727dcdbed85SJan Kara static struct super_block *quotactl_block(const char __user *special, int cmd)
728884d179dSJan Kara {
729884d179dSJan Kara #ifdef CONFIG_BLOCK
730884d179dSJan Kara 	struct block_device *bdev;
731884d179dSJan Kara 	struct super_block *sb;
73291a27b2aSJeff Layton 	struct filename *tmp = getname(special);
733884d179dSJan Kara 
734884d179dSJan Kara 	if (IS_ERR(tmp))
735884d179dSJan Kara 		return ERR_CAST(tmp);
73691a27b2aSJeff Layton 	bdev = lookup_bdev(tmp->name);
737884d179dSJan Kara 	putname(tmp);
738884d179dSJan Kara 	if (IS_ERR(bdev))
739884d179dSJan Kara 		return ERR_CAST(bdev);
740dcdbed85SJan Kara 	if (quotactl_cmd_write(cmd))
741dcdbed85SJan Kara 		sb = get_super_thawed(bdev);
742dcdbed85SJan Kara 	else
743884d179dSJan Kara 		sb = get_super(bdev);
744884d179dSJan Kara 	bdput(bdev);
745884d179dSJan Kara 	if (!sb)
746884d179dSJan Kara 		return ERR_PTR(-ENODEV);
747884d179dSJan Kara 
748884d179dSJan Kara 	return sb;
749884d179dSJan Kara #else
750884d179dSJan Kara 	return ERR_PTR(-ENODEV);
751884d179dSJan Kara #endif
752884d179dSJan Kara }
753884d179dSJan Kara 
754884d179dSJan Kara /*
755884d179dSJan Kara  * This is the system call interface. This communicates with
756884d179dSJan Kara  * the user-level programs. Currently this only supports diskquota
757884d179dSJan Kara  * calls. Maybe we need to add the process quotas etc. in the future,
758884d179dSJan Kara  * but we probably should use rlimits for that.
759884d179dSJan Kara  */
760884d179dSJan Kara SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
761884d179dSJan Kara 		qid_t, id, void __user *, addr)
762884d179dSJan Kara {
763884d179dSJan Kara 	uint cmds, type;
764884d179dSJan Kara 	struct super_block *sb = NULL;
765f00c9e44SJan Kara 	struct path path, *pathp = NULL;
766884d179dSJan Kara 	int ret;
767884d179dSJan Kara 
768884d179dSJan Kara 	cmds = cmd >> SUBCMDSHIFT;
769884d179dSJan Kara 	type = cmd & SUBCMDMASK;
770884d179dSJan Kara 
7716ae09575SChristoph Hellwig 	/*
7726ae09575SChristoph Hellwig 	 * As a special case Q_SYNC can be called without a specific device.
7736ae09575SChristoph Hellwig 	 * It will iterate all superblocks that have quota enabled and call
7746ae09575SChristoph Hellwig 	 * the sync action on each of them.
7756ae09575SChristoph Hellwig 	 */
7766ae09575SChristoph Hellwig 	if (!special) {
7776ae09575SChristoph Hellwig 		if (cmds == Q_SYNC)
7786ae09575SChristoph Hellwig 			return quota_sync_all(type);
7796ae09575SChristoph Hellwig 		return -ENODEV;
7806ae09575SChristoph Hellwig 	}
7816ae09575SChristoph Hellwig 
782f00c9e44SJan Kara 	/*
783f00c9e44SJan Kara 	 * Path for quotaon has to be resolved before grabbing superblock
784f00c9e44SJan Kara 	 * because that gets s_umount sem which is also possibly needed by path
785f00c9e44SJan Kara 	 * resolution (think about autofs) and thus deadlocks could arise.
786f00c9e44SJan Kara 	 */
787f00c9e44SJan Kara 	if (cmds == Q_QUOTAON) {
788815d405cSTrond Myklebust 		ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
789f00c9e44SJan Kara 		if (ret)
790f00c9e44SJan Kara 			pathp = ERR_PTR(ret);
791f00c9e44SJan Kara 		else
792f00c9e44SJan Kara 			pathp = &path;
793f00c9e44SJan Kara 	}
794f00c9e44SJan Kara 
795dcdbed85SJan Kara 	sb = quotactl_block(special, cmds);
7960aaa6188SJan Kara 	if (IS_ERR(sb)) {
7970aaa6188SJan Kara 		ret = PTR_ERR(sb);
7980aaa6188SJan Kara 		goto out;
7990aaa6188SJan Kara 	}
800884d179dSJan Kara 
801f00c9e44SJan Kara 	ret = do_quotactl(sb, type, cmds, id, addr, pathp);
802884d179dSJan Kara 
8036ae09575SChristoph Hellwig 	drop_super(sb);
8040aaa6188SJan Kara out:
805f00c9e44SJan Kara 	if (pathp && !IS_ERR(pathp))
806f00c9e44SJan Kara 		path_put(pathp);
807884d179dSJan Kara 	return ret;
808884d179dSJan Kara }
809