1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/resize.c 3ac27a0ecSDave Kleikamp * 4617ba13bSMingming Cao * Support for resizing an ext4 filesystem while it is mounted. 5ac27a0ecSDave Kleikamp * 6ac27a0ecSDave Kleikamp * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com> 7ac27a0ecSDave Kleikamp * 8ac27a0ecSDave Kleikamp * This could probably be made into a module, because it is not often in use. 9ac27a0ecSDave Kleikamp */ 10ac27a0ecSDave Kleikamp 11ac27a0ecSDave Kleikamp 12617ba13bSMingming Cao #define EXT4FS_DEBUG 13ac27a0ecSDave Kleikamp 14ac27a0ecSDave Kleikamp #include <linux/errno.h> 15ac27a0ecSDave Kleikamp #include <linux/slab.h> 16ac27a0ecSDave Kleikamp 173dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 18ac27a0ecSDave Kleikamp 198f82f840SYongqiang Yang int ext4_resize_begin(struct super_block *sb) 208f82f840SYongqiang Yang { 218f82f840SYongqiang Yang int ret = 0; 228f82f840SYongqiang Yang 238f82f840SYongqiang Yang if (!capable(CAP_SYS_RESOURCE)) 248f82f840SYongqiang Yang return -EPERM; 258f82f840SYongqiang Yang 26ce723c31SYongqiang Yang /* 27*011fa994STheodore Ts'o * If we are not using the primary superblock/GDT copy don't resize, 28*011fa994STheodore Ts'o * because the user tools have no way of handling this. Probably a 29*011fa994STheodore Ts'o * bad time to do it anyways. 30*011fa994STheodore Ts'o */ 31*011fa994STheodore Ts'o if (EXT4_SB(sb)->s_sbh->b_blocknr != 32*011fa994STheodore Ts'o le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 33*011fa994STheodore Ts'o ext4_warning(sb, "won't resize using backup superblock at %llu", 34*011fa994STheodore Ts'o (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 35*011fa994STheodore Ts'o return -EPERM; 36*011fa994STheodore Ts'o } 37*011fa994STheodore Ts'o 38*011fa994STheodore Ts'o /* 39ce723c31SYongqiang Yang * We are not allowed to do online-resizing on a filesystem mounted 40ce723c31SYongqiang Yang * with error, because it can destroy the filesystem easily. 41ce723c31SYongqiang Yang */ 42ce723c31SYongqiang Yang if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 43ce723c31SYongqiang Yang ext4_warning(sb, "There are errors in the filesystem, " 44ce723c31SYongqiang Yang "so online resizing is not allowed\n"); 45ce723c31SYongqiang Yang return -EPERM; 46ce723c31SYongqiang Yang } 47ce723c31SYongqiang Yang 488f82f840SYongqiang Yang if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags)) 498f82f840SYongqiang Yang ret = -EBUSY; 508f82f840SYongqiang Yang 518f82f840SYongqiang Yang return ret; 528f82f840SYongqiang Yang } 538f82f840SYongqiang Yang 548f82f840SYongqiang Yang void ext4_resize_end(struct super_block *sb) 558f82f840SYongqiang Yang { 568f82f840SYongqiang Yang clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags); 574e857c58SPeter Zijlstra smp_mb__after_atomic(); 588f82f840SYongqiang Yang } 598f82f840SYongqiang Yang 6001f795f9SYongqiang Yang static ext4_group_t ext4_meta_bg_first_group(struct super_block *sb, 6101f795f9SYongqiang Yang ext4_group_t group) { 6201f795f9SYongqiang Yang return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) << 6301f795f9SYongqiang Yang EXT4_DESC_PER_BLOCK_BITS(sb); 6401f795f9SYongqiang Yang } 6501f795f9SYongqiang Yang 6601f795f9SYongqiang Yang static ext4_fsblk_t ext4_meta_bg_first_block_no(struct super_block *sb, 6701f795f9SYongqiang Yang ext4_group_t group) { 6801f795f9SYongqiang Yang group = ext4_meta_bg_first_group(sb, group); 6901f795f9SYongqiang Yang return ext4_group_first_block_no(sb, group); 7001f795f9SYongqiang Yang } 7101f795f9SYongqiang Yang 7201f795f9SYongqiang Yang static ext4_grpblk_t ext4_group_overhead_blocks(struct super_block *sb, 7301f795f9SYongqiang Yang ext4_group_t group) { 7401f795f9SYongqiang Yang ext4_grpblk_t overhead; 7501f795f9SYongqiang Yang overhead = ext4_bg_num_gdb(sb, group); 7601f795f9SYongqiang Yang if (ext4_bg_has_super(sb, group)) 7701f795f9SYongqiang Yang overhead += 1 + 7801f795f9SYongqiang Yang le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 7901f795f9SYongqiang Yang return overhead; 8001f795f9SYongqiang Yang } 8101f795f9SYongqiang Yang 82ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 83ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 84ac27a0ecSDave Kleikamp 85ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 86617ba13bSMingming Cao struct ext4_new_group_data *input) 87ac27a0ecSDave Kleikamp { 88617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 89617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 90bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 91617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 92fd2d4291SAvantika Mathur ext4_group_t group = input->group; 93617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 94b302ef2dSTheodore Ts'o unsigned overhead; 95b302ef2dSTheodore Ts'o ext4_fsblk_t metaend; 96ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 973a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 98ac27a0ecSDave Kleikamp int err = -EINVAL; 99ac27a0ecSDave Kleikamp 100b302ef2dSTheodore Ts'o if (group != sbi->s_groups_count) { 101b302ef2dSTheodore Ts'o ext4_warning(sb, "Cannot add at group %u (only %u groups)", 102b302ef2dSTheodore Ts'o input->group, sbi->s_groups_count); 103b302ef2dSTheodore Ts'o return -EINVAL; 104b302ef2dSTheodore Ts'o } 105b302ef2dSTheodore Ts'o 106b302ef2dSTheodore Ts'o overhead = ext4_group_overhead_blocks(sb, group); 107b302ef2dSTheodore Ts'o metaend = start + overhead; 108ac27a0ecSDave Kleikamp input->free_blocks_count = free_blocks_count = 109ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 110ac27a0ecSDave Kleikamp 111ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 112617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 113ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 114617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 115ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 116ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 117ac27a0ecSDave Kleikamp 1183a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 119b302ef2dSTheodore Ts'o if (offset != 0) 12012062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 121ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 12212062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 123ac27a0ecSDave Kleikamp input->reserved_blocks); 124ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 12512062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 126ac27a0ecSDave Kleikamp input->blocks_count); 127ac27a0ecSDave Kleikamp else if (!(bh = sb_bread(sb, end - 1))) 12812062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 129ac27a0ecSDave Kleikamp end - 1); 130ac27a0ecSDave Kleikamp else if (outside(input->block_bitmap, start, end)) 13112062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 1321939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 133ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 13412062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 1351939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 136ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 137ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 13812062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1391939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 140ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 14112062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1421939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 143ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 14412062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 14512062dddSEric Sandeen "(%llu-%llu)", 1461939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1471939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 148ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 14912062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 15012062dddSEric Sandeen "(%llu-%llu)", 1511939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1521939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 153ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 15412062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 1551939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1561939e49aSRandy Dunlap start, metaend - 1); 157ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 15812062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 1591939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1601939e49aSRandy Dunlap start, metaend - 1); 161ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 162ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 16312062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 16412062dddSEric Sandeen "(%llu-%llu)", 1651939e49aSRandy Dunlap (unsigned long long)input->inode_table, 1661939e49aSRandy Dunlap itend - 1, start, metaend - 1); 167ac27a0ecSDave Kleikamp else 168ac27a0ecSDave Kleikamp err = 0; 169ac27a0ecSDave Kleikamp brelse(bh); 170ac27a0ecSDave Kleikamp 171ac27a0ecSDave Kleikamp return err; 172ac27a0ecSDave Kleikamp } 173ac27a0ecSDave Kleikamp 17428c7bac0SYongqiang Yang /* 17528c7bac0SYongqiang Yang * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex 17628c7bac0SYongqiang Yang * group each time. 17728c7bac0SYongqiang Yang */ 17828c7bac0SYongqiang Yang struct ext4_new_flex_group_data { 17928c7bac0SYongqiang Yang struct ext4_new_group_data *groups; /* new_group_data for groups 18028c7bac0SYongqiang Yang in the flex group */ 18128c7bac0SYongqiang Yang __u16 *bg_flags; /* block group flags of groups 18228c7bac0SYongqiang Yang in @groups */ 18328c7bac0SYongqiang Yang ext4_group_t count; /* number of groups in @groups 18428c7bac0SYongqiang Yang */ 18528c7bac0SYongqiang Yang }; 18628c7bac0SYongqiang Yang 18728c7bac0SYongqiang Yang /* 18828c7bac0SYongqiang Yang * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of 18928c7bac0SYongqiang Yang * @flexbg_size. 19028c7bac0SYongqiang Yang * 19128c7bac0SYongqiang Yang * Returns NULL on failure otherwise address of the allocated structure. 19228c7bac0SYongqiang Yang */ 19328c7bac0SYongqiang Yang static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) 19428c7bac0SYongqiang Yang { 19528c7bac0SYongqiang Yang struct ext4_new_flex_group_data *flex_gd; 19628c7bac0SYongqiang Yang 19728c7bac0SYongqiang Yang flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS); 19828c7bac0SYongqiang Yang if (flex_gd == NULL) 19928c7bac0SYongqiang Yang goto out3; 20028c7bac0SYongqiang Yang 201967ac8afSHaogang Chen if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_flex_group_data)) 202967ac8afSHaogang Chen goto out2; 20328c7bac0SYongqiang Yang flex_gd->count = flexbg_size; 20428c7bac0SYongqiang Yang 20528c7bac0SYongqiang Yang flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) * 20628c7bac0SYongqiang Yang flexbg_size, GFP_NOFS); 20728c7bac0SYongqiang Yang if (flex_gd->groups == NULL) 20828c7bac0SYongqiang Yang goto out2; 20928c7bac0SYongqiang Yang 21028c7bac0SYongqiang Yang flex_gd->bg_flags = kmalloc(flexbg_size * sizeof(__u16), GFP_NOFS); 21128c7bac0SYongqiang Yang if (flex_gd->bg_flags == NULL) 21228c7bac0SYongqiang Yang goto out1; 21328c7bac0SYongqiang Yang 21428c7bac0SYongqiang Yang return flex_gd; 21528c7bac0SYongqiang Yang 21628c7bac0SYongqiang Yang out1: 21728c7bac0SYongqiang Yang kfree(flex_gd->groups); 21828c7bac0SYongqiang Yang out2: 21928c7bac0SYongqiang Yang kfree(flex_gd); 22028c7bac0SYongqiang Yang out3: 22128c7bac0SYongqiang Yang return NULL; 22228c7bac0SYongqiang Yang } 22328c7bac0SYongqiang Yang 22428c7bac0SYongqiang Yang static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd) 22528c7bac0SYongqiang Yang { 22628c7bac0SYongqiang Yang kfree(flex_gd->bg_flags); 22728c7bac0SYongqiang Yang kfree(flex_gd->groups); 22828c7bac0SYongqiang Yang kfree(flex_gd); 22928c7bac0SYongqiang Yang } 23028c7bac0SYongqiang Yang 2313fbea4b3SYongqiang Yang /* 2323fbea4b3SYongqiang Yang * ext4_alloc_group_tables() allocates block bitmaps, inode bitmaps 2333fbea4b3SYongqiang Yang * and inode tables for a flex group. 2343fbea4b3SYongqiang Yang * 2353fbea4b3SYongqiang Yang * This function is used by 64bit-resize. Note that this function allocates 2363fbea4b3SYongqiang Yang * group tables from the 1st group of groups contained by @flexgd, which may 2373fbea4b3SYongqiang Yang * be a partial of a flex group. 2383fbea4b3SYongqiang Yang * 2393fbea4b3SYongqiang Yang * @sb: super block of fs to which the groups belongs 24003c1c290SYongqiang Yang * 24103c1c290SYongqiang Yang * Returns 0 on a successful allocation of the metadata blocks in the 24203c1c290SYongqiang Yang * block group. 2433fbea4b3SYongqiang Yang */ 24403c1c290SYongqiang Yang static int ext4_alloc_group_tables(struct super_block *sb, 2453fbea4b3SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 2463fbea4b3SYongqiang Yang int flexbg_size) 2473fbea4b3SYongqiang Yang { 2483fbea4b3SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 2493fbea4b3SYongqiang Yang ext4_fsblk_t start_blk; 2503fbea4b3SYongqiang Yang ext4_fsblk_t last_blk; 2513fbea4b3SYongqiang Yang ext4_group_t src_group; 2523fbea4b3SYongqiang Yang ext4_group_t bb_index = 0; 2533fbea4b3SYongqiang Yang ext4_group_t ib_index = 0; 2543fbea4b3SYongqiang Yang ext4_group_t it_index = 0; 2553fbea4b3SYongqiang Yang ext4_group_t group; 2563fbea4b3SYongqiang Yang ext4_group_t last_group; 2573fbea4b3SYongqiang Yang unsigned overhead; 258b93c9535STheodore Ts'o __u16 uninit_mask = (flexbg_size > 1) ? ~EXT4_BG_BLOCK_UNINIT : ~0; 2593fbea4b3SYongqiang Yang 2603fbea4b3SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 2613fbea4b3SYongqiang Yang 2623fbea4b3SYongqiang Yang src_group = group_data[0].group; 2633fbea4b3SYongqiang Yang last_group = src_group + flex_gd->count - 1; 2643fbea4b3SYongqiang Yang 2653fbea4b3SYongqiang Yang BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) != 2663fbea4b3SYongqiang Yang (last_group & ~(flexbg_size - 1)))); 2673fbea4b3SYongqiang Yang next_group: 2683fbea4b3SYongqiang Yang group = group_data[0].group; 26903c1c290SYongqiang Yang if (src_group >= group_data[0].group + flex_gd->count) 27003c1c290SYongqiang Yang return -ENOSPC; 2713fbea4b3SYongqiang Yang start_blk = ext4_group_first_block_no(sb, src_group); 2723fbea4b3SYongqiang Yang last_blk = start_blk + group_data[src_group - group].blocks_count; 2733fbea4b3SYongqiang Yang 27401f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 2753fbea4b3SYongqiang Yang 2763fbea4b3SYongqiang Yang start_blk += overhead; 2773fbea4b3SYongqiang Yang 2783fbea4b3SYongqiang Yang /* We collect contiguous blocks as much as possible. */ 2793fbea4b3SYongqiang Yang src_group++; 28001f795f9SYongqiang Yang for (; src_group <= last_group; src_group++) { 28101f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, src_group); 282b93c9535STheodore Ts'o if (overhead == 0) 2833fbea4b3SYongqiang Yang last_blk += group_data[src_group - group].blocks_count; 2843fbea4b3SYongqiang Yang else 2853fbea4b3SYongqiang Yang break; 28601f795f9SYongqiang Yang } 2873fbea4b3SYongqiang Yang 2883fbea4b3SYongqiang Yang /* Allocate block bitmaps */ 2893fbea4b3SYongqiang Yang for (; bb_index < flex_gd->count; bb_index++) { 2903fbea4b3SYongqiang Yang if (start_blk >= last_blk) 2913fbea4b3SYongqiang Yang goto next_group; 2923fbea4b3SYongqiang Yang group_data[bb_index].block_bitmap = start_blk++; 293bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 2943fbea4b3SYongqiang Yang group -= group_data[0].group; 2953fbea4b3SYongqiang Yang group_data[group].free_blocks_count--; 296b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 2973fbea4b3SYongqiang Yang } 2983fbea4b3SYongqiang Yang 2993fbea4b3SYongqiang Yang /* Allocate inode bitmaps */ 3003fbea4b3SYongqiang Yang for (; ib_index < flex_gd->count; ib_index++) { 3013fbea4b3SYongqiang Yang if (start_blk >= last_blk) 3023fbea4b3SYongqiang Yang goto next_group; 3033fbea4b3SYongqiang Yang group_data[ib_index].inode_bitmap = start_blk++; 304bd86298eSLukas Czerner group = ext4_get_group_number(sb, start_blk - 1); 3053fbea4b3SYongqiang Yang group -= group_data[0].group; 3063fbea4b3SYongqiang Yang group_data[group].free_blocks_count--; 307b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3083fbea4b3SYongqiang Yang } 3093fbea4b3SYongqiang Yang 3103fbea4b3SYongqiang Yang /* Allocate inode tables */ 3113fbea4b3SYongqiang Yang for (; it_index < flex_gd->count; it_index++) { 312b93c9535STheodore Ts'o unsigned int itb = EXT4_SB(sb)->s_itb_per_group; 313b93c9535STheodore Ts'o ext4_fsblk_t next_group_start; 314b93c9535STheodore Ts'o 315b93c9535STheodore Ts'o if (start_blk + itb > last_blk) 3163fbea4b3SYongqiang Yang goto next_group; 3173fbea4b3SYongqiang Yang group_data[it_index].inode_table = start_blk; 318b93c9535STheodore Ts'o group = ext4_get_group_number(sb, start_blk); 319b93c9535STheodore Ts'o next_group_start = ext4_group_first_block_no(sb, group + 1); 3203fbea4b3SYongqiang Yang group -= group_data[0].group; 3213fbea4b3SYongqiang Yang 322b93c9535STheodore Ts'o if (start_blk + itb > next_group_start) { 323b93c9535STheodore Ts'o flex_gd->bg_flags[group + 1] &= uninit_mask; 324b93c9535STheodore Ts'o overhead = start_blk + itb - next_group_start; 325b93c9535STheodore Ts'o group_data[group + 1].free_blocks_count -= overhead; 326b93c9535STheodore Ts'o itb -= overhead; 327b93c9535STheodore Ts'o } 328b93c9535STheodore Ts'o 329b93c9535STheodore Ts'o group_data[group].free_blocks_count -= itb; 330b93c9535STheodore Ts'o flex_gd->bg_flags[group] &= uninit_mask; 3313fbea4b3SYongqiang Yang start_blk += EXT4_SB(sb)->s_itb_per_group; 3323fbea4b3SYongqiang Yang } 3333fbea4b3SYongqiang Yang 3343fbea4b3SYongqiang Yang if (test_opt(sb, DEBUG)) { 3353fbea4b3SYongqiang Yang int i; 3363fbea4b3SYongqiang Yang group = group_data[0].group; 3373fbea4b3SYongqiang Yang 3383fbea4b3SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: adding a flex group with " 3393fbea4b3SYongqiang Yang "%d groups, flexbg size is %d:\n", flex_gd->count, 3403fbea4b3SYongqiang Yang flexbg_size); 3413fbea4b3SYongqiang Yang 3423fbea4b3SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 3433fbea4b3SYongqiang Yang printk(KERN_DEBUG "adding %s group %u: %u " 3443fbea4b3SYongqiang Yang "blocks (%d free)\n", 3453fbea4b3SYongqiang Yang ext4_bg_has_super(sb, group + i) ? "normal" : 3463fbea4b3SYongqiang Yang "no-super", group + i, 3473fbea4b3SYongqiang Yang group_data[i].blocks_count, 3483fbea4b3SYongqiang Yang group_data[i].free_blocks_count); 3493fbea4b3SYongqiang Yang } 3503fbea4b3SYongqiang Yang } 35103c1c290SYongqiang Yang return 0; 3523fbea4b3SYongqiang Yang } 3533fbea4b3SYongqiang Yang 354ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 355617ba13bSMingming Cao ext4_fsblk_t blk) 356ac27a0ecSDave Kleikamp { 357ac27a0ecSDave Kleikamp struct buffer_head *bh; 358ac27a0ecSDave Kleikamp int err; 359ac27a0ecSDave Kleikamp 360ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 361aebf0243SWang Shilong if (unlikely(!bh)) 362860d21e2STheodore Ts'o return ERR_PTR(-ENOMEM); 3635d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 364617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 365ac27a0ecSDave Kleikamp brelse(bh); 366ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 367ac27a0ecSDave Kleikamp } else { 368ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 369ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 370ac27a0ecSDave Kleikamp } 371ac27a0ecSDave Kleikamp 372ac27a0ecSDave Kleikamp return bh; 373ac27a0ecSDave Kleikamp } 374ac27a0ecSDave Kleikamp 375ac27a0ecSDave Kleikamp /* 37614904107SEric Sandeen * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA. 37714904107SEric Sandeen * If that fails, restart the transaction & regain write access for the 37814904107SEric Sandeen * buffer head which is used for block_bitmap modifications. 37914904107SEric Sandeen */ 3806d40bc5aSYongqiang Yang static int extend_or_restart_transaction(handle_t *handle, int thresh) 38114904107SEric Sandeen { 38214904107SEric Sandeen int err; 38314904107SEric Sandeen 3840390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, thresh)) 38514904107SEric Sandeen return 0; 38614904107SEric Sandeen 38714904107SEric Sandeen err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA); 38814904107SEric Sandeen if (err < 0) 38914904107SEric Sandeen return err; 39014904107SEric Sandeen if (err) { 3916d40bc5aSYongqiang Yang err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA); 3926d40bc5aSYongqiang Yang if (err) 39314904107SEric Sandeen return err; 39414904107SEric Sandeen } 39514904107SEric Sandeen 39614904107SEric Sandeen return 0; 39714904107SEric Sandeen } 39814904107SEric Sandeen 39914904107SEric Sandeen /* 40033afdcc5SYongqiang Yang * set_flexbg_block_bitmap() mark @count blocks starting from @block used. 40133afdcc5SYongqiang Yang * 40233afdcc5SYongqiang Yang * Helper function for ext4_setup_new_group_blocks() which set . 40333afdcc5SYongqiang Yang * 40433afdcc5SYongqiang Yang * @sb: super block 40533afdcc5SYongqiang Yang * @handle: journal handle 40633afdcc5SYongqiang Yang * @flex_gd: flex group data 40733afdcc5SYongqiang Yang */ 40833afdcc5SYongqiang Yang static int set_flexbg_block_bitmap(struct super_block *sb, handle_t *handle, 40933afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 41033afdcc5SYongqiang Yang ext4_fsblk_t block, ext4_group_t count) 41133afdcc5SYongqiang Yang { 41233afdcc5SYongqiang Yang ext4_group_t count2; 41333afdcc5SYongqiang Yang 41433afdcc5SYongqiang Yang ext4_debug("mark blocks [%llu/%u] used\n", block, count); 41533afdcc5SYongqiang Yang for (count2 = count; count > 0; count -= count2, block += count2) { 41633afdcc5SYongqiang Yang ext4_fsblk_t start; 41733afdcc5SYongqiang Yang struct buffer_head *bh; 41833afdcc5SYongqiang Yang ext4_group_t group; 41933afdcc5SYongqiang Yang int err; 42033afdcc5SYongqiang Yang 421bd86298eSLukas Czerner group = ext4_get_group_number(sb, block); 42233afdcc5SYongqiang Yang start = ext4_group_first_block_no(sb, group); 42333afdcc5SYongqiang Yang group -= flex_gd->groups[0].group; 42433afdcc5SYongqiang Yang 4253d2660d0STheodore Ts'o count2 = EXT4_BLOCKS_PER_GROUP(sb) - (block - start); 42633afdcc5SYongqiang Yang if (count2 > count) 42733afdcc5SYongqiang Yang count2 = count; 42833afdcc5SYongqiang Yang 42933afdcc5SYongqiang Yang if (flex_gd->bg_flags[group] & EXT4_BG_BLOCK_UNINIT) { 43033afdcc5SYongqiang Yang BUG_ON(flex_gd->count > 1); 43133afdcc5SYongqiang Yang continue; 43233afdcc5SYongqiang Yang } 43333afdcc5SYongqiang Yang 43433afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 43533afdcc5SYongqiang Yang if (err) 43633afdcc5SYongqiang Yang return err; 43733afdcc5SYongqiang Yang 43833afdcc5SYongqiang Yang bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap); 439aebf0243SWang Shilong if (unlikely(!bh)) 440860d21e2STheodore Ts'o return -ENOMEM; 44133afdcc5SYongqiang Yang 4425d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 44333afdcc5SYongqiang Yang err = ext4_journal_get_write_access(handle, bh); 44433afdcc5SYongqiang Yang if (err) 44533afdcc5SYongqiang Yang return err; 44633afdcc5SYongqiang Yang ext4_debug("mark block bitmap %#04llx (+%llu/%u)\n", block, 44733afdcc5SYongqiang Yang block - start, count2); 44833afdcc5SYongqiang Yang ext4_set_bits(bh->b_data, block - start, count2); 44933afdcc5SYongqiang Yang 45033afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 45133afdcc5SYongqiang Yang if (unlikely(err)) 45233afdcc5SYongqiang Yang return err; 45333afdcc5SYongqiang Yang brelse(bh); 45433afdcc5SYongqiang Yang } 45533afdcc5SYongqiang Yang 45633afdcc5SYongqiang Yang return 0; 45733afdcc5SYongqiang Yang } 45833afdcc5SYongqiang Yang 45933afdcc5SYongqiang Yang /* 46033afdcc5SYongqiang Yang * Set up the block and inode bitmaps, and the inode table for the new groups. 46133afdcc5SYongqiang Yang * This doesn't need to be part of the main transaction, since we are only 46233afdcc5SYongqiang Yang * changing blocks outside the actual filesystem. We still do journaling to 46333afdcc5SYongqiang Yang * ensure the recovery is correct in case of a failure just after resize. 46433afdcc5SYongqiang Yang * If any part of this fails, we simply abort the resize. 46533afdcc5SYongqiang Yang * 46633afdcc5SYongqiang Yang * setup_new_flex_group_blocks handles a flex group as follow: 46733afdcc5SYongqiang Yang * 1. copy super block and GDT, and initialize group tables if necessary. 46833afdcc5SYongqiang Yang * In this step, we only set bits in blocks bitmaps for blocks taken by 46933afdcc5SYongqiang Yang * super block and GDT. 47033afdcc5SYongqiang Yang * 2. allocate group tables in block bitmaps, that is, set bits in block 47133afdcc5SYongqiang Yang * bitmap for blocks taken by group tables. 47233afdcc5SYongqiang Yang */ 47333afdcc5SYongqiang Yang static int setup_new_flex_group_blocks(struct super_block *sb, 47433afdcc5SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 47533afdcc5SYongqiang Yang { 47633afdcc5SYongqiang Yang int group_table_count[] = {1, 1, EXT4_SB(sb)->s_itb_per_group}; 47733afdcc5SYongqiang Yang ext4_fsblk_t start; 47833afdcc5SYongqiang Yang ext4_fsblk_t block; 47933afdcc5SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 48033afdcc5SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 48133afdcc5SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 48233afdcc5SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 48333afdcc5SYongqiang Yang handle_t *handle; 48433afdcc5SYongqiang Yang ext4_group_t group, count; 48533afdcc5SYongqiang Yang struct buffer_head *bh = NULL; 48633afdcc5SYongqiang Yang int reserved_gdb, i, j, err = 0, err2; 48701f795f9SYongqiang Yang int meta_bg; 48833afdcc5SYongqiang Yang 48933afdcc5SYongqiang Yang BUG_ON(!flex_gd->count || !group_data || 49033afdcc5SYongqiang Yang group_data[0].group != sbi->s_groups_count); 49133afdcc5SYongqiang Yang 49233afdcc5SYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 49301f795f9SYongqiang Yang meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG); 49433afdcc5SYongqiang Yang 49533afdcc5SYongqiang Yang /* This transaction may be extended/restarted along the way */ 4969924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 49733afdcc5SYongqiang Yang if (IS_ERR(handle)) 49833afdcc5SYongqiang Yang return PTR_ERR(handle); 49933afdcc5SYongqiang Yang 50033afdcc5SYongqiang Yang group = group_data[0].group; 50133afdcc5SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group++) { 50233afdcc5SYongqiang Yang unsigned long gdblocks; 50301f795f9SYongqiang Yang ext4_grpblk_t overhead; 50433afdcc5SYongqiang Yang 50533afdcc5SYongqiang Yang gdblocks = ext4_bg_num_gdb(sb, group); 50633afdcc5SYongqiang Yang start = ext4_group_first_block_no(sb, group); 50733afdcc5SYongqiang Yang 50801f795f9SYongqiang Yang if (meta_bg == 0 && !ext4_bg_has_super(sb, group)) 5096df935adSYongqiang Yang goto handle_itb; 5106df935adSYongqiang Yang 51101f795f9SYongqiang Yang if (meta_bg == 1) { 51201f795f9SYongqiang Yang ext4_group_t first_group; 51301f795f9SYongqiang Yang first_group = ext4_meta_bg_first_group(sb, group); 51401f795f9SYongqiang Yang if (first_group != group + 1 && 51501f795f9SYongqiang Yang first_group != group + EXT4_DESC_PER_BLOCK(sb) - 1) 51601f795f9SYongqiang Yang goto handle_itb; 51701f795f9SYongqiang Yang } 51801f795f9SYongqiang Yang 51901f795f9SYongqiang Yang block = start + ext4_bg_has_super(sb, group); 52033afdcc5SYongqiang Yang /* Copy all of the GDT blocks into the backup in this group */ 52101f795f9SYongqiang Yang for (j = 0; j < gdblocks; j++, block++) { 52233afdcc5SYongqiang Yang struct buffer_head *gdb; 52333afdcc5SYongqiang Yang 52433afdcc5SYongqiang Yang ext4_debug("update backup group %#04llx\n", block); 52533afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 52633afdcc5SYongqiang Yang if (err) 52733afdcc5SYongqiang Yang goto out; 52833afdcc5SYongqiang Yang 52933afdcc5SYongqiang Yang gdb = sb_getblk(sb, block); 530aebf0243SWang Shilong if (unlikely(!gdb)) { 531860d21e2STheodore Ts'o err = -ENOMEM; 53233afdcc5SYongqiang Yang goto out; 53333afdcc5SYongqiang Yang } 53433afdcc5SYongqiang Yang 5355d601255Sliang xie BUFFER_TRACE(gdb, "get_write_access"); 53633afdcc5SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb); 53733afdcc5SYongqiang Yang if (err) { 53833afdcc5SYongqiang Yang brelse(gdb); 53933afdcc5SYongqiang Yang goto out; 54033afdcc5SYongqiang Yang } 54133afdcc5SYongqiang Yang memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data, 54233afdcc5SYongqiang Yang gdb->b_size); 54333afdcc5SYongqiang Yang set_buffer_uptodate(gdb); 54433afdcc5SYongqiang Yang 54533afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb); 54633afdcc5SYongqiang Yang if (unlikely(err)) { 54733afdcc5SYongqiang Yang brelse(gdb); 54833afdcc5SYongqiang Yang goto out; 54933afdcc5SYongqiang Yang } 55033afdcc5SYongqiang Yang brelse(gdb); 55133afdcc5SYongqiang Yang } 55233afdcc5SYongqiang Yang 55333afdcc5SYongqiang Yang /* Zero out all of the reserved backup group descriptor 55433afdcc5SYongqiang Yang * table blocks 55533afdcc5SYongqiang Yang */ 55633afdcc5SYongqiang Yang if (ext4_bg_has_super(sb, group)) { 55733afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, gdblocks + start + 1, 55833afdcc5SYongqiang Yang reserved_gdb, GFP_NOFS); 55933afdcc5SYongqiang Yang if (err) 56033afdcc5SYongqiang Yang goto out; 56133afdcc5SYongqiang Yang } 56233afdcc5SYongqiang Yang 5636df935adSYongqiang Yang handle_itb: 56433afdcc5SYongqiang Yang /* Initialize group tables of the grop @group */ 56533afdcc5SYongqiang Yang if (!(bg_flags[i] & EXT4_BG_INODE_ZEROED)) 56633afdcc5SYongqiang Yang goto handle_bb; 56733afdcc5SYongqiang Yang 56833afdcc5SYongqiang Yang /* Zero out all of the inode table blocks */ 56933afdcc5SYongqiang Yang block = group_data[i].inode_table; 57033afdcc5SYongqiang Yang ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 57133afdcc5SYongqiang Yang block, sbi->s_itb_per_group); 57233afdcc5SYongqiang Yang err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, 57333afdcc5SYongqiang Yang GFP_NOFS); 57433afdcc5SYongqiang Yang if (err) 57533afdcc5SYongqiang Yang goto out; 57633afdcc5SYongqiang Yang 57733afdcc5SYongqiang Yang handle_bb: 57833afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT) 57933afdcc5SYongqiang Yang goto handle_ib; 58033afdcc5SYongqiang Yang 58133afdcc5SYongqiang Yang /* Initialize block bitmap of the @group */ 58233afdcc5SYongqiang Yang block = group_data[i].block_bitmap; 58333afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 58433afdcc5SYongqiang Yang if (err) 58533afdcc5SYongqiang Yang goto out; 58633afdcc5SYongqiang Yang 58733afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 58833afdcc5SYongqiang Yang if (IS_ERR(bh)) { 58933afdcc5SYongqiang Yang err = PTR_ERR(bh); 590a9cfcd63STheodore Ts'o bh = NULL; 59133afdcc5SYongqiang Yang goto out; 59233afdcc5SYongqiang Yang } 59301f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group); 59401f795f9SYongqiang Yang if (overhead != 0) { 59533afdcc5SYongqiang Yang ext4_debug("mark backup superblock %#04llx (+0)\n", 59633afdcc5SYongqiang Yang start); 59701f795f9SYongqiang Yang ext4_set_bits(bh->b_data, 0, overhead); 59833afdcc5SYongqiang Yang } 59933afdcc5SYongqiang Yang ext4_mark_bitmap_end(group_data[i].blocks_count, 60033afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 60133afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 60233afdcc5SYongqiang Yang if (err) 60333afdcc5SYongqiang Yang goto out; 60433afdcc5SYongqiang Yang brelse(bh); 60533afdcc5SYongqiang Yang 60633afdcc5SYongqiang Yang handle_ib: 60733afdcc5SYongqiang Yang if (bg_flags[i] & EXT4_BG_INODE_UNINIT) 60833afdcc5SYongqiang Yang continue; 60933afdcc5SYongqiang Yang 61033afdcc5SYongqiang Yang /* Initialize inode bitmap of the @group */ 61133afdcc5SYongqiang Yang block = group_data[i].inode_bitmap; 61233afdcc5SYongqiang Yang err = extend_or_restart_transaction(handle, 1); 61333afdcc5SYongqiang Yang if (err) 61433afdcc5SYongqiang Yang goto out; 61533afdcc5SYongqiang Yang /* Mark unused entries in inode bitmap used */ 61633afdcc5SYongqiang Yang bh = bclean(handle, sb, block); 61733afdcc5SYongqiang Yang if (IS_ERR(bh)) { 61833afdcc5SYongqiang Yang err = PTR_ERR(bh); 619a9cfcd63STheodore Ts'o bh = NULL; 62033afdcc5SYongqiang Yang goto out; 62133afdcc5SYongqiang Yang } 62233afdcc5SYongqiang Yang 62333afdcc5SYongqiang Yang ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 62433afdcc5SYongqiang Yang sb->s_blocksize * 8, bh->b_data); 62533afdcc5SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, bh); 62633afdcc5SYongqiang Yang if (err) 62733afdcc5SYongqiang Yang goto out; 62833afdcc5SYongqiang Yang brelse(bh); 62933afdcc5SYongqiang Yang } 63033afdcc5SYongqiang Yang bh = NULL; 63133afdcc5SYongqiang Yang 63233afdcc5SYongqiang Yang /* Mark group tables in block bitmap */ 63333afdcc5SYongqiang Yang for (j = 0; j < GROUP_TABLE_COUNT; j++) { 63433afdcc5SYongqiang Yang count = group_table_count[j]; 63533afdcc5SYongqiang Yang start = (&group_data[0].block_bitmap)[j]; 63633afdcc5SYongqiang Yang block = start; 63733afdcc5SYongqiang Yang for (i = 1; i < flex_gd->count; i++) { 63833afdcc5SYongqiang Yang block += group_table_count[j]; 63933afdcc5SYongqiang Yang if (block == (&group_data[i].block_bitmap)[j]) { 64033afdcc5SYongqiang Yang count += group_table_count[j]; 64133afdcc5SYongqiang Yang continue; 64233afdcc5SYongqiang Yang } 64333afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 64433afdcc5SYongqiang Yang flex_gd, start, count); 64533afdcc5SYongqiang Yang if (err) 64633afdcc5SYongqiang Yang goto out; 64733afdcc5SYongqiang Yang count = group_table_count[j]; 648b93c9535STheodore Ts'o start = (&group_data[i].block_bitmap)[j]; 64933afdcc5SYongqiang Yang block = start; 65033afdcc5SYongqiang Yang } 65133afdcc5SYongqiang Yang 65233afdcc5SYongqiang Yang if (count) { 65333afdcc5SYongqiang Yang err = set_flexbg_block_bitmap(sb, handle, 65433afdcc5SYongqiang Yang flex_gd, start, count); 65533afdcc5SYongqiang Yang if (err) 65633afdcc5SYongqiang Yang goto out; 65733afdcc5SYongqiang Yang } 65833afdcc5SYongqiang Yang } 65933afdcc5SYongqiang Yang 66033afdcc5SYongqiang Yang out: 66133afdcc5SYongqiang Yang brelse(bh); 66233afdcc5SYongqiang Yang err2 = ext4_journal_stop(handle); 66333afdcc5SYongqiang Yang if (err2 && !err) 66433afdcc5SYongqiang Yang err = err2; 66533afdcc5SYongqiang Yang 66633afdcc5SYongqiang Yang return err; 66733afdcc5SYongqiang Yang } 66833afdcc5SYongqiang Yang 66933afdcc5SYongqiang Yang /* 670ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 671617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 672ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 673ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 674ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 675ac27a0ecSDave Kleikamp */ 676617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 677ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 678ac27a0ecSDave Kleikamp { 679ac27a0ecSDave Kleikamp unsigned *min = three; 680ac27a0ecSDave Kleikamp int mult = 3; 681ac27a0ecSDave Kleikamp unsigned ret; 682ac27a0ecSDave Kleikamp 683617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 684617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 685ac27a0ecSDave Kleikamp ret = *min; 686ac27a0ecSDave Kleikamp *min += 1; 687ac27a0ecSDave Kleikamp return ret; 688ac27a0ecSDave Kleikamp } 689ac27a0ecSDave Kleikamp 690ac27a0ecSDave Kleikamp if (*five < *min) { 691ac27a0ecSDave Kleikamp min = five; 692ac27a0ecSDave Kleikamp mult = 5; 693ac27a0ecSDave Kleikamp } 694ac27a0ecSDave Kleikamp if (*seven < *min) { 695ac27a0ecSDave Kleikamp min = seven; 696ac27a0ecSDave Kleikamp mult = 7; 697ac27a0ecSDave Kleikamp } 698ac27a0ecSDave Kleikamp 699ac27a0ecSDave Kleikamp ret = *min; 700ac27a0ecSDave Kleikamp *min *= mult; 701ac27a0ecSDave Kleikamp 702ac27a0ecSDave Kleikamp return ret; 703ac27a0ecSDave Kleikamp } 704ac27a0ecSDave Kleikamp 705ac27a0ecSDave Kleikamp /* 706ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 707ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 708ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 709ac27a0ecSDave Kleikamp */ 710ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 711c72df9f9SYongqiang Yang ext4_group_t end, 712ac27a0ecSDave Kleikamp struct buffer_head *primary) 713ac27a0ecSDave Kleikamp { 714617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 715ac27a0ecSDave Kleikamp unsigned three = 1; 716ac27a0ecSDave Kleikamp unsigned five = 5; 717ac27a0ecSDave Kleikamp unsigned seven = 7; 718ac27a0ecSDave Kleikamp unsigned grp; 719ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 720ac27a0ecSDave Kleikamp int gdbackups = 0; 721ac27a0ecSDave Kleikamp 722617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 723bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 724bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 72512062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 7262ae02107SMingming Cao " missing grp %d (%llu)", 727ac27a0ecSDave Kleikamp blk, grp, 728bd81d8eeSLaurent Vivier grp * 729bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 730bd81d8eeSLaurent Vivier blk); 731ac27a0ecSDave Kleikamp return -EINVAL; 732ac27a0ecSDave Kleikamp } 733617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 734ac27a0ecSDave Kleikamp return -EFBIG; 735ac27a0ecSDave Kleikamp } 736ac27a0ecSDave Kleikamp 737ac27a0ecSDave Kleikamp return gdbackups; 738ac27a0ecSDave Kleikamp } 739ac27a0ecSDave Kleikamp 740ac27a0ecSDave Kleikamp /* 741ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 742ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 743ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 744ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 745ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 746ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 747ac27a0ecSDave Kleikamp * 748ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 749ac27a0ecSDave Kleikamp * 750ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 751ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 752ac27a0ecSDave Kleikamp */ 753ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 7542f919710SYongqiang Yang ext4_group_t group) 755ac27a0ecSDave Kleikamp { 756ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 757617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 7582f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 759617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 760ac27a0ecSDave Kleikamp struct buffer_head **o_group_desc, **n_group_desc; 761ac27a0ecSDave Kleikamp struct buffer_head *dind; 7622f919710SYongqiang Yang struct buffer_head *gdb_bh; 763ac27a0ecSDave Kleikamp int gdbackups; 764617ba13bSMingming Cao struct ext4_iloc iloc; 765ac27a0ecSDave Kleikamp __le32 *data; 766ac27a0ecSDave Kleikamp int err; 767ac27a0ecSDave Kleikamp 768ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 769ac27a0ecSDave Kleikamp printk(KERN_DEBUG 770617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 771ac27a0ecSDave Kleikamp gdb_num); 772ac27a0ecSDave Kleikamp 7732f919710SYongqiang Yang gdb_bh = sb_bread(sb, gdblock); 7742f919710SYongqiang Yang if (!gdb_bh) 775ac27a0ecSDave Kleikamp return -EIO; 776ac27a0ecSDave Kleikamp 777c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, gdb_bh); 7782f919710SYongqiang Yang if (gdbackups < 0) { 779ac27a0ecSDave Kleikamp err = gdbackups; 780ac27a0ecSDave Kleikamp goto exit_bh; 781ac27a0ecSDave Kleikamp } 782ac27a0ecSDave Kleikamp 783617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 784ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 785ac27a0ecSDave Kleikamp if (!dind) { 786ac27a0ecSDave Kleikamp err = -EIO; 787ac27a0ecSDave Kleikamp goto exit_bh; 788ac27a0ecSDave Kleikamp } 789ac27a0ecSDave Kleikamp 790ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 791617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 79212062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 7932f919710SYongqiang Yang group, gdblock); 794ac27a0ecSDave Kleikamp err = -EINVAL; 795ac27a0ecSDave Kleikamp goto exit_dind; 796ac27a0ecSDave Kleikamp } 797ac27a0ecSDave Kleikamp 7985d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 799b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 800b4097142STheodore Ts'o if (unlikely(err)) 801ac27a0ecSDave Kleikamp goto exit_dind; 802ac27a0ecSDave Kleikamp 8035d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 8042f919710SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 805b4097142STheodore Ts'o if (unlikely(err)) 80637be2f59SEric Sandeen goto exit_dind; 807ac27a0ecSDave Kleikamp 8085d601255Sliang xie BUFFER_TRACE(dind, "get_write_access"); 809b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, dind); 810b4097142STheodore Ts'o if (unlikely(err)) 811b4097142STheodore Ts'o ext4_std_error(sb, err); 812ac27a0ecSDave Kleikamp 813617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 814b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 815b4097142STheodore Ts'o if (unlikely(err)) 81637be2f59SEric Sandeen goto exit_dind; 817ac27a0ecSDave Kleikamp 818f18a5f21STheodore Ts'o n_group_desc = ext4_kvmalloc((gdb_num + 1) * 819f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 820216553c4SJosef Bacik GFP_NOFS); 821ac27a0ecSDave Kleikamp if (!n_group_desc) { 822ac27a0ecSDave Kleikamp err = -ENOMEM; 823f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 824f18a5f21STheodore Ts'o gdb_num + 1); 825ac27a0ecSDave Kleikamp goto exit_inode; 826ac27a0ecSDave Kleikamp } 827ac27a0ecSDave Kleikamp 828ac27a0ecSDave Kleikamp /* 829ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 830ac27a0ecSDave Kleikamp * 831ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 832ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 833ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 834ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 835ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 836ac27a0ecSDave Kleikamp */ 837617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 838b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 839b4097142STheodore Ts'o if (unlikely(err)) { 840b4097142STheodore Ts'o ext4_std_error(sb, err); 841b4097142STheodore Ts'o goto exit_inode; 842b4097142STheodore Ts'o } 843ac27a0ecSDave Kleikamp inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; 844617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 8452f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 8462f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 847b4097142STheodore Ts'o if (unlikely(err)) { 848b4097142STheodore Ts'o ext4_std_error(sb, err); 849b4097142STheodore Ts'o goto exit_inode; 850b4097142STheodore Ts'o } 851b4097142STheodore Ts'o brelse(dind); 852ac27a0ecSDave Kleikamp 853617ba13bSMingming Cao o_group_desc = EXT4_SB(sb)->s_group_desc; 854ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 855617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 8562f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 857617ba13bSMingming Cao EXT4_SB(sb)->s_group_desc = n_group_desc; 858617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 859b93b41d4SAl Viro kvfree(o_group_desc); 860ac27a0ecSDave Kleikamp 861e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 862b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 863b4097142STheodore Ts'o if (err) 864b4097142STheodore Ts'o ext4_std_error(sb, err); 865ac27a0ecSDave Kleikamp 866b4097142STheodore Ts'o return err; 867ac27a0ecSDave Kleikamp 868ac27a0ecSDave Kleikamp exit_inode: 869b93b41d4SAl Viro kvfree(n_group_desc); 870ac27a0ecSDave Kleikamp brelse(iloc.bh); 871ac27a0ecSDave Kleikamp exit_dind: 872ac27a0ecSDave Kleikamp brelse(dind); 873ac27a0ecSDave Kleikamp exit_bh: 8742f919710SYongqiang Yang brelse(gdb_bh); 875ac27a0ecSDave Kleikamp 876617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 877ac27a0ecSDave Kleikamp return err; 878ac27a0ecSDave Kleikamp } 879ac27a0ecSDave Kleikamp 880ac27a0ecSDave Kleikamp /* 88101f795f9SYongqiang Yang * add_new_gdb_meta_bg is the sister of add_new_gdb. 88201f795f9SYongqiang Yang */ 88301f795f9SYongqiang Yang static int add_new_gdb_meta_bg(struct super_block *sb, 88401f795f9SYongqiang Yang handle_t *handle, ext4_group_t group) { 88501f795f9SYongqiang Yang ext4_fsblk_t gdblock; 88601f795f9SYongqiang Yang struct buffer_head *gdb_bh; 88701f795f9SYongqiang Yang struct buffer_head **o_group_desc, **n_group_desc; 88801f795f9SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 88901f795f9SYongqiang Yang int err; 89001f795f9SYongqiang Yang 89101f795f9SYongqiang Yang gdblock = ext4_meta_bg_first_block_no(sb, group) + 89201f795f9SYongqiang Yang ext4_bg_has_super(sb, group); 89301f795f9SYongqiang Yang gdb_bh = sb_bread(sb, gdblock); 89401f795f9SYongqiang Yang if (!gdb_bh) 89501f795f9SYongqiang Yang return -EIO; 89601f795f9SYongqiang Yang n_group_desc = ext4_kvmalloc((gdb_num + 1) * 89701f795f9SYongqiang Yang sizeof(struct buffer_head *), 89801f795f9SYongqiang Yang GFP_NOFS); 89901f795f9SYongqiang Yang if (!n_group_desc) { 90001f795f9SYongqiang Yang err = -ENOMEM; 90101f795f9SYongqiang Yang ext4_warning(sb, "not enough memory for %lu groups", 90201f795f9SYongqiang Yang gdb_num + 1); 90301f795f9SYongqiang Yang return err; 90401f795f9SYongqiang Yang } 90501f795f9SYongqiang Yang 90601f795f9SYongqiang Yang o_group_desc = EXT4_SB(sb)->s_group_desc; 90701f795f9SYongqiang Yang memcpy(n_group_desc, o_group_desc, 90801f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 90901f795f9SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 91001f795f9SYongqiang Yang EXT4_SB(sb)->s_group_desc = n_group_desc; 91101f795f9SYongqiang Yang EXT4_SB(sb)->s_gdb_count++; 912b93b41d4SAl Viro kvfree(o_group_desc); 9135d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 91401f795f9SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 91501f795f9SYongqiang Yang if (unlikely(err)) 91601f795f9SYongqiang Yang brelse(gdb_bh); 91701f795f9SYongqiang Yang return err; 91801f795f9SYongqiang Yang } 91901f795f9SYongqiang Yang 92001f795f9SYongqiang Yang /* 921ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 922ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 923ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 924ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 925ac27a0ecSDave Kleikamp * 926ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 927ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 928ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 929ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 930ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 931ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 932ac27a0ecSDave Kleikamp */ 933ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 934668f4dc5SYongqiang Yang ext4_group_t group) 935ac27a0ecSDave Kleikamp { 936ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 937617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 938ac27a0ecSDave Kleikamp struct buffer_head **primary; 939ac27a0ecSDave Kleikamp struct buffer_head *dind; 940617ba13bSMingming Cao struct ext4_iloc iloc; 941617ba13bSMingming Cao ext4_fsblk_t blk; 942ac27a0ecSDave Kleikamp __le32 *data, *end; 943ac27a0ecSDave Kleikamp int gdbackups = 0; 944ac27a0ecSDave Kleikamp int res, i; 945ac27a0ecSDave Kleikamp int err; 946ac27a0ecSDave Kleikamp 947216553c4SJosef Bacik primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS); 948ac27a0ecSDave Kleikamp if (!primary) 949ac27a0ecSDave Kleikamp return -ENOMEM; 950ac27a0ecSDave Kleikamp 951617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 952ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 953ac27a0ecSDave Kleikamp if (!dind) { 954ac27a0ecSDave Kleikamp err = -EIO; 955ac27a0ecSDave Kleikamp goto exit_free; 956ac27a0ecSDave Kleikamp } 957ac27a0ecSDave Kleikamp 958617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 95994460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 96094460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 961617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 962ac27a0ecSDave Kleikamp 963ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 964ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 965ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 96612062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 967ac27a0ecSDave Kleikamp " not at offset %ld", 968ac27a0ecSDave Kleikamp blk, 969ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 970ac27a0ecSDave Kleikamp err = -EINVAL; 971ac27a0ecSDave Kleikamp goto exit_bh; 972ac27a0ecSDave Kleikamp } 973ac27a0ecSDave Kleikamp primary[res] = sb_bread(sb, blk); 974ac27a0ecSDave Kleikamp if (!primary[res]) { 975ac27a0ecSDave Kleikamp err = -EIO; 976ac27a0ecSDave Kleikamp goto exit_bh; 977ac27a0ecSDave Kleikamp } 978c72df9f9SYongqiang Yang gdbackups = verify_reserved_gdb(sb, group, primary[res]); 979c72df9f9SYongqiang Yang if (gdbackups < 0) { 980ac27a0ecSDave Kleikamp brelse(primary[res]); 981ac27a0ecSDave Kleikamp err = gdbackups; 982ac27a0ecSDave Kleikamp goto exit_bh; 983ac27a0ecSDave Kleikamp } 984ac27a0ecSDave Kleikamp if (++data >= end) 985ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 986ac27a0ecSDave Kleikamp } 987ac27a0ecSDave Kleikamp 988ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 9895d601255Sliang xie BUFFER_TRACE(primary[i], "get_write_access"); 99037be2f59SEric Sandeen if ((err = ext4_journal_get_write_access(handle, primary[i]))) 991ac27a0ecSDave Kleikamp goto exit_bh; 992ac27a0ecSDave Kleikamp } 993ac27a0ecSDave Kleikamp 994617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 995ac27a0ecSDave Kleikamp goto exit_bh; 996ac27a0ecSDave Kleikamp 997ac27a0ecSDave Kleikamp /* 998ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 999ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 1000ac27a0ecSDave Kleikamp */ 1001668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 1002ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 1003ac27a0ecSDave Kleikamp int err2; 1004ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 1005ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 1006ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 1007ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 1008ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 10090390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 1010ac27a0ecSDave Kleikamp if (!err) 1011ac27a0ecSDave Kleikamp err = err2; 1012ac27a0ecSDave Kleikamp } 1013ac27a0ecSDave Kleikamp inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; 1014617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 1015ac27a0ecSDave Kleikamp 1016ac27a0ecSDave Kleikamp exit_bh: 1017ac27a0ecSDave Kleikamp while (--res >= 0) 1018ac27a0ecSDave Kleikamp brelse(primary[res]); 1019ac27a0ecSDave Kleikamp brelse(dind); 1020ac27a0ecSDave Kleikamp 1021ac27a0ecSDave Kleikamp exit_free: 1022ac27a0ecSDave Kleikamp kfree(primary); 1023ac27a0ecSDave Kleikamp 1024ac27a0ecSDave Kleikamp return err; 1025ac27a0ecSDave Kleikamp } 1026ac27a0ecSDave Kleikamp 1027ac27a0ecSDave Kleikamp /* 1028617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 1029ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 1030ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 1031ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 1032ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 1033ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 1034ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 1035ac27a0ecSDave Kleikamp * 103632ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 103732ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 103832ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 103932ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 104032ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 104132ed5058STheodore Ts'o * which changed s_groups_count will backup again. 1042ac27a0ecSDave Kleikamp */ 104301f795f9SYongqiang Yang static void update_backups(struct super_block *sb, int blk_off, char *data, 104401f795f9SYongqiang Yang int size, int meta_bg) 1045ac27a0ecSDave Kleikamp { 1046617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 104701f795f9SYongqiang Yang ext4_group_t last; 1048617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 1049ac27a0ecSDave Kleikamp unsigned three = 1; 1050ac27a0ecSDave Kleikamp unsigned five = 5; 1051ac27a0ecSDave Kleikamp unsigned seven = 7; 105201f795f9SYongqiang Yang ext4_group_t group = 0; 1053ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 1054ac27a0ecSDave Kleikamp handle_t *handle; 1055ac27a0ecSDave Kleikamp int err = 0, err2; 1056ac27a0ecSDave Kleikamp 10579924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, EXT4_MAX_TRANS_DATA); 1058ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1059ac27a0ecSDave Kleikamp group = 1; 1060ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 1061ac27a0ecSDave Kleikamp goto exit_err; 1062ac27a0ecSDave Kleikamp } 1063ac27a0ecSDave Kleikamp 106401f795f9SYongqiang Yang if (meta_bg == 0) { 106501f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 106601f795f9SYongqiang Yang last = sbi->s_groups_count; 106701f795f9SYongqiang Yang } else { 106801f795f9SYongqiang Yang group = ext4_meta_bg_first_group(sb, group) + 1; 106901f795f9SYongqiang Yang last = (ext4_group_t)(group + EXT4_DESC_PER_BLOCK(sb) - 2); 107001f795f9SYongqiang Yang } 107101f795f9SYongqiang Yang 107201f795f9SYongqiang Yang while (group < sbi->s_groups_count) { 1073ac27a0ecSDave Kleikamp struct buffer_head *bh; 107401f795f9SYongqiang Yang ext4_fsblk_t backup_block; 1075ac27a0ecSDave Kleikamp 1076ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 10770390131bSFrank Mayhar if (ext4_handle_valid(handle) && 10780390131bSFrank Mayhar handle->h_buffer_credits == 0 && 1079617ba13bSMingming Cao ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && 1080617ba13bSMingming Cao (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 1081ac27a0ecSDave Kleikamp break; 1082ac27a0ecSDave Kleikamp 108301f795f9SYongqiang Yang if (meta_bg == 0) 10849378c676SJan Kara backup_block = ((ext4_fsblk_t)group) * bpg + blk_off; 108501f795f9SYongqiang Yang else 108601f795f9SYongqiang Yang backup_block = (ext4_group_first_block_no(sb, group) + 108701f795f9SYongqiang Yang ext4_bg_has_super(sb, group)); 108801f795f9SYongqiang Yang 108901f795f9SYongqiang Yang bh = sb_getblk(sb, backup_block); 1090aebf0243SWang Shilong if (unlikely(!bh)) { 1091860d21e2STheodore Ts'o err = -ENOMEM; 1092ac27a0ecSDave Kleikamp break; 1093ac27a0ecSDave Kleikamp } 109401f795f9SYongqiang Yang ext4_debug("update metadata backup %llu(+%llu)\n", 109501f795f9SYongqiang Yang backup_block, backup_block - 109601f795f9SYongqiang Yang ext4_group_first_block_no(sb, group)); 10975d601255Sliang xie BUFFER_TRACE(bh, "get_write_access"); 1098617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) 1099ac27a0ecSDave Kleikamp break; 1100ac27a0ecSDave Kleikamp lock_buffer(bh); 1101ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 1102ac27a0ecSDave Kleikamp if (rest) 1103ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 1104ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1105ac27a0ecSDave Kleikamp unlock_buffer(bh); 1106b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 1107b4097142STheodore Ts'o if (unlikely(err)) 1108b4097142STheodore Ts'o ext4_std_error(sb, err); 1109ac27a0ecSDave Kleikamp brelse(bh); 111001f795f9SYongqiang Yang 111101f795f9SYongqiang Yang if (meta_bg == 0) 111201f795f9SYongqiang Yang group = ext4_list_backups(sb, &three, &five, &seven); 111301f795f9SYongqiang Yang else if (group == last) 111401f795f9SYongqiang Yang break; 111501f795f9SYongqiang Yang else 111601f795f9SYongqiang Yang group = last; 1117ac27a0ecSDave Kleikamp } 1118617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 1119ac27a0ecSDave Kleikamp err = err2; 1120ac27a0ecSDave Kleikamp 1121ac27a0ecSDave Kleikamp /* 1122ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 1123ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 1124ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 1125ac27a0ecSDave Kleikamp * 1126ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 1127ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 1128ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 1129ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 1130ac27a0ecSDave Kleikamp */ 1131ac27a0ecSDave Kleikamp exit_err: 1132ac27a0ecSDave Kleikamp if (err) { 113312062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 1134ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 1135617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 1136617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 1137ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 1138ac27a0ecSDave Kleikamp } 1139ac27a0ecSDave Kleikamp } 1140ac27a0ecSDave Kleikamp 1141bb08c1e7SYongqiang Yang /* 1142bb08c1e7SYongqiang Yang * ext4_add_new_descs() adds @count group descriptor of groups 1143bb08c1e7SYongqiang Yang * starting at @group 1144bb08c1e7SYongqiang Yang * 1145bb08c1e7SYongqiang Yang * @handle: journal handle 1146bb08c1e7SYongqiang Yang * @sb: super block 1147bb08c1e7SYongqiang Yang * @group: the group no. of the first group desc to be added 1148bb08c1e7SYongqiang Yang * @resize_inode: the resize inode 1149bb08c1e7SYongqiang Yang * @count: number of group descriptors to be added 1150bb08c1e7SYongqiang Yang */ 1151bb08c1e7SYongqiang Yang static int ext4_add_new_descs(handle_t *handle, struct super_block *sb, 1152bb08c1e7SYongqiang Yang ext4_group_t group, struct inode *resize_inode, 1153bb08c1e7SYongqiang Yang ext4_group_t count) 1154bb08c1e7SYongqiang Yang { 1155bb08c1e7SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1156bb08c1e7SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 1157bb08c1e7SYongqiang Yang struct buffer_head *gdb_bh; 1158bb08c1e7SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 115901f795f9SYongqiang Yang int meta_bg; 1160bb08c1e7SYongqiang Yang 116101f795f9SYongqiang Yang meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG); 1162bb08c1e7SYongqiang Yang for (i = 0; i < count; i++, group++) { 1163bb08c1e7SYongqiang Yang int reserved_gdb = ext4_bg_has_super(sb, group) ? 1164bb08c1e7SYongqiang Yang le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1165bb08c1e7SYongqiang Yang 1166bb08c1e7SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1167bb08c1e7SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1168bb08c1e7SYongqiang Yang 1169bb08c1e7SYongqiang Yang /* 1170bb08c1e7SYongqiang Yang * We will only either add reserved group blocks to a backup group 1171bb08c1e7SYongqiang Yang * or remove reserved blocks for the first group in a new group block. 1172bb08c1e7SYongqiang Yang * Doing both would be mean more complex code, and sane people don't 1173bb08c1e7SYongqiang Yang * use non-sparse filesystems anymore. This is already checked above. 1174bb08c1e7SYongqiang Yang */ 1175bb08c1e7SYongqiang Yang if (gdb_off) { 1176bb08c1e7SYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 11775d601255Sliang xie BUFFER_TRACE(gdb_bh, "get_write_access"); 1178bb08c1e7SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 1179bb08c1e7SYongqiang Yang 1180bb08c1e7SYongqiang Yang if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 1181bb08c1e7SYongqiang Yang err = reserve_backup_gdb(handle, resize_inode, group); 118201f795f9SYongqiang Yang } else if (meta_bg != 0) { 118301f795f9SYongqiang Yang err = add_new_gdb_meta_bg(sb, handle, group); 118401f795f9SYongqiang Yang } else { 1185bb08c1e7SYongqiang Yang err = add_new_gdb(handle, resize_inode, group); 118601f795f9SYongqiang Yang } 1187bb08c1e7SYongqiang Yang if (err) 1188bb08c1e7SYongqiang Yang break; 1189bb08c1e7SYongqiang Yang } 1190bb08c1e7SYongqiang Yang return err; 1191bb08c1e7SYongqiang Yang } 1192bb08c1e7SYongqiang Yang 119341a246d1SDarrick J. Wong static struct buffer_head *ext4_get_bitmap(struct super_block *sb, __u64 block) 119441a246d1SDarrick J. Wong { 119541a246d1SDarrick J. Wong struct buffer_head *bh = sb_getblk(sb, block); 1196aebf0243SWang Shilong if (unlikely(!bh)) 119741a246d1SDarrick J. Wong return NULL; 11987f1468d1SDmitry Monakhov if (!bh_uptodate_or_lock(bh)) { 119941a246d1SDarrick J. Wong if (bh_submit_read(bh) < 0) { 120041a246d1SDarrick J. Wong brelse(bh); 120141a246d1SDarrick J. Wong return NULL; 120241a246d1SDarrick J. Wong } 12037f1468d1SDmitry Monakhov } 120441a246d1SDarrick J. Wong 120541a246d1SDarrick J. Wong return bh; 120641a246d1SDarrick J. Wong } 120741a246d1SDarrick J. Wong 120841a246d1SDarrick J. Wong static int ext4_set_bitmap_checksums(struct super_block *sb, 120941a246d1SDarrick J. Wong ext4_group_t group, 121041a246d1SDarrick J. Wong struct ext4_group_desc *gdp, 121141a246d1SDarrick J. Wong struct ext4_new_group_data *group_data) 121241a246d1SDarrick J. Wong { 121341a246d1SDarrick J. Wong struct buffer_head *bh; 121441a246d1SDarrick J. Wong 12159aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 121641a246d1SDarrick J. Wong return 0; 121741a246d1SDarrick J. Wong 121841a246d1SDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->inode_bitmap); 121941a246d1SDarrick J. Wong if (!bh) 122041a246d1SDarrick J. Wong return -EIO; 122141a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, bh, 122241a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 122341a246d1SDarrick J. Wong brelse(bh); 122441a246d1SDarrick J. Wong 1225fa77dcfaSDarrick J. Wong bh = ext4_get_bitmap(sb, group_data->block_bitmap); 1226fa77dcfaSDarrick J. Wong if (!bh) 1227fa77dcfaSDarrick J. Wong return -EIO; 122879f1ba49STao Ma ext4_block_bitmap_csum_set(sb, group, gdp, bh); 1229fa77dcfaSDarrick J. Wong brelse(bh); 1230fa77dcfaSDarrick J. Wong 123141a246d1SDarrick J. Wong return 0; 123241a246d1SDarrick J. Wong } 123341a246d1SDarrick J. Wong 1234083f5b24SYongqiang Yang /* 1235083f5b24SYongqiang Yang * ext4_setup_new_descs() will set up the group descriptor descriptors of a flex bg 1236083f5b24SYongqiang Yang */ 1237083f5b24SYongqiang Yang static int ext4_setup_new_descs(handle_t *handle, struct super_block *sb, 1238083f5b24SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 1239083f5b24SYongqiang Yang { 1240083f5b24SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 1241083f5b24SYongqiang Yang struct ext4_group_desc *gdp; 1242083f5b24SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 1243083f5b24SYongqiang Yang struct buffer_head *gdb_bh; 1244083f5b24SYongqiang Yang ext4_group_t group; 1245083f5b24SYongqiang Yang __u16 *bg_flags = flex_gd->bg_flags; 1246083f5b24SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 1247083f5b24SYongqiang Yang 1248083f5b24SYongqiang Yang 1249083f5b24SYongqiang Yang for (i = 0; i < flex_gd->count; i++, group_data++, bg_flags++) { 1250083f5b24SYongqiang Yang group = group_data->group; 1251083f5b24SYongqiang Yang 1252083f5b24SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 1253083f5b24SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 1254083f5b24SYongqiang Yang 1255083f5b24SYongqiang Yang /* 1256083f5b24SYongqiang Yang * get_write_access() has been called on gdb_bh by ext4_add_new_desc(). 1257083f5b24SYongqiang Yang */ 1258083f5b24SYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 1259083f5b24SYongqiang Yang /* Update group descriptor block for new group */ 12602716b802STheodore Ts'o gdp = (struct ext4_group_desc *)(gdb_bh->b_data + 1261083f5b24SYongqiang Yang gdb_off * EXT4_DESC_SIZE(sb)); 1262083f5b24SYongqiang Yang 1263083f5b24SYongqiang Yang memset(gdp, 0, EXT4_DESC_SIZE(sb)); 1264083f5b24SYongqiang Yang ext4_block_bitmap_set(sb, gdp, group_data->block_bitmap); 1265083f5b24SYongqiang Yang ext4_inode_bitmap_set(sb, gdp, group_data->inode_bitmap); 126641a246d1SDarrick J. Wong err = ext4_set_bitmap_checksums(sb, group, gdp, group_data); 126741a246d1SDarrick J. Wong if (err) { 126841a246d1SDarrick J. Wong ext4_std_error(sb, err); 126941a246d1SDarrick J. Wong break; 127041a246d1SDarrick J. Wong } 127141a246d1SDarrick J. Wong 1272083f5b24SYongqiang Yang ext4_inode_table_set(sb, gdp, group_data->inode_table); 1273083f5b24SYongqiang Yang ext4_free_group_clusters_set(sb, gdp, 1274810da240SLukas Czerner EXT4_NUM_B2C(sbi, group_data->free_blocks_count)); 1275083f5b24SYongqiang Yang ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 127693f90526STheodore Ts'o if (ext4_has_group_desc_csum(sb)) 127793f90526STheodore Ts'o ext4_itable_unused_set(sb, gdp, 127893f90526STheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 1279083f5b24SYongqiang Yang gdp->bg_flags = cpu_to_le16(*bg_flags); 1280feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1281083f5b24SYongqiang Yang 1282083f5b24SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 1283083f5b24SYongqiang Yang if (unlikely(err)) { 1284083f5b24SYongqiang Yang ext4_std_error(sb, err); 1285083f5b24SYongqiang Yang break; 1286083f5b24SYongqiang Yang } 1287083f5b24SYongqiang Yang 1288083f5b24SYongqiang Yang /* 1289083f5b24SYongqiang Yang * We can allocate memory for mb_alloc based on the new group 1290083f5b24SYongqiang Yang * descriptor 1291083f5b24SYongqiang Yang */ 1292083f5b24SYongqiang Yang err = ext4_mb_add_groupinfo(sb, group, gdp); 1293083f5b24SYongqiang Yang if (err) 1294083f5b24SYongqiang Yang break; 1295083f5b24SYongqiang Yang } 1296083f5b24SYongqiang Yang return err; 1297083f5b24SYongqiang Yang } 1298083f5b24SYongqiang Yang 12992e10e2f2SYongqiang Yang /* 13002e10e2f2SYongqiang Yang * ext4_update_super() updates the super block so that the newly added 13012e10e2f2SYongqiang Yang * groups can be seen by the filesystem. 13022e10e2f2SYongqiang Yang * 13032e10e2f2SYongqiang Yang * @sb: super block 13042e10e2f2SYongqiang Yang * @flex_gd: new added groups 13052e10e2f2SYongqiang Yang */ 13062e10e2f2SYongqiang Yang static void ext4_update_super(struct super_block *sb, 13072e10e2f2SYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 13082e10e2f2SYongqiang Yang { 13092e10e2f2SYongqiang Yang ext4_fsblk_t blocks_count = 0; 13102e10e2f2SYongqiang Yang ext4_fsblk_t free_blocks = 0; 13112e10e2f2SYongqiang Yang ext4_fsblk_t reserved_blocks = 0; 13122e10e2f2SYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 13132e10e2f2SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 13142e10e2f2SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 13158a991849STheodore Ts'o int i; 13162e10e2f2SYongqiang Yang 13172e10e2f2SYongqiang Yang BUG_ON(flex_gd->count == 0 || group_data == NULL); 13182e10e2f2SYongqiang Yang /* 13192e10e2f2SYongqiang Yang * Make the new blocks and inodes valid next. We do this before 13202e10e2f2SYongqiang Yang * increasing the group count so that once the group is enabled, 13212e10e2f2SYongqiang Yang * all of its blocks and inodes are already valid. 13222e10e2f2SYongqiang Yang * 13232e10e2f2SYongqiang Yang * We always allocate group-by-group, then block-by-block or 13242e10e2f2SYongqiang Yang * inode-by-inode within a group, so enabling these 13252e10e2f2SYongqiang Yang * blocks/inodes before the group is live won't actually let us 13262e10e2f2SYongqiang Yang * allocate the new space yet. 13272e10e2f2SYongqiang Yang */ 13282e10e2f2SYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 13292e10e2f2SYongqiang Yang blocks_count += group_data[i].blocks_count; 13302e10e2f2SYongqiang Yang free_blocks += group_data[i].free_blocks_count; 13312e10e2f2SYongqiang Yang } 13322e10e2f2SYongqiang Yang 13332e10e2f2SYongqiang Yang reserved_blocks = ext4_r_blocks_count(es) * 100; 133401f795f9SYongqiang Yang reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es)); 13352e10e2f2SYongqiang Yang reserved_blocks *= blocks_count; 13362e10e2f2SYongqiang Yang do_div(reserved_blocks, 100); 13372e10e2f2SYongqiang Yang 13382e10e2f2SYongqiang Yang ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count); 1339636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks); 13402e10e2f2SYongqiang Yang le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb) * 13412e10e2f2SYongqiang Yang flex_gd->count); 1342636d7e2eSDarrick J. Wong le32_add_cpu(&es->s_free_inodes_count, EXT4_INODES_PER_GROUP(sb) * 1343636d7e2eSDarrick J. Wong flex_gd->count); 13442e10e2f2SYongqiang Yang 134501f795f9SYongqiang Yang ext4_debug("free blocks count %llu", ext4_free_blocks_count(es)); 13462e10e2f2SYongqiang Yang /* 13472e10e2f2SYongqiang Yang * We need to protect s_groups_count against other CPUs seeing 13482e10e2f2SYongqiang Yang * inconsistent state in the superblock. 13492e10e2f2SYongqiang Yang * 13502e10e2f2SYongqiang Yang * The precise rules we use are: 13512e10e2f2SYongqiang Yang * 13522e10e2f2SYongqiang Yang * * Writers must perform a smp_wmb() after updating all 13532e10e2f2SYongqiang Yang * dependent data and before modifying the groups count 13542e10e2f2SYongqiang Yang * 13552e10e2f2SYongqiang Yang * * Readers must perform an smp_rmb() after reading the groups 13562e10e2f2SYongqiang Yang * count and before reading any dependent data. 13572e10e2f2SYongqiang Yang * 13582e10e2f2SYongqiang Yang * NB. These rules can be relaxed when checking the group count 13592e10e2f2SYongqiang Yang * while freeing data, as we can only allocate from a block 13602e10e2f2SYongqiang Yang * group after serialising against the group count, and we can 13612e10e2f2SYongqiang Yang * only then free after serialising in turn against that 13622e10e2f2SYongqiang Yang * allocation. 13632e10e2f2SYongqiang Yang */ 13642e10e2f2SYongqiang Yang smp_wmb(); 13652e10e2f2SYongqiang Yang 13662e10e2f2SYongqiang Yang /* Update the global fs size fields */ 13672e10e2f2SYongqiang Yang sbi->s_groups_count += flex_gd->count; 1368c5c72d81STheodore Ts'o sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 1369c5c72d81STheodore Ts'o (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 13702e10e2f2SYongqiang Yang 13712e10e2f2SYongqiang Yang /* Update the reserved block counts only once the new group is 13722e10e2f2SYongqiang Yang * active. */ 13732e10e2f2SYongqiang Yang ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 13742e10e2f2SYongqiang Yang reserved_blocks); 13752e10e2f2SYongqiang Yang 13762e10e2f2SYongqiang Yang /* Update the free space counts */ 13772e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeclusters_counter, 1378810da240SLukas Czerner EXT4_NUM_B2C(sbi, free_blocks)); 13792e10e2f2SYongqiang Yang percpu_counter_add(&sbi->s_freeinodes_counter, 13802e10e2f2SYongqiang Yang EXT4_INODES_PER_GROUP(sb) * flex_gd->count); 13812e10e2f2SYongqiang Yang 138201f795f9SYongqiang Yang ext4_debug("free blocks count %llu", 138301f795f9SYongqiang Yang percpu_counter_read(&sbi->s_freeclusters_counter)); 13842e10e2f2SYongqiang Yang if (EXT4_HAS_INCOMPAT_FEATURE(sb, 13852e10e2f2SYongqiang Yang EXT4_FEATURE_INCOMPAT_FLEX_BG) && 13862e10e2f2SYongqiang Yang sbi->s_log_groups_per_flex) { 13872e10e2f2SYongqiang Yang ext4_group_t flex_group; 13882e10e2f2SYongqiang Yang flex_group = ext4_flex_group(sbi, group_data[0].group); 138990ba983fSTheodore Ts'o atomic64_add(EXT4_NUM_B2C(sbi, free_blocks), 13902e10e2f2SYongqiang Yang &sbi->s_flex_groups[flex_group].free_clusters); 13912e10e2f2SYongqiang Yang atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count, 13922e10e2f2SYongqiang Yang &sbi->s_flex_groups[flex_group].free_inodes); 13932e10e2f2SYongqiang Yang } 13942e10e2f2SYongqiang Yang 1395952fc18eSTheodore Ts'o /* 1396952fc18eSTheodore Ts'o * Update the fs overhead information 1397952fc18eSTheodore Ts'o */ 1398952fc18eSTheodore Ts'o ext4_calculate_overhead(sb); 1399952fc18eSTheodore Ts'o 14002e10e2f2SYongqiang Yang if (test_opt(sb, DEBUG)) 14012e10e2f2SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: added group %u:" 14022e10e2f2SYongqiang Yang "%llu blocks(%llu free %llu reserved)\n", flex_gd->count, 14032e10e2f2SYongqiang Yang blocks_count, free_blocks, reserved_blocks); 14042e10e2f2SYongqiang Yang } 14052e10e2f2SYongqiang Yang 14064bac1f8cSYongqiang Yang /* Add a flex group to an fs. Ensure we handle all possible error conditions 14074bac1f8cSYongqiang Yang * _before_ we start modifying the filesystem, because we cannot abort the 14084bac1f8cSYongqiang Yang * transaction and not have it write the data to disk. 14094bac1f8cSYongqiang Yang */ 14104bac1f8cSYongqiang Yang static int ext4_flex_group_add(struct super_block *sb, 14114bac1f8cSYongqiang Yang struct inode *resize_inode, 14124bac1f8cSYongqiang Yang struct ext4_new_flex_group_data *flex_gd) 14134bac1f8cSYongqiang Yang { 14144bac1f8cSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 14154bac1f8cSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 14164bac1f8cSYongqiang Yang ext4_fsblk_t o_blocks_count; 14174bac1f8cSYongqiang Yang ext4_grpblk_t last; 14184bac1f8cSYongqiang Yang ext4_group_t group; 14194bac1f8cSYongqiang Yang handle_t *handle; 14204bac1f8cSYongqiang Yang unsigned reserved_gdb; 14214bac1f8cSYongqiang Yang int err = 0, err2 = 0, credit; 14224bac1f8cSYongqiang Yang 14234bac1f8cSYongqiang Yang BUG_ON(!flex_gd->count || !flex_gd->groups || !flex_gd->bg_flags); 14244bac1f8cSYongqiang Yang 14254bac1f8cSYongqiang Yang reserved_gdb = le16_to_cpu(es->s_reserved_gdt_blocks); 14264bac1f8cSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 14274bac1f8cSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 14284bac1f8cSYongqiang Yang BUG_ON(last); 14294bac1f8cSYongqiang Yang 14304bac1f8cSYongqiang Yang err = setup_new_flex_group_blocks(sb, flex_gd); 14314bac1f8cSYongqiang Yang if (err) 14324bac1f8cSYongqiang Yang goto exit; 14334bac1f8cSYongqiang Yang /* 14344bac1f8cSYongqiang Yang * We will always be modifying at least the superblock and GDT 14354bac1f8cSYongqiang Yang * block. If we are adding a group past the last current GDT block, 14364bac1f8cSYongqiang Yang * we will also modify the inode and the dindirect block. If we 14374bac1f8cSYongqiang Yang * are adding a group with superblock/GDT backups we will also 14384bac1f8cSYongqiang Yang * modify each of the reserved GDT dindirect blocks. 14394bac1f8cSYongqiang Yang */ 14404bac1f8cSYongqiang Yang credit = flex_gd->count * 4 + reserved_gdb; 14419924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit); 14424bac1f8cSYongqiang Yang if (IS_ERR(handle)) { 14434bac1f8cSYongqiang Yang err = PTR_ERR(handle); 14444bac1f8cSYongqiang Yang goto exit; 14454bac1f8cSYongqiang Yang } 14464bac1f8cSYongqiang Yang 14475d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 14484bac1f8cSYongqiang Yang err = ext4_journal_get_write_access(handle, sbi->s_sbh); 14494bac1f8cSYongqiang Yang if (err) 14504bac1f8cSYongqiang Yang goto exit_journal; 14514bac1f8cSYongqiang Yang 14524bac1f8cSYongqiang Yang group = flex_gd->groups[0].group; 14534bac1f8cSYongqiang Yang BUG_ON(group != EXT4_SB(sb)->s_groups_count); 14544bac1f8cSYongqiang Yang err = ext4_add_new_descs(handle, sb, group, 14554bac1f8cSYongqiang Yang resize_inode, flex_gd->count); 14564bac1f8cSYongqiang Yang if (err) 14574bac1f8cSYongqiang Yang goto exit_journal; 14584bac1f8cSYongqiang Yang 14594bac1f8cSYongqiang Yang err = ext4_setup_new_descs(handle, sb, flex_gd); 14604bac1f8cSYongqiang Yang if (err) 14614bac1f8cSYongqiang Yang goto exit_journal; 14624bac1f8cSYongqiang Yang 14634bac1f8cSYongqiang Yang ext4_update_super(sb, flex_gd); 14644bac1f8cSYongqiang Yang 14654bac1f8cSYongqiang Yang err = ext4_handle_dirty_super(handle, sb); 14664bac1f8cSYongqiang Yang 14674bac1f8cSYongqiang Yang exit_journal: 14684bac1f8cSYongqiang Yang err2 = ext4_journal_stop(handle); 14694bac1f8cSYongqiang Yang if (!err) 14704bac1f8cSYongqiang Yang err = err2; 14714bac1f8cSYongqiang Yang 14724bac1f8cSYongqiang Yang if (!err) { 14732ebd1704SYongqiang Yang int gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 14742ebd1704SYongqiang Yang int gdb_num_end = ((group + flex_gd->count - 1) / 14752ebd1704SYongqiang Yang EXT4_DESC_PER_BLOCK(sb)); 147601f795f9SYongqiang Yang int meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, 147701f795f9SYongqiang Yang EXT4_FEATURE_INCOMPAT_META_BG); 14780acdb887STao Ma sector_t old_gdb = 0; 14792ebd1704SYongqiang Yang 14804bac1f8cSYongqiang Yang update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 148101f795f9SYongqiang Yang sizeof(struct ext4_super_block), 0); 14822ebd1704SYongqiang Yang for (; gdb_num <= gdb_num_end; gdb_num++) { 14834bac1f8cSYongqiang Yang struct buffer_head *gdb_bh; 14842ebd1704SYongqiang Yang 14854bac1f8cSYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 14860acdb887STao Ma if (old_gdb == gdb_bh->b_blocknr) 14870acdb887STao Ma continue; 14884bac1f8cSYongqiang Yang update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data, 148901f795f9SYongqiang Yang gdb_bh->b_size, meta_bg); 14900acdb887STao Ma old_gdb = gdb_bh->b_blocknr; 14914bac1f8cSYongqiang Yang } 14924bac1f8cSYongqiang Yang } 14934bac1f8cSYongqiang Yang exit: 14944bac1f8cSYongqiang Yang return err; 14954bac1f8cSYongqiang Yang } 14964bac1f8cSYongqiang Yang 149719c5246dSYongqiang Yang static int ext4_setup_next_flex_gd(struct super_block *sb, 149819c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd, 149919c5246dSYongqiang Yang ext4_fsblk_t n_blocks_count, 150019c5246dSYongqiang Yang unsigned long flexbg_size) 150119c5246dSYongqiang Yang { 150219c5246dSYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 150319c5246dSYongqiang Yang struct ext4_new_group_data *group_data = flex_gd->groups; 150419c5246dSYongqiang Yang ext4_fsblk_t o_blocks_count; 150519c5246dSYongqiang Yang ext4_group_t n_group; 150619c5246dSYongqiang Yang ext4_group_t group; 150719c5246dSYongqiang Yang ext4_group_t last_group; 150819c5246dSYongqiang Yang ext4_grpblk_t last; 150919c5246dSYongqiang Yang ext4_grpblk_t blocks_per_group; 151019c5246dSYongqiang Yang unsigned long i; 151119c5246dSYongqiang Yang 151219c5246dSYongqiang Yang blocks_per_group = EXT4_BLOCKS_PER_GROUP(sb); 151319c5246dSYongqiang Yang 151419c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 151519c5246dSYongqiang Yang 151619c5246dSYongqiang Yang if (o_blocks_count == n_blocks_count) 151719c5246dSYongqiang Yang return 0; 151819c5246dSYongqiang Yang 151919c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 152019c5246dSYongqiang Yang BUG_ON(last); 152119c5246dSYongqiang Yang ext4_get_group_no_and_offset(sb, n_blocks_count - 1, &n_group, &last); 152219c5246dSYongqiang Yang 152319c5246dSYongqiang Yang last_group = group | (flexbg_size - 1); 152419c5246dSYongqiang Yang if (last_group > n_group) 152519c5246dSYongqiang Yang last_group = n_group; 152619c5246dSYongqiang Yang 152719c5246dSYongqiang Yang flex_gd->count = last_group - group + 1; 152819c5246dSYongqiang Yang 152919c5246dSYongqiang Yang for (i = 0; i < flex_gd->count; i++) { 153019c5246dSYongqiang Yang int overhead; 153119c5246dSYongqiang Yang 153219c5246dSYongqiang Yang group_data[i].group = group + i; 153319c5246dSYongqiang Yang group_data[i].blocks_count = blocks_per_group; 153401f795f9SYongqiang Yang overhead = ext4_group_overhead_blocks(sb, group + i); 153519c5246dSYongqiang Yang group_data[i].free_blocks_count = blocks_per_group - overhead; 15367f511862STheodore Ts'o if (ext4_has_group_desc_csum(sb)) { 153719c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_BLOCK_UNINIT | 153819c5246dSYongqiang Yang EXT4_BG_INODE_UNINIT; 15397f511862STheodore Ts'o if (!test_opt(sb, INIT_INODE_TABLE)) 15407f511862STheodore Ts'o flex_gd->bg_flags[i] |= EXT4_BG_INODE_ZEROED; 15417f511862STheodore Ts'o } else 154219c5246dSYongqiang Yang flex_gd->bg_flags[i] = EXT4_BG_INODE_ZEROED; 154319c5246dSYongqiang Yang } 154419c5246dSYongqiang Yang 1545feb0ab32SDarrick J. Wong if (last_group == n_group && ext4_has_group_desc_csum(sb)) 154619c5246dSYongqiang Yang /* We need to initialize block bitmap of last group. */ 154719c5246dSYongqiang Yang flex_gd->bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT; 154819c5246dSYongqiang Yang 154919c5246dSYongqiang Yang if ((last_group == n_group) && (last != blocks_per_group - 1)) { 155019c5246dSYongqiang Yang group_data[i - 1].blocks_count = last + 1; 155119c5246dSYongqiang Yang group_data[i - 1].free_blocks_count -= blocks_per_group- 155219c5246dSYongqiang Yang last - 1; 155319c5246dSYongqiang Yang } 155419c5246dSYongqiang Yang 155519c5246dSYongqiang Yang return 1; 155619c5246dSYongqiang Yang } 155719c5246dSYongqiang Yang 1558ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 1559ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 1560ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 1561ac27a0ecSDave Kleikamp * write the data to disk. 1562ac27a0ecSDave Kleikamp * 1563ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 1564ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 1565ac27a0ecSDave Kleikamp * 1566ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 1567ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 1568ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 1569ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 1570ac27a0ecSDave Kleikamp */ 1571617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 1572ac27a0ecSDave Kleikamp { 157361f296ccSYongqiang Yang struct ext4_new_flex_group_data flex_gd; 1574617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1575617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 1576617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 1577ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 1578ac27a0ecSDave Kleikamp struct inode *inode = NULL; 157903b40e34SJon Ernst int gdb_off; 158061f296ccSYongqiang Yang int err; 158161f296ccSYongqiang Yang __u16 bg_flags = 0; 1582ac27a0ecSDave Kleikamp 1583617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 1584ac27a0ecSDave Kleikamp 1585617ba13bSMingming Cao if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb, 1586617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 158712062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 1588ac27a0ecSDave Kleikamp return -EPERM; 1589ac27a0ecSDave Kleikamp } 1590ac27a0ecSDave Kleikamp 1591bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 1592bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 159312062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1594ac27a0ecSDave Kleikamp return -EINVAL; 1595ac27a0ecSDave Kleikamp } 1596ac27a0ecSDave Kleikamp 1597617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 1598ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 159912062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 1600ac27a0ecSDave Kleikamp return -EINVAL; 1601ac27a0ecSDave Kleikamp } 1602ac27a0ecSDave Kleikamp 1603ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 1604617ba13bSMingming Cao if (!EXT4_HAS_COMPAT_FEATURE(sb, 160537609fd5SJosef Bacik EXT4_FEATURE_COMPAT_RESIZE_INODE) 160637609fd5SJosef Bacik || !le16_to_cpu(es->s_reserved_gdt_blocks)) { 160712062dddSEric Sandeen ext4_warning(sb, 1608ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 1609ac27a0ecSDave Kleikamp return -EPERM; 1610ac27a0ecSDave Kleikamp } 16111d1fe1eeSDavid Howells inode = ext4_iget(sb, EXT4_RESIZE_INO); 16121d1fe1eeSDavid Howells if (IS_ERR(inode)) { 161312062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 16141d1fe1eeSDavid Howells return PTR_ERR(inode); 1615ac27a0ecSDave Kleikamp } 1616ac27a0ecSDave Kleikamp } 1617ac27a0ecSDave Kleikamp 1618920313a7SAneesh Kumar K.V 161961f296ccSYongqiang Yang err = verify_group_input(sb, input); 1620668f4dc5SYongqiang Yang if (err) 162161f296ccSYongqiang Yang goto out; 1622ac27a0ecSDave Kleikamp 1623117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, input->group + 1); 1624117fff10STheodore Ts'o if (err) 16257f511862STheodore Ts'o goto out; 1626117fff10STheodore Ts'o 162728623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, input->group + 1); 162828623c2fSTheodore Ts'o if (err) 162928623c2fSTheodore Ts'o goto out; 163028623c2fSTheodore Ts'o 163161f296ccSYongqiang Yang flex_gd.count = 1; 163261f296ccSYongqiang Yang flex_gd.groups = input; 163361f296ccSYongqiang Yang flex_gd.bg_flags = &bg_flags; 163461f296ccSYongqiang Yang err = ext4_flex_group_add(sb, inode, &flex_gd); 163561f296ccSYongqiang Yang out: 1636ac27a0ecSDave Kleikamp iput(inode); 1637ac27a0ecSDave Kleikamp return err; 1638617ba13bSMingming Cao } /* ext4_group_add */ 1639ac27a0ecSDave Kleikamp 16402b2d6d01STheodore Ts'o /* 164118e31438SYongqiang Yang * extend a group without checking assuming that checking has been done. 164218e31438SYongqiang Yang */ 164318e31438SYongqiang Yang static int ext4_group_extend_no_check(struct super_block *sb, 164418e31438SYongqiang Yang ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) 164518e31438SYongqiang Yang { 164618e31438SYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 164718e31438SYongqiang Yang handle_t *handle; 164818e31438SYongqiang Yang int err = 0, err2; 164918e31438SYongqiang Yang 165018e31438SYongqiang Yang /* We will update the superblock, one block bitmap, and 165118e31438SYongqiang Yang * one group descriptor via ext4_group_add_blocks(). 165218e31438SYongqiang Yang */ 16539924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, 3); 165418e31438SYongqiang Yang if (IS_ERR(handle)) { 165518e31438SYongqiang Yang err = PTR_ERR(handle); 165618e31438SYongqiang Yang ext4_warning(sb, "error %d on journal start", err); 165718e31438SYongqiang Yang return err; 165818e31438SYongqiang Yang } 165918e31438SYongqiang Yang 16605d601255Sliang xie BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 166118e31438SYongqiang Yang err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 166218e31438SYongqiang Yang if (err) { 166318e31438SYongqiang Yang ext4_warning(sb, "error %d on journal write access", err); 166418e31438SYongqiang Yang goto errout; 166518e31438SYongqiang Yang } 166618e31438SYongqiang Yang 166718e31438SYongqiang Yang ext4_blocks_count_set(es, o_blocks_count + add); 1668636d7e2eSDarrick J. Wong ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add); 166918e31438SYongqiang Yang ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 167018e31438SYongqiang Yang o_blocks_count + add); 167118e31438SYongqiang Yang /* We add the blocks to the bitmap and set the group need init bit */ 167218e31438SYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 167318e31438SYongqiang Yang if (err) 167418e31438SYongqiang Yang goto errout; 167518e31438SYongqiang Yang ext4_handle_dirty_super(handle, sb); 167618e31438SYongqiang Yang ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 167718e31438SYongqiang Yang o_blocks_count + add); 167818e31438SYongqiang Yang errout: 167918e31438SYongqiang Yang err2 = ext4_journal_stop(handle); 168018e31438SYongqiang Yang if (err2 && !err) 168118e31438SYongqiang Yang err = err2; 168218e31438SYongqiang Yang 168318e31438SYongqiang Yang if (!err) { 168418e31438SYongqiang Yang if (test_opt(sb, DEBUG)) 168518e31438SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extended group to %llu " 168618e31438SYongqiang Yang "blocks\n", ext4_blocks_count(es)); 16876ca792edSMaarten ter Huurne update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, 168801f795f9SYongqiang Yang (char *)es, sizeof(struct ext4_super_block), 0); 168918e31438SYongqiang Yang } 169018e31438SYongqiang Yang return err; 169118e31438SYongqiang Yang } 169218e31438SYongqiang Yang 169318e31438SYongqiang Yang /* 16942b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 1695ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 1696ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 1697ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 1698ac27a0ecSDave Kleikamp * 1699617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 1700ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 1701ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 1702ac27a0ecSDave Kleikamp */ 1703617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 1704617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 1705ac27a0ecSDave Kleikamp { 1706617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 1707617ba13bSMingming Cao ext4_grpblk_t last; 1708617ba13bSMingming Cao ext4_grpblk_t add; 1709ac27a0ecSDave Kleikamp struct buffer_head *bh; 1710d89651c8SYongqiang Yang int err; 17115f21b0e6SFrederic Bohe ext4_group_t group; 1712ac27a0ecSDave Kleikamp 1713bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 1714ac27a0ecSDave Kleikamp 1715ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 171692b97816STheodore Ts'o ext4_msg(sb, KERN_DEBUG, 171792b97816STheodore Ts'o "extending last group from %llu to %llu blocks", 1718ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 1719ac27a0ecSDave Kleikamp 1720ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 1721ac27a0ecSDave Kleikamp return 0; 1722ac27a0ecSDave Kleikamp 1723ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 172492b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, 172592b97816STheodore Ts'o "filesystem too large to resize to %llu blocks safely", 172692b97816STheodore Ts'o n_blocks_count); 1727ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 172812062dddSEric Sandeen ext4_warning(sb, "CONFIG_LBDAF not enabled"); 1729ac27a0ecSDave Kleikamp return -EINVAL; 1730ac27a0ecSDave Kleikamp } 1731ac27a0ecSDave Kleikamp 1732ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 173312062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 17348f82f840SYongqiang Yang return -EINVAL; 1735ac27a0ecSDave Kleikamp } 1736ac27a0ecSDave Kleikamp 1737ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 17385f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1739ac27a0ecSDave Kleikamp 1740ac27a0ecSDave Kleikamp if (last == 0) { 174112062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1742ac27a0ecSDave Kleikamp return -EPERM; 1743ac27a0ecSDave Kleikamp } 1744ac27a0ecSDave Kleikamp 1745617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1746ac27a0ecSDave Kleikamp 1747ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 174812062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1749ac27a0ecSDave Kleikamp return -EINVAL; 1750ac27a0ecSDave Kleikamp } 1751ac27a0ecSDave Kleikamp 1752ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1753ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1754ac27a0ecSDave Kleikamp 1755ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 175612062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1757ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1758ac27a0ecSDave Kleikamp 1759ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 1760ac27a0ecSDave Kleikamp bh = sb_bread(sb, o_blocks_count + add - 1); 1761ac27a0ecSDave Kleikamp if (!bh) { 176212062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1763ac27a0ecSDave Kleikamp return -ENOSPC; 1764ac27a0ecSDave Kleikamp } 1765ac27a0ecSDave Kleikamp brelse(bh); 1766ac27a0ecSDave Kleikamp 1767d89651c8SYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 1768ac27a0ecSDave Kleikamp return err; 1769617ba13bSMingming Cao } /* ext4_group_extend */ 177019c5246dSYongqiang Yang 17711c6bd717STheodore Ts'o 17721c6bd717STheodore Ts'o static int num_desc_blocks(struct super_block *sb, ext4_group_t groups) 17731c6bd717STheodore Ts'o { 17741c6bd717STheodore Ts'o return (groups + EXT4_DESC_PER_BLOCK(sb) - 1) / EXT4_DESC_PER_BLOCK(sb); 17751c6bd717STheodore Ts'o } 17761c6bd717STheodore Ts'o 17771c6bd717STheodore Ts'o /* 17781c6bd717STheodore Ts'o * Release the resize inode and drop the resize_inode feature if there 17791c6bd717STheodore Ts'o * are no more reserved gdt blocks, and then convert the file system 17801c6bd717STheodore Ts'o * to enable meta_bg 17811c6bd717STheodore Ts'o */ 17821c6bd717STheodore Ts'o static int ext4_convert_meta_bg(struct super_block *sb, struct inode *inode) 17831c6bd717STheodore Ts'o { 17841c6bd717STheodore Ts'o handle_t *handle; 17851c6bd717STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 17861c6bd717STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 178759e31c15STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 17881c6bd717STheodore Ts'o ext4_fsblk_t nr; 17891c6bd717STheodore Ts'o int i, ret, err = 0; 17901c6bd717STheodore Ts'o int credits = 1; 17911c6bd717STheodore Ts'o 17921c6bd717STheodore Ts'o ext4_msg(sb, KERN_INFO, "Converting file system to meta_bg"); 179359e31c15STheodore Ts'o if (inode) { 17941c6bd717STheodore Ts'o if (es->s_reserved_gdt_blocks) { 17951c6bd717STheodore Ts'o ext4_error(sb, "Unexpected non-zero " 17961c6bd717STheodore Ts'o "s_reserved_gdt_blocks"); 17971c6bd717STheodore Ts'o return -EPERM; 17981c6bd717STheodore Ts'o } 17991c6bd717STheodore Ts'o 18001c6bd717STheodore Ts'o /* Do a quick sanity check of the resize inode */ 18011c6bd717STheodore Ts'o if (inode->i_blocks != 1 << (inode->i_blkbits - 9)) 18021c6bd717STheodore Ts'o goto invalid_resize_inode; 18031c6bd717STheodore Ts'o for (i = 0; i < EXT4_N_BLOCKS; i++) { 18041c6bd717STheodore Ts'o if (i == EXT4_DIND_BLOCK) { 18051c6bd717STheodore Ts'o if (ei->i_data[i]) 18061c6bd717STheodore Ts'o continue; 18071c6bd717STheodore Ts'o else 18081c6bd717STheodore Ts'o goto invalid_resize_inode; 18091c6bd717STheodore Ts'o } 18101c6bd717STheodore Ts'o if (ei->i_data[i]) 18111c6bd717STheodore Ts'o goto invalid_resize_inode; 18121c6bd717STheodore Ts'o } 18131c6bd717STheodore Ts'o credits += 3; /* block bitmap, bg descriptor, resize inode */ 18141c6bd717STheodore Ts'o } 18151c6bd717STheodore Ts'o 18169924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credits); 18171c6bd717STheodore Ts'o if (IS_ERR(handle)) 18181c6bd717STheodore Ts'o return PTR_ERR(handle); 18191c6bd717STheodore Ts'o 18205d601255Sliang xie BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 18211c6bd717STheodore Ts'o err = ext4_journal_get_write_access(handle, sbi->s_sbh); 18221c6bd717STheodore Ts'o if (err) 18231c6bd717STheodore Ts'o goto errout; 18241c6bd717STheodore Ts'o 18251c6bd717STheodore Ts'o EXT4_CLEAR_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_RESIZE_INODE); 18261c6bd717STheodore Ts'o EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG); 18271c6bd717STheodore Ts'o sbi->s_es->s_first_meta_bg = 18281c6bd717STheodore Ts'o cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count)); 18291c6bd717STheodore Ts'o 18301c6bd717STheodore Ts'o err = ext4_handle_dirty_super(handle, sb); 18311c6bd717STheodore Ts'o if (err) { 18321c6bd717STheodore Ts'o ext4_std_error(sb, err); 18331c6bd717STheodore Ts'o goto errout; 18341c6bd717STheodore Ts'o } 18351c6bd717STheodore Ts'o 18361c6bd717STheodore Ts'o if (inode) { 18371c6bd717STheodore Ts'o nr = le32_to_cpu(ei->i_data[EXT4_DIND_BLOCK]); 18381c6bd717STheodore Ts'o ext4_free_blocks(handle, inode, NULL, nr, 1, 18391c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | 18401c6bd717STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 18411c6bd717STheodore Ts'o ei->i_data[EXT4_DIND_BLOCK] = 0; 18421c6bd717STheodore Ts'o inode->i_blocks = 0; 18431c6bd717STheodore Ts'o 18441c6bd717STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 18451c6bd717STheodore Ts'o if (err) 18461c6bd717STheodore Ts'o ext4_std_error(sb, err); 18471c6bd717STheodore Ts'o } 18481c6bd717STheodore Ts'o 18491c6bd717STheodore Ts'o errout: 18501c6bd717STheodore Ts'o ret = ext4_journal_stop(handle); 18511c6bd717STheodore Ts'o if (!err) 18521c6bd717STheodore Ts'o err = ret; 18531c6bd717STheodore Ts'o return ret; 18541c6bd717STheodore Ts'o 18551c6bd717STheodore Ts'o invalid_resize_inode: 18561c6bd717STheodore Ts'o ext4_error(sb, "corrupted/inconsistent resize inode"); 18571c6bd717STheodore Ts'o return -EINVAL; 18581c6bd717STheodore Ts'o } 18591c6bd717STheodore Ts'o 186019c5246dSYongqiang Yang /* 186119c5246dSYongqiang Yang * ext4_resize_fs() resizes a fs to new size specified by @n_blocks_count 186219c5246dSYongqiang Yang * 186319c5246dSYongqiang Yang * @sb: super block of the fs to be resized 186419c5246dSYongqiang Yang * @n_blocks_count: the number of blocks resides in the resized fs 186519c5246dSYongqiang Yang */ 186619c5246dSYongqiang Yang int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count) 186719c5246dSYongqiang Yang { 186819c5246dSYongqiang Yang struct ext4_new_flex_group_data *flex_gd = NULL; 186919c5246dSYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 187019c5246dSYongqiang Yang struct ext4_super_block *es = sbi->s_es; 187119c5246dSYongqiang Yang struct buffer_head *bh; 187201f795f9SYongqiang Yang struct inode *resize_inode = NULL; 187301f795f9SYongqiang Yang ext4_grpblk_t add, offset; 187419c5246dSYongqiang Yang unsigned long n_desc_blocks; 187519c5246dSYongqiang Yang unsigned long o_desc_blocks; 187601f795f9SYongqiang Yang ext4_group_t o_group; 187701f795f9SYongqiang Yang ext4_group_t n_group; 187801f795f9SYongqiang Yang ext4_fsblk_t o_blocks_count; 18791c6bd717STheodore Ts'o ext4_fsblk_t n_blocks_count_retry = 0; 18804da4a56eSTheodore Ts'o unsigned long last_update_time = 0; 1881117fff10STheodore Ts'o int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex; 188201f795f9SYongqiang Yang int meta_bg; 188319c5246dSYongqiang Yang 188459e31c15STheodore Ts'o /* See if the device is actually as big as what was requested */ 188559e31c15STheodore Ts'o bh = sb_bread(sb, n_blocks_count - 1); 188659e31c15STheodore Ts'o if (!bh) { 188759e31c15STheodore Ts'o ext4_warning(sb, "can't read last block, resize aborted"); 188859e31c15STheodore Ts'o return -ENOSPC; 188959e31c15STheodore Ts'o } 189059e31c15STheodore Ts'o brelse(bh); 189159e31c15STheodore Ts'o 18921c6bd717STheodore Ts'o retry: 189319c5246dSYongqiang Yang o_blocks_count = ext4_blocks_count(es); 189419c5246dSYongqiang Yang 189559e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resizing filesystem from %llu " 189692b97816STheodore Ts'o "to %llu blocks", o_blocks_count, n_blocks_count); 189719c5246dSYongqiang Yang 189819c5246dSYongqiang Yang if (n_blocks_count < o_blocks_count) { 189919c5246dSYongqiang Yang /* On-line shrinking not supported */ 190019c5246dSYongqiang Yang ext4_warning(sb, "can't shrink FS - resize aborted"); 190119c5246dSYongqiang Yang return -EINVAL; 190219c5246dSYongqiang Yang } 190319c5246dSYongqiang Yang 190419c5246dSYongqiang Yang if (n_blocks_count == o_blocks_count) 190519c5246dSYongqiang Yang /* Nothing need to do */ 190619c5246dSYongqiang Yang return 0; 190719c5246dSYongqiang Yang 1908bd86298eSLukas Czerner n_group = ext4_get_group_number(sb, n_blocks_count - 1); 19093f8a6411STheodore Ts'o if (n_group > (0xFFFFFFFFUL / EXT4_INODES_PER_GROUP(sb))) { 19103f8a6411STheodore Ts'o ext4_warning(sb, "resize would cause inodes_count overflow"); 19113f8a6411STheodore Ts'o return -EINVAL; 19123f8a6411STheodore Ts'o } 1913a0ade1deSLukas Czerner ext4_get_group_no_and_offset(sb, o_blocks_count - 1, &o_group, &offset); 191419c5246dSYongqiang Yang 19151c6bd717STheodore Ts'o n_desc_blocks = num_desc_blocks(sb, n_group + 1); 19161c6bd717STheodore Ts'o o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count); 191719c5246dSYongqiang Yang 191801f795f9SYongqiang Yang meta_bg = EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG); 191901f795f9SYongqiang Yang 192001f795f9SYongqiang Yang if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_RESIZE_INODE)) { 192101f795f9SYongqiang Yang if (meta_bg) { 192201f795f9SYongqiang Yang ext4_error(sb, "resize_inode and meta_bg enabled " 192301f795f9SYongqiang Yang "simultaneously"); 192401f795f9SYongqiang Yang return -EINVAL; 192501f795f9SYongqiang Yang } 19261c6bd717STheodore Ts'o if (n_desc_blocks > o_desc_blocks + 19271c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks)) { 19281c6bd717STheodore Ts'o n_blocks_count_retry = n_blocks_count; 19291c6bd717STheodore Ts'o n_desc_blocks = o_desc_blocks + 19301c6bd717STheodore Ts'o le16_to_cpu(es->s_reserved_gdt_blocks); 19311c6bd717STheodore Ts'o n_group = n_desc_blocks * EXT4_DESC_PER_BLOCK(sb); 19321c6bd717STheodore Ts'o n_blocks_count = n_group * EXT4_BLOCKS_PER_GROUP(sb); 19331c6bd717STheodore Ts'o n_group--; /* set to last group number */ 193419c5246dSYongqiang Yang } 19351c6bd717STheodore Ts'o 19361c6bd717STheodore Ts'o if (!resize_inode) 193719c5246dSYongqiang Yang resize_inode = ext4_iget(sb, EXT4_RESIZE_INO); 193819c5246dSYongqiang Yang if (IS_ERR(resize_inode)) { 193919c5246dSYongqiang Yang ext4_warning(sb, "Error opening resize inode"); 194019c5246dSYongqiang Yang return PTR_ERR(resize_inode); 194119c5246dSYongqiang Yang } 19421c6bd717STheodore Ts'o } 19431c6bd717STheodore Ts'o 194459e31c15STheodore Ts'o if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) { 19451c6bd717STheodore Ts'o err = ext4_convert_meta_bg(sb, resize_inode); 19461c6bd717STheodore Ts'o if (err) 19471c6bd717STheodore Ts'o goto out; 19481c6bd717STheodore Ts'o if (resize_inode) { 19491c6bd717STheodore Ts'o iput(resize_inode); 19501c6bd717STheodore Ts'o resize_inode = NULL; 19511c6bd717STheodore Ts'o } 19521c6bd717STheodore Ts'o if (n_blocks_count_retry) { 19531c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 19541c6bd717STheodore Ts'o n_blocks_count_retry = 0; 19551c6bd717STheodore Ts'o goto retry; 19561c6bd717STheodore Ts'o } 195701f795f9SYongqiang Yang } 195819c5246dSYongqiang Yang 195919c5246dSYongqiang Yang /* extend the last group */ 1960a0ade1deSLukas Czerner if (n_group == o_group) 1961a0ade1deSLukas Czerner add = n_blocks_count - o_blocks_count; 1962a0ade1deSLukas Czerner else 1963a0ade1deSLukas Czerner add = EXT4_BLOCKS_PER_GROUP(sb) - (offset + 1); 1964a0ade1deSLukas Czerner if (add > 0) { 196519c5246dSYongqiang Yang err = ext4_group_extend_no_check(sb, o_blocks_count, add); 196619c5246dSYongqiang Yang if (err) 196719c5246dSYongqiang Yang goto out; 196819c5246dSYongqiang Yang } 196919c5246dSYongqiang Yang 1970d7574ad0SYongqiang Yang if (ext4_blocks_count(es) == n_blocks_count) 197119c5246dSYongqiang Yang goto out; 197219c5246dSYongqiang Yang 1973117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, n_group + 1); 1974117fff10STheodore Ts'o if (err) 1975117fff10STheodore Ts'o return err; 1976117fff10STheodore Ts'o 197728623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, n_group + 1); 197828623c2fSTheodore Ts'o if (err) 197928623c2fSTheodore Ts'o goto out; 198028623c2fSTheodore Ts'o 198119c5246dSYongqiang Yang flex_gd = alloc_flex_gd(flexbg_size); 198219c5246dSYongqiang Yang if (flex_gd == NULL) { 198319c5246dSYongqiang Yang err = -ENOMEM; 198419c5246dSYongqiang Yang goto out; 198519c5246dSYongqiang Yang } 198619c5246dSYongqiang Yang 198719c5246dSYongqiang Yang /* Add flex groups. Note that a regular group is a 198819c5246dSYongqiang Yang * flex group with 1 group. 198919c5246dSYongqiang Yang */ 199019c5246dSYongqiang Yang while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count, 199119c5246dSYongqiang Yang flexbg_size)) { 19924da4a56eSTheodore Ts'o if (jiffies - last_update_time > HZ * 10) { 19934da4a56eSTheodore Ts'o if (last_update_time) 19944da4a56eSTheodore Ts'o ext4_msg(sb, KERN_INFO, 19954da4a56eSTheodore Ts'o "resized to %llu blocks", 19964da4a56eSTheodore Ts'o ext4_blocks_count(es)); 19974da4a56eSTheodore Ts'o last_update_time = jiffies; 19984da4a56eSTheodore Ts'o } 199903c1c290SYongqiang Yang if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0) 200003c1c290SYongqiang Yang break; 200119c5246dSYongqiang Yang err = ext4_flex_group_add(sb, resize_inode, flex_gd); 200219c5246dSYongqiang Yang if (unlikely(err)) 200319c5246dSYongqiang Yang break; 200419c5246dSYongqiang Yang } 200519c5246dSYongqiang Yang 20061c6bd717STheodore Ts'o if (!err && n_blocks_count_retry) { 20071c6bd717STheodore Ts'o n_blocks_count = n_blocks_count_retry; 20081c6bd717STheodore Ts'o n_blocks_count_retry = 0; 20091c6bd717STheodore Ts'o free_flex_gd(flex_gd); 20101c6bd717STheodore Ts'o flex_gd = NULL; 20111c6bd717STheodore Ts'o goto retry; 20121c6bd717STheodore Ts'o } 20131c6bd717STheodore Ts'o 201419c5246dSYongqiang Yang out: 201519c5246dSYongqiang Yang if (flex_gd) 201619c5246dSYongqiang Yang free_flex_gd(flex_gd); 201701f795f9SYongqiang Yang if (resize_inode != NULL) 201819c5246dSYongqiang Yang iput(resize_inode); 201959e31c15STheodore Ts'o ext4_msg(sb, KERN_INFO, "resized filesystem to %llu", n_blocks_count); 202019c5246dSYongqiang Yang return err; 202119c5246dSYongqiang Yang } 2022