1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2ac27a0ecSDave Kleikamp /* 3617ba13bSMingming Cao * linux/fs/ext4/resize.c 4ac27a0ecSDave Kleikamp * 5617ba13bSMingming Cao * Support for resizing an ext4 filesystem while it is mounted. 6ac27a0ecSDave Kleikamp * 7ac27a0ecSDave Kleikamp * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com> 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * This could probably be made into a module, because it is not often in use. 10ac27a0ecSDave Kleikamp */ 11ac27a0ecSDave Kleikamp 12ac27a0ecSDave Kleikamp 13617ba13bSMingming Cao #define EXT4FS_DEBUG 14ac27a0ecSDave Kleikamp 15ac27a0ecSDave Kleikamp #include <linux/errno.h> 16ac27a0ecSDave Kleikamp #include <linux/slab.h> 17a861fb9fSWang Qing #include <linux/jiffies.h> 18ac27a0ecSDave Kleikamp 193dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 20ac27a0ecSDave Kleikamp 211d0c3924STheodore Ts'o struct ext4_rcu_ptr { 221d0c3924STheodore Ts'o struct rcu_head rcu; 231d0c3924STheodore Ts'o void *ptr; 241d0c3924STheodore Ts'o }; 251d0c3924STheodore Ts'o 261d0c3924STheodore Ts'o static void ext4_rcu_ptr_callback(struct rcu_head *head) 271d0c3924STheodore Ts'o { 281d0c3924STheodore Ts'o struct ext4_rcu_ptr *ptr; 291d0c3924STheodore Ts'o 301d0c3924STheodore Ts'o ptr = container_of(head, struct ext4_rcu_ptr, rcu); 311d0c3924STheodore Ts'o kvfree(ptr->ptr); 321d0c3924STheodore Ts'o kfree(ptr); 331d0c3924STheodore Ts'o } 341d0c3924STheodore Ts'o 351d0c3924STheodore Ts'o void ext4_kvfree_array_rcu(void *to_free) 361d0c3924STheodore Ts'o { 371d0c3924STheodore Ts'o struct ext4_rcu_ptr *ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); 381d0c3924STheodore Ts'o 391d0c3924STheodore Ts'o if (ptr) { 401d0c3924STheodore Ts'o ptr->ptr = to_free; 411d0c3924STheodore Ts'o call_rcu(&ptr->rcu, ext4_rcu_ptr_callback); 421d0c3924STheodore Ts'o return; 431d0c3924STheodore Ts'o } 441d0c3924STheodore Ts'o synchronize_rcu(); 451d0c3924STheodore Ts'o kvfree(to_free); 461d0c3924STheodore Ts'o } 471d0c3924STheodore Ts'o 488f82f840SYongqiang Yang int ext4_resize_begin(struct super_block *sb) 498f82f840SYongqiang Yang { 505f8c1093STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 518f82f840SYongqiang Yang int ret = 0; 528f82f840SYongqiang Yang 538f82f840SYongqiang Yang if (!capable(CAP_SYS_RESOURCE)) 548f82f840SYongqiang Yang return -EPERM; 558f82f840SYongqiang Yang 56ce723c31SYongqiang Yang /* 57b55c3cd1SZhang Yi * If the reserved GDT blocks is non-zero, the resize_inode feature 58b55c3cd1SZhang Yi * should always be set. 59b55c3cd1SZhang Yi */ 60b55c3cd1SZhang Yi if (EXT4_SB(sb)->s_es->s_reserved_gdt_blocks && 61b55c3cd1SZhang Yi !ext4_has_feature_resize_inode(sb)) { 62b55c3cd1SZhang Yi ext4_error(sb, "resize_inode disabled but reserved GDT blocks non-zero"); 63b55c3cd1SZhang Yi return -EFSCORRUPTED; 64b55c3cd1SZhang Yi } 65b55c3cd1SZhang Yi 66b55c3cd1SZhang Yi /* 67011fa994STheodore Ts'o * If we are not using the primary superblock/GDT copy don't resize, 68011fa994STheodore Ts'o * because the user tools have no way of handling this. Probably a 69011fa994STheodore Ts'o * bad time to do it anyways. 70011fa994STheodore Ts'o */ 715f8c1093STheodore Ts'o if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) != 72011fa994STheodore Ts'o le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 73011fa994STheodore Ts'o ext4_warning(sb, "won't resize using backup superblock at %llu", 74011fa994STheodore Ts'o (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 75011fa994STheodore Ts'o return -EPERM; 76011fa994STheodore Ts'o } 77011fa994STheodore Ts'o 78011fa994STheodore Ts'o /* 79ce723c31SYongqiang Yang * We are not allowed to do online-resizing on a filesystem mounted 80ce723c31SYongqiang Yang * with error, because it can destroy the filesystem easily. 81ce723c31SYongqiang Yang */ 82ce723c31SYongqiang Yang if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 83ce723c31SYongqiang Yang ext4_warning(sb, "There are errors in the filesystem, " 848d2ae1cbSJakub Wilk "so online resizing is not allowed"); 85ce723c31SYongqiang Yang return -EPERM; 86ce723c31SYongqiang Yang } 87ce723c31SYongqiang Yang 88b1489186SJosh Triplett if (ext4_has_feature_sparse_super2(sb)) { 89b1489186SJosh Triplett ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2"); 90b1489186SJosh Triplett return -EOPNOTSUPP; 91b1489186SJosh Triplett } 92e9f9f61dSJosh Triplett 939549a168STheodore Ts'o if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING, 949549a168STheodore Ts'o &EXT4_SB(sb)->s_ext4_flags)) 958f82f840SYongqiang Yang ret = -EBUSY; 968f82f840SYongqiang Yang 978f82f840SYongqiang Yang return ret; 988f82f840SYongqiang Yang } 998f82f840SYongqiang Yang 100827891a3STheodore Ts'o int ext4_resize_end(struct super_block *sb, bool update_backups) 1018f82f840SYongqiang Yang { 1029549a168STheodore Ts'o clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags); 1034e857c58SPeter Zijlstra smp_mb__after_atomic(); 104827891a3STheodore Ts'o if (update_backups) 105827891a3STheodore Ts'o return ext4_update_overhead(sb, true); 106827891a3STheodore Ts'o return 0; 1078f82f840SYongqiang Yang } 1088f82f840SYongqiang Yang 10901f795f9SYongqiang Yang static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb, 11001f795f9SYongqiang Yang ext4_group_t group) { 11101f795f9SYongqiang Yang return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) << 11201f795f9SYongqiang Yang EXT4_DESC_PER_BLOCK_BITS(sb); 11301f795f9SYongqiang Yang } 11401f795f9SYongqiang Yang 11501f795f9SYongqiang Yang static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb, 11601f795f9SYongqiang Yang ext4_group_t group) { 11701f795f9SYongqiang Yang group = ext4_meta_bg_first_group(sb, group); 11801f795f9SYongqiang Yang return ext4_group_first_block_no(sb, group); 11901f795f9SYongqiang Yang } 12001f795f9SYongqiang Yang 12101f795f9SYongqiang Yang static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb, 12201f795f9SYongqiang Yang ext4_group_t group) { 12301f795f9SYongqiang Yang ext4_grpblk_t overhead; 12401f795f9SYongqiang Yang overhead = ext4_bg_num_gdb(sb, group); 12501f795f9SYongqiang Yang if (ext4_bg_has_super(sb, group)) 12601f795f9SYongqiang Yang overhead += 1 + 12701f795f9SYongqiang Yang le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 12801f795f9SYongqiang Yang return overhead; 12901f795f9SYongqiang Yang } 13001f795f9SYongqiang Yang 131ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 132ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 133ac27a0ecSDave Kleikamp 134ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 135617ba13bSMingming Cao struct ext4_new_group_data *input) 136ac27a0ecSDave Kleikamp { 137617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 138617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 139bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 140617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 141fd2d4291SAvantika Mathur ext4_group_t group = input->group; 142617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 143b302ef2dSTheodore Ts'o unsigned overhead; 144b302ef2dSTheodore Ts'o ext4_fsblk_t metaend; 145ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 1463a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 147ac27a0ecSDave Kleikamp int err = -EINVAL; 148ac27a0ecSDave Kleikamp 149b302ef2dSTheodore Ts'o if (group != sbi->s_groups_count) { 150b302ef2dSTheodore Ts'o ext4_warning(sb, "Cannot add at group %u (only %u groups)", 151b302ef2dSTheodore Ts'o input->group, sbi->s_groups_count); 152b302ef2dSTheodore Ts'o return -EINVAL; 153b302ef2dSTheodore Ts'o } 154b302ef2dSTheodore Ts'o 155b302ef2dSTheodore Ts'o overhead = ext4_group_overhead_blocks(sb, group); 156b302ef2dSTheodore Ts'o metaend = start + overhead; 157d77147ffSharshads input->free_clusters_count = free_blocks_count = 158ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 159ac27a0ecSDave Kleikamp 160ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 161617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 162ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 163617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 164ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 165ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 166ac27a0ecSDave Kleikamp 1673a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 168b302ef2dSTheodore Ts'o if (offset != 0) 16912062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 170ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 17112062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 172ac27a0ecSDave Kleikamp input->reserved_blocks); 173ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 17412062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 175ac27a0ecSDave Kleikamp input->blocks_count); 176fb265c9cSTheodore Ts'o else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) { 177fb265c9cSTheodore Ts'o err = PTR_ERR(bh); 178fb265c9cSTheodore Ts'o bh = NULL; 17912062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 180ac27a0ecSDave Kleikamp end - 1); 181fb265c9cSTheodore Ts'o } else if (outside(input->block_bitmap, start, end)) 18212062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 1831939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 184ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 18512062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 1861939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 187ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 188ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 18912062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1901939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 191ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 19212062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1931939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 194ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 19512062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 19612062dddSEric Sandeen "(%llu-%llu)", 1971939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1981939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 199ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 20012062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 20112062dddSEric Sandeen "(%llu-%llu)", 2021939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 2031939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 204ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 20512062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 2061939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 2071939e49aSRandy Dunlap start, metaend - 1); 208ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 20912062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 2101939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 2111939e49aSRandy Dunlap start, metaend - 1); 212ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 213ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 21412062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 21512062dddSEric Sandeen "(%llu-%llu)", 2161939e49aSRandy Dunlap (unsigned long long)input->inode_table, 2171939e49aSRandy Dunlap itend - 1, start, metaend - 1); 218ac27a0ecSDave Kleikamp else 219ac27a0ecSDave Kleikamp err = 0; 220ac27a0ecSDave Kleikamp brelse(bh); 221ac27a0ecSDave Kleikamp 222ac27a0ecSDave Kleikamp return err; 223ac27a0ecSDave Kleikamp } 224ac27a0ecSDave Kleikamp 22528c7bac0SYongqiang Yang /* 22628c7bac0SYongqiang Yang * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex 22728c7bac0SYongqiang Yang * group each time. 22828c7bac0SYongqiang Yang */ 22928c7bac0SYongqiang Yang struct ext4_new_flex_group_data { 23028c7bac0SYongqiang Yang struct ext4_new_group_data *groups; /* new_group_data for groups 23128c7bac0SYongqiang Yang in the flex group */ 23228c7bac0SYongqiang Yang __u16 *bg_flags; /* block group flags of groups 23328c7bac0SYongqiang Yang in @groups */ 23428c7bac0SYongqiang Yang ext4_group_t count; /* number of groups in @groups 23528c7bac0SYongqiang Yang */ 23628c7bac0SYongqiang Yang }; 23728c7bac0SYongqiang Yang 23828c7bac0SYongqiang Yang /* 23928c7bac0SYongqiang Yang * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of 24028c7bac0SYongqiang Yang * @flexbg_size. 24128c7bac0SYongqiang Yang * 24228c7bac0SYongqiang Yang * Returns NULL on failure otherwise address of the allocated structure. 24328c7bac0SYongqiang Yang */ 24428c7bac0SYongqiang Yang static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) 24528c7bac0SYongqiang Yang { 24628c7bac0SYongqiang Yang struct ext4_new_flex_group_data *flex_gd; 24728c7bac0SYongqiang Yang 24828c7bac0SYongqiang Yang flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS); 24928c7bac0SYongqiang Yang if (flex_gd == NULL) 25028c7bac0SYongqiang Yang goto out3; 25128c7bac0SYongqiang Yang 25246901760SInsu Yun if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data)) 253967ac8afSHaogang Chen goto out2; 25428c7bac0SYongqiang Yang flex_gd->count = flexbg_size; 25528c7bac0SYongqiang Yang 2566da2ec56SKees Cook flex_gd->groups = kmalloc_array(flexbg_size, 2576da2ec56SKees Cook sizeof(struct ext4_new_group_data), 2586da2ec56SKees Cook GFP_NOFS); 25928c7bac0SYongqiang Yang if (flex_gd->groups == NULL) 26028c7bac0SYongqiang Yang goto out2; 26128c7bac0SYongqiang Yang 2626da2ec56SKees Cook flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16), 2636da2ec56SKees Cook GFP_NOFS); 26428c7bac0SYongqiang Yang if (flex_gd->bg_flags == NULL) 26528c7bac0SYongqiang Yang goto out1; 26628c7bac0SYongqiang Yang 26728c7bac0SYongqiang Yang return flex_gd; 26828c7bac0SYongqiang Yang 26928c7bac0SYongqiang Yang out1: 27028c7bac0SYongqiang Yang kfree(flex_gd->groups); 27128c7bac0SYongqiang Yang out2: 27228c7bac0SYongqiang Yang kfree(flex_gd); 27328c7bac0SYongqiang Yang out3: 27428c7bac0SYongqiang Yang return NULL; 27528c7bac0SYongqiang Yang } 27628c7bac0SYongqiang Yang 27728c7bac0SYongqiang Yang static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd) 27828c7bac0SYongqiang Yang { 27928c7bac0SYongqiang Yang kfree(flex_gd->bg_flags); 28028c7bac0SYongqiang Yang kfree(flex_gd->groups); 28128c7bac0SYongqiang Yang kfree(flex_gd); 28228c7bac0SYongqiang Yang } 28328c7bac0SYongqiang Yang 2843fbea4b3SYongqiang Yang /* 2853fbea4b3SYongqiang Yang * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps 2863fbea4b3SYongqiang Yang * and inode tables for a flex group. 2873fbea4b3SYongqiang Yang * 2883fbea4b3SYongqiang Yang * This function is used by 64bit-resize. Note that this function allocates 2893fbea4b3SYongqiang Yang * group tables from the 1st group of groups contained by @flexgd, which may 2903fbea4b3SYongqiang Yang * be a partial of a flex group. 2913fbea4b3SYongqiang Yang * 2923fbea4b3SYongqiang Yang * @sb: super block of fs to which the groups belongs 29303c1c290SYongqiang Yang * 29403c1c290SYongqiang Yang * Returns 0 on a successful allocation of the metadata blocks in the 29503c1c290SYongqiang Yang * block group. 2963fbea4b3SYongqiang Yang */ 29703c1c290SYongqiang Yang static int ext4_alloc_group_tables(struct super_block *sb, 2983fbea4b3SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 2993fbea4b3SYongqiang Yang int flexbg_size) 3003fbea4b3SYongqiang Yang { 3013fbea4b3SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 3023fbea4b3SYongqiang Yang ext4_fsblk_t start_blk; 3033fbea4b3SYongqiang Yang ext4_fsblk_t last_blk; 3043fbea4b3SYongqiang Yang ext4_group_t src_group; 3053fbea4b3SYongqiang Yang ext4_group_t bb_index = 0; 3063fbea4b3SYongqiang Yang ext4_group_t ib_index = 0; 3073fbea4b3SYongqiang Yang ext4_group_t it_index = 0; 3083fbea4b3SYongqiang Yang ext4_group_t group; 3093fbea4b3SYongqiang Yang ext4_group_t last_group; 3103fbea4b3SYongqiang Yang unsigned overhead; 311b93c9535STheodore Ts'o __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0; 312d77147ffSharshads int i; 3133fbea4b3SYongqiang Yang 3143fbea4b3SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 3153fbea4b3SYongqiang Yang 3163fbea4b3SYongqiang Yang src_group = group_data[0].group; 3173fbea4b3SYongqiang Yang last_group = src_group + flex_gd->count - 1; 3183fbea4b3SYongqiang Yang 3193fbea4b3SYongqiang Yang BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) != 3203fbea4b3SYongqiang Yang (last_group & ~(flexbg_size - 1)))); 3213fbea4b3SYongqiang Yang next_group: 3223fbea4b3SYongqiang Yang group = group_data[0].group; 32303c1c290SYongqiang Yang if (src_group >= group_data[0].group + flex_gd->count) 32403c1c290SYongqiang Yang return -ENOSPC; 3253fbea4b3SYongqiang Yang start_blk = ext4_group_first_block_no(sb, src_group); 3263fbea4b3SYongqiang Yang last_blk = start_blk + group_data[src_group - group].blocks_count; 3273fbea4b3SYongqiang Yang 32801f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 3293fbea4b3SYongqiang Yang 3303fbea4b3SYongqiang Yang start_blk += overhead; 3313fbea4b3SYongqiang Yang 3323fbea4b3SYongqiang Yang /* We collect contiguous blocks as much as possible. */ 3333fbea4b3SYongqiang Yang src_group++; 33401f795f9SYongqiang Yang for (; src_group <= last_group; src_group++) { 33501f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 336b93c9535STheodore Ts'o if (overhead == 0) 3373fbea4b3SYongqiang Yang last_blk += group_data[src_group - group].blocks_count; 3383fbea4b3SYongqiang Yang else 3393fbea4b3SYongqiang Yang break; 34001f795f9SYongqiang Yang } 3413fbea4b3SYongqiang Yang 3423fbea4b3SYongqiang Yang /* Allocate block bitmaps */ 3433fbea4b3SYongqiang Yang for (; bb_index < flex_gd->count; bb_index++) { 3443fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3453fbea4b3SYongqiang Yang goto next_group; 3463fbea4b3SYongqiang Yang group_data[bb_index].block_bitmap = start_blk++; 347bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3483fbea4b3SYongqiang Yang group -= group_data[0].group; 349d77147ffSharshads group_data[group].mdata_blocks++; 350b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3513fbea4b3SYongqiang Yang } 3523fbea4b3SYongqiang Yang 3533fbea4b3SYongqiang Yang /* Allocate inode bitmaps */ 3543fbea4b3SYongqiang Yang for (; ib_index < flex_gd->count; ib_index++) { 3553fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3563fbea4b3SYongqiang Yang goto next_group; 3573fbea4b3SYongqiang Yang group_data[ib_index].inode_bitmap = start_blk++; 358bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3593fbea4b3SYongqiang Yang group -= group_data[0].group; 360d77147ffSharshads group_data[group].mdata_blocks++; 361b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3623fbea4b3SYongqiang Yang } 3633fbea4b3SYongqiang Yang 3643fbea4b3SYongqiang Yang /* Allocate inode tables */ 3653fbea4b3SYongqiang Yang for (; it_index < flex_gd->count; it_index++) { 366b93c9535STheodore Ts'o unsigned int itb = EXT4_SB(sb)->s_itb_per_group; 367b93c9535STheodore Ts'o ext4_fsblk_t next_group_start; 368b93c9535STheodore Ts'o 369b93c9535STheodore Ts'o if (start_blk + itb > last_blk) 3703fbea4b3SYongqiang Yang goto next_group; 3713fbea4b3SYongqiang Yang group_data[it_index].inode_table = start_blk; 372b93c9535STheodore Ts'o group = ext4_get_group_number(sb, start_blk); 373b93c9535STheodore Ts'o next_group_start = ext4_group_first_block_no(sb, group + 1); 3743fbea4b3SYongqiang Yang group -= group_data[0].group; 3753fbea4b3SYongqiang Yang 376b93c9535STheodore Ts'o if (start_blk + itb > next_group_start) { 377b93c9535STheodore Ts'o flex_gd->bg_flags[group + 1] &= uninit_mask; 378b93c9535STheodore Ts'o overhead = start_blk + itb - next_group_start; 379d77147ffSharshads group_data[group + 1].mdata_blocks += overhead; 380b93c9535STheodore Ts'o itb -= overhead; 381b93c9535STheodore Ts'o } 382b93c9535STheodore Ts'o 383d77147ffSharshads group_data[group].mdata_blocks += itb; 384b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3853fbea4b3SYongqiang Yang start_blk += EXT4_SB(sb)->s_itb_per_group; 3863fbea4b3SYongqiang Yang } 3873fbea4b3SYongqiang Yang 388d77147ffSharshads /* Update free clusters count to exclude metadata blocks */ 389d77147ffSharshads for (i = 0; i < flex_gd->count; i++) { 390d77147ffSharshads group_data[i].free_clusters_count -= 391d77147ffSharshads EXT4_NUM_B2C(EXT4_SB(sb), 392d77147ffSharshads group_data[i].mdata_blocks); 393d77147ffSharshads } 394d77147ffSharshads 3953fbea4b3SYongqiang Yang if (test_opt(sb, DEBUG)) { 3963fbea4b3SYongqiang Yang int i; 3973fbea4b3SYongqiang Yang group = group_data[0].group; 3983fbea4b3SYongqiang Yang 3993fbea4b3SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: adding a flex group with " 4003fbea4b3SYongqiang Yang "%d groups, flexbg size is %d:\n", flex_gd->count, 4013fbea4b3SYongqiang Yang flexbg_size); 4023fbea4b3SYongqiang Yang 4033fbea4b3SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 404d77147ffSharshads ext4_debug( 405d77147ffSharshads "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n", 4063fbea4b3SYongqiang Yang ext4_bg_has_super(sb, group + i) ? "normal" : 4073fbea4b3SYongqiang Yang "no-super", group + i, 4083fbea4b3SYongqiang Yang group_data[i].blocks_count, 409d77147ffSharshads group_data[i].free_clusters_count, 410d77147ffSharshads group_data[i].mdata_blocks); 4113fbea4b3SYongqiang Yang } 4123fbea4b3SYongqiang Yang } 41303c1c290SYongqiang Yang return 0; 4143fbea4b3SYongqiang Yang } 4153fbea4b3SYongqiang Yang 416ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 417617ba13bSMingming Cao ext4_fsblk_t blk) 418ac27a0ecSDave Kleikamp { 419ac27a0ecSDave Kleikamp struct buffer_head *bh; 420ac27a0ecSDave Kleikamp int err; 421ac27a0ecSDave Kleikamp 422ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 423aebf0243SWang Shilong if (unlikely(!bh)) 424860d21e2STheodore Ts'o return ERR_PTR(-ENOMEM); 4255d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 426188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE); 427188c299eSJan Kara if (err) { 428ac27a0ecSDave Kleikamp brelse(bh); 429ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 430ac27a0ecSDave Kleikamp } else { 431ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 432ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 433ac27a0ecSDave Kleikamp } 434ac27a0ecSDave Kleikamp 435ac27a0ecSDave Kleikamp return bh; 436ac27a0ecSDave Kleikamp } 437ac27a0ecSDave Kleikamp 43883448bdfSJan Kara static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits) 43983448bdfSJan Kara { 44083448bdfSJan Kara return ext4_journal_ensure_credits_fn(handle, credits, 44183448bdfSJan Kara EXT4_MAX_TRANS_DATA, 0, 0); 44283448bdfSJan Kara } 44383448bdfSJan Kara 444ac27a0ecSDave Kleikamp /* 445d77147ffSharshads * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used. 44633afdcc5SYongqiang Yang * 44733afdcc5SYongqiang Yang * Helper function for ext4_setup_new_group_blocks() which set . 44833afdcc5SYongqiang Yang * 44933afdcc5SYongqiang Yang * @sb: super block 45033afdcc5SYongqiang Yang * @handle: journal handle 45133afdcc5SYongqiang Yang * @flex_gd: flex group data 45233afdcc5SYongqiang Yang */ 45333afdcc5SYongqiang Yang static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle, 45433afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 455d77147ffSharshads ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster) 45633afdcc5SYongqiang Yang { 457d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 458d77147ffSharshads ext4_group_t count = last_cluster - first_cluster + 1; 45933afdcc5SYongqiang Yang ext4_group_t count2; 46033afdcc5SYongqiang Yang 461d77147ffSharshads ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster, 462d77147ffSharshads last_cluster); 463d77147ffSharshads for (count2 = count; count > 0; 464d77147ffSharshads count -= count2, first_cluster += count2) { 46533afdcc5SYongqiang Yang ext4_fsblk_t start; 46633afdcc5SYongqiang Yang struct buffer_head *bh; 46733afdcc5SYongqiang Yang ext4_group_t group; 46833afdcc5SYongqiang Yang int err; 46933afdcc5SYongqiang Yang 470d77147ffSharshads group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster)); 471d77147ffSharshads start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group)); 47233afdcc5SYongqiang Yang group -= flex_gd->groups[0].group; 47333afdcc5SYongqiang Yang 474d77147ffSharshads count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start); 47533afdcc5SYongqiang Yang if (count2 > count) 47633afdcc5SYongqiang Yang count2 = count; 47733afdcc5SYongqiang Yang 47833afdcc5SYongqiang Yang if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) { 47933afdcc5SYongqiang Yang BUG_ON(flex_gd->count > 1); 48033afdcc5SYongqiang Yang continue; 48133afdcc5SYongqiang Yang } 48233afdcc5SYongqiang Yang 48383448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 484a4130367SJan Kara if (err < 0) 48533afdcc5SYongqiang Yang return err; 48633afdcc5SYongqiang Yang 48733afdcc5SYongqiang Yang bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap); 488aebf0243SWang Shilong if (unlikely(!bh)) 489860d21e2STheodore Ts'o return -ENOMEM; 49033afdcc5SYongqiang Yang 4915d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 492188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bh, 493188c299eSJan Kara EXT4_JTR_NONE); 494cea57941SVasily Averin if (err) { 495cea57941SVasily Averin brelse(bh); 49633afdcc5SYongqiang Yang return err; 497cea57941SVasily Averin } 498d77147ffSharshads ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", 499d77147ffSharshads first_cluster, first_cluster - start, count2); 500123e3016SRitesh Harjani mb_set_bits(bh->b_data, first_cluster - start, count2); 50133afdcc5SYongqiang Yang 50233afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 503cea57941SVasily Averin brelse(bh); 50433afdcc5SYongqiang Yang if (unlikely(err)) 50533afdcc5SYongqiang Yang return err; 50633afdcc5SYongqiang Yang } 50733afdcc5SYongqiang Yang 50833afdcc5SYongqiang Yang return 0; 50933afdcc5SYongqiang Yang } 51033afdcc5SYongqiang Yang 51133afdcc5SYongqiang Yang /* 51233afdcc5SYongqiang Yang * Set up the block and inode bitmaps, and the inode table for the new groups. 51333afdcc5SYongqiang Yang * This doesn't need to be part of the main transaction, since we are only 51433afdcc5SYongqiang Yang * changing blocks outside the actual filesystem. We still do journaling to 51533afdcc5SYongqiang Yang * ensure the recovery is correct in case of a failure just after resize. 51633afdcc5SYongqiang Yang * If any part of this fails, we simply abort the resize. 51733afdcc5SYongqiang Yang * 51833afdcc5SYongqiang Yang * setup_new_flex_group_blocks handles a flex group as follow: 51933afdcc5SYongqiang Yang * 1. copy super block and GDT, and initialize group tables if necessary. 52033afdcc5SYongqiang Yang * In this step, we only set bits in blocks bitmaps for blocks taken by 52133afdcc5SYongqiang Yang * super block and GDT. 52233afdcc5SYongqiang Yang * 2. allocate group tables in block bitmaps, that is, set bits in block 52333afdcc5SYongqiang Yang * bitmap for blocks taken by group tables. 52433afdcc5SYongqiang Yang */ 52533afdcc5SYongqiang Yang static int setup_new_flex_group_blocks(struct super_block *sb, 52633afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 52733afdcc5SYongqiang Yang { 52833afdcc5SYongqiang Yang int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group}; 52933afdcc5SYongqiang Yang ext4_fsblk_t start; 53033afdcc5SYongqiang Yang ext4_fsblk_t block; 53133afdcc5SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 53233afdcc5SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 53333afdcc5SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 53433afdcc5SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 53533afdcc5SYongqiang Yang handle_t *handle; 53633afdcc5SYongqiang Yang ext4_group_t group, count; 53733afdcc5SYongqiang Yang struct buffer_head *bh = NULL; 53833afdcc5SYongqiang Yang int reserved_gdb, i, j, err = 0, err2; 53901f795f9SYongqiang Yang int meta_bg; 54033afdcc5SYongqiang Yang 54133afdcc5SYongqiang Yang BUG_ON(!flex_gd->count || !group_data || 54233afdcc5SYongqiang Yang group_data[0].group != sbi->s_groups_count); 54333afdcc5SYongqiang Yang 54433afdcc5SYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 545e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 54633afdcc5SYongqiang Yang 54733afdcc5SYongqiang Yang /* This transaction may be extended/restarted along the way */ 5489924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 54933afdcc5SYongqiang Yang if (IS_ERR(handle)) 55033afdcc5SYongqiang Yang return PTR_ERR(handle); 55133afdcc5SYongqiang Yang 55233afdcc5SYongqiang Yang group = group_data[0].group; 55333afdcc5SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group++) { 55433afdcc5SYongqiang Yang unsigned long gdblocks; 55501f795f9SYongqiang Yang ext4_grpblk_t overhead; 55633afdcc5SYongqiang Yang 55733afdcc5SYongqiang Yang gdblocks = ext4_bg_num_gdb(sb, group); 55833afdcc5SYongqiang Yang start = ext4_group_first_block_no(sb, group); 55933afdcc5SYongqiang Yang 56001f795f9SYongqiang Yang if (meta_bg == 0 && !ext4_bg_has_super(sb, group)) 5616df935adSYongqiang Yang goto handle_itb; 5626df935adSYongqiang Yang 56301f795f9SYongqiang Yang if (meta_bg == 1) { 56401f795f9SYongqiang Yang ext4_group_t first_group; 56501f795f9SYongqiang Yang first_group = ext4_meta_bg_first_group(sb, group); 56601f795f9SYongqiang Yang if (first_group != group + 1 && 56701f795f9SYongqiang Yang first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1) 56801f795f9SYongqiang Yang goto handle_itb; 56901f795f9SYongqiang Yang } 57001f795f9SYongqiang Yang 57101f795f9SYongqiang Yang block = start + ext4_bg_has_super(sb, group); 57233afdcc5SYongqiang Yang /* Copy all of the GDT blocks into the backup in this group */ 57301f795f9SYongqiang Yang for (j = 0; j < gdblocks; j++, block++) { 57433afdcc5SYongqiang Yang struct buffer_head *gdb; 57533afdcc5SYongqiang Yang 57633afdcc5SYongqiang Yang ext4_debug("update backup group %#04llx\n", block); 57783448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 578a4130367SJan Kara if (err < 0) 57933afdcc5SYongqiang Yang goto out; 58033afdcc5SYongqiang Yang 58133afdcc5SYongqiang Yang gdb = sb_getblk(sb, block); 582aebf0243SWang Shilong if (unlikely(!gdb)) { 583860d21e2STheodore Ts'o err = -ENOMEM; 58433afdcc5SYongqiang Yang goto out; 58533afdcc5SYongqiang Yang } 58633afdcc5SYongqiang Yang 5875d601255Sliang xie BUFFER_TRACE(gdb, "get_write_access"); 588188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb, 589188c299eSJan Kara EXT4_JTR_NONE); 59033afdcc5SYongqiang Yang if (err) { 59133afdcc5SYongqiang Yang brelse(gdb); 59233afdcc5SYongqiang Yang goto out; 59333afdcc5SYongqiang Yang } 5941d0c3924STheodore Ts'o memcpy(gdb->b_data, sbi_array_rcu_deref(sbi, 5951d0c3924STheodore Ts'o s_group_desc, j)->b_data, gdb->b_size); 59633afdcc5SYongqiang Yang set_buffer_uptodate(gdb); 59733afdcc5SYongqiang Yang 59833afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb); 59933afdcc5SYongqiang Yang if (unlikely(err)) { 60033afdcc5SYongqiang Yang brelse(gdb); 60133afdcc5SYongqiang Yang goto out; 60233afdcc5SYongqiang Yang } 60333afdcc5SYongqiang Yang brelse(gdb); 60433afdcc5SYongqiang Yang } 60533afdcc5SYongqiang Yang 60633afdcc5SYongqiang Yang /* Zero out all of the reserved backup group descriptor 60733afdcc5SYongqiang Yang * table blocks 60833afdcc5SYongqiang Yang */ 60933afdcc5SYongqiang Yang if (ext4_bg_has_super(sb, group)) { 61033afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, gdblocks + start + 1, 61133afdcc5SYongqiang Yang reserved_gdb, GFP_NOFS); 61233afdcc5SYongqiang Yang if (err) 61333afdcc5SYongqiang Yang goto out; 61433afdcc5SYongqiang Yang } 61533afdcc5SYongqiang Yang 6166df935adSYongqiang Yang handle_itb: 61733afdcc5SYongqiang Yang /* Initialize group tables of the grop @group */ 61833afdcc5SYongqiang Yang if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED)) 61933afdcc5SYongqiang Yang goto handle_bb; 62033afdcc5SYongqiang Yang 62133afdcc5SYongqiang Yang /* Zero out all of the inode table blocks */ 62233afdcc5SYongqiang Yang block = group_data[i].inode_table; 62333afdcc5SYongqiang Yang ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 62433afdcc5SYongqiang Yang block, sbi->s_itb_per_group); 62533afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, 62633afdcc5SYongqiang Yang GFP_NOFS); 62733afdcc5SYongqiang Yang if (err) 62833afdcc5SYongqiang Yang goto out; 62933afdcc5SYongqiang Yang 63033afdcc5SYongqiang Yang handle_bb: 63133afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT) 63233afdcc5SYongqiang Yang goto handle_ib; 63333afdcc5SYongqiang Yang 63433afdcc5SYongqiang Yang /* Initialize block bitmap of the @group */ 63533afdcc5SYongqiang Yang block = group_data[i].block_bitmap; 63683448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 637a4130367SJan Kara if (err < 0) 63833afdcc5SYongqiang Yang goto out; 63933afdcc5SYongqiang Yang 64033afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 64133afdcc5SYongqiang Yang if (IS_ERR(bh)) { 64233afdcc5SYongqiang Yang err = PTR_ERR(bh); 64333afdcc5SYongqiang Yang goto out; 64433afdcc5SYongqiang Yang } 64501f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group); 64601f795f9SYongqiang Yang if (overhead != 0) { 64733afdcc5SYongqiang Yang ext4_debug("mark backup superblock %#04llx (+0)\n", 64833afdcc5SYongqiang Yang start); 649123e3016SRitesh Harjani mb_set_bits(bh->b_data, 0, 650d77147ffSharshads EXT4_NUM_B2C(sbi, overhead)); 65133afdcc5SYongqiang Yang } 652d77147ffSharshads ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count), 65333afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 65433afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6559e402893SVasily Averin brelse(bh); 65633afdcc5SYongqiang Yang if (err) 65733afdcc5SYongqiang Yang goto out; 65833afdcc5SYongqiang Yang 65933afdcc5SYongqiang Yang handle_ib: 66033afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_INODE_UNINIT) 66133afdcc5SYongqiang Yang continue; 66233afdcc5SYongqiang Yang 66333afdcc5SYongqiang Yang /* Initialize inode bitmap of the @group */ 66433afdcc5SYongqiang Yang block = group_data[i].inode_bitmap; 66583448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 666a4130367SJan Kara if (err < 0) 66733afdcc5SYongqiang Yang goto out; 66833afdcc5SYongqiang Yang /* Mark unused entries in inode bitmap used */ 66933afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 67033afdcc5SYongqiang Yang if (IS_ERR(bh)) { 67133afdcc5SYongqiang Yang err = PTR_ERR(bh); 67233afdcc5SYongqiang Yang goto out; 67333afdcc5SYongqiang Yang } 67433afdcc5SYongqiang Yang 67533afdcc5SYongqiang Yang ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 67633afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 67733afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6789e402893SVasily Averin brelse(bh); 67933afdcc5SYongqiang Yang if (err) 68033afdcc5SYongqiang Yang goto out; 68133afdcc5SYongqiang Yang } 68233afdcc5SYongqiang Yang 68333afdcc5SYongqiang Yang /* Mark group tables in block bitmap */ 68433afdcc5SYongqiang Yang for (j = 0; j < GROUP_TABLE_COUNT; j++) { 68533afdcc5SYongqiang Yang count = group_table_count[j]; 68633afdcc5SYongqiang Yang start = (&group_data[0].block_bitmap)[j]; 68733afdcc5SYongqiang Yang block = start; 68833afdcc5SYongqiang Yang for (i = 1; i < flex_gd->count; i++) { 68933afdcc5SYongqiang Yang block += group_table_count[j]; 69033afdcc5SYongqiang Yang if (block == (&group_data[i].block_bitmap)[j]) { 69133afdcc5SYongqiang Yang count += group_table_count[j]; 69233afdcc5SYongqiang Yang continue; 69333afdcc5SYongqiang Yang } 69433afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 695d77147ffSharshads flex_gd, 696d77147ffSharshads EXT4_B2C(sbi, start), 697d77147ffSharshads EXT4_B2C(sbi, 698d77147ffSharshads start + count 699d77147ffSharshads - 1)); 70033afdcc5SYongqiang Yang if (err) 70133afdcc5SYongqiang Yang goto out; 70233afdcc5SYongqiang Yang count = group_table_count[j]; 703b93c9535STheodore Ts'o start = (&group_data[i].block_bitmap)[j]; 70433afdcc5SYongqiang Yang block = start; 70533afdcc5SYongqiang Yang } 70633afdcc5SYongqiang Yang 70733afdcc5SYongqiang Yang if (count) { 70833afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 709d77147ffSharshads flex_gd, 710d77147ffSharshads EXT4_B2C(sbi, start), 711d77147ffSharshads EXT4_B2C(sbi, 712d77147ffSharshads start + count 713d77147ffSharshads - 1)); 71433afdcc5SYongqiang Yang if (err) 71533afdcc5SYongqiang Yang goto out; 71633afdcc5SYongqiang Yang } 71733afdcc5SYongqiang Yang } 71833afdcc5SYongqiang Yang 71933afdcc5SYongqiang Yang out: 72033afdcc5SYongqiang Yang err2 = ext4_journal_stop(handle); 72133afdcc5SYongqiang Yang if (err2 && !err) 72233afdcc5SYongqiang Yang err = err2; 72333afdcc5SYongqiang Yang 72433afdcc5SYongqiang Yang return err; 72533afdcc5SYongqiang Yang } 72633afdcc5SYongqiang Yang 72733afdcc5SYongqiang Yang /* 728ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 729617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 730ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 731ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 732ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 733ac27a0ecSDave Kleikamp */ 734bbc605cdSLukas Czerner unsigned int ext4_list_backups(struct super_block *sb, unsigned int *three, 735bbc605cdSLukas Czerner unsigned int *five, unsigned int *seven) 736ac27a0ecSDave Kleikamp { 737bbc605cdSLukas Czerner struct ext4_super_block *es = EXT4_SB(sb)->s_es; 738bbc605cdSLukas Czerner unsigned int *min = three; 739ac27a0ecSDave Kleikamp int mult = 3; 740bbc605cdSLukas Czerner unsigned int ret; 741bbc605cdSLukas Czerner 742bbc605cdSLukas Czerner if (ext4_has_feature_sparse_super2(sb)) { 743bbc605cdSLukas Czerner do { 744bbc605cdSLukas Czerner if (*min > 2) 745bbc605cdSLukas Czerner return UINT_MAX; 746bbc605cdSLukas Czerner ret = le32_to_cpu(es->s_backup_bgs[*min - 1]); 747bbc605cdSLukas Czerner *min += 1; 748bbc605cdSLukas Czerner } while (!ret); 749bbc605cdSLukas Czerner return ret; 750bbc605cdSLukas Czerner } 751ac27a0ecSDave Kleikamp 752e2b911c5SDarrick J. Wong if (!ext4_has_feature_sparse_super(sb)) { 753ac27a0ecSDave Kleikamp ret = *min; 754ac27a0ecSDave Kleikamp *min += 1; 755ac27a0ecSDave Kleikamp return ret; 756ac27a0ecSDave Kleikamp } 757ac27a0ecSDave Kleikamp 758ac27a0ecSDave Kleikamp if (*five < *min) { 759ac27a0ecSDave Kleikamp min = five; 760ac27a0ecSDave Kleikamp mult = 5; 761ac27a0ecSDave Kleikamp } 762ac27a0ecSDave Kleikamp if (*seven < *min) { 763ac27a0ecSDave Kleikamp min = seven; 764ac27a0ecSDave Kleikamp mult = 7; 765ac27a0ecSDave Kleikamp } 766ac27a0ecSDave Kleikamp 767ac27a0ecSDave Kleikamp ret = *min; 768ac27a0ecSDave Kleikamp *min *= mult; 769ac27a0ecSDave Kleikamp 770ac27a0ecSDave Kleikamp return ret; 771ac27a0ecSDave Kleikamp } 772ac27a0ecSDave Kleikamp 773ac27a0ecSDave Kleikamp /* 774ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 775ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 776ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 777ac27a0ecSDave Kleikamp */ 778ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 779c72df9f9SYongqiang Yang ext4_group_t end, 780ac27a0ecSDave Kleikamp struct buffer_head *primary) 781ac27a0ecSDave Kleikamp { 782617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 783ac27a0ecSDave Kleikamp unsigned three = 1; 784ac27a0ecSDave Kleikamp unsigned five = 5; 785ac27a0ecSDave Kleikamp unsigned seven = 7; 786ac27a0ecSDave Kleikamp unsigned grp; 787ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 788ac27a0ecSDave Kleikamp int gdbackups = 0; 789ac27a0ecSDave Kleikamp 790617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 791bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 792bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 79312062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 7942ae02107SMingming Cao " missing grp %d (%llu)", 795ac27a0ecSDave Kleikamp blk, grp, 796bd81d8eeSLaurent Vivier grp * 797bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 798bd81d8eeSLaurent Vivier blk); 799ac27a0ecSDave Kleikamp return -EINVAL; 800ac27a0ecSDave Kleikamp } 801617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 802ac27a0ecSDave Kleikamp return -EFBIG; 803ac27a0ecSDave Kleikamp } 804ac27a0ecSDave Kleikamp 805ac27a0ecSDave Kleikamp return gdbackups; 806ac27a0ecSDave Kleikamp } 807ac27a0ecSDave Kleikamp 808ac27a0ecSDave Kleikamp /* 809ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 810ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 811ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 812ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 813ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 814ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 815ac27a0ecSDave Kleikamp * 816ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 817ac27a0ecSDave Kleikamp * 818ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 819ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 820ac27a0ecSDave Kleikamp */ 821ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 8222f919710SYongqiang Yang ext4_group_t group) 823ac27a0ecSDave Kleikamp { 824ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 825617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 8262f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 827617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 828fb265c9cSTheodore Ts'o struct buffer_head **o_group_desc, **n_group_desc = NULL; 829fb265c9cSTheodore Ts'o struct buffer_head *dind = NULL; 830fb265c9cSTheodore Ts'o struct buffer_head *gdb_bh = NULL; 831ac27a0ecSDave Kleikamp int gdbackups; 832fb265c9cSTheodore Ts'o struct ext4_iloc iloc = { .bh = NULL }; 833ac27a0ecSDave Kleikamp __le32 *data; 834ac27a0ecSDave Kleikamp int err; 835ac27a0ecSDave Kleikamp 836ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 837ac27a0ecSDave Kleikamp printk(KERN_DEBUG 838617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 839ac27a0ecSDave Kleikamp gdb_num); 840ac27a0ecSDave Kleikamp 841fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 842fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 843fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 844ac27a0ecSDave Kleikamp 845c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, gdb_bh); 8462f919710SYongqiang Yang if (gdbackups < 0) { 847ac27a0ecSDave Kleikamp err = gdbackups; 848fb265c9cSTheodore Ts'o goto errout; 849ac27a0ecSDave Kleikamp } 850ac27a0ecSDave Kleikamp 851617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 852fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 853fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 854fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 855fb265c9cSTheodore Ts'o dind = NULL; 856fb265c9cSTheodore Ts'o goto errout; 857ac27a0ecSDave Kleikamp } 858ac27a0ecSDave Kleikamp 859ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 860617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 86112062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 8622f919710SYongqiang Yang group, gdblock); 863ac27a0ecSDave Kleikamp err = -EINVAL; 864fb265c9cSTheodore Ts'o goto errout; 865ac27a0ecSDave Kleikamp } 866ac27a0ecSDave Kleikamp 8675d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 868188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 869188c299eSJan Kara EXT4_JTR_NONE); 870b4097142STheodore Ts'o if (unlikely(err)) 871fb265c9cSTheodore Ts'o goto errout; 872ac27a0ecSDave Kleikamp 8735d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 874188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE); 875b4097142STheodore Ts'o if (unlikely(err)) 876fb265c9cSTheodore Ts'o goto errout; 877ac27a0ecSDave Kleikamp 8785d601255Sliang xie BUFFER_TRACE(dind, "get_write_access"); 879188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, dind, EXT4_JTR_NONE); 880c9e87161SDinghao Liu if (unlikely(err)) { 881b4097142STheodore Ts'o ext4_std_error(sb, err); 882c9e87161SDinghao Liu goto errout; 883c9e87161SDinghao Liu } 884ac27a0ecSDave Kleikamp 885617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 886b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 887b4097142STheodore Ts'o if (unlikely(err)) 888fb265c9cSTheodore Ts'o goto errout; 889ac27a0ecSDave Kleikamp 89071b565ceSTheodore Ts'o n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 89171b565ceSTheodore Ts'o GFP_KERNEL); 892ac27a0ecSDave Kleikamp if (!n_group_desc) { 893ac27a0ecSDave Kleikamp err = -ENOMEM; 894f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 895f18a5f21STheodore Ts'o gdb_num + 1); 896fb265c9cSTheodore Ts'o goto errout; 897ac27a0ecSDave Kleikamp } 898ac27a0ecSDave Kleikamp 899ac27a0ecSDave Kleikamp /* 900ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 901ac27a0ecSDave Kleikamp * 902ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 903ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 904ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 905ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 906ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 907ac27a0ecSDave Kleikamp */ 908617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 909b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 910b4097142STheodore Ts'o if (unlikely(err)) { 911b4097142STheodore Ts'o ext4_std_error(sb, err); 912fb265c9cSTheodore Ts'o goto errout; 913b4097142STheodore Ts'o } 914d77147ffSharshads inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 915d77147ffSharshads (9 - EXT4_SB(sb)->s_cluster_bits); 916617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 9172f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 9182f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 919b4097142STheodore Ts'o if (unlikely(err)) { 920b4097142STheodore Ts'o ext4_std_error(sb, err); 9218c380ab4SPan Bian iloc.bh = NULL; 922fb265c9cSTheodore Ts'o goto errout; 923b4097142STheodore Ts'o } 924b4097142STheodore Ts'o brelse(dind); 925ac27a0ecSDave Kleikamp 9261d0c3924STheodore Ts'o rcu_read_lock(); 9271d0c3924STheodore Ts'o o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc); 928ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 929617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 9301d0c3924STheodore Ts'o rcu_read_unlock(); 9312f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 9321d0c3924STheodore Ts'o rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc); 933617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 9341d0c3924STheodore Ts'o ext4_kvfree_array_rcu(o_group_desc); 935ac27a0ecSDave Kleikamp 93605c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 937e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 93805c2c00fSJan Kara ext4_superblock_csum_set(sb); 93905c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 940a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 941b4097142STheodore Ts'o if (err) 942b4097142STheodore Ts'o ext4_std_error(sb, err); 943b4097142STheodore Ts'o return err; 944fb265c9cSTheodore Ts'o errout: 945b93b41d4SAl Viro kvfree(n_group_desc); 946ac27a0ecSDave Kleikamp brelse(iloc.bh); 947ac27a0ecSDave Kleikamp brelse(dind); 9482f919710SYongqiang Yang brelse(gdb_bh); 949ac27a0ecSDave Kleikamp 950617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 951ac27a0ecSDave Kleikamp return err; 952ac27a0ecSDave Kleikamp } 953ac27a0ecSDave Kleikamp 954ac27a0ecSDave Kleikamp /* 95501f795f9SYongqiang Yang * add_new_gdb_meta_bg is the sister of add_new_gdb. 95601f795f9SYongqiang Yang */ 95701f795f9SYongqiang Yang static int add_new_gdb_meta_bg(struct super_block *sb, 95801f795f9SYongqiang Yang handle_t *handle, ext4_group_t group) { 95901f795f9SYongqiang Yang ext4_fsblk_t gdblock; 96001f795f9SYongqiang Yang struct buffer_head *gdb_bh; 96101f795f9SYongqiang Yang struct buffer_head **o_group_desc, **n_group_desc; 96201f795f9SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 96301f795f9SYongqiang Yang int err; 96401f795f9SYongqiang Yang 96501f795f9SYongqiang Yang gdblock = ext4_meta_bg_first_block_no(sb, group) + 96601f795f9SYongqiang Yang ext4_bg_has_super(sb, group); 967fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 968fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 969fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 97071b565ceSTheodore Ts'o n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 97171b565ceSTheodore Ts'o GFP_KERNEL); 97201f795f9SYongqiang Yang if (!n_group_desc) { 97361a9c11eSVasily Averin brelse(gdb_bh); 97401f795f9SYongqiang Yang err = -ENOMEM; 97501f795f9SYongqiang Yang ext4_warning(sb, "not enough memory for %lu groups", 97601f795f9SYongqiang Yang gdb_num + 1); 97701f795f9SYongqiang Yang return err; 97801f795f9SYongqiang Yang } 97901f795f9SYongqiang Yang 9801d0c3924STheodore Ts'o rcu_read_lock(); 9811d0c3924STheodore Ts'o o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc); 98201f795f9SYongqiang Yang memcpy(n_group_desc, o_group_desc, 98301f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 9841d0c3924STheodore Ts'o rcu_read_unlock(); 98501f795f9SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 986d64264d6SLukas Czerner 987d64264d6SLukas Czerner BUFFER_TRACE(gdb_bh, "get_write_access"); 988188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE); 989d64264d6SLukas Czerner if (err) { 990d64264d6SLukas Czerner kvfree(n_group_desc); 991d64264d6SLukas Czerner brelse(gdb_bh); 992d64264d6SLukas Czerner return err; 993d64264d6SLukas Czerner } 994d64264d6SLukas Czerner 9951d0c3924STheodore Ts'o rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc); 99601f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count++; 9971d0c3924STheodore Ts'o ext4_kvfree_array_rcu(o_group_desc); 99801f795f9SYongqiang Yang return err; 99901f795f9SYongqiang Yang } 100001f795f9SYongqiang Yang 100101f795f9SYongqiang Yang /* 1002ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 1003ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 1004ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 1005ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 1006ac27a0ecSDave Kleikamp * 1007ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 1008ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 1009ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 1010ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 1011ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 1012ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 1013ac27a0ecSDave Kleikamp */ 1014ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 1015668f4dc5SYongqiang Yang ext4_group_t group) 1016ac27a0ecSDave Kleikamp { 1017ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 1018617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 1019d77147ffSharshads int cluster_bits = EXT4_SB(sb)->s_cluster_bits; 1020ac27a0ecSDave Kleikamp struct buffer_head **primary; 1021ac27a0ecSDave Kleikamp struct buffer_head *dind; 1022617ba13bSMingming Cao struct ext4_iloc iloc; 1023617ba13bSMingming Cao ext4_fsblk_t blk; 1024ac27a0ecSDave Kleikamp __le32 *data, *end; 1025ac27a0ecSDave Kleikamp int gdbackups = 0; 1026ac27a0ecSDave Kleikamp int res, i; 1027ac27a0ecSDave Kleikamp int err; 1028ac27a0ecSDave Kleikamp 10296da2ec56SKees Cook primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS); 1030ac27a0ecSDave Kleikamp if (!primary) 1031ac27a0ecSDave Kleikamp return -ENOMEM; 1032ac27a0ecSDave Kleikamp 1033617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 1034fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 1035fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 1036fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 1037fb265c9cSTheodore Ts'o dind = NULL; 1038ac27a0ecSDave Kleikamp goto exit_free; 1039ac27a0ecSDave Kleikamp } 1040ac27a0ecSDave Kleikamp 1041617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 104294460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 104394460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 1044617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 1045ac27a0ecSDave Kleikamp 1046ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 1047ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 1048ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 104912062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 1050ac27a0ecSDave Kleikamp " not at offset %ld", 1051ac27a0ecSDave Kleikamp blk, 1052ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 1053ac27a0ecSDave Kleikamp err = -EINVAL; 1054ac27a0ecSDave Kleikamp goto exit_bh; 1055ac27a0ecSDave Kleikamp } 1056fb265c9cSTheodore Ts'o primary[res] = ext4_sb_bread(sb, blk, 0); 1057fb265c9cSTheodore Ts'o if (IS_ERR(primary[res])) { 1058fb265c9cSTheodore Ts'o err = PTR_ERR(primary[res]); 1059fb265c9cSTheodore Ts'o primary[res] = NULL; 1060ac27a0ecSDave Kleikamp goto exit_bh; 1061ac27a0ecSDave Kleikamp } 1062c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, primary[res]); 1063c72df9f9SYongqiang Yang if (gdbackups < 0) { 1064ac27a0ecSDave Kleikamp brelse(primary[res]); 1065ac27a0ecSDave Kleikamp err = gdbackups; 1066ac27a0ecSDave Kleikamp goto exit_bh; 1067ac27a0ecSDave Kleikamp } 1068ac27a0ecSDave Kleikamp if (++data >= end) 1069ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 1070ac27a0ecSDave Kleikamp } 1071ac27a0ecSDave Kleikamp 1072ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 10735d601255Sliang xie BUFFER_TRACE(primary[i], "get_write_access"); 1074188c299eSJan Kara if ((err = ext4_journal_get_write_access(handle, sb, primary[i], 1075188c299eSJan Kara EXT4_JTR_NONE))) 1076ac27a0ecSDave Kleikamp goto exit_bh; 1077ac27a0ecSDave Kleikamp } 1078ac27a0ecSDave Kleikamp 1079617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 1080ac27a0ecSDave Kleikamp goto exit_bh; 1081ac27a0ecSDave Kleikamp 1082ac27a0ecSDave Kleikamp /* 1083ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 1084ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 1085ac27a0ecSDave Kleikamp */ 1086668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 1087ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 1088ac27a0ecSDave Kleikamp int err2; 1089ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 1090ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 1091ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 1092ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 1093ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 10940390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 1095ac27a0ecSDave Kleikamp if (!err) 1096ac27a0ecSDave Kleikamp err = err2; 1097ac27a0ecSDave Kleikamp } 1098d77147ffSharshads 1099d77147ffSharshads inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits); 1100617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 1101ac27a0ecSDave Kleikamp 1102ac27a0ecSDave Kleikamp exit_bh: 1103ac27a0ecSDave Kleikamp while (--res >= 0) 1104ac27a0ecSDave Kleikamp brelse(primary[res]); 1105ac27a0ecSDave Kleikamp brelse(dind); 1106ac27a0ecSDave Kleikamp 1107ac27a0ecSDave Kleikamp exit_free: 1108ac27a0ecSDave Kleikamp kfree(primary); 1109ac27a0ecSDave Kleikamp 1110ac27a0ecSDave Kleikamp return err; 1111ac27a0ecSDave Kleikamp } 1112ac27a0ecSDave Kleikamp 1113ac27a0ecSDave Kleikamp /* 1114617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 1115ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 1116ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 1117ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 1118ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 1119ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 1120ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 1121ac27a0ecSDave Kleikamp * 112232ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 112332ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 112432ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 112532ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 112632ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 112732ed5058STheodore Ts'o * which changed s_groups_count will backup again. 1128ac27a0ecSDave Kleikamp */ 1129904dad47SAndy Leiserson static void update_backups(struct super_block *sb, sector_t blk_off, char *data, 113001f795f9SYongqiang Yang int size, int meta_bg) 1131ac27a0ecSDave Kleikamp { 1132617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 113301f795f9SYongqiang Yang ext4_group_t last; 1134617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 1135ac27a0ecSDave Kleikamp unsigned three = 1; 1136ac27a0ecSDave Kleikamp unsigned five = 5; 1137ac27a0ecSDave Kleikamp unsigned seven = 7; 113801f795f9SYongqiang Yang ext4_group_t group = 0; 1139ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 1140ac27a0ecSDave Kleikamp handle_t *handle; 1141ac27a0ecSDave Kleikamp int err = 0, err2; 1142ac27a0ecSDave Kleikamp 11439924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 1144ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1145ac27a0ecSDave Kleikamp group = 1; 1146ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 1147ac27a0ecSDave Kleikamp goto exit_err; 1148ac27a0ecSDave Kleikamp } 1149ac27a0ecSDave Kleikamp 115001f795f9SYongqiang Yang if (meta_bg == 0) { 115101f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 115201f795f9SYongqiang Yang last = sbi->s_groups_count; 115301f795f9SYongqiang Yang } else { 1154904dad47SAndy Leiserson group = ext4_get_group_number(sb, blk_off) + 1; 115501f795f9SYongqiang Yang last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2); 115601f795f9SYongqiang Yang } 115701f795f9SYongqiang Yang 115801f795f9SYongqiang Yang while (group < sbi->s_groups_count) { 1159ac27a0ecSDave Kleikamp struct buffer_head *bh; 116001f795f9SYongqiang Yang ext4_fsblk_t backup_block; 1161ac27a0ecSDave Kleikamp 1162ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 116383448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 1164a4130367SJan Kara if (err < 0) 1165ac27a0ecSDave Kleikamp break; 1166ac27a0ecSDave Kleikamp 116701f795f9SYongqiang Yang if (meta_bg == 0) 11689378c676SJan Kara backup_block = ((ext4_fsblk_t)group) * bpg + blk_off; 116901f795f9SYongqiang Yang else 117001f795f9SYongqiang Yang backup_block = (ext4_group_first_block_no(sb, group) + 117101f795f9SYongqiang Yang ext4_bg_has_super(sb, group)); 117201f795f9SYongqiang Yang 117301f795f9SYongqiang Yang bh = sb_getblk(sb, backup_block); 1174aebf0243SWang Shilong if (unlikely(!bh)) { 1175860d21e2STheodore Ts'o err = -ENOMEM; 1176ac27a0ecSDave Kleikamp break; 1177ac27a0ecSDave Kleikamp } 117801f795f9SYongqiang Yang ext4_debug("update metadata backup %llu(+%llu)\n", 117901f795f9SYongqiang Yang backup_block, backup_block - 118001f795f9SYongqiang Yang ext4_group_first_block_no(sb, group)); 11815d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 1182188c299eSJan Kara if ((err = ext4_journal_get_write_access(handle, sb, bh, 1183188c299eSJan Kara EXT4_JTR_NONE))) 1184ac27a0ecSDave Kleikamp break; 1185ac27a0ecSDave Kleikamp lock_buffer(bh); 1186ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 1187ac27a0ecSDave Kleikamp if (rest) 1188ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 1189ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1190ac27a0ecSDave Kleikamp unlock_buffer(bh); 1191b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 1192b4097142STheodore Ts'o if (unlikely(err)) 1193b4097142STheodore Ts'o ext4_std_error(sb, err); 1194ac27a0ecSDave Kleikamp brelse(bh); 119501f795f9SYongqiang Yang 119601f795f9SYongqiang Yang if (meta_bg == 0) 119701f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 119801f795f9SYongqiang Yang else if (group == last) 119901f795f9SYongqiang Yang break; 120001f795f9SYongqiang Yang else 120101f795f9SYongqiang Yang group = last; 1202ac27a0ecSDave Kleikamp } 1203617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 1204ac27a0ecSDave Kleikamp err = err2; 1205ac27a0ecSDave Kleikamp 1206ac27a0ecSDave Kleikamp /* 1207ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 1208ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 1209ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 1210ac27a0ecSDave Kleikamp * 1211ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 1212ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 1213ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 1214ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 1215ac27a0ecSDave Kleikamp */ 1216ac27a0ecSDave Kleikamp exit_err: 1217ac27a0ecSDave Kleikamp if (err) { 121812062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 1219ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 1220617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 1221617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 1222ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 1223ac27a0ecSDave Kleikamp } 1224ac27a0ecSDave Kleikamp } 1225ac27a0ecSDave Kleikamp 1226bb08c1e7SYongqiang Yang /* 1227bb08c1e7SYongqiang Yang * ext4_add_new_descs() adds @count group descriptor of groups 1228bb08c1e7SYongqiang Yang * starting at @group 1229bb08c1e7SYongqiang Yang * 1230bb08c1e7SYongqiang Yang * @handle: journal handle 1231bb08c1e7SYongqiang Yang * @sb: super block 1232bb08c1e7SYongqiang Yang * @group: the group no. of the first group desc to be added 1233bb08c1e7SYongqiang Yang * @resize_inode: the resize inode 1234bb08c1e7SYongqiang Yang * @count: number of group descriptors to be added 1235bb08c1e7SYongqiang Yang */ 1236bb08c1e7SYongqiang Yang static int ext4_add_new_descs(handle_t *handle, struct super_block *sb, 1237bb08c1e7SYongqiang Yang ext4_group_t group, struct inode *resize_inode, 1238bb08c1e7SYongqiang Yang ext4_group_t count) 1239bb08c1e7SYongqiang Yang { 1240bb08c1e7SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1241bb08c1e7SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 1242bb08c1e7SYongqiang Yang struct buffer_head *gdb_bh; 1243bb08c1e7SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 124401f795f9SYongqiang Yang int meta_bg; 1245bb08c1e7SYongqiang Yang 1246e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 1247bb08c1e7SYongqiang Yang for (i = 0; i < count; i++, group++) { 1248bb08c1e7SYongqiang Yang int reserved_gdb = ext4_bg_has_super(sb, group) ? 1249bb08c1e7SYongqiang Yang le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1250bb08c1e7SYongqiang Yang 1251bb08c1e7SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1252bb08c1e7SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1253bb08c1e7SYongqiang Yang 1254bb08c1e7SYongqiang Yang /* 1255bb08c1e7SYongqiang Yang * We will only either add reserved group blocks to a backup group 1256bb08c1e7SYongqiang Yang * or remove reserved blocks for the first group in a new group block. 1257bb08c1e7SYongqiang Yang * Doing both would be mean more complex code, and sane people don't 1258bb08c1e7SYongqiang Yang * use non-sparse filesystems anymore. This is already checked above. 1259bb08c1e7SYongqiang Yang */ 1260bb08c1e7SYongqiang Yang if (gdb_off) { 12611d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 12621d0c3924STheodore Ts'o gdb_num); 12635d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 1264188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb_bh, 1265188c299eSJan Kara EXT4_JTR_NONE); 1266bb08c1e7SYongqiang Yang 1267bb08c1e7SYongqiang Yang if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 1268bb08c1e7SYongqiang Yang err = reserve_backup_gdb(handle, resize_inode, group); 126901f795f9SYongqiang Yang } else if (meta_bg != 0) { 127001f795f9SYongqiang Yang err = add_new_gdb_meta_bg(sb, handle, group); 127101f795f9SYongqiang Yang } else { 1272bb08c1e7SYongqiang Yang err = add_new_gdb(handle, resize_inode, group); 127301f795f9SYongqiang Yang } 1274bb08c1e7SYongqiang Yang if (err) 1275bb08c1e7SYongqiang Yang break; 1276bb08c1e7SYongqiang Yang } 1277bb08c1e7SYongqiang Yang return err; 1278bb08c1e7SYongqiang Yang } 1279bb08c1e7SYongqiang Yang 128041a246d1SDarrick J. Wong static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block) 128141a246d1SDarrick J. Wong { 128241a246d1SDarrick J. Wong struct buffer_head *bh = sb_getblk(sb, block); 1283aebf0243SWang Shilong if (unlikely(!bh)) 128441a246d1SDarrick J. Wong return NULL; 12857f1468d1SDmitry Monakhov if (!bh_uptodate_or_lock(bh)) { 12862d069c08Szhangyi (F) if (ext4_read_bh(bh, 0, NULL) < 0) { 128741a246d1SDarrick J. Wong brelse(bh); 128841a246d1SDarrick J. Wong return NULL; 128941a246d1SDarrick J. Wong } 12907f1468d1SDmitry Monakhov } 129141a246d1SDarrick J. Wong 129241a246d1SDarrick J. Wong return bh; 129341a246d1SDarrick J. Wong } 129441a246d1SDarrick J. Wong 129541a246d1SDarrick J. Wong static int ext4_set_bitmap_checksums(struct super_block *sb, 129641a246d1SDarrick J. Wong ext4_group_t group, 129741a246d1SDarrick J. Wong struct ext4_group_desc *gdp, 129841a246d1SDarrick J. Wong struct ext4_new_group_data *group_data) 129941a246d1SDarrick J. Wong { 130041a246d1SDarrick J. Wong struct buffer_head *bh; 130141a246d1SDarrick J. Wong 13029aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 130341a246d1SDarrick J. Wong return 0; 130441a246d1SDarrick J. Wong 130541a246d1SDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->inode_bitmap); 130641a246d1SDarrick J. Wong if (!bh) 130741a246d1SDarrick J. Wong return -EIO; 130841a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, bh, 130941a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 131041a246d1SDarrick J. Wong brelse(bh); 131141a246d1SDarrick J. Wong 1312fa77dcfaSDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->block_bitmap); 1313fa77dcfaSDarrick J. Wong if (!bh) 1314fa77dcfaSDarrick J. Wong return -EIO; 131579f1ba49STao Ma ext4_block_bitmap_csum_set(sb, group, gdp, bh); 1316fa77dcfaSDarrick J. Wong brelse(bh); 1317fa77dcfaSDarrick J. Wong 131841a246d1SDarrick J. Wong return 0; 131941a246d1SDarrick J. Wong } 132041a246d1SDarrick J. Wong 1321083f5b24SYongqiang Yang /* 1322083f5b24SYongqiang Yang * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg 1323083f5b24SYongqiang Yang */ 1324083f5b24SYongqiang Yang static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb, 1325083f5b24SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 1326083f5b24SYongqiang Yang { 1327083f5b24SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 1328083f5b24SYongqiang Yang struct ext4_group_desc *gdp; 1329083f5b24SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1330083f5b24SYongqiang Yang struct buffer_head *gdb_bh; 1331083f5b24SYongqiang Yang ext4_group_t group; 1332083f5b24SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 1333083f5b24SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 1334083f5b24SYongqiang Yang 1335083f5b24SYongqiang Yang 1336083f5b24SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) { 1337083f5b24SYongqiang Yang group = group_data->group; 1338083f5b24SYongqiang Yang 1339083f5b24SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1340083f5b24SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1341083f5b24SYongqiang Yang 1342083f5b24SYongqiang Yang /* 1343083f5b24SYongqiang Yang * get_write_access() has been called on gdb_bh by ext4_add_new_desc(). 1344083f5b24SYongqiang Yang */ 13451d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num); 1346083f5b24SYongqiang Yang /* Update group descriptor block for new group */ 13472716b802STheodore Ts'o gdp = (struct ext4_group_desc *)(gdb_bh->b_data + 1348083f5b24SYongqiang Yang gdb_off * EXT4_DESC_SIZE(sb)); 1349083f5b24SYongqiang Yang 1350083f5b24SYongqiang Yang memset(gdp, 0, EXT4_DESC_SIZE(sb)); 1351083f5b24SYongqiang Yang ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap); 1352083f5b24SYongqiang Yang ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap); 135341a246d1SDarrick J. Wong err = ext4_set_bitmap_checksums(sb, group, gdp, group_data); 135441a246d1SDarrick J. Wong if (err) { 135541a246d1SDarrick J. Wong ext4_std_error(sb, err); 135641a246d1SDarrick J. Wong break; 135741a246d1SDarrick J. Wong } 135841a246d1SDarrick J. Wong 1359083f5b24SYongqiang Yang ext4_inode_table_set(sb, gdp, group_data->inode_table); 1360083f5b24SYongqiang Yang ext4_free_group_clusters_set(sb, gdp, 1361d77147ffSharshads group_data->free_clusters_count); 1362083f5b24SYongqiang Yang ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 136393f90526STheodore Ts'o if (ext4_has_group_desc_csum(sb)) 136493f90526STheodore Ts'o ext4_itable_unused_set(sb, gdp, 136593f90526STheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 1366083f5b24SYongqiang Yang gdp->bg_flags = cpu_to_le16(*bg_flags); 1367feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1368083f5b24SYongqiang Yang 1369083f5b24SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 1370083f5b24SYongqiang Yang if (unlikely(err)) { 1371083f5b24SYongqiang Yang ext4_std_error(sb, err); 1372083f5b24SYongqiang Yang break; 1373083f5b24SYongqiang Yang } 1374083f5b24SYongqiang Yang 1375083f5b24SYongqiang Yang /* 1376083f5b24SYongqiang Yang * We can allocate memory for mb_alloc based on the new group 1377083f5b24SYongqiang Yang * descriptor 1378083f5b24SYongqiang Yang */ 1379083f5b24SYongqiang Yang err = ext4_mb_add_groupinfo(sb, group, gdp); 1380083f5b24SYongqiang Yang if (err) 1381083f5b24SYongqiang Yang break; 1382083f5b24SYongqiang Yang } 1383083f5b24SYongqiang Yang return err; 1384083f5b24SYongqiang Yang } 1385083f5b24SYongqiang Yang 1386026d0d27SKiselev, Oleg static void ext4_add_overhead(struct super_block *sb, 1387026d0d27SKiselev, Oleg const ext4_fsblk_t overhead) 1388026d0d27SKiselev, Oleg { 1389026d0d27SKiselev, Oleg struct ext4_sb_info *sbi = EXT4_SB(sb); 1390026d0d27SKiselev, Oleg struct ext4_super_block *es = sbi->s_es; 1391026d0d27SKiselev, Oleg 1392026d0d27SKiselev, Oleg sbi->s_overhead += overhead; 1393026d0d27SKiselev, Oleg es->s_overhead_clusters = cpu_to_le32(sbi->s_overhead); 1394026d0d27SKiselev, Oleg smp_wmb(); 1395026d0d27SKiselev, Oleg } 1396026d0d27SKiselev, Oleg 13972e10e2f2SYongqiang Yang /* 13982e10e2f2SYongqiang Yang * ext4_update_super() updates the super block so that the newly added 13992e10e2f2SYongqiang Yang * groups can be seen by the filesystem. 14002e10e2f2SYongqiang Yang * 14012e10e2f2SYongqiang Yang * @sb: super block 14022e10e2f2SYongqiang Yang * @flex_gd: new added groups 14032e10e2f2SYongqiang Yang */ 14042e10e2f2SYongqiang Yang static void ext4_update_super(struct super_block *sb, 14052e10e2f2SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 14062e10e2f2SYongqiang Yang { 14072e10e2f2SYongqiang Yang ext4_fsblk_t blocks_count = 0; 14082e10e2f2SYongqiang Yang ext4_fsblk_t free_blocks = 0; 14092e10e2f2SYongqiang Yang ext4_fsblk_t reserved_blocks = 0; 14102e10e2f2SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 14112e10e2f2SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 14122e10e2f2SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 14138a991849STheodore Ts'o int i; 14142e10e2f2SYongqiang Yang 14152e10e2f2SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 14162e10e2f2SYongqiang Yang /* 14172e10e2f2SYongqiang Yang * Make the new blocks and inodes valid next. We do this before 14182e10e2f2SYongqiang Yang * increasing the group count so that once the group is enabled, 14192e10e2f2SYongqiang Yang * all of its blocks and inodes are already valid. 14202e10e2f2SYongqiang Yang * 14212e10e2f2SYongqiang Yang * We always allocate group-by-group, then block-by-block or 14222e10e2f2SYongqiang Yang * inode-by-inode within a group, so enabling these 14232e10e2f2SYongqiang Yang * blocks/inodes before the group is live won't actually let us 14242e10e2f2SYongqiang Yang * allocate the new space yet. 14252e10e2f2SYongqiang Yang */ 14262e10e2f2SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 14272e10e2f2SYongqiang Yang blocks_count += group_data[i].blocks_count; 1428d77147ffSharshads free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count); 14292e10e2f2SYongqiang Yang } 14302e10e2f2SYongqiang Yang 14312e10e2f2SYongqiang Yang reserved_blocks = ext4_r_blocks_count(es) * 100; 143201f795f9SYongqiang Yang reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es)); 14332e10e2f2SYongqiang Yang reserved_blocks *= blocks_count; 14342e10e2f2SYongqiang Yang do_div(reserved_blocks, 100); 14352e10e2f2SYongqiang Yang 143605c2c00fSJan Kara lock_buffer(sbi->s_sbh); 14372e10e2f2SYongqiang Yang ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count); 1438636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks); 14392e10e2f2SYongqiang Yang le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) * 14402e10e2f2SYongqiang Yang flex_gd->count); 1441636d7e2eSDarrick J. Wong le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) * 1442636d7e2eSDarrick J. Wong flex_gd->count); 14432e10e2f2SYongqiang Yang 144401f795f9SYongqiang Yang ext4_debug("free blocks count %llu", ext4_free_blocks_count(es)); 14452e10e2f2SYongqiang Yang /* 14462e10e2f2SYongqiang Yang * We need to protect s_groups_count against other CPUs seeing 14472e10e2f2SYongqiang Yang * inconsistent state in the superblock. 14482e10e2f2SYongqiang Yang * 14492e10e2f2SYongqiang Yang * The precise rules we use are: 14502e10e2f2SYongqiang Yang * 14512e10e2f2SYongqiang Yang * * Writers must perform a smp_wmb() after updating all 14522e10e2f2SYongqiang Yang * dependent data and before modifying the groups count 14532e10e2f2SYongqiang Yang * 14542e10e2f2SYongqiang Yang * * Readers must perform an smp_rmb() after reading the groups 14552e10e2f2SYongqiang Yang * count and before reading any dependent data. 14562e10e2f2SYongqiang Yang * 14572e10e2f2SYongqiang Yang * NB. These rules can be relaxed when checking the group count 14582e10e2f2SYongqiang Yang * while freeing data, as we can only allocate from a block 14592e10e2f2SYongqiang Yang * group after serialising against the group count, and we can 14602e10e2f2SYongqiang Yang * only then free after serialising in turn against that 14612e10e2f2SYongqiang Yang * allocation. 14622e10e2f2SYongqiang Yang */ 14632e10e2f2SYongqiang Yang smp_wmb(); 14642e10e2f2SYongqiang Yang 14652e10e2f2SYongqiang Yang /* Update the global fs size fields */ 14662e10e2f2SYongqiang Yang sbi->s_groups_count += flex_gd->count; 1467c5c72d81STheodore Ts'o sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 1468c5c72d81STheodore Ts'o (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 14692e10e2f2SYongqiang Yang 14702e10e2f2SYongqiang Yang /* Update the reserved block counts only once the new group is 14712e10e2f2SYongqiang Yang * active. */ 14722e10e2f2SYongqiang Yang ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 14732e10e2f2SYongqiang Yang reserved_blocks); 147405c2c00fSJan Kara ext4_superblock_csum_set(sb); 147505c2c00fSJan Kara unlock_buffer(sbi->s_sbh); 14762e10e2f2SYongqiang Yang 14772e10e2f2SYongqiang Yang /* Update the free space counts */ 14782e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeclusters_counter, 1479810da240SLukas Czerner EXT4_NUM_B2C(sbi, free_blocks)); 14802e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeinodes_counter, 14812e10e2f2SYongqiang Yang EXT4_INODES_PER_GROUP(sb) * flex_gd->count); 14822e10e2f2SYongqiang Yang 148301f795f9SYongqiang Yang ext4_debug("free blocks count %llu", 148401f795f9SYongqiang Yang percpu_counter_read(&sbi->s_freeclusters_counter)); 1485e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) { 14862e10e2f2SYongqiang Yang ext4_group_t flex_group; 14877c990728SSuraj Jitindar Singh struct flex_groups *fg; 14887c990728SSuraj Jitindar Singh 14892e10e2f2SYongqiang Yang flex_group = ext4_flex_group(sbi, group_data[0].group); 14907c990728SSuraj Jitindar Singh fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group); 149190ba983fSTheodore Ts'o atomic64_add(EXT4_NUM_B2C(sbi, free_blocks), 14927c990728SSuraj Jitindar Singh &fg->free_clusters); 14932e10e2f2SYongqiang Yang atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count, 14947c990728SSuraj Jitindar Singh &fg->free_inodes); 14952e10e2f2SYongqiang Yang } 14962e10e2f2SYongqiang Yang 1497952fc18eSTheodore Ts'o /* 1498026d0d27SKiselev, Oleg * Update the fs overhead information. 1499026d0d27SKiselev, Oleg * 1500026d0d27SKiselev, Oleg * For bigalloc, if the superblock already has a properly calculated 1501026d0d27SKiselev, Oleg * overhead, update it with a value based on numbers already computed 1502026d0d27SKiselev, Oleg * above for the newly allocated capacity. 1503952fc18eSTheodore Ts'o */ 1504026d0d27SKiselev, Oleg if (ext4_has_feature_bigalloc(sb) && (sbi->s_overhead != 0)) 1505026d0d27SKiselev, Oleg ext4_add_overhead(sb, 1506026d0d27SKiselev, Oleg EXT4_NUM_B2C(sbi, blocks_count - free_blocks)); 1507026d0d27SKiselev, Oleg else 1508952fc18eSTheodore Ts'o ext4_calculate_overhead(sb); 1509de394a86STheodore Ts'o es->s_overhead_clusters = cpu_to_le32(sbi->s_overhead); 1510952fc18eSTheodore Ts'o 15112e10e2f2SYongqiang Yang if (test_opt(sb, DEBUG)) 15122e10e2f2SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: added group %u:" 15132e10e2f2SYongqiang Yang "%llu blocks(%llu free %llu reserved)\n", flex_gd->count, 15142e10e2f2SYongqiang Yang blocks_count, free_blocks, reserved_blocks); 15152e10e2f2SYongqiang Yang } 15162e10e2f2SYongqiang Yang 15174bac1f8cSYongqiang Yang /* Add a flex group to an fs. Ensure we handle all possible error conditions 15184bac1f8cSYongqiang Yang * _before_ we start modifying the filesystem, because we cannot abort the 15194bac1f8cSYongqiang Yang * transaction and not have it write the data to disk. 15204bac1f8cSYongqiang Yang */ 15214bac1f8cSYongqiang Yang static int ext4_flex_group_add(struct super_block *sb, 15224bac1f8cSYongqiang Yang struct inode *resize_inode, 15234bac1f8cSYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 15244bac1f8cSYongqiang Yang { 15254bac1f8cSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 15264bac1f8cSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 15274bac1f8cSYongqiang Yang ext4_fsblk_t o_blocks_count; 15284bac1f8cSYongqiang Yang ext4_grpblk_t last; 15294bac1f8cSYongqiang Yang ext4_group_t group; 15304bac1f8cSYongqiang Yang handle_t *handle; 15314bac1f8cSYongqiang Yang unsigned reserved_gdb; 15324bac1f8cSYongqiang Yang int err = 0, err2 = 0, credit; 15334bac1f8cSYongqiang Yang 15344bac1f8cSYongqiang Yang BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags); 15354bac1f8cSYongqiang Yang 15364bac1f8cSYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 15374bac1f8cSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 15384bac1f8cSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 15394bac1f8cSYongqiang Yang BUG_ON(last); 15404bac1f8cSYongqiang Yang 15414bac1f8cSYongqiang Yang err = setup_new_flex_group_blocks(sb, flex_gd); 15424bac1f8cSYongqiang Yang if (err) 15434bac1f8cSYongqiang Yang goto exit; 15444bac1f8cSYongqiang Yang /* 15454bac1f8cSYongqiang Yang * We will always be modifying at least the superblock and GDT 15462c869b26SJan Kara * blocks. If we are adding a group past the last current GDT block, 15474bac1f8cSYongqiang Yang * we will also modify the inode and the dindirect block. If we 15484bac1f8cSYongqiang Yang * are adding a group with superblock/GDT backups we will also 15494bac1f8cSYongqiang Yang * modify each of the reserved GDT dindirect blocks. 15504bac1f8cSYongqiang Yang */ 15512c869b26SJan Kara credit = 3; /* sb, resize inode, resize inode dindirect */ 15522c869b26SJan Kara /* GDT blocks */ 15532c869b26SJan Kara credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb)); 15542c869b26SJan Kara credit += reserved_gdb; /* Reserved GDT dindirect blocks */ 15559924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit); 15564bac1f8cSYongqiang Yang if (IS_ERR(handle)) { 15574bac1f8cSYongqiang Yang err = PTR_ERR(handle); 15584bac1f8cSYongqiang Yang goto exit; 15594bac1f8cSYongqiang Yang } 15604bac1f8cSYongqiang Yang 15615d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 1562188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 1563188c299eSJan Kara EXT4_JTR_NONE); 15644bac1f8cSYongqiang Yang if (err) 15654bac1f8cSYongqiang Yang goto exit_journal; 15664bac1f8cSYongqiang Yang 15674bac1f8cSYongqiang Yang group = flex_gd->groups[0].group; 156849598e04SJun Piao BUG_ON(group != sbi->s_groups_count); 15694bac1f8cSYongqiang Yang err = ext4_add_new_descs(handle, sb, group, 15704bac1f8cSYongqiang Yang resize_inode, flex_gd->count); 15714bac1f8cSYongqiang Yang if (err) 15724bac1f8cSYongqiang Yang goto exit_journal; 15734bac1f8cSYongqiang Yang 15744bac1f8cSYongqiang Yang err = ext4_setup_new_descs(handle, sb, flex_gd); 15754bac1f8cSYongqiang Yang if (err) 15764bac1f8cSYongqiang Yang goto exit_journal; 15774bac1f8cSYongqiang Yang 15784bac1f8cSYongqiang Yang ext4_update_super(sb, flex_gd); 15794bac1f8cSYongqiang Yang 1580a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 15814bac1f8cSYongqiang Yang 15824bac1f8cSYongqiang Yang exit_journal: 15834bac1f8cSYongqiang Yang err2 = ext4_journal_stop(handle); 15844bac1f8cSYongqiang Yang if (!err) 15854bac1f8cSYongqiang Yang err = err2; 15864bac1f8cSYongqiang Yang 15874bac1f8cSYongqiang Yang if (!err) { 15882ebd1704SYongqiang Yang int gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 15892ebd1704SYongqiang Yang int gdb_num_end = ((group + flex_gd->count - 1) / 15902ebd1704SYongqiang Yang EXT4_DESC_PER_BLOCK(sb)); 1591e2b911c5SDarrick J. Wong int meta_bg = ext4_has_feature_meta_bg(sb); 15920acdb887STao Ma sector_t old_gdb = 0; 15932ebd1704SYongqiang Yang 15944bac1f8cSYongqiang Yang update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 159501f795f9SYongqiang Yang sizeof(struct ext4_super_block), 0); 15962ebd1704SYongqiang Yang for (; gdb_num <= gdb_num_end; gdb_num++) { 15974bac1f8cSYongqiang Yang struct buffer_head *gdb_bh; 15982ebd1704SYongqiang Yang 15991d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 16001d0c3924STheodore Ts'o gdb_num); 16010acdb887STao Ma if (old_gdb == gdb_bh->b_blocknr) 16020acdb887STao Ma continue; 16034bac1f8cSYongqiang Yang update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data, 160401f795f9SYongqiang Yang gdb_bh->b_size, meta_bg); 16050acdb887STao Ma old_gdb = gdb_bh->b_blocknr; 16064bac1f8cSYongqiang Yang } 16074bac1f8cSYongqiang Yang } 16084bac1f8cSYongqiang Yang exit: 16094bac1f8cSYongqiang Yang return err; 16104bac1f8cSYongqiang Yang } 16114bac1f8cSYongqiang Yang 161219c5246dSYongqiang Yang static int ext4_setup_next_flex_gd(struct super_block *sb, 161319c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 161419c5246dSYongqiang Yang ext4_fsblk_t n_blocks_count, 161519c5246dSYongqiang Yang unsigned long flexbg_size) 161619c5246dSYongqiang Yang { 1617d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 1618d77147ffSharshads struct ext4_super_block *es = sbi->s_es; 161919c5246dSYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 162019c5246dSYongqiang Yang ext4_fsblk_t o_blocks_count; 162119c5246dSYongqiang Yang ext4_group_t n_group; 162219c5246dSYongqiang Yang ext4_group_t group; 162319c5246dSYongqiang Yang ext4_group_t last_group; 162419c5246dSYongqiang Yang ext4_grpblk_t last; 1625d77147ffSharshads ext4_grpblk_t clusters_per_group; 162619c5246dSYongqiang Yang unsigned long i; 162719c5246dSYongqiang Yang 1628d77147ffSharshads clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb); 162919c5246dSYongqiang Yang 163019c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 163119c5246dSYongqiang Yang 163219c5246dSYongqiang Yang if (o_blocks_count == n_blocks_count) 163319c5246dSYongqiang Yang return 0; 163419c5246dSYongqiang Yang 163519c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 163619c5246dSYongqiang Yang BUG_ON(last); 163719c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last); 163819c5246dSYongqiang Yang 163919c5246dSYongqiang Yang last_group = group | (flexbg_size - 1); 164019c5246dSYongqiang Yang if (last_group > n_group) 164119c5246dSYongqiang Yang last_group = n_group; 164219c5246dSYongqiang Yang 164319c5246dSYongqiang Yang flex_gd->count = last_group - group + 1; 164419c5246dSYongqiang Yang 164519c5246dSYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 164619c5246dSYongqiang Yang int overhead; 164719c5246dSYongqiang Yang 164819c5246dSYongqiang Yang group_data[i].group = group + i; 1649d77147ffSharshads group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb); 165001f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group + i); 1651d77147ffSharshads group_data[i].mdata_blocks = overhead; 1652d77147ffSharshads group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb); 16537f511862STheodore Ts'o if (ext4_has_group_desc_csum(sb)) { 165419c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT | 165519c5246dSYongqiang Yang EXT4_BG_INODE_UNINIT; 16567f511862STheodore Ts'o if (!test_opt(sb, INIT_INODE_TABLE)) 16577f511862STheodore Ts'o flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED; 16587f511862STheodore Ts'o } else 165919c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED; 166019c5246dSYongqiang Yang } 166119c5246dSYongqiang Yang 1662feb0ab32SDarrick J. Wong if (last_group == n_group && ext4_has_group_desc_csum(sb)) 166319c5246dSYongqiang Yang /* We need to initialize block bitmap of last group. */ 166419c5246dSYongqiang Yang flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT; 166519c5246dSYongqiang Yang 1666d77147ffSharshads if ((last_group == n_group) && (last != clusters_per_group - 1)) { 1667d77147ffSharshads group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1); 1668d77147ffSharshads group_data[i - 1].free_clusters_count -= clusters_per_group - 166919c5246dSYongqiang Yang last - 1; 167019c5246dSYongqiang Yang } 167119c5246dSYongqiang Yang 167219c5246dSYongqiang Yang return 1; 167319c5246dSYongqiang Yang } 167419c5246dSYongqiang Yang 1675ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 1676ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 1677ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 1678ac27a0ecSDave Kleikamp * write the data to disk. 1679ac27a0ecSDave Kleikamp * 1680ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 1681ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 1682ac27a0ecSDave Kleikamp * 1683ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 1684ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 1685ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 1686ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 1687ac27a0ecSDave Kleikamp */ 1688617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 1689ac27a0ecSDave Kleikamp { 169061f296ccSYongqiang Yang struct ext4_new_flex_group_data flex_gd; 1691617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1692617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 1693617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 1694ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1695ac27a0ecSDave Kleikamp struct inode *inode = NULL; 169603b40e34SJon Ernst int gdb_off; 169761f296ccSYongqiang Yang int err; 169861f296ccSYongqiang Yang __u16 bg_flags = 0; 1699ac27a0ecSDave Kleikamp 1700617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 1701ac27a0ecSDave Kleikamp 1702e2b911c5SDarrick J. Wong if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) { 170312062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 1704ac27a0ecSDave Kleikamp return -EPERM; 1705ac27a0ecSDave Kleikamp } 1706ac27a0ecSDave Kleikamp 1707bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 1708bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 170912062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1710ac27a0ecSDave Kleikamp return -EINVAL; 1711ac27a0ecSDave Kleikamp } 1712ac27a0ecSDave Kleikamp 1713617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 1714ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 171512062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 1716ac27a0ecSDave Kleikamp return -EINVAL; 1717ac27a0ecSDave Kleikamp } 1718ac27a0ecSDave Kleikamp 1719ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 1720e647e291Sruippan (潘睿) if (!ext4_has_feature_resize_inode(sb) || 1721e2b911c5SDarrick J. Wong !le16_to_cpu(es->s_reserved_gdt_blocks)) { 172212062dddSEric Sandeen ext4_warning(sb, 1723ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 1724ac27a0ecSDave Kleikamp return -EPERM; 1725ac27a0ecSDave Kleikamp } 17268a363970STheodore Ts'o inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL); 17271d1fe1eeSDavid Howells if (IS_ERR(inode)) { 172812062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 17291d1fe1eeSDavid Howells return PTR_ERR(inode); 1730ac27a0ecSDave Kleikamp } 1731ac27a0ecSDave Kleikamp } 1732ac27a0ecSDave Kleikamp 1733920313a7SAneesh Kumar K.V 173461f296ccSYongqiang Yang err = verify_group_input(sb, input); 1735668f4dc5SYongqiang Yang if (err) 173661f296ccSYongqiang Yang goto out; 1737ac27a0ecSDave Kleikamp 1738117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, input->group + 1); 1739117fff10STheodore Ts'o if (err) 17407f511862STheodore Ts'o goto out; 1741117fff10STheodore Ts'o 174228623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, input->group + 1); 174328623c2fSTheodore Ts'o if (err) 174428623c2fSTheodore Ts'o goto out; 174528623c2fSTheodore Ts'o 174661f296ccSYongqiang Yang flex_gd.count = 1; 174761f296ccSYongqiang Yang flex_gd.groups = input; 174861f296ccSYongqiang Yang flex_gd.bg_flags = &bg_flags; 174961f296ccSYongqiang Yang err = ext4_flex_group_add(sb, inode, &flex_gd); 175061f296ccSYongqiang Yang out: 1751ac27a0ecSDave Kleikamp iput(inode); 1752ac27a0ecSDave Kleikamp return err; 1753617ba13bSMingming Cao } /* ext4_group_add */ 1754ac27a0ecSDave Kleikamp 17552b2d6d01STheodore Ts'o /* 175618e31438SYongqiang Yang * extend a group without checking assuming that checking has been done. 175718e31438SYongqiang Yang */ 175818e31438SYongqiang Yang static int ext4_group_extend_no_check(struct super_block *sb, 175918e31438SYongqiang Yang ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) 176018e31438SYongqiang Yang { 176118e31438SYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 176218e31438SYongqiang Yang handle_t *handle; 176318e31438SYongqiang Yang int err = 0, err2; 176418e31438SYongqiang Yang 176518e31438SYongqiang Yang /* We will update the superblock, one block bitmap, and 176618e31438SYongqiang Yang * one group descriptor via ext4_group_add_blocks(). 176718e31438SYongqiang Yang */ 17689924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3); 176918e31438SYongqiang Yang if (IS_ERR(handle)) { 177018e31438SYongqiang Yang err = PTR_ERR(handle); 177118e31438SYongqiang Yang ext4_warning(sb, "error %d on journal start", err); 177218e31438SYongqiang Yang return err; 177318e31438SYongqiang Yang } 177418e31438SYongqiang Yang 17755d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 1776188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 1777188c299eSJan Kara EXT4_JTR_NONE); 177818e31438SYongqiang Yang if (err) { 177918e31438SYongqiang Yang ext4_warning(sb, "error %d on journal write access", err); 178018e31438SYongqiang Yang goto errout; 178118e31438SYongqiang Yang } 178218e31438SYongqiang Yang 178305c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 178418e31438SYongqiang Yang ext4_blocks_count_set(es, o_blocks_count + add); 1785636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add); 178605c2c00fSJan Kara ext4_superblock_csum_set(sb); 178705c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 178818e31438SYongqiang Yang ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 178918e31438SYongqiang Yang o_blocks_count + add); 179018e31438SYongqiang Yang /* We add the blocks to the bitmap and set the group need init bit */ 179118e31438SYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 179218e31438SYongqiang Yang if (err) 179318e31438SYongqiang Yang goto errout; 1794a3f5cf14SJan Kara ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 179518e31438SYongqiang Yang ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 179618e31438SYongqiang Yang o_blocks_count + add); 179718e31438SYongqiang Yang errout: 179818e31438SYongqiang Yang err2 = ext4_journal_stop(handle); 179918e31438SYongqiang Yang if (err2 && !err) 180018e31438SYongqiang Yang err = err2; 180118e31438SYongqiang Yang 180218e31438SYongqiang Yang if (!err) { 180318e31438SYongqiang Yang if (test_opt(sb, DEBUG)) 180418e31438SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extended group to %llu " 180518e31438SYongqiang Yang "blocks\n", ext4_blocks_count(es)); 18066ca792edSMaarten ter Huurne update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, 180701f795f9SYongqiang Yang (char *)es, sizeof(struct ext4_super_block), 0); 180818e31438SYongqiang Yang } 180918e31438SYongqiang Yang return err; 181018e31438SYongqiang Yang } 181118e31438SYongqiang Yang 181218e31438SYongqiang Yang /* 18132b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 1814ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 1815ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 1816ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 1817ac27a0ecSDave Kleikamp * 1818617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 1819ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 1820ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 1821ac27a0ecSDave Kleikamp */ 1822617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 1823617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 1824ac27a0ecSDave Kleikamp { 1825617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 1826617ba13bSMingming Cao ext4_grpblk_t last; 1827617ba13bSMingming Cao ext4_grpblk_t add; 1828ac27a0ecSDave Kleikamp struct buffer_head *bh; 1829d89651c8SYongqiang Yang int err; 18305f21b0e6SFrederic Bohe ext4_group_t group; 1831ac27a0ecSDave Kleikamp 1832bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 1833ac27a0ecSDave Kleikamp 1834ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 183592b97816STheodore Ts'o ext4_msg(sb, KERN_DEBUG, 183692b97816STheodore Ts'o "extending last group from %llu to %llu blocks", 1837ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 1838ac27a0ecSDave Kleikamp 1839ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 1840ac27a0ecSDave Kleikamp return 0; 1841ac27a0ecSDave Kleikamp 1842ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 184392b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, 184492b97816STheodore Ts'o "filesystem too large to resize to %llu blocks safely", 184592b97816STheodore Ts'o n_blocks_count); 1846ac27a0ecSDave Kleikamp return -EINVAL; 1847ac27a0ecSDave Kleikamp } 1848ac27a0ecSDave Kleikamp 1849ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 185012062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 18518f82f840SYongqiang Yang return -EINVAL; 1852ac27a0ecSDave Kleikamp } 1853ac27a0ecSDave Kleikamp 1854ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 18555f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1856ac27a0ecSDave Kleikamp 1857ac27a0ecSDave Kleikamp if (last == 0) { 185812062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1859ac27a0ecSDave Kleikamp return -EPERM; 1860ac27a0ecSDave Kleikamp } 1861ac27a0ecSDave Kleikamp 1862617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1863ac27a0ecSDave Kleikamp 1864ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 186512062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1866ac27a0ecSDave Kleikamp return -EINVAL; 1867ac27a0ecSDave Kleikamp } 1868ac27a0ecSDave Kleikamp 1869ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1870ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1871ac27a0ecSDave Kleikamp 1872ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 187312062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1874ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1875ac27a0ecSDave Kleikamp 1876ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 18770a846f49Szhangyi (F) bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0); 18780a846f49Szhangyi (F) if (IS_ERR(bh)) { 187912062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1880ac27a0ecSDave Kleikamp return -ENOSPC; 1881ac27a0ecSDave Kleikamp } 1882ac27a0ecSDave Kleikamp brelse(bh); 1883ac27a0ecSDave Kleikamp 1884d89651c8SYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 1885ac27a0ecSDave Kleikamp return err; 1886617ba13bSMingming Cao } /* ext4_group_extend */ 188719c5246dSYongqiang Yang 18881c6bd717STheodore Ts'o 18891c6bd717STheodore Ts'o static int num_desc_blocks(struct super_block *sb, ext4_group_t groups) 18901c6bd717STheodore Ts'o { 18911c6bd717STheodore Ts'o return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb); 18921c6bd717STheodore Ts'o } 18931c6bd717STheodore Ts'o 18941c6bd717STheodore Ts'o /* 18951c6bd717STheodore Ts'o * Release the resize inode and drop the resize_inode feature if there 18961c6bd717STheodore Ts'o * are no more reserved gdt blocks, and then convert the file system 18971c6bd717STheodore Ts'o * to enable meta_bg 18981c6bd717STheodore Ts'o */ 18991c6bd717STheodore Ts'o static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode) 19001c6bd717STheodore Ts'o { 19011c6bd717STheodore Ts'o handle_t *handle; 19021c6bd717STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 19031c6bd717STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 190459e31c15STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 19051c6bd717STheodore Ts'o ext4_fsblk_t nr; 19061c6bd717STheodore Ts'o int i, ret, err = 0; 19071c6bd717STheodore Ts'o int credits = 1; 19081c6bd717STheodore Ts'o 19091c6bd717STheodore Ts'o ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg"); 191059e31c15STheodore Ts'o if (inode) { 19111c6bd717STheodore Ts'o if (es->s_reserved_gdt_blocks) { 19121c6bd717STheodore Ts'o ext4_error(sb, "Unexpected non-zero " 19131c6bd717STheodore Ts'o "s_reserved_gdt_blocks"); 19141c6bd717STheodore Ts'o return -EPERM; 19151c6bd717STheodore Ts'o } 19161c6bd717STheodore Ts'o 19171c6bd717STheodore Ts'o /* Do a quick sanity check of the resize inode */ 1918d77147ffSharshads if (inode->i_blocks != 1 << (inode->i_blkbits - 1919d77147ffSharshads (9 - sbi->s_cluster_bits))) 19201c6bd717STheodore Ts'o goto invalid_resize_inode; 19211c6bd717STheodore Ts'o for (i = 0; i < EXT4_N_BLOCKS; i++) { 19221c6bd717STheodore Ts'o if (i == EXT4_DIND_BLOCK) { 19231c6bd717STheodore Ts'o if (ei->i_data[i]) 19241c6bd717STheodore Ts'o continue; 19251c6bd717STheodore Ts'o else 19261c6bd717STheodore Ts'o goto invalid_resize_inode; 19271c6bd717STheodore Ts'o } 19281c6bd717STheodore Ts'o if (ei->i_data[i]) 19291c6bd717STheodore Ts'o goto invalid_resize_inode; 19301c6bd717STheodore Ts'o } 19311c6bd717STheodore Ts'o credits += 3; /* block bitmap, bg descriptor, resize inode */ 19321c6bd717STheodore Ts'o } 19331c6bd717STheodore Ts'o 19349924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits); 19351c6bd717STheodore Ts'o if (IS_ERR(handle)) 19361c6bd717STheodore Ts'o return PTR_ERR(handle); 19371c6bd717STheodore Ts'o 19385d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 1939188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 1940188c299eSJan Kara EXT4_JTR_NONE); 19411c6bd717STheodore Ts'o if (err) 19421c6bd717STheodore Ts'o goto errout; 19431c6bd717STheodore Ts'o 194405c2c00fSJan Kara lock_buffer(sbi->s_sbh); 1945e2b911c5SDarrick J. Wong ext4_clear_feature_resize_inode(sb); 1946e2b911c5SDarrick J. Wong ext4_set_feature_meta_bg(sb); 19471c6bd717STheodore Ts'o sbi->s_es->s_first_meta_bg = 19481c6bd717STheodore Ts'o cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count)); 194905c2c00fSJan Kara ext4_superblock_csum_set(sb); 195005c2c00fSJan Kara unlock_buffer(sbi->s_sbh); 19511c6bd717STheodore Ts'o 1952a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 19531c6bd717STheodore Ts'o if (err) { 19541c6bd717STheodore Ts'o ext4_std_error(sb, err); 19551c6bd717STheodore Ts'o goto errout; 19561c6bd717STheodore Ts'o } 19571c6bd717STheodore Ts'o 19581c6bd717STheodore Ts'o if (inode) { 19591c6bd717STheodore Ts'o nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]); 19601c6bd717STheodore Ts'o ext4_free_blocks(handle, inode, NULL, nr, 1, 19611c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | 19621c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 19631c6bd717STheodore Ts'o ei->i_data[EXT4_DIND_BLOCK] = 0; 19641c6bd717STheodore Ts'o inode->i_blocks = 0; 19651c6bd717STheodore Ts'o 19661c6bd717STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 19671c6bd717STheodore Ts'o if (err) 19681c6bd717STheodore Ts'o ext4_std_error(sb, err); 19691c6bd717STheodore Ts'o } 19701c6bd717STheodore Ts'o 19711c6bd717STheodore Ts'o errout: 19721c6bd717STheodore Ts'o ret = ext4_journal_stop(handle); 19731c6bd717STheodore Ts'o if (!err) 19741c6bd717STheodore Ts'o err = ret; 19751c6bd717STheodore Ts'o return ret; 19761c6bd717STheodore Ts'o 19771c6bd717STheodore Ts'o invalid_resize_inode: 19781c6bd717STheodore Ts'o ext4_error(sb, "corrupted/inconsistent resize inode"); 19791c6bd717STheodore Ts'o return -EINVAL; 19801c6bd717STheodore Ts'o } 19811c6bd717STheodore Ts'o 198219c5246dSYongqiang Yang /* 198319c5246dSYongqiang Yang * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count 198419c5246dSYongqiang Yang * 198519c5246dSYongqiang Yang * @sb: super block of the fs to be resized 198619c5246dSYongqiang Yang * @n_blocks_count: the number of blocks resides in the resized fs 198719c5246dSYongqiang Yang */ 198819c5246dSYongqiang Yang int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count) 198919c5246dSYongqiang Yang { 199019c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd = NULL; 199119c5246dSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 199219c5246dSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 199319c5246dSYongqiang Yang struct buffer_head *bh; 199401f795f9SYongqiang Yang struct inode *resize_inode = NULL; 199501f795f9SYongqiang Yang ext4_grpblk_t add, offset; 199619c5246dSYongqiang Yang unsigned long n_desc_blocks; 199719c5246dSYongqiang Yang unsigned long o_desc_blocks; 199801f795f9SYongqiang Yang ext4_group_t o_group; 199901f795f9SYongqiang Yang ext4_group_t n_group; 200001f795f9SYongqiang Yang ext4_fsblk_t o_blocks_count; 20011c6bd717STheodore Ts'o ext4_fsblk_t n_blocks_count_retry = 0; 20024da4a56eSTheodore Ts'o unsigned long last_update_time = 0; 2003117fff10STheodore Ts'o int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex; 200401f795f9SYongqiang Yang int meta_bg; 200519c5246dSYongqiang Yang 200659e31c15STheodore Ts'o /* See if the device is actually as big as what was requested */ 20070a846f49Szhangyi (F) bh = ext4_sb_bread(sb, n_blocks_count - 1, 0); 20080a846f49Szhangyi (F) if (IS_ERR(bh)) { 200959e31c15STheodore Ts'o ext4_warning(sb, "can't read last block, resize aborted"); 201059e31c15STheodore Ts'o return -ENOSPC; 201159e31c15STheodore Ts'o } 201259e31c15STheodore Ts'o brelse(bh); 201359e31c15STheodore Ts'o 201469cb8e9dSKiselev, Oleg /* 201569cb8e9dSKiselev, Oleg * For bigalloc, trim the requested size to the nearest cluster 201669cb8e9dSKiselev, Oleg * boundary to avoid creating an unusable filesystem. We do this 201769cb8e9dSKiselev, Oleg * silently, instead of returning an error, to avoid breaking 201869cb8e9dSKiselev, Oleg * callers that blindly resize the filesystem to the full size of 201969cb8e9dSKiselev, Oleg * the underlying block device. 202069cb8e9dSKiselev, Oleg */ 202169cb8e9dSKiselev, Oleg if (ext4_has_feature_bigalloc(sb)) 202269cb8e9dSKiselev, Oleg n_blocks_count &= ~((1 << EXT4_CLUSTER_BITS(sb)) - 1); 202369cb8e9dSKiselev, Oleg 20241c6bd717STheodore Ts'o retry: 202519c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 202619c5246dSYongqiang Yang 202759e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu " 202892b97816STheodore Ts'o "to %llu blocks", o_blocks_count, n_blocks_count); 202919c5246dSYongqiang Yang 203019c5246dSYongqiang Yang if (n_blocks_count < o_blocks_count) { 203119c5246dSYongqiang Yang /* On-line shrinking not supported */ 203219c5246dSYongqiang Yang ext4_warning(sb, "can't shrink FS - resize aborted"); 203319c5246dSYongqiang Yang return -EINVAL; 203419c5246dSYongqiang Yang } 203519c5246dSYongqiang Yang 203619c5246dSYongqiang Yang if (n_blocks_count == o_blocks_count) 203719c5246dSYongqiang Yang /* Nothing need to do */ 203819c5246dSYongqiang Yang return 0; 203919c5246dSYongqiang Yang 2040bd86298eSLukas Czerner n_group = ext4_get_group_number(sb, n_blocks_count - 1); 20414f2f76f7SJan Kara if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) { 20423f8a6411STheodore Ts'o ext4_warning(sb, "resize would cause inodes_count overflow"); 20433f8a6411STheodore Ts'o return -EINVAL; 20443f8a6411STheodore Ts'o } 2045a0ade1deSLukas Czerner ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset); 204619c5246dSYongqiang Yang 20471c6bd717STheodore Ts'o n_desc_blocks = num_desc_blocks(sb, n_group + 1); 20481c6bd717STheodore Ts'o o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count); 204919c5246dSYongqiang Yang 2050e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 205101f795f9SYongqiang Yang 2052e2b911c5SDarrick J. Wong if (ext4_has_feature_resize_inode(sb)) { 205301f795f9SYongqiang Yang if (meta_bg) { 205401f795f9SYongqiang Yang ext4_error(sb, "resize_inode and meta_bg enabled " 205501f795f9SYongqiang Yang "simultaneously"); 205601f795f9SYongqiang Yang return -EINVAL; 205701f795f9SYongqiang Yang } 20581c6bd717STheodore Ts'o if (n_desc_blocks > o_desc_blocks + 20591c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks)) { 20601c6bd717STheodore Ts'o n_blocks_count_retry = n_blocks_count; 20611c6bd717STheodore Ts'o n_desc_blocks = o_desc_blocks + 20621c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks); 20631c6bd717STheodore Ts'o n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb); 2064aec51758SJerry Lee n_blocks_count = (ext4_fsblk_t)n_group * 2065f96c3ac8SJan Kara EXT4_BLOCKS_PER_GROUP(sb) + 2066f96c3ac8SJan Kara le32_to_cpu(es->s_first_data_block); 20671c6bd717STheodore Ts'o n_group--; /* set to last group number */ 206819c5246dSYongqiang Yang } 20691c6bd717STheodore Ts'o 20701c6bd717STheodore Ts'o if (!resize_inode) 20718a363970STheodore Ts'o resize_inode = ext4_iget(sb, EXT4_RESIZE_INO, 20728a363970STheodore Ts'o EXT4_IGET_SPECIAL); 207319c5246dSYongqiang Yang if (IS_ERR(resize_inode)) { 207419c5246dSYongqiang Yang ext4_warning(sb, "Error opening resize inode"); 207519c5246dSYongqiang Yang return PTR_ERR(resize_inode); 207619c5246dSYongqiang Yang } 20771c6bd717STheodore Ts'o } 20781c6bd717STheodore Ts'o 207959e31c15STheodore Ts'o if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) { 20801c6bd717STheodore Ts'o err = ext4_convert_meta_bg(sb, resize_inode); 20811c6bd717STheodore Ts'o if (err) 20821c6bd717STheodore Ts'o goto out; 20831c6bd717STheodore Ts'o if (resize_inode) { 20841c6bd717STheodore Ts'o iput(resize_inode); 20851c6bd717STheodore Ts'o resize_inode = NULL; 20861c6bd717STheodore Ts'o } 20871c6bd717STheodore Ts'o if (n_blocks_count_retry) { 20881c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 20891c6bd717STheodore Ts'o n_blocks_count_retry = 0; 20901c6bd717STheodore Ts'o goto retry; 20911c6bd717STheodore Ts'o } 209201f795f9SYongqiang Yang } 209319c5246dSYongqiang Yang 2094f0a459deSTheodore Ts'o /* 2095f0a459deSTheodore Ts'o * Make sure the last group has enough space so that it's 2096f0a459deSTheodore Ts'o * guaranteed to have enough space for all metadata blocks 2097f0a459deSTheodore Ts'o * that it might need to hold. (We might not need to store 2098f0a459deSTheodore Ts'o * the inode table blocks in the last block group, but there 2099f0a459deSTheodore Ts'o * will be cases where this might be needed.) 2100f0a459deSTheodore Ts'o */ 2101f0a459deSTheodore Ts'o if ((ext4_group_first_block_no(sb, n_group) + 2102f0a459deSTheodore Ts'o ext4_group_overhead_blocks(sb, n_group) + 2 + 2103f0a459deSTheodore Ts'o sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) { 2104f0a459deSTheodore Ts'o n_blocks_count = ext4_group_first_block_no(sb, n_group); 2105f0a459deSTheodore Ts'o n_group--; 2106f0a459deSTheodore Ts'o n_blocks_count_retry = 0; 2107f0a459deSTheodore Ts'o if (resize_inode) { 2108f0a459deSTheodore Ts'o iput(resize_inode); 2109f0a459deSTheodore Ts'o resize_inode = NULL; 2110f0a459deSTheodore Ts'o } 2111f0a459deSTheodore Ts'o goto retry; 2112f0a459deSTheodore Ts'o } 2113f0a459deSTheodore Ts'o 211419c5246dSYongqiang Yang /* extend the last group */ 2115a0ade1deSLukas Czerner if (n_group == o_group) 2116a0ade1deSLukas Czerner add = n_blocks_count - o_blocks_count; 2117a0ade1deSLukas Czerner else 2118d77147ffSharshads add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1)); 2119a0ade1deSLukas Czerner if (add > 0) { 212019c5246dSYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 212119c5246dSYongqiang Yang if (err) 212219c5246dSYongqiang Yang goto out; 212319c5246dSYongqiang Yang } 212419c5246dSYongqiang Yang 2125*df3cb754SJerry Lee 李修賢 if (ext4_blocks_count(es) == n_blocks_count && n_blocks_count_retry == 0) 212619c5246dSYongqiang Yang goto out; 212719c5246dSYongqiang Yang 2128117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, n_group + 1); 2129117fff10STheodore Ts'o if (err) 2130f348e224SVasily Averin goto out; 2131117fff10STheodore Ts'o 213228623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, n_group + 1); 213328623c2fSTheodore Ts'o if (err) 213428623c2fSTheodore Ts'o goto out; 213528623c2fSTheodore Ts'o 213619c5246dSYongqiang Yang flex_gd = alloc_flex_gd(flexbg_size); 213719c5246dSYongqiang Yang if (flex_gd == NULL) { 213819c5246dSYongqiang Yang err = -ENOMEM; 213919c5246dSYongqiang Yang goto out; 214019c5246dSYongqiang Yang } 214119c5246dSYongqiang Yang 214219c5246dSYongqiang Yang /* Add flex groups. Note that a regular group is a 214319c5246dSYongqiang Yang * flex group with 1 group. 214419c5246dSYongqiang Yang */ 214519c5246dSYongqiang Yang while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count, 214619c5246dSYongqiang Yang flexbg_size)) { 2147a861fb9fSWang Qing if (time_is_before_jiffies(last_update_time + HZ * 10)) { 21484da4a56eSTheodore Ts'o if (last_update_time) 21494da4a56eSTheodore Ts'o ext4_msg(sb, KERN_INFO, 21504da4a56eSTheodore Ts'o "resized to %llu blocks", 21514da4a56eSTheodore Ts'o ext4_blocks_count(es)); 21524da4a56eSTheodore Ts'o last_update_time = jiffies; 21534da4a56eSTheodore Ts'o } 215403c1c290SYongqiang Yang if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0) 215503c1c290SYongqiang Yang break; 215619c5246dSYongqiang Yang err = ext4_flex_group_add(sb, resize_inode, flex_gd); 215719c5246dSYongqiang Yang if (unlikely(err)) 215819c5246dSYongqiang Yang break; 215919c5246dSYongqiang Yang } 216019c5246dSYongqiang Yang 21611c6bd717STheodore Ts'o if (!err && n_blocks_count_retry) { 21621c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 21631c6bd717STheodore Ts'o n_blocks_count_retry = 0; 21641c6bd717STheodore Ts'o free_flex_gd(flex_gd); 21651c6bd717STheodore Ts'o flex_gd = NULL; 2166db6aee62SVasily Averin if (resize_inode) { 2167db6aee62SVasily Averin iput(resize_inode); 2168db6aee62SVasily Averin resize_inode = NULL; 2169db6aee62SVasily Averin } 21701c6bd717STheodore Ts'o goto retry; 21711c6bd717STheodore Ts'o } 21721c6bd717STheodore Ts'o 217319c5246dSYongqiang Yang out: 217419c5246dSYongqiang Yang if (flex_gd) 217519c5246dSYongqiang Yang free_flex_gd(flex_gd); 217601f795f9SYongqiang Yang if (resize_inode != NULL) 217719c5246dSYongqiang Yang iput(resize_inode); 21786c732840SLukas Czerner if (err) 21796c732840SLukas Czerner ext4_warning(sb, "error (%d) occurred during " 21806c732840SLukas Czerner "file system resize", err); 21816c732840SLukas Czerner ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", 21826c732840SLukas Czerner ext4_blocks_count(es)); 218319c5246dSYongqiang Yang return err; 218419c5246dSYongqiang Yang } 2185