2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 /** @addtogroup lwext4
38 * @brief More complex filesystem functions.
41 #include <ext4_config.h>
42 #include <ext4_types.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>
55 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
60 bool read_only = false;
62 ext4_assert(fs && bdev);
66 r = ext4_sb_read(fs->bdev, &fs->sb);
70 if(!ext4_sb_check(&fs->sb))
73 bsize = ext4_sb_get_block_size(&fs->sb);
74 if (bsize > EXT4_MAX_BLOCK_SIZE)
77 r = ext4_fs_check_features(fs, &read_only);
84 /* Compute limits for indirect block levels */
85 uint32_t blocks_id = bsize / sizeof(uint32_t);
87 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
88 fs->inode_blocks_per_level[0] = 1;
90 for (i = 1; i < 4; i++) {
91 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
93 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
94 fs->inode_blocks_per_level[i];
98 tmp = ext4_get16(&fs->sb, state);
99 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) {
100 ext4_dprintf(EXT4_DEBUG_FS, "Filesystem was not cleanly unmounted before \n");
103 /* Mark system as mounted */
104 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
105 r = ext4_sb_write(fs->bdev, &fs->sb);
110 /*Update mount count*/
111 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
117 int ext4_fs_fini(struct ext4_fs *fs)
121 /*Set superblock state*/
122 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
124 return ext4_sb_write(fs->bdev, &fs->sb);
127 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
129 ext4_assert(fs && read_only);
131 if(ext4_get32(&fs->sb, rev_level) == 0){
136 /*Check features_incompatible*/
137 if ((ext4_get32(&fs->sb, features_incompatible) & (~EXT4_FEATURE_INCOMPAT_SUPP)) )
141 /*Check features_read_only*/
142 if ((ext4_get32(&fs->sb, features_read_only) & (~EXT4_FEATURE_RO_COMPAT_SUPP))){
152 uint32_t ext4_fs_baddr2_index_in_group(struct ext4_sblock *s, uint32_t baddr)
155 if(ext4_get32(s, first_data_block))
158 return baddr % ext4_get32(s, blocks_per_group);
163 uint32_t ext4_fs_index_in_group2_baddr(struct ext4_sblock *s, uint32_t index, uint32_t bgid)
165 if(ext4_get32(s, first_data_block))
168 return ext4_get32(s, blocks_per_group) * bgid + index;
174 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
177 uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
178 bg_ref->block_group, &bg_ref->fs->sb);
180 struct ext4_block block_bitmap;
181 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
186 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
188 /* Determine first block and first data block in group */
189 uint32_t first_idx = 0;
191 uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
192 &bg_ref->fs->sb, bg_ref);
193 uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
194 &bg_ref->fs->sb, first_data);
196 /* Set bits from to first block to first data block - 1 to one (allocated) */
197 /*TODO: Optimize it*/
198 for (i = first_idx; i < first_data_idx; ++i)
199 ext4_bmap_bit_set(block_bitmap.data, i);
202 block_bitmap.dirty = true;
205 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
208 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
211 uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
212 bg_ref->block_group, &bg_ref->fs->sb);
214 struct ext4_block block_bitmap;
215 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
219 /* Initialize all bitmap bits to zero */
220 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
221 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
223 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
225 uint32_t start_bit = inodes_per_group;
226 uint32_t end_bit = block_size * 8;
229 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
230 ext4_bmap_bit_set(block_bitmap.data, i);
233 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
235 block_bitmap.dirty = true;
238 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
241 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
243 struct ext4_sblock *sb = &bg_ref->fs->sb;
245 uint32_t inode_size = ext4_get32(sb, inode_size);
246 uint32_t block_size = ext4_sb_get_block_size(sb);
247 uint32_t inodes_per_block = block_size / inode_size;
248 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
249 uint32_t table_blocks = inodes_in_group / inodes_per_block;
252 if (inodes_in_group % inodes_per_block)
255 /* Compute initialization bounds */
256 uint32_t first_block = ext4_bg_get_inode_table_first_block(
257 bg_ref->block_group, sb);
259 uint32_t last_block = first_block + table_blocks - 1;
261 /* Initialization of all itable blocks */
262 for (fblock = first_block; fblock <= last_block; ++fblock) {
264 struct ext4_block block;
265 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
269 memset(block.data, 0, block_size);
272 ext4_block_set(bg_ref->fs->bdev, &block);
281 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid, struct ext4_block_group_ref *ref)
283 /* Compute number of descriptors, that fits in one data block */
284 uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
285 ext4_sb_get_desc_size(&fs->sb);
287 /* Block group descriptor table starts at the next block after superblock */
288 uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1;
290 /* Find the block containing the descriptor we are looking for */
291 block_id += bgid / dsc_per_block;
292 uint32_t offset = (bgid % dsc_per_block) *
293 ext4_sb_get_desc_size(&fs->sb);
296 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
300 ref->block_group = (void *)(ref->block.data + offset);
305 if (ext4_bg_has_flag(ref->block_group,
306 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
307 rc = ext4_fs_init_block_bitmap(ref);
309 ext4_block_set(fs->bdev, &ref->block);
312 ext4_bg_clear_flag(ref->block_group,
313 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
318 if (ext4_bg_has_flag(ref->block_group,
319 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
320 rc = ext4_fs_init_inode_bitmap(ref);
322 ext4_block_set(ref->fs->bdev, &ref->block);
326 ext4_bg_clear_flag(ref->block_group,
327 EXT4_BLOCK_GROUP_INODE_UNINIT);
329 if (!ext4_bg_has_flag(ref->block_group,
330 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
331 rc = ext4_fs_init_inode_table(ref);
333 ext4_block_set(fs->bdev, &ref->block);
337 ext4_bg_set_flag(ref->block_group,
338 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
347 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid, struct ext4_bgroup *bg)
349 /* If checksum not supported, 0 will be returned */
352 /* Compute the checksum only if the filesystem supports it */
353 if (ext4_sb_check_read_only(sb,
354 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
356 void *checksum = &bg->checksum;
358 uint32_t offset = (uint32_t) (checksum - base);
360 /* Convert block group index to little endian */
361 uint32_t le_group = to_le32(bgid);
364 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
366 /* Include index of block group */
367 crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
369 /* Compute crc from the first part (stop before checksum field) */
370 crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
373 offset += sizeof(bg->checksum);
375 /* Checksum of the rest of block group descriptor */
376 if ((ext4_sb_check_feature_incompatible(sb,
377 EXT4_FEATURE_INCOMPAT_64BIT)) &&
378 (offset < ext4_sb_get_desc_size(sb)))
380 crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
381 ext4_sb_get_desc_size(sb) - offset);
386 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
388 /* Check if reference modified */
390 /* Compute new checksum of block group */
392 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
395 ref->block_group->checksum = to_le16(checksum);
397 /* Mark block dirty for writing changes to physical device */
398 ref->block.dirty = true;
401 /* Put back block, that contains block group descriptor */
402 return ext4_block_set(ref->fs->bdev, &ref->block);
405 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index, struct ext4_inode_ref *ref)
407 /* Compute number of i-nodes, that fits in one data block */
408 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
411 * Inode numbers are 1-based, but it is simpler to work with 0-based
412 * when computing indices
415 uint32_t block_group = index / inodes_per_group;
416 uint32_t offset_in_group = index % inodes_per_group;
418 /* Load block group, where i-node is located */
419 struct ext4_block_group_ref bg_ref;
421 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
426 /* Load block address, where i-node table is located */
427 uint32_t inode_table_start =
428 ext4_bg_get_inode_table_first_block(bg_ref.block_group,
431 /* Put back block group reference (not needed more) */
432 rc = ext4_fs_put_block_group_ref(&bg_ref);
437 /* Compute position of i-node in the block group */
438 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
439 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
440 uint32_t byte_offset_in_group = offset_in_group * inode_size;
442 /* Compute block address */
443 uint64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
446 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
451 /* Compute position of i-node in the data block */
452 uint32_t offset_in_block = byte_offset_in_group % block_size;
453 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
455 /* We need to store the original value of index in the reference */
456 ref->index = index + 1;
463 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
465 /* Check if reference modified */
467 /* Mark block dirty for writing changes to physical device */
468 ref->block.dirty = true;
471 /* Put back block, that contains i-node */
472 return ext4_block_set(ref->fs->bdev, &ref->block);
475 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref, bool is_directory)
477 /* Check if newly allocated i-node will be a directory */
481 is_dir = is_directory;
483 /* Allocate inode by allocation algorithm */
485 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
489 /* Load i-node from on-disk i-node table */
490 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
492 ext4_ialloc_free_inode(fs, index, is_dir);
496 /* Initialize i-node */
497 struct ext4_inode *inode = inode_ref->inode;
502 * Default directory permissions to be compatible with other systems
503 * 0777 (octal) == rwxrwxrwx
507 mode |= EXT4_INODE_MODE_DIRECTORY;
508 ext4_inode_set_mode(&fs->sb, inode, mode);
509 ext4_inode_set_links_count(inode, 1); /* '.' entry */
513 * Default file permissions to be compatible with other systems
514 * 0666 (octal) == rw-rw-rw-
518 mode |= EXT4_INODE_MODE_FILE;
519 ext4_inode_set_mode(&fs->sb, inode, mode);
520 ext4_inode_set_links_count(inode, 0);
523 ext4_inode_set_uid(inode, 0);
524 ext4_inode_set_gid(inode, 0);
525 ext4_inode_set_size(inode, 0);
526 ext4_inode_set_access_time(inode, 0);
527 ext4_inode_set_change_inode_time(inode, 0);
528 ext4_inode_set_modification_time(inode, 0);
529 ext4_inode_set_deletion_time(inode, 0);
530 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
531 ext4_inode_set_flags(inode, 0);
532 ext4_inode_set_generation(inode, 0);
534 /* Reset blocks array */
535 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
536 inode->blocks[i] = 0;
539 /* Initialize extents if needed */
540 if (ext4_sb_check_feature_incompatible(
541 &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
542 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
545 /* Initialize extent root header */
546 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
547 ext4_extent_header_set_depth(header, 0);
548 ext4_extent_header_set_entries_count(header, 0);
549 ext4_extent_header_set_generation(header, 0);
550 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
552 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
553 sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
555 ext4_extent_header_set_max_entries_count(header, max_entries);
559 inode_ref->dirty = true;
564 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
566 struct ext4_fs *fs = inode_ref->fs;
569 /* For extents must be data block destroyed by other way */
570 if ((ext4_sb_check_feature_incompatible(&fs->sb,
571 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
572 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
573 /* Data structures are released during truncate operation... */
577 /* Release all indirect (no data) blocks */
579 /* 1) Single indirect */
580 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
582 int rc = ext4_balloc_free_block(inode_ref, fblock);
586 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
589 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
590 uint32_t count = block_size / sizeof(uint32_t);
592 struct ext4_block block;
594 /* 2) Double indirect */
595 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
597 int rc = ext4_block_get(fs->bdev, &block, fblock);
602 for (offset = 0; offset < count; ++offset) {
603 ind_block = to_le32(((uint32_t *) block.data)[offset]);
605 if (ind_block != 0) {
606 rc = ext4_balloc_free_block(inode_ref, ind_block);
608 ext4_block_set(fs->bdev, &block);
614 ext4_block_set(fs->bdev, &block);
615 rc = ext4_balloc_free_block(inode_ref, fblock);
619 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
622 /* 3) Tripple indirect */
623 struct ext4_block subblock;
624 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
626 int rc = ext4_block_get(fs->bdev, &block, fblock);
631 for ( offset = 0; offset < count; ++offset) {
632 ind_block = to_le32(((uint32_t *) block.data)[offset]);
634 if (ind_block != 0) {
635 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
637 ext4_block_set(fs->bdev, &block);
641 uint32_t ind_subblock;
642 for (suboffset = 0; suboffset < count;
644 ind_subblock = to_le32(((uint32_t *)
645 subblock.data)[suboffset]);
647 if (ind_subblock != 0) {
648 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
650 ext4_block_set(fs->bdev, &subblock);
651 ext4_block_set(fs->bdev, &block);
657 ext4_block_set(fs->bdev, &subblock);
660 rc = ext4_balloc_free_block(inode_ref, ind_block);
662 ext4_block_set(fs->bdev, &block);
668 ext4_block_set(fs->bdev, &block);
669 rc = ext4_balloc_free_block(inode_ref, fblock);
673 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
677 /* Mark inode dirty for writing to the physical device */
678 inode_ref->dirty = true;
680 /* Free block with extended attributes if present */
681 uint32_t xattr_block = ext4_inode_get_file_acl(
682 inode_ref->inode, &fs->sb);
684 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
688 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
691 /* Free inode by allocator */
693 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
694 EXT4_INODE_MODE_DIRECTORY))
695 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
697 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
702 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
705 struct ext4_sblock *sb = &inode_ref->fs->sb;
708 /* Check flags, if i-node can be truncated */
709 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
712 /* If sizes are equal, nothing has to be done. */
713 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
714 if (old_size == new_size)
717 /* It's not suppported to make the larger file by truncate operation */
718 if (old_size < new_size)
721 /* Compute how many blocks will be released */
722 uint64_t size_diff = old_size - new_size;
723 uint32_t block_size = ext4_sb_get_block_size(sb);
724 uint32_t diff_blocks_count = size_diff / block_size;
725 if (size_diff % block_size != 0)
728 uint32_t old_blocks_count = old_size / block_size;
729 if (old_size % block_size != 0)
732 if ((ext4_sb_check_feature_incompatible(sb,
733 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
734 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
736 /* Extents require special operation */
737 int rc = ext4_extent_release_blocks_from(inode_ref,
738 old_blocks_count - diff_blocks_count);
743 /* Release data blocks from the end of file */
745 /* Starting from 1 because of logical blocks are numbered from 0 */
746 for (i = 1; i <= diff_blocks_count; ++i) {
747 int rc = ext4_fs_release_inode_block(inode_ref,
748 old_blocks_count - i);
755 ext4_inode_set_size(inode_ref->inode, new_size);
756 inode_ref->dirty = true;
761 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
762 uint64_t iblock, uint32_t *fblock)
764 struct ext4_fs *fs = inode_ref->fs;
766 /* For empty file is situation simple */
767 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
772 uint32_t current_block;
774 /* Handle i-node using extents */
775 if ((ext4_sb_check_feature_incompatible(&fs->sb,
776 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
777 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
780 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
784 *fblock = current_block;
789 struct ext4_inode *inode = inode_ref->inode;
791 /* Direct block are read directly from array in i-node structure */
792 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
793 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
794 *fblock = current_block;
798 /* Determine indirection level of the target block */
799 unsigned int level = 0;
801 for (i = 1; i < 4; i++) {
802 if (iblock < fs->inode_block_limits[i]) {
811 /* Compute offsets for the topmost level */
812 uint64_t block_offset_in_level =
813 iblock - fs->inode_block_limits[level - 1];
814 current_block = ext4_inode_get_indirect_block(inode, level - 1);
815 uint32_t offset_in_block =
816 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
819 if (current_block == 0) {
824 struct ext4_block block;
827 * Navigate through other levels, until we find the block number
828 * or find null reference meaning we are dealing with sparse file
831 /* Load indirect block */
832 int rc = ext4_block_get(fs->bdev, &block, current_block);
836 /* Read block address from indirect block */
838 to_le32(((uint32_t *) block.data)[offset_in_block]);
840 /* Put back indirect block untouched */
841 rc = ext4_block_set(fs->bdev, &block);
845 /* Check for sparse file */
846 if (current_block == 0) {
851 /* Jump to the next level */
854 /* Termination condition - we have address of data block loaded */
858 /* Visit the next level */
859 block_offset_in_level %= fs->inode_blocks_per_level[level];
861 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
864 *fblock = current_block;
869 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
870 uint64_t iblock, uint32_t fblock)
872 struct ext4_fs *fs = inode_ref->fs;
874 /* Handle inode using extents */
875 if ((ext4_sb_check_feature_incompatible(&fs->sb,
876 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
877 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
882 /* Handle simple case when we are dealing with direct reference */
883 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
884 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
885 inode_ref->dirty = true;
890 /* Determine the indirection level needed to get the desired block */
891 unsigned int level = 0;
893 for (i = 1; i < 4; i++) {
894 if (iblock < fs->inode_block_limits[i]) {
903 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
905 /* Compute offsets for the topmost level */
906 uint64_t block_offset_in_level =
907 iblock - fs->inode_block_limits[level - 1];
908 uint32_t current_block =
909 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
910 uint32_t offset_in_block =
911 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
913 uint32_t new_block_addr;
915 struct ext4_block block;
916 struct ext4_block new_block;
918 /* Is needed to allocate indirect block on the i-node level */
919 if (current_block == 0) {
920 /* Allocate new indirect block */
921 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
926 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
928 inode_ref->dirty = true;
930 /* Load newly allocated block */
931 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
933 ext4_balloc_free_block(inode_ref, new_block_addr);
937 /* Initialize new block */
938 memset(new_block.data, 0, block_size);
939 new_block.dirty = true;
941 /* Put back the allocated block */
942 rc = ext4_block_set(fs->bdev, &new_block);
946 current_block = new_block_addr;
950 * Navigate through other levels, until we find the block number
951 * or find null reference meaning we are dealing with sparse file
954 int rc = ext4_block_get(fs->bdev, &block, current_block);
959 to_le32(((uint32_t *) block.data)[offset_in_block]);
961 if ((level > 1) && (current_block == 0)) {
962 /* Allocate new block */
963 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
965 ext4_block_set(fs->bdev, &block);
969 /* Load newly allocated block */
970 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
973 ext4_block_set(fs->bdev, &block);
977 /* Initialize allocated block */
978 memset(new_block.data, 0, block_size);
979 new_block.dirty = true;
981 rc = ext4_block_set(fs->bdev, &new_block);
983 ext4_block_set(fs->bdev, &block);
987 /* Write block address to the parent */
988 ((uint32_t *) block.data)[offset_in_block] =
989 to_le32(new_block_addr);
991 current_block = new_block_addr;
994 /* Will be finished, write the fblock address */
996 ((uint32_t *) block.data)[offset_in_block] =
1001 rc = ext4_block_set(fs->bdev, &block);
1008 * If we are on the last level, break here as
1009 * there is no next level to visit
1014 /* Visit the next level */
1015 block_offset_in_level %= fs->inode_blocks_per_level[level];
1017 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1023 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1028 struct ext4_fs *fs = inode_ref->fs;
1030 /* Extents are handled otherwise = there is not support in this function */
1031 ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb,
1032 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1033 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1035 struct ext4_inode *inode = inode_ref->inode;
1037 /* Handle simple case when we are dealing with direct reference */
1038 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1039 fblock = ext4_inode_get_direct_block(inode, iblock);
1045 ext4_inode_set_direct_block(inode, iblock, 0);
1046 return ext4_balloc_free_block(inode_ref, fblock);
1049 /* Determine the indirection level needed to get the desired block */
1050 unsigned int level = 0;
1052 for (i = 1; i < 4; i++) {
1053 if (iblock < fs->inode_block_limits[i]) {
1062 /* Compute offsets for the topmost level */
1063 uint64_t block_offset_in_level =
1064 iblock - fs->inode_block_limits[level - 1];
1065 uint32_t current_block =
1066 ext4_inode_get_indirect_block(inode, level - 1);
1067 uint32_t offset_in_block =
1068 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1071 * Navigate through other levels, until we find the block number
1072 * or find null reference meaning we are dealing with sparse file
1074 struct ext4_block block;
1079 if (current_block == 0)
1082 int rc = ext4_block_get(fs->bdev, &block, current_block);
1087 to_le32(((uint32_t *) block.data)[offset_in_block]);
1089 /* Set zero if physical data block address found */
1091 ((uint32_t *) block.data)[offset_in_block] =
1096 rc = ext4_block_set(fs->bdev, &block);
1103 * If we are on the last level, break here as
1104 * there is no next level to visit
1109 /* Visit the next level */
1110 block_offset_in_level %= fs->inode_blocks_per_level[level];
1112 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1115 fblock = current_block;
1119 /* Physical block is not referenced, it can be released */
1120 return ext4_balloc_free_block(inode_ref, fblock);
1124 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1125 uint32_t *fblock, uint32_t *iblock)
1127 /* Handle extents separately */
1128 if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb,
1129 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1130 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1133 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1137 struct ext4_sblock *sb = &inode_ref->fs->sb;
1139 /* Compute next block index and allocate data block */
1140 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1141 uint32_t block_size = ext4_sb_get_block_size(sb);
1143 /* Align size i-node size */
1144 if ((inode_size % block_size) != 0)
1145 inode_size += block_size - (inode_size % block_size);
1147 /* Logical blocks are numbered from 0 */
1148 uint32_t new_block_idx = inode_size / block_size;
1150 /* Allocate new physical block */
1151 uint32_t phys_block;
1152 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1156 /* Add physical block address to the i-node */
1157 rc = ext4_fs_set_inode_data_block_index(inode_ref,
1158 new_block_idx, phys_block);
1160 ext4_balloc_free_block(inode_ref, phys_block);
1165 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1166 inode_ref->dirty = true;
1168 *fblock = phys_block;
1169 *iblock = new_block_idx;