Changeset 38542dc in mainline for uspace/lib/ext4/libext4_ialloc.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_ialloc.c

    rb08e7970 r38542dc  
    2929/** @addtogroup libext4
    3030 * @{
    31  */
    32 
     31 */
    3332/**
    34  * @file        libext4_ialloc.c
    35  * @brief       Inode (de)allocation operations.
     33 * @file  libext4_ialloc.c
     34 * @brief I-node (de)allocation operations.
    3635 */
    3736
     
    4342/** Convert i-node number to relative index in block group.
    4443 *
    45  * @param sb    superblock
    46  * @param inode i-node number to be converted
    47  * @return              index of the i-node in the block group
     44 * @param sb    Superblock
     45 * @param inode I-node number to be converted
     46 *
     47 * @return Index of the i-node in the block group
     48 *
    4849 */
    4950static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
    50                 uint32_t inode)
     51    uint32_t inode)
    5152{
    5253        uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
     
    5657/** Convert relative index of i-node to absolute i-node number.
    5758 *
    58  * @param sb    superblock
    59  * @param inode index to be converted
    60  * @return              absolute number of the i-node
     59 * @param sb    Superblock
     60 * @param inode Index to be converted
     61 *
     62 * @return Absolute number of the i-node
     63 *
    6164 */
    6265static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
    63                 uint32_t index, uint32_t bgid)
     66    uint32_t index, uint32_t bgid)
    6467{
    6568        uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
     
    6972/** Compute block group number from the i-node number.
    7073 *
    71  * @param sb            superblock
    72  * @param inode         i-node number to be found the block group for
    73  * @return                      block group number computed from i-node number
     74 * @param sb    Superblock
     75 * @param inode I-node number to be found the block group for
     76 *
     77 * @return Block group number computed from i-node number
     78 *
    7479 */
    7580static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
    76                 uint32_t inode)
     81    uint32_t inode)
    7782{
    7883        uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
    7984        return (inode - 1) / inodes_per_group;
    80 
    8185}
    8286
     
    8488/** Free i-node number and modify filesystem data structers.
    8589 *
    86  * @param fs            filesystem, where the i-node is located
    87  * @param index         index of i-node to be release
    88  * @param is_dir        flag us for information whether i-node is directory or not
     90 * @param fs     Filesystem, where the i-node is located
     91 * @param index  Index of i-node to be release
     92 * @param is_dir Flag us for information whether i-node is directory or not
     93 *
    8994 */
    9095int ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
    9196{
    92         int rc;
    93 
    9497        ext4_superblock_t *sb = fs->superblock;
    95 
     98       
    9699        /* Compute index of block group and load it */
    97100        uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
    98 
     101       
    99102        ext4_block_group_ref_t *bg_ref;
    100         rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
    101         if (rc != EOK) {
    102                 return rc;
    103         }
    104 
     103        int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     104        if (rc != EOK)
     105                return rc;
     106       
    105107        /* Load i-node bitmap */
    106108        uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
    107                         bg_ref->block_group, sb);
     109            bg_ref->block_group, sb);
    108110        block_t *bitmap_block;
    109         rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, BLOCK_FLAGS_NONE);
    110         if (rc != EOK) {
    111                 return rc;
    112         }
    113 
     111        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
     112            BLOCK_FLAGS_NONE);
     113        if (rc != EOK)
     114                return rc;
     115       
    114116        /* Free i-node in the bitmap */
    115117        uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
    116118        ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
    117119        bitmap_block->dirty = true;
    118 
     120       
    119121        /* Put back the block with bitmap */
    120122        rc = block_put(bitmap_block);
     
    124126                return rc;
    125127        }
    126 
     128       
    127129        /* If released i-node is a directory, decrement used directories count */
    128130        if (is_dir) {
    129131                uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
    130                         bg_ref->block_group, sb);
     132                    bg_ref->block_group, sb);
    131133                bg_used_dirs--;
    132                 ext4_block_group_set_used_dirs_count(
    133                                 bg_ref->block_group, sb, bg_used_dirs);
     134                ext4_block_group_set_used_dirs_count(bg_ref->block_group, sb,
     135                    bg_used_dirs);
    134136        }
    135 
     137       
    136138        /* Update block group free inodes count */
    137139        uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
    138                         bg_ref->block_group, sb);
     140            bg_ref->block_group, sb);
    139141        free_inodes++;
    140         ext4_block_group_set_free_inodes_count(bg_ref->block_group,
    141                         sb, free_inodes);
    142 
     142        ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
     143            free_inodes);
     144       
    143145        bg_ref->dirty = true;
    144 
     146       
    145147        /* Put back the modified block group */
    146148        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    147         if (rc != EOK) {
    148                 return rc;
    149         }
    150 
     149        if (rc != EOK)
     150                return rc;
     151       
    151152        /* Update superblock free inodes count */
    152         uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
     153        uint32_t sb_free_inodes =
     154            ext4_superblock_get_free_inodes_count(sb);
    153155        sb_free_inodes++;
    154156        ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
    155 
     157       
    156158        return EOK;
    157159}
     
    159161/** I-node allocation algorithm.
    160162 *
    161  * This is more simple algorithm, than Orlov allocator used in the Linux kernel
    162  *
    163  * @param fs            filesystem to allocate i-node on
    164  * @param index         output value - allocated i-node number
    165  * @param is_dir        flag if allocated i-node will be file or directory
    166  * @return                      error code
     163 * This is more simple algorithm, than Orlov allocator used
     164 * in the Linux kernel.
     165 *
     166 * @param fs     Filesystem to allocate i-node on
     167 * @param index  Output value - allocated i-node number
     168 * @param is_dir Flag if allocated i-node will be file or directory
     169 *
     170 * @return Error code
     171 *
    167172 */
    168173int ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
    169174{
    170         int rc;
    171 
    172175        ext4_superblock_t *sb = fs->superblock;
    173 
     176       
    174177        uint32_t bgid = 0;
    175178        uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
    176179        uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
    177180        uint32_t avg_free_inodes = sb_free_inodes / bg_count;
    178 
     181       
    179182        /* Try to find free i-node in all block groups */
    180183        while (bgid < bg_count) {
    181 
    182184                /* Load block group to check */
    183185                ext4_block_group_ref_t *bg_ref;
    184                 rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
    185                 if (rc != EOK) {
     186                int rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
     187                if (rc != EOK)
    186188                        return rc;
    187                 }
    188 
     189               
    189190                ext4_block_group_t *bg = bg_ref->block_group;
    190 
     191               
    191192                /* Read necessary values for algorithm */
    192193                uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
    193194                uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
    194195                uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
    195 
     196               
    196197                /* Check if this block group is good candidate for allocation */
    197198                if ((free_inodes >= avg_free_inodes) && (free_blocks > 0)) {
    198 
    199199                        /* Load block with bitmap */
    200                         uint32_t bitmap_block_addr =  ext4_block_group_get_inode_bitmap(
    201                                         bg_ref->block_group, sb);
    202 
     200                        uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
     201                            bg_ref->block_group, sb);
     202                       
    203203                        block_t *bitmap_block;
    204                         rc = block_get(&bitmap_block, fs->device,
    205                                         bitmap_block_addr, BLOCK_FLAGS_NONE);
    206                         if (rc != EOK) {
     204                        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
     205                            BLOCK_FLAGS_NONE);
     206                        if (rc != EOK)
    207207                                return rc;
    208                         }
    209 
     208                       
    210209                        /* Try to allocate i-node in the bitmap */
    211210                        uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
    212211                        uint32_t index_in_group;
    213                         rc = ext4_bitmap_find_free_bit_and_set(
    214                                         bitmap_block->data, 0, &index_in_group, inodes_in_group);
    215 
     212                        rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     213                            0, &index_in_group, inodes_in_group);
     214                       
    216215                        /* Block group has not any free i-node */
    217216                        if (rc == ENOSPC) {
     
    220219                                continue;
    221220                        }
    222 
     221                       
    223222                        /* Free i-node found, save the bitmap */
    224223                        bitmap_block->dirty = true;
    225 
     224                       
    226225                        rc = block_put(bitmap_block);
    227                         if (rc != EOK) {
     226                        if (rc != EOK)
    228227                                return rc;
    229                         }
    230 
     228                       
    231229                        /* Modify filesystem counters */
    232230                        free_inodes--;
    233231                        ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
    234 
     232                       
    235233                        /* Increment used directories counter */
    236234                        if (is_dir) {
     
    238236                                ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
    239237                        }
    240 
     238                       
    241239                        /* Decrease unused inodes count */
    242240                        if (ext4_block_group_has_flag(bg,
    243                                 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
    244 
     241                            EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
    245242                                uint32_t unused =
    246                                                 ext4_block_group_get_itable_unused(bg, sb);
    247 
     243                                    ext4_block_group_get_itable_unused(bg, sb);
     244                               
    248245                                uint32_t inodes_in_group =
    249                                                 ext4_superblock_get_inodes_in_group(sb, bgid);
    250 
     246                                    ext4_superblock_get_inodes_in_group(sb, bgid);
     247                               
    251248                                uint32_t free = inodes_in_group - unused;
    252 
     249                               
    253250                                if (index_in_group >= free) {
    254251                                        unused = inodes_in_group - (index_in_group + 1);
    255 
    256252                                        ext4_block_group_set_itable_unused(bg, sb, unused);
    257253                                }
    258254                        }
    259 
     255                       
    260256                        /* Save modified block group */
    261257                        bg_ref->dirty = true;
    262 
     258                       
    263259                        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    264                         if (rc != EOK) {
     260                        if (rc != EOK)
    265261                                return rc;
    266                         }
    267 
     262                       
    268263                        /* Update superblock */
    269264                        sb_free_inodes--;
    270265                        ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
    271 
     266                       
    272267                        /* Compute the absolute i-nodex number */
    273268                        *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
    274 
     269                       
    275270                        return EOK;
    276 
    277271                }
    278 
     272               
    279273                /* Block group not modified, put it and jump to the next block group */
    280274                ext4_filesystem_put_block_group_ref(bg_ref);
    281275                ++bgid;
    282276        }
    283 
     277       
    284278        return ENOSPC;
    285279}
     
    287281/**
    288282 * @}
    289  */ 
     283 */
Note: See TracChangeset for help on using the changeset viewer.