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