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"
54 #include "ext4_extent_full.h"
58 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
63 bool read_only = false;
65 ext4_assert(fs && bdev);
69 r = ext4_sb_read(fs->bdev, &fs->sb);
73 if (!ext4_sb_check(&fs->sb))
76 bsize = ext4_sb_get_block_size(&fs->sb);
77 if (bsize > EXT4_MAX_BLOCK_SIZE)
80 r = ext4_fs_check_features(fs, &read_only);
87 /* Compute limits for indirect block levels */
88 uint32_t blocks_id = bsize / sizeof(uint32_t);
90 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
91 fs->inode_blocks_per_level[0] = 1;
93 for (i = 1; i < 4; i++) {
94 fs->inode_blocks_per_level[i] =
95 fs->inode_blocks_per_level[i - 1] * blocks_id;
96 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
97 fs->inode_blocks_per_level[i];
101 tmp = ext4_get16(&fs->sb, state);
102 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
103 ext4_dbg(DEBUG_FS, DBG_WARN
104 "last umount error: superblock fs_error flag\n");
107 /* Mark system as mounted */
108 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
109 r = ext4_sb_write(fs->bdev, &fs->sb);
113 /*Update mount count*/
114 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_inc(uint32_t features_incompatible)
131 if (features_incompatible & EXT4_FEATURE_INCOMPAT_COMPRESSION)
132 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
133 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE)
134 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
135 if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER)
136 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
137 if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)
138 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
139 if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG)
140 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
141 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS)
142 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
143 if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT)
144 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
145 if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP)
146 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
147 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG)
148 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
149 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE)
150 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
151 if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA)
152 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
153 if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM)
154 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
155 if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR)
156 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
157 if (features_incompatible & EXT4_FEATURE_INCOMPAT_INLINE_DATA)
158 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
160 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
162 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_PREALLOC)
163 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
164 if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES)
165 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
166 if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
167 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
168 if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR)
169 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
170 if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE)
171 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
172 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_INDEX)
173 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
176 static void ext4_fs_debug_features_ro(uint32_t features_ro)
178 if (features_ro & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)
179 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
180 if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE)
181 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
182 if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
183 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
184 if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
185 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
186 if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
187 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
188 if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
189 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
190 if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
191 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
192 if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA)
193 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
194 if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC)
195 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
196 if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
197 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
200 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
202 ext4_assert(fs && read_only);
204 if (ext4_get32(&fs->sb, rev_level) == 0) {
209 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
210 ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
212 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
213 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
215 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
216 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
218 /*Check features_incompatible*/
219 v = (ext4_get32(&fs->sb, features_incompatible) &
220 (~CONFIG_FEATURE_INCOMPAT_SUPP));
222 ext4_dbg(DEBUG_FS, DBG_ERROR
223 "sblock has unsupported features incompatible:\n");
224 ext4_fs_debug_features_inc(v);
228 /*Check features_read_only*/
229 v = (ext4_get32(&fs->sb, features_read_only) &
230 (~CONFIG_FEATURE_RO_COMPAT_SUPP));
232 ext4_dbg(DEBUG_FS, DBG_WARN
233 "sblock has unsupported features read only:\n");
234 ext4_fs_debug_features_ro(v);
243 /**@brief Determine whether the block is inside the group.
244 * @param baddr block address
245 * @param bgid block group id
248 static int ext4_block_in_group(struct ext4_sblock *s,
252 uint32_t actual_bgid;
253 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
254 if (actual_bgid == bgid)
259 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
260 * need to use it within a single byte (to ensure we get endianness right).
261 * We can use memset for the rest of the bitmap as there are no other users.
263 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
267 if (start_bit >= end_bit)
270 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
271 ext4_bmap_bit_set(bitmap, i);
274 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
277 /**@brief Initialize block bitmap in block group.
278 * @param bg_ref Reference to block group
281 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
283 uint32_t i, bit, bit_max;
284 uint32_t group_blocks;
285 uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
286 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
287 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
288 uint32_t bitmap_block_addr =
289 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
290 uint32_t bitmap_inode_addr =
291 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
292 uint32_t inode_table_addr =
293 ext4_bg_get_inode_table_first_block(bg_ref->block_group,
295 uint32_t first_group_addr =
296 ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
298 uint32_t dsc_per_block =
299 ext4_sb_get_block_size(&bg_ref->fs->sb) /
300 ext4_sb_get_desc_size(&bg_ref->fs->sb);
303 ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
304 EXT4_FEATURE_INCOMPAT_FLEX_BG);
306 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
308 struct ext4_block block_bitmap;
310 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
314 memset(block_bitmap.data, 0, block_size);
316 bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
317 if (!ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
318 EXT4_FEATURE_INCOMPAT_META_BG) ||
319 bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
322 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
325 ext4_get16(&bg_ref->fs->sb,
326 s_reserved_gdt_blocks);
328 } else { /* For META_BG_BLOCK_GROUPS */
329 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
332 for (bit = 0; bit < bit_max; bit++)
333 ext4_bmap_bit_set(block_bitmap.data, bit);
335 if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
337 * Even though mke2fs always initialize first and last group
338 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
339 * to make sure we calculate the right free blocks
341 group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
342 ext4_get32(&bg_ref->fs->sb, first_data_block) -
343 (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
344 (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
346 group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
349 ext4_block_in_group(&bg_ref->fs->sb,
350 bitmap_block_addr, bg_ref->index))
351 ext4_bmap_bit_set(block_bitmap.data,
352 bitmap_block_addr - first_group_addr);
355 ext4_block_in_group(&bg_ref->fs->sb,
356 bitmap_inode_addr, bg_ref->index))
357 ext4_bmap_bit_set(block_bitmap.data,
358 bitmap_inode_addr - first_group_addr);
360 for (i = inode_table_addr;
361 i < inode_table_addr + inode_table_bcnt; i++) {
363 ext4_block_in_group(&bg_ref->fs->sb,
366 ext4_bmap_bit_set(block_bitmap.data,
367 i - first_group_addr);
370 * Also if the number of blocks within the group is
371 * less than the blocksize * 8 ( which is the size
372 * of bitmap ), set rest of the block bitmap to 1
374 ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
375 block_bitmap.dirty = true;
378 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
381 /**@brief Initialize i-node bitmap in block group.
382 * @param bg_ref Reference to block group
385 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
388 uint32_t bitmap_block_addr =
389 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
391 struct ext4_block block_bitmap;
393 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
397 /* Initialize all bitmap bits to zero */
398 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
399 uint32_t inodes_per_group =
400 ext4_get32(&bg_ref->fs->sb, inodes_per_group);
402 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
404 uint32_t start_bit = inodes_per_group;
405 uint32_t end_bit = block_size * 8;
408 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
409 ext4_bmap_bit_set(block_bitmap.data, i);
412 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
414 block_bitmap.dirty = true;
417 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
420 /**@brief Initialize i-node table in block group.
421 * @param bg_ref Reference to block group
424 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
426 struct ext4_sblock *sb = &bg_ref->fs->sb;
428 uint32_t inode_size = ext4_get32(sb, inode_size);
429 uint32_t block_size = ext4_sb_get_block_size(sb);
430 uint32_t inodes_per_block = block_size / inode_size;
431 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
432 uint32_t table_blocks = inodes_in_group / inodes_per_block;
435 if (inodes_in_group % inodes_per_block)
438 /* Compute initialization bounds */
439 uint32_t first_block =
440 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
442 uint32_t last_block = first_block + table_blocks - 1;
444 /* Initialization of all itable blocks */
445 for (fblock = first_block; fblock <= last_block; ++fblock) {
447 struct ext4_block block;
448 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
452 memset(block.data, 0, block_size);
455 ext4_block_set(bg_ref->fs->bdev, &block);
463 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
465 uint32_t dsc_per_block)
467 uint32_t first_meta_bg, dsc_id;
471 dsc_id = bgid / dsc_per_block;
472 first_meta_bg = ext4_sb_first_meta_bg(s);
474 if (!ext4_sb_has_feature_incompatible(s,
475 EXT4_FEATURE_INCOMPAT_META_BG) ||
476 dsc_id < first_meta_bg)
477 return ext4_get32(s, first_data_block) + dsc_id + 1;
479 if (ext4_sb_is_super_in_bg(s, bgid))
482 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
485 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
486 struct ext4_block_group_ref *ref)
488 /* Compute number of descriptors, that fits in one data block */
489 uint32_t dsc_per_block =
490 ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
492 /* Block group descriptor table starts at the next block after
495 ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
498 (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
500 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
504 ref->block_group = (void *)(ref->block.data + offset);
509 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
510 rc = ext4_fs_init_block_bitmap(ref);
512 ext4_block_set(fs->bdev, &ref->block);
515 ext4_bg_clear_flag(ref->block_group,
516 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
521 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
522 rc = ext4_fs_init_inode_bitmap(ref);
524 ext4_block_set(ref->fs->bdev, &ref->block);
528 ext4_bg_clear_flag(ref->block_group,
529 EXT4_BLOCK_GROUP_INODE_UNINIT);
531 if (!ext4_bg_has_flag(ref->block_group,
532 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
533 rc = ext4_fs_init_inode_table(ref);
535 ext4_block_set(fs->bdev, &ref->block);
539 ext4_bg_set_flag(ref->block_group,
540 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
549 /**@brief Compute checksum of block group descriptor.
550 * @param sb Superblock
551 * @param bgid Index of block group in the filesystem
552 * @param bg Block group to compute checksum for
553 * @return Checksum value
555 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
556 struct ext4_bgroup *bg)
558 /* If checksum not supported, 0 will be returned */
561 /* Compute the checksum only if the filesystem supports it */
562 if (ext4_sb_has_feature_read_only(sb,
563 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
564 uint8_t *base = (uint8_t *)bg;
565 uint8_t *checksum = (uint8_t *)&bg->checksum;
567 uint32_t offset = (uint32_t)(checksum - base);
569 /* Convert block group index to little endian */
570 uint32_t le_group = to_le32(bgid);
573 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
575 /* Include index of block group */
577 ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
579 /* Compute crc from the first part (stop before checksum field)
581 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
584 offset += sizeof(bg->checksum);
586 /* Checksum of the rest of block group descriptor */
587 if ((ext4_sb_has_feature_incompatible(
588 sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
589 (offset < ext4_sb_get_desc_size(sb)))
591 crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
592 ext4_sb_get_desc_size(sb) - offset);
597 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
599 /* Check if reference modified */
601 /* Compute new checksum of block group */
602 uint16_t checksum = ext4_fs_bg_checksum(
603 &ref->fs->sb, ref->index, ref->block_group);
605 ref->block_group->checksum = to_le16(checksum);
607 /* Mark block dirty for writing changes to physical device */
608 ref->block.dirty = true;
611 /* Put back block, that contains block group descriptor */
612 return ext4_block_set(ref->fs->bdev, &ref->block);
615 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
616 struct ext4_inode_ref *ref)
618 /* Compute number of i-nodes, that fits in one data block */
619 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
622 * Inode numbers are 1-based, but it is simpler to work with 0-based
623 * when computing indices
626 uint32_t block_group = index / inodes_per_group;
627 uint32_t offset_in_group = index % inodes_per_group;
629 /* Load block group, where i-node is located */
630 struct ext4_block_group_ref bg_ref;
632 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
637 /* Load block address, where i-node table is located */
638 uint32_t inode_table_start =
639 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
641 /* Put back block group reference (not needed more) */
642 rc = ext4_fs_put_block_group_ref(&bg_ref);
647 /* Compute position of i-node in the block group */
648 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
649 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
650 uint32_t byte_offset_in_group = offset_in_group * inode_size;
652 /* Compute block address */
654 inode_table_start + (byte_offset_in_group / block_size);
656 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
661 /* Compute position of i-node in the data block */
662 uint32_t offset_in_block = byte_offset_in_group % block_size;
663 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
665 /* We need to store the original value of index in the reference */
666 ref->index = index + 1;
673 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
675 /* Check if reference modified */
677 /* Mark block dirty for writing changes to physical device */
678 ref->block.dirty = true;
681 /* Put back block, that contains i-node */
682 return ext4_block_set(ref->fs->bdev, &ref->block);
685 void ext4_fs_inode_blocks_init(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref)
688 struct ext4_inode *inode = inode_ref->inode;
690 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
691 inode->blocks[i] = 0;
694 #if CONFIG_EXTENT_ENABLE
695 /* Initialize extents if needed */
696 if (ext4_sb_has_feature_incompatible(&fs->sb,
697 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
698 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
700 /* Initialize extent root header */
701 ext4_ext_tree_init(inode_ref);
706 static uint32_t ext4_fs_correspond_inode_mode(int filetype)
709 case EXT4_DIRENTRY_DIR:
710 return EXT4_INODE_MODE_DIRECTORY;
711 case EXT4_DIRENTRY_REG_FILE:
712 return EXT4_INODE_MODE_FILE;
713 case EXT4_DIRENTRY_SYMLINK:
714 return EXT4_INODE_MODE_SOFTLINK;
716 /* FIXME: right now we only support 3 file type. */
722 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
725 /* Check if newly allocated i-node will be a directory */
728 is_dir = (filetype == EXT4_DIRENTRY_DIR);
730 /* Allocate inode by allocation algorithm */
732 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
736 /* Load i-node from on-disk i-node table */
737 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
739 ext4_ialloc_free_inode(fs, index, is_dir);
743 /* Initialize i-node */
744 struct ext4_inode *inode = inode_ref->inode;
749 * Default directory permissions to be compatible with other
751 * 0777 (octal) == rwxrwxrwx
755 mode |= EXT4_INODE_MODE_DIRECTORY;
758 * Default file permissions to be compatible with other systems
759 * 0666 (octal) == rw-rw-rw-
763 mode |= ext4_fs_correspond_inode_mode(filetype);
765 ext4_inode_set_mode(&fs->sb, inode, mode);
767 ext4_inode_set_links_count(inode, 0);
768 ext4_inode_set_uid(inode, 0);
769 ext4_inode_set_gid(inode, 0);
770 ext4_inode_set_size(inode, 0);
771 ext4_inode_set_access_time(inode, 0);
772 ext4_inode_set_change_inode_time(inode, 0);
773 ext4_inode_set_modification_time(inode, 0);
774 ext4_inode_set_deletion_time(inode, 0);
775 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
776 ext4_inode_set_flags(inode, 0);
777 ext4_inode_set_generation(inode, 0);
779 /* Reset blocks array. For symbolic link inode, just
780 * fill in blocks with 0 */
781 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
782 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
783 inode->blocks[i] = 0;
786 ext4_fs_inode_blocks_init(fs, inode_ref);
788 inode_ref->dirty = true;
793 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
795 struct ext4_fs *fs = inode_ref->fs;
799 #if CONFIG_EXTENT_ENABLE
800 /* For extents must be data block destroyed by other way */
801 if ((ext4_sb_has_feature_incompatible(&fs->sb,
802 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
803 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
804 /* Data structures are released during truncate operation... */
808 /* Release all indirect (no data) blocks */
810 /* 1) Single indirect */
811 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
813 int rc = ext4_balloc_free_block(inode_ref, fblock);
817 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
820 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
821 uint32_t count = block_size / sizeof(uint32_t);
823 struct ext4_block block;
825 /* 2) Double indirect */
826 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
828 int rc = ext4_block_get(fs->bdev, &block, fblock);
833 for (offset = 0; offset < count; ++offset) {
834 ind_block = to_le32(((uint32_t *)block.data)[offset]);
838 rc = ext4_balloc_free_block(inode_ref, ind_block);
840 ext4_block_set(fs->bdev, &block);
846 ext4_block_set(fs->bdev, &block);
847 rc = ext4_balloc_free_block(inode_ref, fblock);
851 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
854 /* 3) Tripple indirect */
855 struct ext4_block subblock;
856 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
859 rc = ext4_block_get(fs->bdev, &block, fblock);
864 for (offset = 0; offset < count; ++offset) {
865 ind_block = to_le32(((uint32_t *)block.data)[offset]);
869 rc = ext4_block_get(fs->bdev, &subblock,
872 ext4_block_set(fs->bdev, &block);
877 for (suboff = 0; suboff < count; ++suboff) {
878 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
882 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
884 ext4_block_set(fs->bdev, &subblock);
885 ext4_block_set(fs->bdev, &block);
891 ext4_block_set(fs->bdev, &subblock);
893 rc = ext4_balloc_free_block(inode_ref,
896 ext4_block_set(fs->bdev, &block);
902 ext4_block_set(fs->bdev, &block);
903 rc = ext4_balloc_free_block(inode_ref, fblock);
907 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
909 /* Mark inode dirty for writing to the physical device */
910 inode_ref->dirty = true;
912 /* Free block with extended attributes if present */
913 uint32_t xattr_block =
914 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
916 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
920 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
923 /* Free inode by allocator */
924 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
925 EXT4_INODE_MODE_DIRECTORY))
926 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
928 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
933 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
935 struct ext4_sblock *sb = &inode_ref->fs->sb;
938 /* Check flags, if i-node can be truncated */
939 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
942 /* If sizes are equal, nothing has to be done. */
943 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
944 if (old_size == new_size)
947 /* It's not supported to make the larger file by truncate operation */
948 if (old_size < new_size)
951 if (ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK)
952 && old_size < sizeof(inode_ref->inode->blocks)
953 && !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
954 char *content = (char *)inode_ref->inode->blocks;
955 memset(content + new_size, 0,
956 sizeof(inode_ref->inode->blocks) - new_size);
957 ext4_inode_set_size(inode_ref->inode, new_size);
958 inode_ref->dirty = true;
963 /* Compute how many blocks will be released */
964 uint32_t block_size = ext4_sb_get_block_size(sb);
965 uint32_t new_blocks_count = (new_size + block_size - 1) /
967 uint32_t old_blocks_count = (old_size + block_size - 1) /
969 uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
970 #if CONFIG_EXTENT_ENABLE
971 if ((ext4_sb_has_feature_incompatible(sb,
972 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
973 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
975 /* Extents require special operation */
976 if (diff_blocks_count) {
977 int rc = ext4_ext_remove_space(
988 /* Release data blocks from the end of file */
990 /* Starting from 1 because of logical blocks are numbered from 0
992 for (i = 0; i < diff_blocks_count; ++i) {
993 int rc = ext4_fs_release_inode_block(
994 inode_ref, new_blocks_count + i);
1001 ext4_inode_set_size(inode_ref->inode, new_size);
1002 inode_ref->dirty = true;
1007 static int ext4_fs_get_inode_data_block_idx(struct ext4_inode_ref *inode_ref,
1008 uint64_t iblock, uint32_t *fblock,
1011 struct ext4_fs *fs = inode_ref->fs;
1013 /* For empty file is situation simple */
1014 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1019 uint32_t current_block;
1021 (void)extent_create;
1022 #if CONFIG_EXTENT_ENABLE
1023 /* Handle i-node using extents */
1024 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1025 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1026 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1028 ext4_fsblk_t current_fsblk;
1029 int rc = ext4_ext_get_blocks(inode_ref, iblock, 1, ¤t_fsblk,
1030 extent_create, NULL);
1034 current_block = (uint32_t)current_fsblk;
1035 *fblock = current_block;
1037 ext4_assert(*fblock);
1042 struct ext4_inode *inode = inode_ref->inode;
1044 /* Direct block are read directly from array in i-node structure */
1045 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1047 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1048 *fblock = current_block;
1052 /* Determine indirection level of the target block */
1053 unsigned int level = 0;
1055 for (i = 1; i < 4; i++) {
1056 if (iblock < fs->inode_block_limits[i]) {
1065 /* Compute offsets for the topmost level */
1066 uint64_t block_offset_in_level =
1067 iblock - fs->inode_block_limits[level - 1];
1068 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1069 uint32_t offset_in_block =
1070 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1073 if (current_block == 0) {
1078 struct ext4_block block;
1081 * Navigate through other levels, until we find the block number
1082 * or find null reference meaning we are dealing with sparse file
1085 /* Load indirect block */
1086 int rc = ext4_block_get(fs->bdev, &block, current_block);
1090 /* Read block address from indirect block */
1092 to_le32(((uint32_t *)block.data)[offset_in_block]);
1094 /* Put back indirect block untouched */
1095 rc = ext4_block_set(fs->bdev, &block);
1099 /* Check for sparse file */
1100 if (current_block == 0) {
1105 /* Jump to the next level */
1108 /* Termination condition - we have address of data block loaded
1113 /* Visit the next level */
1114 block_offset_in_level %= fs->inode_blocks_per_level[level];
1115 offset_in_block = block_offset_in_level /
1116 fs->inode_blocks_per_level[level - 1];
1119 *fblock = current_block;
1125 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1126 uint64_t iblock, uint32_t *fblock)
1128 return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1132 int ext4_fs_init_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1133 uint64_t iblock, uint32_t *fblock)
1135 return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1139 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1140 uint64_t iblock, uint32_t fblock)
1142 struct ext4_fs *fs = inode_ref->fs;
1144 #if CONFIG_EXTENT_ENABLE
1145 /* Handle inode using extents */
1146 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1147 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1148 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1154 /* Handle simple case when we are dealing with direct reference */
1155 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1156 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1158 inode_ref->dirty = true;
1163 /* Determine the indirection level needed to get the desired block */
1164 unsigned int level = 0;
1166 for (i = 1; i < 4; i++) {
1167 if (iblock < fs->inode_block_limits[i]) {
1176 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1178 /* Compute offsets for the topmost level */
1179 uint64_t block_offset_in_level =
1180 iblock - fs->inode_block_limits[level - 1];
1181 uint32_t current_block =
1182 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1183 uint32_t offset_in_block =
1184 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1186 uint32_t new_block_addr;
1188 struct ext4_block block;
1189 struct ext4_block new_block;
1191 /* Is needed to allocate indirect block on the i-node level */
1192 if (current_block == 0) {
1193 /* Allocate new indirect block */
1194 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1199 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1201 inode_ref->dirty = true;
1203 /* Load newly allocated block */
1204 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1206 ext4_balloc_free_block(inode_ref, new_block_addr);
1210 /* Initialize new block */
1211 memset(new_block.data, 0, block_size);
1212 new_block.dirty = true;
1214 /* Put back the allocated block */
1215 rc = ext4_block_set(fs->bdev, &new_block);
1219 current_block = new_block_addr;
1223 * Navigate through other levels, until we find the block number
1224 * or find null reference meaning we are dealing with sparse file
1227 int rc = ext4_block_get(fs->bdev, &block, current_block);
1232 to_le32(((uint32_t *)block.data)[offset_in_block]);
1234 if ((level > 1) && (current_block == 0)) {
1235 /* Allocate new block */
1237 ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1239 ext4_block_set(fs->bdev, &block);
1243 /* Load newly allocated block */
1244 rc = ext4_block_get(fs->bdev, &new_block,
1248 ext4_block_set(fs->bdev, &block);
1252 /* Initialize allocated block */
1253 memset(new_block.data, 0, block_size);
1254 new_block.dirty = true;
1256 rc = ext4_block_set(fs->bdev, &new_block);
1258 ext4_block_set(fs->bdev, &block);
1262 /* Write block address to the parent */
1263 ((uint32_t *)block.data)[offset_in_block] =
1264 to_le32(new_block_addr);
1266 current_block = new_block_addr;
1269 /* Will be finished, write the fblock address */
1271 ((uint32_t *)block.data)[offset_in_block] =
1276 rc = ext4_block_set(fs->bdev, &block);
1283 * If we are on the last level, break here as
1284 * there is no next level to visit
1289 /* Visit the next level */
1290 block_offset_in_level %= fs->inode_blocks_per_level[level];
1291 offset_in_block = block_offset_in_level /
1292 fs->inode_blocks_per_level[level - 1];
1298 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1303 struct ext4_fs *fs = inode_ref->fs;
1305 /* Extents are handled otherwise = there is not support in this function
1308 ext4_sb_has_feature_incompatible(&fs->sb,
1309 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1310 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1312 struct ext4_inode *inode = inode_ref->inode;
1314 /* Handle simple case when we are dealing with direct reference */
1315 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1316 fblock = ext4_inode_get_direct_block(inode, iblock);
1322 ext4_inode_set_direct_block(inode, iblock, 0);
1323 return ext4_balloc_free_block(inode_ref, fblock);
1326 /* Determine the indirection level needed to get the desired block */
1327 unsigned int level = 0;
1329 for (i = 1; i < 4; i++) {
1330 if (iblock < fs->inode_block_limits[i]) {
1339 /* Compute offsets for the topmost level */
1340 uint64_t block_offset_in_level =
1341 iblock - fs->inode_block_limits[level - 1];
1342 uint32_t current_block =
1343 ext4_inode_get_indirect_block(inode, level - 1);
1344 uint32_t offset_in_block =
1345 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1348 * Navigate through other levels, until we find the block number
1349 * or find null reference meaning we are dealing with sparse file
1351 struct ext4_block block;
1356 if (current_block == 0)
1359 int rc = ext4_block_get(fs->bdev, &block, current_block);
1364 to_le32(((uint32_t *)block.data)[offset_in_block]);
1366 /* Set zero if physical data block address found */
1368 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1372 rc = ext4_block_set(fs->bdev, &block);
1379 * If we are on the last level, break here as
1380 * there is no next level to visit
1385 /* Visit the next level */
1386 block_offset_in_level %= fs->inode_blocks_per_level[level];
1387 offset_in_block = block_offset_in_level /
1388 fs->inode_blocks_per_level[level - 1];
1391 fblock = current_block;
1395 /* Physical block is not referenced, it can be released */
1396 return ext4_balloc_free_block(inode_ref, fblock);
1399 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1400 uint32_t *fblock, uint32_t *iblock)
1402 #if CONFIG_EXTENT_ENABLE
1403 /* Handle extents separately */
1404 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1405 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1406 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1408 ext4_fsblk_t current_fsblk;
1409 struct ext4_sblock *sb = &inode_ref->fs->sb;
1410 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1411 uint32_t block_size = ext4_sb_get_block_size(sb);
1412 *iblock = (inode_size + block_size - 1) /
1415 rc = ext4_ext_get_blocks(inode_ref, *iblock, 1, ¤t_fsblk,
1419 *fblock = current_fsblk;
1420 ext4_assert(*fblock);
1422 ext4_inode_set_size(inode_ref->inode,
1423 inode_size + block_size);
1424 inode_ref->dirty = true;
1430 struct ext4_sblock *sb = &inode_ref->fs->sb;
1432 /* Compute next block index and allocate data block */
1433 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1434 uint32_t block_size = ext4_sb_get_block_size(sb);
1436 /* Align size i-node size */
1437 if ((inode_size % block_size) != 0)
1438 inode_size += block_size - (inode_size % block_size);
1440 /* Logical blocks are numbered from 0 */
1441 uint32_t new_block_idx = inode_size / block_size;
1443 /* Allocate new physical block */
1444 uint32_t phys_block;
1445 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1449 /* Add physical block address to the i-node */
1450 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1453 ext4_balloc_free_block(inode_ref, phys_block);
1458 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1459 inode_ref->dirty = true;
1461 *fblock = phys_block;
1462 *iblock = new_block_idx;
1467 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1471 link = ext4_inode_get_links_count(inode_ref->inode);
1473 ext4_inode_set_links_count(inode_ref->inode, link);
1476 ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1477 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1478 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1480 if (is_dx && link > 1) {
1481 if (link >= EXT4_LINK_MAX || link == 2) {
1482 ext4_inode_set_links_count(inode_ref->inode, 1);
1485 ext4_get32(&inode_ref->fs->sb, features_read_only);
1486 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1487 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1492 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1494 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1495 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1496 EXT4_INODE_MODE_DIRECTORY)) {
1498 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1503 ext4_inode_set_links_count(inode_ref->inode, links - 1);