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 14dab291afSMingming Cao #include <linux/ext4_jbd2.h> 15ac27a0ecSDave Kleikamp 16ac27a0ecSDave Kleikamp #include <linux/errno.h> 17ac27a0ecSDave Kleikamp #include <linux/slab.h> 18ac27a0ecSDave Kleikamp 19717d50e4SAndreas Dilger #include "group.h" 20ac27a0ecSDave Kleikamp 21ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 22ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 23ac27a0ecSDave Kleikamp 24ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 25617ba13bSMingming Cao struct ext4_new_group_data *input) 26ac27a0ecSDave Kleikamp { 27617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 28617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 29bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 30617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 31*fd2d4291SAvantika Mathur ext4_group_t group = input->group; 32617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 33617ba13bSMingming Cao unsigned overhead = ext4_bg_has_super(sb, group) ? 34617ba13bSMingming Cao (1 + ext4_bg_num_gdb(sb, group) + 35ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks)) : 0; 36617ba13bSMingming Cao ext4_fsblk_t metaend = start + overhead; 37ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 383a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 39ac27a0ecSDave Kleikamp int err = -EINVAL; 40ac27a0ecSDave Kleikamp 41ac27a0ecSDave Kleikamp input->free_blocks_count = free_blocks_count = 42ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 43ac27a0ecSDave Kleikamp 44ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 45617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 46ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 47617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 48ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 49ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 50ac27a0ecSDave Kleikamp 513a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 52ac27a0ecSDave Kleikamp if (group != sbi->s_groups_count) 53617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 54ac27a0ecSDave Kleikamp "Cannot add at group %u (only %lu groups)", 55ac27a0ecSDave Kleikamp input->group, sbi->s_groups_count); 563a5b2ecdSMingming Cao else if (offset != 0) 57617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "Last group not full"); 58ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 59617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)", 60ac27a0ecSDave Kleikamp input->reserved_blocks); 61ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 62617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "Bad blocks count %u", 63ac27a0ecSDave Kleikamp input->blocks_count); 64ac27a0ecSDave Kleikamp else if (!(bh = sb_bread(sb, end - 1))) 65617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 662ae02107SMingming Cao "Cannot read last block (%llu)", 67ac27a0ecSDave Kleikamp end - 1); 68ac27a0ecSDave Kleikamp else if (outside(input->block_bitmap, start, end)) 69617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 70bd81d8eeSLaurent Vivier "Block bitmap not in group (block %llu)", 711939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 72ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 73617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 74bd81d8eeSLaurent Vivier "Inode bitmap not in group (block %llu)", 751939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 76ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 77ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 78617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 79bd81d8eeSLaurent Vivier "Inode table not in group (blocks %llu-%llu)", 801939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 81ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 82617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 83bd81d8eeSLaurent Vivier "Block bitmap same as inode bitmap (%llu)", 841939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 85ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 86617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 87bd81d8eeSLaurent Vivier "Block bitmap (%llu) in inode table (%llu-%llu)", 881939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 891939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 90ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 91617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 92bd81d8eeSLaurent Vivier "Inode bitmap (%llu) in inode table (%llu-%llu)", 931939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 941939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 95ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 96617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 97bd81d8eeSLaurent Vivier "Block bitmap (%llu) in GDT table" 982ae02107SMingming Cao " (%llu-%llu)", 991939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1001939e49aSRandy Dunlap start, metaend - 1); 101ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 102617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 103bd81d8eeSLaurent Vivier "Inode bitmap (%llu) in GDT table" 1042ae02107SMingming Cao " (%llu-%llu)", 1051939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1061939e49aSRandy Dunlap start, metaend - 1); 107ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 108ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 109617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 1102ae02107SMingming Cao "Inode table (%llu-%llu) overlaps" 1112ae02107SMingming Cao "GDT table (%llu-%llu)", 1121939e49aSRandy Dunlap (unsigned long long)input->inode_table, 1131939e49aSRandy Dunlap itend - 1, start, metaend - 1); 114ac27a0ecSDave Kleikamp else 115ac27a0ecSDave Kleikamp err = 0; 116ac27a0ecSDave Kleikamp brelse(bh); 117ac27a0ecSDave Kleikamp 118ac27a0ecSDave Kleikamp return err; 119ac27a0ecSDave Kleikamp } 120ac27a0ecSDave Kleikamp 121ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 122617ba13bSMingming Cao ext4_fsblk_t blk) 123ac27a0ecSDave Kleikamp { 124ac27a0ecSDave Kleikamp struct buffer_head *bh; 125ac27a0ecSDave Kleikamp int err; 126ac27a0ecSDave Kleikamp 127ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 128ac27a0ecSDave Kleikamp if (!bh) 129ac27a0ecSDave Kleikamp return ERR_PTR(-EIO); 130617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 131ac27a0ecSDave Kleikamp brelse(bh); 132ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 133ac27a0ecSDave Kleikamp } else { 134ac27a0ecSDave Kleikamp lock_buffer(bh); 135ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 136ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 137ac27a0ecSDave Kleikamp unlock_buffer(bh); 138ac27a0ecSDave Kleikamp } 139ac27a0ecSDave Kleikamp 140ac27a0ecSDave Kleikamp return bh; 141ac27a0ecSDave Kleikamp } 142ac27a0ecSDave Kleikamp 143ac27a0ecSDave Kleikamp /* 14414904107SEric Sandeen * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA. 14514904107SEric Sandeen * If that fails, restart the transaction & regain write access for the 14614904107SEric Sandeen * buffer head which is used for block_bitmap modifications. 14714904107SEric Sandeen */ 14814904107SEric Sandeen static int extend_or_restart_transaction(handle_t *handle, int thresh, 14914904107SEric Sandeen struct buffer_head *bh) 15014904107SEric Sandeen { 15114904107SEric Sandeen int err; 15214904107SEric Sandeen 15314904107SEric Sandeen if (handle->h_buffer_credits >= thresh) 15414904107SEric Sandeen return 0; 15514904107SEric Sandeen 15614904107SEric Sandeen err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA); 15714904107SEric Sandeen if (err < 0) 15814904107SEric Sandeen return err; 15914904107SEric Sandeen if (err) { 16014904107SEric Sandeen if ((err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 16114904107SEric Sandeen return err; 16214904107SEric Sandeen if ((err = ext4_journal_get_write_access(handle, bh))) 16314904107SEric Sandeen return err; 16414904107SEric Sandeen } 16514904107SEric Sandeen 16614904107SEric Sandeen return 0; 16714904107SEric Sandeen } 16814904107SEric Sandeen 16914904107SEric Sandeen /* 170ac27a0ecSDave Kleikamp * Set up the block and inode bitmaps, and the inode table for the new group. 171ac27a0ecSDave Kleikamp * This doesn't need to be part of the main transaction, since we are only 172ac27a0ecSDave Kleikamp * changing blocks outside the actual filesystem. We still do journaling to 173ac27a0ecSDave Kleikamp * ensure the recovery is correct in case of a failure just after resize. 174ac27a0ecSDave Kleikamp * If any part of this fails, we simply abort the resize. 175ac27a0ecSDave Kleikamp */ 176ac27a0ecSDave Kleikamp static int setup_new_group_blocks(struct super_block *sb, 177617ba13bSMingming Cao struct ext4_new_group_data *input) 178ac27a0ecSDave Kleikamp { 179617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 180617ba13bSMingming Cao ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group); 181617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 182ac27a0ecSDave Kleikamp le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0; 183617ba13bSMingming Cao unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group); 184ac27a0ecSDave Kleikamp struct buffer_head *bh; 185ac27a0ecSDave Kleikamp handle_t *handle; 186617ba13bSMingming Cao ext4_fsblk_t block; 187617ba13bSMingming Cao ext4_grpblk_t bit; 188ac27a0ecSDave Kleikamp int i; 189ac27a0ecSDave Kleikamp int err = 0, err2; 190ac27a0ecSDave Kleikamp 19114904107SEric Sandeen /* This transaction may be extended/restarted along the way */ 19214904107SEric Sandeen handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 19314904107SEric Sandeen 194ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 195ac27a0ecSDave Kleikamp return PTR_ERR(handle); 196ac27a0ecSDave Kleikamp 197ac27a0ecSDave Kleikamp lock_super(sb); 198ac27a0ecSDave Kleikamp if (input->group != sbi->s_groups_count) { 199ac27a0ecSDave Kleikamp err = -EBUSY; 200ac27a0ecSDave Kleikamp goto exit_journal; 201ac27a0ecSDave Kleikamp } 202ac27a0ecSDave Kleikamp 203ac27a0ecSDave Kleikamp if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) { 204ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 205ac27a0ecSDave Kleikamp goto exit_journal; 206ac27a0ecSDave Kleikamp } 207ac27a0ecSDave Kleikamp 208617ba13bSMingming Cao if (ext4_bg_has_super(sb, input->group)) { 209617ba13bSMingming Cao ext4_debug("mark backup superblock %#04lx (+0)\n", start); 210617ba13bSMingming Cao ext4_set_bit(0, bh->b_data); 211ac27a0ecSDave Kleikamp } 212ac27a0ecSDave Kleikamp 213ac27a0ecSDave Kleikamp /* Copy all of the GDT blocks into the backup in this group */ 214ac27a0ecSDave Kleikamp for (i = 0, bit = 1, block = start + 1; 215ac27a0ecSDave Kleikamp i < gdblocks; i++, block++, bit++) { 216ac27a0ecSDave Kleikamp struct buffer_head *gdb; 217ac27a0ecSDave Kleikamp 218617ba13bSMingming Cao ext4_debug("update backup group %#04lx (+%d)\n", block, bit); 219ac27a0ecSDave Kleikamp 22014904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 1, bh))) 22114904107SEric Sandeen goto exit_bh; 22214904107SEric Sandeen 223ac27a0ecSDave Kleikamp gdb = sb_getblk(sb, block); 224ac27a0ecSDave Kleikamp if (!gdb) { 225ac27a0ecSDave Kleikamp err = -EIO; 226ac27a0ecSDave Kleikamp goto exit_bh; 227ac27a0ecSDave Kleikamp } 228617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, gdb))) { 229ac27a0ecSDave Kleikamp brelse(gdb); 230ac27a0ecSDave Kleikamp goto exit_bh; 231ac27a0ecSDave Kleikamp } 2325b615287SEric Sandeen lock_buffer(gdb); 2335b615287SEric Sandeen memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size); 234ac27a0ecSDave Kleikamp set_buffer_uptodate(gdb); 2355b615287SEric Sandeen unlock_buffer(gdb); 236617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, gdb); 237617ba13bSMingming Cao ext4_set_bit(bit, bh->b_data); 238ac27a0ecSDave Kleikamp brelse(gdb); 239ac27a0ecSDave Kleikamp } 240ac27a0ecSDave Kleikamp 241ac27a0ecSDave Kleikamp /* Zero out all of the reserved backup group descriptor table blocks */ 242ac27a0ecSDave Kleikamp for (i = 0, bit = gdblocks + 1, block = start + bit; 243ac27a0ecSDave Kleikamp i < reserved_gdb; i++, block++, bit++) { 244ac27a0ecSDave Kleikamp struct buffer_head *gdb; 245ac27a0ecSDave Kleikamp 246617ba13bSMingming Cao ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit); 247ac27a0ecSDave Kleikamp 24814904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 1, bh))) 24914904107SEric Sandeen goto exit_bh; 25014904107SEric Sandeen 251ac27a0ecSDave Kleikamp if (IS_ERR(gdb = bclean(handle, sb, block))) { 252ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 253ac27a0ecSDave Kleikamp goto exit_bh; 254ac27a0ecSDave Kleikamp } 255617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, gdb); 256617ba13bSMingming Cao ext4_set_bit(bit, bh->b_data); 257ac27a0ecSDave Kleikamp brelse(gdb); 258ac27a0ecSDave Kleikamp } 259617ba13bSMingming Cao ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap, 260ac27a0ecSDave Kleikamp input->block_bitmap - start); 261617ba13bSMingming Cao ext4_set_bit(input->block_bitmap - start, bh->b_data); 262617ba13bSMingming Cao ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap, 263ac27a0ecSDave Kleikamp input->inode_bitmap - start); 264617ba13bSMingming Cao ext4_set_bit(input->inode_bitmap - start, bh->b_data); 265ac27a0ecSDave Kleikamp 266ac27a0ecSDave Kleikamp /* Zero out all of the inode table blocks */ 267ac27a0ecSDave Kleikamp for (i = 0, block = input->inode_table, bit = block - start; 268ac27a0ecSDave Kleikamp i < sbi->s_itb_per_group; i++, bit++, block++) { 269ac27a0ecSDave Kleikamp struct buffer_head *it; 270ac27a0ecSDave Kleikamp 271617ba13bSMingming Cao ext4_debug("clear inode block %#04lx (+%d)\n", block, bit); 27214904107SEric Sandeen 27314904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 1, bh))) 27414904107SEric Sandeen goto exit_bh; 27514904107SEric Sandeen 276ac27a0ecSDave Kleikamp if (IS_ERR(it = bclean(handle, sb, block))) { 277ac27a0ecSDave Kleikamp err = PTR_ERR(it); 278ac27a0ecSDave Kleikamp goto exit_bh; 279ac27a0ecSDave Kleikamp } 280617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, it); 281ac27a0ecSDave Kleikamp brelse(it); 282617ba13bSMingming Cao ext4_set_bit(bit, bh->b_data); 283ac27a0ecSDave Kleikamp } 28414904107SEric Sandeen 28514904107SEric Sandeen if ((err = extend_or_restart_transaction(handle, 2, bh))) 28614904107SEric Sandeen goto exit_bh; 28714904107SEric Sandeen 288617ba13bSMingming Cao mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb), 289ac27a0ecSDave Kleikamp bh->b_data); 290617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, bh); 291ac27a0ecSDave Kleikamp brelse(bh); 292ac27a0ecSDave Kleikamp 293ac27a0ecSDave Kleikamp /* Mark unused entries in inode bitmap used */ 294617ba13bSMingming Cao ext4_debug("clear inode bitmap %#04x (+%ld)\n", 295ac27a0ecSDave Kleikamp input->inode_bitmap, input->inode_bitmap - start); 296ac27a0ecSDave Kleikamp if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) { 297ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 298ac27a0ecSDave Kleikamp goto exit_journal; 299ac27a0ecSDave Kleikamp } 300ac27a0ecSDave Kleikamp 301617ba13bSMingming Cao mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), 302ac27a0ecSDave Kleikamp bh->b_data); 303617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, bh); 304ac27a0ecSDave Kleikamp exit_bh: 305ac27a0ecSDave Kleikamp brelse(bh); 306ac27a0ecSDave Kleikamp 307ac27a0ecSDave Kleikamp exit_journal: 308ac27a0ecSDave Kleikamp unlock_super(sb); 309617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 310ac27a0ecSDave Kleikamp err = err2; 311ac27a0ecSDave Kleikamp 312ac27a0ecSDave Kleikamp return err; 313ac27a0ecSDave Kleikamp } 314ac27a0ecSDave Kleikamp 315ac27a0ecSDave Kleikamp /* 316ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 317617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 318ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 319ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 320ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 321ac27a0ecSDave Kleikamp */ 322617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 323ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 324ac27a0ecSDave Kleikamp { 325ac27a0ecSDave Kleikamp unsigned *min = three; 326ac27a0ecSDave Kleikamp int mult = 3; 327ac27a0ecSDave Kleikamp unsigned ret; 328ac27a0ecSDave Kleikamp 329617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 330617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 331ac27a0ecSDave Kleikamp ret = *min; 332ac27a0ecSDave Kleikamp *min += 1; 333ac27a0ecSDave Kleikamp return ret; 334ac27a0ecSDave Kleikamp } 335ac27a0ecSDave Kleikamp 336ac27a0ecSDave Kleikamp if (*five < *min) { 337ac27a0ecSDave Kleikamp min = five; 338ac27a0ecSDave Kleikamp mult = 5; 339ac27a0ecSDave Kleikamp } 340ac27a0ecSDave Kleikamp if (*seven < *min) { 341ac27a0ecSDave Kleikamp min = seven; 342ac27a0ecSDave Kleikamp mult = 7; 343ac27a0ecSDave Kleikamp } 344ac27a0ecSDave Kleikamp 345ac27a0ecSDave Kleikamp ret = *min; 346ac27a0ecSDave Kleikamp *min *= mult; 347ac27a0ecSDave Kleikamp 348ac27a0ecSDave Kleikamp return ret; 349ac27a0ecSDave Kleikamp } 350ac27a0ecSDave Kleikamp 351ac27a0ecSDave Kleikamp /* 352ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 353ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 354ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 355ac27a0ecSDave Kleikamp */ 356ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 357ac27a0ecSDave Kleikamp struct buffer_head *primary) 358ac27a0ecSDave Kleikamp { 359617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 360*fd2d4291SAvantika Mathur const ext4_group_t end = EXT4_SB(sb)->s_groups_count; 361ac27a0ecSDave Kleikamp unsigned three = 1; 362ac27a0ecSDave Kleikamp unsigned five = 5; 363ac27a0ecSDave Kleikamp unsigned seven = 7; 364ac27a0ecSDave Kleikamp unsigned grp; 365ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 366ac27a0ecSDave Kleikamp int gdbackups = 0; 367ac27a0ecSDave Kleikamp 368617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 369bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 370bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 371617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 3722ae02107SMingming Cao "reserved GDT %llu" 3732ae02107SMingming Cao " missing grp %d (%llu)", 374ac27a0ecSDave Kleikamp blk, grp, 375bd81d8eeSLaurent Vivier grp * 376bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 377bd81d8eeSLaurent Vivier blk); 378ac27a0ecSDave Kleikamp return -EINVAL; 379ac27a0ecSDave Kleikamp } 380617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 381ac27a0ecSDave Kleikamp return -EFBIG; 382ac27a0ecSDave Kleikamp } 383ac27a0ecSDave Kleikamp 384ac27a0ecSDave Kleikamp return gdbackups; 385ac27a0ecSDave Kleikamp } 386ac27a0ecSDave Kleikamp 387ac27a0ecSDave Kleikamp /* 388ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 389ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 390ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 391ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 392ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 393ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 394ac27a0ecSDave Kleikamp * 395ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 396ac27a0ecSDave Kleikamp * 397ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 398ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 399ac27a0ecSDave Kleikamp */ 400ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 401617ba13bSMingming Cao struct ext4_new_group_data *input, 402ac27a0ecSDave Kleikamp struct buffer_head **primary) 403ac27a0ecSDave Kleikamp { 404ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 405617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 406617ba13bSMingming Cao unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); 407617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 408ac27a0ecSDave Kleikamp struct buffer_head **o_group_desc, **n_group_desc; 409ac27a0ecSDave Kleikamp struct buffer_head *dind; 410ac27a0ecSDave Kleikamp int gdbackups; 411617ba13bSMingming Cao struct ext4_iloc iloc; 412ac27a0ecSDave Kleikamp __le32 *data; 413ac27a0ecSDave Kleikamp int err; 414ac27a0ecSDave Kleikamp 415ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 416ac27a0ecSDave Kleikamp printk(KERN_DEBUG 417617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 418ac27a0ecSDave Kleikamp gdb_num); 419ac27a0ecSDave Kleikamp 420ac27a0ecSDave Kleikamp /* 421ac27a0ecSDave Kleikamp * If we are not using the primary superblock/GDT copy don't resize, 422ac27a0ecSDave Kleikamp * because the user tools have no way of handling this. Probably a 423ac27a0ecSDave Kleikamp * bad time to do it anyways. 424ac27a0ecSDave Kleikamp */ 425617ba13bSMingming Cao if (EXT4_SB(sb)->s_sbh->b_blocknr != 426617ba13bSMingming Cao le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 427617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 428ac27a0ecSDave Kleikamp "won't resize using backup superblock at %llu", 429617ba13bSMingming Cao (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 430ac27a0ecSDave Kleikamp return -EPERM; 431ac27a0ecSDave Kleikamp } 432ac27a0ecSDave Kleikamp 433ac27a0ecSDave Kleikamp *primary = sb_bread(sb, gdblock); 434ac27a0ecSDave Kleikamp if (!*primary) 435ac27a0ecSDave Kleikamp return -EIO; 436ac27a0ecSDave Kleikamp 437ac27a0ecSDave Kleikamp if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) { 438ac27a0ecSDave Kleikamp err = gdbackups; 439ac27a0ecSDave Kleikamp goto exit_bh; 440ac27a0ecSDave Kleikamp } 441ac27a0ecSDave Kleikamp 442617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 443ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 444ac27a0ecSDave Kleikamp if (!dind) { 445ac27a0ecSDave Kleikamp err = -EIO; 446ac27a0ecSDave Kleikamp goto exit_bh; 447ac27a0ecSDave Kleikamp } 448ac27a0ecSDave Kleikamp 449ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 450617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 451617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 4522ae02107SMingming Cao "new group %u GDT block %llu not reserved", 453ac27a0ecSDave Kleikamp input->group, gdblock); 454ac27a0ecSDave Kleikamp err = -EINVAL; 455ac27a0ecSDave Kleikamp goto exit_dind; 456ac27a0ecSDave Kleikamp } 457ac27a0ecSDave Kleikamp 458617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh))) 459ac27a0ecSDave Kleikamp goto exit_dind; 460ac27a0ecSDave Kleikamp 461617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, *primary))) 462ac27a0ecSDave Kleikamp goto exit_sbh; 463ac27a0ecSDave Kleikamp 464617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, dind))) 465ac27a0ecSDave Kleikamp goto exit_primary; 466ac27a0ecSDave Kleikamp 467617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 468617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 469ac27a0ecSDave Kleikamp goto exit_dindj; 470ac27a0ecSDave Kleikamp 471ac27a0ecSDave Kleikamp n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *), 472ac27a0ecSDave Kleikamp GFP_KERNEL); 473ac27a0ecSDave Kleikamp if (!n_group_desc) { 474ac27a0ecSDave Kleikamp err = -ENOMEM; 475617ba13bSMingming Cao ext4_warning (sb, __FUNCTION__, 476ac27a0ecSDave Kleikamp "not enough memory for %lu groups", gdb_num + 1); 477ac27a0ecSDave Kleikamp goto exit_inode; 478ac27a0ecSDave Kleikamp } 479ac27a0ecSDave Kleikamp 480ac27a0ecSDave Kleikamp /* 481ac27a0ecSDave Kleikamp * Finally, we have all of the possible failures behind us... 482ac27a0ecSDave Kleikamp * 483ac27a0ecSDave Kleikamp * Remove new GDT block from inode double-indirect block and clear out 484ac27a0ecSDave Kleikamp * the new GDT block for use (which also "frees" the backup GDT blocks 485ac27a0ecSDave Kleikamp * from the reserved inode). We don't need to change the bitmaps for 486ac27a0ecSDave Kleikamp * these blocks, because they are marked as in-use from being in the 487ac27a0ecSDave Kleikamp * reserved inode, and will become GDT blocks (primary and backup). 488ac27a0ecSDave Kleikamp */ 489617ba13bSMingming Cao data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; 490617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, dind); 491ac27a0ecSDave Kleikamp brelse(dind); 492ac27a0ecSDave Kleikamp inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; 493617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 494ac27a0ecSDave Kleikamp memset((*primary)->b_data, 0, sb->s_blocksize); 495617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, *primary); 496ac27a0ecSDave Kleikamp 497617ba13bSMingming Cao o_group_desc = EXT4_SB(sb)->s_group_desc; 498ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 499617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 500ac27a0ecSDave Kleikamp n_group_desc[gdb_num] = *primary; 501617ba13bSMingming Cao EXT4_SB(sb)->s_group_desc = n_group_desc; 502617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 503ac27a0ecSDave Kleikamp kfree(o_group_desc); 504ac27a0ecSDave Kleikamp 505ac27a0ecSDave Kleikamp es->s_reserved_gdt_blocks = 506ac27a0ecSDave Kleikamp cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1); 507617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); 508ac27a0ecSDave Kleikamp 509ac27a0ecSDave Kleikamp return 0; 510ac27a0ecSDave Kleikamp 511ac27a0ecSDave Kleikamp exit_inode: 512617ba13bSMingming Cao //ext4_journal_release_buffer(handle, iloc.bh); 513ac27a0ecSDave Kleikamp brelse(iloc.bh); 514ac27a0ecSDave Kleikamp exit_dindj: 515617ba13bSMingming Cao //ext4_journal_release_buffer(handle, dind); 516ac27a0ecSDave Kleikamp exit_primary: 517617ba13bSMingming Cao //ext4_journal_release_buffer(handle, *primary); 518ac27a0ecSDave Kleikamp exit_sbh: 519617ba13bSMingming Cao //ext4_journal_release_buffer(handle, *primary); 520ac27a0ecSDave Kleikamp exit_dind: 521ac27a0ecSDave Kleikamp brelse(dind); 522ac27a0ecSDave Kleikamp exit_bh: 523ac27a0ecSDave Kleikamp brelse(*primary); 524ac27a0ecSDave Kleikamp 525617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 526ac27a0ecSDave Kleikamp return err; 527ac27a0ecSDave Kleikamp } 528ac27a0ecSDave Kleikamp 529ac27a0ecSDave Kleikamp /* 530ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 531ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 532ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 533ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 534ac27a0ecSDave Kleikamp * 535ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 536ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 537ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 538ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 539ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 540ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 541ac27a0ecSDave Kleikamp */ 542ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 543617ba13bSMingming Cao struct ext4_new_group_data *input) 544ac27a0ecSDave Kleikamp { 545ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 546617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 547ac27a0ecSDave Kleikamp struct buffer_head **primary; 548ac27a0ecSDave Kleikamp struct buffer_head *dind; 549617ba13bSMingming Cao struct ext4_iloc iloc; 550617ba13bSMingming Cao ext4_fsblk_t blk; 551ac27a0ecSDave Kleikamp __le32 *data, *end; 552ac27a0ecSDave Kleikamp int gdbackups = 0; 553ac27a0ecSDave Kleikamp int res, i; 554ac27a0ecSDave Kleikamp int err; 555ac27a0ecSDave Kleikamp 556ac27a0ecSDave Kleikamp primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL); 557ac27a0ecSDave Kleikamp if (!primary) 558ac27a0ecSDave Kleikamp return -ENOMEM; 559ac27a0ecSDave Kleikamp 560617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 561ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 562ac27a0ecSDave Kleikamp if (!dind) { 563ac27a0ecSDave Kleikamp err = -EIO; 564ac27a0ecSDave Kleikamp goto exit_free; 565ac27a0ecSDave Kleikamp } 566ac27a0ecSDave Kleikamp 567617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 568617ba13bSMingming Cao data = (__le32 *)dind->b_data + EXT4_SB(sb)->s_gdb_count; 569617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 570ac27a0ecSDave Kleikamp 571ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 572ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 573ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 574617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 5752ae02107SMingming Cao "reserved block %llu" 576ac27a0ecSDave Kleikamp " not at offset %ld", 577ac27a0ecSDave Kleikamp blk, 578ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 579ac27a0ecSDave Kleikamp err = -EINVAL; 580ac27a0ecSDave Kleikamp goto exit_bh; 581ac27a0ecSDave Kleikamp } 582ac27a0ecSDave Kleikamp primary[res] = sb_bread(sb, blk); 583ac27a0ecSDave Kleikamp if (!primary[res]) { 584ac27a0ecSDave Kleikamp err = -EIO; 585ac27a0ecSDave Kleikamp goto exit_bh; 586ac27a0ecSDave Kleikamp } 587ac27a0ecSDave Kleikamp if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) { 588ac27a0ecSDave Kleikamp brelse(primary[res]); 589ac27a0ecSDave Kleikamp err = gdbackups; 590ac27a0ecSDave Kleikamp goto exit_bh; 591ac27a0ecSDave Kleikamp } 592ac27a0ecSDave Kleikamp if (++data >= end) 593ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 594ac27a0ecSDave Kleikamp } 595ac27a0ecSDave Kleikamp 596ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 597617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary[i]))) { 598ac27a0ecSDave Kleikamp /* 599ac27a0ecSDave Kleikamp int j; 600ac27a0ecSDave Kleikamp for (j = 0; j < i; j++) 601617ba13bSMingming Cao ext4_journal_release_buffer(handle, primary[j]); 602ac27a0ecSDave Kleikamp */ 603ac27a0ecSDave Kleikamp goto exit_bh; 604ac27a0ecSDave Kleikamp } 605ac27a0ecSDave Kleikamp } 606ac27a0ecSDave Kleikamp 607617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 608ac27a0ecSDave Kleikamp goto exit_bh; 609ac27a0ecSDave Kleikamp 610ac27a0ecSDave Kleikamp /* 611ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 612ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 613ac27a0ecSDave Kleikamp */ 614617ba13bSMingming Cao blk = input->group * EXT4_BLOCKS_PER_GROUP(sb); 615ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 616ac27a0ecSDave Kleikamp int err2; 617ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 618ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 619ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 620ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 621ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 622617ba13bSMingming Cao err2 = ext4_journal_dirty_metadata(handle, primary[i]); 623ac27a0ecSDave Kleikamp if (!err) 624ac27a0ecSDave Kleikamp err = err2; 625ac27a0ecSDave Kleikamp } 626ac27a0ecSDave Kleikamp inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; 627617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 628ac27a0ecSDave Kleikamp 629ac27a0ecSDave Kleikamp exit_bh: 630ac27a0ecSDave Kleikamp while (--res >= 0) 631ac27a0ecSDave Kleikamp brelse(primary[res]); 632ac27a0ecSDave Kleikamp brelse(dind); 633ac27a0ecSDave Kleikamp 634ac27a0ecSDave Kleikamp exit_free: 635ac27a0ecSDave Kleikamp kfree(primary); 636ac27a0ecSDave Kleikamp 637ac27a0ecSDave Kleikamp return err; 638ac27a0ecSDave Kleikamp } 639ac27a0ecSDave Kleikamp 640ac27a0ecSDave Kleikamp /* 641617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 642ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 643ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 644ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 645ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 646ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 647ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 648ac27a0ecSDave Kleikamp * 649ac27a0ecSDave Kleikamp * We do not need lock_super() for this, because these blocks are not 650ac27a0ecSDave Kleikamp * otherwise touched by the filesystem code when it is mounted. We don't 651ac27a0ecSDave Kleikamp * need to worry about last changing from sbi->s_groups_count, because the 652ac27a0ecSDave Kleikamp * worst that can happen is that we do not copy the full number of backups 653ac27a0ecSDave Kleikamp * at this time. The resize which changed s_groups_count will backup again. 654ac27a0ecSDave Kleikamp */ 655ac27a0ecSDave Kleikamp static void update_backups(struct super_block *sb, 656ac27a0ecSDave Kleikamp int blk_off, char *data, int size) 657ac27a0ecSDave Kleikamp { 658617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 659*fd2d4291SAvantika Mathur const ext4_group_t last = sbi->s_groups_count; 660617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 661ac27a0ecSDave Kleikamp unsigned three = 1; 662ac27a0ecSDave Kleikamp unsigned five = 5; 663ac27a0ecSDave Kleikamp unsigned seven = 7; 664*fd2d4291SAvantika Mathur ext4_group_t group; 665ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 666ac27a0ecSDave Kleikamp handle_t *handle; 667ac27a0ecSDave Kleikamp int err = 0, err2; 668ac27a0ecSDave Kleikamp 669617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 670ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 671ac27a0ecSDave Kleikamp group = 1; 672ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 673ac27a0ecSDave Kleikamp goto exit_err; 674ac27a0ecSDave Kleikamp } 675ac27a0ecSDave Kleikamp 676617ba13bSMingming Cao while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) { 677ac27a0ecSDave Kleikamp struct buffer_head *bh; 678ac27a0ecSDave Kleikamp 679ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 680ac27a0ecSDave Kleikamp if (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); 700617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, 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) { 718617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 719*fd2d4291SAvantika 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)) { 758617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 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)) { 765617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "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)) { 771617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "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, 777617ba13bSMingming Cao EXT4_FEATURE_COMPAT_RESIZE_INODE)){ 778617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 779ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 780ac27a0ecSDave Kleikamp return -EPERM; 781ac27a0ecSDave Kleikamp } 782617ba13bSMingming Cao inode = iget(sb, EXT4_RESIZE_INO); 783ac27a0ecSDave Kleikamp if (!inode || is_bad_inode(inode)) { 784617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 785ac27a0ecSDave Kleikamp "Error opening resize inode"); 786ac27a0ecSDave Kleikamp iput(inode); 787ac27a0ecSDave Kleikamp return -ENOENT; 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) { 814617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 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 */ 860617ba13bSMingming Cao gdp = (struct ext4_group_desc *)primary->b_data + gdb_off; 861ac27a0ecSDave Kleikamp 8628fadc143SAlexandre Ratchov ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */ 8638fadc143SAlexandre Ratchov ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */ 8648fadc143SAlexandre Ratchov ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */ 865ac27a0ecSDave Kleikamp gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count); 866617ba13bSMingming Cao gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb)); 867717d50e4SAndreas Dilger gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp); 868ac27a0ecSDave Kleikamp 869ac27a0ecSDave Kleikamp /* 870ac27a0ecSDave Kleikamp * Make the new blocks and inodes valid next. We do this before 871ac27a0ecSDave Kleikamp * increasing the group count so that once the group is enabled, 872ac27a0ecSDave Kleikamp * all of its blocks and inodes are already valid. 873ac27a0ecSDave Kleikamp * 874ac27a0ecSDave Kleikamp * We always allocate group-by-group, then block-by-block or 875ac27a0ecSDave Kleikamp * inode-by-inode within a group, so enabling these 876ac27a0ecSDave Kleikamp * blocks/inodes before the group is live won't actually let us 877ac27a0ecSDave Kleikamp * allocate the new space yet. 878ac27a0ecSDave Kleikamp */ 879bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, ext4_blocks_count(es) + 880ac27a0ecSDave Kleikamp input->blocks_count); 881ac27a0ecSDave Kleikamp es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) + 882617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb)); 883ac27a0ecSDave Kleikamp 884ac27a0ecSDave Kleikamp /* 885ac27a0ecSDave Kleikamp * We need to protect s_groups_count against other CPUs seeing 886ac27a0ecSDave Kleikamp * inconsistent state in the superblock. 887ac27a0ecSDave Kleikamp * 888ac27a0ecSDave Kleikamp * The precise rules we use are: 889ac27a0ecSDave Kleikamp * 890ac27a0ecSDave Kleikamp * * Writers of s_groups_count *must* hold lock_super 891ac27a0ecSDave Kleikamp * AND 892ac27a0ecSDave Kleikamp * * Writers must perform a smp_wmb() after updating all dependent 893ac27a0ecSDave Kleikamp * data and before modifying the groups count 894ac27a0ecSDave Kleikamp * 895ac27a0ecSDave Kleikamp * * Readers must hold lock_super() over the access 896ac27a0ecSDave Kleikamp * OR 897ac27a0ecSDave Kleikamp * * Readers must perform an smp_rmb() after reading the groups count 898ac27a0ecSDave Kleikamp * and before reading any dependent data. 899ac27a0ecSDave Kleikamp * 900ac27a0ecSDave Kleikamp * NB. These rules can be relaxed when checking the group count 901ac27a0ecSDave Kleikamp * while freeing data, as we can only allocate from a block 902ac27a0ecSDave Kleikamp * group after serialising against the group count, and we can 903ac27a0ecSDave Kleikamp * only then free after serialising in turn against that 904ac27a0ecSDave Kleikamp * allocation. 905ac27a0ecSDave Kleikamp */ 906ac27a0ecSDave Kleikamp smp_wmb(); 907ac27a0ecSDave Kleikamp 908ac27a0ecSDave Kleikamp /* Update the global fs size fields */ 909ac27a0ecSDave Kleikamp sbi->s_groups_count++; 910ac27a0ecSDave Kleikamp 911617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, primary); 912ac27a0ecSDave Kleikamp 913ac27a0ecSDave Kleikamp /* Update the reserved block counts only once the new group is 914ac27a0ecSDave Kleikamp * active. */ 915bd81d8eeSLaurent Vivier ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 916ac27a0ecSDave Kleikamp input->reserved_blocks); 917ac27a0ecSDave Kleikamp 918ac27a0ecSDave Kleikamp /* Update the free space counts */ 919aa0dff2dSPeter Zijlstra percpu_counter_add(&sbi->s_freeblocks_counter, 920ac27a0ecSDave Kleikamp input->free_blocks_count); 921aa0dff2dSPeter Zijlstra percpu_counter_add(&sbi->s_freeinodes_counter, 922617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb)); 923ac27a0ecSDave Kleikamp 924617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, sbi->s_sbh); 925ac27a0ecSDave Kleikamp sb->s_dirt = 1; 926ac27a0ecSDave Kleikamp 927ac27a0ecSDave Kleikamp exit_journal: 928ac27a0ecSDave Kleikamp unlock_super(sb); 929617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 930ac27a0ecSDave Kleikamp err = err2; 931ac27a0ecSDave Kleikamp if (!err) { 932ac27a0ecSDave Kleikamp update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 933617ba13bSMingming Cao sizeof(struct ext4_super_block)); 934ac27a0ecSDave Kleikamp update_backups(sb, primary->b_blocknr, primary->b_data, 935ac27a0ecSDave Kleikamp primary->b_size); 936ac27a0ecSDave Kleikamp } 937ac27a0ecSDave Kleikamp exit_put: 938ac27a0ecSDave Kleikamp iput(inode); 939ac27a0ecSDave Kleikamp return err; 940617ba13bSMingming Cao } /* ext4_group_add */ 941ac27a0ecSDave Kleikamp 942ac27a0ecSDave Kleikamp /* Extend the filesystem to the new number of blocks specified. This entry 943ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 944ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 945ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 946ac27a0ecSDave Kleikamp * 947617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 948ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 949ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 950ac27a0ecSDave Kleikamp */ 951617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 952617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 953ac27a0ecSDave Kleikamp { 954617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 955*fd2d4291SAvantika Mathur ext4_group_t o_groups_count; 956617ba13bSMingming Cao ext4_grpblk_t last; 957617ba13bSMingming Cao ext4_grpblk_t add; 958ac27a0ecSDave Kleikamp struct buffer_head * bh; 959ac27a0ecSDave Kleikamp handle_t *handle; 960ac27a0ecSDave Kleikamp int err; 961ac27a0ecSDave Kleikamp unsigned long freed_blocks; 962ac27a0ecSDave Kleikamp 963ac27a0ecSDave Kleikamp /* We don't need to worry about locking wrt other resizers just 964ac27a0ecSDave Kleikamp * yet: we're going to revalidate es->s_blocks_count after 965ac27a0ecSDave Kleikamp * taking lock_super() below. */ 966bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 967617ba13bSMingming Cao o_groups_count = EXT4_SB(sb)->s_groups_count; 968ac27a0ecSDave Kleikamp 969ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 9702ae02107SMingming Cao printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n", 971ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 972ac27a0ecSDave Kleikamp 973ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 974ac27a0ecSDave Kleikamp return 0; 975ac27a0ecSDave Kleikamp 976ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 977617ba13bSMingming Cao printk(KERN_ERR "EXT4-fs: filesystem on %s:" 9782ae02107SMingming Cao " too large to resize to %llu blocks safely\n", 979ac27a0ecSDave Kleikamp sb->s_id, n_blocks_count); 980ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 981617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 982ac27a0ecSDave Kleikamp "CONFIG_LBD not enabled\n"); 983ac27a0ecSDave Kleikamp return -EINVAL; 984ac27a0ecSDave Kleikamp } 985ac27a0ecSDave Kleikamp 986ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 987617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 988ac27a0ecSDave Kleikamp "can't shrink FS - resize aborted"); 989ac27a0ecSDave Kleikamp return -EBUSY; 990ac27a0ecSDave Kleikamp } 991ac27a0ecSDave Kleikamp 992ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 9933a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last); 994ac27a0ecSDave Kleikamp 995ac27a0ecSDave Kleikamp if (last == 0) { 996617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 997ac27a0ecSDave Kleikamp "need to use ext2online to resize further"); 998ac27a0ecSDave Kleikamp return -EPERM; 999ac27a0ecSDave Kleikamp } 1000ac27a0ecSDave Kleikamp 1001617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1002ac27a0ecSDave Kleikamp 1003ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 1004617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "blocks_count overflow"); 1005ac27a0ecSDave Kleikamp return -EINVAL; 1006ac27a0ecSDave Kleikamp } 1007ac27a0ecSDave Kleikamp 1008ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1009ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1010ac27a0ecSDave Kleikamp 1011ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 1012617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 10132ae02107SMingming Cao "will only finish group (%llu" 1014ac27a0ecSDave Kleikamp " blocks, %u new)", 1015ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1016ac27a0ecSDave Kleikamp 1017ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 1018ac27a0ecSDave Kleikamp bh = sb_bread(sb, o_blocks_count + add -1); 1019ac27a0ecSDave Kleikamp if (!bh) { 1020617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 1021ac27a0ecSDave Kleikamp "can't read last block, resize aborted"); 1022ac27a0ecSDave Kleikamp return -ENOSPC; 1023ac27a0ecSDave Kleikamp } 1024ac27a0ecSDave Kleikamp brelse(bh); 1025ac27a0ecSDave Kleikamp 1026ac27a0ecSDave Kleikamp /* We will update the superblock, one block bitmap, and 1027617ba13bSMingming Cao * one group descriptor via ext4_free_blocks(). 1028ac27a0ecSDave Kleikamp */ 1029617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 3); 1030ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1031ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 1032617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, "error %d on journal start",err); 1033ac27a0ecSDave Kleikamp goto exit_put; 1034ac27a0ecSDave Kleikamp } 1035ac27a0ecSDave Kleikamp 1036ac27a0ecSDave Kleikamp lock_super(sb); 1037bd81d8eeSLaurent Vivier if (o_blocks_count != ext4_blocks_count(es)) { 1038617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 1039ac27a0ecSDave Kleikamp "multiple resizers run on filesystem!"); 1040ac27a0ecSDave Kleikamp unlock_super(sb); 1041ac27a0ecSDave Kleikamp err = -EBUSY; 1042ac27a0ecSDave Kleikamp goto exit_put; 1043ac27a0ecSDave Kleikamp } 1044ac27a0ecSDave Kleikamp 1045617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, 1046617ba13bSMingming Cao EXT4_SB(sb)->s_sbh))) { 1047617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 1048ac27a0ecSDave Kleikamp "error %d on journal write access", err); 1049ac27a0ecSDave Kleikamp unlock_super(sb); 1050617ba13bSMingming Cao ext4_journal_stop(handle); 1051ac27a0ecSDave Kleikamp goto exit_put; 1052ac27a0ecSDave Kleikamp } 1053bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, o_blocks_count + add); 1054617ba13bSMingming Cao ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); 1055ac27a0ecSDave Kleikamp sb->s_dirt = 1; 1056ac27a0ecSDave Kleikamp unlock_super(sb); 10572ae02107SMingming Cao ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count, 1058ac27a0ecSDave Kleikamp o_blocks_count + add); 1059617ba13bSMingming Cao ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks); 10602ae02107SMingming Cao ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 1061ac27a0ecSDave Kleikamp o_blocks_count + add); 1062617ba13bSMingming Cao if ((err = ext4_journal_stop(handle))) 1063ac27a0ecSDave Kleikamp goto exit_put; 1064ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 1065bd81d8eeSLaurent Vivier printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n", 1066bd81d8eeSLaurent Vivier ext4_blocks_count(es)); 1067617ba13bSMingming Cao update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, 1068617ba13bSMingming Cao sizeof(struct ext4_super_block)); 1069ac27a0ecSDave Kleikamp exit_put: 1070ac27a0ecSDave Kleikamp return err; 1071617ba13bSMingming Cao } /* ext4_group_extend */ 1072