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/smp_lock.h>
16 #include <linux/mount.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 if (!S_ISDIR(inode
->i_mode
))
52 flags
&= ~EXT4_DIRSYNC_FL
;
55 mutex_lock(&inode
->i_mutex
);
56 /* Is it quota file? Do not allow user to mess with it */
57 if (IS_NOQUOTA(inode
))
60 oldflags
= ei
->i_flags
;
62 /* The JOURNAL_DATA flag is modifiable only by root */
63 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
66 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
67 * the relevant capability.
69 * This test looks nicer. Thanks to Pauline Middelink
71 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
72 if (!capable(CAP_LINUX_IMMUTABLE
))
77 * The JOURNAL_DATA flag can only be changed by
78 * the relevant capability.
80 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
81 if (!capable(CAP_SYS_RESOURCE
))
84 if (oldflags
& EXT4_EXTENTS_FL
) {
85 /* We don't support clearning extent flags */
86 if (!(flags
& EXT4_EXTENTS_FL
)) {
90 } else if (flags
& EXT4_EXTENTS_FL
) {
91 /* migrate the file */
93 flags
&= ~EXT4_EXTENTS_FL
;
96 handle
= ext4_journal_start(inode
, 1);
98 err
= PTR_ERR(handle
);
103 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
107 flags
= flags
& EXT4_FL_USER_MODIFIABLE
;
108 flags
|= oldflags
& ~EXT4_FL_USER_MODIFIABLE
;
111 ext4_set_inode_flags(inode
);
112 inode
->i_ctime
= ext4_current_time(inode
);
114 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
116 ext4_journal_stop(handle
);
120 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
121 err
= ext4_change_inode_journal_flag(inode
, jflag
);
125 err
= ext4_ext_migrate(inode
);
127 mutex_unlock(&inode
->i_mutex
);
128 mnt_drop_write(filp
->f_path
.mnt
);
131 case EXT4_IOC_GETVERSION
:
132 case EXT4_IOC_GETVERSION_OLD
:
133 return put_user(inode
->i_generation
, (int __user
*) arg
);
134 case EXT4_IOC_SETVERSION
:
135 case EXT4_IOC_SETVERSION_OLD
: {
137 struct ext4_iloc iloc
;
141 if (!is_owner_or_cap(inode
))
144 err
= mnt_want_write(filp
->f_path
.mnt
);
147 if (get_user(generation
, (int __user
*) arg
)) {
152 handle
= ext4_journal_start(inode
, 1);
153 if (IS_ERR(handle
)) {
154 err
= PTR_ERR(handle
);
157 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
159 inode
->i_ctime
= ext4_current_time(inode
);
160 inode
->i_generation
= generation
;
161 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
163 ext4_journal_stop(handle
);
165 mnt_drop_write(filp
->f_path
.mnt
);
168 #ifdef CONFIG_JBD2_DEBUG
169 case EXT4_IOC_WAIT_FOR_READONLY
:
171 * This is racy - by the time we're woken up and running,
172 * the superblock could be released. And the module could
173 * have been unloaded. So sue me.
175 * Returns 1 if it slept, else zero.
178 struct super_block
*sb
= inode
->i_sb
;
179 DECLARE_WAITQUEUE(wait
, current
);
182 set_current_state(TASK_INTERRUPTIBLE
);
183 add_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
184 if (timer_pending(&EXT4_SB(sb
)->turn_ro_timer
)) {
188 remove_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
192 case EXT4_IOC_GROUP_EXTEND
: {
193 ext4_fsblk_t n_blocks_count
;
194 struct super_block
*sb
= inode
->i_sb
;
197 if (!capable(CAP_SYS_RESOURCE
))
200 if (get_user(n_blocks_count
, (__u32 __user
*)arg
))
203 err
= mnt_want_write(filp
->f_path
.mnt
);
207 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
208 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
209 jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
210 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
211 mnt_drop_write(filp
->f_path
.mnt
);
215 case EXT4_IOC_GROUP_ADD
: {
216 struct ext4_new_group_data input
;
217 struct super_block
*sb
= inode
->i_sb
;
220 if (!capable(CAP_SYS_RESOURCE
))
223 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
227 err
= mnt_want_write(filp
->f_path
.mnt
);
231 err
= ext4_group_add(sb
, &input
);
232 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
233 jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
234 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
235 mnt_drop_write(filp
->f_path
.mnt
);
240 case EXT4_IOC_MIGRATE
:
243 if (!is_owner_or_cap(inode
))
246 err
= mnt_want_write(filp
->f_path
.mnt
);
250 * inode_mutex prevent write and truncate on the file.
251 * Read still goes through. We take i_data_sem in
252 * ext4_ext_swap_inode_data before we switch the
253 * inode format to prevent read.
255 mutex_lock(&(inode
->i_mutex
));
256 err
= ext4_ext_migrate(inode
);
257 mutex_unlock(&(inode
->i_mutex
));
258 mnt_drop_write(filp
->f_path
.mnt
);
268 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
270 /* These are just misnamed, they actually get/put from/to user an int */
272 case EXT4_IOC32_GETFLAGS
:
273 cmd
= EXT4_IOC_GETFLAGS
;
275 case EXT4_IOC32_SETFLAGS
:
276 cmd
= EXT4_IOC_SETFLAGS
;
278 case EXT4_IOC32_GETVERSION
:
279 cmd
= EXT4_IOC_GETVERSION
;
281 case EXT4_IOC32_SETVERSION
:
282 cmd
= EXT4_IOC_SETVERSION
;
284 case EXT4_IOC32_GROUP_EXTEND
:
285 cmd
= EXT4_IOC_GROUP_EXTEND
;
287 case EXT4_IOC32_GETVERSION_OLD
:
288 cmd
= EXT4_IOC_GETVERSION_OLD
;
290 case EXT4_IOC32_SETVERSION_OLD
:
291 cmd
= EXT4_IOC_SETVERSION_OLD
;
293 #ifdef CONFIG_JBD2_DEBUG
294 case EXT4_IOC32_WAIT_FOR_READONLY
:
295 cmd
= EXT4_IOC_WAIT_FOR_READONLY
;
298 case EXT4_IOC32_GETRSVSZ
:
299 cmd
= EXT4_IOC_GETRSVSZ
;
301 case EXT4_IOC32_SETRSVSZ
:
302 cmd
= EXT4_IOC_SETRSVSZ
;
304 case EXT4_IOC_GROUP_ADD
:
309 return ext4_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));