Changeset b73530a in mainline


Ignore:
Timestamp:
2012-04-08T14:01:49Z (12 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
82cb6768
Parents:
936132f
Message:

appending fblock to existing extent

  • actually not solved situation, when extent is full !!!
Location:
uspace/lib/ext4
Files:
4 edited

Legend:

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

    r936132f rb73530a  
    569569}
    570570
     571int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock)
     572{
     573        int rc;
     574
     575        ext4_filesystem_t *fs = inode_ref->fs;
     576        ext4_superblock_t *sb = fs->superblock;
     577
     578        uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
     579        uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, fblock);
     580
     581        ext4_block_group_ref_t *bg_ref;
     582        rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     583        if (rc != EOK) {
     584                EXT4FS_DBG("error in loading bg_ref \%d", rc);
     585                return rc;
     586        }
     587
     588        uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
     589                        bg_ref->block_group, sb);
     590        block_t *bitmap_block;
     591        rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
     592        if (rc != EOK) {
     593                EXT4FS_DBG("error in loading bitmap \%d", rc);
     594                return rc;
     595        }
     596
     597        bool free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
     598
     599        if (free) {
     600                ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
     601                bitmap_block->dirty = true;
     602        }
     603
     604        rc = block_put(bitmap_block);
     605        if (rc != EOK) {
     606                // Error in saving bitmap
     607                ext4_filesystem_put_block_group_ref(bg_ref);
     608                EXT4FS_DBG("error in saving bitmap \%d", rc);
     609                return rc;
     610        }
     611
     612        if (!free) {
     613                rc = EINVAL;
     614                goto terminate;
     615        }
     616
     617        uint32_t block_size = ext4_superblock_get_block_size(sb);
     618
     619        // Update superblock free blocks count
     620        uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
     621        sb_free_blocks--;
     622        ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
     623
     624        // Update inode blocks count
     625        uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
     626        ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
     627        ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
     628        inode_ref->dirty = true;
     629
     630        // Update block group free blocks count
     631        uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
     632                        bg_ref->block_group, sb);
     633        free_blocks--;
     634        ext4_block_group_set_free_blocks_count(bg_ref->block_group,
     635                        sb, free_blocks);
     636        bg_ref->dirty = true;
     637
     638terminate:
     639
     640        rc = ext4_filesystem_put_block_group_ref(bg_ref);
     641        if (rc != EOK) {
     642                EXT4FS_DBG("error in saving bg_ref \%d", rc);
     643                return rc;
     644        }
     645
     646        return rc;
     647}
    571648
    572649/**
  • uspace/lib/ext4/libext4_balloc.h

    r936132f rb73530a  
    4141                uint32_t , uint32_t);
    4242extern int ext4_balloc_alloc_block(ext4_inode_ref_t *, uint32_t *);
     43extern int ext4_balloc_try_alloc_block(ext4_inode_ref_t *, uint32_t);
    4344
    4445#endif
  • uspace/lib/ext4/libext4_extent.c

    r936132f rb73530a  
    223223        int rc;
    224224
     225        uint64_t inode_size = ext4_inode_get_size(
     226                        inode_ref->fs->superblock, inode_ref->inode);
     227
     228        uint32_t block_size = ext4_superblock_get_block_size(
     229                        inode_ref->fs->superblock);
     230
     231        uint32_t last_idx = (inode_size - 1) / block_size;
     232
     233        if (iblock > last_idx) {
     234                *fblock = 0;
     235                return EOK;
     236        }
     237
    225238        block_t* block = NULL;
    226239
     
    549562        ext4_superblock_t *sb = inode_ref->fs->superblock;
    550563        uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
    551 
    552564        uint32_t block_size = ext4_superblock_get_block_size(sb);
    553         uint32_t new_block_idx = inode_size / block_size;
     565
     566        uint32_t new_block_idx = 0;
     567        if (inode_size > 0) {
     568                if ((inode_size % block_size) != 0) {
     569                        inode_size += block_size - (inode_size % block_size);
     570                }
     571                new_block_idx = inode_size / block_size;
     572        }
    554573
    555574        ext4_extent_path_t *path;
     
    565584        }
    566585
    567         // if extent == NULL -> add extent to leaf
     586        // if extent == NULL -> add extent to empty leaf
    568587        if (path_ptr->extent == NULL) {
     588
     589                EXT4FS_DBG("NULL extent");
     590
    569591                ext4_extent_t *ext = EXT4_EXTENT_FIRST(path_ptr->header);
    570592                ext4_extent_set_block_count(ext, 0);
     
    579601        if (ext4_extent_get_block_count(path_ptr->extent) == 0) {
    580602
     603                EXT4FS_DBG("no blocks in extent");
     604
    581605                // Add first block to the extent
    582606
    583607                rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
    584608                if (rc != EOK) {
     609                        EXT4FS_DBG("error in allocation");
    585610                        goto finish;
    586611                }
     
    588613                ext4_extent_set_block_count(path_ptr->extent, 1);
    589614                ext4_extent_set_start(path_ptr->extent, phys_block);
     615                ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
    590616
    591617                path_ptr->block->dirty = true;
     
    594620
    595621        } else {
    596                 // try allocate scceeding extent block
    597 
     622                // try allocate succeeding extent block
     623
     624                EXT4FS_DBG("existing extent");
     625
     626                uint16_t blocks = ext4_extent_get_block_count(path_ptr->extent);
     627
     628                if (blocks < (1 << 15)) {
     629                        // there is place for more blocks
     630
     631                        EXT4FS_DBG("appending block to existing extent");
     632
     633                        uint64_t last = ext4_extent_get_start(path_ptr->extent) + blocks - 1;
     634
     635                        rc = ext4_balloc_try_alloc_block(inode_ref, last + 1);
     636                        if (rc == EOK) {
     637                                path_ptr->block->dirty = true;
     638                                ext4_extent_set_block_count(path_ptr->extent, blocks + 1);
     639                                phys_block = last + 1;
     640                                goto finish;
     641                        }
     642
     643                        if (rc != EINVAL) {
     644                                goto finish;
     645                        }
     646                }
     647
     648                // Add new extent
    598649                // TODO
    599650                assert(false);
    600 
    601651        }
    602652
  • uspace/lib/ext4/libext4_filesystem.c

    r936132f rb73530a  
    578578
    579579int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
    580                 aoff64_t iblock, uint32_t* fblock)
     580                aoff64_t iblock, uint32_t *fblock)
    581581{
    582582        int rc;
    583583
    584584        ext4_filesystem_t *fs = inode_ref->fs;
     585
     586        if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
     587                *fblock = 0;
     588                return EOK;
     589        }
    585590
    586591        uint32_t current_block;
Note: See TracChangeset for help on using the changeset viewer.