2 * bitmaps.c --- routines to read, write, and manipulate the inode and
5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
22 #include <sys/types.h>
27 #include <linux/ext2_fs.h>
31 static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end,
32 const char *descr, char *init_map,
33 ext2fs_generic_bitmap *ret)
35 ext2fs_generic_bitmap bitmap;
38 bitmap = malloc(sizeof(struct ext2fs_struct_generic_bitmap));
42 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
44 bitmap->start = start;
46 bitmap->real_end = real_end;
47 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
49 bitmap->description = malloc(strlen(descr)+1);
50 if (!bitmap->description) {
54 strcpy(bitmap->description, descr);
56 bitmap->description = 0;
58 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
59 bitmap->bitmap = malloc(size);
60 if (!bitmap->bitmap) {
61 free(bitmap->description);
67 memcpy(bitmap->bitmap, init_map, size);
69 memset(bitmap->bitmap, 0, size);
74 errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
78 ext2fs_generic_bitmap *ret)
80 return make_bitmap(start, end, real_end, descr, 0, ret);
83 errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
84 ext2fs_generic_bitmap *dest)
87 ext2fs_generic_bitmap new;
89 retval = make_bitmap(src->start, src->end, src->real_end,
90 src->description, src->bitmap, &new);
93 new->magic = src->magic;
95 new->base_error_code = src->base_error_code;
101 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
103 ext2fs_inode_bitmap *ret)
105 ext2fs_inode_bitmap bitmap;
107 __u32 start, end, real_end;
109 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
111 fs->write_bitmaps = ext2fs_write_bitmaps;
114 end = fs->super->s_inodes_count;
115 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
117 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
122 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
124 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
130 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
132 ext2fs_block_bitmap *ret)
134 ext2fs_block_bitmap bitmap;
136 __u32 start, end, real_end;
138 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
140 fs->write_bitmaps = ext2fs_write_bitmaps;
142 start = fs->super->s_first_data_block;
143 end = fs->super->s_blocks_count-1;
144 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
145 * fs->group_desc_count)-1 + start;
147 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
152 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
154 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
160 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
161 ino_t end, ino_t *oend)
163 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
165 if (end > bitmap->real_end)
166 return EXT2_ET_FUDGE_INODE_BITMAP_END;
173 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
174 blk_t end, blk_t *oend)
176 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
178 if (end > bitmap->real_end)
179 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
186 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
188 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
191 memset(bitmap->bitmap, 0,
192 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
195 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
197 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
200 memset(bitmap->bitmap, 0,
201 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));