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 1008f82f840SYongqiang Yang void ext4_resize_end(struct super_block *sb) 1018f82f840SYongqiang Yang { 1029549a168STheodore Ts'o clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags); 1034e857c58SPeter Zijlstra smp_mb__after_atomic(); 1048f82f840SYongqiang Yang } 1058f82f840SYongqiang Yang 10601f795f9SYongqiang Yang static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb, 10701f795f9SYongqiang Yang ext4_group_t group) { 10801f795f9SYongqiang Yang return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) << 10901f795f9SYongqiang Yang EXT4_DESC_PER_BLOCK_BITS(sb); 11001f795f9SYongqiang Yang } 11101f795f9SYongqiang Yang 11201f795f9SYongqiang Yang static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb, 11301f795f9SYongqiang Yang ext4_group_t group) { 11401f795f9SYongqiang Yang group = ext4_meta_bg_first_group(sb, group); 11501f795f9SYongqiang Yang return ext4_group_first_block_no(sb, group); 11601f795f9SYongqiang Yang } 11701f795f9SYongqiang Yang 11801f795f9SYongqiang Yang static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb, 11901f795f9SYongqiang Yang ext4_group_t group) { 12001f795f9SYongqiang Yang ext4_grpblk_t overhead; 12101f795f9SYongqiang Yang overhead = ext4_bg_num_gdb(sb, group); 12201f795f9SYongqiang Yang if (ext4_bg_has_super(sb, group)) 12301f795f9SYongqiang Yang overhead += 1 + 12401f795f9SYongqiang Yang le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 12501f795f9SYongqiang Yang return overhead; 12601f795f9SYongqiang Yang } 12701f795f9SYongqiang Yang 128ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 129ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 130ac27a0ecSDave Kleikamp 131ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 132617ba13bSMingming Cao struct ext4_new_group_data *input) 133ac27a0ecSDave Kleikamp { 134617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 135617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 136bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 137617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 138fd2d4291SAvantika Mathur ext4_group_t group = input->group; 139617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 140b302ef2dSTheodore Ts'o unsigned overhead; 141b302ef2dSTheodore Ts'o ext4_fsblk_t metaend; 142ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 1433a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 144ac27a0ecSDave Kleikamp int err = -EINVAL; 145ac27a0ecSDave Kleikamp 146b302ef2dSTheodore Ts'o if (group != sbi->s_groups_count) { 147b302ef2dSTheodore Ts'o ext4_warning(sb, "Cannot add at group %u (only %u groups)", 148b302ef2dSTheodore Ts'o input->group, sbi->s_groups_count); 149b302ef2dSTheodore Ts'o return -EINVAL; 150b302ef2dSTheodore Ts'o } 151b302ef2dSTheodore Ts'o 152b302ef2dSTheodore Ts'o overhead = ext4_group_overhead_blocks(sb, group); 153b302ef2dSTheodore Ts'o metaend = start + overhead; 154d77147ffSharshads input->free_clusters_count = free_blocks_count = 155ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 156ac27a0ecSDave Kleikamp 157ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 158617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 159ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 160617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 161ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 162ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 163ac27a0ecSDave Kleikamp 1643a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 165b302ef2dSTheodore Ts'o if (offset != 0) 16612062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 167ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 16812062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 169ac27a0ecSDave Kleikamp input->reserved_blocks); 170ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 17112062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 172ac27a0ecSDave Kleikamp input->blocks_count); 173fb265c9cSTheodore Ts'o else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) { 174fb265c9cSTheodore Ts'o err = PTR_ERR(bh); 175fb265c9cSTheodore Ts'o bh = NULL; 17612062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 177ac27a0ecSDave Kleikamp end - 1); 178fb265c9cSTheodore Ts'o } else if (outside(input->block_bitmap, start, end)) 17912062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 1801939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 181ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 18212062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 1831939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 184ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 185ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 18612062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1871939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 188ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 18912062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1901939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 191ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 19212062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 19312062dddSEric Sandeen "(%llu-%llu)", 1941939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1951939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 196ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 19712062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 19812062dddSEric Sandeen "(%llu-%llu)", 1991939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 2001939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 201ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 20212062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 2031939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 2041939e49aSRandy Dunlap start, metaend - 1); 205ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 20612062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 2071939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 2081939e49aSRandy Dunlap start, metaend - 1); 209ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 210ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 21112062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 21212062dddSEric Sandeen "(%llu-%llu)", 2131939e49aSRandy Dunlap (unsigned long long)input->inode_table, 2141939e49aSRandy Dunlap itend - 1, start, metaend - 1); 215ac27a0ecSDave Kleikamp else 216ac27a0ecSDave Kleikamp err = 0; 217ac27a0ecSDave Kleikamp brelse(bh); 218ac27a0ecSDave Kleikamp 219ac27a0ecSDave Kleikamp return err; 220ac27a0ecSDave Kleikamp } 221ac27a0ecSDave Kleikamp 22228c7bac0SYongqiang Yang /* 22328c7bac0SYongqiang Yang * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex 22428c7bac0SYongqiang Yang * group each time. 22528c7bac0SYongqiang Yang */ 22628c7bac0SYongqiang Yang struct ext4_new_flex_group_data { 22728c7bac0SYongqiang Yang struct ext4_new_group_data *groups; /* new_group_data for groups 22828c7bac0SYongqiang Yang in the flex group */ 22928c7bac0SYongqiang Yang __u16 *bg_flags; /* block group flags of groups 23028c7bac0SYongqiang Yang in @groups */ 23128c7bac0SYongqiang Yang ext4_group_t count; /* number of groups in @groups 23228c7bac0SYongqiang Yang */ 23328c7bac0SYongqiang Yang }; 23428c7bac0SYongqiang Yang 23528c7bac0SYongqiang Yang /* 23628c7bac0SYongqiang Yang * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of 23728c7bac0SYongqiang Yang * @flexbg_size. 23828c7bac0SYongqiang Yang * 23928c7bac0SYongqiang Yang * Returns NULL on failure otherwise address of the allocated structure. 24028c7bac0SYongqiang Yang */ 24128c7bac0SYongqiang Yang static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) 24228c7bac0SYongqiang Yang { 24328c7bac0SYongqiang Yang struct ext4_new_flex_group_data *flex_gd; 24428c7bac0SYongqiang Yang 24528c7bac0SYongqiang Yang flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS); 24628c7bac0SYongqiang Yang if (flex_gd == NULL) 24728c7bac0SYongqiang Yang goto out3; 24828c7bac0SYongqiang Yang 24946901760SInsu Yun if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data)) 250967ac8afSHaogang Chen goto out2; 25128c7bac0SYongqiang Yang flex_gd->count = flexbg_size; 25228c7bac0SYongqiang Yang 2536da2ec56SKees Cook flex_gd->groups = kmalloc_array(flexbg_size, 2546da2ec56SKees Cook sizeof(struct ext4_new_group_data), 2556da2ec56SKees Cook GFP_NOFS); 25628c7bac0SYongqiang Yang if (flex_gd->groups == NULL) 25728c7bac0SYongqiang Yang goto out2; 25828c7bac0SYongqiang Yang 2596da2ec56SKees Cook flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16), 2606da2ec56SKees Cook GFP_NOFS); 26128c7bac0SYongqiang Yang if (flex_gd->bg_flags == NULL) 26228c7bac0SYongqiang Yang goto out1; 26328c7bac0SYongqiang Yang 26428c7bac0SYongqiang Yang return flex_gd; 26528c7bac0SYongqiang Yang 26628c7bac0SYongqiang Yang out1: 26728c7bac0SYongqiang Yang kfree(flex_gd->groups); 26828c7bac0SYongqiang Yang out2: 26928c7bac0SYongqiang Yang kfree(flex_gd); 27028c7bac0SYongqiang Yang out3: 27128c7bac0SYongqiang Yang return NULL; 27228c7bac0SYongqiang Yang } 27328c7bac0SYongqiang Yang 27428c7bac0SYongqiang Yang static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd) 27528c7bac0SYongqiang Yang { 27628c7bac0SYongqiang Yang kfree(flex_gd->bg_flags); 27728c7bac0SYongqiang Yang kfree(flex_gd->groups); 27828c7bac0SYongqiang Yang kfree(flex_gd); 27928c7bac0SYongqiang Yang } 28028c7bac0SYongqiang Yang 2813fbea4b3SYongqiang Yang /* 2823fbea4b3SYongqiang Yang * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps 2833fbea4b3SYongqiang Yang * and inode tables for a flex group. 2843fbea4b3SYongqiang Yang * 2853fbea4b3SYongqiang Yang * This function is used by 64bit-resize. Note that this function allocates 2863fbea4b3SYongqiang Yang * group tables from the 1st group of groups contained by @flexgd, which may 2873fbea4b3SYongqiang Yang * be a partial of a flex group. 2883fbea4b3SYongqiang Yang * 2893fbea4b3SYongqiang Yang * @sb: super block of fs to which the groups belongs 29003c1c290SYongqiang Yang * 29103c1c290SYongqiang Yang * Returns 0 on a successful allocation of the metadata blocks in the 29203c1c290SYongqiang Yang * block group. 2933fbea4b3SYongqiang Yang */ 29403c1c290SYongqiang Yang static int ext4_alloc_group_tables(struct super_block *sb, 2953fbea4b3SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 2963fbea4b3SYongqiang Yang int flexbg_size) 2973fbea4b3SYongqiang Yang { 2983fbea4b3SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 2993fbea4b3SYongqiang Yang ext4_fsblk_t start_blk; 3003fbea4b3SYongqiang Yang ext4_fsblk_t last_blk; 3013fbea4b3SYongqiang Yang ext4_group_t src_group; 3023fbea4b3SYongqiang Yang ext4_group_t bb_index = 0; 3033fbea4b3SYongqiang Yang ext4_group_t ib_index = 0; 3043fbea4b3SYongqiang Yang ext4_group_t it_index = 0; 3053fbea4b3SYongqiang Yang ext4_group_t group; 3063fbea4b3SYongqiang Yang ext4_group_t last_group; 3073fbea4b3SYongqiang Yang unsigned overhead; 308b93c9535STheodore Ts'o __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0; 309d77147ffSharshads int i; 3103fbea4b3SYongqiang Yang 3113fbea4b3SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 3123fbea4b3SYongqiang Yang 3133fbea4b3SYongqiang Yang src_group = group_data[0].group; 3143fbea4b3SYongqiang Yang last_group = src_group + flex_gd->count - 1; 3153fbea4b3SYongqiang Yang 3163fbea4b3SYongqiang Yang BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) != 3173fbea4b3SYongqiang Yang (last_group & ~(flexbg_size - 1)))); 3183fbea4b3SYongqiang Yang next_group: 3193fbea4b3SYongqiang Yang group = group_data[0].group; 32003c1c290SYongqiang Yang if (src_group >= group_data[0].group + flex_gd->count) 32103c1c290SYongqiang Yang return -ENOSPC; 3223fbea4b3SYongqiang Yang start_blk = ext4_group_first_block_no(sb, src_group); 3233fbea4b3SYongqiang Yang last_blk = start_blk + group_data[src_group - group].blocks_count; 3243fbea4b3SYongqiang Yang 32501f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 3263fbea4b3SYongqiang Yang 3273fbea4b3SYongqiang Yang start_blk += overhead; 3283fbea4b3SYongqiang Yang 3293fbea4b3SYongqiang Yang /* We collect contiguous blocks as much as possible. */ 3303fbea4b3SYongqiang Yang src_group++; 33101f795f9SYongqiang Yang for (; src_group <= last_group; src_group++) { 33201f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 333b93c9535STheodore Ts'o if (overhead == 0) 3343fbea4b3SYongqiang Yang last_blk += group_data[src_group - group].blocks_count; 3353fbea4b3SYongqiang Yang else 3363fbea4b3SYongqiang Yang break; 33701f795f9SYongqiang Yang } 3383fbea4b3SYongqiang Yang 3393fbea4b3SYongqiang Yang /* Allocate block bitmaps */ 3403fbea4b3SYongqiang Yang for (; bb_index < flex_gd->count; bb_index++) { 3413fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3423fbea4b3SYongqiang Yang goto next_group; 3433fbea4b3SYongqiang Yang group_data[bb_index].block_bitmap = start_blk++; 344bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3453fbea4b3SYongqiang Yang group -= group_data[0].group; 346d77147ffSharshads group_data[group].mdata_blocks++; 347b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3483fbea4b3SYongqiang Yang } 3493fbea4b3SYongqiang Yang 3503fbea4b3SYongqiang Yang /* Allocate inode bitmaps */ 3513fbea4b3SYongqiang Yang for (; ib_index < flex_gd->count; ib_index++) { 3523fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3533fbea4b3SYongqiang Yang goto next_group; 3543fbea4b3SYongqiang Yang group_data[ib_index].inode_bitmap = start_blk++; 355bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3563fbea4b3SYongqiang Yang group -= group_data[0].group; 357d77147ffSharshads group_data[group].mdata_blocks++; 358b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3593fbea4b3SYongqiang Yang } 3603fbea4b3SYongqiang Yang 3613fbea4b3SYongqiang Yang /* Allocate inode tables */ 3623fbea4b3SYongqiang Yang for (; it_index < flex_gd->count; it_index++) { 363b93c9535STheodore Ts'o unsigned int itb = EXT4_SB(sb)->s_itb_per_group; 364b93c9535STheodore Ts'o ext4_fsblk_t next_group_start; 365b93c9535STheodore Ts'o 366b93c9535STheodore Ts'o if (start_blk + itb > last_blk) 3673fbea4b3SYongqiang Yang goto next_group; 3683fbea4b3SYongqiang Yang group_data[it_index].inode_table = start_blk; 369b93c9535STheodore Ts'o group = ext4_get_group_number(sb, start_blk); 370b93c9535STheodore Ts'o next_group_start = ext4_group_first_block_no(sb, group + 1); 3713fbea4b3SYongqiang Yang group -= group_data[0].group; 3723fbea4b3SYongqiang Yang 373b93c9535STheodore Ts'o if (start_blk + itb > next_group_start) { 374b93c9535STheodore Ts'o flex_gd->bg_flags[group + 1] &= uninit_mask; 375b93c9535STheodore Ts'o overhead = start_blk + itb - next_group_start; 376d77147ffSharshads group_data[group + 1].mdata_blocks += overhead; 377b93c9535STheodore Ts'o itb -= overhead; 378b93c9535STheodore Ts'o } 379b93c9535STheodore Ts'o 380d77147ffSharshads group_data[group].mdata_blocks += itb; 381b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3823fbea4b3SYongqiang Yang start_blk += EXT4_SB(sb)->s_itb_per_group; 3833fbea4b3SYongqiang Yang } 3843fbea4b3SYongqiang Yang 385d77147ffSharshads /* Update free clusters count to exclude metadata blocks */ 386d77147ffSharshads for (i = 0; i < flex_gd->count; i++) { 387d77147ffSharshads group_data[i].free_clusters_count -= 388d77147ffSharshads EXT4_NUM_B2C(EXT4_SB(sb), 389d77147ffSharshads group_data[i].mdata_blocks); 390d77147ffSharshads } 391d77147ffSharshads 3923fbea4b3SYongqiang Yang if (test_opt(sb, DEBUG)) { 3933fbea4b3SYongqiang Yang int i; 3943fbea4b3SYongqiang Yang group = group_data[0].group; 3953fbea4b3SYongqiang Yang 3963fbea4b3SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: adding a flex group with " 3973fbea4b3SYongqiang Yang "%d groups, flexbg size is %d:\n", flex_gd->count, 3983fbea4b3SYongqiang Yang flexbg_size); 3993fbea4b3SYongqiang Yang 4003fbea4b3SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 401d77147ffSharshads ext4_debug( 402d77147ffSharshads "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n", 4033fbea4b3SYongqiang Yang ext4_bg_has_super(sb, group + i) ? "normal" : 4043fbea4b3SYongqiang Yang "no-super", group + i, 4053fbea4b3SYongqiang Yang group_data[i].blocks_count, 406d77147ffSharshads group_data[i].free_clusters_count, 407d77147ffSharshads group_data[i].mdata_blocks); 4083fbea4b3SYongqiang Yang } 4093fbea4b3SYongqiang Yang } 41003c1c290SYongqiang Yang return 0; 4113fbea4b3SYongqiang Yang } 4123fbea4b3SYongqiang Yang 413ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 414617ba13bSMingming Cao ext4_fsblk_t blk) 415ac27a0ecSDave Kleikamp { 416ac27a0ecSDave Kleikamp struct buffer_head *bh; 417ac27a0ecSDave Kleikamp int err; 418ac27a0ecSDave Kleikamp 419ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 420aebf0243SWang Shilong if (unlikely(!bh)) 421860d21e2STheodore Ts'o return ERR_PTR(-ENOMEM); 4225d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 423188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE); 424188c299eSJan Kara if (err) { 425ac27a0ecSDave Kleikamp brelse(bh); 426ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 427ac27a0ecSDave Kleikamp } else { 428ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 429ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 430ac27a0ecSDave Kleikamp } 431ac27a0ecSDave Kleikamp 432ac27a0ecSDave Kleikamp return bh; 433ac27a0ecSDave Kleikamp } 434ac27a0ecSDave Kleikamp 43583448bdfSJan Kara static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits) 43683448bdfSJan Kara { 43783448bdfSJan Kara return ext4_journal_ensure_credits_fn(handle, credits, 43883448bdfSJan Kara EXT4_MAX_TRANS_DATA, 0, 0); 43983448bdfSJan Kara } 44083448bdfSJan Kara 441ac27a0ecSDave Kleikamp /* 442d77147ffSharshads * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used. 44333afdcc5SYongqiang Yang * 44433afdcc5SYongqiang Yang * Helper function for ext4_setup_new_group_blocks() which set . 44533afdcc5SYongqiang Yang * 44633afdcc5SYongqiang Yang * @sb: super block 44733afdcc5SYongqiang Yang * @handle: journal handle 44833afdcc5SYongqiang Yang * @flex_gd: flex group data 44933afdcc5SYongqiang Yang */ 45033afdcc5SYongqiang Yang static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle, 45133afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 452d77147ffSharshads ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster) 45333afdcc5SYongqiang Yang { 454d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 455d77147ffSharshads ext4_group_t count = last_cluster - first_cluster + 1; 45633afdcc5SYongqiang Yang ext4_group_t count2; 45733afdcc5SYongqiang Yang 458d77147ffSharshads ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster, 459d77147ffSharshads last_cluster); 460d77147ffSharshads for (count2 = count; count > 0; 461d77147ffSharshads count -= count2, first_cluster += count2) { 46233afdcc5SYongqiang Yang ext4_fsblk_t start; 46333afdcc5SYongqiang Yang struct buffer_head *bh; 46433afdcc5SYongqiang Yang ext4_group_t group; 46533afdcc5SYongqiang Yang int err; 46633afdcc5SYongqiang Yang 467d77147ffSharshads group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster)); 468d77147ffSharshads start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group)); 46933afdcc5SYongqiang Yang group -= flex_gd->groups[0].group; 47033afdcc5SYongqiang Yang 471d77147ffSharshads count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start); 47233afdcc5SYongqiang Yang if (count2 > count) 47333afdcc5SYongqiang Yang count2 = count; 47433afdcc5SYongqiang Yang 47533afdcc5SYongqiang Yang if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) { 47633afdcc5SYongqiang Yang BUG_ON(flex_gd->count > 1); 47733afdcc5SYongqiang Yang continue; 47833afdcc5SYongqiang Yang } 47933afdcc5SYongqiang Yang 48083448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 481a4130367SJan Kara if (err < 0) 48233afdcc5SYongqiang Yang return err; 48333afdcc5SYongqiang Yang 48433afdcc5SYongqiang Yang bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap); 485aebf0243SWang Shilong if (unlikely(!bh)) 486860d21e2STheodore Ts'o return -ENOMEM; 48733afdcc5SYongqiang Yang 4885d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 489188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bh, 490188c299eSJan Kara EXT4_JTR_NONE); 491cea57941SVasily Averin if (err) { 492cea57941SVasily Averin brelse(bh); 49333afdcc5SYongqiang Yang return err; 494cea57941SVasily Averin } 495d77147ffSharshads ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", 496d77147ffSharshads first_cluster, first_cluster - start, count2); 497123e3016SRitesh Harjani mb_set_bits(bh->b_data, first_cluster - start, count2); 49833afdcc5SYongqiang Yang 49933afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 500cea57941SVasily Averin brelse(bh); 50133afdcc5SYongqiang Yang if (unlikely(err)) 50233afdcc5SYongqiang Yang return err; 50333afdcc5SYongqiang Yang } 50433afdcc5SYongqiang Yang 50533afdcc5SYongqiang Yang return 0; 50633afdcc5SYongqiang Yang } 50733afdcc5SYongqiang Yang 50833afdcc5SYongqiang Yang /* 50933afdcc5SYongqiang Yang * Set up the block and inode bitmaps, and the inode table for the new groups. 51033afdcc5SYongqiang Yang * This doesn't need to be part of the main transaction, since we are only 51133afdcc5SYongqiang Yang * changing blocks outside the actual filesystem. We still do journaling to 51233afdcc5SYongqiang Yang * ensure the recovery is correct in case of a failure just after resize. 51333afdcc5SYongqiang Yang * If any part of this fails, we simply abort the resize. 51433afdcc5SYongqiang Yang * 51533afdcc5SYongqiang Yang * setup_new_flex_group_blocks handles a flex group as follow: 51633afdcc5SYongqiang Yang * 1. copy super block and GDT, and initialize group tables if necessary. 51733afdcc5SYongqiang Yang * In this step, we only set bits in blocks bitmaps for blocks taken by 51833afdcc5SYongqiang Yang * super block and GDT. 51933afdcc5SYongqiang Yang * 2. allocate group tables in block bitmaps, that is, set bits in block 52033afdcc5SYongqiang Yang * bitmap for blocks taken by group tables. 52133afdcc5SYongqiang Yang */ 52233afdcc5SYongqiang Yang static int setup_new_flex_group_blocks(struct super_block *sb, 52333afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 52433afdcc5SYongqiang Yang { 52533afdcc5SYongqiang Yang int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group}; 52633afdcc5SYongqiang Yang ext4_fsblk_t start; 52733afdcc5SYongqiang Yang ext4_fsblk_t block; 52833afdcc5SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 52933afdcc5SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 53033afdcc5SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 53133afdcc5SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 53233afdcc5SYongqiang Yang handle_t *handle; 53333afdcc5SYongqiang Yang ext4_group_t group, count; 53433afdcc5SYongqiang Yang struct buffer_head *bh = NULL; 53533afdcc5SYongqiang Yang int reserved_gdb, i, j, err = 0, err2; 53601f795f9SYongqiang Yang int meta_bg; 53733afdcc5SYongqiang Yang 53833afdcc5SYongqiang Yang BUG_ON(!flex_gd->count || !group_data || 53933afdcc5SYongqiang Yang group_data[0].group != sbi->s_groups_count); 54033afdcc5SYongqiang Yang 54133afdcc5SYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 542e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 54333afdcc5SYongqiang Yang 54433afdcc5SYongqiang Yang /* This transaction may be extended/restarted along the way */ 5459924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 54633afdcc5SYongqiang Yang if (IS_ERR(handle)) 54733afdcc5SYongqiang Yang return PTR_ERR(handle); 54833afdcc5SYongqiang Yang 54933afdcc5SYongqiang Yang group = group_data[0].group; 55033afdcc5SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group++) { 55133afdcc5SYongqiang Yang unsigned long gdblocks; 55201f795f9SYongqiang Yang ext4_grpblk_t overhead; 55333afdcc5SYongqiang Yang 55433afdcc5SYongqiang Yang gdblocks = ext4_bg_num_gdb(sb, group); 55533afdcc5SYongqiang Yang start = ext4_group_first_block_no(sb, group); 55633afdcc5SYongqiang Yang 55701f795f9SYongqiang Yang if (meta_bg == 0 && !ext4_bg_has_super(sb, group)) 5586df935adSYongqiang Yang goto handle_itb; 5596df935adSYongqiang Yang 56001f795f9SYongqiang Yang if (meta_bg == 1) { 56101f795f9SYongqiang Yang ext4_group_t first_group; 56201f795f9SYongqiang Yang first_group = ext4_meta_bg_first_group(sb, group); 56301f795f9SYongqiang Yang if (first_group != group + 1 && 56401f795f9SYongqiang Yang first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1) 56501f795f9SYongqiang Yang goto handle_itb; 56601f795f9SYongqiang Yang } 56701f795f9SYongqiang Yang 56801f795f9SYongqiang Yang block = start + ext4_bg_has_super(sb, group); 56933afdcc5SYongqiang Yang /* Copy all of the GDT blocks into the backup in this group */ 57001f795f9SYongqiang Yang for (j = 0; j < gdblocks; j++, block++) { 57133afdcc5SYongqiang Yang struct buffer_head *gdb; 57233afdcc5SYongqiang Yang 57333afdcc5SYongqiang Yang ext4_debug("update backup group %#04llx\n", block); 57483448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 575a4130367SJan Kara if (err < 0) 57633afdcc5SYongqiang Yang goto out; 57733afdcc5SYongqiang Yang 57833afdcc5SYongqiang Yang gdb = sb_getblk(sb, block); 579aebf0243SWang Shilong if (unlikely(!gdb)) { 580860d21e2STheodore Ts'o err = -ENOMEM; 58133afdcc5SYongqiang Yang goto out; 58233afdcc5SYongqiang Yang } 58333afdcc5SYongqiang Yang 5845d601255Sliang xie BUFFER_TRACE(gdb, "get_write_access"); 585188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb, 586188c299eSJan Kara EXT4_JTR_NONE); 58733afdcc5SYongqiang Yang if (err) { 58833afdcc5SYongqiang Yang brelse(gdb); 58933afdcc5SYongqiang Yang goto out; 59033afdcc5SYongqiang Yang } 5911d0c3924STheodore Ts'o memcpy(gdb->b_data, sbi_array_rcu_deref(sbi, 5921d0c3924STheodore Ts'o s_group_desc, j)->b_data, gdb->b_size); 59333afdcc5SYongqiang Yang set_buffer_uptodate(gdb); 59433afdcc5SYongqiang Yang 59533afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb); 59633afdcc5SYongqiang Yang if (unlikely(err)) { 59733afdcc5SYongqiang Yang brelse(gdb); 59833afdcc5SYongqiang Yang goto out; 59933afdcc5SYongqiang Yang } 60033afdcc5SYongqiang Yang brelse(gdb); 60133afdcc5SYongqiang Yang } 60233afdcc5SYongqiang Yang 60333afdcc5SYongqiang Yang /* Zero out all of the reserved backup group descriptor 60433afdcc5SYongqiang Yang * table blocks 60533afdcc5SYongqiang Yang */ 60633afdcc5SYongqiang Yang if (ext4_bg_has_super(sb, group)) { 60733afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, gdblocks + start + 1, 60833afdcc5SYongqiang Yang reserved_gdb, GFP_NOFS); 60933afdcc5SYongqiang Yang if (err) 61033afdcc5SYongqiang Yang goto out; 61133afdcc5SYongqiang Yang } 61233afdcc5SYongqiang Yang 6136df935adSYongqiang Yang handle_itb: 61433afdcc5SYongqiang Yang /* Initialize group tables of the grop @group */ 61533afdcc5SYongqiang Yang if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED)) 61633afdcc5SYongqiang Yang goto handle_bb; 61733afdcc5SYongqiang Yang 61833afdcc5SYongqiang Yang /* Zero out all of the inode table blocks */ 61933afdcc5SYongqiang Yang block = group_data[i].inode_table; 62033afdcc5SYongqiang Yang ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 62133afdcc5SYongqiang Yang block, sbi->s_itb_per_group); 62233afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, 62333afdcc5SYongqiang Yang GFP_NOFS); 62433afdcc5SYongqiang Yang if (err) 62533afdcc5SYongqiang Yang goto out; 62633afdcc5SYongqiang Yang 62733afdcc5SYongqiang Yang handle_bb: 62833afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT) 62933afdcc5SYongqiang Yang goto handle_ib; 63033afdcc5SYongqiang Yang 63133afdcc5SYongqiang Yang /* Initialize block bitmap of the @group */ 63233afdcc5SYongqiang Yang block = group_data[i].block_bitmap; 63383448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 634a4130367SJan Kara if (err < 0) 63533afdcc5SYongqiang Yang goto out; 63633afdcc5SYongqiang Yang 63733afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 63833afdcc5SYongqiang Yang if (IS_ERR(bh)) { 63933afdcc5SYongqiang Yang err = PTR_ERR(bh); 64033afdcc5SYongqiang Yang goto out; 64133afdcc5SYongqiang Yang } 64201f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group); 64301f795f9SYongqiang Yang if (overhead != 0) { 64433afdcc5SYongqiang Yang ext4_debug("mark backup superblock %#04llx (+0)\n", 64533afdcc5SYongqiang Yang start); 646123e3016SRitesh Harjani mb_set_bits(bh->b_data, 0, 647d77147ffSharshads EXT4_NUM_B2C(sbi, overhead)); 64833afdcc5SYongqiang Yang } 649d77147ffSharshads ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count), 65033afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 65133afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6529e402893SVasily Averin brelse(bh); 65333afdcc5SYongqiang Yang if (err) 65433afdcc5SYongqiang Yang goto out; 65533afdcc5SYongqiang Yang 65633afdcc5SYongqiang Yang handle_ib: 65733afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_INODE_UNINIT) 65833afdcc5SYongqiang Yang continue; 65933afdcc5SYongqiang Yang 66033afdcc5SYongqiang Yang /* Initialize inode bitmap of the @group */ 66133afdcc5SYongqiang Yang block = group_data[i].inode_bitmap; 66283448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 663a4130367SJan Kara if (err < 0) 66433afdcc5SYongqiang Yang goto out; 66533afdcc5SYongqiang Yang /* Mark unused entries in inode bitmap used */ 66633afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 66733afdcc5SYongqiang Yang if (IS_ERR(bh)) { 66833afdcc5SYongqiang Yang err = PTR_ERR(bh); 66933afdcc5SYongqiang Yang goto out; 67033afdcc5SYongqiang Yang } 67133afdcc5SYongqiang Yang 67233afdcc5SYongqiang Yang ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 67333afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 67433afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6759e402893SVasily Averin brelse(bh); 67633afdcc5SYongqiang Yang if (err) 67733afdcc5SYongqiang Yang goto out; 67833afdcc5SYongqiang Yang } 67933afdcc5SYongqiang Yang 68033afdcc5SYongqiang Yang /* Mark group tables in block bitmap */ 68133afdcc5SYongqiang Yang for (j = 0; j < GROUP_TABLE_COUNT; j++) { 68233afdcc5SYongqiang Yang count = group_table_count[j]; 68333afdcc5SYongqiang Yang start = (&group_data[0].block_bitmap)[j]; 68433afdcc5SYongqiang Yang block = start; 68533afdcc5SYongqiang Yang for (i = 1; i < flex_gd->count; i++) { 68633afdcc5SYongqiang Yang block += group_table_count[j]; 68733afdcc5SYongqiang Yang if (block == (&group_data[i].block_bitmap)[j]) { 68833afdcc5SYongqiang Yang count += group_table_count[j]; 68933afdcc5SYongqiang Yang continue; 69033afdcc5SYongqiang Yang } 69133afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 692d77147ffSharshads flex_gd, 693d77147ffSharshads EXT4_B2C(sbi, start), 694d77147ffSharshads EXT4_B2C(sbi, 695d77147ffSharshads start + count 696d77147ffSharshads - 1)); 69733afdcc5SYongqiang Yang if (err) 69833afdcc5SYongqiang Yang goto out; 69933afdcc5SYongqiang Yang count = group_table_count[j]; 700b93c9535STheodore Ts'o start = (&group_data[i].block_bitmap)[j]; 70133afdcc5SYongqiang Yang block = start; 70233afdcc5SYongqiang Yang } 70333afdcc5SYongqiang Yang 70433afdcc5SYongqiang Yang if (count) { 70533afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 706d77147ffSharshads flex_gd, 707d77147ffSharshads EXT4_B2C(sbi, start), 708d77147ffSharshads EXT4_B2C(sbi, 709d77147ffSharshads start + count 710d77147ffSharshads - 1)); 71133afdcc5SYongqiang Yang if (err) 71233afdcc5SYongqiang Yang goto out; 71333afdcc5SYongqiang Yang } 71433afdcc5SYongqiang Yang } 71533afdcc5SYongqiang Yang 71633afdcc5SYongqiang Yang out: 71733afdcc5SYongqiang Yang err2 = ext4_journal_stop(handle); 71833afdcc5SYongqiang Yang if (err2 && !err) 71933afdcc5SYongqiang Yang err = err2; 72033afdcc5SYongqiang Yang 72133afdcc5SYongqiang Yang return err; 72233afdcc5SYongqiang Yang } 72333afdcc5SYongqiang Yang 72433afdcc5SYongqiang Yang /* 725ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 726617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 727ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 728ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 729ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 730ac27a0ecSDave Kleikamp */ 731bbc605cdSLukas Czerner unsigned int ext4_list_backups(struct super_block *sb, unsigned int *three, 732bbc605cdSLukas Czerner unsigned int *five, unsigned int *seven) 733ac27a0ecSDave Kleikamp { 734bbc605cdSLukas Czerner struct ext4_super_block *es = EXT4_SB(sb)->s_es; 735bbc605cdSLukas Czerner unsigned int *min = three; 736ac27a0ecSDave Kleikamp int mult = 3; 737bbc605cdSLukas Czerner unsigned int ret; 738bbc605cdSLukas Czerner 739bbc605cdSLukas Czerner if (ext4_has_feature_sparse_super2(sb)) { 740bbc605cdSLukas Czerner do { 741bbc605cdSLukas Czerner if (*min > 2) 742bbc605cdSLukas Czerner return UINT_MAX; 743bbc605cdSLukas Czerner ret = le32_to_cpu(es->s_backup_bgs[*min - 1]); 744bbc605cdSLukas Czerner *min += 1; 745bbc605cdSLukas Czerner } while (!ret); 746bbc605cdSLukas Czerner return ret; 747bbc605cdSLukas Czerner } 748ac27a0ecSDave Kleikamp 749e2b911c5SDarrick J. Wong if (!ext4_has_feature_sparse_super(sb)) { 750ac27a0ecSDave Kleikamp ret = *min; 751ac27a0ecSDave Kleikamp *min += 1; 752ac27a0ecSDave Kleikamp return ret; 753ac27a0ecSDave Kleikamp } 754ac27a0ecSDave Kleikamp 755ac27a0ecSDave Kleikamp if (*five < *min) { 756ac27a0ecSDave Kleikamp min = five; 757ac27a0ecSDave Kleikamp mult = 5; 758ac27a0ecSDave Kleikamp } 759ac27a0ecSDave Kleikamp if (*seven < *min) { 760ac27a0ecSDave Kleikamp min = seven; 761ac27a0ecSDave Kleikamp mult = 7; 762ac27a0ecSDave Kleikamp } 763ac27a0ecSDave Kleikamp 764ac27a0ecSDave Kleikamp ret = *min; 765ac27a0ecSDave Kleikamp *min *= mult; 766ac27a0ecSDave Kleikamp 767ac27a0ecSDave Kleikamp return ret; 768ac27a0ecSDave Kleikamp } 769ac27a0ecSDave Kleikamp 770ac27a0ecSDave Kleikamp /* 771ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 772ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 773ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 774ac27a0ecSDave Kleikamp */ 775ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 776c72df9f9SYongqiang Yang ext4_group_t end, 777ac27a0ecSDave Kleikamp struct buffer_head *primary) 778ac27a0ecSDave Kleikamp { 779617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 780ac27a0ecSDave Kleikamp unsigned three = 1; 781ac27a0ecSDave Kleikamp unsigned five = 5; 782ac27a0ecSDave Kleikamp unsigned seven = 7; 783ac27a0ecSDave Kleikamp unsigned grp; 784ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 785ac27a0ecSDave Kleikamp int gdbackups = 0; 786ac27a0ecSDave Kleikamp 787617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 788bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 789bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 79012062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 7912ae02107SMingming Cao " missing grp %d (%llu)", 792ac27a0ecSDave Kleikamp blk, grp, 793bd81d8eeSLaurent Vivier grp * 794bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 795bd81d8eeSLaurent Vivier blk); 796ac27a0ecSDave Kleikamp return -EINVAL; 797ac27a0ecSDave Kleikamp } 798617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 799ac27a0ecSDave Kleikamp return -EFBIG; 800ac27a0ecSDave Kleikamp } 801ac27a0ecSDave Kleikamp 802ac27a0ecSDave Kleikamp return gdbackups; 803ac27a0ecSDave Kleikamp } 804ac27a0ecSDave Kleikamp 805ac27a0ecSDave Kleikamp /* 806ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 807ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 808ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 809ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 810ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 811ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 812ac27a0ecSDave Kleikamp * 813ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 814ac27a0ecSDave Kleikamp * 815ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 816ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 817ac27a0ecSDave Kleikamp */ 818ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 8192f919710SYongqiang Yang ext4_group_t group) 820ac27a0ecSDave Kleikamp { 821ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 822617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 8232f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 824617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 825fb265c9cSTheodore Ts'o struct buffer_head **o_group_desc, **n_group_desc = NULL; 826fb265c9cSTheodore Ts'o struct buffer_head *dind = NULL; 827fb265c9cSTheodore Ts'o struct buffer_head *gdb_bh = NULL; 828ac27a0ecSDave Kleikamp int gdbackups; 829fb265c9cSTheodore Ts'o struct ext4_iloc iloc = { .bh = NULL }; 830ac27a0ecSDave Kleikamp __le32 *data; 831ac27a0ecSDave Kleikamp int err; 832ac27a0ecSDave Kleikamp 833ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 834ac27a0ecSDave Kleikamp printk(KERN_DEBUG 835617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 836ac27a0ecSDave Kleikamp gdb_num); 837ac27a0ecSDave Kleikamp 838fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 839fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 840fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 841ac27a0ecSDave Kleikamp 842c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, gdb_bh); 8432f919710SYongqiang Yang if (gdbackups < 0) { 844ac27a0ecSDave Kleikamp err = gdbackups; 845fb265c9cSTheodore Ts'o goto errout; 846ac27a0ecSDave Kleikamp } 847ac27a0ecSDave Kleikamp 848617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 849fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 850fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 851fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 852fb265c9cSTheodore Ts'o dind = NULL; 853fb265c9cSTheodore Ts'o goto errout; 854ac27a0ecSDave Kleikamp } 855ac27a0ecSDave Kleikamp 856ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 857617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 85812062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 8592f919710SYongqiang Yang group, gdblock); 860ac27a0ecSDave Kleikamp err = -EINVAL; 861fb265c9cSTheodore Ts'o goto errout; 862ac27a0ecSDave Kleikamp } 863ac27a0ecSDave Kleikamp 8645d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 865188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 866188c299eSJan Kara EXT4_JTR_NONE); 867b4097142STheodore Ts'o if (unlikely(err)) 868fb265c9cSTheodore Ts'o goto errout; 869ac27a0ecSDave Kleikamp 8705d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 871188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE); 872b4097142STheodore Ts'o if (unlikely(err)) 873fb265c9cSTheodore Ts'o goto errout; 874ac27a0ecSDave Kleikamp 8755d601255Sliang xie BUFFER_TRACE(dind, "get_write_access"); 876188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, dind, EXT4_JTR_NONE); 877c9e87161SDinghao Liu if (unlikely(err)) { 878b4097142STheodore Ts'o ext4_std_error(sb, err); 879c9e87161SDinghao Liu goto errout; 880c9e87161SDinghao Liu } 881ac27a0ecSDave Kleikamp 882617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 883b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 884b4097142STheodore Ts'o if (unlikely(err)) 885fb265c9cSTheodore Ts'o goto errout; 886ac27a0ecSDave Kleikamp 88771b565ceSTheodore Ts'o n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 88871b565ceSTheodore Ts'o GFP_KERNEL); 889ac27a0ecSDave Kleikamp if (!n_group_desc) { 890ac27a0ecSDave Kleikamp err = -ENOMEM; 891f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 892f18a5f21STheodore Ts'o gdb_num + 1); 893fb265c9cSTheodore Ts'o goto errout; 894ac27a0ecSDave Kleikamp } 895ac27a0ecSDave Kleikamp 896ac27a0ecSDave Kleikamp /* 897ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 898ac27a0ecSDave Kleikamp * 899ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 900ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 901ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 902ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 903ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 904ac27a0ecSDave Kleikamp */ 905617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 906b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 907b4097142STheodore Ts'o if (unlikely(err)) { 908b4097142STheodore Ts'o ext4_std_error(sb, err); 909fb265c9cSTheodore Ts'o goto errout; 910b4097142STheodore Ts'o } 911d77147ffSharshads inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 912d77147ffSharshads (9 - EXT4_SB(sb)->s_cluster_bits); 913617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 9142f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 9152f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 916b4097142STheodore Ts'o if (unlikely(err)) { 917b4097142STheodore Ts'o ext4_std_error(sb, err); 9188c380ab4SPan Bian iloc.bh = NULL; 919fb265c9cSTheodore Ts'o goto errout; 920b4097142STheodore Ts'o } 921b4097142STheodore Ts'o brelse(dind); 922ac27a0ecSDave Kleikamp 9231d0c3924STheodore Ts'o rcu_read_lock(); 9241d0c3924STheodore Ts'o o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc); 925ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 926617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 9271d0c3924STheodore Ts'o rcu_read_unlock(); 9282f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 9291d0c3924STheodore Ts'o rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc); 930617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 9311d0c3924STheodore Ts'o ext4_kvfree_array_rcu(o_group_desc); 932ac27a0ecSDave Kleikamp 93305c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 934e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 93505c2c00fSJan Kara ext4_superblock_csum_set(sb); 93605c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 937a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 938b4097142STheodore Ts'o if (err) 939b4097142STheodore Ts'o ext4_std_error(sb, err); 940b4097142STheodore Ts'o return err; 941fb265c9cSTheodore Ts'o errout: 942b93b41d4SAl Viro kvfree(n_group_desc); 943ac27a0ecSDave Kleikamp brelse(iloc.bh); 944ac27a0ecSDave Kleikamp brelse(dind); 9452f919710SYongqiang Yang brelse(gdb_bh); 946ac27a0ecSDave Kleikamp 947617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 948ac27a0ecSDave Kleikamp return err; 949ac27a0ecSDave Kleikamp } 950ac27a0ecSDave Kleikamp 951ac27a0ecSDave Kleikamp /* 95201f795f9SYongqiang Yang * add_new_gdb_meta_bg is the sister of add_new_gdb. 95301f795f9SYongqiang Yang */ 95401f795f9SYongqiang Yang static int add_new_gdb_meta_bg(struct super_block *sb, 95501f795f9SYongqiang Yang handle_t *handle, ext4_group_t group) { 95601f795f9SYongqiang Yang ext4_fsblk_t gdblock; 95701f795f9SYongqiang Yang struct buffer_head *gdb_bh; 95801f795f9SYongqiang Yang struct buffer_head **o_group_desc, **n_group_desc; 95901f795f9SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 96001f795f9SYongqiang Yang int err; 96101f795f9SYongqiang Yang 96201f795f9SYongqiang Yang gdblock = ext4_meta_bg_first_block_no(sb, group) + 96301f795f9SYongqiang Yang ext4_bg_has_super(sb, group); 964fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 965fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 966fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 96771b565ceSTheodore Ts'o n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 96871b565ceSTheodore Ts'o GFP_KERNEL); 96901f795f9SYongqiang Yang if (!n_group_desc) { 97061a9c11eSVasily Averin brelse(gdb_bh); 97101f795f9SYongqiang Yang err = -ENOMEM; 97201f795f9SYongqiang Yang ext4_warning(sb, "not enough memory for %lu groups", 97301f795f9SYongqiang Yang gdb_num + 1); 97401f795f9SYongqiang Yang return err; 97501f795f9SYongqiang Yang } 97601f795f9SYongqiang Yang 9771d0c3924STheodore Ts'o rcu_read_lock(); 9781d0c3924STheodore Ts'o o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc); 97901f795f9SYongqiang Yang memcpy(n_group_desc, o_group_desc, 98001f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 9811d0c3924STheodore Ts'o rcu_read_unlock(); 98201f795f9SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 983d64264d6SLukas Czerner 984d64264d6SLukas Czerner BUFFER_TRACE(gdb_bh, "get_write_access"); 985188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb_bh, EXT4_JTR_NONE); 986d64264d6SLukas Czerner if (err) { 987d64264d6SLukas Czerner kvfree(n_group_desc); 988d64264d6SLukas Czerner brelse(gdb_bh); 989d64264d6SLukas Czerner return err; 990d64264d6SLukas Czerner } 991d64264d6SLukas Czerner 9921d0c3924STheodore Ts'o rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc); 99301f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count++; 9941d0c3924STheodore Ts'o ext4_kvfree_array_rcu(o_group_desc); 99501f795f9SYongqiang Yang return err; 99601f795f9SYongqiang Yang } 99701f795f9SYongqiang Yang 99801f795f9SYongqiang Yang /* 999ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 1000ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 1001ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 1002ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 1003ac27a0ecSDave Kleikamp * 1004ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 1005ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 1006ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 1007ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 1008ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 1009ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 1010ac27a0ecSDave Kleikamp */ 1011ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 1012668f4dc5SYongqiang Yang ext4_group_t group) 1013ac27a0ecSDave Kleikamp { 1014ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 1015617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 1016d77147ffSharshads int cluster_bits = EXT4_SB(sb)->s_cluster_bits; 1017ac27a0ecSDave Kleikamp struct buffer_head **primary; 1018ac27a0ecSDave Kleikamp struct buffer_head *dind; 1019617ba13bSMingming Cao struct ext4_iloc iloc; 1020617ba13bSMingming Cao ext4_fsblk_t blk; 1021ac27a0ecSDave Kleikamp __le32 *data, *end; 1022ac27a0ecSDave Kleikamp int gdbackups = 0; 1023ac27a0ecSDave Kleikamp int res, i; 1024ac27a0ecSDave Kleikamp int err; 1025ac27a0ecSDave Kleikamp 10266da2ec56SKees Cook primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS); 1027ac27a0ecSDave Kleikamp if (!primary) 1028ac27a0ecSDave Kleikamp return -ENOMEM; 1029ac27a0ecSDave Kleikamp 1030617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 1031fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 1032fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 1033fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 1034fb265c9cSTheodore Ts'o dind = NULL; 1035ac27a0ecSDave Kleikamp goto exit_free; 1036ac27a0ecSDave Kleikamp } 1037ac27a0ecSDave Kleikamp 1038617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 103994460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 104094460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 1041617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 1042ac27a0ecSDave Kleikamp 1043ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 1044ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 1045ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 104612062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 1047ac27a0ecSDave Kleikamp " not at offset %ld", 1048ac27a0ecSDave Kleikamp blk, 1049ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 1050ac27a0ecSDave Kleikamp err = -EINVAL; 1051ac27a0ecSDave Kleikamp goto exit_bh; 1052ac27a0ecSDave Kleikamp } 1053fb265c9cSTheodore Ts'o primary[res] = ext4_sb_bread(sb, blk, 0); 1054fb265c9cSTheodore Ts'o if (IS_ERR(primary[res])) { 1055fb265c9cSTheodore Ts'o err = PTR_ERR(primary[res]); 1056fb265c9cSTheodore Ts'o primary[res] = NULL; 1057ac27a0ecSDave Kleikamp goto exit_bh; 1058ac27a0ecSDave Kleikamp } 1059c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, primary[res]); 1060c72df9f9SYongqiang Yang if (gdbackups < 0) { 1061ac27a0ecSDave Kleikamp brelse(primary[res]); 1062ac27a0ecSDave Kleikamp err = gdbackups; 1063ac27a0ecSDave Kleikamp goto exit_bh; 1064ac27a0ecSDave Kleikamp } 1065ac27a0ecSDave Kleikamp if (++data >= end) 1066ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 1067ac27a0ecSDave Kleikamp } 1068ac27a0ecSDave Kleikamp 1069ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 10705d601255Sliang xie BUFFER_TRACE(primary[i], "get_write_access"); 1071188c299eSJan Kara if ((err = ext4_journal_get_write_access(handle, sb, primary[i], 1072188c299eSJan Kara EXT4_JTR_NONE))) 1073ac27a0ecSDave Kleikamp goto exit_bh; 1074ac27a0ecSDave Kleikamp } 1075ac27a0ecSDave Kleikamp 1076617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 1077ac27a0ecSDave Kleikamp goto exit_bh; 1078ac27a0ecSDave Kleikamp 1079ac27a0ecSDave Kleikamp /* 1080ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 1081ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 1082ac27a0ecSDave Kleikamp */ 1083668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 1084ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 1085ac27a0ecSDave Kleikamp int err2; 1086ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 1087ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 1088ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 1089ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 1090ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 10910390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 1092ac27a0ecSDave Kleikamp if (!err) 1093ac27a0ecSDave Kleikamp err = err2; 1094ac27a0ecSDave Kleikamp } 1095d77147ffSharshads 1096d77147ffSharshads inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits); 1097617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 1098ac27a0ecSDave Kleikamp 1099ac27a0ecSDave Kleikamp exit_bh: 1100ac27a0ecSDave Kleikamp while (--res >= 0) 1101ac27a0ecSDave Kleikamp brelse(primary[res]); 1102ac27a0ecSDave Kleikamp brelse(dind); 1103ac27a0ecSDave Kleikamp 1104ac27a0ecSDave Kleikamp exit_free: 1105ac27a0ecSDave Kleikamp kfree(primary); 1106ac27a0ecSDave Kleikamp 1107ac27a0ecSDave Kleikamp return err; 1108ac27a0ecSDave Kleikamp } 1109ac27a0ecSDave Kleikamp 1110ac27a0ecSDave Kleikamp /* 1111617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 1112ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 1113ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 1114ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 1115ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 1116ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 1117ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 1118ac27a0ecSDave Kleikamp * 111932ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 112032ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 112132ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 112232ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 112332ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 112432ed5058STheodore Ts'o * which changed s_groups_count will backup again. 1125ac27a0ecSDave Kleikamp */ 1126904dad47SAndy Leiserson static void update_backups(struct super_block *sb, sector_t blk_off, char *data, 112701f795f9SYongqiang Yang int size, int meta_bg) 1128ac27a0ecSDave Kleikamp { 1129617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 113001f795f9SYongqiang Yang ext4_group_t last; 1131617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 1132ac27a0ecSDave Kleikamp unsigned three = 1; 1133ac27a0ecSDave Kleikamp unsigned five = 5; 1134ac27a0ecSDave Kleikamp unsigned seven = 7; 113501f795f9SYongqiang Yang ext4_group_t group = 0; 1136ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 1137ac27a0ecSDave Kleikamp handle_t *handle; 1138ac27a0ecSDave Kleikamp int err = 0, err2; 1139ac27a0ecSDave Kleikamp 11409924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 1141ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1142ac27a0ecSDave Kleikamp group = 1; 1143ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 1144ac27a0ecSDave Kleikamp goto exit_err; 1145ac27a0ecSDave Kleikamp } 1146ac27a0ecSDave Kleikamp 114701f795f9SYongqiang Yang if (meta_bg == 0) { 114801f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 114901f795f9SYongqiang Yang last = sbi->s_groups_count; 115001f795f9SYongqiang Yang } else { 1151904dad47SAndy Leiserson group = ext4_get_group_number(sb, blk_off) + 1; 115201f795f9SYongqiang Yang last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2); 115301f795f9SYongqiang Yang } 115401f795f9SYongqiang Yang 115501f795f9SYongqiang Yang while (group < sbi->s_groups_count) { 1156ac27a0ecSDave Kleikamp struct buffer_head *bh; 115701f795f9SYongqiang Yang ext4_fsblk_t backup_block; 1158ac27a0ecSDave Kleikamp 1159ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 116083448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 1161a4130367SJan Kara if (err < 0) 1162ac27a0ecSDave Kleikamp break; 1163ac27a0ecSDave Kleikamp 116401f795f9SYongqiang Yang if (meta_bg == 0) 11659378c676SJan Kara backup_block = ((ext4_fsblk_t)group) * bpg + blk_off; 116601f795f9SYongqiang Yang else 116701f795f9SYongqiang Yang backup_block = (ext4_group_first_block_no(sb, group) + 116801f795f9SYongqiang Yang ext4_bg_has_super(sb, group)); 116901f795f9SYongqiang Yang 117001f795f9SYongqiang Yang bh = sb_getblk(sb, backup_block); 1171aebf0243SWang Shilong if (unlikely(!bh)) { 1172860d21e2STheodore Ts'o err = -ENOMEM; 1173ac27a0ecSDave Kleikamp break; 1174ac27a0ecSDave Kleikamp } 117501f795f9SYongqiang Yang ext4_debug("update metadata backup %llu(+%llu)\n", 117601f795f9SYongqiang Yang backup_block, backup_block - 117701f795f9SYongqiang Yang ext4_group_first_block_no(sb, group)); 11785d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 1179188c299eSJan Kara if ((err = ext4_journal_get_write_access(handle, sb, bh, 1180188c299eSJan Kara EXT4_JTR_NONE))) 1181ac27a0ecSDave Kleikamp break; 1182ac27a0ecSDave Kleikamp lock_buffer(bh); 1183ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 1184ac27a0ecSDave Kleikamp if (rest) 1185ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 1186ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1187ac27a0ecSDave Kleikamp unlock_buffer(bh); 1188b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 1189b4097142STheodore Ts'o if (unlikely(err)) 1190b4097142STheodore Ts'o ext4_std_error(sb, err); 1191ac27a0ecSDave Kleikamp brelse(bh); 119201f795f9SYongqiang Yang 119301f795f9SYongqiang Yang if (meta_bg == 0) 119401f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 119501f795f9SYongqiang Yang else if (group == last) 119601f795f9SYongqiang Yang break; 119701f795f9SYongqiang Yang else 119801f795f9SYongqiang Yang group = last; 1199ac27a0ecSDave Kleikamp } 1200617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 1201ac27a0ecSDave Kleikamp err = err2; 1202ac27a0ecSDave Kleikamp 1203ac27a0ecSDave Kleikamp /* 1204ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 1205ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 1206ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 1207ac27a0ecSDave Kleikamp * 1208ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 1209ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 1210ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 1211ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 1212ac27a0ecSDave Kleikamp */ 1213ac27a0ecSDave Kleikamp exit_err: 1214ac27a0ecSDave Kleikamp if (err) { 121512062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 1216ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 1217617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 1218617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 1219ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 1220ac27a0ecSDave Kleikamp } 1221ac27a0ecSDave Kleikamp } 1222ac27a0ecSDave Kleikamp 1223bb08c1e7SYongqiang Yang /* 1224bb08c1e7SYongqiang Yang * ext4_add_new_descs() adds @count group descriptor of groups 1225bb08c1e7SYongqiang Yang * starting at @group 1226bb08c1e7SYongqiang Yang * 1227bb08c1e7SYongqiang Yang * @handle: journal handle 1228bb08c1e7SYongqiang Yang * @sb: super block 1229bb08c1e7SYongqiang Yang * @group: the group no. of the first group desc to be added 1230bb08c1e7SYongqiang Yang * @resize_inode: the resize inode 1231bb08c1e7SYongqiang Yang * @count: number of group descriptors to be added 1232bb08c1e7SYongqiang Yang */ 1233bb08c1e7SYongqiang Yang static int ext4_add_new_descs(handle_t *handle, struct super_block *sb, 1234bb08c1e7SYongqiang Yang ext4_group_t group, struct inode *resize_inode, 1235bb08c1e7SYongqiang Yang ext4_group_t count) 1236bb08c1e7SYongqiang Yang { 1237bb08c1e7SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1238bb08c1e7SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 1239bb08c1e7SYongqiang Yang struct buffer_head *gdb_bh; 1240bb08c1e7SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 124101f795f9SYongqiang Yang int meta_bg; 1242bb08c1e7SYongqiang Yang 1243e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 1244bb08c1e7SYongqiang Yang for (i = 0; i < count; i++, group++) { 1245bb08c1e7SYongqiang Yang int reserved_gdb = ext4_bg_has_super(sb, group) ? 1246bb08c1e7SYongqiang Yang le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1247bb08c1e7SYongqiang Yang 1248bb08c1e7SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1249bb08c1e7SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1250bb08c1e7SYongqiang Yang 1251bb08c1e7SYongqiang Yang /* 1252bb08c1e7SYongqiang Yang * We will only either add reserved group blocks to a backup group 1253bb08c1e7SYongqiang Yang * or remove reserved blocks for the first group in a new group block. 1254bb08c1e7SYongqiang Yang * Doing both would be mean more complex code, and sane people don't 1255bb08c1e7SYongqiang Yang * use non-sparse filesystems anymore. This is already checked above. 1256bb08c1e7SYongqiang Yang */ 1257bb08c1e7SYongqiang Yang if (gdb_off) { 12581d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 12591d0c3924STheodore Ts'o gdb_num); 12605d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 1261188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdb_bh, 1262188c299eSJan Kara EXT4_JTR_NONE); 1263bb08c1e7SYongqiang Yang 1264bb08c1e7SYongqiang Yang if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 1265bb08c1e7SYongqiang Yang err = reserve_backup_gdb(handle, resize_inode, group); 126601f795f9SYongqiang Yang } else if (meta_bg != 0) { 126701f795f9SYongqiang Yang err = add_new_gdb_meta_bg(sb, handle, group); 126801f795f9SYongqiang Yang } else { 1269bb08c1e7SYongqiang Yang err = add_new_gdb(handle, resize_inode, group); 127001f795f9SYongqiang Yang } 1271bb08c1e7SYongqiang Yang if (err) 1272bb08c1e7SYongqiang Yang break; 1273bb08c1e7SYongqiang Yang } 1274bb08c1e7SYongqiang Yang return err; 1275bb08c1e7SYongqiang Yang } 1276bb08c1e7SYongqiang Yang 127741a246d1SDarrick J. Wong static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block) 127841a246d1SDarrick J. Wong { 127941a246d1SDarrick J. Wong struct buffer_head *bh = sb_getblk(sb, block); 1280aebf0243SWang Shilong if (unlikely(!bh)) 128141a246d1SDarrick J. Wong return NULL; 12827f1468d1SDmitry Monakhov if (!bh_uptodate_or_lock(bh)) { 12832d069c08Szhangyi (F) if (ext4_read_bh(bh, 0, NULL) < 0) { 128441a246d1SDarrick J. Wong brelse(bh); 128541a246d1SDarrick J. Wong return NULL; 128641a246d1SDarrick J. Wong } 12877f1468d1SDmitry Monakhov } 128841a246d1SDarrick J. Wong 128941a246d1SDarrick J. Wong return bh; 129041a246d1SDarrick J. Wong } 129141a246d1SDarrick J. Wong 129241a246d1SDarrick J. Wong static int ext4_set_bitmap_checksums(struct super_block *sb, 129341a246d1SDarrick J. Wong ext4_group_t group, 129441a246d1SDarrick J. Wong struct ext4_group_desc *gdp, 129541a246d1SDarrick J. Wong struct ext4_new_group_data *group_data) 129641a246d1SDarrick J. Wong { 129741a246d1SDarrick J. Wong struct buffer_head *bh; 129841a246d1SDarrick J. Wong 12999aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 130041a246d1SDarrick J. Wong return 0; 130141a246d1SDarrick J. Wong 130241a246d1SDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->inode_bitmap); 130341a246d1SDarrick J. Wong if (!bh) 130441a246d1SDarrick J. Wong return -EIO; 130541a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, bh, 130641a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 130741a246d1SDarrick J. Wong brelse(bh); 130841a246d1SDarrick J. Wong 1309fa77dcfaSDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->block_bitmap); 1310fa77dcfaSDarrick J. Wong if (!bh) 1311fa77dcfaSDarrick J. Wong return -EIO; 131279f1ba49STao Ma ext4_block_bitmap_csum_set(sb, group, gdp, bh); 1313fa77dcfaSDarrick J. Wong brelse(bh); 1314fa77dcfaSDarrick J. Wong 131541a246d1SDarrick J. Wong return 0; 131641a246d1SDarrick J. Wong } 131741a246d1SDarrick J. Wong 1318083f5b24SYongqiang Yang /* 1319083f5b24SYongqiang Yang * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg 1320083f5b24SYongqiang Yang */ 1321083f5b24SYongqiang Yang static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb, 1322083f5b24SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 1323083f5b24SYongqiang Yang { 1324083f5b24SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 1325083f5b24SYongqiang Yang struct ext4_group_desc *gdp; 1326083f5b24SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1327083f5b24SYongqiang Yang struct buffer_head *gdb_bh; 1328083f5b24SYongqiang Yang ext4_group_t group; 1329083f5b24SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 1330083f5b24SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 1331083f5b24SYongqiang Yang 1332083f5b24SYongqiang Yang 1333083f5b24SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) { 1334083f5b24SYongqiang Yang group = group_data->group; 1335083f5b24SYongqiang Yang 1336083f5b24SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1337083f5b24SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1338083f5b24SYongqiang Yang 1339083f5b24SYongqiang Yang /* 1340083f5b24SYongqiang Yang * get_write_access() has been called on gdb_bh by ext4_add_new_desc(). 1341083f5b24SYongqiang Yang */ 13421d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num); 1343083f5b24SYongqiang Yang /* Update group descriptor block for new group */ 13442716b802STheodore Ts'o gdp = (struct ext4_group_desc *)(gdb_bh->b_data + 1345083f5b24SYongqiang Yang gdb_off * EXT4_DESC_SIZE(sb)); 1346083f5b24SYongqiang Yang 1347083f5b24SYongqiang Yang memset(gdp, 0, EXT4_DESC_SIZE(sb)); 1348083f5b24SYongqiang Yang ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap); 1349083f5b24SYongqiang Yang ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap); 135041a246d1SDarrick J. Wong err = ext4_set_bitmap_checksums(sb, group, gdp, group_data); 135141a246d1SDarrick J. Wong if (err) { 135241a246d1SDarrick J. Wong ext4_std_error(sb, err); 135341a246d1SDarrick J. Wong break; 135441a246d1SDarrick J. Wong } 135541a246d1SDarrick J. Wong 1356083f5b24SYongqiang Yang ext4_inode_table_set(sb, gdp, group_data->inode_table); 1357083f5b24SYongqiang Yang ext4_free_group_clusters_set(sb, gdp, 1358d77147ffSharshads group_data->free_clusters_count); 1359083f5b24SYongqiang Yang ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 136093f90526STheodore Ts'o if (ext4_has_group_desc_csum(sb)) 136193f90526STheodore Ts'o ext4_itable_unused_set(sb, gdp, 136293f90526STheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 1363083f5b24SYongqiang Yang gdp->bg_flags = cpu_to_le16(*bg_flags); 1364feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1365083f5b24SYongqiang Yang 1366083f5b24SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 1367083f5b24SYongqiang Yang if (unlikely(err)) { 1368083f5b24SYongqiang Yang ext4_std_error(sb, err); 1369083f5b24SYongqiang Yang break; 1370083f5b24SYongqiang Yang } 1371083f5b24SYongqiang Yang 1372083f5b24SYongqiang Yang /* 1373083f5b24SYongqiang Yang * We can allocate memory for mb_alloc based on the new group 1374083f5b24SYongqiang Yang * descriptor 1375083f5b24SYongqiang Yang */ 1376083f5b24SYongqiang Yang err = ext4_mb_add_groupinfo(sb, group, gdp); 1377083f5b24SYongqiang Yang if (err) 1378083f5b24SYongqiang Yang break; 1379083f5b24SYongqiang Yang } 1380083f5b24SYongqiang Yang return err; 1381083f5b24SYongqiang Yang } 1382083f5b24SYongqiang Yang 13832e10e2f2SYongqiang Yang /* 13842e10e2f2SYongqiang Yang * ext4_update_super() updates the super block so that the newly added 13852e10e2f2SYongqiang Yang * groups can be seen by the filesystem. 13862e10e2f2SYongqiang Yang * 13872e10e2f2SYongqiang Yang * @sb: super block 13882e10e2f2SYongqiang Yang * @flex_gd: new added groups 13892e10e2f2SYongqiang Yang */ 13902e10e2f2SYongqiang Yang static void ext4_update_super(struct super_block *sb, 13912e10e2f2SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 13922e10e2f2SYongqiang Yang { 13932e10e2f2SYongqiang Yang ext4_fsblk_t blocks_count = 0; 13942e10e2f2SYongqiang Yang ext4_fsblk_t free_blocks = 0; 13952e10e2f2SYongqiang Yang ext4_fsblk_t reserved_blocks = 0; 13962e10e2f2SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 13972e10e2f2SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 13982e10e2f2SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 13998a991849STheodore Ts'o int i; 14002e10e2f2SYongqiang Yang 14012e10e2f2SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 14022e10e2f2SYongqiang Yang /* 14032e10e2f2SYongqiang Yang * Make the new blocks and inodes valid next. We do this before 14042e10e2f2SYongqiang Yang * increasing the group count so that once the group is enabled, 14052e10e2f2SYongqiang Yang * all of its blocks and inodes are already valid. 14062e10e2f2SYongqiang Yang * 14072e10e2f2SYongqiang Yang * We always allocate group-by-group, then block-by-block or 14082e10e2f2SYongqiang Yang * inode-by-inode within a group, so enabling these 14092e10e2f2SYongqiang Yang * blocks/inodes before the group is live won't actually let us 14102e10e2f2SYongqiang Yang * allocate the new space yet. 14112e10e2f2SYongqiang Yang */ 14122e10e2f2SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 14132e10e2f2SYongqiang Yang blocks_count += group_data[i].blocks_count; 1414d77147ffSharshads free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count); 14152e10e2f2SYongqiang Yang } 14162e10e2f2SYongqiang Yang 14172e10e2f2SYongqiang Yang reserved_blocks = ext4_r_blocks_count(es) * 100; 141801f795f9SYongqiang Yang reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es)); 14192e10e2f2SYongqiang Yang reserved_blocks *= blocks_count; 14202e10e2f2SYongqiang Yang do_div(reserved_blocks, 100); 14212e10e2f2SYongqiang Yang 142205c2c00fSJan Kara lock_buffer(sbi->s_sbh); 14232e10e2f2SYongqiang Yang ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count); 1424636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks); 14252e10e2f2SYongqiang Yang le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) * 14262e10e2f2SYongqiang Yang flex_gd->count); 1427636d7e2eSDarrick J. Wong le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) * 1428636d7e2eSDarrick J. Wong flex_gd->count); 14292e10e2f2SYongqiang Yang 143001f795f9SYongqiang Yang ext4_debug("free blocks count %llu", ext4_free_blocks_count(es)); 14312e10e2f2SYongqiang Yang /* 14322e10e2f2SYongqiang Yang * We need to protect s_groups_count against other CPUs seeing 14332e10e2f2SYongqiang Yang * inconsistent state in the superblock. 14342e10e2f2SYongqiang Yang * 14352e10e2f2SYongqiang Yang * The precise rules we use are: 14362e10e2f2SYongqiang Yang * 14372e10e2f2SYongqiang Yang * * Writers must perform a smp_wmb() after updating all 14382e10e2f2SYongqiang Yang * dependent data and before modifying the groups count 14392e10e2f2SYongqiang Yang * 14402e10e2f2SYongqiang Yang * * Readers must perform an smp_rmb() after reading the groups 14412e10e2f2SYongqiang Yang * count and before reading any dependent data. 14422e10e2f2SYongqiang Yang * 14432e10e2f2SYongqiang Yang * NB. These rules can be relaxed when checking the group count 14442e10e2f2SYongqiang Yang * while freeing data, as we can only allocate from a block 14452e10e2f2SYongqiang Yang * group after serialising against the group count, and we can 14462e10e2f2SYongqiang Yang * only then free after serialising in turn against that 14472e10e2f2SYongqiang Yang * allocation. 14482e10e2f2SYongqiang Yang */ 14492e10e2f2SYongqiang Yang smp_wmb(); 14502e10e2f2SYongqiang Yang 14512e10e2f2SYongqiang Yang /* Update the global fs size fields */ 14522e10e2f2SYongqiang Yang sbi->s_groups_count += flex_gd->count; 1453c5c72d81STheodore Ts'o sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 1454c5c72d81STheodore Ts'o (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 14552e10e2f2SYongqiang Yang 14562e10e2f2SYongqiang Yang /* Update the reserved block counts only once the new group is 14572e10e2f2SYongqiang Yang * active. */ 14582e10e2f2SYongqiang Yang ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 14592e10e2f2SYongqiang Yang reserved_blocks); 146005c2c00fSJan Kara ext4_superblock_csum_set(sb); 146105c2c00fSJan Kara unlock_buffer(sbi->s_sbh); 14622e10e2f2SYongqiang Yang 14632e10e2f2SYongqiang Yang /* Update the free space counts */ 14642e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeclusters_counter, 1465810da240SLukas Czerner EXT4_NUM_B2C(sbi, free_blocks)); 14662e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeinodes_counter, 14672e10e2f2SYongqiang Yang EXT4_INODES_PER_GROUP(sb) * flex_gd->count); 14682e10e2f2SYongqiang Yang 146901f795f9SYongqiang Yang ext4_debug("free blocks count %llu", 147001f795f9SYongqiang Yang percpu_counter_read(&sbi->s_freeclusters_counter)); 1471e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) { 14722e10e2f2SYongqiang Yang ext4_group_t flex_group; 14737c990728SSuraj Jitindar Singh struct flex_groups *fg; 14747c990728SSuraj Jitindar Singh 14752e10e2f2SYongqiang Yang flex_group = ext4_flex_group(sbi, group_data[0].group); 14767c990728SSuraj Jitindar Singh fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group); 147790ba983fSTheodore Ts'o atomic64_add(EXT4_NUM_B2C(sbi, free_blocks), 14787c990728SSuraj Jitindar Singh &fg->free_clusters); 14792e10e2f2SYongqiang Yang atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count, 14807c990728SSuraj Jitindar Singh &fg->free_inodes); 14812e10e2f2SYongqiang Yang } 14822e10e2f2SYongqiang Yang 1483952fc18eSTheodore Ts'o /* 1484952fc18eSTheodore Ts'o * Update the fs overhead information 1485952fc18eSTheodore Ts'o */ 1486952fc18eSTheodore Ts'o ext4_calculate_overhead(sb); 1487*de394a86STheodore Ts'o es->s_overhead_clusters = cpu_to_le32(sbi->s_overhead); 1488952fc18eSTheodore Ts'o 14892e10e2f2SYongqiang Yang if (test_opt(sb, DEBUG)) 14902e10e2f2SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: added group %u:" 14912e10e2f2SYongqiang Yang "%llu blocks(%llu free %llu reserved)\n", flex_gd->count, 14922e10e2f2SYongqiang Yang blocks_count, free_blocks, reserved_blocks); 14932e10e2f2SYongqiang Yang } 14942e10e2f2SYongqiang Yang 14954bac1f8cSYongqiang Yang /* Add a flex group to an fs. Ensure we handle all possible error conditions 14964bac1f8cSYongqiang Yang * _before_ we start modifying the filesystem, because we cannot abort the 14974bac1f8cSYongqiang Yang * transaction and not have it write the data to disk. 14984bac1f8cSYongqiang Yang */ 14994bac1f8cSYongqiang Yang static int ext4_flex_group_add(struct super_block *sb, 15004bac1f8cSYongqiang Yang struct inode *resize_inode, 15014bac1f8cSYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 15024bac1f8cSYongqiang Yang { 15034bac1f8cSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 15044bac1f8cSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 15054bac1f8cSYongqiang Yang ext4_fsblk_t o_blocks_count; 15064bac1f8cSYongqiang Yang ext4_grpblk_t last; 15074bac1f8cSYongqiang Yang ext4_group_t group; 15084bac1f8cSYongqiang Yang handle_t *handle; 15094bac1f8cSYongqiang Yang unsigned reserved_gdb; 15104bac1f8cSYongqiang Yang int err = 0, err2 = 0, credit; 15114bac1f8cSYongqiang Yang 15124bac1f8cSYongqiang Yang BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags); 15134bac1f8cSYongqiang Yang 15144bac1f8cSYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 15154bac1f8cSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 15164bac1f8cSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 15174bac1f8cSYongqiang Yang BUG_ON(last); 15184bac1f8cSYongqiang Yang 15194bac1f8cSYongqiang Yang err = setup_new_flex_group_blocks(sb, flex_gd); 15204bac1f8cSYongqiang Yang if (err) 15214bac1f8cSYongqiang Yang goto exit; 15224bac1f8cSYongqiang Yang /* 15234bac1f8cSYongqiang Yang * We will always be modifying at least the superblock and GDT 15242c869b26SJan Kara * blocks. If we are adding a group past the last current GDT block, 15254bac1f8cSYongqiang Yang * we will also modify the inode and the dindirect block. If we 15264bac1f8cSYongqiang Yang * are adding a group with superblock/GDT backups we will also 15274bac1f8cSYongqiang Yang * modify each of the reserved GDT dindirect blocks. 15284bac1f8cSYongqiang Yang */ 15292c869b26SJan Kara credit = 3; /* sb, resize inode, resize inode dindirect */ 15302c869b26SJan Kara /* GDT blocks */ 15312c869b26SJan Kara credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb)); 15322c869b26SJan Kara credit += reserved_gdb; /* Reserved GDT dindirect blocks */ 15339924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit); 15344bac1f8cSYongqiang Yang if (IS_ERR(handle)) { 15354bac1f8cSYongqiang Yang err = PTR_ERR(handle); 15364bac1f8cSYongqiang Yang goto exit; 15374bac1f8cSYongqiang Yang } 15384bac1f8cSYongqiang Yang 15395d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 1540188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 1541188c299eSJan Kara EXT4_JTR_NONE); 15424bac1f8cSYongqiang Yang if (err) 15434bac1f8cSYongqiang Yang goto exit_journal; 15444bac1f8cSYongqiang Yang 15454bac1f8cSYongqiang Yang group = flex_gd->groups[0].group; 154649598e04SJun Piao BUG_ON(group != sbi->s_groups_count); 15474bac1f8cSYongqiang Yang err = ext4_add_new_descs(handle, sb, group, 15484bac1f8cSYongqiang Yang resize_inode, flex_gd->count); 15494bac1f8cSYongqiang Yang if (err) 15504bac1f8cSYongqiang Yang goto exit_journal; 15514bac1f8cSYongqiang Yang 15524bac1f8cSYongqiang Yang err = ext4_setup_new_descs(handle, sb, flex_gd); 15534bac1f8cSYongqiang Yang if (err) 15544bac1f8cSYongqiang Yang goto exit_journal; 15554bac1f8cSYongqiang Yang 15564bac1f8cSYongqiang Yang ext4_update_super(sb, flex_gd); 15574bac1f8cSYongqiang Yang 1558a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 15594bac1f8cSYongqiang Yang 15604bac1f8cSYongqiang Yang exit_journal: 15614bac1f8cSYongqiang Yang err2 = ext4_journal_stop(handle); 15624bac1f8cSYongqiang Yang if (!err) 15634bac1f8cSYongqiang Yang err = err2; 15644bac1f8cSYongqiang Yang 15654bac1f8cSYongqiang Yang if (!err) { 15662ebd1704SYongqiang Yang int gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 15672ebd1704SYongqiang Yang int gdb_num_end = ((group + flex_gd->count - 1) / 15682ebd1704SYongqiang Yang EXT4_DESC_PER_BLOCK(sb)); 1569e2b911c5SDarrick J. Wong int meta_bg = ext4_has_feature_meta_bg(sb); 15700acdb887STao Ma sector_t old_gdb = 0; 15712ebd1704SYongqiang Yang 15724bac1f8cSYongqiang Yang update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 157301f795f9SYongqiang Yang sizeof(struct ext4_super_block), 0); 15742ebd1704SYongqiang Yang for (; gdb_num <= gdb_num_end; gdb_num++) { 15754bac1f8cSYongqiang Yang struct buffer_head *gdb_bh; 15762ebd1704SYongqiang Yang 15771d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 15781d0c3924STheodore Ts'o gdb_num); 15790acdb887STao Ma if (old_gdb == gdb_bh->b_blocknr) 15800acdb887STao Ma continue; 15814bac1f8cSYongqiang Yang update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data, 158201f795f9SYongqiang Yang gdb_bh->b_size, meta_bg); 15830acdb887STao Ma old_gdb = gdb_bh->b_blocknr; 15844bac1f8cSYongqiang Yang } 15854bac1f8cSYongqiang Yang } 15864bac1f8cSYongqiang Yang exit: 15874bac1f8cSYongqiang Yang return err; 15884bac1f8cSYongqiang Yang } 15894bac1f8cSYongqiang Yang 159019c5246dSYongqiang Yang static int ext4_setup_next_flex_gd(struct super_block *sb, 159119c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 159219c5246dSYongqiang Yang ext4_fsblk_t n_blocks_count, 159319c5246dSYongqiang Yang unsigned long flexbg_size) 159419c5246dSYongqiang Yang { 1595d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 1596d77147ffSharshads struct ext4_super_block *es = sbi->s_es; 159719c5246dSYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 159819c5246dSYongqiang Yang ext4_fsblk_t o_blocks_count; 159919c5246dSYongqiang Yang ext4_group_t n_group; 160019c5246dSYongqiang Yang ext4_group_t group; 160119c5246dSYongqiang Yang ext4_group_t last_group; 160219c5246dSYongqiang Yang ext4_grpblk_t last; 1603d77147ffSharshads ext4_grpblk_t clusters_per_group; 160419c5246dSYongqiang Yang unsigned long i; 160519c5246dSYongqiang Yang 1606d77147ffSharshads clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb); 160719c5246dSYongqiang Yang 160819c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 160919c5246dSYongqiang Yang 161019c5246dSYongqiang Yang if (o_blocks_count == n_blocks_count) 161119c5246dSYongqiang Yang return 0; 161219c5246dSYongqiang Yang 161319c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 161419c5246dSYongqiang Yang BUG_ON(last); 161519c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last); 161619c5246dSYongqiang Yang 161719c5246dSYongqiang Yang last_group = group | (flexbg_size - 1); 161819c5246dSYongqiang Yang if (last_group > n_group) 161919c5246dSYongqiang Yang last_group = n_group; 162019c5246dSYongqiang Yang 162119c5246dSYongqiang Yang flex_gd->count = last_group - group + 1; 162219c5246dSYongqiang Yang 162319c5246dSYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 162419c5246dSYongqiang Yang int overhead; 162519c5246dSYongqiang Yang 162619c5246dSYongqiang Yang group_data[i].group = group + i; 1627d77147ffSharshads group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb); 162801f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group + i); 1629d77147ffSharshads group_data[i].mdata_blocks = overhead; 1630d77147ffSharshads group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb); 16317f511862STheodore Ts'o if (ext4_has_group_desc_csum(sb)) { 163219c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT | 163319c5246dSYongqiang Yang EXT4_BG_INODE_UNINIT; 16347f511862STheodore Ts'o if (!test_opt(sb, INIT_INODE_TABLE)) 16357f511862STheodore Ts'o flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED; 16367f511862STheodore Ts'o } else 163719c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED; 163819c5246dSYongqiang Yang } 163919c5246dSYongqiang Yang 1640feb0ab32SDarrick J. Wong if (last_group == n_group && ext4_has_group_desc_csum(sb)) 164119c5246dSYongqiang Yang /* We need to initialize block bitmap of last group. */ 164219c5246dSYongqiang Yang flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT; 164319c5246dSYongqiang Yang 1644d77147ffSharshads if ((last_group == n_group) && (last != clusters_per_group - 1)) { 1645d77147ffSharshads group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1); 1646d77147ffSharshads group_data[i - 1].free_clusters_count -= clusters_per_group - 164719c5246dSYongqiang Yang last - 1; 164819c5246dSYongqiang Yang } 164919c5246dSYongqiang Yang 165019c5246dSYongqiang Yang return 1; 165119c5246dSYongqiang Yang } 165219c5246dSYongqiang Yang 1653ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 1654ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 1655ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 1656ac27a0ecSDave Kleikamp * write the data to disk. 1657ac27a0ecSDave Kleikamp * 1658ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 1659ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 1660ac27a0ecSDave Kleikamp * 1661ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 1662ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 1663ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 1664ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 1665ac27a0ecSDave Kleikamp */ 1666617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 1667ac27a0ecSDave Kleikamp { 166861f296ccSYongqiang Yang struct ext4_new_flex_group_data flex_gd; 1669617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1670617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 1671617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 1672ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1673ac27a0ecSDave Kleikamp struct inode *inode = NULL; 167403b40e34SJon Ernst int gdb_off; 167561f296ccSYongqiang Yang int err; 167661f296ccSYongqiang Yang __u16 bg_flags = 0; 1677ac27a0ecSDave Kleikamp 1678617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 1679ac27a0ecSDave Kleikamp 1680e2b911c5SDarrick J. Wong if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) { 168112062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 1682ac27a0ecSDave Kleikamp return -EPERM; 1683ac27a0ecSDave Kleikamp } 1684ac27a0ecSDave Kleikamp 1685bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 1686bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 168712062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1688ac27a0ecSDave Kleikamp return -EINVAL; 1689ac27a0ecSDave Kleikamp } 1690ac27a0ecSDave Kleikamp 1691617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 1692ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 169312062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 1694ac27a0ecSDave Kleikamp return -EINVAL; 1695ac27a0ecSDave Kleikamp } 1696ac27a0ecSDave Kleikamp 1697ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 1698e647e291Sruippan (潘睿) if (!ext4_has_feature_resize_inode(sb) || 1699e2b911c5SDarrick J. Wong !le16_to_cpu(es->s_reserved_gdt_blocks)) { 170012062dddSEric Sandeen ext4_warning(sb, 1701ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 1702ac27a0ecSDave Kleikamp return -EPERM; 1703ac27a0ecSDave Kleikamp } 17048a363970STheodore Ts'o inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL); 17051d1fe1eeSDavid Howells if (IS_ERR(inode)) { 170612062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 17071d1fe1eeSDavid Howells return PTR_ERR(inode); 1708ac27a0ecSDave Kleikamp } 1709ac27a0ecSDave Kleikamp } 1710ac27a0ecSDave Kleikamp 1711920313a7SAneesh Kumar K.V 171261f296ccSYongqiang Yang err = verify_group_input(sb, input); 1713668f4dc5SYongqiang Yang if (err) 171461f296ccSYongqiang Yang goto out; 1715ac27a0ecSDave Kleikamp 1716117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, input->group + 1); 1717117fff10STheodore Ts'o if (err) 17187f511862STheodore Ts'o goto out; 1719117fff10STheodore Ts'o 172028623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, input->group + 1); 172128623c2fSTheodore Ts'o if (err) 172228623c2fSTheodore Ts'o goto out; 172328623c2fSTheodore Ts'o 172461f296ccSYongqiang Yang flex_gd.count = 1; 172561f296ccSYongqiang Yang flex_gd.groups = input; 172661f296ccSYongqiang Yang flex_gd.bg_flags = &bg_flags; 172761f296ccSYongqiang Yang err = ext4_flex_group_add(sb, inode, &flex_gd); 172861f296ccSYongqiang Yang out: 1729ac27a0ecSDave Kleikamp iput(inode); 1730ac27a0ecSDave Kleikamp return err; 1731617ba13bSMingming Cao } /* ext4_group_add */ 1732ac27a0ecSDave Kleikamp 17332b2d6d01STheodore Ts'o /* 173418e31438SYongqiang Yang * extend a group without checking assuming that checking has been done. 173518e31438SYongqiang Yang */ 173618e31438SYongqiang Yang static int ext4_group_extend_no_check(struct super_block *sb, 173718e31438SYongqiang Yang ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) 173818e31438SYongqiang Yang { 173918e31438SYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 174018e31438SYongqiang Yang handle_t *handle; 174118e31438SYongqiang Yang int err = 0, err2; 174218e31438SYongqiang Yang 174318e31438SYongqiang Yang /* We will update the superblock, one block bitmap, and 174418e31438SYongqiang Yang * one group descriptor via ext4_group_add_blocks(). 174518e31438SYongqiang Yang */ 17469924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3); 174718e31438SYongqiang Yang if (IS_ERR(handle)) { 174818e31438SYongqiang Yang err = PTR_ERR(handle); 174918e31438SYongqiang Yang ext4_warning(sb, "error %d on journal start", err); 175018e31438SYongqiang Yang return err; 175118e31438SYongqiang Yang } 175218e31438SYongqiang Yang 17535d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 1754188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh, 1755188c299eSJan Kara EXT4_JTR_NONE); 175618e31438SYongqiang Yang if (err) { 175718e31438SYongqiang Yang ext4_warning(sb, "error %d on journal write access", err); 175818e31438SYongqiang Yang goto errout; 175918e31438SYongqiang Yang } 176018e31438SYongqiang Yang 176105c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 176218e31438SYongqiang Yang ext4_blocks_count_set(es, o_blocks_count + add); 1763636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add); 176405c2c00fSJan Kara ext4_superblock_csum_set(sb); 176505c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 176618e31438SYongqiang Yang ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 176718e31438SYongqiang Yang o_blocks_count + add); 176818e31438SYongqiang Yang /* We add the blocks to the bitmap and set the group need init bit */ 176918e31438SYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 177018e31438SYongqiang Yang if (err) 177118e31438SYongqiang Yang goto errout; 1772a3f5cf14SJan Kara ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 177318e31438SYongqiang Yang ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 177418e31438SYongqiang Yang o_blocks_count + add); 177518e31438SYongqiang Yang errout: 177618e31438SYongqiang Yang err2 = ext4_journal_stop(handle); 177718e31438SYongqiang Yang if (err2 && !err) 177818e31438SYongqiang Yang err = err2; 177918e31438SYongqiang Yang 178018e31438SYongqiang Yang if (!err) { 178118e31438SYongqiang Yang if (test_opt(sb, DEBUG)) 178218e31438SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extended group to %llu " 178318e31438SYongqiang Yang "blocks\n", ext4_blocks_count(es)); 17846ca792edSMaarten ter Huurne update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, 178501f795f9SYongqiang Yang (char *)es, sizeof(struct ext4_super_block), 0); 178618e31438SYongqiang Yang } 178718e31438SYongqiang Yang return err; 178818e31438SYongqiang Yang } 178918e31438SYongqiang Yang 179018e31438SYongqiang Yang /* 17912b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 1792ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 1793ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 1794ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 1795ac27a0ecSDave Kleikamp * 1796617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 1797ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 1798ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 1799ac27a0ecSDave Kleikamp */ 1800617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 1801617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 1802ac27a0ecSDave Kleikamp { 1803617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 1804617ba13bSMingming Cao ext4_grpblk_t last; 1805617ba13bSMingming Cao ext4_grpblk_t add; 1806ac27a0ecSDave Kleikamp struct buffer_head *bh; 1807d89651c8SYongqiang Yang int err; 18085f21b0e6SFrederic Bohe ext4_group_t group; 1809ac27a0ecSDave Kleikamp 1810bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 1811ac27a0ecSDave Kleikamp 1812ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 181392b97816STheodore Ts'o ext4_msg(sb, KERN_DEBUG, 181492b97816STheodore Ts'o "extending last group from %llu to %llu blocks", 1815ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 1816ac27a0ecSDave Kleikamp 1817ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 1818ac27a0ecSDave Kleikamp return 0; 1819ac27a0ecSDave Kleikamp 1820ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 182192b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, 182292b97816STheodore Ts'o "filesystem too large to resize to %llu blocks safely", 182392b97816STheodore Ts'o n_blocks_count); 1824ac27a0ecSDave Kleikamp return -EINVAL; 1825ac27a0ecSDave Kleikamp } 1826ac27a0ecSDave Kleikamp 1827ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 182812062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 18298f82f840SYongqiang Yang return -EINVAL; 1830ac27a0ecSDave Kleikamp } 1831ac27a0ecSDave Kleikamp 1832ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 18335f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1834ac27a0ecSDave Kleikamp 1835ac27a0ecSDave Kleikamp if (last == 0) { 183612062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1837ac27a0ecSDave Kleikamp return -EPERM; 1838ac27a0ecSDave Kleikamp } 1839ac27a0ecSDave Kleikamp 1840617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1841ac27a0ecSDave Kleikamp 1842ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 184312062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1844ac27a0ecSDave Kleikamp return -EINVAL; 1845ac27a0ecSDave Kleikamp } 1846ac27a0ecSDave Kleikamp 1847ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1848ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1849ac27a0ecSDave Kleikamp 1850ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 185112062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1852ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1853ac27a0ecSDave Kleikamp 1854ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 18550a846f49Szhangyi (F) bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0); 18560a846f49Szhangyi (F) if (IS_ERR(bh)) { 185712062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1858ac27a0ecSDave Kleikamp return -ENOSPC; 1859ac27a0ecSDave Kleikamp } 1860ac27a0ecSDave Kleikamp brelse(bh); 1861ac27a0ecSDave Kleikamp 1862d89651c8SYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 1863ac27a0ecSDave Kleikamp return err; 1864617ba13bSMingming Cao } /* ext4_group_extend */ 186519c5246dSYongqiang Yang 18661c6bd717STheodore Ts'o 18671c6bd717STheodore Ts'o static int num_desc_blocks(struct super_block *sb, ext4_group_t groups) 18681c6bd717STheodore Ts'o { 18691c6bd717STheodore Ts'o return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb); 18701c6bd717STheodore Ts'o } 18711c6bd717STheodore Ts'o 18721c6bd717STheodore Ts'o /* 18731c6bd717STheodore Ts'o * Release the resize inode and drop the resize_inode feature if there 18741c6bd717STheodore Ts'o * are no more reserved gdt blocks, and then convert the file system 18751c6bd717STheodore Ts'o * to enable meta_bg 18761c6bd717STheodore Ts'o */ 18771c6bd717STheodore Ts'o static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode) 18781c6bd717STheodore Ts'o { 18791c6bd717STheodore Ts'o handle_t *handle; 18801c6bd717STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 18811c6bd717STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 188259e31c15STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 18831c6bd717STheodore Ts'o ext4_fsblk_t nr; 18841c6bd717STheodore Ts'o int i, ret, err = 0; 18851c6bd717STheodore Ts'o int credits = 1; 18861c6bd717STheodore Ts'o 18871c6bd717STheodore Ts'o ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg"); 188859e31c15STheodore Ts'o if (inode) { 18891c6bd717STheodore Ts'o if (es->s_reserved_gdt_blocks) { 18901c6bd717STheodore Ts'o ext4_error(sb, "Unexpected non-zero " 18911c6bd717STheodore Ts'o "s_reserved_gdt_blocks"); 18921c6bd717STheodore Ts'o return -EPERM; 18931c6bd717STheodore Ts'o } 18941c6bd717STheodore Ts'o 18951c6bd717STheodore Ts'o /* Do a quick sanity check of the resize inode */ 1896d77147ffSharshads if (inode->i_blocks != 1 << (inode->i_blkbits - 1897d77147ffSharshads (9 - sbi->s_cluster_bits))) 18981c6bd717STheodore Ts'o goto invalid_resize_inode; 18991c6bd717STheodore Ts'o for (i = 0; i < EXT4_N_BLOCKS; i++) { 19001c6bd717STheodore Ts'o if (i == EXT4_DIND_BLOCK) { 19011c6bd717STheodore Ts'o if (ei->i_data[i]) 19021c6bd717STheodore Ts'o continue; 19031c6bd717STheodore Ts'o else 19041c6bd717STheodore Ts'o goto invalid_resize_inode; 19051c6bd717STheodore Ts'o } 19061c6bd717STheodore Ts'o if (ei->i_data[i]) 19071c6bd717STheodore Ts'o goto invalid_resize_inode; 19081c6bd717STheodore Ts'o } 19091c6bd717STheodore Ts'o credits += 3; /* block bitmap, bg descriptor, resize inode */ 19101c6bd717STheodore Ts'o } 19111c6bd717STheodore Ts'o 19129924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits); 19131c6bd717STheodore Ts'o if (IS_ERR(handle)) 19141c6bd717STheodore Ts'o return PTR_ERR(handle); 19151c6bd717STheodore Ts'o 19165d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 1917188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 1918188c299eSJan Kara EXT4_JTR_NONE); 19191c6bd717STheodore Ts'o if (err) 19201c6bd717STheodore Ts'o goto errout; 19211c6bd717STheodore Ts'o 192205c2c00fSJan Kara lock_buffer(sbi->s_sbh); 1923e2b911c5SDarrick J. Wong ext4_clear_feature_resize_inode(sb); 1924e2b911c5SDarrick J. Wong ext4_set_feature_meta_bg(sb); 19251c6bd717STheodore Ts'o sbi->s_es->s_first_meta_bg = 19261c6bd717STheodore Ts'o cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count)); 192705c2c00fSJan Kara ext4_superblock_csum_set(sb); 192805c2c00fSJan Kara unlock_buffer(sbi->s_sbh); 19291c6bd717STheodore Ts'o 1930a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 19311c6bd717STheodore Ts'o if (err) { 19321c6bd717STheodore Ts'o ext4_std_error(sb, err); 19331c6bd717STheodore Ts'o goto errout; 19341c6bd717STheodore Ts'o } 19351c6bd717STheodore Ts'o 19361c6bd717STheodore Ts'o if (inode) { 19371c6bd717STheodore Ts'o nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]); 19381c6bd717STheodore Ts'o ext4_free_blocks(handle, inode, NULL, nr, 1, 19391c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | 19401c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 19411c6bd717STheodore Ts'o ei->i_data[EXT4_DIND_BLOCK] = 0; 19421c6bd717STheodore Ts'o inode->i_blocks = 0; 19431c6bd717STheodore Ts'o 19441c6bd717STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 19451c6bd717STheodore Ts'o if (err) 19461c6bd717STheodore Ts'o ext4_std_error(sb, err); 19471c6bd717STheodore Ts'o } 19481c6bd717STheodore Ts'o 19491c6bd717STheodore Ts'o errout: 19501c6bd717STheodore Ts'o ret = ext4_journal_stop(handle); 19511c6bd717STheodore Ts'o if (!err) 19521c6bd717STheodore Ts'o err = ret; 19531c6bd717STheodore Ts'o return ret; 19541c6bd717STheodore Ts'o 19551c6bd717STheodore Ts'o invalid_resize_inode: 19561c6bd717STheodore Ts'o ext4_error(sb, "corrupted/inconsistent resize inode"); 19571c6bd717STheodore Ts'o return -EINVAL; 19581c6bd717STheodore Ts'o } 19591c6bd717STheodore Ts'o 196019c5246dSYongqiang Yang /* 196119c5246dSYongqiang Yang * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count 196219c5246dSYongqiang Yang * 196319c5246dSYongqiang Yang * @sb: super block of the fs to be resized 196419c5246dSYongqiang Yang * @n_blocks_count: the number of blocks resides in the resized fs 196519c5246dSYongqiang Yang */ 196619c5246dSYongqiang Yang int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count) 196719c5246dSYongqiang Yang { 196819c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd = NULL; 196919c5246dSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 197019c5246dSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 197119c5246dSYongqiang Yang struct buffer_head *bh; 197201f795f9SYongqiang Yang struct inode *resize_inode = NULL; 197301f795f9SYongqiang Yang ext4_grpblk_t add, offset; 197419c5246dSYongqiang Yang unsigned long n_desc_blocks; 197519c5246dSYongqiang Yang unsigned long o_desc_blocks; 197601f795f9SYongqiang Yang ext4_group_t o_group; 197701f795f9SYongqiang Yang ext4_group_t n_group; 197801f795f9SYongqiang Yang ext4_fsblk_t o_blocks_count; 19791c6bd717STheodore Ts'o ext4_fsblk_t n_blocks_count_retry = 0; 19804da4a56eSTheodore Ts'o unsigned long last_update_time = 0; 1981117fff10STheodore Ts'o int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex; 198201f795f9SYongqiang Yang int meta_bg; 198319c5246dSYongqiang Yang 198459e31c15STheodore Ts'o /* See if the device is actually as big as what was requested */ 19850a846f49Szhangyi (F) bh = ext4_sb_bread(sb, n_blocks_count - 1, 0); 19860a846f49Szhangyi (F) if (IS_ERR(bh)) { 198759e31c15STheodore Ts'o ext4_warning(sb, "can't read last block, resize aborted"); 198859e31c15STheodore Ts'o return -ENOSPC; 198959e31c15STheodore Ts'o } 199059e31c15STheodore Ts'o brelse(bh); 199159e31c15STheodore Ts'o 19921c6bd717STheodore Ts'o retry: 199319c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 199419c5246dSYongqiang Yang 199559e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu " 199692b97816STheodore Ts'o "to %llu blocks", o_blocks_count, n_blocks_count); 199719c5246dSYongqiang Yang 199819c5246dSYongqiang Yang if (n_blocks_count < o_blocks_count) { 199919c5246dSYongqiang Yang /* On-line shrinking not supported */ 200019c5246dSYongqiang Yang ext4_warning(sb, "can't shrink FS - resize aborted"); 200119c5246dSYongqiang Yang return -EINVAL; 200219c5246dSYongqiang Yang } 200319c5246dSYongqiang Yang 200419c5246dSYongqiang Yang if (n_blocks_count == o_blocks_count) 200519c5246dSYongqiang Yang /* Nothing need to do */ 200619c5246dSYongqiang Yang return 0; 200719c5246dSYongqiang Yang 2008bd86298eSLukas Czerner n_group = ext4_get_group_number(sb, n_blocks_count - 1); 20094f2f76f7SJan Kara if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) { 20103f8a6411STheodore Ts'o ext4_warning(sb, "resize would cause inodes_count overflow"); 20113f8a6411STheodore Ts'o return -EINVAL; 20123f8a6411STheodore Ts'o } 2013a0ade1deSLukas Czerner ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset); 201419c5246dSYongqiang Yang 20151c6bd717STheodore Ts'o n_desc_blocks = num_desc_blocks(sb, n_group + 1); 20161c6bd717STheodore Ts'o o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count); 201719c5246dSYongqiang Yang 2018e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 201901f795f9SYongqiang Yang 2020e2b911c5SDarrick J. Wong if (ext4_has_feature_resize_inode(sb)) { 202101f795f9SYongqiang Yang if (meta_bg) { 202201f795f9SYongqiang Yang ext4_error(sb, "resize_inode and meta_bg enabled " 202301f795f9SYongqiang Yang "simultaneously"); 202401f795f9SYongqiang Yang return -EINVAL; 202501f795f9SYongqiang Yang } 20261c6bd717STheodore Ts'o if (n_desc_blocks > o_desc_blocks + 20271c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks)) { 20281c6bd717STheodore Ts'o n_blocks_count_retry = n_blocks_count; 20291c6bd717STheodore Ts'o n_desc_blocks = o_desc_blocks + 20301c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks); 20311c6bd717STheodore Ts'o n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb); 2032aec51758SJerry Lee n_blocks_count = (ext4_fsblk_t)n_group * 2033f96c3ac8SJan Kara EXT4_BLOCKS_PER_GROUP(sb) + 2034f96c3ac8SJan Kara le32_to_cpu(es->s_first_data_block); 20351c6bd717STheodore Ts'o n_group--; /* set to last group number */ 203619c5246dSYongqiang Yang } 20371c6bd717STheodore Ts'o 20381c6bd717STheodore Ts'o if (!resize_inode) 20398a363970STheodore Ts'o resize_inode = ext4_iget(sb, EXT4_RESIZE_INO, 20408a363970STheodore Ts'o EXT4_IGET_SPECIAL); 204119c5246dSYongqiang Yang if (IS_ERR(resize_inode)) { 204219c5246dSYongqiang Yang ext4_warning(sb, "Error opening resize inode"); 204319c5246dSYongqiang Yang return PTR_ERR(resize_inode); 204419c5246dSYongqiang Yang } 20451c6bd717STheodore Ts'o } 20461c6bd717STheodore Ts'o 204759e31c15STheodore Ts'o if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) { 20481c6bd717STheodore Ts'o err = ext4_convert_meta_bg(sb, resize_inode); 20491c6bd717STheodore Ts'o if (err) 20501c6bd717STheodore Ts'o goto out; 20511c6bd717STheodore Ts'o if (resize_inode) { 20521c6bd717STheodore Ts'o iput(resize_inode); 20531c6bd717STheodore Ts'o resize_inode = NULL; 20541c6bd717STheodore Ts'o } 20551c6bd717STheodore Ts'o if (n_blocks_count_retry) { 20561c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 20571c6bd717STheodore Ts'o n_blocks_count_retry = 0; 20581c6bd717STheodore Ts'o goto retry; 20591c6bd717STheodore Ts'o } 206001f795f9SYongqiang Yang } 206119c5246dSYongqiang Yang 2062f0a459deSTheodore Ts'o /* 2063f0a459deSTheodore Ts'o * Make sure the last group has enough space so that it's 2064f0a459deSTheodore Ts'o * guaranteed to have enough space for all metadata blocks 2065f0a459deSTheodore Ts'o * that it might need to hold. (We might not need to store 2066f0a459deSTheodore Ts'o * the inode table blocks in the last block group, but there 2067f0a459deSTheodore Ts'o * will be cases where this might be needed.) 2068f0a459deSTheodore Ts'o */ 2069f0a459deSTheodore Ts'o if ((ext4_group_first_block_no(sb, n_group) + 2070f0a459deSTheodore Ts'o ext4_group_overhead_blocks(sb, n_group) + 2 + 2071f0a459deSTheodore Ts'o sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) { 2072f0a459deSTheodore Ts'o n_blocks_count = ext4_group_first_block_no(sb, n_group); 2073f0a459deSTheodore Ts'o n_group--; 2074f0a459deSTheodore Ts'o n_blocks_count_retry = 0; 2075f0a459deSTheodore Ts'o if (resize_inode) { 2076f0a459deSTheodore Ts'o iput(resize_inode); 2077f0a459deSTheodore Ts'o resize_inode = NULL; 2078f0a459deSTheodore Ts'o } 2079f0a459deSTheodore Ts'o goto retry; 2080f0a459deSTheodore Ts'o } 2081f0a459deSTheodore Ts'o 208219c5246dSYongqiang Yang /* extend the last group */ 2083a0ade1deSLukas Czerner if (n_group == o_group) 2084a0ade1deSLukas Czerner add = n_blocks_count - o_blocks_count; 2085a0ade1deSLukas Czerner else 2086d77147ffSharshads add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1)); 2087a0ade1deSLukas Czerner if (add > 0) { 208819c5246dSYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 208919c5246dSYongqiang Yang if (err) 209019c5246dSYongqiang Yang goto out; 209119c5246dSYongqiang Yang } 209219c5246dSYongqiang Yang 2093d7574ad0SYongqiang Yang if (ext4_blocks_count(es) == n_blocks_count) 209419c5246dSYongqiang Yang goto out; 209519c5246dSYongqiang Yang 2096117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, n_group + 1); 2097117fff10STheodore Ts'o if (err) 2098f348e224SVasily Averin goto out; 2099117fff10STheodore Ts'o 210028623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, n_group + 1); 210128623c2fSTheodore Ts'o if (err) 210228623c2fSTheodore Ts'o goto out; 210328623c2fSTheodore Ts'o 210419c5246dSYongqiang Yang flex_gd = alloc_flex_gd(flexbg_size); 210519c5246dSYongqiang Yang if (flex_gd == NULL) { 210619c5246dSYongqiang Yang err = -ENOMEM; 210719c5246dSYongqiang Yang goto out; 210819c5246dSYongqiang Yang } 210919c5246dSYongqiang Yang 211019c5246dSYongqiang Yang /* Add flex groups. Note that a regular group is a 211119c5246dSYongqiang Yang * flex group with 1 group. 211219c5246dSYongqiang Yang */ 211319c5246dSYongqiang Yang while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count, 211419c5246dSYongqiang Yang flexbg_size)) { 2115a861fb9fSWang Qing if (time_is_before_jiffies(last_update_time + HZ * 10)) { 21164da4a56eSTheodore Ts'o if (last_update_time) 21174da4a56eSTheodore Ts'o ext4_msg(sb, KERN_INFO, 21184da4a56eSTheodore Ts'o "resized to %llu blocks", 21194da4a56eSTheodore Ts'o ext4_blocks_count(es)); 21204da4a56eSTheodore Ts'o last_update_time = jiffies; 21214da4a56eSTheodore Ts'o } 212203c1c290SYongqiang Yang if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0) 212303c1c290SYongqiang Yang break; 212419c5246dSYongqiang Yang err = ext4_flex_group_add(sb, resize_inode, flex_gd); 212519c5246dSYongqiang Yang if (unlikely(err)) 212619c5246dSYongqiang Yang break; 212719c5246dSYongqiang Yang } 212819c5246dSYongqiang Yang 21291c6bd717STheodore Ts'o if (!err && n_blocks_count_retry) { 21301c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 21311c6bd717STheodore Ts'o n_blocks_count_retry = 0; 21321c6bd717STheodore Ts'o free_flex_gd(flex_gd); 21331c6bd717STheodore Ts'o flex_gd = NULL; 2134db6aee62SVasily Averin if (resize_inode) { 2135db6aee62SVasily Averin iput(resize_inode); 2136db6aee62SVasily Averin resize_inode = NULL; 2137db6aee62SVasily Averin } 21381c6bd717STheodore Ts'o goto retry; 21391c6bd717STheodore Ts'o } 21401c6bd717STheodore Ts'o 214119c5246dSYongqiang Yang out: 214219c5246dSYongqiang Yang if (flex_gd) 214319c5246dSYongqiang Yang free_flex_gd(flex_gd); 214401f795f9SYongqiang Yang if (resize_inode != NULL) 214519c5246dSYongqiang Yang iput(resize_inode); 21466c732840SLukas Czerner if (err) 21476c732840SLukas Czerner ext4_warning(sb, "error (%d) occurred during " 21486c732840SLukas Czerner "file system resize", err); 21496c732840SLukas Czerner ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", 21506c732840SLukas Czerner ext4_blocks_count(es)); 215119c5246dSYongqiang Yang return err; 215219c5246dSYongqiang Yang } 2153