Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/src/balloc.c

    r5a6cc679 ra35b458  
    5757        ext4_filesystem_t *fs = inode_ref->fs;
    5858        ext4_superblock_t *sb = fs->superblock;
    59        
     59
    6060        /* Compute indexes */
    6161        uint32_t block_group = ext4_filesystem_blockaddr2group(sb, block_addr);
    6262        uint32_t index_in_group =
    6363            ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
    64        
     64
    6565        /* Load block group reference */
    6666        ext4_block_group_ref_t *bg_ref;
     
    6868        if (rc != EOK)
    6969                return rc;
    70        
     70
    7171        /* Load block with bitmap */
    7272        uint32_t bitmap_block_addr =
     
    7878                return rc;
    7979        }
    80        
     80
    8181        /* Modify bitmap */
    8282        ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
    8383        bitmap_block->dirty = true;
    84        
     84
    8585        /* Release block with bitmap */
    8686        rc = block_put(bitmap_block);
     
    9090                return rc;
    9191        }
    92        
     92
    9393        uint32_t block_size = ext4_superblock_get_block_size(sb);
    94        
     94
    9595        /* Update superblock free blocks count */
    9696        uint32_t sb_free_blocks =
     
    9898        sb_free_blocks++;
    9999        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    100        
     100
    101101        /* Update inode blocks count */
    102102        uint64_t ino_blocks =
     
    105105        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    106106        inode_ref->dirty = true;
    107        
     107
    108108        /* Update block group free blocks count */
    109109        uint32_t free_blocks =
     
    113113            sb, free_blocks);
    114114        bg_ref->dirty = true;
    115        
     115
    116116        /* Release block group reference */
    117117        return ext4_filesystem_put_block_group_ref(bg_ref);
     
    346346{
    347347        uint32_t allocated_block = 0;
    348        
     348
    349349        uint32_t bitmap_block_addr;
    350350        block_t *bitmap_block;
     
    352352        uint32_t free_blocks;
    353353        uint32_t goal;
    354        
     354
    355355        /* Find GOAL */
    356356        errno_t rc = ext4_balloc_find_goal(inode_ref, &goal);
     
    359359
    360360        ext4_superblock_t *sb = inode_ref->fs->superblock;
    361        
     361
    362362        /* Load block group number for goal and relative index */
    363363        uint32_t block_group = ext4_filesystem_blockaddr2group(sb, goal);
    364364        uint32_t index_in_group =
    365365            ext4_filesystem_blockaddr2_index_in_group(sb, goal);
    366        
     366
    367367        /* Load block group reference */
    368368        ext4_block_group_ref_t *bg_ref;
     
    378378                goto goal_failed;
    379379        }
    380        
     380
    381381        /* Compute indexes */
    382382        uint32_t first_in_group =
    383383            ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
    384        
     384
    385385        uint32_t first_in_group_index =
    386386            ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    387        
     387
    388388        if (index_in_group < first_in_group_index)
    389389                index_in_group = first_in_group_index;
    390        
     390
    391391        /* Load block with bitmap */
    392392        bitmap_block_addr =
    393393            ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
    394        
     394
    395395        rc = block_get(&bitmap_block, inode_ref->fs->device,
    396396            bitmap_block_addr, BLOCK_FLAGS_NONE);
     
    399399                return rc;
    400400        }
    401        
     401
    402402        /* Check if goal is free */
    403403        if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
     
    409409                        return rc;
    410410                }
    411                
     411
    412412                allocated_block =
    413413                    ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
    414414                    block_group);
    415                
     415
    416416                goto success;
    417417        }
    418        
     418
    419419        uint32_t blocks_in_group =
    420420            ext4_superblock_get_blocks_in_group(sb, block_group);
    421        
     421
    422422        uint32_t end_idx = (index_in_group + 63) & ~63;
    423423        if (end_idx > blocks_in_group)
    424424                end_idx = blocks_in_group;
    425        
     425
    426426        /* Try to find free block near to goal */
    427427        for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
     
    433433                        if (rc != EOK)
    434434                                return rc;
    435                        
     435
    436436                        allocated_block =
    437437                            ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
    438438                            block_group);
    439                        
     439
    440440                        goto success;
    441441                }
    442442        }
    443        
     443
    444444        /* Find free BYTE in bitmap */
    445445        rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
     
    450450                if (rc != EOK)
    451451                        return rc;
    452                
     452
    453453                allocated_block =
    454454                    ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    455455                    block_group);
    456                
     456
    457457                goto success;
    458458        }
    459        
     459
    460460        /* Find free bit in bitmap */
    461461        rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     
    466466                if (rc != EOK)
    467467                        return rc;
    468                
     468
    469469                allocated_block =
    470470                    ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    471471                    block_group);
    472                
     472
    473473                goto success;
    474474        }
    475        
     475
    476476        /* No free block found yet */
    477477        rc = block_put(bitmap_block);
     
    486486        if (rc != EOK)
    487487                return rc;
    488        
     488
    489489        /* Try other block groups */
    490490        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    491        
     491
    492492        uint32_t bgid = (block_group + 1) % block_group_count;
    493493        uint32_t count = block_group_count;
    494        
     494
    495495        while (count > 0) {
    496496                rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
     
    509509                bitmap_block_addr =
    510510                    ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
    511                
     511
    512512                rc = block_get(&bitmap_block, inode_ref->fs->device,
    513513                    bitmap_block_addr, 0);
     
    516516                        return rc;
    517517                }
    518                
     518
    519519                /* Compute indexes */
    520520                first_in_group =
     
    523523                    ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    524524                blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
    525                
     525
    526526                first_in_group_index =
    527527                    ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
    528                
     528
    529529                if (index_in_group < first_in_group_index)
    530530                        index_in_group = first_in_group_index;
    531                
     531
    532532                /* Try to find free byte in bitmap */
    533533                rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
     
    540540                                return rc;
    541541                        }
    542                        
     542
    543543                        allocated_block =
    544544                            ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    545545                            bgid);
    546                        
     546
    547547                        goto success;
    548548                }
    549                
     549
    550550                /* Try to find free bit in bitmap */
    551551                rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
     
    558558                                return rc;
    559559                        }
    560                        
     560
    561561                        allocated_block =
    562562                            ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
    563563                            bgid);
    564                        
     564
    565565                        goto success;
    566566                }
    567                
     567
    568568                rc = block_put(bitmap_block);
    569569                if (rc != EOK) {
     
    576576                if (rc != EOK)
    577577                        return rc;
    578                
     578
    579579                /* Goto next group */
    580580                bgid = (bgid + 1) % block_group_count;
    581581                count--;
    582582        }
    583        
     583
    584584        return ENOSPC;
    585        
     585
    586586success:
    587587        /* Empty command - because of syntax */
    588588        ;
    589        
     589
    590590        uint32_t block_size = ext4_superblock_get_block_size(sb);
    591        
     591
    592592        /* Update superblock free blocks count */
    593593        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    594594        sb_free_blocks--;
    595595        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    596        
     596
    597597        /* Update inode blocks (different block size!) count */
    598598        uint64_t ino_blocks =
     
    601601        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    602602        inode_ref->dirty = true;
    603        
     603
    604604        /* Update block group free blocks count */
    605605        uint32_t bg_free_blocks =
     
    609609            bg_free_blocks);
    610610        bg_ref->dirty = true;
    611        
     611
    612612        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    613        
     613
    614614        *fblock = allocated_block;
    615615        return rc;
     
    629629{
    630630        errno_t rc;
    631        
     631
    632632        ext4_filesystem_t *fs = inode_ref->fs;
    633633        ext4_superblock_t *sb = fs->superblock;
    634        
     634
    635635        /* Compute indexes */
    636636        uint32_t block_group = ext4_filesystem_blockaddr2group(sb, fblock);
    637637        uint32_t index_in_group =
    638638            ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
    639        
     639
    640640        /* Load block group reference */
    641641        ext4_block_group_ref_t *bg_ref;
     
    643643        if (rc != EOK)
    644644                return rc;
    645        
     645
    646646        /* Load block with bitmap */
    647647        uint32_t bitmap_block_addr =
     
    653653                return rc;
    654654        }
    655        
     655
    656656        /* Check if block is free */
    657657        *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
    658        
     658
    659659        /* Allocate block if possible */
    660660        if (*free) {
     
    662662                bitmap_block->dirty = true;
    663663        }
    664        
     664
    665665        /* Release block with bitmap */
    666666        rc = block_put(bitmap_block);
     
    670670                return rc;
    671671        }
    672        
     672
    673673        /* If block is not free, return */
    674674        if (!(*free))
    675675                goto terminate;
    676        
     676
    677677        uint32_t block_size = ext4_superblock_get_block_size(sb);
    678        
     678
    679679        /* Update superblock free blocks count */
    680680        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    681681        sb_free_blocks--;
    682682        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    683        
     683
    684684        /* Update inode blocks count */
    685685        uint64_t ino_blocks =
     
    688688        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    689689        inode_ref->dirty = true;
    690        
     690
    691691        /* Update block group free blocks count */
    692692        uint32_t free_blocks =
     
    696696            sb, free_blocks);
    697697        bg_ref->dirty = true;
    698        
     698
    699699terminate:
    700700        return ext4_filesystem_put_block_group_ref(bg_ref);
Note: See TracChangeset for help on using the changeset viewer.