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