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>
23 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
27 /* these commands do not require any special privilegues */
35 /* allow to query information for dquots we "own" */
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))))
43 if (!capable(CAP_SYS_ADMIN))
47 return security_quotactl(cmd, type, id, sb);
50 static void quota_sync_one(struct super_block *sb, void *arg)
52 int type = *(int *)arg;
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);
59 static int quota_sync_all(int type)
63 if (type >= MAXQUOTAS)
65 ret = security_quotactl(Q_SYNC, type, 0, NULL);
67 iterate_supers(quota_sync_one, &type);
71 unsigned int qtype_enforce_flag(int type)
75 return FS_QUOTA_UDQ_ENFD;
77 return FS_QUOTA_GDQ_ENFD;
79 return FS_QUOTA_PDQ_ENFD;
84 static int quota_quotaon(struct super_block *sb, int type, qid_t id,
85 const struct path *path)
87 if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
89 if (sb->s_qcop->quota_enable)
90 return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
93 return sb->s_qcop->quota_on(sb, type, id, path);
96 static int quota_quotaoff(struct super_block *sb, int type)
98 if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
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);
105 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
109 if (!sb_has_quota_active(sb, type))
111 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
112 if (copy_to_user(addr, &fmt, sizeof(fmt)))
117 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
119 struct qc_state state;
120 struct qc_type_state *tstate;
121 struct if_dqinfo uinfo;
124 /* This checks whether qc_state has enough entries... */
125 BUILD_BUG_ON(MAXQUOTAS > XQM_MAXQUOTAS);
126 if (!sb->s_qcop->get_state)
128 ret = sb->s_qcop->get_state(sb, &state);
131 tstate = state.s_state + type;
132 if (!(tstate->flags & QCI_ACCT_ENABLED))
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)))
147 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
149 struct if_dqinfo info;
150 struct qc_info qinfo;
152 if (copy_from_user(&info, addr, sizeof(info)))
154 if (!sb->s_qcop->set_info)
156 if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
158 memset(&qinfo, 0, sizeof(qinfo));
159 if (info.dqi_valid & IIF_FLAGS) {
160 if (info.dqi_flags & ~DQF_SETINFO_MASK)
162 if (info.dqi_flags & DQF_ROOT_SQUASH)
163 qinfo.i_flags |= QCI_ROOT_SQUASH;
164 qinfo.i_fieldmask |= QC_FLAGS;
166 if (info.dqi_valid & IIF_BGRACE) {
167 qinfo.i_spc_timelimit = info.dqi_bgrace;
168 qinfo.i_fieldmask |= QC_SPC_TIMER;
170 if (info.dqi_valid & IIF_IGRACE) {
171 qinfo.i_ino_timelimit = info.dqi_igrace;
172 qinfo.i_fieldmask |= QC_INO_TIMER;
174 return sb->s_qcop->set_info(sb, type, &qinfo);
177 static inline qsize_t qbtos(qsize_t blocks)
179 return blocks << QIF_DQBLKSIZE_BITS;
182 static inline qsize_t stoqb(qsize_t space)
184 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
187 static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
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;
201 static int quota_getquota(struct super_block *sb, int type, qid_t id,
209 if (!sb->s_qcop->get_dqblk)
211 qid = make_kqid(current_user_ns(), type, id);
212 if (!qid_has_mapping(sb->s_user_ns, qid))
214 ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
217 copy_to_if_dqblk(&idq, &fdq);
218 if (copy_to_user(addr, &idq, sizeof(idq)))
224 * Return quota for next active quota >= this id, if any exists,
225 * otherwise return -ENOENT via ->get_nextdqblk
227 static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
232 struct if_nextdqblk idq;
235 if (!sb->s_qcop->get_nextdqblk)
237 qid = make_kqid(current_user_ns(), type, id);
238 if (!qid_has_mapping(sb->s_user_ns, qid))
240 ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
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)))
251 static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
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;
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;
277 static int quota_setquota(struct super_block *sb, int type, qid_t id,
284 if (copy_from_user(&idq, addr, sizeof(idq)))
286 if (!sb->s_qcop->set_dqblk)
288 qid = make_kqid(current_user_ns(), type, id);
289 if (!qid_has_mapping(sb->s_user_ns, qid))
291 copy_from_if_dqblk(&fdq, &idq);
292 return sb->s_qcop->set_dqblk(sb, qid, &fdq);
295 static int quota_enable(struct super_block *sb, void __user *addr)
299 if (copy_from_user(&flags, addr, sizeof(flags)))
301 if (!sb->s_qcop->quota_enable)
303 return sb->s_qcop->quota_enable(sb, flags);
306 static int quota_disable(struct super_block *sb, void __user *addr)
310 if (copy_from_user(&flags, addr, sizeof(flags)))
312 if (!sb->s_qcop->quota_disable)
314 return sb->s_qcop->quota_disable(sb, flags);
317 static int quota_state_to_flags(struct qc_state *state)
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;
336 static int quota_getstate(struct super_block *sb, struct fs_quota_stat *fqs)
339 struct qc_state state;
342 memset(&state, 0, sizeof (struct qc_state));
343 ret = sb->s_qcop->get_state(sb, &state);
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? */
353 fqs->qs_incoredqs = state.s_incoredqs;
355 * GETXSTATE quotactl has space for just one set of time limits so
356 * report them for the first enabled quota type
358 for (type = 0; type < XQM_MAXQUOTAS; type++)
359 if (state.s_state[type].flags & QCI_ACCT_ENABLED)
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;
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;
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;
379 if (state.s_state[PRJQUOTA].ino) {
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.
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;
396 static int quota_getxstate(struct super_block *sb, void __user *addr)
398 struct fs_quota_stat fqs;
401 if (!sb->s_qcop->get_state)
403 ret = quota_getstate(sb, &fqs);
404 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
409 static int quota_getstatev(struct super_block *sb, struct fs_quota_statv *fqs)
412 struct qc_state state;
415 memset(&state, 0, sizeof (struct qc_state));
416 ret = sb->s_qcop->get_state(sb, &state);
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? */
426 fqs->qs_incoredqs = state.s_incoredqs;
428 * GETXSTATV quotactl has space for just one set of time limits so
429 * report them for the first enabled quota type
431 for (type = 0; type < XQM_MAXQUOTAS; type++)
432 if (state.s_state[type].flags & QCI_ACCT_ENABLED)
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;
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;
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;
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;
460 static int quota_getxstatev(struct super_block *sb, void __user *addr)
462 struct fs_quota_statv fqs;
465 if (!sb->s_qcop->get_state)
468 memset(&fqs, 0, sizeof(fqs));
469 if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
472 /* If this kernel doesn't support user specified version, fail */
473 switch (fqs.qs_version) {
474 case FS_QSTATV_VERSION1:
479 ret = quota_getstatev(sb, &fqs);
480 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
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.
490 #define XFS_BB_SHIFT 9
492 static inline u64 quota_bbtob(u64 blocks)
494 return blocks << XFS_BB_SHIFT;
497 static inline u64 quota_btobb(u64 bytes)
499 return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
502 static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
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;
552 static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
553 struct fs_disk_quota *src)
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;
576 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
579 struct fs_disk_quota fdq;
583 if (copy_from_user(&fdq, addr, sizeof(fdq)))
585 if (!sb->s_qcop->set_dqblk)
587 qid = make_kqid(current_user_ns(), type, id);
588 if (!qid_has_mapping(sb->s_user_ns, qid))
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;
596 if (!sb->s_qcop->set_info)
598 copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
599 ret = sb->s_qcop->set_info(sb, type, &qinfo);
602 /* These are already done */
603 fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
605 copy_from_xfs_dqblk(&qdq, &fdq);
606 return sb->s_qcop->set_dqblk(sb, qid, &qdq);
609 static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
612 memset(dst, 0, sizeof(*dst));
613 dst->d_version = FS_DQUOT_VERSION;
615 if (type == USRQUOTA)
616 dst->d_flags = FS_USER_QUOTA;
617 else if (type == PRJQUOTA)
618 dst->d_flags = FS_PROJ_QUOTA;
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;
638 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
641 struct fs_disk_quota fdq;
646 if (!sb->s_qcop->get_dqblk)
648 qid = make_kqid(current_user_ns(), type, id);
649 if (!qid_has_mapping(sb->s_user_ns, qid))
651 ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
654 copy_to_xfs_dqblk(&fdq, &qdq, type, id);
655 if (copy_to_user(addr, &fdq, sizeof(fdq)))
661 * Return quota for next active quota >= this id, if any exists,
662 * otherwise return -ENOENT via ->get_nextdqblk.
664 static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
667 struct fs_disk_quota fdq;
673 if (!sb->s_qcop->get_nextdqblk)
675 qid = make_kqid(current_user_ns(), type, id);
676 if (!qid_has_mapping(sb->s_user_ns, qid))
678 ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
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)))
688 static int quota_rmxquota(struct super_block *sb, void __user *addr)
692 if (copy_from_user(&flags, addr, sizeof(flags)))
694 if (!sb->s_qcop->rm_xquota)
696 return sb->s_qcop->rm_xquota(sb, flags);
699 /* Copy parameters and call proper function */
700 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
701 void __user *addr, const struct path *path)
705 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
707 type = array_index_nospec(type, MAXQUOTAS);
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.
714 if (!(sb->s_quota_types & (1 << type)))
717 ret = check_quotactl_permission(sb, type, cmd, id);
723 return quota_quotaon(sb, type, id, path);
725 return quota_quotaoff(sb, type);
727 return quota_getfmt(sb, type, addr);
729 return quota_getinfo(sb, type, addr);
731 return quota_setinfo(sb, type, addr);
733 return quota_getquota(sb, type, id, addr);
735 return quota_getnextquota(sb, type, id, addr);
737 return quota_setquota(sb, type, id, addr);
739 if (!sb->s_qcop->quota_sync)
741 return sb->s_qcop->quota_sync(sb, type);
743 return quota_enable(sb, addr);
745 return quota_disable(sb, addr);
747 return quota_rmxquota(sb, addr);
749 return quota_getxstate(sb, addr);
751 return quota_getxstatev(sb, addr);
753 return quota_setxquota(sb, type, id, addr);
755 return quota_getxquota(sb, type, id, addr);
756 case Q_XGETNEXTQUOTA:
757 return quota_getnextxquota(sb, type, id, addr);
761 /* XFS quotas are fully coherent now, making this call a noop */
770 /* Return 1 if 'cmd' will block on frozen filesystem */
771 static int quotactl_cmd_write(int cmd)
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.
785 case Q_XGETNEXTQUOTA:
791 #endif /* CONFIG_BLOCK */
793 /* Return true if quotactl command is manipulating quota on/off state */
794 static bool quotactl_cmd_onoff(int cmd)
796 return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
797 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
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
804 static struct super_block *quotactl_block(const char __user *special, int cmd)
807 struct block_device *bdev;
808 struct super_block *sb;
809 struct filename *tmp = getname(special);
812 return ERR_CAST(tmp);
813 bdev = lookup_bdev(tmp->name);
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);
822 sb = get_super(bdev);
825 return ERR_PTR(-ENODEV);
829 return ERR_PTR(-ENODEV);
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.
839 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
840 qid_t, id, void __user *, addr)
843 struct super_block *sb = NULL;
844 struct path path, *pathp = NULL;
847 cmds = cmd >> SUBCMDSHIFT;
848 type = cmd & SUBCMDMASK;
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.
857 return quota_sync_all(type);
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.
866 if (cmds == Q_QUOTAON) {
867 ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
869 pathp = ERR_PTR(ret);
874 sb = quotactl_block(special, cmds);
880 ret = do_quotactl(sb, type, cmds, id, addr, pathp);
882 if (!quotactl_cmd_onoff(cmds))
885 drop_super_exclusive(sb);
887 if (pathp && !IS_ERR(pathp))