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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6ba36a0 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
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 ext4_superblock_t *sb = fs->superblock;
179
180 uint32_t bgid = 0;
181 uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
182 uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
183 uint32_t avg_free_inodes = sb_free_inodes / bg_count;
184
185 /* Try to find free i-node in all block groups */
186 while (bgid < bg_count) {
187 /* Load block group to check */
188 ext4_block_group_ref_t *bg_ref;
189 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
190 if (rc != EOK)
191 return rc;
192
193 ext4_block_group_t *bg = bg_ref->block_group;
194
195 /* Read necessary values for algorithm */
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);
199
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)) {
214 /* Load block with bitmap */
215 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
216 bg_ref->block_group, sb);
217
218 block_t *bitmap_block;
219 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
220 BLOCK_FLAGS_NONE);
221 if (rc != EOK) {
222 ext4_filesystem_put_block_group_ref(bg_ref);
223 return rc;
224 }
225
226 /* Try to allocate i-node in the bitmap */
227 uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
228 uint32_t index_in_group;
229 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
230 0, &index_in_group, inodes_in_group);
231
232 /* Block group has not any free i-node */
233 if (rc == ENOSPC) {
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
244 bgid++;
245 continue;
246 }
247
248 /* Free i-node found, save the bitmap */
249 bitmap_block->dirty = true;
250
251 rc = block_put(bitmap_block);
252 if (rc != EOK) {
253 ext4_filesystem_put_block_group_ref(bg_ref);
254 return rc;
255 }
256
257 /* Modify filesystem counters */
258 free_inodes--;
259 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
260
261 /* Increment used directories counter */
262 if (is_dir) {
263 used_dirs++;
264 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
265 }
266
267 /* Decrease unused inodes count */
268 if (ext4_block_group_has_flag(bg,
269 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
270 uint32_t unused =
271 ext4_block_group_get_itable_unused(bg, sb);
272
273 uint32_t inodes_in_group =
274 ext4_superblock_get_inodes_in_group(sb, bgid);
275
276 uint32_t free = inodes_in_group - unused;
277
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 }
283
284 /* Save modified block group */
285 bg_ref->dirty = true;
286
287 rc = ext4_filesystem_put_block_group_ref(bg_ref);
288 if (rc != EOK)
289 return rc;
290
291 /* Update superblock */
292 sb_free_inodes--;
293 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
294
295 /* Compute the absolute i-nodex number */
296 *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
297
298 return EOK;
299 }
300
301 /* Block group not modified, put it and jump to the next block group */
302 rc = ext4_filesystem_put_block_group_ref(bg_ref);
303 if (rc != EOK)
304 return rc;
305
306 ++bgid;
307 }
308
309 return ENOSPC;
310}
311
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
407/**
408 * @}
409 */
Note: See TracBrowser for help on using the repository browser.