2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
7 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
10 * esd gmbh <www.esd-electronics.com>
11 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
14 * GRUB -- GRand Unified Bootloader
15 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
17 * ext4write : Based on generic ext4 protocol.
19 * SPDX-License-Identifier: GPL-2.0+
23 #include <ext_common.h>
29 #include <linux/stat.h>
30 #include <linux/time.h>
31 #include <asm/byteorder.h>
32 #include "ext4_common.h"
34 struct ext2_data *ext4fs_root;
35 struct ext2fs_node *ext4fs_file;
36 __le32 *ext4fs_indir1_block;
37 int ext4fs_indir1_size;
38 int ext4fs_indir1_blkno = -1;
39 __le32 *ext4fs_indir2_block;
40 int ext4fs_indir2_size;
41 int ext4fs_indir2_blkno = -1;
43 __le32 *ext4fs_indir3_block;
44 int ext4fs_indir3_size;
45 int ext4fs_indir3_blkno = -1;
46 struct ext2_inode *g_parent_inode;
47 static int symlinknest;
49 #if defined(CONFIG_EXT4_WRITE)
50 struct ext2_block_group *ext4fs_get_group_descriptor
51 (const struct ext_filesystem *fs, uint32_t bg_idx)
53 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
56 static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
58 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
61 static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
63 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
64 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
67 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
68 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
71 static inline void ext4fs_bg_free_inodes_dec
72 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
74 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
76 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
79 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
81 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
84 static inline void ext4fs_bg_free_blocks_dec
85 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
87 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
89 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
92 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
94 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
97 static inline void ext4fs_bg_itable_unused_dec
98 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
100 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
101 if (fs->gdsize == 64)
102 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
105 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
106 if (fs->gdsize == 64)
107 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
110 uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
112 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
113 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
117 void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
119 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
120 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
123 uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
124 const struct ext_filesystem *fs)
126 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
127 if (fs->gdsize == 64)
128 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
133 uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
134 const struct ext_filesystem *fs)
136 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
137 if (fs->gdsize == 64)
138 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
142 static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
144 return le16_to_cpu(bg->bg_flags);
147 static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
150 bg->bg_flags = cpu_to_le16(flags);
153 /* Block number of the block bitmap */
154 uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
155 const struct ext_filesystem *fs)
157 uint64_t block_nr = le32_to_cpu(bg->block_id);
158 if (fs->gdsize == 64)
159 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
163 /* Block number of the inode bitmap */
164 uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
165 const struct ext_filesystem *fs)
167 uint64_t block_nr = le32_to_cpu(bg->inode_id);
168 if (fs->gdsize == 64)
169 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
174 /* Block number of the inode table */
175 uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
176 const struct ext_filesystem *fs)
178 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
179 if (fs->gdsize == 64)
181 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
185 #if defined(CONFIG_EXT4_WRITE)
186 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
188 uint32_t res = size / n;
195 void put_ext4(uint64_t off, void *buf, uint32_t size)
199 unsigned char *temp_ptr = NULL;
200 struct ext_filesystem *fs = get_fs();
201 int log2blksz = fs->dev_desc->log2blksz;
202 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
204 startblock = off >> log2blksz;
205 startblock += part_offset;
206 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
208 if (fs->dev_desc == NULL)
211 if ((startblock + (size >> log2blksz)) >
212 (part_offset + fs->total_sect)) {
213 printf("part_offset is " LBAFU "\n", part_offset);
214 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
215 printf("error: overflow occurs\n");
220 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
222 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
223 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
225 if (size >> log2blksz != 0) {
226 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
227 (unsigned long *)buf);
229 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
231 memcpy(temp_ptr, buf, size);
232 blk_dwrite(fs->dev_desc, startblock, 1,
233 (unsigned long *)sec_buf);
238 static int _get_new_inode_no(unsigned char *buffer)
240 struct ext_filesystem *fs = get_fs();
246 /* get the blocksize of the filesystem */
247 unsigned char *ptr = buffer;
248 while (*ptr == 255) {
251 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
255 for (j = 0; j < fs->blksz; j++) {
260 status = input & operand;
275 static int _get_new_blk_no(unsigned char *buffer)
280 unsigned char *ptr = buffer;
281 struct ext_filesystem *fs = get_fs();
283 while (*ptr == 255) {
286 if (count == (fs->blksz * 8))
290 if (fs->blksz == 1024)
293 for (i = 0; i <= 7; i++) {
295 if (*ptr & operand) {
306 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
308 int i, remainder, status;
309 unsigned char *ptr = buffer;
310 unsigned char operand;
312 remainder = blockno % 8;
313 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
315 i = i - (index * blocksize);
316 if (blocksize != 1024) {
318 operand = 1 << remainder;
319 status = *ptr & operand;
323 *ptr = *ptr | operand;
326 if (remainder == 0) {
331 operand = (1 << (remainder - 1));
333 status = *ptr & operand;
337 *ptr = *ptr | operand;
342 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
344 int i, remainder, status;
345 unsigned char *ptr = buffer;
346 unsigned char operand;
348 remainder = blockno % 8;
349 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
351 i = i - (index * blocksize);
352 if (blocksize != 1024) {
354 operand = (1 << remainder);
355 status = *ptr & operand;
357 *ptr = *ptr & ~(operand);
359 if (remainder == 0) {
364 operand = (1 << (remainder - 1));
366 status = *ptr & operand;
368 *ptr = *ptr & ~(operand);
372 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
374 int i, remainder, status;
375 unsigned char *ptr = buffer;
376 unsigned char operand;
378 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
380 remainder = inode_no % 8;
381 if (remainder == 0) {
386 operand = (1 << (remainder - 1));
388 status = *ptr & operand;
392 *ptr = *ptr | operand;
397 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
399 int i, remainder, status;
400 unsigned char *ptr = buffer;
401 unsigned char operand;
403 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
405 remainder = inode_no % 8;
406 if (remainder == 0) {
411 operand = (1 << (remainder - 1));
413 status = *ptr & operand;
415 *ptr = *ptr & ~(operand);
418 uint16_t ext4fs_checksum_update(uint32_t i)
420 struct ext2_block_group *desc;
421 struct ext_filesystem *fs = get_fs();
423 __le32 le32_i = cpu_to_le32(i);
425 desc = ext4fs_get_group_descriptor(fs, i);
426 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
427 int offset = offsetof(struct ext2_block_group, bg_checksum);
429 crc = ext2fs_crc16(~0, fs->sb->unique_id,
430 sizeof(fs->sb->unique_id));
431 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
432 crc = ext2fs_crc16(crc, desc, offset);
433 offset += sizeof(desc->bg_checksum); /* skip checksum */
434 assert(offset == sizeof(*desc));
440 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
443 int sizeof_void_space;
444 int new_entry_byte_reqd;
445 short padding_factor = 0;
447 if (dir->namelen % 4 != 0)
448 padding_factor = 4 - (dir->namelen % 4);
450 dentry_length = sizeof(struct ext2_dirent) +
451 dir->namelen + padding_factor;
452 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
453 if (sizeof_void_space == 0)
457 if (strlen(filename) % 4 != 0)
458 padding_factor = 4 - (strlen(filename) % 4);
460 new_entry_byte_reqd = strlen(filename) +
461 sizeof(struct ext2_dirent) + padding_factor;
462 if (sizeof_void_space >= new_entry_byte_reqd) {
463 dir->direntlen = cpu_to_le16(dentry_length);
464 return sizeof_void_space;
470 int ext4fs_update_parent_dentry(char *filename, int file_type)
472 unsigned int *zero_buffer = NULL;
473 char *root_first_block_buffer = NULL;
475 long int first_block_no_of_root = 0;
477 unsigned int new_entry_byte_reqd;
478 int sizeof_void_space = 0;
482 struct ext_filesystem *fs = get_fs();
483 /* directory entry */
484 struct ext2_dirent *dir;
485 char *temp_dir = NULL;
488 uint32_t new_blockcnt;
489 uint32_t directory_blocks;
491 zero_buffer = zalloc(fs->blksz);
493 printf("No Memory\n");
496 root_first_block_buffer = zalloc(fs->blksz);
497 if (!root_first_block_buffer) {
499 printf("No Memory\n");
502 new_entry_byte_reqd = ROUND(strlen(filename) +
503 sizeof(struct ext2_dirent), 4);
505 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
506 LOG2_BLOCK_SIZE(ext4fs_root);
507 blk_idx = directory_blocks - 1;
510 /* read the block no allocated to a file */
511 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
512 if (first_block_no_of_root <= 0)
515 status = ext4fs_devread((lbaint_t)first_block_no_of_root
517 0, fs->blksz, root_first_block_buffer);
521 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
523 dir = (struct ext2_dirent *)root_first_block_buffer;
526 while (le16_to_cpu(dir->direntlen) > 0) {
527 unsigned short used_len = ROUND(dir->namelen +
528 sizeof(struct ext2_dirent), 4);
530 /* last entry of block */
531 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
533 /* check if new entry fits */
534 if ((used_len + new_entry_byte_reqd) <=
535 le16_to_cpu(dir->direntlen)) {
536 dir->direntlen = cpu_to_le16(used_len);
540 printf("Block full, trying previous\n");
544 printf("All blocks full: Allocate new\n");
546 if (le32_to_cpu(g_parent_inode->flags) &
548 printf("Directory uses extents\n");
551 if (directory_blocks >= INDIRECT_BLOCKS) {
552 printf("Directory exceeds limit\n");
555 new_blk_no = ext4fs_get_new_blk_no();
556 if (new_blk_no == -1) {
557 printf("no block left to assign\n");
560 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
561 g_parent_inode->b.blocks.
562 dir_blocks[directory_blocks] =
563 cpu_to_le32(new_blk_no);
565 new_size = le32_to_cpu(g_parent_inode->size);
566 new_size += fs->blksz;
567 g_parent_inode->size = cpu_to_le32(new_size);
569 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
570 new_blockcnt += fs->sect_perblk;
571 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
573 if (ext4fs_put_metadata
574 (root_first_block_buffer,
575 first_block_no_of_root))
581 templength = le16_to_cpu(dir->direntlen);
582 totalbytes = totalbytes + templength;
583 sizeof_void_space = check_void_in_dentry(dir, filename);
584 if (sizeof_void_space)
587 dir = (struct ext2_dirent *)((char *)dir + templength);
590 /* make a pointer ready for creating next directory entry */
591 templength = le16_to_cpu(dir->direntlen);
592 totalbytes = totalbytes + templength;
593 dir = (struct ext2_dirent *)((char *)dir + templength);
595 /* get the next available inode number */
596 inodeno = ext4fs_get_new_inode_no();
598 printf("no inode left to assign\n");
601 dir->inode = cpu_to_le32(inodeno);
602 if (sizeof_void_space)
603 dir->direntlen = cpu_to_le16(sizeof_void_space);
605 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
607 dir->namelen = strlen(filename);
608 dir->filetype = FILETYPE_REG; /* regular file */
609 temp_dir = (char *)dir;
610 temp_dir = temp_dir + sizeof(struct ext2_dirent);
611 memcpy(temp_dir, filename, strlen(filename));
613 /* update or write the 1st block of root inode */
614 if (ext4fs_put_metadata(root_first_block_buffer,
615 first_block_no_of_root))
620 free(root_first_block_buffer);
625 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
632 char *block_buffer = NULL;
633 struct ext2_dirent *dir = NULL;
634 struct ext_filesystem *fs = get_fs();
635 uint32_t directory_blocks;
638 directory_blocks = le32_to_cpu(parent_inode->size) >>
639 LOG2_BLOCK_SIZE(ext4fs_root);
641 block_buffer = zalloc(fs->blksz);
645 /* get the block no allocated to a file */
646 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
647 blknr = read_allocated_block(parent_inode, blk_idx);
651 /* read the directory block */
652 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
653 0, fs->blksz, (char *)block_buffer);
659 dir = (struct ext2_dirent *)(block_buffer + offset);
660 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
662 int direntlen = le16_to_cpu(dir->direntlen);
663 if (direntlen < sizeof(struct ext2_dirent))
666 if (dir->inode && (strlen(dirname) == dir->namelen) &&
667 (strncmp(dirname, direntname, dir->namelen) == 0)) {
668 inodeno = le32_to_cpu(dir->inode);
674 } while (offset < fs->blksz);
688 static int find_dir_depth(char *dirname)
690 char *token = strtok(dirname, "/");
692 while (token != NULL) {
693 token = strtok(NULL, "/");
696 return count + 1 + 1;
698 * for example for string /home/temp
699 * depth=home(1)+temp(1)+1 extra for NULL;
704 static int parse_path(char **arr, char *dirname)
706 char *token = strtok(dirname, "/");
710 arr[i] = zalloc(strlen("/") + 1);
713 memcpy(arr[i++], "/", strlen("/"));
715 /* add each path entry after root */
716 while (token != NULL) {
717 arr[i] = zalloc(strlen(token) + 1);
720 memcpy(arr[i++], token, strlen(token));
721 token = strtok(NULL, "/");
728 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
730 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
737 * Function: ext4fs_get_parent_inode_num
738 * Return Value: inode Number of the parent directory of file/Directory to be
740 * dirname : Input parmater, input path name of the file/directory to be created
741 * dname : Output parameter, to be filled with the name of the directory
742 * extracted from dirname
744 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
748 int matched_inode_no;
749 int result_inode_no = -1;
751 char *depth_dirname = NULL;
752 char *parse_dirname = NULL;
753 struct ext2_inode *parent_inode = NULL;
754 struct ext2_inode *first_inode = NULL;
755 struct ext2_inode temp_inode;
757 if (*dirname != '/') {
758 printf("Please supply Absolute path\n");
762 /* TODO: input validation make equivalent to linux */
763 depth_dirname = zalloc(strlen(dirname) + 1);
767 memcpy(depth_dirname, dirname, strlen(dirname));
768 depth = find_dir_depth(depth_dirname);
769 parse_dirname = zalloc(strlen(dirname) + 1);
772 memcpy(parse_dirname, dirname, strlen(dirname));
774 /* allocate memory for each directory level */
775 ptr = zalloc((depth) * sizeof(char *));
778 if (parse_path(ptr, parse_dirname))
780 parent_inode = zalloc(sizeof(struct ext2_inode));
783 first_inode = zalloc(sizeof(struct ext2_inode));
786 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
787 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
789 result_inode_no = EXT2_ROOT_INO;
790 for (i = 1; i < depth; i++) {
791 matched_inode_no = search_dir(parent_inode, ptr[i]);
792 if (matched_inode_no == -1) {
793 if (ptr[i + 1] == NULL && i == 1) {
794 result_inode_no = EXT2_ROOT_INO;
797 if (ptr[i + 1] == NULL)
799 printf("Invalid path\n");
800 result_inode_no = -1;
804 if (ptr[i + 1] != NULL) {
805 memset(parent_inode, '\0',
806 sizeof(struct ext2_inode));
807 if (ext4fs_iget(matched_inode_no,
809 result_inode_no = -1;
812 result_inode_no = matched_inode_no;
821 matched_inode_no = search_dir(first_inode, ptr[i]);
823 matched_inode_no = search_dir(parent_inode, ptr[i]);
825 if (matched_inode_no != -1) {
826 ext4fs_iget(matched_inode_no, &temp_inode);
827 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
828 printf("It is a Directory\n");
829 result_inode_no = -1;
834 if (strlen(ptr[i]) > 256) {
835 result_inode_no = -1;
838 memcpy(dname, ptr[i], strlen(ptr[i]));
843 for (i = 0; i < depth; i++) {
852 return result_inode_no;
855 static int unlink_filename(char *filename, unsigned int blknr)
860 char *block_buffer = NULL;
861 struct ext2_dirent *dir = NULL;
862 struct ext2_dirent *previous_dir;
863 struct ext_filesystem *fs = get_fs();
867 block_buffer = zalloc(fs->blksz);
871 /* read the directory block */
872 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
873 fs->blksz, block_buffer);
877 if (ext4fs_log_journal(block_buffer, blknr))
882 dir = (struct ext2_dirent *)(block_buffer + offset);
883 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
885 int direntlen = le16_to_cpu(dir->direntlen);
886 if (direntlen < sizeof(struct ext2_dirent))
889 if (dir->inode && (strlen(filename) == dir->namelen) &&
890 (strncmp(direntname, filename, dir->namelen) == 0)) {
891 inodeno = le32_to_cpu(dir->inode);
894 new_len = le16_to_cpu(previous_dir->direntlen);
895 new_len += le16_to_cpu(dir->direntlen);
896 previous_dir->direntlen = cpu_to_le16(new_len);
905 } while (offset < fs->blksz);
909 if (ext4fs_put_metadata(block_buffer, blknr))
919 int ext4fs_filename_unlink(char *filename)
924 uint32_t directory_blocks;
926 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
927 LOG2_BLOCK_SIZE(ext4fs_root);
929 /* read the block no allocated to a file */
930 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
931 blknr = read_allocated_block(g_parent_inode, blk_idx);
934 inodeno = unlink_filename(filename, blknr);
942 uint32_t ext4fs_get_new_blk_no(void)
948 static int prev_bg_bitmap_index = -1;
949 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
950 struct ext_filesystem *fs = get_fs();
951 char *journal_buffer = zalloc(fs->blksz);
952 char *zero_buffer = zalloc(fs->blksz);
953 if (!journal_buffer || !zero_buffer)
956 if (fs->first_pass_bbmap == 0) {
957 for (i = 0; i < fs->no_blkgrp; i++) {
958 struct ext2_block_group *bgd = NULL;
959 bgd = ext4fs_get_group_descriptor(fs, i);
960 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
961 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
962 uint64_t b_bitmap_blk =
963 ext4fs_bg_get_block_id(bgd, fs);
964 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
965 memcpy(fs->blk_bmaps[i], zero_buffer,
967 put_ext4(b_bitmap_blk * fs->blksz,
968 fs->blk_bmaps[i], fs->blksz);
969 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
970 ext4fs_bg_set_flags(bgd, bg_flags);
973 _get_new_blk_no(fs->blk_bmaps[i]);
974 if (fs->curr_blkno == -1)
975 /* block bitmap is completely filled */
977 fs->curr_blkno = fs->curr_blkno +
979 fs->first_pass_bbmap++;
980 ext4fs_bg_free_blocks_dec(bgd, fs);
981 ext4fs_sb_free_blocks_dec(fs->sb);
982 status = ext4fs_devread(b_bitmap_blk *
988 if (ext4fs_log_journal(journal_buffer,
993 debug("no space left on block group %d\n", i);
1001 /* get the blockbitmap index respective to blockno */
1002 bg_idx = fs->curr_blkno / blk_per_grp;
1003 if (fs->blksz == 1024) {
1004 remainder = fs->curr_blkno % blk_per_grp;
1010 * To skip completely filled block group bitmaps
1011 * Optimize the block allocation
1013 if (bg_idx >= fs->no_blkgrp)
1016 struct ext2_block_group *bgd = NULL;
1017 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1018 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
1019 debug("block group %u is full. Skipping\n", bg_idx);
1020 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1021 if (fs->blksz == 1024)
1022 fs->curr_blkno += 1;
1026 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1027 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1028 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
1029 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1030 put_ext4(b_bitmap_blk * fs->blksz,
1031 zero_buffer, fs->blksz);
1032 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1033 ext4fs_bg_set_flags(bgd, bg_flags);
1036 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1038 debug("going for restart for the block no %ld %u\n",
1039 fs->curr_blkno, bg_idx);
1044 /* journal backup */
1045 if (prev_bg_bitmap_index != bg_idx) {
1046 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
1047 0, fs->blksz, journal_buffer);
1050 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
1053 prev_bg_bitmap_index = bg_idx;
1055 ext4fs_bg_free_blocks_dec(bgd, fs);
1056 ext4fs_sb_free_blocks_dec(fs->sb);
1060 free(journal_buffer);
1063 return fs->curr_blkno;
1065 free(journal_buffer);
1071 int ext4fs_get_new_inode_no(void)
1075 unsigned int ibmap_idx;
1076 static int prev_inode_bitmap_index = -1;
1077 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1078 struct ext_filesystem *fs = get_fs();
1079 char *journal_buffer = zalloc(fs->blksz);
1080 char *zero_buffer = zalloc(fs->blksz);
1081 if (!journal_buffer || !zero_buffer)
1083 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1084 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
1086 if (fs->first_pass_ibmap == 0) {
1087 for (i = 0; i < fs->no_blkgrp; i++) {
1088 uint32_t free_inodes;
1089 struct ext2_block_group *bgd = NULL;
1090 bgd = ext4fs_get_group_descriptor(fs, i);
1091 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1093 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1094 uint64_t i_bitmap_blk =
1095 ext4fs_bg_get_inode_id(bgd, fs);
1097 bgd->bg_itable_unused = free_inodes;
1098 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1099 put_ext4(i_bitmap_blk * fs->blksz,
1100 zero_buffer, fs->blksz);
1101 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1102 ext4fs_bg_set_flags(bgd, bg_flags);
1103 memcpy(fs->inode_bmaps[i],
1104 zero_buffer, fs->blksz);
1107 _get_new_inode_no(fs->inode_bmaps[i]);
1108 if (fs->curr_inode_no == -1)
1109 /* inode bitmap is completely filled */
1111 fs->curr_inode_no = fs->curr_inode_no +
1112 (i * inodes_per_grp);
1113 fs->first_pass_ibmap++;
1114 ext4fs_bg_free_inodes_dec(bgd, fs);
1116 ext4fs_bg_itable_unused_dec(bgd, fs);
1117 ext4fs_sb_free_inodes_dec(fs->sb);
1118 status = ext4fs_devread(i_bitmap_blk *
1124 if (ext4fs_log_journal(journal_buffer,
1129 debug("no inode left on block group %d\n", i);
1134 fs->curr_inode_no++;
1135 /* get the blockbitmap index respective to blockno */
1136 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1137 struct ext2_block_group *bgd =
1138 ext4fs_get_group_descriptor(fs, ibmap_idx);
1139 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1140 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1142 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1143 put_ext4(i_bitmap_blk * fs->blksz,
1144 zero_buffer, fs->blksz);
1145 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1146 ext4fs_bg_set_flags(bgd, bg_flags);
1147 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1151 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1152 fs->inode_bmaps[ibmap_idx],
1154 debug("going for restart for the block no %d %u\n",
1155 fs->curr_inode_no, ibmap_idx);
1159 /* journal backup */
1160 if (prev_inode_bitmap_index != ibmap_idx) {
1161 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
1162 0, fs->blksz, journal_buffer);
1165 if (ext4fs_log_journal(journal_buffer,
1166 le32_to_cpu(bgd->inode_id)))
1168 prev_inode_bitmap_index = ibmap_idx;
1170 ext4fs_bg_free_inodes_dec(bgd, fs);
1172 bgd->bg_itable_unused = bgd->free_inodes;
1173 ext4fs_sb_free_inodes_dec(fs->sb);
1178 free(journal_buffer);
1181 return fs->curr_inode_no;
1183 free(journal_buffer);
1191 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1192 unsigned int *total_remaining_blocks,
1193 unsigned int *no_blks_reqd)
1197 long int actual_block_no;
1198 long int si_blockno;
1199 /* si :single indirect */
1200 __le32 *si_buffer = NULL;
1201 __le32 *si_start_addr = NULL;
1202 struct ext_filesystem *fs = get_fs();
1204 if (*total_remaining_blocks != 0) {
1205 si_buffer = zalloc(fs->blksz);
1207 printf("No Memory\n");
1210 si_start_addr = si_buffer;
1211 si_blockno = ext4fs_get_new_blk_no();
1212 if (si_blockno == -1) {
1213 printf("no block left to assign\n");
1217 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1219 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1220 0, fs->blksz, (char *)si_buffer);
1221 memset(si_buffer, '\0', fs->blksz);
1225 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1226 actual_block_no = ext4fs_get_new_blk_no();
1227 if (actual_block_no == -1) {
1228 printf("no block left to assign\n");
1231 *si_buffer = cpu_to_le32(actual_block_no);
1232 debug("SIAB %u: %u\n", *si_buffer,
1233 *total_remaining_blocks);
1236 (*total_remaining_blocks)--;
1237 if (*total_remaining_blocks == 0)
1241 /* write the block to disk */
1242 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1243 si_start_addr, fs->blksz);
1244 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1247 free(si_start_addr);
1250 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1251 unsigned int *total_remaining_blocks,
1252 unsigned int *no_blks_reqd)
1257 long int actual_block_no;
1258 /* di:double indirect */
1259 long int di_blockno_parent;
1260 long int di_blockno_child;
1261 __le32 *di_parent_buffer = NULL;
1262 __le32 *di_child_buff = NULL;
1263 __le32 *di_block_start_addr = NULL;
1264 __le32 *di_child_buff_start = NULL;
1265 struct ext_filesystem *fs = get_fs();
1267 if (*total_remaining_blocks != 0) {
1268 /* double indirect parent block connecting to inode */
1269 di_blockno_parent = ext4fs_get_new_blk_no();
1270 if (di_blockno_parent == -1) {
1271 printf("no block left to assign\n");
1274 di_parent_buffer = zalloc(fs->blksz);
1275 if (!di_parent_buffer)
1278 di_block_start_addr = di_parent_buffer;
1280 debug("DIPB %ld: %u\n", di_blockno_parent,
1281 *total_remaining_blocks);
1283 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1285 fs->blksz, (char *)di_parent_buffer);
1288 printf("%s: Device read error!\n", __func__);
1291 memset(di_parent_buffer, '\0', fs->blksz);
1294 * start:for each double indirect parent
1295 * block create one more block
1297 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1298 di_blockno_child = ext4fs_get_new_blk_no();
1299 if (di_blockno_child == -1) {
1300 printf("no block left to assign\n");
1303 di_child_buff = zalloc(fs->blksz);
1307 di_child_buff_start = di_child_buff;
1308 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1311 debug("DICB %ld: %u\n", di_blockno_child,
1312 *total_remaining_blocks);
1314 status = ext4fs_devread((lbaint_t)di_blockno_child *
1317 (char *)di_child_buff);
1320 printf("%s: Device read error!\n", __func__);
1323 memset(di_child_buff, '\0', fs->blksz);
1324 /* filling of actual datablocks for each child */
1325 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1326 actual_block_no = ext4fs_get_new_blk_no();
1327 if (actual_block_no == -1) {
1328 printf("no block left to assign\n");
1331 *di_child_buff = cpu_to_le32(actual_block_no);
1332 debug("DIAB %ld: %u\n", actual_block_no,
1333 *total_remaining_blocks);
1336 (*total_remaining_blocks)--;
1337 if (*total_remaining_blocks == 0)
1340 /* write the block table */
1341 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1342 di_child_buff_start, fs->blksz);
1343 free(di_child_buff_start);
1344 di_child_buff_start = NULL;
1346 if (*total_remaining_blocks == 0)
1349 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1350 di_block_start_addr, fs->blksz);
1351 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1354 free(di_block_start_addr);
1357 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1358 unsigned int *total_remaining_blocks,
1359 unsigned int *no_blks_reqd)
1364 long int actual_block_no;
1365 /* ti: Triple Indirect */
1366 long int ti_gp_blockno;
1367 long int ti_parent_blockno;
1368 long int ti_child_blockno;
1369 __le32 *ti_gp_buff = NULL;
1370 __le32 *ti_parent_buff = NULL;
1371 __le32 *ti_child_buff = NULL;
1372 __le32 *ti_gp_buff_start_addr = NULL;
1373 __le32 *ti_pbuff_start_addr = NULL;
1374 __le32 *ti_cbuff_start_addr = NULL;
1375 struct ext_filesystem *fs = get_fs();
1376 if (*total_remaining_blocks != 0) {
1377 /* triple indirect grand parent block connecting to inode */
1378 ti_gp_blockno = ext4fs_get_new_blk_no();
1379 if (ti_gp_blockno == -1) {
1380 printf("no block left to assign\n");
1383 ti_gp_buff = zalloc(fs->blksz);
1387 ti_gp_buff_start_addr = ti_gp_buff;
1389 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1390 *total_remaining_blocks);
1392 /* for each 4 byte grand parent entry create one more block */
1393 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1394 ti_parent_blockno = ext4fs_get_new_blk_no();
1395 if (ti_parent_blockno == -1) {
1396 printf("no block left to assign\n");
1399 ti_parent_buff = zalloc(fs->blksz);
1400 if (!ti_parent_buff)
1403 ti_pbuff_start_addr = ti_parent_buff;
1404 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1407 debug("TIPB %ld: %u\n", ti_parent_blockno,
1408 *total_remaining_blocks);
1410 /* for each 4 byte entry parent create one more block */
1411 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1412 ti_child_blockno = ext4fs_get_new_blk_no();
1413 if (ti_child_blockno == -1) {
1414 printf("no block left assign\n");
1417 ti_child_buff = zalloc(fs->blksz);
1421 ti_cbuff_start_addr = ti_child_buff;
1422 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1425 debug("TICB %ld: %u\n", ti_parent_blockno,
1426 *total_remaining_blocks);
1428 /* fill actual datablocks for each child */
1429 for (k = 0; k < (fs->blksz / sizeof(int));
1432 ext4fs_get_new_blk_no();
1433 if (actual_block_no == -1) {
1434 printf("no block left\n");
1435 free(ti_cbuff_start_addr);
1438 *ti_child_buff = cpu_to_le32(actual_block_no);
1439 debug("TIAB %ld: %u\n", actual_block_no,
1440 *total_remaining_blocks);
1443 (*total_remaining_blocks)--;
1444 if (*total_remaining_blocks == 0)
1447 /* write the child block */
1448 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1449 (uint64_t)fs->blksz)),
1450 ti_cbuff_start_addr, fs->blksz);
1451 free(ti_cbuff_start_addr);
1453 if (*total_remaining_blocks == 0)
1456 /* write the parent block */
1457 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1458 ti_pbuff_start_addr, fs->blksz);
1459 free(ti_pbuff_start_addr);
1461 if (*total_remaining_blocks == 0)
1464 /* write the grand parent block */
1465 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1466 ti_gp_buff_start_addr, fs->blksz);
1467 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1468 free(ti_gp_buff_start_addr);
1472 free(ti_pbuff_start_addr);
1474 free(ti_gp_buff_start_addr);
1477 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1478 unsigned int total_remaining_blocks,
1479 unsigned int *total_no_of_block)
1482 long int direct_blockno;
1483 unsigned int no_blks_reqd = 0;
1485 /* allocation of direct blocks */
1486 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1487 direct_blockno = ext4fs_get_new_blk_no();
1488 if (direct_blockno == -1) {
1489 printf("no block left to assign\n");
1492 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1493 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1495 total_remaining_blocks--;
1498 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1500 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1502 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1504 *total_no_of_block += no_blks_reqd;
1509 static struct ext4_extent_header *ext4fs_get_extent_block
1510 (struct ext2_data *data, char *buf,
1511 struct ext4_extent_header *ext_block,
1512 uint32_t fileblock, int log2_blksz)
1514 struct ext4_extent_idx *index;
1515 unsigned long long block;
1516 int blksz = EXT2_BLOCK_SIZE(data);
1520 index = (struct ext4_extent_idx *)(ext_block + 1);
1522 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1525 if (ext_block->eh_depth == 0)
1530 if (i >= le16_to_cpu(ext_block->eh_entries))
1532 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1537 block = le16_to_cpu(index[i].ei_leaf_hi);
1538 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1540 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1542 ext_block = (struct ext4_extent_header *)buf;
1548 static int ext4fs_blockgroup
1549 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1552 unsigned int blkoff, desc_per_blk;
1553 int log2blksz = get_fs()->dev_desc->log2blksz;
1554 int desc_size = get_fs()->gdsize;
1556 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
1558 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1559 group / desc_per_blk;
1560 blkoff = (group % desc_per_blk) * desc_size;
1562 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1563 group, blkno, blkoff);
1565 return ext4fs_devread((lbaint_t)blkno <<
1566 (LOG2_BLOCK_SIZE(data) - log2blksz),
1567 blkoff, desc_size, (char *)blkgrp);
1570 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1572 struct ext2_block_group blkgrp;
1573 struct ext2_sblock *sblock = &data->sblock;
1574 struct ext_filesystem *fs = get_fs();
1575 int log2blksz = get_fs()->dev_desc->log2blksz;
1576 int inodes_per_block, status;
1578 unsigned int blkoff;
1580 /* It is easier to calculate if the first inode is 0. */
1582 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1583 (sblock->inodes_per_group), &blkgrp);
1587 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1588 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
1589 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1590 blkoff = (ino % inodes_per_block) * fs->inodesz;
1591 /* Read the inode. */
1592 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1594 sizeof(struct ext2_inode), (char *)inode);
1601 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1608 long int perblock_parent;
1609 long int perblock_child;
1610 unsigned long long start;
1611 /* get the blocksize of the filesystem */
1612 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1613 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1614 - get_fs()->dev_desc->log2blksz;
1616 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1617 char *buf = zalloc(blksz);
1620 struct ext4_extent_header *ext_block;
1621 struct ext4_extent *extent;
1624 ext4fs_get_extent_block(ext4fs_root, buf,
1625 (struct ext4_extent_header *)
1626 inode->b.blocks.dir_blocks,
1627 fileblock, log2_blksz);
1629 printf("invalid extent block\n");
1634 extent = (struct ext4_extent *)(ext_block + 1);
1638 if (i >= le16_to_cpu(ext_block->eh_entries))
1640 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1642 fileblock -= le32_to_cpu(extent[i].ee_block);
1643 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1648 start = le16_to_cpu(extent[i].ee_start_hi);
1649 start = (start << 32) +
1650 le32_to_cpu(extent[i].ee_start_lo);
1652 return fileblock + start;
1655 printf("Extent Error\n");
1660 /* Direct blocks. */
1661 if (fileblock < INDIRECT_BLOCKS)
1662 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1665 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1666 if (ext4fs_indir1_block == NULL) {
1667 ext4fs_indir1_block = zalloc(blksz);
1668 if (ext4fs_indir1_block == NULL) {
1669 printf("** SI ext2fs read block (indir 1)"
1670 "malloc failed. **\n");
1673 ext4fs_indir1_size = blksz;
1674 ext4fs_indir1_blkno = -1;
1676 if (blksz != ext4fs_indir1_size) {
1677 free(ext4fs_indir1_block);
1678 ext4fs_indir1_block = NULL;
1679 ext4fs_indir1_size = 0;
1680 ext4fs_indir1_blkno = -1;
1681 ext4fs_indir1_block = zalloc(blksz);
1682 if (ext4fs_indir1_block == NULL) {
1683 printf("** SI ext2fs read block (indir 1):"
1684 "malloc failed. **\n");
1687 ext4fs_indir1_size = blksz;
1689 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1690 log2_blksz) != ext4fs_indir1_blkno) {
1692 ext4fs_devread((lbaint_t)le32_to_cpu
1694 indir_block) << log2_blksz, 0,
1695 blksz, (char *)ext4fs_indir1_block);
1697 printf("** SI ext2fs read block (indir 1)"
1701 ext4fs_indir1_blkno =
1702 le32_to_cpu(inode->b.blocks.
1703 indir_block) << log2_blksz;
1705 blknr = le32_to_cpu(ext4fs_indir1_block
1706 [fileblock - INDIRECT_BLOCKS]);
1708 /* Double indirect. */
1709 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1710 (blksz / 4 + 1)))) {
1712 long int perblock = blksz / 4;
1713 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1715 if (ext4fs_indir1_block == NULL) {
1716 ext4fs_indir1_block = zalloc(blksz);
1717 if (ext4fs_indir1_block == NULL) {
1718 printf("** DI ext2fs read block (indir 2 1)"
1719 "malloc failed. **\n");
1722 ext4fs_indir1_size = blksz;
1723 ext4fs_indir1_blkno = -1;
1725 if (blksz != ext4fs_indir1_size) {
1726 free(ext4fs_indir1_block);
1727 ext4fs_indir1_block = NULL;
1728 ext4fs_indir1_size = 0;
1729 ext4fs_indir1_blkno = -1;
1730 ext4fs_indir1_block = zalloc(blksz);
1731 if (ext4fs_indir1_block == NULL) {
1732 printf("** DI ext2fs read block (indir 2 1)"
1733 "malloc failed. **\n");
1736 ext4fs_indir1_size = blksz;
1738 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1739 log2_blksz) != ext4fs_indir1_blkno) {
1741 ext4fs_devread((lbaint_t)le32_to_cpu
1743 double_indir_block) << log2_blksz,
1745 (char *)ext4fs_indir1_block);
1747 printf("** DI ext2fs read block (indir 2 1)"
1751 ext4fs_indir1_blkno =
1752 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1756 if (ext4fs_indir2_block == NULL) {
1757 ext4fs_indir2_block = zalloc(blksz);
1758 if (ext4fs_indir2_block == NULL) {
1759 printf("** DI ext2fs read block (indir 2 2)"
1760 "malloc failed. **\n");
1763 ext4fs_indir2_size = blksz;
1764 ext4fs_indir2_blkno = -1;
1766 if (blksz != ext4fs_indir2_size) {
1767 free(ext4fs_indir2_block);
1768 ext4fs_indir2_block = NULL;
1769 ext4fs_indir2_size = 0;
1770 ext4fs_indir2_blkno = -1;
1771 ext4fs_indir2_block = zalloc(blksz);
1772 if (ext4fs_indir2_block == NULL) {
1773 printf("** DI ext2fs read block (indir 2 2)"
1774 "malloc failed. **\n");
1777 ext4fs_indir2_size = blksz;
1779 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1780 log2_blksz) != ext4fs_indir2_blkno) {
1781 status = ext4fs_devread((lbaint_t)le32_to_cpu
1782 (ext4fs_indir1_block
1784 perblock]) << log2_blksz, 0,
1786 (char *)ext4fs_indir2_block);
1788 printf("** DI ext2fs read block (indir 2 2)"
1792 ext4fs_indir2_blkno =
1793 le32_to_cpu(ext4fs_indir1_block[rblock
1798 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1800 /* Tripple indirect. */
1802 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1803 (blksz / 4 * blksz / 4));
1804 perblock_child = blksz / 4;
1805 perblock_parent = ((blksz / 4) * (blksz / 4));
1807 if (ext4fs_indir1_block == NULL) {
1808 ext4fs_indir1_block = zalloc(blksz);
1809 if (ext4fs_indir1_block == NULL) {
1810 printf("** TI ext2fs read block (indir 2 1)"
1811 "malloc failed. **\n");
1814 ext4fs_indir1_size = blksz;
1815 ext4fs_indir1_blkno = -1;
1817 if (blksz != ext4fs_indir1_size) {
1818 free(ext4fs_indir1_block);
1819 ext4fs_indir1_block = NULL;
1820 ext4fs_indir1_size = 0;
1821 ext4fs_indir1_blkno = -1;
1822 ext4fs_indir1_block = zalloc(blksz);
1823 if (ext4fs_indir1_block == NULL) {
1824 printf("** TI ext2fs read block (indir 2 1)"
1825 "malloc failed. **\n");
1828 ext4fs_indir1_size = blksz;
1830 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1831 log2_blksz) != ext4fs_indir1_blkno) {
1832 status = ext4fs_devread
1834 le32_to_cpu(inode->b.blocks.triple_indir_block)
1835 << log2_blksz, 0, blksz,
1836 (char *)ext4fs_indir1_block);
1838 printf("** TI ext2fs read block (indir 2 1)"
1842 ext4fs_indir1_blkno =
1843 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1847 if (ext4fs_indir2_block == NULL) {
1848 ext4fs_indir2_block = zalloc(blksz);
1849 if (ext4fs_indir2_block == NULL) {
1850 printf("** TI ext2fs read block (indir 2 2)"
1851 "malloc failed. **\n");
1854 ext4fs_indir2_size = blksz;
1855 ext4fs_indir2_blkno = -1;
1857 if (blksz != ext4fs_indir2_size) {
1858 free(ext4fs_indir2_block);
1859 ext4fs_indir2_block = NULL;
1860 ext4fs_indir2_size = 0;
1861 ext4fs_indir2_blkno = -1;
1862 ext4fs_indir2_block = zalloc(blksz);
1863 if (ext4fs_indir2_block == NULL) {
1864 printf("** TI ext2fs read block (indir 2 2)"
1865 "malloc failed. **\n");
1868 ext4fs_indir2_size = blksz;
1870 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1871 perblock_parent]) <<
1873 != ext4fs_indir2_blkno) {
1874 status = ext4fs_devread((lbaint_t)le32_to_cpu
1875 (ext4fs_indir1_block
1877 perblock_parent]) <<
1878 log2_blksz, 0, blksz,
1879 (char *)ext4fs_indir2_block);
1881 printf("** TI ext2fs read block (indir 2 2)"
1885 ext4fs_indir2_blkno =
1886 le32_to_cpu(ext4fs_indir1_block[rblock /
1891 if (ext4fs_indir3_block == NULL) {
1892 ext4fs_indir3_block = zalloc(blksz);
1893 if (ext4fs_indir3_block == NULL) {
1894 printf("** TI ext2fs read block (indir 2 2)"
1895 "malloc failed. **\n");
1898 ext4fs_indir3_size = blksz;
1899 ext4fs_indir3_blkno = -1;
1901 if (blksz != ext4fs_indir3_size) {
1902 free(ext4fs_indir3_block);
1903 ext4fs_indir3_block = NULL;
1904 ext4fs_indir3_size = 0;
1905 ext4fs_indir3_blkno = -1;
1906 ext4fs_indir3_block = zalloc(blksz);
1907 if (ext4fs_indir3_block == NULL) {
1908 printf("** TI ext2fs read block (indir 2 2)"
1909 "malloc failed. **\n");
1912 ext4fs_indir3_size = blksz;
1914 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1917 log2_blksz) != ext4fs_indir3_blkno) {
1919 ext4fs_devread((lbaint_t)le32_to_cpu
1920 (ext4fs_indir2_block
1921 [(rblock / perblock_child)
1922 % (blksz / 4)]) << log2_blksz, 0,
1923 blksz, (char *)ext4fs_indir3_block);
1925 printf("** TI ext2fs read block (indir 2 2)"
1929 ext4fs_indir3_blkno =
1930 le32_to_cpu(ext4fs_indir2_block[(rblock /
1937 blknr = le32_to_cpu(ext4fs_indir3_block
1938 [rblock % perblock_child]);
1940 debug("read_allocated_block %ld\n", blknr);
1946 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1949 * This function assures that for a file with the same name but different size
1950 * the sequential store on the ext4 filesystem will be correct.
1952 * In this function the global data, responsible for internal representation
1953 * of the ext4 data are initialized to the reset state. Without this, during
1954 * replacement of the smaller file with the bigger truncation of new file was
1957 void ext4fs_reinit_global(void)
1959 if (ext4fs_indir1_block != NULL) {
1960 free(ext4fs_indir1_block);
1961 ext4fs_indir1_block = NULL;
1962 ext4fs_indir1_size = 0;
1963 ext4fs_indir1_blkno = -1;
1965 if (ext4fs_indir2_block != NULL) {
1966 free(ext4fs_indir2_block);
1967 ext4fs_indir2_block = NULL;
1968 ext4fs_indir2_size = 0;
1969 ext4fs_indir2_blkno = -1;
1971 if (ext4fs_indir3_block != NULL) {
1972 free(ext4fs_indir3_block);
1973 ext4fs_indir3_block = NULL;
1974 ext4fs_indir3_size = 0;
1975 ext4fs_indir3_blkno = -1;
1978 void ext4fs_close(void)
1980 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1981 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1984 if (ext4fs_root != NULL) {
1989 ext4fs_reinit_global();
1992 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1993 struct ext2fs_node **fnode, int *ftype)
1995 unsigned int fpos = 0;
1998 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2002 printf("Iterate dir %s\n", name);
2003 #endif /* of DEBUG */
2004 if (!diro->inode_read) {
2005 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2009 /* Search the file. */
2010 while (fpos < le32_to_cpu(diro->inode.size)) {
2011 struct ext2_dirent dirent;
2013 status = ext4fs_read_file(diro, fpos,
2014 sizeof(struct ext2_dirent),
2015 (char *)&dirent, &actread);
2019 if (dirent.direntlen == 0) {
2020 printf("Failed to iterate over directory %s\n", name);
2024 if (dirent.namelen != 0) {
2025 char filename[dirent.namelen + 1];
2026 struct ext2fs_node *fdiro;
2027 int type = FILETYPE_UNKNOWN;
2029 status = ext4fs_read_file(diro,
2031 sizeof(struct ext2_dirent),
2032 dirent.namelen, filename,
2037 fdiro = zalloc(sizeof(struct ext2fs_node));
2041 fdiro->data = diro->data;
2042 fdiro->ino = le32_to_cpu(dirent.inode);
2044 filename[dirent.namelen] = '\0';
2046 if (dirent.filetype != FILETYPE_UNKNOWN) {
2047 fdiro->inode_read = 0;
2049 if (dirent.filetype == FILETYPE_DIRECTORY)
2050 type = FILETYPE_DIRECTORY;
2051 else if (dirent.filetype == FILETYPE_SYMLINK)
2052 type = FILETYPE_SYMLINK;
2053 else if (dirent.filetype == FILETYPE_REG)
2054 type = FILETYPE_REG;
2056 status = ext4fs_read_inode(diro->data,
2064 fdiro->inode_read = 1;
2066 if ((le16_to_cpu(fdiro->inode.mode) &
2067 FILETYPE_INO_MASK) ==
2068 FILETYPE_INO_DIRECTORY) {
2069 type = FILETYPE_DIRECTORY;
2070 } else if ((le16_to_cpu(fdiro->inode.mode)
2071 & FILETYPE_INO_MASK) ==
2072 FILETYPE_INO_SYMLINK) {
2073 type = FILETYPE_SYMLINK;
2074 } else if ((le16_to_cpu(fdiro->inode.mode)
2075 & FILETYPE_INO_MASK) ==
2077 type = FILETYPE_REG;
2081 printf("iterate >%s<\n", filename);
2082 #endif /* of DEBUG */
2083 if ((name != NULL) && (fnode != NULL)
2084 && (ftype != NULL)) {
2085 if (strcmp(filename, name) == 0) {
2091 if (fdiro->inode_read == 0) {
2092 status = ext4fs_read_inode(diro->data,
2100 fdiro->inode_read = 1;
2103 case FILETYPE_DIRECTORY:
2106 case FILETYPE_SYMLINK:
2117 le32_to_cpu(fdiro->inode.size),
2122 fpos += le16_to_cpu(dirent.direntlen);
2127 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2130 struct ext2fs_node *diro = node;
2134 if (!diro->inode_read) {
2135 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2139 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2143 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2144 strncpy(symlink, diro->inode.b.symlink,
2145 le32_to_cpu(diro->inode.size));
2147 status = ext4fs_read_file(diro, 0,
2148 le32_to_cpu(diro->inode.size),
2150 if ((status < 0) || (actread == 0)) {
2155 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2159 static int ext4fs_find_file1(const char *currpath,
2160 struct ext2fs_node *currroot,
2161 struct ext2fs_node **currfound, int *foundtype)
2163 char fpath[strlen(currpath) + 1];
2167 int type = FILETYPE_DIRECTORY;
2168 struct ext2fs_node *currnode = currroot;
2169 struct ext2fs_node *oldnode = currroot;
2171 strncpy(fpath, currpath, strlen(currpath) + 1);
2173 /* Remove all leading slashes. */
2174 while (*name == '/')
2178 *currfound = currnode;
2185 /* Extract the actual part from the pathname. */
2186 next = strchr(name, '/');
2188 /* Remove all leading slashes. */
2189 while (*next == '/')
2193 if (type != FILETYPE_DIRECTORY) {
2194 ext4fs_free_node(currnode, currroot);
2200 /* Iterate over the directory. */
2201 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2208 /* Read in the symlink and follow it. */
2209 if (type == FILETYPE_SYMLINK) {
2212 /* Test if the symlink does not loop. */
2213 if (++symlinknest == 8) {
2214 ext4fs_free_node(currnode, currroot);
2215 ext4fs_free_node(oldnode, currroot);
2219 symlink = ext4fs_read_symlink(currnode);
2220 ext4fs_free_node(currnode, currroot);
2223 ext4fs_free_node(oldnode, currroot);
2227 debug("Got symlink >%s<\n", symlink);
2229 if (symlink[0] == '/') {
2230 ext4fs_free_node(oldnode, currroot);
2231 oldnode = &ext4fs_root->diropen;
2234 /* Lookup the node the symlink points to. */
2235 status = ext4fs_find_file1(symlink, oldnode,
2241 ext4fs_free_node(oldnode, currroot);
2246 ext4fs_free_node(oldnode, currroot);
2248 /* Found the node! */
2249 if (!next || *next == '\0') {
2250 *currfound = currnode;
2259 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2260 struct ext2fs_node **foundnode, int expecttype)
2263 int foundtype = FILETYPE_DIRECTORY;
2269 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2273 /* Check if the node that was found was of the expected type. */
2274 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2276 else if ((expecttype == FILETYPE_DIRECTORY)
2277 && (foundtype != expecttype))
2283 int ext4fs_open(const char *filename, loff_t *len)
2285 struct ext2fs_node *fdiro = NULL;
2288 if (ext4fs_root == NULL)
2292 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2297 if (!fdiro->inode_read) {
2298 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2303 *len = le32_to_cpu(fdiro->inode.size);
2304 ext4fs_file = fdiro;
2308 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2313 int ext4fs_mount(unsigned part_length)
2315 struct ext2_data *data;
2317 struct ext_filesystem *fs = get_fs();
2318 data = zalloc(SUPERBLOCK_SIZE);
2322 /* Read the superblock. */
2323 status = ext4_read_superblock((char *)&data->sblock);
2328 /* Make sure this is an ext2 filesystem. */
2329 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2333 if (le32_to_cpu(data->sblock.revision_level) == 0) {
2336 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2337 __le32_to_cpu(data->sblock.feature_compatibility),
2338 __le32_to_cpu(data->sblock.feature_incompat),
2339 __le32_to_cpu(data->sblock.feature_ro_compat));
2341 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2342 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2343 EXT4_FEATURE_INCOMPAT_64BIT ?
2344 le16_to_cpu(data->sblock.descriptor_size) : 32;
2347 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2348 le32_to_cpu(data->sblock.revision_level),
2349 fs->inodesz, fs->gdsize);
2351 data->diropen.data = data;
2352 data->diropen.ino = 2;
2353 data->diropen.inode_read = 1;
2354 data->inode = &data->diropen.inode;
2356 status = ext4fs_read_inode(data, 2, data->inode);
2364 printf("Failed to mount ext2 filesystem...\n");