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