ext4: fix wrong usage of le32_to_cpu()
[oweals/u-boot.git] / fs / ext4 / ext4_common.c
index 58880b467fbf678e029e793d1e26811675adf70e..cd1bdfe025ad9282c97699995e79d77c20dd70eb 100644 (file)
  *
  * ext4write : Based on generic ext4 protocol.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <ext_common.h>
 #include <ext4fs.h>
+#include <inttypes.h>
 #include <malloc.h>
+#include <memalign.h>
 #include <stddef.h>
 #include <linux/stat.h>
 #include <linux/time.h>
 
 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;
 
 #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 res = size / n;
@@ -84,36 +99,27 @@ void put_ext4(uint64_t off, void *buf, uint32_t size)
 
        if ((startblock + (size >> log2blksz)) >
            (part_offset + fs->total_sect)) {
-               printf("part_offset is %lu\n", part_offset);
-               printf("total_sector is %llu\n", fs->total_sect);
+               printf("part_offset is " LBAFU "\n", part_offset);
+               printf("total_sector is %" PRIu64 "\n", fs->total_sect);
                printf("error: overflow occurs\n");
                return;
        }
 
        if (remainder) {
-               if (fs->dev_desc->block_read) {
-                       fs->dev_desc->block_read(fs->dev_desc->dev,
-                                                startblock, 1, sec_buf);
-                       temp_ptr = sec_buf;
-                       memcpy((temp_ptr + remainder),
-                              (unsigned char *)buf, size);
-                       fs->dev_desc->block_write(fs->dev_desc->dev,
-                                                 startblock, 1, sec_buf);
-               }
+               blk_dread(fs->dev_desc, startblock, 1, sec_buf);
+               temp_ptr = sec_buf;
+               memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
+               blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
        } else {
                if (size >> log2blksz != 0) {
-                       fs->dev_desc->block_write(fs->dev_desc->dev,
-                                                 startblock,
-                                                 size >> log2blksz,
-                                                 (unsigned long *)buf);
+                       blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
+                                  (unsigned long *)buf);
                } else {
-                       fs->dev_desc->block_read(fs->dev_desc->dev,
-                                                startblock, 1, sec_buf);
+                       blk_dread(fs->dev_desc, startblock, 1, sec_buf);
                        temp_ptr = sec_buf;
                        memcpy(temp_ptr, buf, size);
-                       fs->dev_desc->block_write(fs->dev_desc->dev,
-                                                 startblock, 1,
-                                                 (unsigned long *)sec_buf);
+                       blk_dwrite(fs->dev_desc, startblock, 1,
+                                  (unsigned long *)sec_buf);
                }
        }
 }
@@ -131,7 +137,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;
        }
 
@@ -268,7 +274,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) {
@@ -293,7 +299,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) {
@@ -308,19 +314,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));
@@ -341,7 +348,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;
 
@@ -352,7 +359,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;
        }
 
@@ -379,6 +386,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) {
@@ -405,7 +415,7 @@ restart:
                previous_blknr = root_blknr;
        }
 
-       status = ext4fs_devread(first_block_no_of_root
+       status = ext4fs_devread((lbaint_t)first_block_no_of_root
                                * fs->sect_perblk,
                                0, fs->blksz, root_first_block_buffer);
        if (status == 0)
@@ -415,7 +425,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
@@ -424,7 +434,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);
 
@@ -449,32 +459,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)
-                                         (g_parent_inode->b.
-                                          blocks.dir_blocks[direct_blk_idx] *
-                                          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)
@@ -484,7 +496,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);
 
@@ -494,11 +506,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 */
@@ -545,7 +557,7 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname)
                if (!block_buffer)
                        goto fail;
 
-               status = ext4fs_devread(blknr * fs->sect_perblk,
+               status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
                                        0, fs->blksz, (char *)block_buffer);
                if (status == 0)
                        goto fail;
