14 #include <linux/errno.h>
15 #include <linux/slab.h>
31 ext4_warning(sb,
"There are errors in the filesystem, "
32 "so online resizing is not allowed\n");
50 return (group >> EXT4_DESC_PER_BLOCK_BITS(sb)) <<
51 EXT4_DESC_PER_BLOCK_BITS(sb);
56 group = ext4_meta_bg_first_group(sb, group);
57 return ext4_group_first_block_no(sb, group);
70 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
71 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
73 static int verify_group_input(
struct super_block *sb,
76 struct ext4_sb_info *sbi =
EXT4_SB(sb);
82 unsigned overhead = ext4_group_overhead_blocks(sb, group);
84 struct buffer_head *bh =
NULL;
89 input->
blocks_count - 2 - overhead - sbi->s_itb_per_group;
93 "(%d free, %u reserved)\n",
99 if (group != sbi->s_groups_count)
100 ext4_warning(sb,
"Cannot add at group %u (only %u groups)",
101 input->
group, sbi->s_groups_count);
102 else if (offset != 0)
103 ext4_warning(sb,
"Last group not full");
105 ext4_warning(sb,
"Reserved blocks too high (%u)",
107 else if (free_blocks_count < 0)
108 ext4_warning(sb,
"Bad blocks count %u",
110 else if (!(bh = sb_bread(sb, end - 1)))
111 ext4_warning(sb,
"Cannot read last block (%llu)",
114 ext4_warning(sb,
"Block bitmap not in group (block %llu)",
117 ext4_warning(sb,
"Inode bitmap not in group (block %llu)",
120 outside(itend - 1, start, end))
121 ext4_warning(sb,
"Inode table not in group (blocks %llu-%llu)",
122 (
unsigned long long)input->
inode_table, itend - 1);
124 ext4_warning(sb,
"Block bitmap same as inode bitmap (%llu)",
127 ext4_warning(sb,
"Block bitmap (%llu) in inode table "
130 (
unsigned long long)input->
inode_table, itend - 1);
132 ext4_warning(sb,
"Inode bitmap (%llu) in inode table "
135 (
unsigned long long)input->
inode_table, itend - 1);
137 ext4_warning(sb,
"Block bitmap (%llu) in GDT table (%llu-%llu)",
141 ext4_warning(sb,
"Inode bitmap (%llu) in GDT table (%llu-%llu)",
145 inside(itend - 1, start, metaend))
146 ext4_warning(sb,
"Inode table (%llu-%llu) overlaps GDT table "
149 itend - 1, start, metaend - 1);
186 flex_gd->
count = flexbg_size;
227 static int ext4_alloc_group_tables(
struct super_block *sb,
244 src_group = group_data[0].
group;
245 last_group = src_group + flex_gd->
count - 1;
247 BUG_ON((flexbg_size > 1) && ((src_group & ~(flexbg_size - 1)) !=
248 (last_group & ~(flexbg_size - 1))));
250 group = group_data[0].
group;
251 if (src_group >= group_data[0].group + flex_gd->
count)
253 start_blk = ext4_group_first_block_no(sb, src_group);
256 overhead = ext4_group_overhead_blocks(sb, src_group);
258 start_blk += overhead;
262 for (; src_group <= last_group; src_group++) {
263 overhead = ext4_group_overhead_blocks(sb, src_group);
271 for (; bb_index < flex_gd->
count; bb_index++) {
272 if (start_blk >= last_blk)
276 group -= group_data[0].
group;
283 for (; ib_index < flex_gd->
count; ib_index++) {
284 if (start_blk >= last_blk)
288 group -= group_data[0].
group;
295 for (; it_index < flex_gd->
count; it_index++) {
296 if (start_blk +
EXT4_SB(sb)->s_itb_per_group > last_blk)
300 group -= group_data[0].
group;
306 start_blk +=
EXT4_SB(sb)->s_itb_per_group;
311 group = group_data[0].
group;
314 "%d groups, flexbg size is %d:\n", flex_gd->
count,
317 for (i = 0; i < flex_gd->
count; i++) {
319 "blocks (%d free)\n",
321 "no-super", group + i,
332 struct buffer_head *bh;
335 bh = sb_getblk(sb, blk);
337 return ERR_PTR(-
EIO);
343 set_buffer_uptodate(bh);
354 static int extend_or_restart_transaction(handle_t *handle,
int thresh)
358 if (ext4_handle_has_enough_credits(handle, thresh))
382 static int set_flexbg_block_bitmap(
struct super_block *sb, handle_t *handle,
388 ext4_debug(
"mark blocks [%llu/%u] used\n", block, count);
389 for (count2 = count; count > 0; count -=
count2, block +=
count2) {
391 struct buffer_head *bh;
396 start = ext4_group_first_block_no(sb, group);
397 group -= flex_gd->
groups[0].group;
408 err = extend_or_restart_transaction(handle, 1);
412 bh = sb_getblk(sb, flex_gd->
groups[group].block_bitmap);
419 ext4_debug(
"mark block bitmap %#04llx (+%llu/%u)\n", block,
420 block - start, count2);
446 static int setup_new_flex_group_blocks(
struct super_block *sb,
449 int group_table_count[] = {1, 1,
EXT4_SB(sb)->s_itb_per_group};
452 struct ext4_sb_info *sbi =
EXT4_SB(sb);
458 struct buffer_head *bh =
NULL;
459 int reserved_gdb,
i,
j, err = 0, err2;
463 group_data[0].
group != sbi->s_groups_count);
471 return PTR_ERR(handle);
473 group = group_data[0].
group;
474 for (i = 0; i < flex_gd->
count; i++, group++) {
475 unsigned long gdblocks;
479 start = ext4_group_first_block_no(sb, group);
486 first_group = ext4_meta_bg_first_group(sb, group);
487 if (first_group != group + 1 &&
494 for (j = 0; j < gdblocks; j++, block++) {
495 struct buffer_head *gdb;
497 ext4_debug(
"update backup group %#04llx\n", block);
498 err = extend_or_restart_transaction(handle, 1);
502 gdb = sb_getblk(sb, block);
513 memcpy(gdb->b_data, sbi->s_group_desc[j]->b_data,
515 set_buffer_uptodate(gdb);
529 err = sb_issue_zeroout(sb, gdblocks + start + 1,
542 ext4_debug(
"clear inode table blocks %#04llx -> %#04lx\n",
543 block, sbi->s_itb_per_group);
544 err = sb_issue_zeroout(sb, block, sbi->s_itb_per_group,
550 if (bg_flags[i] & EXT4_BG_BLOCK_UNINIT)
555 err = extend_or_restart_transaction(handle, 1);
559 bh = bclean(handle, sb, block);
564 overhead = ext4_group_overhead_blocks(sb, group);
566 ext4_debug(
"mark backup superblock %#04llx (+0)\n",
583 err = extend_or_restart_transaction(handle, 1);
587 bh = bclean(handle, sb, block);
604 count = group_table_count[
j];
607 for (i = 1; i < flex_gd->
count; i++) {
608 block += group_table_count[
j];
609 if (block == (&group_data[i].block_bitmap)[
j]) {
610 count += group_table_count[
j];
613 err = set_flexbg_block_bitmap(sb, handle,
614 flex_gd, start, count);
617 count = group_table_count[
j];
623 err = set_flexbg_block_bitmap(sb, handle,
624 flex_gd, start, count);
646 static unsigned ext4_list_backups(
struct super_block *sb,
unsigned *three,
647 unsigned *five,
unsigned *seven)
649 unsigned *
min = three;
680 static int verify_reserved_gdb(
struct super_block *sb,
692 while ((grp = ext4_list_backups(sb, &three, &five, &seven)) <
end) {
695 ext4_warning(sb,
"reserved GDT %llu"
696 " missing grp %d (%llu)",
723 static int add_new_gdb(handle_t *handle,
struct inode *
inode,
730 struct buffer_head **o_group_desc, **n_group_desc;
731 struct buffer_head *dind;
732 struct buffer_head *gdb_bh;
734 struct ext4_iloc iloc;
740 "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
748 if (
EXT4_SB(sb)->s_sbh->b_blocknr !=
750 ext4_warning(sb,
"won't resize using backup superblock at %llu",
751 (
unsigned long long)
EXT4_SB(sb)->s_sbh->b_blocknr);
755 gdb_bh = sb_bread(sb, gdblock);
759 gdbackups = verify_reserved_gdb(sb, group, gdb_bh);
772 data = (
__le32 *)dind->b_data;
774 ext4_warning(sb,
"new group %u GDT block %llu not reserved",
790 ext4_std_error(sb, err);
798 sizeof(
struct buffer_head *),
802 ext4_warning(sb,
"not enough memory for %lu groups",
819 ext4_std_error(sb, err);
827 ext4_std_error(sb, err);
832 o_group_desc =
EXT4_SB(sb)->s_group_desc;
833 memcpy(n_group_desc, o_group_desc,
834 EXT4_SB(sb)->s_gdb_count *
sizeof(
struct buffer_head *));
835 n_group_desc[gdb_num] = gdb_bh;
836 EXT4_SB(sb)->s_group_desc = n_group_desc;
843 ext4_std_error(sb, err);
867 static int add_new_gdb_meta_bg(
struct super_block *sb,
870 struct buffer_head *gdb_bh;
871 struct buffer_head **o_group_desc, **n_group_desc;
875 gdblock = ext4_meta_bg_first_block_no(sb, group) +
877 gdb_bh = sb_bread(sb, gdblock);
881 sizeof(
struct buffer_head *),
885 ext4_warning(sb,
"not enough memory for %lu groups",
890 o_group_desc =
EXT4_SB(sb)->s_group_desc;
891 memcpy(n_group_desc, o_group_desc,
892 EXT4_SB(sb)->s_gdb_count *
sizeof(
struct buffer_head *));
893 n_group_desc[gdb_num] = gdb_bh;
894 EXT4_SB(sb)->s_group_desc = n_group_desc;
916 static int reserve_backup_gdb(handle_t *handle,
struct inode *inode,
922 struct buffer_head *dind;
923 struct ext4_iloc iloc;
941 blk =
EXT4_SB(sb)->s_sbh->b_blocknr + 1 +
EXT4_SB(sb)->s_gdb_count;
947 for (res = 0; res < reserved_gdb; res++, blk++) {
949 ext4_warning(sb,
"reserved block %llu"
950 " not at offset %ld",
952 (
long)(data - (
__le32 *)dind->b_data));
956 primary[
res] = sb_bread(sb, blk);
961 gdbackups = verify_reserved_gdb(sb, group, primary[res]);
963 brelse(primary[res]);
968 data = (
__le32 *)dind->b_data;
971 for (i = 0; i < reserved_gdb; i++) {
990 for (i = 0; i < reserved_gdb; i++) {
992 data = (
__le32 *)primary[i]->b_data;
996 data[gdbackups] =
cpu_to_le32(blk + primary[i]->b_blocknr);
1006 brelse(primary[res]);
1031 static void update_backups(
struct super_block *sb,
int blk_off,
char *data,
1032 int size,
int meta_bg)
1034 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1046 if (IS_ERR(handle)) {
1048 err = PTR_ERR(handle);
1053 group = ext4_list_backups(sb, &three, &five, &seven);
1054 last = sbi->s_groups_count;
1056 group = ext4_meta_bg_first_group(sb, group) + 1;
1060 while (group < sbi->s_groups_count) {
1061 struct buffer_head *bh;
1065 if (ext4_handle_valid(handle) &&
1066 handle->h_buffer_credits == 0 &&
1072 backup_block = group * bpg + blk_off;
1074 backup_block = (ext4_group_first_block_no(sb, group) +
1077 bh = sb_getblk(sb, backup_block);
1082 ext4_debug(
"update metadata backup %llu(+%llu)\n",
1083 backup_block, backup_block -
1084 ext4_group_first_block_no(sb, group));
1088 memcpy(bh->b_data, data, size);
1090 memset(bh->b_data + size, 0, rest);
1091 set_buffer_uptodate(bh);
1095 ext4_std_error(sb, err);
1099 group = ext4_list_backups(sb, &three, &five, &seven);
1100 else if (group == last)
1120 ext4_warning(sb,
"can't update backup for group %u (err %d), "
1121 "forcing fsck on next reboot", group, err);
1138 static int ext4_add_new_descs(handle_t *handle,
struct super_block *sb,
1142 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1144 struct buffer_head *gdb_bh;
1145 int i, gdb_off, gdb_num, err = 0;
1149 for (i = 0; i <
count; i++, group++) {
1163 gdb_bh = sbi->s_group_desc[gdb_num];
1167 err = reserve_backup_gdb(handle, resize_inode, group);
1168 }
else if (meta_bg != 0) {
1169 err = add_new_gdb_meta_bg(sb, handle, group);
1171 err = add_new_gdb(handle, resize_inode, group);
1179 static struct buffer_head *ext4_get_bitmap(
struct super_block *sb,
__u64 block)
1181 struct buffer_head *bh = sb_getblk(sb, block);
1194 static int ext4_set_bitmap_checksums(
struct super_block *sb,
1199 struct buffer_head *bh;
1224 static int ext4_setup_new_descs(handle_t *handle,
struct super_block *sb,
1229 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1230 struct buffer_head *gdb_bh;
1233 int i, gdb_off, gdb_num, err = 0;
1236 for (i = 0; i < flex_gd->
count; i++, group_data++, bg_flags++) {
1237 group = group_data->group;
1245 gdb_bh = sbi->s_group_desc[gdb_num];
1253 err = ext4_set_bitmap_checksums(sb, group, gdp, group_data);
1255 ext4_std_error(sb, err);
1261 EXT4_B2C(sbi, group_data->free_blocks_count));
1263 if (ext4_has_group_desc_csum(sb))
1271 ext4_std_error(sb, err);
1293 static void ext4_update_super(
struct super_block *sb,
1300 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1315 for (i = 0; i < flex_gd->
count; i++) {
1320 reserved_blocks = ext4_r_blocks_count(es) * 100;
1321 reserved_blocks = div64_u64(reserved_blocks, ext4_blocks_count(es));
1322 reserved_blocks *= blocks_count;
1323 do_div(reserved_blocks, 100);
1325 ext4_blocks_count_set(es, ext4_blocks_count(es) + blocks_count);
1326 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + free_blocks);
1332 ext4_debug(
"free blocks count %llu", ext4_free_blocks_count(es));
1354 sbi->s_groups_count += flex_gd->
count;
1358 ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
1362 percpu_counter_add(&sbi->s_freeclusters_counter,
1364 percpu_counter_add(&sbi->s_freeinodes_counter,
1368 percpu_counter_read(&sbi->s_freeclusters_counter));
1371 sbi->s_log_groups_per_flex) {
1373 flex_group = ext4_flex_group(sbi, group_data[0].group);
1375 &sbi->s_flex_groups[flex_group].free_clusters);
1377 &sbi->s_flex_groups[flex_group].free_inodes);
1387 "%llu blocks(%llu free %llu reserved)\n", flex_gd->
count,
1388 blocks_count, free_blocks, reserved_blocks);
1395 static int ext4_flex_group_add(
struct super_block *sb,
1396 struct inode *resize_inode,
1399 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1405 unsigned reserved_gdb;
1406 int err = 0, err2 = 0, credit;
1411 o_blocks_count = ext4_blocks_count(es);
1415 err = setup_new_flex_group_blocks(sb, flex_gd);
1425 credit = flex_gd->
count * 4 + reserved_gdb;
1427 if (IS_ERR(handle)) {
1428 err = PTR_ERR(handle);
1436 group = flex_gd->
groups[0].group;
1438 err = ext4_add_new_descs(handle, sb, group,
1439 resize_inode, flex_gd->
count);
1443 err = ext4_setup_new_descs(handle, sb, flex_gd);
1447 ext4_update_super(sb, flex_gd);
1458 int gdb_num_end = ((group + flex_gd->
count - 1) /
1464 update_backups(sb, sbi->s_sbh->b_blocknr, (
char *)es,
1466 for (; gdb_num <= gdb_num_end; gdb_num++) {
1467 struct buffer_head *gdb_bh;
1469 gdb_bh = sbi->s_group_desc[gdb_num];
1470 if (old_gdb == gdb_bh->b_blocknr)
1472 update_backups(sb, gdb_bh->b_blocknr, gdb_bh->b_data,
1473 gdb_bh->b_size, meta_bg);
1474 old_gdb = gdb_bh->b_blocknr;
1481 static int ext4_setup_next_flex_gd(
struct super_block *sb,
1484 unsigned long flexbg_size)
1498 o_blocks_count = ext4_blocks_count(es);
1500 if (o_blocks_count == n_blocks_count)
1507 last_group = group | (flexbg_size - 1);
1508 if (last_group > n_group)
1509 last_group = n_group;
1511 flex_gd->
count = last_group - group + 1;
1513 for (i = 0; i < flex_gd->
count; i++) {
1516 group_data[
i].
group = group +
i;
1518 overhead = ext4_group_overhead_blocks(sb, group + i);
1520 if (ext4_has_group_desc_csum(sb))
1521 flex_gd->
bg_flags[
i] = EXT4_BG_BLOCK_UNINIT |
1527 if (last_group == n_group && ext4_has_group_desc_csum(sb))
1529 flex_gd->
bg_flags[i - 1] &= ~EXT4_BG_BLOCK_UNINIT;
1531 if ((last_group == n_group) && (last != blocks_per_group - 1)) {
1556 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1560 struct inode *inode =
NULL;
1561 int gdb_off, gdb_num;
1570 ext4_warning(sb,
"Can't resize non-sparse filesystem further");
1575 ext4_blocks_count(es)) {
1576 ext4_warning(sb,
"blocks_count overflow");
1582 ext4_warning(sb,
"inodes_count overflow");
1586 if (reserved_gdb || gdb_off == 0) {
1591 "No reserved GDT blocks, can't resize");
1595 if (IS_ERR(inode)) {
1596 ext4_warning(sb,
"Error opening resize inode");
1597 return PTR_ERR(inode);
1602 err = verify_group_input(sb, input);
1617 err = ext4_flex_group_add(sb, inode, &flex_gd);
1626 static int ext4_group_extend_no_check(
struct super_block *sb,
1637 if (IS_ERR(handle)) {
1638 err = PTR_ERR(handle);
1639 ext4_warning(sb,
"error %d on journal start", err);
1645 ext4_warning(sb,
"error %d on journal write access", err);
1649 ext4_blocks_count_set(es, o_blocks_count + add);
1650 ext4_free_blocks_count_set(es, ext4_free_blocks_count(es) + add);
1651 ext4_debug(
"freeing blocks %llu through %llu\n", o_blocks_count,
1652 o_blocks_count + add);
1658 ext4_debug(
"freed blocks %llu through %llu\n", o_blocks_count,
1659 o_blocks_count + add);
1667 first_block = ext4_group_first_block_no(sb, 0);
1670 "blocks\n", ext4_blocks_count(es));
1671 update_backups(sb,
EXT4_SB(sb)->s_sbh->b_blocknr - first_block,
1693 struct buffer_head *bh;
1697 o_blocks_count = ext4_blocks_count(es);
1701 "extending last group from %llu to %llu blocks",
1702 o_blocks_count, n_blocks_count);
1704 if (n_blocks_count == 0 || n_blocks_count == o_blocks_count)
1709 "filesystem too large to resize to %llu blocks safely",
1712 ext4_warning(sb,
"CONFIG_LBDAF not enabled");
1716 if (n_blocks_count < o_blocks_count) {
1717 ext4_warning(sb,
"can't shrink FS - resize aborted");
1725 ext4_warning(sb,
"need to use ext2online to resize further");
1731 if (o_blocks_count + add < o_blocks_count) {
1732 ext4_warning(sb,
"blocks_count overflow");
1736 if (o_blocks_count + add > n_blocks_count)
1737 add = n_blocks_count - o_blocks_count;
1739 if (o_blocks_count + add < n_blocks_count)
1740 ext4_warning(sb,
"will only finish group (%llu blocks, %u new)",
1741 o_blocks_count + add, add);
1744 bh = sb_bread(sb, o_blocks_count + add - 1);
1746 ext4_warning(sb,
"can't read last block, resize aborted");
1751 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1766 static int ext4_convert_meta_bg(
struct super_block *sb,
struct inode *inode)
1769 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1773 int i,
ret, err = 0;
1779 ext4_error(sb,
"Unexpected non-zero "
1780 "s_reserved_gdt_blocks");
1786 goto invalid_resize_inode;
1792 goto invalid_resize_inode;
1795 goto invalid_resize_inode;
1802 return PTR_ERR(handle);
1810 sbi->s_es->s_first_meta_bg =
1811 cpu_to_le32(num_desc_blocks(sb, sbi->s_groups_count));
1815 ext4_std_error(sb, err);
1829 ext4_std_error(sb, err);
1838 invalid_resize_inode:
1839 ext4_error(sb,
"corrupted/inconsistent resize inode");
1852 struct ext4_sb_info *sbi =
EXT4_SB(sb);
1854 struct buffer_head *bh;
1855 struct inode *resize_inode =
NULL;
1857 unsigned long n_desc_blocks;
1858 unsigned long o_desc_blocks;
1863 unsigned long last_update_time = 0;
1864 int err = 0, flexbg_size = 1 << sbi->s_log_groups_per_flex;
1868 bh = sb_bread(sb, n_blocks_count - 1);
1870 ext4_warning(sb,
"can't read last block, resize aborted");
1876 o_blocks_count = ext4_blocks_count(es);
1879 "to %llu blocks", o_blocks_count, n_blocks_count);
1881 if (n_blocks_count < o_blocks_count) {
1883 ext4_warning(sb,
"can't shrink FS - resize aborted");
1887 if (n_blocks_count == o_blocks_count)
1894 n_desc_blocks = num_desc_blocks(sb, n_group + 1);
1895 o_desc_blocks = num_desc_blocks(sb, sbi->s_groups_count);
1901 ext4_error(sb,
"resize_inode and meta_bg enabled "
1905 if (n_desc_blocks > o_desc_blocks +
1907 n_blocks_count_retry = n_blocks_count;
1908 n_desc_blocks = o_desc_blocks +
1917 if (IS_ERR(resize_inode)) {
1918 ext4_warning(sb,
"Error opening resize inode");
1919 return PTR_ERR(resize_inode);
1923 if ((!resize_inode && !meta_bg) || n_blocks_count == o_blocks_count) {
1924 err = ext4_convert_meta_bg(sb, resize_inode);
1929 resize_inode =
NULL;
1931 if (n_blocks_count_retry) {
1932 n_blocks_count = n_blocks_count_retry;
1933 n_blocks_count_retry = 0;
1939 if (n_group == o_group)
1940 add = n_blocks_count - o_blocks_count;
1944 err = ext4_group_extend_no_check(sb, o_blocks_count, add);
1949 if (ext4_blocks_count(es) == n_blocks_count)
1960 flex_gd = alloc_flex_gd(flexbg_size);
1961 if (flex_gd ==
NULL) {
1969 while (ext4_setup_next_flex_gd(sb, flex_gd, n_blocks_count,
1971 if (
jiffies - last_update_time >
HZ * 10) {
1972 if (last_update_time)
1974 "resized to %llu blocks",
1975 ext4_blocks_count(es));
1978 if (ext4_alloc_group_tables(sb, flex_gd, flexbg_size) != 0)
1980 err = ext4_flex_group_add(sb, resize_inode, flex_gd);
1985 if (!err && n_blocks_count_retry) {
1986 n_blocks_count = n_blocks_count_retry;
1987 n_blocks_count_retry = 0;
1988 free_flex_gd(flex_gd);
1995 free_flex_gd(flex_gd);
1996 if (resize_inode !=
NULL)