alloc_stats.c revision e8328e31a98a92b39611314612a40b6113921fe2
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#ifndef OMIT_COM_ERR
24	if (ino > fs->super->s_inodes_count) {
25		com_err("ext2fs_inode_alloc_stats2", 0,
26			"Illegal inode number: %lu", (unsigned long) ino);
27		return;
28	}
29#endif
30	if (inuse > 0)
31		ext2fs_mark_inode_bitmap(fs->inode_map, ino);
32	else
33		ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
34	fs->group_desc[group].bg_free_inodes_count -= inuse;
35	if (isdir)
36		fs->group_desc[group].bg_used_dirs_count += inuse;
37
38	/* We don't strictly need to be clearing the uninit flag if inuse < 0
39	 * (i.e. freeing inodes) but it also means something is bad. */
40	fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
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;
51		ext2fs_group_desc_csum_set(fs, group);
52	}
53
54	fs->super->s_free_inodes_count -= inuse;
55	ext2fs_mark_super_dirty(fs);
56	ext2fs_mark_ib_dirty(fs);
57}
58
59void 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
64void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
65{
66	int	group = ext2fs_group_of_blk2(fs, blk);
67
68#ifndef OMIT_COM_ERR
69	if (blk >= fs->super->s_blocks_count) {
70		com_err("ext2fs_block_alloc_stats", 0,
71			"Illegal block number: %lu", (unsigned long) blk);
72		return;
73	}
74#endif
75	if (inuse > 0)
76		/* FIXME-64 */
77		ext2fs_mark_block_bitmap(fs->block_map, blk);
78	else
79		/* FIXME-64 */
80		ext2fs_unmark_block_bitmap(fs->block_map, blk);
81	fs->group_desc[group].bg_free_blocks_count -= inuse;
82	fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
83	ext2fs_group_desc_csum_set(fs, group);
84
85	ext2fs_free_blocks_count_add(fs->super, -inuse);
86	ext2fs_mark_super_dirty(fs);
87	ext2fs_mark_bb_dirty(fs);
88	if (fs->block_alloc_stats)
89		(fs->block_alloc_stats)(fs, (blk64_t) blk, inuse);
90}
91
92void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
93{
94	ext2fs_block_alloc_stats2(fs, blk, inuse);
95}
96
97void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
98					   void (*func)(ext2_filsys fs,
99							blk64_t blk,
100							int inuse),
101					   void (**old)(ext2_filsys fs,
102							blk64_t blk,
103							int inuse))
104{
105	if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
106		return;
107	if (old)
108		*old = fs->block_alloc_stats;
109
110	fs->block_alloc_stats = func;
111}
112