@@ -553,7 +565,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
@@ -561,23 +573,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);
@@ -625,8 +637,7 @@ static int parse_path(char **arr, char *dirname)
        arr[i] = zalloc(strlen("/") + 1);
        if (!arr[i])
                return -ENOMEM;
-
-       arr[i++] = "/";
+       memcpy(arr[i++], "/", strlen("/"));
 
        /* add each path entry after root */
        while (token != NULL) {
@@ -740,7 +751,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;
@@ -756,6 +767,11 @@ end:
 fail:
        free(depth_dirname);
        free(parse_dirname);
+       for (i = 0; i < depth; i++) {
+               if (!ptr[i])
+                       break;
+               free(ptr[i]);
+       }
        free(ptr);
        free(parent_inode);
        free(first_inode);
@@ -776,6 +792,7 @@ static int check_filename(char *filename, unsigned int blknr)
        struct ext2_dirent *previous_dir = NULL;
        char *ptr = NULL;
        struct ext_filesystem *fs = get_fs();
+       int ret = -1;
 
        /* get the first block of root */
        first_block_no_of_root = blknr;
@@ -783,7 +800,7 @@ static int check_filename(char *filename, unsigned int blknr)
        if (!root_first_block_buffer)
                return -ENOMEM;
        root_first_block_addr = root_first_block_buffer;
-       status = ext4fs_devread(first_block_no_of_root *
+       status = ext4fs_devread((lbaint_t)first_block_no_of_root *
                                fs->sect_perblk, 0,
                                fs->blksz, root_first_block_buffer);
        if (status == 0)
@@ -794,7 +811,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
@@ -804,20 +821,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);
@@ -829,12 +849,12 @@ static int check_filename(char *filename, unsigned int blknr)
                if (ext4fs_put_metadata(root_first_block_addr,
                                        first_block_no_of_root))
                        goto fail;
-               return inodeno;
+               ret = inodeno;
        }
 fail:
        free(root_first_block_buffer);
 
-       return -1;
+       return ret;
 }
 
 int ext4fs_filename_check(char *filename)
@@ -857,14 +877,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);
@@ -874,14 +894,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) (bgd[i].block_id *
-                                                             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);
                                }
