source: mainline/uspace/lib/ext4/src/balloc.c@ 009c485

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

Use proper boolean constant in while loops.

  • Property mode set to 100644
File size: 19.1 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 balloc.c
34 * @brief Physical block allocator.
35 */
36
37#include <errno.h>
38#include <stdbool.h>
39#include <stdint.h>
40#include "ext4/balloc.h"
41#include "ext4/bitmap.h"
42#include "ext4/block_group.h"
43#include "ext4/filesystem.h"
44#include "ext4/inode.h"
45#include "ext4/superblock.h"
46#include "ext4/types.h"
47
48/** Free block.
49 *
50 * @param inode_ref Inode, where the block is allocated
51 * @param block_addr Absolute block address to free
52 *
53 * @return Error code
54 *
55 */
56errno_t ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
57{
58 ext4_filesystem_t *fs = inode_ref->fs;
59 ext4_superblock_t *sb = fs->superblock;
60
61 /* Compute indexes */
62 uint32_t block_group = ext4_filesystem_blockaddr2group(sb, block_addr);
63 uint32_t index_in_group =
64 ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
65
66 /* Load block group reference */
67 ext4_block_group_ref_t *bg_ref;
68 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
69 if (rc != EOK)
70 return rc;
71
72 /* Load block with bitmap */
73 uint32_t bitmap_block_addr =
74 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
75 block_t *bitmap_block;
76 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
77 if (rc != EOK) {
78 ext4_filesystem_put_block_group_ref(bg_ref);
79 return rc;
80 }
81
82 /* Modify bitmap */
83 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
84 bitmap_block->dirty = true;
85
86 /* Release block with bitmap */
87 rc = block_put(bitmap_block);
88 if (rc != EOK) {
89 /* Error in saving bitmap */
90 ext4_filesystem_put_block_group_ref(bg_ref);
91 return rc;
92 }
93
94 uint32_t block_size = ext4_superblock_get_block_size(sb);
95
96 /* Update superblock free blocks count */
97 uint32_t sb_free_blocks =
98 ext4_superblock_get_free_blocks_count(sb);
99 sb_free_blocks++;
100 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
101
102 /* Update inode blocks count */
103 uint64_t ino_blocks =
104 ext4_inode_get_blocks_count(sb, inode_ref->inode);
105 ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
106 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
107 inode_ref->dirty = true;
108
109 /* Update block group free blocks count */
110 uint32_t free_blocks =
111 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
112 free_blocks++;
113 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
114 sb, free_blocks);
115 bg_ref->dirty = true;
116
117 /* Release block group reference */
118 return ext4_filesystem_put_block_group_ref(bg_ref);
119}
120
121static errno_t ext4_balloc_free_blocks_internal(ext4_inode_ref_t *inode_ref,
122 uint32_t first, uint32_t count)
123{
124 ext4_filesystem_t *fs = inode_ref->fs;
125 ext4_superblock_t *sb = fs->superblock;
126
127 /* Compute indexes */
128 uint32_t block_group_first = ext4_filesystem_blockaddr2group(sb,
129 first);
130 uint32_t block_group_last = ext4_filesystem_blockaddr2group(sb,
131 first + count - 1);
132
133 assert(block_group_first == block_group_last);
134
135 /* Load block group reference */
136 ext4_block_group_ref_t *bg_ref;
137 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
138 if (rc != EOK)
139 return rc;
140
141 uint32_t index_in_group_first =
142 ext4_filesystem_blockaddr2_index_in_group(sb, first);
143
144 /* Load block with bitmap */
145 uint32_t bitmap_block_addr =
146 ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
147
148 block_t *bitmap_block;
149 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
150 if (rc != EOK) {
151 ext4_filesystem_put_block_group_ref(bg_ref);
152 return rc;
153 }
154
155 /* Modify bitmap */
156 ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
157 bitmap_block->dirty = true;
158
159 /* Release block with bitmap */
160 rc = block_put(bitmap_block);
161 if (rc != EOK) {
162 /* Error in saving bitmap */
163 ext4_filesystem_put_block_group_ref(bg_ref);
164 return rc;
165 }
166
167 uint32_t block_size = ext4_superblock_get_block_size(sb);
168
169 /* Update superblock free blocks count */
170 uint32_t sb_free_blocks =
171 ext4_superblock_get_free_blocks_count(sb);
172 sb_free_blocks += count;
173 ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
174
175 /* Update inode blocks count */
176 uint64_t ino_blocks =
177 ext4_inode_get_blocks_count(sb, inode_ref->inode);
178 ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
179 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
180 inode_ref->dirty = true;
181
182 /* Update block group free blocks count */
183 uint32_t free_blocks =
184 ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
185 free_blocks += count;
186 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
187 sb, free_blocks);
188 bg_ref->dirty = true;
189
190 /* Release block group reference */
191 return ext4_filesystem_put_block_group_ref(bg_ref);
192}
193
194/** Free continuous set of blocks.
195 *
196 * @param inode_ref Inode, where the blocks are allocated
197 * @param first First block to release
198 * @param count Number of blocks to release
199 *
200 */
201errno_t ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
202 uint32_t first, uint32_t count)
203{
204 errno_t r;
205 uint32_t gid;
206 uint64_t limit;
207 ext4_filesystem_t *fs = inode_ref->fs;
208 ext4_superblock_t *sb = fs->superblock;
209
210 while (count) {
211 gid = ext4_filesystem_blockaddr2group(sb, first);
212 limit = ext4_filesystem_index_in_group2blockaddr(sb, 0,
213 gid + 1);
214
215 if ((first + count) >= limit) {
216 /* This extent spans over 2 or more block groups,
217 * we'll break it into smaller parts.
218 */
219 uint32_t s = limit - first;
220
221 r = ext4_balloc_free_blocks_internal(inode_ref,
222 first, s);
223 if (r != EOK)
224 return r;
225
226 first = limit;
227 count -= s;
228 } else {
229 return ext4_balloc_free_blocks_internal(inode_ref,
230 first, count);
231 }
232 }
233
234 return EOK;
235}
236
237/** Compute first block for data in block group.
238 *
239 * @param sb Pointer to superblock
240 * @param bg Pointer to block group
241 * @param bgid Index of block group
242 *
243 * @return Absolute block index of first block
244 *
245 */
246uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
247 ext4_block_group_ref_t *bg_ref)
248{
249 uint32_t r;
250 uint64_t itable = ext4_block_group_get_inode_table_first_block(
251 bg_ref->block_group, sb);
252 uint32_t itable_sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref);
253
254 if (!ext4_superblock_has_feature_incompatible(sb,
255 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
256 /* If we are not using FLEX_BG, the first data block
257 * is always after the inode table.
258 */
259 r = itable + itable_sz;
260 return ext4_filesystem_blockaddr2_index_in_group(sb, r);
261 }
262
263 uint64_t bbmap = ext4_block_group_get_block_bitmap(bg_ref->block_group,
264 sb);
265 uint64_t ibmap = ext4_block_group_get_inode_bitmap(bg_ref->block_group,
266 sb);
267
268 r = ext4_filesystem_index_in_group2blockaddr(sb, 0, bg_ref->index);
269 r += ext4_filesystem_bg_get_backup_blocks(bg_ref);
270
271 if (ext4_filesystem_blockaddr2group(sb, bbmap) != bg_ref->index)
272 bbmap = -1; /* Invalid */
273
274 if (ext4_filesystem_blockaddr2group(sb, ibmap) != bg_ref->index)
275 ibmap = -1;
276
277 while (true) {
278 if (r == bbmap || r == ibmap)
279 r++;
280 else if (r >= itable && r < (itable + itable_sz))
281 r = itable + itable_sz;
282 else
283 break;
284 }
285
286 return r;
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 */
296static 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 */
346errno_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
485goal_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
588success:
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 */
626errno_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
698terminate:
699 return ext4_filesystem_put_block_group_ref(bg_ref);
700}
701
702/**
703 * @}
704 */
Note: See TracBrowser for help on using the repository browser.