headers: smp_lock.h redux
[linux-2.6] / 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/jbd2.h>
12 #include <linux/capability.h>
13 #include <linux/time.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/file.h>
17 #include <asm/uaccess.h>
18 #include "ext4_jbd2.h"
19 #include "ext4.h"
20
21 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
22 {
23         struct inode *inode = filp->f_dentry->d_inode;
24         struct ext4_inode_info *ei = EXT4_I(inode);
25         unsigned int flags;
26
27         ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
28
29         switch (cmd) {
30         case EXT4_IOC_GETFLAGS:
31                 ext4_get_inode_flags(ei);
32                 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
33                 return put_user(flags, (int __user *) arg);
34         case EXT4_IOC_SETFLAGS: {
35                 handle_t *handle = NULL;
36                 int err, migrate = 0;
37                 struct ext4_iloc iloc;
38                 unsigned int oldflags;
39                 unsigned int jflag;
40
41                 if (!is_owner_or_cap(inode))
42                         return -EACCES;
43
44                 if (get_user(flags, (int __user *) arg))
45                         return -EFAULT;
46
47                 err = mnt_want_write(filp->f_path.mnt);
48                 if (err)
49                         return err;
50
51                 flags = ext4_mask_flags(inode->i_mode, flags);
52
53                 err = -EPERM;
54                 mutex_lock(&inode->i_mutex);
55                 /* Is it quota file? Do not allow user to mess with it */
56                 if (IS_NOQUOTA(inode))
57                         goto flags_out;
58
59                 oldflags = ei->i_flags;
60
61                 /* The JOURNAL_DATA flag is modifiable only by root */
62                 jflag = flags & EXT4_JOURNAL_DATA_FL;
63
64                 /*
65                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
66                  * the relevant capability.
67                  *
68                  * This test looks nicer. Thanks to Pauline Middelink
69                  */
70                 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
71                         if (!capable(CAP_LINUX_IMMUTABLE))
72                                 goto flags_out;
73                 }
74
75                 /*
76                  * The JOURNAL_DATA flag can only be changed by
77                  * the relevant capability.
78                  */
79                 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
80                         if (!capable(CAP_SYS_RESOURCE))
81                                 goto flags_out;
82                 }
83                 if (oldflags & EXT4_EXTENTS_FL) {
84                         /* We don't support clearning extent flags */
85                         if (!(flags & EXT4_EXTENTS_FL)) {
86                                 err = -EOPNOTSUPP;
87                                 goto flags_out;
88                         }
89                 } else if (flags & EXT4_EXTENTS_FL) {
90                         /* migrate the file */
91                         migrate = 1;
92                         flags &= ~EXT4_EXTENTS_FL;
93                 }
94
95                 handle = ext4_journal_start(inode, 1);
96                 if (IS_ERR(handle)) {
97                         err = PTR_ERR(handle);
98                         goto flags_out;
99                 }
100                 if (IS_SYNC(inode))
101                         ext4_handle_sync(handle);
102                 err = ext4_reserve_inode_write(handle, inode, &iloc);
103                 if (err)
104                         goto flags_err;
105
106                 flags = flags & EXT4_FL_USER_MODIFIABLE;
107                 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
108                 ei->i_flags = flags;
109
110                 ext4_set_inode_flags(inode);
111                 inode->i_ctime = ext4_current_time(inode);
112
113                 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
114 flags_err:
115                 ext4_journal_stop(handle);
116                 if (err)
117                         goto flags_out;
118
119                 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
120                         err = ext4_change_inode_journal_flag(inode, jflag);
121                 if (err)
122                         goto flags_out;
123                 if (migrate)
124                         err = ext4_ext_migrate(inode);
125 flags_out:
126                 mutex_unlock(&inode->i_mutex);
127                 mnt_drop_write(filp->f_path.mnt);
128                 return err;
129         }
130         case EXT4_IOC_GETVERSION:
131         case EXT4_IOC_GETVERSION_OLD:
132                 return put_user(inode->i_generation, (int __user *) arg);
133         case EXT4_IOC_SETVERSION:
134         case EXT4_IOC_SETVERSION_OLD: {
135                 handle_t *handle;
136                 struct ext4_iloc iloc;
137                 __u32 generation;
138                 int err;
139
140                 if (!is_owner_or_cap(inode))
141                         return -EPERM;
142
143                 err = mnt_want_write(filp->f_path.mnt);
144                 if (err)
145                         return err;
146                 if (get_user(generation, (int __user *) arg)) {
147                         err = -EFAULT;
148                         goto setversion_out;
149                 }
150
151                 handle = ext4_journal_start(inode, 1);
152                 if (IS_ERR(handle)) {
153                         err = PTR_ERR(handle);
154                         goto setversion_out;
155                 }
156                 err = ext4_reserve_inode_write(handle, inode, &iloc);
157                 if (err == 0) {
158                         inode->i_ctime = ext4_current_time(inode);
159                         inode->i_generation = generation;
160                         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
161                 }
162                 ext4_journal_stop(handle);
163 setversion_out:
164                 mnt_drop_write(filp->f_path.mnt);
165                 return err;
166         }
167 #ifdef CONFIG_JBD2_DEBUG
168         case EXT4_IOC_WAIT_FOR_READONLY:
169                 /*
170                  * This is racy - by the time we're woken up and running,
171                  * the superblock could be released.  And the module could
172                  * have been unloaded.  So sue me.
173                  *
174                  * Returns 1 if it slept, else zero.
175                  */
176                 {
177                         struct super_block *sb = inode->i_sb;
178                         DECLARE_WAITQUEUE(wait, current);
179                         int ret = 0;
180
181                         set_current_state(TASK_INTERRUPTIBLE);
182                         add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
183                         if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
184                                 schedule();
185                                 ret = 1;
186                         }
187                         remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
188                         return ret;
189                 }
190 #endif
191         case EXT4_IOC_GROUP_EXTEND: {
192                 ext4_fsblk_t n_blocks_count;
193                 struct super_block *sb = inode->i_sb;
194                 int err, err2;
195
196                 if (!capable(CAP_SYS_RESOURCE))
197                         return -EPERM;
198
199                 if (get_user(n_blocks_count, (__u32 __user *)arg))
200                         return -EFAULT;
201
202                 err = mnt_want_write(filp->f_path.mnt);
203                 if (err)
204                         return err;
205
206                 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
207                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
208                 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
209                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
210                 if (err == 0)
211                         err = err2;
212                 mnt_drop_write(filp->f_path.mnt);
213
214                 return err;
215         }
216
217         case EXT4_IOC_MOVE_EXT: {
218                 struct move_extent me;
219                 struct file *donor_filp;
220                 int err;
221
222                 if (copy_from_user(&me,
223                         (struct move_extent __user *)arg, sizeof(me)))
224                         return -EFAULT;
225
226                 donor_filp = fget(me.donor_fd);
227                 if (!donor_filp)
228                         return -EBADF;
229
230                 if (!capable(CAP_DAC_OVERRIDE)) {
231                         if ((current->real_cred->fsuid != inode->i_uid) ||
232                                 !(inode->i_mode & S_IRUSR) ||
233                                 !(donor_filp->f_dentry->d_inode->i_mode &
234                                 S_IRUSR)) {
235                                 fput(donor_filp);
236                                 return -EACCES;
237                         }
238                 }
239
240                 err = ext4_move_extents(filp, donor_filp, me.orig_start,
241                                         me.donor_start, me.len, &me.moved_len);
242                 fput(donor_filp);
243
244                 if (!err)
245                         if (copy_to_user((struct move_extent *)arg,
246                                 &me, sizeof(me)))
247                                 return -EFAULT;
248                 return err;
249         }
250
251         case EXT4_IOC_GROUP_ADD: {
252                 struct ext4_new_group_data input;
253                 struct super_block *sb = inode->i_sb;
254                 int err, err2;
255
256                 if (!capable(CAP_SYS_RESOURCE))
257                         return -EPERM;
258
259                 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
260                                 sizeof(input)))
261                         return -EFAULT;
262
263                 err = mnt_want_write(filp->f_path.mnt);
264                 if (err)
265                         return err;
266
267                 err = ext4_group_add(sb, &input);
268                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
269                 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
270                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
271                 if (err == 0)
272                         err = err2;
273                 mnt_drop_write(filp->f_path.mnt);
274
275                 return err;
276         }
277
278         case EXT4_IOC_MIGRATE:
279         {
280                 int err;
281                 if (!is_owner_or_cap(inode))
282                         return -EACCES;
283
284                 err = mnt_want_write(filp->f_path.mnt);
285                 if (err)
286                         return err;
287                 /*
288                  * inode_mutex prevent write and truncate on the file.
289                  * Read still goes through. We take i_data_sem in
290                  * ext4_ext_swap_inode_data before we switch the
291                  * inode format to prevent read.
292                  */
293                 mutex_lock(&(inode->i_mutex));
294                 err = ext4_ext_migrate(inode);
295                 mutex_unlock(&(inode->i_mutex));
296                 mnt_drop_write(filp->f_path.mnt);
297                 return err;
298         }
299
300         case EXT4_IOC_ALLOC_DA_BLKS:
301         {
302                 int err;
303                 if (!is_owner_or_cap(inode))
304                         return -EACCES;
305
306                 err = mnt_want_write(filp->f_path.mnt);
307                 if (err)
308                         return err;
309                 err = ext4_alloc_da_blocks(inode);
310                 mnt_drop_write(filp->f_path.mnt);
311                 return err;
312         }
313
314         default:
315                 return -ENOTTY;
316         }
317 }
318
319 #ifdef CONFIG_COMPAT
320 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
321 {
322         /* These are just misnamed, they actually get/put from/to user an int */
323         switch (cmd) {
324         case EXT4_IOC32_GETFLAGS:
325                 cmd = EXT4_IOC_GETFLAGS;
326                 break;
327         case EXT4_IOC32_SETFLAGS:
328                 cmd = EXT4_IOC_SETFLAGS;
329                 break;
330         case EXT4_IOC32_GETVERSION:
331                 cmd = EXT4_IOC_GETVERSION;
332                 break;
333         case EXT4_IOC32_SETVERSION:
334                 cmd = EXT4_IOC_SETVERSION;
335                 break;
336         case EXT4_IOC32_GROUP_EXTEND:
337                 cmd = EXT4_IOC_GROUP_EXTEND;
338                 break;
339         case EXT4_IOC32_GETVERSION_OLD:
340                 cmd = EXT4_IOC_GETVERSION_OLD;
341                 break;
342         case EXT4_IOC32_SETVERSION_OLD:
343                 cmd = EXT4_IOC_SETVERSION_OLD;
344                 break;
345 #ifdef CONFIG_JBD2_DEBUG
346         case EXT4_IOC32_WAIT_FOR_READONLY:
347                 cmd = EXT4_IOC_WAIT_FOR_READONLY;
348                 break;
349 #endif
350         case EXT4_IOC32_GETRSVSZ:
351                 cmd = EXT4_IOC_GETRSVSZ;
352                 break;
353         case EXT4_IOC32_SETRSVSZ:
354                 cmd = EXT4_IOC_SETRSVSZ;
355                 break;
356         case EXT4_IOC_GROUP_ADD:
357                 break;
358         default:
359                 return -ENOIOCTLCMD;
360         }
361         return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
362 }
363 #endif