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
24 #include <sys/types.h>
27 #if EXT2_FLAT_INCLUDES
30 #include <linux/ext2_fs.h>
35 static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end,
36 const char *descr, char *init_map,
37 ext2fs_generic_bitmap *ret)
39 ext2fs_generic_bitmap bitmap;
43 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
48 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
50 bitmap->start = start;
52 bitmap->real_end = real_end;
53 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
55 retval = ext2fs_get_mem(strlen(descr)+1,
56 (void **) &bitmap->description);
58 ext2fs_free_mem((void **) &bitmap);
61 strcpy(bitmap->description, descr);
63 bitmap->description = 0;
65 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
66 retval = ext2fs_get_mem(size, (void **) &bitmap->bitmap);
68 ext2fs_free_mem((void **) &bitmap->description);
69 ext2fs_free_mem((void **) &bitmap);
74 memcpy(bitmap->bitmap, init_map, size);
76 memset(bitmap->bitmap, 0, size);
81 errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
85 ext2fs_generic_bitmap *ret)
87 return make_bitmap(start, end, real_end, descr, 0, ret);
90 errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
91 ext2fs_generic_bitmap *dest)
94 ext2fs_generic_bitmap new_map;
96 retval = make_bitmap(src->start, src->end, src->real_end,
97 src->description, src->bitmap, &new_map);
100 new_map->magic = src->magic;
101 new_map->fs = src->fs;
102 new_map->base_error_code = src->base_error_code;
107 void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
111 for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++)
112 ext2fs_set_bit(j, map->bitmap);
117 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
119 ext2fs_inode_bitmap *ret)
121 ext2fs_inode_bitmap bitmap;
123 __u32 start, end, real_end;
125 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
127 fs->write_bitmaps = ext2fs_write_bitmaps;
130 end = fs->super->s_inodes_count;
131 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
133 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
138 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
140 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
146 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
148 ext2fs_block_bitmap *ret)
150 ext2fs_block_bitmap bitmap;
152 __u32 start, end, real_end;
154 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
156 fs->write_bitmaps = ext2fs_write_bitmaps;
158 start = fs->super->s_first_data_block;
159 end = fs->super->s_blocks_count-1;
160 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
161 * fs->group_desc_count)-1 + start;
163 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
168 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
170 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
176 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
177 ino_t end, ino_t *oend)
179 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
181 if (end > bitmap->real_end)
182 return EXT2_ET_FUDGE_INODE_BITMAP_END;
189 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
190 blk_t end, blk_t *oend)
192 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
194 if (end > bitmap->real_end)
195 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
202 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
204 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
207 memset(bitmap->bitmap, 0,
208 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
211 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
213 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
216 memset(bitmap->bitmap, 0,
217 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));