dda5c8c5b0053a2ae65d740f42bbca810c1e0631
[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                 oldflags = ei->i_flags;
55
56                 /* The JOURNAL_DATA flag is modifiable only by root */
57                 jflag = flags & EXT3_JOURNAL_DATA_FL;
58
59                 /*
60                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
61                  * the relevant capability.
62                  *
63                  * This test looks nicer. Thanks to Pauline Middelink
64                  */
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))
69                                 return -EPERM;
70                 }
71
72                 /*
73                  * The JOURNAL_DATA flag can only be changed by
74                  * the relevant capability.
75                  */
76                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
77                         if (!capable(CAP_SYS_RESOURCE))
78                                 return -EPERM;
79                 }
80
81
82                 handle = ext3_journal_start(inode, 1);
83                 if (IS_ERR(handle))
84                         return PTR_ERR(handle);
85                 if (IS_SYNC(inode))
86                         handle->h_sync = 1;
87                 err = ext3_reserve_inode_write(handle, inode, &iloc);
88                 if (err)
89                         goto flags_err;
90
91                 flags = flags & EXT3_FL_USER_MODIFIABLE;
92                 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
93                 ei->i_flags = flags;
94
95                 ext3_set_inode_flags(inode);
96                 inode->i_ctime = CURRENT_TIME_SEC;
97
98                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
99 flags_err:
100                 ext3_journal_stop(handle);
101                 if (err)
102                         return err;
103
104                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
105                         err = ext3_change_inode_journal_flag(inode, jflag);
106                 return err;
107         }
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: {
113                 handle_t *handle;
114                 struct ext3_iloc iloc;
115                 __u32 generation;
116                 int err;
117
118                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
119                         return -EPERM;
120                 if (IS_RDONLY(inode) ||
121                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
122                         return -EROFS;
123                 if (get_user(generation, (int __user *) arg))
124                         return -EFAULT;
125
126                 handle = ext3_journal_start(inode, 1);
127                 if (IS_ERR(handle))
128                         return PTR_ERR(handle);
129                 err = ext3_reserve_inode_write(handle, inode, &iloc);
130                 if (err == 0) {
131                         inode->i_ctime = CURRENT_TIME_SEC;
132                         inode->i_generation = generation;
133                         err = ext3_mark_iloc_dirty(handle, inode, &iloc);
134                 }
135                 ext3_journal_stop(handle);
136                 return err;
137         }
138 #ifdef CONFIG_JBD_DEBUG
139         case EXT3_IOC_WAIT_FOR_READONLY:
140                 /*
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.
144                  *
145                  * Returns 1 if it slept, else zero.
146                  */
147                 {
148                         struct super_block *sb = inode->i_sb;
149                         DECLARE_WAITQUEUE(wait, current);
150                         int ret = 0;
151
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)) {
155                                 schedule();
156                                 ret = 1;
157                         }
158                         remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
159                         return ret;
160                 }
161 #endif
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);
168                 }
169                 return -ENOTTY;
170         case EXT3_IOC_SETRSVSZ: {
171
172                 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
173                         return -ENOTTY;
174
175                 if (IS_RDONLY(inode) ||
176                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
177                         return -EROFS;
178
179                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
180                         return -EACCES;
181
182                 if (get_user(rsv_window_size, (int __user *)arg))
183                         return -EFAULT;
184
185                 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
186                         rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
187
188                 /*
189                  * need to allocate reservation structure for this inode
190                  * before set the window size
191                  */
192                 down(&ei->truncate_sem);
193                 if (!ei->i_block_alloc_info)
194                         ext3_init_block_alloc_info(inode);
195
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;
199                 }
200                 up(&ei->truncate_sem);
201                 return 0;
202         }
203         case EXT3_IOC_GROUP_EXTEND: {
204                 unsigned long n_blocks_count;
205                 struct super_block *sb = inode->i_sb;
206                 int err;
207
208                 if (!capable(CAP_SYS_RESOURCE))
209                         return -EPERM;
210
211                 if (IS_RDONLY(inode) ||
212                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
213                         return -EROFS;
214
215                 if (get_user(n_blocks_count, (__u32 __user *)arg))
216                         return -EFAULT;
217
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);
222
223                 return err;
224         }
225         case EXT3_IOC_GROUP_ADD: {
226                 struct ext3_new_group_data input;
227                 struct super_block *sb = inode->i_sb;
228                 int err;
229
230                 if (!capable(CAP_SYS_RESOURCE))
231                         return -EPERM;
232
233                 if (IS_RDONLY(inode) ||
234                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
235                         return -EROFS;
236
237                 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
238                                 sizeof(input)))
239                         return -EFAULT;
240
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);
245
246                 return err;
247         }
248
249 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
250         case EXT3_IOC_SETXID: {
251                 handle_t *handle;
252                 struct ext3_iloc iloc;
253                 int xid;
254                 int err;
255
256                 /* fixme: if stealth, return -ENOTTY */
257                 if (!capable(CAP_CONTEXT))
258                         return -EPERM;
259                 if (IS_RDONLY(inode))
260                         return -EROFS;
261                 if (!(inode->i_sb->s_flags & MS_TAGXID))
262                         return -ENOSYS;
263                 if (get_user(xid, (int __user *) arg))
264                         return -EFAULT;
265
266                 handle = ext3_journal_start(inode, 1);
267                 if (IS_ERR(handle))
268                         return PTR_ERR(handle);
269                 err = ext3_reserve_inode_write(handle, inode, &iloc);
270                 if (err)
271                         return err;
272
273                 inode->i_xid = (xid & 0xFFFF);
274                 inode->i_ctime = CURRENT_TIME;
275
276                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
277                 ext3_journal_stop(handle);
278                 return err;
279         }
280 #endif
281
282         default:
283                 return -ENOTTY;
284         }
285 }