blob: 9af95c7a0bbe3c26edfb710ecd6d1b74e3b5912d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Jeff Liuf3da9312012-05-28 23:40:17 +080013#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/security.h>
16#include <linux/syscalls.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080017#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080018#include <linux/quotaops.h>
Vasily Tarasovb7163952007-07-15 23:41:12 -070019#include <linux/types.h>
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -050020#include <linux/writeback.h>
Jeremy Cline7b6924d2018-07-31 01:37:31 +000021#include <linux/nospec.h>
Christoph Hellwig80bdad32020-09-17 09:41:59 +020022#include "compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Christoph Hellwigc988afb2010-02-16 03:44:50 -050024static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
25 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Christoph Hellwigc988afb2010-02-16 03:44:50 -050027 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:
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -050033 case Q_XGETQSTATV:
Christoph Hellwigc988afb2010-02-16 03:44:50 -050034 case Q_XQUOTASYNC:
35 break;
36 /* allow to query information for dquots we "own" */
37 case Q_GETQUOTA:
38 case Q_XGETQUOTA:
Eric W. Biederman74a8a102012-09-16 02:07:49 -070039 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))))
Christoph Hellwigc988afb2010-02-16 03:44:50 -050041 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050042 fallthrough;
Christoph Hellwigc988afb2010-02-16 03:44:50 -050043 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (!capable(CAP_SYS_ADMIN))
45 return -EPERM;
46 }
47
Christoph Hellwigc988afb2010-02-16 03:44:50 -050048 return security_quotactl(cmd, type, id, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Al Viro01a05b32010-03-23 06:06:58 -040051static void quota_sync_one(struct super_block *sb, void *arg)
52{
Jan Kara2c5f6482014-09-30 10:43:09 +020053 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);
Al Viro01a05b32010-03-23 06:06:58 -040058}
59
Christoph Hellwig6ae09572010-02-16 03:44:49 -050060static int quota_sync_all(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Christoph Hellwig6ae09572010-02-16 03:44:49 -050062 int ret;
63
Christoph Hellwig6ae09572010-02-16 03:44:49 -050064 ret = security_quotactl(Q_SYNC, type, 0, NULL);
Al Viro01a05b32010-03-23 06:06:58 -040065 if (!ret)
66 iterate_supers(quota_sync_one, &type);
67 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Jan Karad3b86322014-10-08 16:07:12 +020070unsigned 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
Eric Sandeen3218a3e2016-02-08 11:21:24 +110083static int quota_quotaon(struct super_block *sb, int type, qid_t id,
Al Viro8c54ca92016-11-20 19:49:34 -050084 const struct path *path)
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050085{
Jan Karaaaa3dae2014-10-08 18:35:31 +020086 if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
Jan Karaf00c9e42010-09-15 17:38:58 +020087 return -ENOSYS;
Jan Karad3b86322014-10-08 16:07:12 +020088 if (sb->s_qcop->quota_enable)
89 return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
Jan Karaf00c9e42010-09-15 17:38:58 +020090 if (IS_ERR(path))
91 return PTR_ERR(path);
92 return sb->s_qcop->quota_on(sb, type, id, path);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050093}
94
Jan Karad3b86322014-10-08 16:07:12 +020095static 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
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500104static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
105{
106 __u32 fmt;
107
Jan Kara9d1ccbe2016-11-23 13:35:14 +0100108 if (!sb_has_quota_active(sb, type))
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500109 return -ESRCH;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500110 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500111 if (copy_to_user(addr, &fmt, sizeof(fmt)))
112 return -EFAULT;
113 return 0;
114}
115
116static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
117{
Jan Kara0a2403392014-11-19 00:42:09 +0100118 struct qc_state state;
119 struct qc_type_state *tstate;
120 struct if_dqinfo uinfo;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500121 int ret;
122
Jan Kara0a2403392014-11-19 00:42:09 +0100123 if (!sb->s_qcop->get_state)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500124 return -ENOSYS;
Jan Kara0a2403392014-11-19 00:42:09 +0100125 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;
Dan Carpenter72d4d0e2015-08-11 00:29:55 +0300139 if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500140 return -EFAULT;
Dan Carpenter72d4d0e2015-08-11 00:29:55 +0300141 return 0;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500142}
143
144static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
145{
146 struct if_dqinfo info;
Jan Kara5eacb2a2014-12-16 12:03:51 +0100147 struct qc_info qinfo;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500148
149 if (copy_from_user(&info, addr, sizeof(info)))
150 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500151 if (!sb->s_qcop->set_info)
152 return -ENOSYS;
Jan Kara5eacb2a2014-12-16 12:03:51 +0100153 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);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500172}
173
Jan Kara14bf61f2014-10-09 16:03:13 +0200174static inline qsize_t qbtos(qsize_t blocks)
175{
176 return blocks << QIF_DQBLKSIZE_BITS;
177}
178
179static inline qsize_t stoqb(qsize_t space)
180{
181 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
182}
183
184static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400185{
Dan Carpenter18da65e2013-11-01 13:21:54 +0300186 memset(dst, 0, sizeof(*dst));
Jan Kara14bf61f2014-10-09 16:03:13 +0200187 dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
188 dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
189 dst->dqb_curspace = src->d_space;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400190 dst->dqb_ihardlimit = src->d_ino_hardlimit;
191 dst->dqb_isoftlimit = src->d_ino_softlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +0200192 dst->dqb_curinodes = src->d_ino_count;
193 dst->dqb_btime = src->d_spc_timer;
194 dst->dqb_itime = src->d_ino_timer;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400195 dst->dqb_valid = QIF_ALL;
196}
197
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500198static int quota_getquota(struct super_block *sb, int type, qid_t id,
199 void __user *addr)
200{
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700201 struct kqid qid;
Jan Kara14bf61f2014-10-09 16:03:13 +0200202 struct qc_dqblk fdq;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500203 struct if_dqblk idq;
204 int ret;
205
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500206 if (!sb->s_qcop->get_dqblk)
207 return -ENOSYS;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700208 qid = make_kqid(current_user_ns(), type, id);
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500209 if (!qid_has_mapping(sb->s_user_ns, qid))
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700210 return -EINVAL;
211 ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500212 if (ret)
213 return ret;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400214 copy_to_if_dqblk(&idq, &fdq);
Christoph Hellwig80bdad32020-09-17 09:41:59 +0200215
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 }
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500227 return 0;
228}
229
Eric Sandeen926132c2016-02-08 11:22:21 +1100230/*
231 * Return quota for next active quota >= this id, if any exists,
Eric Sandeenba581482016-01-22 12:25:32 -0600232 * otherwise return -ENOENT via ->get_nextdqblk
Eric Sandeen926132c2016-02-08 11:22:21 +1100233 */
234static 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);
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500245 if (!qid_has_mapping(sb->s_user_ns, qid))
Eric Sandeen926132c2016-02-08 11:22:21 +1100246 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
Jan Kara14bf61f2014-10-09 16:03:13 +0200258static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
Christoph Hellwigc472b432010-05-06 17:05:17 -0400259{
Jan Kara14bf61f2014-10-09 16:03:13 +0200260 dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
261 dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
262 dst->d_space = src->dqb_curspace;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400263 dst->d_ino_hardlimit = src->dqb_ihardlimit;
264 dst->d_ino_softlimit = src->dqb_isoftlimit;
Jan Kara14bf61f2014-10-09 16:03:13 +0200265 dst->d_ino_count = src->dqb_curinodes;
266 dst->d_spc_timer = src->dqb_btime;
267 dst->d_ino_timer = src->dqb_itime;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400268
269 dst->d_fieldmask = 0;
270 if (src->dqb_valid & QIF_BLIMITS)
Jan Kara14bf61f2014-10-09 16:03:13 +0200271 dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400272 if (src->dqb_valid & QIF_SPACE)
Jan Kara14bf61f2014-10-09 16:03:13 +0200273 dst->d_fieldmask |= QC_SPACE;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400274 if (src->dqb_valid & QIF_ILIMITS)
Jan Kara14bf61f2014-10-09 16:03:13 +0200275 dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400276 if (src->dqb_valid & QIF_INODES)
Jan Kara14bf61f2014-10-09 16:03:13 +0200277 dst->d_fieldmask |= QC_INO_COUNT;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400278 if (src->dqb_valid & QIF_BTIME)
Jan Kara14bf61f2014-10-09 16:03:13 +0200279 dst->d_fieldmask |= QC_SPC_TIMER;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400280 if (src->dqb_valid & QIF_ITIME)
Jan Kara14bf61f2014-10-09 16:03:13 +0200281 dst->d_fieldmask |= QC_INO_TIMER;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400282}
283
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500284static int quota_setquota(struct super_block *sb, int type, qid_t id,
285 void __user *addr)
286{
Jan Kara14bf61f2014-10-09 16:03:13 +0200287 struct qc_dqblk fdq;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500288 struct if_dqblk idq;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700289 struct kqid qid;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500290
Christoph Hellwig80bdad32020-09-17 09:41:59 +0200291 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 }
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500301 if (!sb->s_qcop->set_dqblk)
302 return -ENOSYS;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700303 qid = make_kqid(current_user_ns(), type, id);
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500304 if (!qid_has_mapping(sb->s_user_ns, qid))
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700305 return -EINVAL;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400306 copy_from_if_dqblk(&fdq, &idq);
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700307 return sb->s_qcop->set_dqblk(sb, qid, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500308}
309
Jan Kara38e478c2014-10-08 15:56:21 +0200310static int quota_enable(struct super_block *sb, void __user *addr)
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500311{
312 __u32 flags;
313
314 if (copy_from_user(&flags, addr, sizeof(flags)))
315 return -EFAULT;
Jan Kara38e478c2014-10-08 15:56:21 +0200316 if (!sb->s_qcop->quota_enable)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500317 return -ENOSYS;
Jan Kara38e478c2014-10-08 15:56:21 +0200318 return sb->s_qcop->quota_enable(sb, flags);
319}
320
321static 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);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500330}
331
Jan Karabc230e42014-11-19 16:17:45 +0100332static 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
Eric Sandeen555b2c32019-06-21 18:27:13 -0500351static int quota_getstate(struct super_block *sb, int type,
352 struct fs_quota_stat *fqs)
Jan Karabc230e42014-11-19 16:17:45 +0100353{
Jan Karabc230e42014-11-19 16:17:45 +0100354 struct qc_state state;
355 int ret;
356
Eric Sandeen3cd01262016-08-12 17:40:09 -0500357 memset(&state, 0, sizeof (struct qc_state));
Jan Karabc230e42014-11-19 16:17:45 +0100358 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;
Eric Sandeen555b2c32019-06-21 18:27:13 -0500369
Jan Karabc230e42014-11-19 16:17:45 +0100370 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;
Eric Sandeen3cd01262016-08-12 17:40:09 -0500375
376 /* Inodes may be allocated even if inactive; copy out if present */
377 if (state.s_state[USRQUOTA].ino) {
Jan Karabc230e42014-11-19 16:17:45 +0100378 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 }
Eric Sandeen3cd01262016-08-12 17:40:09 -0500382 if (state.s_state[GRPQUOTA].ino) {
Jan Karabc230e42014-11-19 16:17:45 +0100383 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 }
Eric Sandeen3cd01262016-08-12 17:40:09 -0500387 if (state.s_state[PRJQUOTA].ino) {
Jan Karabc230e42014-11-19 16:17:45 +0100388 /*
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
Christoph Hellwig80bdad32020-09-17 09:41:59 +0200404static 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
413static 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
Eric Sandeen555b2c32019-06-21 18:27:13 -0500431static int quota_getxstate(struct super_block *sb, int type, void __user *addr)
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500432{
433 struct fs_quota_stat fqs;
434 int ret;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500435
Jan Kara59b6ba62014-11-19 16:44:58 +0100436 if (!sb->s_qcop->get_state)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500437 return -ENOSYS;
Eric Sandeen555b2c32019-06-21 18:27:13 -0500438 ret = quota_getstate(sb, type, &fqs);
Christoph Hellwig80bdad32020-09-17 09:41:59 +0200439 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)))
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500445 return -EFAULT;
Christoph Hellwig80bdad32020-09-17 09:41:59 +0200446 return 0;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500447}
448
Eric Sandeen555b2c32019-06-21 18:27:13 -0500449static int quota_getstatev(struct super_block *sb, int type,
450 struct fs_quota_statv *fqs)
Jan Karabc230e42014-11-19 16:17:45 +0100451{
Jan Karabc230e42014-11-19 16:17:45 +0100452 struct qc_state state;
453 int ret;
454
Eric Sandeen3cd01262016-08-12 17:40:09 -0500455 memset(&state, 0, sizeof (struct qc_state));
Jan Karabc230e42014-11-19 16:17:45 +0100456 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;
Eric Sandeen555b2c32019-06-21 18:27:13 -0500467
Jan Karabc230e42014-11-19 16:17:45 +0100468 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;
Eric Sandeen3cd01262016-08-12 17:40:09 -0500473
474 /* Inodes may be allocated even if inactive; copy out if present */
475 if (state.s_state[USRQUOTA].ino) {
Jan Karabc230e42014-11-19 16:17:45 +0100476 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 }
Eric Sandeen3cd01262016-08-12 17:40:09 -0500480 if (state.s_state[GRPQUOTA].ino) {
Jan Karabc230e42014-11-19 16:17:45 +0100481 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 }
Eric Sandeen3cd01262016-08-12 17:40:09 -0500485 if (state.s_state[PRJQUOTA].ino) {
Jan Karabc230e42014-11-19 16:17:45 +0100486 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
Eric Sandeen555b2c32019-06-21 18:27:13 -0500493static int quota_getxstatev(struct super_block *sb, int type, void __user *addr)
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -0500494{
495 struct fs_quota_statv fqs;
496 int ret;
497
Jan Kara59b6ba62014-11-19 16:44:58 +0100498 if (!sb->s_qcop->get_state)
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -0500499 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 }
Eric Sandeen555b2c32019-06-21 18:27:13 -0500512 ret = quota_getstatev(sb, type, &fqs);
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -0500513 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
514 return -EFAULT;
515 return ret;
516}
517
Jan Kara14bf61f2014-10-09 16:03:13 +0200518/*
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
525static inline u64 quota_bbtob(u64 blocks)
526{
527 return blocks << XFS_BB_SHIFT;
528}
529
530static inline u64 quota_btobb(u64 bytes)
531{
532 return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
533}
534
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700535static 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
Jan Kara14bf61f2014-10-09 16:03:13 +0200543static 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;
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700551 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);
Jan Kara14bf61f2014-10-09 16:03:13 +0200555 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);
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700560 dst->d_rt_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_rtbtimer,
561 src->d_rtbtimer_hi);
Jan Kara14bf61f2014-10-09 16:03:13 +0200562 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
Jan Karac39fb532014-12-16 16:12:27 +0100596static 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
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500620static int quota_setxquota(struct super_block *sb, int type, qid_t id,
621 void __user *addr)
622{
623 struct fs_disk_quota fdq;
Jan Kara14bf61f2014-10-09 16:03:13 +0200624 struct qc_dqblk qdq;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700625 struct kqid qid;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500626
627 if (copy_from_user(&fdq, addr, sizeof(fdq)))
628 return -EFAULT;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400629 if (!sb->s_qcop->set_dqblk)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500630 return -ENOSYS;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700631 qid = make_kqid(current_user_ns(), type, id);
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500632 if (!qid_has_mapping(sb->s_user_ns, qid))
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700633 return -EINVAL;
Jan Karac39fb532014-12-16 16:12:27 +0100634 /* Are we actually setting timer / warning limits for all users? */
Eric W. Biedermancfd4c702016-07-05 11:10:57 -0500635 if (from_kqid(sb->s_user_ns, qid) == 0 &&
Jan Karac39fb532014-12-16 16:12:27 +0100636 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 }
Jan Kara14bf61f2014-10-09 16:03:13 +0200649 copy_from_xfs_dqblk(&qdq, &fdq);
650 return sb->s_qcop->set_dqblk(sb, qid, &qdq);
651}
652
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700653static 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
661static inline bool want_bigtime(s64 timer)
662{
663 return timer > S32_MAX || timer < S32_MIN;
664}
665
Jan Kara14bf61f2014-10-09 16:03:13 +0200666static 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));
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700670 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;
Jan Kara14bf61f2014-10-09 16:03:13 +0200673 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;
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700687 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);
Jan Kara14bf61f2014-10-09 16:03:13 +0200691 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);
Darrick J. Wongad47ff332020-09-09 09:34:13 -0700696 copy_to_xfs_dqblk_ts(dst, &dst->d_rtbtimer, &dst->d_rtbtimer_hi,
697 src->d_rt_spc_timer);
Jan Kara14bf61f2014-10-09 16:03:13 +0200698 dst->d_rtbwarns = src->d_rt_spc_warns;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500699}
700
701static int quota_getxquota(struct super_block *sb, int type, qid_t id,
702 void __user *addr)
703{
704 struct fs_disk_quota fdq;
Jan Kara14bf61f2014-10-09 16:03:13 +0200705 struct qc_dqblk qdq;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700706 struct kqid qid;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500707 int ret;
708
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400709 if (!sb->s_qcop->get_dqblk)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500710 return -ENOSYS;
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700711 qid = make_kqid(current_user_ns(), type, id);
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500712 if (!qid_has_mapping(sb->s_user_ns, qid))
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700713 return -EINVAL;
Jan Kara14bf61f2014-10-09 16:03:13 +0200714 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)))
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500719 return -EFAULT;
720 return ret;
721}
722
Eric Sandeen8b375242016-02-08 11:21:50 +1100723/*
724 * Return quota for next active quota >= this id, if any exists,
Eric Sandeenba581482016-01-22 12:25:32 -0600725 * otherwise return -ENOENT via ->get_nextdqblk.
Eric Sandeen8b375242016-02-08 11:21:50 +1100726 */
727static 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);
Eric W. Biedermand49d3762016-06-30 16:31:01 -0500739 if (!qid_has_mapping(sb->s_user_ns, qid))
Eric Sandeen8b375242016-02-08 11:21:50 +1100740 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
Eric Sandeen9da93f92014-05-05 17:25:50 +1000751static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100763static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
Al Viro8c54ca92016-11-20 19:49:34 -0500764 void __user *addr, const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500766 int ret;
767
Jeremy Cline7b6924d2018-07-31 01:37:31 +0000768 type = array_index_nospec(type, MAXQUOTAS);
Jan Kara2c5f6482014-09-30 10:43:09 +0200769 /*
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 */
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500773 if (!sb->s_qcop)
774 return -ENOSYS;
Jan Kara2c5f6482014-09-30 10:43:09 +0200775 if (!(sb->s_quota_types & (1 << type)))
776 return -EINVAL;
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500777
778 ret = check_quotactl_permission(sb, type, cmd, id);
779 if (ret < 0)
780 return ret;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500783 case Q_QUOTAON:
Eric Sandeen3218a3e2016-02-08 11:21:24 +1100784 return quota_quotaon(sb, type, id, path);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500785 case Q_QUOTAOFF:
Jan Karad3b86322014-10-08 16:07:12 +0200786 return quota_quotaoff(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500787 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);
Eric Sandeen926132c2016-02-08 11:22:21 +1100795 case Q_GETNEXTQUOTA:
796 return quota_getnextquota(sb, type, id, addr);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500797 case Q_SETQUOTA:
798 return quota_setquota(sb, type, id, addr);
799 case Q_SYNC:
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500800 if (!sb->s_qcop->quota_sync)
801 return -ENOSYS;
Jan Karaceed1722012-07-03 16:45:28 +0200802 return sb->s_qcop->quota_sync(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500803 case Q_XQUOTAON:
Jan Kara38e478c2014-10-08 15:56:21 +0200804 return quota_enable(sb, addr);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500805 case Q_XQUOTAOFF:
Jan Kara38e478c2014-10-08 15:56:21 +0200806 return quota_disable(sb, addr);
Eric Sandeen9da93f92014-05-05 17:25:50 +1000807 case Q_XQUOTARM:
808 return quota_rmxquota(sb, addr);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500809 case Q_XGETQSTAT:
Eric Sandeen555b2c32019-06-21 18:27:13 -0500810 return quota_getxstate(sb, type, addr);
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -0500811 case Q_XGETQSTATV:
Eric Sandeen555b2c32019-06-21 18:27:13 -0500812 return quota_getxstatev(sb, type, addr);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500813 case Q_XSETQLIM:
814 return quota_setxquota(sb, type, id, addr);
815 case Q_XGETQUOTA:
816 return quota_getxquota(sb, type, id, addr);
Eric Sandeen8b375242016-02-08 11:21:50 +1100817 case Q_XGETNEXTQUOTA:
818 return quota_getnextxquota(sb, type, id, addr);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500819 case Q_XQUOTASYNC:
David Howellsbc98a422017-07-17 08:45:34 +0100820 if (sb_rdonly(sb))
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -0500821 return -EROFS;
Christoph Hellwig4b217ed2012-02-20 02:28:18 +0000822 /* XFS quotas are fully coherent now, making this call a noop */
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -0500823 return 0;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500824 default:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Lee Jones56df1272012-11-03 23:02:28 +0100829#ifdef CONFIG_BLOCK
830
Jan Karadcdbed82012-02-10 11:03:01 +0100831/* Return 1 if 'cmd' will block on frozen filesystem */
832static int quotactl_cmd_write(int cmd)
833{
Jan Karaccf370e2016-02-18 14:03:03 +0100834 /*
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 */
Jan Karadcdbed82012-02-10 11:03:01 +0100839 switch (cmd) {
840 case Q_GETFMT:
841 case Q_GETINFO:
842 case Q_SYNC:
843 case Q_XGETQSTAT:
Chandra Seetharamanaf30cb42013-08-06 17:27:07 -0500844 case Q_XGETQSTATV:
Jan Karadcdbed82012-02-10 11:03:01 +0100845 case Q_XGETQUOTA:
Eric Sandeen8b375242016-02-08 11:21:50 +1100846 case Q_XGETNEXTQUOTA:
Jan Karadcdbed82012-02-10 11:03:01 +0100847 case Q_XQUOTASYNC:
848 return 0;
849 }
850 return 1;
851}
Lee Jones56df1272012-11-03 23:02:28 +0100852#endif /* CONFIG_BLOCK */
853
Jan Kara7d6cd732016-11-23 13:16:10 +0100854/* Return true if quotactl command is manipulating quota on/off state */
855static bool quotactl_cmd_onoff(int cmd)
856{
Javier Barrio41c4f852018-12-13 01:06:29 +0100857 return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
858 (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
Jan Kara7d6cd732016-11-23 13:16:10 +0100859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/*
David Howells93614012006-09-30 20:45:40 +0200862 * 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 */
Jan Karadcdbed82012-02-10 11:03:01 +0100865static struct super_block *quotactl_block(const char __user *special, int cmd)
David Howells93614012006-09-30 20:45:40 +0200866{
867#ifdef CONFIG_BLOCK
868 struct block_device *bdev;
869 struct super_block *sb;
Jeff Layton91a27b22012-10-10 15:25:28 -0400870 struct filename *tmp = getname(special);
David Howells93614012006-09-30 20:45:40 +0200871
872 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800873 return ERR_CAST(tmp);
Jeff Layton91a27b22012-10-10 15:25:28 -0400874 bdev = lookup_bdev(tmp->name);
David Howells93614012006-09-30 20:45:40 +0200875 putname(tmp);
876 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800877 return ERR_CAST(bdev);
Jan Kara7d6cd732016-11-23 13:16:10 +0100878 if (quotactl_cmd_onoff(cmd))
879 sb = get_super_exclusive_thawed(bdev);
880 else if (quotactl_cmd_write(cmd))
Jan Karadcdbed82012-02-10 11:03:01 +0100881 sb = get_super_thawed(bdev);
882 else
883 sb = get_super(bdev);
David Howells93614012006-09-30 20:45:40 +0200884 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * 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 */
Christoph Hellwig80bdad32020-09-17 09:41:59 +0200900SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
901 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 uint cmds, type;
904 struct super_block *sb = NULL;
Jan Karaf00c9e42010-09-15 17:38:58 +0200905 struct path path, *pathp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int ret;
907
908 cmds = cmd >> SUBCMDSHIFT;
909 type = cmd & SUBCMDMASK;
910
Chengguang Xua6810312019-10-08 22:50:59 +0800911 if (type >= MAXQUOTAS)
912 return -EINVAL;
913
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500914 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Jan Karaf00c9e42010-09-15 17:38:58 +0200925 /*
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) {
Trond Myklebust815d4052011-09-26 20:36:09 -0400931 ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
Jan Karaf00c9e42010-09-15 17:38:58 +0200932 if (ret)
933 pathp = ERR_PTR(ret);
934 else
935 pathp = &path;
936 }
937
Jan Karadcdbed82012-02-10 11:03:01 +0100938 sb = quotactl_block(special, cmds);
Jan Kara0aaa6182011-10-10 18:32:06 +0200939 if (IS_ERR(sb)) {
940 ret = PTR_ERR(sb);
941 goto out;
942 }
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500943
Jan Karaf00c9e42010-09-15 17:38:58 +0200944 ret = do_quotactl(sb, type, cmds, id, addr, pathp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jan Kara7d6cd732016-11-23 13:16:10 +0100946 if (!quotactl_cmd_onoff(cmds))
947 drop_super(sb);
948 else
949 drop_super_exclusive(sb);
Jan Kara0aaa6182011-10-10 18:32:06 +0200950out:
Jan Karaf00c9e42010-09-15 17:38:58 +0200951 if (pathp && !IS_ERR(pathp))
952 path_put(pathp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return ret;
954}