1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/resize.c 3ac27a0ecSDave Kleikamp * 4617ba13bSMingming Cao * Support for resizing an ext4 filesystem while it is mounted. 5ac27a0ecSDave Kleikamp * 6ac27a0ecSDave Kleikamp * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com> 7ac27a0ecSDave Kleikamp * 8ac27a0ecSDave Kleikamp * This could probably be made into a module, because it is not often in use. 9ac27a0ecSDave Kleikamp */ 10ac27a0ecSDave Kleikamp 11ac27a0ecSDave Kleikamp 12617ba13bSMingming Cao #define EXT4FS_DEBUG 13ac27a0ecSDave Kleikamp 14ac27a0ecSDave Kleikamp #include <linux/errno.h> 15ac27a0ecSDave Kleikamp #include <linux/slab.h> 16ac27a0ecSDave Kleikamp 173dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 18ac27a0ecSDave Kleikamp 198f82f840SYongqiang Yang int ext4_resize_begin(struct super_block *sb) 208f82f840SYongqiang Yang { 218f82f840SYongqiang Yang int ret = 0; 228f82f840SYongqiang Yang 238f82f840SYongqiang Yang if (!capable(CAP_SYS_RESOURCE)) 248f82f840SYongqiang Yang return -EPERM; 258f82f840SYongqiang Yang 26ce723c31SYongqiang Yang /* 27ce723c31SYongqiang Yang * We are not allowed to do online-resizing on a filesystem mounted 28ce723c31SYongqiang Yang * with error, because it can destroy the filesystem easily. 29ce723c31SYongqiang Yang */ 30ce723c31SYongqiang Yang if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 31ce723c31SYongqiang Yang ext4_warning(sb, "There are errors in the filesystem, " 32ce723c31SYongqiang Yang "so online resizing is not allowed\n"); 33ce723c31SYongqiang Yang return -EPERM; 34ce723c31SYongqiang Yang } 35ce723c31SYongqiang Yang 368f82f840SYongqiang Yang if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags)) 378f82f840SYongqiang Yang ret = -EBUSY; 388f82f840SYongqiang Yang 398f82f840SYongqiang Yang return ret; 408f82f840SYongqiang Yang } 418f82f840SYongqiang Yang 428f82f840SYongqiang Yang void ext4_resize_end(struct super_block *sb) 438f82f840SYongqiang Yang { 448f82f840SYongqiang Yang clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags); 458f82f840SYongqiang Yang smp_mb__after_clear_bit(); 468f82f840SYongqiang Yang } 478f82f840SYongqiang Yang 48ac27a0ecSDave Kleikamp #define outside(b, first, last) ((b) < (first) || (b) >= (last)) 49ac27a0ecSDave Kleikamp #define inside(b, first, last) ((b) >= (first) && (b) < (last)) 50ac27a0ecSDave Kleikamp 51ac27a0ecSDave Kleikamp static int verify_group_input(struct super_block *sb, 52617ba13bSMingming Cao struct ext4_new_group_data *input) 53ac27a0ecSDave Kleikamp { 54617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 55617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 56bd81d8eeSLaurent Vivier ext4_fsblk_t start = ext4_blocks_count(es); 57617ba13bSMingming Cao ext4_fsblk_t end = start + input->blocks_count; 58fd2d4291SAvantika Mathur ext4_group_t group = input->group; 59617ba13bSMingming Cao ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; 60617ba13bSMingming Cao unsigned overhead = ext4_bg_has_super(sb, group) ? 61617ba13bSMingming Cao (1 + ext4_bg_num_gdb(sb, group) + 62ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks)) : 0; 63617ba13bSMingming Cao ext4_fsblk_t metaend = start + overhead; 64ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 653a5b2ecdSMingming Cao ext4_grpblk_t free_blocks_count, offset; 66ac27a0ecSDave Kleikamp int err = -EINVAL; 67ac27a0ecSDave Kleikamp 68ac27a0ecSDave Kleikamp input->free_blocks_count = free_blocks_count = 69ac27a0ecSDave Kleikamp input->blocks_count - 2 - overhead - sbi->s_itb_per_group; 70ac27a0ecSDave Kleikamp 71ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 72617ba13bSMingming Cao printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " 73ac27a0ecSDave Kleikamp "(%d free, %u reserved)\n", 74617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? "normal" : 75ac27a0ecSDave Kleikamp "no-super", input->group, input->blocks_count, 76ac27a0ecSDave Kleikamp free_blocks_count, input->reserved_blocks); 77ac27a0ecSDave Kleikamp 783a5b2ecdSMingming Cao ext4_get_group_no_and_offset(sb, start, NULL, &offset); 79ac27a0ecSDave Kleikamp if (group != sbi->s_groups_count) 8012062dddSEric Sandeen ext4_warning(sb, "Cannot add at group %u (only %u groups)", 81ac27a0ecSDave Kleikamp input->group, sbi->s_groups_count); 823a5b2ecdSMingming Cao else if (offset != 0) 8312062dddSEric Sandeen ext4_warning(sb, "Last group not full"); 84ac27a0ecSDave Kleikamp else if (input->reserved_blocks > input->blocks_count / 5) 8512062dddSEric Sandeen ext4_warning(sb, "Reserved blocks too high (%u)", 86ac27a0ecSDave Kleikamp input->reserved_blocks); 87ac27a0ecSDave Kleikamp else if (free_blocks_count < 0) 8812062dddSEric Sandeen ext4_warning(sb, "Bad blocks count %u", 89ac27a0ecSDave Kleikamp input->blocks_count); 90ac27a0ecSDave Kleikamp else if (!(bh = sb_bread(sb, end - 1))) 9112062dddSEric Sandeen ext4_warning(sb, "Cannot read last block (%llu)", 92ac27a0ecSDave Kleikamp end - 1); 93ac27a0ecSDave Kleikamp else if (outside(input->block_bitmap, start, end)) 9412062dddSEric Sandeen ext4_warning(sb, "Block bitmap not in group (block %llu)", 951939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 96ac27a0ecSDave Kleikamp else if (outside(input->inode_bitmap, start, end)) 9712062dddSEric Sandeen ext4_warning(sb, "Inode bitmap not in group (block %llu)", 981939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap); 99ac27a0ecSDave Kleikamp else if (outside(input->inode_table, start, end) || 100ac27a0ecSDave Kleikamp outside(itend - 1, start, end)) 10112062dddSEric Sandeen ext4_warning(sb, "Inode table not in group (blocks %llu-%llu)", 1021939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 103ac27a0ecSDave Kleikamp else if (input->inode_bitmap == input->block_bitmap) 10412062dddSEric Sandeen ext4_warning(sb, "Block bitmap same as inode bitmap (%llu)", 1051939e49aSRandy Dunlap (unsigned long long)input->block_bitmap); 106ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, input->inode_table, itend)) 10712062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in inode table " 10812062dddSEric Sandeen "(%llu-%llu)", 1091939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1101939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 111ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, input->inode_table, itend)) 11212062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in inode table " 11312062dddSEric Sandeen "(%llu-%llu)", 1141939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1151939e49aSRandy Dunlap (unsigned long long)input->inode_table, itend - 1); 116ac27a0ecSDave Kleikamp else if (inside(input->block_bitmap, start, metaend)) 11712062dddSEric Sandeen ext4_warning(sb, "Block bitmap (%llu) in GDT table (%llu-%llu)", 1181939e49aSRandy Dunlap (unsigned long long)input->block_bitmap, 1191939e49aSRandy Dunlap start, metaend - 1); 120ac27a0ecSDave Kleikamp else if (inside(input->inode_bitmap, start, metaend)) 12112062dddSEric Sandeen ext4_warning(sb, "Inode bitmap (%llu) in GDT table (%llu-%llu)", 1221939e49aSRandy Dunlap (unsigned long long)input->inode_bitmap, 1231939e49aSRandy Dunlap start, metaend - 1); 124ac27a0ecSDave Kleikamp else if (inside(input->inode_table, start, metaend) || 125ac27a0ecSDave Kleikamp inside(itend - 1, start, metaend)) 12612062dddSEric Sandeen ext4_warning(sb, "Inode table (%llu-%llu) overlaps GDT table " 12712062dddSEric Sandeen "(%llu-%llu)", 1281939e49aSRandy Dunlap (unsigned long long)input->inode_table, 1291939e49aSRandy Dunlap itend - 1, start, metaend - 1); 130ac27a0ecSDave Kleikamp else 131ac27a0ecSDave Kleikamp err = 0; 132ac27a0ecSDave Kleikamp brelse(bh); 133ac27a0ecSDave Kleikamp 134ac27a0ecSDave Kleikamp return err; 135ac27a0ecSDave Kleikamp } 136ac27a0ecSDave Kleikamp 137ac27a0ecSDave Kleikamp static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, 138617ba13bSMingming Cao ext4_fsblk_t blk) 139ac27a0ecSDave Kleikamp { 140ac27a0ecSDave Kleikamp struct buffer_head *bh; 141ac27a0ecSDave Kleikamp int err; 142ac27a0ecSDave Kleikamp 143ac27a0ecSDave Kleikamp bh = sb_getblk(sb, blk); 144ac27a0ecSDave Kleikamp if (!bh) 145ac27a0ecSDave Kleikamp return ERR_PTR(-EIO); 146617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) { 147ac27a0ecSDave Kleikamp brelse(bh); 148ac27a0ecSDave Kleikamp bh = ERR_PTR(err); 149ac27a0ecSDave Kleikamp } else { 150ac27a0ecSDave Kleikamp memset(bh->b_data, 0, sb->s_blocksize); 151ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 152ac27a0ecSDave Kleikamp } 153ac27a0ecSDave Kleikamp 154ac27a0ecSDave Kleikamp return bh; 155ac27a0ecSDave Kleikamp } 156ac27a0ecSDave Kleikamp 157ac27a0ecSDave Kleikamp /* 15814904107SEric Sandeen * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA. 15914904107SEric Sandeen * If that fails, restart the transaction & regain write access for the 16014904107SEric Sandeen * buffer head which is used for block_bitmap modifications. 16114904107SEric Sandeen */ 1626d40bc5aSYongqiang Yang static int extend_or_restart_transaction(handle_t *handle, int thresh) 16314904107SEric Sandeen { 16414904107SEric Sandeen int err; 16514904107SEric Sandeen 1660390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, thresh)) 16714904107SEric Sandeen return 0; 16814904107SEric Sandeen 16914904107SEric Sandeen err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA); 17014904107SEric Sandeen if (err < 0) 17114904107SEric Sandeen return err; 17214904107SEric Sandeen if (err) { 1736d40bc5aSYongqiang Yang err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA); 1746d40bc5aSYongqiang Yang if (err) 17514904107SEric Sandeen return err; 17614904107SEric Sandeen } 17714904107SEric Sandeen 17814904107SEric Sandeen return 0; 17914904107SEric Sandeen } 18014904107SEric Sandeen 18114904107SEric Sandeen /* 182ac27a0ecSDave Kleikamp * Set up the block and inode bitmaps, and the inode table for the new group. 183ac27a0ecSDave Kleikamp * This doesn't need to be part of the main transaction, since we are only 184ac27a0ecSDave Kleikamp * changing blocks outside the actual filesystem. We still do journaling to 185ac27a0ecSDave Kleikamp * ensure the recovery is correct in case of a failure just after resize. 186ac27a0ecSDave Kleikamp * If any part of this fails, we simply abort the resize. 187ac27a0ecSDave Kleikamp */ 188ac27a0ecSDave Kleikamp static int setup_new_group_blocks(struct super_block *sb, 189617ba13bSMingming Cao struct ext4_new_group_data *input) 190ac27a0ecSDave Kleikamp { 191617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 192617ba13bSMingming Cao ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group); 193617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 194ac27a0ecSDave Kleikamp le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0; 195617ba13bSMingming Cao unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group); 196ac27a0ecSDave Kleikamp struct buffer_head *bh; 197ac27a0ecSDave Kleikamp handle_t *handle; 198617ba13bSMingming Cao ext4_fsblk_t block; 199617ba13bSMingming Cao ext4_grpblk_t bit; 200ac27a0ecSDave Kleikamp int i; 201ac27a0ecSDave Kleikamp int err = 0, err2; 202ac27a0ecSDave Kleikamp 20314904107SEric Sandeen /* This transaction may be extended/restarted along the way */ 20414904107SEric Sandeen handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 20514904107SEric Sandeen 206ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 207ac27a0ecSDave Kleikamp return PTR_ERR(handle); 208ac27a0ecSDave Kleikamp 2098f82f840SYongqiang Yang BUG_ON(input->group != sbi->s_groups_count); 210ac27a0ecSDave Kleikamp 211ac27a0ecSDave Kleikamp /* Copy all of the GDT blocks into the backup in this group */ 212ac27a0ecSDave Kleikamp for (i = 0, bit = 1, block = start + 1; 213ac27a0ecSDave Kleikamp i < gdblocks; i++, block++, bit++) { 214ac27a0ecSDave Kleikamp struct buffer_head *gdb; 215ac27a0ecSDave Kleikamp 216c549a95dSEric Sandeen ext4_debug("update backup group %#04llx (+%d)\n", block, bit); 2176d40bc5aSYongqiang Yang err = extend_or_restart_transaction(handle, 1); 2186d40bc5aSYongqiang Yang if (err) 2196d40bc5aSYongqiang Yang goto exit_journal; 22014904107SEric Sandeen 221ac27a0ecSDave Kleikamp gdb = sb_getblk(sb, block); 222ac27a0ecSDave Kleikamp if (!gdb) { 223ac27a0ecSDave Kleikamp err = -EIO; 2246d40bc5aSYongqiang Yang goto exit_journal; 225ac27a0ecSDave Kleikamp } 226617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, gdb))) { 227ac27a0ecSDave Kleikamp brelse(gdb); 2286d40bc5aSYongqiang Yang goto exit_journal; 229ac27a0ecSDave Kleikamp } 2305b615287SEric Sandeen memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size); 231ac27a0ecSDave Kleikamp set_buffer_uptodate(gdb); 232b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, gdb); 233b4097142STheodore Ts'o if (unlikely(err)) { 234b4097142STheodore Ts'o brelse(gdb); 2356d40bc5aSYongqiang Yang goto exit_journal; 236b4097142STheodore Ts'o } 237ac27a0ecSDave Kleikamp brelse(gdb); 238ac27a0ecSDave Kleikamp } 239ac27a0ecSDave Kleikamp 240ac27a0ecSDave Kleikamp /* Zero out all of the reserved backup group descriptor table blocks */ 241da488945STheodore Ts'o ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 242a31437b8SLukas Czerner block, sbi->s_itb_per_group); 243a31437b8SLukas Czerner err = sb_issue_zeroout(sb, gdblocks + start + 1, reserved_gdb, 244a107e5a3STheodore Ts'o GFP_NOFS); 245a31437b8SLukas Czerner if (err) 2466d40bc5aSYongqiang Yang goto exit_journal; 2476d40bc5aSYongqiang Yang 2486d40bc5aSYongqiang Yang err = extend_or_restart_transaction(handle, 2); 2496d40bc5aSYongqiang Yang if (err) 2506d40bc5aSYongqiang Yang goto exit_journal; 2516d40bc5aSYongqiang Yang 2526d40bc5aSYongqiang Yang bh = bclean(handle, sb, input->block_bitmap); 2536d40bc5aSYongqiang Yang if (IS_ERR(bh)) { 2546d40bc5aSYongqiang Yang err = PTR_ERR(bh); 2556d40bc5aSYongqiang Yang goto exit_journal; 2566d40bc5aSYongqiang Yang } 257c3e94d1dSYongqiang Yang 258c3e94d1dSYongqiang Yang if (ext4_bg_has_super(sb, input->group)) { 259c3e94d1dSYongqiang Yang ext4_debug("mark backup group tables %#04llx (+0)\n", start); 260c3e94d1dSYongqiang Yang ext4_set_bits(bh->b_data, 0, gdblocks + reserved_gdb + 1); 261c3e94d1dSYongqiang Yang } 26214904107SEric Sandeen 263c549a95dSEric Sandeen ext4_debug("mark block bitmap %#04llx (+%llu)\n", input->block_bitmap, 264ac27a0ecSDave Kleikamp input->block_bitmap - start); 265617ba13bSMingming Cao ext4_set_bit(input->block_bitmap - start, bh->b_data); 266c549a95dSEric Sandeen ext4_debug("mark inode bitmap %#04llx (+%llu)\n", input->inode_bitmap, 267ac27a0ecSDave Kleikamp input->inode_bitmap - start); 268617ba13bSMingming Cao ext4_set_bit(input->inode_bitmap - start, bh->b_data); 269ac27a0ecSDave Kleikamp 270ac27a0ecSDave Kleikamp /* Zero out all of the inode table blocks */ 271a31437b8SLukas Czerner block = input->inode_table; 272da488945STheodore Ts'o ext4_debug("clear inode table blocks %#04llx -> %#04lx\n", 273a31437b8SLukas Czerner block, sbi->s_itb_per_group); 274a107e5a3STheodore Ts'o err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group, GFP_NOFS); 275a31437b8SLukas Czerner if (err) 27614904107SEric Sandeen goto exit_bh; 277c3e94d1dSYongqiang Yang ext4_set_bits(bh->b_data, input->inode_table - start, 278c3e94d1dSYongqiang Yang sbi->s_itb_per_group); 27914904107SEric Sandeen 28014904107SEric Sandeen 28161d08673STheodore Ts'o ext4_mark_bitmap_end(input->blocks_count, sb->s_blocksize * 8, 28261d08673STheodore Ts'o bh->b_data); 283b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 284b4097142STheodore Ts'o if (unlikely(err)) { 285b4097142STheodore Ts'o ext4_std_error(sb, err); 286b4097142STheodore Ts'o goto exit_bh; 287b4097142STheodore Ts'o } 288ac27a0ecSDave Kleikamp brelse(bh); 289ac27a0ecSDave Kleikamp /* Mark unused entries in inode bitmap used */ 290c549a95dSEric Sandeen ext4_debug("clear inode bitmap %#04llx (+%llu)\n", 291ac27a0ecSDave Kleikamp input->inode_bitmap, input->inode_bitmap - start); 292ac27a0ecSDave Kleikamp if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) { 293ac27a0ecSDave Kleikamp err = PTR_ERR(bh); 294ac27a0ecSDave Kleikamp goto exit_journal; 295ac27a0ecSDave Kleikamp } 296ac27a0ecSDave Kleikamp 29761d08673STheodore Ts'o ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, 298ac27a0ecSDave Kleikamp bh->b_data); 299b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 300b4097142STheodore Ts'o if (unlikely(err)) 301b4097142STheodore Ts'o ext4_std_error(sb, err); 302ac27a0ecSDave Kleikamp exit_bh: 303ac27a0ecSDave Kleikamp brelse(bh); 304ac27a0ecSDave Kleikamp 305ac27a0ecSDave Kleikamp exit_journal: 306617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 307ac27a0ecSDave Kleikamp err = err2; 308ac27a0ecSDave Kleikamp 309ac27a0ecSDave Kleikamp return err; 310ac27a0ecSDave Kleikamp } 311ac27a0ecSDave Kleikamp 312ac27a0ecSDave Kleikamp /* 313ac27a0ecSDave Kleikamp * Iterate through the groups which hold BACKUP superblock/GDT copies in an 314617ba13bSMingming Cao * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 315ac27a0ecSDave Kleikamp * calling this for the first time. In a sparse filesystem it will be the 316ac27a0ecSDave Kleikamp * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 317ac27a0ecSDave Kleikamp * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 318ac27a0ecSDave Kleikamp */ 319617ba13bSMingming Cao static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, 320ac27a0ecSDave Kleikamp unsigned *five, unsigned *seven) 321ac27a0ecSDave Kleikamp { 322ac27a0ecSDave Kleikamp unsigned *min = three; 323ac27a0ecSDave Kleikamp int mult = 3; 324ac27a0ecSDave Kleikamp unsigned ret; 325ac27a0ecSDave Kleikamp 326617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 327617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 328ac27a0ecSDave Kleikamp ret = *min; 329ac27a0ecSDave Kleikamp *min += 1; 330ac27a0ecSDave Kleikamp return ret; 331ac27a0ecSDave Kleikamp } 332ac27a0ecSDave Kleikamp 333ac27a0ecSDave Kleikamp if (*five < *min) { 334ac27a0ecSDave Kleikamp min = five; 335ac27a0ecSDave Kleikamp mult = 5; 336ac27a0ecSDave Kleikamp } 337ac27a0ecSDave Kleikamp if (*seven < *min) { 338ac27a0ecSDave Kleikamp min = seven; 339ac27a0ecSDave Kleikamp mult = 7; 340ac27a0ecSDave Kleikamp } 341ac27a0ecSDave Kleikamp 342ac27a0ecSDave Kleikamp ret = *min; 343ac27a0ecSDave Kleikamp *min *= mult; 344ac27a0ecSDave Kleikamp 345ac27a0ecSDave Kleikamp return ret; 346ac27a0ecSDave Kleikamp } 347ac27a0ecSDave Kleikamp 348ac27a0ecSDave Kleikamp /* 349ac27a0ecSDave Kleikamp * Check that all of the backup GDT blocks are held in the primary GDT block. 350ac27a0ecSDave Kleikamp * It is assumed that they are stored in group order. Returns the number of 351ac27a0ecSDave Kleikamp * groups in current filesystem that have BACKUPS, or -ve error code. 352ac27a0ecSDave Kleikamp */ 353ac27a0ecSDave Kleikamp static int verify_reserved_gdb(struct super_block *sb, 354ac27a0ecSDave Kleikamp struct buffer_head *primary) 355ac27a0ecSDave Kleikamp { 356617ba13bSMingming Cao const ext4_fsblk_t blk = primary->b_blocknr; 357fd2d4291SAvantika Mathur const ext4_group_t end = EXT4_SB(sb)->s_groups_count; 358ac27a0ecSDave Kleikamp unsigned three = 1; 359ac27a0ecSDave Kleikamp unsigned five = 5; 360ac27a0ecSDave Kleikamp unsigned seven = 7; 361ac27a0ecSDave Kleikamp unsigned grp; 362ac27a0ecSDave Kleikamp __le32 *p = (__le32 *)primary->b_data; 363ac27a0ecSDave Kleikamp int gdbackups = 0; 364ac27a0ecSDave Kleikamp 365617ba13bSMingming Cao while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { 366bd81d8eeSLaurent Vivier if (le32_to_cpu(*p++) != 367bd81d8eeSLaurent Vivier grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ 36812062dddSEric Sandeen ext4_warning(sb, "reserved GDT %llu" 3692ae02107SMingming Cao " missing grp %d (%llu)", 370ac27a0ecSDave Kleikamp blk, grp, 371bd81d8eeSLaurent Vivier grp * 372bd81d8eeSLaurent Vivier (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + 373bd81d8eeSLaurent Vivier blk); 374ac27a0ecSDave Kleikamp return -EINVAL; 375ac27a0ecSDave Kleikamp } 376617ba13bSMingming Cao if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) 377ac27a0ecSDave Kleikamp return -EFBIG; 378ac27a0ecSDave Kleikamp } 379ac27a0ecSDave Kleikamp 380ac27a0ecSDave Kleikamp return gdbackups; 381ac27a0ecSDave Kleikamp } 382ac27a0ecSDave Kleikamp 383ac27a0ecSDave Kleikamp /* 384ac27a0ecSDave Kleikamp * Called when we need to bring a reserved group descriptor table block into 385ac27a0ecSDave Kleikamp * use from the resize inode. The primary copy of the new GDT block currently 386ac27a0ecSDave Kleikamp * is an indirect block (under the double indirect block in the resize inode). 387ac27a0ecSDave Kleikamp * The new backup GDT blocks will be stored as leaf blocks in this indirect 388ac27a0ecSDave Kleikamp * block, in group order. Even though we know all the block numbers we need, 389ac27a0ecSDave Kleikamp * we check to ensure that the resize inode has actually reserved these blocks. 390ac27a0ecSDave Kleikamp * 391ac27a0ecSDave Kleikamp * Don't need to update the block bitmaps because the blocks are still in use. 392ac27a0ecSDave Kleikamp * 393ac27a0ecSDave Kleikamp * We get all of the error cases out of the way, so that we are sure to not 394ac27a0ecSDave Kleikamp * fail once we start modifying the data on disk, because JBD has no rollback. 395ac27a0ecSDave Kleikamp */ 396ac27a0ecSDave Kleikamp static int add_new_gdb(handle_t *handle, struct inode *inode, 3972f919710SYongqiang Yang ext4_group_t group) 398ac27a0ecSDave Kleikamp { 399ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 400617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 4012f919710SYongqiang Yang unsigned long gdb_num = group / EXT4_DESC_PER_BLOCK(sb); 402617ba13bSMingming Cao ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; 403ac27a0ecSDave Kleikamp struct buffer_head **o_group_desc, **n_group_desc; 404ac27a0ecSDave Kleikamp struct buffer_head *dind; 4052f919710SYongqiang Yang struct buffer_head *gdb_bh; 406ac27a0ecSDave Kleikamp int gdbackups; 407617ba13bSMingming Cao struct ext4_iloc iloc; 408ac27a0ecSDave Kleikamp __le32 *data; 409ac27a0ecSDave Kleikamp int err; 410ac27a0ecSDave Kleikamp 411ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 412ac27a0ecSDave Kleikamp printk(KERN_DEBUG 413617ba13bSMingming Cao "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", 414ac27a0ecSDave Kleikamp gdb_num); 415ac27a0ecSDave Kleikamp 416ac27a0ecSDave Kleikamp /* 417ac27a0ecSDave Kleikamp * If we are not using the primary superblock/GDT copy don't resize, 418ac27a0ecSDave Kleikamp * because the user tools have no way of handling this. Probably a 419ac27a0ecSDave Kleikamp * bad time to do it anyways. 420ac27a0ecSDave Kleikamp */ 421617ba13bSMingming Cao if (EXT4_SB(sb)->s_sbh->b_blocknr != 422617ba13bSMingming Cao le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 42312062dddSEric Sandeen ext4_warning(sb, "won't resize using backup superblock at %llu", 424617ba13bSMingming Cao (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); 425ac27a0ecSDave Kleikamp return -EPERM; 426ac27a0ecSDave Kleikamp } 427ac27a0ecSDave Kleikamp 4282f919710SYongqiang Yang gdb_bh = sb_bread(sb, gdblock); 4292f919710SYongqiang Yang if (!gdb_bh) 430ac27a0ecSDave Kleikamp return -EIO; 431ac27a0ecSDave Kleikamp 4322f919710SYongqiang Yang gdbackups = verify_reserved_gdb(sb, gdb_bh); 4332f919710SYongqiang Yang if (gdbackups < 0) { 434ac27a0ecSDave Kleikamp err = gdbackups; 435ac27a0ecSDave Kleikamp goto exit_bh; 436ac27a0ecSDave Kleikamp } 437ac27a0ecSDave Kleikamp 438617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 439ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 440ac27a0ecSDave Kleikamp if (!dind) { 441ac27a0ecSDave Kleikamp err = -EIO; 442ac27a0ecSDave Kleikamp goto exit_bh; 443ac27a0ecSDave Kleikamp } 444ac27a0ecSDave Kleikamp 445ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 446617ba13bSMingming Cao if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { 44712062dddSEric Sandeen ext4_warning(sb, "new group %u GDT block %llu not reserved", 4482f919710SYongqiang Yang group, gdblock); 449ac27a0ecSDave Kleikamp err = -EINVAL; 450ac27a0ecSDave Kleikamp goto exit_dind; 451ac27a0ecSDave Kleikamp } 452ac27a0ecSDave Kleikamp 453b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 454b4097142STheodore Ts'o if (unlikely(err)) 455ac27a0ecSDave Kleikamp goto exit_dind; 456ac27a0ecSDave Kleikamp 4572f919710SYongqiang Yang err = ext4_journal_get_write_access(handle, gdb_bh); 458b4097142STheodore Ts'o if (unlikely(err)) 459ac27a0ecSDave Kleikamp goto exit_sbh; 460ac27a0ecSDave Kleikamp 461b4097142STheodore Ts'o err = ext4_journal_get_write_access(handle, dind); 462b4097142STheodore Ts'o if (unlikely(err)) 463b4097142STheodore Ts'o ext4_std_error(sb, err); 464ac27a0ecSDave Kleikamp 465617ba13bSMingming Cao /* ext4_reserve_inode_write() gets a reference on the iloc */ 466b4097142STheodore Ts'o err = ext4_reserve_inode_write(handle, inode, &iloc); 467b4097142STheodore Ts'o if (unlikely(err)) 468ac27a0ecSDave Kleikamp goto exit_dindj; 469ac27a0ecSDave Kleikamp 470f18a5f21STheodore Ts'o n_group_desc = ext4_kvmalloc((gdb_num + 1) * 471f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 472216553c4SJosef Bacik GFP_NOFS); 473ac27a0ecSDave Kleikamp if (!n_group_desc) { 474ac27a0ecSDave Kleikamp err = -ENOMEM; 475f18a5f21STheodore Ts'o ext4_warning(sb, "not enough memory for %lu groups", 476f18a5f21STheodore Ts'o 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; 490b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, dind); 491b4097142STheodore Ts'o if (unlikely(err)) { 492b4097142STheodore Ts'o ext4_std_error(sb, err); 493b4097142STheodore Ts'o goto exit_inode; 494b4097142STheodore Ts'o } 495ac27a0ecSDave Kleikamp inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; 496617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 4972f919710SYongqiang Yang memset(gdb_bh->b_data, 0, sb->s_blocksize); 4982f919710SYongqiang Yang err = ext4_handle_dirty_metadata(handle, NULL, gdb_bh); 499b4097142STheodore Ts'o if (unlikely(err)) { 500b4097142STheodore Ts'o ext4_std_error(sb, err); 501b4097142STheodore Ts'o goto exit_inode; 502b4097142STheodore Ts'o } 503b4097142STheodore Ts'o brelse(dind); 504ac27a0ecSDave Kleikamp 505617ba13bSMingming Cao o_group_desc = EXT4_SB(sb)->s_group_desc; 506ac27a0ecSDave Kleikamp memcpy(n_group_desc, o_group_desc, 507617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); 5082f919710SYongqiang Yang n_group_desc[gdb_num] = gdb_bh; 509617ba13bSMingming Cao EXT4_SB(sb)->s_group_desc = n_group_desc; 510617ba13bSMingming Cao EXT4_SB(sb)->s_gdb_count++; 511f18a5f21STheodore Ts'o ext4_kvfree(o_group_desc); 512ac27a0ecSDave Kleikamp 513e8546d06SMarcin Slusarz le16_add_cpu(&es->s_reserved_gdt_blocks, -1); 514b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); 515b4097142STheodore Ts'o if (err) 516b4097142STheodore Ts'o ext4_std_error(sb, err); 517ac27a0ecSDave Kleikamp 518b4097142STheodore Ts'o return err; 519ac27a0ecSDave Kleikamp 520ac27a0ecSDave Kleikamp exit_inode: 521f18a5f21STheodore Ts'o ext4_kvfree(n_group_desc); 522537a0310SAmir Goldstein /* ext4_handle_release_buffer(handle, iloc.bh); */ 523ac27a0ecSDave Kleikamp brelse(iloc.bh); 524ac27a0ecSDave Kleikamp exit_dindj: 525537a0310SAmir Goldstein /* ext4_handle_release_buffer(handle, dind); */ 526ac27a0ecSDave Kleikamp exit_sbh: 527537a0310SAmir Goldstein /* ext4_handle_release_buffer(handle, EXT4_SB(sb)->s_sbh); */ 528ac27a0ecSDave Kleikamp exit_dind: 529ac27a0ecSDave Kleikamp brelse(dind); 530ac27a0ecSDave Kleikamp exit_bh: 5312f919710SYongqiang Yang brelse(gdb_bh); 532ac27a0ecSDave Kleikamp 533617ba13bSMingming Cao ext4_debug("leaving with error %d\n", err); 534ac27a0ecSDave Kleikamp return err; 535ac27a0ecSDave Kleikamp } 536ac27a0ecSDave Kleikamp 537ac27a0ecSDave Kleikamp /* 538ac27a0ecSDave Kleikamp * Called when we are adding a new group which has a backup copy of each of 539ac27a0ecSDave Kleikamp * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. 540ac27a0ecSDave Kleikamp * We need to add these reserved backup GDT blocks to the resize inode, so 541ac27a0ecSDave Kleikamp * that they are kept for future resizing and not allocated to files. 542ac27a0ecSDave Kleikamp * 543ac27a0ecSDave Kleikamp * Each reserved backup GDT block will go into a different indirect block. 544ac27a0ecSDave Kleikamp * The indirect blocks are actually the primary reserved GDT blocks, 545ac27a0ecSDave Kleikamp * so we know in advance what their block numbers are. We only get the 546ac27a0ecSDave Kleikamp * double-indirect block to verify it is pointing to the primary reserved 547ac27a0ecSDave Kleikamp * GDT blocks so we don't overwrite a data block by accident. The reserved 548ac27a0ecSDave Kleikamp * backup GDT blocks are stored in their reserved primary GDT block. 549ac27a0ecSDave Kleikamp */ 550ac27a0ecSDave Kleikamp static int reserve_backup_gdb(handle_t *handle, struct inode *inode, 551668f4dc5SYongqiang Yang ext4_group_t group) 552ac27a0ecSDave Kleikamp { 553ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 554617ba13bSMingming Cao int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); 555ac27a0ecSDave Kleikamp struct buffer_head **primary; 556ac27a0ecSDave Kleikamp struct buffer_head *dind; 557617ba13bSMingming Cao struct ext4_iloc iloc; 558617ba13bSMingming Cao ext4_fsblk_t blk; 559ac27a0ecSDave Kleikamp __le32 *data, *end; 560ac27a0ecSDave Kleikamp int gdbackups = 0; 561ac27a0ecSDave Kleikamp int res, i; 562ac27a0ecSDave Kleikamp int err; 563ac27a0ecSDave Kleikamp 564216553c4SJosef Bacik primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS); 565ac27a0ecSDave Kleikamp if (!primary) 566ac27a0ecSDave Kleikamp return -ENOMEM; 567ac27a0ecSDave Kleikamp 568617ba13bSMingming Cao data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; 569ac27a0ecSDave Kleikamp dind = sb_bread(sb, le32_to_cpu(*data)); 570ac27a0ecSDave Kleikamp if (!dind) { 571ac27a0ecSDave Kleikamp err = -EIO; 572ac27a0ecSDave Kleikamp goto exit_free; 573ac27a0ecSDave Kleikamp } 574ac27a0ecSDave Kleikamp 575617ba13bSMingming Cao blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; 57694460093SJosef Bacik data = (__le32 *)dind->b_data + (EXT4_SB(sb)->s_gdb_count % 57794460093SJosef Bacik EXT4_ADDR_PER_BLOCK(sb)); 578617ba13bSMingming Cao end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); 579ac27a0ecSDave Kleikamp 580ac27a0ecSDave Kleikamp /* Get each reserved primary GDT block and verify it holds backups */ 581ac27a0ecSDave Kleikamp for (res = 0; res < reserved_gdb; res++, blk++) { 582ac27a0ecSDave Kleikamp if (le32_to_cpu(*data) != blk) { 58312062dddSEric Sandeen ext4_warning(sb, "reserved block %llu" 584ac27a0ecSDave Kleikamp " not at offset %ld", 585ac27a0ecSDave Kleikamp blk, 586ac27a0ecSDave Kleikamp (long)(data - (__le32 *)dind->b_data)); 587ac27a0ecSDave Kleikamp err = -EINVAL; 588ac27a0ecSDave Kleikamp goto exit_bh; 589ac27a0ecSDave Kleikamp } 590ac27a0ecSDave Kleikamp primary[res] = sb_bread(sb, blk); 591ac27a0ecSDave Kleikamp if (!primary[res]) { 592ac27a0ecSDave Kleikamp err = -EIO; 593ac27a0ecSDave Kleikamp goto exit_bh; 594ac27a0ecSDave Kleikamp } 595ac27a0ecSDave Kleikamp if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) { 596ac27a0ecSDave Kleikamp brelse(primary[res]); 597ac27a0ecSDave Kleikamp err = gdbackups; 598ac27a0ecSDave Kleikamp goto exit_bh; 599ac27a0ecSDave Kleikamp } 600ac27a0ecSDave Kleikamp if (++data >= end) 601ac27a0ecSDave Kleikamp data = (__le32 *)dind->b_data; 602ac27a0ecSDave Kleikamp } 603ac27a0ecSDave Kleikamp 604ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 605617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary[i]))) { 606ac27a0ecSDave Kleikamp /* 607ac27a0ecSDave Kleikamp int j; 608ac27a0ecSDave Kleikamp for (j = 0; j < i; j++) 609537a0310SAmir Goldstein ext4_handle_release_buffer(handle, primary[j]); 610ac27a0ecSDave Kleikamp */ 611ac27a0ecSDave Kleikamp goto exit_bh; 612ac27a0ecSDave Kleikamp } 613ac27a0ecSDave Kleikamp } 614ac27a0ecSDave Kleikamp 615617ba13bSMingming Cao if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) 616ac27a0ecSDave Kleikamp goto exit_bh; 617ac27a0ecSDave Kleikamp 618ac27a0ecSDave Kleikamp /* 619ac27a0ecSDave Kleikamp * Finally we can add each of the reserved backup GDT blocks from 620ac27a0ecSDave Kleikamp * the new group to its reserved primary GDT block. 621ac27a0ecSDave Kleikamp */ 622668f4dc5SYongqiang Yang blk = group * EXT4_BLOCKS_PER_GROUP(sb); 623ac27a0ecSDave Kleikamp for (i = 0; i < reserved_gdb; i++) { 624ac27a0ecSDave Kleikamp int err2; 625ac27a0ecSDave Kleikamp data = (__le32 *)primary[i]->b_data; 626ac27a0ecSDave Kleikamp /* printk("reserving backup %lu[%u] = %lu\n", 627ac27a0ecSDave Kleikamp primary[i]->b_blocknr, gdbackups, 628ac27a0ecSDave Kleikamp blk + primary[i]->b_blocknr); */ 629ac27a0ecSDave Kleikamp data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); 6300390131bSFrank Mayhar err2 = ext4_handle_dirty_metadata(handle, NULL, primary[i]); 631ac27a0ecSDave Kleikamp if (!err) 632ac27a0ecSDave Kleikamp err = err2; 633ac27a0ecSDave Kleikamp } 634ac27a0ecSDave Kleikamp inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; 635617ba13bSMingming Cao ext4_mark_iloc_dirty(handle, inode, &iloc); 636ac27a0ecSDave Kleikamp 637ac27a0ecSDave Kleikamp exit_bh: 638ac27a0ecSDave Kleikamp while (--res >= 0) 639ac27a0ecSDave Kleikamp brelse(primary[res]); 640ac27a0ecSDave Kleikamp brelse(dind); 641ac27a0ecSDave Kleikamp 642ac27a0ecSDave Kleikamp exit_free: 643ac27a0ecSDave Kleikamp kfree(primary); 644ac27a0ecSDave Kleikamp 645ac27a0ecSDave Kleikamp return err; 646ac27a0ecSDave Kleikamp } 647ac27a0ecSDave Kleikamp 648ac27a0ecSDave Kleikamp /* 649617ba13bSMingming Cao * Update the backup copies of the ext4 metadata. These don't need to be part 650ac27a0ecSDave Kleikamp * of the main resize transaction, because e2fsck will re-write them if there 651ac27a0ecSDave Kleikamp * is a problem (basically only OOM will cause a problem). However, we 652ac27a0ecSDave Kleikamp * _should_ update the backups if possible, in case the primary gets trashed 653ac27a0ecSDave Kleikamp * for some reason and we need to run e2fsck from a backup superblock. The 654ac27a0ecSDave Kleikamp * important part is that the new block and inode counts are in the backup 655ac27a0ecSDave Kleikamp * superblocks, and the location of the new group metadata in the GDT backups. 656ac27a0ecSDave Kleikamp * 65732ed5058STheodore Ts'o * We do not need take the s_resize_lock for this, because these 65832ed5058STheodore Ts'o * blocks are not otherwise touched by the filesystem code when it is 65932ed5058STheodore Ts'o * mounted. We don't need to worry about last changing from 66032ed5058STheodore Ts'o * sbi->s_groups_count, because the worst that can happen is that we 66132ed5058STheodore Ts'o * do not copy the full number of backups at this time. The resize 66232ed5058STheodore Ts'o * which changed s_groups_count will backup again. 663ac27a0ecSDave Kleikamp */ 664ac27a0ecSDave Kleikamp static void update_backups(struct super_block *sb, 665ac27a0ecSDave Kleikamp int blk_off, char *data, int size) 666ac27a0ecSDave Kleikamp { 667617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 668fd2d4291SAvantika Mathur const ext4_group_t last = sbi->s_groups_count; 669617ba13bSMingming Cao const int bpg = EXT4_BLOCKS_PER_GROUP(sb); 670ac27a0ecSDave Kleikamp unsigned three = 1; 671ac27a0ecSDave Kleikamp unsigned five = 5; 672ac27a0ecSDave Kleikamp unsigned seven = 7; 673fd2d4291SAvantika Mathur ext4_group_t group; 674ac27a0ecSDave Kleikamp int rest = sb->s_blocksize - size; 675ac27a0ecSDave Kleikamp handle_t *handle; 676ac27a0ecSDave Kleikamp int err = 0, err2; 677ac27a0ecSDave Kleikamp 678617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); 679ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 680ac27a0ecSDave Kleikamp group = 1; 681ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 682ac27a0ecSDave Kleikamp goto exit_err; 683ac27a0ecSDave Kleikamp } 684ac27a0ecSDave Kleikamp 685617ba13bSMingming Cao while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) { 686ac27a0ecSDave Kleikamp struct buffer_head *bh; 687ac27a0ecSDave Kleikamp 688ac27a0ecSDave Kleikamp /* Out of journal space, and can't get more - abort - so sad */ 6890390131bSFrank Mayhar if (ext4_handle_valid(handle) && 6900390131bSFrank Mayhar handle->h_buffer_credits == 0 && 691617ba13bSMingming Cao ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && 692617ba13bSMingming Cao (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) 693ac27a0ecSDave Kleikamp break; 694ac27a0ecSDave Kleikamp 695ac27a0ecSDave Kleikamp bh = sb_getblk(sb, group * bpg + blk_off); 696ac27a0ecSDave Kleikamp if (!bh) { 697ac27a0ecSDave Kleikamp err = -EIO; 698ac27a0ecSDave Kleikamp break; 699ac27a0ecSDave Kleikamp } 700617ba13bSMingming Cao ext4_debug("update metadata backup %#04lx\n", 701ac27a0ecSDave Kleikamp (unsigned long)bh->b_blocknr); 702617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, bh))) 703ac27a0ecSDave Kleikamp break; 704ac27a0ecSDave Kleikamp lock_buffer(bh); 705ac27a0ecSDave Kleikamp memcpy(bh->b_data, data, size); 706ac27a0ecSDave Kleikamp if (rest) 707ac27a0ecSDave Kleikamp memset(bh->b_data + size, 0, rest); 708ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 709ac27a0ecSDave Kleikamp unlock_buffer(bh); 710b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, bh); 711b4097142STheodore Ts'o if (unlikely(err)) 712b4097142STheodore Ts'o ext4_std_error(sb, err); 713ac27a0ecSDave Kleikamp brelse(bh); 714ac27a0ecSDave Kleikamp } 715617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 716ac27a0ecSDave Kleikamp err = err2; 717ac27a0ecSDave Kleikamp 718ac27a0ecSDave Kleikamp /* 719ac27a0ecSDave Kleikamp * Ugh! Need to have e2fsck write the backup copies. It is too 720ac27a0ecSDave Kleikamp * late to revert the resize, we shouldn't fail just because of 721ac27a0ecSDave Kleikamp * the backup copies (they are only needed in case of corruption). 722ac27a0ecSDave Kleikamp * 723ac27a0ecSDave Kleikamp * However, if we got here we have a journal problem too, so we 724ac27a0ecSDave Kleikamp * can't really start a transaction to mark the superblock. 725ac27a0ecSDave Kleikamp * Chicken out and just set the flag on the hope it will be written 726ac27a0ecSDave Kleikamp * to disk, and if not - we will simply wait until next fsck. 727ac27a0ecSDave Kleikamp */ 728ac27a0ecSDave Kleikamp exit_err: 729ac27a0ecSDave Kleikamp if (err) { 73012062dddSEric Sandeen ext4_warning(sb, "can't update backup for group %u (err %d), " 731ac27a0ecSDave Kleikamp "forcing fsck on next reboot", group, err); 732617ba13bSMingming Cao sbi->s_mount_state &= ~EXT4_VALID_FS; 733617ba13bSMingming Cao sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 734ac27a0ecSDave Kleikamp mark_buffer_dirty(sbi->s_sbh); 735ac27a0ecSDave Kleikamp } 736ac27a0ecSDave Kleikamp } 737ac27a0ecSDave Kleikamp 738ac27a0ecSDave Kleikamp /* Add group descriptor data to an existing or new group descriptor block. 739ac27a0ecSDave Kleikamp * Ensure we handle all possible error conditions _before_ we start modifying 740ac27a0ecSDave Kleikamp * the filesystem, because we cannot abort the transaction and not have it 741ac27a0ecSDave Kleikamp * write the data to disk. 742ac27a0ecSDave Kleikamp * 743ac27a0ecSDave Kleikamp * If we are on a GDT block boundary, we need to get the reserved GDT block. 744ac27a0ecSDave Kleikamp * Otherwise, we may need to add backup GDT blocks for a sparse group. 745ac27a0ecSDave Kleikamp * 746ac27a0ecSDave Kleikamp * We only need to hold the superblock lock while we are actually adding 747ac27a0ecSDave Kleikamp * in the new group's counts to the superblock. Prior to that we have 748ac27a0ecSDave Kleikamp * not really "added" the group at all. We re-check that we are still 749ac27a0ecSDave Kleikamp * adding in the last group in case things have changed since verifying. 750ac27a0ecSDave Kleikamp */ 751617ba13bSMingming Cao int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) 752ac27a0ecSDave Kleikamp { 753617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 754617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 755617ba13bSMingming Cao int reserved_gdb = ext4_bg_has_super(sb, input->group) ? 756ac27a0ecSDave Kleikamp le16_to_cpu(es->s_reserved_gdt_blocks) : 0; 757ac27a0ecSDave Kleikamp struct buffer_head *primary = NULL; 758617ba13bSMingming Cao struct ext4_group_desc *gdp; 759ac27a0ecSDave Kleikamp struct inode *inode = NULL; 760ac27a0ecSDave Kleikamp handle_t *handle; 761ac27a0ecSDave Kleikamp int gdb_off, gdb_num; 762ac27a0ecSDave Kleikamp int err, err2; 763ac27a0ecSDave Kleikamp 764617ba13bSMingming Cao gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); 765617ba13bSMingming Cao gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); 766ac27a0ecSDave Kleikamp 767617ba13bSMingming Cao if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb, 768617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { 76912062dddSEric Sandeen ext4_warning(sb, "Can't resize non-sparse filesystem further"); 770ac27a0ecSDave Kleikamp return -EPERM; 771ac27a0ecSDave Kleikamp } 772ac27a0ecSDave Kleikamp 773bd81d8eeSLaurent Vivier if (ext4_blocks_count(es) + input->blocks_count < 774bd81d8eeSLaurent Vivier ext4_blocks_count(es)) { 77512062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 776ac27a0ecSDave Kleikamp return -EINVAL; 777ac27a0ecSDave Kleikamp } 778ac27a0ecSDave Kleikamp 779617ba13bSMingming Cao if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < 780ac27a0ecSDave Kleikamp le32_to_cpu(es->s_inodes_count)) { 78112062dddSEric Sandeen ext4_warning(sb, "inodes_count overflow"); 782ac27a0ecSDave Kleikamp return -EINVAL; 783ac27a0ecSDave Kleikamp } 784ac27a0ecSDave Kleikamp 785ac27a0ecSDave Kleikamp if (reserved_gdb || gdb_off == 0) { 786617ba13bSMingming Cao if (!EXT4_HAS_COMPAT_FEATURE(sb, 78737609fd5SJosef Bacik EXT4_FEATURE_COMPAT_RESIZE_INODE) 78837609fd5SJosef Bacik || !le16_to_cpu(es->s_reserved_gdt_blocks)) { 78912062dddSEric Sandeen ext4_warning(sb, 790ac27a0ecSDave Kleikamp "No reserved GDT blocks, can't resize"); 791ac27a0ecSDave Kleikamp return -EPERM; 792ac27a0ecSDave Kleikamp } 7931d1fe1eeSDavid Howells inode = ext4_iget(sb, EXT4_RESIZE_INO); 7941d1fe1eeSDavid Howells if (IS_ERR(inode)) { 79512062dddSEric Sandeen ext4_warning(sb, "Error opening resize inode"); 7961d1fe1eeSDavid Howells return PTR_ERR(inode); 797ac27a0ecSDave Kleikamp } 798ac27a0ecSDave Kleikamp } 799ac27a0ecSDave Kleikamp 800920313a7SAneesh Kumar K.V 801ac27a0ecSDave Kleikamp if ((err = verify_group_input(sb, input))) 802ac27a0ecSDave Kleikamp goto exit_put; 803ac27a0ecSDave Kleikamp 804ac27a0ecSDave Kleikamp if ((err = setup_new_group_blocks(sb, input))) 805ac27a0ecSDave Kleikamp goto exit_put; 806ac27a0ecSDave Kleikamp 807ac27a0ecSDave Kleikamp /* 808ac27a0ecSDave Kleikamp * We will always be modifying at least the superblock and a GDT 809ac27a0ecSDave Kleikamp * block. If we are adding a group past the last current GDT block, 810ac27a0ecSDave Kleikamp * we will also modify the inode and the dindirect block. If we 811ac27a0ecSDave Kleikamp * are adding a group with superblock/GDT backups we will also 812ac27a0ecSDave Kleikamp * modify each of the reserved GDT dindirect blocks. 813ac27a0ecSDave Kleikamp */ 814617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 815617ba13bSMingming Cao ext4_bg_has_super(sb, input->group) ? 816ac27a0ecSDave Kleikamp 3 + reserved_gdb : 4); 817ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 818ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 819ac27a0ecSDave Kleikamp goto exit_put; 820ac27a0ecSDave Kleikamp } 821ac27a0ecSDave Kleikamp 822617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh))) 823ac27a0ecSDave Kleikamp goto exit_journal; 824ac27a0ecSDave Kleikamp 825ac27a0ecSDave Kleikamp /* 826ac27a0ecSDave Kleikamp * We will only either add reserved group blocks to a backup group 827ac27a0ecSDave Kleikamp * or remove reserved blocks for the first group in a new group block. 828ac27a0ecSDave Kleikamp * Doing both would be mean more complex code, and sane people don't 829ac27a0ecSDave Kleikamp * use non-sparse filesystems anymore. This is already checked above. 830ac27a0ecSDave Kleikamp */ 831ac27a0ecSDave Kleikamp if (gdb_off) { 832ac27a0ecSDave Kleikamp primary = sbi->s_group_desc[gdb_num]; 833617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, primary))) 834ac27a0ecSDave Kleikamp goto exit_journal; 835ac27a0ecSDave Kleikamp 836668f4dc5SYongqiang Yang if (reserved_gdb && ext4_bg_num_gdb(sb, input->group)) { 837668f4dc5SYongqiang Yang err = reserve_backup_gdb(handle, inode, input->group); 838668f4dc5SYongqiang Yang if (err) 839ac27a0ecSDave Kleikamp goto exit_journal; 840668f4dc5SYongqiang Yang } 8412f919710SYongqiang Yang } else { 8422f919710SYongqiang Yang /* 8432f919710SYongqiang Yang * Note that we can access new group descriptor block safely 8442f919710SYongqiang Yang * only if add_new_gdb() succeeds. 8452f919710SYongqiang Yang */ 8462f919710SYongqiang Yang err = add_new_gdb(handle, inode, input->group); 8472f919710SYongqiang Yang if (err) 848ac27a0ecSDave Kleikamp goto exit_journal; 8492f919710SYongqiang Yang primary = sbi->s_group_desc[gdb_num]; 8502f919710SYongqiang Yang } 851ac27a0ecSDave Kleikamp 852ac27a0ecSDave Kleikamp /* 853ac27a0ecSDave Kleikamp * OK, now we've set up the new group. Time to make it active. 854ac27a0ecSDave Kleikamp * 855ac27a0ecSDave Kleikamp * so we have to be safe wrt. concurrent accesses the group 856ac27a0ecSDave Kleikamp * data. So we need to be careful to set all of the relevant 857ac27a0ecSDave Kleikamp * group descriptor data etc. *before* we enable the group. 858ac27a0ecSDave Kleikamp * 859ac27a0ecSDave Kleikamp * The key field here is sbi->s_groups_count: as long as 860ac27a0ecSDave Kleikamp * that retains its old value, nobody is going to access the new 861ac27a0ecSDave Kleikamp * group. 862ac27a0ecSDave Kleikamp * 863ac27a0ecSDave Kleikamp * So first we update all the descriptor metadata for the new 864ac27a0ecSDave Kleikamp * group; then we update the total disk blocks count; then we 865ac27a0ecSDave Kleikamp * update the groups count to enable the group; then finally we 866ac27a0ecSDave Kleikamp * update the free space counts so that the system can start 867ac27a0ecSDave Kleikamp * using the new disk blocks. 868ac27a0ecSDave Kleikamp */ 869ac27a0ecSDave Kleikamp 870ac27a0ecSDave Kleikamp /* Update group descriptor block for new group */ 8712856922cSFrederic Bohe gdp = (struct ext4_group_desc *)((char *)primary->b_data + 8722856922cSFrederic Bohe gdb_off * EXT4_DESC_SIZE(sb)); 873ac27a0ecSDave Kleikamp 874fdff73f0STheodore Ts'o memset(gdp, 0, EXT4_DESC_SIZE(sb)); 8758fadc143SAlexandre Ratchov ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */ 8768fadc143SAlexandre Ratchov ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */ 8778fadc143SAlexandre Ratchov ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */ 878*021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, input->free_blocks_count); 879560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, EXT4_INODES_PER_GROUP(sb)); 880fdff73f0STheodore Ts'o gdp->bg_flags = cpu_to_le16(EXT4_BG_INODE_ZEROED); 881717d50e4SAndreas Dilger gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp); 882ac27a0ecSDave Kleikamp 883ac27a0ecSDave Kleikamp /* 8845f21b0e6SFrederic Bohe * We can allocate memory for mb_alloc based on the new group 8855f21b0e6SFrederic Bohe * descriptor 8865f21b0e6SFrederic Bohe */ 887920313a7SAneesh Kumar K.V err = ext4_mb_add_groupinfo(sb, input->group, gdp); 88808c3a813SAneesh Kumar K.V if (err) 8895f21b0e6SFrederic Bohe goto exit_journal; 890c2ea3fdeSTheodore Ts'o 8915f21b0e6SFrederic Bohe /* 892ac27a0ecSDave Kleikamp * Make the new blocks and inodes valid next. We do this before 893ac27a0ecSDave Kleikamp * increasing the group count so that once the group is enabled, 894ac27a0ecSDave Kleikamp * all of its blocks and inodes are already valid. 895ac27a0ecSDave Kleikamp * 896ac27a0ecSDave Kleikamp * We always allocate group-by-group, then block-by-block or 897ac27a0ecSDave Kleikamp * inode-by-inode within a group, so enabling these 898ac27a0ecSDave Kleikamp * blocks/inodes before the group is live won't actually let us 899ac27a0ecSDave Kleikamp * allocate the new space yet. 900ac27a0ecSDave Kleikamp */ 901bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, ext4_blocks_count(es) + 902ac27a0ecSDave Kleikamp input->blocks_count); 903e8546d06SMarcin Slusarz le32_add_cpu(&es->s_inodes_count, EXT4_INODES_PER_GROUP(sb)); 904ac27a0ecSDave Kleikamp 905ac27a0ecSDave Kleikamp /* 906ac27a0ecSDave Kleikamp * We need to protect s_groups_count against other CPUs seeing 907ac27a0ecSDave Kleikamp * inconsistent state in the superblock. 908ac27a0ecSDave Kleikamp * 909ac27a0ecSDave Kleikamp * The precise rules we use are: 910ac27a0ecSDave Kleikamp * 911ac27a0ecSDave Kleikamp * * Writers must perform a smp_wmb() after updating all dependent 912ac27a0ecSDave Kleikamp * data and before modifying the groups count 913ac27a0ecSDave Kleikamp * 914ac27a0ecSDave Kleikamp * * Readers must perform an smp_rmb() after reading the groups count 915ac27a0ecSDave Kleikamp * and before reading any dependent data. 916ac27a0ecSDave Kleikamp * 917ac27a0ecSDave Kleikamp * NB. These rules can be relaxed when checking the group count 918ac27a0ecSDave Kleikamp * while freeing data, as we can only allocate from a block 919ac27a0ecSDave Kleikamp * group after serialising against the group count, and we can 920ac27a0ecSDave Kleikamp * only then free after serialising in turn against that 921ac27a0ecSDave Kleikamp * allocation. 922ac27a0ecSDave Kleikamp */ 923ac27a0ecSDave Kleikamp smp_wmb(); 924ac27a0ecSDave Kleikamp 925ac27a0ecSDave Kleikamp /* Update the global fs size fields */ 926ac27a0ecSDave Kleikamp sbi->s_groups_count++; 927ac27a0ecSDave Kleikamp 928b4097142STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, primary); 929b4097142STheodore Ts'o if (unlikely(err)) { 930b4097142STheodore Ts'o ext4_std_error(sb, err); 931b4097142STheodore Ts'o goto exit_journal; 932b4097142STheodore Ts'o } 933ac27a0ecSDave Kleikamp 934ac27a0ecSDave Kleikamp /* Update the reserved block counts only once the new group is 935ac27a0ecSDave Kleikamp * active. */ 936bd81d8eeSLaurent Vivier ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + 937ac27a0ecSDave Kleikamp input->reserved_blocks); 938ac27a0ecSDave Kleikamp 939ac27a0ecSDave Kleikamp /* Update the free space counts */ 94057042651STheodore Ts'o percpu_counter_add(&sbi->s_freeclusters_counter, 94157042651STheodore Ts'o EXT4_B2C(sbi, input->free_blocks_count)); 942aa0dff2dSPeter Zijlstra percpu_counter_add(&sbi->s_freeinodes_counter, 943617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb)); 944ac27a0ecSDave Kleikamp 94542007efdSEric Sandeen if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG) && 94642007efdSEric Sandeen sbi->s_log_groups_per_flex) { 947c62a11fdSFrederic Bohe ext4_group_t flex_group; 948c62a11fdSFrederic Bohe flex_group = ext4_flex_group(sbi, input->group); 94924aaa8efSTheodore Ts'o atomic_add(EXT4_B2C(sbi, input->free_blocks_count), 95024aaa8efSTheodore Ts'o &sbi->s_flex_groups[flex_group].free_clusters); 9519f24e420STheodore Ts'o atomic_add(EXT4_INODES_PER_GROUP(sb), 9529f24e420STheodore Ts'o &sbi->s_flex_groups[flex_group].free_inodes); 953c62a11fdSFrederic Bohe } 954c62a11fdSFrederic Bohe 955a0375156STheodore Ts'o ext4_handle_dirty_super(handle, sb); 956ac27a0ecSDave Kleikamp 957ac27a0ecSDave Kleikamp exit_journal: 958617ba13bSMingming Cao if ((err2 = ext4_journal_stop(handle)) && !err) 959ac27a0ecSDave Kleikamp err = err2; 9602f919710SYongqiang Yang if (!err && primary) { 961ac27a0ecSDave Kleikamp update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, 962617ba13bSMingming Cao sizeof(struct ext4_super_block)); 963ac27a0ecSDave Kleikamp update_backups(sb, primary->b_blocknr, primary->b_data, 964ac27a0ecSDave Kleikamp primary->b_size); 965ac27a0ecSDave Kleikamp } 966ac27a0ecSDave Kleikamp exit_put: 967ac27a0ecSDave Kleikamp iput(inode); 968ac27a0ecSDave Kleikamp return err; 969617ba13bSMingming Cao } /* ext4_group_add */ 970ac27a0ecSDave Kleikamp 9712b2d6d01STheodore Ts'o /* 9722b2d6d01STheodore Ts'o * Extend the filesystem to the new number of blocks specified. This entry 973ac27a0ecSDave Kleikamp * point is only used to extend the current filesystem to the end of the last 974ac27a0ecSDave Kleikamp * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" 975ac27a0ecSDave Kleikamp * for emergencies (because it has no dependencies on reserved blocks). 976ac27a0ecSDave Kleikamp * 977617ba13bSMingming Cao * If we _really_ wanted, we could use default values to call ext4_group_add() 978ac27a0ecSDave Kleikamp * allow the "remount" trick to work for arbitrary resizing, assuming enough 979ac27a0ecSDave Kleikamp * GDT blocks are reserved to grow to the desired size. 980ac27a0ecSDave Kleikamp */ 981617ba13bSMingming Cao int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, 982617ba13bSMingming Cao ext4_fsblk_t n_blocks_count) 983ac27a0ecSDave Kleikamp { 984617ba13bSMingming Cao ext4_fsblk_t o_blocks_count; 985617ba13bSMingming Cao ext4_grpblk_t last; 986617ba13bSMingming Cao ext4_grpblk_t add; 987ac27a0ecSDave Kleikamp struct buffer_head *bh; 988ac27a0ecSDave Kleikamp handle_t *handle; 989cc7365dfSYongqiang Yang int err, err2; 9905f21b0e6SFrederic Bohe ext4_group_t group; 991ac27a0ecSDave Kleikamp 992bd81d8eeSLaurent Vivier o_blocks_count = ext4_blocks_count(es); 993ac27a0ecSDave Kleikamp 994ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 9952b79b09dSYongqiang Yang printk(KERN_DEBUG "EXT4-fs: extending last group from %llu to %llu blocks\n", 996ac27a0ecSDave Kleikamp o_blocks_count, n_blocks_count); 997ac27a0ecSDave Kleikamp 998ac27a0ecSDave Kleikamp if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) 999ac27a0ecSDave Kleikamp return 0; 1000ac27a0ecSDave Kleikamp 1001ac27a0ecSDave Kleikamp if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { 1002617ba13bSMingming Cao printk(KERN_ERR "EXT4-fs: filesystem on %s:" 10032ae02107SMingming Cao " too large to resize to %llu blocks safely\n", 1004ac27a0ecSDave Kleikamp sb->s_id, n_blocks_count); 1005ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 100612062dddSEric Sandeen ext4_warning(sb, "CONFIG_LBDAF not enabled"); 1007ac27a0ecSDave Kleikamp return -EINVAL; 1008ac27a0ecSDave Kleikamp } 1009ac27a0ecSDave Kleikamp 1010ac27a0ecSDave Kleikamp if (n_blocks_count < o_blocks_count) { 101112062dddSEric Sandeen ext4_warning(sb, "can't shrink FS - resize aborted"); 10128f82f840SYongqiang Yang return -EINVAL; 1013ac27a0ecSDave Kleikamp } 1014ac27a0ecSDave Kleikamp 1015ac27a0ecSDave Kleikamp /* Handle the remaining blocks in the last group only. */ 10165f21b0e6SFrederic Bohe ext4_get_group_no_and_offset(sb, o_blocks_count, &group, &last); 1017ac27a0ecSDave Kleikamp 1018ac27a0ecSDave Kleikamp if (last == 0) { 101912062dddSEric Sandeen ext4_warning(sb, "need to use ext2online to resize further"); 1020ac27a0ecSDave Kleikamp return -EPERM; 1021ac27a0ecSDave Kleikamp } 1022ac27a0ecSDave Kleikamp 1023617ba13bSMingming Cao add = EXT4_BLOCKS_PER_GROUP(sb) - last; 1024ac27a0ecSDave Kleikamp 1025ac27a0ecSDave Kleikamp if (o_blocks_count + add < o_blocks_count) { 102612062dddSEric Sandeen ext4_warning(sb, "blocks_count overflow"); 1027ac27a0ecSDave Kleikamp return -EINVAL; 1028ac27a0ecSDave Kleikamp } 1029ac27a0ecSDave Kleikamp 1030ac27a0ecSDave Kleikamp if (o_blocks_count + add > n_blocks_count) 1031ac27a0ecSDave Kleikamp add = n_blocks_count - o_blocks_count; 1032ac27a0ecSDave Kleikamp 1033ac27a0ecSDave Kleikamp if (o_blocks_count + add < n_blocks_count) 103412062dddSEric Sandeen ext4_warning(sb, "will only finish group (%llu blocks, %u new)", 1035ac27a0ecSDave Kleikamp o_blocks_count + add, add); 1036ac27a0ecSDave Kleikamp 1037ac27a0ecSDave Kleikamp /* See if the device is actually as big as what was requested */ 1038ac27a0ecSDave Kleikamp bh = sb_bread(sb, o_blocks_count + add - 1); 1039ac27a0ecSDave Kleikamp if (!bh) { 104012062dddSEric Sandeen ext4_warning(sb, "can't read last block, resize aborted"); 1041ac27a0ecSDave Kleikamp return -ENOSPC; 1042ac27a0ecSDave Kleikamp } 1043ac27a0ecSDave Kleikamp brelse(bh); 1044ac27a0ecSDave Kleikamp 1045ac27a0ecSDave Kleikamp /* We will update the superblock, one block bitmap, and 1046617ba13bSMingming Cao * one group descriptor via ext4_free_blocks(). 1047ac27a0ecSDave Kleikamp */ 1048617ba13bSMingming Cao handle = ext4_journal_start_sb(sb, 3); 1049ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 1050ac27a0ecSDave Kleikamp err = PTR_ERR(handle); 105112062dddSEric Sandeen ext4_warning(sb, "error %d on journal start", err); 1052ac27a0ecSDave Kleikamp goto exit_put; 1053ac27a0ecSDave Kleikamp } 1054ac27a0ecSDave Kleikamp 1055617ba13bSMingming Cao if ((err = ext4_journal_get_write_access(handle, 1056617ba13bSMingming Cao EXT4_SB(sb)->s_sbh))) { 105712062dddSEric Sandeen ext4_warning(sb, "error %d on journal write access", err); 1058617ba13bSMingming Cao ext4_journal_stop(handle); 1059ac27a0ecSDave Kleikamp goto exit_put; 1060ac27a0ecSDave Kleikamp } 1061bd81d8eeSLaurent Vivier ext4_blocks_count_set(es, o_blocks_count + add); 1062c549a95dSEric Sandeen ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count, 1063ac27a0ecSDave Kleikamp o_blocks_count + add); 1064e21675d4SAneesh Kumar K.V /* We add the blocks to the bitmap and set the group need init bit */ 1065cc7365dfSYongqiang Yang err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); 1066a0375156STheodore Ts'o ext4_handle_dirty_super(handle, sb); 10672ae02107SMingming Cao ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, 1068ac27a0ecSDave Kleikamp o_blocks_count + add); 1069cc7365dfSYongqiang Yang err2 = ext4_journal_stop(handle); 1070cc7365dfSYongqiang Yang if (!err && err2) 1071cc7365dfSYongqiang Yang err = err2; 1072cc7365dfSYongqiang Yang 1073cc7365dfSYongqiang Yang if (err) 1074ac27a0ecSDave Kleikamp goto exit_put; 10755f21b0e6SFrederic Bohe 1076ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 1077bd81d8eeSLaurent Vivier printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n", 1078bd81d8eeSLaurent Vivier ext4_blocks_count(es)); 1079617ba13bSMingming Cao update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, 1080617ba13bSMingming Cao sizeof(struct ext4_super_block)); 1081ac27a0ecSDave Kleikamp exit_put: 1082ac27a0ecSDave Kleikamp return err; 1083617ba13bSMingming Cao } /* ext4_group_extend */ 1084