[bf66ef4] | 1 | /*
|
---|
[aab85d90] | 2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[f22d5ef0] | 3 | * Copyright (c) 2012 Frantisek Princ
|
---|
[bf66ef4] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libext4
|
---|
| 31 | * @{
|
---|
[38542dc] | 32 | */
|
---|
[bf66ef4] | 33 | /**
|
---|
[4bfad34] | 34 | * @file ialloc.c
|
---|
[38542dc] | 35 | * @brief I-node (de)allocation operations.
|
---|
[bf66ef4] | 36 | */
|
---|
| 37 |
|
---|
[3d4fd2c] | 38 | #include <errno.h>
|
---|
[fcb0d76] | 39 | #include <stdbool.h>
|
---|
| 40 | #include "ext4/bitmap.h"
|
---|
| 41 | #include "ext4/block_group.h"
|
---|
| 42 | #include "ext4/filesystem.h"
|
---|
| 43 | #include "ext4/ialloc.h"
|
---|
| 44 | #include "ext4/superblock.h"
|
---|
[bf66ef4] | 45 |
|
---|
[ee3b6150] | 46 |
|
---|
| 47 | /** Convert i-node number to relative index in block group.
|
---|
| 48 | *
|
---|
[38542dc] | 49 | * @param sb Superblock
|
---|
| 50 | * @param inode I-node number to be converted
|
---|
| 51 | *
|
---|
| 52 | * @return Index of the i-node in the block group
|
---|
| 53 | *
|
---|
[ee3b6150] | 54 | */
|
---|
[3d4fd2c] | 55 | static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
|
---|
[38542dc] | 56 | uint32_t inode)
|
---|
[3d4fd2c] | 57 | {
|
---|
| 58 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 59 | return (inode - 1) % inodes_per_group;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[ee3b6150] | 62 | /** Convert relative index of i-node to absolute i-node number.
|
---|
| 63 | *
|
---|
[38542dc] | 64 | * @param sb Superblock
|
---|
| 65 | * @param inode Index to be converted
|
---|
| 66 | *
|
---|
| 67 | * @return Absolute number of the i-node
|
---|
| 68 | *
|
---|
[ee3b6150] | 69 | */
|
---|
[cd1cc4e6] | 70 | static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
|
---|
[38542dc] | 71 | uint32_t index, uint32_t bgid)
|
---|
[cd1cc4e6] | 72 | {
|
---|
| 73 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 74 | return bgid * inodes_per_group + (index + 1);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[ee3b6150] | 77 | /** Compute block group number from the i-node number.
|
---|
| 78 | *
|
---|
[38542dc] | 79 | * @param sb Superblock
|
---|
| 80 | * @param inode I-node number to be found the block group for
|
---|
| 81 | *
|
---|
| 82 | * @return Block group number computed from i-node number
|
---|
| 83 | *
|
---|
[ee3b6150] | 84 | */
|
---|
[3d4fd2c] | 85 | static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
|
---|
[38542dc] | 86 | uint32_t inode)
|
---|
[3d4fd2c] | 87 | {
|
---|
| 88 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 89 | return (inode - 1) / inodes_per_group;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[ee3b6150] | 93 | /** Free i-node number and modify filesystem data structers.
|
---|
| 94 | *
|
---|
[38542dc] | 95 | * @param fs Filesystem, where the i-node is located
|
---|
| 96 | * @param index Index of i-node to be release
|
---|
| 97 | * @param is_dir Flag us for information whether i-node is directory or not
|
---|
| 98 | *
|
---|
[ee3b6150] | 99 | */
|
---|
[b7fd2a0] | 100 | errno_t ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
|
---|
[3d4fd2c] | 101 | {
|
---|
[304faab] | 102 | ext4_superblock_t *sb = fs->superblock;
|
---|
[a35b458] | 103 |
|
---|
[06d85e5] | 104 | /* Compute index of block group and load it */
|
---|
[304faab] | 105 | uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
|
---|
[a35b458] | 106 |
|
---|
[3d4fd2c] | 107 | ext4_block_group_ref_t *bg_ref;
|
---|
[b7fd2a0] | 108 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
[38542dc] | 109 | if (rc != EOK)
|
---|
[3d4fd2c] | 110 | return rc;
|
---|
[a35b458] | 111 |
|
---|
[06d85e5] | 112 | /* Load i-node bitmap */
|
---|
[3d4fd2c] | 113 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
[38542dc] | 114 | bg_ref->block_group, sb);
|
---|
[3d4fd2c] | 115 | block_t *bitmap_block;
|
---|
[38542dc] | 116 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
| 117 | BLOCK_FLAGS_NONE);
|
---|
| 118 | if (rc != EOK)
|
---|
[3d4fd2c] | 119 | return rc;
|
---|
[a35b458] | 120 |
|
---|
[06d85e5] | 121 | /* Free i-node in the bitmap */
|
---|
[304faab] | 122 | uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
|
---|
[3d4fd2c] | 123 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 124 | bitmap_block->dirty = true;
|
---|
[a35b458] | 125 |
|
---|
[06d85e5] | 126 | /* Put back the block with bitmap */
|
---|
[3d4fd2c] | 127 | rc = block_put(bitmap_block);
|
---|
| 128 | if (rc != EOK) {
|
---|
[06d85e5] | 129 | /* Error in saving bitmap */
|
---|
[3d4fd2c] | 130 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 131 | return rc;
|
---|
| 132 | }
|
---|
[a35b458] | 133 |
|
---|
[06d85e5] | 134 | /* If released i-node is a directory, decrement used directories count */
|
---|
[304faab] | 135 | if (is_dir) {
|
---|
[e5f8762] | 136 | uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
|
---|
[38542dc] | 137 | bg_ref->block_group, sb);
|
---|
[e5f8762] | 138 | bg_used_dirs--;
|
---|
[38542dc] | 139 | ext4_block_group_set_used_dirs_count(bg_ref->block_group, sb,
|
---|
| 140 | bg_used_dirs);
|
---|
[e5f8762] | 141 | }
|
---|
[a35b458] | 142 |
|
---|
[06d85e5] | 143 | /* Update block group free inodes count */
|
---|
[3d4fd2c] | 144 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
|
---|
[38542dc] | 145 | bg_ref->block_group, sb);
|
---|
[3d4fd2c] | 146 | free_inodes++;
|
---|
[38542dc] | 147 | ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
|
---|
| 148 | free_inodes);
|
---|
[a35b458] | 149 |
|
---|
[3d4fd2c] | 150 | bg_ref->dirty = true;
|
---|
[a35b458] | 151 |
|
---|
[06d85e5] | 152 | /* Put back the modified block group */
|
---|
[3d4fd2c] | 153 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 154 | if (rc != EOK)
|
---|
[3d4fd2c] | 155 | return rc;
|
---|
[a35b458] | 156 |
|
---|
[06d85e5] | 157 | /* Update superblock free inodes count */
|
---|
[38542dc] | 158 | uint32_t sb_free_inodes =
|
---|
| 159 | ext4_superblock_get_free_inodes_count(sb);
|
---|
[304faab] | 160 | sb_free_inodes++;
|
---|
| 161 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
[a35b458] | 162 |
|
---|
[3d4fd2c] | 163 | return EOK;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[ee3b6150] | 166 | /** I-node allocation algorithm.
|
---|
| 167 | *
|
---|
[38542dc] | 168 | * This is more simple algorithm, than Orlov allocator used
|
---|
| 169 | * in the Linux kernel.
|
---|
| 170 | *
|
---|
| 171 | * @param fs Filesystem to allocate i-node on
|
---|
| 172 | * @param index Output value - allocated i-node number
|
---|
| 173 | * @param is_dir Flag if allocated i-node will be file or directory
|
---|
| 174 | *
|
---|
| 175 | * @return Error code
|
---|
[ee3b6150] | 176 | *
|
---|
| 177 | */
|
---|
[b7fd2a0] | 178 | errno_t ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
|
---|
[304faab] | 179 | {
|
---|
| 180 | ext4_superblock_t *sb = fs->superblock;
|
---|
[a35b458] | 181 |
|
---|
[304faab] | 182 | uint32_t bgid = 0;
|
---|
| 183 | uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
|
---|
[cd1cc4e6] | 184 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
| 185 | uint32_t avg_free_inodes = sb_free_inodes / bg_count;
|
---|
[a35b458] | 186 |
|
---|
[06d85e5] | 187 | /* Try to find free i-node in all block groups */
|
---|
[304faab] | 188 | while (bgid < bg_count) {
|
---|
[06d85e5] | 189 | /* Load block group to check */
|
---|
[304faab] | 190 | ext4_block_group_ref_t *bg_ref;
|
---|
[b7fd2a0] | 191 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
[38542dc] | 192 | if (rc != EOK)
|
---|
[304faab] | 193 | return rc;
|
---|
[a35b458] | 194 |
|
---|
[304faab] | 195 | ext4_block_group_t *bg = bg_ref->block_group;
|
---|
[a35b458] | 196 |
|
---|
[06d85e5] | 197 | /* Read necessary values for algorithm */
|
---|
[304faab] | 198 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
|
---|
| 199 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
|
---|
| 200 | uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
|
---|
[a35b458] | 201 |
|
---|
[8a45707d] | 202 | /*
|
---|
| 203 | * Check if this block group is a good candidate
|
---|
| 204 | * for allocation.
|
---|
| 205 | *
|
---|
| 206 | * The criterion is based on the average number
|
---|
| 207 | * of free inodes, unless we examine the last block
|
---|
| 208 | * group. In that case the last block group might
|
---|
| 209 | * have less than the average number of free inodes,
|
---|
| 210 | * but it still needs to be taken as a candidate
|
---|
| 211 | * because the previous block groups have zero free
|
---|
| 212 | * blocks.
|
---|
| 213 | */
|
---|
| 214 | if (((free_inodes >= avg_free_inodes) || (bgid == bg_count - 1)) &&
|
---|
| 215 | (free_blocks > 0)) {
|
---|
[06d85e5] | 216 | /* Load block with bitmap */
|
---|
[38542dc] | 217 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
| 218 | bg_ref->block_group, sb);
|
---|
[a35b458] | 219 |
|
---|
[304faab] | 220 | block_t *bitmap_block;
|
---|
[38542dc] | 221 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
| 222 | BLOCK_FLAGS_NONE);
|
---|
[2f591127] | 223 | if (rc != EOK) {
|
---|
| 224 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[304faab] | 225 | return rc;
|
---|
[2f591127] | 226 | }
|
---|
[a35b458] | 227 |
|
---|
[06d85e5] | 228 | /* Try to allocate i-node in the bitmap */
|
---|
[304faab] | 229 | uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
[cd1cc4e6] | 230 | uint32_t index_in_group;
|
---|
[38542dc] | 231 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
| 232 | 0, &index_in_group, inodes_in_group);
|
---|
[a35b458] | 233 |
|
---|
[06d85e5] | 234 | /* Block group has not any free i-node */
|
---|
[1d69c69] | 235 | if (rc == ENOSPC) {
|
---|
[e5a1ace3] | 236 | rc = block_put(bitmap_block);
|
---|
| 237 | if (rc != EOK) {
|
---|
| 238 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 239 | return rc;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 243 | if (rc != EOK)
|
---|
| 244 | return rc;
|
---|
| 245 |
|
---|
[2f591127] | 246 | bgid++;
|
---|
[1d69c69] | 247 | continue;
|
---|
| 248 | }
|
---|
[a35b458] | 249 |
|
---|
[06d85e5] | 250 | /* Free i-node found, save the bitmap */
|
---|
[304faab] | 251 | bitmap_block->dirty = true;
|
---|
[a35b458] | 252 |
|
---|
[304faab] | 253 | rc = block_put(bitmap_block);
|
---|
[2f591127] | 254 | if (rc != EOK) {
|
---|
| 255 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[304faab] | 256 | return rc;
|
---|
[2f591127] | 257 | }
|
---|
[a35b458] | 258 |
|
---|
[06d85e5] | 259 | /* Modify filesystem counters */
|
---|
[304faab] | 260 | free_inodes--;
|
---|
| 261 | ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
|
---|
[a35b458] | 262 |
|
---|
[06d85e5] | 263 | /* Increment used directories counter */
|
---|
[304faab] | 264 | if (is_dir) {
|
---|
[cd1cc4e6] | 265 | used_dirs++;
|
---|
[304faab] | 266 | ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
|
---|
| 267 | }
|
---|
[a35b458] | 268 |
|
---|
[e25d78c] | 269 | /* Decrease unused inodes count */
|
---|
| 270 | if (ext4_block_group_has_flag(bg,
|
---|
[38542dc] | 271 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
---|
[e25d78c] | 272 | uint32_t unused =
|
---|
[38542dc] | 273 | ext4_block_group_get_itable_unused(bg, sb);
|
---|
[a35b458] | 274 |
|
---|
[e25d78c] | 275 | uint32_t inodes_in_group =
|
---|
[38542dc] | 276 | ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
[a35b458] | 277 |
|
---|
[e25d78c] | 278 | uint32_t free = inodes_in_group - unused;
|
---|
[a35b458] | 279 |
|
---|
[e25d78c] | 280 | if (index_in_group >= free) {
|
---|
| 281 | unused = inodes_in_group - (index_in_group + 1);
|
---|
| 282 | ext4_block_group_set_itable_unused(bg, sb, unused);
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
[a35b458] | 285 |
|
---|
[06d85e5] | 286 | /* Save modified block group */
|
---|
[304faab] | 287 | bg_ref->dirty = true;
|
---|
[a35b458] | 288 |
|
---|
[304faab] | 289 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 290 | if (rc != EOK)
|
---|
[847f2cb] | 291 | return rc;
|
---|
[a35b458] | 292 |
|
---|
[06d85e5] | 293 | /* Update superblock */
|
---|
[304faab] | 294 | sb_free_inodes--;
|
---|
| 295 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
[a35b458] | 296 |
|
---|
[06d85e5] | 297 | /* Compute the absolute i-nodex number */
|
---|
[cd1cc4e6] | 298 | *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
|
---|
[a35b458] | 299 |
|
---|
[304faab] | 300 | return EOK;
|
---|
| 301 | }
|
---|
[a35b458] | 302 |
|
---|
[06d85e5] | 303 | /* Block group not modified, put it and jump to the next block group */
|
---|
[e5a1ace3] | 304 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 305 | if (rc != EOK)
|
---|
| 306 | return rc;
|
---|
| 307 |
|
---|
[cd1cc4e6] | 308 | ++bgid;
|
---|
[304faab] | 309 | }
|
---|
[a35b458] | 310 |
|
---|
[304faab] | 311 | return ENOSPC;
|
---|
| 312 | }
|
---|
[bf66ef4] | 313 |
|
---|
[aab85d90] | 314 | /** Allocate a specific I-node.
|
---|
| 315 | *
|
---|
| 316 | * @param fs Filesystem to allocate i-node on
|
---|
| 317 | * @param inode I-node to allocate
|
---|
| 318 | * @param is_dir Flag if allocated i-node will be file or directory
|
---|
| 319 | *
|
---|
| 320 | * @return Error code
|
---|
| 321 | *
|
---|
| 322 | */
|
---|
| 323 | errno_t ext4_ialloc_alloc_this_inode(ext4_filesystem_t *fs, uint32_t inode,
|
---|
| 324 | bool is_dir)
|
---|
| 325 | {
|
---|
| 326 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 327 |
|
---|
| 328 | uint32_t bgid = ext4_ialloc_get_bgid_of_inode(sb, inode);
|
---|
| 329 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
| 330 |
|
---|
| 331 | /* Load block group */
|
---|
| 332 | ext4_block_group_ref_t *bg_ref;
|
---|
| 333 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
| 334 | if (rc != EOK)
|
---|
| 335 | return rc;
|
---|
| 336 |
|
---|
| 337 | ext4_block_group_t *bg = bg_ref->block_group;
|
---|
| 338 |
|
---|
| 339 | /* Read necessary values for algorithm */
|
---|
| 340 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
|
---|
| 341 | uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
|
---|
| 342 |
|
---|
| 343 | /* Load block with bitmap */
|
---|
| 344 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
| 345 | bg_ref->block_group, sb);
|
---|
| 346 |
|
---|
| 347 | block_t *bitmap_block;
|
---|
| 348 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
| 349 | BLOCK_FLAGS_NONE);
|
---|
| 350 | if (rc != EOK) {
|
---|
| 351 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 352 | return rc;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | /* Allocate i-node in the bitmap */
|
---|
| 356 | uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, inode);
|
---|
| 357 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
| 358 |
|
---|
| 359 | /* Save the bitmap */
|
---|
| 360 | bitmap_block->dirty = true;
|
---|
| 361 |
|
---|
| 362 | rc = block_put(bitmap_block);
|
---|
| 363 | if (rc != EOK) {
|
---|
| 364 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 365 | return rc;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /* Modify filesystem counters */
|
---|
| 369 | free_inodes--;
|
---|
| 370 | ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
|
---|
| 371 |
|
---|
| 372 | /* Increment used directories counter */
|
---|
| 373 | if (is_dir) {
|
---|
| 374 | used_dirs++;
|
---|
| 375 | ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /* Decrease unused inodes count */
|
---|
| 379 | if (ext4_block_group_has_flag(bg,
|
---|
| 380 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
---|
| 381 | uint32_t unused =
|
---|
| 382 | ext4_block_group_get_itable_unused(bg, sb);
|
---|
| 383 |
|
---|
| 384 | uint32_t inodes_in_group =
|
---|
| 385 | ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
| 386 |
|
---|
| 387 | uint32_t free = inodes_in_group - unused;
|
---|
| 388 |
|
---|
| 389 | if (index_in_group >= free) {
|
---|
| 390 | unused = inodes_in_group - (index_in_group + 1);
|
---|
| 391 | ext4_block_group_set_itable_unused(bg, sb, unused);
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /* Save modified block group */
|
---|
| 396 | bg_ref->dirty = true;
|
---|
| 397 |
|
---|
| 398 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 399 | if (rc != EOK)
|
---|
| 400 | return rc;
|
---|
| 401 |
|
---|
| 402 | /* Update superblock */
|
---|
| 403 | sb_free_inodes--;
|
---|
| 404 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
| 405 |
|
---|
| 406 | return EOK;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[bf66ef4] | 409 | /**
|
---|
| 410 | * @}
|
---|
[38542dc] | 411 | */
|
---|