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