@@ -893,16 +912,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(bgd[i].block_id *
+                               ext4fs_bg_free_blocks_dec(&bgd[i]);
+                               ext4fs_sb_free_blocks_dec(fs->sb);
+                               status = ext4fs_devread(
+                                                       (lbaint_t)le32_to_cpu(bgd[i].block_id) *
                                                        fs->sect_perblk, 0,
                                                        fs->blksz,
                                                        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 {
@@ -915,10 +935,8 @@ long int ext4fs_get_new_blk_no(void)
 restart:
                fs->curr_blkno++;
                /* get the blockbitmap index respective to blockno */
-               if (fs->blksz != 1024) {
-                       bg_idx = fs->curr_blkno / blk_per_grp;
-               } else {
-                       bg_idx = fs->curr_blkno / blk_per_grp;
+               bg_idx = fs->curr_blkno / blk_per_grp;
+               if (fs->blksz == 1024) {
                        remainder = fs->curr_blkno % blk_per_grp;
                        if (!remainder)
                                bg_idx--;
@@ -938,13 +956,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) (bgd[bg_idx].block_id *
-                                       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],
@@ -957,19 +976,20 @@ restart:
                /* journal backup */
                if (prev_bg_bitmap_index != bg_idx) {
                        memset(journal_buffer, '\0', fs->blksz);
-                       status = ext4fs_devread(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--;
+               ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
+               ext4fs_sb_free_blocks_dec(fs->sb);
                goto success;
        }
 success:
@@ -990,7 +1010,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);
@@ -1005,13 +1025,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)
-                                                 (bgd[i].inode_id *
-                                                       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);
                                }
@@ -1023,17 +1042,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(bgd[i].inode_id *
+                               ext4fs_bg_free_inodes_dec(&bgd[i]);
+                               ext4fs_bg_itable_unused_dec(&bgd[i]);
+                               ext4fs_sb_free_inodes_dec(fs->sb);
+                               status = ext4fs_devread(
+                                                       (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
                                                        fs->sect_perblk, 0,
                                                        fs->blksz,
                                                        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
@@ -1045,13 +1065,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) (bgd[ibmap_idx].inode_id *
-                                             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);
                }
@@ -1067,13 +1087,14 @@ restart:
                /* journal backup */
                if (prev_inode_bitmap_index != ibmap_idx) {
                        memset(journal_buffer, '\0', fs->blksz);
-                       status = ext4fs_devread(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;
                }
@@ -1081,9 +1102,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--;
+               ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
+               ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
+               ext4fs_sb_free_inodes_dec(fs->sb);
                goto success;
        }
 
@@ -1110,8 +1131,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) {
@@ -1129,7 +1150,7 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,
                (*no_blks_reqd)++;
                debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
 
-               status = ext4fs_devread(si_blockno * fs->sect_perblk,
+               status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
                                        0, fs->blksz, (char *)si_buffer);
                memset(si_buffer, '\0', fs->blksz);
                if (status == 0)
@@ -1141,7 +1162,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);
 
@@ -1152,9 +1173,9 @@ static void alloc_single_indirect_block(struct ext2_inode *file_inode,
                }
 
                /* write the block to disk */
-               put_ext4(((uint64_t) (si_blockno * fs->blksz)),
+               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);
@@ -1171,10 +1192,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) {
@@ -1193,7 +1214,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
                debug("DIPB %ld: %u\n", di_blockno_parent,
                      *total_remaining_blocks);
 
-               status = ext4fs_devread(di_blockno_parent *
+               status = ext4fs_devread((lbaint_t)di_blockno_parent *
                                        fs->sect_perblk, 0,
                                        fs->blksz, (char *)di_parent_buffer);
 
@@ -1218,13 +1239,13 @@ 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,
                              *total_remaining_blocks);
 
-                       status = ext4fs_devread(di_blockno_child *
+                       status = ext4fs_devread((lbaint_t)di_blockno_child *
                                                fs->sect_perblk, 0,
                                                fs->blksz,
                                                (char *)di_child_buff);
@@ -1241,7 +1262,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);
 
@@ -1251,7 +1272,7 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
                                        break;
                        }
                        /* write the block  table */
-                       put_ext4(((uint64_t) (di_blockno_child * fs->blksz)),
+                       put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
                                 di_child_buff_start, fs->blksz);
                        free(di_child_buff_start);
                        di_child_buff_start = NULL;
@@ -1259,9 +1280,9 @@ static void alloc_double_indirect_block(struct ext2_inode *file_inode,
                        if (*total_remaining_blocks == 0)
                                break;
                }
-               put_ext4(((uint64_t) (di_blockno_parent * fs->blksz)),
+               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);
@@ -1279,23 +1300,23 @@ 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 */
                ti_gp_blockno = ext4fs_get_new_blk_no();
                if (ti_gp_blockno == -1) {
                        printf("no block left to assign\n");
-                       goto fail;
+                       return;
                }
                ti_gp_buff = zalloc(fs->blksz);
                if (!ti_gp_buff)
-                       goto fail;
+                       return;
 
                ti_gp_buff_start_addr = ti_gp_buff;
                (*no_blks_reqd)++;
@@ -1314,7 +1335,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,
@@ -1325,14 +1346,14 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
                                ti_child_blockno = ext4fs_get_new_blk_no();
                                if (ti_child_blockno == -1) {
                                        printf("no block left assign\n");
-                                       goto fail;
+                                       goto fail1;
                                }
                                ti_child_buff = zalloc(fs->blksz);
                                if (!ti_child_buff)
-                                       goto fail;
+                                       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,
@@ -1345,9 +1366,10 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
                                            ext4fs_get_new_blk_no();
                                        if (actual_block_no == -1) {
                                                printf("no block left\n");
-                                               goto fail;
+                                               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);
 
@@ -1357,8 +1379,8 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
                                                break;
                                }
                                /* write the child block */
