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/uuid.h>
20 #include <linux/uaccess.h>
21 #include <linux/delay.h>
22 #include "ext4_jbd2.h"
24 #include <linux/fsmap.h>
26 #include <trace/events/ext4.h>
29 * Swap memory between @a and @b for @len bytes.
31 * @a: pointer to first memory area
32 * @b: pointer to second memory area
33 * @len: number of bytes to swap
36 static void memswap(void *a, void *b, size_t len)
38 unsigned char *ap, *bp;
40 ap = (unsigned char *)a;
41 bp = (unsigned char *)b;
50 * Swap i_data and associated attributes between @inode1 and @inode2.
51 * This function is used for the primary swap between inode1 and inode2
52 * and also to revert this primary swap in case of errors.
54 * Therefore you have to make sure, that calling this method twice
55 * will revert all changes.
57 * @inode1: pointer to first inode
58 * @inode2: pointer to second inode
60 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
63 struct ext4_inode_info *ei1;
64 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 swap(ei1->i_flags, ei2->i_flags);
78 swap(ei1->i_disksize, ei2->i_disksize);
79 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
80 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
82 isize = i_size_read(inode1);
83 i_size_write(inode1, i_size_read(inode2));
84 i_size_write(inode2, isize);
88 * Swap the information from the given @inode and the inode
89 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
90 * important fields of the inodes.
92 * @sb: the super block of the filesystem
93 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
96 static long swap_inode_boot_loader(struct super_block *sb,
101 struct inode *inode_bl;
102 struct ext4_inode_info *ei_bl;
104 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
107 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
110 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
111 if (IS_ERR(inode_bl))
112 return PTR_ERR(inode_bl);
113 ei_bl = EXT4_I(inode_bl);
115 filemap_flush(inode->i_mapping);
116 filemap_flush(inode_bl->i_mapping);
118 /* Protect orig inodes against a truncate and make sure,
119 * that only 1 swap_inode_boot_loader is running. */
120 lock_two_nondirectories(inode, inode_bl);
122 truncate_inode_pages(&inode->i_data, 0);
123 truncate_inode_pages(&inode_bl->i_data, 0);
125 /* Wait for all existing dio workers */
126 ext4_inode_block_unlocked_dio(inode);
127 ext4_inode_block_unlocked_dio(inode_bl);
128 inode_dio_wait(inode);
129 inode_dio_wait(inode_bl);
131 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
132 if (IS_ERR(handle)) {
134 goto journal_err_out;
137 /* Protect extent tree against block allocations via delalloc */
138 ext4_double_down_write_data_sem(inode, inode_bl);
140 if (inode_bl->i_nlink == 0) {
141 /* this inode has never been used as a BOOT_LOADER */
142 set_nlink(inode_bl, 1);
143 i_uid_write(inode_bl, 0);
144 i_gid_write(inode_bl, 0);
145 inode_bl->i_flags = 0;
147 inode_bl->i_version = 1;
148 i_size_write(inode_bl, 0);
149 inode_bl->i_mode = S_IFREG;
150 if (ext4_has_feature_extents(sb)) {
151 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
152 ext4_ext_tree_init(handle, inode_bl);
154 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
157 swap_inode_data(inode, inode_bl);
159 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
161 inode->i_generation = prandom_u32();
162 inode_bl->i_generation = prandom_u32();
164 ext4_discard_preallocations(inode);
166 err = ext4_mark_inode_dirty(handle, inode);
168 ext4_warning(inode->i_sb,
169 "couldn't mark inode #%lu dirty (err %d)",
171 /* Revert all changes: */
172 swap_inode_data(inode, inode_bl);
174 err = ext4_mark_inode_dirty(handle, inode_bl);
176 ext4_warning(inode_bl->i_sb,
177 "couldn't mark inode #%lu dirty (err %d)",
178 inode_bl->i_ino, err);
179 /* Revert all changes: */
180 swap_inode_data(inode, inode_bl);
181 ext4_mark_inode_dirty(handle, inode);
184 ext4_journal_stop(handle);
185 ext4_double_up_write_data_sem(inode, inode_bl);
188 ext4_inode_resume_unlocked_dio(inode);
189 ext4_inode_resume_unlocked_dio(inode_bl);
190 unlock_two_nondirectories(inode, inode_bl);
195 #ifdef CONFIG_EXT4_FS_ENCRYPTION
196 static int uuid_is_zero(__u8 u[16])
200 for (i = 0; i < 16; i++)
207 static int ext4_ioctl_setflags(struct inode *inode,
210 struct ext4_inode_info *ei = EXT4_I(inode);
211 handle_t *handle = NULL;
212 int err = -EPERM, migrate = 0;
213 struct ext4_iloc iloc;
214 unsigned int oldflags, mask, i;
217 /* Is it quota file? Do not allow user to mess with it */
218 if (ext4_is_quota_file(inode))
221 oldflags = ei->i_flags;
223 /* The JOURNAL_DATA flag is modifiable only by root */
224 jflag = flags & EXT4_JOURNAL_DATA_FL;
227 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
228 * the relevant capability.
230 * This test looks nicer. Thanks to Pauline Middelink
232 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
233 if (!capable(CAP_LINUX_IMMUTABLE))
238 * The JOURNAL_DATA flag can only be changed by
239 * the relevant capability.
241 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
242 if (!capable(CAP_SYS_RESOURCE))
245 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
248 if (flags & EXT4_EOFBLOCKS_FL) {
249 /* we don't support adding EOFBLOCKS flag */
250 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
254 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
255 err = ext4_truncate(inode);
260 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
261 if (IS_ERR(handle)) {
262 err = PTR_ERR(handle);
266 ext4_handle_sync(handle);
267 err = ext4_reserve_inode_write(handle, inode, &iloc);
271 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
272 if (!(mask & EXT4_FL_USER_MODIFIABLE))
274 /* These flags get special treatment later */
275 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
278 ext4_set_inode_flag(inode, i);
280 ext4_clear_inode_flag(inode, i);
283 ext4_set_inode_flags(inode);
284 inode->i_ctime = current_time(inode);
286 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
288 ext4_journal_stop(handle);
292 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
294 * Changes to the journaling mode can cause unsafe changes to
295 * S_DAX if we are using the DAX mount option.
297 if (test_opt(inode->i_sb, DAX)) {
302 err = ext4_change_inode_journal_flag(inode, jflag);
307 if (flags & EXT4_EXTENTS_FL)
308 err = ext4_ext_migrate(inode);
310 err = ext4_ind_migrate(inode);
318 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
320 struct inode *inode = file_inode(filp);
321 struct super_block *sb = inode->i_sb;
322 struct ext4_inode_info *ei = EXT4_I(inode);
326 struct ext4_iloc iloc;
327 struct ext4_inode *raw_inode;
328 struct dquot *transfer_to[MAXQUOTAS] = { };
330 if (!ext4_has_feature_project(sb)) {
331 if (projid != EXT4_DEF_PROJID)
337 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
340 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
342 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
345 err = mnt_want_write_file(filp);
351 /* Is it quota file? Do not allow user to mess with it */
352 if (ext4_is_quota_file(inode))
355 err = ext4_get_inode_loc(inode, &iloc);
359 raw_inode = ext4_raw_inode(&iloc);
360 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
361 err = ext4_expand_extra_isize(inode,
362 EXT4_SB(sb)->s_want_extra_isize,
370 dquot_initialize(inode);
372 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
373 EXT4_QUOTA_INIT_BLOCKS(sb) +
374 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
375 if (IS_ERR(handle)) {
376 err = PTR_ERR(handle);
380 err = ext4_reserve_inode_write(handle, inode, &iloc);
384 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
385 if (!IS_ERR(transfer_to[PRJQUOTA])) {
387 /* __dquot_transfer() calls back ext4_get_inode_usage() which
388 * counts xattr inode references.
390 down_read(&EXT4_I(inode)->xattr_sem);
391 err = __dquot_transfer(inode, transfer_to);
392 up_read(&EXT4_I(inode)->xattr_sem);
393 dqput(transfer_to[PRJQUOTA]);
398 EXT4_I(inode)->i_projid = kprojid;
399 inode->i_ctime = current_time(inode);
401 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
405 ext4_journal_stop(handle);
408 mnt_drop_write_file(filp);
412 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
414 if (projid != EXT4_DEF_PROJID)
420 /* Transfer internal flags to xflags */
421 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
425 if (iflags & EXT4_SYNC_FL)
426 xflags |= FS_XFLAG_SYNC;
427 if (iflags & EXT4_IMMUTABLE_FL)
428 xflags |= FS_XFLAG_IMMUTABLE;
429 if (iflags & EXT4_APPEND_FL)
430 xflags |= FS_XFLAG_APPEND;
431 if (iflags & EXT4_NODUMP_FL)
432 xflags |= FS_XFLAG_NODUMP;
433 if (iflags & EXT4_NOATIME_FL)
434 xflags |= FS_XFLAG_NOATIME;
435 if (iflags & EXT4_PROJINHERIT_FL)
436 xflags |= FS_XFLAG_PROJINHERIT;
440 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
441 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
442 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
444 /* Transfer xflags flags to internal */
445 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
447 unsigned long iflags = 0;
449 if (xflags & FS_XFLAG_SYNC)
450 iflags |= EXT4_SYNC_FL;
451 if (xflags & FS_XFLAG_IMMUTABLE)
452 iflags |= EXT4_IMMUTABLE_FL;
453 if (xflags & FS_XFLAG_APPEND)
454 iflags |= EXT4_APPEND_FL;
455 if (xflags & FS_XFLAG_NODUMP)
456 iflags |= EXT4_NODUMP_FL;
457 if (xflags & FS_XFLAG_NOATIME)
458 iflags |= EXT4_NOATIME_FL;
459 if (xflags & FS_XFLAG_PROJINHERIT)
460 iflags |= EXT4_PROJINHERIT_FL;
465 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
467 struct ext4_sb_info *sbi = EXT4_SB(sb);
470 if (!capable(CAP_SYS_ADMIN))
473 if (get_user(flags, (__u32 __user *)arg))
476 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
479 if (ext4_forced_shutdown(sbi))
482 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
485 case EXT4_GOING_FLAGS_DEFAULT:
486 freeze_bdev(sb->s_bdev);
487 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
488 thaw_bdev(sb->s_bdev, sb);
490 case EXT4_GOING_FLAGS_LOGFLUSH:
491 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
492 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
493 (void) ext4_force_commit(sb);
494 jbd2_journal_abort(sbi->s_journal, 0);
497 case EXT4_GOING_FLAGS_NOLOGFLUSH:
498 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
499 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
501 jbd2_journal_abort(sbi->s_journal, 0);
507 clear_opt(sb, DISCARD);
511 struct getfsmap_info {
512 struct super_block *gi_sb;
513 struct fsmap_head __user *gi_data;
518 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
520 struct getfsmap_info *info = priv;
523 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
525 info->gi_last_flags = xfm->fmr_flags;
526 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
527 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
528 sizeof(struct fsmap)))
534 static int ext4_ioc_getfsmap(struct super_block *sb,
535 struct fsmap_head __user *arg)
537 struct getfsmap_info info = {0};
538 struct ext4_fsmap_head xhead = {0};
539 struct fsmap_head head;
540 bool aborted = false;
543 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
545 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
546 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
547 sizeof(head.fmh_keys[0].fmr_reserved)) ||
548 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
549 sizeof(head.fmh_keys[1].fmr_reserved)))
552 * ext4 doesn't report file extents at all, so the only valid
553 * file offsets are the magic ones (all zeroes or all ones).
555 if (head.fmh_keys[0].fmr_offset ||
556 (head.fmh_keys[1].fmr_offset != 0 &&
557 head.fmh_keys[1].fmr_offset != -1ULL))
560 xhead.fmh_iflags = head.fmh_iflags;
561 xhead.fmh_count = head.fmh_count;
562 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
563 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
565 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
566 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
570 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
571 if (error == EXT4_QUERY_RANGE_ABORT) {
577 /* If we didn't abort, set the "last" flag in the last fmx */
578 if (!aborted && info.gi_idx) {
579 info.gi_last_flags |= FMR_OF_LAST;
580 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
582 sizeof(info.gi_last_flags)))
586 /* copy back header */
587 head.fmh_entries = xhead.fmh_entries;
588 head.fmh_oflags = xhead.fmh_oflags;
589 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
595 static long ext4_ioctl_group_add(struct file *file,
596 struct ext4_new_group_data *input)
598 struct super_block *sb = file_inode(file)->i_sb;
601 err = ext4_resize_begin(sb);
605 if (ext4_has_feature_bigalloc(sb)) {
606 ext4_msg(sb, KERN_ERR,
607 "Online resizing not supported with bigalloc");
612 err = mnt_want_write_file(file);
616 err = ext4_group_add(sb, input);
617 if (EXT4_SB(sb)->s_journal) {
618 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
619 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
620 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
624 mnt_drop_write_file(file);
625 if (!err && ext4_has_group_desc_csum(sb) &&
626 test_opt(sb, INIT_INODE_TABLE))
627 err = ext4_register_li_request(sb, input->group);
633 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
635 struct inode *inode = file_inode(filp);
636 struct super_block *sb = inode->i_sb;
637 struct ext4_inode_info *ei = EXT4_I(inode);
640 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
643 case FS_IOC_GETFSMAP:
644 return ext4_ioc_getfsmap(sb, (void __user *)arg);
645 case EXT4_IOC_GETFLAGS:
646 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
647 return put_user(flags, (int __user *) arg);
648 case EXT4_IOC_SETFLAGS: {
651 if (!inode_owner_or_capable(inode))
654 if (get_user(flags, (int __user *) arg))
657 if (flags & ~EXT4_FL_USER_VISIBLE)
660 * chattr(1) grabs flags via GETFLAGS, modifies the result and
661 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
662 * more restrictive than just silently masking off visible but
663 * not settable flags as we always did.
665 flags &= EXT4_FL_USER_MODIFIABLE;
666 if (ext4_mask_flags(inode->i_mode, flags) != flags)
669 err = mnt_want_write_file(filp);
674 err = ext4_ioctl_setflags(inode, flags);
676 mnt_drop_write_file(filp);
679 case EXT4_IOC_GETVERSION:
680 case EXT4_IOC_GETVERSION_OLD:
681 return put_user(inode->i_generation, (int __user *) arg);
682 case EXT4_IOC_SETVERSION:
683 case EXT4_IOC_SETVERSION_OLD: {
685 struct ext4_iloc iloc;
689 if (!inode_owner_or_capable(inode))
692 if (ext4_has_metadata_csum(inode->i_sb)) {
693 ext4_warning(sb, "Setting inode version is not "
694 "supported with metadata_csum enabled.");
698 err = mnt_want_write_file(filp);
701 if (get_user(generation, (int __user *) arg)) {
707 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
708 if (IS_ERR(handle)) {
709 err = PTR_ERR(handle);
712 err = ext4_reserve_inode_write(handle, inode, &iloc);
714 inode->i_ctime = current_time(inode);
715 inode->i_generation = generation;
716 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
718 ext4_journal_stop(handle);
723 mnt_drop_write_file(filp);
726 case EXT4_IOC_GROUP_EXTEND: {
727 ext4_fsblk_t n_blocks_count;
730 err = ext4_resize_begin(sb);
734 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
736 goto group_extend_out;
739 if (ext4_has_feature_bigalloc(sb)) {
740 ext4_msg(sb, KERN_ERR,
741 "Online resizing not supported with bigalloc");
743 goto group_extend_out;
746 err = mnt_want_write_file(filp);
748 goto group_extend_out;
750 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
751 if (EXT4_SB(sb)->s_journal) {
752 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
753 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
754 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
758 mnt_drop_write_file(filp);
764 case EXT4_IOC_MOVE_EXT: {
765 struct move_extent me;
769 if (!(filp->f_mode & FMODE_READ) ||
770 !(filp->f_mode & FMODE_WRITE))
773 if (copy_from_user(&me,
774 (struct move_extent __user *)arg, sizeof(me)))
778 donor = fdget(me.donor_fd);
782 if (!(donor.file->f_mode & FMODE_WRITE)) {
787 if (ext4_has_feature_bigalloc(sb)) {
788 ext4_msg(sb, KERN_ERR,
789 "Online defrag not supported with bigalloc");
792 } else if (IS_DAX(inode)) {
793 ext4_msg(sb, KERN_ERR,
794 "Online defrag not supported with DAX");
799 err = mnt_want_write_file(filp);
803 err = ext4_move_extents(filp, donor.file, me.orig_start,
804 me.donor_start, me.len, &me.moved_len);
805 mnt_drop_write_file(filp);
807 if (copy_to_user((struct move_extent __user *)arg,
815 case EXT4_IOC_GROUP_ADD: {
816 struct ext4_new_group_data input;
818 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
822 return ext4_ioctl_group_add(filp, &input);
825 case EXT4_IOC_MIGRATE:
828 if (!inode_owner_or_capable(inode))
831 err = mnt_want_write_file(filp);
835 * inode_mutex prevent write and truncate on the file.
836 * Read still goes through. We take i_data_sem in
837 * ext4_ext_swap_inode_data before we switch the
838 * inode format to prevent read.
841 err = ext4_ext_migrate(inode);
842 inode_unlock((inode));
843 mnt_drop_write_file(filp);
847 case EXT4_IOC_ALLOC_DA_BLKS:
850 if (!inode_owner_or_capable(inode))
853 err = mnt_want_write_file(filp);
856 err = ext4_alloc_da_blocks(inode);
857 mnt_drop_write_file(filp);
861 case EXT4_IOC_SWAP_BOOT:
864 if (!(filp->f_mode & FMODE_WRITE))
866 err = mnt_want_write_file(filp);
869 err = swap_inode_boot_loader(sb, inode);
870 mnt_drop_write_file(filp);
874 case EXT4_IOC_RESIZE_FS: {
875 ext4_fsblk_t n_blocks_count;
876 int err = 0, err2 = 0;
877 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
879 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
884 err = ext4_resize_begin(sb);
888 err = mnt_want_write_file(filp);
892 err = ext4_resize_fs(sb, n_blocks_count);
893 if (EXT4_SB(sb)->s_journal) {
894 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
895 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
896 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
900 mnt_drop_write_file(filp);
901 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
902 ext4_has_group_desc_csum(sb) &&
903 test_opt(sb, INIT_INODE_TABLE))
904 err = ext4_register_li_request(sb, o_group);
913 struct request_queue *q = bdev_get_queue(sb->s_bdev);
914 struct fstrim_range range;
917 if (!capable(CAP_SYS_ADMIN))
920 if (!blk_queue_discard(q))
923 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
927 range.minlen = max((unsigned int)range.minlen,
928 q->limits.discard_granularity);
929 ret = ext4_trim_fs(sb, &range);
933 if (copy_to_user((struct fstrim_range __user *)arg, &range,
939 case EXT4_IOC_PRECACHE_EXTENTS:
940 return ext4_ext_precache(inode);
942 case EXT4_IOC_SET_ENCRYPTION_POLICY:
943 if (!ext4_has_feature_encrypt(sb))
945 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
947 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
948 #ifdef CONFIG_EXT4_FS_ENCRYPTION
950 struct ext4_sb_info *sbi = EXT4_SB(sb);
953 if (!ext4_has_feature_encrypt(sb))
955 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
956 err = mnt_want_write_file(filp);
959 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
960 if (IS_ERR(handle)) {
961 err = PTR_ERR(handle);
962 goto pwsalt_err_exit;
964 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
966 goto pwsalt_err_journal;
967 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
968 err = ext4_handle_dirty_metadata(handle, NULL,
971 err2 = ext4_journal_stop(handle);
975 mnt_drop_write_file(filp);
979 if (copy_to_user((void __user *) arg,
980 sbi->s_es->s_encrypt_pw_salt, 16))
987 case EXT4_IOC_GET_ENCRYPTION_POLICY:
988 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
990 case EXT4_IOC_FSGETXATTR:
994 memset(&fa, 0, sizeof(struct fsxattr));
995 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
997 if (ext4_has_feature_project(inode->i_sb)) {
998 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
999 EXT4_I(inode)->i_projid);
1002 if (copy_to_user((struct fsxattr __user *)arg,
1007 case EXT4_IOC_FSSETXATTR:
1012 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1016 /* Make sure caller has proper permission */
1017 if (!inode_owner_or_capable(inode))
1020 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1023 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1024 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1027 err = mnt_want_write_file(filp);
1032 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1033 (flags & EXT4_FL_XFLAG_VISIBLE);
1034 err = ext4_ioctl_setflags(inode, flags);
1035 inode_unlock(inode);
1036 mnt_drop_write_file(filp);
1040 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1046 case EXT4_IOC_SHUTDOWN:
1047 return ext4_shutdown(sb, arg);
1053 #ifdef CONFIG_COMPAT
1054 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1056 /* These are just misnamed, they actually get/put from/to user an int */
1058 case EXT4_IOC32_GETFLAGS:
1059 cmd = EXT4_IOC_GETFLAGS;
1061 case EXT4_IOC32_SETFLAGS:
1062 cmd = EXT4_IOC_SETFLAGS;
1064 case EXT4_IOC32_GETVERSION:
1065 cmd = EXT4_IOC_GETVERSION;
1067 case EXT4_IOC32_SETVERSION:
1068 cmd = EXT4_IOC_SETVERSION;
1070 case EXT4_IOC32_GROUP_EXTEND:
1071 cmd = EXT4_IOC_GROUP_EXTEND;
1073 case EXT4_IOC32_GETVERSION_OLD:
1074 cmd = EXT4_IOC_GETVERSION_OLD;
1076 case EXT4_IOC32_SETVERSION_OLD:
1077 cmd = EXT4_IOC_SETVERSION_OLD;
1079 case EXT4_IOC32_GETRSVSZ:
1080 cmd = EXT4_IOC_GETRSVSZ;
1082 case EXT4_IOC32_SETRSVSZ:
1083 cmd = EXT4_IOC_SETRSVSZ;
1085 case EXT4_IOC32_GROUP_ADD: {
1086 struct compat_ext4_new_group_input __user *uinput;
1087 struct ext4_new_group_data input;
1090 uinput = compat_ptr(arg);
1091 err = get_user(input.group, &uinput->group);
1092 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1093 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1094 err |= get_user(input.inode_table, &uinput->inode_table);
1095 err |= get_user(input.blocks_count, &uinput->blocks_count);
1096 err |= get_user(input.reserved_blocks,
1097 &uinput->reserved_blocks);
1100 return ext4_ioctl_group_add(file, &input);
1102 case EXT4_IOC_MOVE_EXT:
1103 case EXT4_IOC_RESIZE_FS:
1104 case EXT4_IOC_PRECACHE_EXTENTS:
1105 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1106 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1107 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1108 case EXT4_IOC_SHUTDOWN:
1109 case FS_IOC_GETFSMAP:
1112 return -ENOIOCTLCMD;
1114 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));