Changeset 06d85e5 in mainline for uspace/lib/ext4/libext4_balloc.c
- Timestamp:
- 2012-06-18T11:09:34Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 2616a75b
- Parents:
- 9a487cc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ext4/libext4_balloc.c
r9a487cc r06d85e5 52 52 uint32_t first_block = ext4_superblock_get_first_data_block(sb); 53 53 54 / / First block == 0 or 154 /* First block == 0 or 1 */ 55 55 if (first_block == 0) { 56 56 return block_addr % blocks_per_group; … … 92 92 uint32_t first_block = ext4_superblock_get_first_data_block(sb); 93 93 94 / / First block == 0 or 194 /* First block == 0 or 1 */ 95 95 if (first_block == 0) { 96 96 return block_addr / blocks_per_group; … … 114 114 ext4_superblock_t *sb = fs->superblock; 115 115 116 / / Compute indexes116 /* Compute indexes */ 117 117 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr); 118 118 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, block_addr); 119 119 120 / / Load block group reference120 /* Load block group reference */ 121 121 ext4_block_group_ref_t *bg_ref; 122 122 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref); … … 126 126 } 127 127 128 / / Load block with bitmap128 /* Load block with bitmap */ 129 129 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap( 130 130 bg_ref->block_group, sb); … … 136 136 } 137 137 138 / / Modify bitmap138 /* Modify bitmap */ 139 139 ext4_bitmap_free_bit(bitmap_block->data, index_in_group); 140 140 bitmap_block->dirty = true; 141 141 142 142 143 / / Release block with bitmap143 /* Release block with bitmap */ 144 144 rc = block_put(bitmap_block); 145 145 if (rc != EOK) { 146 / / Error in saving bitmap146 /* Error in saving bitmap */ 147 147 ext4_filesystem_put_block_group_ref(bg_ref); 148 148 EXT4FS_DBG("error in saving bitmap \%d", rc); … … 152 152 uint32_t block_size = ext4_superblock_get_block_size(sb); 153 153 154 / / Update superblock free blocks count154 /* Update superblock free blocks count */ 155 155 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb); 156 156 sb_free_blocks++; 157 157 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 158 158 159 / / Update inode blocks count159 /* Update inode blocks count */ 160 160 uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); 161 161 ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE; … … 163 163 inode_ref->dirty = true; 164 164 165 / / Update block group free blocks count165 /* Update block group free blocks count */ 166 166 uint32_t free_blocks = ext4_block_group_get_free_blocks_count( 167 167 bg_ref->block_group, sb); … … 171 171 bg_ref->dirty = true; 172 172 173 / / Release block group reference173 /* Release block group reference */ 174 174 rc = ext4_filesystem_put_block_group_ref(bg_ref); 175 175 if (rc != EOK) { … … 196 196 ext4_superblock_t *sb = fs->superblock; 197 197 198 / / Compute indexes198 /* Compute indexes */ 199 199 uint32_t block_group_first = 200 200 ext4_balloc_get_bgid_of_block(sb, first); … … 204 204 assert(block_group_first == block_group_last); 205 205 206 / / Load block group reference206 /* Load block group reference */ 207 207 ext4_block_group_ref_t *bg_ref; 208 208 rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref); … … 216 216 217 217 218 / / Load block with bitmap218 /* Load block with bitmap */ 219 219 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap( 220 220 bg_ref->block_group, sb); … … 227 227 } 228 228 229 / / Modify bitmap229 /* Modify bitmap */ 230 230 ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count); 231 231 bitmap_block->dirty = true; 232 232 233 / / Release block with bitmap233 /* Release block with bitmap */ 234 234 rc = block_put(bitmap_block); 235 235 if (rc != EOK) { 236 / / Error in saving bitmap236 /* Error in saving bitmap */ 237 237 ext4_filesystem_put_block_group_ref(bg_ref); 238 238 EXT4FS_DBG("error in saving bitmap \%d", rc); … … 242 242 uint32_t block_size = ext4_superblock_get_block_size(sb); 243 243 244 / / Update superblock free blocks count244 /* Update superblock free blocks count */ 245 245 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb); 246 246 sb_free_blocks += count; 247 247 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 248 248 249 / / Update inode blocks count249 /* Update inode blocks count */ 250 250 uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); 251 251 ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE); … … 253 253 inode_ref->dirty = true; 254 254 255 / / Update block group free blocks count255 /* Update block group free blocks count */ 256 256 uint32_t free_blocks = ext4_block_group_get_free_blocks_count( 257 257 bg_ref->block_group, sb); … … 261 261 bg_ref->dirty = true; 262 262 263 / / Release block group reference263 /* Release block group reference */ 264 264 rc = ext4_filesystem_put_block_group_ref(bg_ref); 265 265 if (rc != EOK) { … … 292 292 inode_table_bytes = inodes_per_group * inode_table_item_size; 293 293 } else { 294 / / last block group could be smaller294 /* last block group could be smaller */ 295 295 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb); 296 296 inode_table_bytes = … … 328 328 } 329 329 330 / / If inode has some blocks, get last block address + 1330 /* If inode has some blocks, get last block address + 1 */ 331 331 if (inode_block_count > 0) { 332 332 … … 341 341 } 342 342 343 / / if goal == 0, sparse file -> continue344 } 345 346 / / Identify block group of inode343 /* if goal == 0, sparse file -> continue */ 344 } 345 346 /* Identify block group of inode */ 347 347 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb); 348 348 uint32_t block_group = (inode_ref->index - 1) / inodes_per_group; 349 349 block_size = ext4_superblock_get_block_size(sb); 350 350 351 / / Load block group reference351 /* Load block group reference */ 352 352 ext4_block_group_ref_t *bg_ref; 353 353 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref); … … 356 356 } 357 357 358 / / Compute indexes358 /* Compute indexes */ 359 359 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb); 360 360 uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block( … … 363 363 uint32_t inode_table_bytes; 364 364 365 / / Check for last block group365 /* Check for last block group */ 366 366 if (block_group < block_group_count - 1) { 367 367 inode_table_bytes = inodes_per_group * inode_table_item_size; 368 368 } else { 369 / / last block group could be smaller369 /* last block group could be smaller */ 370 370 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb); 371 371 inode_table_bytes = … … 403 403 uint32_t rel_block_idx = 0; 404 404 405 / / Find GOAL405 /* Find GOAL */ 406 406 uint32_t goal = ext4_balloc_find_goal(inode_ref); 407 407 if (goal == 0) { 408 / / no goal found => partition is full409 EXT4FS_DBG("ERROR R(goal == 0)");408 /* no goal found => partition is full */ 409 EXT4FS_DBG("ERROR (goal == 0)"); 410 410 return ENOSPC; 411 411 } … … 413 413 ext4_superblock_t *sb = inode_ref->fs->superblock; 414 414 415 / / Load block group number for goal and relative index415 /* Load block group number for goal and relative index */ 416 416 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal); 417 417 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, goal); 418 418 419 419 420 / / Load block group reference420 /* Load block group reference */ 421 421 ext4_block_group_ref_t *bg_ref; 422 422 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref); … … 426 426 } 427 427 428 / / Compute indexes428 /* Compute indexes */ 429 429 uint32_t first_in_group = 430 430 ext4_balloc_get_first_data_block_in_group(sb, … … 438 438 } 439 439 440 / / Load block with bitmap440 /* Load block with bitmap */ 441 441 bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group, 442 442 sb); … … 449 449 } 450 450 451 / / Check if goal is free451 /* Check if goal is free */ 452 452 if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) { 453 453 ext4_bitmap_set_bit(bitmap_block->data, index_in_group); … … 472 472 } 473 473 474 / / Try to find free block near to goal474 /* Try to find free block near to goal */ 475 475 for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) { 476 476 if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) { … … 492 492 } 493 493 494 / / Find free BYTE in bitmap494 /* Find free BYTE in bitmap */ 495 495 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group); 496 496 if (rc == EOK) { … … 508 508 } 509 509 510 / / Find free bit in bitmap510 /* Find free bit in bitmap */ 511 511 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group); 512 512 if (rc == EOK) { … … 524 524 } 525 525 526 / / No free block found yet526 /* No free block found yet */ 527 527 block_put(bitmap_block); 528 528 ext4_filesystem_put_block_group_ref(bg_ref); 529 529 530 / / Try other block groups530 /* Try other block groups */ 531 531 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb); 532 532 … … 541 541 } 542 542 543 / / Load block with bitmap543 /* Load block with bitmap */ 544 544 bitmap_block_addr = ext4_block_group_get_block_bitmap( 545 545 bg_ref->block_group, sb); … … 552 552 } 553 553 554 / / Compute indexes554 /* Compute indexes */ 555 555 first_in_group = ext4_balloc_get_first_data_block_in_group( 556 556 sb, bg_ref->block_group, bgid); … … 566 566 } 567 567 568 / / Try to find free byte in bitmap568 /* Try to find free byte in bitmap */ 569 569 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group); 570 570 if (rc == EOK) { … … 582 582 } 583 583 584 / / Try to find free bit in bitmap584 /* Try to find free bit in bitmap */ 585 585 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group); 586 586 if (rc == EOK) { … … 601 601 ext4_filesystem_put_block_group_ref(bg_ref); 602 602 603 / / Goto next group603 /* Goto next group */ 604 604 bgid = (bgid + 1) % block_group_count; 605 605 count--; … … 609 609 610 610 success: 611 ; / / Empty command - because of syntax611 ; /* Empty command - because of syntax */ 612 612 613 613 uint32_t block_size = ext4_superblock_get_block_size(sb); 614 614 615 / / Update superblock free blocks count615 /* Update superblock free blocks count */ 616 616 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb); 617 617 sb_free_blocks--; 618 618 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 619 619 620 / / Update inode blocks (different block size!) count620 /* Update inode blocks (different block size!) count */ 621 621 uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); 622 622 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; … … 624 624 inode_ref->dirty = true; 625 625 626 / / Update block group free blocks count626 /* Update block group free blocks count */ 627 627 uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count( 628 628 bg_ref->block_group, sb); … … 652 652 ext4_superblock_t *sb = fs->superblock; 653 653 654 / / Compute indexes654 /* Compute indexes */ 655 655 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock); 656 656 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, fblock); 657 657 658 / / Load block group reference658 /* Load block group reference */ 659 659 ext4_block_group_ref_t *bg_ref; 660 660 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref); … … 664 664 } 665 665 666 / / Load block with bitmap666 /* Load block with bitmap */ 667 667 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap( 668 668 bg_ref->block_group, sb); … … 674 674 } 675 675 676 / / Check if block is free676 /* Check if block is free */ 677 677 *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group); 678 678 679 / / Allocate block if possible679 /* Allocate block if possible */ 680 680 if (*free) { 681 681 ext4_bitmap_set_bit(bitmap_block->data, index_in_group); … … 683 683 } 684 684 685 / / Release block with bitmap685 /* Release block with bitmap */ 686 686 rc = block_put(bitmap_block); 687 687 if (rc != EOK) { 688 / / Error in saving bitmap688 /* Error in saving bitmap */ 689 689 ext4_filesystem_put_block_group_ref(bg_ref); 690 690 EXT4FS_DBG("error in saving bitmap \%d", rc); … … 692 692 } 693 693 694 / / If block is not free, return694 /* If block is not free, return */ 695 695 if (!(*free)) { 696 696 goto terminate; … … 699 699 uint32_t block_size = ext4_superblock_get_block_size(sb); 700 700 701 / / Update superblock free blocks count701 /* Update superblock free blocks count */ 702 702 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb); 703 703 sb_free_blocks--; 704 704 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 705 705 706 / / Update inode blocks count706 /* Update inode blocks count */ 707 707 uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); 708 708 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; … … 710 710 inode_ref->dirty = true; 711 711 712 / / Update block group free blocks count712 /* Update block group free blocks count */ 713 713 uint32_t free_blocks = ext4_block_group_get_free_blocks_count( 714 714 bg_ref->block_group, sb);
Note:
See TracChangeset
for help on using the changeset viewer.