• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Quota code necessary even when VFS quota support is not compiled
4  * into the kernel.  The interesting stuff is over in dquot.c, here
5  * we have symbols for initial quotactl(2) handling, the sysctl(2)
6  * variables, etc - things needed even when quota support disabled.
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/namei.h>
11 #include <linux/slab.h>
12 #include <asm/current.h>
13 #include <linux/uaccess.h>
14 #include <linux/kernel.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 #include <linux/capability.h>
18 #include <linux/quotaops.h>
19 #include <linux/types.h>
20 #include <linux/writeback.h>
21 #include <linux/nospec.h>
22 
check_quotactl_permission(struct super_block * sb,int type,int cmd,qid_t id)23 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
24 				     qid_t id)
25 {
26 	switch (cmd) {
27 	/* these commands do not require any special privilegues */
28 	case Q_GETFMT:
29 	case Q_SYNC:
30 	case Q_GETINFO:
31 	case Q_XGETQSTAT:
32 	case Q_XGETQSTATV:
33 	case Q_XQUOTASYNC:
34 		break;
35 	/* allow to query information for dquots we "own" */
36 	case Q_GETQUOTA:
37 	case Q_XGETQUOTA:
38 		if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
39 		    (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
40 			break;
41 		/*FALLTHROUGH*/
42 	default:
43 		if (!capable(CAP_SYS_ADMIN))
44 			return -EPERM;
45 	}
46 
47 	return security_quotactl(cmd, type, id, sb);
48 }
49 
quota_sync_one(struct super_block * sb,void * arg)50 static void quota_sync_one(struct super_block *sb, void *arg)
51 {
52 	int type = *(int *)arg;
53 
54 	if (sb->s_qcop && sb->s_qcop->quota_sync &&
55 	    (sb->s_quota_types & (1 << type)))
56 		sb->s_qcop->quota_sync(sb, type);
57 }
58 
quota_sync_all(int type)59 static int quota_sync_all(int type)
60 {
61 	int ret;
62 
63 	if (type >= MAXQUOTAS)
64 		return -EINVAL;
65 	ret = security_quotactl(Q_SYNC, type, 0, NULL);
66 	if (!ret)
67 		iterate_supers(quota_sync_one, &type);
68 	return ret;
69 }
70 
qtype_enforce_flag(int type)71 unsigned int qtype_enforce_flag(int type)
72 {
73 	switch (type) {
74 	case USRQUOTA:
75 		return FS_QUOTA_UDQ_ENFD;
76 	case GRPQUOTA:
77 		return FS_QUOTA_GDQ_ENFD;
78 	case PRJQUOTA:
79 		return FS_QUOTA_PDQ_ENFD;
80 	}
81 	return 0;
82 }
83 
quota_quotaon(struct super_block * sb,int type,qid_t id,const struct path * path)84 static int quota_quotaon(struct super_block *sb, int type, qid_t id,
85 		         const struct path *path)
86 {
87 	if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
88 		return -ENOSYS;
89 	if (sb->s_qcop->quota_enable)
90 		return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
91 	if (IS_ERR(path))
92 		return PTR_ERR(path);
93 	return sb->s_qcop->quota_on(sb, type, id, path);
94 }
95 
quota_quotaoff(struct super_block * sb,int type)96 static int quota_quotaoff(struct super_block *sb, int type)
97 {
98 	if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
99 		return -ENOSYS;
100 	if (sb->s_qcop->quota_disable)
101 		return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type));
102 	return sb->s_qcop->quota_off(sb, type);
103 }
104 
quota_getfmt(struct super_block * sb,int type,void __user * addr)105 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
106 {
107 	__u32 fmt;
108 
109 	if (!sb_has_quota_active(sb, type))
110 		return -ESRCH;
111 	fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
112 	if (copy_to_user(addr, &fmt, sizeof(fmt)))
113 		return -EFAULT;
114 	return 0;
115 }
116 
quota_getinfo(struct super_block * sb,int type,void __user * addr)117 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
118 {
119 	struct qc_state state;
120 	struct qc_type_state *tstate;
121 	struct if_dqinfo uinfo;
122 	int ret;
123 
124 	/* This checks whether qc_state has enough entries... */
125 	BUILD_BUG_ON(MAXQUOTAS > XQM_MAXQUOTAS);
126 	if (!sb->s_qcop->get_state)
127 		return -ENOSYS;
128 	ret = sb->s_qcop->get_state(sb, &state);
129 	if (ret)
130 		return ret;
131 	tstate = state.s_state + type;
132 	if (!(tstate->flags & QCI_ACCT_ENABLED))
133 		return -ESRCH;
134 	memset(&uinfo, 0, sizeof(uinfo));
135 	uinfo.dqi_bgrace = tstate->spc_timelimit;
136 	uinfo.dqi_igrace = tstate->ino_timelimit;
137 	if (tstate->flags & QCI_SYSFILE)
138 		uinfo.dqi_flags |= DQF_SYS_FILE;
139 	if (tstate->flags & QCI_ROOT_SQUASH)
140 		uinfo.dqi_flags |= DQF_ROOT_SQUASH;
141 	uinfo.dqi_valid = IIF_ALL;
142 	if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
143 		return -EFAULT;
144 	return 0;
145 }
146 
quota_setinfo(struct super_block * sb,int type,void __user * addr)147 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
148 {
149 	struct if_dqinfo info;
150 	struct qc_info qinfo;
151 
152 	if (copy_from_user(&info, addr, sizeof(info)))
153 		return -EFAULT;
154 	if (!sb->s_qcop->set_info)
155 		return -ENOSYS;
156 	if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
157 		return -EINVAL;
158 	memset(&qinfo, 0, sizeof(qinfo));
159 	if (info.dqi_valid & IIF_FLAGS) {
160 		if (info.dqi_flags & ~DQF_SETINFO_MASK)
161 			return -EINVAL;
162 		if (info.dqi_flags & DQF_ROOT_SQUASH)
163 			qinfo.i_flags |= QCI_ROOT_SQUASH;
164 		qinfo.i_fieldmask |= QC_FLAGS;
165 	}
166 	if (info.dqi_valid & IIF_BGRACE) {
167 		qinfo.i_spc_timelimit = info.dqi_bgrace;
168 		qinfo.i_fieldmask |= QC_SPC_TIMER;
169 	}
170 	if (info.dqi_valid & IIF_IGRACE) {
171 		qinfo.i_ino_timelimit = info.dqi_igrace;
172 		qinfo.i_fieldmask |= QC_INO_TIMER;
173 	}
174 	return sb->s_qcop->set_info(sb, type, &qinfo);
175 }
176 
qbtos(qsize_t blocks)177 static inline qsize_t qbtos(qsize_t blocks)
178 {
179 	return blocks << QIF_DQBLKSIZE_BITS;
180 }
181 
stoqb(qsize_t space)182 static inline qsize_t stoqb(qsize_t space)
183 {
184 	return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
185 }
186 
copy_to_if_dqblk(struct if_dqblk * dst,struct qc_dqblk * src)187 static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
188 {
189 	memset(dst, 0, sizeof(*dst));
190 	dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
191 	dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
192 	dst->dqb_curspace = src->d_space;
193 	dst->dqb_ihardlimit = src->d_ino_hardlimit;
194 	dst->dqb_isoftlimit = src->d_ino_softlimit;
195 	dst->dqb_curinodes = src->d_ino_count;
196 	dst->dqb_btime = src->d_spc_timer;
197 	dst->dqb_itime = src->d_ino_timer;
198 	dst->dqb_valid = QIF_ALL;
199 }
200 
quota_getquota(struct super_block * sb,int type,qid_t id,void __user * addr)201 static int quota_getquota(struct super_block *sb, int type, qid_t id,
202 			  void __user *addr)
203 {
204 	struct kqid qid;
205 	struct qc_dqblk fdq;
206 	struct if_dqblk idq;
207 	int ret;
208 
209 	if (!sb->s_qcop->get_dqblk)
210 		return -ENOSYS;
211 	qid = make_kqid(current_user_ns(), type, id);
212 	if (!qid_has_mapping(sb->s_user_ns, qid))
213 		return -EINVAL;
214 	ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
215 	if (ret)
216 		return ret;
217 	copy_to_if_dqblk(&idq, &fdq);
218 	if (copy_to_user(addr, &idq, sizeof(idq)))
219 		return -EFAULT;
220 	return 0;
221 }
222 
223 /*
224  * Return quota for next active quota >= this id, if any exists,
225  * otherwise return -ENOENT via ->get_nextdqblk
226  */
quota_getnextquota(struct super_block * sb,int type,qid_t id,void __user * addr)227 static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
228 			  void __user *addr)
229 {
230 	struct kqid qid;
231 	struct qc_dqblk fdq;
232 	struct if_nextdqblk idq;
233 	int ret;
234 
235 	if (!sb->s_qcop->get_nextdqblk)
236 		return -ENOSYS;
237 	qid = make_kqid(current_user_ns(), type, id);
238 	if (!qid_has_mapping(sb->s_user_ns, qid))
239 		return -EINVAL;
240 	ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
241 	if (ret)
242 		return ret;
243 	/* struct if_nextdqblk is a superset of struct if_dqblk */
244 	copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq);
245 	idq.dqb_id = from_kqid(current_user_ns(), qid);
246 	if (copy_to_user(addr, &idq, sizeof(idq)))
247 		return -EFAULT;
248 	return 0;
249 }
250 
copy_from_if_dqblk(struct qc_dqblk * dst,struct if_dqblk * src)251 static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
252 {
253 	dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
254 	dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
255 	dst->d_space = src->dqb_curspace;
256 	dst->d_ino_hardlimit = src->dqb_ihardlimit;
257 	dst->d_ino_softlimit = src->dqb_isoftlimit;
258 	dst->d_ino_count = src->dqb_curinodes;
259 	dst->d_spc_timer = src->dqb_btime;
260 	dst->d_ino_timer = src->dqb_itime;
261 
262 	dst->d_fieldmask = 0;
263 	if (src->dqb_valid & QIF_BLIMITS)
264 		dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
265 	if (src->dqb_valid & QIF_SPACE)
266 		dst->d_fieldmask |= QC_SPACE;
267 	if (src->dqb_valid & QIF_ILIMITS)
268 		dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
269 	if (src->dqb_valid & QIF_INODES)
270 		dst->d_fieldmask |= QC_INO_COUNT;
271 	if (src->dqb_valid & QIF_BTIME)
272 		dst->d_fieldmask |= QC_SPC_TIMER;
273 	if (src->dqb_valid & QIF_ITIME)
274 		dst->d_fieldmask |= QC_INO_TIMER;
275 }
276 
quota_setquota(struct super_block * sb,int type,qid_t id,void __user * addr)277 static int quota_setquota(struct super_block *sb, int type, qid_t id,
278 			  void __user *addr)
279 {
280 	struct qc_dqblk fdq;
281 	struct if_dqblk idq;
282 	struct kqid qid;
283 
284 	if (copy_from_user(&idq, addr, sizeof(idq)))
285 		return -EFAULT;
286 	if (!sb->s_qcop->set_dqblk)
287 		return -ENOSYS;
288 	qid = make_kqid(current_user_ns(), type, id);
289 	if (!qid_has_mapping(sb->s_user_ns, qid))
290 		return -EINVAL;
291 	copy_from_if_dqblk(&fdq, &idq);
292 	return sb->s_qcop->set_dqblk(sb, qid, &fdq);
293 }
294 
quota_enable(struct super_block * sb,void __user * addr)295 static int quota_enable(struct super_block *sb, void __user *addr)
296 {
297 	__u32 flags;
298 
299 	if (copy_from_user(&flags, addr, sizeof(flags)))
300 		return -EFAULT;
301 	if (!sb->s_qcop->quota_enable)
302 		return -ENOSYS;
303 	return sb->s_qcop->quota_enable(sb, flags);
304 }
305 
quota_disable(struct super_block * sb,void __user * addr)306 static int quota_disable(struct super_block *sb, void __user *addr)
307 {
308 	__u32 flags;
309 
310 	if (copy_from_user(&flags, addr, sizeof(flags)))
311 		return -EFAULT;
312 	if (!sb->s_qcop->quota_disable)
313 		return -ENOSYS;
314 	return sb->s_qcop->quota_disable(sb, flags);
315 }
316 
quota_state_to_flags(struct qc_state * state)317 static int quota_state_to_flags(struct qc_state *state)
318 {
319 	int flags = 0;
320 
321 	if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
322 		flags |= FS_QUOTA_UDQ_ACCT;
323 	if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
324 		flags |= FS_QUOTA_UDQ_ENFD;
325 	if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
326 		flags |= FS_QUOTA_GDQ_ACCT;
327 	if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
328 		flags |= FS_QUOTA_GDQ_ENFD;
329 	if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
330 		flags |= FS_QUOTA_PDQ_ACCT;
331 	if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
332 		flags |= FS_QUOTA_PDQ_ENFD;
333 	return flags;
334 }
335 
quota_getstate(struct super_block * sb,struct fs_quota_stat * fqs)336 static int quota_getstate(struct super_block *sb, struct fs_quota_stat *fqs)
337 {
338 	int type;
339 	struct qc_state state;
340 	int ret;
341 
342 	memset(&state, 0, sizeof (struct qc_state));
343 	ret = sb->s_qcop->get_state(sb, &state);
344 	if (ret < 0)
345 		return ret;
346 
347 	memset(fqs, 0, sizeof(*fqs));
348 	fqs->qs_version = FS_QSTAT_VERSION;
349 	fqs->qs_flags = quota_state_to_flags(&state);
350 	/* No quota enabled? */
351 	if (!fqs->qs_flags)
352 		return -ENOSYS;
353 	fqs->qs_incoredqs = state.s_incoredqs;
354 	/*
355 	 * GETXSTATE quotactl has space for just one set of time limits so
356 	 * report them for the first enabled quota type
357 	 */
358 	for (type = 0; type < XQM_MAXQUOTAS; type++)
359 		if (state.s_state[type].flags & QCI_ACCT_ENABLED)
360 			break;
361 	BUG_ON(type == XQM_MAXQUOTAS);
362 	fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
363 	fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
364 	fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
365 	fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
366 	fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
367 
368 	/* Inodes may be allocated even if inactive; copy out if present */
369 	if (state.s_state[USRQUOTA].ino) {
370 		fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
371 		fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
372 		fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
373 	}
374 	if (state.s_state[GRPQUOTA].ino) {
375 		fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
376 		fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
377 		fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
378 	}
379 	if (state.s_state[PRJQUOTA].ino) {
380 		/*
381 		 * Q_XGETQSTAT doesn't have room for both group and project
382 		 * quotas.  So, allow the project quota values to be copied out
383 		 * only if there is no group quota information available.
384 		 */
385 		if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
386 			fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
387 			fqs->qs_gquota.qfs_nblks =
388 					state.s_state[PRJQUOTA].blocks;
389 			fqs->qs_gquota.qfs_nextents =
390 					state.s_state[PRJQUOTA].nextents;
391 		}
392 	}
393 	return 0;
394 }
395 
quota_getxstate(struct super_block * sb,void __user * addr)396 static int quota_getxstate(struct super_block *sb, void __user *addr)
397 {
398 	struct fs_quota_stat fqs;
399 	int ret;
400 
401 	if (!sb->s_qcop->get_state)
402 		return -ENOSYS;
403 	ret = quota_getstate(sb, &fqs);
404 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
405 		return -EFAULT;
406 	return ret;
407 }
408 
quota_getstatev(struct super_block * sb,struct fs_quota_statv * fqs)409 static int quota_getstatev(struct super_block *sb, struct fs_quota_statv *fqs)
410 {
411 	int type;
412 	struct qc_state state;
413 	int ret;
414 
415 	memset(&state, 0, sizeof (struct qc_state));
416 	ret = sb->s_qcop->get_state(sb, &state);
417 	if (ret < 0)
418 		return ret;
419 
420 	memset(fqs, 0, sizeof(*fqs));
421 	fqs->qs_version = FS_QSTAT_VERSION;
422 	fqs->qs_flags = quota_state_to_flags(&state);
423 	/* No quota enabled? */
424 	if (!fqs->qs_flags)
425 		return -ENOSYS;
426 	fqs->qs_incoredqs = state.s_incoredqs;
427 	/*
428 	 * GETXSTATV quotactl has space for just one set of time limits so
429 	 * report them for the first enabled quota type
430 	 */
431 	for (type = 0; type < XQM_MAXQUOTAS; type++)
432 		if (state.s_state[type].flags & QCI_ACCT_ENABLED)
433 			break;
434 	BUG_ON(type == XQM_MAXQUOTAS);
435 	fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
436 	fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
437 	fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
438 	fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
439 	fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
440 
441 	/* Inodes may be allocated even if inactive; copy out if present */
442 	if (state.s_state[USRQUOTA].ino) {
443 		fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
444 		fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
445 		fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
446 	}
447 	if (state.s_state[GRPQUOTA].ino) {
448 		fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
449 		fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
450 		fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
451 	}
452 	if (state.s_state[PRJQUOTA].ino) {
453 		fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
454 		fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
455 		fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
456 	}
457 	return 0;
458 }
459 
quota_getxstatev(struct super_block * sb,void __user * addr)460 static int quota_getxstatev(struct super_block *sb, void __user *addr)
461 {
462 	struct fs_quota_statv fqs;
463 	int ret;
464 
465 	if (!sb->s_qcop->get_state)
466 		return -ENOSYS;
467 
468 	memset(&fqs, 0, sizeof(fqs));
469 	if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
470 		return -EFAULT;
471 
472 	/* If this kernel doesn't support user specified version, fail */
473 	switch (fqs.qs_version) {
474 	case FS_QSTATV_VERSION1:
475 		break;
476 	default:
477 		return -EINVAL;
478 	}
479 	ret = quota_getstatev(sb, &fqs);
480 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
481 		return -EFAULT;
482 	return ret;
483 }
484 
485 /*
486  * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
487  * out of there as xfsprogs rely on definitions being in that header file. So
488  * just define same functions here for quota purposes.
489  */
490 #define XFS_BB_SHIFT 9
491 
quota_bbtob(u64 blocks)492 static inline u64 quota_bbtob(u64 blocks)
493 {
494 	return blocks << XFS_BB_SHIFT;
495 }
496 
quota_btobb(u64 bytes)497 static inline u64 quota_btobb(u64 bytes)
498 {
499 	return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
500 }
501 
copy_from_xfs_dqblk(struct qc_dqblk * dst,struct fs_disk_quota * src)502 static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
503 {
504 	dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
505 	dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
506 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
507 	dst->d_ino_softlimit = src->d_ino_softlimit;
508 	dst->d_space = quota_bbtob(src->d_bcount);
509 	dst->d_ino_count = src->d_icount;
510 	dst->d_ino_timer = src->d_itimer;
511 	dst->d_spc_timer = src->d_btimer;
512 	dst->d_ino_warns = src->d_iwarns;
513 	dst->d_spc_warns = src->d_bwarns;
514 	dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
515 	dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
516 	dst->d_rt_space = quota_bbtob(src->d_rtbcount);
517 	dst->d_rt_spc_timer = src->d_rtbtimer;
518 	dst->d_rt_spc_warns = src->d_rtbwarns;
519 	dst->d_fieldmask = 0;
520 	if (src->d_fieldmask & FS_DQ_ISOFT)
521 		dst->d_fieldmask |= QC_INO_SOFT;
522 	if (src->d_fieldmask & FS_DQ_IHARD)
523 		dst->d_fieldmask |= QC_INO_HARD;
524 	if (src->d_fieldmask & FS_DQ_BSOFT)
525 		dst->d_fieldmask |= QC_SPC_SOFT;
526 	if (src->d_fieldmask & FS_DQ_BHARD)
527 		dst->d_fieldmask |= QC_SPC_HARD;
528 	if (src->d_fieldmask & FS_DQ_RTBSOFT)
529 		dst->d_fieldmask |= QC_RT_SPC_SOFT;
530 	if (src->d_fieldmask & FS_DQ_RTBHARD)
531 		dst->d_fieldmask |= QC_RT_SPC_HARD;
532 	if (src->d_fieldmask & FS_DQ_BTIMER)
533 		dst->d_fieldmask |= QC_SPC_TIMER;
534 	if (src->d_fieldmask & FS_DQ_ITIMER)
535 		dst->d_fieldmask |= QC_INO_TIMER;
536 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
537 		dst->d_fieldmask |= QC_RT_SPC_TIMER;
538 	if (src->d_fieldmask & FS_DQ_BWARNS)
539 		dst->d_fieldmask |= QC_SPC_WARNS;
540 	if (src->d_fieldmask & FS_DQ_IWARNS)
541 		dst->d_fieldmask |= QC_INO_WARNS;
542 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
543 		dst->d_fieldmask |= QC_RT_SPC_WARNS;
544 	if (src->d_fieldmask & FS_DQ_BCOUNT)
545 		dst->d_fieldmask |= QC_SPACE;
546 	if (src->d_fieldmask & FS_DQ_ICOUNT)
547 		dst->d_fieldmask |= QC_INO_COUNT;
548 	if (src->d_fieldmask & FS_DQ_RTBCOUNT)
549 		dst->d_fieldmask |= QC_RT_SPACE;
550 }
551 
copy_qcinfo_from_xfs_dqblk(struct qc_info * dst,struct fs_disk_quota * src)552 static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
553 				       struct fs_disk_quota *src)
554 {
555 	memset(dst, 0, sizeof(*dst));
556 	dst->i_spc_timelimit = src->d_btimer;
557 	dst->i_ino_timelimit = src->d_itimer;
558 	dst->i_rt_spc_timelimit = src->d_rtbtimer;
559 	dst->i_ino_warnlimit = src->d_iwarns;
560 	dst->i_spc_warnlimit = src->d_bwarns;
561 	dst->i_rt_spc_warnlimit = src->d_rtbwarns;
562 	if (src->d_fieldmask & FS_DQ_BWARNS)
563 		dst->i_fieldmask |= QC_SPC_WARNS;
564 	if (src->d_fieldmask & FS_DQ_IWARNS)
565 		dst->i_fieldmask |= QC_INO_WARNS;
566 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
567 		dst->i_fieldmask |= QC_RT_SPC_WARNS;
568 	if (src->d_fieldmask & FS_DQ_BTIMER)
569 		dst->i_fieldmask |= QC_SPC_TIMER;
570 	if (src->d_fieldmask & FS_DQ_ITIMER)
571 		dst->i_fieldmask |= QC_INO_TIMER;
572 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
573 		dst->i_fieldmask |= QC_RT_SPC_TIMER;
574 }
575 
quota_setxquota(struct super_block * sb,int type,qid_t id,void __user * addr)576 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
577 			   void __user *addr)
578 {
579 	struct fs_disk_quota fdq;
580 	struct qc_dqblk qdq;
581 	struct kqid qid;
582 
583 	if (copy_from_user(&fdq, addr, sizeof(fdq)))
584 		return -EFAULT;
585 	if (!sb->s_qcop->set_dqblk)
586 		return -ENOSYS;
587 	qid = make_kqid(current_user_ns(), type, id);
588 	if (!qid_has_mapping(sb->s_user_ns, qid))
589 		return -EINVAL;
590 	/* Are we actually setting timer / warning limits for all users? */
591 	if (from_kqid(sb->s_user_ns, qid) == 0 &&
592 	    fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
593 		struct qc_info qinfo;
594 		int ret;
595 
596 		if (!sb->s_qcop->set_info)
597 			return -EINVAL;
598 		copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
599 		ret = sb->s_qcop->set_info(sb, type, &qinfo);
600 		if (ret)
601 			return ret;
602 		/* These are already done */
603 		fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
604 	}
605 	copy_from_xfs_dqblk(&qdq, &fdq);
606 	return sb->s_qcop->set_dqblk(sb, qid, &qdq);
607 }
608 
copy_to_xfs_dqblk(struct fs_disk_quota * dst,struct qc_dqblk * src,int type,qid_t id)609 static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
610 			      int type, qid_t id)
611 {
612 	memset(dst, 0, sizeof(*dst));
613 	dst->d_version = FS_DQUOT_VERSION;
614 	dst->d_id = id;
615 	if (type == USRQUOTA)
616 		dst->d_flags = FS_USER_QUOTA;
617 	else if (type == PRJQUOTA)
618 		dst->d_flags = FS_PROJ_QUOTA;
619 	else
620 		dst->d_flags = FS_GROUP_QUOTA;
621 	dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
622 	dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
623 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
624 	dst->d_ino_softlimit = src->d_ino_softlimit;
625 	dst->d_bcount = quota_btobb(src->d_space);
626 	dst->d_icount = src->d_ino_count;
627 	dst->d_itimer = src->d_ino_timer;
628 	dst->d_btimer = src->d_spc_timer;
629 	dst->d_iwarns = src->d_ino_warns;
630 	dst->d_bwarns = src->d_spc_warns;
631 	dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
632 	dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
633 	dst->d_rtbcount = quota_btobb(src->d_rt_space);
634 	dst->d_rtbtimer = src->d_rt_spc_timer;
635 	dst->d_rtbwarns = src->d_rt_spc_warns;
636 }
637 
quota_getxquota(struct super_block * sb,int type,qid_t id,void __user * addr)638 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
639 			   void __user *addr)
640 {
641 	struct fs_disk_quota fdq;
642 	struct qc_dqblk qdq;
643 	struct kqid qid;
644 	int ret;
645 
646 	if (!sb->s_qcop->get_dqblk)
647 		return -ENOSYS;
648 	qid = make_kqid(current_user_ns(), type, id);
649 	if (!qid_has_mapping(sb->s_user_ns, qid))
650 		return -EINVAL;
651 	ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
652 	if (ret)
653 		return ret;
654 	copy_to_xfs_dqblk(&fdq, &qdq, type, id);
655 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
656 		return -EFAULT;
657 	return ret;
658 }
659 
660 /*
661  * Return quota for next active quota >= this id, if any exists,
662  * otherwise return -ENOENT via ->get_nextdqblk.
663  */
quota_getnextxquota(struct super_block * sb,int type,qid_t id,void __user * addr)664 static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
665 			    void __user *addr)
666 {
667 	struct fs_disk_quota fdq;
668 	struct qc_dqblk qdq;
669 	struct kqid qid;
670 	qid_t id_out;
671 	int ret;
672 
673 	if (!sb->s_qcop->get_nextdqblk)
674 		return -ENOSYS;
675 	qid = make_kqid(current_user_ns(), type, id);
676 	if (!qid_has_mapping(sb->s_user_ns, qid))
677 		return -EINVAL;
678 	ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
679 	if (ret)
680 		return ret;
681 	id_out = from_kqid(current_user_ns(), qid);
682 	copy_to_xfs_dqblk(&fdq, &qdq, type, id_out);
683 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
684 		return -EFAULT;
685 	return ret;
686 }
687 
quota_rmxquota(struct super_block * sb,void __user * addr)688 static int quota_rmxquota(struct super_block *sb, void __user *addr)
689 {
690 	__u32 flags;
691 
692 	if (copy_from_user(&flags, addr, sizeof(flags)))
693 		return -EFAULT;
694 	if (!sb->s_qcop->rm_xquota)
695 		return -ENOSYS;
696 	return sb->s_qcop->rm_xquota(sb, flags);
697 }
698 
699 /* Copy parameters and call proper function */
do_quotactl(struct super_block * sb,int type,int cmd,qid_t id,void __user * addr,const struct path * path)700 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
701 		       void __user *addr, const struct path *path)
702 {
703 	int ret;
704 
705 	if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
706 		return -EINVAL;
707 	type = array_index_nospec(type, MAXQUOTAS);
708 	/*
709 	 * Quota not supported on this fs? Check this before s_quota_types
710 	 * since they needn't be set if quota is not supported at all.
711 	 */
712 	if (!sb->s_qcop)
713 		return -ENOSYS;
714 	if (!(sb->s_quota_types & (1 << type)))
715 		return -EINVAL;
716 
717 	ret = check_quotactl_permission(sb, type, cmd, id);
718 	if (ret < 0)
719 		return ret;
720 
721 	switch (cmd) {
722 	case Q_QUOTAON:
723 		return quota_quotaon(sb, type, id, path);
724 	case Q_QUOTAOFF:
725 		return quota_quotaoff(sb, type);
726 	case Q_GETFMT:
727 		return quota_getfmt(sb, type, addr);
728 	case Q_GETINFO:
729 		return quota_getinfo(sb, type, addr);
730 	case Q_SETINFO:
731 		return quota_setinfo(sb, type, addr);
732 	case Q_GETQUOTA:
733 		return quota_getquota(sb, type, id, addr);
734 	case Q_GETNEXTQUOTA:
735 		return quota_getnextquota(sb, type, id, addr);
736 	case Q_SETQUOTA:
737 		return quota_setquota(sb, type, id, addr);
738 	case Q_SYNC:
739 		if (!sb->s_qcop->quota_sync)
740 			return -ENOSYS;
741 		return sb->s_qcop->quota_sync(sb, type);
742 	case Q_XQUOTAON:
743 		return quota_enable(sb, addr);
744 	case Q_XQUOTAOFF:
745 		return quota_disable(sb, addr);
746 	case Q_XQUOTARM:
747 		return quota_rmxquota(sb, addr);
748 	case Q_XGETQSTAT:
749 		return quota_getxstate(sb, addr);
750 	case Q_XGETQSTATV:
751 		return quota_getxstatev(sb, addr);
752 	case Q_XSETQLIM:
753 		return quota_setxquota(sb, type, id, addr);
754 	case Q_XGETQUOTA:
755 		return quota_getxquota(sb, type, id, addr);
756 	case Q_XGETNEXTQUOTA:
757 		return quota_getnextxquota(sb, type, id, addr);
758 	case Q_XQUOTASYNC:
759 		if (sb_rdonly(sb))
760 			return -EROFS;
761 		/* XFS quotas are fully coherent now, making this call a noop */
762 		return 0;
763 	default:
764 		return -EINVAL;
765 	}
766 }
767 
768 #ifdef CONFIG_BLOCK
769 
770 /* Return 1 if 'cmd' will block on frozen filesystem */
quotactl_cmd_write(int cmd)771 static int quotactl_cmd_write(int cmd)
772 {
773 	/*
774 	 * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access
775 	 * as dquot_acquire() may allocate space for new structure and OCFS2
776 	 * needs to increment on-disk use count.
777 	 */
778 	switch (cmd) {
779 	case Q_GETFMT:
780 	case Q_GETINFO:
781 	case Q_SYNC:
782 	case Q_XGETQSTAT:
783 	case Q_XGETQSTATV:
784 	case Q_XGETQUOTA:
785 	case Q_XGETNEXTQUOTA:
786 	case Q_XQUOTASYNC:
787 		return 0;
788 	}
789 	return 1;
790 }
791 #endif /* CONFIG_BLOCK */
792 
793 /* Return true if quotactl command is manipulating quota on/off state */
quotactl_cmd_onoff(int cmd)794 static bool quotactl_cmd_onoff(int cmd)
795 {
796 	return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
797 		 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
798 }
799 
800 /*
801  * look up a superblock on which quota ops will be performed
802  * - use the name of a block device to find the superblock thereon
803  */
quotactl_block(const char __user * special,int cmd)804 static struct super_block *quotactl_block(const char __user *special, int cmd)
805 {
806 #ifdef CONFIG_BLOCK
807 	struct block_device *bdev;
808 	struct super_block *sb;
809 	struct filename *tmp = getname(special);
810 
811 	if (IS_ERR(tmp))
812 		return ERR_CAST(tmp);
813 	bdev = lookup_bdev(tmp->name);
814 	putname(tmp);
815 	if (IS_ERR(bdev))
816 		return ERR_CAST(bdev);
817 	if (quotactl_cmd_onoff(cmd))
818 		sb = get_super_exclusive_thawed(bdev);
819 	else if (quotactl_cmd_write(cmd))
820 		sb = get_super_thawed(bdev);
821 	else
822 		sb = get_super(bdev);
823 	bdput(bdev);
824 	if (!sb)
825 		return ERR_PTR(-ENODEV);
826 
827 	return sb;
828 #else
829 	return ERR_PTR(-ENODEV);
830 #endif
831 }
832 
833 /*
834  * This is the system call interface. This communicates with
835  * the user-level programs. Currently this only supports diskquota
836  * calls. Maybe we need to add the process quotas etc. in the future,
837  * but we probably should use rlimits for that.
838  */
SYSCALL_DEFINE4(quotactl,unsigned int,cmd,const char __user *,special,qid_t,id,void __user *,addr)839 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
840 		qid_t, id, void __user *, addr)
841 {
842 	uint cmds, type;
843 	struct super_block *sb = NULL;
844 	struct path path, *pathp = NULL;
845 	int ret;
846 
847 	cmds = cmd >> SUBCMDSHIFT;
848 	type = cmd & SUBCMDMASK;
849 
850 	/*
851 	 * As a special case Q_SYNC can be called without a specific device.
852 	 * It will iterate all superblocks that have quota enabled and call
853 	 * the sync action on each of them.
854 	 */
855 	if (!special) {
856 		if (cmds == Q_SYNC)
857 			return quota_sync_all(type);
858 		return -ENODEV;
859 	}
860 
861 	/*
862 	 * Path for quotaon has to be resolved before grabbing superblock
863 	 * because that gets s_umount sem which is also possibly needed by path
864 	 * resolution (think about autofs) and thus deadlocks could arise.
865 	 */
866 	if (cmds == Q_QUOTAON) {
867 		ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
868 		if (ret)
869 			pathp = ERR_PTR(ret);
870 		else
871 			pathp = &path;
872 	}
873 
874 	sb = quotactl_block(special, cmds);
875 	if (IS_ERR(sb)) {
876 		ret = PTR_ERR(sb);
877 		goto out;
878 	}
879 
880 	ret = do_quotactl(sb, type, cmds, id, addr, pathp);
881 
882 	if (!quotactl_cmd_onoff(cmds))
883 		drop_super(sb);
884 	else
885 		drop_super_exclusive(sb);
886 out:
887 	if (pathp && !IS_ERR(pathp))
888 		path_put(pathp);
889 	return ret;
890 }
891