Add comments to crc32c module
[lwext4.git] / lwext4 / ext4_ialloc.c
index 3c10e5e74f4929a7a6384faa1e79a8401bebc78e..63e26242368d281246743a913e0d95bacb76788f 100644 (file)
@@ -158,7 +158,6 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
     uint32_t bgid = fs->last_inode_bg_id;\r
     uint32_t bg_count = ext4_block_group_cnt(sb);\r
     uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);\r
-    uint32_t avg_free_inodes = sb_free_inodes / bg_count;\r
     bool     rewind = false;\r
 \r
     /* Try to find free i-node in all block groups */\r
@@ -186,15 +185,17 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
         uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);\r
 \r
         /* Check if this block group is good candidate for allocation */\r
-        if (free_inodes >= avg_free_inodes) {\r
+        if (free_inodes > 0) {\r
             /* Load block with bitmap */\r
             uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(\r
                     bg_ref.block_group, sb);\r
 \r
             struct ext4_block bitmap_block;\r
             rc = ext4_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);\r
-            if (rc != EOK)\r
+            if (rc != EOK){\r
+                ext4_fs_put_block_group_ref(&bg_ref);\r
                 return rc;\r
+            }\r
 \r
             /* Try to allocate i-node in the bitmap */\r
             uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bgid);\r
@@ -204,8 +205,16 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
                     &index_in_group);\r
             /* Block group has not any free i-node */\r
             if (rc == ENOSPC) {\r
-                ext4_block_set(fs->bdev, &bitmap_block);\r
-                ext4_fs_put_block_group_ref(&bg_ref);\r
+                rc = ext4_block_set(fs->bdev, &bitmap_block);\r
+                if(rc != EOK){\r
+                    ext4_fs_put_block_group_ref(&bg_ref);\r
+                    return rc;\r
+                }\r
+\r
+                rc = ext4_fs_put_block_group_ref(&bg_ref);\r
+                if (rc != EOK)\r
+                    return rc;\r
+\r
                 continue;\r
             }\r
 \r
@@ -215,8 +224,10 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
             bitmap_block.dirty = true;\r
 \r
             ext4_block_set(fs->bdev, &bitmap_block);\r
-            if (rc != EOK)\r
+            if (rc != EOK){\r
+                ext4_fs_put_block_group_ref(&bg_ref);\r
                 return rc;\r
+            }\r
 \r
             /* Modify filesystem counters */\r
             free_inodes--;\r
@@ -268,6 +279,9 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir)
 \r
         /* Block group not modified, put it and jump to the next block group */\r
         ext4_fs_put_block_group_ref(&bg_ref);\r
+        if (rc != EOK)\r
+            return rc;\r
+\r
         ++bgid;\r
     }\r
 \r