2 * bitmaps.c --- routines to read, write, and manipulate the inode and
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
16 #include <sys/types.h>
18 #include <linux/ext2_fs.h>
22 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
24 ext2fs_inode_bitmap *ret)
26 ext2fs_inode_bitmap bitmap;
29 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
31 fs->write_bitmaps = ext2fs_write_bitmaps;
33 bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
37 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
40 bitmap->end = fs->super->s_inodes_count;
41 bitmap->real_end = (EXT2_INODES_PER_GROUP(fs->super)
42 * fs->group_desc_count);
44 bitmap->description = malloc(strlen(descr)+1);
45 if (!bitmap->description) {
49 strcpy(bitmap->description, descr);
51 bitmap->description = 0;
53 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
54 bitmap->bitmap = malloc(size);
55 if (!bitmap->bitmap) {
56 free(bitmap->description);
61 memset(bitmap->bitmap, 0, size);
66 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
68 ext2fs_block_bitmap *ret)
70 ext2fs_block_bitmap bitmap;
73 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
75 fs->write_bitmaps = ext2fs_write_bitmaps;
77 bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
81 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
83 bitmap->start = fs->super->s_first_data_block;
84 bitmap->end = fs->super->s_blocks_count-1;
85 bitmap->real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
86 * fs->group_desc_count)-1 + bitmap->start;
88 bitmap->description = malloc(strlen(descr)+1);
89 if (!bitmap->description) {
93 strcpy(bitmap->description, descr);
95 bitmap->description = 0;
97 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
98 bitmap->bitmap = malloc(size);
99 if (!bitmap->bitmap) {
100 free(bitmap->description);
105 memset(bitmap->bitmap, 0, size);
110 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
111 ino_t end, ino_t *oend)
113 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
115 if (end > bitmap->real_end)
116 return EXT2_ET_FUDGE_INODE_BITMAP_END;
123 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
124 blk_t end, blk_t *oend)
126 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
128 if (end > bitmap->real_end)
129 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
136 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
138 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
141 memset(bitmap->bitmap, 0,
142 ((bitmap->real_end - bitmap->start) / 8) + 1);
145 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
147 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
150 memset(bitmap->bitmap, 0,
151 ((bitmap->real_end - bitmap->start) / 8) + 1);
154 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
156 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
160 if (bitmap->description) {
161 free(bitmap->description);
162 bitmap->description = 0;
164 if (bitmap->bitmap) {
165 free(bitmap->bitmap);
171 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
173 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
177 if (bitmap->description) {
178 free(bitmap->description);
179 bitmap->description = 0;
181 if (bitmap->bitmap) {
182 free(bitmap->bitmap);