xref: /openbmc/linux/fs/quota/quota.c (revision 38e478c4489a845a5e8baf7849c286af5fed5b66)
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 
69c411e5f6SChristoph Hellwig static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
70f00c9e44SJan Kara 		         struct path *path)
71884d179dSJan Kara {
72f00c9e44SJan Kara 	if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta)
73f00c9e44SJan Kara 		return -ENOSYS;
74f00c9e44SJan Kara 	if (sb->s_qcop->quota_on_meta)
75f00c9e44SJan Kara 		return sb->s_qcop->quota_on_meta(sb, type, id);
76f00c9e44SJan Kara 	if (IS_ERR(path))
77f00c9e44SJan Kara 		return PTR_ERR(path);
78f00c9e44SJan Kara 	return sb->s_qcop->quota_on(sb, type, id, path);
79884d179dSJan Kara }
80884d179dSJan Kara 
81c411e5f6SChristoph Hellwig static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
82c411e5f6SChristoph Hellwig {
83884d179dSJan Kara 	__u32 fmt;
84884d179dSJan Kara 
85606cdccaSNiu Yawei 	mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
86884d179dSJan Kara 	if (!sb_has_quota_active(sb, type)) {
87606cdccaSNiu Yawei 		mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
88884d179dSJan Kara 		return -ESRCH;
89884d179dSJan Kara 	}
90884d179dSJan Kara 	fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
91606cdccaSNiu Yawei 	mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
92884d179dSJan Kara 	if (copy_to_user(addr, &fmt, sizeof(fmt)))
93884d179dSJan Kara 		return -EFAULT;
94884d179dSJan Kara 	return 0;
95884d179dSJan Kara }
96c411e5f6SChristoph Hellwig 
97c411e5f6SChristoph Hellwig static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
98c411e5f6SChristoph Hellwig {
99884d179dSJan Kara 	struct if_dqinfo info;
100c411e5f6SChristoph Hellwig 	int ret;
101884d179dSJan Kara 
102f450d4feSChristoph Hellwig 	if (!sb->s_qcop->get_info)
103f450d4feSChristoph Hellwig 		return -ENOSYS;
104268157baSJan Kara 	ret = sb->s_qcop->get_info(sb, type, &info);
105c411e5f6SChristoph Hellwig 	if (!ret && copy_to_user(addr, &info, sizeof(info)))
106884d179dSJan Kara 		return -EFAULT;
107c411e5f6SChristoph Hellwig 	return ret;
108884d179dSJan Kara }
109c411e5f6SChristoph Hellwig 
110c411e5f6SChristoph Hellwig static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
111c411e5f6SChristoph Hellwig {
112884d179dSJan Kara 	struct if_dqinfo info;
113884d179dSJan Kara 
114884d179dSJan Kara 	if (copy_from_user(&info, addr, sizeof(info)))
115884d179dSJan Kara 		return -EFAULT;
116f450d4feSChristoph Hellwig 	if (!sb->s_qcop->set_info)
117f450d4feSChristoph Hellwig 		return -ENOSYS;
118884d179dSJan Kara 	return sb->s_qcop->set_info(sb, type, &info);
119884d179dSJan Kara }
120c411e5f6SChristoph Hellwig 
12114bf61ffSJan Kara static inline qsize_t qbtos(qsize_t blocks)
12214bf61ffSJan Kara {
12314bf61ffSJan Kara 	return blocks << QIF_DQBLKSIZE_BITS;
12414bf61ffSJan Kara }
12514bf61ffSJan Kara 
12614bf61ffSJan Kara static inline qsize_t stoqb(qsize_t space)
12714bf61ffSJan Kara {
12814bf61ffSJan Kara 	return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
12914bf61ffSJan Kara }
13014bf61ffSJan Kara 
13114bf61ffSJan Kara static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
132b9b2dd36SChristoph Hellwig {
13318da65e7SDan Carpenter 	memset(dst, 0, sizeof(*dst));
13414bf61ffSJan Kara 	dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
13514bf61ffSJan Kara 	dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
13614bf61ffSJan Kara 	dst->dqb_curspace = src->d_space;
137b9b2dd36SChristoph Hellwig 	dst->dqb_ihardlimit = src->d_ino_hardlimit;
138b9b2dd36SChristoph Hellwig 	dst->dqb_isoftlimit = src->d_ino_softlimit;
13914bf61ffSJan Kara 	dst->dqb_curinodes = src->d_ino_count;
14014bf61ffSJan Kara 	dst->dqb_btime = src->d_spc_timer;
14114bf61ffSJan Kara 	dst->dqb_itime = src->d_ino_timer;
142b9b2dd36SChristoph Hellwig 	dst->dqb_valid = QIF_ALL;
143b9b2dd36SChristoph Hellwig }
144b9b2dd36SChristoph Hellwig 
145c411e5f6SChristoph Hellwig static int quota_getquota(struct super_block *sb, int type, qid_t id,
146c411e5f6SChristoph Hellwig 			  void __user *addr)
147c411e5f6SChristoph Hellwig {
14874a8a103SEric W. Biederman 	struct kqid qid;
14914bf61ffSJan Kara 	struct qc_dqblk fdq;
150884d179dSJan Kara 	struct if_dqblk idq;
151c411e5f6SChristoph Hellwig 	int ret;
152884d179dSJan Kara 
153f450d4feSChristoph Hellwig 	if (!sb->s_qcop->get_dqblk)
154f450d4feSChristoph Hellwig 		return -ENOSYS;
15574a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
15674a8a103SEric W. Biederman 	if (!qid_valid(qid))
15774a8a103SEric W. Biederman 		return -EINVAL;
15874a8a103SEric W. Biederman 	ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
159268157baSJan Kara 	if (ret)
160884d179dSJan Kara 		return ret;
161b9b2dd36SChristoph Hellwig 	copy_to_if_dqblk(&idq, &fdq);
162884d179dSJan Kara 	if (copy_to_user(addr, &idq, sizeof(idq)))
163884d179dSJan Kara 		return -EFAULT;
164884d179dSJan Kara 	return 0;
165884d179dSJan Kara }
166c411e5f6SChristoph Hellwig 
16714bf61ffSJan Kara static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
168c472b432SChristoph Hellwig {
16914bf61ffSJan Kara 	dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
17014bf61ffSJan Kara 	dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
17114bf61ffSJan Kara 	dst->d_space = src->dqb_curspace;
172c472b432SChristoph Hellwig 	dst->d_ino_hardlimit = src->dqb_ihardlimit;
173c472b432SChristoph Hellwig 	dst->d_ino_softlimit = src->dqb_isoftlimit;
17414bf61ffSJan Kara 	dst->d_ino_count = src->dqb_curinodes;
17514bf61ffSJan Kara 	dst->d_spc_timer = src->dqb_btime;
17614bf61ffSJan Kara 	dst->d_ino_timer = src->dqb_itime;
177c472b432SChristoph Hellwig 
178c472b432SChristoph Hellwig 	dst->d_fieldmask = 0;
179c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_BLIMITS)
18014bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
181c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_SPACE)
18214bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPACE;
183c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_ILIMITS)
18414bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
185c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_INODES)
18614bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_COUNT;
187c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_BTIME)
18814bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_TIMER;
189c472b432SChristoph Hellwig 	if (src->dqb_valid & QIF_ITIME)
19014bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_TIMER;
191c472b432SChristoph Hellwig }
192c472b432SChristoph Hellwig 
193c411e5f6SChristoph Hellwig static int quota_setquota(struct super_block *sb, int type, qid_t id,
194c411e5f6SChristoph Hellwig 			  void __user *addr)
195c411e5f6SChristoph Hellwig {
19614bf61ffSJan Kara 	struct qc_dqblk fdq;
197884d179dSJan Kara 	struct if_dqblk idq;
19874a8a103SEric W. Biederman 	struct kqid qid;
199884d179dSJan Kara 
200884d179dSJan Kara 	if (copy_from_user(&idq, addr, sizeof(idq)))
201884d179dSJan Kara 		return -EFAULT;
202f450d4feSChristoph Hellwig 	if (!sb->s_qcop->set_dqblk)
203f450d4feSChristoph Hellwig 		return -ENOSYS;
20474a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
20574a8a103SEric W. Biederman 	if (!qid_valid(qid))
20674a8a103SEric W. Biederman 		return -EINVAL;
207c472b432SChristoph Hellwig 	copy_from_if_dqblk(&fdq, &idq);
20874a8a103SEric W. Biederman 	return sb->s_qcop->set_dqblk(sb, qid, &fdq);
209884d179dSJan Kara }
210884d179dSJan Kara 
211*38e478c4SJan Kara static int quota_enable(struct super_block *sb, void __user *addr)
212c411e5f6SChristoph Hellwig {
213884d179dSJan Kara 	__u32 flags;
214884d179dSJan Kara 
215884d179dSJan Kara 	if (copy_from_user(&flags, addr, sizeof(flags)))
216884d179dSJan Kara 		return -EFAULT;
217*38e478c4SJan Kara 	if (!sb->s_qcop->quota_enable)
218f450d4feSChristoph Hellwig 		return -ENOSYS;
219*38e478c4SJan Kara 	return sb->s_qcop->quota_enable(sb, flags);
220*38e478c4SJan Kara }
221*38e478c4SJan Kara 
222*38e478c4SJan Kara static int quota_disable(struct super_block *sb, void __user *addr)
223*38e478c4SJan Kara {
224*38e478c4SJan Kara 	__u32 flags;
225*38e478c4SJan Kara 
226*38e478c4SJan Kara 	if (copy_from_user(&flags, addr, sizeof(flags)))
227*38e478c4SJan Kara 		return -EFAULT;
228*38e478c4SJan Kara 	if (!sb->s_qcop->quota_disable)
229*38e478c4SJan Kara 		return -ENOSYS;
230*38e478c4SJan Kara 	return sb->s_qcop->quota_disable(sb, flags);
231884d179dSJan Kara }
232884d179dSJan Kara 
233c411e5f6SChristoph Hellwig static int quota_getxstate(struct super_block *sb, void __user *addr)
234c411e5f6SChristoph Hellwig {
235c411e5f6SChristoph Hellwig 	struct fs_quota_stat fqs;
236c411e5f6SChristoph Hellwig 	int ret;
237c411e5f6SChristoph Hellwig 
238f450d4feSChristoph Hellwig 	if (!sb->s_qcop->get_xstate)
239f450d4feSChristoph Hellwig 		return -ENOSYS;
240c411e5f6SChristoph Hellwig 	ret = sb->s_qcop->get_xstate(sb, &fqs);
241c411e5f6SChristoph Hellwig 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
242884d179dSJan Kara 		return -EFAULT;
243c411e5f6SChristoph Hellwig 	return ret;
244884d179dSJan Kara }
245c411e5f6SChristoph Hellwig 
246af30cb44SChandra Seetharaman static int quota_getxstatev(struct super_block *sb, void __user *addr)
247af30cb44SChandra Seetharaman {
248af30cb44SChandra Seetharaman 	struct fs_quota_statv fqs;
249af30cb44SChandra Seetharaman 	int ret;
250af30cb44SChandra Seetharaman 
251af30cb44SChandra Seetharaman 	if (!sb->s_qcop->get_xstatev)
252af30cb44SChandra Seetharaman 		return -ENOSYS;
253af30cb44SChandra Seetharaman 
254af30cb44SChandra Seetharaman 	memset(&fqs, 0, sizeof(fqs));
255af30cb44SChandra Seetharaman 	if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
256af30cb44SChandra Seetharaman 		return -EFAULT;
257af30cb44SChandra Seetharaman 
258af30cb44SChandra Seetharaman 	/* If this kernel doesn't support user specified version, fail */
259af30cb44SChandra Seetharaman 	switch (fqs.qs_version) {
260af30cb44SChandra Seetharaman 	case FS_QSTATV_VERSION1:
261af30cb44SChandra Seetharaman 		break;
262af30cb44SChandra Seetharaman 	default:
263af30cb44SChandra Seetharaman 		return -EINVAL;
264af30cb44SChandra Seetharaman 	}
265af30cb44SChandra Seetharaman 	ret = sb->s_qcop->get_xstatev(sb, &fqs);
266af30cb44SChandra Seetharaman 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
267af30cb44SChandra Seetharaman 		return -EFAULT;
268af30cb44SChandra Seetharaman 	return ret;
269af30cb44SChandra Seetharaman }
270af30cb44SChandra Seetharaman 
27114bf61ffSJan Kara /*
27214bf61ffSJan Kara  * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
27314bf61ffSJan Kara  * out of there as xfsprogs rely on definitions being in that header file. So
27414bf61ffSJan Kara  * just define same functions here for quota purposes.
27514bf61ffSJan Kara  */
27614bf61ffSJan Kara #define XFS_BB_SHIFT 9
27714bf61ffSJan Kara 
27814bf61ffSJan Kara static inline u64 quota_bbtob(u64 blocks)
27914bf61ffSJan Kara {
28014bf61ffSJan Kara 	return blocks << XFS_BB_SHIFT;
28114bf61ffSJan Kara }
28214bf61ffSJan Kara 
28314bf61ffSJan Kara static inline u64 quota_btobb(u64 bytes)
28414bf61ffSJan Kara {
28514bf61ffSJan Kara 	return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
28614bf61ffSJan Kara }
28714bf61ffSJan Kara 
28814bf61ffSJan Kara static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
28914bf61ffSJan Kara {
29014bf61ffSJan Kara 	dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
29114bf61ffSJan Kara 	dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
29214bf61ffSJan Kara 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
29314bf61ffSJan Kara 	dst->d_ino_softlimit = src->d_ino_softlimit;
29414bf61ffSJan Kara 	dst->d_space = quota_bbtob(src->d_bcount);
29514bf61ffSJan Kara 	dst->d_ino_count = src->d_icount;
29614bf61ffSJan Kara 	dst->d_ino_timer = src->d_itimer;
29714bf61ffSJan Kara 	dst->d_spc_timer = src->d_btimer;
29814bf61ffSJan Kara 	dst->d_ino_warns = src->d_iwarns;
29914bf61ffSJan Kara 	dst->d_spc_warns = src->d_bwarns;
30014bf61ffSJan Kara 	dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
30114bf61ffSJan Kara 	dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
30214bf61ffSJan Kara 	dst->d_rt_space = quota_bbtob(src->d_rtbcount);
30314bf61ffSJan Kara 	dst->d_rt_spc_timer = src->d_rtbtimer;
30414bf61ffSJan Kara 	dst->d_rt_spc_warns = src->d_rtbwarns;
30514bf61ffSJan Kara 	dst->d_fieldmask = 0;
30614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ISOFT)
30714bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_SOFT;
30814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_IHARD)
30914bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_HARD;
31014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BSOFT)
31114bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_SOFT;
31214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BHARD)
31314bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_HARD;
31414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBSOFT)
31514bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_SOFT;
31614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBHARD)
31714bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_HARD;
31814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BTIMER)
31914bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_TIMER;
32014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ITIMER)
32114bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_TIMER;
32214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
32314bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_TIMER;
32414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BWARNS)
32514bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPC_WARNS;
32614bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_IWARNS)
32714bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_WARNS;
32814bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
32914bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPC_WARNS;
33014bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_BCOUNT)
33114bf61ffSJan Kara 		dst->d_fieldmask |= QC_SPACE;
33214bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_ICOUNT)
33314bf61ffSJan Kara 		dst->d_fieldmask |= QC_INO_COUNT;
33414bf61ffSJan Kara 	if (src->d_fieldmask & FS_DQ_RTBCOUNT)
33514bf61ffSJan Kara 		dst->d_fieldmask |= QC_RT_SPACE;
33614bf61ffSJan Kara }
33714bf61ffSJan Kara 
338c411e5f6SChristoph Hellwig static int quota_setxquota(struct super_block *sb, int type, qid_t id,
339c411e5f6SChristoph Hellwig 			   void __user *addr)
340c411e5f6SChristoph Hellwig {
341884d179dSJan Kara 	struct fs_disk_quota fdq;
34214bf61ffSJan Kara 	struct qc_dqblk qdq;
34374a8a103SEric W. Biederman 	struct kqid qid;
344884d179dSJan Kara 
345884d179dSJan Kara 	if (copy_from_user(&fdq, addr, sizeof(fdq)))
346884d179dSJan Kara 		return -EFAULT;
347c472b432SChristoph Hellwig 	if (!sb->s_qcop->set_dqblk)
348f450d4feSChristoph Hellwig 		return -ENOSYS;
34974a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
35074a8a103SEric W. Biederman 	if (!qid_valid(qid))
35174a8a103SEric W. Biederman 		return -EINVAL;
35214bf61ffSJan Kara 	copy_from_xfs_dqblk(&qdq, &fdq);
35314bf61ffSJan Kara 	return sb->s_qcop->set_dqblk(sb, qid, &qdq);
35414bf61ffSJan Kara }
35514bf61ffSJan Kara 
35614bf61ffSJan Kara static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
35714bf61ffSJan Kara 			      int type, qid_t id)
35814bf61ffSJan Kara {
35914bf61ffSJan Kara 	memset(dst, 0, sizeof(*dst));
36014bf61ffSJan Kara 	dst->d_version = FS_DQUOT_VERSION;
36114bf61ffSJan Kara 	dst->d_id = id;
36214bf61ffSJan Kara 	if (type == USRQUOTA)
36314bf61ffSJan Kara 		dst->d_flags = FS_USER_QUOTA;
36414bf61ffSJan Kara 	else if (type == PRJQUOTA)
36514bf61ffSJan Kara 		dst->d_flags = FS_PROJ_QUOTA;
36614bf61ffSJan Kara 	else
36714bf61ffSJan Kara 		dst->d_flags = FS_GROUP_QUOTA;
36814bf61ffSJan Kara 	dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
36914bf61ffSJan Kara 	dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
37014bf61ffSJan Kara 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
37114bf61ffSJan Kara 	dst->d_ino_softlimit = src->d_ino_softlimit;
37214bf61ffSJan Kara 	dst->d_bcount = quota_btobb(src->d_space);
37314bf61ffSJan Kara 	dst->d_icount = src->d_ino_count;
37414bf61ffSJan Kara 	dst->d_itimer = src->d_ino_timer;
37514bf61ffSJan Kara 	dst->d_btimer = src->d_spc_timer;
37614bf61ffSJan Kara 	dst->d_iwarns = src->d_ino_warns;
37714bf61ffSJan Kara 	dst->d_bwarns = src->d_spc_warns;
37814bf61ffSJan Kara 	dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
37914bf61ffSJan Kara 	dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
38014bf61ffSJan Kara 	dst->d_rtbcount = quota_btobb(src->d_rt_space);
38114bf61ffSJan Kara 	dst->d_rtbtimer = src->d_rt_spc_timer;
38214bf61ffSJan Kara 	dst->d_rtbwarns = src->d_rt_spc_warns;
383884d179dSJan Kara }
384c411e5f6SChristoph Hellwig 
385c411e5f6SChristoph Hellwig static int quota_getxquota(struct super_block *sb, int type, qid_t id,
386c411e5f6SChristoph Hellwig 			   void __user *addr)
387c411e5f6SChristoph Hellwig {
388884d179dSJan Kara 	struct fs_disk_quota fdq;
38914bf61ffSJan Kara 	struct qc_dqblk qdq;
39074a8a103SEric W. Biederman 	struct kqid qid;
391c411e5f6SChristoph Hellwig 	int ret;
392884d179dSJan Kara 
393b9b2dd36SChristoph Hellwig 	if (!sb->s_qcop->get_dqblk)
394f450d4feSChristoph Hellwig 		return -ENOSYS;
39574a8a103SEric W. Biederman 	qid = make_kqid(current_user_ns(), type, id);
39674a8a103SEric W. Biederman 	if (!qid_valid(qid))
39774a8a103SEric W. Biederman 		return -EINVAL;
39814bf61ffSJan Kara 	ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
39914bf61ffSJan Kara 	if (ret)
40014bf61ffSJan Kara 		return ret;
40114bf61ffSJan Kara 	copy_to_xfs_dqblk(&fdq, &qdq, type, id);
40214bf61ffSJan Kara 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
403884d179dSJan Kara 		return -EFAULT;
404c411e5f6SChristoph Hellwig 	return ret;
405884d179dSJan Kara }
406c411e5f6SChristoph Hellwig 
4079da93f9bSEric Sandeen static int quota_rmxquota(struct super_block *sb, void __user *addr)
4089da93f9bSEric Sandeen {
4099da93f9bSEric Sandeen 	__u32 flags;
4109da93f9bSEric Sandeen 
4119da93f9bSEric Sandeen 	if (copy_from_user(&flags, addr, sizeof(flags)))
4129da93f9bSEric Sandeen 		return -EFAULT;
4139da93f9bSEric Sandeen 	if (!sb->s_qcop->rm_xquota)
4149da93f9bSEric Sandeen 		return -ENOSYS;
4159da93f9bSEric Sandeen 	return sb->s_qcop->rm_xquota(sb, flags);
4169da93f9bSEric Sandeen }
4179da93f9bSEric Sandeen 
418c411e5f6SChristoph Hellwig /* Copy parameters and call proper function */
419c411e5f6SChristoph Hellwig static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
420f00c9e44SJan Kara 		       void __user *addr, struct path *path)
421c411e5f6SChristoph Hellwig {
422c988afb5SChristoph Hellwig 	int ret;
423c988afb5SChristoph Hellwig 
424c988afb5SChristoph Hellwig 	if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
425c988afb5SChristoph Hellwig 		return -EINVAL;
4262c5f648aSJan Kara 	/*
4272c5f648aSJan Kara 	 * Quota not supported on this fs? Check this before s_quota_types
4282c5f648aSJan Kara 	 * since they needn't be set if quota is not supported at all.
4292c5f648aSJan Kara 	 */
430c988afb5SChristoph Hellwig 	if (!sb->s_qcop)
431c988afb5SChristoph Hellwig 		return -ENOSYS;
4322c5f648aSJan Kara 	if (!(sb->s_quota_types & (1 << type)))
4332c5f648aSJan Kara 		return -EINVAL;
434c988afb5SChristoph Hellwig 
435c988afb5SChristoph Hellwig 	ret = check_quotactl_permission(sb, type, cmd, id);
436c988afb5SChristoph Hellwig 	if (ret < 0)
437c988afb5SChristoph Hellwig 		return ret;
438c988afb5SChristoph Hellwig 
439c411e5f6SChristoph Hellwig 	switch (cmd) {
440c411e5f6SChristoph Hellwig 	case Q_QUOTAON:
441f00c9e44SJan Kara 		return quota_quotaon(sb, type, cmd, id, path);
442c411e5f6SChristoph Hellwig 	case Q_QUOTAOFF:
443f450d4feSChristoph Hellwig 		if (!sb->s_qcop->quota_off)
444f450d4feSChristoph Hellwig 			return -ENOSYS;
445307ae18aSChristoph Hellwig 		return sb->s_qcop->quota_off(sb, type);
446c411e5f6SChristoph Hellwig 	case Q_GETFMT:
447c411e5f6SChristoph Hellwig 		return quota_getfmt(sb, type, addr);
448c411e5f6SChristoph Hellwig 	case Q_GETINFO:
449c411e5f6SChristoph Hellwig 		return quota_getinfo(sb, type, addr);
450c411e5f6SChristoph Hellwig 	case Q_SETINFO:
451c411e5f6SChristoph Hellwig 		return quota_setinfo(sb, type, addr);
452c411e5f6SChristoph Hellwig 	case Q_GETQUOTA:
453c411e5f6SChristoph Hellwig 		return quota_getquota(sb, type, id, addr);
454c411e5f6SChristoph Hellwig 	case Q_SETQUOTA:
455c411e5f6SChristoph Hellwig 		return quota_setquota(sb, type, id, addr);
456c411e5f6SChristoph Hellwig 	case Q_SYNC:
457f450d4feSChristoph Hellwig 		if (!sb->s_qcop->quota_sync)
458f450d4feSChristoph Hellwig 			return -ENOSYS;
459ceed1723SJan Kara 		return sb->s_qcop->quota_sync(sb, type);
460c411e5f6SChristoph Hellwig 	case Q_XQUOTAON:
461*38e478c4SJan Kara 		return quota_enable(sb, addr);
462c411e5f6SChristoph Hellwig 	case Q_XQUOTAOFF:
463*38e478c4SJan Kara 		return quota_disable(sb, addr);
4649da93f9bSEric Sandeen 	case Q_XQUOTARM:
4659da93f9bSEric Sandeen 		return quota_rmxquota(sb, addr);
466c411e5f6SChristoph Hellwig 	case Q_XGETQSTAT:
467c411e5f6SChristoph Hellwig 		return quota_getxstate(sb, addr);
468af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
469af30cb44SChandra Seetharaman 		return quota_getxstatev(sb, addr);
470c411e5f6SChristoph Hellwig 	case Q_XSETQLIM:
471c411e5f6SChristoph Hellwig 		return quota_setxquota(sb, type, id, addr);
472c411e5f6SChristoph Hellwig 	case Q_XGETQUOTA:
473c411e5f6SChristoph Hellwig 		return quota_getxquota(sb, type, id, addr);
474884d179dSJan Kara 	case Q_XQUOTASYNC:
4758c4e4acdSChristoph Hellwig 		if (sb->s_flags & MS_RDONLY)
4768c4e4acdSChristoph Hellwig 			return -EROFS;
4774b217ed9SChristoph Hellwig 		/* XFS quotas are fully coherent now, making this call a noop */
4788c4e4acdSChristoph Hellwig 		return 0;
479884d179dSJan Kara 	default:
480f450d4feSChristoph Hellwig 		return -EINVAL;
481884d179dSJan Kara 	}
482884d179dSJan Kara }
483884d179dSJan Kara 
48456df1278SLee Jones #ifdef CONFIG_BLOCK
48556df1278SLee Jones 
486dcdbed85SJan Kara /* Return 1 if 'cmd' will block on frozen filesystem */
487dcdbed85SJan Kara static int quotactl_cmd_write(int cmd)
488dcdbed85SJan Kara {
489dcdbed85SJan Kara 	switch (cmd) {
490dcdbed85SJan Kara 	case Q_GETFMT:
491dcdbed85SJan Kara 	case Q_GETINFO:
492dcdbed85SJan Kara 	case Q_SYNC:
493dcdbed85SJan Kara 	case Q_XGETQSTAT:
494af30cb44SChandra Seetharaman 	case Q_XGETQSTATV:
495dcdbed85SJan Kara 	case Q_XGETQUOTA:
496dcdbed85SJan Kara 	case Q_XQUOTASYNC:
497dcdbed85SJan Kara 		return 0;
498dcdbed85SJan Kara 	}
499dcdbed85SJan Kara 	return 1;
500dcdbed85SJan Kara }
501dcdbed85SJan Kara 
50256df1278SLee Jones #endif /* CONFIG_BLOCK */
50356df1278SLee Jones 
504884d179dSJan Kara /*
505884d179dSJan Kara  * look up a superblock on which quota ops will be performed
506884d179dSJan Kara  * - use the name of a block device to find the superblock thereon
507884d179dSJan Kara  */
508dcdbed85SJan Kara static struct super_block *quotactl_block(const char __user *special, int cmd)
509884d179dSJan Kara {
510884d179dSJan Kara #ifdef CONFIG_BLOCK
511884d179dSJan Kara 	struct block_device *bdev;
512884d179dSJan Kara 	struct super_block *sb;
51391a27b2aSJeff Layton 	struct filename *tmp = getname(special);
514884d179dSJan Kara 
515884d179dSJan Kara 	if (IS_ERR(tmp))
516884d179dSJan Kara 		return ERR_CAST(tmp);
51791a27b2aSJeff Layton 	bdev = lookup_bdev(tmp->name);
518884d179dSJan Kara 	putname(tmp);
519884d179dSJan Kara 	if (IS_ERR(bdev))
520884d179dSJan Kara 		return ERR_CAST(bdev);
521dcdbed85SJan Kara 	if (quotactl_cmd_write(cmd))
522dcdbed85SJan Kara 		sb = get_super_thawed(bdev);
523dcdbed85SJan Kara 	else
524884d179dSJan Kara 		sb = get_super(bdev);
525884d179dSJan Kara 	bdput(bdev);
526884d179dSJan Kara 	if (!sb)
527884d179dSJan Kara 		return ERR_PTR(-ENODEV);
528884d179dSJan Kara 
529884d179dSJan Kara 	return sb;
530884d179dSJan Kara #else
531884d179dSJan Kara 	return ERR_PTR(-ENODEV);
532884d179dSJan Kara #endif
533884d179dSJan Kara }
534884d179dSJan Kara 
535884d179dSJan Kara /*
536884d179dSJan Kara  * This is the system call interface. This communicates with
537884d179dSJan Kara  * the user-level programs. Currently this only supports diskquota
538884d179dSJan Kara  * calls. Maybe we need to add the process quotas etc. in the future,
539884d179dSJan Kara  * but we probably should use rlimits for that.
540884d179dSJan Kara  */
541884d179dSJan Kara SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
542884d179dSJan Kara 		qid_t, id, void __user *, addr)
543884d179dSJan Kara {
544884d179dSJan Kara 	uint cmds, type;
545884d179dSJan Kara 	struct super_block *sb = NULL;
546f00c9e44SJan Kara 	struct path path, *pathp = NULL;
547884d179dSJan Kara 	int ret;
548884d179dSJan Kara 
549884d179dSJan Kara 	cmds = cmd >> SUBCMDSHIFT;
550884d179dSJan Kara 	type = cmd & SUBCMDMASK;
551884d179dSJan Kara 
5526ae09575SChristoph Hellwig 	/*
5536ae09575SChristoph Hellwig 	 * As a special case Q_SYNC can be called without a specific device.
5546ae09575SChristoph Hellwig 	 * It will iterate all superblocks that have quota enabled and call
5556ae09575SChristoph Hellwig 	 * the sync action on each of them.
5566ae09575SChristoph Hellwig 	 */
5576ae09575SChristoph Hellwig 	if (!special) {
5586ae09575SChristoph Hellwig 		if (cmds == Q_SYNC)
5596ae09575SChristoph Hellwig 			return quota_sync_all(type);
5606ae09575SChristoph Hellwig 		return -ENODEV;
5616ae09575SChristoph Hellwig 	}
5626ae09575SChristoph Hellwig 
563f00c9e44SJan Kara 	/*
564f00c9e44SJan Kara 	 * Path for quotaon has to be resolved before grabbing superblock
565f00c9e44SJan Kara 	 * because that gets s_umount sem which is also possibly needed by path
566f00c9e44SJan Kara 	 * resolution (think about autofs) and thus deadlocks could arise.
567f00c9e44SJan Kara 	 */
568f00c9e44SJan Kara 	if (cmds == Q_QUOTAON) {
569815d405cSTrond Myklebust 		ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
570f00c9e44SJan Kara 		if (ret)
571f00c9e44SJan Kara 			pathp = ERR_PTR(ret);
572f00c9e44SJan Kara 		else
573f00c9e44SJan Kara 			pathp = &path;
574f00c9e44SJan Kara 	}
575f00c9e44SJan Kara 
576dcdbed85SJan Kara 	sb = quotactl_block(special, cmds);
5770aaa6188SJan Kara 	if (IS_ERR(sb)) {
5780aaa6188SJan Kara 		ret = PTR_ERR(sb);
5790aaa6188SJan Kara 		goto out;
5800aaa6188SJan Kara 	}
581884d179dSJan Kara 
582f00c9e44SJan Kara 	ret = do_quotactl(sb, type, cmds, id, addr, pathp);
583884d179dSJan Kara 
5846ae09575SChristoph Hellwig 	drop_super(sb);
5850aaa6188SJan Kara out:
586f00c9e44SJan Kara 	if (pathp && !IS_ERR(pathp))
587f00c9e44SJan Kara 		path_put(pathp);
588884d179dSJan Kara 	return ret;
589884d179dSJan Kara }
590