Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 1ac1ab4 in mainline


Ignore:
Timestamp:
2012-03-31T20:00:15Z (10 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master
Children:
a2fa350
Parents:
38384ae
Message:

simplied headers of more functions, improved bg_ref and inode_ref structures, added block group checksumming and fixed bug in block_group values updating

Location:
uspace
Files:
2 added
16 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/Makefile

    r38384ae r1ac1ab4  
    3636        libext4_bitmap.c \
    3737        libext4_block_group.c \
     38        libext4_crc.c \
    3839        libext4_directory.c \
    3940        libext4_directory_index.c \
  • uspace/lib/ext4/libext4.h

    r38384ae r1ac1ab4  
    3737#include "libext4_bitmap.h"
    3838#include "libext4_block_group.h"
     39#include "libext4_crc.h"
    3940#include "libext4_directory.h"
    4041#include "libext4_directory_index.h"
  • uspace/lib/ext4/libext4_balloc.c

    r38384ae r1ac1ab4  
    8282
    8383
    84 int ext4_balloc_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_addr)
     84int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
    8585{
    8686        int rc;
    8787
    88         uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, block_addr);
    89         uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, block_addr);
     88        ext4_filesystem_t *fs = inode_ref->fs;
     89        ext4_superblock_t *sb = fs->superblock;
     90
     91        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
     92        uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, block_addr);
    9093
    9194        ext4_block_group_ref_t *bg_ref;
     
    97100
    98101        uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
    99                         bg_ref->block_group, fs->superblock);
     102                        bg_ref->block_group, sb);
    100103        block_t *bitmap_block;
    101104        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
     
    116119        }
    117120
    118         uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
     121        uint32_t block_size = ext4_superblock_get_block_size(sb);
    119122
    120123        // Update superblock free blocks count
    121         uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
     124        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    122125        sb_free_blocks--;
    123         ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
     126        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    124127
    125128        // Update inode blocks count
    126         uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
     129        uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
    127130        ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
    128         ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
     131        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    129132        inode_ref->dirty = true;
    130133
    131134        // Update block group free blocks count
    132135        uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
    133                         bg_ref->block_group, fs->superblock);
     136                        bg_ref->block_group, sb);
    134137        free_blocks++;
    135138        ext4_block_group_set_free_blocks_count(bg_ref->block_group,
    136                         fs->superblock, free_blocks);
     139                        sb, free_blocks);
    137140        bg_ref->dirty = true;
    138141
     
    178181
    179182
    180 static uint32_t ext4_balloc_find_goal(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
     183static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
    181184{
    182185        int rc;
    183186        uint32_t goal = 0;
    184187
    185         uint64_t inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
    186         uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
     188        ext4_superblock_t *sb = inode_ref->fs->superblock;
     189
     190        uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
     191        uint32_t block_size = ext4_superblock_get_block_size(sb);
    187192        uint32_t inode_block_count = inode_size / block_size;
    188193
     
    193198        if (inode_block_count > 0) {
    194199                // TODO check retval
    195                 ext4_filesystem_get_inode_data_block_index(fs, inode_ref, inode_block_count - 1, &goal);
     200                ext4_filesystem_get_inode_data_block_index(inode_ref, inode_block_count - 1, &goal);
    196201
    197202                // TODO
     
    203208
    204209        // Identify block group of inode
    205         uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
     210        uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
    206211        uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
    207         block_size = ext4_superblock_get_block_size(fs->superblock);
     212        block_size = ext4_superblock_get_block_size(sb);
    208213
    209214        ext4_block_group_ref_t *bg_ref;
    210         rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     215        rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
    211216        if (rc != EOK) {
    212217                return 0;
    213218        }
    214219
    215         uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
     220        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    216221        uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
    217                         bg_ref->block_group, fs->superblock);
    218         uint16_t inode_table_item_size = ext4_superblock_get_inode_size(fs->superblock);
     222                        bg_ref->block_group, sb);
     223        uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
    219224        uint32_t inode_table_bytes;
    220225
     
    223228        } else {
    224229                // last block group could be smaller
    225                 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(fs->superblock);
     230                uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
    226231                inode_table_bytes =
    227232                                (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
     
    242247}
    243248
    244 int ext4_balloc_alloc_block(ext4_filesystem_t *fs,
     249int ext4_balloc_alloc_block(
    245250                ext4_inode_ref_t *inode_ref, uint32_t *fblock)
    246251{
     
    253258
    254259        // Find GOAL
    255         uint32_t goal = ext4_balloc_find_goal(fs, inode_ref);
     260        uint32_t goal = ext4_balloc_find_goal(inode_ref);
    256261        if (goal == 0) {
    257262                // TODO
     
    260265        }
    261266
     267        ext4_superblock_t *sb = inode_ref->fs->superblock;
     268
    262269        // Load block group number for goal and relative index
    263         uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, goal);
    264         uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, goal);
     270        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
     271        uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, goal);
    265272
    266273
    267274        ext4_block_group_ref_t *bg_ref;
    268         rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     275        rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
    269276        if (rc != EOK) {
    270277                EXT4FS_DBG("initial BG ref not loaded");
     
    273280
    274281        uint32_t first_in_group =
    275                         ext4_balloc_get_first_data_block_in_group(fs->superblock,
     282                        ext4_balloc_get_first_data_block_in_group(sb,
    276283                                        bg_ref->block_group, block_group);
    277284
    278285        uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
    279                         fs->superblock, first_in_group);
     286                        sb, first_in_group);
    280287
    281288        if (index_in_group < first_in_group_index) {
     
    285292        // Load bitmap
    286293        bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
    287                         fs->superblock);
    288 
    289         rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
     294                        sb);
     295
     296        rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
    290297        if (rc != EOK) {
    291298                ext4_filesystem_put_block_group_ref(bg_ref);
     
    311318        }
    312319
    313         uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, block_group);
     320        uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(sb, block_group);
    314321
    315322        uint32_t end_idx = (index_in_group + 63) & ~63;
     
    331338
    332339                        allocated_block = ext4_balloc_index_in_group2blockaddr(
    333                                         fs->superblock, tmp_idx, block_group);
     340                                        sb, tmp_idx, block_group);
    334341
    335342                        goto success;
     
    349356
    350357                allocated_block = ext4_balloc_index_in_group2blockaddr(
    351                                 fs->superblock, rel_block_idx, block_group);
     358                                sb, rel_block_idx, block_group);
    352359
    353360                goto success;
     
    365372
    366373                allocated_block = ext4_balloc_index_in_group2blockaddr(
    367                                 fs->superblock, rel_block_idx, block_group);
     374                                sb, rel_block_idx, block_group);
    368375
    369376                goto success;
     
    375382
    376383        // Try other block groups
    377         uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
     384        uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
    378385
    379386        uint32_t bgid = (block_group + 1) % block_group_count;
     
    381388
    382389        while (count > 0) {
    383                 rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
     390                rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
    384391                if (rc != EOK) {
    385392                        EXT4FS_DBG("errrrrrrrrrrr");
     
    389396                // Load bitmap
    390397                bitmap_block_addr = ext4_block_group_get_block_bitmap(
    391                                 bg_ref->block_group, fs->superblock);
    392 
    393                 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
     398                                bg_ref->block_group, sb);
     399
     400                rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
    394401                if (rc != EOK) {
    395402                        ext4_filesystem_put_block_group_ref(bg_ref);
     
    399406
    400407                first_in_group = ext4_balloc_get_first_data_block_in_group(
    401                                 fs->superblock, bg_ref->block_group, bgid);
    402                 index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock,
     408                                sb, bg_ref->block_group, bgid);
     409                index_in_group = ext4_balloc_blockaddr2_index_in_group(sb,
    403410                                                first_in_group);
    404                 blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, bgid);
     411                blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
    405412
    406413                first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
    407                         fs->superblock, first_in_group);
     414                        sb, first_in_group);
    408415
    409416                if (index_in_group < first_in_group_index) {
     
    422429
    423430                        allocated_block = ext4_balloc_index_in_group2blockaddr(
    424                                         fs->superblock, rel_block_idx, bgid);
     431                                        sb, rel_block_idx, bgid);
    425432
    426433                        goto success;
     
    438445
    439446                        allocated_block = ext4_balloc_index_in_group2blockaddr(
    440                                         fs->superblock, rel_block_idx, bgid);
     447                                        sb, rel_block_idx, bgid);
    441448
    442449                        goto success;
     
    456463        ;       // Empty command - because of syntax
    457464       
    458         uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
     465        uint32_t block_size = ext4_superblock_get_block_size(sb);
    459466
    460467        // Update superblock free blocks count
    461         uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
     468        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
    462469        sb_free_blocks--;
    463         ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
     470        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
    464471
    465472        // Update inode blocks (different block size!) count
    466473
    467         uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
     474        uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
    468475        ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
    469         ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
     476        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
    470477        inode_ref->dirty = true;
    471478
    472479        // Update block group free blocks count
    473480        uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
    474                         bg_ref->block_group, fs->superblock);
     481                        bg_ref->block_group, sb);
    475482        bg_free_blocks--;
    476         ext4_block_group_set_free_blocks_count(bg_ref->block_group,
    477                         fs->superblock, bg_free_blocks);
     483        ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb, bg_free_blocks);
    478484        bg_ref->dirty = true;
    479485
  • uspace/lib/ext4/libext4_balloc.h

    r38384ae r1ac1ab4  
    3737#include "libext4_types.h"
    3838
    39 extern int ext4_balloc_free_block(ext4_filesystem_t *,
    40                 ext4_inode_ref_t *, uint32_t);
    41 extern int ext4_balloc_alloc_block(ext4_filesystem_t *,
    42                 ext4_inode_ref_t *, uint32_t *);
     39extern int ext4_balloc_free_block(ext4_inode_ref_t *, uint32_t);
     40extern int ext4_balloc_alloc_block(ext4_inode_ref_t *, uint32_t *);
    4341
    4442#endif
  • uspace/lib/ext4/libext4_directory.c

    r38384ae r1ac1ab4  
    186186
    187187                uint32_t next_block_phys_idx;
    188                 rc = ext4_filesystem_get_inode_data_block_index(it->fs,
    189                     it->inode_ref, next_block_idx, &next_block_phys_idx);
     188                rc = ext4_filesystem_get_inode_data_block_index(it->inode_ref,
     189                                next_block_idx, &next_block_phys_idx);
    190190                if (rc != EOK) {
    191191                        return rc;
     
    260260}
    261261
    262 int ext4_directory_append_block(ext4_filesystem_t *fs,
    263                 ext4_inode_ref_t *inode_ref, uint32_t *fblock, uint32_t *iblock)
     262int ext4_directory_append_block(ext4_inode_ref_t *inode_ref,
     263                uint32_t *fblock, uint32_t *iblock)
    264264{
    265265        int rc;
    266266
     267        ext4_superblock_t *sb = inode_ref->fs->superblock;
     268
    267269        // Compute next block index and allocate data block
    268         uint64_t inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
    269         uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
     270        uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
     271        uint32_t block_size = ext4_superblock_get_block_size(sb);
    270272
    271273        assert(inode_size % block_size == 0);
     
    275277
    276278        uint32_t phys_block;
    277         rc =  ext4_balloc_alloc_block(fs, inode_ref, &phys_block);
     279        rc =  ext4_balloc_alloc_block(inode_ref, &phys_block);
    278280        if (rc != EOK) {
    279281                return rc;
    280282        }
    281283
    282         rc = ext4_filesystem_set_inode_data_block_index(fs, inode_ref, new_block_idx, phys_block);
     284        rc = ext4_filesystem_set_inode_data_block_index(inode_ref, new_block_idx, phys_block);
    283285        if (rc != EOK) {
    284                 ext4_balloc_free_block(fs, inode_ref, phys_block);
     286                ext4_balloc_free_block(inode_ref, phys_block);
    285287                return rc;
    286288        }
     
    313315}
    314316
    315 int ext4_directory_add_entry(ext4_filesystem_t *fs, ext4_inode_ref_t * parent,
     317int ext4_directory_add_entry(ext4_inode_ref_t * parent,
    316318                const char *name, ext4_inode_ref_t *child)
    317319{
     
    319321
    320322        EXT4FS_DBG("adding entry to directory \%u [ino = \%u, name = \%s]", parent->index, child->index, name);
     323
     324        ext4_filesystem_t *fs = parent->fs;
    321325
    322326        // Index adding (if allowed)
     
    324328                        ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
    325329
    326                 rc = ext4_directory_dx_add_entry(fs, parent, child, name);
     330                rc = ext4_directory_dx_add_entry(parent, child, name);
    327331
    328332                // Check if index is not corrupted
     
    356360        for (iblock = 0; iblock < total_blocks; ++iblock) {
    357361
    358                 rc = ext4_filesystem_get_inode_data_block_index(fs, parent, iblock, &fblock);
     362                rc = ext4_filesystem_get_inode_data_block_index(parent, iblock, &fblock);
    359363                if (rc != EOK) {
    360364                        return rc;
     
    384388        // No free block found - needed to allocate next block
    385389
    386         rc = ext4_directory_append_block(fs, parent, &fblock, &iblock);
     390        rc = ext4_directory_append_block(parent, &fblock, &iblock);
    387391        if (rc != EOK) {
    388392                return rc;
     
    411415}
    412416
    413 int ext4_directory_find_entry(ext4_filesystem_t *fs,
    414                 ext4_directory_search_result_t *result, ext4_inode_ref_t *parent,
    415                 const char *name)
     417int ext4_directory_find_entry(ext4_directory_search_result_t *result,
     418                ext4_inode_ref_t *parent, const char *name)
    416419{
    417420        int rc;
    418421        uint32_t name_len = strlen(name);
    419422
     423        ext4_superblock_t *sb = parent->fs->superblock;
     424
    420425        // Index search
    421         if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
     426        if (ext4_superblock_has_feature_compatible(sb, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
    422427                        ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
    423428
    424                 rc = ext4_directory_dx_find_entry(result, fs, parent, name_len, name);
     429                rc = ext4_directory_dx_find_entry(result, parent, name_len, name);
    425430
    426431                // Check if index is not corrupted
     
    437442
    438443        uint32_t iblock, fblock;
    439         uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    440         uint32_t inode_size = ext4_inode_get_size(fs->superblock, parent->inode);
     444        uint32_t block_size = ext4_superblock_get_block_size(sb);
     445        uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
    441446        uint32_t total_blocks = inode_size / block_size;
    442447
    443448        for (iblock = 0; iblock < total_blocks; ++iblock) {
    444449
    445                 rc = ext4_filesystem_get_inode_data_block_index(fs, parent, iblock, &fblock);
     450                rc = ext4_filesystem_get_inode_data_block_index(parent, iblock, &fblock);
    446451                if (rc != EOK) {
    447452                        return rc;
     
    449454
    450455                block_t *block;
    451                 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
     456                rc = block_get(&block, parent->fs->device, fblock, BLOCK_FLAGS_NONE);
    452457                if (rc != EOK) {
    453458                        return rc;
     
    456461                // find block entry
    457462                ext4_directory_entry_ll_t *res_entry;
    458                 rc = ext4_directory_find_in_block(block, fs->superblock, name_len, name, &res_entry);
     463                rc = ext4_directory_find_in_block(block, sb, name_len, name, &res_entry);
    459464                if (rc == EOK) {
    460465                        result->block = block;
     
    476481
    477482
    478 int ext4_directory_remove_entry(ext4_filesystem_t* fs,
    479                 ext4_inode_ref_t *parent, const char *name)
     483int ext4_directory_remove_entry(ext4_inode_ref_t *parent, const char *name)
    480484{
    481485        int rc;
    482486
    483         if (!ext4_inode_is_type(fs->superblock, parent->inode,
     487        if (!ext4_inode_is_type(parent->fs->superblock, parent->inode,
    484488            EXT4_INODE_MODE_DIRECTORY)) {
    485489                return ENOTDIR;
     
    487491
    488492        ext4_directory_search_result_t result;
    489         rc  = ext4_directory_find_entry(fs, &result, parent, name);
     493        rc  = ext4_directory_find_entry(&result, parent, name);
    490494        if (rc != EOK) {
    491495                return rc;
  • uspace/lib/ext4/libext4_directory.h

    r38384ae r1ac1ab4  
    5858extern int ext4_directory_iterator_fini(ext4_directory_iterator_t *);
    5959
    60 extern int ext4_directory_append_block(ext4_filesystem_t *,
    61                 ext4_inode_ref_t *, uint32_t *, uint32_t *);
     60extern int ext4_directory_append_block(ext4_inode_ref_t *,
     61                uint32_t *, uint32_t *);
    6262
    6363extern void ext4_directory_write_entry(ext4_superblock_t *,
    6464                ext4_directory_entry_ll_t *, uint16_t, ext4_inode_ref_t *,
    6565                const char *, size_t);
    66 extern int ext4_directory_add_entry(ext4_filesystem_t *, ext4_inode_ref_t *,
     66extern int ext4_directory_add_entry(ext4_inode_ref_t *,
    6767                const char *, ext4_inode_ref_t *);
    68 extern int ext4_directory_find_entry(ext4_filesystem_t *,
    69                 ext4_directory_search_result_t *, ext4_inode_ref_t *, const char *);
    70 extern int ext4_directory_remove_entry(ext4_filesystem_t* ,
     68extern int ext4_directory_find_entry(ext4_directory_search_result_t *,
    7169                ext4_inode_ref_t *, const char *);
     70extern int ext4_directory_remove_entry(ext4_inode_ref_t *, const char *);
    7271
    7372extern int ext4_directory_try_insert_entry(ext4_superblock_t *,
  • uspace/lib/ext4/libext4_directory_index.c

    r38384ae r1ac1ab4  
    189189
    190190static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
    191                 ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, block_t *root_block,
     191                ext4_inode_ref_t *inode_ref, block_t *root_block,
    192192                ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
    193193{
     
    239239
    240240        uint32_t fblock;
    241         rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, next_block, &fblock);
     241        rc = ext4_filesystem_get_inode_data_block_index(
     242                        inode_ref, next_block, &fblock);
    242243        if (rc != EOK) {
    243244                return rc;
    244245        }
    245246
    246         rc = block_get(&tmp_block, fs->device, fblock, BLOCK_FLAGS_NONE);
     247        rc = block_get(&tmp_block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
    247248        if (rc != EOK) {
    248249                return rc;
     
    250251
    251252                entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
    252                 limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
    253 
    254         uint16_t entry_space = ext4_superblock_get_block_size(fs->superblock)
     253                limit = ext4_directory_dx_countlimit_get_limit(
     254                                (ext4_directory_dx_countlimit_t *)entries);
     255
     256        uint16_t entry_space = ext4_superblock_get_block_size(inode_ref->fs->superblock)
    255257                        - sizeof(ext4_directory_dx_dot_entry_t);
    256258        entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
     
    270272
    271273
    272 static int ext4_directory_dx_next_block(ext4_filesystem_t *fs,
    273                 ext4_inode_ref_t *inode_ref, uint32_t hash,
     274static int ext4_directory_dx_next_block(ext4_inode_ref_t *inode_ref, uint32_t hash,
    274275                ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
    275276{
     
    310311        uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
    311312        uint32_t block_addr;
    312         rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, block_idx, &block_addr);
     313        rc = ext4_filesystem_get_inode_data_block_index(inode_ref, block_idx, &block_addr);
    313314        if (rc != EOK) {
    314315                return rc;
     
    316317
    317318        block_t *block;
    318         rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
     319        rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
    319320        if (rc != EOK) {
    320321                return rc;
     
    334335
    335336int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
    336                 ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
     337                ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
    337338{
    338339        int rc;
     
    340341        // get direct block 0 (index root)
    341342        uint32_t root_block_addr;
    342         rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, 0, &root_block_addr);
     343        rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0, &root_block_addr);
    343344        if (rc != EOK) {
    344345                return rc;
    345346        }
     347
     348        ext4_filesystem_t *fs = inode_ref->fs;
    346349
    347350        block_t *root_block;
     
    361364        ext4_directory_dx_block_t dx_blocks[2];
    362365        ext4_directory_dx_block_t *dx_block, *tmp;
    363         rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref, root_block, &dx_block, dx_blocks);
     366        rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block, &dx_block, dx_blocks);
    364367        if (rc != EOK) {
    365368                block_put(root_block);
     
    372375                uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
    373376                uint32_t leaf_block_addr;
    374         rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, leaf_block_idx, &leaf_block_addr);
     377        rc = ext4_filesystem_get_inode_data_block_index(inode_ref, leaf_block_idx, &leaf_block_addr);
    375378        if (rc != EOK) {
    376379                goto cleanup;
     
    400403                }
    401404
    402                 rc = ext4_directory_dx_next_block(fs, inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
     405                rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
    403406                if (rc < 0) {
    404407                        goto cleanup;
     
    524527        uint32_t new_fblock;
    525528        uint32_t new_iblock;
    526         rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
     529        rc = ext4_directory_append_block(inode_ref, &new_fblock, &new_iblock);
    527530        if (rc != EOK) {
    528531                free(sort_array);
     
    648651                uint32_t new_fblock;
    649652                uint32_t new_iblock;
    650                 rc =  ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
     653                rc =  ext4_directory_append_block(inode_ref, &new_fblock, &new_iblock);
    651654                if (rc != EOK) {
    652655                        return rc;
     
    740743}
    741744
    742 int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
    743                 ext4_inode_ref_t *parent, ext4_inode_ref_t *child, const char *name)
     745int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
     746                ext4_inode_ref_t *child, const char *name)
    744747{
    745748        int rc = EOK;
     
    748751        // get direct block 0 (index root)
    749752        uint32_t root_block_addr;
    750         rc = ext4_filesystem_get_inode_data_block_index(fs, parent, 0, &root_block_addr);
     753        rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
    751754        if (rc != EOK) {
    752755                return rc;
    753756        }
     757
     758        ext4_filesystem_t *fs = parent->fs;
    754759
    755760        block_t *root_block;
     
    770775        ext4_directory_dx_block_t dx_blocks[2];
    771776        ext4_directory_dx_block_t *dx_block, *dx_it;
    772         rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent, root_block, &dx_block, dx_blocks);
     777        rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
    773778        if (rc != EOK) {
    774779                rc = EXT4_ERR_BAD_DX_DIR;
     
    780785        uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
    781786        uint32_t leaf_block_addr;
    782         rc = ext4_filesystem_get_inode_data_block_index(fs, parent, leaf_block_idx, &leaf_block_addr);
     787        rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
    783788        if (rc != EOK) {
    784789                goto release_index;
  • uspace/lib/ext4/libext4_directory_index.h

    r38384ae r1ac1ab4  
    6767
    6868extern int ext4_directory_dx_find_entry(ext4_directory_search_result_t *,
    69                 ext4_filesystem_t *, ext4_inode_ref_t *, size_t, const char *);
    70 extern int ext4_directory_dx_add_entry(ext4_filesystem_t *,
     69                ext4_inode_ref_t *, size_t, const char *);
     70extern int ext4_directory_dx_add_entry(
    7171                ext4_inode_ref_t *, ext4_inode_ref_t *, const char *);
    7272
  • uspace/lib/ext4/libext4_extent.c

    r38384ae r1ac1ab4  
    218218}
    219219
    220 // Reading routine without saving blocks to path
    221 //static int ext4_extent_find_extent(ext4_filesystem_t *fs,
    222 //              ext4_inode_ref_t *inode_ref, uint32_t iblock, ext4_extent_t *extent)
    223 //{
    224 //      int rc;
    225 //
    226 //      block_t* block = NULL;
    227 //
    228 //      ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
    229 //      while (ext4_extent_header_get_depth(header) != 0) {
    230 //
    231 //              ext4_extent_index_t *index;
    232 //              ext4_extent_binsearch_idx(header, &index, iblock);
    233 //
    234 //              uint64_t child = ext4_extent_index_get_leaf(index);
    235 //
    236 //              if (block != NULL) {
    237 //                      block_put(block);
    238 //              }
    239 //
    240 //              rc = block_get(&block, fs->device, child, BLOCK_FLAGS_NONE);
    241 //              if (rc != EOK) {
    242 //                      return rc;
    243 //              }
    244 //
    245 //              header = (ext4_extent_header_t *)block->data;
    246 //      }
    247 //
    248 //
    249 //      ext4_extent_t* tmp_extent;
    250 //      ext4_extent_binsearch(header, &tmp_extent, iblock);
    251 //
    252 //      memcpy(extent, tmp_extent, sizeof(ext4_extent_t));
    253 //
    254 //      return EOK;
    255 //}
    256 
    257 static int ext4_extent_find_extent(ext4_filesystem_t *fs,
    258                 ext4_inode_ref_t *inode_ref, uint32_t iblock, ext4_extent_path_t **ret_path)
     220// Reading routine without saving blocks to path - for saving memory during finding block
     221int ext4_extent_find_block(ext4_inode_ref_t *inode_ref, uint32_t iblock, uint32_t *fblock)
    259222{
    260223        int rc;
    261224
    262         ext4_extent_header_t *eh =
    263                         ext4_inode_get_extent_header(inode_ref->inode);
    264 
    265         uint16_t depth = ext4_extent_header_get_depth(eh);
    266 
    267         ext4_extent_path_t *tmp_path;
    268 
    269         // Added 2 for possible tree growing
    270         tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
    271         if (tmp_path == NULL) {
    272                 return ENOMEM;
    273         }
    274 
    275         tmp_path[0].block = inode_ref->block;
    276         tmp_path[0].header = eh;
    277 
    278         uint16_t pos = 0;
    279         while (ext4_extent_header_get_depth(eh) != 0) {
    280 
    281                 ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
    282 
    283                 tmp_path[pos].depth = depth;
    284                 tmp_path[pos].extent = NULL;
    285 
    286                 assert(tmp_path[pos].index != NULL);
    287 
    288                 uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
    289 
    290                 block_t *block;
    291                 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
     225        block_t* block = NULL;
     226
     227        ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
     228        while (ext4_extent_header_get_depth(header) != 0) {
     229
     230                ext4_extent_index_t *index;
     231                ext4_extent_binsearch_idx(header, &index, iblock);
     232
     233                uint64_t child = ext4_extent_index_get_leaf(index);
     234
     235                if (block != NULL) {
     236                        block_put(block);
     237                }
     238
     239                rc = block_get(&block, inode_ref->fs->device, child, BLOCK_FLAGS_NONE);
    292240                if (rc != EOK) {
    293                         // TODO cleanup
    294                         EXT4FS_DBG("ERRRR");
    295241                        return rc;
    296242                }
    297243
    298                 pos++;
    299 
    300                 eh = (ext4_extent_header_t *)block->data;
    301                 tmp_path[pos].block = block;
    302                 tmp_path[pos].header = eh;
    303 
    304         }
    305 
    306         tmp_path[pos].depth = 0;
    307         tmp_path[pos].extent = NULL;
    308         tmp_path[pos].index = NULL;
    309 
    310     /* find extent */
    311         ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
    312 
    313         *ret_path = tmp_path;
    314 
    315         return EOK;
    316 }
    317 
    318 
    319 int ext4_extent_find_block(ext4_filesystem_t *fs,
    320                 ext4_inode_ref_t *inode_ref, uint32_t iblock, uint32_t *fblock)
    321 {
    322         int rc;
    323 
    324         ext4_extent_path_t *path;
    325         rc = ext4_extent_find_extent(fs, inode_ref, iblock, &path);
    326         if (rc != EOK) {
    327                 return rc;
    328         }
    329 
    330         uint16_t depth = path->depth;
    331 
    332         ext4_extent_t *extent = path[depth].extent;
     244                header = (ext4_extent_header_t *)block->data;
     245        }
     246
     247
     248        ext4_extent_t* extent;
     249        ext4_extent_binsearch(header, &extent, iblock);
     250
    333251
    334252        uint32_t phys_block;
     
    338256        *fblock = phys_block;
    339257
    340         // Put loaded blocks
    341         // From 1 -> 0 is a block with inode data
    342         for (uint16_t i = 1; i < depth; ++i) {
    343                 if (path[i].block) {
    344                         block_put(path[i].block);
    345                 }
    346         }
    347 
    348         // Destroy temporary data structure
    349         free(path);
     258        if (block != NULL) {
     259                block_put(block);
     260        }
     261
    350262
    351263        return EOK;
    352 
    353 }
     264}
     265
     266//static int ext4_extent_find_extent(ext4_filesystem_t *fs,
     267//              ext4_inode_ref_t *inode_ref, uint32_t iblock, ext4_extent_path_t **ret_path)
     268//{
     269//      int rc;
     270//
     271//      ext4_extent_header_t *eh =
     272//                      ext4_inode_get_extent_header(inode_ref->inode);
     273//
     274//      uint16_t depth = ext4_extent_header_get_depth(eh);
     275//
     276//      ext4_extent_path_t *tmp_path;
     277//
     278//      // Added 2 for possible tree growing
     279//      tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
     280//      if (tmp_path == NULL) {
     281//              return ENOMEM;
     282//      }
     283//
     284//      tmp_path[0].block = inode_ref->block;
     285//      tmp_path[0].header = eh;
     286//
     287//      uint16_t pos = 0;
     288//      while (ext4_extent_header_get_depth(eh) != 0) {
     289//
     290//              ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
     291//
     292//              tmp_path[pos].depth = depth;
     293//              tmp_path[pos].extent = NULL;
     294//
     295//              assert(tmp_path[pos].index != NULL);
     296//
     297//              uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
     298//
     299//              block_t *block;
     300//              rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
     301//              if (rc != EOK) {
     302//                      // TODO cleanup
     303//                      EXT4FS_DBG("ERRRR");
     304//                      return rc;
     305//              }
     306//
     307//              pos++;
     308//
     309//              eh = (ext4_extent_header_t *)block->data;
     310//              tmp_path[pos].block = block;
     311//              tmp_path[pos].header = eh;
     312//
     313//      }
     314//
     315//      tmp_path[pos].depth = 0;
     316//      tmp_path[pos].extent = NULL;
     317//      tmp_path[pos].index = NULL;
     318//
     319//    /* find extent */
     320//      ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
     321//
     322//      *ret_path = tmp_path;
     323//
     324//      return EOK;
     325//}
     326
     327
     328//int ext4_extent_find_block(ext4_filesystem_t *fs,
     329//              ext4_inode_ref_t *inode_ref, uint32_t iblock, uint32_t *fblock)
     330//{
     331//      int rc;
     332//
     333//      ext4_extent_path_t *path;
     334//      rc = ext4_extent_find_extent(fs, inode_ref, iblock, &path);
     335//      if (rc != EOK) {
     336//              return rc;
     337//      }
     338//
     339//      uint16_t depth = path->depth;
     340//
     341//      ext4_extent_t *extent = path[depth].extent;
     342//
     343//      uint32_t phys_block;
     344//      phys_block = ext4_extent_get_start(extent) + iblock;
     345//      phys_block -= ext4_extent_get_first_block(extent);
     346//
     347//      *fblock = phys_block;
     348//
     349//      // Put loaded blocks
     350//      // From 1 -> 0 is a block with inode data
     351//      for (uint16_t i = 1; i < depth; ++i) {
     352//              if (path[i].block) {
     353//                      block_put(path[i].block);
     354//              }
     355//      }
     356//
     357//      // Destroy temporary data structure
     358//      free(path);
     359//
     360//      return EOK;
     361//
     362//}
    354363
    355364/**
  • uspace/lib/ext4/libext4_extent.h

    r38384ae r1ac1ab4  
    6161extern void ext4_extent_header_set_generation(ext4_extent_header_t *, uint32_t);
    6262
    63 extern int ext4_extent_find_block(ext4_filesystem_t *, ext4_inode_ref_t *,
    64                 uint32_t, uint32_t *);
     63extern int ext4_extent_find_block(ext4_inode_ref_t *, uint32_t, uint32_t *);
    6564#endif
    6665
  • uspace/lib/ext4/libext4_filesystem.c

    r38384ae r1ac1ab4  
    169169
    170170        newref->block_group = newref->block->data + offset;
     171        newref->fs = fs;
     172        newref->index = bgid;
    171173        newref->dirty = false;
    172174
     
    176178}
    177179
     180static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
     181                            ext4_block_group_t *bg)
     182{
     183        uint16_t crc = 0;
     184
     185        if (ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
     186
     187                void *base = bg;
     188                void *checksum = &bg->checksum;
     189
     190                uint32_t offset = (uint32_t)(checksum - base);
     191
     192                uint32_t le_group = host2uint32_t_le(bgid);
     193
     194                crc = crc16(~0, sb->uuid, sizeof(sb->uuid));
     195                crc = crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
     196                crc = crc16(crc, (uint8_t *)bg, offset);
     197
     198                offset += sizeof(bg->checksum); /* skip checksum */
     199
     200                /* for checksum of struct ext4_group_desc do the rest...*/
     201                if ((ext4_superblock_has_feature_incompatible(sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
     202                        offset < ext4_superblock_get_desc_size(sb)) {
     203
     204                        crc = crc16(crc, ((uint8_t *)bg) + offset, ext4_superblock_get_desc_size(sb) - offset);
     205                }
     206        }
     207
     208        return crc;
     209
     210}
     211
     212
    178213int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
    179214{
     
    181216
    182217        if (ref->dirty) {
     218                 uint16_t checksum = ext4_filesystem_bg_checksum(
     219                                ref->fs->superblock, ref->index, ref->block_group);
     220
     221                 ext4_block_group_set_checksum(ref->block_group, checksum);
     222
    183223                ref->block->dirty = true;
    184224        }
     
    242282         * in the reference
    243283         */
    244         newref->index = index+1;
     284        newref->index = index + 1;
     285        newref->fs = fs;
    245286        newref->dirty = false;
    246287
     
    321362}
    322363
    323 int ext4_filesystem_free_inode(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
     364int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
    324365{
    325366        int rc;
     
    330371        uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
    331372        if (fblock != 0) {
    332                 rc = ext4_balloc_free_block(fs, inode_ref, fblock);
     373                rc = ext4_balloc_free_block(inode_ref, fblock);
    333374                if (rc != EOK) {
    334375                        return rc;
     
    337378                ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
    338379        }
     380
     381        ext4_filesystem_t *fs = inode_ref->fs;
    339382
    340383        block_t *block;
     
    355398
    356399                        if (ind_block != 0) {
    357                                 rc = ext4_balloc_free_block(fs, inode_ref, ind_block);
     400                                rc = ext4_balloc_free_block(inode_ref, ind_block);
    358401                                if (rc != EOK) {
    359402                                        block_put(block);
     
    364407
    365408                block_put(block);
    366                 rc = ext4_balloc_free_block(fs, inode_ref, fblock);
     409                rc = ext4_balloc_free_block(inode_ref, fblock);
    367410                if (rc != EOK) {
    368411                        return rc;
     
    398441
    399442                                        if (ind_subblock != 0) {
    400                                                 rc = ext4_balloc_free_block(fs, inode_ref, ind_subblock);
     443                                                rc = ext4_balloc_free_block(inode_ref, ind_subblock);
    401444                                                if (rc != EOK) {
    402445                                                        block_put(subblock);
     
    411454                        }
    412455
    413                         rc = ext4_balloc_free_block(fs, inode_ref, ind_block);
     456                        rc = ext4_balloc_free_block(inode_ref, ind_block);
    414457                        if (rc != EOK) {
    415458                                block_put(block);
     
    421464
    422465                block_put(block);
    423                 rc = ext4_balloc_free_block(fs, inode_ref, fblock);
     466                rc = ext4_balloc_free_block(inode_ref, fblock);
    424467                if (rc != EOK) {
    425468                        return rc;
     
    445488}
    446489
    447 int ext4_filesystem_truncate_inode(ext4_filesystem_t *fs,
     490int ext4_filesystem_truncate_inode(
    448491                ext4_inode_ref_t *inode_ref, aoff64_t new_size)
    449492{
    450493        int rc;
    451494
    452         if (! ext4_inode_can_truncate(fs->superblock, inode_ref->inode)) {
     495        ext4_superblock_t *sb = inode_ref->fs->superblock;
     496
     497        if (! ext4_inode_can_truncate(sb, inode_ref->inode)) {
    453498                // Unable to truncate
    454499                return EINVAL;
    455500        }
    456501
    457         aoff64_t old_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
     502        aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
    458503        if (old_size == new_size) {
    459504                // Nothing to do
     
    467512
    468513        aoff64_t size_diff = old_size - new_size;
    469         uint32_t block_size  = ext4_superblock_get_block_size(fs->superblock);
     514        uint32_t block_size  = ext4_superblock_get_block_size(sb);
    470515        uint32_t diff_blocks_count = size_diff / block_size;
    471516        if (size_diff % block_size != 0) {
     
    480525        // starting from 1 because of logical blocks are numbered from 0
    481526        for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
    482                 rc = ext4_filesystem_release_inode_block(fs, inode_ref, old_blocks_count - i);
     527                rc = ext4_filesystem_release_inode_block(inode_ref, old_blocks_count - i);
    483528                if (rc != EOK) {
    484529                        return rc;
     
    493538}
    494539
    495 int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs,
    496                 ext4_inode_ref_t *inode_ref, aoff64_t iblock, uint32_t* fblock)
    497 {
    498         int rc;
    499 
     540int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
     541                aoff64_t iblock, uint32_t* fblock)
     542{
     543        int rc;
     544
     545        ext4_filesystem_t *fs = inode_ref->fs;
    500546
    501547        uint32_t current_block;
     
    504550        if (ext4_superblock_has_feature_incompatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
    505551                        ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
    506                 rc = ext4_extent_find_block(fs, inode_ref, iblock, &current_block);
     552                rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
    507553
    508554                if (rc != EOK) {
     
    591637
    592638
    593 int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *fs,
    594                 ext4_inode_ref_t *inode_ref, aoff64_t iblock, uint32_t fblock)
    595 {
    596         int rc;
    597 
     639int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
     640                aoff64_t iblock, uint32_t fblock)
     641{
     642        int rc;
     643
     644        ext4_filesystem_t *fs = inode_ref->fs;
    598645
    599646        /* Handle inode using extents */
     
    635682
    636683        if (current_block == 0) {
    637                 rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
     684                rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
    638685                if (rc != EOK) {
    639686                        return rc;
     
    646693                rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
    647694                if (rc != EOK) {
    648                         ext4_balloc_free_block(fs, inode_ref, new_block_addr);
     695                        ext4_balloc_free_block(inode_ref, new_block_addr);
    649696                        return rc;
    650697                }
     
    674721
    675722                if ((level > 1) && (current_block == 0)) {
    676                         rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
     723                        rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
    677724                        if (rc != EOK) {
    678725                                block_put(block);
     
    727774}
    728775
    729 int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
     776int ext4_filesystem_release_inode_block(
    730777                ext4_inode_ref_t *inode_ref, uint32_t iblock)
    731778{
     
    733780
    734781        uint32_t fblock;
     782
     783        ext4_filesystem_t *fs = inode_ref->fs;
    735784
    736785        /* TODO Handle extents */
     
    772821
    773822                ext4_inode_set_direct_block(inode, iblock, 0);
    774                 return ext4_balloc_free_block(fs, inode_ref, fblock);
     823                return ext4_balloc_free_block(inode_ref, fblock);
    775824        }
    776825
     
    837886        }
    838887
    839         return ext4_balloc_free_block(fs, inode_ref, fblock);
    840 
    841 }
    842 
    843 int ext4_filesystem_add_orphan(ext4_filesystem_t *fs,
    844                 ext4_inode_ref_t *inode_ref)
    845 {
    846         uint32_t next_orphan = ext4_superblock_get_last_orphan(fs->superblock);
     888        return ext4_balloc_free_block(inode_ref, fblock);
     889
     890}
     891
     892int ext4_filesystem_add_orphan(ext4_inode_ref_t *inode_ref)
     893{
     894        uint32_t next_orphan = ext4_superblock_get_last_orphan(
     895                        inode_ref->fs->superblock);
    847896        ext4_inode_set_deletion_time(inode_ref->inode, next_orphan);
    848         ext4_superblock_set_last_orphan(fs->superblock, inode_ref->index);
     897        ext4_superblock_set_last_orphan(
     898                        inode_ref->fs->superblock, inode_ref->index);
    849899        inode_ref->dirty = true;
    850900
     
    852902}
    853903
    854 int ext4_filesystem_delete_orphan(ext4_filesystem_t *fs,
    855                 ext4_inode_ref_t *inode_ref)
    856 {
    857         int rc;
    858 
    859         uint32_t last_orphan = ext4_superblock_get_last_orphan(fs->superblock);
     904int ext4_filesystem_delete_orphan(ext4_inode_ref_t *inode_ref)
     905{
     906        int rc;
     907
     908        uint32_t last_orphan = ext4_superblock_get_last_orphan(
     909                        inode_ref->fs->superblock);
    860910        assert(last_orphan > 0);
    861911
     
    863913
    864914        if (last_orphan == inode_ref->index) {
    865                 ext4_superblock_set_last_orphan(fs->superblock, next_orphan);
     915                ext4_superblock_set_last_orphan(inode_ref->fs->superblock, next_orphan);
    866916                ext4_inode_set_deletion_time(inode_ref->inode, 0);
    867917                inode_ref->dirty = true;
     
    870920
    871921        ext4_inode_ref_t *current;
    872         rc = ext4_filesystem_get_inode_ref(fs, last_orphan, &current);
     922        rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, &current);
    873923        if (rc != EOK) {
    874924                return rc;
     
    889939                ext4_filesystem_put_inode_ref(current);
    890940
    891                 rc = ext4_filesystem_get_inode_ref(fs, next_orphan, &current);
     941                rc = ext4_filesystem_get_inode_ref(inode_ref->fs, next_orphan, &current);
    892942                if (rc != EOK) {
    893943                        return rc;
  • uspace/lib/ext4/libext4_filesystem.h

    r38384ae r1ac1ab4  
    4949extern int ext4_filesystem_alloc_inode(ext4_filesystem_t *,
    5050                ext4_inode_ref_t **, int);
    51 extern int ext4_filesystem_free_inode(ext4_filesystem_t *, ext4_inode_ref_t *);
    52 extern int ext4_filesystem_truncate_inode(ext4_filesystem_t *,
    53                 ext4_inode_ref_t *, aoff64_t);
    54 extern int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *,
    55         ext4_inode_ref_t *, aoff64_t iblock, uint32_t *);
    56 extern int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *,
    57                 ext4_inode_ref_t *, aoff64_t, uint32_t);
    58 extern int ext4_filesystem_release_inode_block(ext4_filesystem_t *,
     51extern int ext4_filesystem_free_inode(ext4_inode_ref_t *);
     52extern int ext4_filesystem_truncate_inode(ext4_inode_ref_t *, aoff64_t);
     53extern int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *,
     54                aoff64_t iblock, uint32_t *);
     55extern int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *,
     56                aoff64_t, uint32_t);
     57extern int ext4_filesystem_release_inode_block(
    5958                ext4_inode_ref_t *, uint32_t);
    60 extern int ext4_filesystem_add_orphan(ext4_filesystem_t *,
    61                 ext4_inode_ref_t *);
    62 extern int ext4_filesystem_delete_orphan(ext4_filesystem_t *,
    63                 ext4_inode_ref_t *);
     59extern int ext4_filesystem_add_orphan(ext4_inode_ref_t *);
     60extern int ext4_filesystem_delete_orphan(ext4_inode_ref_t *);
     61
     62extern uint16_t ext4_group_desc_csum(ext4_superblock_t *, uint32_t,
     63                ext4_block_group_t *);
    6464#endif
    6565
  • uspace/lib/ext4/libext4_ialloc.c

    r38384ae r1ac1ab4  
    111111        ext4_block_group_set_free_inodes_count(bg_ref->block_group,
    112112                        sb, free_inodes);
     113
     114        uint32_t unused_inodes = ext4_block_group_get_itable_unused(
     115                        bg_ref->block_group, sb);
     116        unused_inodes++;
     117        ext4_block_group_set_itable_unused(bg_ref->block_group, sb, unused_inodes);
     118
     119
    113120        bg_ref->dirty = true;
    114121
     
    186193                        ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
    187194
     195                        uint16_t unused_inodes = ext4_block_group_get_itable_unused(bg, sb);
     196                        unused_inodes--;
     197                        ext4_block_group_set_itable_unused(bg, sb, unused_inodes);
     198
    188199                        if (is_dir) {
    189200                                used_dirs++;
  • uspace/lib/ext4/libext4_types.h

    r38384ae r1ac1ab4  
    194194
    195195/*****************************************************************************/
     196
     197typedef struct ext4_filesystem {
     198        service_id_t device;
     199        ext4_superblock_t *     superblock;
     200        aoff64_t inode_block_limits[4];
     201        aoff64_t inode_blocks_per_level[4];
     202} ext4_filesystem_t;
     203
     204
     205/*****************************************************************************/
     206
     207
    196208/*
    197209 * Structure of a blocks group descriptor
     
    222234        block_t *block; // Reference to a block containing this block group descr
    223235        ext4_block_group_t *block_group;
     236        ext4_filesystem_t *fs;
     237        uint32_t index;
    224238        bool dirty;
    225239} ext4_block_group_ref_t;
     
    229243#define EXT4_BLOCK_MAX_GROUP_DESCRIPTOR_SIZE 64
    230244
    231 
    232 /*****************************************************************************/
    233 
    234 typedef struct ext4_filesystem {
    235         service_id_t device;
    236         ext4_superblock_t *     superblock;
    237         aoff64_t inode_block_limits[4];
    238         aoff64_t inode_blocks_per_level[4];
    239 } ext4_filesystem_t;
     245/*****************************************************************************/
     246
    240247
    241248#define EXT4_MIN_BLOCK_SIZE             1024  //1 KiB
    242249#define EXT4_MAX_BLOCK_SIZE     65536 //64 KiB
    243250#define EXT4_REV0_INODE_SIZE    128
    244 
    245 /*****************************************************************************/
    246 
    247251
    248252#define EXT4_INODE_BLOCK_SIZE                           512
     
    346350        block_t *block; // Reference to a block containing this inode
    347351        ext4_inode_t *inode;
     352        ext4_filesystem_t *fs;
    348353        uint32_t index; // Index number of this inode
    349354        bool dirty;
  • uspace/srv/fs/ext4fs/ext4fs.c

    r38384ae r1ac1ab4  
    6161                        ext4fs_vfs_info.instance = strtol(argv[2], NULL, 10);
    6262                else {
    63                         printf(NAME " Unrecognized parameters");
     63                        printf(NAME " Unrecognized parameters\n");
    6464                        return -1;
    6565                }
  • uspace/srv/fs/ext4fs/ext4fs_ops.c

    r38384ae r1ac1ab4  
    211211
    212212        ext4_directory_search_result_t result;
    213         rc = ext4_directory_find_entry(fs, &result, eparent->inode_ref, component);
     213        rc = ext4_directory_find_entry(&result, eparent->inode_ref, component);
    214214        if (rc != EOK) {
    215215                if (rc == ENOENT) {
     
    448448        ext4_inode_ref_t *inode_ref = enode->inode_ref;
    449449
    450         rc = ext4_filesystem_truncate_inode(fs, inode_ref, 0);
     450        rc = ext4_filesystem_truncate_inode(inode_ref, 0);
    451451        if (rc != EOK) {
    452452                ext4fs_node_put(fn);
     
    456456        uint32_t rev_level = ext4_superblock_get_rev_level(fs->superblock);
    457457        if (rev_level > 0) {
    458                 ext4_filesystem_delete_orphan(fs, inode_ref);
     458                ext4_filesystem_delete_orphan(inode_ref);
    459459        }
    460460
     
    465465        inode_ref->dirty = true;
    466466
    467         rc = ext4_filesystem_free_inode(fs, inode_ref);
     467        rc = ext4_filesystem_free_inode(inode_ref);
    468468        if (rc != EOK) {
    469469                ext4fs_node_put(fn);
     
    489489
    490490        // Add entry to parent directory
    491         rc = ext4_directory_add_entry(fs, parent->inode_ref, name, child->inode_ref);
     491        rc = ext4_directory_add_entry(parent->inode_ref, name, child->inode_ref);
    492492        if (rc != EOK) {
    493493                return rc;
     
    497497        if (ext4_inode_is_type(fs->superblock, child->inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
    498498
    499                 rc = ext4_directory_add_entry(fs, child->inode_ref, ".", child->inode_ref);
     499                rc = ext4_directory_add_entry(child->inode_ref, ".", child->inode_ref);
    500500                if (rc != EOK) {
    501                         ext4_directory_remove_entry(fs, parent->inode_ref, name);
     501                        ext4_directory_remove_entry(parent->inode_ref, name);
    502502                        return rc;
    503503                }
    504504
    505                 rc = ext4_directory_add_entry(fs, child->inode_ref, "..", parent->inode_ref);
     505                rc = ext4_directory_add_entry(child->inode_ref, "..", parent->inode_ref);
    506506                if (rc != EOK) {
    507                         ext4_directory_remove_entry(fs, parent->inode_ref, name);
    508                         ext4_directory_remove_entry(fs, child->inode_ref, ".");
     507                        ext4_directory_remove_entry(parent->inode_ref, name);
     508                        ext4_directory_remove_entry(child->inode_ref, ".");
    509509                        return rc;
    510510                }
     
    546546        ext4_inode_ref_t *parent = EXT4FS_NODE(pfn)->inode_ref;
    547547        ext4_filesystem_t *fs = EXT4FS_NODE(pfn)->instance->filesystem;
    548         rc = ext4_directory_remove_entry(fs, parent, name);
     548        rc = ext4_directory_remove_entry(parent, name);
    549549        if (rc != EOK) {
    550550                return rc;
     
    576576        uint32_t rev_level = ext4_superblock_get_rev_level(fs->superblock);
    577577        if ((rev_level > 0) && (lnk_count == 0)) {
    578                 ext4_filesystem_add_orphan(fs, child_inode_ref);
     578                ext4_filesystem_add_orphan(child_inode_ref);
    579579        }
    580580
     
    10021002        /* Get the real block number */
    10031003        uint32_t fs_block;
    1004         rc = ext4_filesystem_get_inode_data_block_index(inst->filesystem,
    1005                 inode_ref, file_block, &fs_block);
     1004        rc = ext4_filesystem_get_inode_data_block_index(inode_ref, file_block, &fs_block);
    10061005        if (rc != EOK) {
    10071006                async_answer_0(callid, rc);
     
    10901089
    10911090        ext4_inode_ref_t *inode_ref = enode->inode_ref;
    1092         rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, iblock, &fblock);
     1091        rc = ext4_filesystem_get_inode_data_block_index(inode_ref, iblock, &fblock);
    10931092        if (rc != EOK) {
    10941093                ext4fs_node_put(fn);
     
    10981097
    10991098        if (fblock == 0) {
    1100                 rc =  ext4_balloc_alloc_block(fs, inode_ref, &fblock);
     1099                rc =  ext4_balloc_alloc_block(inode_ref, &fblock);
    11011100                if (rc != EOK) {
    11021101                        ext4fs_node_put(fn);
     
    11051104                }
    11061105
    1107                 rc = ext4_filesystem_set_inode_data_block_index(fs, inode_ref, iblock, fblock);
     1106                rc = ext4_filesystem_set_inode_data_block_index(inode_ref, iblock, fblock);
    11081107                if (rc != EOK) {
    1109                         ext4_balloc_free_block(fs, inode_ref, fblock);
     1108                        ext4_balloc_free_block(inode_ref, fblock);
    11101109                        ext4fs_node_put(fn);
    11111110                        async_answer_0(callid, rc);
     
    11691168        ext4fs_node_t *enode = EXT4FS_NODE(fn);
    11701169        ext4_inode_ref_t *inode_ref = enode->inode_ref;
    1171         ext4_filesystem_t *fs = enode->instance->filesystem;
    1172 
    1173         rc = ext4_filesystem_truncate_inode(fs, inode_ref, new_size);
     1170
     1171        rc = ext4_filesystem_truncate_inode(inode_ref, new_size);
    11741172        ext4fs_node_put(fn);
    11751173
Note: See TracChangeset for help on using the changeset viewer.