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 handle
= ext4_journal_start(inode
, 1);
97 err
= PTR_ERR(handle
);
101 ext4_handle_sync(handle
);
102 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
106 flags
= flags
& EXT4_FL_USER_MODIFIABLE
;
107 flags
|= oldflags
& ~EXT4_FL_USER_MODIFIABLE
;
110 ext4_set_inode_flags(inode
);
111 inode
->i_ctime
= ext4_current_time(inode
);
113 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
115 ext4_journal_stop(handle
);
119 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
120 err
= ext4_change_inode_journal_flag(inode
, jflag
);
124 err
= ext4_ext_migrate(inode
);
126 mutex_unlock(&inode
->i_mutex
);
127 mnt_drop_write(filp
->f_path
.mnt
);
130 case EXT4_IOC_GETVERSION
:
131 case EXT4_IOC_GETVERSION_OLD
:
132 return put_user(inode
->i_generation
, (int __user
*) arg
);
133 case EXT4_IOC_SETVERSION
:
134 case EXT4_IOC_SETVERSION_OLD
: {
136 struct ext4_iloc iloc
;
140 if (!is_owner_or_cap(inode
))
143 err
= mnt_want_write(filp
->f_path
.mnt
);
146 if (get_user(generation
, (int __user
*) arg
)) {
151 handle
= ext4_journal_start(inode
, 1);
152 if (IS_ERR(handle
)) {
153 err
= PTR_ERR(handle
);
156 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
158 inode
->i_ctime
= ext4_current_time(inode
);
159 inode
->i_generation
= generation
;
160 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
162 ext4_journal_stop(handle
);
164 mnt_drop_write(filp
->f_path
.mnt
);
167 #ifdef CONFIG_JBD2_DEBUG
168 case EXT4_IOC_WAIT_FOR_READONLY
:
170 * This is racy - by the time we're woken up and running,
171 * the superblock could be released. And the module could
172 * have been unloaded. So sue me.
174 * Returns 1 if it slept, else zero.
177 struct super_block
*sb
= inode
->i_sb
;
178 DECLARE_WAITQUEUE(wait
, current
);
181 set_current_state(TASK_INTERRUPTIBLE
);
182 add_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
183 if (timer_pending(&EXT4_SB(sb
)->turn_ro_timer
)) {
187 remove_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
191 case EXT4_IOC_GROUP_EXTEND
: {
192 ext4_fsblk_t n_blocks_count
;
193 struct super_block
*sb
= inode
->i_sb
;
196 if (!capable(CAP_SYS_RESOURCE
))
199 if (get_user(n_blocks_count
, (__u32 __user
*)arg
))
202 err
= mnt_want_write(filp
->f_path
.mnt
);
206 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
207 if (EXT4_SB(sb
)->s_journal
) {
208 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
209 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
210 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
214 mnt_drop_write(filp
->f_path
.mnt
);
219 case EXT4_IOC_MOVE_EXT
: {
220 struct move_extent me
;
221 struct file
*donor_filp
;
224 if (!(filp
->f_mode
& FMODE_READ
) ||
225 !(filp
->f_mode
& FMODE_WRITE
))
228 if (copy_from_user(&me
,
229 (struct move_extent __user
*)arg
, sizeof(me
)))
233 donor_filp
= fget(me
.donor_fd
);
237 if (!(donor_filp
->f_mode
& FMODE_WRITE
)) {
242 err
= mnt_want_write(filp
->f_path
.mnt
);
246 err
= ext4_move_extents(filp
, donor_filp
, me
.orig_start
,
247 me
.donor_start
, me
.len
, &me
.moved_len
);
248 mnt_drop_write(filp
->f_path
.mnt
);
249 if (me
.moved_len
> 0)
250 file_remove_suid(donor_filp
);
252 if (copy_to_user((struct move_extent
*)arg
, &me
, sizeof(me
)))
259 case EXT4_IOC_GROUP_ADD
: {
260 struct ext4_new_group_data input
;
261 struct super_block
*sb
= inode
->i_sb
;
264 if (!capable(CAP_SYS_RESOURCE
))
267 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
271 err
= mnt_want_write(filp
->f_path
.mnt
);
275 err
= ext4_group_add(sb
, &input
);
276 if (EXT4_SB(sb
)->s_journal
) {
277 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
278 err2
= jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
279 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
283 mnt_drop_write(filp
->f_path
.mnt
);
288 case EXT4_IOC_MIGRATE
:
291 if (!is_owner_or_cap(inode
))
294 err
= mnt_want_write(filp
->f_path
.mnt
);
298 * inode_mutex prevent write and truncate on the file.
299 * Read still goes through. We take i_data_sem in
300 * ext4_ext_swap_inode_data before we switch the
301 * inode format to prevent read.
303 mutex_lock(&(inode
->i_mutex
));
304 err
= ext4_ext_migrate(inode
);
305 mutex_unlock(&(inode
->i_mutex
));
306 mnt_drop_write(filp
->f_path
.mnt
);
310 case EXT4_IOC_ALLOC_DA_BLKS
:
313 if (!is_owner_or_cap(inode
))
316 err
= mnt_want_write(filp
->f_path
.mnt
);
319 err
= ext4_alloc_da_blocks(inode
);
320 mnt_drop_write(filp
->f_path
.mnt
);
330 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
332 /* These are just misnamed, they actually get/put from/to user an int */
334 case EXT4_IOC32_GETFLAGS
:
335 cmd
= EXT4_IOC_GETFLAGS
;
337 case EXT4_IOC32_SETFLAGS
:
338 cmd
= EXT4_IOC_SETFLAGS
;
340 case EXT4_IOC32_GETVERSION
:
341 cmd
= EXT4_IOC_GETVERSION
;
343 case EXT4_IOC32_SETVERSION
:
344 cmd
= EXT4_IOC_SETVERSION
;
346 case EXT4_IOC32_GROUP_EXTEND
:
347 cmd
= EXT4_IOC_GROUP_EXTEND
;
349 case EXT4_IOC32_GETVERSION_OLD
:
350 cmd
= EXT4_IOC_GETVERSION_OLD
;
352 case EXT4_IOC32_SETVERSION_OLD
:
353 cmd
= EXT4_IOC_SETVERSION_OLD
;
355 #ifdef CONFIG_JBD2_DEBUG
356 case EXT4_IOC32_WAIT_FOR_READONLY
:
357 cmd
= EXT4_IOC_WAIT_FOR_READONLY
;
360 case EXT4_IOC32_GETRSVSZ
:
361 cmd
= EXT4_IOC_GETRSVSZ
;
363 case EXT4_IOC32_SETRSVSZ
:
364 cmd
= EXT4_IOC_SETRSVSZ
;
366 case EXT4_IOC_GROUP_ADD
:
371 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));