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/mount.h>
12 #include <linux/jbd.h>
13 #include <linux/capability.h>
14 #include <linux/ext3_fs.h>
15 #include <linux/ext3_jbd.h>
16 #include <linux/time.h>
17 #include <linux/vserver/xid.h>
18 #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 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
33 return put_user(flags, (int __user *) arg);
34 case EXT3_IOC_SETFLAGS: {
35 handle_t *handle = NULL;
37 struct ext3_iloc iloc;
38 unsigned int oldflags;
41 if (IS_RDONLY(inode) ||
42 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
45 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
48 if (get_user(flags, (int __user *) arg))
51 if (!S_ISDIR(inode->i_mode))
52 flags &= ~EXT3_DIRSYNC_FL;
54 mutex_lock(&inode->i_mutex);
55 oldflags = ei->i_flags;
57 /* The JOURNAL_DATA flag is modifiable only by root */
58 jflag = flags & EXT3_JOURNAL_DATA_FL;
61 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
62 * the relevant capability.
64 * This test looks nicer. Thanks to Pauline Middelink
66 if ((oldflags & EXT3_IMMUTABLE_FL) ||
67 ((flags ^ oldflags) & (EXT3_APPEND_FL |
68 EXT3_IMMUTABLE_FL | EXT3_IUNLINK_FL))) {
69 if (!capable(CAP_LINUX_IMMUTABLE)) {
70 mutex_unlock(&inode->i_mutex);
76 * The JOURNAL_DATA flag can only be changed by
77 * the relevant capability.
79 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
80 if (!capable(CAP_SYS_RESOURCE)) {
81 mutex_unlock(&inode->i_mutex);
87 handle = ext3_journal_start(inode, 1);
89 mutex_unlock(&inode->i_mutex);
90 return PTR_ERR(handle);
94 err = ext3_reserve_inode_write(handle, inode, &iloc);
98 flags = flags & EXT3_FL_USER_MODIFIABLE;
99 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
102 ext3_set_inode_flags(inode);
103 inode->i_ctime = CURRENT_TIME_SEC;
105 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
107 ext3_journal_stop(handle);
109 mutex_unlock(&inode->i_mutex);
113 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
114 err = ext3_change_inode_journal_flag(inode, jflag);
115 mutex_unlock(&inode->i_mutex);
118 case EXT3_IOC_GETVERSION:
119 case EXT3_IOC_GETVERSION_OLD:
120 return put_user(inode->i_generation, (int __user *) arg);
121 case EXT3_IOC_SETVERSION:
122 case EXT3_IOC_SETVERSION_OLD: {
124 struct ext3_iloc iloc;
128 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
130 if (IS_RDONLY(inode) ||
131 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
133 if (get_user(generation, (int __user *) arg))
136 handle = ext3_journal_start(inode, 1);
138 return PTR_ERR(handle);
139 err = ext3_reserve_inode_write(handle, inode, &iloc);
141 inode->i_ctime = CURRENT_TIME_SEC;
142 inode->i_generation = generation;
143 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
145 ext3_journal_stop(handle);
148 #ifdef CONFIG_JBD_DEBUG
149 case EXT3_IOC_WAIT_FOR_READONLY:
151 * This is racy - by the time we're woken up and running,
152 * the superblock could be released. And the module could
153 * have been unloaded. So sue me.
155 * Returns 1 if it slept, else zero.
158 struct super_block *sb = inode->i_sb;
159 DECLARE_WAITQUEUE(wait, current);
162 set_current_state(TASK_INTERRUPTIBLE);
163 add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
164 if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
168 remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
172 case EXT3_IOC_GETRSVSZ:
173 if (test_opt(inode->i_sb, RESERVATION)
174 && S_ISREG(inode->i_mode)
175 && ei->i_block_alloc_info) {
176 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
177 return put_user(rsv_window_size, (int __user *)arg);
180 case EXT3_IOC_SETRSVSZ: {
182 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
185 if (IS_RDONLY(inode) ||
186 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
189 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
192 if (get_user(rsv_window_size, (int __user *)arg))
195 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
196 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
199 * need to allocate reservation structure for this inode
200 * before set the window size
202 mutex_lock(&ei->truncate_mutex);
203 if (!ei->i_block_alloc_info)
204 ext3_init_block_alloc_info(inode);
206 if (ei->i_block_alloc_info){
207 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
208 rsv->rsv_goal_size = rsv_window_size;
210 mutex_unlock(&ei->truncate_mutex);
213 case EXT3_IOC_GROUP_EXTEND: {
214 unsigned long n_blocks_count;
215 struct super_block *sb = inode->i_sb;
218 if (!capable(CAP_SYS_RESOURCE))
221 if (IS_RDONLY(inode) ||
222 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
225 if (get_user(n_blocks_count, (__u32 __user *)arg))
228 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
229 journal_lock_updates(EXT3_SB(sb)->s_journal);
230 journal_flush(EXT3_SB(sb)->s_journal);
231 journal_unlock_updates(EXT3_SB(sb)->s_journal);
235 case EXT3_IOC_GROUP_ADD: {
236 struct ext3_new_group_data input;
237 struct super_block *sb = inode->i_sb;
240 if (!capable(CAP_SYS_RESOURCE))
243 if (IS_RDONLY(inode) ||
244 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
247 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
251 err = ext3_group_add(sb, &input);
252 journal_lock_updates(EXT3_SB(sb)->s_journal);
253 journal_flush(EXT3_SB(sb)->s_journal);
254 journal_unlock_updates(EXT3_SB(sb)->s_journal);
259 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
260 case EXT3_IOC_SETXID: {
262 struct ext3_iloc iloc;
266 /* fixme: if stealth, return -ENOTTY */
267 if (!capable(CAP_CONTEXT))
269 if (IS_RDONLY(inode))
271 if (!(inode->i_sb->s_flags & MS_TAGXID))
273 if (get_user(xid, (int __user *) arg))
276 handle = ext3_journal_start(inode, 1);
278 return PTR_ERR(handle);
279 err = ext3_reserve_inode_write(handle, inode, &iloc);
283 inode->i_xid = (xid & 0xFFFF);
284 inode->i_ctime = CURRENT_TIME;
286 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
287 ext3_journal_stop(handle);