]>
Commit | Line | Data |
---|---|---|
8bd0c959 TT |
1 | /* |
2 | * alloc_stats.c --- Update allocation statistics for ext2fs | |
3 | * | |
4 | * Copyright (C) 2001 Theodore Ts'o. | |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the GNU Public | |
8 | * License. | |
9 | * %End-Header% | |
efc6f628 | 10 | * |
8bd0c959 TT |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | ||
15 | #include "ext2_fs.h" | |
16 | #include "ext2fs.h" | |
17 | ||
7f961d42 TT |
18 | void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino, |
19 | int inuse, int isdir) | |
8bd0c959 TT |
20 | { |
21 | int group = ext2fs_group_of_ino(fs, ino); | |
22 | ||
cf4e06d6 TT |
23 | #ifndef OMIT_COM_ERR |
24 | if (ino > fs->super->s_inodes_count) { | |
25 | com_err("ext2fs_inode_alloc_stats2", 0, | |
46d6f84e | 26 | "Illegal inode number: %lu", (unsigned long) ino); |
cf4e06d6 TT |
27 | return; |
28 | } | |
29 | #endif | |
8bd0c959 | 30 | if (inuse > 0) |
8f82ef98 | 31 | ext2fs_mark_inode_bitmap2(fs->inode_map, ino); |
8bd0c959 | 32 | else |
8f82ef98 | 33 | ext2fs_unmark_inode_bitmap2(fs->inode_map, ino); |
8bd0c959 | 34 | fs->group_desc[group].bg_free_inodes_count -= inuse; |
7f961d42 TT |
35 | if (isdir) |
36 | fs->group_desc[group].bg_used_dirs_count += inuse; | |
d4f34d41 | 37 | |
5711ed29 | 38 | /* We don't strictly need to be clearing the uninit flag if inuse < 0 |
d4f34d41 | 39 | * (i.e. freeing inodes) but it also means something is bad. */ |
732c8cd5 | 40 | ext2fs_bg_flag_clear(fs, group, EXT2_BG_INODE_UNINIT); |
d4f34d41 JS |
41 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
42 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
43 | ext2_ino_t first_unused_inode = fs->super->s_inodes_per_group - | |
44 | fs->group_desc[group].bg_itable_unused + | |
45 | group * fs->super->s_inodes_per_group + 1; | |
46 | ||
47 | if (ino >= first_unused_inode) | |
48 | fs->group_desc[group].bg_itable_unused = | |
49 | group * fs->super->s_inodes_per_group + | |
50 | fs->super->s_inodes_per_group - ino; | |
d4f34d41 JS |
51 | ext2fs_group_desc_csum_set(fs, group); |
52 | } | |
53 | ||
8bd0c959 TT |
54 | fs->super->s_free_inodes_count -= inuse; |
55 | ext2fs_mark_super_dirty(fs); | |
56 | ext2fs_mark_ib_dirty(fs); | |
57 | } | |
58 | ||
7f961d42 TT |
59 | void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse) |
60 | { | |
61 | ext2fs_inode_alloc_stats2(fs, ino, inuse, 0); | |
62 | } | |
63 | ||
e8328e31 | 64 | void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse) |
8bd0c959 | 65 | { |
e8328e31 | 66 | int group = ext2fs_group_of_blk2(fs, blk); |
8bd0c959 | 67 | |
cf4e06d6 | 68 | #ifndef OMIT_COM_ERR |
4efbac6f | 69 | if (blk >= ext2fs_blocks_count(fs->super)) { |
46d6f84e TT |
70 | com_err("ext2fs_block_alloc_stats", 0, |
71 | "Illegal block number: %lu", (unsigned long) blk); | |
cf4e06d6 TT |
72 | return; |
73 | } | |
74 | #endif | |
8bd0c959 | 75 | if (inuse > 0) |
8f82ef98 | 76 | ext2fs_mark_block_bitmap2(fs->block_map, blk); |
8bd0c959 | 77 | else |
8f82ef98 | 78 | ext2fs_unmark_block_bitmap2(fs->block_map, blk); |
8bd0c959 | 79 | fs->group_desc[group].bg_free_blocks_count -= inuse; |
732c8cd5 | 80 | ext2fs_bg_flag_clear(fs, group, EXT2_BG_BLOCK_UNINIT); |
d4f34d41 JS |
81 | ext2fs_group_desc_csum_set(fs, group); |
82 | ||
e8328e31 | 83 | ext2fs_free_blocks_count_add(fs->super, -inuse); |
8bd0c959 TT |
84 | ext2fs_mark_super_dirty(fs); |
85 | ext2fs_mark_bb_dirty(fs); | |
f5c562e2 TT |
86 | if (fs->block_alloc_stats) |
87 | (fs->block_alloc_stats)(fs, (blk64_t) blk, inuse); | |
88 | } | |
89 | ||
e8328e31 JS |
90 | void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse) |
91 | { | |
92 | ext2fs_block_alloc_stats2(fs, blk, inuse); | |
93 | } | |
94 | ||
efc6f628 | 95 | void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs, |
f5c562e2 TT |
96 | void (*func)(ext2_filsys fs, |
97 | blk64_t blk, | |
98 | int inuse), | |
99 | void (**old)(ext2_filsys fs, | |
100 | blk64_t blk, | |
101 | int inuse)) | |
102 | { | |
103 | if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS) | |
104 | return; | |
105 | if (old) | |
106 | *old = fs->block_alloc_stats; | |
107 | ||
108 | fs->block_alloc_stats = func; | |
8bd0c959 | 109 | } |