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