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"
57 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
62 bool read_only = false;
64 ext4_assert(fs && bdev);
68 r = ext4_sb_read(fs->bdev, &fs->sb);
72 if (!ext4_sb_check(&fs->sb))
75 bsize = ext4_sb_get_block_size(&fs->sb);
76 if (bsize > EXT4_MAX_BLOCK_SIZE)
79 r = ext4_fs_check_features(fs, &read_only);
86 /* Compute limits for indirect block levels */
87 uint32_t blocks_id = bsize / sizeof(uint32_t);
89 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
90 fs->inode_blocks_per_level[0] = 1;
92 for (i = 1; i < 4; i++) {
93 fs->inode_blocks_per_level[i] =
94 fs->inode_blocks_per_level[i - 1] * blocks_id;
95 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
96 fs->inode_blocks_per_level[i];
100 tmp = ext4_get16(&fs->sb, state);
101 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) {
102 ext4_dprintf(EXT4_DEBUG_FS, "last umount error\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);
111 /*Update mount count*/
112 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 static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
130 if (features_incompatible & EXT4_FEATURE_INCOMPAT_COMPRESSION) {
131 ext4_dprintf(EXT4_DEBUG_FS, "compression\n");
133 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE) {
134 ext4_dprintf(EXT4_DEBUG_FS, "filetype\n");
136 if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER) {
137 ext4_dprintf(EXT4_DEBUG_FS, "recover\n");
139 if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV) {
140 ext4_dprintf(EXT4_DEBUG_FS, "journal_dev\n");
142 if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG) {
143 ext4_dprintf(EXT4_DEBUG_FS, "meta_bg\n");
145 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS) {
146 ext4_dprintf(EXT4_DEBUG_FS, "extents\n");
148 if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT) {
149 ext4_dprintf(EXT4_DEBUG_FS, "64bit\n");
151 if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP) {
152 ext4_dprintf(EXT4_DEBUG_FS, "mnp\n");
154 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
155 ext4_dprintf(EXT4_DEBUG_FS, "flex_bg\n");
157 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE) {
158 ext4_dprintf(EXT4_DEBUG_FS, "ea_inode\n");
160 if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA) {
161 ext4_dprintf(EXT4_DEBUG_FS, "dirdata\n");
163 if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM) {
164 ext4_dprintf(EXT4_DEBUG_FS, "meta_csum\n");
166 if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR) {
167 ext4_dprintf(EXT4_DEBUG_FS, "largedir\n");
169 if (features_incompatible & EXT4_FEATURE_INCOMPAT_INLINE_DATA) {
170 ext4_dprintf(EXT4_DEBUG_FS, "inline_data\n");
173 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
175 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_PREALLOC) {
176 ext4_dprintf(EXT4_DEBUG_FS, " dir_prealloc\n");
178 if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES) {
179 ext4_dprintf(EXT4_DEBUG_FS, "imagic_inodes\n");
181 if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL) {
182 ext4_dprintf(EXT4_DEBUG_FS, "has_journal\n");
184 if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR) {
185 ext4_dprintf(EXT4_DEBUG_FS, "ext_attr\n");
187 if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE) {
188 ext4_dprintf(EXT4_DEBUG_FS, "resize_inode\n");
190 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_INDEX) {
191 ext4_dprintf(EXT4_DEBUG_FS, "dir_index\n");
195 static void ext4_fs_debug_features_ro(uint32_t features_ro)
197 if (features_ro & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) {
198 ext4_dprintf(EXT4_DEBUG_FS, "sparse_super\n");
200 if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE) {
201 ext4_dprintf(EXT4_DEBUG_FS, "large_file\n");
203 if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR) {
204 ext4_dprintf(EXT4_DEBUG_FS, "btree_dir\n");
206 if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
207 ext4_dprintf(EXT4_DEBUG_FS, "huge_file\n");
209 if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
210 ext4_dprintf(EXT4_DEBUG_FS, "gtd_csum\n");
212 if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK) {
213 ext4_dprintf(EXT4_DEBUG_FS, "dir_nlink\n");
215 if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) {
216 ext4_dprintf(EXT4_DEBUG_FS, "extra_isize\n");
218 if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA) {
219 ext4_dprintf(EXT4_DEBUG_FS, "quota\n");
221 if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
222 ext4_dprintf(EXT4_DEBUG_FS, "bigalloc\n");
224 if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
225 ext4_dprintf(EXT4_DEBUG_FS, "metadata_csum\n");
229 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
231 ext4_assert(fs && read_only);
233 if (ext4_get32(&fs->sb, rev_level) == 0) {
238 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_incompatible:\n");
239 ext4_fs_debug_features_incomp(
240 ext4_get32(&fs->sb, features_incompatible));
242 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_compatible:\n");
243 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
245 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_read_only:\n");
246 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
248 /*Check features_incompatible*/
249 v = (ext4_get32(&fs->sb, features_incompatible) &
250 (~CONFIG_FEATURE_INCOMPAT_SUPP));
252 ext4_dprintf(EXT4_DEBUG_FS, "SB features_incompatible: fail\n");
253 ext4_fs_debug_features_incomp(v);
257 /*Check features_read_only*/
258 v = (ext4_get32(&fs->sb, features_read_only) &
259 (~CONFIG_FEATURE_RO_COMPAT_SUPP));
263 "\nERROR sblock features_read_only . Unsupported:\n");
264 ext4_fs_debug_features_incomp(v);
274 /**@brief Determine whether the block is inside the group.
275 * @param baddr block address
276 * @param bgid block group id
279 static int ext4_block_in_group(struct ext4_sblock *s,
283 uint32_t actual_bgid;
284 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
285 if (actual_bgid == bgid)
290 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
291 * need to use it within a single byte (to ensure we get endianness right).
292 * We can use memset for the rest of the bitmap as there are no other users.
294 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
298 if (start_bit >= end_bit)
301 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
302 ext4_bmap_bit_set(bitmap, i);
305 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
308 /**@brief Initialize block bitmap in block group.
309 * @param bg_ref Reference to block group
312 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
314 uint32_t i, bit, bit_max;
315 uint32_t group_blocks;
316 uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
317 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
318 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
319 uint32_t bitmap_block_addr =
320 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
321 uint32_t bitmap_inode_addr =
322 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
323 uint32_t inode_table_addr =
324 ext4_bg_get_inode_table_first_block(bg_ref->block_group,
326 uint32_t first_group_addr =
327 ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
329 uint32_t dsc_per_block =
330 ext4_sb_get_block_size(&bg_ref->fs->sb) /
331 ext4_sb_get_desc_size(&bg_ref->fs->sb);
334 ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
335 EXT4_FEATURE_INCOMPAT_FLEX_BG);
337 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
339 struct ext4_block block_bitmap;
341 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
345 memset(block_bitmap.data, 0, block_size);
347 bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
348 if (!ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
349 EXT4_FEATURE_INCOMPAT_META_BG) ||
350 bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
353 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
356 ext4_get16(&bg_ref->fs->sb,
357 s_reserved_gdt_blocks);
359 } else { /* For META_BG_BLOCK_GROUPS */
360 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
363 for (bit = 0; bit < bit_max; bit++)
364 ext4_bmap_bit_set(block_bitmap.data, bit);
366 if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
368 * Even though mke2fs always initialize first and last group
369 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
370 * to make sure we calculate the right free blocks
372 group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
373 ext4_get32(&bg_ref->fs->sb, first_data_block) -
374 (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
375 (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
377 group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
380 ext4_block_in_group(&bg_ref->fs->sb,
381 bitmap_block_addr, bg_ref->index))
382 ext4_bmap_bit_set(block_bitmap.data,
383 bitmap_block_addr - first_group_addr);
386 ext4_block_in_group(&bg_ref->fs->sb,
387 bitmap_inode_addr, bg_ref->index))
388 ext4_bmap_bit_set(block_bitmap.data,
389 bitmap_inode_addr - first_group_addr);
391 for (i = inode_table_addr;
392 i < inode_table_addr + inode_table_bcnt; i++) {
394 ext4_block_in_group(&bg_ref->fs->sb,
397 ext4_bmap_bit_set(block_bitmap.data,
398 i - first_group_addr);
401 * Also if the number of blocks within the group is
402 * less than the blocksize * 8 ( which is the size
403 * of bitmap ), set rest of the block bitmap to 1
405 ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
406 block_bitmap.dirty = true;
409 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
412 /**@brief Initialize i-node bitmap in block group.
413 * @param bg_ref Reference to block group
416 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
419 uint32_t bitmap_block_addr =
420 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
422 struct ext4_block block_bitmap;
424 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
428 /* Initialize all bitmap bits to zero */
429 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
430 uint32_t inodes_per_group =
431 ext4_get32(&bg_ref->fs->sb, inodes_per_group);
433 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
435 uint32_t start_bit = inodes_per_group;
436 uint32_t end_bit = block_size * 8;
439 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
440 ext4_bmap_bit_set(block_bitmap.data, i);
443 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
445 block_bitmap.dirty = true;
448 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
451 /**@brief Initialize i-node table in block group.
452 * @param bg_ref Reference to block group
455 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
457 struct ext4_sblock *sb = &bg_ref->fs->sb;
459 uint32_t inode_size = ext4_get32(sb, inode_size);
460 uint32_t block_size = ext4_sb_get_block_size(sb);
461 uint32_t inodes_per_block = block_size / inode_size;
462 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
463 uint32_t table_blocks = inodes_in_group / inodes_per_block;
466 if (inodes_in_group % inodes_per_block)
469 /* Compute initialization bounds */
470 uint32_t first_block =
471 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
473 uint32_t last_block = first_block + table_blocks - 1;
475 /* Initialization of all itable blocks */
476 for (fblock = first_block; fblock <= last_block; ++fblock) {
478 struct ext4_block block;
479 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
483 memset(block.data, 0, block_size);
486 ext4_block_set(bg_ref->fs->bdev, &block);
494 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
496 uint32_t dsc_per_block)
498 uint32_t first_meta_bg, dsc_id;
502 dsc_id = bgid / dsc_per_block;
503 first_meta_bg = ext4_sb_first_meta_bg(s);
505 if (!ext4_sb_has_feature_incompatible(s,
506 EXT4_FEATURE_INCOMPAT_META_BG) ||
507 dsc_id < first_meta_bg)
508 return ext4_get32(s, first_data_block) + dsc_id + 1;
510 if (ext4_sb_is_super_in_bg(s, bgid))
513 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
516 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
517 struct ext4_block_group_ref *ref)
519 /* Compute number of descriptors, that fits in one data block */
520 uint32_t dsc_per_block =
521 ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
523 /* Block group descriptor table starts at the next block after
526 ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
529 (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
531 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
535 ref->block_group = (void *)(ref->block.data + offset);
540 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
541 rc = ext4_fs_init_block_bitmap(ref);
543 ext4_block_set(fs->bdev, &ref->block);
546 ext4_bg_clear_flag(ref->block_group,
547 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
552 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
553 rc = ext4_fs_init_inode_bitmap(ref);
555 ext4_block_set(ref->fs->bdev, &ref->block);
559 ext4_bg_clear_flag(ref->block_group,
560 EXT4_BLOCK_GROUP_INODE_UNINIT);
562 if (!ext4_bg_has_flag(ref->block_group,
563 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
564 rc = ext4_fs_init_inode_table(ref);
566 ext4_block_set(fs->bdev, &ref->block);
570 ext4_bg_set_flag(ref->block_group,
571 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
580 /**@brief Compute checksum of block group descriptor.
581 * @param sb Superblock
582 * @param bgid Index of block group in the filesystem
583 * @param bg Block group to compute checksum for
584 * @return Checksum value
586 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
587 struct ext4_bgroup *bg)
589 /* If checksum not supported, 0 will be returned */
592 /* Compute the checksum only if the filesystem supports it */
593 if (ext4_sb_has_feature_read_only(sb,
594 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
595 uint8_t *base = (uint8_t *)bg;
596 uint8_t *checksum = (uint8_t *)&bg->checksum;
598 uint32_t offset = (uint32_t)(checksum - base);
600 /* Convert block group index to little endian */
601 uint32_t le_group = to_le32(bgid);
604 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
606 /* Include index of block group */
608 ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
610 /* Compute crc from the first part (stop before checksum field)
612 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
615 offset += sizeof(bg->checksum);
617 /* Checksum of the rest of block group descriptor */
618 if ((ext4_sb_has_feature_incompatible(
619 sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
620 (offset < ext4_sb_get_desc_size(sb)))
622 crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
623 ext4_sb_get_desc_size(sb) - offset);
628 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
630 /* Check if reference modified */
632 /* Compute new checksum of block group */
633 uint16_t checksum = ext4_fs_bg_checksum(
634 &ref->fs->sb, ref->index, ref->block_group);
636 ref->block_group->checksum = to_le16(checksum);
638 /* Mark block dirty for writing changes to physical device */
639 ref->block.dirty = true;
642 /* Put back block, that contains block group descriptor */
643 return ext4_block_set(ref->fs->bdev, &ref->block);
646 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
647 struct ext4_inode_ref *ref)
649 /* Compute number of i-nodes, that fits in one data block */
650 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
653 * Inode numbers are 1-based, but it is simpler to work with 0-based
654 * when computing indices
657 uint32_t block_group = index / inodes_per_group;
658 uint32_t offset_in_group = index % inodes_per_group;
660 /* Load block group, where i-node is located */
661 struct ext4_block_group_ref bg_ref;
663 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
668 /* Load block address, where i-node table is located */
669 uint32_t inode_table_start =
670 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
672 /* Put back block group reference (not needed more) */
673 rc = ext4_fs_put_block_group_ref(&bg_ref);
678 /* Compute position of i-node in the block group */
679 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
680 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
681 uint32_t byte_offset_in_group = offset_in_group * inode_size;
683 /* Compute block address */
685 inode_table_start + (byte_offset_in_group / block_size);
687 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
692 /* Compute position of i-node in the data block */
693 uint32_t offset_in_block = byte_offset_in_group % block_size;
694 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
696 /* We need to store the original value of index in the reference */
697 ref->index = index + 1;
704 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
706 /* Check if reference modified */
708 /* Mark block dirty for writing changes to physical device */
709 ref->block.dirty = true;
712 /* Put back block, that contains i-node */
713 return ext4_block_set(ref->fs->bdev, &ref->block);
716 void ext4_fs_inode_blocks_init(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref)
719 struct ext4_inode *inode = inode_ref->inode;
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(&fs->sb,
727 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
728 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
730 /* Initialize extent root header */
731 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
732 ext4_extent_header_set_depth(header, 0);
733 ext4_extent_header_set_entries_count(header, 0);
734 ext4_extent_header_set_generation(header, 0);
735 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
737 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
738 sizeof(struct ext4_extent_header)) /
739 sizeof(struct ext4_extent);
741 ext4_extent_header_set_max_entries_count(header, max_entries);
746 static uint32_t ext4_fs_correspond_inode_mode(int filetype)
749 case EXT4_DIRENTRY_DIR:
750 return EXT4_INODE_MODE_DIRECTORY;
751 case EXT4_DIRENTRY_REG_FILE:
752 return EXT4_INODE_MODE_FILE;
753 case EXT4_DIRENTRY_SYMLINK:
754 return EXT4_INODE_MODE_SOFTLINK;
756 /* FIXME: right now we only support 3 file type. */
762 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
765 /* Check if newly allocated i-node will be a directory */
768 is_dir = (filetype == EXT4_DIRENTRY_DIR);
770 /* Allocate inode by allocation algorithm */
772 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
776 /* Load i-node from on-disk i-node table */
777 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
779 ext4_ialloc_free_inode(fs, index, is_dir);
783 /* Initialize i-node */
784 struct ext4_inode *inode = inode_ref->inode;
789 * Default directory permissions to be compatible with other
791 * 0777 (octal) == rwxrwxrwx
795 mode |= EXT4_INODE_MODE_DIRECTORY;
798 * Default file permissions to be compatible with other systems
799 * 0666 (octal) == rw-rw-rw-
803 mode |= ext4_fs_correspond_inode_mode(filetype);
805 ext4_inode_set_mode(&fs->sb, inode, mode);
807 ext4_inode_set_links_count(inode, 0);
808 ext4_inode_set_uid(inode, 0);
809 ext4_inode_set_gid(inode, 0);
810 ext4_inode_set_size(inode, 0);
811 ext4_inode_set_access_time(inode, 0);
812 ext4_inode_set_change_inode_time(inode, 0);
813 ext4_inode_set_modification_time(inode, 0);
814 ext4_inode_set_deletion_time(inode, 0);
815 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
816 ext4_inode_set_flags(inode, 0);
817 ext4_inode_set_generation(inode, 0);
819 /* Reset blocks array. For symbolic link inode, just
820 * fill in blocks with 0 */
821 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
822 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
823 inode->blocks[i] = 0;
826 ext4_fs_inode_blocks_init(fs, inode_ref);
828 inode_ref->dirty = true;
833 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
835 struct ext4_fs *fs = inode_ref->fs;
839 #if CONFIG_EXTENT_ENABLE
840 /* For extents must be data block destroyed by other way */
841 if ((ext4_sb_has_feature_incompatible(&fs->sb,
842 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
843 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
844 /* Data structures are released during truncate operation... */
848 /* Release all indirect (no data) blocks */
850 /* 1) Single indirect */
851 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
853 int rc = ext4_balloc_free_block(inode_ref, fblock);
857 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
860 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
861 uint32_t count = block_size / sizeof(uint32_t);
863 struct ext4_block block;
865 /* 2) Double indirect */
866 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
868 int rc = ext4_block_get(fs->bdev, &block, fblock);
873 for (offset = 0; offset < count; ++offset) {
874 ind_block = to_le32(((uint32_t *)block.data)[offset]);
878 rc = ext4_balloc_free_block(inode_ref, ind_block);
880 ext4_block_set(fs->bdev, &block);
886 ext4_block_set(fs->bdev, &block);
887 rc = ext4_balloc_free_block(inode_ref, fblock);
891 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
894 /* 3) Tripple indirect */
895 struct ext4_block subblock;
896 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
899 rc = ext4_block_get(fs->bdev, &block, fblock);
904 for (offset = 0; offset < count; ++offset) {
905 ind_block = to_le32(((uint32_t *)block.data)[offset]);
909 rc = ext4_block_get(fs->bdev, &subblock,
912 ext4_block_set(fs->bdev, &block);
917 for (suboff = 0; suboff < count; ++suboff) {
918 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
922 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
924 ext4_block_set(fs->bdev, &subblock);
925 ext4_block_set(fs->bdev, &block);
931 ext4_block_set(fs->bdev, &subblock);
933 rc = ext4_balloc_free_block(inode_ref,
936 ext4_block_set(fs->bdev, &block);
942 ext4_block_set(fs->bdev, &block);
943 rc = ext4_balloc_free_block(inode_ref, fblock);
947 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
949 /* Mark inode dirty for writing to the physical device */
950 inode_ref->dirty = true;
952 /* Free block with extended attributes if present */
953 uint32_t xattr_block =
954 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
956 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
960 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
963 /* Free inode by allocator */
964 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
965 EXT4_INODE_MODE_DIRECTORY))
966 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
968 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
973 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
975 struct ext4_sblock *sb = &inode_ref->fs->sb;
978 /* Check flags, if i-node can be truncated */
979 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
982 /* If sizes are equal, nothing has to be done. */
983 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
984 if (old_size == new_size)
987 /* It's not supported to make the larger file by truncate operation */
988 if (old_size < new_size)
991 if (ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK)
992 && old_size < sizeof(inode_ref->inode->blocks)
993 && !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
994 char *content = (char *)inode_ref->inode->blocks;
995 memset(content + new_size, 0,
996 sizeof(inode_ref->inode->blocks) - new_size);
997 ext4_inode_set_size(inode_ref->inode, new_size);
998 inode_ref->dirty = true;
1003 /* Compute how many blocks will be released */
1004 uint32_t block_size = ext4_sb_get_block_size(sb);
1005 uint32_t new_blocks_count = (new_size + block_size - 1) /
1007 uint32_t old_blocks_count = (old_size + block_size - 1) /
1009 uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
1010 #if CONFIG_EXTENT_ENABLE
1011 if ((ext4_sb_has_feature_incompatible(sb,
1012 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1013 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1015 /* Extents require special operation */
1016 if (diff_blocks_count) {
1017 int rc = ext4_extent_release_blocks_from(
1027 /* Release data blocks from the end of file */
1029 /* Starting from 1 because of logical blocks are numbered from 0
1031 for (i = 0; i < diff_blocks_count; ++i) {
1032 int rc = ext4_fs_release_inode_block(
1033 inode_ref, new_blocks_count + i);
1040 ext4_inode_set_size(inode_ref->inode, new_size);
1041 inode_ref->dirty = true;
1046 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1047 uint64_t iblock, uint32_t *fblock)
1049 struct ext4_fs *fs = inode_ref->fs;
1051 /* For empty file is situation simple */
1052 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1057 uint32_t current_block;
1058 #if CONFIG_EXTENT_ENABLE
1059 /* Handle i-node using extents */
1060 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1061 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1062 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1065 ext4_extent_find_block(inode_ref, iblock, ¤t_block);
1069 *fblock = current_block;
1074 struct ext4_inode *inode = inode_ref->inode;
1076 /* Direct block are read directly from array in i-node structure */
1077 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1079 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1080 *fblock = current_block;
1084 /* Determine indirection level of the target block */
1085 unsigned int level = 0;
1087 for (i = 1; i < 4; i++) {
1088 if (iblock < fs->inode_block_limits[i]) {
1097 /* Compute offsets for the topmost level */
1098 uint64_t block_offset_in_level =
1099 iblock - fs->inode_block_limits[level - 1];
1100 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1101 uint32_t offset_in_block =
1102 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1105 if (current_block == 0) {
1110 struct ext4_block block;
1113 * Navigate through other levels, until we find the block number
1114 * or find null reference meaning we are dealing with sparse file
1117 /* Load indirect block */
1118 int rc = ext4_block_get(fs->bdev, &block, current_block);
1122 /* Read block address from indirect block */
1124 to_le32(((uint32_t *)block.data)[offset_in_block]);
1126 /* Put back indirect block untouched */
1127 rc = ext4_block_set(fs->bdev, &block);
1131 /* Check for sparse file */
1132 if (current_block == 0) {
1137 /* Jump to the next level */
1140 /* Termination condition - we have address of data block loaded
1145 /* Visit the next level */
1146 block_offset_in_level %= fs->inode_blocks_per_level[level];
1147 offset_in_block = block_offset_in_level /
1148 fs->inode_blocks_per_level[level - 1];
1151 *fblock = current_block;
1156 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1157 uint64_t iblock, uint32_t fblock)
1159 struct ext4_fs *fs = inode_ref->fs;
1161 #if CONFIG_EXTENT_ENABLE
1162 /* Handle inode using extents */
1163 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1164 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1165 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1171 /* Handle simple case when we are dealing with direct reference */
1172 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1173 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1175 inode_ref->dirty = true;
1180 /* Determine the indirection level needed to get the desired block */
1181 unsigned int level = 0;
1183 for (i = 1; i < 4; i++) {
1184 if (iblock < fs->inode_block_limits[i]) {
1193 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1195 /* Compute offsets for the topmost level */
1196 uint64_t block_offset_in_level =
1197 iblock - fs->inode_block_limits[level - 1];
1198 uint32_t current_block =
1199 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1200 uint32_t offset_in_block =
1201 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1203 uint32_t new_block_addr;
1205 struct ext4_block block;
1206 struct ext4_block new_block;
1208 /* Is needed to allocate indirect block on the i-node level */
1209 if (current_block == 0) {
1210 /* Allocate new indirect block */
1211 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1216 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1218 inode_ref->dirty = true;
1220 /* Load newly allocated block */
1221 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1223 ext4_balloc_free_block(inode_ref, new_block_addr);
1227 /* Initialize new block */
1228 memset(new_block.data, 0, block_size);
1229 new_block.dirty = true;
1231 /* Put back the allocated block */
1232 rc = ext4_block_set(fs->bdev, &new_block);
1236 current_block = new_block_addr;
1240 * Navigate through other levels, until we find the block number
1241 * or find null reference meaning we are dealing with sparse file
1244 int rc = ext4_block_get(fs->bdev, &block, current_block);
1249 to_le32(((uint32_t *)block.data)[offset_in_block]);
1251 if ((level > 1) && (current_block == 0)) {
1252 /* Allocate new block */
1254 ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1256 ext4_block_set(fs->bdev, &block);
1260 /* Load newly allocated block */
1261 rc = ext4_block_get(fs->bdev, &new_block,
1265 ext4_block_set(fs->bdev, &block);
1269 /* Initialize allocated block */
1270 memset(new_block.data, 0, block_size);
1271 new_block.dirty = true;
1273 rc = ext4_block_set(fs->bdev, &new_block);
1275 ext4_block_set(fs->bdev, &block);
1279 /* Write block address to the parent */
1280 ((uint32_t *)block.data)[offset_in_block] =
1281 to_le32(new_block_addr);
1283 current_block = new_block_addr;
1286 /* Will be finished, write the fblock address */
1288 ((uint32_t *)block.data)[offset_in_block] =
1293 rc = ext4_block_set(fs->bdev, &block);
1300 * If we are on the last level, break here as
1301 * there is no next level to visit
1306 /* Visit the next level */
1307 block_offset_in_level %= fs->inode_blocks_per_level[level];
1308 offset_in_block = block_offset_in_level /
1309 fs->inode_blocks_per_level[level - 1];
1315 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1320 struct ext4_fs *fs = inode_ref->fs;
1322 /* Extents are handled otherwise = there is not support in this function
1325 ext4_sb_has_feature_incompatible(&fs->sb,
1326 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1327 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1329 struct ext4_inode *inode = inode_ref->inode;
1331 /* Handle simple case when we are dealing with direct reference */
1332 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1333 fblock = ext4_inode_get_direct_block(inode, iblock);
1339 ext4_inode_set_direct_block(inode, iblock, 0);
1340 return ext4_balloc_free_block(inode_ref, fblock);
1343 /* Determine the indirection level needed to get the desired block */
1344 unsigned int level = 0;
1346 for (i = 1; i < 4; i++) {
1347 if (iblock < fs->inode_block_limits[i]) {
1356 /* Compute offsets for the topmost level */
1357 uint64_t block_offset_in_level =
1358 iblock - fs->inode_block_limits[level - 1];
1359 uint32_t current_block =
1360 ext4_inode_get_indirect_block(inode, level - 1);
1361 uint32_t offset_in_block =
1362 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1365 * Navigate through other levels, until we find the block number
1366 * or find null reference meaning we are dealing with sparse file
1368 struct ext4_block block;
1373 if (current_block == 0)
1376 int rc = ext4_block_get(fs->bdev, &block, current_block);
1381 to_le32(((uint32_t *)block.data)[offset_in_block]);
1383 /* Set zero if physical data block address found */
1385 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1389 rc = ext4_block_set(fs->bdev, &block);
1396 * If we are on the last level, break here as
1397 * there is no next level to visit
1402 /* Visit the next level */
1403 block_offset_in_level %= fs->inode_blocks_per_level[level];
1404 offset_in_block = block_offset_in_level /
1405 fs->inode_blocks_per_level[level - 1];
1408 fblock = current_block;
1412 /* Physical block is not referenced, it can be released */
1413 return ext4_balloc_free_block(inode_ref, fblock);
1416 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1417 uint32_t *fblock, uint32_t *iblock)
1419 #if CONFIG_EXTENT_ENABLE
1420 /* Handle extents separately */
1421 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1422 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1423 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1424 return ext4_extent_append_block(inode_ref, iblock, fblock,
1428 struct ext4_sblock *sb = &inode_ref->fs->sb;
1430 /* Compute next block index and allocate data block */
1431 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1432 uint32_t block_size = ext4_sb_get_block_size(sb);
1434 /* Align size i-node size */
1435 if ((inode_size % block_size) != 0)
1436 inode_size += block_size - (inode_size % block_size);
1438 /* Logical blocks are numbered from 0 */
1439 uint32_t new_block_idx = inode_size / block_size;
1441 /* Allocate new physical block */
1442 uint32_t phys_block;
1443 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1447 /* Add physical block address to the i-node */
1448 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1451 ext4_balloc_free_block(inode_ref, phys_block);
1456 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1457 inode_ref->dirty = true;
1459 *fblock = phys_block;
1460 *iblock = new_block_idx;
1465 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1469 link = ext4_inode_get_links_count(inode_ref->inode);
1471 ext4_inode_set_links_count(inode_ref->inode, link);
1474 ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1475 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1476 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1478 if (is_dx && link > 1) {
1479 if (link >= EXT4_LINK_MAX || link == 2) {
1480 ext4_inode_set_links_count(inode_ref->inode, 1);
1483 ext4_get32(&inode_ref->fs->sb, features_read_only);
1484 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1485 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1490 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1492 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1493 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1494 EXT4_INODE_MODE_DIRECTORY)) {
1496 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1501 ext4_inode_set_links_count(inode_ref->inode, links - 1);