upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[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/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>
17
18
19 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
20                 unsigned long arg)
21 {
22         struct ext3_inode_info *ei = EXT3_I(inode);
23         unsigned int flags;
24         unsigned short rsv_window_size;
25
26         ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
27
28         switch (cmd) {
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;
34                 int err;
35                 struct ext3_iloc iloc;
36                 unsigned int oldflags;
37                 unsigned int jflag;
38
39                 if (IS_RDONLY(inode) ||
40                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
41                         return -EROFS;
42
43                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
44                         return -EACCES;
45
46                 if (get_user(flags, (int __user *) arg))
47                         return -EFAULT;
48
49                 if (!S_ISDIR(inode->i_mode))
50                         flags &= ~EXT3_DIRSYNC_FL;
51
52                 oldflags = ei->i_flags;
53
54                 /* The JOURNAL_DATA flag is modifiable only by root */
55                 jflag = flags & EXT3_JOURNAL_DATA_FL;
56
57                 /*
58                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
59                  * the relevant capability.
60                  *
61                  * This test looks nicer. Thanks to Pauline Middelink
62                  */
63                 if ((oldflags & EXT3_IMMUTABLE_FL) ||
64                         ((flags ^ oldflags) & (EXT3_APPEND_FL |
65                         EXT3_IMMUTABLE_FL | EXT3_IUNLINK_FL))) {
66                         if (!capable(CAP_LINUX_IMMUTABLE))
67                                 return -EPERM;
68                 }
69
70                 /*
71                  * The JOURNAL_DATA flag can only be changed by
72                  * the relevant capability.
73                  */
74                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
75                         if (!capable(CAP_SYS_RESOURCE))
76                                 return -EPERM;
77                 }
78
79
80                 handle = ext3_journal_start(inode, 1);
81                 if (IS_ERR(handle))
82                         return PTR_ERR(handle);
83                 if (IS_SYNC(inode))
84                         handle->h_sync = 1;
85                 err = ext3_reserve_inode_write(handle, inode, &iloc);
86                 if (err)
87                         goto flags_err;
88
89                 flags = flags & EXT3_FL_USER_MODIFIABLE;
90                 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
91                 ei->i_flags = flags;
92
93                 ext3_set_inode_flags(inode);
94                 inode->i_ctime = CURRENT_TIME_SEC;
95
96                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
97 flags_err:
98                 ext3_journal_stop(handle);
99                 if (err)
100                         return err;
101
102                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
103                         err = ext3_change_inode_journal_flag(inode, jflag);
104                 return err;
105         }
106         case EXT3_IOC_GETVERSION:
107         case EXT3_IOC_GETVERSION_OLD:
108                 return put_user(inode->i_generation, (int __user *) arg);
109         case EXT3_IOC_SETVERSION:
110         case EXT3_IOC_SETVERSION_OLD: {
111                 handle_t *handle;
112                 struct ext3_iloc iloc;
113                 __u32 generation;
114                 int err;
115
116                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
117                         return -EPERM;
118                 if (IS_RDONLY(inode) ||
119                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
120                         return -EROFS;
121                 if (get_user(generation, (int __user *) arg))
122                         return -EFAULT;
123
124                 handle = ext3_journal_start(inode, 1);
125                 if (IS_ERR(handle))
126                         return PTR_ERR(handle);
127                 err = ext3_reserve_inode_write(handle, inode, &iloc);
128                 if (err == 0) {
129                         inode->i_ctime = CURRENT_TIME_SEC;
130                         inode->i_generation = generation;
131                         err = ext3_mark_iloc_dirty(handle, inode, &iloc);
132                 }
133                 ext3_journal_stop(handle);
134                 return err;
135         }
136 #ifdef CONFIG_JBD_DEBUG
137         case EXT3_IOC_WAIT_FOR_READONLY:
138                 /*
139                  * This is racy - by the time we're woken up and running,
140                  * the superblock could be released.  And the module could
141                  * have been unloaded.  So sue me.
142                  *
143                  * Returns 1 if it slept, else zero.
144                  */
145                 {
146                         struct super_block *sb = inode->i_sb;
147                         DECLARE_WAITQUEUE(wait, current);
148                         int ret = 0;
149
150                         set_current_state(TASK_INTERRUPTIBLE);
151                         add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
152                         if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
153                                 schedule();
154                                 ret = 1;
155                         }
156                         remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
157                         return ret;
158                 }
159 #endif
160         case EXT3_IOC_GETRSVSZ:
161                 if (test_opt(inode->i_sb, RESERVATION)
162                         && S_ISREG(inode->i_mode)
163                         && ei->i_block_alloc_info) {
164                         rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
165                         return put_user(rsv_window_size, (int __user *)arg);
166                 }
167                 return -ENOTTY;
168         case EXT3_IOC_SETRSVSZ: {
169
170                 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
171                         return -ENOTTY;
172
173                 if (IS_RDONLY(inode))
174                         return -EROFS;
175
176                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
177                         return -EACCES;
178
179                 if (get_user(rsv_window_size, (int __user *)arg))
180                         return -EFAULT;
181
182                 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
183                         rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
184
185                 /*
186                  * need to allocate reservation structure for this inode
187                  * before set the window size
188                  */
189                 down(&ei->truncate_sem);
190                 if (!ei->i_block_alloc_info)
191                         ext3_init_block_alloc_info(inode);
192
193                 if (ei->i_block_alloc_info){
194                         struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
195                         rsv->rsv_goal_size = rsv_window_size;
196                 }
197                 up(&ei->truncate_sem);
198                 return 0;
199         }
200         case EXT3_IOC_GROUP_EXTEND: {
201                 unsigned long n_blocks_count;
202                 struct super_block *sb = inode->i_sb;
203                 int err;
204
205                 if (!capable(CAP_SYS_RESOURCE))
206                         return -EPERM;
207
208                 if (IS_RDONLY(inode))
209                         return -EROFS;
210
211                 if (get_user(n_blocks_count, (__u32 __user *)arg))
212                         return -EFAULT;
213
214                 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
215                 journal_lock_updates(EXT3_SB(sb)->s_journal);
216                 journal_flush(EXT3_SB(sb)->s_journal);
217                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
218
219                 return err;
220         }
221         case EXT3_IOC_GROUP_ADD: {
222                 struct ext3_new_group_data input;
223                 struct super_block *sb = inode->i_sb;
224                 int err;
225
226                 if (!capable(CAP_SYS_RESOURCE))
227                         return -EPERM;
228
229                 if (IS_RDONLY(inode))
230                         return -EROFS;
231
232                 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
233                                 sizeof(input)))
234                         return -EFAULT;
235
236                 err = ext3_group_add(sb, &input);
237                 journal_lock_updates(EXT3_SB(sb)->s_journal);
238                 journal_flush(EXT3_SB(sb)->s_journal);
239                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
240
241                 return err;
242         }
243
244 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
245         case EXT3_IOC_SETXID: {
246                 handle_t *handle;
247                 struct ext3_iloc iloc;
248                 int xid;
249                 int err;
250
251                 /* fixme: if stealth, return -ENOTTY */
252                 if (!capable(CAP_CONTEXT))
253                         return -EPERM;
254                 if (IS_RDONLY(inode))
255                         return -EROFS;
256                 if (!(inode->i_sb->s_flags & MS_TAGXID))
257                         return -ENOSYS;
258                 if (get_user(xid, (int *) arg))
259                         return -EFAULT;
260
261                 handle = ext3_journal_start(inode, 1);
262                 if (IS_ERR(handle))
263                         return PTR_ERR(handle);
264                 err = ext3_reserve_inode_write(handle, inode, &iloc);
265                 if (err)
266                         return err;
267
268                 inode->i_xid = (xid & 0xFFFF);
269                 inode->i_ctime = CURRENT_TIME;
270
271                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
272                 ext3_journal_stop(handle);
273                 return err;
274         }
275 #endif
276
277         default:
278                 return -ENOTTY;
279         }
280 }