source: mainline/uspace/lib/ext4/libext4_balloc.c@ 22fb7ab

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

libext4: every allocation group contains the number of free blocks, use this information to avoid to scan the bitmap if the group is full.

  • Property mode set to 100644
File size: 19.5 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
137/** Free continuous set of blocks.
138 *
139 * @param inode_ref Inode, where the blocks are allocated
140 * @param first First block to release
141 * @param count Number of blocks to release
142 *
143 */
144int ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
145 uint32_t first, uint32_t count)
146{
147 ext4_filesystem_t *fs = inode_ref->fs;
148 ext4_superblock_t *sb = fs->superblock;
149
150 /* Compute indexes */
151 uint32_t block_group_first =
152 ext4_balloc_get_bgid_of_block(sb, first);
153 uint32_t block_group_last =
154 ext4_balloc_get_bgid_of_block(sb, first + count - 1);
155
156 assert(block_group_first == block_group_last);
157
158 /* Load block group reference */
159 ext4_block_group_ref_t *bg_ref;
160 int rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
161 if (rc != EOK)
162 return rc;
163
164 uint32_t index_in_group_first =
165 ext4_filesystem_blockaddr2_index_in_group(sb, first);
166
167 /* Load block with bitmap */
168 uint32_t bitmap_block_addr =
169 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
170
171 block_t *bitmap_block;
172 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
173 if (rc != EOK) {
174 ext4_filesystem_put_block_group_ref(bg_ref);
175 return rc;
176 }
177
178 /* Modify bitmap */
179 ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
180 bitmap_block->dirty = true;
181
182 /* Release block with bitmap */
183 rc = block_put(bitmap_block);
184 if (rc != EOK) {
185 /* Error in saving bitmap */
186 ext4_filesystem_put_block_group_ref(bg_ref);
187 return rc;
188 }
189
190 uint32_t block_size = ext4_superblock_get_block_size(sb);
191
192 /* Update superblock free blocks count */
193 uint32_t sb_free_blocks =
194 ext4_superblock_get_free_blocks_count(sb);
195 sb_free_blocks += count;
196 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
197
198 /* Update inode blocks count */
199 uint64_t ino_blocks =
200 ext4_inode_get_blocks_count(sb, inode_ref->inode);
201 ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
202 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
203 inode_ref->dirty = true;
204
205 /* Update block group free blocks count */
206 uint32_t free_blocks =
207 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
208 free_blocks += count;
209 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
210 sb, free_blocks);
211 bg_ref->dirty = true;
212
213 /* Release block group reference */
214 return ext4_filesystem_put_block_group_ref(bg_ref);
215}
216
217/** Compute first block for data in block group.
218 *
219 * @param sb Pointer to superblock
220 * @param bg Pointer to block group
221 * @param bgid Index of block group
222 *
223 * @return Absolute block index of first block
224 *
225 */
226uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
227 ext4_block_group_ref_t *bg_ref)
228{
229 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
230 uint32_t inode_table_first_block =
231 ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
232 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
233 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
234 uint32_t block_size = ext4_superblock_get_block_size(sb);
235 uint32_t inode_table_bytes;
236
237 if (bg_ref->index < block_group_count - 1) {
238 inode_table_bytes = inodes_per_group * inode_table_item_size;
239 } else {
240 /* Last block group could be smaller */
241 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
242 inode_table_bytes =
243 (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
244 inode_table_item_size;
245 }
246
247 uint32_t inode_table_blocks = inode_table_bytes / block_size;
248
249 if (inode_table_bytes % block_size)
250 inode_table_blocks++;
251
252 return inode_table_first_block + inode_table_blocks;
253}
254
255/** Compute 'goal' for allocation algorithm.
256 *
257 * @param inode_ref Reference to inode, to allocate block for
258 *
259 * @return Goal block number
260 *
261 */
262static int ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref, uint32_t *goal)
263{
264 *goal = 0;
265 ext4_superblock_t *sb = inode_ref->fs->superblock;
266
267 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
268 uint32_t block_size = ext4_superblock_get_block_size(sb);
269 uint32_t inode_block_count = inode_size / block_size;
270
271 if (inode_size % block_size != 0)
272 inode_block_count++;
273
274 /* If inode has some blocks, get last block address + 1 */
275 if (inode_block_count > 0) {
276 int rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
277 inode_block_count - 1, goal);
278 if (rc != EOK)
279 return rc;
280
281 if (goal != 0) {
282 (*goal)++;
283 return EOK;
284 }
285
286 /* If goal == 0, sparse file -> continue */
287 }
288
289 /* Identify block group of inode */
290 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
291 uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
292 block_size = ext4_superblock_get_block_size(sb);
293
294 /* Load block group reference */
295 ext4_block_group_ref_t *bg_ref;
296 int rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
297 block_group, &bg_ref);
298 if (rc != EOK)
299 return rc;
300
301 /* Compute indexes */
302 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
303 uint32_t inode_table_first_block =
304 ext4_block_group_get_inode_table_first_block(bg_ref->block_group, sb);
305 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
306 uint32_t inode_table_bytes;
307
308 /* Check for last block group */
309 if (block_group < block_group_count - 1) {
310 inode_table_bytes = inodes_per_group * inode_table_item_size;
311 } else {
312 /* Last block group could be smaller */
313 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
314 inode_table_bytes =
315 (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
316 inode_table_item_size;
317 }
318
319 uint32_t inode_table_blocks = inode_table_bytes / block_size;
320
321 if (inode_table_bytes % block_size)
322 inode_table_blocks++;
323
324 *goal = inode_table_first_block + inode_table_blocks;
325
326 return ext4_filesystem_put_block_group_ref(bg_ref);
327}
328
329/** Data block allocation algorithm.
330 *
331 * @param inode_ref Inode to allocate block for
332 * @param fblock Allocated block address
333 *
334 * @return Error code
335 *
336 */
337int ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
338{
339 uint32_t allocated_block = 0;
340
341 uint32_t bitmap_block_addr;
342 block_t *bitmap_block;
343 uint32_t rel_block_idx = 0;
344 uint32_t free_blocks;
345 uint32_t goal;
346
347 /* Find GOAL */
348 int rc = ext4_balloc_find_goal(inode_ref, &goal);
349 if (rc != EOK)
350 return rc;
351
352 ext4_superblock_t *sb = inode_ref->fs->superblock;
353
354 /* Load block group number for goal and relative index */
355 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
356 uint32_t index_in_group =
357 ext4_filesystem_blockaddr2_index_in_group(sb, goal);
358
359 /* Load block group reference */
360 ext4_block_group_ref_t *bg_ref;
361 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
362 block_group, &bg_ref);
363 if (rc != EOK)
364 return rc;
365
366 free_blocks =
367 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
368 if (free_blocks == 0) {
369 /* This group has no free blocks */
370 goto goal_failed;
371 }
372
373 /* Compute indexes */
374 uint32_t first_in_group =
375 ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
376
377 uint32_t first_in_group_index =
378 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
379
380 if (index_in_group < first_in_group_index)
381 index_in_group = first_in_group_index;
382
383 /* Load block with bitmap */
384 bitmap_block_addr =
385 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
386
387 rc = block_get(&bitmap_block, inode_ref->fs->device,
388 bitmap_block_addr, BLOCK_FLAGS_NONE);
389 if (rc != EOK) {
390 ext4_filesystem_put_block_group_ref(bg_ref);
391 return rc;
392 }
393
394 /* Check if goal is free */
395 if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
396 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
397 bitmap_block->dirty = true;
398 rc = block_put(bitmap_block);
399 if (rc != EOK) {
400 ext4_filesystem_put_block_group_ref(bg_ref);
401 return rc;
402 }
403
404 allocated_block =
405 ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
406 block_group);
407
408 goto success;
409 }
410
411 uint32_t blocks_in_group =
412 ext4_superblock_get_blocks_in_group(sb, block_group);
413
414 uint32_t end_idx = (index_in_group + 63) & ~63;
415 if (end_idx > blocks_in_group)
416 end_idx = blocks_in_group;
417
418 /* Try to find free block near to goal */
419 for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
420 ++tmp_idx) {
421 if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
422 ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
423 bitmap_block->dirty = true;
424 rc = block_put(bitmap_block);
425 if (rc != EOK)
426 return rc;
427
428 allocated_block =
429 ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
430 block_group);
431
432 goto success;
433 }
434 }
435
436 /* Find free BYTE in bitmap */
437 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
438 index_in_group, &rel_block_idx, blocks_in_group);
439 if (rc == EOK) {
440 bitmap_block->dirty = true;
441 rc = block_put(bitmap_block);
442 if (rc != EOK)
443 return rc;
444
445 allocated_block =
446 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
447 block_group);
448
449 goto success;
450 }
451
452 /* Find free bit in bitmap */
453 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
454 index_in_group, &rel_block_idx, blocks_in_group);
455 if (rc == EOK) {
456 bitmap_block->dirty = true;
457 rc = block_put(bitmap_block);
458 if (rc != EOK)
459 return rc;
460
461 allocated_block =
462 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
463 block_group);
464
465 goto success;
466 }
467
468 /* No free block found yet */
469 rc = block_put(bitmap_block);
470 if (rc != EOK) {
471 ext4_filesystem_put_block_group_ref(bg_ref);
472 return rc;
473 }
474
475goal_failed:
476
477 rc = ext4_filesystem_put_block_group_ref(bg_ref);
478 if (rc != EOK)
479 return rc;
480
481 /* Try other block groups */
482 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
483
484 uint32_t bgid = (block_group + 1) % block_group_count;
485 uint32_t count = block_group_count;
486
487 while (count > 0) {
488 rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
489 &bg_ref);
490 if (rc != EOK)
491 return rc;
492
493 free_blocks =
494 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
495 if (free_blocks == 0) {
496 /* This group has no free blocks */
497 goto next_group;
498 }
499
500 /* Load block with bitmap */
501 bitmap_block_addr =
502 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
503
504 rc = block_get(&bitmap_block, inode_ref->fs->device,
505 bitmap_block_addr, 0);
506 if (rc != EOK) {
507 ext4_filesystem_put_block_group_ref(bg_ref);
508 return rc;
509 }
510
511 /* Compute indexes */
512 first_in_group =
513 ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
514 index_in_group =
515 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
516 blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
517
518 first_in_group_index =
519 ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
520
521 if (index_in_group < first_in_group_index)
522 index_in_group = first_in_group_index;
523
524 /* Try to find free byte in bitmap */
525 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
526 index_in_group, &rel_block_idx, blocks_in_group);
527 if (rc == EOK) {
528 bitmap_block->dirty = true;
529 rc = block_put(bitmap_block);
530 if (rc != EOK) {
531 ext4_filesystem_put_block_group_ref(bg_ref);
532 return rc;
533 }
534
535 allocated_block =
536 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
537 bgid);
538
539 goto success;
540 }
541
542 /* Try to find free bit in bitmap */
543 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
544 index_in_group, &rel_block_idx, blocks_in_group);
545 if (rc == EOK) {
546 bitmap_block->dirty = true;
547 rc = block_put(bitmap_block);
548 if (rc != EOK) {
549 ext4_filesystem_put_block_group_ref(bg_ref);
550 return rc;
551 }
552
553 allocated_block =
554 ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
555 bgid);
556
557 goto success;
558 }
559
560 rc = block_put(bitmap_block);
561 if (rc != EOK) {
562 ext4_filesystem_put_block_group_ref(bg_ref);
563 return rc;
564 }
565
566next_group:
567 rc = ext4_filesystem_put_block_group_ref(bg_ref);
568 if (rc != EOK)
569 return rc;
570
571 /* Goto next group */
572 bgid = (bgid + 1) % block_group_count;
573 count--;
574 }
575
576 return ENOSPC;
577
578success:
579 /* Empty command - because of syntax */
580 ;
581
582 uint32_t block_size = ext4_superblock_get_block_size(sb);
583
584 /* Update superblock free blocks count */
585 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
586 sb_free_blocks--;
587 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
588
589 /* Update inode blocks (different block size!) count */
590 uint64_t ino_blocks =
591 ext4_inode_get_blocks_count(sb, inode_ref->inode);
592 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
593 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
594 inode_ref->dirty = true;
595
596 /* Update block group free blocks count */
597 uint32_t bg_free_blocks =
598 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
599 bg_free_blocks--;
600 ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
601 bg_free_blocks);
602 bg_ref->dirty = true;
603
604 rc = ext4_filesystem_put_block_group_ref(bg_ref);
605
606 *fblock = allocated_block;
607 return rc;
608}
609
610/** Try to allocate concrete block.
611 *
612 * @param inode_ref Inode to allocate block for
613 * @param fblock Block address to allocate
614 * @param free Output value - if target block is free
615 *
616 * @return Error code
617 *
618 */
619int ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
620 bool *free)
621{
622 int rc;
623
624 ext4_filesystem_t *fs = inode_ref->fs;
625 ext4_superblock_t *sb = fs->superblock;
626
627 /* Compute indexes */
628 uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, fblock);
629 uint32_t index_in_group =
630 ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
631
632 /* Load block group reference */
633 ext4_block_group_ref_t *bg_ref;
634 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
635 if (rc != EOK)
636 return rc;
637
638 /* Load block with bitmap */
639 uint32_t bitmap_block_addr =
640 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
641 block_t *bitmap_block;
642 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
643 if (rc != EOK) {
644 ext4_filesystem_put_block_group_ref(bg_ref);
645 return rc;
646 }
647
648 /* Check if block is free */
649 *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
650
651 /* Allocate block if possible */
652 if (*free) {
653 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
654 bitmap_block->dirty = true;
655 }
656
657 /* Release block with bitmap */
658 rc = block_put(bitmap_block);
659 if (rc != EOK) {
660 /* Error in saving bitmap */
661 ext4_filesystem_put_block_group_ref(bg_ref);
662 return rc;
663 }
664
665 /* If block is not free, return */
666 if (!(*free))
667 goto terminate;
668
669 uint32_t block_size = ext4_superblock_get_block_size(sb);
670
671 /* Update superblock free blocks count */
672 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
673 sb_free_blocks--;
674 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
675
676 /* Update inode blocks count */
677 uint64_t ino_blocks =
678 ext4_inode_get_blocks_count(sb, inode_ref->inode);
679 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
680 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
681 inode_ref->dirty = true;
682
683 /* Update block group free blocks count */
684 uint32_t free_blocks =
685 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
686 free_blocks--;
687 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
688 sb, free_blocks);
689 bg_ref->dirty = true;
690
691terminate:
692 return ext4_filesystem_put_block_group_ref(bg_ref);
693}
694
695/**
696 * @}
697 */
Note: See TracBrowser for help on using the repository browser.