1
0
mirror of https://xff.cz/git/u-boot/ synced 2025-09-04 02:02:08 +02:00

ext4: fix endianess problems in ext4 write support

All fields were accessed directly instead of using the proper byte swap
functions. Thus, ext4 write support was only usable on little-endian
architectures. Fix this.

Signed-off-by: Michael Walle <michael@walle.cc>
This commit is contained in:
Michael Walle
2016-09-01 11:21:40 +02:00
committed by Tom Rini
parent 7f101be314
commit 58a9ecbaf4
6 changed files with 311 additions and 250 deletions

View File

@@ -33,20 +33,45 @@
struct ext2_data *ext4fs_root; struct ext2_data *ext4fs_root;
struct ext2fs_node *ext4fs_file; struct ext2fs_node *ext4fs_file;
uint32_t *ext4fs_indir1_block; __le32 *ext4fs_indir1_block;
int ext4fs_indir1_size; int ext4fs_indir1_size;
int ext4fs_indir1_blkno = -1; int ext4fs_indir1_blkno = -1;
uint32_t *ext4fs_indir2_block; __le32 *ext4fs_indir2_block;
int ext4fs_indir2_size; int ext4fs_indir2_size;
int ext4fs_indir2_blkno = -1; int ext4fs_indir2_blkno = -1;
uint32_t *ext4fs_indir3_block; __le32 *ext4fs_indir3_block;
int ext4fs_indir3_size; int ext4fs_indir3_size;
int ext4fs_indir3_blkno = -1; int ext4fs_indir3_blkno = -1;
struct ext2_inode *g_parent_inode; struct ext2_inode *g_parent_inode;
static int symlinknest; static int symlinknest;
#if defined(CONFIG_EXT4_WRITE) #if defined(CONFIG_EXT4_WRITE)
static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
{
sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
}
static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
{
sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
}
static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
{
bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
}
static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
{
bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
}
static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
{
bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
}
uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n) uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
{ {
uint32_t res = size / n; uint32_t res = size / n;
@@ -112,7 +137,7 @@ static int _get_new_inode_no(unsigned char *buffer)
while (*ptr == 255) { while (*ptr == 255) {
ptr++; ptr++;
count += 8; count += 8;
if (count > ext4fs_root->sblock.inodes_per_group) if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
return -1; return -1;
} }
@@ -249,7 +274,7 @@ int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
unsigned char *ptr = buffer; unsigned char *ptr = buffer;
unsigned char operand; unsigned char operand;
inode_no -= (index * ext4fs_root->sblock.inodes_per_group); inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
i = inode_no / 8; i = inode_no / 8;
remainder = inode_no % 8; remainder = inode_no % 8;
if (remainder == 0) { if (remainder == 0) {
@@ -274,7 +299,7 @@ void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
unsigned char *ptr = buffer; unsigned char *ptr = buffer;
unsigned char operand; unsigned char operand;
inode_no -= (index * ext4fs_root->sblock.inodes_per_group); inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
i = inode_no / 8; i = inode_no / 8;
remainder = inode_no % 8; remainder = inode_no % 8;
if (remainder == 0) { if (remainder == 0) {
@@ -289,19 +314,20 @@ void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
*ptr = *ptr & ~(operand); *ptr = *ptr & ~(operand);
} }
int ext4fs_checksum_update(unsigned int i) uint16_t ext4fs_checksum_update(uint32_t i)
{ {
struct ext2_block_group *desc; struct ext2_block_group *desc;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
__u16 crc = 0; uint16_t crc = 0;
__le32 le32_i = cpu_to_le32(i);
desc = (struct ext2_block_group *)&fs->bgd[i]; desc = (struct ext2_block_group *)&fs->bgd[i];
if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
int offset = offsetof(struct ext2_block_group, bg_checksum); int offset = offsetof(struct ext2_block_group, bg_checksum);
crc = ext2fs_crc16(~0, fs->sb->unique_id, crc = ext2fs_crc16(~0, fs->sb->unique_id,
sizeof(fs->sb->unique_id)); sizeof(fs->sb->unique_id));
crc = ext2fs_crc16(crc, &i, sizeof(i)); crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
crc = ext2fs_crc16(crc, desc, offset); crc = ext2fs_crc16(crc, desc, offset);
offset += sizeof(desc->bg_checksum); /* skip checksum */ offset += sizeof(desc->bg_checksum); /* skip checksum */
assert(offset == sizeof(*desc)); assert(offset == sizeof(*desc));
@@ -322,7 +348,7 @@ static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
dentry_length = sizeof(struct ext2_dirent) + dentry_length = sizeof(struct ext2_dirent) +
dir->namelen + padding_factor; dir->namelen + padding_factor;
sizeof_void_space = dir->direntlen - dentry_length; sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
if (sizeof_void_space == 0) if (sizeof_void_space == 0)
return 0; return 0;
@@ -333,7 +359,7 @@ static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
new_entry_byte_reqd = strlen(filename) + new_entry_byte_reqd = strlen(filename) +
sizeof(struct ext2_dirent) + padding_factor; sizeof(struct ext2_dirent) + padding_factor;
if (sizeof_void_space >= new_entry_byte_reqd) { if (sizeof_void_space >= new_entry_byte_reqd) {
dir->direntlen = dentry_length; dir->direntlen = cpu_to_le16(dentry_length);
return sizeof_void_space; return sizeof_void_space;
} }
@@ -360,6 +386,9 @@ void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
/* directory entry */ /* directory entry */
struct ext2_dirent *dir; struct ext2_dirent *dir;
char *temp_dir = NULL; char *temp_dir = NULL;
uint32_t new_blk_no;
uint32_t new_size;
uint32_t new_blockcnt;
zero_buffer = zalloc(fs->blksz); zero_buffer = zalloc(fs->blksz);
if (!zero_buffer) { if (!zero_buffer) {
@@ -396,7 +425,7 @@ restart:
goto fail; goto fail;
dir = (struct ext2_dirent *)root_first_block_buffer; dir = (struct ext2_dirent *)root_first_block_buffer;
totalbytes = 0; totalbytes = 0;
while (dir->direntlen > 0) { while (le16_to_cpu(dir->direntlen) > 0) {
/* /*
* blocksize-totalbytes because last directory length * blocksize-totalbytes because last directory length
* i.e. dir->direntlen is free availble space in the * i.e. dir->direntlen is free availble space in the
@@ -405,7 +434,7 @@ restart:
*/ */
/* traversing the each directory entry */ /* traversing the each directory entry */
if (fs->blksz - totalbytes == dir->direntlen) { if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
if (strlen(filename) % 4 != 0) if (strlen(filename) % 4 != 0)
padding_factor = 4 - (strlen(filename) % 4); padding_factor = 4 - (strlen(filename) % 4);
@@ -430,32 +459,34 @@ restart:
printf("Directory exceeds limit\n"); printf("Directory exceeds limit\n");
goto fail; goto fail;
} }
g_parent_inode->b.blocks.dir_blocks new_blk_no = ext4fs_get_new_blk_no();
[direct_blk_idx] = ext4fs_get_new_blk_no(); if (new_blk_no == -1) {
if (g_parent_inode->b.blocks.dir_blocks
[direct_blk_idx] == -1) {
printf("no block left to assign\n"); printf("no block left to assign\n");
goto fail; goto fail;
} }
put_ext4(((uint64_t) put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
((uint64_t)g_parent_inode->b. g_parent_inode->b.blocks.dir_blocks[direct_blk_idx] =
blocks.dir_blocks[direct_blk_idx] * cpu_to_le32(new_blk_no);
(uint64_t)fs->blksz)), zero_buffer, fs->blksz);
g_parent_inode->size = new_size = le32_to_cpu(g_parent_inode->size);
g_parent_inode->size + fs->blksz; new_size += fs->blksz;
g_parent_inode->blockcnt = g_parent_inode->size = cpu_to_le32(new_size);
g_parent_inode->blockcnt + fs->sect_perblk;
new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
new_blockcnt += fs->sect_perblk;
g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
if (ext4fs_put_metadata if (ext4fs_put_metadata
(root_first_block_buffer, (root_first_block_buffer,
first_block_no_of_root)) first_block_no_of_root))
goto fail; goto fail;
goto restart; goto restart;
} }
dir->direntlen = last_entry_dirlen; dir->direntlen = cpu_to_le16(last_entry_dirlen);
break; break;
} }
templength = dir->direntlen; templength = le16_to_cpu(dir->direntlen);
totalbytes = totalbytes + templength; totalbytes = totalbytes + templength;
sizeof_void_space = check_void_in_dentry(dir, filename); sizeof_void_space = check_void_in_dentry(dir, filename);
if (sizeof_void_space) if (sizeof_void_space)
@@ -465,7 +496,7 @@ restart:
} }
/* make a pointer ready for creating next directory entry */ /* make a pointer ready for creating next directory entry */
templength = dir->direntlen; templength = le16_to_cpu(dir->direntlen);
totalbytes = totalbytes + templength; totalbytes = totalbytes + templength;
dir = (struct ext2_dirent *)((char *)dir + templength); dir = (struct ext2_dirent *)((char *)dir + templength);
@@ -475,11 +506,11 @@ restart:
printf("no inode left to assign\n"); printf("no inode left to assign\n");
goto fail; goto fail;
} }
dir->inode = inodeno; dir->inode = cpu_to_le32(inodeno);
if (sizeof_void_space) if (sizeof_void_space)
dir->direntlen = sizeof_void_space; dir->direntlen = cpu_to_le16(sizeof_void_space);
else else
dir->direntlen = fs->blksz - totalbytes; dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
dir->namelen = strlen(filename); dir->namelen = strlen(filename);
dir->filetype = FILETYPE_REG; /* regular file */ dir->filetype = FILETYPE_REG; /* regular file */
@@ -534,7 +565,7 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname)
dir = (struct ext2_dirent *)block_buffer; dir = (struct ext2_dirent *)block_buffer;
ptr = (char *)dir; ptr = (char *)dir;
totalbytes = 0; totalbytes = 0;
while (dir->direntlen >= 0) { while (le16_to_cpu(dir->direntlen) >= 0) {
/* /*
* blocksize-totalbytes because last directory * blocksize-totalbytes because last directory
* length i.e.,*dir->direntlen is free availble * length i.e.,*dir->direntlen is free availble
@@ -542,23 +573,23 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname)
* it is a last entry of directory entry * it is a last entry of directory entry
*/ */
if (strlen(dirname) == dir->namelen) { if (strlen(dirname) == dir->namelen) {
if (strncmp(dirname, ptr + if (strncmp(dirname, ptr + sizeof(struct ext2_dirent), dir->namelen) == 0) {
sizeof(struct ext2_dirent), uint16_t new_len;
dir->namelen) == 0) { new_len = le16_to_cpu(previous_dir->direntlen);
previous_dir->direntlen += new_len += le16_to_cpu(dir->direntlen);
dir->direntlen; previous_dir->direntlen = cpu_to_le16(new_len);
inodeno = dir->inode; inodeno = le32_to_cpu(dir->inode);
dir->inode = 0; dir->inode = 0;
found = 1; found = 1;
break; break;
} }
} }
if (fs->blksz - totalbytes == dir->direntlen) if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
break; break;
/* traversing the each directory entry */ /* traversing the each directory entry */
templength = dir->direntlen; templength = le16_to_cpu(dir->direntlen);
totalbytes = totalbytes + templength; totalbytes = totalbytes + templength;
previous_dir = dir; previous_dir = dir;
dir = (struct ext2_dirent *)((char *)dir + templength); dir = (struct ext2_dirent *)((char *)dir + templength);
@@ -720,7 +751,7 @@ end:
if (matched_inode_no != -1) { if (matched_inode_no != -1) {
ext4fs_iget(matched_inode_no, &temp_inode); ext4fs_iget(matched_inode_no, &temp_inode);
if (temp_inode.mode & S_IFDIR) { if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
printf("It is a Directory\n"); printf("It is a Directory\n");
result_inode_no = -1; result_inode_no = -1;
goto fail; goto fail;
@@ -780,7 +811,7 @@ static int check_filename(char *filename, unsigned int blknr)
dir = (struct ext2_dirent *)root_first_block_buffer; dir = (struct ext2_dirent *)root_first_block_buffer;
ptr = (char *)dir; ptr = (char *)dir;
totalbytes = 0; totalbytes = 0;
while (dir->direntlen >= 0) { while (le16_to_cpu(dir->direntlen) >= 0) {
/* /*
* blocksize-totalbytes because last * blocksize-totalbytes because last
* directory length i.e., *dir->direntlen * directory length i.e., *dir->direntlen
@@ -790,20 +821,23 @@ static int check_filename(char *filename, unsigned int blknr)
if (strlen(filename) == dir->namelen) { if (strlen(filename) == dir->namelen) {
if (strncmp(filename, ptr + sizeof(struct ext2_dirent), if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
dir->namelen) == 0) { dir->namelen) == 0) {
uint16_t new_len;
printf("file found deleting\n"); printf("file found deleting\n");
previous_dir->direntlen += dir->direntlen; new_len = le16_to_cpu(previous_dir->direntlen);
inodeno = dir->inode; new_len += le16_to_cpu(dir->direntlen);
previous_dir->direntlen = cpu_to_le16(new_len);
inodeno = le32_to_cpu(dir->inode);
dir->inode = 0; dir->inode = 0;
found = 1; found = 1;
break; break;
} }
} }
if (fs->blksz - totalbytes == dir->direntlen) if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
break; break;
/* traversing the each directory entry */ /* traversing the each directory entry */
templength = dir->direntlen; templength = le16_to_cpu(dir->direntlen);
totalbytes = totalbytes + templength; totalbytes = totalbytes + templength;
previous_dir = dir; previous_dir = dir;
dir = (struct ext2_dirent *)((char *)dir + templength); dir = (struct ext2_dirent *)((char *)dir + templength);
@@ -843,14 +877,14 @@ int ext4fs_filename_check(char *filename)
return -1; return -1;
} }
long int ext4fs_get_new_blk_no(void) uint32_t ext4fs_get_new_blk_no(void)
{ {
short i; short i;
short status; short status;
int remainder; int remainder;
unsigned int bg_idx; unsigned int bg_idx;
static int prev_bg_bitmap_index = -1; static int prev_bg_bitmap_index = -1;
unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
char *journal_buffer = zalloc(fs->blksz); char *journal_buffer = zalloc(fs->blksz);
char *zero_buffer = zalloc(fs->blksz); char *zero_buffer = zalloc(fs->blksz);
@@ -860,14 +894,13 @@ long int ext4fs_get_new_blk_no(void)
if (fs->first_pass_bbmap == 0) { if (fs->first_pass_bbmap == 0) {
for (i = 0; i < fs->no_blkgrp; i++) { for (i = 0; i < fs->no_blkgrp; i++) {
if (bgd[i].free_blocks) { if (le16_to_cpu(bgd[i].free_blocks)) {
if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) { if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
put_ext4(((uint64_t) ((uint64_t)bgd[i].block_id * uint16_t new_flags;
(uint64_t)fs->blksz)), put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
zero_buffer, fs->blksz); zero_buffer, fs->blksz);
bgd[i].bg_flags = new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
bgd[i]. bgd[i].bg_flags = cpu_to_le16(new_flags);
bg_flags & ~EXT4_BG_BLOCK_UNINIT;
memcpy(fs->blk_bmaps[i], zero_buffer, memcpy(fs->blk_bmaps[i], zero_buffer,
fs->blksz); fs->blksz);
} }
@@ -879,17 +912,17 @@ long int ext4fs_get_new_blk_no(void)
fs->curr_blkno = fs->curr_blkno + fs->curr_blkno = fs->curr_blkno +
(i * fs->blksz * 8); (i * fs->blksz * 8);
fs->first_pass_bbmap++; fs->first_pass_bbmap++;
bgd[i].free_blocks--; ext4fs_bg_free_blocks_dec(&bgd[i]);
fs->sb->free_blocks--; ext4fs_sb_free_blocks_dec(fs->sb);
status = ext4fs_devread((lbaint_t) status = ext4fs_devread(
bgd[i].block_id * (lbaint_t)le32_to_cpu(bgd[i].block_id) *
fs->sect_perblk, 0, fs->sect_perblk, 0,
fs->blksz, fs->blksz,
journal_buffer); journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[i].block_id)) le32_to_cpu(bgd[i].block_id)))
goto fail; goto fail;
goto success; goto success;
} else { } else {
@@ -923,13 +956,14 @@ restart:
goto restart; goto restart;
} }
if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) { if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
uint16_t new_flags;
memset(zero_buffer, '\0', fs->blksz); memset(zero_buffer, '\0', fs->blksz);
put_ext4(((uint64_t) ((uint64_t)bgd[bg_idx].block_id * put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
(uint64_t)fs->blksz)), zero_buffer, fs->blksz); zero_buffer, fs->blksz);
memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz); memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags & new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
~EXT4_BG_BLOCK_UNINIT; bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
} }
if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx], if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
@@ -942,19 +976,20 @@ restart:
/* journal backup */ /* journal backup */
if (prev_bg_bitmap_index != bg_idx) { if (prev_bg_bitmap_index != bg_idx) {
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id status = ext4fs_devread(
(lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
* fs->sect_perblk, * fs->sect_perblk,
0, fs->blksz, journal_buffer); 0, fs->blksz, journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bitmap_index = bg_idx; prev_bg_bitmap_index = bg_idx;
} }
bgd[bg_idx].free_blocks--; ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
fs->sb->free_blocks--; ext4fs_sb_free_blocks_dec(fs->sb);
goto success; goto success;
} }
success: success:
@@ -975,7 +1010,7 @@ int ext4fs_get_new_inode_no(void)
short status; short status;
unsigned int ibmap_idx; unsigned int ibmap_idx;
static int prev_inode_bitmap_index = -1; static int prev_inode_bitmap_index = -1;
unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group; unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
char *journal_buffer = zalloc(fs->blksz); char *journal_buffer = zalloc(fs->blksz);
char *zero_buffer = zalloc(fs->blksz); char *zero_buffer = zalloc(fs->blksz);
@@ -990,13 +1025,12 @@ int ext4fs_get_new_inode_no(void)
bgd[i].free_inodes) bgd[i].free_inodes)
bgd[i].bg_itable_unused = bgd[i].bg_itable_unused =
bgd[i].free_inodes; bgd[i].free_inodes;
if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) { if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
put_ext4(((uint64_t) int new_flags;
((uint64_t)bgd[i].inode_id * put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
(uint64_t)fs->blksz)),
zero_buffer, fs->blksz); zero_buffer, fs->blksz);
bgd[i].bg_flags = bgd[i].bg_flags & new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
~EXT4_BG_INODE_UNINIT; bgd[i].bg_flags = cpu_to_le16(new_flags);
memcpy(fs->inode_bmaps[i], memcpy(fs->inode_bmaps[i],
zero_buffer, fs->blksz); zero_buffer, fs->blksz);
} }
@@ -1008,18 +1042,18 @@ int ext4fs_get_new_inode_no(void)
fs->curr_inode_no = fs->curr_inode_no + fs->curr_inode_no = fs->curr_inode_no +
(i * inodes_per_grp); (i * inodes_per_grp);
fs->first_pass_ibmap++; fs->first_pass_ibmap++;
bgd[i].free_inodes--; ext4fs_bg_free_inodes_dec(&bgd[i]);
bgd[i].bg_itable_unused--; ext4fs_bg_itable_unused_dec(&bgd[i]);
fs->sb->free_inodes--; ext4fs_sb_free_inodes_dec(fs->sb);
status = ext4fs_devread((lbaint_t) status = ext4fs_devread(
bgd[i].inode_id * (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
fs->sect_perblk, 0, fs->sect_perblk, 0,
fs->blksz, fs->blksz,
journal_buffer); journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[i].inode_id)) le32_to_cpu(bgd[i].inode_id)))
goto fail; goto fail;
goto success; goto success;
} else } else
@@ -1031,13 +1065,13 @@ restart:
fs->curr_inode_no++; fs->curr_inode_no++;
/* get the blockbitmap index respective to blockno */ /* get the blockbitmap index respective to blockno */
ibmap_idx = fs->curr_inode_no / inodes_per_grp; ibmap_idx = fs->curr_inode_no / inodes_per_grp;
if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) { if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
int new_flags;
memset(zero_buffer, '\0', fs->blksz); memset(zero_buffer, '\0', fs->blksz);
put_ext4(((uint64_t) ((uint64_t)bgd[ibmap_idx].inode_id * put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
(uint64_t)fs->blksz)), zero_buffer, zero_buffer, fs->blksz);
fs->blksz); new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
bgd[ibmap_idx].bg_flags = bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT;
memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer, memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
fs->blksz); fs->blksz);
} }
@@ -1053,14 +1087,14 @@ restart:
/* journal backup */ /* journal backup */
if (prev_inode_bitmap_index != ibmap_idx) { if (prev_inode_bitmap_index != ibmap_idx) {
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = ext4fs_devread((lbaint_t) status = ext4fs_devread(
bgd[ibmap_idx].inode_id (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
* fs->sect_perblk, * fs->sect_perblk,
0, fs->blksz, journal_buffer); 0, fs->blksz, journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[ibmap_idx].inode_id)) le32_to_cpu(bgd[ibmap_idx].inode_id)))
goto fail; goto fail;
prev_inode_bitmap_index = ibmap_idx; prev_inode_bitmap_index = ibmap_idx;
} }
@@ -1068,9 +1102,9 @@ restart:
bgd[ibmap_idx].free_inodes) bgd[ibmap_idx].free_inodes)
bgd[ibmap_idx].bg_itable_unused = bgd[ibmap_idx].bg_itable_unused =
bgd[ibmap_idx].free_inodes; bgd[ibmap_idx].free_inodes;
bgd[ibmap_idx].free_inodes--; ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
bgd[ibmap_idx].bg_itable_unused--; ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
fs->sb->free_inodes--; ext4fs_sb_free_inodes_dec(fs->sb);
goto success; goto success;
} }
@@ -1097,8 +1131,8 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,
long int actual_block_no; long int actual_block_no;
long int si_blockno; long int si_blockno;
/* si :single indirect */ /* si :single indirect */
unsigned int *si_buffer = NULL; __le32 *si_buffer = NULL;
unsigned int *si_start_addr = NULL; __le32 *si_start_addr = NULL;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
if (*total_remaining_blocks != 0) { if (*total_remaining_blocks != 0) {
@@ -1128,7 +1162,7 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,
printf("no block left to assign\n"); printf("no block left to assign\n");
goto fail; goto fail;
} }
*si_buffer = actual_block_no; *si_buffer = cpu_to_le32(actual_block_no);
debug("SIAB %u: %u\n", *si_buffer, debug("SIAB %u: %u\n", *si_buffer,
*total_remaining_blocks); *total_remaining_blocks);
@@ -1141,7 +1175,7 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,
/* write the block to disk */ /* write the block to disk */
put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)), put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
si_start_addr, fs->blksz); si_start_addr, fs->blksz);
file_inode->b.blocks.indir_block = si_blockno; file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
} }
fail: fail:
free(si_start_addr); free(si_start_addr);
@@ -1158,10 +1192,10 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
/* di:double indirect */ /* di:double indirect */
long int di_blockno_parent; long int di_blockno_parent;
long int di_blockno_child; long int di_blockno_child;
unsigned int *di_parent_buffer = NULL; __le32 *di_parent_buffer = NULL;
unsigned int *di_child_buff = NULL; __le32 *di_child_buff = NULL;
unsigned int *di_block_start_addr = NULL; __le32 *di_block_start_addr = NULL;
unsigned int *di_child_buff_start = NULL; __le32 *di_child_buff_start = NULL;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
if (*total_remaining_blocks != 0) { if (*total_remaining_blocks != 0) {
@@ -1205,7 +1239,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
goto fail; goto fail;
di_child_buff_start = di_child_buff; di_child_buff_start = di_child_buff;
*di_parent_buffer = di_blockno_child; *di_parent_buffer = cpu_to_le32(di_blockno_child);
di_parent_buffer++; di_parent_buffer++;
(*no_blks_reqd)++; (*no_blks_reqd)++;
debug("DICB %ld: %u\n", di_blockno_child, debug("DICB %ld: %u\n", di_blockno_child,
@@ -1228,7 +1262,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
printf("no block left to assign\n"); printf("no block left to assign\n");
goto fail; goto fail;
} }
*di_child_buff = actual_block_no; *di_child_buff = cpu_to_le32(actual_block_no);
debug("DIAB %ld: %u\n", actual_block_no, debug("DIAB %ld: %u\n", actual_block_no,
*total_remaining_blocks); *total_remaining_blocks);
@@ -1248,7 +1282,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
} }
put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)), put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
di_block_start_addr, fs->blksz); di_block_start_addr, fs->blksz);
file_inode->b.blocks.double_indir_block = di_blockno_parent; file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
} }
fail: fail:
free(di_block_start_addr); free(di_block_start_addr);
@@ -1266,12 +1300,12 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
long int ti_gp_blockno; long int ti_gp_blockno;
long int ti_parent_blockno; long int ti_parent_blockno;
long int ti_child_blockno; long int ti_child_blockno;
unsigned int *ti_gp_buff = NULL; __le32 *ti_gp_buff = NULL;
unsigned int *ti_parent_buff = NULL; __le32 *ti_parent_buff = NULL;
unsigned int *ti_child_buff = NULL; __le32 *ti_child_buff = NULL;
unsigned int *ti_gp_buff_start_addr = NULL; __le32 *ti_gp_buff_start_addr = NULL;
unsigned int *ti_pbuff_start_addr = NULL; __le32 *ti_pbuff_start_addr = NULL;
unsigned int *ti_cbuff_start_addr = NULL; __le32 *ti_cbuff_start_addr = NULL;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
if (*total_remaining_blocks != 0) { if (*total_remaining_blocks != 0) {
/* triple indirect grand parent block connecting to inode */ /* triple indirect grand parent block connecting to inode */
@@ -1301,7 +1335,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
goto fail; goto fail;
ti_pbuff_start_addr = ti_parent_buff; ti_pbuff_start_addr = ti_parent_buff;
*ti_gp_buff = ti_parent_blockno; *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
ti_gp_buff++; ti_gp_buff++;
(*no_blks_reqd)++; (*no_blks_reqd)++;
debug("TIPB %ld: %u\n", ti_parent_blockno, debug("TIPB %ld: %u\n", ti_parent_blockno,
@@ -1319,7 +1353,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
goto fail1; goto fail1;
ti_cbuff_start_addr = ti_child_buff; ti_cbuff_start_addr = ti_child_buff;
*ti_parent_buff = ti_child_blockno; *ti_parent_buff = cpu_to_le32(ti_child_blockno);
ti_parent_buff++; ti_parent_buff++;
(*no_blks_reqd)++; (*no_blks_reqd)++;
debug("TICB %ld: %u\n", ti_parent_blockno, debug("TICB %ld: %u\n", ti_parent_blockno,
@@ -1335,7 +1369,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
free(ti_cbuff_start_addr); free(ti_cbuff_start_addr);
goto fail1; goto fail1;
} }
*ti_child_buff = actual_block_no; *ti_child_buff = cpu_to_le32(actual_block_no);
debug("TIAB %ld: %u\n", actual_block_no, debug("TIAB %ld: %u\n", actual_block_no,
*total_remaining_blocks); *total_remaining_blocks);
@@ -1364,7 +1398,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
/* write the grand parent block */ /* write the grand parent block */
put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)), put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
ti_gp_buff_start_addr, fs->blksz); ti_gp_buff_start_addr, fs->blksz);
file_inode->b.blocks.triple_indir_block = ti_gp_blockno; file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
free(ti_gp_buff_start_addr); free(ti_gp_buff_start_addr);
return; return;
} }
@@ -1389,7 +1423,7 @@ void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
printf("no block left to assign\n"); printf("no block left to assign\n");
return; return;
} }
file_inode->b.blocks.dir_blocks[i] = direct_blockno; file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks); debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
total_remaining_blocks--; total_remaining_blocks--;
@@ -1420,7 +1454,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block
index = (struct ext4_extent_idx *)(ext_block + 1); index = (struct ext4_extent_idx *)(ext_block + 1);
if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC) if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
return 0; return NULL;
if (ext_block->eh_depth == 0) if (ext_block->eh_depth == 0)
return ext_block; return ext_block;
@@ -1432,7 +1466,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block
} while (fileblock >= le32_to_cpu(index[i].ei_block)); } while (fileblock >= le32_to_cpu(index[i].ei_block));
if (--i < 0) if (--i < 0)
return 0; return NULL;
block = le16_to_cpu(index[i].ei_leaf_hi); block = le16_to_cpu(index[i].ei_leaf_hi);
block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo); block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
@@ -1441,7 +1475,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block
buf)) buf))
ext_block = (struct ext4_extent_header *)buf; ext_block = (struct ext4_extent_header *)buf;
else else
return 0; return NULL;
} }
} }
@@ -2034,11 +2068,11 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)
if (!diro->inode_read) { if (!diro->inode_read) {
status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
if (status == 0) if (status == 0)
return 0; return NULL;
} }
symlink = zalloc(le32_to_cpu(diro->inode.size) + 1); symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
if (!symlink) if (!symlink)
return 0; return NULL;
if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) { if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
strncpy(symlink, diro->inode.b.symlink, strncpy(symlink, diro->inode.b.symlink,
@@ -2049,7 +2083,7 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)
symlink, &actread); symlink, &actread);
if ((status < 0) || (actread == 0)) { if ((status < 0) || (actread == 0)) {
free(symlink); free(symlink);
return 0; return NULL;
} }
} }
symlink[le32_to_cpu(diro->inode.size)] = '\0'; symlink[le32_to_cpu(diro->inode.size)] = '\0';
@@ -2234,7 +2268,7 @@ int ext4fs_mount(unsigned part_length)
* and we do not support metadata_csum (and cannot reliably find * and we do not support metadata_csum (and cannot reliably find
* files when it is set. Refuse to mount. * files when it is set. Refuse to mount.
*/ */
if (data->sblock.feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) { if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n"); printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
goto fail; goto fail;
} }

View File

@@ -59,10 +59,10 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
#if defined(CONFIG_EXT4_WRITE) #if defined(CONFIG_EXT4_WRITE)
uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n); uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
int ext4fs_checksum_update(unsigned int i); uint16_t ext4fs_checksum_update(unsigned int i);
int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags); int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags);
void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type); void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type);
long int ext4fs_get_new_blk_no(void); uint32_t ext4fs_get_new_blk_no(void);
int ext4fs_get_new_inode_no(void); int ext4fs_get_new_inode_no(void);
void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer,
int index); int index);

