2 * linux/fs/ext4/ioctl.c
4 * Copyright (C) 1993, 1994, 1995
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
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"
21 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
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;
36 ap = (unsigned char *)a;
37 bp = (unsigned char *)b;
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)
61 struct ext4_inode_info *ei1;
62 struct ext4_inode_info *ei2;
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);
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,
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))
108 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
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)) {
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;
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);
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);
172 ext4_warning(inode->i_sb,
173 "couldn't mark inode #%lu dirty (err %d)",
175 /* Revert all changes: */
176 swap_inode_data(inode, inode_bl);
178 err = ext4_mark_inode_dirty(handle, inode_bl);
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);
192 ext4_inode_resume_unlocked_dio(inode);
193 ext4_inode_resume_unlocked_dio(inode_bl);
194 unlock_two_nondirectories(inode, inode_bl);
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);
206 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
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;
220 if (!inode_owner_or_capable(inode))
223 if (get_user(flags, (int __user *) arg))
226 err = mnt_want_write_file(filp);
230 flags = ext4_mask_flags(inode->i_mode, flags);
233 mutex_lock(&inode->i_mutex);
234 /* Is it quota file? Do not allow user to mess with it */
235 if (IS_NOQUOTA(inode))
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))
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))
262 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
265 if (flags & EXT4_EOFBLOCKS_FL) {
266 /* we don't support adding EOFBLOCKS flag */
267 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
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);
280 ext4_handle_sync(handle);
281 err = ext4_reserve_inode_write(handle, inode, &iloc);
285 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
286 if (!(mask & EXT4_FL_USER_MODIFIABLE))
289 ext4_set_inode_flag(inode, i);
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);
299 ext4_journal_stop(handle);
303 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
304 err = ext4_change_inode_journal_flag(inode, jflag);
308 if (flags & EXT4_EXTENTS_FL)
309 err = ext4_ext_migrate(inode);
311 err = ext4_ind_migrate(inode);
315 mutex_unlock(&inode->i_mutex);
316 mnt_drop_write_file(filp);
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: {
325 struct ext4_iloc iloc;
329 if (!inode_owner_or_capable(inode))
332 if (ext4_has_metadata_csum(inode->i_sb)) {
333 ext4_warning(sb, "Setting inode version is not "
334 "supported with metadata_csum enabled.");
338 err = mnt_want_write_file(filp);
341 if (get_user(generation, (int __user *) arg)) {
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);
352 err = ext4_reserve_inode_write(handle, inode, &iloc);
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);
361 mutex_unlock(&inode->i_mutex);
363 mnt_drop_write_file(filp);
366 case EXT4_IOC_GROUP_EXTEND: {
367 ext4_fsblk_t n_blocks_count;
370 err = ext4_resize_begin(sb);
374 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
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");
384 goto group_extend_out;
387 err = mnt_want_write_file(filp);
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);
399 mnt_drop_write_file(filp);
405 case EXT4_IOC_MOVE_EXT: {
406 struct move_extent me;
410 if (!(filp->f_mode & FMODE_READ) ||
411 !(filp->f_mode & FMODE_WRITE))
414 if (copy_from_user(&me,
415 (struct move_extent __user *)arg, sizeof(me)))
419 donor = fdget(me.donor_fd);
423 if (!(donor.file->f_mode & FMODE_WRITE)) {
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");
436 err = mnt_want_write_file(filp);
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,
452 case EXT4_IOC_GROUP_ADD: {
453 struct ext4_new_group_data input;
456 err = ext4_resize_begin(sb);
460 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
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");
474 err = mnt_want_write_file(filp);
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);
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);
495 case EXT4_IOC_MIGRATE:
498 if (!inode_owner_or_capable(inode))
501 err = mnt_want_write_file(filp);
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);
517 case EXT4_IOC_ALLOC_DA_BLKS:
520 if (!inode_owner_or_capable(inode))
523 err = mnt_want_write_file(filp);
526 err = ext4_alloc_da_blocks(inode);
527 mnt_drop_write_file(filp);
531 case EXT4_IOC_SWAP_BOOT:
534 if (!(filp->f_mode & FMODE_WRITE))
536 err = mnt_want_write_file(filp);
539 err = swap_inode_boot_loader(sb, inode);
540 mnt_drop_write_file(filp);
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");
556 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
561 err = ext4_resize_begin(sb);
565 err = mnt_want_write_file(filp);
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);
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);
590 struct request_queue *q = bdev_get_queue(sb->s_bdev);
591 struct fstrim_range range;
594 if (!capable(CAP_SYS_ADMIN))
597 if (!blk_queue_discard(q))
600 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
604 range.minlen = max((unsigned int)range.minlen,
605 q->limits.discard_granularity);
606 ret = ext4_trim_fs(sb, &range);
610 if (copy_to_user((struct fstrim_range __user *)arg, &range,
616 case EXT4_IOC_PRECACHE_EXTENTS:
617 return ext4_ext_precache(inode);
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 */
629 case EXT4_IOC32_GETFLAGS:
630 cmd = EXT4_IOC_GETFLAGS;
632 case EXT4_IOC32_SETFLAGS:
633 cmd = EXT4_IOC_SETFLAGS;
635 case EXT4_IOC32_GETVERSION:
636 cmd = EXT4_IOC_GETVERSION;
638 case EXT4_IOC32_SETVERSION:
639 cmd = EXT4_IOC_SETVERSION;
641 case EXT4_IOC32_GROUP_EXTEND:
642 cmd = EXT4_IOC_GROUP_EXTEND;
644 case EXT4_IOC32_GETVERSION_OLD:
645 cmd = EXT4_IOC_GETVERSION_OLD;
647 case EXT4_IOC32_SETVERSION_OLD:
648 cmd = EXT4_IOC_SETVERSION_OLD;
650 case EXT4_IOC32_GETRSVSZ:
651 cmd = EXT4_IOC_GETRSVSZ;
653 case EXT4_IOC32_SETRSVSZ:
654 cmd = EXT4_IOC_SETRSVSZ;
656 case EXT4_IOC32_GROUP_ADD: {
657 struct compat_ext4_new_group_input __user *uinput;
658 struct ext4_new_group_input input;
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);
674 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
675 (unsigned long) &input);
679 case EXT4_IOC_MOVE_EXT:
681 case EXT4_IOC_RESIZE_FS:
682 case EXT4_IOC_PRECACHE_EXTENTS:
687 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));