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