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" 18717d50e4SAndreas Dilger #include "group.h" 19ac27a0ecSDave Kleikamp 20ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 21ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 22ac27a0ecSDave Kleikamp 23ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 24617ba13bSMingming Cao struct ext4_new_group_data *input) 25ac27a0ecSDave Kleikamp { 26617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 27617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 28bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 29617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 30fd2d4291SAvantika Mathur ext4_group_t group = input->group; 31617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 32617ba13bSMingming Cao unsigned overhead = ext4_bg_has_super(sb, group) ? 33617ba13bSMingming Cao (1 + ext4_bg_num_gdb(sb, group) + 34ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks)) : 0; 35617ba13bSMingming Cao ext4_fsblk_t metaend = start + overhead; 36ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 373a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 38ac27a0ecSDave Kleikamp int err = -EINVAL; 39ac27a0ecSDave Kleikamp 40ac27a0ecSDave Kleikamp input->free_blocks_count = free_blocks_count = 41ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 42ac27a0ecSDave Kleikamp 43ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 44617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 45ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 46617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 47ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 48ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 49ac27a0ecSDave Kleikamp 503a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 51ac27a0ecSDave Kleikamp if (group != sbi->s_groups_count) 5246e665e9SHarvey Harrison ext4_warning(sb, __func__, 53ac27a0ecSDave Kleikamp "Cannot add at group %u (only %lu groups)", 54ac27a0ecSDave Kleikamp input->group, sbi->s_groups_count); 553a5b2ecdSMingming Cao else if (offset != 0) 5646e665e9SHarvey Harrison ext4_warning(sb, __func__, "Last group not full"); 57ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 5846e665e9SHarvey Harrison ext4_warning(sb, __func__, "Reserved blocks too high (%u)", 59ac27a0ecSDave Kleikamp input->reserved_blocks); 60ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 6146e665e9SHarvey Harrison ext4_warning(sb, __func__, "Bad blocks count %u", 62ac27a0ecSDave Kleikamp input->blocks_count); 63ac27a0ecSDave Kleikamp else if (!(bh = sb_bread(sb, end - 1))) 6446e665e9SHarvey Harrison ext4_warning(sb, __func__, 652ae02107SMingming Cao "Cannot read last block (%llu)", 66ac27a0ecSDave Kleikamp end - 1); 67ac27a0ecSDave Kleikamp else if (outside(input->block_bitmap, start, end)) 6846e665e9SHarvey Harrison ext4_warning(sb, __func__, 69bd81d8eeSLaurent Vivier "Block bitmap not in group (block %llu)", 701939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 71ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 7246e665e9SHarvey Harrison ext4_warning(sb, __func__, 73bd81d8eeSLaurent Vivier "Inode bitmap not in group (block %llu)", 741939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 75ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 76ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 7746e665e9SHarvey Harrison ext4_warning(sb, __func__, 78bd81d8eeSLaurent Vivier "Inode table not in group (blocks %llu-%llu)", 791939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 80ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 8146e665e9SHarvey Harrison ext4_warning(sb, __func__, 82bd81d8eeSLaurent Vivier "Block bitmap same as inode bitmap (%llu)", 831939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 84ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 8546e665e9SHarvey Harrison ext4_warning(sb, __func__, 86bd81d8eeSLaurent Vivier "Block bitmap (%llu) in inode table (%llu-%llu)", 871939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 881939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 89ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 9046e665e9SHarvey Harrison ext4_warning(sb, __func__, 91bd81d8eeSLaurent Vivier "Inode bitmap (%llu) in inode table (%llu-%llu)", 921939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 931939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 94ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 9546e665e9SHarvey Harrison ext4_warning(sb, __func__, 96bd81d8eeSLaurent Vivier "Block bitmap (%llu) in GDT table" 972ae02107SMingming Cao " (%llu-%llu)", 981939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 991939e49aSRandy Dunlap start, metaend - 1); 100ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 10146e665e9SHarvey Harrison ext4_warning(sb, __func__, 102bd81d8eeSLaurent Vivier "Inode bitmap (%llu) in GDT table" 1032ae02107SMingming Cao " (%llu-%llu)", 1041939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1051939e49aSRandy Dunlap start, metaend - 1); 106ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 107ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 10846e665e9SHarvey Harrison ext4_warning(sb, __func__, 1092ae02107SMingming Cao "Inode table (%llu-%llu) overlaps" 1102ae02107SMingming Cao "GDT table (%llu-%llu)", 1111939e49aSRandy Dunlap (unsigned long long)input->inode_table, 1121939e49aSRandy Dunlap itend - 1, start, metaend - 1); 113ac27a0ecSDave Kleikamp else 114ac27a0ecSDave Kleikamp err = 0; 115ac27a0ecSDave Kleikamp brelse(bh); 116ac27a0ecSDave Kleikamp 117ac27a0ecSDave Kleikamp return err; 118ac27a0ecSDave Kleikamp } 119ac27a0ecSDave Kleikamp 120ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 121617ba13bSMingming Cao ext4_fsblk_t blk) 122ac27a0ecSDave Kleikamp { 123ac27a0ecSDave Kleikamp struct buffer_head *bh; 124ac27a0ecSDave Kleikamp int err; 125ac27a0ecSDave Kleikamp 126ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 127ac27a0ecSDave Kleikamp if (!bh) 128ac27a0ecSDave Kleikamp return ERR_PTR(-EIO); 129617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 130ac27a0ecSDave Kleikamp brelse(bh); 131ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 132ac27a0ecSDave Kleikamp } else { 133ac27a0ecSDave Kleikamp lock_buffer(bh); 134ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 135ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 136ac27a0ecSDave Kleikamp unlock_buffer(bh); 137ac27a0ecSDave Kleikamp } 138ac27a0ecSDave Kleikamp 139ac27a0ecSDave Kleikamp return bh; 140ac27a0ecSDave Kleikamp } 141ac27a0ecSDave Kleikamp 142ac27a0ecSDave Kleikamp /* 14314904107SEric Sandeen * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA. 14414904107SEric Sandeen * If that fails, restart the transaction & regain write access for the 14514904107SEric Sandeen * buffer head which is used for block_bitmap modifications. 14614904107SEric Sandeen */ 14714904107SEric Sandeen static int extend_or_restart_transaction(handle_t *handle, int thresh, 14814904107SEric Sandeen struct buffer_head *bh) 14914904107SEric Sandeen { 15014904107SEric Sandeen int err; 15114904107SEric Sandeen 152*0390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, thresh)) 15314904107SEric Sandeen return 0; 15414904107SEric Sandeen 15514904107SEric Sandeen err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA); 15614904107SEric Sandeen if (err < 0) 15714904107SEric Sandeen return err; 15814904107SEric Sandeen if (err) { 15914904107SEric Sandeen if ((err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 16014904107SEric Sandeen return err; 16114904107SEric Sandeen if ((err = ext4_journal_get_write_access(handle, bh))) 16214904107SEric Sandeen return err; 16314904107SEric Sandeen } 16414904107SEric Sandeen 16514904107SEric Sandeen return 0; 16614904107SEric Sandeen } 16714904107SEric Sandeen 16814904107SEric Sandeen /* 169ac27a0ecSDave Kleikamp * Set up the block and inode bitmaps, and the inode table for the new group. 170ac27a0ecSDave Kleikamp * This doesn't need to be part of the main transaction, since we are only 171ac27a0ecSDave Kleikamp * changing blocks outside the actual filesystem. We still do journaling to 172ac27a0ecSDave Kleikamp * ensure the recovery is correct in case of a failure just after resize. 173ac27a0ecSDave Kleikamp * If any part of this fails, we simply abort the resize. 174ac27a0ecSDave Kleikamp */ 175ac27a0ecSDave Kleikamp static int setup_new_group_blocks(struct super_block *sb, 176617ba13bSMingming Cao struct ext4_new_group_data *input) 177ac27a0ecSDave Kleikamp { 178617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 179617ba13bSMingming Cao ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group); 180617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 181ac27a0ecSDave Kleikamp le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0; 182617ba13bSMingming Cao unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group); 183ac27a0ecSDave Kleikamp struct buffer_head *bh; 184ac27a0ecSDave Kleikamp handle_t *handle; 185617ba13bSMingming Cao ext4_fsblk_t block; 186617ba13bSMingming Cao ext4_grpblk_t bit; 187ac27a0ecSDave Kleikamp int i; 188ac27a0ecSDave Kleikamp int err = 0, err2; 189ac27a0ecSDave Kleikamp 19014904107SEric Sandeen /* This transaction may be extended/restarted along the way */ 19114904107SEric Sandeen handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 19214904107SEric Sandeen 193ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 194ac27a0ecSDave Kleikamp return PTR_ERR(handle); 195ac27a0ecSDave Kleikamp 196ac27a0ecSDave Kleikamp lock_super(sb); 197ac27a0ecSDave Kleikamp if (input->group != sbi->s_groups_count) { 198ac27a0ecSDave Kleikamp err = -EBUSY; 199ac27a0ecSDave Kleikamp goto exit_journal; 200ac27a0ecSDave Kleikamp } 201ac27a0ecSDave Kleikamp 202ac27a0ecSDave Kleikamp if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) { 203ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 204ac27a0ecSDave Kleikamp goto exit_journal; 205ac27a0ecSDave Kleikamp } 206ac27a0ecSDave Kleikamp 207617ba13bSMingming Cao if (ext4_bg_has_super(sb, input->group)) { 208c549a95dSEric Sandeen ext4_debug("mark backup superblock %#04llx (+0)\n", start); 209617ba13bSMingming Cao ext4_set_bit(0, bh->b_data); 210ac27a0ecSDave Kleikamp } 211ac27a0ecSDave Kleikamp 212ac27a0ecSDave Kleikamp /* Copy all of the GDT blocks into the backup in this group */ 213ac27a0ecSDave Kleikamp for (i = 0, bit = 1, block = start + 1; 214ac27a0ecSDave Kleikamp i < gdblocks; i++, block++, bit++) { 215ac27a0ecSDave Kleikamp struct buffer_head *gdb; 216ac27a0ecSDave Kleikamp 217c549a95dSEric Sandeen ext4_debug("update backup group %#04llx (+%d)\n", block, bit); 218ac27a0ecSDave Kleikamp 21914904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 1, bh))) 22014904107SEric Sandeen goto exit_bh; 22114904107SEric Sandeen 222ac27a0ecSDave Kleikamp gdb = sb_getblk(sb, block); 223ac27a0ecSDave Kleikamp if (!gdb) { 224ac27a0ecSDave Kleikamp err = -EIO; 225ac27a0ecSDave Kleikamp goto exit_bh; 226ac27a0ecSDave Kleikamp } 227617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, gdb))) { 228ac27a0ecSDave Kleikamp brelse(gdb); 229ac27a0ecSDave Kleikamp goto exit_bh; 230ac27a0ecSDave Kleikamp } 2315b615287SEric Sandeen lock_buffer(gdb); 2325b615287SEric Sandeen memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size); 233ac27a0ecSDave Kleikamp set_buffer_uptodate(gdb); 2345b615287SEric Sandeen unlock_buffer(gdb); 235*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, gdb); 236617ba13bSMingming Cao ext4_set_bit(bit, bh->b_data); 237ac27a0ecSDave Kleikamp brelse(gdb); 238ac27a0ecSDave Kleikamp } 239ac27a0ecSDave Kleikamp 240ac27a0ecSDave Kleikamp /* Zero out all of the reserved backup group descriptor table blocks */ 241ac27a0ecSDave Kleikamp for (i = 0, bit = gdblocks + 1, block = start + bit; 242ac27a0ecSDave Kleikamp i < reserved_gdb; i++, block++, bit++) { 243ac27a0ecSDave Kleikamp struct buffer_head *gdb; 244ac27a0ecSDave Kleikamp 245c549a95dSEric Sandeen ext4_debug("clear reserved block %#04llx (+%d)\n", block, bit); 246ac27a0ecSDave Kleikamp 24714904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 1, bh))) 24814904107SEric Sandeen goto exit_bh; 24914904107SEric Sandeen 250ac27a0ecSDave Kleikamp if (IS_ERR(gdb = bclean(handle, sb, block))) { 251ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 252ac27a0ecSDave Kleikamp goto exit_bh; 253ac27a0ecSDave Kleikamp } 254*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, gdb); 255617ba13bSMingming Cao ext4_set_bit(bit, bh->b_data); 256ac27a0ecSDave Kleikamp brelse(gdb); 257ac27a0ecSDave Kleikamp } 258c549a95dSEric Sandeen ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap, 259ac27a0ecSDave Kleikamp input->block_bitmap - start); 260617ba13bSMingming Cao ext4_set_bit(input->block_bitmap - start, bh->b_data); 261c549a95dSEric Sandeen ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap, 262ac27a0ecSDave Kleikamp input->inode_bitmap - start); 263617ba13bSMingming Cao ext4_set_bit(input->inode_bitmap - start, bh->b_data); 264ac27a0ecSDave Kleikamp 265ac27a0ecSDave Kleikamp /* Zero out all of the inode table blocks */ 266ac27a0ecSDave Kleikamp for (i = 0, block = input->inode_table, bit = block - start; 267ac27a0ecSDave Kleikamp i < sbi->s_itb_per_group; i++, bit++, block++) { 268ac27a0ecSDave Kleikamp struct buffer_head *it; 269ac27a0ecSDave Kleikamp 270c549a95dSEric Sandeen ext4_debug("clear inode block %#04llx (+%d)\n", block, bit); 27114904107SEric Sandeen 27214904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 1, bh))) 27314904107SEric Sandeen goto exit_bh; 27414904107SEric Sandeen 275ac27a0ecSDave Kleikamp if (IS_ERR(it = bclean(handle, sb, block))) { 276ac27a0ecSDave Kleikamp err = PTR_ERR(it); 277ac27a0ecSDave Kleikamp goto exit_bh; 278ac27a0ecSDave Kleikamp } 279*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, it); 280ac27a0ecSDave Kleikamp brelse(it); 281617ba13bSMingming Cao ext4_set_bit(bit, bh->b_data); 282ac27a0ecSDave Kleikamp } 28314904107SEric Sandeen 28414904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 2, bh))) 28514904107SEric Sandeen goto exit_bh; 28614904107SEric Sandeen 287617ba13bSMingming Cao mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb), 288ac27a0ecSDave Kleikamp bh->b_data); 289*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, bh); 290ac27a0ecSDave Kleikamp brelse(bh); 291ac27a0ecSDave Kleikamp 292ac27a0ecSDave Kleikamp /* Mark unused entries in inode bitmap used */ 293c549a95dSEric Sandeen ext4_debug("clear inode bitmap %#04llx (+%llu)\n", 294ac27a0ecSDave Kleikamp input->inode_bitmap, input->inode_bitmap - start); 295ac27a0ecSDave Kleikamp if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) { 296ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 297ac27a0ecSDave Kleikamp goto exit_journal; 298ac27a0ecSDave Kleikamp } 299ac27a0ecSDave Kleikamp 300617ba13bSMingming Cao mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), 301ac27a0ecSDave Kleikamp bh->b_data); 302*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, bh); 303ac27a0ecSDave Kleikamp exit_bh: 304ac27a0ecSDave Kleikamp brelse(bh); 305ac27a0ecSDave Kleikamp 306ac27a0ecSDave Kleikamp exit_journal: 307ac27a0ecSDave Kleikamp unlock_super(sb); 308617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 309ac27a0ecSDave Kleikamp err = err2; 310ac27a0ecSDave Kleikamp 311ac27a0ecSDave Kleikamp return err; 312ac27a0ecSDave Kleikamp } 313ac27a0ecSDave Kleikamp 314ac27a0ecSDave Kleikamp /* 315ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 316617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 317ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 318ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 319ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 320ac27a0ecSDave Kleikamp */ 321617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 322ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 323ac27a0ecSDave Kleikamp { 324ac27a0ecSDave Kleikamp unsigned *min = three; 325ac27a0ecSDave Kleikamp int mult = 3; 326ac27a0ecSDave Kleikamp unsigned ret; 327ac27a0ecSDave Kleikamp 328617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 329617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 330ac27a0ecSDave Kleikamp ret = *min; 331ac27a0ecSDave Kleikamp *min += 1; 332ac27a0ecSDave Kleikamp return ret; 333ac27a0ecSDave Kleikamp } 334ac27a0ecSDave Kleikamp 335ac27a0ecSDave Kleikamp if (*five < *min) { 336ac27a0ecSDave Kleikamp min = five; 337ac27a0ecSDave Kleikamp mult = 5; 338ac27a0ecSDave Kleikamp } 339ac27a0ecSDave Kleikamp if (*seven < *min) { 340ac27a0ecSDave Kleikamp min = seven; 341ac27a0ecSDave Kleikamp mult = 7; 342ac27a0ecSDave Kleikamp } 343ac27a0ecSDave Kleikamp 344ac27a0ecSDave Kleikamp ret = *min; 345ac27a0ecSDave Kleikamp *min *= mult; 346ac27a0ecSDave Kleikamp 347ac27a0ecSDave Kleikamp return ret; 348ac27a0ecSDave Kleikamp } 349ac27a0ecSDave Kleikamp 350ac27a0ecSDave Kleikamp /* 351ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 352ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 353ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 354ac27a0ecSDave Kleikamp */ 355ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 356ac27a0ecSDave Kleikamp struct buffer_head *primary) 357ac27a0ecSDave Kleikamp { 358617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 359fd2d4291SAvantika Mathur const ext4_group_t end = EXT4_SB(sb)->s_groups_count; 360ac27a0ecSDave Kleikamp unsigned three = 1; 361ac27a0ecSDave Kleikamp unsigned five = 5; 362ac27a0ecSDave Kleikamp unsigned seven = 7; 363ac27a0ecSDave Kleikamp unsigned grp; 364ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 365ac27a0ecSDave Kleikamp int gdbackups = 0; 366ac27a0ecSDave Kleikamp 367617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 368bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 369bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 37046e665e9SHarvey Harrison ext4_warning(sb, __func__, 3712ae02107SMingming Cao "reserved GDT %llu" 3722ae02107SMingming Cao " missing grp %d (%llu)", 373ac27a0ecSDave Kleikamp blk, grp, 374bd81d8eeSLaurent Vivier grp * 375bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 376bd81d8eeSLaurent Vivier blk); 377ac27a0ecSDave Kleikamp return -EINVAL; 378ac27a0ecSDave Kleikamp } 379617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 380ac27a0ecSDave Kleikamp return -EFBIG; 381ac27a0ecSDave Kleikamp } 382ac27a0ecSDave Kleikamp 383ac27a0ecSDave Kleikamp return gdbackups; 384ac27a0ecSDave Kleikamp } 385ac27a0ecSDave Kleikamp 386ac27a0ecSDave Kleikamp /* 387ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 388ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 389ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 390ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 391ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 392ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 393ac27a0ecSDave Kleikamp * 394ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 395ac27a0ecSDave Kleikamp * 396ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 397ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 398ac27a0ecSDave Kleikamp */ 399ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 400617ba13bSMingming Cao struct ext4_new_group_data *input, 401ac27a0ecSDave Kleikamp struct buffer_head **primary) 402ac27a0ecSDave Kleikamp { 403ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 404617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 405617ba13bSMingming Cao unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); 406617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 407ac27a0ecSDave Kleikamp struct buffer_head **o_group_desc, **n_group_desc; 408ac27a0ecSDave Kleikamp struct buffer_head *dind; 409ac27a0ecSDave Kleikamp int gdbackups; 410617ba13bSMingming Cao struct ext4_iloc iloc; 411ac27a0ecSDave Kleikamp __le32 *data; 412ac27a0ecSDave Kleikamp int err; 413ac27a0ecSDave Kleikamp 414ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 415ac27a0ecSDave Kleikamp printk(KERN_DEBUG 416617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 417ac27a0ecSDave Kleikamp gdb_num); 418ac27a0ecSDave Kleikamp 419ac27a0ecSDave Kleikamp /* 420ac27a0ecSDave Kleikamp * If we are not using the primary superblock/GDT copy don't resize, 421ac27a0ecSDave Kleikamp * because the user tools have no way of handling this. Probably a 422ac27a0ecSDave Kleikamp * bad time to do it anyways. 423ac27a0ecSDave Kleikamp */ 424617ba13bSMingming Cao if (EXT4_SB(sb)->s_sbh->b_blocknr != 425617ba13bSMingming Cao le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 42646e665e9SHarvey Harrison ext4_warning(sb, __func__, 427ac27a0ecSDave Kleikamp "won't resize using backup superblock at %llu", 428617ba13bSMingming Cao (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 429ac27a0ecSDave Kleikamp return -EPERM; 430ac27a0ecSDave Kleikamp } 431ac27a0ecSDave Kleikamp 432ac27a0ecSDave Kleikamp *primary = sb_bread(sb, gdblock); 433ac27a0ecSDave Kleikamp if (!*primary) 434ac27a0ecSDave Kleikamp return -EIO; 435ac27a0ecSDave Kleikamp 436ac27a0ecSDave Kleikamp if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) { 437ac27a0ecSDave Kleikamp err = gdbackups; 438ac27a0ecSDave Kleikamp goto exit_bh; 439ac27a0ecSDave Kleikamp } 440ac27a0ecSDave Kleikamp 441617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 442ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 443ac27a0ecSDave Kleikamp if (!dind) { 444ac27a0ecSDave Kleikamp err = -EIO; 445ac27a0ecSDave Kleikamp goto exit_bh; 446ac27a0ecSDave Kleikamp } 447ac27a0ecSDave Kleikamp 448ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 449617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 45046e665e9SHarvey Harrison ext4_warning(sb, __func__, 4512ae02107SMingming Cao "new group %u GDT block %llu not reserved", 452ac27a0ecSDave Kleikamp input->group, gdblock); 453ac27a0ecSDave Kleikamp err = -EINVAL; 454ac27a0ecSDave Kleikamp goto exit_dind; 455ac27a0ecSDave Kleikamp } 456ac27a0ecSDave Kleikamp 457617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh))) 458ac27a0ecSDave Kleikamp goto exit_dind; 459ac27a0ecSDave Kleikamp 460617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, *primary))) 461ac27a0ecSDave Kleikamp goto exit_sbh; 462ac27a0ecSDave Kleikamp 463617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, dind))) 464ac27a0ecSDave Kleikamp goto exit_primary; 465ac27a0ecSDave Kleikamp 466617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 467617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 468ac27a0ecSDave Kleikamp goto exit_dindj; 469ac27a0ecSDave Kleikamp 470ac27a0ecSDave Kleikamp n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 471216553c4SJosef Bacik GFP_NOFS); 472ac27a0ecSDave Kleikamp if (!n_group_desc) { 473ac27a0ecSDave Kleikamp err = -ENOMEM; 47446e665e9SHarvey Harrison ext4_warning(sb, __func__, 475ac27a0ecSDave Kleikamp "not enough memory for %lu groups", gdb_num + 1); 476ac27a0ecSDave Kleikamp goto exit_inode; 477ac27a0ecSDave Kleikamp } 478ac27a0ecSDave Kleikamp 479ac27a0ecSDave Kleikamp /* 480ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 481ac27a0ecSDave Kleikamp * 482ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 483ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 484ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 485ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 486ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 487ac27a0ecSDave Kleikamp */ 488617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 489*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, dind); 490ac27a0ecSDave Kleikamp brelse(dind); 491ac27a0ecSDave Kleikamp inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; 492617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 493ac27a0ecSDave Kleikamp memset((*primary)->b_data, 0, sb->s_blocksize); 494*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, *primary); 495ac27a0ecSDave Kleikamp 496617ba13bSMingming Cao o_group_desc = EXT4_SB(sb)->s_group_desc; 497ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 498617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 499ac27a0ecSDave Kleikamp n_group_desc[gdb_num] = *primary; 500617ba13bSMingming Cao EXT4_SB(sb)->s_group_desc = n_group_desc; 501617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 502ac27a0ecSDave Kleikamp kfree(o_group_desc); 503ac27a0ecSDave Kleikamp 504e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 505*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 506ac27a0ecSDave Kleikamp 507ac27a0ecSDave Kleikamp return 0; 508ac27a0ecSDave Kleikamp 509ac27a0ecSDave Kleikamp exit_inode: 5102b2d6d01STheodore Ts'o /* ext4_journal_release_buffer(handle, iloc.bh); */ 511ac27a0ecSDave Kleikamp brelse(iloc.bh); 512ac27a0ecSDave Kleikamp exit_dindj: 5132b2d6d01STheodore Ts'o /* ext4_journal_release_buffer(handle, dind); */ 514ac27a0ecSDave Kleikamp exit_primary: 5152b2d6d01STheodore Ts'o /* ext4_journal_release_buffer(handle, *primary); */ 516ac27a0ecSDave Kleikamp exit_sbh: 5172b2d6d01STheodore Ts'o /* ext4_journal_release_buffer(handle, *primary); */ 518ac27a0ecSDave Kleikamp exit_dind: 519ac27a0ecSDave Kleikamp brelse(dind); 520ac27a0ecSDave Kleikamp exit_bh: 521ac27a0ecSDave Kleikamp brelse(*primary); 522ac27a0ecSDave Kleikamp 523617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 524ac27a0ecSDave Kleikamp return err; 525ac27a0ecSDave Kleikamp } 526ac27a0ecSDave Kleikamp 527ac27a0ecSDave Kleikamp /* 528ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 529ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 530ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 531ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 532ac27a0ecSDave Kleikamp * 533ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 534ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 535ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 536ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 537ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 538ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 539ac27a0ecSDave Kleikamp */ 540ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 541617ba13bSMingming Cao struct ext4_new_group_data *input) 542ac27a0ecSDave Kleikamp { 543ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 544617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 545ac27a0ecSDave Kleikamp struct buffer_head **primary; 546ac27a0ecSDave Kleikamp struct buffer_head *dind; 547617ba13bSMingming Cao struct ext4_iloc iloc; 548617ba13bSMingming Cao ext4_fsblk_t blk; 549ac27a0ecSDave Kleikamp __le32 *data, *end; 550ac27a0ecSDave Kleikamp int gdbackups = 0; 551ac27a0ecSDave Kleikamp int res, i; 552ac27a0ecSDave Kleikamp int err; 553ac27a0ecSDave Kleikamp 554216553c4SJosef Bacik primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS); 555ac27a0ecSDave Kleikamp if (!primary) 556ac27a0ecSDave Kleikamp return -ENOMEM; 557ac27a0ecSDave Kleikamp 558617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 559ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 560ac27a0ecSDave Kleikamp if (!dind) { 561ac27a0ecSDave Kleikamp err = -EIO; 562ac27a0ecSDave Kleikamp goto exit_free; 563ac27a0ecSDave Kleikamp } 564ac27a0ecSDave Kleikamp 565617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 56694460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 56794460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 568617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 569ac27a0ecSDave Kleikamp 570ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 571ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 572ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 57346e665e9SHarvey Harrison ext4_warning(sb, __func__, 5742ae02107SMingming Cao "reserved block %llu" 575ac27a0ecSDave Kleikamp " not at offset %ld", 576ac27a0ecSDave Kleikamp blk, 577ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 578ac27a0ecSDave Kleikamp err = -EINVAL; 579ac27a0ecSDave Kleikamp goto exit_bh; 580ac27a0ecSDave Kleikamp } 581ac27a0ecSDave Kleikamp primary[res] = sb_bread(sb, blk); 582ac27a0ecSDave Kleikamp if (!primary[res]) { 583ac27a0ecSDave Kleikamp err = -EIO; 584ac27a0ecSDave Kleikamp goto exit_bh; 585ac27a0ecSDave Kleikamp } 586ac27a0ecSDave Kleikamp if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) { 587ac27a0ecSDave Kleikamp brelse(primary[res]); 588ac27a0ecSDave Kleikamp err = gdbackups; 589ac27a0ecSDave Kleikamp goto exit_bh; 590ac27a0ecSDave Kleikamp } 591ac27a0ecSDave Kleikamp if (++data >= end) 592ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 593ac27a0ecSDave Kleikamp } 594ac27a0ecSDave Kleikamp 595ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 596617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary[i]))) { 597ac27a0ecSDave Kleikamp /* 598ac27a0ecSDave Kleikamp int j; 599ac27a0ecSDave Kleikamp for (j = 0; j < i; j++) 600617ba13bSMingming Cao ext4_journal_release_buffer(handle, primary[j]); 601ac27a0ecSDave Kleikamp */ 602ac27a0ecSDave Kleikamp goto exit_bh; 603ac27a0ecSDave Kleikamp } 604ac27a0ecSDave Kleikamp } 605ac27a0ecSDave Kleikamp 606617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 607ac27a0ecSDave Kleikamp goto exit_bh; 608ac27a0ecSDave Kleikamp 609ac27a0ecSDave Kleikamp /* 610ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 611ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 612ac27a0ecSDave Kleikamp */ 613617ba13bSMingming Cao blk = input->group * EXT4_BLOCKS_PER_GROUP(sb); 614ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 615ac27a0ecSDave Kleikamp int err2; 616ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 617ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 618ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 619ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 620ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 621*0390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 622ac27a0ecSDave Kleikamp if (!err) 623ac27a0ecSDave Kleikamp err = err2; 624ac27a0ecSDave Kleikamp } 625ac27a0ecSDave Kleikamp inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; 626617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 627ac27a0ecSDave Kleikamp 628ac27a0ecSDave Kleikamp exit_bh: 629ac27a0ecSDave Kleikamp while (--res >= 0) 630ac27a0ecSDave Kleikamp brelse(primary[res]); 631ac27a0ecSDave Kleikamp brelse(dind); 632ac27a0ecSDave Kleikamp 633ac27a0ecSDave Kleikamp exit_free: 634ac27a0ecSDave Kleikamp kfree(primary); 635ac27a0ecSDave Kleikamp 636ac27a0ecSDave Kleikamp return err; 637ac27a0ecSDave Kleikamp } 638ac27a0ecSDave Kleikamp 639ac27a0ecSDave Kleikamp /* 640617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 641ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 642ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 643ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 644ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 645ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 646ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 647ac27a0ecSDave Kleikamp * 648ac27a0ecSDave Kleikamp * We do not need lock_super() for this, because these blocks are not 649ac27a0ecSDave Kleikamp * otherwise touched by the filesystem code when it is mounted. We don't 650ac27a0ecSDave Kleikamp * need to worry about last changing from sbi->s_groups_count, because the 651ac27a0ecSDave Kleikamp * worst that can happen is that we do not copy the full number of backups 652ac27a0ecSDave Kleikamp * at this time. The resize which changed s_groups_count will backup again. 653ac27a0ecSDave Kleikamp */ 654ac27a0ecSDave Kleikamp static void update_backups(struct super_block *sb, 655ac27a0ecSDave Kleikamp int blk_off, char *data, int size) 656ac27a0ecSDave Kleikamp { 657617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 658fd2d4291SAvantika Mathur const ext4_group_t last = sbi->s_groups_count; 659617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 660ac27a0ecSDave Kleikamp unsigned three = 1; 661ac27a0ecSDave Kleikamp unsigned five = 5; 662ac27a0ecSDave Kleikamp unsigned seven = 7; 663fd2d4291SAvantika Mathur ext4_group_t group; 664ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 665ac27a0ecSDave Kleikamp handle_t *handle; 666ac27a0ecSDave Kleikamp int err = 0, err2; 667ac27a0ecSDave Kleikamp 668617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 669ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 670ac27a0ecSDave Kleikamp group = 1; 671ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 672ac27a0ecSDave Kleikamp goto exit_err; 673ac27a0ecSDave Kleikamp } 674ac27a0ecSDave Kleikamp 675617ba13bSMingming Cao while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) { 676ac27a0ecSDave Kleikamp struct buffer_head *bh; 677ac27a0ecSDave Kleikamp 678ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 679*0390131bSFrank Mayhar if (ext4_handle_valid(handle) && 680*0390131bSFrank Mayhar handle->h_buffer_credits == 0 && 681617ba13bSMingming Cao ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && 682617ba13bSMingming Cao (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 683ac27a0ecSDave Kleikamp break; 684ac27a0ecSDave Kleikamp 685ac27a0ecSDave Kleikamp bh = sb_getblk(sb, group * bpg + blk_off); 686ac27a0ecSDave Kleikamp if (!bh) { 687ac27a0ecSDave Kleikamp err = -EIO; 688ac27a0ecSDave Kleikamp break; 689ac27a0ecSDave Kleikamp } 690617ba13bSMingming Cao ext4_debug("update metadata backup %#04lx\n", 691ac27a0ecSDave Kleikamp (unsigned long)bh->b_blocknr); 692617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) 693ac27a0ecSDave Kleikamp break; 694ac27a0ecSDave Kleikamp lock_buffer(bh); 695ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 696ac27a0ecSDave Kleikamp if (rest) 697ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 698ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 699ac27a0ecSDave Kleikamp unlock_buffer(bh); 700*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, bh); 701ac27a0ecSDave Kleikamp brelse(bh); 702ac27a0ecSDave Kleikamp } 703617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 704ac27a0ecSDave Kleikamp err = err2; 705ac27a0ecSDave Kleikamp 706ac27a0ecSDave Kleikamp /* 707ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 708ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 709ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 710ac27a0ecSDave Kleikamp * 711ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 712ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 713ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 714ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 715ac27a0ecSDave Kleikamp */ 716ac27a0ecSDave Kleikamp exit_err: 717ac27a0ecSDave Kleikamp if (err) { 71846e665e9SHarvey Harrison ext4_warning(sb, __func__, 719fd2d4291SAvantika Mathur "can't update backup for group %lu (err %d), " 720ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 721617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 722617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 723ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 724ac27a0ecSDave Kleikamp } 725ac27a0ecSDave Kleikamp } 726ac27a0ecSDave Kleikamp 727ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 728ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 729ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 730ac27a0ecSDave Kleikamp * write the data to disk. 731ac27a0ecSDave Kleikamp * 732ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 733ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 734ac27a0ecSDave Kleikamp * 735ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 736ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 737ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 738ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 739ac27a0ecSDave Kleikamp */ 740617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 741ac27a0ecSDave Kleikamp { 742617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 743617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 744617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 745ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 746ac27a0ecSDave Kleikamp struct buffer_head *primary = NULL; 747617ba13bSMingming Cao struct ext4_group_desc *gdp; 748ac27a0ecSDave Kleikamp struct inode *inode = NULL; 749ac27a0ecSDave Kleikamp handle_t *handle; 750ac27a0ecSDave Kleikamp int gdb_off, gdb_num; 751ac27a0ecSDave Kleikamp int err, err2; 752ac27a0ecSDave Kleikamp 753617ba13bSMingming Cao gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); 754617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 755ac27a0ecSDave Kleikamp 756617ba13bSMingming Cao if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb, 757617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 75846e665e9SHarvey Harrison ext4_warning(sb, __func__, 759ac27a0ecSDave Kleikamp "Can't resize non-sparse filesystem further"); 760ac27a0ecSDave Kleikamp return -EPERM; 761ac27a0ecSDave Kleikamp } 762ac27a0ecSDave Kleikamp 763bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 764bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 76546e665e9SHarvey Harrison ext4_warning(sb, __func__, "blocks_count overflow\n"); 766ac27a0ecSDave Kleikamp return -EINVAL; 767ac27a0ecSDave Kleikamp } 768ac27a0ecSDave Kleikamp 769617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 770ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 77146e665e9SHarvey Harrison ext4_warning(sb, __func__, "inodes_count overflow\n"); 772ac27a0ecSDave Kleikamp return -EINVAL; 773ac27a0ecSDave Kleikamp } 774ac27a0ecSDave Kleikamp 775ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 776617ba13bSMingming Cao if (!EXT4_HAS_COMPAT_FEATURE(sb, 77737609fd5SJosef Bacik EXT4_FEATURE_COMPAT_RESIZE_INODE) 77837609fd5SJosef Bacik || !le16_to_cpu(es->s_reserved_gdt_blocks)) { 77946e665e9SHarvey Harrison ext4_warning(sb, __func__, 780ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 781ac27a0ecSDave Kleikamp return -EPERM; 782ac27a0ecSDave Kleikamp } 7831d1fe1eeSDavid Howells inode = ext4_iget(sb, EXT4_RESIZE_INO); 7841d1fe1eeSDavid Howells if (IS_ERR(inode)) { 78546e665e9SHarvey Harrison ext4_warning(sb, __func__, 786ac27a0ecSDave Kleikamp "Error opening resize inode"); 7871d1fe1eeSDavid Howells return PTR_ERR(inode); 788ac27a0ecSDave Kleikamp } 789ac27a0ecSDave Kleikamp } 790ac27a0ecSDave Kleikamp 791ac27a0ecSDave Kleikamp if ((err = verify_group_input(sb, input))) 792ac27a0ecSDave Kleikamp goto exit_put; 793ac27a0ecSDave Kleikamp 794ac27a0ecSDave Kleikamp if ((err = setup_new_group_blocks(sb, input))) 795ac27a0ecSDave Kleikamp goto exit_put; 796ac27a0ecSDave Kleikamp 797ac27a0ecSDave Kleikamp /* 798ac27a0ecSDave Kleikamp * We will always be modifying at least the superblock and a GDT 799ac27a0ecSDave Kleikamp * block. If we are adding a group past the last current GDT block, 800ac27a0ecSDave Kleikamp * we will also modify the inode and the dindirect block. If we 801ac27a0ecSDave Kleikamp * are adding a group with superblock/GDT backups we will also 802ac27a0ecSDave Kleikamp * modify each of the reserved GDT dindirect blocks. 803ac27a0ecSDave Kleikamp */ 804617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 805617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? 806ac27a0ecSDave Kleikamp 3 + reserved_gdb : 4); 807ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 808ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 809ac27a0ecSDave Kleikamp goto exit_put; 810ac27a0ecSDave Kleikamp } 811ac27a0ecSDave Kleikamp 812ac27a0ecSDave Kleikamp lock_super(sb); 813ac27a0ecSDave Kleikamp if (input->group != sbi->s_groups_count) { 81446e665e9SHarvey Harrison ext4_warning(sb, __func__, 815ac27a0ecSDave Kleikamp "multiple resizers run on filesystem!"); 816ac27a0ecSDave Kleikamp err = -EBUSY; 817ac27a0ecSDave Kleikamp goto exit_journal; 818ac27a0ecSDave Kleikamp } 819ac27a0ecSDave Kleikamp 820617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh))) 821ac27a0ecSDave Kleikamp goto exit_journal; 822ac27a0ecSDave Kleikamp 823ac27a0ecSDave Kleikamp /* 824ac27a0ecSDave Kleikamp * We will only either add reserved group blocks to a backup group 825ac27a0ecSDave Kleikamp * or remove reserved blocks for the first group in a new group block. 826ac27a0ecSDave Kleikamp * Doing both would be mean more complex code, and sane people don't 827ac27a0ecSDave Kleikamp * use non-sparse filesystems anymore. This is already checked above. 828ac27a0ecSDave Kleikamp */ 829ac27a0ecSDave Kleikamp if (gdb_off) { 830ac27a0ecSDave Kleikamp primary = sbi->s_group_desc[gdb_num]; 831617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary))) 832ac27a0ecSDave Kleikamp goto exit_journal; 833ac27a0ecSDave Kleikamp 834617ba13bSMingming Cao if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) && 835ac27a0ecSDave Kleikamp (err = reserve_backup_gdb(handle, inode, input))) 836ac27a0ecSDave Kleikamp goto exit_journal; 837ac27a0ecSDave Kleikamp } else if ((err = add_new_gdb(handle, inode, input, &primary))) 838ac27a0ecSDave Kleikamp goto exit_journal; 839ac27a0ecSDave Kleikamp 840ac27a0ecSDave Kleikamp /* 841ac27a0ecSDave Kleikamp * OK, now we've set up the new group. Time to make it active. 842ac27a0ecSDave Kleikamp * 843ac27a0ecSDave Kleikamp * Current kernels don't lock all allocations via lock_super(), 844ac27a0ecSDave Kleikamp * so we have to be safe wrt. concurrent accesses the group 845ac27a0ecSDave Kleikamp * data. So we need to be careful to set all of the relevant 846ac27a0ecSDave Kleikamp * group descriptor data etc. *before* we enable the group. 847ac27a0ecSDave Kleikamp * 848ac27a0ecSDave Kleikamp * The key field here is sbi->s_groups_count: as long as 849ac27a0ecSDave Kleikamp * that retains its old value, nobody is going to access the new 850ac27a0ecSDave Kleikamp * group. 851ac27a0ecSDave Kleikamp * 852ac27a0ecSDave Kleikamp * So first we update all the descriptor metadata for the new 853ac27a0ecSDave Kleikamp * group; then we update the total disk blocks count; then we 854ac27a0ecSDave Kleikamp * update the groups count to enable the group; then finally we 855ac27a0ecSDave Kleikamp * update the free space counts so that the system can start 856ac27a0ecSDave Kleikamp * using the new disk blocks. 857ac27a0ecSDave Kleikamp */ 858ac27a0ecSDave Kleikamp 859ac27a0ecSDave Kleikamp /* Update group descriptor block for new group */ 8602856922cSFrederic Bohe gdp = (struct ext4_group_desc *)((char *)primary->b_data + 8612856922cSFrederic Bohe gdb_off * EXT4_DESC_SIZE(sb)); 862ac27a0ecSDave Kleikamp 8638fadc143SAlexandre Ratchov ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */ 8648fadc143SAlexandre Ratchov ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */ 8658fadc143SAlexandre Ratchov ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */ 866ac27a0ecSDave Kleikamp gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count); 867617ba13bSMingming Cao gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb)); 86893c0d863SSolofo.Ramangalahy@bull.net gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); 869717d50e4SAndreas Dilger gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp); 870ac27a0ecSDave Kleikamp 871ac27a0ecSDave Kleikamp /* 8725f21b0e6SFrederic Bohe * We can allocate memory for mb_alloc based on the new group 8735f21b0e6SFrederic Bohe * descriptor 8745f21b0e6SFrederic Bohe */ 8755f21b0e6SFrederic Bohe err = ext4_mb_add_more_groupinfo(sb, input->group, gdp); 8765f21b0e6SFrederic Bohe if (err) 8775f21b0e6SFrederic Bohe goto exit_journal; 878c2ea3fdeSTheodore Ts'o 8795f21b0e6SFrederic Bohe /* 880ac27a0ecSDave Kleikamp * Make the new blocks and inodes valid next. We do this before 881ac27a0ecSDave Kleikamp * increasing the group count so that once the group is enabled, 882ac27a0ecSDave Kleikamp * all of its blocks and inodes are already valid. 883ac27a0ecSDave Kleikamp * 884ac27a0ecSDave Kleikamp * We always allocate group-by-group, then block-by-block or 885ac27a0ecSDave Kleikamp * inode-by-inode within a group, so enabling these 886ac27a0ecSDave Kleikamp * blocks/inodes before the group is live won't actually let us 887ac27a0ecSDave Kleikamp * allocate the new space yet. 888ac27a0ecSDave Kleikamp */ 889bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, ext4_blocks_count(es) + 890ac27a0ecSDave Kleikamp input->blocks_count); 891e8546d06SMarcin Slusarz le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb)); 892ac27a0ecSDave Kleikamp 893ac27a0ecSDave Kleikamp /* 894ac27a0ecSDave Kleikamp * We need to protect s_groups_count against other CPUs seeing 895ac27a0ecSDave Kleikamp * inconsistent state in the superblock. 896ac27a0ecSDave Kleikamp * 897ac27a0ecSDave Kleikamp * The precise rules we use are: 898ac27a0ecSDave Kleikamp * 899ac27a0ecSDave Kleikamp * * Writers of s_groups_count *must* hold lock_super 900ac27a0ecSDave Kleikamp * AND 901ac27a0ecSDave Kleikamp * * Writers must perform a smp_wmb() after updating all dependent 902ac27a0ecSDave Kleikamp * data and before modifying the groups count 903ac27a0ecSDave Kleikamp * 904ac27a0ecSDave Kleikamp * * Readers must hold lock_super() over the access 905ac27a0ecSDave Kleikamp * OR 906ac27a0ecSDave Kleikamp * * Readers must perform an smp_rmb() after reading the groups count 907ac27a0ecSDave Kleikamp * and before reading any dependent data. 908ac27a0ecSDave Kleikamp * 909ac27a0ecSDave Kleikamp * NB. These rules can be relaxed when checking the group count 910ac27a0ecSDave Kleikamp * while freeing data, as we can only allocate from a block 911ac27a0ecSDave Kleikamp * group after serialising against the group count, and we can 912ac27a0ecSDave Kleikamp * only then free after serialising in turn against that 913ac27a0ecSDave Kleikamp * allocation. 914ac27a0ecSDave Kleikamp */ 915ac27a0ecSDave Kleikamp smp_wmb(); 916ac27a0ecSDave Kleikamp 917ac27a0ecSDave Kleikamp /* Update the global fs size fields */ 918ac27a0ecSDave Kleikamp sbi->s_groups_count++; 919ac27a0ecSDave Kleikamp 920*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, primary); 921ac27a0ecSDave Kleikamp 922ac27a0ecSDave Kleikamp /* Update the reserved block counts only once the new group is 923ac27a0ecSDave Kleikamp * active. */ 924bd81d8eeSLaurent Vivier ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 925ac27a0ecSDave Kleikamp input->reserved_blocks); 926ac27a0ecSDave Kleikamp 927ac27a0ecSDave Kleikamp /* Update the free space counts */ 928aa0dff2dSPeter Zijlstra percpu_counter_add(&sbi->s_freeblocks_counter, 929ac27a0ecSDave Kleikamp input->free_blocks_count); 930aa0dff2dSPeter Zijlstra percpu_counter_add(&sbi->s_freeinodes_counter, 931617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb)); 932ac27a0ecSDave Kleikamp 933c62a11fdSFrederic Bohe if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { 934c62a11fdSFrederic Bohe ext4_group_t flex_group; 935c62a11fdSFrederic Bohe flex_group = ext4_flex_group(sbi, input->group); 936c62a11fdSFrederic Bohe sbi->s_flex_groups[flex_group].free_blocks += 937c62a11fdSFrederic Bohe input->free_blocks_count; 938c62a11fdSFrederic Bohe sbi->s_flex_groups[flex_group].free_inodes += 939c62a11fdSFrederic Bohe EXT4_INODES_PER_GROUP(sb); 940c62a11fdSFrederic Bohe } 941c62a11fdSFrederic Bohe 942*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh); 943ac27a0ecSDave Kleikamp sb->s_dirt = 1; 944ac27a0ecSDave Kleikamp 945ac27a0ecSDave Kleikamp exit_journal: 946ac27a0ecSDave Kleikamp unlock_super(sb); 947617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 948ac27a0ecSDave Kleikamp err = err2; 949ac27a0ecSDave Kleikamp if (!err) { 950ac27a0ecSDave Kleikamp update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 951617ba13bSMingming Cao sizeof(struct ext4_super_block)); 952ac27a0ecSDave Kleikamp update_backups(sb, primary->b_blocknr, primary->b_data, 953ac27a0ecSDave Kleikamp primary->b_size); 954ac27a0ecSDave Kleikamp } 955ac27a0ecSDave Kleikamp exit_put: 956ac27a0ecSDave Kleikamp iput(inode); 957ac27a0ecSDave Kleikamp return err; 958617ba13bSMingming Cao } /* ext4_group_add */ 959ac27a0ecSDave Kleikamp 9602b2d6d01STheodore Ts'o /* 9612b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 962ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 963ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 964ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 965ac27a0ecSDave Kleikamp * 966617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 967ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 968ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 969ac27a0ecSDave Kleikamp */ 970617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 971617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 972ac27a0ecSDave Kleikamp { 973617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 974fd2d4291SAvantika Mathur ext4_group_t o_groups_count; 975617ba13bSMingming Cao ext4_grpblk_t last; 976617ba13bSMingming Cao ext4_grpblk_t add; 977ac27a0ecSDave Kleikamp struct buffer_head *bh; 978ac27a0ecSDave Kleikamp handle_t *handle; 979ac27a0ecSDave Kleikamp int err; 980ac27a0ecSDave Kleikamp unsigned long freed_blocks; 9815f21b0e6SFrederic Bohe ext4_group_t group; 9825f21b0e6SFrederic Bohe struct ext4_group_info *grp; 983ac27a0ecSDave Kleikamp 984ac27a0ecSDave Kleikamp /* We don't need to worry about locking wrt other resizers just 985ac27a0ecSDave Kleikamp * yet: we're going to revalidate es->s_blocks_count after 986ac27a0ecSDave Kleikamp * taking lock_super() below. */ 987bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 988617ba13bSMingming Cao o_groups_count = EXT4_SB(sb)->s_groups_count; 989ac27a0ecSDave Kleikamp 990ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 9912ae02107SMingming Cao printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n", 992ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 993ac27a0ecSDave Kleikamp 994ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 995ac27a0ecSDave Kleikamp return 0; 996ac27a0ecSDave Kleikamp 997ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 998617ba13bSMingming Cao printk(KERN_ERR "EXT4-fs: filesystem on %s:" 9992ae02107SMingming Cao " too large to resize to %llu blocks safely\n", 1000ac27a0ecSDave Kleikamp sb->s_id, n_blocks_count); 1001ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 100246e665e9SHarvey Harrison ext4_warning(sb, __func__, 1003ac27a0ecSDave Kleikamp "CONFIG_LBD not enabled\n"); 1004ac27a0ecSDave Kleikamp return -EINVAL; 1005ac27a0ecSDave Kleikamp } 1006ac27a0ecSDave Kleikamp 1007ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 100846e665e9SHarvey Harrison ext4_warning(sb, __func__, 1009ac27a0ecSDave Kleikamp "can't shrink FS - resize aborted"); 1010ac27a0ecSDave Kleikamp return -EBUSY; 1011ac27a0ecSDave Kleikamp } 1012ac27a0ecSDave Kleikamp 1013ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 10145f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1015ac27a0ecSDave Kleikamp 1016ac27a0ecSDave Kleikamp if (last == 0) { 101746e665e9SHarvey Harrison ext4_warning(sb, __func__, 1018ac27a0ecSDave Kleikamp "need to use ext2online to resize further"); 1019ac27a0ecSDave Kleikamp return -EPERM; 1020ac27a0ecSDave Kleikamp } 1021ac27a0ecSDave Kleikamp 1022617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1023ac27a0ecSDave Kleikamp 1024ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 102546e665e9SHarvey Harrison ext4_warning(sb, __func__, "blocks_count overflow"); 1026ac27a0ecSDave Kleikamp return -EINVAL; 1027ac27a0ecSDave Kleikamp } 1028ac27a0ecSDave Kleikamp 1029ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1030ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1031ac27a0ecSDave Kleikamp 1032ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 103346e665e9SHarvey Harrison ext4_warning(sb, __func__, 10342ae02107SMingming Cao "will only finish group (%llu" 1035ac27a0ecSDave Kleikamp " blocks, %u new)", 1036ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1037ac27a0ecSDave Kleikamp 1038ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 1039ac27a0ecSDave Kleikamp bh = sb_bread(sb, o_blocks_count + add - 1); 1040ac27a0ecSDave Kleikamp if (!bh) { 104146e665e9SHarvey Harrison ext4_warning(sb, __func__, 1042ac27a0ecSDave Kleikamp "can't read last block, resize aborted"); 1043ac27a0ecSDave Kleikamp return -ENOSPC; 1044ac27a0ecSDave Kleikamp } 1045ac27a0ecSDave Kleikamp brelse(bh); 1046ac27a0ecSDave Kleikamp 1047ac27a0ecSDave Kleikamp /* We will update the superblock, one block bitmap, and 1048617ba13bSMingming Cao * one group descriptor via ext4_free_blocks(). 1049ac27a0ecSDave Kleikamp */ 1050617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 3); 1051ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1052ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 105346e665e9SHarvey Harrison ext4_warning(sb, __func__, "error %d on journal start", err); 1054ac27a0ecSDave Kleikamp goto exit_put; 1055ac27a0ecSDave Kleikamp } 1056ac27a0ecSDave Kleikamp 1057ac27a0ecSDave Kleikamp lock_super(sb); 1058bd81d8eeSLaurent Vivier if (o_blocks_count != ext4_blocks_count(es)) { 105946e665e9SHarvey Harrison ext4_warning(sb, __func__, 1060ac27a0ecSDave Kleikamp "multiple resizers run on filesystem!"); 1061ac27a0ecSDave Kleikamp unlock_super(sb); 10625606bf5dSAkinobu Mita ext4_journal_stop(handle); 1063ac27a0ecSDave Kleikamp err = -EBUSY; 1064ac27a0ecSDave Kleikamp goto exit_put; 1065ac27a0ecSDave Kleikamp } 1066ac27a0ecSDave Kleikamp 1067617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, 1068617ba13bSMingming Cao EXT4_SB(sb)->s_sbh))) { 106946e665e9SHarvey Harrison ext4_warning(sb, __func__, 1070ac27a0ecSDave Kleikamp "error %d on journal write access", err); 1071ac27a0ecSDave Kleikamp unlock_super(sb); 1072617ba13bSMingming Cao ext4_journal_stop(handle); 1073ac27a0ecSDave Kleikamp goto exit_put; 1074ac27a0ecSDave Kleikamp } 1075bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, o_blocks_count + add); 1076*0390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 1077ac27a0ecSDave Kleikamp sb->s_dirt = 1; 1078ac27a0ecSDave Kleikamp unlock_super(sb); 1079c549a95dSEric Sandeen ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 1080ac27a0ecSDave Kleikamp o_blocks_count + add); 1081617ba13bSMingming Cao ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks); 10822ae02107SMingming Cao ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 1083ac27a0ecSDave Kleikamp o_blocks_count + add); 1084617ba13bSMingming Cao if ((err = ext4_journal_stop(handle))) 1085ac27a0ecSDave Kleikamp goto exit_put; 10865f21b0e6SFrederic Bohe 10875f21b0e6SFrederic Bohe /* 10885f21b0e6SFrederic Bohe * Mark mballoc pages as not up to date so that they will be updated 10895f21b0e6SFrederic Bohe * next time they are loaded by ext4_mb_load_buddy. 1090c2ea3fdeSTheodore Ts'o * 1091c2ea3fdeSTheodore Ts'o * XXX Bad, Bad, BAD!!! We should not be overloading the 1092c2ea3fdeSTheodore Ts'o * Uptodate flag, particularly on thte bitmap bh, as way of 1093c2ea3fdeSTheodore Ts'o * hinting to ext4_mb_load_buddy() that it needs to be 1094c2ea3fdeSTheodore Ts'o * overloaded. A user could take a LVM snapshot, then do an 1095c2ea3fdeSTheodore Ts'o * on-line fsck, and clear the uptodate flag, and this would 1096c2ea3fdeSTheodore Ts'o * not be a bug in userspace, but a bug in the kernel. FIXME!!! 10975f21b0e6SFrederic Bohe */ 1098c2ea3fdeSTheodore Ts'o { 10995f21b0e6SFrederic Bohe struct ext4_sb_info *sbi = EXT4_SB(sb); 11005f21b0e6SFrederic Bohe struct inode *inode = sbi->s_buddy_cache; 11015f21b0e6SFrederic Bohe int blocks_per_page; 11025f21b0e6SFrederic Bohe int block; 11035f21b0e6SFrederic Bohe int pnum; 11045f21b0e6SFrederic Bohe struct page *page; 11055f21b0e6SFrederic Bohe 11065f21b0e6SFrederic Bohe /* Set buddy page as not up to date */ 11075f21b0e6SFrederic Bohe blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize; 11085f21b0e6SFrederic Bohe block = group * 2; 11095f21b0e6SFrederic Bohe pnum = block / blocks_per_page; 11105f21b0e6SFrederic Bohe page = find_get_page(inode->i_mapping, pnum); 11115f21b0e6SFrederic Bohe if (page != NULL) { 11125f21b0e6SFrederic Bohe ClearPageUptodate(page); 11135f21b0e6SFrederic Bohe page_cache_release(page); 11145f21b0e6SFrederic Bohe } 11155f21b0e6SFrederic Bohe 11165f21b0e6SFrederic Bohe /* Set bitmap page as not up to date */ 11175f21b0e6SFrederic Bohe block++; 11185f21b0e6SFrederic Bohe pnum = block / blocks_per_page; 11195f21b0e6SFrederic Bohe page = find_get_page(inode->i_mapping, pnum); 11205f21b0e6SFrederic Bohe if (page != NULL) { 11215f21b0e6SFrederic Bohe ClearPageUptodate(page); 11225f21b0e6SFrederic Bohe page_cache_release(page); 11235f21b0e6SFrederic Bohe } 11245f21b0e6SFrederic Bohe 11255f21b0e6SFrederic Bohe /* Get the info on the last group */ 11265f21b0e6SFrederic Bohe grp = ext4_get_group_info(sb, group); 11275f21b0e6SFrederic Bohe 11285f21b0e6SFrederic Bohe /* Update free blocks in group info */ 11295f21b0e6SFrederic Bohe ext4_mb_update_group_info(grp, add); 11305f21b0e6SFrederic Bohe } 11315f21b0e6SFrederic Bohe 1132ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 1133bd81d8eeSLaurent Vivier printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n", 1134bd81d8eeSLaurent Vivier ext4_blocks_count(es)); 1135617ba13bSMingming Cao update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, 1136617ba13bSMingming Cao sizeof(struct ext4_super_block)); 1137ac27a0ecSDave Kleikamp exit_put: 1138ac27a0ecSDave Kleikamp return err; 1139617ba13bSMingming Cao } /* ext4_group_extend */ 1140