[052e82d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Frantisek Princ
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext4
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file libext4_bitmap.c
|
---|
[12b4a7f] | 35 | * @brief Ext4 bitmap (block & inode) operations.
|
---|
[052e82d] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <libblock.h>
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include "libext4.h"
|
---|
| 42 |
|
---|
| 43 | static void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 44 | {
|
---|
[1e65444] | 45 | // Block numbers are 1-based
|
---|
[052e82d] | 46 | uint32_t byte_index = index / 8;
|
---|
| 47 | uint32_t bit_index = index % 8;
|
---|
| 48 |
|
---|
[1e65444] | 49 | // EXT4FS_DBG("freeing block \%u, byte \%u and bit \%u", index, byte_index, bit_index);
|
---|
| 50 |
|
---|
[052e82d] | 51 | uint8_t *target = bitmap + byte_index;
|
---|
| 52 | uint8_t old_value = *target;
|
---|
| 53 |
|
---|
| 54 | uint8_t new_value = old_value & (~ (1 << bit_index));
|
---|
| 55 |
|
---|
| 56 | *target = new_value;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[1c1c736] | 59 | static int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t *index, uint32_t size)
|
---|
| 60 | {
|
---|
| 61 | uint8_t *pos = bitmap;
|
---|
| 62 | int i;
|
---|
| 63 | uint32_t idx = 0;
|
---|
[1e65444] | 64 | uint8_t value, new_value;
|
---|
[1c1c736] | 65 |
|
---|
| 66 | while (pos < bitmap + size) {
|
---|
| 67 | if ((*pos & 255) != 255) {
|
---|
| 68 | // free bit found
|
---|
| 69 | break;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | ++pos;
|
---|
| 73 | idx += 8;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if (pos < bitmap + size) {
|
---|
| 77 |
|
---|
[1e65444] | 78 | // EXT4FS_DBG("byte found \%u", (uint32_t)(pos - bitmap));
|
---|
| 79 |
|
---|
[1c1c736] | 80 | for(i = 0; i < 8; ++i) {
|
---|
[1e65444] | 81 | value = *pos;
|
---|
| 82 |
|
---|
| 83 | if ((value & (1 << i)) == 0) {
|
---|
[1c1c736] | 84 | // free bit found
|
---|
[1e65444] | 85 | // EXT4FS_DBG("bit found \%u", i);
|
---|
| 86 | new_value = value | (1 << i);
|
---|
| 87 | *pos = new_value;
|
---|
| 88 | *index = idx + i;
|
---|
[1c1c736] | 89 | return EOK;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return ENOSPC;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[6088193] | 97 | int ext4_bitmap_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_index)
|
---|
[052e82d] | 98 | {
|
---|
| 99 | int rc;
|
---|
| 100 | uint32_t blocks_per_group;
|
---|
| 101 | uint32_t block_group;
|
---|
| 102 | uint32_t index_in_group;
|
---|
| 103 | uint32_t bitmap_block;
|
---|
[6088193] | 104 | uint32_t block_size;
|
---|
[052e82d] | 105 | ext4_block_group_ref_t *bg_ref;
|
---|
| 106 | block_t *block;
|
---|
| 107 |
|
---|
[6088193] | 108 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 109 |
|
---|
[052e82d] | 110 | blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
|
---|
[1e65444] | 111 | block_group = ((block_index - 1) / blocks_per_group);
|
---|
| 112 | index_in_group = (block_index - 1) % blocks_per_group;
|
---|
[052e82d] | 113 |
|
---|
| 114 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 115 | if (rc != EOK) {
|
---|
| 116 | return rc;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
|
---|
| 120 |
|
---|
| 121 | rc = block_get(&block, fs->device, bitmap_block, 0);
|
---|
| 122 | if (rc != EOK) {
|
---|
| 123 | return rc;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | ext4_bitmap_free_bit(block->data, index_in_group);
|
---|
| 127 |
|
---|
| 128 | block->dirty = true;
|
---|
| 129 | rc = block_put(block);
|
---|
| 130 | if (rc != EOK) {
|
---|
| 131 | return rc;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[6088193] | 134 | uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
|
---|
| 135 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 136 | ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
|
---|
| 137 | inode_ref->dirty = true;
|
---|
| 138 |
|
---|
[052e82d] | 139 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
|
---|
| 140 | free_blocks++;
|
---|
| 141 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, free_blocks);
|
---|
[12b4a7f] | 142 | bg_ref->dirty = true;
|
---|
[052e82d] | 143 |
|
---|
[1c1c736] | 144 | // TODO change free blocks count in superblock
|
---|
| 145 |
|
---|
[052e82d] | 146 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 147 | if (rc != EOK) {
|
---|
[6088193] | 148 | EXT4FS_DBG("error in saving bg_ref \%d", rc);
|
---|
[052e82d] | 149 | // TODO error
|
---|
| 150 | return rc;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[6088193] | 153 | // EXT4FS_DBG("block \%u released", block_index);
|
---|
[1e65444] | 154 |
|
---|
[052e82d] | 155 | return EOK;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[1c1c736] | 158 | int ext4_bitmap_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
| 159 | {
|
---|
| 160 | int rc;
|
---|
| 161 | uint32_t inodes_per_group, block_group, blocks_per_group;
|
---|
| 162 | ext4_block_group_ref_t *bg_ref;
|
---|
| 163 | uint32_t bitmap_block;
|
---|
| 164 | block_t *block;
|
---|
| 165 | uint32_t rel_block_idx = 0;
|
---|
| 166 | uint32_t block_size;
|
---|
| 167 |
|
---|
| 168 | inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
[1e65444] | 169 | block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
[1c1c736] | 170 |
|
---|
| 171 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 172 |
|
---|
| 173 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 174 | if (rc != EOK) {
|
---|
| 175 | return rc;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
|
---|
| 179 |
|
---|
| 180 | rc = block_get(&block, fs->device, bitmap_block, 0);
|
---|
| 181 | if (rc != EOK) {
|
---|
| 182 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 183 | return rc;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | rc = ext4_bitmap_find_free_bit_and_set(block->data, &rel_block_idx, block_size);
|
---|
[35f48f2] | 187 | if (rc != EOK) {
|
---|
[6088193] | 188 | EXT4FS_DBG("no free block found");
|
---|
| 189 | // TODO if ENOSPC - try next block groups
|
---|
[35f48f2] | 190 | }
|
---|
[1c1c736] | 191 |
|
---|
[1e65444] | 192 | block->dirty = true;
|
---|
| 193 |
|
---|
[6088193] | 194 | rc = block_put(block);
|
---|
| 195 | if (rc != EOK) {
|
---|
| 196 | // TODO error
|
---|
| 197 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
---|
| 198 | }
|
---|
[1e65444] | 199 |
|
---|
[35f48f2] | 200 | // TODO decrement superblock free blocks count
|
---|
| 201 | //uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
| 202 | //sb_free_blocks--;
|
---|
| 203 | //ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[1c1c736] | 204 |
|
---|
[6088193] | 205 | uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
|
---|
| 206 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 207 | ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
|
---|
| 208 | inode_ref->dirty = true;
|
---|
| 209 |
|
---|
[35f48f2] | 210 | uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
|
---|
[1e65444] | 211 | bg_free_blocks--;
|
---|
[35f48f2] | 212 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, bg_free_blocks);
|
---|
| 213 | bg_ref->dirty = true;
|
---|
[1c1c736] | 214 |
|
---|
[1e65444] | 215 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 216 |
|
---|
[1c1c736] | 217 | blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
|
---|
[1e65444] | 218 |
|
---|
[6088193] | 219 | // EXT4FS_DBG("block \%u allocated", blocks_per_group * block_group + rel_block_idx + 1);
|
---|
[1e65444] | 220 |
|
---|
| 221 | *fblock = blocks_per_group * block_group + rel_block_idx + 1;
|
---|
[1c1c736] | 222 | return EOK;
|
---|
| 223 |
|
---|
| 224 | }
|
---|
[052e82d] | 225 |
|
---|
| 226 | /**
|
---|
| 227 | * @}
|
---|
| 228 | */
|
---|