source: mainline/uspace/lib/ext4/src/ialloc.c@ cc74cb5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc74cb5 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[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/** Convert i-node number to relative index in block group.
47 *
[38542dc]48 * @param sb Superblock
49 * @param inode I-node number to be converted
50 *
51 * @return Index of the i-node in the block group
52 *
[ee3b6150]53 */
[3d4fd2c]54static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
[38542dc]55 uint32_t inode)
[3d4fd2c]56{
57 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
58 return (inode - 1) % inodes_per_group;
59}
60
[ee3b6150]61/** Convert relative index of i-node to absolute i-node number.
62 *
[38542dc]63 * @param sb Superblock
64 * @param inode Index to be converted
65 *
66 * @return Absolute number of the i-node
67 *
[ee3b6150]68 */
[cd1cc4e6]69static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
[38542dc]70 uint32_t index, uint32_t bgid)
[cd1cc4e6]71{
72 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
73 return bgid * inodes_per_group + (index + 1);
74}
75
[ee3b6150]76/** Compute block group number from the i-node number.
77 *
[38542dc]78 * @param sb Superblock
79 * @param inode I-node number to be found the block group for
80 *
81 * @return Block group number computed from i-node number
82 *
[ee3b6150]83 */
[3d4fd2c]84static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
[38542dc]85 uint32_t inode)
[3d4fd2c]86{
87 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
88 return (inode - 1) / inodes_per_group;
89}
90
[ee3b6150]91/** Free i-node number and modify filesystem data structers.
92 *
[38542dc]93 * @param fs Filesystem, where the i-node is located
94 * @param index Index of i-node to be release
95 * @param is_dir Flag us for information whether i-node is directory or not
96 *
[ee3b6150]97 */
[b7fd2a0]98errno_t ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
[3d4fd2c]99{
[304faab]100 ext4_superblock_t *sb = fs->superblock;
[a35b458]101
[06d85e5]102 /* Compute index of block group and load it */
[304faab]103 uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
[a35b458]104
[3d4fd2c]105 ext4_block_group_ref_t *bg_ref;
[b7fd2a0]106 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
[38542dc]107 if (rc != EOK)
[3d4fd2c]108 return rc;
[a35b458]109
[06d85e5]110 /* Load i-node bitmap */
[3d4fd2c]111 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
[38542dc]112 bg_ref->block_group, sb);
[3d4fd2c]113 block_t *bitmap_block;
[38542dc]114 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
115 BLOCK_FLAGS_NONE);
116 if (rc != EOK)
[3d4fd2c]117 return rc;
[a35b458]118
[06d85e5]119 /* Free i-node in the bitmap */
[304faab]120 uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
[3d4fd2c]121 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
122 bitmap_block->dirty = true;
[a35b458]123
[06d85e5]124 /* Put back the block with bitmap */
[3d4fd2c]125 rc = block_put(bitmap_block);
126 if (rc != EOK) {
[06d85e5]127 /* Error in saving bitmap */
[3d4fd2c]128 ext4_filesystem_put_block_group_ref(bg_ref);
129 return rc;
130 }
[a35b458]131
[06d85e5]132 /* If released i-node is a directory, decrement used directories count */
[304faab]133 if (is_dir) {
[e5f8762]134 uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
[38542dc]135 bg_ref->block_group, sb);
[e5f8762]136 bg_used_dirs--;
[38542dc]137 ext4_block_group_set_used_dirs_count(bg_ref->block_group, sb,
138 bg_used_dirs);
[e5f8762]139 }
[a35b458]140
[06d85e5]141 /* Update block group free inodes count */
[3d4fd2c]142 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
[38542dc]143 bg_ref->block_group, sb);
[3d4fd2c]144 free_inodes++;
[38542dc]145 ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
146 free_inodes);
[a35b458]147
[3d4fd2c]148 bg_ref->dirty = true;
[a35b458]149
[06d85e5]150 /* Put back the modified block group */
[3d4fd2c]151 rc = ext4_filesystem_put_block_group_ref(bg_ref);
[38542dc]152 if (rc != EOK)
[3d4fd2c]153 return rc;
[a35b458]154
[06d85e5]155 /* Update superblock free inodes count */
[38542dc]156 uint32_t sb_free_inodes =
157 ext4_superblock_get_free_inodes_count(sb);
[304faab]158 sb_free_inodes++;
159 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
[a35b458]160
[3d4fd2c]161 return EOK;
162}
163
[ee3b6150]164/** I-node allocation algorithm.
165 *
[38542dc]166 * This is more simple algorithm, than Orlov allocator used
167 * in the Linux kernel.
168 *
169 * @param fs Filesystem to allocate i-node on
170 * @param index Output value - allocated i-node number
171 * @param is_dir Flag if allocated i-node will be file or directory
172 *
173 * @return Error code
[ee3b6150]174 *
175 */
[b7fd2a0]176errno_t ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
[304faab]177{
178 ext4_superblock_t *sb = fs->superblock;
[a35b458]179
[304faab]180 uint32_t bgid = 0;
181 uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
[cd1cc4e6]182 uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
183 uint32_t avg_free_inodes = sb_free_inodes / bg_count;
[a35b458]184
[06d85e5]185 /* Try to find free i-node in all block groups */
[304faab]186 while (bgid < bg_count) {
[06d85e5]187 /* Load block group to check */
[304faab]188 ext4_block_group_ref_t *bg_ref;
[b7fd2a0]189 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
[38542dc]190 if (rc != EOK)
[304faab]191 return rc;
[a35b458]192
[304faab]193 ext4_block_group_t *bg = bg_ref->block_group;
[a35b458]194
[06d85e5]195 /* Read necessary values for algorithm */
[304faab]196 uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
197 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
198 uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
[a35b458]199
[8a45707d]200 /*
201 * Check if this block group is a good candidate
202 * for allocation.
203 *
204 * The criterion is based on the average number
205 * of free inodes, unless we examine the last block
206 * group. In that case the last block group might
207 * have less than the average number of free inodes,
208 * but it still needs to be taken as a candidate
209 * because the previous block groups have zero free
210 * blocks.
211 */
212 if (((free_inodes >= avg_free_inodes) || (bgid == bg_count - 1)) &&
213 (free_blocks > 0)) {
[06d85e5]214 /* Load block with bitmap */
[38542dc]215 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
216 bg_ref->block_group, sb);
[a35b458]217
[304faab]218 block_t *bitmap_block;
[38542dc]219 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
220 BLOCK_FLAGS_NONE);
[2f591127]221 if (rc != EOK) {
222 ext4_filesystem_put_block_group_ref(bg_ref);
[304faab]223 return rc;
[2f591127]224 }
[a35b458]225
[06d85e5]226 /* Try to allocate i-node in the bitmap */
[304faab]227 uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
[cd1cc4e6]228 uint32_t index_in_group;
[38542dc]229 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
230 0, &index_in_group, inodes_in_group);
[a35b458]231
[06d85e5]232 /* Block group has not any free i-node */
[1d69c69]233 if (rc == ENOSPC) {
[e5a1ace3]234 rc = block_put(bitmap_block);
235 if (rc != EOK) {
236 ext4_filesystem_put_block_group_ref(bg_ref);
237 return rc;
238 }
239
240 rc = ext4_filesystem_put_block_group_ref(bg_ref);
241 if (rc != EOK)
242 return rc;
243
[2f591127]244 bgid++;
[1d69c69]245 continue;
246 }
[a35b458]247
[06d85e5]248 /* Free i-node found, save the bitmap */
[304faab]249 bitmap_block->dirty = true;
[a35b458]250
[304faab]251 rc = block_put(bitmap_block);
[2f591127]252 if (rc != EOK) {
253 ext4_filesystem_put_block_group_ref(bg_ref);
[304faab]254 return rc;
[2f591127]255 }
[a35b458]256
[06d85e5]257 /* Modify filesystem counters */
[304faab]258 free_inodes--;
259 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
[a35b458]260
[06d85e5]261 /* Increment used directories counter */
[304faab]262 if (is_dir) {
[cd1cc4e6]263 used_dirs++;
[304faab]264 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
265 }
[a35b458]266
[e25d78c]267 /* Decrease unused inodes count */
268 if (ext4_block_group_has_flag(bg,
[38542dc]269 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
[e25d78c]270 uint32_t unused =
[38542dc]271 ext4_block_group_get_itable_unused(bg, sb);
[a35b458]272
[e25d78c]273 uint32_t inodes_in_group =
[38542dc]274 ext4_superblock_get_inodes_in_group(sb, bgid);
[a35b458]275
[e25d78c]276 uint32_t free = inodes_in_group - unused;
[a35b458]277
[e25d78c]278 if (index_in_group >= free) {
279 unused = inodes_in_group - (index_in_group + 1);
280 ext4_block_group_set_itable_unused(bg, sb, unused);
281 }
282 }
[a35b458]283
[06d85e5]284 /* Save modified block group */
[304faab]285 bg_ref->dirty = true;
[a35b458]286
[304faab]287 rc = ext4_filesystem_put_block_group_ref(bg_ref);
[38542dc]288 if (rc != EOK)
[847f2cb]289 return rc;
[a35b458]290
[06d85e5]291 /* Update superblock */
[304faab]292 sb_free_inodes--;
293 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
[a35b458]294
[06d85e5]295 /* Compute the absolute i-nodex number */
[cd1cc4e6]296 *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
[a35b458]297
[304faab]298 return EOK;
299 }
[a35b458]300
[06d85e5]301 /* Block group not modified, put it and jump to the next block group */
[e5a1ace3]302 rc = ext4_filesystem_put_block_group_ref(bg_ref);
303 if (rc != EOK)
304 return rc;
305
[cd1cc4e6]306 ++bgid;
[304faab]307 }
[a35b458]308
[304faab]309 return ENOSPC;
310}
[bf66ef4]311
[aab85d90]312/** Allocate a specific I-node.
313 *
314 * @param fs Filesystem to allocate i-node on
315 * @param inode I-node to allocate
316 * @param is_dir Flag if allocated i-node will be file or directory
317 *
318 * @return Error code
319 *
320 */
321errno_t ext4_ialloc_alloc_this_inode(ext4_filesystem_t *fs, uint32_t inode,
322 bool is_dir)
323{
324 ext4_superblock_t *sb = fs->superblock;
325
326 uint32_t bgid = ext4_ialloc_get_bgid_of_inode(sb, inode);
327 uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
328
329 /* Load block group */
330 ext4_block_group_ref_t *bg_ref;
331 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
332 if (rc != EOK)
333 return rc;
334
335 ext4_block_group_t *bg = bg_ref->block_group;
336
337 /* Read necessary values for algorithm */
338 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
339 uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
340
341 /* Load block with bitmap */
342 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
343 bg_ref->block_group, sb);
344
345 block_t *bitmap_block;
346 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
347 BLOCK_FLAGS_NONE);
348 if (rc != EOK) {
349 ext4_filesystem_put_block_group_ref(bg_ref);
350 return rc;
351 }
352
353 /* Allocate i-node in the bitmap */
354 uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, inode);
355 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
356
357 /* Save the bitmap */
358 bitmap_block->dirty = true;
359
360 rc = block_put(bitmap_block);
361 if (rc != EOK) {
362 ext4_filesystem_put_block_group_ref(bg_ref);
363 return rc;
364 }
365
366 /* Modify filesystem counters */
367 free_inodes--;
368 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
369
370 /* Increment used directories counter */
371 if (is_dir) {
372 used_dirs++;
373 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
374 }
375
376 /* Decrease unused inodes count */
377 if (ext4_block_group_has_flag(bg,
378 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
379 uint32_t unused =
380 ext4_block_group_get_itable_unused(bg, sb);
381
382 uint32_t inodes_in_group =
383 ext4_superblock_get_inodes_in_group(sb, bgid);
384
385 uint32_t free = inodes_in_group - unused;
386
387 if (index_in_group >= free) {
388 unused = inodes_in_group - (index_in_group + 1);
389 ext4_block_group_set_itable_unused(bg, sb, unused);
390 }
391 }
392
393 /* Save modified block group */
394 bg_ref->dirty = true;
395
396 rc = ext4_filesystem_put_block_group_ref(bg_ref);
397 if (rc != EOK)
398 return rc;
399
400 /* Update superblock */
401 sb_free_inodes--;
402 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
403
404 return EOK;
405}
406
[bf66ef4]407/**
408 * @}
[38542dc]409 */
Note: See TracBrowser for help on using the repository browser.