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);
112 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
114 ext2fs_inode_bitmap *ret)
116 ext2fs_inode_bitmap bitmap;
118 __u32 start, end, real_end;
120 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
122 fs->write_bitmaps = ext2fs_write_bitmaps;
125 end = fs->super->s_inodes_count;
126 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
128 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
133 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
135 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
141 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
143 ext2fs_block_bitmap *ret)
145 ext2fs_block_bitmap bitmap;
147 __u32 start, end, real_end;
149 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
151 fs->write_bitmaps = ext2fs_write_bitmaps;
153 start = fs->super->s_first_data_block;
154 end = fs->super->s_blocks_count-1;
155 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
156 * fs->group_desc_count)-1 + start;
158 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
163 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
165 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
171 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
172 ext2_ino_t end, ext2_ino_t *oend)
174 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
176 if (end > bitmap->real_end)
177 return EXT2_ET_FUDGE_INODE_BITMAP_END;
184 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
185 blk_t end, blk_t *oend)
187 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
189 if (end > bitmap->real_end)
190 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
197 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
199 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
202 memset(bitmap->bitmap, 0,
203 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
206 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
208 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
211 memset(bitmap->bitmap, 0,
212 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));