defconfig: exynos9610: Re-add dropped Wi-Fi AP options lost
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / fs / ext4 / ioctl.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
ac27a0ec 2/*
617ba13b 3 * linux/fs/ext4/ioctl.c
ac27a0ec
DK
4 *
5 * Copyright (C) 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 */
10
11#include <linux/fs.h>
ac27a0ec 12#include <linux/capability.h>
ac27a0ec
DK
13#include <linux/time.h>
14#include <linux/compat.h>
42a74f20 15#include <linux/mount.h>
748de673 16#include <linux/file.h>
9b7365fc 17#include <linux/quotaops.h>
8da4b8c4 18#include <linux/uuid.h>
7c0f6ba6 19#include <linux/uaccess.h>
783d9485 20#include <linux/delay.h>
3dcf5451
CH
21#include "ext4_jbd2.h"
22#include "ext4.h"
0c9ec4be
DW
23#include <linux/fsmap.h>
24#include "fsmap.h"
25#include <trace/events/ext4.h>
ac27a0ec 26
393d1d1d
DTB
27/**
28 * Swap memory between @a and @b for @len bytes.
29 *
30 * @a: pointer to first memory area
31 * @b: pointer to second memory area
32 * @len: number of bytes to swap
33 *
34 */
35static void memswap(void *a, void *b, size_t len)
36{
37 unsigned char *ap, *bp;
393d1d1d
DTB
38
39 ap = (unsigned char *)a;
40 bp = (unsigned char *)b;
41 while (len-- > 0) {
4b7e2db5 42 swap(*ap, *bp);
393d1d1d
DTB
43 ap++;
44 bp++;
45 }
46}
47
48/**
49 * Swap i_data and associated attributes between @inode1 and @inode2.
50 * This function is used for the primary swap between inode1 and inode2
51 * and also to revert this primary swap in case of errors.
52 *
53 * Therefore you have to make sure, that calling this method twice
54 * will revert all changes.
55 *
56 * @inode1: pointer to first inode
57 * @inode2: pointer to second inode
58 */
59static void swap_inode_data(struct inode *inode1, struct inode *inode2)
60{
61 loff_t isize;
62 struct ext4_inode_info *ei1;
63 struct ext4_inode_info *ei2;
80988933 64 unsigned long tmp;
393d1d1d
DTB
65
66 ei1 = EXT4_I(inode1);
67 ei2 = EXT4_I(inode2);
68
9c5d58fb
JL
69 swap(inode1->i_flags, inode2->i_flags);
70 swap(inode1->i_version, inode2->i_version);
71 swap(inode1->i_blocks, inode2->i_blocks);
72 swap(inode1->i_bytes, inode2->i_bytes);
73 swap(inode1->i_atime, inode2->i_atime);
74 swap(inode1->i_mtime, inode2->i_mtime);
393d1d1d
DTB
75
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
80988933 77 tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
78 ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
79 (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
80 ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
9c5d58fb 81 swap(ei1->i_disksize, ei2->i_disksize);
cde2d7a7
TT
82 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
83 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
393d1d1d
DTB
84
85 isize = i_size_read(inode1);
86 i_size_write(inode1, i_size_read(inode2));
87 i_size_write(inode2, isize);
88}
89
90/**
91 * Swap the information from the given @inode and the inode
92 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
93 * important fields of the inodes.
94 *
95 * @sb: the super block of the filesystem
96 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
97 *
98 */
99static long swap_inode_boot_loader(struct super_block *sb,
100 struct inode *inode)
101{
102 handle_t *handle;
103 int err;
104 struct inode *inode_bl;
393d1d1d 105 struct ext4_inode_info *ei_bl;
d7092ae2 106 struct ext4_sb_info *sbi = EXT4_SB(sb);
393d1d1d 107
d8558a29
TT
108 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
109 return -EINVAL;
393d1d1d 110
d8558a29
TT
111 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
112 return -EPERM;
393d1d1d 113
393d1d1d 114 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
d8558a29
TT
115 if (IS_ERR(inode_bl))
116 return PTR_ERR(inode_bl);
393d1d1d
DTB
117 ei_bl = EXT4_I(inode_bl);
118
119 filemap_flush(inode->i_mapping);
120 filemap_flush(inode_bl->i_mapping);
121
122 /* Protect orig inodes against a truncate and make sure,
123 * that only 1 swap_inode_boot_loader is running. */
375e289e 124 lock_two_nondirectories(inode, inode_bl);
393d1d1d
DTB
125
126 truncate_inode_pages(&inode->i_data, 0);
127 truncate_inode_pages(&inode_bl->i_data, 0);
128
129 /* Wait for all existing dio workers */
130 ext4_inode_block_unlocked_dio(inode);
131 ext4_inode_block_unlocked_dio(inode_bl);
132 inode_dio_wait(inode);
133 inode_dio_wait(inode_bl);
134
135 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
136 if (IS_ERR(handle)) {
137 err = -EINVAL;
30d29b11 138 goto journal_err_out;
393d1d1d
DTB
139 }
140
141 /* Protect extent tree against block allocations via delalloc */
142 ext4_double_down_write_data_sem(inode, inode_bl);
143
144 if (inode_bl->i_nlink == 0) {
145 /* this inode has never been used as a BOOT_LOADER */
146 set_nlink(inode_bl, 1);
147 i_uid_write(inode_bl, 0);
148 i_gid_write(inode_bl, 0);
149 inode_bl->i_flags = 0;
150 ei_bl->i_flags = 0;
151 inode_bl->i_version = 1;
152 i_size_write(inode_bl, 0);
153 inode_bl->i_mode = S_IFREG;
e2b911c5 154 if (ext4_has_feature_extents(sb)) {
393d1d1d
DTB
155 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
156 ext4_ext_tree_init(handle, inode_bl);
157 } else
158 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
159 }
160
161 swap_inode_data(inode, inode_bl);
162
eeca7ea1 163 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
393d1d1d
DTB
164
165 spin_lock(&sbi->s_next_gen_lock);
166 inode->i_generation = sbi->s_next_generation++;
167 inode_bl->i_generation = sbi->s_next_generation++;
168 spin_unlock(&sbi->s_next_gen_lock);
169
170 ext4_discard_preallocations(inode);
171
172 err = ext4_mark_inode_dirty(handle, inode);
173 if (err < 0) {
174 ext4_warning(inode->i_sb,
175 "couldn't mark inode #%lu dirty (err %d)",
176 inode->i_ino, err);
177 /* Revert all changes: */
178 swap_inode_data(inode, inode_bl);
179 } else {
180 err = ext4_mark_inode_dirty(handle, inode_bl);
181 if (err < 0) {
182 ext4_warning(inode_bl->i_sb,
183 "couldn't mark inode #%lu dirty (err %d)",
184 inode_bl->i_ino, err);
185 /* Revert all changes: */
186 swap_inode_data(inode, inode_bl);
187 ext4_mark_inode_dirty(handle, inode);
188 }
189 }
393d1d1d 190 ext4_journal_stop(handle);
393d1d1d
DTB
191 ext4_double_up_write_data_sem(inode, inode_bl);
192
30d29b11 193journal_err_out:
393d1d1d
DTB
194 ext4_inode_resume_unlocked_dio(inode);
195 ext4_inode_resume_unlocked_dio(inode_bl);
375e289e 196 unlock_two_nondirectories(inode, inode_bl);
393d1d1d 197 iput(inode_bl);
393d1d1d
DTB
198 return err;
199}
200
ba679017 201#ifdef CONFIG_EXT4_FS_ENCRYPTION
9bd8212f
MH
202static int uuid_is_zero(__u8 u[16])
203{
204 int i;
205
206 for (i = 0; i < 16; i++)
207 if (u[i])
208 return 0;
209 return 1;
210}
ba679017 211#endif
9bd8212f 212
9b7365fc
LX
213static int ext4_ioctl_setflags(struct inode *inode,
214 unsigned int flags)
215{
216 struct ext4_inode_info *ei = EXT4_I(inode);
217 handle_t *handle = NULL;
fdde368e 218 int err = -EPERM, migrate = 0;
9b7365fc
LX
219 struct ext4_iloc iloc;
220 unsigned int oldflags, mask, i;
221 unsigned int jflag;
222
223 /* Is it quota file? Do not allow user to mess with it */
02749a4c 224 if (ext4_is_quota_file(inode))
9b7365fc
LX
225 goto flags_out;
226
227 oldflags = ei->i_flags;
228
229 /* The JOURNAL_DATA flag is modifiable only by root */
230 jflag = flags & EXT4_JOURNAL_DATA_FL;
231
232 /*
233 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
234 * the relevant capability.
235 *
236 * This test looks nicer. Thanks to Pauline Middelink
237 */
238 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
239 if (!capable(CAP_LINUX_IMMUTABLE))
240 goto flags_out;
241 }
242
243 /*
244 * The JOURNAL_DATA flag can only be changed by
245 * the relevant capability.
246 */
247 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
248 if (!capable(CAP_SYS_RESOURCE))
249 goto flags_out;
250 }
251 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
252 migrate = 1;
253
254 if (flags & EXT4_EOFBLOCKS_FL) {
255 /* we don't support adding EOFBLOCKS flag */
256 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
257 err = -EOPNOTSUPP;
258 goto flags_out;
259 }
2c98eb5e
TT
260 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
261 err = ext4_truncate(inode);
262 if (err)
263 goto flags_out;
264 }
9b7365fc
LX
265
266 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
267 if (IS_ERR(handle)) {
268 err = PTR_ERR(handle);
269 goto flags_out;
270 }
271 if (IS_SYNC(inode))
272 ext4_handle_sync(handle);
273 err = ext4_reserve_inode_write(handle, inode, &iloc);
274 if (err)
275 goto flags_err;
276
277 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
278 if (!(mask & EXT4_FL_USER_MODIFIABLE))
279 continue;
f8011d93
JK
280 /* These flags get special treatment later */
281 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
282 continue;
9b7365fc
LX
283 if (mask & flags)
284 ext4_set_inode_flag(inode, i);
285 else
286 ext4_clear_inode_flag(inode, i);
287 }
288
289 ext4_set_inode_flags(inode);
eeca7ea1 290 inode->i_ctime = current_time(inode);
9b7365fc
LX
291
292 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
293flags_err:
294 ext4_journal_stop(handle);
295 if (err)
296 goto flags_out;
297
22e51908
RZ
298 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
299 /*
300 * Changes to the journaling mode can cause unsafe changes to
301 * S_DAX if we are using the DAX mount option.
302 */
303 if (test_opt(inode->i_sb, DAX)) {
304 err = -EBUSY;
305 goto flags_out;
306 }
307
9b7365fc 308 err = ext4_change_inode_journal_flag(inode, jflag);
22e51908
RZ
309 if (err)
310 goto flags_out;
311 }
9b7365fc
LX
312 if (migrate) {
313 if (flags & EXT4_EXTENTS_FL)
314 err = ext4_ext_migrate(inode);
315 else
316 err = ext4_ind_migrate(inode);
317 }
318
319flags_out:
320 return err;
321}
322
323#ifdef CONFIG_QUOTA
324static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
325{
326 struct inode *inode = file_inode(filp);
327 struct super_block *sb = inode->i_sb;
328 struct ext4_inode_info *ei = EXT4_I(inode);
329 int err, rc;
330 handle_t *handle;
331 kprojid_t kprojid;
332 struct ext4_iloc iloc;
333 struct ext4_inode *raw_inode;
079788d0 334 struct dquot *transfer_to[MAXQUOTAS] = { };
9b7365fc 335
0b7b7779 336 if (!ext4_has_feature_project(sb)) {
9b7365fc
LX
337 if (projid != EXT4_DEF_PROJID)
338 return -EOPNOTSUPP;
339 else
340 return 0;
341 }
342
343 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
344 return -EOPNOTSUPP;
345
346 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
347
348 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
349 return 0;
350
9b7365fc 351 err = -EPERM;
9b7365fc 352 /* Is it quota file? Do not allow user to mess with it */
02749a4c 353 if (ext4_is_quota_file(inode))
65dc3dd8 354 return err;
9b7365fc
LX
355
356 err = ext4_get_inode_loc(inode, &iloc);
357 if (err)
65dc3dd8 358 return err;
9b7365fc
LX
359
360 raw_inode = ext4_raw_inode(&iloc);
361 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
c03b45b8
MX
362 err = ext4_expand_extra_isize(inode,
363 EXT4_SB(sb)->s_want_extra_isize,
364 &iloc);
365 if (err)
65dc3dd8 366 return err;
c03b45b8 367 } else {
9b7365fc 368 brelse(iloc.bh);
9b7365fc 369 }
9b7365fc 370
d197b725
WS
371 err = dquot_initialize(inode);
372 if (err)
373 return err;
9b7365fc
LX
374
375 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
376 EXT4_QUOTA_INIT_BLOCKS(sb) +
377 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
65dc3dd8
WS
378 if (IS_ERR(handle))
379 return PTR_ERR(handle);
9b7365fc
LX
380
381 err = ext4_reserve_inode_write(handle, inode, &iloc);
382 if (err)
383 goto out_stop;
384
079788d0
WS
385 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
386 if (!IS_ERR(transfer_to[PRJQUOTA])) {
7a9ca53a
TE
387
388 /* __dquot_transfer() calls back ext4_get_inode_usage() which
389 * counts xattr inode references.
390 */
391 down_read(&EXT4_I(inode)->xattr_sem);
079788d0 392 err = __dquot_transfer(inode, transfer_to);
7a9ca53a 393 up_read(&EXT4_I(inode)->xattr_sem);
079788d0
WS
394 dqput(transfer_to[PRJQUOTA]);
395 if (err)
396 goto out_dirty;
9b7365fc 397 }
079788d0 398
9b7365fc 399 EXT4_I(inode)->i_projid = kprojid;
eeca7ea1 400 inode->i_ctime = current_time(inode);
9b7365fc
LX
401out_dirty:
402 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
403 if (!err)
404 err = rc;
405out_stop:
406 ext4_journal_stop(handle);
9b7365fc
LX
407 return err;
408}
409#else
410static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
411{
412 if (projid != EXT4_DEF_PROJID)
413 return -EOPNOTSUPP;
414 return 0;
415}
416#endif
417
418/* Transfer internal flags to xflags */
419static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
420{
421 __u32 xflags = 0;
422
423 if (iflags & EXT4_SYNC_FL)
424 xflags |= FS_XFLAG_SYNC;
425 if (iflags & EXT4_IMMUTABLE_FL)
426 xflags |= FS_XFLAG_IMMUTABLE;
427 if (iflags & EXT4_APPEND_FL)
428 xflags |= FS_XFLAG_APPEND;
429 if (iflags & EXT4_NODUMP_FL)
430 xflags |= FS_XFLAG_NODUMP;
431 if (iflags & EXT4_NOATIME_FL)
432 xflags |= FS_XFLAG_NOATIME;
433 if (iflags & EXT4_PROJINHERIT_FL)
434 xflags |= FS_XFLAG_PROJINHERIT;
435 return xflags;
436}
437
d14e7683
JK
438#define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
439 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
440 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
441
9b7365fc
LX
442/* Transfer xflags flags to internal */
443static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
444{
445 unsigned long iflags = 0;
446
447 if (xflags & FS_XFLAG_SYNC)
448 iflags |= EXT4_SYNC_FL;
449 if (xflags & FS_XFLAG_IMMUTABLE)
450 iflags |= EXT4_IMMUTABLE_FL;
451 if (xflags & FS_XFLAG_APPEND)
452 iflags |= EXT4_APPEND_FL;
453 if (xflags & FS_XFLAG_NODUMP)
454 iflags |= EXT4_NODUMP_FL;
455 if (xflags & FS_XFLAG_NOATIME)
456 iflags |= EXT4_NOATIME_FL;
457 if (xflags & FS_XFLAG_PROJINHERIT)
458 iflags |= EXT4_PROJINHERIT_FL;
459
460 return iflags;
461}
462
1a20a630 463static int ext4_shutdown(struct super_block *sb, unsigned long arg)
783d9485
TT
464{
465 struct ext4_sb_info *sbi = EXT4_SB(sb);
466 __u32 flags;
467
468 if (!capable(CAP_SYS_ADMIN))
469 return -EPERM;
470
471 if (get_user(flags, (__u32 __user *)arg))
472 return -EFAULT;
473
474 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
475 return -EINVAL;
476
477 if (ext4_forced_shutdown(sbi))
478 return 0;
479
480 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
481
482 switch (flags) {
483 case EXT4_GOING_FLAGS_DEFAULT:
484 freeze_bdev(sb->s_bdev);
485 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
486 thaw_bdev(sb->s_bdev, sb);
487 break;
488 case EXT4_GOING_FLAGS_LOGFLUSH:
489 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
490 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
491 (void) ext4_force_commit(sb);
73297f13 492 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
783d9485
TT
493 }
494 break;
495 case EXT4_GOING_FLAGS_NOLOGFLUSH:
496 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
09439481 497 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
73297f13 498 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
783d9485
TT
499 break;
500 default:
501 return -EINVAL;
502 }
503 clear_opt(sb, DISCARD);
504 return 0;
505}
506
0c9ec4be
DW
507struct getfsmap_info {
508 struct super_block *gi_sb;
509 struct fsmap_head __user *gi_data;
510 unsigned int gi_idx;
511 __u32 gi_last_flags;
512};
513
514static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
515{
516 struct getfsmap_info *info = priv;
517 struct fsmap fm;
518
519 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
520
521 info->gi_last_flags = xfm->fmr_flags;
522 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
523 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
524 sizeof(struct fsmap)))
525 return -EFAULT;
526
527 return 0;
528}
529
530static int ext4_ioc_getfsmap(struct super_block *sb,
531 struct fsmap_head __user *arg)
532{
533 struct getfsmap_info info = {0};
534 struct ext4_fsmap_head xhead = {0};
535 struct fsmap_head head;
536 bool aborted = false;
537 int error;
538
539 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
540 return -EFAULT;
541 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
542 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
543 sizeof(head.fmh_keys[0].fmr_reserved)) ||
544 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
545 sizeof(head.fmh_keys[1].fmr_reserved)))
546 return -EINVAL;
547 /*
548 * ext4 doesn't report file extents at all, so the only valid
549 * file offsets are the magic ones (all zeroes or all ones).
550 */
551 if (head.fmh_keys[0].fmr_offset ||
552 (head.fmh_keys[1].fmr_offset != 0 &&
553 head.fmh_keys[1].fmr_offset != -1ULL))
554 return -EINVAL;
555
556 xhead.fmh_iflags = head.fmh_iflags;
557 xhead.fmh_count = head.fmh_count;
558 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
559 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
560
561 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
562 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
563
564 info.gi_sb = sb;
565 info.gi_data = arg;
566 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
567 if (error == EXT4_QUERY_RANGE_ABORT) {
568 error = 0;
569 aborted = true;
570 } else if (error)
571 return error;
572
573 /* If we didn't abort, set the "last" flag in the last fmx */
574 if (!aborted && info.gi_idx) {
575 info.gi_last_flags |= FMR_OF_LAST;
576 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
577 &info.gi_last_flags,
578 sizeof(info.gi_last_flags)))
579 return -EFAULT;
580 }
581
582 /* copy back header */
583 head.fmh_entries = xhead.fmh_entries;
584 head.fmh_oflags = xhead.fmh_oflags;
585 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
586 return -EFAULT;
587
588 return 0;
589}
590
65dc3dd8
WS
591static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
592{
593 /*
594 * Project Quota ID state is only allowed to change from within the init
595 * namespace. Enforce that restriction only if we are trying to change
596 * the quota ID state. Everything else is allowed in user namespaces.
597 */
598 if (current_user_ns() == &init_user_ns)
599 return 0;
600
601 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
602 return -EINVAL;
603
604 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
605 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
606 return -EINVAL;
607 } else {
608 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
609 return -EINVAL;
610 }
611
612 return 0;
613}
614
5cdd7b2d 615long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ac27a0ec 616{
496ad9aa 617 struct inode *inode = file_inode(filp);
bab08ab9 618 struct super_block *sb = inode->i_sb;
617ba13b 619 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec 620 unsigned int flags;
ac27a0ec 621
af5bc92d 622 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
ac27a0ec
DK
623
624 switch (cmd) {
0c9ec4be
DW
625 case FS_IOC_GETFSMAP:
626 return ext4_ioc_getfsmap(sb, (void __user *)arg);
617ba13b
MC
627 case EXT4_IOC_GETFLAGS:
628 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
ac27a0ec 629 return put_user(flags, (int __user *) arg);
617ba13b 630 case EXT4_IOC_SETFLAGS: {
9b7365fc 631 int err;
ac27a0ec 632
2e149670 633 if (!inode_owner_or_capable(inode))
ac27a0ec
DK
634 return -EACCES;
635
636 if (get_user(flags, (int __user *) arg))
637 return -EFAULT;
638
d14e7683
JK
639 if (flags & ~EXT4_FL_USER_VISIBLE)
640 return -EOPNOTSUPP;
641 /*
642 * chattr(1) grabs flags via GETFLAGS, modifies the result and
643 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
644 * more restrictive than just silently masking off visible but
645 * not settable flags as we always did.
646 */
647 flags &= EXT4_FL_USER_MODIFIABLE;
648 if (ext4_mask_flags(inode->i_mode, flags) != flags)
649 return -EOPNOTSUPP;
650
a561be71 651 err = mnt_want_write_file(filp);
42a74f20
DH
652 if (err)
653 return err;
654
5955102c 655 inode_lock(inode);
9b7365fc 656 err = ext4_ioctl_setflags(inode, flags);
5955102c 657 inode_unlock(inode);
2a79f17e 658 mnt_drop_write_file(filp);
ac27a0ec
DK
659 return err;
660 }
617ba13b
MC
661 case EXT4_IOC_GETVERSION:
662 case EXT4_IOC_GETVERSION_OLD:
ac27a0ec 663 return put_user(inode->i_generation, (int __user *) arg);
617ba13b
MC
664 case EXT4_IOC_SETVERSION:
665 case EXT4_IOC_SETVERSION_OLD: {
ac27a0ec 666 handle_t *handle;
617ba13b 667 struct ext4_iloc iloc;
ac27a0ec
DK
668 __u32 generation;
669 int err;
670
2e149670 671 if (!inode_owner_or_capable(inode))
ac27a0ec 672 return -EPERM;
42a74f20 673
9aa5d32b 674 if (ext4_has_metadata_csum(inode->i_sb)) {
814525f4
DW
675 ext4_warning(sb, "Setting inode version is not "
676 "supported with metadata_csum enabled.");
677 return -ENOTTY;
678 }
679
a561be71 680 err = mnt_want_write_file(filp);
42a74f20
DH
681 if (err)
682 return err;
683 if (get_user(generation, (int __user *) arg)) {
684 err = -EFAULT;
685 goto setversion_out;
686 }
ac27a0ec 687
5955102c 688 inode_lock(inode);
9924a92a 689 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
42a74f20
DH
690 if (IS_ERR(handle)) {
691 err = PTR_ERR(handle);
6c2155b9 692 goto unlock_out;
42a74f20 693 }
617ba13b 694 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec 695 if (err == 0) {
eeca7ea1 696 inode->i_ctime = current_time(inode);
ac27a0ec 697 inode->i_generation = generation;
617ba13b 698 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 699 }
617ba13b 700 ext4_journal_stop(handle);
6c2155b9
DH
701
702unlock_out:
5955102c 703 inode_unlock(inode);
42a74f20 704setversion_out:
2a79f17e 705 mnt_drop_write_file(filp);
ac27a0ec
DK
706 return err;
707 }
617ba13b
MC
708 case EXT4_IOC_GROUP_EXTEND: {
709 ext4_fsblk_t n_blocks_count;
ac046f1d 710 int err, err2=0;
ac27a0ec 711
8f82f840
YY
712 err = ext4_resize_begin(sb);
713 if (err)
714 return err;
ac27a0ec 715
014a1770
DH
716 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
717 err = -EFAULT;
718 goto group_extend_out;
719 }
ac27a0ec 720
e2b911c5 721 if (ext4_has_feature_bigalloc(sb)) {
bab08ab9
TT
722 ext4_msg(sb, KERN_ERR,
723 "Online resizing not supported with bigalloc");
014a1770
DH
724 err = -EOPNOTSUPP;
725 goto group_extend_out;
bab08ab9
TT
726 }
727
a561be71 728 err = mnt_want_write_file(filp);
42a74f20 729 if (err)
014a1770 730 goto group_extend_out;
42a74f20 731
617ba13b 732 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
ac046f1d
PT
733 if (EXT4_SB(sb)->s_journal) {
734 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
735 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
736 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
737 }
7ffe1ea8
HK
738 if (err == 0)
739 err = err2;
2a79f17e 740 mnt_drop_write_file(filp);
014a1770 741group_extend_out:
8f82f840 742 ext4_resize_end(sb);
ac27a0ec
DK
743 return err;
744 }
748de673
AF
745
746 case EXT4_IOC_MOVE_EXT: {
747 struct move_extent me;
2903ff01
AV
748 struct fd donor;
749 int err;
748de673 750
4a58579b
AF
751 if (!(filp->f_mode & FMODE_READ) ||
752 !(filp->f_mode & FMODE_WRITE))
753 return -EBADF;
754
748de673
AF
755 if (copy_from_user(&me,
756 (struct move_extent __user *)arg, sizeof(me)))
757 return -EFAULT;
4a58579b 758 me.moved_len = 0;
748de673 759
2903ff01
AV
760 donor = fdget(me.donor_fd);
761 if (!donor.file)
748de673
AF
762 return -EBADF;
763
2903ff01 764 if (!(donor.file->f_mode & FMODE_WRITE)) {
4a58579b
AF
765 err = -EBADF;
766 goto mext_out;
748de673
AF
767 }
768
e2b911c5 769 if (ext4_has_feature_bigalloc(sb)) {
bab08ab9
TT
770 ext4_msg(sb, KERN_ERR,
771 "Online defrag not supported with bigalloc");
399c9b86
AV
772 err = -EOPNOTSUPP;
773 goto mext_out;
73f34a5e
RZ
774 } else if (IS_DAX(inode)) {
775 ext4_msg(sb, KERN_ERR,
776 "Online defrag not supported with DAX");
777 err = -EOPNOTSUPP;
778 goto mext_out;
bab08ab9
TT
779 }
780
a561be71 781 err = mnt_want_write_file(filp);
4a58579b
AF
782 if (err)
783 goto mext_out;
784
2903ff01 785 err = ext4_move_extents(filp, donor.file, me.orig_start,
748de673 786 me.donor_start, me.len, &me.moved_len);
2a79f17e 787 mnt_drop_write_file(filp);
748de673 788
60e6679e 789 if (copy_to_user((struct move_extent __user *)arg,
c437b273 790 &me, sizeof(me)))
4a58579b
AF
791 err = -EFAULT;
792mext_out:
2903ff01 793 fdput(donor);
748de673
AF
794 return err;
795 }
796
617ba13b
MC
797 case EXT4_IOC_GROUP_ADD: {
798 struct ext4_new_group_data input;
ac046f1d 799 int err, err2=0;
ac27a0ec 800
8f82f840
YY
801 err = ext4_resize_begin(sb);
802 if (err)
803 return err;
ac27a0ec 804
617ba13b 805 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
014a1770
DH
806 sizeof(input))) {
807 err = -EFAULT;
808 goto group_add_out;
809 }
ac27a0ec 810
e2b911c5 811 if (ext4_has_feature_bigalloc(sb)) {
bab08ab9
TT
812 ext4_msg(sb, KERN_ERR,
813 "Online resizing not supported with bigalloc");
014a1770
DH
814 err = -EOPNOTSUPP;
815 goto group_add_out;
bab08ab9
TT
816 }
817
a561be71 818 err = mnt_want_write_file(filp);
42a74f20 819 if (err)
014a1770 820 goto group_add_out;
42a74f20 821
617ba13b 822 err = ext4_group_add(sb, &input);
ac046f1d
PT
823 if (EXT4_SB(sb)->s_journal) {
824 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
825 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
826 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
827 }
7ffe1ea8
HK
828 if (err == 0)
829 err = err2;
2a79f17e 830 mnt_drop_write_file(filp);
7f511862
TT
831 if (!err && ext4_has_group_desc_csum(sb) &&
832 test_opt(sb, INIT_INODE_TABLE))
833 err = ext4_register_li_request(sb, input.group);
014a1770 834group_add_out:
8f82f840 835 ext4_resize_end(sb);
ac27a0ec
DK
836 return err;
837 }
838
c14c6fd5 839 case EXT4_IOC_MIGRATE:
2a43a878
AK
840 {
841 int err;
2e149670 842 if (!inode_owner_or_capable(inode))
2a43a878
AK
843 return -EACCES;
844
a561be71 845 err = mnt_want_write_file(filp);
2a43a878
AK
846 if (err)
847 return err;
848 /*
849 * inode_mutex prevent write and truncate on the file.
850 * Read still goes through. We take i_data_sem in
851 * ext4_ext_swap_inode_data before we switch the
852 * inode format to prevent read.
853 */
5955102c 854 inode_lock((inode));
2a43a878 855 err = ext4_ext_migrate(inode);
5955102c 856 inode_unlock((inode));
2a79f17e 857 mnt_drop_write_file(filp);
2a43a878
AK
858 return err;
859 }
c14c6fd5 860
ccd2506b
TT
861 case EXT4_IOC_ALLOC_DA_BLKS:
862 {
863 int err;
2e149670 864 if (!inode_owner_or_capable(inode))
ccd2506b
TT
865 return -EACCES;
866
a561be71 867 err = mnt_want_write_file(filp);
ccd2506b
TT
868 if (err)
869 return err;
870 err = ext4_alloc_da_blocks(inode);
2a79f17e 871 mnt_drop_write_file(filp);
ccd2506b
TT
872 return err;
873 }
874
393d1d1d 875 case EXT4_IOC_SWAP_BOOT:
3e67cfad
DM
876 {
877 int err;
393d1d1d
DTB
878 if (!(filp->f_mode & FMODE_WRITE))
879 return -EBADF;
3e67cfad
DM
880 err = mnt_want_write_file(filp);
881 if (err)
882 return err;
883 err = swap_inode_boot_loader(sb, inode);
884 mnt_drop_write_file(filp);
885 return err;
886 }
393d1d1d 887
19c5246d
YY
888 case EXT4_IOC_RESIZE_FS: {
889 ext4_fsblk_t n_blocks_count;
19c5246d 890 int err = 0, err2 = 0;
7f511862 891 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
19c5246d 892
e2b911c5 893 if (ext4_has_feature_bigalloc(sb)) {
19c5246d
YY
894 ext4_msg(sb, KERN_ERR,
895 "Online resizing not (yet) supported with bigalloc");
896 return -EOPNOTSUPP;
897 }
898
19c5246d
YY
899 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
900 sizeof(__u64))) {
901 return -EFAULT;
902 }
903
19c5246d
YY
904 err = ext4_resize_begin(sb);
905 if (err)
906 return err;
907
8cae6f71 908 err = mnt_want_write_file(filp);
19c5246d
YY
909 if (err)
910 goto resizefs_out;
911
912 err = ext4_resize_fs(sb, n_blocks_count);
913 if (EXT4_SB(sb)->s_journal) {
914 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
915 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
916 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
917 }
918 if (err == 0)
919 err = err2;
8cae6f71 920 mnt_drop_write_file(filp);
7f511862
TT
921 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
922 ext4_has_group_desc_csum(sb) &&
923 test_opt(sb, INIT_INODE_TABLE))
924 err = ext4_register_li_request(sb, o_group);
925
19c5246d
YY
926resizefs_out:
927 ext4_resize_end(sb);
928 return err;
929 }
930
e681c047
LC
931 case FITRIM:
932 {
41431792 933 struct request_queue *q = bdev_get_queue(sb->s_bdev);
e681c047
LC
934 struct fstrim_range range;
935 int ret = 0;
936
937 if (!capable(CAP_SYS_ADMIN))
938 return -EPERM;
939
41431792
LC
940 if (!blk_queue_discard(q))
941 return -EOPNOTSUPP;
942
6fee657d
DW
943 /*
944 * We haven't replayed the journal, so we cannot use our
945 * block-bitmap-guided storage zapping commands.
946 */
947 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
948 return -EROFS;
949
e6705f7c 950 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
e681c047
LC
951 sizeof(range)))
952 return -EFAULT;
953
5c2ed62f
LC
954 range.minlen = max((unsigned int)range.minlen,
955 q->limits.discard_granularity);
e681c047
LC
956 ret = ext4_trim_fs(sb, &range);
957 if (ret < 0)
958 return ret;
959
e6705f7c 960 if (copy_to_user((struct fstrim_range __user *)arg, &range,
e681c047
LC
961 sizeof(range)))
962 return -EFAULT;
963
964 return 0;
965 }
7869a4a6
TT
966 case EXT4_IOC_PRECACHE_EXTENTS:
967 return ext4_ext_precache(inode);
9bd8212f 968
db717d8e 969 case EXT4_IOC_SET_ENCRYPTION_POLICY:
9a200d07
RW
970 if (!ext4_has_feature_encrypt(sb))
971 return -EOPNOTSUPP;
db717d8e 972 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
9a200d07 973
9bd8212f 974 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
ba679017 975#ifdef CONFIG_EXT4_FS_ENCRYPTION
9bd8212f
MH
976 int err, err2;
977 struct ext4_sb_info *sbi = EXT4_SB(sb);
978 handle_t *handle;
979
35997d1c 980 if (!ext4_has_feature_encrypt(sb))
9bd8212f
MH
981 return -EOPNOTSUPP;
982 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
983 err = mnt_want_write_file(filp);
984 if (err)
985 return err;
986 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
987 if (IS_ERR(handle)) {
988 err = PTR_ERR(handle);
989 goto pwsalt_err_exit;
990 }
991 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
992 if (err)
993 goto pwsalt_err_journal;
994 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
995 err = ext4_handle_dirty_metadata(handle, NULL,
996 sbi->s_sbh);
997 pwsalt_err_journal:
998 err2 = ext4_journal_stop(handle);
999 if (err2 && !err)
1000 err = err2;
1001 pwsalt_err_exit:
1002 mnt_drop_write_file(filp);
1003 if (err)
1004 return err;
1005 }
b4ab9e29
FF
1006 if (copy_to_user((void __user *) arg,
1007 sbi->s_es->s_encrypt_pw_salt, 16))
9bd8212f
MH
1008 return -EFAULT;
1009 return 0;
ba679017
EB
1010#else
1011 return -EOPNOTSUPP;
1012#endif
9bd8212f 1013 }
db717d8e
EB
1014 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1015 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
9bd8212f 1016
9b7365fc
LX
1017 case EXT4_IOC_FSGETXATTR:
1018 {
1019 struct fsxattr fa;
1020
1021 memset(&fa, 0, sizeof(struct fsxattr));
9b7365fc
LX
1022 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1023
0b7b7779 1024 if (ext4_has_feature_project(inode->i_sb)) {
9b7365fc
LX
1025 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1026 EXT4_I(inode)->i_projid);
1027 }
1028
1029 if (copy_to_user((struct fsxattr __user *)arg,
1030 &fa, sizeof(fa)))
1031 return -EFAULT;
1032 return 0;
1033 }
1034 case EXT4_IOC_FSSETXATTR:
1035 {
1036 struct fsxattr fa;
1037 int err;
1038
1039 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1040 sizeof(fa)))
1041 return -EFAULT;
1042
1043 /* Make sure caller has proper permission */
1044 if (!inode_owner_or_capable(inode))
1045 return -EACCES;
1046
d14e7683
JK
1047 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1048 return -EOPNOTSUPP;
1049
1050 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1051 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1052 return -EOPNOTSUPP;
1053
9b7365fc
LX
1054 err = mnt_want_write_file(filp);
1055 if (err)
1056 return err;
1057
5955102c 1058 inode_lock(inode);
65dc3dd8
WS
1059 err = ext4_ioctl_check_project(inode, &fa);
1060 if (err)
1061 goto out;
9b7365fc
LX
1062 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1063 (flags & EXT4_FL_XFLAG_VISIBLE);
1064 err = ext4_ioctl_setflags(inode, flags);
9b7365fc 1065 if (err)
65dc3dd8 1066 goto out;
9b7365fc 1067 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
65dc3dd8
WS
1068out:
1069 inode_unlock(inode);
1070 mnt_drop_write_file(filp);
1071 return err;
9b7365fc 1072 }
e9be2ac7
TT
1073 case EXT4_IOC_SHUTDOWN:
1074 return ext4_shutdown(sb, arg);
ac27a0ec
DK
1075 default:
1076 return -ENOTTY;
1077 }
1078}
1079
1080#ifdef CONFIG_COMPAT
617ba13b 1081long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ac27a0ec 1082{
ac27a0ec
DK
1083 /* These are just misnamed, they actually get/put from/to user an int */
1084 switch (cmd) {
617ba13b
MC
1085 case EXT4_IOC32_GETFLAGS:
1086 cmd = EXT4_IOC_GETFLAGS;
ac27a0ec 1087 break;
617ba13b
MC
1088 case EXT4_IOC32_SETFLAGS:
1089 cmd = EXT4_IOC_SETFLAGS;
ac27a0ec 1090 break;
617ba13b
MC
1091 case EXT4_IOC32_GETVERSION:
1092 cmd = EXT4_IOC_GETVERSION;
ac27a0ec 1093 break;
617ba13b
MC
1094 case EXT4_IOC32_SETVERSION:
1095 cmd = EXT4_IOC_SETVERSION;
ac27a0ec 1096 break;
617ba13b
MC
1097 case EXT4_IOC32_GROUP_EXTEND:
1098 cmd = EXT4_IOC_GROUP_EXTEND;
ac27a0ec 1099 break;
617ba13b
MC
1100 case EXT4_IOC32_GETVERSION_OLD:
1101 cmd = EXT4_IOC_GETVERSION_OLD;
ac27a0ec 1102 break;
617ba13b
MC
1103 case EXT4_IOC32_SETVERSION_OLD:
1104 cmd = EXT4_IOC_SETVERSION_OLD;
ac27a0ec 1105 break;
617ba13b
MC
1106 case EXT4_IOC32_GETRSVSZ:
1107 cmd = EXT4_IOC_GETRSVSZ;
ac27a0ec 1108 break;
617ba13b
MC
1109 case EXT4_IOC32_SETRSVSZ:
1110 cmd = EXT4_IOC_SETRSVSZ;
ac27a0ec 1111 break;
4d92dc0f
BH
1112 case EXT4_IOC32_GROUP_ADD: {
1113 struct compat_ext4_new_group_input __user *uinput;
1114 struct ext4_new_group_input input;
1115 mm_segment_t old_fs;
1116 int err;
1117
1118 uinput = compat_ptr(arg);
1119 err = get_user(input.group, &uinput->group);
1120 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1121 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1122 err |= get_user(input.inode_table, &uinput->inode_table);
1123 err |= get_user(input.blocks_count, &uinput->blocks_count);
1124 err |= get_user(input.reserved_blocks,
1125 &uinput->reserved_blocks);
1126 if (err)
1127 return -EFAULT;
1128 old_fs = get_fs();
1129 set_fs(KERNEL_DS);
1130 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
1131 (unsigned long) &input);
1132 set_fs(old_fs);
1133 return err;
1134 }
b684b2ee 1135 case EXT4_IOC_MOVE_EXT:
19c5246d 1136 case EXT4_IOC_RESIZE_FS:
7869a4a6 1137 case EXT4_IOC_PRECACHE_EXTENTS:
9bd8212f
MH
1138 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1139 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1140 case EXT4_IOC_GET_ENCRYPTION_POLICY:
e9be2ac7 1141 case EXT4_IOC_SHUTDOWN:
0c9ec4be 1142 case FS_IOC_GETFSMAP:
b684b2ee 1143 break;
ac27a0ec
DK
1144 default:
1145 return -ENOIOCTLCMD;
1146 }
5cdd7b2d 1147 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
ac27a0ec
DK
1148}
1149#endif