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
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * Copyright (c) 2012 Frantisek Princ
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 * @{
32 */
33/**
34 * @file ialloc.c
35 * @brief I-node (de)allocation operations.
36 */
37
38#include <errno.h>
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"
45
46/** Convert i-node number to relative index in block group.
47 *
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 *
53 */
54static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
55 uint32_t inode)
56{
57 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
58 return (inode - 1) % inodes_per_group;
59}
60
61/** Convert relative index of i-node to absolute i-node number.
62 *
63 * @param sb Superblock
64 * @param inode Index to be converted
65 *
66 * @return Absolute number of the i-node
67 *
68 */
69static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
70 uint32_t index, uint32_t bgid)
71{
72 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
73 return bgid * inodes_per_group + (index + 1);
74}
75
76/** Compute block group number from the i-node number.
77 *
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 *
83 */
84static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
85 uint32_t inode)
86{
87 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
88 return (inode - 1) / inodes_per_group;
89}
90
91/** Free i-node number and modify filesystem data structers.
92 *
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 *
97 */
98errno_t ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
99{
100 ext4_superblock_t *sb = fs->superblock;
101
102 /* Compute index of block group and load it */
103 uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
104
105 ext4_block_group_ref_t *bg_ref;
106 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
107 if (rc != EOK)
108 return rc;
109
110 /* Load i-node bitmap */
111 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
112 bg_ref->block_group, sb);
113 block_t *bitmap_block;
114 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
115 BLOCK_FLAGS_NONE);
116 if (rc != EOK)
117 return rc;
118
119 /* Free i-node in the bitmap */
120 uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
121 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
122 bitmap_block->dirty = true;
123
124 /* Put back the block with bitmap */
125 rc = block_put(bitmap_block);
126 if (rc != EOK) {
127 /* Error in saving bitmap */
128 ext4_filesystem_put_block_group_ref(bg_ref);
129 return rc;
130 }
131
132 /* If released i-node is a directory, decrement used directories count */
133 if (is_dir) {
134 uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
135 bg_ref->block_group, sb);
136 bg_used_dirs--;
137 ext4_block_group_set_used_dirs_count(bg_ref->block_group, sb,
138 bg_used_dirs);
139 }
140
141 /* Update block group free inodes count */
142 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
143 bg_ref->block_group, sb);
144 free_inodes++;
145 ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
146 free_inodes);
147
148 bg_ref->dirty = true;
149
150 /* Put back the modified block group */
151 rc = ext4_filesystem_put_block_group_ref(bg_ref);
152 if (rc != EOK)
153 return rc;
154
155 /* Update superblock free inodes count */
156 uint32_t sb_free_inodes =
157 ext4_superblock_get_free_inodes_count(sb);
158 sb_free_inodes++;
159 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
160
161 return EOK;
162}
163
164/** I-node allocation algorithm.
165 *
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
174 *
175 */
176errno_t ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
177{
178 int pick_first_free = 0;
179 ext4_superblock_t *sb = fs->superblock;
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);
184
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;
190
191 /* Try to find free i-node in all block groups */
192 while (bgid < bg_count) {
193 /* Load block group to check */
194 ext4_block_group_ref_t *bg_ref;
195 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
196 if (rc != EOK)
197 return rc;
198
199 ext4_block_group_t *bg = bg_ref->block_group;
200
201 /* Read necessary values for algorithm */
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);
205
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 */
218 if (((free_inodes >= avg_free_inodes) || (bgid == bg_count - 1)
219 || pick_first_free) && (free_blocks > 0)) {
220 /* Load block with bitmap */
221 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
222 bg_ref->block_group, sb);
223
224 block_t *bitmap_block;
225 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
226 BLOCK_FLAGS_NONE);
227 if (rc != EOK) {
228 ext4_filesystem_put_block_group_ref(bg_ref);
229 return rc;
230 }
231
232 /* Try to allocate i-node in the bitmap */
233 uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
234 uint32_t index_in_group;
235 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
236 0, &index_in_group, inodes_in_group);
237
238 /* Block group has not any free i-node */
239 if (rc == ENOSPC) {
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
250 bgid++;
251 continue;
252 }
253
254 /* Free i-node found, save the bitmap */
255 bitmap_block->dirty = true;
256
257 rc = block_put(bitmap_block);
258 if (rc != EOK) {
259 ext4_filesystem_put_block_group_ref(bg_ref);
260 return rc;
261 }
262
263 /* Modify filesystem counters */
264 free_inodes--;
265 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
266
267 /* Increment used directories counter */
268 if (is_dir) {
269 used_dirs++;
270 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
271 }
272
273 /* Decrease unused inodes count */
274 if (ext4_block_group_has_flag(bg,
275 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
276 uint32_t unused =
277 ext4_block_group_get_itable_unused(bg, sb);
278
279 uint32_t inodes_in_group =
280 ext4_superblock_get_inodes_in_group(sb, bgid);
281
282 uint32_t free = inodes_in_group - unused;
283
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 }
289
290 /* Save modified block group */
291 bg_ref->dirty = true;
292
293 rc = ext4_filesystem_put_block_group_ref(bg_ref);
294 if (rc != EOK)
295 return rc;
296
297 /* Update superblock */
298 sb_free_inodes--;
299 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
300
301 /* Compute the absolute i-nodex number */
302 *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
303
304 return EOK;
305 }
306
307 /* Block group not modified, put it and jump to the next block group */
308 rc = ext4_filesystem_put_block_group_ref(bg_ref);
309 if (rc != EOK)
310 return rc;
311
312 ++bgid;
313 }
314
315 /* Try again with less strict conditions */
316 if (pick_first_free == 0) {
317 pick_first_free = 1;
318 goto retry;
319 }
320
321 return ENOSPC;
322}
323
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
419/**
420 * @}
421 */
Note: See TracBrowser for help on using the repository browser.