From: Ana Bobillo <anabobillo3@gmail.com>
To: Tom Rini <trini@konsulko.com>
Cc: u-boot@lists.u-boot-project.org, Tony Dinh <mibodhi@gmail.com>,
Simon Glass <sjg@chromium.org>
Subject: [PATCH 2/6] fs: ext4: use ext4fs_blk_to_group() in ext4fs_get_new_blk_no()
Date: Sun, 08 Nov 2026 15:12:04 +0000 [thread overview]
Message-ID: <eb534601-abb7-4912-be31-a9ce05ec7be3@gmail.com> (raw)
In-Reply-To: <5072bec2-e2e3-405d-91eb-58fe78ea6669@gmail.com>
From: Ana Bobillo <anabobillo3@gmail.com>
ext4fs_get_new_blk_no() works out the block group of the next block to
allocate with its own arithmetic, which takes the first data block to
be block 1 with 1 KiB blocks and block 0 otherwise. Now that
ext4fs_init() refuses any other first data block, ext4fs_blk_to_group()
gives the same group, so use it, and step to the first block of the next
group from the first data block too.
Running past the last group means here that the filesystem is full, not
that a block number is corrupt, so first check quietly that the block
lies inside the filesystem, as ext4fs_get_new_inode_no() does for
inodes, and stop rather than skip past a full last group. The search for
the next block then ends at the end of the filesystem rather than at the
end of the last group, so it no longer takes a block past the end when
the padding bits of the last group's bitmap are clear.
Suggested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Ana Bobillo <anabobillo3@gmail.com>
---
fs/ext4/ext4_common.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c
index 273e6628b8d..ccfce5c745c 100644
--- a/fs/ext4/ext4_common.c
+++ b/fs/ext4/ext4_common.c
@@ -1037,10 +1037,11 @@ uint32_t ext4fs_get_new_blk_no(void)
{
short i;
short status;
- int remainder;
- unsigned int bg_idx;
+ int bg_idx;
static int prev_bg_bitmap_index = -1;
unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
+ uint32_t first_data_block =
+ le32_to_cpu(ext4fs_root->sblock.first_data_block);
struct ext_filesystem *fs = get_fs();
char *journal_buffer = zalloc(fs->blksz);
char *zero_buffer = zalloc(fs->blksz);
@@ -1092,28 +1093,27 @@ uint32_t ext4fs_get_new_blk_no(void)
} else {
fs->curr_blkno++;
restart:
+ /* no free block left after the last one allocated */
+ if (fs->curr_blkno >=
+ le32_to_cpu(ext4fs_root->sblock.total_blocks))
+ goto fail;
/* get the blockbitmap index respective to blockno */
- bg_idx = fs->curr_blkno / blk_per_grp;
- if (fs->blksz == 1024) {
- remainder = fs->curr_blkno % blk_per_grp;
- if (!remainder)
- bg_idx--;
- }
+ bg_idx = ext4fs_blk_to_group(fs, fs->curr_blkno);
+ if (bg_idx < 0)
+ goto fail;
/*
* To skip completely filled block group bitmaps
* Optimize the block allocation
*/
- if (bg_idx >= fs->no_blkgrp)
- goto fail;
-
struct ext2_block_group *bgd = NULL;
bgd = ext4fs_get_group_descriptor(fs, bg_idx);
if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
- debug("block group %u is full. Skipping\n", bg_idx);
- fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
- if (fs->blksz == 1024)
- fs->curr_blkno += 1;
+ debug("block group %d is full. Skipping\n", bg_idx);
+ if (bg_idx + 1 >= fs->no_blkgrp)
+ goto fail;
+ fs->curr_blkno = (bg_idx + 1) * blk_per_grp +
+ first_data_block;
goto restart;
}
@@ -1129,7 +1129,7 @@ restart:
if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
bg_idx) != 0) {
- debug("going for restart for the block no %ld %u\n",
+ debug("going for restart for the block no %ld %d\n",
fs->curr_blkno, bg_idx);
fs->curr_blkno++;
goto restart;
next prev parent reply other threads:[~2026-11-08 15:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 15:11 [PATCH 0/6] fs: ext4: check on-disk values in the write path Ana Bobillo
2026-11-08 15:11 ` [PATCH 1/6] fs: ext4: refuse to write with an unexpected first data block Ana Bobillo
2026-11-08 15:12 ` Ana Bobillo [this message]
2026-11-08 15:12 ` [PATCH 3/6] fs: ext4: check directory entries before using them Ana Bobillo
2026-11-08 15:12 ` [PATCH 4/6] fs: ext4: bound the journal entry tables Ana Bobillo
2026-11-08 15:12 ` [PATCH 5/6] fs: ext4: stop the journal scan at the end of the log Ana Bobillo
2026-11-08 15:12 ` [PATCH 6/6] test: fs: ext4: add tests for the write path checks Ana Bobillo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eb534601-abb7-4912-be31-a9ce05ec7be3@gmail.com \
--to=anabobillo3@gmail.com \
--cc=mibodhi@gmail.com \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.u-boot-project.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.