summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Brüns <stefan.bruens@rwth-aachen.de>2016-09-17 03:10:10 +0300
committerDongjin Kim <tobetter@gmail.com>2020-02-10 16:44:41 +0300
commit4a68406f3a3f3a1b57af59074d1139f3e020d090 (patch)
tree9a6197f4ee224d7ffaf994b1b6cfb6764c892ed8
parent17e782965819aba698e60473d49755aa74bce0c7 (diff)
downloadu-boot-4a68406f3a3f3a1b57af59074d1139f3e020d090.tar.xz
ext4: Use helper function to access group descriptor and its fields
The descriptor size is variable, thus array indices are not generically applicable. The larger group descriptors also contain e.g. high parts of block numbers, which have to be read and written. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Change-Id: I83e44f27fdbdcb2cb65b309d28e66ab59566e1cf
-rw-r--r--fs/ext4/ext4_common.c127
-rw-r--r--fs/ext4/ext4_write.c167
-rw-r--r--include/ext4fs.h1
3 files changed, 156 insertions, 139 deletions
diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c
index 5878d5f51a..7e496dc6ee 100644
--- a/fs/ext4/ext4_common.c
+++ b/fs/ext4/ext4_common.c
@@ -401,7 +401,7 @@ uint16_t ext4fs_checksum_update(uint32_t i)
uint16_t crc = 0;
__le32 le32_i = cpu_to_le32(i);
- desc = (struct ext2_block_group *)&fs->bgd[i];
+ desc = ext4fs_get_group_descriptor(fs, i);
if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
int offset = offsetof(struct ext2_block_group, bg_checksum);
@@ -941,39 +941,41 @@ uint32_t ext4fs_get_new_blk_no(void)
char *zero_buffer = zalloc(fs->blksz);
if (!journal_buffer || !zero_buffer)
goto fail;
- struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
if (fs->first_pass_bbmap == 0) {
for (i = 0; i < fs->no_blkgrp; i++) {
- if (le16_to_cpu(bgd[i].free_blocks)) {
- if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
- uint16_t new_flags;
- put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
- zero_buffer, fs->blksz);
- new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
- bgd[i].bg_flags = cpu_to_le16(new_flags);
+ struct ext2_block_group *bgd = NULL;
+ bgd = ext4fs_get_group_descriptor(fs, i);
+ if (ext4fs_bg_get_free_blocks(bgd, fs)) {
+ uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
+ uint64_t b_bitmap_blk =
+ ext4fs_bg_get_block_id(bgd, fs);
+ if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
memcpy(fs->blk_bmaps[i], zero_buffer,
fs->blksz);
+ put_ext4(b_bitmap_blk * fs->blksz,
+ fs->blk_bmaps[i], fs->blksz);
+ bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
+ ext4fs_bg_set_flags(bgd, bg_flags);
}
fs->curr_blkno =
_get_new_blk_no(fs->blk_bmaps[i]);
if (fs->curr_blkno == -1)
- /* if block bitmap is completely fill */
+ /* block bitmap is completely filled */
continue;
fs->curr_blkno = fs->curr_blkno +
(i * fs->blksz * 8);
fs->first_pass_bbmap++;
- ext4fs_bg_free_blocks_dec(&bgd[i]);
+ ext4fs_bg_free_blocks_dec(bgd);
ext4fs_sb_free_blocks_dec(fs->sb);
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[i].block_id) *
- fs->sect_perblk, 0,
- fs->blksz,
+ status = ext4fs_devread(b_bitmap_blk *
+ fs->sect_perblk,
+ 0, fs->blksz,
journal_buffer);
if (status == 0)
goto fail;
if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[i].block_id)))
+ b_bitmap_blk))
goto fail;
goto success;
} else {
@@ -1000,7 +1002,9 @@ restart:
if (bg_idx >= fs->no_blkgrp)
goto fail;
- if (bgd[bg_idx].free_blocks == 0) {
+ 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)
@@ -1008,13 +1012,14 @@ restart:
goto restart;
}
- if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
- uint16_t new_flags;
- put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
- zero_buffer, fs->blksz);
+ uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
+ uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
+ if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
- new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
- bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
+ put_ext4(b_bitmap_blk * fs->blksz,
+ zero_buffer, fs->blksz);
+ bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
+ ext4fs_bg_set_flags(bgd, bg_flags);
}
if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
@@ -1027,19 +1032,16 @@ restart:
/* journal backup */
if (prev_bg_bitmap_index != bg_idx) {
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
- * fs->sect_perblk,
+ status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
0, fs->blksz, journal_buffer);
if (status == 0)
goto fail;
- if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[bg_idx].block_id)))
+ if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
goto fail;
prev_bg_bitmap_index = bg_idx;
}
- ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
+ ext4fs_bg_free_blocks_dec(bgd);
ext4fs_sb_free_blocks_dec(fs->sb);
goto success;
}
@@ -1067,46 +1069,49 @@ int ext4fs_get_new_inode_no(void)
char *zero_buffer = zalloc(fs->blksz);
if (!journal_buffer || !zero_buffer)
goto fail;
- struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
if (fs->first_pass_ibmap == 0) {
for (i = 0; i < fs->no_blkgrp; i++) {
- if (bgd[i].free_inodes) {
+ uint32_t free_inodes;
+ struct ext2_block_group *bgd = NULL;
+ bgd = ext4fs_get_group_descriptor(fs, i);
+ free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
+ if (free_inodes) {
+ uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
+ uint64_t i_bitmap_blk =
+ ext4fs_bg_get_inode_id(bgd, fs);
if (has_gdt_chksum)
- bgd[i].bg_itable_unused =
- bgd[i].free_inodes;
- if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
- int new_flags;
- put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
+ bgd->bg_itable_unused = free_inodes;
+ if (bg_flags & EXT4_BG_INODE_UNINIT) {
+ put_ext4(i_bitmap_blk * fs->blksz,
zero_buffer, fs->blksz);
- new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
- bgd[i].bg_flags = cpu_to_le16(new_flags);
+ bg_flags &= ~EXT4_BG_INODE_UNINIT;
+ ext4fs_bg_set_flags(bgd, bg_flags);
memcpy(fs->inode_bmaps[i],
zero_buffer, fs->blksz);
}
fs->curr_inode_no =
_get_new_inode_no(fs->inode_bmaps[i]);
if (fs->curr_inode_no == -1)
- /* if block bitmap is completely fill */
+ /* inode bitmap is completely filled */
continue;
fs->curr_inode_no = fs->curr_inode_no +
(i * inodes_per_grp);
fs->first_pass_ibmap++;
- ext4fs_bg_free_inodes_dec(&bgd[i]);
+ ext4fs_bg_free_inodes_dec(bgd);
if (has_gdt_chksum)
- ext4fs_bg_itable_unused_dec(&bgd[i]);
+ ext4fs_bg_itable_unused_dec(bgd);
ext4fs_sb_free_inodes_dec(fs->sb);
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
- fs->sect_perblk, 0,
- fs->blksz,
+ status = ext4fs_devread(i_bitmap_blk *
+ fs->sect_perblk,
+ 0, fs->blksz,
journal_buffer);
if (status == 0)
goto fail;
if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[i].inode_id)))
+ i_bitmap_blk))
goto fail;
goto success;
} else
@@ -1118,12 +1123,16 @@ restart:
fs->curr_inode_no++;
/* get the blockbitmap index respective to blockno */
ibmap_idx = fs->curr_inode_no / inodes_per_grp;
- if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
- int new_flags;
- put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
+ struct ext2_block_group *bgd =
+ ext4fs_get_group_descriptor(fs, ibmap_idx);
+ uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
+ uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
+
+ if (bg_flags & EXT4_BG_INODE_UNINIT) {
+ put_ext4(i_bitmap_blk * fs->blksz,
zero_buffer, fs->blksz);
- new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
- bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
+ bg_flags &= ~EXT4_BG_INODE_UNINIT;
+ ext4fs_bg_set_flags(bgd, bg_flags);
memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
fs->blksz);
}
@@ -1138,22 +1147,18 @@ restart:
/* journal backup */
if (prev_inode_bitmap_index != ibmap_idx) {
- memset(journal_buffer, '\0', fs->blksz);
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
- * fs->sect_perblk,
+ status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
0, fs->blksz, journal_buffer);
if (status == 0)
goto fail;
if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[ibmap_idx].inode_id)))
+ le32_to_cpu(bgd->inode_id)))
goto fail;
prev_inode_bitmap_index = ibmap_idx;
}
- ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
+ ext4fs_bg_free_inodes_dec(bgd);
if (has_gdt_chksum)
- bgd[ibmap_idx].bg_itable_unused =
- bgd[ibmap_idx].free_inodes;
+ bgd->bg_itable_unused = bgd->free_inodes;
ext4fs_sb_free_inodes_dec(fs->sb);
goto success;
}
@@ -1564,8 +1569,8 @@ int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
return 0;
inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
- blkno = __le32_to_cpu(blkgrp.inode_table_id) +
- (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
+ blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
+ (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
blkoff = (ino % inodes_per_block) * fs->inodesz;
/* Read the inode. */
status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c
index df53774ce4..8343ab1d6d 100644
--- a/fs/ext4/ext4_write.c
+++ b/fs/ext4/ext4_write.c
@@ -56,21 +56,26 @@ static void ext4fs_update(void)
short i;
ext4fs_update_journal();
struct ext_filesystem *fs = get_fs();
+ struct ext2_block_group *bgd = NULL;
/* update super block */
put_ext4((uint64_t)(SUPERBLOCK_SIZE),
(struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
- /* update block groups */
+ /* update block bitmaps */
for (i = 0; i < fs->no_blkgrp; i++) {
- fs->bgd[i].bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
- put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].block_id) * fs->blksz,
+ bgd = ext4fs_get_group_descriptor(fs, i);
+ bgd->bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
+ uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
+ put_ext4(b_bitmap_blk * fs->blksz,
fs->blk_bmaps[i], fs->blksz);
}
- /* update inode table groups */
+ /* update inode bitmaps */
for (i = 0; i < fs->no_blkgrp; i++) {
- put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].inode_id) * fs->blksz,
+ bgd = ext4fs_get_group_descriptor(fs, i);
+ uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
+ put_ext4(i_bitmap_blk * fs->blksz,
fs->inode_bmaps[i], fs->blksz);
}
@@ -88,15 +93,12 @@ static void ext4fs_update(void)
int ext4fs_get_bgdtable(void)
{
int status;
- int grp_desc_size;
struct ext_filesystem *fs = get_fs();
- grp_desc_size = sizeof(struct ext2_block_group);
- fs->no_blk_pergdt = (fs->no_blkgrp * grp_desc_size) / fs->blksz;
- if ((fs->no_blkgrp * grp_desc_size) % fs->blksz)
- fs->no_blk_pergdt++;
+ int gdsize_total = ROUND(fs->no_blkgrp * fs->gdsize, fs->blksz);
+ fs->no_blk_pergdt = gdsize_total / fs->blksz;
/* allocate memory for gdtable */
- fs->gdtable = zalloc(fs->blksz * fs->no_blk_pergdt);
+ fs->gdtable = zalloc(gdsize_total);
if (!fs->gdtable)
return -ENOMEM;
/* read the group descriptor table */
@@ -133,8 +135,6 @@ static void delete_single_indirect_block(struct ext2_inode *inode)
printf("No memory\n");
return;
}
- /* get block group descriptor table */
- bgd = (struct ext2_block_group *)fs->gdtable;
/* deleting the single indirect block associated with inode */
if (inode->b.blocks.indir_block != 0) {
@@ -147,18 +147,19 @@ static void delete_single_indirect_block(struct ext2_inode *inode)
bg_idx--;
}
ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
+ uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
- fs->sect_perblk, 0, fs->blksz,
- journal_buffer);
+ b_bitmap_blk * fs->sect_perblk,
+ 0, fs->blksz, journal_buffer);
if (status == 0)
goto fail;
- if (ext4fs_log_journal
- (journal_buffer, le32_to_cpu(bgd[bg_idx].block_id)))
+ if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -185,8 +186,6 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
printf("No memory\n");
return;
}
- /* get the block group descriptor table */
- bgd = (struct ext2_block_group *)fs->gdtable;
if (inode->b.blocks.double_indir_block != 0) {
di_buffer = zalloc(fs->blksz);
@@ -209,15 +208,18 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
if (!remainder)
bg_idx--;
}
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
fs->blk_bmaps[bg_idx], bg_idx);
di_buffer++;
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
+ uint64_t b_bitmap_blk =
+ ext4fs_bg_get_block_id(bgd, fs);
+ status = ext4fs_devread(b_bitmap_blk
* fs->sect_perblk, 0,
fs->blksz,
journal_buffer);
@@ -225,7 +227,7 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
goto fail;
if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[bg_idx].block_id)))
+ b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -239,20 +241,20 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
if (!remainder)
bg_idx--;
}
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
- memset(journal_buffer, '\0', fs->blksz);
- status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
- fs->sect_perblk, 0, fs->blksz,
- journal_buffer);
+ uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
+ status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
+ 0, fs->blksz, journal_buffer);
if (status == 0)
goto fail;
- if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[bg_idx].block_id)))
+ if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -283,8 +285,6 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
printf("No memory\n");
return;
}
- /* get block group descriptor table */
- bgd = (struct ext2_block_group *)fs->gdtable;
if (inode->b.blocks.triple_indir_block != 0) {
tigp_buffer = zalloc(fs->blksz);
@@ -323,13 +323,17 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
bg_idx);
tip_buffer++;
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
+ uint64_t b_bitmap_blk =
+ ext4fs_bg_get_block_id(bgd, fs);
status =
ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
+ b_bitmap_blk *
fs->sect_perblk, 0,
fs->blksz,
journal_buffer);
@@ -337,7 +341,7 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
goto fail;
if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[bg_idx].block_id)))
+ b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -359,21 +363,24 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
fs->blk_bmaps[bg_idx], bg_idx);
tigp_buffer++;
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
+ uint64_t b_bitmap_blk =
+ ext4fs_bg_get_block_id(bgd, fs);
memset(journal_buffer, '\0', fs->blksz);
- status =
- ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
- fs->sect_perblk, 0,
- fs->blksz, journal_buffer);
+ status = ext4fs_devread(b_bitmap_blk *
+ fs->sect_perblk, 0,
+ fs->blksz,
+ journal_buffer);
if (status == 0)
goto fail;
if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[bg_idx].block_id)))
+ b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -388,20 +395,19 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
bg_idx--;
}
ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
- memset(journal_buffer, '\0', fs->blksz);
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
- fs->sect_perblk, 0, fs->blksz,
- journal_buffer);
+ uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
+ status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
+ 0, fs->blksz, journal_buffer);
if (status == 0)
goto fail;
- if (ext4fs_log_journal(journal_buffer,
- le32_to_cpu(bgd[bg_idx].block_id)))
+ if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -438,8 +444,6 @@ static int ext4fs_delete_file(int inodeno)
char *journal_buffer = zalloc(fs->blksz);
if (!journal_buffer)
return -ENOMEM;
- /* get the block group descriptor table */
- bgd = (struct ext2_block_group *)fs->gdtable;
status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
if (status == 0)
goto fail;
@@ -478,18 +482,19 @@ static int ext4fs_delete_file(int inodeno)
bg_idx);
debug("EXT4 Block releasing %ld: %d\n", blknr, bg_idx);
- ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, bg_idx);
+ ext4fs_bg_free_blocks_inc(bgd);
ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */
if (prev_bg_bmap_idx != bg_idx) {
- uint32_t bgd_blknr = le32_to_cpu(bgd[bg_idx].block_id);
- status = ext4fs_devread((lbaint_t)bgd_blknr *
- fs->sect_perblk,
+ uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
+ status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
0, fs->blksz,
journal_buffer);
if (status == 0)
goto fail;
- if (ext4fs_log_journal(journal_buffer, bgd_blknr))
+ if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
goto fail;
prev_bg_bmap_idx = bg_idx;
}
@@ -502,7 +507,9 @@ static int ext4fs_delete_file(int inodeno)
/* get the block no */
inodeno--;
- blkno = le32_to_cpu(bgd[ibmap_idx].inode_table_id) +
+ /* get block group descriptor table */
+ bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
+ blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
(inodeno % inode_per_grp) / inodes_per_block;
/* get the offset of the inode */
@@ -532,15 +539,15 @@ static int ext4fs_delete_file(int inodeno)
/* update the respective inode bitmaps */
inodeno++;
ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
- ext4fs_bg_free_inodes_inc(&bgd[ibmap_idx]);
+ ext4fs_bg_free_inodes_inc(bgd);
ext4fs_sb_free_inodes_inc(fs->sb);
/* journal backup */
memset(journal_buffer, '\0', fs->blksz);
- status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id) *
+ status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
fs->sect_perblk, 0, fs->blksz, journal_buffer);
if (status == 0)
goto fail;
- if (ext4fs_log_journal(journal_buffer, le32_to_cpu(bgd[ibmap_idx].inode_id)))
+ if (ext4fs_log_journal(journal_buffer, ext4fs_bg_get_inode_id(bgd, fs)))
goto fail;
ext4fs_update();
@@ -597,7 +604,6 @@ int ext4fs_init(void)
printf("Error in getting the block group descriptor table\n");
goto fail;
}
- fs->bgd = (struct ext2_block_group *)fs->gdtable;
/* load all the available bitmap block of the partition */
fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
@@ -610,9 +616,9 @@ int ext4fs_init(void)
}
for (i = 0; i < fs->no_blkgrp; i++) {
- status =
- ext4fs_devread(
- (lbaint_t)le32_to_cpu(fs->bgd[i].block_id) *
+ struct ext2_block_group *bgd =
+ ext4fs_get_group_descriptor(fs, i);
+ status = ext4fs_devread(ext4fs_bg_get_block_id(bgd, fs) *
fs->sect_perblk, 0,
fs->blksz, (char *)fs->blk_bmaps[i]);
if (status == 0)
@@ -630,8 +636,9 @@ int ext4fs_init(void)
}
for (i = 0; i < fs->no_blkgrp; i++) {
- status = ext4fs_devread(
- (lbaint_t)le32_to_cpu(fs->bgd[i].inode_id) *
+ struct ext2_block_group *bgd =
+ ext4fs_get_group_descriptor(fs, i);
+ status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
fs->sect_perblk,
0, fs->blksz,
(char *)fs->inode_bmaps[i]);
@@ -645,10 +652,14 @@ int ext4fs_init(void)
* with the blockgroups freeblocks when improper
* reboot of a linux kernel
*/
- for (i = 0; i < fs->no_blkgrp; i++)
- real_free_blocks = real_free_blocks + le16_to_cpu(fs->bgd[i].free_blocks);
- if (real_free_blocks != le32_to_cpu(fs->sb->free_blocks))
- fs->sb->free_blocks = cpu_to_le32(real_free_blocks);
+ for (i = 0; i < fs->no_blkgrp; i++) {
+ struct ext2_block_group *bgd =
+ ext4fs_get_group_descriptor(fs, i);
+ real_free_blocks = real_free_blocks +
+ ext4fs_bg_get_free_blocks(bgd, fs);
+ }
+ if (real_free_blocks != ext4fs_sb_get_free_blocks(fs->sb))
+ ext4fs_sb_set_free_blocks(fs->sb, real_free_blocks);
return 0;
fail:
@@ -714,7 +725,6 @@ void ext4fs_deinit(void)
free(fs->gdtable);
fs->gdtable = NULL;
- fs->bgd = NULL;
/*
* reinitiliazed the global inode and
* block bitmap first execution check variables
@@ -849,6 +859,7 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
struct ext2_sblock *sblock = &(ext4fs_root->sblock);
unsigned int inodes_per_block;
unsigned int ibmap_idx;
+ struct ext2_block_group *bgd = NULL;
struct ext_filesystem *fs = get_fs();
ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256);
memset(filename, 0x00, 256);
@@ -926,8 +937,9 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
goto fail;
ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
inodeno--;
- itable_blkno = __le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
- (inodeno % __le32_to_cpu(sblock->inodes_per_group)) /
+ bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
+ itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
+ (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
inodes_per_block;
blkoff = (inodeno % inodes_per_block) * fs->inodesz;
ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz,
@@ -946,7 +958,8 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
}
ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
parent_inodeno--;
- parent_itable_blkno = __le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
+ bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
+ parent_itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
(parent_inodeno %
__le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
diff --git a/include/ext4fs.h b/include/ext4fs.h
index 7455313b5c..ee8d94194b 100644
--- a/include/ext4fs.h
+++ b/include/ext4fs.h
@@ -98,7 +98,6 @@ struct ext_filesystem {
/* Superblock */
struct ext2_sblock *sb;
/* Block group descritpor table */
- struct ext2_block_group *bgd;
char *gdtable;
/* Block Bitmap Related */