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