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 208f82f840SYongqiang Yang int ext4_resize_begin(struct super_block *sb) 218f82f840SYongqiang Yang { 225f8c1093STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 238f82f840SYongqiang Yang int ret = 0; 248f82f840SYongqiang Yang 258f82f840SYongqiang Yang if (!capable(CAP_SYS_RESOURCE)) 268f82f840SYongqiang Yang return -EPERM; 278f82f840SYongqiang Yang 28ce723c31SYongqiang Yang /* 29011fa994STheodore Ts'o * If we are not using the primary superblock/GDT copy don't resize, 30011fa994STheodore Ts'o * because the user tools have no way of handling this. Probably a 31011fa994STheodore Ts'o * bad time to do it anyways. 32011fa994STheodore Ts'o */ 335f8c1093STheodore Ts'o if (EXT4_B2C(sbi, sbi->s_sbh->b_blocknr) != 34011fa994STheodore Ts'o le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 35011fa994STheodore Ts'o ext4_warning(sb, "won't resize using backup superblock at %llu", 36011fa994STheodore Ts'o (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 37011fa994STheodore Ts'o return -EPERM; 38011fa994STheodore Ts'o } 39011fa994STheodore Ts'o 40011fa994STheodore Ts'o /* 41ce723c31SYongqiang Yang * We are not allowed to do online-resizing on a filesystem mounted 42ce723c31SYongqiang Yang * with error, because it can destroy the filesystem easily. 43ce723c31SYongqiang Yang */ 44ce723c31SYongqiang Yang if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 45ce723c31SYongqiang Yang ext4_warning(sb, "There are errors in the filesystem, " 468d2ae1cbSJakub Wilk "so online resizing is not allowed"); 47ce723c31SYongqiang Yang return -EPERM; 48ce723c31SYongqiang Yang } 49ce723c31SYongqiang Yang 509549a168STheodore Ts'o if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING, 519549a168STheodore Ts'o &EXT4_SB(sb)->s_ext4_flags)) 528f82f840SYongqiang Yang ret = -EBUSY; 538f82f840SYongqiang Yang 548f82f840SYongqiang Yang return ret; 558f82f840SYongqiang Yang } 568f82f840SYongqiang Yang 578f82f840SYongqiang Yang void ext4_resize_end(struct super_block *sb) 588f82f840SYongqiang Yang { 599549a168STheodore Ts'o clear_bit_unlock(EXT4_FLAGS_RESIZING, &EXT4_SB(sb)->s_ext4_flags); 604e857c58SPeter Zijlstra smp_mb__after_atomic(); 618f82f840SYongqiang Yang } 628f82f840SYongqiang Yang 6301f795f9SYongqiang Yang static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb, 6401f795f9SYongqiang Yang ext4_group_t group) { 6501f795f9SYongqiang Yang return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) << 6601f795f9SYongqiang Yang EXT4_DESC_PER_BLOCK_BITS(sb); 6701f795f9SYongqiang Yang } 6801f795f9SYongqiang Yang 6901f795f9SYongqiang Yang static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb, 7001f795f9SYongqiang Yang ext4_group_t group) { 7101f795f9SYongqiang Yang group = ext4_meta_bg_first_group(sb, group); 7201f795f9SYongqiang Yang return ext4_group_first_block_no(sb, group); 7301f795f9SYongqiang Yang } 7401f795f9SYongqiang Yang 7501f795f9SYongqiang Yang static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb, 7601f795f9SYongqiang Yang ext4_group_t group) { 7701f795f9SYongqiang Yang ext4_grpblk_t overhead; 7801f795f9SYongqiang Yang overhead = ext4_bg_num_gdb(sb, group); 7901f795f9SYongqiang Yang if (ext4_bg_has_super(sb, group)) 8001f795f9SYongqiang Yang overhead += 1 + 8101f795f9SYongqiang Yang le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 8201f795f9SYongqiang Yang return overhead; 8301f795f9SYongqiang Yang } 8401f795f9SYongqiang Yang 85ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 86ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 87ac27a0ecSDave Kleikamp 88ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 89617ba13bSMingming Cao struct ext4_new_group_data *input) 90ac27a0ecSDave Kleikamp { 91617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 92617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 93bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 94617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 95fd2d4291SAvantika Mathur ext4_group_t group = input->group; 96617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 97b302ef2dSTheodore Ts'o unsigned overhead; 98b302ef2dSTheodore Ts'o ext4_fsblk_t metaend; 99ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 1003a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 101ac27a0ecSDave Kleikamp int err = -EINVAL; 102ac27a0ecSDave Kleikamp 103b302ef2dSTheodore Ts'o if (group != sbi->s_groups_count) { 104b302ef2dSTheodore Ts'o ext4_warning(sb, "Cannot add at group %u (only %u groups)", 105b302ef2dSTheodore Ts'o input->group, sbi->s_groups_count); 106b302ef2dSTheodore Ts'o return -EINVAL; 107b302ef2dSTheodore Ts'o } 108b302ef2dSTheodore Ts'o 109b302ef2dSTheodore Ts'o overhead = ext4_group_overhead_blocks(sb, group); 110b302ef2dSTheodore Ts'o metaend = start + overhead; 111d77147ffSharshads input->free_clusters_count = free_blocks_count = 112ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 113ac27a0ecSDave Kleikamp 114ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 115617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 116ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 117617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 118ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 119ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 120ac27a0ecSDave Kleikamp 1213a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 122b302ef2dSTheodore Ts'o if (offset != 0) 12312062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 124ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 12512062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 126ac27a0ecSDave Kleikamp input->reserved_blocks); 127ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 12812062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 129ac27a0ecSDave Kleikamp input->blocks_count); 130fb265c9cSTheodore Ts'o else if (IS_ERR(bh = ext4_sb_bread(sb, end - 1, 0))) { 131fb265c9cSTheodore Ts'o err = PTR_ERR(bh); 132fb265c9cSTheodore Ts'o bh = NULL; 13312062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 134ac27a0ecSDave Kleikamp end - 1); 135fb265c9cSTheodore Ts'o } else if (outside(input->block_bitmap, start, end)) 13612062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 1371939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 138ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 13912062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 1401939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 141ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 142ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 14312062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1441939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 145ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 14612062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1471939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 148ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 14912062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 15012062dddSEric Sandeen "(%llu-%llu)", 1511939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1521939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 153ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 15412062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 15512062dddSEric Sandeen "(%llu-%llu)", 1561939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1571939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 158ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 15912062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 1601939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1611939e49aSRandy Dunlap start, metaend - 1); 162ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 16312062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 1641939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1651939e49aSRandy Dunlap start, metaend - 1); 166ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 167ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 16812062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 16912062dddSEric Sandeen "(%llu-%llu)", 1701939e49aSRandy Dunlap (unsigned long long)input->inode_table, 1711939e49aSRandy Dunlap itend - 1, start, metaend - 1); 172ac27a0ecSDave Kleikamp else 173ac27a0ecSDave Kleikamp err = 0; 174ac27a0ecSDave Kleikamp brelse(bh); 175ac27a0ecSDave Kleikamp 176ac27a0ecSDave Kleikamp return err; 177ac27a0ecSDave Kleikamp } 178ac27a0ecSDave Kleikamp 17928c7bac0SYongqiang Yang /* 18028c7bac0SYongqiang Yang * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex 18128c7bac0SYongqiang Yang * group each time. 18228c7bac0SYongqiang Yang */ 18328c7bac0SYongqiang Yang struct ext4_new_flex_group_data { 18428c7bac0SYongqiang Yang struct ext4_new_group_data *groups; /* new_group_data for groups 18528c7bac0SYongqiang Yang in the flex group */ 18628c7bac0SYongqiang Yang __u16 *bg_flags; /* block group flags of groups 18728c7bac0SYongqiang Yang in @groups */ 18828c7bac0SYongqiang Yang ext4_group_t count; /* number of groups in @groups 18928c7bac0SYongqiang Yang */ 19028c7bac0SYongqiang Yang }; 19128c7bac0SYongqiang Yang 19228c7bac0SYongqiang Yang /* 19328c7bac0SYongqiang Yang * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of 19428c7bac0SYongqiang Yang * @flexbg_size. 19528c7bac0SYongqiang Yang * 19628c7bac0SYongqiang Yang * Returns NULL on failure otherwise address of the allocated structure. 19728c7bac0SYongqiang Yang */ 19828c7bac0SYongqiang Yang static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) 19928c7bac0SYongqiang Yang { 20028c7bac0SYongqiang Yang struct ext4_new_flex_group_data *flex_gd; 20128c7bac0SYongqiang Yang 20228c7bac0SYongqiang Yang flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS); 20328c7bac0SYongqiang Yang if (flex_gd == NULL) 20428c7bac0SYongqiang Yang goto out3; 20528c7bac0SYongqiang Yang 20646901760SInsu Yun if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data)) 207967ac8afSHaogang Chen goto out2; 20828c7bac0SYongqiang Yang flex_gd->count = flexbg_size; 20928c7bac0SYongqiang Yang 2106da2ec56SKees Cook flex_gd->groups = kmalloc_array(flexbg_size, 2116da2ec56SKees Cook sizeof(struct ext4_new_group_data), 2126da2ec56SKees Cook GFP_NOFS); 21328c7bac0SYongqiang Yang if (flex_gd->groups == NULL) 21428c7bac0SYongqiang Yang goto out2; 21528c7bac0SYongqiang Yang 2166da2ec56SKees Cook flex_gd->bg_flags = kmalloc_array(flexbg_size, sizeof(__u16), 2176da2ec56SKees Cook GFP_NOFS); 21828c7bac0SYongqiang Yang if (flex_gd->bg_flags == NULL) 21928c7bac0SYongqiang Yang goto out1; 22028c7bac0SYongqiang Yang 22128c7bac0SYongqiang Yang return flex_gd; 22228c7bac0SYongqiang Yang 22328c7bac0SYongqiang Yang out1: 22428c7bac0SYongqiang Yang kfree(flex_gd->groups); 22528c7bac0SYongqiang Yang out2: 22628c7bac0SYongqiang Yang kfree(flex_gd); 22728c7bac0SYongqiang Yang out3: 22828c7bac0SYongqiang Yang return NULL; 22928c7bac0SYongqiang Yang } 23028c7bac0SYongqiang Yang 23128c7bac0SYongqiang Yang static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd) 23228c7bac0SYongqiang Yang { 23328c7bac0SYongqiang Yang kfree(flex_gd->bg_flags); 23428c7bac0SYongqiang Yang kfree(flex_gd->groups); 23528c7bac0SYongqiang Yang kfree(flex_gd); 23628c7bac0SYongqiang Yang } 23728c7bac0SYongqiang Yang 2383fbea4b3SYongqiang Yang /* 2393fbea4b3SYongqiang Yang * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps 2403fbea4b3SYongqiang Yang * and inode tables for a flex group. 2413fbea4b3SYongqiang Yang * 2423fbea4b3SYongqiang Yang * This function is used by 64bit-resize. Note that this function allocates 2433fbea4b3SYongqiang Yang * group tables from the 1st group of groups contained by @flexgd, which may 2443fbea4b3SYongqiang Yang * be a partial of a flex group. 2453fbea4b3SYongqiang Yang * 2463fbea4b3SYongqiang Yang * @sb: super block of fs to which the groups belongs 24703c1c290SYongqiang Yang * 24803c1c290SYongqiang Yang * Returns 0 on a successful allocation of the metadata blocks in the 24903c1c290SYongqiang Yang * block group. 2503fbea4b3SYongqiang Yang */ 25103c1c290SYongqiang Yang static int ext4_alloc_group_tables(struct super_block *sb, 2523fbea4b3SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 2533fbea4b3SYongqiang Yang int flexbg_size) 2543fbea4b3SYongqiang Yang { 2553fbea4b3SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 2563fbea4b3SYongqiang Yang ext4_fsblk_t start_blk; 2573fbea4b3SYongqiang Yang ext4_fsblk_t last_blk; 2583fbea4b3SYongqiang Yang ext4_group_t src_group; 2593fbea4b3SYongqiang Yang ext4_group_t bb_index = 0; 2603fbea4b3SYongqiang Yang ext4_group_t ib_index = 0; 2613fbea4b3SYongqiang Yang ext4_group_t it_index = 0; 2623fbea4b3SYongqiang Yang ext4_group_t group; 2633fbea4b3SYongqiang Yang ext4_group_t last_group; 2643fbea4b3SYongqiang Yang unsigned overhead; 265b93c9535STheodore Ts'o __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0; 266d77147ffSharshads int i; 2673fbea4b3SYongqiang Yang 2683fbea4b3SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 2693fbea4b3SYongqiang Yang 2703fbea4b3SYongqiang Yang src_group = group_data[0].group; 2713fbea4b3SYongqiang Yang last_group = src_group + flex_gd->count - 1; 2723fbea4b3SYongqiang Yang 2733fbea4b3SYongqiang Yang BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) != 2743fbea4b3SYongqiang Yang (last_group & ~(flexbg_size - 1)))); 2753fbea4b3SYongqiang Yang next_group: 2763fbea4b3SYongqiang Yang group = group_data[0].group; 27703c1c290SYongqiang Yang if (src_group >= group_data[0].group + flex_gd->count) 27803c1c290SYongqiang Yang return -ENOSPC; 2793fbea4b3SYongqiang Yang start_blk = ext4_group_first_block_no(sb, src_group); 2803fbea4b3SYongqiang Yang last_blk = start_blk + group_data[src_group - group].blocks_count; 2813fbea4b3SYongqiang Yang 28201f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 2833fbea4b3SYongqiang Yang 2843fbea4b3SYongqiang Yang start_blk += overhead; 2853fbea4b3SYongqiang Yang 2863fbea4b3SYongqiang Yang /* We collect contiguous blocks as much as possible. */ 2873fbea4b3SYongqiang Yang src_group++; 28801f795f9SYongqiang Yang for (; src_group <= last_group; src_group++) { 28901f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 290b93c9535STheodore Ts'o if (overhead == 0) 2913fbea4b3SYongqiang Yang last_blk += group_data[src_group - group].blocks_count; 2923fbea4b3SYongqiang Yang else 2933fbea4b3SYongqiang Yang break; 29401f795f9SYongqiang Yang } 2953fbea4b3SYongqiang Yang 2963fbea4b3SYongqiang Yang /* Allocate block bitmaps */ 2973fbea4b3SYongqiang Yang for (; bb_index < flex_gd->count; bb_index++) { 2983fbea4b3SYongqiang Yang if (start_blk >= last_blk) 2993fbea4b3SYongqiang Yang goto next_group; 3003fbea4b3SYongqiang Yang group_data[bb_index].block_bitmap = start_blk++; 301bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3023fbea4b3SYongqiang Yang group -= group_data[0].group; 303d77147ffSharshads group_data[group].mdata_blocks++; 304b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3053fbea4b3SYongqiang Yang } 3063fbea4b3SYongqiang Yang 3073fbea4b3SYongqiang Yang /* Allocate inode bitmaps */ 3083fbea4b3SYongqiang Yang for (; ib_index < flex_gd->count; ib_index++) { 3093fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3103fbea4b3SYongqiang Yang goto next_group; 3113fbea4b3SYongqiang Yang group_data[ib_index].inode_bitmap = start_blk++; 312bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3133fbea4b3SYongqiang Yang group -= group_data[0].group; 314d77147ffSharshads group_data[group].mdata_blocks++; 315b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3163fbea4b3SYongqiang Yang } 3173fbea4b3SYongqiang Yang 3183fbea4b3SYongqiang Yang /* Allocate inode tables */ 3193fbea4b3SYongqiang Yang for (; it_index < flex_gd->count; it_index++) { 320b93c9535STheodore Ts'o unsigned int itb = EXT4_SB(sb)->s_itb_per_group; 321b93c9535STheodore Ts'o ext4_fsblk_t next_group_start; 322b93c9535STheodore Ts'o 323b93c9535STheodore Ts'o if (start_blk + itb > last_blk) 3243fbea4b3SYongqiang Yang goto next_group; 3253fbea4b3SYongqiang Yang group_data[it_index].inode_table = start_blk; 326b93c9535STheodore Ts'o group = ext4_get_group_number(sb, start_blk); 327b93c9535STheodore Ts'o next_group_start = ext4_group_first_block_no(sb, group + 1); 3283fbea4b3SYongqiang Yang group -= group_data[0].group; 3293fbea4b3SYongqiang Yang 330b93c9535STheodore Ts'o if (start_blk + itb > next_group_start) { 331b93c9535STheodore Ts'o flex_gd->bg_flags[group + 1] &= uninit_mask; 332b93c9535STheodore Ts'o overhead = start_blk + itb - next_group_start; 333d77147ffSharshads group_data[group + 1].mdata_blocks += overhead; 334b93c9535STheodore Ts'o itb -= overhead; 335b93c9535STheodore Ts'o } 336b93c9535STheodore Ts'o 337d77147ffSharshads group_data[group].mdata_blocks += itb; 338b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3393fbea4b3SYongqiang Yang start_blk += EXT4_SB(sb)->s_itb_per_group; 3403fbea4b3SYongqiang Yang } 3413fbea4b3SYongqiang Yang 342d77147ffSharshads /* Update free clusters count to exclude metadata blocks */ 343d77147ffSharshads for (i = 0; i < flex_gd->count; i++) { 344d77147ffSharshads group_data[i].free_clusters_count -= 345d77147ffSharshads EXT4_NUM_B2C(EXT4_SB(sb), 346d77147ffSharshads group_data[i].mdata_blocks); 347d77147ffSharshads } 348d77147ffSharshads 3493fbea4b3SYongqiang Yang if (test_opt(sb, DEBUG)) { 3503fbea4b3SYongqiang Yang int i; 3513fbea4b3SYongqiang Yang group = group_data[0].group; 3523fbea4b3SYongqiang Yang 3533fbea4b3SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: adding a flex group with " 3543fbea4b3SYongqiang Yang "%d groups, flexbg size is %d:\n", flex_gd->count, 3553fbea4b3SYongqiang Yang flexbg_size); 3563fbea4b3SYongqiang Yang 3573fbea4b3SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 358d77147ffSharshads ext4_debug( 359d77147ffSharshads "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n", 3603fbea4b3SYongqiang Yang ext4_bg_has_super(sb, group + i) ? "normal" : 3613fbea4b3SYongqiang Yang "no-super", group + i, 3623fbea4b3SYongqiang Yang group_data[i].blocks_count, 363d77147ffSharshads group_data[i].free_clusters_count, 364d77147ffSharshads group_data[i].mdata_blocks); 3653fbea4b3SYongqiang Yang } 3663fbea4b3SYongqiang Yang } 36703c1c290SYongqiang Yang return 0; 3683fbea4b3SYongqiang Yang } 3693fbea4b3SYongqiang Yang 370ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 371617ba13bSMingming Cao ext4_fsblk_t blk) 372ac27a0ecSDave Kleikamp { 373ac27a0ecSDave Kleikamp struct buffer_head *bh; 374ac27a0ecSDave Kleikamp int err; 375ac27a0ecSDave Kleikamp 376ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 377aebf0243SWang Shilong if (unlikely(!bh)) 378860d21e2STheodore Ts'o return ERR_PTR(-ENOMEM); 3795d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 380617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 381ac27a0ecSDave Kleikamp brelse(bh); 382ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 383ac27a0ecSDave Kleikamp } else { 384ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 385ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 386ac27a0ecSDave Kleikamp } 387ac27a0ecSDave Kleikamp 388ac27a0ecSDave Kleikamp return bh; 389ac27a0ecSDave Kleikamp } 390ac27a0ecSDave Kleikamp 391ac27a0ecSDave Kleikamp /* 39214904107SEric Sandeen * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA. 39314904107SEric Sandeen * If that fails, restart the transaction & regain write access for the 39414904107SEric Sandeen * buffer head which is used for block_bitmap modifications. 39514904107SEric Sandeen */ 3966d40bc5aSYongqiang Yang static int extend_or_restart_transaction(handle_t *handle, int thresh) 39714904107SEric Sandeen { 39814904107SEric Sandeen int err; 39914904107SEric Sandeen 4000390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, thresh)) 40114904107SEric Sandeen return 0; 40214904107SEric Sandeen 40314904107SEric Sandeen err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA); 40414904107SEric Sandeen if (err < 0) 40514904107SEric Sandeen return err; 40614904107SEric Sandeen if (err) { 4076d40bc5aSYongqiang Yang err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA); 4086d40bc5aSYongqiang Yang if (err) 40914904107SEric Sandeen return err; 41014904107SEric Sandeen } 41114904107SEric Sandeen 41214904107SEric Sandeen return 0; 41314904107SEric Sandeen } 41414904107SEric Sandeen 41514904107SEric Sandeen /* 416d77147ffSharshads * set_flexbg_block_bitmap() mark clusters [@first_cluster, @last_cluster] used. 41733afdcc5SYongqiang Yang * 41833afdcc5SYongqiang Yang * Helper function for ext4_setup_new_group_blocks() which set . 41933afdcc5SYongqiang Yang * 42033afdcc5SYongqiang Yang * @sb: super block 42133afdcc5SYongqiang Yang * @handle: journal handle 42233afdcc5SYongqiang Yang * @flex_gd: flex group data 42333afdcc5SYongqiang Yang */ 42433afdcc5SYongqiang Yang static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle, 42533afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 426d77147ffSharshads ext4_fsblk_t first_cluster, ext4_fsblk_t last_cluster) 42733afdcc5SYongqiang Yang { 428d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 429d77147ffSharshads ext4_group_t count = last_cluster - first_cluster + 1; 43033afdcc5SYongqiang Yang ext4_group_t count2; 43133afdcc5SYongqiang Yang 432d77147ffSharshads ext4_debug("mark clusters [%llu-%llu] used\n", first_cluster, 433d77147ffSharshads last_cluster); 434d77147ffSharshads for (count2 = count; count > 0; 435d77147ffSharshads count -= count2, first_cluster += count2) { 43633afdcc5SYongqiang Yang ext4_fsblk_t start; 43733afdcc5SYongqiang Yang struct buffer_head *bh; 43833afdcc5SYongqiang Yang ext4_group_t group; 43933afdcc5SYongqiang Yang int err; 44033afdcc5SYongqiang Yang 441d77147ffSharshads group = ext4_get_group_number(sb, EXT4_C2B(sbi, first_cluster)); 442d77147ffSharshads start = EXT4_B2C(sbi, ext4_group_first_block_no(sb, group)); 44333afdcc5SYongqiang Yang group -= flex_gd->groups[0].group; 44433afdcc5SYongqiang Yang 445d77147ffSharshads count2 = EXT4_CLUSTERS_PER_GROUP(sb) - (first_cluster - start); 44633afdcc5SYongqiang Yang if (count2 > count) 44733afdcc5SYongqiang Yang count2 = count; 44833afdcc5SYongqiang Yang 44933afdcc5SYongqiang Yang if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) { 45033afdcc5SYongqiang Yang BUG_ON(flex_gd->count > 1); 45133afdcc5SYongqiang Yang continue; 45233afdcc5SYongqiang Yang } 45333afdcc5SYongqiang Yang 45433afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 45533afdcc5SYongqiang Yang if (err) 45633afdcc5SYongqiang Yang return err; 45733afdcc5SYongqiang Yang 45833afdcc5SYongqiang Yang bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap); 459aebf0243SWang Shilong if (unlikely(!bh)) 460860d21e2STheodore Ts'o return -ENOMEM; 46133afdcc5SYongqiang Yang 4625d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 46333afdcc5SYongqiang Yang err = ext4_journal_get_write_access(handle, bh); 464cea57941SVasily Averin if (err) { 465cea57941SVasily Averin brelse(bh); 46633afdcc5SYongqiang Yang return err; 467cea57941SVasily Averin } 468d77147ffSharshads ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", 469d77147ffSharshads first_cluster, first_cluster - start, count2); 470d77147ffSharshads ext4_set_bits(bh->b_data, first_cluster - start, count2); 47133afdcc5SYongqiang Yang 47233afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 473cea57941SVasily Averin brelse(bh); 47433afdcc5SYongqiang Yang if (unlikely(err)) 47533afdcc5SYongqiang Yang return err; 47633afdcc5SYongqiang Yang } 47733afdcc5SYongqiang Yang 47833afdcc5SYongqiang Yang return 0; 47933afdcc5SYongqiang Yang } 48033afdcc5SYongqiang Yang 48133afdcc5SYongqiang Yang /* 48233afdcc5SYongqiang Yang * Set up the block and inode bitmaps, and the inode table for the new groups. 48333afdcc5SYongqiang Yang * This doesn't need to be part of the main transaction, since we are only 48433afdcc5SYongqiang Yang * changing blocks outside the actual filesystem. We still do journaling to 48533afdcc5SYongqiang Yang * ensure the recovery is correct in case of a failure just after resize. 48633afdcc5SYongqiang Yang * If any part of this fails, we simply abort the resize. 48733afdcc5SYongqiang Yang * 48833afdcc5SYongqiang Yang * setup_new_flex_group_blocks handles a flex group as follow: 48933afdcc5SYongqiang Yang * 1. copy super block and GDT, and initialize group tables if necessary. 49033afdcc5SYongqiang Yang * In this step, we only set bits in blocks bitmaps for blocks taken by 49133afdcc5SYongqiang Yang * super block and GDT. 49233afdcc5SYongqiang Yang * 2. allocate group tables in block bitmaps, that is, set bits in block 49333afdcc5SYongqiang Yang * bitmap for blocks taken by group tables. 49433afdcc5SYongqiang Yang */ 49533afdcc5SYongqiang Yang static int setup_new_flex_group_blocks(struct super_block *sb, 49633afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 49733afdcc5SYongqiang Yang { 49833afdcc5SYongqiang Yang int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group}; 49933afdcc5SYongqiang Yang ext4_fsblk_t start; 50033afdcc5SYongqiang Yang ext4_fsblk_t block; 50133afdcc5SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 50233afdcc5SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 50333afdcc5SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 50433afdcc5SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 50533afdcc5SYongqiang Yang handle_t *handle; 50633afdcc5SYongqiang Yang ext4_group_t group, count; 50733afdcc5SYongqiang Yang struct buffer_head *bh = NULL; 50833afdcc5SYongqiang Yang int reserved_gdb, i, j, err = 0, err2; 50901f795f9SYongqiang Yang int meta_bg; 51033afdcc5SYongqiang Yang 51133afdcc5SYongqiang Yang BUG_ON(!flex_gd->count || !group_data || 51233afdcc5SYongqiang Yang group_data[0].group != sbi->s_groups_count); 51333afdcc5SYongqiang Yang 51433afdcc5SYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 515e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 51633afdcc5SYongqiang Yang 51733afdcc5SYongqiang Yang /* This transaction may be extended/restarted along the way */ 5189924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 51933afdcc5SYongqiang Yang if (IS_ERR(handle)) 52033afdcc5SYongqiang Yang return PTR_ERR(handle); 52133afdcc5SYongqiang Yang 52233afdcc5SYongqiang Yang group = group_data[0].group; 52333afdcc5SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group++) { 52433afdcc5SYongqiang Yang unsigned long gdblocks; 52501f795f9SYongqiang Yang ext4_grpblk_t overhead; 52633afdcc5SYongqiang Yang 52733afdcc5SYongqiang Yang gdblocks = ext4_bg_num_gdb(sb, group); 52833afdcc5SYongqiang Yang start = ext4_group_first_block_no(sb, group); 52933afdcc5SYongqiang Yang 53001f795f9SYongqiang Yang if (meta_bg == 0 && !ext4_bg_has_super(sb, group)) 5316df935adSYongqiang Yang goto handle_itb; 5326df935adSYongqiang Yang 53301f795f9SYongqiang Yang if (meta_bg == 1) { 53401f795f9SYongqiang Yang ext4_group_t first_group; 53501f795f9SYongqiang Yang first_group = ext4_meta_bg_first_group(sb, group); 53601f795f9SYongqiang Yang if (first_group != group + 1 && 53701f795f9SYongqiang Yang first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1) 53801f795f9SYongqiang Yang goto handle_itb; 53901f795f9SYongqiang Yang } 54001f795f9SYongqiang Yang 54101f795f9SYongqiang Yang block = start + ext4_bg_has_super(sb, group); 54233afdcc5SYongqiang Yang /* Copy all of the GDT blocks into the backup in this group */ 54301f795f9SYongqiang Yang for (j = 0; j < gdblocks; j++, block++) { 54433afdcc5SYongqiang Yang struct buffer_head *gdb; 54533afdcc5SYongqiang Yang 54633afdcc5SYongqiang Yang ext4_debug("update backup group %#04llx\n", block); 54733afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 54833afdcc5SYongqiang Yang if (err) 54933afdcc5SYongqiang Yang goto out; 55033afdcc5SYongqiang Yang 55133afdcc5SYongqiang Yang gdb = sb_getblk(sb, block); 552aebf0243SWang Shilong if (unlikely(!gdb)) { 553860d21e2STheodore Ts'o err = -ENOMEM; 55433afdcc5SYongqiang Yang goto out; 55533afdcc5SYongqiang Yang } 55633afdcc5SYongqiang Yang 5575d601255Sliang xie BUFFER_TRACE(gdb, "get_write_access"); 55833afdcc5SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb); 55933afdcc5SYongqiang Yang if (err) { 56033afdcc5SYongqiang Yang brelse(gdb); 56133afdcc5SYongqiang Yang goto out; 56233afdcc5SYongqiang Yang } 56333afdcc5SYongqiang Yang memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data, 56433afdcc5SYongqiang Yang gdb->b_size); 56533afdcc5SYongqiang Yang set_buffer_uptodate(gdb); 56633afdcc5SYongqiang Yang 56733afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb); 56833afdcc5SYongqiang Yang if (unlikely(err)) { 56933afdcc5SYongqiang Yang brelse(gdb); 57033afdcc5SYongqiang Yang goto out; 57133afdcc5SYongqiang Yang } 57233afdcc5SYongqiang Yang brelse(gdb); 57333afdcc5SYongqiang Yang } 57433afdcc5SYongqiang Yang 57533afdcc5SYongqiang Yang /* Zero out all of the reserved backup group descriptor 57633afdcc5SYongqiang Yang * table blocks 57733afdcc5SYongqiang Yang */ 57833afdcc5SYongqiang Yang if (ext4_bg_has_super(sb, group)) { 57933afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, gdblocks + start + 1, 58033afdcc5SYongqiang Yang reserved_gdb, GFP_NOFS); 58133afdcc5SYongqiang Yang if (err) 58233afdcc5SYongqiang Yang goto out; 58333afdcc5SYongqiang Yang } 58433afdcc5SYongqiang Yang 5856df935adSYongqiang Yang handle_itb: 58633afdcc5SYongqiang Yang /* Initialize group tables of the grop @group */ 58733afdcc5SYongqiang Yang if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED)) 58833afdcc5SYongqiang Yang goto handle_bb; 58933afdcc5SYongqiang Yang 59033afdcc5SYongqiang Yang /* Zero out all of the inode table blocks */ 59133afdcc5SYongqiang Yang block = group_data[i].inode_table; 59233afdcc5SYongqiang Yang ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 59333afdcc5SYongqiang Yang block, sbi->s_itb_per_group); 59433afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, 59533afdcc5SYongqiang Yang GFP_NOFS); 59633afdcc5SYongqiang Yang if (err) 59733afdcc5SYongqiang Yang goto out; 59833afdcc5SYongqiang Yang 59933afdcc5SYongqiang Yang handle_bb: 60033afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT) 60133afdcc5SYongqiang Yang goto handle_ib; 60233afdcc5SYongqiang Yang 60333afdcc5SYongqiang Yang /* Initialize block bitmap of the @group */ 60433afdcc5SYongqiang Yang block = group_data[i].block_bitmap; 60533afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 60633afdcc5SYongqiang Yang if (err) 60733afdcc5SYongqiang Yang goto out; 60833afdcc5SYongqiang Yang 60933afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 61033afdcc5SYongqiang Yang if (IS_ERR(bh)) { 61133afdcc5SYongqiang Yang err = PTR_ERR(bh); 61233afdcc5SYongqiang Yang goto out; 61333afdcc5SYongqiang Yang } 61401f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group); 61501f795f9SYongqiang Yang if (overhead != 0) { 61633afdcc5SYongqiang Yang ext4_debug("mark backup superblock %#04llx (+0)\n", 61733afdcc5SYongqiang Yang start); 618d77147ffSharshads ext4_set_bits(bh->b_data, 0, 619d77147ffSharshads EXT4_NUM_B2C(sbi, overhead)); 62033afdcc5SYongqiang Yang } 621d77147ffSharshads ext4_mark_bitmap_end(EXT4_B2C(sbi, group_data[i].blocks_count), 62233afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 62333afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6249e402893SVasily Averin brelse(bh); 62533afdcc5SYongqiang Yang if (err) 62633afdcc5SYongqiang Yang goto out; 62733afdcc5SYongqiang Yang 62833afdcc5SYongqiang Yang handle_ib: 62933afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_INODE_UNINIT) 63033afdcc5SYongqiang Yang continue; 63133afdcc5SYongqiang Yang 63233afdcc5SYongqiang Yang /* Initialize inode bitmap of the @group */ 63333afdcc5SYongqiang Yang block = group_data[i].inode_bitmap; 63433afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 63533afdcc5SYongqiang Yang if (err) 63633afdcc5SYongqiang Yang goto out; 63733afdcc5SYongqiang Yang /* Mark unused entries in inode bitmap used */ 63833afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 63933afdcc5SYongqiang Yang if (IS_ERR(bh)) { 64033afdcc5SYongqiang Yang err = PTR_ERR(bh); 64133afdcc5SYongqiang Yang goto out; 64233afdcc5SYongqiang Yang } 64333afdcc5SYongqiang Yang 64433afdcc5SYongqiang Yang ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 64533afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 64633afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 6479e402893SVasily Averin brelse(bh); 64833afdcc5SYongqiang Yang if (err) 64933afdcc5SYongqiang Yang goto out; 65033afdcc5SYongqiang Yang } 65133afdcc5SYongqiang Yang 65233afdcc5SYongqiang Yang /* Mark group tables in block bitmap */ 65333afdcc5SYongqiang Yang for (j = 0; j < GROUP_TABLE_COUNT; j++) { 65433afdcc5SYongqiang Yang count = group_table_count[j]; 65533afdcc5SYongqiang Yang start = (&group_data[0].block_bitmap)[j]; 65633afdcc5SYongqiang Yang block = start; 65733afdcc5SYongqiang Yang for (i = 1; i < flex_gd->count; i++) { 65833afdcc5SYongqiang Yang block += group_table_count[j]; 65933afdcc5SYongqiang Yang if (block == (&group_data[i].block_bitmap)[j]) { 66033afdcc5SYongqiang Yang count += group_table_count[j]; 66133afdcc5SYongqiang Yang continue; 66233afdcc5SYongqiang Yang } 66333afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 664d77147ffSharshads flex_gd, 665d77147ffSharshads EXT4_B2C(sbi, start), 666d77147ffSharshads EXT4_B2C(sbi, 667d77147ffSharshads start + count 668d77147ffSharshads - 1)); 66933afdcc5SYongqiang Yang if (err) 67033afdcc5SYongqiang Yang goto out; 67133afdcc5SYongqiang Yang count = group_table_count[j]; 672b93c9535STheodore Ts'o start = (&group_data[i].block_bitmap)[j]; 67333afdcc5SYongqiang Yang block = start; 67433afdcc5SYongqiang Yang } 67533afdcc5SYongqiang Yang 67633afdcc5SYongqiang Yang if (count) { 67733afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 678d77147ffSharshads flex_gd, 679d77147ffSharshads EXT4_B2C(sbi, start), 680d77147ffSharshads EXT4_B2C(sbi, 681d77147ffSharshads start + count 682d77147ffSharshads - 1)); 68333afdcc5SYongqiang Yang if (err) 68433afdcc5SYongqiang Yang goto out; 68533afdcc5SYongqiang Yang } 68633afdcc5SYongqiang Yang } 68733afdcc5SYongqiang Yang 68833afdcc5SYongqiang Yang out: 68933afdcc5SYongqiang Yang err2 = ext4_journal_stop(handle); 69033afdcc5SYongqiang Yang if (err2 && !err) 69133afdcc5SYongqiang Yang err = err2; 69233afdcc5SYongqiang Yang 69333afdcc5SYongqiang Yang return err; 69433afdcc5SYongqiang Yang } 69533afdcc5SYongqiang Yang 69633afdcc5SYongqiang Yang /* 697ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 698617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 699ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 700ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 701ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 702ac27a0ecSDave Kleikamp */ 703617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 704ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 705ac27a0ecSDave Kleikamp { 706ac27a0ecSDave Kleikamp unsigned *min = three; 707ac27a0ecSDave Kleikamp int mult = 3; 708ac27a0ecSDave Kleikamp unsigned ret; 709ac27a0ecSDave Kleikamp 710e2b911c5SDarrick J. Wong if (!ext4_has_feature_sparse_super(sb)) { 711ac27a0ecSDave Kleikamp ret = *min; 712ac27a0ecSDave Kleikamp *min += 1; 713ac27a0ecSDave Kleikamp return ret; 714ac27a0ecSDave Kleikamp } 715ac27a0ecSDave Kleikamp 716ac27a0ecSDave Kleikamp if (*five < *min) { 717ac27a0ecSDave Kleikamp min = five; 718ac27a0ecSDave Kleikamp mult = 5; 719ac27a0ecSDave Kleikamp } 720ac27a0ecSDave Kleikamp if (*seven < *min) { 721ac27a0ecSDave Kleikamp min = seven; 722ac27a0ecSDave Kleikamp mult = 7; 723ac27a0ecSDave Kleikamp } 724ac27a0ecSDave Kleikamp 725ac27a0ecSDave Kleikamp ret = *min; 726ac27a0ecSDave Kleikamp *min *= mult; 727ac27a0ecSDave Kleikamp 728ac27a0ecSDave Kleikamp return ret; 729ac27a0ecSDave Kleikamp } 730ac27a0ecSDave Kleikamp 731ac27a0ecSDave Kleikamp /* 732ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 733ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 734ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 735ac27a0ecSDave Kleikamp */ 736ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 737c72df9f9SYongqiang Yang ext4_group_t end, 738ac27a0ecSDave Kleikamp struct buffer_head *primary) 739ac27a0ecSDave Kleikamp { 740617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 741ac27a0ecSDave Kleikamp unsigned three = 1; 742ac27a0ecSDave Kleikamp unsigned five = 5; 743ac27a0ecSDave Kleikamp unsigned seven = 7; 744ac27a0ecSDave Kleikamp unsigned grp; 745ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 746ac27a0ecSDave Kleikamp int gdbackups = 0; 747ac27a0ecSDave Kleikamp 748617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 749bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 750bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 75112062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 7522ae02107SMingming Cao " missing grp %d (%llu)", 753ac27a0ecSDave Kleikamp blk, grp, 754bd81d8eeSLaurent Vivier grp * 755bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 756bd81d8eeSLaurent Vivier blk); 757ac27a0ecSDave Kleikamp return -EINVAL; 758ac27a0ecSDave Kleikamp } 759617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 760ac27a0ecSDave Kleikamp return -EFBIG; 761ac27a0ecSDave Kleikamp } 762ac27a0ecSDave Kleikamp 763ac27a0ecSDave Kleikamp return gdbackups; 764ac27a0ecSDave Kleikamp } 765ac27a0ecSDave Kleikamp 766ac27a0ecSDave Kleikamp /* 767ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 768ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 769ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 770ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 771ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 772ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 773ac27a0ecSDave Kleikamp * 774ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 775ac27a0ecSDave Kleikamp * 776ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 777ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 778ac27a0ecSDave Kleikamp */ 779ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 7802f919710SYongqiang Yang ext4_group_t group) 781ac27a0ecSDave Kleikamp { 782ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 783617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 7842f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 785617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 786fb265c9cSTheodore Ts'o struct buffer_head **o_group_desc, **n_group_desc = NULL; 787fb265c9cSTheodore Ts'o struct buffer_head *dind = NULL; 788fb265c9cSTheodore Ts'o struct buffer_head *gdb_bh = NULL; 789ac27a0ecSDave Kleikamp int gdbackups; 790fb265c9cSTheodore Ts'o struct ext4_iloc iloc = { .bh = NULL }; 791ac27a0ecSDave Kleikamp __le32 *data; 792ac27a0ecSDave Kleikamp int err; 793ac27a0ecSDave Kleikamp 794ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 795ac27a0ecSDave Kleikamp printk(KERN_DEBUG 796617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 797ac27a0ecSDave Kleikamp gdb_num); 798ac27a0ecSDave Kleikamp 799fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 800fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 801fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 802ac27a0ecSDave Kleikamp 803c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, gdb_bh); 8042f919710SYongqiang Yang if (gdbackups < 0) { 805ac27a0ecSDave Kleikamp err = gdbackups; 806fb265c9cSTheodore Ts'o goto errout; 807ac27a0ecSDave Kleikamp } 808ac27a0ecSDave Kleikamp 809617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 810fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 811fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 812fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 813fb265c9cSTheodore Ts'o dind = NULL; 814fb265c9cSTheodore Ts'o goto errout; 815ac27a0ecSDave Kleikamp } 816ac27a0ecSDave Kleikamp 817ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 818617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 81912062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 8202f919710SYongqiang Yang group, gdblock); 821ac27a0ecSDave Kleikamp err = -EINVAL; 822fb265c9cSTheodore Ts'o goto errout; 823ac27a0ecSDave Kleikamp } 824ac27a0ecSDave Kleikamp 8255d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 826b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 827b4097142STheodore Ts'o if (unlikely(err)) 828fb265c9cSTheodore Ts'o goto errout; 829ac27a0ecSDave Kleikamp 8305d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 8312f919710SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 832b4097142STheodore Ts'o if (unlikely(err)) 833fb265c9cSTheodore Ts'o goto errout; 834ac27a0ecSDave Kleikamp 8355d601255Sliang xie BUFFER_TRACE(dind, "get_write_access"); 836b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, dind); 837b4097142STheodore Ts'o if (unlikely(err)) 838b4097142STheodore Ts'o ext4_std_error(sb, err); 839ac27a0ecSDave Kleikamp 840617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 841b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 842b4097142STheodore Ts'o if (unlikely(err)) 843fb265c9cSTheodore Ts'o goto errout; 844ac27a0ecSDave Kleikamp 845f18a5f21STheodore Ts'o n_group_desc = ext4_kvmalloc((gdb_num + 1) * 846f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 847216553c4SJosef Bacik GFP_NOFS); 848ac27a0ecSDave Kleikamp if (!n_group_desc) { 849ac27a0ecSDave Kleikamp err = -ENOMEM; 850f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 851f18a5f21STheodore Ts'o gdb_num + 1); 852fb265c9cSTheodore Ts'o goto errout; 853ac27a0ecSDave Kleikamp } 854ac27a0ecSDave Kleikamp 855ac27a0ecSDave Kleikamp /* 856ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 857ac27a0ecSDave Kleikamp * 858ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 859ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 860ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 861ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 862ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 863ac27a0ecSDave Kleikamp */ 864617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 865b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 866b4097142STheodore Ts'o if (unlikely(err)) { 867b4097142STheodore Ts'o ext4_std_error(sb, err); 868fb265c9cSTheodore Ts'o goto errout; 869b4097142STheodore Ts'o } 870d77147ffSharshads inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 871d77147ffSharshads (9 - EXT4_SB(sb)->s_cluster_bits); 872617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 8732f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 8742f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 875b4097142STheodore Ts'o if (unlikely(err)) { 876b4097142STheodore Ts'o ext4_std_error(sb, err); 877fb265c9cSTheodore Ts'o goto errout; 878b4097142STheodore Ts'o } 879b4097142STheodore Ts'o brelse(dind); 880ac27a0ecSDave Kleikamp 881617ba13bSMingming Cao o_group_desc = EXT4_SB(sb)->s_group_desc; 882ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 883617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 8842f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 885617ba13bSMingming Cao EXT4_SB(sb)->s_group_desc = n_group_desc; 886617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 887b93b41d4SAl Viro kvfree(o_group_desc); 888ac27a0ecSDave Kleikamp 889e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 890b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 891b4097142STheodore Ts'o if (err) 892b4097142STheodore Ts'o ext4_std_error(sb, err); 893b4097142STheodore Ts'o return err; 894fb265c9cSTheodore Ts'o errout: 895b93b41d4SAl Viro kvfree(n_group_desc); 896ac27a0ecSDave Kleikamp brelse(iloc.bh); 897ac27a0ecSDave Kleikamp brelse(dind); 8982f919710SYongqiang Yang brelse(gdb_bh); 899ac27a0ecSDave Kleikamp 900617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 901ac27a0ecSDave Kleikamp return err; 902ac27a0ecSDave Kleikamp } 903ac27a0ecSDave Kleikamp 904ac27a0ecSDave Kleikamp /* 90501f795f9SYongqiang Yang * add_new_gdb_meta_bg is the sister of add_new_gdb. 90601f795f9SYongqiang Yang */ 90701f795f9SYongqiang Yang static int add_new_gdb_meta_bg(struct super_block *sb, 90801f795f9SYongqiang Yang handle_t *handle, ext4_group_t group) { 90901f795f9SYongqiang Yang ext4_fsblk_t gdblock; 91001f795f9SYongqiang Yang struct buffer_head *gdb_bh; 91101f795f9SYongqiang Yang struct buffer_head **o_group_desc, **n_group_desc; 91201f795f9SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 91301f795f9SYongqiang Yang int err; 91401f795f9SYongqiang Yang 91501f795f9SYongqiang Yang gdblock = ext4_meta_bg_first_block_no(sb, group) + 91601f795f9SYongqiang Yang ext4_bg_has_super(sb, group); 917fb265c9cSTheodore Ts'o gdb_bh = ext4_sb_bread(sb, gdblock, 0); 918fb265c9cSTheodore Ts'o if (IS_ERR(gdb_bh)) 919fb265c9cSTheodore Ts'o return PTR_ERR(gdb_bh); 92001f795f9SYongqiang Yang n_group_desc = ext4_kvmalloc((gdb_num + 1) * 92101f795f9SYongqiang Yang sizeof(struct buffer_head *), 92201f795f9SYongqiang Yang GFP_NOFS); 92301f795f9SYongqiang Yang if (!n_group_desc) { 92461a9c11eSVasily Averin brelse(gdb_bh); 92501f795f9SYongqiang Yang err = -ENOMEM; 92601f795f9SYongqiang Yang ext4_warning(sb, "not enough memory for %lu groups", 92701f795f9SYongqiang Yang gdb_num + 1); 92801f795f9SYongqiang Yang return err; 92901f795f9SYongqiang Yang } 93001f795f9SYongqiang Yang 93101f795f9SYongqiang Yang o_group_desc = EXT4_SB(sb)->s_group_desc; 93201f795f9SYongqiang Yang memcpy(n_group_desc, o_group_desc, 93301f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 93401f795f9SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 93501f795f9SYongqiang Yang EXT4_SB(sb)->s_group_desc = n_group_desc; 93601f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count++; 937b93b41d4SAl Viro kvfree(o_group_desc); 9385d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 93901f795f9SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 94001f795f9SYongqiang Yang return err; 94101f795f9SYongqiang Yang } 94201f795f9SYongqiang Yang 94301f795f9SYongqiang Yang /* 944ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 945ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 946ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 947ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 948ac27a0ecSDave Kleikamp * 949ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 950ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 951ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 952ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 953ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 954ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 955ac27a0ecSDave Kleikamp */ 956ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 957668f4dc5SYongqiang Yang ext4_group_t group) 958ac27a0ecSDave Kleikamp { 959ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 960617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 961d77147ffSharshads int cluster_bits = EXT4_SB(sb)->s_cluster_bits; 962ac27a0ecSDave Kleikamp struct buffer_head **primary; 963ac27a0ecSDave Kleikamp struct buffer_head *dind; 964617ba13bSMingming Cao struct ext4_iloc iloc; 965617ba13bSMingming Cao ext4_fsblk_t blk; 966ac27a0ecSDave Kleikamp __le32 *data, *end; 967ac27a0ecSDave Kleikamp int gdbackups = 0; 968ac27a0ecSDave Kleikamp int res, i; 969ac27a0ecSDave Kleikamp int err; 970ac27a0ecSDave Kleikamp 9716da2ec56SKees Cook primary = kmalloc_array(reserved_gdb, sizeof(*primary), GFP_NOFS); 972ac27a0ecSDave Kleikamp if (!primary) 973ac27a0ecSDave Kleikamp return -ENOMEM; 974ac27a0ecSDave Kleikamp 975617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 976fb265c9cSTheodore Ts'o dind = ext4_sb_bread(sb, le32_to_cpu(*data), 0); 977fb265c9cSTheodore Ts'o if (IS_ERR(dind)) { 978fb265c9cSTheodore Ts'o err = PTR_ERR(dind); 979fb265c9cSTheodore Ts'o dind = NULL; 980ac27a0ecSDave Kleikamp goto exit_free; 981ac27a0ecSDave Kleikamp } 982ac27a0ecSDave Kleikamp 983617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 98494460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 98594460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 986617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 987ac27a0ecSDave Kleikamp 988ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 989ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 990ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 99112062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 992ac27a0ecSDave Kleikamp " not at offset %ld", 993ac27a0ecSDave Kleikamp blk, 994ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 995ac27a0ecSDave Kleikamp err = -EINVAL; 996ac27a0ecSDave Kleikamp goto exit_bh; 997ac27a0ecSDave Kleikamp } 998fb265c9cSTheodore Ts'o primary[res] = ext4_sb_bread(sb, blk, 0); 999fb265c9cSTheodore Ts'o if (IS_ERR(primary[res])) { 1000fb265c9cSTheodore Ts'o err = PTR_ERR(primary[res]); 1001fb265c9cSTheodore Ts'o primary[res] = NULL; 1002ac27a0ecSDave Kleikamp goto exit_bh; 1003ac27a0ecSDave Kleikamp } 1004c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, primary[res]); 1005c72df9f9SYongqiang Yang if (gdbackups < 0) { 1006ac27a0ecSDave Kleikamp brelse(primary[res]); 1007ac27a0ecSDave Kleikamp err = gdbackups; 1008ac27a0ecSDave Kleikamp goto exit_bh; 1009ac27a0ecSDave Kleikamp } 1010ac27a0ecSDave Kleikamp if (++data >= end) 1011ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 1012ac27a0ecSDave Kleikamp } 1013ac27a0ecSDave Kleikamp 1014ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 10155d601255Sliang xie BUFFER_TRACE(primary[i], "get_write_access"); 101637be2f59SEric Sandeen if ((err = ext4_journal_get_write_access(handle, primary[i]))) 1017ac27a0ecSDave Kleikamp goto exit_bh; 1018ac27a0ecSDave Kleikamp } 1019ac27a0ecSDave Kleikamp 1020617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 1021ac27a0ecSDave Kleikamp goto exit_bh; 1022ac27a0ecSDave Kleikamp 1023ac27a0ecSDave Kleikamp /* 1024ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 1025ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 1026ac27a0ecSDave Kleikamp */ 1027668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 1028ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 1029ac27a0ecSDave Kleikamp int err2; 1030ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 1031ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 1032ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 1033ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 1034ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 10350390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 1036ac27a0ecSDave Kleikamp if (!err) 1037ac27a0ecSDave Kleikamp err = err2; 1038ac27a0ecSDave Kleikamp } 1039d77147ffSharshads 1040d77147ffSharshads inode->i_blocks += reserved_gdb * sb->s_blocksize >> (9 - cluster_bits); 1041617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 1042ac27a0ecSDave Kleikamp 1043ac27a0ecSDave Kleikamp exit_bh: 1044ac27a0ecSDave Kleikamp while (--res >= 0) 1045ac27a0ecSDave Kleikamp brelse(primary[res]); 1046ac27a0ecSDave Kleikamp brelse(dind); 1047ac27a0ecSDave Kleikamp 1048ac27a0ecSDave Kleikamp exit_free: 1049ac27a0ecSDave Kleikamp kfree(primary); 1050ac27a0ecSDave Kleikamp 1051ac27a0ecSDave Kleikamp return err; 1052ac27a0ecSDave Kleikamp } 1053ac27a0ecSDave Kleikamp 1054ac27a0ecSDave Kleikamp /* 1055617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 1056ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 1057ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 1058ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 1059ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 1060ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 1061ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 1062ac27a0ecSDave Kleikamp * 106332ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 106432ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 106532ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 106632ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 106732ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 106832ed5058STheodore Ts'o * which changed s_groups_count will backup again. 1069ac27a0ecSDave Kleikamp */ 1070904dad47SAndy Leiserson static void update_backups(struct super_block *sb, sector_t blk_off, char *data, 107101f795f9SYongqiang Yang int size, int meta_bg) 1072ac27a0ecSDave Kleikamp { 1073617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 107401f795f9SYongqiang Yang ext4_group_t last; 1075617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 1076ac27a0ecSDave Kleikamp unsigned three = 1; 1077ac27a0ecSDave Kleikamp unsigned five = 5; 1078ac27a0ecSDave Kleikamp unsigned seven = 7; 107901f795f9SYongqiang Yang ext4_group_t group = 0; 1080ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 1081ac27a0ecSDave Kleikamp handle_t *handle; 1082ac27a0ecSDave Kleikamp int err = 0, err2; 1083ac27a0ecSDave Kleikamp 10849924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 1085ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1086ac27a0ecSDave Kleikamp group = 1; 1087ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 1088ac27a0ecSDave Kleikamp goto exit_err; 1089ac27a0ecSDave Kleikamp } 1090ac27a0ecSDave Kleikamp 109101f795f9SYongqiang Yang if (meta_bg == 0) { 109201f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 109301f795f9SYongqiang Yang last = sbi->s_groups_count; 109401f795f9SYongqiang Yang } else { 1095904dad47SAndy Leiserson group = ext4_get_group_number(sb, blk_off) + 1; 109601f795f9SYongqiang Yang last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2); 109701f795f9SYongqiang Yang } 109801f795f9SYongqiang Yang 109901f795f9SYongqiang Yang while (group < sbi->s_groups_count) { 1100ac27a0ecSDave Kleikamp struct buffer_head *bh; 110101f795f9SYongqiang Yang ext4_fsblk_t backup_block; 1102ac27a0ecSDave Kleikamp 1103ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 11040390131bSFrank Mayhar if (ext4_handle_valid(handle) && 11050390131bSFrank Mayhar handle->h_buffer_credits == 0 && 1106617ba13bSMingming Cao ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && 1107617ba13bSMingming Cao (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 1108ac27a0ecSDave Kleikamp break; 1109ac27a0ecSDave Kleikamp 111001f795f9SYongqiang Yang if (meta_bg == 0) 11119378c676SJan Kara backup_block = ((ext4_fsblk_t)group) * bpg + blk_off; 111201f795f9SYongqiang Yang else 111301f795f9SYongqiang Yang backup_block = (ext4_group_first_block_no(sb, group) + 111401f795f9SYongqiang Yang ext4_bg_has_super(sb, group)); 111501f795f9SYongqiang Yang 111601f795f9SYongqiang Yang bh = sb_getblk(sb, backup_block); 1117aebf0243SWang Shilong if (unlikely(!bh)) { 1118860d21e2STheodore Ts'o err = -ENOMEM; 1119ac27a0ecSDave Kleikamp break; 1120ac27a0ecSDave Kleikamp } 112101f795f9SYongqiang Yang ext4_debug("update metadata backup %llu(+%llu)\n", 112201f795f9SYongqiang Yang backup_block, backup_block - 112301f795f9SYongqiang Yang ext4_group_first_block_no(sb, group)); 11245d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 1125ea0abbb6SVasily Averin if ((err = ext4_journal_get_write_access(handle, bh))) { 1126ea0abbb6SVasily Averin brelse(bh); 1127ac27a0ecSDave Kleikamp break; 1128ea0abbb6SVasily Averin } 1129ac27a0ecSDave Kleikamp lock_buffer(bh); 1130ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 1131ac27a0ecSDave Kleikamp if (rest) 1132ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 1133ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1134ac27a0ecSDave Kleikamp unlock_buffer(bh); 1135b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 1136b4097142STheodore Ts'o if (unlikely(err)) 1137b4097142STheodore Ts'o ext4_std_error(sb, err); 1138ac27a0ecSDave Kleikamp brelse(bh); 113901f795f9SYongqiang Yang 114001f795f9SYongqiang Yang if (meta_bg == 0) 114101f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 114201f795f9SYongqiang Yang else if (group == last) 114301f795f9SYongqiang Yang break; 114401f795f9SYongqiang Yang else 114501f795f9SYongqiang Yang group = last; 1146ac27a0ecSDave Kleikamp } 1147617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 1148ac27a0ecSDave Kleikamp err = err2; 1149ac27a0ecSDave Kleikamp 1150ac27a0ecSDave Kleikamp /* 1151ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 1152ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 1153ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 1154ac27a0ecSDave Kleikamp * 1155ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 1156ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 1157ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 1158ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 1159ac27a0ecSDave Kleikamp */ 1160ac27a0ecSDave Kleikamp exit_err: 1161ac27a0ecSDave Kleikamp if (err) { 116212062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 1163ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 1164617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 1165617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 1166ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 1167ac27a0ecSDave Kleikamp } 1168ac27a0ecSDave Kleikamp } 1169ac27a0ecSDave Kleikamp 1170bb08c1e7SYongqiang Yang /* 1171bb08c1e7SYongqiang Yang * ext4_add_new_descs() adds @count group descriptor of groups 1172bb08c1e7SYongqiang Yang * starting at @group 1173bb08c1e7SYongqiang Yang * 1174bb08c1e7SYongqiang Yang * @handle: journal handle 1175bb08c1e7SYongqiang Yang * @sb: super block 1176bb08c1e7SYongqiang Yang * @group: the group no. of the first group desc to be added 1177bb08c1e7SYongqiang Yang * @resize_inode: the resize inode 1178bb08c1e7SYongqiang Yang * @count: number of group descriptors to be added 1179bb08c1e7SYongqiang Yang */ 1180bb08c1e7SYongqiang Yang static int ext4_add_new_descs(handle_t *handle, struct super_block *sb, 1181bb08c1e7SYongqiang Yang ext4_group_t group, struct inode *resize_inode, 1182bb08c1e7SYongqiang Yang ext4_group_t count) 1183bb08c1e7SYongqiang Yang { 1184bb08c1e7SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1185bb08c1e7SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 1186bb08c1e7SYongqiang Yang struct buffer_head *gdb_bh; 1187bb08c1e7SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 118801f795f9SYongqiang Yang int meta_bg; 1189bb08c1e7SYongqiang Yang 1190e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 1191bb08c1e7SYongqiang Yang for (i = 0; i < count; i++, group++) { 1192bb08c1e7SYongqiang Yang int reserved_gdb = ext4_bg_has_super(sb, group) ? 1193bb08c1e7SYongqiang Yang le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1194bb08c1e7SYongqiang Yang 1195bb08c1e7SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1196bb08c1e7SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1197bb08c1e7SYongqiang Yang 1198bb08c1e7SYongqiang Yang /* 1199bb08c1e7SYongqiang Yang * We will only either add reserved group blocks to a backup group 1200bb08c1e7SYongqiang Yang * or remove reserved blocks for the first group in a new group block. 1201bb08c1e7SYongqiang Yang * Doing both would be mean more complex code, and sane people don't 1202bb08c1e7SYongqiang Yang * use non-sparse filesystems anymore. This is already checked above. 1203bb08c1e7SYongqiang Yang */ 1204bb08c1e7SYongqiang Yang if (gdb_off) { 1205bb08c1e7SYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 12065d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 1207bb08c1e7SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 1208bb08c1e7SYongqiang Yang 1209bb08c1e7SYongqiang Yang if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 1210bb08c1e7SYongqiang Yang err = reserve_backup_gdb(handle, resize_inode, group); 121101f795f9SYongqiang Yang } else if (meta_bg != 0) { 121201f795f9SYongqiang Yang err = add_new_gdb_meta_bg(sb, handle, group); 121301f795f9SYongqiang Yang } else { 1214bb08c1e7SYongqiang Yang err = add_new_gdb(handle, resize_inode, group); 121501f795f9SYongqiang Yang } 1216bb08c1e7SYongqiang Yang if (err) 1217bb08c1e7SYongqiang Yang break; 1218bb08c1e7SYongqiang Yang } 1219bb08c1e7SYongqiang Yang return err; 1220bb08c1e7SYongqiang Yang } 1221bb08c1e7SYongqiang Yang 122241a246d1SDarrick J. Wong static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block) 122341a246d1SDarrick J. Wong { 122441a246d1SDarrick J. Wong struct buffer_head *bh = sb_getblk(sb, block); 1225aebf0243SWang Shilong if (unlikely(!bh)) 122641a246d1SDarrick J. Wong return NULL; 12277f1468d1SDmitry Monakhov if (!bh_uptodate_or_lock(bh)) { 122841a246d1SDarrick J. Wong if (bh_submit_read(bh) < 0) { 122941a246d1SDarrick J. Wong brelse(bh); 123041a246d1SDarrick J. Wong return NULL; 123141a246d1SDarrick J. Wong } 12327f1468d1SDmitry Monakhov } 123341a246d1SDarrick J. Wong 123441a246d1SDarrick J. Wong return bh; 123541a246d1SDarrick J. Wong } 123641a246d1SDarrick J. Wong 123741a246d1SDarrick J. Wong static int ext4_set_bitmap_checksums(struct super_block *sb, 123841a246d1SDarrick J. Wong ext4_group_t group, 123941a246d1SDarrick J. Wong struct ext4_group_desc *gdp, 124041a246d1SDarrick J. Wong struct ext4_new_group_data *group_data) 124141a246d1SDarrick J. Wong { 124241a246d1SDarrick J. Wong struct buffer_head *bh; 124341a246d1SDarrick J. Wong 12449aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 124541a246d1SDarrick J. Wong return 0; 124641a246d1SDarrick J. Wong 124741a246d1SDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->inode_bitmap); 124841a246d1SDarrick J. Wong if (!bh) 124941a246d1SDarrick J. Wong return -EIO; 125041a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, bh, 125141a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 125241a246d1SDarrick J. Wong brelse(bh); 125341a246d1SDarrick J. Wong 1254fa77dcfaSDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->block_bitmap); 1255fa77dcfaSDarrick J. Wong if (!bh) 1256fa77dcfaSDarrick J. Wong return -EIO; 125779f1ba49STao Ma ext4_block_bitmap_csum_set(sb, group, gdp, bh); 1258fa77dcfaSDarrick J. Wong brelse(bh); 1259fa77dcfaSDarrick J. Wong 126041a246d1SDarrick J. Wong return 0; 126141a246d1SDarrick J. Wong } 126241a246d1SDarrick J. Wong 1263083f5b24SYongqiang Yang /* 1264083f5b24SYongqiang Yang * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg 1265083f5b24SYongqiang Yang */ 1266083f5b24SYongqiang Yang static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb, 1267083f5b24SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 1268083f5b24SYongqiang Yang { 1269083f5b24SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 1270083f5b24SYongqiang Yang struct ext4_group_desc *gdp; 1271083f5b24SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1272083f5b24SYongqiang Yang struct buffer_head *gdb_bh; 1273083f5b24SYongqiang Yang ext4_group_t group; 1274083f5b24SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 1275083f5b24SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 1276083f5b24SYongqiang Yang 1277083f5b24SYongqiang Yang 1278083f5b24SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) { 1279083f5b24SYongqiang Yang group = group_data->group; 1280083f5b24SYongqiang Yang 1281083f5b24SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1282083f5b24SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1283083f5b24SYongqiang Yang 1284083f5b24SYongqiang Yang /* 1285083f5b24SYongqiang Yang * get_write_access() has been called on gdb_bh by ext4_add_new_desc(). 1286083f5b24SYongqiang Yang */ 1287083f5b24SYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 1288083f5b24SYongqiang Yang /* Update group descriptor block for new group */ 12892716b802STheodore Ts'o gdp = (struct ext4_group_desc *)(gdb_bh->b_data + 1290083f5b24SYongqiang Yang gdb_off * EXT4_DESC_SIZE(sb)); 1291083f5b24SYongqiang Yang 1292083f5b24SYongqiang Yang memset(gdp, 0, EXT4_DESC_SIZE(sb)); 1293083f5b24SYongqiang Yang ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap); 1294083f5b24SYongqiang Yang ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap); 129541a246d1SDarrick J. Wong err = ext4_set_bitmap_checksums(sb, group, gdp, group_data); 129641a246d1SDarrick J. Wong if (err) { 129741a246d1SDarrick J. Wong ext4_std_error(sb, err); 129841a246d1SDarrick J. Wong break; 129941a246d1SDarrick J. Wong } 130041a246d1SDarrick J. Wong 1301083f5b24SYongqiang Yang ext4_inode_table_set(sb, gdp, group_data->inode_table); 1302083f5b24SYongqiang Yang ext4_free_group_clusters_set(sb, gdp, 1303d77147ffSharshads group_data->free_clusters_count); 1304083f5b24SYongqiang Yang ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 130593f90526STheodore Ts'o if (ext4_has_group_desc_csum(sb)) 130693f90526STheodore Ts'o ext4_itable_unused_set(sb, gdp, 130793f90526STheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 1308083f5b24SYongqiang Yang gdp->bg_flags = cpu_to_le16(*bg_flags); 1309feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1310083f5b24SYongqiang Yang 1311083f5b24SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 1312083f5b24SYongqiang Yang if (unlikely(err)) { 1313083f5b24SYongqiang Yang ext4_std_error(sb, err); 1314083f5b24SYongqiang Yang break; 1315083f5b24SYongqiang Yang } 1316083f5b24SYongqiang Yang 1317083f5b24SYongqiang Yang /* 1318083f5b24SYongqiang Yang * We can allocate memory for mb_alloc based on the new group 1319083f5b24SYongqiang Yang * descriptor 1320083f5b24SYongqiang Yang */ 1321083f5b24SYongqiang Yang err = ext4_mb_add_groupinfo(sb, group, gdp); 1322083f5b24SYongqiang Yang if (err) 1323083f5b24SYongqiang Yang break; 1324083f5b24SYongqiang Yang } 1325083f5b24SYongqiang Yang return err; 1326083f5b24SYongqiang Yang } 1327083f5b24SYongqiang Yang 13282e10e2f2SYongqiang Yang /* 13292e10e2f2SYongqiang Yang * ext4_update_super() updates the super block so that the newly added 13302e10e2f2SYongqiang Yang * groups can be seen by the filesystem. 13312e10e2f2SYongqiang Yang * 13322e10e2f2SYongqiang Yang * @sb: super block 13332e10e2f2SYongqiang Yang * @flex_gd: new added groups 13342e10e2f2SYongqiang Yang */ 13352e10e2f2SYongqiang Yang static void ext4_update_super(struct super_block *sb, 13362e10e2f2SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 13372e10e2f2SYongqiang Yang { 13382e10e2f2SYongqiang Yang ext4_fsblk_t blocks_count = 0; 13392e10e2f2SYongqiang Yang ext4_fsblk_t free_blocks = 0; 13402e10e2f2SYongqiang Yang ext4_fsblk_t reserved_blocks = 0; 13412e10e2f2SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 13422e10e2f2SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 13432e10e2f2SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 13448a991849STheodore Ts'o int i; 13452e10e2f2SYongqiang Yang 13462e10e2f2SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 13472e10e2f2SYongqiang Yang /* 13482e10e2f2SYongqiang Yang * Make the new blocks and inodes valid next. We do this before 13492e10e2f2SYongqiang Yang * increasing the group count so that once the group is enabled, 13502e10e2f2SYongqiang Yang * all of its blocks and inodes are already valid. 13512e10e2f2SYongqiang Yang * 13522e10e2f2SYongqiang Yang * We always allocate group-by-group, then block-by-block or 13532e10e2f2SYongqiang Yang * inode-by-inode within a group, so enabling these 13542e10e2f2SYongqiang Yang * blocks/inodes before the group is live won't actually let us 13552e10e2f2SYongqiang Yang * allocate the new space yet. 13562e10e2f2SYongqiang Yang */ 13572e10e2f2SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 13582e10e2f2SYongqiang Yang blocks_count += group_data[i].blocks_count; 1359d77147ffSharshads free_blocks += EXT4_C2B(sbi, group_data[i].free_clusters_count); 13602e10e2f2SYongqiang Yang } 13612e10e2f2SYongqiang Yang 13622e10e2f2SYongqiang Yang reserved_blocks = ext4_r_blocks_count(es) * 100; 136301f795f9SYongqiang Yang reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es)); 13642e10e2f2SYongqiang Yang reserved_blocks *= blocks_count; 13652e10e2f2SYongqiang Yang do_div(reserved_blocks, 100); 13662e10e2f2SYongqiang Yang 13672e10e2f2SYongqiang Yang ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count); 1368636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks); 13692e10e2f2SYongqiang Yang le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) * 13702e10e2f2SYongqiang Yang flex_gd->count); 1371636d7e2eSDarrick J. Wong le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) * 1372636d7e2eSDarrick J. Wong flex_gd->count); 13732e10e2f2SYongqiang Yang 137401f795f9SYongqiang Yang ext4_debug("free blocks count %llu", ext4_free_blocks_count(es)); 13752e10e2f2SYongqiang Yang /* 13762e10e2f2SYongqiang Yang * We need to protect s_groups_count against other CPUs seeing 13772e10e2f2SYongqiang Yang * inconsistent state in the superblock. 13782e10e2f2SYongqiang Yang * 13792e10e2f2SYongqiang Yang * The precise rules we use are: 13802e10e2f2SYongqiang Yang * 13812e10e2f2SYongqiang Yang * * Writers must perform a smp_wmb() after updating all 13822e10e2f2SYongqiang Yang * dependent data and before modifying the groups count 13832e10e2f2SYongqiang Yang * 13842e10e2f2SYongqiang Yang * * Readers must perform an smp_rmb() after reading the groups 13852e10e2f2SYongqiang Yang * count and before reading any dependent data. 13862e10e2f2SYongqiang Yang * 13872e10e2f2SYongqiang Yang * NB. These rules can be relaxed when checking the group count 13882e10e2f2SYongqiang Yang * while freeing data, as we can only allocate from a block 13892e10e2f2SYongqiang Yang * group after serialising against the group count, and we can 13902e10e2f2SYongqiang Yang * only then free after serialising in turn against that 13912e10e2f2SYongqiang Yang * allocation. 13922e10e2f2SYongqiang Yang */ 13932e10e2f2SYongqiang Yang smp_wmb(); 13942e10e2f2SYongqiang Yang 13952e10e2f2SYongqiang Yang /* Update the global fs size fields */ 13962e10e2f2SYongqiang Yang sbi->s_groups_count += flex_gd->count; 1397c5c72d81STheodore Ts'o sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 1398c5c72d81STheodore Ts'o (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 13992e10e2f2SYongqiang Yang 14002e10e2f2SYongqiang Yang /* Update the reserved block counts only once the new group is 14012e10e2f2SYongqiang Yang * active. */ 14022e10e2f2SYongqiang Yang ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 14032e10e2f2SYongqiang Yang reserved_blocks); 14042e10e2f2SYongqiang Yang 14052e10e2f2SYongqiang Yang /* Update the free space counts */ 14062e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeclusters_counter, 1407810da240SLukas Czerner EXT4_NUM_B2C(sbi, free_blocks)); 14082e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeinodes_counter, 14092e10e2f2SYongqiang Yang EXT4_INODES_PER_GROUP(sb) * flex_gd->count); 14102e10e2f2SYongqiang Yang 141101f795f9SYongqiang Yang ext4_debug("free blocks count %llu", 141201f795f9SYongqiang Yang percpu_counter_read(&sbi->s_freeclusters_counter)); 1413e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) { 14142e10e2f2SYongqiang Yang ext4_group_t flex_group; 14152e10e2f2SYongqiang Yang flex_group = ext4_flex_group(sbi, group_data[0].group); 141690ba983fSTheodore Ts'o atomic64_add(EXT4_NUM_B2C(sbi, free_blocks), 14172e10e2f2SYongqiang Yang &sbi->s_flex_groups[flex_group].free_clusters); 14182e10e2f2SYongqiang Yang atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count, 14192e10e2f2SYongqiang Yang &sbi->s_flex_groups[flex_group].free_inodes); 14202e10e2f2SYongqiang Yang } 14212e10e2f2SYongqiang Yang 1422952fc18eSTheodore Ts'o /* 1423952fc18eSTheodore Ts'o * Update the fs overhead information 1424952fc18eSTheodore Ts'o */ 1425952fc18eSTheodore Ts'o ext4_calculate_overhead(sb); 1426952fc18eSTheodore Ts'o 14272e10e2f2SYongqiang Yang if (test_opt(sb, DEBUG)) 14282e10e2f2SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: added group %u:" 14292e10e2f2SYongqiang Yang "%llu blocks(%llu free %llu reserved)\n", flex_gd->count, 14302e10e2f2SYongqiang Yang blocks_count, free_blocks, reserved_blocks); 14312e10e2f2SYongqiang Yang } 14322e10e2f2SYongqiang Yang 14334bac1f8cSYongqiang Yang /* Add a flex group to an fs. Ensure we handle all possible error conditions 14344bac1f8cSYongqiang Yang * _before_ we start modifying the filesystem, because we cannot abort the 14354bac1f8cSYongqiang Yang * transaction and not have it write the data to disk. 14364bac1f8cSYongqiang Yang */ 14374bac1f8cSYongqiang Yang static int ext4_flex_group_add(struct super_block *sb, 14384bac1f8cSYongqiang Yang struct inode *resize_inode, 14394bac1f8cSYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 14404bac1f8cSYongqiang Yang { 14414bac1f8cSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 14424bac1f8cSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 14434bac1f8cSYongqiang Yang ext4_fsblk_t o_blocks_count; 14444bac1f8cSYongqiang Yang ext4_grpblk_t last; 14454bac1f8cSYongqiang Yang ext4_group_t group; 14464bac1f8cSYongqiang Yang handle_t *handle; 14474bac1f8cSYongqiang Yang unsigned reserved_gdb; 14484bac1f8cSYongqiang Yang int err = 0, err2 = 0, credit; 14494bac1f8cSYongqiang Yang 14504bac1f8cSYongqiang Yang BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags); 14514bac1f8cSYongqiang Yang 14524bac1f8cSYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 14534bac1f8cSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 14544bac1f8cSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 14554bac1f8cSYongqiang Yang BUG_ON(last); 14564bac1f8cSYongqiang Yang 14574bac1f8cSYongqiang Yang err = setup_new_flex_group_blocks(sb, flex_gd); 14584bac1f8cSYongqiang Yang if (err) 14594bac1f8cSYongqiang Yang goto exit; 14604bac1f8cSYongqiang Yang /* 14614bac1f8cSYongqiang Yang * We will always be modifying at least the superblock and GDT 14622c869b26SJan Kara * blocks. If we are adding a group past the last current GDT block, 14634bac1f8cSYongqiang Yang * we will also modify the inode and the dindirect block. If we 14644bac1f8cSYongqiang Yang * are adding a group with superblock/GDT backups we will also 14654bac1f8cSYongqiang Yang * modify each of the reserved GDT dindirect blocks. 14664bac1f8cSYongqiang Yang */ 14672c869b26SJan Kara credit = 3; /* sb, resize inode, resize inode dindirect */ 14682c869b26SJan Kara /* GDT blocks */ 14692c869b26SJan Kara credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb)); 14702c869b26SJan Kara credit += reserved_gdb; /* Reserved GDT dindirect blocks */ 14719924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit); 14724bac1f8cSYongqiang Yang if (IS_ERR(handle)) { 14734bac1f8cSYongqiang Yang err = PTR_ERR(handle); 14744bac1f8cSYongqiang Yang goto exit; 14754bac1f8cSYongqiang Yang } 14764bac1f8cSYongqiang Yang 14775d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 14784bac1f8cSYongqiang Yang err = ext4_journal_get_write_access(handle, sbi->s_sbh); 14794bac1f8cSYongqiang Yang if (err) 14804bac1f8cSYongqiang Yang goto exit_journal; 14814bac1f8cSYongqiang Yang 14824bac1f8cSYongqiang Yang group = flex_gd->groups[0].group; 148349598e04SJun Piao BUG_ON(group != sbi->s_groups_count); 14844bac1f8cSYongqiang Yang err = ext4_add_new_descs(handle, sb, group, 14854bac1f8cSYongqiang Yang resize_inode, flex_gd->count); 14864bac1f8cSYongqiang Yang if (err) 14874bac1f8cSYongqiang Yang goto exit_journal; 14884bac1f8cSYongqiang Yang 14894bac1f8cSYongqiang Yang err = ext4_setup_new_descs(handle, sb, flex_gd); 14904bac1f8cSYongqiang Yang if (err) 14914bac1f8cSYongqiang Yang goto exit_journal; 14924bac1f8cSYongqiang Yang 14934bac1f8cSYongqiang Yang ext4_update_super(sb, flex_gd); 14944bac1f8cSYongqiang Yang 14954bac1f8cSYongqiang Yang err = ext4_handle_dirty_super(handle, sb); 14964bac1f8cSYongqiang Yang 14974bac1f8cSYongqiang Yang exit_journal: 14984bac1f8cSYongqiang Yang err2 = ext4_journal_stop(handle); 14994bac1f8cSYongqiang Yang if (!err) 15004bac1f8cSYongqiang Yang err = err2; 15014bac1f8cSYongqiang Yang 15024bac1f8cSYongqiang Yang if (!err) { 15032ebd1704SYongqiang Yang int gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 15042ebd1704SYongqiang Yang int gdb_num_end = ((group + flex_gd->count - 1) / 15052ebd1704SYongqiang Yang EXT4_DESC_PER_BLOCK(sb)); 1506e2b911c5SDarrick J. Wong int meta_bg = ext4_has_feature_meta_bg(sb); 15070acdb887STao Ma sector_t old_gdb = 0; 15082ebd1704SYongqiang Yang 15094bac1f8cSYongqiang Yang update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 151001f795f9SYongqiang Yang sizeof(struct ext4_super_block), 0); 15112ebd1704SYongqiang Yang for (; gdb_num <= gdb_num_end; gdb_num++) { 15124bac1f8cSYongqiang Yang struct buffer_head *gdb_bh; 15132ebd1704SYongqiang Yang 15144bac1f8cSYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 15150acdb887STao Ma if (old_gdb == gdb_bh->b_blocknr) 15160acdb887STao Ma continue; 15174bac1f8cSYongqiang Yang update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data, 151801f795f9SYongqiang Yang gdb_bh->b_size, meta_bg); 15190acdb887STao Ma old_gdb = gdb_bh->b_blocknr; 15204bac1f8cSYongqiang Yang } 15214bac1f8cSYongqiang Yang } 15224bac1f8cSYongqiang Yang exit: 15234bac1f8cSYongqiang Yang return err; 15244bac1f8cSYongqiang Yang } 15254bac1f8cSYongqiang Yang 152619c5246dSYongqiang Yang static int ext4_setup_next_flex_gd(struct super_block *sb, 152719c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 152819c5246dSYongqiang Yang ext4_fsblk_t n_blocks_count, 152919c5246dSYongqiang Yang unsigned long flexbg_size) 153019c5246dSYongqiang Yang { 1531d77147ffSharshads struct ext4_sb_info *sbi = EXT4_SB(sb); 1532d77147ffSharshads struct ext4_super_block *es = sbi->s_es; 153319c5246dSYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 153419c5246dSYongqiang Yang ext4_fsblk_t o_blocks_count; 153519c5246dSYongqiang Yang ext4_group_t n_group; 153619c5246dSYongqiang Yang ext4_group_t group; 153719c5246dSYongqiang Yang ext4_group_t last_group; 153819c5246dSYongqiang Yang ext4_grpblk_t last; 1539d77147ffSharshads ext4_grpblk_t clusters_per_group; 154019c5246dSYongqiang Yang unsigned long i; 154119c5246dSYongqiang Yang 1542d77147ffSharshads clusters_per_group = EXT4_CLUSTERS_PER_GROUP(sb); 154319c5246dSYongqiang Yang 154419c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 154519c5246dSYongqiang Yang 154619c5246dSYongqiang Yang if (o_blocks_count == n_blocks_count) 154719c5246dSYongqiang Yang return 0; 154819c5246dSYongqiang Yang 154919c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 155019c5246dSYongqiang Yang BUG_ON(last); 155119c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last); 155219c5246dSYongqiang Yang 155319c5246dSYongqiang Yang last_group = group | (flexbg_size - 1); 155419c5246dSYongqiang Yang if (last_group > n_group) 155519c5246dSYongqiang Yang last_group = n_group; 155619c5246dSYongqiang Yang 155719c5246dSYongqiang Yang flex_gd->count = last_group - group + 1; 155819c5246dSYongqiang Yang 155919c5246dSYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 156019c5246dSYongqiang Yang int overhead; 156119c5246dSYongqiang Yang 156219c5246dSYongqiang Yang group_data[i].group = group + i; 1563d77147ffSharshads group_data[i].blocks_count = EXT4_BLOCKS_PER_GROUP(sb); 156401f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group + i); 1565d77147ffSharshads group_data[i].mdata_blocks = overhead; 1566d77147ffSharshads group_data[i].free_clusters_count = EXT4_CLUSTERS_PER_GROUP(sb); 15677f511862STheodore Ts'o if (ext4_has_group_desc_csum(sb)) { 156819c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT | 156919c5246dSYongqiang Yang EXT4_BG_INODE_UNINIT; 15707f511862STheodore Ts'o if (!test_opt(sb, INIT_INODE_TABLE)) 15717f511862STheodore Ts'o flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED; 15727f511862STheodore Ts'o } else 157319c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED; 157419c5246dSYongqiang Yang } 157519c5246dSYongqiang Yang 1576feb0ab32SDarrick J. Wong if (last_group == n_group && ext4_has_group_desc_csum(sb)) 157719c5246dSYongqiang Yang /* We need to initialize block bitmap of last group. */ 157819c5246dSYongqiang Yang flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT; 157919c5246dSYongqiang Yang 1580d77147ffSharshads if ((last_group == n_group) && (last != clusters_per_group - 1)) { 1581d77147ffSharshads group_data[i - 1].blocks_count = EXT4_C2B(sbi, last + 1); 1582d77147ffSharshads group_data[i - 1].free_clusters_count -= clusters_per_group - 158319c5246dSYongqiang Yang last - 1; 158419c5246dSYongqiang Yang } 158519c5246dSYongqiang Yang 158619c5246dSYongqiang Yang return 1; 158719c5246dSYongqiang Yang } 158819c5246dSYongqiang Yang 1589ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 1590ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 1591ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 1592ac27a0ecSDave Kleikamp * write the data to disk. 1593ac27a0ecSDave Kleikamp * 1594ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 1595ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 1596ac27a0ecSDave Kleikamp * 1597ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 1598ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 1599ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 1600ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 1601ac27a0ecSDave Kleikamp */ 1602617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 1603ac27a0ecSDave Kleikamp { 160461f296ccSYongqiang Yang struct ext4_new_flex_group_data flex_gd; 1605617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1606617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 1607617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 1608ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1609ac27a0ecSDave Kleikamp struct inode *inode = NULL; 161003b40e34SJon Ernst int gdb_off; 161161f296ccSYongqiang Yang int err; 161261f296ccSYongqiang Yang __u16 bg_flags = 0; 1613ac27a0ecSDave Kleikamp 1614617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 1615ac27a0ecSDave Kleikamp 1616e2b911c5SDarrick J. Wong if (gdb_off == 0 && !ext4_has_feature_sparse_super(sb)) { 161712062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 1618ac27a0ecSDave Kleikamp return -EPERM; 1619ac27a0ecSDave Kleikamp } 1620ac27a0ecSDave Kleikamp 1621bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 1622bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 162312062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1624ac27a0ecSDave Kleikamp return -EINVAL; 1625ac27a0ecSDave Kleikamp } 1626ac27a0ecSDave Kleikamp 1627617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 1628ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 162912062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 1630ac27a0ecSDave Kleikamp return -EINVAL; 1631ac27a0ecSDave Kleikamp } 1632ac27a0ecSDave Kleikamp 1633ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 1634*e647e291Sruippan (潘睿) if (!ext4_has_feature_resize_inode(sb) || 1635e2b911c5SDarrick J. Wong !le16_to_cpu(es->s_reserved_gdt_blocks)) { 163612062dddSEric Sandeen ext4_warning(sb, 1637ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 1638ac27a0ecSDave Kleikamp return -EPERM; 1639ac27a0ecSDave Kleikamp } 16401d1fe1eeSDavid Howells inode = ext4_iget(sb, EXT4_RESIZE_INO); 16411d1fe1eeSDavid Howells if (IS_ERR(inode)) { 164212062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 16431d1fe1eeSDavid Howells return PTR_ERR(inode); 1644ac27a0ecSDave Kleikamp } 1645ac27a0ecSDave Kleikamp } 1646ac27a0ecSDave Kleikamp 1647920313a7SAneesh Kumar K.V 164861f296ccSYongqiang Yang err = verify_group_input(sb, input); 1649668f4dc5SYongqiang Yang if (err) 165061f296ccSYongqiang Yang goto out; 1651ac27a0ecSDave Kleikamp 1652117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, input->group + 1); 1653117fff10STheodore Ts'o if (err) 16547f511862STheodore Ts'o goto out; 1655117fff10STheodore Ts'o 165628623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, input->group + 1); 165728623c2fSTheodore Ts'o if (err) 165828623c2fSTheodore Ts'o goto out; 165928623c2fSTheodore Ts'o 166061f296ccSYongqiang Yang flex_gd.count = 1; 166161f296ccSYongqiang Yang flex_gd.groups = input; 166261f296ccSYongqiang Yang flex_gd.bg_flags = &bg_flags; 166361f296ccSYongqiang Yang err = ext4_flex_group_add(sb, inode, &flex_gd); 166461f296ccSYongqiang Yang out: 1665ac27a0ecSDave Kleikamp iput(inode); 1666ac27a0ecSDave Kleikamp return err; 1667617ba13bSMingming Cao } /* ext4_group_add */ 1668ac27a0ecSDave Kleikamp 16692b2d6d01STheodore Ts'o /* 167018e31438SYongqiang Yang * extend a group without checking assuming that checking has been done. 167118e31438SYongqiang Yang */ 167218e31438SYongqiang Yang static int ext4_group_extend_no_check(struct super_block *sb, 167318e31438SYongqiang Yang ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) 167418e31438SYongqiang Yang { 167518e31438SYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 167618e31438SYongqiang Yang handle_t *handle; 167718e31438SYongqiang Yang int err = 0, err2; 167818e31438SYongqiang Yang 167918e31438SYongqiang Yang /* We will update the superblock, one block bitmap, and 168018e31438SYongqiang Yang * one group descriptor via ext4_group_add_blocks(). 168118e31438SYongqiang Yang */ 16829924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3); 168318e31438SYongqiang Yang if (IS_ERR(handle)) { 168418e31438SYongqiang Yang err = PTR_ERR(handle); 168518e31438SYongqiang Yang ext4_warning(sb, "error %d on journal start", err); 168618e31438SYongqiang Yang return err; 168718e31438SYongqiang Yang } 168818e31438SYongqiang Yang 16895d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 169018e31438SYongqiang Yang err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 169118e31438SYongqiang Yang if (err) { 169218e31438SYongqiang Yang ext4_warning(sb, "error %d on journal write access", err); 169318e31438SYongqiang Yang goto errout; 169418e31438SYongqiang Yang } 169518e31438SYongqiang Yang 169618e31438SYongqiang Yang ext4_blocks_count_set(es, o_blocks_count + add); 1697636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add); 169818e31438SYongqiang Yang ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 169918e31438SYongqiang Yang o_blocks_count + add); 170018e31438SYongqiang Yang /* We add the blocks to the bitmap and set the group need init bit */ 170118e31438SYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 170218e31438SYongqiang Yang if (err) 170318e31438SYongqiang Yang goto errout; 170418e31438SYongqiang Yang ext4_handle_dirty_super(handle, sb); 170518e31438SYongqiang Yang ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 170618e31438SYongqiang Yang o_blocks_count + add); 170718e31438SYongqiang Yang errout: 170818e31438SYongqiang Yang err2 = ext4_journal_stop(handle); 170918e31438SYongqiang Yang if (err2 && !err) 171018e31438SYongqiang Yang err = err2; 171118e31438SYongqiang Yang 171218e31438SYongqiang Yang if (!err) { 171318e31438SYongqiang Yang if (test_opt(sb, DEBUG)) 171418e31438SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extended group to %llu " 171518e31438SYongqiang Yang "blocks\n", ext4_blocks_count(es)); 17166ca792edSMaarten ter Huurne update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, 171701f795f9SYongqiang Yang (char *)es, sizeof(struct ext4_super_block), 0); 171818e31438SYongqiang Yang } 171918e31438SYongqiang Yang return err; 172018e31438SYongqiang Yang } 172118e31438SYongqiang Yang 172218e31438SYongqiang Yang /* 17232b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 1724ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 1725ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 1726ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 1727ac27a0ecSDave Kleikamp * 1728617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 1729ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 1730ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 1731ac27a0ecSDave Kleikamp */ 1732617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 1733617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 1734ac27a0ecSDave Kleikamp { 1735617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 1736617ba13bSMingming Cao ext4_grpblk_t last; 1737617ba13bSMingming Cao ext4_grpblk_t add; 1738ac27a0ecSDave Kleikamp struct buffer_head *bh; 1739d89651c8SYongqiang Yang int err; 17405f21b0e6SFrederic Bohe ext4_group_t group; 1741ac27a0ecSDave Kleikamp 1742bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 1743ac27a0ecSDave Kleikamp 1744ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 174592b97816STheodore Ts'o ext4_msg(sb, KERN_DEBUG, 174692b97816STheodore Ts'o "extending last group from %llu to %llu blocks", 1747ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 1748ac27a0ecSDave Kleikamp 1749ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 1750ac27a0ecSDave Kleikamp return 0; 1751ac27a0ecSDave Kleikamp 1752ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 175392b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, 175492b97816STheodore Ts'o "filesystem too large to resize to %llu blocks safely", 175592b97816STheodore Ts'o n_blocks_count); 1756ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 175712062dddSEric Sandeen ext4_warning(sb, "CONFIG_LBDAF not enabled"); 1758ac27a0ecSDave Kleikamp return -EINVAL; 1759ac27a0ecSDave Kleikamp } 1760ac27a0ecSDave Kleikamp 1761ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 176212062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 17638f82f840SYongqiang Yang return -EINVAL; 1764ac27a0ecSDave Kleikamp } 1765ac27a0ecSDave Kleikamp 1766ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 17675f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1768ac27a0ecSDave Kleikamp 1769ac27a0ecSDave Kleikamp if (last == 0) { 177012062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1771ac27a0ecSDave Kleikamp return -EPERM; 1772ac27a0ecSDave Kleikamp } 1773ac27a0ecSDave Kleikamp 1774617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1775ac27a0ecSDave Kleikamp 1776ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 177712062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1778ac27a0ecSDave Kleikamp return -EINVAL; 1779ac27a0ecSDave Kleikamp } 1780ac27a0ecSDave Kleikamp 1781ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1782ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1783ac27a0ecSDave Kleikamp 1784ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 178512062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1786ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1787ac27a0ecSDave Kleikamp 1788ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 1789ac27a0ecSDave Kleikamp bh = sb_bread(sb, o_blocks_count + add - 1); 1790ac27a0ecSDave Kleikamp if (!bh) { 179112062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1792ac27a0ecSDave Kleikamp return -ENOSPC; 1793ac27a0ecSDave Kleikamp } 1794ac27a0ecSDave Kleikamp brelse(bh); 1795ac27a0ecSDave Kleikamp 1796d89651c8SYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 1797ac27a0ecSDave Kleikamp return err; 1798617ba13bSMingming Cao } /* ext4_group_extend */ 179919c5246dSYongqiang Yang 18001c6bd717STheodore Ts'o 18011c6bd717STheodore Ts'o static int num_desc_blocks(struct super_block *sb, ext4_group_t groups) 18021c6bd717STheodore Ts'o { 18031c6bd717STheodore Ts'o return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb); 18041c6bd717STheodore Ts'o } 18051c6bd717STheodore Ts'o 18061c6bd717STheodore Ts'o /* 18071c6bd717STheodore Ts'o * Release the resize inode and drop the resize_inode feature if there 18081c6bd717STheodore Ts'o * are no more reserved gdt blocks, and then convert the file system 18091c6bd717STheodore Ts'o * to enable meta_bg 18101c6bd717STheodore Ts'o */ 18111c6bd717STheodore Ts'o static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode) 18121c6bd717STheodore Ts'o { 18131c6bd717STheodore Ts'o handle_t *handle; 18141c6bd717STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 18151c6bd717STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 181659e31c15STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 18171c6bd717STheodore Ts'o ext4_fsblk_t nr; 18181c6bd717STheodore Ts'o int i, ret, err = 0; 18191c6bd717STheodore Ts'o int credits = 1; 18201c6bd717STheodore Ts'o 18211c6bd717STheodore Ts'o ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg"); 182259e31c15STheodore Ts'o if (inode) { 18231c6bd717STheodore Ts'o if (es->s_reserved_gdt_blocks) { 18241c6bd717STheodore Ts'o ext4_error(sb, "Unexpected non-zero " 18251c6bd717STheodore Ts'o "s_reserved_gdt_blocks"); 18261c6bd717STheodore Ts'o return -EPERM; 18271c6bd717STheodore Ts'o } 18281c6bd717STheodore Ts'o 18291c6bd717STheodore Ts'o /* Do a quick sanity check of the resize inode */ 1830d77147ffSharshads if (inode->i_blocks != 1 << (inode->i_blkbits - 1831d77147ffSharshads (9 - sbi->s_cluster_bits))) 18321c6bd717STheodore Ts'o goto invalid_resize_inode; 18331c6bd717STheodore Ts'o for (i = 0; i < EXT4_N_BLOCKS; i++) { 18341c6bd717STheodore Ts'o if (i == EXT4_DIND_BLOCK) { 18351c6bd717STheodore Ts'o if (ei->i_data[i]) 18361c6bd717STheodore Ts'o continue; 18371c6bd717STheodore Ts'o else 18381c6bd717STheodore Ts'o goto invalid_resize_inode; 18391c6bd717STheodore Ts'o } 18401c6bd717STheodore Ts'o if (ei->i_data[i]) 18411c6bd717STheodore Ts'o goto invalid_resize_inode; 18421c6bd717STheodore Ts'o } 18431c6bd717STheodore Ts'o credits += 3; /* block bitmap, bg descriptor, resize inode */ 18441c6bd717STheodore Ts'o } 18451c6bd717STheodore Ts'o 18469924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits); 18471c6bd717STheodore Ts'o if (IS_ERR(handle)) 18481c6bd717STheodore Ts'o return PTR_ERR(handle); 18491c6bd717STheodore Ts'o 18505d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 18511c6bd717STheodore Ts'o err = ext4_journal_get_write_access(handle, sbi->s_sbh); 18521c6bd717STheodore Ts'o if (err) 18531c6bd717STheodore Ts'o goto errout; 18541c6bd717STheodore Ts'o 1855e2b911c5SDarrick J. Wong ext4_clear_feature_resize_inode(sb); 1856e2b911c5SDarrick J. Wong ext4_set_feature_meta_bg(sb); 18571c6bd717STheodore Ts'o sbi->s_es->s_first_meta_bg = 18581c6bd717STheodore Ts'o cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count)); 18591c6bd717STheodore Ts'o 18601c6bd717STheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 18611c6bd717STheodore Ts'o if (err) { 18621c6bd717STheodore Ts'o ext4_std_error(sb, err); 18631c6bd717STheodore Ts'o goto errout; 18641c6bd717STheodore Ts'o } 18651c6bd717STheodore Ts'o 18661c6bd717STheodore Ts'o if (inode) { 18671c6bd717STheodore Ts'o nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]); 18681c6bd717STheodore Ts'o ext4_free_blocks(handle, inode, NULL, nr, 1, 18691c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | 18701c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 18711c6bd717STheodore Ts'o ei->i_data[EXT4_DIND_BLOCK] = 0; 18721c6bd717STheodore Ts'o inode->i_blocks = 0; 18731c6bd717STheodore Ts'o 18741c6bd717STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 18751c6bd717STheodore Ts'o if (err) 18761c6bd717STheodore Ts'o ext4_std_error(sb, err); 18771c6bd717STheodore Ts'o } 18781c6bd717STheodore Ts'o 18791c6bd717STheodore Ts'o errout: 18801c6bd717STheodore Ts'o ret = ext4_journal_stop(handle); 18811c6bd717STheodore Ts'o if (!err) 18821c6bd717STheodore Ts'o err = ret; 18831c6bd717STheodore Ts'o return ret; 18841c6bd717STheodore Ts'o 18851c6bd717STheodore Ts'o invalid_resize_inode: 18861c6bd717STheodore Ts'o ext4_error(sb, "corrupted/inconsistent resize inode"); 18871c6bd717STheodore Ts'o return -EINVAL; 18881c6bd717STheodore Ts'o } 18891c6bd717STheodore Ts'o 189019c5246dSYongqiang Yang /* 189119c5246dSYongqiang Yang * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count 189219c5246dSYongqiang Yang * 189319c5246dSYongqiang Yang * @sb: super block of the fs to be resized 189419c5246dSYongqiang Yang * @n_blocks_count: the number of blocks resides in the resized fs 189519c5246dSYongqiang Yang */ 189619c5246dSYongqiang Yang int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count) 189719c5246dSYongqiang Yang { 189819c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd = NULL; 189919c5246dSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 190019c5246dSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 190119c5246dSYongqiang Yang struct buffer_head *bh; 190201f795f9SYongqiang Yang struct inode *resize_inode = NULL; 190301f795f9SYongqiang Yang ext4_grpblk_t add, offset; 190419c5246dSYongqiang Yang unsigned long n_desc_blocks; 190519c5246dSYongqiang Yang unsigned long o_desc_blocks; 190601f795f9SYongqiang Yang ext4_group_t o_group; 190701f795f9SYongqiang Yang ext4_group_t n_group; 190801f795f9SYongqiang Yang ext4_fsblk_t o_blocks_count; 19091c6bd717STheodore Ts'o ext4_fsblk_t n_blocks_count_retry = 0; 19104da4a56eSTheodore Ts'o unsigned long last_update_time = 0; 1911117fff10STheodore Ts'o int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex; 191201f795f9SYongqiang Yang int meta_bg; 191319c5246dSYongqiang Yang 191459e31c15STheodore Ts'o /* See if the device is actually as big as what was requested */ 191559e31c15STheodore Ts'o bh = sb_bread(sb, n_blocks_count - 1); 191659e31c15STheodore Ts'o if (!bh) { 191759e31c15STheodore Ts'o ext4_warning(sb, "can't read last block, resize aborted"); 191859e31c15STheodore Ts'o return -ENOSPC; 191959e31c15STheodore Ts'o } 192059e31c15STheodore Ts'o brelse(bh); 192159e31c15STheodore Ts'o 19221c6bd717STheodore Ts'o retry: 192319c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 192419c5246dSYongqiang Yang 192559e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu " 192692b97816STheodore Ts'o "to %llu blocks", o_blocks_count, n_blocks_count); 192719c5246dSYongqiang Yang 192819c5246dSYongqiang Yang if (n_blocks_count < o_blocks_count) { 192919c5246dSYongqiang Yang /* On-line shrinking not supported */ 193019c5246dSYongqiang Yang ext4_warning(sb, "can't shrink FS - resize aborted"); 193119c5246dSYongqiang Yang return -EINVAL; 193219c5246dSYongqiang Yang } 193319c5246dSYongqiang Yang 193419c5246dSYongqiang Yang if (n_blocks_count == o_blocks_count) 193519c5246dSYongqiang Yang /* Nothing need to do */ 193619c5246dSYongqiang Yang return 0; 193719c5246dSYongqiang Yang 1938bd86298eSLukas Czerner n_group = ext4_get_group_number(sb, n_blocks_count - 1); 19394f2f76f7SJan Kara if (n_group >= (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) { 19403f8a6411STheodore Ts'o ext4_warning(sb, "resize would cause inodes_count overflow"); 19413f8a6411STheodore Ts'o return -EINVAL; 19423f8a6411STheodore Ts'o } 1943a0ade1deSLukas Czerner ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset); 194419c5246dSYongqiang Yang 19451c6bd717STheodore Ts'o n_desc_blocks = num_desc_blocks(sb, n_group + 1); 19461c6bd717STheodore Ts'o o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count); 194719c5246dSYongqiang Yang 1948e2b911c5SDarrick J. Wong meta_bg = ext4_has_feature_meta_bg(sb); 194901f795f9SYongqiang Yang 1950e2b911c5SDarrick J. Wong if (ext4_has_feature_resize_inode(sb)) { 195101f795f9SYongqiang Yang if (meta_bg) { 195201f795f9SYongqiang Yang ext4_error(sb, "resize_inode and meta_bg enabled " 195301f795f9SYongqiang Yang "simultaneously"); 195401f795f9SYongqiang Yang return -EINVAL; 195501f795f9SYongqiang Yang } 19561c6bd717STheodore Ts'o if (n_desc_blocks > o_desc_blocks + 19571c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks)) { 19581c6bd717STheodore Ts'o n_blocks_count_retry = n_blocks_count; 19591c6bd717STheodore Ts'o n_desc_blocks = o_desc_blocks + 19601c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks); 19611c6bd717STheodore Ts'o n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb); 1962aec51758SJerry Lee n_blocks_count = (ext4_fsblk_t)n_group * 1963aec51758SJerry Lee EXT4_BLOCKS_PER_GROUP(sb); 19641c6bd717STheodore Ts'o n_group--; /* set to last group number */ 196519c5246dSYongqiang Yang } 19661c6bd717STheodore Ts'o 19671c6bd717STheodore Ts'o if (!resize_inode) 196819c5246dSYongqiang Yang resize_inode = ext4_iget(sb, EXT4_RESIZE_INO); 196919c5246dSYongqiang Yang if (IS_ERR(resize_inode)) { 197019c5246dSYongqiang Yang ext4_warning(sb, "Error opening resize inode"); 197119c5246dSYongqiang Yang return PTR_ERR(resize_inode); 197219c5246dSYongqiang Yang } 19731c6bd717STheodore Ts'o } 19741c6bd717STheodore Ts'o 197559e31c15STheodore Ts'o if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) { 19761c6bd717STheodore Ts'o err = ext4_convert_meta_bg(sb, resize_inode); 19771c6bd717STheodore Ts'o if (err) 19781c6bd717STheodore Ts'o goto out; 19791c6bd717STheodore Ts'o if (resize_inode) { 19801c6bd717STheodore Ts'o iput(resize_inode); 19811c6bd717STheodore Ts'o resize_inode = NULL; 19821c6bd717STheodore Ts'o } 19831c6bd717STheodore Ts'o if (n_blocks_count_retry) { 19841c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 19851c6bd717STheodore Ts'o n_blocks_count_retry = 0; 19861c6bd717STheodore Ts'o goto retry; 19871c6bd717STheodore Ts'o } 198801f795f9SYongqiang Yang } 198919c5246dSYongqiang Yang 1990f0a459deSTheodore Ts'o /* 1991f0a459deSTheodore Ts'o * Make sure the last group has enough space so that it's 1992f0a459deSTheodore Ts'o * guaranteed to have enough space for all metadata blocks 1993f0a459deSTheodore Ts'o * that it might need to hold. (We might not need to store 1994f0a459deSTheodore Ts'o * the inode table blocks in the last block group, but there 1995f0a459deSTheodore Ts'o * will be cases where this might be needed.) 1996f0a459deSTheodore Ts'o */ 1997f0a459deSTheodore Ts'o if ((ext4_group_first_block_no(sb, n_group) + 1998f0a459deSTheodore Ts'o ext4_group_overhead_blocks(sb, n_group) + 2 + 1999f0a459deSTheodore Ts'o sbi->s_itb_per_group + sbi->s_cluster_ratio) >= n_blocks_count) { 2000f0a459deSTheodore Ts'o n_blocks_count = ext4_group_first_block_no(sb, n_group); 2001f0a459deSTheodore Ts'o n_group--; 2002f0a459deSTheodore Ts'o n_blocks_count_retry = 0; 2003f0a459deSTheodore Ts'o if (resize_inode) { 2004f0a459deSTheodore Ts'o iput(resize_inode); 2005f0a459deSTheodore Ts'o resize_inode = NULL; 2006f0a459deSTheodore Ts'o } 2007f0a459deSTheodore Ts'o goto retry; 2008f0a459deSTheodore Ts'o } 2009f0a459deSTheodore Ts'o 201019c5246dSYongqiang Yang /* extend the last group */ 2011a0ade1deSLukas Czerner if (n_group == o_group) 2012a0ade1deSLukas Czerner add = n_blocks_count - o_blocks_count; 2013a0ade1deSLukas Czerner else 2014d77147ffSharshads add = EXT4_C2B(sbi, EXT4_CLUSTERS_PER_GROUP(sb) - (offset + 1)); 2015a0ade1deSLukas Czerner if (add > 0) { 201619c5246dSYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 201719c5246dSYongqiang Yang if (err) 201819c5246dSYongqiang Yang goto out; 201919c5246dSYongqiang Yang } 202019c5246dSYongqiang Yang 2021d7574ad0SYongqiang Yang if (ext4_blocks_count(es) == n_blocks_count) 202219c5246dSYongqiang Yang goto out; 202319c5246dSYongqiang Yang 2024117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, n_group + 1); 2025117fff10STheodore Ts'o if (err) 2026f348e224SVasily Averin goto out; 2027117fff10STheodore Ts'o 202828623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, n_group + 1); 202928623c2fSTheodore Ts'o if (err) 203028623c2fSTheodore Ts'o goto out; 203128623c2fSTheodore Ts'o 203219c5246dSYongqiang Yang flex_gd = alloc_flex_gd(flexbg_size); 203319c5246dSYongqiang Yang if (flex_gd == NULL) { 203419c5246dSYongqiang Yang err = -ENOMEM; 203519c5246dSYongqiang Yang goto out; 203619c5246dSYongqiang Yang } 203719c5246dSYongqiang Yang 203819c5246dSYongqiang Yang /* Add flex groups. Note that a regular group is a 203919c5246dSYongqiang Yang * flex group with 1 group. 204019c5246dSYongqiang Yang */ 204119c5246dSYongqiang Yang while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count, 204219c5246dSYongqiang Yang flexbg_size)) { 20434da4a56eSTheodore Ts'o if (jiffies - last_update_time > HZ * 10) { 20444da4a56eSTheodore Ts'o if (last_update_time) 20454da4a56eSTheodore Ts'o ext4_msg(sb, KERN_INFO, 20464da4a56eSTheodore Ts'o "resized to %llu blocks", 20474da4a56eSTheodore Ts'o ext4_blocks_count(es)); 20484da4a56eSTheodore Ts'o last_update_time = jiffies; 20494da4a56eSTheodore Ts'o } 205003c1c290SYongqiang Yang if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0) 205103c1c290SYongqiang Yang break; 205219c5246dSYongqiang Yang err = ext4_flex_group_add(sb, resize_inode, flex_gd); 205319c5246dSYongqiang Yang if (unlikely(err)) 205419c5246dSYongqiang Yang break; 205519c5246dSYongqiang Yang } 205619c5246dSYongqiang Yang 20571c6bd717STheodore Ts'o if (!err && n_blocks_count_retry) { 20581c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 20591c6bd717STheodore Ts'o n_blocks_count_retry = 0; 20601c6bd717STheodore Ts'o free_flex_gd(flex_gd); 20611c6bd717STheodore Ts'o flex_gd = NULL; 2062db6aee62SVasily Averin if (resize_inode) { 2063db6aee62SVasily Averin iput(resize_inode); 2064db6aee62SVasily Averin resize_inode = NULL; 2065db6aee62SVasily Averin } 20661c6bd717STheodore Ts'o goto retry; 20671c6bd717STheodore Ts'o } 20681c6bd717STheodore Ts'o 206919c5246dSYongqiang Yang out: 207019c5246dSYongqiang Yang if (flex_gd) 207119c5246dSYongqiang Yang free_flex_gd(flex_gd); 207201f795f9SYongqiang Yang if (resize_inode != NULL) 207319c5246dSYongqiang Yang iput(resize_inode); 207459e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", n_blocks_count); 207519c5246dSYongqiang Yang return err; 207619c5246dSYongqiang Yang } 2077