[2674db6] | 1 | /*
|
---|
[f22d5ef0] | 2 | * Copyright (c) 2012 Frantisek Princ
|
---|
[2674db6] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext4
|
---|
| 30 | * @{
|
---|
[38542dc] | 31 | */
|
---|
[b12ca16] | 32 | /**
|
---|
[38542dc] | 33 | * @file libext4_balloc.c
|
---|
| 34 | * @brief Physical block allocator.
|
---|
[b12ca16] | 35 | */
|
---|
| 36 |
|
---|
[2674db6] | 37 | #include <errno.h>
|
---|
| 38 | #include <sys/types.h>
|
---|
| 39 | #include "libext4.h"
|
---|
| 40 |
|
---|
[4358513] | 41 | /** Compute number of block group from block address.
|
---|
| 42 | *
|
---|
[38542dc] | 43 | * @param sb Superblock pointer.
|
---|
| 44 | * @param block_addr Absolute address of block.
|
---|
| 45 | *
|
---|
| 46 | * @return Block group index
|
---|
| 47 | *
|
---|
[4358513] | 48 | */
|
---|
[b12ca16] | 49 | static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb,
|
---|
[38542dc] | 50 | uint32_t block_addr)
|
---|
[b12ca16] | 51 | {
|
---|
[38542dc] | 52 | uint32_t blocks_per_group =
|
---|
| 53 | ext4_superblock_get_blocks_per_group(sb);
|
---|
| 54 | uint32_t first_block =
|
---|
| 55 | ext4_superblock_get_first_data_block(sb);
|
---|
| 56 |
|
---|
[06d85e5] | 57 | /* First block == 0 or 1 */
|
---|
[38542dc] | 58 | if (first_block == 0)
|
---|
[b12ca16] | 59 | return block_addr / blocks_per_group;
|
---|
[38542dc] | 60 | else
|
---|
[b12ca16] | 61 | return (block_addr - 1) / blocks_per_group;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[4358513] | 64 | /** Free block.
|
---|
| 65 | *
|
---|
[38542dc] | 66 | * @param inode_ref Inode, where the block is allocated
|
---|
| 67 | * @param block_addr Absolute block address to free
|
---|
| 68 | *
|
---|
| 69 | * @return Error code
|
---|
| 70 | *
|
---|
[4358513] | 71 | */
|
---|
[1ac1ab4] | 72 | int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
|
---|
[b12ca16] | 73 | {
|
---|
[1ac1ab4] | 74 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 75 | ext4_superblock_t *sb = fs->superblock;
|
---|
[38542dc] | 76 |
|
---|
[06d85e5] | 77 | /* Compute indexes */
|
---|
[1ac1ab4] | 78 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
|
---|
[4cdac68] | 79 | uint32_t index_in_group =
|
---|
[38542dc] | 80 | ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
|
---|
| 81 |
|
---|
[06d85e5] | 82 | /* Load block group reference */
|
---|
[b12ca16] | 83 | ext4_block_group_ref_t *bg_ref;
|
---|
[38542dc] | 84 | int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 85 | if (rc != EOK)
|
---|
[2674db6] | 86 | return rc;
|
---|
[38542dc] | 87 |
|
---|
[06d85e5] | 88 | /* Load block with bitmap */
|
---|
[38542dc] | 89 | uint32_t bitmap_block_addr =
|
---|
| 90 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
[b12ca16] | 91 | block_t *bitmap_block;
|
---|
| 92 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[38542dc] | 93 | if (rc != EOK)
|
---|
[2674db6] | 94 | return rc;
|
---|
[38542dc] | 95 |
|
---|
[06d85e5] | 96 | /* Modify bitmap */
|
---|
[b12ca16] | 97 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 98 | bitmap_block->dirty = true;
|
---|
[38542dc] | 99 |
|
---|
[06d85e5] | 100 | /* Release block with bitmap */
|
---|
[b12ca16] | 101 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 102 | if (rc != EOK) {
|
---|
[06d85e5] | 103 | /* Error in saving bitmap */
|
---|
[b12ca16] | 104 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[2674db6] | 105 | return rc;
|
---|
| 106 | }
|
---|
[38542dc] | 107 |
|
---|
[1ac1ab4] | 108 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[38542dc] | 109 |
|
---|
[06d85e5] | 110 | /* Update superblock free blocks count */
|
---|
[38542dc] | 111 | uint32_t sb_free_blocks =
|
---|
| 112 | ext4_superblock_get_free_blocks_count(sb);
|
---|
[662bd71] | 113 | sb_free_blocks++;
|
---|
[1ac1ab4] | 114 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[38542dc] | 115 |
|
---|
[06d85e5] | 116 | /* Update inode blocks count */
|
---|
[38542dc] | 117 | uint64_t ino_blocks =
|
---|
| 118 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[2674db6] | 119 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
[1ac1ab4] | 120 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
[2674db6] | 121 | inode_ref->dirty = true;
|
---|
[38542dc] | 122 |
|
---|
[06d85e5] | 123 | /* Update block group free blocks count */
|
---|
[38542dc] | 124 | uint32_t free_blocks =
|
---|
| 125 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[2674db6] | 126 | free_blocks++;
|
---|
[fe27eb4] | 127 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
[38542dc] | 128 | sb, free_blocks);
|
---|
[2674db6] | 129 | bg_ref->dirty = true;
|
---|
[38542dc] | 130 |
|
---|
[06d85e5] | 131 | /* Release block group reference */
|
---|
[2674db6] | 132 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 133 | if (rc != EOK)
|
---|
[2674db6] | 134 | return rc;
|
---|
[38542dc] | 135 |
|
---|
[2674db6] | 136 | return EOK;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[4358513] | 139 | /** Free continuous set of blocks.
|
---|
| 140 | *
|
---|
[38542dc] | 141 | * @param inode_ref Inode, where the blocks are allocated
|
---|
| 142 | * @param first First block to release
|
---|
| 143 | * @param count Number of blocks to release
|
---|
| 144 | *
|
---|
[4358513] | 145 | */
|
---|
[662bd71] | 146 | int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 147 | uint32_t first, uint32_t count)
|
---|
[662bd71] | 148 | {
|
---|
| 149 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 150 | ext4_superblock_t *sb = fs->superblock;
|
---|
[38542dc] | 151 |
|
---|
[06d85e5] | 152 | /* Compute indexes */
|
---|
[662bd71] | 153 | uint32_t block_group_first =
|
---|
[38542dc] | 154 | ext4_balloc_get_bgid_of_block(sb, first);
|
---|
[662bd71] | 155 | uint32_t block_group_last =
|
---|
[38542dc] | 156 | ext4_balloc_get_bgid_of_block(sb, first + count - 1);
|
---|
| 157 |
|
---|
[3e2952b] | 158 | assert(block_group_first == block_group_last);
|
---|
[38542dc] | 159 |
|
---|
[06d85e5] | 160 | /* Load block group reference */
|
---|
[c6a44a3] | 161 | ext4_block_group_ref_t *bg_ref;
|
---|
[38542dc] | 162 | int rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
|
---|
| 163 | if (rc != EOK)
|
---|
[662bd71] | 164 | return rc;
|
---|
[38542dc] | 165 |
|
---|
[662bd71] | 166 | uint32_t index_in_group_first =
|
---|
[38542dc] | 167 | ext4_filesystem_blockaddr2_index_in_group(sb, first);
|
---|
| 168 |
|
---|
[06d85e5] | 169 | /* Load block with bitmap */
|
---|
[38542dc] | 170 | uint32_t bitmap_block_addr =
|
---|
| 171 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
| 172 |
|
---|
[662bd71] | 173 | block_t *bitmap_block;
|
---|
| 174 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[38542dc] | 175 | if (rc != EOK)
|
---|
[662bd71] | 176 | return rc;
|
---|
[38542dc] | 177 |
|
---|
[06d85e5] | 178 | /* Modify bitmap */
|
---|
[662bd71] | 179 | ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
|
---|
| 180 | bitmap_block->dirty = true;
|
---|
[38542dc] | 181 |
|
---|
[06d85e5] | 182 | /* Release block with bitmap */
|
---|
[662bd71] | 183 | rc = block_put(bitmap_block);
|
---|
| 184 | if (rc != EOK) {
|
---|
[06d85e5] | 185 | /* Error in saving bitmap */
|
---|
[662bd71] | 186 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 187 | return rc;
|
---|
| 188 | }
|
---|
[38542dc] | 189 |
|
---|
[662bd71] | 190 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[38542dc] | 191 |
|
---|
[06d85e5] | 192 | /* Update superblock free blocks count */
|
---|
[38542dc] | 193 | uint32_t sb_free_blocks =
|
---|
| 194 | ext4_superblock_get_free_blocks_count(sb);
|
---|
[662bd71] | 195 | sb_free_blocks += count;
|
---|
| 196 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[38542dc] | 197 |
|
---|
[06d85e5] | 198 | /* Update inode blocks count */
|
---|
[38542dc] | 199 | uint64_t ino_blocks =
|
---|
| 200 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[662bd71] | 201 | ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
|
---|
| 202 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
| 203 | inode_ref->dirty = true;
|
---|
[38542dc] | 204 |
|
---|
[06d85e5] | 205 | /* Update block group free blocks count */
|
---|
[38542dc] | 206 | uint32_t free_blocks =
|
---|
| 207 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[662bd71] | 208 | free_blocks += count;
|
---|
| 209 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
[38542dc] | 210 | sb, free_blocks);
|
---|
[662bd71] | 211 | bg_ref->dirty = true;
|
---|
[38542dc] | 212 |
|
---|
[06d85e5] | 213 | /* Release block group reference */
|
---|
[662bd71] | 214 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 215 | if (rc != EOK)
|
---|
[662bd71] | 216 | return rc;
|
---|
[38542dc] | 217 |
|
---|
[662bd71] | 218 | return EOK;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[4358513] | 221 | /** Compute first block for data in block group.
|
---|
| 222 | *
|
---|
[38542dc] | 223 | * @param sb Pointer to superblock
|
---|
| 224 | * @param bg Pointer to block group
|
---|
| 225 | * @param bgid Index of block group
|
---|
| 226 | *
|
---|
| 227 | * @return Absolute block index of first block
|
---|
| 228 | *
|
---|
[4358513] | 229 | */
|
---|
[38542dc] | 230 | uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
|
---|
| 231 | ext4_block_group_ref_t *bg_ref)
|
---|
[b12ca16] | 232 | {
|
---|
| 233 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[38542dc] | 234 | uint32_t inode_table_first_block =
|
---|
| 235 | ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
|
---|
[b12ca16] | 236 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
---|
| 237 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 238 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 239 | uint32_t inode_table_bytes;
|
---|
[38542dc] | 240 |
|
---|
[9aa82e6] | 241 | if (bg_ref->index < block_group_count - 1) {
|
---|
[b12ca16] | 242 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
---|
| 243 | } else {
|
---|
[38542dc] | 244 | /* Last block group could be smaller */
|
---|
[b12ca16] | 245 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
---|
| 246 | inode_table_bytes =
|
---|
[38542dc] | 247 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
|
---|
| 248 | inode_table_item_size;
|
---|
[b12ca16] | 249 | }
|
---|
[38542dc] | 250 |
|
---|
[b12ca16] | 251 | uint32_t inode_table_blocks = inode_table_bytes / block_size;
|
---|
[38542dc] | 252 |
|
---|
| 253 | if (inode_table_bytes % block_size)
|
---|
[b12ca16] | 254 | inode_table_blocks++;
|
---|
[38542dc] | 255 |
|
---|
[b12ca16] | 256 | return inode_table_first_block + inode_table_blocks;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[4358513] | 259 | /** Compute 'goal' for allocation algorithm.
|
---|
| 260 | *
|
---|
[38542dc] | 261 | * @param inode_ref Reference to inode, to allocate block for
|
---|
| 262 | *
|
---|
| 263 | * @return Goal block number
|
---|
| 264 | *
|
---|
[4358513] | 265 | */
|
---|
[1ac1ab4] | 266 | static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
|
---|
[2674db6] | 267 | {
|
---|
| 268 | uint32_t goal = 0;
|
---|
[38542dc] | 269 |
|
---|
[1ac1ab4] | 270 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
[38542dc] | 271 |
|
---|
[1ac1ab4] | 272 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
| 273 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[2674db6] | 274 | uint32_t inode_block_count = inode_size / block_size;
|
---|
[38542dc] | 275 |
|
---|
| 276 | if (inode_size % block_size != 0)
|
---|
[2674db6] | 277 | inode_block_count++;
|
---|
[38542dc] | 278 |
|
---|
[06d85e5] | 279 | /* If inode has some blocks, get last block address + 1 */
|
---|
[2674db6] | 280 | if (inode_block_count > 0) {
|
---|
[38542dc] | 281 | int rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
|
---|
| 282 | inode_block_count - 1, &goal);
|
---|
| 283 | if (rc != EOK)
|
---|
[95947d24] | 284 | return 0;
|
---|
[38542dc] | 285 |
|
---|
[95947d24] | 286 | if (goal != 0) {
|
---|
| 287 | goal++;
|
---|
| 288 | return goal;
|
---|
| 289 | }
|
---|
[38542dc] | 290 |
|
---|
| 291 | /* If goal == 0, sparse file -> continue */
|
---|
[2674db6] | 292 | }
|
---|
[38542dc] | 293 |
|
---|
[06d85e5] | 294 | /* Identify block group of inode */
|
---|
[1ac1ab4] | 295 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
[2674db6] | 296 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
[1ac1ab4] | 297 | block_size = ext4_superblock_get_block_size(sb);
|
---|
[38542dc] | 298 |
|
---|
[06d85e5] | 299 | /* Load block group reference */
|
---|
[2674db6] | 300 | ext4_block_group_ref_t *bg_ref;
|
---|
[38542dc] | 301 | int rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
| 302 | block_group, &bg_ref);
|
---|
| 303 | if (rc != EOK)
|
---|
[2674db6] | 304 | return 0;
|
---|
[38542dc] | 305 |
|
---|
[06d85e5] | 306 | /* Compute indexes */
|
---|
[1ac1ab4] | 307 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[38542dc] | 308 | uint32_t inode_table_first_block =
|
---|
| 309 | ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
|
---|
[1ac1ab4] | 310 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
---|
[b12ca16] | 311 | uint32_t inode_table_bytes;
|
---|
[38542dc] | 312 |
|
---|
[06d85e5] | 313 | /* Check for last block group */
|
---|
[b12ca16] | 314 | if (block_group < block_group_count - 1) {
|
---|
| 315 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
---|
| 316 | } else {
|
---|
[38542dc] | 317 | /* Last block group could be smaller */
|
---|
[1ac1ab4] | 318 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
---|
[b12ca16] | 319 | inode_table_bytes =
|
---|
[38542dc] | 320 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
|
---|
| 321 | inode_table_item_size;
|
---|
[b12ca16] | 322 | }
|
---|
[38542dc] | 323 |
|
---|
[2674db6] | 324 | uint32_t inode_table_blocks = inode_table_bytes / block_size;
|
---|
[38542dc] | 325 |
|
---|
| 326 | if (inode_table_bytes % block_size)
|
---|
[2674db6] | 327 | inode_table_blocks++;
|
---|
[38542dc] | 328 |
|
---|
[2674db6] | 329 | goal = inode_table_first_block + inode_table_blocks;
|
---|
[38542dc] | 330 |
|
---|
[2674db6] | 331 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 332 |
|
---|
[2674db6] | 333 | return goal;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[4358513] | 336 | /** Data block allocation algorithm.
|
---|
| 337 | *
|
---|
[38542dc] | 338 | * @param inode_ref Inode to allocate block for
|
---|
| 339 | * @param fblock Allocated block address
|
---|
| 340 | *
|
---|
| 341 | * @return Error code
|
---|
| 342 | *
|
---|
[4358513] | 343 | */
|
---|
[38542dc] | 344 | int ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
[2674db6] | 345 | {
|
---|
[b12ca16] | 346 | uint32_t allocated_block = 0;
|
---|
[38542dc] | 347 |
|
---|
[528e5b3] | 348 | uint32_t bitmap_block_addr;
|
---|
| 349 | block_t *bitmap_block;
|
---|
[2674db6] | 350 | uint32_t rel_block_idx = 0;
|
---|
[38542dc] | 351 |
|
---|
[06d85e5] | 352 | /* Find GOAL */
|
---|
[1ac1ab4] | 353 | uint32_t goal = ext4_balloc_find_goal(inode_ref);
|
---|
[2674db6] | 354 | if (goal == 0) {
|
---|
[06d85e5] | 355 | /* no goal found => partition is full */
|
---|
[b12ca16] | 356 | return ENOSPC;
|
---|
[2674db6] | 357 | }
|
---|
[38542dc] | 358 |
|
---|
[1ac1ab4] | 359 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
[38542dc] | 360 |
|
---|
[06d85e5] | 361 | /* Load block group number for goal and relative index */
|
---|
[1ac1ab4] | 362 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
|
---|
[4cdac68] | 363 | uint32_t index_in_group =
|
---|
[38542dc] | 364 | ext4_filesystem_blockaddr2_index_in_group(sb, goal);
|
---|
| 365 |
|
---|
[06d85e5] | 366 | /* Load block group reference */
|
---|
[b12ca16] | 367 | ext4_block_group_ref_t *bg_ref;
|
---|
[38542dc] | 368 | int rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
| 369 | block_group, &bg_ref);
|
---|
| 370 | if (rc != EOK)
|
---|
[2674db6] | 371 | return rc;
|
---|
[38542dc] | 372 |
|
---|
[06d85e5] | 373 | /* Compute indexes */
|
---|
[b12ca16] | 374 | uint32_t first_in_group =
|
---|
[38542dc] | 375 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
| 376 |
|
---|
| 377 | uint32_t first_in_group_index =
|
---|
| 378 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
| 379 |
|
---|
| 380 | if (index_in_group < first_in_group_index)
|
---|
[528e5b3] | 381 | index_in_group = first_in_group_index;
|
---|
[38542dc] | 382 |
|
---|
[06d85e5] | 383 | /* Load block with bitmap */
|
---|
[38542dc] | 384 | bitmap_block_addr =
|
---|
| 385 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
| 386 |
|
---|
[9aa82e6] | 387 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
[38542dc] | 388 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
[2674db6] | 389 | if (rc != EOK) {
|
---|
| 390 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 391 | return rc;
|
---|
| 392 | }
|
---|
[38542dc] | 393 |
|
---|
[06d85e5] | 394 | /* Check if goal is free */
|
---|
[528e5b3] | 395 | if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
|
---|
| 396 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 397 | bitmap_block->dirty = true;
|
---|
| 398 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 399 | if (rc != EOK) {
|
---|
[b12ca16] | 400 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 401 | return rc;
|
---|
[2674db6] | 402 | }
|
---|
[38542dc] | 403 |
|
---|
| 404 | allocated_block =
|
---|
| 405 | ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
|
---|
| 406 | block_group);
|
---|
| 407 |
|
---|
[b12ca16] | 408 | goto success;
|
---|
[2674db6] | 409 | }
|
---|
[38542dc] | 410 |
|
---|
| 411 | uint32_t blocks_in_group =
|
---|
| 412 | ext4_superblock_get_blocks_in_group(sb, block_group);
|
---|
| 413 |
|
---|
[b12ca16] | 414 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
[38542dc] | 415 | if (end_idx > blocks_in_group)
|
---|
[b12ca16] | 416 | end_idx = blocks_in_group;
|
---|
[38542dc] | 417 |
|
---|
[06d85e5] | 418 | /* Try to find free block near to goal */
|
---|
[38542dc] | 419 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
|
---|
| 420 | ++tmp_idx) {
|
---|
[528e5b3] | 421 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
| 422 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
| 423 | bitmap_block->dirty = true;
|
---|
| 424 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 425 | if (rc != EOK)
|
---|
[95947d24] | 426 | return rc;
|
---|
[38542dc] | 427 |
|
---|
| 428 | allocated_block =
|
---|
| 429 | ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
|
---|
| 430 | block_group);
|
---|
| 431 |
|
---|
[b12ca16] | 432 | goto success;
|
---|
[2674db6] | 433 | }
|
---|
[b12ca16] | 434 | }
|
---|
[38542dc] | 435 |
|
---|
[06d85e5] | 436 | /* Find free BYTE in bitmap */
|
---|
[38542dc] | 437 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
| 438 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 439 | if (rc == EOK) {
|
---|
[528e5b3] | 440 | bitmap_block->dirty = true;
|
---|
| 441 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 442 | if (rc != EOK)
|
---|
[95947d24] | 443 | return rc;
|
---|
[38542dc] | 444 |
|
---|
| 445 | allocated_block =
|
---|
| 446 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 447 | block_group);
|
---|
| 448 |
|
---|
[b12ca16] | 449 | goto success;
|
---|
| 450 | }
|
---|
[38542dc] | 451 |
|
---|
[06d85e5] | 452 | /* Find free bit in bitmap */
|
---|
[38542dc] | 453 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
| 454 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 455 | if (rc == EOK) {
|
---|
[528e5b3] | 456 | bitmap_block->dirty = true;
|
---|
| 457 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 458 | if (rc != EOK)
|
---|
[95947d24] | 459 | return rc;
|
---|
[38542dc] | 460 |
|
---|
| 461 | allocated_block =
|
---|
| 462 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 463 | block_group);
|
---|
| 464 |
|
---|
[b12ca16] | 465 | goto success;
|
---|
[2674db6] | 466 | }
|
---|
[38542dc] | 467 |
|
---|
[06d85e5] | 468 | /* No free block found yet */
|
---|
[528e5b3] | 469 | block_put(bitmap_block);
|
---|
[b12ca16] | 470 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 471 |
|
---|
[06d85e5] | 472 | /* Try other block groups */
|
---|
[1ac1ab4] | 473 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[38542dc] | 474 |
|
---|
[b12ca16] | 475 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
| 476 | uint32_t count = block_group_count;
|
---|
[38542dc] | 477 |
|
---|
[b12ca16] | 478 | while (count > 0) {
|
---|
[38542dc] | 479 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
|
---|
| 480 | &bg_ref);
|
---|
| 481 | if (rc != EOK)
|
---|
[b12ca16] | 482 | return rc;
|
---|
[38542dc] | 483 |
|
---|
[06d85e5] | 484 | /* Load block with bitmap */
|
---|
[38542dc] | 485 | bitmap_block_addr =
|
---|
| 486 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
| 487 |
|
---|
| 488 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
| 489 | bitmap_block_addr, 0);
|
---|
[b12ca16] | 490 | if (rc != EOK) {
|
---|
| 491 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 492 | return rc;
|
---|
| 493 | }
|
---|
[38542dc] | 494 |
|
---|
[06d85e5] | 495 | /* Compute indexes */
|
---|
[38542dc] | 496 | first_in_group =
|
---|
| 497 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
| 498 | index_in_group =
|
---|
| 499 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
[1ac1ab4] | 500 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
---|
[38542dc] | 501 |
|
---|
| 502 | first_in_group_index =
|
---|
| 503 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
| 504 |
|
---|
| 505 | if (index_in_group < first_in_group_index)
|
---|
[528e5b3] | 506 | index_in_group = first_in_group_index;
|
---|
[38542dc] | 507 |
|
---|
[06d85e5] | 508 | /* Try to find free byte in bitmap */
|
---|
[fe61181] | 509 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
[38542dc] | 510 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 511 | if (rc == EOK) {
|
---|
[528e5b3] | 512 | bitmap_block->dirty = true;
|
---|
| 513 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 514 | if (rc != EOK)
|
---|
[95947d24] | 515 | return rc;
|
---|
[38542dc] | 516 |
|
---|
| 517 | allocated_block =
|
---|
| 518 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 519 | bgid);
|
---|
| 520 |
|
---|
[b12ca16] | 521 | goto success;
|
---|
| 522 | }
|
---|
[38542dc] | 523 |
|
---|
[06d85e5] | 524 | /* Try to find free bit in bitmap */
|
---|
[38542dc] | 525 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
| 526 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 527 | if (rc == EOK) {
|
---|
[528e5b3] | 528 | bitmap_block->dirty = true;
|
---|
| 529 | rc = block_put(bitmap_block);
|
---|
[38542dc] | 530 | if (rc != EOK)
|
---|
[95947d24] | 531 | return rc;
|
---|
[38542dc] | 532 |
|
---|
| 533 | allocated_block =
|
---|
| 534 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
| 535 | bgid);
|
---|
| 536 |
|
---|
[b12ca16] | 537 | goto success;
|
---|
| 538 | }
|
---|
[38542dc] | 539 |
|
---|
[528e5b3] | 540 | block_put(bitmap_block);
|
---|
[b12ca16] | 541 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 542 |
|
---|
[06d85e5] | 543 | /* Goto next group */
|
---|
[b12ca16] | 544 | bgid = (bgid + 1) % block_group_count;
|
---|
| 545 | count--;
|
---|
| 546 | }
|
---|
[38542dc] | 547 |
|
---|
[2674db6] | 548 | return ENOSPC;
|
---|
[38542dc] | 549 |
|
---|
[b12ca16] | 550 | success:
|
---|
[38542dc] | 551 | /* Empty command - because of syntax */
|
---|
| 552 | ;
|
---|
[528e5b3] | 553 |
|
---|
[1ac1ab4] | 554 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[38542dc] | 555 |
|
---|
[06d85e5] | 556 | /* Update superblock free blocks count */
|
---|
[1ac1ab4] | 557 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
[ae3d4f8] | 558 | sb_free_blocks--;
|
---|
[1ac1ab4] | 559 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[38542dc] | 560 |
|
---|
[06d85e5] | 561 | /* Update inode blocks (different block size!) count */
|
---|
[38542dc] | 562 | uint64_t ino_blocks =
|
---|
| 563 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[2674db6] | 564 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
[1ac1ab4] | 565 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
[2674db6] | 566 | inode_ref->dirty = true;
|
---|
[38542dc] | 567 |
|
---|
[06d85e5] | 568 | /* Update block group free blocks count */
|
---|
[38542dc] | 569 | uint32_t bg_free_blocks =
|
---|
| 570 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[2674db6] | 571 | bg_free_blocks--;
|
---|
[38542dc] | 572 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
|
---|
| 573 | bg_free_blocks);
|
---|
[2674db6] | 574 | bg_ref->dirty = true;
|
---|
[38542dc] | 575 |
|
---|
[2674db6] | 576 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 577 |
|
---|
[2674db6] | 578 | *fblock = allocated_block;
|
---|
| 579 | return EOK;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[4358513] | 582 | /** Try to allocate concrete block.
|
---|
| 583 | *
|
---|
[38542dc] | 584 | * @param inode_ref Inode to allocate block for
|
---|
| 585 | * @param fblock Block address to allocate
|
---|
| 586 | * @param free Output value - if target block is free
|
---|
| 587 | *
|
---|
| 588 | * @return Error code
|
---|
| 589 | *
|
---|
[4358513] | 590 | */
|
---|
[38542dc] | 591 | int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
|
---|
| 592 | bool *free)
|
---|
[b73530a] | 593 | {
|
---|
[4358513] | 594 | int rc = EOK;
|
---|
[38542dc] | 595 |
|
---|
[b73530a] | 596 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 597 | ext4_superblock_t *sb = fs->superblock;
|
---|
[38542dc] | 598 |
|
---|
[06d85e5] | 599 | /* Compute indexes */
|
---|
[b73530a] | 600 | uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
|
---|
[4cdac68] | 601 | uint32_t index_in_group =
|
---|
[38542dc] | 602 | ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
|
---|
| 603 |
|
---|
[06d85e5] | 604 | /* Load block group reference */
|
---|
[b73530a] | 605 | ext4_block_group_ref_t *bg_ref;
|
---|
| 606 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
[38542dc] | 607 | if (rc != EOK)
|
---|
[b73530a] | 608 | return rc;
|
---|
[38542dc] | 609 |
|
---|
[06d85e5] | 610 | /* Load block with bitmap */
|
---|
[38542dc] | 611 | uint32_t bitmap_block_addr =
|
---|
| 612 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
[b73530a] | 613 | block_t *bitmap_block;
|
---|
| 614 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[38542dc] | 615 | if (rc != EOK)
|
---|
[b73530a] | 616 | return rc;
|
---|
[38542dc] | 617 |
|
---|
[06d85e5] | 618 | /* Check if block is free */
|
---|
[82cb6768] | 619 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
---|
[38542dc] | 620 |
|
---|
[06d85e5] | 621 | /* Allocate block if possible */
|
---|
[82cb6768] | 622 | if (*free) {
|
---|
[b73530a] | 623 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 624 | bitmap_block->dirty = true;
|
---|
| 625 | }
|
---|
[38542dc] | 626 |
|
---|
[06d85e5] | 627 | /* Release block with bitmap */
|
---|
[b73530a] | 628 | rc = block_put(bitmap_block);
|
---|
| 629 | if (rc != EOK) {
|
---|
[06d85e5] | 630 | /* Error in saving bitmap */
|
---|
[b73530a] | 631 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 632 | return rc;
|
---|
| 633 | }
|
---|
[38542dc] | 634 |
|
---|
[06d85e5] | 635 | /* If block is not free, return */
|
---|
[38542dc] | 636 | if (!(*free))
|
---|
[b73530a] | 637 | goto terminate;
|
---|
[38542dc] | 638 |
|
---|
[b73530a] | 639 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[38542dc] | 640 |
|
---|
[06d85e5] | 641 | /* Update superblock free blocks count */
|
---|
[b73530a] | 642 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
| 643 | sb_free_blocks--;
|
---|
| 644 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
[38542dc] | 645 |
|
---|
[06d85e5] | 646 | /* Update inode blocks count */
|
---|
[38542dc] | 647 | uint64_t ino_blocks =
|
---|
| 648 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
[b73530a] | 649 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 650 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
| 651 | inode_ref->dirty = true;
|
---|
[38542dc] | 652 |
|
---|
[06d85e5] | 653 | /* Update block group free blocks count */
|
---|
[38542dc] | 654 | uint32_t free_blocks =
|
---|
| 655 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
[b73530a] | 656 | free_blocks--;
|
---|
| 657 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
[38542dc] | 658 | sb, free_blocks);
|
---|
[b73530a] | 659 | bg_ref->dirty = true;
|
---|
[38542dc] | 660 |
|
---|
[b73530a] | 661 | terminate:
|
---|
[38542dc] | 662 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[b73530a] | 663 | }
|
---|
[2674db6] | 664 |
|
---|
| 665 | /**
|
---|
| 666 | * @}
|
---|
[38542dc] | 667 | */
|
---|