2 * linux/fs/ext3/resize.c
4 * Support for resizing an ext3 filesystem while it is mounted.
6 * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com>
8 * This could probably be made into a module, because it is not often in use.
17 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
18 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
20 static int verify_group_input(struct super_block *sb,
21 struct ext3_new_group_data *input)
23 struct ext3_sb_info *sbi = EXT3_SB(sb);
24 struct ext3_super_block *es = sbi->s_es;
25 ext3_fsblk_t start = le32_to_cpu(es->s_blocks_count);
26 ext3_fsblk_t end = start + input->blocks_count;
27 unsigned group = input->group;
28 ext3_fsblk_t itend = input->inode_table + sbi->s_itb_per_group;
29 unsigned overhead = ext3_bg_has_super(sb, group) ?
30 (1 + ext3_bg_num_gdb(sb, group) +
31 le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
32 ext3_fsblk_t metaend = start + overhead;
33 struct buffer_head *bh = NULL;
34 ext3_grpblk_t free_blocks_count;
37 input->free_blocks_count = free_blocks_count =
38 input->blocks_count - 2 - overhead - sbi->s_itb_per_group;
40 if (test_opt(sb, DEBUG))
41 printk(KERN_DEBUG "EXT3-fs: adding %s group %u: %u blocks "
42 "(%d free, %u reserved)\n",
43 ext3_bg_has_super(sb, input->group) ? "normal" :
44 "no-super", input->group, input->blocks_count,
45 free_blocks_count, input->reserved_blocks);
47 if (group != sbi->s_groups_count)
48 ext3_warning(sb, __func__,
49 "Cannot add at group %u (only %lu groups)",
50 input->group, sbi->s_groups_count);
51 else if ((start - le32_to_cpu(es->s_first_data_block)) %
52 EXT3_BLOCKS_PER_GROUP(sb))
53 ext3_warning(sb, __func__, "Last group not full");
54 else if (input->reserved_blocks > input->blocks_count / 5)
55 ext3_warning(sb, __func__, "Reserved blocks too high (%u)",
56 input->reserved_blocks);
57 else if (free_blocks_count < 0)
58 ext3_warning(sb, __func__, "Bad blocks count %u",
60 else if (!(bh = sb_bread(sb, end - 1)))
61 ext3_warning(sb, __func__,
62 "Cannot read last block ("E3FSBLK")",
64 else if (outside(input->block_bitmap, start, end))
65 ext3_warning(sb, __func__,
66 "Block bitmap not in group (block %u)",
68 else if (outside(input->inode_bitmap, start, end))
69 ext3_warning(sb, __func__,
70 "Inode bitmap not in group (block %u)",
72 else if (outside(input->inode_table, start, end) ||
73 outside(itend - 1, start, end))
74 ext3_warning(sb, __func__,
75 "Inode table not in group (blocks %u-"E3FSBLK")",
76 input->inode_table, itend - 1);
77 else if (input->inode_bitmap == input->block_bitmap)
78 ext3_warning(sb, __func__,
79 "Block bitmap same as inode bitmap (%u)",
81 else if (inside(input->block_bitmap, input->inode_table, itend))
82 ext3_warning(sb, __func__,
83 "Block bitmap (%u) in inode table (%u-"E3FSBLK")",
84 input->block_bitmap, input->inode_table, itend-1);
85 else if (inside(input->inode_bitmap, input->inode_table, itend))
86 ext3_warning(sb, __func__,
87 "Inode bitmap (%u) in inode table (%u-"E3FSBLK")",
88 input->inode_bitmap, input->inode_table, itend-1);
89 else if (inside(input->block_bitmap, start, metaend))
90 ext3_warning(sb, __func__,
91 "Block bitmap (%u) in GDT table"
92 " ("E3FSBLK"-"E3FSBLK")",
93 input->block_bitmap, start, metaend - 1);
94 else if (inside(input->inode_bitmap, start, metaend))
95 ext3_warning(sb, __func__,
96 "Inode bitmap (%u) in GDT table"
97 " ("E3FSBLK"-"E3FSBLK")",
98 input->inode_bitmap, start, metaend - 1);
99 else if (inside(input->inode_table, start, metaend) ||
100 inside(itend - 1, start, metaend))
101 ext3_warning(sb, __func__,
102 "Inode table (%u-"E3FSBLK") overlaps"
103 "GDT table ("E3FSBLK"-"E3FSBLK")",
104 input->inode_table, itend - 1, start, metaend - 1);
112 static struct buffer_head *bclean(handle_t *handle, struct super_block *sb,
115 struct buffer_head *bh;
118 bh = sb_getblk(sb, blk);
120 return ERR_PTR(-ENOMEM);
121 if ((err = ext3_journal_get_write_access(handle, bh))) {
126 memset(bh->b_data, 0, sb->s_blocksize);
127 set_buffer_uptodate(bh);
135 * To avoid calling the atomic setbit hundreds or thousands of times, we only
136 * need to use it within a single byte (to ensure we get endianness right).
137 * We can use memset for the rest of the bitmap as there are no other users.
139 static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
143 if (start_bit >= end_bit)
146 ext3_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
147 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
148 ext3_set_bit(i, bitmap);
150 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
154 * If we have fewer than thresh credits, extend by EXT3_MAX_TRANS_DATA.
155 * If that fails, restart the transaction & regain write access for the
156 * buffer head which is used for block_bitmap modifications.
158 static int extend_or_restart_transaction(handle_t *handle, int thresh,
159 struct buffer_head *bh)
163 if (handle->h_buffer_credits >= thresh)
166 err = ext3_journal_extend(handle, EXT3_MAX_TRANS_DATA);
170 err = ext3_journal_restart(handle, EXT3_MAX_TRANS_DATA);
173 err = ext3_journal_get_write_access(handle, bh);
182 * Set up the block and inode bitmaps, and the inode table for the new group.
183 * This doesn't need to be part of the main transaction, since we are only
184 * changing blocks outside the actual filesystem. We still do journaling to
185 * ensure the recovery is correct in case of a failure just after resize.
186 * If any part of this fails, we simply abort the resize.
188 static int setup_new_group_blocks(struct super_block *sb,
189 struct ext3_new_group_data *input)
191 struct ext3_sb_info *sbi = EXT3_SB(sb);
192 ext3_fsblk_t start = ext3_group_first_block_no(sb, input->group);
193 int reserved_gdb = ext3_bg_has_super(sb, input->group) ?
194 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
195 unsigned long gdblocks = ext3_bg_num_gdb(sb, input->group);
196 struct buffer_head *bh;
203 /* This transaction may be extended/restarted along the way */
204 handle = ext3_journal_start_sb(sb, EXT3_MAX_TRANS_DATA);
207 return PTR_ERR(handle);
209 mutex_lock(&sbi->s_resize_lock);
210 if (input->group != sbi->s_groups_count) {
215 if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
220 if (ext3_bg_has_super(sb, input->group)) {
221 ext3_debug("mark backup superblock %#04lx (+0)\n", start);
222 ext3_set_bit(0, bh->b_data);
225 /* Copy all of the GDT blocks into the backup in this group */
226 for (i = 0, bit = 1, block = start + 1;
227 i < gdblocks; i++, block++, bit++) {
228 struct buffer_head *gdb;
230 ext3_debug("update backup group %#04lx (+%d)\n", block, bit);
232 err = extend_or_restart_transaction(handle, 1, bh);
236 gdb = sb_getblk(sb, block);
237 if (unlikely(!gdb)) {
241 if ((err = ext3_journal_get_write_access(handle, gdb))) {
246 memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, gdb->b_size);
247 set_buffer_uptodate(gdb);
249 err = ext3_journal_dirty_metadata(handle, gdb);
254 ext3_set_bit(bit, bh->b_data);
258 /* Zero out all of the reserved backup group descriptor table blocks */
259 for (i = 0, bit = gdblocks + 1, block = start + bit;
260 i < reserved_gdb; i++, block++, bit++) {
261 struct buffer_head *gdb;
263 ext3_debug("clear reserved block %#04lx (+%d)\n", block, bit);
265 err = extend_or_restart_transaction(handle, 1, bh);
269 if (IS_ERR(gdb = bclean(handle, sb, block))) {
273 err = ext3_journal_dirty_metadata(handle, gdb);
278 ext3_set_bit(bit, bh->b_data);
281 ext3_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
282 input->block_bitmap - start);
283 ext3_set_bit(input->block_bitmap - start, bh->b_data);
284 ext3_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
285 input->inode_bitmap - start);
286 ext3_set_bit(input->inode_bitmap - start, bh->b_data);
288 /* Zero out all of the inode table blocks */
289 for (i = 0, block = input->inode_table, bit = block - start;
290 i < sbi->s_itb_per_group; i++, bit++, block++) {
291 struct buffer_head *it;
293 ext3_debug("clear inode block %#04lx (+%d)\n", block, bit);
295 err = extend_or_restart_transaction(handle, 1, bh);
299 if (IS_ERR(it = bclean(handle, sb, block))) {
303 err = ext3_journal_dirty_metadata(handle, it);
309 ext3_set_bit(bit, bh->b_data);
312 err = extend_or_restart_transaction(handle, 2, bh);
316 mark_bitmap_end(input->blocks_count, EXT3_BLOCKS_PER_GROUP(sb),
318 err = ext3_journal_dirty_metadata(handle, bh);
323 /* Mark unused entries in inode bitmap used */
324 ext3_debug("clear inode bitmap %#04x (+%ld)\n",
325 input->inode_bitmap, input->inode_bitmap - start);
326 if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) {
331 mark_bitmap_end(EXT3_INODES_PER_GROUP(sb), EXT3_BLOCKS_PER_GROUP(sb),
333 err = ext3_journal_dirty_metadata(handle, bh);
338 mutex_unlock(&sbi->s_resize_lock);
339 if ((err2 = ext3_journal_stop(handle)) && !err)
346 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
347 * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
348 * calling this for the first time. In a sparse filesystem it will be the
349 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
350 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
352 static unsigned ext3_list_backups(struct super_block *sb, unsigned *three,
353 unsigned *five, unsigned *seven)
355 unsigned *min = three;
359 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
360 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
382 * Check that all of the backup GDT blocks are held in the primary GDT block.
383 * It is assumed that they are stored in group order. Returns the number of
384 * groups in current filesystem that have BACKUPS, or -ve error code.
386 static int verify_reserved_gdb(struct super_block *sb,
387 struct buffer_head *primary)
389 const ext3_fsblk_t blk = primary->b_blocknr;
390 const unsigned long end = EXT3_SB(sb)->s_groups_count;
395 __le32 *p = (__le32 *)primary->b_data;
398 while ((grp = ext3_list_backups(sb, &three, &five, &seven)) < end) {
399 if (le32_to_cpu(*p++) != grp * EXT3_BLOCKS_PER_GROUP(sb) + blk){
400 ext3_warning(sb, __func__,
401 "reserved GDT "E3FSBLK
402 " missing grp %d ("E3FSBLK")",
404 grp * EXT3_BLOCKS_PER_GROUP(sb) + blk);
407 if (++gdbackups > EXT3_ADDR_PER_BLOCK(sb))
415 * Called when we need to bring a reserved group descriptor table block into
416 * use from the resize inode. The primary copy of the new GDT block currently
417 * is an indirect block (under the double indirect block in the resize inode).
418 * The new backup GDT blocks will be stored as leaf blocks in this indirect
419 * block, in group order. Even though we know all the block numbers we need,
420 * we check to ensure that the resize inode has actually reserved these blocks.
422 * Don't need to update the block bitmaps because the blocks are still in use.
424 * We get all of the error cases out of the way, so that we are sure to not
425 * fail once we start modifying the data on disk, because JBD has no rollback.
427 static int add_new_gdb(handle_t *handle, struct inode *inode,
428 struct ext3_new_group_data *input,
429 struct buffer_head **primary)
431 struct super_block *sb = inode->i_sb;
432 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
433 unsigned long gdb_num = input->group / EXT3_DESC_PER_BLOCK(sb);
434 ext3_fsblk_t gdblock = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num;
435 struct buffer_head **o_group_desc, **n_group_desc;
436 struct buffer_head *dind;
438 struct ext3_iloc iloc;
442 if (test_opt(sb, DEBUG))
444 "EXT3-fs: ext3_add_new_gdb: adding group block %lu\n",
448 * If we are not using the primary superblock/GDT copy don't resize,
449 * because the user tools have no way of handling this. Probably a
450 * bad time to do it anyways.
452 if (EXT3_SB(sb)->s_sbh->b_blocknr !=
453 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) {
454 ext3_warning(sb, __func__,
455 "won't resize using backup superblock at %llu",
456 (unsigned long long)EXT3_SB(sb)->s_sbh->b_blocknr);
460 *primary = sb_bread(sb, gdblock);
464 if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) {
469 data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
470 dind = sb_bread(sb, le32_to_cpu(*data));
476 data = (__le32 *)dind->b_data;
477 if (le32_to_cpu(data[gdb_num % EXT3_ADDR_PER_BLOCK(sb)]) != gdblock) {
478 ext3_warning(sb, __func__,
479 "new group %u GDT block "E3FSBLK" not reserved",
480 input->group, gdblock);
485 if ((err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh)))
488 if ((err = ext3_journal_get_write_access(handle, *primary)))
491 if ((err = ext3_journal_get_write_access(handle, dind)))
494 /* ext3_reserve_inode_write() gets a reference on the iloc */
495 if ((err = ext3_reserve_inode_write(handle, inode, &iloc)))
498 n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
502 ext3_warning (sb, __func__,
503 "not enough memory for %lu groups", gdb_num + 1);
508 * Finally, we have all of the possible failures behind us...
510 * Remove new GDT block from inode double-indirect block and clear out
511 * the new GDT block for use (which also "frees" the backup GDT blocks
512 * from the reserved inode). We don't need to change the bitmaps for
513 * these blocks, because they are marked as in-use from being in the
514 * reserved inode, and will become GDT blocks (primary and backup).
516 data[gdb_num % EXT3_ADDR_PER_BLOCK(sb)] = 0;
517 err = ext3_journal_dirty_metadata(handle, dind);
519 goto exit_group_desc;
522 inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
523 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
525 goto exit_group_desc;
526 memset((*primary)->b_data, 0, sb->s_blocksize);
527 err = ext3_journal_dirty_metadata(handle, *primary);
529 goto exit_group_desc;
531 o_group_desc = EXT3_SB(sb)->s_group_desc;
532 memcpy(n_group_desc, o_group_desc,
533 EXT3_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
534 n_group_desc[gdb_num] = *primary;
535 EXT3_SB(sb)->s_group_desc = n_group_desc;
536 EXT3_SB(sb)->s_gdb_count++;
539 le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
540 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
549 //ext3_journal_release_buffer(handle, iloc.bh);
552 //ext3_journal_release_buffer(handle, dind);
554 //ext3_journal_release_buffer(handle, *primary);
556 //ext3_journal_release_buffer(handle, *primary);
562 ext3_debug("leaving with error %d\n", err);
567 * Called when we are adding a new group which has a backup copy of each of
568 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
569 * We need to add these reserved backup GDT blocks to the resize inode, so
570 * that they are kept for future resizing and not allocated to files.
572 * Each reserved backup GDT block will go into a different indirect block.
573 * The indirect blocks are actually the primary reserved GDT blocks,
574 * so we know in advance what their block numbers are. We only get the
575 * double-indirect block to verify it is pointing to the primary reserved
576 * GDT blocks so we don't overwrite a data block by accident. The reserved
577 * backup GDT blocks are stored in their reserved primary GDT block.
579 static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
580 struct ext3_new_group_data *input)
582 struct super_block *sb = inode->i_sb;
583 int reserved_gdb =le16_to_cpu(EXT3_SB(sb)->s_es->s_reserved_gdt_blocks);
584 struct buffer_head **primary;
585 struct buffer_head *dind;
586 struct ext3_iloc iloc;
593 primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_NOFS);
597 data = EXT3_I(inode)->i_data + EXT3_DIND_BLOCK;
598 dind = sb_bread(sb, le32_to_cpu(*data));
604 blk = EXT3_SB(sb)->s_sbh->b_blocknr + 1 + EXT3_SB(sb)->s_gdb_count;
605 data = (__le32 *)dind->b_data + (EXT3_SB(sb)->s_gdb_count %
606 EXT3_ADDR_PER_BLOCK(sb));
607 end = (__le32 *)dind->b_data + EXT3_ADDR_PER_BLOCK(sb);
609 /* Get each reserved primary GDT block and verify it holds backups */
610 for (res = 0; res < reserved_gdb; res++, blk++) {
611 if (le32_to_cpu(*data) != blk) {
612 ext3_warning(sb, __func__,
613 "reserved block "E3FSBLK
614 " not at offset %ld",
616 (long)(data - (__le32 *)dind->b_data));
620 primary[res] = sb_bread(sb, blk);
625 if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) {
626 brelse(primary[res]);
631 data = (__le32 *)dind->b_data;
634 for (i = 0; i < reserved_gdb; i++) {
635 if ((err = ext3_journal_get_write_access(handle, primary[i]))) {
638 for (j = 0; j < i; j++)
639 ext3_journal_release_buffer(handle, primary[j]);
645 if ((err = ext3_reserve_inode_write(handle, inode, &iloc)))
649 * Finally we can add each of the reserved backup GDT blocks from
650 * the new group to its reserved primary GDT block.
652 blk = input->group * EXT3_BLOCKS_PER_GROUP(sb);
653 for (i = 0; i < reserved_gdb; i++) {
655 data = (__le32 *)primary[i]->b_data;
656 /* printk("reserving backup %lu[%u] = %lu\n",
657 primary[i]->b_blocknr, gdbackups,
658 blk + primary[i]->b_blocknr); */
659 data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr);
660 err2 = ext3_journal_dirty_metadata(handle, primary[i]);
664 inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
665 ext3_mark_iloc_dirty(handle, inode, &iloc);
669 brelse(primary[res]);
679 * Update the backup copies of the ext3 metadata. These don't need to be part
680 * of the main resize transaction, because e2fsck will re-write them if there
681 * is a problem (basically only OOM will cause a problem). However, we
682 * _should_ update the backups if possible, in case the primary gets trashed
683 * for some reason and we need to run e2fsck from a backup superblock. The
684 * important part is that the new block and inode counts are in the backup
685 * superblocks, and the location of the new group metadata in the GDT backups.
687 * We do not need take the s_resize_lock for this, because these
688 * blocks are not otherwise touched by the filesystem code when it is
689 * mounted. We don't need to worry about last changing from
690 * sbi->s_groups_count, because the worst that can happen is that we
691 * do not copy the full number of backups at this time. The resize
692 * which changed s_groups_count will backup again.
694 static void update_backups(struct super_block *sb,
695 int blk_off, char *data, int size)
697 struct ext3_sb_info *sbi = EXT3_SB(sb);
698 const unsigned long last = sbi->s_groups_count;
699 const int bpg = EXT3_BLOCKS_PER_GROUP(sb);
704 int rest = sb->s_blocksize - size;
708 handle = ext3_journal_start_sb(sb, EXT3_MAX_TRANS_DATA);
709 if (IS_ERR(handle)) {
711 err = PTR_ERR(handle);
715 while ((group = ext3_list_backups(sb, &three, &five, &seven)) < last) {
716 struct buffer_head *bh;
718 /* Out of journal space, and can't get more - abort - so sad */
719 if (handle->h_buffer_credits == 0 &&
720 ext3_journal_extend(handle, EXT3_MAX_TRANS_DATA) &&
721 (err = ext3_journal_restart(handle, EXT3_MAX_TRANS_DATA)))
724 bh = sb_getblk(sb, group * bpg + blk_off);
729 ext3_debug("update metadata backup %#04lx\n",
730 (unsigned long)bh->b_blocknr);
731 if ((err = ext3_journal_get_write_access(handle, bh))) {
736 memcpy(bh->b_data, data, size);
738 memset(bh->b_data + size, 0, rest);
739 set_buffer_uptodate(bh);
741 err = ext3_journal_dirty_metadata(handle, bh);
746 if ((err2 = ext3_journal_stop(handle)) && !err)
750 * Ugh! Need to have e2fsck write the backup copies. It is too
751 * late to revert the resize, we shouldn't fail just because of
752 * the backup copies (they are only needed in case of corruption).
754 * However, if we got here we have a journal problem too, so we
755 * can't really start a transaction to mark the superblock.
756 * Chicken out and just set the flag on the hope it will be written
757 * to disk, and if not - we will simply wait until next fsck.
761 ext3_warning(sb, __func__,
762 "can't update backup for group %d (err %d), "
763 "forcing fsck on next reboot", group, err);
764 sbi->s_mount_state &= ~EXT3_VALID_FS;
765 sbi->s_es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
766 mark_buffer_dirty(sbi->s_sbh);
770 /* Add group descriptor data to an existing or new group descriptor block.
771 * Ensure we handle all possible error conditions _before_ we start modifying
772 * the filesystem, because we cannot abort the transaction and not have it
773 * write the data to disk.
775 * If we are on a GDT block boundary, we need to get the reserved GDT block.
776 * Otherwise, we may need to add backup GDT blocks for a sparse group.
778 * We only need to hold the superblock lock while we are actually adding
779 * in the new group's counts to the superblock. Prior to that we have
780 * not really "added" the group at all. We re-check that we are still
781 * adding in the last group in case things have changed since verifying.
783 int ext3_group_add(struct super_block *sb, struct ext3_new_group_data *input)
785 struct ext3_sb_info *sbi = EXT3_SB(sb);
786 struct ext3_super_block *es = sbi->s_es;
787 int reserved_gdb = ext3_bg_has_super(sb, input->group) ?
788 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
789 struct buffer_head *primary = NULL;
790 struct ext3_group_desc *gdp;
791 struct inode *inode = NULL;
793 int gdb_off, gdb_num;
796 gdb_num = input->group / EXT3_DESC_PER_BLOCK(sb);
797 gdb_off = input->group % EXT3_DESC_PER_BLOCK(sb);
799 if (gdb_off == 0 && !EXT3_HAS_RO_COMPAT_FEATURE(sb,
800 EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
801 ext3_warning(sb, __func__,
802 "Can't resize non-sparse filesystem further");
806 if (le32_to_cpu(es->s_blocks_count) + input->blocks_count <
807 le32_to_cpu(es->s_blocks_count)) {
808 ext3_warning(sb, __func__, "blocks_count overflow\n");
812 if (le32_to_cpu(es->s_inodes_count) + EXT3_INODES_PER_GROUP(sb) <
813 le32_to_cpu(es->s_inodes_count)) {
814 ext3_warning(sb, __func__, "inodes_count overflow\n");
818 if (reserved_gdb || gdb_off == 0) {
819 if (!EXT3_HAS_COMPAT_FEATURE(sb,
820 EXT3_FEATURE_COMPAT_RESIZE_INODE)
821 || !le16_to_cpu(es->s_reserved_gdt_blocks)) {
822 ext3_warning(sb, __func__,
823 "No reserved GDT blocks, can't resize");
826 inode = ext3_iget(sb, EXT3_RESIZE_INO);
828 ext3_warning(sb, __func__,
829 "Error opening resize inode");
830 return PTR_ERR(inode);
834 if ((err = verify_group_input(sb, input)))
837 if ((err = setup_new_group_blocks(sb, input)))
841 * We will always be modifying at least the superblock and a GDT
842 * block. If we are adding a group past the last current GDT block,
843 * we will also modify the inode and the dindirect block. If we
844 * are adding a group with superblock/GDT backups we will also
845 * modify each of the reserved GDT dindirect blocks.
847 handle = ext3_journal_start_sb(sb,
848 ext3_bg_has_super(sb, input->group) ?
849 3 + reserved_gdb : 4);
850 if (IS_ERR(handle)) {
851 err = PTR_ERR(handle);
855 mutex_lock(&sbi->s_resize_lock);
856 if (input->group != sbi->s_groups_count) {
857 ext3_warning(sb, __func__,
858 "multiple resizers run on filesystem!");
863 if ((err = ext3_journal_get_write_access(handle, sbi->s_sbh)))
867 * We will only either add reserved group blocks to a backup group
868 * or remove reserved blocks for the first group in a new group block.
869 * Doing both would be mean more complex code, and sane people don't
870 * use non-sparse filesystems anymore. This is already checked above.
873 primary = sbi->s_group_desc[gdb_num];
874 if ((err = ext3_journal_get_write_access(handle, primary)))
877 if (reserved_gdb && ext3_bg_num_gdb(sb, input->group) &&
878 (err = reserve_backup_gdb(handle, inode, input)))
880 } else if ((err = add_new_gdb(handle, inode, input, &primary)))
884 * OK, now we've set up the new group. Time to make it active.
886 * We do not lock all allocations via s_resize_lock
887 * so we have to be safe wrt. concurrent accesses the group
888 * data. So we need to be careful to set all of the relevant
889 * group descriptor data etc. *before* we enable the group.
891 * The key field here is sbi->s_groups_count: as long as
892 * that retains its old value, nobody is going to access the new
895 * So first we update all the descriptor metadata for the new
896 * group; then we update the total disk blocks count; then we
897 * update the groups count to enable the group; then finally we
898 * update the free space counts so that the system can start
899 * using the new disk blocks.
902 /* Update group descriptor block for new group */
903 gdp = (struct ext3_group_desc *)primary->b_data + gdb_off;
905 gdp->bg_block_bitmap = cpu_to_le32(input->block_bitmap);
906 gdp->bg_inode_bitmap = cpu_to_le32(input->inode_bitmap);
907 gdp->bg_inode_table = cpu_to_le32(input->inode_table);
908 gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
909 gdp->bg_free_inodes_count = cpu_to_le16(EXT3_INODES_PER_GROUP(sb));
912 * Make the new blocks and inodes valid next. We do this before
913 * increasing the group count so that once the group is enabled,
914 * all of its blocks and inodes are already valid.
916 * We always allocate group-by-group, then block-by-block or
917 * inode-by-inode within a group, so enabling these
918 * blocks/inodes before the group is live won't actually let us
919 * allocate the new space yet.
921 le32_add_cpu(&es->s_blocks_count, input->blocks_count);
922 le32_add_cpu(&es->s_inodes_count, EXT3_INODES_PER_GROUP(sb));
925 * We need to protect s_groups_count against other CPUs seeing
926 * inconsistent state in the superblock.
928 * The precise rules we use are:
930 * * Writers of s_groups_count *must* hold s_resize_lock
932 * * Writers must perform a smp_wmb() after updating all dependent
933 * data and before modifying the groups count
935 * * Readers must hold s_resize_lock over the access
937 * * Readers must perform an smp_rmb() after reading the groups count
938 * and before reading any dependent data.
940 * NB. These rules can be relaxed when checking the group count
941 * while freeing data, as we can only allocate from a block
942 * group after serialising against the group count, and we can
943 * only then free after serialising in turn against that
948 /* Update the global fs size fields */
949 sbi->s_groups_count++;
951 err = ext3_journal_dirty_metadata(handle, primary);
955 /* Update the reserved block counts only once the new group is
957 le32_add_cpu(&es->s_r_blocks_count, input->reserved_blocks);
959 /* Update the free space counts */
960 percpu_counter_add(&sbi->s_freeblocks_counter,
961 input->free_blocks_count);
962 percpu_counter_add(&sbi->s_freeinodes_counter,
963 EXT3_INODES_PER_GROUP(sb));
965 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
968 mutex_unlock(&sbi->s_resize_lock);
969 if ((err2 = ext3_journal_stop(handle)) && !err)
972 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
973 sizeof(struct ext3_super_block));
974 update_backups(sb, primary->b_blocknr, primary->b_data,
980 } /* ext3_group_add */
982 /* Extend the filesystem to the new number of blocks specified. This entry
983 * point is only used to extend the current filesystem to the end of the last
984 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
985 * for emergencies (because it has no dependencies on reserved blocks).
987 * If we _really_ wanted, we could use default values to call ext3_group_add()
988 * allow the "remount" trick to work for arbitrary resizing, assuming enough
989 * GDT blocks are reserved to grow to the desired size.
991 int ext3_group_extend(struct super_block *sb, struct ext3_super_block *es,
992 ext3_fsblk_t n_blocks_count)
994 ext3_fsblk_t o_blocks_count;
997 struct buffer_head * bh;
1000 unsigned long freed_blocks;
1002 /* We don't need to worry about locking wrt other resizers just
1003 * yet: we're going to revalidate es->s_blocks_count after
1004 * taking the s_resize_lock below. */
1005 o_blocks_count = le32_to_cpu(es->s_blocks_count);
1007 if (test_opt(sb, DEBUG))
1008 printk(KERN_DEBUG "EXT3-fs: extending last group from "E3FSBLK
1009 " up to "E3FSBLK" blocks\n",
1010 o_blocks_count, n_blocks_count);
1012 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1015 if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1016 printk(KERN_ERR "EXT3-fs: filesystem on %s:"
1017 " too large to resize to "E3FSBLK" blocks safely\n",
1018 sb->s_id, n_blocks_count);
1019 if (sizeof(sector_t) < 8)
1020 ext3_warning(sb, __func__,
1021 "CONFIG_LBDAF not enabled\n");
1025 if (n_blocks_count < o_blocks_count) {
1026 ext3_warning(sb, __func__,
1027 "can't shrink FS - resize aborted");
1031 /* Handle the remaining blocks in the last group only. */
1032 last = (o_blocks_count - le32_to_cpu(es->s_first_data_block)) %
1033 EXT3_BLOCKS_PER_GROUP(sb);
1036 ext3_warning(sb, __func__,
1037 "need to use ext2online to resize further");
1041 add = EXT3_BLOCKS_PER_GROUP(sb) - last;
1043 if (o_blocks_count + add < o_blocks_count) {
1044 ext3_warning(sb, __func__, "blocks_count overflow");
1048 if (o_blocks_count + add > n_blocks_count)
1049 add = n_blocks_count - o_blocks_count;
1051 if (o_blocks_count + add < n_blocks_count)
1052 ext3_warning(sb, __func__,
1053 "will only finish group ("E3FSBLK
1055 o_blocks_count + add, add);
1057 /* See if the device is actually as big as what was requested */
1058 bh = sb_bread(sb, o_blocks_count + add -1);
1060 ext3_warning(sb, __func__,
1061 "can't read last block, resize aborted");
1066 /* We will update the superblock, one block bitmap, and
1067 * one group descriptor via ext3_free_blocks().
1069 handle = ext3_journal_start_sb(sb, 3);
1070 if (IS_ERR(handle)) {
1071 err = PTR_ERR(handle);
1072 ext3_warning(sb, __func__, "error %d on journal start",err);
1076 mutex_lock(&EXT3_SB(sb)->s_resize_lock);
1077 if (o_blocks_count != le32_to_cpu(es->s_blocks_count)) {
1078 ext3_warning(sb, __func__,
1079 "multiple resizers run on filesystem!");
1080 mutex_unlock(&EXT3_SB(sb)->s_resize_lock);
1081 ext3_journal_stop(handle);
1086 if ((err = ext3_journal_get_write_access(handle,
1087 EXT3_SB(sb)->s_sbh))) {
1088 ext3_warning(sb, __func__,
1089 "error %d on journal write access", err);
1090 mutex_unlock(&EXT3_SB(sb)->s_resize_lock);
1091 ext3_journal_stop(handle);
1094 es->s_blocks_count = cpu_to_le32(o_blocks_count + add);
1095 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
1096 mutex_unlock(&EXT3_SB(sb)->s_resize_lock);
1098 ext3_warning(sb, __func__,
1099 "error %d on journal dirty metadata", err);
1100 ext3_journal_stop(handle);
1103 ext3_debug("freeing blocks "E3FSBLK" through "E3FSBLK"\n",
1104 o_blocks_count, o_blocks_count + add);
1105 ext3_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
1106 ext3_debug("freed blocks "E3FSBLK" through "E3FSBLK"\n",
1107 o_blocks_count, o_blocks_count + add);
1108 if ((err = ext3_journal_stop(handle)))
1110 if (test_opt(sb, DEBUG))
1111 printk(KERN_DEBUG "EXT3-fs: extended group to %u blocks\n",
1112 le32_to_cpu(es->s_blocks_count));
1113 update_backups(sb, EXT3_SB(sb)->s_sbh->b_blocknr, (char *)es,
1114 sizeof(struct ext3_super_block));
1117 } /* ext3_group_extend */