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