-                               put_ext4(((uint64_t) (ti_child_blockno *
-                                                     fs->blksz)),
+                               put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
+                                                     (uint64_t)fs->blksz)),
                                         ti_cbuff_start_addr, fs->blksz);
                                free(ti_cbuff_start_addr);
 
@@ -1366,7 +1388,7 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
                                        break;
                        }
                        /* write the parent block */
-                       put_ext4(((uint64_t) (ti_parent_blockno * fs->blksz)),
+                       put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
                                 ti_pbuff_start_addr, fs->blksz);
                        free(ti_pbuff_start_addr);
 
@@ -1374,10 +1396,14 @@ static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
                                break;
                }
                /* write the grand parent block */
-               put_ext4(((uint64_t) (ti_gp_blockno * fs->blksz)),
+               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;
        }
+fail1:
+       free(ti_pbuff_start_addr);
 fail:
        free(ti_gp_buff_start_addr);
 }
@@ -1391,18 +1417,16 @@ void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
        unsigned int no_blks_reqd = 0;
 
        /* allocation of direct blocks */
-       for (i = 0; i < INDIRECT_BLOCKS; i++) {
+       for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
                direct_blockno = ext4fs_get_new_blk_no();
                if (direct_blockno == -1) {
                        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--;
-               if (total_remaining_blocks == 0)
-                       break;
        }
 
        alloc_single_indirect_block(file_inode, &total_remaining_blocks,
@@ -1423,34 +1447,35 @@ static struct ext4_extent_header *ext4fs_get_extent_block
 {
        struct ext4_extent_idx *index;
        unsigned long long block;
-       struct ext_filesystem *fs = get_fs();
+       int blksz = EXT2_BLOCK_SIZE(data);
        int i;
 
        while (1) {
                index = (struct ext4_extent_idx *)(ext_block + 1);
 
-               if (le32_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
-                       return 0;
+               if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
+                       return NULL;
 
                if (ext_block->eh_depth == 0)
                        return ext_block;
                i = -1;
                do {
                        i++;
-                       if (i >= le32_to_cpu(ext_block->eh_entries))
+                       if (i >= le16_to_cpu(ext_block->eh_entries))
                                break;
-               } while (fileblock > le32_to_cpu(index[i].ei_block));
+               } while (fileblock >= le32_to_cpu(index[i].ei_block));
 
                if (--i < 0)
-                       return 0;
+                       return NULL;
 
-               block = le32_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);
 
-               if (ext4fs_devread(block << log2_blksz, 0, fs->blksz, buf))
+               if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
+                                  buf))
                        ext_block = (struct ext4_extent_header *)buf;
                else
-                       return 0;
+                       return NULL;
        }
 }
 
@@ -1463,14 +1488,15 @@ 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);
 
        debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
              group, blkno, blkoff);
 
-       return ext4fs_devread(blkno << (LOG2_BLOCK_SIZE(data) - log2blksz),
+       return ext4fs_devread((lbaint_t)blkno <<
+                             (LOG2_BLOCK_SIZE(data) - log2blksz),
                              blkoff, sizeof(struct ext2_block_group),
                              (char *)blkgrp);
 }
@@ -1487,18 +1513,18 @@ 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(blkno << (LOG2_BLOCK_SIZE(data) - log2blksz),
-                               blkoff,
+       status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
+                               log2blksz), blkoff,
                                sizeof(struct ext2_inode), (char *)inode);
        if (status == 0)
                return 0;
