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/ext3_fs.h>
13 #include <linux/ext3_jbd.h>
14 #include <linux/time.h>
15 #include <linux/vserver/xid.h>
16 #include <asm/uaccess.h>
19 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
22 struct ext3_inode_info *ei = EXT3_I(inode);
24 unsigned short rsv_window_size;
26 ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
29 case EXT3_IOC_GETFLAGS:
30 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
31 return put_user(flags, (int __user *) arg);
32 case EXT3_IOC_SETFLAGS: {
33 handle_t *handle = NULL;
35 struct ext3_iloc iloc;
36 unsigned int oldflags;
42 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
45 if (get_user(flags, (int __user *) arg))
48 if (!S_ISDIR(inode->i_mode))
49 flags &= ~EXT3_DIRSYNC_FL;
51 oldflags = ei->i_flags;
53 /* The JOURNAL_DATA flag is modifiable only by root */
54 jflag = flags & EXT3_JOURNAL_DATA_FL;
57 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
58 * the relevant capability.
60 * This test looks nicer. Thanks to Pauline Middelink
62 if ((oldflags & EXT3_IMMUTABLE_FL) ||
63 ((flags ^ oldflags) & (EXT3_APPEND_FL |
64 EXT3_IMMUTABLE_FL | EXT3_IUNLINK_FL))) {
65 if (!capable(CAP_LINUX_IMMUTABLE))
70 * The JOURNAL_DATA flag can only be changed by
71 * the relevant capability.
73 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
74 if (!capable(CAP_SYS_RESOURCE))
79 handle = ext3_journal_start(inode, 1);
81 return PTR_ERR(handle);
84 err = ext3_reserve_inode_write(handle, inode, &iloc);
88 flags = flags & EXT3_FL_USER_MODIFIABLE;
89 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
92 ext3_set_inode_flags(inode);
93 inode->i_ctime = CURRENT_TIME_SEC;
95 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
97 ext3_journal_stop(handle);
101 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
102 err = ext3_change_inode_journal_flag(inode, jflag);
105 case EXT3_IOC_GETVERSION:
106 case EXT3_IOC_GETVERSION_OLD:
107 return put_user(inode->i_generation, (int __user *) arg);
108 case EXT3_IOC_SETVERSION:
109 case EXT3_IOC_SETVERSION_OLD: {
111 struct ext3_iloc iloc;
115 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
117 if (IS_RDONLY(inode))
119 if (get_user(generation, (int __user *) arg))
122 handle = ext3_journal_start(inode, 1);
124 return PTR_ERR(handle);
125 err = ext3_reserve_inode_write(handle, inode, &iloc);
127 inode->i_ctime = CURRENT_TIME_SEC;
128 inode->i_generation = generation;
129 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
131 ext3_journal_stop(handle);
134 #ifdef CONFIG_JBD_DEBUG
135 case EXT3_IOC_WAIT_FOR_READONLY:
137 * This is racy - by the time we're woken up and running,
138 * the superblock could be released. And the module could
139 * have been unloaded. So sue me.
141 * Returns 1 if it slept, else zero.
144 struct super_block *sb = inode->i_sb;
145 DECLARE_WAITQUEUE(wait, current);
148 set_current_state(TASK_INTERRUPTIBLE);
149 add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
150 if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
154 remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
158 case EXT3_IOC_GETRSVSZ:
159 if (test_opt(inode->i_sb, RESERVATION)
160 && S_ISREG(inode->i_mode)
161 && ei->i_block_alloc_info) {
162 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
163 return put_user(rsv_window_size, (int __user *)arg);
166 case EXT3_IOC_SETRSVSZ: {
168 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
171 if (IS_RDONLY(inode))
174 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
177 if (get_user(rsv_window_size, (int __user *)arg))
180 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
181 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
184 * need to allocate reservation structure for this inode
185 * before set the window size
187 down(&ei->truncate_sem);
188 if (!ei->i_block_alloc_info)
189 ext3_init_block_alloc_info(inode);
191 if (ei->i_block_alloc_info){
192 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
193 rsv->rsv_goal_size = rsv_window_size;
195 up(&ei->truncate_sem);
198 case EXT3_IOC_GROUP_EXTEND: {
199 unsigned long n_blocks_count;
200 struct super_block *sb = inode->i_sb;
203 if (!capable(CAP_SYS_RESOURCE))
206 if (IS_RDONLY(inode))
209 if (get_user(n_blocks_count, (__u32 __user *)arg))
212 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
213 journal_lock_updates(EXT3_SB(sb)->s_journal);
214 journal_flush(EXT3_SB(sb)->s_journal);
215 journal_unlock_updates(EXT3_SB(sb)->s_journal);
219 case EXT3_IOC_GROUP_ADD: {
220 struct ext3_new_group_data input;
221 struct super_block *sb = inode->i_sb;
224 if (!capable(CAP_SYS_RESOURCE))
227 if (IS_RDONLY(inode))
230 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
234 err = ext3_group_add(sb, &input);
235 journal_lock_updates(EXT3_SB(sb)->s_journal);
236 journal_flush(EXT3_SB(sb)->s_journal);
237 journal_unlock_updates(EXT3_SB(sb)->s_journal);
242 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
243 case EXT3_IOC_SETXID: {
245 struct ext3_iloc iloc;
249 /* fixme: if stealth, return -ENOTTY */
250 if (!capable(CAP_CONTEXT))
252 if (IS_RDONLY(inode))
254 if (!(inode->i_sb->s_flags & MS_TAGXID))
256 if (get_user(xid, (int *) arg))
259 handle = ext3_journal_start(inode, 1);
261 return PTR_ERR(handle);
262 err = ext3_reserve_inode_write(handle, inode, &iloc);
266 inode->i_xid = (xid & 0xFFFF);
267 inode->i_ctime = CURRENT_TIME;
269 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
270 ext3_journal_stop(handle);