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