1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/ioctl.c
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)
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 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
120 if (IS_ERR(inode_bl))
121 return PTR_ERR(inode_bl);
122 ei_bl = EXT4_I(inode_bl);
124 filemap_flush(inode->i_mapping);
125 filemap_flush(inode_bl->i_mapping);
127 /* Protect orig inodes against a truncate and make sure,
128 * that only 1 swap_inode_boot_loader is running. */
129 lock_two_nondirectories(inode, inode_bl);
131 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
132 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
133 ext4_has_inline_data(inode)) {
135 goto journal_err_out;
138 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
139 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
141 goto journal_err_out;
144 /* Wait for all existing dio workers */
145 inode_dio_wait(inode);
146 inode_dio_wait(inode_bl);
148 truncate_inode_pages(&inode->i_data, 0);
149 truncate_inode_pages(&inode_bl->i_data, 0);
151 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
152 if (IS_ERR(handle)) {
154 goto journal_err_out;
157 /* Protect extent tree against block allocations via delalloc */
158 ext4_double_down_write_data_sem(inode, inode_bl);
160 if (inode_bl->i_nlink == 0) {
161 /* this inode has never been used as a BOOT_LOADER */
162 set_nlink(inode_bl, 1);
163 i_uid_write(inode_bl, 0);
164 i_gid_write(inode_bl, 0);
165 inode_bl->i_flags = 0;
167 inode_set_iversion(inode_bl, 1);
168 i_size_write(inode_bl, 0);
169 inode_bl->i_mode = S_IFREG;
170 if (ext4_has_feature_extents(sb)) {
171 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
172 ext4_ext_tree_init(handle, inode_bl);
174 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
177 swap_inode_data(inode, inode_bl);
179 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
181 inode->i_generation = prandom_u32();
182 inode_bl->i_generation = prandom_u32();
183 reset_inode_seed(inode);
184 reset_inode_seed(inode_bl);
186 ext4_discard_preallocations(inode);
188 err = ext4_mark_inode_dirty(handle, inode);
190 ext4_warning(inode->i_sb,
191 "couldn't mark inode #%lu dirty (err %d)",
193 /* Revert all changes: */
194 swap_inode_data(inode, inode_bl);
195 ext4_mark_inode_dirty(handle, inode);
197 err = ext4_mark_inode_dirty(handle, inode_bl);
199 ext4_warning(inode_bl->i_sb,
200 "couldn't mark inode #%lu dirty (err %d)",
201 inode_bl->i_ino, err);
202 /* Revert all changes: */
203 swap_inode_data(inode, inode_bl);
204 ext4_mark_inode_dirty(handle, inode);
205 ext4_mark_inode_dirty(handle, inode_bl);
208 ext4_journal_stop(handle);
209 ext4_double_up_write_data_sem(inode, inode_bl);
212 unlock_two_nondirectories(inode, inode_bl);
217 #ifdef CONFIG_EXT4_FS_ENCRYPTION
218 static int uuid_is_zero(__u8 u[16])
222 for (i = 0; i < 16; i++)
229 static int ext4_ioctl_setflags(struct inode *inode,
232 struct ext4_inode_info *ei = EXT4_I(inode);
233 handle_t *handle = NULL;
234 int err = -EPERM, migrate = 0;
235 struct ext4_iloc iloc;
236 unsigned int oldflags, mask, i;
239 /* Is it quota file? Do not allow user to mess with it */
240 if (ext4_is_quota_file(inode))
243 oldflags = ei->i_flags;
245 /* The JOURNAL_DATA flag is modifiable only by root */
246 jflag = flags & EXT4_JOURNAL_DATA_FL;
249 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
250 * the relevant capability.
252 * This test looks nicer. Thanks to Pauline Middelink
254 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
255 if (!capable(CAP_LINUX_IMMUTABLE))
260 * The JOURNAL_DATA flag can only be changed by
261 * the relevant capability.
263 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
264 if (!capable(CAP_SYS_RESOURCE))
267 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
270 if (flags & EXT4_EOFBLOCKS_FL) {
271 /* we don't support adding EOFBLOCKS flag */
272 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
276 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
277 err = ext4_truncate(inode);
282 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
283 if (IS_ERR(handle)) {
284 err = PTR_ERR(handle);
288 ext4_handle_sync(handle);
289 err = ext4_reserve_inode_write(handle, inode, &iloc);
293 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
294 if (!(mask & EXT4_FL_USER_MODIFIABLE))
296 /* These flags get special treatment later */
297 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
300 ext4_set_inode_flag(inode, i);
302 ext4_clear_inode_flag(inode, i);
305 ext4_set_inode_flags(inode);
306 inode->i_ctime = current_time(inode);
308 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
310 ext4_journal_stop(handle);
314 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
316 * Changes to the journaling mode can cause unsafe changes to
317 * S_DAX if we are using the DAX mount option.
319 if (test_opt(inode->i_sb, DAX)) {
324 err = ext4_change_inode_journal_flag(inode, jflag);
329 if (flags & EXT4_EXTENTS_FL)
330 err = ext4_ext_migrate(inode);
332 err = ext4_ind_migrate(inode);
340 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
342 struct inode *inode = file_inode(filp);
343 struct super_block *sb = inode->i_sb;
344 struct ext4_inode_info *ei = EXT4_I(inode);
348 struct ext4_iloc iloc;
349 struct ext4_inode *raw_inode;
350 struct dquot *transfer_to[MAXQUOTAS] = { };
352 if (!ext4_has_feature_project(sb)) {
353 if (projid != EXT4_DEF_PROJID)
359 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
362 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
364 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
368 /* Is it quota file? Do not allow user to mess with it */
369 if (ext4_is_quota_file(inode))
372 err = ext4_get_inode_loc(inode, &iloc);
376 raw_inode = ext4_raw_inode(&iloc);
377 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
378 err = ext4_expand_extra_isize(inode,
379 EXT4_SB(sb)->s_want_extra_isize,
387 err = dquot_initialize(inode);
391 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
392 EXT4_QUOTA_INIT_BLOCKS(sb) +
393 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
395 return PTR_ERR(handle);
397 err = ext4_reserve_inode_write(handle, inode, &iloc);
401 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
402 if (!IS_ERR(transfer_to[PRJQUOTA])) {
404 /* __dquot_transfer() calls back ext4_get_inode_usage() which
405 * counts xattr inode references.
407 down_read(&EXT4_I(inode)->xattr_sem);
408 err = __dquot_transfer(inode, transfer_to);
409 up_read(&EXT4_I(inode)->xattr_sem);
410 dqput(transfer_to[PRJQUOTA]);
415 EXT4_I(inode)->i_projid = kprojid;
416 inode->i_ctime = current_time(inode);
418 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
422 ext4_journal_stop(handle);
426 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
428 if (projid != EXT4_DEF_PROJID)
434 /* Transfer internal flags to xflags */
435 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
439 if (iflags & EXT4_SYNC_FL)
440 xflags |= FS_XFLAG_SYNC;
441 if (iflags & EXT4_IMMUTABLE_FL)
442 xflags |= FS_XFLAG_IMMUTABLE;
443 if (iflags & EXT4_APPEND_FL)
444 xflags |= FS_XFLAG_APPEND;
445 if (iflags & EXT4_NODUMP_FL)
446 xflags |= FS_XFLAG_NODUMP;
447 if (iflags & EXT4_NOATIME_FL)
448 xflags |= FS_XFLAG_NOATIME;
449 if (iflags & EXT4_PROJINHERIT_FL)
450 xflags |= FS_XFLAG_PROJINHERIT;
454 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
455 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
456 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
458 /* Transfer xflags flags to internal */
459 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
461 unsigned long iflags = 0;
463 if (xflags & FS_XFLAG_SYNC)
464 iflags |= EXT4_SYNC_FL;
465 if (xflags & FS_XFLAG_IMMUTABLE)
466 iflags |= EXT4_IMMUTABLE_FL;
467 if (xflags & FS_XFLAG_APPEND)
468 iflags |= EXT4_APPEND_FL;
469 if (xflags & FS_XFLAG_NODUMP)
470 iflags |= EXT4_NODUMP_FL;
471 if (xflags & FS_XFLAG_NOATIME)
472 iflags |= EXT4_NOATIME_FL;
473 if (xflags & FS_XFLAG_PROJINHERIT)
474 iflags |= EXT4_PROJINHERIT_FL;
479 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
481 struct ext4_sb_info *sbi = EXT4_SB(sb);
484 if (!capable(CAP_SYS_ADMIN))
487 if (get_user(flags, (__u32 __user *)arg))
490 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
493 if (ext4_forced_shutdown(sbi))
496 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
497 trace_ext4_shutdown(sb, flags);
500 case EXT4_GOING_FLAGS_DEFAULT:
501 freeze_bdev(sb->s_bdev);
502 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
503 thaw_bdev(sb->s_bdev, sb);
505 case EXT4_GOING_FLAGS_LOGFLUSH:
506 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
507 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
508 (void) ext4_force_commit(sb);
509 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
512 case EXT4_GOING_FLAGS_NOLOGFLUSH:
513 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
514 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
515 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
520 clear_opt(sb, DISCARD);
524 struct getfsmap_info {
525 struct super_block *gi_sb;
526 struct fsmap_head __user *gi_data;
531 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
533 struct getfsmap_info *info = priv;
536 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
538 info->gi_last_flags = xfm->fmr_flags;
539 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
540 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
541 sizeof(struct fsmap)))
547 static int ext4_ioc_getfsmap(struct super_block *sb,
548 struct fsmap_head __user *arg)
550 struct getfsmap_info info = {0};
551 struct ext4_fsmap_head xhead = {0};
552 struct fsmap_head head;
553 bool aborted = false;
556 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
558 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
559 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
560 sizeof(head.fmh_keys[0].fmr_reserved)) ||
561 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
562 sizeof(head.fmh_keys[1].fmr_reserved)))
565 * ext4 doesn't report file extents at all, so the only valid
566 * file offsets are the magic ones (all zeroes or all ones).
568 if (head.fmh_keys[0].fmr_offset ||
569 (head.fmh_keys[1].fmr_offset != 0 &&
570 head.fmh_keys[1].fmr_offset != -1ULL))
573 xhead.fmh_iflags = head.fmh_iflags;
574 xhead.fmh_count = head.fmh_count;
575 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
576 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
578 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
579 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
583 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
584 if (error == EXT4_QUERY_RANGE_ABORT) {
590 /* If we didn't abort, set the "last" flag in the last fmx */
591 if (!aborted && info.gi_idx) {
592 info.gi_last_flags |= FMR_OF_LAST;
593 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
595 sizeof(info.gi_last_flags)))
599 /* copy back header */
600 head.fmh_entries = xhead.fmh_entries;
601 head.fmh_oflags = xhead.fmh_oflags;
602 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
608 static long ext4_ioctl_group_add(struct file *file,
609 struct ext4_new_group_data *input)
611 struct super_block *sb = file_inode(file)->i_sb;
614 err = ext4_resize_begin(sb);
618 if (ext4_has_feature_bigalloc(sb)) {
619 ext4_msg(sb, KERN_ERR,
620 "Online resizing not supported with bigalloc");
625 err = mnt_want_write_file(file);
629 err = ext4_group_add(sb, input);
630 if (EXT4_SB(sb)->s_journal) {
631 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
632 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
633 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
637 mnt_drop_write_file(file);
638 if (!err && ext4_has_group_desc_csum(sb) &&
639 test_opt(sb, INIT_INODE_TABLE))
640 err = ext4_register_li_request(sb, input->group);
646 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
649 * Project Quota ID state is only allowed to change from within the init
650 * namespace. Enforce that restriction only if we are trying to change
651 * the quota ID state. Everything else is allowed in user namespaces.
653 if (current_user_ns() == &init_user_ns)
656 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
659 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
660 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
663 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
670 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
672 struct inode *inode = file_inode(filp);
673 struct super_block *sb = inode->i_sb;
674 struct ext4_inode_info *ei = EXT4_I(inode);
677 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
680 case FS_IOC_GETFSMAP:
681 return ext4_ioc_getfsmap(sb, (void __user *)arg);
682 case EXT4_IOC_GETFLAGS:
683 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
684 return put_user(flags, (int __user *) arg);
685 case EXT4_IOC_SETFLAGS: {
688 if (!inode_owner_or_capable(inode))
691 if (get_user(flags, (int __user *) arg))
694 if (flags & ~EXT4_FL_USER_VISIBLE)
697 * chattr(1) grabs flags via GETFLAGS, modifies the result and
698 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
699 * more restrictive than just silently masking off visible but
700 * not settable flags as we always did.
702 flags &= EXT4_FL_USER_MODIFIABLE;
703 if (ext4_mask_flags(inode->i_mode, flags) != flags)
706 err = mnt_want_write_file(filp);
711 err = ext4_ioctl_setflags(inode, flags);
713 mnt_drop_write_file(filp);
716 case EXT4_IOC_GETVERSION:
717 case EXT4_IOC_GETVERSION_OLD:
718 return put_user(inode->i_generation, (int __user *) arg);
719 case EXT4_IOC_SETVERSION:
720 case EXT4_IOC_SETVERSION_OLD: {
722 struct ext4_iloc iloc;
726 if (!inode_owner_or_capable(inode))
729 if (ext4_has_metadata_csum(inode->i_sb)) {
730 ext4_warning(sb, "Setting inode version is not "
731 "supported with metadata_csum enabled.");
735 err = mnt_want_write_file(filp);
738 if (get_user(generation, (int __user *) arg)) {
744 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
745 if (IS_ERR(handle)) {
746 err = PTR_ERR(handle);
749 err = ext4_reserve_inode_write(handle, inode, &iloc);
751 inode->i_ctime = current_time(inode);
752 inode->i_generation = generation;
753 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
755 ext4_journal_stop(handle);
760 mnt_drop_write_file(filp);
763 case EXT4_IOC_GROUP_EXTEND: {
764 ext4_fsblk_t n_blocks_count;
767 err = ext4_resize_begin(sb);
771 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
773 goto group_extend_out;
776 if (ext4_has_feature_bigalloc(sb)) {
777 ext4_msg(sb, KERN_ERR,
778 "Online resizing not supported with bigalloc");
780 goto group_extend_out;
783 err = mnt_want_write_file(filp);
785 goto group_extend_out;
787 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
788 if (EXT4_SB(sb)->s_journal) {
789 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
790 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
791 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
795 mnt_drop_write_file(filp);
801 case EXT4_IOC_MOVE_EXT: {
802 struct move_extent me;
806 if (!(filp->f_mode & FMODE_READ) ||
807 !(filp->f_mode & FMODE_WRITE))
810 if (copy_from_user(&me,
811 (struct move_extent __user *)arg, sizeof(me)))
815 donor = fdget(me.donor_fd);
819 if (!(donor.file->f_mode & FMODE_WRITE)) {
824 if (ext4_has_feature_bigalloc(sb)) {
825 ext4_msg(sb, KERN_ERR,
826 "Online defrag not supported with bigalloc");
829 } else if (IS_DAX(inode)) {
830 ext4_msg(sb, KERN_ERR,
831 "Online defrag not supported with DAX");
836 err = mnt_want_write_file(filp);
840 err = ext4_move_extents(filp, donor.file, me.orig_start,
841 me.donor_start, me.len, &me.moved_len);
842 mnt_drop_write_file(filp);
844 if (copy_to_user((struct move_extent __user *)arg,
852 case EXT4_IOC_GROUP_ADD: {
853 struct ext4_new_group_data input;
855 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
859 return ext4_ioctl_group_add(filp, &input);
862 case EXT4_IOC_MIGRATE:
865 if (!inode_owner_or_capable(inode))
868 err = mnt_want_write_file(filp);
872 * inode_mutex prevent write and truncate on the file.
873 * Read still goes through. We take i_data_sem in
874 * ext4_ext_swap_inode_data before we switch the
875 * inode format to prevent read.
878 err = ext4_ext_migrate(inode);
879 inode_unlock((inode));
880 mnt_drop_write_file(filp);
884 case EXT4_IOC_ALLOC_DA_BLKS:
887 if (!inode_owner_or_capable(inode))
890 err = mnt_want_write_file(filp);
893 err = ext4_alloc_da_blocks(inode);
894 mnt_drop_write_file(filp);
898 case EXT4_IOC_SWAP_BOOT:
901 if (!(filp->f_mode & FMODE_WRITE))
903 err = mnt_want_write_file(filp);
906 err = swap_inode_boot_loader(sb, inode);
907 mnt_drop_write_file(filp);
911 case EXT4_IOC_RESIZE_FS: {
912 ext4_fsblk_t n_blocks_count;
913 int err = 0, err2 = 0;
914 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
916 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
921 err = ext4_resize_begin(sb);
925 err = mnt_want_write_file(filp);
929 err = ext4_resize_fs(sb, n_blocks_count);
930 if (EXT4_SB(sb)->s_journal) {
931 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
932 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
933 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
937 mnt_drop_write_file(filp);
938 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
939 ext4_has_group_desc_csum(sb) &&
940 test_opt(sb, INIT_INODE_TABLE))
941 err = ext4_register_li_request(sb, o_group);
950 struct request_queue *q = bdev_get_queue(sb->s_bdev);
951 struct fstrim_range range;
954 if (!capable(CAP_SYS_ADMIN))
957 if (!blk_queue_discard(q))
960 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
964 range.minlen = max((unsigned int)range.minlen,
965 q->limits.discard_granularity);
966 ret = ext4_trim_fs(sb, &range);
970 if (copy_to_user((struct fstrim_range __user *)arg, &range,
976 case EXT4_IOC_PRECACHE_EXTENTS:
977 return ext4_ext_precache(inode);
979 case EXT4_IOC_SET_ENCRYPTION_POLICY:
980 if (!ext4_has_feature_encrypt(sb))
982 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
984 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
985 #ifdef CONFIG_EXT4_FS_ENCRYPTION
987 struct ext4_sb_info *sbi = EXT4_SB(sb);
990 if (!ext4_has_feature_encrypt(sb))
992 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
993 err = mnt_want_write_file(filp);
996 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
997 if (IS_ERR(handle)) {
998 err = PTR_ERR(handle);
999 goto pwsalt_err_exit;
1001 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1003 goto pwsalt_err_journal;
1004 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1005 err = ext4_handle_dirty_metadata(handle, NULL,
1008 err2 = ext4_journal_stop(handle);
1012 mnt_drop_write_file(filp);
1016 if (copy_to_user((void __user *) arg,
1017 sbi->s_es->s_encrypt_pw_salt, 16))
1024 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1025 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1027 case EXT4_IOC_FSGETXATTR:
1031 memset(&fa, 0, sizeof(struct fsxattr));
1032 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1034 if (ext4_has_feature_project(inode->i_sb)) {
1035 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1036 EXT4_I(inode)->i_projid);
1039 if (copy_to_user((struct fsxattr __user *)arg,
1044 case EXT4_IOC_FSSETXATTR:
1049 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1053 /* Make sure caller has proper permission */
1054 if (!inode_owner_or_capable(inode))
1057 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1060 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1061 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1064 err = mnt_want_write_file(filp);
1069 err = ext4_ioctl_check_project(inode, &fa);
1072 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1073 (flags & EXT4_FL_XFLAG_VISIBLE);
1074 err = ext4_ioctl_setflags(inode, flags);
1077 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1079 inode_unlock(inode);
1080 mnt_drop_write_file(filp);
1083 case EXT4_IOC_SHUTDOWN:
1084 return ext4_shutdown(sb, arg);
1090 #ifdef CONFIG_COMPAT
1091 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1093 /* These are just misnamed, they actually get/put from/to user an int */
1095 case EXT4_IOC32_GETFLAGS:
1096 cmd = EXT4_IOC_GETFLAGS;
1098 case EXT4_IOC32_SETFLAGS:
1099 cmd = EXT4_IOC_SETFLAGS;
1101 case EXT4_IOC32_GETVERSION:
1102 cmd = EXT4_IOC_GETVERSION;
1104 case EXT4_IOC32_SETVERSION:
1105 cmd = EXT4_IOC_SETVERSION;
1107 case EXT4_IOC32_GROUP_EXTEND:
1108 cmd = EXT4_IOC_GROUP_EXTEND;
1110 case EXT4_IOC32_GETVERSION_OLD:
1111 cmd = EXT4_IOC_GETVERSION_OLD;
1113 case EXT4_IOC32_SETVERSION_OLD:
1114 cmd = EXT4_IOC_SETVERSION_OLD;
1116 case EXT4_IOC32_GETRSVSZ:
1117 cmd = EXT4_IOC_GETRSVSZ;
1119 case EXT4_IOC32_SETRSVSZ:
1120 cmd = EXT4_IOC_SETRSVSZ;
1122 case EXT4_IOC32_GROUP_ADD: {
1123 struct compat_ext4_new_group_input __user *uinput;
1124 struct ext4_new_group_data input;
1127 uinput = compat_ptr(arg);
1128 err = get_user(input.group, &uinput->group);
1129 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1130 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1131 err |= get_user(input.inode_table, &uinput->inode_table);
1132 err |= get_user(input.blocks_count, &uinput->blocks_count);
1133 err |= get_user(input.reserved_blocks,
1134 &uinput->reserved_blocks);
1137 return ext4_ioctl_group_add(file, &input);
1139 case EXT4_IOC_MOVE_EXT:
1140 case EXT4_IOC_RESIZE_FS:
1141 case EXT4_IOC_PRECACHE_EXTENTS:
1142 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1143 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1144 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1145 case EXT4_IOC_SHUTDOWN:
1146 case FS_IOC_GETFSMAP:
1149 return -ENOIOCTLCMD;
1151 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));