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 <asm/uaccess.h>
17 #include "ext4_jbd2.h"
20 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
23 * Swap memory between @a and @b for @len bytes.
25 * @a: pointer to first memory area
26 * @b: pointer to second memory area
27 * @len: number of bytes to swap
30 static void memswap(void *a
, void *b
, size_t len
)
32 unsigned char *ap
, *bp
;
35 ap
= (unsigned char *)a
;
36 bp
= (unsigned char *)b
;
47 * Swap i_data and associated attributes between @inode1 and @inode2.
48 * This function is used for the primary swap between inode1 and inode2
49 * and also to revert this primary swap in case of errors.
51 * Therefore you have to make sure, that calling this method twice
52 * will revert all changes.
54 * @inode1: pointer to first inode
55 * @inode2: pointer to second inode
57 static void swap_inode_data(struct inode
*inode1
, struct inode
*inode2
)
60 struct ext4_inode_info
*ei1
;
61 struct ext4_inode_info
*ei2
;
66 memswap(&inode1
->i_flags
, &inode2
->i_flags
, sizeof(inode1
->i_flags
));
67 memswap(&inode1
->i_version
, &inode2
->i_version
,
68 sizeof(inode1
->i_version
));
69 memswap(&inode1
->i_blocks
, &inode2
->i_blocks
,
70 sizeof(inode1
->i_blocks
));
71 memswap(&inode1
->i_bytes
, &inode2
->i_bytes
, sizeof(inode1
->i_bytes
));
72 memswap(&inode1
->i_atime
, &inode2
->i_atime
, sizeof(inode1
->i_atime
));
73 memswap(&inode1
->i_mtime
, &inode2
->i_mtime
, sizeof(inode1
->i_mtime
));
75 memswap(ei1
->i_data
, ei2
->i_data
, sizeof(ei1
->i_data
));
76 memswap(&ei1
->i_flags
, &ei2
->i_flags
, sizeof(ei1
->i_flags
));
77 memswap(&ei1
->i_disksize
, &ei2
->i_disksize
, sizeof(ei1
->i_disksize
));
78 ext4_es_remove_extent(inode1
, 0, EXT_MAX_BLOCKS
);
79 ext4_es_remove_extent(inode2
, 0, EXT_MAX_BLOCKS
);
81 isize
= i_size_read(inode1
);
82 i_size_write(inode1
, i_size_read(inode2
));
83 i_size_write(inode2
, isize
);
87 * Swap the information from the given @inode and the inode
88 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
89 * important fields of the inodes.
91 * @sb: the super block of the filesystem
92 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
95 static long swap_inode_boot_loader(struct super_block
*sb
,
100 struct inode
*inode_bl
;
101 struct ext4_inode_info
*ei_bl
;
102 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
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_INCOMPAT_FEATURE(sb
,
151 EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
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
= ext4_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 long ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
200 struct inode
*inode
= file_inode(filp
);
201 struct super_block
*sb
= inode
->i_sb
;
202 struct ext4_inode_info
*ei
= EXT4_I(inode
);
205 ext4_debug("cmd = %u, arg = %lu\n", cmd
, arg
);
208 case EXT4_IOC_GETFLAGS
:
209 ext4_get_inode_flags(ei
);
210 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
211 return put_user(flags
, (int __user
*) arg
);
212 case EXT4_IOC_SETFLAGS
: {
213 handle_t
*handle
= NULL
;
214 int err
, migrate
= 0;
215 struct ext4_iloc iloc
;
216 unsigned int oldflags
, mask
, i
;
219 if (!inode_owner_or_capable(inode
))
222 if (get_user(flags
, (int __user
*) arg
))
225 err
= mnt_want_write_file(filp
);
229 flags
= ext4_mask_flags(inode
->i_mode
, flags
);
232 mutex_lock(&inode
->i_mutex
);
233 /* Is it quota file? Do not allow user to mess with it */
234 if (IS_NOQUOTA(inode
))
237 oldflags
= ei
->i_flags
;
239 /* The JOURNAL_DATA flag is modifiable only by root */
240 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
243 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
244 * the relevant capability.
246 * This test looks nicer. Thanks to Pauline Middelink
248 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
249 if (!capable(CAP_LINUX_IMMUTABLE
))
254 * The JOURNAL_DATA flag can only be changed by
255 * the relevant capability.
257 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
258 if (!capable(CAP_SYS_RESOURCE
))
261 if ((flags
^ oldflags
) & EXT4_EXTENTS_FL
)
264 if (flags
& EXT4_EOFBLOCKS_FL
) {
265 /* we don't support adding EOFBLOCKS flag */
266 if (!(oldflags
& EXT4_EOFBLOCKS_FL
)) {
270 } else if (oldflags
& EXT4_EOFBLOCKS_FL
)
271 ext4_truncate(inode
);
273 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
274 if (IS_ERR(handle
)) {
275 err
= PTR_ERR(handle
);
279 ext4_handle_sync(handle
);
280 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
284 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) {
285 if (!(mask
& EXT4_FL_USER_MODIFIABLE
))
288 ext4_set_inode_flag(inode
, i
);
290 ext4_clear_inode_flag(inode
, i
);
293 ext4_set_inode_flags(inode
);
294 inode
->i_ctime
= ext4_current_time(inode
);
296 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
298 ext4_journal_stop(handle
);
302 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
303 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
);
314 mutex_unlock(&inode
->i_mutex
);
315 mnt_drop_write_file(filp
);
318 case EXT4_IOC_GETVERSION
:
319 case EXT4_IOC_GETVERSION_OLD
:
320 return put_user(inode
->i_generation
, (int __user
*) arg
);
321 case EXT4_IOC_SETVERSION
:
322 case EXT4_IOC_SETVERSION_OLD
: {
324 struct ext4_iloc iloc
;
328 if (!inode_owner_or_capable(inode
))
331 if (ext4_has_metadata_csum(inode
->i_sb
)) {
332 ext4_warning(sb
, "Setting inode version is not "
333 "supported with metadata_csum enabled.");
337 err
= mnt_want_write_file(filp
);
340 if (get_user(generation
, (int __user
*) arg
)) {
345 mutex_lock(&inode
->i_mutex
);
346 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
347 if (IS_ERR(handle
)) {
348 err
= PTR_ERR(handle
);
351 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
353 inode
->i_ctime
= ext4_current_time(inode
);
354 inode
->i_generation
= generation
;
355 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
357 ext4_journal_stop(handle
);
360 mutex_unlock(&inode
->i_mutex
);
362 mnt_drop_write_file(filp
);
365 case EXT4_IOC_GROUP_EXTEND
: {
366 ext4_fsblk_t n_blocks_count
;
369 err
= ext4_resize_begin(sb
);
373 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
375 goto group_extend_out
;
378 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
379 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
380 ext4_msg(sb
, KERN_ERR
,
381 "Online resizing not supported with bigalloc");
383 goto group_extend_out
;
386 err
= mnt_want_write_file(filp
);
388 goto group_extend_out
;
390 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
391 if (EXT4_SB(sb
)->s_journal
) {
392 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
393 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
394 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
398 mnt_drop_write_file(filp
);
404 case EXT4_IOC_MOVE_EXT
: {
405 struct move_extent me
;
409 if (!(filp
->f_mode
& FMODE_READ
) ||
410 !(filp
->f_mode
& FMODE_WRITE
))
413 if (copy_from_user(&me
,
414 (struct move_extent __user
*)arg
, sizeof(me
)))
418 donor
= fdget(me
.donor_fd
);
422 if (!(donor
.file
->f_mode
& FMODE_WRITE
)) {
427 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
428 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
429 ext4_msg(sb
, KERN_ERR
,
430 "Online defrag not supported with bigalloc");
435 err
= mnt_want_write_file(filp
);
439 err
= ext4_move_extents(filp
, donor
.file
, me
.orig_start
,
440 me
.donor_start
, me
.len
, &me
.moved_len
);
441 mnt_drop_write_file(filp
);
443 if (copy_to_user((struct move_extent __user
*)arg
,
451 case EXT4_IOC_GROUP_ADD
: {
452 struct ext4_new_group_data input
;
455 err
= ext4_resize_begin(sb
);
459 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
465 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
466 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
467 ext4_msg(sb
, KERN_ERR
,
468 "Online resizing not supported with bigalloc");
473 err
= mnt_want_write_file(filp
);
477 err
= ext4_group_add(sb
, &input
);
478 if (EXT4_SB(sb
)->s_journal
) {
479 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
480 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
481 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
485 mnt_drop_write_file(filp
);
486 if (!err
&& ext4_has_group_desc_csum(sb
) &&
487 test_opt(sb
, INIT_INODE_TABLE
))
488 err
= ext4_register_li_request(sb
, input
.group
);
494 case EXT4_IOC_MIGRATE
:
497 if (!inode_owner_or_capable(inode
))
500 err
= mnt_want_write_file(filp
);
504 * inode_mutex prevent write and truncate on the file.
505 * Read still goes through. We take i_data_sem in
506 * ext4_ext_swap_inode_data before we switch the
507 * inode format to prevent read.
509 mutex_lock(&(inode
->i_mutex
));
510 err
= ext4_ext_migrate(inode
);
511 mutex_unlock(&(inode
->i_mutex
));
512 mnt_drop_write_file(filp
);
516 case EXT4_IOC_ALLOC_DA_BLKS
:
519 if (!inode_owner_or_capable(inode
))
522 err
= mnt_want_write_file(filp
);
525 err
= ext4_alloc_da_blocks(inode
);
526 mnt_drop_write_file(filp
);
530 case EXT4_IOC_SWAP_BOOT
:
533 if (!(filp
->f_mode
& FMODE_WRITE
))
535 err
= mnt_want_write_file(filp
);
538 err
= swap_inode_boot_loader(sb
, inode
);
539 mnt_drop_write_file(filp
);
543 case EXT4_IOC_RESIZE_FS
: {
544 ext4_fsblk_t n_blocks_count
;
545 int err
= 0, err2
= 0;
546 ext4_group_t o_group
= EXT4_SB(sb
)->s_groups_count
;
548 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
549 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
550 ext4_msg(sb
, KERN_ERR
,
551 "Online resizing not (yet) supported with bigalloc");
555 if (copy_from_user(&n_blocks_count
, (__u64 __user
*)arg
,
560 err
= ext4_resize_begin(sb
);
564 err
= mnt_want_write_file(filp
);
568 err
= ext4_resize_fs(sb
, n_blocks_count
);
569 if (EXT4_SB(sb
)->s_journal
) {
570 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
571 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
572 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
576 mnt_drop_write_file(filp
);
577 if (!err
&& (o_group
> EXT4_SB(sb
)->s_groups_count
) &&
578 ext4_has_group_desc_csum(sb
) &&
579 test_opt(sb
, INIT_INODE_TABLE
))
580 err
= ext4_register_li_request(sb
, o_group
);
589 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
590 struct fstrim_range range
;
593 if (!capable(CAP_SYS_ADMIN
))
596 if (!blk_queue_discard(q
))
599 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
603 range
.minlen
= max((unsigned int)range
.minlen
,
604 q
->limits
.discard_granularity
);
605 ret
= ext4_trim_fs(sb
, &range
);
609 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
615 case EXT4_IOC_PRECACHE_EXTENTS
:
616 return ext4_ext_precache(inode
);
624 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
626 /* These are just misnamed, they actually get/put from/to user an int */
628 case EXT4_IOC32_GETFLAGS
:
629 cmd
= EXT4_IOC_GETFLAGS
;
631 case EXT4_IOC32_SETFLAGS
:
632 cmd
= EXT4_IOC_SETFLAGS
;
634 case EXT4_IOC32_GETVERSION
:
635 cmd
= EXT4_IOC_GETVERSION
;
637 case EXT4_IOC32_SETVERSION
:
638 cmd
= EXT4_IOC_SETVERSION
;
640 case EXT4_IOC32_GROUP_EXTEND
:
641 cmd
= EXT4_IOC_GROUP_EXTEND
;
643 case EXT4_IOC32_GETVERSION_OLD
:
644 cmd
= EXT4_IOC_GETVERSION_OLD
;
646 case EXT4_IOC32_SETVERSION_OLD
:
647 cmd
= EXT4_IOC_SETVERSION_OLD
;
649 case EXT4_IOC32_GETRSVSZ
:
650 cmd
= EXT4_IOC_GETRSVSZ
;
652 case EXT4_IOC32_SETRSVSZ
:
653 cmd
= EXT4_IOC_SETRSVSZ
;
655 case EXT4_IOC32_GROUP_ADD
: {
656 struct compat_ext4_new_group_input __user
*uinput
;
657 struct ext4_new_group_input input
;
661 uinput
= compat_ptr(arg
);
662 err
= get_user(input
.group
, &uinput
->group
);
663 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
664 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
665 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
666 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
667 err
|= get_user(input
.reserved_blocks
,
668 &uinput
->reserved_blocks
);
673 err
= ext4_ioctl(file
, EXT4_IOC_GROUP_ADD
,
674 (unsigned long) &input
);
678 case EXT4_IOC_MOVE_EXT
:
680 case EXT4_IOC_RESIZE_FS
:
681 case EXT4_IOC_PRECACHE_EXTENTS
:
686 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));