1 // SPDX-License-Identifier: GPL-2.0
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.
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>
24 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
28 /* these commands do not require any special privilegues */
36 /* allow to query information for dquots we "own" */
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))))
44 if (!capable(CAP_SYS_ADMIN))
48 return security_quotactl(cmd, type, id, sb);
51 static void quota_sync_one(struct super_block *sb, void *arg)
53 int type = *(int *)arg;
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);
60 static int quota_sync_all(int type)
64 ret = security_quotactl(Q_SYNC, type, 0, NULL);
66 iterate_supers(quota_sync_one, &type);
70 unsigned int qtype_enforce_flag(int type)
74 return FS_QUOTA_UDQ_ENFD;
76 return FS_QUOTA_GDQ_ENFD;
78 return FS_QUOTA_PDQ_ENFD;
83 static int quota_quotaon(struct super_block *sb, int type, qid_t id,
84 const struct path *path)
86 if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
88 if (sb->s_qcop->quota_enable)
89 return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
92 return sb->s_qcop->quota_on(sb, type, id, path);
95 static int quota_quotaoff(struct super_block *sb, int type)
97 if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
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);
104 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
108 if (!sb_has_quota_active(sb, type))
110 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
111 if (copy_to_user(addr, &fmt, sizeof(fmt)))
116 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
118 struct qc_state state;
119 struct qc_type_state *tstate;
120 struct if_dqinfo uinfo;
123 if (!sb->s_qcop->get_state)
125 ret = sb->s_qcop->get_state(sb, &state);
128 tstate = state.s_state + type;
129 if (!(tstate->flags & QCI_ACCT_ENABLED))
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)))
144 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
146 struct if_dqinfo info;
147 struct qc_info qinfo;
149 if (copy_from_user(&info, addr, sizeof(info)))
151 if (!sb->s_qcop->set_info)
153 if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
155 memset(&qinfo, 0, sizeof(qinfo));
156 if (info.dqi_valid & IIF_FLAGS) {
157 if (info.dqi_flags & ~DQF_SETINFO_MASK)
159 if (info.dqi_flags & DQF_ROOT_SQUASH)
160 qinfo.i_flags |= QCI_ROOT_SQUASH;
161 qinfo.i_fieldmask |= QC_FLAGS;
163 if (info.dqi_valid & IIF_BGRACE) {
164 qinfo.i_spc_timelimit = info.dqi_bgrace;
165 qinfo.i_fieldmask |= QC_SPC_TIMER;
167 if (info.dqi_valid & IIF_IGRACE) {
168 qinfo.i_ino_timelimit = info.dqi_igrace;
169 qinfo.i_fieldmask |= QC_INO_TIMER;
171 return sb->s_qcop->set_info(sb, type, &qinfo);
174 static inline qsize_t qbtos(qsize_t blocks)
176 return blocks << QIF_DQBLKSIZE_BITS;
179 static inline qsize_t stoqb(qsize_t space)
181 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
184 static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
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;
198 static int quota_getquota(struct super_block *sb, int type, qid_t id,
206 if (!sb->s_qcop->get_dqblk)
208 qid = make_kqid(current_user_ns(), type, id);
209 if (!qid_has_mapping(sb->s_user_ns, qid))
211 ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
214 copy_to_if_dqblk(&idq, &fdq);
216 if (compat_need_64bit_alignment_fixup()) {
217 struct compat_if_dqblk __user *compat_dqblk = addr;
219 if (copy_to_user(compat_dqblk, &idq, sizeof(*compat_dqblk)))
221 if (put_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
224 if (copy_to_user(addr, &idq, sizeof(idq)))
231 * Return quota for next active quota >= this id, if any exists,
232 * otherwise return -ENOENT via ->get_nextdqblk
234 static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
239 struct if_nextdqblk idq;
242 if (!sb->s_qcop->get_nextdqblk)
244 qid = make_kqid(current_user_ns(), type, id);
245 if (!qid_has_mapping(sb->s_user_ns, qid))
247 ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
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)))
258 static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
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;
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;
284 static int quota_setquota(struct super_block *sb, int type, qid_t id,
291 if (compat_need_64bit_alignment_fixup()) {
292 struct compat_if_dqblk __user *compat_dqblk = addr;
294 if (copy_from_user(&idq, compat_dqblk, sizeof(*compat_dqblk)) ||
295 get_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
298 if (copy_from_user(&idq, addr, sizeof(idq)))
301 if (!sb->s_qcop->set_dqblk)
303 qid = make_kqid(current_user_ns(), type, id);
304 if (!qid_has_mapping(sb->s_user_ns, qid))
306 copy_from_if_dqblk(&fdq, &idq);
307 return sb->s_qcop->set_dqblk(sb, qid, &fdq);
310 static int quota_enable(struct super_block *sb, void __user *addr)
314 if (copy_from_user(&flags, addr, sizeof(flags)))
316 if (!sb->s_qcop->quota_enable)
318 return sb->s_qcop->quota_enable(sb, flags);
321 static int quota_disable(struct super_block *sb, void __user *addr)
325 if (copy_from_user(&flags, addr, sizeof(flags)))
327 if (!sb->s_qcop->quota_disable)
329 return sb->s_qcop->quota_disable(sb, flags);
332 static int quota_state_to_flags(struct qc_state *state)
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;
351 static int quota_getstate(struct super_block *sb, int type,
352 struct fs_quota_stat *fqs)
354 struct qc_state state;
357 memset(&state, 0, sizeof (struct qc_state));
358 ret = sb->s_qcop->get_state(sb, &state);
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? */
368 fqs->qs_incoredqs = state.s_incoredqs;
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;
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;
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;
387 if (state.s_state[PRJQUOTA].ino) {
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.
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;
404 static int compat_copy_fs_qfilestat(struct compat_fs_qfilestat __user *to,
405 struct fs_qfilestat *from)
407 if (copy_to_user(to, from, sizeof(*to)) ||
408 put_user(from->qfs_nextents, &to->qfs_nextents))
413 static int compat_copy_fs_quota_stat(struct compat_fs_quota_stat __user *to,
414 struct fs_quota_stat *from)
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))
431 static int quota_getxstate(struct super_block *sb, int type, void __user *addr)
433 struct fs_quota_stat fqs;
436 if (!sb->s_qcop->get_state)
438 ret = quota_getstate(sb, type, &fqs);
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)))
449 static int quota_getstatev(struct super_block *sb, int type,
450 struct fs_quota_statv *fqs)
452 struct qc_state state;
455 memset(&state, 0, sizeof (struct qc_state));
456 ret = sb->s_qcop->get_state(sb, &state);
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? */
466 fqs->qs_incoredqs = state.s_incoredqs;
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;
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;
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;
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;
493 static int quota_getxstatev(struct super_block *sb, int type, void __user *addr)
495 struct fs_quota_statv fqs;
498 if (!sb->s_qcop->get_state)
501 memset(&fqs, 0, sizeof(fqs));
502 if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
505 /* If this kernel doesn't support user specified version, fail */
506 switch (fqs.qs_version) {
507 case FS_QSTATV_VERSION1:
512 ret = quota_getstatev(sb, type, &fqs);
513 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
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.
523 #define XFS_BB_SHIFT 9
525 static inline u64 quota_bbtob(u64 blocks)
527 return blocks << XFS_BB_SHIFT;
530 static inline u64 quota_btobb(u64 bytes)
532 return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
535 static inline s64 copy_from_xfs_dqblk_ts(const struct fs_disk_quota *d,
536 __s32 timer, __s8 timer_hi)
538 if (d->d_fieldmask & FS_DQ_BIGTIME)
539 return (u32)timer | (s64)timer_hi << 32;
543 static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
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,
553 dst->d_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_btimer,
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,
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;
596 static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
597 struct fs_disk_quota *src)
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;
620 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
623 struct fs_disk_quota fdq;
627 if (copy_from_user(&fdq, addr, sizeof(fdq)))
629 if (!sb->s_qcop->set_dqblk)
631 qid = make_kqid(current_user_ns(), type, id);
632 if (!qid_has_mapping(sb->s_user_ns, qid))
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;
640 if (!sb->s_qcop->set_info)
642 copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
643 ret = sb->s_qcop->set_info(sb, type, &qinfo);
646 /* These are already done */
647 fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
649 copy_from_xfs_dqblk(&qdq, &fdq);
650 return sb->s_qcop->set_dqblk(sb, qid, &qdq);
653 static inline void copy_to_xfs_dqblk_ts(const struct fs_disk_quota *d,
654 __s32 *timer_lo, __s8 *timer_hi, s64 timer)
657 if (d->d_fieldmask & FS_DQ_BIGTIME)
658 *timer_hi = timer >> 32;
661 static inline bool want_bigtime(s64 timer)
663 return timer > S32_MAX || timer < S32_MIN;
666 static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
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;
675 if (type == USRQUOTA)
676 dst->d_flags = FS_USER_QUOTA;
677 else if (type == PRJQUOTA)
678 dst->d_flags = FS_PROJ_QUOTA;
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,
689 copy_to_xfs_dqblk_ts(dst, &dst->d_btimer, &dst->d_btimer_hi,
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;
701 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
704 struct fs_disk_quota fdq;
709 if (!sb->s_qcop->get_dqblk)
711 qid = make_kqid(current_user_ns(), type, id);
712 if (!qid_has_mapping(sb->s_user_ns, qid))
714 ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
717 copy_to_xfs_dqblk(&fdq, &qdq, type, id);
718 if (copy_to_user(addr, &fdq, sizeof(fdq)))
724 * Return quota for next active quota >= this id, if any exists,
725 * otherwise return -ENOENT via ->get_nextdqblk.
727 static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
730 struct fs_disk_quota fdq;
736 if (!sb->s_qcop->get_nextdqblk)
738 qid = make_kqid(current_user_ns(), type, id);
739 if (!qid_has_mapping(sb->s_user_ns, qid))
741 ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
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)))
751 static int quota_rmxquota(struct super_block *sb, void __user *addr)
755 if (copy_from_user(&flags, addr, sizeof(flags)))
757 if (!sb->s_qcop->rm_xquota)
759 return sb->s_qcop->rm_xquota(sb, flags);
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)
768 type = array_index_nospec(type, MAXQUOTAS);
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.
775 if (!(sb->s_quota_types & (1 << type)))
778 ret = check_quotactl_permission(sb, type, cmd, id);
784 return quota_quotaon(sb, type, id, path);
786 return quota_quotaoff(sb, type);
788 return quota_getfmt(sb, type, addr);
790 return quota_getinfo(sb, type, addr);
792 return quota_setinfo(sb, type, addr);
794 return quota_getquota(sb, type, id, addr);
796 return quota_getnextquota(sb, type, id, addr);
798 return quota_setquota(sb, type, id, addr);
800 if (!sb->s_qcop->quota_sync)
802 return sb->s_qcop->quota_sync(sb, type);
804 return quota_enable(sb, addr);
806 return quota_disable(sb, addr);
808 return quota_rmxquota(sb, addr);
810 return quota_getxstate(sb, type, addr);
812 return quota_getxstatev(sb, type, addr);
814 return quota_setxquota(sb, type, id, addr);
816 return quota_getxquota(sb, type, id, addr);
817 case Q_XGETNEXTQUOTA:
818 return quota_getnextxquota(sb, type, id, addr);
822 /* XFS quotas are fully coherent now, making this call a noop */
831 /* Return 1 if 'cmd' will block on frozen filesystem */
832 static int quotactl_cmd_write(int cmd)
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.
846 case Q_XGETNEXTQUOTA:
852 #endif /* CONFIG_BLOCK */
854 /* Return true if quotactl command is manipulating quota on/off state */
855 static bool quotactl_cmd_onoff(int cmd)
857 return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
858 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
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
865 static struct super_block *quotactl_block(const char __user *special, int cmd)
868 struct block_device *bdev;
869 struct super_block *sb;
870 struct filename *tmp = getname(special);
873 return ERR_CAST(tmp);
874 bdev = lookup_bdev(tmp->name);
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);
883 sb = get_super(bdev);
886 return ERR_PTR(-ENODEV);
890 return ERR_PTR(-ENODEV);
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.
900 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
901 qid_t, id, void __user *, addr)
904 struct super_block *sb = NULL;
905 struct path path, *pathp = NULL;
908 cmds = cmd >> SUBCMDSHIFT;
909 type = cmd & SUBCMDMASK;
911 if (type >= MAXQUOTAS)
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.
921 return quota_sync_all(type);
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.
930 if (cmds == Q_QUOTAON) {
931 ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
933 pathp = ERR_PTR(ret);
938 sb = quotactl_block(special, cmds);
944 ret = do_quotactl(sb, type, cmds, id, addr, pathp);
946 if (!quotactl_cmd_onoff(cmds))
949 drop_super_exclusive(sb);
951 if (pathp && !IS_ERR(pathp))