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)
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"
20 #include "ext4_extents.h"
22 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
25 * Swap memory between @a and @b for @len bytes.
27 * @a: pointer to first memory area
28 * @b: pointer to second memory area
29 * @len: number of bytes to swap
32 static void memswap(void *a, void *b, size_t len)
34 unsigned char *ap, *bp;
37 ap = (unsigned char *)a;
38 bp = (unsigned char *)b;
49 * Swap i_data and associated attributes between @inode1 and @inode2.
50 * This function is used for the primary swap between inode1 and inode2
51 * and also to revert this primary swap in case of errors.
53 * Therefore you have to make sure, that calling this method twice
54 * will revert all changes.
56 * @inode1: pointer to first inode
57 * @inode2: pointer to second inode
59 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
62 struct ext4_inode_info *ei1;
63 struct ext4_inode_info *ei2;
68 memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
69 memswap(&inode1->i_version, &inode2->i_version,
70 sizeof(inode1->i_version));
71 memswap(&inode1->i_blocks, &inode2->i_blocks,
72 sizeof(inode1->i_blocks));
73 memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
74 memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
75 memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
77 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
78 memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
79 memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
80 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
81 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
82 ext4_es_lru_del(inode1);
83 ext4_es_lru_del(inode2);
85 isize = i_size_read(inode1);
86 i_size_write(inode1, i_size_read(inode2));
87 i_size_write(inode2, isize);
91 * Swap the information from the given @inode and the inode
92 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
93 * important fields of the inodes.
95 * @sb: the super block of the filesystem
96 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
99 static long swap_inode_boot_loader(struct super_block *sb,
104 struct inode *inode_bl;
105 struct ext4_inode_info *ei;
106 struct ext4_inode_info *ei_bl;
107 struct ext4_sb_info *sbi;
109 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode)) {
114 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
122 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
123 if (IS_ERR(inode_bl)) {
124 err = PTR_ERR(inode_bl);
127 ei_bl = EXT4_I(inode_bl);
129 filemap_flush(inode->i_mapping);
130 filemap_flush(inode_bl->i_mapping);
132 /* Protect orig inodes against a truncate and make sure,
133 * that only 1 swap_inode_boot_loader is running. */
134 ext4_inode_double_lock(inode, inode_bl);
136 truncate_inode_pages(&inode->i_data, 0);
137 truncate_inode_pages(&inode_bl->i_data, 0);
139 /* Wait for all existing dio workers */
140 ext4_inode_block_unlocked_dio(inode);
141 ext4_inode_block_unlocked_dio(inode_bl);
142 inode_dio_wait(inode);
143 inode_dio_wait(inode_bl);
145 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
146 if (IS_ERR(handle)) {
151 /* Protect extent tree against block allocations via delalloc */
152 ext4_double_down_write_data_sem(inode, inode_bl);
154 if (inode_bl->i_nlink == 0) {
155 /* this inode has never been used as a BOOT_LOADER */
156 set_nlink(inode_bl, 1);
157 i_uid_write(inode_bl, 0);
158 i_gid_write(inode_bl, 0);
159 inode_bl->i_flags = 0;
161 inode_bl->i_version = 1;
162 i_size_write(inode_bl, 0);
163 inode_bl->i_mode = S_IFREG;
164 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
165 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
166 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
167 ext4_ext_tree_init(handle, inode_bl);
169 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
172 swap_inode_data(inode, inode_bl);
174 inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
176 spin_lock(&sbi->s_next_gen_lock);
177 inode->i_generation = sbi->s_next_generation++;
178 inode_bl->i_generation = sbi->s_next_generation++;
179 spin_unlock(&sbi->s_next_gen_lock);
181 ext4_discard_preallocations(inode);
183 err = ext4_mark_inode_dirty(handle, inode);
185 ext4_warning(inode->i_sb,
186 "couldn't mark inode #%lu dirty (err %d)",
188 /* Revert all changes: */
189 swap_inode_data(inode, inode_bl);
191 err = ext4_mark_inode_dirty(handle, inode_bl);
193 ext4_warning(inode_bl->i_sb,
194 "couldn't mark inode #%lu dirty (err %d)",
195 inode_bl->i_ino, err);
196 /* Revert all changes: */
197 swap_inode_data(inode, inode_bl);
198 ext4_mark_inode_dirty(handle, inode);
202 ext4_journal_stop(handle);
204 ext4_double_up_write_data_sem(inode, inode_bl);
206 ext4_inode_resume_unlocked_dio(inode);
207 ext4_inode_resume_unlocked_dio(inode_bl);
209 ext4_inode_double_unlock(inode, inode_bl);
217 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
219 struct inode *inode = file_inode(filp);
220 struct super_block *sb = inode->i_sb;
221 struct ext4_inode_info *ei = EXT4_I(inode);
224 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
227 case EXT4_IOC_GETFLAGS:
228 ext4_get_inode_flags(ei);
229 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
230 return put_user(flags, (int __user *) arg);
231 case EXT4_IOC_SETFLAGS: {
232 handle_t *handle = NULL;
233 int err, migrate = 0;
234 struct ext4_iloc iloc;
235 unsigned int oldflags, mask, i;
238 if (!inode_owner_or_capable(inode))
241 if (get_user(flags, (int __user *) arg))
244 err = mnt_want_write_file(filp);
248 flags = ext4_mask_flags(inode->i_mode, flags);
251 mutex_lock(&inode->i_mutex);
252 /* Is it quota file? Do not allow user to mess with it */
253 if (IS_NOQUOTA(inode))
256 oldflags = ei->i_flags;
258 /* The JOURNAL_DATA flag is modifiable only by root */
259 jflag = flags & EXT4_JOURNAL_DATA_FL;
262 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
263 * the relevant capability.
265 * This test looks nicer. Thanks to Pauline Middelink
267 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
268 if (!capable(CAP_LINUX_IMMUTABLE))
273 * The JOURNAL_DATA flag can only be changed by
274 * the relevant capability.
276 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
277 if (!capable(CAP_SYS_RESOURCE))
280 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
283 if (flags & EXT4_EOFBLOCKS_FL) {
284 /* we don't support adding EOFBLOCKS flag */
285 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
289 } else if (oldflags & EXT4_EOFBLOCKS_FL)
290 ext4_truncate(inode);
292 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
293 if (IS_ERR(handle)) {
294 err = PTR_ERR(handle);
298 ext4_handle_sync(handle);
299 err = ext4_reserve_inode_write(handle, inode, &iloc);
303 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
304 if (!(mask & EXT4_FL_USER_MODIFIABLE))
307 ext4_set_inode_flag(inode, i);
309 ext4_clear_inode_flag(inode, i);
312 ext4_set_inode_flags(inode);
313 inode->i_ctime = ext4_current_time(inode);
315 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
317 ext4_journal_stop(handle);
321 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
322 err = ext4_change_inode_journal_flag(inode, jflag);
326 if (flags & EXT4_EXTENTS_FL)
327 err = ext4_ext_migrate(inode);
329 err = ext4_ind_migrate(inode);
333 mutex_unlock(&inode->i_mutex);
334 mnt_drop_write_file(filp);
337 case EXT4_IOC_GETVERSION:
338 case EXT4_IOC_GETVERSION_OLD:
339 return put_user(inode->i_generation, (int __user *) arg);
340 case EXT4_IOC_SETVERSION:
341 case EXT4_IOC_SETVERSION_OLD: {
343 struct ext4_iloc iloc;
347 if (!inode_owner_or_capable(inode))
350 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
351 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
352 ext4_warning(sb, "Setting inode version is not "
353 "supported with metadata_csum enabled.");
357 err = mnt_want_write_file(filp);
360 if (get_user(generation, (int __user *) arg)) {
365 mutex_lock(&inode->i_mutex);
366 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
367 if (IS_ERR(handle)) {
368 err = PTR_ERR(handle);
371 err = ext4_reserve_inode_write(handle, inode, &iloc);
373 inode->i_ctime = ext4_current_time(inode);
374 inode->i_generation = generation;
375 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
377 ext4_journal_stop(handle);
380 mutex_unlock(&inode->i_mutex);
382 mnt_drop_write_file(filp);
385 case EXT4_IOC_GROUP_EXTEND: {
386 ext4_fsblk_t n_blocks_count;
389 err = ext4_resize_begin(sb);
393 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
395 goto group_extend_out;
398 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
399 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
400 ext4_msg(sb, KERN_ERR,
401 "Online resizing not supported with bigalloc");
403 goto group_extend_out;
406 err = mnt_want_write_file(filp);
408 goto group_extend_out;
410 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
411 if (EXT4_SB(sb)->s_journal) {
412 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
413 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
414 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
418 mnt_drop_write_file(filp);
424 case EXT4_IOC_MOVE_EXT: {
425 struct move_extent me;
429 if (!(filp->f_mode & FMODE_READ) ||
430 !(filp->f_mode & FMODE_WRITE))
433 if (copy_from_user(&me,
434 (struct move_extent __user *)arg, sizeof(me)))
438 donor = fdget(me.donor_fd);
442 if (!(donor.file->f_mode & FMODE_WRITE)) {
447 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
448 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
449 ext4_msg(sb, KERN_ERR,
450 "Online defrag not supported with bigalloc");
455 err = mnt_want_write_file(filp);
459 err = ext4_move_extents(filp, donor.file, me.orig_start,
460 me.donor_start, me.len, &me.moved_len);
461 mnt_drop_write_file(filp);
463 if (copy_to_user((struct move_extent __user *)arg,
471 case EXT4_IOC_GROUP_ADD: {
472 struct ext4_new_group_data input;
475 err = ext4_resize_begin(sb);
479 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
485 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
486 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
487 ext4_msg(sb, KERN_ERR,
488 "Online resizing not supported with bigalloc");
493 err = mnt_want_write_file(filp);
497 err = ext4_group_add(sb, &input);
498 if (EXT4_SB(sb)->s_journal) {
499 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
500 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
501 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
505 mnt_drop_write_file(filp);
506 if (!err && ext4_has_group_desc_csum(sb) &&
507 test_opt(sb, INIT_INODE_TABLE))
508 err = ext4_register_li_request(sb, input.group);
514 case EXT4_IOC_MIGRATE:
517 if (!inode_owner_or_capable(inode))
520 err = mnt_want_write_file(filp);
524 * inode_mutex prevent write and truncate on the file.
525 * Read still goes through. We take i_data_sem in
526 * ext4_ext_swap_inode_data before we switch the
527 * inode format to prevent read.
529 mutex_lock(&(inode->i_mutex));
530 err = ext4_ext_migrate(inode);
531 mutex_unlock(&(inode->i_mutex));
532 mnt_drop_write_file(filp);
536 case EXT4_IOC_ALLOC_DA_BLKS:
539 if (!inode_owner_or_capable(inode))
542 err = mnt_want_write_file(filp);
545 err = ext4_alloc_da_blocks(inode);
546 mnt_drop_write_file(filp);
550 case EXT4_IOC_SWAP_BOOT:
551 if (!(filp->f_mode & FMODE_WRITE))
553 return swap_inode_boot_loader(sb, inode);
555 case EXT4_IOC_RESIZE_FS: {
556 ext4_fsblk_t n_blocks_count;
557 int err = 0, err2 = 0;
558 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
560 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
561 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
562 ext4_msg(sb, KERN_ERR,
563 "Online resizing not (yet) supported with bigalloc");
567 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
572 err = ext4_resize_begin(sb);
576 err = mnt_want_write_file(filp);
580 err = ext4_resize_fs(sb, n_blocks_count);
581 if (EXT4_SB(sb)->s_journal) {
582 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
583 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
584 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
588 mnt_drop_write_file(filp);
589 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
590 ext4_has_group_desc_csum(sb) &&
591 test_opt(sb, INIT_INODE_TABLE))
592 err = ext4_register_li_request(sb, o_group);
601 struct request_queue *q = bdev_get_queue(sb->s_bdev);
602 struct fstrim_range range;
605 if (!capable(CAP_SYS_ADMIN))
608 if (!blk_queue_discard(q))
611 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
615 range.minlen = max((unsigned int)range.minlen,
616 q->limits.discard_granularity);
617 ret = ext4_trim_fs(sb, &range);
621 if (copy_to_user((struct fstrim_range __user *)arg, &range,
634 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
636 /* These are just misnamed, they actually get/put from/to user an int */
638 case EXT4_IOC32_GETFLAGS:
639 cmd = EXT4_IOC_GETFLAGS;
641 case EXT4_IOC32_SETFLAGS:
642 cmd = EXT4_IOC_SETFLAGS;
644 case EXT4_IOC32_GETVERSION:
645 cmd = EXT4_IOC_GETVERSION;
647 case EXT4_IOC32_SETVERSION:
648 cmd = EXT4_IOC_SETVERSION;
650 case EXT4_IOC32_GROUP_EXTEND:
651 cmd = EXT4_IOC_GROUP_EXTEND;
653 case EXT4_IOC32_GETVERSION_OLD:
654 cmd = EXT4_IOC_GETVERSION_OLD;
656 case EXT4_IOC32_SETVERSION_OLD:
657 cmd = EXT4_IOC_SETVERSION_OLD;
659 case EXT4_IOC32_GETRSVSZ:
660 cmd = EXT4_IOC_GETRSVSZ;
662 case EXT4_IOC32_SETRSVSZ:
663 cmd = EXT4_IOC_SETRSVSZ;
665 case EXT4_IOC32_GROUP_ADD: {
666 struct compat_ext4_new_group_input __user *uinput;
667 struct ext4_new_group_input input;
671 uinput = compat_ptr(arg);
672 err = get_user(input.group, &uinput->group);
673 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
674 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
675 err |= get_user(input.inode_table, &uinput->inode_table);
676 err |= get_user(input.blocks_count, &uinput->blocks_count);
677 err |= get_user(input.reserved_blocks,
678 &uinput->reserved_blocks);
683 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
684 (unsigned long) &input);
688 case EXT4_IOC_MOVE_EXT:
690 case EXT4_IOC_RESIZE_FS:
695 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));