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/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;
35 ap = (unsigned char *)a;
36 bp = (unsigned char *)b;
45 * Swap i_data and associated attributes between @inode1 and @inode2.
46 * This function is used for the primary swap between inode1 and inode2
47 * and also to revert this primary swap in case of errors.
49 * Therefore you have to make sure, that calling this method twice
50 * will revert all changes.
52 * @inode1: pointer to first inode
53 * @inode2: pointer to second inode
55 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
58 struct ext4_inode_info *ei1;
59 struct ext4_inode_info *ei2;
64 memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
65 memswap(&inode1->i_version, &inode2->i_version,
66 sizeof(inode1->i_version));
67 memswap(&inode1->i_blocks, &inode2->i_blocks,
68 sizeof(inode1->i_blocks));
69 memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
70 memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
71 memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
73 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
74 memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
75 memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
76 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
77 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
79 isize = i_size_read(inode1);
80 i_size_write(inode1, i_size_read(inode2));
81 i_size_write(inode2, isize);
85 * Swap the information from the given @inode and the inode
86 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
87 * important fields of the inodes.
89 * @sb: the super block of the filesystem
90 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
93 static long swap_inode_boot_loader(struct super_block *sb,
98 struct inode *inode_bl;
99 struct ext4_inode_info *ei_bl;
100 struct ext4_sb_info *sbi = EXT4_SB(sb);
102 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
105 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
108 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
109 if (IS_ERR(inode_bl))
110 return PTR_ERR(inode_bl);
111 ei_bl = EXT4_I(inode_bl);
113 filemap_flush(inode->i_mapping);
114 filemap_flush(inode_bl->i_mapping);
116 /* Protect orig inodes against a truncate and make sure,
117 * that only 1 swap_inode_boot_loader is running. */
118 lock_two_nondirectories(inode, inode_bl);
120 truncate_inode_pages(&inode->i_data, 0);
121 truncate_inode_pages(&inode_bl->i_data, 0);
123 /* Wait for all existing dio workers */
124 ext4_inode_block_unlocked_dio(inode);
125 ext4_inode_block_unlocked_dio(inode_bl);
126 inode_dio_wait(inode);
127 inode_dio_wait(inode_bl);
129 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
130 if (IS_ERR(handle)) {
132 goto journal_err_out;
135 /* Protect extent tree against block allocations via delalloc */
136 ext4_double_down_write_data_sem(inode, inode_bl);
138 if (inode_bl->i_nlink == 0) {
139 /* this inode has never been used as a BOOT_LOADER */
140 set_nlink(inode_bl, 1);
141 i_uid_write(inode_bl, 0);
142 i_gid_write(inode_bl, 0);
143 inode_bl->i_flags = 0;
145 inode_bl->i_version = 1;
146 i_size_write(inode_bl, 0);
147 inode_bl->i_mode = S_IFREG;
148 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
149 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
150 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
151 ext4_ext_tree_init(handle, inode_bl);
153 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
156 swap_inode_data(inode, inode_bl);
158 inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
160 spin_lock(&sbi->s_next_gen_lock);
161 inode->i_generation = sbi->s_next_generation++;
162 inode_bl->i_generation = sbi->s_next_generation++;
163 spin_unlock(&sbi->s_next_gen_lock);
165 ext4_discard_preallocations(inode);
167 err = ext4_mark_inode_dirty(handle, inode);
169 ext4_warning(inode->i_sb,
170 "couldn't mark inode #%lu dirty (err %d)",
172 /* Revert all changes: */
173 swap_inode_data(inode, inode_bl);
175 err = ext4_mark_inode_dirty(handle, inode_bl);
177 ext4_warning(inode_bl->i_sb,
178 "couldn't mark inode #%lu dirty (err %d)",
179 inode_bl->i_ino, err);
180 /* Revert all changes: */
181 swap_inode_data(inode, inode_bl);
182 ext4_mark_inode_dirty(handle, inode);
185 ext4_journal_stop(handle);
186 ext4_double_up_write_data_sem(inode, inode_bl);
189 ext4_inode_resume_unlocked_dio(inode);
190 ext4_inode_resume_unlocked_dio(inode_bl);
191 unlock_two_nondirectories(inode, inode_bl);
196 static int uuid_is_zero(__u8 u[16])
200 for (i = 0; i < 16; i++)
206 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
208 struct inode *inode = file_inode(filp);
209 struct super_block *sb = inode->i_sb;
210 struct ext4_inode_info *ei = EXT4_I(inode);
213 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
216 case EXT4_IOC_GETFLAGS:
217 ext4_get_inode_flags(ei);
218 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
219 return put_user(flags, (int __user *) arg);
220 case EXT4_IOC_SETFLAGS: {
221 handle_t *handle = NULL;
222 int err, migrate = 0;
223 struct ext4_iloc iloc;
224 unsigned int oldflags, mask, i;
227 if (!inode_owner_or_capable(inode))
230 if (get_user(flags, (int __user *) arg))
233 err = mnt_want_write_file(filp);
237 flags = ext4_mask_flags(inode->i_mode, flags);
240 mutex_lock(&inode->i_mutex);
241 /* Is it quota file? Do not allow user to mess with it */
242 if (IS_NOQUOTA(inode))
245 oldflags = ei->i_flags;
247 /* The JOURNAL_DATA flag is modifiable only by root */
248 jflag = flags & EXT4_JOURNAL_DATA_FL;
251 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
252 * the relevant capability.
254 * This test looks nicer. Thanks to Pauline Middelink
256 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
257 if (!capable(CAP_LINUX_IMMUTABLE))
262 * The JOURNAL_DATA flag can only be changed by
263 * the relevant capability.
265 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
266 if (!capable(CAP_SYS_RESOURCE))
269 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
272 if (flags & EXT4_EOFBLOCKS_FL) {
273 /* we don't support adding EOFBLOCKS flag */
274 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
278 } else if (oldflags & EXT4_EOFBLOCKS_FL)
279 ext4_truncate(inode);
281 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
282 if (IS_ERR(handle)) {
283 err = PTR_ERR(handle);
287 ext4_handle_sync(handle);
288 err = ext4_reserve_inode_write(handle, inode, &iloc);
292 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
293 if (!(mask & EXT4_FL_USER_MODIFIABLE))
296 ext4_set_inode_flag(inode, i);
298 ext4_clear_inode_flag(inode, i);
301 ext4_set_inode_flags(inode);
302 inode->i_ctime = ext4_current_time(inode);
304 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
306 ext4_journal_stop(handle);
310 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
311 err = ext4_change_inode_journal_flag(inode, jflag);
315 if (flags & EXT4_EXTENTS_FL)
316 err = ext4_ext_migrate(inode);
318 err = ext4_ind_migrate(inode);
322 mutex_unlock(&inode->i_mutex);
323 mnt_drop_write_file(filp);
326 case EXT4_IOC_GETVERSION:
327 case EXT4_IOC_GETVERSION_OLD:
328 return put_user(inode->i_generation, (int __user *) arg);
329 case EXT4_IOC_SETVERSION:
330 case EXT4_IOC_SETVERSION_OLD: {
332 struct ext4_iloc iloc;
336 if (!inode_owner_or_capable(inode))
339 if (ext4_has_metadata_csum(inode->i_sb)) {
340 ext4_warning(sb, "Setting inode version is not "
341 "supported with metadata_csum enabled.");
345 err = mnt_want_write_file(filp);
348 if (get_user(generation, (int __user *) arg)) {
353 mutex_lock(&inode->i_mutex);
354 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
355 if (IS_ERR(handle)) {
356 err = PTR_ERR(handle);
359 err = ext4_reserve_inode_write(handle, inode, &iloc);
361 inode->i_ctime = ext4_current_time(inode);
362 inode->i_generation = generation;
363 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
365 ext4_journal_stop(handle);
368 mutex_unlock(&inode->i_mutex);
370 mnt_drop_write_file(filp);
373 case EXT4_IOC_GROUP_EXTEND: {
374 ext4_fsblk_t n_blocks_count;
377 err = ext4_resize_begin(sb);
381 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
383 goto group_extend_out;
386 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
387 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
388 ext4_msg(sb, KERN_ERR,
389 "Online resizing not supported with bigalloc");
391 goto group_extend_out;
394 err = mnt_want_write_file(filp);
396 goto group_extend_out;
398 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
399 if (EXT4_SB(sb)->s_journal) {
400 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
401 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
402 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
406 mnt_drop_write_file(filp);
412 case EXT4_IOC_MOVE_EXT: {
413 struct move_extent me;
417 if (!(filp->f_mode & FMODE_READ) ||
418 !(filp->f_mode & FMODE_WRITE))
421 if (copy_from_user(&me,
422 (struct move_extent __user *)arg, sizeof(me)))
426 donor = fdget(me.donor_fd);
430 if (!(donor.file->f_mode & FMODE_WRITE)) {
435 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
436 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
437 ext4_msg(sb, KERN_ERR,
438 "Online defrag not supported with bigalloc");
443 err = mnt_want_write_file(filp);
447 err = ext4_move_extents(filp, donor.file, me.orig_start,
448 me.donor_start, me.len, &me.moved_len);
449 mnt_drop_write_file(filp);
451 if (copy_to_user((struct move_extent __user *)arg,
459 case EXT4_IOC_GROUP_ADD: {
460 struct ext4_new_group_data input;
463 err = ext4_resize_begin(sb);
467 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
473 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
474 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
475 ext4_msg(sb, KERN_ERR,
476 "Online resizing not supported with bigalloc");
481 err = mnt_want_write_file(filp);
485 err = ext4_group_add(sb, &input);
486 if (EXT4_SB(sb)->s_journal) {
487 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
488 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
489 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
493 mnt_drop_write_file(filp);
494 if (!err && ext4_has_group_desc_csum(sb) &&
495 test_opt(sb, INIT_INODE_TABLE))
496 err = ext4_register_li_request(sb, input.group);
502 case EXT4_IOC_MIGRATE:
505 if (!inode_owner_or_capable(inode))
508 err = mnt_want_write_file(filp);
512 * inode_mutex prevent write and truncate on the file.
513 * Read still goes through. We take i_data_sem in
514 * ext4_ext_swap_inode_data before we switch the
515 * inode format to prevent read.
517 mutex_lock(&(inode->i_mutex));
518 err = ext4_ext_migrate(inode);
519 mutex_unlock(&(inode->i_mutex));
520 mnt_drop_write_file(filp);
524 case EXT4_IOC_ALLOC_DA_BLKS:
527 if (!inode_owner_or_capable(inode))
530 err = mnt_want_write_file(filp);
533 err = ext4_alloc_da_blocks(inode);
534 mnt_drop_write_file(filp);
538 case EXT4_IOC_SWAP_BOOT:
541 if (!(filp->f_mode & FMODE_WRITE))
543 err = mnt_want_write_file(filp);
546 err = swap_inode_boot_loader(sb, inode);
547 mnt_drop_write_file(filp);
551 case EXT4_IOC_RESIZE_FS: {
552 ext4_fsblk_t n_blocks_count;
553 int err = 0, err2 = 0;
554 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
556 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
557 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
558 ext4_msg(sb, KERN_ERR,
559 "Online resizing not (yet) supported with bigalloc");
563 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
568 err = ext4_resize_begin(sb);
572 err = mnt_want_write_file(filp);
576 err = ext4_resize_fs(sb, n_blocks_count);
577 if (EXT4_SB(sb)->s_journal) {
578 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
579 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
580 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
584 mnt_drop_write_file(filp);
585 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
586 ext4_has_group_desc_csum(sb) &&
587 test_opt(sb, INIT_INODE_TABLE))
588 err = ext4_register_li_request(sb, o_group);
597 struct request_queue *q = bdev_get_queue(sb->s_bdev);
598 struct fstrim_range range;
601 if (!capable(CAP_SYS_ADMIN))
604 if (!blk_queue_discard(q))
607 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
611 range.minlen = max((unsigned int)range.minlen,
612 q->limits.discard_granularity);
613 ret = ext4_trim_fs(sb, &range);
617 if (copy_to_user((struct fstrim_range __user *)arg, &range,
623 case EXT4_IOC_PRECACHE_EXTENTS:
624 return ext4_ext_precache(inode);
625 case EXT4_IOC_SET_ENCRYPTION_POLICY: {
626 #ifdef CONFIG_EXT4_FS_ENCRYPTION
627 struct ext4_encryption_policy policy;
630 if (copy_from_user(&policy,
631 (struct ext4_encryption_policy __user *)arg,
634 goto encryption_policy_out;
637 err = ext4_process_policy(&policy, inode);
638 encryption_policy_out:
644 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
646 struct ext4_sb_info *sbi = EXT4_SB(sb);
649 if (!ext4_sb_has_crypto(sb))
651 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
652 err = mnt_want_write_file(filp);
655 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
656 if (IS_ERR(handle)) {
657 err = PTR_ERR(handle);
658 goto pwsalt_err_exit;
660 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
662 goto pwsalt_err_journal;
663 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
664 err = ext4_handle_dirty_metadata(handle, NULL,
667 err2 = ext4_journal_stop(handle);
671 mnt_drop_write_file(filp);
675 if (copy_to_user((void __user *) arg,
676 sbi->s_es->s_encrypt_pw_salt, 16))
680 case EXT4_IOC_GET_ENCRYPTION_POLICY: {
681 #ifdef CONFIG_EXT4_FS_ENCRYPTION
682 struct ext4_encryption_policy policy;
685 if (!ext4_encrypted_inode(inode))
687 err = ext4_get_policy(inode, &policy);
690 if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
703 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
705 /* These are just misnamed, they actually get/put from/to user an int */
707 case EXT4_IOC32_GETFLAGS:
708 cmd = EXT4_IOC_GETFLAGS;
710 case EXT4_IOC32_SETFLAGS:
711 cmd = EXT4_IOC_SETFLAGS;
713 case EXT4_IOC32_GETVERSION:
714 cmd = EXT4_IOC_GETVERSION;
716 case EXT4_IOC32_SETVERSION:
717 cmd = EXT4_IOC_SETVERSION;
719 case EXT4_IOC32_GROUP_EXTEND:
720 cmd = EXT4_IOC_GROUP_EXTEND;
722 case EXT4_IOC32_GETVERSION_OLD:
723 cmd = EXT4_IOC_GETVERSION_OLD;
725 case EXT4_IOC32_SETVERSION_OLD:
726 cmd = EXT4_IOC_SETVERSION_OLD;
728 case EXT4_IOC32_GETRSVSZ:
729 cmd = EXT4_IOC_GETRSVSZ;
731 case EXT4_IOC32_SETRSVSZ:
732 cmd = EXT4_IOC_SETRSVSZ;
734 case EXT4_IOC32_GROUP_ADD: {
735 struct compat_ext4_new_group_input __user *uinput;
736 struct ext4_new_group_input input;
740 uinput = compat_ptr(arg);
741 err = get_user(input.group, &uinput->group);
742 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
743 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
744 err |= get_user(input.inode_table, &uinput->inode_table);
745 err |= get_user(input.blocks_count, &uinput->blocks_count);
746 err |= get_user(input.reserved_blocks,
747 &uinput->reserved_blocks);
752 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
753 (unsigned long) &input);
757 case EXT4_IOC_MOVE_EXT:
758 case EXT4_IOC_RESIZE_FS:
759 case EXT4_IOC_PRECACHE_EXTENTS:
760 case EXT4_IOC_SET_ENCRYPTION_POLICY:
761 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
762 case EXT4_IOC_GET_ENCRYPTION_POLICY:
767 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));