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 <linux/fileattr.h>
24 #include "ext4_jbd2.h"
26 #include <linux/fsmap.h>
28 #include <trace/events/ext4.h>
31 * Swap memory between @a and @b for @len bytes.
33 * @a: pointer to first memory area
34 * @b: pointer to second memory area
35 * @len: number of bytes to swap
38 static void memswap(void *a
, void *b
, size_t len
)
40 unsigned char *ap
, *bp
;
42 ap
= (unsigned char *)a
;
43 bp
= (unsigned char *)b
;
52 * Swap i_data and associated attributes between @inode1 and @inode2.
53 * This function is used for the primary swap between inode1 and inode2
54 * and also to revert this primary swap in case of errors.
56 * Therefore you have to make sure, that calling this method twice
57 * will revert all changes.
59 * @inode1: pointer to first inode
60 * @inode2: pointer to second inode
62 static void swap_inode_data(struct inode
*inode1
, struct inode
*inode2
)
65 struct ext4_inode_info
*ei1
;
66 struct ext4_inode_info
*ei2
;
72 swap(inode1
->i_version
, inode2
->i_version
);
73 swap(inode1
->i_atime
, inode2
->i_atime
);
74 swap(inode1
->i_mtime
, inode2
->i_mtime
);
76 memswap(ei1
->i_data
, ei2
->i_data
, sizeof(ei1
->i_data
));
77 tmp
= ei1
->i_flags
& EXT4_FL_SHOULD_SWAP
;
78 ei1
->i_flags
= (ei2
->i_flags
& EXT4_FL_SHOULD_SWAP
) |
79 (ei1
->i_flags
& ~EXT4_FL_SHOULD_SWAP
);
80 ei2
->i_flags
= tmp
| (ei2
->i_flags
& ~EXT4_FL_SHOULD_SWAP
);
81 swap(ei1
->i_disksize
, ei2
->i_disksize
);
82 ext4_es_remove_extent(inode1
, 0, EXT_MAX_BLOCKS
);
83 ext4_es_remove_extent(inode2
, 0, EXT_MAX_BLOCKS
);
85 isize
= i_size_read(inode1
);
86 i_size_write(inode1
, i_size_read(inode2
));
87 i_size_write(inode2
, isize
);
90 void ext4_reset_inode_seed(struct inode
*inode
)
92 struct ext4_inode_info
*ei
= EXT4_I(inode
);
93 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
94 __le32 inum
= cpu_to_le32(inode
->i_ino
);
95 __le32 gen
= cpu_to_le32(inode
->i_generation
);
98 if (!ext4_has_metadata_csum(inode
->i_sb
))
101 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&inum
, sizeof(inum
));
102 ei
->i_csum_seed
= ext4_chksum(sbi
, csum
, (__u8
*)&gen
, sizeof(gen
));
106 * Swap the information from the given @inode and the inode
107 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
108 * important fields of the inodes.
110 * @sb: the super block of the filesystem
111 * @mnt_userns: user namespace of the mount the inode was found from
112 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
115 static long swap_inode_boot_loader(struct super_block
*sb
,
116 struct user_namespace
*mnt_userns
,
121 struct inode
*inode_bl
;
122 struct ext4_inode_info
*ei_bl
;
123 qsize_t size
, size_bl
, diff
;
125 unsigned short bytes
;
127 inode_bl
= ext4_iget(sb
, EXT4_BOOT_LOADER_INO
, EXT4_IGET_SPECIAL
);
128 if (IS_ERR(inode_bl
))
129 return PTR_ERR(inode_bl
);
130 ei_bl
= EXT4_I(inode_bl
);
132 /* Protect orig inodes against a truncate and make sure,
133 * that only 1 swap_inode_boot_loader is running. */
134 lock_two_nondirectories(inode
, inode_bl
);
136 if (inode
->i_nlink
!= 1 || !S_ISREG(inode
->i_mode
) ||
137 IS_SWAPFILE(inode
) || IS_ENCRYPTED(inode
) ||
138 (EXT4_I(inode
)->i_flags
& EXT4_JOURNAL_DATA_FL
) ||
139 ext4_has_inline_data(inode
)) {
141 goto journal_err_out
;
144 if (IS_RDONLY(inode
) || IS_APPEND(inode
) || IS_IMMUTABLE(inode
) ||
145 !inode_owner_or_capable(mnt_userns
, inode
) ||
146 !capable(CAP_SYS_ADMIN
)) {
148 goto journal_err_out
;
151 filemap_invalidate_lock(inode
->i_mapping
);
152 err
= filemap_write_and_wait(inode
->i_mapping
);
156 err
= filemap_write_and_wait(inode_bl
->i_mapping
);
160 /* Wait for all existing dio workers */
161 inode_dio_wait(inode
);
162 inode_dio_wait(inode_bl
);
164 truncate_inode_pages(&inode
->i_data
, 0);
165 truncate_inode_pages(&inode_bl
->i_data
, 0);
167 handle
= ext4_journal_start(inode_bl
, EXT4_HT_MOVE_EXTENTS
, 2);
168 if (IS_ERR(handle
)) {
172 ext4_fc_mark_ineligible(sb
, EXT4_FC_REASON_SWAP_BOOT
, handle
);
174 /* Protect extent tree against block allocations via delalloc */
175 ext4_double_down_write_data_sem(inode
, inode_bl
);
177 if (inode_bl
->i_nlink
== 0) {
178 /* this inode has never been used as a BOOT_LOADER */
179 set_nlink(inode_bl
, 1);
180 i_uid_write(inode_bl
, 0);
181 i_gid_write(inode_bl
, 0);
182 inode_bl
->i_flags
= 0;
184 inode_set_iversion(inode_bl
, 1);
185 i_size_write(inode_bl
, 0);
186 inode_bl
->i_mode
= S_IFREG
;
187 if (ext4_has_feature_extents(sb
)) {
188 ext4_set_inode_flag(inode_bl
, EXT4_INODE_EXTENTS
);
189 ext4_ext_tree_init(handle
, inode_bl
);
191 memset(ei_bl
->i_data
, 0, sizeof(ei_bl
->i_data
));
194 err
= dquot_initialize(inode
);
198 size
= (qsize_t
)(inode
->i_blocks
) * (1 << 9) + inode
->i_bytes
;
199 size_bl
= (qsize_t
)(inode_bl
->i_blocks
) * (1 << 9) + inode_bl
->i_bytes
;
200 diff
= size
- size_bl
;
201 swap_inode_data(inode
, inode_bl
);
203 inode
->i_ctime
= inode_bl
->i_ctime
= current_time(inode
);
205 inode
->i_generation
= prandom_u32();
206 inode_bl
->i_generation
= prandom_u32();
207 ext4_reset_inode_seed(inode
);
208 ext4_reset_inode_seed(inode_bl
);
210 ext4_discard_preallocations(inode
, 0);
212 err
= ext4_mark_inode_dirty(handle
, inode
);
214 /* No need to update quota information. */
215 ext4_warning(inode
->i_sb
,
216 "couldn't mark inode #%lu dirty (err %d)",
218 /* Revert all changes: */
219 swap_inode_data(inode
, inode_bl
);
220 ext4_mark_inode_dirty(handle
, inode
);
224 blocks
= inode_bl
->i_blocks
;
225 bytes
= inode_bl
->i_bytes
;
226 inode_bl
->i_blocks
= inode
->i_blocks
;
227 inode_bl
->i_bytes
= inode
->i_bytes
;
228 err
= ext4_mark_inode_dirty(handle
, inode_bl
);
230 /* No need to update quota information. */
231 ext4_warning(inode_bl
->i_sb
,
232 "couldn't mark inode #%lu dirty (err %d)",
233 inode_bl
->i_ino
, err
);
237 /* Bootloader inode should not be counted into quota information. */
239 dquot_free_space(inode
, diff
);
241 err
= dquot_alloc_space(inode
, -1 * diff
);
245 /* Revert all changes: */
246 inode_bl
->i_blocks
= blocks
;
247 inode_bl
->i_bytes
= bytes
;
248 swap_inode_data(inode
, inode_bl
);
249 ext4_mark_inode_dirty(handle
, inode
);
250 ext4_mark_inode_dirty(handle
, inode_bl
);
254 ext4_journal_stop(handle
);
255 ext4_double_up_write_data_sem(inode
, inode_bl
);
258 filemap_invalidate_unlock(inode
->i_mapping
);
260 unlock_two_nondirectories(inode
, inode_bl
);
265 #ifdef CONFIG_FS_ENCRYPTION
266 static int uuid_is_zero(__u8 u
[16])
270 for (i
= 0; i
< 16; i
++)
278 * If immutable is set and we are not clearing it, we're not allowed to change
279 * anything else in the inode. Don't error out if we're only trying to set
280 * immutable on an immutable file.
282 static int ext4_ioctl_check_immutable(struct inode
*inode
, __u32 new_projid
,
285 struct ext4_inode_info
*ei
= EXT4_I(inode
);
286 unsigned int oldflags
= ei
->i_flags
;
288 if (!(oldflags
& EXT4_IMMUTABLE_FL
) || !(flags
& EXT4_IMMUTABLE_FL
))
291 if ((oldflags
& ~EXT4_IMMUTABLE_FL
) != (flags
& ~EXT4_IMMUTABLE_FL
))
293 if (ext4_has_feature_project(inode
->i_sb
) &&
294 __kprojid_val(ei
->i_projid
) != new_projid
)
300 static void ext4_dax_dontcache(struct inode
*inode
, unsigned int flags
)
302 struct ext4_inode_info
*ei
= EXT4_I(inode
);
304 if (S_ISDIR(inode
->i_mode
))
307 if (test_opt2(inode
->i_sb
, DAX_NEVER
) ||
308 test_opt(inode
->i_sb
, DAX_ALWAYS
))
311 if ((ei
->i_flags
^ flags
) & EXT4_DAX_FL
)
312 d_mark_dontcache(inode
);
315 static bool dax_compatible(struct inode
*inode
, unsigned int oldflags
,
318 /* Allow the DAX flag to be changed on inline directories */
319 if (S_ISDIR(inode
->i_mode
)) {
320 flags
&= ~EXT4_INLINE_DATA_FL
;
321 oldflags
&= ~EXT4_INLINE_DATA_FL
;
324 if (flags
& EXT4_DAX_FL
) {
325 if ((oldflags
& EXT4_DAX_MUT_EXCL
) ||
326 ext4_test_inode_state(inode
,
327 EXT4_STATE_VERITY_IN_PROGRESS
)) {
332 if ((flags
& EXT4_DAX_MUT_EXCL
) && (oldflags
& EXT4_DAX_FL
))
338 static int ext4_ioctl_setflags(struct inode
*inode
,
341 struct ext4_inode_info
*ei
= EXT4_I(inode
);
342 handle_t
*handle
= NULL
;
343 int err
= -EPERM
, migrate
= 0;
344 struct ext4_iloc iloc
;
345 unsigned int oldflags
, mask
, i
;
346 struct super_block
*sb
= inode
->i_sb
;
348 /* Is it quota file? Do not allow user to mess with it */
349 if (ext4_is_quota_file(inode
))
352 oldflags
= ei
->i_flags
;
354 * The JOURNAL_DATA flag can only be changed by
355 * the relevant capability.
357 if ((flags
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
358 if (!capable(CAP_SYS_RESOURCE
))
362 if (!dax_compatible(inode
, oldflags
, flags
)) {
367 if ((flags
^ oldflags
) & EXT4_EXTENTS_FL
)
370 if ((flags
^ oldflags
) & EXT4_CASEFOLD_FL
) {
371 if (!ext4_has_feature_casefold(sb
)) {
376 if (!S_ISDIR(inode
->i_mode
)) {
381 if (!ext4_empty_dir(inode
)) {
388 * Wait for all pending directio and then flush all the dirty pages
389 * for this file. The flush marks all the pages readonly, so any
390 * subsequent attempt to write to the file (particularly mmap pages)
391 * will come through the filesystem and fail.
393 if (S_ISREG(inode
->i_mode
) && !IS_IMMUTABLE(inode
) &&
394 (flags
& EXT4_IMMUTABLE_FL
)) {
395 inode_dio_wait(inode
);
396 err
= filemap_write_and_wait(inode
->i_mapping
);
401 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
402 if (IS_ERR(handle
)) {
403 err
= PTR_ERR(handle
);
407 ext4_handle_sync(handle
);
408 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
412 ext4_dax_dontcache(inode
, flags
);
414 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) {
415 if (!(mask
& EXT4_FL_USER_MODIFIABLE
))
417 /* These flags get special treatment later */
418 if (mask
== EXT4_JOURNAL_DATA_FL
|| mask
== EXT4_EXTENTS_FL
)
421 ext4_set_inode_flag(inode
, i
);
423 ext4_clear_inode_flag(inode
, i
);
426 ext4_set_inode_flags(inode
, false);
428 inode
->i_ctime
= current_time(inode
);
430 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
432 ext4_journal_stop(handle
);
436 if ((flags
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
438 * Changes to the journaling mode can cause unsafe changes to
439 * S_DAX if the inode is DAX
446 err
= ext4_change_inode_journal_flag(inode
,
447 flags
& EXT4_JOURNAL_DATA_FL
);
452 if (flags
& EXT4_EXTENTS_FL
)
453 err
= ext4_ext_migrate(inode
);
455 err
= ext4_ind_migrate(inode
);
463 static int ext4_ioctl_setproject(struct inode
*inode
, __u32 projid
)
465 struct super_block
*sb
= inode
->i_sb
;
466 struct ext4_inode_info
*ei
= EXT4_I(inode
);
470 struct ext4_iloc iloc
;
471 struct ext4_inode
*raw_inode
;
472 struct dquot
*transfer_to
[MAXQUOTAS
] = { };
474 if (!ext4_has_feature_project(sb
)) {
475 if (projid
!= EXT4_DEF_PROJID
)
481 if (EXT4_INODE_SIZE(sb
) <= EXT4_GOOD_OLD_INODE_SIZE
)
484 kprojid
= make_kprojid(&init_user_ns
, (projid_t
)projid
);
486 if (projid_eq(kprojid
, EXT4_I(inode
)->i_projid
))
490 /* Is it quota file? Do not allow user to mess with it */
491 if (ext4_is_quota_file(inode
))
494 err
= ext4_get_inode_loc(inode
, &iloc
);
498 raw_inode
= ext4_raw_inode(&iloc
);
499 if (!EXT4_FITS_IN_INODE(raw_inode
, ei
, i_projid
)) {
500 err
= ext4_expand_extra_isize(inode
,
501 EXT4_SB(sb
)->s_want_extra_isize
,
509 err
= dquot_initialize(inode
);
513 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
514 EXT4_QUOTA_INIT_BLOCKS(sb
) +
515 EXT4_QUOTA_DEL_BLOCKS(sb
) + 3);
517 return PTR_ERR(handle
);
519 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
523 transfer_to
[PRJQUOTA
] = dqget(sb
, make_kqid_projid(kprojid
));
524 if (!IS_ERR(transfer_to
[PRJQUOTA
])) {
526 /* __dquot_transfer() calls back ext4_get_inode_usage() which
527 * counts xattr inode references.
529 down_read(&EXT4_I(inode
)->xattr_sem
);
530 err
= __dquot_transfer(inode
, transfer_to
);
531 up_read(&EXT4_I(inode
)->xattr_sem
);
532 dqput(transfer_to
[PRJQUOTA
]);
537 EXT4_I(inode
)->i_projid
= kprojid
;
538 inode
->i_ctime
= current_time(inode
);
540 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
544 ext4_journal_stop(handle
);
548 static int ext4_ioctl_setproject(struct inode
*inode
, __u32 projid
)
550 if (projid
!= EXT4_DEF_PROJID
)
556 static int ext4_shutdown(struct super_block
*sb
, unsigned long arg
)
558 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
561 if (!capable(CAP_SYS_ADMIN
))
564 if (get_user(flags
, (__u32 __user
*)arg
))
567 if (flags
> EXT4_GOING_FLAGS_NOLOGFLUSH
)
570 if (ext4_forced_shutdown(sbi
))
573 ext4_msg(sb
, KERN_ALERT
, "shut down requested (%d)", flags
);
574 trace_ext4_shutdown(sb
, flags
);
577 case EXT4_GOING_FLAGS_DEFAULT
:
578 freeze_bdev(sb
->s_bdev
);
579 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
580 thaw_bdev(sb
->s_bdev
);
582 case EXT4_GOING_FLAGS_LOGFLUSH
:
583 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
584 if (sbi
->s_journal
&& !is_journal_aborted(sbi
->s_journal
)) {
585 (void) ext4_force_commit(sb
);
586 jbd2_journal_abort(sbi
->s_journal
, -ESHUTDOWN
);
589 case EXT4_GOING_FLAGS_NOLOGFLUSH
:
590 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
591 if (sbi
->s_journal
&& !is_journal_aborted(sbi
->s_journal
))
592 jbd2_journal_abort(sbi
->s_journal
, -ESHUTDOWN
);
597 clear_opt(sb
, DISCARD
);
601 struct getfsmap_info
{
602 struct super_block
*gi_sb
;
603 struct fsmap_head __user
*gi_data
;
608 static int ext4_getfsmap_format(struct ext4_fsmap
*xfm
, void *priv
)
610 struct getfsmap_info
*info
= priv
;
613 trace_ext4_getfsmap_mapping(info
->gi_sb
, xfm
);
615 info
->gi_last_flags
= xfm
->fmr_flags
;
616 ext4_fsmap_from_internal(info
->gi_sb
, &fm
, xfm
);
617 if (copy_to_user(&info
->gi_data
->fmh_recs
[info
->gi_idx
++], &fm
,
618 sizeof(struct fsmap
)))
624 static int ext4_ioc_getfsmap(struct super_block
*sb
,
625 struct fsmap_head __user
*arg
)
627 struct getfsmap_info info
= { NULL
};
628 struct ext4_fsmap_head xhead
= {0};
629 struct fsmap_head head
;
630 bool aborted
= false;
633 if (copy_from_user(&head
, arg
, sizeof(struct fsmap_head
)))
635 if (memchr_inv(head
.fmh_reserved
, 0, sizeof(head
.fmh_reserved
)) ||
636 memchr_inv(head
.fmh_keys
[0].fmr_reserved
, 0,
637 sizeof(head
.fmh_keys
[0].fmr_reserved
)) ||
638 memchr_inv(head
.fmh_keys
[1].fmr_reserved
, 0,
639 sizeof(head
.fmh_keys
[1].fmr_reserved
)))
642 * ext4 doesn't report file extents at all, so the only valid
643 * file offsets are the magic ones (all zeroes or all ones).
645 if (head
.fmh_keys
[0].fmr_offset
||
646 (head
.fmh_keys
[1].fmr_offset
!= 0 &&
647 head
.fmh_keys
[1].fmr_offset
!= -1ULL))
650 xhead
.fmh_iflags
= head
.fmh_iflags
;
651 xhead
.fmh_count
= head
.fmh_count
;
652 ext4_fsmap_to_internal(sb
, &xhead
.fmh_keys
[0], &head
.fmh_keys
[0]);
653 ext4_fsmap_to_internal(sb
, &xhead
.fmh_keys
[1], &head
.fmh_keys
[1]);
655 trace_ext4_getfsmap_low_key(sb
, &xhead
.fmh_keys
[0]);
656 trace_ext4_getfsmap_high_key(sb
, &xhead
.fmh_keys
[1]);
660 error
= ext4_getfsmap(sb
, &xhead
, ext4_getfsmap_format
, &info
);
661 if (error
== EXT4_QUERY_RANGE_ABORT
)
666 /* If we didn't abort, set the "last" flag in the last fmx */
667 if (!aborted
&& info
.gi_idx
) {
668 info
.gi_last_flags
|= FMR_OF_LAST
;
669 if (copy_to_user(&info
.gi_data
->fmh_recs
[info
.gi_idx
- 1].fmr_flags
,
671 sizeof(info
.gi_last_flags
)))
675 /* copy back header */
676 head
.fmh_entries
= xhead
.fmh_entries
;
677 head
.fmh_oflags
= xhead
.fmh_oflags
;
678 if (copy_to_user(arg
, &head
, sizeof(struct fsmap_head
)))
684 static long ext4_ioctl_group_add(struct file
*file
,
685 struct ext4_new_group_data
*input
)
687 struct super_block
*sb
= file_inode(file
)->i_sb
;
690 err
= ext4_resize_begin(sb
);
694 if (ext4_has_feature_bigalloc(sb
)) {
695 ext4_msg(sb
, KERN_ERR
,
696 "Online resizing not supported with bigalloc");
701 err
= mnt_want_write_file(file
);
705 err
= ext4_group_add(sb
, input
);
706 if (EXT4_SB(sb
)->s_journal
) {
707 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
708 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
, 0);
709 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
713 mnt_drop_write_file(file
);
714 if (!err
&& ext4_has_group_desc_csum(sb
) &&
715 test_opt(sb
, INIT_INODE_TABLE
))
716 err
= ext4_register_li_request(sb
, input
->group
);
722 int ext4_fileattr_get(struct dentry
*dentry
, struct fileattr
*fa
)
724 struct inode
*inode
= d_inode(dentry
);
725 struct ext4_inode_info
*ei
= EXT4_I(inode
);
726 u32 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
728 if (S_ISREG(inode
->i_mode
))
729 flags
&= ~FS_PROJINHERIT_FL
;
731 fileattr_fill_flags(fa
, flags
);
732 if (ext4_has_feature_project(inode
->i_sb
))
733 fa
->fsx_projid
= from_kprojid(&init_user_ns
, ei
->i_projid
);
738 int ext4_fileattr_set(struct user_namespace
*mnt_userns
,
739 struct dentry
*dentry
, struct fileattr
*fa
)
741 struct inode
*inode
= d_inode(dentry
);
742 u32 flags
= fa
->flags
;
743 int err
= -EOPNOTSUPP
;
745 ext4_fc_start_update(inode
);
746 if (flags
& ~EXT4_FL_USER_VISIBLE
)
750 * chattr(1) grabs flags via GETFLAGS, modifies the result and
751 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
752 * more restrictive than just silently masking off visible but
753 * not settable flags as we always did.
755 flags
&= EXT4_FL_USER_MODIFIABLE
;
756 if (ext4_mask_flags(inode
->i_mode
, flags
) != flags
)
758 err
= ext4_ioctl_check_immutable(inode
, fa
->fsx_projid
, flags
);
761 err
= ext4_ioctl_setflags(inode
, flags
);
764 err
= ext4_ioctl_setproject(inode
, fa
->fsx_projid
);
766 ext4_fc_stop_update(inode
);
770 /* So that the fiemap access checks can't overflow on 32 bit machines. */
771 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
773 static int ext4_ioctl_get_es_cache(struct file
*filp
, unsigned long arg
)
775 struct fiemap fiemap
;
776 struct fiemap __user
*ufiemap
= (struct fiemap __user
*) arg
;
777 struct fiemap_extent_info fieinfo
= { 0, };
778 struct inode
*inode
= file_inode(filp
);
781 if (copy_from_user(&fiemap
, ufiemap
, sizeof(fiemap
)))
784 if (fiemap
.fm_extent_count
> FIEMAP_MAX_EXTENTS
)
787 fieinfo
.fi_flags
= fiemap
.fm_flags
;
788 fieinfo
.fi_extents_max
= fiemap
.fm_extent_count
;
789 fieinfo
.fi_extents_start
= ufiemap
->fm_extents
;
791 error
= ext4_get_es_cache(inode
, &fieinfo
, fiemap
.fm_start
,
793 fiemap
.fm_flags
= fieinfo
.fi_flags
;
794 fiemap
.fm_mapped_extents
= fieinfo
.fi_extents_mapped
;
795 if (copy_to_user(ufiemap
, &fiemap
, sizeof(fiemap
)))
801 static int ext4_ioctl_checkpoint(struct file
*filp
, unsigned long arg
)
805 unsigned int flush_flags
= 0;
806 struct super_block
*sb
= file_inode(filp
)->i_sb
;
807 struct request_queue
*q
;
809 if (copy_from_user(&flags
, (__u32 __user
*)arg
,
813 if (!capable(CAP_SYS_ADMIN
))
816 /* check for invalid bits set */
817 if ((flags
& ~EXT4_IOC_CHECKPOINT_FLAG_VALID
) ||
818 ((flags
& JBD2_JOURNAL_FLUSH_DISCARD
) &&
819 (flags
& JBD2_JOURNAL_FLUSH_ZEROOUT
)))
822 if (!EXT4_SB(sb
)->s_journal
)
825 if (flags
& ~EXT4_IOC_CHECKPOINT_FLAG_VALID
)
828 q
= bdev_get_queue(EXT4_SB(sb
)->s_journal
->j_dev
);
831 if ((flags
& JBD2_JOURNAL_FLUSH_DISCARD
) && !blk_queue_discard(q
))
834 if (flags
& EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN
)
837 if (flags
& EXT4_IOC_CHECKPOINT_FLAG_DISCARD
)
838 flush_flags
|= JBD2_JOURNAL_FLUSH_DISCARD
;
840 if (flags
& EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT
) {
841 flush_flags
|= JBD2_JOURNAL_FLUSH_ZEROOUT
;
842 pr_info_ratelimited("warning: checkpointing journal with EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT can be slow");
845 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
846 err
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
, flush_flags
);
847 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
852 static long __ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
854 struct inode
*inode
= file_inode(filp
);
855 struct super_block
*sb
= inode
->i_sb
;
856 struct user_namespace
*mnt_userns
= file_mnt_user_ns(filp
);
858 ext4_debug("cmd = %u, arg = %lu\n", cmd
, arg
);
861 case FS_IOC_GETFSMAP
:
862 return ext4_ioc_getfsmap(sb
, (void __user
*)arg
);
863 case EXT4_IOC_GETVERSION
:
864 case EXT4_IOC_GETVERSION_OLD
:
865 return put_user(inode
->i_generation
, (int __user
*) arg
);
866 case EXT4_IOC_SETVERSION
:
867 case EXT4_IOC_SETVERSION_OLD
: {
869 struct ext4_iloc iloc
;
873 if (!inode_owner_or_capable(mnt_userns
, inode
))
876 if (ext4_has_metadata_csum(inode
->i_sb
)) {
877 ext4_warning(sb
, "Setting inode version is not "
878 "supported with metadata_csum enabled.");
882 err
= mnt_want_write_file(filp
);
885 if (get_user(generation
, (int __user
*) arg
)) {
891 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
892 if (IS_ERR(handle
)) {
893 err
= PTR_ERR(handle
);
896 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
898 inode
->i_ctime
= current_time(inode
);
899 inode
->i_generation
= generation
;
900 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
902 ext4_journal_stop(handle
);
907 mnt_drop_write_file(filp
);
910 case EXT4_IOC_GROUP_EXTEND
: {
911 ext4_fsblk_t n_blocks_count
;
914 err
= ext4_resize_begin(sb
);
918 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
920 goto group_extend_out
;
923 if (ext4_has_feature_bigalloc(sb
)) {
924 ext4_msg(sb
, KERN_ERR
,
925 "Online resizing not supported with bigalloc");
927 goto group_extend_out
;
930 err
= mnt_want_write_file(filp
);
932 goto group_extend_out
;
934 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
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(filp
);
948 case EXT4_IOC_MOVE_EXT
: {
949 struct move_extent me
;
953 if (!(filp
->f_mode
& FMODE_READ
) ||
954 !(filp
->f_mode
& FMODE_WRITE
))
957 if (copy_from_user(&me
,
958 (struct move_extent __user
*)arg
, sizeof(me
)))
962 donor
= fdget(me
.donor_fd
);
966 if (!(donor
.file
->f_mode
& FMODE_WRITE
)) {
971 if (ext4_has_feature_bigalloc(sb
)) {
972 ext4_msg(sb
, KERN_ERR
,
973 "Online defrag not supported with bigalloc");
976 } else if (IS_DAX(inode
)) {
977 ext4_msg(sb
, KERN_ERR
,
978 "Online defrag not supported with DAX");
983 err
= mnt_want_write_file(filp
);
987 err
= ext4_move_extents(filp
, donor
.file
, me
.orig_start
,
988 me
.donor_start
, me
.len
, &me
.moved_len
);
989 mnt_drop_write_file(filp
);
991 if (copy_to_user((struct move_extent __user
*)arg
,
999 case EXT4_IOC_GROUP_ADD
: {
1000 struct ext4_new_group_data input
;
1002 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
1006 return ext4_ioctl_group_add(filp
, &input
);
1009 case EXT4_IOC_MIGRATE
:
1012 if (!inode_owner_or_capable(mnt_userns
, inode
))
1015 err
= mnt_want_write_file(filp
);
1019 * inode_mutex prevent write and truncate on the file.
1020 * Read still goes through. We take i_data_sem in
1021 * ext4_ext_swap_inode_data before we switch the
1022 * inode format to prevent read.
1024 inode_lock((inode
));
1025 err
= ext4_ext_migrate(inode
);
1026 inode_unlock((inode
));
1027 mnt_drop_write_file(filp
);
1031 case EXT4_IOC_ALLOC_DA_BLKS
:
1034 if (!inode_owner_or_capable(mnt_userns
, inode
))
1037 err
= mnt_want_write_file(filp
);
1040 err
= ext4_alloc_da_blocks(inode
);
1041 mnt_drop_write_file(filp
);
1045 case EXT4_IOC_SWAP_BOOT
:
1048 if (!(filp
->f_mode
& FMODE_WRITE
))
1050 err
= mnt_want_write_file(filp
);
1053 err
= swap_inode_boot_loader(sb
, mnt_userns
, inode
);
1054 mnt_drop_write_file(filp
);
1058 case EXT4_IOC_RESIZE_FS
: {
1059 ext4_fsblk_t n_blocks_count
;
1060 int err
= 0, err2
= 0;
1061 ext4_group_t o_group
= EXT4_SB(sb
)->s_groups_count
;
1063 if (copy_from_user(&n_blocks_count
, (__u64 __user
*)arg
,
1068 err
= ext4_resize_begin(sb
);
1072 err
= mnt_want_write_file(filp
);
1076 err
= ext4_resize_fs(sb
, n_blocks_count
);
1077 if (EXT4_SB(sb
)->s_journal
) {
1078 ext4_fc_mark_ineligible(sb
, EXT4_FC_REASON_RESIZE
, NULL
);
1079 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
1080 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
, 0);
1081 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
1085 mnt_drop_write_file(filp
);
1086 if (!err
&& (o_group
< EXT4_SB(sb
)->s_groups_count
) &&
1087 ext4_has_group_desc_csum(sb
) &&
1088 test_opt(sb
, INIT_INODE_TABLE
))
1089 err
= ext4_register_li_request(sb
, o_group
);
1092 ext4_resize_end(sb
);
1098 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
1099 struct fstrim_range range
;
1102 if (!capable(CAP_SYS_ADMIN
))
1105 if (!blk_queue_discard(q
))
1109 * We haven't replayed the journal, so we cannot use our
1110 * block-bitmap-guided storage zapping commands.
1112 if (test_opt(sb
, NOLOAD
) && ext4_has_feature_journal(sb
))
1115 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
1119 ret
= ext4_trim_fs(sb
, &range
);
1123 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
1129 case EXT4_IOC_PRECACHE_EXTENTS
:
1130 return ext4_ext_precache(inode
);
1132 case FS_IOC_SET_ENCRYPTION_POLICY
:
1133 if (!ext4_has_feature_encrypt(sb
))
1135 return fscrypt_ioctl_set_policy(filp
, (const void __user
*)arg
);
1137 case FS_IOC_GET_ENCRYPTION_PWSALT
: {
1138 #ifdef CONFIG_FS_ENCRYPTION
1140 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1143 if (!ext4_has_feature_encrypt(sb
))
1145 if (uuid_is_zero(sbi
->s_es
->s_encrypt_pw_salt
)) {
1146 err
= mnt_want_write_file(filp
);
1149 handle
= ext4_journal_start_sb(sb
, EXT4_HT_MISC
, 1);
1150 if (IS_ERR(handle
)) {
1151 err
= PTR_ERR(handle
);
1152 goto pwsalt_err_exit
;
1154 err
= ext4_journal_get_write_access(handle
, sb
,
1158 goto pwsalt_err_journal
;
1159 lock_buffer(sbi
->s_sbh
);
1160 generate_random_uuid(sbi
->s_es
->s_encrypt_pw_salt
);
1161 ext4_superblock_csum_set(sb
);
1162 unlock_buffer(sbi
->s_sbh
);
1163 err
= ext4_handle_dirty_metadata(handle
, NULL
,
1166 err2
= ext4_journal_stop(handle
);
1170 mnt_drop_write_file(filp
);
1174 if (copy_to_user((void __user
*) arg
,
1175 sbi
->s_es
->s_encrypt_pw_salt
, 16))
1182 case FS_IOC_GET_ENCRYPTION_POLICY
:
1183 if (!ext4_has_feature_encrypt(sb
))
1185 return fscrypt_ioctl_get_policy(filp
, (void __user
*)arg
);
1187 case FS_IOC_GET_ENCRYPTION_POLICY_EX
:
1188 if (!ext4_has_feature_encrypt(sb
))
1190 return fscrypt_ioctl_get_policy_ex(filp
, (void __user
*)arg
);
1192 case FS_IOC_ADD_ENCRYPTION_KEY
:
1193 if (!ext4_has_feature_encrypt(sb
))
1195 return fscrypt_ioctl_add_key(filp
, (void __user
*)arg
);
1197 case FS_IOC_REMOVE_ENCRYPTION_KEY
:
1198 if (!ext4_has_feature_encrypt(sb
))
1200 return fscrypt_ioctl_remove_key(filp
, (void __user
*)arg
);
1202 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
:
1203 if (!ext4_has_feature_encrypt(sb
))
1205 return fscrypt_ioctl_remove_key_all_users(filp
,
1206 (void __user
*)arg
);
1207 case FS_IOC_GET_ENCRYPTION_KEY_STATUS
:
1208 if (!ext4_has_feature_encrypt(sb
))
1210 return fscrypt_ioctl_get_key_status(filp
, (void __user
*)arg
);
1212 case FS_IOC_GET_ENCRYPTION_NONCE
:
1213 if (!ext4_has_feature_encrypt(sb
))
1215 return fscrypt_ioctl_get_nonce(filp
, (void __user
*)arg
);
1217 case EXT4_IOC_CLEAR_ES_CACHE
:
1219 if (!inode_owner_or_capable(mnt_userns
, inode
))
1221 ext4_clear_inode_es(inode
);
1225 case EXT4_IOC_GETSTATE
:
1229 if (ext4_test_inode_state(inode
, EXT4_STATE_EXT_PRECACHED
))
1230 state
|= EXT4_STATE_FLAG_EXT_PRECACHED
;
1231 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
))
1232 state
|= EXT4_STATE_FLAG_NEW
;
1233 if (ext4_test_inode_state(inode
, EXT4_STATE_NEWENTRY
))
1234 state
|= EXT4_STATE_FLAG_NEWENTRY
;
1235 if (ext4_test_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
))
1236 state
|= EXT4_STATE_FLAG_DA_ALLOC_CLOSE
;
1238 return put_user(state
, (__u32 __user
*) arg
);
1241 case EXT4_IOC_GET_ES_CACHE
:
1242 return ext4_ioctl_get_es_cache(filp
, arg
);
1244 case EXT4_IOC_SHUTDOWN
:
1245 return ext4_shutdown(sb
, arg
);
1247 case FS_IOC_ENABLE_VERITY
:
1248 if (!ext4_has_feature_verity(sb
))
1250 return fsverity_ioctl_enable(filp
, (const void __user
*)arg
);
1252 case FS_IOC_MEASURE_VERITY
:
1253 if (!ext4_has_feature_verity(sb
))
1255 return fsverity_ioctl_measure(filp
, (void __user
*)arg
);
1257 case FS_IOC_READ_VERITY_METADATA
:
1258 if (!ext4_has_feature_verity(sb
))
1260 return fsverity_ioctl_read_metadata(filp
,
1261 (const void __user
*)arg
);
1263 case EXT4_IOC_CHECKPOINT
:
1264 return ext4_ioctl_checkpoint(filp
, arg
);
1271 long ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1275 ext4_fc_start_update(file_inode(filp
));
1276 ret
= __ext4_ioctl(filp
, cmd
, arg
);
1277 ext4_fc_stop_update(file_inode(filp
));
1282 #ifdef CONFIG_COMPAT
1283 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1285 /* These are just misnamed, they actually get/put from/to user an int */
1287 case EXT4_IOC32_GETVERSION
:
1288 cmd
= EXT4_IOC_GETVERSION
;
1290 case EXT4_IOC32_SETVERSION
:
1291 cmd
= EXT4_IOC_SETVERSION
;
1293 case EXT4_IOC32_GROUP_EXTEND
:
1294 cmd
= EXT4_IOC_GROUP_EXTEND
;
1296 case EXT4_IOC32_GETVERSION_OLD
:
1297 cmd
= EXT4_IOC_GETVERSION_OLD
;
1299 case EXT4_IOC32_SETVERSION_OLD
:
1300 cmd
= EXT4_IOC_SETVERSION_OLD
;
1302 case EXT4_IOC32_GETRSVSZ
:
1303 cmd
= EXT4_IOC_GETRSVSZ
;
1305 case EXT4_IOC32_SETRSVSZ
:
1306 cmd
= EXT4_IOC_SETRSVSZ
;
1308 case EXT4_IOC32_GROUP_ADD
: {
1309 struct compat_ext4_new_group_input __user
*uinput
;
1310 struct ext4_new_group_data input
;
1313 uinput
= compat_ptr(arg
);
1314 err
= get_user(input
.group
, &uinput
->group
);
1315 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
1316 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
1317 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
1318 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
1319 err
|= get_user(input
.reserved_blocks
,
1320 &uinput
->reserved_blocks
);
1323 return ext4_ioctl_group_add(file
, &input
);
1325 case EXT4_IOC_MOVE_EXT
:
1326 case EXT4_IOC_RESIZE_FS
:
1328 case EXT4_IOC_PRECACHE_EXTENTS
:
1329 case FS_IOC_SET_ENCRYPTION_POLICY
:
1330 case FS_IOC_GET_ENCRYPTION_PWSALT
:
1331 case FS_IOC_GET_ENCRYPTION_POLICY
:
1332 case FS_IOC_GET_ENCRYPTION_POLICY_EX
:
1333 case FS_IOC_ADD_ENCRYPTION_KEY
:
1334 case FS_IOC_REMOVE_ENCRYPTION_KEY
:
1335 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
:
1336 case FS_IOC_GET_ENCRYPTION_KEY_STATUS
:
1337 case FS_IOC_GET_ENCRYPTION_NONCE
:
1338 case EXT4_IOC_SHUTDOWN
:
1339 case FS_IOC_GETFSMAP
:
1340 case FS_IOC_ENABLE_VERITY
:
1341 case FS_IOC_MEASURE_VERITY
:
1342 case FS_IOC_READ_VERITY_METADATA
:
1343 case EXT4_IOC_CLEAR_ES_CACHE
:
1344 case EXT4_IOC_GETSTATE
:
1345 case EXT4_IOC_GET_ES_CACHE
:
1346 case EXT4_IOC_CHECKPOINT
:
1349 return -ENOIOCTLCMD
;
1351 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));