1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
/*
* blknum.c --- Functions to handle blk64_t and high/low 64-bit block
* number.
*
* Copyright IBM Corporation, 2007
* Author Jose R. Santos <jrs@us.ibm.com>
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#include "config.h"
#include "ext2fs.h"
/*
* Return the group # of a block
*/
dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t blk)
{
return (blk - fs->super->s_first_data_block) /
fs->super->s_blocks_per_group;
}
/*
* Return the first block (inclusive) in a group
*/
blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group)
{
return fs->super->s_first_data_block +
EXT2_GROUPS_TO_BLOCKS(fs->super, group);
}
/*
* Return the last block (inclusive) in a group
*/
blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group)
{
return (group == fs->group_desc_count - 1 ?
ext2fs_blocks_count(fs->super) - 1 :
ext2fs_group_first_block2(fs, group) +
(fs->super->s_blocks_per_group - 1));
}
/*
* Return the number of blocks in a group
*/
int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group)
{
int num_blocks;
if (group == fs->group_desc_count - 1) {
num_blocks = (ext2fs_blocks_count(fs->super) -
fs->super->s_first_data_block) %
fs->super->s_blocks_per_group;
if (!num_blocks)
num_blocks = fs->super->s_blocks_per_group;
} else
num_blocks = fs->super->s_blocks_per_group;
return num_blocks;
}
/*
* Return the inode data block count
*/
blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
struct ext2_inode *inode)
{
return (inode->i_blocks |
(ext2fs_has_feature_huge_file(fs->super) ?
(__u64) inode->osd2.linux2.l_i_blocks_hi << 32 : 0)) -
(inode->i_file_acl ? EXT2_CLUSTER_SIZE(fs->super) >> 9 : 0);
}
/*
* Return the inode i_blocks count
*/
blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
struct ext2_inode *inode)
{
return (inode->i_blocks |
(ext2fs_has_feature_huge_file(fs->super) ?
(__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
}
/*
* Return the inode i_blocks in stat (512 byte) units
*/
blk64_t ext2fs_get_stat_i_blocks(ext2_filsys fs,
struct ext2_inode *inode)
{
blk64_t ret = inode->i_blocks;
if (ext2fs_has_feature_huge_file(fs->super)) {
ret += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
if (inode->i_flags & EXT4_HUGE_FILE_FL)
ret *= (fs->blocksize / 512);
}
return ret;
}
/*
* Return the fs block count
*/
blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
{
return super->s_blocks_count |
(ext2fs_has_feature_64bit(super) ?
(__u64) super->s_blocks_count_hi << 32 : 0);
}
/*
* Set the fs block count
*/
void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
{
super->s_blocks_count = blk;
if (ext2fs_has_feature_64bit(super))
super->s_blocks_count_hi = (__u64) blk >> 32;
}
/*
* Add to the current fs block count
*/
void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
{
blk64_t tmp;
tmp = ext2fs_blocks_count(super) + blk;
ext2fs_blocks_count_set(super, tmp);
}
/*
* Return the fs reserved block count
*/
blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
{
return super->s_r_blocks_count |
(ext2fs_has_feature_64bit(super) ?
(__u64) super->s_r_blocks_count_hi << 32 : 0);
}
/*
* Set the fs reserved block count
*/
void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
{
super->s_r_blocks_count = blk;
if (ext2fs_has_feature_64bit(super))
super->s_r_blocks_count_hi = (__u64) blk >> 32;
}
/*
* Add to the current reserved fs block count
*/
void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
{
blk64_t tmp;
tmp = ext2fs_r_blocks_count(super) + blk;
ext2fs_r_blocks_count_set(super, tmp);
}
/*
* Return the fs free block count
*/
blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
{
return super->s_free_blocks_count |
(ext2fs_has_feature_64bit(super) ?
(__u64) super->s_free_blocks_hi << 32 : 0);
}
/*
* Set the fs free block count
*/
void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
{
super->s_free_blocks_count = blk;
if (ext2fs_has_feature_64bit(super))
super->s_free_blocks_hi = (__u64) blk >> 32;
}
/*
* Add to the current free fs block count
*/
void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
{
blk64_t tmp;
tmp = ext2fs_free_blocks_count(super) + blk;
ext2fs_free_blocks_count_set(super, tmp);
}
/*
* Get a pointer to a block group descriptor. We need the explicit
* pointer to the group desc for code that swaps block group
* descriptors before writing them out, as it wants to make a copy and
* do the swap there.
*/
struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
struct opaque_ext2_group_desc *gdp,
dgrp_t group)
{
struct ext2_group_desc *ret_gdp;
errcode_t retval;
static char *buf = 0;
static unsigned bufsize = 0;
blk64_t blk;
int desc_size = EXT2_DESC_SIZE(fs->super) & ~7;
int desc_per_blk = EXT2_DESC_PER_BLOCK(fs->super);
if (group > fs->group_desc_count)
return NULL;
if (gdp)
return (struct ext2_group_desc *)((char *)gdp +
group * desc_size);
/*
* If fs->group_desc wasn't read in when the file system was
* opened, then read it on demand here.
*/
if (bufsize < fs->blocksize)
ext2fs_free_mem(&buf);
if (!buf) {
retval = ext2fs_get_mem(fs->blocksize, &buf);
if (retval)
return NULL;
bufsize = fs->blocksize;
}
blk = ext2fs_descriptor_block_loc2(fs, fs->super->s_first_data_block,
group / desc_per_blk);
retval = io_channel_read_blk(fs->io, blk, 1, buf);
if (retval)
return NULL;
ret_gdp = (struct ext2_group_desc *)
(buf + ((group % desc_per_blk) * desc_size));
#ifdef WORDS_BIGENDIAN
ext2fs_swap_group_desc2(fs, ret_gdp);
#endif
return ret_gdp;
}
/* Do the same but as an ext4 group desc for internal use here */
static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
struct opaque_ext2_group_desc *gdp,
dgrp_t group)
{
return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
}
/*
* Return the block bitmap checksum of a group
*/
__u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
__u32 csum;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
csum = gdp->bg_block_bitmap_csum_lo;
if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
csum |= ((__u32)gdp->bg_block_bitmap_csum_hi << 16);
return csum;
}
/*
* Return the block bitmap block of a group
*/
blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_block_bitmap |
(ext2fs_has_feature_64bit(fs->super) ?
(__u64)gdp->bg_block_bitmap_hi << 32 : 0);
}
/*
* Set the block bitmap block of a group
*/
void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_block_bitmap = blk;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
}
/*
* Return the inode bitmap checksum of a group
*/
__u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
__u32 csum;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
csum = gdp->bg_inode_bitmap_csum_lo;
if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
csum |= ((__u32)gdp->bg_inode_bitmap_csum_hi << 16);
return csum;
}
/*
* Return the inode bitmap block of a group
*/
blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_inode_bitmap |
(ext2fs_has_feature_64bit(fs->super) ?
(__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
}
/*
* Set the inode bitmap block of a group
*/
void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_inode_bitmap = blk;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
}
/*
* Return the inode table block of a group
*/
blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_inode_table |
(ext2fs_has_feature_64bit(fs->super) ?
(__u64) gdp->bg_inode_table_hi << 32 : 0);
}
/*
* Set the inode table block of a group
*/
void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_inode_table = blk;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_inode_table_hi = (__u64) blk >> 32;
}
/*
* Return the free blocks count of a group
*/
__u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_free_blocks_count |
(ext2fs_has_feature_64bit(fs->super) ?
(__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
}
/*
* Set the free blocks count of a group
*/
void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_free_blocks_count = n;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
}
/*
* Return the free inodes count of a group
*/
__u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_free_inodes_count |
(ext2fs_has_feature_64bit(fs->super) ?
(__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
}
/*
* Set the free inodes count of a group
*/
void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_free_inodes_count = n;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
}
/*
* Return the used dirs count of a group
*/
__u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_used_dirs_count |
(ext2fs_has_feature_64bit(fs->super) ?
(__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
}
/*
* Set the used dirs count of a group
*/
void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_used_dirs_count = n;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
}
/*
* Return the unused inodes count of a group
*/
__u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_itable_unused |
(ext2fs_has_feature_64bit(fs->super) ?
(__u32) gdp->bg_itable_unused_hi << 16 : 0);
}
/*
* Set the unused inodes count of a group
*/
void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_itable_unused = n;
if (ext2fs_has_feature_64bit(fs->super))
gdp->bg_itable_unused_hi = (__u32) n >> 16;
}
/*
* Get the flags for this block group
*/
__u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_flags;
}
/*
* Zero out the flags for this block group
*/
void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_flags = 0;
return;
}
/*
* Get the value of a particular flag for this block group
*/
int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_flags & bg_flag;
}
/*
* Set a flag or set of flags for this block group
*/
void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_flags |= bg_flags;
return;
}
/*
* Clear a flag or set of flags for this block group
*/
void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_flags &= ~bg_flags;
return;
}
/*
* Get the checksum for this block group
*/
__u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
return gdp->bg_checksum;
}
/*
* Set the checksum for this block group to a previously calculated value
*/
void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
{
struct ext4_group_desc *gdp;
gdp = ext4fs_group_desc(fs, fs->group_desc, group);
gdp->bg_checksum = checksum;
return;
}
/*
* Get the acl block of a file
*/
blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
{
blk64_t blk = inode->i_file_acl;
if (fs && ext2fs_has_feature_64bit(fs->super))
blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
return blk;
}
/*
* Set the acl block of a file
*/
void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
blk64_t blk)
{
inode->i_file_acl = blk;
if (fs && ext2fs_has_feature_64bit(fs->super))
inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
}
/*
* Set the size of the inode
*/
errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
ext2_off64_t size)
{
if (size < 0)
return EINVAL;
/* If writing a large inode, set the large_file or large_dir flag */
if (ext2fs_needs_large_file_feature(size)) {
int dirty_sb = 0;
if (LINUX_S_ISREG(inode->i_mode)) {
if (!ext2fs_has_feature_large_file(fs->super)) {
ext2fs_set_feature_large_file(fs->super);
dirty_sb = 1;
}
} else if (LINUX_S_ISDIR(inode->i_mode)) {
if (!ext2fs_has_feature_largedir(fs->super)) {
ext2fs_set_feature_largedir(fs->super);
dirty_sb = 1;
}
} else {
/* Only regular files get to be larger than 4GB */
return EXT2_ET_FILE_TOO_BIG;
}
if (dirty_sb) {
if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV)
ext2fs_update_dynamic_rev(fs);
ext2fs_mark_super_dirty(fs);
}
}
inode->i_size = size & 0xffffffff;
inode->i_size_high = (size >> 32);
return 0;
}
|