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 long ext4_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
23 struct inode
*inode
= filp
->f_dentry
->d_inode
;
24 struct ext4_inode_info
*ei
= EXT4_I(inode
);
27 ext4_debug("cmd = %u, arg = %lu\n", cmd
, arg
);
30 case EXT4_IOC_GETFLAGS
:
31 ext4_get_inode_flags(ei
);
32 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
33 return put_user(flags
, (int __user
*) arg
);
34 case EXT4_IOC_SETFLAGS
: {
35 handle_t
*handle
= NULL
;
37 struct ext4_iloc iloc
;
38 unsigned int oldflags
;
41 if (!is_owner_or_cap(inode
))
44 if (get_user(flags
, (int __user
*) arg
))
47 err
= mnt_want_write(filp
->f_path
.mnt
);
51 flags
= ext4_mask_flags(inode
->i_mode
, flags
);
54 mutex_lock(&inode
->i_mutex
);
55 /* Is it quota file? Do not allow user to mess with it */
56 if (IS_NOQUOTA(inode
))
59 oldflags
= ei
->i_flags
;
61 /* The JOURNAL_DATA flag is modifiable only by root */
62 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
65 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
66 * the relevant capability.
68 * This test looks nicer. Thanks to Pauline Middelink
70 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
71 if (!capable(CAP_LINUX_IMMUTABLE
))
76 * The JOURNAL_DATA flag can only be changed by
77 * the relevant capability.
79 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
80 if (!capable(CAP_SYS_RESOURCE
))
83 if (oldflags
& EXT4_EXTENTS_FL
) {
84 /* We don't support clearning extent flags */
85 if (!(flags
& EXT4_EXTENTS_FL
)) {
89 } else if (flags
& EXT4_EXTENTS_FL
) {
90 /* migrate the file */
92 flags
&= ~EXT4_EXTENTS_FL
;
95 if (flags
& EXT4_EOFBLOCKS_FL
) {
96 /* we don't support adding EOFBLOCKS flag */
97 if (!(oldflags
& EXT4_EOFBLOCKS_FL
)) {
101 } else if (oldflags
& EXT4_EOFBLOCKS_FL
)
102 ext4_truncate(inode
);
104 handle
= ext4_journal_start(inode
, 1);
105 if (IS_ERR(handle
)) {
106 err
= PTR_ERR(handle
);
110 ext4_handle_sync(handle
);
111 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
115 flags
= flags
& EXT4_FL_USER_MODIFIABLE
;
116 flags
|= oldflags
& ~EXT4_FL_USER_MODIFIABLE
;
119 ext4_set_inode_flags(inode
);
120 inode
->i_ctime
= ext4_current_time(inode
);
122 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
124 ext4_journal_stop(handle
);
128 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
129 err
= ext4_change_inode_journal_flag(inode
, jflag
);
133 err
= ext4_ext_migrate(inode
);
135 mutex_unlock(&inode
->i_mutex
);
136 mnt_drop_write(filp
->f_path
.mnt
);
139 case EXT4_IOC_GETVERSION
:
140 case EXT4_IOC_GETVERSION_OLD
:
141 return put_user(inode
->i_generation
, (int __user
*) arg
);
142 case EXT4_IOC_SETVERSION
:
143 case EXT4_IOC_SETVERSION_OLD
: {
145 struct ext4_iloc iloc
;
149 if (!is_owner_or_cap(inode
))
152 err
= mnt_want_write(filp
->f_path
.mnt
);
155 if (get_user(generation
, (int __user
*) arg
)) {
160 handle
= ext4_journal_start(inode
, 1);
161 if (IS_ERR(handle
)) {
162 err
= PTR_ERR(handle
);
165 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
167 inode
->i_ctime
= ext4_current_time(inode
);
168 inode
->i_generation
= generation
;
169 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
171 ext4_journal_stop(handle
);
173 mnt_drop_write(filp
->f_path
.mnt
);
176 #ifdef CONFIG_JBD2_DEBUG
177 case EXT4_IOC_WAIT_FOR_READONLY
:
179 * This is racy - by the time we're woken up and running,
180 * the superblock could be released. And the module could
181 * have been unloaded. So sue me.
183 * Returns 1 if it slept, else zero.
186 struct super_block
*sb
= inode
->i_sb
;
187 DECLARE_WAITQUEUE(wait
, current
);
190 set_current_state(TASK_INTERRUPTIBLE
);
191 add_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
192 if (timer_pending(&EXT4_SB(sb
)->turn_ro_timer
)) {
196 remove_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
200 case EXT4_IOC_GROUP_EXTEND
: {
201 ext4_fsblk_t n_blocks_count
;
202 struct super_block
*sb
= inode
->i_sb
;
205 if (!capable(CAP_SYS_RESOURCE
))
208 if (get_user(n_blocks_count
, (__u32 __user
*)arg
))
211 err
= mnt_want_write(filp
->f_path
.mnt
);
215 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
216 if (EXT4_SB(sb
)->s_journal
) {
217 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
218 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
219 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
223 mnt_drop_write(filp
->f_path
.mnt
);
228 case EXT4_IOC_MOVE_EXT
: {
229 struct move_extent me
;
230 struct file
*donor_filp
;
233 if (!(filp
->f_mode
& FMODE_READ
) ||
234 !(filp
->f_mode
& FMODE_WRITE
))
237 if (copy_from_user(&me
,
238 (struct move_extent __user
*)arg
, sizeof(me
)))
242 donor_filp
= fget(me
.donor_fd
);
246 if (!(donor_filp
->f_mode
& FMODE_WRITE
)) {
251 err
= mnt_want_write(filp
->f_path
.mnt
);
255 err
= ext4_move_extents(filp
, donor_filp
, me
.orig_start
,
256 me
.donor_start
, me
.len
, &me
.moved_len
);
257 mnt_drop_write(filp
->f_path
.mnt
);
258 if (me
.moved_len
> 0)
259 file_remove_suid(donor_filp
);
261 if (copy_to_user((struct move_extent __user
*)arg
,
269 case EXT4_IOC_GROUP_ADD
: {
270 struct ext4_new_group_data input
;
271 struct super_block
*sb
= inode
->i_sb
;
274 if (!capable(CAP_SYS_RESOURCE
))
277 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
281 err
= mnt_want_write(filp
->f_path
.mnt
);
285 err
= ext4_group_add(sb
, &input
);
286 if (EXT4_SB(sb
)->s_journal
) {
287 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
288 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
289 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
293 mnt_drop_write(filp
->f_path
.mnt
);
298 case EXT4_IOC_MIGRATE
:
301 if (!is_owner_or_cap(inode
))
304 err
= mnt_want_write(filp
->f_path
.mnt
);
308 * inode_mutex prevent write and truncate on the file.
309 * Read still goes through. We take i_data_sem in
310 * ext4_ext_swap_inode_data before we switch the
311 * inode format to prevent read.
313 mutex_lock(&(inode
->i_mutex
));
314 err
= ext4_ext_migrate(inode
);
315 mutex_unlock(&(inode
->i_mutex
));
316 mnt_drop_write(filp
->f_path
.mnt
);
320 case EXT4_IOC_ALLOC_DA_BLKS
:
323 if (!is_owner_or_cap(inode
))
326 err
= mnt_want_write(filp
->f_path
.mnt
);
329 err
= ext4_alloc_da_blocks(inode
);
330 mnt_drop_write(filp
->f_path
.mnt
);
336 struct super_block
*sb
= inode
->i_sb
;
337 struct fstrim_range range
;
340 if (!capable(CAP_SYS_ADMIN
))
343 if (copy_from_user(&range
, (struct fstrim_range
*)arg
,
347 ret
= ext4_trim_fs(sb
, &range
);
351 if (copy_to_user((struct fstrim_range
*)arg
, &range
,
364 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
366 /* These are just misnamed, they actually get/put from/to user an int */
368 case EXT4_IOC32_GETFLAGS
:
369 cmd
= EXT4_IOC_GETFLAGS
;
371 case EXT4_IOC32_SETFLAGS
:
372 cmd
= EXT4_IOC_SETFLAGS
;
374 case EXT4_IOC32_GETVERSION
:
375 cmd
= EXT4_IOC_GETVERSION
;
377 case EXT4_IOC32_SETVERSION
:
378 cmd
= EXT4_IOC_SETVERSION
;
380 case EXT4_IOC32_GROUP_EXTEND
:
381 cmd
= EXT4_IOC_GROUP_EXTEND
;
383 case EXT4_IOC32_GETVERSION_OLD
:
384 cmd
= EXT4_IOC_GETVERSION_OLD
;
386 case EXT4_IOC32_SETVERSION_OLD
:
387 cmd
= EXT4_IOC_SETVERSION_OLD
;
389 #ifdef CONFIG_JBD2_DEBUG
390 case EXT4_IOC32_WAIT_FOR_READONLY
:
391 cmd
= EXT4_IOC_WAIT_FOR_READONLY
;
394 case EXT4_IOC32_GETRSVSZ
:
395 cmd
= EXT4_IOC_GETRSVSZ
;
397 case EXT4_IOC32_SETRSVSZ
:
398 cmd
= EXT4_IOC_SETRSVSZ
;
400 case EXT4_IOC32_GROUP_ADD
: {
401 struct compat_ext4_new_group_input __user
*uinput
;
402 struct ext4_new_group_input input
;
406 uinput
= compat_ptr(arg
);
407 err
= get_user(input
.group
, &uinput
->group
);
408 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
409 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
410 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
411 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
412 err
|= get_user(input
.reserved_blocks
,
413 &uinput
->reserved_blocks
);
418 err
= ext4_ioctl(file
, EXT4_IOC_GROUP_ADD
,
419 (unsigned long) &input
);
423 case EXT4_IOC_MOVE_EXT
:
428 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));