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 (inode_bl->i_nlink == 0) {
145 /* this inode has never been used as a BOOT_LOADER */
146 set_nlink(inode_bl, 1);
147 i_uid_write(inode_bl, 0);
148 i_gid_write(inode_bl, 0);
149 inode_bl->i_flags = 0;
151 inode_bl->i_version = 1;
152 i_size_write(inode_bl, 0);
153 inode_bl->i_mode = S_IFREG;
154 if (ext4_has_feature_extents(sb)) {
155 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
156 ext4_ext_tree_init(handle, inode_bl);
158 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
161 swap_inode_data(inode, inode_bl);
163 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
165 spin_lock(&sbi->s_next_gen_lock);
166 inode->i_generation = sbi->s_next_generation++;
167 inode_bl->i_generation = sbi->s_next_generation++;
168 spin_unlock(&sbi->s_next_gen_lock);
170 ext4_discard_preallocations(inode);
172 err = ext4_mark_inode_dirty(handle, inode);
174 ext4_warning(inode->i_sb,
175 "couldn't mark inode #%lu dirty (err %d)",
177 /* Revert all changes: */
178 swap_inode_data(inode, inode_bl);
180 err = ext4_mark_inode_dirty(handle, inode_bl);
182 ext4_warning(inode_bl->i_sb,
183 "couldn't mark inode #%lu dirty (err %d)",
184 inode_bl->i_ino, err);
185 /* Revert all changes: */
186 swap_inode_data(inode, inode_bl);
187 ext4_mark_inode_dirty(handle, inode);
190 ext4_journal_stop(handle);
191 ext4_double_up_write_data_sem(inode, inode_bl);
194 ext4_inode_resume_unlocked_dio(inode);
195 ext4_inode_resume_unlocked_dio(inode_bl);
196 unlock_two_nondirectories(inode, inode_bl);
201 #ifdef CONFIG_EXT4_FS_ENCRYPTION
202 static int uuid_is_zero(__u8 u[16])
206 for (i = 0; i < 16; i++)
214 * If immutable is set and we are not clearing it, we're not allowed to change
215 * anything else in the inode. Don't error out if we're only trying to set
216 * immutable on an immutable file.
218 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
221 struct ext4_inode_info *ei = EXT4_I(inode);
222 unsigned int oldflags = ei->i_flags;
224 if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
227 if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
229 if (ext4_has_feature_project(inode->i_sb) &&
230 __kprojid_val(ei->i_projid) != new_projid)
236 static int ext4_ioctl_setflags(struct inode *inode,
239 struct ext4_inode_info *ei = EXT4_I(inode);
240 handle_t *handle = NULL;
241 int err = -EPERM, migrate = 0;
242 struct ext4_iloc iloc;
243 unsigned int oldflags, mask, i;
246 /* Is it quota file? Do not allow user to mess with it */
247 if (ext4_is_quota_file(inode))
250 oldflags = ei->i_flags;
252 /* The JOURNAL_DATA flag is modifiable only by root */
253 jflag = flags & EXT4_JOURNAL_DATA_FL;
256 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
257 * the relevant capability.
259 * This test looks nicer. Thanks to Pauline Middelink
261 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
262 if (!capable(CAP_LINUX_IMMUTABLE))
267 * The JOURNAL_DATA flag can only be changed by
268 * the relevant capability.
270 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
271 if (!capable(CAP_SYS_RESOURCE))
274 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
277 if (flags & EXT4_EOFBLOCKS_FL) {
278 /* we don't support adding EOFBLOCKS flag */
279 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
283 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
284 err = ext4_truncate(inode);
290 * Wait for all pending directio and then flush all the dirty pages
291 * for this file. The flush marks all the pages readonly, so any
292 * subsequent attempt to write to the file (particularly mmap pages)
293 * will come through the filesystem and fail.
295 if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
296 (flags & EXT4_IMMUTABLE_FL)) {
297 inode_dio_wait(inode);
298 err = filemap_write_and_wait(inode->i_mapping);
303 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
304 if (IS_ERR(handle)) {
305 err = PTR_ERR(handle);
309 ext4_handle_sync(handle);
310 err = ext4_reserve_inode_write(handle, inode, &iloc);
314 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
315 if (!(mask & EXT4_FL_USER_MODIFIABLE))
317 /* These flags get special treatment later */
318 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
321 ext4_set_inode_flag(inode, i);
323 ext4_clear_inode_flag(inode, i);
326 ext4_set_inode_flags(inode);
327 inode->i_ctime = current_time(inode);
329 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
331 ext4_journal_stop(handle);
335 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
337 * Changes to the journaling mode can cause unsafe changes to
338 * S_DAX if we are using the DAX mount option.
340 if (test_opt(inode->i_sb, DAX)) {
345 err = ext4_change_inode_journal_flag(inode, jflag);
350 if (flags & EXT4_EXTENTS_FL)
351 err = ext4_ext_migrate(inode);
353 err = ext4_ind_migrate(inode);
361 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
363 struct inode *inode = file_inode(filp);
364 struct super_block *sb = inode->i_sb;
365 struct ext4_inode_info *ei = EXT4_I(inode);
369 struct ext4_iloc iloc;
370 struct ext4_inode *raw_inode;
371 struct dquot *transfer_to[MAXQUOTAS] = { };
373 if (!ext4_has_feature_project(sb)) {
374 if (projid != EXT4_DEF_PROJID)
380 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
383 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
385 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
389 /* Is it quota file? Do not allow user to mess with it */
390 if (ext4_is_quota_file(inode))
393 err = ext4_get_inode_loc(inode, &iloc);
397 raw_inode = ext4_raw_inode(&iloc);
398 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
399 err = ext4_expand_extra_isize(inode,
400 EXT4_SB(sb)->s_want_extra_isize,
408 err = dquot_initialize(inode);
412 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
413 EXT4_QUOTA_INIT_BLOCKS(sb) +
414 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
416 return PTR_ERR(handle);
418 err = ext4_reserve_inode_write(handle, inode, &iloc);
422 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
423 if (!IS_ERR(transfer_to[PRJQUOTA])) {
425 /* __dquot_transfer() calls back ext4_get_inode_usage() which
426 * counts xattr inode references.
428 down_read(&EXT4_I(inode)->xattr_sem);
429 err = __dquot_transfer(inode, transfer_to);
430 up_read(&EXT4_I(inode)->xattr_sem);
431 dqput(transfer_to[PRJQUOTA]);
436 EXT4_I(inode)->i_projid = kprojid;
437 inode->i_ctime = current_time(inode);
439 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
443 ext4_journal_stop(handle);
447 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
449 if (projid != EXT4_DEF_PROJID)
455 /* Transfer internal flags to xflags */
456 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
460 if (iflags & EXT4_SYNC_FL)
461 xflags |= FS_XFLAG_SYNC;
462 if (iflags & EXT4_IMMUTABLE_FL)
463 xflags |= FS_XFLAG_IMMUTABLE;
464 if (iflags & EXT4_APPEND_FL)
465 xflags |= FS_XFLAG_APPEND;
466 if (iflags & EXT4_NODUMP_FL)
467 xflags |= FS_XFLAG_NODUMP;
468 if (iflags & EXT4_NOATIME_FL)
469 xflags |= FS_XFLAG_NOATIME;
470 if (iflags & EXT4_PROJINHERIT_FL)
471 xflags |= FS_XFLAG_PROJINHERIT;
475 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
476 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
477 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
479 /* Transfer xflags flags to internal */
480 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
482 unsigned long iflags = 0;
484 if (xflags & FS_XFLAG_SYNC)
485 iflags |= EXT4_SYNC_FL;
486 if (xflags & FS_XFLAG_IMMUTABLE)
487 iflags |= EXT4_IMMUTABLE_FL;
488 if (xflags & FS_XFLAG_APPEND)
489 iflags |= EXT4_APPEND_FL;
490 if (xflags & FS_XFLAG_NODUMP)
491 iflags |= EXT4_NODUMP_FL;
492 if (xflags & FS_XFLAG_NOATIME)
493 iflags |= EXT4_NOATIME_FL;
494 if (xflags & FS_XFLAG_PROJINHERIT)
495 iflags |= EXT4_PROJINHERIT_FL;
500 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
502 struct ext4_sb_info *sbi = EXT4_SB(sb);
505 if (!capable(CAP_SYS_ADMIN))
508 if (get_user(flags, (__u32 __user *)arg))
511 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
514 if (ext4_forced_shutdown(sbi))
517 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
520 case EXT4_GOING_FLAGS_DEFAULT:
521 freeze_bdev(sb->s_bdev);
522 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
523 thaw_bdev(sb->s_bdev, sb);
525 case EXT4_GOING_FLAGS_LOGFLUSH:
526 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
527 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
528 (void) ext4_force_commit(sb);
529 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
532 case EXT4_GOING_FLAGS_NOLOGFLUSH:
533 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
534 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
535 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
540 clear_opt(sb, DISCARD);
544 struct getfsmap_info {
545 struct super_block *gi_sb;
546 struct fsmap_head __user *gi_data;
551 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
553 struct getfsmap_info *info = priv;
556 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
558 info->gi_last_flags = xfm->fmr_flags;
559 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
560 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
561 sizeof(struct fsmap)))
567 static int ext4_ioc_getfsmap(struct super_block *sb,
568 struct fsmap_head __user *arg)
570 struct getfsmap_info info = {0};
571 struct ext4_fsmap_head xhead = {0};
572 struct fsmap_head head;
573 bool aborted = false;
576 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
578 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
579 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
580 sizeof(head.fmh_keys[0].fmr_reserved)) ||
581 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
582 sizeof(head.fmh_keys[1].fmr_reserved)))
585 * ext4 doesn't report file extents at all, so the only valid
586 * file offsets are the magic ones (all zeroes or all ones).
588 if (head.fmh_keys[0].fmr_offset ||
589 (head.fmh_keys[1].fmr_offset != 0 &&
590 head.fmh_keys[1].fmr_offset != -1ULL))
593 xhead.fmh_iflags = head.fmh_iflags;
594 xhead.fmh_count = head.fmh_count;
595 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
596 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
598 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
599 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
603 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
604 if (error == EXT4_QUERY_RANGE_ABORT) {
610 /* If we didn't abort, set the "last" flag in the last fmx */
611 if (!aborted && info.gi_idx) {
612 info.gi_last_flags |= FMR_OF_LAST;
613 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
615 sizeof(info.gi_last_flags)))
619 /* copy back header */
620 head.fmh_entries = xhead.fmh_entries;
621 head.fmh_oflags = xhead.fmh_oflags;
622 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
628 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
631 * Project Quota ID state is only allowed to change from within the init
632 * namespace. Enforce that restriction only if we are trying to change
633 * the quota ID state. Everything else is allowed in user namespaces.
635 if (current_user_ns() == &init_user_ns)
638 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
641 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
642 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
645 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
652 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
654 struct inode *inode = file_inode(filp);
655 struct super_block *sb = inode->i_sb;
656 struct ext4_inode_info *ei = EXT4_I(inode);
659 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
662 case FS_IOC_GETFSMAP:
663 return ext4_ioc_getfsmap(sb, (void __user *)arg);
664 case EXT4_IOC_GETFLAGS:
665 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
666 return put_user(flags, (int __user *) arg);
667 case EXT4_IOC_SETFLAGS: {
670 if (!inode_owner_or_capable(inode))
673 if (get_user(flags, (int __user *) arg))
676 if (flags & ~EXT4_FL_USER_VISIBLE)
679 * chattr(1) grabs flags via GETFLAGS, modifies the result and
680 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
681 * more restrictive than just silently masking off visible but
682 * not settable flags as we always did.
684 flags &= EXT4_FL_USER_MODIFIABLE;
685 if (ext4_mask_flags(inode->i_mode, flags) != flags)
688 err = mnt_want_write_file(filp);
693 err = ext4_ioctl_check_immutable(inode,
694 from_kprojid(&init_user_ns, ei->i_projid),
697 err = ext4_ioctl_setflags(inode, flags);
699 mnt_drop_write_file(filp);
702 case EXT4_IOC_GETVERSION:
703 case EXT4_IOC_GETVERSION_OLD:
704 return put_user(inode->i_generation, (int __user *) arg);
705 case EXT4_IOC_SETVERSION:
706 case EXT4_IOC_SETVERSION_OLD: {
708 struct ext4_iloc iloc;
712 if (!inode_owner_or_capable(inode))
715 if (ext4_has_metadata_csum(inode->i_sb)) {
716 ext4_warning(sb, "Setting inode version is not "
717 "supported with metadata_csum enabled.");
721 err = mnt_want_write_file(filp);
724 if (get_user(generation, (int __user *) arg)) {
730 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
731 if (IS_ERR(handle)) {
732 err = PTR_ERR(handle);
735 err = ext4_reserve_inode_write(handle, inode, &iloc);
737 inode->i_ctime = current_time(inode);
738 inode->i_generation = generation;
739 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
741 ext4_journal_stop(handle);
746 mnt_drop_write_file(filp);
749 case EXT4_IOC_GROUP_EXTEND: {
750 ext4_fsblk_t n_blocks_count;
753 err = ext4_resize_begin(sb);
757 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
759 goto group_extend_out;
762 if (ext4_has_feature_bigalloc(sb)) {
763 ext4_msg(sb, KERN_ERR,
764 "Online resizing not supported with bigalloc");
766 goto group_extend_out;
769 err = mnt_want_write_file(filp);
771 goto group_extend_out;
773 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
774 if (EXT4_SB(sb)->s_journal) {
775 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
776 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
777 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
781 mnt_drop_write_file(filp);
787 case EXT4_IOC_MOVE_EXT: {
788 struct move_extent me;
792 if (!(filp->f_mode & FMODE_READ) ||
793 !(filp->f_mode & FMODE_WRITE))
796 if (copy_from_user(&me,
797 (struct move_extent __user *)arg, sizeof(me)))
801 donor = fdget(me.donor_fd);
805 if (!(donor.file->f_mode & FMODE_WRITE)) {
810 if (ext4_has_feature_bigalloc(sb)) {
811 ext4_msg(sb, KERN_ERR,
812 "Online defrag not supported with bigalloc");
815 } else if (IS_DAX(inode)) {
816 ext4_msg(sb, KERN_ERR,
817 "Online defrag not supported with DAX");
822 err = mnt_want_write_file(filp);
826 err = ext4_move_extents(filp, donor.file, me.orig_start,
827 me.donor_start, me.len, &me.moved_len);
828 mnt_drop_write_file(filp);
830 if (copy_to_user((struct move_extent __user *)arg,
838 case EXT4_IOC_GROUP_ADD: {
839 struct ext4_new_group_data input;
842 err = ext4_resize_begin(sb);
846 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
852 if (ext4_has_feature_bigalloc(sb)) {
853 ext4_msg(sb, KERN_ERR,
854 "Online resizing not supported with bigalloc");
859 err = mnt_want_write_file(filp);
863 err = ext4_group_add(sb, &input);
864 if (EXT4_SB(sb)->s_journal) {
865 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
866 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
867 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
871 mnt_drop_write_file(filp);
872 if (!err && ext4_has_group_desc_csum(sb) &&
873 test_opt(sb, INIT_INODE_TABLE))
874 err = ext4_register_li_request(sb, input.group);
880 case EXT4_IOC_MIGRATE:
883 if (!inode_owner_or_capable(inode))
886 err = mnt_want_write_file(filp);
890 * inode_mutex prevent write and truncate on the file.
891 * Read still goes through. We take i_data_sem in
892 * ext4_ext_swap_inode_data before we switch the
893 * inode format to prevent read.
896 err = ext4_ext_migrate(inode);
897 inode_unlock((inode));
898 mnt_drop_write_file(filp);
902 case EXT4_IOC_ALLOC_DA_BLKS:
905 if (!inode_owner_or_capable(inode))
908 err = mnt_want_write_file(filp);
911 err = ext4_alloc_da_blocks(inode);
912 mnt_drop_write_file(filp);
916 case EXT4_IOC_SWAP_BOOT:
919 if (!(filp->f_mode & FMODE_WRITE))
921 err = mnt_want_write_file(filp);
924 err = swap_inode_boot_loader(sb, inode);
925 mnt_drop_write_file(filp);
929 case EXT4_IOC_RESIZE_FS: {
930 ext4_fsblk_t n_blocks_count;
931 int err = 0, err2 = 0;
932 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
934 if (ext4_has_feature_bigalloc(sb)) {
935 ext4_msg(sb, KERN_ERR,
936 "Online resizing not (yet) supported with bigalloc");
940 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
945 err = ext4_resize_begin(sb);
949 err = mnt_want_write_file(filp);
953 err = ext4_resize_fs(sb, n_blocks_count);
954 if (EXT4_SB(sb)->s_journal) {
955 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
956 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
957 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
961 mnt_drop_write_file(filp);
962 if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
963 ext4_has_group_desc_csum(sb) &&
964 test_opt(sb, INIT_INODE_TABLE))
965 err = ext4_register_li_request(sb, o_group);
974 struct request_queue *q = bdev_get_queue(sb->s_bdev);
975 struct fstrim_range range;
978 if (!capable(CAP_SYS_ADMIN))
981 if (!blk_queue_discard(q))
985 * We haven't replayed the journal, so we cannot use our
986 * block-bitmap-guided storage zapping commands.
988 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
991 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
995 range.minlen = max((unsigned int)range.minlen,
996 q->limits.discard_granularity);
997 ret = ext4_trim_fs(sb, &range);
1001 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1007 case EXT4_IOC_PRECACHE_EXTENTS:
1008 return ext4_ext_precache(inode);
1010 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1011 if (!ext4_has_feature_encrypt(sb))
1013 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1015 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
1016 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1018 struct ext4_sb_info *sbi = EXT4_SB(sb);
1021 if (!ext4_has_feature_encrypt(sb))
1023 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1024 err = mnt_want_write_file(filp);
1027 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1028 if (IS_ERR(handle)) {
1029 err = PTR_ERR(handle);
1030 goto pwsalt_err_exit;
1032 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1034 goto pwsalt_err_journal;
1035 lock_buffer(sbi->s_sbh);
1036 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1037 ext4_superblock_csum_set(sb);
1038 unlock_buffer(sbi->s_sbh);
1039 err = ext4_handle_dirty_metadata(handle, NULL,
1042 err2 = ext4_journal_stop(handle);
1046 mnt_drop_write_file(filp);
1050 if (copy_to_user((void __user *) arg,
1051 sbi->s_es->s_encrypt_pw_salt, 16))
1058 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1059 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1061 case EXT4_IOC_FSGETXATTR:
1065 memset(&fa, 0, sizeof(struct fsxattr));
1066 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1068 if (ext4_has_feature_project(inode->i_sb)) {
1069 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1070 EXT4_I(inode)->i_projid);
1073 if (copy_to_user((struct fsxattr __user *)arg,
1078 case EXT4_IOC_FSSETXATTR:
1083 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1087 /* Make sure caller has proper permission */
1088 if (!inode_owner_or_capable(inode))
1091 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1094 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1095 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1098 err = mnt_want_write_file(filp);
1103 err = ext4_ioctl_check_project(inode, &fa);
1106 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1107 (flags & EXT4_FL_XFLAG_VISIBLE);
1108 err = ext4_ioctl_check_immutable(inode, fa.fsx_projid, flags);
1111 err = ext4_ioctl_setflags(inode, flags);
1114 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1116 inode_unlock(inode);
1117 mnt_drop_write_file(filp);
1120 case EXT4_IOC_SHUTDOWN:
1121 return ext4_shutdown(sb, arg);
1127 #ifdef CONFIG_COMPAT
1128 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1130 /* These are just misnamed, they actually get/put from/to user an int */
1132 case EXT4_IOC32_GETFLAGS:
1133 cmd = EXT4_IOC_GETFLAGS;
1135 case EXT4_IOC32_SETFLAGS:
1136 cmd = EXT4_IOC_SETFLAGS;
1138 case EXT4_IOC32_GETVERSION:
1139 cmd = EXT4_IOC_GETVERSION;
1141 case EXT4_IOC32_SETVERSION:
1142 cmd = EXT4_IOC_SETVERSION;
1144 case EXT4_IOC32_GROUP_EXTEND:
1145 cmd = EXT4_IOC_GROUP_EXTEND;
1147 case EXT4_IOC32_GETVERSION_OLD:
1148 cmd = EXT4_IOC_GETVERSION_OLD;
1150 case EXT4_IOC32_SETVERSION_OLD:
1151 cmd = EXT4_IOC_SETVERSION_OLD;
1153 case EXT4_IOC32_GETRSVSZ:
1154 cmd = EXT4_IOC_GETRSVSZ;
1156 case EXT4_IOC32_SETRSVSZ:
1157 cmd = EXT4_IOC_SETRSVSZ;
1159 case EXT4_IOC32_GROUP_ADD: {
1160 struct compat_ext4_new_group_input __user *uinput;
1161 struct ext4_new_group_input input;
1162 mm_segment_t old_fs;
1165 uinput = compat_ptr(arg);
1166 err = get_user(input.group, &uinput->group);
1167 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1168 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1169 err |= get_user(input.inode_table, &uinput->inode_table);
1170 err |= get_user(input.blocks_count, &uinput->blocks_count);
1171 err |= get_user(input.reserved_blocks,
1172 &uinput->reserved_blocks);
1177 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
1178 (unsigned long) &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));