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/uuid.h>
19 #include <linux/uaccess.h>
20 #include <linux/delay.h>
21 #include "ext4_jbd2.h"
23 #include <linux/fsmap.h>
25 #include <trace/events/ext4.h>
28 * Swap memory between @a and @b for @len bytes.
30 * @a: pointer to first memory area
31 * @b: pointer to second memory area
32 * @len: number of bytes to swap
35 static void memswap(void *a, void *b, size_t len)
37 unsigned char *ap, *bp;
39 ap = (unsigned char *)a;
40 bp = (unsigned char *)b;
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.
53 * Therefore you have to make sure, that calling this method twice
54 * will revert all changes.
56 * @inode1: pointer to first inode
57 * @inode2: pointer to second inode
59 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
62 struct ext4_inode_info *ei1;
63 struct ext4_inode_info *ei2;
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);
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);
85 isize = i_size_read(inode1);
86 i_size_write(inode1, i_size_read(inode2));
87 i_size_write(inode2, isize);
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.
95 * @sb: the super block of the filesystem
96 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
99 static long swap_inode_boot_loader(struct super_block *sb,
104 struct inode *inode_bl;
105 struct ext4_inode_info *ei_bl;
106 struct ext4_sb_info *sbi = EXT4_SB(sb);
108 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
111 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
114 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
115 if (IS_ERR(inode_bl))
116 return PTR_ERR(inode_bl);
117 ei_bl = EXT4_I(inode_bl);
119 filemap_flush(inode->i_mapping);
120 filemap_flush(inode_bl->i_mapping);
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);
126 truncate_inode_pages(&inode->i_data, 0);
127 truncate_inode_pages(&inode_bl->i_data, 0);
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);
135 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
136 if (IS_ERR(handle)) {
138 goto journal_err_out;
141 /* Protect extent tree against block allocations via delalloc */
142 ext4_double_down_write_data_sem(inode, inode_bl);
144 if (is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)) {
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;
151 inode_bl->i_version = 1;
152 i_size_write(inode_bl, 0);
153 EXT4_I(inode_bl)->i_disksize = inode_bl->i_size;
154 inode_bl->i_mode = S_IFREG;
155 if (ext4_has_feature_extents(sb)) {
156 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
157 ext4_ext_tree_init(handle, inode_bl);
159 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
162 swap_inode_data(inode, inode_bl);
164 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
166 spin_lock(&sbi->s_next_gen_lock);
167 inode->i_generation = sbi->s_next_generation++;
168 inode_bl->i_generation = sbi->s_next_generation++;
169 spin_unlock(&sbi->s_next_gen_lock);
171 ext4_discard_preallocations(inode);
173 err = ext4_mark_inode_dirty(handle, inode);
175 ext4_warning(inode->i_sb,
176 "couldn't mark inode #%lu dirty (err %d)",
178 /* Revert all changes: */
179 swap_inode_data(inode, inode_bl);
181 err = ext4_mark_inode_dirty(handle, inode_bl);
183 ext4_warning(inode_bl->i_sb,
184 "couldn't mark inode #%lu dirty (err %d)",
185 inode_bl->i_ino, err);
186 /* Revert all changes: */
187 swap_inode_data(inode, inode_bl);
188 ext4_mark_inode_dirty(handle, inode);
191 ext4_journal_stop(handle);
192 ext4_double_up_write_data_sem(inode, inode_bl);
195 ext4_inode_resume_unlocked_dio(inode);
196 ext4_inode_resume_unlocked_dio(inode_bl);
197 unlock_two_nondirectories(inode, inode_bl);
202 #ifdef CONFIG_EXT4_FS_ENCRYPTION
203 static int uuid_is_zero(__u8 u[16])
207 for (i = 0; i < 16; i++)
215 * If immutable is set and we are not clearing it, we're not allowed to change
216 * anything else in the inode. Don't error out if we're only trying to set
217 * immutable on an immutable file.
219 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
222 struct ext4_inode_info *ei = EXT4_I(inode);
223 unsigned int oldflags = ei->i_flags;
225 if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
228 if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
230 if (ext4_has_feature_project(inode->i_sb) &&
231 __kprojid_val(ei->i_projid) != new_projid)
237 static int ext4_ioctl_setflags(struct inode *inode,
240 struct ext4_inode_info *ei = EXT4_I(inode);
241 handle_t *handle = NULL;
242 int err = -EPERM, migrate = 0;
243 struct ext4_iloc iloc;
244 unsigned int oldflags, mask, i;
247 /* Is it quota file? Do not allow user to mess with it */
248 if (ext4_is_quota_file(inode))
251 oldflags = ei->i_flags;
253 /* The JOURNAL_DATA flag is modifiable only by root */
254 jflag = flags & EXT4_JOURNAL_DATA_FL;
257 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
258 * the relevant capability.
260 * This test looks nicer. Thanks to Pauline Middelink
262 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
263 if (!capable(CAP_LINUX_IMMUTABLE))
268 * The JOURNAL_DATA flag can only be changed by
269 * the relevant capability.
271 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
272 if (!capable(CAP_SYS_RESOURCE))
275 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
278 if (flags & EXT4_EOFBLOCKS_FL) {
279 /* we don't support adding EOFBLOCKS flag */
280 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
284 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
285 err = ext4_truncate(inode);
291 * Wait for all pending directio and then flush all the dirty pages
292 * for this file. The flush marks all the pages readonly, so any
293 * subsequent attempt to write to the file (particularly mmap pages)
294 * will come through the filesystem and fail.
296 if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
297 (flags & EXT4_IMMUTABLE_FL)) {
298 inode_dio_wait(inode);
299 err = filemap_write_and_wait(inode->i_mapping);
304 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
305 if (IS_ERR(handle)) {
306 err = PTR_ERR(handle);
310 ext4_handle_sync(handle);
311 err = ext4_reserve_inode_write(handle, inode, &iloc);
315 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
316 if (!(mask & EXT4_FL_USER_MODIFIABLE))
318 /* These flags get special treatment later */
319 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
322 ext4_set_inode_flag(inode, i);
324 ext4_clear_inode_flag(inode, i);
327 ext4_set_inode_flags(inode);
328 inode->i_ctime = current_time(inode);
330 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
332 ext4_journal_stop(handle);
336 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
338 * Changes to the journaling mode can cause unsafe changes to
339 * S_DAX if we are using the DAX mount option.
341 if (test_opt(inode->i_sb, DAX)) {
346 err = ext4_change_inode_journal_flag(inode, jflag);
351 if (flags & EXT4_EXTENTS_FL)
352 err = ext4_ext_migrate(inode);
354 err = ext4_ind_migrate(inode);
362 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
364 struct inode *inode = file_inode(filp);
365 struct super_block *sb = inode->i_sb;
366 struct ext4_inode_info *ei = EXT4_I(inode);
370 struct ext4_iloc iloc;
371 struct ext4_inode *raw_inode;
372 struct dquot *transfer_to[MAXQUOTAS] = { };
374 if (!ext4_has_feature_project(sb)) {
375 if (projid != EXT4_DEF_PROJID)
381 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
384 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
386 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
390 /* Is it quota file? Do not allow user to mess with it */
391 if (ext4_is_quota_file(inode))
394 err = dquot_initialize(inode);
398 err = ext4_get_inode_loc(inode, &iloc);
402 raw_inode = ext4_raw_inode(&iloc);
403 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
404 err = ext4_expand_extra_isize(inode,
405 EXT4_SB(sb)->s_want_extra_isize,
413 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
414 EXT4_QUOTA_INIT_BLOCKS(sb) +
415 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
417 return PTR_ERR(handle);
419 err = ext4_reserve_inode_write(handle, inode, &iloc);
423 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
424 if (!IS_ERR(transfer_to[PRJQUOTA])) {
426 /* __dquot_transfer() calls back ext4_get_inode_usage() which
427 * counts xattr inode references.
429 down_read(&EXT4_I(inode)->xattr_sem);
430 err = __dquot_transfer(inode, transfer_to);
431 up_read(&EXT4_I(inode)->xattr_sem);
432 dqput(transfer_to[PRJQUOTA]);
437 EXT4_I(inode)->i_projid = kprojid;
438 inode->i_ctime = current_time(inode);
440 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
444 ext4_journal_stop(handle);
448 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
450 if (projid != EXT4_DEF_PROJID)
456 /* Transfer internal flags to xflags */
457 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
461 if (iflags & EXT4_SYNC_FL)
462 xflags |= FS_XFLAG_SYNC;
463 if (iflags & EXT4_IMMUTABLE_FL)
464 xflags |= FS_XFLAG_IMMUTABLE;
465 if (iflags & EXT4_APPEND_FL)
466 xflags |= FS_XFLAG_APPEND;
467 if (iflags & EXT4_NODUMP_FL)
468 xflags |= FS_XFLAG_NODUMP;
469 if (iflags & EXT4_NOATIME_FL)
470 xflags |= FS_XFLAG_NOATIME;
471 if (iflags & EXT4_PROJINHERIT_FL)
472 xflags |= FS_XFLAG_PROJINHERIT;
476 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
477 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
478 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
480 /* Transfer xflags flags to internal */
481 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
483 unsigned long iflags = 0;
485 if (xflags & FS_XFLAG_SYNC)
486 iflags |= EXT4_SYNC_FL;
487 if (xflags & FS_XFLAG_IMMUTABLE)
488 iflags |= EXT4_IMMUTABLE_FL;
489 if (xflags & FS_XFLAG_APPEND)
490 iflags |= EXT4_APPEND_FL;
491 if (xflags & FS_XFLAG_NODUMP)
492 iflags |= EXT4_NODUMP_FL;
493 if (xflags & FS_XFLAG_NOATIME)
494 iflags |= EXT4_NOATIME_FL;
495 if (xflags & FS_XFLAG_PROJINHERIT)
496 iflags |= EXT4_PROJINHERIT_FL;
501 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
503 struct ext4_sb_info *sbi = EXT4_SB(sb);
506 if (!capable(CAP_SYS_ADMIN))
509 if (get_user(flags, (__u32 __user *)arg))
512 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
515 if (ext4_forced_shutdown(sbi))
518 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
521 case EXT4_GOING_FLAGS_DEFAULT:
522 freeze_bdev(sb->s_bdev);
523 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
524 thaw_bdev(sb->s_bdev, sb);
526 case EXT4_GOING_FLAGS_LOGFLUSH:
527 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
528 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
529 (void) ext4_force_commit(sb);
530 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
533 case EXT4_GOING_FLAGS_NOLOGFLUSH:
534 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
535 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
536 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
541 clear_opt(sb, DISCARD);
545 struct getfsmap_info {
546 struct super_block *gi_sb;
547 struct fsmap_head __user *gi_data;
552 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
554 struct getfsmap_info *info = priv;
557 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
559 info->gi_last_flags = xfm->fmr_flags;
560 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
561 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
562 sizeof(struct fsmap)))
568 static int ext4_ioc_getfsmap(struct super_block *sb,
569 struct fsmap_head __user *arg)
571 struct getfsmap_info info = {0};
572 struct ext4_fsmap_head xhead = {0};
573 struct fsmap_head head;
574 bool aborted = false;
577 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
579 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
580 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
581 sizeof(head.fmh_keys[0].fmr_reserved)) ||
582 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
583 sizeof(head.fmh_keys[1].fmr_reserved)))
586 * ext4 doesn't report file extents at all, so the only valid
587 * file offsets are the magic ones (all zeroes or all ones).
589 if (head.fmh_keys[0].fmr_offset ||
590 (head.fmh_keys[1].fmr_offset != 0 &&
591 head.fmh_keys[1].fmr_offset != -1ULL))
594 xhead.fmh_iflags = head.fmh_iflags;
595 xhead.fmh_count = head.fmh_count;
596 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
597 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
599 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
600 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
604 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
605 if (error == EXT4_QUERY_RANGE_ABORT) {
611 /* If we didn't abort, set the "last" flag in the last fmx */
612 if (!aborted && info.gi_idx) {
613 info.gi_last_flags |= FMR_OF_LAST;
614 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
616 sizeof(info.gi_last_flags)))
620 /* copy back header */
621 head.fmh_entries = xhead.fmh_entries;
622 head.fmh_oflags = xhead.fmh_oflags;
623 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
629 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
632 * Project Quota ID state is only allowed to change from within the init
633 * namespace. Enforce that restriction only if we are trying to change
634 * the quota ID state. Everything else is allowed in user namespaces.
636 if (current_user_ns() == &init_user_ns)
639 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
642 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
643 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
646 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
653 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
655 struct inode *inode = file_inode(filp);
656 struct super_block *sb = inode->i_sb;
657 struct ext4_inode_info *ei = EXT4_I(inode);
660 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
663 case FS_IOC_GETFSMAP:
664 return ext4_ioc_getfsmap(sb, (void __user *)arg);
665 case EXT4_IOC_GETFLAGS:
666 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
667 return put_user(flags, (int __user *) arg);
668 case EXT4_IOC_SETFLAGS: {
671 if (!inode_owner_or_capable(inode))
674 if (get_user(flags, (int __user *) arg))
677 if (flags & ~EXT4_FL_USER_VISIBLE)
680 * chattr(1) grabs flags via GETFLAGS, modifies the result and
681 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
682 * more restrictive than just silently masking off visible but
683 * not settable flags as we always did.
685 flags &= EXT4_FL_USER_MODIFIABLE;
686 if (ext4_mask_flags(inode->i_mode, flags) != flags)
689 err = mnt_want_write_file(filp);
694 err = ext4_ioctl_check_immutable(inode,
695 from_kprojid(&init_user_ns, ei->i_projid),
698 err = ext4_ioctl_setflags(inode, flags);
700 mnt_drop_write_file(filp);
703 case EXT4_IOC_GETVERSION:
704 case EXT4_IOC_GETVERSION_OLD:
705 return put_user(inode->i_generation, (int __user *) arg);
706 case EXT4_IOC_SETVERSION:
707 case EXT4_IOC_SETVERSION_OLD: {
709 struct ext4_iloc iloc;
713 if (!inode_owner_or_capable(inode))
716 if (ext4_has_metadata_csum(inode->i_sb)) {
717 ext4_warning(sb, "Setting inode version is not "
718 "supported with metadata_csum enabled.");
722 err = mnt_want_write_file(filp);
725 if (get_user(generation, (int __user *) arg)) {
731 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
732 if (IS_ERR(handle)) {
733 err = PTR_ERR(handle);
736 err = ext4_reserve_inode_write(handle, inode, &iloc);
738 inode->i_ctime = current_time(inode);
739 inode->i_generation = generation;
740 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
742 ext4_journal_stop(handle);
747 mnt_drop_write_file(filp);
750 case EXT4_IOC_GROUP_EXTEND: {
751 ext4_fsblk_t n_blocks_count;
754 err = ext4_resize_begin(sb);
758 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
760 goto group_extend_out;
763 if (ext4_has_feature_bigalloc(sb)) {
764 ext4_msg(sb, KERN_ERR,
765 "Online resizing not supported with bigalloc");
767 goto group_extend_out;
770 err = mnt_want_write_file(filp);
772 goto group_extend_out;
774 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
775 if (EXT4_SB(sb)->s_journal) {
776 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
777 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
778 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
782 mnt_drop_write_file(filp);
788 case EXT4_IOC_MOVE_EXT: {
789 struct move_extent me;
793 if (!(filp->f_mode & FMODE_READ) ||
794 !(filp->f_mode & FMODE_WRITE))
797 if (copy_from_user(&me,
798 (struct move_extent __user *)arg, sizeof(me)))
802 donor = fdget(me.donor_fd);
806 if (!(donor.file->f_mode & FMODE_WRITE)) {
811 if (ext4_has_feature_bigalloc(sb)) {
812 ext4_msg(sb, KERN_ERR,
813 "Online defrag not supported with bigalloc");
816 } else if (IS_DAX(inode)) {
817 ext4_msg(sb, KERN_ERR,
818 "Online defrag not supported with DAX");
823 err = mnt_want_write_file(filp);
827 err = ext4_move_extents(filp, donor.file, me.orig_start,
828 me.donor_start, me.len, &me.moved_len);
829 mnt_drop_write_file(filp);
831 if (copy_to_user((struct move_extent __user *)arg,
839 case EXT4_IOC_GROUP_ADD: {
840 struct ext4_new_group_data input;
843 err = ext4_resize_begin(sb);
847 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
853 if (ext4_has_feature_bigalloc(sb)) {
854 ext4_msg(sb, KERN_ERR,
855 "Online resizing not supported with bigalloc");
860 err = mnt_want_write_file(filp);
864 err = ext4_group_add(sb, &input);
865 if (EXT4_SB(sb)->s_journal) {
866 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
867 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
868 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
872 mnt_drop_write_file(filp);
873 if (!err && ext4_has_group_desc_csum(sb) &&
874 test_opt(sb, INIT_INODE_TABLE))
875 err = ext4_register_li_request(sb, input.group);
881 case EXT4_IOC_MIGRATE:
884 if (!inode_owner_or_capable(inode))
887 err = mnt_want_write_file(filp);
891 * inode_mutex prevent write and truncate on the file.
892 * Read still goes through. We take i_data_sem in
893 * ext4_ext_swap_inode_data before we switch the
894 * inode format to prevent read.
897 err = ext4_ext_migrate(inode);
898 inode_unlock((inode));
899 mnt_drop_write_file(filp);
903 case EXT4_IOC_ALLOC_DA_BLKS:
906 if (!inode_owner_or_capable(inode))
909 err = mnt_want_write_file(filp);
912 err = ext4_alloc_da_blocks(inode);
913 mnt_drop_write_file(filp);
917 case EXT4_IOC_SWAP_BOOT:
920 if (!(filp->f_mode & FMODE_WRITE))
922 err = mnt_want_write_file(filp);
925 err = swap_inode_boot_loader(sb, inode);
926 mnt_drop_write_file(filp);
930 case EXT4_IOC_RESIZE_FS: {
931 ext4_fsblk_t n_blocks_count;
932 int err = 0, err2 = 0;
933 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
935 if (ext4_has_feature_bigalloc(sb)) {
936 ext4_msg(sb, KERN_ERR,
937 "Online resizing not (yet) supported with bigalloc");
941 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
946 err = ext4_resize_begin(sb);
950 err = mnt_want_write_file(filp);
954 err = ext4_resize_fs(sb, n_blocks_count);
955 if (EXT4_SB(sb)->s_journal) {
956 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
957 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
958 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
962 mnt_drop_write_file(filp);
963 if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
964 ext4_has_group_desc_csum(sb) &&
965 test_opt(sb, INIT_INODE_TABLE))
966 err = ext4_register_li_request(sb, o_group);
975 struct request_queue *q = bdev_get_queue(sb->s_bdev);
976 struct fstrim_range range;
979 if (!capable(CAP_SYS_ADMIN))
982 if (!blk_queue_discard(q))
986 * We haven't replayed the journal, so we cannot use our
987 * block-bitmap-guided storage zapping commands.
989 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
992 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
996 ret = ext4_trim_fs(sb, &range);
1000 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1006 case EXT4_IOC_PRECACHE_EXTENTS:
1007 return ext4_ext_precache(inode);
1009 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1010 if (!ext4_has_feature_encrypt(sb))
1012 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1014 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
1015 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1017 struct ext4_sb_info *sbi = EXT4_SB(sb);
1020 if (!ext4_has_feature_encrypt(sb))
1022 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1023 err = mnt_want_write_file(filp);
1026 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1027 if (IS_ERR(handle)) {
1028 err = PTR_ERR(handle);
1029 goto pwsalt_err_exit;
1031 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1033 goto pwsalt_err_journal;
1034 lock_buffer(sbi->s_sbh);
1035 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1036 ext4_superblock_csum_set(sb);
1037 unlock_buffer(sbi->s_sbh);
1038 err = ext4_handle_dirty_metadata(handle, NULL,
1041 err2 = ext4_journal_stop(handle);
1045 mnt_drop_write_file(filp);
1049 if (copy_to_user((void __user *) arg,
1050 sbi->s_es->s_encrypt_pw_salt, 16))
1057 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1058 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1060 case EXT4_IOC_FSGETXATTR:
1064 memset(&fa, 0, sizeof(struct fsxattr));
1065 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1067 if (ext4_has_feature_project(inode->i_sb)) {
1068 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1069 EXT4_I(inode)->i_projid);
1072 if (copy_to_user((struct fsxattr __user *)arg,
1077 case EXT4_IOC_FSSETXATTR:
1082 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1086 /* Make sure caller has proper permission */
1087 if (!inode_owner_or_capable(inode))
1090 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1093 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1094 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1097 err = mnt_want_write_file(filp);
1102 err = ext4_ioctl_check_project(inode, &fa);
1105 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1106 (flags & EXT4_FL_XFLAG_VISIBLE);
1107 err = ext4_ioctl_check_immutable(inode, fa.fsx_projid, flags);
1110 err = ext4_ioctl_setflags(inode, flags);
1113 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1115 inode_unlock(inode);
1116 mnt_drop_write_file(filp);
1119 case EXT4_IOC_SHUTDOWN:
1120 return ext4_shutdown(sb, arg);
1126 #ifdef CONFIG_COMPAT
1127 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1129 /* These are just misnamed, they actually get/put from/to user an int */
1131 case EXT4_IOC32_GETFLAGS:
1132 cmd = EXT4_IOC_GETFLAGS;
1134 case EXT4_IOC32_SETFLAGS:
1135 cmd = EXT4_IOC_SETFLAGS;
1137 case EXT4_IOC32_GETVERSION:
1138 cmd = EXT4_IOC_GETVERSION;
1140 case EXT4_IOC32_SETVERSION:
1141 cmd = EXT4_IOC_SETVERSION;
1143 case EXT4_IOC32_GROUP_EXTEND:
1144 cmd = EXT4_IOC_GROUP_EXTEND;
1146 case EXT4_IOC32_GETVERSION_OLD:
1147 cmd = EXT4_IOC_GETVERSION_OLD;
1149 case EXT4_IOC32_SETVERSION_OLD:
1150 cmd = EXT4_IOC_SETVERSION_OLD;
1152 case EXT4_IOC32_GETRSVSZ:
1153 cmd = EXT4_IOC_GETRSVSZ;
1155 case EXT4_IOC32_SETRSVSZ:
1156 cmd = EXT4_IOC_SETRSVSZ;
1158 case EXT4_IOC32_GROUP_ADD: {
1159 struct compat_ext4_new_group_input __user *uinput;
1160 struct ext4_new_group_input input;
1161 mm_segment_t old_fs;
1164 uinput = compat_ptr(arg);
1165 err = get_user(input.group, &uinput->group);
1166 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1167 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1168 err |= get_user(input.inode_table, &uinput->inode_table);
1169 err |= get_user(input.blocks_count, &uinput->blocks_count);
1170 err |= get_user(input.reserved_blocks,
1171 &uinput->reserved_blocks);
1176 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
1177 (unsigned long) &input);
1181 case EXT4_IOC_MOVE_EXT:
1182 case EXT4_IOC_RESIZE_FS:
1183 case EXT4_IOC_PRECACHE_EXTENTS:
1184 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1185 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1186 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1187 case EXT4_IOC_SHUTDOWN:
1188 case FS_IOC_GETFSMAP:
1191 return -ENOIOCTLCMD;
1193 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));