blob: 5ac5205042ae1a73aeff83b56b2baf6cb8bb8652 [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%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o8bd0c952002-01-03 03:29:19 -05009 * %End-Header%
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050010 */
11
12#include <stdio.h>
13
14#include "ext2_fs.h"
15#include "ext2fs.h"
16
Theodore Ts'o7f961d42002-02-03 01:28:52 -050017void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
18 int inuse, int isdir)
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050019{
20 int group = ext2fs_group_of_ino(fs, ino);
21
Theodore Ts'ocf4e06d2009-01-22 16:01:38 -050022#ifndef OMIT_COM_ERR
23 if (ino > fs->super->s_inodes_count) {
24 com_err("ext2fs_inode_alloc_stats2", 0,
Theodore Ts'o46d6f842009-04-22 15:07:00 -040025 "Illegal inode number: %lu", (unsigned long) ino);
Theodore Ts'ocf4e06d2009-01-22 16:01:38 -050026 return;
27 }
28#endif
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050029 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;
Theodore Ts'o7f961d42002-02-03 01:28:52 -050034 if (isdir)
35 fs->group_desc[group].bg_used_dirs_count += inuse;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050036
Theodore Ts'o5711ed22008-04-21 01:29:01 -040037 /* We don't strictly need to be clearing the uninit flag if inuse < 0
Jose R. Santosd4f34d42007-10-21 21:03:25 -050038 * (i.e. freeing inodes) but it also means something is bad. */
Theodore Ts'o5711ed22008-04-21 01:29:01 -040039 fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050040 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;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050050 ext2fs_group_desc_csum_set(fs, group);
51 }
52
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050053 fs->super->s_free_inodes_count -= inuse;
54 ext2fs_mark_super_dirty(fs);
55 ext2fs_mark_ib_dirty(fs);
56}
57
Theodore Ts'o7f961d42002-02-03 01:28:52 -050058void 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
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050063void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse)
64{
65 int group = ext2fs_group_of_blk(fs, blk);
66
Theodore Ts'ocf4e06d2009-01-22 16:01:38 -050067#ifndef OMIT_COM_ERR
68 if (blk >= fs->super->s_blocks_count) {
Theodore Ts'o46d6f842009-04-22 15:07:00 -040069 com_err("ext2fs_block_alloc_stats", 0,
70 "Illegal block number: %lu", (unsigned long) blk);
Theodore Ts'ocf4e06d2009-01-22 16:01:38 -050071 return;
72 }
73#endif
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050074 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;
Jose R. Santosd4f34d42007-10-21 21:03:25 -050079 fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
80 ext2fs_group_desc_csum_set(fs, group);
81
Theodore Ts'o8bd0c952002-01-03 03:29:19 -050082 fs->super->s_free_blocks_count -= inuse;
83 ext2fs_mark_super_dirty(fs);
84 ext2fs_mark_bb_dirty(fs);
Theodore Ts'of5c562e2008-06-02 17:21:37 -040085 if (fs->block_alloc_stats)
86 (fs->block_alloc_stats)(fs, (blk64_t) blk, inuse);
87}
88
Theodore Ts'oefc6f622008-08-27 23:07:54 -040089void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
Theodore Ts'of5c562e2008-06-02 17:21:37 -040090 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;
Theodore Ts'o8bd0c952002-01-03 03:29:19 -0500103}