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