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/ext4_jbd2.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
20 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
21 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
23 static int verify_group_input(struct super_block
*sb
,
24 struct ext4_new_group_data
*input
)
26 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
27 struct ext4_super_block
*es
= sbi
->s_es
;
28 ext4_fsblk_t start
= ext4_blocks_count(es
);
29 ext4_fsblk_t end
= start
+ input
->blocks_count
;
30 unsigned group
= input
->group
;
31 ext4_fsblk_t itend
= input
->inode_table
+ sbi
->s_itb_per_group
;
32 unsigned overhead
= ext4_bg_has_super(sb
, group
) ?
33 (1 + ext4_bg_num_gdb(sb
, group
) +
34 le16_to_cpu(es
->s_reserved_gdt_blocks
)) : 0;
35 ext4_fsblk_t metaend
= start
+ overhead
;
36 struct buffer_head
*bh
= NULL
;
37 ext4_grpblk_t free_blocks_count
, offset
;
40 input
->free_blocks_count
= free_blocks_count
=
41 input
->blocks_count
- 2 - overhead
- sbi
->s_itb_per_group
;
43 if (test_opt(sb
, DEBUG
))
44 printk(KERN_DEBUG
"EXT4-fs: adding %s group %u: %u blocks "
45 "(%d free, %u reserved)\n",
46 ext4_bg_has_super(sb
, input
->group
) ? "normal" :
47 "no-super", input
->group
, input
->blocks_count
,
48 free_blocks_count
, input
->reserved_blocks
);
50 ext4_get_group_no_and_offset(sb
, start
, NULL
, &offset
);
51 if (group
!= sbi
->s_groups_count
)
52 ext4_warning(sb
, __FUNCTION__
,
53 "Cannot add at group %u (only %lu groups)",
54 input
->group
, sbi
->s_groups_count
);
56 ext4_warning(sb
, __FUNCTION__
, "Last group not full");
57 else if (input
->reserved_blocks
> input
->blocks_count
/ 5)
58 ext4_warning(sb
, __FUNCTION__
, "Reserved blocks too high (%u)",
59 input
->reserved_blocks
);
60 else if (free_blocks_count
< 0)
61 ext4_warning(sb
, __FUNCTION__
, "Bad blocks count %u",
63 else if (!(bh
= sb_bread(sb
, end
- 1)))
64 ext4_warning(sb
, __FUNCTION__
,
65 "Cannot read last block (%llu)",
67 else if (outside(input
->block_bitmap
, start
, end
))
68 ext4_warning(sb
, __FUNCTION__
,
69 "Block bitmap not in group (block %llu)",
70 (unsigned long long)input
->block_bitmap
);
71 else if (outside(input
->inode_bitmap
, start
, end
))
72 ext4_warning(sb
, __FUNCTION__
,
73 "Inode bitmap not in group (block %llu)",
74 (unsigned long long)input
->inode_bitmap
);
75 else if (outside(input
->inode_table
, start
, end
) ||
76 outside(itend
- 1, start
, end
))
77 ext4_warning(sb
, __FUNCTION__
,
78 "Inode table not in group (blocks %llu-%llu)",
79 (unsigned long long)input
->inode_table
, itend
- 1);
80 else if (input
->inode_bitmap
== input
->block_bitmap
)
81 ext4_warning(sb
, __FUNCTION__
,
82 "Block bitmap same as inode bitmap (%llu)",
83 (unsigned long long)input
->block_bitmap
);
84 else if (inside(input
->block_bitmap
, input
->inode_table
, itend
))
85 ext4_warning(sb
, __FUNCTION__
,
86 "Block bitmap (%llu) in inode table (%llu-%llu)",
87 (unsigned long long)input
->block_bitmap
,
88 (unsigned long long)input
->inode_table
, itend
- 1);
89 else if (inside(input
->inode_bitmap
, input
->inode_table
, itend
))
90 ext4_warning(sb
, __FUNCTION__
,
91 "Inode bitmap (%llu) in inode table (%llu-%llu)",
92 (unsigned long long)input
->inode_bitmap
,
93 (unsigned long long)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 (unsigned long long)input
->block_bitmap
,
100 else if (inside(input
->inode_bitmap
, start
, metaend
))
101 ext4_warning(sb
, __FUNCTION__
,
102 "Inode bitmap (%llu) in GDT table"
104 (unsigned long long)input
->inode_bitmap
,
106 else if (inside(input
->inode_table
, start
, metaend
) ||
107 inside(itend
- 1, start
, metaend
))
108 ext4_warning(sb
, __FUNCTION__
,
109 "Inode table (%llu-%llu) overlaps"
110 "GDT table (%llu-%llu)",
111 (unsigned long long)input
->inode_table
,
112 itend
- 1, start
, metaend
- 1);
120 static struct buffer_head
*bclean(handle_t
*handle
, struct super_block
*sb
,
123 struct buffer_head
*bh
;
126 bh
= sb_getblk(sb
, blk
);
128 return ERR_PTR(-EIO
);
129 if ((err
= ext4_journal_get_write_access(handle
, bh
))) {
134 memset(bh
->b_data
, 0, sb
->s_blocksize
);
135 set_buffer_uptodate(bh
);
143 * To avoid calling the atomic setbit hundreds or thousands of times, we only
144 * need to use it within a single byte (to ensure we get endianness right).
145 * We can use memset for the rest of the bitmap as there are no other users.
147 static void mark_bitmap_end(int start_bit
, int end_bit
, char *bitmap
)
151 if (start_bit
>= end_bit
)
154 ext4_debug("mark end bits +%d through +%d used\n", start_bit
, end_bit
);
155 for (i
= start_bit
; i
< ((start_bit
+ 7) & ~7UL); i
++)
156 ext4_set_bit(i
, bitmap
);
158 memset(bitmap
+ (i
>> 3), 0xff, (end_bit
- i
) >> 3);
162 * Set up the block and inode bitmaps, and the inode table for the new group.
163 * This doesn't need to be part of the main transaction, since we are only
164 * changing blocks outside the actual filesystem. We still do journaling to
165 * ensure the recovery is correct in case of a failure just after resize.
166 * If any part of this fails, we simply abort the resize.
168 static int setup_new_group_blocks(struct super_block
*sb
,
169 struct ext4_new_group_data
*input
)
171 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
172 ext4_fsblk_t start
= ext4_group_first_block_no(sb
, input
->group
);
173 int reserved_gdb
= ext4_bg_has_super(sb
, input
->group
) ?
174 le16_to_cpu(sbi
->s_es
->s_reserved_gdt_blocks
) : 0;
175 unsigned long gdblocks
= ext4_bg_num_gdb(sb
, input
->group
);
176 struct buffer_head
*bh
;
183 handle
= ext4_journal_start_sb(sb
, reserved_gdb
+ gdblocks
+
184 2 + sbi
->s_itb_per_group
);
186 return PTR_ERR(handle
);
189 if (input
->group
!= sbi
->s_groups_count
) {
194 if (IS_ERR(bh
= bclean(handle
, sb
, input
->block_bitmap
))) {
199 if (ext4_bg_has_super(sb
, input
->group
)) {
200 ext4_debug("mark backup superblock %#04lx (+0)\n", start
);
201 ext4_set_bit(0, bh
->b_data
);
204 /* Copy all of the GDT blocks into the backup in this group */
205 for (i
= 0, bit
= 1, block
= start
+ 1;
206 i
< gdblocks
; i
++, block
++, bit
++) {
207 struct buffer_head
*gdb
;
209 ext4_debug("update backup group %#04lx (+%d)\n", block
, bit
);
211 gdb
= sb_getblk(sb
, block
);
216 if ((err
= ext4_journal_get_write_access(handle
, gdb
))) {
221 memcpy(gdb
->b_data
, sbi
->s_group_desc
[i
]->b_data
, bh
->b_size
);
222 set_buffer_uptodate(gdb
);
224 ext4_journal_dirty_metadata(handle
, gdb
);
225 ext4_set_bit(bit
, bh
->b_data
);
229 /* Zero out all of the reserved backup group descriptor table blocks */
230 for (i
= 0, bit
= gdblocks
+ 1, block
= start
+ bit
;
231 i
< reserved_gdb
; i
++, block
++, bit
++) {
232 struct buffer_head
*gdb
;
234 ext4_debug("clear reserved block %#04lx (+%d)\n", block
, bit
);
236 if (IS_ERR(gdb
= bclean(handle
, sb
, block
))) {
240 ext4_journal_dirty_metadata(handle
, gdb
);
241 ext4_set_bit(bit
, bh
->b_data
);
244 ext4_debug("mark block bitmap %#04x (+%ld)\n", input
->block_bitmap
,
245 input
->block_bitmap
- start
);
246 ext4_set_bit(input
->block_bitmap
- start
, bh
->b_data
);
247 ext4_debug("mark inode bitmap %#04x (+%ld)\n", input
->inode_bitmap
,
248 input
->inode_bitmap
- start
);
249 ext4_set_bit(input
->inode_bitmap
- start
, bh
->b_data
);
251 /* Zero out all of the inode table blocks */
252 for (i
= 0, block
= input
->inode_table
, bit
= block
- start
;
253 i
< sbi
->s_itb_per_group
; i
++, bit
++, block
++) {
254 struct buffer_head
*it
;
256 ext4_debug("clear inode block %#04lx (+%d)\n", block
, bit
);
257 if (IS_ERR(it
= bclean(handle
, sb
, block
))) {
261 ext4_journal_dirty_metadata(handle
, it
);
263 ext4_set_bit(bit
, bh
->b_data
);
265 mark_bitmap_end(input
->blocks_count
, EXT4_BLOCKS_PER_GROUP(sb
),
267 ext4_journal_dirty_metadata(handle
, bh
);
270 /* Mark unused entries in inode bitmap used */
271 ext4_debug("clear inode bitmap %#04x (+%ld)\n",
272 input
->inode_bitmap
, input
->inode_bitmap
- start
);
273 if (IS_ERR(bh
= bclean(handle
, sb
, input
->inode_bitmap
))) {
278 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb
), EXT4_BLOCKS_PER_GROUP(sb
),
280 ext4_journal_dirty_metadata(handle
, bh
);
286 if ((err2
= ext4_journal_stop(handle
)) && !err
)
294 * Iterate through the groups which hold BACKUP superblock/GDT copies in an
295 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before
296 * calling this for the first time. In a sparse filesystem it will be the
297 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
298 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
300 static unsigned ext4_list_backups(struct super_block
*sb
, unsigned *three
,
301 unsigned *five
, unsigned *seven
)
303 unsigned *min
= three
;
307 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb
,
308 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
330 * Check that all of the backup GDT blocks are held in the primary GDT block.
331 * It is assumed that they are stored in group order. Returns the number of
332 * groups in current filesystem that have BACKUPS, or -ve error code.
334 static int verify_reserved_gdb(struct super_block
*sb
,
335 struct buffer_head
*primary
)
337 const ext4_fsblk_t blk
= primary
->b_blocknr
;
338 const unsigned long end
= EXT4_SB(sb
)->s_groups_count
;
343 __le32
*p
= (__le32
*)primary
->b_data
;
346 while ((grp
= ext4_list_backups(sb
, &three
, &five
, &seven
)) < end
) {
347 if (le32_to_cpu(*p
++) !=
348 grp
* EXT4_BLOCKS_PER_GROUP(sb
) + blk
){
349 ext4_warning(sb
, __FUNCTION__
,
351 " missing grp %d (%llu)",
354 (ext4_fsblk_t
)EXT4_BLOCKS_PER_GROUP(sb
) +
358 if (++gdbackups
> EXT4_ADDR_PER_BLOCK(sb
))
366 * Called when we need to bring a reserved group descriptor table block into
367 * use from the resize inode. The primary copy of the new GDT block currently
368 * is an indirect block (under the double indirect block in the resize inode).
369 * The new backup GDT blocks will be stored as leaf blocks in this indirect
370 * block, in group order. Even though we know all the block numbers we need,
371 * we check to ensure that the resize inode has actually reserved these blocks.
373 * Don't need to update the block bitmaps because the blocks are still in use.
375 * We get all of the error cases out of the way, so that we are sure to not
376 * fail once we start modifying the data on disk, because JBD has no rollback.
378 static int add_new_gdb(handle_t
*handle
, struct inode
*inode
,
379 struct ext4_new_group_data
*input
,
380 struct buffer_head
**primary
)
382 struct super_block
*sb
= inode
->i_sb
;
383 struct ext4_super_block
*es
= EXT4_SB(sb
)->s_es
;
384 unsigned long gdb_num
= input
->group
/ EXT4_DESC_PER_BLOCK(sb
);
385 ext4_fsblk_t gdblock
= EXT4_SB(sb
)->s_sbh
->b_blocknr
+ 1 + gdb_num
;
386 struct buffer_head
**o_group_desc
, **n_group_desc
;
387 struct buffer_head
*dind
;
389 struct ext4_iloc iloc
;
393 if (test_opt(sb
, DEBUG
))
395 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
399 * If we are not using the primary superblock/GDT copy don't resize,
400 * because the user tools have no way of handling this. Probably a
401 * bad time to do it anyways.
403 if (EXT4_SB(sb
)->s_sbh
->b_blocknr
!=
404 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
)) {
405 ext4_warning(sb
, __FUNCTION__
,
406 "won't resize using backup superblock at %llu",
407 (unsigned long long)EXT4_SB(sb
)->s_sbh
->b_blocknr
);
411 *primary
= sb_bread(sb
, gdblock
);
415 if ((gdbackups
= verify_reserved_gdb(sb
, *primary
)) < 0) {
420 data
= EXT4_I(inode
)->i_data
+ EXT4_DIND_BLOCK
;
421 dind
= sb_bread(sb
, le32_to_cpu(*data
));
427 data
= (__le32
*)dind
->b_data
;
428 if (le32_to_cpu(data
[gdb_num
% EXT4_ADDR_PER_BLOCK(sb
)]) != gdblock
) {
429 ext4_warning(sb
, __FUNCTION__
,
430 "new group %u GDT block %llu not reserved",
431 input
->group
, gdblock
);
436 if ((err
= ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
)))
439 if ((err
= ext4_journal_get_write_access(handle
, *primary
)))
442 if ((err
= ext4_journal_get_write_access(handle
, dind
)))
445 /* ext4_reserve_inode_write() gets a reference on the iloc */
446 if ((err
= ext4_reserve_inode_write(handle
, inode
, &iloc
)))
449 n_group_desc
= kmalloc((gdb_num
+ 1) * sizeof(struct buffer_head
*),
453 ext4_warning (sb
, __FUNCTION__
,
454 "not enough memory for %lu groups", gdb_num
+ 1);
459 * Finally, we have all of the possible failures behind us...
461 * Remove new GDT block from inode double-indirect block and clear out
462 * the new GDT block for use (which also "frees" the backup GDT blocks
463 * from the reserved inode). We don't need to change the bitmaps for
464 * these blocks, because they are marked as in-use from being in the
465 * reserved inode, and will become GDT blocks (primary and backup).
467 data
[gdb_num
% EXT4_ADDR_PER_BLOCK(sb
)] = 0;
468 ext4_journal_dirty_metadata(handle
, dind
);
470 inode
->i_blocks
-= (gdbackups
+ 1) * sb
->s_blocksize
>> 9;
471 ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
472 memset((*primary
)->b_data
, 0, sb
->s_blocksize
);
473 ext4_journal_dirty_metadata(handle
, *primary
);
475 o_group_desc
= EXT4_SB(sb
)->s_group_desc
;
476 memcpy(n_group_desc
, o_group_desc
,
477 EXT4_SB(sb
)->s_gdb_count
* sizeof(struct buffer_head
*));
478 n_group_desc
[gdb_num
] = *primary
;
479 EXT4_SB(sb
)->s_group_desc
= n_group_desc
;
480 EXT4_SB(sb
)->s_gdb_count
++;
483 es
->s_reserved_gdt_blocks
=
484 cpu_to_le16(le16_to_cpu(es
->s_reserved_gdt_blocks
) - 1);
485 ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
490 //ext4_journal_release_buffer(handle, iloc.bh);
493 //ext4_journal_release_buffer(handle, dind);
495 //ext4_journal_release_buffer(handle, *primary);
497 //ext4_journal_release_buffer(handle, *primary);
503 ext4_debug("leaving with error %d\n", err
);
508 * Called when we are adding a new group which has a backup copy of each of
509 * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks.
510 * We need to add these reserved backup GDT blocks to the resize inode, so
511 * that they are kept for future resizing and not allocated to files.
513 * Each reserved backup GDT block will go into a different indirect block.
514 * The indirect blocks are actually the primary reserved GDT blocks,
515 * so we know in advance what their block numbers are. We only get the
516 * double-indirect block to verify it is pointing to the primary reserved
517 * GDT blocks so we don't overwrite a data block by accident. The reserved
518 * backup GDT blocks are stored in their reserved primary GDT block.
520 static int reserve_backup_gdb(handle_t
*handle
, struct inode
*inode
,
521 struct ext4_new_group_data
*input
)
523 struct super_block
*sb
= inode
->i_sb
;
524 int reserved_gdb
=le16_to_cpu(EXT4_SB(sb
)->s_es
->s_reserved_gdt_blocks
);
525 struct buffer_head
**primary
;
526 struct buffer_head
*dind
;
527 struct ext4_iloc iloc
;
534 primary
= kmalloc(reserved_gdb
* sizeof(*primary
), GFP_KERNEL
);
538 data
= EXT4_I(inode
)->i_data
+ EXT4_DIND_BLOCK
;
539 dind
= sb_bread(sb
, le32_to_cpu(*data
));
545 blk
= EXT4_SB(sb
)->s_sbh
->b_blocknr
+ 1 + EXT4_SB(sb
)->s_gdb_count
;
546 data
= (__le32
*)dind
->b_data
+ EXT4_SB(sb
)->s_gdb_count
;
547 end
= (__le32
*)dind
->b_data
+ EXT4_ADDR_PER_BLOCK(sb
);
549 /* Get each reserved primary GDT block and verify it holds backups */
550 for (res
= 0; res
< reserved_gdb
; res
++, blk
++) {
551 if (le32_to_cpu(*data
) != blk
) {
552 ext4_warning(sb
, __FUNCTION__
,
553 "reserved block %llu"
554 " not at offset %ld",
556 (long)(data
- (__le32
*)dind
->b_data
));
560 primary
[res
] = sb_bread(sb
, blk
);
565 if ((gdbackups
= verify_reserved_gdb(sb
, primary
[res
])) < 0) {
566 brelse(primary
[res
]);
571 data
= (__le32
*)dind
->b_data
;
574 for (i
= 0; i
< reserved_gdb
; i
++) {
575 if ((err
= ext4_journal_get_write_access(handle
, primary
[i
]))) {
578 for (j = 0; j < i; j++)
579 ext4_journal_release_buffer(handle, primary[j]);
585 if ((err
= ext4_reserve_inode_write(handle
, inode
, &iloc
)))
589 * Finally we can add each of the reserved backup GDT blocks from
590 * the new group to its reserved primary GDT block.
592 blk
= input
->group
* EXT4_BLOCKS_PER_GROUP(sb
);
593 for (i
= 0; i
< reserved_gdb
; i
++) {
595 data
= (__le32
*)primary
[i
]->b_data
;
596 /* printk("reserving backup %lu[%u] = %lu\n",
597 primary[i]->b_blocknr, gdbackups,
598 blk + primary[i]->b_blocknr); */
599 data
[gdbackups
] = cpu_to_le32(blk
+ primary
[i
]->b_blocknr
);
600 err2
= ext4_journal_dirty_metadata(handle
, primary
[i
]);
604 inode
->i_blocks
+= reserved_gdb
* sb
->s_blocksize
>> 9;
605 ext4_mark_iloc_dirty(handle
, inode
, &iloc
);
609 brelse(primary
[res
]);
619 * Update the backup copies of the ext4 metadata. These don't need to be part
620 * of the main resize transaction, because e2fsck will re-write them if there
621 * is a problem (basically only OOM will cause a problem). However, we
622 * _should_ update the backups if possible, in case the primary gets trashed
623 * for some reason and we need to run e2fsck from a backup superblock. The
624 * important part is that the new block and inode counts are in the backup
625 * superblocks, and the location of the new group metadata in the GDT backups.
627 * We do not need lock_super() for this, because these blocks are not
628 * otherwise touched by the filesystem code when it is mounted. We don't
629 * need to worry about last changing from sbi->s_groups_count, because the
630 * worst that can happen is that we do not copy the full number of backups
631 * at this time. The resize which changed s_groups_count will backup again.
633 static void update_backups(struct super_block
*sb
,
634 int blk_off
, char *data
, int size
)
636 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
637 const unsigned long last
= sbi
->s_groups_count
;
638 const int bpg
= EXT4_BLOCKS_PER_GROUP(sb
);
643 int rest
= sb
->s_blocksize
- size
;
647 handle
= ext4_journal_start_sb(sb
, EXT4_MAX_TRANS_DATA
);
648 if (IS_ERR(handle
)) {
650 err
= PTR_ERR(handle
);
654 while ((group
= ext4_list_backups(sb
, &three
, &five
, &seven
)) < last
) {
655 struct buffer_head
*bh
;
657 /* Out of journal space, and can't get more - abort - so sad */
658 if (handle
->h_buffer_credits
== 0 &&
659 ext4_journal_extend(handle
, EXT4_MAX_TRANS_DATA
) &&
660 (err
= ext4_journal_restart(handle
, EXT4_MAX_TRANS_DATA
)))
663 bh
= sb_getblk(sb
, group
* bpg
+ blk_off
);
668 ext4_debug("update metadata backup %#04lx\n",
669 (unsigned long)bh
->b_blocknr
);
670 if ((err
= ext4_journal_get_write_access(handle
, bh
)))
673 memcpy(bh
->b_data
, data
, size
);
675 memset(bh
->b_data
+ size
, 0, rest
);
676 set_buffer_uptodate(bh
);
678 ext4_journal_dirty_metadata(handle
, bh
);
681 if ((err2
= ext4_journal_stop(handle
)) && !err
)
685 * Ugh! Need to have e2fsck write the backup copies. It is too
686 * late to revert the resize, we shouldn't fail just because of
687 * the backup copies (they are only needed in case of corruption).
689 * However, if we got here we have a journal problem too, so we
690 * can't really start a transaction to mark the superblock.
691 * Chicken out and just set the flag on the hope it will be written
692 * to disk, and if not - we will simply wait until next fsck.
696 ext4_warning(sb
, __FUNCTION__
,
697 "can't update backup for group %d (err %d), "
698 "forcing fsck on next reboot", group
, err
);
699 sbi
->s_mount_state
&= ~EXT4_VALID_FS
;
700 sbi
->s_es
->s_state
&= cpu_to_le16(~EXT4_VALID_FS
);
701 mark_buffer_dirty(sbi
->s_sbh
);
705 /* Add group descriptor data to an existing or new group descriptor block.
706 * Ensure we handle all possible error conditions _before_ we start modifying
707 * the filesystem, because we cannot abort the transaction and not have it
708 * write the data to disk.
710 * If we are on a GDT block boundary, we need to get the reserved GDT block.
711 * Otherwise, we may need to add backup GDT blocks for a sparse group.
713 * We only need to hold the superblock lock while we are actually adding
714 * in the new group's counts to the superblock. Prior to that we have
715 * not really "added" the group at all. We re-check that we are still
716 * adding in the last group in case things have changed since verifying.
718 int ext4_group_add(struct super_block
*sb
, struct ext4_new_group_data
*input
)
720 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
721 struct ext4_super_block
*es
= sbi
->s_es
;
722 int reserved_gdb
= ext4_bg_has_super(sb
, input
->group
) ?
723 le16_to_cpu(es
->s_reserved_gdt_blocks
) : 0;
724 struct buffer_head
*primary
= NULL
;
725 struct ext4_group_desc
*gdp
;
726 struct inode
*inode
= NULL
;
728 int gdb_off
, gdb_num
;
731 gdb_num
= input
->group
/ EXT4_DESC_PER_BLOCK(sb
);
732 gdb_off
= input
->group
% EXT4_DESC_PER_BLOCK(sb
);
734 if (gdb_off
== 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb
,
735 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
736 ext4_warning(sb
, __FUNCTION__
,
737 "Can't resize non-sparse filesystem further");
741 if (ext4_blocks_count(es
) + input
->blocks_count
<
742 ext4_blocks_count(es
)) {
743 ext4_warning(sb
, __FUNCTION__
, "blocks_count overflow\n");
747 if (le32_to_cpu(es
->s_inodes_count
) + EXT4_INODES_PER_GROUP(sb
) <
748 le32_to_cpu(es
->s_inodes_count
)) {
749 ext4_warning(sb
, __FUNCTION__
, "inodes_count overflow\n");
753 if (reserved_gdb
|| gdb_off
== 0) {
754 if (!EXT4_HAS_COMPAT_FEATURE(sb
,
755 EXT4_FEATURE_COMPAT_RESIZE_INODE
)){
756 ext4_warning(sb
, __FUNCTION__
,
757 "No reserved GDT blocks, can't resize");
760 inode
= iget(sb
, EXT4_RESIZE_INO
);
761 if (!inode
|| is_bad_inode(inode
)) {
762 ext4_warning(sb
, __FUNCTION__
,
763 "Error opening resize inode");
769 if ((err
= verify_group_input(sb
, input
)))
772 if ((err
= setup_new_group_blocks(sb
, input
)))
776 * We will always be modifying at least the superblock and a GDT
777 * block. If we are adding a group past the last current GDT block,
778 * we will also modify the inode and the dindirect block. If we
779 * are adding a group with superblock/GDT backups we will also
780 * modify each of the reserved GDT dindirect blocks.
782 handle
= ext4_journal_start_sb(sb
,
783 ext4_bg_has_super(sb
, input
->group
) ?
784 3 + reserved_gdb
: 4);
785 if (IS_ERR(handle
)) {
786 err
= PTR_ERR(handle
);
791 if (input
->group
!= sbi
->s_groups_count
) {
792 ext4_warning(sb
, __FUNCTION__
,
793 "multiple resizers run on filesystem!");
798 if ((err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
)))
802 * We will only either add reserved group blocks to a backup group
803 * or remove reserved blocks for the first group in a new group block.
804 * Doing both would be mean more complex code, and sane people don't
805 * use non-sparse filesystems anymore. This is already checked above.
808 primary
= sbi
->s_group_desc
[gdb_num
];
809 if ((err
= ext4_journal_get_write_access(handle
, primary
)))
812 if (reserved_gdb
&& ext4_bg_num_gdb(sb
, input
->group
) &&
813 (err
= reserve_backup_gdb(handle
, inode
, input
)))
815 } else if ((err
= add_new_gdb(handle
, inode
, input
, &primary
)))
819 * OK, now we've set up the new group. Time to make it active.
821 * Current kernels don't lock all allocations via lock_super(),
822 * so we have to be safe wrt. concurrent accesses the group
823 * data. So we need to be careful to set all of the relevant
824 * group descriptor data etc. *before* we enable the group.
826 * The key field here is sbi->s_groups_count: as long as
827 * that retains its old value, nobody is going to access the new
830 * So first we update all the descriptor metadata for the new
831 * group; then we update the total disk blocks count; then we
832 * update the groups count to enable the group; then finally we
833 * update the free space counts so that the system can start
834 * using the new disk blocks.
837 /* Update group descriptor block for new group */
838 gdp
= (struct ext4_group_desc
*)primary
->b_data
+ gdb_off
;
840 ext4_block_bitmap_set(sb
, gdp
, input
->block_bitmap
); /* LV FIXME */
841 ext4_inode_bitmap_set(sb
, gdp
, input
->inode_bitmap
); /* LV FIXME */
842 ext4_inode_table_set(sb
, gdp
, input
->inode_table
); /* LV FIXME */
843 gdp
->bg_free_blocks_count
= cpu_to_le16(input
->free_blocks_count
);
844 gdp
->bg_free_inodes_count
= cpu_to_le16(EXT4_INODES_PER_GROUP(sb
));
847 * Make the new blocks and inodes valid next. We do this before
848 * increasing the group count so that once the group is enabled,
849 * all of its blocks and inodes are already valid.
851 * We always allocate group-by-group, then block-by-block or
852 * inode-by-inode within a group, so enabling these
853 * blocks/inodes before the group is live won't actually let us
854 * allocate the new space yet.
856 ext4_blocks_count_set(es
, ext4_blocks_count(es
) +
857 input
->blocks_count
);
858 es
->s_inodes_count
= cpu_to_le32(le32_to_cpu(es
->s_inodes_count
) +
859 EXT4_INODES_PER_GROUP(sb
));
862 * We need to protect s_groups_count against other CPUs seeing
863 * inconsistent state in the superblock.
865 * The precise rules we use are:
867 * * Writers of s_groups_count *must* hold lock_super
869 * * Writers must perform a smp_wmb() after updating all dependent
870 * data and before modifying the groups count
872 * * Readers must hold lock_super() over the access
874 * * Readers must perform an smp_rmb() after reading the groups count
875 * and before reading any dependent data.
877 * NB. These rules can be relaxed when checking the group count
878 * while freeing data, as we can only allocate from a block
879 * group after serialising against the group count, and we can
880 * only then free after serialising in turn against that
885 /* Update the global fs size fields */
886 sbi
->s_groups_count
++;
888 ext4_journal_dirty_metadata(handle
, primary
);
890 /* Update the reserved block counts only once the new group is
892 ext4_r_blocks_count_set(es
, ext4_r_blocks_count(es
) +
893 input
->reserved_blocks
);
895 /* Update the free space counts */
896 percpu_counter_mod(&sbi
->s_freeblocks_counter
,
897 input
->free_blocks_count
);
898 percpu_counter_mod(&sbi
->s_freeinodes_counter
,
899 EXT4_INODES_PER_GROUP(sb
));
901 ext4_journal_dirty_metadata(handle
, sbi
->s_sbh
);
906 if ((err2
= ext4_journal_stop(handle
)) && !err
)
909 update_backups(sb
, sbi
->s_sbh
->b_blocknr
, (char *)es
,
910 sizeof(struct ext4_super_block
));
911 update_backups(sb
, primary
->b_blocknr
, primary
->b_data
,
917 } /* ext4_group_add */
919 /* Extend the filesystem to the new number of blocks specified. This entry
920 * point is only used to extend the current filesystem to the end of the last
921 * existing group. It can be accessed via ioctl, or by "remount,resize=<size>"
922 * for emergencies (because it has no dependencies on reserved blocks).
924 * If we _really_ wanted, we could use default values to call ext4_group_add()
925 * allow the "remount" trick to work for arbitrary resizing, assuming enough
926 * GDT blocks are reserved to grow to the desired size.
928 int ext4_group_extend(struct super_block
*sb
, struct ext4_super_block
*es
,
929 ext4_fsblk_t n_blocks_count
)
931 ext4_fsblk_t o_blocks_count
;
932 unsigned long o_groups_count
;
935 struct buffer_head
* bh
;
938 unsigned long freed_blocks
;
940 /* We don't need to worry about locking wrt other resizers just
941 * yet: we're going to revalidate es->s_blocks_count after
942 * taking lock_super() below. */
943 o_blocks_count
= ext4_blocks_count(es
);
944 o_groups_count
= EXT4_SB(sb
)->s_groups_count
;
946 if (test_opt(sb
, DEBUG
))
947 printk(KERN_DEBUG
"EXT4-fs: extending last group from %llu uto %llu blocks\n",
948 o_blocks_count
, n_blocks_count
);
950 if (n_blocks_count
== 0 || n_blocks_count
== o_blocks_count
)
953 if (n_blocks_count
> (sector_t
)(~0ULL) >> (sb
->s_blocksize_bits
- 9)) {
954 printk(KERN_ERR
"EXT4-fs: filesystem on %s:"
955 " too large to resize to %llu blocks safely\n",
956 sb
->s_id
, n_blocks_count
);
957 if (sizeof(sector_t
) < 8)
958 ext4_warning(sb
, __FUNCTION__
,
959 "CONFIG_LBD not enabled\n");
963 if (n_blocks_count
< o_blocks_count
) {
964 ext4_warning(sb
, __FUNCTION__
,
965 "can't shrink FS - resize aborted");
969 /* Handle the remaining blocks in the last group only. */
970 ext4_get_group_no_and_offset(sb
, o_blocks_count
, NULL
, &last
);
973 ext4_warning(sb
, __FUNCTION__
,
974 "need to use ext2online to resize further");
978 add
= EXT4_BLOCKS_PER_GROUP(sb
) - last
;
980 if (o_blocks_count
+ add
< o_blocks_count
) {
981 ext4_warning(sb
, __FUNCTION__
, "blocks_count overflow");
985 if (o_blocks_count
+ add
> n_blocks_count
)
986 add
= n_blocks_count
- o_blocks_count
;
988 if (o_blocks_count
+ add
< n_blocks_count
)
989 ext4_warning(sb
, __FUNCTION__
,
990 "will only finish group (%llu"
992 o_blocks_count
+ add
, add
);
994 /* See if the device is actually as big as what was requested */
995 bh
= sb_bread(sb
, o_blocks_count
+ add
-1);
997 ext4_warning(sb
, __FUNCTION__
,
998 "can't read last block, resize aborted");
1003 /* We will update the superblock, one block bitmap, and
1004 * one group descriptor via ext4_free_blocks().
1006 handle
= ext4_journal_start_sb(sb
, 3);
1007 if (IS_ERR(handle
)) {
1008 err
= PTR_ERR(handle
);
1009 ext4_warning(sb
, __FUNCTION__
, "error %d on journal start",err
);
1014 if (o_blocks_count
!= ext4_blocks_count(es
)) {
1015 ext4_warning(sb
, __FUNCTION__
,
1016 "multiple resizers run on filesystem!");
1022 if ((err
= ext4_journal_get_write_access(handle
,
1023 EXT4_SB(sb
)->s_sbh
))) {
1024 ext4_warning(sb
, __FUNCTION__
,
1025 "error %d on journal write access", err
);
1027 ext4_journal_stop(handle
);
1030 ext4_blocks_count_set(es
, o_blocks_count
+ add
);
1031 ext4_journal_dirty_metadata(handle
, EXT4_SB(sb
)->s_sbh
);
1034 ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count
,
1035 o_blocks_count
+ add
);
1036 ext4_free_blocks_sb(handle
, sb
, o_blocks_count
, add
, &freed_blocks
);
1037 ext4_debug("freed blocks %llu through %llu\n", o_blocks_count
,
1038 o_blocks_count
+ add
);
1039 if ((err
= ext4_journal_stop(handle
)))
1041 if (test_opt(sb
, DEBUG
))
1042 printk(KERN_DEBUG
"EXT4-fs: extended group to %llu blocks\n",
1043 ext4_blocks_count(es
));
1044 update_backups(sb
, EXT4_SB(sb
)->s_sbh
->b_blocknr
, (char *)es
,
1045 sizeof(struct ext4_super_block
));
1048 } /* ext4_group_extend */