5edfda550619e36ce62ba2e33b3436512a41898f
[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, "compression\n");
135     }
136     if(features_incompatible &
137             EXT4_FEATURE_INCOMPAT_FILETYPE){
138         ext4_dprintf(EXT4_DEBUG_FS, "filetype\n");
139     }
140     if(features_incompatible &
141             EXT4_FEATURE_INCOMPAT_RECOVER){
142         ext4_dprintf(EXT4_DEBUG_FS, "recover\n");
143     }
144     if(features_incompatible &
145             EXT4_FEATURE_INCOMPAT_JOURNAL_DEV){
146         ext4_dprintf(EXT4_DEBUG_FS,"journal_dev\n");
147     }
148     if(features_incompatible &
149             EXT4_FEATURE_INCOMPAT_META_BG){
150         ext4_dprintf(EXT4_DEBUG_FS, "meta_bg\n");
151     }
152     if(features_incompatible &
153             EXT4_FEATURE_INCOMPAT_EXTENTS){
154         ext4_dprintf(EXT4_DEBUG_FS, "extents\n");
155     }
156     if(features_incompatible &
157             EXT4_FEATURE_INCOMPAT_64BIT){
158         ext4_dprintf(EXT4_DEBUG_FS, "64bit\n");
159     }
160     if(features_incompatible &
161             EXT4_FEATURE_INCOMPAT_MMP){
162         ext4_dprintf(EXT4_DEBUG_FS, "mnp\n");
163     }
164     if(features_incompatible &
165             EXT4_FEATURE_INCOMPAT_FLEX_BG){
166         ext4_dprintf(EXT4_DEBUG_FS, "flex_bg\n");
167     }
168     if(features_incompatible &
169             EXT4_FEATURE_INCOMPAT_EA_INODE){
170         ext4_dprintf(EXT4_DEBUG_FS, "ea_inode\n");
171     }
172     if(features_incompatible &
173             EXT4_FEATURE_INCOMPAT_DIRDATA){
174         ext4_dprintf(EXT4_DEBUG_FS, "dirdata\n");
175     }
176     if(features_incompatible &
177             EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM){
178         ext4_dprintf(EXT4_DEBUG_FS, "meta_csum\n");
179     }
180     if(features_incompatible &
181             EXT4_FEATURE_INCOMPAT_LARGEDIR){
182         ext4_dprintf(EXT4_DEBUG_FS, "largedir\n");
183     }
184     if(features_incompatible &
185             EXT4_FEATURE_INCOMPAT_INLINE_DATA){
186         ext4_dprintf(EXT4_DEBUG_FS, "inline_data\n");
187     }
188 }
189 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
190 {
191     if(features_compatible &
192             EXT4_FEATURE_COMPAT_DIR_PREALLOC){
193         ext4_dprintf(EXT4_DEBUG_FS, " dir_prealloc\n");
194     }
195     if(features_compatible &
196             EXT4_FEATURE_COMPAT_IMAGIC_INODES){
197         ext4_dprintf(EXT4_DEBUG_FS, "imagic_inodes\n");
198     }
199     if(features_compatible &
200             EXT4_FEATURE_COMPAT_HAS_JOURNAL){
201         ext4_dprintf(EXT4_DEBUG_FS, "has_journal\n");
202     }
203     if(features_compatible &
204             EXT4_FEATURE_COMPAT_EXT_ATTR){
205         ext4_dprintf(EXT4_DEBUG_FS, "ext_attr\n");
206     }
207     if(features_compatible &
208             EXT4_FEATURE_COMPAT_RESIZE_INODE){
209         ext4_dprintf(EXT4_DEBUG_FS, "resize_inode\n");
210     }
211     if(features_compatible &
212             EXT4_FEATURE_COMPAT_DIR_INDEX){
213         ext4_dprintf(EXT4_DEBUG_FS, "dir_index\n");
214     }
215 }
216
217 static void ext4_fs_debug_features_ro(uint32_t features_ro)
218 {
219     if(features_ro &
220             EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER){
221         ext4_dprintf(EXT4_DEBUG_FS, "sparse_super\n");
222     }
223     if(features_ro &
224             EXT4_FEATURE_RO_COMPAT_LARGE_FILE){
225         ext4_dprintf(EXT4_DEBUG_FS, "large_file\n");
226     }
227     if(features_ro &
228             EXT4_FEATURE_RO_COMPAT_BTREE_DIR){
229         ext4_dprintf(EXT4_DEBUG_FS, "btree_dir\n");
230     }
231     if(features_ro &
232             EXT4_FEATURE_RO_COMPAT_HUGE_FILE){
233         ext4_dprintf(EXT4_DEBUG_FS, "huge_file\n");
234     }
235     if(features_ro &
236             EXT4_FEATURE_RO_COMPAT_GDT_CSUM){
237         ext4_dprintf(EXT4_DEBUG_FS, "gtd_csum\n");
238     }
239     if(features_ro &
240             EXT4_FEATURE_RO_COMPAT_DIR_NLINK){
241         ext4_dprintf(EXT4_DEBUG_FS, "dir_nlink\n");
242     }
243     if(features_ro &
244             EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE){
245         ext4_dprintf(EXT4_DEBUG_FS, "extra_isize\n");
246     }
247     if(features_ro &
248             EXT4_FEATURE_RO_COMPAT_QUOTA){
249         ext4_dprintf(EXT4_DEBUG_FS, "quota\n");
250     }
251     if(features_ro &
252             EXT4_FEATURE_RO_COMPAT_BIGALLOC){
253         ext4_dprintf(EXT4_DEBUG_FS, "bigalloc\n");
254     }
255     if(features_ro &
256             EXT4_FEATURE_RO_COMPAT_METADATA_CSUM){
257         ext4_dprintf(EXT4_DEBUG_FS, "metadata_csum\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             (~CONFIG_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             (~CONFIG_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     *read_only = false;
311
312     return EOK;
313 }
314
315
316 /**@brief Initialize block bitmap in block group.
317  * @param bg_ref Reference to block group
318  * @return Error code
319  */
320 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
321 {
322     uint32_t i;
323     uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
324             bg_ref->block_group, &bg_ref->fs->sb);
325
326     struct ext4_block block_bitmap;
327     int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
328             bitmap_block_addr);
329     if (rc != EOK)
330         return rc;
331
332
333     memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
334
335     /* Determine first block and first data block in group */
336     uint32_t first_idx = 0;
337
338     uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
339             &bg_ref->fs->sb, bg_ref);
340     uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
341             &bg_ref->fs->sb, first_data);
342
343     /*Set bits from to first block to first data block - 1 to one (allocated)*/
344     /*TODO: Optimize it*/
345     for (i = first_idx; i < first_data_idx; ++i)
346         ext4_bmap_bit_set(block_bitmap.data, i);
347
348
349     block_bitmap.dirty = true;
350
351     /* Save bitmap */
352     return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
353 }
354
355 /**@brief Initialize i-node bitmap in block group.
356  * @param bg_ref Reference to block group
357  * @return Error code
358  */
359 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
360 {
361     /* Load bitmap */
362     uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
363             bg_ref->block_group, &bg_ref->fs->sb);
364
365     struct ext4_block block_bitmap;
366     int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
367             bitmap_block_addr);
368     if (rc != EOK)
369         return rc;
370
371     /* Initialize all bitmap bits to zero */
372     uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
373     uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
374
375     memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
376
377     uint32_t start_bit = inodes_per_group;
378     uint32_t end_bit = block_size * 8;
379
380     uint32_t i;
381     for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
382         ext4_bmap_bit_set(block_bitmap.data, i);
383
384     if (i < end_bit)
385         memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
386
387     block_bitmap.dirty = true;
388
389     /* Save bitmap */
390     return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
391 }
392
393 /**@brief Initialize i-node table in block group.
394  * @param bg_ref Reference to block group
395  * @return Error code
396  */
397 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
398 {
399     struct ext4_sblock *sb = &bg_ref->fs->sb;
400
401     uint32_t inode_size = ext4_get32(sb, inode_size);
402     uint32_t block_size = ext4_sb_get_block_size(sb);
403     uint32_t inodes_per_block = block_size / inode_size;
404     uint32_t inodes_in_group  = ext4_inodes_in_group_cnt(sb, bg_ref->index);
405     uint32_t table_blocks = inodes_in_group / inodes_per_block;
406     uint32_t fblock;
407
408     if (inodes_in_group % inodes_per_block)
409         table_blocks++;
410
411     /* Compute initialization bounds */
412     uint32_t first_block = ext4_bg_get_inode_table_first_block(
413             bg_ref->block_group, sb);
414
415     uint32_t last_block = first_block + table_blocks - 1;
416
417     /* Initialization of all itable blocks */
418     for (fblock = first_block; fblock <= last_block; ++fblock) {
419
420         struct ext4_block block;
421         int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
422         if (rc != EOK)
423             return rc;
424
425         memset(block.data, 0, block_size);
426         block.dirty = true;
427
428         ext4_block_set(bg_ref->fs->bdev, &block);
429         if (rc != EOK)
430             return rc;
431     }
432
433     return EOK;
434 }
435
436 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
437         uint32_t bgid, uint32_t dsc_per_block)
438 {
439     uint32_t first_meta_bg, dsc_id;
440
441     int has_super = 0;
442
443     dsc_id = bgid / dsc_per_block;
444     first_meta_bg = ext4_sb_first_meta_bg(s);
445
446     if (!ext4_sb_has_feature_incompatible(s, EXT4_FEATURE_INCOMPAT_META_BG) ||
447             dsc_id < first_meta_bg)
448         return ext4_get32(s, first_data_block) + dsc_id + 1;
449
450     if (ext4_sb_is_super_in_bg(s, bgid))
451         has_super = 1;
452
453     return (has_super + ext4_fs_first_bg_block_no(s, bgid));
454 }
455
456
457 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
458     struct ext4_block_group_ref *ref)
459 {
460     /* Compute number of descriptors, that fits in one data block */
461     uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
462             ext4_sb_get_desc_size(&fs->sb);
463
464     /* Block group descriptor table starts at the next block after superblock */
465     uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid,
466             dsc_per_block);
467
468     uint32_t offset = (bgid % dsc_per_block) *
469             ext4_sb_get_desc_size(&fs->sb);
470
471     int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
472     if (rc != EOK)
473         return rc;
474
475     ref->block_group = (void *)(ref->block.data + offset);
476     ref->fs = fs;
477     ref->index = bgid;
478     ref->dirty = false;
479
480     if (ext4_bg_has_flag(ref->block_group,
481             EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
482         rc = ext4_fs_init_block_bitmap(ref);
483         if (rc != EOK) {
484             ext4_block_set(fs->bdev, &ref->block);
485             return rc;
486         }
487         ext4_bg_clear_flag(ref->block_group,
488             EXT4_BLOCK_GROUP_BLOCK_UNINIT);
489
490         ref->dirty = true;
491     }
492
493     if (ext4_bg_has_flag(ref->block_group,
494             EXT4_BLOCK_GROUP_INODE_UNINIT)) {
495         rc = ext4_fs_init_inode_bitmap(ref);
496         if (rc != EOK) {
497             ext4_block_set(ref->fs->bdev, &ref->block);
498             return rc;
499         }
500
501         ext4_bg_clear_flag(ref->block_group,
502             EXT4_BLOCK_GROUP_INODE_UNINIT);
503
504         if (!ext4_bg_has_flag(ref->block_group,
505                 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
506             rc = ext4_fs_init_inode_table(ref);
507             if (rc != EOK){
508                 ext4_block_set(fs->bdev, &ref->block);
509                 return rc;
510             }
511
512             ext4_bg_set_flag(ref->block_group,
513                 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
514         }
515
516         ref->dirty = true;
517     }
518
519     return EOK;
520 }
521
522 /**@brief  Compute checksum of block group descriptor.
523  * @param sb   Superblock
524  * @param bgid Index of block group in the filesystem
525  * @param bg   Block group to compute checksum for
526  * @return Checksum value
527  */
528 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
529     struct ext4_bgroup *bg)
530 {
531     /* If checksum not supported, 0 will be returned */
532     uint16_t crc = 0;
533
534     /* Compute the checksum only if the filesystem supports it */
535     if (ext4_sb_has_feature_read_only(sb,
536             EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
537         uint8_t  *base = (uint8_t  *)bg;
538         uint8_t  *checksum = (uint8_t  *)&bg->checksum;
539
540         uint32_t offset = (uint32_t) (checksum - base);
541
542         /* Convert block group index to little endian */
543         uint32_t le_group = to_le32(bgid);
544
545         /* Initialization */
546         crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
547
548         /* Include index of block group */
549         crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
550
551         /* Compute crc from the first part (stop before checksum field) */
552         crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
553
554         /* Skip checksum */
555         offset += sizeof(bg->checksum);
556
557         /* Checksum of the rest of block group descriptor */
558         if ((ext4_sb_has_feature_incompatible(sb,
559                 EXT4_FEATURE_INCOMPAT_64BIT)) &&
560                 (offset < ext4_sb_get_desc_size(sb)))
561
562             crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
563                     ext4_sb_get_desc_size(sb) - offset);
564     }
565     return crc;
566 }
567
568 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
569 {
570     /* Check if reference modified */
571     if (ref->dirty) {
572         /* Compute new checksum of block group */
573         uint16_t checksum =
574                 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
575                         ref->block_group);
576
577         ref->block_group->checksum = to_le16(checksum);
578
579         /* Mark block dirty for writing changes to physical device */
580         ref->block.dirty = true;
581     }
582
583     /* Put back block, that contains block group descriptor */
584     return ext4_block_set(ref->fs->bdev, &ref->block);
585 }
586
587 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
588     struct ext4_inode_ref *ref)
589 {
590     /* Compute number of i-nodes, that fits in one data block */
591     uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
592
593     /*
594      * Inode numbers are 1-based, but it is simpler to work with 0-based
595      * when computing indices
596      */
597     index -= 1;
598     uint32_t block_group = index / inodes_per_group;
599     uint32_t offset_in_group = index % inodes_per_group;
600
601     /* Load block group, where i-node is located */
602     struct ext4_block_group_ref bg_ref;
603
604     int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
605     if (rc != EOK) {
606         return rc;
607     }
608
609     /* Load block address, where i-node table is located */
610     uint32_t inode_table_start =
611             ext4_bg_get_inode_table_first_block(bg_ref.block_group,
612                     &fs->sb);
613
614     /* Put back block group reference (not needed more) */
615     rc = ext4_fs_put_block_group_ref(&bg_ref);
616     if (rc != EOK) {
617         return rc;
618     }
619
620     /* Compute position of i-node in the block group */
621     uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
622     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
623     uint32_t byte_offset_in_group = offset_in_group * inode_size;
624
625     /* Compute block address */
626     uint64_t block_id = inode_table_start +
627             (byte_offset_in_group / block_size);
628
629
630     rc = ext4_block_get(fs->bdev, &ref->block, block_id);
631     if (rc != EOK) {
632         return rc;
633     }
634
635     /* Compute position of i-node in the data block */
636     uint32_t offset_in_block = byte_offset_in_group % block_size;
637     ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
638
639     /* We need to store the original value of index in the reference */
640     ref->index = index + 1;
641     ref->fs = fs;
642     ref->dirty = false;
643
644     return EOK;
645 }
646
647 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
648 {
649     /* Check if reference modified */
650     if (ref->dirty) {
651         /* Mark block dirty for writing changes to physical device */
652         ref->block.dirty = true;
653     }
654
655     /* Put back block, that contains i-node */
656     return  ext4_block_set(ref->fs->bdev, &ref->block);
657 }
658
659 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
660     bool is_directory)
661 {
662     /* Check if newly allocated i-node will be a directory */
663     uint32_t i;
664     bool is_dir;
665
666     is_dir = is_directory;
667
668     /* Allocate inode by allocation algorithm */
669     uint32_t index;
670     int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
671     if (rc != EOK)
672         return rc;
673
674     /* Load i-node from on-disk i-node table */
675     rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
676     if (rc != EOK) {
677         ext4_ialloc_free_inode(fs, index, is_dir);
678         return rc;
679     }
680
681     /* Initialize i-node */
682     struct ext4_inode *inode = inode_ref->inode;
683
684     uint16_t mode;
685     if (is_dir) {
686         /*
687          * Default directory permissions to be compatible with other systems
688          * 0777 (octal) == rwxrwxrwx
689          */
690
691         mode = 0777;
692         mode |= EXT4_INODE_MODE_DIRECTORY;
693         ext4_inode_set_mode(&fs->sb, inode, mode);
694         ext4_inode_set_links_count(inode, 0);
695
696     } else {
697         /*
698          * Default file permissions to be compatible with other systems
699          * 0666 (octal) == rw-rw-rw-
700          */
701
702         mode = 0666;
703         mode |= EXT4_INODE_MODE_FILE;
704         ext4_inode_set_mode(&fs->sb, inode, mode);
705         ext4_inode_set_links_count(inode, 1);
706     }
707
708
709     ext4_inode_set_uid(inode, 0);
710     ext4_inode_set_gid(inode, 0);
711     ext4_inode_set_size(inode, 0);
712     ext4_inode_set_access_time(inode, 0);
713     ext4_inode_set_change_inode_time(inode, 0);
714     ext4_inode_set_modification_time(inode, 0);
715     ext4_inode_set_deletion_time(inode, 0);
716     ext4_inode_set_blocks_count(&fs->sb, inode, 0);
717     ext4_inode_set_flags(inode, 0);
718     ext4_inode_set_generation(inode, 0);
719
720     /* Reset blocks array */
721     for (i = 0; i < EXT4_INODE_BLOCKS; i++)
722         inode->blocks[i] = 0;
723
724 #if CONFIG_EXTENT_ENABLE
725     /* Initialize extents if needed */
726     if (ext4_sb_has_feature_incompatible(
727             &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
728         ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
729
730
731         /* Initialize extent root header */
732         struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
733         ext4_extent_header_set_depth(header, 0);
734         ext4_extent_header_set_entries_count(header, 0);
735         ext4_extent_header_set_generation(header, 0);
736         ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
737
738         uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
739                 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
740
741         ext4_extent_header_set_max_entries_count(header, max_entries);
742     }
743 #endif
744
745     inode_ref->dirty = true;
746
747     return EOK;
748 }
749
750 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
751 {
752     struct ext4_fs *fs = inode_ref->fs;
753     uint32_t offset;
754     uint32_t suboffset;
755 #if CONFIG_EXTENT_ENABLE
756     /* For extents must be data block destroyed by other way */
757     if ((ext4_sb_has_feature_incompatible(&fs->sb,
758             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
759             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
760         /* Data structures are released during truncate operation... */
761         goto finish;
762     }
763 #endif
764     /* Release all indirect (no data) blocks */
765
766     /* 1) Single indirect */
767     uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
768     if (fblock != 0) {
769         int rc = ext4_balloc_free_block(inode_ref, fblock);
770         if (rc != EOK)
771             return rc;
772
773         ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
774     }
775
776     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
777     uint32_t count = block_size / sizeof(uint32_t);
778
779     struct ext4_block  block;
780
781     /* 2) Double indirect */
782     fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
783     if (fblock != 0) {
784         int rc = ext4_block_get(fs->bdev, &block, fblock);
785         if (rc != EOK)
786             return rc;
787
788         uint32_t ind_block;
789         for (offset = 0; offset < count; ++offset) {
790             ind_block = to_le32(((uint32_t *) block.data)[offset]);
791
792             if (ind_block != 0) {
793                 rc = ext4_balloc_free_block(inode_ref, ind_block);
794                 if (rc != EOK) {
795                     ext4_block_set(fs->bdev, &block);
796                     return rc;
797                 }
798             }
799         }
800
801         ext4_block_set(fs->bdev, &block);
802         rc = ext4_balloc_free_block(inode_ref, fblock);
803         if (rc != EOK)
804             return rc;
805
806         ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
807     }
808
809     /* 3) Tripple indirect */
810     struct ext4_block  subblock;
811     fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
812     if (fblock != 0) {
813         int rc = ext4_block_get(fs->bdev, &block, fblock);
814         if (rc != EOK)
815             return rc;
816
817         uint32_t ind_block;
818         for ( offset = 0; offset < count; ++offset) {
819             ind_block = to_le32(((uint32_t *) block.data)[offset]);
820
821             if (ind_block != 0) {
822                 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
823                 if (rc != EOK) {
824                     ext4_block_set(fs->bdev, &block);
825                     return rc;
826                 }
827
828                 uint32_t ind_subblock;
829                 for (suboffset = 0; suboffset < count;
830                         ++suboffset) {
831                     ind_subblock = to_le32(((uint32_t *)
832                             subblock.data)[suboffset]);
833
834                     if (ind_subblock != 0) {
835                         rc = ext4_balloc_free_block(inode_ref, ind_subblock);
836                         if (rc != EOK) {
837                             ext4_block_set(fs->bdev, &subblock);
838                             ext4_block_set(fs->bdev, &block);
839                             return rc;
840                         }
841                     }
842                 }
843
844                 ext4_block_set(fs->bdev, &subblock);
845
846
847                 rc = ext4_balloc_free_block(inode_ref, ind_block);
848                 if (rc != EOK) {
849                     ext4_block_set(fs->bdev, &block);
850                     return rc;
851                 }
852             }
853         }
854
855         ext4_block_set(fs->bdev, &block);
856         rc = ext4_balloc_free_block(inode_ref, fblock);
857         if (rc != EOK)
858             return rc;
859
860         ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
861     }
862 #if CONFIG_EXTENT_ENABLE
863     finish:
864 #endif
865     /* Mark inode dirty for writing to the physical device */
866     inode_ref->dirty = true;
867
868     /* Free block with extended attributes if present */
869     uint32_t xattr_block = ext4_inode_get_file_acl(
870             inode_ref->inode, &fs->sb);
871     if (xattr_block) {
872         int rc = ext4_balloc_free_block(inode_ref, xattr_block);
873         if (rc != EOK)
874             return rc;
875
876         ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
877     }
878
879     /* Free inode by allocator */
880     int rc;
881     if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
882             EXT4_INODE_MODE_DIRECTORY))
883         rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
884     else
885         rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
886
887     return rc;
888 }
889
890 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
891     uint64_t new_size)
892 {
893     struct ext4_sblock *sb = &inode_ref->fs->sb;
894     uint32_t i;
895
896     /* Check flags, if i-node can be truncated */
897     if (!ext4_inode_can_truncate(sb, inode_ref->inode))
898         return EINVAL;
899
900     /* If sizes are equal, nothing has to be done. */
901     uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
902     if (old_size == new_size)
903         return EOK;
904
905     /* It's not suppported to make the larger file by truncate operation */
906     if (old_size < new_size)
907         return EINVAL;
908
909     /* Compute how many blocks will be released */
910     uint64_t size_diff = old_size - new_size;
911     uint32_t block_size  = ext4_sb_get_block_size(sb);
912     uint32_t diff_blocks_count = size_diff / block_size;
913     if (size_diff % block_size != 0)
914         diff_blocks_count++;
915
916     uint32_t old_blocks_count = old_size / block_size;
917     if (old_size % block_size != 0)
918         old_blocks_count++;
919 #if CONFIG_EXTENT_ENABLE
920     if ((ext4_sb_has_feature_incompatible(sb,
921             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
922             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
923
924         /* Extents require special operation */
925         int rc = ext4_extent_release_blocks_from(inode_ref,
926                 old_blocks_count - diff_blocks_count);
927         if (rc != EOK)
928             return rc;
929
930     } else
931 #endif
932     {
933         /* Release data blocks from the end of file */
934
935         /* Starting from 1 because of logical blocks are numbered from 0 */
936         for (i = 1; i <= diff_blocks_count; ++i) {
937             int rc = ext4_fs_release_inode_block(inode_ref,
938                     old_blocks_count - i);
939             if (rc != EOK)
940                 return rc;
941         }
942     }
943
944     /* Update i-node */
945     ext4_inode_set_size(inode_ref->inode, new_size);
946     inode_ref->dirty = true;
947
948     return EOK;
949 }
950
951 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
952     uint64_t iblock, uint32_t *fblock)
953 {
954     struct ext4_fs *fs = inode_ref->fs;
955
956     /* For empty file is situation simple */
957     if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
958         *fblock = 0;
959         return EOK;
960     }
961
962     uint32_t current_block;
963 #if CONFIG_EXTENT_ENABLE
964     /* Handle i-node using extents */
965     if ((ext4_sb_has_feature_incompatible(&fs->sb,
966             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
967             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
968
969
970         int rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
971         if (rc != EOK)
972             return rc;
973
974         *fblock = current_block;
975         return EOK;
976     }
977 #endif
978
979     struct ext4_inode *inode = inode_ref->inode;
980
981     /* Direct block are read directly from array in i-node structure */
982     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
983         current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
984         *fblock = current_block;
985         return EOK;
986     }
987
988     /* Determine indirection level of the target block */
989     unsigned int level = 0;
990     unsigned int i;
991     for (i = 1; i < 4; i++) {
992         if (iblock < fs->inode_block_limits[i]) {
993             level = i;
994             break;
995         }
996     }
997
998     if (level == 0)
999         return EIO;
1000
1001     /* Compute offsets for the topmost level */
1002     uint64_t block_offset_in_level =
1003             iblock - fs->inode_block_limits[level - 1];
1004     current_block = ext4_inode_get_indirect_block(inode, level - 1);
1005     uint32_t offset_in_block =
1006             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1007
1008     /* Sparse file */
1009     if (current_block == 0) {
1010         *fblock = 0;
1011         return EOK;
1012     }
1013
1014     struct ext4_block block;
1015
1016     /*
1017      * Navigate through other levels, until we find the block number
1018      * or find null reference meaning we are dealing with sparse file
1019      */
1020     while (level > 0) {
1021         /* Load indirect block */
1022         int rc = ext4_block_get(fs->bdev, &block, current_block);
1023         if (rc != EOK)
1024             return rc;
1025
1026         /* Read block address from indirect block */
1027         current_block =
1028                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1029
1030         /* Put back indirect block untouched */
1031         rc = ext4_block_set(fs->bdev, &block);
1032         if (rc != EOK)
1033             return rc;
1034
1035         /* Check for sparse file */
1036         if (current_block == 0) {
1037             *fblock = 0;
1038             return EOK;
1039         }
1040
1041         /* Jump to the next level */
1042         level--;
1043
1044         /* Termination condition - we have address of data block loaded */
1045         if (level == 0)
1046             break;
1047
1048         /* Visit the next level */
1049         block_offset_in_level %= fs->inode_blocks_per_level[level];
1050         offset_in_block =
1051                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1052     }
1053
1054     *fblock = current_block;
1055
1056     return EOK;
1057 }
1058
1059 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1060     uint64_t iblock, uint32_t fblock)
1061 {
1062     struct ext4_fs *fs = inode_ref->fs;
1063
1064 #if CONFIG_EXTENT_ENABLE
1065     /* Handle inode using extents */
1066     if ((ext4_sb_has_feature_incompatible(&fs->sb,
1067             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1068             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1069         /* Not reachable */
1070         return ENOTSUP;
1071     }
1072 #endif
1073
1074     /* Handle simple case when we are dealing with direct reference */
1075     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1076         ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1077                 fblock);
1078         inode_ref->dirty = true;
1079
1080         return EOK;
1081     }
1082
1083     /* Determine the indirection level needed to get the desired block */
1084     unsigned int level = 0;
1085     unsigned int i;
1086     for (i = 1; i < 4; i++) {
1087         if (iblock < fs->inode_block_limits[i]) {
1088             level = i;
1089             break;
1090         }
1091     }
1092
1093     if (level == 0)
1094         return EIO;
1095
1096     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1097
1098     /* Compute offsets for the topmost level */
1099     uint64_t block_offset_in_level =
1100             iblock - fs->inode_block_limits[level - 1];
1101     uint32_t current_block =
1102             ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1103     uint32_t offset_in_block =
1104             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1105
1106     uint32_t new_block_addr;
1107
1108     struct ext4_block block;
1109     struct ext4_block new_block;
1110
1111     /* Is needed to allocate indirect block on the i-node level */
1112     if (current_block == 0) {
1113         /* Allocate new indirect block */
1114         int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1115         if (rc != EOK)
1116             return rc;
1117
1118         /* Update i-node */
1119         ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1120             new_block_addr);
1121         inode_ref->dirty = true;
1122
1123         /* Load newly allocated block */
1124         rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1125         if (rc != EOK) {
1126             ext4_balloc_free_block(inode_ref, new_block_addr);
1127             return rc;
1128         }
1129
1130         /* Initialize new block */
1131         memset(new_block.data, 0, block_size);
1132         new_block.dirty = true;
1133
1134         /* Put back the allocated block */
1135         rc = ext4_block_set(fs->bdev, &new_block);
1136         if (rc != EOK)
1137             return rc;
1138
1139         current_block = new_block_addr;
1140     }
1141
1142     /*
1143      * Navigate through other levels, until we find the block number
1144      * or find null reference meaning we are dealing with sparse file
1145      */
1146     while (level > 0) {
1147         int rc = ext4_block_get(fs->bdev, &block, current_block);
1148         if (rc != EOK)
1149             return rc;
1150
1151         current_block =
1152                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1153
1154         if ((level > 1) && (current_block == 0)) {
1155             /* Allocate new block */
1156             rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1157             if (rc != EOK) {
1158                 ext4_block_set(fs->bdev, &block);
1159                 return rc;
1160             }
1161
1162             /* Load newly allocated block */
1163             rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1164
1165             if (rc != EOK) {
1166                 ext4_block_set(fs->bdev, &block);
1167                 return rc;
1168             }
1169
1170             /* Initialize allocated block */
1171             memset(new_block.data, 0, block_size);
1172             new_block.dirty = true;
1173
1174             rc = ext4_block_set(fs->bdev, &new_block);
1175             if (rc != EOK) {
1176                 ext4_block_set(fs->bdev, &block);
1177                 return rc;
1178             }
1179
1180             /* Write block address to the parent */
1181             ((uint32_t *) block.data)[offset_in_block] =
1182                     to_le32(new_block_addr);
1183             block.dirty = true;
1184             current_block = new_block_addr;
1185         }
1186
1187         /* Will be finished, write the fblock address */
1188         if (level == 1) {
1189             ((uint32_t *) block.data)[offset_in_block] =
1190                     to_le32(fblock);
1191             block.dirty = true;
1192         }
1193
1194         rc = ext4_block_set(fs->bdev, &block);
1195         if (rc != EOK)
1196             return rc;
1197
1198         level--;
1199
1200         /*
1201          * If we are on the last level, break here as
1202          * there is no next level to visit
1203          */
1204         if (level == 0)
1205             break;
1206
1207         /* Visit the next level */
1208         block_offset_in_level %= fs->inode_blocks_per_level[level];
1209         offset_in_block =
1210                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1211     }
1212
1213     return EOK;
1214 }
1215
1216 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1217     uint32_t iblock)
1218 {
1219     uint32_t fblock;
1220
1221     struct ext4_fs *fs = inode_ref->fs;
1222
1223     /* Extents are handled otherwise = there is not support in this function */
1224     ext4_assert(!(ext4_sb_has_feature_incompatible(&fs->sb,
1225             EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1226             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1227
1228     struct ext4_inode *inode = inode_ref->inode;
1229
1230     /* Handle simple case when we are dealing with direct reference */
1231     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1232         fblock = ext4_inode_get_direct_block(inode, iblock);
1233
1234         /* Sparse file */
1235         if (fblock == 0)
1236             return EOK;
1237
1238         ext4_inode_set_direct_block(inode, iblock, 0);
1239         return ext4_balloc_free_block(inode_ref, fblock);
1240     }
1241
1242     /* Determine the indirection level needed to get the desired block */
1243     unsigned int level = 0;
1244     unsigned int i;
1245     for (i = 1; i < 4; i++) {
1246         if (iblock < fs->inode_block_limits[i]) {
1247             level = i;
1248             break;
1249         }
1250     }
1251
1252     if (level == 0)
1253         return EIO;
1254
1255     /* Compute offsets for the topmost level */
1256     uint64_t block_offset_in_level =
1257             iblock - fs->inode_block_limits[level - 1];
1258     uint32_t current_block =
1259             ext4_inode_get_indirect_block(inode, level - 1);
1260     uint32_t offset_in_block =
1261             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1262
1263     /*
1264      * Navigate through other levels, until we find the block number
1265      * or find null reference meaning we are dealing with sparse file
1266      */
1267     struct ext4_block block;
1268
1269     while (level > 0) {
1270
1271         /* Sparse check */
1272         if (current_block == 0)
1273             return EOK;
1274
1275         int rc = ext4_block_get(fs->bdev, &block, current_block);
1276         if (rc != EOK)
1277             return rc;
1278
1279         current_block =
1280                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1281
1282         /* Set zero if physical data block address found */
1283         if (level == 1) {
1284             ((uint32_t *) block.data)[offset_in_block] =
1285                     to_le32(0);
1286             block.dirty = true;
1287         }
1288
1289         rc = ext4_block_set(fs->bdev, &block);
1290         if (rc != EOK)
1291             return rc;
1292
1293         level--;
1294
1295         /*
1296          * If we are on the last level, break here as
1297          * there is no next level to visit
1298          */
1299         if (level == 0)
1300             break;
1301
1302         /* Visit the next level */
1303         block_offset_in_level %= fs->inode_blocks_per_level[level];
1304         offset_in_block =
1305                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1306     }
1307
1308     fblock = current_block;
1309     if (fblock == 0)
1310         return EOK;
1311
1312     /* Physical block is not referenced, it can be released */
1313     return ext4_balloc_free_block(inode_ref, fblock);
1314 }
1315
1316
1317 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1318     uint32_t *fblock, uint32_t *iblock)
1319 {
1320 #if CONFIG_EXTENT_ENABLE
1321     /* Handle extents separately */
1322     if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1323             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1324             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1325         return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1326     }
1327 #endif
1328     struct ext4_sblock *sb = &inode_ref->fs->sb;
1329
1330     /* Compute next block index and allocate data block */
1331     uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1332     uint32_t block_size = ext4_sb_get_block_size(sb);
1333
1334     /* Align size i-node size */
1335     if ((inode_size % block_size) != 0)
1336         inode_size += block_size - (inode_size % block_size);
1337
1338     /* Logical blocks are numbered from 0 */
1339     uint32_t new_block_idx = inode_size / block_size;
1340
1341     /* Allocate new physical block */
1342     uint32_t phys_block;
1343     int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1344     if (rc != EOK)
1345         return rc;
1346
1347     /* Add physical block address to the i-node */
1348     rc = ext4_fs_set_inode_data_block_index(inode_ref,
1349             new_block_idx, phys_block);
1350     if (rc != EOK) {
1351         ext4_balloc_free_block(inode_ref, phys_block);
1352         return rc;
1353     }
1354
1355     /* Update i-node */
1356     ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1357     inode_ref->dirty = true;
1358
1359     *fblock = phys_block;
1360     *iblock = new_block_idx;
1361
1362     return EOK;
1363 }
1364
1365 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1366 {
1367     uint16_t link;
1368     if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1369                 EXT4_INODE_MODE_DIRECTORY)){
1370         ext4_inode_set_links_count(inode_ref->inode, 0);
1371         return;
1372     }
1373
1374     link = ext4_inode_get_links_count(inode_ref->inode);
1375     link++;
1376     ext4_inode_set_links_count(inode_ref->inode, link);
1377
1378
1379     bool is_dx = ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1380             EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1381                  ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1382
1383     if(is_dx &&  link > 1){
1384         if(link >= EXT4_LINK_MAX || link == 2){
1385             ext4_inode_set_links_count(inode_ref->inode, 1);
1386
1387             uint32_t v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1388             v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1389             ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1390         }
1391     }
1392 }
1393
1394 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1395 {
1396     if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1397                 EXT4_INODE_MODE_DIRECTORY)){
1398         ext4_inode_set_links_count(inode_ref->inode, 0);
1399         return;
1400     }
1401
1402     uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1403
1404
1405     if(links > 2)
1406         ext4_inode_set_links_count(inode_ref->inode, links - 1);
1407
1408 }
1409
1410
1411 /**
1412  * @}
1413  */
1414