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>
53 #include <ext4_extent.h>
56 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
61 bool read_only = false;
63 ext4_assert(fs && bdev);
67 r = ext4_sb_read(fs->bdev, &fs->sb);
71 if(!ext4_sb_check(&fs->sb))
74 bsize = ext4_sb_get_block_size(&fs->sb);
75 if (bsize > EXT4_MAX_BLOCK_SIZE)
78 r = ext4_fs_check_features(fs, &read_only);
85 /* Compute limits for indirect block levels */
86 uint32_t blocks_id = bsize / sizeof(uint32_t);
88 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
89 fs->inode_blocks_per_level[0] = 1;
91 for (i = 1; i < 4; i++) {
92 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
94 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
95 fs->inode_blocks_per_level[i];
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");
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);
112 /*Update mount count*/
113 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
119 int ext4_fs_fini(struct ext4_fs *fs)
123 /*Set superblock state*/
124 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
126 return ext4_sb_write(fs->bdev, &fs->sb);
129 static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
132 if(features_incompatible &
133 EXT4_FEATURE_INCOMPAT_COMPRESSION){
134 ext4_dprintf(EXT4_DEBUG_FS, "compression\n");
136 if(features_incompatible &
137 EXT4_FEATURE_INCOMPAT_FILETYPE){
138 ext4_dprintf(EXT4_DEBUG_FS, "filetype\n");
140 if(features_incompatible &
141 EXT4_FEATURE_INCOMPAT_RECOVER){
142 ext4_dprintf(EXT4_DEBUG_FS, "recover\n");
144 if(features_incompatible &
145 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV){
146 ext4_dprintf(EXT4_DEBUG_FS,"journal_dev\n");
148 if(features_incompatible &
149 EXT4_FEATURE_INCOMPAT_META_BG){
150 ext4_dprintf(EXT4_DEBUG_FS, "meta_bg\n");
152 if(features_incompatible &
153 EXT4_FEATURE_INCOMPAT_EXTENTS){
154 ext4_dprintf(EXT4_DEBUG_FS, "extents\n");
156 if(features_incompatible &
157 EXT4_FEATURE_INCOMPAT_64BIT){
158 ext4_dprintf(EXT4_DEBUG_FS, "64bit\n");
160 if(features_incompatible &
161 EXT4_FEATURE_INCOMPAT_MMP){
162 ext4_dprintf(EXT4_DEBUG_FS, "mnp\n");
164 if(features_incompatible &
165 EXT4_FEATURE_INCOMPAT_FLEX_BG){
166 ext4_dprintf(EXT4_DEBUG_FS, "flex_bg\n");
168 if(features_incompatible &
169 EXT4_FEATURE_INCOMPAT_EA_INODE){
170 ext4_dprintf(EXT4_DEBUG_FS, "ea_inode\n");
172 if(features_incompatible &
173 EXT4_FEATURE_INCOMPAT_DIRDATA){
174 ext4_dprintf(EXT4_DEBUG_FS, "dirdata\n");
176 if(features_incompatible &
177 EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM){
178 ext4_dprintf(EXT4_DEBUG_FS, "meta_csum\n");
180 if(features_incompatible &
181 EXT4_FEATURE_INCOMPAT_LARGEDIR){
182 ext4_dprintf(EXT4_DEBUG_FS, "largedir\n");
184 if(features_incompatible &
185 EXT4_FEATURE_INCOMPAT_INLINE_DATA){
186 ext4_dprintf(EXT4_DEBUG_FS, "inline_data\n");
189 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
191 if(features_compatible &
192 EXT4_FEATURE_COMPAT_DIR_PREALLOC){
193 ext4_dprintf(EXT4_DEBUG_FS, " dir_prealloc\n");
195 if(features_compatible &
196 EXT4_FEATURE_COMPAT_IMAGIC_INODES){
197 ext4_dprintf(EXT4_DEBUG_FS, "imagic_inodes\n");
199 if(features_compatible &
200 EXT4_FEATURE_COMPAT_HAS_JOURNAL){
201 ext4_dprintf(EXT4_DEBUG_FS, "has_journal\n");
203 if(features_compatible &
204 EXT4_FEATURE_COMPAT_EXT_ATTR){
205 ext4_dprintf(EXT4_DEBUG_FS, "ext_attr\n");
207 if(features_compatible &
208 EXT4_FEATURE_COMPAT_RESIZE_INODE){
209 ext4_dprintf(EXT4_DEBUG_FS, "resize_inode\n");
211 if(features_compatible &
212 EXT4_FEATURE_COMPAT_DIR_INDEX){
213 ext4_dprintf(EXT4_DEBUG_FS, "dir_index\n");
217 static void ext4_fs_debug_features_ro(uint32_t features_ro)
220 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER){
221 ext4_dprintf(EXT4_DEBUG_FS, "sparse_super\n");
224 EXT4_FEATURE_RO_COMPAT_LARGE_FILE){
225 ext4_dprintf(EXT4_DEBUG_FS, "large_file\n");
228 EXT4_FEATURE_RO_COMPAT_BTREE_DIR){
229 ext4_dprintf(EXT4_DEBUG_FS, "btree_dir\n");
232 EXT4_FEATURE_RO_COMPAT_HUGE_FILE){
233 ext4_dprintf(EXT4_DEBUG_FS, "huge_file\n");
236 EXT4_FEATURE_RO_COMPAT_GDT_CSUM){
237 ext4_dprintf(EXT4_DEBUG_FS, "gtd_csum\n");
240 EXT4_FEATURE_RO_COMPAT_DIR_NLINK){
241 ext4_dprintf(EXT4_DEBUG_FS, "dir_nlink\n");
244 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE){
245 ext4_dprintf(EXT4_DEBUG_FS, "extra_isize\n");
248 EXT4_FEATURE_RO_COMPAT_QUOTA){
249 ext4_dprintf(EXT4_DEBUG_FS, "quota\n");
252 EXT4_FEATURE_RO_COMPAT_BIGALLOC){
253 ext4_dprintf(EXT4_DEBUG_FS, "bigalloc\n");
256 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM){
257 ext4_dprintf(EXT4_DEBUG_FS, "metadata_csum\n");
261 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
263 ext4_assert(fs && read_only);
265 if(ext4_get32(&fs->sb, rev_level) == 0){
269 ext4_dprintf(EXT4_DEBUG_FS,
270 "\nSblock rev_level: \n%d\n", (int)ext4_get32(&fs->sb, rev_level));
272 ext4_dprintf(EXT4_DEBUG_FS,
273 "\nSblock minor_rev_level: \n%d\n",
274 ext4_get32(&fs->sb, minor_rev_level));
276 ext4_dprintf(EXT4_DEBUG_FS,
277 "\nSblock features_incompatible:\n");
278 ext4_fs_debug_features_incomp(ext4_get32(&fs->sb, features_incompatible));
280 ext4_dprintf(EXT4_DEBUG_FS,
281 "\nSblock features_compatible:\n");
282 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
284 ext4_dprintf(EXT4_DEBUG_FS,
285 "\nSblock features_read_only:\n");
286 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
288 /*Check features_incompatible*/
289 v = (ext4_get32(&fs->sb, features_incompatible) &
290 (~EXT4_FEATURE_INCOMPAT_SUPP));
292 ext4_dprintf(EXT4_DEBUG_FS,
293 "\nERROR sblock features_incompatible. Unsupported:\n");
294 ext4_fs_debug_features_incomp(v);
299 /*Check features_read_only*/
300 v = (ext4_get32(&fs->sb, features_read_only) &
301 (~EXT4_FEATURE_RO_COMPAT_SUPP));
303 ext4_dprintf(EXT4_DEBUG_FS,
304 "\nERROR sblock features_read_only . Unsupported:\n");
305 ext4_fs_debug_features_incomp(v);
316 /**@brief Initialize block bitmap in block group.
317 * @param bg_ref Reference to block group
320 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
323 uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
324 bg_ref->block_group, &bg_ref->fs->sb);
326 struct ext4_block block_bitmap;
327 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
333 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
335 /* Determine first block and first data block in group */
336 uint32_t first_idx = 0;
338 uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
339 &bg_ref->fs->sb, bg_ref);
340 uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
341 &bg_ref->fs->sb, first_data);
343 /*Set bits from to first block to first data block - 1 to one (allocated)*/
344 /*TODO: Optimize it*/
345 for (i = first_idx; i < first_data_idx; ++i)
346 ext4_bmap_bit_set(block_bitmap.data, i);
349 block_bitmap.dirty = true;
352 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
355 /**@brief Initialize i-node bitmap in block group.
356 * @param bg_ref Reference to block group
359 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
362 uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
363 bg_ref->block_group, &bg_ref->fs->sb);
365 struct ext4_block block_bitmap;
366 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
371 /* Initialize all bitmap bits to zero */
372 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
373 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
375 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
377 uint32_t start_bit = inodes_per_group;
378 uint32_t end_bit = block_size * 8;
381 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
382 ext4_bmap_bit_set(block_bitmap.data, i);
385 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
387 block_bitmap.dirty = true;
390 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
393 /**@brief Initialize i-node table in block group.
394 * @param bg_ref Reference to block group
397 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
399 struct ext4_sblock *sb = &bg_ref->fs->sb;
401 uint32_t inode_size = ext4_get32(sb, inode_size);
402 uint32_t block_size = ext4_sb_get_block_size(sb);
403 uint32_t inodes_per_block = block_size / inode_size;
404 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
405 uint32_t table_blocks = inodes_in_group / inodes_per_block;
408 if (inodes_in_group % inodes_per_block)
411 /* Compute initialization bounds */
412 uint32_t first_block = ext4_bg_get_inode_table_first_block(
413 bg_ref->block_group, sb);
415 uint32_t last_block = first_block + table_blocks - 1;
417 /* Initialization of all itable blocks */
418 for (fblock = first_block; fblock <= last_block; ++fblock) {
420 struct ext4_block block;
421 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
425 memset(block.data, 0, block_size);
428 ext4_block_set(bg_ref->fs->bdev, &block);
436 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
437 uint32_t bgid, uint32_t dsc_per_block)
439 uint32_t first_meta_bg, dsc_id;
443 dsc_id = bgid / dsc_per_block;
444 first_meta_bg = ext4_sb_first_meta_bg(s);
446 if (!ext4_sb_has_feature_incompatible(s, EXT4_FEATURE_INCOMPAT_META_BG) ||
447 dsc_id < first_meta_bg)
448 return ext4_get32(s, first_data_block) + dsc_id + 1;
450 if (ext4_sb_is_super_in_bg(s, bgid))
453 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
457 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
458 struct ext4_block_group_ref *ref)
460 /* Compute number of descriptors, that fits in one data block */
461 uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
462 ext4_sb_get_desc_size(&fs->sb);
464 /* Block group descriptor table starts at the next block after superblock */
465 uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid,
468 uint32_t offset = (bgid % dsc_per_block) *
469 ext4_sb_get_desc_size(&fs->sb);
471 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
475 ref->block_group = (void *)(ref->block.data + offset);
480 if (ext4_bg_has_flag(ref->block_group,
481 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
482 rc = ext4_fs_init_block_bitmap(ref);
484 ext4_block_set(fs->bdev, &ref->block);
487 ext4_bg_clear_flag(ref->block_group,
488 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
493 if (ext4_bg_has_flag(ref->block_group,
494 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
495 rc = ext4_fs_init_inode_bitmap(ref);
497 ext4_block_set(ref->fs->bdev, &ref->block);
501 ext4_bg_clear_flag(ref->block_group,
502 EXT4_BLOCK_GROUP_INODE_UNINIT);
504 if (!ext4_bg_has_flag(ref->block_group,
505 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
506 rc = ext4_fs_init_inode_table(ref);
508 ext4_block_set(fs->bdev, &ref->block);
512 ext4_bg_set_flag(ref->block_group,
513 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
522 /**@brief Compute checksum of block group descriptor.
523 * @param sb Superblock
524 * @param bgid Index of block group in the filesystem
525 * @param bg Block group to compute checksum for
526 * @return Checksum value
528 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
529 struct ext4_bgroup *bg)
531 /* If checksum not supported, 0 will be returned */
534 /* Compute the checksum only if the filesystem supports it */
535 if (ext4_sb_has_feature_read_only(sb,
536 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
537 uint8_t *base = (uint8_t *)bg;
538 uint8_t *checksum = (uint8_t *)&bg->checksum;
540 uint32_t offset = (uint32_t) (checksum - base);
542 /* Convert block group index to little endian */
543 uint32_t le_group = to_le32(bgid);
546 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
548 /* Include index of block group */
549 crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
551 /* Compute crc from the first part (stop before checksum field) */
552 crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
555 offset += sizeof(bg->checksum);
557 /* Checksum of the rest of block group descriptor */
558 if ((ext4_sb_has_feature_incompatible(sb,
559 EXT4_FEATURE_INCOMPAT_64BIT)) &&
560 (offset < ext4_sb_get_desc_size(sb)))
562 crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
563 ext4_sb_get_desc_size(sb) - offset);
568 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
570 /* Check if reference modified */
572 /* Compute new checksum of block group */
574 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
577 ref->block_group->checksum = to_le16(checksum);
579 /* Mark block dirty for writing changes to physical device */
580 ref->block.dirty = true;
583 /* Put back block, that contains block group descriptor */
584 return ext4_block_set(ref->fs->bdev, &ref->block);
587 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
588 struct ext4_inode_ref *ref)
590 /* Compute number of i-nodes, that fits in one data block */
591 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
594 * Inode numbers are 1-based, but it is simpler to work with 0-based
595 * when computing indices
598 uint32_t block_group = index / inodes_per_group;
599 uint32_t offset_in_group = index % inodes_per_group;
601 /* Load block group, where i-node is located */
602 struct ext4_block_group_ref bg_ref;
604 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
609 /* Load block address, where i-node table is located */
610 uint32_t inode_table_start =
611 ext4_bg_get_inode_table_first_block(bg_ref.block_group,
614 /* Put back block group reference (not needed more) */
615 rc = ext4_fs_put_block_group_ref(&bg_ref);
620 /* Compute position of i-node in the block group */
621 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
622 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
623 uint32_t byte_offset_in_group = offset_in_group * inode_size;
625 /* Compute block address */
626 uint64_t block_id = inode_table_start +
627 (byte_offset_in_group / block_size);
630 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
635 /* Compute position of i-node in the data block */
636 uint32_t offset_in_block = byte_offset_in_group % block_size;
637 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
639 /* We need to store the original value of index in the reference */
640 ref->index = index + 1;
647 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
649 /* Check if reference modified */
651 /* Mark block dirty for writing changes to physical device */
652 ref->block.dirty = true;
655 /* Put back block, that contains i-node */
656 return ext4_block_set(ref->fs->bdev, &ref->block);
659 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
662 /* Check if newly allocated i-node will be a directory */
666 is_dir = is_directory;
668 /* Allocate inode by allocation algorithm */
670 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
674 /* Load i-node from on-disk i-node table */
675 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
677 ext4_ialloc_free_inode(fs, index, is_dir);
681 /* Initialize i-node */
682 struct ext4_inode *inode = inode_ref->inode;
687 * Default directory permissions to be compatible with other systems
688 * 0777 (octal) == rwxrwxrwx
692 mode |= EXT4_INODE_MODE_DIRECTORY;
693 ext4_inode_set_mode(&fs->sb, inode, mode);
697 * Default file permissions to be compatible with other systems
698 * 0666 (octal) == rw-rw-rw-
702 mode |= EXT4_INODE_MODE_FILE;
703 ext4_inode_set_mode(&fs->sb, inode, mode);
706 ext4_inode_set_links_count(inode, 0);
707 ext4_inode_set_uid(inode, 0);
708 ext4_inode_set_gid(inode, 0);
709 ext4_inode_set_size(inode, 0);
710 ext4_inode_set_access_time(inode, 0);
711 ext4_inode_set_change_inode_time(inode, 0);
712 ext4_inode_set_modification_time(inode, 0);
713 ext4_inode_set_deletion_time(inode, 0);
714 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
715 ext4_inode_set_flags(inode, 0);
716 ext4_inode_set_generation(inode, 0);
718 /* Reset blocks array */
719 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
720 inode->blocks[i] = 0;
722 #if CONFIG_EXTENT_ENABLE
723 /* Initialize extents if needed */
724 if (ext4_sb_has_feature_incompatible(
725 &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
726 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
729 /* Initialize extent root header */
730 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
731 ext4_extent_header_set_depth(header, 0);
732 ext4_extent_header_set_entries_count(header, 0);
733 ext4_extent_header_set_generation(header, 0);
734 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
736 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
737 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
739 ext4_extent_header_set_max_entries_count(header, max_entries);
743 inode_ref->dirty = true;
748 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
750 struct ext4_fs *fs = inode_ref->fs;
753 #if CONFIG_EXTENT_ENABLE
754 /* For extents must be data block destroyed by other way */
755 if ((ext4_sb_has_feature_incompatible(&fs->sb,
756 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
757 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
758 /* Data structures are released during truncate operation... */
762 /* Release all indirect (no data) blocks */
764 /* 1) Single indirect */
765 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
767 int rc = ext4_balloc_free_block(inode_ref, fblock);
771 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
774 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
775 uint32_t count = block_size / sizeof(uint32_t);
777 struct ext4_block block;
779 /* 2) Double indirect */
780 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
782 int rc = ext4_block_get(fs->bdev, &block, fblock);
787 for (offset = 0; offset < count; ++offset) {
788 ind_block = to_le32(((uint32_t *) block.data)[offset]);
790 if (ind_block != 0) {
791 rc = ext4_balloc_free_block(inode_ref, ind_block);
793 ext4_block_set(fs->bdev, &block);
799 ext4_block_set(fs->bdev, &block);
800 rc = ext4_balloc_free_block(inode_ref, fblock);
804 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
807 /* 3) Tripple indirect */
808 struct ext4_block subblock;
809 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
811 int rc = ext4_block_get(fs->bdev, &block, fblock);
816 for ( offset = 0; offset < count; ++offset) {
817 ind_block = to_le32(((uint32_t *) block.data)[offset]);
819 if (ind_block != 0) {
820 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
822 ext4_block_set(fs->bdev, &block);
826 uint32_t ind_subblock;
827 for (suboffset = 0; suboffset < count;
829 ind_subblock = to_le32(((uint32_t *)
830 subblock.data)[suboffset]);
832 if (ind_subblock != 0) {
833 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
835 ext4_block_set(fs->bdev, &subblock);
836 ext4_block_set(fs->bdev, &block);
842 ext4_block_set(fs->bdev, &subblock);
845 rc = ext4_balloc_free_block(inode_ref, ind_block);
847 ext4_block_set(fs->bdev, &block);
853 ext4_block_set(fs->bdev, &block);
854 rc = ext4_balloc_free_block(inode_ref, fblock);
858 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
862 /* Mark inode dirty for writing to the physical device */
863 inode_ref->dirty = true;
865 /* Free block with extended attributes if present */
866 uint32_t xattr_block = ext4_inode_get_file_acl(
867 inode_ref->inode, &fs->sb);
869 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
873 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
876 /* Free inode by allocator */
878 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
879 EXT4_INODE_MODE_DIRECTORY))
880 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
882 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
887 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
890 struct ext4_sblock *sb = &inode_ref->fs->sb;
893 /* Check flags, if i-node can be truncated */
894 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
897 /* If sizes are equal, nothing has to be done. */
898 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
899 if (old_size == new_size)
902 /* It's not suppported to make the larger file by truncate operation */
903 if (old_size < new_size)
906 /* Compute how many blocks will be released */
907 uint64_t size_diff = old_size - new_size;
908 uint32_t block_size = ext4_sb_get_block_size(sb);
909 uint32_t diff_blocks_count = size_diff / block_size;
910 if (size_diff % block_size != 0)
913 uint32_t old_blocks_count = old_size / block_size;
914 if (old_size % block_size != 0)
916 #if CONFIG_EXTENT_ENABLE
917 if ((ext4_sb_has_feature_incompatible(sb,
918 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
919 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
921 /* Extents require special operation */
922 int rc = ext4_extent_release_blocks_from(inode_ref,
923 old_blocks_count - diff_blocks_count);
930 /* Release data blocks from the end of file */
932 /* Starting from 1 because of logical blocks are numbered from 0 */
933 for (i = 1; i <= diff_blocks_count; ++i) {
934 int rc = ext4_fs_release_inode_block(inode_ref,
935 old_blocks_count - i);
942 ext4_inode_set_size(inode_ref->inode, new_size);
943 inode_ref->dirty = true;
948 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
949 uint64_t iblock, uint32_t *fblock)
951 struct ext4_fs *fs = inode_ref->fs;
953 /* For empty file is situation simple */
954 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
959 uint32_t current_block;
960 #if CONFIG_EXTENT_ENABLE
961 /* Handle i-node using extents */
962 if ((ext4_sb_has_feature_incompatible(&fs->sb,
963 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
964 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
967 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
971 *fblock = current_block;
976 struct ext4_inode *inode = inode_ref->inode;
978 /* Direct block are read directly from array in i-node structure */
979 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
980 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
981 *fblock = current_block;
985 /* Determine indirection level of the target block */
986 unsigned int level = 0;
988 for (i = 1; i < 4; i++) {
989 if (iblock < fs->inode_block_limits[i]) {
998 /* Compute offsets for the topmost level */
999 uint64_t block_offset_in_level =
1000 iblock - fs->inode_block_limits[level - 1];
1001 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1002 uint32_t offset_in_block =
1003 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1006 if (current_block == 0) {
1011 struct ext4_block block;
1014 * Navigate through other levels, until we find the block number
1015 * or find null reference meaning we are dealing with sparse file
1018 /* Load indirect block */
1019 int rc = ext4_block_get(fs->bdev, &block, current_block);
1023 /* Read block address from indirect block */
1025 to_le32(((uint32_t *) block.data)[offset_in_block]);
1027 /* Put back indirect block untouched */
1028 rc = ext4_block_set(fs->bdev, &block);
1032 /* Check for sparse file */
1033 if (current_block == 0) {
1038 /* Jump to the next level */
1041 /* Termination condition - we have address of data block loaded */
1045 /* Visit the next level */
1046 block_offset_in_level %= fs->inode_blocks_per_level[level];
1048 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1051 *fblock = current_block;
1056 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1057 uint64_t iblock, uint32_t fblock)
1059 struct ext4_fs *fs = inode_ref->fs;
1061 #if CONFIG_EXTENT_ENABLE
1062 /* Handle inode using extents */
1063 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1064 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1065 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1071 /* Handle simple case when we are dealing with direct reference */
1072 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1073 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1075 inode_ref->dirty = true;
1080 /* Determine the indirection level needed to get the desired block */
1081 unsigned int level = 0;
1083 for (i = 1; i < 4; i++) {
1084 if (iblock < fs->inode_block_limits[i]) {
1093 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1095 /* Compute offsets for the topmost level */
1096 uint64_t block_offset_in_level =
1097 iblock - fs->inode_block_limits[level - 1];
1098 uint32_t current_block =
1099 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1100 uint32_t offset_in_block =
1101 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1103 uint32_t new_block_addr;
1105 struct ext4_block block;
1106 struct ext4_block new_block;
1108 /* Is needed to allocate indirect block on the i-node level */
1109 if (current_block == 0) {
1110 /* Allocate new indirect block */
1111 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1116 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1118 inode_ref->dirty = true;
1120 /* Load newly allocated block */
1121 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1123 ext4_balloc_free_block(inode_ref, new_block_addr);
1127 /* Initialize new block */
1128 memset(new_block.data, 0, block_size);
1129 new_block.dirty = true;
1131 /* Put back the allocated block */
1132 rc = ext4_block_set(fs->bdev, &new_block);
1136 current_block = new_block_addr;
1140 * Navigate through other levels, until we find the block number
1141 * or find null reference meaning we are dealing with sparse file
1144 int rc = ext4_block_get(fs->bdev, &block, current_block);
1149 to_le32(((uint32_t *) block.data)[offset_in_block]);
1151 if ((level > 1) && (current_block == 0)) {
1152 /* Allocate new block */
1153 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1155 ext4_block_set(fs->bdev, &block);
1159 /* Load newly allocated block */
1160 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1163 ext4_block_set(fs->bdev, &block);
1167 /* Initialize allocated block */
1168 memset(new_block.data, 0, block_size);
1169 new_block.dirty = true;
1171 rc = ext4_block_set(fs->bdev, &new_block);
1173 ext4_block_set(fs->bdev, &block);
1177 /* Write block address to the parent */
1178 ((uint32_t *) block.data)[offset_in_block] =
1179 to_le32(new_block_addr);
1181 current_block = new_block_addr;
1184 /* Will be finished, write the fblock address */
1186 ((uint32_t *) block.data)[offset_in_block] =
1191 rc = ext4_block_set(fs->bdev, &block);
1198 * If we are on the last level, break here as
1199 * there is no next level to visit
1204 /* Visit the next level */
1205 block_offset_in_level %= fs->inode_blocks_per_level[level];
1207 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1213 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1218 struct ext4_fs *fs = inode_ref->fs;
1220 /* Extents are handled otherwise = there is not support in this function */
1221 ext4_assert(!(ext4_sb_has_feature_incompatible(&fs->sb,
1222 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1223 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1225 struct ext4_inode *inode = inode_ref->inode;
1227 /* Handle simple case when we are dealing with direct reference */
1228 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1229 fblock = ext4_inode_get_direct_block(inode, iblock);
1235 ext4_inode_set_direct_block(inode, iblock, 0);
1236 return ext4_balloc_free_block(inode_ref, fblock);
1239 /* Determine the indirection level needed to get the desired block */
1240 unsigned int level = 0;
1242 for (i = 1; i < 4; i++) {
1243 if (iblock < fs->inode_block_limits[i]) {
1252 /* Compute offsets for the topmost level */
1253 uint64_t block_offset_in_level =
1254 iblock - fs->inode_block_limits[level - 1];
1255 uint32_t current_block =
1256 ext4_inode_get_indirect_block(inode, level - 1);
1257 uint32_t offset_in_block =
1258 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1261 * Navigate through other levels, until we find the block number
1262 * or find null reference meaning we are dealing with sparse file
1264 struct ext4_block block;
1269 if (current_block == 0)
1272 int rc = ext4_block_get(fs->bdev, &block, current_block);
1277 to_le32(((uint32_t *) block.data)[offset_in_block]);
1279 /* Set zero if physical data block address found */
1281 ((uint32_t *) block.data)[offset_in_block] =
1286 rc = ext4_block_set(fs->bdev, &block);
1293 * If we are on the last level, break here as
1294 * there is no next level to visit
1299 /* Visit the next level */
1300 block_offset_in_level %= fs->inode_blocks_per_level[level];
1302 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1305 fblock = current_block;
1309 /* Physical block is not referenced, it can be released */
1310 return ext4_balloc_free_block(inode_ref, fblock);
1314 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1315 uint32_t *fblock, uint32_t *iblock)
1317 #if CONFIG_EXTENT_ENABLE
1318 /* Handle extents separately */
1319 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1320 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1321 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1322 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1325 struct ext4_sblock *sb = &inode_ref->fs->sb;
1327 /* Compute next block index and allocate data block */
1328 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1329 uint32_t block_size = ext4_sb_get_block_size(sb);
1331 /* Align size i-node size */
1332 if ((inode_size % block_size) != 0)
1333 inode_size += block_size - (inode_size % block_size);
1335 /* Logical blocks are numbered from 0 */
1336 uint32_t new_block_idx = inode_size / block_size;
1338 /* Allocate new physical block */
1339 uint32_t phys_block;
1340 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1344 /* Add physical block address to the i-node */
1345 rc = ext4_fs_set_inode_data_block_index(inode_ref,
1346 new_block_idx, phys_block);
1348 ext4_balloc_free_block(inode_ref, phys_block);
1353 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1354 inode_ref->dirty = true;
1356 *fblock = phys_block;
1357 *iblock = new_block_idx;
1362 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1365 if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1366 EXT4_INODE_MODE_DIRECTORY)){
1367 ext4_inode_set_links_count(inode_ref->inode, 0);
1371 link = ext4_inode_get_links_count(inode_ref->inode);
1373 ext4_inode_set_links_count(inode_ref->inode, link);
1376 bool is_dx = ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1377 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1378 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1380 if(is_dx && link > 1){
1381 if(link >= EXT4_LINK_MAX || link == 2){
1382 ext4_inode_set_links_count(inode_ref->inode, 1);
1384 uint32_t v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1385 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1386 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1391 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1393 if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1394 EXT4_INODE_MODE_DIRECTORY)){
1395 ext4_inode_set_links_count(inode_ref->inode, 0);
1399 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1403 ext4_inode_set_links_count(inode_ref->inode, links - 1);