[bf66ef4] | 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 |
|
---|
| 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 |
|
---|
[3d4fd2c] | 42 | static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
|
---|
| 43 | uint32_t inode)
|
---|
| 44 | {
|
---|
| 45 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 46 | return (inode - 1) % inodes_per_group;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[cd1cc4e6] | 49 | static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
|
---|
| 50 | uint32_t index, uint32_t bgid)
|
---|
| 51 | {
|
---|
| 52 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 53 | return bgid * inodes_per_group + (index + 1);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[3d4fd2c] | 56 | static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
|
---|
| 57 | uint32_t inode)
|
---|
| 58 | {
|
---|
| 59 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 60 | return (inode - 1) / inodes_per_group;
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
[304faab] | 65 | int ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
|
---|
[3d4fd2c] | 66 | {
|
---|
| 67 | int rc;
|
---|
[d9bbe45] | 68 |
|
---|
[304faab] | 69 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 70 |
|
---|
| 71 | uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
|
---|
[3d4fd2c] | 72 |
|
---|
| 73 | ext4_block_group_ref_t *bg_ref;
|
---|
| 74 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 75 | if (rc != EOK) {
|
---|
| 76 | return rc;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
[304faab] | 80 | bg_ref->block_group, sb);
|
---|
[3d4fd2c] | 81 | block_t *bitmap_block;
|
---|
[304faab] | 82 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
[3d4fd2c] | 83 | if (rc != EOK) {
|
---|
| 84 | return rc;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[304faab] | 87 | uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
|
---|
[3d4fd2c] | 88 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
| 89 | bitmap_block->dirty = true;
|
---|
| 90 |
|
---|
| 91 | rc = block_put(bitmap_block);
|
---|
| 92 | if (rc != EOK) {
|
---|
| 93 | // Error in saving bitmap
|
---|
| 94 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 95 | return rc;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[304faab] | 98 | // if inode is directory, decrement used directories count
|
---|
| 99 | if (is_dir) {
|
---|
[e5f8762] | 100 | uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
|
---|
[304faab] | 101 | bg_ref->block_group, sb);
|
---|
[e5f8762] | 102 | bg_used_dirs--;
|
---|
| 103 | ext4_block_group_set_used_dirs_count(
|
---|
[304faab] | 104 | bg_ref->block_group, sb, bg_used_dirs);
|
---|
[e5f8762] | 105 | }
|
---|
| 106 |
|
---|
[3d4fd2c] | 107 | // Update block group free inodes count
|
---|
| 108 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
|
---|
[304faab] | 109 | bg_ref->block_group, sb);
|
---|
[3d4fd2c] | 110 | free_inodes++;
|
---|
| 111 | ext4_block_group_set_free_inodes_count(bg_ref->block_group,
|
---|
[304faab] | 112 | sb, free_inodes);
|
---|
[1ac1ab4] | 113 |
|
---|
[81092ce] | 114 | if (ext4_block_group_has_flag(bg_ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
|
---|
| 115 | uint32_t unused_inodes = ext4_block_group_get_itable_unused(
|
---|
| 116 | bg_ref->block_group, sb);
|
---|
| 117 | unused_inodes++;
|
---|
| 118 | ext4_block_group_set_itable_unused(bg_ref->block_group, sb, unused_inodes);
|
---|
| 119 | }
|
---|
[1ac1ab4] | 120 |
|
---|
[3d4fd2c] | 121 | bg_ref->dirty = true;
|
---|
| 122 |
|
---|
| 123 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 124 | if (rc != EOK) {
|
---|
| 125 | return rc;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[304faab] | 128 | // Update superblock free inodes count
|
---|
| 129 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
| 130 | sb_free_inodes++;
|
---|
| 131 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
| 132 |
|
---|
[3d4fd2c] | 133 | return EOK;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[304faab] | 136 | int ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
|
---|
| 137 | {
|
---|
| 138 | int rc;
|
---|
| 139 |
|
---|
| 140 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 141 |
|
---|
| 142 | uint32_t bgid = 0;
|
---|
| 143 | uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
|
---|
[cd1cc4e6] | 144 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
| 145 | uint32_t avg_free_inodes = sb_free_inodes / bg_count;
|
---|
[304faab] | 146 |
|
---|
| 147 | while (bgid < bg_count) {
|
---|
| 148 |
|
---|
| 149 | ext4_block_group_ref_t *bg_ref;
|
---|
| 150 | rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
| 151 | if (rc != EOK) {
|
---|
| 152 | return rc;
|
---|
| 153 | }
|
---|
| 154 | ext4_block_group_t *bg = bg_ref->block_group;
|
---|
| 155 |
|
---|
| 156 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
|
---|
| 157 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
|
---|
| 158 | uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
|
---|
| 159 |
|
---|
[cd1cc4e6] | 160 | if ((free_inodes >= avg_free_inodes) && (free_blocks > 0)) {
|
---|
[304faab] | 161 |
|
---|
[cd1cc4e6] | 162 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
[304faab] | 163 | bg_ref->block_group, sb);
|
---|
| 164 |
|
---|
| 165 | block_t *bitmap_block;
|
---|
| 166 | rc = block_get(&bitmap_block, fs->device,
|
---|
| 167 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 168 | if (rc != EOK) {
|
---|
| 169 | return rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | // Alloc bit
|
---|
| 173 | uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
[cd1cc4e6] | 174 | uint32_t index_in_group;
|
---|
| 175 | rc = ext4_bitmap_find_free_bit_and_set(
|
---|
| 176 | bitmap_block->data, 0, &index_in_group, inodes_in_group);
|
---|
[1d69c69] | 177 |
|
---|
| 178 | // TODO check
|
---|
| 179 | if (rc == ENOSPC) {
|
---|
| 180 | block_put(bitmap_block);
|
---|
| 181 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 182 | continue;
|
---|
| 183 | }
|
---|
[304faab] | 184 |
|
---|
| 185 | bitmap_block->dirty = true;
|
---|
| 186 |
|
---|
| 187 | rc = block_put(bitmap_block);
|
---|
| 188 | if (rc != EOK) {
|
---|
| 189 | return rc;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | // Modify filesystem counters
|
---|
| 193 | free_inodes--;
|
---|
| 194 | ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
|
---|
| 195 |
|
---|
[81092ce] | 196 | if (ext4_block_group_has_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
|
---|
| 197 | uint16_t unused_inodes = ext4_block_group_get_itable_unused(bg, sb);
|
---|
| 198 | unused_inodes--;
|
---|
| 199 | ext4_block_group_set_itable_unused(bg, sb, unused_inodes);
|
---|
| 200 | }
|
---|
[1ac1ab4] | 201 |
|
---|
[304faab] | 202 | if (is_dir) {
|
---|
[cd1cc4e6] | 203 | used_dirs++;
|
---|
[304faab] | 204 | ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | bg_ref->dirty = true;
|
---|
| 208 |
|
---|
| 209 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 210 | if (rc != EOK) {
|
---|
| 211 | // TODO
|
---|
[cd1cc4e6] | 212 | EXT4FS_DBG("ERRRRR");
|
---|
[304faab] | 213 | }
|
---|
| 214 |
|
---|
| 215 | sb_free_inodes--;
|
---|
| 216 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
| 217 |
|
---|
[cd1cc4e6] | 218 | *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
|
---|
| 219 |
|
---|
[304faab] | 220 | return EOK;
|
---|
| 221 |
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[cd1cc4e6] | 224 | // Not modified
|
---|
| 225 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 226 | ++bgid;
|
---|
[304faab] | 227 | }
|
---|
| 228 |
|
---|
| 229 | return ENOSPC;
|
---|
| 230 | }
|
---|
[bf66ef4] | 231 |
|
---|
| 232 | /**
|
---|
| 233 | * @}
|
---|
| 234 | */
|
---|