• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #include <stdio.h>
13 
14 #include "ext2_fs.h"
15 #include "ext2fs.h"
16 
ext2fs_inode_alloc_stats2(ext2_filsys fs,ext2_ino_t ino,int inuse,int isdir)17 void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
18 			       int inuse, int isdir)
19 {
20 	int	group = ext2fs_group_of_ino(fs, ino);
21 
22 #ifndef OMIT_COM_ERR
23 	if (ino > fs->super->s_inodes_count) {
24 		com_err("ext2fs_inode_alloc_stats2", 0,
25 			"Illegal inode number: %lu", (unsigned long) ino);
26 		return;
27 	}
28 #endif
29 	if (inuse > 0)
30 		ext2fs_mark_inode_bitmap(fs->inode_map, ino);
31 	else
32 		ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
33 	fs->group_desc[group].bg_free_inodes_count -= inuse;
34 	if (isdir)
35 		fs->group_desc[group].bg_used_dirs_count += inuse;
36 
37 	/* We don't strictly need to be clearing the uninit flag if inuse < 0
38 	 * (i.e. freeing inodes) but it also means something is bad. */
39 	fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
40 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
41 				       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
42 		ext2_ino_t first_unused_inode =	fs->super->s_inodes_per_group -
43 			fs->group_desc[group].bg_itable_unused +
44 			group * fs->super->s_inodes_per_group + 1;
45 
46 		if (ino >= first_unused_inode)
47 			fs->group_desc[group].bg_itable_unused =
48 				group * fs->super->s_inodes_per_group +
49 				fs->super->s_inodes_per_group - ino;
50 		ext2fs_group_desc_csum_set(fs, group);
51 	}
52 
53 	fs->super->s_free_inodes_count -= inuse;
54 	ext2fs_mark_super_dirty(fs);
55 	ext2fs_mark_ib_dirty(fs);
56 }
57 
ext2fs_inode_alloc_stats(ext2_filsys fs,ext2_ino_t ino,int inuse)58 void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse)
59 {
60 	ext2fs_inode_alloc_stats2(fs, ino, inuse, 0);
61 }
62 
ext2fs_block_alloc_stats(ext2_filsys fs,blk_t blk,int inuse)63 void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
64 {
65 	int	group = ext2fs_group_of_blk(fs, blk);
66 
67 #ifndef OMIT_COM_ERR
68 	if (blk >= fs->super->s_blocks_count) {
69 		com_err("ext2fs_block_alloc_stats", 0,
70 			"Illegal block number: %lu", (unsigned long) blk);
71 		return;
72 	}
73 #endif
74 	if (inuse > 0)
75 		ext2fs_mark_block_bitmap(fs->block_map, blk);
76 	else
77 		ext2fs_unmark_block_bitmap(fs->block_map, blk);
78 	fs->group_desc[group].bg_free_blocks_count -= inuse;
79 	fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
80 	ext2fs_group_desc_csum_set(fs, group);
81 
82 	fs->super->s_free_blocks_count -= inuse;
83 	ext2fs_mark_super_dirty(fs);
84 	ext2fs_mark_bb_dirty(fs);
85 	if (fs->block_alloc_stats)
86 		(fs->block_alloc_stats)(fs, (blk64_t) blk, inuse);
87 }
88 
ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,void (* func)(ext2_filsys fs,blk64_t blk,int inuse),void (** old)(ext2_filsys fs,blk64_t blk,int inuse))89 void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
90 					   void (*func)(ext2_filsys fs,
91 							blk64_t blk,
92 							int inuse),
93 					   void (**old)(ext2_filsys fs,
94 							blk64_t blk,
95 							int inuse))
96 {
97 	if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
98 		return;
99 	if (old)
100 		*old = fs->block_alloc_stats;
101 
102 	fs->block_alloc_stats = func;
103 }
104