[821] | 1 | /*
|
---|
| 2 | * ext2fs.h --- ext2fs
|
---|
| 3 | *
|
---|
| 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 |
|
---|
| 12 | #include "ext2fs.h"
|
---|
| 13 | #include "bitops.h"
|
---|
| 14 | #include <string.h>
|
---|
| 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 | }
|
---|
| 40 |
|
---|
| 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;
|
---|
| 48 |
|
---|
| 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);
|
---|
| 53 | if (!p)
|
---|
| 54 | return EXT2_ET_NO_MEMORY;
|
---|
| 55 | memcpy(ptr, &p, sizeof (p));
|
---|
| 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 -
|
---|
| 160 | (inode->i_file_acl ? fs->blocksize >> 9 : 0);
|
---|
| 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 |
|
---|
| 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 | {
|
---|
| 206 | return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
|
---|
| 207 | block);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | int ext2fs_test_block_bitmap(ext2fs_block_bitmap bitmap,
|
---|
| 211 | blk_t block)
|
---|
| 212 | {
|
---|
| 213 | return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
|
---|
| 214 | block);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | int ext2fs_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
|
---|
| 218 | ext2_ino_t inode)
|
---|
| 219 | {
|
---|
| 220 | return ext2fs_mark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
|
---|
| 221 | inode);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | int ext2fs_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
|
---|
| 225 | ext2_ino_t inode)
|
---|
| 226 | {
|
---|
| 227 | return ext2fs_unmark_generic_bitmap((ext2fs_generic_bitmap) bitmap,
|
---|
| 228 | inode);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | int ext2fs_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
|
---|
| 232 | ext2_ino_t inode)
|
---|
| 233 | {
|
---|
| 234 | return ext2fs_test_generic_bitmap((ext2fs_generic_bitmap) bitmap,
|
---|
| 235 | inode);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | void ext2fs_fast_mark_block_bitmap(ext2fs_block_bitmap bitmap,
|
---|
| 239 | blk_t block)
|
---|
| 240 | {
|
---|
| 241 | ext2fs_set_bit(block - bitmap->start, bitmap->bitmap);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | void ext2fs_fast_unmark_block_bitmap(ext2fs_block_bitmap bitmap,
|
---|
| 245 | blk_t block)
|
---|
| 246 | {
|
---|
| 247 | ext2fs_clear_bit(block - bitmap->start, bitmap->bitmap);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | int ext2fs_fast_test_block_bitmap(ext2fs_block_bitmap bitmap,
|
---|
| 251 | blk_t block)
|
---|
| 252 | {
|
---|
| 253 | return ext2fs_test_bit(block - bitmap->start, bitmap->bitmap);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void ext2fs_fast_mark_inode_bitmap(ext2fs_inode_bitmap bitmap,
|
---|
| 257 | ext2_ino_t inode)
|
---|
| 258 | {
|
---|
| 259 | ext2fs_set_bit(inode - bitmap->start, bitmap->bitmap);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | void ext2fs_fast_unmark_inode_bitmap(ext2fs_inode_bitmap bitmap,
|
---|
| 263 | ext2_ino_t inode)
|
---|
| 264 | {
|
---|
| 265 | ext2fs_clear_bit(inode - bitmap->start, bitmap->bitmap);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | int ext2fs_fast_test_inode_bitmap(ext2fs_inode_bitmap bitmap,
|
---|
| 269 | ext2_ino_t inode)
|
---|
| 270 | {
|
---|
| 271 | return ext2fs_test_bit(inode - bitmap->start, bitmap->bitmap);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | blk_t ext2fs_get_block_bitmap_start(ext2fs_block_bitmap bitmap)
|
---|
| 275 | {
|
---|
| 276 | return bitmap->start;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | ext2_ino_t ext2fs_get_inode_bitmap_start(ext2fs_inode_bitmap bitmap)
|
---|
| 280 | {
|
---|
| 281 | return bitmap->start;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | blk_t ext2fs_get_block_bitmap_end(ext2fs_block_bitmap bitmap)
|
---|
| 285 | {
|
---|
| 286 | return bitmap->end;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | ext2_ino_t ext2fs_get_inode_bitmap_end(ext2fs_inode_bitmap bitmap)
|
---|
| 290 | {
|
---|
| 291 | return bitmap->end;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | int ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
|
---|
| 295 | blk_t block, int num)
|
---|
| 296 | {
|
---|
| 297 | int i;
|
---|
| 298 |
|
---|
| 299 | if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
|
---|
| 300 | ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
|
---|
| 301 | block, bitmap->description);
|
---|
| 302 | return 0;
|
---|
| 303 | }
|
---|
| 304 | for (i=0; i < num; i++) {
|
---|
| 305 | if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
|
---|
| 306 | return 0;
|
---|
| 307 | }
|
---|
| 308 | return 1;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | int ext2fs_fast_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
|
---|
| 312 | blk_t block, int num)
|
---|
| 313 | {
|
---|
| 314 | int i;
|
---|
| 315 |
|
---|
| 316 | for (i=0; i < num; i++) {
|
---|
| 317 | if (ext2fs_fast_test_block_bitmap(bitmap, block+i))
|
---|
| 318 | return 0;
|
---|
| 319 | }
|
---|
| 320 | return 1;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | void ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
|
---|
| 324 | blk_t block, int num)
|
---|
| 325 | {
|
---|
| 326 | int i;
|
---|
| 327 |
|
---|
| 328 | if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
|
---|
| 329 | ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
|
---|
| 330 | bitmap->description);
|
---|
| 331 | return;
|
---|
| 332 | }
|
---|
| 333 | for (i=0; i < num; i++)
|
---|
| 334 | ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | void ext2fs_fast_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
|
---|
| 338 | blk_t block, int num)
|
---|
| 339 | {
|
---|
| 340 | int i;
|
---|
| 341 |
|
---|
| 342 | for (i=0; i < num; i++)
|
---|
| 343 | ext2fs_set_bit(block + i - bitmap->start, bitmap->bitmap);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
|
---|
| 347 | blk_t block, int num)
|
---|
| 348 | {
|
---|
| 349 | int i;
|
---|
| 350 |
|
---|
| 351 | if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
|
---|
| 352 | ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
|
---|
| 353 | bitmap->description);
|
---|
| 354 | return;
|
---|
| 355 | }
|
---|
| 356 | for (i=0; i < num; i++)
|
---|
| 357 | ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | void ext2fs_fast_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
|
---|
| 361 | blk_t block, int num)
|
---|
| 362 | {
|
---|
| 363 | int i;
|
---|
| 364 | for (i=0; i < num; i++)
|
---|
| 365 | ext2fs_clear_bit(block + i - bitmap->start, bitmap->bitmap);
|
---|
| 366 | }
|
---|