@@ -1543,17 +1569,17 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)
 
                do {
                        i++;
-                       if (i >= le32_to_cpu(ext_block->eh_entries))
+                       if (i >= le16_to_cpu(ext_block->eh_entries))
                                break;
                } while (fileblock >= le32_to_cpu(extent[i].ee_block));
                if (--i >= 0) {
                        fileblock -= le32_to_cpu(extent[i].ee_block);
-                       if (fileblock >= le32_to_cpu(extent[i].ee_len)) {
+                       if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
                                free(buf);
                                return 0;
                        }
 
-                       start = le32_to_cpu(extent[i].ee_start_hi);
+                       start = le16_to_cpu(extent[i].ee_start_hi);
                        start = (start << 32) +
                                        le32_to_cpu(extent[i].ee_start_lo);
                        free(buf);
@@ -1567,7 +1593,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))) {
@@ -1594,10 +1620,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(__le32_to_cpu
+                           ext4fs_devread((lbaint_t)le32_to_cpu
                                           (inode->b.blocks.
                                            indir_block) << log2_blksz, 0,
                                           blksz, (char *)ext4fs_indir1_block);
@@ -1607,10 +1633,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. */
@@ -1643,10 +1669,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(__le32_to_cpu
+                           ext4fs_devread((lbaint_t)le32_to_cpu
                                           (inode->b.blocks.
                                            double_indir_block) << log2_blksz,
                                           0, blksz,
@@ -1657,7 +1683,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;
                }
 
@@ -1684,9 +1710,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(__le32_to_cpu
+                       status = ext4fs_devread((lbaint_t)le32_to_cpu
                                                (ext4fs_indir1_block
                                                 [rblock /
                                                  perblock]) << log2_blksz, 0,
@@ -1698,12 +1724,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 {
@@ -1735,10 +1761,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
-                           (__le32_to_cpu(inode->b.blocks.triple_indir_block)
+                           ((lbaint_t)
+                            le32_to_cpu(inode->b.blocks.triple_indir_block)
                             << log2_blksz, 0, blksz,
                             (char *)ext4fs_indir1_block);
                        if (status == 0) {
@@ -1747,7 +1774,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;
                }
 
@@ -1774,11 +1801,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(__le32_to_cpu
+                       status = ext4fs_devread((lbaint_t)le32_to_cpu
                                                (ext4fs_indir1_block
                                                 [rblock /
                                                  perblock_parent]) <<
@@ -1790,7 +1817,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;
                }
@@ -1818,12 +1845,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(__le32_to_cpu
+                           ext4fs_devread((lbaint_t)le32_to_cpu
                                           (ext4fs_indir2_block
                                            [(rblock / perblock_child)
                                             % (blksz / 4)]) << log2_blksz, 0,
@@ -1834,14 +1861,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);
@@ -1849,16 +1876,20 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)
        return blknr;
 }
 
-void ext4fs_close(void)
+/**
+ * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
+ *                         global pointers
+ *
+ * This function assures that for a file with the same name but different size
+ * the sequential store on the ext4 filesystem will be correct.
+ *
+ * In this function the global data, responsible for internal representation
+ * of the ext4 data are initialized to the reset state. Without this, during
+ * replacement of the smaller file with the bigger truncation of new file was
+ * performed.
+ */
+void ext4fs_reinit_global(void)
 {
-       if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
-               ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
-               ext4fs_file = NULL;
-       }
-       if (ext4fs_root != NULL) {
-               free(ext4fs_root);
-               ext4fs_root = NULL;
-       }
        if (ext4fs_indir1_block != NULL) {
                free(ext4fs_indir1_block);
                ext4fs_indir1_block = NULL;
@@ -1878,12 +1909,26 @@ void ext4fs_close(void)
                ext4fs_indir3_blkno = -1;
        }
 }
+void ext4fs_close(void)
+{
+       if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
+               ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
+               ext4fs_file = NULL;
+       }
+       if (ext4fs_root != NULL) {
+               free(ext4fs_root);
+               ext4fs_root = NULL;
+       }
+
+       ext4fs_reinit_global();
+}
 
 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
                                struct ext2fs_node **fnode, int *ftype)
 {
        unsigned int fpos = 0;
        int status;
+       loff_t actread;
        struct ext2fs_node *diro = (struct ext2fs_node *) dir;
 
 #ifdef DEBUG
@@ -1896,14 +1941,19 @@ 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,
                                           sizeof(struct ext2_dirent),
-                                          (char *) &dirent);
-               if (status < 1)
+                                          (char *)&dirent, &actread);
+               if (status < 0)
+                       return 0;
+
+               if (dirent.direntlen == 0) {
+                       printf("Failed to iterate over directory %s\n", name);
                        return 0;
+               }
 
                if (dirent.namelen != 0) {
                        char filename[dirent.namelen + 1];
@@ -1913,8 +1963,9 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
                        status = ext4fs_read_file(diro,
                                                  fpos +
                                                  sizeof(struct ext2_dirent),
-                                                 dirent.namelen, filename);
-                       if (status < 1)
+                                                 dirent.namelen, filename,
+                                                 &actread);
+                       if (status < 0)
                                return 0;
 
                        fdiro = zalloc(sizeof(struct ext2fs_node));
@@ -1922,7 +1973,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';
 
@@ -1937,7 +1988,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) {
@@ -1946,15 +1997,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;
@@ -1973,7 +2024,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) {
@@ -1996,13 +2047,13 @@ int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
                                        printf("< ? > ");
                                        break;
                                }
-                               printf("%10d %s\n",
-                                       __le32_to_cpu(fdiro->inode.size),
+                               printf("%10u %s\n",
+                                      le32_to_cpu(fdiro->inode.size),
                                        filename);
                        }
                        free(fdiro);
                }
-               fpos += __le16_to_cpu(dirent.direntlen);
+               fpos += le16_to_cpu(dirent.direntlen);
        }
        return 0;
 }
