ext4: change structure fields to __le/__be types
PATCH 1/4 ext4: change structure fields to __le/__be types
Change all the types of ext2/4 fields to little endian types and all the JBD fields to big endian types. Now we can use sparse (make C=1) to check for statements where we need byteswaps.
--- fs/ext4/ext4_journal.h | 40 +++++++-------- include/ext_common.h | 136 ++++++++++++++++++++++++------------------------- 2 files changed, 88 insertions(+), 88 deletions(-)
fs/ext4/ext4_journal.h +20 -20
@@ -49,9 +49,9 @@ struct dirty_blocks {/* Standard header for all descriptor blocks: */ struct journal_header_t { -__u32 h_magic; -__u32 h_blocktype; -__u32 h_sequence; +__be32 h_magic; +__be32 h_blocktype; +__be32 h_sequence; };/* The journal superblock. All fields are in big-endian byte order. */
@@ -60,35 +60,35 @@ struct journal_superblock_t {struct journal_header_t s_header;/* Static information describing the journal */ -__u32 s_blocksize;/* journal device blocksize */ -__u32 s_maxlen;/* total blocks in journal file */ -__u32 s_first;/* first block of log information */ +__be32 s_blocksize;/* journal device blocksize */ +__be32 s_maxlen;/* total blocks in journal file */ +__be32 s_first;/* first block of log information */ /* Dynamic information describing the current state of the log */ -__u32 s_sequence;/* first commit ID expected in log */ -__u32 s_start;/* blocknr of start of log */ +__be32 s_sequence;/* first commit ID expected in log */ +__be32 s_start;/* blocknr of start of log */ /* Error value, as set by journal_abort(). */ -__s32 s_errno; +__be32 s_errno;/* Remaining fields are only valid in a version-2 superblock */ -__u32 s_feature_compat;/* compatible feature set */ -__u32 s_feature_incompat;/* incompatible feature set */ -__u32 s_feature_ro_compat;/* readonly-compatible feature set */ +__be32 s_feature_compat;/* compatible feature set */ +__be32 s_feature_incompat;/* incompatible feature set */ +__be32 s_feature_ro_compat;/* readonly-compatible feature set */ /* 0x0030 */ __u8 s_uuid[16 ];/* 128-bit uuid for journal */ /* 0x0040 */ -__u32 s_nr_users;/* Nr of filesystems sharing log */ +__be32 s_nr_users;/* Nr of filesystems sharing log */ -__u32 s_dynsuper;/* Blocknr of dynamic superblock copy */ +__be32 s_dynsuper;/* Blocknr of dynamic superblock copy */ /* 0x0048 */ -__u32 s_max_transaction;/* Limit of journal blocks per trans. */ -__u32 s_max_trans_data;/* Limit of data blocks per trans. */ +__be32 s_max_transaction;/* Limit of journal blocks per trans. */ +__be32 s_max_trans_data;/* Limit of data blocks per trans. */ /* 0x0050 */ -__u32 s_padding[44 ]; +__be32 s_padding[44 ];/* 0x0100 */ __u8 s_users[16 *48 ];/* ids of all fs'es sharing the log */
@@ -96,13 +96,13 @@ struct journal_superblock_t { } ;struct ext3_journal_block_tag { -uint32_t block; -uint32_t flags; +__be32 block; +__be32 flags; };struct journal_revoke_header_t {struct journal_header_t r_header; -int r_count;/* Count of bytes used in the block */ +__be32 r_count;/* Count of bytes used in the block */ };struct revoke_blk_list {
include/ext_common.h +68 -68
160 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH 2/4 ext4: use kernel names for byte swaps
Instead of __{be,le}{16,32}_to_cpu use {be,le}{16,32}_to_cpu.
--- fs/ext4/ext4_common.c | 90 +++++++++++++++++++++++++-------------------------- fs/ext4/ext4_write.c | 14 ++++---- fs/ext4/ext4fs.c | 2 +- include/ext_common.h | 4 +-- 4 files changed, 55 insertions(+), 55 deletions(-)
fs/ext4/ext4_common.c +45 -45
@@ -1454,7 +1454,7 @@ static int ext4fs_blockgroup desc_per_blk =EXT2_BLOCK_SIZE (data) /sizeof (struct ext2_block_group ); - blkno =__le32_to_cpu (data->sblock.first_data_block) +1 + + blkno =le32_to_cpu (data->sblock.first_data_block) +1 + group / desc_per_blk; blkoff = (group % desc_per_blk) *sizeof (struct ext2_block_group );
@@ -1479,14 +1479,14 @@ int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)/* It is easier to calculate if the first inode is 0. */ ino--; - status =ext4fs_blockgroup (data, ino /__le32_to_cpu + status =ext4fs_blockgroup (data, ino /le32_to_cpu (sblock->inodes_per_group), &blkgrp);if (status ==0 )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 =le32_to_cpu (blkgrp.inode_table_id) + + (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) -
@@ -1559,7 +1559,7 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)/* Direct blocks. */ if (fileblock <INDIRECT_BLOCKS ) - blknr =__le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]); + blknr =le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);/* Indirect. */ else if (fileblock < (INDIRECT_BLOCKS + (blksz /4 ))) {
@@ -1586,10 +1586,10 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock) } ext4fs_indir1_size = blksz; } -if ((__le32_to_cpu (inode->b.blocks.indir_block) << +if ((le32_to_cpu (inode->b.blocks.indir_block) << log2_blksz) != ext4fs_indir1_blkno) { status = -ext4fs_devread ((lbaint_t )__le32_to_cpu +ext4fs_devread ((lbaint_t )le32_to_cpu (inode->b.blocks. indir_block) << log2_blksz,0 , blksz, (char *)ext4fs_indir1_block);
@@ -1599,10 +1599,10 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)return 0 ; } ext4fs_indir1_blkno = -__le32_to_cpu (inode->b.blocks. +le32_to_cpu (inode->b.blocks. indir_block) << log2_blksz; } - blknr =__le32_to_cpu (ext4fs_indir1_block + blknr =le32_to_cpu (ext4fs_indir1_block [fileblock -INDIRECT_BLOCKS ]); }/* Double indirect. */
@@ -1635,10 +1635,10 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock) } ext4fs_indir1_size = blksz; } -if ((__le32_to_cpu (inode->b.blocks.double_indir_block) << +if ((le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz) != ext4fs_indir1_blkno) { status = - ext4fs_devread((lbaint_t)__le32_to_cpu + ext4fs_devread((lbaint_t)le32_to_cpu (inode->b.blocks. double_indir_block) << log2_blksz, 0, blksz,
@@ -1649,7 +1649,7 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)return -1 ; } ext4fs_indir1_blkno = -__le32_to_cpu (inode->b.blocks.double_indir_block) << +le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz; }
@@ -1676,9 +1676,9 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock) } ext4fs_indir2_size = blksz; } -if ((__le32_to_cpu (ext4fs_indir1_block[rblock / perblock]) << +if ((le32_to_cpu (ext4fs_indir1_block[rblock / perblock]) << log2_blksz) != ext4fs_indir2_blkno) { - status = ext4fs_devread((lbaint_t)__le32_to_cpu + status = ext4fs_devread((lbaint_t)le32_to_cpu (ext4fs_indir1_block [rblock / perblock]) << log2_blksz, 0,
@@ -1690,12 +1690,12 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)return -1 ; } ext4fs_indir2_blkno = -__le32_to_cpu (ext4fs_indir1_block[rblock +le32_to_cpu (ext4fs_indir1_block[rblock / perblock]) << log2_blksz; } - blknr =__le32_to_cpu (ext4fs_indir2_block[rblock % perblock]); + blknr =le32_to_cpu (ext4fs_indir2_block[rblock % perblock]); }/* Tripple indirect. */ else {
@@ -1727,11 +1727,11 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock) } ext4fs_indir1_size = blksz; } -if ((__le32_to_cpu (inode->b.blocks.triple_indir_block) << +if ((le32_to_cpu (inode->b.blocks.triple_indir_block) << log2_blksz) != ext4fs_indir1_blkno) { status =ext4fs_devread ((lbaint_t ) -__le32_to_cpu (inode->b.blocks.triple_indir_block) +le32_to_cpu (inode->b.blocks.triple_indir_block) << log2_blksz,0 , blksz, (char *)ext4fs_indir1_block);if (status ==0 ) {
@@ -1740,7 +1740,7 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)return -1 ; } ext4fs_indir1_blkno = -__le32_to_cpu (inode->b.blocks.triple_indir_block) << +le32_to_cpu (inode->b.blocks.triple_indir_block) << log2_blksz; }
@@ -1767,11 +1767,11 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock) } ext4fs_indir2_size = blksz; } -if ((__le32_to_cpu (ext4fs_indir1_block[rblock / +if ((le32_to_cpu (ext4fs_indir1_block[rblock / perblock_parent]) << log2_blksz) != ext4fs_indir2_blkno) { - status = ext4fs_devread((lbaint_t)__le32_to_cpu + status = ext4fs_devread((lbaint_t)le32_to_cpu (ext4fs_indir1_block [rblock / perblock_parent]) <<
@@ -1783,7 +1783,7 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)return -1 ; } ext4fs_indir2_blkno = -__le32_to_cpu (ext4fs_indir1_block[rblock / +le32_to_cpu (ext4fs_indir1_block[rblock / perblock_parent]) << log2_blksz; }
@@ -1811,12 +1811,12 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock) } ext4fs_indir3_size = blksz; } -if ((__le32_to_cpu (ext4fs_indir2_block[rblock +if ((le32_to_cpu (ext4fs_indir2_block[rblock / perblock_child]) << log2_blksz) != ext4fs_indir3_blkno) { status = - ext4fs_devread((lbaint_t)__le32_to_cpu + ext4fs_devread((lbaint_t)le32_to_cpu (ext4fs_indir2_block [(rblock / perblock_child) % (blksz / 4)]) << log2_blksz, 0,
@@ -1827,14 +1827,14 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)return -1 ; } ext4fs_indir3_blkno = -__le32_to_cpu (ext4fs_indir2_block[(rblock / +le32_to_cpu (ext4fs_indir2_block[(rblock / perblock_child) % (blksz /4 )]) << log2_blksz; } - blknr =__le32_to_cpu (ext4fs_indir3_block + blknr =le32_to_cpu (ext4fs_indir3_block [rblock % perblock_child]); }debug ("read_allocated_block %ld\n" , blknr);
@@ -1907,7 +1907,7 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,return 0 ; }/* Search the file. */ -while (fpos <__le32_to_cpu (diro->inode.size)) { +while (fpos <le32_to_cpu (diro->inode.size)) {struct ext2_dirent dirent; status = ext4fs_read_file(diro, fpos,
@@ -1939,7 +1939,7 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,return 0 ; fdiro->data = diro->data; - fdiro->ino =__le32_to_cpu (dirent.inode); + fdiro->ino =le32_to_cpu (dirent.inode); filename[dirent.namelen] ='\0' ;
@@ -1954,7 +1954,7 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, type =FILETYPE_REG ; }else { status =ext4fs_read_inode (diro->data, -__le32_to_cpu +le32_to_cpu (dirent.inode), &fdiro->inode);if (status ==0 ) {
@@ -1963,15 +1963,15 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, } fdiro->inode_read =1 ; -if ((__le16_to_cpu (fdiro->inode.mode) & +if ((le16_to_cpu (fdiro->inode.mode) &FILETYPE_INO_MASK ) ==FILETYPE_INO_DIRECTORY ) { type =FILETYPE_DIRECTORY ; - }else if ((__le16_to_cpu (fdiro->inode.mode) + }else if ((le16_to_cpu (fdiro->inode.mode) &FILETYPE_INO_MASK ) ==FILETYPE_INO_SYMLINK ) { type =FILETYPE_SYMLINK ; - }else if ((__le16_to_cpu (fdiro->inode.mode) + }else if ((le16_to_cpu (fdiro->inode.mode) &FILETYPE_INO_MASK ) ==FILETYPE_INO_REG ) { type =FILETYPE_REG ;
@@ -1990,7 +1990,7 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, }else {if (fdiro->inode_read ==0 ) { status =ext4fs_read_inode (diro->data, -__le32_to_cpu ( +le32_to_cpu ( dirent.inode), &fdiro->inode);if (status ==0 ) {
@@ -2014,12 +2014,12 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,break ; }printf ("%10u %s\n" , -__le32_to_cpu (fdiro->inode.size), +le32_to_cpu (fdiro->inode.size), filename); }free (fdiro); } - fpos +=__le16_to_cpu (dirent.direntlen); + fpos +=le16_to_cpu (dirent.direntlen); }return 0 ; }
@@ -2036,23 +2036,23 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)if (status ==0 )return 0 ; } - symlink =zalloc (__le32_to_cpu (diro->inode.size) +1 ); + symlink =zalloc (le32_to_cpu (diro->inode.size) +1 );if (!symlink)return 0 ; -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, -__le32_to_cpu (diro->inode.size)); +le32_to_cpu (diro->inode.size)); }else { status =ext4fs_read_file (diro,0 , -__le32_to_cpu (diro->inode.size), +le32_to_cpu (diro->inode.size), symlink, &actread);if ((status <0 ) || (actread ==0 )) {free (symlink);return 0 ; } } - symlink[__le32_to_cpu (diro->inode.size)] ='\0' ; + symlink[le32_to_cpu (diro->inode.size)] ='\0' ;return symlink; }
@@ -2200,7 +2200,7 @@ int ext4fs_open(const char *filename, loff_t *len)if (status ==0 ) goto fail; } - *len =__le32_to_cpu (fdiro->inode.size); + *len =le32_to_cpu (fdiro->inode.size); ext4fs_file = fdiro;return 0 ;
@@ -2226,7 +2226,7 @@ int ext4fs_mount(unsigned part_length) goto fail;/* Make sure this is an ext2 filesystem. */ -if (__le16_to_cpu (data->sblock.magic) !=EXT2_MAGIC ) +if (le16_to_cpu (data->sblock.magic) !=EXT2_MAGIC ) goto fail;/*
@@ -2239,13 +2239,13 @@ int ext4fs_mount(unsigned part_length) goto fail; } -if (__le32_to_cpu (data->sblock.revision_level ==0 )) +if (le32_to_cpu (data->sblock.revision_level ==0 )) fs->inodesz =128 ;else - fs->inodesz =__le16_to_cpu (data->sblock.inode_size); + fs->inodesz =le16_to_cpu (data->sblock.inode_size);debug ("EXT2 rev %d, inode_size %d\n" , -__le32_to_cpu (data->sblock.revision_level), fs->inodesz); +le32_to_cpu (data->sblock.revision_level), fs->inodesz); data->diropen.data = data; data->diropen.ino =2 ;
fs/ext4/ext4_write.c +7 -7
@@ -520,8 +520,8 @@ static int ext4fs_delete_file(int inodeno)/* get the block no */ inodeno--; - blkno =__le32_to_cpu (bgd[ibmap_idx].inode_table_id) + - (inodeno %__le32_to_cpu (inode_per_grp)) / inodes_per_block; + blkno =le32_to_cpu (bgd[ibmap_idx].inode_table_id) + + (inodeno %le32_to_cpu (inode_per_grp)) / inodes_per_block;/* get the offset of the inode */ blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
@@ -744,7 +744,7 @@ static int ext4fs_write_file(struct ext2_inode *file_inode, {int i;int blockcnt; -unsigned int filesize =__le32_to_cpu (file_inode->size); +unsigned int filesize =le32_to_cpu (file_inode->size);struct ext_filesystem *fs =get_fs ();int log2blksz = fs->dev_desc->log2blksz;int log2_fs_blocksize =LOG2_BLOCK_SIZE (ext4fs_root) - log2blksz;
@@ -909,8 +909,8 @@ int ext4fs_write(const char *fname, unsigned char *buffer, goto fail; ibmap_idx = inodeno / 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)) / + itable_blkno =le32_to_cpu (fs->bgd[ibmap_idx].inode_table_id) + + (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,
@@ -928,9 +928,9 @@ int ext4fs_write(const char *fname, unsigned char *buffer, } ibmap_idx = parent_inodeno / ext4fs_root->sblock.inodes_per_group; 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 % -__le32_to_cpu (sblock->inodes_per_group)) / inodes_per_block; +le32_to_cpu (sblock->inodes_per_group)) / inodes_per_block; blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;if (parent_itable_blkno != itable_blkno) {memset (temp_ptr,'\0' , fs->blksz);
fs/ext4/ext4fs.c +1 -1
@@ -55,7 +55,7 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,int log2blksz = fs->dev_desc->log2blksz;int log2_fs_blocksize =LOG2_BLOCK_SIZE (node->data) - log2blksz;int blocksize = (1 << (log2_fs_blocksize + log2blksz)); -unsigned int filesize =__le32_to_cpu (node->inode.size); +unsigned int filesize =le32_to_cpu (node->inode.size);lbaint_t previous_block_number =-1 ;lbaint_t delayed_start =0 ;lbaint_t delayed_extent =0 ;
include/ext_common.h +2 -2
@@ -49,10 +49,10 @@#define EXT2_BLOCK_SIZE (data) (1 << LOG2_BLOCK_SIZE(data))/* Log2 size of ext2 block in bytes. */ -#define LOG2_BLOCK_SIZE (data) (__le32_to_cpu \ +#define LOG2_BLOCK_SIZE (data) (le32_to_cpu \ (data->sblock.log2_block_size) \ + EXT2_MIN_BLOCK_LOG_SIZE) -#define INODE_SIZE_FILESYSTEM (data) (__le32_to_cpu \ +#define INODE_SIZE_FILESYSTEM (data) (le32_to_cpu \ (data->sblock.inode_size))#define EXT2_FT_DIR 2
Signature · 1 line
2.1.4
RFC PATCH 3/4 ext4: fix endianess problems in ext4 write support
In reply to Michael WalleAll 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.
---
Ok this patch is huge, please comment. I know, checkpatch fails because there are longer lines than 80 characters. I don't want do be rude, but the coding style is awful :/ Eg.
http://git.denx.de/?p=u-boot.git;a=blob;f=fs/ext4/ext4_common.c;h=eb49fce04c5a290e839575945da722bc97d2f670;hb=HEAD#l440
http://git.denx.de/?p=u-boot.git;a=blob;f=fs/ext4/ext4_write.c;h=e027916763f9b52937c7fe13f1698b67b94eb901;hb=HEAD#l137
Many ugly places seems to come from the 80 characters limit. Most of the code looks like its right-aligned. I know there is a valid point for having this limit, eg refactor code into small functions. And frankly, the ext4 write code badly needs such a refactoring. But I won't and can't do it. I can resend a new version which meet the 80 character restriction, but it won't make the code easier to read. And believe me, it is already hard to do it.
Ok, back to business. Sparse really helped to find the places where the byteswaps were missing once you annotated it correctly with the __le16/__le32 types. If someone want to do check again, just run
make C=1
See Documentation/sparse.txt in the linux kernel for more information. I've done a short test, to verify I can (over)write a 700kB file. No cornertests etc.
fs/ext4/ext4_common.c | 260 ++++++++++++++++++++++++++----------------------- fs/ext4/ext4_common.h | 4 +- fs/ext4/ext4_journal.c | 15 +-- fs/ext4/ext4_journal.h | 4 +- fs/ext4/ext4_write.c | 195 +++++++++++++++++++------------------ include/ext_common.h | 2 +- 6 files changed, 251 insertions(+), 229 deletions(-)
fs/ext4/ext4_common.c +136 -124
@@ -33,19 +33,22 @@struct ext2_data *ext4fs_root;struct ext2fs_node *ext4fs_file; -uint32_t *ext4fs_indir1_block; +__le32 *ext4fs_indir1_block;int ext4fs_indir1_size;int ext4fs_indir1_blkno =-1 ; -uint32_t *ext4fs_indir2_block; +__le32 *ext4fs_indir2_block;int ext4fs_indir2_size;int ext4fs_indir2_blkno =-1 ; -uint32_t *ext4fs_indir3_block; +__le32 *ext4fs_indir3_block;int ext4fs_indir3_size;int ext4fs_indir3_blkno =-1 ;struct ext2_inode *g_parent_inode;static int symlinknest; +#define DEC_LE16 (x) (x = cpu_to_le16(le16_to_cpu(x) - 1)) +#define DEC_LE32 (x) (x = cpu_to_le32(le32_to_cpu(x) - 1)) +#if defined(CONFIG_EXT4_WRITE )uint32_t ext4fs_div_roundup (uint32_t size,uint32_t n) {
@@ -112,7 +115,7 @@ static int _get_new_inode_no(unsigned char *buffer)while (*ptr ==255 ) { ptr++; count +=8 ; -if (count > ext4fs_root->sblock.inodes_per_group) +if (count >le32_to_cpu (ext4fs_root->sblock.inodes_per_group))return -1 ; }
@@ -249,7 +252,7 @@ int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)unsigned char *ptr = buffer;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 ; remainder = inode_no %8 ;if (remainder ==0 ) {
@@ -274,7 +277,7 @@ void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)unsigned char *ptr = buffer;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 ; remainder = inode_no %8 ;if (remainder ==0 ) {
@@ -289,19 +292,20 @@ void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index) *ptr = *ptr & ~(operand); } -int ext4fs_checksum_update (unsigned int i) +uint16_t ext4fs_checksum_update (uint32_t i) {struct ext2_block_group *desc;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]; -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); crc =ext2fs_crc16 (~0 , 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); offset +=sizeof (desc->bg_checksum);/* skip checksum */ assert (offset ==sizeof (*desc));
@@ -322,7 +326,7 @@ static int check_void_in_dentry(struct ext2_dirent *dir, char *filename) dentry_length =sizeof (struct ext2_dirent ) + 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 )return 0 ;
@@ -333,7 +337,7 @@ static int check_void_in_dentry(struct ext2_dirent *dir, char *filename) new_entry_byte_reqd =strlen (filename) +sizeof (struct ext2_dirent ) + padding_factor;if (sizeof_void_space >= new_entry_byte_reqd) { - dir->direntlen = dentry_length; + dir->direntlen =cpu_to_le16 (dentry_length);return sizeof_void_space; }
@@ -360,6 +364,9 @@ void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)/* directory entry */ struct ext2_dirent *dir;char *temp_dir =NULL ; +uint32_t new_blk_no; +uint32_t new_size; +uint32_t new_blockcnt; zero_buffer =zalloc (fs->blksz);if (!zero_buffer) {
@@ -396,7 +403,7 @@ restart: goto fail; dir = (struct ext2_dirent *)root_first_block_buffer; totalbytes =0 ; -while (dir->direntlen >0 ) { +while (le16_to_cpu (dir->direntlen) >0 ) {/* * blocksize-totalbytes because last directory length * i.e. dir->direntlen is free availble space in the
@@ -405,7 +412,7 @@ restart:*/ /* 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 ) padding_factor =4 - (strlen (filename) %4 );
@@ -430,32 +437,34 @@ restart:printf ("Directory exceeds limit\n" ); goto fail; } - g_parent_inode->b.blocks.dir_blocks - [direct_blk_idx] =ext4fs_get_new_blk_no (); -if (g_parent_inode->b.blocks.dir_blocks - [direct_blk_idx] ==-1 ) { + new_blk_no =ext4fs_get_new_blk_no (); +if (new_blk_no ==-1 ) {printf ("no block left to assign\n" ); goto fail; } -put_ext4 (((uint64_t ) - ((uint64_t )g_parent_inode->b. - blocks.dir_blocks[direct_blk_idx] * - (uint64_t )fs->blksz)), zero_buffer, fs->blksz); - g_parent_inode->size = - g_parent_inode->size + fs->blksz; - g_parent_inode->blockcnt = - g_parent_inode->blockcnt + fs->sect_perblk; +put_ext4 ((uint64_t )new_blk_no * fs->blksz, zero_buffer, fs->blksz); + g_parent_inode->b.blocks.dir_blocks[direct_blk_idx] = +cpu_to_le32 (new_blk_no); + + new_size =le32_to_cpu (g_parent_inode->size); + new_size += fs->blksz; + g_parent_inode->size =cpu_to_le32 (new_size); + + 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 (root_first_block_buffer, first_block_no_of_root)) goto fail; goto restart; } - dir->direntlen = last_entry_dirlen; + dir->direntlen =cpu_to_le16 (last_entry_dirlen);break ; } - templength = dir->direntlen; + templength =le16_to_cpu (dir->direntlen); totalbytes = totalbytes + templength; sizeof_void_space =check_void_in_dentry (dir, filename);if (sizeof_void_space)
@@ -465,7 +474,7 @@ restart: }/* make a pointer ready for creating next directory entry */ - templength = dir->direntlen; + templength =le16_to_cpu (dir->direntlen); totalbytes = totalbytes + templength; dir = (struct ext2_dirent *)((char *)dir + templength);
@@ -475,11 +484,11 @@ restart:printf ("no inode left to assign\n" ); goto fail; } - dir->inode = inodeno; + dir->inode =cpu_to_le32 (inodeno);if (sizeof_void_space) - dir->direntlen = sizeof_void_space; + dir->direntlen =cpu_to_le16 (sizeof_void_space);else - dir->direntlen = fs->blksz - totalbytes; + dir->direntlen =cpu_to_le16 (fs->blksz - totalbytes); dir->namelen =strlen (filename); dir->filetype =FILETYPE_REG ;/* regular file */
@@ -534,7 +543,7 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname) dir = (struct ext2_dirent *)block_buffer; ptr = (char *)dir; totalbytes =0 ; -while (dir->direntlen >=0 ) { +while (le16_to_cpu (dir->direntlen) >=0 ) {/* * blocksize-totalbytes because last directory * length i.e.,*dir->direntlen is free availble
@@ -542,23 +551,23 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname)* it is a last entry of directory entry */ if (strlen (dirname) == dir->namelen) { -if (strncmp (dirname, ptr + -sizeof (struct ext2_dirent ), - dir->namelen) ==0 ) { - previous_dir->direntlen += - dir->direntlen; - inodeno = dir->inode; +if (strncmp (dirname, ptr +sizeof (struct ext2_dirent ), dir->namelen) ==0 ) { +uint16_t new_len; + new_len =le16_to_cpu (previous_dir->direntlen); + new_len +=le16_to_cpu (dir->direntlen); + previous_dir->direntlen =cpu_to_le16 (new_len); + inodeno =le32_to_cpu (dir->inode); dir->inode =0 ; found =1 ;break ; } } -if (fs->blksz - totalbytes == dir->direntlen) +if (fs->blksz - totalbytes ==le16_to_cpu (dir->direntlen))break ;/* traversing the each directory entry */ - templength = dir->direntlen; + templength =le16_to_cpu (dir->direntlen); totalbytes = totalbytes + templength; previous_dir = dir; dir = (struct ext2_dirent *)((char *)dir + templength);
@@ -720,7 +729,7 @@ end:if (matched_inode_no !=-1 ) {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" ); result_inode_no =-1 ; goto fail;
@@ -780,7 +789,7 @@ static int check_filename(char *filename, unsigned int blknr) dir = (struct ext2_dirent *)root_first_block_buffer; ptr = (char *)dir; totalbytes =0 ; -while (dir->direntlen >=0 ) { +while (le16_to_cpu (dir->direntlen) >=0 ) {/* * blocksize-totalbytes because last * directory length i.e., *dir->direntlen
@@ -790,20 +799,23 @@ static int check_filename(char *filename, unsigned int blknr)if (strlen (filename) == dir->namelen) {if (strncmp (filename, ptr +sizeof (struct ext2_dirent ), dir->namelen) ==0 ) { +uint16_t new_len;printf ("file found deleting\n" ); - previous_dir->direntlen += dir->direntlen; - inodeno = dir->inode; + new_len =le16_to_cpu (previous_dir->direntlen); + new_len +=le16_to_cpu (dir->direntlen); + previous_dir->direntlen =cpu_to_le16 (new_len); + inodeno =le32_to_cpu (dir->inode); dir->inode =0 ; found =1 ;break ; } } -if (fs->blksz - totalbytes == dir->direntlen) +if (fs->blksz - totalbytes ==le16_to_cpu (dir->direntlen))break ;/* traversing the each directory entry */ - templength = dir->direntlen; + templength =le16_to_cpu (dir->direntlen); totalbytes = totalbytes + templength; previous_dir = dir; dir = (struct ext2_dirent *)((char *)dir + templength);
@@ -843,14 +855,14 @@ int ext4fs_filename_check(char *filename)return -1 ; } -long int ext4fs_get_new_blk_no (void ) +uint32_t ext4fs_get_new_blk_no (void ) {short i;short status;int remainder;unsigned int bg_idx;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 ();char *journal_buffer =zalloc (fs->blksz);char *zero_buffer =zalloc (fs->blksz);
@@ -860,14 +872,13 @@ long int ext4fs_get_new_blk_no(void)if (fs->first_pass_bbmap ==0 ) {for (i =0 ; i < fs->no_blkgrp; i++) { -if (bgd[i].free_blocks) { -if (bgd[i].bg_flags &EXT4_BG_BLOCK_UNINIT ) { -put_ext4 (((uint64_t ) ((uint64_t )bgd[i].block_id * - (uint64_t )fs->blksz)), +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); - bgd[i].bg_flags = - bgd[i]. - bg_flags & ~EXT4_BG_BLOCK_UNINIT ; + new_flags =le16_to_cpu (bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT ; + bgd[i].bg_flags =cpu_to_le16 (new_flags);memcpy (fs->blk_bmaps[i], zero_buffer, fs->blksz); }
@@ -879,17 +890,17 @@ long int ext4fs_get_new_blk_no(void) fs->curr_blkno = fs->curr_blkno + (i * fs->blksz *8 ); fs->first_pass_bbmap++; - bgd[i].free_blocks--; - fs->sb->free_blocks--; - status =ext4fs_devread ((lbaint_t ) - bgd[i].block_id * +DEC_LE16 (bgd[i].free_blocks); +DEC_LE32 (fs->sb->free_blocks); + status =ext4fs_devread ( + (lbaint_t )le32_to_cpu (bgd[i].block_id) * fs->sect_perblk,0 , fs->blksz, journal_buffer);if (status ==0 ) goto fail;if (ext4fs_log_journal (journal_buffer, - bgd[i].block_id)) +le32_to_cpu (bgd[i].block_id))) goto fail; goto success; }else {
@@ -923,13 +934,14 @@ 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); -put_ext4 (((uint64_t ) ((uint64_t )bgd[bg_idx].block_id * - (uint64_t )fs->blksz)), zero_buffer, fs->blksz); +put_ext4 ((uint64_t )le32_to_cpu (bgd[bg_idx].block_id) * fs->blksz, + zero_buffer, fs->blksz);memcpy (fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz); - bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags & - ~EXT4_BG_BLOCK_UNINIT ; + new_flags =le16_to_cpu (bgd[bg_idx].bg_flags) & ~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],
@@ -942,19 +954,20 @@ restart:/* journal backup */ if (prev_bg_bitmap_index != bg_idx) {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, journal_buffer);if (status ==0 ) goto fail;if (ext4fs_log_journal (journal_buffer, - bgd[bg_idx].block_id)) +le32_to_cpu (bgd[bg_idx].block_id))) goto fail; prev_bg_bitmap_index = bg_idx; } - bgd[bg_idx].free_blocks--; - fs->sb->free_blocks--; +DEC_LE16 (bgd[bg_idx].free_blocks); +DEC_LE32 (fs->sb->free_blocks); goto success; } success:
@@ -975,7 +988,7 @@ int ext4fs_get_new_inode_no(void)short status;unsigned int ibmap_idx;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 ();char *journal_buffer =zalloc (fs->blksz);char *zero_buffer =zalloc (fs->blksz);
@@ -990,13 +1003,12 @@ int ext4fs_get_new_inode_no(void) bgd[i].free_inodes) bgd[i].bg_itable_unused = bgd[i].free_inodes; -if (bgd[i].bg_flags &EXT4_BG_INODE_UNINIT ) { -put_ext4 (((uint64_t ) - ((uint64_t )bgd[i].inode_id * - (uint64_t )fs->blksz)), +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, zero_buffer, fs->blksz); - bgd[i].bg_flags = bgd[i].bg_flags & - ~EXT4_BG_INODE_UNINIT ; + new_flags =le16_to_cpu (bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT ; + bgd[i].bg_flags =cpu_to_le16 (new_flags);memcpy (fs->inode_bmaps[i], zero_buffer, fs->blksz); }
@@ -1008,18 +1020,18 @@ int ext4fs_get_new_inode_no(void) fs->curr_inode_no = fs->curr_inode_no + (i * inodes_per_grp); fs->first_pass_ibmap++; - bgd[i].free_inodes--; - bgd[i].bg_itable_unused--; - fs->sb->free_inodes--; - status =ext4fs_devread ((lbaint_t ) - bgd[i].inode_id * +DEC_LE16 (bgd[i].free_inodes); +DEC_LE16 (bgd[i].bg_itable_unused); +DEC_LE32 (fs->sb->free_inodes); + status =ext4fs_devread ( + (lbaint_t )le32_to_cpu (bgd[i].inode_id) * fs->sect_perblk,0 , fs->blksz, journal_buffer);if (status ==0 ) goto fail;if (ext4fs_log_journal (journal_buffer, - bgd[i].inode_id)) +le32_to_cpu (bgd[i].inode_id))) goto fail; goto success; }else
@@ -1031,13 +1043,13 @@ restart: fs->curr_inode_no++;/* get the blockbitmap index respective to blockno */ 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); -put_ext4 (((uint64_t ) ((uint64_t )bgd[ibmap_idx].inode_id * - (uint64_t )fs->blksz)), zero_buffer, - fs->blksz); - bgd[ibmap_idx].bg_flags = - bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT ; +put_ext4 ((uint64_t )le32_to_cpu (bgd[ibmap_idx].inode_id) * 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);memcpy (fs->inode_bmaps[ibmap_idx], zero_buffer, fs->blksz); }
@@ -1053,14 +1065,14 @@ restart:/* journal backup */ if (prev_inode_bitmap_index != ibmap_idx) {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);if (status ==0 ) goto fail;if (ext4fs_log_journal (journal_buffer, - bgd[ibmap_idx].inode_id)) +le32_to_cpu (bgd[ibmap_idx].inode_id))) goto fail; prev_inode_bitmap_index = ibmap_idx; }
@@ -1068,9 +1080,9 @@ restart: bgd[ibmap_idx].free_inodes) bgd[ibmap_idx].bg_itable_unused = bgd[ibmap_idx].free_inodes; - bgd[ibmap_idx].free_inodes--; - bgd[ibmap_idx].bg_itable_unused--; - fs->sb->free_inodes--; +DEC_LE16 (bgd[ibmap_idx].free_inodes); +DEC_LE16 (bgd[ibmap_idx].bg_itable_unused); +DEC_LE32 (fs->sb->free_inodes); goto success; }
@@ -1097,8 +1109,8 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,long int actual_block_no;long int si_blockno;/* si :single indirect */ -unsigned int *si_buffer =NULL ; -unsigned int *si_start_addr =NULL ; +__le32 *si_buffer =NULL ; +__le32 *si_start_addr =NULL ;struct ext_filesystem *fs =get_fs ();if (*total_remaining_blocks !=0 ) {
@@ -1128,7 +1140,7 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,printf ("no block left to assign\n" ); goto fail; } - *si_buffer = actual_block_no; + *si_buffer =cpu_to_le32 (actual_block_no);debug ("SIAB %u: %u\n" , *si_buffer, *total_remaining_blocks);
@@ -1141,7 +1153,7 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,/* write the block to disk */ put_ext4 (((uint64_t ) ((uint64_t )si_blockno * (uint64_t )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:free (si_start_addr);
@@ -1158,10 +1170,10 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,/* di:double indirect */ long int di_blockno_parent;long int di_blockno_child; -unsigned int *di_parent_buffer =NULL ; -unsigned int *di_child_buff =NULL ; -unsigned int *di_block_start_addr =NULL ; -unsigned int *di_child_buff_start =NULL ; +__le32 *di_parent_buffer =NULL ; +__le32 *di_child_buff =NULL ; +__le32 *di_block_start_addr =NULL ; +__le32 *di_child_buff_start =NULL ;struct ext_filesystem *fs =get_fs ();if (*total_remaining_blocks !=0 ) {
@@ -1205,7 +1217,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode, goto fail; 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++; (*no_blks_reqd)++; debug("DICB %ld: %u\n", di_blockno_child,
@@ -1228,7 +1240,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,printf ("no block left to assign\n" ); 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, *total_remaining_blocks);
@@ -1248,7 +1260,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)), 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:free (di_block_start_addr);
@@ -1266,12 +1278,12 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,long int ti_gp_blockno;long int ti_parent_blockno;long int ti_child_blockno; -unsigned int *ti_gp_buff =NULL ; -unsigned int *ti_parent_buff =NULL ; -unsigned int *ti_child_buff =NULL ; -unsigned int *ti_gp_buff_start_addr =NULL ; -unsigned int *ti_pbuff_start_addr =NULL ; -unsigned int *ti_cbuff_start_addr =NULL ; +__le32 *ti_gp_buff =NULL ; +__le32 *ti_parent_buff =NULL ; +__le32 *ti_child_buff =NULL ; +__le32 *ti_gp_buff_start_addr =NULL ; +__le32 *ti_pbuff_start_addr =NULL ; +__le32 *ti_cbuff_start_addr =NULL ;struct ext_filesystem *fs =get_fs ();if (*total_remaining_blocks !=0 ) {/* triple indirect grand parent block connecting to inode */
@@ -1301,7 +1313,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode, goto fail; 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++; (*no_blks_reqd)++; debug("TIPB %ld: %u\n", ti_parent_blockno,
@@ -1319,7 +1331,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode, goto fail1; 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++; (*no_blks_reqd)++; debug("TICB %ld: %u\n", ti_parent_blockno,
@@ -1335,7 +1347,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,free (ti_cbuff_start_addr); 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, *total_remaining_blocks);
@@ -1364,7 +1376,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,/* write the grand parent block */ put_ext4 (((uint64_t ) ((uint64_t )ti_gp_blockno * (uint64_t )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);return ; }
@@ -1389,7 +1401,7 @@ void ext4fs_allocate_blocks(struct ext2_inode *file_inode,printf ("no block left to assign\n" );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); total_remaining_blocks--;
@@ -1420,7 +1432,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block index = (struct ext4_extent_idx *)(ext_block +1 );if (le16_to_cpu (ext_block->eh_magic) !=EXT4_EXT_MAGIC ) -return 0 ; +return NULL ;if (ext_block->eh_depth ==0 )return ext_block;
@@ -1432,7 +1444,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block }while (fileblock >=le32_to_cpu (index[i].ei_block));if (--i <0 ) -return 0 ; +return NULL ; block =le16_to_cpu (index[i].ei_leaf_hi); block = (block <<32 ) +le32_to_cpu (index[i].ei_leaf_lo);
@@ -1441,7 +1453,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block buf)) ext_block = (struct ext4_extent_header *)buf; else - return 0; + return NULL; } }
@@ -2034,11 +2046,11 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)if (!diro->inode_read) { status =ext4fs_read_inode (diro->data, diro->ino, &diro->inode);if (status ==0 ) -return 0 ; +return NULL ; } symlink =zalloc (le32_to_cpu (diro->inode.size) +1 );if (!symlink) -return 0 ; +return NULL ;if (le32_to_cpu (diro->inode.size) <sizeof (diro->inode.b.symlink)) { strncpy(symlink, diro->inode.b.symlink,
@@ -2049,7 +2061,7 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node) symlink, &actread);if ((status <0 ) || (actread ==0 )) {free (symlink); -return 0 ; +return NULL ; } } symlink[le32_to_cpu (diro->inode.size)] ='\0' ;
@@ -2234,7 +2246,7 @@ int ext4fs_mount(unsigned part_length)* and we do not support metadata_csum (and cannot reliably find * 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" ); goto fail; }
fs/ext4/ext4_common.h +2 -2
@@ -59,10 +59,10 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,#if defined(CONFIG_EXT4_WRITE )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);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 );void ext4fs_reset_block_bmap (long int blockno,unsigned char *buffer,int index);
fs/ext4/ext4_journal.c +9 -6
@@ -151,7 +151,7 @@ int ext4fs_log_gdt(char *gd_table)* journal_buffer -- Buffer containing meta data * 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 ();short i;
@@ -183,7 +183,7 @@ int ext4fs_log_journal(char *journal_buffer, long int blknr)* metadata_buffer -- Buffer containing meta data * 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 ();if (!metadata_buffer) {
@@ -215,7 +215,7 @@ void print_revoke_blks(char *revk_blk)printf ("total bytes %d\n" , 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); offset +=4 ; }
@@ -302,7 +302,7 @@ int check_blknr_for_revoke(long int blknr, int sequence_no) max =be32_to_cpu (header->r_count);while (offset < max) { - blocknr =be32_to_cpu (*((long int *) + blocknr =be32_to_cpu (*((__be32 *) (revk_blk + offset)));if (blocknr == blknr) goto found;
@@ -420,7 +420,7 @@ int ext4fs_check_journal_state(int recovery_flag) 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 )printf ("Recovery required\n" ); }else {
@@ -517,11 +517,14 @@ int ext4fs_check_journal_state(int recovery_flag) end:if (recovery_flag ==RECOVER ) { +uint32_t new_feature_incompat; jsb->s_start =cpu_to_be32 (1 ); jsb->s_sequence =cpu_to_be32 (be32_to_cpu (jsb->s_sequence) +1 );/* get the superblock */ 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 */ put_ext4((uint64_t) (SUPERBLOCK_SIZE),
fs/ext4/ext4_journal.h +2 -2
@@ -115,8 +115,8 @@ extern struct ext2_data *ext4fs_root;int ext4fs_init_journal (void );int ext4fs_log_gdt (char *gd_table);int ext4fs_check_journal_state (int recovery_flag); -int ext4fs_log_journal (char *journal_buffer,long int blknr); -int ext4fs_put_metadata (char *metadata_buffer,long int blknr); +int ext4fs_log_journal (char *journal_buffer,uint32_t blknr); +int ext4fs_put_metadata (char *metadata_buffer,uint32_t blknr);void ext4fs_update_journal (void );void ext4fs_dump_metadata (void );void ext4fs_push_revoke_blk (char *buffer);
fs/ext4/ext4_write.c +101 -94
513 lines not on this page · raw message
include/ext_common.h +1 -1
@@ -52,7 +52,7 @@#define LOG2_BLOCK_SIZE (data) (le32_to_cpu \ (data->sblock.log2_block_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))#define EXT2_FT_DIR 2
Signature · 1 line
2.1.4
PATCH 4/4 ext4: fix wrong usage of le32_to_cpu()
In reply to Michael Wallele32_to_cpu() must only convert the revision_level and not the boolean result.
--- fs/ext4/ext4_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4_common.c +1 -1
@@ -2251,7 +2251,7 @@ int ext4fs_mount(unsigned part_length) goto fail; } -if (le32_to_cpu (data->sblock.revision_level ==0 )) +if (le32_to_cpu (data->sblock.revision_level) ==0 ) fs->inodesz =128 ;else fs->inodesz =le16_to_cpu (data->sblock.inode_size);
Signature · 1 line
2.1.4
RFC PATCH 3/4 ext4: fix endianess problems in ext4 write support
In reply to Michael WalleQuoted 5 lines · On Freitag, 12. August 2016 15:16:20 CEST Michael Walle wrote:
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 <[email protected]>
I have tested this on sandbox (x86_64), no regressions found. Some remarks below.
Quoted 52 lines
---
Ok this patch is huge, please comment. I know, checkpatch fails because there are longer lines than 80 characters. I don't want do be rude, but the coding style is awful :/ Eg.
http://git.denx.de/?p=u-boot.git;a=blob;f=fs/ext4/ext4_common.c;h=eb49fce04
c5a290e839575945da722bc97d2f670;hb=HEAD#l440
http://git.denx.de/?p=u-boot.git;a=blob;f=fs/ext4/ext4_write.c;h=e027916763
f9b52937c7fe13f1698b67b94eb901;hb=HEAD#l137Many ugly places seems to come from the 80 characters limit. Most of the code looks like its right-aligned. I know there is a valid point for having this limit, eg refactor code into small functions. And frankly, the ext4 write code badly needs such a refactoring. But I won't and can't do it. I can resend a new version which meet the 80 character restriction, but it won't make the code easier to read. And believe me, it is already hard to do it.
Ok, back to business. Sparse really helped to find the places where the byteswaps were missing once you annotated it correctly with the __le16/__le32 types. If someone want to do check again, just run
make C=1
SeeDocumentation/sparse.txtin the linux kernel for more information. I've done a short test, to verify I can (over)write a 700kB file. No cornertests etc.fs/ext4/ext4_common.c | 260 ++++++++++++++++++++++++++----------------------- fs/ext4/ext4_common.h | 4 +- fs/ext4/ext4_journal.c | 15 +-- fs/ext4/ext4_journal.h | 4 +- fs/ext4/ext4_write.c | 195 +++++++++++++++++++------------------ include/ext_common.h | 2 +- 6 files changed, 251 insertions(+), 229 deletions(-)fs/ext4/ext4_common.c +2
-2@@ -33,19 +33,22 @@struct ext2_data *ext4fs_root;struct ext2fs_node *ext4fs_file; -uint32_t *ext4fs_indir1_block; +__le32 *ext4fs_indir1_block;int ext4fs_indir1_size;int ext4fs_indir1_blkno =-1 ; -uint32_t *ext4fs_indir2_block; +__le32 *ext4fs_indir2_block;int ext4fs_indir2_size;int ext4fs_indir2_blkno =-1 ;
fs/ext4/ext4_common.c line 50
Quoted 10 lines
fs/ext4/ext4_common.c +3
-1-uint32_t *ext4fs_indir3_block; +__le32 *ext4fs_indir3_block;int ext4fs_indir3_size;int ext4fs_indir3_blkno =-1 ;struct ext2_inode *g_parent_inode;static int symlinknest; +#define DEC_LE16 (x) (x = cpu_to_le16(le16_to_cpu(x) - 1)) +#define DEC_LE32 (x) (x = cpu_to_le32(le32_to_cpu(x) - 1))
I would prefer a static function *per variable/field* - this will make it easier to manipulate any high/low split counts for FEATURE_64BIT. Same for the INC_xxx in ext4_write.c
Quoted 11 lines
fs/ext4/ext4_common.c +3
-0@@ -360,6 +364,9 @@ void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type) /* directory entry */struct ext2_dirent *dir;char *temp_dir =NULL ; +uint32_t new_blk_no; +uint32_t new_size; +uint32_t new_blockcnt; zero_buffer =zalloc (fs->blksz);if (!zero_buffer) {
fs/ext4/ext4_common.c restart: · line 406
Quoted 5 lines
fs/ext4/ext4_common.c +1
-1@@ -396,7 +403,7 @@ restart: goto fail; dir = (struct ext2_dirent *)root_first_block_buffer; totalbytes =0 ; -while (dir->direntlen >0 ) { +while (le16_to_cpu (dir->direntlen) >0 ) {
direntlen is at least 8, so this is "while (true) {"
Quoted 27 lines
fs/ext4/ext4_common.c +3
-7/* * blocksize-totalbytes because last directory length * i.e. dir->direntlen is free availble space in the @@ -405,7 +412,7 @@ restart:*/ /* 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 ) padding_factor =4 - (strlen (filename) %4 );@@ -430,32 +437,34 @@ restart:printf ("Directory exceeds limit\n" ); goto fail; } - g_parent_inode->b.blocks.dir_blocks - [direct_blk_idx] =ext4fs_get_new_blk_no (); -if (g_parent_inode->b.blocks.dir_blocks - [direct_blk_idx] ==-1 ) { + new_blk_no =ext4fs_get_new_blk_no (); +if (new_blk_no ==-1 ) {printf ("no block left to assign\n" ); goto fail; } - put_ext4(((uint64_t) - ((uint64_t)g_parent_inode->b.
fs/ext4/ext4_common.c restart: · line 448
Quoted 10 lines
fs/ext4/ext4_common.c +3
-6- blocks.dir_blocks[direct_blk_idx] * - (uint64_t)fs->blksz)), zero_buffer, fs->blksz); - g_parent_inode->size = - g_parent_inode->size + fs->blksz; - g_parent_inode->blockcnt = - g_parent_inode->blockcnt + fs->sect_perblk; +put_ext4 ((uint64_t )new_blk_no * fs->blksz, zero_buffer, fs- blksz); + g_parent_inode->b.blocks.dir_blocks[direct_blk_idx] = +cpu_to_le32 (new_blk_no);
This is (already was) broken if the directory uses extents, we have to fix this (in a seperate patch).
Quoted 27 lines
fs/ext4/ext4_common.c +11
-2+ + new_size =le32_to_cpu (g_parent_inode->size); + new_size += fs->blksz; + g_parent_inode->size =cpu_to_le32 (new_size); + + 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 (root_first_block_buffer, first_block_no_of_root)) goto fail; goto restart; } - dir->direntlen = last_entry_dirlen; + dir->direntlen =cpu_to_le16 (last_entry_dirlen);break ; } - templength = dir->direntlen; + templength =le16_to_cpu (dir->direntlen); totalbytes = totalbytes + templength; sizeof_void_space =check_void_in_dentry (dir, filename);fs/ext4/ext4_common.c +0
-0if (sizeof_void_space)
fs/ext4/ext4_common.c static int search_dir(struct ext2_inode *parent_inode, · line 546
Quoted 5 lines
fs/ext4/ext4_common.c +1
-1@@ -534,7 +543,7 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname) dir = (struct ext2_dirent *)block_buffer; ptr = (char *)dir; totalbytes =0 ; -while (dir->direntlen >=0 ) { +while (le16_to_cpu (dir->direntlen) >=0 ) {
dito, "while (true) { "
fs/ext4/ext4_common.c static int check_filename(char *filename, unsigned int · line 792
Quoted 6 lines
fs/ext4/ext4_common.c +1
-1@@ -780,7 +789,7 @@ static int check_filename(char *filename, unsigned int blknr) dir = (struct ext2_dirent *)root_first_block_buffer; ptr = (char *)dir; totalbytes =0 ; -while (dir->direntlen >=0 ) { +while (le16_to_cpu (dir->direntlen) >=0 ) {
dito
fs/ext4/ext4_common.c int ext4fs_mount(unsigned part_length) · line 2252
Quoted 9 lines
fs/ext4/ext4_common.c +1
-1@@ -2234,7 +2246,7 @@ int ext4fs_mount(unsigned part_length)* and we do not support metadata_csum (and cannot reliably find * 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"); goto fail; }
This should have a if ((data->sblock.revision_level !=0) && ... in front, features are not defined for revision 0. Applies to other places as well ...
Quoted 59 lines
fs/ext4/ext4_common.h +1
-1@@ -59,10 +59,10 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,#if defined(CONFIG_EXT4_WRITE )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, intflags); void
ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type); -long intext4fs_get_new_blk_no(void);fs/ext4/ext4_common.h +1
-0+uint32_t ext4fs_get_new_blk_no (void );int ext4fs_get_new_inode_no (void );void ext4fs_reset_block_bmap (long int blockno,unsigned char *buffer,int index);fs/ext4/ext4_journal.c +1
-1@@ -151,7 +151,7 @@ int ext4fs_log_gdt(char *gd_table)* journal_buffer -- Buffer containing meta data * 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 ();short i;@@ -183,7 +183,7 @@ int ext4fs_log_journal(char *journal_buffer, long intblknr) *
metadata_buffer-- Buffer containing meta data
* blknr -- Block number on disk of the meta data buffer */fs/ext4/ext4_journal.c +3
-3-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 ();if (!metadata_buffer) {@@ -215,7 +215,7 @@ void print_revoke_blks(char *revk_blk)printf ("total bytes %d\n" , 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); offset +=4 ; }@@ -302,7 +302,7 @@ int check_blknr_for_revoke(long int blknr, int sequence_no) max = be32_to_cpu(header->r_count);while (offset < max) { - blocknr =be32_to_cpu (*((long int *) + blocknr =be32_to_cpu (*((__be32 *) (revk_blk + offset)));if (blocknr == blknr) goto found;
fs/ext4/ext4_journal.c int ext4fs_check_journal_state(int recovery_flag) · line 423
Quoted 5 lines
fs/ext4/ext4_journal.c +1
-1@@ -420,7 +420,7 @@ int ext4fs_check_journal_state(int recovery_flag) 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 (fs->sb->revision_level != 0) && ..
Signature · 3 lines
Stefan Br?ns / Bergstra?e 21 / 52062 Aachen home: +49 241 53809034 mobile: +49 151 50412019 work: +49 2405 49936-424
RFC PATCH 3/4 ext4: fix endianess problems in ext4 write support
Am 2016-08-14 03:50, schrieb Stefan Bruens:
Quoted 87 lines · On Freitag, 12. August 2016 15:16:20 CEST Michael Walle wrote:
Quoted 6 lines
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 <[email protected]>I have tested this on sandbox (
x86_64), no regressions found. Some remarks
below.Reviewed-by Stefan Br?ns <[email protected]>Tested-by Stefan Br?ns <[email protected]>Quoted 72 lines
---
Ok this patch is huge, please comment. I know, checkpatch fails because
there are longer lines than 80 characters. I don't want do be rude, but the
coding style is awful :/ Eg.http://git.denx.de/?p=u-boot.git;a=blob;f=fs/ext4/ext4_common.c;h=eb49fce04
c5a290e839575945da722bc97d2f670;hb=HEAD#l440
http://git.denx.de/?p=u-boot.git;a=blob;f=fs/ext4/ext4_write.c;h=e027916763
f9b52937c7fe13f1698b67b94eb901;hb=HEAD#l137Many ugly places seems to come from the 80 characters limit. Most of the
code looks like its right-aligned. I know there is a valid point for having
this limit, eg refactor code into small functions. And frankly, the ext4
write code badly needs such a refactoring. But I won't and can't do it. I
can resend a new version which meet the 80 character restriction, but it
won't make the code easier to read. And believe me, it is already hard to
do it.Ok, back to business. Sparse really helped to find the places where the
byteswaps were missing once you annotated it correctly with the __le16/__le32 types. If someone want to do check again, just run
make C=1
SeeDocumentation/sparse.txtin the linux kernel for more information. I've
done a short test, to verify I can (over)write a 700kB file. No cornertests
etc.fs/ext4/ext4_common.c | 260 ++++++++++++++++++++++++++----------------------- fs/ext4/ext4_common.h | 4 +- fs/ext4/ext4_journal.c | 15 +-- fs/ext4/ext4_journal.h | 4 +- fs/ext4/ext4_write.c | 195 +++++++++++++++++++------------------ include/ext_common.h | 2 +- 6 files changed, 251 insertions(+), 229 deletions(-)fs/ext4/ext4_common.c +3
-3@@ -33,19 +33,22 @@struct ext2_data *ext4fs_root;struct ext2fs_node *ext4fs_file; -uint32_t *ext4fs_indir1_block; +__le32 *ext4fs_indir1_block;int ext4fs_indir1_size;int ext4fs_indir1_blkno =-1 ; -uint32_t *ext4fs_indir2_block; +__le32 *ext4fs_indir2_block;int ext4fs_indir2_size;int ext4fs_indir2_blkno =-1 ; -uint32_t *ext4fs_indir3_block; +__le32 *ext4fs_indir3_block;int ext4fs_indir3_size;int ext4fs_indir3_blkno =-1 ;struct ext2_inode *g_parent_inode;static int symlinknest;
Quoted 8 lines
fs/ext4/ext4_common.c line 50
Quoted 2 lines
fs/ext4/ext4_common.c +2
-0+#define DEC_LE16 (x) (x = cpu_to_le16(le16_to_cpu(x) - 1)) +#define DEC_LE32 (x) (x = cpu_to_le32(le32_to_cpu(x) - 1))I would prefer a static function *per variable/field* - this will make it
easier to manipulate any high/low split counts forFEATURE_64BIT. Same for theINC_xxxinext4_write.c
ok, sounds right ;)
Quoted 9 lines
Quoted 9 lines
fs/ext4/ext4_common.c +0
-0@@ -360,6 +364,9 @@ void ext4fs_update_parent_dentry(char *filename,int
*p_ino, intfile_type) /* directory entry */fs/ext4/ext4_common.c +3
-0struct ext2_dirent *dir;char *temp_dir =NULL ; +uint32_t new_blk_no; +uint32_t new_size; +uint32_t new_blockcnt;
Quoted 10 lines
fs/ext4/ext4_common.c restart: · line 406
Quoted 8 lines
fs/ext4/ext4_common.c +1
-1zero_buffer =zalloc (fs->blksz);if (!zero_buffer) {@@ -396,7 +403,7 @@ restart: goto fail; dir = (struct ext2_dirent *)root_first_block_buffer; totalbytes =0 ; -while (dir->direntlen >0 ) { +while (le16_to_cpu (dir->direntlen) >0 ) {direntlen is at least 8, so this is "while (true) {"
I'll put this and all remarks below, into a separate patch, because I want this patch to only fix the endianess problem without any functional changes.
-michael
Quoted 176 lines
Quoted 28 lines
fs/ext4/ext4_common.c +3
-7/* * blocksize-totalbytes because last directory length * i.e. dir->direntlen is free availble space in the @@ -405,7 +412,7 @@ restart:*/ /* 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 ) padding_factor =4 - (strlen (filename) %4 );@@ -430,32 +437,34 @@ restart:printf ("Directory exceeds limit\n" ); goto fail; } - g_parent_inode->b.blocks.dir_blocks - [direct_blk_idx] =ext4fs_get_new_blk_no (); -if (g_parent_inode->b.blocks.dir_blocks - [direct_blk_idx] ==-1 ) { + new_blk_no =ext4fs_get_new_blk_no (); +if (new_blk_no ==-1 ) {printf ("no block left to assign\n" ); goto fail; } - put_ext4(((uint64_t) - ((uint64_t)g_parent_inode->b.fs/ext4/ext4_common.c restart: · line 448
Quoted 10 lines
fs/ext4/ext4_common.c +3
-6- blocks.dir_blocks[direct_blk_idx] * - (uint64_t)fs->blksz)), zero_buffer, fs->blksz); - g_parent_inode->size = - g_parent_inode->size + fs->blksz; - g_parent_inode->blockcnt = - g_parent_inode->blockcnt + fs->sect_perblk; +put_ext4 ((uint64_t )new_blk_no * fs->blksz, zero_buffer, fs- blksz); + g_parent_inode->b.blocks.dir_blocks[direct_blk_idx] = +cpu_to_le32 (new_blk_no);This is (already was) broken if the directory uses extents, we have to fix
this (in a seperate patch).Quoted 27 lines
fs/ext4/ext4_common.c +11
-2+ + new_size =le32_to_cpu (g_parent_inode->size); + new_size += fs->blksz; + g_parent_inode->size =cpu_to_le32 (new_size); + + 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 (root_first_block_buffer, first_block_no_of_root)) goto fail; goto restart; } - dir->direntlen = last_entry_dirlen; + dir->direntlen =cpu_to_le16 (last_entry_dirlen);break ; } - templength = dir->direntlen; + templength =le16_to_cpu (dir->direntlen); totalbytes = totalbytes + templength; sizeof_void_space =check_void_in_dentry (dir, filename);fs/ext4/ext4_common.c +0
-0if (sizeof_void_space)fs/ext4/ext4_common.c static int search_dir(struct ext2_inode · line 547
Quoted 6 lines
fs/ext4/ext4_common.c +1
-1@@ -534,7 +543,7 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname) dir = (struct ext2_dirent *)block_buffer; ptr = (char *)dir; totalbytes =0 ; -while (dir->direntlen >=0 ) { +while (le16_to_cpu (dir->direntlen) >=0 ) {dito, "while (true) { "
fs/ext4/ext4_common.c static int search_dir(struct ext2_inode *parent_inode, char *dirname) · line 546
Quoted 7 lines
fs/ext4/ext4_common.c +0
-0@@ -780,7 +789,7 @@ static int check_filename(char *filename, unsignedint
blknr) dir = (structext2_dirent*)root_first_block_buffer;fs/ext4/ext4_common.c +1
-1ptr = (char *)dir; totalbytes =0 ; -while (dir->direntlen >=0 ) { +while (le16_to_cpu (dir->direntlen) >=0 ) {dito
Quoted 10 lines
fs/ext4/ext4_common.c +1
-1@@ -2234,7 +2246,7 @@ int ext4fs_mount(unsigned part_length)* and we do not support metadata_csum (and cannot reliably find * 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,
possiblymetadata_csum), not mounting\n"); goto fail;
}This should have a if ((
data->sblock.revision_level!=0) && ... in front,
features are not defined for revision 0. Applies to other places as well ...Quoted 62 lines
fs/ext4/ext4_common.h +0
-0@@ -59,10 +59,10 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir,char
*name,#if
defined(CONFIG_EXT4_WRITE)
uint32_text4fs_div_roundup(uint32_t size, uint32_t n);fs/ext4/ext4_common.h +1
-1-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, intflags); void ext4fs_update_parent_dentry(char *filename, int
*p_ino, intfile_type); -long intext4fs_get_new_blk_no(void);fs/ext4/ext4_common.h +1
-0+uint32_t ext4fs_get_new_blk_no (void );int ext4fs_get_new_inode_no (void );void ext4fs_reset_block_bmap (long int blockno,unsigned char *buffer,int index);fs/ext4/ext4_journal.c +1
-1@@ -151,7 +151,7 @@ int ext4fs_log_gdt(char *gd_table)* journal_buffer -- Buffer containing meta data * 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 ();short i;@@ -183,7 +183,7 @@ int ext4fs_log_journal(char *journal_buffer, longint
blknr) *metadata_buffer-- Buffer containing meta data
* blknr -- Block number on disk of the meta data buffer */fs/ext4/ext4_journal.c +3
-3-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 ();if (!metadata_buffer) {@@ -215,7 +215,7 @@ void print_revoke_blks(char *revk_blk)printf ("total bytes %d\n" , 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); offset +=4 ; }@@ -302,7 +302,7 @@ int check_blknr_for_revoke(long int blknr, int sequence_no) max = be32_to_cpu(header->r_count);while (offset < max) { - blocknr =be32_to_cpu (*((long int *) + blocknr =be32_to_cpu (*((__be32 *) (revk_blk + offset)));if (blocknr == blknr) goto found;fs/ext4/ext4_journal.c int ext4fs_check_journal_state(int recovery_flag) · line 424
Quoted 6 lines
fs/ext4/ext4_journal.c +1
-1@@ -420,7 +420,7 @@ int ext4fs_check_journal_state(int recovery_flag) 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 (
fs->sb->revision_level!= 0) && ..
RFC PATCH 3/4 ext4: fix endianess problems in ext4 write support
In reply to Stefan BruensHi Stefan,
Am 2016-08-14 03:50, schrieb Stefan Bruens:
Quoted 15 lines · On Freitag, 12. August 2016 15:16:20 CEST Michael Walle wrote:
Quoted 6 lines
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 <[email protected]>I have tested this on sandbox (
x86_64), no regressions found. Some remarks
below.Reviewed-by Stefan Br?ns <[email protected]>Tested-by Stefan Br?ns <[email protected]>
[snip]
Quoted 7 lines
Quoted 7 lines
fs/ext4/ext4_common.c +1
-1@@ -2234,7 +2246,7 @@ int ext4fs_mount(unsigned part_length)* and we do not support metadata_csum (and cannot reliably find * 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
Quoted 9 lines
Quoted 3 lines
(64bit,
possiblymetadata_csum), not mounting\n"); goto fail;
}This should have a if ((
data->sblock.revision_level!=0) && ... in front,
features are not defined for revision 0. Applies to other places as well ...
are you sure about that? I can't find any code in the kernel where features are only valid if revision > 0. Also, I couldn't find anything in the ext4 documentation wiki:
https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
-michael
RFC PATCH 3/4 ext4: fix endianess problems in ext4 write support
Quoted 39 lines · On Dienstag, 16. August 2016 13:41:21 CEST you wrote:
Hi Stefan,
Am 2016-08-14 03:50, schrieb Stefan Bruens:
Quoted 14 lines · On Freitag, 12. August 2016 15:16:20 CEST Michael Walle wrote:
Quoted 6 lines
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 <[email protected]>I have tested this on sandbox (
x86_64), no regressions found. Some remarks
below.Reviewed-by Stefan Br?ns <[email protected]>Tested-by Stefan Br?ns <[email protected]>[snip]
Quoted 12 lines
Quoted 12 lines
fs/ext4/ext4_common.c +1
-1@@ -2234,7 +2246,7 @@ int ext4fs_mount(unsigned part_length)* and we do not support metadata_csum (and cannot reliably find * 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,
possiblymetadata_csum), not mounting\n"); goto fail;Quoted 6 lines
Quoted 1 line
}
This should have a if ((
data->sblock.revision_level!=0) && ... in front,
features are not defined for revision 0. Applies to other places as well ...
Quoted 5 lines
are you sure about that? I can't find any code in the kernel where features are only valid if revision > 0. Also, I couldn't find anything in the ext4 documentation wiki:
This document states that inode_size and all later fields are only defined for revision 1.
The statement seems to originate from here:
http://git.kernel.org/cgit/fs/ext2/e2fsprogs.git/tree/lib/ext2fs/
ext2_fs.h#n651
Now, if the remainder of the superblock after def_resgid is guaranteed to be zeroed the feature checks are ok for any revision, whereas a zero inode size would be bad and defaulting to 128 is needed.
As e2fsprogs is skipping any revision-level tests for feature checks, it seems to be ok to assume remaining fields to be zeroed.
Kind regards,
Stefan