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_dbg(DEBUG_FS, DBG_WARN
103 "last umount error: superblock fs_error flag\n");
106 /* Mark system as mounted */
107 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
108 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);
118 int ext4_fs_fini(struct ext4_fs *fs)
122 /*Set superblock state*/
123 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
125 return ext4_sb_write(fs->bdev, &fs->sb);
128 static void ext4_fs_debug_features_inc(uint32_t features_incompatible)
130 if (features_incompatible & EXT4_FEATURE_INCOMPAT_COMPRESSION)
131 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
132 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE)
133 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
134 if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER)
135 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
136 if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)
137 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
138 if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG)
139 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
140 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS)
141 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
142 if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT)
143 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
144 if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP)
145 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
146 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG)
147 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
148 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE)
149 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
150 if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA)
151 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
152 if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM)
153 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
154 if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR)
155 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
156 if (features_incompatible & EXT4_FEATURE_INCOMPAT_INLINE_DATA)
157 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
159 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
161 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_PREALLOC)
162 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
163 if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES)
164 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
165 if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
166 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
167 if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR)
168 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
169 if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE)
170 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
171 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_INDEX)
172 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
175 static void ext4_fs_debug_features_ro(uint32_t features_ro)
177 if (features_ro & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)
178 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
179 if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE)
180 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
181 if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
182 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
183 if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
184 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
185 if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
186 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
187 if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
188 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
189 if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
190 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
191 if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA)
192 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
193 if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC)
194 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
195 if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
196 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
199 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
201 ext4_assert(fs && read_only);
203 if (ext4_get32(&fs->sb, rev_level) == 0) {
208 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
209 ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
211 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
212 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
214 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
215 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
217 /*Check features_incompatible*/
218 v = (ext4_get32(&fs->sb, features_incompatible) &
219 (~CONFIG_FEATURE_INCOMPAT_SUPP));
221 ext4_dbg(DEBUG_FS, DBG_ERROR
222 "sblock has unsupported features incompatible:\n");
223 ext4_fs_debug_features_inc(v);
227 /*Check features_read_only*/
228 v = (ext4_get32(&fs->sb, features_read_only) &
229 (~CONFIG_FEATURE_RO_COMPAT_SUPP));
231 ext4_dbg(DEBUG_FS, DBG_WARN
232 "sblock has unsupported features read only:\n");
233 ext4_fs_debug_features_ro(v);
242 /**@brief Determine whether the block is inside the group.
243 * @param baddr block address
244 * @param bgid block group id
247 static int ext4_block_in_group(struct ext4_sblock *s,
251 uint32_t actual_bgid;
252 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
253 if (actual_bgid == bgid)
258 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
259 * need to use it within a single byte (to ensure we get endianness right).
260 * We can use memset for the rest of the bitmap as there are no other users.
262 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
266 if (start_bit >= end_bit)
269 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
270 ext4_bmap_bit_set(bitmap, i);
273 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
276 /**@brief Initialize block bitmap in block group.
277 * @param bg_ref Reference to block group
280 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
282 uint32_t i, bit, bit_max;
283 uint32_t group_blocks;
284 uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
285 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
286 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
287 uint32_t bitmap_block_addr =
288 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
289 uint32_t bitmap_inode_addr =
290 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
291 uint32_t inode_table_addr =
292 ext4_bg_get_inode_table_first_block(bg_ref->block_group,
294 uint32_t first_group_addr =
295 ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
297 uint32_t dsc_per_block =
298 ext4_sb_get_block_size(&bg_ref->fs->sb) /
299 ext4_sb_get_desc_size(&bg_ref->fs->sb);
302 ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
303 EXT4_FEATURE_INCOMPAT_FLEX_BG);
305 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
307 struct ext4_block block_bitmap;
309 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
313 memset(block_bitmap.data, 0, block_size);
315 bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
316 if (!ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
317 EXT4_FEATURE_INCOMPAT_META_BG) ||
318 bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
321 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
324 ext4_get16(&bg_ref->fs->sb,
325 s_reserved_gdt_blocks);
327 } else { /* For META_BG_BLOCK_GROUPS */
328 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
331 for (bit = 0; bit < bit_max; bit++)
332 ext4_bmap_bit_set(block_bitmap.data, bit);
334 if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
336 * Even though mke2fs always initialize first and last group
337 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
338 * to make sure we calculate the right free blocks
340 group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
341 ext4_get32(&bg_ref->fs->sb, first_data_block) -
342 (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
343 (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
345 group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
348 ext4_block_in_group(&bg_ref->fs->sb,
349 bitmap_block_addr, bg_ref->index))
350 ext4_bmap_bit_set(block_bitmap.data,
351 bitmap_block_addr - first_group_addr);
354 ext4_block_in_group(&bg_ref->fs->sb,
355 bitmap_inode_addr, bg_ref->index))
356 ext4_bmap_bit_set(block_bitmap.data,
357 bitmap_inode_addr - first_group_addr);
359 for (i = inode_table_addr;
360 i < inode_table_addr + inode_table_bcnt; i++) {
362 ext4_block_in_group(&bg_ref->fs->sb,
365 ext4_bmap_bit_set(block_bitmap.data,
366 i - first_group_addr);
369 * Also if the number of blocks within the group is
370 * less than the blocksize * 8 ( which is the size
371 * of bitmap ), set rest of the block bitmap to 1
373 ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
374 block_bitmap.dirty = true;
377 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
380 /**@brief Initialize i-node bitmap in block group.
381 * @param bg_ref Reference to block group
384 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
387 uint32_t bitmap_block_addr =
388 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
390 struct ext4_block block_bitmap;
392 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
396 /* Initialize all bitmap bits to zero */
397 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
398 uint32_t inodes_per_group =
399 ext4_get32(&bg_ref->fs->sb, inodes_per_group);
401 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
403 uint32_t start_bit = inodes_per_group;
404 uint32_t end_bit = block_size * 8;
407 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
408 ext4_bmap_bit_set(block_bitmap.data, i);
411 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
413 block_bitmap.dirty = true;
416 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
419 /**@brief Initialize i-node table in block group.
420 * @param bg_ref Reference to block group
423 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
425 struct ext4_sblock *sb = &bg_ref->fs->sb;
427 uint32_t inode_size = ext4_get32(sb, inode_size);
428 uint32_t block_size = ext4_sb_get_block_size(sb);
429 uint32_t inodes_per_block = block_size / inode_size;
430 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
431 uint32_t table_blocks = inodes_in_group / inodes_per_block;
434 if (inodes_in_group % inodes_per_block)
437 /* Compute initialization bounds */
438 uint32_t first_block =
439 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
441 uint32_t last_block = first_block + table_blocks - 1;
443 /* Initialization of all itable blocks */
444 for (fblock = first_block; fblock <= last_block; ++fblock) {
446 struct ext4_block block;
447 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
451 memset(block.data, 0, block_size);
454 ext4_block_set(bg_ref->fs->bdev, &block);
462 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
464 uint32_t dsc_per_block)
466 uint32_t first_meta_bg, dsc_id;
470 dsc_id = bgid / dsc_per_block;
471 first_meta_bg = ext4_sb_first_meta_bg(s);
473 if (!ext4_sb_has_feature_incompatible(s,
474 EXT4_FEATURE_INCOMPAT_META_BG) ||
475 dsc_id < first_meta_bg)
476 return ext4_get32(s, first_data_block) + dsc_id + 1;
478 if (ext4_sb_is_super_in_bg(s, bgid))
481 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
484 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
485 struct ext4_block_group_ref *ref)
487 /* Compute number of descriptors, that fits in one data block */
488 uint32_t dsc_per_block =
489 ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
491 /* Block group descriptor table starts at the next block after
494 ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
497 (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
499 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
503 ref->block_group = (void *)(ref->block.data + offset);
508 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
509 rc = ext4_fs_init_block_bitmap(ref);
511 ext4_block_set(fs->bdev, &ref->block);
514 ext4_bg_clear_flag(ref->block_group,
515 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
520 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
521 rc = ext4_fs_init_inode_bitmap(ref);
523 ext4_block_set(ref->fs->bdev, &ref->block);
527 ext4_bg_clear_flag(ref->block_group,
528 EXT4_BLOCK_GROUP_INODE_UNINIT);
530 if (!ext4_bg_has_flag(ref->block_group,
531 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
532 rc = ext4_fs_init_inode_table(ref);
534 ext4_block_set(fs->bdev, &ref->block);
538 ext4_bg_set_flag(ref->block_group,
539 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
548 /**@brief Compute checksum of block group descriptor.
549 * @param sb Superblock
550 * @param bgid Index of block group in the filesystem
551 * @param bg Block group to compute checksum for
552 * @return Checksum value
554 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
555 struct ext4_bgroup *bg)
557 /* If checksum not supported, 0 will be returned */
560 /* Compute the checksum only if the filesystem supports it */
561 if (ext4_sb_has_feature_read_only(sb,
562 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
563 uint8_t *base = (uint8_t *)bg;
564 uint8_t *checksum = (uint8_t *)&bg->checksum;
566 uint32_t offset = (uint32_t)(checksum - base);
568 /* Convert block group index to little endian */
569 uint32_t le_group = to_le32(bgid);
572 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
574 /* Include index of block group */
576 ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
578 /* Compute crc from the first part (stop before checksum field)
580 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
583 offset += sizeof(bg->checksum);
585 /* Checksum of the rest of block group descriptor */
586 if ((ext4_sb_has_feature_incompatible(
587 sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
588 (offset < ext4_sb_get_desc_size(sb)))
590 crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
591 ext4_sb_get_desc_size(sb) - offset);
596 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
598 /* Check if reference modified */
600 /* Compute new checksum of block group */
601 uint16_t checksum = ext4_fs_bg_checksum(
602 &ref->fs->sb, ref->index, ref->block_group);
604 ref->block_group->checksum = to_le16(checksum);
606 /* Mark block dirty for writing changes to physical device */
607 ref->block.dirty = true;
610 /* Put back block, that contains block group descriptor */
611 return ext4_block_set(ref->fs->bdev, &ref->block);
614 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
615 struct ext4_inode_ref *ref)
617 /* Compute number of i-nodes, that fits in one data block */
618 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
621 * Inode numbers are 1-based, but it is simpler to work with 0-based
622 * when computing indices
625 uint32_t block_group = index / inodes_per_group;
626 uint32_t offset_in_group = index % inodes_per_group;
628 /* Load block group, where i-node is located */
629 struct ext4_block_group_ref bg_ref;
631 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
636 /* Load block address, where i-node table is located */
637 uint32_t inode_table_start =
638 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
640 /* Put back block group reference (not needed more) */
641 rc = ext4_fs_put_block_group_ref(&bg_ref);
646 /* Compute position of i-node in the block group */
647 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
648 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
649 uint32_t byte_offset_in_group = offset_in_group * inode_size;
651 /* Compute block address */
653 inode_table_start + (byte_offset_in_group / block_size);
655 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
660 /* Compute position of i-node in the data block */
661 uint32_t offset_in_block = byte_offset_in_group % block_size;
662 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
664 /* We need to store the original value of index in the reference */
665 ref->index = index + 1;
672 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
674 /* Check if reference modified */
676 /* Mark block dirty for writing changes to physical device */
677 ref->block.dirty = true;
680 /* Put back block, that contains i-node */
681 return ext4_block_set(ref->fs->bdev, &ref->block);
684 void ext4_fs_inode_blocks_init(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref)
687 struct ext4_inode *inode = inode_ref->inode;
689 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
690 inode->blocks[i] = 0;
692 #if CONFIG_EXTENT_ENABLE
693 /* Initialize extents if needed */
694 if (ext4_sb_has_feature_incompatible(&fs->sb,
695 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
696 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
698 /* Initialize extent root header */
699 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
700 ext4_extent_header_set_depth(header, 0);
701 ext4_extent_header_set_entries_count(header, 0);
702 ext4_extent_header_set_generation(header, 0);
703 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
705 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
706 sizeof(struct ext4_extent_header)) /
707 sizeof(struct ext4_extent);
709 ext4_extent_header_set_max_entries_count(header, max_entries);
714 static uint32_t ext4_fs_correspond_inode_mode(int filetype)
717 case EXT4_DIRENTRY_DIR:
718 return EXT4_INODE_MODE_DIRECTORY;
719 case EXT4_DIRENTRY_REG_FILE:
720 return EXT4_INODE_MODE_FILE;
721 case EXT4_DIRENTRY_SYMLINK:
722 return EXT4_INODE_MODE_SOFTLINK;
724 /* FIXME: right now we only support 3 file type. */
730 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
733 /* Check if newly allocated i-node will be a directory */
736 is_dir = (filetype == EXT4_DIRENTRY_DIR);
738 /* Allocate inode by allocation algorithm */
740 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
744 /* Load i-node from on-disk i-node table */
745 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
747 ext4_ialloc_free_inode(fs, index, is_dir);
751 /* Initialize i-node */
752 struct ext4_inode *inode = inode_ref->inode;
757 * Default directory permissions to be compatible with other
759 * 0777 (octal) == rwxrwxrwx
763 mode |= EXT4_INODE_MODE_DIRECTORY;
766 * Default file permissions to be compatible with other systems
767 * 0666 (octal) == rw-rw-rw-
771 mode |= ext4_fs_correspond_inode_mode(filetype);
773 ext4_inode_set_mode(&fs->sb, inode, mode);
775 ext4_inode_set_links_count(inode, 0);
776 ext4_inode_set_uid(inode, 0);
777 ext4_inode_set_gid(inode, 0);
778 ext4_inode_set_size(inode, 0);
779 ext4_inode_set_access_time(inode, 0);
780 ext4_inode_set_change_inode_time(inode, 0);
781 ext4_inode_set_modification_time(inode, 0);
782 ext4_inode_set_deletion_time(inode, 0);
783 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
784 ext4_inode_set_flags(inode, 0);
785 ext4_inode_set_generation(inode, 0);
787 /* Reset blocks array. For symbolic link inode, just
788 * fill in blocks with 0 */
789 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
790 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
791 inode->blocks[i] = 0;
794 ext4_fs_inode_blocks_init(fs, inode_ref);
796 inode_ref->dirty = true;
801 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
803 struct ext4_fs *fs = inode_ref->fs;
807 #if CONFIG_EXTENT_ENABLE
808 /* For extents must be data block destroyed by other way */
809 if ((ext4_sb_has_feature_incompatible(&fs->sb,
810 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
811 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
812 /* Data structures are released during truncate operation... */
816 /* Release all indirect (no data) blocks */
818 /* 1) Single indirect */
819 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
821 int rc = ext4_balloc_free_block(inode_ref, fblock);
825 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
828 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
829 uint32_t count = block_size / sizeof(uint32_t);
831 struct ext4_block block;
833 /* 2) Double indirect */
834 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
836 int rc = ext4_block_get(fs->bdev, &block, fblock);
841 for (offset = 0; offset < count; ++offset) {
842 ind_block = to_le32(((uint32_t *)block.data)[offset]);
846 rc = ext4_balloc_free_block(inode_ref, ind_block);
848 ext4_block_set(fs->bdev, &block);
854 ext4_block_set(fs->bdev, &block);
855 rc = ext4_balloc_free_block(inode_ref, fblock);
859 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
862 /* 3) Tripple indirect */
863 struct ext4_block subblock;
864 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
867 rc = ext4_block_get(fs->bdev, &block, fblock);
872 for (offset = 0; offset < count; ++offset) {
873 ind_block = to_le32(((uint32_t *)block.data)[offset]);
877 rc = ext4_block_get(fs->bdev, &subblock,
880 ext4_block_set(fs->bdev, &block);
885 for (suboff = 0; suboff < count; ++suboff) {
886 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
890 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
892 ext4_block_set(fs->bdev, &subblock);
893 ext4_block_set(fs->bdev, &block);
899 ext4_block_set(fs->bdev, &subblock);
901 rc = ext4_balloc_free_block(inode_ref,
904 ext4_block_set(fs->bdev, &block);
910 ext4_block_set(fs->bdev, &block);
911 rc = ext4_balloc_free_block(inode_ref, fblock);
915 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
917 /* Mark inode dirty for writing to the physical device */
918 inode_ref->dirty = true;
920 /* Free block with extended attributes if present */
921 uint32_t xattr_block =
922 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
924 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
928 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
931 /* Free inode by allocator */
932 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
933 EXT4_INODE_MODE_DIRECTORY))
934 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
936 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
941 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
943 struct ext4_sblock *sb = &inode_ref->fs->sb;
946 /* Check flags, if i-node can be truncated */
947 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
950 /* If sizes are equal, nothing has to be done. */
951 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
952 if (old_size == new_size)
955 /* It's not supported to make the larger file by truncate operation */
956 if (old_size < new_size)
959 if (ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK)
960 && old_size < sizeof(inode_ref->inode->blocks)
961 && !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
962 char *content = (char *)inode_ref->inode->blocks;
963 memset(content + new_size, 0,
964 sizeof(inode_ref->inode->blocks) - new_size);
965 ext4_inode_set_size(inode_ref->inode, new_size);
966 inode_ref->dirty = true;
971 /* Compute how many blocks will be released */
972 uint32_t block_size = ext4_sb_get_block_size(sb);
973 uint32_t new_blocks_count = (new_size + block_size - 1) /
975 uint32_t old_blocks_count = (old_size + block_size - 1) /
977 uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
978 #if CONFIG_EXTENT_ENABLE
979 if ((ext4_sb_has_feature_incompatible(sb,
980 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
981 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
983 /* Extents require special operation */
984 if (diff_blocks_count) {
985 int rc = ext4_extent_release_blocks_from(
995 /* Release data blocks from the end of file */
997 /* Starting from 1 because of logical blocks are numbered from 0
999 for (i = 0; i < diff_blocks_count; ++i) {
1000 int rc = ext4_fs_release_inode_block(
1001 inode_ref, new_blocks_count + i);
1008 ext4_inode_set_size(inode_ref->inode, new_size);
1009 inode_ref->dirty = true;
1014 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1015 uint64_t iblock, uint32_t *fblock)
1017 struct ext4_fs *fs = inode_ref->fs;
1019 /* For empty file is situation simple */
1020 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1025 uint32_t current_block;
1026 #if CONFIG_EXTENT_ENABLE
1027 /* Handle i-node using extents */
1028 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1029 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1030 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1033 ext4_extent_find_block(inode_ref, iblock, ¤t_block);
1037 *fblock = current_block;
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;
1124 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1125 uint64_t iblock, uint32_t fblock)
1127 struct ext4_fs *fs = inode_ref->fs;
1129 #if CONFIG_EXTENT_ENABLE
1130 /* Handle inode using extents */
1131 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1132 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1133 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1139 /* Handle simple case when we are dealing with direct reference */
1140 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1141 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1143 inode_ref->dirty = true;
1148 /* Determine the indirection level needed to get the desired block */
1149 unsigned int level = 0;
1151 for (i = 1; i < 4; i++) {
1152 if (iblock < fs->inode_block_limits[i]) {
1161 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1163 /* Compute offsets for the topmost level */
1164 uint64_t block_offset_in_level =
1165 iblock - fs->inode_block_limits[level - 1];
1166 uint32_t current_block =
1167 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1168 uint32_t offset_in_block =
1169 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1171 uint32_t new_block_addr;
1173 struct ext4_block block;
1174 struct ext4_block new_block;
1176 /* Is needed to allocate indirect block on the i-node level */
1177 if (current_block == 0) {
1178 /* Allocate new indirect block */
1179 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1184 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1186 inode_ref->dirty = true;
1188 /* Load newly allocated block */
1189 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1191 ext4_balloc_free_block(inode_ref, new_block_addr);
1195 /* Initialize new block */
1196 memset(new_block.data, 0, block_size);
1197 new_block.dirty = true;
1199 /* Put back the allocated block */
1200 rc = ext4_block_set(fs->bdev, &new_block);
1204 current_block = new_block_addr;
1208 * Navigate through other levels, until we find the block number
1209 * or find null reference meaning we are dealing with sparse file
1212 int rc = ext4_block_get(fs->bdev, &block, current_block);
1217 to_le32(((uint32_t *)block.data)[offset_in_block]);
1219 if ((level > 1) && (current_block == 0)) {
1220 /* Allocate new block */
1222 ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1224 ext4_block_set(fs->bdev, &block);
1228 /* Load newly allocated block */
1229 rc = ext4_block_get(fs->bdev, &new_block,
1233 ext4_block_set(fs->bdev, &block);
1237 /* Initialize allocated block */
1238 memset(new_block.data, 0, block_size);
1239 new_block.dirty = true;
1241 rc = ext4_block_set(fs->bdev, &new_block);
1243 ext4_block_set(fs->bdev, &block);
1247 /* Write block address to the parent */
1248 ((uint32_t *)block.data)[offset_in_block] =
1249 to_le32(new_block_addr);
1251 current_block = new_block_addr;
1254 /* Will be finished, write the fblock address */
1256 ((uint32_t *)block.data)[offset_in_block] =
1261 rc = ext4_block_set(fs->bdev, &block);
1268 * If we are on the last level, break here as
1269 * there is no next level to visit
1274 /* Visit the next level */
1275 block_offset_in_level %= fs->inode_blocks_per_level[level];
1276 offset_in_block = block_offset_in_level /
1277 fs->inode_blocks_per_level[level - 1];
1283 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1288 struct ext4_fs *fs = inode_ref->fs;
1290 /* Extents are handled otherwise = there is not support in this function
1293 ext4_sb_has_feature_incompatible(&fs->sb,
1294 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1295 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1297 struct ext4_inode *inode = inode_ref->inode;
1299 /* Handle simple case when we are dealing with direct reference */
1300 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1301 fblock = ext4_inode_get_direct_block(inode, iblock);
1307 ext4_inode_set_direct_block(inode, iblock, 0);
1308 return ext4_balloc_free_block(inode_ref, fblock);
1311 /* Determine the indirection level needed to get the desired block */
1312 unsigned int level = 0;
1314 for (i = 1; i < 4; i++) {
1315 if (iblock < fs->inode_block_limits[i]) {
1324 /* Compute offsets for the topmost level */
1325 uint64_t block_offset_in_level =
1326 iblock - fs->inode_block_limits[level - 1];
1327 uint32_t current_block =
1328 ext4_inode_get_indirect_block(inode, level - 1);
1329 uint32_t offset_in_block =
1330 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1333 * Navigate through other levels, until we find the block number
1334 * or find null reference meaning we are dealing with sparse file
1336 struct ext4_block block;
1341 if (current_block == 0)
1344 int rc = ext4_block_get(fs->bdev, &block, current_block);
1349 to_le32(((uint32_t *)block.data)[offset_in_block]);
1351 /* Set zero if physical data block address found */
1353 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1357 rc = ext4_block_set(fs->bdev, &block);
1364 * If we are on the last level, break here as
1365 * there is no next level to visit
1370 /* Visit the next level */
1371 block_offset_in_level %= fs->inode_blocks_per_level[level];
1372 offset_in_block = block_offset_in_level /
1373 fs->inode_blocks_per_level[level - 1];
1376 fblock = current_block;
1380 /* Physical block is not referenced, it can be released */
1381 return ext4_balloc_free_block(inode_ref, fblock);
1384 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1385 uint32_t *fblock, uint32_t *iblock)
1387 #if CONFIG_EXTENT_ENABLE
1388 /* Handle extents separately */
1389 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1390 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1391 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1392 return ext4_extent_append_block(inode_ref, iblock, fblock,
1396 struct ext4_sblock *sb = &inode_ref->fs->sb;
1398 /* Compute next block index and allocate data block */
1399 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1400 uint32_t block_size = ext4_sb_get_block_size(sb);
1402 /* Align size i-node size */
1403 if ((inode_size % block_size) != 0)
1404 inode_size += block_size - (inode_size % block_size);
1406 /* Logical blocks are numbered from 0 */
1407 uint32_t new_block_idx = inode_size / block_size;
1409 /* Allocate new physical block */
1410 uint32_t phys_block;
1411 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1415 /* Add physical block address to the i-node */
1416 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1419 ext4_balloc_free_block(inode_ref, phys_block);
1424 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1425 inode_ref->dirty = true;
1427 *fblock = phys_block;
1428 *iblock = new_block_idx;
1433 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1437 link = ext4_inode_get_links_count(inode_ref->inode);
1439 ext4_inode_set_links_count(inode_ref->inode, link);
1442 ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1443 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1444 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1446 if (is_dx && link > 1) {
1447 if (link >= EXT4_LINK_MAX || link == 2) {
1448 ext4_inode_set_links_count(inode_ref->inode, 1);
1451 ext4_get32(&inode_ref->fs->sb, features_read_only);
1452 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1453 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1458 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1460 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1461 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1462 EXT4_INODE_MODE_DIRECTORY)) {
1464 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1469 ext4_inode_set_links_count(inode_ref->inode, links - 1);