diff options
author | Stefan Brüns <stefan.bruens@rwth-aachen.de> | 2016-09-17 02:10:10 +0200 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2016-09-23 09:20:15 -0400 |
commit | 688d0e79f657ed0b60e5b721bd00540021e1e518 (patch) | |
tree | a4e14ccf4c7c89cc2641b079fa98af6119f2cd16 /fs/ext4/ext4_write.c | |
parent | f798b1dda1c5de818b806189e523d1b75db7e72d (diff) | |
download | u-boot-688d0e79f657ed0b60e5b721bd00540021e1e518.tar.gz u-boot-688d0e79f657ed0b60e5b721bd00540021e1e518.tar.bz2 u-boot-688d0e79f657ed0b60e5b721bd00540021e1e518.zip |
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>
Diffstat (limited to 'fs/ext4/ext4_write.c')
-rw-r--r-- | fs/ext4/ext4_write.c | 165 |
1 files changed, 89 insertions, 76 deletions
diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c index e4f0905876..2c123e3646 100644 --- a/fs/ext4/ext4_write.c +++ b/fs/ext4/ext4_write.c @@ -53,21 +53,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); } @@ -85,15 +90,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 */ @@ -130,8 +132,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) { @@ -144,18 +144,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; } @@ -182,8 +183,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); @@ -206,15 +205,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); @@ -222,7 +224,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; } @@ -236,20 +238,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; } @@ -280,8 +282,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); @@ -320,13 +320,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); @@ -334,7 +338,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; } @@ -356,21 +360,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; } @@ -385,20 +392,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; } @@ -435,8 +441,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; @@ -475,18 +479,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; } @@ -499,7 +504,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 */ @@ -529,15 +536,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(); @@ -594,7 +601,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 *)); @@ -607,9 +613,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) @@ -627,8 +633,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]); @@ -642,10 +649,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: @@ -711,7 +722,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 @@ -826,6 +836,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); @@ -903,7 +914,8 @@ 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) + + 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; @@ -923,7 +935,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; |