2 * linux/fs/ext4/resize.c
4 * Support for resizing an ext4 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.
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/ext4_jbd2.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
22 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
23 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
25 static int verify_group_input(struct super_block
*sb
,
26 struct ext4_new_group_data
*input
)
28 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
29 struct ext4_super_block
*es
= sbi
->s_es
;
30 ext4_fsblk_t start
= ext4_blocks_count(es
);
31 ext4_fsblk_t end
= start
+ input
->blocks_count
;
32 unsigned group
= input
->group
;
33 ext4_fsblk_t itend
= input
->inode_table
+ sbi
->s_itb_per_group
;
34 unsigned overhead
= ext4_bg_has_super(sb
, group
) ?
35 (1 + ext4_bg_num_gdb(sb
, group
) +
36 le16_to_cpu(es
->s_reserved_gdt_blocks
)) : 0;
37 ext4_fsblk_t metaend
= start
+ overhead
;
38 struct buffer_head
*bh
= NULL
;
39 ext4_grpblk_t free_blocks_count
, offset
;
42 input
->free_blocks_count
= free_blocks_count
=
43 input
->blocks_count
- 2 - overhead
- sbi
->s_itb_per_group
;
45 if (test_opt(sb
, DEBUG
))
46 printk(KERN_DEBUG
"EXT4-fs: adding %s group %u: %u blocks "
47 "(%d free, %u reserved)\n",
48 ext4_bg_has_super(sb
, input
->group
) ? "normal" :
49 "no-super", input
->group
, input
->blocks_count
,
50 free_blocks_count
, input
->reserved_blocks
);
52 ext4_get_group_no_and_offset(sb
, start
, NULL
, &offset
);
53 if (group
!= sbi
->s_groups_count
)
54 ext4_warning(sb
, __FUNCTION__
,
55 "Cannot add at group %u (only %lu groups)",
56 input
->group
, sbi
->s_groups_count
);
58 ext4_warning(sb
, __FUNCTION__
, "Last group not full");
59 else if (input
->reserved_blocks
> input
->blocks_count
/ 5)
60 ext4_warning(sb
, __FUNCTION__
, "Reserved blocks too high (%u)",
61 input
->reserved_blocks
);
62 else if (free_blocks_count
< 0)
63 ext4_warning(sb
, __FUNCTION__
, "Bad blocks count %u",
65 else if (!(bh
= sb_bread(sb
, end
- 1)))
66 ext4_warning(sb
, __FUNCTION__
,
67 "Cannot read last block (%llu)",
69 else if (outside(input
->block_bitmap
, start
, end
))
70 ext4_warning(sb
, __FUNCTION__
,
71 "Block bitmap not in group (block %llu)",
73 else if (outside(input
->inode_bitmap
, start
, end
))
74 ext4_warning(sb
, __FUNCTION__
,
75 "Inode bitmap not in group (block %llu)",
77 else if (outside(input
->inode_table
, start
, end
) ||
78 outside(itend
- 1, start
, end
))
79 ext4_warning(sb
, __FUNCTION__
,
80 "Inode table not in group (blocks %llu-%llu)",
81 input
->inode_table
, itend
- 1);
82 else if (input
->inode_bitmap
== input
->block_bitmap
)
83 ext4_warning(sb
, __FUNCTION__
,
84 "Block bitmap same as inode bitmap (%llu)",
86 else if (inside(input
->block_bitmap
, input
->inode_table
, itend
))
87 ext4_warning(sb
, __FUNCTION__
,
88 "Block bitmap (%llu) in inode table (%llu-%llu)",
89 input
->block_bitmap
, input
->inode_table
, itend
-1);
90 else if (inside(input
->inode_bitmap
, input
->inode_table
, itend
))
91 ext4_warning(sb
, __FUNCTION__
,
92 "Inode bitmap (%llu) in inode table (%llu-%llu)",
93 input
->inode_bitmap
, input
->inode_table
, itend
-1);
94 else if (inside(input
->block_bitmap
, start
, metaend
))
95 ext4_warning(sb
, __FUNCTION__
,
96 "Block bitmap (%llu) in GDT table"
98 input
->block_bitmap
, start
, metaend
- 1);
99 else if (inside(input
->inode_bitmap
, start
, metaend
))
100 ext4_warning(sb
, __FUNCTION__
,
101 "Inode bitmap (%llu) in GDT table"
103 input
->inode_bitmap
, start
, metaend
- 1);
104 else if (inside(input
->inode_table
, start
, metaend
) ||
105 inside(itend
- 1, start
, metaend
))
106 ext4_warning(sb
, __FUNCTION__
,
107 "Inode table (%llu-%llu) overlaps"
108 "GDT table (%llu-%llu)",
109 input
->inode_table
, itend
- 1, start
, metaend
- 1);
117 static struct buffer_head
*bclean(handle_t
*handle
, struct super_block
*sb
,
120 struct buffer_head
*bh
;
123 bh
= sb_getblk(sb
, blk
);
125 return ERR_PTR(-EIO
);
126 if ((err
= ext4_journal_get_write_access(handle
, bh
))) {
131 memset(bh
->b_data
, 0, sb
->s_blocksize
);
132 set_buffer_uptodate(bh
);
140 * To avoid calling the atomic setbit hundreds or thousands of times, we only
141 * need to use it within a single byte (to ensure we get endianness right).
142 * We can use memset for the rest of the bitmap as there are no other users.
144 static void mark_bitmap_end(int start_bit
, int end_bit
, char *bitmap
)
148 if (start_bit
>= end_bit
)
151 ext4_debug("mark end bits +%d through +%d used\n", start_bit
, end_bit
);
152 for (i
= start_bit
; i
< ((start_bit
+ 7) & ~7UL); i
++)
153 ext4_set_bit(i
, bitmap
);
155 memset(bitmap
+ (i
>> 3), 0xff, (end_bit
- i
) >> 3);
159 * Set up the block and inode bitmaps, and the inode table for the new group.
160 * This doesn't need to be part of the main transaction, since we are only
161 * changing blocks outside the actual filesystem. We still do journaling to
162 * ensure the recovery is correct in case of a failure just after resize.
163 * If any part of this fails, we simply abort the resize.
165 static int setup_new_group_blocks(struct super_block
*sb
,
166 struct ext4_new_group_data
*input
)
168 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
169 ext4_fsblk_t start
= ext4_group_first_block_no(sb
, input
->group
);
170 int reserved_gdb
= ext4_bg_has_super(sb
, input
->group
) ?
171 le16_to_cpu(sbi
->s_es
->s_reserved_gdt_blocks
) : 0;
172 unsigned long gdblocks
= ext4_bg_num_gdb(sb
, input
->group
);
173 struct buffer_head
*bh
;
180 handle
= ext4_journal_start_sb(sb
, reserved_gdb
+ gdblocks
+
181 2 + sbi
->s_itb_per_group
);
183 return PTR_ERR(handle
);
186 if (input
->group
!= sbi
->s_groups_count
) {
191 if (IS_ERR(bh
= bclean(handle
, sb
, input
->block_bitmap
))) {
196 if (ext4_bg_has_super(sb
, input
->group
)) {
197 ext4_debug("mark backup superblock %#04lx (+0)\n", start
);
198 ext4_set_bit(0, bh
->b_data
);
201 /* Copy all of the GDT blocks into the backup in this group */
202 for (i
= 0, bit
= 1, block
= start
+ 1;
203 i
< gdblocks
; i
++, block
++, bit
++) {
204 struct buffer_head
*gdb
;
206 ext4_debug("update backup group %#04lx (+%d)\n", block
, bit
);
208 gdb
= sb_getblk(sb
, block
);
213 if ((err
= ext4_journal_get_write_access(handle
, gdb
))) {
218 memcpy(gdb
->b_data
, sbi
->s_group_desc
[i
]->b_data
, bh
->b_size
);
219 set_buffer_uptodate(gdb
);
221 ext4_journal_dirty_metadata(handle
, gdb
);
222 ext4_set_bit(bit
, bh
->b_data
);
226 /* Zero out all of the reserved backup group descriptor table blocks */
227 for (i
= 0, bit
= gdblocks
+ 1, block
= start
+ bit
;
228 i
< reserved_gdb
; i
++, block
++, bit
++) {
229 struct buffer_head
*gdb
;
231 ext4_debug("clear reserved block %#04lx (+%d)\n", block
, bit
);
233 if (IS_ERR(gdb
= bclean(handle
, sb
, block
))) {
237 ext4_journal_dirty_metadata(handle
, gdb
);
238 ext4_set_bit(bit
, bh
->b_data
);
241 ext4_debug("mark block bitmap %#04x (+%ld)\n", input
->block_bitmap
,
242 input
->block_bitmap
- start
);
243 ext4_set_bit(input
->block_bitmap
- start
, bh
->b_data
);
244 ext4_debug("mark inode bitmap %#04x (+%ld)\n", input
->inode_bitmap
,
245 input
->inode_bitmap
- start
);
246 ext4_set_bit(input
->inode_bitmap
- start
, bh
->b_data
);
248 /* Zero out all of the inode table blocks */
249 for (i
= 0, block
= input
->inode_table
, bit
= block
- start
;
250 i
< sbi
->s_itb_per_group
; i
++, bit
++, block
++) {
251 struct buffer_head
*it
;
253 ext4_debug("clear inode block %#04lx (+%d)\n", block
, bit
);
254 if (IS_ERR(it
= bclean(handle
, sb
, block
))) {
258 ext4_journal_dirty_metadata(handle
, it
);
260 ext4_set_bit(bit
, bh
->b_data
);
262 mark_bitmap_end(input
->blocks_count
, EXT4_BLOCKS_PER_GROUP(sb
),
264 ext4_journal_dirty_metadata(handle
, bh
);
267 /* Mark unused entries in inode bitmap used */
268 ext4_debug("clear inode bitmap %#04x (+%ld)\n",
269 input
->inode_bitmap
, input
->inode_bitmap
- start
);
270 if (IS_ERR(bh
= bclean(handle
, sb
, input
->inode_bitmap
))) {
275 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb
), EXT4_BLOCKS_PER_GROUP(sb
),
277 ext4_journal_dirty_metadata(handle
, bh
);
283 if ((err2
= ext4_journal_stop(handle
)) && !err
)
291 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
292 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
293 * calling this for the first time. In a sparse filesystem it will be the
294 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
295 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
297 static unsigned ext4_list_backups(struct super_block
*sb
, unsigned *three
,
298 unsigned *five
, unsigned *seven
)
300 unsigned *min
= three
;
304 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb
,
305 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
327 * Check that all of the backup GDT blocks are held in the primary GDT block.
328 * It is assumed that they are stored in group order. Returns the number of
329 * groups in current filesystem that have BACKUPS, or -ve error code.
331 static int verify_reserved_gdb(struct super_block
*sb
,
332 struct buffer_head
*primary
)
334 const ext4_fsblk_t blk
= primary
->b_blocknr
;
335 const unsigned long end
= EXT4_SB(sb
)->s_groups_count
;
340 __le32
*p
= (__le32
*)primary
->b_data
;
343 while ((grp
= ext4_list_backups(sb
, &three
, &five
, &seven
)) < end
) {
344 if (le32_to_cpu(*p
++) !=
345 grp
* EXT4_BLOCKS_PER_GROUP(sb
) + blk
){
346 ext4_warning(sb
, __FUNCTION__
,
348 " missing grp %d (%llu)",
351 (ext4_fsblk_t
)EXT4_BLOCKS_PER_GROUP(sb
) +
355 if (++gdbackups
> EXT4_ADDR_PER_BLOCK(sb
))
363 * Called when we need to bring a reserved group descriptor table block into
364 * use from the resize inode. The primary copy of the new GDT block currently
365 * is an indirect block (under the double indirect block in the resize inode).
366 * The new backup GDT blocks will be stored as leaf blocks in this indirect
367 * block, in group order. Even though we know all the block numbers we need,
368 * we check to ensure that the resize inode has actually reserved these blocks.
370 * Don't need to update the block bitmaps because the blocks are still in use.
372 * We get all of the error cases out of the way, so that we are sure to not
373 * fail once we start modifying the data on disk, because JBD has no rollback.
375 static int add_new_gdb(handle_t
*handle
, struct inode
*inode
,
376 struct ext4_new_group_data
*input
,
377 struct buffer_head
**primary
)
379 struct super_block
*sb
= inode
->i_sb
;
380 struct ext4_super_block
*es
= EXT4_SB(sb
)->s_es
;
381 unsigned long gdb_num
= input
->group
/ EXT4_DESC_PER_BLOCK(sb
);
382 ext4_fsblk_t gdblock
= EXT4_SB(sb
)->s_sbh
->b_blocknr
+ 1 + gdb_num
;
383 struct buffer_head
**o_group_desc
, **n_group_desc
;
384 struct buffer_head
*dind
;
386 struct ext4_iloc iloc
;
390 if (test_opt(sb
, DEBUG
))
392 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
396 * If we are not using the primary superblock/GDT copy don't resize,
397 * because the user tools have no way of handling this. Probably a
398 * bad time to do it anyways.
400 if (EXT4_SB(sb
)->s_sbh
->b_blocknr
!=
401 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
)) {
402 ext4_warning(sb
, __FUNCTION__
,
403 "won't resize using backup superblock at %llu",
404 (unsigned long long)EXT4_SB(sb
)->s_sbh
->b_blocknr
);
408 *primary
= sb_bread(sb
, gdblock
);
412 if ((gdbackups
= verify_reserved_gdb(sb
, *primary
)) < 0) {
417 data
= EXT4_I(inode
)->i_data
+ EXT4_DIND_BLOCK
;
418 dind
= sb_bread(sb
, le32_to_cpu(*data
));
424 data
= (__le32
*)dind
->b_data
;
425 if (le32_to_cpu(data
[gdb_num
% EXT4_ADDR_PER_BLOCK(sb
)]) != gdblock
) {
426 ext4_warning(sb
, __FUNCTION__
,
427 "new group %u GDT block %llu not reserved",
428 input
->group
, gdblock
);
433 if ((err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
)))
436 if ((err
= ext4_journal_get_write_access(handle
, *primary
)))
439 if ((err
= ext4_journal_get_write_access(handle
, dind
)))
442 /* ext4_reserve_inode_write() gets a reference on the iloc */
443 if ((err
= ext4_reserve_inode_write(handle
, inode
, &iloc
)))
446 n_group_desc
= kmalloc((gdb_num
+ 1) * sizeof(struct buffer_head
*),
450 ext4_warning (sb
, __FUNCTION__
,
451 "not enough memory for %lu groups", gdb_num
+ 1);
456 * Finally, we have all of the possible failures behind us...
458 * Remove new GDT block from inode double-indirect block and clear out
459 * the new GDT block for use (which also "frees" the backup GDT blocks
460 * from the reserved inode). We don't need to change the bitmaps for
461 * these blocks, because they are marked as in-use from being in the
462 * reserved inode, and will become GDT blocks (primary and backup).
464 data
[gdb_num
% EXT4_ADDR_PER_BLOCK(sb
)] = 0;
465 ext4_journal_dirty_metadata(handle
, dind
);
467 inode
->i_blocks
-= (gdbackups
+ 1) * sb
->s_blocksize
>> 9;
468 ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
469 memset((*primary
)->b_data
, 0, sb
->s_blocksize
);
470 ext4_journal_dirty_metadata(handle
, *primary
);
472 o_group_desc
= EXT4_SB(sb
)->s_group_desc
;
473 memcpy(n_group_desc
, o_group_desc
,
474 EXT4_SB(sb
)->s_gdb_count
* sizeof(struct buffer_head
*));
475 n_group_desc
[gdb_num
] = *primary
;
476 EXT4_SB(sb
)->s_group_desc
= n_group_desc
;
477 EXT4_SB(sb
)->s_gdb_count
++;
480 es
->s_reserved_gdt_blocks
=
481 cpu_to_le16(le16_to_cpu(es
->s_reserved_gdt_blocks
) - 1);
482 ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
487 //ext4_journal_release_buffer(handle, iloc.bh);
490 //ext4_journal_release_buffer(handle, dind);
492 //ext4_journal_release_buffer(handle, *primary);
494 //ext4_journal_release_buffer(handle, *primary);
500 ext4_debug("leaving with error %d\n", err
);
505 * Called when we are adding a new group which has a backup copy of each of
506 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
507 * We need to add these reserved backup GDT blocks to the resize inode, so
508 * that they are kept for future resizing and not allocated to files.
510 * Each reserved backup GDT block will go into a different indirect block.
511 * The indirect blocks are actually the primary reserved GDT blocks,
512 * so we know in advance what their block numbers are. We only get the
513 * double-indirect block to verify it is pointing to the primary reserved
514 * GDT blocks so we don't overwrite a data block by accident. The reserved
515 * backup GDT blocks are stored in their reserved primary GDT block.
517 static int reserve_backup_gdb(handle_t
*handle
, struct inode
*inode
,
518 struct ext4_new_group_data
*input
)
520 struct super_block
*sb
= inode
->i_sb
;
521 int reserved_gdb
=le16_to_cpu(EXT4_SB(sb
)->s_es
->s_reserved_gdt_blocks
);
522 struct buffer_head
**primary
;
523 struct buffer_head
*dind
;
524 struct ext4_iloc iloc
;
531 primary
= kmalloc(reserved_gdb
* sizeof(*primary
), GFP_KERNEL
);
535 data
= EXT4_I(inode
)->i_data
+ EXT4_DIND_BLOCK
;
536 dind
= sb_bread(sb
, le32_to_cpu(*data
));
542 blk
= EXT4_SB(sb
)->s_sbh
->b_blocknr
+ 1 + EXT4_SB(sb
)->s_gdb_count
;
543 data
= (__le32
*)dind
->b_data
+ EXT4_SB(sb
)->s_gdb_count
;
544 end
= (__le32
*)dind
->b_data
+ EXT4_ADDR_PER_BLOCK(sb
);
546 /* Get each reserved primary GDT block and verify it holds backups */
547 for (res
= 0; res
< reserved_gdb
; res
++, blk
++) {
548 if (le32_to_cpu(*data
) != blk
) {
549 ext4_warning(sb
, __FUNCTION__
,
550 "reserved block %llu"
551 " not at offset %ld",
553 (long)(data
- (__le32
*)dind
->b_data
));
557 primary
[res
] = sb_bread(sb
, blk
);
562 if ((gdbackups
= verify_reserved_gdb(sb
, primary
[res
])) < 0) {
563 brelse(primary
[res
]);
568 data
= (__le32
*)dind
->b_data
;
571 for (i
= 0; i
< reserved_gdb
; i
++) {
572 if ((err
= ext4_journal_get_write_access(handle
, primary
[i
]))) {
575 for (j = 0; j < i; j++)
576 ext4_journal_release_buffer(handle, primary[j]);
582 if ((err
= ext4_reserve_inode_write(handle
, inode
, &iloc
)))
586 * Finally we can add each of the reserved backup GDT blocks from
587 * the new group to its reserved primary GDT block.
589 blk
= input
->group
* EXT4_BLOCKS_PER_GROUP(sb
);
590 for (i
= 0; i
< reserved_gdb
; i
++) {
592 data
= (__le32
*)primary
[i
]->b_data
;
593 /* printk("reserving backup %lu[%u] = %lu\n",
594 primary[i]->b_blocknr, gdbackups,
595 blk + primary[i]->b_blocknr); */
596 data
[gdbackups
] = cpu_to_le32(blk
+ primary
[i
]->b_blocknr
);
597 err2
= ext4_journal_dirty_metadata(handle
, primary
[i
]);
601 inode
->i_blocks
+= reserved_gdb
* sb
->s_blocksize
>> 9;
602 ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
606 brelse(primary
[res
]);
616 * Update the backup copies of the ext4 metadata. These don't need to be part
617 * of the main resize transaction, because e2fsck will re-write them if there
618 * is a problem (basically only OOM will cause a problem). However, we
619 * _should_ update the backups if possible, in case the primary gets trashed
620 * for some reason and we need to run e2fsck from a backup superblock. The
621 * important part is that the new block and inode counts are in the backup
622 * superblocks, and the location of the new group metadata in the GDT backups.
624 * We do not need lock_super() for this, because these blocks are not
625 * otherwise touched by the filesystem code when it is mounted. We don't
626 * need to worry about last changing from sbi->s_groups_count, because the
627 * worst that can happen is that we do not copy the full number of backups
628 * at this time. The resize which changed s_groups_count will backup again.
630 static void update_backups(struct super_block
*sb
,
631 int blk_off
, char *data
, int size
)
633 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
634 const unsigned long last
= sbi
->s_groups_count
;
635 const int bpg
= EXT4_BLOCKS_PER_GROUP(sb
);
640 int rest
= sb
->s_blocksize
- size
;
644 handle
= ext4_journal_start_sb(sb
, EXT4_MAX_TRANS_DATA
);
645 if (IS_ERR(handle
)) {
647 err
= PTR_ERR(handle
);
651 while ((group
= ext4_list_backups(sb
, &three
, &five
, &seven
)) < last
) {
652 struct buffer_head
*bh
;
654 /* Out of journal space, and can't get more - abort - so sad */
655 if (handle
->h_buffer_credits
== 0 &&
656 ext4_journal_extend(handle
, EXT4_MAX_TRANS_DATA
) &&
657 (err
= ext4_journal_restart(handle
, EXT4_MAX_TRANS_DATA
)))
660 bh
= sb_getblk(sb
, group
* bpg
+ blk_off
);
665 ext4_debug("update metadata backup %#04lx\n",
666 (unsigned long)bh
->b_blocknr
);
667 if ((err
= ext4_journal_get_write_access(handle
, bh
)))
670 memcpy(bh
->b_data
, data
, size
);
672 memset(bh
->b_data
+ size
, 0, rest
);
673 set_buffer_uptodate(bh
);
675 ext4_journal_dirty_metadata(handle
, bh
);
678 if ((err2
= ext4_journal_stop(handle
)) && !err
)
682 * Ugh! Need to have e2fsck write the backup copies. It is too
683 * late to revert the resize, we shouldn't fail just because of
684 * the backup copies (they are only needed in case of corruption).
686 * However, if we got here we have a journal problem too, so we
687 * can't really start a transaction to mark the superblock.
688 * Chicken out and just set the flag on the hope it will be written
689 * to disk, and if not - we will simply wait until next fsck.
693 ext4_warning(sb
, __FUNCTION__
,
694 "can't update backup for group %d (err %d), "
695 "forcing fsck on next reboot", group
, err
);
696 sbi
->s_mount_state
&= ~EXT4_VALID_FS
;
697 sbi
->s_es
->s_state
&= cpu_to_le16(~EXT4_VALID_FS
);
698 mark_buffer_dirty(sbi
->s_sbh
);
702 /* Add group descriptor data to an existing or new group descriptor block.
703 * Ensure we handle all possible error conditions _before_ we start modifying
704 * the filesystem, because we cannot abort the transaction and not have it
705 * write the data to disk.
707 * If we are on a GDT block boundary, we need to get the reserved GDT block.
708 * Otherwise, we may need to add backup GDT blocks for a sparse group.
710 * We only need to hold the superblock lock while we are actually adding
711 * in the new group's counts to the superblock. Prior to that we have
712 * not really "added" the group at all. We re-check that we are still
713 * adding in the last group in case things have changed since verifying.
715 int ext4_group_add(struct super_block
*sb
, struct ext4_new_group_data
*input
)
717 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
718 struct ext4_super_block
*es
= sbi
->s_es
;
719 int reserved_gdb
= ext4_bg_has_super(sb
, input
->group
) ?
720 le16_to_cpu(es
->s_reserved_gdt_blocks
) : 0;
721 struct buffer_head
*primary
= NULL
;
722 struct ext4_group_desc
*gdp
;
723 struct inode
*inode
= NULL
;
725 int gdb_off
, gdb_num
;
728 gdb_num
= input
->group
/ EXT4_DESC_PER_BLOCK(sb
);
729 gdb_off
= input
->group
% EXT4_DESC_PER_BLOCK(sb
);
731 if (gdb_off
== 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb
,
732 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
733 ext4_warning(sb
, __FUNCTION__
,
734 "Can't resize non-sparse filesystem further");
738 if (ext4_blocks_count(es
) + input
->blocks_count
<
739 ext4_blocks_count(es
)) {
740 ext4_warning(sb
, __FUNCTION__
, "blocks_count overflow\n");
744 if (le32_to_cpu(es
->s_inodes_count
) + EXT4_INODES_PER_GROUP(sb
) <
745 le32_to_cpu(es
->s_inodes_count
)) {
746 ext4_warning(sb
, __FUNCTION__
, "inodes_count overflow\n");
750 if (reserved_gdb
|| gdb_off
== 0) {
751 if (!EXT4_HAS_COMPAT_FEATURE(sb
,
752 EXT4_FEATURE_COMPAT_RESIZE_INODE
)){
753 ext4_warning(sb
, __FUNCTION__
,
754 "No reserved GDT blocks, can't resize");
757 inode
= iget(sb
, EXT4_RESIZE_INO
);
758 if (!inode
|| is_bad_inode(inode
)) {
759 ext4_warning(sb
, __FUNCTION__
,
760 "Error opening resize inode");
766 if ((err
= verify_group_input(sb
, input
)))
769 if ((err
= setup_new_group_blocks(sb
, input
)))
773 * We will always be modifying at least the superblock and a GDT
774 * block. If we are adding a group past the last current GDT block,
775 * we will also modify the inode and the dindirect block. If we
776 * are adding a group with superblock/GDT backups we will also
777 * modify each of the reserved GDT dindirect blocks.
779 handle
= ext4_journal_start_sb(sb
,
780 ext4_bg_has_super(sb
, input
->group
) ?
781 3 + reserved_gdb
: 4);
782 if (IS_ERR(handle
)) {
783 err
= PTR_ERR(handle
);
788 if (input
->group
!= sbi
->s_groups_count
) {
789 ext4_warning(sb
, __FUNCTION__
,
790 "multiple resizers run on filesystem!");
795 if ((err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
)))
799 * We will only either add reserved group blocks to a backup group
800 * or remove reserved blocks for the first group in a new group block.
801 * Doing both would be mean more complex code, and sane people don't
802 * use non-sparse filesystems anymore. This is already checked above.
805 primary
= sbi
->s_group_desc
[gdb_num
];
806 if ((err
= ext4_journal_get_write_access(handle
, primary
)))
809 if (reserved_gdb
&& ext4_bg_num_gdb(sb
, input
->group
) &&
810 (err
= reserve_backup_gdb(handle
, inode
, input
)))
812 } else if ((err
= add_new_gdb(handle
, inode
, input
, &primary
)))
816 * OK, now we've set up the new group. Time to make it active.
818 * Current kernels don't lock all allocations via lock_super(),
819 * so we have to be safe wrt. concurrent accesses the group
820 * data. So we need to be careful to set all of the relevant
821 * group descriptor data etc. *before* we enable the group.
823 * The key field here is sbi->s_groups_count: as long as
824 * that retains its old value, nobody is going to access the new
827 * So first we update all the descriptor metadata for the new
828 * group; then we update the total disk blocks count; then we
829 * update the groups count to enable the group; then finally we
830 * update the free space counts so that the system can start
831 * using the new disk blocks.
834 /* Update group descriptor block for new group */
835 gdp
= (struct ext4_group_desc
*)primary
->b_data
+ gdb_off
;
837 ext4_block_bitmap_set(gdp
, input
->block_bitmap
); /* LV FIXME */
838 ext4_inode_bitmap_set(gdp
, input
->inode_bitmap
); /* LV FIXME */
839 ext4_inode_table_set(gdp
, input
->inode_table
); /* LV FIXME */
840 gdp
->bg_free_blocks_count
= cpu_to_le16(input
->free_blocks_count
);
841 gdp
->bg_free_inodes_count
= cpu_to_le16(EXT4_INODES_PER_GROUP(sb
));
844 * Make the new blocks and inodes valid next. We do this before
845 * increasing the group count so that once the group is enabled,
846 * all of its blocks and inodes are already valid.
848 * We always allocate group-by-group, then block-by-block or
849 * inode-by-inode within a group, so enabling these
850 * blocks/inodes before the group is live won't actually let us
851 * allocate the new space yet.
853 ext4_blocks_count_set(es
, ext4_blocks_count(es
) +
854 input
->blocks_count
);
855 es
->s_inodes_count
= cpu_to_le32(le32_to_cpu(es
->s_inodes_count
) +
856 EXT4_INODES_PER_GROUP(sb
));
859 * We need to protect s_groups_count against other CPUs seeing
860 * inconsistent state in the superblock.
862 * The precise rules we use are:
864 * * Writers of s_groups_count *must* hold lock_super
866 * * Writers must perform a smp_wmb() after updating all dependent
867 * data and before modifying the groups count
869 * * Readers must hold lock_super() over the access
871 * * Readers must perform an smp_rmb() after reading the groups count
872 * and before reading any dependent data.
874 * NB. These rules can be relaxed when checking the group count
875 * while freeing data, as we can only allocate from a block
876 * group after serialising against the group count, and we can
877 * only then free after serialising in turn against that
882 /* Update the global fs size fields */
883 sbi
->s_groups_count
++;
885 ext4_journal_dirty_metadata(handle
, primary
);
887 /* Update the reserved block counts only once the new group is
889 ext4_r_blocks_count_set(es
, ext4_r_blocks_count(es
) +
890 input
->reserved_blocks
);
892 /* Update the free space counts */
893 percpu_counter_mod(&sbi
->s_freeblocks_counter
,
894 input
->free_blocks_count
);
895 percpu_counter_mod(&sbi
->s_freeinodes_counter
,
896 EXT4_INODES_PER_GROUP(sb
));
898 ext4_journal_dirty_metadata(handle
, sbi
->s_sbh
);
903 if ((err2
= ext4_journal_stop(handle
)) && !err
)
906 update_backups(sb
, sbi
->s_sbh
->b_blocknr
, (char *)es
,
907 sizeof(struct ext4_super_block
));
908 update_backups(sb
, primary
->b_blocknr
, primary
->b_data
,
914 } /* ext4_group_add */
916 /* Extend the filesystem to the new number of blocks specified. This entry
917 * point is only used to extend the current filesystem to the end of the last
918 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
919 * for emergencies (because it has no dependencies on reserved blocks).
921 * If we _really_ wanted, we could use default values to call ext4_group_add()
922 * allow the "remount" trick to work for arbitrary resizing, assuming enough
923 * GDT blocks are reserved to grow to the desired size.
925 int ext4_group_extend(struct super_block
*sb
, struct ext4_super_block
*es
,
926 ext4_fsblk_t n_blocks_count
)
928 ext4_fsblk_t o_blocks_count
;
929 unsigned long o_groups_count
;
932 struct buffer_head
* bh
;
935 unsigned long freed_blocks
;
937 /* We don't need to worry about locking wrt other resizers just
938 * yet: we're going to revalidate es->s_blocks_count after
939 * taking lock_super() below. */
940 o_blocks_count
= ext4_blocks_count(es
);
941 o_groups_count
= EXT4_SB(sb
)->s_groups_count
;
943 if (test_opt(sb
, DEBUG
))
944 printk(KERN_DEBUG
"EXT4-fs: extending last group from %llu uto %llu blocks\n",
945 o_blocks_count
, n_blocks_count
);
947 if (n_blocks_count
== 0 || n_blocks_count
== o_blocks_count
)
950 if (n_blocks_count
> (sector_t
)(~0ULL) >> (sb
->s_blocksize_bits
- 9)) {
951 printk(KERN_ERR
"EXT4-fs: filesystem on %s:"
952 " too large to resize to %llu blocks safely\n",
953 sb
->s_id
, n_blocks_count
);
954 if (sizeof(sector_t
) < 8)
955 ext4_warning(sb
, __FUNCTION__
,
956 "CONFIG_LBD not enabled\n");
960 if (n_blocks_count
< o_blocks_count
) {
961 ext4_warning(sb
, __FUNCTION__
,
962 "can't shrink FS - resize aborted");
966 /* Handle the remaining blocks in the last group only. */
967 ext4_get_group_no_and_offset(sb
, o_blocks_count
, NULL
, &last
);
970 ext4_warning(sb
, __FUNCTION__
,
971 "need to use ext2online to resize further");
975 add
= EXT4_BLOCKS_PER_GROUP(sb
) - last
;
977 if (o_blocks_count
+ add
< o_blocks_count
) {
978 ext4_warning(sb
, __FUNCTION__
, "blocks_count overflow");
982 if (o_blocks_count
+ add
> n_blocks_count
)
983 add
= n_blocks_count
- o_blocks_count
;
985 if (o_blocks_count
+ add
< n_blocks_count
)
986 ext4_warning(sb
, __FUNCTION__
,
987 "will only finish group (%llu"
989 o_blocks_count
+ add
, add
);
991 /* See if the device is actually as big as what was requested */
992 bh
= sb_bread(sb
, o_blocks_count
+ add
-1);
994 ext4_warning(sb
, __FUNCTION__
,
995 "can't read last block, resize aborted");
1000 /* We will update the superblock, one block bitmap, and
1001 * one group descriptor via ext4_free_blocks().
1003 handle
= ext4_journal_start_sb(sb
, 3);
1004 if (IS_ERR(handle
)) {
1005 err
= PTR_ERR(handle
);
1006 ext4_warning(sb
, __FUNCTION__
, "error %d on journal start",err
);
1011 if (o_blocks_count
!= ext4_blocks_count(es
)) {
1012 ext4_warning(sb
, __FUNCTION__
,
1013 "multiple resizers run on filesystem!");
1019 if ((err
= ext4_journal_get_write_access(handle
,
1020 EXT4_SB(sb
)->s_sbh
))) {
1021 ext4_warning(sb
, __FUNCTION__
,
1022 "error %d on journal write access", err
);
1024 ext4_journal_stop(handle
);
1027 ext4_blocks_count_set(es
, o_blocks_count
+ add
);
1028 ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
1031 ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count
,
1032 o_blocks_count
+ add
);
1033 ext4_free_blocks_sb(handle
, sb
, o_blocks_count
, add
, &freed_blocks
);
1034 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count
,
1035 o_blocks_count
+ add
);
1036 if ((err
= ext4_journal_stop(handle
)))
1038 if (test_opt(sb
, DEBUG
))
1039 printk(KERN_DEBUG
"EXT4-fs: extended group to %llu blocks\n",
1040 ext4_blocks_count(es
));
1041 update_backups(sb
, EXT4_SB(sb
)->s_sbh
->b_blocknr
, (char *)es
,
1042 sizeof(struct ext4_super_block
));
1045 } /* ext4_group_extend */