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/ext4_fs.h>
14 #include <linux/ext4_jbd2.h>
15 #include <linux/time.h>
16 #include <linux/compat.h>
17 #include <linux/smp_lock.h>
18 #include <asm/uaccess.h>
20 int ext4_ioctl (struct inode
* inode
, struct file
* filp
, unsigned int cmd
,
23 struct ext4_inode_info
*ei
= EXT4_I(inode
);
25 unsigned short rsv_window_size
;
27 ext4_debug ("cmd = %u, arg = %lu\n", cmd
, arg
);
30 case EXT4_IOC_GETFLAGS
:
31 flags
= ei
->i_flags
& EXT4_FL_USER_VISIBLE
;
32 return put_user(flags
, (int __user
*) arg
);
33 case EXT4_IOC_SETFLAGS
: {
34 handle_t
*handle
= NULL
;
36 struct ext4_iloc iloc
;
37 unsigned int oldflags
;
43 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
46 if (get_user(flags
, (int __user
*) arg
))
49 if (!S_ISDIR(inode
->i_mode
))
50 flags
&= ~EXT4_DIRSYNC_FL
;
52 mutex_lock(&inode
->i_mutex
);
53 oldflags
= ei
->i_flags
;
55 /* The JOURNAL_DATA flag is modifiable only by root */
56 jflag
= flags
& EXT4_JOURNAL_DATA_FL
;
59 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
60 * the relevant capability.
62 * This test looks nicer. Thanks to Pauline Middelink
64 if ((flags
^ oldflags
) & (EXT4_APPEND_FL
| EXT4_IMMUTABLE_FL
)) {
65 if (!capable(CAP_LINUX_IMMUTABLE
)) {
66 mutex_unlock(&inode
->i_mutex
);
72 * The JOURNAL_DATA flag can only be changed by
73 * the relevant capability.
75 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
)) {
76 if (!capable(CAP_SYS_RESOURCE
)) {
77 mutex_unlock(&inode
->i_mutex
);
83 handle
= ext4_journal_start(inode
, 1);
85 mutex_unlock(&inode
->i_mutex
);
86 return PTR_ERR(handle
);
90 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
94 flags
= flags
& EXT4_FL_USER_MODIFIABLE
;
95 flags
|= oldflags
& ~EXT4_FL_USER_MODIFIABLE
;
98 ext4_set_inode_flags(inode
);
99 inode
->i_ctime
= CURRENT_TIME_SEC
;
101 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
103 ext4_journal_stop(handle
);
105 mutex_unlock(&inode
->i_mutex
);
109 if ((jflag
^ oldflags
) & (EXT4_JOURNAL_DATA_FL
))
110 err
= ext4_change_inode_journal_flag(inode
, jflag
);
111 mutex_unlock(&inode
->i_mutex
);
114 case EXT4_IOC_GETVERSION
:
115 case EXT4_IOC_GETVERSION_OLD
:
116 return put_user(inode
->i_generation
, (int __user
*) arg
);
117 case EXT4_IOC_SETVERSION
:
118 case EXT4_IOC_SETVERSION_OLD
: {
120 struct ext4_iloc iloc
;
124 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
126 if (IS_RDONLY(inode
))
128 if (get_user(generation
, (int __user
*) arg
))
131 handle
= ext4_journal_start(inode
, 1);
133 return PTR_ERR(handle
);
134 err
= ext4_reserve_inode_write(handle
, inode
, &iloc
);
136 inode
->i_ctime
= CURRENT_TIME_SEC
;
137 inode
->i_generation
= generation
;
138 err
= ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
140 ext4_journal_stop(handle
);
143 #ifdef CONFIG_JBD_DEBUG
144 case EXT4_IOC_WAIT_FOR_READONLY
:
146 * This is racy - by the time we're woken up and running,
147 * the superblock could be released. And the module could
148 * have been unloaded. So sue me.
150 * Returns 1 if it slept, else zero.
153 struct super_block
*sb
= inode
->i_sb
;
154 DECLARE_WAITQUEUE(wait
, current
);
157 set_current_state(TASK_INTERRUPTIBLE
);
158 add_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
159 if (timer_pending(&EXT4_SB(sb
)->turn_ro_timer
)) {
163 remove_wait_queue(&EXT4_SB(sb
)->ro_wait_queue
, &wait
);
167 case EXT4_IOC_GETRSVSZ
:
168 if (test_opt(inode
->i_sb
, RESERVATION
)
169 && S_ISREG(inode
->i_mode
)
170 && ei
->i_block_alloc_info
) {
171 rsv_window_size
= ei
->i_block_alloc_info
->rsv_window_node
.rsv_goal_size
;
172 return put_user(rsv_window_size
, (int __user
*)arg
);
175 case EXT4_IOC_SETRSVSZ
: {
177 if (!test_opt(inode
->i_sb
, RESERVATION
) ||!S_ISREG(inode
->i_mode
))
180 if (IS_RDONLY(inode
))
183 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
186 if (get_user(rsv_window_size
, (int __user
*)arg
))
189 if (rsv_window_size
> EXT4_MAX_RESERVE_BLOCKS
)
190 rsv_window_size
= EXT4_MAX_RESERVE_BLOCKS
;
193 * need to allocate reservation structure for this inode
194 * before set the window size
196 mutex_lock(&ei
->truncate_mutex
);
197 if (!ei
->i_block_alloc_info
)
198 ext4_init_block_alloc_info(inode
);
200 if (ei
->i_block_alloc_info
){
201 struct ext4_reserve_window_node
*rsv
= &ei
->i_block_alloc_info
->rsv_window_node
;
202 rsv
->rsv_goal_size
= rsv_window_size
;
204 mutex_unlock(&ei
->truncate_mutex
);
207 case EXT4_IOC_GROUP_EXTEND
: {
208 ext4_fsblk_t n_blocks_count
;
209 struct super_block
*sb
= inode
->i_sb
;
212 if (!capable(CAP_SYS_RESOURCE
))
215 if (IS_RDONLY(inode
))
218 if (get_user(n_blocks_count
, (__u32 __user
*)arg
))
221 err
= ext4_group_extend(sb
, EXT4_SB(sb
)->s_es
, n_blocks_count
);
222 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
223 jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
224 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
228 case EXT4_IOC_GROUP_ADD
: {
229 struct ext4_new_group_data input
;
230 struct super_block
*sb
= inode
->i_sb
;
233 if (!capable(CAP_SYS_RESOURCE
))
236 if (IS_RDONLY(inode
))
239 if (copy_from_user(&input
, (struct ext4_new_group_input __user
*)arg
,
243 err
= ext4_group_add(sb
, &input
);
244 jbd2_journal_lock_updates(EXT4_SB(sb
)->s_journal
);
245 jbd2_journal_flush(EXT4_SB(sb
)->s_journal
);
246 jbd2_journal_unlock_updates(EXT4_SB(sb
)->s_journal
);
257 long ext4_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
259 struct inode
*inode
= file
->f_dentry
->d_inode
;
262 /* These are just misnamed, they actually get/put from/to user an int */
264 case EXT4_IOC32_GETFLAGS
:
265 cmd
= EXT4_IOC_GETFLAGS
;
267 case EXT4_IOC32_SETFLAGS
:
268 cmd
= EXT4_IOC_SETFLAGS
;
270 case EXT4_IOC32_GETVERSION
:
271 cmd
= EXT4_IOC_GETVERSION
;
273 case EXT4_IOC32_SETVERSION
:
274 cmd
= EXT4_IOC_SETVERSION
;
276 case EXT4_IOC32_GROUP_EXTEND
:
277 cmd
= EXT4_IOC_GROUP_EXTEND
;
279 case EXT4_IOC32_GETVERSION_OLD
:
280 cmd
= EXT4_IOC_GETVERSION_OLD
;
282 case EXT4_IOC32_SETVERSION_OLD
:
283 cmd
= EXT4_IOC_SETVERSION_OLD
;
285 #ifdef CONFIG_JBD_DEBUG
286 case EXT4_IOC32_WAIT_FOR_READONLY
:
287 cmd
= EXT4_IOC_WAIT_FOR_READONLY
;
290 case EXT4_IOC32_GETRSVSZ
:
291 cmd
= EXT4_IOC_GETRSVSZ
;
293 case EXT4_IOC32_SETRSVSZ
:
294 cmd
= EXT4_IOC_SETRSVSZ
;
296 case EXT4_IOC_GROUP_ADD
:
302 ret
= ext4_ioctl(inode
, file
, cmd
, (unsigned long) compat_ptr(arg
));