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