xref: /linux/fs/quota/quota.c (revision a6810312b835ed927edaef0e96217c02084c7f4f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2884d179dSJan Kara /*
3884d179dSJan Kara  * Quota code necessary even when VFS quota support is not compiled
4884d179dSJan Kara  * into the kernel.  The interesting stuff is over in dquot.c, here
5884d179dSJan Kara  * we have symbols for initial quotactl(2) handling, the sysctl(2)
6884d179dSJan Kara  * variables, etc - things needed even when quota support disabled.
7884d179dSJan Kara  */
8884d179dSJan Kara 
9884d179dSJan Kara #include <linux/fs.h>
10884d179dSJan Kara #include <linux/namei.h>
11884d179dSJan Kara #include <linux/slab.h>
12884d179dSJan Kara #include <asm/current.h>
13f3da9310SJeff Liu #include <linux/uaccess.h>
14884d179dSJan Kara #include <linux/kernel.h>
15884d179dSJan Kara #include <linux/security.h>
16884d179dSJan Kara #include <linux/syscalls.h>
17884d179dSJan Kara #include <linux/capability.h>
18884d179dSJan Kara #include <linux/quotaops.h>
19884d179dSJan Kara #include <linux/types.h>
208c4e4acdSChristoph Hellwig #include <linux/writeback.h>
217b6924d9SJeremy Cline #include <linux/nospec.h>
22884d179dSJan Kara 
23c988afb5SChristoph Hellwig static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
24268157baSJan Kara 				     qid_t id)
25884d179dSJan Kara {
26c988afb5SChristoph Hellwig 	switch (cmd) {
27c988afb5SChristoph Hellwig 	/* these commands do not require any special privilegues */
28c988afb5SChristoph Hellwig 	case Q_GETFMT:
29c988afb5SChristoph Hellwig 	case Q_SYNC:
30c988afb5SChristoph Hellwig 	case Q_GETINFO:
31c988afb5SChristoph Hellwig 	case Q_XGETQSTAT:
32af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
33c988afb5SChristoph Hellwig 	case Q_XQUOTASYNC:
34c988afb5SChristoph Hellwig 		break;
35c988afb5SChristoph Hellwig 	/* allow to query information for dquots we "own" */
36c988afb5SChristoph Hellwig 	case Q_GETQUOTA:
37c988afb5SChristoph Hellwig 	case Q_XGETQUOTA:
3874a8a103SEric W. Biederman 		if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
3974a8a103SEric W. Biederman 		    (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
40c988afb5SChristoph Hellwig 			break;
41c988afb5SChristoph Hellwig 		/*FALLTHROUGH*/
42c988afb5SChristoph Hellwig 	default:
43884d179dSJan Kara 		if (!capable(CAP_SYS_ADMIN))
44884d179dSJan Kara 			return -EPERM;
45884d179dSJan Kara 	}
46884d179dSJan Kara 
47c988afb5SChristoph Hellwig 	return security_quotactl(cmd, type, id, sb);
48884d179dSJan Kara }
49884d179dSJan Kara 
5001a05b33SAl Viro static void quota_sync_one(struct super_block *sb, void *arg)
5101a05b33SAl Viro {
522c5f648aSJan Kara 	int type = *(int *)arg;
532c5f648aSJan Kara 
542c5f648aSJan Kara 	if (sb->s_qcop && sb->s_qcop->quota_sync &&
552c5f648aSJan Kara 	    (sb->s_quota_types & (1 << type)))
562c5f648aSJan Kara 		sb->s_qcop->quota_sync(sb, type);
5701a05b33SAl Viro }
5801a05b33SAl Viro 
596ae09575SChristoph Hellwig static int quota_sync_all(int type)
60884d179dSJan Kara {
616ae09575SChristoph Hellwig 	int ret;
626ae09575SChristoph Hellwig 
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 
823218a3ecSEric Sandeen static int quota_quotaon(struct super_block *sb, int type, qid_t id,
838c54ca9cSAl Viro 		         const 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 
1079d1ccbe7SJan Kara 	if (!sb_has_quota_active(sb, type))
108884d179dSJan Kara 		return -ESRCH;
109884d179dSJan Kara 	fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
110884d179dSJan Kara 	if (copy_to_user(addr, &fmt, sizeof(fmt)))
111884d179dSJan Kara 		return -EFAULT;
112884d179dSJan Kara 	return 0;
113884d179dSJan Kara }
114c411e5f6SChristoph Hellwig 
115c411e5f6SChristoph Hellwig static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
116c411e5f6SChristoph Hellwig {
1170a240339SJan Kara 	struct qc_state state;
1180a240339SJan Kara 	struct qc_type_state *tstate;
1190a240339SJan Kara 	struct if_dqinfo uinfo;
120c411e5f6SChristoph Hellwig 	int ret;
121884d179dSJan Kara 
1220a240339SJan Kara 	if (!sb->s_qcop->get_state)
123f450d4feSChristoph Hellwig 		return -ENOSYS;
1240a240339SJan Kara 	ret = sb->s_qcop->get_state(sb, &state);
1250a240339SJan Kara 	if (ret)
1260a240339SJan Kara 		return ret;
1270a240339SJan Kara 	tstate = state.s_state + type;
1280a240339SJan Kara 	if (!(tstate->flags & QCI_ACCT_ENABLED))
1290a240339SJan Kara 		return -ESRCH;
1300a240339SJan Kara 	memset(&uinfo, 0, sizeof(uinfo));
1310a240339SJan Kara 	uinfo.dqi_bgrace = tstate->spc_timelimit;
1320a240339SJan Kara 	uinfo.dqi_igrace = tstate->ino_timelimit;
1330a240339SJan Kara 	if (tstate->flags & QCI_SYSFILE)
1340a240339SJan Kara 		uinfo.dqi_flags |= DQF_SYS_FILE;
1350a240339SJan Kara 	if (tstate->flags & QCI_ROOT_SQUASH)
1360a240339SJan Kara 		uinfo.dqi_flags |= DQF_ROOT_SQUASH;
1370a240339SJan Kara 	uinfo.dqi_valid = IIF_ALL;
13872d4d0e4SDan Carpenter 	if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
139884d179dSJan Kara 		return -EFAULT;
14072d4d0e4SDan Carpenter 	return 0;
141884d179dSJan Kara }
142c411e5f6SChristoph Hellwig 
143c411e5f6SChristoph Hellwig static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
144c411e5f6SChristoph Hellwig {
145884d179dSJan Kara 	struct if_dqinfo info;
1465eacb2acSJan Kara 	struct qc_info qinfo;
147884d179dSJan Kara 
148884d179dSJan Kara 	if (copy_from_user(&info, addr, sizeof(info)))
149884d179dSJan Kara 		return -EFAULT;
150f450d4feSChristoph Hellwig 	if (!sb->s_qcop->set_info)
151f450d4feSChristoph Hellwig 		return -ENOSYS;
1525eacb2acSJan Kara 	if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
1535eacb2acSJan Kara 		return -EINVAL;
1545eacb2acSJan Kara 	memset(&qinfo, 0, sizeof(qinfo));
1555eacb2acSJan Kara 	if (info.dqi_valid & IIF_FLAGS) {
1565eacb2acSJan Kara 		if (info.dqi_flags & ~DQF_SETINFO_MASK)
1575eacb2acSJan Kara 			return -EINVAL;
1585eacb2acSJan Kara 		if (info.dqi_flags & DQF_ROOT_SQUASH)
1595eacb2acSJan Kara 			qinfo.i_flags |= QCI_ROOT_SQUASH;
1605eacb2acSJan Kara 		qinfo.i_fieldmask |= QC_FLAGS;
1615eacb2acSJan Kara 	}
1625eacb2acSJan Kara 	if (info.dqi_valid & IIF_BGRACE) {
1635eacb2acSJan Kara 		qinfo.i_spc_timelimit = info.dqi_bgrace;
1645eacb2acSJan Kara 		qinfo.i_fieldmask |= QC_SPC_TIMER;
1655eacb2acSJan Kara 	}
1665eacb2acSJan Kara 	if (info.dqi_valid & IIF_IGRACE) {
1675eacb2acSJan Kara 		qinfo.i_ino_timelimit = info.dqi_igrace;
1685eacb2acSJan Kara 		qinfo.i_fieldmask |= QC_INO_TIMER;
1695eacb2acSJan Kara 	}
1705eacb2acSJan Kara 	return sb->s_qcop->set_info(sb, type, &qinfo);
171884d179dSJan Kara }
172c411e5f6SChristoph Hellwig 
17314bf61ffSJan Kara static inline qsize_t qbtos(qsize_t blocks)
17414bf61ffSJan Kara {
17514bf61ffSJan Kara 	return blocks << QIF_DQBLKSIZE_BITS;
17614bf61ffSJan Kara }
17714bf61ffSJan Kara 
17814bf61ffSJan Kara static inline qsize_t stoqb(qsize_t space)
17914bf61ffSJan Kara {
18014bf61ffSJan Kara 	return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
18114bf61ffSJan Kara }
18214bf61ffSJan Kara 
18314bf61ffSJan Kara static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
184b9b2dd36SChristoph Hellwig {
18518da65e7SDan Carpenter 	memset(dst, 0, sizeof(*dst));
18614bf61ffSJan Kara 	dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
18714bf61ffSJan Kara 	dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
18814bf61ffSJan Kara 	dst->dqb_curspace = src->d_space;
189b9b2dd36SChristoph Hellwig 	dst->dqb_ihardlimit = src->d_ino_hardlimit;
190b9b2dd36SChristoph Hellwig 	dst->dqb_isoftlimit = src->d_ino_softlimit;
19114bf61ffSJan Kara 	dst->dqb_curinodes = src->d_ino_count;
19214bf61ffSJan Kara 	dst->dqb_btime = src->d_spc_timer;
19314bf61ffSJan Kara 	dst->dqb_itime = src->d_ino_timer;
194b9b2dd36SChristoph Hellwig 	dst->dqb_valid = QIF_ALL;
195b9b2dd36SChristoph Hellwig }
196b9b2dd36SChristoph Hellwig 
197c411e5f6SChristoph Hellwig static int quota_getquota(struct super_block *sb, int type, qid_t id,
198c411e5f6SChristoph Hellwig 			  void __user *addr)
199c411e5f6SChristoph Hellwig {
20074a8a103SEric W. Biederman 	struct kqid qid;
20114bf61ffSJan Kara 	struct qc_dqblk fdq;
202884d179dSJan Kara 	struct if_dqblk idq;
203c411e5f6SChristoph Hellwig 	int ret;
204884d179dSJan Kara 
205f450d4feSChristoph Hellwig 	if (!sb->s_qcop->get_dqblk)
206f450d4feSChristoph Hellwig 		return -ENOSYS;
20774a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
208d49d3762SEric W. Biederman 	if (!qid_has_mapping(sb->s_user_ns, qid))
20974a8a103SEric W. Biederman 		return -EINVAL;
21074a8a103SEric W. Biederman 	ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
211268157baSJan Kara 	if (ret)
212884d179dSJan Kara 		return ret;
213b9b2dd36SChristoph Hellwig 	copy_to_if_dqblk(&idq, &fdq);
214884d179dSJan Kara 	if (copy_to_user(addr, &idq, sizeof(idq)))
215884d179dSJan Kara 		return -EFAULT;
216884d179dSJan Kara 	return 0;
217884d179dSJan Kara }
218c411e5f6SChristoph Hellwig 
219926132c0SEric Sandeen /*
220926132c0SEric Sandeen  * Return quota for next active quota >= this id, if any exists,
221ba58148bSEric Sandeen  * otherwise return -ENOENT via ->get_nextdqblk
222926132c0SEric Sandeen  */
223926132c0SEric Sandeen static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
224926132c0SEric Sandeen 			  void __user *addr)
225926132c0SEric Sandeen {
226926132c0SEric Sandeen 	struct kqid qid;
227926132c0SEric Sandeen 	struct qc_dqblk fdq;
228926132c0SEric Sandeen 	struct if_nextdqblk idq;
229926132c0SEric Sandeen 	int ret;
230926132c0SEric Sandeen 
231926132c0SEric Sandeen 	if (!sb->s_qcop->get_nextdqblk)
232926132c0SEric Sandeen 		return -ENOSYS;
233926132c0SEric Sandeen 	qid = make_kqid(current_user_ns(), type, id);
234d49d3762SEric W. Biederman 	if (!qid_has_mapping(sb->s_user_ns, qid))
235926132c0SEric Sandeen 		return -EINVAL;
236926132c0SEric Sandeen 	ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
237926132c0SEric Sandeen 	if (ret)
238926132c0SEric Sandeen 		return ret;
239926132c0SEric Sandeen 	/* struct if_nextdqblk is a superset of struct if_dqblk */
240926132c0SEric Sandeen 	copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq);
241926132c0SEric Sandeen 	idq.dqb_id = from_kqid(current_user_ns(), qid);
242926132c0SEric Sandeen 	if (copy_to_user(addr, &idq, sizeof(idq)))
243926132c0SEric Sandeen 		return -EFAULT;
244926132c0SEric Sandeen 	return 0;
245926132c0SEric Sandeen }
246926132c0SEric Sandeen 
24714bf61ffSJan Kara static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
248c472b432SChristoph Hellwig {
24914bf61ffSJan Kara 	dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
25014bf61ffSJan Kara 	dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
25114bf61ffSJan Kara 	dst->d_space = src->dqb_curspace;
252c472b432SChristoph Hellwig 	dst->d_ino_hardlimit = src->dqb_ihardlimit;
253c472b432SChristoph Hellwig 	dst->d_ino_softlimit = src->dqb_isoftlimit;
25414bf61ffSJan Kara 	dst->d_ino_count = src->dqb_curinodes;
25514bf61ffSJan Kara 	dst->d_spc_timer = src->dqb_btime;
25614bf61ffSJan Kara 	dst->d_ino_timer = src->dqb_itime;
257c472b432SChristoph Hellwig 
258c472b432SChristoph Hellwig 	dst->d_fieldmask = 0;
259c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_BLIMITS)
26014bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
261c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_SPACE)
26214bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPACE;
263c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_ILIMITS)
26414bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
265c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_INODES)
26614bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_COUNT;
267c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_BTIME)
26814bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_TIMER;
269c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_ITIME)
27014bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_TIMER;
271c472b432SChristoph Hellwig }
272c472b432SChristoph Hellwig 
273c411e5f6SChristoph Hellwig static int quota_setquota(struct super_block *sb, int type, qid_t id,
274c411e5f6SChristoph Hellwig 			  void __user *addr)
275c411e5f6SChristoph Hellwig {
27614bf61ffSJan Kara 	struct qc_dqblk fdq;
277884d179dSJan Kara 	struct if_dqblk idq;
27874a8a103SEric W. Biederman 	struct kqid qid;
279884d179dSJan Kara 
280884d179dSJan Kara 	if (copy_from_user(&idq, addr, sizeof(idq)))
281884d179dSJan Kara 		return -EFAULT;
282f450d4feSChristoph Hellwig 	if (!sb->s_qcop->set_dqblk)
283f450d4feSChristoph Hellwig 		return -ENOSYS;
28474a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
285d49d3762SEric W. Biederman 	if (!qid_has_mapping(sb->s_user_ns, qid))
28674a8a103SEric W. Biederman 		return -EINVAL;
287c472b432SChristoph Hellwig 	copy_from_if_dqblk(&fdq, &idq);
28874a8a103SEric W. Biederman 	return sb->s_qcop->set_dqblk(sb, qid, &fdq);
289884d179dSJan Kara }
290884d179dSJan Kara 
29138e478c4SJan Kara static int quota_enable(struct super_block *sb, void __user *addr)
292c411e5f6SChristoph Hellwig {
293884d179dSJan Kara 	__u32 flags;
294884d179dSJan Kara 
295884d179dSJan Kara 	if (copy_from_user(&flags, addr, sizeof(flags)))
296884d179dSJan Kara 		return -EFAULT;
29738e478c4SJan Kara 	if (!sb->s_qcop->quota_enable)
298f450d4feSChristoph Hellwig 		return -ENOSYS;
29938e478c4SJan Kara 	return sb->s_qcop->quota_enable(sb, flags);
30038e478c4SJan Kara }
30138e478c4SJan Kara 
30238e478c4SJan Kara static int quota_disable(struct super_block *sb, void __user *addr)
30338e478c4SJan Kara {
30438e478c4SJan Kara 	__u32 flags;
30538e478c4SJan Kara 
30638e478c4SJan Kara 	if (copy_from_user(&flags, addr, sizeof(flags)))
30738e478c4SJan Kara 		return -EFAULT;
30838e478c4SJan Kara 	if (!sb->s_qcop->quota_disable)
30938e478c4SJan Kara 		return -ENOSYS;
31038e478c4SJan Kara 	return sb->s_qcop->quota_disable(sb, flags);
311884d179dSJan Kara }
312884d179dSJan Kara 
313bc230e4aSJan Kara static int quota_state_to_flags(struct qc_state *state)
314bc230e4aSJan Kara {
315bc230e4aSJan Kara 	int flags = 0;
316bc230e4aSJan Kara 
317bc230e4aSJan Kara 	if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
318bc230e4aSJan Kara 		flags |= FS_QUOTA_UDQ_ACCT;
319bc230e4aSJan Kara 	if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
320bc230e4aSJan Kara 		flags |= FS_QUOTA_UDQ_ENFD;
321bc230e4aSJan Kara 	if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
322bc230e4aSJan Kara 		flags |= FS_QUOTA_GDQ_ACCT;
323bc230e4aSJan Kara 	if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
324bc230e4aSJan Kara 		flags |= FS_QUOTA_GDQ_ENFD;
325bc230e4aSJan Kara 	if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
326bc230e4aSJan Kara 		flags |= FS_QUOTA_PDQ_ACCT;
327bc230e4aSJan Kara 	if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
328bc230e4aSJan Kara 		flags |= FS_QUOTA_PDQ_ENFD;
329bc230e4aSJan Kara 	return flags;
330bc230e4aSJan Kara }
331bc230e4aSJan Kara 
332555b2c3dSEric Sandeen static int quota_getstate(struct super_block *sb, int type,
333555b2c3dSEric Sandeen 			  struct fs_quota_stat *fqs)
334bc230e4aSJan Kara {
335bc230e4aSJan Kara 	struct qc_state state;
336bc230e4aSJan Kara 	int ret;
337bc230e4aSJan Kara 
3383cd0126dSEric Sandeen 	memset(&state, 0, sizeof (struct qc_state));
339bc230e4aSJan Kara 	ret = sb->s_qcop->get_state(sb, &state);
340bc230e4aSJan Kara 	if (ret < 0)
341bc230e4aSJan Kara 		return ret;
342bc230e4aSJan Kara 
343bc230e4aSJan Kara 	memset(fqs, 0, sizeof(*fqs));
344bc230e4aSJan Kara 	fqs->qs_version = FS_QSTAT_VERSION;
345bc230e4aSJan Kara 	fqs->qs_flags = quota_state_to_flags(&state);
346bc230e4aSJan Kara 	/* No quota enabled? */
347bc230e4aSJan Kara 	if (!fqs->qs_flags)
348bc230e4aSJan Kara 		return -ENOSYS;
349bc230e4aSJan Kara 	fqs->qs_incoredqs = state.s_incoredqs;
350555b2c3dSEric Sandeen 
351bc230e4aSJan Kara 	fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
352bc230e4aSJan Kara 	fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
353bc230e4aSJan Kara 	fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
354bc230e4aSJan Kara 	fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
355bc230e4aSJan Kara 	fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
3563cd0126dSEric Sandeen 
3573cd0126dSEric Sandeen 	/* Inodes may be allocated even if inactive; copy out if present */
3583cd0126dSEric Sandeen 	if (state.s_state[USRQUOTA].ino) {
359bc230e4aSJan Kara 		fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
360bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
361bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
362bc230e4aSJan Kara 	}
3633cd0126dSEric Sandeen 	if (state.s_state[GRPQUOTA].ino) {
364bc230e4aSJan Kara 		fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
365bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
366bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
367bc230e4aSJan Kara 	}
3683cd0126dSEric Sandeen 	if (state.s_state[PRJQUOTA].ino) {
369bc230e4aSJan Kara 		/*
370bc230e4aSJan Kara 		 * Q_XGETQSTAT doesn't have room for both group and project
371bc230e4aSJan Kara 		 * quotas.  So, allow the project quota values to be copied out
372bc230e4aSJan Kara 		 * only if there is no group quota information available.
373bc230e4aSJan Kara 		 */
374bc230e4aSJan Kara 		if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
375bc230e4aSJan Kara 			fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
376bc230e4aSJan Kara 			fqs->qs_gquota.qfs_nblks =
377bc230e4aSJan Kara 					state.s_state[PRJQUOTA].blocks;
378bc230e4aSJan Kara 			fqs->qs_gquota.qfs_nextents =
379bc230e4aSJan Kara 					state.s_state[PRJQUOTA].nextents;
380bc230e4aSJan Kara 		}
381bc230e4aSJan Kara 	}
382bc230e4aSJan Kara 	return 0;
383bc230e4aSJan Kara }
384bc230e4aSJan Kara 
385555b2c3dSEric Sandeen static int quota_getxstate(struct super_block *sb, int type, void __user *addr)
386c411e5f6SChristoph Hellwig {
387c411e5f6SChristoph Hellwig 	struct fs_quota_stat fqs;
388c411e5f6SChristoph Hellwig 	int ret;
389c411e5f6SChristoph Hellwig 
39059b6ba69SJan Kara 	if (!sb->s_qcop->get_state)
391f450d4feSChristoph Hellwig 		return -ENOSYS;
392555b2c3dSEric Sandeen 	ret = quota_getstate(sb, type, &fqs);
393c411e5f6SChristoph Hellwig 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
394884d179dSJan Kara 		return -EFAULT;
395c411e5f6SChristoph Hellwig 	return ret;
396884d179dSJan Kara }
397c411e5f6SChristoph Hellwig 
398555b2c3dSEric Sandeen static int quota_getstatev(struct super_block *sb, int type,
399555b2c3dSEric Sandeen 			   struct fs_quota_statv *fqs)
400bc230e4aSJan Kara {
401bc230e4aSJan Kara 	struct qc_state state;
402bc230e4aSJan Kara 	int ret;
403bc230e4aSJan Kara 
4043cd0126dSEric Sandeen 	memset(&state, 0, sizeof (struct qc_state));
405bc230e4aSJan Kara 	ret = sb->s_qcop->get_state(sb, &state);
406bc230e4aSJan Kara 	if (ret < 0)
407bc230e4aSJan Kara 		return ret;
408bc230e4aSJan Kara 
409bc230e4aSJan Kara 	memset(fqs, 0, sizeof(*fqs));
410bc230e4aSJan Kara 	fqs->qs_version = FS_QSTAT_VERSION;
411bc230e4aSJan Kara 	fqs->qs_flags = quota_state_to_flags(&state);
412bc230e4aSJan Kara 	/* No quota enabled? */
413bc230e4aSJan Kara 	if (!fqs->qs_flags)
414bc230e4aSJan Kara 		return -ENOSYS;
415bc230e4aSJan Kara 	fqs->qs_incoredqs = state.s_incoredqs;
416555b2c3dSEric Sandeen 
417bc230e4aSJan Kara 	fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
418bc230e4aSJan Kara 	fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
419bc230e4aSJan Kara 	fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
420bc230e4aSJan Kara 	fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
421bc230e4aSJan Kara 	fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
4223cd0126dSEric Sandeen 
4233cd0126dSEric Sandeen 	/* Inodes may be allocated even if inactive; copy out if present */
4243cd0126dSEric Sandeen 	if (state.s_state[USRQUOTA].ino) {
425bc230e4aSJan Kara 		fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
426bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
427bc230e4aSJan Kara 		fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
428bc230e4aSJan Kara 	}
4293cd0126dSEric Sandeen 	if (state.s_state[GRPQUOTA].ino) {
430bc230e4aSJan Kara 		fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
431bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
432bc230e4aSJan Kara 		fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
433bc230e4aSJan Kara 	}
4343cd0126dSEric Sandeen 	if (state.s_state[PRJQUOTA].ino) {
435bc230e4aSJan Kara 		fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
436bc230e4aSJan Kara 		fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
437bc230e4aSJan Kara 		fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
438bc230e4aSJan Kara 	}
439bc230e4aSJan Kara 	return 0;
440bc230e4aSJan Kara }
441bc230e4aSJan Kara 
442555b2c3dSEric Sandeen static int quota_getxstatev(struct super_block *sb, int type, void __user *addr)
443af30cb44SChandra Seetharaman {
444af30cb44SChandra Seetharaman 	struct fs_quota_statv fqs;
445af30cb44SChandra Seetharaman 	int ret;
446af30cb44SChandra Seetharaman 
44759b6ba69SJan Kara 	if (!sb->s_qcop->get_state)
448af30cb44SChandra Seetharaman 		return -ENOSYS;
449af30cb44SChandra Seetharaman 
450af30cb44SChandra Seetharaman 	memset(&fqs, 0, sizeof(fqs));
451af30cb44SChandra Seetharaman 	if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
452af30cb44SChandra Seetharaman 		return -EFAULT;
453af30cb44SChandra Seetharaman 
454af30cb44SChandra Seetharaman 	/* If this kernel doesn't support user specified version, fail */
455af30cb44SChandra Seetharaman 	switch (fqs.qs_version) {
456af30cb44SChandra Seetharaman 	case FS_QSTATV_VERSION1:
457af30cb44SChandra Seetharaman 		break;
458af30cb44SChandra Seetharaman 	default:
459af30cb44SChandra Seetharaman 		return -EINVAL;
460af30cb44SChandra Seetharaman 	}
461555b2c3dSEric Sandeen 	ret = quota_getstatev(sb, type, &fqs);
462af30cb44SChandra Seetharaman 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
463af30cb44SChandra Seetharaman 		return -EFAULT;
464af30cb44SChandra Seetharaman 	return ret;
465af30cb44SChandra Seetharaman }
466af30cb44SChandra Seetharaman 
46714bf61ffSJan Kara /*
46814bf61ffSJan Kara  * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
46914bf61ffSJan Kara  * out of there as xfsprogs rely on definitions being in that header file. So
47014bf61ffSJan Kara  * just define same functions here for quota purposes.
47114bf61ffSJan Kara  */
47214bf61ffSJan Kara #define XFS_BB_SHIFT 9
47314bf61ffSJan Kara 
47414bf61ffSJan Kara static inline u64 quota_bbtob(u64 blocks)
47514bf61ffSJan Kara {
47614bf61ffSJan Kara 	return blocks << XFS_BB_SHIFT;
47714bf61ffSJan Kara }
47814bf61ffSJan Kara 
47914bf61ffSJan Kara static inline u64 quota_btobb(u64 bytes)
48014bf61ffSJan Kara {
48114bf61ffSJan Kara 	return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
48214bf61ffSJan Kara }
48314bf61ffSJan Kara 
48414bf61ffSJan Kara static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
48514bf61ffSJan Kara {
48614bf61ffSJan Kara 	dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
48714bf61ffSJan Kara 	dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
48814bf61ffSJan Kara 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
48914bf61ffSJan Kara 	dst->d_ino_softlimit = src->d_ino_softlimit;
49014bf61ffSJan Kara 	dst->d_space = quota_bbtob(src->d_bcount);
49114bf61ffSJan Kara 	dst->d_ino_count = src->d_icount;
49214bf61ffSJan Kara 	dst->d_ino_timer = src->d_itimer;
49314bf61ffSJan Kara 	dst->d_spc_timer = src->d_btimer;
49414bf61ffSJan Kara 	dst->d_ino_warns = src->d_iwarns;
49514bf61ffSJan Kara 	dst->d_spc_warns = src->d_bwarns;
49614bf61ffSJan Kara 	dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
49714bf61ffSJan Kara 	dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
49814bf61ffSJan Kara 	dst->d_rt_space = quota_bbtob(src->d_rtbcount);
49914bf61ffSJan Kara 	dst->d_rt_spc_timer = src->d_rtbtimer;
50014bf61ffSJan Kara 	dst->d_rt_spc_warns = src->d_rtbwarns;
50114bf61ffSJan Kara 	dst->d_fieldmask = 0;
50214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ISOFT)
50314bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_SOFT;
50414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_IHARD)
50514bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_HARD;
50614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BSOFT)
50714bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_SOFT;
50814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BHARD)
50914bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_HARD;
51014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBSOFT)
51114bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_SOFT;
51214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBHARD)
51314bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_HARD;
51414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BTIMER)
51514bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_TIMER;
51614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ITIMER)
51714bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_TIMER;
51814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
51914bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_TIMER;
52014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BWARNS)
52114bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_WARNS;
52214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_IWARNS)
52314bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_WARNS;
52414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
52514bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_WARNS;
52614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BCOUNT)
52714bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPACE;
52814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ICOUNT)
52914bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_COUNT;
53014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBCOUNT)
53114bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPACE;
53214bf61ffSJan Kara }
53314bf61ffSJan Kara 
534c39fb53bSJan Kara static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
535c39fb53bSJan Kara 				       struct fs_disk_quota *src)
536c39fb53bSJan Kara {
537c39fb53bSJan Kara 	memset(dst, 0, sizeof(*dst));
538c39fb53bSJan Kara 	dst->i_spc_timelimit = src->d_btimer;
539c39fb53bSJan Kara 	dst->i_ino_timelimit = src->d_itimer;
540c39fb53bSJan Kara 	dst->i_rt_spc_timelimit = src->d_rtbtimer;
541c39fb53bSJan Kara 	dst->i_ino_warnlimit = src->d_iwarns;
542c39fb53bSJan Kara 	dst->i_spc_warnlimit = src->d_bwarns;
543c39fb53bSJan Kara 	dst->i_rt_spc_warnlimit = src->d_rtbwarns;
544c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_BWARNS)
545c39fb53bSJan Kara 		dst->i_fieldmask |= QC_SPC_WARNS;
546c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_IWARNS)
547c39fb53bSJan Kara 		dst->i_fieldmask |= QC_INO_WARNS;
548c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
549c39fb53bSJan Kara 		dst->i_fieldmask |= QC_RT_SPC_WARNS;
550c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_BTIMER)
551c39fb53bSJan Kara 		dst->i_fieldmask |= QC_SPC_TIMER;
552c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_ITIMER)
553c39fb53bSJan Kara 		dst->i_fieldmask |= QC_INO_TIMER;
554c39fb53bSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
555c39fb53bSJan Kara 		dst->i_fieldmask |= QC_RT_SPC_TIMER;
556c39fb53bSJan Kara }
557c39fb53bSJan Kara 
558c411e5f6SChristoph Hellwig static int quota_setxquota(struct super_block *sb, int type, qid_t id,
559c411e5f6SChristoph Hellwig 			   void __user *addr)
560c411e5f6SChristoph Hellwig {
561884d179dSJan Kara 	struct fs_disk_quota fdq;
56214bf61ffSJan Kara 	struct qc_dqblk qdq;
56374a8a103SEric W. Biederman 	struct kqid qid;
564884d179dSJan Kara 
565884d179dSJan Kara 	if (copy_from_user(&fdq, addr, sizeof(fdq)))
566884d179dSJan Kara 		return -EFAULT;
567c472b432SChristoph Hellwig 	if (!sb->s_qcop->set_dqblk)
568f450d4feSChristoph Hellwig 		return -ENOSYS;
56974a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
570d49d3762SEric W. Biederman 	if (!qid_has_mapping(sb->s_user_ns, qid))
57174a8a103SEric W. Biederman 		return -EINVAL;
572c39fb53bSJan Kara 	/* Are we actually setting timer / warning limits for all users? */
573cfd4c70aSEric W. Biederman 	if (from_kqid(sb->s_user_ns, qid) == 0 &&
574c39fb53bSJan Kara 	    fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
575c39fb53bSJan Kara 		struct qc_info qinfo;
576c39fb53bSJan Kara 		int ret;
577c39fb53bSJan Kara 
578c39fb53bSJan Kara 		if (!sb->s_qcop->set_info)
579c39fb53bSJan Kara 			return -EINVAL;
580c39fb53bSJan Kara 		copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
581c39fb53bSJan Kara 		ret = sb->s_qcop->set_info(sb, type, &qinfo);
582c39fb53bSJan Kara 		if (ret)
583c39fb53bSJan Kara 			return ret;
584c39fb53bSJan Kara 		/* These are already done */
585c39fb53bSJan Kara 		fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
586c39fb53bSJan Kara 	}
58714bf61ffSJan Kara 	copy_from_xfs_dqblk(&qdq, &fdq);
58814bf61ffSJan Kara 	return sb->s_qcop->set_dqblk(sb, qid, &qdq);
58914bf61ffSJan Kara }
59014bf61ffSJan Kara 
59114bf61ffSJan Kara static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
59214bf61ffSJan Kara 			      int type, qid_t id)
59314bf61ffSJan Kara {
59414bf61ffSJan Kara 	memset(dst, 0, sizeof(*dst));
59514bf61ffSJan Kara 	dst->d_version = FS_DQUOT_VERSION;
59614bf61ffSJan Kara 	dst->d_id = id;
59714bf61ffSJan Kara 	if (type == USRQUOTA)
59814bf61ffSJan Kara 		dst->d_flags = FS_USER_QUOTA;
59914bf61ffSJan Kara 	else if (type == PRJQUOTA)
60014bf61ffSJan Kara 		dst->d_flags = FS_PROJ_QUOTA;
60114bf61ffSJan Kara 	else
60214bf61ffSJan Kara 		dst->d_flags = FS_GROUP_QUOTA;
60314bf61ffSJan Kara 	dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
60414bf61ffSJan Kara 	dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
60514bf61ffSJan Kara 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
60614bf61ffSJan Kara 	dst->d_ino_softlimit = src->d_ino_softlimit;
60714bf61ffSJan Kara 	dst->d_bcount = quota_btobb(src->d_space);
60814bf61ffSJan Kara 	dst->d_icount = src->d_ino_count;
60914bf61ffSJan Kara 	dst->d_itimer = src->d_ino_timer;
61014bf61ffSJan Kara 	dst->d_btimer = src->d_spc_timer;
61114bf61ffSJan Kara 	dst->d_iwarns = src->d_ino_warns;
61214bf61ffSJan Kara 	dst->d_bwarns = src->d_spc_warns;
61314bf61ffSJan Kara 	dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
61414bf61ffSJan Kara 	dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
61514bf61ffSJan Kara 	dst->d_rtbcount = quota_btobb(src->d_rt_space);
61614bf61ffSJan Kara 	dst->d_rtbtimer = src->d_rt_spc_timer;
61714bf61ffSJan Kara 	dst->d_rtbwarns = src->d_rt_spc_warns;
618884d179dSJan Kara }
619c411e5f6SChristoph Hellwig 
620c411e5f6SChristoph Hellwig static int quota_getxquota(struct super_block *sb, int type, qid_t id,
621c411e5f6SChristoph Hellwig 			   void __user *addr)
622c411e5f6SChristoph Hellwig {
623884d179dSJan Kara 	struct fs_disk_quota fdq;
62414bf61ffSJan Kara 	struct qc_dqblk qdq;
62574a8a103SEric W. Biederman 	struct kqid qid;
626c411e5f6SChristoph Hellwig 	int ret;
627884d179dSJan Kara 
628b9b2dd36SChristoph Hellwig 	if (!sb->s_qcop->get_dqblk)
629f450d4feSChristoph Hellwig 		return -ENOSYS;
63074a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
631d49d3762SEric W. Biederman 	if (!qid_has_mapping(sb->s_user_ns, qid))
63274a8a103SEric W. Biederman 		return -EINVAL;
63314bf61ffSJan Kara 	ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
63414bf61ffSJan Kara 	if (ret)
63514bf61ffSJan Kara 		return ret;
63614bf61ffSJan Kara 	copy_to_xfs_dqblk(&fdq, &qdq, type, id);
63714bf61ffSJan Kara 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
638884d179dSJan Kara 		return -EFAULT;
639c411e5f6SChristoph Hellwig 	return ret;
640884d179dSJan Kara }
641c411e5f6SChristoph Hellwig 
6428b375249SEric Sandeen /*
6438b375249SEric Sandeen  * Return quota for next active quota >= this id, if any exists,
644ba58148bSEric Sandeen  * otherwise return -ENOENT via ->get_nextdqblk.
6458b375249SEric Sandeen  */
6468b375249SEric Sandeen static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
6478b375249SEric Sandeen 			    void __user *addr)
6488b375249SEric Sandeen {
6498b375249SEric Sandeen 	struct fs_disk_quota fdq;
6508b375249SEric Sandeen 	struct qc_dqblk qdq;
6518b375249SEric Sandeen 	struct kqid qid;
6528b375249SEric Sandeen 	qid_t id_out;
6538b375249SEric Sandeen 	int ret;
6548b375249SEric Sandeen 
6558b375249SEric Sandeen 	if (!sb->s_qcop->get_nextdqblk)
6568b375249SEric Sandeen 		return -ENOSYS;
6578b375249SEric Sandeen 	qid = make_kqid(current_user_ns(), type, id);
658d49d3762SEric W. Biederman 	if (!qid_has_mapping(sb->s_user_ns, qid))
6598b375249SEric Sandeen 		return -EINVAL;
6608b375249SEric Sandeen 	ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
6618b375249SEric Sandeen 	if (ret)
6628b375249SEric Sandeen 		return ret;
6638b375249SEric Sandeen 	id_out = from_kqid(current_user_ns(), qid);
6648b375249SEric Sandeen 	copy_to_xfs_dqblk(&fdq, &qdq, type, id_out);
6658b375249SEric Sandeen 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
6668b375249SEric Sandeen 		return -EFAULT;
6678b375249SEric Sandeen 	return ret;
6688b375249SEric Sandeen }
6698b375249SEric Sandeen 
6709da93f9bSEric Sandeen static int quota_rmxquota(struct super_block *sb, void __user *addr)
6719da93f9bSEric Sandeen {
6729da93f9bSEric Sandeen 	__u32 flags;
6739da93f9bSEric Sandeen 
6749da93f9bSEric Sandeen 	if (copy_from_user(&flags, addr, sizeof(flags)))
6759da93f9bSEric Sandeen 		return -EFAULT;
6769da93f9bSEric Sandeen 	if (!sb->s_qcop->rm_xquota)
6779da93f9bSEric Sandeen 		return -ENOSYS;
6789da93f9bSEric Sandeen 	return sb->s_qcop->rm_xquota(sb, flags);
6799da93f9bSEric Sandeen }
6809da93f9bSEric Sandeen 
681c411e5f6SChristoph Hellwig /* Copy parameters and call proper function */
682c411e5f6SChristoph Hellwig static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
6838c54ca9cSAl Viro 		       void __user *addr, const struct path *path)
684c411e5f6SChristoph Hellwig {
685c988afb5SChristoph Hellwig 	int ret;
686c988afb5SChristoph Hellwig 
6877b6924d9SJeremy Cline 	type = array_index_nospec(type, MAXQUOTAS);
6882c5f648aSJan Kara 	/*
6892c5f648aSJan Kara 	 * Quota not supported on this fs? Check this before s_quota_types
6902c5f648aSJan Kara 	 * since they needn't be set if quota is not supported at all.
6912c5f648aSJan Kara 	 */
692c988afb5SChristoph Hellwig 	if (!sb->s_qcop)
693c988afb5SChristoph Hellwig 		return -ENOSYS;
6942c5f648aSJan Kara 	if (!(sb->s_quota_types & (1 << type)))
6952c5f648aSJan Kara 		return -EINVAL;
696c988afb5SChristoph Hellwig 
697c988afb5SChristoph Hellwig 	ret = check_quotactl_permission(sb, type, cmd, id);
698c988afb5SChristoph Hellwig 	if (ret < 0)
699c988afb5SChristoph Hellwig 		return ret;
700c988afb5SChristoph Hellwig 
701c411e5f6SChristoph Hellwig 	switch (cmd) {
702c411e5f6SChristoph Hellwig 	case Q_QUOTAON:
7033218a3ecSEric Sandeen 		return quota_quotaon(sb, type, id, path);
704c411e5f6SChristoph Hellwig 	case Q_QUOTAOFF:
705d3b86324SJan Kara 		return quota_quotaoff(sb, type);
706c411e5f6SChristoph Hellwig 	case Q_GETFMT:
707c411e5f6SChristoph Hellwig 		return quota_getfmt(sb, type, addr);
708c411e5f6SChristoph Hellwig 	case Q_GETINFO:
709c411e5f6SChristoph Hellwig 		return quota_getinfo(sb, type, addr);
710c411e5f6SChristoph Hellwig 	case Q_SETINFO:
711c411e5f6SChristoph Hellwig 		return quota_setinfo(sb, type, addr);
712c411e5f6SChristoph Hellwig 	case Q_GETQUOTA:
713c411e5f6SChristoph Hellwig 		return quota_getquota(sb, type, id, addr);
714926132c0SEric Sandeen 	case Q_GETNEXTQUOTA:
715926132c0SEric Sandeen 		return quota_getnextquota(sb, type, id, addr);
716c411e5f6SChristoph Hellwig 	case Q_SETQUOTA:
717c411e5f6SChristoph Hellwig 		return quota_setquota(sb, type, id, addr);
718c411e5f6SChristoph Hellwig 	case Q_SYNC:
719f450d4feSChristoph Hellwig 		if (!sb->s_qcop->quota_sync)
720f450d4feSChristoph Hellwig 			return -ENOSYS;
721ceed1723SJan Kara 		return sb->s_qcop->quota_sync(sb, type);
722c411e5f6SChristoph Hellwig 	case Q_XQUOTAON:
72338e478c4SJan Kara 		return quota_enable(sb, addr);
724c411e5f6SChristoph Hellwig 	case Q_XQUOTAOFF:
72538e478c4SJan Kara 		return quota_disable(sb, addr);
7269da93f9bSEric Sandeen 	case Q_XQUOTARM:
7279da93f9bSEric Sandeen 		return quota_rmxquota(sb, addr);
728c411e5f6SChristoph Hellwig 	case Q_XGETQSTAT:
729555b2c3dSEric Sandeen 		return quota_getxstate(sb, type, addr);
730af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
731555b2c3dSEric Sandeen 		return quota_getxstatev(sb, type, addr);
732c411e5f6SChristoph Hellwig 	case Q_XSETQLIM:
733c411e5f6SChristoph Hellwig 		return quota_setxquota(sb, type, id, addr);
734c411e5f6SChristoph Hellwig 	case Q_XGETQUOTA:
735c411e5f6SChristoph Hellwig 		return quota_getxquota(sb, type, id, addr);
7368b375249SEric Sandeen 	case Q_XGETNEXTQUOTA:
7378b375249SEric Sandeen 		return quota_getnextxquota(sb, type, id, addr);
738884d179dSJan Kara 	case Q_XQUOTASYNC:
739bc98a42cSDavid Howells 		if (sb_rdonly(sb))
7408c4e4acdSChristoph Hellwig 			return -EROFS;
7414b217ed9SChristoph Hellwig 		/* XFS quotas are fully coherent now, making this call a noop */
7428c4e4acdSChristoph Hellwig 		return 0;
743884d179dSJan Kara 	default:
744f450d4feSChristoph Hellwig 		return -EINVAL;
745884d179dSJan Kara 	}
746884d179dSJan Kara }
747884d179dSJan Kara 
74856df1278SLee Jones #ifdef CONFIG_BLOCK
74956df1278SLee Jones 
750dcdbed85SJan Kara /* Return 1 if 'cmd' will block on frozen filesystem */
751dcdbed85SJan Kara static int quotactl_cmd_write(int cmd)
752dcdbed85SJan Kara {
753ccf370e4SJan Kara 	/*
754ccf370e4SJan Kara 	 * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access
755ccf370e4SJan Kara 	 * as dquot_acquire() may allocate space for new structure and OCFS2
756ccf370e4SJan Kara 	 * needs to increment on-disk use count.
757ccf370e4SJan Kara 	 */
758dcdbed85SJan Kara 	switch (cmd) {
759dcdbed85SJan Kara 	case Q_GETFMT:
760dcdbed85SJan Kara 	case Q_GETINFO:
761dcdbed85SJan Kara 	case Q_SYNC:
762dcdbed85SJan Kara 	case Q_XGETQSTAT:
763af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
764dcdbed85SJan Kara 	case Q_XGETQUOTA:
7658b375249SEric Sandeen 	case Q_XGETNEXTQUOTA:
766dcdbed85SJan Kara 	case Q_XQUOTASYNC:
767dcdbed85SJan Kara 		return 0;
768dcdbed85SJan Kara 	}
769dcdbed85SJan Kara 	return 1;
770dcdbed85SJan Kara }
77156df1278SLee Jones #endif /* CONFIG_BLOCK */
77256df1278SLee Jones 
7737d6cd73dSJan Kara /* Return true if quotactl command is manipulating quota on/off state */
7747d6cd73dSJan Kara static bool quotactl_cmd_onoff(int cmd)
7757d6cd73dSJan Kara {
77641c4f85cSJavier Barrio 	return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
77741c4f85cSJavier Barrio 		 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
7787d6cd73dSJan Kara }
7797d6cd73dSJan Kara 
780884d179dSJan Kara /*
781884d179dSJan Kara  * look up a superblock on which quota ops will be performed
782884d179dSJan Kara  * - use the name of a block device to find the superblock thereon
783884d179dSJan Kara  */
784dcdbed85SJan Kara static struct super_block *quotactl_block(const char __user *special, int cmd)
785884d179dSJan Kara {
786884d179dSJan Kara #ifdef CONFIG_BLOCK
787884d179dSJan Kara 	struct block_device *bdev;
788884d179dSJan Kara 	struct super_block *sb;
78991a27b2aSJeff Layton 	struct filename *tmp = getname(special);
790884d179dSJan Kara 
791884d179dSJan Kara 	if (IS_ERR(tmp))
792884d179dSJan Kara 		return ERR_CAST(tmp);
79391a27b2aSJeff Layton 	bdev = lookup_bdev(tmp->name);
794884d179dSJan Kara 	putname(tmp);
795884d179dSJan Kara 	if (IS_ERR(bdev))
796884d179dSJan Kara 		return ERR_CAST(bdev);
7977d6cd73dSJan Kara 	if (quotactl_cmd_onoff(cmd))
7987d6cd73dSJan Kara 		sb = get_super_exclusive_thawed(bdev);
7997d6cd73dSJan Kara 	else if (quotactl_cmd_write(cmd))
800dcdbed85SJan Kara 		sb = get_super_thawed(bdev);
801dcdbed85SJan Kara 	else
802884d179dSJan Kara 		sb = get_super(bdev);
803884d179dSJan Kara 	bdput(bdev);
804884d179dSJan Kara 	if (!sb)
805884d179dSJan Kara 		return ERR_PTR(-ENODEV);
806884d179dSJan Kara 
807884d179dSJan Kara 	return sb;
808884d179dSJan Kara #else
809884d179dSJan Kara 	return ERR_PTR(-ENODEV);
810884d179dSJan Kara #endif
811884d179dSJan Kara }
812884d179dSJan Kara 
813884d179dSJan Kara /*
814884d179dSJan Kara  * This is the system call interface. This communicates with
815884d179dSJan Kara  * the user-level programs. Currently this only supports diskquota
816884d179dSJan Kara  * calls. Maybe we need to add the process quotas etc. in the future,
817884d179dSJan Kara  * but we probably should use rlimits for that.
818884d179dSJan Kara  */
819cb0b476aSDominik Brodowski int kernel_quotactl(unsigned int cmd, const char __user *special,
820cb0b476aSDominik Brodowski 		    qid_t id, void __user *addr)
821884d179dSJan Kara {
822884d179dSJan Kara 	uint cmds, type;
823884d179dSJan Kara 	struct super_block *sb = NULL;
824f00c9e44SJan Kara 	struct path path, *pathp = NULL;
825884d179dSJan Kara 	int ret;
826884d179dSJan Kara 
827884d179dSJan Kara 	cmds = cmd >> SUBCMDSHIFT;
828884d179dSJan Kara 	type = cmd & SUBCMDMASK;
829884d179dSJan Kara 
830*a6810312SChengguang Xu 	if (type >= MAXQUOTAS)
831*a6810312SChengguang Xu 		return -EINVAL;
832*a6810312SChengguang Xu 
8336ae09575SChristoph Hellwig 	/*
8346ae09575SChristoph Hellwig 	 * As a special case Q_SYNC can be called without a specific device.
8356ae09575SChristoph Hellwig 	 * It will iterate all superblocks that have quota enabled and call
8366ae09575SChristoph Hellwig 	 * the sync action on each of them.
8376ae09575SChristoph Hellwig 	 */
8386ae09575SChristoph Hellwig 	if (!special) {
8396ae09575SChristoph Hellwig 		if (cmds == Q_SYNC)
8406ae09575SChristoph Hellwig 			return quota_sync_all(type);
8416ae09575SChristoph Hellwig 		return -ENODEV;
8426ae09575SChristoph Hellwig 	}
8436ae09575SChristoph Hellwig 
844f00c9e44SJan Kara 	/*
845f00c9e44SJan Kara 	 * Path for quotaon has to be resolved before grabbing superblock
846f00c9e44SJan Kara 	 * because that gets s_umount sem which is also possibly needed by path
847f00c9e44SJan Kara 	 * resolution (think about autofs) and thus deadlocks could arise.
848f00c9e44SJan Kara 	 */
849f00c9e44SJan Kara 	if (cmds == Q_QUOTAON) {
850815d405cSTrond Myklebust 		ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
851f00c9e44SJan Kara 		if (ret)
852f00c9e44SJan Kara 			pathp = ERR_PTR(ret);
853f00c9e44SJan Kara 		else
854f00c9e44SJan Kara 			pathp = &path;
855f00c9e44SJan Kara 	}
856f00c9e44SJan Kara 
857dcdbed85SJan Kara 	sb = quotactl_block(special, cmds);
8580aaa6188SJan Kara 	if (IS_ERR(sb)) {
8590aaa6188SJan Kara 		ret = PTR_ERR(sb);
8600aaa6188SJan Kara 		goto out;
8610aaa6188SJan Kara 	}
862884d179dSJan Kara 
863f00c9e44SJan Kara 	ret = do_quotactl(sb, type, cmds, id, addr, pathp);
864884d179dSJan Kara 
8657d6cd73dSJan Kara 	if (!quotactl_cmd_onoff(cmds))
8666ae09575SChristoph Hellwig 		drop_super(sb);
8677d6cd73dSJan Kara 	else
8687d6cd73dSJan Kara 		drop_super_exclusive(sb);
8690aaa6188SJan Kara out:
870f00c9e44SJan Kara 	if (pathp && !IS_ERR(pathp))
871f00c9e44SJan Kara 		path_put(pathp);
872884d179dSJan Kara 	return ret;
873884d179dSJan Kara }
874cb0b476aSDominik Brodowski 
875cb0b476aSDominik Brodowski SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
876cb0b476aSDominik Brodowski 		qid_t, id, void __user *, addr)
877cb0b476aSDominik Brodowski {
878cb0b476aSDominik Brodowski 	return kernel_quotactl(cmd, special, id, addr);
879cb0b476aSDominik Brodowski }
880