source: mainline/uspace/lib/ext4/libext4_balloc.c@ 60c409c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 60c409c was d76973c, checked in by Maurizio Lombardi <m.lombardi85@…>, 11 years ago

libext4: introduce support for flexible block groups.

With FLEX_BG, several block groups are tied together as one single group,
the first group of the flex_bg contains the block/inode bitmaps and the inode tables of all the other groups.
This improves metadata locality and frees up a lot of contiguous free space for extents.

  • modified ext4_balloc_free_blocks() so it can free extents spanning 2 or more block groups
  • with FLEX_BG the inode table, the bitmap and inode blocks of a group can reside on a different group.
    • the ext4_balloc_get_first_data_block_in_group() function has been modified to return a correct result in such a case.
    • improve the ext4_filesystem_init_block_bitmap() for the flex_bg case.
  • added the ext4_filesystem_blockaddr2group() function that given a block number returns the id of the group it belongs to.
  • added the ext4_filesystem_bg_get_itable_size() functions that given a group id number returns the size in blocks of its inode table.
  • Property mode set to 100644
File size: 20.6 KB
Line 
1/*
2 * Copyright (c) 2012 Frantisek Princ
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext4
30 * @{
31 */
32/**
33 * @file libext4_balloc.c
34 * @brief Physical block allocator.
35 */
36
37#include <errno.h>
38#include <sys/types.h>
39#include "libext4.h"
40
41/** Compute number of block group from block address.
42 *
43 * @param sb Superblock pointer.
44 * @param block_addr Absolute address of block.
45 *
46 * @return Block group index
47 *
48 */
49static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb,
50 uint32_t block_addr)
51{
52 uint32_t blocks_per_group =
53 ext4_superblock_get_blocks_per_group(sb);
54 uint32_t first_block =
55 ext4_superblock_get_first_data_block(sb);
56
57 /* First block == 0 or 1 */
58 if (first_block == 0)
59 return block_addr / blocks_per_group;
60 else
61 return (block_addr - 1) / blocks_per_group;
62}
63
64/** Free block.
65 *
66 * @param inode_ref Inode, where the block is allocated
67 * @param block_addr Absolute block address to free
68 *
69 * @return Error code
70 *
71 */
72int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
73{
74 ext4_filesystem_t *fs = inode_ref->fs;
75 ext4_superblock_t *sb = fs->superblock;
76
77 /* Compute indexes */
78 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
79 uint32_t index_in_group =
80 ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
81
82 /* Load block group reference */
83 ext4_block_group_ref_t *bg_ref;
84 int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
85 if (rc != EOK)
86 return rc;
87
88 /* Load block with bitmap */
89 uint32_t bitmap_block_addr =
90 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
91 block_t *bitmap_block;
92 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
93 if (rc != EOK) {
94 ext4_filesystem_put_block_group_ref(bg_ref);
95 return rc;
96 }
97
98 /* Modify bitmap */
99 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
100 bitmap_block->dirty = true;
101
102 /* Release block with bitmap */
103 rc = block_put(bitmap_block);
104 if (rc != EOK) {
105 /* Error in saving bitmap */
106 ext4_filesystem_put_block_group_ref(bg_ref);
107 return rc;
108 }
109
110 uint32_t block_size = ext4_superblock_get_block_size(sb);
111
112 /* Update superblock free blocks count */
113 uint32_t sb_free_blocks =
114 ext4_superblock_get_free_blocks_count(sb);
115 sb_free_blocks++;
116 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
117
118 /* Update inode blocks count */
119 uint64_t ino_blocks =
120 ext4_inode_get_blocks_count(sb, inode_ref->inode);
121 ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
122 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
123 inode_ref->dirty = true;
124
125 /* Update block group free blocks count */
126 uint32_t free_blocks =
127 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
128 free_blocks++;
129 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
130 sb, free_blocks);
131 bg_ref->dirty = true;
132
133 /* Release block group reference */
134 return ext4_filesystem_put_block_group_ref(bg_ref);
135}
136
137static int ext4_balloc_free_blocks_internal(ext4_inode_ref_t *inode_ref,
138 uint32_t first, uint32_t count)
139{
140 ext4_filesystem_t *fs = inode_ref->fs;
141 ext4_superblock_t *sb = fs->superblock;
142
143 /* Compute indexes */
144 uint32_t block_group_first =
145 ext4_balloc_get_bgid_of_block(sb, first);
146 uint32_t block_group_last =
147 ext4_balloc_get_bgid_of_block(sb, first + count - 1);
148
149 assert(block_group_first == block_group_last);
150
151 /* Load block group reference */
152 ext4_block_group_ref_t *bg_ref;
153 int rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
154 if (rc != EOK)
155 return rc;
156
157 uint32_t index_in_group_first =
158 ext4_filesystem_blockaddr2_index_in_group(sb, first);
159
160 /* Load block with bitmap */
161 uint32_t bitmap_block_addr =
162 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
163
164 block_t *bitmap_block;
165 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
166 if (rc != EOK) {
167 ext4_filesystem_put_block_group_ref(bg_ref);
168 return rc;
169 }
170
171 /* Modify bitmap */
172 ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
173 bitmap_block->dirty = true;
174
175 /* Release block with bitmap */
176 rc = block_put(bitmap_block);
177 if (rc != EOK) {
178 /* Error in saving bitmap */
179 ext4_filesystem_put_block_group_ref(bg_ref);
180 return rc;
181 }
182
183 uint32_t block_size = ext4_superblock_get_block_size(sb);
184
185 /* Update superblock free blocks count */
186 uint32_t sb_free_blocks =
187 ext4_superblock_get_free_blocks_count(sb);
188 sb_free_blocks += count;
189 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
190
191 /* Update inode blocks count */
192 uint64_t ino_blocks =
193 ext4_inode_get_blocks_count(sb, inode_ref->inode);
194 ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
195 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
196 inode_ref->dirty = true;
197
198 /* Update block group free blocks count */
199 uint32_t free_blocks =
200 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
201 free_blocks += count;
202 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
203 sb, free_blocks);
204 bg_ref->dirty = true;
205
206 /* Release block group reference */
207 return ext4_filesystem_put_block_group_ref(bg_ref);
208}
209
210/** Free continuous set of blocks.
211 *
212 * @param inode_ref Inode, where the blocks are allocated
213 * @param first First block to release
214 * @param count Number of blocks to release
215 *
216 */
217int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
218 uint32_t first, uint32_t count)
219{
220 int r;
221 uint32_t gid;
222 uint64_t limit;
223 ext4_filesystem_t *fs = inode_ref->fs;
224 ext4_superblock_t *sb = fs->superblock;
225
226 while (count) {
227 gid = ext4_filesystem_blockaddr2group(sb, first);
228 limit = ext4_filesystem_index_in_group2blockaddr(sb, 0,
229 gid + 1);
230
231 if ((first + count) >= limit) {
232 /* This extent spans over 2 or more block groups,
233 * we'll break it into smaller parts.
234 */
235 uint32_t s = limit - first;
236
237 r = ext4_balloc_free_blocks_internal(inode_ref,
238 first, s);
239 if (r != EOK)
240 return r;
241
242 first = limit;
243 count -= s;
244 } else {
245 return ext4_balloc_free_blocks_internal(inode_ref,
246 first, count);
247 }
248 }
249
250 return EOK;
251}
252
253/** Compute first block for data in block group.
254 *
255 * @param sb Pointer to superblock
256 * @param bg Pointer to block group
257 * @param bgid Index of block group
258 *
259 * @return Absolute block index of first block
260 *
261 */
262uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
263 ext4_block_group_ref_t *bg_ref)
264{
265 uint32_t r;
266 uint64_t itable = ext4_block_group_get_inode_table_first_block(
267 bg_ref->block_group, sb);
268 uint32_t itable_sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref);
269
270 if (!ext4_superblock_has_feature_incompatible(sb,
271 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
272 /* If we are not using FLEX_BG, the first data block
273 * is always after the inode table.
274 */
275 r = itable + itable_sz;
276 return ext4_filesystem_blockaddr2_index_in_group(sb, r);
277 }
278
279 uint64_t bbmap = ext4_block_group_get_block_bitmap(bg_ref->block_group,
280 sb);
281 uint64_t ibmap = ext4_block_group_get_inode_bitmap(bg_ref->block_group,
282 sb);
283
284 r = ext4_filesystem_bg_get_backup_blocks(bg_ref);
285
286 if (ext4_filesystem_blockaddr2group(sb, bbmap) == bg_ref->index)
287 bbmap = ext4_filesystem_blockaddr2_index_in_group(sb, bbmap);
288 else
289 bbmap = -1; /* Invalid */
290
291 if (ext4_filesystem_blockaddr2group(sb, ibmap) == bg_ref->index)
292 ibmap = ext4_filesystem_blockaddr2_index_in_group(sb, ibmap);
293 else
294 ibmap = -1;
295
296 while (1) {
297 uint64_t r_abs = ext4_filesystem_index_in_group2blockaddr(sb,
298 r, bg_ref->index);
299
300 if (r == bbmap || r == ibmap)
301 r++;
302 else if (r_abs >= itable && r_abs < itable_sz) {
303 r = ext4_filesystem_blockaddr2_index_in_group(sb,
304 itable + itable_sz);
305 } else
306 break;
307 }
308
309 return r;
310}
311
312/** Compute 'goal' for allocation algorithm.
313 *
314 * @param inode_ref Reference to inode, to allocate block for
315 *
316 * @return Goal block number
317 *
318 */
319static int ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref, uint32_t *goal)
320{
321 *goal = 0;
322 ext4_superblock_t *sb = inode_ref->fs->superblock;
323
324 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
325 uint32_t block_size = ext4_superblock_get_block_size(sb);
326 uint32_t inode_block_count = inode_size / block_size;
327
328 if (inode_size % block_size != 0)
329 inode_block_count++;
330
331 /* If inode has some blocks, get last block address + 1 */
332 if (inode_block_count > 0) {
333 int rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
334 inode_block_count - 1, goal);
335 if (rc != EOK)
336 return rc;
337
338 if (goal != 0) {
339 (*goal)++;
340 return EOK;
341 }
342
343 /* If goal == 0, sparse file -> continue */
344 }
345
346 /* Identify block group of inode */
347 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
348 uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
349 block_size = ext4_superblock_get_block_size(sb);
350
351 /* Load block group reference */
352 ext4_block_group_ref_t *bg_ref;
353 int rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
354 block_group, &bg_ref);
355 if (rc != EOK)
356 return rc;
357
358 /* Compute indexes */
359 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
360 uint32_t inode_table_first_block =
361 ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
362 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
363 uint32_t inode_table_bytes;
364
365 /* Check for last block group */
366 if (block_group < block_group_count - 1) {
367 inode_table_bytes = inodes_per_group * inode_table_item_size;
368 } else {
369 /* Last block group could be smaller */
370 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
371 inode_table_bytes =
372 (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
373 inode_table_item_size;
374 }
375
376 uint32_t inode_table_blocks = inode_table_bytes / block_size;
377
378 if (inode_table_bytes % block_size)
379 inode_table_blocks++;
380
381 *goal = inode_table_first_block + inode_table_blocks;
382
383 return ext4_filesystem_put_block_group_ref(bg_ref);
384}
385
386/** Data block allocation algorithm.
387 *
388 * @param inode_ref Inode to allocate block for
389 * @param fblock Allocated block address
390 *
391 * @return Error code
392 *
393 */
394int ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
395{
396 uint32_t allocated_block = 0;
397
398 uint32_t bitmap_block_addr;
399 block_t *bitmap_block;
400 uint32_t rel_block_idx = 0;
401 uint32_t free_blocks;
402 uint32_t goal;
403
404 /* Find GOAL */
405 int rc = ext4_balloc_find_goal(inode_ref, &goal);
406 if (rc != EOK)
407 return rc;
408
409 ext4_superblock_t *sb = inode_ref->fs->superblock;
410
411 /* Load block group number for goal and relative index */
412 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
413 uint32_t index_in_group =
414 ext4_filesystem_blockaddr2_index_in_group(sb, goal);
415
416 /* Load block group reference */
417 ext4_block_group_ref_t *bg_ref;
418 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
419 block_group, &bg_ref);
420 if (rc != EOK)
421 return rc;
422
423 free_blocks =
424 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
425 if (free_blocks == 0) {
426 /* This group has no free blocks */
427 goto goal_failed;
428 }
429
430 /* Compute indexes */
431 uint32_t first_in_group =
432 ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
433
434 uint32_t first_in_group_index =
435 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
436
437 if (index_in_group < first_in_group_index)
438 index_in_group = first_in_group_index;
439
440 /* Load block with bitmap */
441 bitmap_block_addr =
442 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
443
444 rc = block_get(&bitmap_block, inode_ref->fs->device,
445 bitmap_block_addr, BLOCK_FLAGS_NONE);
446 if (rc != EOK) {
447 ext4_filesystem_put_block_group_ref(bg_ref);
448 return rc;
449 }
450
451 /* Check if goal is free */
452 if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
453 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
454 bitmap_block->dirty = true;
455 rc = block_put(bitmap_block);
456 if (rc != EOK) {
457 ext4_filesystem_put_block_group_ref(bg_ref);
458 return rc;
459 }
460
461 allocated_block =
462 ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
463 block_group);
464
465 goto success;
466 }
467
468 uint32_t blocks_in_group =
469 ext4_superblock_get_blocks_in_group(sb, block_group);
470
471 uint32_t end_idx = (index_in_group + 63) & ~63;
472 if (end_idx > blocks_in_group)
473 end_idx = blocks_in_group;
474
475 /* Try to find free block near to goal */
476 for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
477 ++tmp_idx) {
478 if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
479 ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
480 bitmap_block->dirty = true;
481 rc = block_put(bitmap_block);
482 if (rc != EOK)
483 return rc;
484
485 allocated_block =
486 ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
487 block_group);
488
489 goto success;
490 }
491 }
492
493 /* Find free BYTE in bitmap */
494 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
495 index_in_group, &rel_block_idx, blocks_in_group);
496 if (rc == EOK) {
497 bitmap_block->dirty = true;
498 rc = block_put(bitmap_block);
499 if (rc != EOK)
500 return rc;
501
502 allocated_block =
503 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
504 block_group);
505
506 goto success;
507 }
508
509 /* Find free bit in bitmap */
510 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
511 index_in_group, &rel_block_idx, blocks_in_group);
512 if (rc == EOK) {
513 bitmap_block->dirty = true;
514 rc = block_put(bitmap_block);
515 if (rc != EOK)
516 return rc;
517
518 allocated_block =
519 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
520 block_group);
521
522 goto success;
523 }
524
525 /* No free block found yet */
526 rc = block_put(bitmap_block);
527 if (rc != EOK) {
528 ext4_filesystem_put_block_group_ref(bg_ref);
529 return rc;
530 }
531
532goal_failed:
533
534 rc = ext4_filesystem_put_block_group_ref(bg_ref);
535 if (rc != EOK)
536 return rc;
537
538 /* Try other block groups */
539 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
540
541 uint32_t bgid = (block_group + 1) % block_group_count;
542 uint32_t count = block_group_count;
543
544 while (count > 0) {
545 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
546 &bg_ref);
547 if (rc != EOK)
548 return rc;
549
550 free_blocks =
551 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
552 if (free_blocks == 0) {
553 /* This group has no free blocks */
554 goto next_group;
555 }
556
557 /* Load block with bitmap */
558 bitmap_block_addr =
559 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
560
561 rc = block_get(&bitmap_block, inode_ref->fs->device,
562 bitmap_block_addr, 0);
563 if (rc != EOK) {
564 ext4_filesystem_put_block_group_ref(bg_ref);
565 return rc;
566 }
567
568 /* Compute indexes */
569 first_in_group =
570 ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
571 index_in_group =
572 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
573 blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
574
575 first_in_group_index =
576 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
577
578 if (index_in_group < first_in_group_index)
579 index_in_group = first_in_group_index;
580
581 /* Try to find free byte in bitmap */
582 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
583 index_in_group, &rel_block_idx, blocks_in_group);
584 if (rc == EOK) {
585 bitmap_block->dirty = true;
586 rc = block_put(bitmap_block);
587 if (rc != EOK) {
588 ext4_filesystem_put_block_group_ref(bg_ref);
589 return rc;
590 }
591
592 allocated_block =
593 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
594 bgid);
595
596 goto success;
597 }
598
599 /* Try to find free bit in bitmap */
600 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
601 index_in_group, &rel_block_idx, blocks_in_group);
602 if (rc == EOK) {
603 bitmap_block->dirty = true;
604 rc = block_put(bitmap_block);
605 if (rc != EOK) {
606 ext4_filesystem_put_block_group_ref(bg_ref);
607 return rc;
608 }
609
610 allocated_block =
611 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
612 bgid);
613
614 goto success;
615 }
616
617 rc = block_put(bitmap_block);
618 if (rc != EOK) {
619 ext4_filesystem_put_block_group_ref(bg_ref);
620 return rc;
621 }
622
623next_group:
624 rc = ext4_filesystem_put_block_group_ref(bg_ref);
625 if (rc != EOK)
626 return rc;
627
628 /* Goto next group */
629 bgid = (bgid + 1) % block_group_count;
630 count--;
631 }
632
633 return ENOSPC;
634
635success:
636 /* Empty command - because of syntax */
637 ;
638
639 uint32_t block_size = ext4_superblock_get_block_size(sb);
640
641 /* Update superblock free blocks count */
642 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
643 sb_free_blocks--;
644 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
645
646 /* Update inode blocks (different block size!) count */
647 uint64_t ino_blocks =
648 ext4_inode_get_blocks_count(sb, inode_ref->inode);
649 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
650 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
651 inode_ref->dirty = true;
652
653 /* Update block group free blocks count */
654 uint32_t bg_free_blocks =
655 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
656 bg_free_blocks--;
657 ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
658 bg_free_blocks);
659 bg_ref->dirty = true;
660
661 rc = ext4_filesystem_put_block_group_ref(bg_ref);
662
663 *fblock = allocated_block;
664 return rc;
665}
666
667/** Try to allocate concrete block.
668 *
669 * @param inode_ref Inode to allocate block for
670 * @param fblock Block address to allocate
671 * @param free Output value - if target block is free
672 *
673 * @return Error code
674 *
675 */
676int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
677 bool *free)
678{
679 int rc;
680
681 ext4_filesystem_t *fs = inode_ref->fs;
682 ext4_superblock_t *sb = fs->superblock;
683
684 /* Compute indexes */
685 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
686 uint32_t index_in_group =
687 ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
688
689 /* Load block group reference */
690 ext4_block_group_ref_t *bg_ref;
691 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
692 if (rc != EOK)
693 return rc;
694
695 /* Load block with bitmap */
696 uint32_t bitmap_block_addr =
697 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
698 block_t *bitmap_block;
699 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
700 if (rc != EOK) {
701 ext4_filesystem_put_block_group_ref(bg_ref);
702 return rc;
703 }
704
705 /* Check if block is free */
706 *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
707
708 /* Allocate block if possible */
709 if (*free) {
710 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
711 bitmap_block->dirty = true;
712 }
713
714 /* Release block with bitmap */
715 rc = block_put(bitmap_block);
716 if (rc != EOK) {
717 /* Error in saving bitmap */
718 ext4_filesystem_put_block_group_ref(bg_ref);
719 return rc;
720 }
721
722 /* If block is not free, return */
723 if (!(*free))
724 goto terminate;
725
726 uint32_t block_size = ext4_superblock_get_block_size(sb);
727
728 /* Update superblock free blocks count */
729 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
730 sb_free_blocks--;
731 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
732
733 /* Update inode blocks count */
734 uint64_t ino_blocks =
735 ext4_inode_get_blocks_count(sb, inode_ref->inode);
736 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
737 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
738 inode_ref->dirty = true;
739
740 /* Update block group free blocks count */
741 uint32_t free_blocks =
742 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
743 free_blocks--;
744 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
745 sb, free_blocks);
746 bg_ref->dirty = true;
747
748terminate:
749 return ext4_filesystem_put_block_group_ref(bg_ref);
750}
751
752/**
753 * @}
754 */
Note: See TracBrowser for help on using the repository browser.