Changeset 4358513 in mainline
- Timestamp:
- 2012-04-14T17:48:10Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7b16549
- Parents:
- d2f5148
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ext4/libext4_balloc.c
rd2f5148 r4358513 40 40 #include "libext4.h" 41 41 42 /** Convert block address to relative index in block group. 43 * 44 * @param sb superblock pointer 45 * @param block_addr block number to convert 46 * @return relative number of block 47 */ 42 48 static uint32_t ext4_balloc_blockaddr2_index_in_group(ext4_superblock_t *sb, 43 49 uint32_t block_addr) … … 54 60 } 55 61 62 /** Convert relative block number to absolute. 63 * 64 * @param sb superblock pointer 65 * @param index relative index of block in group 66 * @param bgid index of block group 67 * @return absolute number of block 68 */ 56 69 static uint32_t ext4_balloc_index_in_group2blockaddr(ext4_superblock_t *sb, 57 70 uint32_t index, uint32_t bgid) … … 67 80 } 68 81 82 /** Compute number of block group from block address. 83 * 84 * @param sb superblock pointer 85 * @param block_addr absolute address of block 86 * @return block group index 87 */ 69 88 static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb, 70 89 uint32_t block_addr) … … 82 101 83 102 103 /** Free block. 104 * 105 * @param inode_ref inode, where the block is allocated 106 * @param block_addr absolute block address to free 107 * @return error code 108 */ 84 109 int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr) 85 110 { … … 89 114 ext4_superblock_t *sb = fs->superblock; 90 115 116 // Compute indexes 91 117 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr); 92 118 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, block_addr); 93 119 120 // Load block group reference 94 121 ext4_block_group_ref_t *bg_ref; 95 122 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref); … … 99 126 } 100 127 128 // Load block with bitmap 101 129 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap( 102 130 bg_ref->block_group, sb); … … 108 136 } 109 137 138 // Modify bitmap 110 139 ext4_bitmap_free_bit(bitmap_block->data, index_in_group); 111 140 bitmap_block->dirty = true; 112 141 142 143 // Release block with bitmap 113 144 rc = block_put(bitmap_block); 114 145 if (rc != EOK) { … … 140 171 bg_ref->dirty = true; 141 172 173 // Release block group reference 142 174 rc = ext4_filesystem_put_block_group_ref(bg_ref); 143 175 if (rc != EOK) { … … 149 181 } 150 182 183 184 /** Free continuous set of blocks. 185 * 186 * @param inode_ref inode, where the blocks are allocated 187 * @param first first block to release 188 * @param count number of blocks to release 189 */ 151 190 int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref, 152 191 uint32_t first, uint32_t count) … … 157 196 ext4_superblock_t *sb = fs->superblock; 158 197 198 // Compute indexes 159 199 uint32_t block_group_first = 160 200 ext4_balloc_get_bgid_of_block(sb, first); … … 164 204 assert(block_group_first == block_group_last); 165 205 206 // Load block group reference 166 207 ext4_block_group_ref_t *bg_ref; 167 208 rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref); … … 174 215 ext4_balloc_blockaddr2_index_in_group(sb, first); 175 216 217 218 // Load block with bitmap 176 219 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap( 177 220 bg_ref->block_group, sb); … … 184 227 } 185 228 229 // Modify bitmap 186 230 ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count); 187 188 231 bitmap_block->dirty = true; 189 232 233 // Release block with bitmap 190 234 rc = block_put(bitmap_block); 191 235 if (rc != EOK) { … … 217 261 bg_ref->dirty = true; 218 262 263 // Release block group reference 219 264 rc = ext4_filesystem_put_block_group_ref(bg_ref); 220 265 if (rc != EOK) { … … 226 271 } 227 272 273 /** Compute first block for data in block group. 274 * 275 * @param sb pointer to superblock 276 * @param bg pointer to block group 277 * @param bgid index of block group 278 * @return absolute block index of first block 279 */ 228 280 static uint32_t ext4_balloc_get_first_data_block_in_group( 229 281 ext4_superblock_t *sb, ext4_block_group_t *bg, uint32_t bgid) … … 256 308 } 257 309 258 310 /** Compute 'goal' for allocation algorithm. 311 * 312 * @param inode_ref reference to inode, to allocate block for 313 * @return goal block number 314 */ 259 315 static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref) 260 316 { … … 272 328 } 273 329 330 // If inode has some blocks, get last block address + 1 274 331 if (inode_block_count > 0) { 275 332 … … 292 349 block_size = ext4_superblock_get_block_size(sb); 293 350 351 // Load block group reference 294 352 ext4_block_group_ref_t *bg_ref; 295 353 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref); … … 298 356 } 299 357 358 // Compute indexes 300 359 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb); 301 360 uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block( … … 304 363 uint32_t inode_table_bytes; 305 364 365 // Check for last block group 306 366 if (block_group < block_group_count - 1) { 307 367 inode_table_bytes = inodes_per_group * inode_table_item_size; … … 327 387 } 328 388 389 /** Data block allocation algorithm. 390 * 391 * @param inode_ref inode to allocate block for 392 * @param fblock allocated block address 393 * @return error code 394 */ 329 395 int ext4_balloc_alloc_block( 330 396 ext4_inode_ref_t *inode_ref, uint32_t *fblock) … … 352 418 353 419 420 // Load block group reference 354 421 ext4_block_group_ref_t *bg_ref; 355 422 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref); … … 359 426 } 360 427 428 // Compute indexes 361 429 uint32_t first_in_group = 362 430 ext4_balloc_get_first_data_block_in_group(sb, … … 370 438 } 371 439 372 // Load b itmap440 // Load block with bitmap 373 441 bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group, 374 442 sb); … … 473 541 } 474 542 475 // Load b itmap543 // Load block with bitmap 476 544 bitmap_block_addr = ext4_block_group_get_block_bitmap( 477 545 bg_ref->block_group, sb); … … 484 552 } 485 553 554 // Compute indexes 486 555 first_in_group = ext4_balloc_get_first_data_block_in_group( 487 556 sb, bg_ref->block_group, bgid); … … 497 566 } 498 567 499 568 // Try to find free byte in bitmap 500 569 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group); 501 570 if (rc == EOK) { … … 513 582 } 514 583 515 // Find free bit in bitmap584 // Try to find free bit in bitmap 516 585 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group); 517 586 if (rc == EOK) { … … 529 598 } 530 599 531 532 // Next group533 600 block_put(bitmap_block); 534 601 ext4_filesystem_put_block_group_ref(bg_ref); 602 603 // Goto next group 535 604 bgid = (bgid + 1) % block_group_count; 536 605 count--; … … 550 619 551 620 // Update inode blocks (different block size!) count 552 553 621 uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); 554 622 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; … … 569 637 } 570 638 639 /** Try to allocate concrete block. 640 * 641 * @param inode_ref inode to allocate block for 642 * @param fblock block address to allocate 643 * @param free output value - if target block is free 644 * @return error code 645 */ 571 646 int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, 572 647 uint32_t fblock, bool *free) 573 648 { 574 int rc ;649 int rc = EOK; 575 650 576 651 ext4_filesystem_t *fs = inode_ref->fs; 577 652 ext4_superblock_t *sb = fs->superblock; 578 653 654 // Compute indexes 579 655 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock); 580 656 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, fblock); 581 657 658 // Load block group reference 582 659 ext4_block_group_ref_t *bg_ref; 583 660 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref); … … 587 664 } 588 665 666 // Load block with bitmap 589 667 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap( 590 668 bg_ref->block_group, sb); … … 596 674 } 597 675 676 // Check if block is free 598 677 *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group); 599 678 679 // Allocate block if possible 600 680 if (*free) { 601 681 ext4_bitmap_set_bit(bitmap_block->data, index_in_group); … … 603 683 } 604 684 685 // Release block with bitmap 605 686 rc = block_put(bitmap_block); 606 687 if (rc != EOK) { … … 611 692 } 612 693 694 // If block is not free, return 613 695 if (!(*free)) { 614 696 goto terminate;
Note:
See TracChangeset
for help on using the changeset viewer.