ext4_xattr: fix xattr functions not recognizing acl attribute name.
[lwext4.git] / src / ext4_balloc.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  *
4  * HelenOS:
5  * Copyright (c) 2012 Martin Sucha
6  * Copyright (c) 2012 Frantisek Princ
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * - Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright
16  *   notice, this list of conditions and the following disclaimer in the
17  *   documentation and/or other materials provided with the distribution.
18  * - The name of the author may not be used to endorse or promote products
19  *   derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /** @addtogroup lwext4
34  * @{
35  */
36 /**
37  * @file  ext4_balloc.c
38  * @brief Physical block allocator.
39  */
40
41 #include "ext4_config.h"
42 #include "ext4_types.h"
43 #include "ext4_misc.h"
44 #include "ext4_errno.h"
45 #include "ext4_debug.h"
46
47 #include "ext4_trans.h"
48 #include "ext4_balloc.h"
49 #include "ext4_super.h"
50 #include "ext4_crc32.h"
51 #include "ext4_block_group.h"
52 #include "ext4_fs.h"
53 #include "ext4_bitmap.h"
54 #include "ext4_inode.h"
55
56 /**@brief Compute number of block group from block address.
57  * @param sb superblock pointer.
58  * @param baddr Absolute address of block.
59  * @return Block group index
60  */
61 uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s,
62                                        uint64_t baddr)
63 {
64         if (ext4_get32(s, first_data_block) && baddr)
65                 baddr--;
66
67         return (uint32_t)(baddr / ext4_get32(s, blocks_per_group));
68 }
69
70 /**@brief Compute the starting block address of a block group
71  * @param sb   superblock pointer.
72  * @param bgid block group index
73  * @return Block address
74  */
75 uint64_t ext4_balloc_get_block_of_bgid(struct ext4_sblock *s,
76                                        uint32_t bgid)
77 {
78         uint64_t baddr = 0;
79         if (ext4_get32(s, first_data_block))
80                 baddr++;
81
82         baddr += bgid * ext4_get32(s, blocks_per_group);
83         return baddr;
84 }
85
86 #if CONFIG_META_CSUM_ENABLE
87 static uint32_t ext4_balloc_bitmap_csum(struct ext4_sblock *sb,
88                                         void *bitmap)
89 {
90         uint32_t checksum = 0;
91         if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
92                 uint32_t blocks_per_group = ext4_get32(sb, blocks_per_group);
93
94                 /* First calculate crc32 checksum against fs uuid */
95                 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
96                                 sizeof(sb->uuid));
97                 /* Then calculate crc32 checksum against block_group_desc */
98                 checksum = ext4_crc32c(checksum, bitmap, blocks_per_group / 8);
99         }
100         return checksum;
101 }
102 #else
103 #define ext4_balloc_bitmap_csum(...) 0
104 #endif
105
106 void ext4_balloc_set_bitmap_csum(struct ext4_sblock *sb,
107                                  struct ext4_bgroup *bg,
108                                  void *bitmap __unused)
109 {
110         int desc_size = ext4_sb_get_desc_size(sb);
111         uint32_t checksum = ext4_balloc_bitmap_csum(sb, bitmap);
112         uint16_t lo_checksum = to_le16(checksum & 0xFFFF),
113                  hi_checksum = to_le16(checksum >> 16);
114
115         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
116                 return;
117
118         /* See if we need to assign a 32bit checksum */
119         bg->block_bitmap_csum_lo = lo_checksum;
120         if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
121                 bg->block_bitmap_csum_hi = hi_checksum;
122
123 }
124
125 #if CONFIG_META_CSUM_ENABLE
126 static bool
127 ext4_balloc_verify_bitmap_csum(struct ext4_sblock *sb,
128                                struct ext4_bgroup *bg,
129                                void *bitmap __unused)
130 {
131         int desc_size = ext4_sb_get_desc_size(sb);
132         uint32_t checksum = ext4_balloc_bitmap_csum(sb, bitmap);
133         uint16_t lo_checksum = to_le16(checksum & 0xFFFF),
134                  hi_checksum = to_le16(checksum >> 16);
135
136         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
137                 return true;
138
139         if (bg->block_bitmap_csum_lo != lo_checksum)
140                 return false;
141
142         if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
143                 if (bg->block_bitmap_csum_hi != hi_checksum)
144                         return false;
145
146         return true;
147 }
148 #else
149 #define ext4_balloc_verify_bitmap_csum(...) true
150 #endif
151
152 int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, ext4_fsblk_t baddr)
153 {
154         struct ext4_fs *fs = inode_ref->fs;
155         struct ext4_sblock *sb = &fs->sb;
156
157         uint32_t bg_id = ext4_balloc_get_bgid_of_block(sb, baddr);
158         uint32_t index_in_group = ext4_fs_addr_to_idx_bg(sb, baddr);
159
160         /* Load block group reference */
161         struct ext4_block_group_ref bg_ref;
162         int rc = ext4_fs_get_block_group_ref(fs, bg_id, &bg_ref);
163         if (rc != EOK)
164                 return rc;
165
166         struct ext4_bgroup *bg = bg_ref.block_group;
167
168         /* Load block with bitmap */
169         ext4_fsblk_t bitmap_block_addr =
170             ext4_bg_get_block_bitmap(bg, sb);
171
172         struct ext4_block bitmap_block;
173
174         rc = ext4_trans_block_get(fs->bdev, &bitmap_block, bitmap_block_addr);
175         if (rc != EOK) {
176                 ext4_fs_put_block_group_ref(&bg_ref);
177                 return rc;
178         }
179
180         if (!ext4_balloc_verify_bitmap_csum(sb, bg, bitmap_block.data)) {
181                 ext4_dbg(DEBUG_BALLOC,
182                         DBG_WARN "Bitmap checksum failed."
183                         "Group: %" PRIu32"\n",
184                         bg_ref.index);
185         }
186
187         /* Modify bitmap */
188         ext4_bmap_bit_clr(bitmap_block.data, index_in_group);
189         ext4_balloc_set_bitmap_csum(sb, bg, bitmap_block.data);
190         ext4_trans_set_block_dirty(bitmap_block.buf);
191
192         /* Release block with bitmap */
193         rc = ext4_block_set(fs->bdev, &bitmap_block);
194         if (rc != EOK) {
195                 /* Error in saving bitmap */
196                 ext4_fs_put_block_group_ref(&bg_ref);
197                 return rc;
198         }
199
200         uint32_t block_size = ext4_sb_get_block_size(sb);
201
202         /* Update superblock free blocks count */
203         uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb);
204         sb_free_blocks++;
205         ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks);
206
207         /* Update inode blocks count */
208         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
209         ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
210         ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
211         inode_ref->dirty = true;
212
213         /* Update block group free blocks count */
214         uint32_t free_blocks = ext4_bg_get_free_blocks_count(bg, sb);
215         free_blocks++;
216         ext4_bg_set_free_blocks_count(bg, sb, free_blocks);
217
218         bg_ref.dirty = true;
219
220         rc = ext4_trans_try_revoke_block(fs->bdev, baddr);
221         if (rc != EOK) {
222                 bg_ref.dirty = false;
223                 ext4_fs_put_block_group_ref(&bg_ref);
224                 return rc;
225         }
226         ext4_bcache_invalidate_lba(fs->bdev->bc, baddr, 1);
227         /* Release block group reference */
228         return ext4_fs_put_block_group_ref(&bg_ref);
229 }
230
231 int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref,
232                             ext4_fsblk_t first, uint32_t count)
233 {
234         int rc = EOK;
235         struct ext4_fs *fs = inode_ref->fs;
236         struct ext4_sblock *sb = &fs->sb;
237
238         /* Compute indexes */
239         uint32_t bg_first = ext4_balloc_get_bgid_of_block(sb, first);
240
241         /* Compute indexes */
242         uint32_t bg_last = ext4_balloc_get_bgid_of_block(sb, first + count - 1);
243
244         if (!ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG)) {
245                 /*It is not possible without flex_bg that blocks are continuous
246                  * and and last block belongs to other bg.*/
247                 if (bg_last != bg_first) {
248                         ext4_dbg(DEBUG_BALLOC, DBG_WARN "FLEX_BG: disabled & "
249                                 "bg_last: %"PRIu32" bg_first: %"PRIu32"\n",
250                                 bg_last, bg_first);
251                 }
252         }
253
254         /* Load block group reference */
255         struct ext4_block_group_ref bg_ref;
256         while (bg_first <= bg_last) {
257
258                 rc = ext4_fs_get_block_group_ref(fs, bg_first, &bg_ref);
259                 if (rc != EOK)
260                         return rc;
261
262                 struct ext4_bgroup *bg = bg_ref.block_group;
263
264                 uint32_t idx_in_bg_first;
265                 idx_in_bg_first = ext4_fs_addr_to_idx_bg(sb, first);
266
267                 /* Load block with bitmap */
268                 ext4_fsblk_t bitmap_blk = ext4_bg_get_block_bitmap(bg, sb);
269
270                 struct ext4_block blk;
271                 rc = ext4_trans_block_get(fs->bdev, &blk, bitmap_blk);
272                 if (rc != EOK) {
273                         ext4_fs_put_block_group_ref(&bg_ref);
274                         return rc;
275                 }
276
277                 if (!ext4_balloc_verify_bitmap_csum(sb, bg, blk.data)) {
278                         ext4_dbg(DEBUG_BALLOC,
279                                 DBG_WARN "Bitmap checksum failed."
280                                 "Group: %" PRIu32"\n",
281                                 bg_ref.index);
282                 }
283                 uint32_t free_cnt;
284                 free_cnt = ext4_sb_get_block_size(sb) * 8 - idx_in_bg_first;
285
286                 /*If last block, free only count blocks*/
287                 free_cnt = count > free_cnt ? free_cnt : count;
288
289                 /* Modify bitmap */
290                 ext4_bmap_bits_free(blk.data, idx_in_bg_first, free_cnt);
291                 ext4_balloc_set_bitmap_csum(sb, bg, blk.data);
292                 ext4_trans_set_block_dirty(blk.buf);
293
294                 count -= free_cnt;
295                 first += free_cnt;
296
297                 /* Release block with bitmap */
298                 rc = ext4_block_set(fs->bdev, &blk);
299                 if (rc != EOK) {
300                         ext4_fs_put_block_group_ref(&bg_ref);
301                         return rc;
302                 }
303
304                 uint32_t block_size = ext4_sb_get_block_size(sb);
305
306                 /* Update superblock free blocks count */
307                 uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb);
308                 sb_free_blocks += free_cnt;
309                 ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks);
310
311                 /* Update inode blocks count */
312                 uint64_t ino_blocks;
313                 ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
314                 ino_blocks -= free_cnt * (block_size / EXT4_INODE_BLOCK_SIZE);
315                 ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
316                 inode_ref->dirty = true;
317
318                 /* Update block group free blocks count */
319                 uint32_t free_blocks;
320                 free_blocks = ext4_bg_get_free_blocks_count(bg, sb);
321                 free_blocks += free_cnt;
322                 ext4_bg_set_free_blocks_count(bg, sb, free_blocks);
323                 bg_ref.dirty = true;
324
325                 /* Release block group reference */
326                 rc = ext4_fs_put_block_group_ref(&bg_ref);
327                 if (rc != EOK)
328                         break;
329
330                 bg_first++;
331         }
332
333         uint32_t i;
334         for (i = 0;i < count;i++) {
335                 rc = ext4_trans_try_revoke_block(fs->bdev, first + i);
336                 if (rc != EOK)
337                         return rc;
338
339         }
340
341         ext4_bcache_invalidate_lba(fs->bdev->bc, first, count);
342         /*All blocks should be released*/
343         ext4_assert(count == 0);
344         return rc;
345 }
346
347 int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref,
348                             ext4_fsblk_t goal,
349                             ext4_fsblk_t *fblock)
350 {
351         ext4_fsblk_t alloc = 0;
352         ext4_fsblk_t bmp_blk_adr;
353         uint32_t rel_blk_idx = 0;
354         uint64_t free_blocks;
355         int r;
356         struct ext4_sblock *sb = &inode_ref->fs->sb;
357
358         /* Load block group number for goal and relative index */
359         uint32_t bg_id = ext4_balloc_get_bgid_of_block(sb, goal);
360         uint32_t idx_in_bg = ext4_fs_addr_to_idx_bg(sb, goal);
361
362         struct ext4_block b;
363         struct ext4_block_group_ref bg_ref;
364
365         /* Load block group reference */
366         r = ext4_fs_get_block_group_ref(inode_ref->fs, bg_id, &bg_ref);
367         if (r != EOK)
368                 return r;
369
370         struct ext4_bgroup *bg = bg_ref.block_group;
371
372         free_blocks = ext4_bg_get_free_blocks_count(bg_ref.block_group, sb);
373         if (free_blocks == 0) {
374                 /* This group has no free blocks */
375                 goto goal_failed;
376         }
377
378         /* Compute indexes */
379         ext4_fsblk_t first_in_bg;
380         first_in_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref.index);
381
382         uint32_t first_in_bg_index;
383         first_in_bg_index = ext4_fs_addr_to_idx_bg(sb, first_in_bg);
384
385         if (idx_in_bg < first_in_bg_index)
386                 idx_in_bg = first_in_bg_index;
387
388         /* Load block with bitmap */
389         bmp_blk_adr = ext4_bg_get_block_bitmap(bg_ref.block_group, sb);
390
391         r = ext4_trans_block_get(inode_ref->fs->bdev, &b, bmp_blk_adr);
392         if (r != EOK) {
393                 ext4_fs_put_block_group_ref(&bg_ref);
394                 return r;
395         }
396
397         if (!ext4_balloc_verify_bitmap_csum(sb, bg, b.data)) {
398                 ext4_dbg(DEBUG_BALLOC,
399                         DBG_WARN "Bitmap checksum failed."
400                         "Group: %" PRIu32"\n",
401                         bg_ref.index);
402         }
403
404         /* Check if goal is free */
405         if (ext4_bmap_is_bit_clr(b.data, idx_in_bg)) {
406                 ext4_bmap_bit_set(b.data, idx_in_bg);
407                 ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group,
408                                             b.data);
409                 ext4_trans_set_block_dirty(b.buf);
410                 r = ext4_block_set(inode_ref->fs->bdev, &b);
411                 if (r != EOK) {
412                         ext4_fs_put_block_group_ref(&bg_ref);
413                         return r;
414                 }
415
416                 alloc = ext4_fs_bg_idx_to_addr(sb, idx_in_bg, bg_id);
417                 goto success;
418         }
419
420         uint32_t blk_in_bg = ext4_blocks_in_group_cnt(sb, bg_id);
421
422         uint32_t end_idx = (idx_in_bg + 63) & ~63;
423         if (end_idx > blk_in_bg)
424                 end_idx = blk_in_bg;
425
426         /* Try to find free block near to goal */
427         uint32_t tmp_idx;
428         for (tmp_idx = idx_in_bg + 1; tmp_idx < end_idx; ++tmp_idx) {
429                 if (ext4_bmap_is_bit_clr(b.data, tmp_idx)) {
430                         ext4_bmap_bit_set(b.data, tmp_idx);
431
432                         ext4_balloc_set_bitmap_csum(sb, bg, b.data);
433                         ext4_trans_set_block_dirty(b.buf);
434                         r = ext4_block_set(inode_ref->fs->bdev, &b);
435                         if (r != EOK)
436                                 return r;
437
438                         alloc = ext4_fs_bg_idx_to_addr(sb, tmp_idx, bg_id);
439                         goto success;
440                 }
441         }
442
443         /* Find free bit in bitmap */
444         r = ext4_bmap_bit_find_clr(b.data, idx_in_bg, blk_in_bg, &rel_blk_idx);
445         if (r == EOK) {
446                 ext4_bmap_bit_set(b.data, rel_blk_idx);
447                 ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group, b.data);
448                 ext4_trans_set_block_dirty(b.buf);
449                 r = ext4_block_set(inode_ref->fs->bdev, &b);
450                 if (r != EOK)
451                         return r;
452
453                 alloc = ext4_fs_bg_idx_to_addr(sb, rel_blk_idx, bg_id);
454                 goto success;
455         }
456
457         /* No free block found yet */
458         r = ext4_block_set(inode_ref->fs->bdev, &b);
459         if (r != EOK) {
460                 ext4_fs_put_block_group_ref(&bg_ref);
461                 return r;
462         }
463
464 goal_failed:
465
466         r = ext4_fs_put_block_group_ref(&bg_ref);
467         if (r != EOK)
468                 return r;
469
470         /* Try other block groups */
471         uint32_t block_group_count = ext4_block_group_cnt(sb);
472         uint32_t bgid = (bg_id + 1) % block_group_count;
473         uint32_t count = block_group_count;
474
475         while (count > 0) {
476                 r = ext4_fs_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
477                 if (r != EOK)
478                         return r;
479
480                 struct ext4_bgroup *bg = bg_ref.block_group;
481                 free_blocks = ext4_bg_get_free_blocks_count(bg, sb);
482                 if (free_blocks == 0) {
483                         /* This group has no free blocks */
484                         goto next_group;
485                 }
486
487                 /* Load block with bitmap */
488                 bmp_blk_adr = ext4_bg_get_block_bitmap(bg, sb);
489                 r = ext4_trans_block_get(inode_ref->fs->bdev, &b, bmp_blk_adr);
490                 if (r != EOK) {
491                         ext4_fs_put_block_group_ref(&bg_ref);
492                         return r;
493                 }
494
495                 if (!ext4_balloc_verify_bitmap_csum(sb, bg, b.data)) {
496                         ext4_dbg(DEBUG_BALLOC,
497                                 DBG_WARN "Bitmap checksum failed."
498                                 "Group: %" PRIu32"\n",
499                                 bg_ref.index);
500                 }
501
502                 /* Compute indexes */
503                 first_in_bg = ext4_balloc_get_block_of_bgid(sb, bgid);
504                 idx_in_bg = ext4_fs_addr_to_idx_bg(sb, first_in_bg);
505                 blk_in_bg = ext4_blocks_in_group_cnt(sb, bgid);
506                 first_in_bg_index = ext4_fs_addr_to_idx_bg(sb, first_in_bg);
507
508                 if (idx_in_bg < first_in_bg_index)
509                         idx_in_bg = first_in_bg_index;
510
511                 r = ext4_bmap_bit_find_clr(b.data, idx_in_bg, blk_in_bg,
512                                 &rel_blk_idx);
513                 if (r == EOK) {
514                         ext4_bmap_bit_set(b.data, rel_blk_idx);
515                         ext4_balloc_set_bitmap_csum(sb, bg, b.data);
516                         ext4_trans_set_block_dirty(b.buf);
517                         r = ext4_block_set(inode_ref->fs->bdev, &b);
518                         if (r != EOK) {
519                                 ext4_fs_put_block_group_ref(&bg_ref);
520                                 return r;
521                         }
522
523                         alloc = ext4_fs_bg_idx_to_addr(sb, rel_blk_idx, bgid);
524                         goto success;
525                 }
526
527                 r = ext4_block_set(inode_ref->fs->bdev, &b);
528                 if (r != EOK) {
529                         ext4_fs_put_block_group_ref(&bg_ref);
530                         return r;
531                 }
532
533         next_group:
534                 r = ext4_fs_put_block_group_ref(&bg_ref);
535                 if (r != EOK) {
536                         return r;
537                 }
538
539                 /* Goto next group */
540                 bgid = (bgid + 1) % block_group_count;
541                 count--;
542         }
543
544         return ENOSPC;
545
546 success:
547     /* Empty command - because of syntax */
548     ;
549
550         uint32_t block_size = ext4_sb_get_block_size(sb);
551
552         /* Update superblock free blocks count */
553         uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb);
554         sb_free_blocks--;
555         ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks);
556
557         /* Update inode blocks (different block size!) count */
558         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
559         ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
560         ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
561         inode_ref->dirty = true;
562
563         /* Update block group free blocks count */
564
565         uint32_t fb_cnt = ext4_bg_get_free_blocks_count(bg_ref.block_group, sb);
566         fb_cnt--;
567         ext4_bg_set_free_blocks_count(bg_ref.block_group, sb, fb_cnt);
568
569         bg_ref.dirty = true;
570         r = ext4_fs_put_block_group_ref(&bg_ref);
571
572         *fblock = alloc;
573         return r;
574 }
575
576 int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref,
577                                 ext4_fsblk_t baddr, bool *free)
578 {
579         int rc;
580
581         struct ext4_fs *fs = inode_ref->fs;
582         struct ext4_sblock *sb = &fs->sb;
583
584         /* Compute indexes */
585         uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, baddr);
586         uint32_t index_in_group = ext4_fs_addr_to_idx_bg(sb, baddr);
587
588         /* Load block group reference */
589         struct ext4_block_group_ref bg_ref;
590         rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
591         if (rc != EOK)
592                 return rc;
593
594         /* Load block with bitmap */
595         ext4_fsblk_t bmp_blk_addr;
596         bmp_blk_addr = ext4_bg_get_block_bitmap(bg_ref.block_group, sb);
597
598         struct ext4_block b;
599         rc = ext4_trans_block_get(fs->bdev, &b, bmp_blk_addr);
600         if (rc != EOK) {
601                 ext4_fs_put_block_group_ref(&bg_ref);
602                 return rc;
603         }
604
605         if (!ext4_balloc_verify_bitmap_csum(sb, bg_ref.block_group, b.data)) {
606                 ext4_dbg(DEBUG_BALLOC,
607                         DBG_WARN "Bitmap checksum failed."
608                         "Group: %" PRIu32"\n",
609                         bg_ref.index);
610         }
611
612         /* Check if block is free */
613         *free = ext4_bmap_is_bit_clr(b.data, index_in_group);
614
615         /* Allocate block if possible */
616         if (*free) {
617                 ext4_bmap_bit_set(b.data, index_in_group);
618                 ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group, b.data);
619                 ext4_trans_set_block_dirty(b.buf);
620         }
621
622         /* Release block with bitmap */
623         rc = ext4_block_set(fs->bdev, &b);
624         if (rc != EOK) {
625                 /* Error in saving bitmap */
626                 ext4_fs_put_block_group_ref(&bg_ref);
627                 return rc;
628         }
629
630         /* If block is not free, return */
631         if (!(*free))
632                 goto terminate;
633
634         uint32_t block_size = ext4_sb_get_block_size(sb);
635
636         /* Update superblock free blocks count */
637         uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb);
638         sb_free_blocks--;
639         ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks);
640
641         /* Update inode blocks count */
642         uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
643         ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
644         ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
645         inode_ref->dirty = true;
646
647         /* Update block group free blocks count */
648         uint32_t fb_cnt = ext4_bg_get_free_blocks_count(bg_ref.block_group, sb);
649         fb_cnt--;
650         ext4_bg_set_free_blocks_count(bg_ref.block_group, sb, fb_cnt);
651
652         bg_ref.dirty = true;
653
654 terminate:
655         return ext4_fs_put_block_group_ref(&bg_ref);
656 }
657
658 /**
659  * @}
660  */