blob: 725f28d1f96641578ad835b028fe4203a8dd30e0 [file] [log] [blame]
Theodore Ts'o8bd0c952002-01-03 03:29:19 -05001/*
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%
10 *
11 */
12
13#include <stdio.h>
14
15#include "ext2_fs.h"
16#include "ext2fs.h"
17
Theodore Ts'o7f961d42002-02-03 01:28:52 -050018void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
19 int inuse, int isdir)
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050020{
21 int group = ext2fs_group_of_ino(fs, ino);
22
23 if (inuse > 0)
24 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
25 else
26 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
27 fs->group_desc[group].bg_free_inodes_count -= inuse;
Theodore Ts'o7f961d42002-02-03 01:28:52 -050028 if (isdir)
29 fs->group_desc[group].bg_used_dirs_count += inuse;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050030
31 /* We don't strictly need to be clearing these if inuse < 0
32 * (i.e. freeing inodes) but it also means something is bad. */
33 fs->group_desc[group].bg_flags &= ~(EXT2_BG_INODE_UNINIT |
34 EXT2_BG_BLOCK_UNINIT);
35 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
36 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
37 ext2_ino_t first_unused_inode = fs->super->s_inodes_per_group -
38 fs->group_desc[group].bg_itable_unused +
39 group * fs->super->s_inodes_per_group + 1;
40
41 if (ino >= first_unused_inode)
42 fs->group_desc[group].bg_itable_unused =
43 group * fs->super->s_inodes_per_group +
44 fs->super->s_inodes_per_group - ino;
45
46 ext2fs_group_desc_csum_set(fs, group);
47 }
48
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050049 fs->super->s_free_inodes_count -= inuse;
50 ext2fs_mark_super_dirty(fs);
51 ext2fs_mark_ib_dirty(fs);
52}
53
Theodore Ts'o7f961d42002-02-03 01:28:52 -050054void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse)
55{
56 ext2fs_inode_alloc_stats2(fs, ino, inuse, 0);
57}
58
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050059void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
60{
61 int group = ext2fs_group_of_blk(fs, blk);
62
63 if (inuse > 0)
64 ext2fs_mark_block_bitmap(fs->block_map, blk);
65 else
66 ext2fs_unmark_block_bitmap(fs->block_map, blk);
67 fs->group_desc[group].bg_free_blocks_count -= inuse;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050068 fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
69 ext2fs_group_desc_csum_set(fs, group);
70
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050071 fs->super->s_free_blocks_count -= inuse;
72 ext2fs_mark_super_dirty(fs);
73 ext2fs_mark_bb_dirty(fs);
74}