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/jbd2.h>
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 <asm/uaccess.h>
18 #include "ext4_jbd2.h"
21 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
23 long ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
25 struct inode
*inode
= file_inode(filp
);
26 struct super_block
*sb
= inode
->i_sb
;
27 struct ext4_inode_info
*ei
= EXT4_I(inode
);
30 ext4_debug("cmd = %u, arg = %lu\n", cmd
, arg
);
33 case EXT4_IOC_GETFLAGS
:
34 ext4_get_inode_flags(ei
);
35 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
36 return put_user(flags
, (int __user
*) arg
);
37 case EXT4_IOC_SETFLAGS
: {
38 handle_t
*handle
= NULL
;
40 struct ext4_iloc iloc
;
41 unsigned int oldflags
, mask
, i
;
44 if (!inode_owner_or_capable(inode
))
47 if (get_user(flags
, (int __user
*) arg
))
50 err
= mnt_want_write_file(filp
);
54 flags
= ext4_mask_flags(inode
->i_mode
, flags
);
57 mutex_lock(&inode
->i_mutex
);
58 /* Is it quota file? Do not allow user to mess with it */
59 if (IS_NOQUOTA(inode
))
62 oldflags
= ei
->i_flags
;
64 /* The JOURNAL_DATA flag is modifiable only by root */
65 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
68 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
69 * the relevant capability.
71 * This test looks nicer. Thanks to Pauline Middelink
73 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
74 if (!capable(CAP_LINUX_IMMUTABLE
))
79 * The JOURNAL_DATA flag can only be changed by
80 * the relevant capability.
82 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
83 if (!capable(CAP_SYS_RESOURCE
))
86 if (oldflags
& EXT4_EXTENTS_FL
) {
87 /* We don't support clearning extent flags */
88 if (!(flags
& EXT4_EXTENTS_FL
)) {
92 } else if (flags
& EXT4_EXTENTS_FL
) {
93 /* migrate the file */
95 flags
&= ~EXT4_EXTENTS_FL
;
98 if (flags
& EXT4_EOFBLOCKS_FL
) {
99 /* we don't support adding EOFBLOCKS flag */
100 if (!(oldflags
& EXT4_EOFBLOCKS_FL
)) {
104 } else if (oldflags
& EXT4_EOFBLOCKS_FL
)
105 ext4_truncate(inode
);
107 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
108 if (IS_ERR(handle
)) {
109 err
= PTR_ERR(handle
);
113 ext4_handle_sync(handle
);
114 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
118 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) {
119 if (!(mask
& EXT4_FL_USER_MODIFIABLE
))
122 ext4_set_inode_flag(inode
, i
);
124 ext4_clear_inode_flag(inode
, i
);
127 ext4_set_inode_flags(inode
);
128 inode
->i_ctime
= ext4_current_time(inode
);
130 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
132 ext4_journal_stop(handle
);
136 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
137 err
= ext4_change_inode_journal_flag(inode
, jflag
);
141 err
= ext4_ext_migrate(inode
);
143 mutex_unlock(&inode
->i_mutex
);
144 mnt_drop_write_file(filp
);
147 case EXT4_IOC_GETVERSION
:
148 case EXT4_IOC_GETVERSION_OLD
:
149 return put_user(inode
->i_generation
, (int __user
*) arg
);
150 case EXT4_IOC_SETVERSION
:
151 case EXT4_IOC_SETVERSION_OLD
: {
153 struct ext4_iloc iloc
;
157 if (!inode_owner_or_capable(inode
))
160 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
161 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
)) {
162 ext4_warning(sb
, "Setting inode version is not "
163 "supported with metadata_csum enabled.");
167 err
= mnt_want_write_file(filp
);
170 if (get_user(generation
, (int __user
*) arg
)) {
175 mutex_lock(&inode
->i_mutex
);
176 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
177 if (IS_ERR(handle
)) {
178 err
= PTR_ERR(handle
);
181 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
183 inode
->i_ctime
= ext4_current_time(inode
);
184 inode
->i_generation
= generation
;
185 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
187 ext4_journal_stop(handle
);
190 mutex_unlock(&inode
->i_mutex
);
192 mnt_drop_write_file(filp
);
195 case EXT4_IOC_GROUP_EXTEND
: {
196 ext4_fsblk_t n_blocks_count
;
199 err
= ext4_resize_begin(sb
);
203 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
205 goto group_extend_out
;
208 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
209 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
210 ext4_msg(sb
, KERN_ERR
,
211 "Online resizing not supported with bigalloc");
213 goto group_extend_out
;
216 err
= mnt_want_write_file(filp
);
218 goto group_extend_out
;
220 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
221 if (EXT4_SB(sb
)->s_journal
) {
222 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
223 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
224 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
228 mnt_drop_write_file(filp
);
234 case EXT4_IOC_MOVE_EXT
: {
235 struct move_extent me
;
239 if (!(filp
->f_mode
& FMODE_READ
) ||
240 !(filp
->f_mode
& FMODE_WRITE
))
243 if (copy_from_user(&me
,
244 (struct move_extent __user
*)arg
, sizeof(me
)))
248 donor
= fdget(me
.donor_fd
);
252 if (!(donor
.file
->f_mode
& FMODE_WRITE
)) {
257 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
258 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
259 ext4_msg(sb
, KERN_ERR
,
260 "Online defrag not supported with bigalloc");
265 err
= mnt_want_write_file(filp
);
269 err
= ext4_move_extents(filp
, donor
.file
, me
.orig_start
,
270 me
.donor_start
, me
.len
, &me
.moved_len
);
271 mnt_drop_write_file(filp
);
273 if (copy_to_user((struct move_extent __user
*)arg
,
281 case EXT4_IOC_GROUP_ADD
: {
282 struct ext4_new_group_data input
;
285 err
= ext4_resize_begin(sb
);
289 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
295 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
296 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
297 ext4_msg(sb
, KERN_ERR
,
298 "Online resizing not supported with bigalloc");
303 err
= mnt_want_write_file(filp
);
307 err
= ext4_group_add(sb
, &input
);
308 if (EXT4_SB(sb
)->s_journal
) {
309 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
310 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
311 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
315 mnt_drop_write_file(filp
);
316 if (!err
&& ext4_has_group_desc_csum(sb
) &&
317 test_opt(sb
, INIT_INODE_TABLE
))
318 err
= ext4_register_li_request(sb
, input
.group
);
324 case EXT4_IOC_MIGRATE
:
327 if (!inode_owner_or_capable(inode
))
330 err
= mnt_want_write_file(filp
);
334 * inode_mutex prevent write and truncate on the file.
335 * Read still goes through. We take i_data_sem in
336 * ext4_ext_swap_inode_data before we switch the
337 * inode format to prevent read.
339 mutex_lock(&(inode
->i_mutex
));
340 err
= ext4_ext_migrate(inode
);
341 mutex_unlock(&(inode
->i_mutex
));
342 mnt_drop_write_file(filp
);
346 case EXT4_IOC_ALLOC_DA_BLKS
:
349 if (!inode_owner_or_capable(inode
))
352 err
= mnt_want_write_file(filp
);
355 err
= ext4_alloc_da_blocks(inode
);
356 mnt_drop_write_file(filp
);
360 case EXT4_IOC_RESIZE_FS
: {
361 ext4_fsblk_t n_blocks_count
;
362 struct super_block
*sb
= inode
->i_sb
;
363 int err
= 0, err2
= 0;
364 ext4_group_t o_group
= EXT4_SB(sb
)->s_groups_count
;
366 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
,
367 EXT4_FEATURE_RO_COMPAT_BIGALLOC
)) {
368 ext4_msg(sb
, KERN_ERR
,
369 "Online resizing not (yet) supported with bigalloc");
373 if (copy_from_user(&n_blocks_count
, (__u64 __user
*)arg
,
378 err
= ext4_resize_begin(sb
);
382 err
= mnt_want_write_file(filp
);
386 err
= ext4_resize_fs(sb
, n_blocks_count
);
387 if (EXT4_SB(sb
)->s_journal
) {
388 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
389 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
390 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
394 mnt_drop_write_file(filp
);
395 if (!err
&& (o_group
> EXT4_SB(sb
)->s_groups_count
) &&
396 ext4_has_group_desc_csum(sb
) &&
397 test_opt(sb
, INIT_INODE_TABLE
))
398 err
= ext4_register_li_request(sb
, o_group
);
407 struct request_queue
*q
= bdev_get_queue(sb
->s_bdev
);
408 struct fstrim_range range
;
411 if (!capable(CAP_SYS_ADMIN
))
414 if (!blk_queue_discard(q
))
417 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
421 range
.minlen
= max((unsigned int)range
.minlen
,
422 q
->limits
.discard_granularity
);
423 ret
= ext4_trim_fs(sb
, &range
);
427 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
440 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
442 /* These are just misnamed, they actually get/put from/to user an int */
444 case EXT4_IOC32_GETFLAGS
:
445 cmd
= EXT4_IOC_GETFLAGS
;
447 case EXT4_IOC32_SETFLAGS
:
448 cmd
= EXT4_IOC_SETFLAGS
;
450 case EXT4_IOC32_GETVERSION
:
451 cmd
= EXT4_IOC_GETVERSION
;
453 case EXT4_IOC32_SETVERSION
:
454 cmd
= EXT4_IOC_SETVERSION
;
456 case EXT4_IOC32_GROUP_EXTEND
:
457 cmd
= EXT4_IOC_GROUP_EXTEND
;
459 case EXT4_IOC32_GETVERSION_OLD
:
460 cmd
= EXT4_IOC_GETVERSION_OLD
;
462 case EXT4_IOC32_SETVERSION_OLD
:
463 cmd
= EXT4_IOC_SETVERSION_OLD
;
465 case EXT4_IOC32_GETRSVSZ
:
466 cmd
= EXT4_IOC_GETRSVSZ
;
468 case EXT4_IOC32_SETRSVSZ
:
469 cmd
= EXT4_IOC_SETRSVSZ
;
471 case EXT4_IOC32_GROUP_ADD
: {
472 struct compat_ext4_new_group_input __user
*uinput
;
473 struct ext4_new_group_input input
;
477 uinput
= compat_ptr(arg
);
478 err
= get_user(input
.group
, &uinput
->group
);
479 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
480 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
481 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
482 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
483 err
|= get_user(input
.reserved_blocks
,
484 &uinput
->reserved_blocks
);
489 err
= ext4_ioctl(file
, EXT4_IOC_GROUP_ADD
,
490 (unsigned long) &input
);
494 case EXT4_IOC_MOVE_EXT
:
496 case EXT4_IOC_RESIZE_FS
:
501 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));