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
);
340 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
342 /* These are just misnamed, they actually get/put from/to user an int */
344 case EXT4_IOC32_GETFLAGS
:
345 cmd
= EXT4_IOC_GETFLAGS
;
347 case EXT4_IOC32_SETFLAGS
:
348 cmd
= EXT4_IOC_SETFLAGS
;
350 case EXT4_IOC32_GETVERSION
:
351 cmd
= EXT4_IOC_GETVERSION
;
353 case EXT4_IOC32_SETVERSION
:
354 cmd
= EXT4_IOC_SETVERSION
;
356 case EXT4_IOC32_GROUP_EXTEND
:
357 cmd
= EXT4_IOC_GROUP_EXTEND
;
359 case EXT4_IOC32_GETVERSION_OLD
:
360 cmd
= EXT4_IOC_GETVERSION_OLD
;
362 case EXT4_IOC32_SETVERSION_OLD
:
363 cmd
= EXT4_IOC_SETVERSION_OLD
;
365 #ifdef CONFIG_JBD2_DEBUG
366 case EXT4_IOC32_WAIT_FOR_READONLY
:
367 cmd
= EXT4_IOC_WAIT_FOR_READONLY
;
370 case EXT4_IOC32_GETRSVSZ
:
371 cmd
= EXT4_IOC_GETRSVSZ
;
373 case EXT4_IOC32_SETRSVSZ
:
374 cmd
= EXT4_IOC_SETRSVSZ
;
376 case EXT4_IOC32_GROUP_ADD
: {
377 struct compat_ext4_new_group_input __user
*uinput
;
378 struct ext4_new_group_input input
;
382 uinput
= compat_ptr(arg
);
383 err
= get_user(input
.group
, &uinput
->group
);
384 err
|= get_user(input
.block_bitmap
, &uinput
->block_bitmap
);
385 err
|= get_user(input
.inode_bitmap
, &uinput
->inode_bitmap
);
386 err
|= get_user(input
.inode_table
, &uinput
->inode_table
);
387 err
|= get_user(input
.blocks_count
, &uinput
->blocks_count
);
388 err
|= get_user(input
.reserved_blocks
,
389 &uinput
->reserved_blocks
);
394 err
= ext4_ioctl(file
, EXT4_IOC_GROUP_ADD
,
395 (unsigned long) &input
);
399 case EXT4_IOC_MOVE_EXT
:
404 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));