1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/ioctl.c
5 * Copyright (C) 1993, 1994, 1995
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
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/random.h>
19 #include <linux/uuid.h>
20 #include <linux/uaccess.h>
21 #include <linux/delay.h>
22 #include <linux/iversion.h>
23 #include "ext4_jbd2.h"
25 #include <linux/fsmap.h>
27 #include <trace/events/ext4.h>
30 * Swap memory between @a and @b for @len bytes.
32 * @a: pointer to first memory area
33 * @b: pointer to second memory area
34 * @len: number of bytes to swap
37 static void memswap(void *a, void *b, size_t len)
39 unsigned char *ap, *bp;
41 ap = (unsigned char *)a;
42 bp = (unsigned char *)b;
51 * Swap i_data and associated attributes between @inode1 and @inode2.
52 * This function is used for the primary swap between inode1 and inode2
53 * and also to revert this primary swap in case of errors.
55 * Therefore you have to make sure, that calling this method twice
56 * will revert all changes.
58 * @inode1: pointer to first inode
59 * @inode2: pointer to second inode
61 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
64 struct ext4_inode_info *ei1;
65 struct ext4_inode_info *ei2;
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);
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
77 swap(ei1->i_flags, ei2->i_flags);
78 swap(ei1->i_disksize, ei2->i_disksize);
79 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
80 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
82 isize = i_size_read(inode1);
83 i_size_write(inode1, i_size_read(inode2));
84 i_size_write(inode2, isize);
87 static void reset_inode_seed(struct inode *inode)
89 struct ext4_inode_info *ei = EXT4_I(inode);
90 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
91 __le32 inum = cpu_to_le32(inode->i_ino);
92 __le32 gen = cpu_to_le32(inode->i_generation);
95 if (!ext4_has_metadata_csum(inode->i_sb))
98 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
99 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
103 * Swap the information from the given @inode and the inode
104 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
105 * important fields of the inodes.
107 * @sb: the super block of the filesystem
108 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
111 static long swap_inode_boot_loader(struct super_block *sb,
116 struct inode *inode_bl;
117 struct ext4_inode_info *ei_bl;
119 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
120 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
121 ext4_has_inline_data(inode))
124 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
125 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
128 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
129 if (IS_ERR(inode_bl))
130 return PTR_ERR(inode_bl);
131 ei_bl = EXT4_I(inode_bl);
133 filemap_flush(inode->i_mapping);
134 filemap_flush(inode_bl->i_mapping);
136 /* Protect orig inodes against a truncate and make sure,
137 * that only 1 swap_inode_boot_loader is running. */
138 lock_two_nondirectories(inode, inode_bl);
140 /* Wait for all existing dio workers */
141 inode_dio_wait(inode);
142 inode_dio_wait(inode_bl);
144 truncate_inode_pages(&inode->i_data, 0);
145 truncate_inode_pages(&inode_bl->i_data, 0);
147 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
148 if (IS_ERR(handle)) {
150 goto journal_err_out;
153 /* Protect extent tree against block allocations via delalloc */
154 ext4_double_down_write_data_sem(inode, inode_bl);
156 if (inode_bl->i_nlink == 0) {
157 /* this inode has never been used as a BOOT_LOADER */
158 set_nlink(inode_bl, 1);
159 i_uid_write(inode_bl, 0);
160 i_gid_write(inode_bl, 0);
161 inode_bl->i_flags = 0;
163 inode_set_iversion(inode_bl, 1);
164 i_size_write(inode_bl, 0);
165 inode_bl->i_mode = S_IFREG;
166 if (ext4_has_feature_extents(sb)) {
167 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
168 ext4_ext_tree_init(handle, inode_bl);
170 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
173 swap_inode_data(inode, inode_bl);
175 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
177 inode->i_generation = prandom_u32();
178 inode_bl->i_generation = prandom_u32();
179 reset_inode_seed(inode);
180 reset_inode_seed(inode_bl);
182 ext4_discard_preallocations(inode);
184 err = ext4_mark_inode_dirty(handle, inode);
186 ext4_warning(inode->i_sb,
187 "couldn't mark inode #%lu dirty (err %d)",
189 /* Revert all changes: */
190 swap_inode_data(inode, inode_bl);
191 ext4_mark_inode_dirty(handle, inode);
193 err = ext4_mark_inode_dirty(handle, inode_bl);
195 ext4_warning(inode_bl->i_sb,
196 "couldn't mark inode #%lu dirty (err %d)",
197 inode_bl->i_ino, err);
198 /* Revert all changes: */
199 swap_inode_data(inode, inode_bl);
200 ext4_mark_inode_dirty(handle, inode);
201 ext4_mark_inode_dirty(handle, inode_bl);
204 ext4_journal_stop(handle);
205 ext4_double_up_write_data_sem(inode, inode_bl);
208 unlock_two_nondirectories(inode, inode_bl);
213 #ifdef CONFIG_EXT4_FS_ENCRYPTION
214 static int uuid_is_zero(__u8 u[16])
218 for (i = 0; i < 16; i++)
225 static int ext4_ioctl_setflags(struct inode *inode,
228 struct ext4_inode_info *ei = EXT4_I(inode);
229 handle_t *handle = NULL;
230 int err = -EPERM, migrate = 0;
231 struct ext4_iloc iloc;
232 unsigned int oldflags, mask, i;
235 /* Is it quota file? Do not allow user to mess with it */
236 if (ext4_is_quota_file(inode))
239 oldflags = ei->i_flags;
241 /* The JOURNAL_DATA flag is modifiable only by root */
242 jflag = flags & EXT4_JOURNAL_DATA_FL;
245 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
246 * the relevant capability.
248 * This test looks nicer. Thanks to Pauline Middelink
250 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
251 if (!capable(CAP_LINUX_IMMUTABLE))
256 * The JOURNAL_DATA flag can only be changed by
257 * the relevant capability.
259 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
260 if (!capable(CAP_SYS_RESOURCE))
263 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
266 if (flags & EXT4_EOFBLOCKS_FL) {
267 /* we don't support adding EOFBLOCKS flag */
268 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
272 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
273 err = ext4_truncate(inode);
278 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
279 if (IS_ERR(handle)) {
280 err = PTR_ERR(handle);
284 ext4_handle_sync(handle);
285 err = ext4_reserve_inode_write(handle, inode, &iloc);
289 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
290 if (!(mask & EXT4_FL_USER_MODIFIABLE))
292 /* These flags get special treatment later */
293 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
296 ext4_set_inode_flag(inode, i);
298 ext4_clear_inode_flag(inode, i);
301 ext4_set_inode_flags(inode);
302 inode->i_ctime = current_time(inode);
304 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
306 ext4_journal_stop(handle);
310 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
312 * Changes to the journaling mode can cause unsafe changes to
313 * S_DAX if we are using the DAX mount option.
315 if (test_opt(inode->i_sb, DAX)) {
320 err = ext4_change_inode_journal_flag(inode, jflag);
325 if (flags & EXT4_EXTENTS_FL)
326 err = ext4_ext_migrate(inode);
328 err = ext4_ind_migrate(inode);
336 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
338 struct inode *inode = file_inode(filp);
339 struct super_block *sb = inode->i_sb;
340 struct ext4_inode_info *ei = EXT4_I(inode);
344 struct ext4_iloc iloc;
345 struct ext4_inode *raw_inode;
346 struct dquot *transfer_to[MAXQUOTAS] = { };
348 if (!ext4_has_feature_project(sb)) {
349 if (projid != EXT4_DEF_PROJID)
355 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
358 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
360 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
364 /* Is it quota file? Do not allow user to mess with it */
365 if (ext4_is_quota_file(inode))
368 err = ext4_get_inode_loc(inode, &iloc);
372 raw_inode = ext4_raw_inode(&iloc);
373 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
374 err = ext4_expand_extra_isize(inode,
375 EXT4_SB(sb)->s_want_extra_isize,
383 err = dquot_initialize(inode);
387 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
388 EXT4_QUOTA_INIT_BLOCKS(sb) +
389 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
391 return PTR_ERR(handle);
393 err = ext4_reserve_inode_write(handle, inode, &iloc);
397 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
398 if (!IS_ERR(transfer_to[PRJQUOTA])) {
400 /* __dquot_transfer() calls back ext4_get_inode_usage() which
401 * counts xattr inode references.
403 down_read(&EXT4_I(inode)->xattr_sem);
404 err = __dquot_transfer(inode, transfer_to);
405 up_read(&EXT4_I(inode)->xattr_sem);
406 dqput(transfer_to[PRJQUOTA]);
411 EXT4_I(inode)->i_projid = kprojid;
412 inode->i_ctime = current_time(inode);
414 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
418 ext4_journal_stop(handle);
422 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
424 if (projid != EXT4_DEF_PROJID)
430 /* Transfer internal flags to xflags */
431 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
435 if (iflags & EXT4_SYNC_FL)
436 xflags |= FS_XFLAG_SYNC;
437 if (iflags & EXT4_IMMUTABLE_FL)
438 xflags |= FS_XFLAG_IMMUTABLE;
439 if (iflags & EXT4_APPEND_FL)
440 xflags |= FS_XFLAG_APPEND;
441 if (iflags & EXT4_NODUMP_FL)
442 xflags |= FS_XFLAG_NODUMP;
443 if (iflags & EXT4_NOATIME_FL)
444 xflags |= FS_XFLAG_NOATIME;
445 if (iflags & EXT4_PROJINHERIT_FL)
446 xflags |= FS_XFLAG_PROJINHERIT;
450 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
451 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
452 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
454 /* Transfer xflags flags to internal */
455 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
457 unsigned long iflags = 0;
459 if (xflags & FS_XFLAG_SYNC)
460 iflags |= EXT4_SYNC_FL;
461 if (xflags & FS_XFLAG_IMMUTABLE)
462 iflags |= EXT4_IMMUTABLE_FL;
463 if (xflags & FS_XFLAG_APPEND)
464 iflags |= EXT4_APPEND_FL;
465 if (xflags & FS_XFLAG_NODUMP)
466 iflags |= EXT4_NODUMP_FL;
467 if (xflags & FS_XFLAG_NOATIME)
468 iflags |= EXT4_NOATIME_FL;
469 if (xflags & FS_XFLAG_PROJINHERIT)
470 iflags |= EXT4_PROJINHERIT_FL;
475 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
477 struct ext4_sb_info *sbi = EXT4_SB(sb);
480 if (!capable(CAP_SYS_ADMIN))
483 if (get_user(flags, (__u32 __user *)arg))
486 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
489 if (ext4_forced_shutdown(sbi))
492 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
493 trace_ext4_shutdown(sb, flags);
496 case EXT4_GOING_FLAGS_DEFAULT:
497 freeze_bdev(sb->s_bdev);
498 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
499 thaw_bdev(sb->s_bdev, sb);
501 case EXT4_GOING_FLAGS_LOGFLUSH:
502 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
503 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
504 (void) ext4_force_commit(sb);
505 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
508 case EXT4_GOING_FLAGS_NOLOGFLUSH:
509 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
510 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
511 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
516 clear_opt(sb, DISCARD);
520 struct getfsmap_info {
521 struct super_block *gi_sb;
522 struct fsmap_head __user *gi_data;
527 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
529 struct getfsmap_info *info = priv;
532 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
534 info->gi_last_flags = xfm->fmr_flags;
535 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
536 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
537 sizeof(struct fsmap)))
543 static int ext4_ioc_getfsmap(struct super_block *sb,
544 struct fsmap_head __user *arg)
546 struct getfsmap_info info = {0};
547 struct ext4_fsmap_head xhead = {0};
548 struct fsmap_head head;
549 bool aborted = false;
552 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
554 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
555 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
556 sizeof(head.fmh_keys[0].fmr_reserved)) ||
557 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
558 sizeof(head.fmh_keys[1].fmr_reserved)))
561 * ext4 doesn't report file extents at all, so the only valid
562 * file offsets are the magic ones (all zeroes or all ones).
564 if (head.fmh_keys[0].fmr_offset ||
565 (head.fmh_keys[1].fmr_offset != 0 &&
566 head.fmh_keys[1].fmr_offset != -1ULL))
569 xhead.fmh_iflags = head.fmh_iflags;
570 xhead.fmh_count = head.fmh_count;
571 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
572 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
574 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
575 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
579 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
580 if (error == EXT4_QUERY_RANGE_ABORT) {
586 /* If we didn't abort, set the "last" flag in the last fmx */
587 if (!aborted && info.gi_idx) {
588 info.gi_last_flags |= FMR_OF_LAST;
589 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
591 sizeof(info.gi_last_flags)))
595 /* copy back header */
596 head.fmh_entries = xhead.fmh_entries;
597 head.fmh_oflags = xhead.fmh_oflags;
598 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
604 static long ext4_ioctl_group_add(struct file *file,
605 struct ext4_new_group_data *input)
607 struct super_block *sb = file_inode(file)->i_sb;
610 err = ext4_resize_begin(sb);
614 if (ext4_has_feature_bigalloc(sb)) {
615 ext4_msg(sb, KERN_ERR,
616 "Online resizing not supported with bigalloc");
621 err = mnt_want_write_file(file);
625 err = ext4_group_add(sb, input);
626 if (EXT4_SB(sb)->s_journal) {
627 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
628 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
629 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
633 mnt_drop_write_file(file);
634 if (!err && ext4_has_group_desc_csum(sb) &&
635 test_opt(sb, INIT_INODE_TABLE))
636 err = ext4_register_li_request(sb, input->group);
642 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
645 * Project Quota ID state is only allowed to change from within the init
646 * namespace. Enforce that restriction only if we are trying to change
647 * the quota ID state. Everything else is allowed in user namespaces.
649 if (current_user_ns() == &init_user_ns)
652 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
655 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
656 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
659 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
666 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
668 struct inode *inode = file_inode(filp);
669 struct super_block *sb = inode->i_sb;
670 struct ext4_inode_info *ei = EXT4_I(inode);
673 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
676 case FS_IOC_GETFSMAP:
677 return ext4_ioc_getfsmap(sb, (void __user *)arg);
678 case EXT4_IOC_GETFLAGS:
679 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
680 return put_user(flags, (int __user *) arg);
681 case EXT4_IOC_SETFLAGS: {
684 if (!inode_owner_or_capable(inode))
687 if (get_user(flags, (int __user *) arg))
690 if (flags & ~EXT4_FL_USER_VISIBLE)
693 * chattr(1) grabs flags via GETFLAGS, modifies the result and
694 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
695 * more restrictive than just silently masking off visible but
696 * not settable flags as we always did.
698 flags &= EXT4_FL_USER_MODIFIABLE;
699 if (ext4_mask_flags(inode->i_mode, flags) != flags)
702 err = mnt_want_write_file(filp);
707 err = ext4_ioctl_setflags(inode, flags);
709 mnt_drop_write_file(filp);
712 case EXT4_IOC_GETVERSION:
713 case EXT4_IOC_GETVERSION_OLD:
714 return put_user(inode->i_generation, (int __user *) arg);
715 case EXT4_IOC_SETVERSION:
716 case EXT4_IOC_SETVERSION_OLD: {
718 struct ext4_iloc iloc;
722 if (!inode_owner_or_capable(inode))
725 if (ext4_has_metadata_csum(inode->i_sb)) {
726 ext4_warning(sb, "Setting inode version is not "
727 "supported with metadata_csum enabled.");
731 err = mnt_want_write_file(filp);
734 if (get_user(generation, (int __user *) arg)) {
740 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
741 if (IS_ERR(handle)) {
742 err = PTR_ERR(handle);
745 err = ext4_reserve_inode_write(handle, inode, &iloc);
747 inode->i_ctime = current_time(inode);
748 inode->i_generation = generation;
749 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
751 ext4_journal_stop(handle);
756 mnt_drop_write_file(filp);
759 case EXT4_IOC_GROUP_EXTEND: {
760 ext4_fsblk_t n_blocks_count;
763 err = ext4_resize_begin(sb);
767 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
769 goto group_extend_out;
772 if (ext4_has_feature_bigalloc(sb)) {
773 ext4_msg(sb, KERN_ERR,
774 "Online resizing not supported with bigalloc");
776 goto group_extend_out;
779 err = mnt_want_write_file(filp);
781 goto group_extend_out;
783 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
784 if (EXT4_SB(sb)->s_journal) {
785 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
786 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
787 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
791 mnt_drop_write_file(filp);
797 case EXT4_IOC_MOVE_EXT: {
798 struct move_extent me;
802 if (!(filp->f_mode & FMODE_READ) ||
803 !(filp->f_mode & FMODE_WRITE))
806 if (copy_from_user(&me,
807 (struct move_extent __user *)arg, sizeof(me)))
811 donor = fdget(me.donor_fd);
815 if (!(donor.file->f_mode & FMODE_WRITE)) {
820 if (ext4_has_feature_bigalloc(sb)) {
821 ext4_msg(sb, KERN_ERR,
822 "Online defrag not supported with bigalloc");
825 } else if (IS_DAX(inode)) {
826 ext4_msg(sb, KERN_ERR,
827 "Online defrag not supported with DAX");
832 err = mnt_want_write_file(filp);
836 err = ext4_move_extents(filp, donor.file, me.orig_start,
837 me.donor_start, me.len, &me.moved_len);
838 mnt_drop_write_file(filp);
840 if (copy_to_user((struct move_extent __user *)arg,
848 case EXT4_IOC_GROUP_ADD: {
849 struct ext4_new_group_data input;
851 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
855 return ext4_ioctl_group_add(filp, &input);
858 case EXT4_IOC_MIGRATE:
861 if (!inode_owner_or_capable(inode))
864 err = mnt_want_write_file(filp);
868 * inode_mutex prevent write and truncate on the file.
869 * Read still goes through. We take i_data_sem in
870 * ext4_ext_swap_inode_data before we switch the
871 * inode format to prevent read.
874 err = ext4_ext_migrate(inode);
875 inode_unlock((inode));
876 mnt_drop_write_file(filp);
880 case EXT4_IOC_ALLOC_DA_BLKS:
883 if (!inode_owner_or_capable(inode))
886 err = mnt_want_write_file(filp);
889 err = ext4_alloc_da_blocks(inode);
890 mnt_drop_write_file(filp);
894 case EXT4_IOC_SWAP_BOOT:
897 if (!(filp->f_mode & FMODE_WRITE))
899 err = mnt_want_write_file(filp);
902 err = swap_inode_boot_loader(sb, inode);
903 mnt_drop_write_file(filp);
907 case EXT4_IOC_RESIZE_FS: {
908 ext4_fsblk_t n_blocks_count;
909 int err = 0, err2 = 0;
910 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
912 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
917 err = ext4_resize_begin(sb);
921 err = mnt_want_write_file(filp);
925 err = ext4_resize_fs(sb, n_blocks_count);
926 if (EXT4_SB(sb)->s_journal) {
927 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
928 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
929 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
933 mnt_drop_write_file(filp);
934 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
935 ext4_has_group_desc_csum(sb) &&
936 test_opt(sb, INIT_INODE_TABLE))
937 err = ext4_register_li_request(sb, o_group);
946 struct request_queue *q = bdev_get_queue(sb->s_bdev);
947 struct fstrim_range range;
950 if (!capable(CAP_SYS_ADMIN))
953 if (!blk_queue_discard(q))
956 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
960 range.minlen = max((unsigned int)range.minlen,
961 q->limits.discard_granularity);
962 ret = ext4_trim_fs(sb, &range);
966 if (copy_to_user((struct fstrim_range __user *)arg, &range,
972 case EXT4_IOC_PRECACHE_EXTENTS:
973 return ext4_ext_precache(inode);
975 case EXT4_IOC_SET_ENCRYPTION_POLICY:
976 if (!ext4_has_feature_encrypt(sb))
978 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
980 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
981 #ifdef CONFIG_EXT4_FS_ENCRYPTION
983 struct ext4_sb_info *sbi = EXT4_SB(sb);
986 if (!ext4_has_feature_encrypt(sb))
988 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
989 err = mnt_want_write_file(filp);
992 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
993 if (IS_ERR(handle)) {
994 err = PTR_ERR(handle);
995 goto pwsalt_err_exit;
997 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
999 goto pwsalt_err_journal;
1000 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1001 err = ext4_handle_dirty_metadata(handle, NULL,
1004 err2 = ext4_journal_stop(handle);
1008 mnt_drop_write_file(filp);
1012 if (copy_to_user((void __user *) arg,
1013 sbi->s_es->s_encrypt_pw_salt, 16))
1020 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1021 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1023 case EXT4_IOC_FSGETXATTR:
1027 memset(&fa, 0, sizeof(struct fsxattr));
1028 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1030 if (ext4_has_feature_project(inode->i_sb)) {
1031 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1032 EXT4_I(inode)->i_projid);
1035 if (copy_to_user((struct fsxattr __user *)arg,
1040 case EXT4_IOC_FSSETXATTR:
1045 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1049 /* Make sure caller has proper permission */
1050 if (!inode_owner_or_capable(inode))
1053 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1056 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1057 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1060 err = mnt_want_write_file(filp);
1065 err = ext4_ioctl_check_project(inode, &fa);
1068 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1069 (flags & EXT4_FL_XFLAG_VISIBLE);
1070 err = ext4_ioctl_setflags(inode, flags);
1073 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1075 inode_unlock(inode);
1076 mnt_drop_write_file(filp);
1079 case EXT4_IOC_SHUTDOWN:
1080 return ext4_shutdown(sb, arg);
1086 #ifdef CONFIG_COMPAT
1087 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1089 /* These are just misnamed, they actually get/put from/to user an int */
1091 case EXT4_IOC32_GETFLAGS:
1092 cmd = EXT4_IOC_GETFLAGS;
1094 case EXT4_IOC32_SETFLAGS:
1095 cmd = EXT4_IOC_SETFLAGS;
1097 case EXT4_IOC32_GETVERSION:
1098 cmd = EXT4_IOC_GETVERSION;
1100 case EXT4_IOC32_SETVERSION:
1101 cmd = EXT4_IOC_SETVERSION;
1103 case EXT4_IOC32_GROUP_EXTEND:
1104 cmd = EXT4_IOC_GROUP_EXTEND;
1106 case EXT4_IOC32_GETVERSION_OLD:
1107 cmd = EXT4_IOC_GETVERSION_OLD;
1109 case EXT4_IOC32_SETVERSION_OLD:
1110 cmd = EXT4_IOC_SETVERSION_OLD;
1112 case EXT4_IOC32_GETRSVSZ:
1113 cmd = EXT4_IOC_GETRSVSZ;
1115 case EXT4_IOC32_SETRSVSZ:
1116 cmd = EXT4_IOC_SETRSVSZ;
1118 case EXT4_IOC32_GROUP_ADD: {
1119 struct compat_ext4_new_group_input __user *uinput;
1120 struct ext4_new_group_data input;
1123 uinput = compat_ptr(arg);
1124 err = get_user(input.group, &uinput->group);
1125 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1126 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1127 err |= get_user(input.inode_table, &uinput->inode_table);
1128 err |= get_user(input.blocks_count, &uinput->blocks_count);
1129 err |= get_user(input.reserved_blocks,
1130 &uinput->reserved_blocks);
1133 return ext4_ioctl_group_add(file, &input);
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:
1145 return -ENOIOCTLCMD;
1147 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));