defconfig: exynos9610: Re-add dropped Wi-Fi AP options lost
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / fs / ext4 / ioctl.c
... / ...
CommitLineData
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/ext4/ioctl.c
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>
12#include <linux/capability.h>
13#include <linux/time.h>
14#include <linux/compat.h>
15#include <linux/mount.h>
16#include <linux/file.h>
17#include <linux/quotaops.h>
18#include <linux/uuid.h>
19#include <linux/uaccess.h>
20#include <linux/delay.h>
21#include "ext4_jbd2.h"
22#include "ext4.h"
23#include <linux/fsmap.h>
24#include "fsmap.h"
25#include <trace/events/ext4.h>
26
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;
38
39 ap = (unsigned char *)a;
40 bp = (unsigned char *)b;
41 while (len-- > 0) {
42 swap(*ap, *bp);
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;
64 unsigned long tmp;
65
66 ei1 = EXT4_I(inode1);
67 ei2 = EXT4_I(inode2);
68
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);
75
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
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);
81 swap(ei1->i_disksize, ei2->i_disksize);
82 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
83 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
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;
105 struct ext4_inode_info *ei_bl;
106 struct ext4_sb_info *sbi = EXT4_SB(sb);
107
108 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
109 return -EINVAL;
110
111 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
112 return -EPERM;
113
114 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
115 if (IS_ERR(inode_bl))
116 return PTR_ERR(inode_bl);
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. */
124 lock_two_nondirectories(inode, inode_bl);
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;
138 goto journal_err_out;
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;
154 if (ext4_has_feature_extents(sb)) {
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
163 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
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 }
190 ext4_journal_stop(handle);
191 ext4_double_up_write_data_sem(inode, inode_bl);
192
193journal_err_out:
194 ext4_inode_resume_unlocked_dio(inode);
195 ext4_inode_resume_unlocked_dio(inode_bl);
196 unlock_two_nondirectories(inode, inode_bl);
197 iput(inode_bl);
198 return err;
199}
200
201#ifdef CONFIG_EXT4_FS_ENCRYPTION
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}
211#endif
212
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;
218 int err = -EPERM, migrate = 0;
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 */
224 if (ext4_is_quota_file(inode))
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 }
260 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
261 err = ext4_truncate(inode);
262 if (err)
263 goto flags_out;
264 }
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;
280 /* These flags get special treatment later */
281 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
282 continue;
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);
290 inode->i_ctime = current_time(inode);
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
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
308 err = ext4_change_inode_journal_flag(inode, jflag);
309 if (err)
310 goto flags_out;
311 }
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;
334 struct dquot *transfer_to[MAXQUOTAS] = { };
335
336 if (!ext4_has_feature_project(sb)) {
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
351 err = -EPERM;
352 /* Is it quota file? Do not allow user to mess with it */
353 if (ext4_is_quota_file(inode))
354 return err;
355
356 err = ext4_get_inode_loc(inode, &iloc);
357 if (err)
358 return err;
359
360 raw_inode = ext4_raw_inode(&iloc);
361 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
362 err = ext4_expand_extra_isize(inode,
363 EXT4_SB(sb)->s_want_extra_isize,
364 &iloc);
365 if (err)
366 return err;
367 } else {
368 brelse(iloc.bh);
369 }
370
371 err = dquot_initialize(inode);
372 if (err)
373 return err;
374
375 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
376 EXT4_QUOTA_INIT_BLOCKS(sb) +
377 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
378 if (IS_ERR(handle))
379 return PTR_ERR(handle);
380
381 err = ext4_reserve_inode_write(handle, inode, &iloc);
382 if (err)
383 goto out_stop;
384
385 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
386 if (!IS_ERR(transfer_to[PRJQUOTA])) {
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);
392 err = __dquot_transfer(inode, transfer_to);
393 up_read(&EXT4_I(inode)->xattr_sem);
394 dqput(transfer_to[PRJQUOTA]);
395 if (err)
396 goto out_dirty;
397 }
398
399 EXT4_I(inode)->i_projid = kprojid;
400 inode->i_ctime = current_time(inode);
401out_dirty:
402 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
403 if (!err)
404 err = rc;
405out_stop:
406 ext4_journal_stop(handle);
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
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
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
463static int ext4_shutdown(struct super_block *sb, unsigned long arg)
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);
492 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
493 }
494 break;
495 case EXT4_GOING_FLAGS_NOLOGFLUSH:
496 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
497 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
498 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
499 break;
500 default:
501 return -EINVAL;
502 }
503 clear_opt(sb, DISCARD);
504 return 0;
505}
506
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
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
615long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
616{
617 struct inode *inode = file_inode(filp);
618 struct super_block *sb = inode->i_sb;
619 struct ext4_inode_info *ei = EXT4_I(inode);
620 unsigned int flags;
621
622 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
623
624 switch (cmd) {
625 case FS_IOC_GETFSMAP:
626 return ext4_ioc_getfsmap(sb, (void __user *)arg);
627 case EXT4_IOC_GETFLAGS:
628 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
629 return put_user(flags, (int __user *) arg);
630 case EXT4_IOC_SETFLAGS: {
631 int err;
632
633 if (!inode_owner_or_capable(inode))
634 return -EACCES;
635
636 if (get_user(flags, (int __user *) arg))
637 return -EFAULT;
638
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
651 err = mnt_want_write_file(filp);
652 if (err)
653 return err;
654
655 inode_lock(inode);
656 err = ext4_ioctl_setflags(inode, flags);
657 inode_unlock(inode);
658 mnt_drop_write_file(filp);
659 return err;
660 }
661 case EXT4_IOC_GETVERSION:
662 case EXT4_IOC_GETVERSION_OLD:
663 return put_user(inode->i_generation, (int __user *) arg);
664 case EXT4_IOC_SETVERSION:
665 case EXT4_IOC_SETVERSION_OLD: {
666 handle_t *handle;
667 struct ext4_iloc iloc;
668 __u32 generation;
669 int err;
670
671 if (!inode_owner_or_capable(inode))
672 return -EPERM;
673
674 if (ext4_has_metadata_csum(inode->i_sb)) {
675 ext4_warning(sb, "Setting inode version is not "
676 "supported with metadata_csum enabled.");
677 return -ENOTTY;
678 }
679
680 err = mnt_want_write_file(filp);
681 if (err)
682 return err;
683 if (get_user(generation, (int __user *) arg)) {
684 err = -EFAULT;
685 goto setversion_out;
686 }
687
688 inode_lock(inode);
689 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
690 if (IS_ERR(handle)) {
691 err = PTR_ERR(handle);
692 goto unlock_out;
693 }
694 err = ext4_reserve_inode_write(handle, inode, &iloc);
695 if (err == 0) {
696 inode->i_ctime = current_time(inode);
697 inode->i_generation = generation;
698 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
699 }
700 ext4_journal_stop(handle);
701
702unlock_out:
703 inode_unlock(inode);
704setversion_out:
705 mnt_drop_write_file(filp);
706 return err;
707 }
708 case EXT4_IOC_GROUP_EXTEND: {
709 ext4_fsblk_t n_blocks_count;
710 int err, err2=0;
711
712 err = ext4_resize_begin(sb);
713 if (err)
714 return err;
715
716 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
717 err = -EFAULT;
718 goto group_extend_out;
719 }
720
721 if (ext4_has_feature_bigalloc(sb)) {
722 ext4_msg(sb, KERN_ERR,
723 "Online resizing not supported with bigalloc");
724 err = -EOPNOTSUPP;
725 goto group_extend_out;
726 }
727
728 err = mnt_want_write_file(filp);
729 if (err)
730 goto group_extend_out;
731
732 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
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 }
738 if (err == 0)
739 err = err2;
740 mnt_drop_write_file(filp);
741group_extend_out:
742 ext4_resize_end(sb);
743 return err;
744 }
745
746 case EXT4_IOC_MOVE_EXT: {
747 struct move_extent me;
748 struct fd donor;
749 int err;
750
751 if (!(filp->f_mode & FMODE_READ) ||
752 !(filp->f_mode & FMODE_WRITE))
753 return -EBADF;
754
755 if (copy_from_user(&me,
756 (struct move_extent __user *)arg, sizeof(me)))
757 return -EFAULT;
758 me.moved_len = 0;
759
760 donor = fdget(me.donor_fd);
761 if (!donor.file)
762 return -EBADF;
763
764 if (!(donor.file->f_mode & FMODE_WRITE)) {
765 err = -EBADF;
766 goto mext_out;
767 }
768
769 if (ext4_has_feature_bigalloc(sb)) {
770 ext4_msg(sb, KERN_ERR,
771 "Online defrag not supported with bigalloc");
772 err = -EOPNOTSUPP;
773 goto mext_out;
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;
779 }
780
781 err = mnt_want_write_file(filp);
782 if (err)
783 goto mext_out;
784
785 err = ext4_move_extents(filp, donor.file, me.orig_start,
786 me.donor_start, me.len, &me.moved_len);
787 mnt_drop_write_file(filp);
788
789 if (copy_to_user((struct move_extent __user *)arg,
790 &me, sizeof(me)))
791 err = -EFAULT;
792mext_out:
793 fdput(donor);
794 return err;
795 }
796
797 case EXT4_IOC_GROUP_ADD: {
798 struct ext4_new_group_data input;
799 int err, err2=0;
800
801 err = ext4_resize_begin(sb);
802 if (err)
803 return err;
804
805 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
806 sizeof(input))) {
807 err = -EFAULT;
808 goto group_add_out;
809 }
810
811 if (ext4_has_feature_bigalloc(sb)) {
812 ext4_msg(sb, KERN_ERR,
813 "Online resizing not supported with bigalloc");
814 err = -EOPNOTSUPP;
815 goto group_add_out;
816 }
817
818 err = mnt_want_write_file(filp);
819 if (err)
820 goto group_add_out;
821
822 err = ext4_group_add(sb, &input);
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 }
828 if (err == 0)
829 err = err2;
830 mnt_drop_write_file(filp);
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);
834group_add_out:
835 ext4_resize_end(sb);
836 return err;
837 }
838
839 case EXT4_IOC_MIGRATE:
840 {
841 int err;
842 if (!inode_owner_or_capable(inode))
843 return -EACCES;
844
845 err = mnt_want_write_file(filp);
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 */
854 inode_lock((inode));
855 err = ext4_ext_migrate(inode);
856 inode_unlock((inode));
857 mnt_drop_write_file(filp);
858 return err;
859 }
860
861 case EXT4_IOC_ALLOC_DA_BLKS:
862 {
863 int err;
864 if (!inode_owner_or_capable(inode))
865 return -EACCES;
866
867 err = mnt_want_write_file(filp);
868 if (err)
869 return err;
870 err = ext4_alloc_da_blocks(inode);
871 mnt_drop_write_file(filp);
872 return err;
873 }
874
875 case EXT4_IOC_SWAP_BOOT:
876 {
877 int err;
878 if (!(filp->f_mode & FMODE_WRITE))
879 return -EBADF;
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 }
887
888 case EXT4_IOC_RESIZE_FS: {
889 ext4_fsblk_t n_blocks_count;
890 int err = 0, err2 = 0;
891 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
892
893 if (ext4_has_feature_bigalloc(sb)) {
894 ext4_msg(sb, KERN_ERR,
895 "Online resizing not (yet) supported with bigalloc");
896 return -EOPNOTSUPP;
897 }
898
899 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
900 sizeof(__u64))) {
901 return -EFAULT;
902 }
903
904 err = ext4_resize_begin(sb);
905 if (err)
906 return err;
907
908 err = mnt_want_write_file(filp);
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;
920 mnt_drop_write_file(filp);
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
926resizefs_out:
927 ext4_resize_end(sb);
928 return err;
929 }
930
931 case FITRIM:
932 {
933 struct request_queue *q = bdev_get_queue(sb->s_bdev);
934 struct fstrim_range range;
935 int ret = 0;
936
937 if (!capable(CAP_SYS_ADMIN))
938 return -EPERM;
939
940 if (!blk_queue_discard(q))
941 return -EOPNOTSUPP;
942
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
950 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
951 sizeof(range)))
952 return -EFAULT;
953
954 range.minlen = max((unsigned int)range.minlen,
955 q->limits.discard_granularity);
956 ret = ext4_trim_fs(sb, &range);
957 if (ret < 0)
958 return ret;
959
960 if (copy_to_user((struct fstrim_range __user *)arg, &range,
961 sizeof(range)))
962 return -EFAULT;
963
964 return 0;
965 }
966 case EXT4_IOC_PRECACHE_EXTENTS:
967 return ext4_ext_precache(inode);
968
969 case EXT4_IOC_SET_ENCRYPTION_POLICY:
970 if (!ext4_has_feature_encrypt(sb))
971 return -EOPNOTSUPP;
972 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
973
974 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
975#ifdef CONFIG_EXT4_FS_ENCRYPTION
976 int err, err2;
977 struct ext4_sb_info *sbi = EXT4_SB(sb);
978 handle_t *handle;
979
980 if (!ext4_has_feature_encrypt(sb))
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 }
1006 if (copy_to_user((void __user *) arg,
1007 sbi->s_es->s_encrypt_pw_salt, 16))
1008 return -EFAULT;
1009 return 0;
1010#else
1011 return -EOPNOTSUPP;
1012#endif
1013 }
1014 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1015 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1016
1017 case EXT4_IOC_FSGETXATTR:
1018 {
1019 struct fsxattr fa;
1020
1021 memset(&fa, 0, sizeof(struct fsxattr));
1022 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1023
1024 if (ext4_has_feature_project(inode->i_sb)) {
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
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
1054 err = mnt_want_write_file(filp);
1055 if (err)
1056 return err;
1057
1058 inode_lock(inode);
1059 err = ext4_ioctl_check_project(inode, &fa);
1060 if (err)
1061 goto out;
1062 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1063 (flags & EXT4_FL_XFLAG_VISIBLE);
1064 err = ext4_ioctl_setflags(inode, flags);
1065 if (err)
1066 goto out;
1067 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1068out:
1069 inode_unlock(inode);
1070 mnt_drop_write_file(filp);
1071 return err;
1072 }
1073 case EXT4_IOC_SHUTDOWN:
1074 return ext4_shutdown(sb, arg);
1075 default:
1076 return -ENOTTY;
1077 }
1078}
1079
1080#ifdef CONFIG_COMPAT
1081long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1082{
1083 /* These are just misnamed, they actually get/put from/to user an int */
1084 switch (cmd) {
1085 case EXT4_IOC32_GETFLAGS:
1086 cmd = EXT4_IOC_GETFLAGS;
1087 break;
1088 case EXT4_IOC32_SETFLAGS:
1089 cmd = EXT4_IOC_SETFLAGS;
1090 break;
1091 case EXT4_IOC32_GETVERSION:
1092 cmd = EXT4_IOC_GETVERSION;
1093 break;
1094 case EXT4_IOC32_SETVERSION:
1095 cmd = EXT4_IOC_SETVERSION;
1096 break;
1097 case EXT4_IOC32_GROUP_EXTEND:
1098 cmd = EXT4_IOC_GROUP_EXTEND;
1099 break;
1100 case EXT4_IOC32_GETVERSION_OLD:
1101 cmd = EXT4_IOC_GETVERSION_OLD;
1102 break;
1103 case EXT4_IOC32_SETVERSION_OLD:
1104 cmd = EXT4_IOC_SETVERSION_OLD;
1105 break;
1106 case EXT4_IOC32_GETRSVSZ:
1107 cmd = EXT4_IOC_GETRSVSZ;
1108 break;
1109 case EXT4_IOC32_SETRSVSZ:
1110 cmd = EXT4_IOC_SETRSVSZ;
1111 break;
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 }
1135 case EXT4_IOC_MOVE_EXT:
1136 case EXT4_IOC_RESIZE_FS:
1137 case EXT4_IOC_PRECACHE_EXTENTS:
1138 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1139 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1140 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1141 case EXT4_IOC_SHUTDOWN:
1142 case FS_IOC_GETFSMAP:
1143 break;
1144 default:
1145 return -ENOIOCTLCMD;
1146 }
1147 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1148}
1149#endif