2 * linux/fs/ext4/ioctl.c
4 * Copyright (C) 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 #include <linux/capability.h>
12 #include <linux/time.h>
13 #include <linux/compat.h>
14 #include <linux/mount.h>
15 #include <linux/file.h>
16 #include <linux/quotaops.h>
17 #include <linux/uuid.h>
18 #include <linux/uaccess.h>
19 #include <linux/delay.h>
20 #include "ext4_jbd2.h"
22 #include <linux/fsmap.h>
24 #include <trace/events/ext4.h>
27 * Swap memory between @a and @b for @len bytes.
29 * @a: pointer to first memory area
30 * @b: pointer to second memory area
31 * @len: number of bytes to swap
34 static void memswap(void *a
, void *b
, size_t len
)
36 unsigned char *ap
, *bp
;
38 ap
= (unsigned char *)a
;
39 bp
= (unsigned char *)b
;
48 * Swap i_data and associated attributes between @inode1 and @inode2.
49 * This function is used for the primary swap between inode1 and inode2
50 * and also to revert this primary swap in case of errors.
52 * Therefore you have to make sure, that calling this method twice
53 * will revert all changes.
55 * @inode1: pointer to first inode
56 * @inode2: pointer to second inode
58 static void swap_inode_data(struct inode
*inode1
, struct inode
*inode2
)
61 struct ext4_inode_info
*ei1
;
62 struct ext4_inode_info
*ei2
;
67 memswap(&inode1
->i_flags
, &inode2
->i_flags
, sizeof(inode1
->i_flags
));
68 memswap(&inode1
->i_version
, &inode2
->i_version
,
69 sizeof(inode1
->i_version
));
70 memswap(&inode1
->i_blocks
, &inode2
->i_blocks
,
71 sizeof(inode1
->i_blocks
));
72 memswap(&inode1
->i_bytes
, &inode2
->i_bytes
, sizeof(inode1
->i_bytes
));
73 memswap(&inode1
->i_atime
, &inode2
->i_atime
, sizeof(inode1
->i_atime
));
74 memswap(&inode1
->i_mtime
, &inode2
->i_mtime
, sizeof(inode1
->i_mtime
));
76 memswap(ei1
->i_data
, ei2
->i_data
, sizeof(ei1
->i_data
));
77 memswap(&ei1
->i_flags
, &ei2
->i_flags
, sizeof(ei1
->i_flags
));
78 memswap(&ei1
->i_disksize
, &ei2
->i_disksize
, sizeof(ei1
->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
;
103 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
105 if (inode
->i_nlink
!= 1 || !S_ISREG(inode
->i_mode
))
108 if (!inode_owner_or_capable(inode
) || !capable(CAP_SYS_ADMIN
))
111 inode_bl
= ext4_iget(sb
, EXT4_BOOT_LOADER_INO
);
112 if (IS_ERR(inode_bl
))
113 return PTR_ERR(inode_bl
);
114 ei_bl
= EXT4_I(inode_bl
);
116 filemap_flush(inode
->i_mapping
);
117 filemap_flush(inode_bl
->i_mapping
);
119 /* Protect orig inodes against a truncate and make sure,
120 * that only 1 swap_inode_boot_loader is running. */
121 lock_two_nondirectories(inode
, inode_bl
);
123 truncate_inode_pages(&inode
->i_data
, 0);
124 truncate_inode_pages(&inode_bl
->i_data
, 0);
126 /* Wait for all existing dio workers */
127 ext4_inode_block_unlocked_dio(inode
);
128 ext4_inode_block_unlocked_dio(inode_bl
);
129 inode_dio_wait(inode
);
130 inode_dio_wait(inode_bl
);
132 handle
= ext4_journal_start(inode_bl
, EXT4_HT_MOVE_EXTENTS
, 2);
133 if (IS_ERR(handle
)) {
135 goto journal_err_out
;
138 /* Protect extent tree against block allocations via delalloc */
139 ext4_double_down_write_data_sem(inode
, inode_bl
);
141 if (inode_bl
->i_nlink
== 0) {
142 /* this inode has never been used as a BOOT_LOADER */
143 set_nlink(inode_bl
, 1);
144 i_uid_write(inode_bl
, 0);
145 i_gid_write(inode_bl
, 0);
146 inode_bl
->i_flags
= 0;
148 inode_bl
->i_version
= 1;
149 i_size_write(inode_bl
, 0);
150 inode_bl
->i_mode
= S_IFREG
;
151 if (ext4_has_feature_extents(sb
)) {
152 ext4_set_inode_flag(inode_bl
, EXT4_INODE_EXTENTS
);
153 ext4_ext_tree_init(handle
, inode_bl
);
155 memset(ei_bl
->i_data
, 0, sizeof(ei_bl
->i_data
));
158 swap_inode_data(inode
, inode_bl
);
160 inode
->i_ctime
= inode_bl
->i_ctime
= current_time(inode
);
162 spin_lock(&sbi
->s_next_gen_lock
);
163 inode
->i_generation
= sbi
->s_next_generation
++;
164 inode_bl
->i_generation
= sbi
->s_next_generation
++;
165 spin_unlock(&sbi
->s_next_gen_lock
);
167 ext4_discard_preallocations(inode
);
169 err
= ext4_mark_inode_dirty(handle
, inode
);
171 ext4_warning(inode
->i_sb
,
172 "couldn't mark inode #%lu dirty (err %d)",
174 /* Revert all changes: */
175 swap_inode_data(inode
, inode_bl
);
177 err
= ext4_mark_inode_dirty(handle
, inode_bl
);
179 ext4_warning(inode_bl
->i_sb
,
180 "couldn't mark inode #%lu dirty (err %d)",
181 inode_bl
->i_ino
, err
);
182 /* Revert all changes: */
183 swap_inode_data(inode
, inode_bl
);
184 ext4_mark_inode_dirty(handle
, inode
);
187 ext4_journal_stop(handle
);
188 ext4_double_up_write_data_sem(inode
, inode_bl
);
191 ext4_inode_resume_unlocked_dio(inode
);
192 ext4_inode_resume_unlocked_dio(inode_bl
);
193 unlock_two_nondirectories(inode
, inode_bl
);
198 #ifdef CONFIG_EXT4_FS_ENCRYPTION
199 static int uuid_is_zero(__u8 u
[16])
203 for (i
= 0; i
< 16; i
++)
210 static int ext4_ioctl_setflags(struct inode
*inode
,
213 struct ext4_inode_info
*ei
= EXT4_I(inode
);
214 handle_t
*handle
= NULL
;
215 int err
= -EPERM
, migrate
= 0;
216 struct ext4_iloc iloc
;
217 unsigned int oldflags
, mask
, i
;
220 /* Is it quota file? Do not allow user to mess with it */
221 if (IS_NOQUOTA(inode
))
224 oldflags
= ei
->i_flags
;
226 /* The JOURNAL_DATA flag is modifiable only by root */
227 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
230 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
231 * the relevant capability.
233 * This test looks nicer. Thanks to Pauline Middelink
235 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
236 if (!capable(CAP_LINUX_IMMUTABLE
))
241 * The JOURNAL_DATA flag can only be changed by
242 * the relevant capability.
244 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
245 if (!capable(CAP_SYS_RESOURCE
))
248 if ((flags
^ oldflags
) & EXT4_EXTENTS_FL
)
251 if (flags
& EXT4_EOFBLOCKS_FL
) {
252 /* we don't support adding EOFBLOCKS flag */
253 if (!(oldflags
& EXT4_EOFBLOCKS_FL
)) {
257 } else if (oldflags
& EXT4_EOFBLOCKS_FL
) {
258 err
= ext4_truncate(inode
);
263 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
264 if (IS_ERR(handle
)) {
265 err
= PTR_ERR(handle
);
269 ext4_handle_sync(handle
);
270 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
274 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) {
275 if (!(mask
& EXT4_FL_USER_MODIFIABLE
))
277 /* These flags get special treatment later */
278 if (mask
== EXT4_JOURNAL_DATA_FL
|| mask
== EXT4_EXTENTS_FL
)
281 ext4_set_inode_flag(inode
, i
);
283 ext4_clear_inode_flag(inode
, i
);
286 ext4_set_inode_flags(inode
);
287 inode
->i_ctime
= current_time(inode
);
289 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
291 ext4_journal_stop(handle
);
295 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
296 err
= ext4_change_inode_journal_flag(inode
, jflag
);
300 if (flags
& EXT4_EXTENTS_FL
)
301 err
= ext4_ext_migrate(inode
);
303 err
= ext4_ind_migrate(inode
);
311 static int ext4_ioctl_setproject(struct file
*filp
, __u32 projid
)
313 struct inode
*inode
= file_inode(filp
);
314 struct super_block
*sb
= inode
->i_sb
;
315 struct ext4_inode_info
*ei
= EXT4_I(inode
);
319 struct ext4_iloc iloc
;
320 struct ext4_inode
*raw_inode
;
321 struct dquot
*transfer_to
[MAXQUOTAS
] = { };
323 if (!ext4_has_feature_project(sb
)) {
324 if (projid
!= EXT4_DEF_PROJID
)
330 if (EXT4_INODE_SIZE(sb
) <= EXT4_GOOD_OLD_INODE_SIZE
)
333 kprojid
= make_kprojid(&init_user_ns
, (projid_t
)projid
);
335 if (projid_eq(kprojid
, EXT4_I(inode
)->i_projid
))
338 err
= mnt_want_write_file(filp
);
344 /* Is it quota file? Do not allow user to mess with it */
345 if (IS_NOQUOTA(inode
))
348 err
= ext4_get_inode_loc(inode
, &iloc
);
352 raw_inode
= ext4_raw_inode(&iloc
);
353 if (!EXT4_FITS_IN_INODE(raw_inode
, ei
, i_projid
)) {
360 dquot_initialize(inode
);
362 handle
= ext4_journal_start(inode
, EXT4_HT_QUOTA
,
363 EXT4_QUOTA_INIT_BLOCKS(sb
) +
364 EXT4_QUOTA_DEL_BLOCKS(sb
) + 3);
365 if (IS_ERR(handle
)) {
366 err
= PTR_ERR(handle
);
370 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
374 transfer_to
[PRJQUOTA
] = dqget(sb
, make_kqid_projid(kprojid
));
375 if (!IS_ERR(transfer_to
[PRJQUOTA
])) {
376 err
= __dquot_transfer(inode
, transfer_to
);
377 dqput(transfer_to
[PRJQUOTA
]);
382 EXT4_I(inode
)->i_projid
= kprojid
;
383 inode
->i_ctime
= current_time(inode
);
385 rc
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
389 ext4_journal_stop(handle
);
392 mnt_drop_write_file(filp
);
396 static int ext4_ioctl_setproject(struct file
*filp
, __u32 projid
)
398 if (projid
!= EXT4_DEF_PROJID
)
404 /* Transfer internal flags to xflags */
405 static inline __u32
ext4_iflags_to_xflags(unsigned long iflags
)
409 if (iflags
& EXT4_SYNC_FL
)
410 xflags
|= FS_XFLAG_SYNC
;
411 if (iflags
& EXT4_IMMUTABLE_FL
)
412 xflags
|= FS_XFLAG_IMMUTABLE
;
413 if (iflags
& EXT4_APPEND_FL
)
414 xflags
|= FS_XFLAG_APPEND
;
415 if (iflags
& EXT4_NODUMP_FL
)
416 xflags
|= FS_XFLAG_NODUMP
;
417 if (iflags
& EXT4_NOATIME_FL
)
418 xflags
|= FS_XFLAG_NOATIME
;
419 if (iflags
& EXT4_PROJINHERIT_FL
)
420 xflags
|= FS_XFLAG_PROJINHERIT
;
424 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
425 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
426 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
428 /* Transfer xflags flags to internal */
429 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags
)
431 unsigned long iflags
= 0;
433 if (xflags
& FS_XFLAG_SYNC
)
434 iflags
|= EXT4_SYNC_FL
;
435 if (xflags
& FS_XFLAG_IMMUTABLE
)
436 iflags
|= EXT4_IMMUTABLE_FL
;
437 if (xflags
& FS_XFLAG_APPEND
)
438 iflags
|= EXT4_APPEND_FL
;
439 if (xflags
& FS_XFLAG_NODUMP
)
440 iflags
|= EXT4_NODUMP_FL
;
441 if (xflags
& FS_XFLAG_NOATIME
)
442 iflags
|= EXT4_NOATIME_FL
;
443 if (xflags
& FS_XFLAG_PROJINHERIT
)
444 iflags
|= EXT4_PROJINHERIT_FL
;
449 static int ext4_shutdown(struct super_block
*sb
, unsigned long arg
)
451 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
454 if (!capable(CAP_SYS_ADMIN
))
457 if (get_user(flags
, (__u32 __user
*)arg
))
460 if (flags
> EXT4_GOING_FLAGS_NOLOGFLUSH
)
463 if (ext4_forced_shutdown(sbi
))
466 ext4_msg(sb
, KERN_ALERT
, "shut down requested (%d)", flags
);
469 case EXT4_GOING_FLAGS_DEFAULT
:
470 freeze_bdev(sb
->s_bdev
);
471 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
472 thaw_bdev(sb
->s_bdev
, sb
);
474 case EXT4_GOING_FLAGS_LOGFLUSH
:
475 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
476 if (sbi
->s_journal
&& !is_journal_aborted(sbi
->s_journal
)) {
477 (void) ext4_force_commit(sb
);
478 jbd2_journal_abort(sbi
->s_journal
, 0);
481 case EXT4_GOING_FLAGS_NOLOGFLUSH
:
482 set_bit(EXT4_FLAGS_SHUTDOWN
, &sbi
->s_ext4_flags
);
483 if (sbi
->s_journal
&& !is_journal_aborted(sbi
->s_journal
)) {
485 jbd2_journal_abort(sbi
->s_journal
, 0);
491 clear_opt(sb
, DISCARD
);
495 struct getfsmap_info
{
496 struct super_block
*gi_sb
;
497 struct fsmap_head __user
*gi_data
;
502 static int ext4_getfsmap_format(struct ext4_fsmap
*xfm
, void *priv
)
504 struct getfsmap_info
*info
= priv
;
507 trace_ext4_getfsmap_mapping(info
->gi_sb
, xfm
);
509 info
->gi_last_flags
= xfm
->fmr_flags
;
510 ext4_fsmap_from_internal(info
->gi_sb
, &fm
, xfm
);
511 if (copy_to_user(&info
->gi_data
->fmh_recs
[info
->gi_idx
++], &fm
,
512 sizeof(struct fsmap
)))
518 static int ext4_ioc_getfsmap(struct super_block
*sb
,
519 struct fsmap_head __user
*arg
)
521 struct getfsmap_info info
= {0};
522 struct ext4_fsmap_head xhead
= {0};
523 struct fsmap_head head
;
524 bool aborted
= false;
527 if (copy_from_user(&head
, arg
, sizeof(struct fsmap_head
)))
529 if (memchr_inv(head
.fmh_reserved
, 0, sizeof(head
.fmh_reserved
)) ||
530 memchr_inv(head
.fmh_keys
[0].fmr_reserved
, 0,
531 sizeof(head
.fmh_keys
[0].fmr_reserved
)) ||
532 memchr_inv(head
.fmh_keys
[1].fmr_reserved
, 0,
533 sizeof(head
.fmh_keys
[1].fmr_reserved
)))
536 * ext4 doesn't report file extents at all, so the only valid
537 * file offsets are the magic ones (all zeroes or all ones).
539 if (head
.fmh_keys
[0].fmr_offset
||
540 (head
.fmh_keys
[1].fmr_offset
!= 0 &&
541 head
.fmh_keys
[1].fmr_offset
!= -1ULL))
544 xhead
.fmh_iflags
= head
.fmh_iflags
;
545 xhead
.fmh_count
= head
.fmh_count
;
546 ext4_fsmap_to_internal(sb
, &xhead
.fmh_keys
[0], &head
.fmh_keys
[0]);
547 ext4_fsmap_to_internal(sb
, &xhead
.fmh_keys
[1], &head
.fmh_keys
[1]);
549 trace_ext4_getfsmap_low_key(sb
, &xhead
.fmh_keys
[0]);
550 trace_ext4_getfsmap_high_key(sb
, &xhead
.fmh_keys
[1]);
554 error
= ext4_getfsmap(sb
, &xhead
, ext4_getfsmap_format
, &info
);
555 if (error
== EXT4_QUERY_RANGE_ABORT
) {
561 /* If we didn't abort, set the "last" flag in the last fmx */
562 if (!aborted
&& info
.gi_idx
) {
563 info
.gi_last_flags
|= FMR_OF_LAST
;
564 if (copy_to_user(&info
.gi_data
->fmh_recs
[info
.gi_idx
- 1].fmr_flags
,
566 sizeof(info
.gi_last_flags
)))
570 /* copy back header */
571 head
.fmh_entries
= xhead
.fmh_entries
;
572 head
.fmh_oflags
= xhead
.fmh_oflags
;
573 if (copy_to_user(arg
, &head
, sizeof(struct fsmap_head
)))
579 long ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
581 struct inode
*inode
= file_inode(filp
);
582 struct super_block
*sb
= inode
->i_sb
;
583 struct ext4_inode_info
*ei
= EXT4_I(inode
);
586 ext4_debug("cmd = %u, arg = %lu\n", cmd
, arg
);
589 case FS_IOC_GETFSMAP
:
590 return ext4_ioc_getfsmap(sb
, (void __user
*)arg
);
591 case EXT4_IOC_GETFLAGS
:
592 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
593 return put_user(flags
, (int __user
*) arg
);
594 case EXT4_IOC_SETFLAGS
: {
597 if (!inode_owner_or_capable(inode
))
600 if (get_user(flags
, (int __user
*) arg
))
603 if (flags
& ~EXT4_FL_USER_VISIBLE
)
606 * chattr(1) grabs flags via GETFLAGS, modifies the result and
607 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
608 * more restrictive than just silently masking off visible but
609 * not settable flags as we always did.
611 flags
&= EXT4_FL_USER_MODIFIABLE
;
612 if (ext4_mask_flags(inode
->i_mode
, flags
) != flags
)
615 err
= mnt_want_write_file(filp
);
620 err
= ext4_ioctl_setflags(inode
, flags
);
622 mnt_drop_write_file(filp
);
625 case EXT4_IOC_GETVERSION
:
626 case EXT4_IOC_GETVERSION_OLD
:
627 return put_user(inode
->i_generation
, (int __user
*) arg
);
628 case EXT4_IOC_SETVERSION
:
629 case EXT4_IOC_SETVERSION_OLD
: {
631 struct ext4_iloc iloc
;
635 if (!inode_owner_or_capable(inode
))
638 if (ext4_has_metadata_csum(inode
->i_sb
)) {
639 ext4_warning(sb
, "Setting inode version is not "
640 "supported with metadata_csum enabled.");
644 err
= mnt_want_write_file(filp
);
647 if (get_user(generation
, (int __user
*) arg
)) {
653 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
654 if (IS_ERR(handle
)) {
655 err
= PTR_ERR(handle
);
658 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
660 inode
->i_ctime
= current_time(inode
);
661 inode
->i_generation
= generation
;
662 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
664 ext4_journal_stop(handle
);
669 mnt_drop_write_file(filp
);
672 case EXT4_IOC_GROUP_EXTEND
: {
673 ext4_fsblk_t n_blocks_count
;
676 err
= ext4_resize_begin(sb
);
680 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
682 goto group_extend_out
;
685 if (ext4_has_feature_bigalloc(sb
)) {
686 ext4_msg(sb
, KERN_ERR
,
687 "Online resizing not supported with bigalloc");
689 goto group_extend_out
;
692 err
= mnt_want_write_file(filp
);
694 goto group_extend_out
;
696 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
697 if (EXT4_SB(sb
)->s_journal
) {
698 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
699 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
700 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
704 mnt_drop_write_file(filp
);
710 case EXT4_IOC_MOVE_EXT
: {
711 struct move_extent me
;
715 if (!(filp
->f_mode
& FMODE_READ
) ||
716 !(filp
->f_mode
& FMODE_WRITE
))
719 if (copy_from_user(&me
,
720 (struct move_extent __user
*)arg
, sizeof(me
)))
724 donor
= fdget(me
.donor_fd
);
728 if (!(donor
.file
->f_mode
& FMODE_WRITE
)) {
733 if (ext4_has_feature_bigalloc(sb
)) {
734 ext4_msg(sb
, KERN_ERR
,
735 "Online defrag not supported with bigalloc");
738 } else if (IS_DAX(inode
)) {
739 ext4_msg(sb
, KERN_ERR
,
740 "Online defrag not supported with DAX");
745 err
= mnt_want_write_file(filp
);
749 err
= ext4_move_extents(filp
, donor
.file
, me
.orig_start
,
750 me
.donor_start
, me
.len
, &me
.moved_len
);
751 mnt_drop_write_file(filp
);
753 if (copy_to_user((struct move_extent __user
*)arg
,
761 case EXT4_IOC_GROUP_ADD
: {
762 struct ext4_new_group_data input
;
765 err
= ext4_resize_begin(sb
);
769 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
775 if (ext4_has_feature_bigalloc(sb
)) {
776 ext4_msg(sb
, KERN_ERR
,
777 "Online resizing not supported with bigalloc");
782 err
= mnt_want_write_file(filp
);
786 err
= ext4_group_add(sb
, &input
);
787 if (EXT4_SB(sb
)->s_journal
) {
788 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
789 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
790 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
794 mnt_drop_write_file(filp
);
795 if (!err
&& ext4_has_group_desc_csum(sb
) &&
796 test_opt(sb
, INIT_INODE_TABLE
))
797 err
= ext4_register_li_request(sb
, input
.group
);
803 case EXT4_IOC_MIGRATE
:
806 if (!inode_owner_or_capable(inode
))
809 err
= mnt_want_write_file(filp
);
813 * inode_mutex prevent write and truncate on the file.
814 * Read still goes through. We take i_data_sem in
815 * ext4_ext_swap_inode_data before we switch the
816 * inode format to prevent read.
819 err
= ext4_ext_migrate(inode
);
820 inode_unlock((inode
));
821 mnt_drop_write_file(filp
);
825 case EXT4_IOC_ALLOC_DA_BLKS
:
828 if (!inode_owner_or_capable(inode
))
831 err
= mnt_want_write_file(filp
);
834 err
= ext4_alloc_da_blocks(inode
);
835 mnt_drop_write_file(filp
);
839 case EXT4_IOC_SWAP_BOOT
:
842 if (!(filp
->f_mode
& FMODE_WRITE
))
844 err
= mnt_want_write_file(filp
);
847 err
= swap_inode_boot_loader(sb
, inode
);
848 mnt_drop_write_file(filp
);
852 case EXT4_IOC_RESIZE_FS
: {
853 ext4_fsblk_t n_blocks_count
;
854 int err
= 0, err2
= 0;
855 ext4_group_t o_group
= EXT4_SB(sb
)->s_groups_count
;
857 if (ext4_has_feature_bigalloc(sb
)) {
858 ext4_msg(sb
, KERN_ERR
,
859 "Online resizing not (yet) supported with bigalloc");
863 if (copy_from_user(&n_blocks_count
, (__u64 __user
*)arg
,
868 err
= ext4_resize_begin(sb
);
872 err
= mnt_want_write_file(filp
);
876 err
= ext4_resize_fs(sb
, n_blocks_count
);
877 if (EXT4_SB(sb
)->s_journal
) {
878 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
879 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
880 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
884 mnt_drop_write_file(filp
);
885 if (!err
&& (o_group
> EXT4_SB(sb
)->s_groups_count
) &&
886 ext4_has_group_desc_csum(sb
) &&
887 test_opt(sb
, INIT_INODE_TABLE
))
888 err
= ext4_register_li_request(sb
, o_group
);
897 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
898 struct fstrim_range range
;
901 if (!capable(CAP_SYS_ADMIN
))
904 if (!blk_queue_discard(q
))
907 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
911 range
.minlen
= max((unsigned int)range
.minlen
,
912 q
->limits
.discard_granularity
);
913 ret
= ext4_trim_fs(sb
, &range
);
917 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
923 case EXT4_IOC_PRECACHE_EXTENTS
:
924 return ext4_ext_precache(inode
);
926 case EXT4_IOC_SET_ENCRYPTION_POLICY
:
927 if (!ext4_has_feature_encrypt(sb
))
929 return fscrypt_ioctl_set_policy(filp
, (const void __user
*)arg
);
931 case EXT4_IOC_GET_ENCRYPTION_PWSALT
: {
932 #ifdef CONFIG_EXT4_FS_ENCRYPTION
934 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
937 if (!ext4_has_feature_encrypt(sb
))
939 if (uuid_is_zero(sbi
->s_es
->s_encrypt_pw_salt
)) {
940 err
= mnt_want_write_file(filp
);
943 handle
= ext4_journal_start_sb(sb
, EXT4_HT_MISC
, 1);
944 if (IS_ERR(handle
)) {
945 err
= PTR_ERR(handle
);
946 goto pwsalt_err_exit
;
948 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
950 goto pwsalt_err_journal
;
951 generate_random_uuid(sbi
->s_es
->s_encrypt_pw_salt
);
952 err
= ext4_handle_dirty_metadata(handle
, NULL
,
955 err2
= ext4_journal_stop(handle
);
959 mnt_drop_write_file(filp
);
963 if (copy_to_user((void __user
*) arg
,
964 sbi
->s_es
->s_encrypt_pw_salt
, 16))
971 case EXT4_IOC_GET_ENCRYPTION_POLICY
:
972 return fscrypt_ioctl_get_policy(filp
, (void __user
*)arg
);
974 case EXT4_IOC_FSGETXATTR
:
978 memset(&fa
, 0, sizeof(struct fsxattr
));
979 fa
.fsx_xflags
= ext4_iflags_to_xflags(ei
->i_flags
& EXT4_FL_USER_VISIBLE
);
981 if (ext4_has_feature_project(inode
->i_sb
)) {
982 fa
.fsx_projid
= (__u32
)from_kprojid(&init_user_ns
,
983 EXT4_I(inode
)->i_projid
);
986 if (copy_to_user((struct fsxattr __user
*)arg
,
991 case EXT4_IOC_FSSETXATTR
:
996 if (copy_from_user(&fa
, (struct fsxattr __user
*)arg
,
1000 /* Make sure caller has proper permission */
1001 if (!inode_owner_or_capable(inode
))
1004 if (fa
.fsx_xflags
& ~EXT4_SUPPORTED_FS_XFLAGS
)
1007 flags
= ext4_xflags_to_iflags(fa
.fsx_xflags
);
1008 if (ext4_mask_flags(inode
->i_mode
, flags
) != flags
)
1011 err
= mnt_want_write_file(filp
);
1016 flags
= (ei
->i_flags
& ~EXT4_FL_XFLAG_VISIBLE
) |
1017 (flags
& EXT4_FL_XFLAG_VISIBLE
);
1018 err
= ext4_ioctl_setflags(inode
, flags
);
1019 inode_unlock(inode
);
1020 mnt_drop_write_file(filp
);
1024 err
= ext4_ioctl_setproject(filp
, fa
.fsx_projid
);
1030 case EXT4_IOC_SHUTDOWN
:
1031 return ext4_shutdown(sb
, arg
);
1037 #ifdef CONFIG_COMPAT
1038 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1040 /* These are just misnamed, they actually get/put from/to user an int */
1042 case EXT4_IOC32_GETFLAGS
:
1043 cmd
= EXT4_IOC_GETFLAGS
;
1045 case EXT4_IOC32_SETFLAGS
:
1046 cmd
= EXT4_IOC_SETFLAGS
;
1048 case EXT4_IOC32_GETVERSION
:
1049 cmd
= EXT4_IOC_GETVERSION
;
1051 case EXT4_IOC32_SETVERSION
:
1052 cmd
= EXT4_IOC_SETVERSION
;
1054 case EXT4_IOC32_GROUP_EXTEND
:
1055 cmd
= EXT4_IOC_GROUP_EXTEND
;
1057 case EXT4_IOC32_GETVERSION_OLD
:
1058 cmd
= EXT4_IOC_GETVERSION_OLD
;
1060 case EXT4_IOC32_SETVERSION_OLD
:
1061 cmd
= EXT4_IOC_SETVERSION_OLD
;
1063 case EXT4_IOC32_GETRSVSZ
:
1064 cmd
= EXT4_IOC_GETRSVSZ
;
1066 case EXT4_IOC32_SETRSVSZ
:
1067 cmd
= EXT4_IOC_SETRSVSZ
;
1069 case EXT4_IOC32_GROUP_ADD
: {
1070 struct compat_ext4_new_group_input __user
*uinput
;
1071 struct ext4_new_group_input input
;
1072 mm_segment_t old_fs
;
1075 uinput
= compat_ptr(arg
);
1076 err
= get_user(input
.group
, &uinput
->group
);
1077 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
1078 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
1079 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
1080 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
1081 err
|= get_user(input
.reserved_blocks
,
1082 &uinput
->reserved_blocks
);
1087 err
= ext4_ioctl(file
, EXT4_IOC_GROUP_ADD
,
1088 (unsigned long) &input
);
1092 case EXT4_IOC_MOVE_EXT
:
1093 case EXT4_IOC_RESIZE_FS
:
1094 case EXT4_IOC_PRECACHE_EXTENTS
:
1095 case EXT4_IOC_SET_ENCRYPTION_POLICY
:
1096 case EXT4_IOC_GET_ENCRYPTION_PWSALT
:
1097 case EXT4_IOC_GET_ENCRYPTION_POLICY
:
1098 case EXT4_IOC_SHUTDOWN
:
1099 case FS_IOC_GETFSMAP
:
1102 return -ENOIOCTLCMD
;
1104 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));