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