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