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 /* 27ce723c31SYongqiang Yang * We are not allowed to do online-resizing on a filesystem mounted 28ce723c31SYongqiang Yang * with error, because it can destroy the filesystem easily. 29ce723c31SYongqiang Yang */ 30ce723c31SYongqiang Yang if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 31ce723c31SYongqiang Yang ext4_warning(sb, "There are errors in the filesystem, " 32ce723c31SYongqiang Yang "so online resizing is not allowed\n"); 33ce723c31SYongqiang Yang return -EPERM; 34ce723c31SYongqiang Yang } 35ce723c31SYongqiang Yang 368f82f840SYongqiang Yang if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags)) 378f82f840SYongqiang Yang ret = -EBUSY; 388f82f840SYongqiang Yang 398f82f840SYongqiang Yang return ret; 408f82f840SYongqiang Yang } 418f82f840SYongqiang Yang 428f82f840SYongqiang Yang void ext4_resize_end(struct super_block *sb) 438f82f840SYongqiang Yang { 448f82f840SYongqiang Yang clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags); 458f82f840SYongqiang Yang smp_mb__after_clear_bit(); 468f82f840SYongqiang Yang } 478f82f840SYongqiang Yang 48ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 49ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 50ac27a0ecSDave Kleikamp 51ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 52617ba13bSMingming Cao struct ext4_new_group_data *input) 53ac27a0ecSDave Kleikamp { 54617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 55617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 56bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 57617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 58fd2d4291SAvantika Mathur ext4_group_t group = input->group; 59617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 60617ba13bSMingming Cao unsigned overhead = ext4_bg_has_super(sb, group) ? 61617ba13bSMingming Cao (1 + ext4_bg_num_gdb(sb, group) + 62ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks)) : 0; 63617ba13bSMingming Cao ext4_fsblk_t metaend = start + overhead; 64ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 653a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 66ac27a0ecSDave Kleikamp int err = -EINVAL; 67ac27a0ecSDave Kleikamp 68ac27a0ecSDave Kleikamp input->free_blocks_count = free_blocks_count = 69ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 70ac27a0ecSDave Kleikamp 71ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 72617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 73ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 74617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 75ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 76ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 77ac27a0ecSDave Kleikamp 783a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 79ac27a0ecSDave Kleikamp if (group != sbi->s_groups_count) 8012062dddSEric Sandeen ext4_warning(sb, "Cannot add at group %u (only %u groups)", 81ac27a0ecSDave Kleikamp input->group, sbi->s_groups_count); 823a5b2ecdSMingming Cao else if (offset != 0) 8312062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 84ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 8512062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 86ac27a0ecSDave Kleikamp input->reserved_blocks); 87ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 8812062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 89ac27a0ecSDave Kleikamp input->blocks_count); 90ac27a0ecSDave Kleikamp else if (!(bh = sb_bread(sb, end - 1))) 9112062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 92ac27a0ecSDave Kleikamp end - 1); 93ac27a0ecSDave Kleikamp else if (outside(input->block_bitmap, start, end)) 9412062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 951939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 96ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 9712062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 981939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 99ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 100ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 10112062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1021939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 103ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 10412062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1051939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 106ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 10712062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 10812062dddSEric Sandeen "(%llu-%llu)", 1091939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1101939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 111ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 11212062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 11312062dddSEric Sandeen "(%llu-%llu)", 1141939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1151939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 116ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 11712062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 1181939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1191939e49aSRandy Dunlap start, metaend - 1); 120ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 12112062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 1221939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1231939e49aSRandy Dunlap start, metaend - 1); 124ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 125ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 12612062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 12712062dddSEric Sandeen "(%llu-%llu)", 1281939e49aSRandy Dunlap (unsigned long long)input->inode_table, 1291939e49aSRandy Dunlap itend - 1, start, metaend - 1); 130ac27a0ecSDave Kleikamp else 131ac27a0ecSDave Kleikamp err = 0; 132ac27a0ecSDave Kleikamp brelse(bh); 133ac27a0ecSDave Kleikamp 134ac27a0ecSDave Kleikamp return err; 135ac27a0ecSDave Kleikamp } 136ac27a0ecSDave Kleikamp 137*28c7bac0SYongqiang Yang /* 138*28c7bac0SYongqiang Yang * ext4_new_flex_group_data is used by 64bit-resize interface to add a flex 139*28c7bac0SYongqiang Yang * group each time. 140*28c7bac0SYongqiang Yang */ 141*28c7bac0SYongqiang Yang struct ext4_new_flex_group_data { 142*28c7bac0SYongqiang Yang struct ext4_new_group_data *groups; /* new_group_data for groups 143*28c7bac0SYongqiang Yang in the flex group */ 144*28c7bac0SYongqiang Yang __u16 *bg_flags; /* block group flags of groups 145*28c7bac0SYongqiang Yang in @groups */ 146*28c7bac0SYongqiang Yang ext4_group_t count; /* number of groups in @groups 147*28c7bac0SYongqiang Yang */ 148*28c7bac0SYongqiang Yang }; 149*28c7bac0SYongqiang Yang 150*28c7bac0SYongqiang Yang /* 151*28c7bac0SYongqiang Yang * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of 152*28c7bac0SYongqiang Yang * @flexbg_size. 153*28c7bac0SYongqiang Yang * 154*28c7bac0SYongqiang Yang * Returns NULL on failure otherwise address of the allocated structure. 155*28c7bac0SYongqiang Yang */ 156*28c7bac0SYongqiang Yang static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) 157*28c7bac0SYongqiang Yang { 158*28c7bac0SYongqiang Yang struct ext4_new_flex_group_data *flex_gd; 159*28c7bac0SYongqiang Yang 160*28c7bac0SYongqiang Yang flex_gd = kmalloc(sizeof(*flex_gd), GFP_NOFS); 161*28c7bac0SYongqiang Yang if (flex_gd == NULL) 162*28c7bac0SYongqiang Yang goto out3; 163*28c7bac0SYongqiang Yang 164*28c7bac0SYongqiang Yang flex_gd->count = flexbg_size; 165*28c7bac0SYongqiang Yang 166*28c7bac0SYongqiang Yang flex_gd->groups = kmalloc(sizeof(struct ext4_new_group_data) * 167*28c7bac0SYongqiang Yang flexbg_size, GFP_NOFS); 168*28c7bac0SYongqiang Yang if (flex_gd->groups == NULL) 169*28c7bac0SYongqiang Yang goto out2; 170*28c7bac0SYongqiang Yang 171*28c7bac0SYongqiang Yang flex_gd->bg_flags = kmalloc(flexbg_size * sizeof(__u16), GFP_NOFS); 172*28c7bac0SYongqiang Yang if (flex_gd->bg_flags == NULL) 173*28c7bac0SYongqiang Yang goto out1; 174*28c7bac0SYongqiang Yang 175*28c7bac0SYongqiang Yang return flex_gd; 176*28c7bac0SYongqiang Yang 177*28c7bac0SYongqiang Yang out1: 178*28c7bac0SYongqiang Yang kfree(flex_gd->groups); 179*28c7bac0SYongqiang Yang out2: 180*28c7bac0SYongqiang Yang kfree(flex_gd); 181*28c7bac0SYongqiang Yang out3: 182*28c7bac0SYongqiang Yang return NULL; 183*28c7bac0SYongqiang Yang } 184*28c7bac0SYongqiang Yang 185*28c7bac0SYongqiang Yang static void free_flex_gd(struct ext4_new_flex_group_data *flex_gd) 186*28c7bac0SYongqiang Yang { 187*28c7bac0SYongqiang Yang kfree(flex_gd->bg_flags); 188*28c7bac0SYongqiang Yang kfree(flex_gd->groups); 189*28c7bac0SYongqiang Yang kfree(flex_gd); 190*28c7bac0SYongqiang Yang } 191*28c7bac0SYongqiang Yang 192ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 193617ba13bSMingming Cao ext4_fsblk_t blk) 194ac27a0ecSDave Kleikamp { 195ac27a0ecSDave Kleikamp struct buffer_head *bh; 196ac27a0ecSDave Kleikamp int err; 197ac27a0ecSDave Kleikamp 198ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 199ac27a0ecSDave Kleikamp if (!bh) 200ac27a0ecSDave Kleikamp return ERR_PTR(-EIO); 201617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 202ac27a0ecSDave Kleikamp brelse(bh); 203ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 204ac27a0ecSDave Kleikamp } else { 205ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 206ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 207ac27a0ecSDave Kleikamp } 208ac27a0ecSDave Kleikamp 209ac27a0ecSDave Kleikamp return bh; 210ac27a0ecSDave Kleikamp } 211ac27a0ecSDave Kleikamp 212ac27a0ecSDave Kleikamp /* 21314904107SEric Sandeen * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA. 21414904107SEric Sandeen * If that fails, restart the transaction & regain write access for the 21514904107SEric Sandeen * buffer head which is used for block_bitmap modifications. 21614904107SEric Sandeen */ 2176d40bc5aSYongqiang Yang static int extend_or_restart_transaction(handle_t *handle, int thresh) 21814904107SEric Sandeen { 21914904107SEric Sandeen int err; 22014904107SEric Sandeen 2210390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, thresh)) 22214904107SEric Sandeen return 0; 22314904107SEric Sandeen 22414904107SEric Sandeen err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA); 22514904107SEric Sandeen if (err < 0) 22614904107SEric Sandeen return err; 22714904107SEric Sandeen if (err) { 2286d40bc5aSYongqiang Yang err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA); 2296d40bc5aSYongqiang Yang if (err) 23014904107SEric Sandeen return err; 23114904107SEric Sandeen } 23214904107SEric Sandeen 23314904107SEric Sandeen return 0; 23414904107SEric Sandeen } 23514904107SEric Sandeen 23614904107SEric Sandeen /* 237ac27a0ecSDave Kleikamp * Set up the block and inode bitmaps, and the inode table for the new group. 238ac27a0ecSDave Kleikamp * This doesn't need to be part of the main transaction, since we are only 239ac27a0ecSDave Kleikamp * changing blocks outside the actual filesystem. We still do journaling to 240ac27a0ecSDave Kleikamp * ensure the recovery is correct in case of a failure just after resize. 241ac27a0ecSDave Kleikamp * If any part of this fails, we simply abort the resize. 242ac27a0ecSDave Kleikamp */ 243ac27a0ecSDave Kleikamp static int setup_new_group_blocks(struct super_block *sb, 244617ba13bSMingming Cao struct ext4_new_group_data *input) 245ac27a0ecSDave Kleikamp { 246617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 247617ba13bSMingming Cao ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group); 248617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 249ac27a0ecSDave Kleikamp le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0; 250617ba13bSMingming Cao unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group); 251ac27a0ecSDave Kleikamp struct buffer_head *bh; 252ac27a0ecSDave Kleikamp handle_t *handle; 253617ba13bSMingming Cao ext4_fsblk_t block; 254617ba13bSMingming Cao ext4_grpblk_t bit; 255ac27a0ecSDave Kleikamp int i; 256ac27a0ecSDave Kleikamp int err = 0, err2; 257ac27a0ecSDave Kleikamp 25814904107SEric Sandeen /* This transaction may be extended/restarted along the way */ 25914904107SEric Sandeen handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 26014904107SEric Sandeen 261ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 262ac27a0ecSDave Kleikamp return PTR_ERR(handle); 263ac27a0ecSDave Kleikamp 2648f82f840SYongqiang Yang BUG_ON(input->group != sbi->s_groups_count); 265ac27a0ecSDave Kleikamp 266ac27a0ecSDave Kleikamp /* Copy all of the GDT blocks into the backup in this group */ 267ac27a0ecSDave Kleikamp for (i = 0, bit = 1, block = start + 1; 268ac27a0ecSDave Kleikamp i < gdblocks; i++, block++, bit++) { 269ac27a0ecSDave Kleikamp struct buffer_head *gdb; 270ac27a0ecSDave Kleikamp 271c549a95dSEric Sandeen ext4_debug("update backup group %#04llx (+%d)\n", block, bit); 2726d40bc5aSYongqiang Yang err = extend_or_restart_transaction(handle, 1); 2736d40bc5aSYongqiang Yang if (err) 2746d40bc5aSYongqiang Yang goto exit_journal; 27514904107SEric Sandeen 276ac27a0ecSDave Kleikamp gdb = sb_getblk(sb, block); 277ac27a0ecSDave Kleikamp if (!gdb) { 278ac27a0ecSDave Kleikamp err = -EIO; 2796d40bc5aSYongqiang Yang goto exit_journal; 280ac27a0ecSDave Kleikamp } 281617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, gdb))) { 282ac27a0ecSDave Kleikamp brelse(gdb); 2836d40bc5aSYongqiang Yang goto exit_journal; 284ac27a0ecSDave Kleikamp } 2855b615287SEric Sandeen memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size); 286ac27a0ecSDave Kleikamp set_buffer_uptodate(gdb); 287b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, gdb); 288b4097142STheodore Ts'o if (unlikely(err)) { 289b4097142STheodore Ts'o brelse(gdb); 2906d40bc5aSYongqiang Yang goto exit_journal; 291b4097142STheodore Ts'o } 292ac27a0ecSDave Kleikamp brelse(gdb); 293ac27a0ecSDave Kleikamp } 294ac27a0ecSDave Kleikamp 295ac27a0ecSDave Kleikamp /* Zero out all of the reserved backup group descriptor table blocks */ 296da488945STheodore Ts'o ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 297a31437b8SLukas Czerner block, sbi->s_itb_per_group); 298a31437b8SLukas Czerner err = sb_issue_zeroout(sb, gdblocks + start + 1, reserved_gdb, 299a107e5a3STheodore Ts'o GFP_NOFS); 300a31437b8SLukas Czerner if (err) 3016d40bc5aSYongqiang Yang goto exit_journal; 3026d40bc5aSYongqiang Yang 3036d40bc5aSYongqiang Yang err = extend_or_restart_transaction(handle, 2); 3046d40bc5aSYongqiang Yang if (err) 3056d40bc5aSYongqiang Yang goto exit_journal; 3066d40bc5aSYongqiang Yang 3076d40bc5aSYongqiang Yang bh = bclean(handle, sb, input->block_bitmap); 3086d40bc5aSYongqiang Yang if (IS_ERR(bh)) { 3096d40bc5aSYongqiang Yang err = PTR_ERR(bh); 3106d40bc5aSYongqiang Yang goto exit_journal; 3116d40bc5aSYongqiang Yang } 312c3e94d1dSYongqiang Yang 313c3e94d1dSYongqiang Yang if (ext4_bg_has_super(sb, input->group)) { 314c3e94d1dSYongqiang Yang ext4_debug("mark backup group tables %#04llx (+0)\n", start); 315c3e94d1dSYongqiang Yang ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb + 1); 316c3e94d1dSYongqiang Yang } 31714904107SEric Sandeen 318c549a95dSEric Sandeen ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap, 319ac27a0ecSDave Kleikamp input->block_bitmap - start); 320617ba13bSMingming Cao ext4_set_bit(input->block_bitmap - start, bh->b_data); 321c549a95dSEric Sandeen ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap, 322ac27a0ecSDave Kleikamp input->inode_bitmap - start); 323617ba13bSMingming Cao ext4_set_bit(input->inode_bitmap - start, bh->b_data); 324ac27a0ecSDave Kleikamp 325ac27a0ecSDave Kleikamp /* Zero out all of the inode table blocks */ 326a31437b8SLukas Czerner block = input->inode_table; 327da488945STheodore Ts'o ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 328a31437b8SLukas Czerner block, sbi->s_itb_per_group); 329a107e5a3STheodore Ts'o err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, GFP_NOFS); 330a31437b8SLukas Czerner if (err) 33114904107SEric Sandeen goto exit_bh; 332c3e94d1dSYongqiang Yang ext4_set_bits(bh->b_data, input->inode_table - start, 333c3e94d1dSYongqiang Yang sbi->s_itb_per_group); 33414904107SEric Sandeen 33514904107SEric Sandeen 33661d08673STheodore Ts'o ext4_mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8, 33761d08673STheodore Ts'o bh->b_data); 338b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 339b4097142STheodore Ts'o if (unlikely(err)) { 340b4097142STheodore Ts'o ext4_std_error(sb, err); 341b4097142STheodore Ts'o goto exit_bh; 342b4097142STheodore Ts'o } 343ac27a0ecSDave Kleikamp brelse(bh); 344ac27a0ecSDave Kleikamp /* Mark unused entries in inode bitmap used */ 345c549a95dSEric Sandeen ext4_debug("clear inode bitmap %#04llx (+%llu)\n", 346ac27a0ecSDave Kleikamp input->inode_bitmap, input->inode_bitmap - start); 347ac27a0ecSDave Kleikamp if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) { 348ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 349ac27a0ecSDave Kleikamp goto exit_journal; 350ac27a0ecSDave Kleikamp } 351ac27a0ecSDave Kleikamp 35261d08673STheodore Ts'o ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, 353ac27a0ecSDave Kleikamp bh->b_data); 354b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 355b4097142STheodore Ts'o if (unlikely(err)) 356b4097142STheodore Ts'o ext4_std_error(sb, err); 357ac27a0ecSDave Kleikamp exit_bh: 358ac27a0ecSDave Kleikamp brelse(bh); 359ac27a0ecSDave Kleikamp 360ac27a0ecSDave Kleikamp exit_journal: 361617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 362ac27a0ecSDave Kleikamp err = err2; 363ac27a0ecSDave Kleikamp 364ac27a0ecSDave Kleikamp return err; 365ac27a0ecSDave Kleikamp } 366ac27a0ecSDave Kleikamp 367ac27a0ecSDave Kleikamp /* 368ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 369617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 370ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 371ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 372ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 373ac27a0ecSDave Kleikamp */ 374617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 375ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 376ac27a0ecSDave Kleikamp { 377ac27a0ecSDave Kleikamp unsigned *min = three; 378ac27a0ecSDave Kleikamp int mult = 3; 379ac27a0ecSDave Kleikamp unsigned ret; 380ac27a0ecSDave Kleikamp 381617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 382617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 383ac27a0ecSDave Kleikamp ret = *min; 384ac27a0ecSDave Kleikamp *min += 1; 385ac27a0ecSDave Kleikamp return ret; 386ac27a0ecSDave Kleikamp } 387ac27a0ecSDave Kleikamp 388ac27a0ecSDave Kleikamp if (*five < *min) { 389ac27a0ecSDave Kleikamp min = five; 390ac27a0ecSDave Kleikamp mult = 5; 391ac27a0ecSDave Kleikamp } 392ac27a0ecSDave Kleikamp if (*seven < *min) { 393ac27a0ecSDave Kleikamp min = seven; 394ac27a0ecSDave Kleikamp mult = 7; 395ac27a0ecSDave Kleikamp } 396ac27a0ecSDave Kleikamp 397ac27a0ecSDave Kleikamp ret = *min; 398ac27a0ecSDave Kleikamp *min *= mult; 399ac27a0ecSDave Kleikamp 400ac27a0ecSDave Kleikamp return ret; 401ac27a0ecSDave Kleikamp } 402ac27a0ecSDave Kleikamp 403ac27a0ecSDave Kleikamp /* 404ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 405ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 406ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 407ac27a0ecSDave Kleikamp */ 408ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 409ac27a0ecSDave Kleikamp struct buffer_head *primary) 410ac27a0ecSDave Kleikamp { 411617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 412fd2d4291SAvantika Mathur const ext4_group_t end = EXT4_SB(sb)->s_groups_count; 413ac27a0ecSDave Kleikamp unsigned three = 1; 414ac27a0ecSDave Kleikamp unsigned five = 5; 415ac27a0ecSDave Kleikamp unsigned seven = 7; 416ac27a0ecSDave Kleikamp unsigned grp; 417ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 418ac27a0ecSDave Kleikamp int gdbackups = 0; 419ac27a0ecSDave Kleikamp 420617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 421bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 422bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 42312062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 4242ae02107SMingming Cao " missing grp %d (%llu)", 425ac27a0ecSDave Kleikamp blk, grp, 426bd81d8eeSLaurent Vivier grp * 427bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 428bd81d8eeSLaurent Vivier blk); 429ac27a0ecSDave Kleikamp return -EINVAL; 430ac27a0ecSDave Kleikamp } 431617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 432ac27a0ecSDave Kleikamp return -EFBIG; 433ac27a0ecSDave Kleikamp } 434ac27a0ecSDave Kleikamp 435ac27a0ecSDave Kleikamp return gdbackups; 436ac27a0ecSDave Kleikamp } 437ac27a0ecSDave Kleikamp 438ac27a0ecSDave Kleikamp /* 439ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 440ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 441ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 442ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 443ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 444ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 445ac27a0ecSDave Kleikamp * 446ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 447ac27a0ecSDave Kleikamp * 448ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 449ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 450ac27a0ecSDave Kleikamp */ 451ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 4522f919710SYongqiang Yang ext4_group_t group) 453ac27a0ecSDave Kleikamp { 454ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 455617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 4562f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 457617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 458ac27a0ecSDave Kleikamp struct buffer_head **o_group_desc, **n_group_desc; 459ac27a0ecSDave Kleikamp struct buffer_head *dind; 4602f919710SYongqiang Yang struct buffer_head *gdb_bh; 461ac27a0ecSDave Kleikamp int gdbackups; 462617ba13bSMingming Cao struct ext4_iloc iloc; 463ac27a0ecSDave Kleikamp __le32 *data; 464ac27a0ecSDave Kleikamp int err; 465ac27a0ecSDave Kleikamp 466ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 467ac27a0ecSDave Kleikamp printk(KERN_DEBUG 468617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 469ac27a0ecSDave Kleikamp gdb_num); 470ac27a0ecSDave Kleikamp 471ac27a0ecSDave Kleikamp /* 472ac27a0ecSDave Kleikamp * If we are not using the primary superblock/GDT copy don't resize, 473ac27a0ecSDave Kleikamp * because the user tools have no way of handling this. Probably a 474ac27a0ecSDave Kleikamp * bad time to do it anyways. 475ac27a0ecSDave Kleikamp */ 476617ba13bSMingming Cao if (EXT4_SB(sb)->s_sbh->b_blocknr != 477617ba13bSMingming Cao le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 47812062dddSEric Sandeen ext4_warning(sb, "won't resize using backup superblock at %llu", 479617ba13bSMingming Cao (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 480ac27a0ecSDave Kleikamp return -EPERM; 481ac27a0ecSDave Kleikamp } 482ac27a0ecSDave Kleikamp 4832f919710SYongqiang Yang gdb_bh = sb_bread(sb, gdblock); 4842f919710SYongqiang Yang if (!gdb_bh) 485ac27a0ecSDave Kleikamp return -EIO; 486ac27a0ecSDave Kleikamp 4872f919710SYongqiang Yang gdbackups = verify_reserved_gdb(sb, gdb_bh); 4882f919710SYongqiang Yang if (gdbackups < 0) { 489ac27a0ecSDave Kleikamp err = gdbackups; 490ac27a0ecSDave Kleikamp goto exit_bh; 491ac27a0ecSDave Kleikamp } 492ac27a0ecSDave Kleikamp 493617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 494ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 495ac27a0ecSDave Kleikamp if (!dind) { 496ac27a0ecSDave Kleikamp err = -EIO; 497ac27a0ecSDave Kleikamp goto exit_bh; 498ac27a0ecSDave Kleikamp } 499ac27a0ecSDave Kleikamp 500ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 501617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 50212062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 5032f919710SYongqiang Yang group, gdblock); 504ac27a0ecSDave Kleikamp err = -EINVAL; 505ac27a0ecSDave Kleikamp goto exit_dind; 506ac27a0ecSDave Kleikamp } 507ac27a0ecSDave Kleikamp 508b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 509b4097142STheodore Ts'o if (unlikely(err)) 510ac27a0ecSDave Kleikamp goto exit_dind; 511ac27a0ecSDave Kleikamp 5122f919710SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 513b4097142STheodore Ts'o if (unlikely(err)) 514ac27a0ecSDave Kleikamp goto exit_sbh; 515ac27a0ecSDave Kleikamp 516b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, dind); 517b4097142STheodore Ts'o if (unlikely(err)) 518b4097142STheodore Ts'o ext4_std_error(sb, err); 519ac27a0ecSDave Kleikamp 520617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 521b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 522b4097142STheodore Ts'o if (unlikely(err)) 523ac27a0ecSDave Kleikamp goto exit_dindj; 524ac27a0ecSDave Kleikamp 525f18a5f21STheodore Ts'o n_group_desc = ext4_kvmalloc((gdb_num + 1) * 526f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 527216553c4SJosef Bacik GFP_NOFS); 528ac27a0ecSDave Kleikamp if (!n_group_desc) { 529ac27a0ecSDave Kleikamp err = -ENOMEM; 530f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 531f18a5f21STheodore Ts'o gdb_num + 1); 532ac27a0ecSDave Kleikamp goto exit_inode; 533ac27a0ecSDave Kleikamp } 534ac27a0ecSDave Kleikamp 535ac27a0ecSDave Kleikamp /* 536ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 537ac27a0ecSDave Kleikamp * 538ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 539ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 540ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 541ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 542ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 543ac27a0ecSDave Kleikamp */ 544617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 545b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 546b4097142STheodore Ts'o if (unlikely(err)) { 547b4097142STheodore Ts'o ext4_std_error(sb, err); 548b4097142STheodore Ts'o goto exit_inode; 549b4097142STheodore Ts'o } 550ac27a0ecSDave Kleikamp inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; 551617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 5522f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 5532f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 554b4097142STheodore Ts'o if (unlikely(err)) { 555b4097142STheodore Ts'o ext4_std_error(sb, err); 556b4097142STheodore Ts'o goto exit_inode; 557b4097142STheodore Ts'o } 558b4097142STheodore Ts'o brelse(dind); 559ac27a0ecSDave Kleikamp 560617ba13bSMingming Cao o_group_desc = EXT4_SB(sb)->s_group_desc; 561ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 562617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 5632f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 564617ba13bSMingming Cao EXT4_SB(sb)->s_group_desc = n_group_desc; 565617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 566f18a5f21STheodore Ts'o ext4_kvfree(o_group_desc); 567ac27a0ecSDave Kleikamp 568e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 569b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 570b4097142STheodore Ts'o if (err) 571b4097142STheodore Ts'o ext4_std_error(sb, err); 572ac27a0ecSDave Kleikamp 573b4097142STheodore Ts'o return err; 574ac27a0ecSDave Kleikamp 575ac27a0ecSDave Kleikamp exit_inode: 576f18a5f21STheodore Ts'o ext4_kvfree(n_group_desc); 577537a0310SAmir Goldstein /* ext4_handle_release_buffer(handle, iloc.bh); */ 578ac27a0ecSDave Kleikamp brelse(iloc.bh); 579ac27a0ecSDave Kleikamp exit_dindj: 580537a0310SAmir Goldstein /* ext4_handle_release_buffer(handle, dind); */ 581ac27a0ecSDave Kleikamp exit_sbh: 582537a0310SAmir Goldstein /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */ 583ac27a0ecSDave Kleikamp exit_dind: 584ac27a0ecSDave Kleikamp brelse(dind); 585ac27a0ecSDave Kleikamp exit_bh: 5862f919710SYongqiang Yang brelse(gdb_bh); 587ac27a0ecSDave Kleikamp 588617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 589ac27a0ecSDave Kleikamp return err; 590ac27a0ecSDave Kleikamp } 591ac27a0ecSDave Kleikamp 592ac27a0ecSDave Kleikamp /* 593ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 594ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 595ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 596ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 597ac27a0ecSDave Kleikamp * 598ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 599ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 600ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 601ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 602ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 603ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 604ac27a0ecSDave Kleikamp */ 605ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 606668f4dc5SYongqiang Yang ext4_group_t group) 607ac27a0ecSDave Kleikamp { 608ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 609617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 610ac27a0ecSDave Kleikamp struct buffer_head **primary; 611ac27a0ecSDave Kleikamp struct buffer_head *dind; 612617ba13bSMingming Cao struct ext4_iloc iloc; 613617ba13bSMingming Cao ext4_fsblk_t blk; 614ac27a0ecSDave Kleikamp __le32 *data, *end; 615ac27a0ecSDave Kleikamp int gdbackups = 0; 616ac27a0ecSDave Kleikamp int res, i; 617ac27a0ecSDave Kleikamp int err; 618ac27a0ecSDave Kleikamp 619216553c4SJosef Bacik primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS); 620ac27a0ecSDave Kleikamp if (!primary) 621ac27a0ecSDave Kleikamp return -ENOMEM; 622ac27a0ecSDave Kleikamp 623617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 624ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 625ac27a0ecSDave Kleikamp if (!dind) { 626ac27a0ecSDave Kleikamp err = -EIO; 627ac27a0ecSDave Kleikamp goto exit_free; 628ac27a0ecSDave Kleikamp } 629ac27a0ecSDave Kleikamp 630617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 63194460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 63294460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 633617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 634ac27a0ecSDave Kleikamp 635ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 636ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 637ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 63812062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 639ac27a0ecSDave Kleikamp " not at offset %ld", 640ac27a0ecSDave Kleikamp blk, 641ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 642ac27a0ecSDave Kleikamp err = -EINVAL; 643ac27a0ecSDave Kleikamp goto exit_bh; 644ac27a0ecSDave Kleikamp } 645ac27a0ecSDave Kleikamp primary[res] = sb_bread(sb, blk); 646ac27a0ecSDave Kleikamp if (!primary[res]) { 647ac27a0ecSDave Kleikamp err = -EIO; 648ac27a0ecSDave Kleikamp goto exit_bh; 649ac27a0ecSDave Kleikamp } 650ac27a0ecSDave Kleikamp if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) { 651ac27a0ecSDave Kleikamp brelse(primary[res]); 652ac27a0ecSDave Kleikamp err = gdbackups; 653ac27a0ecSDave Kleikamp goto exit_bh; 654ac27a0ecSDave Kleikamp } 655ac27a0ecSDave Kleikamp if (++data >= end) 656ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 657ac27a0ecSDave Kleikamp } 658ac27a0ecSDave Kleikamp 659ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 660617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary[i]))) { 661ac27a0ecSDave Kleikamp /* 662ac27a0ecSDave Kleikamp int j; 663ac27a0ecSDave Kleikamp for (j = 0; j < i; j++) 664537a0310SAmir Goldstein ext4_handle_release_buffer(handle, primary[j]); 665ac27a0ecSDave Kleikamp */ 666ac27a0ecSDave Kleikamp goto exit_bh; 667ac27a0ecSDave Kleikamp } 668ac27a0ecSDave Kleikamp } 669ac27a0ecSDave Kleikamp 670617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 671ac27a0ecSDave Kleikamp goto exit_bh; 672ac27a0ecSDave Kleikamp 673ac27a0ecSDave Kleikamp /* 674ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 675ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 676ac27a0ecSDave Kleikamp */ 677668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 678ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 679ac27a0ecSDave Kleikamp int err2; 680ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 681ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 682ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 683ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 684ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 6850390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 686ac27a0ecSDave Kleikamp if (!err) 687ac27a0ecSDave Kleikamp err = err2; 688ac27a0ecSDave Kleikamp } 689ac27a0ecSDave Kleikamp inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; 690617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 691ac27a0ecSDave Kleikamp 692ac27a0ecSDave Kleikamp exit_bh: 693ac27a0ecSDave Kleikamp while (--res >= 0) 694ac27a0ecSDave Kleikamp brelse(primary[res]); 695ac27a0ecSDave Kleikamp brelse(dind); 696ac27a0ecSDave Kleikamp 697ac27a0ecSDave Kleikamp exit_free: 698ac27a0ecSDave Kleikamp kfree(primary); 699ac27a0ecSDave Kleikamp 700ac27a0ecSDave Kleikamp return err; 701ac27a0ecSDave Kleikamp } 702ac27a0ecSDave Kleikamp 703ac27a0ecSDave Kleikamp /* 704617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 705ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 706ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 707ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 708ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 709ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 710ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 711ac27a0ecSDave Kleikamp * 71232ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 71332ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 71432ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 71532ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 71632ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 71732ed5058STheodore Ts'o * which changed s_groups_count will backup again. 718ac27a0ecSDave Kleikamp */ 719ac27a0ecSDave Kleikamp static void update_backups(struct super_block *sb, 720ac27a0ecSDave Kleikamp int blk_off, char *data, int size) 721ac27a0ecSDave Kleikamp { 722617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 723fd2d4291SAvantika Mathur const ext4_group_t last = sbi->s_groups_count; 724617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 725ac27a0ecSDave Kleikamp unsigned three = 1; 726ac27a0ecSDave Kleikamp unsigned five = 5; 727ac27a0ecSDave Kleikamp unsigned seven = 7; 728fd2d4291SAvantika Mathur ext4_group_t group; 729ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 730ac27a0ecSDave Kleikamp handle_t *handle; 731ac27a0ecSDave Kleikamp int err = 0, err2; 732ac27a0ecSDave Kleikamp 733617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 734ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 735ac27a0ecSDave Kleikamp group = 1; 736ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 737ac27a0ecSDave Kleikamp goto exit_err; 738ac27a0ecSDave Kleikamp } 739ac27a0ecSDave Kleikamp 740617ba13bSMingming Cao while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) { 741ac27a0ecSDave Kleikamp struct buffer_head *bh; 742ac27a0ecSDave Kleikamp 743ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 7440390131bSFrank Mayhar if (ext4_handle_valid(handle) && 7450390131bSFrank Mayhar handle->h_buffer_credits == 0 && 746617ba13bSMingming Cao ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && 747617ba13bSMingming Cao (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 748ac27a0ecSDave Kleikamp break; 749ac27a0ecSDave Kleikamp 750ac27a0ecSDave Kleikamp bh = sb_getblk(sb, group * bpg + blk_off); 751ac27a0ecSDave Kleikamp if (!bh) { 752ac27a0ecSDave Kleikamp err = -EIO; 753ac27a0ecSDave Kleikamp break; 754ac27a0ecSDave Kleikamp } 755617ba13bSMingming Cao ext4_debug("update metadata backup %#04lx\n", 756ac27a0ecSDave Kleikamp (unsigned long)bh->b_blocknr); 757617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) 758ac27a0ecSDave Kleikamp break; 759ac27a0ecSDave Kleikamp lock_buffer(bh); 760ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 761ac27a0ecSDave Kleikamp if (rest) 762ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 763ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 764ac27a0ecSDave Kleikamp unlock_buffer(bh); 765b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 766b4097142STheodore Ts'o if (unlikely(err)) 767b4097142STheodore Ts'o ext4_std_error(sb, err); 768ac27a0ecSDave Kleikamp brelse(bh); 769ac27a0ecSDave Kleikamp } 770617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 771ac27a0ecSDave Kleikamp err = err2; 772ac27a0ecSDave Kleikamp 773ac27a0ecSDave Kleikamp /* 774ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 775ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 776ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 777ac27a0ecSDave Kleikamp * 778ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 779ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 780ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 781ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 782ac27a0ecSDave Kleikamp */ 783ac27a0ecSDave Kleikamp exit_err: 784ac27a0ecSDave Kleikamp if (err) { 78512062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 786ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 787617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 788617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 789ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 790ac27a0ecSDave Kleikamp } 791ac27a0ecSDave Kleikamp } 792ac27a0ecSDave Kleikamp 793bb08c1e7SYongqiang Yang /* 794bb08c1e7SYongqiang Yang * ext4_add_new_descs() adds @count group descriptor of groups 795bb08c1e7SYongqiang Yang * starting at @group 796bb08c1e7SYongqiang Yang * 797bb08c1e7SYongqiang Yang * @handle: journal handle 798bb08c1e7SYongqiang Yang * @sb: super block 799bb08c1e7SYongqiang Yang * @group: the group no. of the first group desc to be added 800bb08c1e7SYongqiang Yang * @resize_inode: the resize inode 801bb08c1e7SYongqiang Yang * @count: number of group descriptors to be added 802bb08c1e7SYongqiang Yang */ 803bb08c1e7SYongqiang Yang static int ext4_add_new_descs(handle_t *handle, struct super_block *sb, 804bb08c1e7SYongqiang Yang ext4_group_t group, struct inode *resize_inode, 805bb08c1e7SYongqiang Yang ext4_group_t count) 806bb08c1e7SYongqiang Yang { 807bb08c1e7SYongqiang Yang struct ext4_sb_info *sbi = EXT4_SB(sb); 808bb08c1e7SYongqiang Yang struct ext4_super_block *es = sbi->s_es; 809bb08c1e7SYongqiang Yang struct buffer_head *gdb_bh; 810bb08c1e7SYongqiang Yang int i, gdb_off, gdb_num, err = 0; 811bb08c1e7SYongqiang Yang 812bb08c1e7SYongqiang Yang for (i = 0; i < count; i++, group++) { 813bb08c1e7SYongqiang Yang int reserved_gdb = ext4_bg_has_super(sb, group) ? 814bb08c1e7SYongqiang Yang le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 815bb08c1e7SYongqiang Yang 816bb08c1e7SYongqiang Yang gdb_off = group % EXT4_DESC_PER_BLOCK(sb); 817bb08c1e7SYongqiang Yang gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 818bb08c1e7SYongqiang Yang 819bb08c1e7SYongqiang Yang /* 820bb08c1e7SYongqiang Yang * We will only either add reserved group blocks to a backup group 821bb08c1e7SYongqiang Yang * or remove reserved blocks for the first group in a new group block. 822bb08c1e7SYongqiang Yang * Doing both would be mean more complex code, and sane people don't 823bb08c1e7SYongqiang Yang * use non-sparse filesystems anymore. This is already checked above. 824bb08c1e7SYongqiang Yang */ 825bb08c1e7SYongqiang Yang if (gdb_off) { 826bb08c1e7SYongqiang Yang gdb_bh = sbi->s_group_desc[gdb_num]; 827bb08c1e7SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 828bb08c1e7SYongqiang Yang 829bb08c1e7SYongqiang Yang if (!err && reserved_gdb && ext4_bg_num_gdb(sb, group)) 830bb08c1e7SYongqiang Yang err = reserve_backup_gdb(handle, resize_inode, group); 831bb08c1e7SYongqiang Yang } else 832bb08c1e7SYongqiang Yang err = add_new_gdb(handle, resize_inode, group); 833bb08c1e7SYongqiang Yang if (err) 834bb08c1e7SYongqiang Yang break; 835bb08c1e7SYongqiang Yang } 836bb08c1e7SYongqiang Yang return err; 837bb08c1e7SYongqiang Yang } 838bb08c1e7SYongqiang Yang 839ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 840ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 841ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 842ac27a0ecSDave Kleikamp * write the data to disk. 843ac27a0ecSDave Kleikamp * 844ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 845ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 846ac27a0ecSDave Kleikamp * 847ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 848ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 849ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 850ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 851ac27a0ecSDave Kleikamp */ 852617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 853ac27a0ecSDave Kleikamp { 854617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 855617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 856617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 857ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 858ac27a0ecSDave Kleikamp struct buffer_head *primary = NULL; 859617ba13bSMingming Cao struct ext4_group_desc *gdp; 860ac27a0ecSDave Kleikamp struct inode *inode = NULL; 861ac27a0ecSDave Kleikamp handle_t *handle; 862ac27a0ecSDave Kleikamp int gdb_off, gdb_num; 863ac27a0ecSDave Kleikamp int err, err2; 864ac27a0ecSDave Kleikamp 865617ba13bSMingming Cao gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); 866617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 867ac27a0ecSDave Kleikamp 868617ba13bSMingming Cao if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb, 869617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 87012062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 871ac27a0ecSDave Kleikamp return -EPERM; 872ac27a0ecSDave Kleikamp } 873ac27a0ecSDave Kleikamp 874bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 875bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 87612062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 877ac27a0ecSDave Kleikamp return -EINVAL; 878ac27a0ecSDave Kleikamp } 879ac27a0ecSDave Kleikamp 880617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 881ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 88212062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 883ac27a0ecSDave Kleikamp return -EINVAL; 884ac27a0ecSDave Kleikamp } 885ac27a0ecSDave Kleikamp 886ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 887617ba13bSMingming Cao if (!EXT4_HAS_COMPAT_FEATURE(sb, 88837609fd5SJosef Bacik EXT4_FEATURE_COMPAT_RESIZE_INODE) 88937609fd5SJosef Bacik || !le16_to_cpu(es->s_reserved_gdt_blocks)) { 89012062dddSEric Sandeen ext4_warning(sb, 891ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 892ac27a0ecSDave Kleikamp return -EPERM; 893ac27a0ecSDave Kleikamp } 8941d1fe1eeSDavid Howells inode = ext4_iget(sb, EXT4_RESIZE_INO); 8951d1fe1eeSDavid Howells if (IS_ERR(inode)) { 89612062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 8971d1fe1eeSDavid Howells return PTR_ERR(inode); 898ac27a0ecSDave Kleikamp } 899ac27a0ecSDave Kleikamp } 900ac27a0ecSDave Kleikamp 901920313a7SAneesh Kumar K.V 902ac27a0ecSDave Kleikamp if ((err = verify_group_input(sb, input))) 903ac27a0ecSDave Kleikamp goto exit_put; 904ac27a0ecSDave Kleikamp 905ac27a0ecSDave Kleikamp if ((err = setup_new_group_blocks(sb, input))) 906ac27a0ecSDave Kleikamp goto exit_put; 907ac27a0ecSDave Kleikamp 908ac27a0ecSDave Kleikamp /* 909ac27a0ecSDave Kleikamp * We will always be modifying at least the superblock and a GDT 910ac27a0ecSDave Kleikamp * block. If we are adding a group past the last current GDT block, 911ac27a0ecSDave Kleikamp * we will also modify the inode and the dindirect block. If we 912ac27a0ecSDave Kleikamp * are adding a group with superblock/GDT backups we will also 913ac27a0ecSDave Kleikamp * modify each of the reserved GDT dindirect blocks. 914ac27a0ecSDave Kleikamp */ 915617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 916617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? 917ac27a0ecSDave Kleikamp 3 + reserved_gdb : 4); 918ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 919ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 920ac27a0ecSDave Kleikamp goto exit_put; 921ac27a0ecSDave Kleikamp } 922ac27a0ecSDave Kleikamp 923617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh))) 924ac27a0ecSDave Kleikamp goto exit_journal; 925ac27a0ecSDave Kleikamp 926ac27a0ecSDave Kleikamp /* 927ac27a0ecSDave Kleikamp * We will only either add reserved group blocks to a backup group 928ac27a0ecSDave Kleikamp * or remove reserved blocks for the first group in a new group block. 929ac27a0ecSDave Kleikamp * Doing both would be mean more complex code, and sane people don't 930ac27a0ecSDave Kleikamp * use non-sparse filesystems anymore. This is already checked above. 931ac27a0ecSDave Kleikamp */ 932ac27a0ecSDave Kleikamp if (gdb_off) { 933ac27a0ecSDave Kleikamp primary = sbi->s_group_desc[gdb_num]; 934617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary))) 935ac27a0ecSDave Kleikamp goto exit_journal; 936ac27a0ecSDave Kleikamp 937668f4dc5SYongqiang Yang if (reserved_gdb && ext4_bg_num_gdb(sb, input->group)) { 938668f4dc5SYongqiang Yang err = reserve_backup_gdb(handle, inode, input->group); 939668f4dc5SYongqiang Yang if (err) 940ac27a0ecSDave Kleikamp goto exit_journal; 941668f4dc5SYongqiang Yang } 9422f919710SYongqiang Yang } else { 9432f919710SYongqiang Yang /* 9442f919710SYongqiang Yang * Note that we can access new group descriptor block safely 9452f919710SYongqiang Yang * only if add_new_gdb() succeeds. 9462f919710SYongqiang Yang */ 9472f919710SYongqiang Yang err = add_new_gdb(handle, inode, input->group); 9482f919710SYongqiang Yang if (err) 949ac27a0ecSDave Kleikamp goto exit_journal; 9502f919710SYongqiang Yang primary = sbi->s_group_desc[gdb_num]; 9512f919710SYongqiang Yang } 952ac27a0ecSDave Kleikamp 953ac27a0ecSDave Kleikamp /* 954ac27a0ecSDave Kleikamp * OK, now we've set up the new group. Time to make it active. 955ac27a0ecSDave Kleikamp * 956ac27a0ecSDave Kleikamp * so we have to be safe wrt. concurrent accesses the group 957ac27a0ecSDave Kleikamp * data. So we need to be careful to set all of the relevant 958ac27a0ecSDave Kleikamp * group descriptor data etc. *before* we enable the group. 959ac27a0ecSDave Kleikamp * 960ac27a0ecSDave Kleikamp * The key field here is sbi->s_groups_count: as long as 961ac27a0ecSDave Kleikamp * that retains its old value, nobody is going to access the new 962ac27a0ecSDave Kleikamp * group. 963ac27a0ecSDave Kleikamp * 964ac27a0ecSDave Kleikamp * So first we update all the descriptor metadata for the new 965ac27a0ecSDave Kleikamp * group; then we update the total disk blocks count; then we 966ac27a0ecSDave Kleikamp * update the groups count to enable the group; then finally we 967ac27a0ecSDave Kleikamp * update the free space counts so that the system can start 968ac27a0ecSDave Kleikamp * using the new disk blocks. 969ac27a0ecSDave Kleikamp */ 970ac27a0ecSDave Kleikamp 971ac27a0ecSDave Kleikamp /* Update group descriptor block for new group */ 9722856922cSFrederic Bohe gdp = (struct ext4_group_desc *)((char *)primary->b_data + 9732856922cSFrederic Bohe gdb_off * EXT4_DESC_SIZE(sb)); 974ac27a0ecSDave Kleikamp 975fdff73f0STheodore Ts'o memset(gdp, 0, EXT4_DESC_SIZE(sb)); 9768fadc143SAlexandre Ratchov ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */ 9778fadc143SAlexandre Ratchov ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */ 9788fadc143SAlexandre Ratchov ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */ 979021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count); 980560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 981fdff73f0STheodore Ts'o gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED); 982717d50e4SAndreas Dilger gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp); 983ac27a0ecSDave Kleikamp 984ac27a0ecSDave Kleikamp /* 9855f21b0e6SFrederic Bohe * We can allocate memory for mb_alloc based on the new group 9865f21b0e6SFrederic Bohe * descriptor 9875f21b0e6SFrederic Bohe */ 988920313a7SAneesh Kumar K.V err = ext4_mb_add_groupinfo(sb, input->group, gdp); 98908c3a813SAneesh Kumar K.V if (err) 9905f21b0e6SFrederic Bohe goto exit_journal; 991c2ea3fdeSTheodore Ts'o 9925f21b0e6SFrederic Bohe /* 993ac27a0ecSDave Kleikamp * Make the new blocks and inodes valid next. We do this before 994ac27a0ecSDave Kleikamp * increasing the group count so that once the group is enabled, 995ac27a0ecSDave Kleikamp * all of its blocks and inodes are already valid. 996ac27a0ecSDave Kleikamp * 997ac27a0ecSDave Kleikamp * We always allocate group-by-group, then block-by-block or 998ac27a0ecSDave Kleikamp * inode-by-inode within a group, so enabling these 999ac27a0ecSDave Kleikamp * blocks/inodes before the group is live won't actually let us 1000ac27a0ecSDave Kleikamp * allocate the new space yet. 1001ac27a0ecSDave Kleikamp */ 1002bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, ext4_blocks_count(es) + 1003ac27a0ecSDave Kleikamp input->blocks_count); 1004e8546d06SMarcin Slusarz le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb)); 1005ac27a0ecSDave Kleikamp 1006ac27a0ecSDave Kleikamp /* 1007ac27a0ecSDave Kleikamp * We need to protect s_groups_count against other CPUs seeing 1008ac27a0ecSDave Kleikamp * inconsistent state in the superblock. 1009ac27a0ecSDave Kleikamp * 1010ac27a0ecSDave Kleikamp * The precise rules we use are: 1011ac27a0ecSDave Kleikamp * 1012ac27a0ecSDave Kleikamp * * Writers must perform a smp_wmb() after updating all dependent 1013ac27a0ecSDave Kleikamp * data and before modifying the groups count 1014ac27a0ecSDave Kleikamp * 1015ac27a0ecSDave Kleikamp * * Readers must perform an smp_rmb() after reading the groups count 1016ac27a0ecSDave Kleikamp * and before reading any dependent data. 1017ac27a0ecSDave Kleikamp * 1018ac27a0ecSDave Kleikamp * NB. These rules can be relaxed when checking the group count 1019ac27a0ecSDave Kleikamp * while freeing data, as we can only allocate from a block 1020ac27a0ecSDave Kleikamp * group after serialising against the group count, and we can 1021ac27a0ecSDave Kleikamp * only then free after serialising in turn against that 1022ac27a0ecSDave Kleikamp * allocation. 1023ac27a0ecSDave Kleikamp */ 1024ac27a0ecSDave Kleikamp smp_wmb(); 1025ac27a0ecSDave Kleikamp 1026ac27a0ecSDave Kleikamp /* Update the global fs size fields */ 1027ac27a0ecSDave Kleikamp sbi->s_groups_count++; 1028ac27a0ecSDave Kleikamp 1029b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, primary); 1030b4097142STheodore Ts'o if (unlikely(err)) { 1031b4097142STheodore Ts'o ext4_std_error(sb, err); 1032b4097142STheodore Ts'o goto exit_journal; 1033b4097142STheodore Ts'o } 1034ac27a0ecSDave Kleikamp 1035ac27a0ecSDave Kleikamp /* Update the reserved block counts only once the new group is 1036ac27a0ecSDave Kleikamp * active. */ 1037bd81d8eeSLaurent Vivier ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 1038ac27a0ecSDave Kleikamp input->reserved_blocks); 1039ac27a0ecSDave Kleikamp 1040ac27a0ecSDave Kleikamp /* Update the free space counts */ 104157042651STheodore Ts'o percpu_counter_add(&sbi->s_freeclusters_counter, 104257042651STheodore Ts'o EXT4_B2C(sbi, input->free_blocks_count)); 1043aa0dff2dSPeter Zijlstra percpu_counter_add(&sbi->s_freeinodes_counter, 1044617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb)); 1045ac27a0ecSDave Kleikamp 104642007efdSEric Sandeen if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) && 104742007efdSEric Sandeen sbi->s_log_groups_per_flex) { 1048c62a11fdSFrederic Bohe ext4_group_t flex_group; 1049c62a11fdSFrederic Bohe flex_group = ext4_flex_group(sbi, input->group); 105024aaa8efSTheodore Ts'o atomic_add(EXT4_B2C(sbi, input->free_blocks_count), 105124aaa8efSTheodore Ts'o &sbi->s_flex_groups[flex_group].free_clusters); 10529f24e420STheodore Ts'o atomic_add(EXT4_INODES_PER_GROUP(sb), 10539f24e420STheodore Ts'o &sbi->s_flex_groups[flex_group].free_inodes); 1054c62a11fdSFrederic Bohe } 1055c62a11fdSFrederic Bohe 1056a0375156STheodore Ts'o ext4_handle_dirty_super(handle, sb); 1057ac27a0ecSDave Kleikamp 1058ac27a0ecSDave Kleikamp exit_journal: 1059617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 1060ac27a0ecSDave Kleikamp err = err2; 10612f919710SYongqiang Yang if (!err && primary) { 1062ac27a0ecSDave Kleikamp update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 1063617ba13bSMingming Cao sizeof(struct ext4_super_block)); 1064ac27a0ecSDave Kleikamp update_backups(sb, primary->b_blocknr, primary->b_data, 1065ac27a0ecSDave Kleikamp primary->b_size); 1066ac27a0ecSDave Kleikamp } 1067ac27a0ecSDave Kleikamp exit_put: 1068ac27a0ecSDave Kleikamp iput(inode); 1069ac27a0ecSDave Kleikamp return err; 1070617ba13bSMingming Cao } /* ext4_group_add */ 1071ac27a0ecSDave Kleikamp 10722b2d6d01STheodore Ts'o /* 107318e31438SYongqiang Yang * extend a group without checking assuming that checking has been done. 107418e31438SYongqiang Yang */ 107518e31438SYongqiang Yang static int ext4_group_extend_no_check(struct super_block *sb, 107618e31438SYongqiang Yang ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) 107718e31438SYongqiang Yang { 107818e31438SYongqiang Yang struct ext4_super_block *es = EXT4_SB(sb)->s_es; 107918e31438SYongqiang Yang handle_t *handle; 108018e31438SYongqiang Yang int err = 0, err2; 108118e31438SYongqiang Yang 108218e31438SYongqiang Yang /* We will update the superblock, one block bitmap, and 108318e31438SYongqiang Yang * one group descriptor via ext4_group_add_blocks(). 108418e31438SYongqiang Yang */ 108518e31438SYongqiang Yang handle = ext4_journal_start_sb(sb, 3); 108618e31438SYongqiang Yang if (IS_ERR(handle)) { 108718e31438SYongqiang Yang err = PTR_ERR(handle); 108818e31438SYongqiang Yang ext4_warning(sb, "error %d on journal start", err); 108918e31438SYongqiang Yang return err; 109018e31438SYongqiang Yang } 109118e31438SYongqiang Yang 109218e31438SYongqiang Yang err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 109318e31438SYongqiang Yang if (err) { 109418e31438SYongqiang Yang ext4_warning(sb, "error %d on journal write access", err); 109518e31438SYongqiang Yang goto errout; 109618e31438SYongqiang Yang } 109718e31438SYongqiang Yang 109818e31438SYongqiang Yang ext4_blocks_count_set(es, o_blocks_count + add); 109918e31438SYongqiang Yang ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 110018e31438SYongqiang Yang o_blocks_count + add); 110118e31438SYongqiang Yang /* We add the blocks to the bitmap and set the group need init bit */ 110218e31438SYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 110318e31438SYongqiang Yang if (err) 110418e31438SYongqiang Yang goto errout; 110518e31438SYongqiang Yang ext4_handle_dirty_super(handle, sb); 110618e31438SYongqiang Yang ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 110718e31438SYongqiang Yang o_blocks_count + add); 110818e31438SYongqiang Yang errout: 110918e31438SYongqiang Yang err2 = ext4_journal_stop(handle); 111018e31438SYongqiang Yang if (err2 && !err) 111118e31438SYongqiang Yang err = err2; 111218e31438SYongqiang Yang 111318e31438SYongqiang Yang if (!err) { 111418e31438SYongqiang Yang if (test_opt(sb, DEBUG)) 111518e31438SYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extended group to %llu " 111618e31438SYongqiang Yang "blocks\n", ext4_blocks_count(es)); 111718e31438SYongqiang Yang update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, 111818e31438SYongqiang Yang sizeof(struct ext4_super_block)); 111918e31438SYongqiang Yang } 112018e31438SYongqiang Yang return err; 112118e31438SYongqiang Yang } 112218e31438SYongqiang Yang 112318e31438SYongqiang Yang /* 11242b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 1125ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 1126ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 1127ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 1128ac27a0ecSDave Kleikamp * 1129617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 1130ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 1131ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 1132ac27a0ecSDave Kleikamp */ 1133617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 1134617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 1135ac27a0ecSDave Kleikamp { 1136617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 1137617ba13bSMingming Cao ext4_grpblk_t last; 1138617ba13bSMingming Cao ext4_grpblk_t add; 1139ac27a0ecSDave Kleikamp struct buffer_head *bh; 1140ac27a0ecSDave Kleikamp handle_t *handle; 1141cc7365dfSYongqiang Yang int err, err2; 11425f21b0e6SFrederic Bohe ext4_group_t group; 1143ac27a0ecSDave Kleikamp 1144bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 1145ac27a0ecSDave Kleikamp 1146ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 11472b79b09dSYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n", 1148ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 1149ac27a0ecSDave Kleikamp 1150ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 1151ac27a0ecSDave Kleikamp return 0; 1152ac27a0ecSDave Kleikamp 1153ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 1154617ba13bSMingming Cao printk(KERN_ERR "EXT4-fs: filesystem on %s:" 11552ae02107SMingming Cao " too large to resize to %llu blocks safely\n", 1156ac27a0ecSDave Kleikamp sb->s_id, n_blocks_count); 1157ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 115812062dddSEric Sandeen ext4_warning(sb, "CONFIG_LBDAF not enabled"); 1159ac27a0ecSDave Kleikamp return -EINVAL; 1160ac27a0ecSDave Kleikamp } 1161ac27a0ecSDave Kleikamp 1162ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 116312062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 11648f82f840SYongqiang Yang return -EINVAL; 1165ac27a0ecSDave Kleikamp } 1166ac27a0ecSDave Kleikamp 1167ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 11685f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1169ac27a0ecSDave Kleikamp 1170ac27a0ecSDave Kleikamp if (last == 0) { 117112062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1172ac27a0ecSDave Kleikamp return -EPERM; 1173ac27a0ecSDave Kleikamp } 1174ac27a0ecSDave Kleikamp 1175617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1176ac27a0ecSDave Kleikamp 1177ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 117812062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1179ac27a0ecSDave Kleikamp return -EINVAL; 1180ac27a0ecSDave Kleikamp } 1181ac27a0ecSDave Kleikamp 1182ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1183ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1184ac27a0ecSDave Kleikamp 1185ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 118612062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1187ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1188ac27a0ecSDave Kleikamp 1189ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 1190ac27a0ecSDave Kleikamp bh = sb_bread(sb, o_blocks_count + add - 1); 1191ac27a0ecSDave Kleikamp if (!bh) { 119212062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1193ac27a0ecSDave Kleikamp return -ENOSPC; 1194ac27a0ecSDave Kleikamp } 1195ac27a0ecSDave Kleikamp brelse(bh); 1196ac27a0ecSDave Kleikamp 1197ac27a0ecSDave Kleikamp /* We will update the superblock, one block bitmap, and 1198617ba13bSMingming Cao * one group descriptor via ext4_free_blocks(). 1199ac27a0ecSDave Kleikamp */ 1200617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 3); 1201ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1202ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 120312062dddSEric Sandeen ext4_warning(sb, "error %d on journal start", err); 1204ac27a0ecSDave Kleikamp goto exit_put; 1205ac27a0ecSDave Kleikamp } 1206ac27a0ecSDave Kleikamp 1207617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, 1208617ba13bSMingming Cao EXT4_SB(sb)->s_sbh))) { 120912062dddSEric Sandeen ext4_warning(sb, "error %d on journal write access", err); 1210617ba13bSMingming Cao ext4_journal_stop(handle); 1211ac27a0ecSDave Kleikamp goto exit_put; 1212ac27a0ecSDave Kleikamp } 1213bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, o_blocks_count + add); 1214c549a95dSEric Sandeen ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 1215ac27a0ecSDave Kleikamp o_blocks_count + add); 1216e21675d4SAneesh Kumar K.V /* We add the blocks to the bitmap and set the group need init bit */ 1217cc7365dfSYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 1218a0375156STheodore Ts'o ext4_handle_dirty_super(handle, sb); 12192ae02107SMingming Cao ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 1220ac27a0ecSDave Kleikamp o_blocks_count + add); 1221cc7365dfSYongqiang Yang err2 = ext4_journal_stop(handle); 1222cc7365dfSYongqiang Yang if (!err && err2) 1223cc7365dfSYongqiang Yang err = err2; 1224cc7365dfSYongqiang Yang 1225cc7365dfSYongqiang Yang if (err) 1226ac27a0ecSDave Kleikamp goto exit_put; 12275f21b0e6SFrederic Bohe 1228ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 1229bd81d8eeSLaurent Vivier printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n", 1230bd81d8eeSLaurent Vivier ext4_blocks_count(es)); 1231617ba13bSMingming Cao update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, 1232617ba13bSMingming Cao sizeof(struct ext4_super_block)); 1233ac27a0ecSDave Kleikamp exit_put: 1234ac27a0ecSDave Kleikamp return err; 1235617ba13bSMingming Cao } /* ext4_group_extend */ 1236