Changes in uspace/lib/ext4/src/balloc.c [5a6cc679:a35b458] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ext4/src/balloc.c
r5a6cc679 ra35b458 57 57 ext4_filesystem_t *fs = inode_ref->fs; 58 58 ext4_superblock_t *sb = fs->superblock; 59 59 60 60 /* Compute indexes */ 61 61 uint32_t block_group = ext4_filesystem_blockaddr2group(sb, block_addr); 62 62 uint32_t index_in_group = 63 63 ext4_filesystem_blockaddr2_index_in_group(sb, block_addr); 64 64 65 65 /* Load block group reference */ 66 66 ext4_block_group_ref_t *bg_ref; … … 68 68 if (rc != EOK) 69 69 return rc; 70 70 71 71 /* Load block with bitmap */ 72 72 uint32_t bitmap_block_addr = … … 78 78 return rc; 79 79 } 80 80 81 81 /* Modify bitmap */ 82 82 ext4_bitmap_free_bit(bitmap_block->data, index_in_group); 83 83 bitmap_block->dirty = true; 84 84 85 85 /* Release block with bitmap */ 86 86 rc = block_put(bitmap_block); … … 90 90 return rc; 91 91 } 92 92 93 93 uint32_t block_size = ext4_superblock_get_block_size(sb); 94 94 95 95 /* Update superblock free blocks count */ 96 96 uint32_t sb_free_blocks = … … 98 98 sb_free_blocks++; 99 99 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 100 100 101 101 /* Update inode blocks count */ 102 102 uint64_t ino_blocks = … … 105 105 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); 106 106 inode_ref->dirty = true; 107 107 108 108 /* Update block group free blocks count */ 109 109 uint32_t free_blocks = … … 113 113 sb, free_blocks); 114 114 bg_ref->dirty = true; 115 115 116 116 /* Release block group reference */ 117 117 return ext4_filesystem_put_block_group_ref(bg_ref); … … 346 346 { 347 347 uint32_t allocated_block = 0; 348 348 349 349 uint32_t bitmap_block_addr; 350 350 block_t *bitmap_block; … … 352 352 uint32_t free_blocks; 353 353 uint32_t goal; 354 354 355 355 /* Find GOAL */ 356 356 errno_t rc = ext4_balloc_find_goal(inode_ref, &goal); … … 359 359 360 360 ext4_superblock_t *sb = inode_ref->fs->superblock; 361 361 362 362 /* Load block group number for goal and relative index */ 363 363 uint32_t block_group = ext4_filesystem_blockaddr2group(sb, goal); 364 364 uint32_t index_in_group = 365 365 ext4_filesystem_blockaddr2_index_in_group(sb, goal); 366 366 367 367 /* Load block group reference */ 368 368 ext4_block_group_ref_t *bg_ref; … … 378 378 goto goal_failed; 379 379 } 380 380 381 381 /* Compute indexes */ 382 382 uint32_t first_in_group = 383 383 ext4_balloc_get_first_data_block_in_group(sb, bg_ref); 384 384 385 385 uint32_t first_in_group_index = 386 386 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group); 387 387 388 388 if (index_in_group < first_in_group_index) 389 389 index_in_group = first_in_group_index; 390 390 391 391 /* Load block with bitmap */ 392 392 bitmap_block_addr = 393 393 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb); 394 394 395 395 rc = block_get(&bitmap_block, inode_ref->fs->device, 396 396 bitmap_block_addr, BLOCK_FLAGS_NONE); … … 399 399 return rc; 400 400 } 401 401 402 402 /* Check if goal is free */ 403 403 if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) { … … 409 409 return rc; 410 410 } 411 411 412 412 allocated_block = 413 413 ext4_filesystem_index_in_group2blockaddr(sb, index_in_group, 414 414 block_group); 415 415 416 416 goto success; 417 417 } 418 418 419 419 uint32_t blocks_in_group = 420 420 ext4_superblock_get_blocks_in_group(sb, block_group); 421 421 422 422 uint32_t end_idx = (index_in_group + 63) & ~63; 423 423 if (end_idx > blocks_in_group) 424 424 end_idx = blocks_in_group; 425 425 426 426 /* Try to find free block near to goal */ 427 427 for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; … … 433 433 if (rc != EOK) 434 434 return rc; 435 435 436 436 allocated_block = 437 437 ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx, 438 438 block_group); 439 439 440 440 goto success; 441 441 } 442 442 } 443 443 444 444 /* Find free BYTE in bitmap */ 445 445 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, … … 450 450 if (rc != EOK) 451 451 return rc; 452 452 453 453 allocated_block = 454 454 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx, 455 455 block_group); 456 456 457 457 goto success; 458 458 } 459 459 460 460 /* Find free bit in bitmap */ 461 461 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, … … 466 466 if (rc != EOK) 467 467 return rc; 468 468 469 469 allocated_block = 470 470 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx, 471 471 block_group); 472 472 473 473 goto success; 474 474 } 475 475 476 476 /* No free block found yet */ 477 477 rc = block_put(bitmap_block); … … 486 486 if (rc != EOK) 487 487 return rc; 488 488 489 489 /* Try other block groups */ 490 490 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb); 491 491 492 492 uint32_t bgid = (block_group + 1) % block_group_count; 493 493 uint32_t count = block_group_count; 494 494 495 495 while (count > 0) { 496 496 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, … … 509 509 bitmap_block_addr = 510 510 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb); 511 511 512 512 rc = block_get(&bitmap_block, inode_ref->fs->device, 513 513 bitmap_block_addr, 0); … … 516 516 return rc; 517 517 } 518 518 519 519 /* Compute indexes */ 520 520 first_in_group = … … 523 523 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group); 524 524 blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid); 525 525 526 526 first_in_group_index = 527 527 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group); 528 528 529 529 if (index_in_group < first_in_group_index) 530 530 index_in_group = first_in_group_index; 531 531 532 532 /* Try to find free byte in bitmap */ 533 533 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, … … 540 540 return rc; 541 541 } 542 542 543 543 allocated_block = 544 544 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx, 545 545 bgid); 546 546 547 547 goto success; 548 548 } 549 549 550 550 /* Try to find free bit in bitmap */ 551 551 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, … … 558 558 return rc; 559 559 } 560 560 561 561 allocated_block = 562 562 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx, 563 563 bgid); 564 564 565 565 goto success; 566 566 } 567 567 568 568 rc = block_put(bitmap_block); 569 569 if (rc != EOK) { … … 576 576 if (rc != EOK) 577 577 return rc; 578 578 579 579 /* Goto next group */ 580 580 bgid = (bgid + 1) % block_group_count; 581 581 count--; 582 582 } 583 583 584 584 return ENOSPC; 585 585 586 586 success: 587 587 /* Empty command - because of syntax */ 588 588 ; 589 589 590 590 uint32_t block_size = ext4_superblock_get_block_size(sb); 591 591 592 592 /* Update superblock free blocks count */ 593 593 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb); 594 594 sb_free_blocks--; 595 595 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 596 596 597 597 /* Update inode blocks (different block size!) count */ 598 598 uint64_t ino_blocks = … … 601 601 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); 602 602 inode_ref->dirty = true; 603 603 604 604 /* Update block group free blocks count */ 605 605 uint32_t bg_free_blocks = … … 609 609 bg_free_blocks); 610 610 bg_ref->dirty = true; 611 611 612 612 rc = ext4_filesystem_put_block_group_ref(bg_ref); 613 613 614 614 *fblock = allocated_block; 615 615 return rc; … … 629 629 { 630 630 errno_t rc; 631 631 632 632 ext4_filesystem_t *fs = inode_ref->fs; 633 633 ext4_superblock_t *sb = fs->superblock; 634 634 635 635 /* Compute indexes */ 636 636 uint32_t block_group = ext4_filesystem_blockaddr2group(sb, fblock); 637 637 uint32_t index_in_group = 638 638 ext4_filesystem_blockaddr2_index_in_group(sb, fblock); 639 639 640 640 /* Load block group reference */ 641 641 ext4_block_group_ref_t *bg_ref; … … 643 643 if (rc != EOK) 644 644 return rc; 645 645 646 646 /* Load block with bitmap */ 647 647 uint32_t bitmap_block_addr = … … 653 653 return rc; 654 654 } 655 655 656 656 /* Check if block is free */ 657 657 *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group); 658 658 659 659 /* Allocate block if possible */ 660 660 if (*free) { … … 662 662 bitmap_block->dirty = true; 663 663 } 664 664 665 665 /* Release block with bitmap */ 666 666 rc = block_put(bitmap_block); … … 670 670 return rc; 671 671 } 672 672 673 673 /* If block is not free, return */ 674 674 if (!(*free)) 675 675 goto terminate; 676 676 677 677 uint32_t block_size = ext4_superblock_get_block_size(sb); 678 678 679 679 /* Update superblock free blocks count */ 680 680 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb); 681 681 sb_free_blocks--; 682 682 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks); 683 683 684 684 /* Update inode blocks count */ 685 685 uint64_t ino_blocks = … … 688 688 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); 689 689 inode_ref->dirty = true; 690 690 691 691 /* Update block group free blocks count */ 692 692 uint32_t free_blocks = … … 696 696 sb, free_blocks); 697 697 bg_ref->dirty = true; 698 698 699 699 terminate: 700 700 return ext4_filesystem_put_block_group_ref(bg_ref);
Note:
See TracChangeset
for help on using the changeset viewer.