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