[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 |
|
---|
| 84 | int ext4_balloc_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_addr)
|
---|
| 85 | {
|
---|
| 86 | int rc;
|
---|
[2674db6] | 87 |
|
---|
[b12ca16] | 88 | uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, block_addr);
|
---|
| 89 | uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, block_addr);
|
---|
| 90 |
|
---|
| 91 | ext4_block_group_ref_t *bg_ref;
|
---|
[2674db6] | 92 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 93 | if (rc != EOK) {
|
---|
[b12ca16] | 94 | EXT4FS_DBG("error in loading bg_ref \%d", rc);
|
---|
[2674db6] | 95 | return rc;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[fe27eb4] | 98 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
| 99 | bg_ref->block_group, fs->superblock);
|
---|
[b12ca16] | 100 | block_t *bitmap_block;
|
---|
| 101 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[2674db6] | 102 | if (rc != EOK) {
|
---|
[b12ca16] | 103 | EXT4FS_DBG("error in loading bitmap \%d", rc);
|
---|
[2674db6] | 104 | return rc;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[b12ca16] | 107 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 108 | bitmap_block->dirty = true;
|
---|
[2674db6] | 109 |
|
---|
[b12ca16] | 110 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 111 | if (rc != EOK) {
|
---|
[b12ca16] | 112 | // Error in saving bitmap
|
---|
| 113 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 114 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
---|
[2674db6] | 115 | return rc;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[b12ca16] | 118 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 119 |
|
---|
[ae3d4f8] | 120 | // Update superblock free blocks count
|
---|
| 121 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
|
---|
| 122 | sb_free_blocks--;
|
---|
| 123 | ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
|
---|
| 124 |
|
---|
| 125 | // Update inode blocks count
|
---|
[2674db6] | 126 | uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
|
---|
| 127 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 128 | ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
|
---|
| 129 | inode_ref->dirty = true;
|
---|
| 130 |
|
---|
[ae3d4f8] | 131 | // Update block group free blocks count
|
---|
[fe27eb4] | 132 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
|
---|
| 133 | bg_ref->block_group, fs->superblock);
|
---|
[2674db6] | 134 | free_blocks++;
|
---|
[fe27eb4] | 135 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
| 136 | fs->superblock, free_blocks);
|
---|
[2674db6] | 137 | bg_ref->dirty = true;
|
---|
| 138 |
|
---|
| 139 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 140 | if (rc != EOK) {
|
---|
| 141 | EXT4FS_DBG("error in saving bg_ref \%d", rc);
|
---|
| 142 | // TODO error
|
---|
| 143 | return rc;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return EOK;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[b12ca16] | 149 | static uint32_t ext4_balloc_get_first_data_block_in_group(
|
---|
| 150 | ext4_superblock_t *sb, ext4_block_group_t *bg, uint32_t bgid)
|
---|
| 151 | {
|
---|
| 152 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
[fe27eb4] | 153 | uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
|
---|
| 154 | bg, sb);
|
---|
[b12ca16] | 155 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
---|
| 156 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 157 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 158 | uint32_t inode_table_bytes;
|
---|
| 159 |
|
---|
| 160 | if (bgid < block_group_count - 1) {
|
---|
| 161 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
---|
| 162 | } else {
|
---|
| 163 | // last block group could be smaller
|
---|
| 164 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
---|
| 165 | inode_table_bytes =
|
---|
| 166 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
|
---|
| 167 | * inode_table_item_size;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | uint32_t inode_table_blocks = inode_table_bytes / block_size;
|
---|
| 171 |
|
---|
| 172 | if (inode_table_bytes % block_size) {
|
---|
| 173 | inode_table_blocks++;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | return inode_table_first_block + inode_table_blocks;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[2674db6] | 179 |
|
---|
[b12ca16] | 180 | static uint32_t ext4_balloc_find_goal(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
|
---|
[2674db6] | 181 | {
|
---|
[b12ca16] | 182 | int rc;
|
---|
[2674db6] | 183 | uint32_t goal = 0;
|
---|
| 184 |
|
---|
| 185 | uint64_t inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
|
---|
| 186 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 187 | uint32_t inode_block_count = inode_size / block_size;
|
---|
| 188 |
|
---|
| 189 | if (inode_size % block_size != 0) {
|
---|
| 190 | inode_block_count++;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | if (inode_block_count > 0) {
|
---|
| 194 | // TODO check retval
|
---|
| 195 | ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, inode_block_count - 1, &goal);
|
---|
| 196 |
|
---|
| 197 | // TODO
|
---|
| 198 | // If goal == 0 -> SPARSE file !!!
|
---|
[b12ca16] | 199 |
|
---|
[2674db6] | 200 | goal++;
|
---|
| 201 | return goal;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // Identify block group of inode
|
---|
| 205 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
| 206 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
| 207 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 208 |
|
---|
| 209 | ext4_block_group_ref_t *bg_ref;
|
---|
| 210 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 211 | if (rc != EOK) {
|
---|
| 212 | return 0;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[b12ca16] | 215 | uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
|
---|
[fe27eb4] | 216 | uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
|
---|
| 217 | bg_ref->block_group, fs->superblock);
|
---|
[2674db6] | 218 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
[b12ca16] | 219 | uint32_t inode_table_bytes;
|
---|
| 220 |
|
---|
| 221 | if (block_group < block_group_count - 1) {
|
---|
| 222 | inode_table_bytes = inodes_per_group * inode_table_item_size;
|
---|
| 223 | } else {
|
---|
| 224 | // last block group could be smaller
|
---|
| 225 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(fs->superblock);
|
---|
| 226 | inode_table_bytes =
|
---|
| 227 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
|
---|
| 228 | * inode_table_item_size;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[2674db6] | 231 | uint32_t inode_table_blocks = inode_table_bytes / block_size;
|
---|
| 232 |
|
---|
| 233 | if (inode_table_bytes % block_size) {
|
---|
| 234 | inode_table_blocks++;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | goal = inode_table_first_block + inode_table_blocks;
|
---|
| 238 |
|
---|
| 239 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 240 |
|
---|
| 241 | return goal;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[412f813] | 244 | int ext4_balloc_alloc_block(ext4_filesystem_t *fs,
|
---|
| 245 | ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
[2674db6] | 246 | {
|
---|
| 247 | int rc;
|
---|
[b12ca16] | 248 | uint32_t allocated_block = 0;
|
---|
| 249 |
|
---|
[528e5b3] | 250 | uint32_t bitmap_block_addr;
|
---|
| 251 | block_t *bitmap_block;
|
---|
[2674db6] | 252 | uint32_t rel_block_idx = 0;
|
---|
| 253 |
|
---|
[b12ca16] | 254 | // Find GOAL
|
---|
| 255 | uint32_t goal = ext4_balloc_find_goal(fs, inode_ref);
|
---|
[2674db6] | 256 | if (goal == 0) {
|
---|
| 257 | // TODO
|
---|
| 258 | EXT4FS_DBG("ERRORR (goal == 0)");
|
---|
[b12ca16] | 259 | return ENOSPC;
|
---|
[2674db6] | 260 | }
|
---|
| 261 |
|
---|
[b12ca16] | 262 | // Load block group number for goal and relative index
|
---|
| 263 | uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, goal);
|
---|
| 264 | uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, goal);
|
---|
[2674db6] | 265 |
|
---|
| 266 |
|
---|
[b12ca16] | 267 | ext4_block_group_ref_t *bg_ref;
|
---|
[2674db6] | 268 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 269 | if (rc != EOK) {
|
---|
[b12ca16] | 270 | EXT4FS_DBG("initial BG ref not loaded");
|
---|
[2674db6] | 271 | return rc;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[b12ca16] | 274 | uint32_t first_in_group =
|
---|
| 275 | ext4_balloc_get_first_data_block_in_group(fs->superblock,
|
---|
| 276 | bg_ref->block_group, block_group);
|
---|
| 277 |
|
---|
[528e5b3] | 278 | uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
|
---|
| 279 | fs->superblock, first_in_group);
|
---|
| 280 |
|
---|
| 281 | if (index_in_group < first_in_group_index) {
|
---|
| 282 | index_in_group = first_in_group_index;
|
---|
[b12ca16] | 283 | }
|
---|
| 284 |
|
---|
[2674db6] | 285 | // Load bitmap
|
---|
[fe27eb4] | 286 | bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
|
---|
| 287 | fs->superblock);
|
---|
[2674db6] | 288 |
|
---|
[528e5b3] | 289 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[2674db6] | 290 | if (rc != EOK) {
|
---|
| 291 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[b12ca16] | 292 | EXT4FS_DBG("initial bitmap not loaded");
|
---|
[2674db6] | 293 | return rc;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[b12ca16] | 296 | // Check if goal is free
|
---|
[528e5b3] | 297 | if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
|
---|
| 298 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 299 | bitmap_block->dirty = true;
|
---|
| 300 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 301 | if (rc != EOK) {
|
---|
| 302 | // TODO error
|
---|
[b12ca16] | 303 | EXT4FS_DBG("goal check: error in saving initial bitmap \%d", rc);
|
---|
| 304 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 305 | return rc;
|
---|
[2674db6] | 306 | }
|
---|
| 307 |
|
---|
| 308 | allocated_block = goal;
|
---|
[b12ca16] | 309 | goto success;
|
---|
[2674db6] | 310 |
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[b12ca16] | 313 | uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, block_group);
|
---|
[2674db6] | 314 |
|
---|
[b12ca16] | 315 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
| 316 | if (end_idx > blocks_in_group) {
|
---|
| 317 | end_idx = blocks_in_group;
|
---|
| 318 | }
|
---|
[2674db6] | 319 |
|
---|
[b12ca16] | 320 | // Try to find free block near to goal
|
---|
| 321 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) {
|
---|
[528e5b3] | 322 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
[2674db6] | 323 |
|
---|
[528e5b3] | 324 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
| 325 | bitmap_block->dirty = true;
|
---|
| 326 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 327 | if (rc != EOK) {
|
---|
| 328 | // TODO error
|
---|
[b12ca16] | 329 | EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
|
---|
[2674db6] | 330 | }
|
---|
| 331 |
|
---|
[b12ca16] | 332 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
| 333 | fs->superblock, tmp_idx, block_group);
|
---|
| 334 |
|
---|
| 335 | goto success;
|
---|
[2674db6] | 336 | }
|
---|
| 337 |
|
---|
[b12ca16] | 338 | }
|
---|
[2674db6] | 339 |
|
---|
| 340 | // Find free BYTE in bitmap
|
---|
[528e5b3] | 341 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 342 | if (rc == EOK) {
|
---|
[528e5b3] | 343 | bitmap_block->dirty = true;
|
---|
| 344 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 345 | if (rc != EOK) {
|
---|
| 346 | // TODO error
|
---|
[b12ca16] | 347 | EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
|
---|
[2674db6] | 348 | }
|
---|
| 349 |
|
---|
[b12ca16] | 350 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
| 351 | fs->superblock, rel_block_idx, block_group);
|
---|
[2674db6] | 352 |
|
---|
[b12ca16] | 353 | goto success;
|
---|
| 354 | }
|
---|
[2674db6] | 355 |
|
---|
| 356 | // Find free bit in bitmap
|
---|
[528e5b3] | 357 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[2674db6] | 358 | if (rc == EOK) {
|
---|
[528e5b3] | 359 | bitmap_block->dirty = true;
|
---|
| 360 | rc = block_put(bitmap_block);
|
---|
[2674db6] | 361 | if (rc != EOK) {
|
---|
| 362 | // TODO error
|
---|
[b12ca16] | 363 | EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
|
---|
[2674db6] | 364 | }
|
---|
| 365 |
|
---|
[b12ca16] | 366 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
| 367 | fs->superblock, rel_block_idx, block_group);
|
---|
[2674db6] | 368 |
|
---|
[b12ca16] | 369 | goto success;
|
---|
[2674db6] | 370 | }
|
---|
| 371 |
|
---|
[e63ce679] | 372 | // No free block found yet
|
---|
[528e5b3] | 373 | block_put(bitmap_block);
|
---|
[b12ca16] | 374 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 375 |
|
---|
| 376 | // Try other block groups
|
---|
| 377 | uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
|
---|
| 378 |
|
---|
| 379 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
| 380 | uint32_t count = block_group_count;
|
---|
| 381 |
|
---|
| 382 | while (count > 0) {
|
---|
| 383 | rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
| 384 | if (rc != EOK) {
|
---|
| 385 | EXT4FS_DBG("errrrrrrrrrrr");
|
---|
| 386 | return rc;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | // Load bitmap
|
---|
[fe27eb4] | 390 | bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
| 391 | bg_ref->block_group, fs->superblock);
|
---|
[b12ca16] | 392 |
|
---|
[528e5b3] | 393 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
[b12ca16] | 394 | if (rc != EOK) {
|
---|
| 395 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 396 | EXT4FS_DBG("errrrrrrrrrr");
|
---|
| 397 | return rc;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | first_in_group = ext4_balloc_get_first_data_block_in_group(
|
---|
| 401 | fs->superblock, bg_ref->block_group, bgid);
|
---|
| 402 | index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock,
|
---|
| 403 | first_in_group);
|
---|
| 404 | blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, bgid);
|
---|
| 405 |
|
---|
[528e5b3] | 406 | first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
|
---|
| 407 | fs->superblock, first_in_group);
|
---|
| 408 |
|
---|
| 409 | if (index_in_group < first_in_group_index) {
|
---|
| 410 | index_in_group = first_in_group_index;
|
---|
[b12ca16] | 411 | }
|
---|
| 412 |
|
---|
[528e5b3] | 413 |
|
---|
| 414 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 415 | if (rc == EOK) {
|
---|
[528e5b3] | 416 | bitmap_block->dirty = true;
|
---|
| 417 | rc = block_put(bitmap_block);
|
---|
[b12ca16] | 418 | if (rc != EOK) {
|
---|
| 419 | // TODO error
|
---|
| 420 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
| 424 | fs->superblock, rel_block_idx, bgid);
|
---|
| 425 |
|
---|
| 426 | goto success;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | // Find free bit in bitmap
|
---|
[528e5b3] | 430 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
|
---|
[b12ca16] | 431 | if (rc == EOK) {
|
---|
[528e5b3] | 432 | bitmap_block->dirty = true;
|
---|
| 433 | rc = block_put(bitmap_block);
|
---|
[b12ca16] | 434 | if (rc != EOK) {
|
---|
| 435 | // TODO error
|
---|
| 436 | EXT4FS_DBG("error in saving bitmap \%d", rc);
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | allocated_block = ext4_balloc_index_in_group2blockaddr(
|
---|
| 440 | fs->superblock, rel_block_idx, bgid);
|
---|
| 441 |
|
---|
| 442 | goto success;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[2674db6] | 445 |
|
---|
[b12ca16] | 446 | // Next group
|
---|
[528e5b3] | 447 | block_put(bitmap_block);
|
---|
[b12ca16] | 448 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 449 | bgid = (bgid + 1) % block_group_count;
|
---|
| 450 | count--;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
[2674db6] | 453 | return ENOSPC;
|
---|
| 454 |
|
---|
[b12ca16] | 455 | success:
|
---|
[528e5b3] | 456 | ; // Empty command - because of syntax
|
---|
| 457 |
|
---|
[b12ca16] | 458 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 459 |
|
---|
[ae3d4f8] | 460 | // Update superblock free blocks count
|
---|
| 461 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
|
---|
| 462 | sb_free_blocks--;
|
---|
| 463 | ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
|
---|
[2674db6] | 464 |
|
---|
[ae3d4f8] | 465 | // Update inode blocks (different block size!) count
|
---|
[412f813] | 466 |
|
---|
[2674db6] | 467 | uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
|
---|
| 468 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
| 469 | ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
|
---|
| 470 | inode_ref->dirty = true;
|
---|
| 471 |
|
---|
[ae3d4f8] | 472 | // Update block group free blocks count
|
---|
[fe27eb4] | 473 | uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
|
---|
| 474 | bg_ref->block_group, fs->superblock);
|
---|
[2674db6] | 475 | bg_free_blocks--;
|
---|
[fe27eb4] | 476 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
| 477 | fs->superblock, bg_free_blocks);
|
---|
[2674db6] | 478 | bg_ref->dirty = true;
|
---|
| 479 |
|
---|
| 480 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 481 |
|
---|
| 482 | *fblock = allocated_block;
|
---|
| 483 | return EOK;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 |
|
---|
| 487 | /**
|
---|
| 488 | * @}
|
---|
| 489 | */
|
---|