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