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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4d7bf35 was 4d7bf35, checked in by Maurizio Lombardi <mlombard@…>, 7 years ago

libext4: fix ext4_ialloc_alloc_inode() to be less picky

If can't find an ideal candidate, restart the search and pick the first
block group with free_block > 0 before giving up with ENOSPC.

  • Property mode set to 100644
File size: 11.8 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{
[4d7bf35]178 int pick_first_free = 0;
[304faab]179 ext4_superblock_t *sb = fs->superblock;
[4d7bf35]180 uint32_t bgid;
181 uint32_t sb_free_inodes;
182 uint32_t avg_free_inodes;
183 uint32_t const bg_count = ext4_superblock_get_block_group_count(sb);
[a35b458]184
[4d7bf35]185retry:
186
187 bgid = 0;
188 sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
189 avg_free_inodes = sb_free_inodes / bg_count;
[a35b458]190
[06d85e5]191 /* Try to find free i-node in all block groups */
[304faab]192 while (bgid < bg_count) {
[06d85e5]193 /* Load block group to check */
[304faab]194 ext4_block_group_ref_t *bg_ref;
[b7fd2a0]195 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
[38542dc]196 if (rc != EOK)
[304faab]197 return rc;
[a35b458]198
[304faab]199 ext4_block_group_t *bg = bg_ref->block_group;
[a35b458]200
[06d85e5]201 /* Read necessary values for algorithm */
[304faab]202 uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
203 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
204 uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
[a35b458]205
[8a45707d]206 /*
207 * Check if this block group is a good candidate
208 * for allocation.
209 *
210 * The criterion is based on the average number
211 * of free inodes, unless we examine the last block
212 * group. In that case the last block group might
213 * have less than the average number of free inodes,
214 * but it still needs to be taken as a candidate
215 * because the previous block groups have zero free
216 * blocks.
217 */
[4d7bf35]218 if (((free_inodes >= avg_free_inodes) || (bgid == bg_count - 1)
219 || pick_first_free) && (free_blocks > 0)) {
[06d85e5]220 /* Load block with bitmap */
[38542dc]221 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
222 bg_ref->block_group, sb);
[a35b458]223
[304faab]224 block_t *bitmap_block;
[38542dc]225 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
226 BLOCK_FLAGS_NONE);
[2f591127]227 if (rc != EOK) {
228 ext4_filesystem_put_block_group_ref(bg_ref);
[304faab]229 return rc;
[2f591127]230 }
[a35b458]231
[06d85e5]232 /* Try to allocate i-node in the bitmap */
[304faab]233 uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
[cd1cc4e6]234 uint32_t index_in_group;
[38542dc]235 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
236 0, &index_in_group, inodes_in_group);
[a35b458]237
[06d85e5]238 /* Block group has not any free i-node */
[1d69c69]239 if (rc == ENOSPC) {
[e5a1ace3]240 rc = block_put(bitmap_block);
241 if (rc != EOK) {
242 ext4_filesystem_put_block_group_ref(bg_ref);
243 return rc;
244 }
245
246 rc = ext4_filesystem_put_block_group_ref(bg_ref);
247 if (rc != EOK)
248 return rc;
249
[2f591127]250 bgid++;
[1d69c69]251 continue;
252 }
[a35b458]253
[06d85e5]254 /* Free i-node found, save the bitmap */
[304faab]255 bitmap_block->dirty = true;
[a35b458]256
[304faab]257 rc = block_put(bitmap_block);
[2f591127]258 if (rc != EOK) {
259 ext4_filesystem_put_block_group_ref(bg_ref);
[304faab]260 return rc;
[2f591127]261 }
[a35b458]262
[06d85e5]263 /* Modify filesystem counters */
[304faab]264 free_inodes--;
265 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
[a35b458]266
[06d85e5]267 /* Increment used directories counter */
[304faab]268 if (is_dir) {
[cd1cc4e6]269 used_dirs++;
[304faab]270 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
271 }
[a35b458]272
[e25d78c]273 /* Decrease unused inodes count */
274 if (ext4_block_group_has_flag(bg,
[38542dc]275 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
[e25d78c]276 uint32_t unused =
[38542dc]277 ext4_block_group_get_itable_unused(bg, sb);
[a35b458]278
[e25d78c]279 uint32_t inodes_in_group =
[38542dc]280 ext4_superblock_get_inodes_in_group(sb, bgid);
[a35b458]281
[e25d78c]282 uint32_t free = inodes_in_group - unused;
[a35b458]283
[e25d78c]284 if (index_in_group >= free) {
285 unused = inodes_in_group - (index_in_group + 1);
286 ext4_block_group_set_itable_unused(bg, sb, unused);
287 }
288 }
[a35b458]289
[06d85e5]290 /* Save modified block group */
[304faab]291 bg_ref->dirty = true;
[a35b458]292
[304faab]293 rc = ext4_filesystem_put_block_group_ref(bg_ref);
[38542dc]294 if (rc != EOK)
[847f2cb]295 return rc;
[a35b458]296
[06d85e5]297 /* Update superblock */
[304faab]298 sb_free_inodes--;
299 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
[a35b458]300
[06d85e5]301 /* Compute the absolute i-nodex number */
[cd1cc4e6]302 *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
[a35b458]303
[304faab]304 return EOK;
305 }
[a35b458]306
[06d85e5]307 /* Block group not modified, put it and jump to the next block group */
[e5a1ace3]308 rc = ext4_filesystem_put_block_group_ref(bg_ref);
309 if (rc != EOK)
310 return rc;
311
[cd1cc4e6]312 ++bgid;
[304faab]313 }
[a35b458]314
[4d7bf35]315 /* Try again with less strict conditions */
316 if (pick_first_free == 0) {
317 pick_first_free = 1;
318 goto retry;
319 }
320
[304faab]321 return ENOSPC;
322}
[bf66ef4]323
[aab85d90]324/** Allocate a specific I-node.
325 *
326 * @param fs Filesystem to allocate i-node on
327 * @param inode I-node to allocate
328 * @param is_dir Flag if allocated i-node will be file or directory
329 *
330 * @return Error code
331 *
332 */
333errno_t ext4_ialloc_alloc_this_inode(ext4_filesystem_t *fs, uint32_t inode,
334 bool is_dir)
335{
336 ext4_superblock_t *sb = fs->superblock;
337
338 uint32_t bgid = ext4_ialloc_get_bgid_of_inode(sb, inode);
339 uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
340
341 /* Load block group */
342 ext4_block_group_ref_t *bg_ref;
343 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
344 if (rc != EOK)
345 return rc;
346
347 ext4_block_group_t *bg = bg_ref->block_group;
348
349 /* Read necessary values for algorithm */
350 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
351 uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
352
353 /* Load block with bitmap */
354 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
355 bg_ref->block_group, sb);
356
357 block_t *bitmap_block;
358 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
359 BLOCK_FLAGS_NONE);
360 if (rc != EOK) {
361 ext4_filesystem_put_block_group_ref(bg_ref);
362 return rc;
363 }
364
365 /* Allocate i-node in the bitmap */
366 uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, inode);
367 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
368
369 /* Save the bitmap */
370 bitmap_block->dirty = true;
371
372 rc = block_put(bitmap_block);
373 if (rc != EOK) {
374 ext4_filesystem_put_block_group_ref(bg_ref);
375 return rc;
376 }
377
378 /* Modify filesystem counters */
379 free_inodes--;
380 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
381
382 /* Increment used directories counter */
383 if (is_dir) {
384 used_dirs++;
385 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
386 }
387
388 /* Decrease unused inodes count */
389 if (ext4_block_group_has_flag(bg,
390 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
391 uint32_t unused =
392 ext4_block_group_get_itable_unused(bg, sb);
393
394 uint32_t inodes_in_group =
395 ext4_superblock_get_inodes_in_group(sb, bgid);
396
397 uint32_t free = inodes_in_group - unused;
398
399 if (index_in_group >= free) {
400 unused = inodes_in_group - (index_in_group + 1);
401 ext4_block_group_set_itable_unused(bg, sb, unused);
402 }
403 }
404
405 /* Save modified block group */
406 bg_ref->dirty = true;
407
408 rc = ext4_filesystem_put_block_group_ref(bg_ref);
409 if (rc != EOK)
410 return rc;
411
412 /* Update superblock */
413 sb_free_inodes--;
414 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
415
416 return EOK;
417}
418
[bf66ef4]419/**
420 * @}
[38542dc]421 */
Note: See TracBrowser for help on using the repository browser.