hostfs: hostfs_file_open: Fix a fd leak in hostfs_file_open
[linux/fpc-iii.git] / fs / ext4 / ioctl.c
blobf58a0d106726100262aad4557fc91dd46f76ddf4
1 /*
2 * linux/fs/ext4/ioctl.c
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 */
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"
21 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
23 /**
24 * Swap memory between @a and @b for @len bytes.
26 * @a: pointer to first memory area
27 * @b: pointer to second memory area
28 * @len: number of bytes to swap
31 static void memswap(void *a, void *b, size_t len)
33 unsigned char *ap, *bp;
34 unsigned char tmp;
36 ap = (unsigned char *)a;
37 bp = (unsigned char *)b;
38 while (len-- > 0) {
39 tmp = *ap;
40 *ap = *bp;
41 *bp = tmp;
42 ap++;
43 bp++;
47 /**
48 * Swap i_data and associated attributes between @inode1 and @inode2.
49 * This function is used for the primary swap between inode1 and inode2
50 * and also to revert this primary swap in case of errors.
52 * Therefore you have to make sure, that calling this method twice
53 * will revert all changes.
55 * @inode1: pointer to first inode
56 * @inode2: pointer to second inode
58 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
60 loff_t isize;
61 struct ext4_inode_info *ei1;
62 struct ext4_inode_info *ei2;
64 ei1 = EXT4_I(inode1);
65 ei2 = EXT4_I(inode2);
67 memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
68 memswap(&inode1->i_version, &inode2->i_version,
69 sizeof(inode1->i_version));
70 memswap(&inode1->i_blocks, &inode2->i_blocks,
71 sizeof(inode1->i_blocks));
72 memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
73 memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
74 memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
77 memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
78 memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
79 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
80 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
82 isize = i_size_read(inode1);
83 i_size_write(inode1, i_size_read(inode2));
84 i_size_write(inode2, isize);
87 /**
88 * Swap the information from the given @inode and the inode
89 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
90 * important fields of the inodes.
92 * @sb: the super block of the filesystem
93 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
96 static long swap_inode_boot_loader(struct super_block *sb,
97 struct inode *inode)
99 handle_t *handle;
100 int err;
101 struct inode *inode_bl;
102 struct ext4_inode_info *ei_bl;
103 struct ext4_sb_info *sbi = EXT4_SB(sb);
105 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
106 return -EINVAL;
108 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
109 return -EPERM;
111 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
112 if (IS_ERR(inode_bl))
113 return PTR_ERR(inode_bl);
114 ei_bl = EXT4_I(inode_bl);
116 filemap_flush(inode->i_mapping);
117 filemap_flush(inode_bl->i_mapping);
119 /* Protect orig inodes against a truncate and make sure,
120 * that only 1 swap_inode_boot_loader is running. */
121 lock_two_nondirectories(inode, inode_bl);
123 truncate_inode_pages(&inode->i_data, 0);
124 truncate_inode_pages(&inode_bl->i_data, 0);
126 /* Wait for all existing dio workers */
127 ext4_inode_block_unlocked_dio(inode);
128 ext4_inode_block_unlocked_dio(inode_bl);
129 inode_dio_wait(inode);
130 inode_dio_wait(inode_bl);
132 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
133 if (IS_ERR(handle)) {
134 err = -EINVAL;
135 goto journal_err_out;
138 /* Protect extent tree against block allocations via delalloc */
139 ext4_double_down_write_data_sem(inode, inode_bl);
141 if (inode_bl->i_nlink == 0) {
142 /* this inode has never been used as a BOOT_LOADER */
143 set_nlink(inode_bl, 1);
144 i_uid_write(inode_bl, 0);
145 i_gid_write(inode_bl, 0);
146 inode_bl->i_flags = 0;
147 ei_bl->i_flags = 0;
148 inode_bl->i_version = 1;
149 i_size_write(inode_bl, 0);
150 inode_bl->i_mode = S_IFREG;
151 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
152 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
153 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
154 ext4_ext_tree_init(handle, inode_bl);
155 } else
156 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
159 swap_inode_data(inode, inode_bl);
161 inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
163 spin_lock(&sbi->s_next_gen_lock);
164 inode->i_generation = sbi->s_next_generation++;
165 inode_bl->i_generation = sbi->s_next_generation++;
166 spin_unlock(&sbi->s_next_gen_lock);
168 ext4_discard_preallocations(inode);
170 err = ext4_mark_inode_dirty(handle, inode);
171 if (err < 0) {
172 ext4_warning(inode->i_sb,
173 "couldn't mark inode #%lu dirty (err %d)",
174 inode->i_ino, err);
175 /* Revert all changes: */
176 swap_inode_data(inode, inode_bl);
177 } else {
178 err = ext4_mark_inode_dirty(handle, inode_bl);
179 if (err < 0) {
180 ext4_warning(inode_bl->i_sb,
181 "couldn't mark inode #%lu dirty (err %d)",
182 inode_bl->i_ino, err);
183 /* Revert all changes: */
184 swap_inode_data(inode, inode_bl);
185 ext4_mark_inode_dirty(handle, inode);
188 ext4_journal_stop(handle);
189 ext4_double_up_write_data_sem(inode, inode_bl);
191 journal_err_out:
192 ext4_inode_resume_unlocked_dio(inode);
193 ext4_inode_resume_unlocked_dio(inode_bl);
194 unlock_two_nondirectories(inode, inode_bl);
195 iput(inode_bl);
196 return err;
199 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
201 struct inode *inode = file_inode(filp);
202 struct super_block *sb = inode->i_sb;
203 struct ext4_inode_info *ei = EXT4_I(inode);
204 unsigned int flags;
206 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
208 switch (cmd) {
209 case EXT4_IOC_GETFLAGS:
210 ext4_get_inode_flags(ei);
211 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
212 return put_user(flags, (int __user *) arg);
213 case EXT4_IOC_SETFLAGS: {
214 handle_t *handle = NULL;
215 int err, migrate = 0;
216 struct ext4_iloc iloc;
217 unsigned int oldflags, mask, i;
218 unsigned int jflag;
220 if (!inode_owner_or_capable(inode))
221 return -EACCES;
223 if (get_user(flags, (int __user *) arg))
224 return -EFAULT;
226 err = mnt_want_write_file(filp);
227 if (err)
228 return err;
230 flags = ext4_mask_flags(inode->i_mode, flags);
232 err = -EPERM;
233 mutex_lock(&inode->i_mutex);
234 /* Is it quota file? Do not allow user to mess with it */
235 if (IS_NOQUOTA(inode))
236 goto flags_out;
238 oldflags = ei->i_flags;
240 /* The JOURNAL_DATA flag is modifiable only by root */
241 jflag = flags & EXT4_JOURNAL_DATA_FL;
244 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
245 * the relevant capability.
247 * This test looks nicer. Thanks to Pauline Middelink
249 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
250 if (!capable(CAP_LINUX_IMMUTABLE))
251 goto flags_out;
255 * The JOURNAL_DATA flag can only be changed by
256 * the relevant capability.
258 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
259 if (!capable(CAP_SYS_RESOURCE))
260 goto flags_out;
262 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
263 migrate = 1;
265 if (flags & EXT4_EOFBLOCKS_FL) {
266 /* we don't support adding EOFBLOCKS flag */
267 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
268 err = -EOPNOTSUPP;
269 goto flags_out;
271 } else if (oldflags & EXT4_EOFBLOCKS_FL)
272 ext4_truncate(inode);
274 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
275 if (IS_ERR(handle)) {
276 err = PTR_ERR(handle);
277 goto flags_out;
279 if (IS_SYNC(inode))
280 ext4_handle_sync(handle);
281 err = ext4_reserve_inode_write(handle, inode, &iloc);
282 if (err)
283 goto flags_err;
285 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
286 if (!(mask & EXT4_FL_USER_MODIFIABLE))
287 continue;
288 if (mask & flags)
289 ext4_set_inode_flag(inode, i);
290 else
291 ext4_clear_inode_flag(inode, i);
294 ext4_set_inode_flags(inode);
295 inode->i_ctime = ext4_current_time(inode);
297 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
298 flags_err:
299 ext4_journal_stop(handle);
300 if (err)
301 goto flags_out;
303 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
304 err = ext4_change_inode_journal_flag(inode, jflag);
305 if (err)
306 goto flags_out;
307 if (migrate) {
308 if (flags & EXT4_EXTENTS_FL)
309 err = ext4_ext_migrate(inode);
310 else
311 err = ext4_ind_migrate(inode);
314 flags_out:
315 mutex_unlock(&inode->i_mutex);
316 mnt_drop_write_file(filp);
317 return err;
319 case EXT4_IOC_GETVERSION:
320 case EXT4_IOC_GETVERSION_OLD:
321 return put_user(inode->i_generation, (int __user *) arg);
322 case EXT4_IOC_SETVERSION:
323 case EXT4_IOC_SETVERSION_OLD: {
324 handle_t *handle;
325 struct ext4_iloc iloc;
326 __u32 generation;
327 int err;
329 if (!inode_owner_or_capable(inode))
330 return -EPERM;
332 if (ext4_has_metadata_csum(inode->i_sb)) {
333 ext4_warning(sb, "Setting inode version is not "
334 "supported with metadata_csum enabled.");
335 return -ENOTTY;
338 err = mnt_want_write_file(filp);
339 if (err)
340 return err;
341 if (get_user(generation, (int __user *) arg)) {
342 err = -EFAULT;
343 goto setversion_out;
346 mutex_lock(&inode->i_mutex);
347 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
348 if (IS_ERR(handle)) {
349 err = PTR_ERR(handle);
350 goto unlock_out;
352 err = ext4_reserve_inode_write(handle, inode, &iloc);
353 if (err == 0) {
354 inode->i_ctime = ext4_current_time(inode);
355 inode->i_generation = generation;
356 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
358 ext4_journal_stop(handle);
360 unlock_out:
361 mutex_unlock(&inode->i_mutex);
362 setversion_out:
363 mnt_drop_write_file(filp);
364 return err;
366 case EXT4_IOC_GROUP_EXTEND: {
367 ext4_fsblk_t n_blocks_count;
368 int err, err2=0;
370 err = ext4_resize_begin(sb);
371 if (err)
372 return err;
374 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
375 err = -EFAULT;
376 goto group_extend_out;
379 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
380 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
381 ext4_msg(sb, KERN_ERR,
382 "Online resizing not supported with bigalloc");
383 err = -EOPNOTSUPP;
384 goto group_extend_out;
387 err = mnt_want_write_file(filp);
388 if (err)
389 goto group_extend_out;
391 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
392 if (EXT4_SB(sb)->s_journal) {
393 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
394 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
395 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
397 if (err == 0)
398 err = err2;
399 mnt_drop_write_file(filp);
400 group_extend_out:
401 ext4_resize_end(sb);
402 return err;
405 case EXT4_IOC_MOVE_EXT: {
406 struct move_extent me;
407 struct fd donor;
408 int err;
410 if (!(filp->f_mode & FMODE_READ) ||
411 !(filp->f_mode & FMODE_WRITE))
412 return -EBADF;
414 if (copy_from_user(&me,
415 (struct move_extent __user *)arg, sizeof(me)))
416 return -EFAULT;
417 me.moved_len = 0;
419 donor = fdget(me.donor_fd);
420 if (!donor.file)
421 return -EBADF;
423 if (!(donor.file->f_mode & FMODE_WRITE)) {
424 err = -EBADF;
425 goto mext_out;
428 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
429 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
430 ext4_msg(sb, KERN_ERR,
431 "Online defrag not supported with bigalloc");
432 err = -EOPNOTSUPP;
433 goto mext_out;
436 err = mnt_want_write_file(filp);
437 if (err)
438 goto mext_out;
440 err = ext4_move_extents(filp, donor.file, me.orig_start,
441 me.donor_start, me.len, &me.moved_len);
442 mnt_drop_write_file(filp);
444 if (copy_to_user((struct move_extent __user *)arg,
445 &me, sizeof(me)))
446 err = -EFAULT;
447 mext_out:
448 fdput(donor);
449 return err;
452 case EXT4_IOC_GROUP_ADD: {
453 struct ext4_new_group_data input;
454 int err, err2=0;
456 err = ext4_resize_begin(sb);
457 if (err)
458 return err;
460 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
461 sizeof(input))) {
462 err = -EFAULT;
463 goto group_add_out;
466 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
467 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
468 ext4_msg(sb, KERN_ERR,
469 "Online resizing not supported with bigalloc");
470 err = -EOPNOTSUPP;
471 goto group_add_out;
474 err = mnt_want_write_file(filp);
475 if (err)
476 goto group_add_out;
478 err = ext4_group_add(sb, &input);
479 if (EXT4_SB(sb)->s_journal) {
480 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
481 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
482 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
484 if (err == 0)
485 err = err2;
486 mnt_drop_write_file(filp);
487 if (!err && ext4_has_group_desc_csum(sb) &&
488 test_opt(sb, INIT_INODE_TABLE))
489 err = ext4_register_li_request(sb, input.group);
490 group_add_out:
491 ext4_resize_end(sb);
492 return err;
495 case EXT4_IOC_MIGRATE:
497 int err;
498 if (!inode_owner_or_capable(inode))
499 return -EACCES;
501 err = mnt_want_write_file(filp);
502 if (err)
503 return err;
505 * inode_mutex prevent write and truncate on the file.
506 * Read still goes through. We take i_data_sem in
507 * ext4_ext_swap_inode_data before we switch the
508 * inode format to prevent read.
510 mutex_lock(&(inode->i_mutex));
511 err = ext4_ext_migrate(inode);
512 mutex_unlock(&(inode->i_mutex));
513 mnt_drop_write_file(filp);
514 return err;
517 case EXT4_IOC_ALLOC_DA_BLKS:
519 int err;
520 if (!inode_owner_or_capable(inode))
521 return -EACCES;
523 err = mnt_want_write_file(filp);
524 if (err)
525 return err;
526 err = ext4_alloc_da_blocks(inode);
527 mnt_drop_write_file(filp);
528 return err;
531 case EXT4_IOC_SWAP_BOOT:
533 int err;
534 if (!(filp->f_mode & FMODE_WRITE))
535 return -EBADF;
536 err = mnt_want_write_file(filp);
537 if (err)
538 return err;
539 err = swap_inode_boot_loader(sb, inode);
540 mnt_drop_write_file(filp);
541 return err;
544 case EXT4_IOC_RESIZE_FS: {
545 ext4_fsblk_t n_blocks_count;
546 int err = 0, err2 = 0;
547 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
549 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
550 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
551 ext4_msg(sb, KERN_ERR,
552 "Online resizing not (yet) supported with bigalloc");
553 return -EOPNOTSUPP;
556 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
557 sizeof(__u64))) {
558 return -EFAULT;
561 err = ext4_resize_begin(sb);
562 if (err)
563 return err;
565 err = mnt_want_write_file(filp);
566 if (err)
567 goto resizefs_out;
569 err = ext4_resize_fs(sb, n_blocks_count);
570 if (EXT4_SB(sb)->s_journal) {
571 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
572 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
573 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
575 if (err == 0)
576 err = err2;
577 mnt_drop_write_file(filp);
578 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
579 ext4_has_group_desc_csum(sb) &&
580 test_opt(sb, INIT_INODE_TABLE))
581 err = ext4_register_li_request(sb, o_group);
583 resizefs_out:
584 ext4_resize_end(sb);
585 return err;
588 case FITRIM:
590 struct request_queue *q = bdev_get_queue(sb->s_bdev);
591 struct fstrim_range range;
592 int ret = 0;
594 if (!capable(CAP_SYS_ADMIN))
595 return -EPERM;
597 if (!blk_queue_discard(q))
598 return -EOPNOTSUPP;
600 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
601 sizeof(range)))
602 return -EFAULT;
604 range.minlen = max((unsigned int)range.minlen,
605 q->limits.discard_granularity);
606 ret = ext4_trim_fs(sb, &range);
607 if (ret < 0)
608 return ret;
610 if (copy_to_user((struct fstrim_range __user *)arg, &range,
611 sizeof(range)))
612 return -EFAULT;
614 return 0;
616 case EXT4_IOC_PRECACHE_EXTENTS:
617 return ext4_ext_precache(inode);
619 default:
620 return -ENOTTY;
624 #ifdef CONFIG_COMPAT
625 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
627 /* These are just misnamed, they actually get/put from/to user an int */
628 switch (cmd) {
629 case EXT4_IOC32_GETFLAGS:
630 cmd = EXT4_IOC_GETFLAGS;
631 break;
632 case EXT4_IOC32_SETFLAGS:
633 cmd = EXT4_IOC_SETFLAGS;
634 break;
635 case EXT4_IOC32_GETVERSION:
636 cmd = EXT4_IOC_GETVERSION;
637 break;
638 case EXT4_IOC32_SETVERSION:
639 cmd = EXT4_IOC_SETVERSION;
640 break;
641 case EXT4_IOC32_GROUP_EXTEND:
642 cmd = EXT4_IOC_GROUP_EXTEND;
643 break;
644 case EXT4_IOC32_GETVERSION_OLD:
645 cmd = EXT4_IOC_GETVERSION_OLD;
646 break;
647 case EXT4_IOC32_SETVERSION_OLD:
648 cmd = EXT4_IOC_SETVERSION_OLD;
649 break;
650 case EXT4_IOC32_GETRSVSZ:
651 cmd = EXT4_IOC_GETRSVSZ;
652 break;
653 case EXT4_IOC32_SETRSVSZ:
654 cmd = EXT4_IOC_SETRSVSZ;
655 break;
656 case EXT4_IOC32_GROUP_ADD: {
657 struct compat_ext4_new_group_input __user *uinput;
658 struct ext4_new_group_input input;
659 mm_segment_t old_fs;
660 int err;
662 uinput = compat_ptr(arg);
663 err = get_user(input.group, &uinput->group);
664 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
665 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
666 err |= get_user(input.inode_table, &uinput->inode_table);
667 err |= get_user(input.blocks_count, &uinput->blocks_count);
668 err |= get_user(input.reserved_blocks,
669 &uinput->reserved_blocks);
670 if (err)
671 return -EFAULT;
672 old_fs = get_fs();
673 set_fs(KERNEL_DS);
674 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
675 (unsigned long) &input);
676 set_fs(old_fs);
677 return err;
679 case EXT4_IOC_MOVE_EXT:
680 case FITRIM:
681 case EXT4_IOC_RESIZE_FS:
682 case EXT4_IOC_PRECACHE_EXTENTS:
683 break;
684 default:
685 return -ENOIOCTLCMD;
687 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
689 #endif