Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/ioctl.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 11 | #include <linux/capability.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 12 | #include <linux/time.h> |
| 13 | #include <linux/compat.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 14 | #include <linux/mount.h> |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 15 | #include <linux/file.h> |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 16 | #include <linux/random.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 17 | #include <asm/uaccess.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 18 | #include "ext4_jbd2.h" |
| 19 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 20 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 21 | #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1) |
| 22 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 23 | /** |
| 24 | * Swap memory between @a and @b for @len bytes. |
| 25 | * |
| 26 | * @a: pointer to first memory area |
| 27 | * @b: pointer to second memory area |
| 28 | * @len: number of bytes to swap |
| 29 | * |
| 30 | */ |
| 31 | static void memswap(void *a, void *b, size_t len) |
| 32 | { |
| 33 | unsigned char *ap, *bp; |
| 34 | unsigned char tmp; |
| 35 | |
| 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++; |
| 44 | } |
| 45 | } |
| 46 | |
| 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. |
| 51 | * |
| 52 | * Therefore you have to make sure, that calling this method twice |
| 53 | * will revert all changes. |
| 54 | * |
| 55 | * @inode1: pointer to first inode |
| 56 | * @inode2: pointer to second inode |
| 57 | */ |
| 58 | static void swap_inode_data(struct inode *inode1, struct inode *inode2) |
| 59 | { |
| 60 | loff_t isize; |
| 61 | struct ext4_inode_info *ei1; |
| 62 | struct ext4_inode_info *ei2; |
| 63 | |
| 64 | ei1 = EXT4_I(inode1); |
| 65 | ei2 = EXT4_I(inode2); |
| 66 | |
| 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)); |
| 75 | |
| 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)); |
Theodore Ts'o | cde2d7a | 2013-08-12 09:29:30 -0400 | [diff] [blame] | 79 | ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS); |
| 80 | ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 81 | |
| 82 | isize = i_size_read(inode1); |
| 83 | i_size_write(inode1, i_size_read(inode2)); |
| 84 | i_size_write(inode2, isize); |
| 85 | } |
| 86 | |
| 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. |
| 91 | * |
| 92 | * @sb: the super block of the filesystem |
| 93 | * @inode: the inode to swap with EXT4_BOOT_LOADER_INO |
| 94 | * |
| 95 | */ |
| 96 | static long swap_inode_boot_loader(struct super_block *sb, |
| 97 | struct inode *inode) |
| 98 | { |
| 99 | handle_t *handle; |
| 100 | int err; |
| 101 | struct inode *inode_bl; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 102 | struct ext4_inode_info *ei_bl; |
jon ernst | d7092ae | 2014-01-11 13:26:56 -0500 | [diff] [blame] | 103 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 104 | |
Theodore Ts'o | d8558a2 | 2014-02-17 20:44:36 -0500 | [diff] [blame] | 105 | if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode)) |
| 106 | return -EINVAL; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 107 | |
Theodore Ts'o | d8558a2 | 2014-02-17 20:44:36 -0500 | [diff] [blame] | 108 | if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) |
| 109 | return -EPERM; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 110 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 111 | inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO); |
Theodore Ts'o | d8558a2 | 2014-02-17 20:44:36 -0500 | [diff] [blame] | 112 | if (IS_ERR(inode_bl)) |
| 113 | return PTR_ERR(inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 114 | ei_bl = EXT4_I(inode_bl); |
| 115 | |
| 116 | filemap_flush(inode->i_mapping); |
| 117 | filemap_flush(inode_bl->i_mapping); |
| 118 | |
| 119 | /* Protect orig inodes against a truncate and make sure, |
| 120 | * that only 1 swap_inode_boot_loader is running. */ |
J. Bruce Fields | 375e289 | 2012-04-18 15:16:33 -0400 | [diff] [blame] | 121 | lock_two_nondirectories(inode, inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 122 | |
| 123 | truncate_inode_pages(&inode->i_data, 0); |
| 124 | truncate_inode_pages(&inode_bl->i_data, 0); |
| 125 | |
| 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); |
| 131 | |
| 132 | handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); |
| 133 | if (IS_ERR(handle)) { |
| 134 | err = -EINVAL; |
Zheng Liu | 30d29b1 | 2014-02-12 11:48:31 -0500 | [diff] [blame] | 135 | goto journal_err_out; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Protect extent tree against block allocations via delalloc */ |
| 139 | ext4_double_down_write_data_sem(inode, inode_bl); |
| 140 | |
| 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)); |
| 157 | } |
| 158 | |
| 159 | swap_inode_data(inode, inode_bl); |
| 160 | |
| 161 | inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode); |
| 162 | |
| 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); |
| 167 | |
| 168 | ext4_discard_preallocations(inode); |
| 169 | |
| 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); |
| 186 | } |
| 187 | } |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 188 | ext4_journal_stop(handle); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 189 | ext4_double_up_write_data_sem(inode, inode_bl); |
| 190 | |
Zheng Liu | 30d29b1 | 2014-02-12 11:48:31 -0500 | [diff] [blame] | 191 | journal_err_out: |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 192 | ext4_inode_resume_unlocked_dio(inode); |
| 193 | ext4_inode_resume_unlocked_dio(inode_bl); |
J. Bruce Fields | 375e289 | 2012-04-18 15:16:33 -0400 | [diff] [blame] | 194 | unlock_two_nondirectories(inode, inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 195 | iput(inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 196 | return err; |
| 197 | } |
| 198 | |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 199 | static int uuid_is_zero(__u8 u[16]) |
| 200 | { |
| 201 | int i; |
| 202 | |
| 203 | for (i = 0; i < 16; i++) |
| 204 | if (u[i]) |
| 205 | return 0; |
| 206 | return 1; |
| 207 | } |
| 208 | |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 209 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 210 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 211 | struct inode *inode = file_inode(filp); |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 212 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 213 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 214 | unsigned int flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 215 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 216 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 217 | |
| 218 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 219 | case EXT4_IOC_GETFLAGS: |
Jan Kara | ff9ddf7 | 2007-07-18 09:24:20 -0400 | [diff] [blame] | 220 | ext4_get_inode_flags(ei); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 221 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 222 | return put_user(flags, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 223 | case EXT4_IOC_SETFLAGS: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 224 | handle_t *handle = NULL; |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 225 | int err, migrate = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 226 | struct ext4_iloc iloc; |
Theodore Ts'o | 7990696 | 2012-05-31 23:46:01 -0400 | [diff] [blame] | 227 | unsigned int oldflags, mask, i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 228 | unsigned int jflag; |
| 229 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 230 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 231 | return -EACCES; |
| 232 | |
| 233 | if (get_user(flags, (int __user *) arg)) |
| 234 | return -EFAULT; |
| 235 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 236 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 237 | if (err) |
| 238 | return err; |
| 239 | |
Duane Griffin | 2dc6b0d | 2009-02-15 18:09:20 -0500 | [diff] [blame] | 240 | flags = ext4_mask_flags(inode->i_mode, flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 241 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 242 | err = -EPERM; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 243 | mutex_lock(&inode->i_mutex); |
Jan Kara | e47776a | 2007-11-14 16:58:56 -0800 | [diff] [blame] | 244 | /* Is it quota file? Do not allow user to mess with it */ |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 245 | if (IS_NOQUOTA(inode)) |
| 246 | goto flags_out; |
| 247 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 248 | oldflags = ei->i_flags; |
| 249 | |
| 250 | /* The JOURNAL_DATA flag is modifiable only by root */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 251 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 255 | * the relevant capability. |
| 256 | * |
| 257 | * This test looks nicer. Thanks to Pauline Middelink |
| 258 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 259 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 260 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 261 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
| 265 | * The JOURNAL_DATA flag can only be changed by |
| 266 | * the relevant capability. |
| 267 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 268 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 269 | if (!capable(CAP_SYS_RESOURCE)) |
| 270 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 271 | } |
Theodore Ts'o | 996bb9f | 2013-04-03 22:04:52 -0400 | [diff] [blame] | 272 | if ((flags ^ oldflags) & EXT4_EXTENTS_FL) |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 273 | migrate = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 274 | |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 275 | if (flags & EXT4_EOFBLOCKS_FL) { |
| 276 | /* we don't support adding EOFBLOCKS flag */ |
| 277 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { |
| 278 | err = -EOPNOTSUPP; |
| 279 | goto flags_out; |
| 280 | } |
| 281 | } else if (oldflags & EXT4_EOFBLOCKS_FL) |
| 282 | ext4_truncate(inode); |
| 283 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 284 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 285 | if (IS_ERR(handle)) { |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 286 | err = PTR_ERR(handle); |
| 287 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 288 | } |
| 289 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 290 | ext4_handle_sync(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 291 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 292 | if (err) |
| 293 | goto flags_err; |
| 294 | |
Theodore Ts'o | 7990696 | 2012-05-31 23:46:01 -0400 | [diff] [blame] | 295 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
| 296 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) |
| 297 | continue; |
| 298 | if (mask & flags) |
| 299 | ext4_set_inode_flag(inode, i); |
| 300 | else |
| 301 | ext4_clear_inode_flag(inode, i); |
| 302 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 303 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 304 | ext4_set_inode_flags(inode); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 305 | inode->i_ctime = ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 306 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 307 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 308 | flags_err: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 309 | ext4_journal_stop(handle); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 310 | if (err) |
| 311 | goto flags_out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 312 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 313 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
| 314 | err = ext4_change_inode_journal_flag(inode, jflag); |
Aneesh Kumar K.V | 4db46fc | 2008-10-08 23:34:06 -0400 | [diff] [blame] | 315 | if (err) |
| 316 | goto flags_out; |
Theodore Ts'o | 996bb9f | 2013-04-03 22:04:52 -0400 | [diff] [blame] | 317 | if (migrate) { |
| 318 | if (flags & EXT4_EXTENTS_FL) |
| 319 | err = ext4_ext_migrate(inode); |
| 320 | else |
| 321 | err = ext4_ind_migrate(inode); |
| 322 | } |
| 323 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 324 | flags_out: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 325 | mutex_unlock(&inode->i_mutex); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 326 | mnt_drop_write_file(filp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 327 | return err; |
| 328 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 329 | case EXT4_IOC_GETVERSION: |
| 330 | case EXT4_IOC_GETVERSION_OLD: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 331 | return put_user(inode->i_generation, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 332 | case EXT4_IOC_SETVERSION: |
| 333 | case EXT4_IOC_SETVERSION_OLD: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 334 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 335 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 336 | __u32 generation; |
| 337 | int err; |
| 338 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 339 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 340 | return -EPERM; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 341 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 342 | if (ext4_has_metadata_csum(inode->i_sb)) { |
Darrick J. Wong | 814525f | 2012-04-29 18:31:10 -0400 | [diff] [blame] | 343 | ext4_warning(sb, "Setting inode version is not " |
| 344 | "supported with metadata_csum enabled."); |
| 345 | return -ENOTTY; |
| 346 | } |
| 347 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 348 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 349 | if (err) |
| 350 | return err; |
| 351 | if (get_user(generation, (int __user *) arg)) { |
| 352 | err = -EFAULT; |
| 353 | goto setversion_out; |
| 354 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 355 | |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 356 | mutex_lock(&inode->i_mutex); |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 357 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 358 | if (IS_ERR(handle)) { |
| 359 | err = PTR_ERR(handle); |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 360 | goto unlock_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 361 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 362 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 363 | if (err == 0) { |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 364 | inode->i_ctime = ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 365 | inode->i_generation = generation; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 366 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 367 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 368 | ext4_journal_stop(handle); |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 369 | |
| 370 | unlock_out: |
| 371 | mutex_unlock(&inode->i_mutex); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 372 | setversion_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 373 | mnt_drop_write_file(filp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 374 | return err; |
| 375 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 376 | case EXT4_IOC_GROUP_EXTEND: { |
| 377 | ext4_fsblk_t n_blocks_count; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 378 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 379 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 380 | err = ext4_resize_begin(sb); |
| 381 | if (err) |
| 382 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 383 | |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 384 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
| 385 | err = -EFAULT; |
| 386 | goto group_extend_out; |
| 387 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 388 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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"); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 393 | err = -EOPNOTSUPP; |
| 394 | goto group_extend_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 397 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 398 | if (err) |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 399 | goto group_extend_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 400 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 401 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 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); |
| 406 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 407 | if (err == 0) |
| 408 | err = err2; |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 409 | mnt_drop_write_file(filp); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 410 | group_extend_out: |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 411 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 412 | return err; |
| 413 | } |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 414 | |
| 415 | case EXT4_IOC_MOVE_EXT: { |
| 416 | struct move_extent me; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 417 | struct fd donor; |
| 418 | int err; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 419 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 420 | if (!(filp->f_mode & FMODE_READ) || |
| 421 | !(filp->f_mode & FMODE_WRITE)) |
| 422 | return -EBADF; |
| 423 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 424 | if (copy_from_user(&me, |
| 425 | (struct move_extent __user *)arg, sizeof(me))) |
| 426 | return -EFAULT; |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 427 | me.moved_len = 0; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 428 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 429 | donor = fdget(me.donor_fd); |
| 430 | if (!donor.file) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 431 | return -EBADF; |
| 432 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 433 | if (!(donor.file->f_mode & FMODE_WRITE)) { |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 434 | err = -EBADF; |
| 435 | goto mext_out; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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"); |
Al Viro | 399c9b8 | 2012-08-26 21:00:03 -0400 | [diff] [blame] | 442 | err = -EOPNOTSUPP; |
| 443 | goto mext_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 446 | err = mnt_want_write_file(filp); |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 447 | if (err) |
| 448 | goto mext_out; |
| 449 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 450 | err = ext4_move_extents(filp, donor.file, me.orig_start, |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 451 | me.donor_start, me.len, &me.moved_len); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 452 | mnt_drop_write_file(filp); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 453 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 454 | if (copy_to_user((struct move_extent __user *)arg, |
Akira Fujita | c437b27 | 2010-03-04 00:39:24 -0500 | [diff] [blame] | 455 | &me, sizeof(me))) |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 456 | err = -EFAULT; |
| 457 | mext_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 458 | fdput(donor); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 459 | return err; |
| 460 | } |
| 461 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 462 | case EXT4_IOC_GROUP_ADD: { |
| 463 | struct ext4_new_group_data input; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 464 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 465 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 466 | err = ext4_resize_begin(sb); |
| 467 | if (err) |
| 468 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 469 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 470 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 471 | sizeof(input))) { |
| 472 | err = -EFAULT; |
| 473 | goto group_add_out; |
| 474 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 475 | |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 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"); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 480 | err = -EOPNOTSUPP; |
| 481 | goto group_add_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 484 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 485 | if (err) |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 486 | goto group_add_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 487 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 488 | err = ext4_group_add(sb, &input); |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 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); |
| 493 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 494 | if (err == 0) |
| 495 | err = err2; |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 496 | mnt_drop_write_file(filp); |
Theodore Ts'o | 7f51186 | 2013-01-13 08:41:45 -0500 | [diff] [blame] | 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); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 500 | group_add_out: |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 501 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 502 | return err; |
| 503 | } |
| 504 | |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 505 | case EXT4_IOC_MIGRATE: |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 506 | { |
| 507 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 508 | if (!inode_owner_or_capable(inode)) |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 509 | return -EACCES; |
| 510 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 511 | err = mnt_want_write_file(filp); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 512 | if (err) |
| 513 | return err; |
| 514 | /* |
| 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. |
| 519 | */ |
| 520 | mutex_lock(&(inode->i_mutex)); |
| 521 | err = ext4_ext_migrate(inode); |
| 522 | mutex_unlock(&(inode->i_mutex)); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 523 | mnt_drop_write_file(filp); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 524 | return err; |
| 525 | } |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 526 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 527 | case EXT4_IOC_ALLOC_DA_BLKS: |
| 528 | { |
| 529 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 530 | if (!inode_owner_or_capable(inode)) |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 531 | return -EACCES; |
| 532 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 533 | err = mnt_want_write_file(filp); |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 534 | if (err) |
| 535 | return err; |
| 536 | err = ext4_alloc_da_blocks(inode); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 537 | mnt_drop_write_file(filp); |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 538 | return err; |
| 539 | } |
| 540 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 541 | case EXT4_IOC_SWAP_BOOT: |
Dmitry Monakhov | 3e67cfad2 | 2014-10-03 12:47:23 -0400 | [diff] [blame] | 542 | { |
| 543 | int err; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 544 | if (!(filp->f_mode & FMODE_WRITE)) |
| 545 | return -EBADF; |
Dmitry Monakhov | 3e67cfad2 | 2014-10-03 12:47:23 -0400 | [diff] [blame] | 546 | err = mnt_want_write_file(filp); |
| 547 | if (err) |
| 548 | return err; |
| 549 | err = swap_inode_boot_loader(sb, inode); |
| 550 | mnt_drop_write_file(filp); |
| 551 | return err; |
| 552 | } |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 553 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 554 | case EXT4_IOC_RESIZE_FS: { |
| 555 | ext4_fsblk_t n_blocks_count; |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 556 | int err = 0, err2 = 0; |
Theodore Ts'o | 7f51186 | 2013-01-13 08:41:45 -0500 | [diff] [blame] | 557 | ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 558 | |
| 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"); |
| 563 | return -EOPNOTSUPP; |
| 564 | } |
| 565 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 566 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
| 567 | sizeof(__u64))) { |
| 568 | return -EFAULT; |
| 569 | } |
| 570 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 571 | err = ext4_resize_begin(sb); |
| 572 | if (err) |
| 573 | return err; |
| 574 | |
Al Viro | 8cae6f7 | 2012-07-19 11:19:07 +0400 | [diff] [blame] | 575 | err = mnt_want_write_file(filp); |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 576 | if (err) |
| 577 | goto resizefs_out; |
| 578 | |
| 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); |
| 584 | } |
| 585 | if (err == 0) |
| 586 | err = err2; |
Al Viro | 8cae6f7 | 2012-07-19 11:19:07 +0400 | [diff] [blame] | 587 | mnt_drop_write_file(filp); |
Theodore Ts'o | 7f51186 | 2013-01-13 08:41:45 -0500 | [diff] [blame] | 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); |
| 592 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 593 | resizefs_out: |
| 594 | ext4_resize_end(sb); |
| 595 | return err; |
| 596 | } |
| 597 | |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 598 | case FITRIM: |
| 599 | { |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 600 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 601 | struct fstrim_range range; |
| 602 | int ret = 0; |
| 603 | |
| 604 | if (!capable(CAP_SYS_ADMIN)) |
| 605 | return -EPERM; |
| 606 | |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 607 | if (!blk_queue_discard(q)) |
| 608 | return -EOPNOTSUPP; |
| 609 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 610 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 611 | sizeof(range))) |
| 612 | return -EFAULT; |
| 613 | |
Lukas Czerner | 5c2ed62 | 2011-02-23 17:49:51 -0500 | [diff] [blame] | 614 | range.minlen = max((unsigned int)range.minlen, |
| 615 | q->limits.discard_granularity); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 616 | ret = ext4_trim_fs(sb, &range); |
| 617 | if (ret < 0) |
| 618 | return ret; |
| 619 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 620 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 621 | sizeof(range))) |
| 622 | return -EFAULT; |
| 623 | |
| 624 | return 0; |
| 625 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 626 | case EXT4_IOC_PRECACHE_EXTENTS: |
| 627 | return ext4_ext_precache(inode); |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 628 | case EXT4_IOC_SET_ENCRYPTION_POLICY: { |
| 629 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
| 630 | struct ext4_encryption_policy policy; |
| 631 | int err = 0; |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 632 | |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 633 | if (copy_from_user(&policy, |
| 634 | (struct ext4_encryption_policy __user *)arg, |
| 635 | sizeof(policy))) { |
| 636 | err = -EFAULT; |
| 637 | goto encryption_policy_out; |
| 638 | } |
| 639 | |
| 640 | err = ext4_process_policy(&policy, inode); |
| 641 | encryption_policy_out: |
| 642 | return err; |
| 643 | #else |
| 644 | return -EOPNOTSUPP; |
| 645 | #endif |
| 646 | } |
| 647 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: { |
| 648 | int err, err2; |
| 649 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 650 | handle_t *handle; |
| 651 | |
| 652 | if (!ext4_sb_has_crypto(sb)) |
| 653 | return -EOPNOTSUPP; |
| 654 | if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { |
| 655 | err = mnt_want_write_file(filp); |
| 656 | if (err) |
| 657 | return err; |
| 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; |
| 662 | } |
| 663 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); |
| 664 | if (err) |
| 665 | goto pwsalt_err_journal; |
| 666 | generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); |
| 667 | err = ext4_handle_dirty_metadata(handle, NULL, |
| 668 | sbi->s_sbh); |
| 669 | pwsalt_err_journal: |
| 670 | err2 = ext4_journal_stop(handle); |
| 671 | if (err2 && !err) |
| 672 | err = err2; |
| 673 | pwsalt_err_exit: |
| 674 | mnt_drop_write_file(filp); |
| 675 | if (err) |
| 676 | return err; |
| 677 | } |
Fabian Frederick | b4ab9e2 | 2015-06-08 12:23:21 -0400 | [diff] [blame^] | 678 | if (copy_to_user((void __user *) arg, |
| 679 | sbi->s_es->s_encrypt_pw_salt, 16)) |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 680 | return -EFAULT; |
| 681 | return 0; |
| 682 | } |
| 683 | case EXT4_IOC_GET_ENCRYPTION_POLICY: { |
| 684 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
| 685 | struct ext4_encryption_policy policy; |
| 686 | int err = 0; |
| 687 | |
| 688 | if (!ext4_encrypted_inode(inode)) |
| 689 | return -ENOENT; |
| 690 | err = ext4_get_policy(inode, &policy); |
| 691 | if (err) |
| 692 | return err; |
Fabian Frederick | b4ab9e2 | 2015-06-08 12:23:21 -0400 | [diff] [blame^] | 693 | if (copy_to_user((void __user *)arg, &policy, sizeof(policy))) |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 694 | return -EFAULT; |
| 695 | return 0; |
| 696 | #else |
| 697 | return -EOPNOTSUPP; |
| 698 | #endif |
| 699 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 700 | default: |
| 701 | return -ENOTTY; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | #ifdef CONFIG_COMPAT |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 706 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 707 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 708 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 709 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 710 | case EXT4_IOC32_GETFLAGS: |
| 711 | cmd = EXT4_IOC_GETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 712 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 713 | case EXT4_IOC32_SETFLAGS: |
| 714 | cmd = EXT4_IOC_SETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 715 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 716 | case EXT4_IOC32_GETVERSION: |
| 717 | cmd = EXT4_IOC_GETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 718 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 719 | case EXT4_IOC32_SETVERSION: |
| 720 | cmd = EXT4_IOC_SETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 721 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 722 | case EXT4_IOC32_GROUP_EXTEND: |
| 723 | cmd = EXT4_IOC_GROUP_EXTEND; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 724 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 725 | case EXT4_IOC32_GETVERSION_OLD: |
| 726 | cmd = EXT4_IOC_GETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 727 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 728 | case EXT4_IOC32_SETVERSION_OLD: |
| 729 | cmd = EXT4_IOC_SETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 730 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 731 | case EXT4_IOC32_GETRSVSZ: |
| 732 | cmd = EXT4_IOC_GETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 733 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 734 | case EXT4_IOC32_SETRSVSZ: |
| 735 | cmd = EXT4_IOC_SETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 736 | break; |
Ben Hutchings | 4d92dc0 | 2010-05-17 06:00:00 -0400 | [diff] [blame] | 737 | case EXT4_IOC32_GROUP_ADD: { |
| 738 | struct compat_ext4_new_group_input __user *uinput; |
| 739 | struct ext4_new_group_input input; |
| 740 | mm_segment_t old_fs; |
| 741 | int err; |
| 742 | |
| 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); |
| 751 | if (err) |
| 752 | return -EFAULT; |
| 753 | old_fs = get_fs(); |
| 754 | set_fs(KERNEL_DS); |
| 755 | err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD, |
| 756 | (unsigned long) &input); |
| 757 | set_fs(old_fs); |
| 758 | return err; |
| 759 | } |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 760 | case EXT4_IOC_MOVE_EXT: |
Tao Ma | a56e69c | 2011-03-20 23:16:58 -0400 | [diff] [blame] | 761 | case FITRIM: |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 762 | case EXT4_IOC_RESIZE_FS: |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 763 | case EXT4_IOC_PRECACHE_EXTENTS: |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 764 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
| 765 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: |
| 766 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 767 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 768 | default: |
| 769 | return -ENOIOCTLCMD; |
| 770 | } |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 771 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 772 | } |
| 773 | #endif |