1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext4/ioctl.c 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> 12 #include <linux/capability.h> 13 #include <linux/time.h> 14 #include <linux/compat.h> 15 #include <linux/mount.h> 16 #include <linux/file.h> 17 #include <linux/quotaops.h> 18 #include <linux/random.h> 19 #include <linux/uaccess.h> 20 #include <linux/delay.h> 21 #include <linux/iversion.h> 22 #include <linux/fileattr.h> 23 #include "ext4_jbd2.h" 24 #include "ext4.h" 25 #include <linux/fsmap.h> 26 #include "fsmap.h" 27 #include <trace/events/ext4.h> 28 29 typedef void ext4_update_sb_callback(struct ext4_super_block *es, 30 const void *arg); 31 32 /* 33 * Superblock modification callback function for changing file system 34 * label 35 */ 36 static void ext4_sb_setlabel(struct ext4_super_block *es, const void *arg) 37 { 38 /* Sanity check, this should never happen */ 39 BUILD_BUG_ON(sizeof(es->s_volume_name) < EXT4_LABEL_MAX); 40 41 memcpy(es->s_volume_name, (char *)arg, EXT4_LABEL_MAX); 42 } 43 44 static 45 int ext4_update_primary_sb(struct super_block *sb, handle_t *handle, 46 ext4_update_sb_callback func, 47 const void *arg) 48 { 49 int err = 0; 50 struct ext4_sb_info *sbi = EXT4_SB(sb); 51 struct buffer_head *bh = sbi->s_sbh; 52 struct ext4_super_block *es = sbi->s_es; 53 54 trace_ext4_update_sb(sb, bh->b_blocknr, 1); 55 56 BUFFER_TRACE(bh, "get_write_access"); 57 err = ext4_journal_get_write_access(handle, sb, 58 bh, 59 EXT4_JTR_NONE); 60 if (err) 61 goto out_err; 62 63 lock_buffer(bh); 64 func(es, arg); 65 ext4_superblock_csum_set(sb); 66 unlock_buffer(bh); 67 68 if (buffer_write_io_error(bh) || !buffer_uptodate(bh)) { 69 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to " 70 "superblock detected"); 71 clear_buffer_write_io_error(bh); 72 set_buffer_uptodate(bh); 73 } 74 75 err = ext4_handle_dirty_metadata(handle, NULL, bh); 76 if (err) 77 goto out_err; 78 err = sync_dirty_buffer(bh); 79 out_err: 80 ext4_std_error(sb, err); 81 return err; 82 } 83 84 /* 85 * Update one backup superblock in the group 'grp' using the callback 86 * function 'func' and argument 'arg'. If the handle is NULL the 87 * modification is not journalled. 88 * 89 * Returns: 0 when no modification was done (no superblock in the group) 90 * 1 when the modification was successful 91 * <0 on error 92 */ 93 static int ext4_update_backup_sb(struct super_block *sb, 94 handle_t *handle, ext4_group_t grp, 95 ext4_update_sb_callback func, const void *arg) 96 { 97 int err = 0; 98 ext4_fsblk_t sb_block; 99 struct buffer_head *bh; 100 unsigned long offset = 0; 101 struct ext4_super_block *es; 102 103 if (!ext4_bg_has_super(sb, grp)) 104 return 0; 105 106 /* 107 * For the group 0 there is always 1k padding, so we have 108 * either adjust offset, or sb_block depending on blocksize 109 */ 110 if (grp == 0) { 111 sb_block = 1 * EXT4_MIN_BLOCK_SIZE; 112 offset = do_div(sb_block, sb->s_blocksize); 113 } else { 114 sb_block = ext4_group_first_block_no(sb, grp); 115 offset = 0; 116 } 117 118 trace_ext4_update_sb(sb, sb_block, handle ? 1 : 0); 119 120 bh = ext4_sb_bread(sb, sb_block, 0); 121 if (IS_ERR(bh)) 122 return PTR_ERR(bh); 123 124 if (handle) { 125 BUFFER_TRACE(bh, "get_write_access"); 126 err = ext4_journal_get_write_access(handle, sb, 127 bh, 128 EXT4_JTR_NONE); 129 if (err) 130 goto out_bh; 131 } 132 133 es = (struct ext4_super_block *) (bh->b_data + offset); 134 lock_buffer(bh); 135 if (ext4_has_metadata_csum(sb) && 136 es->s_checksum != ext4_superblock_csum(sb, es)) { 137 ext4_msg(sb, KERN_ERR, "Invalid checksum for backup " 138 "superblock %llu\n", sb_block); 139 unlock_buffer(bh); 140 err = -EFSBADCRC; 141 goto out_bh; 142 } 143 func(es, arg); 144 if (ext4_has_metadata_csum(sb)) 145 es->s_checksum = ext4_superblock_csum(sb, es); 146 set_buffer_uptodate(bh); 147 unlock_buffer(bh); 148 149 if (err) 150 goto out_bh; 151 152 if (handle) { 153 err = ext4_handle_dirty_metadata(handle, NULL, bh); 154 if (err) 155 goto out_bh; 156 } else { 157 BUFFER_TRACE(bh, "marking dirty"); 158 mark_buffer_dirty(bh); 159 } 160 err = sync_dirty_buffer(bh); 161 162 out_bh: 163 brelse(bh); 164 ext4_std_error(sb, err); 165 return (err) ? err : 1; 166 } 167 168 /* 169 * Update primary and backup superblocks using the provided function 170 * func and argument arg. 171 * 172 * Only the primary superblock and at most two backup superblock 173 * modifications are journalled; the rest is modified without journal. 174 * This is safe because e2fsck will re-write them if there is a problem, 175 * and we're very unlikely to ever need more than two backups. 176 */ 177 static 178 int ext4_update_superblocks_fn(struct super_block *sb, 179 ext4_update_sb_callback func, 180 const void *arg) 181 { 182 handle_t *handle; 183 ext4_group_t ngroups; 184 unsigned int three = 1; 185 unsigned int five = 5; 186 unsigned int seven = 7; 187 int err = 0, ret, i; 188 ext4_group_t grp, primary_grp; 189 struct ext4_sb_info *sbi = EXT4_SB(sb); 190 191 /* 192 * We can't update superblocks while the online resize is running 193 */ 194 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING, 195 &sbi->s_ext4_flags)) { 196 ext4_msg(sb, KERN_ERR, "Can't modify superblock while" 197 "performing online resize"); 198 return -EBUSY; 199 } 200 201 /* 202 * We're only going to update primary superblock and two 203 * backup superblocks in this transaction. 204 */ 205 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 3); 206 if (IS_ERR(handle)) { 207 err = PTR_ERR(handle); 208 goto out; 209 } 210 211 /* Update primary superblock */ 212 err = ext4_update_primary_sb(sb, handle, func, arg); 213 if (err) { 214 ext4_msg(sb, KERN_ERR, "Failed to update primary " 215 "superblock"); 216 goto out_journal; 217 } 218 219 primary_grp = ext4_get_group_number(sb, sbi->s_sbh->b_blocknr); 220 ngroups = ext4_get_groups_count(sb); 221 222 /* 223 * Update backup superblocks. We have to start from group 0 224 * because it might not be where the primary superblock is 225 * if the fs is mounted with -o sb=<backup_sb_block> 226 */ 227 i = 0; 228 grp = 0; 229 while (grp < ngroups) { 230 /* Skip primary superblock */ 231 if (grp == primary_grp) 232 goto next_grp; 233 234 ret = ext4_update_backup_sb(sb, handle, grp, func, arg); 235 if (ret < 0) { 236 /* Ignore bad checksum; try to update next sb */ 237 if (ret == -EFSBADCRC) 238 goto next_grp; 239 err = ret; 240 goto out_journal; 241 } 242 243 i += ret; 244 if (handle && i > 1) { 245 /* 246 * We're only journalling primary superblock and 247 * two backup superblocks; the rest is not 248 * journalled. 249 */ 250 err = ext4_journal_stop(handle); 251 if (err) 252 goto out; 253 handle = NULL; 254 } 255 next_grp: 256 grp = ext4_list_backups(sb, &three, &five, &seven); 257 } 258 259 out_journal: 260 if (handle) { 261 ret = ext4_journal_stop(handle); 262 if (ret && !err) 263 err = ret; 264 } 265 out: 266 clear_bit_unlock(EXT4_FLAGS_RESIZING, &sbi->s_ext4_flags); 267 smp_mb__after_atomic(); 268 return err ? err : 0; 269 } 270 271 /* 272 * Swap memory between @a and @b for @len bytes. 273 * 274 * @a: pointer to first memory area 275 * @b: pointer to second memory area 276 * @len: number of bytes to swap 277 * 278 */ 279 static void memswap(void *a, void *b, size_t len) 280 { 281 unsigned char *ap, *bp; 282 283 ap = (unsigned char *)a; 284 bp = (unsigned char *)b; 285 while (len-- > 0) { 286 swap(*ap, *bp); 287 ap++; 288 bp++; 289 } 290 } 291 292 /* 293 * Swap i_data and associated attributes between @inode1 and @inode2. 294 * This function is used for the primary swap between inode1 and inode2 295 * and also to revert this primary swap in case of errors. 296 * 297 * Therefore you have to make sure, that calling this method twice 298 * will revert all changes. 299 * 300 * @inode1: pointer to first inode 301 * @inode2: pointer to second inode 302 */ 303 static void swap_inode_data(struct inode *inode1, struct inode *inode2) 304 { 305 loff_t isize; 306 struct ext4_inode_info *ei1; 307 struct ext4_inode_info *ei2; 308 unsigned long tmp; 309 310 ei1 = EXT4_I(inode1); 311 ei2 = EXT4_I(inode2); 312 313 swap(inode1->i_version, inode2->i_version); 314 swap(inode1->i_atime, inode2->i_atime); 315 swap(inode1->i_mtime, inode2->i_mtime); 316 317 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data)); 318 tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP; 319 ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) | 320 (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP); 321 ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP); 322 swap(ei1->i_disksize, ei2->i_disksize); 323 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS); 324 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS); 325 326 isize = i_size_read(inode1); 327 i_size_write(inode1, i_size_read(inode2)); 328 i_size_write(inode2, isize); 329 } 330 331 void ext4_reset_inode_seed(struct inode *inode) 332 { 333 struct ext4_inode_info *ei = EXT4_I(inode); 334 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 335 __le32 inum = cpu_to_le32(inode->i_ino); 336 __le32 gen = cpu_to_le32(inode->i_generation); 337 __u32 csum; 338 339 if (!ext4_has_metadata_csum(inode->i_sb)) 340 return; 341 342 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum)); 343 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen)); 344 } 345 346 /* 347 * Swap the information from the given @inode and the inode 348 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other 349 * important fields of the inodes. 350 * 351 * @sb: the super block of the filesystem 352 * @mnt_userns: user namespace of the mount the inode was found from 353 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO 354 * 355 */ 356 static long swap_inode_boot_loader(struct super_block *sb, 357 struct user_namespace *mnt_userns, 358 struct inode *inode) 359 { 360 handle_t *handle; 361 int err; 362 struct inode *inode_bl; 363 struct ext4_inode_info *ei_bl; 364 qsize_t size, size_bl, diff; 365 blkcnt_t blocks; 366 unsigned short bytes; 367 368 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL); 369 if (IS_ERR(inode_bl)) 370 return PTR_ERR(inode_bl); 371 ei_bl = EXT4_I(inode_bl); 372 373 /* Protect orig inodes against a truncate and make sure, 374 * that only 1 swap_inode_boot_loader is running. */ 375 lock_two_nondirectories(inode, inode_bl); 376 377 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) || 378 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) || 379 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) || 380 ext4_has_inline_data(inode)) { 381 err = -EINVAL; 382 goto journal_err_out; 383 } 384 385 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) || 386 !inode_owner_or_capable(mnt_userns, inode) || 387 !capable(CAP_SYS_ADMIN)) { 388 err = -EPERM; 389 goto journal_err_out; 390 } 391 392 filemap_invalidate_lock(inode->i_mapping); 393 err = filemap_write_and_wait(inode->i_mapping); 394 if (err) 395 goto err_out; 396 397 err = filemap_write_and_wait(inode_bl->i_mapping); 398 if (err) 399 goto err_out; 400 401 /* Wait for all existing dio workers */ 402 inode_dio_wait(inode); 403 inode_dio_wait(inode_bl); 404 405 truncate_inode_pages(&inode->i_data, 0); 406 truncate_inode_pages(&inode_bl->i_data, 0); 407 408 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); 409 if (IS_ERR(handle)) { 410 err = -EINVAL; 411 goto err_out; 412 } 413 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_SWAP_BOOT, handle); 414 415 /* Protect extent tree against block allocations via delalloc */ 416 ext4_double_down_write_data_sem(inode, inode_bl); 417 418 if (inode_bl->i_nlink == 0) { 419 /* this inode has never been used as a BOOT_LOADER */ 420 set_nlink(inode_bl, 1); 421 i_uid_write(inode_bl, 0); 422 i_gid_write(inode_bl, 0); 423 inode_bl->i_flags = 0; 424 ei_bl->i_flags = 0; 425 inode_set_iversion(inode_bl, 1); 426 i_size_write(inode_bl, 0); 427 inode_bl->i_mode = S_IFREG; 428 if (ext4_has_feature_extents(sb)) { 429 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS); 430 ext4_ext_tree_init(handle, inode_bl); 431 } else 432 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data)); 433 } 434 435 err = dquot_initialize(inode); 436 if (err) 437 goto err_out1; 438 439 size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes; 440 size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes; 441 diff = size - size_bl; 442 swap_inode_data(inode, inode_bl); 443 444 inode->i_ctime = inode_bl->i_ctime = current_time(inode); 445 446 inode->i_generation = prandom_u32(); 447 inode_bl->i_generation = prandom_u32(); 448 ext4_reset_inode_seed(inode); 449 ext4_reset_inode_seed(inode_bl); 450 451 ext4_discard_preallocations(inode, 0); 452 453 err = ext4_mark_inode_dirty(handle, inode); 454 if (err < 0) { 455 /* No need to update quota information. */ 456 ext4_warning(inode->i_sb, 457 "couldn't mark inode #%lu dirty (err %d)", 458 inode->i_ino, err); 459 /* Revert all changes: */ 460 swap_inode_data(inode, inode_bl); 461 ext4_mark_inode_dirty(handle, inode); 462 goto err_out1; 463 } 464 465 blocks = inode_bl->i_blocks; 466 bytes = inode_bl->i_bytes; 467 inode_bl->i_blocks = inode->i_blocks; 468 inode_bl->i_bytes = inode->i_bytes; 469 err = ext4_mark_inode_dirty(handle, inode_bl); 470 if (err < 0) { 471 /* No need to update quota information. */ 472 ext4_warning(inode_bl->i_sb, 473 "couldn't mark inode #%lu dirty (err %d)", 474 inode_bl->i_ino, err); 475 goto revert; 476 } 477 478 /* Bootloader inode should not be counted into quota information. */ 479 if (diff > 0) 480 dquot_free_space(inode, diff); 481 else 482 err = dquot_alloc_space(inode, -1 * diff); 483 484 if (err < 0) { 485 revert: 486 /* Revert all changes: */ 487 inode_bl->i_blocks = blocks; 488 inode_bl->i_bytes = bytes; 489 swap_inode_data(inode, inode_bl); 490 ext4_mark_inode_dirty(handle, inode); 491 ext4_mark_inode_dirty(handle, inode_bl); 492 } 493 494 err_out1: 495 ext4_journal_stop(handle); 496 ext4_double_up_write_data_sem(inode, inode_bl); 497 498 err_out: 499 filemap_invalidate_unlock(inode->i_mapping); 500 journal_err_out: 501 unlock_two_nondirectories(inode, inode_bl); 502 iput(inode_bl); 503 return err; 504 } 505 506 /* 507 * If immutable is set and we are not clearing it, we're not allowed to change 508 * anything else in the inode. Don't error out if we're only trying to set 509 * immutable on an immutable file. 510 */ 511 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid, 512 unsigned int flags) 513 { 514 struct ext4_inode_info *ei = EXT4_I(inode); 515 unsigned int oldflags = ei->i_flags; 516 517 if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL)) 518 return 0; 519 520 if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL)) 521 return -EPERM; 522 if (ext4_has_feature_project(inode->i_sb) && 523 __kprojid_val(ei->i_projid) != new_projid) 524 return -EPERM; 525 526 return 0; 527 } 528 529 static void ext4_dax_dontcache(struct inode *inode, unsigned int flags) 530 { 531 struct ext4_inode_info *ei = EXT4_I(inode); 532 533 if (S_ISDIR(inode->i_mode)) 534 return; 535 536 if (test_opt2(inode->i_sb, DAX_NEVER) || 537 test_opt(inode->i_sb, DAX_ALWAYS)) 538 return; 539 540 if ((ei->i_flags ^ flags) & EXT4_DAX_FL) 541 d_mark_dontcache(inode); 542 } 543 544 static bool dax_compatible(struct inode *inode, unsigned int oldflags, 545 unsigned int flags) 546 { 547 /* Allow the DAX flag to be changed on inline directories */ 548 if (S_ISDIR(inode->i_mode)) { 549 flags &= ~EXT4_INLINE_DATA_FL; 550 oldflags &= ~EXT4_INLINE_DATA_FL; 551 } 552 553 if (flags & EXT4_DAX_FL) { 554 if ((oldflags & EXT4_DAX_MUT_EXCL) || 555 ext4_test_inode_state(inode, 556 EXT4_STATE_VERITY_IN_PROGRESS)) { 557 return false; 558 } 559 } 560 561 if ((flags & EXT4_DAX_MUT_EXCL) && (oldflags & EXT4_DAX_FL)) 562 return false; 563 564 return true; 565 } 566 567 static int ext4_ioctl_setflags(struct inode *inode, 568 unsigned int flags) 569 { 570 struct ext4_inode_info *ei = EXT4_I(inode); 571 handle_t *handle = NULL; 572 int err = -EPERM, migrate = 0; 573 struct ext4_iloc iloc; 574 unsigned int oldflags, mask, i; 575 struct super_block *sb = inode->i_sb; 576 577 /* Is it quota file? Do not allow user to mess with it */ 578 if (ext4_is_quota_file(inode)) 579 goto flags_out; 580 581 oldflags = ei->i_flags; 582 /* 583 * The JOURNAL_DATA flag can only be changed by 584 * the relevant capability. 585 */ 586 if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { 587 if (!capable(CAP_SYS_RESOURCE)) 588 goto flags_out; 589 } 590 591 if (!dax_compatible(inode, oldflags, flags)) { 592 err = -EOPNOTSUPP; 593 goto flags_out; 594 } 595 596 if ((flags ^ oldflags) & EXT4_EXTENTS_FL) 597 migrate = 1; 598 599 if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) { 600 if (!ext4_has_feature_casefold(sb)) { 601 err = -EOPNOTSUPP; 602 goto flags_out; 603 } 604 605 if (!S_ISDIR(inode->i_mode)) { 606 err = -ENOTDIR; 607 goto flags_out; 608 } 609 610 if (!ext4_empty_dir(inode)) { 611 err = -ENOTEMPTY; 612 goto flags_out; 613 } 614 } 615 616 /* 617 * Wait for all pending directio and then flush all the dirty pages 618 * for this file. The flush marks all the pages readonly, so any 619 * subsequent attempt to write to the file (particularly mmap pages) 620 * will come through the filesystem and fail. 621 */ 622 if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) && 623 (flags & EXT4_IMMUTABLE_FL)) { 624 inode_dio_wait(inode); 625 err = filemap_write_and_wait(inode->i_mapping); 626 if (err) 627 goto flags_out; 628 } 629 630 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 631 if (IS_ERR(handle)) { 632 err = PTR_ERR(handle); 633 goto flags_out; 634 } 635 if (IS_SYNC(inode)) 636 ext4_handle_sync(handle); 637 err = ext4_reserve_inode_write(handle, inode, &iloc); 638 if (err) 639 goto flags_err; 640 641 ext4_dax_dontcache(inode, flags); 642 643 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { 644 if (!(mask & EXT4_FL_USER_MODIFIABLE)) 645 continue; 646 /* These flags get special treatment later */ 647 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL) 648 continue; 649 if (mask & flags) 650 ext4_set_inode_flag(inode, i); 651 else 652 ext4_clear_inode_flag(inode, i); 653 } 654 655 ext4_set_inode_flags(inode, false); 656 657 inode->i_ctime = current_time(inode); 658 659 err = ext4_mark_iloc_dirty(handle, inode, &iloc); 660 flags_err: 661 ext4_journal_stop(handle); 662 if (err) 663 goto flags_out; 664 665 if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { 666 /* 667 * Changes to the journaling mode can cause unsafe changes to 668 * S_DAX if the inode is DAX 669 */ 670 if (IS_DAX(inode)) { 671 err = -EBUSY; 672 goto flags_out; 673 } 674 675 err = ext4_change_inode_journal_flag(inode, 676 flags & EXT4_JOURNAL_DATA_FL); 677 if (err) 678 goto flags_out; 679 } 680 if (migrate) { 681 if (flags & EXT4_EXTENTS_FL) 682 err = ext4_ext_migrate(inode); 683 else 684 err = ext4_ind_migrate(inode); 685 } 686 687 flags_out: 688 return err; 689 } 690 691 #ifdef CONFIG_QUOTA 692 static int ext4_ioctl_setproject(struct inode *inode, __u32 projid) 693 { 694 struct super_block *sb = inode->i_sb; 695 struct ext4_inode_info *ei = EXT4_I(inode); 696 int err, rc; 697 handle_t *handle; 698 kprojid_t kprojid; 699 struct ext4_iloc iloc; 700 struct ext4_inode *raw_inode; 701 struct dquot *transfer_to[MAXQUOTAS] = { }; 702 703 if (!ext4_has_feature_project(sb)) { 704 if (projid != EXT4_DEF_PROJID) 705 return -EOPNOTSUPP; 706 else 707 return 0; 708 } 709 710 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE) 711 return -EOPNOTSUPP; 712 713 kprojid = make_kprojid(&init_user_ns, (projid_t)projid); 714 715 if (projid_eq(kprojid, EXT4_I(inode)->i_projid)) 716 return 0; 717 718 err = -EPERM; 719 /* Is it quota file? Do not allow user to mess with it */ 720 if (ext4_is_quota_file(inode)) 721 return err; 722 723 err = ext4_get_inode_loc(inode, &iloc); 724 if (err) 725 return err; 726 727 raw_inode = ext4_raw_inode(&iloc); 728 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) { 729 err = ext4_expand_extra_isize(inode, 730 EXT4_SB(sb)->s_want_extra_isize, 731 &iloc); 732 if (err) 733 return err; 734 } else { 735 brelse(iloc.bh); 736 } 737 738 err = dquot_initialize(inode); 739 if (err) 740 return err; 741 742 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 743 EXT4_QUOTA_INIT_BLOCKS(sb) + 744 EXT4_QUOTA_DEL_BLOCKS(sb) + 3); 745 if (IS_ERR(handle)) 746 return PTR_ERR(handle); 747 748 err = ext4_reserve_inode_write(handle, inode, &iloc); 749 if (err) 750 goto out_stop; 751 752 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); 753 if (!IS_ERR(transfer_to[PRJQUOTA])) { 754 755 /* __dquot_transfer() calls back ext4_get_inode_usage() which 756 * counts xattr inode references. 757 */ 758 down_read(&EXT4_I(inode)->xattr_sem); 759 err = __dquot_transfer(inode, transfer_to); 760 up_read(&EXT4_I(inode)->xattr_sem); 761 dqput(transfer_to[PRJQUOTA]); 762 if (err) 763 goto out_dirty; 764 } 765 766 EXT4_I(inode)->i_projid = kprojid; 767 inode->i_ctime = current_time(inode); 768 out_dirty: 769 rc = ext4_mark_iloc_dirty(handle, inode, &iloc); 770 if (!err) 771 err = rc; 772 out_stop: 773 ext4_journal_stop(handle); 774 return err; 775 } 776 #else 777 static int ext4_ioctl_setproject(struct inode *inode, __u32 projid) 778 { 779 if (projid != EXT4_DEF_PROJID) 780 return -EOPNOTSUPP; 781 return 0; 782 } 783 #endif 784 785 static int ext4_shutdown(struct super_block *sb, unsigned long arg) 786 { 787 struct ext4_sb_info *sbi = EXT4_SB(sb); 788 __u32 flags; 789 790 if (!capable(CAP_SYS_ADMIN)) 791 return -EPERM; 792 793 if (get_user(flags, (__u32 __user *)arg)) 794 return -EFAULT; 795 796 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH) 797 return -EINVAL; 798 799 if (ext4_forced_shutdown(sbi)) 800 return 0; 801 802 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags); 803 trace_ext4_shutdown(sb, flags); 804 805 switch (flags) { 806 case EXT4_GOING_FLAGS_DEFAULT: 807 freeze_bdev(sb->s_bdev); 808 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); 809 thaw_bdev(sb->s_bdev); 810 break; 811 case EXT4_GOING_FLAGS_LOGFLUSH: 812 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); 813 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) { 814 (void) ext4_force_commit(sb); 815 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); 816 } 817 break; 818 case EXT4_GOING_FLAGS_NOLOGFLUSH: 819 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); 820 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) 821 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); 822 break; 823 default: 824 return -EINVAL; 825 } 826 clear_opt(sb, DISCARD); 827 return 0; 828 } 829 830 struct getfsmap_info { 831 struct super_block *gi_sb; 832 struct fsmap_head __user *gi_data; 833 unsigned int gi_idx; 834 __u32 gi_last_flags; 835 }; 836 837 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv) 838 { 839 struct getfsmap_info *info = priv; 840 struct fsmap fm; 841 842 trace_ext4_getfsmap_mapping(info->gi_sb, xfm); 843 844 info->gi_last_flags = xfm->fmr_flags; 845 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm); 846 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm, 847 sizeof(struct fsmap))) 848 return -EFAULT; 849 850 return 0; 851 } 852 853 static int ext4_ioc_getfsmap(struct super_block *sb, 854 struct fsmap_head __user *arg) 855 { 856 struct getfsmap_info info = { NULL }; 857 struct ext4_fsmap_head xhead = {0}; 858 struct fsmap_head head; 859 bool aborted = false; 860 int error; 861 862 if (copy_from_user(&head, arg, sizeof(struct fsmap_head))) 863 return -EFAULT; 864 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) || 865 memchr_inv(head.fmh_keys[0].fmr_reserved, 0, 866 sizeof(head.fmh_keys[0].fmr_reserved)) || 867 memchr_inv(head.fmh_keys[1].fmr_reserved, 0, 868 sizeof(head.fmh_keys[1].fmr_reserved))) 869 return -EINVAL; 870 /* 871 * ext4 doesn't report file extents at all, so the only valid 872 * file offsets are the magic ones (all zeroes or all ones). 873 */ 874 if (head.fmh_keys[0].fmr_offset || 875 (head.fmh_keys[1].fmr_offset != 0 && 876 head.fmh_keys[1].fmr_offset != -1ULL)) 877 return -EINVAL; 878 879 xhead.fmh_iflags = head.fmh_iflags; 880 xhead.fmh_count = head.fmh_count; 881 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]); 882 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]); 883 884 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]); 885 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]); 886 887 info.gi_sb = sb; 888 info.gi_data = arg; 889 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info); 890 if (error == EXT4_QUERY_RANGE_ABORT) 891 aborted = true; 892 else if (error) 893 return error; 894 895 /* If we didn't abort, set the "last" flag in the last fmx */ 896 if (!aborted && info.gi_idx) { 897 info.gi_last_flags |= FMR_OF_LAST; 898 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags, 899 &info.gi_last_flags, 900 sizeof(info.gi_last_flags))) 901 return -EFAULT; 902 } 903 904 /* copy back header */ 905 head.fmh_entries = xhead.fmh_entries; 906 head.fmh_oflags = xhead.fmh_oflags; 907 if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) 908 return -EFAULT; 909 910 return 0; 911 } 912 913 static long ext4_ioctl_group_add(struct file *file, 914 struct ext4_new_group_data *input) 915 { 916 struct super_block *sb = file_inode(file)->i_sb; 917 int err, err2=0; 918 919 err = ext4_resize_begin(sb); 920 if (err) 921 return err; 922 923 if (ext4_has_feature_bigalloc(sb)) { 924 ext4_msg(sb, KERN_ERR, 925 "Online resizing not supported with bigalloc"); 926 err = -EOPNOTSUPP; 927 goto group_add_out; 928 } 929 930 err = mnt_want_write_file(file); 931 if (err) 932 goto group_add_out; 933 934 err = ext4_group_add(sb, input); 935 if (EXT4_SB(sb)->s_journal) { 936 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 937 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0); 938 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 939 } 940 if (err == 0) 941 err = err2; 942 mnt_drop_write_file(file); 943 if (!err && ext4_has_group_desc_csum(sb) && 944 test_opt(sb, INIT_INODE_TABLE)) 945 err = ext4_register_li_request(sb, input->group); 946 group_add_out: 947 ext4_resize_end(sb); 948 return err; 949 } 950 951 int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa) 952 { 953 struct inode *inode = d_inode(dentry); 954 struct ext4_inode_info *ei = EXT4_I(inode); 955 u32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE; 956 957 if (S_ISREG(inode->i_mode)) 958 flags &= ~FS_PROJINHERIT_FL; 959 960 fileattr_fill_flags(fa, flags); 961 if (ext4_has_feature_project(inode->i_sb)) 962 fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid); 963 964 return 0; 965 } 966 967 int ext4_fileattr_set(struct user_namespace *mnt_userns, 968 struct dentry *dentry, struct fileattr *fa) 969 { 970 struct inode *inode = d_inode(dentry); 971 u32 flags = fa->flags; 972 int err = -EOPNOTSUPP; 973 974 if (flags & ~EXT4_FL_USER_VISIBLE) 975 goto out; 976 977 /* 978 * chattr(1) grabs flags via GETFLAGS, modifies the result and 979 * passes that to SETFLAGS. So we cannot easily make SETFLAGS 980 * more restrictive than just silently masking off visible but 981 * not settable flags as we always did. 982 */ 983 flags &= EXT4_FL_USER_MODIFIABLE; 984 if (ext4_mask_flags(inode->i_mode, flags) != flags) 985 goto out; 986 err = ext4_ioctl_check_immutable(inode, fa->fsx_projid, flags); 987 if (err) 988 goto out; 989 err = ext4_ioctl_setflags(inode, flags); 990 if (err) 991 goto out; 992 err = ext4_ioctl_setproject(inode, fa->fsx_projid); 993 out: 994 return err; 995 } 996 997 /* So that the fiemap access checks can't overflow on 32 bit machines. */ 998 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) 999 1000 static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg) 1001 { 1002 struct fiemap fiemap; 1003 struct fiemap __user *ufiemap = (struct fiemap __user *) arg; 1004 struct fiemap_extent_info fieinfo = { 0, }; 1005 struct inode *inode = file_inode(filp); 1006 int error; 1007 1008 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap))) 1009 return -EFAULT; 1010 1011 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) 1012 return -EINVAL; 1013 1014 fieinfo.fi_flags = fiemap.fm_flags; 1015 fieinfo.fi_extents_max = fiemap.fm_extent_count; 1016 fieinfo.fi_extents_start = ufiemap->fm_extents; 1017 1018 error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start, 1019 fiemap.fm_length); 1020 fiemap.fm_flags = fieinfo.fi_flags; 1021 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; 1022 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap))) 1023 error = -EFAULT; 1024 1025 return error; 1026 } 1027 1028 static int ext4_ioctl_checkpoint(struct file *filp, unsigned long arg) 1029 { 1030 int err = 0; 1031 __u32 flags = 0; 1032 unsigned int flush_flags = 0; 1033 struct super_block *sb = file_inode(filp)->i_sb; 1034 struct request_queue *q; 1035 1036 if (copy_from_user(&flags, (__u32 __user *)arg, 1037 sizeof(__u32))) 1038 return -EFAULT; 1039 1040 if (!capable(CAP_SYS_ADMIN)) 1041 return -EPERM; 1042 1043 /* check for invalid bits set */ 1044 if ((flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID) || 1045 ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && 1046 (flags & JBD2_JOURNAL_FLUSH_ZEROOUT))) 1047 return -EINVAL; 1048 1049 if (!EXT4_SB(sb)->s_journal) 1050 return -ENODEV; 1051 1052 if (flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID) 1053 return -EINVAL; 1054 1055 q = bdev_get_queue(EXT4_SB(sb)->s_journal->j_dev); 1056 if (!q) 1057 return -ENXIO; 1058 if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && !blk_queue_discard(q)) 1059 return -EOPNOTSUPP; 1060 1061 if (flags & EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN) 1062 return 0; 1063 1064 if (flags & EXT4_IOC_CHECKPOINT_FLAG_DISCARD) 1065 flush_flags |= JBD2_JOURNAL_FLUSH_DISCARD; 1066 1067 if (flags & EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT) { 1068 flush_flags |= JBD2_JOURNAL_FLUSH_ZEROOUT; 1069 pr_info_ratelimited("warning: checkpointing journal with EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT can be slow"); 1070 } 1071 1072 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 1073 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, flush_flags); 1074 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 1075 1076 return err; 1077 } 1078 1079 static int ext4_ioctl_setlabel(struct file *filp, const char __user *user_label) 1080 { 1081 size_t len; 1082 int ret = 0; 1083 char new_label[EXT4_LABEL_MAX + 1]; 1084 struct super_block *sb = file_inode(filp)->i_sb; 1085 1086 if (!capable(CAP_SYS_ADMIN)) 1087 return -EPERM; 1088 1089 /* 1090 * Copy the maximum length allowed for ext4 label with one more to 1091 * find the required terminating null byte in order to test the 1092 * label length. The on disk label doesn't need to be null terminated. 1093 */ 1094 if (copy_from_user(new_label, user_label, EXT4_LABEL_MAX + 1)) 1095 return -EFAULT; 1096 1097 len = strnlen(new_label, EXT4_LABEL_MAX + 1); 1098 if (len > EXT4_LABEL_MAX) 1099 return -EINVAL; 1100 1101 /* 1102 * Clear the buffer after the new label 1103 */ 1104 memset(new_label + len, 0, EXT4_LABEL_MAX - len); 1105 1106 ret = mnt_want_write_file(filp); 1107 if (ret) 1108 return ret; 1109 1110 ret = ext4_update_superblocks_fn(sb, ext4_sb_setlabel, new_label); 1111 1112 mnt_drop_write_file(filp); 1113 return ret; 1114 } 1115 1116 static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label) 1117 { 1118 char label[EXT4_LABEL_MAX + 1]; 1119 1120 /* 1121 * EXT4_LABEL_MAX must always be smaller than FSLABEL_MAX because 1122 * FSLABEL_MAX must include terminating null byte, while s_volume_name 1123 * does not have to. 1124 */ 1125 BUILD_BUG_ON(EXT4_LABEL_MAX >= FSLABEL_MAX); 1126 1127 memset(label, 0, sizeof(label)); 1128 lock_buffer(sbi->s_sbh); 1129 strncpy(label, sbi->s_es->s_volume_name, EXT4_LABEL_MAX); 1130 unlock_buffer(sbi->s_sbh); 1131 1132 if (copy_to_user(user_label, label, sizeof(label))) 1133 return -EFAULT; 1134 return 0; 1135 } 1136 1137 static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1138 { 1139 struct inode *inode = file_inode(filp); 1140 struct super_block *sb = inode->i_sb; 1141 struct user_namespace *mnt_userns = file_mnt_user_ns(filp); 1142 1143 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); 1144 1145 switch (cmd) { 1146 case FS_IOC_GETFSMAP: 1147 return ext4_ioc_getfsmap(sb, (void __user *)arg); 1148 case EXT4_IOC_GETVERSION: 1149 case EXT4_IOC_GETVERSION_OLD: 1150 return put_user(inode->i_generation, (int __user *) arg); 1151 case EXT4_IOC_SETVERSION: 1152 case EXT4_IOC_SETVERSION_OLD: { 1153 handle_t *handle; 1154 struct ext4_iloc iloc; 1155 __u32 generation; 1156 int err; 1157 1158 if (!inode_owner_or_capable(mnt_userns, inode)) 1159 return -EPERM; 1160 1161 if (ext4_has_metadata_csum(inode->i_sb)) { 1162 ext4_warning(sb, "Setting inode version is not " 1163 "supported with metadata_csum enabled."); 1164 return -ENOTTY; 1165 } 1166 1167 err = mnt_want_write_file(filp); 1168 if (err) 1169 return err; 1170 if (get_user(generation, (int __user *) arg)) { 1171 err = -EFAULT; 1172 goto setversion_out; 1173 } 1174 1175 inode_lock(inode); 1176 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 1177 if (IS_ERR(handle)) { 1178 err = PTR_ERR(handle); 1179 goto unlock_out; 1180 } 1181 err = ext4_reserve_inode_write(handle, inode, &iloc); 1182 if (err == 0) { 1183 inode->i_ctime = current_time(inode); 1184 inode->i_generation = generation; 1185 err = ext4_mark_iloc_dirty(handle, inode, &iloc); 1186 } 1187 ext4_journal_stop(handle); 1188 1189 unlock_out: 1190 inode_unlock(inode); 1191 setversion_out: 1192 mnt_drop_write_file(filp); 1193 return err; 1194 } 1195 case EXT4_IOC_GROUP_EXTEND: { 1196 ext4_fsblk_t n_blocks_count; 1197 int err, err2=0; 1198 1199 err = ext4_resize_begin(sb); 1200 if (err) 1201 return err; 1202 1203 if (get_user(n_blocks_count, (__u32 __user *)arg)) { 1204 err = -EFAULT; 1205 goto group_extend_out; 1206 } 1207 1208 if (ext4_has_feature_bigalloc(sb)) { 1209 ext4_msg(sb, KERN_ERR, 1210 "Online resizing not supported with bigalloc"); 1211 err = -EOPNOTSUPP; 1212 goto group_extend_out; 1213 } 1214 1215 err = mnt_want_write_file(filp); 1216 if (err) 1217 goto group_extend_out; 1218 1219 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); 1220 if (EXT4_SB(sb)->s_journal) { 1221 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 1222 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0); 1223 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 1224 } 1225 if (err == 0) 1226 err = err2; 1227 mnt_drop_write_file(filp); 1228 group_extend_out: 1229 ext4_resize_end(sb); 1230 return err; 1231 } 1232 1233 case EXT4_IOC_MOVE_EXT: { 1234 struct move_extent me; 1235 struct fd donor; 1236 int err; 1237 1238 if (!(filp->f_mode & FMODE_READ) || 1239 !(filp->f_mode & FMODE_WRITE)) 1240 return -EBADF; 1241 1242 if (copy_from_user(&me, 1243 (struct move_extent __user *)arg, sizeof(me))) 1244 return -EFAULT; 1245 me.moved_len = 0; 1246 1247 donor = fdget(me.donor_fd); 1248 if (!donor.file) 1249 return -EBADF; 1250 1251 if (!(donor.file->f_mode & FMODE_WRITE)) { 1252 err = -EBADF; 1253 goto mext_out; 1254 } 1255 1256 if (ext4_has_feature_bigalloc(sb)) { 1257 ext4_msg(sb, KERN_ERR, 1258 "Online defrag not supported with bigalloc"); 1259 err = -EOPNOTSUPP; 1260 goto mext_out; 1261 } else if (IS_DAX(inode)) { 1262 ext4_msg(sb, KERN_ERR, 1263 "Online defrag not supported with DAX"); 1264 err = -EOPNOTSUPP; 1265 goto mext_out; 1266 } 1267 1268 err = mnt_want_write_file(filp); 1269 if (err) 1270 goto mext_out; 1271 1272 err = ext4_move_extents(filp, donor.file, me.orig_start, 1273 me.donor_start, me.len, &me.moved_len); 1274 mnt_drop_write_file(filp); 1275 1276 if (copy_to_user((struct move_extent __user *)arg, 1277 &me, sizeof(me))) 1278 err = -EFAULT; 1279 mext_out: 1280 fdput(donor); 1281 return err; 1282 } 1283 1284 case EXT4_IOC_GROUP_ADD: { 1285 struct ext4_new_group_data input; 1286 1287 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, 1288 sizeof(input))) 1289 return -EFAULT; 1290 1291 return ext4_ioctl_group_add(filp, &input); 1292 } 1293 1294 case EXT4_IOC_MIGRATE: 1295 { 1296 int err; 1297 if (!inode_owner_or_capable(mnt_userns, inode)) 1298 return -EACCES; 1299 1300 err = mnt_want_write_file(filp); 1301 if (err) 1302 return err; 1303 /* 1304 * inode_mutex prevent write and truncate on the file. 1305 * Read still goes through. We take i_data_sem in 1306 * ext4_ext_swap_inode_data before we switch the 1307 * inode format to prevent read. 1308 */ 1309 inode_lock((inode)); 1310 err = ext4_ext_migrate(inode); 1311 inode_unlock((inode)); 1312 mnt_drop_write_file(filp); 1313 return err; 1314 } 1315 1316 case EXT4_IOC_ALLOC_DA_BLKS: 1317 { 1318 int err; 1319 if (!inode_owner_or_capable(mnt_userns, inode)) 1320 return -EACCES; 1321 1322 err = mnt_want_write_file(filp); 1323 if (err) 1324 return err; 1325 err = ext4_alloc_da_blocks(inode); 1326 mnt_drop_write_file(filp); 1327 return err; 1328 } 1329 1330 case EXT4_IOC_SWAP_BOOT: 1331 { 1332 int err; 1333 if (!(filp->f_mode & FMODE_WRITE)) 1334 return -EBADF; 1335 err = mnt_want_write_file(filp); 1336 if (err) 1337 return err; 1338 err = swap_inode_boot_loader(sb, mnt_userns, inode); 1339 mnt_drop_write_file(filp); 1340 return err; 1341 } 1342 1343 case EXT4_IOC_RESIZE_FS: { 1344 ext4_fsblk_t n_blocks_count; 1345 int err = 0, err2 = 0; 1346 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; 1347 1348 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, 1349 sizeof(__u64))) { 1350 return -EFAULT; 1351 } 1352 1353 err = ext4_resize_begin(sb); 1354 if (err) 1355 return err; 1356 1357 err = mnt_want_write_file(filp); 1358 if (err) 1359 goto resizefs_out; 1360 1361 err = ext4_resize_fs(sb, n_blocks_count); 1362 if (EXT4_SB(sb)->s_journal) { 1363 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_RESIZE, NULL); 1364 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 1365 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0); 1366 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 1367 } 1368 if (err == 0) 1369 err = err2; 1370 mnt_drop_write_file(filp); 1371 if (!err && (o_group < EXT4_SB(sb)->s_groups_count) && 1372 ext4_has_group_desc_csum(sb) && 1373 test_opt(sb, INIT_INODE_TABLE)) 1374 err = ext4_register_li_request(sb, o_group); 1375 1376 resizefs_out: 1377 ext4_resize_end(sb); 1378 return err; 1379 } 1380 1381 case FITRIM: 1382 { 1383 struct request_queue *q = bdev_get_queue(sb->s_bdev); 1384 struct fstrim_range range; 1385 int ret = 0; 1386 1387 if (!capable(CAP_SYS_ADMIN)) 1388 return -EPERM; 1389 1390 if (!blk_queue_discard(q)) 1391 return -EOPNOTSUPP; 1392 1393 /* 1394 * We haven't replayed the journal, so we cannot use our 1395 * block-bitmap-guided storage zapping commands. 1396 */ 1397 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) 1398 return -EROFS; 1399 1400 if (copy_from_user(&range, (struct fstrim_range __user *)arg, 1401 sizeof(range))) 1402 return -EFAULT; 1403 1404 ret = ext4_trim_fs(sb, &range); 1405 if (ret < 0) 1406 return ret; 1407 1408 if (copy_to_user((struct fstrim_range __user *)arg, &range, 1409 sizeof(range))) 1410 return -EFAULT; 1411 1412 return 0; 1413 } 1414 case EXT4_IOC_PRECACHE_EXTENTS: 1415 return ext4_ext_precache(inode); 1416 1417 case FS_IOC_SET_ENCRYPTION_POLICY: 1418 if (!ext4_has_feature_encrypt(sb)) 1419 return -EOPNOTSUPP; 1420 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); 1421 1422 case FS_IOC_GET_ENCRYPTION_PWSALT: 1423 return ext4_ioctl_get_encryption_pwsalt(filp, (void __user *)arg); 1424 1425 case FS_IOC_GET_ENCRYPTION_POLICY: 1426 if (!ext4_has_feature_encrypt(sb)) 1427 return -EOPNOTSUPP; 1428 return fscrypt_ioctl_get_policy(filp, (void __user *)arg); 1429 1430 case FS_IOC_GET_ENCRYPTION_POLICY_EX: 1431 if (!ext4_has_feature_encrypt(sb)) 1432 return -EOPNOTSUPP; 1433 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg); 1434 1435 case FS_IOC_ADD_ENCRYPTION_KEY: 1436 if (!ext4_has_feature_encrypt(sb)) 1437 return -EOPNOTSUPP; 1438 return fscrypt_ioctl_add_key(filp, (void __user *)arg); 1439 1440 case FS_IOC_REMOVE_ENCRYPTION_KEY: 1441 if (!ext4_has_feature_encrypt(sb)) 1442 return -EOPNOTSUPP; 1443 return fscrypt_ioctl_remove_key(filp, (void __user *)arg); 1444 1445 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: 1446 if (!ext4_has_feature_encrypt(sb)) 1447 return -EOPNOTSUPP; 1448 return fscrypt_ioctl_remove_key_all_users(filp, 1449 (void __user *)arg); 1450 case FS_IOC_GET_ENCRYPTION_KEY_STATUS: 1451 if (!ext4_has_feature_encrypt(sb)) 1452 return -EOPNOTSUPP; 1453 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg); 1454 1455 case FS_IOC_GET_ENCRYPTION_NONCE: 1456 if (!ext4_has_feature_encrypt(sb)) 1457 return -EOPNOTSUPP; 1458 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg); 1459 1460 case EXT4_IOC_CLEAR_ES_CACHE: 1461 { 1462 if (!inode_owner_or_capable(mnt_userns, inode)) 1463 return -EACCES; 1464 ext4_clear_inode_es(inode); 1465 return 0; 1466 } 1467 1468 case EXT4_IOC_GETSTATE: 1469 { 1470 __u32 state = 0; 1471 1472 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED)) 1473 state |= EXT4_STATE_FLAG_EXT_PRECACHED; 1474 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 1475 state |= EXT4_STATE_FLAG_NEW; 1476 if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) 1477 state |= EXT4_STATE_FLAG_NEWENTRY; 1478 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) 1479 state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE; 1480 1481 return put_user(state, (__u32 __user *) arg); 1482 } 1483 1484 case EXT4_IOC_GET_ES_CACHE: 1485 return ext4_ioctl_get_es_cache(filp, arg); 1486 1487 case EXT4_IOC_SHUTDOWN: 1488 return ext4_shutdown(sb, arg); 1489 1490 case FS_IOC_ENABLE_VERITY: 1491 if (!ext4_has_feature_verity(sb)) 1492 return -EOPNOTSUPP; 1493 return fsverity_ioctl_enable(filp, (const void __user *)arg); 1494 1495 case FS_IOC_MEASURE_VERITY: 1496 if (!ext4_has_feature_verity(sb)) 1497 return -EOPNOTSUPP; 1498 return fsverity_ioctl_measure(filp, (void __user *)arg); 1499 1500 case FS_IOC_READ_VERITY_METADATA: 1501 if (!ext4_has_feature_verity(sb)) 1502 return -EOPNOTSUPP; 1503 return fsverity_ioctl_read_metadata(filp, 1504 (const void __user *)arg); 1505 1506 case EXT4_IOC_CHECKPOINT: 1507 return ext4_ioctl_checkpoint(filp, arg); 1508 1509 case FS_IOC_GETFSLABEL: 1510 return ext4_ioctl_getlabel(EXT4_SB(sb), (void __user *)arg); 1511 1512 case FS_IOC_SETFSLABEL: 1513 return ext4_ioctl_setlabel(filp, 1514 (const void __user *)arg); 1515 1516 default: 1517 return -ENOTTY; 1518 } 1519 } 1520 1521 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1522 { 1523 return __ext4_ioctl(filp, cmd, arg); 1524 } 1525 1526 #ifdef CONFIG_COMPAT 1527 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1528 { 1529 /* These are just misnamed, they actually get/put from/to user an int */ 1530 switch (cmd) { 1531 case EXT4_IOC32_GETVERSION: 1532 cmd = EXT4_IOC_GETVERSION; 1533 break; 1534 case EXT4_IOC32_SETVERSION: 1535 cmd = EXT4_IOC_SETVERSION; 1536 break; 1537 case EXT4_IOC32_GROUP_EXTEND: 1538 cmd = EXT4_IOC_GROUP_EXTEND; 1539 break; 1540 case EXT4_IOC32_GETVERSION_OLD: 1541 cmd = EXT4_IOC_GETVERSION_OLD; 1542 break; 1543 case EXT4_IOC32_SETVERSION_OLD: 1544 cmd = EXT4_IOC_SETVERSION_OLD; 1545 break; 1546 case EXT4_IOC32_GETRSVSZ: 1547 cmd = EXT4_IOC_GETRSVSZ; 1548 break; 1549 case EXT4_IOC32_SETRSVSZ: 1550 cmd = EXT4_IOC_SETRSVSZ; 1551 break; 1552 case EXT4_IOC32_GROUP_ADD: { 1553 struct compat_ext4_new_group_input __user *uinput; 1554 struct ext4_new_group_data input; 1555 int err; 1556 1557 uinput = compat_ptr(arg); 1558 err = get_user(input.group, &uinput->group); 1559 err |= get_user(input.block_bitmap, &uinput->block_bitmap); 1560 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); 1561 err |= get_user(input.inode_table, &uinput->inode_table); 1562 err |= get_user(input.blocks_count, &uinput->blocks_count); 1563 err |= get_user(input.reserved_blocks, 1564 &uinput->reserved_blocks); 1565 if (err) 1566 return -EFAULT; 1567 return ext4_ioctl_group_add(file, &input); 1568 } 1569 case EXT4_IOC_MOVE_EXT: 1570 case EXT4_IOC_RESIZE_FS: 1571 case FITRIM: 1572 case EXT4_IOC_PRECACHE_EXTENTS: 1573 case FS_IOC_SET_ENCRYPTION_POLICY: 1574 case FS_IOC_GET_ENCRYPTION_PWSALT: 1575 case FS_IOC_GET_ENCRYPTION_POLICY: 1576 case FS_IOC_GET_ENCRYPTION_POLICY_EX: 1577 case FS_IOC_ADD_ENCRYPTION_KEY: 1578 case FS_IOC_REMOVE_ENCRYPTION_KEY: 1579 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: 1580 case FS_IOC_GET_ENCRYPTION_KEY_STATUS: 1581 case FS_IOC_GET_ENCRYPTION_NONCE: 1582 case EXT4_IOC_SHUTDOWN: 1583 case FS_IOC_GETFSMAP: 1584 case FS_IOC_ENABLE_VERITY: 1585 case FS_IOC_MEASURE_VERITY: 1586 case FS_IOC_READ_VERITY_METADATA: 1587 case EXT4_IOC_CLEAR_ES_CACHE: 1588 case EXT4_IOC_GETSTATE: 1589 case EXT4_IOC_GET_ES_CACHE: 1590 case EXT4_IOC_CHECKPOINT: 1591 case FS_IOC_GETFSLABEL: 1592 case FS_IOC_SETFSLABEL: 1593 break; 1594 default: 1595 return -ENOIOCTLCMD; 1596 } 1597 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 1598 } 1599 #endif 1600 1601 static void set_overhead(struct ext4_super_block *es, const void *arg) 1602 { 1603 es->s_overhead_clusters = cpu_to_le32(*((unsigned long *) arg)); 1604 } 1605 1606 int ext4_update_overhead(struct super_block *sb) 1607 { 1608 struct ext4_sb_info *sbi = EXT4_SB(sb); 1609 1610 if (sb_rdonly(sb) || sbi->s_overhead == 0 || 1611 sbi->s_overhead == le32_to_cpu(sbi->s_es->s_overhead_clusters)) 1612 return 0; 1613 1614 return ext4_update_superblocks_fn(sb, set_overhead, &sbi->s_overhead); 1615 } 1616