hc
2024-05-10 10ebd8556b7990499c896a550e3d416b444211e6
kernel/fs/ext4/resize.c
....@@ -53,6 +53,16 @@
5353 return -EPERM;
5454
5555 /*
56
+ * If the reserved GDT blocks is non-zero, the resize_inode feature
57
+ * should always be set.
58
+ */
59
+ if (EXT4_SB(sb)->s_es->s_reserved_gdt_blocks &&
60
+ !ext4_has_feature_resize_inode(sb)) {
61
+ ext4_error(sb, "resize_inode disabled but reserved GDT blocks non-zero");
62
+ return -EFSCORRUPTED;
63
+ }
64
+
65
+ /*
5666 * If we are not using the primary superblock/GDT copy don't resize,
5767 * because the user tools have no way of handling this. Probably a
5868 * bad time to do it anyways.
....@@ -72,6 +82,11 @@
7282 ext4_warning(sb, "There are errors in the filesystem, "
7383 "so online resizing is not allowed");
7484 return -EPERM;
85
+ }
86
+
87
+ if (ext4_has_feature_sparse_super2(sb)) {
88
+ ext4_msg(sb, KERN_ERR, "Online resizing not supported with sparse_super2");
89
+ return -EOPNOTSUPP;
7590 }
7691
7792 if (test_and_set_bit_lock(EXT4_FLAGS_RESIZING,
....@@ -415,28 +430,10 @@
415430 return bh;
416431 }
417432
418
-/*
419
- * If we have fewer than thresh credits, extend by EXT4_MAX_TRANS_DATA.
420
- * If that fails, restart the transaction & regain write access for the
421
- * buffer head which is used for block_bitmap modifications.
422
- */
423
-static int extend_or_restart_transaction(handle_t *handle, int thresh)
433
+static int ext4_resize_ensure_credits_batch(handle_t *handle, int credits)
424434 {
425
- int err;
426
-
427
- if (ext4_handle_has_enough_credits(handle, thresh))
428
- return 0;
429
-
430
- err = ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA);
431
- if (err < 0)
432
- return err;
433
- if (err) {
434
- err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA);
435
- if (err)
436
- return err;
437
- }
438
-
439
- return 0;
435
+ return ext4_journal_ensure_credits_fn(handle, credits,
436
+ EXT4_MAX_TRANS_DATA, 0, 0);
440437 }
441438
442439 /*
....@@ -478,8 +475,8 @@
478475 continue;
479476 }
480477
481
- err = extend_or_restart_transaction(handle, 1);
482
- if (err)
478
+ err = ext4_resize_ensure_credits_batch(handle, 1);
479
+ if (err < 0)
483480 return err;
484481
485482 bh = sb_getblk(sb, flex_gd->groups[group].block_bitmap);
....@@ -571,8 +568,8 @@
571568 struct buffer_head *gdb;
572569
573570 ext4_debug("update backup group %#04llx\n", block);
574
- err = extend_or_restart_transaction(handle, 1);
575
- if (err)
571
+ err = ext4_resize_ensure_credits_batch(handle, 1);
572
+ if (err < 0)
576573 goto out;
577574
578575 gdb = sb_getblk(sb, block);
....@@ -629,8 +626,8 @@
629626
630627 /* Initialize block bitmap of the @group */
631628 block = group_data[i].block_bitmap;
632
- err = extend_or_restart_transaction(handle, 1);
633
- if (err)
629
+ err = ext4_resize_ensure_credits_batch(handle, 1);
630
+ if (err < 0)
634631 goto out;
635632
636633 bh = bclean(handle, sb, block);
....@@ -658,8 +655,8 @@
658655
659656 /* Initialize inode bitmap of the @group */
660657 block = group_data[i].inode_bitmap;
661
- err = extend_or_restart_transaction(handle, 1);
662
- if (err)
658
+ err = ext4_resize_ensure_credits_batch(handle, 1);
659
+ if (err < 0)
663660 goto out;
664661 /* Mark unused entries in inode bitmap used */
665662 bh = bclean(handle, sb, block);
....@@ -871,9 +868,8 @@
871868 if (unlikely(err))
872869 goto errout;
873870
874
- n_group_desc = ext4_kvmalloc((gdb_num + 1) *
875
- sizeof(struct buffer_head *),
876
- GFP_NOFS);
871
+ n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
872
+ GFP_KERNEL);
877873 if (!n_group_desc) {
878874 err = -ENOMEM;
879875 ext4_warning(sb, "not enough memory for %lu groups",
....@@ -949,9 +945,8 @@
949945 gdb_bh = ext4_sb_bread(sb, gdblock, 0);
950946 if (IS_ERR(gdb_bh))
951947 return PTR_ERR(gdb_bh);
952
- n_group_desc = ext4_kvmalloc((gdb_num + 1) *
953
- sizeof(struct buffer_head *),
954
- GFP_NOFS);
948
+ n_group_desc = kvmalloc((gdb_num + 1) * sizeof(struct buffer_head *),
949
+ GFP_KERNEL);
955950 if (!n_group_desc) {
956951 brelse(gdb_bh);
957952 err = -ENOMEM;
....@@ -1142,10 +1137,8 @@
11421137 ext4_fsblk_t backup_block;
11431138
11441139 /* Out of journal space, and can't get more - abort - so sad */
1145
- if (ext4_handle_valid(handle) &&
1146
- handle->h_buffer_credits == 0 &&
1147
- ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
1148
- (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
1140
+ err = ext4_resize_ensure_credits_batch(handle, 1);
1141
+ if (err < 0)
11491142 break;
11501143
11511144 if (meta_bg == 0)
....@@ -1267,7 +1260,7 @@
12671260 if (unlikely(!bh))
12681261 return NULL;
12691262 if (!bh_uptodate_or_lock(bh)) {
1270
- if (bh_submit_read(bh) < 0) {
1263
+ if (ext4_read_bh(bh, 0, NULL) < 0) {
12711264 brelse(bh);
12721265 return NULL;
12731266 }
....@@ -1468,6 +1461,7 @@
14681461 * Update the fs overhead information
14691462 */
14701463 ext4_calculate_overhead(sb);
1464
+ es->s_overhead_clusters = cpu_to_le32(sbi->s_overhead);
14711465
14721466 if (test_opt(sb, DEBUG))
14731467 printk(KERN_DEBUG "EXT4-fs: added group %u:"
....@@ -1551,8 +1545,8 @@
15511545 int meta_bg = ext4_has_feature_meta_bg(sb);
15521546 sector_t old_gdb = 0;
15531547
1554
- update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
1555
- sizeof(struct ext4_super_block), 0);
1548
+ update_backups(sb, ext4_group_first_block_no(sb, 0),
1549
+ (char *)es, sizeof(struct ext4_super_block), 0);
15561550 for (; gdb_num <= gdb_num_end; gdb_num++) {
15571551 struct buffer_head *gdb_bh;
15581552
....@@ -1759,7 +1753,7 @@
17591753 if (test_opt(sb, DEBUG))
17601754 printk(KERN_DEBUG "EXT4-fs: extended group to %llu "
17611755 "blocks\n", ext4_blocks_count(es));
1762
- update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr,
1756
+ update_backups(sb, ext4_group_first_block_no(sb, 0),
17631757 (char *)es, sizeof(struct ext4_super_block), 0);
17641758 }
17651759 return err;
....@@ -1799,8 +1793,6 @@
17991793 ext4_msg(sb, KERN_ERR,
18001794 "filesystem too large to resize to %llu blocks safely",
18011795 n_blocks_count);
1802
- if (sizeof(sector_t) < 8)
1803
- ext4_warning(sb, "CONFIG_LBDAF not enabled");
18041796 return -EINVAL;
18051797 }
18061798
....@@ -1832,8 +1824,8 @@
18321824 o_blocks_count + add, add);
18331825
18341826 /* See if the device is actually as big as what was requested */
1835
- bh = sb_bread(sb, o_blocks_count + add - 1);
1836
- if (!bh) {
1827
+ bh = ext4_sb_bread(sb, o_blocks_count + add - 1, 0);
1828
+ if (IS_ERR(bh)) {
18371829 ext4_warning(sb, "can't read last block, resize aborted");
18381830 return -ENOSPC;
18391831 }
....@@ -1958,12 +1950,22 @@
19581950 int meta_bg;
19591951
19601952 /* See if the device is actually as big as what was requested */
1961
- bh = sb_bread(sb, n_blocks_count - 1);
1962
- if (!bh) {
1953
+ bh = ext4_sb_bread(sb, n_blocks_count - 1, 0);
1954
+ if (IS_ERR(bh)) {
19631955 ext4_warning(sb, "can't read last block, resize aborted");
19641956 return -ENOSPC;
19651957 }
19661958 brelse(bh);
1959
+
1960
+ /*
1961
+ * For bigalloc, trim the requested size to the nearest cluster
1962
+ * boundary to avoid creating an unusable filesystem. We do this
1963
+ * silently, instead of returning an error, to avoid breaking
1964
+ * callers that blindly resize the filesystem to the full size of
1965
+ * the underlying block device.
1966
+ */
1967
+ if (ext4_has_feature_bigalloc(sb))
1968
+ n_blocks_count &= ~((1 << EXT4_CLUSTER_BITS(sb)) - 1);
19671969
19681970 retry:
19691971 o_blocks_count = ext4_blocks_count(es);
....@@ -2066,7 +2068,7 @@
20662068 goto out;
20672069 }
20682070
2069
- if (ext4_blocks_count(es) == n_blocks_count)
2071
+ if (ext4_blocks_count(es) == n_blocks_count && n_blocks_count_retry == 0)
20702072 goto out;
20712073
20722074 err = ext4_alloc_flex_bg_array(sb, n_group + 1);