Refactoring superblock features getters
[lwext4.git] / lwext4 / ext4_fs.c
index dccde5e6745f55b8b54de5fba71897f51ebf2203..cb2c91e54acc6d028f2e99745acd0da394b11ff9 100644 (file)
@@ -44,6 +44,7 @@
 #include "ext4_errno.h"
 #include "ext4_blockdev.h"
 #include "ext4_super.h"
+#include "ext4_crc32c.h"
 #include "ext4_debug.h"
 #include "ext4_block_group.h"
 #include "ext4_balloc.h"
@@ -98,9 +99,10 @@ int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
 
        /*Validate FS*/
        tmp = ext4_get16(&fs->sb, state);
-       if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) {
-               ext4_dprintf(EXT4_DEBUG_FS, "last umount error\n");
-       }
+       if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
+               ext4_dbg(DEBUG_FS, DBG_WARN
+                               "last umount error: superblock fs_error flag\n");
+
 
        /* Mark system as mounted */
        ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
@@ -124,106 +126,75 @@ int ext4_fs_fini(struct ext4_fs *fs)
        return ext4_sb_write(fs->bdev, &fs->sb);
 }
 
-static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
+static void ext4_fs_debug_features_inc(uint32_t features_incompatible)
 {
-
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_COMPRESSION) {
-               ext4_dprintf(EXT4_DEBUG_FS, "compression\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE) {
-               ext4_dprintf(EXT4_DEBUG_FS, "filetype\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER) {
-               ext4_dprintf(EXT4_DEBUG_FS, "recover\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV) {
-               ext4_dprintf(EXT4_DEBUG_FS, "journal_dev\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG) {
-               ext4_dprintf(EXT4_DEBUG_FS, "meta_bg\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS) {
-               ext4_dprintf(EXT4_DEBUG_FS, "extents\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT) {
-               ext4_dprintf(EXT4_DEBUG_FS, "64bit\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP) {
-               ext4_dprintf(EXT4_DEBUG_FS, "mnp\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
-               ext4_dprintf(EXT4_DEBUG_FS, "flex_bg\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE) {
-               ext4_dprintf(EXT4_DEBUG_FS, "ea_inode\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA) {
-               ext4_dprintf(EXT4_DEBUG_FS, "dirdata\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM) {
-               ext4_dprintf(EXT4_DEBUG_FS, "meta_csum\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR) {
-               ext4_dprintf(EXT4_DEBUG_FS, "largedir\n");
-       }
-       if (features_incompatible & EXT4_FEATURE_INCOMPAT_INLINE_DATA) {
-               ext4_dprintf(EXT4_DEBUG_FS, "inline_data\n");
-       }
+       if (features_incompatible & EXT4_FINCOM_COMPRESSION)
+               ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
+       if (features_incompatible & EXT4_FINCOM_FILETYPE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
+       if (features_incompatible & EXT4_FINCOM_RECOVER)
+               ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
+       if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
+               ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
+       if (features_incompatible & EXT4_FINCOM_META_BG)
+               ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
+       if (features_incompatible & EXT4_FINCOM_EXTENTS)
+               ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
+       if (features_incompatible & EXT4_FINCOM_64BIT)
+               ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
+       if (features_incompatible & EXT4_FINCOM_MMP)
+               ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
+       if (features_incompatible & EXT4_FINCOM_FLEX_BG)
+               ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
+       if (features_incompatible & EXT4_FINCOM_EA_INODE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
+       if (features_incompatible & EXT4_FINCOM_DIRDATA)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
+       if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
+               ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
+       if (features_incompatible & EXT4_FINCOM_LARGEDIR)
+               ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
+       if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
+               ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
 }
 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
 {
-       if (features_compatible & EXT4_FEATURE_COMPAT_DIR_PREALLOC) {
-               ext4_dprintf(EXT4_DEBUG_FS, " dir_prealloc\n");
-       }
-       if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES) {
-               ext4_dprintf(EXT4_DEBUG_FS, "imagic_inodes\n");
-       }
-       if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL) {
-               ext4_dprintf(EXT4_DEBUG_FS, "has_journal\n");
-       }
-       if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR) {
-               ext4_dprintf(EXT4_DEBUG_FS, "ext_attr\n");
-       }
-       if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE) {
-               ext4_dprintf(EXT4_DEBUG_FS, "resize_inode\n");
-       }
-       if (features_compatible & EXT4_FEATURE_COMPAT_DIR_INDEX) {
-               ext4_dprintf(EXT4_DEBUG_FS, "dir_index\n");
-       }
+       if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
+       if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
+               ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
+       if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
+               ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
+       if (features_compatible & EXT4_FCOM_EXT_ATTR)
+               ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
+       if (features_compatible & EXT4_FCOM_RESIZE_INODE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
+       if (features_compatible & EXT4_FCOM_DIR_INDEX)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
 }
 
 static void ext4_fs_debug_features_ro(uint32_t features_ro)
 {
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) {
-               ext4_dprintf(EXT4_DEBUG_FS, "sparse_super\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE) {
-               ext4_dprintf(EXT4_DEBUG_FS, "large_file\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR) {
-               ext4_dprintf(EXT4_DEBUG_FS, "btree_dir\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
-               ext4_dprintf(EXT4_DEBUG_FS, "huge_file\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
-               ext4_dprintf(EXT4_DEBUG_FS, "gtd_csum\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK) {
-               ext4_dprintf(EXT4_DEBUG_FS, "dir_nlink\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) {
-               ext4_dprintf(EXT4_DEBUG_FS, "extra_isize\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA) {
-               ext4_dprintf(EXT4_DEBUG_FS, "quota\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
-               ext4_dprintf(EXT4_DEBUG_FS, "bigalloc\n");
-       }
-       if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
-               ext4_dprintf(EXT4_DEBUG_FS, "metadata_csum\n");
-       }
+       if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
+               ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
+       if (features_ro & EXT4_FRO_COM_LARGE_FILE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
+       if (features_ro & EXT4_FRO_COM_BTREE_DIR)
+               ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
+       if (features_ro & EXT4_FRO_COM_HUGE_FILE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
+       if (features_ro & EXT4_FRO_COM_GDT_CSUM)
+               ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
+       if (features_ro & EXT4_FRO_COM_DIR_NLINK)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
+       if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
+       if (features_ro & EXT4_FRO_COM_QUOTA)
+               ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
+       if (features_ro & EXT4_FRO_COM_BIGALLOC)
+               ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
+       if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
+               ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
 }
 
 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
@@ -235,34 +206,32 @@ int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
                return EOK;
        }
 
-       ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_incompatible:\n");
-       ext4_fs_debug_features_incomp(
-           ext4_get32(&fs->sb, features_incompatible));
+       ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
+       ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
 
-       ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_compatible:\n");
+       ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
        ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
 
-       ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_read_only:\n");
+       ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
        ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
 
        /*Check features_incompatible*/
        v = (ext4_get32(&fs->sb, features_incompatible) &
-            (~CONFIG_FEATURE_INCOMPAT_SUPP));
+            (~CONFIG_SUPPORTED_FINCOM));
        if (v) {
-               ext4_dprintf(EXT4_DEBUG_FS, "SB features_incompatible: fail\n");
-               ext4_fs_debug_features_incomp(v);
+               ext4_dbg(DEBUG_FS, DBG_ERROR
+                               "sblock has unsupported features incompatible:\n");
+               ext4_fs_debug_features_inc(v);
                return ENOTSUP;
        }
 
        /*Check features_read_only*/
        v = (ext4_get32(&fs->sb, features_read_only) &
-            (~CONFIG_FEATURE_RO_COMPAT_SUPP));
+            (~CONFIG_SUPPORTED_FRO_COM));
        if (v) {
-               ext4_dprintf(
-                   EXT4_DEBUG_FS,
-                   "\nERROR sblock features_read_only . Unsupported:\n");
-               ext4_fs_debug_features_incomp(v);
-
+               ext4_dbg(DEBUG_FS, DBG_WARN
+                               "sblock has unsupported features read only:\n");
+               ext4_fs_debug_features_ro(v);
                *read_only = true;
                return EOK;
        }
@@ -271,15 +240,69 @@ int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
        return EOK;
 }
 
+/**@brief Determine whether the block is inside the group.
+ * @param baddr   block address
+ * @param bgid    block group id
+ * @return Error code
+ */
+static int ext4_block_in_group(struct ext4_sblock *s,
+                              ext4_fsblk_t baddr,
+                              uint32_t bgid)
+{
+       uint32_t actual_bgid;
+       actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
+       if (actual_bgid == bgid)
+               return 1;
+       return 0;
+}
+
+/**@brief   To avoid calling the atomic setbit hundreds or thousands of times, we only
+ *          need to use it within a single byte (to ensure we get endianness right).
+ *          We can use memset for the rest of the bitmap as there are no other users.
+ */
+static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
+{
+       int i;
+
+       if (start_bit >= end_bit)
+               return;
+
+       for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
+               ext4_bmap_bit_set(bitmap, i);
+
+       if (i < end_bit)
+               memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
+}
+
 /**@brief Initialize block bitmap in block group.
  * @param bg_ref Reference to block group
  * @return Error code
  */
 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
 {
-       uint32_t i;
-       uint32_t bitmap_block_addr =
+       uint32_t i, bit, bit_max;
+       uint32_t group_blocks;
+       uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
+       uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
+       uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
+       ext4_fsblk_t bitmap_block_addr =
            ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
+       ext4_fsblk_t bitmap_inode_addr =
+           ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
+       ext4_fsblk_t inode_table_addr =
+           ext4_bg_get_inode_table_first_block(bg_ref->block_group,
+                                               &bg_ref->fs->sb);
+       ext4_fsblk_t first_group_addr =
+           ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
+
+       uint32_t dsc_per_block =
+           ext4_sb_get_block_size(&bg_ref->fs->sb) /
+           ext4_sb_get_desc_size(&bg_ref->fs->sb);
+
+       bool flex_bg =
+               ext4_sb_feature_incom(&bg_ref->fs->sb, EXT4_FINCOM_FLEX_BG);
+
+       uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
 
        struct ext4_block block_bitmap;
        int rc =
@@ -287,24 +310,73 @@ static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
        if (rc != EOK)
                return rc;
 
-       memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
-
-       /* Determine first block and first data block in group */
-       uint32_t first_idx = 0;
-
-       uint32_t first_data =
-           ext4_balloc_get_first_data_block_in_group(&bg_ref->fs->sb, bg_ref);
-       uint32_t first_data_idx =
-           ext4_fs_baddr2_index_in_group(&bg_ref->fs->sb, first_data);
-
-       /*Set bits from to first block to first data block - 1 to one
-        * (allocated)*/
-       /*TODO: Optimize it*/
-       for (i = first_idx; i < first_data_idx; ++i)
-               ext4_bmap_bit_set(block_bitmap.data, i);
+       memset(block_bitmap.data, 0, block_size);
+
+       bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
+       if (!ext4_sb_feature_incom(&bg_ref->fs->sb, EXT4_FINCOM_META_BG) ||
+                       bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
+                       dsc_per_block) {
+               if (bit_max) {
+                       bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
+                                                  bg_ref->index);
+                       bit_max +=
+                               ext4_get16(&bg_ref->fs->sb,
+                                          s_reserved_gdt_blocks);
+               }
+       } else { /* For META_BG_BLOCK_GROUPS */
+               bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
+                                          bg_ref->index);
+       }
+       for (bit = 0; bit < bit_max; bit++)
+               ext4_bmap_bit_set(block_bitmap.data, bit);
 
+       if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
+               /*
+                * Even though mke2fs always initialize first and last group
+                * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
+                * to make sure we calculate the right free blocks
+                */
+               group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
+                               ext4_get32(&bg_ref->fs->sb, first_data_block) -
+                               (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
+                                (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
+       } else {
+               group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
+       }
+       if (!flex_bg ||
+           ext4_block_in_group(&bg_ref->fs->sb,
+                               bitmap_block_addr, bg_ref->index))
+               ext4_bmap_bit_set(block_bitmap.data,
+                                 bitmap_block_addr - first_group_addr);
+
+       if (!flex_bg ||
+           ext4_block_in_group(&bg_ref->fs->sb,
+                               bitmap_inode_addr, bg_ref->index))
+               ext4_bmap_bit_set(block_bitmap.data,
+                                 bitmap_inode_addr - first_group_addr);
+
+        for (i = inode_table_addr;
+               i < inode_table_addr + inode_table_bcnt; i++) {
+               if (!flex_bg ||
+                   ext4_block_in_group(&bg_ref->fs->sb,
+                                       i,
+                                       bg_ref->index))
+                       ext4_bmap_bit_set(block_bitmap.data,
+                                       i - first_group_addr);
+       }
+        /*
+         * Also if the number of blocks within the group is
+         * less than the blocksize * 8 ( which is the size
+         * of bitmap ), set rest of the block bitmap to 1
+         */
+        ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
        block_bitmap.dirty = true;
 
+       ext4_balloc_set_bitmap_csum(&bg_ref->fs->sb,
+                                   bg_ref->block_group,
+                                   block_bitmap.data);
+       bg_ref->dirty = true;
+
        /* Save bitmap */
        return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
 }
@@ -316,7 +388,7 @@ static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
 {
        /* Load bitmap */
-       uint32_t bitmap_block_addr =
+       ext4_fsblk_t bitmap_block_addr =
            ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
 
        struct ext4_block block_bitmap;
@@ -344,6 +416,11 @@ static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
 
        block_bitmap.dirty = true;
 
+       ext4_ialloc_set_bitmap_csum(&bg_ref->fs->sb,
+                                   bg_ref->block_group,
+                                   block_bitmap.data);
+       bg_ref->dirty = true;
+
        /* Save bitmap */
        return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
 }
@@ -361,16 +438,16 @@ static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
        uint32_t inodes_per_block = block_size / inode_size;
        uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
        uint32_t table_blocks = inodes_in_group / inodes_per_block;
-       uint32_t fblock;
+       ext4_fsblk_t fblock;
 
        if (inodes_in_group % inodes_per_block)
                table_blocks++;
 
        /* Compute initialization bounds */
-       uint32_t first_block =
+       ext4_fsblk_t first_block =
            ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
 
-       uint32_t last_block = first_block + table_blocks - 1;
+       ext4_fsblk_t last_block = first_block + table_blocks - 1;
 
        /* Initialization of all itable blocks */
        for (fblock = first_block; fblock <= last_block; ++fblock) {
@@ -391,7 +468,7 @@ static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
        return EOK;
 }
 
-static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
+static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
                                             uint32_t bgid,
                                             uint32_t dsc_per_block)
 {
@@ -402,8 +479,7 @@ static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
        dsc_id = bgid / dsc_per_block;
        first_meta_bg = ext4_sb_first_meta_bg(s);
 
-       if (!ext4_sb_has_feature_incompatible(s,
-                                             EXT4_FEATURE_INCOMPAT_META_BG) ||
+       if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) ||
            dsc_id < first_meta_bg)
                return ext4_get32(s, first_data_block) + dsc_id + 1;
 
@@ -477,6 +553,12 @@ int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
        return EOK;
 }
 
+/*
+ * BIG FAT NOTES:
+ *       Currently we do not verify the checksum of block_group_desc
+ *       and inode.
+ */
+
 /**@brief  Compute checksum of block group descriptor.
  * @param sb   Superblock
  * @param bgid Index of block group in the filesystem
@@ -490,8 +572,27 @@ static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
        uint16_t crc = 0;
 
        /* Compute the checksum only if the filesystem supports it */
-       if (ext4_sb_has_feature_read_only(sb,
-                                         EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
+       if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
+               /* Use metadata_csum algorithm instead */
+               uint32_t le32_bgid = to_le32(bgid);
+               uint32_t orig_checksum, checksum;
+
+               /* Preparation: temporarily set bg checksum to 0 */
+               orig_checksum = bg->checksum;
+               bg->checksum = 0;
+
+               /* First calculate crc32 checksum against fs uuid */
+               checksum = ext4_crc32c(~0, sb->uuid, sizeof(sb->uuid));
+               /* Then calculate crc32 checksum against bgid */
+               checksum = ext4_crc32c(checksum, &le32_bgid,
+                                    sizeof(bgid));
+               /* Finally calculate crc32 checksum against block_group_desc */
+               checksum = ext4_crc32c(checksum, bg,
+                                    ext4_sb_get_desc_size(sb));
+               bg->checksum = orig_checksum;
+
+               crc = checksum & 0xFFFF;
+       } else if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
                uint8_t *base = (uint8_t *)bg;
                uint8_t *checksum = (uint8_t *)&bg->checksum;
 
@@ -515,8 +616,7 @@ static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
                offset += sizeof(bg->checksum);
 
                /* Checksum of the rest of block group descriptor */
-               if ((ext4_sb_has_feature_incompatible(
-                       sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
+               if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
                    (offset < ext4_sb_get_desc_size(sb)))
 
                        crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
@@ -543,6 +643,51 @@ int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
        return ext4_block_set(ref->fs->bdev, &ref->block);
 }
 
+static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
+{
+       uint32_t checksum = 0;
+       struct ext4_sblock *sb = &inode_ref->fs->sb;
+       uint16_t inode_size = ext4_get16(sb, inode_size);
+
+       if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
+               uint32_t orig_checksum;
+
+               uint32_t ino_index = to_le32(inode_ref->index);
+               uint32_t ino_gen =
+                       to_le32(ext4_inode_get_generation(inode_ref->inode));
+
+               /* Preparation: temporarily set bg checksum to 0 */
+               orig_checksum = ext4_inode_get_checksum(sb, inode_ref->inode);
+               ext4_inode_set_checksum(sb, inode_ref->inode, 0);
+
+               /* First calculate crc32 checksum against fs uuid */
+               checksum = ext4_crc32c(~0, sb->uuid, sizeof(sb->uuid));
+               /* Then calculate crc32 checksum against inode number
+                * and inode generation */
+               checksum = ext4_crc32c(checksum, &ino_index,
+                                    sizeof(ino_index));
+               checksum = ext4_crc32c(checksum, &ino_gen,
+                                    sizeof(ino_gen));
+               /* Finally calculate crc32 checksum against 
+                * the entire inode */
+               checksum = ext4_crc32c(checksum, inode_ref->inode,
+                               inode_size);
+               ext4_inode_set_checksum(sb, inode_ref->inode,
+                               orig_checksum);
+       }
+       return checksum;
+}
+
+static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
+{
+       struct ext4_sblock *sb = &inode_ref->fs->sb;
+       if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
+               return;
+
+       ext4_inode_set_checksum(sb, inode_ref->inode,
+                               ext4_fs_inode_checksum(inode_ref));
+}
+
 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
                          struct ext4_inode_ref *ref)
 {
@@ -581,7 +726,7 @@ int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
        uint32_t byte_offset_in_group = offset_in_group * inode_size;
 
        /* Compute block address */
-       uint64_t block_id =
+       ext4_fsblk_t block_id =
            inode_table_start + (byte_offset_in_group / block_size);
 
        rc = ext4_block_get(fs->bdev, &ref->block, block_id);
@@ -606,6 +751,7 @@ int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
        /* Check if reference modified */
        if (ref->dirty) {
                /* Mark block dirty for writing changes to physical device */
+               ext4_fs_set_inode_checksum(ref);
                ref->block.dirty = true;
        }
 
@@ -621,24 +767,14 @@ void ext4_fs_inode_blocks_init(struct ext4_fs *fs, struct ext4_inode_ref *inode_
        for (i = 0; i < EXT4_INODE_BLOCKS; i++)
                inode->blocks[i] = 0;
 
+       (void)fs;
 #if CONFIG_EXTENT_ENABLE
        /* Initialize extents if needed */
-       if (ext4_sb_has_feature_incompatible(&fs->sb,
-                               EXT4_FEATURE_INCOMPAT_EXTENTS)) {
+       if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
                ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
 
                /* Initialize extent root header */
-               struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
-               ext4_extent_header_set_depth(header, 0);
-               ext4_extent_header_set_entries_count(header, 0);
-               ext4_extent_header_set_generation(header, 0);
-               ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
-
-               uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
-                               sizeof(struct ext4_extent_header)) /
-                       sizeof(struct ext4_extent);
-
-               ext4_extent_header_set_max_entries_count(header, max_entries);
+               ext4_extent_tree_init(inode_ref);
        }
 #endif
 }
@@ -664,6 +800,7 @@ int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
 {
        /* Check if newly allocated i-node will be a directory */
        bool is_dir;
+       uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
 
        is_dir = (filetype == EXT4_DIRENTRY_DIR);
 
@@ -715,6 +852,11 @@ int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
        ext4_inode_set_blocks_count(&fs->sb, inode, 0);
        ext4_inode_set_flags(inode, 0);
        ext4_inode_set_generation(inode, 0);
+       if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
+               ext4_inode_set_extra_isize(inode,
+                               sizeof(struct ext4_inode) -
+                               ext4_offsetof(struct ext4_inode,
+                                       extra_isize));
 
        /* Reset blocks array. For symbolic link inode, just
         * fill in blocks with 0 */
@@ -734,11 +876,11 @@ int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
 {
        struct ext4_fs *fs = inode_ref->fs;
        uint32_t offset;
-       uint32_t suboffset;
+       uint32_t suboff;
+       int rc;
 #if CONFIG_EXTENT_ENABLE
        /* For extents must be data block destroyed by other way */
-       if ((ext4_sb_has_feature_incompatible(&fs->sb,
-                                             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
+       if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
                /* Data structures are released during truncate operation... */
                goto finish;
@@ -747,7 +889,7 @@ int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
        /* Release all indirect (no data) blocks */
 
        /* 1) Single indirect */
-       uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
+       ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
        if (fblock != 0) {
                int rc = ext4_balloc_free_block(inode_ref, fblock);
                if (rc != EOK)
@@ -768,18 +910,18 @@ int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
                if (rc != EOK)
                        return rc;
 
-               uint32_t ind_block;
+               ext4_fsblk_t ind_block;
                for (offset = 0; offset < count; ++offset) {
                        ind_block = to_le32(((uint32_t *)block.data)[offset]);
 
-                       if (ind_block != 0) {
-                               rc = ext4_balloc_free_block(inode_ref,
-                                                           ind_block);
-                               if (rc != EOK) {
-                                       ext4_block_set(fs->bdev, &block);
-                                       return rc;
-                               }
+                       if (ind_block == 0)
+                               continue;
+                       rc = ext4_balloc_free_block(inode_ref, ind_block);
+                       if (rc != EOK) {
+                               ext4_block_set(fs->bdev, &block);
+                               return rc;
                        }
+
                }
 
                ext4_block_set(fs->bdev, &block);
@@ -793,70 +935,63 @@ int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
        /* 3) Tripple indirect */
        struct ext4_block subblock;
        fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
-       if (fblock != 0) {
-               int rc = ext4_block_get(fs->bdev, &block, fblock);
-               if (rc != EOK)
-                       return rc;
+       if (fblock == 0)
+               goto finish;
+       rc = ext4_block_get(fs->bdev, &block, fblock);
+       if (rc != EOK)
+               return rc;
 
-               uint32_t ind_block;
-               for (offset = 0; offset < count; ++offset) {
-                       ind_block = to_le32(((uint32_t *)block.data)[offset]);
+       ext4_fsblk_t ind_block;
+       for (offset = 0; offset < count; ++offset) {
+               ind_block = to_le32(((uint32_t *)block.data)[offset]);
+
+               if (ind_block == 0)
+                       continue;
+               rc = ext4_block_get(fs->bdev, &subblock,
+                               ind_block);
+               if (rc != EOK) {
+                       ext4_block_set(fs->bdev, &block);
+                       return rc;
+               }
 
-                       if (ind_block != 0) {
-                               rc = ext4_block_get(fs->bdev, &subblock,
-                                                   ind_block);
-                               if (rc != EOK) {
-                                       ext4_block_set(fs->bdev, &block);
-                                       return rc;
-                               }
-
-                               uint32_t ind_subblock;
-                               for (suboffset = 0; suboffset < count;
-                                    ++suboffset) {
-                                       ind_subblock = to_le32(
-                                           ((uint32_t *)
-                                                subblock.data)[suboffset]);
-
-                                       if (ind_subblock != 0) {
-                                               rc = ext4_balloc_free_block(
-                                                   inode_ref, ind_subblock);
-                                               if (rc != EOK) {
-                                                       ext4_block_set(
-                                                           fs->bdev,
-                                                           &subblock);
-                                                       ext4_block_set(fs->bdev,
-                                                                      &block);
-                                                       return rc;
-                                               }
-                                       }
-                               }
+               ext4_fsblk_t ind_subblk;
+               for (suboff = 0; suboff < count; ++suboff) {
+                       ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
 
+                       if (ind_subblk == 0)
+                               continue;
+                       rc = ext4_balloc_free_block(inode_ref, ind_subblk);
+                       if (rc != EOK) {
                                ext4_block_set(fs->bdev, &subblock);
-
-                               rc = ext4_balloc_free_block(inode_ref,
-                                                           ind_block);
-                               if (rc != EOK) {
-                                       ext4_block_set(fs->bdev, &block);
-                                       return rc;
-                               }
+                               ext4_block_set(fs->bdev, &block);
+                               return rc;
                        }
+
                }
 
-               ext4_block_set(fs->bdev, &block);
-               rc = ext4_balloc_free_block(inode_ref, fblock);
-               if (rc != EOK)
+               ext4_block_set(fs->bdev, &subblock);
+
+               rc = ext4_balloc_free_block(inode_ref,
+                               ind_block);
+               if (rc != EOK) {
+                       ext4_block_set(fs->bdev, &block);
                        return rc;
+               }
 
-               ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
        }
-#if CONFIG_EXTENT_ENABLE
+
+       ext4_block_set(fs->bdev, &block);
+       rc = ext4_balloc_free_block(inode_ref, fblock);
+       if (rc != EOK)
+               return rc;
+
+       ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
 finish:
-#endif
        /* Mark inode dirty for writing to the physical device */
        inode_ref->dirty = true;
 
        /* Free block with extended attributes if present */
-       uint32_t xattr_block =
+       ext4_fsblk_t xattr_block =
            ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
        if (xattr_block) {
                int rc = ext4_balloc_free_block(inode_ref, xattr_block);
@@ -867,7 +1002,6 @@ finish:
        }
 
        /* Free inode by allocator */
-       int rc;
        if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
                               EXT4_INODE_MODE_DIRECTORY))
                rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
@@ -877,6 +1011,112 @@ finish:
        return rc;
 }
 
+
+/**@brief Release data block from i-node
+ * @param inode_ref I-node to release block from
+ * @param iblock    Logical block to be released
+ * @return Error code
+ */
+static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
+                               uint32_t iblock)
+{
+       ext4_fsblk_t fblock;
+
+       struct ext4_fs *fs = inode_ref->fs;
+
+       /* Extents are handled otherwise = there is not support in this function
+        */
+       ext4_assert(!(
+           ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
+           (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
+
+       struct ext4_inode *inode = inode_ref->inode;
+
+       /* Handle simple case when we are dealing with direct reference */
+       if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
+               fblock = ext4_inode_get_direct_block(inode, iblock);
+
+               /* Sparse file */
+               if (fblock == 0)
+                       return EOK;
+
+               ext4_inode_set_direct_block(inode, iblock, 0);
+               return ext4_balloc_free_block(inode_ref, fblock);
+       }
+
+       /* Determine the indirection level needed to get the desired block */
+       unsigned int level = 0;
+       unsigned int i;
+       for (i = 1; i < 4; i++) {
+               if (iblock < fs->inode_block_limits[i]) {
+                       level = i;
+                       break;
+               }
+       }
+
+       if (level == 0)
+               return EIO;
+
+       /* Compute offsets for the topmost level */
+       uint64_t block_offset_in_level =
+           iblock - fs->inode_block_limits[level - 1];
+       ext4_fsblk_t current_block =
+           ext4_inode_get_indirect_block(inode, level - 1);
+       uint32_t offset_in_block =
+           block_offset_in_level / fs->inode_blocks_per_level[level - 1];
+
+       /*
+        * Navigate through other levels, until we find the block number
+        * or find null reference meaning we are dealing with sparse file
+        */
+       struct ext4_block block;
+
+       while (level > 0) {
+
+               /* Sparse check */
+               if (current_block == 0)
+                       return EOK;
+
+               int rc = ext4_block_get(fs->bdev, &block, current_block);
+               if (rc != EOK)
+                       return rc;
+
+               current_block =
+                   to_le32(((uint32_t *)block.data)[offset_in_block]);
+
+               /* Set zero if physical data block address found */
+               if (level == 1) {
+                       ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
+                       block.dirty = true;
+               }
+
+               rc = ext4_block_set(fs->bdev, &block);
+               if (rc != EOK)
+                       return rc;
+
+               level--;
+
+               /*
+                * If we are on the last level, break here as
+                * there is no next level to visit
+                */
+               if (level == 0)
+                       break;
+
+               /* Visit the next level */
+               block_offset_in_level %= fs->inode_blocks_per_level[level];
+               offset_in_block = block_offset_in_level /
+                                 fs->inode_blocks_per_level[level - 1];
+       }
+
+       fblock = current_block;
+       if (fblock == 0)
+               return EOK;
+
+       /* Physical block is not referenced, it can be released */
+       return ext4_balloc_free_block(inode_ref, fblock);
+}
+
 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
 {
        struct ext4_sblock *sb = &inode_ref->fs->sb;
@@ -915,15 +1155,13 @@ int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
                                    block_size;
        uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
 #if CONFIG_EXTENT_ENABLE
-       if ((ext4_sb_has_feature_incompatible(sb,
-                                             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
+       if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
 
                /* Extents require special operation */
                if (diff_blocks_count) {
-                       int rc = ext4_extent_release_blocks_from(
-                                       inode_ref,
-                                       new_blocks_count);
+                       int rc = ext4_extent_remove_space(inode_ref,
+                                       new_blocks_count, EXT_MAX_BLOCKS);
                        if (rc != EOK)
                                return rc;
 
@@ -950,8 +1188,97 @@ int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
        return EOK;
 }
 
-int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
-                                      uint64_t iblock, uint32_t *fblock)
+/**@brief Compute 'goal' for inode index
+ * @param inode_ref Reference to inode, to allocate block for
+ * @return goal
+ */
+ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
+{
+       uint32_t group_inodes =
+               ext4_get32(&inode_ref->fs->sb, inodes_per_group);
+       return (inode_ref->index - 1) / group_inodes;
+}
+
+/**@brief Compute 'goal' for allocation algorithm (For blockmap).
+ * @param inode_ref Reference to inode, to allocate block for
+ * @param goal
+ * @return error code
+ */
+int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
+                               ext4_fsblk_t *goal)
+{
+       struct ext4_sblock *sb = &inode_ref->fs->sb;
+       *goal = 0;
+
+       uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
+       uint32_t block_size = ext4_sb_get_block_size(sb);
+       uint32_t inode_block_count = inode_size / block_size;
+
+       if (inode_size % block_size != 0)
+               inode_block_count++;
+
+       /* If inode has some blocks, get last block address + 1 */
+       if (inode_block_count > 0) {
+               int rc = ext4_fs_get_inode_data_block_index(
+                   inode_ref, inode_block_count - 1, goal, false);
+               if (rc != EOK)
+                       return rc;
+
+               if (*goal != 0) {
+                       (*goal)++;
+                       return rc;
+               }
+
+               /* If goal == 0, sparse file -> continue */
+       }
+
+       /* Identify block group of inode */
+
+       uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
+       uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
+       block_size = ext4_sb_get_block_size(sb);
+
+       /* Load block group reference */
+       struct ext4_block_group_ref bg_ref;
+       int rc =
+           ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
+       if (rc != EOK)
+               return rc;
+
+       /* Compute indexes */
+       uint32_t block_group_count = ext4_block_group_cnt(sb);
+       ext4_fsblk_t inode_table_first_block =
+           ext4_bg_get_inode_table_first_block(bg_ref.block_group, sb);
+       uint16_t inode_table_item_size = ext4_get16(sb, inode_size);
+       uint32_t inode_table_bytes;
+
+       /* Check for last block group */
+       if (block_group < block_group_count - 1) {
+               inode_table_bytes = inodes_per_group * inode_table_item_size;
+       } else {
+               /* Last block group could be smaller */
+               uint32_t inodes_count_total = ext4_get32(sb, inodes_count);
+
+               inode_table_bytes =
+                   (inodes_count_total -
+                    ((block_group_count - 1) * inodes_per_group)) *
+                   inode_table_item_size;
+       }
+
+       ext4_fsblk_t inode_table_blocks = inode_table_bytes / block_size;
+
+       if (inode_table_bytes % block_size)
+               inode_table_blocks++;
+
+       *goal = inode_table_first_block + inode_table_blocks;
+
+       return ext4_fs_put_block_group_ref(&bg_ref);
+}
+
+static int ext4_fs_get_inode_data_block_idx(struct ext4_inode_ref *inode_ref,
+                                      uint64_t iblock, ext4_fsblk_t *fblock,
+                                      bool extent_create,
+                                      bool support_unwritten)
 {
        struct ext4_fs *fs = inode_ref->fs;
 
@@ -961,19 +1288,24 @@ int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
                return EOK;
        }
 
-       uint32_t current_block;
+       ext4_fsblk_t current_block;
+
+       (void)extent_create;
 #if CONFIG_EXTENT_ENABLE
        /* Handle i-node using extents */
-       if ((ext4_sb_has_feature_incompatible(&fs->sb,
-                                             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
+       if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
 
-               int rc =
-                   ext4_extent_find_block(inode_ref, iblock, &current_block);
+               ext4_fsblk_t current_fsblk;
+               int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
+                               &current_fsblk, extent_create, NULL);
                if (rc != EOK)
                        return rc;
 
+               current_block = current_fsblk;
                *fblock = current_block;
+
+               ext4_assert(*fblock || support_unwritten);
                return EOK;
        }
 #endif
@@ -1060,15 +1392,30 @@ int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
        return EOK;
 }
 
-int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
-                                      uint64_t iblock, uint32_t fblock)
+
+int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
+                                      uint64_t iblock, ext4_fsblk_t *fblock,
+                                      bool support_unwritten)
+{
+       return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
+                       false, support_unwritten);
+}
+
+int ext4_fs_init_inode_data_block_index(struct ext4_inode_ref *inode_ref,
+                                      uint64_t iblock, ext4_fsblk_t *fblock)
+{
+       return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
+                       true, true);
+}
+
+static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
+                                      uint64_t iblock, ext4_fsblk_t fblock)
 {
        struct ext4_fs *fs = inode_ref->fs;
 
 #if CONFIG_EXTENT_ENABLE
        /* Handle inode using extents */
-       if ((ext4_sb_has_feature_incompatible(&fs->sb,
-                                             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
+       if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
                /* Not reachable */
                return ENOTSUP;
@@ -1078,7 +1425,7 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
        /* Handle simple case when we are dealing with direct reference */
        if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
                ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
-                                           fblock);
+                                           (uint32_t)fblock);
                inode_ref->dirty = true;
 
                return EOK;
@@ -1102,12 +1449,12 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
        /* Compute offsets for the topmost level */
        uint64_t block_offset_in_level =
            iblock - fs->inode_block_limits[level - 1];
-       uint32_t current_block =
+       ext4_fsblk_t current_block =
            ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
        uint32_t offset_in_block =
            block_offset_in_level / fs->inode_blocks_per_level[level - 1];
 
-       uint32_t new_block_addr;
+       ext4_fsblk_t new_block_addr;
 
        struct ext4_block block;
        struct ext4_block new_block;
@@ -1115,13 +1462,20 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
        /* Is needed to allocate indirect block on the i-node level */
        if (current_block == 0) {
                /* Allocate new indirect block */
-               int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
+               ext4_fsblk_t goal;
+               int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
+               if (rc != EOK)
+                       return rc;
+
+               rc = ext4_balloc_alloc_block(inode_ref,
+                                            goal,
+                                            &new_block_addr);
                if (rc != EOK)
                        return rc;
 
                /* Update i-node */
                ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
-                                             new_block_addr);
+                                             (uint32_t)new_block_addr);
                inode_ref->dirty = true;
 
                /* Load newly allocated block */
@@ -1156,9 +1510,16 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
                    to_le32(((uint32_t *)block.data)[offset_in_block]);
 
                if ((level > 1) && (current_block == 0)) {
+                       ext4_fsblk_t goal;
+                       rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
+                       if (rc != EOK) {
+                               ext4_block_set(fs->bdev, &block);
+                               return rc;
+                       }
+
                        /* Allocate new block */
                        rc =
-                           ext4_balloc_alloc_block(inode_ref, &new_block_addr);
+                           ext4_balloc_alloc_block(inode_ref, goal, &new_block_addr);
                        if (rc != EOK) {
                                ext4_block_set(fs->bdev, &block);
                                return rc;
@@ -1185,7 +1546,7 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
 
                        /* Write block address to the parent */
                        ((uint32_t *)block.data)[offset_in_block] =
-                           to_le32(new_block_addr);
+                           to_le32((uint32_t)new_block_addr);
                        block.dirty = true;
                        current_block = new_block_addr;
                }
@@ -1193,7 +1554,7 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
                /* Will be finished, write the fblock address */
                if (level == 1) {
                        ((uint32_t *)block.data)[offset_in_block] =
-                           to_le32(fblock);
+                           to_le32((uint32_t)fblock);
                        block.dirty = true;
                }
 
@@ -1219,117 +1580,35 @@ int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
        return EOK;
 }
 
-int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
-                               uint32_t iblock)
-{
-       uint32_t fblock;
-
-       struct ext4_fs *fs = inode_ref->fs;
-
-       /* Extents are handled otherwise = there is not support in this function
-        */
-       ext4_assert(!(
-           ext4_sb_has_feature_incompatible(&fs->sb,
-                                            EXT4_FEATURE_INCOMPAT_EXTENTS) &&
-           (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
 
-       struct ext4_inode *inode = inode_ref->inode;
-
-       /* Handle simple case when we are dealing with direct reference */
-       if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
-               fblock = ext4_inode_get_direct_block(inode, iblock);
-
-               /* Sparse file */
-               if (fblock == 0)
-                       return EOK;
-
-               ext4_inode_set_direct_block(inode, iblock, 0);
-               return ext4_balloc_free_block(inode_ref, fblock);
-       }
-
-       /* Determine the indirection level needed to get the desired block */
-       unsigned int level = 0;
-       unsigned int i;
-       for (i = 1; i < 4; i++) {
-               if (iblock < fs->inode_block_limits[i]) {
-                       level = i;
-                       break;
-               }
-       }
-
-       if (level == 0)
-               return EIO;
-
-       /* Compute offsets for the topmost level */
-       uint64_t block_offset_in_level =
-           iblock - fs->inode_block_limits[level - 1];
-       uint32_t current_block =
-           ext4_inode_get_indirect_block(inode, level - 1);
-       uint32_t offset_in_block =
-           block_offset_in_level / fs->inode_blocks_per_level[level - 1];
-
-       /*
-        * Navigate through other levels, until we find the block number
-        * or find null reference meaning we are dealing with sparse file
-        */
-       struct ext4_block block;
-
-       while (level > 0) {
-
-               /* Sparse check */
-               if (current_block == 0)
-                       return EOK;
-
-               int rc = ext4_block_get(fs->bdev, &block, current_block);
-               if (rc != EOK)
-                       return rc;
-
-               current_block =
-                   to_le32(((uint32_t *)block.data)[offset_in_block]);
+int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
+                              ext4_fsblk_t *fblock, uint32_t *iblock)
+{
+#if CONFIG_EXTENT_ENABLE
+       /* Handle extents separately */
+       if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
+           (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
+               int rc;
+               ext4_fsblk_t current_fsblk;
+               struct ext4_sblock *sb = &inode_ref->fs->sb;
+               uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
+               uint32_t block_size = ext4_sb_get_block_size(sb);
+               *iblock = (inode_size + block_size - 1) /
+                                   block_size;
 
-               /* Set zero if physical data block address found */
-               if (level == 1) {
-                       ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
-                       block.dirty = true;
-               }
+               rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
+                               &current_fsblk, true, NULL);
 
-               rc = ext4_block_set(fs->bdev, &block);
-               if (rc != EOK)
-                       return rc;
 
-               level--;
+               *fblock = current_fsblk;
+               ext4_assert(*fblock);
 
-               /*
-                * If we are on the last level, break here as
-                * there is no next level to visit
-                */
-               if (level == 0)
-                       break;
-
-               /* Visit the next level */
-               block_offset_in_level %= fs->inode_blocks_per_level[level];
-               offset_in_block = block_offset_in_level /
-                                 fs->inode_blocks_per_level[level - 1];
-       }
+               ext4_inode_set_size(inode_ref->inode,
+                                   inode_size + block_size);
+               inode_ref->dirty = true;
 
-       fblock = current_block;
-       if (fblock == 0)
-               return EOK;
 
-       /* Physical block is not referenced, it can be released */
-       return ext4_balloc_free_block(inode_ref, fblock);
-}
-
-int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
-                              uint32_t *fblock, uint32_t *iblock)
-{
-#if CONFIG_EXTENT_ENABLE
-       /* Handle extents separately */
-       if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
-                                             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
-           (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
-               return ext4_extent_append_block(inode_ref, iblock, fblock,
-                                               true);
+               return rc;
        }
 #endif
        struct ext4_sblock *sb = &inode_ref->fs->sb;
@@ -1346,8 +1625,12 @@ int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
        uint32_t new_block_idx = inode_size / block_size;
 
        /* Allocate new physical block */
-       uint32_t phys_block;
-       int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
+       ext4_fsblk_t goal, phys_block;
+       int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
+       if (rc != EOK)
+               return rc;
+
+       rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
        if (rc != EOK)
                return rc;
 
@@ -1378,8 +1661,7 @@ void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
        ext4_inode_set_links_count(inode_ref->inode, link);
 
        bool is_dx =
-           ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
-                                          EXT4_FEATURE_COMPAT_DIR_INDEX) &&
+           ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
            ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
 
        if (is_dx && link > 1) {
@@ -1388,7 +1670,7 @@ void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
 
                        uint32_t v =
                            ext4_get32(&inode_ref->fs->sb, features_read_only);
-                       v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
+                       v |= EXT4_FRO_COM_DIR_NLINK;
                        ext4_set32(&inode_ref->fs->sb, features_read_only, v);
                }
        }