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