2 * alloc_stats.c --- Update allocation statistics for ext2fs
4 * Copyright (C) 2001 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
17 void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
20 int group = ext2fs_group_of_ino(fs, ino);
23 if (ino > fs->super->s_inodes_count) {
24 com_err("ext2fs_inode_alloc_stats2", 0,
25 "Illegal inode number: %lu", (unsigned long) ino);
30 ext2fs_mark_inode_bitmap2(fs->inode_map, ino);
32 ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
33 ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) - inuse);
35 ext2fs_bg_used_dirs_count_set(fs, group, ext2fs_bg_used_dirs_count(fs, group) + inuse);
37 /* We don't strictly need to be clearing the uninit flag if inuse < 0
38 * (i.e. freeing inodes) but it also means something is bad. */
39 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
40 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
41 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
42 ext2_ino_t first_unused_inode = fs->super->s_inodes_per_group -
43 ext2fs_bg_itable_unused(fs, group) +
44 group * fs->super->s_inodes_per_group + 1;
46 if (ino >= first_unused_inode)
47 ext2fs_bg_itable_unused_set(fs, group, group * fs->super->s_inodes_per_group + fs->super->s_inodes_per_group - ino);
48 ext2fs_group_desc_csum_set(fs, group);
51 fs->super->s_free_inodes_count -= inuse;
52 ext2fs_mark_super_dirty(fs);
53 ext2fs_mark_ib_dirty(fs);
56 void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse)
58 ext2fs_inode_alloc_stats2(fs, ino, inuse, 0);
61 void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
63 int group = ext2fs_group_of_blk2(fs, blk);
66 if (blk >= ext2fs_blocks_count(fs->super)) {
67 com_err("ext2fs_block_alloc_stats", 0,
68 "Illegal block number: %lu", (unsigned long) blk);
73 ext2fs_mark_block_bitmap2(fs->block_map, blk);
75 ext2fs_unmark_block_bitmap2(fs->block_map, blk);
76 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) - inuse);
77 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
78 ext2fs_group_desc_csum_set(fs, group);
80 ext2fs_free_blocks_count_add(fs->super, -inuse);
81 ext2fs_mark_super_dirty(fs);
82 ext2fs_mark_bb_dirty(fs);
83 if (fs->block_alloc_stats)
84 (fs->block_alloc_stats)(fs, (blk64_t) blk, inuse);
87 void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
89 ext2fs_block_alloc_stats2(fs, blk, inuse);
92 void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
93 void (*func)(ext2_filsys fs,
96 void (**old)(ext2_filsys fs,
100 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
103 *old = fs->block_alloc_stats;
105 fs->block_alloc_stats = func;