alloc_stats.c revision f5c562e2324a8950d659ebfc8db4356121d6104e
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%
10 *
11 */
12
13#include <stdio.h>
14
15#include "ext2_fs.h"
16#include "ext2fs.h"
17
18void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
19			       int inuse, int isdir)
20{
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;
28	if (isdir)
29		fs->group_desc[group].bg_used_dirs_count += inuse;
30
31	/* We don't strictly need to be clearing the uninit flag 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	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
35				       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
36		ext2_ino_t first_unused_inode =	fs->super->s_inodes_per_group -
37			fs->group_desc[group].bg_itable_unused +
38			group * fs->super->s_inodes_per_group + 1;
39
40		if (ino >= first_unused_inode)
41			fs->group_desc[group].bg_itable_unused =
42				group * fs->super->s_inodes_per_group +
43				fs->super->s_inodes_per_group - ino;
44		ext2fs_group_desc_csum_set(fs, group);
45	}
46
47	fs->super->s_free_inodes_count -= inuse;
48	ext2fs_mark_super_dirty(fs);
49	ext2fs_mark_ib_dirty(fs);
50}
51
52void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse)
53{
54	ext2fs_inode_alloc_stats2(fs, ino, inuse, 0);
55}
56
57void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
58{
59	int	group = ext2fs_group_of_blk(fs, blk);
60
61	if (inuse > 0)
62		ext2fs_mark_block_bitmap(fs->block_map, blk);
63	else
64		ext2fs_unmark_block_bitmap(fs->block_map, blk);
65	fs->group_desc[group].bg_free_blocks_count -= inuse;
66	fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
67	ext2fs_group_desc_csum_set(fs, group);
68
69	fs->super->s_free_blocks_count -= inuse;
70	ext2fs_mark_super_dirty(fs);
71	ext2fs_mark_bb_dirty(fs);
72	if (fs->block_alloc_stats)
73		(fs->block_alloc_stats)(fs, (blk64_t) blk, inuse);
74}
75
76void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
77					   void (*func)(ext2_filsys fs,
78							blk64_t blk,
79							int inuse),
80					   void (**old)(ext2_filsys fs,
81							blk64_t blk,
82							int inuse))
83{
84	if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
85		return;
86	if (old)
87		*old = fs->block_alloc_stats;
88
89	fs->block_alloc_stats = func;
90}
91