Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3 | * linux/fs/ext4/ioctl.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1993, 1994, 1995 |
| 6 | * Remy Card (card@masi.ibp.fr) |
| 7 | * Laboratoire MASI - Institut Blaise Pascal |
| 8 | * Universite Pierre et Marie Curie (Paris VI) |
| 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 12 | #include <linux/capability.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 13 | #include <linux/time.h> |
| 14 | #include <linux/compat.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 15 | #include <linux/mount.h> |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 16 | #include <linux/file.h> |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 17 | #include <linux/quotaops.h> |
Theodore Ts'o | 2325306 | 2017-11-08 22:23:20 -0500 | [diff] [blame] | 18 | #include <linux/random.h> |
Andy Shevchenko | 8da4b8c | 2016-05-20 17:01:00 -0700 | [diff] [blame] | 19 | #include <linux/uuid.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Theodore Ts'o | 783d948 | 2017-02-05 19:47:14 -0500 | [diff] [blame] | 21 | #include <linux/delay.h> |
Jeff Layton | ee73f9a | 2018-01-09 08:21:39 -0500 | [diff] [blame] | 22 | #include <linux/iversion.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 23 | #include "ext4_jbd2.h" |
| 24 | #include "ext4.h" |
Darrick J. Wong | 0c9ec4b | 2017-04-30 00:36:53 -0400 | [diff] [blame] | 25 | #include <linux/fsmap.h> |
| 26 | #include "fsmap.h" |
| 27 | #include <trace/events/ext4.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 28 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 29 | /** |
| 30 | * Swap memory between @a and @b for @len bytes. |
| 31 | * |
| 32 | * @a: pointer to first memory area |
| 33 | * @b: pointer to second memory area |
| 34 | * @len: number of bytes to swap |
| 35 | * |
| 36 | */ |
| 37 | static void memswap(void *a, void *b, size_t len) |
| 38 | { |
| 39 | unsigned char *ap, *bp; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 40 | |
| 41 | ap = (unsigned char *)a; |
| 42 | bp = (unsigned char *)b; |
| 43 | while (len-- > 0) { |
Fabian Frederick | 4b7e2db | 2015-06-12 23:46:33 -0400 | [diff] [blame] | 44 | swap(*ap, *bp); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 45 | ap++; |
| 46 | bp++; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Swap i_data and associated attributes between @inode1 and @inode2. |
| 52 | * This function is used for the primary swap between inode1 and inode2 |
| 53 | * and also to revert this primary swap in case of errors. |
| 54 | * |
| 55 | * Therefore you have to make sure, that calling this method twice |
| 56 | * will revert all changes. |
| 57 | * |
| 58 | * @inode1: pointer to first inode |
| 59 | * @inode2: pointer to second inode |
| 60 | */ |
| 61 | static void swap_inode_data(struct inode *inode1, struct inode *inode2) |
| 62 | { |
| 63 | loff_t isize; |
| 64 | struct ext4_inode_info *ei1; |
| 65 | struct ext4_inode_info *ei2; |
| 66 | |
| 67 | ei1 = EXT4_I(inode1); |
| 68 | ei2 = EXT4_I(inode2); |
| 69 | |
Jeff Layton | 9c5d58f | 2017-07-31 00:55:34 -0400 | [diff] [blame] | 70 | swap(inode1->i_version, inode2->i_version); |
| 71 | swap(inode1->i_blocks, inode2->i_blocks); |
| 72 | swap(inode1->i_bytes, inode2->i_bytes); |
| 73 | swap(inode1->i_atime, inode2->i_atime); |
| 74 | swap(inode1->i_mtime, inode2->i_mtime); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 75 | |
| 76 | memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data)); |
Jeff Layton | 9c5d58f | 2017-07-31 00:55:34 -0400 | [diff] [blame] | 77 | swap(ei1->i_flags, ei2->i_flags); |
| 78 | swap(ei1->i_disksize, ei2->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 | |
Theodore Ts'o | 18aded1 | 2018-10-02 18:21:19 -0400 | [diff] [blame] | 87 | static void reset_inode_seed(struct inode *inode) |
| 88 | { |
| 89 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 90 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 91 | __le32 inum = cpu_to_le32(inode->i_ino); |
| 92 | __le32 gen = cpu_to_le32(inode->i_generation); |
| 93 | __u32 csum; |
| 94 | |
| 95 | if (!ext4_has_metadata_csum(inode->i_sb)) |
| 96 | return; |
| 97 | |
| 98 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum)); |
| 99 | ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen)); |
| 100 | } |
| 101 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Swap the information from the given @inode and the inode |
| 104 | * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other |
| 105 | * important fields of the inodes. |
| 106 | * |
| 107 | * @sb: the super block of the filesystem |
| 108 | * @inode: the inode to swap with EXT4_BOOT_LOADER_INO |
| 109 | * |
| 110 | */ |
| 111 | static long swap_inode_boot_loader(struct super_block *sb, |
| 112 | struct inode *inode) |
| 113 | { |
| 114 | handle_t *handle; |
| 115 | int err; |
| 116 | struct inode *inode_bl; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 117 | struct ext4_inode_info *ei_bl; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 118 | |
Theodore Ts'o | 8a36397 | 2018-12-19 12:29:13 -0500 | [diff] [blame] | 119 | inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL); |
Theodore Ts'o | d8558a2 | 2014-02-17 20:44:36 -0500 | [diff] [blame] | 120 | if (IS_ERR(inode_bl)) |
| 121 | return PTR_ERR(inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 122 | ei_bl = EXT4_I(inode_bl); |
| 123 | |
| 124 | filemap_flush(inode->i_mapping); |
| 125 | filemap_flush(inode_bl->i_mapping); |
| 126 | |
| 127 | /* Protect orig inodes against a truncate and make sure, |
| 128 | * that only 1 swap_inode_boot_loader is running. */ |
J. Bruce Fields | 375e289 | 2012-04-18 15:16:33 -0400 | [diff] [blame] | 129 | lock_two_nondirectories(inode, inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 130 | |
yangerkun | 67a1161 | 2019-02-11 00:02:05 -0500 | [diff] [blame^] | 131 | if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) || |
| 132 | IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) || |
| 133 | ext4_has_inline_data(inode)) { |
| 134 | err = -EINVAL; |
| 135 | goto journal_err_out; |
| 136 | } |
| 137 | |
| 138 | if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) || |
| 139 | !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) { |
| 140 | err = -EPERM; |
| 141 | goto journal_err_out; |
| 142 | } |
| 143 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 144 | /* Wait for all existing dio workers */ |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 145 | inode_dio_wait(inode); |
| 146 | inode_dio_wait(inode_bl); |
| 147 | |
Theodore Ts'o | 18aded1 | 2018-10-02 18:21:19 -0400 | [diff] [blame] | 148 | truncate_inode_pages(&inode->i_data, 0); |
| 149 | truncate_inode_pages(&inode_bl->i_data, 0); |
| 150 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 151 | handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); |
| 152 | if (IS_ERR(handle)) { |
| 153 | err = -EINVAL; |
Zheng Liu | 30d29b1 | 2014-02-12 11:48:31 -0500 | [diff] [blame] | 154 | goto journal_err_out; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Protect extent tree against block allocations via delalloc */ |
| 158 | ext4_double_down_write_data_sem(inode, inode_bl); |
| 159 | |
| 160 | if (inode_bl->i_nlink == 0) { |
| 161 | /* this inode has never been used as a BOOT_LOADER */ |
| 162 | set_nlink(inode_bl, 1); |
| 163 | i_uid_write(inode_bl, 0); |
| 164 | i_gid_write(inode_bl, 0); |
| 165 | inode_bl->i_flags = 0; |
| 166 | ei_bl->i_flags = 0; |
Jeff Layton | ee73f9a | 2018-01-09 08:21:39 -0500 | [diff] [blame] | 167 | inode_set_iversion(inode_bl, 1); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 168 | i_size_write(inode_bl, 0); |
| 169 | inode_bl->i_mode = S_IFREG; |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 170 | if (ext4_has_feature_extents(sb)) { |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 171 | ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS); |
| 172 | ext4_ext_tree_init(handle, inode_bl); |
| 173 | } else |
| 174 | memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data)); |
| 175 | } |
| 176 | |
| 177 | swap_inode_data(inode, inode_bl); |
| 178 | |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 179 | inode->i_ctime = inode_bl->i_ctime = current_time(inode); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 180 | |
Theodore Ts'o | 2325306 | 2017-11-08 22:23:20 -0500 | [diff] [blame] | 181 | inode->i_generation = prandom_u32(); |
| 182 | inode_bl->i_generation = prandom_u32(); |
Theodore Ts'o | 18aded1 | 2018-10-02 18:21:19 -0400 | [diff] [blame] | 183 | reset_inode_seed(inode); |
| 184 | reset_inode_seed(inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 185 | |
| 186 | ext4_discard_preallocations(inode); |
| 187 | |
| 188 | err = ext4_mark_inode_dirty(handle, inode); |
| 189 | if (err < 0) { |
| 190 | ext4_warning(inode->i_sb, |
| 191 | "couldn't mark inode #%lu dirty (err %d)", |
| 192 | inode->i_ino, err); |
| 193 | /* Revert all changes: */ |
| 194 | swap_inode_data(inode, inode_bl); |
Theodore Ts'o | 18aded1 | 2018-10-02 18:21:19 -0400 | [diff] [blame] | 195 | ext4_mark_inode_dirty(handle, inode); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 196 | } else { |
| 197 | err = ext4_mark_inode_dirty(handle, inode_bl); |
| 198 | if (err < 0) { |
| 199 | ext4_warning(inode_bl->i_sb, |
| 200 | "couldn't mark inode #%lu dirty (err %d)", |
| 201 | inode_bl->i_ino, err); |
| 202 | /* Revert all changes: */ |
| 203 | swap_inode_data(inode, inode_bl); |
| 204 | ext4_mark_inode_dirty(handle, inode); |
Theodore Ts'o | 18aded1 | 2018-10-02 18:21:19 -0400 | [diff] [blame] | 205 | ext4_mark_inode_dirty(handle, inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 208 | ext4_journal_stop(handle); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 209 | ext4_double_up_write_data_sem(inode, inode_bl); |
| 210 | |
Zheng Liu | 30d29b1 | 2014-02-12 11:48:31 -0500 | [diff] [blame] | 211 | journal_err_out: |
J. Bruce Fields | 375e289 | 2012-04-18 15:16:33 -0400 | [diff] [blame] | 212 | unlock_two_nondirectories(inode, inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 213 | iput(inode_bl); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 214 | return err; |
| 215 | } |
| 216 | |
Eric Biggers | ba67901 | 2016-12-01 11:55:51 -0500 | [diff] [blame] | 217 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 218 | static int uuid_is_zero(__u8 u[16]) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < 16; i++) |
| 223 | if (u[i]) |
| 224 | return 0; |
| 225 | return 1; |
| 226 | } |
Eric Biggers | ba67901 | 2016-12-01 11:55:51 -0500 | [diff] [blame] | 227 | #endif |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 228 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 229 | static int ext4_ioctl_setflags(struct inode *inode, |
| 230 | unsigned int flags) |
| 231 | { |
| 232 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 233 | handle_t *handle = NULL; |
Anton Protopopov | fdde368 | 2016-02-11 23:57:21 -0500 | [diff] [blame] | 234 | int err = -EPERM, migrate = 0; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 235 | struct ext4_iloc iloc; |
| 236 | unsigned int oldflags, mask, i; |
| 237 | unsigned int jflag; |
| 238 | |
| 239 | /* Is it quota file? Do not allow user to mess with it */ |
Tahsin Erdogan | 02749a4 | 2017-06-22 11:31:25 -0400 | [diff] [blame] | 240 | if (ext4_is_quota_file(inode)) |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 241 | goto flags_out; |
| 242 | |
| 243 | oldflags = ei->i_flags; |
| 244 | |
| 245 | /* The JOURNAL_DATA flag is modifiable only by root */ |
| 246 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
| 247 | |
| 248 | /* |
| 249 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 250 | * the relevant capability. |
| 251 | * |
| 252 | * This test looks nicer. Thanks to Pauline Middelink |
| 253 | */ |
| 254 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
| 255 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 256 | goto flags_out; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * The JOURNAL_DATA flag can only be changed by |
| 261 | * the relevant capability. |
| 262 | */ |
| 263 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
| 264 | if (!capable(CAP_SYS_RESOURCE)) |
| 265 | goto flags_out; |
| 266 | } |
| 267 | if ((flags ^ oldflags) & EXT4_EXTENTS_FL) |
| 268 | migrate = 1; |
| 269 | |
| 270 | if (flags & EXT4_EOFBLOCKS_FL) { |
| 271 | /* we don't support adding EOFBLOCKS flag */ |
| 272 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { |
| 273 | err = -EOPNOTSUPP; |
| 274 | goto flags_out; |
| 275 | } |
Theodore Ts'o | 2c98eb5 | 2016-11-13 22:02:26 -0500 | [diff] [blame] | 276 | } else if (oldflags & EXT4_EOFBLOCKS_FL) { |
| 277 | err = ext4_truncate(inode); |
| 278 | if (err) |
| 279 | goto flags_out; |
| 280 | } |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 281 | |
| 282 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
| 283 | if (IS_ERR(handle)) { |
| 284 | err = PTR_ERR(handle); |
| 285 | goto flags_out; |
| 286 | } |
| 287 | if (IS_SYNC(inode)) |
| 288 | ext4_handle_sync(handle); |
| 289 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
| 290 | if (err) |
| 291 | goto flags_err; |
| 292 | |
| 293 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
| 294 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) |
| 295 | continue; |
Jan Kara | f8011d9 | 2016-11-29 11:13:13 -0500 | [diff] [blame] | 296 | /* These flags get special treatment later */ |
| 297 | if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL) |
| 298 | continue; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 299 | if (mask & flags) |
| 300 | ext4_set_inode_flag(inode, i); |
| 301 | else |
| 302 | ext4_clear_inode_flag(inode, i); |
| 303 | } |
| 304 | |
| 305 | ext4_set_inode_flags(inode); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 306 | inode->i_ctime = current_time(inode); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 307 | |
| 308 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
| 309 | flags_err: |
| 310 | ext4_journal_stop(handle); |
| 311 | if (err) |
| 312 | goto flags_out; |
| 313 | |
Ross Zwisler | e9072d8 | 2017-10-12 11:54:08 -0400 | [diff] [blame] | 314 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
| 315 | /* |
| 316 | * Changes to the journaling mode can cause unsafe changes to |
| 317 | * S_DAX if we are using the DAX mount option. |
| 318 | */ |
| 319 | if (test_opt(inode->i_sb, DAX)) { |
| 320 | err = -EBUSY; |
| 321 | goto flags_out; |
| 322 | } |
| 323 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 324 | err = ext4_change_inode_journal_flag(inode, jflag); |
Ross Zwisler | e9072d8 | 2017-10-12 11:54:08 -0400 | [diff] [blame] | 325 | if (err) |
| 326 | goto flags_out; |
| 327 | } |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 328 | if (migrate) { |
| 329 | if (flags & EXT4_EXTENTS_FL) |
| 330 | err = ext4_ext_migrate(inode); |
| 331 | else |
| 332 | err = ext4_ind_migrate(inode); |
| 333 | } |
| 334 | |
| 335 | flags_out: |
| 336 | return err; |
| 337 | } |
| 338 | |
| 339 | #ifdef CONFIG_QUOTA |
| 340 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) |
| 341 | { |
| 342 | struct inode *inode = file_inode(filp); |
| 343 | struct super_block *sb = inode->i_sb; |
| 344 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 345 | int err, rc; |
| 346 | handle_t *handle; |
| 347 | kprojid_t kprojid; |
| 348 | struct ext4_iloc iloc; |
| 349 | struct ext4_inode *raw_inode; |
Wang Shilong | 079788d | 2016-07-05 21:33:52 -0400 | [diff] [blame] | 350 | struct dquot *transfer_to[MAXQUOTAS] = { }; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 351 | |
Kaho Ng | 0b7b777 | 2016-09-05 23:11:58 -0400 | [diff] [blame] | 352 | if (!ext4_has_feature_project(sb)) { |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 353 | if (projid != EXT4_DEF_PROJID) |
| 354 | return -EOPNOTSUPP; |
| 355 | else |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE) |
| 360 | return -EOPNOTSUPP; |
| 361 | |
| 362 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); |
| 363 | |
| 364 | if (projid_eq(kprojid, EXT4_I(inode)->i_projid)) |
| 365 | return 0; |
| 366 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 367 | err = -EPERM; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 368 | /* Is it quota file? Do not allow user to mess with it */ |
Tahsin Erdogan | 02749a4 | 2017-06-22 11:31:25 -0400 | [diff] [blame] | 369 | if (ext4_is_quota_file(inode)) |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 370 | return err; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 371 | |
| 372 | err = ext4_get_inode_loc(inode, &iloc); |
| 373 | if (err) |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 374 | return err; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 375 | |
| 376 | raw_inode = ext4_raw_inode(&iloc); |
| 377 | if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) { |
Miao Xie | c03b45b | 2017-08-06 01:00:49 -0400 | [diff] [blame] | 378 | err = ext4_expand_extra_isize(inode, |
| 379 | EXT4_SB(sb)->s_want_extra_isize, |
| 380 | &iloc); |
| 381 | if (err) |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 382 | return err; |
Miao Xie | c03b45b | 2017-08-06 01:00:49 -0400 | [diff] [blame] | 383 | } else { |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 384 | brelse(iloc.bh); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 385 | } |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 386 | |
Wang Shilong | 182a79e | 2018-10-03 12:19:21 -0400 | [diff] [blame] | 387 | err = dquot_initialize(inode); |
| 388 | if (err) |
| 389 | return err; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 390 | |
| 391 | handle = ext4_journal_start(inode, EXT4_HT_QUOTA, |
| 392 | EXT4_QUOTA_INIT_BLOCKS(sb) + |
| 393 | EXT4_QUOTA_DEL_BLOCKS(sb) + 3); |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 394 | if (IS_ERR(handle)) |
| 395 | return PTR_ERR(handle); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 396 | |
| 397 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
| 398 | if (err) |
| 399 | goto out_stop; |
| 400 | |
Wang Shilong | 079788d | 2016-07-05 21:33:52 -0400 | [diff] [blame] | 401 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); |
| 402 | if (!IS_ERR(transfer_to[PRJQUOTA])) { |
Tahsin Erdogan | 7a9ca53 | 2017-06-22 11:46:48 -0400 | [diff] [blame] | 403 | |
| 404 | /* __dquot_transfer() calls back ext4_get_inode_usage() which |
| 405 | * counts xattr inode references. |
| 406 | */ |
| 407 | down_read(&EXT4_I(inode)->xattr_sem); |
Wang Shilong | 079788d | 2016-07-05 21:33:52 -0400 | [diff] [blame] | 408 | err = __dquot_transfer(inode, transfer_to); |
Tahsin Erdogan | 7a9ca53 | 2017-06-22 11:46:48 -0400 | [diff] [blame] | 409 | up_read(&EXT4_I(inode)->xattr_sem); |
Wang Shilong | 079788d | 2016-07-05 21:33:52 -0400 | [diff] [blame] | 410 | dqput(transfer_to[PRJQUOTA]); |
| 411 | if (err) |
| 412 | goto out_dirty; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 413 | } |
Wang Shilong | 079788d | 2016-07-05 21:33:52 -0400 | [diff] [blame] | 414 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 415 | EXT4_I(inode)->i_projid = kprojid; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 416 | inode->i_ctime = current_time(inode); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 417 | out_dirty: |
| 418 | rc = ext4_mark_iloc_dirty(handle, inode, &iloc); |
| 419 | if (!err) |
| 420 | err = rc; |
| 421 | out_stop: |
| 422 | ext4_journal_stop(handle); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 423 | return err; |
| 424 | } |
| 425 | #else |
| 426 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) |
| 427 | { |
| 428 | if (projid != EXT4_DEF_PROJID) |
| 429 | return -EOPNOTSUPP; |
| 430 | return 0; |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | /* Transfer internal flags to xflags */ |
| 435 | static inline __u32 ext4_iflags_to_xflags(unsigned long iflags) |
| 436 | { |
| 437 | __u32 xflags = 0; |
| 438 | |
| 439 | if (iflags & EXT4_SYNC_FL) |
| 440 | xflags |= FS_XFLAG_SYNC; |
| 441 | if (iflags & EXT4_IMMUTABLE_FL) |
| 442 | xflags |= FS_XFLAG_IMMUTABLE; |
| 443 | if (iflags & EXT4_APPEND_FL) |
| 444 | xflags |= FS_XFLAG_APPEND; |
| 445 | if (iflags & EXT4_NODUMP_FL) |
| 446 | xflags |= FS_XFLAG_NODUMP; |
| 447 | if (iflags & EXT4_NOATIME_FL) |
| 448 | xflags |= FS_XFLAG_NOATIME; |
| 449 | if (iflags & EXT4_PROJINHERIT_FL) |
| 450 | xflags |= FS_XFLAG_PROJINHERIT; |
| 451 | return xflags; |
| 452 | } |
| 453 | |
Jan Kara | d14e768 | 2016-11-29 11:18:39 -0500 | [diff] [blame] | 454 | #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \ |
| 455 | FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \ |
| 456 | FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT) |
| 457 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 458 | /* Transfer xflags flags to internal */ |
| 459 | static inline unsigned long ext4_xflags_to_iflags(__u32 xflags) |
| 460 | { |
| 461 | unsigned long iflags = 0; |
| 462 | |
| 463 | if (xflags & FS_XFLAG_SYNC) |
| 464 | iflags |= EXT4_SYNC_FL; |
| 465 | if (xflags & FS_XFLAG_IMMUTABLE) |
| 466 | iflags |= EXT4_IMMUTABLE_FL; |
| 467 | if (xflags & FS_XFLAG_APPEND) |
| 468 | iflags |= EXT4_APPEND_FL; |
| 469 | if (xflags & FS_XFLAG_NODUMP) |
| 470 | iflags |= EXT4_NODUMP_FL; |
| 471 | if (xflags & FS_XFLAG_NOATIME) |
| 472 | iflags |= EXT4_NOATIME_FL; |
| 473 | if (xflags & FS_XFLAG_PROJINHERIT) |
| 474 | iflags |= EXT4_PROJINHERIT_FL; |
| 475 | |
| 476 | return iflags; |
| 477 | } |
| 478 | |
Eric Biggers | 1a20a630 | 2017-04-30 00:40:44 -0400 | [diff] [blame] | 479 | static int ext4_shutdown(struct super_block *sb, unsigned long arg) |
Theodore Ts'o | 783d948 | 2017-02-05 19:47:14 -0500 | [diff] [blame] | 480 | { |
| 481 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 482 | __u32 flags; |
| 483 | |
| 484 | if (!capable(CAP_SYS_ADMIN)) |
| 485 | return -EPERM; |
| 486 | |
| 487 | if (get_user(flags, (__u32 __user *)arg)) |
| 488 | return -EFAULT; |
| 489 | |
| 490 | if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH) |
| 491 | return -EINVAL; |
| 492 | |
| 493 | if (ext4_forced_shutdown(sbi)) |
| 494 | return 0; |
| 495 | |
| 496 | ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags); |
Theodore Ts'o | ccf0f32 | 2018-02-18 20:53:23 -0500 | [diff] [blame] | 497 | trace_ext4_shutdown(sb, flags); |
Theodore Ts'o | 783d948 | 2017-02-05 19:47:14 -0500 | [diff] [blame] | 498 | |
| 499 | switch (flags) { |
| 500 | case EXT4_GOING_FLAGS_DEFAULT: |
| 501 | freeze_bdev(sb->s_bdev); |
| 502 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); |
| 503 | thaw_bdev(sb->s_bdev, sb); |
| 504 | break; |
| 505 | case EXT4_GOING_FLAGS_LOGFLUSH: |
| 506 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); |
| 507 | if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) { |
| 508 | (void) ext4_force_commit(sb); |
Theodore Ts'o | fb7c024 | 2018-02-18 23:45:18 -0500 | [diff] [blame] | 509 | jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); |
Theodore Ts'o | 783d948 | 2017-02-05 19:47:14 -0500 | [diff] [blame] | 510 | } |
| 511 | break; |
| 512 | case EXT4_GOING_FLAGS_NOLOGFLUSH: |
| 513 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); |
Theodore Ts'o | a6d9946 | 2018-02-18 23:16:28 -0500 | [diff] [blame] | 514 | if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) |
Theodore Ts'o | fb7c024 | 2018-02-18 23:45:18 -0500 | [diff] [blame] | 515 | jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); |
Theodore Ts'o | 783d948 | 2017-02-05 19:47:14 -0500 | [diff] [blame] | 516 | break; |
| 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | clear_opt(sb, DISCARD); |
| 521 | return 0; |
| 522 | } |
| 523 | |
Darrick J. Wong | 0c9ec4b | 2017-04-30 00:36:53 -0400 | [diff] [blame] | 524 | struct getfsmap_info { |
| 525 | struct super_block *gi_sb; |
| 526 | struct fsmap_head __user *gi_data; |
| 527 | unsigned int gi_idx; |
| 528 | __u32 gi_last_flags; |
| 529 | }; |
| 530 | |
| 531 | static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv) |
| 532 | { |
| 533 | struct getfsmap_info *info = priv; |
| 534 | struct fsmap fm; |
| 535 | |
| 536 | trace_ext4_getfsmap_mapping(info->gi_sb, xfm); |
| 537 | |
| 538 | info->gi_last_flags = xfm->fmr_flags; |
| 539 | ext4_fsmap_from_internal(info->gi_sb, &fm, xfm); |
| 540 | if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm, |
| 541 | sizeof(struct fsmap))) |
| 542 | return -EFAULT; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static int ext4_ioc_getfsmap(struct super_block *sb, |
| 548 | struct fsmap_head __user *arg) |
| 549 | { |
| 550 | struct getfsmap_info info = {0}; |
| 551 | struct ext4_fsmap_head xhead = {0}; |
| 552 | struct fsmap_head head; |
| 553 | bool aborted = false; |
| 554 | int error; |
| 555 | |
| 556 | if (copy_from_user(&head, arg, sizeof(struct fsmap_head))) |
| 557 | return -EFAULT; |
| 558 | if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) || |
| 559 | memchr_inv(head.fmh_keys[0].fmr_reserved, 0, |
| 560 | sizeof(head.fmh_keys[0].fmr_reserved)) || |
| 561 | memchr_inv(head.fmh_keys[1].fmr_reserved, 0, |
| 562 | sizeof(head.fmh_keys[1].fmr_reserved))) |
| 563 | return -EINVAL; |
| 564 | /* |
| 565 | * ext4 doesn't report file extents at all, so the only valid |
| 566 | * file offsets are the magic ones (all zeroes or all ones). |
| 567 | */ |
| 568 | if (head.fmh_keys[0].fmr_offset || |
| 569 | (head.fmh_keys[1].fmr_offset != 0 && |
| 570 | head.fmh_keys[1].fmr_offset != -1ULL)) |
| 571 | return -EINVAL; |
| 572 | |
| 573 | xhead.fmh_iflags = head.fmh_iflags; |
| 574 | xhead.fmh_count = head.fmh_count; |
| 575 | ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]); |
| 576 | ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]); |
| 577 | |
| 578 | trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]); |
| 579 | trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]); |
| 580 | |
| 581 | info.gi_sb = sb; |
| 582 | info.gi_data = arg; |
| 583 | error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info); |
| 584 | if (error == EXT4_QUERY_RANGE_ABORT) { |
| 585 | error = 0; |
| 586 | aborted = true; |
| 587 | } else if (error) |
| 588 | return error; |
| 589 | |
| 590 | /* If we didn't abort, set the "last" flag in the last fmx */ |
| 591 | if (!aborted && info.gi_idx) { |
| 592 | info.gi_last_flags |= FMR_OF_LAST; |
| 593 | if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags, |
| 594 | &info.gi_last_flags, |
| 595 | sizeof(info.gi_last_flags))) |
| 596 | return -EFAULT; |
| 597 | } |
| 598 | |
| 599 | /* copy back header */ |
| 600 | head.fmh_entries = xhead.fmh_entries; |
| 601 | head.fmh_oflags = xhead.fmh_oflags; |
| 602 | if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) |
| 603 | return -EFAULT; |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
Al Viro | e145b35 | 2017-09-29 21:39:59 -0400 | [diff] [blame] | 608 | static long ext4_ioctl_group_add(struct file *file, |
| 609 | struct ext4_new_group_data *input) |
| 610 | { |
| 611 | struct super_block *sb = file_inode(file)->i_sb; |
| 612 | int err, err2=0; |
| 613 | |
| 614 | err = ext4_resize_begin(sb); |
| 615 | if (err) |
| 616 | return err; |
| 617 | |
| 618 | if (ext4_has_feature_bigalloc(sb)) { |
| 619 | ext4_msg(sb, KERN_ERR, |
| 620 | "Online resizing not supported with bigalloc"); |
| 621 | err = -EOPNOTSUPP; |
| 622 | goto group_add_out; |
| 623 | } |
| 624 | |
| 625 | err = mnt_want_write_file(file); |
| 626 | if (err) |
| 627 | goto group_add_out; |
| 628 | |
| 629 | err = ext4_group_add(sb, input); |
| 630 | if (EXT4_SB(sb)->s_journal) { |
| 631 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 632 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 633 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 634 | } |
| 635 | if (err == 0) |
| 636 | err = err2; |
| 637 | mnt_drop_write_file(file); |
| 638 | if (!err && ext4_has_group_desc_csum(sb) && |
| 639 | test_opt(sb, INIT_INODE_TABLE)) |
| 640 | err = ext4_register_li_request(sb, input->group); |
| 641 | group_add_out: |
| 642 | ext4_resize_end(sb); |
| 643 | return err; |
| 644 | } |
| 645 | |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 646 | static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa) |
| 647 | { |
| 648 | /* |
| 649 | * Project Quota ID state is only allowed to change from within the init |
| 650 | * namespace. Enforce that restriction only if we are trying to change |
| 651 | * the quota ID state. Everything else is allowed in user namespaces. |
| 652 | */ |
| 653 | if (current_user_ns() == &init_user_ns) |
| 654 | return 0; |
| 655 | |
| 656 | if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid) |
| 657 | return -EINVAL; |
| 658 | |
| 659 | if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) { |
| 660 | if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT)) |
| 661 | return -EINVAL; |
| 662 | } else { |
| 663 | if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT) |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 670 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 671 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 672 | struct inode *inode = file_inode(filp); |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 673 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 674 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 675 | unsigned int flags; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 676 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 677 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 678 | |
| 679 | switch (cmd) { |
Darrick J. Wong | 0c9ec4b | 2017-04-30 00:36:53 -0400 | [diff] [blame] | 680 | case FS_IOC_GETFSMAP: |
| 681 | return ext4_ioc_getfsmap(sb, (void __user *)arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 682 | case EXT4_IOC_GETFLAGS: |
| 683 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 684 | return put_user(flags, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 685 | case EXT4_IOC_SETFLAGS: { |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 686 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 687 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 688 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 689 | return -EACCES; |
| 690 | |
| 691 | if (get_user(flags, (int __user *) arg)) |
| 692 | return -EFAULT; |
| 693 | |
Jan Kara | d14e768 | 2016-11-29 11:18:39 -0500 | [diff] [blame] | 694 | if (flags & ~EXT4_FL_USER_VISIBLE) |
| 695 | return -EOPNOTSUPP; |
| 696 | /* |
| 697 | * chattr(1) grabs flags via GETFLAGS, modifies the result and |
| 698 | * passes that to SETFLAGS. So we cannot easily make SETFLAGS |
| 699 | * more restrictive than just silently masking off visible but |
| 700 | * not settable flags as we always did. |
| 701 | */ |
| 702 | flags &= EXT4_FL_USER_MODIFIABLE; |
| 703 | if (ext4_mask_flags(inode->i_mode, flags) != flags) |
| 704 | return -EOPNOTSUPP; |
| 705 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 706 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 707 | if (err) |
| 708 | return err; |
| 709 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 710 | inode_lock(inode); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 711 | err = ext4_ioctl_setflags(inode, flags); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 712 | inode_unlock(inode); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 713 | mnt_drop_write_file(filp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 714 | return err; |
| 715 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 716 | case EXT4_IOC_GETVERSION: |
| 717 | case EXT4_IOC_GETVERSION_OLD: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 718 | return put_user(inode->i_generation, (int __user *) arg); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 719 | case EXT4_IOC_SETVERSION: |
| 720 | case EXT4_IOC_SETVERSION_OLD: { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 721 | handle_t *handle; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 722 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 723 | __u32 generation; |
| 724 | int err; |
| 725 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 726 | if (!inode_owner_or_capable(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 727 | return -EPERM; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 728 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 729 | if (ext4_has_metadata_csum(inode->i_sb)) { |
Darrick J. Wong | 814525f | 2012-04-29 18:31:10 -0400 | [diff] [blame] | 730 | ext4_warning(sb, "Setting inode version is not " |
| 731 | "supported with metadata_csum enabled."); |
| 732 | return -ENOTTY; |
| 733 | } |
| 734 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 735 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 736 | if (err) |
| 737 | return err; |
| 738 | if (get_user(generation, (int __user *) arg)) { |
| 739 | err = -EFAULT; |
| 740 | goto setversion_out; |
| 741 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 742 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 743 | inode_lock(inode); |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 744 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 745 | if (IS_ERR(handle)) { |
| 746 | err = PTR_ERR(handle); |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 747 | goto unlock_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 748 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 749 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 750 | if (err == 0) { |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 751 | inode->i_ctime = current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 752 | inode->i_generation = generation; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 753 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 754 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 755 | ext4_journal_stop(handle); |
Djalal Harouni | 6c2155b | 2012-01-03 02:31:52 +0100 | [diff] [blame] | 756 | |
| 757 | unlock_out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 758 | inode_unlock(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 759 | setversion_out: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 760 | mnt_drop_write_file(filp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 761 | return err; |
| 762 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 763 | case EXT4_IOC_GROUP_EXTEND: { |
| 764 | ext4_fsblk_t n_blocks_count; |
Peng Tao | ac046f1 | 2009-07-13 09:30:17 -0400 | [diff] [blame] | 765 | int err, err2=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 766 | |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 767 | err = ext4_resize_begin(sb); |
| 768 | if (err) |
| 769 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 770 | |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 771 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
| 772 | err = -EFAULT; |
| 773 | goto group_extend_out; |
| 774 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 775 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 776 | if (ext4_has_feature_bigalloc(sb)) { |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 777 | ext4_msg(sb, KERN_ERR, |
| 778 | "Online resizing not supported with bigalloc"); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 779 | err = -EOPNOTSUPP; |
| 780 | goto group_extend_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 781 | } |
| 782 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 783 | err = mnt_want_write_file(filp); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 784 | if (err) |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 785 | goto group_extend_out; |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 786 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 787 | 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] | 788 | if (EXT4_SB(sb)->s_journal) { |
| 789 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 790 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 791 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 792 | } |
Hidehiro Kawai | 7ffe1ea | 2008-10-10 20:29:21 -0400 | [diff] [blame] | 793 | if (err == 0) |
| 794 | err = err2; |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 795 | mnt_drop_write_file(filp); |
Djalal Harouni | 014a177 | 2012-01-04 17:09:52 -0500 | [diff] [blame] | 796 | group_extend_out: |
Yongqiang Yang | 8f82f84 | 2011-07-26 21:35:44 -0400 | [diff] [blame] | 797 | ext4_resize_end(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 798 | return err; |
| 799 | } |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 800 | |
| 801 | case EXT4_IOC_MOVE_EXT: { |
| 802 | struct move_extent me; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 803 | struct fd donor; |
| 804 | int err; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 805 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 806 | if (!(filp->f_mode & FMODE_READ) || |
| 807 | !(filp->f_mode & FMODE_WRITE)) |
| 808 | return -EBADF; |
| 809 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 810 | if (copy_from_user(&me, |
| 811 | (struct move_extent __user *)arg, sizeof(me))) |
| 812 | return -EFAULT; |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 813 | me.moved_len = 0; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 814 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 815 | donor = fdget(me.donor_fd); |
| 816 | if (!donor.file) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 817 | return -EBADF; |
| 818 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 819 | if (!(donor.file->f_mode & FMODE_WRITE)) { |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 820 | err = -EBADF; |
| 821 | goto mext_out; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 822 | } |
| 823 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 824 | if (ext4_has_feature_bigalloc(sb)) { |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 825 | ext4_msg(sb, KERN_ERR, |
| 826 | "Online defrag not supported with bigalloc"); |
Al Viro | 399c9b8 | 2012-08-26 21:00:03 -0400 | [diff] [blame] | 827 | err = -EOPNOTSUPP; |
| 828 | goto mext_out; |
Ross Zwisler | 73f34a5 | 2016-02-26 15:19:49 -0800 | [diff] [blame] | 829 | } else if (IS_DAX(inode)) { |
| 830 | ext4_msg(sb, KERN_ERR, |
| 831 | "Online defrag not supported with DAX"); |
| 832 | err = -EOPNOTSUPP; |
| 833 | goto mext_out; |
Theodore Ts'o | bab08ab | 2011-09-09 18:36:51 -0400 | [diff] [blame] | 834 | } |
| 835 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 836 | err = mnt_want_write_file(filp); |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 837 | if (err) |
| 838 | goto mext_out; |
| 839 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 840 | err = ext4_move_extents(filp, donor.file, me.orig_start, |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 841 | me.donor_start, me.len, &me.moved_len); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 842 | mnt_drop_write_file(filp); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 843 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 844 | if (copy_to_user((struct move_extent __user *)arg, |
Akira Fujita | c437b27 | 2010-03-04 00:39:24 -0500 | [diff] [blame] | 845 | &me, sizeof(me))) |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 846 | err = -EFAULT; |
| 847 | mext_out: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 848 | fdput(donor); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 849 | return err; |
| 850 | } |
| 851 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 852 | case EXT4_IOC_GROUP_ADD: { |
| 853 | struct ext4_new_group_data input; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 854 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 855 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
Al Viro | e145b35 | 2017-09-29 21:39:59 -0400 | [diff] [blame] | 856 | sizeof(input))) |
| 857 | return -EFAULT; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 858 | |
Al Viro | e145b35 | 2017-09-29 21:39:59 -0400 | [diff] [blame] | 859 | return ext4_ioctl_group_add(filp, &input); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 862 | case EXT4_IOC_MIGRATE: |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 863 | { |
| 864 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 865 | if (!inode_owner_or_capable(inode)) |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 866 | return -EACCES; |
| 867 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 868 | err = mnt_want_write_file(filp); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 869 | if (err) |
| 870 | return err; |
| 871 | /* |
| 872 | * inode_mutex prevent write and truncate on the file. |
| 873 | * Read still goes through. We take i_data_sem in |
| 874 | * ext4_ext_swap_inode_data before we switch the |
| 875 | * inode format to prevent read. |
| 876 | */ |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 877 | inode_lock((inode)); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 878 | err = ext4_ext_migrate(inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 879 | inode_unlock((inode)); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 880 | mnt_drop_write_file(filp); |
Aneesh Kumar K.V | 2a43a87 | 2008-09-13 12:52:26 -0400 | [diff] [blame] | 881 | return err; |
| 882 | } |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 883 | |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 884 | case EXT4_IOC_ALLOC_DA_BLKS: |
| 885 | { |
| 886 | int err; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 887 | if (!inode_owner_or_capable(inode)) |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 888 | return -EACCES; |
| 889 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 890 | err = mnt_want_write_file(filp); |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 891 | if (err) |
| 892 | return err; |
| 893 | err = ext4_alloc_da_blocks(inode); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 894 | mnt_drop_write_file(filp); |
Theodore Ts'o | ccd2506 | 2009-02-26 01:04:07 -0500 | [diff] [blame] | 895 | return err; |
| 896 | } |
| 897 | |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 898 | case EXT4_IOC_SWAP_BOOT: |
Dmitry Monakhov | 3e67cfad2 | 2014-10-03 12:47:23 -0400 | [diff] [blame] | 899 | { |
| 900 | int err; |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 901 | if (!(filp->f_mode & FMODE_WRITE)) |
| 902 | return -EBADF; |
Dmitry Monakhov | 3e67cfad2 | 2014-10-03 12:47:23 -0400 | [diff] [blame] | 903 | err = mnt_want_write_file(filp); |
| 904 | if (err) |
| 905 | return err; |
| 906 | err = swap_inode_boot_loader(sb, inode); |
| 907 | mnt_drop_write_file(filp); |
| 908 | return err; |
| 909 | } |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 910 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 911 | case EXT4_IOC_RESIZE_FS: { |
| 912 | ext4_fsblk_t n_blocks_count; |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 913 | int err = 0, err2 = 0; |
Theodore Ts'o | 7f51186 | 2013-01-13 08:41:45 -0500 | [diff] [blame] | 914 | ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 915 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 916 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
| 917 | sizeof(__u64))) { |
| 918 | return -EFAULT; |
| 919 | } |
| 920 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 921 | err = ext4_resize_begin(sb); |
| 922 | if (err) |
| 923 | return err; |
| 924 | |
Al Viro | 8cae6f7 | 2012-07-19 11:19:07 +0400 | [diff] [blame] | 925 | err = mnt_want_write_file(filp); |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 926 | if (err) |
| 927 | goto resizefs_out; |
| 928 | |
| 929 | err = ext4_resize_fs(sb, n_blocks_count); |
| 930 | if (EXT4_SB(sb)->s_journal) { |
| 931 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 932 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 933 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 934 | } |
| 935 | if (err == 0) |
| 936 | err = err2; |
Al Viro | 8cae6f7 | 2012-07-19 11:19:07 +0400 | [diff] [blame] | 937 | mnt_drop_write_file(filp); |
Theodore Ts'o | 7f51186 | 2013-01-13 08:41:45 -0500 | [diff] [blame] | 938 | if (!err && (o_group > EXT4_SB(sb)->s_groups_count) && |
| 939 | ext4_has_group_desc_csum(sb) && |
| 940 | test_opt(sb, INIT_INODE_TABLE)) |
| 941 | err = ext4_register_li_request(sb, o_group); |
| 942 | |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 943 | resizefs_out: |
| 944 | ext4_resize_end(sb); |
| 945 | return err; |
| 946 | } |
| 947 | |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 948 | case FITRIM: |
| 949 | { |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 950 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 951 | struct fstrim_range range; |
| 952 | int ret = 0; |
| 953 | |
| 954 | if (!capable(CAP_SYS_ADMIN)) |
| 955 | return -EPERM; |
| 956 | |
Lukas Czerner | 4143179 | 2011-02-23 12:42:32 -0500 | [diff] [blame] | 957 | if (!blk_queue_discard(q)) |
| 958 | return -EOPNOTSUPP; |
| 959 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 960 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 961 | sizeof(range))) |
| 962 | return -EFAULT; |
| 963 | |
Lukas Czerner | 5c2ed62 | 2011-02-23 17:49:51 -0500 | [diff] [blame] | 964 | range.minlen = max((unsigned int)range.minlen, |
| 965 | q->limits.discard_granularity); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 966 | ret = ext4_trim_fs(sb, &range); |
| 967 | if (ret < 0) |
| 968 | return ret; |
| 969 | |
H Hartley Sweeten | e6705f7 | 2011-10-18 10:59:51 -0400 | [diff] [blame] | 970 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 971 | sizeof(range))) |
| 972 | return -EFAULT; |
| 973 | |
| 974 | return 0; |
| 975 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 976 | case EXT4_IOC_PRECACHE_EXTENTS: |
| 977 | return ext4_ext_precache(inode); |
Lukas Czerner | e681c04 | 2010-11-19 21:47:07 -0500 | [diff] [blame] | 978 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 979 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
Richard Weinberger | 9a200d0 | 2016-09-30 01:49:55 -0400 | [diff] [blame] | 980 | if (!ext4_has_feature_encrypt(sb)) |
| 981 | return -EOPNOTSUPP; |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 982 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); |
Richard Weinberger | 9a200d0 | 2016-09-30 01:49:55 -0400 | [diff] [blame] | 983 | |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 984 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: { |
Eric Biggers | ba67901 | 2016-12-01 11:55:51 -0500 | [diff] [blame] | 985 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 986 | int err, err2; |
| 987 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 988 | handle_t *handle; |
| 989 | |
Eric Biggers | 35997d1 | 2016-12-01 11:54:18 -0500 | [diff] [blame] | 990 | if (!ext4_has_feature_encrypt(sb)) |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 991 | return -EOPNOTSUPP; |
| 992 | if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { |
| 993 | err = mnt_want_write_file(filp); |
| 994 | if (err) |
| 995 | return err; |
| 996 | handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); |
| 997 | if (IS_ERR(handle)) { |
| 998 | err = PTR_ERR(handle); |
| 999 | goto pwsalt_err_exit; |
| 1000 | } |
| 1001 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); |
| 1002 | if (err) |
| 1003 | goto pwsalt_err_journal; |
| 1004 | generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); |
| 1005 | err = ext4_handle_dirty_metadata(handle, NULL, |
| 1006 | sbi->s_sbh); |
| 1007 | pwsalt_err_journal: |
| 1008 | err2 = ext4_journal_stop(handle); |
| 1009 | if (err2 && !err) |
| 1010 | err = err2; |
| 1011 | pwsalt_err_exit: |
| 1012 | mnt_drop_write_file(filp); |
| 1013 | if (err) |
| 1014 | return err; |
| 1015 | } |
Fabian Frederick | b4ab9e2 | 2015-06-08 12:23:21 -0400 | [diff] [blame] | 1016 | if (copy_to_user((void __user *) arg, |
| 1017 | sbi->s_es->s_encrypt_pw_salt, 16)) |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 1018 | return -EFAULT; |
| 1019 | return 0; |
Eric Biggers | ba67901 | 2016-12-01 11:55:51 -0500 | [diff] [blame] | 1020 | #else |
| 1021 | return -EOPNOTSUPP; |
| 1022 | #endif |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 1023 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 1024 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
| 1025 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 1026 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1027 | case EXT4_IOC_FSGETXATTR: |
| 1028 | { |
| 1029 | struct fsxattr fa; |
| 1030 | |
| 1031 | memset(&fa, 0, sizeof(struct fsxattr)); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1032 | fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE); |
| 1033 | |
Kaho Ng | 0b7b777 | 2016-09-05 23:11:58 -0400 | [diff] [blame] | 1034 | if (ext4_has_feature_project(inode->i_sb)) { |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1035 | fa.fsx_projid = (__u32)from_kprojid(&init_user_ns, |
| 1036 | EXT4_I(inode)->i_projid); |
| 1037 | } |
| 1038 | |
| 1039 | if (copy_to_user((struct fsxattr __user *)arg, |
| 1040 | &fa, sizeof(fa))) |
| 1041 | return -EFAULT; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | case EXT4_IOC_FSSETXATTR: |
| 1045 | { |
| 1046 | struct fsxattr fa; |
| 1047 | int err; |
| 1048 | |
| 1049 | if (copy_from_user(&fa, (struct fsxattr __user *)arg, |
| 1050 | sizeof(fa))) |
| 1051 | return -EFAULT; |
| 1052 | |
| 1053 | /* Make sure caller has proper permission */ |
| 1054 | if (!inode_owner_or_capable(inode)) |
| 1055 | return -EACCES; |
| 1056 | |
Jan Kara | d14e768 | 2016-11-29 11:18:39 -0500 | [diff] [blame] | 1057 | if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS) |
| 1058 | return -EOPNOTSUPP; |
| 1059 | |
| 1060 | flags = ext4_xflags_to_iflags(fa.fsx_xflags); |
| 1061 | if (ext4_mask_flags(inode->i_mode, flags) != flags) |
| 1062 | return -EOPNOTSUPP; |
| 1063 | |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1064 | err = mnt_want_write_file(filp); |
| 1065 | if (err) |
| 1066 | return err; |
| 1067 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1068 | inode_lock(inode); |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 1069 | err = ext4_ioctl_check_project(inode, &fa); |
| 1070 | if (err) |
| 1071 | goto out; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1072 | flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) | |
| 1073 | (flags & EXT4_FL_XFLAG_VISIBLE); |
| 1074 | err = ext4_ioctl_setflags(inode, flags); |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 1075 | if (err) |
| 1076 | goto out; |
| 1077 | err = ext4_ioctl_setproject(filp, fa.fsx_projid); |
| 1078 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1079 | inode_unlock(inode); |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1080 | mnt_drop_write_file(filp); |
Wang Shilong | dc7ac6c | 2018-10-03 10:33:32 -0400 | [diff] [blame] | 1081 | return err; |
Li Xi | 9b7365f | 2016-01-08 16:01:22 -0500 | [diff] [blame] | 1082 | } |
Theodore Ts'o | e9be2ac | 2017-02-20 15:34:59 -0500 | [diff] [blame] | 1083 | case EXT4_IOC_SHUTDOWN: |
| 1084 | return ext4_shutdown(sb, arg); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1085 | default: |
| 1086 | return -ENOTTY; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | #ifdef CONFIG_COMPAT |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1091 | 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] | 1092 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1093 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 1094 | switch (cmd) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1095 | case EXT4_IOC32_GETFLAGS: |
| 1096 | cmd = EXT4_IOC_GETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1097 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1098 | case EXT4_IOC32_SETFLAGS: |
| 1099 | cmd = EXT4_IOC_SETFLAGS; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1100 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1101 | case EXT4_IOC32_GETVERSION: |
| 1102 | cmd = EXT4_IOC_GETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1103 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1104 | case EXT4_IOC32_SETVERSION: |
| 1105 | cmd = EXT4_IOC_SETVERSION; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1106 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1107 | case EXT4_IOC32_GROUP_EXTEND: |
| 1108 | cmd = EXT4_IOC_GROUP_EXTEND; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1109 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1110 | case EXT4_IOC32_GETVERSION_OLD: |
| 1111 | cmd = EXT4_IOC_GETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1112 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1113 | case EXT4_IOC32_SETVERSION_OLD: |
| 1114 | cmd = EXT4_IOC_SETVERSION_OLD; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1115 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1116 | case EXT4_IOC32_GETRSVSZ: |
| 1117 | cmd = EXT4_IOC_GETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1118 | break; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1119 | case EXT4_IOC32_SETRSVSZ: |
| 1120 | cmd = EXT4_IOC_SETRSVSZ; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1121 | break; |
Ben Hutchings | 4d92dc0 | 2010-05-17 06:00:00 -0400 | [diff] [blame] | 1122 | case EXT4_IOC32_GROUP_ADD: { |
| 1123 | struct compat_ext4_new_group_input __user *uinput; |
Al Viro | e145b35 | 2017-09-29 21:39:59 -0400 | [diff] [blame] | 1124 | struct ext4_new_group_data input; |
Ben Hutchings | 4d92dc0 | 2010-05-17 06:00:00 -0400 | [diff] [blame] | 1125 | int err; |
| 1126 | |
| 1127 | uinput = compat_ptr(arg); |
| 1128 | err = get_user(input.group, &uinput->group); |
| 1129 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); |
| 1130 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); |
| 1131 | err |= get_user(input.inode_table, &uinput->inode_table); |
| 1132 | err |= get_user(input.blocks_count, &uinput->blocks_count); |
| 1133 | err |= get_user(input.reserved_blocks, |
| 1134 | &uinput->reserved_blocks); |
| 1135 | if (err) |
| 1136 | return -EFAULT; |
Al Viro | e145b35 | 2017-09-29 21:39:59 -0400 | [diff] [blame] | 1137 | return ext4_ioctl_group_add(file, &input); |
Ben Hutchings | 4d92dc0 | 2010-05-17 06:00:00 -0400 | [diff] [blame] | 1138 | } |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 1139 | case EXT4_IOC_MOVE_EXT: |
Yongqiang Yang | 19c5246 | 2012-01-04 17:09:44 -0500 | [diff] [blame] | 1140 | case EXT4_IOC_RESIZE_FS: |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 1141 | case EXT4_IOC_PRECACHE_EXTENTS: |
Michael Halcrow | 9bd8212 | 2015-04-11 07:48:01 -0400 | [diff] [blame] | 1142 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
| 1143 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: |
| 1144 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
Theodore Ts'o | e9be2ac | 2017-02-20 15:34:59 -0500 | [diff] [blame] | 1145 | case EXT4_IOC_SHUTDOWN: |
Darrick J. Wong | 0c9ec4b | 2017-04-30 00:36:53 -0400 | [diff] [blame] | 1146 | case FS_IOC_GETFSMAP: |
Christian Borntraeger | b684b2e | 2010-05-15 00:00:00 -0400 | [diff] [blame] | 1147 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1148 | default: |
| 1149 | return -ENOIOCTLCMD; |
| 1150 | } |
Andi Kleen | 5cdd7b2 | 2008-04-29 22:03:54 -0400 | [diff] [blame] | 1151 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1152 | } |
| 1153 | #endif |