lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * blknum.c --- Functions to handle blk64_t and high/low 64-bit block |
| 3 | * number. |
| 4 | * |
| 5 | * Copyright IBM Corporation, 2007 |
| 6 | * Author Jose R. Santos <jrs@us.ibm.com> |
| 7 | * |
| 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the GNU Public |
| 10 | * License. |
| 11 | * %End-Header% |
| 12 | */ |
| 13 | |
| 14 | #include "config.h" |
| 15 | #include "ext2fs.h" |
| 16 | |
| 17 | /* |
| 18 | * Return the group # of a block |
| 19 | */ |
| 20 | dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t blk) |
| 21 | { |
| 22 | return (blk - fs->super->s_first_data_block) / |
| 23 | fs->super->s_blocks_per_group; |
| 24 | } |
| 25 | |
| 26 | /* |
| 27 | * Return the first block (inclusive) in a group |
| 28 | */ |
| 29 | blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group) |
| 30 | { |
| 31 | return fs->super->s_first_data_block + |
| 32 | ((blk64_t)group * fs->super->s_blocks_per_group); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Return the last block (inclusive) in a group |
| 37 | */ |
| 38 | blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group) |
| 39 | { |
| 40 | return (group == fs->group_desc_count - 1 ? |
| 41 | ext2fs_blocks_count(fs->super) - 1 : |
| 42 | ext2fs_group_first_block2(fs, group) + |
| 43 | (fs->super->s_blocks_per_group - 1)); |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Return the number of blocks in a group |
| 48 | */ |
| 49 | int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group) |
| 50 | { |
| 51 | int num_blocks; |
| 52 | |
| 53 | if (group == fs->group_desc_count - 1) { |
| 54 | num_blocks = (ext2fs_blocks_count(fs->super) - |
| 55 | fs->super->s_first_data_block) % |
| 56 | fs->super->s_blocks_per_group; |
| 57 | if (!num_blocks) |
| 58 | num_blocks = fs->super->s_blocks_per_group; |
| 59 | } else |
| 60 | num_blocks = fs->super->s_blocks_per_group; |
| 61 | |
| 62 | return num_blocks; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Return the inode data block count |
| 67 | */ |
| 68 | blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs, |
| 69 | struct ext2_inode *inode) |
| 70 | { |
| 71 | return (inode->i_blocks | |
| 72 | ((fs->super->s_feature_ro_compat & |
| 73 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ? |
| 74 | (__u64) inode->osd2.linux2.l_i_blocks_hi << 32 : 0)) - |
| 75 | (inode->i_file_acl ? fs->blocksize >> 9 : 0); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Return the inode i_blocks count |
| 80 | */ |
| 81 | blk64_t ext2fs_inode_i_blocks(ext2_filsys fs, |
| 82 | struct ext2_inode *inode) |
| 83 | { |
| 84 | return (inode->i_blocks | |
| 85 | ((fs->super->s_feature_ro_compat & |
| 86 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ? |
| 87 | (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0)); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Return the fs block count |
| 92 | */ |
| 93 | blk64_t ext2fs_blocks_count(struct ext2_super_block *super) |
| 94 | { |
| 95 | return super->s_blocks_count | |
| 96 | (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 97 | (__u64) super->s_blocks_count_hi << 32 : 0); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Set the fs block count |
| 102 | */ |
| 103 | void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk) |
| 104 | { |
| 105 | super->s_blocks_count = blk; |
| 106 | if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 107 | super->s_blocks_count_hi = (__u64) blk >> 32; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Add to the current fs block count |
| 112 | */ |
| 113 | void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk) |
| 114 | { |
| 115 | blk64_t tmp; |
| 116 | tmp = ext2fs_blocks_count(super) + blk; |
| 117 | ext2fs_blocks_count_set(super, tmp); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Return the fs reserved block count |
| 122 | */ |
| 123 | blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super) |
| 124 | { |
| 125 | return super->s_r_blocks_count | |
| 126 | (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 127 | (__u64) super->s_r_blocks_count_hi << 32 : 0); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Set the fs reserved block count |
| 132 | */ |
| 133 | void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk) |
| 134 | { |
| 135 | super->s_r_blocks_count = blk; |
| 136 | if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 137 | super->s_r_blocks_count_hi = (__u64) blk >> 32; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Add to the current reserved fs block count |
| 142 | */ |
| 143 | void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk) |
| 144 | { |
| 145 | blk64_t tmp; |
| 146 | tmp = ext2fs_r_blocks_count(super) + blk; |
| 147 | ext2fs_r_blocks_count_set(super, tmp); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Return the fs free block count |
| 152 | */ |
| 153 | blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super) |
| 154 | { |
| 155 | return super->s_free_blocks_count | |
| 156 | (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 157 | (__u64) super->s_free_blocks_hi << 32 : 0); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Set the fs free block count |
| 162 | */ |
| 163 | void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk) |
| 164 | { |
| 165 | super->s_free_blocks_count = blk; |
| 166 | if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 167 | super->s_free_blocks_hi = (__u64) blk >> 32; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Add to the current free fs block count |
| 172 | */ |
| 173 | void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk) |
| 174 | { |
| 175 | blk64_t tmp; |
| 176 | tmp = ext2fs_free_blocks_count(super) + blk; |
| 177 | ext2fs_free_blocks_count_set(super, tmp); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Get a pointer to a block group descriptor. We need the explicit |
| 182 | * pointer to the group desc for code that swaps block group |
| 183 | * descriptors before writing them out, as it wants to make a copy and |
| 184 | * do the swap there. |
| 185 | */ |
| 186 | struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs, |
| 187 | struct opaque_ext2_group_desc *gdp, |
| 188 | dgrp_t group) |
| 189 | { |
| 190 | return (struct ext2_group_desc *)((char *)gdp + |
| 191 | group * EXT2_DESC_SIZE(fs->super)); |
| 192 | } |
| 193 | |
| 194 | /* Do the same but as an ext4 group desc for internal use here */ |
| 195 | static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs, |
| 196 | struct opaque_ext2_group_desc *gdp, |
| 197 | dgrp_t group) |
| 198 | { |
| 199 | return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Return the block bitmap block of a group |
| 204 | */ |
| 205 | blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group) |
| 206 | { |
| 207 | struct ext4_group_desc *gdp; |
| 208 | |
| 209 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 210 | return gdp->bg_block_bitmap | |
| 211 | (fs->super->s_feature_incompat |
| 212 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 213 | (__u64)gdp->bg_block_bitmap_hi << 32 : 0); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Set the block bitmap block of a group |
| 218 | */ |
| 219 | void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk) |
| 220 | { |
| 221 | struct ext4_group_desc *gdp; |
| 222 | |
| 223 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 224 | gdp->bg_block_bitmap = blk; |
| 225 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 226 | gdp->bg_block_bitmap_hi = (__u64) blk >> 32; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Return the inode bitmap block of a group |
| 231 | */ |
| 232 | blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group) |
| 233 | { |
| 234 | struct ext4_group_desc *gdp; |
| 235 | |
| 236 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 237 | return gdp->bg_inode_bitmap | |
| 238 | (fs->super->s_feature_incompat |
| 239 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 240 | (__u64) gdp->bg_inode_bitmap_hi << 32 : 0); |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Set the inode bitmap block of a group |
| 245 | */ |
| 246 | void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk) |
| 247 | { |
| 248 | struct ext4_group_desc *gdp; |
| 249 | |
| 250 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 251 | gdp->bg_inode_bitmap = blk; |
| 252 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 253 | gdp->bg_inode_bitmap_hi = (__u64) blk >> 32; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Return the inode table block of a group |
| 258 | */ |
| 259 | blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group) |
| 260 | { |
| 261 | struct ext4_group_desc *gdp; |
| 262 | |
| 263 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 264 | return gdp->bg_inode_table | |
| 265 | (fs->super->s_feature_incompat |
| 266 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 267 | (__u64) gdp->bg_inode_table_hi << 32 : 0); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Set the inode table block of a group |
| 272 | */ |
| 273 | void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk) |
| 274 | { |
| 275 | struct ext4_group_desc *gdp; |
| 276 | |
| 277 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 278 | gdp->bg_inode_table = blk; |
| 279 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 280 | gdp->bg_inode_table_hi = (__u64) blk >> 32; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Return the free blocks count of a group |
| 285 | */ |
| 286 | __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group) |
| 287 | { |
| 288 | struct ext4_group_desc *gdp; |
| 289 | |
| 290 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 291 | return gdp->bg_free_blocks_count | |
| 292 | (fs->super->s_feature_incompat |
| 293 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 294 | (__u32) gdp->bg_free_blocks_count_hi << 16 : 0); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Set the free blocks count of a group |
| 299 | */ |
| 300 | void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n) |
| 301 | { |
| 302 | struct ext4_group_desc *gdp; |
| 303 | |
| 304 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 305 | gdp->bg_free_blocks_count = n; |
| 306 | |
| 307 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 308 | gdp->bg_free_blocks_count_hi = (__u32) n >> 16; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Return the free inodes count of a group |
| 313 | */ |
| 314 | __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group) |
| 315 | { |
| 316 | struct ext4_group_desc *gdp; |
| 317 | |
| 318 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 319 | return gdp->bg_free_inodes_count | |
| 320 | (fs->super->s_feature_incompat |
| 321 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 322 | (__u32) gdp->bg_free_inodes_count_hi << 16 : 0); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Set the free inodes count of a group |
| 327 | */ |
| 328 | void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n) |
| 329 | { |
| 330 | struct ext4_group_desc *gdp; |
| 331 | |
| 332 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 333 | gdp->bg_free_inodes_count = n; |
| 334 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 335 | gdp->bg_free_inodes_count_hi = (__u32) n >> 16; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Return the used dirs count of a group |
| 340 | */ |
| 341 | __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group) |
| 342 | { |
| 343 | struct ext4_group_desc *gdp; |
| 344 | |
| 345 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 346 | return gdp->bg_used_dirs_count | |
| 347 | (fs->super->s_feature_incompat |
| 348 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 349 | (__u32) gdp->bg_used_dirs_count_hi << 16 : 0); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Set the used dirs count of a group |
| 354 | */ |
| 355 | void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n) |
| 356 | { |
| 357 | struct ext4_group_desc *gdp; |
| 358 | |
| 359 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 360 | gdp->bg_used_dirs_count = n; |
| 361 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 362 | gdp->bg_used_dirs_count_hi = (__u32) n >> 16; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Return the unused inodes count of a group |
| 367 | */ |
| 368 | __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group) |
| 369 | { |
| 370 | struct ext4_group_desc *gdp; |
| 371 | |
| 372 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 373 | return gdp->bg_itable_unused | |
| 374 | (fs->super->s_feature_incompat |
| 375 | & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 376 | (__u32) gdp->bg_itable_unused_hi << 16 : 0); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Set the unused inodes count of a group |
| 381 | */ |
| 382 | void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n) |
| 383 | { |
| 384 | struct ext4_group_desc *gdp; |
| 385 | |
| 386 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 387 | gdp->bg_itable_unused = n; |
| 388 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 389 | gdp->bg_itable_unused_hi = (__u32) n >> 16; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Get the flags for this block group |
| 394 | */ |
| 395 | __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group) |
| 396 | { |
| 397 | struct ext4_group_desc *gdp; |
| 398 | |
| 399 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 400 | return gdp->bg_flags; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Zero out the flags for this block group |
| 405 | */ |
| 406 | void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group) |
| 407 | { |
| 408 | struct ext4_group_desc *gdp; |
| 409 | |
| 410 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 411 | gdp->bg_flags = 0; |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Get the value of a particular flag for this block group |
| 417 | */ |
| 418 | int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag) |
| 419 | { |
| 420 | struct ext4_group_desc *gdp; |
| 421 | |
| 422 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 423 | return gdp->bg_flags & bg_flag; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Set a flag or set of flags for this block group |
| 428 | */ |
| 429 | void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags) |
| 430 | { |
| 431 | struct ext4_group_desc *gdp; |
| 432 | |
| 433 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 434 | gdp->bg_flags |= bg_flags; |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Clear a flag or set of flags for this block group |
| 440 | */ |
| 441 | void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags) |
| 442 | { |
| 443 | struct ext4_group_desc *gdp; |
| 444 | |
| 445 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 446 | gdp->bg_flags &= ~bg_flags; |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Get the checksum for this block group |
| 452 | */ |
| 453 | __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group) |
| 454 | { |
| 455 | struct ext4_group_desc *gdp; |
| 456 | |
| 457 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 458 | return gdp->bg_checksum; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Set the checksum for this block group to a previously calculated value |
| 463 | */ |
| 464 | void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum) |
| 465 | { |
| 466 | struct ext4_group_desc *gdp; |
| 467 | |
| 468 | gdp = ext4fs_group_desc(fs, fs->group_desc, group); |
| 469 | gdp->bg_checksum = checksum; |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Get the acl block of a file |
| 475 | */ |
| 476 | blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode) |
| 477 | { |
| 478 | blk64_t blk = inode->i_file_acl; |
| 479 | |
| 480 | if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 481 | blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32; |
| 482 | return blk; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Set the acl block of a file |
| 487 | */ |
| 488 | void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode, |
| 489 | blk64_t blk) |
| 490 | { |
| 491 | inode->i_file_acl = blk; |
| 492 | if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 493 | inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32; |
| 494 | } |
| 495 | |