blob: a8514cc2efdefeb56188e4e43486dc793be15304 [file] [log] [blame]
Theodore Ts'o8bd0c952002-01-03 03:29:19 -05001/*
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
Theodore Ts'o7f961d42002-02-03 01:28:52 -050018void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
19 int inuse, int isdir)
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050020{
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;
Theodore Ts'o7f961d42002-02-03 01:28:52 -050028 if (isdir)
29 fs->group_desc[group].bg_used_dirs_count += inuse;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050030
Theodore Ts'o5711ed22008-04-21 01:29:01 -040031 /* We don't strictly need to be clearing the uninit flag if inuse < 0
Jose R. Santosd4f34d42007-10-21 21:03:25 -050032 * (i.e. freeing inodes) but it also means something is bad. */
Theodore Ts'o5711ed22008-04-21 01:29:01 -040033 fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050034 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;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050044 ext2fs_group_desc_csum_set(fs, group);
45 }
46
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050047 fs->super->s_free_inodes_count -= inuse;
48 ext2fs_mark_super_dirty(fs);
49 ext2fs_mark_ib_dirty(fs);
50}
51
Theodore Ts'o7f961d42002-02-03 01:28:52 -050052void 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
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050057void 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;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050066 fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
67 ext2fs_group_desc_csum_set(fs, group);
68
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050069 fs->super->s_free_blocks_count -= inuse;
70 ext2fs_mark_super_dirty(fs);
71 ext2fs_mark_bb_dirty(fs);
Theodore Ts'of5c562e2008-06-02 17:21:37 -040072 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;
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050090}