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 (~CONFIG_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 (~CONFIG_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);
694 ext4_inode_set_links_count(inode, 0);
698 * Default file permissions to be compatible with other systems
699 * 0666 (octal) == rw-rw-rw-
703 mode |= EXT4_INODE_MODE_FILE;
704 ext4_inode_set_mode(&fs->sb, inode, mode);
705 ext4_inode_set_links_count(inode, 1);
709 ext4_inode_set_uid(inode, 0);
710 ext4_inode_set_gid(inode, 0);
711 ext4_inode_set_size(inode, 0);
712 ext4_inode_set_access_time(inode, 0);
713 ext4_inode_set_change_inode_time(inode, 0);
714 ext4_inode_set_modification_time(inode, 0);
715 ext4_inode_set_deletion_time(inode, 0);
716 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
717 ext4_inode_set_flags(inode, 0);
718 ext4_inode_set_generation(inode, 0);
720 /* Reset blocks array */
721 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
722 inode->blocks[i] = 0;
724 #if CONFIG_EXTENT_ENABLE
725 /* Initialize extents if needed */
726 if (ext4_sb_has_feature_incompatible(
727 &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
728 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
731 /* Initialize extent root header */
732 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
733 ext4_extent_header_set_depth(header, 0);
734 ext4_extent_header_set_entries_count(header, 0);
735 ext4_extent_header_set_generation(header, 0);
736 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
738 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
739 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
741 ext4_extent_header_set_max_entries_count(header, max_entries);
745 inode_ref->dirty = true;
750 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
752 struct ext4_fs *fs = inode_ref->fs;
755 #if CONFIG_EXTENT_ENABLE
756 /* For extents must be data block destroyed by other way */
757 if ((ext4_sb_has_feature_incompatible(&fs->sb,
758 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
759 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
760 /* Data structures are released during truncate operation... */
764 /* Release all indirect (no data) blocks */
766 /* 1) Single indirect */
767 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
769 int rc = ext4_balloc_free_block(inode_ref, fblock);
773 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
776 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
777 uint32_t count = block_size / sizeof(uint32_t);
779 struct ext4_block block;
781 /* 2) Double indirect */
782 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
784 int rc = ext4_block_get(fs->bdev, &block, fblock);
789 for (offset = 0; offset < count; ++offset) {
790 ind_block = to_le32(((uint32_t *) block.data)[offset]);
792 if (ind_block != 0) {
793 rc = ext4_balloc_free_block(inode_ref, ind_block);
795 ext4_block_set(fs->bdev, &block);
801 ext4_block_set(fs->bdev, &block);
802 rc = ext4_balloc_free_block(inode_ref, fblock);
806 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
809 /* 3) Tripple indirect */
810 struct ext4_block subblock;
811 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
813 int rc = ext4_block_get(fs->bdev, &block, fblock);
818 for ( offset = 0; offset < count; ++offset) {
819 ind_block = to_le32(((uint32_t *) block.data)[offset]);
821 if (ind_block != 0) {
822 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
824 ext4_block_set(fs->bdev, &block);
828 uint32_t ind_subblock;
829 for (suboffset = 0; suboffset < count;
831 ind_subblock = to_le32(((uint32_t *)
832 subblock.data)[suboffset]);
834 if (ind_subblock != 0) {
835 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
837 ext4_block_set(fs->bdev, &subblock);
838 ext4_block_set(fs->bdev, &block);
844 ext4_block_set(fs->bdev, &subblock);
847 rc = ext4_balloc_free_block(inode_ref, ind_block);
849 ext4_block_set(fs->bdev, &block);
855 ext4_block_set(fs->bdev, &block);
856 rc = ext4_balloc_free_block(inode_ref, fblock);
860 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
862 #if CONFIG_EXTENT_ENABLE
865 /* Mark inode dirty for writing to the physical device */
866 inode_ref->dirty = true;
868 /* Free block with extended attributes if present */
869 uint32_t xattr_block = ext4_inode_get_file_acl(
870 inode_ref->inode, &fs->sb);
872 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
876 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
879 /* Free inode by allocator */
881 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
882 EXT4_INODE_MODE_DIRECTORY))
883 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
885 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
890 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
893 struct ext4_sblock *sb = &inode_ref->fs->sb;
896 /* Check flags, if i-node can be truncated */
897 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
900 /* If sizes are equal, nothing has to be done. */
901 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
902 if (old_size == new_size)
905 /* It's not suppported to make the larger file by truncate operation */
906 if (old_size < new_size)
909 /* Compute how many blocks will be released */
910 uint64_t size_diff = old_size - new_size;
911 uint32_t block_size = ext4_sb_get_block_size(sb);
912 uint32_t diff_blocks_count = size_diff / block_size;
913 if (size_diff % block_size != 0)
916 uint32_t old_blocks_count = old_size / block_size;
917 if (old_size % block_size != 0)
919 #if CONFIG_EXTENT_ENABLE
920 if ((ext4_sb_has_feature_incompatible(sb,
921 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
922 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
924 /* Extents require special operation */
925 int rc = ext4_extent_release_blocks_from(inode_ref,
926 old_blocks_count - diff_blocks_count);
933 /* Release data blocks from the end of file */
935 /* Starting from 1 because of logical blocks are numbered from 0 */
936 for (i = 1; i <= diff_blocks_count; ++i) {
937 int rc = ext4_fs_release_inode_block(inode_ref,
938 old_blocks_count - i);
945 ext4_inode_set_size(inode_ref->inode, new_size);
946 inode_ref->dirty = true;
951 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
952 uint64_t iblock, uint32_t *fblock)
954 struct ext4_fs *fs = inode_ref->fs;
956 /* For empty file is situation simple */
957 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
962 uint32_t current_block;
963 #if CONFIG_EXTENT_ENABLE
964 /* Handle i-node using extents */
965 if ((ext4_sb_has_feature_incompatible(&fs->sb,
966 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
967 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
970 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
974 *fblock = current_block;
979 struct ext4_inode *inode = inode_ref->inode;
981 /* Direct block are read directly from array in i-node structure */
982 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
983 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
984 *fblock = current_block;
988 /* Determine indirection level of the target block */
989 unsigned int level = 0;
991 for (i = 1; i < 4; i++) {
992 if (iblock < fs->inode_block_limits[i]) {
1001 /* Compute offsets for the topmost level */
1002 uint64_t block_offset_in_level =
1003 iblock - fs->inode_block_limits[level - 1];
1004 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1005 uint32_t offset_in_block =
1006 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1009 if (current_block == 0) {
1014 struct ext4_block block;
1017 * Navigate through other levels, until we find the block number
1018 * or find null reference meaning we are dealing with sparse file
1021 /* Load indirect block */
1022 int rc = ext4_block_get(fs->bdev, &block, current_block);
1026 /* Read block address from indirect block */
1028 to_le32(((uint32_t *) block.data)[offset_in_block]);
1030 /* Put back indirect block untouched */
1031 rc = ext4_block_set(fs->bdev, &block);
1035 /* Check for sparse file */
1036 if (current_block == 0) {
1041 /* Jump to the next level */
1044 /* Termination condition - we have address of data block loaded */
1048 /* Visit the next level */
1049 block_offset_in_level %= fs->inode_blocks_per_level[level];
1051 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1054 *fblock = current_block;
1059 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1060 uint64_t iblock, uint32_t fblock)
1062 struct ext4_fs *fs = inode_ref->fs;
1064 #if CONFIG_EXTENT_ENABLE
1065 /* Handle inode using extents */
1066 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1067 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1068 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1074 /* Handle simple case when we are dealing with direct reference */
1075 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1076 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1078 inode_ref->dirty = true;
1083 /* Determine the indirection level needed to get the desired block */
1084 unsigned int level = 0;
1086 for (i = 1; i < 4; i++) {
1087 if (iblock < fs->inode_block_limits[i]) {
1096 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1098 /* Compute offsets for the topmost level */
1099 uint64_t block_offset_in_level =
1100 iblock - fs->inode_block_limits[level - 1];
1101 uint32_t current_block =
1102 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1103 uint32_t offset_in_block =
1104 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1106 uint32_t new_block_addr;
1108 struct ext4_block block;
1109 struct ext4_block new_block;
1111 /* Is needed to allocate indirect block on the i-node level */
1112 if (current_block == 0) {
1113 /* Allocate new indirect block */
1114 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1119 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1121 inode_ref->dirty = true;
1123 /* Load newly allocated block */
1124 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1126 ext4_balloc_free_block(inode_ref, new_block_addr);
1130 /* Initialize new block */
1131 memset(new_block.data, 0, block_size);
1132 new_block.dirty = true;
1134 /* Put back the allocated block */
1135 rc = ext4_block_set(fs->bdev, &new_block);
1139 current_block = new_block_addr;
1143 * Navigate through other levels, until we find the block number
1144 * or find null reference meaning we are dealing with sparse file
1147 int rc = ext4_block_get(fs->bdev, &block, current_block);
1152 to_le32(((uint32_t *) block.data)[offset_in_block]);
1154 if ((level > 1) && (current_block == 0)) {
1155 /* Allocate new block */
1156 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1158 ext4_block_set(fs->bdev, &block);
1162 /* Load newly allocated block */
1163 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1166 ext4_block_set(fs->bdev, &block);
1170 /* Initialize allocated block */
1171 memset(new_block.data, 0, block_size);
1172 new_block.dirty = true;
1174 rc = ext4_block_set(fs->bdev, &new_block);
1176 ext4_block_set(fs->bdev, &block);
1180 /* Write block address to the parent */
1181 ((uint32_t *) block.data)[offset_in_block] =
1182 to_le32(new_block_addr);
1184 current_block = new_block_addr;
1187 /* Will be finished, write the fblock address */
1189 ((uint32_t *) block.data)[offset_in_block] =
1194 rc = ext4_block_set(fs->bdev, &block);
1201 * If we are on the last level, break here as
1202 * there is no next level to visit
1207 /* Visit the next level */
1208 block_offset_in_level %= fs->inode_blocks_per_level[level];
1210 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1216 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1221 struct ext4_fs *fs = inode_ref->fs;
1223 /* Extents are handled otherwise = there is not support in this function */
1224 ext4_assert(!(ext4_sb_has_feature_incompatible(&fs->sb,
1225 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1226 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1228 struct ext4_inode *inode = inode_ref->inode;
1230 /* Handle simple case when we are dealing with direct reference */
1231 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1232 fblock = ext4_inode_get_direct_block(inode, iblock);
1238 ext4_inode_set_direct_block(inode, iblock, 0);
1239 return ext4_balloc_free_block(inode_ref, fblock);
1242 /* Determine the indirection level needed to get the desired block */
1243 unsigned int level = 0;
1245 for (i = 1; i < 4; i++) {
1246 if (iblock < fs->inode_block_limits[i]) {
1255 /* Compute offsets for the topmost level */
1256 uint64_t block_offset_in_level =
1257 iblock - fs->inode_block_limits[level - 1];
1258 uint32_t current_block =
1259 ext4_inode_get_indirect_block(inode, level - 1);
1260 uint32_t offset_in_block =
1261 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1264 * Navigate through other levels, until we find the block number
1265 * or find null reference meaning we are dealing with sparse file
1267 struct ext4_block block;
1272 if (current_block == 0)
1275 int rc = ext4_block_get(fs->bdev, &block, current_block);
1280 to_le32(((uint32_t *) block.data)[offset_in_block]);
1282 /* Set zero if physical data block address found */
1284 ((uint32_t *) block.data)[offset_in_block] =
1289 rc = ext4_block_set(fs->bdev, &block);
1296 * If we are on the last level, break here as
1297 * there is no next level to visit
1302 /* Visit the next level */
1303 block_offset_in_level %= fs->inode_blocks_per_level[level];
1305 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1308 fblock = current_block;
1312 /* Physical block is not referenced, it can be released */
1313 return ext4_balloc_free_block(inode_ref, fblock);
1317 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1318 uint32_t *fblock, uint32_t *iblock)
1320 #if CONFIG_EXTENT_ENABLE
1321 /* Handle extents separately */
1322 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1323 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1324 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1325 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1328 struct ext4_sblock *sb = &inode_ref->fs->sb;
1330 /* Compute next block index and allocate data block */
1331 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1332 uint32_t block_size = ext4_sb_get_block_size(sb);
1334 /* Align size i-node size */
1335 if ((inode_size % block_size) != 0)
1336 inode_size += block_size - (inode_size % block_size);
1338 /* Logical blocks are numbered from 0 */
1339 uint32_t new_block_idx = inode_size / block_size;
1341 /* Allocate new physical block */
1342 uint32_t phys_block;
1343 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1347 /* Add physical block address to the i-node */
1348 rc = ext4_fs_set_inode_data_block_index(inode_ref,
1349 new_block_idx, phys_block);
1351 ext4_balloc_free_block(inode_ref, phys_block);
1356 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1357 inode_ref->dirty = true;
1359 *fblock = phys_block;
1360 *iblock = new_block_idx;
1365 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1368 if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1369 EXT4_INODE_MODE_DIRECTORY)){
1370 ext4_inode_set_links_count(inode_ref->inode, 0);
1374 link = ext4_inode_get_links_count(inode_ref->inode);
1376 ext4_inode_set_links_count(inode_ref->inode, link);
1379 bool is_dx = ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1380 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1381 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1383 if(is_dx && link > 1){
1384 if(link >= EXT4_LINK_MAX || link == 2){
1385 ext4_inode_set_links_count(inode_ref->inode, 1);
1387 uint32_t v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1388 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1389 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1394 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1396 if(!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1397 EXT4_INODE_MODE_DIRECTORY)){
1398 ext4_inode_set_links_count(inode_ref->inode, 0);
1402 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1406 ext4_inode_set_links_count(inode_ref->inode, links - 1);