[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 | * @{
|
---|
| 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 |
|
---|
[06d85e5] | 54 | /* First block == 0 or 1 */
|
---|
[b12ca16] | 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 |
|
---|
[06d85e5] | 94 | /* First block == 0 or 1 */
|
---|
[2674db6] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 138 | /* Modify bitmap */
|
---|
[b12ca16] | 139 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 140 | bitmap_block->dirty = true;
|
---|
[2674db6] | 141 |
|
---|
[4358513] | 142 |
|
---|
[06d85e5] | 143 | /* Release block with bitmap */
|
---|
[b12ca16] | 144 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 145 | if (rc != EOK) {
|
---|
[06d85e5] | 146 | /* Error in saving bitmap */
|
---|
[b12ca16] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 229 | /* Modify bitmap */
|
---|
[662bd71] | 230 | ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
|
---|
| 231 | bitmap_block->dirty = true;
|
---|
| 232 |
|
---|
[06d85e5] | 233 | /* Release block with bitmap */
|
---|
[662bd71] | 234 | rc = block_put(bitmap_block);
|
---|
| 235 | if (rc != EOK) {
|
---|
[06d85e5] | 236 | /* Error in saving bitmap */
|
---|
[662bd71] | 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 |
|
---|
[06d85e5] | 244 | /* Update superblock free blocks count */
|
---|
[662bd71] | 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 |
|
---|
[06d85e5] | 249 | /* Update inode blocks count */
|
---|
[662bd71] | 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 |
|
---|
[06d85e5] | 255 | /* Update block group free blocks count */
|
---|
[662bd71] | 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 |
|
---|
[06d85e5] | 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(
|
---|
[9aa82e6] | 281 | ext4_superblock_t *sb, ext4_block_group_ref_t *bg_ref)
|
---|
[b12ca16] | 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(
|
---|
[9aa82e6] | 285 | bg_ref->block_group, 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 |
|
---|
[9aa82e6] | 291 | if (bg_ref->index < block_group_count - 1) {
|
---|
[b12ca16] | 292 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
---|
| 293 | } else {
|
---|
[06d85e5] | 294 | /* last block group could be smaller */
|
---|
[b12ca16] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 343 | /* if goal == 0, sparse file -> continue */
|
---|
[2674db6] | 344 | }
|
---|
| 345 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 {
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 405 | /* Find GOAL */
|
---|
[1ac1ab4] | 406 | uint32_t goal = ext4_balloc_find_goal(inode_ref);
|
---|
[2674db6] | 407 | if (goal == 0) {
|
---|
[06d85e5] | 408 | /* no goal found => partition is full */
|
---|
| 409 | EXT4FS_DBG("ERROR (goal == 0)");
|
---|
[b12ca16] | 410 | return ENOSPC;
|
---|
[2674db6] | 411 | }
|
---|
| 412 |
|
---|
[1ac1ab4] | 413 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
| 414 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 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 |
|
---|
[06d85e5] | 428 | /* Compute indexes */
|
---|
[b12ca16] | 429 | uint32_t first_in_group =
|
---|
[9aa82e6] | 430 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
[b12ca16] | 431 |
|
---|
[528e5b3] | 432 | uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
|
---|
[1ac1ab4] | 433 | sb, first_in_group);
|
---|
[528e5b3] | 434 |
|
---|
| 435 | if (index_in_group < first_in_group_index) {
|
---|
| 436 | index_in_group = first_in_group_index;
|
---|
[b12ca16] | 437 | }
|
---|
| 438 |
|
---|
[06d85e5] | 439 | /* Load block with bitmap */
|
---|
[9aa82e6] | 440 | bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
| 441 | bg_ref->block_group, sb);
|
---|
[2674db6] | 442 |
|
---|
[9aa82e6] | 443 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
| 444 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
[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 |
|
---|
[06d85e5] | 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 |
|
---|
[292c843] | 462 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
| 463 | sb, index_in_group, block_group);
|
---|
| 464 |
|
---|
[b12ca16] | 465 | goto success;
|
---|
[2674db6] | 466 |
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[1ac1ab4] | 469 | uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(sb, block_group);
|
---|
[2674db6] | 470 |
|
---|
[b12ca16] | 471 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
| 472 | if (end_idx > blocks_in_group) {
|
---|
| 473 | end_idx = blocks_in_group;
|
---|
| 474 | }
|
---|
[2674db6] | 475 |
|
---|
[06d85e5] | 476 | /* Try to find free block near to goal */
|
---|
[b12ca16] | 477 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) {
|
---|
[528e5b3] | 478 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
[2674db6] | 479 |
|
---|
[528e5b3] | 480 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
| 481 | bitmap_block->dirty = true;
|
---|
| 482 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 483 | if (rc != EOK) {
|
---|
[b12ca16] | 484 | EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
|
---|
[95947d24] | 485 | return rc;
|
---|
[2674db6] | 486 | }
|
---|
| 487 |
|
---|
[b12ca16] | 488 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 489 | sb, tmp_idx, block_group);
|
---|
[b12ca16] | 490 |
|
---|
| 491 | goto success;
|
---|
[2674db6] | 492 | }
|
---|
| 493 |
|
---|
[b12ca16] | 494 | }
|
---|
[2674db6] | 495 |
|
---|
[06d85e5] | 496 | /* Find free BYTE in bitmap */
|
---|
[528e5b3] | 497 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 498 | if (rc == EOK) {
|
---|
[528e5b3] | 499 | bitmap_block->dirty = true;
|
---|
| 500 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 501 | if (rc != EOK) {
|
---|
[b12ca16] | 502 | EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
|
---|
[95947d24] | 503 | return rc;
|
---|
[2674db6] | 504 | }
|
---|
| 505 |
|
---|
[b12ca16] | 506 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 507 | sb, rel_block_idx, block_group);
|
---|
[2674db6] | 508 |
|
---|
[b12ca16] | 509 | goto success;
|
---|
| 510 | }
|
---|
[2674db6] | 511 |
|
---|
[06d85e5] | 512 | /* Find free bit in bitmap */
|
---|
[528e5b3] | 513 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 514 | if (rc == EOK) {
|
---|
[528e5b3] | 515 | bitmap_block->dirty = true;
|
---|
| 516 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 517 | if (rc != EOK) {
|
---|
[b12ca16] | 518 | EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
|
---|
[95947d24] | 519 | return rc;
|
---|
[2674db6] | 520 | }
|
---|
| 521 |
|
---|
[b12ca16] | 522 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 523 | sb, rel_block_idx, block_group);
|
---|
[2674db6] | 524 |
|
---|
[b12ca16] | 525 | goto success;
|
---|
[2674db6] | 526 | }
|
---|
| 527 |
|
---|
[06d85e5] | 528 | /* No free block found yet */
|
---|
[528e5b3] | 529 | block_put(bitmap_block);
|
---|
[b12ca16] | 530 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 531 |
|
---|
[06d85e5] | 532 | /* Try other block groups */
|
---|
[1ac1ab4] | 533 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[b12ca16] | 534 |
|
---|
| 535 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
| 536 | uint32_t count = block_group_count;
|
---|
| 537 |
|
---|
| 538 | while (count > 0) {
|
---|
[1ac1ab4] | 539 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
|
---|
[b12ca16] | 540 | if (rc != EOK) {
|
---|
[95947d24] | 541 | EXT4FS_DBG("ERROR: unable to load block group \%u", bgid);
|
---|
[b12ca16] | 542 | return rc;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[06d85e5] | 545 | /* Load block with bitmap */
|
---|
[fe27eb4] | 546 | bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
[1ac1ab4] | 547 | bg_ref->block_group, sb);
|
---|
[b12ca16] | 548 |
|
---|
[1ac1ab4] | 549 | rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
|
---|
[b12ca16] | 550 | if (rc != EOK) {
|
---|
| 551 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[95947d24] | 552 | EXT4FS_DBG("ERROR: unable to load bitmap block");
|
---|
[b12ca16] | 553 | return rc;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[06d85e5] | 556 | /* Compute indexes */
|
---|
[b12ca16] | 557 | first_in_group = ext4_balloc_get_first_data_block_in_group(
|
---|
[9aa82e6] | 558 | sb, bg_ref);
|
---|
[1ac1ab4] | 559 | index_in_group = ext4_balloc_blockaddr2_index_in_group(sb,
|
---|
[b12ca16] | 560 | first_in_group);
|
---|
[1ac1ab4] | 561 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
---|
[b12ca16] | 562 |
|
---|
[528e5b3] | 563 | first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
|
---|
[1ac1ab4] | 564 | sb, first_in_group);
|
---|
[528e5b3] | 565 |
|
---|
| 566 | if (index_in_group < first_in_group_index) {
|
---|
| 567 | index_in_group = first_in_group_index;
|
---|
[b12ca16] | 568 | }
|
---|
| 569 |
|
---|
[06d85e5] | 570 | /* Try to find free byte in bitmap */
|
---|
[528e5b3] | 571 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 572 | if (rc == EOK) {
|
---|
[528e5b3] | 573 | bitmap_block->dirty = true;
|
---|
| 574 | rc = block_put(bitmap_block);
|
---|
[b12ca16] | 575 | if (rc != EOK) {
|
---|
[95947d24] | 576 | EXT4FS_DBG("ERROR: unable to save bitmap block");
|
---|
| 577 | return rc;
|
---|
[b12ca16] | 578 | }
|
---|
| 579 |
|
---|
| 580 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 581 | sb, rel_block_idx, bgid);
|
---|
[b12ca16] | 582 |
|
---|
| 583 | goto success;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[06d85e5] | 586 | /* Try to find free bit in bitmap */
|
---|
[528e5b3] | 587 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 588 | if (rc == EOK) {
|
---|
[528e5b3] | 589 | bitmap_block->dirty = true;
|
---|
| 590 | rc = block_put(bitmap_block);
|
---|
[b12ca16] | 591 | if (rc != EOK) {
|
---|
[95947d24] | 592 | EXT4FS_DBG("ERROR: unable to save bitmap block");
|
---|
| 593 | return rc;
|
---|
[b12ca16] | 594 | }
|
---|
| 595 |
|
---|
| 596 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
[1ac1ab4] | 597 | sb, rel_block_idx, bgid);
|
---|
[b12ca16] | 598 |
|
---|
| 599 | goto success;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[528e5b3] | 602 | block_put(bitmap_block);
|
---|
[b12ca16] | 603 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[4358513] | 604 |
|
---|
[06d85e5] | 605 | /* Goto next group */
|
---|
[b12ca16] | 606 | bgid = (bgid + 1) % block_group_count;
|
---|
| 607 | count--;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[2674db6] | 610 | return ENOSPC;
|
---|
| 611 |
|
---|
[b12ca16] | 612 | success:
|
---|
[06d85e5] | 613 | ; /* Empty command - because of syntax */
|
---|
[528e5b3] | 614 |
|
---|
[1ac1ab4] | 615 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[b12ca16] | 616 |
|
---|
[06d85e5] | 617 | /* Update superblock free blocks count */
|
---|
[1ac1ab4] | 618 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
[ae3d4f8] | 619 | sb_free_blocks--;
|
---|
[1ac1ab4] | 620 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[2674db6] | 621 |
|
---|
[06d85e5] | 622 | /* Update inode blocks (different block size!) count */
|
---|
[1ac1ab4] | 623 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[2674db6] | 624 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
[1ac1ab4] | 625 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
[2674db6] | 626 | inode_ref->dirty = true;
|
---|
| 627 |
|
---|
[06d85e5] | 628 | /* Update block group free blocks count */
|
---|
[fe27eb4] | 629 | uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
|
---|
[1ac1ab4] | 630 | bg_ref->block_group, sb);
|
---|
[2674db6] | 631 | bg_free_blocks--;
|
---|
[1ac1ab4] | 632 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb, bg_free_blocks);
|
---|
[2674db6] | 633 | bg_ref->dirty = true;
|
---|
| 634 |
|
---|
| 635 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 636 |
|
---|
| 637 | *fblock = allocated_block;
|
---|
| 638 | return EOK;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
[4358513] | 641 | /** Try to allocate concrete block.
|
---|
| 642 | *
|
---|
| 643 | * @param inode_ref inode to allocate block for
|
---|
| 644 | * @param fblock block address to allocate
|
---|
| 645 | * @param free output value - if target block is free
|
---|
| 646 | * @return error code
|
---|
| 647 | */
|
---|
[82cb6768] | 648 | int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref,
|
---|
| 649 | uint32_t fblock, bool *free)
|
---|
[b73530a] | 650 | {
|
---|
[4358513] | 651 | int rc = EOK;
|
---|
[b73530a] | 652 |
|
---|
| 653 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 654 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 655 |
|
---|
[06d85e5] | 656 | /* Compute indexes */
|
---|
[b73530a] | 657 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
|
---|
| 658 | uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, fblock);
|
---|
| 659 |
|
---|
[06d85e5] | 660 | /* Load block group reference */
|
---|
[b73530a] | 661 | ext4_block_group_ref_t *bg_ref;
|
---|
| 662 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 663 | if (rc != EOK) {
|
---|
| 664 | EXT4FS_DBG("error in loading bg_ref \%d", rc);
|
---|
| 665 | return rc;
|
---|
| 666 | }
|
---|
| 667 |
|
---|
[06d85e5] | 668 | /* Load block with bitmap */
|
---|
[b73530a] | 669 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
| 670 | bg_ref->block_group, sb);
|
---|
| 671 | block_t *bitmap_block;
|
---|
| 672 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
| 673 | if (rc != EOK) {
|
---|
| 674 | EXT4FS_DBG("error in loading bitmap \%d", rc);
|
---|
| 675 | return rc;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[06d85e5] | 678 | /* Check if block is free */
|
---|
[82cb6768] | 679 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
---|
[b73530a] | 680 |
|
---|
[06d85e5] | 681 | /* Allocate block if possible */
|
---|
[82cb6768] | 682 | if (*free) {
|
---|
[b73530a] | 683 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 684 | bitmap_block->dirty = true;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[06d85e5] | 687 | /* Release block with bitmap */
|
---|
[b73530a] | 688 | rc = block_put(bitmap_block);
|
---|
| 689 | if (rc != EOK) {
|
---|
[06d85e5] | 690 | /* Error in saving bitmap */
|
---|
[b73530a] | 691 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 692 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
---|
| 693 | return rc;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[06d85e5] | 696 | /* If block is not free, return */
|
---|
[82cb6768] | 697 | if (!(*free)) {
|
---|
[b73530a] | 698 | goto terminate;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 702 |
|
---|
[06d85e5] | 703 | /* Update superblock free blocks count */
|
---|
[b73530a] | 704 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
| 705 | sb_free_blocks--;
|
---|
| 706 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
| 707 |
|
---|
[06d85e5] | 708 | /* Update inode blocks count */
|
---|
[b73530a] | 709 | uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
| 710 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 711 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
| 712 | inode_ref->dirty = true;
|
---|
| 713 |
|
---|
[06d85e5] | 714 | /* Update block group free blocks count */
|
---|
[b73530a] | 715 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
|
---|
| 716 | bg_ref->block_group, sb);
|
---|
| 717 | free_blocks--;
|
---|
| 718 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
| 719 | sb, free_blocks);
|
---|
| 720 | bg_ref->dirty = true;
|
---|
| 721 |
|
---|
| 722 | terminate:
|
---|
| 723 |
|
---|
| 724 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 725 | if (rc != EOK) {
|
---|
| 726 | EXT4FS_DBG("error in saving bg_ref \%d", rc);
|
---|
| 727 | return rc;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | return rc;
|
---|
| 731 | }
|
---|
[2674db6] | 732 |
|
---|
| 733 | /**
|
---|
| 734 | * @}
|
---|
| 735 | */
|
---|