[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 | */
|
---|
| 263 | r = itable + itable_sz;
|
---|
| 264 | return ext4_filesystem_blockaddr2_index_in_group(sb, r);
|
---|
[b12ca16] | 265 | }
|
---|
[d76973c] | 266 |
|
---|
| 267 | uint64_t bbmap = ext4_block_group_get_block_bitmap(bg_ref->block_group,
|
---|
| 268 | sb);
|
---|
| 269 | uint64_t ibmap = ext4_block_group_get_inode_bitmap(bg_ref->block_group,
|
---|
| 270 | sb);
|
---|
| 271 |
|
---|
[5cc1853] | 272 | r = ext4_filesystem_index_in_group2blockaddr(sb, 0, bg_ref->index);
|
---|
| 273 | r += ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
---|
[d76973c] | 274 |
|
---|
[5cc1853] | 275 | if (ext4_filesystem_blockaddr2group(sb, bbmap) != bg_ref->index)
|
---|
[d76973c] | 276 | bbmap = -1; /* Invalid */
|
---|
| 277 |
|
---|
[5cc1853] | 278 | if (ext4_filesystem_blockaddr2group(sb, ibmap) != bg_ref->index)
|
---|
[d76973c] | 279 | ibmap = -1;
|
---|
| 280 |
|
---|
[76d0981d] | 281 | while (true) {
|
---|
[d76973c] | 282 | if (r == bbmap || r == ibmap)
|
---|
| 283 | r++;
|
---|
[5cc1853] | 284 | else if (r >= itable && r < (itable + itable_sz))
|
---|
| 285 | r = itable + itable_sz;
|
---|
| 286 | else
|
---|
[d76973c] | 287 | break;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | return r;
|
---|
[b12ca16] | 291 | }
|
---|
| 292 |
|
---|
[4358513] | 293 | /** Compute 'goal' for allocation algorithm.
|
---|
| 294 | *
|
---|
[38542dc] | 295 | * @param inode_ref Reference to inode, to allocate block for
|
---|
| 296 | *
|
---|
| 297 | * @return Goal block number
|
---|
| 298 | *
|
---|
[4358513] | 299 | */
|
---|
[b7fd2a0] | 300 | static errno_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref, uint32_t *goal)
|
---|
[2674db6] | 301 | {
|
---|
[2764497] | 302 | *goal = 0;
|
---|
[1ac1ab4] | 303 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
[24df0de6] | 304 |
|
---|
[1ac1ab4] | 305 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
| 306 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[2674db6] | 307 | uint32_t inode_block_count = inode_size / block_size;
|
---|
[24df0de6] | 308 |
|
---|
[38542dc] | 309 | if (inode_size % block_size != 0)
|
---|
[2674db6] | 310 | inode_block_count++;
|
---|
[24df0de6] | 311 |
|
---|
[06d85e5] | 312 | /* If inode has some blocks, get last block address + 1 */
|
---|
[2674db6] | 313 | if (inode_block_count > 0) {
|
---|
[b7fd2a0] | 314 | errno_t rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
|
---|
[2764497] | 315 | inode_block_count - 1, goal);
|
---|
[38542dc] | 316 | if (rc != EOK)
|
---|
[2764497] | 317 | return rc;
|
---|
[24df0de6] | 318 |
|
---|
[95947d24] | 319 | if (goal != 0) {
|
---|
[2764497] | 320 | (*goal)++;
|
---|
| 321 | return EOK;
|
---|
[95947d24] | 322 | }
|
---|
[38542dc] | 323 | /* If goal == 0, sparse file -> continue */
|
---|
[2674db6] | 324 | }
|
---|
[24df0de6] | 325 |
|
---|
[06d85e5] | 326 | /* Identify block group of inode */
|
---|
[1ac1ab4] | 327 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
[2674db6] | 328 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
[24df0de6] | 329 |
|
---|
[06d85e5] | 330 | /* Load block group reference */
|
---|
[2674db6] | 331 | ext4_block_group_ref_t *bg_ref;
|
---|
[b7fd2a0] | 332 | errno_t rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
[38542dc] | 333 | block_group, &bg_ref);
|
---|
| 334 | if (rc != EOK)
|
---|
[2764497] | 335 | return rc;
|
---|
[24df0de6] | 336 |
|
---|
| 337 | *goal = ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
| 338 |
|
---|
[e5a1ace3] | 339 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[2674db6] | 340 | }
|
---|
| 341 |
|
---|
[4358513] | 342 | /** Data block allocation algorithm.
|
---|
| 343 | *
|
---|
[38542dc] | 344 | * @param inode_ref Inode to allocate block for
|
---|
| 345 | * @param fblock Allocated block address
|
---|
| 346 | *
|
---|
| 347 | * @return Error code
|
---|
| 348 | *
|
---|
[4358513] | 349 | */
|
---|
[b7fd2a0] | 350 | errno_t ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
[2674db6] | 351 | {
|
---|
[b12ca16] | 352 | uint32_t allocated_block = 0;
|
---|
[a35b458] | 353 |
|
---|
[528e5b3] | 354 | uint32_t bitmap_block_addr;
|
---|
| 355 | block_t *bitmap_block;
|
---|
[2674db6] | 356 | uint32_t rel_block_idx = 0;
|
---|
[d579acc] | 357 | uint32_t free_blocks;
|
---|
[2764497] | 358 | uint32_t goal;
|
---|
[84239b1] | 359 | uint32_t block_size;
|
---|
[a35b458] | 360 |
|
---|
[06d85e5] | 361 | /* Find GOAL */
|
---|
[b7fd2a0] | 362 | errno_t rc = ext4_balloc_find_goal(inode_ref, &goal);
|
---|
[2764497] | 363 | if (rc != EOK)
|
---|
| 364 | return rc;
|
---|
[d579acc] | 365 |
|
---|
[1ac1ab4] | 366 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
[a35b458] | 367 |
|
---|
[06d85e5] | 368 | /* Load block group number for goal and relative index */
|
---|
[418f21d] | 369 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, goal);
|
---|
[4cdac68] | 370 | uint32_t index_in_group =
|
---|
[38542dc] | 371 | ext4_filesystem_blockaddr2_index_in_group(sb, goal);
|
---|
[a35b458] | 372 |
|
---|
[06d85e5] | 373 | /* Load block group reference */
|
---|
[b12ca16] | 374 | ext4_block_group_ref_t *bg_ref;
|
---|
[2764497] | 375 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
[38542dc] | 376 | block_group, &bg_ref);
|
---|
| 377 | if (rc != EOK)
|
---|
[2674db6] | 378 | return rc;
|
---|
[d579acc] | 379 |
|
---|
| 380 | free_blocks =
|
---|
| 381 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
| 382 | if (free_blocks == 0) {
|
---|
| 383 | /* This group has no free blocks */
|
---|
| 384 | goto goal_failed;
|
---|
| 385 | }
|
---|
[a35b458] | 386 |
|
---|
[06d85e5] | 387 | /* Compute indexes */
|
---|
[b12ca16] | 388 | uint32_t first_in_group =
|
---|
[38542dc] | 389 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
[a35b458] | 390 |
|
---|
[38542dc] | 391 | uint32_t first_in_group_index =
|
---|
| 392 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
[a35b458] | 393 |
|
---|
[38542dc] | 394 | if (index_in_group < first_in_group_index)
|
---|
[528e5b3] | 395 | index_in_group = first_in_group_index;
|
---|
[a35b458] | 396 |
|
---|
[06d85e5] | 397 | /* Load block with bitmap */
|
---|
[38542dc] | 398 | bitmap_block_addr =
|
---|
| 399 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
[a35b458] | 400 |
|
---|
[9aa82e6] | 401 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
[38542dc] | 402 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
[2674db6] | 403 | if (rc != EOK) {
|
---|
| 404 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 405 | return rc;
|
---|
| 406 | }
|
---|
[a35b458] | 407 |
|
---|
[06d85e5] | 408 | /* Check if goal is free */
|
---|
[528e5b3] | 409 | if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
|
---|
| 410 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 411 | bitmap_block->dirty = true;
|
---|
| 412 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 413 | if (rc != EOK) {
|
---|
[b12ca16] | 414 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 415 | return rc;
|
---|
[2674db6] | 416 | }
|
---|
[a35b458] | 417 |
|
---|
[38542dc] | 418 | allocated_block =
|
---|
| 419 | ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
|
---|
| 420 | block_group);
|
---|
[a35b458] | 421 |
|
---|
[b12ca16] | 422 | goto success;
|
---|
[2674db6] | 423 | }
|
---|
[a35b458] | 424 |
|
---|
[38542dc] | 425 | uint32_t blocks_in_group =
|
---|
| 426 | ext4_superblock_get_blocks_in_group(sb, block_group);
|
---|
[a35b458] | 427 |
|
---|
[b12ca16] | 428 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
[38542dc] | 429 | if (end_idx > blocks_in_group)
|
---|
[b12ca16] | 430 | end_idx = blocks_in_group;
|
---|
[a35b458] | 431 |
|
---|
[06d85e5] | 432 | /* Try to find free block near to goal */
|
---|
[38542dc] | 433 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
|
---|
| 434 | ++tmp_idx) {
|
---|
[528e5b3] | 435 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
| 436 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
| 437 | bitmap_block->dirty = true;
|
---|
| 438 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 439 | if (rc != EOK)
|
---|
[95947d24] | 440 | return rc;
|
---|
[a35b458] | 441 |
|
---|
[38542dc] | 442 | allocated_block =
|
---|
| 443 | ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
|
---|
| 444 | block_group);
|
---|
[a35b458] | 445 |
|
---|
[b12ca16] | 446 | goto success;
|
---|
[2674db6] | 447 | }
|
---|
[b12ca16] | 448 | }
|
---|
[a35b458] | 449 |
|
---|
[06d85e5] | 450 | /* Find free BYTE in bitmap */
|
---|
[38542dc] | 451 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
| 452 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 453 | if (rc == EOK) {
|
---|
[528e5b3] | 454 | bitmap_block->dirty = true;
|
---|
| 455 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 456 | if (rc != EOK)
|
---|
[95947d24] | 457 | return rc;
|
---|
[a35b458] | 458 |
|
---|
[38542dc] | 459 | allocated_block =
|
---|
| 460 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 461 | block_group);
|
---|
[a35b458] | 462 |
|
---|
[b12ca16] | 463 | goto success;
|
---|
| 464 | }
|
---|
[a35b458] | 465 |
|
---|
[06d85e5] | 466 | /* Find free bit in bitmap */
|
---|
[38542dc] | 467 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
| 468 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 469 | if (rc == EOK) {
|
---|
[528e5b3] | 470 | bitmap_block->dirty = true;
|
---|
| 471 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 472 | if (rc != EOK)
|
---|
[95947d24] | 473 | return rc;
|
---|
[a35b458] | 474 |
|
---|
[38542dc] | 475 | allocated_block =
|
---|
| 476 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 477 | block_group);
|
---|
[a35b458] | 478 |
|
---|
[b12ca16] | 479 | goto success;
|
---|
[2674db6] | 480 | }
|
---|
[a35b458] | 481 |
|
---|
[06d85e5] | 482 | /* No free block found yet */
|
---|
[e5a1ace3] | 483 | rc = block_put(bitmap_block);
|
---|
| 484 | if (rc != EOK) {
|
---|
| 485 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 486 | return rc;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
[d579acc] | 489 | goal_failed:
|
---|
| 490 |
|
---|
[e5a1ace3] | 491 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 492 | if (rc != EOK)
|
---|
| 493 | return rc;
|
---|
[a35b458] | 494 |
|
---|
[06d85e5] | 495 | /* Try other block groups */
|
---|
[1ac1ab4] | 496 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[a35b458] | 497 |
|
---|
[b12ca16] | 498 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
| 499 | uint32_t count = block_group_count;
|
---|
[a35b458] | 500 |
|
---|
[b12ca16] | 501 | while (count > 0) {
|
---|
[38542dc] | 502 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
|
---|
| 503 | &bg_ref);
|
---|
| 504 | if (rc != EOK)
|
---|
[b12ca16] | 505 | return rc;
|
---|
[d579acc] | 506 |
|
---|
| 507 | free_blocks =
|
---|
[1433ecda] | 508 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[d579acc] | 509 | if (free_blocks == 0) {
|
---|
| 510 | /* This group has no free blocks */
|
---|
| 511 | goto next_group;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[06d85e5] | 514 | /* Load block with bitmap */
|
---|
[38542dc] | 515 | bitmap_block_addr =
|
---|
| 516 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
[a35b458] | 517 |
|
---|
[38542dc] | 518 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
| 519 | bitmap_block_addr, 0);
|
---|
[b12ca16] | 520 | if (rc != EOK) {
|
---|
| 521 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 522 | return rc;
|
---|
| 523 | }
|
---|
[a35b458] | 524 |
|
---|
[06d85e5] | 525 | /* Compute indexes */
|
---|
[38542dc] | 526 | first_in_group =
|
---|
| 527 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
| 528 | index_in_group =
|
---|
| 529 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
[1ac1ab4] | 530 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
---|
[a35b458] | 531 |
|
---|
[38542dc] | 532 | first_in_group_index =
|
---|
| 533 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
[a35b458] | 534 |
|
---|
[38542dc] | 535 | if (index_in_group < first_in_group_index)
|
---|
[528e5b3] | 536 | index_in_group = first_in_group_index;
|
---|
[a35b458] | 537 |
|
---|
[06d85e5] | 538 | /* Try to find free byte in bitmap */
|
---|
[fe61181] | 539 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
[38542dc] | 540 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 541 | if (rc == EOK) {
|
---|
[528e5b3] | 542 | bitmap_block->dirty = true;
|
---|
| 543 | rc = block_put(bitmap_block);
|
---|
[2f591127] | 544 | if (rc != EOK) {
|
---|
| 545 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[95947d24] | 546 | return rc;
|
---|
[2f591127] | 547 | }
|
---|
[a35b458] | 548 |
|
---|
[38542dc] | 549 | allocated_block =
|
---|
| 550 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 551 | bgid);
|
---|
[a35b458] | 552 |
|
---|
[b12ca16] | 553 | goto success;
|
---|
| 554 | }
|
---|
[a35b458] | 555 |
|
---|
[06d85e5] | 556 | /* Try to find free bit in bitmap */
|
---|
[38542dc] | 557 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
| 558 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 559 | if (rc == EOK) {
|
---|
[528e5b3] | 560 | bitmap_block->dirty = true;
|
---|
| 561 | rc = block_put(bitmap_block);
|
---|
[2f591127] | 562 | if (rc != EOK) {
|
---|
| 563 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[95947d24] | 564 | return rc;
|
---|
[2f591127] | 565 | }
|
---|
[a35b458] | 566 |
|
---|
[38542dc] | 567 | allocated_block =
|
---|
| 568 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 569 | bgid);
|
---|
[a35b458] | 570 |
|
---|
[b12ca16] | 571 | goto success;
|
---|
| 572 | }
|
---|
[a35b458] | 573 |
|
---|
[e5a1ace3] | 574 | rc = block_put(bitmap_block);
|
---|
| 575 | if (rc != EOK) {
|
---|
| 576 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 577 | return rc;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[1433ecda] | 580 | next_group:
|
---|
[1f63d9d] | 581 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[e5a1ace3] | 582 | if (rc != EOK)
|
---|
| 583 | return rc;
|
---|
[a35b458] | 584 |
|
---|
[06d85e5] | 585 | /* Goto next group */
|
---|
[b12ca16] | 586 | bgid = (bgid + 1) % block_group_count;
|
---|
| 587 | count--;
|
---|
| 588 | }
|
---|
[a35b458] | 589 |
|
---|
[2674db6] | 590 | return ENOSPC;
|
---|
[a35b458] | 591 |
|
---|
[b12ca16] | 592 | success:
|
---|
[84239b1] | 593 | block_size = ext4_superblock_get_block_size(sb);
|
---|
[a35b458] | 594 |
|
---|
[06d85e5] | 595 | /* Update superblock free blocks count */
|
---|
[1ac1ab4] | 596 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
[ae3d4f8] | 597 | sb_free_blocks--;
|
---|
[1ac1ab4] | 598 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[a35b458] | 599 |
|
---|
[06d85e5] | 600 | /* Update inode blocks (different block size!) count */
|
---|
[38542dc] | 601 | uint64_t ino_blocks =
|
---|
| 602 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[2674db6] | 603 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
[1ac1ab4] | 604 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
[2674db6] | 605 | inode_ref->dirty = true;
|
---|
[a35b458] | 606 |
|
---|
[06d85e5] | 607 | /* Update block group free blocks count */
|
---|
[38542dc] | 608 | uint32_t bg_free_blocks =
|
---|
| 609 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[2674db6] | 610 | bg_free_blocks--;
|
---|
[38542dc] | 611 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
|
---|
| 612 | bg_free_blocks);
|
---|
[2674db6] | 613 | bg_ref->dirty = true;
|
---|
[a35b458] | 614 |
|
---|
[e5a1ace3] | 615 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[a35b458] | 616 |
|
---|
[2674db6] | 617 | *fblock = allocated_block;
|
---|
[e5a1ace3] | 618 | return rc;
|
---|
[2674db6] | 619 | }
|
---|
| 620 |
|
---|
[4358513] | 621 | /** Try to allocate concrete block.
|
---|
| 622 | *
|
---|
[38542dc] | 623 | * @param inode_ref Inode to allocate block for
|
---|
| 624 | * @param fblock Block address to allocate
|
---|
| 625 | * @param free Output value - if target block is free
|
---|
| 626 | *
|
---|
| 627 | * @return Error code
|
---|
| 628 | *
|
---|
[4358513] | 629 | */
|
---|
[b7fd2a0] | 630 | errno_t ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
|
---|
[38542dc] | 631 | bool *free)
|
---|
[b73530a] | 632 | {
|
---|
[b7fd2a0] | 633 | errno_t rc;
|
---|
[a35b458] | 634 |
|
---|
[b73530a] | 635 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 636 | ext4_superblock_t *sb = fs->superblock;
|
---|
[a35b458] | 637 |
|
---|
[06d85e5] | 638 | /* Compute indexes */
|
---|
[418f21d] | 639 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, fblock);
|
---|
[4cdac68] | 640 | uint32_t index_in_group =
|
---|
[38542dc] | 641 | ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
|
---|
[a35b458] | 642 |
|
---|
[06d85e5] | 643 | /* Load block group reference */
|
---|
[b73530a] | 644 | ext4_block_group_ref_t *bg_ref;
|
---|
| 645 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
[38542dc] | 646 | if (rc != EOK)
|
---|
[b73530a] | 647 | return rc;
|
---|
[a35b458] | 648 |
|
---|
[06d85e5] | 649 | /* Load block with bitmap */
|
---|
[38542dc] | 650 | uint32_t bitmap_block_addr =
|
---|
| 651 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
[b73530a] | 652 | block_t *bitmap_block;
|
---|
| 653 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[2f591127] | 654 | if (rc != EOK) {
|
---|
| 655 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[b73530a] | 656 | return rc;
|
---|
[2f591127] | 657 | }
|
---|
[a35b458] | 658 |
|
---|
[06d85e5] | 659 | /* Check if block is free */
|
---|
[82cb6768] | 660 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
---|
[a35b458] | 661 |
|
---|
[06d85e5] | 662 | /* Allocate block if possible */
|
---|
[82cb6768] | 663 | if (*free) {
|
---|
[b73530a] | 664 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 665 | bitmap_block->dirty = true;
|
---|
| 666 | }
|
---|
[a35b458] | 667 |
|
---|
[06d85e5] | 668 | /* Release block with bitmap */
|
---|
[b73530a] | 669 | rc = block_put(bitmap_block);
|
---|
| 670 | if (rc != EOK) {
|
---|
[06d85e5] | 671 | /* Error in saving bitmap */
|
---|
[b73530a] | 672 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 673 | return rc;
|
---|
| 674 | }
|
---|
[a35b458] | 675 |
|
---|
[06d85e5] | 676 | /* If block is not free, return */
|
---|
[38542dc] | 677 | if (!(*free))
|
---|
[b73530a] | 678 | goto terminate;
|
---|
[a35b458] | 679 |
|
---|
[b73530a] | 680 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[a35b458] | 681 |
|
---|
[06d85e5] | 682 | /* Update superblock free blocks count */
|
---|
[b73530a] | 683 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
| 684 | sb_free_blocks--;
|
---|
| 685 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[a35b458] | 686 |
|
---|
[06d85e5] | 687 | /* Update inode blocks count */
|
---|
[38542dc] | 688 | uint64_t ino_blocks =
|
---|
| 689 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[b73530a] | 690 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 691 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
| 692 | inode_ref->dirty = true;
|
---|
[a35b458] | 693 |
|
---|
[06d85e5] | 694 | /* Update block group free blocks count */
|
---|
[38542dc] | 695 | uint32_t free_blocks =
|
---|
| 696 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[b73530a] | 697 | free_blocks--;
|
---|
| 698 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
[38542dc] | 699 | sb, free_blocks);
|
---|
[b73530a] | 700 | bg_ref->dirty = true;
|
---|
[a35b458] | 701 |
|
---|
[b73530a] | 702 | terminate:
|
---|
[38542dc] | 703 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[b73530a] | 704 | }
|
---|
[2674db6] | 705 |
|
---|
| 706 | /**
|
---|
| 707 | * @}
|
---|
[38542dc] | 708 | */
|
---|