Changeset 38542dc in mainline for uspace/lib/ext4/libext4_balloc.c


Ignore:
Timestamp:
2012-08-12T18:36:10Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
49505fe
Parents:
b08e7970
Message:

ext4 code review and coding style cleanup

File:
1 edited

Legend:

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

    rb08e7970 r38542dc  
    2929/** @addtogroup libext4
    3030 * @{
    31  */
    32 
     31 */
    3332/**
    34  * @file        libext4_balloc.c
    35  * @brief       Physical block allocator.
     33 * @file  libext4_balloc.c
     34 * @brief Physical block allocator.
    3635 */
    3736
     
    4241/** Compute number of block group from block address.
    4342 *
    44  * @param sb                    superblock pointer
    45  * @param block_addr    absolute address of block
    46  * @return                              block group index
     43 * @param sb         Superblock pointer.
     44 * @param block_addr Absolute address of block.
     45 *
     46 * @return Block group index
     47 *
    4748 */
    4849static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb,
    49                 uint32_t block_addr)
     50    uint32_t block_addr)
    5051{
    51         uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
    52         uint32_t first_block = ext4_superblock_get_first_data_block(sb);
    53 
     52        uint32_t blocks_per_group =
     53            ext4_superblock_get_blocks_per_group(sb);
     54        uint32_t first_block =
     55            ext4_superblock_get_first_data_block(sb);
     56       
    5457        /* First block == 0 or 1 */
    55         if (first_block == 0) {
     58        if (first_block == 0)
    5659                return block_addr / blocks_per_group;
    57         } else {
     60        else
    5861                return (block_addr - 1) / blocks_per_group;
    59         }
    6062}
    6163
    6264/** Free block.
    6365 *
    64  * @param inode_ref                     inode, where the block is allocated
    65  * @param block_addr            absolute block address to free
    66  * @return                                      error code
     66 * @param inode_ref  Inode, where the block is allocated
     67 * @param block_addr Absolute block address to free
     68 *
     69 * @return Error code
     70 *
    6771 */
    6872int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
    6973{
    70         int rc;
    71 
    7274        ext4_filesystem_t *fs = inode_ref->fs;
    7375        ext4_superblock_t *sb = fs->superblock;
    74 
     76       
    7577        /* Compute indexes */
    7678        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
    7779        uint32_t index_in_group =
    78                         ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
    79 
     80            ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
     81       
    8082        /* Load block group reference */
    8183        ext4_block_group_ref_t *bg_ref;
    82         rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
    83         if (rc != EOK) {
    84                 return rc;
    85         }
    86 
     84        int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     85        if (rc != EOK)
     86                return rc;
     87       
    8788        /* Load block with bitmap */
    88         uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    89                         bg_ref->block_group, sb);
     89        uint32_t bitmap_block_addr =
     90            ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
    9091        block_t *bitmap_block;
    9192        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
    92         if (rc != EOK) {
    93                 return rc;
    94         }
    95 
     93        if (rc != EOK)
     94                return rc;
     95       
    9696        /* Modify bitmap */
    9797        ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
    9898        bitmap_block->dirty = true;
    99 
    100 
     99       
    101100        /* Release block with bitmap */
    102101        rc = block_put(bitmap_block);
     
    106105                return rc;
    107106        }
    108 
     107       
    109108        uint32_t block_size = ext4_superblock_get_block_size(sb);
    110 
     109       
    111110        /* Update superblock free blocks count */
    112         uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
     111        uint32_t sb_free_blocks =
     112            ext4_superblock_get_free_blocks_count(sb);
    113113        sb_free_blocks++;
    114114        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    115 
     115       
    116116        /* Update inode blocks count */
    117         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
     117        uint64_t ino_blocks =
     118            ext4_inode_get_blocks_count(sb, inode_ref->inode);
    118119        ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
    119120        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    120121        inode_ref->dirty = true;
    121 
     122       
    122123        /* Update block group free blocks count */
    123         uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
    124                         bg_ref->block_group, sb);
     124        uint32_t free_blocks =
     125            ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
    125126        free_blocks++;
    126127        ext4_block_group_set_free_blocks_count(bg_ref->block_group,
    127                         sb, free_blocks);
     128            sb, free_blocks);
    128129        bg_ref->dirty = true;
    129 
     130       
    130131        /* Release block group reference */
    131132        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    132         if (rc != EOK) {
    133                 return rc;
    134         }
    135 
     133        if (rc != EOK)
     134                return rc;
     135       
    136136        return EOK;
    137137}
    138138
    139 
    140139/** Free continuous set of blocks.
    141140 *
    142  * @param inode_ref                     inode, where the blocks are allocated
    143  * @param first                         first block to release
    144  * @param count                         number of blocks to release
     141 * @param inode_ref Inode, where the blocks are allocated
     142 * @param first     First block to release
     143 * @param count     Number of blocks to release
     144 *
    145145 */
    146146int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
    147                 uint32_t first, uint32_t count)
     147    uint32_t first, uint32_t count)
    148148{
    149         int rc;
    150 
    151149        ext4_filesystem_t *fs = inode_ref->fs;
    152150        ext4_superblock_t *sb = fs->superblock;
    153 
     151       
    154152        /* Compute indexes */
    155153        uint32_t block_group_first =
    156                         ext4_balloc_get_bgid_of_block(sb, first);
     154            ext4_balloc_get_bgid_of_block(sb, first);
    157155        uint32_t block_group_last =
    158                         ext4_balloc_get_bgid_of_block(sb, first + count - 1);
    159 
     156            ext4_balloc_get_bgid_of_block(sb, first + count - 1);
     157       
    160158        assert(block_group_first == block_group_last);
    161 
     159       
    162160        /* Load block group reference */
    163161        ext4_block_group_ref_t *bg_ref;
    164         rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
    165         if (rc != EOK) {
    166                 return rc;
    167         }
    168 
     162        int rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
     163        if (rc != EOK)
     164                return rc;
     165       
    169166        uint32_t index_in_group_first =
    170                         ext4_filesystem_blockaddr2_index_in_group(sb, first);
    171 
    172 
     167            ext4_filesystem_blockaddr2_index_in_group(sb, first);
     168       
    173169        /* Load block with bitmap */
    174         uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    175                         bg_ref->block_group, sb);
    176 
     170        uint32_t bitmap_block_addr =
     171            ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
     172       
    177173        block_t *bitmap_block;
    178174        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
    179         if (rc != EOK) {
    180                 return rc;
    181         }
    182 
     175        if (rc != EOK)
     176                return rc;
     177       
    183178        /* Modify bitmap */
    184179        ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
    185180        bitmap_block->dirty = true;
    186 
     181       
    187182        /* Release block with bitmap */
    188183        rc = block_put(bitmap_block);
     
    192187                return rc;
    193188        }
    194 
     189       
    195190        uint32_t block_size = ext4_superblock_get_block_size(sb);
    196 
     191       
    197192        /* Update superblock free blocks count */
    198         uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
     193        uint32_t sb_free_blocks =
     194            ext4_superblock_get_free_blocks_count(sb);
    199195        sb_free_blocks += count;
    200196        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    201 
     197       
    202198        /* Update inode blocks count */
    203         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
     199        uint64_t ino_blocks =
     200            ext4_inode_get_blocks_count(sb, inode_ref->inode);
    204201        ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
    205202        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    206203        inode_ref->dirty = true;
    207 
     204       
    208205        /* Update block group free blocks count */
    209         uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
    210                         bg_ref->block_group, sb);
     206        uint32_t free_blocks =
     207            ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
    211208        free_blocks += count;
    212209        ext4_block_group_set_free_blocks_count(bg_ref->block_group,
    213                         sb, free_blocks);
     210            sb, free_blocks);
    214211        bg_ref->dirty = true;
    215 
     212       
    216213        /* Release block group reference */
    217214        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    218         if (rc != EOK) {
    219                 return rc;
    220         }
    221 
     215        if (rc != EOK)
     216                return rc;
     217       
    222218        return EOK;
    223219}
     
    225221/** Compute first block for data in block group.
    226222 *
    227  * @param sb            pointer to superblock
    228  * @param bg            pointer to block group
    229  * @param bgid          index of block group
    230  * @return                      absolute block index of first block
    231  */
    232 uint32_t ext4_balloc_get_first_data_block_in_group(
    233                 ext4_superblock_t *sb, ext4_block_group_ref_t *bg_ref)
     223 * @param sb   Pointer to superblock
     224 * @param bg   Pointer to block group
     225 * @param bgid Index of block group
     226 *
     227 * @return Absolute block index of first block
     228 *
     229 */
     230uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
     231    ext4_block_group_ref_t *bg_ref)
    234232{
    235233        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    236         uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
    237                         bg_ref->block_group, sb);
     234        uint32_t inode_table_first_block =
     235            ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
    238236        uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
    239237        uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
    240238        uint32_t block_size = ext4_superblock_get_block_size(sb);
    241239        uint32_t inode_table_bytes;
    242 
     240       
    243241        if (bg_ref->index < block_group_count - 1) {
    244242                inode_table_bytes = inodes_per_group * inode_table_item_size;
    245243        } else {
    246                 /* last block group could be smaller */
     244                /* Last block group could be smaller */
    247245                uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
    248246                inode_table_bytes =
    249                                 (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
    250                                 * inode_table_item_size;
    251         }
    252 
     247                    (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
     248                    inode_table_item_size;
     249        }
     250       
    253251        uint32_t inode_table_blocks = inode_table_bytes / block_size;
    254 
    255         if (inode_table_bytes % block_size) {
     252       
     253        if (inode_table_bytes % block_size)
    256254                inode_table_blocks++;
    257         }
    258 
     255       
    259256        return inode_table_first_block + inode_table_blocks;
    260257}
     
    262259/** Compute 'goal' for allocation algorithm.
    263260 *
    264  * @param inode_ref             reference to inode, to allocate block for
    265  * @return                              goal block number
     261 * @param inode_ref Reference to inode, to allocate block for
     262 *
     263 * @return Goal block number
     264 *
    266265 */
    267266static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
    268267{
    269         int rc;
    270268        uint32_t goal = 0;
    271 
     269       
    272270        ext4_superblock_t *sb = inode_ref->fs->superblock;
    273 
     271       
    274272        uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
    275273        uint32_t block_size = ext4_superblock_get_block_size(sb);
    276274        uint32_t inode_block_count = inode_size / block_size;
    277 
    278         if (inode_size % block_size != 0) {
     275       
     276        if (inode_size % block_size != 0)
    279277                inode_block_count++;
    280         }
    281 
     278       
    282279        /* If inode has some blocks, get last block address + 1 */
    283280        if (inode_block_count > 0) {
    284 
    285                 rc = ext4_filesystem_get_inode_data_block_index(inode_ref, inode_block_count - 1, &goal);
    286                 if (rc != EOK) {
     281                int rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
     282                    inode_block_count - 1, &goal);
     283                if (rc != EOK)
    287284                        return 0;
    288                 }
    289 
     285               
    290286                if (goal != 0) {
    291287                        goal++;
    292288                        return goal;
    293289                }
    294 
    295                 /* if goal == 0, sparse file -> continue */
    296         }
    297 
     290               
     291                /* If goal == 0, sparse file -> continue */
     292        }
     293       
    298294        /* Identify block group of inode */
    299295        uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
    300296        uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
    301297        block_size = ext4_superblock_get_block_size(sb);
    302 
     298       
    303299        /* Load block group reference */
    304300        ext4_block_group_ref_t *bg_ref;
    305         rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
    306         if (rc != EOK) {
     301        int rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
     302            block_group, &bg_ref);
     303        if (rc != EOK)
    307304                return 0;
    308         }
    309 
     305       
    310306        /* Compute indexes */
    311307        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    312         uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
    313                         bg_ref->block_group, sb);
     308        uint32_t inode_table_first_block =
     309            ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
    314310        uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
    315311        uint32_t inode_table_bytes;
    316 
     312       
    317313        /* Check for last block group */
    318314        if (block_group < block_group_count - 1) {
    319315                inode_table_bytes = inodes_per_group * inode_table_item_size;
    320316        } else {
    321                 /* last block group could be smaller */
     317                /* Last block group could be smaller */
    322318                uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
    323319                inode_table_bytes =
    324                                 (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
    325                                 * inode_table_item_size;
    326         }
    327 
     320                    (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
     321                    inode_table_item_size;
     322        }
     323       
    328324        uint32_t inode_table_blocks = inode_table_bytes / block_size;
    329 
    330         if (inode_table_bytes % block_size) {
     325       
     326        if (inode_table_bytes % block_size)
    331327                inode_table_blocks++;
    332         }
    333 
     328       
    334329        goal = inode_table_first_block + inode_table_blocks;
    335 
     330       
    336331        ext4_filesystem_put_block_group_ref(bg_ref);
    337 
     332       
    338333        return goal;
    339334}
     
    341336/** Data block allocation algorithm.
    342337 *
    343  * @param inode_ref             inode to allocate block for
    344  * @param fblock                allocated block address
    345  * @return                              error code
    346  */
    347 int ext4_balloc_alloc_block(
    348                 ext4_inode_ref_t *inode_ref, uint32_t *fblock)
     338 * @param inode_ref Inode to allocate block for
     339 * @param fblock    Allocated block address
     340 *
     341 * @return Error code
     342 *
     343 */
     344int ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
    349345{
    350         int rc;
    351346        uint32_t allocated_block = 0;
    352 
     347       
    353348        uint32_t bitmap_block_addr;
    354349        block_t *bitmap_block;
    355350        uint32_t rel_block_idx = 0;
    356 
     351       
    357352        /* Find GOAL */
    358353        uint32_t goal = ext4_balloc_find_goal(inode_ref);
     
    361356                return ENOSPC;
    362357        }
    363 
     358       
    364359        ext4_superblock_t *sb = inode_ref->fs->superblock;
    365 
     360       
    366361        /* Load block group number for goal and relative index */
    367362        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
    368363        uint32_t index_in_group =
    369                         ext4_filesystem_blockaddr2_index_in_group(sb, goal);
    370 
    371 
     364            ext4_filesystem_blockaddr2_index_in_group(sb, goal);
     365       
    372366        /* Load block group reference */
    373367        ext4_block_group_ref_t *bg_ref;
    374         rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
    375         if (rc != EOK) {
    376                 return rc;
    377         }
    378 
     368        int rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
     369            block_group, &bg_ref);
     370        if (rc != EOK)
     371                return rc;
     372       
    379373        /* Compute indexes */
    380374        uint32_t first_in_group =
    381                         ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
    382 
    383         uint32_t first_in_group_index = ext4_filesystem_blockaddr2_index_in_group(
    384                         sb, first_in_group);
    385 
    386         if (index_in_group < first_in_group_index) {
     375            ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
     376       
     377        uint32_t first_in_group_index =
     378            ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
     379       
     380        if (index_in_group < first_in_group_index)
    387381                index_in_group = first_in_group_index;
    388         }
    389 
     382       
    390383        /* Load block with bitmap */
    391         bitmap_block_addr = ext4_block_group_get_block_bitmap(
    392                         bg_ref->block_group, sb);
    393 
     384        bitmap_block_addr =
     385            ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
     386       
    394387        rc = block_get(&bitmap_block, inode_ref->fs->device,
    395                         bitmap_block_addr, BLOCK_FLAGS_NONE);
     388            bitmap_block_addr, BLOCK_FLAGS_NONE);
    396389        if (rc != EOK) {
    397390                ext4_filesystem_put_block_group_ref(bg_ref);
    398391                return rc;
    399392        }
    400 
     393       
    401394        /* Check if goal is free */
    402395        if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
     
    408401                        return rc;
    409402                }
    410 
    411                 allocated_block = ext4_filesystem_index_in_group2blockaddr(
    412                                                         sb, index_in_group, block_group);
    413 
     403               
     404                allocated_block =
     405                    ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
     406                    block_group);
     407               
    414408                goto success;
    415 
    416         }
    417 
    418         uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(sb, block_group);
    419 
     409        }
     410       
     411        uint32_t blocks_in_group =
     412            ext4_superblock_get_blocks_in_group(sb, block_group);
     413       
    420414        uint32_t end_idx = (index_in_group + 63) & ~63;
    421         if (end_idx > blocks_in_group) {
     415        if (end_idx > blocks_in_group)
    422416                end_idx = blocks_in_group;
    423         }
    424 
     417       
    425418        /* Try to find free block near to goal */
    426         for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) {
     419        for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
     420            ++tmp_idx) {
    427421                if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
    428 
    429422                        ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
    430423                        bitmap_block->dirty = true;
    431424                        rc = block_put(bitmap_block);
    432                         if (rc != EOK) {
     425                        if (rc != EOK)
    433426                                return rc;
    434                         }
    435 
    436                         allocated_block = ext4_filesystem_index_in_group2blockaddr(
    437                                         sb, tmp_idx, block_group);
    438 
     427                       
     428                        allocated_block =
     429                            ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
     430                            block_group);
     431                       
    439432                        goto success;
    440433                }
    441 
    442         }
    443 
     434        }
     435       
    444436        /* Find free BYTE in bitmap */
    445         rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
     437        rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
     438            index_in_group, &rel_block_idx, blocks_in_group);
    446439        if (rc == EOK) {
    447440                bitmap_block->dirty = true;
    448441                rc = block_put(bitmap_block);
    449                 if (rc != EOK) {
     442                if (rc != EOK)
    450443                        return rc;
    451                 }
    452 
    453                 allocated_block = ext4_filesystem_index_in_group2blockaddr(
    454                                 sb, rel_block_idx, block_group);
    455 
     444               
     445                allocated_block =
     446                    ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
     447                    block_group);
     448               
    456449                goto success;
    457450        }
    458 
     451       
    459452        /* Find free bit in bitmap */
    460         rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
     453        rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     454            index_in_group, &rel_block_idx, blocks_in_group);
    461455        if (rc == EOK) {
    462456                bitmap_block->dirty = true;
    463457                rc = block_put(bitmap_block);
    464                 if (rc != EOK) {
     458                if (rc != EOK)
    465459                        return rc;
    466                 }
    467 
    468                 allocated_block = ext4_filesystem_index_in_group2blockaddr(
    469                                 sb, rel_block_idx, block_group);
    470 
     460               
     461                allocated_block =
     462                    ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
     463                    block_group);
     464               
    471465                goto success;
    472466        }
    473 
     467       
    474468        /* No free block found yet */
    475469        block_put(bitmap_block);
    476470        ext4_filesystem_put_block_group_ref(bg_ref);
    477 
     471       
    478472        /* Try other block groups */
    479473        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    480 
     474       
    481475        uint32_t bgid = (block_group + 1) % block_group_count;
    482476        uint32_t count = block_group_count;
    483 
     477       
    484478        while (count > 0) {
    485                 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
    486                 if (rc != EOK) {
     479                rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
     480                    &bg_ref);
     481                if (rc != EOK)
    487482                        return rc;
    488                 }
    489 
     483               
    490484                /* Load block with bitmap */
    491                 bitmap_block_addr = ext4_block_group_get_block_bitmap(
    492                                 bg_ref->block_group, sb);
    493 
    494                 rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
     485                bitmap_block_addr =
     486                    ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
     487               
     488                rc = block_get(&bitmap_block, inode_ref->fs->device,
     489                    bitmap_block_addr, 0);
    495490                if (rc != EOK) {
    496491                        ext4_filesystem_put_block_group_ref(bg_ref);
    497492                        return rc;
    498493                }
    499 
     494               
    500495                /* Compute indexes */
    501                 first_in_group = ext4_balloc_get_first_data_block_in_group(
    502                                 sb, bg_ref);
    503                 index_in_group = ext4_filesystem_blockaddr2_index_in_group(sb,
    504                                                 first_in_group);
     496                first_in_group =
     497                    ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
     498                index_in_group =
     499                    ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    505500                blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
    506 
    507                 first_in_group_index = ext4_filesystem_blockaddr2_index_in_group(
    508                         sb, first_in_group);
    509 
    510                 if (index_in_group < first_in_group_index) {
     501               
     502                first_in_group_index =
     503                    ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
     504               
     505                if (index_in_group < first_in_group_index)
    511506                        index_in_group = first_in_group_index;
    512                 }
    513 
     507               
    514508                /* Try to find free byte in bitmap */
    515509                rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
    516                                 index_in_group, &rel_block_idx, blocks_in_group);
     510                    index_in_group, &rel_block_idx, blocks_in_group);
    517511                if (rc == EOK) {
    518512                        bitmap_block->dirty = true;
    519513                        rc = block_put(bitmap_block);
    520                         if (rc != EOK) {
     514                        if (rc != EOK)
    521515                                return rc;
    522                         }
    523 
    524                         allocated_block = ext4_filesystem_index_in_group2blockaddr(
    525                                         sb, rel_block_idx, bgid);
    526 
     516                       
     517                        allocated_block =
     518                            ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
     519                            bgid);
     520                       
    527521                        goto success;
    528522                }
    529 
     523               
    530524                /* Try to find free bit in bitmap */
    531                 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
     525                rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     526                    index_in_group, &rel_block_idx, blocks_in_group);
    532527                if (rc == EOK) {
    533528                        bitmap_block->dirty = true;
    534529                        rc = block_put(bitmap_block);
    535                         if (rc != EOK) {
     530                        if (rc != EOK)
    536531                                return rc;
    537                         }
    538 
    539                         allocated_block = ext4_filesystem_index_in_group2blockaddr(
    540                                         sb, rel_block_idx, bgid);
    541 
     532                       
     533                        allocated_block =
     534                            ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
     535                            bgid);
     536                       
    542537                        goto success;
    543538                }
    544 
     539               
    545540                block_put(bitmap_block);
    546541                ext4_filesystem_put_block_group_ref(bg_ref);
    547 
     542               
    548543                /* Goto next group */
    549544                bgid = (bgid + 1) % block_group_count;
    550545                count--;
    551546        }
    552 
     547       
    553548        return ENOSPC;
    554 
     549       
    555550success:
    556         ;       /* Empty command - because of syntax */
     551        /* Empty command - because of syntax */
     552        ;
    557553       
    558554        uint32_t block_size = ext4_superblock_get_block_size(sb);
    559 
     555       
    560556        /* Update superblock free blocks count */
    561557        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    562558        sb_free_blocks--;
    563559        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    564 
     560       
    565561        /* Update inode blocks (different block size!) count */
    566         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
     562        uint64_t ino_blocks =
     563            ext4_inode_get_blocks_count(sb, inode_ref->inode);
    567564        ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
    568565        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    569566        inode_ref->dirty = true;
    570 
     567       
    571568        /* Update block group free blocks count */
    572         uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
    573                         bg_ref->block_group, sb);
     569        uint32_t bg_free_blocks =
     570            ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
    574571        bg_free_blocks--;
    575         ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb, bg_free_blocks);
     572        ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
     573            bg_free_blocks);
    576574        bg_ref->dirty = true;
    577 
     575       
    578576        ext4_filesystem_put_block_group_ref(bg_ref);
    579 
     577       
    580578        *fblock = allocated_block;
    581579        return EOK;
     
    584582/** Try to allocate concrete block.
    585583 *
    586  * @param inode_ref             inode to allocate block for
    587  * @param fblock                block address to allocate
    588  * @param free                  output value - if target block is free
    589  * @return                              error code
    590  */
    591 int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref,
    592                 uint32_t fblock, bool *free)
     584 * @param inode_ref Inode to allocate block for
     585 * @param fblock    Block address to allocate
     586 * @param free      Output value - if target block is free
     587 *
     588 * @return Error code
     589 *
     590 */
     591int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
     592    bool *free)
    593593{
    594594        int rc = EOK;
    595 
     595       
    596596        ext4_filesystem_t *fs = inode_ref->fs;
    597597        ext4_superblock_t *sb = fs->superblock;
    598 
     598       
    599599        /* Compute indexes */
    600600        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
    601601        uint32_t index_in_group =
    602                         ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
    603 
     602            ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
     603       
    604604        /* Load block group reference */
    605605        ext4_block_group_ref_t *bg_ref;
    606606        rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
    607         if (rc != EOK) {
    608                 return rc;
    609         }
    610 
     607        if (rc != EOK)
     608                return rc;
     609       
    611610        /* Load block with bitmap */
    612         uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    613                         bg_ref->block_group, sb);
     611        uint32_t bitmap_block_addr =
     612            ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
    614613        block_t *bitmap_block;
    615614        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
    616         if (rc != EOK) {
    617                 return rc;
    618         }
    619 
     615        if (rc != EOK)
     616                return rc;
     617       
    620618        /* Check if block is free */
    621619        *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
    622 
     620       
    623621        /* Allocate block if possible */
    624622        if (*free) {
     
    626624                bitmap_block->dirty = true;
    627625        }
    628 
     626       
    629627        /* Release block with bitmap */
    630628        rc = block_put(bitmap_block);
     
    634632                return rc;
    635633        }
    636 
     634       
    637635        /* If block is not free, return */
    638         if (!(*free)) {
     636        if (!(*free))
    639637                goto terminate;
    640         }
    641 
     638       
    642639        uint32_t block_size = ext4_superblock_get_block_size(sb);
    643 
     640       
    644641        /* Update superblock free blocks count */
    645642        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    646643        sb_free_blocks--;
    647644        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    648 
     645       
    649646        /* Update inode blocks count */
    650         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
     647        uint64_t ino_blocks =
     648            ext4_inode_get_blocks_count(sb, inode_ref->inode);
    651649        ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
    652650        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    653651        inode_ref->dirty = true;
    654 
     652       
    655653        /* Update block group free blocks count */
    656         uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
    657                         bg_ref->block_group, sb);
     654        uint32_t free_blocks =
     655            ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
    658656        free_blocks--;
    659657        ext4_block_group_set_free_blocks_count(bg_ref->block_group,
    660                         sb, free_blocks);
     658            sb, free_blocks);
    661659        bg_ref->dirty = true;
    662 
     660       
    663661terminate:
    664 
    665         rc = ext4_filesystem_put_block_group_ref(bg_ref);
    666         if (rc != EOK) {
    667                 return rc;
    668         }
    669 
    670         return rc;
     662        return ext4_filesystem_put_block_group_ref(bg_ref);
    671663}
    672664
    673665/**
    674666 * @}
    675  */ 
     667 */
Note: See TracChangeset for help on using the changeset viewer.