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 oldflags = ei->i_flags;
56 /* The JOURNAL_DATA flag is modifiable only by root */
57 jflag = flags & EXT3_JOURNAL_DATA_FL;
60 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
61 * the relevant capability.
63 * This test looks nicer. Thanks to Pauline Middelink
65 if ((oldflags & EXT3_IMMUTABLE_FL) ||
66 ((flags ^ oldflags) & (EXT3_APPEND_FL |
67 EXT3_IMMUTABLE_FL | EXT3_IUNLINK_FL))) {
68 if (!capable(CAP_LINUX_IMMUTABLE))
73 * The JOURNAL_DATA flag can only be changed by
74 * the relevant capability.
76 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
77 if (!capable(CAP_SYS_RESOURCE))
82 handle = ext3_journal_start(inode, 1);
84 return PTR_ERR(handle);
87 err = ext3_reserve_inode_write(handle, inode, &iloc);
91 flags = flags & EXT3_FL_USER_MODIFIABLE;
92 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
95 ext3_set_inode_flags(inode);
96 inode->i_ctime = CURRENT_TIME_SEC;
98 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
100 ext3_journal_stop(handle);
104 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
105 err = ext3_change_inode_journal_flag(inode, jflag);
108 case EXT3_IOC_GETVERSION:
109 case EXT3_IOC_GETVERSION_OLD:
110 return put_user(inode->i_generation, (int __user *) arg);
111 case EXT3_IOC_SETVERSION:
112 case EXT3_IOC_SETVERSION_OLD: {
114 struct ext3_iloc iloc;
118 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
120 if (IS_RDONLY(inode) ||
121 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
123 if (get_user(generation, (int __user *) arg))
126 handle = ext3_journal_start(inode, 1);
128 return PTR_ERR(handle);
129 err = ext3_reserve_inode_write(handle, inode, &iloc);
131 inode->i_ctime = CURRENT_TIME_SEC;
132 inode->i_generation = generation;
133 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
135 ext3_journal_stop(handle);
138 #ifdef CONFIG_JBD_DEBUG
139 case EXT3_IOC_WAIT_FOR_READONLY:
141 * This is racy - by the time we're woken up and running,
142 * the superblock could be released. And the module could
143 * have been unloaded. So sue me.
145 * Returns 1 if it slept, else zero.
148 struct super_block *sb = inode->i_sb;
149 DECLARE_WAITQUEUE(wait, current);
152 set_current_state(TASK_INTERRUPTIBLE);
153 add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
154 if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
158 remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
162 case EXT3_IOC_GETRSVSZ:
163 if (test_opt(inode->i_sb, RESERVATION)
164 && S_ISREG(inode->i_mode)
165 && ei->i_block_alloc_info) {
166 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
167 return put_user(rsv_window_size, (int __user *)arg);
170 case EXT3_IOC_SETRSVSZ: {
172 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
175 if (IS_RDONLY(inode) ||
176 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
179 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
182 if (get_user(rsv_window_size, (int __user *)arg))
185 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
186 rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
189 * need to allocate reservation structure for this inode
190 * before set the window size
192 down(&ei->truncate_sem);
193 if (!ei->i_block_alloc_info)
194 ext3_init_block_alloc_info(inode);
196 if (ei->i_block_alloc_info){
197 struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
198 rsv->rsv_goal_size = rsv_window_size;
200 up(&ei->truncate_sem);
203 case EXT3_IOC_GROUP_EXTEND: {
204 unsigned long n_blocks_count;
205 struct super_block *sb = inode->i_sb;
208 if (!capable(CAP_SYS_RESOURCE))
211 if (IS_RDONLY(inode) ||
212 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
215 if (get_user(n_blocks_count, (__u32 __user *)arg))
218 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
219 journal_lock_updates(EXT3_SB(sb)->s_journal);
220 journal_flush(EXT3_SB(sb)->s_journal);
221 journal_unlock_updates(EXT3_SB(sb)->s_journal);
225 case EXT3_IOC_GROUP_ADD: {
226 struct ext3_new_group_data input;
227 struct super_block *sb = inode->i_sb;
230 if (!capable(CAP_SYS_RESOURCE))
233 if (IS_RDONLY(inode) ||
234 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
237 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
241 err = ext3_group_add(sb, &input);
242 journal_lock_updates(EXT3_SB(sb)->s_journal);
243 journal_flush(EXT3_SB(sb)->s_journal);
244 journal_unlock_updates(EXT3_SB(sb)->s_journal);
249 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
250 case EXT3_IOC_SETXID: {
252 struct ext3_iloc iloc;
256 /* fixme: if stealth, return -ENOTTY */
257 if (!capable(CAP_CONTEXT))
259 if (IS_RDONLY(inode))
261 if (!(inode->i_sb->s_flags & MS_TAGXID))
263 if (get_user(xid, (int __user *) arg))
266 handle = ext3_journal_start(inode, 1);
268 return PTR_ERR(handle);
269 err = ext3_reserve_inode_write(handle, inode, &iloc);
273 inode->i_xid = (xid & 0xFFFF);
274 inode->i_ctime = CURRENT_TIME;
276 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
277 ext3_journal_stop(handle);