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 ext4_fsblk_t bitmap_block_addr =
288 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
289 ext4_fsblk_t bitmap_inode_addr =
290 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
291 ext4_fsblk_t inode_table_addr =
292 ext4_bg_get_inode_table_first_block(bg_ref->block_group,
294 ext4_fsblk_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 ext4_fsblk_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 ext4_fsblk_t first_block =
439 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
441 ext4_fsblk_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 ext4_fsblk_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 */
652 ext4_fsblk_t block_id =
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;
693 #if CONFIG_EXTENT_ENABLE
694 /* Initialize extents if needed */
695 if (ext4_sb_has_feature_incompatible(&fs->sb,
696 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
697 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
699 /* Initialize extent root header */
700 ext4_extent_tree_init(inode_ref);
705 static uint32_t ext4_fs_correspond_inode_mode(int filetype)
708 case EXT4_DIRENTRY_DIR:
709 return EXT4_INODE_MODE_DIRECTORY;
710 case EXT4_DIRENTRY_REG_FILE:
711 return EXT4_INODE_MODE_FILE;
712 case EXT4_DIRENTRY_SYMLINK:
713 return EXT4_INODE_MODE_SOFTLINK;
715 /* FIXME: right now we only support 3 file type. */
721 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
724 /* Check if newly allocated i-node will be a directory */
727 is_dir = (filetype == EXT4_DIRENTRY_DIR);
729 /* Allocate inode by allocation algorithm */
731 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
735 /* Load i-node from on-disk i-node table */
736 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
738 ext4_ialloc_free_inode(fs, index, is_dir);
742 /* Initialize i-node */
743 struct ext4_inode *inode = inode_ref->inode;
748 * Default directory permissions to be compatible with other
750 * 0777 (octal) == rwxrwxrwx
754 mode |= EXT4_INODE_MODE_DIRECTORY;
757 * Default file permissions to be compatible with other systems
758 * 0666 (octal) == rw-rw-rw-
762 mode |= ext4_fs_correspond_inode_mode(filetype);
764 ext4_inode_set_mode(&fs->sb, inode, mode);
766 ext4_inode_set_links_count(inode, 0);
767 ext4_inode_set_uid(inode, 0);
768 ext4_inode_set_gid(inode, 0);
769 ext4_inode_set_size(inode, 0);
770 ext4_inode_set_access_time(inode, 0);
771 ext4_inode_set_change_inode_time(inode, 0);
772 ext4_inode_set_modification_time(inode, 0);
773 ext4_inode_set_deletion_time(inode, 0);
774 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
775 ext4_inode_set_flags(inode, 0);
776 ext4_inode_set_generation(inode, 0);
778 /* Reset blocks array. For symbolic link inode, just
779 * fill in blocks with 0 */
780 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
781 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
782 inode->blocks[i] = 0;
785 ext4_fs_inode_blocks_init(fs, inode_ref);
787 inode_ref->dirty = true;
792 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
794 struct ext4_fs *fs = inode_ref->fs;
798 #if CONFIG_EXTENT_ENABLE
799 /* For extents must be data block destroyed by other way */
800 if ((ext4_sb_has_feature_incompatible(&fs->sb,
801 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
802 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
803 /* Data structures are released during truncate operation... */
807 /* Release all indirect (no data) blocks */
809 /* 1) Single indirect */
810 ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
812 int rc = ext4_balloc_free_block(inode_ref, fblock);
816 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
819 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
820 uint32_t count = block_size / sizeof(uint32_t);
822 struct ext4_block block;
824 /* 2) Double indirect */
825 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
827 int rc = ext4_block_get(fs->bdev, &block, fblock);
831 ext4_fsblk_t ind_block;
832 for (offset = 0; offset < count; ++offset) {
833 ind_block = to_le32(((uint32_t *)block.data)[offset]);
837 rc = ext4_balloc_free_block(inode_ref, ind_block);
839 ext4_block_set(fs->bdev, &block);
845 ext4_block_set(fs->bdev, &block);
846 rc = ext4_balloc_free_block(inode_ref, fblock);
850 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
853 /* 3) Tripple indirect */
854 struct ext4_block subblock;
855 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
858 rc = ext4_block_get(fs->bdev, &block, fblock);
862 ext4_fsblk_t ind_block;
863 for (offset = 0; offset < count; ++offset) {
864 ind_block = to_le32(((uint32_t *)block.data)[offset]);
868 rc = ext4_block_get(fs->bdev, &subblock,
871 ext4_block_set(fs->bdev, &block);
875 ext4_fsblk_t ind_subblk;
876 for (suboff = 0; suboff < count; ++suboff) {
877 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
881 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
883 ext4_block_set(fs->bdev, &subblock);
884 ext4_block_set(fs->bdev, &block);
890 ext4_block_set(fs->bdev, &subblock);
892 rc = ext4_balloc_free_block(inode_ref,
895 ext4_block_set(fs->bdev, &block);
901 ext4_block_set(fs->bdev, &block);
902 rc = ext4_balloc_free_block(inode_ref, fblock);
906 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
908 /* Mark inode dirty for writing to the physical device */
909 inode_ref->dirty = true;
911 /* Free block with extended attributes if present */
912 ext4_fsblk_t xattr_block =
913 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
915 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
919 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
922 /* Free inode by allocator */
923 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
924 EXT4_INODE_MODE_DIRECTORY))
925 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
927 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
932 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
934 struct ext4_sblock *sb = &inode_ref->fs->sb;
937 /* Check flags, if i-node can be truncated */
938 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
941 /* If sizes are equal, nothing has to be done. */
942 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
943 if (old_size == new_size)
946 /* It's not supported to make the larger file by truncate operation */
947 if (old_size < new_size)
950 if (ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK)
951 && old_size < sizeof(inode_ref->inode->blocks)
952 && !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
953 char *content = (char *)inode_ref->inode->blocks;
954 memset(content + new_size, 0,
955 sizeof(inode_ref->inode->blocks) - new_size);
956 ext4_inode_set_size(inode_ref->inode, new_size);
957 inode_ref->dirty = true;
962 /* Compute how many blocks will be released */
963 uint32_t block_size = ext4_sb_get_block_size(sb);
964 uint32_t new_blocks_count = (new_size + block_size - 1) /
966 uint32_t old_blocks_count = (old_size + block_size - 1) /
968 uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
969 #if CONFIG_EXTENT_ENABLE
970 if ((ext4_sb_has_feature_incompatible(sb,
971 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
972 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
974 /* Extents require special operation */
975 if (diff_blocks_count) {
976 int rc = ext4_extent_remove_space(inode_ref,
977 new_blocks_count, EXT_MAX_BLOCKS);
985 /* Release data blocks from the end of file */
987 /* Starting from 1 because of logical blocks are numbered from 0
989 for (i = 0; i < diff_blocks_count; ++i) {
990 int rc = ext4_fs_release_inode_block(
991 inode_ref, new_blocks_count + i);
998 ext4_inode_set_size(inode_ref->inode, new_size);
999 inode_ref->dirty = true;
1004 /**@brief Compute 'goal' for inode index
1005 * @param inode_ref Reference to inode, to allocate block for
1008 ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
1010 uint32_t group_inodes =
1011 ext4_get32(&inode_ref->fs->sb, inodes_per_group);
1012 return (inode_ref->index - 1) / group_inodes;
1015 /**@brief Compute 'goal' for allocation algorithm (For blockmap).
1016 * @param inode_ref Reference to inode, to allocate block for
1018 * @return error code
1020 int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
1023 struct ext4_sblock *sb = &inode_ref->fs->sb;
1026 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1027 uint32_t block_size = ext4_sb_get_block_size(sb);
1028 uint32_t inode_block_count = inode_size / block_size;
1030 if (inode_size % block_size != 0)
1031 inode_block_count++;
1033 /* If inode has some blocks, get last block address + 1 */
1034 if (inode_block_count > 0) {
1035 int rc = ext4_fs_get_inode_data_block_index(
1036 inode_ref, inode_block_count - 1, goal, false);
1045 /* If goal == 0, sparse file -> continue */
1048 /* Identify block group of inode */
1050 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
1051 uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
1052 block_size = ext4_sb_get_block_size(sb);
1054 /* Load block group reference */
1055 struct ext4_block_group_ref bg_ref;
1057 ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
1061 /* Compute indexes */
1062 uint32_t block_group_count = ext4_block_group_cnt(sb);
1063 ext4_fsblk_t inode_table_first_block =
1064 ext4_bg_get_inode_table_first_block(bg_ref.block_group, sb);
1065 uint16_t inode_table_item_size = ext4_get16(sb, inode_size);
1066 uint32_t inode_table_bytes;
1068 /* Check for last block group */
1069 if (block_group < block_group_count - 1) {
1070 inode_table_bytes = inodes_per_group * inode_table_item_size;
1072 /* Last block group could be smaller */
1073 uint32_t inodes_count_total = ext4_get32(sb, inodes_count);
1076 (inodes_count_total -
1077 ((block_group_count - 1) * inodes_per_group)) *
1078 inode_table_item_size;
1081 ext4_fsblk_t inode_table_blocks = inode_table_bytes / block_size;
1083 if (inode_table_bytes % block_size)
1084 inode_table_blocks++;
1086 *goal = inode_table_first_block + inode_table_blocks;
1088 return ext4_fs_put_block_group_ref(&bg_ref);
1091 static int ext4_fs_get_inode_data_block_idx(struct ext4_inode_ref *inode_ref,
1092 uint64_t iblock, ext4_fsblk_t *fblock,
1094 bool support_unwritten)
1096 struct ext4_fs *fs = inode_ref->fs;
1098 /* For empty file is situation simple */
1099 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1104 ext4_fsblk_t current_block;
1106 (void)extent_create;
1107 #if CONFIG_EXTENT_ENABLE
1108 /* Handle i-node using extents */
1109 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1110 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1111 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1113 ext4_fsblk_t current_fsblk;
1114 int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
1115 ¤t_fsblk, extent_create, NULL);
1119 current_block = current_fsblk;
1120 *fblock = current_block;
1122 ext4_assert(*fblock || support_unwritten);
1127 struct ext4_inode *inode = inode_ref->inode;
1129 /* Direct block are read directly from array in i-node structure */
1130 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1132 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1133 *fblock = current_block;
1137 /* Determine indirection level of the target block */
1138 unsigned int level = 0;
1140 for (i = 1; i < 4; i++) {
1141 if (iblock < fs->inode_block_limits[i]) {
1150 /* Compute offsets for the topmost level */
1151 uint64_t block_offset_in_level =
1152 iblock - fs->inode_block_limits[level - 1];
1153 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1154 uint32_t offset_in_block =
1155 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1158 if (current_block == 0) {
1163 struct ext4_block block;
1166 * Navigate through other levels, until we find the block number
1167 * or find null reference meaning we are dealing with sparse file
1170 /* Load indirect block */
1171 int rc = ext4_block_get(fs->bdev, &block, current_block);
1175 /* Read block address from indirect block */
1177 to_le32(((uint32_t *)block.data)[offset_in_block]);
1179 /* Put back indirect block untouched */
1180 rc = ext4_block_set(fs->bdev, &block);
1184 /* Check for sparse file */
1185 if (current_block == 0) {
1190 /* Jump to the next level */
1193 /* Termination condition - we have address of data block loaded
1198 /* Visit the next level */
1199 block_offset_in_level %= fs->inode_blocks_per_level[level];
1200 offset_in_block = block_offset_in_level /
1201 fs->inode_blocks_per_level[level - 1];
1204 *fblock = current_block;
1210 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1211 uint64_t iblock, ext4_fsblk_t *fblock,
1212 bool support_unwritten)
1214 return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1215 false, support_unwritten);
1218 int ext4_fs_init_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1219 uint64_t iblock, ext4_fsblk_t *fblock)
1221 return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1225 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1226 uint64_t iblock, ext4_fsblk_t fblock)
1228 struct ext4_fs *fs = inode_ref->fs;
1230 #if CONFIG_EXTENT_ENABLE
1231 /* Handle inode using extents */
1232 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1233 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1234 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1240 /* Handle simple case when we are dealing with direct reference */
1241 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1242 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1244 inode_ref->dirty = true;
1249 /* Determine the indirection level needed to get the desired block */
1250 unsigned int level = 0;
1252 for (i = 1; i < 4; i++) {
1253 if (iblock < fs->inode_block_limits[i]) {
1262 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1264 /* Compute offsets for the topmost level */
1265 uint64_t block_offset_in_level =
1266 iblock - fs->inode_block_limits[level - 1];
1267 ext4_fsblk_t current_block =
1268 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1269 uint32_t offset_in_block =
1270 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1272 ext4_fsblk_t new_block_addr;
1274 struct ext4_block block;
1275 struct ext4_block new_block;
1277 /* Is needed to allocate indirect block on the i-node level */
1278 if (current_block == 0) {
1279 /* Allocate new indirect block */
1281 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1285 rc = ext4_balloc_alloc_block(inode_ref,
1292 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1293 (uint32_t)new_block_addr);
1294 inode_ref->dirty = true;
1296 /* Load newly allocated block */
1297 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1299 ext4_balloc_free_block(inode_ref, new_block_addr);
1303 /* Initialize new block */
1304 memset(new_block.data, 0, block_size);
1305 new_block.dirty = true;
1307 /* Put back the allocated block */
1308 rc = ext4_block_set(fs->bdev, &new_block);
1312 current_block = new_block_addr;
1316 * Navigate through other levels, until we find the block number
1317 * or find null reference meaning we are dealing with sparse file
1320 int rc = ext4_block_get(fs->bdev, &block, current_block);
1325 to_le32(((uint32_t *)block.data)[offset_in_block]);
1327 if ((level > 1) && (current_block == 0)) {
1329 rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1331 ext4_block_set(fs->bdev, &block);
1335 /* Allocate new block */
1337 ext4_balloc_alloc_block(inode_ref, goal, &new_block_addr);
1339 ext4_block_set(fs->bdev, &block);
1343 /* Load newly allocated block */
1344 rc = ext4_block_get(fs->bdev, &new_block,
1348 ext4_block_set(fs->bdev, &block);
1352 /* Initialize allocated block */
1353 memset(new_block.data, 0, block_size);
1354 new_block.dirty = true;
1356 rc = ext4_block_set(fs->bdev, &new_block);
1358 ext4_block_set(fs->bdev, &block);
1362 /* Write block address to the parent */
1363 ((uint32_t *)block.data)[offset_in_block] =
1364 to_le32((uint32_t)new_block_addr);
1366 current_block = new_block_addr;
1369 /* Will be finished, write the fblock address */
1371 ((uint32_t *)block.data)[offset_in_block] =
1372 to_le32((uint32_t)fblock);
1376 rc = ext4_block_set(fs->bdev, &block);
1383 * If we are on the last level, break here as
1384 * there is no next level to visit
1389 /* Visit the next level */
1390 block_offset_in_level %= fs->inode_blocks_per_level[level];
1391 offset_in_block = block_offset_in_level /
1392 fs->inode_blocks_per_level[level - 1];
1398 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1401 ext4_fsblk_t fblock;
1403 struct ext4_fs *fs = inode_ref->fs;
1405 /* Extents are handled otherwise = there is not support in this function
1408 ext4_sb_has_feature_incompatible(&fs->sb,
1409 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1410 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1412 struct ext4_inode *inode = inode_ref->inode;
1414 /* Handle simple case when we are dealing with direct reference */
1415 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1416 fblock = ext4_inode_get_direct_block(inode, iblock);
1422 ext4_inode_set_direct_block(inode, iblock, 0);
1423 return ext4_balloc_free_block(inode_ref, fblock);
1426 /* Determine the indirection level needed to get the desired block */
1427 unsigned int level = 0;
1429 for (i = 1; i < 4; i++) {
1430 if (iblock < fs->inode_block_limits[i]) {
1439 /* Compute offsets for the topmost level */
1440 uint64_t block_offset_in_level =
1441 iblock - fs->inode_block_limits[level - 1];
1442 ext4_fsblk_t current_block =
1443 ext4_inode_get_indirect_block(inode, level - 1);
1444 uint32_t offset_in_block =
1445 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1448 * Navigate through other levels, until we find the block number
1449 * or find null reference meaning we are dealing with sparse file
1451 struct ext4_block block;
1456 if (current_block == 0)
1459 int rc = ext4_block_get(fs->bdev, &block, current_block);
1464 to_le32(((uint32_t *)block.data)[offset_in_block]);
1466 /* Set zero if physical data block address found */
1468 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1472 rc = ext4_block_set(fs->bdev, &block);
1479 * If we are on the last level, break here as
1480 * there is no next level to visit
1485 /* Visit the next level */
1486 block_offset_in_level %= fs->inode_blocks_per_level[level];
1487 offset_in_block = block_offset_in_level /
1488 fs->inode_blocks_per_level[level - 1];
1491 fblock = current_block;
1495 /* Physical block is not referenced, it can be released */
1496 return ext4_balloc_free_block(inode_ref, fblock);
1499 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1500 ext4_fsblk_t *fblock, uint32_t *iblock)
1502 #if CONFIG_EXTENT_ENABLE
1503 /* Handle extents separately */
1504 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1505 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1506 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1508 ext4_fsblk_t current_fsblk;
1509 struct ext4_sblock *sb = &inode_ref->fs->sb;
1510 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1511 uint32_t block_size = ext4_sb_get_block_size(sb);
1512 *iblock = (inode_size + block_size - 1) /
1515 rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
1516 ¤t_fsblk, true, NULL);
1519 *fblock = current_fsblk;
1520 ext4_assert(*fblock);
1522 ext4_inode_set_size(inode_ref->inode,
1523 inode_size + block_size);
1524 inode_ref->dirty = true;
1530 struct ext4_sblock *sb = &inode_ref->fs->sb;
1532 /* Compute next block index and allocate data block */
1533 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1534 uint32_t block_size = ext4_sb_get_block_size(sb);
1536 /* Align size i-node size */
1537 if ((inode_size % block_size) != 0)
1538 inode_size += block_size - (inode_size % block_size);
1540 /* Logical blocks are numbered from 0 */
1541 uint32_t new_block_idx = inode_size / block_size;
1543 /* Allocate new physical block */
1544 ext4_fsblk_t goal, phys_block;
1545 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1549 rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
1553 /* Add physical block address to the i-node */
1554 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1557 ext4_balloc_free_block(inode_ref, phys_block);
1562 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1563 inode_ref->dirty = true;
1565 *fblock = phys_block;
1566 *iblock = new_block_idx;
1571 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1575 link = ext4_inode_get_links_count(inode_ref->inode);
1577 ext4_inode_set_links_count(inode_ref->inode, link);
1580 ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1581 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1582 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1584 if (is_dx && link > 1) {
1585 if (link >= EXT4_LINK_MAX || link == 2) {
1586 ext4_inode_set_links_count(inode_ref->inode, 1);
1589 ext4_get32(&inode_ref->fs->sb, features_read_only);
1590 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1591 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1596 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1598 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1599 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1600 EXT4_INODE_MODE_DIRECTORY)) {
1602 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1607 ext4_inode_set_links_count(inode_ref->inode, links - 1);