xref: /openbmc/linux/fs/quota/quota.c (revision b77a69b8)
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 inline s64 copy_from_xfs_dqblk_ts(const struct fs_disk_quota *d,
536 		__s32 timer, __s8 timer_hi)
537 {
538 	if (d->d_fieldmask & FS_DQ_BIGTIME)
539 		return (u32)timer | (s64)timer_hi << 32;
540 	return timer;
541 }
542 
543 static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
544 {
545 	dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
546 	dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
547 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
548 	dst->d_ino_softlimit = src->d_ino_softlimit;
549 	dst->d_space = quota_bbtob(src->d_bcount);
550 	dst->d_ino_count = src->d_icount;
551 	dst->d_ino_timer = copy_from_xfs_dqblk_ts(src, src->d_itimer,
552 						  src->d_itimer_hi);
553 	dst->d_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_btimer,
554 						  src->d_btimer_hi);
555 	dst->d_ino_warns = src->d_iwarns;
556 	dst->d_spc_warns = src->d_bwarns;
557 	dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
558 	dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
559 	dst->d_rt_space = quota_bbtob(src->d_rtbcount);
560 	dst->d_rt_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_rtbtimer,
561 						     src->d_rtbtimer_hi);
562 	dst->d_rt_spc_warns = src->d_rtbwarns;
563 	dst->d_fieldmask = 0;
564 	if (src->d_fieldmask & FS_DQ_ISOFT)
565 		dst->d_fieldmask |= QC_INO_SOFT;
566 	if (src->d_fieldmask & FS_DQ_IHARD)
567 		dst->d_fieldmask |= QC_INO_HARD;
568 	if (src->d_fieldmask & FS_DQ_BSOFT)
569 		dst->d_fieldmask |= QC_SPC_SOFT;
570 	if (src->d_fieldmask & FS_DQ_BHARD)
571 		dst->d_fieldmask |= QC_SPC_HARD;
572 	if (src->d_fieldmask & FS_DQ_RTBSOFT)
573 		dst->d_fieldmask |= QC_RT_SPC_SOFT;
574 	if (src->d_fieldmask & FS_DQ_RTBHARD)
575 		dst->d_fieldmask |= QC_RT_SPC_HARD;
576 	if (src->d_fieldmask & FS_DQ_BTIMER)
577 		dst->d_fieldmask |= QC_SPC_TIMER;
578 	if (src->d_fieldmask & FS_DQ_ITIMER)
579 		dst->d_fieldmask |= QC_INO_TIMER;
580 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
581 		dst->d_fieldmask |= QC_RT_SPC_TIMER;
582 	if (src->d_fieldmask & FS_DQ_BWARNS)
583 		dst->d_fieldmask |= QC_SPC_WARNS;
584 	if (src->d_fieldmask & FS_DQ_IWARNS)
585 		dst->d_fieldmask |= QC_INO_WARNS;
586 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
587 		dst->d_fieldmask |= QC_RT_SPC_WARNS;
588 	if (src->d_fieldmask & FS_DQ_BCOUNT)
589 		dst->d_fieldmask |= QC_SPACE;
590 	if (src->d_fieldmask & FS_DQ_ICOUNT)
591 		dst->d_fieldmask |= QC_INO_COUNT;
592 	if (src->d_fieldmask & FS_DQ_RTBCOUNT)
593 		dst->d_fieldmask |= QC_RT_SPACE;
594 }
595 
596 static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
597 				       struct fs_disk_quota *src)
598 {
599 	memset(dst, 0, sizeof(*dst));
600 	dst->i_spc_timelimit = src->d_btimer;
601 	dst->i_ino_timelimit = src->d_itimer;
602 	dst->i_rt_spc_timelimit = src->d_rtbtimer;
603 	dst->i_ino_warnlimit = src->d_iwarns;
604 	dst->i_spc_warnlimit = src->d_bwarns;
605 	dst->i_rt_spc_warnlimit = src->d_rtbwarns;
606 	if (src->d_fieldmask & FS_DQ_BWARNS)
607 		dst->i_fieldmask |= QC_SPC_WARNS;
608 	if (src->d_fieldmask & FS_DQ_IWARNS)
609 		dst->i_fieldmask |= QC_INO_WARNS;
610 	if (src->d_fieldmask & FS_DQ_RTBWARNS)
611 		dst->i_fieldmask |= QC_RT_SPC_WARNS;
612 	if (src->d_fieldmask & FS_DQ_BTIMER)
613 		dst->i_fieldmask |= QC_SPC_TIMER;
614 	if (src->d_fieldmask & FS_DQ_ITIMER)
615 		dst->i_fieldmask |= QC_INO_TIMER;
616 	if (src->d_fieldmask & FS_DQ_RTBTIMER)
617 		dst->i_fieldmask |= QC_RT_SPC_TIMER;
618 }
619 
620 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
621 			   void __user *addr)
622 {
623 	struct fs_disk_quota fdq;
624 	struct qc_dqblk qdq;
625 	struct kqid qid;
626 
627 	if (copy_from_user(&fdq, addr, sizeof(fdq)))
628 		return -EFAULT;
629 	if (!sb->s_qcop->set_dqblk)
630 		return -ENOSYS;
631 	qid = make_kqid(current_user_ns(), type, id);
632 	if (!qid_has_mapping(sb->s_user_ns, qid))
633 		return -EINVAL;
634 	/* Are we actually setting timer / warning limits for all users? */
635 	if (from_kqid(sb->s_user_ns, qid) == 0 &&
636 	    fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
637 		struct qc_info qinfo;
638 		int ret;
639 
640 		if (!sb->s_qcop->set_info)
641 			return -EINVAL;
642 		copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
643 		ret = sb->s_qcop->set_info(sb, type, &qinfo);
644 		if (ret)
645 			return ret;
646 		/* These are already done */
647 		fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
648 	}
649 	copy_from_xfs_dqblk(&qdq, &fdq);
650 	return sb->s_qcop->set_dqblk(sb, qid, &qdq);
651 }
652 
653 static inline void copy_to_xfs_dqblk_ts(const struct fs_disk_quota *d,
654 		__s32 *timer_lo, __s8 *timer_hi, s64 timer)
655 {
656 	*timer_lo = timer;
657 	if (d->d_fieldmask & FS_DQ_BIGTIME)
658 		*timer_hi = timer >> 32;
659 }
660 
661 static inline bool want_bigtime(s64 timer)
662 {
663 	return timer > S32_MAX || timer < S32_MIN;
664 }
665 
666 static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
667 			      int type, qid_t id)
668 {
669 	memset(dst, 0, sizeof(*dst));
670 	if (want_bigtime(src->d_ino_timer) || want_bigtime(src->d_spc_timer) ||
671 	    want_bigtime(src->d_rt_spc_timer))
672 		dst->d_fieldmask |= FS_DQ_BIGTIME;
673 	dst->d_version = FS_DQUOT_VERSION;
674 	dst->d_id = id;
675 	if (type == USRQUOTA)
676 		dst->d_flags = FS_USER_QUOTA;
677 	else if (type == PRJQUOTA)
678 		dst->d_flags = FS_PROJ_QUOTA;
679 	else
680 		dst->d_flags = FS_GROUP_QUOTA;
681 	dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
682 	dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
683 	dst->d_ino_hardlimit = src->d_ino_hardlimit;
684 	dst->d_ino_softlimit = src->d_ino_softlimit;
685 	dst->d_bcount = quota_btobb(src->d_space);
686 	dst->d_icount = src->d_ino_count;
687 	copy_to_xfs_dqblk_ts(dst, &dst->d_itimer, &dst->d_itimer_hi,
688 			     src->d_ino_timer);
689 	copy_to_xfs_dqblk_ts(dst, &dst->d_btimer, &dst->d_btimer_hi,
690 			     src->d_spc_timer);
691 	dst->d_iwarns = src->d_ino_warns;
692 	dst->d_bwarns = src->d_spc_warns;
693 	dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
694 	dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
695 	dst->d_rtbcount = quota_btobb(src->d_rt_space);
696 	copy_to_xfs_dqblk_ts(dst, &dst->d_rtbtimer, &dst->d_rtbtimer_hi,
697 			     src->d_rt_spc_timer);
698 	dst->d_rtbwarns = src->d_rt_spc_warns;
699 }
700 
701 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
702 			   void __user *addr)
703 {
704 	struct fs_disk_quota fdq;
705 	struct qc_dqblk qdq;
706 	struct kqid qid;
707 	int ret;
708 
709 	if (!sb->s_qcop->get_dqblk)
710 		return -ENOSYS;
711 	qid = make_kqid(current_user_ns(), type, id);
712 	if (!qid_has_mapping(sb->s_user_ns, qid))
713 		return -EINVAL;
714 	ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
715 	if (ret)
716 		return ret;
717 	copy_to_xfs_dqblk(&fdq, &qdq, type, id);
718 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
719 		return -EFAULT;
720 	return ret;
721 }
722 
723 /*
724  * Return quota for next active quota >= this id, if any exists,
725  * otherwise return -ENOENT via ->get_nextdqblk.
726  */
727 static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
728 			    void __user *addr)
729 {
730 	struct fs_disk_quota fdq;
731 	struct qc_dqblk qdq;
732 	struct kqid qid;
733 	qid_t id_out;
734 	int ret;
735 
736 	if (!sb->s_qcop->get_nextdqblk)
737 		return -ENOSYS;
738 	qid = make_kqid(current_user_ns(), type, id);
739 	if (!qid_has_mapping(sb->s_user_ns, qid))
740 		return -EINVAL;
741 	ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
742 	if (ret)
743 		return ret;
744 	id_out = from_kqid(current_user_ns(), qid);
745 	copy_to_xfs_dqblk(&fdq, &qdq, type, id_out);
746 	if (copy_to_user(addr, &fdq, sizeof(fdq)))
747 		return -EFAULT;
748 	return ret;
749 }
750 
751 static int quota_rmxquota(struct super_block *sb, void __user *addr)
752 {
753 	__u32 flags;
754 
755 	if (copy_from_user(&flags, addr, sizeof(flags)))
756 		return -EFAULT;
757 	if (!sb->s_qcop->rm_xquota)
758 		return -ENOSYS;
759 	return sb->s_qcop->rm_xquota(sb, flags);
760 }
761 
762 /* Copy parameters and call proper function */
763 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
764 		       void __user *addr, const struct path *path)
765 {
766 	int ret;
767 
768 	type = array_index_nospec(type, MAXQUOTAS);
769 	/*
770 	 * Quota not supported on this fs? Check this before s_quota_types
771 	 * since they needn't be set if quota is not supported at all.
772 	 */
773 	if (!sb->s_qcop)
774 		return -ENOSYS;
775 	if (!(sb->s_quota_types & (1 << type)))
776 		return -EINVAL;
777 
778 	ret = check_quotactl_permission(sb, type, cmd, id);
779 	if (ret < 0)
780 		return ret;
781 
782 	switch (cmd) {
783 	case Q_QUOTAON:
784 		return quota_quotaon(sb, type, id, path);
785 	case Q_QUOTAOFF:
786 		return quota_quotaoff(sb, type);
787 	case Q_GETFMT:
788 		return quota_getfmt(sb, type, addr);
789 	case Q_GETINFO:
790 		return quota_getinfo(sb, type, addr);
791 	case Q_SETINFO:
792 		return quota_setinfo(sb, type, addr);
793 	case Q_GETQUOTA:
794 		return quota_getquota(sb, type, id, addr);
795 	case Q_GETNEXTQUOTA:
796 		return quota_getnextquota(sb, type, id, addr);
797 	case Q_SETQUOTA:
798 		return quota_setquota(sb, type, id, addr);
799 	case Q_SYNC:
800 		if (!sb->s_qcop->quota_sync)
801 			return -ENOSYS;
802 		return sb->s_qcop->quota_sync(sb, type);
803 	case Q_XQUOTAON:
804 		return quota_enable(sb, addr);
805 	case Q_XQUOTAOFF:
806 		return quota_disable(sb, addr);
807 	case Q_XQUOTARM:
808 		return quota_rmxquota(sb, addr);
809 	case Q_XGETQSTAT:
810 		return quota_getxstate(sb, type, addr);
811 	case Q_XGETQSTATV:
812 		return quota_getxstatev(sb, type, addr);
813 	case Q_XSETQLIM:
814 		return quota_setxquota(sb, type, id, addr);
815 	case Q_XGETQUOTA:
816 		return quota_getxquota(sb, type, id, addr);
817 	case Q_XGETNEXTQUOTA:
818 		return quota_getnextxquota(sb, type, id, addr);
819 	case Q_XQUOTASYNC:
820 		if (sb_rdonly(sb))
821 			return -EROFS;
822 		/* XFS quotas are fully coherent now, making this call a noop */
823 		return 0;
824 	default:
825 		return -EINVAL;
826 	}
827 }
828 
829 #ifdef CONFIG_BLOCK
830 
831 /* Return 1 if 'cmd' will block on frozen filesystem */
832 static int quotactl_cmd_write(int cmd)
833 {
834 	/*
835 	 * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access
836 	 * as dquot_acquire() may allocate space for new structure and OCFS2
837 	 * needs to increment on-disk use count.
838 	 */
839 	switch (cmd) {
840 	case Q_GETFMT:
841 	case Q_GETINFO:
842 	case Q_SYNC:
843 	case Q_XGETQSTAT:
844 	case Q_XGETQSTATV:
845 	case Q_XGETQUOTA:
846 	case Q_XGETNEXTQUOTA:
847 	case Q_XQUOTASYNC:
848 		return 0;
849 	}
850 	return 1;
851 }
852 #endif /* CONFIG_BLOCK */
853 
854 /* Return true if quotactl command is manipulating quota on/off state */
855 static bool quotactl_cmd_onoff(int cmd)
856 {
857 	return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
858 		 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
859 }
860 
861 /*
862  * look up a superblock on which quota ops will be performed
863  * - use the name of a block device to find the superblock thereon
864  */
865 static struct super_block *quotactl_block(const char __user *special, int cmd)
866 {
867 #ifdef CONFIG_BLOCK
868 	struct block_device *bdev;
869 	struct super_block *sb;
870 	struct filename *tmp = getname(special);
871 
872 	if (IS_ERR(tmp))
873 		return ERR_CAST(tmp);
874 	bdev = lookup_bdev(tmp->name);
875 	putname(tmp);
876 	if (IS_ERR(bdev))
877 		return ERR_CAST(bdev);
878 	if (quotactl_cmd_onoff(cmd))
879 		sb = get_super_exclusive_thawed(bdev);
880 	else if (quotactl_cmd_write(cmd))
881 		sb = get_super_thawed(bdev);
882 	else
883 		sb = get_super(bdev);
884 	bdput(bdev);
885 	if (!sb)
886 		return ERR_PTR(-ENODEV);
887 
888 	return sb;
889 #else
890 	return ERR_PTR(-ENODEV);
891 #endif
892 }
893 
894 /*
895  * This is the system call interface. This communicates with
896  * the user-level programs. Currently this only supports diskquota
897  * calls. Maybe we need to add the process quotas etc. in the future,
898  * but we probably should use rlimits for that.
899  */
900 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
901 		qid_t, id, void __user *, addr)
902 {
903 	uint cmds, type;
904 	struct super_block *sb = NULL;
905 	struct path path, *pathp = NULL;
906 	int ret;
907 
908 	cmds = cmd >> SUBCMDSHIFT;
909 	type = cmd & SUBCMDMASK;
910 
911 	if (type >= MAXQUOTAS)
912 		return -EINVAL;
913 
914 	/*
915 	 * As a special case Q_SYNC can be called without a specific device.
916 	 * It will iterate all superblocks that have quota enabled and call
917 	 * the sync action on each of them.
918 	 */
919 	if (!special) {
920 		if (cmds == Q_SYNC)
921 			return quota_sync_all(type);
922 		return -ENODEV;
923 	}
924 
925 	/*
926 	 * Path for quotaon has to be resolved before grabbing superblock
927 	 * because that gets s_umount sem which is also possibly needed by path
928 	 * resolution (think about autofs) and thus deadlocks could arise.
929 	 */
930 	if (cmds == Q_QUOTAON) {
931 		ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
932 		if (ret)
933 			pathp = ERR_PTR(ret);
934 		else
935 			pathp = &path;
936 	}
937 
938 	sb = quotactl_block(special, cmds);
939 	if (IS_ERR(sb)) {
940 		ret = PTR_ERR(sb);
941 		goto out;
942 	}
943 
944 	ret = do_quotactl(sb, type, cmds, id, addr, pathp);
945 
946 	if (!quotactl_cmd_onoff(cmds))
947 		drop_super(sb);
948 	else
949 		drop_super_exclusive(sb);
950 out:
951 	if (pathp && !IS_ERR(pathp))
952 		path_put(pathp);
953 	return ret;
954 }
955