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