2 * linux/fs/ext3/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/jbd.h>
12 #include <linux/capability.h>
13 #include <linux/ext3_fs.h>
14 #include <linux/ext3_jbd.h>
15 #include <linux/mount.h>
16 #include <linux/time.h>
17 #include <linux/compat.h>
18 #include <linux/smp_lock.h>
19 #include <asm/uaccess.h>
21 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
24 struct ext3_inode_info *ei = EXT3_I(inode);
26 unsigned short rsv_window_size;
28 ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
31 case EXT3_IOC_GETFLAGS:
32 ext3_get_inode_flags(ei);
33 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
34 return put_user(flags, (int __user *) arg);
35 case EXT3_IOC_SETFLAGS: {
36 handle_t *handle = NULL;
38 struct ext3_iloc iloc;
39 unsigned int oldflags;
42 err = mnt_want_write(filp->f_path.mnt);
46 if (!is_owner_or_cap(inode)) {
51 if (get_user(flags, (int __user *) arg)) {
56 flags = ext3_mask_flags(inode->i_mode, flags);
58 mutex_lock(&inode->i_mutex);
59 /* Is it quota file? Do not allow user to mess with it */
60 if (IS_NOQUOTA(inode)) {
61 mutex_unlock(&inode->i_mutex);
65 oldflags = ei->i_flags;
67 /* The JOURNAL_DATA flag is modifiable only by root */
68 jflag = flags & EXT3_JOURNAL_DATA_FL;
71 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
72 * the relevant capability.
74 * This test looks nicer. Thanks to Pauline Middelink
76 if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
77 if (!capable(CAP_LINUX_IMMUTABLE)) {
78 mutex_unlock(&inode->i_mutex);
85 * The JOURNAL_DATA flag can only be changed by
86 * the relevant capability.
88 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
89 if (!capable(CAP_SYS_RESOURCE)) {
90 mutex_unlock(&inode->i_mutex);
97 handle = ext3_journal_start(inode, 1);
99 mutex_unlock(&inode->i_mutex);
100 err = PTR_ERR(handle);
105 err = ext3_reserve_inode_write(handle, inode, &iloc);
109 flags = flags & EXT3_FL_USER_MODIFIABLE;
110 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
113 ext3_set_inode_flags(inode);
114 inode->i_ctime = CURRENT_TIME_SEC;
116 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
118 ext3_journal_stop(handle);
120 mutex_unlock(&inode->i_mutex);
124 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
125 err = ext3_change_inode_journal_flag(inode, jflag);
126 mutex_unlock(&inode->i_mutex);
128 mnt_drop_write(filp->f_path.mnt);
131 case EXT3_IOC_GETVERSION:
132 case EXT3_IOC_GETVERSION_OLD:
133 return put_user(inode->i_generation, (int __user *) arg);
134 case EXT3_IOC_SETVERSION:
135 case EXT3_IOC_SETVERSION_OLD: {
137 struct ext3_iloc iloc;
141 if (!is_owner_or_cap(inode))
143 err = mnt_want_write(filp->f_path.mnt);
146 if (get_user(generation, (int __user *) arg)) {
150 handle = ext3_journal_start(inode, 1);
151 if (IS_ERR(handle)) {
152 err = PTR_ERR(handle);
155 err = ext3_reserve_inode_write(handle, inode, &iloc);
157 inode->i_ctime = CURRENT_TIME_SEC;
158 inode->i_generation = generation;
159 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
161 ext3_journal_stop(handle);
163 mnt_drop_write(filp->f_path.mnt);
166 #ifdef CONFIG_JBD_DEBUG
167 case EXT3_IOC_WAIT_FOR_READONLY:
169 * This is racy - by the time we're woken up and running,
170 * the superblock could be released. And the module could
171 * have been unloaded. So sue me.
173 * Returns 1 if it slept, else zero.
176 struct super_block *sb = inode->i_sb;
177 DECLARE_WAITQUEUE(wait, current);
180 set_current_state(TASK_INTERRUPTIBLE);
181 add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
182 if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
186 remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
190 case EXT3_IOC_GETRSVSZ:
191 if (test_opt(inode->i_sb, RESERVATION)
192 && S_ISREG(inode->i_mode)
193 && ei->i_block_alloc_info) {
194 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
195 return put_user(rsv_window_size, (int __user *)arg);
198 case EXT3_IOC_SETRSVSZ: {
201 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
204 err = mnt_want_write(filp->f_path.mnt);
208 if (!is_owner_or_cap(inode)) {
213 if (get_user(rsv_window_size, (int __user *)arg)) {
218 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
219 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
222 * need to allocate reservation structure for this inode
223 * before set the window size
225 mutex_lock(&ei->truncate_mutex);
226 if (!ei->i_block_alloc_info)
227 ext3_init_block_alloc_info(inode);
229 if (ei->i_block_alloc_info){
230 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
231 rsv->rsv_goal_size = rsv_window_size;
233 mutex_unlock(&ei->truncate_mutex);
235 mnt_drop_write(filp->f_path.mnt);
238 case EXT3_IOC_GROUP_EXTEND: {
239 ext3_fsblk_t n_blocks_count;
240 struct super_block *sb = inode->i_sb;
243 if (!capable(CAP_SYS_RESOURCE))
246 err = mnt_want_write(filp->f_path.mnt);
250 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
252 goto group_extend_out;
254 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
255 journal_lock_updates(EXT3_SB(sb)->s_journal);
256 err2 = journal_flush(EXT3_SB(sb)->s_journal);
257 journal_unlock_updates(EXT3_SB(sb)->s_journal);
261 mnt_drop_write(filp->f_path.mnt);
264 case EXT3_IOC_GROUP_ADD: {
265 struct ext3_new_group_data input;
266 struct super_block *sb = inode->i_sb;
269 if (!capable(CAP_SYS_RESOURCE))
272 err = mnt_want_write(filp->f_path.mnt);
276 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
282 err = ext3_group_add(sb, &input);
283 journal_lock_updates(EXT3_SB(sb)->s_journal);
284 err2 = journal_flush(EXT3_SB(sb)->s_journal);
285 journal_unlock_updates(EXT3_SB(sb)->s_journal);
289 mnt_drop_write(filp->f_path.mnt);
300 long ext3_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
302 struct inode *inode = file->f_path.dentry->d_inode;
305 /* These are just misnamed, they actually get/put from/to user an int */
307 case EXT3_IOC32_GETFLAGS:
308 cmd = EXT3_IOC_GETFLAGS;
310 case EXT3_IOC32_SETFLAGS:
311 cmd = EXT3_IOC_SETFLAGS;
313 case EXT3_IOC32_GETVERSION:
314 cmd = EXT3_IOC_GETVERSION;
316 case EXT3_IOC32_SETVERSION:
317 cmd = EXT3_IOC_SETVERSION;
319 case EXT3_IOC32_GROUP_EXTEND:
320 cmd = EXT3_IOC_GROUP_EXTEND;
322 case EXT3_IOC32_GETVERSION_OLD:
323 cmd = EXT3_IOC_GETVERSION_OLD;
325 case EXT3_IOC32_SETVERSION_OLD:
326 cmd = EXT3_IOC_SETVERSION_OLD;
328 #ifdef CONFIG_JBD_DEBUG
329 case EXT3_IOC32_WAIT_FOR_READONLY:
330 cmd = EXT3_IOC_WAIT_FOR_READONLY;
333 case EXT3_IOC32_GETRSVSZ:
334 cmd = EXT3_IOC_GETRSVSZ;
336 case EXT3_IOC32_SETRSVSZ:
337 cmd = EXT3_IOC_SETRSVSZ;
339 case EXT3_IOC_GROUP_ADD:
345 ret = ext3_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));