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;
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,
125 EXT4_IGET_SPECIAL | EXT4_IGET_BAD);
126 if (IS_ERR(inode_bl))
127 return PTR_ERR(inode_bl);
128 ei_bl = EXT4_I(inode_bl);
130 /* Protect orig inodes against a truncate and make sure,
131 * that only 1 swap_inode_boot_loader is running. */
132 lock_two_nondirectories(inode, inode_bl);
134 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
135 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
136 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
137 ext4_has_inline_data(inode)) {
139 goto journal_err_out;
142 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
143 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
145 goto journal_err_out;
148 down_write(&EXT4_I(inode)->i_mmap_sem);
149 err = filemap_write_and_wait(inode->i_mapping);
153 err = filemap_write_and_wait(inode_bl->i_mapping);
157 /* Wait for all existing dio workers */
158 inode_dio_wait(inode);
159 inode_dio_wait(inode_bl);
161 truncate_inode_pages(&inode->i_data, 0);
162 truncate_inode_pages(&inode_bl->i_data, 0);
164 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
165 if (IS_ERR(handle)) {
170 /* Protect extent tree against block allocations via delalloc */
171 ext4_double_down_write_data_sem(inode, inode_bl);
173 if (is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)) {
174 /* this inode has never been used as a BOOT_LOADER */
175 set_nlink(inode_bl, 1);
176 i_uid_write(inode_bl, 0);
177 i_gid_write(inode_bl, 0);
178 inode_bl->i_flags = 0;
180 inode_set_iversion(inode_bl, 1);
181 i_size_write(inode_bl, 0);
182 EXT4_I(inode_bl)->i_disksize = inode_bl->i_size;
183 inode_bl->i_mode = S_IFREG;
184 if (ext4_has_feature_extents(sb)) {
185 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
186 ext4_ext_tree_init(handle, inode_bl);
188 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
191 err = dquot_initialize(inode);
195 size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
196 size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
197 diff = size - size_bl;
198 swap_inode_data(inode, inode_bl);
200 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
202 inode->i_generation = prandom_u32();
203 inode_bl->i_generation = prandom_u32();
204 reset_inode_seed(inode);
205 reset_inode_seed(inode_bl);
207 ext4_discard_preallocations(inode);
209 err = ext4_mark_inode_dirty(handle, inode);
211 /* No need to update quota information. */
212 ext4_warning(inode->i_sb,
213 "couldn't mark inode #%lu dirty (err %d)",
215 /* Revert all changes: */
216 swap_inode_data(inode, inode_bl);
217 ext4_mark_inode_dirty(handle, inode);
221 blocks = inode_bl->i_blocks;
222 bytes = inode_bl->i_bytes;
223 inode_bl->i_blocks = inode->i_blocks;
224 inode_bl->i_bytes = inode->i_bytes;
225 err = ext4_mark_inode_dirty(handle, inode_bl);
227 /* No need to update quota information. */
228 ext4_warning(inode_bl->i_sb,
229 "couldn't mark inode #%lu dirty (err %d)",
230 inode_bl->i_ino, err);
234 /* Bootloader inode should not be counted into quota information. */
236 dquot_free_space(inode, diff);
238 err = dquot_alloc_space(inode, -1 * diff);
242 /* Revert all changes: */
243 inode_bl->i_blocks = blocks;
244 inode_bl->i_bytes = bytes;
245 swap_inode_data(inode, inode_bl);
246 ext4_mark_inode_dirty(handle, inode);
247 ext4_mark_inode_dirty(handle, inode_bl);
251 ext4_journal_stop(handle);
252 ext4_double_up_write_data_sem(inode, inode_bl);
255 up_write(&EXT4_I(inode)->i_mmap_sem);
257 unlock_two_nondirectories(inode, inode_bl);
262 #ifdef CONFIG_FS_ENCRYPTION
263 static int uuid_is_zero(__u8 u[16])
267 for (i = 0; i < 16; i++)
275 * If immutable is set and we are not clearing it, we're not allowed to change
276 * anything else in the inode. Don't error out if we're only trying to set
277 * immutable on an immutable file.
279 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
282 struct ext4_inode_info *ei = EXT4_I(inode);
283 unsigned int oldflags = ei->i_flags;
285 if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
288 if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
290 if (ext4_has_feature_project(inode->i_sb) &&
291 __kprojid_val(ei->i_projid) != new_projid)
297 static int ext4_ioctl_setflags(struct inode *inode,
300 struct ext4_inode_info *ei = EXT4_I(inode);
301 handle_t *handle = NULL;
302 int err = -EPERM, migrate = 0;
303 struct ext4_iloc iloc;
304 unsigned int oldflags, mask, i;
306 struct super_block *sb = inode->i_sb;
308 /* Is it quota file? Do not allow user to mess with it */
309 if (ext4_is_quota_file(inode))
312 oldflags = ei->i_flags;
314 /* The JOURNAL_DATA flag is modifiable only by root */
315 jflag = flags & EXT4_JOURNAL_DATA_FL;
317 err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
322 * The JOURNAL_DATA flag can only be changed by
323 * the relevant capability.
325 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
326 if (!capable(CAP_SYS_RESOURCE))
329 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
332 if (flags & EXT4_EOFBLOCKS_FL) {
333 /* we don't support adding EOFBLOCKS flag */
334 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
338 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
339 err = ext4_truncate(inode);
344 if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
345 if (!ext4_has_feature_casefold(sb)) {
350 if (!S_ISDIR(inode->i_mode)) {
355 if (!ext4_empty_dir(inode)) {
362 * Wait for all pending directio and then flush all the dirty pages
363 * for this file. The flush marks all the pages readonly, so any
364 * subsequent attempt to write to the file (particularly mmap pages)
365 * will come through the filesystem and fail.
367 if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
368 (flags & EXT4_IMMUTABLE_FL)) {
369 inode_dio_wait(inode);
370 err = filemap_write_and_wait(inode->i_mapping);
375 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
376 if (IS_ERR(handle)) {
377 err = PTR_ERR(handle);
381 ext4_handle_sync(handle);
382 err = ext4_reserve_inode_write(handle, inode, &iloc);
386 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
387 if (!(mask & EXT4_FL_USER_MODIFIABLE))
389 /* These flags get special treatment later */
390 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
393 ext4_set_inode_flag(inode, i);
395 ext4_clear_inode_flag(inode, i);
398 ext4_set_inode_flags(inode);
399 inode->i_ctime = current_time(inode);
401 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
403 ext4_journal_stop(handle);
407 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
409 * Changes to the journaling mode can cause unsafe changes to
410 * S_DAX if we are using the DAX mount option.
412 if (test_opt(inode->i_sb, DAX)) {
417 err = ext4_change_inode_journal_flag(inode, jflag);
422 if (flags & EXT4_EXTENTS_FL)
423 err = ext4_ext_migrate(inode);
425 err = ext4_ind_migrate(inode);
433 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
435 struct inode *inode = file_inode(filp);
436 struct super_block *sb = inode->i_sb;
437 struct ext4_inode_info *ei = EXT4_I(inode);
441 struct ext4_iloc iloc;
442 struct ext4_inode *raw_inode;
443 struct dquot *transfer_to[MAXQUOTAS] = { };
445 if (!ext4_has_feature_project(sb)) {
446 if (projid != EXT4_DEF_PROJID)
452 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
455 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
457 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
461 /* Is it quota file? Do not allow user to mess with it */
462 if (ext4_is_quota_file(inode))
465 err = dquot_initialize(inode);
469 err = ext4_get_inode_loc(inode, &iloc);
473 raw_inode = ext4_raw_inode(&iloc);
474 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
475 err = ext4_expand_extra_isize(inode,
476 EXT4_SB(sb)->s_want_extra_isize,
484 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
485 EXT4_QUOTA_INIT_BLOCKS(sb) +
486 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
488 return PTR_ERR(handle);
490 err = ext4_reserve_inode_write(handle, inode, &iloc);
494 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
495 if (!IS_ERR(transfer_to[PRJQUOTA])) {
497 /* __dquot_transfer() calls back ext4_get_inode_usage() which
498 * counts xattr inode references.
500 down_read(&EXT4_I(inode)->xattr_sem);
501 err = __dquot_transfer(inode, transfer_to);
502 up_read(&EXT4_I(inode)->xattr_sem);
503 dqput(transfer_to[PRJQUOTA]);
508 EXT4_I(inode)->i_projid = kprojid;
509 inode->i_ctime = current_time(inode);
511 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
515 ext4_journal_stop(handle);
519 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
521 if (projid != EXT4_DEF_PROJID)
527 /* Transfer internal flags to xflags */
528 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
532 if (iflags & EXT4_SYNC_FL)
533 xflags |= FS_XFLAG_SYNC;
534 if (iflags & EXT4_IMMUTABLE_FL)
535 xflags |= FS_XFLAG_IMMUTABLE;
536 if (iflags & EXT4_APPEND_FL)
537 xflags |= FS_XFLAG_APPEND;
538 if (iflags & EXT4_NODUMP_FL)
539 xflags |= FS_XFLAG_NODUMP;
540 if (iflags & EXT4_NOATIME_FL)
541 xflags |= FS_XFLAG_NOATIME;
542 if (iflags & EXT4_PROJINHERIT_FL)
543 xflags |= FS_XFLAG_PROJINHERIT;
547 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
548 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
549 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
551 /* Transfer xflags flags to internal */
552 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
554 unsigned long iflags = 0;
556 if (xflags & FS_XFLAG_SYNC)
557 iflags |= EXT4_SYNC_FL;
558 if (xflags & FS_XFLAG_IMMUTABLE)
559 iflags |= EXT4_IMMUTABLE_FL;
560 if (xflags & FS_XFLAG_APPEND)
561 iflags |= EXT4_APPEND_FL;
562 if (xflags & FS_XFLAG_NODUMP)
563 iflags |= EXT4_NODUMP_FL;
564 if (xflags & FS_XFLAG_NOATIME)
565 iflags |= EXT4_NOATIME_FL;
566 if (xflags & FS_XFLAG_PROJINHERIT)
567 iflags |= EXT4_PROJINHERIT_FL;
572 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
574 struct ext4_sb_info *sbi = EXT4_SB(sb);
576 struct super_block *ret;
578 if (!capable(CAP_SYS_ADMIN))
581 if (get_user(flags, (__u32 __user *)arg))
584 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
587 if (ext4_forced_shutdown(sbi))
590 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
591 trace_ext4_shutdown(sb, flags);
594 case EXT4_GOING_FLAGS_DEFAULT:
595 ret = freeze_bdev(sb->s_bdev);
598 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
599 thaw_bdev(sb->s_bdev, sb);
601 case EXT4_GOING_FLAGS_LOGFLUSH:
602 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
603 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
604 (void) ext4_force_commit(sb);
605 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
608 case EXT4_GOING_FLAGS_NOLOGFLUSH:
609 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
610 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
611 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
616 clear_opt(sb, DISCARD);
620 struct getfsmap_info {
621 struct super_block *gi_sb;
622 struct fsmap_head __user *gi_data;
627 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
629 struct getfsmap_info *info = priv;
632 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
634 info->gi_last_flags = xfm->fmr_flags;
635 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
636 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
637 sizeof(struct fsmap)))
643 static int ext4_ioc_getfsmap(struct super_block *sb,
644 struct fsmap_head __user *arg)
646 struct getfsmap_info info = { NULL };
647 struct ext4_fsmap_head xhead = {0};
648 struct fsmap_head head;
649 bool aborted = false;
652 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
654 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
655 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
656 sizeof(head.fmh_keys[0].fmr_reserved)) ||
657 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
658 sizeof(head.fmh_keys[1].fmr_reserved)))
661 * ext4 doesn't report file extents at all, so the only valid
662 * file offsets are the magic ones (all zeroes or all ones).
664 if (head.fmh_keys[0].fmr_offset ||
665 (head.fmh_keys[1].fmr_offset != 0 &&
666 head.fmh_keys[1].fmr_offset != -1ULL))
669 xhead.fmh_iflags = head.fmh_iflags;
670 xhead.fmh_count = head.fmh_count;
671 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
672 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
674 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
675 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
679 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
680 if (error == EXT4_QUERY_RANGE_ABORT) {
686 /* If we didn't abort, set the "last" flag in the last fmx */
687 if (!aborted && info.gi_idx) {
688 info.gi_last_flags |= FMR_OF_LAST;
689 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
691 sizeof(info.gi_last_flags)))
695 /* copy back header */
696 head.fmh_entries = xhead.fmh_entries;
697 head.fmh_oflags = xhead.fmh_oflags;
698 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
704 static long ext4_ioctl_group_add(struct file *file,
705 struct ext4_new_group_data *input)
707 struct super_block *sb = file_inode(file)->i_sb;
710 err = ext4_resize_begin(sb);
714 if (ext4_has_feature_bigalloc(sb)) {
715 ext4_msg(sb, KERN_ERR,
716 "Online resizing not supported with bigalloc");
721 err = mnt_want_write_file(file);
725 err = ext4_group_add(sb, input);
726 if (EXT4_SB(sb)->s_journal) {
727 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
728 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
729 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
733 mnt_drop_write_file(file);
734 if (!err && ext4_has_group_desc_csum(sb) &&
735 test_opt(sb, INIT_INODE_TABLE))
736 err = ext4_register_li_request(sb, input->group);
742 static void ext4_fill_fsxattr(struct inode *inode, struct fsxattr *fa)
744 struct ext4_inode_info *ei = EXT4_I(inode);
746 simple_fill_fsxattr(fa, ext4_iflags_to_xflags(ei->i_flags &
747 EXT4_FL_USER_VISIBLE));
749 if (ext4_has_feature_project(inode->i_sb))
750 fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
753 /* copied from fs/ioctl.c */
754 static int fiemap_check_ranges(struct super_block *sb,
755 u64 start, u64 len, u64 *new_len)
757 u64 maxbytes = (u64) sb->s_maxbytes;
764 if (start > maxbytes)
768 * Shrink request scope to what the fs can actually handle.
770 if (len > maxbytes || (maxbytes - len) < start)
771 *new_len = maxbytes - start;
776 /* So that the fiemap access checks can't overflow on 32 bit machines. */
777 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
779 static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
781 struct fiemap fiemap;
782 struct fiemap __user *ufiemap = (struct fiemap __user *) arg;
783 struct fiemap_extent_info fieinfo = { 0, };
784 struct inode *inode = file_inode(filp);
785 struct super_block *sb = inode->i_sb;
789 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
792 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
795 error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
800 fieinfo.fi_flags = fiemap.fm_flags;
801 fieinfo.fi_extents_max = fiemap.fm_extent_count;
802 fieinfo.fi_extents_start = ufiemap->fm_extents;
804 if (fiemap.fm_extent_count != 0 &&
805 !access_ok(fieinfo.fi_extents_start,
806 fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
809 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
810 filemap_write_and_wait(inode->i_mapping);
812 error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start, len);
813 fiemap.fm_flags = fieinfo.fi_flags;
814 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
815 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
821 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
823 struct inode *inode = file_inode(filp);
824 struct super_block *sb = inode->i_sb;
825 struct ext4_inode_info *ei = EXT4_I(inode);
828 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
831 case FS_IOC_GETFSMAP:
832 return ext4_ioc_getfsmap(sb, (void __user *)arg);
833 case EXT4_IOC_GETFLAGS:
834 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
835 if (S_ISREG(inode->i_mode))
836 flags &= ~EXT4_PROJINHERIT_FL;
837 return put_user(flags, (int __user *) arg);
838 case EXT4_IOC_SETFLAGS: {
841 if (!inode_owner_or_capable(inode))
844 if (get_user(flags, (int __user *) arg))
847 if (flags & ~EXT4_FL_USER_VISIBLE)
850 * chattr(1) grabs flags via GETFLAGS, modifies the result and
851 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
852 * more restrictive than just silently masking off visible but
853 * not settable flags as we always did.
855 flags &= EXT4_FL_USER_MODIFIABLE;
856 if (ext4_mask_flags(inode->i_mode, flags) != flags)
859 err = mnt_want_write_file(filp);
864 err = ext4_ioctl_check_immutable(inode,
865 from_kprojid(&init_user_ns, ei->i_projid),
868 err = ext4_ioctl_setflags(inode, flags);
870 mnt_drop_write_file(filp);
873 case EXT4_IOC_GETVERSION:
874 case EXT4_IOC_GETVERSION_OLD:
875 return put_user(inode->i_generation, (int __user *) arg);
876 case EXT4_IOC_SETVERSION:
877 case EXT4_IOC_SETVERSION_OLD: {
879 struct ext4_iloc iloc;
883 if (!inode_owner_or_capable(inode))
886 if (ext4_has_metadata_csum(inode->i_sb)) {
887 ext4_warning(sb, "Setting inode version is not "
888 "supported with metadata_csum enabled.");
892 err = mnt_want_write_file(filp);
895 if (get_user(generation, (int __user *) arg)) {
901 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
902 if (IS_ERR(handle)) {
903 err = PTR_ERR(handle);
906 err = ext4_reserve_inode_write(handle, inode, &iloc);
908 inode->i_ctime = current_time(inode);
909 inode->i_generation = generation;
910 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
912 ext4_journal_stop(handle);
917 mnt_drop_write_file(filp);
920 case EXT4_IOC_GROUP_EXTEND: {
921 ext4_fsblk_t n_blocks_count;
924 err = ext4_resize_begin(sb);
928 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
930 goto group_extend_out;
933 if (ext4_has_feature_bigalloc(sb)) {
934 ext4_msg(sb, KERN_ERR,
935 "Online resizing not supported with bigalloc");
937 goto group_extend_out;
940 err = mnt_want_write_file(filp);
942 goto group_extend_out;
944 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
945 if (EXT4_SB(sb)->s_journal) {
946 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
947 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
948 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
952 mnt_drop_write_file(filp);
958 case EXT4_IOC_MOVE_EXT: {
959 struct move_extent me;
963 if (!(filp->f_mode & FMODE_READ) ||
964 !(filp->f_mode & FMODE_WRITE))
967 if (copy_from_user(&me,
968 (struct move_extent __user *)arg, sizeof(me)))
972 donor = fdget(me.donor_fd);
976 if (!(donor.file->f_mode & FMODE_WRITE)) {
981 if (ext4_has_feature_bigalloc(sb)) {
982 ext4_msg(sb, KERN_ERR,
983 "Online defrag not supported with bigalloc");
986 } else if (IS_DAX(inode)) {
987 ext4_msg(sb, KERN_ERR,
988 "Online defrag not supported with DAX");
993 err = mnt_want_write_file(filp);
997 err = ext4_move_extents(filp, donor.file, me.orig_start,
998 me.donor_start, me.len, &me.moved_len);
999 mnt_drop_write_file(filp);
1001 if (copy_to_user((struct move_extent __user *)arg,
1009 case EXT4_IOC_GROUP_ADD: {
1010 struct ext4_new_group_data input;
1012 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
1016 return ext4_ioctl_group_add(filp, &input);
1019 case EXT4_IOC_MIGRATE:
1022 if (!inode_owner_or_capable(inode))
1025 err = mnt_want_write_file(filp);
1029 * inode_mutex prevent write and truncate on the file.
1030 * Read still goes through. We take i_data_sem in
1031 * ext4_ext_swap_inode_data before we switch the
1032 * inode format to prevent read.
1034 inode_lock((inode));
1035 err = ext4_ext_migrate(inode);
1036 inode_unlock((inode));
1037 mnt_drop_write_file(filp);
1041 case EXT4_IOC_ALLOC_DA_BLKS:
1044 if (!inode_owner_or_capable(inode))
1047 err = mnt_want_write_file(filp);
1050 err = ext4_alloc_da_blocks(inode);
1051 mnt_drop_write_file(filp);
1055 case EXT4_IOC_SWAP_BOOT:
1058 if (!(filp->f_mode & FMODE_WRITE))
1060 err = mnt_want_write_file(filp);
1063 err = swap_inode_boot_loader(sb, inode);
1064 mnt_drop_write_file(filp);
1068 case EXT4_IOC_RESIZE_FS: {
1069 ext4_fsblk_t n_blocks_count;
1070 int err = 0, err2 = 0;
1071 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
1073 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
1078 err = ext4_resize_begin(sb);
1082 err = mnt_want_write_file(filp);
1086 err = ext4_resize_fs(sb, n_blocks_count);
1087 if (EXT4_SB(sb)->s_journal) {
1088 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1089 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
1090 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1094 mnt_drop_write_file(filp);
1095 if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
1096 ext4_has_group_desc_csum(sb) &&
1097 test_opt(sb, INIT_INODE_TABLE))
1098 err = ext4_register_li_request(sb, o_group);
1101 ext4_resize_end(sb);
1107 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1108 struct fstrim_range range;
1111 if (!capable(CAP_SYS_ADMIN))
1114 if (!blk_queue_discard(q))
1118 * We haven't replayed the journal, so we cannot use our
1119 * block-bitmap-guided storage zapping commands.
1121 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
1124 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1128 ret = ext4_trim_fs(sb, &range);
1132 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1138 case EXT4_IOC_PRECACHE_EXTENTS:
1139 return ext4_ext_precache(inode);
1141 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1142 if (!ext4_has_feature_encrypt(sb))
1144 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1146 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
1147 #ifdef CONFIG_FS_ENCRYPTION
1149 struct ext4_sb_info *sbi = EXT4_SB(sb);
1152 if (!ext4_has_feature_encrypt(sb))
1154 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1155 err = mnt_want_write_file(filp);
1158 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1159 if (IS_ERR(handle)) {
1160 err = PTR_ERR(handle);
1161 goto pwsalt_err_exit;
1163 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1165 goto pwsalt_err_journal;
1166 lock_buffer(sbi->s_sbh);
1167 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1168 ext4_superblock_csum_set(sb);
1169 unlock_buffer(sbi->s_sbh);
1170 err = ext4_handle_dirty_metadata(handle, NULL,
1173 err2 = ext4_journal_stop(handle);
1177 mnt_drop_write_file(filp);
1181 if (copy_to_user((void __user *) arg,
1182 sbi->s_es->s_encrypt_pw_salt, 16))
1189 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1190 if (!ext4_has_feature_encrypt(sb))
1192 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1194 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1195 if (!ext4_has_feature_encrypt(sb))
1197 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
1199 case FS_IOC_ADD_ENCRYPTION_KEY:
1200 if (!ext4_has_feature_encrypt(sb))
1202 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
1204 case FS_IOC_REMOVE_ENCRYPTION_KEY:
1205 if (!ext4_has_feature_encrypt(sb))
1207 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
1209 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1210 if (!ext4_has_feature_encrypt(sb))
1212 return fscrypt_ioctl_remove_key_all_users(filp,
1213 (void __user *)arg);
1214 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1215 if (!ext4_has_feature_encrypt(sb))
1217 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
1219 case EXT4_IOC_CLEAR_ES_CACHE:
1221 if (!inode_owner_or_capable(inode))
1223 ext4_clear_inode_es(inode);
1227 case EXT4_IOC_GETSTATE:
1231 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED))
1232 state |= EXT4_STATE_FLAG_EXT_PRECACHED;
1233 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
1234 state |= EXT4_STATE_FLAG_NEW;
1235 if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
1236 state |= EXT4_STATE_FLAG_NEWENTRY;
1237 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE))
1238 state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE;
1240 return put_user(state, (__u32 __user *) arg);
1243 case EXT4_IOC_GET_ES_CACHE:
1244 return ext4_ioctl_get_es_cache(filp, arg);
1246 case EXT4_IOC_FSGETXATTR:
1250 ext4_fill_fsxattr(inode, &fa);
1252 if (copy_to_user((struct fsxattr __user *)arg,
1257 case EXT4_IOC_FSSETXATTR:
1259 struct fsxattr fa, old_fa;
1262 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1266 /* Make sure caller has proper permission */
1267 if (!inode_owner_or_capable(inode))
1270 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1273 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1274 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1277 err = mnt_want_write_file(filp);
1282 ext4_fill_fsxattr(inode, &old_fa);
1283 err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
1286 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1287 (flags & EXT4_FL_XFLAG_VISIBLE);
1288 err = ext4_ioctl_check_immutable(inode, fa.fsx_projid, flags);
1291 err = ext4_ioctl_setflags(inode, flags);
1294 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1296 inode_unlock(inode);
1297 mnt_drop_write_file(filp);
1300 case EXT4_IOC_SHUTDOWN:
1301 return ext4_shutdown(sb, arg);
1303 case FS_IOC_ENABLE_VERITY:
1304 if (!ext4_has_feature_verity(sb))
1306 return fsverity_ioctl_enable(filp, (const void __user *)arg);
1308 case FS_IOC_MEASURE_VERITY:
1309 if (!ext4_has_feature_verity(sb))
1311 return fsverity_ioctl_measure(filp, (void __user *)arg);
1318 #ifdef CONFIG_COMPAT
1319 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1321 /* These are just misnamed, they actually get/put from/to user an int */
1323 case EXT4_IOC32_GETFLAGS:
1324 cmd = EXT4_IOC_GETFLAGS;
1326 case EXT4_IOC32_SETFLAGS:
1327 cmd = EXT4_IOC_SETFLAGS;
1329 case EXT4_IOC32_GETVERSION:
1330 cmd = EXT4_IOC_GETVERSION;
1332 case EXT4_IOC32_SETVERSION:
1333 cmd = EXT4_IOC_SETVERSION;
1335 case EXT4_IOC32_GROUP_EXTEND:
1336 cmd = EXT4_IOC_GROUP_EXTEND;
1338 case EXT4_IOC32_GETVERSION_OLD:
1339 cmd = EXT4_IOC_GETVERSION_OLD;
1341 case EXT4_IOC32_SETVERSION_OLD:
1342 cmd = EXT4_IOC_SETVERSION_OLD;
1344 case EXT4_IOC32_GETRSVSZ:
1345 cmd = EXT4_IOC_GETRSVSZ;
1347 case EXT4_IOC32_SETRSVSZ:
1348 cmd = EXT4_IOC_SETRSVSZ;
1350 case EXT4_IOC32_GROUP_ADD: {
1351 struct compat_ext4_new_group_input __user *uinput;
1352 struct ext4_new_group_data input;
1355 uinput = compat_ptr(arg);
1356 err = get_user(input.group, &uinput->group);
1357 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1358 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1359 err |= get_user(input.inode_table, &uinput->inode_table);
1360 err |= get_user(input.blocks_count, &uinput->blocks_count);
1361 err |= get_user(input.reserved_blocks,
1362 &uinput->reserved_blocks);
1365 return ext4_ioctl_group_add(file, &input);
1367 case EXT4_IOC_MOVE_EXT:
1368 case EXT4_IOC_RESIZE_FS:
1369 case EXT4_IOC_PRECACHE_EXTENTS:
1370 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1371 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1372 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1373 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1374 case FS_IOC_ADD_ENCRYPTION_KEY:
1375 case FS_IOC_REMOVE_ENCRYPTION_KEY:
1376 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1377 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1378 case EXT4_IOC_SHUTDOWN:
1379 case FS_IOC_GETFSMAP:
1380 case FS_IOC_ENABLE_VERITY:
1381 case FS_IOC_MEASURE_VERITY:
1382 case EXT4_IOC_CLEAR_ES_CACHE:
1383 case EXT4_IOC_GETSTATE:
1384 case EXT4_IOC_GET_ES_CACHE:
1387 return -ENOIOCTLCMD;
1389 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));