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/capability.h>
12 #include <linux/time.h>
13 #include <linux/compat.h>
14 #include <linux/mount.h>
15 #include <linux/file.h>
16 #include <linux/random.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 static int uuid_is_zero(__u8 u[16])
203 for (i = 0; i < 16; i++)
209 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
211 struct inode *inode = file_inode(filp);
212 struct super_block *sb = inode->i_sb;
213 struct ext4_inode_info *ei = EXT4_I(inode);
216 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
219 case EXT4_IOC_GETFLAGS:
220 ext4_get_inode_flags(ei);
221 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
222 return put_user(flags, (int __user *) arg);
223 case EXT4_IOC_SETFLAGS: {
224 handle_t *handle = NULL;
225 int err, migrate = 0;
226 struct ext4_iloc iloc;
227 unsigned int oldflags, mask, i;
230 if (!inode_owner_or_capable(inode))
233 if (get_user(flags, (int __user *) arg))
236 err = mnt_want_write_file(filp);
240 flags = ext4_mask_flags(inode->i_mode, flags);
243 mutex_lock(&inode->i_mutex);
244 /* Is it quota file? Do not allow user to mess with it */
245 if (IS_NOQUOTA(inode))
248 oldflags = ei->i_flags;
250 /* The JOURNAL_DATA flag is modifiable only by root */
251 jflag = flags & EXT4_JOURNAL_DATA_FL;
254 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
255 * the relevant capability.
257 * This test looks nicer. Thanks to Pauline Middelink
259 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
260 if (!capable(CAP_LINUX_IMMUTABLE))
265 * The JOURNAL_DATA flag can only be changed by
266 * the relevant capability.
268 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
269 if (!capable(CAP_SYS_RESOURCE))
272 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
275 if (flags & EXT4_EOFBLOCKS_FL) {
276 /* we don't support adding EOFBLOCKS flag */
277 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
281 } else if (oldflags & EXT4_EOFBLOCKS_FL)
282 ext4_truncate(inode);
284 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
285 if (IS_ERR(handle)) {
286 err = PTR_ERR(handle);
290 ext4_handle_sync(handle);
291 err = ext4_reserve_inode_write(handle, inode, &iloc);
295 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
296 if (!(mask & EXT4_FL_USER_MODIFIABLE))
299 ext4_set_inode_flag(inode, i);
301 ext4_clear_inode_flag(inode, i);
304 ext4_set_inode_flags(inode);
305 inode->i_ctime = ext4_current_time(inode);
307 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
309 ext4_journal_stop(handle);
313 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
314 err = ext4_change_inode_journal_flag(inode, jflag);
318 if (flags & EXT4_EXTENTS_FL)
319 err = ext4_ext_migrate(inode);
321 err = ext4_ind_migrate(inode);
325 mutex_unlock(&inode->i_mutex);
326 mnt_drop_write_file(filp);
329 case EXT4_IOC_GETVERSION:
330 case EXT4_IOC_GETVERSION_OLD:
331 return put_user(inode->i_generation, (int __user *) arg);
332 case EXT4_IOC_SETVERSION:
333 case EXT4_IOC_SETVERSION_OLD: {
335 struct ext4_iloc iloc;
339 if (!inode_owner_or_capable(inode))
342 if (ext4_has_metadata_csum(inode->i_sb)) {
343 ext4_warning(sb, "Setting inode version is not "
344 "supported with metadata_csum enabled.");
348 err = mnt_want_write_file(filp);
351 if (get_user(generation, (int __user *) arg)) {
356 mutex_lock(&inode->i_mutex);
357 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
358 if (IS_ERR(handle)) {
359 err = PTR_ERR(handle);
362 err = ext4_reserve_inode_write(handle, inode, &iloc);
364 inode->i_ctime = ext4_current_time(inode);
365 inode->i_generation = generation;
366 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
368 ext4_journal_stop(handle);
371 mutex_unlock(&inode->i_mutex);
373 mnt_drop_write_file(filp);
376 case EXT4_IOC_GROUP_EXTEND: {
377 ext4_fsblk_t n_blocks_count;
380 err = ext4_resize_begin(sb);
384 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
386 goto group_extend_out;
389 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
390 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
391 ext4_msg(sb, KERN_ERR,
392 "Online resizing not supported with bigalloc");
394 goto group_extend_out;
397 err = mnt_want_write_file(filp);
399 goto group_extend_out;
401 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
402 if (EXT4_SB(sb)->s_journal) {
403 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
404 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
405 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
409 mnt_drop_write_file(filp);
415 case EXT4_IOC_MOVE_EXT: {
416 struct move_extent me;
420 if (!(filp->f_mode & FMODE_READ) ||
421 !(filp->f_mode & FMODE_WRITE))
424 if (copy_from_user(&me,
425 (struct move_extent __user *)arg, sizeof(me)))
429 donor = fdget(me.donor_fd);
433 if (!(donor.file->f_mode & FMODE_WRITE)) {
438 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
439 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
440 ext4_msg(sb, KERN_ERR,
441 "Online defrag not supported with bigalloc");
446 err = mnt_want_write_file(filp);
450 err = ext4_move_extents(filp, donor.file, me.orig_start,
451 me.donor_start, me.len, &me.moved_len);
452 mnt_drop_write_file(filp);
454 if (copy_to_user((struct move_extent __user *)arg,
462 case EXT4_IOC_GROUP_ADD: {
463 struct ext4_new_group_data input;
466 err = ext4_resize_begin(sb);
470 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
476 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
477 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
478 ext4_msg(sb, KERN_ERR,
479 "Online resizing not supported with bigalloc");
484 err = mnt_want_write_file(filp);
488 err = ext4_group_add(sb, &input);
489 if (EXT4_SB(sb)->s_journal) {
490 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
491 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
492 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
496 mnt_drop_write_file(filp);
497 if (!err && ext4_has_group_desc_csum(sb) &&
498 test_opt(sb, INIT_INODE_TABLE))
499 err = ext4_register_li_request(sb, input.group);
505 case EXT4_IOC_MIGRATE:
508 if (!inode_owner_or_capable(inode))
511 err = mnt_want_write_file(filp);
515 * inode_mutex prevent write and truncate on the file.
516 * Read still goes through. We take i_data_sem in
517 * ext4_ext_swap_inode_data before we switch the
518 * inode format to prevent read.
520 mutex_lock(&(inode->i_mutex));
521 err = ext4_ext_migrate(inode);
522 mutex_unlock(&(inode->i_mutex));
523 mnt_drop_write_file(filp);
527 case EXT4_IOC_ALLOC_DA_BLKS:
530 if (!inode_owner_or_capable(inode))
533 err = mnt_want_write_file(filp);
536 err = ext4_alloc_da_blocks(inode);
537 mnt_drop_write_file(filp);
541 case EXT4_IOC_SWAP_BOOT:
544 if (!(filp->f_mode & FMODE_WRITE))
546 err = mnt_want_write_file(filp);
549 err = swap_inode_boot_loader(sb, inode);
550 mnt_drop_write_file(filp);
554 case EXT4_IOC_RESIZE_FS: {
555 ext4_fsblk_t n_blocks_count;
556 int err = 0, err2 = 0;
557 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
559 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
561 ext4_msg(sb, KERN_ERR,
562 "Online resizing not (yet) supported with bigalloc");
566 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
571 err = ext4_resize_begin(sb);
575 err = mnt_want_write_file(filp);
579 err = ext4_resize_fs(sb, n_blocks_count);
580 if (EXT4_SB(sb)->s_journal) {
581 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
582 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
583 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
587 mnt_drop_write_file(filp);
588 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
589 ext4_has_group_desc_csum(sb) &&
590 test_opt(sb, INIT_INODE_TABLE))
591 err = ext4_register_li_request(sb, o_group);
600 struct request_queue *q = bdev_get_queue(sb->s_bdev);
601 struct fstrim_range range;
604 if (!capable(CAP_SYS_ADMIN))
607 if (!blk_queue_discard(q))
610 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
614 range.minlen = max((unsigned int)range.minlen,
615 q->limits.discard_granularity);
616 ret = ext4_trim_fs(sb, &range);
620 if (copy_to_user((struct fstrim_range __user *)arg, &range,
626 case EXT4_IOC_PRECACHE_EXTENTS:
627 return ext4_ext_precache(inode);
628 case EXT4_IOC_SET_ENCRYPTION_POLICY: {
629 #ifdef CONFIG_EXT4_FS_ENCRYPTION
630 struct ext4_encryption_policy policy;
633 if (copy_from_user(&policy,
634 (struct ext4_encryption_policy __user *)arg,
637 goto encryption_policy_out;
640 err = ext4_process_policy(&policy, inode);
641 encryption_policy_out:
647 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
649 struct ext4_sb_info *sbi = EXT4_SB(sb);
652 if (!ext4_sb_has_crypto(sb))
654 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
655 err = mnt_want_write_file(filp);
658 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
659 if (IS_ERR(handle)) {
660 err = PTR_ERR(handle);
661 goto pwsalt_err_exit;
663 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
665 goto pwsalt_err_journal;
666 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
667 err = ext4_handle_dirty_metadata(handle, NULL,
670 err2 = ext4_journal_stop(handle);
674 mnt_drop_write_file(filp);
678 if (copy_to_user((void *) arg, sbi->s_es->s_encrypt_pw_salt,
683 case EXT4_IOC_GET_ENCRYPTION_POLICY: {
684 #ifdef CONFIG_EXT4_FS_ENCRYPTION
685 struct ext4_encryption_policy policy;
688 if (!ext4_encrypted_inode(inode))
690 err = ext4_get_policy(inode, &policy);
693 if (copy_to_user((void *)arg, &policy, sizeof(policy)))
706 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
708 /* These are just misnamed, they actually get/put from/to user an int */
710 case EXT4_IOC32_GETFLAGS:
711 cmd = EXT4_IOC_GETFLAGS;
713 case EXT4_IOC32_SETFLAGS:
714 cmd = EXT4_IOC_SETFLAGS;
716 case EXT4_IOC32_GETVERSION:
717 cmd = EXT4_IOC_GETVERSION;
719 case EXT4_IOC32_SETVERSION:
720 cmd = EXT4_IOC_SETVERSION;
722 case EXT4_IOC32_GROUP_EXTEND:
723 cmd = EXT4_IOC_GROUP_EXTEND;
725 case EXT4_IOC32_GETVERSION_OLD:
726 cmd = EXT4_IOC_GETVERSION_OLD;
728 case EXT4_IOC32_SETVERSION_OLD:
729 cmd = EXT4_IOC_SETVERSION_OLD;
731 case EXT4_IOC32_GETRSVSZ:
732 cmd = EXT4_IOC_GETRSVSZ;
734 case EXT4_IOC32_SETRSVSZ:
735 cmd = EXT4_IOC_SETRSVSZ;
737 case EXT4_IOC32_GROUP_ADD: {
738 struct compat_ext4_new_group_input __user *uinput;
739 struct ext4_new_group_input input;
743 uinput = compat_ptr(arg);
744 err = get_user(input.group, &uinput->group);
745 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
746 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
747 err |= get_user(input.inode_table, &uinput->inode_table);
748 err |= get_user(input.blocks_count, &uinput->blocks_count);
749 err |= get_user(input.reserved_blocks,
750 &uinput->reserved_blocks);
755 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
756 (unsigned long) &input);
760 case EXT4_IOC_MOVE_EXT:
762 case EXT4_IOC_RESIZE_FS:
763 case EXT4_IOC_PRECACHE_EXTENTS:
764 case EXT4_IOC_SET_ENCRYPTION_POLICY:
765 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
766 case EXT4_IOC_GET_ENCRYPTION_POLICY:
771 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));