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> 17ac27a0ecSDave Kleikamp 183dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 19ac27a0ecSDave Kleikamp 201d0c3924STheodore Ts'o struct ext4_rcu_ptr { 211d0c3924STheodore Ts'o struct rcu_head rcu; 221d0c3924STheodore Ts'o void *ptr; 231d0c3924STheodore Ts'o }; 241d0c3924STheodore Ts'o 251d0c3924STheodore Ts'o static void ext4_rcu_ptr_callback(struct rcu_head *head) 261d0c3924STheodore Ts'o { 271d0c3924STheodore Ts'o struct ext4_rcu_ptr *ptr; 281d0c3924STheodore Ts'o 291d0c3924STheodore Ts'o ptr = container_of(head, struct ext4_rcu_ptr, rcu); 301d0c3924STheodore Ts'o kvfree(ptr->ptr); 311d0c3924STheodore Ts'o kfree(ptr); 321d0c3924STheodore Ts'o } 331d0c3924STheodore Ts'o 341d0c3924STheodore Ts'o void ext4_kvfree_array_rcu(void *to_free) 351d0c3924STheodore Ts'o { 361d0c3924STheodore Ts'o struct ext4_rcu_ptr *ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); 371d0c3924STheodore Ts'o 381d0c3924STheodore Ts'o if (ptr) { 391d0c3924STheodore Ts'o ptr->ptr = to_free; 401d0c3924STheodore Ts'o call_rcu(&ptr->rcu, ext4_rcu_ptr_callback); 411d0c3924STheodore Ts'o return; 421d0c3924STheodore Ts'o } 431d0c3924STheodore Ts'o synchronize_rcu(); 441d0c3924STheodore Ts'o kvfree(to_free); 451d0c3924STheodore Ts'o } 461d0c3924STheodore Ts'o 478f82f840SYongqiang Yang int ext4_resize_begin(struct super_block *sb) 488f82f840SYongqiang Yang { 495f8c1093STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 508f82f840SYongqiang Yang int ret = 0; 518f82f840SYongqiang Yang 528f82f840SYongqiang Yang if (!capable(CAP_SYS_RESOURCE)) 538f82f840SYongqiang Yang return -EPERM; 548f82f840SYongqiang Yang 55ce723c31SYongqiang Yang /* 56011fa994STheodore Ts'o * If we are not using the primary superblock/GDT copy don't resize, 57011fa994STheodore Ts'o * because the user tools have no way of handling this. Probably a 58011fa994STheodore Ts'o * bad time to do it anyways. 59011fa994STheodore Ts'o */ 605f8c1093STheodore Ts'o if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) != 61011fa994STheodore Ts'o le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 62011fa994STheodore Ts'o ext4_warning(sb, "won't resize using backup superblock at %llu", 63011fa994STheodore Ts'o (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 64011fa994STheodore Ts'o return -EPERM; 65011fa994STheodore Ts'o } 66011fa994STheodore Ts'o 67011fa994STheodore Ts'o /* 68ce723c31SYongqiang Yang * We are not allowed to do online-resizing on a filesystem mounted 69ce723c31SYongqiang Yang * with error, because it can destroy the filesystem easily. 70ce723c31SYongqiang Yang */ 71ce723c31SYongqiang Yang if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 72ce723c31SYongqiang Yang ext4_warning(sb, "There are errors in the filesystem, " 738d2ae1cbSJakub Wilk "so online resizing is not allowed"); 74ce723c31SYongqiang Yang return -EPERM; 75ce723c31SYongqiang Yang } 76ce723c31SYongqiang Yang 77e9f9f61dSJosh Triplett if (ext4_has_feature_bigalloc(sb)) { 78e9f9f61dSJosh Triplett ext4_msg(sb, KERN_ERR, "Online resizing not supported with bigalloc"); 79e9f9f61dSJosh Triplett return -EOPNOTSUPP; 80e9f9f61dSJosh Triplett } 81*b1489186SJosh Triplett if (ext4_has_feature_sparse_super2(sb)) { 82*b1489186SJosh Triplett ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2"); 83*b1489186SJosh Triplett return -EOPNOTSUPP; 84*b1489186SJosh Triplett } 85e9f9f61dSJosh Triplett 869549a168STheodore Ts'o if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING, 879549a168STheodore Ts'o &EXT4_SB(sb)->s_ext4_flags)) 888f82f840SYongqiang Yang ret = -EBUSY; 898f82f840SYongqiang Yang 908f82f840SYongqiang Yang return ret; 918f82f840SYongqiang Yang } 928f82f840SYongqiang Yang 938f82f840SYongqiang Yang void ext4_resize_end(struct super_block *sb) 948f82f840SYongqiang Yang { 959549a168STheodore Ts'o clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags); 964e857c58SPeter Zijlstra smp_mb__after_atomic(); 978f82f840SYongqiang Yang } 988f82f840SYongqiang Yang 9901f795f9SYongqiang Yang static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb, 10001f795f9SYongqiang Yang ext4_group_t group) { 10101f795f9SYongqiang Yang return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) << 10201f795f9SYongqiang Yang EXT4_DESC_PER_BLOCK_BITS(sb); 10301f795f9SYongqiang Yang } 10401f795f9SYongqiang Yang 10501f795f9SYongqiang Yang static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb, 10601f795f9SYongqiang Yang ext4_group_t group) { 10701f795f9SYongqiang Yang group = ext4_meta_bg_first_group(sb, group); 10801f795f9SYongqiang Yang return ext4_group_first_block_no(sb, group); 10901f795f9SYongqiang Yang } 11001f795f9SYongqiang Yang 11101f795f9SYongqiang Yang static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb, 11201f795f9SYongqiang Yang ext4_group_t group) { 11301f795f9SYongqiang Yang ext4_grpblk_t overhead; 11401f795f9SYongqiang Yang overhead = ext4_bg_num_gdb(sb, group); 11501f795f9SYongqiang Yang if (ext4_bg_has_super(sb, group)) 11601f795f9SYongqiang Yang overhead += 1 + 11701f795f9SYongqiang Yang le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 11801f795f9SYongqiang Yang return overhead; 11901f795f9SYongqiang Yang } 12001f795f9SYongqiang Yang 121ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 122ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 123ac27a0ecSDave Kleikamp 124ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 125617ba13bSMingming Cao struct ext4_new_group_data *input) 126ac27a0ecSDave Kleikamp { 127617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 128617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 129bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 130617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 131fd2d4291SAvantika Mathur ext4_group_t group = input->group; 132617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 133b302ef2dSTheodore Ts'o unsigned overhead; 134b302ef2dSTheodore Ts'o ext4_fsblk_t metaend; 135ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 1363a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 137ac27a0ecSDave Kleikamp int err = -EINVAL; 138ac27a0ecSDave Kleikamp 139b302ef2dSTheodore Ts'o if (group != sbi->s_groups_count) { 140b302ef2dSTheodore Ts'o ext4_warning(sb, "Cannot add at group %u (only %u groups)", 141b302ef2dSTheodore Ts'o input->group, sbi->s_groups_count); 142b302ef2dSTheodore Ts'o return -EINVAL; 143b302ef2dSTheodore Ts'o } 144b302ef2dSTheodore Ts'o 145b302ef2dSTheodore Ts'o overhead = ext4_group_overhead_blocks(sb, group); 146b302ef2dSTheodore Ts'o metaend = start + overhead; 147d77147ffSharshads input->free_clusters_count = free_blocks_count = 148ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 149ac27a0ecSDave Kleikamp 150ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 151617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 152ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 153617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 154ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 155ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 156ac27a0ecSDave Kleikamp 1573a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 158b302ef2dSTheodore Ts'o if (offset != 0) 15912062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 160ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 16112062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 162ac27a0ecSDave Kleikamp input->reserved_blocks); 163ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 16412062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 165ac27a0ecSDave Kleikamp input->blocks_count); 166fb265c9cSTheodore Ts'o else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) { 167fb265c9cSTheodore Ts'o err = PTR_ERR(bh); 168fb265c9cSTheodore Ts'o bh = NULL; 16912062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 170ac27a0ecSDave Kleikamp end - 1); 171fb265c9cSTheodore Ts'o } else if (outside(input->block_bitmap, start, end)) 17212062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 1731939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 174ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 17512062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 1761939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 177ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 178ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 17912062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1801939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 181ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 18212062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1831939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 184ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 18512062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 18612062dddSEric Sandeen "(%llu-%llu)", 1871939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1881939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 189ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 19012062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 19112062dddSEric Sandeen "(%llu-%llu)", 1921939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1931939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 194ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 19512062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 1961939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1971939e49aSRandy Dunlap start, metaend - 1); 198ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 19912062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 2001939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 2011939e49aSRandy Dunlap start, metaend - 1); 202ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 203ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 20412062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 20512062dddSEric Sandeen "(%llu-%llu)", 2061939e49aSRandy Dunlap (unsigned long long)input->inode_table, 2071939e49aSRandy Dunlap itend - 1, start, metaend - 1); 208ac27a0ecSDave Kleikamp else 209ac27a0ecSDave Kleikamp err = 0; 210ac27a0ecSDave Kleikamp brelse(bh); 211ac27a0ecSDave Kleikamp 212ac27a0ecSDave Kleikamp return err; 213ac27a0ecSDave Kleikamp } 214ac27a0ecSDave Kleikamp 21528c7bac0SYongqiang Yang /* 21628c7bac0SYongqiang Yang * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex 21728c7bac0SYongqiang Yang * group each time. 21828c7bac0SYongqiang Yang */ 21928c7bac0SYongqiang Yang struct ext4_new_flex_group_data { 22028c7bac0SYongqiang Yang struct ext4_new_group_data *groups; /* new_group_data for groups 22128c7bac0SYongqiang Yang in the flex group */ 22228c7bac0SYongqiang Yang __u16 *bg_flags; /* block group flags of groups 22328c7bac0SYongqiang Yang in @groups */ 22428c7bac0SYongqiang Yang ext4_group_t count; /* number of groups in @groups 22528c7bac0SYongqiang Yang */ 22628c7bac0SYongqiang Yang }; 22728c7bac0SYongqiang Yang 22828c7bac0SYongqiang Yang /* 22928c7bac0SYongqiang Yang * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of 23028c7bac0SYongqiang Yang * @flexbg_size. 23128c7bac0SYongqiang Yang * 23228c7bac0SYongqiang Yang * Returns NULL on failure otherwise address of the allocated structure. 23328c7bac0SYongqiang Yang */ 23428c7bac0SYongqiang Yang static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) 23528c7bac0SYongqiang Yang { 23628c7bac0SYongqiang Yang struct ext4_new_flex_group_data *flex_gd; 23728c7bac0SYongqiang Yang 23828c7bac0SYongqiang Yang flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS); 23928c7bac0SYongqiang Yang if (flex_gd == NULL) 24028c7bac0SYongqiang Yang goto out3; 24128c7bac0SYongqiang Yang 24246901760SInsu Yun if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data)) 243967ac8afSHaogang Chen goto out2; 24428c7bac0SYongqiang Yang flex_gd->count = flexbg_size; 24528c7bac0SYongqiang Yang 2466da2ec56SKees Cook flex_gd->groups = kmalloc_array(flexbg_size, 2476da2ec56SKees Cook sizeof(struct ext4_new_group_data), 2486da2ec56SKees Cook GFP_NOFS); 24928c7bac0SYongqiang Yang if (flex_gd->groups == NULL) 25028c7bac0SYongqiang Yang goto out2; 25128c7bac0SYongqiang Yang 2526da2ec56SKees Cook flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16), 2536da2ec56SKees Cook GFP_NOFS); 25428c7bac0SYongqiang Yang if (flex_gd->bg_flags == NULL) 25528c7bac0SYongqiang Yang goto out1; 25628c7bac0SYongqiang Yang 25728c7bac0SYongqiang Yang return flex_gd; 25828c7bac0SYongqiang Yang 25928c7bac0SYongqiang Yang out1: 26028c7bac0SYongqiang Yang kfree(flex_gd->groups); 26128c7bac0SYongqiang Yang out2: 26228c7bac0SYongqiang Yang kfree(flex_gd); 26328c7bac0SYongqiang Yang out3: 26428c7bac0SYongqiang Yang return NULL; 26528c7bac0SYongqiang Yang } 26628c7bac0SYongqiang Yang 26728c7bac0SYongqiang Yang static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd) 26828c7bac0SYongqiang Yang { 26928c7bac0SYongqiang Yang kfree(flex_gd->bg_flags); 27028c7bac0SYongqiang Yang kfree(flex_gd->groups); 27128c7bac0SYongqiang Yang kfree(flex_gd); 27228c7bac0SYongqiang Yang } 27328c7bac0SYongqiang Yang 2743fbea4b3SYongqiang Yang /* 2753fbea4b3SYongqiang Yang * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps 2763fbea4b3SYongqiang Yang * and inode tables for a flex group. 2773fbea4b3SYongqiang Yang * 2783fbea4b3SYongqiang Yang * This function is used by 64bit-resize. Note that this function allocates 2793fbea4b3SYongqiang Yang * group tables from the 1st group of groups contained by @flexgd, which may 2803fbea4b3SYongqiang Yang * be a partial of a flex group. 2813fbea4b3SYongqiang Yang * 2823fbea4b3SYongqiang Yang * @sb: super block of fs to which the groups belongs 28303c1c290SYongqiang Yang * 28403c1c290SYongqiang Yang * Returns 0 on a successful allocation of the metadata blocks in the 28503c1c290SYongqiang Yang * block group. 2863fbea4b3SYongqiang Yang */ 28703c1c290SYongqiang Yang static int ext4_alloc_group_tables(struct super_block *sb, 2883fbea4b3SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 2893fbea4b3SYongqiang Yang int flexbg_size) 2903fbea4b3SYongqiang Yang { 2913fbea4b3SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 2923fbea4b3SYongqiang Yang ext4_fsblk_t start_blk; 2933fbea4b3SYongqiang Yang ext4_fsblk_t last_blk; 2943fbea4b3SYongqiang Yang ext4_group_t src_group; 2953fbea4b3SYongqiang Yang ext4_group_t bb_index = 0; 2963fbea4b3SYongqiang Yang ext4_group_t ib_index = 0; 2973fbea4b3SYongqiang Yang ext4_group_t it_index = 0; 2983fbea4b3SYongqiang Yang ext4_group_t group; 2993fbea4b3SYongqiang Yang ext4_group_t last_group; 3003fbea4b3SYongqiang Yang unsigned overhead; 301b93c9535STheodore Ts'o __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0; 302d77147ffSharshads int i; 3033fbea4b3SYongqiang Yang 3043fbea4b3SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 3053fbea4b3SYongqiang Yang 3063fbea4b3SYongqiang Yang src_group = group_data[0].group; 3073fbea4b3SYongqiang Yang last_group = src_group + flex_gd->count - 1; 3083fbea4b3SYongqiang Yang 3093fbea4b3SYongqiang Yang BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) != 3103fbea4b3SYongqiang Yang (last_group & ~(flexbg_size - 1)))); 3113fbea4b3SYongqiang Yang next_group: 3123fbea4b3SYongqiang Yang group = group_data[0].group; 31303c1c290SYongqiang Yang if (src_group >= group_data[0].group + flex_gd->count) 31403c1c290SYongqiang Yang return -ENOSPC; 3153fbea4b3SYongqiang Yang start_blk = ext4_group_first_block_no(sb, src_group); 3163fbea4b3SYongqiang Yang last_blk = start_blk + group_data[src_group - group].blocks_count; 3173fbea4b3SYongqiang Yang 31801f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 3193fbea4b3SYongqiang Yang 3203fbea4b3SYongqiang Yang start_blk += overhead; 3213fbea4b3SYongqiang Yang 3223fbea4b3SYongqiang Yang /* We collect contiguous blocks as much as possible. */ 3233fbea4b3SYongqiang Yang src_group++; 32401f795f9SYongqiang Yang for (; src_group <= last_group; src_group++) { 32501f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 326b93c9535STheodore Ts'o if (overhead == 0) 3273fbea4b3SYongqiang Yang last_blk += group_data[src_group - group].blocks_count; 3283fbea4b3SYongqiang Yang else 3293fbea4b3SYongqiang Yang break; 33001f795f9SYongqiang Yang } 3313fbea4b3SYongqiang Yang 3323fbea4b3SYongqiang Yang /* Allocate block bitmaps */ 3333fbea4b3SYongqiang Yang for (; bb_index < flex_gd->count; bb_index++) { 3343fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3353fbea4b3SYongqiang Yang goto next_group; 3363fbea4b3SYongqiang Yang group_data[bb_index].block_bitmap = start_blk++; 337bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3383fbea4b3SYongqiang Yang group -= group_data[0].group; 339d77147ffSharshads group_data[group].mdata_blocks++; 340b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3413fbea4b3SYongqiang Yang } 3423fbea4b3SYongqiang Yang 3433fbea4b3SYongqiang Yang /* Allocate inode bitmaps */ 3443fbea4b3SYongqiang Yang for (; ib_index < flex_gd->count; ib_index++) { 3453fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3463fbea4b3SYongqiang Yang goto next_group; 3473fbea4b3SYongqiang Yang group_data[ib_index].inode_bitmap = start_blk++; 348bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3493fbea4b3SYongqiang Yang group -= group_data[0].group; 350d77147ffSharshads group_data[group].mdata_blocks++; 351b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3523fbea4b3SYongqiang Yang } 3533fbea4b3SYongqiang Yang 3543fbea4b3SYongqiang Yang /* Allocate inode tables */ 3553fbea4b3SYongqiang Yang for (; it_index < flex_gd->count; it_index++) { 356b93c9535STheodore Ts'o unsigned int itb = EXT4_SB(sb)->s_itb_per_group; 357b93c9535STheodore Ts'o ext4_fsblk_t next_group_start; 358b93c9535STheodore Ts'o 359b93c9535STheodore Ts'o if (start_blk + itb > last_blk) 3603fbea4b3SYongqiang Yang goto next_group; 3613fbea4b3SYongqiang Yang group_data[it_index].inode_table = start_blk; 362b93c9535STheodore Ts'o group = ext4_get_group_number(sb, start_blk); 363b93c9535STheodore Ts'o next_group_start = ext4_group_first_block_no(sb, group + 1); 3643fbea4b3SYongqiang Yang group -= group_data[0].group; 3653fbea4b3SYongqiang Yang 366b93c9535STheodore Ts'o if (start_blk + itb > next_group_start) { 367b93c9535STheodore Ts'o flex_gd->bg_flags[group + 1] &= uninit_mask; 368b93c9535STheodore Ts'o overhead = start_blk + itb - next_group_start; 369d77147ffSharshads group_data[group + 1].mdata_blocks += overhead; 370b93c9535STheodore Ts'o itb -= overhead; 371b93c9535STheodore Ts'o } 372b93c9535STheodore Ts'o 373d77147ffSharshads group_data[group].mdata_blocks += itb; 374b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3753fbea4b3SYongqiang Yang start_blk += EXT4_SB(sb)->s_itb_per_group; 3763fbea4b3SYongqiang Yang } 3773fbea4b3SYongqiang Yang 378d77147ffSharshads /* Update free clusters count to exclude metadata blocks */ 379d77147ffSharshads for (i = 0; i < flex_gd->count; i++) { 380d77147ffSharshads group_data[i].free_clusters_count -= 381d77147ffSharshads EXT4_NUM_B2C(EXT4_SB(sb), 382d77147ffSharshads group_data[i].mdata_blocks); 383d77147ffSharshads } 384d77147ffSharshads 3853fbea4b3SYongqiang Yang if (test_opt(sb, DEBUG)) { 3863fbea4b3SYongqiang Yang int i; 3873fbea4b3SYongqiang Yang group = group_data[0].group; 3883fbea4b3SYongqiang Yang 3893fbea4b3SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: adding a flex group with " 3903fbea4b3SYongqiang Yang "%d groups, flexbg size is %d:\n", flex_gd->count, 3913fbea4b3SYongqiang Yang flexbg_size); 3923fbea4b3SYongqiang Yang 3933fbea4b3SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 394d77147ffSharshads ext4_debug( 395d77147ffSharshads "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n", 3963fbea4b3SYongqiang Yang ext4_bg_has_super(sb, group + i) ? "normal" : 3973fbea4b3SYongqiang Yang "no-super", group + i, 3983fbea4b3SYongqiang Yang group_data[i].blocks_count, 399d77147ffSharshads group_data[i].free_clusters_count, 400d77147ffSharshads group_data[i].mdata_blocks); 4013fbea4b3SYongqiang Yang } 4023fbea4b3SYongqiang Yang } 40303c1c290SYongqiang Yang return 0; 4043fbea4b3SYongqiang Yang } 4053fbea4b3SYongqiang Yang 406ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 407617ba13bSMingming Cao ext4_fsblk_t blk) 408ac27a0ecSDave Kleikamp { 409ac27a0ecSDave Kleikamp struct buffer_head *bh; 410ac27a0ecSDave Kleikamp int err; 411ac27a0ecSDave Kleikamp 412ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 413aebf0243SWang Shilong if (unlikely(!bh)) 414860d21e2STheodore Ts'o return ERR_PTR(-ENOMEM); 4155d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 416617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 417ac27a0ecSDave Kleikamp brelse(bh); 418ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 419ac27a0ecSDave Kleikamp } else { 420ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 421ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 422ac27a0ecSDave Kleikamp } 423ac27a0ecSDave Kleikamp 424ac27a0ecSDave Kleikamp return bh; 425ac27a0ecSDave Kleikamp } 426ac27a0ecSDave Kleikamp 42783448bdfSJan Kara static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits) 42883448bdfSJan Kara { 42983448bdfSJan Kara return ext4_journal_ensure_credits_fn(handle, credits, 43083448bdfSJan Kara EXT4_MAX_TRANS_DATA, 0, 0); 43183448bdfSJan Kara } 43283448bdfSJan Kara 433ac27a0ecSDave Kleikamp /* 434d77147ffSharshads * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used. 43533afdcc5SYongqiang Yang * 43633afdcc5SYongqiang Yang * Helper function for ext4_setup_new_group_blocks() which set . 43733afdcc5SYongqiang Yang * 43833afdcc5SYongqiang Yang * @sb: super block 43933afdcc5SYongqiang Yang * @handle: journal handle 44033afdcc5SYongqiang Yang * @flex_gd: flex group data 44133afdcc5SYongqiang Yang */ 44233afdcc5SYongqiang Yang static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle, 44333afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 444d77147ffSharshads ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster) 44533afdcc5SYongqiang Yang { 446d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 447d77147ffSharshads ext4_group_t count = last_cluster - first_cluster + 1; 44833afdcc5SYongqiang Yang ext4_group_t count2; 44933afdcc5SYongqiang Yang 450d77147ffSharshads ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster, 451d77147ffSharshads last_cluster); 452d77147ffSharshads for (count2 = count; count > 0; 453d77147ffSharshads count -= count2, first_cluster += count2) { 45433afdcc5SYongqiang Yang ext4_fsblk_t start; 45533afdcc5SYongqiang Yang struct buffer_head *bh; 45633afdcc5SYongqiang Yang ext4_group_t group; 45733afdcc5SYongqiang Yang int err; 45833afdcc5SYongqiang Yang 459d77147ffSharshads group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster)); 460d77147ffSharshads start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group)); 46133afdcc5SYongqiang Yang group -= flex_gd->groups[0].group; 46233afdcc5SYongqiang Yang 463d77147ffSharshads count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start); 46433afdcc5SYongqiang Yang if (count2 > count) 46533afdcc5SYongqiang Yang count2 = count; 46633afdcc5SYongqiang Yang 46733afdcc5SYongqiang Yang if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) { 46833afdcc5SYongqiang Yang BUG_ON(flex_gd->count > 1); 46933afdcc5SYongqiang Yang continue; 47033afdcc5SYongqiang Yang } 47133afdcc5SYongqiang Yang 47283448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 473a4130367SJan Kara if (err < 0) 47433afdcc5SYongqiang Yang return err; 47533afdcc5SYongqiang Yang 47633afdcc5SYongqiang Yang bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap); 477aebf0243SWang Shilong if (unlikely(!bh)) 478860d21e2STheodore Ts'o return -ENOMEM; 47933afdcc5SYongqiang Yang 4805d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 48133afdcc5SYongqiang Yang err = ext4_journal_get_write_access(handle, bh); 482cea57941SVasily Averin if (err) { 483cea57941SVasily Averin brelse(bh); 48433afdcc5SYongqiang Yang return err; 485cea57941SVasily Averin } 486d77147ffSharshads ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", 487d77147ffSharshads first_cluster, first_cluster - start, count2); 488d77147ffSharshads ext4_set_bits(bh->b_data, first_cluster - start, count2); 48933afdcc5SYongqiang Yang 49033afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 491cea57941SVasily Averin brelse(bh); 49233afdcc5SYongqiang Yang if (unlikely(err)) 49333afdcc5SYongqiang Yang return err; 49433afdcc5SYongqiang Yang } 49533afdcc5SYongqiang Yang 49633afdcc5SYongqiang Yang return 0; 49733afdcc5SYongqiang Yang } 49833afdcc5SYongqiang Yang 49933afdcc5SYongqiang Yang /* 50033afdcc5SYongqiang Yang * Set up the block and inode bitmaps, and the inode table for the new groups. 50133afdcc5SYongqiang Yang * This doesn't need to be part of the main transaction, since we are only 50233afdcc5SYongqiang Yang * changing blocks outside the actual filesystem. We still do journaling to 50333afdcc5SYongqiang Yang * ensure the recovery is correct in case of a failure just after resize. 50433afdcc5SYongqiang Yang * If any part of this fails, we simply abort the resize. 50533afdcc5SYongqiang Yang * 50633afdcc5SYongqiang Yang * setup_new_flex_group_blocks handles a flex group as follow: 50733afdcc5SYongqiang Yang * 1. copy super block and GDT, and initialize group tables if necessary. 50833afdcc5SYongqiang Yang * In this step, we only set bits in blocks bitmaps for blocks taken by 50933afdcc5SYongqiang Yang * super block and GDT. 51033afdcc5SYongqiang Yang * 2. allocate group tables in block bitmaps, that is, set bits in block 51133afdcc5SYongqiang Yang * bitmap for blocks taken by group tables. 51233afdcc5SYongqiang Yang */ 51333afdcc5SYongqiang Yang static int setup_new_flex_group_blocks(struct super_block *sb, 51433afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 51533afdcc5SYongqiang Yang { 51633afdcc5SYongqiang Yang int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group}; 51733afdcc5SYongqiang Yang ext4_fsblk_t start; 51833afdcc5SYongqiang Yang ext4_fsblk_t block; 51933afdcc5SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 52033afdcc5SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 52133afdcc5SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 52233afdcc5SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 52333afdcc5SYongqiang Yang handle_t *handle; 52433afdcc5SYongqiang Yang ext4_group_t group, count; 52533afdcc5SYongqiang Yang struct buffer_head *bh = NULL; 52633afdcc5SYongqiang Yang int reserved_gdb, i, j, err = 0, err2; 52701f795f9SYongqiang Yang int meta_bg; 52833afdcc5SYongqiang Yang 52933afdcc5SYongqiang Yang BUG_ON(!flex_gd->count || !group_data || 53033afdcc5SYongqiang Yang group_data[0].group != sbi->s_groups_count); 53133afdcc5SYongqiang Yang 53233afdcc5SYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 533e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 53433afdcc5SYongqiang Yang 53533afdcc5SYongqiang Yang /* This transaction may be extended/restarted along the way */ 5369924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 53733afdcc5SYongqiang Yang if (IS_ERR(handle)) 53833afdcc5SYongqiang Yang return PTR_ERR(handle); 53933afdcc5SYongqiang Yang 54033afdcc5SYongqiang Yang group = group_data[0].group; 54133afdcc5SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group++) { 54233afdcc5SYongqiang Yang unsigned long gdblocks; 54301f795f9SYongqiang Yang ext4_grpblk_t overhead; 54433afdcc5SYongqiang Yang 54533afdcc5SYongqiang Yang gdblocks = ext4_bg_num_gdb(sb, group); 54633afdcc5SYongqiang Yang start = ext4_group_first_block_no(sb, group); 54733afdcc5SYongqiang Yang 54801f795f9SYongqiang Yang if (meta_bg == 0 && !ext4_bg_has_super(sb, group)) 5496df935adSYongqiang Yang goto handle_itb; 5506df935adSYongqiang Yang 55101f795f9SYongqiang Yang if (meta_bg == 1) { 55201f795f9SYongqiang Yang ext4_group_t first_group; 55301f795f9SYongqiang Yang first_group = ext4_meta_bg_first_group(sb, group); 55401f795f9SYongqiang Yang if (first_group != group + 1 && 55501f795f9SYongqiang Yang first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1) 55601f795f9SYongqiang Yang goto handle_itb; 55701f795f9SYongqiang Yang } 55801f795f9SYongqiang Yang 55901f795f9SYongqiang Yang block = start + ext4_bg_has_super(sb, group); 56033afdcc5SYongqiang Yang /* Copy all of the GDT blocks into the backup in this group */ 56101f795f9SYongqiang Yang for (j = 0; j < gdblocks; j++, block++) { 56233afdcc5SYongqiang Yang struct buffer_head *gdb; 56333afdcc5SYongqiang Yang 56433afdcc5SYongqiang Yang ext4_debug("update backup group %#04llx\n", block); 56583448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 566a4130367SJan Kara if (err < 0) 56733afdcc5SYongqiang Yang goto out; 56833afdcc5SYongqiang Yang 56933afdcc5SYongqiang Yang gdb = sb_getblk(sb, block); 570aebf0243SWang Shilong if (unlikely(!gdb)) { 571860d21e2STheodore Ts'o err = -ENOMEM; 57233afdcc5SYongqiang Yang goto out; 57333afdcc5SYongqiang Yang } 57433afdcc5SYongqiang Yang 5755d601255Sliang xie BUFFER_TRACE(gdb, "get_write_access"); 57633afdcc5SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb); 57733afdcc5SYongqiang Yang if (err) { 57833afdcc5SYongqiang Yang brelse(gdb); 57933afdcc5SYongqiang Yang goto out; 58033afdcc5SYongqiang Yang } 5811d0c3924STheodore Ts'o memcpy(gdb->b_data, sbi_array_rcu_deref(sbi, 5821d0c3924STheodore Ts'o s_group_desc, j)->b_data, gdb->b_size); 58333afdcc5SYongqiang Yang set_buffer_uptodate(gdb); 58433afdcc5SYongqiang Yang 58533afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb); 58633afdcc5SYongqiang Yang if (unlikely(err)) { 58733afdcc5SYongqiang Yang brelse(gdb); 58833afdcc5SYongqiang Yang goto out; 58933afdcc5SYongqiang Yang } 59033afdcc5SYongqiang Yang brelse(gdb); 59133afdcc5SYongqiang Yang } 59233afdcc5SYongqiang Yang 59333afdcc5SYongqiang Yang /* Zero out all of the reserved backup group descriptor 59433afdcc5SYongqiang Yang * table blocks 59533afdcc5SYongqiang Yang */ 59633afdcc5SYongqiang Yang if (ext4_bg_has_super(sb, group)) { 59733afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, gdblocks + start + 1, 59833afdcc5SYongqiang Yang reserved_gdb, GFP_NOFS); 59933afdcc5SYongqiang Yang if (err) 60033afdcc5SYongqiang Yang goto out; 60133afdcc5SYongqiang Yang } 60233afdcc5SYongqiang Yang 6036df935adSYongqiang Yang handle_itb: 60433afdcc5SYongqiang Yang /* Initialize group tables of the grop @group */ 60533afdcc5SYongqiang Yang if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED)) 60633afdcc5SYongqiang Yang goto handle_bb; 60733afdcc5SYongqiang Yang 60833afdcc5SYongqiang Yang /* Zero out all of the inode table blocks */ 60933afdcc5SYongqiang Yang block = group_data[i].inode_table; 61033afdcc5SYongqiang Yang ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 61133afdcc5SYongqiang Yang block, sbi->s_itb_per_group); 61233afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, 61333afdcc5SYongqiang Yang GFP_NOFS); 61433afdcc5SYongqiang Yang if (err) 61533afdcc5SYongqiang Yang goto out; 61633afdcc5SYongqiang Yang 61733afdcc5SYongqiang Yang handle_bb: 61833afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT) 61933afdcc5SYongqiang Yang goto handle_ib; 62033afdcc5SYongqiang Yang 62133afdcc5SYongqiang Yang /* Initialize block bitmap of the @group */ 62233afdcc5SYongqiang Yang block = group_data[i].block_bitmap; 62383448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 624a4130367SJan Kara if (err < 0) 62533afdcc5SYongqiang Yang goto out; 62633afdcc5SYongqiang Yang 62733afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 62833afdcc5SYongqiang Yang if (IS_ERR(bh)) { 62933afdcc5SYongqiang Yang err = PTR_ERR(bh); 63033afdcc5SYongqiang Yang goto out; 63133afdcc5SYongqiang Yang } 63201f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group); 63301f795f9SYongqiang Yang if (overhead != 0) { 63433afdcc5SYongqiang Yang ext4_debug("mark backup superblock %#04llx (+0)\n", 63533afdcc5SYongqiang Yang start); 636d77147ffSharshads ext4_set_bits(bh->b_data, 0, 637d77147ffSharshads EXT4_NUM_B2C(sbi, overhead)); 63833afdcc5SYongqiang Yang } 639d77147ffSharshads ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count), 64033afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 64133afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6429e402893SVasily Averin brelse(bh); 64333afdcc5SYongqiang Yang if (err) 64433afdcc5SYongqiang Yang goto out; 64533afdcc5SYongqiang Yang 64633afdcc5SYongqiang Yang handle_ib: 64733afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_INODE_UNINIT) 64833afdcc5SYongqiang Yang continue; 64933afdcc5SYongqiang Yang 65033afdcc5SYongqiang Yang /* Initialize inode bitmap of the @group */ 65133afdcc5SYongqiang Yang block = group_data[i].inode_bitmap; 65283448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 653a4130367SJan Kara if (err < 0) 65433afdcc5SYongqiang Yang goto out; 65533afdcc5SYongqiang Yang /* Mark unused entries in inode bitmap used */ 65633afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 65733afdcc5SYongqiang Yang if (IS_ERR(bh)) { 65833afdcc5SYongqiang Yang err = PTR_ERR(bh); 65933afdcc5SYongqiang Yang goto out; 66033afdcc5SYongqiang Yang } 66133afdcc5SYongqiang Yang 66233afdcc5SYongqiang Yang ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 66333afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 66433afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6659e402893SVasily Averin brelse(bh); 66633afdcc5SYongqiang Yang if (err) 66733afdcc5SYongqiang Yang goto out; 66833afdcc5SYongqiang Yang } 66933afdcc5SYongqiang Yang 67033afdcc5SYongqiang Yang /* Mark group tables in block bitmap */ 67133afdcc5SYongqiang Yang for (j = 0; j < GROUP_TABLE_COUNT; j++) { 67233afdcc5SYongqiang Yang count = group_table_count[j]; 67333afdcc5SYongqiang Yang start = (&group_data[0].block_bitmap)[j]; 67433afdcc5SYongqiang Yang block = start; 67533afdcc5SYongqiang Yang for (i = 1; i < flex_gd->count; i++) { 67633afdcc5SYongqiang Yang block += group_table_count[j]; 67733afdcc5SYongqiang Yang if (block == (&group_data[i].block_bitmap)[j]) { 67833afdcc5SYongqiang Yang count += group_table_count[j]; 67933afdcc5SYongqiang Yang continue; 68033afdcc5SYongqiang Yang } 68133afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 682d77147ffSharshads flex_gd, 683d77147ffSharshads EXT4_B2C(sbi, start), 684d77147ffSharshads EXT4_B2C(sbi, 685d77147ffSharshads start + count 686d77147ffSharshads - 1)); 68733afdcc5SYongqiang Yang if (err) 68833afdcc5SYongqiang Yang goto out; 68933afdcc5SYongqiang Yang count = group_table_count[j]; 690b93c9535STheodore Ts'o start = (&group_data[i].block_bitmap)[j]; 69133afdcc5SYongqiang Yang block = start; 69233afdcc5SYongqiang Yang } 69333afdcc5SYongqiang Yang 69433afdcc5SYongqiang Yang if (count) { 69533afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 696d77147ffSharshads flex_gd, 697d77147ffSharshads EXT4_B2C(sbi, start), 698d77147ffSharshads EXT4_B2C(sbi, 699d77147ffSharshads start + count 700d77147ffSharshads - 1)); 70133afdcc5SYongqiang Yang if (err) 70233afdcc5SYongqiang Yang goto out; 70333afdcc5SYongqiang Yang } 70433afdcc5SYongqiang Yang } 70533afdcc5SYongqiang Yang 70633afdcc5SYongqiang Yang out: 70733afdcc5SYongqiang Yang err2 = ext4_journal_stop(handle); 70833afdcc5SYongqiang Yang if (err2 && !err) 70933afdcc5SYongqiang Yang err = err2; 71033afdcc5SYongqiang Yang 71133afdcc5SYongqiang Yang return err; 71233afdcc5SYongqiang Yang } 71333afdcc5SYongqiang Yang 71433afdcc5SYongqiang Yang /* 715ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 716617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 717ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 718ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 719ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 720ac27a0ecSDave Kleikamp */ 721617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 722ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 723ac27a0ecSDave Kleikamp { 724ac27a0ecSDave Kleikamp unsigned *min = three; 725ac27a0ecSDave Kleikamp int mult = 3; 726ac27a0ecSDave Kleikamp unsigned ret; 727ac27a0ecSDave Kleikamp 728e2b911c5SDarrick J. Wong if (!ext4_has_feature_sparse_super(sb)) { 729ac27a0ecSDave Kleikamp ret = *min; 730ac27a0ecSDave Kleikamp *min += 1; 731ac27a0ecSDave Kleikamp return ret; 732ac27a0ecSDave Kleikamp } 733ac27a0ecSDave Kleikamp 734ac27a0ecSDave Kleikamp if (*five < *min) { 735ac27a0ecSDave Kleikamp min = five; 736ac27a0ecSDave Kleikamp mult = 5; 737ac27a0ecSDave Kleikamp } 738ac27a0ecSDave Kleikamp if (*seven < *min) { 739ac27a0ecSDave Kleikamp min = seven; 740ac27a0ecSDave Kleikamp mult = 7; 741ac27a0ecSDave Kleikamp } 742ac27a0ecSDave Kleikamp 743ac27a0ecSDave Kleikamp ret = *min; 744ac27a0ecSDave Kleikamp *min *= mult; 745ac27a0ecSDave Kleikamp 746ac27a0ecSDave Kleikamp return ret; 747ac27a0ecSDave Kleikamp } 748ac27a0ecSDave Kleikamp 749ac27a0ecSDave Kleikamp /* 750ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 751ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 752ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 753ac27a0ecSDave Kleikamp */ 754ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 755c72df9f9SYongqiang Yang ext4_group_t end, 756ac27a0ecSDave Kleikamp struct buffer_head *primary) 757ac27a0ecSDave Kleikamp { 758617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 759ac27a0ecSDave Kleikamp unsigned three = 1; 760ac27a0ecSDave Kleikamp unsigned five = 5; 761ac27a0ecSDave Kleikamp unsigned seven = 7; 762ac27a0ecSDave Kleikamp unsigned grp; 763ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 764ac27a0ecSDave Kleikamp int gdbackups = 0; 765ac27a0ecSDave Kleikamp 766617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 767bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 768bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 76912062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 7702ae02107SMingming Cao " missing grp %d (%llu)", 771ac27a0ecSDave Kleikamp blk, grp, 772bd81d8eeSLaurent Vivier grp * 773bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 774bd81d8eeSLaurent Vivier blk); 775ac27a0ecSDave Kleikamp return -EINVAL; 776ac27a0ecSDave Kleikamp } 777617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 778ac27a0ecSDave Kleikamp return -EFBIG; 779ac27a0ecSDave Kleikamp } 780ac27a0ecSDave Kleikamp 781ac27a0ecSDave Kleikamp return gdbackups; 782ac27a0ecSDave Kleikamp } 783ac27a0ecSDave Kleikamp 784ac27a0ecSDave Kleikamp /* 785ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 786ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 787ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 788ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 789ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 790ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 791ac27a0ecSDave Kleikamp * 792ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 793ac27a0ecSDave Kleikamp * 794ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 795ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 796ac27a0ecSDave Kleikamp */ 797ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 7982f919710SYongqiang Yang ext4_group_t group) 799ac27a0ecSDave Kleikamp { 800ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 801617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 8022f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 803617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 804fb265c9cSTheodore Ts'o struct buffer_head **o_group_desc, **n_group_desc = NULL; 805fb265c9cSTheodore Ts'o struct buffer_head *dind = NULL; 806fb265c9cSTheodore Ts'o struct buffer_head *gdb_bh = NULL; 807ac27a0ecSDave Kleikamp int gdbackups; 808fb265c9cSTheodore Ts'o struct ext4_iloc iloc = { .bh = NULL }; 809ac27a0ecSDave Kleikamp __le32 *data; 810ac27a0ecSDave Kleikamp int err; 811ac27a0ecSDave Kleikamp 812ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 813ac27a0ecSDave Kleikamp printk(KERN_DEBUG 814617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 815ac27a0ecSDave Kleikamp gdb_num); 816ac27a0ecSDave Kleikamp 817fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 818fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 819fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 820ac27a0ecSDave Kleikamp 821c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, gdb_bh); 8222f919710SYongqiang Yang if (gdbackups < 0) { 823ac27a0ecSDave Kleikamp err = gdbackups; 824fb265c9cSTheodore Ts'o goto errout; 825ac27a0ecSDave Kleikamp } 826ac27a0ecSDave Kleikamp 827617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 828fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 829fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 830fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 831fb265c9cSTheodore Ts'o dind = NULL; 832fb265c9cSTheodore Ts'o goto errout; 833ac27a0ecSDave Kleikamp } 834ac27a0ecSDave Kleikamp 835ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 836617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 83712062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 8382f919710SYongqiang Yang group, gdblock); 839ac27a0ecSDave Kleikamp err = -EINVAL; 840fb265c9cSTheodore Ts'o goto errout; 841ac27a0ecSDave Kleikamp } 842ac27a0ecSDave Kleikamp 8435d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 844b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 845b4097142STheodore Ts'o if (unlikely(err)) 846fb265c9cSTheodore Ts'o goto errout; 847ac27a0ecSDave Kleikamp 8485d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 8492f919710SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 850b4097142STheodore Ts'o if (unlikely(err)) 851fb265c9cSTheodore Ts'o goto errout; 852ac27a0ecSDave Kleikamp 8535d601255Sliang xie BUFFER_TRACE(dind, "get_write_access"); 854b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, dind); 855c9e87161SDinghao Liu if (unlikely(err)) { 856b4097142STheodore Ts'o ext4_std_error(sb, err); 857c9e87161SDinghao Liu goto errout; 858c9e87161SDinghao Liu } 859ac27a0ecSDave Kleikamp 860617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 861b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 862b4097142STheodore Ts'o if (unlikely(err)) 863fb265c9cSTheodore Ts'o goto errout; 864ac27a0ecSDave Kleikamp 86571b565ceSTheodore Ts'o n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 86671b565ceSTheodore Ts'o GFP_KERNEL); 867ac27a0ecSDave Kleikamp if (!n_group_desc) { 868ac27a0ecSDave Kleikamp err = -ENOMEM; 869f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 870f18a5f21STheodore Ts'o gdb_num + 1); 871fb265c9cSTheodore Ts'o goto errout; 872ac27a0ecSDave Kleikamp } 873ac27a0ecSDave Kleikamp 874ac27a0ecSDave Kleikamp /* 875ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 876ac27a0ecSDave Kleikamp * 877ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 878ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 879ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 880ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 881ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 882ac27a0ecSDave Kleikamp */ 883617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 884b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 885b4097142STheodore Ts'o if (unlikely(err)) { 886b4097142STheodore Ts'o ext4_std_error(sb, err); 887fb265c9cSTheodore Ts'o goto errout; 888b4097142STheodore Ts'o } 889d77147ffSharshads inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 890d77147ffSharshads (9 - EXT4_SB(sb)->s_cluster_bits); 891617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 8922f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 8932f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 894b4097142STheodore Ts'o if (unlikely(err)) { 895b4097142STheodore Ts'o ext4_std_error(sb, err); 8968c380ab4SPan Bian iloc.bh = NULL; 897fb265c9cSTheodore Ts'o goto errout; 898b4097142STheodore Ts'o } 899b4097142STheodore Ts'o brelse(dind); 900ac27a0ecSDave Kleikamp 9011d0c3924STheodore Ts'o rcu_read_lock(); 9021d0c3924STheodore Ts'o o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc); 903ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 904617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 9051d0c3924STheodore Ts'o rcu_read_unlock(); 9062f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 9071d0c3924STheodore Ts'o rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc); 908617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 9091d0c3924STheodore Ts'o ext4_kvfree_array_rcu(o_group_desc); 910ac27a0ecSDave Kleikamp 91105c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 912e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 91305c2c00fSJan Kara ext4_superblock_csum_set(sb); 91405c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 915a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 916b4097142STheodore Ts'o if (err) 917b4097142STheodore Ts'o ext4_std_error(sb, err); 918b4097142STheodore Ts'o return err; 919fb265c9cSTheodore Ts'o errout: 920b93b41d4SAl Viro kvfree(n_group_desc); 921ac27a0ecSDave Kleikamp brelse(iloc.bh); 922ac27a0ecSDave Kleikamp brelse(dind); 9232f919710SYongqiang Yang brelse(gdb_bh); 924ac27a0ecSDave Kleikamp 925617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 926ac27a0ecSDave Kleikamp return err; 927ac27a0ecSDave Kleikamp } 928ac27a0ecSDave Kleikamp 929ac27a0ecSDave Kleikamp /* 93001f795f9SYongqiang Yang * add_new_gdb_meta_bg is the sister of add_new_gdb. 93101f795f9SYongqiang Yang */ 93201f795f9SYongqiang Yang static int add_new_gdb_meta_bg(struct super_block *sb, 93301f795f9SYongqiang Yang handle_t *handle, ext4_group_t group) { 93401f795f9SYongqiang Yang ext4_fsblk_t gdblock; 93501f795f9SYongqiang Yang struct buffer_head *gdb_bh; 93601f795f9SYongqiang Yang struct buffer_head **o_group_desc, **n_group_desc; 93701f795f9SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 93801f795f9SYongqiang Yang int err; 93901f795f9SYongqiang Yang 94001f795f9SYongqiang Yang gdblock = ext4_meta_bg_first_block_no(sb, group) + 94101f795f9SYongqiang Yang ext4_bg_has_super(sb, group); 942fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 943fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 944fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 94571b565ceSTheodore Ts'o n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 94671b565ceSTheodore Ts'o GFP_KERNEL); 94701f795f9SYongqiang Yang if (!n_group_desc) { 94861a9c11eSVasily Averin brelse(gdb_bh); 94901f795f9SYongqiang Yang err = -ENOMEM; 95001f795f9SYongqiang Yang ext4_warning(sb, "not enough memory for %lu groups", 95101f795f9SYongqiang Yang gdb_num + 1); 95201f795f9SYongqiang Yang return err; 95301f795f9SYongqiang Yang } 95401f795f9SYongqiang Yang 9551d0c3924STheodore Ts'o rcu_read_lock(); 9561d0c3924STheodore Ts'o o_group_desc = rcu_dereference(EXT4_SB(sb)->s_group_desc); 95701f795f9SYongqiang Yang memcpy(n_group_desc, o_group_desc, 95801f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 9591d0c3924STheodore Ts'o rcu_read_unlock(); 96001f795f9SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 961d64264d6SLukas Czerner 962d64264d6SLukas Czerner BUFFER_TRACE(gdb_bh, "get_write_access"); 963d64264d6SLukas Czerner err = ext4_journal_get_write_access(handle, gdb_bh); 964d64264d6SLukas Czerner if (err) { 965d64264d6SLukas Czerner kvfree(n_group_desc); 966d64264d6SLukas Czerner brelse(gdb_bh); 967d64264d6SLukas Czerner return err; 968d64264d6SLukas Czerner } 969d64264d6SLukas Czerner 9701d0c3924STheodore Ts'o rcu_assign_pointer(EXT4_SB(sb)->s_group_desc, n_group_desc); 97101f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count++; 9721d0c3924STheodore Ts'o ext4_kvfree_array_rcu(o_group_desc); 97301f795f9SYongqiang Yang return err; 97401f795f9SYongqiang Yang } 97501f795f9SYongqiang Yang 97601f795f9SYongqiang Yang /* 977ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 978ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 979ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 980ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 981ac27a0ecSDave Kleikamp * 982ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 983ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 984ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 985ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 986ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 987ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 988ac27a0ecSDave Kleikamp */ 989ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 990668f4dc5SYongqiang Yang ext4_group_t group) 991ac27a0ecSDave Kleikamp { 992ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 993617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 994d77147ffSharshads int cluster_bits = EXT4_SB(sb)->s_cluster_bits; 995ac27a0ecSDave Kleikamp struct buffer_head **primary; 996ac27a0ecSDave Kleikamp struct buffer_head *dind; 997617ba13bSMingming Cao struct ext4_iloc iloc; 998617ba13bSMingming Cao ext4_fsblk_t blk; 999ac27a0ecSDave Kleikamp __le32 *data, *end; 1000ac27a0ecSDave Kleikamp int gdbackups = 0; 1001ac27a0ecSDave Kleikamp int res, i; 1002ac27a0ecSDave Kleikamp int err; 1003ac27a0ecSDave Kleikamp 10046da2ec56SKees Cook primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS); 1005ac27a0ecSDave Kleikamp if (!primary) 1006ac27a0ecSDave Kleikamp return -ENOMEM; 1007ac27a0ecSDave Kleikamp 1008617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 1009fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 1010fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 1011fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 1012fb265c9cSTheodore Ts'o dind = NULL; 1013ac27a0ecSDave Kleikamp goto exit_free; 1014ac27a0ecSDave Kleikamp } 1015ac27a0ecSDave Kleikamp 1016617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 101794460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 101894460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 1019617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 1020ac27a0ecSDave Kleikamp 1021ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 1022ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 1023ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 102412062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 1025ac27a0ecSDave Kleikamp " not at offset %ld", 1026ac27a0ecSDave Kleikamp blk, 1027ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 1028ac27a0ecSDave Kleikamp err = -EINVAL; 1029ac27a0ecSDave Kleikamp goto exit_bh; 1030ac27a0ecSDave Kleikamp } 1031fb265c9cSTheodore Ts'o primary[res] = ext4_sb_bread(sb, blk, 0); 1032fb265c9cSTheodore Ts'o if (IS_ERR(primary[res])) { 1033fb265c9cSTheodore Ts'o err = PTR_ERR(primary[res]); 1034fb265c9cSTheodore Ts'o primary[res] = NULL; 1035ac27a0ecSDave Kleikamp goto exit_bh; 1036ac27a0ecSDave Kleikamp } 1037c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, primary[res]); 1038c72df9f9SYongqiang Yang if (gdbackups < 0) { 1039ac27a0ecSDave Kleikamp brelse(primary[res]); 1040ac27a0ecSDave Kleikamp err = gdbackups; 1041ac27a0ecSDave Kleikamp goto exit_bh; 1042ac27a0ecSDave Kleikamp } 1043ac27a0ecSDave Kleikamp if (++data >= end) 1044ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 1045ac27a0ecSDave Kleikamp } 1046ac27a0ecSDave Kleikamp 1047ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 10485d601255Sliang xie BUFFER_TRACE(primary[i], "get_write_access"); 104937be2f59SEric Sandeen if ((err = ext4_journal_get_write_access(handle, primary[i]))) 1050ac27a0ecSDave Kleikamp goto exit_bh; 1051ac27a0ecSDave Kleikamp } 1052ac27a0ecSDave Kleikamp 1053617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 1054ac27a0ecSDave Kleikamp goto exit_bh; 1055ac27a0ecSDave Kleikamp 1056ac27a0ecSDave Kleikamp /* 1057ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 1058ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 1059ac27a0ecSDave Kleikamp */ 1060668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 1061ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 1062ac27a0ecSDave Kleikamp int err2; 1063ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 1064ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 1065ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 1066ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 1067ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 10680390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 1069ac27a0ecSDave Kleikamp if (!err) 1070ac27a0ecSDave Kleikamp err = err2; 1071ac27a0ecSDave Kleikamp } 1072d77147ffSharshads 1073d77147ffSharshads inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits); 1074617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 1075ac27a0ecSDave Kleikamp 1076ac27a0ecSDave Kleikamp exit_bh: 1077ac27a0ecSDave Kleikamp while (--res >= 0) 1078ac27a0ecSDave Kleikamp brelse(primary[res]); 1079ac27a0ecSDave Kleikamp brelse(dind); 1080ac27a0ecSDave Kleikamp 1081ac27a0ecSDave Kleikamp exit_free: 1082ac27a0ecSDave Kleikamp kfree(primary); 1083ac27a0ecSDave Kleikamp 1084ac27a0ecSDave Kleikamp return err; 1085ac27a0ecSDave Kleikamp } 1086ac27a0ecSDave Kleikamp 1087ac27a0ecSDave Kleikamp /* 1088617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 1089ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 1090ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 1091ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 1092ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 1093ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 1094ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 1095ac27a0ecSDave Kleikamp * 109632ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 109732ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 109832ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 109932ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 110032ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 110132ed5058STheodore Ts'o * which changed s_groups_count will backup again. 1102ac27a0ecSDave Kleikamp */ 1103904dad47SAndy Leiserson static void update_backups(struct super_block *sb, sector_t blk_off, char *data, 110401f795f9SYongqiang Yang int size, int meta_bg) 1105ac27a0ecSDave Kleikamp { 1106617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 110701f795f9SYongqiang Yang ext4_group_t last; 1108617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 1109ac27a0ecSDave Kleikamp unsigned three = 1; 1110ac27a0ecSDave Kleikamp unsigned five = 5; 1111ac27a0ecSDave Kleikamp unsigned seven = 7; 111201f795f9SYongqiang Yang ext4_group_t group = 0; 1113ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 1114ac27a0ecSDave Kleikamp handle_t *handle; 1115ac27a0ecSDave Kleikamp int err = 0, err2; 1116ac27a0ecSDave Kleikamp 11179924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 1118ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1119ac27a0ecSDave Kleikamp group = 1; 1120ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 1121ac27a0ecSDave Kleikamp goto exit_err; 1122ac27a0ecSDave Kleikamp } 1123ac27a0ecSDave Kleikamp 112401f795f9SYongqiang Yang if (meta_bg == 0) { 112501f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 112601f795f9SYongqiang Yang last = sbi->s_groups_count; 112701f795f9SYongqiang Yang } else { 1128904dad47SAndy Leiserson group = ext4_get_group_number(sb, blk_off) + 1; 112901f795f9SYongqiang Yang last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2); 113001f795f9SYongqiang Yang } 113101f795f9SYongqiang Yang 113201f795f9SYongqiang Yang while (group < sbi->s_groups_count) { 1133ac27a0ecSDave Kleikamp struct buffer_head *bh; 113401f795f9SYongqiang Yang ext4_fsblk_t backup_block; 1135ac27a0ecSDave Kleikamp 1136ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 113783448bdfSJan Kara err = ext4_resize_ensure_credits_batch(handle, 1); 1138a4130367SJan Kara if (err < 0) 1139ac27a0ecSDave Kleikamp break; 1140ac27a0ecSDave Kleikamp 114101f795f9SYongqiang Yang if (meta_bg == 0) 11429378c676SJan Kara backup_block = ((ext4_fsblk_t)group) * bpg + blk_off; 114301f795f9SYongqiang Yang else 114401f795f9SYongqiang Yang backup_block = (ext4_group_first_block_no(sb, group) + 114501f795f9SYongqiang Yang ext4_bg_has_super(sb, group)); 114601f795f9SYongqiang Yang 114701f795f9SYongqiang Yang bh = sb_getblk(sb, backup_block); 1148aebf0243SWang Shilong if (unlikely(!bh)) { 1149860d21e2STheodore Ts'o err = -ENOMEM; 1150ac27a0ecSDave Kleikamp break; 1151ac27a0ecSDave Kleikamp } 115201f795f9SYongqiang Yang ext4_debug("update metadata backup %llu(+%llu)\n", 115301f795f9SYongqiang Yang backup_block, backup_block - 115401f795f9SYongqiang Yang ext4_group_first_block_no(sb, group)); 11555d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 1156ea0abbb6SVasily Averin if ((err = ext4_journal_get_write_access(handle, bh))) { 1157ea0abbb6SVasily Averin brelse(bh); 1158ac27a0ecSDave Kleikamp break; 1159ea0abbb6SVasily Averin } 1160ac27a0ecSDave Kleikamp lock_buffer(bh); 1161ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 1162ac27a0ecSDave Kleikamp if (rest) 1163ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 1164ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1165ac27a0ecSDave Kleikamp unlock_buffer(bh); 1166b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 1167b4097142STheodore Ts'o if (unlikely(err)) 1168b4097142STheodore Ts'o ext4_std_error(sb, err); 1169ac27a0ecSDave Kleikamp brelse(bh); 117001f795f9SYongqiang Yang 117101f795f9SYongqiang Yang if (meta_bg == 0) 117201f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 117301f795f9SYongqiang Yang else if (group == last) 117401f795f9SYongqiang Yang break; 117501f795f9SYongqiang Yang else 117601f795f9SYongqiang Yang group = last; 1177ac27a0ecSDave Kleikamp } 1178617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 1179ac27a0ecSDave Kleikamp err = err2; 1180ac27a0ecSDave Kleikamp 1181ac27a0ecSDave Kleikamp /* 1182ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 1183ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 1184ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 1185ac27a0ecSDave Kleikamp * 1186ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 1187ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 1188ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 1189ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 1190ac27a0ecSDave Kleikamp */ 1191ac27a0ecSDave Kleikamp exit_err: 1192ac27a0ecSDave Kleikamp if (err) { 119312062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 1194ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 1195617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 1196617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 1197ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 1198ac27a0ecSDave Kleikamp } 1199ac27a0ecSDave Kleikamp } 1200ac27a0ecSDave Kleikamp 1201bb08c1e7SYongqiang Yang /* 1202bb08c1e7SYongqiang Yang * ext4_add_new_descs() adds @count group descriptor of groups 1203bb08c1e7SYongqiang Yang * starting at @group 1204bb08c1e7SYongqiang Yang * 1205bb08c1e7SYongqiang Yang * @handle: journal handle 1206bb08c1e7SYongqiang Yang * @sb: super block 1207bb08c1e7SYongqiang Yang * @group: the group no. of the first group desc to be added 1208bb08c1e7SYongqiang Yang * @resize_inode: the resize inode 1209bb08c1e7SYongqiang Yang * @count: number of group descriptors to be added 1210bb08c1e7SYongqiang Yang */ 1211bb08c1e7SYongqiang Yang static int ext4_add_new_descs(handle_t *handle, struct super_block *sb, 1212bb08c1e7SYongqiang Yang ext4_group_t group, struct inode *resize_inode, 1213bb08c1e7SYongqiang Yang ext4_group_t count) 1214bb08c1e7SYongqiang Yang { 1215bb08c1e7SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1216bb08c1e7SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 1217bb08c1e7SYongqiang Yang struct buffer_head *gdb_bh; 1218bb08c1e7SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 121901f795f9SYongqiang Yang int meta_bg; 1220bb08c1e7SYongqiang Yang 1221e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 1222bb08c1e7SYongqiang Yang for (i = 0; i < count; i++, group++) { 1223bb08c1e7SYongqiang Yang int reserved_gdb = ext4_bg_has_super(sb, group) ? 1224bb08c1e7SYongqiang Yang le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1225bb08c1e7SYongqiang Yang 1226bb08c1e7SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1227bb08c1e7SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1228bb08c1e7SYongqiang Yang 1229bb08c1e7SYongqiang Yang /* 1230bb08c1e7SYongqiang Yang * We will only either add reserved group blocks to a backup group 1231bb08c1e7SYongqiang Yang * or remove reserved blocks for the first group in a new group block. 1232bb08c1e7SYongqiang Yang * Doing both would be mean more complex code, and sane people don't 1233bb08c1e7SYongqiang Yang * use non-sparse filesystems anymore. This is already checked above. 1234bb08c1e7SYongqiang Yang */ 1235bb08c1e7SYongqiang Yang if (gdb_off) { 12361d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 12371d0c3924STheodore Ts'o gdb_num); 12385d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 1239bb08c1e7SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 1240bb08c1e7SYongqiang Yang 1241bb08c1e7SYongqiang Yang if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 1242bb08c1e7SYongqiang Yang err = reserve_backup_gdb(handle, resize_inode, group); 124301f795f9SYongqiang Yang } else if (meta_bg != 0) { 124401f795f9SYongqiang Yang err = add_new_gdb_meta_bg(sb, handle, group); 124501f795f9SYongqiang Yang } else { 1246bb08c1e7SYongqiang Yang err = add_new_gdb(handle, resize_inode, group); 124701f795f9SYongqiang Yang } 1248bb08c1e7SYongqiang Yang if (err) 1249bb08c1e7SYongqiang Yang break; 1250bb08c1e7SYongqiang Yang } 1251bb08c1e7SYongqiang Yang return err; 1252bb08c1e7SYongqiang Yang } 1253bb08c1e7SYongqiang Yang 125441a246d1SDarrick J. Wong static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block) 125541a246d1SDarrick J. Wong { 125641a246d1SDarrick J. Wong struct buffer_head *bh = sb_getblk(sb, block); 1257aebf0243SWang Shilong if (unlikely(!bh)) 125841a246d1SDarrick J. Wong return NULL; 12597f1468d1SDmitry Monakhov if (!bh_uptodate_or_lock(bh)) { 12602d069c08Szhangyi (F) if (ext4_read_bh(bh, 0, NULL) < 0) { 126141a246d1SDarrick J. Wong brelse(bh); 126241a246d1SDarrick J. Wong return NULL; 126341a246d1SDarrick J. Wong } 12647f1468d1SDmitry Monakhov } 126541a246d1SDarrick J. Wong 126641a246d1SDarrick J. Wong return bh; 126741a246d1SDarrick J. Wong } 126841a246d1SDarrick J. Wong 126941a246d1SDarrick J. Wong static int ext4_set_bitmap_checksums(struct super_block *sb, 127041a246d1SDarrick J. Wong ext4_group_t group, 127141a246d1SDarrick J. Wong struct ext4_group_desc *gdp, 127241a246d1SDarrick J. Wong struct ext4_new_group_data *group_data) 127341a246d1SDarrick J. Wong { 127441a246d1SDarrick J. Wong struct buffer_head *bh; 127541a246d1SDarrick J. Wong 12769aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 127741a246d1SDarrick J. Wong return 0; 127841a246d1SDarrick J. Wong 127941a246d1SDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->inode_bitmap); 128041a246d1SDarrick J. Wong if (!bh) 128141a246d1SDarrick J. Wong return -EIO; 128241a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, bh, 128341a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 128441a246d1SDarrick J. Wong brelse(bh); 128541a246d1SDarrick J. Wong 1286fa77dcfaSDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->block_bitmap); 1287fa77dcfaSDarrick J. Wong if (!bh) 1288fa77dcfaSDarrick J. Wong return -EIO; 128979f1ba49STao Ma ext4_block_bitmap_csum_set(sb, group, gdp, bh); 1290fa77dcfaSDarrick J. Wong brelse(bh); 1291fa77dcfaSDarrick J. Wong 129241a246d1SDarrick J. Wong return 0; 129341a246d1SDarrick J. Wong } 129441a246d1SDarrick J. Wong 1295083f5b24SYongqiang Yang /* 1296083f5b24SYongqiang Yang * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg 1297083f5b24SYongqiang Yang */ 1298083f5b24SYongqiang Yang static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb, 1299083f5b24SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 1300083f5b24SYongqiang Yang { 1301083f5b24SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 1302083f5b24SYongqiang Yang struct ext4_group_desc *gdp; 1303083f5b24SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1304083f5b24SYongqiang Yang struct buffer_head *gdb_bh; 1305083f5b24SYongqiang Yang ext4_group_t group; 1306083f5b24SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 1307083f5b24SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 1308083f5b24SYongqiang Yang 1309083f5b24SYongqiang Yang 1310083f5b24SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) { 1311083f5b24SYongqiang Yang group = group_data->group; 1312083f5b24SYongqiang Yang 1313083f5b24SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1314083f5b24SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1315083f5b24SYongqiang Yang 1316083f5b24SYongqiang Yang /* 1317083f5b24SYongqiang Yang * get_write_access() has been called on gdb_bh by ext4_add_new_desc(). 1318083f5b24SYongqiang Yang */ 13191d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, gdb_num); 1320083f5b24SYongqiang Yang /* Update group descriptor block for new group */ 13212716b802STheodore Ts'o gdp = (struct ext4_group_desc *)(gdb_bh->b_data + 1322083f5b24SYongqiang Yang gdb_off * EXT4_DESC_SIZE(sb)); 1323083f5b24SYongqiang Yang 1324083f5b24SYongqiang Yang memset(gdp, 0, EXT4_DESC_SIZE(sb)); 1325083f5b24SYongqiang Yang ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap); 1326083f5b24SYongqiang Yang ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap); 132741a246d1SDarrick J. Wong err = ext4_set_bitmap_checksums(sb, group, gdp, group_data); 132841a246d1SDarrick J. Wong if (err) { 132941a246d1SDarrick J. Wong ext4_std_error(sb, err); 133041a246d1SDarrick J. Wong break; 133141a246d1SDarrick J. Wong } 133241a246d1SDarrick J. Wong 1333083f5b24SYongqiang Yang ext4_inode_table_set(sb, gdp, group_data->inode_table); 1334083f5b24SYongqiang Yang ext4_free_group_clusters_set(sb, gdp, 1335d77147ffSharshads group_data->free_clusters_count); 1336083f5b24SYongqiang Yang ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 133793f90526STheodore Ts'o if (ext4_has_group_desc_csum(sb)) 133893f90526STheodore Ts'o ext4_itable_unused_set(sb, gdp, 133993f90526STheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 1340083f5b24SYongqiang Yang gdp->bg_flags = cpu_to_le16(*bg_flags); 1341feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1342083f5b24SYongqiang Yang 1343083f5b24SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 1344083f5b24SYongqiang Yang if (unlikely(err)) { 1345083f5b24SYongqiang Yang ext4_std_error(sb, err); 1346083f5b24SYongqiang Yang break; 1347083f5b24SYongqiang Yang } 1348083f5b24SYongqiang Yang 1349083f5b24SYongqiang Yang /* 1350083f5b24SYongqiang Yang * We can allocate memory for mb_alloc based on the new group 1351083f5b24SYongqiang Yang * descriptor 1352083f5b24SYongqiang Yang */ 1353083f5b24SYongqiang Yang err = ext4_mb_add_groupinfo(sb, group, gdp); 1354083f5b24SYongqiang Yang if (err) 1355083f5b24SYongqiang Yang break; 1356083f5b24SYongqiang Yang } 1357083f5b24SYongqiang Yang return err; 1358083f5b24SYongqiang Yang } 1359083f5b24SYongqiang Yang 13602e10e2f2SYongqiang Yang /* 13612e10e2f2SYongqiang Yang * ext4_update_super() updates the super block so that the newly added 13622e10e2f2SYongqiang Yang * groups can be seen by the filesystem. 13632e10e2f2SYongqiang Yang * 13642e10e2f2SYongqiang Yang * @sb: super block 13652e10e2f2SYongqiang Yang * @flex_gd: new added groups 13662e10e2f2SYongqiang Yang */ 13672e10e2f2SYongqiang Yang static void ext4_update_super(struct super_block *sb, 13682e10e2f2SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 13692e10e2f2SYongqiang Yang { 13702e10e2f2SYongqiang Yang ext4_fsblk_t blocks_count = 0; 13712e10e2f2SYongqiang Yang ext4_fsblk_t free_blocks = 0; 13722e10e2f2SYongqiang Yang ext4_fsblk_t reserved_blocks = 0; 13732e10e2f2SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 13742e10e2f2SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 13752e10e2f2SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 13768a991849STheodore Ts'o int i; 13772e10e2f2SYongqiang Yang 13782e10e2f2SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 13792e10e2f2SYongqiang Yang /* 13802e10e2f2SYongqiang Yang * Make the new blocks and inodes valid next. We do this before 13812e10e2f2SYongqiang Yang * increasing the group count so that once the group is enabled, 13822e10e2f2SYongqiang Yang * all of its blocks and inodes are already valid. 13832e10e2f2SYongqiang Yang * 13842e10e2f2SYongqiang Yang * We always allocate group-by-group, then block-by-block or 13852e10e2f2SYongqiang Yang * inode-by-inode within a group, so enabling these 13862e10e2f2SYongqiang Yang * blocks/inodes before the group is live won't actually let us 13872e10e2f2SYongqiang Yang * allocate the new space yet. 13882e10e2f2SYongqiang Yang */ 13892e10e2f2SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 13902e10e2f2SYongqiang Yang blocks_count += group_data[i].blocks_count; 1391d77147ffSharshads free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count); 13922e10e2f2SYongqiang Yang } 13932e10e2f2SYongqiang Yang 13942e10e2f2SYongqiang Yang reserved_blocks = ext4_r_blocks_count(es) * 100; 139501f795f9SYongqiang Yang reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es)); 13962e10e2f2SYongqiang Yang reserved_blocks *= blocks_count; 13972e10e2f2SYongqiang Yang do_div(reserved_blocks, 100); 13982e10e2f2SYongqiang Yang 139905c2c00fSJan Kara lock_buffer(sbi->s_sbh); 14002e10e2f2SYongqiang Yang ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count); 1401636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks); 14022e10e2f2SYongqiang Yang le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) * 14032e10e2f2SYongqiang Yang flex_gd->count); 1404636d7e2eSDarrick J. Wong le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) * 1405636d7e2eSDarrick J. Wong flex_gd->count); 14062e10e2f2SYongqiang Yang 140701f795f9SYongqiang Yang ext4_debug("free blocks count %llu", ext4_free_blocks_count(es)); 14082e10e2f2SYongqiang Yang /* 14092e10e2f2SYongqiang Yang * We need to protect s_groups_count against other CPUs seeing 14102e10e2f2SYongqiang Yang * inconsistent state in the superblock. 14112e10e2f2SYongqiang Yang * 14122e10e2f2SYongqiang Yang * The precise rules we use are: 14132e10e2f2SYongqiang Yang * 14142e10e2f2SYongqiang Yang * * Writers must perform a smp_wmb() after updating all 14152e10e2f2SYongqiang Yang * dependent data and before modifying the groups count 14162e10e2f2SYongqiang Yang * 14172e10e2f2SYongqiang Yang * * Readers must perform an smp_rmb() after reading the groups 14182e10e2f2SYongqiang Yang * count and before reading any dependent data. 14192e10e2f2SYongqiang Yang * 14202e10e2f2SYongqiang Yang * NB. These rules can be relaxed when checking the group count 14212e10e2f2SYongqiang Yang * while freeing data, as we can only allocate from a block 14222e10e2f2SYongqiang Yang * group after serialising against the group count, and we can 14232e10e2f2SYongqiang Yang * only then free after serialising in turn against that 14242e10e2f2SYongqiang Yang * allocation. 14252e10e2f2SYongqiang Yang */ 14262e10e2f2SYongqiang Yang smp_wmb(); 14272e10e2f2SYongqiang Yang 14282e10e2f2SYongqiang Yang /* Update the global fs size fields */ 14292e10e2f2SYongqiang Yang sbi->s_groups_count += flex_gd->count; 1430c5c72d81STheodore Ts'o sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 1431c5c72d81STheodore Ts'o (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 14322e10e2f2SYongqiang Yang 14332e10e2f2SYongqiang Yang /* Update the reserved block counts only once the new group is 14342e10e2f2SYongqiang Yang * active. */ 14352e10e2f2SYongqiang Yang ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 14362e10e2f2SYongqiang Yang reserved_blocks); 143705c2c00fSJan Kara ext4_superblock_csum_set(sb); 143805c2c00fSJan Kara unlock_buffer(sbi->s_sbh); 14392e10e2f2SYongqiang Yang 14402e10e2f2SYongqiang Yang /* Update the free space counts */ 14412e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeclusters_counter, 1442810da240SLukas Czerner EXT4_NUM_B2C(sbi, free_blocks)); 14432e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeinodes_counter, 14442e10e2f2SYongqiang Yang EXT4_INODES_PER_GROUP(sb) * flex_gd->count); 14452e10e2f2SYongqiang Yang 144601f795f9SYongqiang Yang ext4_debug("free blocks count %llu", 144701f795f9SYongqiang Yang percpu_counter_read(&sbi->s_freeclusters_counter)); 1448e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) { 14492e10e2f2SYongqiang Yang ext4_group_t flex_group; 14507c990728SSuraj Jitindar Singh struct flex_groups *fg; 14517c990728SSuraj Jitindar Singh 14522e10e2f2SYongqiang Yang flex_group = ext4_flex_group(sbi, group_data[0].group); 14537c990728SSuraj Jitindar Singh fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group); 145490ba983fSTheodore Ts'o atomic64_add(EXT4_NUM_B2C(sbi, free_blocks), 14557c990728SSuraj Jitindar Singh &fg->free_clusters); 14562e10e2f2SYongqiang Yang atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count, 14577c990728SSuraj Jitindar Singh &fg->free_inodes); 14582e10e2f2SYongqiang Yang } 14592e10e2f2SYongqiang Yang 1460952fc18eSTheodore Ts'o /* 1461952fc18eSTheodore Ts'o * Update the fs overhead information 1462952fc18eSTheodore Ts'o */ 1463952fc18eSTheodore Ts'o ext4_calculate_overhead(sb); 1464952fc18eSTheodore Ts'o 14652e10e2f2SYongqiang Yang if (test_opt(sb, DEBUG)) 14662e10e2f2SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: added group %u:" 14672e10e2f2SYongqiang Yang "%llu blocks(%llu free %llu reserved)\n", flex_gd->count, 14682e10e2f2SYongqiang Yang blocks_count, free_blocks, reserved_blocks); 14692e10e2f2SYongqiang Yang } 14702e10e2f2SYongqiang Yang 14714bac1f8cSYongqiang Yang /* Add a flex group to an fs. Ensure we handle all possible error conditions 14724bac1f8cSYongqiang Yang * _before_ we start modifying the filesystem, because we cannot abort the 14734bac1f8cSYongqiang Yang * transaction and not have it write the data to disk. 14744bac1f8cSYongqiang Yang */ 14754bac1f8cSYongqiang Yang static int ext4_flex_group_add(struct super_block *sb, 14764bac1f8cSYongqiang Yang struct inode *resize_inode, 14774bac1f8cSYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 14784bac1f8cSYongqiang Yang { 14794bac1f8cSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 14804bac1f8cSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 14814bac1f8cSYongqiang Yang ext4_fsblk_t o_blocks_count; 14824bac1f8cSYongqiang Yang ext4_grpblk_t last; 14834bac1f8cSYongqiang Yang ext4_group_t group; 14844bac1f8cSYongqiang Yang handle_t *handle; 14854bac1f8cSYongqiang Yang unsigned reserved_gdb; 14864bac1f8cSYongqiang Yang int err = 0, err2 = 0, credit; 14874bac1f8cSYongqiang Yang 14884bac1f8cSYongqiang Yang BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags); 14894bac1f8cSYongqiang Yang 14904bac1f8cSYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 14914bac1f8cSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 14924bac1f8cSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 14934bac1f8cSYongqiang Yang BUG_ON(last); 14944bac1f8cSYongqiang Yang 14954bac1f8cSYongqiang Yang err = setup_new_flex_group_blocks(sb, flex_gd); 14964bac1f8cSYongqiang Yang if (err) 14974bac1f8cSYongqiang Yang goto exit; 14984bac1f8cSYongqiang Yang /* 14994bac1f8cSYongqiang Yang * We will always be modifying at least the superblock and GDT 15002c869b26SJan Kara * blocks. If we are adding a group past the last current GDT block, 15014bac1f8cSYongqiang Yang * we will also modify the inode and the dindirect block. If we 15024bac1f8cSYongqiang Yang * are adding a group with superblock/GDT backups we will also 15034bac1f8cSYongqiang Yang * modify each of the reserved GDT dindirect blocks. 15044bac1f8cSYongqiang Yang */ 15052c869b26SJan Kara credit = 3; /* sb, resize inode, resize inode dindirect */ 15062c869b26SJan Kara /* GDT blocks */ 15072c869b26SJan Kara credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb)); 15082c869b26SJan Kara credit += reserved_gdb; /* Reserved GDT dindirect blocks */ 15099924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit); 15104bac1f8cSYongqiang Yang if (IS_ERR(handle)) { 15114bac1f8cSYongqiang Yang err = PTR_ERR(handle); 15124bac1f8cSYongqiang Yang goto exit; 15134bac1f8cSYongqiang Yang } 15144bac1f8cSYongqiang Yang 15155d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 15164bac1f8cSYongqiang Yang err = ext4_journal_get_write_access(handle, sbi->s_sbh); 15174bac1f8cSYongqiang Yang if (err) 15184bac1f8cSYongqiang Yang goto exit_journal; 15194bac1f8cSYongqiang Yang 15204bac1f8cSYongqiang Yang group = flex_gd->groups[0].group; 152149598e04SJun Piao BUG_ON(group != sbi->s_groups_count); 15224bac1f8cSYongqiang Yang err = ext4_add_new_descs(handle, sb, group, 15234bac1f8cSYongqiang Yang resize_inode, flex_gd->count); 15244bac1f8cSYongqiang Yang if (err) 15254bac1f8cSYongqiang Yang goto exit_journal; 15264bac1f8cSYongqiang Yang 15274bac1f8cSYongqiang Yang err = ext4_setup_new_descs(handle, sb, flex_gd); 15284bac1f8cSYongqiang Yang if (err) 15294bac1f8cSYongqiang Yang goto exit_journal; 15304bac1f8cSYongqiang Yang 15314bac1f8cSYongqiang Yang ext4_update_super(sb, flex_gd); 15324bac1f8cSYongqiang Yang 1533a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 15344bac1f8cSYongqiang Yang 15354bac1f8cSYongqiang Yang exit_journal: 15364bac1f8cSYongqiang Yang err2 = ext4_journal_stop(handle); 15374bac1f8cSYongqiang Yang if (!err) 15384bac1f8cSYongqiang Yang err = err2; 15394bac1f8cSYongqiang Yang 15404bac1f8cSYongqiang Yang if (!err) { 15412ebd1704SYongqiang Yang int gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 15422ebd1704SYongqiang Yang int gdb_num_end = ((group + flex_gd->count - 1) / 15432ebd1704SYongqiang Yang EXT4_DESC_PER_BLOCK(sb)); 1544e2b911c5SDarrick J. Wong int meta_bg = ext4_has_feature_meta_bg(sb); 15450acdb887STao Ma sector_t old_gdb = 0; 15462ebd1704SYongqiang Yang 15474bac1f8cSYongqiang Yang update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 154801f795f9SYongqiang Yang sizeof(struct ext4_super_block), 0); 15492ebd1704SYongqiang Yang for (; gdb_num <= gdb_num_end; gdb_num++) { 15504bac1f8cSYongqiang Yang struct buffer_head *gdb_bh; 15512ebd1704SYongqiang Yang 15521d0c3924STheodore Ts'o gdb_bh = sbi_array_rcu_deref(sbi, s_group_desc, 15531d0c3924STheodore Ts'o gdb_num); 15540acdb887STao Ma if (old_gdb == gdb_bh->b_blocknr) 15550acdb887STao Ma continue; 15564bac1f8cSYongqiang Yang update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data, 155701f795f9SYongqiang Yang gdb_bh->b_size, meta_bg); 15580acdb887STao Ma old_gdb = gdb_bh->b_blocknr; 15594bac1f8cSYongqiang Yang } 15604bac1f8cSYongqiang Yang } 15614bac1f8cSYongqiang Yang exit: 15624bac1f8cSYongqiang Yang return err; 15634bac1f8cSYongqiang Yang } 15644bac1f8cSYongqiang Yang 156519c5246dSYongqiang Yang static int ext4_setup_next_flex_gd(struct super_block *sb, 156619c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 156719c5246dSYongqiang Yang ext4_fsblk_t n_blocks_count, 156819c5246dSYongqiang Yang unsigned long flexbg_size) 156919c5246dSYongqiang Yang { 1570d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 1571d77147ffSharshads struct ext4_super_block *es = sbi->s_es; 157219c5246dSYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 157319c5246dSYongqiang Yang ext4_fsblk_t o_blocks_count; 157419c5246dSYongqiang Yang ext4_group_t n_group; 157519c5246dSYongqiang Yang ext4_group_t group; 157619c5246dSYongqiang Yang ext4_group_t last_group; 157719c5246dSYongqiang Yang ext4_grpblk_t last; 1578d77147ffSharshads ext4_grpblk_t clusters_per_group; 157919c5246dSYongqiang Yang unsigned long i; 158019c5246dSYongqiang Yang 1581d77147ffSharshads clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb); 158219c5246dSYongqiang Yang 158319c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 158419c5246dSYongqiang Yang 158519c5246dSYongqiang Yang if (o_blocks_count == n_blocks_count) 158619c5246dSYongqiang Yang return 0; 158719c5246dSYongqiang Yang 158819c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 158919c5246dSYongqiang Yang BUG_ON(last); 159019c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last); 159119c5246dSYongqiang Yang 159219c5246dSYongqiang Yang last_group = group | (flexbg_size - 1); 159319c5246dSYongqiang Yang if (last_group > n_group) 159419c5246dSYongqiang Yang last_group = n_group; 159519c5246dSYongqiang Yang 159619c5246dSYongqiang Yang flex_gd->count = last_group - group + 1; 159719c5246dSYongqiang Yang 159819c5246dSYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 159919c5246dSYongqiang Yang int overhead; 160019c5246dSYongqiang Yang 160119c5246dSYongqiang Yang group_data[i].group = group + i; 1602d77147ffSharshads group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb); 160301f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group + i); 1604d77147ffSharshads group_data[i].mdata_blocks = overhead; 1605d77147ffSharshads group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb); 16067f511862STheodore Ts'o if (ext4_has_group_desc_csum(sb)) { 160719c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT | 160819c5246dSYongqiang Yang EXT4_BG_INODE_UNINIT; 16097f511862STheodore Ts'o if (!test_opt(sb, INIT_INODE_TABLE)) 16107f511862STheodore Ts'o flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED; 16117f511862STheodore Ts'o } else 161219c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED; 161319c5246dSYongqiang Yang } 161419c5246dSYongqiang Yang 1615feb0ab32SDarrick J. Wong if (last_group == n_group && ext4_has_group_desc_csum(sb)) 161619c5246dSYongqiang Yang /* We need to initialize block bitmap of last group. */ 161719c5246dSYongqiang Yang flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT; 161819c5246dSYongqiang Yang 1619d77147ffSharshads if ((last_group == n_group) && (last != clusters_per_group - 1)) { 1620d77147ffSharshads group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1); 1621d77147ffSharshads group_data[i - 1].free_clusters_count -= clusters_per_group - 162219c5246dSYongqiang Yang last - 1; 162319c5246dSYongqiang Yang } 162419c5246dSYongqiang Yang 162519c5246dSYongqiang Yang return 1; 162619c5246dSYongqiang Yang } 162719c5246dSYongqiang Yang 1628ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 1629ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 1630ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 1631ac27a0ecSDave Kleikamp * write the data to disk. 1632ac27a0ecSDave Kleikamp * 1633ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 1634ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 1635ac27a0ecSDave Kleikamp * 1636ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 1637ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 1638ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 1639ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 1640ac27a0ecSDave Kleikamp */ 1641617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 1642ac27a0ecSDave Kleikamp { 164361f296ccSYongqiang Yang struct ext4_new_flex_group_data flex_gd; 1644617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1645617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 1646617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 1647ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1648ac27a0ecSDave Kleikamp struct inode *inode = NULL; 164903b40e34SJon Ernst int gdb_off; 165061f296ccSYongqiang Yang int err; 165161f296ccSYongqiang Yang __u16 bg_flags = 0; 1652ac27a0ecSDave Kleikamp 1653617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 1654ac27a0ecSDave Kleikamp 1655e2b911c5SDarrick J. Wong if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) { 165612062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 1657ac27a0ecSDave Kleikamp return -EPERM; 1658ac27a0ecSDave Kleikamp } 1659ac27a0ecSDave Kleikamp 1660bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 1661bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 166212062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1663ac27a0ecSDave Kleikamp return -EINVAL; 1664ac27a0ecSDave Kleikamp } 1665ac27a0ecSDave Kleikamp 1666617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 1667ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 166812062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 1669ac27a0ecSDave Kleikamp return -EINVAL; 1670ac27a0ecSDave Kleikamp } 1671ac27a0ecSDave Kleikamp 1672ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 1673e647e291Sruippan (潘睿) if (!ext4_has_feature_resize_inode(sb) || 1674e2b911c5SDarrick J. Wong !le16_to_cpu(es->s_reserved_gdt_blocks)) { 167512062dddSEric Sandeen ext4_warning(sb, 1676ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 1677ac27a0ecSDave Kleikamp return -EPERM; 1678ac27a0ecSDave Kleikamp } 16798a363970STheodore Ts'o inode = ext4_iget(sb, EXT4_RESIZE_INO, EXT4_IGET_SPECIAL); 16801d1fe1eeSDavid Howells if (IS_ERR(inode)) { 168112062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 16821d1fe1eeSDavid Howells return PTR_ERR(inode); 1683ac27a0ecSDave Kleikamp } 1684ac27a0ecSDave Kleikamp } 1685ac27a0ecSDave Kleikamp 1686920313a7SAneesh Kumar K.V 168761f296ccSYongqiang Yang err = verify_group_input(sb, input); 1688668f4dc5SYongqiang Yang if (err) 168961f296ccSYongqiang Yang goto out; 1690ac27a0ecSDave Kleikamp 1691117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, input->group + 1); 1692117fff10STheodore Ts'o if (err) 16937f511862STheodore Ts'o goto out; 1694117fff10STheodore Ts'o 169528623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, input->group + 1); 169628623c2fSTheodore Ts'o if (err) 169728623c2fSTheodore Ts'o goto out; 169828623c2fSTheodore Ts'o 169961f296ccSYongqiang Yang flex_gd.count = 1; 170061f296ccSYongqiang Yang flex_gd.groups = input; 170161f296ccSYongqiang Yang flex_gd.bg_flags = &bg_flags; 170261f296ccSYongqiang Yang err = ext4_flex_group_add(sb, inode, &flex_gd); 170361f296ccSYongqiang Yang out: 1704ac27a0ecSDave Kleikamp iput(inode); 1705ac27a0ecSDave Kleikamp return err; 1706617ba13bSMingming Cao } /* ext4_group_add */ 1707ac27a0ecSDave Kleikamp 17082b2d6d01STheodore Ts'o /* 170918e31438SYongqiang Yang * extend a group without checking assuming that checking has been done. 171018e31438SYongqiang Yang */ 171118e31438SYongqiang Yang static int ext4_group_extend_no_check(struct super_block *sb, 171218e31438SYongqiang Yang ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) 171318e31438SYongqiang Yang { 171418e31438SYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 171518e31438SYongqiang Yang handle_t *handle; 171618e31438SYongqiang Yang int err = 0, err2; 171718e31438SYongqiang Yang 171818e31438SYongqiang Yang /* We will update the superblock, one block bitmap, and 171918e31438SYongqiang Yang * one group descriptor via ext4_group_add_blocks(). 172018e31438SYongqiang Yang */ 17219924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3); 172218e31438SYongqiang Yang if (IS_ERR(handle)) { 172318e31438SYongqiang Yang err = PTR_ERR(handle); 172418e31438SYongqiang Yang ext4_warning(sb, "error %d on journal start", err); 172518e31438SYongqiang Yang return err; 172618e31438SYongqiang Yang } 172718e31438SYongqiang Yang 17285d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 172918e31438SYongqiang Yang err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 173018e31438SYongqiang Yang if (err) { 173118e31438SYongqiang Yang ext4_warning(sb, "error %d on journal write access", err); 173218e31438SYongqiang Yang goto errout; 173318e31438SYongqiang Yang } 173418e31438SYongqiang Yang 173505c2c00fSJan Kara lock_buffer(EXT4_SB(sb)->s_sbh); 173618e31438SYongqiang Yang ext4_blocks_count_set(es, o_blocks_count + add); 1737636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add); 173805c2c00fSJan Kara ext4_superblock_csum_set(sb); 173905c2c00fSJan Kara unlock_buffer(EXT4_SB(sb)->s_sbh); 174018e31438SYongqiang Yang ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 174118e31438SYongqiang Yang o_blocks_count + add); 174218e31438SYongqiang Yang /* We add the blocks to the bitmap and set the group need init bit */ 174318e31438SYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 174418e31438SYongqiang Yang if (err) 174518e31438SYongqiang Yang goto errout; 1746a3f5cf14SJan Kara ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 174718e31438SYongqiang Yang ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 174818e31438SYongqiang Yang o_blocks_count + add); 174918e31438SYongqiang Yang errout: 175018e31438SYongqiang Yang err2 = ext4_journal_stop(handle); 175118e31438SYongqiang Yang if (err2 && !err) 175218e31438SYongqiang Yang err = err2; 175318e31438SYongqiang Yang 175418e31438SYongqiang Yang if (!err) { 175518e31438SYongqiang Yang if (test_opt(sb, DEBUG)) 175618e31438SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extended group to %llu " 175718e31438SYongqiang Yang "blocks\n", ext4_blocks_count(es)); 17586ca792edSMaarten ter Huurne update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, 175901f795f9SYongqiang Yang (char *)es, sizeof(struct ext4_super_block), 0); 176018e31438SYongqiang Yang } 176118e31438SYongqiang Yang return err; 176218e31438SYongqiang Yang } 176318e31438SYongqiang Yang 176418e31438SYongqiang Yang /* 17652b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 1766ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 1767ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 1768ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 1769ac27a0ecSDave Kleikamp * 1770617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 1771ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 1772ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 1773ac27a0ecSDave Kleikamp */ 1774617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 1775617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 1776ac27a0ecSDave Kleikamp { 1777617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 1778617ba13bSMingming Cao ext4_grpblk_t last; 1779617ba13bSMingming Cao ext4_grpblk_t add; 1780ac27a0ecSDave Kleikamp struct buffer_head *bh; 1781d89651c8SYongqiang Yang int err; 17825f21b0e6SFrederic Bohe ext4_group_t group; 1783ac27a0ecSDave Kleikamp 1784bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 1785ac27a0ecSDave Kleikamp 1786ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 178792b97816STheodore Ts'o ext4_msg(sb, KERN_DEBUG, 178892b97816STheodore Ts'o "extending last group from %llu to %llu blocks", 1789ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 1790ac27a0ecSDave Kleikamp 1791ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 1792ac27a0ecSDave Kleikamp return 0; 1793ac27a0ecSDave Kleikamp 1794ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 179592b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, 179692b97816STheodore Ts'o "filesystem too large to resize to %llu blocks safely", 179792b97816STheodore Ts'o n_blocks_count); 1798ac27a0ecSDave Kleikamp return -EINVAL; 1799ac27a0ecSDave Kleikamp } 1800ac27a0ecSDave Kleikamp 1801ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 180212062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 18038f82f840SYongqiang Yang return -EINVAL; 1804ac27a0ecSDave Kleikamp } 1805ac27a0ecSDave Kleikamp 1806ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 18075f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1808ac27a0ecSDave Kleikamp 1809ac27a0ecSDave Kleikamp if (last == 0) { 181012062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1811ac27a0ecSDave Kleikamp return -EPERM; 1812ac27a0ecSDave Kleikamp } 1813ac27a0ecSDave Kleikamp 1814617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1815ac27a0ecSDave Kleikamp 1816ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 181712062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1818ac27a0ecSDave Kleikamp return -EINVAL; 1819ac27a0ecSDave Kleikamp } 1820ac27a0ecSDave Kleikamp 1821ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1822ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1823ac27a0ecSDave Kleikamp 1824ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 182512062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1826ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1827ac27a0ecSDave Kleikamp 1828ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 18290a846f49Szhangyi (F) bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0); 18300a846f49Szhangyi (F) if (IS_ERR(bh)) { 183112062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1832ac27a0ecSDave Kleikamp return -ENOSPC; 1833ac27a0ecSDave Kleikamp } 1834ac27a0ecSDave Kleikamp brelse(bh); 1835ac27a0ecSDave Kleikamp 1836d89651c8SYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 1837ac27a0ecSDave Kleikamp return err; 1838617ba13bSMingming Cao } /* ext4_group_extend */ 183919c5246dSYongqiang Yang 18401c6bd717STheodore Ts'o 18411c6bd717STheodore Ts'o static int num_desc_blocks(struct super_block *sb, ext4_group_t groups) 18421c6bd717STheodore Ts'o { 18431c6bd717STheodore Ts'o return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb); 18441c6bd717STheodore Ts'o } 18451c6bd717STheodore Ts'o 18461c6bd717STheodore Ts'o /* 18471c6bd717STheodore Ts'o * Release the resize inode and drop the resize_inode feature if there 18481c6bd717STheodore Ts'o * are no more reserved gdt blocks, and then convert the file system 18491c6bd717STheodore Ts'o * to enable meta_bg 18501c6bd717STheodore Ts'o */ 18511c6bd717STheodore Ts'o static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode) 18521c6bd717STheodore Ts'o { 18531c6bd717STheodore Ts'o handle_t *handle; 18541c6bd717STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 18551c6bd717STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 185659e31c15STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 18571c6bd717STheodore Ts'o ext4_fsblk_t nr; 18581c6bd717STheodore Ts'o int i, ret, err = 0; 18591c6bd717STheodore Ts'o int credits = 1; 18601c6bd717STheodore Ts'o 18611c6bd717STheodore Ts'o ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg"); 186259e31c15STheodore Ts'o if (inode) { 18631c6bd717STheodore Ts'o if (es->s_reserved_gdt_blocks) { 18641c6bd717STheodore Ts'o ext4_error(sb, "Unexpected non-zero " 18651c6bd717STheodore Ts'o "s_reserved_gdt_blocks"); 18661c6bd717STheodore Ts'o return -EPERM; 18671c6bd717STheodore Ts'o } 18681c6bd717STheodore Ts'o 18691c6bd717STheodore Ts'o /* Do a quick sanity check of the resize inode */ 1870d77147ffSharshads if (inode->i_blocks != 1 << (inode->i_blkbits - 1871d77147ffSharshads (9 - sbi->s_cluster_bits))) 18721c6bd717STheodore Ts'o goto invalid_resize_inode; 18731c6bd717STheodore Ts'o for (i = 0; i < EXT4_N_BLOCKS; i++) { 18741c6bd717STheodore Ts'o if (i == EXT4_DIND_BLOCK) { 18751c6bd717STheodore Ts'o if (ei->i_data[i]) 18761c6bd717STheodore Ts'o continue; 18771c6bd717STheodore Ts'o else 18781c6bd717STheodore Ts'o goto invalid_resize_inode; 18791c6bd717STheodore Ts'o } 18801c6bd717STheodore Ts'o if (ei->i_data[i]) 18811c6bd717STheodore Ts'o goto invalid_resize_inode; 18821c6bd717STheodore Ts'o } 18831c6bd717STheodore Ts'o credits += 3; /* block bitmap, bg descriptor, resize inode */ 18841c6bd717STheodore Ts'o } 18851c6bd717STheodore Ts'o 18869924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits); 18871c6bd717STheodore Ts'o if (IS_ERR(handle)) 18881c6bd717STheodore Ts'o return PTR_ERR(handle); 18891c6bd717STheodore Ts'o 18905d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 18911c6bd717STheodore Ts'o err = ext4_journal_get_write_access(handle, sbi->s_sbh); 18921c6bd717STheodore Ts'o if (err) 18931c6bd717STheodore Ts'o goto errout; 18941c6bd717STheodore Ts'o 189505c2c00fSJan Kara lock_buffer(sbi->s_sbh); 1896e2b911c5SDarrick J. Wong ext4_clear_feature_resize_inode(sb); 1897e2b911c5SDarrick J. Wong ext4_set_feature_meta_bg(sb); 18981c6bd717STheodore Ts'o sbi->s_es->s_first_meta_bg = 18991c6bd717STheodore Ts'o cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count)); 190005c2c00fSJan Kara ext4_superblock_csum_set(sb); 190105c2c00fSJan Kara unlock_buffer(sbi->s_sbh); 19021c6bd717STheodore Ts'o 1903a3f5cf14SJan Kara err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 19041c6bd717STheodore Ts'o if (err) { 19051c6bd717STheodore Ts'o ext4_std_error(sb, err); 19061c6bd717STheodore Ts'o goto errout; 19071c6bd717STheodore Ts'o } 19081c6bd717STheodore Ts'o 19091c6bd717STheodore Ts'o if (inode) { 19101c6bd717STheodore Ts'o nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]); 19111c6bd717STheodore Ts'o ext4_free_blocks(handle, inode, NULL, nr, 1, 19121c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | 19131c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 19141c6bd717STheodore Ts'o ei->i_data[EXT4_DIND_BLOCK] = 0; 19151c6bd717STheodore Ts'o inode->i_blocks = 0; 19161c6bd717STheodore Ts'o 19171c6bd717STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 19181c6bd717STheodore Ts'o if (err) 19191c6bd717STheodore Ts'o ext4_std_error(sb, err); 19201c6bd717STheodore Ts'o } 19211c6bd717STheodore Ts'o 19221c6bd717STheodore Ts'o errout: 19231c6bd717STheodore Ts'o ret = ext4_journal_stop(handle); 19241c6bd717STheodore Ts'o if (!err) 19251c6bd717STheodore Ts'o err = ret; 19261c6bd717STheodore Ts'o return ret; 19271c6bd717STheodore Ts'o 19281c6bd717STheodore Ts'o invalid_resize_inode: 19291c6bd717STheodore Ts'o ext4_error(sb, "corrupted/inconsistent resize inode"); 19301c6bd717STheodore Ts'o return -EINVAL; 19311c6bd717STheodore Ts'o } 19321c6bd717STheodore Ts'o 193319c5246dSYongqiang Yang /* 193419c5246dSYongqiang Yang * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count 193519c5246dSYongqiang Yang * 193619c5246dSYongqiang Yang * @sb: super block of the fs to be resized 193719c5246dSYongqiang Yang * @n_blocks_count: the number of blocks resides in the resized fs 193819c5246dSYongqiang Yang */ 193919c5246dSYongqiang Yang int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count) 194019c5246dSYongqiang Yang { 194119c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd = NULL; 194219c5246dSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 194319c5246dSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 194419c5246dSYongqiang Yang struct buffer_head *bh; 194501f795f9SYongqiang Yang struct inode *resize_inode = NULL; 194601f795f9SYongqiang Yang ext4_grpblk_t add, offset; 194719c5246dSYongqiang Yang unsigned long n_desc_blocks; 194819c5246dSYongqiang Yang unsigned long o_desc_blocks; 194901f795f9SYongqiang Yang ext4_group_t o_group; 195001f795f9SYongqiang Yang ext4_group_t n_group; 195101f795f9SYongqiang Yang ext4_fsblk_t o_blocks_count; 19521c6bd717STheodore Ts'o ext4_fsblk_t n_blocks_count_retry = 0; 19534da4a56eSTheodore Ts'o unsigned long last_update_time = 0; 1954117fff10STheodore Ts'o int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex; 195501f795f9SYongqiang Yang int meta_bg; 195619c5246dSYongqiang Yang 195759e31c15STheodore Ts'o /* See if the device is actually as big as what was requested */ 19580a846f49Szhangyi (F) bh = ext4_sb_bread(sb, n_blocks_count - 1, 0); 19590a846f49Szhangyi (F) if (IS_ERR(bh)) { 196059e31c15STheodore Ts'o ext4_warning(sb, "can't read last block, resize aborted"); 196159e31c15STheodore Ts'o return -ENOSPC; 196259e31c15STheodore Ts'o } 196359e31c15STheodore Ts'o brelse(bh); 196459e31c15STheodore Ts'o 19651c6bd717STheodore Ts'o retry: 196619c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 196719c5246dSYongqiang Yang 196859e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu " 196992b97816STheodore Ts'o "to %llu blocks", o_blocks_count, n_blocks_count); 197019c5246dSYongqiang Yang 197119c5246dSYongqiang Yang if (n_blocks_count < o_blocks_count) { 197219c5246dSYongqiang Yang /* On-line shrinking not supported */ 197319c5246dSYongqiang Yang ext4_warning(sb, "can't shrink FS - resize aborted"); 197419c5246dSYongqiang Yang return -EINVAL; 197519c5246dSYongqiang Yang } 197619c5246dSYongqiang Yang 197719c5246dSYongqiang Yang if (n_blocks_count == o_blocks_count) 197819c5246dSYongqiang Yang /* Nothing need to do */ 197919c5246dSYongqiang Yang return 0; 198019c5246dSYongqiang Yang 1981bd86298eSLukas Czerner n_group = ext4_get_group_number(sb, n_blocks_count - 1); 19824f2f76f7SJan Kara if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) { 19833f8a6411STheodore Ts'o ext4_warning(sb, "resize would cause inodes_count overflow"); 19843f8a6411STheodore Ts'o return -EINVAL; 19853f8a6411STheodore Ts'o } 1986a0ade1deSLukas Czerner ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset); 198719c5246dSYongqiang Yang 19881c6bd717STheodore Ts'o n_desc_blocks = num_desc_blocks(sb, n_group + 1); 19891c6bd717STheodore Ts'o o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count); 199019c5246dSYongqiang Yang 1991e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 199201f795f9SYongqiang Yang 1993e2b911c5SDarrick J. Wong if (ext4_has_feature_resize_inode(sb)) { 199401f795f9SYongqiang Yang if (meta_bg) { 199501f795f9SYongqiang Yang ext4_error(sb, "resize_inode and meta_bg enabled " 199601f795f9SYongqiang Yang "simultaneously"); 199701f795f9SYongqiang Yang return -EINVAL; 199801f795f9SYongqiang Yang } 19991c6bd717STheodore Ts'o if (n_desc_blocks > o_desc_blocks + 20001c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks)) { 20011c6bd717STheodore Ts'o n_blocks_count_retry = n_blocks_count; 20021c6bd717STheodore Ts'o n_desc_blocks = o_desc_blocks + 20031c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks); 20041c6bd717STheodore Ts'o n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb); 2005aec51758SJerry Lee n_blocks_count = (ext4_fsblk_t)n_group * 2006f96c3ac8SJan Kara EXT4_BLOCKS_PER_GROUP(sb) + 2007f96c3ac8SJan Kara le32_to_cpu(es->s_first_data_block); 20081c6bd717STheodore Ts'o n_group--; /* set to last group number */ 200919c5246dSYongqiang Yang } 20101c6bd717STheodore Ts'o 20111c6bd717STheodore Ts'o if (!resize_inode) 20128a363970STheodore Ts'o resize_inode = ext4_iget(sb, EXT4_RESIZE_INO, 20138a363970STheodore Ts'o EXT4_IGET_SPECIAL); 201419c5246dSYongqiang Yang if (IS_ERR(resize_inode)) { 201519c5246dSYongqiang Yang ext4_warning(sb, "Error opening resize inode"); 201619c5246dSYongqiang Yang return PTR_ERR(resize_inode); 201719c5246dSYongqiang Yang } 20181c6bd717STheodore Ts'o } 20191c6bd717STheodore Ts'o 202059e31c15STheodore Ts'o if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) { 20211c6bd717STheodore Ts'o err = ext4_convert_meta_bg(sb, resize_inode); 20221c6bd717STheodore Ts'o if (err) 20231c6bd717STheodore Ts'o goto out; 20241c6bd717STheodore Ts'o if (resize_inode) { 20251c6bd717STheodore Ts'o iput(resize_inode); 20261c6bd717STheodore Ts'o resize_inode = NULL; 20271c6bd717STheodore Ts'o } 20281c6bd717STheodore Ts'o if (n_blocks_count_retry) { 20291c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 20301c6bd717STheodore Ts'o n_blocks_count_retry = 0; 20311c6bd717STheodore Ts'o goto retry; 20321c6bd717STheodore Ts'o } 203301f795f9SYongqiang Yang } 203419c5246dSYongqiang Yang 2035f0a459deSTheodore Ts'o /* 2036f0a459deSTheodore Ts'o * Make sure the last group has enough space so that it's 2037f0a459deSTheodore Ts'o * guaranteed to have enough space for all metadata blocks 2038f0a459deSTheodore Ts'o * that it might need to hold. (We might not need to store 2039f0a459deSTheodore Ts'o * the inode table blocks in the last block group, but there 2040f0a459deSTheodore Ts'o * will be cases where this might be needed.) 2041f0a459deSTheodore Ts'o */ 2042f0a459deSTheodore Ts'o if ((ext4_group_first_block_no(sb, n_group) + 2043f0a459deSTheodore Ts'o ext4_group_overhead_blocks(sb, n_group) + 2 + 2044f0a459deSTheodore Ts'o sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) { 2045f0a459deSTheodore Ts'o n_blocks_count = ext4_group_first_block_no(sb, n_group); 2046f0a459deSTheodore Ts'o n_group--; 2047f0a459deSTheodore Ts'o n_blocks_count_retry = 0; 2048f0a459deSTheodore Ts'o if (resize_inode) { 2049f0a459deSTheodore Ts'o iput(resize_inode); 2050f0a459deSTheodore Ts'o resize_inode = NULL; 2051f0a459deSTheodore Ts'o } 2052f0a459deSTheodore Ts'o goto retry; 2053f0a459deSTheodore Ts'o } 2054f0a459deSTheodore Ts'o 205519c5246dSYongqiang Yang /* extend the last group */ 2056a0ade1deSLukas Czerner if (n_group == o_group) 2057a0ade1deSLukas Czerner add = n_blocks_count - o_blocks_count; 2058a0ade1deSLukas Czerner else 2059d77147ffSharshads add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1)); 2060a0ade1deSLukas Czerner if (add > 0) { 206119c5246dSYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 206219c5246dSYongqiang Yang if (err) 206319c5246dSYongqiang Yang goto out; 206419c5246dSYongqiang Yang } 206519c5246dSYongqiang Yang 2066d7574ad0SYongqiang Yang if (ext4_blocks_count(es) == n_blocks_count) 206719c5246dSYongqiang Yang goto out; 206819c5246dSYongqiang Yang 2069117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, n_group + 1); 2070117fff10STheodore Ts'o if (err) 2071f348e224SVasily Averin goto out; 2072117fff10STheodore Ts'o 207328623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, n_group + 1); 207428623c2fSTheodore Ts'o if (err) 207528623c2fSTheodore Ts'o goto out; 207628623c2fSTheodore Ts'o 207719c5246dSYongqiang Yang flex_gd = alloc_flex_gd(flexbg_size); 207819c5246dSYongqiang Yang if (flex_gd == NULL) { 207919c5246dSYongqiang Yang err = -ENOMEM; 208019c5246dSYongqiang Yang goto out; 208119c5246dSYongqiang Yang } 208219c5246dSYongqiang Yang 208319c5246dSYongqiang Yang /* Add flex groups. Note that a regular group is a 208419c5246dSYongqiang Yang * flex group with 1 group. 208519c5246dSYongqiang Yang */ 208619c5246dSYongqiang Yang while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count, 208719c5246dSYongqiang Yang flexbg_size)) { 20884da4a56eSTheodore Ts'o if (jiffies - last_update_time > HZ * 10) { 20894da4a56eSTheodore Ts'o if (last_update_time) 20904da4a56eSTheodore Ts'o ext4_msg(sb, KERN_INFO, 20914da4a56eSTheodore Ts'o "resized to %llu blocks", 20924da4a56eSTheodore Ts'o ext4_blocks_count(es)); 20934da4a56eSTheodore Ts'o last_update_time = jiffies; 20944da4a56eSTheodore Ts'o } 209503c1c290SYongqiang Yang if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0) 209603c1c290SYongqiang Yang break; 209719c5246dSYongqiang Yang err = ext4_flex_group_add(sb, resize_inode, flex_gd); 209819c5246dSYongqiang Yang if (unlikely(err)) 209919c5246dSYongqiang Yang break; 210019c5246dSYongqiang Yang } 210119c5246dSYongqiang Yang 21021c6bd717STheodore Ts'o if (!err && n_blocks_count_retry) { 21031c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 21041c6bd717STheodore Ts'o n_blocks_count_retry = 0; 21051c6bd717STheodore Ts'o free_flex_gd(flex_gd); 21061c6bd717STheodore Ts'o flex_gd = NULL; 2107db6aee62SVasily Averin if (resize_inode) { 2108db6aee62SVasily Averin iput(resize_inode); 2109db6aee62SVasily Averin resize_inode = NULL; 2110db6aee62SVasily Averin } 21111c6bd717STheodore Ts'o goto retry; 21121c6bd717STheodore Ts'o } 21131c6bd717STheodore Ts'o 211419c5246dSYongqiang Yang out: 211519c5246dSYongqiang Yang if (flex_gd) 211619c5246dSYongqiang Yang free_flex_gd(flex_gd); 211701f795f9SYongqiang Yang if (resize_inode != NULL) 211819c5246dSYongqiang Yang iput(resize_inode); 21196c732840SLukas Czerner if (err) 21206c732840SLukas Czerner ext4_warning(sb, "error (%d) occurred during " 21216c732840SLukas Czerner "file system resize", err); 21226c732840SLukas Czerner ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", 21236c732840SLukas Czerner ext4_blocks_count(es)); 212419c5246dSYongqiang Yang return err; 212519c5246dSYongqiang Yang } 2126