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/smp_lock.h>
15 #include <linux/ext4_jbd2.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
21 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
22 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
24 static int verify_group_input(struct super_block
*sb
,
25 struct ext4_new_group_data
*input
)
27 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
28 struct ext4_super_block
*es
= sbi
->s_es
;
29 ext4_fsblk_t start
= ext4_blocks_count(es
);
30 ext4_fsblk_t end
= start
+ input
->blocks_count
;
31 unsigned group
= input
->group
;
32 ext4_fsblk_t itend
= input
->inode_table
+ sbi
->s_itb_per_group
;
33 unsigned overhead
= ext4_bg_has_super(sb
, group
) ?
34 (1 + ext4_bg_num_gdb(sb
, group
) +
35 le16_to_cpu(es
->s_reserved_gdt_blocks
)) : 0;
36 ext4_fsblk_t metaend
= start
+ overhead
;
37 struct buffer_head
*bh
= NULL
;
38 ext4_grpblk_t free_blocks_count
, offset
;
41 input
->free_blocks_count
= free_blocks_count
=
42 input
->blocks_count
- 2 - overhead
- sbi
->s_itb_per_group
;
44 if (test_opt(sb
, DEBUG
))
45 printk(KERN_DEBUG
"EXT4-fs: adding %s group %u: %u blocks "
46 "(%d free, %u reserved)\n",
47 ext4_bg_has_super(sb
, input
->group
) ? "normal" :
48 "no-super", input
->group
, input
->blocks_count
,
49 free_blocks_count
, input
->reserved_blocks
);
51 ext4_get_group_no_and_offset(sb
, start
, NULL
, &offset
);
52 if (group
!= sbi
->s_groups_count
)
53 ext4_warning(sb
, __FUNCTION__
,
54 "Cannot add at group %u (only %lu groups)",
55 input
->group
, sbi
->s_groups_count
);
57 ext4_warning(sb
, __FUNCTION__
, "Last group not full");
58 else if (input
->reserved_blocks
> input
->blocks_count
/ 5)
59 ext4_warning(sb
, __FUNCTION__
, "Reserved blocks too high (%u)",
60 input
->reserved_blocks
);
61 else if (free_blocks_count
< 0)
62 ext4_warning(sb
, __FUNCTION__
, "Bad blocks count %u",
64 else if (!(bh
= sb_bread(sb
, end
- 1)))
65 ext4_warning(sb
, __FUNCTION__
,
66 "Cannot read last block (%llu)",
68 else if (outside(input
->block_bitmap
, start
, end
))
69 ext4_warning(sb
, __FUNCTION__
,
70 "Block bitmap not in group (block %llu)",
71 (unsigned long long)input
->block_bitmap
);
72 else if (outside(input
->inode_bitmap
, start
, end
))
73 ext4_warning(sb
, __FUNCTION__
,
74 "Inode bitmap not in group (block %llu)",
75 (unsigned long long)input
->inode_bitmap
);
76 else if (outside(input
->inode_table
, start
, end
) ||
77 outside(itend
- 1, start
, end
))
78 ext4_warning(sb
, __FUNCTION__
,
79 "Inode table not in group (blocks %llu-%llu)",
80 (unsigned long long)input
->inode_table
, itend
- 1);
81 else if (input
->inode_bitmap
== input
->block_bitmap
)
82 ext4_warning(sb
, __FUNCTION__
,
83 "Block bitmap same as inode bitmap (%llu)",
84 (unsigned long long)input
->block_bitmap
);
85 else if (inside(input
->block_bitmap
, input
->inode_table
, itend
))
86 ext4_warning(sb
, __FUNCTION__
,
87 "Block bitmap (%llu) in inode table (%llu-%llu)",
88 (unsigned long long)input
->block_bitmap
,
89 (unsigned long long)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 (unsigned long long)input
->inode_bitmap
,
94 (unsigned long long)input
->inode_table
, itend
- 1);
95 else if (inside(input
->block_bitmap
, start
, metaend
))
96 ext4_warning(sb
, __FUNCTION__
,
97 "Block bitmap (%llu) in GDT table"
99 (unsigned long long)input
->block_bitmap
,
101 else if (inside(input
->inode_bitmap
, start
, metaend
))
102 ext4_warning(sb
, __FUNCTION__
,
103 "Inode bitmap (%llu) in GDT table"
105 (unsigned long long)input
->inode_bitmap
,
107 else if (inside(input
->inode_table
, start
, metaend
) ||
108 inside(itend
- 1, start
, metaend
))
109 ext4_warning(sb
, __FUNCTION__
,
110 "Inode table (%llu-%llu) overlaps"
111 "GDT table (%llu-%llu)",
112 (unsigned long long)input
->inode_table
,
113 itend
- 1, start
, metaend
- 1);
121 static struct buffer_head
*bclean(handle_t
*handle
, struct super_block
*sb
,
124 struct buffer_head
*bh
;
127 bh
= sb_getblk(sb
, blk
);
129 return ERR_PTR(-EIO
);
130 if ((err
= ext4_journal_get_write_access(handle
, bh
))) {
135 memset(bh
->b_data
, 0, sb
->s_blocksize
);
136 set_buffer_uptodate(bh
);
144 * To avoid calling the atomic setbit hundreds or thousands of times, we only
145 * need to use it within a single byte (to ensure we get endianness right).
146 * We can use memset for the rest of the bitmap as there are no other users.
148 static void mark_bitmap_end(int start_bit
, int end_bit
, char *bitmap
)
152 if (start_bit
>= end_bit
)
155 ext4_debug("mark end bits +%d through +%d used\n", start_bit
, end_bit
);
156 for (i
= start_bit
; i
< ((start_bit
+ 7) & ~7UL); i
++)
157 ext4_set_bit(i
, bitmap
);
159 memset(bitmap
+ (i
>> 3), 0xff, (end_bit
- i
) >> 3);
163 * Set up the block and inode bitmaps, and the inode table for the new group.
164 * This doesn't need to be part of the main transaction, since we are only
165 * changing blocks outside the actual filesystem. We still do journaling to
166 * ensure the recovery is correct in case of a failure just after resize.
167 * If any part of this fails, we simply abort the resize.
169 static int setup_new_group_blocks(struct super_block
*sb
,
170 struct ext4_new_group_data
*input
)
172 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
173 ext4_fsblk_t start
= ext4_group_first_block_no(sb
, input
->group
);
174 int reserved_gdb
= ext4_bg_has_super(sb
, input
->group
) ?
175 le16_to_cpu(sbi
->s_es
->s_reserved_gdt_blocks
) : 0;
176 unsigned long gdblocks
= ext4_bg_num_gdb(sb
, input
->group
);
177 struct buffer_head
*bh
;
184 handle
= ext4_journal_start_sb(sb
, reserved_gdb
+ gdblocks
+
185 2 + sbi
->s_itb_per_group
);
187 return PTR_ERR(handle
);
190 if (input
->group
!= sbi
->s_groups_count
) {
195 if (IS_ERR(bh
= bclean(handle
, sb
, input
->block_bitmap
))) {
200 if (ext4_bg_has_super(sb
, input
->group
)) {
201 ext4_debug("mark backup superblock %#04lx (+0)\n", start
);
202 ext4_set_bit(0, bh
->b_data
);
205 /* Copy all of the GDT blocks into the backup in this group */
206 for (i
= 0, bit
= 1, block
= start
+ 1;
207 i
< gdblocks
; i
++, block
++, bit
++) {
208 struct buffer_head
*gdb
;
210 ext4_debug("update backup group %#04lx (+%d)\n", block
, bit
);
212 gdb
= sb_getblk(sb
, block
);
217 if ((err
= ext4_journal_get_write_access(handle
, gdb
))) {
222 memcpy(gdb
->b_data
, sbi
->s_group_desc
[i
]->b_data
, bh
->b_size
);
223 set_buffer_uptodate(gdb
);
225 ext4_journal_dirty_metadata(handle
, gdb
);
226 ext4_set_bit(bit
, bh
->b_data
);
230 /* Zero out all of the reserved backup group descriptor table blocks */
231 for (i
= 0, bit
= gdblocks
+ 1, block
= start
+ bit
;
232 i
< reserved_gdb
; i
++, block
++, bit
++) {
233 struct buffer_head
*gdb
;
235 ext4_debug("clear reserved block %#04lx (+%d)\n", block
, bit
);
237 if (IS_ERR(gdb
= bclean(handle
, sb
, block
))) {
241 ext4_journal_dirty_metadata(handle
, gdb
);
242 ext4_set_bit(bit
, bh
->b_data
);
245 ext4_debug("mark block bitmap %#04x (+%ld)\n", input
->block_bitmap
,
246 input
->block_bitmap
- start
);
247 ext4_set_bit(input
->block_bitmap
- start
, bh
->b_data
);
248 ext4_debug("mark inode bitmap %#04x (+%ld)\n", input
->inode_bitmap
,
249 input
->inode_bitmap
- start
);
250 ext4_set_bit(input
->inode_bitmap
- start
, bh
->b_data
);
252 /* Zero out all of the inode table blocks */
253 for (i
= 0, block
= input
->inode_table
, bit
= block
- start
;
254 i
< sbi
->s_itb_per_group
; i
++, bit
++, block
++) {
255 struct buffer_head
*it
;
257 ext4_debug("clear inode block %#04lx (+%d)\n", block
, bit
);
258 if (IS_ERR(it
= bclean(handle
, sb
, block
))) {
262 ext4_journal_dirty_metadata(handle
, it
);
264 ext4_set_bit(bit
, bh
->b_data
);
266 mark_bitmap_end(input
->blocks_count
, EXT4_BLOCKS_PER_GROUP(sb
),
268 ext4_journal_dirty_metadata(handle
, bh
);
271 /* Mark unused entries in inode bitmap used */
272 ext4_debug("clear inode bitmap %#04x (+%ld)\n",
273 input
->inode_bitmap
, input
->inode_bitmap
- start
);
274 if (IS_ERR(bh
= bclean(handle
, sb
, input
->inode_bitmap
))) {
279 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb
), EXT4_BLOCKS_PER_GROUP(sb
),
281 ext4_journal_dirty_metadata(handle
, bh
);
287 if ((err2
= ext4_journal_stop(handle
)) && !err
)
295 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
296 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
297 * calling this for the first time. In a sparse filesystem it will be the
298 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
299 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
301 static unsigned ext4_list_backups(struct super_block
*sb
, unsigned *three
,
302 unsigned *five
, unsigned *seven
)
304 unsigned *min
= three
;
308 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb
,
309 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
331 * Check that all of the backup GDT blocks are held in the primary GDT block.
332 * It is assumed that they are stored in group order. Returns the number of
333 * groups in current filesystem that have BACKUPS, or -ve error code.
335 static int verify_reserved_gdb(struct super_block
*sb
,
336 struct buffer_head
*primary
)
338 const ext4_fsblk_t blk
= primary
->b_blocknr
;
339 const unsigned long end
= EXT4_SB(sb
)->s_groups_count
;
344 __le32
*p
= (__le32
*)primary
->b_data
;
347 while ((grp
= ext4_list_backups(sb
, &three
, &five
, &seven
)) < end
) {
348 if (le32_to_cpu(*p
++) !=
349 grp
* EXT4_BLOCKS_PER_GROUP(sb
) + blk
){
350 ext4_warning(sb
, __FUNCTION__
,
352 " missing grp %d (%llu)",
355 (ext4_fsblk_t
)EXT4_BLOCKS_PER_GROUP(sb
) +
359 if (++gdbackups
> EXT4_ADDR_PER_BLOCK(sb
))
367 * Called when we need to bring a reserved group descriptor table block into
368 * use from the resize inode. The primary copy of the new GDT block currently
369 * is an indirect block (under the double indirect block in the resize inode).
370 * The new backup GDT blocks will be stored as leaf blocks in this indirect
371 * block, in group order. Even though we know all the block numbers we need,
372 * we check to ensure that the resize inode has actually reserved these blocks.
374 * Don't need to update the block bitmaps because the blocks are still in use.
376 * We get all of the error cases out of the way, so that we are sure to not
377 * fail once we start modifying the data on disk, because JBD has no rollback.
379 static int add_new_gdb(handle_t
*handle
, struct inode
*inode
,
380 struct ext4_new_group_data
*input
,
381 struct buffer_head
**primary
)
383 struct super_block
*sb
= inode
->i_sb
;
384 struct ext4_super_block
*es
= EXT4_SB(sb
)->s_es
;
385 unsigned long gdb_num
= input
->group
/ EXT4_DESC_PER_BLOCK(sb
);
386 ext4_fsblk_t gdblock
= EXT4_SB(sb
)->s_sbh
->b_blocknr
+ 1 + gdb_num
;
387 struct buffer_head
**o_group_desc
, **n_group_desc
;
388 struct buffer_head
*dind
;
390 struct ext4_iloc iloc
;
394 if (test_opt(sb
, DEBUG
))
396 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
400 * If we are not using the primary superblock/GDT copy don't resize,
401 * because the user tools have no way of handling this. Probably a
402 * bad time to do it anyways.
404 if (EXT4_SB(sb
)->s_sbh
->b_blocknr
!=
405 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
)) {
406 ext4_warning(sb
, __FUNCTION__
,
407 "won't resize using backup superblock at %llu",
408 (unsigned long long)EXT4_SB(sb
)->s_sbh
->b_blocknr
);
412 *primary
= sb_bread(sb
, gdblock
);
416 if ((gdbackups
= verify_reserved_gdb(sb
, *primary
)) < 0) {
421 data
= EXT4_I(inode
)->i_data
+ EXT4_DIND_BLOCK
;
422 dind
= sb_bread(sb
, le32_to_cpu(*data
));
428 data
= (__le32
*)dind
->b_data
;
429 if (le32_to_cpu(data
[gdb_num
% EXT4_ADDR_PER_BLOCK(sb
)]) != gdblock
) {
430 ext4_warning(sb
, __FUNCTION__
,
431 "new group %u GDT block %llu not reserved",
432 input
->group
, gdblock
);
437 if ((err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
)))
440 if ((err
= ext4_journal_get_write_access(handle
, *primary
)))
443 if ((err
= ext4_journal_get_write_access(handle
, dind
)))
446 /* ext4_reserve_inode_write() gets a reference on the iloc */
447 if ((err
= ext4_reserve_inode_write(handle
, inode
, &iloc
)))
450 n_group_desc
= kmalloc((gdb_num
+ 1) * sizeof(struct buffer_head
*),
454 ext4_warning (sb
, __FUNCTION__
,
455 "not enough memory for %lu groups", gdb_num
+ 1);
460 * Finally, we have all of the possible failures behind us...
462 * Remove new GDT block from inode double-indirect block and clear out
463 * the new GDT block for use (which also "frees" the backup GDT blocks
464 * from the reserved inode). We don't need to change the bitmaps for
465 * these blocks, because they are marked as in-use from being in the
466 * reserved inode, and will become GDT blocks (primary and backup).
468 data
[gdb_num
% EXT4_ADDR_PER_BLOCK(sb
)] = 0;
469 ext4_journal_dirty_metadata(handle
, dind
);
471 inode
->i_blocks
-= (gdbackups
+ 1) * sb
->s_blocksize
>> 9;
472 ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
473 memset((*primary
)->b_data
, 0, sb
->s_blocksize
);
474 ext4_journal_dirty_metadata(handle
, *primary
);
476 o_group_desc
= EXT4_SB(sb
)->s_group_desc
;
477 memcpy(n_group_desc
, o_group_desc
,
478 EXT4_SB(sb
)->s_gdb_count
* sizeof(struct buffer_head
*));
479 n_group_desc
[gdb_num
] = *primary
;
480 EXT4_SB(sb
)->s_group_desc
= n_group_desc
;
481 EXT4_SB(sb
)->s_gdb_count
++;
484 es
->s_reserved_gdt_blocks
=
485 cpu_to_le16(le16_to_cpu(es
->s_reserved_gdt_blocks
) - 1);
486 ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
491 //ext4_journal_release_buffer(handle, iloc.bh);
494 //ext4_journal_release_buffer(handle, dind);
496 //ext4_journal_release_buffer(handle, *primary);
498 //ext4_journal_release_buffer(handle, *primary);
504 ext4_debug("leaving with error %d\n", err
);
509 * Called when we are adding a new group which has a backup copy of each of
510 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
511 * We need to add these reserved backup GDT blocks to the resize inode, so
512 * that they are kept for future resizing and not allocated to files.
514 * Each reserved backup GDT block will go into a different indirect block.
515 * The indirect blocks are actually the primary reserved GDT blocks,
516 * so we know in advance what their block numbers are. We only get the
517 * double-indirect block to verify it is pointing to the primary reserved
518 * GDT blocks so we don't overwrite a data block by accident. The reserved
519 * backup GDT blocks are stored in their reserved primary GDT block.
521 static int reserve_backup_gdb(handle_t
*handle
, struct inode
*inode
,
522 struct ext4_new_group_data
*input
)
524 struct super_block
*sb
= inode
->i_sb
;
525 int reserved_gdb
=le16_to_cpu(EXT4_SB(sb
)->s_es
->s_reserved_gdt_blocks
);
526 struct buffer_head
**primary
;
527 struct buffer_head
*dind
;
528 struct ext4_iloc iloc
;
535 primary
= kmalloc(reserved_gdb
* sizeof(*primary
), GFP_KERNEL
);
539 data
= EXT4_I(inode
)->i_data
+ EXT4_DIND_BLOCK
;
540 dind
= sb_bread(sb
, le32_to_cpu(*data
));
546 blk
= EXT4_SB(sb
)->s_sbh
->b_blocknr
+ 1 + EXT4_SB(sb
)->s_gdb_count
;
547 data
= (__le32
*)dind
->b_data
+ EXT4_SB(sb
)->s_gdb_count
;
548 end
= (__le32
*)dind
->b_data
+ EXT4_ADDR_PER_BLOCK(sb
);
550 /* Get each reserved primary GDT block and verify it holds backups */
551 for (res
= 0; res
< reserved_gdb
; res
++, blk
++) {
552 if (le32_to_cpu(*data
) != blk
) {
553 ext4_warning(sb
, __FUNCTION__
,
554 "reserved block %llu"
555 " not at offset %ld",
557 (long)(data
- (__le32
*)dind
->b_data
));
561 primary
[res
] = sb_bread(sb
, blk
);
566 if ((gdbackups
= verify_reserved_gdb(sb
, primary
[res
])) < 0) {
567 brelse(primary
[res
]);
572 data
= (__le32
*)dind
->b_data
;
575 for (i
= 0; i
< reserved_gdb
; i
++) {
576 if ((err
= ext4_journal_get_write_access(handle
, primary
[i
]))) {
579 for (j = 0; j < i; j++)
580 ext4_journal_release_buffer(handle, primary[j]);
586 if ((err
= ext4_reserve_inode_write(handle
, inode
, &iloc
)))
590 * Finally we can add each of the reserved backup GDT blocks from
591 * the new group to its reserved primary GDT block.
593 blk
= input
->group
* EXT4_BLOCKS_PER_GROUP(sb
);
594 for (i
= 0; i
< reserved_gdb
; i
++) {
596 data
= (__le32
*)primary
[i
]->b_data
;
597 /* printk("reserving backup %lu[%u] = %lu\n",
598 primary[i]->b_blocknr, gdbackups,
599 blk + primary[i]->b_blocknr); */
600 data
[gdbackups
] = cpu_to_le32(blk
+ primary
[i
]->b_blocknr
);
601 err2
= ext4_journal_dirty_metadata(handle
, primary
[i
]);
605 inode
->i_blocks
+= reserved_gdb
* sb
->s_blocksize
>> 9;
606 ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
610 brelse(primary
[res
]);
620 * Update the backup copies of the ext4 metadata. These don't need to be part
621 * of the main resize transaction, because e2fsck will re-write them if there
622 * is a problem (basically only OOM will cause a problem). However, we
623 * _should_ update the backups if possible, in case the primary gets trashed
624 * for some reason and we need to run e2fsck from a backup superblock. The
625 * important part is that the new block and inode counts are in the backup
626 * superblocks, and the location of the new group metadata in the GDT backups.
628 * We do not need lock_super() for this, because these blocks are not
629 * otherwise touched by the filesystem code when it is mounted. We don't
630 * need to worry about last changing from sbi->s_groups_count, because the
631 * worst that can happen is that we do not copy the full number of backups
632 * at this time. The resize which changed s_groups_count will backup again.
634 static void update_backups(struct super_block
*sb
,
635 int blk_off
, char *data
, int size
)
637 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
638 const unsigned long last
= sbi
->s_groups_count
;
639 const int bpg
= EXT4_BLOCKS_PER_GROUP(sb
);
644 int rest
= sb
->s_blocksize
- size
;
648 handle
= ext4_journal_start_sb(sb
, EXT4_MAX_TRANS_DATA
);
649 if (IS_ERR(handle
)) {
651 err
= PTR_ERR(handle
);
655 while ((group
= ext4_list_backups(sb
, &three
, &five
, &seven
)) < last
) {
656 struct buffer_head
*bh
;
658 /* Out of journal space, and can't get more - abort - so sad */
659 if (handle
->h_buffer_credits
== 0 &&
660 ext4_journal_extend(handle
, EXT4_MAX_TRANS_DATA
) &&
661 (err
= ext4_journal_restart(handle
, EXT4_MAX_TRANS_DATA
)))
664 bh
= sb_getblk(sb
, group
* bpg
+ blk_off
);
669 ext4_debug("update metadata backup %#04lx\n",
670 (unsigned long)bh
->b_blocknr
);
671 if ((err
= ext4_journal_get_write_access(handle
, bh
)))
674 memcpy(bh
->b_data
, data
, size
);
676 memset(bh
->b_data
+ size
, 0, rest
);
677 set_buffer_uptodate(bh
);
679 ext4_journal_dirty_metadata(handle
, bh
);
682 if ((err2
= ext4_journal_stop(handle
)) && !err
)
686 * Ugh! Need to have e2fsck write the backup copies. It is too
687 * late to revert the resize, we shouldn't fail just because of
688 * the backup copies (they are only needed in case of corruption).
690 * However, if we got here we have a journal problem too, so we
691 * can't really start a transaction to mark the superblock.
692 * Chicken out and just set the flag on the hope it will be written
693 * to disk, and if not - we will simply wait until next fsck.
697 ext4_warning(sb
, __FUNCTION__
,
698 "can't update backup for group %d (err %d), "
699 "forcing fsck on next reboot", group
, err
);
700 sbi
->s_mount_state
&= ~EXT4_VALID_FS
;
701 sbi
->s_es
->s_state
&= cpu_to_le16(~EXT4_VALID_FS
);
702 mark_buffer_dirty(sbi
->s_sbh
);
706 /* Add group descriptor data to an existing or new group descriptor block.
707 * Ensure we handle all possible error conditions _before_ we start modifying
708 * the filesystem, because we cannot abort the transaction and not have it
709 * write the data to disk.
711 * If we are on a GDT block boundary, we need to get the reserved GDT block.
712 * Otherwise, we may need to add backup GDT blocks for a sparse group.
714 * We only need to hold the superblock lock while we are actually adding
715 * in the new group's counts to the superblock. Prior to that we have
716 * not really "added" the group at all. We re-check that we are still
717 * adding in the last group in case things have changed since verifying.
719 int ext4_group_add(struct super_block
*sb
, struct ext4_new_group_data
*input
)
721 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
722 struct ext4_super_block
*es
= sbi
->s_es
;
723 int reserved_gdb
= ext4_bg_has_super(sb
, input
->group
) ?
724 le16_to_cpu(es
->s_reserved_gdt_blocks
) : 0;
725 struct buffer_head
*primary
= NULL
;
726 struct ext4_group_desc
*gdp
;
727 struct inode
*inode
= NULL
;
729 int gdb_off
, gdb_num
;
732 gdb_num
= input
->group
/ EXT4_DESC_PER_BLOCK(sb
);
733 gdb_off
= input
->group
% EXT4_DESC_PER_BLOCK(sb
);
735 if (gdb_off
== 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb
,
736 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
737 ext4_warning(sb
, __FUNCTION__
,
738 "Can't resize non-sparse filesystem further");
742 if (ext4_blocks_count(es
) + input
->blocks_count
<
743 ext4_blocks_count(es
)) {
744 ext4_warning(sb
, __FUNCTION__
, "blocks_count overflow\n");
748 if (le32_to_cpu(es
->s_inodes_count
) + EXT4_INODES_PER_GROUP(sb
) <
749 le32_to_cpu(es
->s_inodes_count
)) {
750 ext4_warning(sb
, __FUNCTION__
, "inodes_count overflow\n");
754 if (reserved_gdb
|| gdb_off
== 0) {
755 if (!EXT4_HAS_COMPAT_FEATURE(sb
,
756 EXT4_FEATURE_COMPAT_RESIZE_INODE
)){
757 ext4_warning(sb
, __FUNCTION__
,
758 "No reserved GDT blocks, can't resize");
761 inode
= iget(sb
, EXT4_RESIZE_INO
);
762 if (!inode
|| is_bad_inode(inode
)) {
763 ext4_warning(sb
, __FUNCTION__
,
764 "Error opening resize inode");
770 if ((err
= verify_group_input(sb
, input
)))
773 if ((err
= setup_new_group_blocks(sb
, input
)))
777 * We will always be modifying at least the superblock and a GDT
778 * block. If we are adding a group past the last current GDT block,
779 * we will also modify the inode and the dindirect block. If we
780 * are adding a group with superblock/GDT backups we will also
781 * modify each of the reserved GDT dindirect blocks.
783 handle
= ext4_journal_start_sb(sb
,
784 ext4_bg_has_super(sb
, input
->group
) ?
785 3 + reserved_gdb
: 4);
786 if (IS_ERR(handle
)) {
787 err
= PTR_ERR(handle
);
792 if (input
->group
!= sbi
->s_groups_count
) {
793 ext4_warning(sb
, __FUNCTION__
,
794 "multiple resizers run on filesystem!");
799 if ((err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
)))
803 * We will only either add reserved group blocks to a backup group
804 * or remove reserved blocks for the first group in a new group block.
805 * Doing both would be mean more complex code, and sane people don't
806 * use non-sparse filesystems anymore. This is already checked above.
809 primary
= sbi
->s_group_desc
[gdb_num
];
810 if ((err
= ext4_journal_get_write_access(handle
, primary
)))
813 if (reserved_gdb
&& ext4_bg_num_gdb(sb
, input
->group
) &&
814 (err
= reserve_backup_gdb(handle
, inode
, input
)))
816 } else if ((err
= add_new_gdb(handle
, inode
, input
, &primary
)))
820 * OK, now we've set up the new group. Time to make it active.
822 * Current kernels don't lock all allocations via lock_super(),
823 * so we have to be safe wrt. concurrent accesses the group
824 * data. So we need to be careful to set all of the relevant
825 * group descriptor data etc. *before* we enable the group.
827 * The key field here is sbi->s_groups_count: as long as
828 * that retains its old value, nobody is going to access the new
831 * So first we update all the descriptor metadata for the new
832 * group; then we update the total disk blocks count; then we
833 * update the groups count to enable the group; then finally we
834 * update the free space counts so that the system can start
835 * using the new disk blocks.
838 /* Update group descriptor block for new group */
839 gdp
= (struct ext4_group_desc
*)primary
->b_data
+ gdb_off
;
841 ext4_block_bitmap_set(sb
, gdp
, input
->block_bitmap
); /* LV FIXME */
842 ext4_inode_bitmap_set(sb
, gdp
, input
->inode_bitmap
); /* LV FIXME */
843 ext4_inode_table_set(sb
, gdp
, input
->inode_table
); /* LV FIXME */
844 gdp
->bg_free_blocks_count
= cpu_to_le16(input
->free_blocks_count
);
845 gdp
->bg_free_inodes_count
= cpu_to_le16(EXT4_INODES_PER_GROUP(sb
));
848 * Make the new blocks and inodes valid next. We do this before
849 * increasing the group count so that once the group is enabled,
850 * all of its blocks and inodes are already valid.
852 * We always allocate group-by-group, then block-by-block or
853 * inode-by-inode within a group, so enabling these
854 * blocks/inodes before the group is live won't actually let us
855 * allocate the new space yet.
857 ext4_blocks_count_set(es
, ext4_blocks_count(es
) +
858 input
->blocks_count
);
859 es
->s_inodes_count
= cpu_to_le32(le32_to_cpu(es
->s_inodes_count
) +
860 EXT4_INODES_PER_GROUP(sb
));
863 * We need to protect s_groups_count against other CPUs seeing
864 * inconsistent state in the superblock.
866 * The precise rules we use are:
868 * * Writers of s_groups_count *must* hold lock_super
870 * * Writers must perform a smp_wmb() after updating all dependent
871 * data and before modifying the groups count
873 * * Readers must hold lock_super() over the access
875 * * Readers must perform an smp_rmb() after reading the groups count
876 * and before reading any dependent data.
878 * NB. These rules can be relaxed when checking the group count
879 * while freeing data, as we can only allocate from a block
880 * group after serialising against the group count, and we can
881 * only then free after serialising in turn against that
886 /* Update the global fs size fields */
887 sbi
->s_groups_count
++;
889 ext4_journal_dirty_metadata(handle
, primary
);
891 /* Update the reserved block counts only once the new group is
893 ext4_r_blocks_count_set(es
, ext4_r_blocks_count(es
) +
894 input
->reserved_blocks
);
896 /* Update the free space counts */
897 percpu_counter_mod(&sbi
->s_freeblocks_counter
,
898 input
->free_blocks_count
);
899 percpu_counter_mod(&sbi
->s_freeinodes_counter
,
900 EXT4_INODES_PER_GROUP(sb
));
902 ext4_journal_dirty_metadata(handle
, sbi
->s_sbh
);
907 if ((err2
= ext4_journal_stop(handle
)) && !err
)
910 update_backups(sb
, sbi
->s_sbh
->b_blocknr
, (char *)es
,
911 sizeof(struct ext4_super_block
));
912 update_backups(sb
, primary
->b_blocknr
, primary
->b_data
,
918 } /* ext4_group_add */
920 /* Extend the filesystem to the new number of blocks specified. This entry
921 * point is only used to extend the current filesystem to the end of the last
922 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
923 * for emergencies (because it has no dependencies on reserved blocks).
925 * If we _really_ wanted, we could use default values to call ext4_group_add()
926 * allow the "remount" trick to work for arbitrary resizing, assuming enough
927 * GDT blocks are reserved to grow to the desired size.
929 int ext4_group_extend(struct super_block
*sb
, struct ext4_super_block
*es
,
930 ext4_fsblk_t n_blocks_count
)
932 ext4_fsblk_t o_blocks_count
;
933 unsigned long o_groups_count
;
936 struct buffer_head
* bh
;
939 unsigned long freed_blocks
;
941 /* We don't need to worry about locking wrt other resizers just
942 * yet: we're going to revalidate es->s_blocks_count after
943 * taking lock_super() below. */
944 o_blocks_count
= ext4_blocks_count(es
);
945 o_groups_count
= EXT4_SB(sb
)->s_groups_count
;
947 if (test_opt(sb
, DEBUG
))
948 printk(KERN_DEBUG
"EXT4-fs: extending last group from %llu uto %llu blocks\n",
949 o_blocks_count
, n_blocks_count
);
951 if (n_blocks_count
== 0 || n_blocks_count
== o_blocks_count
)
954 if (n_blocks_count
> (sector_t
)(~0ULL) >> (sb
->s_blocksize_bits
- 9)) {
955 printk(KERN_ERR
"EXT4-fs: filesystem on %s:"
956 " too large to resize to %llu blocks safely\n",
957 sb
->s_id
, n_blocks_count
);
958 if (sizeof(sector_t
) < 8)
959 ext4_warning(sb
, __FUNCTION__
,
960 "CONFIG_LBD not enabled\n");
964 if (n_blocks_count
< o_blocks_count
) {
965 ext4_warning(sb
, __FUNCTION__
,
966 "can't shrink FS - resize aborted");
970 /* Handle the remaining blocks in the last group only. */
971 ext4_get_group_no_and_offset(sb
, o_blocks_count
, NULL
, &last
);
974 ext4_warning(sb
, __FUNCTION__
,
975 "need to use ext2online to resize further");
979 add
= EXT4_BLOCKS_PER_GROUP(sb
) - last
;
981 if (o_blocks_count
+ add
< o_blocks_count
) {
982 ext4_warning(sb
, __FUNCTION__
, "blocks_count overflow");
986 if (o_blocks_count
+ add
> n_blocks_count
)
987 add
= n_blocks_count
- o_blocks_count
;
989 if (o_blocks_count
+ add
< n_blocks_count
)
990 ext4_warning(sb
, __FUNCTION__
,
991 "will only finish group (%llu"
993 o_blocks_count
+ add
, add
);
995 /* See if the device is actually as big as what was requested */
996 bh
= sb_bread(sb
, o_blocks_count
+ add
-1);
998 ext4_warning(sb
, __FUNCTION__
,
999 "can't read last block, resize aborted");
1004 /* We will update the superblock, one block bitmap, and
1005 * one group descriptor via ext4_free_blocks().
1007 handle
= ext4_journal_start_sb(sb
, 3);
1008 if (IS_ERR(handle
)) {
1009 err
= PTR_ERR(handle
);
1010 ext4_warning(sb
, __FUNCTION__
, "error %d on journal start",err
);
1015 if (o_blocks_count
!= ext4_blocks_count(es
)) {
1016 ext4_warning(sb
, __FUNCTION__
,
1017 "multiple resizers run on filesystem!");
1023 if ((err
= ext4_journal_get_write_access(handle
,
1024 EXT4_SB(sb
)->s_sbh
))) {
1025 ext4_warning(sb
, __FUNCTION__
,
1026 "error %d on journal write access", err
);
1028 ext4_journal_stop(handle
);
1031 ext4_blocks_count_set(es
, o_blocks_count
+ add
);
1032 ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
1035 ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count
,
1036 o_blocks_count
+ add
);
1037 ext4_free_blocks_sb(handle
, sb
, o_blocks_count
, add
, &freed_blocks
);
1038 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count
,
1039 o_blocks_count
+ add
);
1040 if ((err
= ext4_journal_stop(handle
)))
1042 if (test_opt(sb
, DEBUG
))
1043 printk(KERN_DEBUG
"EXT4-fs: extended group to %llu blocks\n",
1044 ext4_blocks_count(es
));
1045 update_backups(sb
, EXT4_SB(sb
)->s_sbh
->b_blocknr
, (char *)es
,
1046 sizeof(struct ext4_super_block
));
1049 } /* ext4_group_extend */