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 balloc.c
|
---|
35 | * @brief Physical block allocator.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include "ext4/balloc.h"
|
---|
42 | #include "ext4/bitmap.h"
|
---|
43 | #include "ext4/block_group.h"
|
---|
44 | #include "ext4/filesystem.h"
|
---|
45 | #include "ext4/inode.h"
|
---|
46 | #include "ext4/superblock.h"
|
---|
47 | #include "ext4/types.h"
|
---|
48 |
|
---|
49 | /** Free block.
|
---|
50 | *
|
---|
51 | * @param inode_ref Inode, where the block is allocated
|
---|
52 | * @param block_addr Absolute block address to free
|
---|
53 | *
|
---|
54 | * @return Error code
|
---|
55 | *
|
---|
56 | */
|
---|
57 | errno_t ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
|
---|
58 | {
|
---|
59 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
60 | ext4_superblock_t *sb = fs->superblock;
|
---|
61 |
|
---|
62 | /* Compute indexes */
|
---|
63 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, block_addr);
|
---|
64 | uint32_t index_in_group =
|
---|
65 | ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
|
---|
66 |
|
---|
67 | /* Load block group reference */
|
---|
68 | ext4_block_group_ref_t *bg_ref;
|
---|
69 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
70 | if (rc != EOK)
|
---|
71 | return rc;
|
---|
72 |
|
---|
73 | /* Load block with bitmap */
|
---|
74 | uint32_t bitmap_block_addr =
|
---|
75 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
76 | block_t *bitmap_block;
|
---|
77 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
78 | if (rc != EOK) {
|
---|
79 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
80 | return rc;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* Modify bitmap */
|
---|
84 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
85 | bitmap_block->dirty = true;
|
---|
86 |
|
---|
87 | /* Release block with bitmap */
|
---|
88 | rc = block_put(bitmap_block);
|
---|
89 | if (rc != EOK) {
|
---|
90 | /* Error in saving bitmap */
|
---|
91 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
96 |
|
---|
97 | /* Update superblock free blocks count */
|
---|
98 | uint32_t sb_free_blocks =
|
---|
99 | ext4_superblock_get_free_blocks_count(sb);
|
---|
100 | sb_free_blocks++;
|
---|
101 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
102 |
|
---|
103 | /* Update inode blocks count */
|
---|
104 | uint64_t ino_blocks =
|
---|
105 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
106 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
107 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
108 | inode_ref->dirty = true;
|
---|
109 |
|
---|
110 | /* Update block group free blocks count */
|
---|
111 | uint32_t free_blocks =
|
---|
112 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
113 | free_blocks++;
|
---|
114 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
115 | sb, free_blocks);
|
---|
116 | bg_ref->dirty = true;
|
---|
117 |
|
---|
118 | /* Release block group reference */
|
---|
119 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
120 | }
|
---|
121 |
|
---|
122 | static errno_t ext4_balloc_free_blocks_internal(ext4_inode_ref_t *inode_ref,
|
---|
123 | uint32_t first, uint32_t count)
|
---|
124 | {
|
---|
125 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
126 | ext4_superblock_t *sb = fs->superblock;
|
---|
127 |
|
---|
128 | /* Compute indexes */
|
---|
129 | uint32_t block_group_first = ext4_filesystem_blockaddr2group(sb,
|
---|
130 | first);
|
---|
131 | uint32_t block_group_last = ext4_filesystem_blockaddr2group(sb,
|
---|
132 | first + count - 1);
|
---|
133 |
|
---|
134 | assert(block_group_first == block_group_last);
|
---|
135 |
|
---|
136 | /* Load block group reference */
|
---|
137 | ext4_block_group_ref_t *bg_ref;
|
---|
138 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
|
---|
139 | if (rc != EOK)
|
---|
140 | return rc;
|
---|
141 |
|
---|
142 | uint32_t index_in_group_first =
|
---|
143 | ext4_filesystem_blockaddr2_index_in_group(sb, first);
|
---|
144 |
|
---|
145 | /* Load block with bitmap */
|
---|
146 | uint32_t bitmap_block_addr =
|
---|
147 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
148 |
|
---|
149 | block_t *bitmap_block;
|
---|
150 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
151 | if (rc != EOK) {
|
---|
152 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* Modify bitmap */
|
---|
157 | ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
|
---|
158 | bitmap_block->dirty = true;
|
---|
159 |
|
---|
160 | /* Release block with bitmap */
|
---|
161 | rc = block_put(bitmap_block);
|
---|
162 | if (rc != EOK) {
|
---|
163 | /* Error in saving bitmap */
|
---|
164 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
169 |
|
---|
170 | /* Update superblock free blocks count */
|
---|
171 | uint32_t sb_free_blocks =
|
---|
172 | ext4_superblock_get_free_blocks_count(sb);
|
---|
173 | sb_free_blocks += count;
|
---|
174 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
175 |
|
---|
176 | /* Update inode blocks count */
|
---|
177 | uint64_t ino_blocks =
|
---|
178 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
179 | ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
|
---|
180 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
181 | inode_ref->dirty = true;
|
---|
182 |
|
---|
183 | /* Update block group free blocks count */
|
---|
184 | uint32_t free_blocks =
|
---|
185 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
186 | free_blocks += count;
|
---|
187 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
188 | sb, free_blocks);
|
---|
189 | bg_ref->dirty = true;
|
---|
190 |
|
---|
191 | /* Release block group reference */
|
---|
192 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Free continuous set of blocks.
|
---|
196 | *
|
---|
197 | * @param inode_ref Inode, where the blocks are allocated
|
---|
198 | * @param first First block to release
|
---|
199 | * @param count Number of blocks to release
|
---|
200 | *
|
---|
201 | */
|
---|
202 | errno_t ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
|
---|
203 | uint32_t first, uint32_t count)
|
---|
204 | {
|
---|
205 | errno_t r;
|
---|
206 | uint32_t gid;
|
---|
207 | uint64_t limit;
|
---|
208 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
209 | ext4_superblock_t *sb = fs->superblock;
|
---|
210 |
|
---|
211 | while (count) {
|
---|
212 | gid = ext4_filesystem_blockaddr2group(sb, first);
|
---|
213 | limit = ext4_filesystem_index_in_group2blockaddr(sb, 0,
|
---|
214 | gid + 1);
|
---|
215 |
|
---|
216 | if ((first + count) >= limit) {
|
---|
217 | /*
|
---|
218 | * This extent spans over 2 or more block groups,
|
---|
219 | * we'll break it into smaller parts.
|
---|
220 | */
|
---|
221 | uint32_t s = limit - first;
|
---|
222 |
|
---|
223 | r = ext4_balloc_free_blocks_internal(inode_ref,
|
---|
224 | first, s);
|
---|
225 | if (r != EOK)
|
---|
226 | return r;
|
---|
227 |
|
---|
228 | first = limit;
|
---|
229 | count -= s;
|
---|
230 | } else {
|
---|
231 | return ext4_balloc_free_blocks_internal(inode_ref,
|
---|
232 | first, count);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | return EOK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Compute first block for data in block group.
|
---|
240 | *
|
---|
241 | * @param sb Pointer to superblock
|
---|
242 | * @param bg Pointer to block group
|
---|
243 | * @param bgid Index of block group
|
---|
244 | *
|
---|
245 | * @return Absolute block index of first block
|
---|
246 | *
|
---|
247 | */
|
---|
248 | uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
|
---|
249 | ext4_block_group_ref_t *bg_ref)
|
---|
250 | {
|
---|
251 | uint32_t r;
|
---|
252 | uint64_t itable = ext4_block_group_get_inode_table_first_block(
|
---|
253 | bg_ref->block_group, sb);
|
---|
254 | uint32_t itable_sz = ext4_filesystem_bg_get_itable_size(sb,
|
---|
255 | bg_ref->index);
|
---|
256 |
|
---|
257 | if (!ext4_superblock_has_feature_incompatible(sb,
|
---|
258 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
|
---|
259 | /*
|
---|
260 | * If we are not using FLEX_BG, the first data block
|
---|
261 | * is always after the inode table.
|
---|
262 | */
|
---|
263 | return itable + itable_sz;
|
---|
264 | }
|
---|
265 |
|
---|
266 | uint32_t flex_group_size = ext4_superblock_get_flex_group_size(sb);
|
---|
267 | if ((bg_ref->index % flex_group_size) == 0) {
|
---|
268 | /* This is the base group */
|
---|
269 | uint32_t i;
|
---|
270 |
|
---|
271 | r = itable + itable_sz;
|
---|
272 |
|
---|
273 | uint32_t total_groups = ext4_superblock_get_block_group_count(sb);
|
---|
274 | for (i = bg_ref->index + 1;
|
---|
275 | i < min(total_groups, bg_ref->index + flex_group_size); ++i) {
|
---|
276 | r += ext4_filesystem_bg_get_itable_size(sb, i);
|
---|
277 | }
|
---|
278 |
|
---|
279 | return r;
|
---|
280 | }
|
---|
281 |
|
---|
282 | uint64_t base_addr = ext4_filesystem_index_in_group2blockaddr(sb, 0,
|
---|
283 | bg_ref->index);
|
---|
284 | uint32_t reserved = ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
---|
285 |
|
---|
286 | return base_addr + reserved;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Compute 'goal' for allocation algorithm.
|
---|
290 | *
|
---|
291 | * @param inode_ref Reference to inode, to allocate block for
|
---|
292 | *
|
---|
293 | * @return Goal block number
|
---|
294 | *
|
---|
295 | */
|
---|
296 | static errno_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref, uint32_t *goal)
|
---|
297 | {
|
---|
298 | *goal = 0;
|
---|
299 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
300 |
|
---|
301 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
302 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
303 | uint32_t inode_block_count = inode_size / block_size;
|
---|
304 |
|
---|
305 | if (inode_size % block_size != 0)
|
---|
306 | inode_block_count++;
|
---|
307 |
|
---|
308 | /* If inode has some blocks, get last block address + 1 */
|
---|
309 | if (inode_block_count > 0) {
|
---|
310 | errno_t rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
|
---|
311 | inode_block_count - 1, goal);
|
---|
312 | if (rc != EOK)
|
---|
313 | return rc;
|
---|
314 |
|
---|
315 | if (goal != 0) {
|
---|
316 | (*goal)++;
|
---|
317 | return EOK;
|
---|
318 | }
|
---|
319 | /* If goal == 0, sparse file -> continue */
|
---|
320 | }
|
---|
321 |
|
---|
322 | /* Identify block group of inode */
|
---|
323 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
324 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
325 |
|
---|
326 | /* Load block group reference */
|
---|
327 | ext4_block_group_ref_t *bg_ref;
|
---|
328 | errno_t rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
329 | block_group, &bg_ref);
|
---|
330 | if (rc != EOK)
|
---|
331 | return rc;
|
---|
332 |
|
---|
333 | *goal = ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
334 |
|
---|
335 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /** Data block allocation algorithm.
|
---|
339 | *
|
---|
340 | * @param inode_ref Inode to allocate block for
|
---|
341 | * @param fblock Allocated block address
|
---|
342 | *
|
---|
343 | * @return Error code
|
---|
344 | *
|
---|
345 | */
|
---|
346 | errno_t ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
347 | {
|
---|
348 | uint32_t allocated_block = 0;
|
---|
349 |
|
---|
350 | uint32_t bitmap_block_addr;
|
---|
351 | block_t *bitmap_block;
|
---|
352 | uint32_t rel_block_idx = 0;
|
---|
353 | uint32_t free_blocks;
|
---|
354 | uint32_t goal;
|
---|
355 | uint32_t block_size;
|
---|
356 |
|
---|
357 | /* Find GOAL */
|
---|
358 | errno_t rc = ext4_balloc_find_goal(inode_ref, &goal);
|
---|
359 | if (rc != EOK)
|
---|
360 | return rc;
|
---|
361 |
|
---|
362 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
363 |
|
---|
364 | /* Load block group number for goal and relative index */
|
---|
365 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, goal);
|
---|
366 | uint32_t index_in_group =
|
---|
367 | ext4_filesystem_blockaddr2_index_in_group(sb, goal);
|
---|
368 |
|
---|
369 | /* Load block group reference */
|
---|
370 | ext4_block_group_ref_t *bg_ref;
|
---|
371 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
372 | block_group, &bg_ref);
|
---|
373 | if (rc != EOK)
|
---|
374 | return rc;
|
---|
375 |
|
---|
376 | free_blocks =
|
---|
377 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
378 | if (free_blocks == 0) {
|
---|
379 | /* This group has no free blocks */
|
---|
380 | goto goal_failed;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /* Compute indexes */
|
---|
384 | uint32_t first_in_group =
|
---|
385 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
386 |
|
---|
387 | uint32_t first_in_group_index =
|
---|
388 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
389 |
|
---|
390 | if (index_in_group < first_in_group_index)
|
---|
391 | index_in_group = first_in_group_index;
|
---|
392 |
|
---|
393 | /* Load block with bitmap */
|
---|
394 | bitmap_block_addr =
|
---|
395 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
396 |
|
---|
397 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
398 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
399 | if (rc != EOK) {
|
---|
400 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
401 | return rc;
|
---|
402 | }
|
---|
403 |
|
---|
404 | /* Check if goal is free */
|
---|
405 | if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
|
---|
406 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
407 | bitmap_block->dirty = true;
|
---|
408 | rc = block_put(bitmap_block);
|
---|
409 | if (rc != EOK) {
|
---|
410 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
411 | return rc;
|
---|
412 | }
|
---|
413 |
|
---|
414 | allocated_block =
|
---|
415 | ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
|
---|
416 | block_group);
|
---|
417 |
|
---|
418 | goto success;
|
---|
419 | }
|
---|
420 |
|
---|
421 | uint32_t blocks_in_group =
|
---|
422 | ext4_superblock_get_blocks_in_group(sb, block_group);
|
---|
423 |
|
---|
424 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
425 | if (end_idx > blocks_in_group)
|
---|
426 | end_idx = blocks_in_group;
|
---|
427 |
|
---|
428 | /* Try to find free block near to goal */
|
---|
429 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
|
---|
430 | ++tmp_idx) {
|
---|
431 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
432 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
433 | bitmap_block->dirty = true;
|
---|
434 | rc = block_put(bitmap_block);
|
---|
435 | if (rc != EOK)
|
---|
436 | return rc;
|
---|
437 |
|
---|
438 | allocated_block =
|
---|
439 | ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
|
---|
440 | block_group);
|
---|
441 |
|
---|
442 | goto success;
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | /* Find free BYTE in bitmap */
|
---|
447 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
448 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
449 | if (rc == EOK) {
|
---|
450 | bitmap_block->dirty = true;
|
---|
451 | rc = block_put(bitmap_block);
|
---|
452 | if (rc != EOK)
|
---|
453 | return rc;
|
---|
454 |
|
---|
455 | allocated_block =
|
---|
456 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
457 | block_group);
|
---|
458 |
|
---|
459 | goto success;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /* Find free bit in bitmap */
|
---|
463 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
464 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
465 | if (rc == EOK) {
|
---|
466 | bitmap_block->dirty = true;
|
---|
467 | rc = block_put(bitmap_block);
|
---|
468 | if (rc != EOK)
|
---|
469 | return rc;
|
---|
470 |
|
---|
471 | allocated_block =
|
---|
472 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
473 | block_group);
|
---|
474 |
|
---|
475 | goto success;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /* No free block found yet */
|
---|
479 | rc = block_put(bitmap_block);
|
---|
480 | if (rc != EOK) {
|
---|
481 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
482 | return rc;
|
---|
483 | }
|
---|
484 |
|
---|
485 | goal_failed:
|
---|
486 |
|
---|
487 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
488 | if (rc != EOK)
|
---|
489 | return rc;
|
---|
490 |
|
---|
491 | /* Try other block groups */
|
---|
492 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
493 |
|
---|
494 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
495 | uint32_t count = block_group_count;
|
---|
496 |
|
---|
497 | while (count > 0) {
|
---|
498 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
|
---|
499 | &bg_ref);
|
---|
500 | if (rc != EOK)
|
---|
501 | return rc;
|
---|
502 |
|
---|
503 | free_blocks =
|
---|
504 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
505 | if (free_blocks == 0) {
|
---|
506 | /* This group has no free blocks */
|
---|
507 | goto next_group;
|
---|
508 | }
|
---|
509 |
|
---|
510 | /* Load block with bitmap */
|
---|
511 | bitmap_block_addr =
|
---|
512 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
513 |
|
---|
514 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
515 | bitmap_block_addr, 0);
|
---|
516 | if (rc != EOK) {
|
---|
517 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
518 | return rc;
|
---|
519 | }
|
---|
520 |
|
---|
521 | /* Compute indexes */
|
---|
522 | first_in_group =
|
---|
523 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
524 | index_in_group =
|
---|
525 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
526 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
---|
527 |
|
---|
528 | first_in_group_index =
|
---|
529 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
530 |
|
---|
531 | if (index_in_group < first_in_group_index)
|
---|
532 | index_in_group = first_in_group_index;
|
---|
533 |
|
---|
534 | /* Try to find free byte in bitmap */
|
---|
535 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
536 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
537 | if (rc == EOK) {
|
---|
538 | bitmap_block->dirty = true;
|
---|
539 | rc = block_put(bitmap_block);
|
---|
540 | if (rc != EOK) {
|
---|
541 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
542 | return rc;
|
---|
543 | }
|
---|
544 |
|
---|
545 | allocated_block =
|
---|
546 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
547 | bgid);
|
---|
548 |
|
---|
549 | goto success;
|
---|
550 | }
|
---|
551 |
|
---|
552 | /* Try to find free bit in bitmap */
|
---|
553 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
554 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
555 | if (rc == EOK) {
|
---|
556 | bitmap_block->dirty = true;
|
---|
557 | rc = block_put(bitmap_block);
|
---|
558 | if (rc != EOK) {
|
---|
559 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
560 | return rc;
|
---|
561 | }
|
---|
562 |
|
---|
563 | allocated_block =
|
---|
564 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
565 | bgid);
|
---|
566 |
|
---|
567 | goto success;
|
---|
568 | }
|
---|
569 |
|
---|
570 | rc = block_put(bitmap_block);
|
---|
571 | if (rc != EOK) {
|
---|
572 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 |
|
---|
576 | next_group:
|
---|
577 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
578 | if (rc != EOK)
|
---|
579 | return rc;
|
---|
580 |
|
---|
581 | /* Goto next group */
|
---|
582 | bgid = (bgid + 1) % block_group_count;
|
---|
583 | count--;
|
---|
584 | }
|
---|
585 |
|
---|
586 | return ENOSPC;
|
---|
587 |
|
---|
588 | success:
|
---|
589 | block_size = ext4_superblock_get_block_size(sb);
|
---|
590 |
|
---|
591 | /* Update superblock free blocks count */
|
---|
592 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
593 | sb_free_blocks--;
|
---|
594 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
595 |
|
---|
596 | /* Update inode blocks (different block size!) count */
|
---|
597 | uint64_t ino_blocks =
|
---|
598 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
599 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
600 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
601 | inode_ref->dirty = true;
|
---|
602 |
|
---|
603 | /* Update block group free blocks count */
|
---|
604 | uint32_t bg_free_blocks =
|
---|
605 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
606 | bg_free_blocks--;
|
---|
607 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
|
---|
608 | bg_free_blocks);
|
---|
609 | bg_ref->dirty = true;
|
---|
610 |
|
---|
611 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
612 |
|
---|
613 | *fblock = allocated_block;
|
---|
614 | return rc;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /** Try to allocate concrete block.
|
---|
618 | *
|
---|
619 | * @param inode_ref Inode to allocate block for
|
---|
620 | * @param fblock Block address to allocate
|
---|
621 | * @param free Output value - if target block is free
|
---|
622 | *
|
---|
623 | * @return Error code
|
---|
624 | *
|
---|
625 | */
|
---|
626 | errno_t ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
|
---|
627 | bool *free)
|
---|
628 | {
|
---|
629 | errno_t rc;
|
---|
630 |
|
---|
631 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
632 | ext4_superblock_t *sb = fs->superblock;
|
---|
633 |
|
---|
634 | /* Compute indexes */
|
---|
635 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, fblock);
|
---|
636 | uint32_t index_in_group =
|
---|
637 | ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
|
---|
638 |
|
---|
639 | /* Load block group reference */
|
---|
640 | ext4_block_group_ref_t *bg_ref;
|
---|
641 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
642 | if (rc != EOK)
|
---|
643 | return rc;
|
---|
644 |
|
---|
645 | /* Load block with bitmap */
|
---|
646 | uint32_t bitmap_block_addr =
|
---|
647 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
648 | block_t *bitmap_block;
|
---|
649 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
650 | if (rc != EOK) {
|
---|
651 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
652 | return rc;
|
---|
653 | }
|
---|
654 |
|
---|
655 | /* Check if block is free */
|
---|
656 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
---|
657 |
|
---|
658 | /* Allocate block if possible */
|
---|
659 | if (*free) {
|
---|
660 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
661 | bitmap_block->dirty = true;
|
---|
662 | }
|
---|
663 |
|
---|
664 | /* Release block with bitmap */
|
---|
665 | rc = block_put(bitmap_block);
|
---|
666 | if (rc != EOK) {
|
---|
667 | /* Error in saving bitmap */
|
---|
668 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
669 | return rc;
|
---|
670 | }
|
---|
671 |
|
---|
672 | /* If block is not free, return */
|
---|
673 | if (!(*free))
|
---|
674 | goto terminate;
|
---|
675 |
|
---|
676 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
677 |
|
---|
678 | /* Update superblock free blocks count */
|
---|
679 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
680 | sb_free_blocks--;
|
---|
681 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
682 |
|
---|
683 | /* Update inode blocks count */
|
---|
684 | uint64_t ino_blocks =
|
---|
685 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
686 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
687 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
688 | inode_ref->dirty = true;
|
---|
689 |
|
---|
690 | /* Update block group free blocks count */
|
---|
691 | uint32_t free_blocks =
|
---|
692 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
693 | free_blocks--;
|
---|
694 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
695 | sb, free_blocks);
|
---|
696 | bg_ref->dirty = true;
|
---|
697 |
|
---|
698 | terminate:
|
---|
699 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
700 | }
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * @}
|
---|
704 | */
|
---|