Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / ext3 / ioctl.c
1 /*
2  * linux/fs/ext3/ioctl.c
3  *
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)
8  */
9
10 #include <linux/fs.h>
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>
19
20
21 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
22                 unsigned long arg)
23 {
24         struct ext3_inode_info *ei = EXT3_I(inode);
25         unsigned int flags;
26         unsigned short rsv_window_size;
27
28         ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
29
30         switch (cmd) {
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;
36                 int err;
37                 struct ext3_iloc iloc;
38                 unsigned int oldflags;
39                 unsigned int jflag;
40
41                 if (IS_RDONLY(inode) ||
42                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
43                         return -EROFS;
44
45                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
46                         return -EACCES;
47
48                 if (get_user(flags, (int __user *) arg))
49                         return -EFAULT;
50
51                 if (!S_ISDIR(inode->i_mode))
52                         flags &= ~EXT3_DIRSYNC_FL;
53
54                 mutex_lock(&inode->i_mutex);
55                 oldflags = ei->i_flags;
56
57                 /* The JOURNAL_DATA flag is modifiable only by root */
58                 jflag = flags & EXT3_JOURNAL_DATA_FL;
59
60                 /*
61                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
62                  * the relevant capability.
63                  *
64                  * This test looks nicer. Thanks to Pauline Middelink
65                  */
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);
71                                 return -EPERM;
72                         }
73                 }
74
75                 /*
76                  * The JOURNAL_DATA flag can only be changed by
77                  * the relevant capability.
78                  */
79                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
80                         if (!capable(CAP_SYS_RESOURCE)) {
81                                 mutex_unlock(&inode->i_mutex);
82                                 return -EPERM;
83                         }
84                 }
85
86
87                 handle = ext3_journal_start(inode, 1);
88                 if (IS_ERR(handle)) {
89                         mutex_unlock(&inode->i_mutex);
90                         return PTR_ERR(handle);
91                 }
92                 if (IS_SYNC(inode))
93                         handle->h_sync = 1;
94                 err = ext3_reserve_inode_write(handle, inode, &iloc);
95                 if (err)
96                         goto flags_err;
97
98                 flags = flags & EXT3_FL_USER_MODIFIABLE;
99                 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
100                 ei->i_flags = flags;
101
102                 ext3_set_inode_flags(inode);
103                 inode->i_ctime = CURRENT_TIME_SEC;
104
105                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
106 flags_err:
107                 ext3_journal_stop(handle);
108                 if (err) {
109                         mutex_unlock(&inode->i_mutex);
110                         return err;
111                 }
112
113                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
114                         err = ext3_change_inode_journal_flag(inode, jflag);
115                 mutex_unlock(&inode->i_mutex);
116                 return err;
117         }
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: {
123                 handle_t *handle;
124                 struct ext3_iloc iloc;
125                 __u32 generation;
126                 int err;
127
128                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
129                         return -EPERM;
130                 if (IS_RDONLY(inode) ||
131                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
132                         return -EROFS;
133                 if (get_user(generation, (int __user *) arg))
134                         return -EFAULT;
135
136                 handle = ext3_journal_start(inode, 1);
137                 if (IS_ERR(handle))
138                         return PTR_ERR(handle);
139                 err = ext3_reserve_inode_write(handle, inode, &iloc);
140                 if (err == 0) {
141                         inode->i_ctime = CURRENT_TIME_SEC;
142                         inode->i_generation = generation;
143                         err = ext3_mark_iloc_dirty(handle, inode, &iloc);
144                 }
145                 ext3_journal_stop(handle);
146                 return err;
147         }
148 #ifdef CONFIG_JBD_DEBUG
149         case EXT3_IOC_WAIT_FOR_READONLY:
150                 /*
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.
154                  *
155                  * Returns 1 if it slept, else zero.
156                  */
157                 {
158                         struct super_block *sb = inode->i_sb;
159                         DECLARE_WAITQUEUE(wait, current);
160                         int ret = 0;
161
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)) {
165                                 schedule();
166                                 ret = 1;
167                         }
168                         remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
169                         return ret;
170                 }
171 #endif
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);
178                 }
179                 return -ENOTTY;
180         case EXT3_IOC_SETRSVSZ: {
181
182                 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
183                         return -ENOTTY;
184
185                 if (IS_RDONLY(inode) ||
186                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
187                         return -EROFS;
188
189                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
190                         return -EACCES;
191
192                 if (get_user(rsv_window_size, (int __user *)arg))
193                         return -EFAULT;
194
195                 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
196                         rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
197
198                 /*
199                  * need to allocate reservation structure for this inode
200                  * before set the window size
201                  */
202                 mutex_lock(&ei->truncate_mutex);
203                 if (!ei->i_block_alloc_info)
204                         ext3_init_block_alloc_info(inode);
205
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;
209                 }
210                 mutex_unlock(&ei->truncate_mutex);
211                 return 0;
212         }
213         case EXT3_IOC_GROUP_EXTEND: {
214                 ext3_fsblk_t n_blocks_count;
215                 struct super_block *sb = inode->i_sb;
216                 int err;
217
218                 if (!capable(CAP_SYS_RESOURCE))
219                         return -EPERM;
220
221                 if (IS_RDONLY(inode) ||
222                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
223                         return -EROFS;
224
225                 if (get_user(n_blocks_count, (__u32 __user *)arg))
226                         return -EFAULT;
227
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);
232
233                 return err;
234         }
235         case EXT3_IOC_GROUP_ADD: {
236                 struct ext3_new_group_data input;
237                 struct super_block *sb = inode->i_sb;
238                 int err;
239
240                 if (!capable(CAP_SYS_RESOURCE))
241                         return -EPERM;
242
243                 if (IS_RDONLY(inode) ||
244                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
245                         return -EROFS;
246
247                 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
248                                 sizeof(input)))
249                         return -EFAULT;
250
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);
255
256                 return err;
257         }
258
259 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
260         case EXT3_IOC_SETXID: {
261                 handle_t *handle;
262                 struct ext3_iloc iloc;
263                 int xid;
264                 int err;
265
266                 /* fixme: if stealth, return -ENOTTY */
267                 if (!capable(CAP_CONTEXT))
268                         return -EPERM;
269                 if (IS_RDONLY(inode))
270                         return -EROFS;
271                 if (!(inode->i_sb->s_flags & MS_TAGXID))
272                         return -ENOSYS;
273                 if (get_user(xid, (int __user *) arg))
274                         return -EFAULT;
275
276                 handle = ext3_journal_start(inode, 1);
277                 if (IS_ERR(handle))
278                         return PTR_ERR(handle);
279                 err = ext3_reserve_inode_write(handle, inode, &iloc);
280                 if (err)
281                         return err;
282
283                 inode->i_xid = (xid & 0xFFFF);
284                 inode->i_ctime = CURRENT_TIME;
285
286                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
287                 ext3_journal_stop(handle);
288                 return err;
289         }
290 #endif
291
292         default:
293                 return -ENOTTY;
294         }
295 }