2fedb8050e4cdcef5a8f3974472ef48c64a2439d
[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             (~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     *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
863     finish:
864     /* Mark inode dirty for writing to the physical device */
865     inode_ref->dirty = true;
866
867     /* Free block with extended attributes if present */
868     uint32_t xattr_block = ext4_inode_get_file_acl(
869             inode_ref->inode, &fs->sb);
870     if (xattr_block) {
871         int rc = ext4_balloc_free_block(inode_ref, xattr_block);
872         if (rc != EOK)
873             return rc;
874
875         ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
876     }
877
878     /* Free inode by allocator */
879     int rc;
880     if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
881             EXT4_INODE_MODE_DIRECTORY))
882         rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
883     else
884         rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
885
886     return rc;
887 }
888
889 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
890     uint64_t new_size)
891 {
892     struct ext4_sblock *sb = &inode_ref->fs->sb;
893     uint32_t i;
894
895     /* Check flags, if i-node can be truncated */
896     if (!ext4_inode_can_truncate(sb, inode_ref->inode))
897         return EINVAL;
898
899     /* If sizes are equal, nothing has to be done. */
900     uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
901     if (old_size == new_size)
902         return EOK;
903
904     /* It's not suppported to make the larger file by truncate operation */
905     if (old_size < new_size)
906         return EINVAL;
907
908     /* Compute how many blocks will be released */
909     uint64_t size_diff = old_size - new_size;
910     uint32_t block_size  = ext4_sb_get_block_size(sb);
911     uint32_t diff_blocks_count = size_diff / block_size;
912     if (size_diff % block_size != 0)
913         diff_blocks_count++;
914
915     uint32_t old_blocks_count = old_size / block_size;
916     if (old_size % block_size != 0)
917         old_blocks_count++;
918 #if CONFIG_EXTENT_ENABLE
919     if ((ext4_sb_has_feature_incompatible(sb,
920             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
921             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
922
923         /* Extents require special operation */
924         int rc = ext4_extent_release_blocks_from(inode_ref,
925                 old_blocks_count - diff_blocks_count);
926         if (rc != EOK)
927             return rc;
928
929     } else
930 #endif
931     {
932         /* Release data blocks from the end of file */
933
934         /* Starting from 1 because of logical blocks are numbered from 0 */
935         for (i = 1; i <= diff_blocks_count; ++i) {
936             int rc = ext4_fs_release_inode_block(inode_ref,
937                     old_blocks_count - i);
938             if (rc != EOK)
939                 return rc;
940         }
941     }
942
943     /* Update i-node */
944     ext4_inode_set_size(inode_ref->inode, new_size);
945     inode_ref->dirty = true;
946
947     return EOK;
948 }
949
950 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
951     uint64_t iblock, uint32_t *fblock)
952 {
953     struct ext4_fs *fs = inode_ref->fs;
954
955     /* For empty file is situation simple */
956     if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
957         *fblock = 0;
958         return EOK;
959     }
960
961     uint32_t current_block;
962 #if CONFIG_EXTENT_ENABLE
963     /* Handle i-node using extents */
964     if ((ext4_sb_has_feature_incompatible(&fs->sb,
965             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
966             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
967
968
969         int rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
970         if (rc != EOK)
971             return rc;
972
973         *fblock = current_block;
974         return EOK;
975     }
976 #endif
977
978     struct ext4_inode *inode = inode_ref->inode;
979
980     /* Direct block are read directly from array in i-node structure */
981     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
982         current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
983         *fblock = current_block;
984         return EOK;
985     }
986
987     /* Determine indirection level of the target block */
988     unsigned int level = 0;
989     unsigned int i;
990     for (i = 1; i < 4; i++) {
991         if (iblock < fs->inode_block_limits[i]) {
992             level = i;
993             break;
994         }
995     }
996
997     if (level == 0)
998         return EIO;
999
1000     /* Compute offsets for the topmost level */
1001     uint64_t block_offset_in_level =
1002             iblock - fs->inode_block_limits[level - 1];
1003     current_block = ext4_inode_get_indirect_block(inode, level - 1);
1004     uint32_t offset_in_block =
1005             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1006
1007     /* Sparse file */
1008     if (current_block == 0) {
1009         *fblock = 0;
1010         return EOK;
1011     }
1012
1013     struct ext4_block block;
1014
1015     /*
1016      * Navigate through other levels, until we find the block number
1017      * or find null reference meaning we are dealing with sparse file
1018      */
1019     while (level > 0) {
1020         /* Load indirect block */
1021         int rc = ext4_block_get(fs->bdev, &block, current_block);
1022         if (rc != EOK)
1023             return rc;
1024
1025         /* Read block address from indirect block */
1026         current_block =
1027                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1028
1029         /* Put back indirect block untouched */
1030         rc = ext4_block_set(fs->bdev, &block);
1031         if (rc != EOK)
1032             return rc;
1033
1034         /* Check for sparse file */
1035         if (current_block == 0) {
1036             *fblock = 0;
1037             return EOK;
1038         }
1039
1040         /* Jump to the next level */
1041         level--;
1042
1043         /* Termination condition - we have address of data block loaded */
1044         if (level == 0)
1045             break;
1046
1047         /* Visit the next level */
1048         block_offset_in_level %= fs->inode_blocks_per_level[level];
1049         offset_in_block =
1050                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1051     }
1052
1053     *fblock = current_block;
1054
1055     return EOK;
1056 }
1057
1058 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1059     uint64_t iblock, uint32_t fblock)
1060 {
1061     struct ext4_fs *fs = inode_ref->fs;
1062
1063 #if CONFIG_EXTENT_ENABLE
1064     /* Handle inode using extents */
1065     if ((ext4_sb_has_feature_incompatible(&fs->sb,
1066             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1067             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1068         /* Not reachable */
1069         return ENOTSUP;
1070     }
1071 #endif
1072
1073     /* Handle simple case when we are dealing with direct reference */
1074     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1075         ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1076                 fblock);
1077         inode_ref->dirty = true;
1078
1079         return EOK;
1080     }
1081
1082     /* Determine the indirection level needed to get the desired block */
1083     unsigned int level = 0;
1084     unsigned int i;
1085     for (i = 1; i < 4; i++) {
1086         if (iblock < fs->inode_block_limits[i]) {
1087             level = i;
1088             break;
1089         }
1090     }
1091
1092     if (level == 0)
1093         return EIO;
1094
1095     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1096
1097     /* Compute offsets for the topmost level */
1098     uint64_t block_offset_in_level =
1099             iblock - fs->inode_block_limits[level - 1];
1100     uint32_t current_block =
1101             ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1102     uint32_t offset_in_block =
1103             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1104
1105     uint32_t new_block_addr;
1106
1107     struct ext4_block block;
1108     struct ext4_block new_block;
1109
1110     /* Is needed to allocate indirect block on the i-node level */
1111     if (current_block == 0) {
1112         /* Allocate new indirect block */
1113         int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1114         if (rc != EOK)
1115             return rc;
1116
1117         /* Update i-node */
1118         ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1119             new_block_addr);
1120         inode_ref->dirty = true;
1121
1122         /* Load newly allocated block */
1123         rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1124         if (rc != EOK) {
1125             ext4_balloc_free_block(inode_ref, new_block_addr);
1126             return rc;
1127         }
1128
1129         /* Initialize new block */
1130         memset(new_block.data, 0, block_size);
1131         new_block.dirty = true;
1132
1133         /* Put back the allocated block */
1134         rc = ext4_block_set(fs->bdev, &new_block);
1135         if (rc != EOK)
1136             return rc;
1137
1138         current_block = new_block_addr;
1139     }
1140
1141     /*
1142      * Navigate through other levels, until we find the block number
1143      * or find null reference meaning we are dealing with sparse file
1144      */
1145     while (level > 0) {
1146         int rc = ext4_block_get(fs->bdev, &block, current_block);
1147         if (rc != EOK)
1148             return rc;
1149
1150         current_block =
1151                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1152
1153         if ((level > 1) && (current_block == 0)) {
1154             /* Allocate new block */
1155             rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1156             if (rc != EOK) {
1157                 ext4_block_set(fs->bdev, &block);
1158                 return rc;
1159             }
1160
1161             /* Load newly allocated block */
1162             rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1163
1164             if (rc != EOK) {
1165                 ext4_block_set(fs->bdev, &block);
1166                 return rc;
1167             }
1168
1169             /* Initialize allocated block */
1170             memset(new_block.data, 0, block_size);
1171             new_block.dirty = true;
1172
1173             rc = ext4_block_set(fs->bdev, &new_block);
1174             if (rc != EOK) {
1175                 ext4_block_set(fs->bdev, &block);
1176                 return rc;
1177             }
1178
1179             /* Write block address to the parent */
1180             ((uint32_t *) block.data)[offset_in_block] =
1181                     to_le32(new_block_addr);
1182             block.dirty = true;
1183             current_block = new_block_addr;
1184         }
1185
1186         /* Will be finished, write the fblock address */
1187         if (level == 1) {
1188             ((uint32_t *) block.data)[offset_in_block] =
1189                     to_le32(fblock);
1190             block.dirty = true;
1191         }
1192
1193         rc = ext4_block_set(fs->bdev, &block);
1194         if (rc != EOK)
1195             return rc;
1196
1197         level--;
1198
1199         /*
1200          * If we are on the last level, break here as
1201          * there is no next level to visit
1202          */
1203         if (level == 0)
1204             break;
1205
1206         /* Visit the next level */
1207         block_offset_in_level %= fs->inode_blocks_per_level[level];
1208         offset_in_block =
1209                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1210     }
1211
1212     return EOK;
1213 }
1214
1215 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1216     uint32_t iblock)
1217 {
1218     uint32_t fblock;
1219
1220     struct ext4_fs *fs = inode_ref->fs;
1221
1222     /* Extents are handled otherwise = there is not support in this function */
1223     ext4_assert(!(ext4_sb_has_feature_incompatible(&fs->sb,
1224             EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1225             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1226
1227     struct ext4_inode *inode = inode_ref->inode;
1228
1229     /* Handle simple case when we are dealing with direct reference */
1230     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1231         fblock = ext4_inode_get_direct_block(inode, iblock);
1232
1233         /* Sparse file */
1234         if (fblock == 0)
1235             return EOK;
1236
1237         ext4_inode_set_direct_block(inode, iblock, 0);
1238         return ext4_balloc_free_block(inode_ref, fblock);
1239     }
1240
1241     /* Determine the indirection level needed to get the desired block */
1242     unsigned int level = 0;
1243     unsigned int i;
1244     for (i = 1; i < 4; i++) {
1245         if (iblock < fs->inode_block_limits[i]) {
1246             level = i;
1247             break;
1248         }
1249     }
1250
1251     if (level == 0)
1252         return EIO;
1253
1254     /* Compute offsets for the topmost level */
1255     uint64_t block_offset_in_level =
1256             iblock - fs->inode_block_limits[level - 1];
1257     uint32_t current_block =
1258             ext4_inode_get_indirect_block(inode, level - 1);
1259     uint32_t offset_in_block =
1260             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1261
1262     /*
1263      * Navigate through other levels, until we find the block number
1264      * or find null reference meaning we are dealing with sparse file
1265      */
1266     struct ext4_block block;
1267
1268     while (level > 0) {
1269
1270         /* Sparse check */
1271         if (current_block == 0)
1272             return EOK;
1273
1274         int rc = ext4_block_get(fs->bdev, &block, current_block);
1275         if (rc != EOK)
1276             return rc;
1277
1278         current_block =
1279                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1280
1281         /* Set zero if physical data block address found */
1282         if (level == 1) {
1283             ((uint32_t *) block.data)[offset_in_block] =
1284                     to_le32(0);
1285             block.dirty = true;
1286         }
1287
1288         rc = ext4_block_set(fs->bdev, &block);
1289         if (rc != EOK)
1290             return rc;
1291
1292         level--;
1293
1294         /*
1295          * If we are on the last level, break here as
1296          * there is no next level to visit
1297          */
1298         if (level == 0)
1299             break;
1300
1301         /* Visit the next level */
1302         block_offset_in_level %= fs->inode_blocks_per_level[level];
1303         offset_in_block =
1304                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1305     }
1306
1307     fblock = current_block;
1308     if (fblock == 0)
1309         return EOK;
1310
1311     /* Physical block is not referenced, it can be released */
1312     return ext4_balloc_free_block(inode_ref, fblock);
1313 }
1314
1315
1316 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1317     uint32_t *fblock, uint32_t *iblock)
1318 {
1319 #if CONFIG_EXTENT_ENABLE
1320     /* Handle extents separately */
1321     if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1322             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1323             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1324         return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1325     }
1326 #endif
1327     struct ext4_sblock *sb = &inode_ref->fs->sb;
1328
1329     /* Compute next block index and allocate data block */
1330     uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1331     uint32_t block_size = ext4_sb_get_block_size(sb);
1332
1333     /* Align size i-node size */
1334     if ((inode_size % block_size) != 0)
1335         inode_size += block_size - (inode_size % block_size);
1336
1337     /* Logical blocks are numbered from 0 */
1338     uint32_t new_block_idx = inode_size / block_size;
1339
1340     /* Allocate new physical block */
1341     uint32_t phys_block;
1342     int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1343     if (rc != EOK)
1344         return rc;
1345
1346     /* Add physical block address to the i-node */
1347     rc = ext4_fs_set_inode_data_block_index(inode_ref,
1348             new_block_idx, phys_block);
1349     if (rc != EOK) {
1350         ext4_balloc_free_block(inode_ref, phys_block);
1351         return rc;
1352     }
1353
1354     /* Update i-node */
1355     ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1356     inode_ref->dirty = true;
1357
1358     *fblock = phys_block;
1359     *iblock = new_block_idx;
1360
1361     return EOK;
1362 }
1363
1364 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1365 {
1366     uint16_t link;
1367     if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1368                 EXT4_INODE_MODE_DIRECTORY)){
1369         ext4_inode_set_links_count(inode_ref->inode, 0);
1370         return;
1371     }
1372
1373     link = ext4_inode_get_links_count(inode_ref->inode);
1374     link++;
1375     ext4_inode_set_links_count(inode_ref->inode, link);
1376
1377
1378     bool is_dx = ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1379             EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1380                  ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1381
1382     if(is_dx &&  link > 1){
1383         if(link >= EXT4_LINK_MAX || link == 2){
1384             ext4_inode_set_links_count(inode_ref->inode, 1);
1385
1386             uint32_t v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1387             v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1388             ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1389         }
1390     }
1391 }
1392
1393 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1394 {
1395     if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1396                 EXT4_INODE_MODE_DIRECTORY)){
1397         ext4_inode_set_links_count(inode_ref->inode, 0);
1398         return;
1399     }
1400
1401     uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1402
1403
1404     if(links > 2)
1405         ext4_inode_set_links_count(inode_ref->inode, links - 1);
1406
1407 }
1408
1409
1410 /**
1411  * @}
1412  */
1413