Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / 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/capability.h>
13 #include <linux/ext3_fs.h>
14 #include <linux/ext3_jbd.h>
15 #include <linux/time.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                         return -EROFS;
41
42                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
43                         return -EACCES;
44
45                 if (get_user(flags, (int __user *) arg))
46                         return -EFAULT;
47
48                 if (!S_ISDIR(inode->i_mode))
49                         flags &= ~EXT3_DIRSYNC_FL;
50
51                 oldflags = ei->i_flags;
52
53                 /* The JOURNAL_DATA flag is modifiable only by root */
54                 jflag = flags & EXT3_JOURNAL_DATA_FL;
55
56                 /*
57                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
58                  * the relevant capability.
59                  *
60                  * This test looks nicer. Thanks to Pauline Middelink
61                  */
62                 if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
63                         if (!capable(CAP_LINUX_IMMUTABLE))
64                                 return -EPERM;
65                 }
66
67                 /*
68                  * The JOURNAL_DATA flag can only be changed by
69                  * the relevant capability.
70                  */
71                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
72                         if (!capable(CAP_SYS_RESOURCE))
73                                 return -EPERM;
74                 }
75
76
77                 handle = ext3_journal_start(inode, 1);
78                 if (IS_ERR(handle))
79                         return PTR_ERR(handle);
80                 if (IS_SYNC(inode))
81                         handle->h_sync = 1;
82                 err = ext3_reserve_inode_write(handle, inode, &iloc);
83                 if (err)
84                         goto flags_err;
85
86                 flags = flags & EXT3_FL_USER_MODIFIABLE;
87                 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
88                 ei->i_flags = flags;
89
90                 ext3_set_inode_flags(inode);
91                 inode->i_ctime = CURRENT_TIME_SEC;
92
93                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
94 flags_err:
95                 ext3_journal_stop(handle);
96                 if (err)
97                         return err;
98
99                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
100                         err = ext3_change_inode_journal_flag(inode, jflag);
101                 return err;
102         }
103         case EXT3_IOC_GETVERSION:
104         case EXT3_IOC_GETVERSION_OLD:
105                 return put_user(inode->i_generation, (int __user *) arg);
106         case EXT3_IOC_SETVERSION:
107         case EXT3_IOC_SETVERSION_OLD: {
108                 handle_t *handle;
109                 struct ext3_iloc iloc;
110                 __u32 generation;
111                 int err;
112
113                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
114                         return -EPERM;
115                 if (IS_RDONLY(inode))
116                         return -EROFS;
117                 if (get_user(generation, (int __user *) arg))
118                         return -EFAULT;
119
120                 handle = ext3_journal_start(inode, 1);
121                 if (IS_ERR(handle))
122                         return PTR_ERR(handle);
123                 err = ext3_reserve_inode_write(handle, inode, &iloc);
124                 if (err == 0) {
125                         inode->i_ctime = CURRENT_TIME_SEC;
126                         inode->i_generation = generation;
127                         err = ext3_mark_iloc_dirty(handle, inode, &iloc);
128                 }
129                 ext3_journal_stop(handle);
130                 return err;
131         }
132 #ifdef CONFIG_JBD_DEBUG
133         case EXT3_IOC_WAIT_FOR_READONLY:
134                 /*
135                  * This is racy - by the time we're woken up and running,
136                  * the superblock could be released.  And the module could
137                  * have been unloaded.  So sue me.
138                  *
139                  * Returns 1 if it slept, else zero.
140                  */
141                 {
142                         struct super_block *sb = inode->i_sb;
143                         DECLARE_WAITQUEUE(wait, current);
144                         int ret = 0;
145
146                         set_current_state(TASK_INTERRUPTIBLE);
147                         add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
148                         if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
149                                 schedule();
150                                 ret = 1;
151                         }
152                         remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
153                         return ret;
154                 }
155 #endif
156         case EXT3_IOC_GETRSVSZ:
157                 if (test_opt(inode->i_sb, RESERVATION)
158                         && S_ISREG(inode->i_mode)
159                         && ei->i_block_alloc_info) {
160                         rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
161                         return put_user(rsv_window_size, (int __user *)arg);
162                 }
163                 return -ENOTTY;
164         case EXT3_IOC_SETRSVSZ: {
165
166                 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
167                         return -ENOTTY;
168
169                 if (IS_RDONLY(inode))
170                         return -EROFS;
171
172                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
173                         return -EACCES;
174
175                 if (get_user(rsv_window_size, (int __user *)arg))
176                         return -EFAULT;
177
178                 if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
179                         rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
180
181                 /*
182                  * need to allocate reservation structure for this inode
183                  * before set the window size
184                  */
185                 mutex_lock(&ei->truncate_mutex);
186                 if (!ei->i_block_alloc_info)
187                         ext3_init_block_alloc_info(inode);
188
189                 if (ei->i_block_alloc_info){
190                         struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
191                         rsv->rsv_goal_size = rsv_window_size;
192                 }
193                 mutex_unlock(&ei->truncate_mutex);
194                 return 0;
195         }
196         case EXT3_IOC_GROUP_EXTEND: {
197                 unsigned long n_blocks_count;
198                 struct super_block *sb = inode->i_sb;
199                 int err;
200
201                 if (!capable(CAP_SYS_RESOURCE))
202                         return -EPERM;
203
204                 if (IS_RDONLY(inode))
205                         return -EROFS;
206
207                 if (get_user(n_blocks_count, (__u32 __user *)arg))
208                         return -EFAULT;
209
210                 err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
211                 journal_lock_updates(EXT3_SB(sb)->s_journal);
212                 journal_flush(EXT3_SB(sb)->s_journal);
213                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
214
215                 return err;
216         }
217         case EXT3_IOC_GROUP_ADD: {
218                 struct ext3_new_group_data input;
219                 struct super_block *sb = inode->i_sb;
220                 int err;
221
222                 if (!capable(CAP_SYS_RESOURCE))
223                         return -EPERM;
224
225                 if (IS_RDONLY(inode))
226                         return -EROFS;
227
228                 if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
229                                 sizeof(input)))
230                         return -EFAULT;
231
232                 err = ext3_group_add(sb, &input);
233                 journal_lock_updates(EXT3_SB(sb)->s_journal);
234                 journal_flush(EXT3_SB(sb)->s_journal);
235                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
236
237                 return err;
238         }
239
240
241         default:
242                 return -ENOTTY;
243         }
244 }