]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ext4/ioctl.c
xps: Transmit Packet Steering
[net-next-2.6.git] / fs / ext4 / ioctl.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/ioctl.c
ac27a0ec
DK
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>
dab291af 11#include <linux/jbd2.h>
ac27a0ec 12#include <linux/capability.h>
ac27a0ec
DK
13#include <linux/time.h>
14#include <linux/compat.h>
42a74f20 15#include <linux/mount.h>
748de673 16#include <linux/file.h>
ac27a0ec 17#include <asm/uaccess.h>
3dcf5451
CH
18#include "ext4_jbd2.h"
19#include "ext4.h"
ac27a0ec 20
5cdd7b2d 21long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ac27a0ec 22{
5cdd7b2d 23 struct inode *inode = filp->f_dentry->d_inode;
617ba13b 24 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec 25 unsigned int flags;
ac27a0ec 26
af5bc92d 27 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
ac27a0ec
DK
28
29 switch (cmd) {
617ba13b 30 case EXT4_IOC_GETFLAGS:
ff9ddf7e 31 ext4_get_inode_flags(ei);
617ba13b 32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
ac27a0ec 33 return put_user(flags, (int __user *) arg);
617ba13b 34 case EXT4_IOC_SETFLAGS: {
ac27a0ec 35 handle_t *handle = NULL;
4db46fc2 36 int err, migrate = 0;
617ba13b 37 struct ext4_iloc iloc;
ac27a0ec
DK
38 unsigned int oldflags;
39 unsigned int jflag;
40
3bd858ab 41 if (!is_owner_or_cap(inode))
ac27a0ec
DK
42 return -EACCES;
43
44 if (get_user(flags, (int __user *) arg))
45 return -EFAULT;
46
42a74f20
DH
47 err = mnt_want_write(filp->f_path.mnt);
48 if (err)
49 return err;
50
2dc6b0d4 51 flags = ext4_mask_flags(inode->i_mode, flags);
ac27a0ec 52
42a74f20 53 err = -EPERM;
ac27a0ec 54 mutex_lock(&inode->i_mutex);
e47776a0 55 /* Is it quota file? Do not allow user to mess with it */
42a74f20
DH
56 if (IS_NOQUOTA(inode))
57 goto flags_out;
58
ac27a0ec
DK
59 oldflags = ei->i_flags;
60
61 /* The JOURNAL_DATA flag is modifiable only by root */
617ba13b 62 jflag = flags & EXT4_JOURNAL_DATA_FL;
ac27a0ec
DK
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 */
617ba13b 70 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
42a74f20
DH
71 if (!capable(CAP_LINUX_IMMUTABLE))
72 goto flags_out;
ac27a0ec
DK
73 }
74
75 /*
76 * The JOURNAL_DATA flag can only be changed by
77 * the relevant capability.
78 */
617ba13b 79 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
42a74f20
DH
80 if (!capable(CAP_SYS_RESOURCE))
81 goto flags_out;
ac27a0ec 82 }
4db46fc2
AK
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 }
ac27a0ec 94
c8d46e41
JZ
95 if (flags & EXT4_EOFBLOCKS_FL) {
96 /* we don't support adding EOFBLOCKS flag */
97 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
98 err = -EOPNOTSUPP;
99 goto flags_out;
100 }
101 } else if (oldflags & EXT4_EOFBLOCKS_FL)
102 ext4_truncate(inode);
103
617ba13b 104 handle = ext4_journal_start(inode, 1);
ac27a0ec 105 if (IS_ERR(handle)) {
42a74f20
DH
106 err = PTR_ERR(handle);
107 goto flags_out;
ac27a0ec
DK
108 }
109 if (IS_SYNC(inode))
0390131b 110 ext4_handle_sync(handle);
617ba13b 111 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec
DK
112 if (err)
113 goto flags_err;
114
617ba13b
MC
115 flags = flags & EXT4_FL_USER_MODIFIABLE;
116 flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
ac27a0ec
DK
117 ei->i_flags = flags;
118
617ba13b 119 ext4_set_inode_flags(inode);
ef7f3835 120 inode->i_ctime = ext4_current_time(inode);
ac27a0ec 121
617ba13b 122 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 123flags_err:
617ba13b 124 ext4_journal_stop(handle);
42a74f20
DH
125 if (err)
126 goto flags_out;
ac27a0ec 127
617ba13b
MC
128 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
129 err = ext4_change_inode_journal_flag(inode, jflag);
4db46fc2
AK
130 if (err)
131 goto flags_out;
132 if (migrate)
133 err = ext4_ext_migrate(inode);
42a74f20 134flags_out:
ac27a0ec 135 mutex_unlock(&inode->i_mutex);
42a74f20 136 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
137 return err;
138 }
617ba13b
MC
139 case EXT4_IOC_GETVERSION:
140 case EXT4_IOC_GETVERSION_OLD:
ac27a0ec 141 return put_user(inode->i_generation, (int __user *) arg);
617ba13b
MC
142 case EXT4_IOC_SETVERSION:
143 case EXT4_IOC_SETVERSION_OLD: {
ac27a0ec 144 handle_t *handle;
617ba13b 145 struct ext4_iloc iloc;
ac27a0ec
DK
146 __u32 generation;
147 int err;
148
3bd858ab 149 if (!is_owner_or_cap(inode))
ac27a0ec 150 return -EPERM;
42a74f20
DH
151
152 err = mnt_want_write(filp->f_path.mnt);
153 if (err)
154 return err;
155 if (get_user(generation, (int __user *) arg)) {
156 err = -EFAULT;
157 goto setversion_out;
158 }
ac27a0ec 159
617ba13b 160 handle = ext4_journal_start(inode, 1);
42a74f20
DH
161 if (IS_ERR(handle)) {
162 err = PTR_ERR(handle);
163 goto setversion_out;
164 }
617ba13b 165 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec 166 if (err == 0) {
ef7f3835 167 inode->i_ctime = ext4_current_time(inode);
ac27a0ec 168 inode->i_generation = generation;
617ba13b 169 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec 170 }
617ba13b 171 ext4_journal_stop(handle);
42a74f20
DH
172setversion_out:
173 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
174 return err;
175 }
e23291b9 176#ifdef CONFIG_JBD2_DEBUG
617ba13b 177 case EXT4_IOC_WAIT_FOR_READONLY:
ac27a0ec
DK
178 /*
179 * This is racy - by the time we're woken up and running,
180 * the superblock could be released. And the module could
181 * have been unloaded. So sue me.
182 *
183 * Returns 1 if it slept, else zero.
184 */
185 {
186 struct super_block *sb = inode->i_sb;
187 DECLARE_WAITQUEUE(wait, current);
188 int ret = 0;
189
190 set_current_state(TASK_INTERRUPTIBLE);
617ba13b
MC
191 add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
192 if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
ac27a0ec
DK
193 schedule();
194 ret = 1;
195 }
617ba13b 196 remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
ac27a0ec
DK
197 return ret;
198 }
199#endif
617ba13b
MC
200 case EXT4_IOC_GROUP_EXTEND: {
201 ext4_fsblk_t n_blocks_count;
ac27a0ec 202 struct super_block *sb = inode->i_sb;
ac046f1d 203 int err, err2=0;
ac27a0ec
DK
204
205 if (!capable(CAP_SYS_RESOURCE))
206 return -EPERM;
207
ac27a0ec
DK
208 if (get_user(n_blocks_count, (__u32 __user *)arg))
209 return -EFAULT;
210
42a74f20
DH
211 err = mnt_want_write(filp->f_path.mnt);
212 if (err)
213 return err;
214
617ba13b 215 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
ac046f1d
PT
216 if (EXT4_SB(sb)->s_journal) {
217 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
218 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
219 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
220 }
7ffe1ea8
HK
221 if (err == 0)
222 err = err2;
42a74f20 223 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
224
225 return err;
226 }
748de673
AF
227
228 case EXT4_IOC_MOVE_EXT: {
229 struct move_extent me;
230 struct file *donor_filp;
231 int err;
232
4a58579b
AF
233 if (!(filp->f_mode & FMODE_READ) ||
234 !(filp->f_mode & FMODE_WRITE))
235 return -EBADF;
236
748de673
AF
237 if (copy_from_user(&me,
238 (struct move_extent __user *)arg, sizeof(me)))
239 return -EFAULT;
4a58579b 240 me.moved_len = 0;
748de673
AF
241
242 donor_filp = fget(me.donor_fd);
243 if (!donor_filp)
244 return -EBADF;
245
4a58579b
AF
246 if (!(donor_filp->f_mode & FMODE_WRITE)) {
247 err = -EBADF;
248 goto mext_out;
748de673
AF
249 }
250
4a58579b
AF
251 err = mnt_want_write(filp->f_path.mnt);
252 if (err)
253 goto mext_out;
254
748de673
AF
255 err = ext4_move_extents(filp, donor_filp, me.orig_start,
256 me.donor_start, me.len, &me.moved_len);
4a58579b
AF
257 mnt_drop_write(filp->f_path.mnt);
258 if (me.moved_len > 0)
259 file_remove_suid(donor_filp);
748de673 260
60e6679e 261 if (copy_to_user((struct move_extent __user *)arg,
c437b273 262 &me, sizeof(me)))
4a58579b
AF
263 err = -EFAULT;
264mext_out:
265 fput(donor_filp);
748de673
AF
266 return err;
267 }
268
617ba13b
MC
269 case EXT4_IOC_GROUP_ADD: {
270 struct ext4_new_group_data input;
ac27a0ec 271 struct super_block *sb = inode->i_sb;
ac046f1d 272 int err, err2=0;
ac27a0ec
DK
273
274 if (!capable(CAP_SYS_RESOURCE))
275 return -EPERM;
276
617ba13b 277 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
ac27a0ec
DK
278 sizeof(input)))
279 return -EFAULT;
280
42a74f20
DH
281 err = mnt_want_write(filp->f_path.mnt);
282 if (err)
283 return err;
284
617ba13b 285 err = ext4_group_add(sb, &input);
ac046f1d
PT
286 if (EXT4_SB(sb)->s_journal) {
287 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
288 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
289 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
290 }
7ffe1ea8
HK
291 if (err == 0)
292 err = err2;
42a74f20 293 mnt_drop_write(filp->f_path.mnt);
ac27a0ec
DK
294
295 return err;
296 }
297
c14c6fd5 298 case EXT4_IOC_MIGRATE:
2a43a878
AK
299 {
300 int err;
301 if (!is_owner_or_cap(inode))
302 return -EACCES;
303
304 err = mnt_want_write(filp->f_path.mnt);
305 if (err)
306 return err;
307 /*
308 * inode_mutex prevent write and truncate on the file.
309 * Read still goes through. We take i_data_sem in
310 * ext4_ext_swap_inode_data before we switch the
311 * inode format to prevent read.
312 */
313 mutex_lock(&(inode->i_mutex));
314 err = ext4_ext_migrate(inode);
315 mutex_unlock(&(inode->i_mutex));
316 mnt_drop_write(filp->f_path.mnt);
317 return err;
318 }
c14c6fd5 319
ccd2506b
TT
320 case EXT4_IOC_ALLOC_DA_BLKS:
321 {
322 int err;
323 if (!is_owner_or_cap(inode))
324 return -EACCES;
325
326 err = mnt_want_write(filp->f_path.mnt);
327 if (err)
328 return err;
329 err = ext4_alloc_da_blocks(inode);
330 mnt_drop_write(filp->f_path.mnt);
331 return err;
332 }
333
ac27a0ec
DK
334 default:
335 return -ENOTTY;
336 }
337}
338
339#ifdef CONFIG_COMPAT
617ba13b 340long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ac27a0ec 341{
ac27a0ec
DK
342 /* These are just misnamed, they actually get/put from/to user an int */
343 switch (cmd) {
617ba13b
MC
344 case EXT4_IOC32_GETFLAGS:
345 cmd = EXT4_IOC_GETFLAGS;
ac27a0ec 346 break;
617ba13b
MC
347 case EXT4_IOC32_SETFLAGS:
348 cmd = EXT4_IOC_SETFLAGS;
ac27a0ec 349 break;
617ba13b
MC
350 case EXT4_IOC32_GETVERSION:
351 cmd = EXT4_IOC_GETVERSION;
ac27a0ec 352 break;
617ba13b
MC
353 case EXT4_IOC32_SETVERSION:
354 cmd = EXT4_IOC_SETVERSION;
ac27a0ec 355 break;
617ba13b
MC
356 case EXT4_IOC32_GROUP_EXTEND:
357 cmd = EXT4_IOC_GROUP_EXTEND;
ac27a0ec 358 break;
617ba13b
MC
359 case EXT4_IOC32_GETVERSION_OLD:
360 cmd = EXT4_IOC_GETVERSION_OLD;
ac27a0ec 361 break;
617ba13b
MC
362 case EXT4_IOC32_SETVERSION_OLD:
363 cmd = EXT4_IOC_SETVERSION_OLD;
ac27a0ec 364 break;
e23291b9 365#ifdef CONFIG_JBD2_DEBUG
617ba13b
MC
366 case EXT4_IOC32_WAIT_FOR_READONLY:
367 cmd = EXT4_IOC_WAIT_FOR_READONLY;
ac27a0ec
DK
368 break;
369#endif
617ba13b
MC
370 case EXT4_IOC32_GETRSVSZ:
371 cmd = EXT4_IOC_GETRSVSZ;
ac27a0ec 372 break;
617ba13b
MC
373 case EXT4_IOC32_SETRSVSZ:
374 cmd = EXT4_IOC_SETRSVSZ;
ac27a0ec 375 break;
4d92dc0f
BH
376 case EXT4_IOC32_GROUP_ADD: {
377 struct compat_ext4_new_group_input __user *uinput;
378 struct ext4_new_group_input input;
379 mm_segment_t old_fs;
380 int err;
381
382 uinput = compat_ptr(arg);
383 err = get_user(input.group, &uinput->group);
384 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
385 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
386 err |= get_user(input.inode_table, &uinput->inode_table);
387 err |= get_user(input.blocks_count, &uinput->blocks_count);
388 err |= get_user(input.reserved_blocks,
389 &uinput->reserved_blocks);
390 if (err)
391 return -EFAULT;
392 old_fs = get_fs();
393 set_fs(KERNEL_DS);
394 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
395 (unsigned long) &input);
396 set_fs(old_fs);
397 return err;
398 }
b684b2ee
CB
399 case EXT4_IOC_MOVE_EXT:
400 break;
ac27a0ec
DK
401 default:
402 return -ENOIOCTLCMD;
403 }
5cdd7b2d 404 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
ac27a0ec
DK
405}
406#endif