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);
26 unsigned short rsv_window_size;
28 ext4_debug ("cmd = %u, arg = %lu\n", cmd, arg);
31 case EXT4_IOC_GETFLAGS:
32 ext4_get_inode_flags(ei);
33 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
34 return put_user(flags, (int __user *) arg);
35 case EXT4_IOC_SETFLAGS: {
36 handle_t *handle = NULL;
38 struct ext4_iloc iloc;
39 unsigned int oldflags;
42 if (!is_owner_or_cap(inode))
45 if (get_user(flags, (int __user *) arg))
48 err = mnt_want_write(filp->f_path.mnt);
52 if (!S_ISDIR(inode->i_mode))
53 flags &= ~EXT4_DIRSYNC_FL;
56 mutex_lock(&inode->i_mutex);
57 /* Is it quota file? Do not allow user to mess with it */
58 if (IS_NOQUOTA(inode))
61 oldflags = ei->i_flags;
63 /* The JOURNAL_DATA flag is modifiable only by root */
64 jflag = flags & EXT4_JOURNAL_DATA_FL;
67 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
68 * the relevant capability.
70 * This test looks nicer. Thanks to Pauline Middelink
72 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
73 if (!capable(CAP_LINUX_IMMUTABLE))
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))
86 handle = ext4_journal_start(inode, 1);
88 err = PTR_ERR(handle);
93 err = ext4_reserve_inode_write(handle, inode, &iloc);
97 flags = flags & EXT4_FL_USER_MODIFIABLE;
98 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
101 ext4_set_inode_flags(inode);
102 inode->i_ctime = ext4_current_time(inode);
104 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
106 ext4_journal_stop(handle);
110 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
111 err = ext4_change_inode_journal_flag(inode, jflag);
113 mutex_unlock(&inode->i_mutex);
114 mnt_drop_write(filp->f_path.mnt);
117 case EXT4_IOC_GETVERSION:
118 case EXT4_IOC_GETVERSION_OLD:
119 return put_user(inode->i_generation, (int __user *) arg);
120 case EXT4_IOC_SETVERSION:
121 case EXT4_IOC_SETVERSION_OLD: {
123 struct ext4_iloc iloc;
127 if (!is_owner_or_cap(inode))
130 err = mnt_want_write(filp->f_path.mnt);
133 if (get_user(generation, (int __user *) arg)) {
138 handle = ext4_journal_start(inode, 1);
139 if (IS_ERR(handle)) {
140 err = PTR_ERR(handle);
143 err = ext4_reserve_inode_write(handle, inode, &iloc);
145 inode->i_ctime = ext4_current_time(inode);
146 inode->i_generation = generation;
147 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
149 ext4_journal_stop(handle);
151 mnt_drop_write(filp->f_path.mnt);
154 #ifdef CONFIG_JBD2_DEBUG
155 case EXT4_IOC_WAIT_FOR_READONLY:
157 * This is racy - by the time we're woken up and running,
158 * the superblock could be released. And the module could
159 * have been unloaded. So sue me.
161 * Returns 1 if it slept, else zero.
164 struct super_block *sb = inode->i_sb;
165 DECLARE_WAITQUEUE(wait, current);
168 set_current_state(TASK_INTERRUPTIBLE);
169 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
170 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
174 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
178 case EXT4_IOC_GETRSVSZ:
179 if (test_opt(inode->i_sb, RESERVATION)
180 && S_ISREG(inode->i_mode)
181 && ei->i_block_alloc_info) {
182 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
183 return put_user(rsv_window_size, (int __user *)arg);
186 case EXT4_IOC_SETRSVSZ: {
189 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
192 if (!is_owner_or_cap(inode))
195 if (get_user(rsv_window_size, (int __user *)arg))
198 err = mnt_want_write(filp->f_path.mnt);
202 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
203 rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
206 * need to allocate reservation structure for this inode
207 * before set the window size
209 down_write(&ei->i_data_sem);
210 if (!ei->i_block_alloc_info)
211 ext4_init_block_alloc_info(inode);
213 if (ei->i_block_alloc_info){
214 struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
215 rsv->rsv_goal_size = rsv_window_size;
217 up_write(&ei->i_data_sem);
218 mnt_drop_write(filp->f_path.mnt);
221 case EXT4_IOC_GROUP_EXTEND: {
222 ext4_fsblk_t n_blocks_count;
223 struct super_block *sb = inode->i_sb;
226 if (!capable(CAP_SYS_RESOURCE))
229 if (get_user(n_blocks_count, (__u32 __user *)arg))
232 err = mnt_want_write(filp->f_path.mnt);
236 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
237 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
238 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
239 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
240 mnt_drop_write(filp->f_path.mnt);
244 case EXT4_IOC_GROUP_ADD: {
245 struct ext4_new_group_data input;
246 struct super_block *sb = inode->i_sb;
249 if (!capable(CAP_SYS_RESOURCE))
252 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
256 err = mnt_want_write(filp->f_path.mnt);
260 err = ext4_group_add(sb, &input);
261 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
262 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
263 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
264 mnt_drop_write(filp->f_path.mnt);
269 case EXT4_IOC_MIGRATE:
270 return ext4_ext_migrate(inode, filp, cmd, arg);
278 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
280 /* These are just misnamed, they actually get/put from/to user an int */
282 case EXT4_IOC32_GETFLAGS:
283 cmd = EXT4_IOC_GETFLAGS;
285 case EXT4_IOC32_SETFLAGS:
286 cmd = EXT4_IOC_SETFLAGS;
288 case EXT4_IOC32_GETVERSION:
289 cmd = EXT4_IOC_GETVERSION;
291 case EXT4_IOC32_SETVERSION:
292 cmd = EXT4_IOC_SETVERSION;
294 case EXT4_IOC32_GROUP_EXTEND:
295 cmd = EXT4_IOC_GROUP_EXTEND;
297 case EXT4_IOC32_GETVERSION_OLD:
298 cmd = EXT4_IOC_GETVERSION_OLD;
300 case EXT4_IOC32_SETVERSION_OLD:
301 cmd = EXT4_IOC_SETVERSION_OLD;
303 #ifdef CONFIG_JBD2_DEBUG
304 case EXT4_IOC32_WAIT_FOR_READONLY:
305 cmd = EXT4_IOC_WAIT_FOR_READONLY;
308 case EXT4_IOC32_GETRSVSZ:
309 cmd = EXT4_IOC_GETRSVSZ;
311 case EXT4_IOC32_SETRSVSZ:
312 cmd = EXT4_IOC_SETRSVSZ;
314 case EXT4_IOC_GROUP_ADD:
319 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));