Add CONFIG_EXTENT_FULL compilation flag
[lwext4.git] / lwext4 / ext4_fs.c
index 0668b553f0df86ab5ea4e89b130ab7efb6a6afcc..c7d3a9d6f789b80051954784fd2d89939530dfaa 100644 (file)
@@ -98,9 +98,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 +125,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_FEATURE_INCOMPAT_COMPRESSION)
+               ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER)
+               ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)
+               ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG)
+               ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS)
+               ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT)
+               ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP)
+               ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG)
+               ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM)
+               ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR)
+               ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
+       if (features_incompatible & EXT4_FEATURE_INCOMPAT_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_FEATURE_COMPAT_DIR_PREALLOC)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
+       if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES)
+               ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
+       if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
+               ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
+       if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR)
+               ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
+       if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
+       if (features_compatible & EXT4_FEATURE_COMPAT_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_FEATURE_RO_COMPAT_SPARSE_SUPER)
+               ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
+               ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
+               ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
+               ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
+               ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA)
+               ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC)
+               ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
+       if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
+               ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
 }
 
 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
@@ -235,22 +205,22 @@ 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));
        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;
        }
 
@@ -258,11 +228,9 @@ int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
        v = (ext4_get32(&fs->sb, features_read_only) &
             (~CONFIG_FEATURE_RO_COMPAT_SUPP));
        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 +239,70 @@ 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,
+                              uint32_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 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);
        uint32_t bitmap_block_addr =
            ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
+       uint32_t bitmap_inode_addr =
+           ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
+       uint32_t inode_table_addr =
+           ext4_bg_get_inode_table_first_block(bg_ref->block_group,
+                                               &bg_ref->fs->sb);
+       uint32_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_has_feature_incompatible(&bg_ref->fs->sb,
+                                                EXT4_FEATURE_INCOMPAT_FLEX_BG);
+
+       uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
 
        struct ext4_block block_bitmap;
        int rc =
@@ -287,22 +310,67 @@ 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;
+       memset(block_bitmap.data, 0, block_size);
 
-       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);
+       bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
+       if (!ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
+                                             EXT4_FEATURE_INCOMPAT_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;
 
        /* Save bitmap */
@@ -734,7 +802,8 @@ 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,
@@ -772,14 +841,14 @@ int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
                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,65 +862,58 @@ 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]);
+       uint32_t ind_block;
+       for (offset = 0; offset < count; ++offset) {
+               ind_block = to_le32(((uint32_t *)block.data)[offset]);
 
-                       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;
-                                               }
-                                       }
-                               }
+               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;
+               }
 
-                               ext4_block_set(fs->bdev, &subblock);
+               uint32_t ind_subblk;
+               for (suboff = 0; suboff < count; ++suboff) {
+                       ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
 
-                               rc = ext4_balloc_free_block(inode_ref,
-                                                           ind_block);
-                               if (rc != EOK) {
-                                       ext4_block_set(fs->bdev, &block);
-                                       return rc;
-                               }
+                       if (ind_subblk == 0)
+                               continue;
+                       rc = ext4_balloc_free_block(inode_ref, ind_subblk);
+                       if (rc != EOK) {
+                               ext4_block_set(fs->bdev, &subblock);
+                               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;
 
@@ -867,7 +929,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);
@@ -908,25 +969,26 @@ int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
        }
 
        /* Compute how many blocks will be released */
-       uint64_t size_diff = old_size - new_size;
        uint32_t block_size = ext4_sb_get_block_size(sb);
-       uint32_t diff_blocks_count = size_diff / block_size;
-       if (size_diff % block_size != 0)
-               diff_blocks_count++;
-
-       uint32_t old_blocks_count = old_size / block_size;
-       if (old_size % block_size != 0)
-               old_blocks_count++;
+       uint32_t new_blocks_count = (new_size + block_size - 1) /
+                                   block_size;
+       uint32_t old_blocks_count = (old_size + block_size - 1) /
+                                   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)) &&
            (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
 
                /* Extents require special operation */
-               int rc = ext4_extent_release_blocks_from(
-                   inode_ref, old_blocks_count - diff_blocks_count);
-               if (rc != EOK)
-                       return rc;
+               if (diff_blocks_count) {
+                       int rc = ext4_extent_release_blocks_from(
+                                       inode_ref,
+                                       new_blocks_count);
+                       if (rc != EOK)
+                               return rc;
+
+               }
        } else
 #endif
        {
@@ -934,9 +996,9 @@ int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
 
                /* Starting from 1 because of logical blocks are numbered from 0
                 */
-               for (i = 1; i <= diff_blocks_count; ++i) {
+               for (i = 0; i < diff_blocks_count; ++i) {
                        int rc = ext4_fs_release_inode_block(
-                           inode_ref, old_blocks_count - i);
+                           inode_ref, new_blocks_count + i);
                        if (rc != EOK)
                                return rc;
                }