"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * ext2fs.h --- ext2fs |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +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% |
| 11 | */ |
| 12 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 13 | #include "ext2fs.h" |
| 14 | #include "bitops.h" |
Rob Landley | 43ac888 | 2006-04-01 00:40:33 +0000 | [diff] [blame] | 15 | #include <string.h> |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Allocate memory |
| 19 | */ |
| 20 | errcode_t ext2fs_get_mem(unsigned long size, void *ptr) |
| 21 | { |
| 22 | void **pp = (void **)ptr; |
| 23 | |
| 24 | *pp = malloc(size); |
| 25 | if (!*pp) |
| 26 | return EXT2_ET_NO_MEMORY; |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Free memory |
| 32 | */ |
| 33 | errcode_t ext2fs_free_mem(void *ptr) |
| 34 | { |
| 35 | void **pp = (void **)ptr; |
| 36 | |
| 37 | free(*pp); |
| 38 | *pp = 0; |
| 39 | return 0; |
| 40 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 41 | |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Resize memory |
| 44 | */ |
| 45 | errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size, |
| 46 | unsigned long size, void *ptr) |
| 47 | { |
| 48 | void *p; |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 49 | |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 50 | /* Use "memcpy" for pointer assignments here to avoid problems |
| 51 | * with C99 strict type aliasing rules. */ |
| 52 | memcpy(&p, ptr, sizeof (p)); |
| 53 | p = realloc(p, size); |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 54 | if (!p) |
| 55 | return EXT2_ET_NO_MEMORY; |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 56 | memcpy(ptr, &p, sizeof (p)); |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Mark a filesystem superblock as dirty |
| 62 | */ |
| 63 | void ext2fs_mark_super_dirty(ext2_filsys fs) |
| 64 | { |
| 65 | fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Mark a filesystem as changed |
| 70 | */ |
| 71 | void ext2fs_mark_changed(ext2_filsys fs) |
| 72 | { |
| 73 | fs->flags |= EXT2_FLAG_CHANGED; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Check to see if a filesystem has changed |
| 78 | */ |
| 79 | int ext2fs_test_changed(ext2_filsys fs) |
| 80 | { |
| 81 | return (fs->flags & EXT2_FLAG_CHANGED); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Mark a filesystem as valid |
| 86 | */ |
| 87 | void ext2fs_mark_valid(ext2_filsys fs) |
| 88 | { |
| 89 | fs->flags |= EXT2_FLAG_VALID; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Mark a filesystem as NOT valid |
| 94 | */ |
| 95 | void ext2fs_unmark_valid(ext2_filsys fs) |
| 96 | { |
| 97 | fs->flags &= ~EXT2_FLAG_VALID; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Check to see if a filesystem is valid |
| 102 | */ |
| 103 | int ext2fs_test_valid(ext2_filsys fs) |
| 104 | { |
| 105 | return (fs->flags & EXT2_FLAG_VALID); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Mark the inode bitmap as dirty |
| 110 | */ |
| 111 | void ext2fs_mark_ib_dirty(ext2_filsys fs) |
| 112 | { |
| 113 | fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Mark the block bitmap as dirty |
| 118 | */ |
| 119 | void ext2fs_mark_bb_dirty(ext2_filsys fs) |
| 120 | { |
| 121 | fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Check to see if a filesystem's inode bitmap is dirty |
| 126 | */ |
| 127 | int ext2fs_test_ib_dirty(ext2_filsys fs) |
| 128 | { |
| 129 | return (fs->flags & EXT2_FLAG_IB_DIRTY); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Check to see if a filesystem's block bitmap is dirty |
| 134 | */ |
| 135 | int ext2fs_test_bb_dirty(ext2_filsys fs) |
| 136 | { |
| 137 | return (fs->flags & EXT2_FLAG_BB_DIRTY); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Return the group # of a block |
| 142 | */ |
| 143 | int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk) |
| 144 | { |
| 145 | return (blk - fs->super->s_first_data_block) / |
| 146 | fs->super->s_blocks_per_group; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Return the group # of an inode number |
| 151 | */ |
| 152 | int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino) |
| 153 | { |
| 154 | return (ino - 1) / fs->super->s_inodes_per_group; |
| 155 | } |
| 156 | |
| 157 | blk_t ext2fs_inode_data_blocks(ext2_filsys fs, |
| 158 | struct ext2_inode *inode) |
| 159 | { |
| 160 | return inode->i_blocks - |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 161 | (inode->i_file_acl ? fs->blocksize >> 9 : 0); |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | __u16 ext2fs_swab16(__u16 val) |
| 173 | { |
| 174 | return (val >> 8) | (val << 8); |
| 175 | } |
| 176 | |
| 177 | __u32 ext2fs_swab32(__u32 val) |
| 178 | { |
| 179 | return ((val>>24) | ((val>>8)&0xFF00) | |
| 180 | ((val<<8)&0xFF0000) | (val<<24)); |
| 181 | } |
| 182 | |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 183 | int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap, |
| 184 | blk_t bitno); |
| 185 | |
| 186 | int ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap, |
| 187 | blk_t bitno) |
| 188 | { |
| 189 | if ((bitno < bitmap->start) || (bitno > bitmap->end)) { |
| 190 | ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno); |
| 191 | return 0; |
| 192 | } |
| 193 | return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap); |
| 194 | } |
| 195 | |
| 196 | int ext2fs_mark_block_bitmap(ext2fs_block_bitmap bitmap, |
| 197 | blk_t block) |
| 198 | { |
| 199 | return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) |
| 200 | bitmap, |
| 201 | block); |
| 202 | } |
| 203 | |
| 204 | int ext2fs_unmark_block_bitmap(ext2fs_block_bitmap bitmap, |
| 205 | blk_t block) |
| 206 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 207 | return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 208 | block); |
| 209 | } |
| 210 | |
| 211 | int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap, |
| 212 | blk_t block) |
| 213 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 214 | return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap, |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 215 | block); |
| 216 | } |
| 217 | |
| 218 | int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, |
| 219 | ext2_ino_t inode) |
| 220 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 221 | return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap, |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 222 | inode); |
| 223 | } |
| 224 | |
| 225 | int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap, |
| 226 | ext2_ino_t inode) |
| 227 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 228 | return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap, |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 229 | inode); |
| 230 | } |
| 231 | |
| 232 | int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap, |
| 233 | ext2_ino_t inode) |
| 234 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 235 | return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap, |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 236 | inode); |
| 237 | } |
| 238 | |
| 239 | void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap, |
| 240 | blk_t block) |
| 241 | { |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 242 | ext2fs_set_bit(block - bitmap->start, bitmap->bitmap); |
| 243 | } |
| 244 | |
| 245 | void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap, |
| 246 | blk_t block) |
| 247 | { |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 248 | ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap); |
| 249 | } |
| 250 | |
| 251 | int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap, |
| 252 | blk_t block) |
| 253 | { |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 254 | return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap); |
| 255 | } |
| 256 | |
| 257 | void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap, |
| 258 | ext2_ino_t inode) |
| 259 | { |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 260 | ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap); |
| 261 | } |
| 262 | |
| 263 | void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap, |
| 264 | ext2_ino_t inode) |
| 265 | { |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 266 | ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap); |
| 267 | } |
| 268 | |
| 269 | int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap, |
| 270 | ext2_ino_t inode) |
| 271 | { |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 272 | return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap); |
| 273 | } |
| 274 | |
| 275 | blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap) |
| 276 | { |
| 277 | return bitmap->start; |
| 278 | } |
| 279 | |
| 280 | ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap) |
| 281 | { |
| 282 | return bitmap->start; |
| 283 | } |
| 284 | |
| 285 | blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap) |
| 286 | { |
| 287 | return bitmap->end; |
| 288 | } |
| 289 | |
| 290 | ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap) |
| 291 | { |
| 292 | return bitmap->end; |
| 293 | } |
| 294 | |
| 295 | int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap, |
| 296 | blk_t block, int num) |
| 297 | { |
| 298 | int i; |
| 299 | |
| 300 | if ((block < bitmap->start) || (block+num-1 > bitmap->end)) { |
| 301 | ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST, |
| 302 | block, bitmap->description); |
| 303 | return 0; |
| 304 | } |
| 305 | for (i=0; i < num; i++) { |
| 306 | if (ext2fs_fast_test_block_bitmap(bitmap, block+i)) |
| 307 | return 0; |
| 308 | } |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap, |
| 313 | blk_t block, int num) |
| 314 | { |
| 315 | int i; |
| 316 | |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 317 | for (i=0; i < num; i++) { |
| 318 | if (ext2fs_fast_test_block_bitmap(bitmap, block+i)) |
| 319 | return 0; |
| 320 | } |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap, |
| 325 | blk_t block, int num) |
| 326 | { |
| 327 | int i; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 328 | |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 329 | if ((block < bitmap->start) || (block+num-1 > bitmap->end)) { |
| 330 | ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block, |
| 331 | bitmap->description); |
| 332 | return; |
| 333 | } |
| 334 | for (i=0; i < num; i++) |
| 335 | ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap); |
| 336 | } |
| 337 | |
| 338 | void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap, |
| 339 | blk_t block, int num) |
| 340 | { |
| 341 | int i; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 342 | |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 343 | for (i=0; i < num; i++) |
| 344 | ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap); |
| 345 | } |
| 346 | |
| 347 | void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap, |
| 348 | blk_t block, int num) |
| 349 | { |
| 350 | int i; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 351 | |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 352 | if ((block < bitmap->start) || (block+num-1 > bitmap->end)) { |
| 353 | ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block, |
| 354 | bitmap->description); |
| 355 | return; |
| 356 | } |
| 357 | for (i=0; i < num; i++) |
| 358 | ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap); |
| 359 | } |
| 360 | |
| 361 | void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap, |
| 362 | blk_t block, int num) |
| 363 | { |
| 364 | int i; |
Mike Frysinger | 7ad9780 | 2005-09-25 05:18:04 +0000 | [diff] [blame] | 365 | for (i=0; i < num; i++) |
| 366 | ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap); |
| 367 | } |