This commit was manufactured by cvs2svn to create branch
[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                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
175                         return -EROFS;
176
177                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
178                         return -EACCES;
179
180                 if (get_user(rsv_window_size, (int __user *)arg))
181                         return -EFAULT;
182
183                 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
184                         rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
185
186                 /*
187                  * need to allocate reservation structure for this inode
188                  * before set the window size
189                  */
190                 down(&ei->truncate_sem);
191                 if (!ei->i_block_alloc_info)
192                         ext3_init_block_alloc_info(inode);
193
194                 if (ei->i_block_alloc_info){
195                         struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
196                         rsv->rsv_goal_size = rsv_window_size;
197                 }
198                 up(&ei->truncate_sem);
199                 return 0;
200         }
201         case EXT3_IOC_GROUP_EXTEND: {
202                 unsigned long n_blocks_count;
203                 struct super_block *sb = inode->i_sb;
204                 int err;
205
206                 if (!capable(CAP_SYS_RESOURCE))
207                         return -EPERM;
208
209                 if (IS_RDONLY(inode) ||
210                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
211                         return -EROFS;
212
213                 if (get_user(n_blocks_count, (__u32 __user *)arg))
214                         return -EFAULT;
215
216                 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
217                 journal_lock_updates(EXT3_SB(sb)->s_journal);
218                 journal_flush(EXT3_SB(sb)->s_journal);
219                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
220
221                 return err;
222         }
223         case EXT3_IOC_GROUP_ADD: {
224                 struct ext3_new_group_data input;
225                 struct super_block *sb = inode->i_sb;
226                 int err;
227
228                 if (!capable(CAP_SYS_RESOURCE))
229                         return -EPERM;
230
231                 if (IS_RDONLY(inode) ||
232                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
233                         return -EROFS;
234
235                 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
236                                 sizeof(input)))
237                         return -EFAULT;
238
239                 err = ext3_group_add(sb, &input);
240                 journal_lock_updates(EXT3_SB(sb)->s_journal);
241                 journal_flush(EXT3_SB(sb)->s_journal);
242                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
243
244                 return err;
245         }
246
247 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
248         case EXT3_IOC_SETXID: {
249                 handle_t *handle;
250                 struct ext3_iloc iloc;
251                 int xid;
252                 int err;
253
254                 /* fixme: if stealth, return -ENOTTY */
255                 if (!capable(CAP_CONTEXT))
256                         return -EPERM;
257                 if (IS_RDONLY(inode) ||
258                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
259                         return -EROFS;
260                 if (!(inode->i_sb->s_flags & MS_TAGXID))
261                         return -ENOSYS;
262                 if (get_user(xid, (int *) arg))
263                         return -EFAULT;
264
265                 handle = ext3_journal_start(inode, 1);
266                 if (IS_ERR(handle))
267                         return PTR_ERR(handle);
268                 err = ext3_reserve_inode_write(handle, inode, &iloc);
269                 if (err)
270                         return err;
271
272                 inode->i_xid = (xid & 0xFFFF);
273                 inode->i_ctime = CURRENT_TIME;
274
275                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
276                 ext3_journal_stop(handle);
277                 return err;
278         }
279 #endif
280
281         default:
282                 return -ENOTTY;
283         }
284 }