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