View File

@@ -151,7 +151,7 @@ int ext4fs_log_gdt(char *gd_table)
* journal_buffer -- Buffer containing meta data * journal_buffer -- Buffer containing meta data
* blknr -- Block number on disk of the meta data buffer * blknr -- Block number on disk of the meta data buffer
*/ */
int ext4fs_log_journal(char *journal_buffer, long int blknr) int ext4fs_log_journal(char *journal_buffer, uint32_t blknr)
{ {
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
short i; short i;
@@ -183,7 +183,7 @@ int ext4fs_log_journal(char *journal_buffer, long int blknr)
* metadata_buffer -- Buffer containing meta data * metadata_buffer -- Buffer containing meta data
* blknr -- Block number on disk of the meta data buffer * blknr -- Block number on disk of the meta data buffer
*/ */
int ext4fs_put_metadata(char *metadata_buffer, long int blknr) int ext4fs_put_metadata(char *metadata_buffer, uint32_t blknr)
{ {
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
if (!metadata_buffer) { if (!metadata_buffer) {
@@ -215,7 +215,7 @@ void print_revoke_blks(char *revk_blk)
printf("total bytes %d\n", max); printf("total bytes %d\n", max);
while (offset < max) { while (offset < max) {
blocknr = be32_to_cpu(*((long int *)(revk_blk + offset))); blocknr = be32_to_cpu(*((__be32 *)(revk_blk + offset)));
printf("revoke blknr is %ld\n", blocknr); printf("revoke blknr is %ld\n", blocknr);
offset += 4; offset += 4;
} }
@@ -302,7 +302,7 @@ int check_blknr_for_revoke(long int blknr, int sequence_no)
max = be32_to_cpu(header->r_count); max = be32_to_cpu(header->r_count);
while (offset < max) { while (offset < max) {
blocknr = be32_to_cpu(*((long int *) blocknr = be32_to_cpu(*((__be32 *)
(revk_blk + offset))); (revk_blk + offset)));
if (blocknr == blknr) if (blocknr == blknr)
goto found; goto found;
@@ -420,7 +420,7 @@ int ext4fs_check_journal_state(int recovery_flag)
temp_buff); temp_buff);
jsb = (struct journal_superblock_t *) temp_buff; jsb = (struct journal_superblock_t *) temp_buff;
if (fs->sb->feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) { if (le32_to_cpu(fs->sb->feature_incompat) & EXT3_FEATURE_INCOMPAT_RECOVER) {
if (recovery_flag == RECOVER) if (recovery_flag == RECOVER)
printf("Recovery required\n"); printf("Recovery required\n");
} else { } else {
@@ -517,11 +517,14 @@ int ext4fs_check_journal_state(int recovery_flag)
end: end:
if (recovery_flag == RECOVER) { if (recovery_flag == RECOVER) {
uint32_t new_feature_incompat;
jsb->s_start = cpu_to_be32(1); jsb->s_start = cpu_to_be32(1);
jsb->s_sequence = cpu_to_be32(be32_to_cpu(jsb->s_sequence) + 1); jsb->s_sequence = cpu_to_be32(be32_to_cpu(jsb->s_sequence) + 1);
/* get the superblock */ /* get the superblock */
ext4_read_superblock((char *)fs->sb); ext4_read_superblock((char *)fs->sb);
fs->sb->feature_incompat |= EXT3_FEATURE_INCOMPAT_RECOVER; new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
new_feature_incompat |= EXT3_FEATURE_INCOMPAT_RECOVER;
fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
/* Update the super block */ /* Update the super block */
put_ext4((uint64_t) (SUPERBLOCK_SIZE), put_ext4((uint64_t) (SUPERBLOCK_SIZE),

View File

@@ -115,8 +115,8 @@ extern struct ext2_data *ext4fs_root;
int ext4fs_init_journal(void); int ext4fs_init_journal(void);
int ext4fs_log_gdt(char *gd_table); int ext4fs_log_gdt(char *gd_table);
int ext4fs_check_journal_state(int recovery_flag); int ext4fs_check_journal_state(int recovery_flag);
int ext4fs_log_journal(char *journal_buffer, long int blknr); int ext4fs_log_journal(char *journal_buffer, uint32_t blknr);
int ext4fs_put_metadata(char *metadata_buffer, long int blknr); int ext4fs_put_metadata(char *metadata_buffer, uint32_t blknr);
void ext4fs_update_journal(void); void ext4fs_update_journal(void);
void ext4fs_dump_metadata(void); void ext4fs_dump_metadata(void);
void ext4fs_push_revoke_blk(char *buffer); void ext4fs_push_revoke_blk(char *buffer);

View File

@@ -28,6 +28,26 @@
#include <div64.h> #include <div64.h>
#include "ext4_common.h" #include "ext4_common.h"
static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock *sb)
{
sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) + 1);
}
static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock *sb)
{
sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) + 1);
}
static inline void ext4fs_bg_free_inodes_inc(struct ext2_block_group *bg)
{
bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) + 1);
}
static inline void ext4fs_bg_free_blocks_inc(struct ext2_block_group *bg)
{
bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) + 1);
}
static void ext4fs_update(void) static void ext4fs_update(void)
{ {
short i; short i;
@@ -40,14 +60,14 @@ static void ext4fs_update(void)
/* update block groups */ /* update block groups */
for (i = 0; i < fs->no_blkgrp; i++) { for (i = 0; i < fs->no_blkgrp; i++) {
fs->bgd[i].bg_checksum = ext4fs_checksum_update(i); fs->bgd[i].bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
put_ext4((uint64_t)((uint64_t)fs->bgd[i].block_id * (uint64_t)fs->blksz), put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].block_id) * fs->blksz,
fs->blk_bmaps[i], fs->blksz); fs->blk_bmaps[i], fs->blksz);
} }
/* update inode table groups */ /* update inode table groups */
for (i = 0; i < fs->no_blkgrp; i++) { for (i = 0; i < fs->no_blkgrp; i++) {
put_ext4((uint64_t) ((uint64_t)fs->bgd[i].inode_id * (uint64_t)fs->blksz), put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].inode_id) * fs->blksz,
fs->inode_bmaps[i], fs->blksz); fs->inode_bmaps[i], fs->blksz);
} }
@@ -99,11 +119,11 @@ static void delete_single_indirect_block(struct ext2_inode *inode)
{ {
struct ext2_block_group *bgd = NULL; struct ext2_block_group *bgd = NULL;
static int prev_bg_bmap_idx = -1; static int prev_bg_bmap_idx = -1;
long int blknr; uint32_t blknr;
int remainder; int remainder;
int bg_idx; int bg_idx;
int status; int status;
unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
char *journal_buffer = zalloc(fs->blksz); char *journal_buffer = zalloc(fs->blksz);
if (!journal_buffer) { if (!journal_buffer) {
@@ -115,8 +135,8 @@ static void delete_single_indirect_block(struct ext2_inode *inode)
/* deleting the single indirect block associated with inode */ /* deleting the single indirect block associated with inode */
if (inode->b.blocks.indir_block != 0) { if (inode->b.blocks.indir_block != 0) {
debug("SIPB releasing %u\n", inode->b.blocks.indir_block); blknr = le32_to_cpu(inode->b.blocks.indir_block);
blknr = inode->b.blocks.indir_block; debug("SIPB releasing %u\n", blknr);
bg_idx = blknr / blk_per_grp; bg_idx = blknr / blk_per_grp;
if (fs->blksz == 1024) { if (fs->blksz == 1024) {
remainder = blknr % blk_per_grp; remainder = blknr % blk_per_grp;
@@ -124,18 +144,18 @@ static void delete_single_indirect_block(struct ext2_inode *inode)
bg_idx--; bg_idx--;
} }
ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx); ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
status = status = ext4fs_devread(
ext4fs_devread((lbaint_t)bgd[bg_idx].block_id * (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
fs->sect_perblk, 0, fs->blksz, fs->sect_perblk, 0, fs->blksz,
journal_buffer); journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal if (ext4fs_log_journal
(journal_buffer, bgd[bg_idx].block_id)) (journal_buffer, le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
@@ -149,12 +169,12 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
int i; int i;
short status; short status;
static int prev_bg_bmap_idx = -1; static int prev_bg_bmap_idx = -1;
long int blknr; uint32_t blknr;
int remainder; int remainder;
int bg_idx; int bg_idx;
unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
unsigned int *di_buffer = NULL; __le32 *di_buffer = NULL;
unsigned int *DIB_start_addr = NULL; void *dib_start_addr = NULL;
struct ext2_block_group *bgd = NULL; struct ext2_block_group *bgd = NULL;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
char *journal_buffer = zalloc(fs->blksz); char *journal_buffer = zalloc(fs->blksz);
@@ -171,8 +191,8 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
printf("No memory\n"); printf("No memory\n");
return; return;
} }
DIB_start_addr = (unsigned int *)di_buffer; dib_start_addr = di_buffer;
blknr = inode->b.blocks.double_indir_block; blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
fs->blksz, (char *)di_buffer); fs->blksz, (char *)di_buffer);
for (i = 0; i < fs->blksz / sizeof(int); i++) { for (i = 0; i < fs->blksz / sizeof(int); i++) {
@@ -180,21 +200,21 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
break; break;
debug("DICB releasing %u\n", *di_buffer); debug("DICB releasing %u\n", *di_buffer);
bg_idx = *di_buffer / blk_per_grp; bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp;
if (fs->blksz == 1024) { if (fs->blksz == 1024) {
remainder = *di_buffer % blk_per_grp; remainder = le32_to_cpu(*di_buffer) % blk_per_grp;
if (!remainder) if (!remainder)
bg_idx--; bg_idx--;
} }
ext4fs_reset_block_bmap(*di_buffer, ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
fs->blk_bmaps[bg_idx], bg_idx); fs->blk_bmaps[bg_idx], bg_idx);
di_buffer++; di_buffer++;
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
status = ext4fs_devread((lbaint_t) status = ext4fs_devread(
bgd[bg_idx].block_id (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
* fs->sect_perblk, 0, * fs->sect_perblk, 0,
fs->blksz, fs->blksz,
journal_buffer); journal_buffer);
@@ -202,14 +222,14 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
} }
/* removing the parent double indirect block */ /* removing the parent double indirect block */
blknr = inode->b.blocks.double_indir_block; blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
bg_idx = blknr / blk_per_grp; bg_idx = blknr / blk_per_grp;
if (fs->blksz == 1024) { if (fs->blksz == 1024) {
remainder = blknr % blk_per_grp; remainder = blknr % blk_per_grp;
@@ -217,26 +237,26 @@ static void delete_double_indirect_block(struct ext2_inode *inode)
bg_idx--; bg_idx--;
} }
ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx); ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id * status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
fs->sect_perblk, 0, fs->blksz, fs->sect_perblk, 0, fs->blksz,
journal_buffer); journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
debug("DIPB releasing %ld\n", blknr); debug("DIPB releasing %d\n", blknr);
} }
fail: fail:
free(DIB_start_addr); free(dib_start_addr);
free(journal_buffer); free(journal_buffer);
} }
@@ -245,14 +265,14 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
int i, j; int i, j;
short status; short status;
static int prev_bg_bmap_idx = -1; static int prev_bg_bmap_idx = -1;
long int blknr; uint32_t blknr;
int remainder; int remainder;
int bg_idx; int bg_idx;
unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
unsigned int *tigp_buffer = NULL; __le32 *tigp_buffer = NULL;
unsigned int *tib_start_addr = NULL; void *tib_start_addr = NULL;
unsigned int *tip_buffer = NULL; __le32 *tip_buffer = NULL;
unsigned int *tipb_start_addr = NULL; void *tipb_start_addr = NULL;
struct ext2_block_group *bgd = NULL; struct ext2_block_group *bgd = NULL;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
char *journal_buffer = zalloc(fs->blksz); char *journal_buffer = zalloc(fs->blksz);
@@ -269,8 +289,8 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
printf("No memory\n"); printf("No memory\n");
return; return;
} }
tib_start_addr = (unsigned int *)tigp_buffer; tib_start_addr = tigp_buffer;
blknr = inode->b.blocks.triple_indir_block; blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
fs->blksz, (char *)tigp_buffer); fs->blksz, (char *)tigp_buffer);
for (i = 0; i < fs->blksz / sizeof(int); i++) { for (i = 0; i < fs->blksz / sizeof(int); i++) {
@@ -281,33 +301,32 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
tip_buffer = zalloc(fs->blksz); tip_buffer = zalloc(fs->blksz);
if (!tip_buffer) if (!tip_buffer)
goto fail; goto fail;
tipb_start_addr = (unsigned int *)tip_buffer; tipb_start_addr = tip_buffer;
status = ext4fs_devread((lbaint_t)(*tigp_buffer) * status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) *
fs->sect_perblk, 0, fs->blksz, fs->sect_perblk, 0, fs->blksz,
(char *)tip_buffer); (char *)tip_buffer);
for (j = 0; j < fs->blksz / sizeof(int); j++) { for (j = 0; j < fs->blksz / sizeof(int); j++) {
if (*tip_buffer == 0) if (le32_to_cpu(*tip_buffer) == 0)
break; break;
bg_idx = *tip_buffer / blk_per_grp; bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp;
if (fs->blksz == 1024) { if (fs->blksz == 1024) {
remainder = *tip_buffer % blk_per_grp; remainder = le32_to_cpu(*tip_buffer) % blk_per_grp;
if (!remainder) if (!remainder)
bg_idx--; bg_idx--;
} }
ext4fs_reset_block_bmap(*tip_buffer, ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer),
fs->blk_bmaps[bg_idx], fs->blk_bmaps[bg_idx],
bg_idx); bg_idx);
tip_buffer++; tip_buffer++;
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
status = status =
ext4fs_devread( ext4fs_devread(
(lbaint_t) (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
bgd[bg_idx].block_id *
fs->sect_perblk, 0, fs->sect_perblk, 0,
fs->blksz, fs->blksz,
journal_buffer); journal_buffer);
@@ -315,8 +334,7 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx]. le32_to_cpu(bgd[bg_idx].block_id)))
block_id))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
@@ -328,38 +346,38 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
* removing the grand parent blocks * removing the grand parent blocks
* which is connected to inode * which is connected to inode
*/ */
bg_idx = *tigp_buffer / blk_per_grp; bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp;
if (fs->blksz == 1024) { if (fs->blksz == 1024) {
remainder = *tigp_buffer % blk_per_grp; remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp;
if (!remainder) if (!remainder)
bg_idx--; bg_idx--;
} }
ext4fs_reset_block_bmap(*tigp_buffer, ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer),
fs->blk_bmaps[bg_idx], bg_idx); fs->blk_bmaps[bg_idx], bg_idx);
tigp_buffer++; tigp_buffer++;
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = status =
ext4fs_devread((lbaint_t) ext4fs_devread(
bgd[bg_idx].block_id * (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
fs->sect_perblk, 0, fs->sect_perblk, 0,
fs->blksz, journal_buffer); fs->blksz, journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
} }
/* removing the grand parent triple indirect block */ /* removing the grand parent triple indirect block */
blknr = inode->b.blocks.triple_indir_block; blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
bg_idx = blknr / blk_per_grp; bg_idx = blknr / blk_per_grp;
if (fs->blksz == 1024) { if (fs->blksz == 1024) {
remainder = blknr % blk_per_grp; remainder = blknr % blk_per_grp;
@@ -367,23 +385,24 @@ static void delete_triple_indirect_block(struct ext2_inode *inode)
bg_idx--; bg_idx--;
} }
ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx); ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id * status = ext4fs_devread(
(lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
fs->sect_perblk, 0, fs->blksz, fs->sect_perblk, 0, fs->blksz,
journal_buffer); journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
debug("tigp buffer itself releasing %ld\n", blknr); debug("tigp buffer itself releasing %d\n", blknr);
} }
fail: fail:
free(tib_start_addr); free(tib_start_addr);
@@ -402,14 +421,14 @@ static int ext4fs_delete_file(int inodeno)
int ibmap_idx; int ibmap_idx;
char *read_buffer = NULL; char *read_buffer = NULL;
char *start_block_address = NULL; char *start_block_address = NULL;
unsigned int no_blocks; uint32_t no_blocks;
static int prev_bg_bmap_idx = -1; static int prev_bg_bmap_idx = -1;
unsigned int inodes_per_block; unsigned int inodes_per_block;
long int blkno; uint32_t blkno;
unsigned int blkoff; unsigned int blkoff;
unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
unsigned int inode_per_grp = ext4fs_root->sblock.inodes_per_group; uint32_t inode_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
struct ext2_inode *inode_buffer = NULL; struct ext2_inode *inode_buffer = NULL;
struct ext2_block_group *bgd = NULL; struct ext2_block_group *bgd = NULL;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
@@ -423,8 +442,8 @@ static int ext4fs_delete_file(int inodeno)
goto fail; goto fail;
/* read the block no allocated to a file */ /* read the block no allocated to a file */
no_blocks = inode.size / fs->blksz; no_blocks = le32_to_cpu(inode.size) / fs->blksz;
if (inode.size % fs->blksz) if (le32_to_cpu(inode.size) % fs->blksz)
no_blocks++; no_blocks++;
if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) { if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
@@ -450,20 +469,20 @@ static int ext4fs_delete_file(int inodeno)
debug("EXT4_EXTENTS Block releasing %ld: %d\n", debug("EXT4_EXTENTS Block releasing %ld: %d\n",
blknr, bg_idx); blknr, bg_idx);
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
status = status =
ext4fs_devread((lbaint_t) ext4fs_devread(
bgd[bg_idx].block_id * (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
fs->sect_perblk, 0, fs->sect_perblk, 0,
fs->blksz, journal_buffer); fs->blksz, journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
@@ -479,8 +498,8 @@ static int ext4fs_delete_file(int inodeno)
delete_triple_indirect_block(&inode); delete_triple_indirect_block(&inode);
/* read the block no allocated to a file */ /* read the block no allocated to a file */
no_blocks = inode.size / fs->blksz; no_blocks = le32_to_cpu(inode.size) / fs->blksz;
if (inode.size % fs->blksz) if (le32_to_cpu(inode.size) % fs->blksz)
no_blocks++; no_blocks++;
for (i = 0; i < no_blocks; i++) { for (i = 0; i < no_blocks; i++) {
blknr = read_allocated_block(&inode, i); blknr = read_allocated_block(&inode, i);
@@ -494,20 +513,20 @@ static int ext4fs_delete_file(int inodeno)
bg_idx); bg_idx);
debug("ActualB releasing %ld: %d\n", blknr, bg_idx); debug("ActualB releasing %ld: %d\n", blknr, bg_idx);
bgd[bg_idx].free_blocks++; ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
fs->sb->free_blocks++; ext4fs_sb_free_blocks_inc(fs->sb);
/* journal backup */ /* journal backup */
if (prev_bg_bmap_idx != bg_idx) { if (prev_bg_bmap_idx != bg_idx) {
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = ext4fs_devread((lbaint_t) status = ext4fs_devread(
bgd[bg_idx].block_id (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
* fs->sect_perblk, * fs->sect_perblk,
0, fs->blksz, 0, fs->blksz,
journal_buffer); journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, if (ext4fs_log_journal(journal_buffer,
bgd[bg_idx].block_id)) le32_to_cpu(bgd[bg_idx].block_id)))
goto fail; goto fail;
prev_bg_bmap_idx = bg_idx; prev_bg_bmap_idx = bg_idx;
} }
@@ -521,7 +540,7 @@ static int ext4fs_delete_file(int inodeno)
/* get the block no */ /* get the block no */
inodeno--; inodeno--;
blkno = le32_to_cpu(bgd[ibmap_idx].inode_table_id) + blkno = le32_to_cpu(bgd[ibmap_idx].inode_table_id) +
(inodeno % le32_to_cpu(inode_per_grp)) / inodes_per_block; (inodeno % inode_per_grp) / inodes_per_block;
/* get the offset of the inode */ /* get the offset of the inode */
blkoff = ((inodeno) % inodes_per_block) * fs->inodesz; blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
@@ -550,15 +569,15 @@ static int ext4fs_delete_file(int inodeno)
/* update the respective inode bitmaps */ /* update the respective inode bitmaps */
inodeno++; inodeno++;
ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx); ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
bgd[ibmap_idx].free_inodes++; ext4fs_bg_free_inodes_inc(&bgd[ibmap_idx]);
fs->sb->free_inodes++; ext4fs_sb_free_inodes_inc(fs->sb);
/* journal backup */ /* journal backup */
memset(journal_buffer, '\0', fs->blksz); memset(journal_buffer, '\0', fs->blksz);
status = ext4fs_devread((lbaint_t)bgd[ibmap_idx].inode_id * status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id) *
fs->sect_perblk, 0, fs->blksz, journal_buffer); fs->sect_perblk, 0, fs->blksz, journal_buffer);
if (status == 0) if (status == 0)
goto fail; goto fail;
if (ext4fs_log_journal(journal_buffer, bgd[ibmap_idx].inode_id)) if (ext4fs_log_journal(journal_buffer, le32_to_cpu(bgd[ibmap_idx].inode_id)))
goto fail; goto fail;
ext4fs_update(); ext4fs_update();
@@ -585,7 +604,7 @@ int ext4fs_init(void)
{ {
short status; short status;
int i; int i;
unsigned int real_free_blocks = 0; uint32_t real_free_blocks = 0;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
/* populate fs */ /* populate fs */
@@ -606,9 +625,9 @@ int ext4fs_init(void)
/* get total no of blockgroups */ /* get total no of blockgroups */
fs->no_blkgrp = (uint32_t)ext4fs_div_roundup( fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
(ext4fs_root->sblock.total_blocks - le32_to_cpu(ext4fs_root->sblock.total_blocks)
ext4fs_root->sblock.first_data_block), - le32_to_cpu(ext4fs_root->sblock.first_data_block),
ext4fs_root->sblock.blocks_per_group); le32_to_cpu(ext4fs_root->sblock.blocks_per_group));
/* get the block group descriptor table */ /* get the block group descriptor table */
fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1); fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
@@ -630,7 +649,8 @@ int ext4fs_init(void)
for (i = 0; i < fs->no_blkgrp; i++) { for (i = 0; i < fs->no_blkgrp; i++) {
status = status =
ext4fs_devread((lbaint_t)fs->bgd[i].block_id * ext4fs_devread(
(lbaint_t)le32_to_cpu(fs->bgd[i].block_id) *
fs->sect_perblk, 0, fs->sect_perblk, 0,
fs->blksz, (char *)fs->blk_bmaps[i]); fs->blksz, (char *)fs->blk_bmaps[i]);
if (status == 0) if (status == 0)
@@ -648,7 +668,8 @@ int ext4fs_init(void)
} }
for (i = 0; i < fs->no_blkgrp; i++) { for (i = 0; i < fs->no_blkgrp; i++) {
status = ext4fs_devread((lbaint_t)fs->bgd[i].inode_id * status = ext4fs_devread(
(lbaint_t)le32_to_cpu(fs->bgd[i].inode_id) *
fs->sect_perblk, fs->sect_perblk,
0, fs->blksz, 0, fs->blksz,
(char *)fs->inode_bmaps[i]); (char *)fs->inode_bmaps[i]);
@@ -663,9 +684,9 @@ int ext4fs_init(void)
* reboot of a linux kernel * reboot of a linux kernel
*/ */
for (i = 0; i < fs->no_blkgrp; i++) for (i = 0; i < fs->no_blkgrp; i++)
real_free_blocks = real_free_blocks + fs->bgd[i].free_blocks; real_free_blocks = real_free_blocks + le16_to_cpu(fs->bgd[i].free_blocks);
if (real_free_blocks != fs->sb->free_blocks) if (real_free_blocks != le32_to_cpu(fs->sb->free_blocks))
fs->sb->free_blocks = real_free_blocks; fs->sb->free_blocks = cpu_to_le32(real_free_blocks);
return 0; return 0;
fail: fail:
@@ -679,8 +700,9 @@ void ext4fs_deinit(void)
int i; int i;
struct ext2_inode inode_journal; struct ext2_inode inode_journal;
struct journal_superblock_t *jsb; struct journal_superblock_t *jsb;
long int blknr; uint32_t blknr;
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
uint32_t new_feature_incompat;
/* free journal */ /* free journal */
char *temp_buff = zalloc(fs->blksz); char *temp_buff = zalloc(fs->blksz);
@@ -692,7 +714,7 @@ void ext4fs_deinit(void)
ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz, ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
temp_buff); temp_buff);
jsb = (struct journal_superblock_t *)temp_buff; jsb = (struct journal_superblock_t *)temp_buff;
jsb->s_start = cpu_to_be32(0); jsb->s_start = 0;
put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
(struct journal_superblock_t *)temp_buff, fs->blksz); (struct journal_superblock_t *)temp_buff, fs->blksz);
free(temp_buff); free(temp_buff);
@@ -701,7 +723,9 @@ void ext4fs_deinit(void)
/* get the superblock */ /* get the superblock */
ext4_read_superblock((char *)fs->sb); ext4_read_superblock((char *)fs->sb);
fs->sb->feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER; new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
put_ext4((uint64_t)(SUPERBLOCK_SIZE), put_ext4((uint64_t)(SUPERBLOCK_SIZE),
(struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE); (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
free(fs->sb); free(fs->sb);
@@ -744,7 +768,7 @@ static int ext4fs_write_file(struct ext2_inode *file_inode,
{ {
int i; int i;
int blockcnt; int blockcnt;
unsigned int filesize = le32_to_cpu(file_inode->size); uint32_t filesize = le32_to_cpu(file_inode->size);
struct ext_filesystem *fs = get_fs(); struct ext_filesystem *fs = get_fs();
int log2blksz = fs->dev_desc->log2blksz; int log2blksz = fs->dev_desc->log2blksz;
int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz; int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz;
@@ -878,7 +902,7 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
} }
blocks_remaining = blks_reqd_for_file; blocks_remaining = blks_reqd_for_file;
/* test for available space in partition */ /* test for available space in partition */
if (fs->sb->free_blocks < blks_reqd_for_file) { if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) {
printf("Not enough space on partition !!!\n"); printf("Not enough space on partition !!!\n");
goto fail; goto fail;
} }
@@ -889,25 +913,25 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
if (!inode_buffer) if (!inode_buffer)
goto fail; goto fail;
file_inode = (struct ext2_inode *)inode_buffer; file_inode = (struct ext2_inode *)inode_buffer;
file_inode->mode = S_IFREG | S_IRWXU | file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU |
S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH; S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
/* ToDo: Update correct time */ /* ToDo: Update correct time */
file_inode->mtime = timestamp; file_inode->mtime = cpu_to_le32(timestamp);
file_inode->atime = timestamp; file_inode->atime = cpu_to_le32(timestamp);
file_inode->ctime = timestamp; file_inode->ctime = cpu_to_le32(timestamp);
file_inode->nlinks = 1; file_inode->nlinks = cpu_to_le16(1);
file_inode->size = sizebytes; file_inode->size = cpu_to_le32(sizebytes);
/* Allocate data blocks */ /* Allocate data blocks */
ext4fs_allocate_blocks(file_inode, blocks_remaining, ext4fs_allocate_blocks(file_inode, blocks_remaining,
&blks_reqd_for_file); &blks_reqd_for_file);
file_inode->blockcnt = (blks_reqd_for_file * fs->blksz) >> file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >>
fs->dev_desc->log2blksz; fs->dev_desc->log2blksz);
temp_ptr = zalloc(fs->blksz); temp_ptr = zalloc(fs->blksz);
if (!temp_ptr) if (!temp_ptr)
goto fail; goto fail;
ibmap_idx = inodeno / ext4fs_root->sblock.inodes_per_group; ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
inodeno--; inodeno--;
itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) + itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
(inodeno % le32_to_cpu(sblock->inodes_per_group)) / (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
@@ -926,7 +950,7 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
printf("Error in copying content\n"); printf("Error in copying content\n");
goto fail; goto fail;
} }
ibmap_idx = parent_inodeno / ext4fs_root->sblock.inodes_per_group; ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
parent_inodeno--; parent_inodeno--;
parent_itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) + parent_itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
(parent_inodeno % (parent_inodeno %

View File

@@ -52,7 +52,7 @@
#define LOG2_BLOCK_SIZE(data) (le32_to_cpu \ #define LOG2_BLOCK_SIZE(data) (le32_to_cpu \
(data->sblock.log2_block_size) \ (data->sblock.log2_block_size) \
+ EXT2_MIN_BLOCK_LOG_SIZE) + EXT2_MIN_BLOCK_LOG_SIZE)
#define INODE_SIZE_FILESYSTEM(data) (le32_to_cpu \ #define INODE_SIZE_FILESYSTEM(data) (le16_to_cpu \
(data->sblock.inode_size)) (data->sblock.inode_size))
#define EXT2_FT_DIR 2 #define EXT2_FT_DIR 2