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/uuid.h>
19 #include <linux/uaccess.h>
20 #include <linux/delay.h>
21 #include "ext4_jbd2.h"
23 #include <linux/fsmap.h>
25 #include <trace/events/ext4.h>
28 * Swap memory between @a and @b for @len bytes.
30 * @a: pointer to first memory area
31 * @b: pointer to second memory area
32 * @len: number of bytes to swap
35 static void memswap(void *a
, void *b
, size_t len
)
37 unsigned char *ap
, *bp
;
39 ap
= (unsigned char *)a
;
40 bp
= (unsigned char *)b
;
49 * Swap i_data and associated attributes between @inode1 and @inode2.
50 * This function is used for the primary swap between inode1 and inode2
51 * and also to revert this primary swap in case of errors.
53 * Therefore you have to make sure, that calling this method twice
54 * will revert all changes.
56 * @inode1: pointer to first inode
57 * @inode2: pointer to second inode
59 static void swap_inode_data(struct inode
*inode1
, struct inode
*inode2
)
62 struct ext4_inode_info
*ei1
;
63 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 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
);
91 * Swap the information from the given @inode and the inode
92 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
93 * important fields of the inodes.
95 * @sb: the super block of the filesystem
96 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
99 static long swap_inode_boot_loader(struct super_block
*sb
,
104 struct inode
*inode_bl
;
105 struct ext4_inode_info
*ei_bl
;
106 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
108 if (inode
->i_nlink
!= 1 || !S_ISREG(inode
->i_mode
))
111 if (!inode_owner_or_capable(inode
) || !capable(CAP_SYS_ADMIN
))
114 inode_bl
= ext4_iget(sb
, EXT4_BOOT_LOADER_INO
);
115 if (IS_ERR(inode_bl
))
116 return PTR_ERR(inode_bl
);
117 ei_bl
= EXT4_I(inode_bl
);
119 filemap_flush(inode
->i_mapping
);
120 filemap_flush(inode_bl
->i_mapping
);
122 /* Protect orig inodes against a truncate and make sure,
123 * that only 1 swap_inode_boot_loader is running. */
124 lock_two_nondirectories(inode
, inode_bl
);
126 truncate_inode_pages(&inode
->i_data
, 0);
127 truncate_inode_pages(&inode_bl
->i_data
, 0);
129 /* Wait for all existing dio workers */
130 ext4_inode_block_unlocked_dio(inode
);
131 ext4_inode_block_unlocked_dio(inode_bl
);
132 inode_dio_wait(inode
);
133 inode_dio_wait(inode_bl
);
135 handle
= ext4_journal_start(inode_bl
, EXT4_HT_MOVE_EXTENTS
, 2);
136 if (IS_ERR(handle
)) {
138 goto journal_err_out
;
141 /* Protect extent tree against block allocations via delalloc */
142 ext4_double_down_write_data_sem(inode
, inode_bl
);
144 if (inode_bl
->i_nlink
== 0) {
145 /* this inode has never been used as a BOOT_LOADER */
146 set_nlink(inode_bl
, 1);
147 i_uid_write(inode_bl
, 0);
148 i_gid_write(inode_bl
, 0);
149 inode_bl
->i_flags
= 0;
151 inode_bl
->i_version
= 1;
152 i_size_write(inode_bl
, 0);
153 inode_bl
->i_mode
= S_IFREG
;
154 if (ext4_has_feature_extents(sb
)) {
155 ext4_set_inode_flag(inode_bl
, EXT4_INODE_EXTENTS
);
156 ext4_ext_tree_init(handle
, inode_bl
);
158 memset(ei_bl
->i_data
, 0, sizeof(ei_bl
->i_data
));
161 swap_inode_data(inode
, inode_bl
);
163 inode
->i_ctime
= inode_bl
->i_ctime
= current_time(inode
);
165 spin_lock(&sbi
->s_next_gen_lock
);
166 inode
->i_generation
= sbi
->s_next_generation
++;
167 inode_bl
->i_generation
= sbi
->s_next_generation
++;
168 spin_unlock(&sbi
->s_next_gen_lock
);
170 ext4_discard_preallocations(inode
);
172 err
= ext4_mark_inode_dirty(handle
, inode
);
174 ext4_warning(inode
->i_sb
,
175 "couldn't mark inode #%lu dirty (err %d)",
177 /* Revert all changes: */
178 swap_inode_data(inode
, inode_bl
);
180 err
= ext4_mark_inode_dirty(handle
, inode_bl
);
182 ext4_warning(inode_bl
->i_sb
,
183 "couldn't mark inode #%lu dirty (err %d)",
184 inode_bl
->i_ino
, err
);
185 /* Revert all changes: */
186 swap_inode_data(inode
, inode_bl
);
187 ext4_mark_inode_dirty(handle
, inode
);
190 ext4_journal_stop(handle
);
191 ext4_double_up_write_data_sem(inode
, inode_bl
);
194 ext4_inode_resume_unlocked_dio(inode
);
195 ext4_inode_resume_unlocked_dio(inode_bl
);
196 unlock_two_nondirectories(inode
, inode_bl
);
201 #ifdef CONFIG_EXT4_FS_ENCRYPTION
202 static int uuid_is_zero(__u8 u
[16])
206 for (i
= 0; i
< 16; i
++)
213 static int ext4_ioctl_setflags(struct inode
*inode
,
216 struct ext4_inode_info
*ei
= EXT4_I(inode
);
217 handle_t
*handle
= NULL
;
218 int err
= -EPERM
, migrate
= 0;
219 struct ext4_iloc iloc
;
220 unsigned int oldflags
, mask
, i
;
223 /* Is it quota file? Do not allow user to mess with it */
224 if (ext4_is_quota_file(inode
))
227 oldflags
= ei
->i_flags
;
229 /* The JOURNAL_DATA flag is modifiable only by root */
230 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
233 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
234 * the relevant capability.
236 * This test looks nicer. Thanks to Pauline Middelink
238 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
239 if (!capable(CAP_LINUX_IMMUTABLE
))
244 * The JOURNAL_DATA flag can only be changed by
245 * the relevant capability.
247 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
248 if (!capable(CAP_SYS_RESOURCE
))
251 if ((flags
^ oldflags
) & EXT4_EXTENTS_FL
)
254 if (flags
& EXT4_EOFBLOCKS_FL
) {
255 /* we don't support adding EOFBLOCKS flag */
256 if (!(oldflags
& EXT4_EOFBLOCKS_FL
)) {
260 } else if (oldflags
& EXT4_EOFBLOCKS_FL
) {
261 err
= ext4_truncate(inode
);
266 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
267 if (IS_ERR(handle
)) {
268 err
= PTR_ERR(handle
);
272 ext4_handle_sync(handle
);
273 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
277 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) {
278 if (!(mask
& EXT4_FL_USER_MODIFIABLE
))
280 /* These flags get special treatment later */
281 if (mask
== EXT4_JOURNAL_DATA_FL
|| mask
== EXT4_EXTENTS_FL
)
284 ext4_set_inode_flag(inode
, i
);
286 ext4_clear_inode_flag(inode
, i
);
289 ext4_set_inode_flags(inode
);
290 inode
->i_ctime
= current_time(inode
);
292 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
294 ext4_journal_stop(handle
);
298 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
300 * Changes to the journaling mode can cause unsafe changes to
301 * S_DAX if we are using the DAX mount option.
303 if (test_opt(inode
->i_sb
, DAX
)) {
308 err
= ext4_change_inode_journal_flag(inode
, jflag
);
313 if (flags
& EXT4_EXTENTS_FL
)
314 err
= ext4_ext_migrate(inode
);
316 err
= ext4_ind_migrate(inode
);
324 static int ext4_ioctl_setproject(struct file
*filp
, __u32 projid
)
326 struct inode
*inode
= file_inode(filp
);
327 struct super_block
*sb
= inode
->i_sb
;
328 struct ext4_inode_info
*ei
= EXT4_I(inode
);
332 struct ext4_iloc iloc
;
333 struct ext4_inode
*raw_inode
;
334 struct dquot
*transfer_to
[MAXQUOTAS
] = { };
336 if (!ext4_has_feature_project(sb
)) {
337 if (projid
!= EXT4_DEF_PROJID
)
343 if (EXT4_INODE_SIZE(sb
) <= EXT4_GOOD_OLD_INODE_SIZE
)
346 kprojid
= make_kprojid(&init_user_ns
, (projid_t
)projid
);
348 if (projid_eq(kprojid
, EXT4_I(inode
)->i_projid
))
352 /* Is it quota file? Do not allow user to mess with it */
353 if (ext4_is_quota_file(inode
))
356 err
= ext4_get_inode_loc(inode
, &iloc
);
360 raw_inode
= ext4_raw_inode(&iloc
);
361 if (!EXT4_FITS_IN_INODE(raw_inode
, ei
, i_projid
)) {
362 err
= ext4_expand_extra_isize(inode
,
363 EXT4_SB(sb
)->s_want_extra_isize
,
371 err
= dquot_initialize(inode
);
375 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
376 EXT4_QUOTA_INIT_BLOCKS(sb
) +
377 EXT4_QUOTA_DEL_BLOCKS(sb
) + 3);
379 return PTR_ERR(handle
);
381 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
385 transfer_to
[PRJQUOTA
] = dqget(sb
, make_kqid_projid(kprojid
));
386 if (!IS_ERR(transfer_to
[PRJQUOTA
])) {
388 /* __dquot_transfer() calls back ext4_get_inode_usage() which
389 * counts xattr inode references.
391 down_read(&EXT4_I(inode
)->xattr_sem
);
392 err
= __dquot_transfer(inode
, transfer_to
);
393 up_read(&EXT4_I(inode
)->xattr_sem
);
394 dqput(transfer_to
[PRJQUOTA
]);
399 EXT4_I(inode
)->i_projid
= kprojid
;
400 inode
->i_ctime
= current_time(inode
);
402 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
406 ext4_journal_stop(handle
);
410 static int ext4_ioctl_setproject(struct file
*filp
, __u32 projid
)
412 if (projid
!= EXT4_DEF_PROJID
)
418 /* Transfer internal flags to xflags */
419 static inline __u32
ext4_iflags_to_xflags(unsigned long iflags
)
423 if (iflags
& EXT4_SYNC_FL
)
424 xflags
|= FS_XFLAG_SYNC
;
425 if (iflags
& EXT4_IMMUTABLE_FL
)
426 xflags
|= FS_XFLAG_IMMUTABLE
;
427 if (iflags
& EXT4_APPEND_FL
)
428 xflags
|= FS_XFLAG_APPEND
;
429 if (iflags
& EXT4_NODUMP_FL
)
430 xflags
|= FS_XFLAG_NODUMP
;
431 if (iflags
& EXT4_NOATIME_FL
)
432 xflags
|= FS_XFLAG_NOATIME
;
433 if (iflags
& EXT4_PROJINHERIT_FL
)
434 xflags
|= FS_XFLAG_PROJINHERIT
;
438 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
439 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
440 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
442 /* Transfer xflags flags to internal */
443 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags
)
445 unsigned long iflags
= 0;
447 if (xflags
& FS_XFLAG_SYNC
)
448 iflags
|= EXT4_SYNC_FL
;
449 if (xflags
& FS_XFLAG_IMMUTABLE
)
450 iflags
|= EXT4_IMMUTABLE_FL
;
451 if (xflags
& FS_XFLAG_APPEND
)
452 iflags
|= EXT4_APPEND_FL
;
453 if (xflags
& FS_XFLAG_NODUMP
)
454 iflags
|= EXT4_NODUMP_FL
;
455 if (xflags
& FS_XFLAG_NOATIME
)
456 iflags
|= EXT4_NOATIME_FL
;
457 if (xflags
& FS_XFLAG_PROJINHERIT
)
458 iflags
|= EXT4_PROJINHERIT_FL
;
463 static int ext4_shutdown(struct super_block
*sb
, unsigned long arg
)
465 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
468 if (!capable(CAP_SYS_ADMIN
))
471 if (get_user(flags
, (__u32 __user
*)arg
))
474 if (flags
> EXT4_GOING_FLAGS_NOLOGFLUSH
)
477 if (ext4_forced_shutdown(sbi
))
480 ext4_msg(sb
, KERN_ALERT
, "shut down requested (%d)", flags
);
483 case EXT4_GOING_FLAGS_DEFAULT
:
484 freeze_bdev(sb
->s_bdev
);
485 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
486 thaw_bdev(sb
->s_bdev
, sb
);
488 case EXT4_GOING_FLAGS_LOGFLUSH
:
489 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
490 if (sbi
->s_journal
&& !is_journal_aborted(sbi
->s_journal
)) {
491 (void) ext4_force_commit(sb
);
492 jbd2_journal_abort(sbi
->s_journal
, -ESHUTDOWN
);
495 case EXT4_GOING_FLAGS_NOLOGFLUSH
:
496 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
497 if (sbi
->s_journal
&& !is_journal_aborted(sbi
->s_journal
))
498 jbd2_journal_abort(sbi
->s_journal
, -ESHUTDOWN
);
503 clear_opt(sb
, DISCARD
);
507 struct getfsmap_info
{
508 struct super_block
*gi_sb
;
509 struct fsmap_head __user
*gi_data
;
514 static int ext4_getfsmap_format(struct ext4_fsmap
*xfm
, void *priv
)
516 struct getfsmap_info
*info
= priv
;
519 trace_ext4_getfsmap_mapping(info
->gi_sb
, xfm
);
521 info
->gi_last_flags
= xfm
->fmr_flags
;
522 ext4_fsmap_from_internal(info
->gi_sb
, &fm
, xfm
);
523 if (copy_to_user(&info
->gi_data
->fmh_recs
[info
->gi_idx
++], &fm
,
524 sizeof(struct fsmap
)))
530 static int ext4_ioc_getfsmap(struct super_block
*sb
,
531 struct fsmap_head __user
*arg
)
533 struct getfsmap_info info
= {0};
534 struct ext4_fsmap_head xhead
= {0};
535 struct fsmap_head head
;
536 bool aborted
= false;
539 if (copy_from_user(&head
, arg
, sizeof(struct fsmap_head
)))
541 if (memchr_inv(head
.fmh_reserved
, 0, sizeof(head
.fmh_reserved
)) ||
542 memchr_inv(head
.fmh_keys
[0].fmr_reserved
, 0,
543 sizeof(head
.fmh_keys
[0].fmr_reserved
)) ||
544 memchr_inv(head
.fmh_keys
[1].fmr_reserved
, 0,
545 sizeof(head
.fmh_keys
[1].fmr_reserved
)))
548 * ext4 doesn't report file extents at all, so the only valid
549 * file offsets are the magic ones (all zeroes or all ones).
551 if (head
.fmh_keys
[0].fmr_offset
||
552 (head
.fmh_keys
[1].fmr_offset
!= 0 &&
553 head
.fmh_keys
[1].fmr_offset
!= -1ULL))
556 xhead
.fmh_iflags
= head
.fmh_iflags
;
557 xhead
.fmh_count
= head
.fmh_count
;
558 ext4_fsmap_to_internal(sb
, &xhead
.fmh_keys
[0], &head
.fmh_keys
[0]);
559 ext4_fsmap_to_internal(sb
, &xhead
.fmh_keys
[1], &head
.fmh_keys
[1]);
561 trace_ext4_getfsmap_low_key(sb
, &xhead
.fmh_keys
[0]);
562 trace_ext4_getfsmap_high_key(sb
, &xhead
.fmh_keys
[1]);
566 error
= ext4_getfsmap(sb
, &xhead
, ext4_getfsmap_format
, &info
);
567 if (error
== EXT4_QUERY_RANGE_ABORT
) {
573 /* If we didn't abort, set the "last" flag in the last fmx */
574 if (!aborted
&& info
.gi_idx
) {
575 info
.gi_last_flags
|= FMR_OF_LAST
;
576 if (copy_to_user(&info
.gi_data
->fmh_recs
[info
.gi_idx
- 1].fmr_flags
,
578 sizeof(info
.gi_last_flags
)))
582 /* copy back header */
583 head
.fmh_entries
= xhead
.fmh_entries
;
584 head
.fmh_oflags
= xhead
.fmh_oflags
;
585 if (copy_to_user(arg
, &head
, sizeof(struct fsmap_head
)))
591 static int ext4_ioctl_check_project(struct inode
*inode
, struct fsxattr
*fa
)
594 * Project Quota ID state is only allowed to change from within the init
595 * namespace. Enforce that restriction only if we are trying to change
596 * the quota ID state. Everything else is allowed in user namespaces.
598 if (current_user_ns() == &init_user_ns
)
601 if (__kprojid_val(EXT4_I(inode
)->i_projid
) != fa
->fsx_projid
)
604 if (ext4_test_inode_flag(inode
, EXT4_INODE_PROJINHERIT
)) {
605 if (!(fa
->fsx_xflags
& FS_XFLAG_PROJINHERIT
))
608 if (fa
->fsx_xflags
& FS_XFLAG_PROJINHERIT
)
615 long ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
617 struct inode
*inode
= file_inode(filp
);
618 struct super_block
*sb
= inode
->i_sb
;
619 struct ext4_inode_info
*ei
= EXT4_I(inode
);
622 ext4_debug("cmd = %u, arg = %lu\n", cmd
, arg
);
625 case FS_IOC_GETFSMAP
:
626 return ext4_ioc_getfsmap(sb
, (void __user
*)arg
);
627 case EXT4_IOC_GETFLAGS
:
628 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
629 return put_user(flags
, (int __user
*) arg
);
630 case EXT4_IOC_SETFLAGS
: {
633 if (!inode_owner_or_capable(inode
))
636 if (get_user(flags
, (int __user
*) arg
))
639 if (flags
& ~EXT4_FL_USER_VISIBLE
)
642 * chattr(1) grabs flags via GETFLAGS, modifies the result and
643 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
644 * more restrictive than just silently masking off visible but
645 * not settable flags as we always did.
647 flags
&= EXT4_FL_USER_MODIFIABLE
;
648 if (ext4_mask_flags(inode
->i_mode
, flags
) != flags
)
651 err
= mnt_want_write_file(filp
);
656 err
= ext4_ioctl_setflags(inode
, flags
);
658 mnt_drop_write_file(filp
);
661 case EXT4_IOC_GETVERSION
:
662 case EXT4_IOC_GETVERSION_OLD
:
663 return put_user(inode
->i_generation
, (int __user
*) arg
);
664 case EXT4_IOC_SETVERSION
:
665 case EXT4_IOC_SETVERSION_OLD
: {
667 struct ext4_iloc iloc
;
671 if (!inode_owner_or_capable(inode
))
674 if (ext4_has_metadata_csum(inode
->i_sb
)) {
675 ext4_warning(sb
, "Setting inode version is not "
676 "supported with metadata_csum enabled.");
680 err
= mnt_want_write_file(filp
);
683 if (get_user(generation
, (int __user
*) arg
)) {
689 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
690 if (IS_ERR(handle
)) {
691 err
= PTR_ERR(handle
);
694 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
696 inode
->i_ctime
= current_time(inode
);
697 inode
->i_generation
= generation
;
698 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
700 ext4_journal_stop(handle
);
705 mnt_drop_write_file(filp
);
708 case EXT4_IOC_GROUP_EXTEND
: {
709 ext4_fsblk_t n_blocks_count
;
712 err
= ext4_resize_begin(sb
);
716 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
718 goto group_extend_out
;
721 if (ext4_has_feature_bigalloc(sb
)) {
722 ext4_msg(sb
, KERN_ERR
,
723 "Online resizing not supported with bigalloc");
725 goto group_extend_out
;
728 err
= mnt_want_write_file(filp
);
730 goto group_extend_out
;
732 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
733 if (EXT4_SB(sb
)->s_journal
) {
734 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
735 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
736 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
740 mnt_drop_write_file(filp
);
746 case EXT4_IOC_MOVE_EXT
: {
747 struct move_extent me
;
751 if (!(filp
->f_mode
& FMODE_READ
) ||
752 !(filp
->f_mode
& FMODE_WRITE
))
755 if (copy_from_user(&me
,
756 (struct move_extent __user
*)arg
, sizeof(me
)))
760 donor
= fdget(me
.donor_fd
);
764 if (!(donor
.file
->f_mode
& FMODE_WRITE
)) {
769 if (ext4_has_feature_bigalloc(sb
)) {
770 ext4_msg(sb
, KERN_ERR
,
771 "Online defrag not supported with bigalloc");
774 } else if (IS_DAX(inode
)) {
775 ext4_msg(sb
, KERN_ERR
,
776 "Online defrag not supported with DAX");
781 err
= mnt_want_write_file(filp
);
785 err
= ext4_move_extents(filp
, donor
.file
, me
.orig_start
,
786 me
.donor_start
, me
.len
, &me
.moved_len
);
787 mnt_drop_write_file(filp
);
789 if (copy_to_user((struct move_extent __user
*)arg
,
797 case EXT4_IOC_GROUP_ADD
: {
798 struct ext4_new_group_data input
;
801 err
= ext4_resize_begin(sb
);
805 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
811 if (ext4_has_feature_bigalloc(sb
)) {
812 ext4_msg(sb
, KERN_ERR
,
813 "Online resizing not supported with bigalloc");
818 err
= mnt_want_write_file(filp
);
822 err
= ext4_group_add(sb
, &input
);
823 if (EXT4_SB(sb
)->s_journal
) {
824 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
825 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
826 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
830 mnt_drop_write_file(filp
);
831 if (!err
&& ext4_has_group_desc_csum(sb
) &&
832 test_opt(sb
, INIT_INODE_TABLE
))
833 err
= ext4_register_li_request(sb
, input
.group
);
839 case EXT4_IOC_MIGRATE
:
842 if (!inode_owner_or_capable(inode
))
845 err
= mnt_want_write_file(filp
);
849 * inode_mutex prevent write and truncate on the file.
850 * Read still goes through. We take i_data_sem in
851 * ext4_ext_swap_inode_data before we switch the
852 * inode format to prevent read.
855 err
= ext4_ext_migrate(inode
);
856 inode_unlock((inode
));
857 mnt_drop_write_file(filp
);
861 case EXT4_IOC_ALLOC_DA_BLKS
:
864 if (!inode_owner_or_capable(inode
))
867 err
= mnt_want_write_file(filp
);
870 err
= ext4_alloc_da_blocks(inode
);
871 mnt_drop_write_file(filp
);
875 case EXT4_IOC_SWAP_BOOT
:
878 if (!(filp
->f_mode
& FMODE_WRITE
))
880 err
= mnt_want_write_file(filp
);
883 err
= swap_inode_boot_loader(sb
, inode
);
884 mnt_drop_write_file(filp
);
888 case EXT4_IOC_RESIZE_FS
: {
889 ext4_fsblk_t n_blocks_count
;
890 int err
= 0, err2
= 0;
891 ext4_group_t o_group
= EXT4_SB(sb
)->s_groups_count
;
893 if (ext4_has_feature_bigalloc(sb
)) {
894 ext4_msg(sb
, KERN_ERR
,
895 "Online resizing not (yet) supported with bigalloc");
899 if (copy_from_user(&n_blocks_count
, (__u64 __user
*)arg
,
904 err
= ext4_resize_begin(sb
);
908 err
= mnt_want_write_file(filp
);
912 err
= ext4_resize_fs(sb
, n_blocks_count
);
913 if (EXT4_SB(sb
)->s_journal
) {
914 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
915 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
916 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
920 mnt_drop_write_file(filp
);
921 if (!err
&& (o_group
> EXT4_SB(sb
)->s_groups_count
) &&
922 ext4_has_group_desc_csum(sb
) &&
923 test_opt(sb
, INIT_INODE_TABLE
))
924 err
= ext4_register_li_request(sb
, o_group
);
933 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
934 struct fstrim_range range
;
937 if (!capable(CAP_SYS_ADMIN
))
940 if (!blk_queue_discard(q
))
944 * We haven't replayed the journal, so we cannot use our
945 * block-bitmap-guided storage zapping commands.
947 if (test_opt(sb
, NOLOAD
) && ext4_has_feature_journal(sb
))
950 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
954 range
.minlen
= max((unsigned int)range
.minlen
,
955 q
->limits
.discard_granularity
);
956 ret
= ext4_trim_fs(sb
, &range
);
960 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
966 case EXT4_IOC_PRECACHE_EXTENTS
:
967 return ext4_ext_precache(inode
);
969 case EXT4_IOC_SET_ENCRYPTION_POLICY
:
970 if (!ext4_has_feature_encrypt(sb
))
972 return fscrypt_ioctl_set_policy(filp
, (const void __user
*)arg
);
974 case EXT4_IOC_GET_ENCRYPTION_PWSALT
: {
975 #ifdef CONFIG_EXT4_FS_ENCRYPTION
977 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
980 if (!ext4_has_feature_encrypt(sb
))
982 if (uuid_is_zero(sbi
->s_es
->s_encrypt_pw_salt
)) {
983 err
= mnt_want_write_file(filp
);
986 handle
= ext4_journal_start_sb(sb
, EXT4_HT_MISC
, 1);
987 if (IS_ERR(handle
)) {
988 err
= PTR_ERR(handle
);
989 goto pwsalt_err_exit
;
991 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
993 goto pwsalt_err_journal
;
994 generate_random_uuid(sbi
->s_es
->s_encrypt_pw_salt
);
995 err
= ext4_handle_dirty_metadata(handle
, NULL
,
998 err2
= ext4_journal_stop(handle
);
1002 mnt_drop_write_file(filp
);
1006 if (copy_to_user((void __user
*) arg
,
1007 sbi
->s_es
->s_encrypt_pw_salt
, 16))
1014 case EXT4_IOC_GET_ENCRYPTION_POLICY
:
1015 return fscrypt_ioctl_get_policy(filp
, (void __user
*)arg
);
1017 case EXT4_IOC_FSGETXATTR
:
1021 memset(&fa
, 0, sizeof(struct fsxattr
));
1022 fa
.fsx_xflags
= ext4_iflags_to_xflags(ei
->i_flags
& EXT4_FL_USER_VISIBLE
);
1024 if (ext4_has_feature_project(inode
->i_sb
)) {
1025 fa
.fsx_projid
= (__u32
)from_kprojid(&init_user_ns
,
1026 EXT4_I(inode
)->i_projid
);
1029 if (copy_to_user((struct fsxattr __user
*)arg
,
1034 case EXT4_IOC_FSSETXATTR
:
1039 if (copy_from_user(&fa
, (struct fsxattr __user
*)arg
,
1043 /* Make sure caller has proper permission */
1044 if (!inode_owner_or_capable(inode
))
1047 if (fa
.fsx_xflags
& ~EXT4_SUPPORTED_FS_XFLAGS
)
1050 flags
= ext4_xflags_to_iflags(fa
.fsx_xflags
);
1051 if (ext4_mask_flags(inode
->i_mode
, flags
) != flags
)
1054 err
= mnt_want_write_file(filp
);
1059 err
= ext4_ioctl_check_project(inode
, &fa
);
1062 flags
= (ei
->i_flags
& ~EXT4_FL_XFLAG_VISIBLE
) |
1063 (flags
& EXT4_FL_XFLAG_VISIBLE
);
1064 err
= ext4_ioctl_setflags(inode
, flags
);
1067 err
= ext4_ioctl_setproject(filp
, fa
.fsx_projid
);
1069 inode_unlock(inode
);
1070 mnt_drop_write_file(filp
);
1073 case EXT4_IOC_SHUTDOWN
:
1074 return ext4_shutdown(sb
, arg
);
1080 #ifdef CONFIG_COMPAT
1081 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1083 /* These are just misnamed, they actually get/put from/to user an int */
1085 case EXT4_IOC32_GETFLAGS
:
1086 cmd
= EXT4_IOC_GETFLAGS
;
1088 case EXT4_IOC32_SETFLAGS
:
1089 cmd
= EXT4_IOC_SETFLAGS
;
1091 case EXT4_IOC32_GETVERSION
:
1092 cmd
= EXT4_IOC_GETVERSION
;
1094 case EXT4_IOC32_SETVERSION
:
1095 cmd
= EXT4_IOC_SETVERSION
;
1097 case EXT4_IOC32_GROUP_EXTEND
:
1098 cmd
= EXT4_IOC_GROUP_EXTEND
;
1100 case EXT4_IOC32_GETVERSION_OLD
:
1101 cmd
= EXT4_IOC_GETVERSION_OLD
;
1103 case EXT4_IOC32_SETVERSION_OLD
:
1104 cmd
= EXT4_IOC_SETVERSION_OLD
;
1106 case EXT4_IOC32_GETRSVSZ
:
1107 cmd
= EXT4_IOC_GETRSVSZ
;
1109 case EXT4_IOC32_SETRSVSZ
:
1110 cmd
= EXT4_IOC_SETRSVSZ
;
1112 case EXT4_IOC32_GROUP_ADD
: {
1113 struct compat_ext4_new_group_input __user
*uinput
;
1114 struct ext4_new_group_input input
;
1115 mm_segment_t old_fs
;
1118 uinput
= compat_ptr(arg
);
1119 err
= get_user(input
.group
, &uinput
->group
);
1120 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
1121 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
1122 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
1123 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
1124 err
|= get_user(input
.reserved_blocks
,
1125 &uinput
->reserved_blocks
);
1130 err
= ext4_ioctl(file
, EXT4_IOC_GROUP_ADD
,
1131 (unsigned long) &input
);
1135 case EXT4_IOC_MOVE_EXT
:
1136 case EXT4_IOC_RESIZE_FS
:
1137 case EXT4_IOC_PRECACHE_EXTENTS
:
1138 case EXT4_IOC_SET_ENCRYPTION_POLICY
:
1139 case EXT4_IOC_GET_ENCRYPTION_PWSALT
:
1140 case EXT4_IOC_GET_ENCRYPTION_POLICY
:
1141 case EXT4_IOC_SHUTDOWN
:
1142 case FS_IOC_GETFSMAP
:
1145 return -ENOIOCTLCMD
;
1147 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));