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 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;
44 if (!is_owner_or_cap(inode))
47 if (get_user(flags, (int __user *) arg))
50 if (!S_ISDIR(inode->i_mode))
51 flags &= ~EXT4_DIRSYNC_FL;
53 mutex_lock(&inode->i_mutex);
54 /* Is it quota file? Do not allow user to mess with it */
55 if (IS_NOQUOTA(inode)) {
56 mutex_unlock(&inode->i_mutex);
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)) {
72 mutex_unlock(&inode->i_mutex);
78 * The JOURNAL_DATA flag can only be changed by
79 * the relevant capability.
81 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
82 if (!capable(CAP_SYS_RESOURCE)) {
83 mutex_unlock(&inode->i_mutex);
89 handle = ext4_journal_start(inode, 1);
91 mutex_unlock(&inode->i_mutex);
92 return PTR_ERR(handle);
96 err = ext4_reserve_inode_write(handle, inode, &iloc);
100 flags = flags & EXT4_FL_USER_MODIFIABLE;
101 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
104 ext4_set_inode_flags(inode);
105 inode->i_ctime = ext4_current_time(inode);
107 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
109 ext4_journal_stop(handle);
111 mutex_unlock(&inode->i_mutex);
115 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
116 err = ext4_change_inode_journal_flag(inode, jflag);
117 mutex_unlock(&inode->i_mutex);
120 case EXT4_IOC_GETVERSION:
121 case EXT4_IOC_GETVERSION_OLD:
122 return put_user(inode->i_generation, (int __user *) arg);
123 case EXT4_IOC_SETVERSION:
124 case EXT4_IOC_SETVERSION_OLD: {
126 struct ext4_iloc iloc;
130 if (!is_owner_or_cap(inode))
132 if (IS_RDONLY(inode))
134 if (get_user(generation, (int __user *) arg))
137 handle = ext4_journal_start(inode, 1);
139 return PTR_ERR(handle);
140 err = ext4_reserve_inode_write(handle, inode, &iloc);
142 inode->i_ctime = ext4_current_time(inode);
143 inode->i_generation = generation;
144 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
146 ext4_journal_stop(handle);
149 #ifdef CONFIG_JBD2_DEBUG
150 case EXT4_IOC_WAIT_FOR_READONLY:
152 * This is racy - by the time we're woken up and running,
153 * the superblock could be released. And the module could
154 * have been unloaded. So sue me.
156 * Returns 1 if it slept, else zero.
159 struct super_block *sb = inode->i_sb;
160 DECLARE_WAITQUEUE(wait, current);
163 set_current_state(TASK_INTERRUPTIBLE);
164 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
165 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
169 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
173 case EXT4_IOC_GETRSVSZ:
174 if (test_opt(inode->i_sb, RESERVATION)
175 && S_ISREG(inode->i_mode)
176 && ei->i_block_alloc_info) {
177 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
178 return put_user(rsv_window_size, (int __user *)arg);
181 case EXT4_IOC_SETRSVSZ: {
183 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
186 if (IS_RDONLY(inode))
189 if (!is_owner_or_cap(inode))
192 if (get_user(rsv_window_size, (int __user *)arg))
195 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
196 rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
199 * need to allocate reservation structure for this inode
200 * before set the window size
202 down_write(&ei->i_data_sem);
203 if (!ei->i_block_alloc_info)
204 ext4_init_block_alloc_info(inode);
206 if (ei->i_block_alloc_info){
207 struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
208 rsv->rsv_goal_size = rsv_window_size;
210 up_write(&ei->i_data_sem);
213 case EXT4_IOC_GROUP_EXTEND: {
214 ext4_fsblk_t n_blocks_count;
215 struct super_block *sb = inode->i_sb;
218 if (!capable(CAP_SYS_RESOURCE))
221 if (IS_RDONLY(inode))
224 if (get_user(n_blocks_count, (__u32 __user *)arg))
227 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
228 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
229 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
230 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
234 case EXT4_IOC_GROUP_ADD: {
235 struct ext4_new_group_data input;
236 struct super_block *sb = inode->i_sb;
239 if (!capable(CAP_SYS_RESOURCE))
242 if (IS_RDONLY(inode))
245 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
249 err = ext4_group_add(sb, &input);
250 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
251 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
252 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
257 case EXT4_IOC_MIGRATE:
258 return ext4_ext_migrate(inode, filp, cmd, arg);
266 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
268 struct inode *inode = file->f_path.dentry->d_inode;
271 /* These are just misnamed, they actually get/put from/to user an int */
273 case EXT4_IOC32_GETFLAGS:
274 cmd = EXT4_IOC_GETFLAGS;
276 case EXT4_IOC32_SETFLAGS:
277 cmd = EXT4_IOC_SETFLAGS;
279 case EXT4_IOC32_GETVERSION:
280 cmd = EXT4_IOC_GETVERSION;
282 case EXT4_IOC32_SETVERSION:
283 cmd = EXT4_IOC_SETVERSION;
285 case EXT4_IOC32_GROUP_EXTEND:
286 cmd = EXT4_IOC_GROUP_EXTEND;
288 case EXT4_IOC32_GETVERSION_OLD:
289 cmd = EXT4_IOC_GETVERSION_OLD;
291 case EXT4_IOC32_SETVERSION_OLD:
292 cmd = EXT4_IOC_SETVERSION_OLD;
294 #ifdef CONFIG_JBD2_DEBUG
295 case EXT4_IOC32_WAIT_FOR_READONLY:
296 cmd = EXT4_IOC_WAIT_FOR_READONLY;
299 case EXT4_IOC32_GETRSVSZ:
300 cmd = EXT4_IOC_GETRSVSZ;
302 case EXT4_IOC32_SETRSVSZ:
303 cmd = EXT4_IOC_SETRSVSZ;
305 case EXT4_IOC_GROUP_ADD:
311 ret = ext4_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));