Create default directory structure in mkfs
[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_crc32c.h"
48 #include "ext4_debug.h"
49 #include "ext4_block_group.h"
50 #include "ext4_balloc.h"
51 #include "ext4_bitmap.h"
52 #include "ext4_inode.h"
53 #include "ext4_ialloc.h"
54 #include "ext4_extent.h"
55
56 #include <string.h>
57
58 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
59 {
60         int r, i;
61         uint16_t tmp;
62         uint32_t bsize;
63         bool read_only = false;
64
65         ext4_assert(fs && bdev);
66
67         fs->bdev = bdev;
68
69         r = ext4_sb_read(fs->bdev, &fs->sb);
70         if (r != EOK)
71                 return r;
72
73         if (!ext4_sb_check(&fs->sb))
74                 return ENOTSUP;
75
76         bsize = ext4_sb_get_block_size(&fs->sb);
77         if (bsize > EXT4_MAX_BLOCK_SIZE)
78                 return ENXIO;
79
80         r = ext4_fs_check_features(fs, &read_only);
81         if (r != EOK)
82                 return r;
83
84         if (read_only)
85                 return ENOTSUP;
86
87         /* Compute limits for indirect block levels */
88         uint32_t blocks_id = bsize / sizeof(uint32_t);
89
90         fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
91         fs->inode_blocks_per_level[0] = 1;
92
93         for (i = 1; i < 4; i++) {
94                 fs->inode_blocks_per_level[i] =
95                     fs->inode_blocks_per_level[i - 1] * blocks_id;
96                 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
97                                             fs->inode_blocks_per_level[i];
98         }
99
100         /*Validate FS*/
101         tmp = ext4_get16(&fs->sb, state);
102         if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
103                 ext4_dbg(DEBUG_FS, DBG_WARN
104                                 "last umount error: superblock fs_error flag\n");
105
106
107         /* Mark system as mounted */
108         ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
109         r = ext4_sb_write(fs->bdev, &fs->sb);
110         if (r != EOK)
111                 return r;
112
113         /*Update mount count*/
114         ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
115
116         return r;
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_inc(uint32_t features_incompatible)
130 {
131         if (features_incompatible & EXT4_FINCOM_COMPRESSION)
132                 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
133         if (features_incompatible & EXT4_FINCOM_FILETYPE)
134                 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
135         if (features_incompatible & EXT4_FINCOM_RECOVER)
136                 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
137         if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
138                 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
139         if (features_incompatible & EXT4_FINCOM_META_BG)
140                 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
141         if (features_incompatible & EXT4_FINCOM_EXTENTS)
142                 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
143         if (features_incompatible & EXT4_FINCOM_64BIT)
144                 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
145         if (features_incompatible & EXT4_FINCOM_MMP)
146                 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
147         if (features_incompatible & EXT4_FINCOM_FLEX_BG)
148                 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
149         if (features_incompatible & EXT4_FINCOM_EA_INODE)
150                 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
151         if (features_incompatible & EXT4_FINCOM_DIRDATA)
152                 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
153         if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
154                 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
155         if (features_incompatible & EXT4_FINCOM_LARGEDIR)
156                 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
157         if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
158                 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
159 }
160 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
161 {
162         if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
163                 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
164         if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
165                 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
166         if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
167                 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
168         if (features_compatible & EXT4_FCOM_EXT_ATTR)
169                 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
170         if (features_compatible & EXT4_FCOM_RESIZE_INODE)
171                 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
172         if (features_compatible & EXT4_FCOM_DIR_INDEX)
173                 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
174 }
175
176 static void ext4_fs_debug_features_ro(uint32_t features_ro)
177 {
178         if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
179                 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
180         if (features_ro & EXT4_FRO_COM_LARGE_FILE)
181                 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
182         if (features_ro & EXT4_FRO_COM_BTREE_DIR)
183                 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
184         if (features_ro & EXT4_FRO_COM_HUGE_FILE)
185                 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
186         if (features_ro & EXT4_FRO_COM_GDT_CSUM)
187                 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
188         if (features_ro & EXT4_FRO_COM_DIR_NLINK)
189                 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
190         if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
191                 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
192         if (features_ro & EXT4_FRO_COM_QUOTA)
193                 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
194         if (features_ro & EXT4_FRO_COM_BIGALLOC)
195                 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
196         if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
197                 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
198 }
199
200 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
201 {
202         ext4_assert(fs && read_only);
203         uint32_t v;
204         if (ext4_get32(&fs->sb, rev_level) == 0) {
205                 *read_only = false;
206                 return EOK;
207         }
208
209         ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
210         ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
211
212         ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
213         ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
214
215         ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
216         ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
217
218         /*Check features_incompatible*/
219         v = (ext4_get32(&fs->sb, features_incompatible) &
220              (~CONFIG_SUPPORTED_FINCOM));
221         if (v) {
222                 ext4_dbg(DEBUG_FS, DBG_ERROR
223                                 "sblock has unsupported features incompatible:\n");
224                 ext4_fs_debug_features_inc(v);
225                 return ENOTSUP;
226         }
227
228         /*Check features_read_only*/
229         v = (ext4_get32(&fs->sb, features_read_only) &
230              (~CONFIG_SUPPORTED_FRO_COM));
231         if (v) {
232                 ext4_dbg(DEBUG_FS, DBG_WARN
233                                 "sblock has unsupported features read only:\n");
234                 ext4_fs_debug_features_ro(v);
235                 *read_only = true;
236                 return EOK;
237         }
238         *read_only = false;
239
240         return EOK;
241 }
242
243 /**@brief Determine whether the block is inside the group.
244  * @param baddr   block address
245  * @param bgid    block group id
246  * @return Error code
247  */
248 static int ext4_block_in_group(struct ext4_sblock *s,
249                                ext4_fsblk_t baddr,
250                                uint32_t bgid)
251 {
252         uint32_t actual_bgid;
253         actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
254         if (actual_bgid == bgid)
255                 return 1;
256         return 0;
257 }
258
259 /**@brief   To avoid calling the atomic setbit hundreds or thousands of times, we only
260  *          need to use it within a single byte (to ensure we get endianness right).
261  *          We can use memset for the rest of the bitmap as there are no other users.
262  */
263 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
264 {
265         int i;
266
267         if (start_bit >= end_bit)
268                 return;
269
270         for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
271                 ext4_bmap_bit_set(bitmap, i);
272
273         if (i < end_bit)
274                 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
275 }
276
277 /**@brief Initialize block bitmap in block group.
278  * @param bg_ref Reference to block group
279  * @return Error code
280  */
281 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
282 {
283         uint32_t i, bit, bit_max;
284         uint32_t group_blocks;
285         uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
286         uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
287         uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
288         ext4_fsblk_t bitmap_block_addr =
289             ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
290         ext4_fsblk_t bitmap_inode_addr =
291             ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
292         ext4_fsblk_t inode_table_addr =
293             ext4_bg_get_inode_table_first_block(bg_ref->block_group,
294                                                 &bg_ref->fs->sb);
295         ext4_fsblk_t first_group_addr =
296             ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
297
298         uint32_t dsc_per_block =
299             ext4_sb_get_block_size(&bg_ref->fs->sb) /
300             ext4_sb_get_desc_size(&bg_ref->fs->sb);
301
302         bool flex_bg =
303                 ext4_sb_feature_incom(&bg_ref->fs->sb, EXT4_FINCOM_FLEX_BG);
304
305         uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
306
307         struct ext4_block block_bitmap;
308         int rc =
309             ext4_block_get_noread(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
310         if (rc != EOK)
311                 return rc;
312
313         memset(block_bitmap.data, 0, block_size);
314
315         bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
316         if (!ext4_sb_feature_incom(&bg_ref->fs->sb, EXT4_FINCOM_META_BG) ||
317                         bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
318                         dsc_per_block) {
319                 if (bit_max) {
320                         bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
321                                                    bg_ref->index);
322                         bit_max +=
323                                 ext4_get16(&bg_ref->fs->sb,
324                                            s_reserved_gdt_blocks);
325                 }
326         } else { /* For META_BG_BLOCK_GROUPS */
327                 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
328                                            bg_ref->index);
329         }
330         for (bit = 0; bit < bit_max; bit++)
331                 ext4_bmap_bit_set(block_bitmap.data, bit);
332
333         if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
334                 /*
335                  * Even though mke2fs always initialize first and last group
336                  * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
337                  * to make sure we calculate the right free blocks
338                  */
339                 group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
340                                 ext4_get32(&bg_ref->fs->sb, first_data_block) -
341                                 (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
342                                  (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
343         } else {
344                 group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
345         }
346         if (!flex_bg ||
347             ext4_block_in_group(&bg_ref->fs->sb,
348                                 bitmap_block_addr, bg_ref->index))
349                 ext4_bmap_bit_set(block_bitmap.data,
350                                   bitmap_block_addr - first_group_addr);
351
352         if (!flex_bg ||
353             ext4_block_in_group(&bg_ref->fs->sb,
354                                 bitmap_inode_addr, bg_ref->index))
355                 ext4_bmap_bit_set(block_bitmap.data,
356                                   bitmap_inode_addr - first_group_addr);
357
358         for (i = inode_table_addr;
359                 i < inode_table_addr + inode_table_bcnt; i++) {
360                 if (!flex_bg ||
361                     ext4_block_in_group(&bg_ref->fs->sb,
362                                         i,
363                                         bg_ref->index))
364                         ext4_bmap_bit_set(block_bitmap.data,
365                                         i - first_group_addr);
366         }
367         /*
368          * Also if the number of blocks within the group is
369          * less than the blocksize * 8 ( which is the size
370          * of bitmap ), set rest of the block bitmap to 1
371          */
372         ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
373         block_bitmap.dirty = true;
374
375         ext4_balloc_set_bitmap_csum(&bg_ref->fs->sb,
376                                     bg_ref->block_group,
377                                     block_bitmap.data);
378         bg_ref->dirty = true;
379
380         /* Save bitmap */
381         return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
382 }
383
384 /**@brief Initialize i-node bitmap in block group.
385  * @param bg_ref Reference to block group
386  * @return Error code
387  */
388 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
389 {
390         /* Load bitmap */
391         ext4_fsblk_t bitmap_block_addr =
392             ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
393
394         struct ext4_block block_bitmap;
395         int rc =
396             ext4_block_get_noread(bg_ref->fs->bdev,
397                                   &block_bitmap, bitmap_block_addr);
398         if (rc != EOK)
399                 return rc;
400
401         /* Initialize all bitmap bits to zero */
402         uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
403         uint32_t inodes_per_group =
404             ext4_get32(&bg_ref->fs->sb, inodes_per_group);
405
406         memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
407
408         uint32_t start_bit = inodes_per_group;
409         uint32_t end_bit = block_size * 8;
410
411         uint32_t i;
412         for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
413                 ext4_bmap_bit_set(block_bitmap.data, i);
414
415         if (i < end_bit)
416                 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
417
418         block_bitmap.dirty = true;
419
420         ext4_ialloc_set_bitmap_csum(&bg_ref->fs->sb,
421                                     bg_ref->block_group,
422                                     block_bitmap.data);
423         bg_ref->dirty = true;
424
425         /* Save bitmap */
426         return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
427 }
428
429 /**@brief Initialize i-node table in block group.
430  * @param bg_ref Reference to block group
431  * @return Error code
432  */
433 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
434 {
435         struct ext4_sblock *sb = &bg_ref->fs->sb;
436
437         uint32_t inode_size = ext4_get32(sb, inode_size);
438         uint32_t block_size = ext4_sb_get_block_size(sb);
439         uint32_t inodes_per_block = block_size / inode_size;
440         uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
441         uint32_t table_blocks = inodes_in_group / inodes_per_block;
442         ext4_fsblk_t fblock;
443
444         if (inodes_in_group % inodes_per_block)
445                 table_blocks++;
446
447         /* Compute initialization bounds */
448         ext4_fsblk_t first_block =
449             ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
450
451         ext4_fsblk_t last_block = first_block + table_blocks - 1;
452
453         /* Initialization of all itable blocks */
454         for (fblock = first_block; fblock <= last_block; ++fblock) {
455
456                 struct ext4_block block;
457                 int rc = ext4_block_get_noread(bg_ref->fs->bdev, &block, fblock);
458                 if (rc != EOK)
459                         return rc;
460
461                 memset(block.data, 0, block_size);
462                 block.dirty = true;
463
464                 ext4_block_set(bg_ref->fs->bdev, &block);
465                 if (rc != EOK)
466                         return rc;
467         }
468
469         return EOK;
470 }
471
472 static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
473                                              uint32_t bgid,
474                                              uint32_t dsc_per_block)
475 {
476         uint32_t first_meta_bg, dsc_id;
477
478         int has_super = 0;
479
480         dsc_id = bgid / dsc_per_block;
481         first_meta_bg = ext4_sb_first_meta_bg(s);
482
483         if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) ||
484             dsc_id < first_meta_bg)
485                 return ext4_get32(s, first_data_block) + dsc_id + 1;
486
487         if (ext4_sb_is_super_in_bg(s, bgid))
488                 has_super = 1;
489
490         return (has_super + ext4_fs_first_bg_block_no(s, bgid));
491 }
492
493 /**@brief  Compute checksum of block group descriptor.
494  * @param sb   Superblock
495  * @param bgid Index of block group in the filesystem
496  * @param bg   Block group to compute checksum for
497  * @return Checksum value
498  */
499 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
500                                     struct ext4_bgroup *bg)
501 {
502         /* If checksum not supported, 0 will be returned */
503         uint16_t crc = 0;
504 #if CONFIG_META_CSUM_ENABLE
505         /* Compute the checksum only if the filesystem supports it */
506         if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
507                 /* Use metadata_csum algorithm instead */
508                 uint32_t le32_bgid = to_le32(bgid);
509                 uint32_t orig_checksum, checksum;
510
511                 /* Preparation: temporarily set bg checksum to 0 */
512                 orig_checksum = bg->checksum;
513                 bg->checksum = 0;
514
515                 /* First calculate crc32 checksum against fs uuid */
516                 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
517                                 sizeof(sb->uuid));
518                 /* Then calculate crc32 checksum against bgid */
519                 checksum = ext4_crc32c(checksum, &le32_bgid,
520                                      sizeof(bgid));
521                 /* Finally calculate crc32 checksum against block_group_desc */
522                 checksum = ext4_crc32c(checksum, bg,
523                                      ext4_sb_get_desc_size(sb));
524                 bg->checksum = orig_checksum;
525
526                 crc = checksum & 0xFFFF;
527                 return crc;
528         }
529 #endif
530         if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
531                 uint8_t *base = (uint8_t *)bg;
532                 uint8_t *checksum = (uint8_t *)&bg->checksum;
533
534                 uint32_t offset = (uint32_t)(checksum - base);
535
536                 /* Convert block group index to little endian */
537                 uint32_t le_group = to_le32(bgid);
538
539                 /* Initialization */
540                 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
541
542                 /* Include index of block group */
543                 crc =
544                     ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
545
546                 /* Compute crc from the first part (stop before checksum field)
547                  */
548                 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
549
550                 /* Skip checksum */
551                 offset += sizeof(bg->checksum);
552
553                 /* Checksum of the rest of block group descriptor */
554                 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
555                     (offset < ext4_sb_get_desc_size(sb)))
556
557                         crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
558                                             ext4_sb_get_desc_size(sb) - offset);
559         }
560         return crc;
561 }
562
563 #if CONFIG_META_CSUM_ENABLE
564 static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb,
565                                    uint32_t bgid,
566                                    struct ext4_bgroup *bg)
567 {
568         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
569                 return true;
570
571         return ext4_fs_bg_checksum(sb,
572                                    bgid,
573                                    bg) ==
574                 to_le16(bg->checksum);
575 }
576 #else
577 #define ext4_fs_verify_bg_csum(...) true
578 #endif
579
580 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
581                                 struct ext4_block_group_ref *ref)
582 {
583         /* Compute number of descriptors, that fits in one data block */
584         uint32_t dsc_per_block =
585             ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
586
587         /* Block group descriptor table starts at the next block after
588          * superblock */
589         uint64_t block_id =
590             ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
591
592         uint32_t offset =
593             (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
594
595         int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
596         if (rc != EOK)
597                 return rc;
598
599         ref->block_group = (void *)(ref->block.data + offset);
600         ref->fs = fs;
601         ref->index = bgid;
602         ref->dirty = false;
603
604         if (!ext4_fs_verify_bg_csum(&fs->sb,
605                                     bgid,
606                                     ref->block_group)) {
607                 ext4_dbg(DEBUG_FS,
608                          DBG_WARN "Block group descriptor checksum failed."
609                          "Block group index: %" PRIu32"\n",
610                          bgid);
611         }
612
613         if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
614                 rc = ext4_fs_init_block_bitmap(ref);
615                 if (rc != EOK) {
616                         ext4_block_set(fs->bdev, &ref->block);
617                         return rc;
618                 }
619                 ext4_bg_clear_flag(ref->block_group,
620                                    EXT4_BLOCK_GROUP_BLOCK_UNINIT);
621
622                 ref->dirty = true;
623         }
624
625         if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
626                 rc = ext4_fs_init_inode_bitmap(ref);
627                 if (rc != EOK) {
628                         ext4_block_set(ref->fs->bdev, &ref->block);
629                         return rc;
630                 }
631
632                 ext4_bg_clear_flag(ref->block_group,
633                                    EXT4_BLOCK_GROUP_INODE_UNINIT);
634
635                 if (!ext4_bg_has_flag(ref->block_group,
636                                       EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
637                         rc = ext4_fs_init_inode_table(ref);
638                         if (rc != EOK) {
639                                 ext4_block_set(fs->bdev, &ref->block);
640                                 return rc;
641                         }
642
643                         ext4_bg_set_flag(ref->block_group,
644                                          EXT4_BLOCK_GROUP_ITABLE_ZEROED);
645                 }
646
647                 ref->dirty = true;
648         }
649
650         return EOK;
651 }
652
653 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
654 {
655         /* Check if reference modified */
656         if (ref->dirty) {
657                 /* Compute new checksum of block group */
658                 uint16_t checksum = ext4_fs_bg_checksum(
659                     &ref->fs->sb, ref->index, ref->block_group);
660
661                 ref->block_group->checksum = to_le16(checksum);
662
663                 /* Mark block dirty for writing changes to physical device */
664                 ref->block.dirty = true;
665         }
666
667         /* Put back block, that contains block group descriptor */
668         return ext4_block_set(ref->fs->bdev, &ref->block);
669 }
670
671 #if CONFIG_META_CSUM_ENABLE
672 static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
673 {
674         uint32_t checksum = 0;
675         struct ext4_sblock *sb = &inode_ref->fs->sb;
676         uint16_t inode_size = ext4_get16(sb, inode_size);
677
678         if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
679                 uint32_t orig_checksum;
680
681                 uint32_t ino_index = to_le32(inode_ref->index);
682                 uint32_t ino_gen =
683                         to_le32(ext4_inode_get_generation(inode_ref->inode));
684
685                 /* Preparation: temporarily set bg checksum to 0 */
686                 orig_checksum = ext4_inode_get_checksum(sb, inode_ref->inode);
687                 ext4_inode_set_checksum(sb, inode_ref->inode, 0);
688
689                 /* First calculate crc32 checksum against fs uuid */
690                 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
691                                 sizeof(sb->uuid));
692                 /* Then calculate crc32 checksum against inode number
693                  * and inode generation */
694                 checksum = ext4_crc32c(checksum, &ino_index,
695                                      sizeof(ino_index));
696                 checksum = ext4_crc32c(checksum, &ino_gen,
697                                      sizeof(ino_gen));
698                 /* Finally calculate crc32 checksum against 
699                  * the entire inode */
700                 checksum = ext4_crc32c(checksum, inode_ref->inode,
701                                 inode_size);
702                 ext4_inode_set_checksum(sb, inode_ref->inode,
703                                 orig_checksum);
704
705                 /* If inode size is not large enough to hold the
706                  * upper 16bit of the checksum */
707                 if (inode_size == EXT4_GOOD_OLD_INODE_SIZE)
708                         checksum &= 0xFFFF;
709
710         }
711         return checksum;
712 }
713 #else
714 #define ext4_fs_inode_checksum(...) 0
715 #endif
716
717 static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
718 {
719         struct ext4_sblock *sb = &inode_ref->fs->sb;
720         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
721                 return;
722
723         ext4_inode_set_checksum(sb, inode_ref->inode,
724                                 ext4_fs_inode_checksum(inode_ref));
725 }
726
727 #if CONFIG_META_CSUM_ENABLE
728 static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref)
729 {
730         struct ext4_sblock *sb = &inode_ref->fs->sb;
731         if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
732                 return true;
733
734         return ext4_inode_get_checksum(sb, inode_ref->inode) ==
735                 ext4_fs_inode_checksum(inode_ref);
736 }
737 #else
738 #define ext4_fs_verify_inode_csum(...) true
739 #endif
740
741 static int
742 __ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
743                         struct ext4_inode_ref *ref,
744                         bool initialized)
745 {
746         /* Compute number of i-nodes, that fits in one data block */
747         uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
748
749         /*
750          * Inode numbers are 1-based, but it is simpler to work with 0-based
751          * when computing indices
752          */
753         index -= 1;
754         uint32_t block_group = index / inodes_per_group;
755         uint32_t offset_in_group = index % inodes_per_group;
756
757         /* Load block group, where i-node is located */
758         struct ext4_block_group_ref bg_ref;
759
760         int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
761         if (rc != EOK) {
762                 return rc;
763         }
764
765         /* Load block address, where i-node table is located */
766         uint32_t inode_table_start =
767             ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
768
769         /* Put back block group reference (not needed more) */
770         rc = ext4_fs_put_block_group_ref(&bg_ref);
771         if (rc != EOK) {
772                 return rc;
773         }
774
775         /* Compute position of i-node in the block group */
776         uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
777         uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
778         uint32_t byte_offset_in_group = offset_in_group * inode_size;
779
780         /* Compute block address */
781         ext4_fsblk_t block_id =
782             inode_table_start + (byte_offset_in_group / block_size);
783
784         rc = ext4_block_get(fs->bdev, &ref->block, block_id);
785         if (rc != EOK) {
786                 return rc;
787         }
788
789         /* Compute position of i-node in the data block */
790         uint32_t offset_in_block = byte_offset_in_group % block_size;
791         ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
792
793         /* We need to store the original value of index in the reference */
794         ref->index = index + 1;
795         ref->fs = fs;
796         ref->dirty = false;
797
798         if (initialized && !ext4_fs_verify_inode_csum(ref)) {
799                 ext4_dbg(DEBUG_FS,
800                         DBG_WARN "Inode checksum failed."
801                         "Inode: %" PRIu32"\n",
802                         ref->index);
803         }
804
805         return EOK;
806 }
807
808 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
809                           struct ext4_inode_ref *ref)
810 {
811         return __ext4_fs_get_inode_ref(fs, index, ref, true);
812 }
813
814 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
815 {
816         /* Check if reference modified */
817         if (ref->dirty) {
818                 /* Mark block dirty for writing changes to physical device */
819                 ext4_fs_set_inode_checksum(ref);
820                 ref->block.dirty = true;
821         }
822
823         /* Put back block, that contains i-node */
824         return ext4_block_set(ref->fs->bdev, &ref->block);
825 }
826
827 void ext4_fs_inode_blocks_init(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref)
828 {
829         int i;
830         struct ext4_inode *inode = inode_ref->inode;
831
832         for (i = 0; i < EXT4_INODE_BLOCKS; i++)
833                 inode->blocks[i] = 0;
834
835         (void)fs;
836 #if CONFIG_EXTENT_ENABLE
837         /* Initialize extents if needed */
838         if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
839                 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
840
841                 /* Initialize extent root header */
842                 ext4_extent_tree_init(inode_ref);
843         }
844 #endif
845 }
846
847 uint32_t ext4_fs_correspond_inode_mode(int filetype)
848 {
849         switch (filetype) {
850         case EXT4_DIRENTRY_DIR:
851                 return EXT4_INODE_MODE_DIRECTORY;
852         case EXT4_DIRENTRY_REG_FILE:
853                 return EXT4_INODE_MODE_FILE;
854         case EXT4_DIRENTRY_SYMLINK:
855                 return EXT4_INODE_MODE_SOFTLINK;
856         default:
857                 /* FIXME: right now we only support 3 file type. */
858                 ext4_assert(0);
859         }
860         return 0;
861 }
862
863 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
864                         int filetype)
865 {
866         /* Check if newly allocated i-node will be a directory */
867         bool is_dir;
868         uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
869
870         is_dir = (filetype == EXT4_DIRENTRY_DIR);
871
872         /* Allocate inode by allocation algorithm */
873         uint32_t index;
874         int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
875         if (rc != EOK)
876                 return rc;
877
878         /* Load i-node from on-disk i-node table */
879         rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false);
880         if (rc != EOK) {
881                 ext4_ialloc_free_inode(fs, index, is_dir);
882                 return rc;
883         }
884
885         /* Initialize i-node */
886         struct ext4_inode *inode = inode_ref->inode;
887
888         uint32_t mode;
889         if (is_dir) {
890                 /*
891                  * Default directory permissions to be compatible with other
892                  * systems
893                  * 0777 (octal) == rwxrwxrwx
894                  */
895
896                 mode = 0777;
897                 mode |= EXT4_INODE_MODE_DIRECTORY;
898         } else {
899                 /*
900                  * Default file permissions to be compatible with other systems
901                  * 0666 (octal) == rw-rw-rw-
902                  */
903
904                 mode = 0666;
905                 mode |= ext4_fs_correspond_inode_mode(filetype);
906         }
907         ext4_inode_set_mode(&fs->sb, inode, mode);
908
909         ext4_inode_set_links_count(inode, 0);
910         ext4_inode_set_uid(inode, 0);
911         ext4_inode_set_gid(inode, 0);
912         ext4_inode_set_size(inode, 0);
913         ext4_inode_set_access_time(inode, 0);
914         ext4_inode_set_change_inode_time(inode, 0);
915         ext4_inode_set_modification_time(inode, 0);
916         ext4_inode_set_deletion_time(inode, 0);
917         ext4_inode_set_blocks_count(&fs->sb, inode, 0);
918         ext4_inode_set_flags(inode, 0);
919         ext4_inode_set_generation(inode, 0);
920         if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
921                 ext4_inode_set_extra_isize(inode,
922                                 sizeof(struct ext4_inode) -
923                                 offsetof(struct ext4_inode,
924                                         extra_isize));
925
926         /* Reset blocks array. For symbolic link inode, just
927          * fill in blocks with 0 */
928         if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
929                 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
930                         inode->blocks[i] = 0;
931
932         } else
933                 ext4_fs_inode_blocks_init(fs, inode_ref);
934
935         inode_ref->dirty = true;
936
937         return EOK;
938 }
939
940 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
941 {
942         struct ext4_fs *fs = inode_ref->fs;
943         uint32_t offset;
944         uint32_t suboff;
945         int rc;
946 #if CONFIG_EXTENT_ENABLE
947         /* For extents must be data block destroyed by other way */
948         if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
949             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
950                 /* Data structures are released during truncate operation... */
951                 goto finish;
952         }
953 #endif
954         /* Release all indirect (no data) blocks */
955
956         /* 1) Single indirect */
957         ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
958         if (fblock != 0) {
959                 int rc = ext4_balloc_free_block(inode_ref, fblock);
960                 if (rc != EOK)
961                         return rc;
962
963                 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
964         }
965
966         uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
967         uint32_t count = block_size / sizeof(uint32_t);
968
969         struct ext4_block block;
970
971         /* 2) Double indirect */
972         fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
973         if (fblock != 0) {
974                 int rc = ext4_block_get(fs->bdev, &block, fblock);
975                 if (rc != EOK)
976                         return rc;
977
978                 ext4_fsblk_t ind_block;
979                 for (offset = 0; offset < count; ++offset) {
980                         ind_block = to_le32(((uint32_t *)block.data)[offset]);
981
982                         if (ind_block == 0)
983                                 continue;
984                         rc = ext4_balloc_free_block(inode_ref, ind_block);
985                         if (rc != EOK) {
986                                 ext4_block_set(fs->bdev, &block);
987                                 return rc;
988                         }
989
990                 }
991
992                 ext4_block_set(fs->bdev, &block);
993                 rc = ext4_balloc_free_block(inode_ref, fblock);
994                 if (rc != EOK)
995                         return rc;
996
997                 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
998         }
999
1000         /* 3) Tripple indirect */
1001         struct ext4_block subblock;
1002         fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
1003         if (fblock == 0)
1004                 goto finish;
1005         rc = ext4_block_get(fs->bdev, &block, fblock);
1006         if (rc != EOK)
1007                 return rc;
1008
1009         ext4_fsblk_t ind_block;
1010         for (offset = 0; offset < count; ++offset) {
1011                 ind_block = to_le32(((uint32_t *)block.data)[offset]);
1012
1013                 if (ind_block == 0)
1014                         continue;
1015                 rc = ext4_block_get(fs->bdev, &subblock,
1016                                 ind_block);
1017                 if (rc != EOK) {
1018                         ext4_block_set(fs->bdev, &block);
1019                         return rc;
1020                 }
1021
1022                 ext4_fsblk_t ind_subblk;
1023                 for (suboff = 0; suboff < count; ++suboff) {
1024                         ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
1025
1026                         if (ind_subblk == 0)
1027                                 continue;
1028                         rc = ext4_balloc_free_block(inode_ref, ind_subblk);
1029                         if (rc != EOK) {
1030                                 ext4_block_set(fs->bdev, &subblock);
1031                                 ext4_block_set(fs->bdev, &block);
1032                                 return rc;
1033                         }
1034
1035                 }
1036
1037                 ext4_block_set(fs->bdev, &subblock);
1038
1039                 rc = ext4_balloc_free_block(inode_ref,
1040                                 ind_block);
1041                 if (rc != EOK) {
1042                         ext4_block_set(fs->bdev, &block);
1043                         return rc;
1044                 }
1045
1046         }
1047
1048         ext4_block_set(fs->bdev, &block);
1049         rc = ext4_balloc_free_block(inode_ref, fblock);
1050         if (rc != EOK)
1051                 return rc;
1052
1053         ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1054 finish:
1055         /* Mark inode dirty for writing to the physical device */
1056         inode_ref->dirty = true;
1057
1058         /* Free block with extended attributes if present */
1059         ext4_fsblk_t xattr_block =
1060             ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
1061         if (xattr_block) {
1062                 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
1063                 if (rc != EOK)
1064                         return rc;
1065
1066                 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
1067         }
1068
1069         /* Free inode by allocator */
1070         if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
1071                                EXT4_INODE_MODE_DIRECTORY))
1072                 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1073         else
1074                 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1075
1076         return rc;
1077 }
1078
1079
1080 /**@brief Release data block from i-node
1081  * @param inode_ref I-node to release block from
1082  * @param iblock    Logical block to be released
1083  * @return Error code
1084  */
1085 static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1086                                 uint32_t iblock)
1087 {
1088         ext4_fsblk_t fblock;
1089
1090         struct ext4_fs *fs = inode_ref->fs;
1091
1092         /* Extents are handled otherwise = there is not support in this function
1093          */
1094         ext4_assert(!(
1095             ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
1096             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1097
1098         struct ext4_inode *inode = inode_ref->inode;
1099
1100         /* Handle simple case when we are dealing with direct reference */
1101         if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1102                 fblock = ext4_inode_get_direct_block(inode, iblock);
1103
1104                 /* Sparse file */
1105                 if (fblock == 0)
1106                         return EOK;
1107
1108                 ext4_inode_set_direct_block(inode, iblock, 0);
1109                 return ext4_balloc_free_block(inode_ref, fblock);
1110         }
1111
1112         /* Determine the indirection level needed to get the desired block */
1113         unsigned int level = 0;
1114         unsigned int i;
1115         for (i = 1; i < 4; i++) {
1116                 if (iblock < fs->inode_block_limits[i]) {
1117                         level = i;
1118                         break;
1119                 }
1120         }
1121
1122         if (level == 0)
1123                 return EIO;
1124
1125         /* Compute offsets for the topmost level */
1126         uint64_t block_offset_in_level =
1127             iblock - fs->inode_block_limits[level - 1];
1128         ext4_fsblk_t current_block =
1129             ext4_inode_get_indirect_block(inode, level - 1);
1130         uint32_t offset_in_block =
1131             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1132
1133         /*
1134          * Navigate through other levels, until we find the block number
1135          * or find null reference meaning we are dealing with sparse file
1136          */
1137         struct ext4_block block;
1138
1139         while (level > 0) {
1140
1141                 /* Sparse check */
1142                 if (current_block == 0)
1143                         return EOK;
1144
1145                 int rc = ext4_block_get(fs->bdev, &block, current_block);
1146                 if (rc != EOK)
1147                         return rc;
1148
1149                 current_block =
1150                     to_le32(((uint32_t *)block.data)[offset_in_block]);
1151
1152                 /* Set zero if physical data block address found */
1153                 if (level == 1) {
1154                         ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1155                         block.dirty = true;
1156                 }
1157
1158                 rc = ext4_block_set(fs->bdev, &block);
1159                 if (rc != EOK)
1160                         return rc;
1161
1162                 level--;
1163
1164                 /*
1165                  * If we are on the last level, break here as
1166                  * there is no next level to visit
1167                  */
1168                 if (level == 0)
1169                         break;
1170
1171                 /* Visit the next level */
1172                 block_offset_in_level %= fs->inode_blocks_per_level[level];
1173                 offset_in_block = block_offset_in_level /
1174                                   fs->inode_blocks_per_level[level - 1];
1175         }
1176
1177         fblock = current_block;
1178         if (fblock == 0)
1179                 return EOK;
1180
1181         /* Physical block is not referenced, it can be released */
1182         return ext4_balloc_free_block(inode_ref, fblock);
1183 }
1184
1185 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
1186 {
1187         struct ext4_sblock *sb = &inode_ref->fs->sb;
1188         uint32_t i;
1189
1190         /* Check flags, if i-node can be truncated */
1191         if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1192                 return EINVAL;
1193
1194         /* If sizes are equal, nothing has to be done. */
1195         uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1196         if (old_size == new_size)
1197                 return EOK;
1198
1199         /* It's not supported to make the larger file by truncate operation */
1200         if (old_size < new_size)
1201                 return EINVAL;
1202
1203         if (ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK)
1204                         && old_size < sizeof(inode_ref->inode->blocks)
1205                         && !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
1206                 char *content = (char *)inode_ref->inode->blocks;
1207                 memset(content + new_size, 0,
1208                         sizeof(inode_ref->inode->blocks) - new_size);
1209                 ext4_inode_set_size(inode_ref->inode, new_size);
1210                 inode_ref->dirty = true;
1211
1212                 return EOK;
1213         }
1214
1215         /* Compute how many blocks will be released */
1216         uint32_t block_size = ext4_sb_get_block_size(sb);
1217         uint32_t new_blocks_count = (new_size + block_size - 1) /
1218                                     block_size;
1219         uint32_t old_blocks_count = (old_size + block_size - 1) /
1220                                     block_size;
1221         uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
1222 #if CONFIG_EXTENT_ENABLE
1223         if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
1224             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1225
1226                 /* Extents require special operation */
1227                 if (diff_blocks_count) {
1228                         int rc = ext4_extent_remove_space(inode_ref,
1229                                         new_blocks_count, EXT_MAX_BLOCKS);
1230                         if (rc != EOK)
1231                                 return rc;
1232
1233                 }
1234         } else
1235 #endif
1236         {
1237                 /* Release data blocks from the end of file */
1238
1239                 /* Starting from 1 because of logical blocks are numbered from 0
1240                  */
1241                 for (i = 0; i < diff_blocks_count; ++i) {
1242                         int rc = ext4_fs_release_inode_block(
1243                             inode_ref, new_blocks_count + i);
1244                         if (rc != EOK)
1245                                 return rc;
1246                 }
1247         }
1248
1249         /* Update i-node */
1250         ext4_inode_set_size(inode_ref->inode, new_size);
1251         inode_ref->dirty = true;
1252
1253         return EOK;
1254 }
1255
1256 /**@brief Compute 'goal' for inode index
1257  * @param inode_ref Reference to inode, to allocate block for
1258  * @return goal
1259  */
1260 ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
1261 {
1262         uint32_t group_inodes =
1263                 ext4_get32(&inode_ref->fs->sb, inodes_per_group);
1264         return (inode_ref->index - 1) / group_inodes;
1265 }
1266
1267 /**@brief Compute 'goal' for allocation algorithm (For blockmap).
1268  * @param inode_ref Reference to inode, to allocate block for
1269  * @param goal
1270  * @return error code
1271  */
1272 int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
1273                                 ext4_fsblk_t *goal)
1274 {
1275         struct ext4_sblock *sb = &inode_ref->fs->sb;
1276         *goal = 0;
1277
1278         uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1279         uint32_t block_size = ext4_sb_get_block_size(sb);
1280         uint32_t inode_block_count = inode_size / block_size;
1281
1282         if (inode_size % block_size != 0)
1283                 inode_block_count++;
1284
1285         /* If inode has some blocks, get last block address + 1 */
1286         if (inode_block_count > 0) {
1287                 int rc = ext4_fs_get_inode_data_block_index(
1288                     inode_ref, inode_block_count - 1, goal, false);
1289                 if (rc != EOK)
1290                         return rc;
1291
1292                 if (*goal != 0) {
1293                         (*goal)++;
1294                         return rc;
1295                 }
1296
1297                 /* If goal == 0, sparse file -> continue */
1298         }
1299
1300         /* Identify block group of inode */
1301
1302         uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
1303         uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
1304         block_size = ext4_sb_get_block_size(sb);
1305
1306         /* Load block group reference */
1307         struct ext4_block_group_ref bg_ref;
1308         int rc =
1309             ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
1310         if (rc != EOK)
1311                 return rc;
1312
1313         /* Compute indexes */
1314         uint32_t block_group_count = ext4_block_group_cnt(sb);
1315         ext4_fsblk_t inode_table_first_block =
1316             ext4_bg_get_inode_table_first_block(bg_ref.block_group, sb);
1317         uint16_t inode_table_item_size = ext4_get16(sb, inode_size);
1318         uint32_t inode_table_bytes;
1319
1320         /* Check for last block group */
1321         if (block_group < block_group_count - 1) {
1322                 inode_table_bytes = inodes_per_group * inode_table_item_size;
1323         } else {
1324                 /* Last block group could be smaller */
1325                 uint32_t inodes_count_total = ext4_get32(sb, inodes_count);
1326
1327                 inode_table_bytes =
1328                     (inodes_count_total -
1329                      ((block_group_count - 1) * inodes_per_group)) *
1330                     inode_table_item_size;
1331         }
1332
1333         ext4_fsblk_t inode_table_blocks = inode_table_bytes / block_size;
1334
1335         if (inode_table_bytes % block_size)
1336                 inode_table_blocks++;
1337
1338         *goal = inode_table_first_block + inode_table_blocks;
1339
1340         return ext4_fs_put_block_group_ref(&bg_ref);
1341 }
1342
1343 static int ext4_fs_get_inode_data_block_idx(struct ext4_inode_ref *inode_ref,
1344                                        uint64_t iblock, ext4_fsblk_t *fblock,
1345                                        bool extent_create,
1346                                        bool support_unwritten __unused)
1347 {
1348         struct ext4_fs *fs = inode_ref->fs;
1349
1350         /* For empty file is situation simple */
1351         if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1352                 *fblock = 0;
1353                 return EOK;
1354         }
1355
1356         ext4_fsblk_t current_block;
1357
1358         (void)extent_create;
1359 #if CONFIG_EXTENT_ENABLE
1360         /* Handle i-node using extents */
1361         if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1362             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1363
1364                 ext4_fsblk_t current_fsblk;
1365                 int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
1366                                 &current_fsblk, extent_create, NULL);
1367                 if (rc != EOK)
1368                         return rc;
1369
1370                 current_block = current_fsblk;
1371                 *fblock = current_block;
1372
1373                 ext4_assert(*fblock || support_unwritten);
1374                 return EOK;
1375         }
1376 #endif
1377
1378         struct ext4_inode *inode = inode_ref->inode;
1379
1380         /* Direct block are read directly from array in i-node structure */
1381         if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1382                 current_block =
1383                     ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1384                 *fblock = current_block;
1385                 return EOK;
1386         }
1387
1388         /* Determine indirection level of the target block */
1389         unsigned int level = 0;
1390         unsigned int i;
1391         for (i = 1; i < 4; i++) {
1392                 if (iblock < fs->inode_block_limits[i]) {
1393                         level = i;
1394                         break;
1395                 }
1396         }
1397
1398         if (level == 0)
1399                 return EIO;
1400
1401         /* Compute offsets for the topmost level */
1402         uint64_t block_offset_in_level =
1403             iblock - fs->inode_block_limits[level - 1];
1404         current_block = ext4_inode_get_indirect_block(inode, level - 1);
1405         uint32_t offset_in_block =
1406             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1407
1408         /* Sparse file */
1409         if (current_block == 0) {
1410                 *fblock = 0;
1411                 return EOK;
1412         }
1413
1414         struct ext4_block block;
1415
1416         /*
1417          * Navigate through other levels, until we find the block number
1418          * or find null reference meaning we are dealing with sparse file
1419          */
1420         while (level > 0) {
1421                 /* Load indirect block */
1422                 int rc = ext4_block_get(fs->bdev, &block, current_block);
1423                 if (rc != EOK)
1424                         return rc;
1425
1426                 /* Read block address from indirect block */
1427                 current_block =
1428                     to_le32(((uint32_t *)block.data)[offset_in_block]);
1429
1430                 /* Put back indirect block untouched */
1431                 rc = ext4_block_set(fs->bdev, &block);
1432                 if (rc != EOK)
1433                         return rc;
1434
1435                 /* Check for sparse file */
1436                 if (current_block == 0) {
1437                         *fblock = 0;
1438                         return EOK;
1439                 }
1440
1441                 /* Jump to the next level */
1442                 level--;
1443
1444                 /* Termination condition - we have address of data block loaded
1445                  */
1446                 if (level == 0)
1447                         break;
1448
1449                 /* Visit the next level */
1450                 block_offset_in_level %= fs->inode_blocks_per_level[level];
1451                 offset_in_block = block_offset_in_level /
1452                                   fs->inode_blocks_per_level[level - 1];
1453         }
1454
1455         *fblock = current_block;
1456
1457         return EOK;
1458 }
1459
1460
1461 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1462                                        uint64_t iblock, ext4_fsblk_t *fblock,
1463                                        bool support_unwritten)
1464 {
1465         return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1466                         false, support_unwritten);
1467 }
1468
1469 int ext4_fs_init_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1470                                        uint64_t iblock, ext4_fsblk_t *fblock)
1471 {
1472         return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1473                         true, true);
1474 }
1475
1476 static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1477                                        uint64_t iblock, ext4_fsblk_t fblock)
1478 {
1479         struct ext4_fs *fs = inode_ref->fs;
1480
1481 #if CONFIG_EXTENT_ENABLE
1482         /* Handle inode using extents */
1483         if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1484             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1485                 /* Not reachable */
1486                 return ENOTSUP;
1487         }
1488 #endif
1489
1490         /* Handle simple case when we are dealing with direct reference */
1491         if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1492                 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1493                                             (uint32_t)fblock);
1494                 inode_ref->dirty = true;
1495
1496                 return EOK;
1497         }
1498
1499         /* Determine the indirection level needed to get the desired block */
1500         unsigned int level = 0;
1501         unsigned int i;
1502         for (i = 1; i < 4; i++) {
1503                 if (iblock < fs->inode_block_limits[i]) {
1504                         level = i;
1505                         break;
1506                 }
1507         }
1508
1509         if (level == 0)
1510                 return EIO;
1511
1512         uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1513
1514         /* Compute offsets for the topmost level */
1515         uint64_t block_offset_in_level =
1516             iblock - fs->inode_block_limits[level - 1];
1517         ext4_fsblk_t current_block =
1518             ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1519         uint32_t offset_in_block =
1520             block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1521
1522         ext4_fsblk_t new_block_addr;
1523
1524         struct ext4_block block;
1525         struct ext4_block new_block;
1526
1527         /* Is needed to allocate indirect block on the i-node level */
1528         if (current_block == 0) {
1529                 /* Allocate new indirect block */
1530                 ext4_fsblk_t goal;
1531                 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1532                 if (rc != EOK)
1533                         return rc;
1534
1535                 rc = ext4_balloc_alloc_block(inode_ref,
1536                                              goal,
1537                                              &new_block_addr);
1538                 if (rc != EOK)
1539                         return rc;
1540
1541                 /* Update i-node */
1542                 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1543                                               (uint32_t)new_block_addr);
1544                 inode_ref->dirty = true;
1545
1546                 /* Load newly allocated block */
1547                 rc = ext4_block_get_noread(fs->bdev, &new_block, new_block_addr);
1548                 if (rc != EOK) {
1549                         ext4_balloc_free_block(inode_ref, new_block_addr);
1550                         return rc;
1551                 }
1552
1553                 /* Initialize new block */
1554                 memset(new_block.data, 0, block_size);
1555                 new_block.dirty = true;
1556
1557                 /* Put back the allocated block */
1558                 rc = ext4_block_set(fs->bdev, &new_block);
1559                 if (rc != EOK)
1560                         return rc;
1561
1562                 current_block = new_block_addr;
1563         }
1564
1565         /*
1566          * Navigate through other levels, until we find the block number
1567          * or find null reference meaning we are dealing with sparse file
1568          */
1569         while (level > 0) {
1570                 int rc = ext4_block_get(fs->bdev, &block, current_block);
1571                 if (rc != EOK)
1572                         return rc;
1573
1574                 current_block =
1575                     to_le32(((uint32_t *)block.data)[offset_in_block]);
1576
1577                 if ((level > 1) && (current_block == 0)) {
1578                         ext4_fsblk_t goal;
1579                         rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1580                         if (rc != EOK) {
1581                                 ext4_block_set(fs->bdev, &block);
1582                                 return rc;
1583                         }
1584
1585                         /* Allocate new block */
1586                         rc =
1587                             ext4_balloc_alloc_block(inode_ref, goal, &new_block_addr);
1588                         if (rc != EOK) {
1589                                 ext4_block_set(fs->bdev, &block);
1590                                 return rc;
1591                         }
1592
1593                         /* Load newly allocated block */
1594                         rc = ext4_block_get_noread(fs->bdev, &new_block,
1595                                             new_block_addr);
1596
1597                         if (rc != EOK) {
1598                                 ext4_block_set(fs->bdev, &block);
1599                                 return rc;
1600                         }
1601
1602                         /* Initialize allocated block */
1603                         memset(new_block.data, 0, block_size);
1604                         new_block.dirty = true;
1605
1606                         rc = ext4_block_set(fs->bdev, &new_block);
1607                         if (rc != EOK) {
1608                                 ext4_block_set(fs->bdev, &block);
1609                                 return rc;
1610                         }
1611
1612                         /* Write block address to the parent */
1613                         ((uint32_t *)block.data)[offset_in_block] =
1614                             to_le32((uint32_t)new_block_addr);
1615                         block.dirty = true;
1616                         current_block = new_block_addr;
1617                 }
1618
1619                 /* Will be finished, write the fblock address */
1620                 if (level == 1) {
1621                         ((uint32_t *)block.data)[offset_in_block] =
1622                             to_le32((uint32_t)fblock);
1623                         block.dirty = true;
1624                 }
1625
1626                 rc = ext4_block_set(fs->bdev, &block);
1627                 if (rc != EOK)
1628                         return rc;
1629
1630                 level--;
1631
1632                 /*
1633                  * If we are on the last level, break here as
1634                  * there is no next level to visit
1635                  */
1636                 if (level == 0)
1637                         break;
1638
1639                 /* Visit the next level */
1640                 block_offset_in_level %= fs->inode_blocks_per_level[level];
1641                 offset_in_block = block_offset_in_level /
1642                                   fs->inode_blocks_per_level[level - 1];
1643         }
1644
1645         return EOK;
1646 }
1647
1648
1649 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1650                                ext4_fsblk_t *fblock, uint32_t *iblock)
1651 {
1652 #if CONFIG_EXTENT_ENABLE
1653         /* Handle extents separately */
1654         if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
1655             (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1656                 int rc;
1657                 ext4_fsblk_t current_fsblk;
1658                 struct ext4_sblock *sb = &inode_ref->fs->sb;
1659                 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1660                 uint32_t block_size = ext4_sb_get_block_size(sb);
1661                 *iblock = (inode_size + block_size - 1) /
1662                                     block_size;
1663
1664                 rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
1665                                 &current_fsblk, true, NULL);
1666
1667
1668                 *fblock = current_fsblk;
1669                 ext4_assert(*fblock);
1670
1671                 ext4_inode_set_size(inode_ref->inode,
1672                                     inode_size + block_size);
1673                 inode_ref->dirty = true;
1674
1675
1676                 return rc;
1677         }
1678 #endif
1679         struct ext4_sblock *sb = &inode_ref->fs->sb;
1680
1681         /* Compute next block index and allocate data block */
1682         uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1683         uint32_t block_size = ext4_sb_get_block_size(sb);
1684
1685         /* Align size i-node size */
1686         if ((inode_size % block_size) != 0)
1687                 inode_size += block_size - (inode_size % block_size);
1688
1689         /* Logical blocks are numbered from 0 */
1690         uint32_t new_block_idx = inode_size / block_size;
1691
1692         /* Allocate new physical block */
1693         ext4_fsblk_t goal, phys_block;
1694         int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1695         if (rc != EOK)
1696                 return rc;
1697
1698         rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
1699         if (rc != EOK)
1700                 return rc;
1701
1702         /* Add physical block address to the i-node */
1703         rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1704                                                 phys_block);
1705         if (rc != EOK) {
1706                 ext4_balloc_free_block(inode_ref, phys_block);
1707                 return rc;
1708         }
1709
1710         /* Update i-node */
1711         ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1712         inode_ref->dirty = true;
1713
1714         *fblock = phys_block;
1715         *iblock = new_block_idx;
1716
1717         return EOK;
1718 }
1719
1720 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1721 {
1722         uint16_t link;
1723
1724         link = ext4_inode_get_links_count(inode_ref->inode);
1725         link++;
1726         ext4_inode_set_links_count(inode_ref->inode, link);
1727
1728         bool is_dx =
1729             ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
1730             ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1731
1732         if (is_dx && link > 1) {
1733                 if (link >= EXT4_LINK_MAX || link == 2) {
1734                         ext4_inode_set_links_count(inode_ref->inode, 1);
1735
1736                         uint32_t v =
1737                             ext4_get32(&inode_ref->fs->sb, features_read_only);
1738                         v |= EXT4_FRO_COM_DIR_NLINK;
1739                         ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1740                 }
1741         }
1742 }
1743
1744 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1745 {
1746         uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1747         if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1748                                 EXT4_INODE_MODE_DIRECTORY)) {
1749                 if (links > 0)
1750                         ext4_inode_set_links_count(inode_ref->inode, links - 1);
1751                 return;
1752         }
1753
1754         if (links > 2)
1755                 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1756 }
1757
1758 /**
1759  * @}
1760  */