Support for feature dir_nlink
[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
695     } else {
696         /*
697          * Default file permissions to be compatible with other systems
698          * 0666 (octal) == rw-rw-rw-
699          */
700
701         mode = 0666;
702         mode |= EXT4_INODE_MODE_FILE;
703         ext4_inode_set_mode(&fs->sb, inode, mode);
704     }
705
706     ext4_inode_set_links_count(inode, 0);
707     ext4_inode_set_uid(inode, 0);
708     ext4_inode_set_gid(inode, 0);
709     ext4_inode_set_size(inode, 0);
710     ext4_inode_set_access_time(inode, 0);
711     ext4_inode_set_change_inode_time(inode, 0);
712     ext4_inode_set_modification_time(inode, 0);
713     ext4_inode_set_deletion_time(inode, 0);
714     ext4_inode_set_blocks_count(&fs->sb, inode, 0);
715     ext4_inode_set_flags(inode, 0);
716     ext4_inode_set_generation(inode, 0);
717
718     /* Reset blocks array */
719     for (i = 0; i < EXT4_INODE_BLOCKS; i++)
720         inode->blocks[i] = 0;
721
722 #if CONFIG_EXTENT_ENABLE
723     /* Initialize extents if needed */
724     if (ext4_sb_has_feature_incompatible(
725             &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
726         ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
727
728
729         /* Initialize extent root header */
730         struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
731         ext4_extent_header_set_depth(header, 0);
732         ext4_extent_header_set_entries_count(header, 0);
733         ext4_extent_header_set_generation(header, 0);
734         ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
735
736         uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
737                 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
738
739         ext4_extent_header_set_max_entries_count(header, max_entries);
740     }
741 #endif
742
743     inode_ref->dirty = true;
744
745     return EOK;
746 }
747
748 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
749 {
750     struct ext4_fs *fs = inode_ref->fs;
751     uint32_t offset;
752     uint32_t suboffset;
753 #if CONFIG_EXTENT_ENABLE
754     /* For extents must be data block destroyed by other way */
755     if ((ext4_sb_has_feature_incompatible(&fs->sb,
756             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
757             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
758         /* Data structures are released during truncate operation... */
759         goto finish;
760     }
761 #endif
762     /* Release all indirect (no data) blocks */
763
764     /* 1) Single indirect */
765     uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
766     if (fblock != 0) {
767         int rc = ext4_balloc_free_block(inode_ref, fblock);
768         if (rc != EOK)
769             return rc;
770
771         ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
772     }
773
774     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
775     uint32_t count = block_size / sizeof(uint32_t);
776
777     struct ext4_block  block;
778
779     /* 2) Double indirect */
780     fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
781     if (fblock != 0) {
782         int rc = ext4_block_get(fs->bdev, &block, fblock);
783         if (rc != EOK)
784             return rc;
785
786         uint32_t ind_block;
787         for (offset = 0; offset < count; ++offset) {
788             ind_block = to_le32(((uint32_t *) block.data)[offset]);
789
790             if (ind_block != 0) {
791                 rc = ext4_balloc_free_block(inode_ref, ind_block);
792                 if (rc != EOK) {
793                     ext4_block_set(fs->bdev, &block);
794                     return rc;
795                 }
796             }
797         }
798
799         ext4_block_set(fs->bdev, &block);
800         rc = ext4_balloc_free_block(inode_ref, fblock);
801         if (rc != EOK)
802             return rc;
803
804         ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
805     }
806
807     /* 3) Tripple indirect */
808     struct ext4_block  subblock;
809     fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
810     if (fblock != 0) {
811         int rc = ext4_block_get(fs->bdev, &block, fblock);
812         if (rc != EOK)
813             return rc;
814
815         uint32_t ind_block;
816         for ( offset = 0; offset < count; ++offset) {
817             ind_block = to_le32(((uint32_t *) block.data)[offset]);
818
819             if (ind_block != 0) {
820                 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
821                 if (rc != EOK) {
822                     ext4_block_set(fs->bdev, &block);
823                     return rc;
824                 }
825
826                 uint32_t ind_subblock;
827                 for (suboffset = 0; suboffset < count;
828                         ++suboffset) {
829                     ind_subblock = to_le32(((uint32_t *)
830                             subblock.data)[suboffset]);
831
832                     if (ind_subblock != 0) {
833                         rc = ext4_balloc_free_block(inode_ref, ind_subblock);
834                         if (rc != EOK) {
835                             ext4_block_set(fs->bdev, &subblock);
836                             ext4_block_set(fs->bdev, &block);
837                             return rc;
838                         }
839                     }
840                 }
841
842                 ext4_block_set(fs->bdev, &subblock);
843
844
845                 rc = ext4_balloc_free_block(inode_ref, ind_block);
846                 if (rc != EOK) {
847                     ext4_block_set(fs->bdev, &block);
848                     return rc;
849                 }
850             }
851         }
852
853         ext4_block_set(fs->bdev, &block);
854         rc = ext4_balloc_free_block(inode_ref, fblock);
855         if (rc != EOK)
856             return rc;
857
858         ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
859     }
860
861     finish:
862     /* Mark inode dirty for writing to the physical device */
863     inode_ref->dirty = true;
864
865     /* Free block with extended attributes if present */
866     uint32_t xattr_block = ext4_inode_get_file_acl(
867             inode_ref->inode, &fs->sb);
868     if (xattr_block) {
869         int rc = ext4_balloc_free_block(inode_ref, xattr_block);
870         if (rc != EOK)
871             return rc;
872
873         ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
874     }
875
876     /* Free inode by allocator */
877     int rc;
878     if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
879             EXT4_INODE_MODE_DIRECTORY))
880         rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
881     else
882         rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
883
884     return rc;
885 }
886
887 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
888     uint64_t new_size)
889 {
890     struct ext4_sblock *sb = &inode_ref->fs->sb;
891     uint32_t i;
892
893     /* Check flags, if i-node can be truncated */
894     if (!ext4_inode_can_truncate(sb, inode_ref->inode))
895         return EINVAL;
896
897     /* If sizes are equal, nothing has to be done. */
898     uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
899     if (old_size == new_size)
900         return EOK;
901
902     /* It's not suppported to make the larger file by truncate operation */
903     if (old_size < new_size)
904         return EINVAL;
905
906     /* Compute how many blocks will be released */
907     uint64_t size_diff = old_size - new_size;
908     uint32_t block_size  = ext4_sb_get_block_size(sb);
909     uint32_t diff_blocks_count = size_diff / block_size;
910     if (size_diff % block_size != 0)
911         diff_blocks_count++;
912
913     uint32_t old_blocks_count = old_size / block_size;
914     if (old_size % block_size != 0)
915         old_blocks_count++;
916 #if CONFIG_EXTENT_ENABLE
917     if ((ext4_sb_has_feature_incompatible(sb,
918             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
919             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
920
921         /* Extents require special operation */
922         int rc = ext4_extent_release_blocks_from(inode_ref,
923                 old_blocks_count - diff_blocks_count);
924         if (rc != EOK)
925             return rc;
926
927     } else
928 #endif
929     {
930         /* Release data blocks from the end of file */
931
932         /* Starting from 1 because of logical blocks are numbered from 0 */
933         for (i = 1; i <= diff_blocks_count; ++i) {
934             int rc = ext4_fs_release_inode_block(inode_ref,
935                     old_blocks_count - i);
936             if (rc != EOK)
937                 return rc;
938         }
939     }
940
941     /* Update i-node */
942     ext4_inode_set_size(inode_ref->inode, new_size);
943     inode_ref->dirty = true;
944
945     return EOK;
946 }
947
948 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
949     uint64_t iblock, uint32_t *fblock)
950 {
951     struct ext4_fs *fs = inode_ref->fs;
952
953     /* For empty file is situation simple */
954     if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
955         *fblock = 0;
956         return EOK;
957     }
958
959     uint32_t current_block;
960 #if CONFIG_EXTENT_ENABLE
961     /* Handle i-node using extents */
962     if ((ext4_sb_has_feature_incompatible(&fs->sb,
963             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
964             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
965
966
967         int rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
968         if (rc != EOK)
969             return rc;
970
971         *fblock = current_block;
972         return EOK;
973     }
974 #endif
975
976     struct ext4_inode *inode = inode_ref->inode;
977
978     /* Direct block are read directly from array in i-node structure */
979     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
980         current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
981         *fblock = current_block;
982         return EOK;
983     }
984
985     /* Determine indirection level of the target block */
986     unsigned int level = 0;
987     unsigned int i;
988     for (i = 1; i < 4; i++) {
989         if (iblock < fs->inode_block_limits[i]) {
990             level = i;
991             break;
992         }
993     }
994
995     if (level == 0)
996         return EIO;
997
998     /* Compute offsets for the topmost level */
999     uint64_t block_offset_in_level =
1000             iblock - fs->inode_block_limits[level - 1];
1001     current_block = ext4_inode_get_indirect_block(inode, level - 1);
1002     uint32_t offset_in_block =
1003             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1004
1005     /* Sparse file */
1006     if (current_block == 0) {
1007         *fblock = 0;
1008         return EOK;
1009     }
1010
1011     struct ext4_block block;
1012
1013     /*
1014      * Navigate through other levels, until we find the block number
1015      * or find null reference meaning we are dealing with sparse file
1016      */
1017     while (level > 0) {
1018         /* Load indirect block */
1019         int rc = ext4_block_get(fs->bdev, &block, current_block);
1020         if (rc != EOK)
1021             return rc;
1022
1023         /* Read block address from indirect block */
1024         current_block =
1025                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1026
1027         /* Put back indirect block untouched */
1028         rc = ext4_block_set(fs->bdev, &block);
1029         if (rc != EOK)
1030             return rc;
1031
1032         /* Check for sparse file */
1033         if (current_block == 0) {
1034             *fblock = 0;
1035             return EOK;
1036         }
1037
1038         /* Jump to the next level */
1039         level--;
1040
1041         /* Termination condition - we have address of data block loaded */
1042         if (level == 0)
1043             break;
1044
1045         /* Visit the next level */
1046         block_offset_in_level %= fs->inode_blocks_per_level[level];
1047         offset_in_block =
1048                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1049     }
1050
1051     *fblock = current_block;
1052
1053     return EOK;
1054 }
1055
1056 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1057     uint64_t iblock, uint32_t fblock)
1058 {
1059     struct ext4_fs *fs = inode_ref->fs;
1060
1061 #if CONFIG_EXTENT_ENABLE
1062     /* Handle inode using extents */
1063     if ((ext4_sb_has_feature_incompatible(&fs->sb,
1064             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1065             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1066         /* Not reachable */
1067         return ENOTSUP;
1068     }
1069 #endif
1070
1071     /* Handle simple case when we are dealing with direct reference */
1072     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1073         ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1074                 fblock);
1075         inode_ref->dirty = true;
1076
1077         return EOK;
1078     }
1079
1080     /* Determine the indirection level needed to get the desired block */
1081     unsigned int level = 0;
1082     unsigned int i;
1083     for (i = 1; i < 4; i++) {
1084         if (iblock < fs->inode_block_limits[i]) {
1085             level = i;
1086             break;
1087         }
1088     }
1089
1090     if (level == 0)
1091         return EIO;
1092
1093     uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1094
1095     /* Compute offsets for the topmost level */
1096     uint64_t block_offset_in_level =
1097             iblock - fs->inode_block_limits[level - 1];
1098     uint32_t current_block =
1099             ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1100     uint32_t offset_in_block =
1101             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1102
1103     uint32_t new_block_addr;
1104
1105     struct ext4_block block;
1106     struct ext4_block new_block;
1107
1108     /* Is needed to allocate indirect block on the i-node level */
1109     if (current_block == 0) {
1110         /* Allocate new indirect block */
1111         int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1112         if (rc != EOK)
1113             return rc;
1114
1115         /* Update i-node */
1116         ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1117             new_block_addr);
1118         inode_ref->dirty = true;
1119
1120         /* Load newly allocated block */
1121         rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1122         if (rc != EOK) {
1123             ext4_balloc_free_block(inode_ref, new_block_addr);
1124             return rc;
1125         }
1126
1127         /* Initialize new block */
1128         memset(new_block.data, 0, block_size);
1129         new_block.dirty = true;
1130
1131         /* Put back the allocated block */
1132         rc = ext4_block_set(fs->bdev, &new_block);
1133         if (rc != EOK)
1134             return rc;
1135
1136         current_block = new_block_addr;
1137     }
1138
1139     /*
1140      * Navigate through other levels, until we find the block number
1141      * or find null reference meaning we are dealing with sparse file
1142      */
1143     while (level > 0) {
1144         int rc = ext4_block_get(fs->bdev, &block, current_block);
1145         if (rc != EOK)
1146             return rc;
1147
1148         current_block =
1149                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1150
1151         if ((level > 1) && (current_block == 0)) {
1152             /* Allocate new block */
1153             rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1154             if (rc != EOK) {
1155                 ext4_block_set(fs->bdev, &block);
1156                 return rc;
1157             }
1158
1159             /* Load newly allocated block */
1160             rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1161
1162             if (rc != EOK) {
1163                 ext4_block_set(fs->bdev, &block);
1164                 return rc;
1165             }
1166
1167             /* Initialize allocated block */
1168             memset(new_block.data, 0, block_size);
1169             new_block.dirty = true;
1170
1171             rc = ext4_block_set(fs->bdev, &new_block);
1172             if (rc != EOK) {
1173                 ext4_block_set(fs->bdev, &block);
1174                 return rc;
1175             }
1176
1177             /* Write block address to the parent */
1178             ((uint32_t *) block.data)[offset_in_block] =
1179                     to_le32(new_block_addr);
1180             block.dirty = true;
1181             current_block = new_block_addr;
1182         }
1183
1184         /* Will be finished, write the fblock address */
1185         if (level == 1) {
1186             ((uint32_t *) block.data)[offset_in_block] =
1187                     to_le32(fblock);
1188             block.dirty = true;
1189         }
1190
1191         rc = ext4_block_set(fs->bdev, &block);
1192         if (rc != EOK)
1193             return rc;
1194
1195         level--;
1196
1197         /*
1198          * If we are on the last level, break here as
1199          * there is no next level to visit
1200          */
1201         if (level == 0)
1202             break;
1203
1204         /* Visit the next level */
1205         block_offset_in_level %= fs->inode_blocks_per_level[level];
1206         offset_in_block =
1207                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1208     }
1209
1210     return EOK;
1211 }
1212
1213 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1214     uint32_t iblock)
1215 {
1216     uint32_t fblock;
1217
1218     struct ext4_fs *fs = inode_ref->fs;
1219
1220     /* Extents are handled otherwise = there is not support in this function */
1221     ext4_assert(!(ext4_sb_has_feature_incompatible(&fs->sb,
1222             EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1223             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1224
1225     struct ext4_inode *inode = inode_ref->inode;
1226
1227     /* Handle simple case when we are dealing with direct reference */
1228     if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1229         fblock = ext4_inode_get_direct_block(inode, iblock);
1230
1231         /* Sparse file */
1232         if (fblock == 0)
1233             return EOK;
1234
1235         ext4_inode_set_direct_block(inode, iblock, 0);
1236         return ext4_balloc_free_block(inode_ref, fblock);
1237     }
1238
1239     /* Determine the indirection level needed to get the desired block */
1240     unsigned int level = 0;
1241     unsigned int i;
1242     for (i = 1; i < 4; i++) {
1243         if (iblock < fs->inode_block_limits[i]) {
1244             level = i;
1245             break;
1246         }
1247     }
1248
1249     if (level == 0)
1250         return EIO;
1251
1252     /* Compute offsets for the topmost level */
1253     uint64_t block_offset_in_level =
1254             iblock - fs->inode_block_limits[level - 1];
1255     uint32_t current_block =
1256             ext4_inode_get_indirect_block(inode, level - 1);
1257     uint32_t offset_in_block =
1258             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1259
1260     /*
1261      * Navigate through other levels, until we find the block number
1262      * or find null reference meaning we are dealing with sparse file
1263      */
1264     struct ext4_block block;
1265
1266     while (level > 0) {
1267
1268         /* Sparse check */
1269         if (current_block == 0)
1270             return EOK;
1271
1272         int rc = ext4_block_get(fs->bdev, &block, current_block);
1273         if (rc != EOK)
1274             return rc;
1275
1276         current_block =
1277                 to_le32(((uint32_t *) block.data)[offset_in_block]);
1278
1279         /* Set zero if physical data block address found */
1280         if (level == 1) {
1281             ((uint32_t *) block.data)[offset_in_block] =
1282                     to_le32(0);
1283             block.dirty = true;
1284         }
1285
1286         rc = ext4_block_set(fs->bdev, &block);
1287         if (rc != EOK)
1288             return rc;
1289
1290         level--;
1291
1292         /*
1293          * If we are on the last level, break here as
1294          * there is no next level to visit
1295          */
1296         if (level == 0)
1297             break;
1298
1299         /* Visit the next level */
1300         block_offset_in_level %= fs->inode_blocks_per_level[level];
1301         offset_in_block =
1302                 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1303     }
1304
1305     fblock = current_block;
1306     if (fblock == 0)
1307         return EOK;
1308
1309     /* Physical block is not referenced, it can be released */
1310     return ext4_balloc_free_block(inode_ref, fblock);
1311 }
1312
1313
1314 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1315     uint32_t *fblock, uint32_t *iblock)
1316 {
1317 #if CONFIG_EXTENT_ENABLE
1318     /* Handle extents separately */
1319     if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1320             EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1321             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1322         return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1323     }
1324 #endif
1325     struct ext4_sblock *sb = &inode_ref->fs->sb;
1326
1327     /* Compute next block index and allocate data block */
1328     uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1329     uint32_t block_size = ext4_sb_get_block_size(sb);
1330
1331     /* Align size i-node size */
1332     if ((inode_size % block_size) != 0)
1333         inode_size += block_size - (inode_size % block_size);
1334
1335     /* Logical blocks are numbered from 0 */
1336     uint32_t new_block_idx = inode_size / block_size;
1337
1338     /* Allocate new physical block */
1339     uint32_t phys_block;
1340     int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1341     if (rc != EOK)
1342         return rc;
1343
1344     /* Add physical block address to the i-node */
1345     rc = ext4_fs_set_inode_data_block_index(inode_ref,
1346             new_block_idx, phys_block);
1347     if (rc != EOK) {
1348         ext4_balloc_free_block(inode_ref, phys_block);
1349         return rc;
1350     }
1351
1352     /* Update i-node */
1353     ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1354     inode_ref->dirty = true;
1355
1356     *fblock = phys_block;
1357     *iblock = new_block_idx;
1358
1359     return EOK;
1360 }
1361
1362 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1363 {
1364     uint16_t link;
1365     if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1366                 EXT4_INODE_MODE_DIRECTORY)){
1367         ext4_inode_set_links_count(inode_ref->inode, 0);
1368         return;
1369     }
1370
1371     link = ext4_inode_get_links_count(inode_ref->inode);
1372     link++;
1373     ext4_inode_set_links_count(inode_ref->inode, link);
1374
1375
1376     bool is_dx = ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1377             EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1378                  ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1379
1380     if(is_dx &&  link > 1){
1381         if(link >= EXT4_LINK_MAX || link == 2){
1382             ext4_inode_set_links_count(inode_ref->inode, 1);
1383
1384             uint32_t v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1385             v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1386             ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1387         }
1388     }
1389 }
1390
1391 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1392 {
1393     if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1394                 EXT4_INODE_MODE_DIRECTORY)){
1395         ext4_inode_set_links_count(inode_ref->inode, 0);
1396         return;
1397     }
1398
1399     uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1400
1401
1402     if(links > 2)
1403         ext4_inode_set_links_count(inode_ref->inode, links - 1);
1404
1405 }
1406
1407
1408 /**
1409  * @}
1410  */
1411