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))
85 if (oldflags & EXT4_EXTENTS_FL) {
86 /* We don't support clearning extent flags */
87 if (!(flags & EXT4_EXTENTS_FL)) {
91 } else if (flags & EXT4_EXTENTS_FL) {
92 /* migrate the file */
94 flags &= ~EXT4_EXTENTS_FL;
97 handle = ext4_journal_start(inode, 1);
99 err = PTR_ERR(handle);
104 err = ext4_reserve_inode_write(handle, inode, &iloc);
108 flags = flags & EXT4_FL_USER_MODIFIABLE;
109 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
112 ext4_set_inode_flags(inode);
113 inode->i_ctime = ext4_current_time(inode);
115 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
117 ext4_journal_stop(handle);
121 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
122 err = ext4_change_inode_journal_flag(inode, jflag);
126 err = ext4_ext_migrate(inode);
128 mutex_unlock(&inode->i_mutex);
129 mnt_drop_write(filp->f_path.mnt);
132 case EXT4_IOC_GETVERSION:
133 case EXT4_IOC_GETVERSION_OLD:
134 return put_user(inode->i_generation, (int __user *) arg);
135 case EXT4_IOC_SETVERSION:
136 case EXT4_IOC_SETVERSION_OLD: {
138 struct ext4_iloc iloc;
142 if (!is_owner_or_cap(inode))
145 err = mnt_want_write(filp->f_path.mnt);
148 if (get_user(generation, (int __user *) arg)) {
153 handle = ext4_journal_start(inode, 1);
154 if (IS_ERR(handle)) {
155 err = PTR_ERR(handle);
158 err = ext4_reserve_inode_write(handle, inode, &iloc);
160 inode->i_ctime = ext4_current_time(inode);
161 inode->i_generation = generation;
162 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
164 ext4_journal_stop(handle);
166 mnt_drop_write(filp->f_path.mnt);
169 #ifdef CONFIG_JBD2_DEBUG
170 case EXT4_IOC_WAIT_FOR_READONLY:
172 * This is racy - by the time we're woken up and running,
173 * the superblock could be released. And the module could
174 * have been unloaded. So sue me.
176 * Returns 1 if it slept, else zero.
179 struct super_block *sb = inode->i_sb;
180 DECLARE_WAITQUEUE(wait, current);
183 set_current_state(TASK_INTERRUPTIBLE);
184 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
185 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
189 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
193 case EXT4_IOC_GETRSVSZ:
194 if (test_opt(inode->i_sb, RESERVATION)
195 && S_ISREG(inode->i_mode)
196 && ei->i_block_alloc_info) {
197 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
198 return put_user(rsv_window_size, (int __user *)arg);
201 case EXT4_IOC_SETRSVSZ: {
204 if (!test_opt(inode->i_sb, RESERVATION) || !S_ISREG(inode->i_mode))
207 if (!is_owner_or_cap(inode))
210 if (get_user(rsv_window_size, (int __user *)arg))
213 err = mnt_want_write(filp->f_path.mnt);
217 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
218 rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
221 * need to allocate reservation structure for this inode
222 * before set the window size
224 down_write(&ei->i_data_sem);
225 if (!ei->i_block_alloc_info)
226 ext4_init_block_alloc_info(inode);
228 if (ei->i_block_alloc_info){
229 struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
230 rsv->rsv_goal_size = rsv_window_size;
232 up_write(&ei->i_data_sem);
233 mnt_drop_write(filp->f_path.mnt);
236 case EXT4_IOC_GROUP_EXTEND: {
237 ext4_fsblk_t n_blocks_count;
238 struct super_block *sb = inode->i_sb;
241 if (!capable(CAP_SYS_RESOURCE))
244 if (get_user(n_blocks_count, (__u32 __user *)arg))
247 err = mnt_want_write(filp->f_path.mnt);
251 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
252 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
253 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
254 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
255 mnt_drop_write(filp->f_path.mnt);
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 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
277 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
278 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
279 mnt_drop_write(filp->f_path.mnt);
284 case EXT4_IOC_MIGRATE:
287 if (!is_owner_or_cap(inode))
290 err = mnt_want_write(filp->f_path.mnt);
294 * inode_mutex prevent write and truncate on the file.
295 * Read still goes through. We take i_data_sem in
296 * ext4_ext_swap_inode_data before we switch the
297 * inode format to prevent read.
299 mutex_lock(&(inode->i_mutex));
300 err = ext4_ext_migrate(inode);
301 mutex_unlock(&(inode->i_mutex));
302 mnt_drop_write(filp->f_path.mnt);
312 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
314 /* These are just misnamed, they actually get/put from/to user an int */
316 case EXT4_IOC32_GETFLAGS:
317 cmd = EXT4_IOC_GETFLAGS;
319 case EXT4_IOC32_SETFLAGS:
320 cmd = EXT4_IOC_SETFLAGS;
322 case EXT4_IOC32_GETVERSION:
323 cmd = EXT4_IOC_GETVERSION;
325 case EXT4_IOC32_SETVERSION:
326 cmd = EXT4_IOC_SETVERSION;
328 case EXT4_IOC32_GROUP_EXTEND:
329 cmd = EXT4_IOC_GROUP_EXTEND;
331 case EXT4_IOC32_GETVERSION_OLD:
332 cmd = EXT4_IOC_GETVERSION_OLD;
334 case EXT4_IOC32_SETVERSION_OLD:
335 cmd = EXT4_IOC_SETVERSION_OLD;
337 #ifdef CONFIG_JBD2_DEBUG
338 case EXT4_IOC32_WAIT_FOR_READONLY:
339 cmd = EXT4_IOC_WAIT_FOR_READONLY;
342 case EXT4_IOC32_GETRSVSZ:
343 cmd = EXT4_IOC_GETRSVSZ;
345 case EXT4_IOC32_SETRSVSZ:
346 cmd = EXT4_IOC_SETRSVSZ;
348 case EXT4_IOC_GROUP_ADD:
353 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));