Changeset 1c1c736 in mainline for uspace/lib/ext4/libext4_bitmap.c


Ignore:
Timestamp:
2011-11-08T13:05:43Z (13 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0f09d4ea
Parents:
e31e56a1
Message:

not functional block allocation + write operation skeleton

File:
1 edited

Legend:

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

    re31e56a1 r1c1c736  
    5454}
    5555
     56static int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t *index, uint32_t size)
     57{
     58        uint8_t *pos = bitmap;
     59        int i;
     60        uint32_t idx = 0;
     61
     62        while (pos < bitmap + size) {
     63                if ((*pos & 255) != 255) {
     64                        // free bit found
     65                        break;
     66                }
     67
     68                ++pos;
     69                idx += 8;
     70        }
     71
     72        if (pos < bitmap + size) {
     73
     74                for(i = 0; i < 8; ++i) {
     75                        if ((*pos & (1 << i)) == 0) {
     76                                // free bit found
     77                                *pos |= (1 << i);
     78                                *index = idx;
     79                                return EOK;
     80                        }
     81
     82                        idx++;
     83                }
     84        }
     85
     86        return ENOSPC;
     87}
     88
    5689int ext4_bitmap_free_block(ext4_filesystem_t *fs, uint32_t block_index)
    5790{
     
    93126        bg_ref->dirty = true;
    94127
     128        // TODO change free blocks count in superblock
     129
    95130        rc = ext4_filesystem_put_block_group_ref(bg_ref);
    96131        if (rc != EOK) {
     
    102137}
    103138
     139int ext4_bitmap_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
     140{
     141        int rc;
     142        uint32_t inodes_per_group, block_group, blocks_per_group;
     143        ext4_block_group_ref_t *bg_ref;
     144        uint32_t bitmap_block;
     145        block_t *block;
     146        uint32_t rel_block_idx = 0;
     147        uint32_t block_size;
     148
     149        inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
     150        block_group = inode_ref->index / inodes_per_group;
     151
     152        block_size = ext4_superblock_get_block_size(fs->superblock);
     153
     154        rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
     155        if (rc != EOK) {
     156                return rc;
     157        }
     158
     159        bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
     160
     161        rc = block_get(&block, fs->device, bitmap_block, 0);
     162        if (rc != EOK) {
     163                ext4_filesystem_put_block_group_ref(bg_ref);
     164                return rc;
     165        }
     166
     167        rc = ext4_bitmap_find_free_bit_and_set(block->data, &rel_block_idx, block_size);
     168
     169        // if ENOSPC - try next block group - try next block groups
     170
     171
     172
     173
     174        // TODO decrement superblock & block group free blocks count
     175
     176        // return
     177        blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
     178        *fblock = blocks_per_group * block_group + rel_block_idx;
     179        return EOK;
     180
     181}
    104182
    105183/**
Note: See TracChangeset for help on using the changeset viewer.