Changeset 4358513 in mainline for uspace/lib/ext4/libext4_balloc.c


Ignore:
Timestamp:
2012-04-14T17:48:10Z (12 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7b16549
Parents:
d2f5148
Message:

comments for block allocator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/libext4_balloc.c

    rd2f5148 r4358513  
    4040#include "libext4.h"
    4141
     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 */
    4248static uint32_t ext4_balloc_blockaddr2_index_in_group(ext4_superblock_t *sb,
    4349                uint32_t block_addr)
     
    5460}
    5561
     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 */
    5669static uint32_t ext4_balloc_index_in_group2blockaddr(ext4_superblock_t *sb,
    5770                uint32_t index, uint32_t bgid)
     
    6780}
    6881
     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 */
    6988static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb,
    7089                uint32_t block_addr)
     
    82101
    83102
     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 */
    84109int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
    85110{
     
    89114        ext4_superblock_t *sb = fs->superblock;
    90115
     116        // Compute indexes
    91117        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
    92118        uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, block_addr);
    93119
     120        // Load block group reference
    94121        ext4_block_group_ref_t *bg_ref;
    95122        rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     
    99126        }
    100127
     128        // Load block with bitmap
    101129        uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    102130                        bg_ref->block_group, sb);
     
    108136        }
    109137
     138        // Modify bitmap
    110139        ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
    111140        bitmap_block->dirty = true;
    112141
     142
     143        // Release block with bitmap
    113144        rc = block_put(bitmap_block);
    114145        if (rc != EOK) {
     
    140171        bg_ref->dirty = true;
    141172
     173        // Release block group reference
    142174        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    143175        if (rc != EOK) {
     
    149181}
    150182
     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 */
    151190int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
    152191                uint32_t first, uint32_t count)
     
    157196        ext4_superblock_t *sb = fs->superblock;
    158197
     198        // Compute indexes
    159199        uint32_t block_group_first =
    160200                        ext4_balloc_get_bgid_of_block(sb, first);
     
    164204        assert(block_group_first == block_group_last);
    165205
     206        // Load block group reference
    166207        ext4_block_group_ref_t *bg_ref;
    167208        rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
     
    174215                        ext4_balloc_blockaddr2_index_in_group(sb, first);
    175216
     217
     218        // Load block with bitmap
    176219        uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    177220                        bg_ref->block_group, sb);
     
    184227        }
    185228
     229        // Modify bitmap
    186230        ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
    187 
    188231        bitmap_block->dirty = true;
    189232
     233        // Release block with bitmap
    190234        rc = block_put(bitmap_block);
    191235        if (rc != EOK) {
     
    217261        bg_ref->dirty = true;
    218262
     263        // Release block group reference
    219264        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    220265        if (rc != EOK) {
     
    226271}
    227272
     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 */
    228280static uint32_t ext4_balloc_get_first_data_block_in_group(
    229281                ext4_superblock_t *sb, ext4_block_group_t *bg, uint32_t bgid)
     
    256308}
    257309
    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 */
    259315static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
    260316{
     
    272328        }
    273329
     330        // If inode has some blocks, get last block address + 1
    274331        if (inode_block_count > 0) {
    275332
     
    292349        block_size = ext4_superblock_get_block_size(sb);
    293350
     351        // Load block group reference
    294352        ext4_block_group_ref_t *bg_ref;
    295353        rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
     
    298356        }
    299357
     358        // Compute indexes
    300359        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    301360        uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
     
    304363        uint32_t inode_table_bytes;
    305364
     365        // Check for last block group
    306366        if (block_group < block_group_count - 1) {
    307367                inode_table_bytes = inodes_per_group * inode_table_item_size;
     
    327387}
    328388
     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 */
    329395int ext4_balloc_alloc_block(
    330396                ext4_inode_ref_t *inode_ref, uint32_t *fblock)
     
    352418
    353419
     420        // Load block group reference
    354421        ext4_block_group_ref_t *bg_ref;
    355422        rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
     
    359426        }
    360427
     428        // Compute indexes
    361429        uint32_t first_in_group =
    362430                        ext4_balloc_get_first_data_block_in_group(sb,
     
    370438        }
    371439
    372         // Load bitmap
     440        // Load block with bitmap
    373441        bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
    374442                        sb);
     
    473541                }
    474542
    475                 // Load bitmap
     543                // Load block with bitmap
    476544                bitmap_block_addr = ext4_block_group_get_block_bitmap(
    477545                                bg_ref->block_group, sb);
     
    484552                }
    485553
     554                // Compute indexes
    486555                first_in_group = ext4_balloc_get_first_data_block_in_group(
    487556                                sb, bg_ref->block_group, bgid);
     
    497566                }
    498567
    499 
     568                // Try to find free byte in bitmap
    500569                rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
    501570                if (rc == EOK) {
     
    513582                }
    514583
    515                 // Find free bit in bitmap
     584                // Try to find free bit in bitmap
    516585                rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
    517586                if (rc == EOK) {
     
    529598                }
    530599
    531 
    532                 // Next group
    533600                block_put(bitmap_block);
    534601                ext4_filesystem_put_block_group_ref(bg_ref);
     602
     603                // Goto next group
    535604                bgid = (bgid + 1) % block_group_count;
    536605                count--;
     
    550619
    551620        // Update inode blocks (different block size!) count
    552 
    553621        uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
    554622        ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
     
    569637}
    570638
     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 */
    571646int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref,
    572647                uint32_t fblock, bool *free)
    573648{
    574         int rc;
     649        int rc = EOK;
    575650
    576651        ext4_filesystem_t *fs = inode_ref->fs;
    577652        ext4_superblock_t *sb = fs->superblock;
    578653
     654        // Compute indexes
    579655        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
    580656        uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, fblock);
    581657
     658        // Load block group reference
    582659        ext4_block_group_ref_t *bg_ref;
    583660        rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     
    587664        }
    588665
     666        // Load block with bitmap
    589667        uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    590668                        bg_ref->block_group, sb);
     
    596674        }
    597675
     676        // Check if block is free
    598677        *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
    599678
     679        // Allocate block if possible
    600680        if (*free) {
    601681                ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
     
    603683        }
    604684
     685        // Release block with bitmap
    605686        rc = block_put(bitmap_block);
    606687        if (rc != EOK) {
     
    611692        }
    612693
     694        // If block is not free, return
    613695        if (!(*free)) {
    614696                goto terminate;
Note: See TracChangeset for help on using the changeset viewer.