[bf66ef4] | 1 | /*
|
---|
[f22d5ef0] | 2 | * Copyright (c) 2012 Frantisek Princ
|
---|
[bf66ef4] | 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 | */
|
---|
[bf66ef4] | 32 | /**
|
---|
[38542dc] | 33 | * @file libext4_ialloc.c
|
---|
| 34 | * @brief I-node (de)allocation operations.
|
---|
[bf66ef4] | 35 | */
|
---|
| 36 |
|
---|
[3d4fd2c] | 37 | #include <errno.h>
|
---|
[e5f8762] | 38 | #include <time.h>
|
---|
[bf66ef4] | 39 | #include "libext4.h"
|
---|
| 40 |
|
---|
[ee3b6150] | 41 |
|
---|
| 42 | /** Convert i-node number to relative index in block group.
|
---|
| 43 | *
|
---|
[38542dc] | 44 | * @param sb Superblock
|
---|
| 45 | * @param inode I-node number to be converted
|
---|
| 46 | *
|
---|
| 47 | * @return Index of the i-node in the block group
|
---|
| 48 | *
|
---|
[ee3b6150] | 49 | */
|
---|
[3d4fd2c] | 50 | static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
|
---|
[38542dc] | 51 | uint32_t inode)
|
---|
[3d4fd2c] | 52 | {
|
---|
| 53 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 54 | return (inode - 1) % inodes_per_group;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[ee3b6150] | 57 | /** Convert relative index of i-node to absolute i-node number.
|
---|
| 58 | *
|
---|
[38542dc] | 59 | * @param sb Superblock
|
---|
| 60 | * @param inode Index to be converted
|
---|
| 61 | *
|
---|
| 62 | * @return Absolute number of the i-node
|
---|
| 63 | *
|
---|
[ee3b6150] | 64 | */
|
---|
[cd1cc4e6] | 65 | static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
|
---|
[38542dc] | 66 | uint32_t index, uint32_t bgid)
|
---|
[cd1cc4e6] | 67 | {
|
---|
| 68 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 69 | return bgid * inodes_per_group + (index + 1);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[ee3b6150] | 72 | /** Compute block group number from the i-node number.
|
---|
| 73 | *
|
---|
[38542dc] | 74 | * @param sb Superblock
|
---|
| 75 | * @param inode I-node number to be found the block group for
|
---|
| 76 | *
|
---|
| 77 | * @return Block group number computed from i-node number
|
---|
| 78 | *
|
---|
[ee3b6150] | 79 | */
|
---|
[3d4fd2c] | 80 | static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
|
---|
[38542dc] | 81 | uint32_t inode)
|
---|
[3d4fd2c] | 82 | {
|
---|
| 83 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 84 | return (inode - 1) / inodes_per_group;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
[ee3b6150] | 88 | /** Free i-node number and modify filesystem data structers.
|
---|
| 89 | *
|
---|
[38542dc] | 90 | * @param fs Filesystem, where the i-node is located
|
---|
| 91 | * @param index Index of i-node to be release
|
---|
| 92 | * @param is_dir Flag us for information whether i-node is directory or not
|
---|
| 93 | *
|
---|
[ee3b6150] | 94 | */
|
---|
[304faab] | 95 | int ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
|
---|
[3d4fd2c] | 96 | {
|
---|
[304faab] | 97 | ext4_superblock_t *sb = fs->superblock;
|
---|
[38542dc] | 98 |
|
---|
[06d85e5] | 99 | /* Compute index of block group and load it */
|
---|
[304faab] | 100 | uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
|
---|
[38542dc] | 101 |
|
---|
[3d4fd2c] | 102 | ext4_block_group_ref_t *bg_ref;
|
---|
[38542dc] | 103 | int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 104 | if (rc != EOK)
|
---|
[3d4fd2c] | 105 | return rc;
|
---|
[38542dc] | 106 |
|
---|
[06d85e5] | 107 | /* Load i-node bitmap */
|
---|
[3d4fd2c] | 108 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
[38542dc] | 109 | bg_ref->block_group, sb);
|
---|
[3d4fd2c] | 110 | block_t *bitmap_block;
|
---|
[38542dc] | 111 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
| 112 | BLOCK_FLAGS_NONE);
|
---|
| 113 | if (rc != EOK)
|
---|
[3d4fd2c] | 114 | return rc;
|
---|
[38542dc] | 115 |
|
---|
[06d85e5] | 116 | /* Free i-node in the bitmap */
|
---|
[304faab] | 117 | uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
|
---|
[3d4fd2c] | 118 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 119 | bitmap_block->dirty = true;
|
---|
[38542dc] | 120 |
|
---|
[06d85e5] | 121 | /* Put back the block with bitmap */
|
---|
[3d4fd2c] | 122 | rc = block_put(bitmap_block);
|
---|
| 123 | if (rc != EOK) {
|
---|
[06d85e5] | 124 | /* Error in saving bitmap */
|
---|
[3d4fd2c] | 125 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 126 | return rc;
|
---|
| 127 | }
|
---|
[38542dc] | 128 |
|
---|
[06d85e5] | 129 | /* If released i-node is a directory, decrement used directories count */
|
---|
[304faab] | 130 | if (is_dir) {
|
---|
[e5f8762] | 131 | uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
|
---|
[38542dc] | 132 | bg_ref->block_group, sb);
|
---|
[e5f8762] | 133 | bg_used_dirs--;
|
---|
[38542dc] | 134 | ext4_block_group_set_used_dirs_count(bg_ref->block_group, sb,
|
---|
| 135 | bg_used_dirs);
|
---|
[e5f8762] | 136 | }
|
---|
[38542dc] | 137 |
|
---|
[06d85e5] | 138 | /* Update block group free inodes count */
|
---|
[3d4fd2c] | 139 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
|
---|
[38542dc] | 140 | bg_ref->block_group, sb);
|
---|
[3d4fd2c] | 141 | free_inodes++;
|
---|
[38542dc] | 142 | ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
|
---|
| 143 | free_inodes);
|
---|
| 144 |
|
---|
[3d4fd2c] | 145 | bg_ref->dirty = true;
|
---|
[38542dc] | 146 |
|
---|
[06d85e5] | 147 | /* Put back the modified block group */
|
---|
[3d4fd2c] | 148 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 149 | if (rc != EOK)
|
---|
[3d4fd2c] | 150 | return rc;
|
---|
[38542dc] | 151 |
|
---|
[06d85e5] | 152 | /* Update superblock free inodes count */
|
---|
[38542dc] | 153 | uint32_t sb_free_inodes =
|
---|
| 154 | ext4_superblock_get_free_inodes_count(sb);
|
---|
[304faab] | 155 | sb_free_inodes++;
|
---|
| 156 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
[38542dc] | 157 |
|
---|
[3d4fd2c] | 158 | return EOK;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[ee3b6150] | 161 | /** I-node allocation algorithm.
|
---|
| 162 | *
|
---|
[38542dc] | 163 | * This is more simple algorithm, than Orlov allocator used
|
---|
| 164 | * in the Linux kernel.
|
---|
| 165 | *
|
---|
| 166 | * @param fs Filesystem to allocate i-node on
|
---|
| 167 | * @param index Output value - allocated i-node number
|
---|
| 168 | * @param is_dir Flag if allocated i-node will be file or directory
|
---|
| 169 | *
|
---|
| 170 | * @return Error code
|
---|
[ee3b6150] | 171 | *
|
---|
| 172 | */
|
---|
[304faab] | 173 | int ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
|
---|
| 174 | {
|
---|
| 175 | ext4_superblock_t *sb = fs->superblock;
|
---|
[38542dc] | 176 |
|
---|
[304faab] | 177 | uint32_t bgid = 0;
|
---|
| 178 | uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
|
---|
[cd1cc4e6] | 179 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
| 180 | uint32_t avg_free_inodes = sb_free_inodes / bg_count;
|
---|
[38542dc] | 181 |
|
---|
[06d85e5] | 182 | /* Try to find free i-node in all block groups */
|
---|
[304faab] | 183 | while (bgid < bg_count) {
|
---|
[06d85e5] | 184 | /* Load block group to check */
|
---|
[304faab] | 185 | ext4_block_group_ref_t *bg_ref;
|
---|
[38542dc] | 186 | int rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
| 187 | if (rc != EOK)
|
---|
[304faab] | 188 | return rc;
|
---|
[38542dc] | 189 |
|
---|
[304faab] | 190 | ext4_block_group_t *bg = bg_ref->block_group;
|
---|
[38542dc] | 191 |
|
---|
[06d85e5] | 192 | /* Read necessary values for algorithm */
|
---|
[304faab] | 193 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
|
---|
| 194 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
|
---|
| 195 | uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
|
---|
[38542dc] | 196 |
|
---|
[06d85e5] | 197 | /* Check if this block group is good candidate for allocation */
|
---|
[cd1cc4e6] | 198 | if ((free_inodes >= avg_free_inodes) && (free_blocks > 0)) {
|
---|
[06d85e5] | 199 | /* Load block with bitmap */
|
---|
[38542dc] | 200 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
| 201 | bg_ref->block_group, sb);
|
---|
| 202 |
|
---|
[304faab] | 203 | block_t *bitmap_block;
|
---|
[38542dc] | 204 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
| 205 | BLOCK_FLAGS_NONE);
|
---|
[2f591127] | 206 | if (rc != EOK) {
|
---|
| 207 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[304faab] | 208 | return rc;
|
---|
[2f591127] | 209 | }
|
---|
[38542dc] | 210 |
|
---|
[06d85e5] | 211 | /* Try to allocate i-node in the bitmap */
|
---|
[304faab] | 212 | uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
[cd1cc4e6] | 213 | uint32_t index_in_group;
|
---|
[38542dc] | 214 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
| 215 | 0, &index_in_group, inodes_in_group);
|
---|
| 216 |
|
---|
[06d85e5] | 217 | /* Block group has not any free i-node */
|
---|
[1d69c69] | 218 | if (rc == ENOSPC) {
|
---|
[e5a1ace3] | 219 | rc = block_put(bitmap_block);
|
---|
| 220 | if (rc != EOK) {
|
---|
| 221 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 222 | return rc;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 226 | if (rc != EOK)
|
---|
| 227 | return rc;
|
---|
| 228 |
|
---|
[2f591127] | 229 | bgid++;
|
---|
[1d69c69] | 230 | continue;
|
---|
| 231 | }
|
---|
[38542dc] | 232 |
|
---|
[06d85e5] | 233 | /* Free i-node found, save the bitmap */
|
---|
[304faab] | 234 | bitmap_block->dirty = true;
|
---|
[38542dc] | 235 |
|
---|
[304faab] | 236 | rc = block_put(bitmap_block);
|
---|
[2f591127] | 237 | if (rc != EOK) {
|
---|
| 238 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[304faab] | 239 | return rc;
|
---|
[2f591127] | 240 | }
|
---|
[38542dc] | 241 |
|
---|
[06d85e5] | 242 | /* Modify filesystem counters */
|
---|
[304faab] | 243 | free_inodes--;
|
---|
| 244 | ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
|
---|
[38542dc] | 245 |
|
---|
[06d85e5] | 246 | /* Increment used directories counter */
|
---|
[304faab] | 247 | if (is_dir) {
|
---|
[cd1cc4e6] | 248 | used_dirs++;
|
---|
[304faab] | 249 | ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
|
---|
| 250 | }
|
---|
[38542dc] | 251 |
|
---|
[e25d78c] | 252 | /* Decrease unused inodes count */
|
---|
| 253 | if (ext4_block_group_has_flag(bg,
|
---|
[38542dc] | 254 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
---|
[e25d78c] | 255 | uint32_t unused =
|
---|
[38542dc] | 256 | ext4_block_group_get_itable_unused(bg, sb);
|
---|
| 257 |
|
---|
[e25d78c] | 258 | uint32_t inodes_in_group =
|
---|
[38542dc] | 259 | ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
| 260 |
|
---|
[e25d78c] | 261 | uint32_t free = inodes_in_group - unused;
|
---|
[38542dc] | 262 |
|
---|
[e25d78c] | 263 | if (index_in_group >= free) {
|
---|
| 264 | unused = inodes_in_group - (index_in_group + 1);
|
---|
| 265 | ext4_block_group_set_itable_unused(bg, sb, unused);
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
[38542dc] | 268 |
|
---|
[06d85e5] | 269 | /* Save modified block group */
|
---|
[304faab] | 270 | bg_ref->dirty = true;
|
---|
[38542dc] | 271 |
|
---|
[304faab] | 272 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
[38542dc] | 273 | if (rc != EOK)
|
---|
[847f2cb] | 274 | return rc;
|
---|
[38542dc] | 275 |
|
---|
[06d85e5] | 276 | /* Update superblock */
|
---|
[304faab] | 277 | sb_free_inodes--;
|
---|
| 278 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
[38542dc] | 279 |
|
---|
[06d85e5] | 280 | /* Compute the absolute i-nodex number */
|
---|
[cd1cc4e6] | 281 | *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
|
---|
[38542dc] | 282 |
|
---|
[304faab] | 283 | return EOK;
|
---|
| 284 | }
|
---|
[38542dc] | 285 |
|
---|
[06d85e5] | 286 | /* Block group not modified, put it and jump to the next block group */
|
---|
[e5a1ace3] | 287 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 288 | if (rc != EOK)
|
---|
| 289 | return rc;
|
---|
| 290 |
|
---|
[cd1cc4e6] | 291 | ++bgid;
|
---|
[304faab] | 292 | }
|
---|
[38542dc] | 293 |
|
---|
[304faab] | 294 | return ENOSPC;
|
---|
| 295 | }
|
---|
[bf66ef4] | 296 |
|
---|
| 297 | /**
|
---|
| 298 | * @}
|
---|
[38542dc] | 299 | */
|
---|