This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / ext4 / ioctl.c
1 /*
2  * linux/fs/ext4/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/jbd2.h>
13 #include <linux/capability.h>
14 #include <linux/ext4_fs.h>
15 #include <linux/ext4_jbd2.h>
16 #include <linux/time.h>
17 #include <linux/compat.h>
18 #include <linux/smp_lock.h>
19 #include <linux/vs_tag.h>
20 #include <asm/uaccess.h>
21
22 int ext4_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
23                 unsigned long arg)
24 {
25         struct ext4_inode_info *ei = EXT4_I(inode);
26         unsigned int flags;
27         unsigned short rsv_window_size;
28
29         ext4_debug ("cmd = %u, arg = %lu\n", cmd, arg);
30
31         switch (cmd) {
32         case EXT4_IOC_GETFLAGS:
33                 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
34                 return put_user(flags, (int __user *) arg);
35         case EXT4_IOC_SETFLAGS: {
36                 handle_t *handle = NULL;
37                 int err;
38                 struct ext4_iloc iloc;
39                 unsigned int oldflags;
40                 unsigned int jflag;
41
42                 if (IS_RDONLY(inode) ||
43                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
44                         return -EROFS;
45
46                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
47                         return -EACCES;
48
49                 if (get_user(flags, (int __user *) arg))
50                         return -EFAULT;
51
52                 if (!S_ISDIR(inode->i_mode))
53                         flags &= ~EXT4_DIRSYNC_FL;
54
55                 mutex_lock(&inode->i_mutex);
56                 oldflags = ei->i_flags;
57
58                 /* The JOURNAL_DATA flag is modifiable only by root */
59                 jflag = flags & EXT4_JOURNAL_DATA_FL;
60
61                 /*
62                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
63                  * the relevant capability.
64                  *
65                  * This test looks nicer. Thanks to Pauline Middelink
66                  */
67                 if ((oldflags & EXT4_IMMUTABLE_FL) ||
68                         ((flags ^ oldflags) & (EXT4_APPEND_FL |
69                         EXT4_IMMUTABLE_FL | EXT4_IUNLINK_FL))) {
70                         if (!capable(CAP_LINUX_IMMUTABLE)) {
71                                 mutex_unlock(&inode->i_mutex);
72                                 return -EPERM;
73                         }
74                 }
75
76                 /*
77                  * The JOURNAL_DATA flag can only be changed by
78                  * the relevant capability.
79                  */
80                 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
81                         if (!capable(CAP_SYS_RESOURCE)) {
82                                 mutex_unlock(&inode->i_mutex);
83                                 return -EPERM;
84                         }
85                 }
86
87
88                 handle = ext4_journal_start(inode, 1);
89                 if (IS_ERR(handle)) {
90                         mutex_unlock(&inode->i_mutex);
91                         return PTR_ERR(handle);
92                 }
93                 if (IS_SYNC(inode))
94                         handle->h_sync = 1;
95                 err = ext4_reserve_inode_write(handle, inode, &iloc);
96                 if (err)
97                         goto flags_err;
98
99                 flags = flags & EXT4_FL_USER_MODIFIABLE;
100                 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
101                 ei->i_flags = flags;
102
103                 ext4_set_inode_flags(inode);
104                 inode->i_ctime = CURRENT_TIME_SEC;
105
106                 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
107 flags_err:
108                 ext4_journal_stop(handle);
109                 if (err) {
110                         mutex_unlock(&inode->i_mutex);
111                         return err;
112                 }
113
114                 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
115                         err = ext4_change_inode_journal_flag(inode, jflag);
116                 mutex_unlock(&inode->i_mutex);
117                 return err;
118         }
119         case EXT4_IOC_GETVERSION:
120         case EXT4_IOC_GETVERSION_OLD:
121                 return put_user(inode->i_generation, (int __user *) arg);
122         case EXT4_IOC_SETVERSION:
123         case EXT4_IOC_SETVERSION_OLD: {
124                 handle_t *handle;
125                 struct ext4_iloc iloc;
126                 __u32 generation;
127                 int err;
128
129                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
130                         return -EPERM;
131                 if (IS_RDONLY(inode) ||
132                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
133                         return -EROFS;
134                 if (get_user(generation, (int __user *) arg))
135                         return -EFAULT;
136
137                 handle = ext4_journal_start(inode, 1);
138                 if (IS_ERR(handle))
139                         return PTR_ERR(handle);
140                 err = ext4_reserve_inode_write(handle, inode, &iloc);
141                 if (err == 0) {
142                         inode->i_ctime = CURRENT_TIME_SEC;
143                         inode->i_generation = generation;
144                         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
145                 }
146                 ext4_journal_stop(handle);
147                 return err;
148         }
149 #ifdef CONFIG_JBD_DEBUG
150         case EXT4_IOC_WAIT_FOR_READONLY:
151                 /*
152                  * This is racy - by the time we're woken up and running,
153                  * the superblock could be released.  And the module could
154                  * have been unloaded.  So sue me.
155                  *
156                  * Returns 1 if it slept, else zero.
157                  */
158                 {
159                         struct super_block *sb = inode->i_sb;
160                         DECLARE_WAITQUEUE(wait, current);
161                         int ret = 0;
162
163                         set_current_state(TASK_INTERRUPTIBLE);
164                         add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
165                         if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
166                                 schedule();
167                                 ret = 1;
168                         }
169                         remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
170                         return ret;
171                 }
172 #endif
173         case EXT4_IOC_GETRSVSZ:
174                 if (test_opt(inode->i_sb, RESERVATION)
175                         && S_ISREG(inode->i_mode)
176                         && ei->i_block_alloc_info) {
177                         rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
178                         return put_user(rsv_window_size, (int __user *)arg);
179                 }
180                 return -ENOTTY;
181         case EXT4_IOC_SETRSVSZ: {
182
183                 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
184                         return -ENOTTY;
185
186                 if (IS_RDONLY(inode) ||
187                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
188                         return -EROFS;
189
190                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
191                         return -EACCES;
192
193                 if (get_user(rsv_window_size, (int __user *)arg))
194                         return -EFAULT;
195
196                 if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
197                         rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
198
199                 /*
200                  * need to allocate reservation structure for this inode
201                  * before set the window size
202                  */
203                 mutex_lock(&ei->truncate_mutex);
204                 if (!ei->i_block_alloc_info)
205                         ext4_init_block_alloc_info(inode);
206
207                 if (ei->i_block_alloc_info){
208                         struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
209                         rsv->rsv_goal_size = rsv_window_size;
210                 }
211                 mutex_unlock(&ei->truncate_mutex);
212                 return 0;
213         }
214         case EXT4_IOC_GROUP_EXTEND: {
215                 ext4_fsblk_t n_blocks_count;
216                 struct super_block *sb = inode->i_sb;
217                 int err;
218
219                 if (!capable(CAP_SYS_RESOURCE))
220                         return -EPERM;
221
222                 if (IS_RDONLY(inode) ||
223                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
224                         return -EROFS;
225
226                 if (get_user(n_blocks_count, (__u32 __user *)arg))
227                         return -EFAULT;
228
229                 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
230                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
231                 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
232                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
233
234                 return err;
235         }
236         case EXT4_IOC_GROUP_ADD: {
237                 struct ext4_new_group_data input;
238                 struct super_block *sb = inode->i_sb;
239                 int err;
240
241                 if (!capable(CAP_SYS_RESOURCE))
242                         return -EPERM;
243
244                 if (IS_RDONLY(inode) ||
245                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
246                         return -EROFS;
247
248                 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
249                                 sizeof(input)))
250                         return -EFAULT;
251
252                 err = ext4_group_add(sb, &input);
253                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
254                 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
255                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
256
257                 return err;
258         }
259
260 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_TAGGING_NONE)
261         case EXT4_IOC_SETTAG: {
262                 handle_t *handle;
263                 struct ext4_iloc iloc;
264                 int tag;
265                 int err;
266
267                 /* fixme: if stealth, return -ENOTTY */
268                 if (!capable(CAP_CONTEXT))
269                         return -EPERM;
270                 if (IS_RDONLY(inode))
271                         return -EROFS;
272                 if (!(inode->i_sb->s_flags & MS_TAGGED))
273                         return -ENOSYS;
274                 if (get_user(tag, (int __user *) arg))
275                         return -EFAULT;
276
277                 handle = ext4_journal_start(inode, 1);
278                 if (IS_ERR(handle))
279                         return PTR_ERR(handle);
280                 err = ext4_reserve_inode_write(handle, inode, &iloc);
281                 if (err)
282                         return err;
283
284                 inode->i_tag = (tag & 0xFFFF);
285                 inode->i_ctime = CURRENT_TIME;
286
287                 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
288                 ext4_journal_stop(handle);
289                 return err;
290         }
291 #endif
292
293         default:
294                 return -ENOTTY;
295         }
296 }
297
298 #ifdef CONFIG_COMPAT
299 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
300 {
301         struct inode *inode = file->f_path.dentry->d_inode;
302         int ret;
303
304         /* These are just misnamed, they actually get/put from/to user an int */
305         switch (cmd) {
306         case EXT4_IOC32_GETFLAGS:
307                 cmd = EXT4_IOC_GETFLAGS;
308                 break;
309         case EXT4_IOC32_SETFLAGS:
310                 cmd = EXT4_IOC_SETFLAGS;
311                 break;
312         case EXT4_IOC32_GETVERSION:
313                 cmd = EXT4_IOC_GETVERSION;
314                 break;
315         case EXT4_IOC32_SETVERSION:
316                 cmd = EXT4_IOC_SETVERSION;
317                 break;
318         case EXT4_IOC32_GROUP_EXTEND:
319                 cmd = EXT4_IOC_GROUP_EXTEND;
320                 break;
321         case EXT4_IOC32_GETVERSION_OLD:
322                 cmd = EXT4_IOC_GETVERSION_OLD;
323                 break;
324         case EXT4_IOC32_SETVERSION_OLD:
325                 cmd = EXT4_IOC_SETVERSION_OLD;
326                 break;
327 #ifdef CONFIG_JBD_DEBUG
328         case EXT4_IOC32_WAIT_FOR_READONLY:
329                 cmd = EXT4_IOC_WAIT_FOR_READONLY;
330                 break;
331 #endif
332         case EXT4_IOC32_GETRSVSZ:
333                 cmd = EXT4_IOC_GETRSVSZ;
334                 break;
335         case EXT4_IOC32_SETRSVSZ:
336                 cmd = EXT4_IOC_SETRSVSZ;
337                 break;
338         case EXT4_IOC_GROUP_ADD:
339                 break;
340         default:
341                 return -ENOIOCTLCMD;
342         }
343         lock_kernel();
344         ret = ext4_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
345         unlock_kernel();
346         return ret;
347 }
348 #endif