1 /* vi: set sw=4 ts=4: */
3 * bitmaps.c --- routines to read, write, and manipulate the inode and
6 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
9 * This file may be redistributed under the terms of the GNU Public
25 #include <sys/types.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;
39 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
44 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
46 bitmap->start = start;
48 bitmap->real_end = real_end;
49 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
51 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
53 ext2fs_free_mem(&bitmap);
56 strcpy(bitmap->description, descr);
58 bitmap->description = 0;
60 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
61 retval = ext2fs_get_mem(size, &bitmap->bitmap);
63 ext2fs_free_mem(&bitmap->description);
64 ext2fs_free_mem(&bitmap);
69 memcpy(bitmap->bitmap, init_map, size);
71 memset(bitmap->bitmap, 0, size);
76 errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
80 ext2fs_generic_bitmap *ret)
82 return make_bitmap(start, end, real_end, descr, 0, ret);
85 errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
86 ext2fs_generic_bitmap *dest)
89 ext2fs_generic_bitmap new_map;
91 retval = make_bitmap(src->start, src->end, src->real_end,
92 src->description, src->bitmap, &new_map);
95 new_map->magic = src->magic;
96 new_map->fs = src->fs;
97 new_map->base_error_code = src->base_error_code;
102 void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
106 for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++)
107 ext2fs_set_bit(j, map->bitmap);
110 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
112 ext2fs_inode_bitmap *ret)
114 ext2fs_inode_bitmap bitmap;
116 __u32 start, end, real_end;
118 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
120 fs->write_bitmaps = ext2fs_write_bitmaps;
123 end = fs->super->s_inodes_count;
124 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
126 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
131 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
133 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
139 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
141 ext2fs_block_bitmap *ret)
143 ext2fs_block_bitmap bitmap;
145 __u32 start, end, real_end;
147 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
149 fs->write_bitmaps = ext2fs_write_bitmaps;
151 start = fs->super->s_first_data_block;
152 end = fs->super->s_blocks_count-1;
153 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
154 * fs->group_desc_count)-1 + start;
156 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
161 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
163 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
169 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
170 ext2_ino_t end, ext2_ino_t *oend)
172 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
174 if (end > bitmap->real_end)
175 return EXT2_ET_FUDGE_INODE_BITMAP_END;
182 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
183 blk_t end, blk_t *oend)
185 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
187 if (end > bitmap->real_end)
188 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
195 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
197 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
200 memset(bitmap->bitmap, 0,
201 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
204 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
206 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
209 memset(bitmap->bitmap, 0,
210 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));