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