alloc_stats.c 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * alloc_stats.c --- Update allocation statistics for ext2fs
 *
 * Copyright (C) 2001 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
10
 *
11 12 13 14 15 16 17
 */

#include <stdio.h>

#include "ext2_fs.h"
#include "ext2fs.h"

18 19
void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
			       int inuse, int isdir)
20 21 22
{
	int	group = ext2fs_group_of_ino(fs, ino);

23 24 25
#ifndef OMIT_COM_ERR
	if (ino > fs->super->s_inodes_count) {
		com_err("ext2fs_inode_alloc_stats2", 0,
26
			"Illegal inode number: %lu", (unsigned long) ino);
27 28 29
		return;
	}
#endif
30
	if (inuse > 0)
31
		ext2fs_mark_inode_bitmap2(fs->inode_map, ino);
32
	else
33
		ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
34
	ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) - inuse);
35
	if (isdir)
36
		ext2fs_bg_used_dirs_count_set(fs, group, ext2fs_bg_used_dirs_count(fs, group) + 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
	ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
41 42 43
	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
				       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
		ext2_ino_t first_unused_inode =	fs->super->s_inodes_per_group -
44
			ext2fs_bg_itable_unused(fs, group) +
45 46 47
			group * fs->super->s_inodes_per_group + 1;

		if (ino >= first_unused_inode)
48
			ext2fs_bg_itable_unused_set(fs, group, group * fs->super->s_inodes_per_group + fs->super->s_inodes_per_group - ino);
49 50 51
		ext2fs_group_desc_csum_set(fs, group);
	}

52 53 54 55 56
	fs->super->s_free_inodes_count -= inuse;
	ext2fs_mark_super_dirty(fs);
	ext2fs_mark_ib_dirty(fs);
}

57 58 59 60 61
void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse)
{
	ext2fs_inode_alloc_stats2(fs, ino, inuse, 0);
}

62
void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
63
{
64
	int	group = ext2fs_group_of_blk2(fs, blk);
65

66
#ifndef OMIT_COM_ERR
67
	if (blk >= ext2fs_blocks_count(fs->super)) {
68 69
		com_err("ext2fs_block_alloc_stats", 0,
			"Illegal block number: %lu", (unsigned long) blk);
70 71 72
		return;
	}
#endif
73
	if (inuse > 0)
74
		ext2fs_mark_block_bitmap2(fs->block_map, blk);
75
	else
76
		ext2fs_unmark_block_bitmap2(fs->block_map, blk);
77
	ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) - inuse);
78
	ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
79 80
	ext2fs_group_desc_csum_set(fs, group);

81
	ext2fs_free_blocks_count_add(fs->super, -inuse);
82 83
	ext2fs_mark_super_dirty(fs);
	ext2fs_mark_bb_dirty(fs);
84 85 86 87
	if (fs->block_alloc_stats)
		(fs->block_alloc_stats)(fs, (blk64_t) blk, inuse);
}

88 89 90 91 92
void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
{
	ext2fs_block_alloc_stats2(fs, blk, inuse);
}

93
void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
94 95 96 97 98 99 100 101 102 103 104 105 106
					   void (*func)(ext2_filsys fs,
							blk64_t blk,
							int inuse),
					   void (**old)(ext2_filsys fs,
							blk64_t blk,
							int inuse))
{
	if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
		return;
	if (old)
		*old = fs->block_alloc_stats;

	fs->block_alloc_stats = func;
107
}