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