0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/fs.h>
0012 #include <linux/capability.h>
0013 #include <linux/time.h>
0014 #include <linux/compat.h>
0015 #include <linux/mount.h>
0016 #include <linux/file.h>
0017 #include <linux/quotaops.h>
0018 #include <linux/random.h>
0019 #include <linux/uaccess.h>
0020 #include <linux/delay.h>
0021 #include <linux/iversion.h>
0022 #include <linux/fileattr.h>
0023 #include <linux/uuid.h>
0024 #include "ext4_jbd2.h"
0025 #include "ext4.h"
0026 #include <linux/fsmap.h>
0027 #include "fsmap.h"
0028 #include <trace/events/ext4.h>
0029
0030 typedef void ext4_update_sb_callback(struct ext4_super_block *es,
0031 const void *arg);
0032
0033
0034
0035
0036
0037 static void ext4_sb_setlabel(struct ext4_super_block *es, const void *arg)
0038 {
0039
0040 BUILD_BUG_ON(sizeof(es->s_volume_name) < EXT4_LABEL_MAX);
0041
0042 memcpy(es->s_volume_name, (char *)arg, EXT4_LABEL_MAX);
0043 }
0044
0045
0046
0047
0048
0049 static void ext4_sb_setuuid(struct ext4_super_block *es, const void *arg)
0050 {
0051 memcpy(es->s_uuid, (__u8 *)arg, UUID_SIZE);
0052 }
0053
0054 static
0055 int ext4_update_primary_sb(struct super_block *sb, handle_t *handle,
0056 ext4_update_sb_callback func,
0057 const void *arg)
0058 {
0059 int err = 0;
0060 struct ext4_sb_info *sbi = EXT4_SB(sb);
0061 struct buffer_head *bh = sbi->s_sbh;
0062 struct ext4_super_block *es = sbi->s_es;
0063
0064 trace_ext4_update_sb(sb, bh->b_blocknr, 1);
0065
0066 BUFFER_TRACE(bh, "get_write_access");
0067 err = ext4_journal_get_write_access(handle, sb,
0068 bh,
0069 EXT4_JTR_NONE);
0070 if (err)
0071 goto out_err;
0072
0073 lock_buffer(bh);
0074 func(es, arg);
0075 ext4_superblock_csum_set(sb);
0076 unlock_buffer(bh);
0077
0078 if (buffer_write_io_error(bh) || !buffer_uptodate(bh)) {
0079 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
0080 "superblock detected");
0081 clear_buffer_write_io_error(bh);
0082 set_buffer_uptodate(bh);
0083 }
0084
0085 err = ext4_handle_dirty_metadata(handle, NULL, bh);
0086 if (err)
0087 goto out_err;
0088 err = sync_dirty_buffer(bh);
0089 out_err:
0090 ext4_std_error(sb, err);
0091 return err;
0092 }
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 static int ext4_update_backup_sb(struct super_block *sb,
0104 handle_t *handle, ext4_group_t grp,
0105 ext4_update_sb_callback func, const void *arg)
0106 {
0107 int err = 0;
0108 ext4_fsblk_t sb_block;
0109 struct buffer_head *bh;
0110 unsigned long offset = 0;
0111 struct ext4_super_block *es;
0112
0113 if (!ext4_bg_has_super(sb, grp))
0114 return 0;
0115
0116
0117
0118
0119
0120 if (grp == 0) {
0121 sb_block = 1 * EXT4_MIN_BLOCK_SIZE;
0122 offset = do_div(sb_block, sb->s_blocksize);
0123 } else {
0124 sb_block = ext4_group_first_block_no(sb, grp);
0125 offset = 0;
0126 }
0127
0128 trace_ext4_update_sb(sb, sb_block, handle ? 1 : 0);
0129
0130 bh = ext4_sb_bread(sb, sb_block, 0);
0131 if (IS_ERR(bh))
0132 return PTR_ERR(bh);
0133
0134 if (handle) {
0135 BUFFER_TRACE(bh, "get_write_access");
0136 err = ext4_journal_get_write_access(handle, sb,
0137 bh,
0138 EXT4_JTR_NONE);
0139 if (err)
0140 goto out_bh;
0141 }
0142
0143 es = (struct ext4_super_block *) (bh->b_data + offset);
0144 lock_buffer(bh);
0145 if (ext4_has_metadata_csum(sb) &&
0146 es->s_checksum != ext4_superblock_csum(sb, es)) {
0147 ext4_msg(sb, KERN_ERR, "Invalid checksum for backup "
0148 "superblock %llu\n", sb_block);
0149 unlock_buffer(bh);
0150 err = -EFSBADCRC;
0151 goto out_bh;
0152 }
0153 func(es, arg);
0154 if (ext4_has_metadata_csum(sb))
0155 es->s_checksum = ext4_superblock_csum(sb, es);
0156 set_buffer_uptodate(bh);
0157 unlock_buffer(bh);
0158
0159 if (err)
0160 goto out_bh;
0161
0162 if (handle) {
0163 err = ext4_handle_dirty_metadata(handle, NULL, bh);
0164 if (err)
0165 goto out_bh;
0166 } else {
0167 BUFFER_TRACE(bh, "marking dirty");
0168 mark_buffer_dirty(bh);
0169 }
0170 err = sync_dirty_buffer(bh);
0171
0172 out_bh:
0173 brelse(bh);
0174 ext4_std_error(sb, err);
0175 return (err) ? err : 1;
0176 }
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 static
0188 int ext4_update_superblocks_fn(struct super_block *sb,
0189 ext4_update_sb_callback func,
0190 const void *arg)
0191 {
0192 handle_t *handle;
0193 ext4_group_t ngroups;
0194 unsigned int three = 1;
0195 unsigned int five = 5;
0196 unsigned int seven = 7;
0197 int err = 0, ret, i;
0198 ext4_group_t grp, primary_grp;
0199 struct ext4_sb_info *sbi = EXT4_SB(sb);
0200
0201
0202
0203
0204 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
0205 &sbi->s_ext4_flags)) {
0206 ext4_msg(sb, KERN_ERR, "Can't modify superblock while"
0207 "performing online resize");
0208 return -EBUSY;
0209 }
0210
0211
0212
0213
0214
0215 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 3);
0216 if (IS_ERR(handle)) {
0217 err = PTR_ERR(handle);
0218 goto out;
0219 }
0220
0221
0222 err = ext4_update_primary_sb(sb, handle, func, arg);
0223 if (err) {
0224 ext4_msg(sb, KERN_ERR, "Failed to update primary "
0225 "superblock");
0226 goto out_journal;
0227 }
0228
0229 primary_grp = ext4_get_group_number(sb, sbi->s_sbh->b_blocknr);
0230 ngroups = ext4_get_groups_count(sb);
0231
0232
0233
0234
0235
0236
0237 i = 0;
0238 grp = 0;
0239 while (grp < ngroups) {
0240
0241 if (grp == primary_grp)
0242 goto next_grp;
0243
0244 ret = ext4_update_backup_sb(sb, handle, grp, func, arg);
0245 if (ret < 0) {
0246
0247 if (ret == -EFSBADCRC)
0248 goto next_grp;
0249 err = ret;
0250 goto out_journal;
0251 }
0252
0253 i += ret;
0254 if (handle && i > 1) {
0255
0256
0257
0258
0259
0260 err = ext4_journal_stop(handle);
0261 if (err)
0262 goto out;
0263 handle = NULL;
0264 }
0265 next_grp:
0266 grp = ext4_list_backups(sb, &three, &five, &seven);
0267 }
0268
0269 out_journal:
0270 if (handle) {
0271 ret = ext4_journal_stop(handle);
0272 if (ret && !err)
0273 err = ret;
0274 }
0275 out:
0276 clear_bit_unlock(EXT4_FLAGS_RESIZING, &sbi->s_ext4_flags);
0277 smp_mb__after_atomic();
0278 return err ? err : 0;
0279 }
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289 static void memswap(void *a, void *b, size_t len)
0290 {
0291 unsigned char *ap, *bp;
0292
0293 ap = (unsigned char *)a;
0294 bp = (unsigned char *)b;
0295 while (len-- > 0) {
0296 swap(*ap, *bp);
0297 ap++;
0298 bp++;
0299 }
0300 }
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
0314 {
0315 loff_t isize;
0316 struct ext4_inode_info *ei1;
0317 struct ext4_inode_info *ei2;
0318 unsigned long tmp;
0319
0320 ei1 = EXT4_I(inode1);
0321 ei2 = EXT4_I(inode2);
0322
0323 swap(inode1->i_version, inode2->i_version);
0324 swap(inode1->i_atime, inode2->i_atime);
0325 swap(inode1->i_mtime, inode2->i_mtime);
0326
0327 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
0328 tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP;
0329 ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) |
0330 (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP);
0331 ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP);
0332 swap(ei1->i_disksize, ei2->i_disksize);
0333 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
0334 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
0335
0336 isize = i_size_read(inode1);
0337 i_size_write(inode1, i_size_read(inode2));
0338 i_size_write(inode2, isize);
0339 }
0340
0341 void ext4_reset_inode_seed(struct inode *inode)
0342 {
0343 struct ext4_inode_info *ei = EXT4_I(inode);
0344 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
0345 __le32 inum = cpu_to_le32(inode->i_ino);
0346 __le32 gen = cpu_to_le32(inode->i_generation);
0347 __u32 csum;
0348
0349 if (!ext4_has_metadata_csum(inode->i_sb))
0350 return;
0351
0352 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
0353 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
0354 }
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366 static long swap_inode_boot_loader(struct super_block *sb,
0367 struct user_namespace *mnt_userns,
0368 struct inode *inode)
0369 {
0370 handle_t *handle;
0371 int err;
0372 struct inode *inode_bl;
0373 struct ext4_inode_info *ei_bl;
0374 qsize_t size, size_bl, diff;
0375 blkcnt_t blocks;
0376 unsigned short bytes;
0377
0378 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
0379 if (IS_ERR(inode_bl))
0380 return PTR_ERR(inode_bl);
0381 ei_bl = EXT4_I(inode_bl);
0382
0383
0384
0385 lock_two_nondirectories(inode, inode_bl);
0386
0387 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
0388 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
0389 (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) ||
0390 ext4_has_inline_data(inode)) {
0391 err = -EINVAL;
0392 goto journal_err_out;
0393 }
0394
0395 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
0396 !inode_owner_or_capable(mnt_userns, inode) ||
0397 !capable(CAP_SYS_ADMIN)) {
0398 err = -EPERM;
0399 goto journal_err_out;
0400 }
0401
0402 filemap_invalidate_lock(inode->i_mapping);
0403 err = filemap_write_and_wait(inode->i_mapping);
0404 if (err)
0405 goto err_out;
0406
0407 err = filemap_write_and_wait(inode_bl->i_mapping);
0408 if (err)
0409 goto err_out;
0410
0411
0412 inode_dio_wait(inode);
0413 inode_dio_wait(inode_bl);
0414
0415 truncate_inode_pages(&inode->i_data, 0);
0416 truncate_inode_pages(&inode_bl->i_data, 0);
0417
0418 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
0419 if (IS_ERR(handle)) {
0420 err = -EINVAL;
0421 goto err_out;
0422 }
0423 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_SWAP_BOOT, handle);
0424
0425
0426 ext4_double_down_write_data_sem(inode, inode_bl);
0427
0428 if (inode_bl->i_nlink == 0) {
0429
0430 set_nlink(inode_bl, 1);
0431 i_uid_write(inode_bl, 0);
0432 i_gid_write(inode_bl, 0);
0433 inode_bl->i_flags = 0;
0434 ei_bl->i_flags = 0;
0435 inode_set_iversion(inode_bl, 1);
0436 i_size_write(inode_bl, 0);
0437 inode_bl->i_mode = S_IFREG;
0438 if (ext4_has_feature_extents(sb)) {
0439 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
0440 ext4_ext_tree_init(handle, inode_bl);
0441 } else
0442 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
0443 }
0444
0445 err = dquot_initialize(inode);
0446 if (err)
0447 goto err_out1;
0448
0449 size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes;
0450 size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes;
0451 diff = size - size_bl;
0452 swap_inode_data(inode, inode_bl);
0453
0454 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
0455
0456 inode->i_generation = prandom_u32();
0457 inode_bl->i_generation = prandom_u32();
0458 ext4_reset_inode_seed(inode);
0459 ext4_reset_inode_seed(inode_bl);
0460
0461 ext4_discard_preallocations(inode, 0);
0462
0463 err = ext4_mark_inode_dirty(handle, inode);
0464 if (err < 0) {
0465
0466 ext4_warning(inode->i_sb,
0467 "couldn't mark inode #%lu dirty (err %d)",
0468 inode->i_ino, err);
0469
0470 swap_inode_data(inode, inode_bl);
0471 ext4_mark_inode_dirty(handle, inode);
0472 goto err_out1;
0473 }
0474
0475 blocks = inode_bl->i_blocks;
0476 bytes = inode_bl->i_bytes;
0477 inode_bl->i_blocks = inode->i_blocks;
0478 inode_bl->i_bytes = inode->i_bytes;
0479 err = ext4_mark_inode_dirty(handle, inode_bl);
0480 if (err < 0) {
0481
0482 ext4_warning(inode_bl->i_sb,
0483 "couldn't mark inode #%lu dirty (err %d)",
0484 inode_bl->i_ino, err);
0485 goto revert;
0486 }
0487
0488
0489 if (diff > 0)
0490 dquot_free_space(inode, diff);
0491 else
0492 err = dquot_alloc_space(inode, -1 * diff);
0493
0494 if (err < 0) {
0495 revert:
0496
0497 inode_bl->i_blocks = blocks;
0498 inode_bl->i_bytes = bytes;
0499 swap_inode_data(inode, inode_bl);
0500 ext4_mark_inode_dirty(handle, inode);
0501 ext4_mark_inode_dirty(handle, inode_bl);
0502 }
0503
0504 err_out1:
0505 ext4_journal_stop(handle);
0506 ext4_double_up_write_data_sem(inode, inode_bl);
0507
0508 err_out:
0509 filemap_invalidate_unlock(inode->i_mapping);
0510 journal_err_out:
0511 unlock_two_nondirectories(inode, inode_bl);
0512 iput(inode_bl);
0513 return err;
0514 }
0515
0516
0517
0518
0519
0520
0521 static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid,
0522 unsigned int flags)
0523 {
0524 struct ext4_inode_info *ei = EXT4_I(inode);
0525 unsigned int oldflags = ei->i_flags;
0526
0527 if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL))
0528 return 0;
0529
0530 if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL))
0531 return -EPERM;
0532 if (ext4_has_feature_project(inode->i_sb) &&
0533 __kprojid_val(ei->i_projid) != new_projid)
0534 return -EPERM;
0535
0536 return 0;
0537 }
0538
0539 static void ext4_dax_dontcache(struct inode *inode, unsigned int flags)
0540 {
0541 struct ext4_inode_info *ei = EXT4_I(inode);
0542
0543 if (S_ISDIR(inode->i_mode))
0544 return;
0545
0546 if (test_opt2(inode->i_sb, DAX_NEVER) ||
0547 test_opt(inode->i_sb, DAX_ALWAYS))
0548 return;
0549
0550 if ((ei->i_flags ^ flags) & EXT4_DAX_FL)
0551 d_mark_dontcache(inode);
0552 }
0553
0554 static bool dax_compatible(struct inode *inode, unsigned int oldflags,
0555 unsigned int flags)
0556 {
0557
0558 if (S_ISDIR(inode->i_mode)) {
0559 flags &= ~EXT4_INLINE_DATA_FL;
0560 oldflags &= ~EXT4_INLINE_DATA_FL;
0561 }
0562
0563 if (flags & EXT4_DAX_FL) {
0564 if ((oldflags & EXT4_DAX_MUT_EXCL) ||
0565 ext4_test_inode_state(inode,
0566 EXT4_STATE_VERITY_IN_PROGRESS)) {
0567 return false;
0568 }
0569 }
0570
0571 if ((flags & EXT4_DAX_MUT_EXCL) && (oldflags & EXT4_DAX_FL))
0572 return false;
0573
0574 return true;
0575 }
0576
0577 static int ext4_ioctl_setflags(struct inode *inode,
0578 unsigned int flags)
0579 {
0580 struct ext4_inode_info *ei = EXT4_I(inode);
0581 handle_t *handle = NULL;
0582 int err = -EPERM, migrate = 0;
0583 struct ext4_iloc iloc;
0584 unsigned int oldflags, mask, i;
0585 struct super_block *sb = inode->i_sb;
0586
0587
0588 if (ext4_is_quota_file(inode))
0589 goto flags_out;
0590
0591 oldflags = ei->i_flags;
0592
0593
0594
0595
0596 if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
0597 if (!capable(CAP_SYS_RESOURCE))
0598 goto flags_out;
0599 }
0600
0601 if (!dax_compatible(inode, oldflags, flags)) {
0602 err = -EOPNOTSUPP;
0603 goto flags_out;
0604 }
0605
0606 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
0607 migrate = 1;
0608
0609 if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
0610 if (!ext4_has_feature_casefold(sb)) {
0611 err = -EOPNOTSUPP;
0612 goto flags_out;
0613 }
0614
0615 if (!S_ISDIR(inode->i_mode)) {
0616 err = -ENOTDIR;
0617 goto flags_out;
0618 }
0619
0620 if (!ext4_empty_dir(inode)) {
0621 err = -ENOTEMPTY;
0622 goto flags_out;
0623 }
0624 }
0625
0626
0627
0628
0629
0630
0631
0632 if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) &&
0633 (flags & EXT4_IMMUTABLE_FL)) {
0634 inode_dio_wait(inode);
0635 err = filemap_write_and_wait(inode->i_mapping);
0636 if (err)
0637 goto flags_out;
0638 }
0639
0640 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
0641 if (IS_ERR(handle)) {
0642 err = PTR_ERR(handle);
0643 goto flags_out;
0644 }
0645 if (IS_SYNC(inode))
0646 ext4_handle_sync(handle);
0647 err = ext4_reserve_inode_write(handle, inode, &iloc);
0648 if (err)
0649 goto flags_err;
0650
0651 ext4_dax_dontcache(inode, flags);
0652
0653 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
0654 if (!(mask & EXT4_FL_USER_MODIFIABLE))
0655 continue;
0656
0657 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
0658 continue;
0659 if (mask & flags)
0660 ext4_set_inode_flag(inode, i);
0661 else
0662 ext4_clear_inode_flag(inode, i);
0663 }
0664
0665 ext4_set_inode_flags(inode, false);
0666
0667 inode->i_ctime = current_time(inode);
0668
0669 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
0670 flags_err:
0671 ext4_journal_stop(handle);
0672 if (err)
0673 goto flags_out;
0674
0675 if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
0676
0677
0678
0679
0680 if (IS_DAX(inode)) {
0681 err = -EBUSY;
0682 goto flags_out;
0683 }
0684
0685 err = ext4_change_inode_journal_flag(inode,
0686 flags & EXT4_JOURNAL_DATA_FL);
0687 if (err)
0688 goto flags_out;
0689 }
0690 if (migrate) {
0691 if (flags & EXT4_EXTENTS_FL)
0692 err = ext4_ext_migrate(inode);
0693 else
0694 err = ext4_ind_migrate(inode);
0695 }
0696
0697 flags_out:
0698 return err;
0699 }
0700
0701 #ifdef CONFIG_QUOTA
0702 static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
0703 {
0704 struct super_block *sb = inode->i_sb;
0705 struct ext4_inode_info *ei = EXT4_I(inode);
0706 int err, rc;
0707 handle_t *handle;
0708 kprojid_t kprojid;
0709 struct ext4_iloc iloc;
0710 struct ext4_inode *raw_inode;
0711 struct dquot *transfer_to[MAXQUOTAS] = { };
0712
0713 if (!ext4_has_feature_project(sb)) {
0714 if (projid != EXT4_DEF_PROJID)
0715 return -EOPNOTSUPP;
0716 else
0717 return 0;
0718 }
0719
0720 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
0721 return -EOPNOTSUPP;
0722
0723 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
0724
0725 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
0726 return 0;
0727
0728 err = -EPERM;
0729
0730 if (ext4_is_quota_file(inode))
0731 return err;
0732
0733 err = ext4_get_inode_loc(inode, &iloc);
0734 if (err)
0735 return err;
0736
0737 raw_inode = ext4_raw_inode(&iloc);
0738 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
0739 err = ext4_expand_extra_isize(inode,
0740 EXT4_SB(sb)->s_want_extra_isize,
0741 &iloc);
0742 if (err)
0743 return err;
0744 } else {
0745 brelse(iloc.bh);
0746 }
0747
0748 err = dquot_initialize(inode);
0749 if (err)
0750 return err;
0751
0752 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
0753 EXT4_QUOTA_INIT_BLOCKS(sb) +
0754 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
0755 if (IS_ERR(handle))
0756 return PTR_ERR(handle);
0757
0758 err = ext4_reserve_inode_write(handle, inode, &iloc);
0759 if (err)
0760 goto out_stop;
0761
0762 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
0763 if (!IS_ERR(transfer_to[PRJQUOTA])) {
0764
0765
0766
0767
0768 down_read(&EXT4_I(inode)->xattr_sem);
0769 err = __dquot_transfer(inode, transfer_to);
0770 up_read(&EXT4_I(inode)->xattr_sem);
0771 dqput(transfer_to[PRJQUOTA]);
0772 if (err)
0773 goto out_dirty;
0774 }
0775
0776 EXT4_I(inode)->i_projid = kprojid;
0777 inode->i_ctime = current_time(inode);
0778 out_dirty:
0779 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
0780 if (!err)
0781 err = rc;
0782 out_stop:
0783 ext4_journal_stop(handle);
0784 return err;
0785 }
0786 #else
0787 static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
0788 {
0789 if (projid != EXT4_DEF_PROJID)
0790 return -EOPNOTSUPP;
0791 return 0;
0792 }
0793 #endif
0794
0795 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
0796 {
0797 struct ext4_sb_info *sbi = EXT4_SB(sb);
0798 __u32 flags;
0799
0800 if (!capable(CAP_SYS_ADMIN))
0801 return -EPERM;
0802
0803 if (get_user(flags, (__u32 __user *)arg))
0804 return -EFAULT;
0805
0806 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
0807 return -EINVAL;
0808
0809 if (ext4_forced_shutdown(sbi))
0810 return 0;
0811
0812 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
0813 trace_ext4_shutdown(sb, flags);
0814
0815 switch (flags) {
0816 case EXT4_GOING_FLAGS_DEFAULT:
0817 freeze_bdev(sb->s_bdev);
0818 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
0819 thaw_bdev(sb->s_bdev);
0820 break;
0821 case EXT4_GOING_FLAGS_LOGFLUSH:
0822 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
0823 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
0824 (void) ext4_force_commit(sb);
0825 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
0826 }
0827 break;
0828 case EXT4_GOING_FLAGS_NOLOGFLUSH:
0829 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
0830 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
0831 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
0832 break;
0833 default:
0834 return -EINVAL;
0835 }
0836 clear_opt(sb, DISCARD);
0837 return 0;
0838 }
0839
0840 struct getfsmap_info {
0841 struct super_block *gi_sb;
0842 struct fsmap_head __user *gi_data;
0843 unsigned int gi_idx;
0844 __u32 gi_last_flags;
0845 };
0846
0847 static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
0848 {
0849 struct getfsmap_info *info = priv;
0850 struct fsmap fm;
0851
0852 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
0853
0854 info->gi_last_flags = xfm->fmr_flags;
0855 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
0856 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
0857 sizeof(struct fsmap)))
0858 return -EFAULT;
0859
0860 return 0;
0861 }
0862
0863 static int ext4_ioc_getfsmap(struct super_block *sb,
0864 struct fsmap_head __user *arg)
0865 {
0866 struct getfsmap_info info = { NULL };
0867 struct ext4_fsmap_head xhead = {0};
0868 struct fsmap_head head;
0869 bool aborted = false;
0870 int error;
0871
0872 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
0873 return -EFAULT;
0874 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
0875 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
0876 sizeof(head.fmh_keys[0].fmr_reserved)) ||
0877 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
0878 sizeof(head.fmh_keys[1].fmr_reserved)))
0879 return -EINVAL;
0880
0881
0882
0883
0884 if (head.fmh_keys[0].fmr_offset ||
0885 (head.fmh_keys[1].fmr_offset != 0 &&
0886 head.fmh_keys[1].fmr_offset != -1ULL))
0887 return -EINVAL;
0888
0889 xhead.fmh_iflags = head.fmh_iflags;
0890 xhead.fmh_count = head.fmh_count;
0891 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
0892 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
0893
0894 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
0895 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
0896
0897 info.gi_sb = sb;
0898 info.gi_data = arg;
0899 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
0900 if (error == EXT4_QUERY_RANGE_ABORT)
0901 aborted = true;
0902 else if (error)
0903 return error;
0904
0905
0906 if (!aborted && info.gi_idx) {
0907 info.gi_last_flags |= FMR_OF_LAST;
0908 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
0909 &info.gi_last_flags,
0910 sizeof(info.gi_last_flags)))
0911 return -EFAULT;
0912 }
0913
0914
0915 head.fmh_entries = xhead.fmh_entries;
0916 head.fmh_oflags = xhead.fmh_oflags;
0917 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
0918 return -EFAULT;
0919
0920 return 0;
0921 }
0922
0923 static long ext4_ioctl_group_add(struct file *file,
0924 struct ext4_new_group_data *input)
0925 {
0926 struct super_block *sb = file_inode(file)->i_sb;
0927 int err, err2=0;
0928
0929 err = ext4_resize_begin(sb);
0930 if (err)
0931 return err;
0932
0933 if (ext4_has_feature_bigalloc(sb)) {
0934 ext4_msg(sb, KERN_ERR,
0935 "Online resizing not supported with bigalloc");
0936 err = -EOPNOTSUPP;
0937 goto group_add_out;
0938 }
0939
0940 err = mnt_want_write_file(file);
0941 if (err)
0942 goto group_add_out;
0943
0944 err = ext4_group_add(sb, input);
0945 if (EXT4_SB(sb)->s_journal) {
0946 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
0947 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
0948 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
0949 }
0950 if (err == 0)
0951 err = err2;
0952 mnt_drop_write_file(file);
0953 if (!err && ext4_has_group_desc_csum(sb) &&
0954 test_opt(sb, INIT_INODE_TABLE))
0955 err = ext4_register_li_request(sb, input->group);
0956 group_add_out:
0957 err2 = ext4_resize_end(sb, false);
0958 if (err == 0)
0959 err = err2;
0960 return err;
0961 }
0962
0963 int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa)
0964 {
0965 struct inode *inode = d_inode(dentry);
0966 struct ext4_inode_info *ei = EXT4_I(inode);
0967 u32 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
0968
0969 if (S_ISREG(inode->i_mode))
0970 flags &= ~FS_PROJINHERIT_FL;
0971
0972 fileattr_fill_flags(fa, flags);
0973 if (ext4_has_feature_project(inode->i_sb))
0974 fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
0975
0976 return 0;
0977 }
0978
0979 int ext4_fileattr_set(struct user_namespace *mnt_userns,
0980 struct dentry *dentry, struct fileattr *fa)
0981 {
0982 struct inode *inode = d_inode(dentry);
0983 u32 flags = fa->flags;
0984 int err = -EOPNOTSUPP;
0985
0986 if (flags & ~EXT4_FL_USER_VISIBLE)
0987 goto out;
0988
0989
0990
0991
0992
0993
0994
0995 flags &= EXT4_FL_USER_MODIFIABLE;
0996 if (ext4_mask_flags(inode->i_mode, flags) != flags)
0997 goto out;
0998 err = ext4_ioctl_check_immutable(inode, fa->fsx_projid, flags);
0999 if (err)
1000 goto out;
1001 err = ext4_ioctl_setflags(inode, flags);
1002 if (err)
1003 goto out;
1004 err = ext4_ioctl_setproject(inode, fa->fsx_projid);
1005 out:
1006 return err;
1007 }
1008
1009
1010 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
1011
1012 static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
1013 {
1014 struct fiemap fiemap;
1015 struct fiemap __user *ufiemap = (struct fiemap __user *) arg;
1016 struct fiemap_extent_info fieinfo = { 0, };
1017 struct inode *inode = file_inode(filp);
1018 int error;
1019
1020 if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
1021 return -EFAULT;
1022
1023 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
1024 return -EINVAL;
1025
1026 fieinfo.fi_flags = fiemap.fm_flags;
1027 fieinfo.fi_extents_max = fiemap.fm_extent_count;
1028 fieinfo.fi_extents_start = ufiemap->fm_extents;
1029
1030 error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start,
1031 fiemap.fm_length);
1032 fiemap.fm_flags = fieinfo.fi_flags;
1033 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
1034 if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
1035 error = -EFAULT;
1036
1037 return error;
1038 }
1039
1040 static int ext4_ioctl_checkpoint(struct file *filp, unsigned long arg)
1041 {
1042 int err = 0;
1043 __u32 flags = 0;
1044 unsigned int flush_flags = 0;
1045 struct super_block *sb = file_inode(filp)->i_sb;
1046
1047 if (copy_from_user(&flags, (__u32 __user *)arg,
1048 sizeof(__u32)))
1049 return -EFAULT;
1050
1051 if (!capable(CAP_SYS_ADMIN))
1052 return -EPERM;
1053
1054
1055 if ((flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID) ||
1056 ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
1057 (flags & JBD2_JOURNAL_FLUSH_ZEROOUT)))
1058 return -EINVAL;
1059
1060 if (!EXT4_SB(sb)->s_journal)
1061 return -ENODEV;
1062
1063 if (flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID)
1064 return -EINVAL;
1065
1066 if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
1067 !bdev_max_discard_sectors(EXT4_SB(sb)->s_journal->j_dev))
1068 return -EOPNOTSUPP;
1069
1070 if (flags & EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN)
1071 return 0;
1072
1073 if (flags & EXT4_IOC_CHECKPOINT_FLAG_DISCARD)
1074 flush_flags |= JBD2_JOURNAL_FLUSH_DISCARD;
1075
1076 if (flags & EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT) {
1077 flush_flags |= JBD2_JOURNAL_FLUSH_ZEROOUT;
1078 pr_info_ratelimited("warning: checkpointing journal with EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT can be slow");
1079 }
1080
1081 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1082 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, flush_flags);
1083 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1084
1085 return err;
1086 }
1087
1088 static int ext4_ioctl_setlabel(struct file *filp, const char __user *user_label)
1089 {
1090 size_t len;
1091 int ret = 0;
1092 char new_label[EXT4_LABEL_MAX + 1];
1093 struct super_block *sb = file_inode(filp)->i_sb;
1094
1095 if (!capable(CAP_SYS_ADMIN))
1096 return -EPERM;
1097
1098
1099
1100
1101
1102
1103 if (copy_from_user(new_label, user_label, EXT4_LABEL_MAX + 1))
1104 return -EFAULT;
1105
1106 len = strnlen(new_label, EXT4_LABEL_MAX + 1);
1107 if (len > EXT4_LABEL_MAX)
1108 return -EINVAL;
1109
1110
1111
1112
1113 memset(new_label + len, 0, EXT4_LABEL_MAX - len);
1114
1115 ret = mnt_want_write_file(filp);
1116 if (ret)
1117 return ret;
1118
1119 ret = ext4_update_superblocks_fn(sb, ext4_sb_setlabel, new_label);
1120
1121 mnt_drop_write_file(filp);
1122 return ret;
1123 }
1124
1125 static int ext4_ioctl_getlabel(struct ext4_sb_info *sbi, char __user *user_label)
1126 {
1127 char label[EXT4_LABEL_MAX + 1];
1128
1129
1130
1131
1132
1133
1134 BUILD_BUG_ON(EXT4_LABEL_MAX >= FSLABEL_MAX);
1135
1136 memset(label, 0, sizeof(label));
1137 lock_buffer(sbi->s_sbh);
1138 strncpy(label, sbi->s_es->s_volume_name, EXT4_LABEL_MAX);
1139 unlock_buffer(sbi->s_sbh);
1140
1141 if (copy_to_user(user_label, label, sizeof(label)))
1142 return -EFAULT;
1143 return 0;
1144 }
1145
1146 static int ext4_ioctl_getuuid(struct ext4_sb_info *sbi,
1147 struct fsuuid __user *ufsuuid)
1148 {
1149 struct fsuuid fsuuid;
1150 __u8 uuid[UUID_SIZE];
1151
1152 if (copy_from_user(&fsuuid, ufsuuid, sizeof(fsuuid)))
1153 return -EFAULT;
1154
1155 if (fsuuid.fsu_len == 0) {
1156 fsuuid.fsu_len = UUID_SIZE;
1157 if (copy_to_user(ufsuuid, &fsuuid, sizeof(fsuuid.fsu_len)))
1158 return -EFAULT;
1159 return -EINVAL;
1160 }
1161
1162 if (fsuuid.fsu_len != UUID_SIZE || fsuuid.fsu_flags != 0)
1163 return -EINVAL;
1164
1165 lock_buffer(sbi->s_sbh);
1166 memcpy(uuid, sbi->s_es->s_uuid, UUID_SIZE);
1167 unlock_buffer(sbi->s_sbh);
1168
1169 if (copy_to_user(&ufsuuid->fsu_uuid[0], uuid, UUID_SIZE))
1170 return -EFAULT;
1171 return 0;
1172 }
1173
1174 static int ext4_ioctl_setuuid(struct file *filp,
1175 const struct fsuuid __user *ufsuuid)
1176 {
1177 int ret = 0;
1178 struct super_block *sb = file_inode(filp)->i_sb;
1179 struct fsuuid fsuuid;
1180 __u8 uuid[UUID_SIZE];
1181
1182 if (!capable(CAP_SYS_ADMIN))
1183 return -EPERM;
1184
1185
1186
1187
1188
1189 if (((ext4_has_feature_gdt_csum(sb) || ext4_has_metadata_csum(sb))
1190 && !ext4_has_feature_csum_seed(sb))
1191 || ext4_has_feature_stable_inodes(sb))
1192 return -EOPNOTSUPP;
1193
1194 if (copy_from_user(&fsuuid, ufsuuid, sizeof(fsuuid)))
1195 return -EFAULT;
1196
1197 if (fsuuid.fsu_len != UUID_SIZE || fsuuid.fsu_flags != 0)
1198 return -EINVAL;
1199
1200 if (copy_from_user(uuid, &ufsuuid->fsu_uuid[0], UUID_SIZE))
1201 return -EFAULT;
1202
1203 ret = mnt_want_write_file(filp);
1204 if (ret)
1205 return ret;
1206
1207 ret = ext4_update_superblocks_fn(sb, ext4_sb_setuuid, &uuid);
1208 mnt_drop_write_file(filp);
1209
1210 return ret;
1211 }
1212
1213 static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1214 {
1215 struct inode *inode = file_inode(filp);
1216 struct super_block *sb = inode->i_sb;
1217 struct user_namespace *mnt_userns = file_mnt_user_ns(filp);
1218
1219 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
1220
1221 switch (cmd) {
1222 case FS_IOC_GETFSMAP:
1223 return ext4_ioc_getfsmap(sb, (void __user *)arg);
1224 case EXT4_IOC_GETVERSION:
1225 case EXT4_IOC_GETVERSION_OLD:
1226 return put_user(inode->i_generation, (int __user *) arg);
1227 case EXT4_IOC_SETVERSION:
1228 case EXT4_IOC_SETVERSION_OLD: {
1229 handle_t *handle;
1230 struct ext4_iloc iloc;
1231 __u32 generation;
1232 int err;
1233
1234 if (!inode_owner_or_capable(mnt_userns, inode))
1235 return -EPERM;
1236
1237 if (ext4_has_metadata_csum(inode->i_sb)) {
1238 ext4_warning(sb, "Setting inode version is not "
1239 "supported with metadata_csum enabled.");
1240 return -ENOTTY;
1241 }
1242
1243 err = mnt_want_write_file(filp);
1244 if (err)
1245 return err;
1246 if (get_user(generation, (int __user *) arg)) {
1247 err = -EFAULT;
1248 goto setversion_out;
1249 }
1250
1251 inode_lock(inode);
1252 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
1253 if (IS_ERR(handle)) {
1254 err = PTR_ERR(handle);
1255 goto unlock_out;
1256 }
1257 err = ext4_reserve_inode_write(handle, inode, &iloc);
1258 if (err == 0) {
1259 inode->i_ctime = current_time(inode);
1260 inode->i_generation = generation;
1261 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
1262 }
1263 ext4_journal_stop(handle);
1264
1265 unlock_out:
1266 inode_unlock(inode);
1267 setversion_out:
1268 mnt_drop_write_file(filp);
1269 return err;
1270 }
1271 case EXT4_IOC_GROUP_EXTEND: {
1272 ext4_fsblk_t n_blocks_count;
1273 int err, err2=0;
1274
1275 err = ext4_resize_begin(sb);
1276 if (err)
1277 return err;
1278
1279 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
1280 err = -EFAULT;
1281 goto group_extend_out;
1282 }
1283
1284 if (ext4_has_feature_bigalloc(sb)) {
1285 ext4_msg(sb, KERN_ERR,
1286 "Online resizing not supported with bigalloc");
1287 err = -EOPNOTSUPP;
1288 goto group_extend_out;
1289 }
1290
1291 err = mnt_want_write_file(filp);
1292 if (err)
1293 goto group_extend_out;
1294
1295 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
1296 if (EXT4_SB(sb)->s_journal) {
1297 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1298 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
1299 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1300 }
1301 if (err == 0)
1302 err = err2;
1303 mnt_drop_write_file(filp);
1304 group_extend_out:
1305 err2 = ext4_resize_end(sb, false);
1306 if (err == 0)
1307 err = err2;
1308 return err;
1309 }
1310
1311 case EXT4_IOC_MOVE_EXT: {
1312 struct move_extent me;
1313 struct fd donor;
1314 int err;
1315
1316 if (!(filp->f_mode & FMODE_READ) ||
1317 !(filp->f_mode & FMODE_WRITE))
1318 return -EBADF;
1319
1320 if (copy_from_user(&me,
1321 (struct move_extent __user *)arg, sizeof(me)))
1322 return -EFAULT;
1323 me.moved_len = 0;
1324
1325 donor = fdget(me.donor_fd);
1326 if (!donor.file)
1327 return -EBADF;
1328
1329 if (!(donor.file->f_mode & FMODE_WRITE)) {
1330 err = -EBADF;
1331 goto mext_out;
1332 }
1333
1334 if (ext4_has_feature_bigalloc(sb)) {
1335 ext4_msg(sb, KERN_ERR,
1336 "Online defrag not supported with bigalloc");
1337 err = -EOPNOTSUPP;
1338 goto mext_out;
1339 } else if (IS_DAX(inode)) {
1340 ext4_msg(sb, KERN_ERR,
1341 "Online defrag not supported with DAX");
1342 err = -EOPNOTSUPP;
1343 goto mext_out;
1344 }
1345
1346 err = mnt_want_write_file(filp);
1347 if (err)
1348 goto mext_out;
1349
1350 err = ext4_move_extents(filp, donor.file, me.orig_start,
1351 me.donor_start, me.len, &me.moved_len);
1352 mnt_drop_write_file(filp);
1353
1354 if (copy_to_user((struct move_extent __user *)arg,
1355 &me, sizeof(me)))
1356 err = -EFAULT;
1357 mext_out:
1358 fdput(donor);
1359 return err;
1360 }
1361
1362 case EXT4_IOC_GROUP_ADD: {
1363 struct ext4_new_group_data input;
1364
1365 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
1366 sizeof(input)))
1367 return -EFAULT;
1368
1369 return ext4_ioctl_group_add(filp, &input);
1370 }
1371
1372 case EXT4_IOC_MIGRATE:
1373 {
1374 int err;
1375 if (!inode_owner_or_capable(mnt_userns, inode))
1376 return -EACCES;
1377
1378 err = mnt_want_write_file(filp);
1379 if (err)
1380 return err;
1381
1382
1383
1384
1385
1386
1387 inode_lock((inode));
1388 err = ext4_ext_migrate(inode);
1389 inode_unlock((inode));
1390 mnt_drop_write_file(filp);
1391 return err;
1392 }
1393
1394 case EXT4_IOC_ALLOC_DA_BLKS:
1395 {
1396 int err;
1397 if (!inode_owner_or_capable(mnt_userns, inode))
1398 return -EACCES;
1399
1400 err = mnt_want_write_file(filp);
1401 if (err)
1402 return err;
1403 err = ext4_alloc_da_blocks(inode);
1404 mnt_drop_write_file(filp);
1405 return err;
1406 }
1407
1408 case EXT4_IOC_SWAP_BOOT:
1409 {
1410 int err;
1411 if (!(filp->f_mode & FMODE_WRITE))
1412 return -EBADF;
1413 err = mnt_want_write_file(filp);
1414 if (err)
1415 return err;
1416 err = swap_inode_boot_loader(sb, mnt_userns, inode);
1417 mnt_drop_write_file(filp);
1418 return err;
1419 }
1420
1421 case EXT4_IOC_RESIZE_FS: {
1422 ext4_fsblk_t n_blocks_count;
1423 int err = 0, err2 = 0;
1424 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
1425
1426 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
1427 sizeof(__u64))) {
1428 return -EFAULT;
1429 }
1430
1431 err = ext4_resize_begin(sb);
1432 if (err)
1433 return err;
1434
1435 err = mnt_want_write_file(filp);
1436 if (err)
1437 goto resizefs_out;
1438
1439 err = ext4_resize_fs(sb, n_blocks_count);
1440 if (EXT4_SB(sb)->s_journal) {
1441 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_RESIZE, NULL);
1442 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
1443 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
1444 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
1445 }
1446 if (err == 0)
1447 err = err2;
1448 mnt_drop_write_file(filp);
1449 if (!err && (o_group < EXT4_SB(sb)->s_groups_count) &&
1450 ext4_has_group_desc_csum(sb) &&
1451 test_opt(sb, INIT_INODE_TABLE))
1452 err = ext4_register_li_request(sb, o_group);
1453
1454 resizefs_out:
1455 err2 = ext4_resize_end(sb, true);
1456 if (err == 0)
1457 err = err2;
1458 return err;
1459 }
1460
1461 case FITRIM:
1462 {
1463 struct fstrim_range range;
1464 int ret = 0;
1465
1466 if (!capable(CAP_SYS_ADMIN))
1467 return -EPERM;
1468
1469 if (!bdev_max_discard_sectors(sb->s_bdev))
1470 return -EOPNOTSUPP;
1471
1472
1473
1474
1475
1476 if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
1477 return -EROFS;
1478
1479 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1480 sizeof(range)))
1481 return -EFAULT;
1482
1483 ret = ext4_trim_fs(sb, &range);
1484 if (ret < 0)
1485 return ret;
1486
1487 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1488 sizeof(range)))
1489 return -EFAULT;
1490
1491 return 0;
1492 }
1493 case EXT4_IOC_PRECACHE_EXTENTS:
1494 return ext4_ext_precache(inode);
1495
1496 case FS_IOC_SET_ENCRYPTION_POLICY:
1497 if (!ext4_has_feature_encrypt(sb))
1498 return -EOPNOTSUPP;
1499 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1500
1501 case FS_IOC_GET_ENCRYPTION_PWSALT:
1502 return ext4_ioctl_get_encryption_pwsalt(filp, (void __user *)arg);
1503
1504 case FS_IOC_GET_ENCRYPTION_POLICY:
1505 if (!ext4_has_feature_encrypt(sb))
1506 return -EOPNOTSUPP;
1507 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1508
1509 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1510 if (!ext4_has_feature_encrypt(sb))
1511 return -EOPNOTSUPP;
1512 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
1513
1514 case FS_IOC_ADD_ENCRYPTION_KEY:
1515 if (!ext4_has_feature_encrypt(sb))
1516 return -EOPNOTSUPP;
1517 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
1518
1519 case FS_IOC_REMOVE_ENCRYPTION_KEY:
1520 if (!ext4_has_feature_encrypt(sb))
1521 return -EOPNOTSUPP;
1522 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
1523
1524 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1525 if (!ext4_has_feature_encrypt(sb))
1526 return -EOPNOTSUPP;
1527 return fscrypt_ioctl_remove_key_all_users(filp,
1528 (void __user *)arg);
1529 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1530 if (!ext4_has_feature_encrypt(sb))
1531 return -EOPNOTSUPP;
1532 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
1533
1534 case FS_IOC_GET_ENCRYPTION_NONCE:
1535 if (!ext4_has_feature_encrypt(sb))
1536 return -EOPNOTSUPP;
1537 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
1538
1539 case EXT4_IOC_CLEAR_ES_CACHE:
1540 {
1541 if (!inode_owner_or_capable(mnt_userns, inode))
1542 return -EACCES;
1543 ext4_clear_inode_es(inode);
1544 return 0;
1545 }
1546
1547 case EXT4_IOC_GETSTATE:
1548 {
1549 __u32 state = 0;
1550
1551 if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED))
1552 state |= EXT4_STATE_FLAG_EXT_PRECACHED;
1553 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
1554 state |= EXT4_STATE_FLAG_NEW;
1555 if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
1556 state |= EXT4_STATE_FLAG_NEWENTRY;
1557 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE))
1558 state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE;
1559
1560 return put_user(state, (__u32 __user *) arg);
1561 }
1562
1563 case EXT4_IOC_GET_ES_CACHE:
1564 return ext4_ioctl_get_es_cache(filp, arg);
1565
1566 case EXT4_IOC_SHUTDOWN:
1567 return ext4_shutdown(sb, arg);
1568
1569 case FS_IOC_ENABLE_VERITY:
1570 if (!ext4_has_feature_verity(sb))
1571 return -EOPNOTSUPP;
1572 return fsverity_ioctl_enable(filp, (const void __user *)arg);
1573
1574 case FS_IOC_MEASURE_VERITY:
1575 if (!ext4_has_feature_verity(sb))
1576 return -EOPNOTSUPP;
1577 return fsverity_ioctl_measure(filp, (void __user *)arg);
1578
1579 case FS_IOC_READ_VERITY_METADATA:
1580 if (!ext4_has_feature_verity(sb))
1581 return -EOPNOTSUPP;
1582 return fsverity_ioctl_read_metadata(filp,
1583 (const void __user *)arg);
1584
1585 case EXT4_IOC_CHECKPOINT:
1586 return ext4_ioctl_checkpoint(filp, arg);
1587
1588 case FS_IOC_GETFSLABEL:
1589 return ext4_ioctl_getlabel(EXT4_SB(sb), (void __user *)arg);
1590
1591 case FS_IOC_SETFSLABEL:
1592 return ext4_ioctl_setlabel(filp,
1593 (const void __user *)arg);
1594
1595 case EXT4_IOC_GETFSUUID:
1596 return ext4_ioctl_getuuid(EXT4_SB(sb), (void __user *)arg);
1597 case EXT4_IOC_SETFSUUID:
1598 return ext4_ioctl_setuuid(filp, (const void __user *)arg);
1599 default:
1600 return -ENOTTY;
1601 }
1602 }
1603
1604 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1605 {
1606 return __ext4_ioctl(filp, cmd, arg);
1607 }
1608
1609 #ifdef CONFIG_COMPAT
1610 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1611 {
1612
1613 switch (cmd) {
1614 case EXT4_IOC32_GETVERSION:
1615 cmd = EXT4_IOC_GETVERSION;
1616 break;
1617 case EXT4_IOC32_SETVERSION:
1618 cmd = EXT4_IOC_SETVERSION;
1619 break;
1620 case EXT4_IOC32_GROUP_EXTEND:
1621 cmd = EXT4_IOC_GROUP_EXTEND;
1622 break;
1623 case EXT4_IOC32_GETVERSION_OLD:
1624 cmd = EXT4_IOC_GETVERSION_OLD;
1625 break;
1626 case EXT4_IOC32_SETVERSION_OLD:
1627 cmd = EXT4_IOC_SETVERSION_OLD;
1628 break;
1629 case EXT4_IOC32_GETRSVSZ:
1630 cmd = EXT4_IOC_GETRSVSZ;
1631 break;
1632 case EXT4_IOC32_SETRSVSZ:
1633 cmd = EXT4_IOC_SETRSVSZ;
1634 break;
1635 case EXT4_IOC32_GROUP_ADD: {
1636 struct compat_ext4_new_group_input __user *uinput;
1637 struct ext4_new_group_data input;
1638 int err;
1639
1640 uinput = compat_ptr(arg);
1641 err = get_user(input.group, &uinput->group);
1642 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1643 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1644 err |= get_user(input.inode_table, &uinput->inode_table);
1645 err |= get_user(input.blocks_count, &uinput->blocks_count);
1646 err |= get_user(input.reserved_blocks,
1647 &uinput->reserved_blocks);
1648 if (err)
1649 return -EFAULT;
1650 return ext4_ioctl_group_add(file, &input);
1651 }
1652 case EXT4_IOC_MOVE_EXT:
1653 case EXT4_IOC_RESIZE_FS:
1654 case FITRIM:
1655 case EXT4_IOC_PRECACHE_EXTENTS:
1656 case FS_IOC_SET_ENCRYPTION_POLICY:
1657 case FS_IOC_GET_ENCRYPTION_PWSALT:
1658 case FS_IOC_GET_ENCRYPTION_POLICY:
1659 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
1660 case FS_IOC_ADD_ENCRYPTION_KEY:
1661 case FS_IOC_REMOVE_ENCRYPTION_KEY:
1662 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
1663 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
1664 case FS_IOC_GET_ENCRYPTION_NONCE:
1665 case EXT4_IOC_SHUTDOWN:
1666 case FS_IOC_GETFSMAP:
1667 case FS_IOC_ENABLE_VERITY:
1668 case FS_IOC_MEASURE_VERITY:
1669 case FS_IOC_READ_VERITY_METADATA:
1670 case EXT4_IOC_CLEAR_ES_CACHE:
1671 case EXT4_IOC_GETSTATE:
1672 case EXT4_IOC_GET_ES_CACHE:
1673 case EXT4_IOC_CHECKPOINT:
1674 case FS_IOC_GETFSLABEL:
1675 case FS_IOC_SETFSLABEL:
1676 case EXT4_IOC_GETFSUUID:
1677 case EXT4_IOC_SETFSUUID:
1678 break;
1679 default:
1680 return -ENOIOCTLCMD;
1681 }
1682 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1683 }
1684 #endif
1685
1686 static void set_overhead(struct ext4_super_block *es, const void *arg)
1687 {
1688 es->s_overhead_clusters = cpu_to_le32(*((unsigned long *) arg));
1689 }
1690
1691 int ext4_update_overhead(struct super_block *sb, bool force)
1692 {
1693 struct ext4_sb_info *sbi = EXT4_SB(sb);
1694
1695 if (sb_rdonly(sb))
1696 return 0;
1697 if (!force &&
1698 (sbi->s_overhead == 0 ||
1699 sbi->s_overhead == le32_to_cpu(sbi->s_es->s_overhead_clusters)))
1700 return 0;
1701 return ext4_update_superblocks_fn(sb, set_overhead, &sbi->s_overhead);
1702 }