| .. | .. |
|---|
| 82 | 82 | struct buffer_head *bh) |
|---|
| 83 | 83 | { |
|---|
| 84 | 84 | ext4_fsblk_t blk; |
|---|
| 85 | | - struct ext4_group_info *grp = ext4_get_group_info(sb, block_group); |
|---|
| 85 | + struct ext4_group_info *grp; |
|---|
| 86 | + |
|---|
| 87 | + if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) |
|---|
| 88 | + return 0; |
|---|
| 89 | + |
|---|
| 90 | + grp = ext4_get_group_info(sb, block_group); |
|---|
| 86 | 91 | |
|---|
| 87 | 92 | if (buffer_verified(bh)) |
|---|
| 88 | 93 | return 0; |
|---|
| 89 | | - if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) |
|---|
| 94 | + if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) |
|---|
| 90 | 95 | return -EFSCORRUPTED; |
|---|
| 91 | 96 | |
|---|
| 92 | 97 | ext4_lock_group(sb, block_group); |
|---|
| .. | .. |
|---|
| 94 | 99 | goto verified; |
|---|
| 95 | 100 | blk = ext4_inode_bitmap(sb, desc); |
|---|
| 96 | 101 | if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, |
|---|
| 97 | | - EXT4_INODES_PER_GROUP(sb) / 8)) { |
|---|
| 102 | + EXT4_INODES_PER_GROUP(sb) / 8) || |
|---|
| 103 | + ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) { |
|---|
| 98 | 104 | ext4_unlock_group(sb, block_group); |
|---|
| 99 | 105 | ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " |
|---|
| 100 | 106 | "inode_bitmap = %llu", block_group, blk); |
|---|
| .. | .. |
|---|
| 112 | 118 | * Read the inode allocation bitmap for a given block_group, reading |
|---|
| 113 | 119 | * into the specified slot in the superblock's bitmap cache. |
|---|
| 114 | 120 | * |
|---|
| 115 | | - * Return buffer_head of bitmap on success or NULL. |
|---|
| 121 | + * Return buffer_head of bitmap on success, or an ERR_PTR on error. |
|---|
| 116 | 122 | */ |
|---|
| 117 | 123 | static struct buffer_head * |
|---|
| 118 | 124 | ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
|---|
| .. | .. |
|---|
| 188 | 194 | * submit the buffer_head for reading |
|---|
| 189 | 195 | */ |
|---|
| 190 | 196 | trace_ext4_load_inode_bitmap(sb, block_group); |
|---|
| 191 | | - bh->b_end_io = ext4_end_bitmap_read; |
|---|
| 192 | | - get_bh(bh); |
|---|
| 193 | | - submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); |
|---|
| 194 | | - wait_on_buffer(bh); |
|---|
| 197 | + ext4_read_bh(bh, REQ_META | REQ_PRIO, ext4_end_bitmap_read); |
|---|
| 198 | + ext4_simulate_fail_bh(sb, bh, EXT4_SIM_IBITMAP_EIO); |
|---|
| 195 | 199 | if (!buffer_uptodate(bh)) { |
|---|
| 196 | 200 | put_bh(bh); |
|---|
| 197 | | - ext4_error(sb, "Cannot read inode bitmap - " |
|---|
| 198 | | - "block_group = %u, inode_bitmap = %llu", |
|---|
| 199 | | - block_group, bitmap_blk); |
|---|
| 201 | + ext4_error_err(sb, EIO, "Cannot read inode bitmap - " |
|---|
| 202 | + "block_group = %u, inode_bitmap = %llu", |
|---|
| 203 | + block_group, bitmap_blk); |
|---|
| 200 | 204 | ext4_mark_group_bitmap_corrupted(sb, block_group, |
|---|
| 201 | 205 | EXT4_GROUP_INFO_IBITMAP_CORRUPT); |
|---|
| 202 | 206 | return ERR_PTR(-EIO); |
|---|
| .. | .. |
|---|
| 265 | 269 | ext4_debug("freeing inode %lu\n", ino); |
|---|
| 266 | 270 | trace_ext4_free_inode(inode); |
|---|
| 267 | 271 | |
|---|
| 268 | | - /* |
|---|
| 269 | | - * Note: we must free any quota before locking the superblock, |
|---|
| 270 | | - * as writing the quota to disk may need the lock as well. |
|---|
| 271 | | - */ |
|---|
| 272 | 272 | dquot_initialize(inode); |
|---|
| 273 | 273 | dquot_free_inode(inode); |
|---|
| 274 | | - dquot_drop(inode); |
|---|
| 275 | 274 | |
|---|
| 276 | 275 | is_directory = S_ISDIR(inode->i_mode); |
|---|
| 277 | 276 | |
|---|
| .. | .. |
|---|
| 287 | 286 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
|---|
| 288 | 287 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
|---|
| 289 | 288 | /* Don't bother if the inode bitmap is corrupt. */ |
|---|
| 290 | | - grp = ext4_get_group_info(sb, block_group); |
|---|
| 291 | 289 | if (IS_ERR(bitmap_bh)) { |
|---|
| 292 | 290 | fatal = PTR_ERR(bitmap_bh); |
|---|
| 293 | 291 | bitmap_bh = NULL; |
|---|
| 294 | 292 | goto error_return; |
|---|
| 295 | 293 | } |
|---|
| 296 | | - if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { |
|---|
| 297 | | - fatal = -EFSCORRUPTED; |
|---|
| 298 | | - goto error_return; |
|---|
| 294 | + if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { |
|---|
| 295 | + grp = ext4_get_group_info(sb, block_group); |
|---|
| 296 | + if (!grp || unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { |
|---|
| 297 | + fatal = -EFSCORRUPTED; |
|---|
| 298 | + goto error_return; |
|---|
| 299 | + } |
|---|
| 299 | 300 | } |
|---|
| 300 | 301 | |
|---|
| 301 | 302 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
|---|
| .. | .. |
|---|
| 321 | 322 | if (is_directory) { |
|---|
| 322 | 323 | count = ext4_used_dirs_count(sb, gdp) - 1; |
|---|
| 323 | 324 | ext4_used_dirs_set(sb, gdp, count); |
|---|
| 324 | | - percpu_counter_dec(&sbi->s_dirs_counter); |
|---|
| 325 | + if (percpu_counter_initialized(&sbi->s_dirs_counter)) |
|---|
| 326 | + percpu_counter_dec(&sbi->s_dirs_counter); |
|---|
| 325 | 327 | } |
|---|
| 326 | 328 | ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, |
|---|
| 327 | 329 | EXT4_INODES_PER_GROUP(sb) / 8); |
|---|
| 328 | 330 | ext4_group_desc_csum_set(sb, block_group, gdp); |
|---|
| 329 | 331 | ext4_unlock_group(sb, block_group); |
|---|
| 330 | 332 | |
|---|
| 331 | | - percpu_counter_inc(&sbi->s_freeinodes_counter); |
|---|
| 333 | + if (percpu_counter_initialized(&sbi->s_freeinodes_counter)) |
|---|
| 334 | + percpu_counter_inc(&sbi->s_freeinodes_counter); |
|---|
| 332 | 335 | if (sbi->s_log_groups_per_flex) { |
|---|
| 333 | 336 | struct flex_groups *fg; |
|---|
| 334 | 337 | |
|---|
| .. | .. |
|---|
| 508 | 511 | goto fallback; |
|---|
| 509 | 512 | } |
|---|
| 510 | 513 | |
|---|
| 511 | | - max_dirs = ndirs / ngroups + inodes_per_group / 16; |
|---|
| 514 | + max_dirs = ndirs / ngroups + inodes_per_group*flex_size / 16; |
|---|
| 512 | 515 | min_inodes = avefreei - inodes_per_group*flex_size / 4; |
|---|
| 513 | 516 | if (min_inodes < 1) |
|---|
| 514 | 517 | min_inodes = 1; |
|---|
| .. | .. |
|---|
| 716 | 719 | static int find_inode_bit(struct super_block *sb, ext4_group_t group, |
|---|
| 717 | 720 | struct buffer_head *bitmap, unsigned long *ino) |
|---|
| 718 | 721 | { |
|---|
| 722 | + bool check_recently_deleted = EXT4_SB(sb)->s_journal == NULL; |
|---|
| 723 | + unsigned long recently_deleted_ino = EXT4_INODES_PER_GROUP(sb); |
|---|
| 724 | + |
|---|
| 719 | 725 | next: |
|---|
| 720 | 726 | *ino = ext4_find_next_zero_bit((unsigned long *) |
|---|
| 721 | 727 | bitmap->b_data, |
|---|
| 722 | 728 | EXT4_INODES_PER_GROUP(sb), *ino); |
|---|
| 723 | 729 | if (*ino >= EXT4_INODES_PER_GROUP(sb)) |
|---|
| 724 | | - return 0; |
|---|
| 730 | + goto not_found; |
|---|
| 725 | 731 | |
|---|
| 726 | | - if ((EXT4_SB(sb)->s_journal == NULL) && |
|---|
| 727 | | - recently_deleted(sb, group, *ino)) { |
|---|
| 732 | + if (check_recently_deleted && recently_deleted(sb, group, *ino)) { |
|---|
| 733 | + recently_deleted_ino = *ino; |
|---|
| 728 | 734 | *ino = *ino + 1; |
|---|
| 729 | 735 | if (*ino < EXT4_INODES_PER_GROUP(sb)) |
|---|
| 730 | 736 | goto next; |
|---|
| 737 | + goto not_found; |
|---|
| 738 | + } |
|---|
| 739 | + return 1; |
|---|
| 740 | +not_found: |
|---|
| 741 | + if (recently_deleted_ino >= EXT4_INODES_PER_GROUP(sb)) |
|---|
| 731 | 742 | return 0; |
|---|
| 743 | + /* |
|---|
| 744 | + * Not reusing recently deleted inodes is mostly a preference. We don't |
|---|
| 745 | + * want to report ENOSPC or skew allocation patterns because of that. |
|---|
| 746 | + * So return even recently deleted inode if we could find better in the |
|---|
| 747 | + * given range. |
|---|
| 748 | + */ |
|---|
| 749 | + *ino = recently_deleted_ino; |
|---|
| 750 | + return 1; |
|---|
| 751 | +} |
|---|
| 752 | + |
|---|
| 753 | +int ext4_mark_inode_used(struct super_block *sb, int ino) |
|---|
| 754 | +{ |
|---|
| 755 | + unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
|---|
| 756 | + struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL; |
|---|
| 757 | + struct ext4_group_desc *gdp; |
|---|
| 758 | + ext4_group_t group; |
|---|
| 759 | + int bit; |
|---|
| 760 | + int err = -EFSCORRUPTED; |
|---|
| 761 | + |
|---|
| 762 | + if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) |
|---|
| 763 | + goto out; |
|---|
| 764 | + |
|---|
| 765 | + group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
|---|
| 766 | + bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
|---|
| 767 | + inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); |
|---|
| 768 | + if (IS_ERR(inode_bitmap_bh)) |
|---|
| 769 | + return PTR_ERR(inode_bitmap_bh); |
|---|
| 770 | + |
|---|
| 771 | + if (ext4_test_bit(bit, inode_bitmap_bh->b_data)) { |
|---|
| 772 | + err = 0; |
|---|
| 773 | + goto out; |
|---|
| 732 | 774 | } |
|---|
| 733 | 775 | |
|---|
| 734 | | - return 1; |
|---|
| 776 | + gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
|---|
| 777 | + if (!gdp || !group_desc_bh) { |
|---|
| 778 | + err = -EINVAL; |
|---|
| 779 | + goto out; |
|---|
| 780 | + } |
|---|
| 781 | + |
|---|
| 782 | + ext4_set_bit(bit, inode_bitmap_bh->b_data); |
|---|
| 783 | + |
|---|
| 784 | + BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); |
|---|
| 785 | + err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh); |
|---|
| 786 | + if (err) { |
|---|
| 787 | + ext4_std_error(sb, err); |
|---|
| 788 | + goto out; |
|---|
| 789 | + } |
|---|
| 790 | + err = sync_dirty_buffer(inode_bitmap_bh); |
|---|
| 791 | + if (err) { |
|---|
| 792 | + ext4_std_error(sb, err); |
|---|
| 793 | + goto out; |
|---|
| 794 | + } |
|---|
| 795 | + |
|---|
| 796 | + /* We may have to initialize the block bitmap if it isn't already */ |
|---|
| 797 | + if (ext4_has_group_desc_csum(sb) && |
|---|
| 798 | + gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
|---|
| 799 | + struct buffer_head *block_bitmap_bh; |
|---|
| 800 | + |
|---|
| 801 | + block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
|---|
| 802 | + if (IS_ERR(block_bitmap_bh)) { |
|---|
| 803 | + err = PTR_ERR(block_bitmap_bh); |
|---|
| 804 | + goto out; |
|---|
| 805 | + } |
|---|
| 806 | + |
|---|
| 807 | + BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
|---|
| 808 | + err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh); |
|---|
| 809 | + sync_dirty_buffer(block_bitmap_bh); |
|---|
| 810 | + |
|---|
| 811 | + /* recheck and clear flag under lock if we still need to */ |
|---|
| 812 | + ext4_lock_group(sb, group); |
|---|
| 813 | + if (ext4_has_group_desc_csum(sb) && |
|---|
| 814 | + (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { |
|---|
| 815 | + gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
|---|
| 816 | + ext4_free_group_clusters_set(sb, gdp, |
|---|
| 817 | + ext4_free_clusters_after_init(sb, group, gdp)); |
|---|
| 818 | + ext4_block_bitmap_csum_set(sb, group, gdp, |
|---|
| 819 | + block_bitmap_bh); |
|---|
| 820 | + ext4_group_desc_csum_set(sb, group, gdp); |
|---|
| 821 | + } |
|---|
| 822 | + ext4_unlock_group(sb, group); |
|---|
| 823 | + brelse(block_bitmap_bh); |
|---|
| 824 | + |
|---|
| 825 | + if (err) { |
|---|
| 826 | + ext4_std_error(sb, err); |
|---|
| 827 | + goto out; |
|---|
| 828 | + } |
|---|
| 829 | + } |
|---|
| 830 | + |
|---|
| 831 | + /* Update the relevant bg descriptor fields */ |
|---|
| 832 | + if (ext4_has_group_desc_csum(sb)) { |
|---|
| 833 | + int free; |
|---|
| 834 | + |
|---|
| 835 | + ext4_lock_group(sb, group); /* while we modify the bg desc */ |
|---|
| 836 | + free = EXT4_INODES_PER_GROUP(sb) - |
|---|
| 837 | + ext4_itable_unused_count(sb, gdp); |
|---|
| 838 | + if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
|---|
| 839 | + gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); |
|---|
| 840 | + free = 0; |
|---|
| 841 | + } |
|---|
| 842 | + |
|---|
| 843 | + /* |
|---|
| 844 | + * Check the relative inode number against the last used |
|---|
| 845 | + * relative inode number in this group. if it is greater |
|---|
| 846 | + * we need to update the bg_itable_unused count |
|---|
| 847 | + */ |
|---|
| 848 | + if (bit >= free) |
|---|
| 849 | + ext4_itable_unused_set(sb, gdp, |
|---|
| 850 | + (EXT4_INODES_PER_GROUP(sb) - bit - 1)); |
|---|
| 851 | + } else { |
|---|
| 852 | + ext4_lock_group(sb, group); |
|---|
| 853 | + } |
|---|
| 854 | + |
|---|
| 855 | + ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); |
|---|
| 856 | + if (ext4_has_group_desc_csum(sb)) { |
|---|
| 857 | + ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, |
|---|
| 858 | + EXT4_INODES_PER_GROUP(sb) / 8); |
|---|
| 859 | + ext4_group_desc_csum_set(sb, group, gdp); |
|---|
| 860 | + } |
|---|
| 861 | + |
|---|
| 862 | + ext4_unlock_group(sb, group); |
|---|
| 863 | + err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh); |
|---|
| 864 | + sync_dirty_buffer(group_desc_bh); |
|---|
| 865 | +out: |
|---|
| 866 | + return err; |
|---|
| 867 | +} |
|---|
| 868 | + |
|---|
| 869 | +static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode, |
|---|
| 870 | + bool encrypt) |
|---|
| 871 | +{ |
|---|
| 872 | + struct super_block *sb = dir->i_sb; |
|---|
| 873 | + int nblocks = 0; |
|---|
| 874 | +#ifdef CONFIG_EXT4_FS_POSIX_ACL |
|---|
| 875 | + struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT); |
|---|
| 876 | + |
|---|
| 877 | + if (IS_ERR(p)) |
|---|
| 878 | + return PTR_ERR(p); |
|---|
| 879 | + if (p) { |
|---|
| 880 | + int acl_size = p->a_count * sizeof(ext4_acl_entry); |
|---|
| 881 | + |
|---|
| 882 | + nblocks += (S_ISDIR(mode) ? 2 : 1) * |
|---|
| 883 | + __ext4_xattr_set_credits(sb, NULL /* inode */, |
|---|
| 884 | + NULL /* block_bh */, acl_size, |
|---|
| 885 | + true /* is_create */); |
|---|
| 886 | + posix_acl_release(p); |
|---|
| 887 | + } |
|---|
| 888 | +#endif |
|---|
| 889 | + |
|---|
| 890 | +#ifdef CONFIG_SECURITY |
|---|
| 891 | + { |
|---|
| 892 | + int num_security_xattrs = 1; |
|---|
| 893 | + |
|---|
| 894 | +#ifdef CONFIG_INTEGRITY |
|---|
| 895 | + num_security_xattrs++; |
|---|
| 896 | +#endif |
|---|
| 897 | + /* |
|---|
| 898 | + * We assume that security xattrs are never more than 1k. |
|---|
| 899 | + * In practice they are under 128 bytes. |
|---|
| 900 | + */ |
|---|
| 901 | + nblocks += num_security_xattrs * |
|---|
| 902 | + __ext4_xattr_set_credits(sb, NULL /* inode */, |
|---|
| 903 | + NULL /* block_bh */, 1024, |
|---|
| 904 | + true /* is_create */); |
|---|
| 905 | + } |
|---|
| 906 | +#endif |
|---|
| 907 | + if (encrypt) |
|---|
| 908 | + nblocks += __ext4_xattr_set_credits(sb, |
|---|
| 909 | + NULL /* inode */, |
|---|
| 910 | + NULL /* block_bh */, |
|---|
| 911 | + FSCRYPT_SET_CONTEXT_MAX_SIZE, |
|---|
| 912 | + true /* is_create */); |
|---|
| 913 | + return nblocks; |
|---|
| 735 | 914 | } |
|---|
| 736 | 915 | |
|---|
| 737 | 916 | /* |
|---|
| .. | .. |
|---|
| 763 | 942 | struct inode *ret; |
|---|
| 764 | 943 | ext4_group_t i; |
|---|
| 765 | 944 | ext4_group_t flex_group; |
|---|
| 766 | | - struct ext4_group_info *grp; |
|---|
| 767 | | - int encrypt = 0; |
|---|
| 945 | + struct ext4_group_info *grp = NULL; |
|---|
| 946 | + bool encrypt = false; |
|---|
| 768 | 947 | |
|---|
| 769 | 948 | /* Cannot create files in a deleted directory */ |
|---|
| 770 | 949 | if (!dir || !dir->i_nlink) |
|---|
| .. | .. |
|---|
| 775 | 954 | |
|---|
| 776 | 955 | if (unlikely(ext4_forced_shutdown(sbi))) |
|---|
| 777 | 956 | return ERR_PTR(-EIO); |
|---|
| 778 | | - |
|---|
| 779 | | - if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && |
|---|
| 780 | | - (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) && |
|---|
| 781 | | - !(i_flags & EXT4_EA_INODE_FL)) { |
|---|
| 782 | | - err = fscrypt_get_encryption_info(dir); |
|---|
| 783 | | - if (err) |
|---|
| 784 | | - return ERR_PTR(err); |
|---|
| 785 | | - if (!fscrypt_has_encryption_key(dir)) |
|---|
| 786 | | - return ERR_PTR(-ENOKEY); |
|---|
| 787 | | - encrypt = 1; |
|---|
| 788 | | - } |
|---|
| 789 | | - |
|---|
| 790 | | - if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { |
|---|
| 791 | | -#ifdef CONFIG_EXT4_FS_POSIX_ACL |
|---|
| 792 | | - struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT); |
|---|
| 793 | | - |
|---|
| 794 | | - if (IS_ERR(p)) |
|---|
| 795 | | - return ERR_CAST(p); |
|---|
| 796 | | - if (p) { |
|---|
| 797 | | - int acl_size = p->a_count * sizeof(ext4_acl_entry); |
|---|
| 798 | | - |
|---|
| 799 | | - nblocks += (S_ISDIR(mode) ? 2 : 1) * |
|---|
| 800 | | - __ext4_xattr_set_credits(sb, NULL /* inode */, |
|---|
| 801 | | - NULL /* block_bh */, acl_size, |
|---|
| 802 | | - true /* is_create */); |
|---|
| 803 | | - posix_acl_release(p); |
|---|
| 804 | | - } |
|---|
| 805 | | -#endif |
|---|
| 806 | | - |
|---|
| 807 | | -#ifdef CONFIG_SECURITY |
|---|
| 808 | | - { |
|---|
| 809 | | - int num_security_xattrs = 1; |
|---|
| 810 | | - |
|---|
| 811 | | -#ifdef CONFIG_INTEGRITY |
|---|
| 812 | | - num_security_xattrs++; |
|---|
| 813 | | -#endif |
|---|
| 814 | | - /* |
|---|
| 815 | | - * We assume that security xattrs are never |
|---|
| 816 | | - * more than 1k. In practice they are under |
|---|
| 817 | | - * 128 bytes. |
|---|
| 818 | | - */ |
|---|
| 819 | | - nblocks += num_security_xattrs * |
|---|
| 820 | | - __ext4_xattr_set_credits(sb, NULL /* inode */, |
|---|
| 821 | | - NULL /* block_bh */, 1024, |
|---|
| 822 | | - true /* is_create */); |
|---|
| 823 | | - } |
|---|
| 824 | | -#endif |
|---|
| 825 | | - if (encrypt) |
|---|
| 826 | | - nblocks += __ext4_xattr_set_credits(sb, |
|---|
| 827 | | - NULL /* inode */, NULL /* block_bh */, |
|---|
| 828 | | - FSCRYPT_SET_CONTEXT_MAX_SIZE, |
|---|
| 829 | | - true /* is_create */); |
|---|
| 830 | | - } |
|---|
| 831 | 957 | |
|---|
| 832 | 958 | ngroups = ext4_get_groups_count(sb); |
|---|
| 833 | 959 | trace_ext4_request_inode(dir, mode); |
|---|
| .. | .. |
|---|
| 858 | 984 | else |
|---|
| 859 | 985 | ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID); |
|---|
| 860 | 986 | |
|---|
| 987 | + if (!(i_flags & EXT4_EA_INODE_FL)) { |
|---|
| 988 | + err = fscrypt_prepare_new_inode(dir, inode, &encrypt); |
|---|
| 989 | + if (err) |
|---|
| 990 | + goto out; |
|---|
| 991 | + } |
|---|
| 992 | + |
|---|
| 861 | 993 | err = dquot_initialize(inode); |
|---|
| 862 | 994 | if (err) |
|---|
| 863 | 995 | goto out; |
|---|
| 996 | + |
|---|
| 997 | + if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { |
|---|
| 998 | + ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt); |
|---|
| 999 | + if (ret2 < 0) { |
|---|
| 1000 | + err = ret2; |
|---|
| 1001 | + goto out; |
|---|
| 1002 | + } |
|---|
| 1003 | + nblocks += ret2; |
|---|
| 1004 | + } |
|---|
| 864 | 1005 | |
|---|
| 865 | 1006 | if (!goal) |
|---|
| 866 | 1007 | goal = sbi->s_inode_goal; |
|---|
| .. | .. |
|---|
| 901 | 1042 | if (ext4_free_inodes_count(sb, gdp) == 0) |
|---|
| 902 | 1043 | goto next_group; |
|---|
| 903 | 1044 | |
|---|
| 904 | | - grp = ext4_get_group_info(sb, group); |
|---|
| 905 | | - /* Skip groups with already-known suspicious inode tables */ |
|---|
| 906 | | - if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) |
|---|
| 907 | | - goto next_group; |
|---|
| 1045 | + if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { |
|---|
| 1046 | + grp = ext4_get_group_info(sb, group); |
|---|
| 1047 | + /* |
|---|
| 1048 | + * Skip groups with already-known suspicious inode |
|---|
| 1049 | + * tables |
|---|
| 1050 | + */ |
|---|
| 1051 | + if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) |
|---|
| 1052 | + goto next_group; |
|---|
| 1053 | + } |
|---|
| 908 | 1054 | |
|---|
| 909 | 1055 | brelse(inode_bitmap_bh); |
|---|
| 910 | 1056 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); |
|---|
| 911 | 1057 | /* Skip groups with suspicious inode tables */ |
|---|
| 912 | | - if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || |
|---|
| 1058 | + if (((!(sbi->s_mount_state & EXT4_FC_REPLAY)) |
|---|
| 1059 | + && EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) || |
|---|
| 913 | 1060 | IS_ERR(inode_bitmap_bh)) { |
|---|
| 914 | 1061 | inode_bitmap_bh = NULL; |
|---|
| 915 | 1062 | goto next_group; |
|---|
| .. | .. |
|---|
| 928 | 1075 | goto next_group; |
|---|
| 929 | 1076 | } |
|---|
| 930 | 1077 | |
|---|
| 931 | | - if (!handle) { |
|---|
| 1078 | + if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) { |
|---|
| 932 | 1079 | BUG_ON(nblocks <= 0); |
|---|
| 933 | 1080 | handle = __ext4_journal_start_sb(dir->i_sb, line_no, |
|---|
| 934 | | - handle_type, nblocks, |
|---|
| 935 | | - 0); |
|---|
| 1081 | + handle_type, nblocks, 0, |
|---|
| 1082 | + ext4_trans_default_revoke_credits(sb)); |
|---|
| 936 | 1083 | if (IS_ERR(handle)) { |
|---|
| 937 | 1084 | err = PTR_ERR(handle); |
|---|
| 938 | 1085 | ext4_std_error(sb, err); |
|---|
| .. | .. |
|---|
| 1032 | 1179 | /* Update the relevant bg descriptor fields */ |
|---|
| 1033 | 1180 | if (ext4_has_group_desc_csum(sb)) { |
|---|
| 1034 | 1181 | int free; |
|---|
| 1035 | | - struct ext4_group_info *grp = ext4_get_group_info(sb, group); |
|---|
| 1182 | + struct ext4_group_info *grp = NULL; |
|---|
| 1036 | 1183 | |
|---|
| 1037 | | - down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ |
|---|
| 1184 | + if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { |
|---|
| 1185 | + grp = ext4_get_group_info(sb, group); |
|---|
| 1186 | + if (!grp) { |
|---|
| 1187 | + err = -EFSCORRUPTED; |
|---|
| 1188 | + goto out; |
|---|
| 1189 | + } |
|---|
| 1190 | + down_read(&grp->alloc_sem); /* |
|---|
| 1191 | + * protect vs itable |
|---|
| 1192 | + * lazyinit |
|---|
| 1193 | + */ |
|---|
| 1194 | + } |
|---|
| 1038 | 1195 | ext4_lock_group(sb, group); /* while we modify the bg desc */ |
|---|
| 1039 | 1196 | free = EXT4_INODES_PER_GROUP(sb) - |
|---|
| 1040 | 1197 | ext4_itable_unused_count(sb, gdp); |
|---|
| .. | .. |
|---|
| 1050 | 1207 | if (ino > free) |
|---|
| 1051 | 1208 | ext4_itable_unused_set(sb, gdp, |
|---|
| 1052 | 1209 | (EXT4_INODES_PER_GROUP(sb) - ino)); |
|---|
| 1053 | | - up_read(&grp->alloc_sem); |
|---|
| 1210 | + if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) |
|---|
| 1211 | + up_read(&grp->alloc_sem); |
|---|
| 1054 | 1212 | } else { |
|---|
| 1055 | 1213 | ext4_lock_group(sb, group); |
|---|
| 1056 | 1214 | } |
|---|
| .. | .. |
|---|
| 1108 | 1266 | ei->i_block_group = group; |
|---|
| 1109 | 1267 | ei->i_last_alloc_group = ~0; |
|---|
| 1110 | 1268 | |
|---|
| 1111 | | - ext4_set_inode_flags(inode); |
|---|
| 1269 | + ext4_set_inode_flags(inode, true); |
|---|
| 1112 | 1270 | if (IS_DIRSYNC(inode)) |
|---|
| 1113 | 1271 | ext4_handle_sync(handle); |
|---|
| 1114 | 1272 | if (insert_inode_locked(inode) < 0) { |
|---|
| .. | .. |
|---|
| 1141 | 1299 | |
|---|
| 1142 | 1300 | ei->i_extra_isize = sbi->s_want_extra_isize; |
|---|
| 1143 | 1301 | ei->i_inline_off = 0; |
|---|
| 1144 | | - if (ext4_has_feature_inline_data(sb)) |
|---|
| 1302 | + if (ext4_has_feature_inline_data(sb) && |
|---|
| 1303 | + (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode))) |
|---|
| 1145 | 1304 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
|---|
| 1146 | 1305 | ret = inode; |
|---|
| 1147 | 1306 | err = dquot_alloc_inode(inode); |
|---|
| .. | .. |
|---|
| 1154 | 1313 | * prevent its deduplication. |
|---|
| 1155 | 1314 | */ |
|---|
| 1156 | 1315 | if (encrypt) { |
|---|
| 1157 | | - err = fscrypt_inherit_context(dir, inode, handle, true); |
|---|
| 1316 | + err = fscrypt_set_context(inode, handle); |
|---|
| 1158 | 1317 | if (err) |
|---|
| 1159 | 1318 | goto fail_free_drop; |
|---|
| 1160 | 1319 | } |
|---|
| .. | .. |
|---|
| 1223 | 1382 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
|---|
| 1224 | 1383 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
|---|
| 1225 | 1384 | if (IS_ERR(bitmap_bh)) |
|---|
| 1226 | | - return (struct inode *) bitmap_bh; |
|---|
| 1385 | + return ERR_CAST(bitmap_bh); |
|---|
| 1227 | 1386 | |
|---|
| 1228 | 1387 | /* Having the inode bit set should be a 100% indicator that this |
|---|
| 1229 | 1388 | * is a valid orphan (no e2fsck run on fs). Orphans also include |
|---|
| .. | .. |
|---|
| 1235 | 1394 | inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL); |
|---|
| 1236 | 1395 | if (IS_ERR(inode)) { |
|---|
| 1237 | 1396 | err = PTR_ERR(inode); |
|---|
| 1238 | | - ext4_error(sb, "couldn't read orphan inode %lu (err %d)", |
|---|
| 1239 | | - ino, err); |
|---|
| 1397 | + ext4_error_err(sb, -err, |
|---|
| 1398 | + "couldn't read orphan inode %lu (err %d)", |
|---|
| 1399 | + ino, err); |
|---|
| 1400 | + brelse(bitmap_bh); |
|---|
| 1240 | 1401 | return inode; |
|---|
| 1241 | 1402 | } |
|---|
| 1242 | 1403 | |
|---|
| .. | .. |
|---|
| 1369 | 1530 | } |
|---|
| 1370 | 1531 | |
|---|
| 1371 | 1532 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
|---|
| 1372 | | - if (!gdp) |
|---|
| 1533 | + if (!gdp || !grp) |
|---|
| 1373 | 1534 | goto out; |
|---|
| 1374 | 1535 | |
|---|
| 1375 | 1536 | /* |
|---|
| .. | .. |
|---|
| 1447 | 1608 | if (ret < 0) |
|---|
| 1448 | 1609 | goto err_out; |
|---|
| 1449 | 1610 | if (barrier) |
|---|
| 1450 | | - blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); |
|---|
| 1611 | + blkdev_issue_flush(sb->s_bdev, GFP_NOFS); |
|---|
| 1451 | 1612 | |
|---|
| 1452 | 1613 | skip_zeroout: |
|---|
| 1453 | 1614 | ext4_lock_group(sb, group); |
|---|