@@ -2012,29 +2063,30 @@ static char *ext4fs_read_symlink(struct ext2fs_node *node)
        char *symlink;
        struct ext2fs_node *diro = node;
        int status;
+       loff_t actread;
 
        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);
+       symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
        if (!symlink)
-               return 0;
+               return NULL;
 
-       if (__le32_to_cpu(diro->inode.size) <= 60) {
+       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),
-                                          symlink);
-               if (status == 0) {
+                                          le32_to_cpu(diro->inode.size),
+                                          symlink, &actread);
+               if ((status < 0) || (actread == 0)) {
                        free(symlink);
-                       return 0;
+                       return NULL;
                }
        }
-       symlink[__le32_to_cpu(diro->inode.size)] = '\0';
+       symlink[le32_to_cpu(diro->inode.size)] = '\0';
        return symlink;
 }
 
@@ -2162,11 +2214,10 @@ int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
        return 1;
 }
 
-int ext4fs_open(const char *filename)
+int ext4fs_open(const char *filename, loff_t *len)
 {
        struct ext2fs_node *fdiro = NULL;
        int status;
-       int len;
 
        if (ext4fs_root == NULL)
                return -1;
@@ -2183,10 +2234,10 @@ int ext4fs_open(const char *filename)
                if (status == 0)
                        goto fail;
        }
-       len = __le32_to_cpu(fdiro->inode.size);
+       *len = le32_to_cpu(fdiro->inode.size);
        ext4fs_file = fdiro;
 
-       return len;
+       return 0;
 fail:
        ext4fs_free_node(fdiro, &ext4fs_root->diropen);
 
@@ -2209,16 +2260,26 @@ 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;
 
-       if (__le32_to_cpu(data->sblock.revision_level == 0))
+       /*
+        * The 64bit feature was enabled when metadata_csum was enabled
+        * and we do not support metadata_csum (and cannot reliably find
+        * files when it is set.  Refuse to mount.
+        */
+       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;
+       }
+
+       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;