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

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

Basic Ext4 filesystem creation (can only create Ext2-old, 1K blocks).

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