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_atime, inode2->i_atime);
72 swap(inode1->i_mtime, inode2->i_mtime);
74 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
75 swap(ei1->i_flags, ei2->i_flags);
76 swap(ei1->i_disksize, ei2->i_disksize);
77 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
78 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
80 isize = i_size_read(inode1);
81 i_size_write(inode1, i_size_read(inode2));
82 i_size_write(inode2, isize);
85 static void reset_inode_seed(struct inode *inode)
87 struct ext4_inode_info *ei = EXT4_I(inode);
88 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
89 __le32 inum = cpu_to_le32(inode->i_ino);
90 __le32 gen = cpu_to_le32(inode->i_generation);
93 if (!ext4_has_metadata_csum(inode->i_sb))
96 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
97 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
101 * Swap the information from the given @inode and the inode
102 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
103 * important fields of the inodes.
105 * @sb: the super block of the filesystem
106 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
109 static long swap_inode_boot_loader(struct super_block *sb,
114 struct inode *inode_bl;
115 struct ext4_inode_info *ei_bl;
116 qsize_t size, size_bl, diff;
118 unsigned short bytes;
120 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
121 if (IS_ERR(inode_bl))
122 return PTR_ERR(inode_bl);
123 ei_bl = EXT4_I(inode_bl);
125 /* Protect orig inodes against a truncate and make sure,
126 * that only 1 swap_inode_boot_loader is running. */
127 lock_two_nondirectories(inode, inode_bl);
129 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
130 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
131 ext4_has_inline_data(inode)) {
133 goto journal_err_out;
136 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
137 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
139 goto journal_err_out;
142 down_write(&EXT4_I(inode)->i_mmap_sem);
143 err = filemap_write_and_wait(inode->i_mapping);
147 err = filemap_write_and_wait(inode_bl->i_mapping);
151 /* Wait for all existing dio workers */
152 inode_dio_wait(inode);
153 inode_dio_wait(inode_bl);
155 truncate_inode_pages(&inode->i_data, 0);
156 truncate_inode_pages(&inode_bl->i_data, 0);
158 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
159 if (IS_ERR(handle)) {
164 /* Protect extent tree against block allocations via delalloc */
165 ext4_double_down_write_data_sem(inode, inode_bl);
167 if (inode_bl->i_nlink == 0) {
168 /* this inode has never been used as a BOOT_LOADER */
169 set_nlink(inode_bl, 1);
170 i_uid_write(inode_bl, 0);
171 i_gid_write(inode_bl, 0);
172 inode_bl->i_flags = 0;
174 inode_set_iversion(inode_bl, 1);
175 i_size_write(inode_bl, 0);
176 inode_bl->i_mode = S_IFREG;
177 if (ext4_has_feature_extents(sb)) {
178 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
179 ext4_ext_tree_init(handle, inode_bl);
181 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
184 err = dquot_initialize(inode);
188 size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
189 size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
190 diff = size - size_bl;
191 swap_inode_data(inode, inode_bl);
193 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
195 inode->i_generation = prandom_u32();
196 inode_bl->i_generation = prandom_u32();
197 reset_inode_seed(inode);
198 reset_inode_seed(inode_bl);
200 ext4_discard_preallocations(inode);
202 err = ext4_mark_inode_dirty(handle, inode);
204 /* No need to update quota information. */
205 ext4_warning(inode->i_sb,
206 "couldn't mark inode #%lu dirty (err %d)",
208 /* Revert all changes: */
209 swap_inode_data(inode, inode_bl);
210 ext4_mark_inode_dirty(handle, inode);
214 blocks = inode_bl->i_blocks;
215 bytes = inode_bl->i_bytes;
216 inode_bl->i_blocks = inode->i_blocks;
217 inode_bl->i_bytes = inode->i_bytes;
218 err = ext4_mark_inode_dirty(handle, inode_bl);
220 /* No need to update quota information. */
221 ext4_warning(inode_bl->i_sb,
222 "couldn't mark inode #%lu dirty (err %d)",
223 inode_bl->i_ino, err);
227 /* Bootloader inode should not be counted into quota information. */
229 dquot_free_space(inode, diff);
231 err = dquot_alloc_space(inode, -1 * diff);
235 /* Revert all changes: */
236 inode_bl->i_blocks = blocks;
237 inode_bl->i_bytes = bytes;
238 swap_inode_data(inode, inode_bl);
239 ext4_mark_inode_dirty(handle, inode);
240 ext4_mark_inode_dirty(handle, inode_bl);
244 ext4_journal_stop(handle);
245 ext4_double_up_write_data_sem(inode, inode_bl);
248 up_write(&EXT4_I(inode)->i_mmap_sem);
250 unlock_two_nondirectories(inode, inode_bl);
255 #ifdef CONFIG_EXT4_FS_ENCRYPTION
256 static int uuid_is_zero(__u8 u[16])
260 for (i = 0; i < 16; i++)
267 static int ext4_ioctl_setflags(struct inode *inode,
270 struct ext4_inode_info *ei = EXT4_I(inode);
271 handle_t *handle = NULL;
272 int err = -EPERM, migrate = 0;
273 struct ext4_iloc iloc;
274 unsigned int oldflags, mask, i;
277 /* Is it quota file? Do not allow user to mess with it */
278 if (ext4_is_quota_file(inode))
281 oldflags = ei->i_flags;
283 /* The JOURNAL_DATA flag is modifiable only by root */
284 jflag = flags & EXT4_JOURNAL_DATA_FL;
287 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
288 * the relevant capability.
290 * This test looks nicer. Thanks to Pauline Middelink
292 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
293 if (!capable(CAP_LINUX_IMMUTABLE))
298 * The JOURNAL_DATA flag can only be changed by
299 * the relevant capability.
301 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
302 if (!capable(CAP_SYS_RESOURCE))
305 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
308 if (flags & EXT4_EOFBLOCKS_FL) {
309 /* we don't support adding EOFBLOCKS flag */
310 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
314 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
315 err = ext4_truncate(inode);
320 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
321 if (IS_ERR(handle)) {
322 err = PTR_ERR(handle);
326 ext4_handle_sync(handle);
327 err = ext4_reserve_inode_write(handle, inode, &iloc);
331 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
332 if (!(mask & EXT4_FL_USER_MODIFIABLE))
334 /* These flags get special treatment later */
335 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
338 ext4_set_inode_flag(inode, i);
340 ext4_clear_inode_flag(inode, i);
343 ext4_set_inode_flags(inode);
344 inode->i_ctime = current_time(inode);
346 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
348 ext4_journal_stop(handle);
352 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
354 * Changes to the journaling mode can cause unsafe changes to
355 * S_DAX if we are using the DAX mount option.
357 if (test_opt(inode->i_sb, DAX)) {
362 err = ext4_change_inode_journal_flag(inode, jflag);
367 if (flags & EXT4_EXTENTS_FL)
368 err = ext4_ext_migrate(inode);
370 err = ext4_ind_migrate(inode);
378 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
380 struct inode *inode = file_inode(filp);
381 struct super_block *sb = inode->i_sb;
382 struct ext4_inode_info *ei = EXT4_I(inode);
386 struct ext4_iloc iloc;
387 struct ext4_inode *raw_inode;
388 struct dquot *transfer_to[MAXQUOTAS] = { };
390 if (!ext4_has_feature_project(sb)) {
391 if (projid != EXT4_DEF_PROJID)
397 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
400 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
402 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
406 /* Is it quota file? Do not allow user to mess with it */
407 if (ext4_is_quota_file(inode))
410 err = ext4_get_inode_loc(inode, &iloc);
414 raw_inode = ext4_raw_inode(&iloc);
415 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
416 err = ext4_expand_extra_isize(inode,
417 EXT4_SB(sb)->s_want_extra_isize,
425 err = dquot_initialize(inode);
429 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
430 EXT4_QUOTA_INIT_BLOCKS(sb) +
431 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
433 return PTR_ERR(handle);
435 err = ext4_reserve_inode_write(handle, inode, &iloc);
439 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
440 if (!IS_ERR(transfer_to[PRJQUOTA])) {
442 /* __dquot_transfer() calls back ext4_get_inode_usage() which
443 * counts xattr inode references.
445 down_read(&EXT4_I(inode)->xattr_sem);
446 err = __dquot_transfer(inode, transfer_to);
447 up_read(&EXT4_I(inode)->xattr_sem);
448 dqput(transfer_to[PRJQUOTA]);
453 EXT4_I(inode)->i_projid = kprojid;
454 inode->i_ctime = current_time(inode);
456 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
460 ext4_journal_stop(handle);
464 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
466 if (projid != EXT4_DEF_PROJID)
472 /* Transfer internal flags to xflags */
473 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
477 if (iflags & EXT4_SYNC_FL)
478 xflags |= FS_XFLAG_SYNC;
479 if (iflags & EXT4_IMMUTABLE_FL)
480 xflags |= FS_XFLAG_IMMUTABLE;
481 if (iflags & EXT4_APPEND_FL)
482 xflags |= FS_XFLAG_APPEND;
483 if (iflags & EXT4_NODUMP_FL)
484 xflags |= FS_XFLAG_NODUMP;
485 if (iflags & EXT4_NOATIME_FL)
486 xflags |= FS_XFLAG_NOATIME;
487 if (iflags & EXT4_PROJINHERIT_FL)
488 xflags |= FS_XFLAG_PROJINHERIT;
492 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
493 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
494 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
496 /* Transfer xflags flags to internal */
497 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
499 unsigned long iflags = 0;
501 if (xflags & FS_XFLAG_SYNC)
502 iflags |= EXT4_SYNC_FL;
503 if (xflags & FS_XFLAG_IMMUTABLE)
504 iflags |= EXT4_IMMUTABLE_FL;
505 if (xflags & FS_XFLAG_APPEND)
506 iflags |= EXT4_APPEND_FL;
507 if (xflags & FS_XFLAG_NODUMP)
508 iflags |= EXT4_NODUMP_FL;
509 if (xflags & FS_XFLAG_NOATIME)
510 iflags |= EXT4_NOATIME_FL;
511 if (xflags & FS_XFLAG_PROJINHERIT)
512 iflags |= EXT4_PROJINHERIT_FL;
517 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
519 struct ext4_sb_info *sbi = EXT4_SB(sb);
522 if (!capable(CAP_SYS_ADMIN))
525 if (get_user(flags, (__u32 __user *)arg))
528 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
531 if (ext4_forced_shutdown(sbi))
534 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
535 trace_ext4_shutdown(sb, flags);
538 case EXT4_GOING_FLAGS_DEFAULT:
539 freeze_bdev(sb->s_bdev);
540 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
541 thaw_bdev(sb->s_bdev, sb);
543 case EXT4_GOING_FLAGS_LOGFLUSH:
544 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
545 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
546 (void) ext4_force_commit(sb);
547 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
550 case EXT4_GOING_FLAGS_NOLOGFLUSH:
551 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
552 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
553 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
558 clear_opt(sb, DISCARD);
562 struct getfsmap_info {
563 struct super_block *gi_sb;
564 struct fsmap_head __user *gi_data;
569 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
571 struct getfsmap_info *info = priv;
574 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
576 info->gi_last_flags = xfm->fmr_flags;
577 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
578 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
579 sizeof(struct fsmap)))
585 static int ext4_ioc_getfsmap(struct super_block *sb,
586 struct fsmap_head __user *arg)
588 struct getfsmap_info info = {0};
589 struct ext4_fsmap_head xhead = {0};
590 struct fsmap_head head;
591 bool aborted = false;
594 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
596 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
597 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
598 sizeof(head.fmh_keys[0].fmr_reserved)) ||
599 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
600 sizeof(head.fmh_keys[1].fmr_reserved)))
603 * ext4 doesn't report file extents at all, so the only valid
604 * file offsets are the magic ones (all zeroes or all ones).
606 if (head.fmh_keys[0].fmr_offset ||
607 (head.fmh_keys[1].fmr_offset != 0 &&
608 head.fmh_keys[1].fmr_offset != -1ULL))
611 xhead.fmh_iflags = head.fmh_iflags;
612 xhead.fmh_count = head.fmh_count;
613 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
614 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
616 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
617 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
621 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
622 if (error == EXT4_QUERY_RANGE_ABORT) {
628 /* If we didn't abort, set the "last" flag in the last fmx */
629 if (!aborted && info.gi_idx) {
630 info.gi_last_flags |= FMR_OF_LAST;
631 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
633 sizeof(info.gi_last_flags)))
637 /* copy back header */
638 head.fmh_entries = xhead.fmh_entries;
639 head.fmh_oflags = xhead.fmh_oflags;
640 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
646 static long ext4_ioctl_group_add(struct file *file,
647 struct ext4_new_group_data *input)
649 struct super_block *sb = file_inode(file)->i_sb;
652 err = ext4_resize_begin(sb);
656 if (ext4_has_feature_bigalloc(sb)) {
657 ext4_msg(sb, KERN_ERR,
658 "Online resizing not supported with bigalloc");
663 err = mnt_want_write_file(file);
667 err = ext4_group_add(sb, input);
668 if (EXT4_SB(sb)->s_journal) {
669 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
670 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
671 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
675 mnt_drop_write_file(file);
676 if (!err && ext4_has_group_desc_csum(sb) &&
677 test_opt(sb, INIT_INODE_TABLE))
678 err = ext4_register_li_request(sb, input->group);
684 static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
687 * Project Quota ID state is only allowed to change from within the init
688 * namespace. Enforce that restriction only if we are trying to change
689 * the quota ID state. Everything else is allowed in user namespaces.
691 if (current_user_ns() == &init_user_ns)
694 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
697 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
698 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
701 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
708 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
710 struct inode *inode = file_inode(filp);
711 struct super_block *sb = inode->i_sb;
712 struct ext4_inode_info *ei = EXT4_I(inode);
715 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
718 case FS_IOC_GETFSMAP:
719 return ext4_ioc_getfsmap(sb, (void __user *)arg);
720 case EXT4_IOC_GETFLAGS:
721 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
722 return put_user(flags, (int __user *) arg);
723 case EXT4_IOC_SETFLAGS: {
726 if (!inode_owner_or_capable(inode))
729 if (get_user(flags, (int __user *) arg))
732 if (flags & ~EXT4_FL_USER_VISIBLE)
735 * chattr(1) grabs flags via GETFLAGS, modifies the result and
736 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
737 * more restrictive than just silently masking off visible but
738 * not settable flags as we always did.
740 flags &= EXT4_FL_USER_MODIFIABLE;
741 if (ext4_mask_flags(inode->i_mode, flags) != flags)
744 err = mnt_want_write_file(filp);
749 err = ext4_ioctl_setflags(inode, flags);
751 mnt_drop_write_file(filp);
754 case EXT4_IOC_GETVERSION:
755 case EXT4_IOC_GETVERSION_OLD:
756 return put_user(inode->i_generation, (int __user *) arg);
757 case EXT4_IOC_SETVERSION:
758 case EXT4_IOC_SETVERSION_OLD: {
760 struct ext4_iloc iloc;
764 if (!inode_owner_or_capable(inode))
767 if (ext4_has_metadata_csum(inode->i_sb)) {
768 ext4_warning(sb, "Setting inode version is not "
769 "supported with metadata_csum enabled.");
773 err = mnt_want_write_file(filp);
776 if (get_user(generation, (int __user *) arg)) {
782 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
783 if (IS_ERR(handle)) {
784 err = PTR_ERR(handle);
787 err = ext4_reserve_inode_write(handle, inode, &iloc);
789 inode->i_ctime = current_time(inode);
790 inode->i_generation = generation;
791 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
793 ext4_journal_stop(handle);
798 mnt_drop_write_file(filp);
801 case EXT4_IOC_GROUP_EXTEND: {
802 ext4_fsblk_t n_blocks_count;
805 err = ext4_resize_begin(sb);
809 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
811 goto group_extend_out;
814 if (ext4_has_feature_bigalloc(sb)) {
815 ext4_msg(sb, KERN_ERR,
816 "Online resizing not supported with bigalloc");
818 goto group_extend_out;
821 err = mnt_want_write_file(filp);
823 goto group_extend_out;
825 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
826 if (EXT4_SB(sb)->s_journal) {
827 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
828 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
829 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
833 mnt_drop_write_file(filp);
839 case EXT4_IOC_MOVE_EXT: {
840 struct move_extent me;
844 if (!(filp->f_mode & FMODE_READ) ||
845 !(filp->f_mode & FMODE_WRITE))
848 if (copy_from_user(&me,
849 (struct move_extent __user *)arg, sizeof(me)))
853 donor = fdget(me.donor_fd);
857 if (!(donor.file->f_mode & FMODE_WRITE)) {
862 if (ext4_has_feature_bigalloc(sb)) {
863 ext4_msg(sb, KERN_ERR,
864 "Online defrag not supported with bigalloc");
867 } else if (IS_DAX(inode)) {
868 ext4_msg(sb, KERN_ERR,
869 "Online defrag not supported with DAX");
874 err = mnt_want_write_file(filp);
878 err = ext4_move_extents(filp, donor.file, me.orig_start,
879 me.donor_start, me.len, &me.moved_len);
880 mnt_drop_write_file(filp);
882 if (copy_to_user((struct move_extent __user *)arg,
890 case EXT4_IOC_GROUP_ADD: {
891 struct ext4_new_group_data input;
893 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
897 return ext4_ioctl_group_add(filp, &input);
900 case EXT4_IOC_MIGRATE:
903 if (!inode_owner_or_capable(inode))
906 err = mnt_want_write_file(filp);
910 * inode_mutex prevent write and truncate on the file.
911 * Read still goes through. We take i_data_sem in
912 * ext4_ext_swap_inode_data before we switch the
913 * inode format to prevent read.
916 err = ext4_ext_migrate(inode);
917 inode_unlock((inode));
918 mnt_drop_write_file(filp);
922 case EXT4_IOC_ALLOC_DA_BLKS:
925 if (!inode_owner_or_capable(inode))
928 err = mnt_want_write_file(filp);
931 err = ext4_alloc_da_blocks(inode);
932 mnt_drop_write_file(filp);
936 case EXT4_IOC_SWAP_BOOT:
939 if (!(filp->f_mode & FMODE_WRITE))
941 err = mnt_want_write_file(filp);
944 err = swap_inode_boot_loader(sb, inode);
945 mnt_drop_write_file(filp);
949 case EXT4_IOC_RESIZE_FS: {
950 ext4_fsblk_t n_blocks_count;
951 int err = 0, err2 = 0;
952 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
954 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
959 err = ext4_resize_begin(sb);
963 err = mnt_want_write_file(filp);
967 err = ext4_resize_fs(sb, n_blocks_count);
968 if (EXT4_SB(sb)->s_journal) {
969 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
970 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
971 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
975 mnt_drop_write_file(filp);
976 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
977 ext4_has_group_desc_csum(sb) &&
978 test_opt(sb, INIT_INODE_TABLE))
979 err = ext4_register_li_request(sb, o_group);
988 struct request_queue *q = bdev_get_queue(sb->s_bdev);
989 struct fstrim_range range;
992 if (!capable(CAP_SYS_ADMIN))
995 if (!blk_queue_discard(q))
998 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1002 range.minlen = max((unsigned int)range.minlen,
1003 q->limits.discard_granularity);
1004 ret = ext4_trim_fs(sb, &range);
1008 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1014 case EXT4_IOC_PRECACHE_EXTENTS:
1015 return ext4_ext_precache(inode);
1017 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1018 if (!ext4_has_feature_encrypt(sb))
1020 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1022 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
1023 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1025 struct ext4_sb_info *sbi = EXT4_SB(sb);
1028 if (!ext4_has_feature_encrypt(sb))
1030 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1031 err = mnt_want_write_file(filp);
1034 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1035 if (IS_ERR(handle)) {
1036 err = PTR_ERR(handle);
1037 goto pwsalt_err_exit;
1039 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1041 goto pwsalt_err_journal;
1042 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1043 err = ext4_handle_dirty_metadata(handle, NULL,
1046 err2 = ext4_journal_stop(handle);
1050 mnt_drop_write_file(filp);
1054 if (copy_to_user((void __user *) arg,
1055 sbi->s_es->s_encrypt_pw_salt, 16))
1062 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1063 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1065 case EXT4_IOC_FSGETXATTR:
1069 memset(&fa, 0, sizeof(struct fsxattr));
1070 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1072 if (ext4_has_feature_project(inode->i_sb)) {
1073 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1074 EXT4_I(inode)->i_projid);
1077 if (copy_to_user((struct fsxattr __user *)arg,
1082 case EXT4_IOC_FSSETXATTR:
1087 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1091 /* Make sure caller has proper permission */
1092 if (!inode_owner_or_capable(inode))
1095 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1098 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1099 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1102 err = mnt_want_write_file(filp);
1107 err = ext4_ioctl_check_project(inode, &fa);
1110 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1111 (flags & EXT4_FL_XFLAG_VISIBLE);
1112 err = ext4_ioctl_setflags(inode, flags);
1115 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1117 inode_unlock(inode);
1118 mnt_drop_write_file(filp);
1121 case EXT4_IOC_SHUTDOWN:
1122 return ext4_shutdown(sb, arg);
1128 #ifdef CONFIG_COMPAT
1129 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1131 /* These are just misnamed, they actually get/put from/to user an int */
1133 case EXT4_IOC32_GETFLAGS:
1134 cmd = EXT4_IOC_GETFLAGS;
1136 case EXT4_IOC32_SETFLAGS:
1137 cmd = EXT4_IOC_SETFLAGS;
1139 case EXT4_IOC32_GETVERSION:
1140 cmd = EXT4_IOC_GETVERSION;
1142 case EXT4_IOC32_SETVERSION:
1143 cmd = EXT4_IOC_SETVERSION;
1145 case EXT4_IOC32_GROUP_EXTEND:
1146 cmd = EXT4_IOC_GROUP_EXTEND;
1148 case EXT4_IOC32_GETVERSION_OLD:
1149 cmd = EXT4_IOC_GETVERSION_OLD;
1151 case EXT4_IOC32_SETVERSION_OLD:
1152 cmd = EXT4_IOC_SETVERSION_OLD;
1154 case EXT4_IOC32_GETRSVSZ:
1155 cmd = EXT4_IOC_GETRSVSZ;
1157 case EXT4_IOC32_SETRSVSZ:
1158 cmd = EXT4_IOC_SETRSVSZ;
1160 case EXT4_IOC32_GROUP_ADD: {
1161 struct compat_ext4_new_group_input __user *uinput;
1162 struct ext4_new_group_data input;
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);
1175 return ext4_ioctl_group_add(file, &input);
1177 case EXT4_IOC_MOVE_EXT:
1178 case EXT4_IOC_RESIZE_FS:
1179 case EXT4_IOC_PRECACHE_EXTENTS:
1180 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1181 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1182 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1183 case EXT4_IOC_SHUTDOWN:
1184 case FS_IOC_GETFSMAP:
1187 return -ENOIOCTLCMD;
1189 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));