FEATURES:
[lwext4.git] / lwext4 / ext4_fs.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  *
4  *
5  * HelenOS:
6  * Copyright (c) 2012 Martin Sucha
7  * Copyright (c) 2012 Frantisek Princ
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * - Redistributions of source code must retain the above copyright
15  *   notice, this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright
17  *   notice, this list of conditions and the following disclaimer in the
18  *   documentation and/or other materials provided with the distribution.
19  * - The name of the author may not be used to endorse or promote products
20  *   derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /** @addtogroup lwext4
34  * @{
35  */
36 /**
37  * @file  ext4_fs.c
38  * @brief More complex filesystem functions.
39  */
40
41 #include <ext4_config.h>
42 #include <ext4_types.h>
43 #include <ext4_fs.h>
44 #include <ext4_errno.h>
45 #include <ext4_blockdev.h>
46 #include <ext4_super.h>
47 #include <ext4_debug.h>
48 #include <ext4_block_group.h>
49 #include <ext4_balloc.h>
50 #include <ext4_bitmap.h>
51 #include <ext4_inode.h>
52 #include <ext4_ialloc.h>
53 #include <ext4_extent.h>
54 #include <string.h>
55
56 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
57 {
58     int r, i;
59     uint16_t tmp;
60     uint32_t bsize;
61     bool read_only = false;
62
63     ext4_assert(fs && bdev);
64
65     fs->bdev = bdev;
66
67     r = ext4_sb_read(fs->bdev, &fs->sb);
68     if(r != EOK)
69         return r;
70
71     if(!ext4_sb_check(&fs->sb))
72         return ENOTSUP;
73
74     bsize = ext4_sb_get_block_size(&fs->sb);
75     if (bsize > EXT4_MAX_BLOCK_SIZE)
76         return ENXIO;
77
78     r = ext4_fs_check_features(fs, &read_only);
79     if(r != EOK)
80         return r;
81
82     if(read_only)
83         return ENOTSUP;
84
85     /* Compute limits for indirect block levels */
86     uint32_t blocks_id = bsize / sizeof(uint32_t);
87
88     fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
89     fs->inode_blocks_per_level[0] = 1;
90
91     for (i = 1; i < 4; i++) {
92         fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
93                 blocks_id;
94         fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
95                 fs->inode_blocks_per_level[i];
96     }
97
98     /*Validate FS*/
99     tmp = ext4_get16(&fs->sb, state);
100     if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) {
101         ext4_dprintf(EXT4_DEBUG_FS,
102                 "Filesystem was not cleanly unmounted before \n");
103     }
104
105     /* Mark system as mounted */
106     ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
107     r = ext4_sb_write(fs->bdev, &fs->sb);
108     if (r != EOK)
109         return r;
110
111
112     /*Update mount count*/
113     ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
114
115     return r;
116 }
117
118
119 int ext4_fs_fini(struct ext4_fs *fs)
120 {
121     ext4_assert(fs);
122
123     /*Set superblock state*/
124     ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
125
126     return ext4_sb_write(fs->bdev, &fs->sb);
127 }
128
129 static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
130 {
131
132     if(features_incompatible &
133             EXT4_FEATURE_INCOMPAT_COMPRESSION){
134         ext4_dprintf(EXT4_DEBUG_FS,
135                 "EXT4_FEATURE_INCOMPAT_COMPRESSION\n");
136     }
137     if(features_incompatible &
138             EXT4_FEATURE_INCOMPAT_FILETYPE){
139         ext4_dprintf(EXT4_DEBUG_FS,
140                 "EXT4_FEATURE_INCOMPAT_FILETYPE\n");
141     }
142     if(features_incompatible &
143             EXT4_FEATURE_INCOMPAT_RECOVER){
144         ext4_dprintf(EXT4_DEBUG_FS,
145                 "EXT4_FEATURE_INCOMPAT_RECOVER\n");
146     }
147     if(features_incompatible &
148             EXT4_FEATURE_INCOMPAT_JOURNAL_DEV){
149         ext4_dprintf(EXT4_DEBUG_FS,
150                 "EXT4_FEATURE_INCOMPAT_JOURNAL_DEV\n");
151     }
152     if(features_incompatible &
153             EXT4_FEATURE_INCOMPAT_META_BG){
154         ext4_dprintf(EXT4_DEBUG_FS,
155                 "EXT4_FEATURE_INCOMPAT_META_BG\n");
156     }
157     if(features_incompatible &
158             EXT4_FEATURE_INCOMPAT_EXTENTS){
159         ext4_dprintf(EXT4_DEBUG_FS,
160                 "EXT4_FEATURE_INCOMPAT_EXTENTS\n");
161     }
162     if(features_incompatible &
163             EXT4_FEATURE_INCOMPAT_64BIT){
164         ext4_dprintf(EXT4_DEBUG_FS,
165                 "EXT4_FEATURE_INCOMPAT_64BIT\n");
166     }
167     if(features_incompatible &
168             EXT4_FEATURE_INCOMPAT_MMP){
169         ext4_dprintf(EXT4_DEBUG_FS,
170                 "EXT4_FEATURE_INCOMPAT_MMP\n");
171     }
172     if(features_incompatible &
173             EXT4_FEATURE_INCOMPAT_FLEX_BG){
174         ext4_dprintf(EXT4_DEBUG_FS,
175                 "EXT4_FEATURE_INCOMPAT_FLEX_BG\n");
176     }
177     if(features_incompatible &
178             EXT4_FEATURE_INCOMPAT_EA_INODE){
179         ext4_dprintf(EXT4_DEBUG_FS,
180                 "EXT4_FEATURE_INCOMPAT_EA_INODE\n");
181     }
182     if(features_incompatible &
183             EXT4_FEATURE_INCOMPAT_DIRDATA){
184         ext4_dprintf(EXT4_DEBUG_FS,
185                 "EXT4_FEATURE_INCOMPAT_DIRDATA\n");
186     }
187 }
188 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
189 {
190     if(features_compatible &
191             EXT4_FEATURE_COMPAT_DIR_PREALLOC){
192         ext4_dprintf(EXT4_DEBUG_FS,
193                 "EXT4_FEATURE_COMPAT_DIR_PREALLOC\n");
194     }
195     if(features_compatible &
196             EXT4_FEATURE_COMPAT_IMAGIC_INODES){
197         ext4_dprintf(EXT4_DEBUG_FS,
198                 "EXT4_FEATURE_COMPAT_IMAGIC_INODES\n");
199     }
200     if(features_compatible &
201             EXT4_FEATURE_COMPAT_HAS_JOURNAL){
202         ext4_dprintf(EXT4_DEBUG_FS,
203                 "EXT4_FEATURE_COMPAT_HAS_JOURNAL\n");
204     }
205     if(features_compatible &
206             EXT4_FEATURE_COMPAT_EXT_ATTR){
207         ext4_dprintf(EXT4_DEBUG_FS,
208                 "EXT4_FEATURE_COMPAT_EXT_ATTR\n");
209     }
210     if(features_compatible &
211             EXT4_FEATURE_COMPAT_RESIZE_INODE){
212         ext4_dprintf(EXT4_DEBUG_FS,
213                 "EXT4_FEATURE_COMPAT_RESIZE_INODE\n");
214     }
215     if(features_compatible &
216             EXT4_FEATURE_COMPAT_DIR_INDEX){
217         ext4_dprintf(EXT4_DEBUG_FS,
218                 "EXT4_FEATURE_COMPAT_DIR_INDEX\n");
219     }
220 }
221
222 static void ext4_fs_debug_features_ro(uint32_t features_ro)
223 {
224     if(features_ro &
225             EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER){
226         ext4_dprintf(EXT4_DEBUG_FS,
227                 "EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER\n");
228     }
229     if(features_ro &
230             EXT4_FEATURE_RO_COMPAT_LARGE_FILE){
231         ext4_dprintf(EXT4_DEBUG_FS,
232                 "EXT4_FEATURE_RO_COMPAT_LARGE_FILE\n");
233     }
234     if(features_ro &
235             EXT4_FEATURE_RO_COMPAT_BTREE_DIR){
236         ext4_dprintf(EXT4_DEBUG_FS,
237                 "EXT4_FEATURE_RO_COMPAT_BTREE_DIR\n");
238     }
239     if(features_ro &
240             EXT4_FEATURE_RO_COMPAT_HUGE_FILE){
241         ext4_dprintf(EXT4_DEBUG_FS,
242                 "EXT4_FEATURE_RO_COMPAT_HUGE_FILE\n");
243     }
244     if(features_ro &
245             EXT4_FEATURE_RO_COMPAT_GDT_CSUM){
246         ext4_dprintf(EXT4_DEBUG_FS,
247                 "EXT4_FEATURE_RO_COMPAT_GDT_CSUM\n");
248     }
249     if(features_ro &
250             EXT4_FEATURE_RO_COMPAT_DIR_NLINK){
251         ext4_dprintf(EXT4_DEBUG_FS,
252                 "EXT4_FEATURE_RO_COMPAT_DIR_NLINK\n");
253     }
254     if(features_ro &
255             EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE){
256         ext4_dprintf(EXT4_DEBUG_FS,
257                 "EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE\n");
258     }
259 }
260
261 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
262 {
263     ext4_assert(fs && read_only);
264     uint32_t v;
265     if(ext4_get32(&fs->sb, rev_level) == 0){
266         *read_only = false;
267         return EOK;
268     }
269     ext4_dprintf(EXT4_DEBUG_FS,
270         "\nSblock rev_level: \n%d\n", (int)ext4_get32(&fs->sb, rev_level));
271
272     ext4_dprintf(EXT4_DEBUG_FS,
273         "\nSblock minor_rev_level: \n%d\n",
274         ext4_get32(&fs->sb, minor_rev_level));
275
276     ext4_dprintf(EXT4_DEBUG_FS,
277         "\nSblock features_incompatible:\n");
278     ext4_fs_debug_features_incomp(ext4_get32(&fs->sb, features_incompatible));
279
280     ext4_dprintf(EXT4_DEBUG_FS,
281         "\nSblock features_compatible:\n");
282     ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
283
284     ext4_dprintf(EXT4_DEBUG_FS,
285         "\nSblock features_read_only:\n");
286     ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
287
288     /*Check features_incompatible*/
289     v = (ext4_get32(&fs->sb, features_incompatible) &
290             (~EXT4_FEATURE_INCOMPAT_SUPP));
291     if (v){
292         ext4_dprintf(EXT4_DEBUG_FS,
293                 "\nERROR sblock features_incompatible. Unsupported:\n");
294         ext4_fs_debug_features_incomp(v);
295         return ENOTSUP;
296     }
297
298
299     /*Check features_read_only*/
300     v = (ext4_get32(&fs->sb, features_read_only) &
301             (~EXT4_FEATURE_RO_COMPAT_SUPP));
302     if (v){
303         ext4_dprintf(EXT4_DEBUG_FS,
304                 "\nERROR sblock features_read_only . Unsupported:\n");
305         ext4_fs_debug_features_incomp(v);
306
307         *read_only = true;
308         return EOK;
309     }
310
311     *read_only = false;
312
313     return EOK;
314 }
315
316 uint32_t ext4_fs_baddr2_index_in_group(struct ext4_sblock *s, uint32_t baddr)
317 {
318     ext4_assert(baddr);
319     if(ext4_get32(s, first_data_block))
320         baddr--;
321
322     return  baddr % ext4_get32(s, blocks_per_group);
323 }
324
325
326
327 uint32_t ext4_fs_index_in_group2_baddr(struct ext4_sblock *s, uint32_t index,
328     uint32_t bgid)
329 {
330     if(ext4_get32(s, first_data_block))
331         index++;
332
333     return ext4_get32(s, blocks_per_group) * bgid + index;
334 }
335
336
337
338
339 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
340 {
341     uint32_t i;
342     uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
343             bg_ref->block_group, &bg_ref->fs->sb);
344
345     struct ext4_block block_bitmap;
346     int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
347             bitmap_block_addr);
348     if (rc != EOK)
349         return rc;
350
351
352     memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
353
354     /* Determine first block and first data block in group */
355     uint32_t first_idx = 0;
356
357     uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
358             &bg_ref->fs->sb, bg_ref);
359     uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
360             &bg_ref->fs->sb, first_data);
361
362     /*Set bits from to first block to first data block - 1 to one (allocated)*/
363     /*TODO: Optimize it*/
364     for (i = first_idx; i < first_data_idx; ++i)
365         ext4_bmap_bit_set(block_bitmap.data, i);
366
367
368     block_bitmap.dirty = true;
369
370     /* Save bitmap */
371     return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
372 }
373
374 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
375 {
376     /* Load bitmap */
377     uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
378             bg_ref->block_group, &bg_ref->fs->sb);
379
380     struct ext4_block block_bitmap;
381     int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
382             bitmap_block_addr);
383     if (rc != EOK)
384         return rc;
385
386     /* Initialize all bitmap bits to zero */
387     uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
388     uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
389
390     memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
391
392     uint32_t start_bit = inodes_per_group;
393     uint32_t end_bit = block_size * 8;
394
395     uint32_t i;
396     for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
397         ext4_bmap_bit_set(block_bitmap.data, i);
398
399     if (i < end_bit)
400         memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
401
402     block_bitmap.dirty = true;
403
404     /* Save bitmap */
405     return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
406 }
407
408 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
409 {
410     struct ext4_sblock *sb = &bg_ref->fs->sb;
411
412     uint32_t inode_size           = ext4_get32(sb, inode_size);
413     uint32_t block_size           = ext4_sb_get_block_size(sb);
414     uint32_t inodes_per_block = block_size / inode_size;
415     uint32_t inodes_in_group  = ext4_inodes_in_group_cnt(sb, bg_ref->index);
416     uint32_t table_blocks = inodes_in_group / inodes_per_block;
417     uint32_t fblock;
418
419     if (inodes_in_group % inodes_per_block)
420         table_blocks++;
421
422     /* Compute initialization bounds */
423     uint32_t first_block = ext4_bg_get_inode_table_first_block(
424             bg_ref->block_group, sb);
425
426     uint32_t last_block = first_block + table_blocks - 1;
427
428     /* Initialization of all itable blocks */
429     for (fblock = first_block; fblock <= last_block; ++fblock) {
430
431         struct ext4_block block;
432         int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
433         if (rc != EOK)
434             return rc;
435
436         memset(block.data, 0, block_size);
437         block.dirty = true;
438
439         ext4_block_set(bg_ref->fs->bdev, &block);
440         if (rc != EOK)
441             return rc;
442     }
443
444     return EOK;
445 }
446
447
448 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
449     struct ext4_block_group_ref *ref)
450 {
451     /* Compute number of descriptors, that fits in one data block */
452     uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
453             ext4_sb_get_desc_size(&fs->sb);
454
455     /* Block group descriptor table starts at the next block after superblock */
456     uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1;
457
458     /* Find the block containing the descriptor we are looking for */
459     block_id += bgid / dsc_per_block;
460     uint32_t offset = (bgid % dsc_per_block) *
461             ext4_sb_get_desc_size(&fs->sb);
462
463
464     int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
465     if (rc != EOK)
466         return rc;
467
468     ref->block_group = (void *)(ref->block.data + offset);
469     ref->fs = fs;
470     ref->index = bgid;
471     ref->dirty = false;
472
473     if (ext4_bg_has_flag(ref->block_group,
474             EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
475         rc = ext4_fs_init_block_bitmap(ref);
476         if (rc != EOK) {
477             ext4_block_set(fs->bdev, &ref->block);
478             return rc;
479         }
480         ext4_bg_clear_flag(ref->block_group,
481             EXT4_BLOCK_GROUP_BLOCK_UNINIT);
482
483         ref->dirty = true;
484     }
485
486     if (ext4_bg_has_flag(ref->block_group,
487             EXT4_BLOCK_GROUP_INODE_UNINIT)) {
488         rc = ext4_fs_init_inode_bitmap(ref);
489         if (rc != EOK) {
490             ext4_block_set(ref->fs->bdev, &ref->block);
491             return rc;
492         }
493
494         ext4_bg_clear_flag(ref->block_group,
495             EXT4_BLOCK_GROUP_INODE_UNINIT);
496
497         if (!ext4_bg_has_flag(ref->block_group,
498                 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
499             rc = ext4_fs_init_inode_table(ref);
500             if (rc != EOK){
501                 ext4_block_set(fs->bdev, &ref->block);
502                 return rc;
503             }
504
505             ext4_bg_set_flag(ref->block_group,
506                 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
507         }
508
509         ref->dirty = true;
510     }
511
512     return EOK;
513 }
514
515 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
516     struct ext4_bgroup *bg)
517 {
518     /* If checksum not supported, 0 will be returned */
519     uint16_t crc = 0;
520
521     /* Compute the checksum only if the filesystem supports it */
522     if (ext4_sb_check_read_only(sb,
523             EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
524         void *base = bg;
525         void *checksum = &bg->checksum;
526
527         uint32_t offset = (uint32_t) (checksum - base);
528
529         /* Convert block group index to little endian */
530         uint32_t le_group = to_le32(bgid);
531
532         /* Initialization */
533         crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
534
535         /* Include index of block group */
536         crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
537
538         /* Compute crc from the first part (stop before checksum field) */
539         crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
540
541         /* Skip checksum */
542         offset += sizeof(bg->checksum);
543
544         /* Checksum of the rest of block group descriptor */
545         if ((ext4_sb_check_feature_incompatible(sb,
546                 EXT4_FEATURE_INCOMPAT_64BIT)) &&
547                 (offset < ext4_sb_get_desc_size(sb)))
548
549             crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
550                     ext4_sb_get_desc_size(sb) - offset);
551     }
552     return crc;
553 }
554
555 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
556 {
557     /* Check if reference modified */
558     if (ref->dirty) {
559         /* Compute new checksum of block group */
560         uint16_t checksum =
561                 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
562                         ref->block_group);
563
564         ref->block_group->checksum = to_le16(checksum);
565
566         /* Mark block dirty for writing changes to physical device */
567         ref->block.dirty = true;
568     }
569
570     /* Put back block, that contains block group descriptor */
571     return ext4_block_set(ref->fs->bdev, &ref->block);
572 }
573
574 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
575     struct ext4_inode_ref *ref)
576 {
577     /* Compute number of i-nodes, that fits in one data block */
578     uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
579
580     /*
581      * Inode numbers are 1-based, but it is simpler to work with 0-based
582      * when computing indices
583      */
584     index -= 1;
585     uint32_t block_group = index / inodes_per_group;
586     uint32_t offset_in_group = index % inodes_per_group;
587
588     /* Load block group, where i-node is located */
589     struct ext4_block_group_ref bg_ref;
590
591     int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
592     if (rc != EOK) {
593         return rc;
594     }
595
596     /* Load block address, where i-node table is located */
597     uint32_t inode_table_start =
598             ext4_bg_get_inode_table_first_block(bg_ref.block_group,
599                     &fs->sb);
600
601     /* Put back block group reference (not needed more) */
602     rc = ext4_fs_put_block_group_ref(&bg_ref);
603     if (rc != EOK) {
604         return rc;
605     }
606
607     /* Compute position of i-node in the block group */
608     uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
609     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
610     uint32_t byte_offset_in_group = offset_in_group * inode_size;
611
612     /* Compute block address */
613     uint64_t block_id = inode_table_start +
614             (byte_offset_in_group / block_size);
615
616
617     rc = ext4_block_get(fs->bdev, &ref->block, block_id);
618     if (rc != EOK) {
619         return rc;
620     }
621
622     /* Compute position of i-node in the data block */
623     uint32_t offset_in_block = byte_offset_in_group % block_size;
624     ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
625
626     /* We need to store the original value of index in the reference */
627     ref->index = index + 1;
628     ref->fs = fs;
629     ref->dirty = false;
630
631     return EOK;
632 }
633
634 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
635 {
636     /* Check if reference modified */
637     if (ref->dirty) {
638         /* Mark block dirty for writing changes to physical device */
639         ref->block.dirty = true;
640     }
641
642     /* Put back block, that contains i-node */
643     return  ext4_block_set(ref->fs->bdev, &ref->block);
644 }
645
646 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
647     bool is_directory)
648 {
649     /* Check if newly allocated i-node will be a directory */
650     uint32_t i;
651     bool is_dir;
652
653     is_dir = is_directory;
654
655     /* Allocate inode by allocation algorithm */
656     uint32_t index;
657     int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
658     if (rc != EOK)
659         return rc;
660
661     /* Load i-node from on-disk i-node table */
662     rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
663     if (rc != EOK) {
664         ext4_ialloc_free_inode(fs, index, is_dir);
665         return rc;
666     }
667
668     /* Initialize i-node */
669     struct ext4_inode *inode = inode_ref->inode;
670
671     uint16_t mode;
672     if (is_dir) {
673         /*
674          * Default directory permissions to be compatible with other systems
675          * 0777 (octal) == rwxrwxrwx
676          */
677
678         mode = 0777;
679         mode |= EXT4_INODE_MODE_DIRECTORY;
680         ext4_inode_set_mode(&fs->sb, inode, mode);
681         ext4_inode_set_links_count(inode, 1);  /* '.' entry */
682
683     } else {
684         /*
685          * Default file permissions to be compatible with other systems
686          * 0666 (octal) == rw-rw-rw-
687          */
688
689         mode = 0666;
690         mode |= EXT4_INODE_MODE_FILE;
691         ext4_inode_set_mode(&fs->sb, inode, mode);
692         ext4_inode_set_links_count(inode, 0);
693     }
694
695     ext4_inode_set_uid(inode, 0);
696     ext4_inode_set_gid(inode, 0);
697     ext4_inode_set_size(inode, 0);
698     ext4_inode_set_access_time(inode,           0);
699     ext4_inode_set_change_inode_time(inode, 0);
700     ext4_inode_set_modification_time(inode, 0);
701     ext4_inode_set_deletion_time(inode,         0);
702     ext4_inode_set_blocks_count(&fs->sb, inode, 0);
703     ext4_inode_set_flags(inode, 0);
704     ext4_inode_set_generation(inode, 0);
705
706     /* Reset blocks array */
707     for (i = 0; i < EXT4_INODE_BLOCKS; i++)
708         inode->blocks[i] = 0;
709
710 #if CONFIG_EXTENT_ENABLE
711     /* Initialize extents if needed */
712     if (ext4_sb_check_feature_incompatible(
713             &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
714         ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
715
716
717         /* Initialize extent root header */
718         struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
719         ext4_extent_header_set_depth(header, 0);
720         ext4_extent_header_set_entries_count(header, 0);
721         ext4_extent_header_set_generation(header, 0);
722         ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
723
724         uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
725                 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
726
727         ext4_extent_header_set_max_entries_count(header, max_entries);
728     }
729 #endif
730
731     inode_ref->dirty = true;
732
733     return EOK;
734 }
735
736 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
737 {
738     struct ext4_fs *fs = inode_ref->fs;
739     uint32_t offset;
740     uint32_t suboffset;
741 #if CONFIG_EXTENT_ENABLE
742     /* For extents must be data block destroyed by other way */
743     if ((ext4_sb_check_feature_incompatible(&fs->sb,
744             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
745             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
746         /* Data structures are released during truncate operation... */
747         goto finish;
748     }
749 #endif
750     /* Release all indirect (no data) blocks */
751
752     /* 1) Single indirect */
753     uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
754     if (fblock != 0) {
755         int rc = ext4_balloc_free_block(inode_ref, fblock);
756         if (rc != EOK)
757             return rc;
758
759         ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
760     }
761
762     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
763     uint32_t count = block_size / sizeof(uint32_t);
764
765     struct      ext4_block  block;
766
767     /* 2) Double indirect */
768     fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
769     if (fblock != 0) {
770         int rc = ext4_block_get(fs->bdev, &block, fblock);
771         if (rc != EOK)
772             return rc;
773
774         uint32_t ind_block;
775         for (offset = 0; offset < count; ++offset) {
776             ind_block = to_le32(((uint32_t *) block.data)[offset]);
777
778             if (ind_block != 0) {
779                 rc = ext4_balloc_free_block(inode_ref, ind_block);
780                 if (rc != EOK) {
781                     ext4_block_set(fs->bdev, &block);
782                     return rc;
783                 }
784             }
785         }
786
787         ext4_block_set(fs->bdev, &block);
788         rc = ext4_balloc_free_block(inode_ref, fblock);
789         if (rc != EOK)
790             return rc;
791
792         ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
793     }
794
795     /* 3) Tripple indirect */
796     struct      ext4_block  subblock;
797     fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
798     if (fblock != 0) {
799         int rc = ext4_block_get(fs->bdev, &block, fblock);
800         if (rc != EOK)
801             return rc;
802
803         uint32_t ind_block;
804         for ( offset = 0; offset < count; ++offset) {
805             ind_block = to_le32(((uint32_t *) block.data)[offset]);
806
807             if (ind_block != 0) {
808                 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
809                 if (rc != EOK) {
810                     ext4_block_set(fs->bdev, &block);
811                     return rc;
812                 }
813
814                 uint32_t ind_subblock;
815                 for (suboffset = 0; suboffset < count;
816                         ++suboffset) {
817                     ind_subblock = to_le32(((uint32_t *)
818                             subblock.data)[suboffset]);
819
820                     if (ind_subblock != 0) {
821                         rc = ext4_balloc_free_block(inode_ref, ind_subblock);
822                         if (rc != EOK) {
823                             ext4_block_set(fs->bdev, &subblock);
824                             ext4_block_set(fs->bdev, &block);
825                             return rc;
826                         }
827                     }
828                 }
829
830                 ext4_block_set(fs->bdev, &subblock);
831
832
833                 rc = ext4_balloc_free_block(inode_ref, ind_block);
834                 if (rc != EOK) {
835                     ext4_block_set(fs->bdev, &block);
836                     return rc;
837                 }
838             }
839         }
840
841         ext4_block_set(fs->bdev, &block);
842         rc = ext4_balloc_free_block(inode_ref, fblock);
843         if (rc != EOK)
844             return rc;
845
846         ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
847     }
848
849     finish:
850     /* Mark inode dirty for writing to the physical device */
851     inode_ref->dirty = true;
852
853     /* Free block with extended attributes if present */
854     uint32_t xattr_block = ext4_inode_get_file_acl(
855             inode_ref->inode, &fs->sb);
856     if (xattr_block) {
857         int rc = ext4_balloc_free_block(inode_ref, xattr_block);
858         if (rc != EOK)
859             return rc;
860
861         ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
862     }
863
864     /* Free inode by allocator */
865     int rc;
866     if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
867             EXT4_INODE_MODE_DIRECTORY))
868         rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
869     else
870         rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
871
872     return rc;
873 }
874
875 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
876     uint64_t new_size)
877 {
878     struct ext4_sblock *sb = &inode_ref->fs->sb;
879     uint32_t i;
880
881     /* Check flags, if i-node can be truncated */
882     if (!ext4_inode_can_truncate(sb, inode_ref->inode))
883         return EINVAL;
884
885     /* If sizes are equal, nothing has to be done. */
886     uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
887     if (old_size == new_size)
888         return EOK;
889
890     /* It's not suppported to make the larger file by truncate operation */
891     if (old_size < new_size)
892         return EINVAL;
893
894     /* Compute how many blocks will be released */
895     uint64_t size_diff = old_size - new_size;
896     uint32_t block_size  = ext4_sb_get_block_size(sb);
897     uint32_t diff_blocks_count = size_diff / block_size;
898     if (size_diff % block_size != 0)
899         diff_blocks_count++;
900
901     uint32_t old_blocks_count = old_size / block_size;
902     if (old_size % block_size != 0)
903         old_blocks_count++;
904 #if CONFIG_EXTENT_ENABLE
905     if ((ext4_sb_check_feature_incompatible(sb,
906             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
907             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
908
909         /* Extents require special operation */
910         int rc = ext4_extent_release_blocks_from(inode_ref,
911                 old_blocks_count - diff_blocks_count);
912         if (rc != EOK)
913             return rc;
914
915     } else
916 #endif
917     {
918         /* Release data blocks from the end of file */
919
920         /* Starting from 1 because of logical blocks are numbered from 0 */
921         for (i = 1; i <= diff_blocks_count; ++i) {
922             int rc = ext4_fs_release_inode_block(inode_ref,
923                     old_blocks_count - i);
924             if (rc != EOK)
925                 return rc;
926         }
927     }
928
929     /* Update i-node */
930     ext4_inode_set_size(inode_ref->inode, new_size);
931     inode_ref->dirty = true;
932
933     return EOK;
934 }
935
936 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
937     uint64_t iblock, uint32_t *fblock)
938 {
939     struct ext4_fs *fs = inode_ref->fs;
940
941     /* For empty file is situation simple */
942     if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
943         *fblock = 0;
944         return EOK;
945     }
946
947     uint32_t current_block;
948 #if CONFIG_EXTENT_ENABLE
949     /* Handle i-node using extents */
950     if ((ext4_sb_check_feature_incompatible(&fs->sb,
951             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
952             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
953
954
955         int rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
956         if (rc != EOK)
957             return rc;
958
959         *fblock = current_block;
960         return EOK;
961     }
962 #endif
963
964     struct ext4_inode *inode = inode_ref->inode;
965
966     /* Direct block are read directly from array in i-node structure */
967     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
968         current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
969         *fblock = current_block;
970         return EOK;
971     }
972
973     /* Determine indirection level of the target block */
974     unsigned int level = 0;
975     unsigned int i;
976     for (i = 1; i < 4; i++) {
977         if (iblock < fs->inode_block_limits[i]) {
978             level = i;
979             break;
980         }
981     }
982
983     if (level == 0)
984         return EIO;
985
986     /* Compute offsets for the topmost level */
987     uint64_t block_offset_in_level =
988             iblock - fs->inode_block_limits[level - 1];
989     current_block = ext4_inode_get_indirect_block(inode, level - 1);
990     uint32_t offset_in_block =
991             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
992
993     /* Sparse file */
994     if (current_block == 0) {
995         *fblock = 0;
996         return EOK;
997     }
998
999     struct      ext4_block block;
1000
1001     /*
1002      * Navigate through other levels, until we find the block number
1003      * or find null reference meaning we are dealing with sparse file
1004      */
1005     while (level > 0) {
1006         /* Load indirect block */
1007         int rc = ext4_block_get(fs->bdev, &block, current_block);
1008         if (rc != EOK)
1009             return rc;
1010
1011         /* Read block address from indirect block */
1012         current_block =
1013                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1014
1015         /* Put back indirect block untouched */
1016         rc = ext4_block_set(fs->bdev, &block);
1017         if (rc != EOK)
1018             return rc;
1019
1020         /* Check for sparse file */
1021         if (current_block == 0) {
1022             *fblock = 0;
1023             return EOK;
1024         }
1025
1026         /* Jump to the next level */
1027         level--;
1028
1029         /* Termination condition - we have address of data block loaded */
1030         if (level == 0)
1031             break;
1032
1033         /* Visit the next level */
1034         block_offset_in_level %= fs->inode_blocks_per_level[level];
1035         offset_in_block =
1036                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1037     }
1038
1039     *fblock = current_block;
1040
1041     return EOK;
1042 }
1043
1044 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1045     uint64_t iblock, uint32_t fblock)
1046 {
1047     struct ext4_fs *fs = inode_ref->fs;
1048
1049 #if CONFIG_EXTENT_ENABLE
1050     /* Handle inode using extents */
1051     if ((ext4_sb_check_feature_incompatible(&fs->sb,
1052             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1053             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1054         /* Not reachable */
1055         return ENOTSUP;
1056     }
1057 #endif
1058
1059     /* Handle simple case when we are dealing with direct reference */
1060     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1061         ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1062                 fblock);
1063         inode_ref->dirty = true;
1064
1065         return EOK;
1066     }
1067
1068     /* Determine the indirection level needed to get the desired block */
1069     unsigned int level = 0;
1070     unsigned int i;
1071     for (i = 1; i < 4; i++) {
1072         if (iblock < fs->inode_block_limits[i]) {
1073             level = i;
1074             break;
1075         }
1076     }
1077
1078     if (level == 0)
1079         return EIO;
1080
1081     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1082
1083     /* Compute offsets for the topmost level */
1084     uint64_t block_offset_in_level =
1085             iblock - fs->inode_block_limits[level - 1];
1086     uint32_t current_block =
1087             ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1088     uint32_t offset_in_block =
1089             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1090
1091     uint32_t new_block_addr;
1092
1093     struct      ext4_block block;
1094     struct      ext4_block new_block;
1095
1096     /* Is needed to allocate indirect block on the i-node level */
1097     if (current_block == 0) {
1098         /* Allocate new indirect block */
1099         int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1100         if (rc != EOK)
1101             return rc;
1102
1103         /* Update i-node */
1104         ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1105             new_block_addr);
1106         inode_ref->dirty = true;
1107
1108         /* Load newly allocated block */
1109         rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1110         if (rc != EOK) {
1111             ext4_balloc_free_block(inode_ref, new_block_addr);
1112             return rc;
1113         }
1114
1115         /* Initialize new block */
1116         memset(new_block.data, 0, block_size);
1117         new_block.dirty = true;
1118
1119         /* Put back the allocated block */
1120         rc = ext4_block_set(fs->bdev, &new_block);
1121         if (rc != EOK)
1122             return rc;
1123
1124         current_block = new_block_addr;
1125     }
1126
1127     /*
1128      * Navigate through other levels, until we find the block number
1129      * or find null reference meaning we are dealing with sparse file
1130      */
1131     while (level > 0) {
1132         int rc = ext4_block_get(fs->bdev, &block, current_block);
1133         if (rc != EOK)
1134             return rc;
1135
1136         current_block =
1137                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1138
1139         if ((level > 1) && (current_block == 0)) {
1140             /* Allocate new block */
1141             rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1142             if (rc != EOK) {
1143                 ext4_block_set(fs->bdev, &block);
1144                 return rc;
1145             }
1146
1147             /* Load newly allocated block */
1148             rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1149
1150             if (rc != EOK) {
1151                 ext4_block_set(fs->bdev, &block);
1152                 return rc;
1153             }
1154
1155             /* Initialize allocated block */
1156             memset(new_block.data, 0, block_size);
1157             new_block.dirty = true;
1158
1159             rc = ext4_block_set(fs->bdev, &new_block);
1160             if (rc != EOK) {
1161                 ext4_block_set(fs->bdev, &block);
1162                 return rc;
1163             }
1164
1165             /* Write block address to the parent */
1166             ((uint32_t *) block.data)[offset_in_block] =
1167                     to_le32(new_block_addr);
1168             block.dirty = true;
1169             current_block = new_block_addr;
1170         }
1171
1172         /* Will be finished, write the fblock address */
1173         if (level == 1) {
1174             ((uint32_t *) block.data)[offset_in_block] =
1175                     to_le32(fblock);
1176             block.dirty = true;
1177         }
1178
1179         rc = ext4_block_set(fs->bdev, &block);
1180         if (rc != EOK)
1181             return rc;
1182
1183         level--;
1184
1185         /*
1186          * If we are on the last level, break here as
1187          * there is no next level to visit
1188          */
1189         if (level == 0)
1190             break;
1191
1192         /* Visit the next level */
1193         block_offset_in_level %= fs->inode_blocks_per_level[level];
1194         offset_in_block =
1195                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1196     }
1197
1198     return EOK;
1199 }
1200
1201 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1202     uint32_t iblock)
1203 {
1204     uint32_t fblock;
1205
1206     struct ext4_fs *fs = inode_ref->fs;
1207
1208     /* Extents are handled otherwise = there is not support in this function */
1209     ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb,
1210             EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1211             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1212
1213     struct ext4_inode *inode = inode_ref->inode;
1214
1215     /* Handle simple case when we are dealing with direct reference */
1216     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1217         fblock = ext4_inode_get_direct_block(inode, iblock);
1218
1219         /* Sparse file */
1220         if (fblock == 0)
1221             return EOK;
1222
1223         ext4_inode_set_direct_block(inode, iblock, 0);
1224         return ext4_balloc_free_block(inode_ref, fblock);
1225     }
1226
1227     /* Determine the indirection level needed to get the desired block */
1228     unsigned int level = 0;
1229     unsigned int i;
1230     for (i = 1; i < 4; i++) {
1231         if (iblock < fs->inode_block_limits[i]) {
1232             level = i;
1233             break;
1234         }
1235     }
1236
1237     if (level == 0)
1238         return EIO;
1239
1240     /* Compute offsets for the topmost level */
1241     uint64_t block_offset_in_level =
1242             iblock - fs->inode_block_limits[level - 1];
1243     uint32_t current_block =
1244             ext4_inode_get_indirect_block(inode, level - 1);
1245     uint32_t offset_in_block =
1246             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1247
1248     /*
1249      * Navigate through other levels, until we find the block number
1250      * or find null reference meaning we are dealing with sparse file
1251      */
1252     struct      ext4_block block;
1253
1254     while (level > 0) {
1255
1256         /* Sparse check */
1257         if (current_block == 0)
1258             return EOK;
1259
1260         int rc = ext4_block_get(fs->bdev, &block, current_block);
1261         if (rc != EOK)
1262             return rc;
1263
1264         current_block =
1265                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1266
1267         /* Set zero if physical data block address found */
1268         if (level == 1) {
1269             ((uint32_t *) block.data)[offset_in_block] =
1270                     to_le32(0);
1271             block.dirty = true;
1272         }
1273
1274         rc = ext4_block_set(fs->bdev, &block);
1275         if (rc != EOK)
1276             return rc;
1277
1278         level--;
1279
1280         /*
1281          * If we are on the last level, break here as
1282          * there is no next level to visit
1283          */
1284         if (level == 0)
1285             break;
1286
1287         /* Visit the next level */
1288         block_offset_in_level %= fs->inode_blocks_per_level[level];
1289         offset_in_block =
1290                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1291     }
1292
1293     fblock = current_block;
1294     if (fblock == 0)
1295         return EOK;
1296
1297     /* Physical block is not referenced, it can be released */
1298     return ext4_balloc_free_block(inode_ref, fblock);
1299 }
1300
1301
1302 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1303     uint32_t *fblock, uint32_t *iblock)
1304 {
1305 #if CONFIG_EXTENT_ENABLE
1306     /* Handle extents separately */
1307     if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb,
1308             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1309             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1310         return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1311     }
1312 #endif
1313     struct ext4_sblock *sb = &inode_ref->fs->sb;
1314
1315     /* Compute next block index and allocate data block */
1316     uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1317     uint32_t block_size = ext4_sb_get_block_size(sb);
1318
1319     /* Align size i-node size */
1320     if ((inode_size % block_size) != 0)
1321         inode_size += block_size - (inode_size % block_size);
1322
1323     /* Logical blocks are numbered from 0 */
1324     uint32_t new_block_idx = inode_size / block_size;
1325
1326     /* Allocate new physical block */
1327     uint32_t phys_block;
1328     int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1329     if (rc != EOK)
1330         return rc;
1331
1332     /* Add physical block address to the i-node */
1333     rc = ext4_fs_set_inode_data_block_index(inode_ref,
1334             new_block_idx, phys_block);
1335     if (rc != EOK) {
1336         ext4_balloc_free_block(inode_ref, phys_block);
1337         return rc;
1338     }
1339
1340     /* Update i-node */
1341     ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1342     inode_ref->dirty = true;
1343
1344     *fblock = phys_block;
1345     *iblock = new_block_idx;
1346
1347     return EOK;
1348 }
1349
1350 /**
1351  * @}
1352  */
1353