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