Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * bitmaps.c --- routines to read, write, and manipulate the inode and |
| 3 | * block bitmaps. |
| 4 | * |
| 5 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
| 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the GNU Public |
| 9 | * License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #if HAVE_UNISTD_H |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | #include <fcntl.h> |
| 19 | #include <time.h> |
| 20 | #if HAVE_SYS_STAT_H |
| 21 | #include <sys/stat.h> |
| 22 | #endif |
| 23 | #if HAVE_SYS_TYPES_H |
| 24 | #include <sys/types.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "ext2_fs.h" |
| 28 | #include "ext2fs.h" |
| 29 | |
| 30 | static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end, |
| 31 | const char *descr, char *init_map, |
| 32 | ext2fs_generic_bitmap *ret) |
| 33 | { |
| 34 | ext2fs_generic_bitmap bitmap; |
| 35 | errcode_t retval; |
| 36 | size_t size; |
| 37 | |
| 38 | retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap), |
| 39 | &bitmap); |
| 40 | if (retval) |
| 41 | return retval; |
| 42 | |
| 43 | bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP; |
| 44 | bitmap->fs = NULL; |
| 45 | bitmap->start = start; |
| 46 | bitmap->end = end; |
| 47 | bitmap->real_end = real_end; |
| 48 | bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK; |
| 49 | if (descr) { |
| 50 | retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description); |
| 51 | if (retval) { |
| 52 | ext2fs_free_mem(&bitmap); |
| 53 | return retval; |
| 54 | } |
| 55 | strcpy(bitmap->description, descr); |
| 56 | } else |
| 57 | bitmap->description = 0; |
| 58 | |
| 59 | size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1); |
| 60 | retval = ext2fs_get_mem(size, &bitmap->bitmap); |
| 61 | if (retval) { |
| 62 | ext2fs_free_mem(&bitmap->description); |
| 63 | ext2fs_free_mem(&bitmap); |
| 64 | return retval; |
| 65 | } |
| 66 | |
| 67 | if (init_map) |
| 68 | memcpy(bitmap->bitmap, init_map, size); |
| 69 | else |
| 70 | memset(bitmap->bitmap, 0, size); |
| 71 | *ret = bitmap; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | errcode_t ext2fs_allocate_generic_bitmap(__u32 start, |
| 76 | __u32 end, |
| 77 | __u32 real_end, |
| 78 | const char *descr, |
| 79 | ext2fs_generic_bitmap *ret) |
| 80 | { |
| 81 | return make_bitmap(start, end, real_end, descr, 0, ret); |
| 82 | } |
| 83 | |
| 84 | errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src, |
| 85 | ext2fs_generic_bitmap *dest) |
| 86 | { |
| 87 | errcode_t retval; |
| 88 | ext2fs_generic_bitmap new_map; |
| 89 | |
| 90 | retval = make_bitmap(src->start, src->end, src->real_end, |
| 91 | src->description, src->bitmap, &new_map); |
| 92 | if (retval) |
| 93 | return retval; |
| 94 | new_map->magic = src->magic; |
| 95 | new_map->fs = src->fs; |
| 96 | new_map->base_error_code = src->base_error_code; |
| 97 | *dest = new_map; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map) |
| 102 | { |
| 103 | __u32 i, j; |
| 104 | |
| 105 | for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++) |
| 106 | ext2fs_set_bit(j, map->bitmap); |
| 107 | |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs, |
| 112 | const char *descr, |
| 113 | ext2fs_inode_bitmap *ret) |
| 114 | { |
| 115 | ext2fs_inode_bitmap bitmap; |
| 116 | errcode_t retval; |
| 117 | __u32 start, end, real_end; |
| 118 | |
| 119 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 120 | |
| 121 | fs->write_bitmaps = ext2fs_write_bitmaps; |
| 122 | |
| 123 | start = 1; |
| 124 | end = fs->super->s_inodes_count; |
| 125 | real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count); |
| 126 | |
| 127 | retval = ext2fs_allocate_generic_bitmap(start, end, real_end, |
| 128 | descr, &bitmap); |
| 129 | if (retval) |
| 130 | return retval; |
| 131 | |
| 132 | bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP; |
| 133 | bitmap->fs = fs; |
| 134 | bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK; |
| 135 | |
| 136 | *ret = bitmap; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs, |
| 141 | const char *descr, |
| 142 | ext2fs_block_bitmap *ret) |
| 143 | { |
| 144 | ext2fs_block_bitmap bitmap; |
| 145 | errcode_t retval; |
| 146 | __u32 start, end, real_end; |
| 147 | |
| 148 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 149 | |
| 150 | fs->write_bitmaps = ext2fs_write_bitmaps; |
| 151 | |
| 152 | start = fs->super->s_first_data_block; |
| 153 | end = fs->super->s_blocks_count-1; |
| 154 | real_end = (EXT2_BLOCKS_PER_GROUP(fs->super) |
| 155 | * fs->group_desc_count)-1 + start; |
| 156 | |
| 157 | retval = ext2fs_allocate_generic_bitmap(start, end, real_end, |
| 158 | descr, &bitmap); |
| 159 | if (retval) |
| 160 | return retval; |
| 161 | |
| 162 | bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP; |
| 163 | bitmap->fs = fs; |
| 164 | bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK; |
| 165 | |
| 166 | *ret = bitmap; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap, |
| 171 | ext2_ino_t end, ext2_ino_t *oend) |
| 172 | { |
| 173 | EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP); |
| 174 | |
| 175 | if (end > bitmap->real_end) |
| 176 | return EXT2_ET_FUDGE_INODE_BITMAP_END; |
| 177 | if (oend) |
| 178 | *oend = bitmap->end; |
| 179 | bitmap->end = end; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap, |
| 184 | blk_t end, blk_t *oend) |
| 185 | { |
| 186 | EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP); |
| 187 | |
| 188 | if (end > bitmap->real_end) |
| 189 | return EXT2_ET_FUDGE_BLOCK_BITMAP_END; |
| 190 | if (oend) |
| 191 | *oend = bitmap->end; |
| 192 | bitmap->end = end; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap) |
| 197 | { |
| 198 | if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP)) |
| 199 | return; |
| 200 | |
| 201 | memset(bitmap->bitmap, 0, |
| 202 | (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1)); |
| 203 | } |
| 204 | |
| 205 | void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap) |
| 206 | { |
| 207 | if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP)) |
| 208 | return; |
| 209 | |
| 210 | memset(bitmap->bitmap, 0, |
| 211 | (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1)); |
| 212 | } |