1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/ioctl.c
5 * Copyright (C) 1993, 1994, 1995
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 #include <linux/capability.h>
13 #include <linux/time.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/file.h>
17 #include <linux/quotaops.h>
18 #include <linux/random.h>
19 #include <linux/uaccess.h>
20 #include <linux/delay.h>
21 #include <linux/iversion.h>
22 #include <linux/fileattr.h>
23 #include "ext4_jbd2.h"
25 #include <linux/fsmap.h>
27 #include <trace/events/ext4.h>
29 typedef void ext4_update_sb_callback(struct ext4_super_block *es,
33 * Superblock modification callback function for changing file system
36 static void ext4_sb_setlabel(struct ext4_super_block *es, const void *arg)
38 /* Sanity check, this should never happen */
39 BUILD_BUG_ON(sizeof(es->s_volume_name) < EXT4_LABEL_MAX);
41 memcpy(es->s_volume_name, (char *)arg, EXT4_LABEL_MAX);
45 int ext4_update_primary_sb(struct super_block *sb, handle_t *handle,
46 ext4_update_sb_callback func,
50 struct ext4_sb_info *sbi = EXT4_SB(sb);
51 struct buffer_head *bh = sbi->s_sbh;
52 struct ext4_super_block *es = sbi->s_es;
54 trace_ext4_update_sb(sb, bh->b_blocknr, 1);
56 BUFFER_TRACE(bh, "get_write_access");
57 err = ext4_journal_get_write_access(handle, sb,
65 ext4_superblock_csum_set(sb);
68 if (buffer_write_io_error(bh) || !buffer_uptodate(bh)) {
69 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
70 "superblock detected");
71 clear_buffer_write_io_error(bh);
72 set_buffer_uptodate(bh);
75 err = ext4_handle_dirty_metadata(handle, NULL, bh);
78 err = sync_dirty_buffer(bh);
80 ext4_std_error(sb, err);
85 * Update one backup superblock in the group 'grp' using the callback
86 * function 'func' and argument 'arg'. If the handle is NULL the
87 * modification is not journalled.
89 * Returns: 0 when no modification was done (no superblock in the group)
90 * 1 when the modification was successful
93 static int ext4_update_backup_sb(struct super_block *sb,
94 handle_t *handle, ext4_group_t grp,
95 ext4_update_sb_callback func, const void *arg)
98 ext4_fsblk_t sb_block;
99 struct buffer_head *bh;
100 unsigned long offset = 0;
101 struct ext4_super_block *es;
103 if (!ext4_bg_has_super(sb, grp))
107 * For the group 0 there is always 1k padding, so we have
108 * either adjust offset, or sb_block depending on blocksize
111 sb_block = 1 * EXT4_MIN_BLOCK_SIZE;
112 offset = do_div(sb_block, sb->s_blocksize);
114 sb_block = ext4_group_first_block_no(sb, grp);
118 trace_ext4_update_sb(sb, sb_block, handle ? 1 : 0);
120 bh = ext4_sb_bread(sb, sb_block, 0);
125 BUFFER_TRACE(bh, "get_write_access");
126 err = ext4_journal_get_write_access(handle, sb,
133 es = (struct ext4_super_block *) (bh->b_data + offset);
135 if (ext4_has_metadata_csum(sb) &&
136 es->s_checksum != ext4_superblock_csum(sb, es)) {
137 ext4_msg(sb, KERN_ERR, "Invalid checksum for backup "
138 "superblock %llu\n", sb_block);
144 if (ext4_has_metadata_csum(sb))
145 es->s_checksum = ext4_superblock_csum(sb, es);
146 set_buffer_uptodate(bh);
153 err = ext4_handle_dirty_metadata(handle, NULL, bh);
157 BUFFER_TRACE(bh, "marking dirty");
158 mark_buffer_dirty(bh);
160 err = sync_dirty_buffer(bh);
164 ext4_std_error(sb, err);
165 return (err) ? err : 1;
169 * Update primary and backup superblocks using the provided function
170 * func and argument arg.
172 * Only the primary superblock and at most two backup superblock
173 * modifications are journalled; the rest is modified without journal.
174 * This is safe because e2fsck will re-write them if there is a problem,
175 * and we're very unlikely to ever need more than two backups.
178 int ext4_update_superblocks_fn(struct super_block *sb,
179 ext4_update_sb_callback func,
183 ext4_group_t ngroups;
184 unsigned int three = 1;
185 unsigned int five = 5;
186 unsigned int seven = 7;
188 ext4_group_t grp, primary_grp;
189 struct ext4_sb_info *sbi = EXT4_SB(sb);
192 * We can't update superblocks while the online resize is running
194 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
195 &sbi->s_ext4_flags)) {
196 ext4_msg(sb, KERN_ERR, "Can't modify superblock while"
197 "performing online resize");
202 * We're only going to update primary superblock and two
203 * backup superblocks in this transaction.
205 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 3);
206 if (IS_ERR(handle)) {
207 err = PTR_ERR(handle);
211 /* Update primary superblock */
212 err = ext4_update_primary_sb(sb, handle, func, arg);
214 ext4_msg(sb, KERN_ERR, "Failed to update primary "
219 primary_grp = ext4_get_group_number(sb, sbi->s_sbh->b_blocknr);
220 ngroups = ext4_get_groups_count(sb);
223 * Update backup superblocks. We have to start from group 0
224 * because it might not be where the primary superblock is
225 * if the fs is mounted with -o sb=<backup_sb_block>
229 while (grp < ngroups) {
230 /* Skip primary superblock */
231 if (grp == primary_grp)
234 ret = ext4_update_backup_sb(sb, handle, grp, func, arg);
236 /* Ignore bad checksum; try to update next sb */
237 if (ret == -EFSBADCRC)
244 if (handle && i > 1) {
246 * We're only journalling primary superblock and
247 * two backup superblocks; the rest is not
250 err = ext4_journal_stop(handle);
256 grp = ext4_list_backups(sb, &three, &five, &seven);
261 ret = ext4_journal_stop(handle);
266 clear_bit_unlock(EXT4_FLAGS_RESIZING, &sbi->s_ext4_flags);
267 smp_mb__after_atomic();
268 return err ? err : 0;
272 * Swap memory between @a and @b for @len bytes.
274 * @a: pointer to first memory area
275 * @b: pointer to second memory area
276 * @len: number of bytes to swap
279 static void memswap(void *a, void *b, size_t len)
281 unsigned char *ap, *bp;
283 ap = (unsigned char *)a;
284 bp = (unsigned char *)b;
293 * Swap i_data and associated attributes between @inode1 and @inode2.
294 * This function is used for the primary swap between inode1 and inode2
295 * and also to revert this primary swap in case of errors.
297 * Therefore you have to make sure, that calling this method twice
298 * will revert all changes.
300 * @inode1: pointer to first inode
301 * @inode2: pointer to second inode
303 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
306 struct ext4_inode_info *ei1;
307 struct ext4_inode_info *ei2;
310 ei1 = EXT4_I(inode1);
311 ei2 = EXT4_I(inode2);
313 swap(inode1->i_version, inode2->i_version);
314 swap(inode1->i_atime, inode2->i_atime);
315 swap(inode1->i_mtime, inode2->i_mtime);
317 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
318 tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
319 ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
320 (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
321 ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
322 swap(ei1->i_disksize, ei2->i_disksize);
323 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
324 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
326 isize = i_size_read(inode1);
327 i_size_write(inode1, i_size_read(inode2));
328 i_size_write(inode2, isize);
331 void ext4_reset_inode_seed(struct inode *inode)
333 struct ext4_inode_info *ei = EXT4_I(inode);
334 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
335 __le32 inum = cpu_to_le32(inode->i_ino);
336 __le32 gen = cpu_to_le32(inode->i_generation);
339 if (!ext4_has_metadata_csum(inode->i_sb))
342 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
343 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
347 * Swap the information from the given @inode and the inode
348 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
349 * important fields of the inodes.
351 * @sb: the super block of the filesystem
352 * @mnt_userns: user namespace of the mount the inode was found from
353 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
356 static long swap_inode_boot_loader(struct super_block *sb,
357 struct user_namespace *mnt_userns,
362 struct inode *inode_bl;
363 struct ext4_inode_info *ei_bl;
364 qsize_t size, size_bl, diff;
366 unsigned short bytes;
368 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
369 if (IS_ERR(inode_bl))
370 return PTR_ERR(inode_bl);
371 ei_bl = EXT4_I(inode_bl);
373 /* Protect orig inodes against a truncate and make sure,
374 * that only 1 swap_inode_boot_loader is running. */
375 lock_two_nondirectories(inode, inode_bl);
377 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
378 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
379 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
380 ext4_has_inline_data(inode)) {
382 goto journal_err_out;
385 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
386 !inode_owner_or_capable(mnt_userns, inode) ||
387 !capable(CAP_SYS_ADMIN)) {
389 goto journal_err_out;
392 filemap_invalidate_lock(inode->i_mapping);
393 err = filemap_write_and_wait(inode->i_mapping);
397 err = filemap_write_and_wait(inode_bl->i_mapping);
401 /* Wait for all existing dio workers */
402 inode_dio_wait(inode);
403 inode_dio_wait(inode_bl);
405 truncate_inode_pages(&inode->i_data, 0);
406 truncate_inode_pages(&inode_bl->i_data, 0);
408 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
409 if (IS_ERR(handle)) {
413 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_SWAP_BOOT, handle);
415 /* Protect extent tree against block allocations via delalloc */
416 ext4_double_down_write_data_sem(inode, inode_bl);
418 if (inode_bl->i_nlink == 0) {
419 /* this inode has never been used as a BOOT_LOADER */
420 set_nlink(inode_bl, 1);
421 i_uid_write(inode_bl, 0);
422 i_gid_write(inode_bl, 0);
423 inode_bl->i_flags = 0;
425 inode_set_iversion(inode_bl, 1);
426 i_size_write(inode_bl, 0);
427 inode_bl->i_mode = S_IFREG;
428 if (ext4_has_feature_extents(sb)) {
429 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
430 ext4_ext_tree_init(handle, inode_bl);
432 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
435 err = dquot_initialize(inode);
439 size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
440 size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
441 diff = size - size_bl;
442 swap_inode_data(inode, inode_bl);
444 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
446 inode->i_generation = prandom_u32();
447 inode_bl->i_generation = prandom_u32();
448 ext4_reset_inode_seed(inode);
449 ext4_reset_inode_seed(inode_bl);
451 ext4_discard_preallocations(inode, 0);
453 err = ext4_mark_inode_dirty(handle, inode);
455 /* No need to update quota information. */
456 ext4_warning(inode->i_sb,
457 "couldn't mark inode #%lu dirty (err %d)",
459 /* Revert all changes: */
460 swap_inode_data(inode, inode_bl);
461 ext4_mark_inode_dirty(handle, inode);
465 blocks = inode_bl->i_blocks;
466 bytes = inode_bl->i_bytes;
467 inode_bl->i_blocks = inode->i_blocks;
468 inode_bl->i_bytes = inode->i_bytes;
469 err = ext4_mark_inode_dirty(handle, inode_bl);
471 /* No need to update quota information. */
472 ext4_warning(inode_bl->i_sb,
473 "couldn't mark inode #%lu dirty (err %d)",
474 inode_bl->i_ino, err);
478 /* Bootloader inode should not be counted into quota information. */
480 dquot_free_space(inode, diff);
482 err = dquot_alloc_space(inode, -1 * diff);
486 /* Revert all changes: */
487 inode_bl->i_blocks = blocks;
488 inode_bl->i_bytes = bytes;
489 swap_inode_data(inode, inode_bl);
490 ext4_mark_inode_dirty(handle, inode);
491 ext4_mark_inode_dirty(handle, inode_bl);
495 ext4_journal_stop(handle);
496 ext4_double_up_write_data_sem(inode, inode_bl);
499 filemap_invalidate_unlock(inode->i_mapping);
501 unlock_two_nondirectories(inode, inode_bl);
507 * If immutable is set and we are not clearing it, we're not allowed to change
508 * anything else in the inode. Don't error out if we're only trying to set
509 * immutable on an immutable file.
511 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
514 struct ext4_inode_info *ei = EXT4_I(inode);
515 unsigned int oldflags = ei->i_flags;
517 if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
520 if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
522 if (ext4_has_feature_project(inode->i_sb) &&
523 __kprojid_val(ei->i_projid) != new_projid)
529 static void ext4_dax_dontcache(struct inode *inode, unsigned int flags)
531 struct ext4_inode_info *ei = EXT4_I(inode);
533 if (S_ISDIR(inode->i_mode))
536 if (test_opt2(inode->i_sb, DAX_NEVER) ||
537 test_opt(inode->i_sb, DAX_ALWAYS))
540 if ((ei->i_flags ^ flags) & EXT4_DAX_FL)
541 d_mark_dontcache(inode);
544 static bool dax_compatible(struct inode *inode, unsigned int oldflags,
547 /* Allow the DAX flag to be changed on inline directories */
548 if (S_ISDIR(inode->i_mode)) {
549 flags &= ~EXT4_INLINE_DATA_FL;
550 oldflags &= ~EXT4_INLINE_DATA_FL;
553 if (flags & EXT4_DAX_FL) {
554 if ((oldflags & EXT4_DAX_MUT_EXCL) ||
555 ext4_test_inode_state(inode,
556 EXT4_STATE_VERITY_IN_PROGRESS)) {
561 if ((flags & EXT4_DAX_MUT_EXCL) && (oldflags & EXT4_DAX_FL))
567 static int ext4_ioctl_setflags(struct inode *inode,
570 struct ext4_inode_info *ei = EXT4_I(inode);
571 handle_t *handle = NULL;
572 int err = -EPERM, migrate = 0;
573 struct ext4_iloc iloc;
574 unsigned int oldflags, mask, i;
575 struct super_block *sb = inode->i_sb;
577 /* Is it quota file? Do not allow user to mess with it */
578 if (ext4_is_quota_file(inode))
581 oldflags = ei->i_flags;
583 * The JOURNAL_DATA flag can only be changed by
584 * the relevant capability.
586 if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
587 if (!capable(CAP_SYS_RESOURCE))
591 if (!dax_compatible(inode, oldflags, flags)) {
596 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
599 if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
600 if (!ext4_has_feature_casefold(sb)) {
605 if (!S_ISDIR(inode->i_mode)) {
610 if (!ext4_empty_dir(inode)) {
617 * Wait for all pending directio and then flush all the dirty pages
618 * for this file. The flush marks all the pages readonly, so any
619 * subsequent attempt to write to the file (particularly mmap pages)
620 * will come through the filesystem and fail.
622 if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
623 (flags & EXT4_IMMUTABLE_FL)) {
624 inode_dio_wait(inode);
625 err = filemap_write_and_wait(inode->i_mapping);
630 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
631 if (IS_ERR(handle)) {
632 err = PTR_ERR(handle);
636 ext4_handle_sync(handle);
637 err = ext4_reserve_inode_write(handle, inode, &iloc);
641 ext4_dax_dontcache(inode, flags);
643 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
644 if (!(mask & EXT4_FL_USER_MODIFIABLE))
646 /* These flags get special treatment later */
647 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
650 ext4_set_inode_flag(inode, i);
652 ext4_clear_inode_flag(inode, i);
655 ext4_set_inode_flags(inode, false);
657 inode->i_ctime = current_time(inode);
659 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
661 ext4_journal_stop(handle);
665 if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
667 * Changes to the journaling mode can cause unsafe changes to
668 * S_DAX if the inode is DAX
675 err = ext4_change_inode_journal_flag(inode,
676 flags & EXT4_JOURNAL_DATA_FL);
681 if (flags & EXT4_EXTENTS_FL)
682 err = ext4_ext_migrate(inode);
684 err = ext4_ind_migrate(inode);
692 static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
694 struct super_block *sb = inode->i_sb;
695 struct ext4_inode_info *ei = EXT4_I(inode);
699 struct ext4_iloc iloc;
700 struct ext4_inode *raw_inode;
701 struct dquot *transfer_to[MAXQUOTAS] = { };
703 if (!ext4_has_feature_project(sb)) {
704 if (projid != EXT4_DEF_PROJID)
710 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
713 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
715 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
719 /* Is it quota file? Do not allow user to mess with it */
720 if (ext4_is_quota_file(inode))
723 err = ext4_get_inode_loc(inode, &iloc);
727 raw_inode = ext4_raw_inode(&iloc);
728 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
729 err = ext4_expand_extra_isize(inode,
730 EXT4_SB(sb)->s_want_extra_isize,
738 err = dquot_initialize(inode);
742 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
743 EXT4_QUOTA_INIT_BLOCKS(sb) +
744 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
746 return PTR_ERR(handle);
748 err = ext4_reserve_inode_write(handle, inode, &iloc);
752 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
753 if (!IS_ERR(transfer_to[PRJQUOTA])) {
755 /* __dquot_transfer() calls back ext4_get_inode_usage() which
756 * counts xattr inode references.
758 down_read(&EXT4_I(inode)->xattr_sem);
759 err = __dquot_transfer(inode, transfer_to);
760 up_read(&EXT4_I(inode)->xattr_sem);
761 dqput(transfer_to[PRJQUOTA]);
766 EXT4_I(inode)->i_projid = kprojid;
767 inode->i_ctime = current_time(inode);
769 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
773 ext4_journal_stop(handle);
777 static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
779 if (projid != EXT4_DEF_PROJID)
785 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
787 struct ext4_sb_info *sbi = EXT4_SB(sb);
790 if (!capable(CAP_SYS_ADMIN))
793 if (get_user(flags, (__u32 __user *)arg))
796 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
799 if (ext4_forced_shutdown(sbi))
802 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
803 trace_ext4_shutdown(sb, flags);
806 case EXT4_GOING_FLAGS_DEFAULT:
807 freeze_bdev(sb->s_bdev);
808 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
809 thaw_bdev(sb->s_bdev);
811 case EXT4_GOING_FLAGS_LOGFLUSH:
812 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
813 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
814 (void) ext4_force_commit(sb);
815 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
818 case EXT4_GOING_FLAGS_NOLOGFLUSH:
819 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
820 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
821 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
826 clear_opt(sb, DISCARD);
830 struct getfsmap_info {
831 struct super_block *gi_sb;
832 struct fsmap_head __user *gi_data;
837 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
839 struct getfsmap_info *info = priv;
842 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
844 info->gi_last_flags = xfm->fmr_flags;
845 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
846 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
847 sizeof(struct fsmap)))
853 static int ext4_ioc_getfsmap(struct super_block *sb,
854 struct fsmap_head __user *arg)
856 struct getfsmap_info info = { NULL };
857 struct ext4_fsmap_head xhead = {0};
858 struct fsmap_head head;
859 bool aborted = false;
862 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
864 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
865 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
866 sizeof(head.fmh_keys[0].fmr_reserved)) ||
867 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
868 sizeof(head.fmh_keys[1].fmr_reserved)))
871 * ext4 doesn't report file extents at all, so the only valid
872 * file offsets are the magic ones (all zeroes or all ones).
874 if (head.fmh_keys[0].fmr_offset ||
875 (head.fmh_keys[1].fmr_offset != 0 &&
876 head.fmh_keys[1].fmr_offset != -1ULL))
879 xhead.fmh_iflags = head.fmh_iflags;
880 xhead.fmh_count = head.fmh_count;
881 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
882 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
884 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
885 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
889 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
890 if (error == EXT4_QUERY_RANGE_ABORT)
895 /* If we didn't abort, set the "last" flag in the last fmx */
896 if (!aborted && info.gi_idx) {
897 info.gi_last_flags |= FMR_OF_LAST;
898 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
900 sizeof(info.gi_last_flags)))
904 /* copy back header */
905 head.fmh_entries = xhead.fmh_entries;
906 head.fmh_oflags = xhead.fmh_oflags;
907 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
913 static long ext4_ioctl_group_add(struct file *file,
914 struct ext4_new_group_data *input)
916 struct super_block *sb = file_inode(file)->i_sb;
919 err = ext4_resize_begin(sb);
923 if (ext4_has_feature_bigalloc(sb)) {
924 ext4_msg(sb, KERN_ERR,
925 "Online resizing not supported with bigalloc");
930 err = mnt_want_write_file(file);
934 err = ext4_group_add(sb, input);
935 if (EXT4_SB(sb)->s_journal) {
936 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
937 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
938 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
942 mnt_drop_write_file(file);
943 if (!err && ext4_has_group_desc_csum(sb) &&
944 test_opt(sb, INIT_INODE_TABLE))
945 err = ext4_register_li_request(sb, input->group);
951 int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa)
953 struct inode *inode = d_inode(dentry);
954 struct ext4_inode_info *ei = EXT4_I(inode);
955 u32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
957 if (S_ISREG(inode->i_mode))
958 flags &= ~FS_PROJINHERIT_FL;
960 fileattr_fill_flags(fa, flags);
961 if (ext4_has_feature_project(inode->i_sb))
962 fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
967 int ext4_fileattr_set(struct user_namespace *mnt_userns,
968 struct dentry *dentry, struct fileattr *fa)
970 struct inode *inode = d_inode(dentry);
971 u32 flags = fa->flags;
972 int err = -EOPNOTSUPP;
974 if (flags & ~EXT4_FL_USER_VISIBLE)
978 * chattr(1) grabs flags via GETFLAGS, modifies the result and
979 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
980 * more restrictive than just silently masking off visible but
981 * not settable flags as we always did.
983 flags &= EXT4_FL_USER_MODIFIABLE;
984 if (ext4_mask_flags(inode->i_mode, flags) != flags)
986 err = ext4_ioctl_check_immutable(inode, fa->fsx_projid, flags);
989 err = ext4_ioctl_setflags(inode, flags);
992 err = ext4_ioctl_setproject(inode, fa->fsx_projid);
997 /* So that the fiemap access checks can't overflow on 32 bit machines. */
998 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
1000 static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
1002 struct fiemap fiemap;
1003 struct fiemap __user *ufiemap = (struct fiemap __user *) arg;
1004 struct fiemap_extent_info fieinfo = { 0, };
1005 struct inode *inode = file_inode(filp);
1008 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
1011 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
1014 fieinfo.fi_flags = fiemap.fm_flags;
1015 fieinfo.fi_extents_max = fiemap.fm_extent_count;
1016 fieinfo.fi_extents_start = ufiemap->fm_extents;
1018 error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start,
1020 fiemap.fm_flags = fieinfo.fi_flags;
1021 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
1022 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
1028 static int ext4_ioctl_checkpoint(struct file *filp, unsigned long arg)
1032 unsigned int flush_flags = 0;
1033 struct super_block *sb = file_inode(filp)->i_sb;
1035 if (copy_from_user(&flags, (__u32 __user *)arg,
1039 if (!capable(CAP_SYS_ADMIN))
1042 /* check for invalid bits set */
1043 if ((flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID) ||
1044 ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
1045 (flags & JBD2_JOURNAL_FLUSH_ZEROOUT)))
1048 if (!EXT4_SB(sb)->s_journal)
1051 if (flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID)
1054 if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
1055 !bdev_max_discard_sectors(EXT4_SB(sb)->s_journal->j_dev))
1058 if (flags & EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN)
1061 if (flags & EXT4_IOC_CHECKPOINT_FLAG_DISCARD)
1062 flush_flags |= JBD2_JOURNAL_FLUSH_DISCARD;
1064 if (flags & EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT) {
1065 flush_flags |= JBD2_JOURNAL_FLUSH_ZEROOUT;
1066 pr_info_ratelimited("warning: checkpointing journal with EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT can be slow");
1069 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1070 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, flush_flags);
1071 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1076 static int ext4_ioctl_setlabel(struct file *filp, const char __user *user_label)
1080 char new_label[EXT4_LABEL_MAX + 1];
1081 struct super_block *sb = file_inode(filp)->i_sb;
1083 if (!capable(CAP_SYS_ADMIN))
1087 * Copy the maximum length allowed for ext4 label with one more to
1088 * find the required terminating null byte in order to test the
1089 * label length. The on disk label doesn't need to be null terminated.
1091 if (copy_from_user(new_label, user_label, EXT4_LABEL_MAX + 1))
1094 len = strnlen(new_label, EXT4_LABEL_MAX + 1);
1095 if (len > EXT4_LABEL_MAX)
1099 * Clear the buffer after the new label
1101 memset(new_label + len, 0, EXT4_LABEL_MAX - len);
1103 ret = mnt_want_write_file(filp);
1107 ret = ext4_update_superblocks_fn(sb, ext4_sb_setlabel, new_label);
1109 mnt_drop_write_file(filp);
1113 static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label)
1115 char label[EXT4_LABEL_MAX + 1];
1118 * EXT4_LABEL_MAX must always be smaller than FSLABEL_MAX because
1119 * FSLABEL_MAX must include terminating null byte, while s_volume_name
1122 BUILD_BUG_ON(EXT4_LABEL_MAX >= FSLABEL_MAX);
1124 memset(label, 0, sizeof(label));
1125 lock_buffer(sbi->s_sbh);
1126 strncpy(label, sbi->s_es->s_volume_name, EXT4_LABEL_MAX);
1127 unlock_buffer(sbi->s_sbh);
1129 if (copy_to_user(user_label, label, sizeof(label)))
1134 static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1136 struct inode *inode = file_inode(filp);
1137 struct super_block *sb = inode->i_sb;
1138 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
1140 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
1143 case FS_IOC_GETFSMAP:
1144 return ext4_ioc_getfsmap(sb, (void __user *)arg);
1145 case EXT4_IOC_GETVERSION:
1146 case EXT4_IOC_GETVERSION_OLD:
1147 return put_user(inode->i_generation, (int __user *) arg);
1148 case EXT4_IOC_SETVERSION:
1149 case EXT4_IOC_SETVERSION_OLD: {
1151 struct ext4_iloc iloc;
1155 if (!inode_owner_or_capable(mnt_userns, inode))
1158 if (ext4_has_metadata_csum(inode->i_sb)) {
1159 ext4_warning(sb, "Setting inode version is not "
1160 "supported with metadata_csum enabled.");
1164 err = mnt_want_write_file(filp);
1167 if (get_user(generation, (int __user *) arg)) {
1169 goto setversion_out;
1173 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
1174 if (IS_ERR(handle)) {
1175 err = PTR_ERR(handle);
1178 err = ext4_reserve_inode_write(handle, inode, &iloc);
1180 inode->i_ctime = current_time(inode);
1181 inode->i_generation = generation;
1182 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
1184 ext4_journal_stop(handle);
1187 inode_unlock(inode);
1189 mnt_drop_write_file(filp);
1192 case EXT4_IOC_GROUP_EXTEND: {
1193 ext4_fsblk_t n_blocks_count;
1196 err = ext4_resize_begin(sb);
1200 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
1202 goto group_extend_out;
1205 if (ext4_has_feature_bigalloc(sb)) {
1206 ext4_msg(sb, KERN_ERR,
1207 "Online resizing not supported with bigalloc");
1209 goto group_extend_out;
1212 err = mnt_want_write_file(filp);
1214 goto group_extend_out;
1216 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
1217 if (EXT4_SB(sb)->s_journal) {
1218 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1219 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
1220 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1224 mnt_drop_write_file(filp);
1226 ext4_resize_end(sb);
1230 case EXT4_IOC_MOVE_EXT: {
1231 struct move_extent me;
1235 if (!(filp->f_mode & FMODE_READ) ||
1236 !(filp->f_mode & FMODE_WRITE))
1239 if (copy_from_user(&me,
1240 (struct move_extent __user *)arg, sizeof(me)))
1244 donor = fdget(me.donor_fd);
1248 if (!(donor.file->f_mode & FMODE_WRITE)) {
1253 if (ext4_has_feature_bigalloc(sb)) {
1254 ext4_msg(sb, KERN_ERR,
1255 "Online defrag not supported with bigalloc");
1258 } else if (IS_DAX(inode)) {
1259 ext4_msg(sb, KERN_ERR,
1260 "Online defrag not supported with DAX");
1265 err = mnt_want_write_file(filp);
1269 err = ext4_move_extents(filp, donor.file, me.orig_start,
1270 me.donor_start, me.len, &me.moved_len);
1271 mnt_drop_write_file(filp);
1273 if (copy_to_user((struct move_extent __user *)arg,
1281 case EXT4_IOC_GROUP_ADD: {
1282 struct ext4_new_group_data input;
1284 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
1288 return ext4_ioctl_group_add(filp, &input);
1291 case EXT4_IOC_MIGRATE:
1294 if (!inode_owner_or_capable(mnt_userns, inode))
1297 err = mnt_want_write_file(filp);
1301 * inode_mutex prevent write and truncate on the file.
1302 * Read still goes through. We take i_data_sem in
1303 * ext4_ext_swap_inode_data before we switch the
1304 * inode format to prevent read.
1306 inode_lock((inode));
1307 err = ext4_ext_migrate(inode);
1308 inode_unlock((inode));
1309 mnt_drop_write_file(filp);
1313 case EXT4_IOC_ALLOC_DA_BLKS:
1316 if (!inode_owner_or_capable(mnt_userns, inode))
1319 err = mnt_want_write_file(filp);
1322 err = ext4_alloc_da_blocks(inode);
1323 mnt_drop_write_file(filp);
1327 case EXT4_IOC_SWAP_BOOT:
1330 if (!(filp->f_mode & FMODE_WRITE))
1332 err = mnt_want_write_file(filp);
1335 err = swap_inode_boot_loader(sb, mnt_userns, inode);
1336 mnt_drop_write_file(filp);
1340 case EXT4_IOC_RESIZE_FS: {
1341 ext4_fsblk_t n_blocks_count;
1342 int err = 0, err2 = 0;
1343 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
1345 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
1350 err = ext4_resize_begin(sb);
1354 err = mnt_want_write_file(filp);
1358 err = ext4_resize_fs(sb, n_blocks_count);
1359 if (EXT4_SB(sb)->s_journal) {
1360 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_RESIZE, NULL);
1361 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1362 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
1363 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1367 mnt_drop_write_file(filp);
1368 if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
1369 ext4_has_group_desc_csum(sb) &&
1370 test_opt(sb, INIT_INODE_TABLE))
1371 err = ext4_register_li_request(sb, o_group);
1374 ext4_resize_end(sb);
1380 struct fstrim_range range;
1383 if (!capable(CAP_SYS_ADMIN))
1386 if (!bdev_max_discard_sectors(sb->s_bdev))
1390 * We haven't replayed the journal, so we cannot use our
1391 * block-bitmap-guided storage zapping commands.
1393 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
1396 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1400 ret = ext4_trim_fs(sb, &range);
1404 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1410 case EXT4_IOC_PRECACHE_EXTENTS:
1411 return ext4_ext_precache(inode);
1413 case FS_IOC_SET_ENCRYPTION_POLICY:
1414 if (!ext4_has_feature_encrypt(sb))
1416 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1418 case FS_IOC_GET_ENCRYPTION_PWSALT:
1419 return ext4_ioctl_get_encryption_pwsalt(filp, (void __user *)arg);
1421 case FS_IOC_GET_ENCRYPTION_POLICY:
1422 if (!ext4_has_feature_encrypt(sb))
1424 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1426 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1427 if (!ext4_has_feature_encrypt(sb))
1429 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
1431 case FS_IOC_ADD_ENCRYPTION_KEY:
1432 if (!ext4_has_feature_encrypt(sb))
1434 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
1436 case FS_IOC_REMOVE_ENCRYPTION_KEY:
1437 if (!ext4_has_feature_encrypt(sb))
1439 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
1441 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1442 if (!ext4_has_feature_encrypt(sb))
1444 return fscrypt_ioctl_remove_key_all_users(filp,
1445 (void __user *)arg);
1446 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1447 if (!ext4_has_feature_encrypt(sb))
1449 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
1451 case FS_IOC_GET_ENCRYPTION_NONCE:
1452 if (!ext4_has_feature_encrypt(sb))
1454 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
1456 case EXT4_IOC_CLEAR_ES_CACHE:
1458 if (!inode_owner_or_capable(mnt_userns, inode))
1460 ext4_clear_inode_es(inode);
1464 case EXT4_IOC_GETSTATE:
1468 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED))
1469 state |= EXT4_STATE_FLAG_EXT_PRECACHED;
1470 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
1471 state |= EXT4_STATE_FLAG_NEW;
1472 if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
1473 state |= EXT4_STATE_FLAG_NEWENTRY;
1474 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE))
1475 state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE;
1477 return put_user(state, (__u32 __user *) arg);
1480 case EXT4_IOC_GET_ES_CACHE:
1481 return ext4_ioctl_get_es_cache(filp, arg);
1483 case EXT4_IOC_SHUTDOWN:
1484 return ext4_shutdown(sb, arg);
1486 case FS_IOC_ENABLE_VERITY:
1487 if (!ext4_has_feature_verity(sb))
1489 return fsverity_ioctl_enable(filp, (const void __user *)arg);
1491 case FS_IOC_MEASURE_VERITY:
1492 if (!ext4_has_feature_verity(sb))
1494 return fsverity_ioctl_measure(filp, (void __user *)arg);
1496 case FS_IOC_READ_VERITY_METADATA:
1497 if (!ext4_has_feature_verity(sb))
1499 return fsverity_ioctl_read_metadata(filp,
1500 (const void __user *)arg);
1502 case EXT4_IOC_CHECKPOINT:
1503 return ext4_ioctl_checkpoint(filp, arg);
1505 case FS_IOC_GETFSLABEL:
1506 return ext4_ioctl_getlabel(EXT4_SB(sb), (void __user *)arg);
1508 case FS_IOC_SETFSLABEL:
1509 return ext4_ioctl_setlabel(filp,
1510 (const void __user *)arg);
1517 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1519 return __ext4_ioctl(filp, cmd, arg);
1522 #ifdef CONFIG_COMPAT
1523 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1525 /* These are just misnamed, they actually get/put from/to user an int */
1527 case EXT4_IOC32_GETVERSION:
1528 cmd = EXT4_IOC_GETVERSION;
1530 case EXT4_IOC32_SETVERSION:
1531 cmd = EXT4_IOC_SETVERSION;
1533 case EXT4_IOC32_GROUP_EXTEND:
1534 cmd = EXT4_IOC_GROUP_EXTEND;
1536 case EXT4_IOC32_GETVERSION_OLD:
1537 cmd = EXT4_IOC_GETVERSION_OLD;
1539 case EXT4_IOC32_SETVERSION_OLD:
1540 cmd = EXT4_IOC_SETVERSION_OLD;
1542 case EXT4_IOC32_GETRSVSZ:
1543 cmd = EXT4_IOC_GETRSVSZ;
1545 case EXT4_IOC32_SETRSVSZ:
1546 cmd = EXT4_IOC_SETRSVSZ;
1548 case EXT4_IOC32_GROUP_ADD: {
1549 struct compat_ext4_new_group_input __user *uinput;
1550 struct ext4_new_group_data input;
1553 uinput = compat_ptr(arg);
1554 err = get_user(input.group, &uinput->group);
1555 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1556 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1557 err |= get_user(input.inode_table, &uinput->inode_table);
1558 err |= get_user(input.blocks_count, &uinput->blocks_count);
1559 err |= get_user(input.reserved_blocks,
1560 &uinput->reserved_blocks);
1563 return ext4_ioctl_group_add(file, &input);
1565 case EXT4_IOC_MOVE_EXT:
1566 case EXT4_IOC_RESIZE_FS:
1568 case EXT4_IOC_PRECACHE_EXTENTS:
1569 case FS_IOC_SET_ENCRYPTION_POLICY:
1570 case FS_IOC_GET_ENCRYPTION_PWSALT:
1571 case FS_IOC_GET_ENCRYPTION_POLICY:
1572 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1573 case FS_IOC_ADD_ENCRYPTION_KEY:
1574 case FS_IOC_REMOVE_ENCRYPTION_KEY:
1575 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1576 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1577 case FS_IOC_GET_ENCRYPTION_NONCE:
1578 case EXT4_IOC_SHUTDOWN:
1579 case FS_IOC_GETFSMAP:
1580 case FS_IOC_ENABLE_VERITY:
1581 case FS_IOC_MEASURE_VERITY:
1582 case FS_IOC_READ_VERITY_METADATA:
1583 case EXT4_IOC_CLEAR_ES_CACHE:
1584 case EXT4_IOC_GETSTATE:
1585 case EXT4_IOC_GET_ES_CACHE:
1586 case EXT4_IOC_CHECKPOINT:
1587 case FS_IOC_GETFSLABEL:
1588 case FS_IOC_SETFSLABEL:
1591 return -ENOIOCTLCMD;
1593 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1597 static void set_overhead(struct ext4_super_block *es, const void *arg)
1599 es->s_overhead_clusters = cpu_to_le32(*((unsigned long *) arg));
1602 int ext4_update_overhead(struct super_block *sb)
1604 struct ext4_sb_info *sbi = EXT4_SB(sb);
1606 if (sb_rdonly(sb) || sbi->s_overhead == 0 ||
1607 sbi->s_overhead == le32_to_cpu(sbi->s_es->s_overhead_clusters))
1610 return ext4_update_superblocks_fn(sb, set_overhead, &sbi->s_overhead);