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;
71 swap(inode1->i_version, inode2->i_version);
72 swap(inode1->i_atime, inode2->i_atime);
73 swap(inode1->i_mtime, inode2->i_mtime);
75 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
76 tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
77 ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
78 (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
79 ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
80 swap(ei1->i_disksize, ei2->i_disksize);
81 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
82 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
84 isize = i_size_read(inode1);
85 i_size_write(inode1, i_size_read(inode2));
86 i_size_write(inode2, isize);
89 static void reset_inode_seed(struct inode *inode)
91 struct ext4_inode_info *ei = EXT4_I(inode);
92 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
93 __le32 inum = cpu_to_le32(inode->i_ino);
94 __le32 gen = cpu_to_le32(inode->i_generation);
97 if (!ext4_has_metadata_csum(inode->i_sb))
100 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
101 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
105 * Swap the information from the given @inode and the inode
106 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
107 * important fields of the inodes.
109 * @sb: the super block of the filesystem
110 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
113 static long swap_inode_boot_loader(struct super_block *sb,
118 struct inode *inode_bl;
119 struct ext4_inode_info *ei_bl;
120 qsize_t size, size_bl, diff;
122 unsigned short bytes;
124 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
125 if (IS_ERR(inode_bl))
126 return PTR_ERR(inode_bl);
127 ei_bl = EXT4_I(inode_bl);
129 /* Protect orig inodes against a truncate and make sure,
130 * that only 1 swap_inode_boot_loader is running. */
131 lock_two_nondirectories(inode, inode_bl);
133 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
134 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
135 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
136 ext4_has_inline_data(inode)) {
138 goto journal_err_out;
141 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
142 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
144 goto journal_err_out;
147 down_write(&EXT4_I(inode)->i_mmap_sem);
148 err = filemap_write_and_wait(inode->i_mapping);
152 err = filemap_write_and_wait(inode_bl->i_mapping);
156 /* Wait for all existing dio workers */
157 inode_dio_wait(inode);
158 inode_dio_wait(inode_bl);
160 truncate_inode_pages(&inode->i_data, 0);
161 truncate_inode_pages(&inode_bl->i_data, 0);
163 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
164 if (IS_ERR(handle)) {
169 /* Protect extent tree against block allocations via delalloc */
170 ext4_double_down_write_data_sem(inode, inode_bl);
172 if (inode_bl->i_nlink == 0) {
173 /* this inode has never been used as a BOOT_LOADER */
174 set_nlink(inode_bl, 1);
175 i_uid_write(inode_bl, 0);
176 i_gid_write(inode_bl, 0);
177 inode_bl->i_flags = 0;
179 inode_set_iversion(inode_bl, 1);
180 i_size_write(inode_bl, 0);
181 inode_bl->i_mode = S_IFREG;
182 if (ext4_has_feature_extents(sb)) {
183 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
184 ext4_ext_tree_init(handle, inode_bl);
186 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
189 err = dquot_initialize(inode);
193 size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
194 size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
195 diff = size - size_bl;
196 swap_inode_data(inode, inode_bl);
198 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
200 inode->i_generation = prandom_u32();
201 inode_bl->i_generation = prandom_u32();
202 reset_inode_seed(inode);
203 reset_inode_seed(inode_bl);
205 ext4_discard_preallocations(inode);
207 err = ext4_mark_inode_dirty(handle, inode);
209 /* No need to update quota information. */
210 ext4_warning(inode->i_sb,
211 "couldn't mark inode #%lu dirty (err %d)",
213 /* Revert all changes: */
214 swap_inode_data(inode, inode_bl);
215 ext4_mark_inode_dirty(handle, inode);
219 blocks = inode_bl->i_blocks;
220 bytes = inode_bl->i_bytes;
221 inode_bl->i_blocks = inode->i_blocks;
222 inode_bl->i_bytes = inode->i_bytes;
223 err = ext4_mark_inode_dirty(handle, inode_bl);
225 /* No need to update quota information. */
226 ext4_warning(inode_bl->i_sb,
227 "couldn't mark inode #%lu dirty (err %d)",
228 inode_bl->i_ino, err);
232 /* Bootloader inode should not be counted into quota information. */
234 dquot_free_space(inode, diff);
236 err = dquot_alloc_space(inode, -1 * diff);
240 /* Revert all changes: */
241 inode_bl->i_blocks = blocks;
242 inode_bl->i_bytes = bytes;
243 swap_inode_data(inode, inode_bl);
244 ext4_mark_inode_dirty(handle, inode);
245 ext4_mark_inode_dirty(handle, inode_bl);
249 ext4_journal_stop(handle);
250 ext4_double_up_write_data_sem(inode, inode_bl);
253 up_write(&EXT4_I(inode)->i_mmap_sem);
255 unlock_two_nondirectories(inode, inode_bl);
260 #ifdef CONFIG_FS_ENCRYPTION
261 static int uuid_is_zero(__u8 u[16])
265 for (i = 0; i < 16; i++)
272 static int ext4_ioctl_setflags(struct inode *inode,
275 struct ext4_inode_info *ei = EXT4_I(inode);
276 handle_t *handle = NULL;
277 int err = -EPERM, migrate = 0;
278 struct ext4_iloc iloc;
279 unsigned int oldflags, mask, i;
282 /* Is it quota file? Do not allow user to mess with it */
283 if (ext4_is_quota_file(inode))
286 oldflags = ei->i_flags;
288 /* The JOURNAL_DATA flag is modifiable only by root */
289 jflag = flags & EXT4_JOURNAL_DATA_FL;
292 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
293 * the relevant capability.
295 * This test looks nicer. Thanks to Pauline Middelink
297 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
298 if (!capable(CAP_LINUX_IMMUTABLE))
303 * The JOURNAL_DATA flag can only be changed by
304 * the relevant capability.
306 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
307 if (!capable(CAP_SYS_RESOURCE))
310 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
313 if (flags & EXT4_EOFBLOCKS_FL) {
314 /* we don't support adding EOFBLOCKS flag */
315 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
319 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
320 err = ext4_truncate(inode);
325 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
326 if (IS_ERR(handle)) {
327 err = PTR_ERR(handle);
331 ext4_handle_sync(handle);
332 err = ext4_reserve_inode_write(handle, inode, &iloc);
336 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
337 if (!(mask & EXT4_FL_USER_MODIFIABLE))
339 /* These flags get special treatment later */
340 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
343 ext4_set_inode_flag(inode, i);
345 ext4_clear_inode_flag(inode, i);
348 ext4_set_inode_flags(inode);
349 inode->i_ctime = current_time(inode);
351 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
353 ext4_journal_stop(handle);
357 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
359 * Changes to the journaling mode can cause unsafe changes to
360 * S_DAX if we are using the DAX mount option.
362 if (test_opt(inode->i_sb, DAX)) {
367 err = ext4_change_inode_journal_flag(inode, jflag);
372 if (flags & EXT4_EXTENTS_FL)
373 err = ext4_ext_migrate(inode);
375 err = ext4_ind_migrate(inode);
383 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
385 struct inode *inode = file_inode(filp);
386 struct super_block *sb = inode->i_sb;
387 struct ext4_inode_info *ei = EXT4_I(inode);
391 struct ext4_iloc iloc;
392 struct ext4_inode *raw_inode;
393 struct dquot *transfer_to[MAXQUOTAS] = { };
395 if (!ext4_has_feature_project(sb)) {
396 if (projid != EXT4_DEF_PROJID)
402 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
405 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
407 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
411 /* Is it quota file? Do not allow user to mess with it */
412 if (ext4_is_quota_file(inode))
415 err = ext4_get_inode_loc(inode, &iloc);
419 raw_inode = ext4_raw_inode(&iloc);
420 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
421 err = ext4_expand_extra_isize(inode,
422 EXT4_SB(sb)->s_want_extra_isize,
430 err = dquot_initialize(inode);
434 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
435 EXT4_QUOTA_INIT_BLOCKS(sb) +
436 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
438 return PTR_ERR(handle);
440 err = ext4_reserve_inode_write(handle, inode, &iloc);
444 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
445 if (!IS_ERR(transfer_to[PRJQUOTA])) {
447 /* __dquot_transfer() calls back ext4_get_inode_usage() which
448 * counts xattr inode references.
450 down_read(&EXT4_I(inode)->xattr_sem);
451 err = __dquot_transfer(inode, transfer_to);
452 up_read(&EXT4_I(inode)->xattr_sem);
453 dqput(transfer_to[PRJQUOTA]);
458 EXT4_I(inode)->i_projid = kprojid;
459 inode->i_ctime = current_time(inode);
461 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
465 ext4_journal_stop(handle);
469 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
471 if (projid != EXT4_DEF_PROJID)
477 /* Transfer internal flags to xflags */
478 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
482 if (iflags & EXT4_SYNC_FL)
483 xflags |= FS_XFLAG_SYNC;
484 if (iflags & EXT4_IMMUTABLE_FL)
485 xflags |= FS_XFLAG_IMMUTABLE;
486 if (iflags & EXT4_APPEND_FL)
487 xflags |= FS_XFLAG_APPEND;
488 if (iflags & EXT4_NODUMP_FL)
489 xflags |= FS_XFLAG_NODUMP;
490 if (iflags & EXT4_NOATIME_FL)
491 xflags |= FS_XFLAG_NOATIME;
492 if (iflags & EXT4_PROJINHERIT_FL)
493 xflags |= FS_XFLAG_PROJINHERIT;
497 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
498 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
499 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
501 /* Transfer xflags flags to internal */
502 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
504 unsigned long iflags = 0;
506 if (xflags & FS_XFLAG_SYNC)
507 iflags |= EXT4_SYNC_FL;
508 if (xflags & FS_XFLAG_IMMUTABLE)
509 iflags |= EXT4_IMMUTABLE_FL;
510 if (xflags & FS_XFLAG_APPEND)
511 iflags |= EXT4_APPEND_FL;
512 if (xflags & FS_XFLAG_NODUMP)
513 iflags |= EXT4_NODUMP_FL;
514 if (xflags & FS_XFLAG_NOATIME)
515 iflags |= EXT4_NOATIME_FL;
516 if (xflags & FS_XFLAG_PROJINHERIT)
517 iflags |= EXT4_PROJINHERIT_FL;
522 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
524 struct ext4_sb_info *sbi = EXT4_SB(sb);
527 if (!capable(CAP_SYS_ADMIN))
530 if (get_user(flags, (__u32 __user *)arg))
533 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
536 if (ext4_forced_shutdown(sbi))
539 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
540 trace_ext4_shutdown(sb, flags);
543 case EXT4_GOING_FLAGS_DEFAULT:
544 freeze_bdev(sb->s_bdev);
545 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
546 thaw_bdev(sb->s_bdev, sb);
548 case EXT4_GOING_FLAGS_LOGFLUSH:
549 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
550 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
551 (void) ext4_force_commit(sb);
552 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
555 case EXT4_GOING_FLAGS_NOLOGFLUSH:
556 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
557 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
558 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
563 clear_opt(sb, DISCARD);
567 struct getfsmap_info {
568 struct super_block *gi_sb;
569 struct fsmap_head __user *gi_data;
574 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
576 struct getfsmap_info *info = priv;
579 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
581 info->gi_last_flags = xfm->fmr_flags;
582 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
583 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
584 sizeof(struct fsmap)))
590 static int ext4_ioc_getfsmap(struct super_block *sb,
591 struct fsmap_head __user *arg)
593 struct getfsmap_info info = {0};
594 struct ext4_fsmap_head xhead = {0};
595 struct fsmap_head head;
596 bool aborted = false;
599 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
601 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
602 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
603 sizeof(head.fmh_keys[0].fmr_reserved)) ||
604 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
605 sizeof(head.fmh_keys[1].fmr_reserved)))
608 * ext4 doesn't report file extents at all, so the only valid
609 * file offsets are the magic ones (all zeroes or all ones).
611 if (head.fmh_keys[0].fmr_offset ||
612 (head.fmh_keys[1].fmr_offset != 0 &&
613 head.fmh_keys[1].fmr_offset != -1ULL))
616 xhead.fmh_iflags = head.fmh_iflags;
617 xhead.fmh_count = head.fmh_count;
618 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
619 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
621 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
622 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
626 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
627 if (error == EXT4_QUERY_RANGE_ABORT) {
633 /* If we didn't abort, set the "last" flag in the last fmx */
634 if (!aborted && info.gi_idx) {
635 info.gi_last_flags |= FMR_OF_LAST;
636 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
638 sizeof(info.gi_last_flags)))
642 /* copy back header */
643 head.fmh_entries = xhead.fmh_entries;
644 head.fmh_oflags = xhead.fmh_oflags;
645 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
651 static long ext4_ioctl_group_add(struct file *file,
652 struct ext4_new_group_data *input)
654 struct super_block *sb = file_inode(file)->i_sb;
657 err = ext4_resize_begin(sb);
661 if (ext4_has_feature_bigalloc(sb)) {
662 ext4_msg(sb, KERN_ERR,
663 "Online resizing not supported with bigalloc");
668 err = mnt_want_write_file(file);
672 err = ext4_group_add(sb, input);
673 if (EXT4_SB(sb)->s_journal) {
674 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
675 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
676 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
680 mnt_drop_write_file(file);
681 if (!err && ext4_has_group_desc_csum(sb) &&
682 test_opt(sb, INIT_INODE_TABLE))
683 err = ext4_register_li_request(sb, input->group);
689 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
692 * Project Quota ID state is only allowed to change from within the init
693 * namespace. Enforce that restriction only if we are trying to change
694 * the quota ID state. Everything else is allowed in user namespaces.
696 if (current_user_ns() == &init_user_ns)
699 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
702 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
703 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
706 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
713 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
715 struct inode *inode = file_inode(filp);
716 struct super_block *sb = inode->i_sb;
717 struct ext4_inode_info *ei = EXT4_I(inode);
720 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
723 case FS_IOC_GETFSMAP:
724 return ext4_ioc_getfsmap(sb, (void __user *)arg);
725 case EXT4_IOC_GETFLAGS:
726 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
727 return put_user(flags, (int __user *) arg);
728 case EXT4_IOC_SETFLAGS: {
731 if (!inode_owner_or_capable(inode))
734 if (get_user(flags, (int __user *) arg))
737 if (flags & ~EXT4_FL_USER_VISIBLE)
740 * chattr(1) grabs flags via GETFLAGS, modifies the result and
741 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
742 * more restrictive than just silently masking off visible but
743 * not settable flags as we always did.
745 flags &= EXT4_FL_USER_MODIFIABLE;
746 if (ext4_mask_flags(inode->i_mode, flags) != flags)
749 err = mnt_want_write_file(filp);
754 err = ext4_ioctl_setflags(inode, flags);
756 mnt_drop_write_file(filp);
759 case EXT4_IOC_GETVERSION:
760 case EXT4_IOC_GETVERSION_OLD:
761 return put_user(inode->i_generation, (int __user *) arg);
762 case EXT4_IOC_SETVERSION:
763 case EXT4_IOC_SETVERSION_OLD: {
765 struct ext4_iloc iloc;
769 if (!inode_owner_or_capable(inode))
772 if (ext4_has_metadata_csum(inode->i_sb)) {
773 ext4_warning(sb, "Setting inode version is not "
774 "supported with metadata_csum enabled.");
778 err = mnt_want_write_file(filp);
781 if (get_user(generation, (int __user *) arg)) {
787 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
788 if (IS_ERR(handle)) {
789 err = PTR_ERR(handle);
792 err = ext4_reserve_inode_write(handle, inode, &iloc);
794 inode->i_ctime = current_time(inode);
795 inode->i_generation = generation;
796 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
798 ext4_journal_stop(handle);
803 mnt_drop_write_file(filp);
806 case EXT4_IOC_GROUP_EXTEND: {
807 ext4_fsblk_t n_blocks_count;
810 err = ext4_resize_begin(sb);
814 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
816 goto group_extend_out;
819 if (ext4_has_feature_bigalloc(sb)) {
820 ext4_msg(sb, KERN_ERR,
821 "Online resizing not supported with bigalloc");
823 goto group_extend_out;
826 err = mnt_want_write_file(filp);
828 goto group_extend_out;
830 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
831 if (EXT4_SB(sb)->s_journal) {
832 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
833 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
834 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
838 mnt_drop_write_file(filp);
844 case EXT4_IOC_MOVE_EXT: {
845 struct move_extent me;
849 if (!(filp->f_mode & FMODE_READ) ||
850 !(filp->f_mode & FMODE_WRITE))
853 if (copy_from_user(&me,
854 (struct move_extent __user *)arg, sizeof(me)))
858 donor = fdget(me.donor_fd);
862 if (!(donor.file->f_mode & FMODE_WRITE)) {
867 if (ext4_has_feature_bigalloc(sb)) {
868 ext4_msg(sb, KERN_ERR,
869 "Online defrag not supported with bigalloc");
872 } else if (IS_DAX(inode)) {
873 ext4_msg(sb, KERN_ERR,
874 "Online defrag not supported with DAX");
879 err = mnt_want_write_file(filp);
883 err = ext4_move_extents(filp, donor.file, me.orig_start,
884 me.donor_start, me.len, &me.moved_len);
885 mnt_drop_write_file(filp);
887 if (copy_to_user((struct move_extent __user *)arg,
895 case EXT4_IOC_GROUP_ADD: {
896 struct ext4_new_group_data input;
898 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
902 return ext4_ioctl_group_add(filp, &input);
905 case EXT4_IOC_MIGRATE:
908 if (!inode_owner_or_capable(inode))
911 err = mnt_want_write_file(filp);
915 * inode_mutex prevent write and truncate on the file.
916 * Read still goes through. We take i_data_sem in
917 * ext4_ext_swap_inode_data before we switch the
918 * inode format to prevent read.
921 err = ext4_ext_migrate(inode);
922 inode_unlock((inode));
923 mnt_drop_write_file(filp);
927 case EXT4_IOC_ALLOC_DA_BLKS:
930 if (!inode_owner_or_capable(inode))
933 err = mnt_want_write_file(filp);
936 err = ext4_alloc_da_blocks(inode);
937 mnt_drop_write_file(filp);
941 case EXT4_IOC_SWAP_BOOT:
944 if (!(filp->f_mode & FMODE_WRITE))
946 err = mnt_want_write_file(filp);
949 err = swap_inode_boot_loader(sb, inode);
950 mnt_drop_write_file(filp);
954 case EXT4_IOC_RESIZE_FS: {
955 ext4_fsblk_t n_blocks_count;
956 int err = 0, err2 = 0;
957 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
959 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
964 err = ext4_resize_begin(sb);
968 err = mnt_want_write_file(filp);
972 err = ext4_resize_fs(sb, n_blocks_count);
973 if (EXT4_SB(sb)->s_journal) {
974 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
975 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
976 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
980 mnt_drop_write_file(filp);
981 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
982 ext4_has_group_desc_csum(sb) &&
983 test_opt(sb, INIT_INODE_TABLE))
984 err = ext4_register_li_request(sb, o_group);
993 struct request_queue *q = bdev_get_queue(sb->s_bdev);
994 struct fstrim_range range;
997 if (!capable(CAP_SYS_ADMIN))
1000 if (!blk_queue_discard(q))
1003 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1007 range.minlen = max((unsigned int)range.minlen,
1008 q->limits.discard_granularity);
1009 ret = ext4_trim_fs(sb, &range);
1013 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1019 case EXT4_IOC_PRECACHE_EXTENTS:
1020 return ext4_ext_precache(inode);
1022 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1023 if (!ext4_has_feature_encrypt(sb))
1025 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1027 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
1028 #ifdef CONFIG_FS_ENCRYPTION
1030 struct ext4_sb_info *sbi = EXT4_SB(sb);
1033 if (!ext4_has_feature_encrypt(sb))
1035 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1036 err = mnt_want_write_file(filp);
1039 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1040 if (IS_ERR(handle)) {
1041 err = PTR_ERR(handle);
1042 goto pwsalt_err_exit;
1044 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1046 goto pwsalt_err_journal;
1047 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1048 err = ext4_handle_dirty_metadata(handle, NULL,
1051 err2 = ext4_journal_stop(handle);
1055 mnt_drop_write_file(filp);
1059 if (copy_to_user((void __user *) arg,
1060 sbi->s_es->s_encrypt_pw_salt, 16))
1067 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1068 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1070 case EXT4_IOC_FSGETXATTR:
1074 memset(&fa, 0, sizeof(struct fsxattr));
1075 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1077 if (ext4_has_feature_project(inode->i_sb)) {
1078 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1079 EXT4_I(inode)->i_projid);
1082 if (copy_to_user((struct fsxattr __user *)arg,
1087 case EXT4_IOC_FSSETXATTR:
1092 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1096 /* Make sure caller has proper permission */
1097 if (!inode_owner_or_capable(inode))
1100 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1103 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1104 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1107 err = mnt_want_write_file(filp);
1112 err = ext4_ioctl_check_project(inode, &fa);
1115 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1116 (flags & EXT4_FL_XFLAG_VISIBLE);
1117 err = ext4_ioctl_setflags(inode, flags);
1120 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1122 inode_unlock(inode);
1123 mnt_drop_write_file(filp);
1126 case EXT4_IOC_SHUTDOWN:
1127 return ext4_shutdown(sb, arg);
1133 #ifdef CONFIG_COMPAT
1134 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1136 /* These are just misnamed, they actually get/put from/to user an int */
1138 case EXT4_IOC32_GETFLAGS:
1139 cmd = EXT4_IOC_GETFLAGS;
1141 case EXT4_IOC32_SETFLAGS:
1142 cmd = EXT4_IOC_SETFLAGS;
1144 case EXT4_IOC32_GETVERSION:
1145 cmd = EXT4_IOC_GETVERSION;
1147 case EXT4_IOC32_SETVERSION:
1148 cmd = EXT4_IOC_SETVERSION;
1150 case EXT4_IOC32_GROUP_EXTEND:
1151 cmd = EXT4_IOC_GROUP_EXTEND;
1153 case EXT4_IOC32_GETVERSION_OLD:
1154 cmd = EXT4_IOC_GETVERSION_OLD;
1156 case EXT4_IOC32_SETVERSION_OLD:
1157 cmd = EXT4_IOC_SETVERSION_OLD;
1159 case EXT4_IOC32_GETRSVSZ:
1160 cmd = EXT4_IOC_GETRSVSZ;
1162 case EXT4_IOC32_SETRSVSZ:
1163 cmd = EXT4_IOC_SETRSVSZ;
1165 case EXT4_IOC32_GROUP_ADD: {
1166 struct compat_ext4_new_group_input __user *uinput;
1167 struct ext4_new_group_data input;
1170 uinput = compat_ptr(arg);
1171 err = get_user(input.group, &uinput->group);
1172 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1173 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1174 err |= get_user(input.inode_table, &uinput->inode_table);
1175 err |= get_user(input.blocks_count, &uinput->blocks_count);
1176 err |= get_user(input.reserved_blocks,
1177 &uinput->reserved_blocks);
1180 return ext4_ioctl_group_add(file, &input);
1182 case EXT4_IOC_MOVE_EXT:
1183 case EXT4_IOC_RESIZE_FS:
1184 case EXT4_IOC_PRECACHE_EXTENTS:
1185 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1186 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1187 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1188 case EXT4_IOC_SHUTDOWN:
1189 case FS_IOC_GETFSMAP:
1192 return -ENOIOCTLCMD;
1194 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));