| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 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_balloc.c
|
|---|
| 35 | * @brief Physical block allocator.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <sys/types.h>
|
|---|
| 40 | #include "libext4.h"
|
|---|
| 41 |
|
|---|
| 42 | /** Compute number of block group from block address.
|
|---|
| 43 | *
|
|---|
| 44 | * @param sb superblock pointer
|
|---|
| 45 | * @param block_addr absolute address of block
|
|---|
| 46 | * @return block group index
|
|---|
| 47 | */
|
|---|
| 48 | static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb,
|
|---|
| 49 | uint32_t block_addr)
|
|---|
| 50 | {
|
|---|
| 51 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 52 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
|---|
| 53 |
|
|---|
| 54 | /* First block == 0 or 1 */
|
|---|
| 55 | if (first_block == 0) {
|
|---|
| 56 | return block_addr / blocks_per_group;
|
|---|
| 57 | } else {
|
|---|
| 58 | return (block_addr - 1) / blocks_per_group;
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | /** Free block.
|
|---|
| 64 | *
|
|---|
| 65 | * @param inode_ref inode, where the block is allocated
|
|---|
| 66 | * @param block_addr absolute block address to free
|
|---|
| 67 | * @return error code
|
|---|
| 68 | */
|
|---|
| 69 | int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
|
|---|
| 70 | {
|
|---|
| 71 | int rc;
|
|---|
| 72 |
|
|---|
| 73 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 74 | ext4_superblock_t *sb = fs->superblock;
|
|---|
| 75 |
|
|---|
| 76 | /* Compute indexes */
|
|---|
| 77 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
|
|---|
| 78 | uint32_t index_in_group =
|
|---|
| 79 | ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
|
|---|
| 80 |
|
|---|
| 81 | /* Load block group reference */
|
|---|
| 82 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 83 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
|---|
| 84 | if (rc != EOK) {
|
|---|
| 85 | EXT4FS_DBG("error in loading bg_ref \%d", rc);
|
|---|
| 86 | return rc;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* Load block with bitmap */
|
|---|
| 90 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 91 | bg_ref->block_group, sb);
|
|---|
| 92 | block_t *bitmap_block;
|
|---|
| 93 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
|---|
| 94 | if (rc != EOK) {
|
|---|
| 95 | EXT4FS_DBG("error in loading bitmap \%d", rc);
|
|---|
| 96 | return rc;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /* Modify bitmap */
|
|---|
| 100 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
|---|
| 101 | bitmap_block->dirty = true;
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | /* Release block with bitmap */
|
|---|
| 105 | rc = block_put(bitmap_block);
|
|---|
| 106 | if (rc != EOK) {
|
|---|
| 107 | /* Error in saving bitmap */
|
|---|
| 108 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 109 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
|---|
| 110 | return rc;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 114 |
|
|---|
| 115 | /* Update superblock free blocks count */
|
|---|
| 116 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
|---|
| 117 | sb_free_blocks++;
|
|---|
| 118 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
|---|
| 119 |
|
|---|
| 120 | /* Update inode blocks count */
|
|---|
| 121 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
|---|
| 122 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
|---|
| 123 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
|---|
| 124 | inode_ref->dirty = true;
|
|---|
| 125 |
|
|---|
| 126 | /* Update block group free blocks count */
|
|---|
| 127 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
|
|---|
| 128 | bg_ref->block_group, sb);
|
|---|
| 129 | free_blocks++;
|
|---|
| 130 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
|---|
| 131 | sb, free_blocks);
|
|---|
| 132 | bg_ref->dirty = true;
|
|---|
| 133 |
|
|---|
| 134 | /* Release block group reference */
|
|---|
| 135 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 136 | if (rc != EOK) {
|
|---|
| 137 | EXT4FS_DBG("error in saving bg_ref \%d", rc);
|
|---|
| 138 | return rc;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | /** Free continuous set of blocks.
|
|---|
| 146 | *
|
|---|
| 147 | * @param inode_ref inode, where the blocks are allocated
|
|---|
| 148 | * @param first first block to release
|
|---|
| 149 | * @param count number of blocks to release
|
|---|
| 150 | */
|
|---|
| 151 | int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
|
|---|
| 152 | uint32_t first, uint32_t count)
|
|---|
| 153 | {
|
|---|
| 154 | int rc;
|
|---|
| 155 |
|
|---|
| 156 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 157 | ext4_superblock_t *sb = fs->superblock;
|
|---|
| 158 |
|
|---|
| 159 | /* Compute indexes */
|
|---|
| 160 | uint32_t block_group_first =
|
|---|
| 161 | ext4_balloc_get_bgid_of_block(sb, first);
|
|---|
| 162 | uint32_t block_group_last =
|
|---|
| 163 | ext4_balloc_get_bgid_of_block(sb, first + count - 1);
|
|---|
| 164 |
|
|---|
| 165 | assert(block_group_first == block_group_last);
|
|---|
| 166 |
|
|---|
| 167 | /* Load block group reference */
|
|---|
| 168 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 169 | rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
|
|---|
| 170 | if (rc != EOK) {
|
|---|
| 171 | EXT4FS_DBG("error in loading bg_ref \%d", rc);
|
|---|
| 172 | return rc;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | uint32_t index_in_group_first =
|
|---|
| 176 | ext4_filesystem_blockaddr2_index_in_group(sb, first);
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 | /* Load block with bitmap */
|
|---|
| 180 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 181 | bg_ref->block_group, sb);
|
|---|
| 182 |
|
|---|
| 183 | block_t *bitmap_block;
|
|---|
| 184 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
|---|
| 185 | if (rc != EOK) {
|
|---|
| 186 | EXT4FS_DBG("error in loading bitmap \%d", rc);
|
|---|
| 187 | return rc;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /* Modify bitmap */
|
|---|
| 191 | ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
|
|---|
| 192 | bitmap_block->dirty = true;
|
|---|
| 193 |
|
|---|
| 194 | /* Release block with bitmap */
|
|---|
| 195 | rc = block_put(bitmap_block);
|
|---|
| 196 | if (rc != EOK) {
|
|---|
| 197 | /* Error in saving bitmap */
|
|---|
| 198 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 199 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
|---|
| 200 | return rc;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 204 |
|
|---|
| 205 | /* Update superblock free blocks count */
|
|---|
| 206 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
|---|
| 207 | sb_free_blocks += count;
|
|---|
| 208 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
|---|
| 209 |
|
|---|
| 210 | /* Update inode blocks count */
|
|---|
| 211 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
|---|
| 212 | ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
|
|---|
| 213 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
|---|
| 214 | inode_ref->dirty = true;
|
|---|
| 215 |
|
|---|
| 216 | /* Update block group free blocks count */
|
|---|
| 217 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
|
|---|
| 218 | bg_ref->block_group, sb);
|
|---|
| 219 | free_blocks += count;
|
|---|
| 220 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
|---|
| 221 | sb, free_blocks);
|
|---|
| 222 | bg_ref->dirty = true;
|
|---|
| 223 |
|
|---|
| 224 | /* Release block group reference */
|
|---|
| 225 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 226 | if (rc != EOK) {
|
|---|
| 227 | EXT4FS_DBG("error in saving bg_ref \%d", rc);
|
|---|
| 228 | return rc;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Compute first block for data in block group.
|
|---|
| 235 | *
|
|---|
| 236 | * @param sb pointer to superblock
|
|---|
| 237 | * @param bg pointer to block group
|
|---|
| 238 | * @param bgid index of block group
|
|---|
| 239 | * @return absolute block index of first block
|
|---|
| 240 | */
|
|---|
| 241 | uint32_t ext4_balloc_get_first_data_block_in_group(
|
|---|
| 242 | ext4_superblock_t *sb, ext4_block_group_ref_t *bg_ref)
|
|---|
| 243 | {
|
|---|
| 244 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 245 | uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
|
|---|
| 246 | bg_ref->block_group, sb);
|
|---|
| 247 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
|---|
| 248 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
|---|
| 249 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 250 | uint32_t inode_table_bytes;
|
|---|
| 251 |
|
|---|
| 252 | if (bg_ref->index < block_group_count - 1) {
|
|---|
| 253 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
|---|
| 254 | } else {
|
|---|
| 255 | /* last block group could be smaller */
|
|---|
| 256 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
|---|
| 257 | inode_table_bytes =
|
|---|
| 258 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
|
|---|
| 259 | * inode_table_item_size;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | uint32_t inode_table_blocks = inode_table_bytes / block_size;
|
|---|
| 263 |
|
|---|
| 264 | if (inode_table_bytes % block_size) {
|
|---|
| 265 | inode_table_blocks++;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | return inode_table_first_block + inode_table_blocks;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /** Compute 'goal' for allocation algorithm.
|
|---|
| 272 | *
|
|---|
| 273 | * @param inode_ref reference to inode, to allocate block for
|
|---|
| 274 | * @return goal block number
|
|---|
| 275 | */
|
|---|
| 276 | static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
|
|---|
| 277 | {
|
|---|
| 278 | int rc;
|
|---|
| 279 | uint32_t goal = 0;
|
|---|
| 280 |
|
|---|
| 281 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 282 |
|
|---|
| 283 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
|---|
| 284 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 285 | uint32_t inode_block_count = inode_size / block_size;
|
|---|
| 286 |
|
|---|
| 287 | if (inode_size % block_size != 0) {
|
|---|
| 288 | inode_block_count++;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /* If inode has some blocks, get last block address + 1 */
|
|---|
| 292 | if (inode_block_count > 0) {
|
|---|
| 293 |
|
|---|
| 294 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, inode_block_count - 1, &goal);
|
|---|
| 295 | if (rc != EOK) {
|
|---|
| 296 | return 0;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | if (goal != 0) {
|
|---|
| 300 | goal++;
|
|---|
| 301 | return goal;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /* if goal == 0, sparse file -> continue */
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /* Identify block group of inode */
|
|---|
| 308 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
|---|
| 309 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
|---|
| 310 | block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 311 |
|
|---|
| 312 | /* Load block group reference */
|
|---|
| 313 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 314 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
|
|---|
| 315 | if (rc != EOK) {
|
|---|
| 316 | return 0;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /* Compute indexes */
|
|---|
| 320 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 321 | uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
|
|---|
| 322 | bg_ref->block_group, sb);
|
|---|
| 323 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
|---|
| 324 | uint32_t inode_table_bytes;
|
|---|
| 325 |
|
|---|
| 326 | /* Check for last block group */
|
|---|
| 327 | if (block_group < block_group_count - 1) {
|
|---|
| 328 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
|---|
| 329 | } else {
|
|---|
| 330 | /* last block group could be smaller */
|
|---|
| 331 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
|---|
| 332 | inode_table_bytes =
|
|---|
| 333 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
|
|---|
| 334 | * inode_table_item_size;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | uint32_t inode_table_blocks = inode_table_bytes / block_size;
|
|---|
| 338 |
|
|---|
| 339 | if (inode_table_bytes % block_size) {
|
|---|
| 340 | inode_table_blocks++;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | goal = inode_table_first_block + inode_table_blocks;
|
|---|
| 344 |
|
|---|
| 345 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 346 |
|
|---|
| 347 | return goal;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /** Data block allocation algorithm.
|
|---|
| 351 | *
|
|---|
| 352 | * @param inode_ref inode to allocate block for
|
|---|
| 353 | * @param fblock allocated block address
|
|---|
| 354 | * @return error code
|
|---|
| 355 | */
|
|---|
| 356 | int ext4_balloc_alloc_block(
|
|---|
| 357 | ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
|---|
| 358 | {
|
|---|
| 359 | int rc;
|
|---|
| 360 | uint32_t allocated_block = 0;
|
|---|
| 361 |
|
|---|
| 362 | uint32_t bitmap_block_addr;
|
|---|
| 363 | block_t *bitmap_block;
|
|---|
| 364 | uint32_t rel_block_idx = 0;
|
|---|
| 365 |
|
|---|
| 366 | /* Find GOAL */
|
|---|
| 367 | uint32_t goal = ext4_balloc_find_goal(inode_ref);
|
|---|
| 368 | if (goal == 0) {
|
|---|
| 369 | /* no goal found => partition is full */
|
|---|
| 370 | EXT4FS_DBG("ERROR (goal == 0)");
|
|---|
| 371 | return ENOSPC;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 375 |
|
|---|
| 376 | /* Load block group number for goal and relative index */
|
|---|
| 377 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
|
|---|
| 378 | uint32_t index_in_group =
|
|---|
| 379 | ext4_filesystem_blockaddr2_index_in_group(sb, goal);
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 | /* Load block group reference */
|
|---|
| 383 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 384 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
|
|---|
| 385 | if (rc != EOK) {
|
|---|
| 386 | EXT4FS_DBG("initial BG ref not loaded");
|
|---|
| 387 | return rc;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /* Compute indexes */
|
|---|
| 391 | uint32_t first_in_group =
|
|---|
| 392 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
|---|
| 393 |
|
|---|
| 394 | uint32_t first_in_group_index = ext4_filesystem_blockaddr2_index_in_group(
|
|---|
| 395 | sb, first_in_group);
|
|---|
| 396 |
|
|---|
| 397 | if (index_in_group < first_in_group_index) {
|
|---|
| 398 | index_in_group = first_in_group_index;
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | /* Load block with bitmap */
|
|---|
| 402 | bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 403 | bg_ref->block_group, sb);
|
|---|
| 404 |
|
|---|
| 405 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
|---|
| 406 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
|---|
| 407 | if (rc != EOK) {
|
|---|
| 408 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 409 | EXT4FS_DBG("initial bitmap not loaded");
|
|---|
| 410 | return rc;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | /* Check if goal is free */
|
|---|
| 414 | if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
|
|---|
| 415 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
|---|
| 416 | bitmap_block->dirty = true;
|
|---|
| 417 | rc = block_put(bitmap_block);
|
|---|
| 418 | if (rc != EOK) {
|
|---|
| 419 | EXT4FS_DBG("goal check: error in saving bitmap \%d", rc);
|
|---|
| 420 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 421 | return rc;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | allocated_block = ext4_filesystem_index_in_group2blockaddr(
|
|---|
| 425 | sb, index_in_group, block_group);
|
|---|
| 426 |
|
|---|
| 427 | goto success;
|
|---|
| 428 |
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(sb, block_group);
|
|---|
| 432 |
|
|---|
| 433 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
|---|
| 434 | if (end_idx > blocks_in_group) {
|
|---|
| 435 | end_idx = blocks_in_group;
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /* Try to find free block near to goal */
|
|---|
| 439 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) {
|
|---|
| 440 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
|---|
| 441 |
|
|---|
| 442 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
|---|
| 443 | bitmap_block->dirty = true;
|
|---|
| 444 | rc = block_put(bitmap_block);
|
|---|
| 445 | if (rc != EOK) {
|
|---|
| 446 | EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
|
|---|
| 447 | return rc;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | allocated_block = ext4_filesystem_index_in_group2blockaddr(
|
|---|
| 451 | sb, tmp_idx, block_group);
|
|---|
| 452 |
|
|---|
| 453 | goto success;
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /* Find free BYTE in bitmap */
|
|---|
| 459 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
|---|
| 460 | if (rc == EOK) {
|
|---|
| 461 | bitmap_block->dirty = true;
|
|---|
| 462 | rc = block_put(bitmap_block);
|
|---|
| 463 | if (rc != EOK) {
|
|---|
| 464 | EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
|
|---|
| 465 | return rc;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | allocated_block = ext4_filesystem_index_in_group2blockaddr(
|
|---|
| 469 | sb, rel_block_idx, block_group);
|
|---|
| 470 |
|
|---|
| 471 | goto success;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | /* Find free bit in bitmap */
|
|---|
| 475 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
|---|
| 476 | if (rc == EOK) {
|
|---|
| 477 | bitmap_block->dirty = true;
|
|---|
| 478 | rc = block_put(bitmap_block);
|
|---|
| 479 | if (rc != EOK) {
|
|---|
| 480 | EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
|
|---|
| 481 | return rc;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | allocated_block = ext4_filesystem_index_in_group2blockaddr(
|
|---|
| 485 | sb, rel_block_idx, block_group);
|
|---|
| 486 |
|
|---|
| 487 | goto success;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | /* No free block found yet */
|
|---|
| 491 | block_put(bitmap_block);
|
|---|
| 492 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 493 |
|
|---|
| 494 | /* Try other block groups */
|
|---|
| 495 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 496 |
|
|---|
| 497 | uint32_t bgid = (block_group + 1) % block_group_count;
|
|---|
| 498 | uint32_t count = block_group_count;
|
|---|
| 499 |
|
|---|
| 500 | while (count > 0) {
|
|---|
| 501 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
|
|---|
| 502 | if (rc != EOK) {
|
|---|
| 503 | EXT4FS_DBG("ERROR: unable to load block group \%u", bgid);
|
|---|
| 504 | return rc;
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | /* Load block with bitmap */
|
|---|
| 508 | bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 509 | bg_ref->block_group, sb);
|
|---|
| 510 |
|
|---|
| 511 | rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
|
|---|
| 512 | if (rc != EOK) {
|
|---|
| 513 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 514 | EXT4FS_DBG("ERROR: unable to load bitmap block");
|
|---|
| 515 | return rc;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | /* Compute indexes */
|
|---|
| 519 | first_in_group = ext4_balloc_get_first_data_block_in_group(
|
|---|
| 520 | sb, bg_ref);
|
|---|
| 521 | index_in_group = ext4_filesystem_blockaddr2_index_in_group(sb,
|
|---|
| 522 | first_in_group);
|
|---|
| 523 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
|---|
| 524 |
|
|---|
| 525 | first_in_group_index = ext4_filesystem_blockaddr2_index_in_group(
|
|---|
| 526 | sb, first_in_group);
|
|---|
| 527 |
|
|---|
| 528 | if (index_in_group < first_in_group_index) {
|
|---|
| 529 | index_in_group = first_in_group_index;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | /* Try to find free byte in bitmap */
|
|---|
| 533 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
|---|
| 534 | if (rc == EOK) {
|
|---|
| 535 | bitmap_block->dirty = true;
|
|---|
| 536 | rc = block_put(bitmap_block);
|
|---|
| 537 | if (rc != EOK) {
|
|---|
| 538 | EXT4FS_DBG("ERROR: unable to save bitmap block");
|
|---|
| 539 | return rc;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | allocated_block = ext4_filesystem_index_in_group2blockaddr(
|
|---|
| 543 | sb, rel_block_idx, bgid);
|
|---|
| 544 |
|
|---|
| 545 | goto success;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | /* Try to find free bit in bitmap */
|
|---|
| 549 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
|---|
| 550 | if (rc == EOK) {
|
|---|
| 551 | bitmap_block->dirty = true;
|
|---|
| 552 | rc = block_put(bitmap_block);
|
|---|
| 553 | if (rc != EOK) {
|
|---|
| 554 | EXT4FS_DBG("ERROR: unable to save bitmap block");
|
|---|
| 555 | return rc;
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | allocated_block = ext4_filesystem_index_in_group2blockaddr(
|
|---|
| 559 | sb, rel_block_idx, bgid);
|
|---|
| 560 |
|
|---|
| 561 | goto success;
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | block_put(bitmap_block);
|
|---|
| 565 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 566 |
|
|---|
| 567 | /* Goto next group */
|
|---|
| 568 | bgid = (bgid + 1) % block_group_count;
|
|---|
| 569 | count--;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | return ENOSPC;
|
|---|
| 573 |
|
|---|
| 574 | success:
|
|---|
| 575 | ; /* Empty command - because of syntax */
|
|---|
| 576 |
|
|---|
| 577 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 578 |
|
|---|
| 579 | /* Update superblock free blocks count */
|
|---|
| 580 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
|---|
| 581 | sb_free_blocks--;
|
|---|
| 582 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
|---|
| 583 |
|
|---|
| 584 | /* Update inode blocks (different block size!) count */
|
|---|
| 585 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
|---|
| 586 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
|---|
| 587 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
|---|
| 588 | inode_ref->dirty = true;
|
|---|
| 589 |
|
|---|
| 590 | /* Update block group free blocks count */
|
|---|
| 591 | uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
|
|---|
| 592 | bg_ref->block_group, sb);
|
|---|
| 593 | bg_free_blocks--;
|
|---|
| 594 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb, bg_free_blocks);
|
|---|
| 595 | bg_ref->dirty = true;
|
|---|
| 596 |
|
|---|
| 597 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 598 |
|
|---|
| 599 | *fblock = allocated_block;
|
|---|
| 600 | return EOK;
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | /** Try to allocate concrete block.
|
|---|
| 604 | *
|
|---|
| 605 | * @param inode_ref inode to allocate block for
|
|---|
| 606 | * @param fblock block address to allocate
|
|---|
| 607 | * @param free output value - if target block is free
|
|---|
| 608 | * @return error code
|
|---|
| 609 | */
|
|---|
| 610 | int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref,
|
|---|
| 611 | uint32_t fblock, bool *free)
|
|---|
| 612 | {
|
|---|
| 613 | int rc = EOK;
|
|---|
| 614 |
|
|---|
| 615 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 616 | ext4_superblock_t *sb = fs->superblock;
|
|---|
| 617 |
|
|---|
| 618 | /* Compute indexes */
|
|---|
| 619 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
|
|---|
| 620 | uint32_t index_in_group =
|
|---|
| 621 | ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
|
|---|
| 622 |
|
|---|
| 623 | /* Load block group reference */
|
|---|
| 624 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 625 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
|---|
| 626 | if (rc != EOK) {
|
|---|
| 627 | EXT4FS_DBG("error in loading bg_ref \%d", rc);
|
|---|
| 628 | return rc;
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | /* Load block with bitmap */
|
|---|
| 632 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 633 | bg_ref->block_group, sb);
|
|---|
| 634 | block_t *bitmap_block;
|
|---|
| 635 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
|---|
| 636 | if (rc != EOK) {
|
|---|
| 637 | EXT4FS_DBG("error in loading bitmap \%d", rc);
|
|---|
| 638 | return rc;
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | /* Check if block is free */
|
|---|
| 642 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
|---|
| 643 |
|
|---|
| 644 | /* Allocate block if possible */
|
|---|
| 645 | if (*free) {
|
|---|
| 646 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
|---|
| 647 | bitmap_block->dirty = true;
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | /* Release block with bitmap */
|
|---|
| 651 | rc = block_put(bitmap_block);
|
|---|
| 652 | if (rc != EOK) {
|
|---|
| 653 | /* Error in saving bitmap */
|
|---|
| 654 | ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 655 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
|---|
| 656 | return rc;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | /* If block is not free, return */
|
|---|
| 660 | if (!(*free)) {
|
|---|
| 661 | goto terminate;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 665 |
|
|---|
| 666 | /* Update superblock free blocks count */
|
|---|
| 667 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
|---|
| 668 | sb_free_blocks--;
|
|---|
| 669 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
|---|
| 670 |
|
|---|
| 671 | /* Update inode blocks count */
|
|---|
| 672 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
|---|
| 673 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
|---|
| 674 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
|---|
| 675 | inode_ref->dirty = true;
|
|---|
| 676 |
|
|---|
| 677 | /* Update block group free blocks count */
|
|---|
| 678 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
|
|---|
| 679 | bg_ref->block_group, sb);
|
|---|
| 680 | free_blocks--;
|
|---|
| 681 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
|---|
| 682 | sb, free_blocks);
|
|---|
| 683 | bg_ref->dirty = true;
|
|---|
| 684 |
|
|---|
| 685 | terminate:
|
|---|
| 686 |
|
|---|
| 687 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 688 | if (rc != EOK) {
|
|---|
| 689 | EXT4FS_DBG("error in saving bg_ref \%d", rc);
|
|---|
| 690 | return rc;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | return rc;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | /**
|
|---|
| 697 | * @}
|
|---|
| 698 | */
|
|---|