]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - lib/ext2fs/alloc_stats.c
Merge branch 'maint' into next
[thirdparty/e2fsprogs.git] / lib / ext2fs / alloc_stats.c
index 0f276659389dea59dfec02c62c1c26b06f308339..aca5004f37b03e3bd5fea00ba96f80f0f0d14100 100644 (file)
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 
 #include "ext2_fs.h"
@@ -19,13 +20,13 @@ void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
 {
        int     group = ext2fs_group_of_ino(fs, ino);
 
-#ifndef OMIT_COM_ERR
        if (ino > fs->super->s_inodes_count) {
+#ifndef OMIT_COM_ERR
                com_err("ext2fs_inode_alloc_stats2", 0,
                        "Illegal inode number: %lu", (unsigned long) ino);
+#endif
                return;
        }
-#endif
        if (inuse > 0)
                ext2fs_mark_inode_bitmap2(fs->inode_map, ino);
        else
@@ -37,8 +38,7 @@ void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
        /* We don't strictly need to be clearing the uninit flag if inuse < 0
         * (i.e. freeing inodes) but it also means something is bad. */
        ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
-       if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
-                                      EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
+       if (ext2fs_has_group_desc_csum(fs)) {
                ext2_ino_t first_unused_inode = fs->super->s_inodes_per_group -
                        ext2fs_bg_itable_unused(fs, group) +
                        group * fs->super->s_inodes_per_group + 1;
@@ -62,13 +62,13 @@ void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
 {
        int     group = ext2fs_group_of_blk2(fs, blk);
 
-#ifndef OMIT_COM_ERR
        if (blk >= ext2fs_blocks_count(fs->super)) {
+#ifndef OMIT_COM_ERR
                com_err("ext2fs_block_alloc_stats", 0,
                        "Illegal block number: %lu", (unsigned long) blk);
+#endif
                return;
        }
-#endif
        if (inuse > 0)
                ext2fs_mark_block_bitmap2(fs->block_map, blk);
        else
@@ -77,7 +77,8 @@ void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
        ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
        ext2fs_group_desc_csum_set(fs, group);
 
-       ext2fs_free_blocks_count_add(fs->super, -inuse);
+       ext2fs_free_blocks_count_add(fs->super,
+                            -inuse * (blk64_t) EXT2FS_CLUSTER_RATIO(fs));
        ext2fs_mark_super_dirty(fs);
        ext2fs_mark_bb_dirty(fs);
        if (fs->block_alloc_stats)
@@ -104,3 +105,44 @@ void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
 
        fs->block_alloc_stats = func;
 }
+
+void ext2fs_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
+                                   blk_t num, int inuse)
+{
+#ifndef OMIT_COM_ERR
+       if (blk + num > ext2fs_blocks_count(fs->super)) {
+               com_err("ext2fs_block_alloc_stats_range", 0,
+                       "Illegal block range: %llu (%u) ",
+                       (unsigned long long) blk, num);
+               return;
+       }
+#endif
+       if (inuse == 0)
+               return;
+       if (inuse > 0) {
+               ext2fs_mark_block_bitmap_range2(fs->block_map, blk, num);
+               inuse = 1;
+       } else {
+               ext2fs_unmark_block_bitmap_range2(fs->block_map, blk, num);
+               inuse = -1;
+       }
+       while (num) {
+               int group = ext2fs_group_of_blk2(fs, blk);
+               blk64_t last_blk = ext2fs_group_last_block2(fs, group);
+               blk64_t n = num;
+
+               if (blk + num > last_blk)
+                       n = last_blk - blk + 1;
+
+               ext2fs_bg_free_blocks_count_set(fs, group,
+                       ext2fs_bg_free_blocks_count(fs, group) -
+                       inuse*n/EXT2FS_CLUSTER_RATIO(fs));
+               ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
+               ext2fs_group_desc_csum_set(fs, group);
+               ext2fs_free_blocks_count_add(fs->super, -inuse * (blk64_t) n);
+               blk += n;
+               num -= n;
+       }
+       ext2fs_mark_super_dirty(fs);
+       ext2fs_mark_bb_dirty(fs);
+}