[2674db6] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 |
|
---|
[b12ca16] | 33 | /**
|
---|
| 34 | * @file libext4_balloc.c
|
---|
[e63ce679] | 35 | * @brief Physical block allocator.
|
---|
[b12ca16] | 36 | */
|
---|
| 37 |
|
---|
[2674db6] | 38 | #include <errno.h>
|
---|
| 39 | #include <sys/types.h>
|
---|
| 40 | #include "libext4.h"
|
---|
| 41 |
|
---|
[4358513] | 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 | */
|
---|
[b12ca16] | 48 | static uint32_t ext4_balloc_blockaddr2_index_in_group(ext4_superblock_t *sb,
|
---|
| 49 | uint32_t block_addr)
|
---|
[2674db6] | 50 | {
|
---|
[b12ca16] | 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);
|
---|
[2674db6] | 53 |
|
---|
[b12ca16] | 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 |
|
---|
[4358513] | 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 | */
|
---|
[b12ca16] | 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 |
|
---|
[4358513] | 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 | */
|
---|
[b12ca16] | 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);
|
---|
[2674db6] | 93 |
|
---|
| 94 | // First block == 0 or 1
|
---|
| 95 | if (first_block == 0) {
|
---|
[b12ca16] | 96 | return block_addr / blocks_per_group;
|
---|
[2674db6] | 97 | } else {
|
---|
[b12ca16] | 98 | return (block_addr - 1) / blocks_per_group;
|
---|
[2674db6] | 99 | }
|
---|
[b12ca16] | 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[4358513] | 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 | */
|
---|
[1ac1ab4] | 109 | int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
|
---|
[b12ca16] | 110 | {
|
---|
| 111 | int rc;
|
---|
[2674db6] | 112 |
|
---|
[1ac1ab4] | 113 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 114 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 115 |
|
---|
[4358513] | 116 | // Compute indexes
|
---|
[1ac1ab4] | 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);
|
---|
[b12ca16] | 119 |
|
---|
[4358513] | 120 | // Load block group reference
|
---|
[b12ca16] | 121 | ext4_block_group_ref_t *bg_ref;
|
---|
[2674db6] | 122 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 123 | if (rc != EOK) {
|
---|
[b12ca16] | 124 | EXT4FS_DBG("error in loading bg_ref \%d", rc);
|
---|
[2674db6] | 125 | return rc;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[4358513] | 128 | // Load block with bitmap
|
---|
[fe27eb4] | 129 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
[1ac1ab4] | 130 | bg_ref->block_group, sb);
|
---|
[b12ca16] | 131 | block_t *bitmap_block;
|
---|
| 132 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[2674db6] | 133 | if (rc != EOK) {
|
---|
[b12ca16] | 134 | EXT4FS_DBG("error in loading bitmap \%d", rc);
|
---|
[2674db6] | 135 | return rc;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[4358513] | 138 | // Modify bitmap
|
---|
[b12ca16] | 139 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 140 | bitmap_block->dirty = true;
|
---|
[2674db6] | 141 |
|
---|
[4358513] | 142 |
|
---|
| 143 | // Release block with bitmap
|
---|
[b12ca16] | 144 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 145 | if (rc != EOK) {
|
---|
[b12ca16] | 146 | // Error in saving bitmap
|
---|
| 147 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 148 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
---|
[2674db6] | 149 | return rc;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[1ac1ab4] | 152 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[b12ca16] | 153 |
|
---|
[ae3d4f8] | 154 | // Update superblock free blocks count
|
---|
[1ac1ab4] | 155 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
[662bd71] | 156 | sb_free_blocks++;
|
---|
[1ac1ab4] | 157 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[ae3d4f8] | 158 |
|
---|
| 159 | // Update inode blocks count
|
---|
[1ac1ab4] | 160 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[2674db6] | 161 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
[1ac1ab4] | 162 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
[2674db6] | 163 | inode_ref->dirty = true;
|
---|
| 164 |
|
---|
[ae3d4f8] | 165 | // Update block group free blocks count
|
---|
[fe27eb4] | 166 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
|
---|
[1ac1ab4] | 167 | bg_ref->block_group, sb);
|
---|
[2674db6] | 168 | free_blocks++;
|
---|
[fe27eb4] | 169 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
[1ac1ab4] | 170 | sb, free_blocks);
|
---|
[2674db6] | 171 | bg_ref->dirty = true;
|
---|
| 172 |
|
---|
[4358513] | 173 | // Release block group reference
|
---|
[2674db6] | 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 |
|
---|
[4358513] | 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 | */
|
---|
[662bd71] | 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 |
|
---|
[4358513] | 198 | // Compute indexes
|
---|
[662bd71] | 199 | uint32_t block_group_first =
|
---|
| 200 | ext4_balloc_get_bgid_of_block(sb, first);
|
---|
| 201 | uint32_t block_group_last =
|
---|
[3e2952b] | 202 | ext4_balloc_get_bgid_of_block(sb, first + count - 1);
|
---|
[662bd71] | 203 |
|
---|
[3e2952b] | 204 | assert(block_group_first == block_group_last);
|
---|
[662bd71] | 205 |
|
---|
[4358513] | 206 | // Load block group reference
|
---|
[c6a44a3] | 207 | ext4_block_group_ref_t *bg_ref;
|
---|
[662bd71] | 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 |
|
---|
[4358513] | 217 |
|
---|
| 218 | // Load block with bitmap
|
---|
[662bd71] | 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 |
|
---|
[4358513] | 229 | // Modify bitmap
|
---|
[662bd71] | 230 | ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
|
---|
| 231 | bitmap_block->dirty = true;
|
---|
| 232 |
|
---|
[4358513] | 233 | // Release block with bitmap
|
---|
[662bd71] | 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 |
|
---|
[4358513] | 263 | // Release block group reference
|
---|
[662bd71] | 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 |
|
---|
[4358513] | 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 | */
|
---|
[b12ca16] | 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);
|
---|
[fe27eb4] | 284 | uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
|
---|
| 285 | bg, sb);
|
---|
[b12ca16] | 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 |
|
---|
[4358513] | 310 | /** Compute 'goal' for allocation algorithm.
|
---|
| 311 | *
|
---|
| 312 | * @param inode_ref reference to inode, to allocate block for
|
---|
| 313 | * @return goal block number
|
---|
| 314 | */
|
---|
[1ac1ab4] | 315 | static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
|
---|
[2674db6] | 316 | {
|
---|
[b12ca16] | 317 | int rc;
|
---|
[2674db6] | 318 | uint32_t goal = 0;
|
---|
| 319 |
|
---|
[1ac1ab4] | 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);
|
---|
[2674db6] | 324 | uint32_t inode_block_count = inode_size / block_size;
|
---|
| 325 |
|
---|
| 326 | if (inode_size % block_size != 0) {
|
---|
| 327 | inode_block_count++;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[4358513] | 330 | // If inode has some blocks, get last block address + 1
|
---|
[2674db6] | 331 | if (inode_block_count > 0) {
|
---|
| 332 |
|
---|
[95947d24] | 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 | }
|
---|
[b12ca16] | 342 |
|
---|
[95947d24] | 343 | // if goal == 0, sparse file -> continue
|
---|
[2674db6] | 344 | }
|
---|
| 345 |
|
---|
| 346 | // Identify block group of inode
|
---|
[1ac1ab4] | 347 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
[2674db6] | 348 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
[1ac1ab4] | 349 | block_size = ext4_superblock_get_block_size(sb);
|
---|
[2674db6] | 350 |
|
---|
[4358513] | 351 | // Load block group reference
|
---|
[2674db6] | 352 | ext4_block_group_ref_t *bg_ref;
|
---|
[1ac1ab4] | 353 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
|
---|
[2674db6] | 354 | if (rc != EOK) {
|
---|
| 355 | return 0;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[4358513] | 358 | // Compute indexes
|
---|
[1ac1ab4] | 359 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[fe27eb4] | 360 | uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
|
---|
[1ac1ab4] | 361 | bg_ref->block_group, sb);
|
---|
| 362 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
---|
[b12ca16] | 363 | uint32_t inode_table_bytes;
|
---|
| 364 |
|
---|
[4358513] | 365 | // Check for last block group
|
---|
[b12ca16] | 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
|
---|
[1ac1ab4] | 370 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
---|
[b12ca16] | 371 | inode_table_bytes =
|
---|
| 372 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
|
---|
| 373 | * inode_table_item_size;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[2674db6] | 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 |
|
---|
[4358513] | 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 | */
|
---|
[1ac1ab4] | 395 | int ext4_balloc_alloc_block(
|
---|
[412f813] | 396 | ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
[2674db6] | 397 | {
|
---|
| 398 | int rc;
|
---|
[b12ca16] | 399 | uint32_t allocated_block = 0;
|
---|
| 400 |
|
---|
[528e5b3] | 401 | uint32_t bitmap_block_addr;
|
---|
| 402 | block_t *bitmap_block;
|
---|
[2674db6] | 403 | uint32_t rel_block_idx = 0;
|
---|
| 404 |
|
---|
[b12ca16] | 405 | // Find GOAL
|
---|
[1ac1ab4] | 406 | uint32_t goal = ext4_balloc_find_goal(inode_ref);
|
---|
[2674db6] | 407 | if (goal == 0) {
|
---|
[95947d24] | 408 | // no goal found => partition is full
|
---|
[2674db6] | 409 | EXT4FS_DBG("ERRORR (goal == 0)");
|
---|
[b12ca16] | 410 | return ENOSPC;
|
---|
[2674db6] | 411 | }
|
---|
| 412 |
|
---|
[1ac1ab4] | 413 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
| 414 |
|
---|
[b12ca16] | 415 | // Load block group number for goal and relative index
|
---|
[1ac1ab4] | 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);
|
---|
[2674db6] | 418 |
|
---|
| 419 |
|
---|
[4358513] | 420 | // Load block group reference
|
---|
[b12ca16] | 421 | ext4_block_group_ref_t *bg_ref;
|
---|
[1ac1ab4] | 422 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
|
---|
[2674db6] | 423 | if (rc != EOK) {
|
---|
[b12ca16] | 424 | EXT4FS_DBG("initial BG ref not loaded");
|
---|
[2674db6] | 425 | return rc;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[4358513] | 428 | // Compute indexes
|
---|
[b12ca16] | 429 | uint32_t first_in_group =
|
---|
[1ac1ab4] | 430 | ext4_balloc_get_first_data_block_in_group(sb,
|
---|
[b12ca16] | 431 | bg_ref->block_group, block_group);
|
---|
| 432 |
|
---|
[528e5b3] | 433 | uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
|
---|
[1ac1ab4] | 434 | sb, first_in_group);
|
---|
[528e5b3] | 435 |
|
---|
| 436 | if (index_in_group < first_in_group_index) {
|
---|
| 437 | index_in_group = first_in_group_index;
|
---|
[b12ca16] | 438 | }
|
---|
| 439 |
|
---|
[4358513] | 440 | // Load block with bitmap
|
---|
[fe27eb4] | 441 | bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
|
---|
[1ac1ab4] | 442 | sb);
|
---|
[2674db6] | 443 |
|
---|
[1ac1ab4] | 444 | rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
|
---|
[2674db6] | 445 | if (rc != EOK) {
|
---|
| 446 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[b12ca16] | 447 | EXT4FS_DBG("initial bitmap not loaded");
|
---|
[2674db6] | 448 | return rc;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[b12ca16] | 451 | // Check if goal is free
|
---|
[528e5b3] | 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);
|
---|
[2674db6] | 456 | if (rc != EOK) {
|
---|
[95947d24] | 457 | EXT4FS_DBG("goal check: error in saving bitmap \%d", rc);
|
---|
[b12ca16] | 458 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 459 | return rc;
|
---|
[2674db6] | 460 | }
|
---|
| 461 |
|
---|
| 462 | allocated_block = goal;
|
---|
[b12ca16] | 463 | goto success;
|
---|
[2674db6] | 464 |
|
---|
| 465 | }
|
---|
| 466 |
|
---|
[1ac1ab4] | 467 | uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(sb, block_group);
|
---|
[2674db6] | 468 |
|
---|
[b12ca16] | 469 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
| 470 | if (end_idx > blocks_in_group) {
|
---|
| 471 | end_idx = blocks_in_group;
|
---|
| 472 | }
|
---|
[2674db6] | 473 |
|
---|
[b12ca16] | 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) {
|
---|
[528e5b3] | 476 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
[2674db6] | 477 |
|
---|
[528e5b3] | 478 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
| 479 | bitmap_block->dirty = true;
|
---|
| 480 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 481 | if (rc != EOK) {
|
---|
[b12ca16] | 482 | EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
|
---|
[95947d24] | 483 | return rc;
|
---|
[2674db6] | 484 | }
|
---|
| 485 |
|
---|
[b12ca16] | 486 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 487 | sb, tmp_idx, block_group);
|
---|
[b12ca16] | 488 |
|
---|
| 489 | goto success;
|
---|
[2674db6] | 490 | }
|
---|
| 491 |
|
---|
[b12ca16] | 492 | }
|
---|
[2674db6] | 493 |
|
---|
| 494 | // Find free BYTE in bitmap
|
---|
[528e5b3] | 495 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 496 | if (rc == EOK) {
|
---|
[528e5b3] | 497 | bitmap_block->dirty = true;
|
---|
| 498 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 499 | if (rc != EOK) {
|
---|
[b12ca16] | 500 | EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
|
---|
[95947d24] | 501 | return rc;
|
---|
[2674db6] | 502 | }
|
---|
| 503 |
|
---|
[b12ca16] | 504 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 505 | sb, rel_block_idx, block_group);
|
---|
[2674db6] | 506 |
|
---|
[b12ca16] | 507 | goto success;
|
---|
| 508 | }
|
---|
[2674db6] | 509 |
|
---|
| 510 | // Find free bit in bitmap
|
---|
[528e5b3] | 511 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 512 | if (rc == EOK) {
|
---|
[528e5b3] | 513 | bitmap_block->dirty = true;
|
---|
| 514 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 515 | if (rc != EOK) {
|
---|
[b12ca16] | 516 | EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
|
---|
[95947d24] | 517 | return rc;
|
---|
[2674db6] | 518 | }
|
---|
| 519 |
|
---|
[b12ca16] | 520 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 521 | sb, rel_block_idx, block_group);
|
---|
[2674db6] | 522 |
|
---|
[b12ca16] | 523 | goto success;
|
---|
[2674db6] | 524 | }
|
---|
| 525 |
|
---|
[e63ce679] | 526 | // No free block found yet
|
---|
[528e5b3] | 527 | block_put(bitmap_block);
|
---|
[b12ca16] | 528 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 529 |
|
---|
| 530 | // Try other block groups
|
---|
[1ac1ab4] | 531 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[b12ca16] | 532 |
|
---|
| 533 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
| 534 | uint32_t count = block_group_count;
|
---|
| 535 |
|
---|
| 536 | while (count > 0) {
|
---|
[1ac1ab4] | 537 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
|
---|
[b12ca16] | 538 | if (rc != EOK) {
|
---|
[95947d24] | 539 | EXT4FS_DBG("ERROR: unable to load block group \%u", bgid);
|
---|
[b12ca16] | 540 | return rc;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[4358513] | 543 | // Load block with bitmap
|
---|
[fe27eb4] | 544 | bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
[1ac1ab4] | 545 | bg_ref->block_group, sb);
|
---|
[b12ca16] | 546 |
|
---|
[1ac1ab4] | 547 | rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
|
---|
[b12ca16] | 548 | if (rc != EOK) {
|
---|
| 549 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[95947d24] | 550 | EXT4FS_DBG("ERROR: unable to load bitmap block");
|
---|
[b12ca16] | 551 | return rc;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
[4358513] | 554 | // Compute indexes
|
---|
[b12ca16] | 555 | first_in_group = ext4_balloc_get_first_data_block_in_group(
|
---|
[1ac1ab4] | 556 | sb, bg_ref->block_group, bgid);
|
---|
| 557 | index_in_group = ext4_balloc_blockaddr2_index_in_group(sb,
|
---|
[b12ca16] | 558 | first_in_group);
|
---|
[1ac1ab4] | 559 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
---|
[b12ca16] | 560 |
|
---|
[528e5b3] | 561 | first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
|
---|
[1ac1ab4] | 562 | sb, first_in_group);
|
---|
[528e5b3] | 563 |
|
---|
| 564 | if (index_in_group < first_in_group_index) {
|
---|
| 565 | index_in_group = first_in_group_index;
|
---|
[b12ca16] | 566 | }
|
---|
| 567 |
|
---|
[4358513] | 568 | // Try to find free byte in bitmap
|
---|
[528e5b3] | 569 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 570 | if (rc == EOK) {
|
---|
[528e5b3] | 571 | bitmap_block->dirty = true;
|
---|
| 572 | rc = block_put(bitmap_block);
|
---|
[b12ca16] | 573 | if (rc != EOK) {
|
---|
[95947d24] | 574 | EXT4FS_DBG("ERROR: unable to save bitmap block");
|
---|
| 575 | return rc;
|
---|
[b12ca16] | 576 | }
|
---|
| 577 |
|
---|
| 578 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 579 | sb, rel_block_idx, bgid);
|
---|
[b12ca16] | 580 |
|
---|
| 581 | goto success;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[4358513] | 584 | // Try to find free bit in bitmap
|
---|
[528e5b3] | 585 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 586 | if (rc == EOK) {
|
---|
[528e5b3] | 587 | bitmap_block->dirty = true;
|
---|
| 588 | rc = block_put(bitmap_block);
|
---|
[b12ca16] | 589 | if (rc != EOK) {
|
---|
[95947d24] | 590 | EXT4FS_DBG("ERROR: unable to save bitmap block");
|
---|
| 591 | return rc;
|
---|
[b12ca16] | 592 | }
|
---|
| 593 |
|
---|
| 594 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 595 | sb, rel_block_idx, bgid);
|
---|
[b12ca16] | 596 |
|
---|
| 597 | goto success;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[528e5b3] | 600 | block_put(bitmap_block);
|
---|
[b12ca16] | 601 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[4358513] | 602 |
|
---|
| 603 | // Goto next group
|
---|
[b12ca16] | 604 | bgid = (bgid + 1) % block_group_count;
|
---|
| 605 | count--;
|
---|
| 606 | }
|
---|
| 607 |
|
---|
[2674db6] | 608 | return ENOSPC;
|
---|
| 609 |
|
---|
[b12ca16] | 610 | success:
|
---|
[528e5b3] | 611 | ; // Empty command - because of syntax
|
---|
| 612 |
|
---|
[1ac1ab4] | 613 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[b12ca16] | 614 |
|
---|
[ae3d4f8] | 615 | // Update superblock free blocks count
|
---|
[1ac1ab4] | 616 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
[ae3d4f8] | 617 | sb_free_blocks--;
|
---|
[1ac1ab4] | 618 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[2674db6] | 619 |
|
---|
[ae3d4f8] | 620 | // Update inode blocks (different block size!) count
|
---|
[1ac1ab4] | 621 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[2674db6] | 622 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
[1ac1ab4] | 623 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
[2674db6] | 624 | inode_ref->dirty = true;
|
---|
| 625 |
|
---|
[ae3d4f8] | 626 | // Update block group free blocks count
|
---|
[fe27eb4] | 627 | uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
|
---|
[1ac1ab4] | 628 | bg_ref->block_group, sb);
|
---|
[2674db6] | 629 | bg_free_blocks--;
|
---|
[1ac1ab4] | 630 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb, bg_free_blocks);
|
---|
[2674db6] | 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 |
|
---|
[4358513] | 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 | */
|
---|
[82cb6768] | 646 | int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref,
|
---|
| 647 | uint32_t fblock, bool *free)
|
---|
[b73530a] | 648 | {
|
---|
[4358513] | 649 | int rc = EOK;
|
---|
[b73530a] | 650 |
|
---|
| 651 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 652 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 653 |
|
---|
[4358513] | 654 | // Compute indexes
|
---|
[b73530a] | 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 |
|
---|
[4358513] | 658 | // Load block group reference
|
---|
[b73530a] | 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 |
|
---|
[4358513] | 666 | // Load block with bitmap
|
---|
[b73530a] | 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 |
|
---|
[4358513] | 676 | // Check if block is free
|
---|
[82cb6768] | 677 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
---|
[b73530a] | 678 |
|
---|
[4358513] | 679 | // Allocate block if possible
|
---|
[82cb6768] | 680 | if (*free) {
|
---|
[b73530a] | 681 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 682 | bitmap_block->dirty = true;
|
---|
| 683 | }
|
---|
| 684 |
|
---|
[4358513] | 685 | // Release block with bitmap
|
---|
[b73530a] | 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 |
|
---|
[4358513] | 694 | // If block is not free, return
|
---|
[82cb6768] | 695 | if (!(*free)) {
|
---|
[b73530a] | 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 | }
|
---|
[2674db6] | 730 |
|
---|
| 731 | /**
|
---|
| 732 | * @}
|
---|
| 733 | */
|
---|