2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
7 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
10 * esd gmbh <www.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>
28 #include <linux/stat.h>
29 #include <linux/time.h>
30 #include <asm/byteorder.h>
31 #include "ext4_common.h"
33 struct ext2_data *ext4fs_root;
34 struct ext2fs_node *ext4fs_file;
35 uint32_t *ext4fs_indir1_block;
36 int ext4fs_indir1_size;
37 int ext4fs_indir1_blkno = -1;
38 uint32_t *ext4fs_indir2_block;
39 int ext4fs_indir2_size;
40 int ext4fs_indir2_blkno = -1;
42 uint32_t *ext4fs_indir3_block;
43 int ext4fs_indir3_size;
44 int ext4fs_indir3_blkno = -1;
45 struct ext2_inode *g_parent_inode;
46 static int symlinknest;
48 #if defined(CONFIG_EXT4_WRITE)
49 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
51 uint32_t res = size / n;
58 void put_ext4(uint64_t off, void *buf, uint32_t size)
62 unsigned char *temp_ptr = NULL;
63 struct ext_filesystem *fs = get_fs();
64 int log2blksz = fs->dev_desc->log2blksz;
65 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
67 startblock = off >> log2blksz;
68 startblock += part_offset;
69 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
71 if (fs->dev_desc == NULL)
74 if ((startblock + (size >> log2blksz)) >
75 (part_offset + fs->total_sect)) {
76 printf("part_offset is " LBAFU "\n", part_offset);
77 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
78 printf("error: overflow occurs\n");
83 if (fs->dev_desc->block_read) {
84 fs->dev_desc->block_read(fs->dev_desc->dev,
85 startblock, 1, sec_buf);
87 memcpy((temp_ptr + remainder),
88 (unsigned char *)buf, size);
89 fs->dev_desc->block_write(fs->dev_desc->dev,
90 startblock, 1, sec_buf);
93 if (size >> log2blksz != 0) {
94 fs->dev_desc->block_write(fs->dev_desc->dev,
97 (unsigned long *)buf);
99 fs->dev_desc->block_read(fs->dev_desc->dev,
100 startblock, 1, sec_buf);
102 memcpy(temp_ptr, buf, size);
103 fs->dev_desc->block_write(fs->dev_desc->dev,
105 (unsigned long *)sec_buf);
110 static int _get_new_inode_no(unsigned char *buffer)
112 struct ext_filesystem *fs = get_fs();
118 /* get the blocksize of the filesystem */
119 unsigned char *ptr = buffer;
120 while (*ptr == 255) {
123 if (count > ext4fs_root->sblock.inodes_per_group)
127 for (j = 0; j < fs->blksz; j++) {
132 status = input & operand;
147 static int _get_new_blk_no(unsigned char *buffer)
153 unsigned char *ptr = buffer;
154 struct ext_filesystem *fs = get_fs();
156 if (fs->blksz != 1024)
161 while (*ptr == 255) {
164 if (count == (fs->blksz * 8))
168 for (j = 0; j < fs->blksz; j++) {
173 status = input & operand;
188 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
190 int i, remainder, status;
191 unsigned char *ptr = buffer;
192 unsigned char operand;
194 remainder = blockno % 8;
195 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
197 i = i - (index * blocksize);
198 if (blocksize != 1024) {
200 operand = 1 << remainder;
201 status = *ptr & operand;
205 *ptr = *ptr | operand;
208 if (remainder == 0) {
213 operand = (1 << (remainder - 1));
215 status = *ptr & operand;
219 *ptr = *ptr | operand;
224 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
226 int i, remainder, status;
227 unsigned char *ptr = buffer;
228 unsigned char operand;
230 remainder = blockno % 8;
231 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
233 i = i - (index * blocksize);
234 if (blocksize != 1024) {
236 operand = (1 << remainder);
237 status = *ptr & operand;
239 *ptr = *ptr & ~(operand);
241 if (remainder == 0) {
246 operand = (1 << (remainder - 1));
248 status = *ptr & operand;
250 *ptr = *ptr & ~(operand);
254 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
256 int i, remainder, status;
257 unsigned char *ptr = buffer;
258 unsigned char operand;
260 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
262 remainder = inode_no % 8;
263 if (remainder == 0) {
268 operand = (1 << (remainder - 1));
270 status = *ptr & operand;
274 *ptr = *ptr | operand;
279 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
281 int i, remainder, status;
282 unsigned char *ptr = buffer;
283 unsigned char operand;
285 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
287 remainder = inode_no % 8;
288 if (remainder == 0) {
293 operand = (1 << (remainder - 1));
295 status = *ptr & operand;
297 *ptr = *ptr & ~(operand);
300 int ext4fs_checksum_update(unsigned int i)
302 struct ext2_block_group *desc;
303 struct ext_filesystem *fs = get_fs();
306 desc = (struct ext2_block_group *)&fs->bgd[i];
307 if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
308 int offset = offsetof(struct ext2_block_group, bg_checksum);
310 crc = ext2fs_crc16(~0, fs->sb->unique_id,
311 sizeof(fs->sb->unique_id));
312 crc = ext2fs_crc16(crc, &i, sizeof(i));
313 crc = ext2fs_crc16(crc, desc, offset);
314 offset += sizeof(desc->bg_checksum); /* skip checksum */
315 assert(offset == sizeof(*desc));
321 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
324 int sizeof_void_space;
325 int new_entry_byte_reqd;
326 short padding_factor = 0;
328 if (dir->namelen % 4 != 0)
329 padding_factor = 4 - (dir->namelen % 4);
331 dentry_length = sizeof(struct ext2_dirent) +
332 dir->namelen + padding_factor;
333 sizeof_void_space = dir->direntlen - dentry_length;
334 if (sizeof_void_space == 0)
338 if (strlen(filename) % 4 != 0)
339 padding_factor = 4 - (strlen(filename) % 4);
341 new_entry_byte_reqd = strlen(filename) +
342 sizeof(struct ext2_dirent) + padding_factor;
343 if (sizeof_void_space >= new_entry_byte_reqd) {
344 dir->direntlen = dentry_length;
345 return sizeof_void_space;
351 void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
353 unsigned int *zero_buffer = NULL;
354 char *root_first_block_buffer = NULL;
357 long int first_block_no_of_root = 0;
358 long int previous_blknr = -1;
360 short int padding_factor = 0;
361 unsigned int new_entry_byte_reqd;
362 unsigned int last_entry_dirlen;
363 int sizeof_void_space = 0;
367 struct ext_filesystem *fs = get_fs();
368 /* directory entry */
369 struct ext2_dirent *dir;
370 char *temp_dir = NULL;
372 zero_buffer = zalloc(fs->blksz);
374 printf("No Memory\n");
377 root_first_block_buffer = zalloc(fs->blksz);
378 if (!root_first_block_buffer) {
380 printf("No Memory\n");
385 /* read the block no allocated to a file */
386 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
388 root_blknr = read_allocated_block(g_parent_inode,
390 if (root_blknr == 0) {
391 first_block_no_of_root = previous_blknr;
394 previous_blknr = root_blknr;
397 status = ext4fs_devread((lbaint_t)first_block_no_of_root
399 0, fs->blksz, root_first_block_buffer);
403 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
405 dir = (struct ext2_dirent *)root_first_block_buffer;
407 while (dir->direntlen > 0) {
409 * blocksize-totalbytes because last directory length
410 * i.e. dir->direntlen is free availble space in the
411 * block that means it is a last entry of directory
415 /* traversing the each directory entry */
416 if (fs->blksz - totalbytes == dir->direntlen) {
417 if (strlen(filename) % 4 != 0)
418 padding_factor = 4 - (strlen(filename) % 4);
420 new_entry_byte_reqd = strlen(filename) +
421 sizeof(struct ext2_dirent) + padding_factor;
424 * update last directory entry length to its
425 * length because we are creating new directory
428 if (dir->namelen % 4 != 0)
429 padding_factor = 4 - (dir->namelen % 4);
431 last_entry_dirlen = dir->namelen +
432 sizeof(struct ext2_dirent) + padding_factor;
433 if ((fs->blksz - totalbytes - last_entry_dirlen) <
434 new_entry_byte_reqd) {
435 printf("1st Block Full:Allocate new block\n");
437 if (direct_blk_idx == INDIRECT_BLOCKS - 1) {
438 printf("Directory exceeds limit\n");
441 g_parent_inode->b.blocks.dir_blocks
442 [direct_blk_idx] = ext4fs_get_new_blk_no();
443 if (g_parent_inode->b.blocks.dir_blocks
444 [direct_blk_idx] == -1) {
445 printf("no block left to assign\n");
449 ((uint64_t)g_parent_inode->b.
450 blocks.dir_blocks[direct_blk_idx] *
451 (uint64_t)fs->blksz)), zero_buffer, fs->blksz);
452 g_parent_inode->size =
453 g_parent_inode->size + fs->blksz;
454 g_parent_inode->blockcnt =
455 g_parent_inode->blockcnt + fs->sect_perblk;
456 if (ext4fs_put_metadata
457 (root_first_block_buffer,
458 first_block_no_of_root))
462 dir->direntlen = last_entry_dirlen;
466 templength = dir->direntlen;
467 totalbytes = totalbytes + templength;
468 sizeof_void_space = check_void_in_dentry(dir, filename);
469 if (sizeof_void_space)
472 dir = (struct ext2_dirent *)((char *)dir + templength);
475 /* make a pointer ready for creating next directory entry */
476 templength = dir->direntlen;
477 totalbytes = totalbytes + templength;
478 dir = (struct ext2_dirent *)((char *)dir + templength);
480 /* get the next available inode number */
481 inodeno = ext4fs_get_new_inode_no();
483 printf("no inode left to assign\n");
486 dir->inode = inodeno;
487 if (sizeof_void_space)
488 dir->direntlen = sizeof_void_space;
490 dir->direntlen = fs->blksz - totalbytes;
492 dir->namelen = strlen(filename);
493 dir->filetype = FILETYPE_REG; /* regular file */
494 temp_dir = (char *)dir;
495 temp_dir = temp_dir + sizeof(struct ext2_dirent);
496 memcpy(temp_dir, filename, strlen(filename));
500 /* update or write the 1st block of root inode */
501 if (ext4fs_put_metadata(root_first_block_buffer,
502 first_block_no_of_root))
507 free(root_first_block_buffer);
510 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
520 unsigned char *block_buffer = NULL;
521 struct ext2_dirent *dir = NULL;
522 struct ext2_dirent *previous_dir = NULL;
523 struct ext_filesystem *fs = get_fs();
525 /* read the block no allocated to a file */
526 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
528 blknr = read_allocated_block(parent_inode, direct_blk_idx);
532 /* read the blocks of parenet inode */
533 block_buffer = zalloc(fs->blksz);
537 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
538 0, fs->blksz, (char *)block_buffer);
542 dir = (struct ext2_dirent *)block_buffer;
545 while (dir->direntlen >= 0) {
547 * blocksize-totalbytes because last directory
548 * length i.e.,*dir->direntlen is free availble
549 * space in the block that means
550 * it is a last entry of directory entry
552 if (strlen(dirname) == dir->namelen) {
553 if (strncmp(dirname, ptr +
554 sizeof(struct ext2_dirent),
555 dir->namelen) == 0) {
556 previous_dir->direntlen +=
558 inodeno = dir->inode;
565 if (fs->blksz - totalbytes == dir->direntlen)
568 /* traversing the each directory entry */
569 templength = dir->direntlen;
570 totalbytes = totalbytes + templength;
572 dir = (struct ext2_dirent *)((char *)dir + templength);
592 static int find_dir_depth(char *dirname)
594 char *token = strtok(dirname, "/");
596 while (token != NULL) {
597 token = strtok(NULL, "/");
600 return count + 1 + 1;
602 * for example for string /home/temp
603 * depth=home(1)+temp(1)+1 extra for NULL;
608 static int parse_path(char **arr, char *dirname)
610 char *token = strtok(dirname, "/");
614 arr[i] = zalloc(strlen("/") + 1);
620 /* add each path entry after root */
621 while (token != NULL) {
622 arr[i] = zalloc(strlen(token) + 1);
625 memcpy(arr[i++], token, strlen(token));
626 token = strtok(NULL, "/");
633 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
635 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
642 * Function: ext4fs_get_parent_inode_num
643 * Return Value: inode Number of the parent directory of file/Directory to be
645 * dirname : Input parmater, input path name of the file/directory to be created
646 * dname : Output parameter, to be filled with the name of the directory
647 * extracted from dirname
649 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
653 int matched_inode_no;
654 int result_inode_no = -1;
656 char *depth_dirname = NULL;
657 char *parse_dirname = NULL;
658 struct ext2_inode *parent_inode = NULL;
659 struct ext2_inode *first_inode = NULL;
660 struct ext2_inode temp_inode;
662 if (*dirname != '/') {
663 printf("Please supply Absolute path\n");
667 /* TODO: input validation make equivalent to linux */
668 depth_dirname = zalloc(strlen(dirname) + 1);
672 memcpy(depth_dirname, dirname, strlen(dirname));
673 depth = find_dir_depth(depth_dirname);
674 parse_dirname = zalloc(strlen(dirname) + 1);
677 memcpy(parse_dirname, dirname, strlen(dirname));
679 /* allocate memory for each directory level */
680 ptr = zalloc((depth) * sizeof(char *));
683 if (parse_path(ptr, parse_dirname))
685 parent_inode = zalloc(sizeof(struct ext2_inode));
688 first_inode = zalloc(sizeof(struct ext2_inode));
691 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
692 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
694 result_inode_no = EXT2_ROOT_INO;
695 for (i = 1; i < depth; i++) {
696 matched_inode_no = search_dir(parent_inode, ptr[i]);
697 if (matched_inode_no == -1) {
698 if (ptr[i + 1] == NULL && i == 1) {
699 result_inode_no = EXT2_ROOT_INO;
702 if (ptr[i + 1] == NULL)
704 printf("Invalid path\n");
705 result_inode_no = -1;
709 if (ptr[i + 1] != NULL) {
710 memset(parent_inode, '\0',
711 sizeof(struct ext2_inode));
712 if (ext4fs_iget(matched_inode_no,
714 result_inode_no = -1;
717 result_inode_no = matched_inode_no;
726 matched_inode_no = search_dir(first_inode, ptr[i]);
728 matched_inode_no = search_dir(parent_inode, ptr[i]);
730 if (matched_inode_no != -1) {
731 ext4fs_iget(matched_inode_no, &temp_inode);
732 if (temp_inode.mode & S_IFDIR) {
733 printf("It is a Directory\n");
734 result_inode_no = -1;
739 if (strlen(ptr[i]) > 256) {
740 result_inode_no = -1;
743 memcpy(dname, ptr[i], strlen(ptr[i]));
752 return result_inode_no;
755 static int check_filename(char *filename, unsigned int blknr)
757 unsigned int first_block_no_of_root;
762 char *root_first_block_buffer = NULL;
763 char *root_first_block_addr = NULL;
764 struct ext2_dirent *dir = NULL;
765 struct ext2_dirent *previous_dir = NULL;
767 struct ext_filesystem *fs = get_fs();
769 /* get the first block of root */
770 first_block_no_of_root = blknr;
771 root_first_block_buffer = zalloc(fs->blksz);
772 if (!root_first_block_buffer)
774 root_first_block_addr = root_first_block_buffer;
775 status = ext4fs_devread((lbaint_t)first_block_no_of_root *
777 fs->blksz, root_first_block_buffer);
781 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
783 dir = (struct ext2_dirent *)root_first_block_buffer;
786 while (dir->direntlen >= 0) {
788 * blocksize-totalbytes because last
789 * directory length i.e., *dir->direntlen
790 * is free availble space in the block that
791 * means it is a last entry of directory entry
793 if (strlen(filename) == dir->namelen) {
794 if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
795 dir->namelen) == 0) {
796 printf("file found deleting\n");
797 previous_dir->direntlen += dir->direntlen;
798 inodeno = dir->inode;
805 if (fs->blksz - totalbytes == dir->direntlen)
808 /* traversing the each directory entry */
809 templength = dir->direntlen;
810 totalbytes = totalbytes + templength;
812 dir = (struct ext2_dirent *)((char *)dir + templength);
818 if (ext4fs_put_metadata(root_first_block_addr,
819 first_block_no_of_root))
824 free(root_first_block_buffer);
829 int ext4fs_filename_check(char *filename)
831 short direct_blk_idx = 0;
835 /* read the block no allocated to a file */
836 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
838 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
841 inodeno = check_filename(filename, blknr);
849 long int ext4fs_get_new_blk_no(void)
855 static int prev_bg_bitmap_index = -1;
856 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
857 struct ext_filesystem *fs = get_fs();
858 char *journal_buffer = zalloc(fs->blksz);
859 char *zero_buffer = zalloc(fs->blksz);
860 if (!journal_buffer || !zero_buffer)
862 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
864 if (fs->first_pass_bbmap == 0) {
865 for (i = 0; i < fs->no_blkgrp; i++) {
866 if (bgd[i].free_blocks) {
867 if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
868 put_ext4(((uint64_t) ((uint64_t)bgd[i].block_id *
869 (uint64_t)fs->blksz)),
870 zero_buffer, fs->blksz);
873 bg_flags & ~EXT4_BG_BLOCK_UNINIT;
874 memcpy(fs->blk_bmaps[i], zero_buffer,
878 _get_new_blk_no(fs->blk_bmaps[i]);
879 if (fs->curr_blkno == -1)
880 /* if block bitmap is completely fill */
882 fs->curr_blkno = fs->curr_blkno +
884 fs->first_pass_bbmap++;
885 bgd[i].free_blocks--;
886 fs->sb->free_blocks--;
887 status = ext4fs_devread((lbaint_t)
894 if (ext4fs_log_journal(journal_buffer,
899 debug("no space left on block group %d\n", i);
907 /* get the blockbitmap index respective to blockno */
908 bg_idx = fs->curr_blkno / blk_per_grp;
909 if (fs->blksz == 1024) {
910 remainder = fs->curr_blkno % blk_per_grp;
916 * To skip completely filled block group bitmaps
917 * Optimize the block allocation
919 if (bg_idx >= fs->no_blkgrp)
922 if (bgd[bg_idx].free_blocks == 0) {
923 debug("block group %u is full. Skipping\n", bg_idx);
924 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
929 if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) {
930 memset(zero_buffer, '\0', fs->blksz);
931 put_ext4(((uint64_t) ((uint64_t)bgd[bg_idx].block_id *
932 (uint64_t)fs->blksz)), zero_buffer, fs->blksz);
933 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
934 bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags &
935 ~EXT4_BG_BLOCK_UNINIT;
938 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
940 debug("going for restart for the block no %ld %u\n",
941 fs->curr_blkno, bg_idx);
946 if (prev_bg_bitmap_index != bg_idx) {
947 memset(journal_buffer, '\0', fs->blksz);
948 status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id
950 0, fs->blksz, journal_buffer);
953 if (ext4fs_log_journal(journal_buffer,
954 bgd[bg_idx].block_id))
957 prev_bg_bitmap_index = bg_idx;
959 bgd[bg_idx].free_blocks--;
960 fs->sb->free_blocks--;
964 free(journal_buffer);
967 return fs->curr_blkno;
969 free(journal_buffer);
975 int ext4fs_get_new_inode_no(void)
979 unsigned int ibmap_idx;
980 static int prev_inode_bitmap_index = -1;
981 unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group;
982 struct ext_filesystem *fs = get_fs();
983 char *journal_buffer = zalloc(fs->blksz);
984 char *zero_buffer = zalloc(fs->blksz);
985 if (!journal_buffer || !zero_buffer)
987 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
989 if (fs->first_pass_ibmap == 0) {
990 for (i = 0; i < fs->no_blkgrp; i++) {
991 if (bgd[i].free_inodes) {
992 if (bgd[i].bg_itable_unused !=
994 bgd[i].bg_itable_unused =
996 if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) {
998 ((uint64_t)bgd[i].inode_id *
999 (uint64_t)fs->blksz)),
1000 zero_buffer, fs->blksz);
1001 bgd[i].bg_flags = bgd[i].bg_flags &
1002 ~EXT4_BG_INODE_UNINIT;
1003 memcpy(fs->inode_bmaps[i],
1004 zero_buffer, fs->blksz);
1007 _get_new_inode_no(fs->inode_bmaps[i]);
1008 if (fs->curr_inode_no == -1)
1009 /* if block bitmap is completely fill */
1011 fs->curr_inode_no = fs->curr_inode_no +
1012 (i * inodes_per_grp);
1013 fs->first_pass_ibmap++;
1014 bgd[i].free_inodes--;
1015 bgd[i].bg_itable_unused--;
1016 fs->sb->free_inodes--;
1017 status = ext4fs_devread((lbaint_t)
1024 if (ext4fs_log_journal(journal_buffer,
1029 debug("no inode left on block group %d\n", i);
1034 fs->curr_inode_no++;
1035 /* get the blockbitmap index respective to blockno */
1036 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1037 if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) {
1038 memset(zero_buffer, '\0', fs->blksz);
1039 put_ext4(((uint64_t) ((uint64_t)bgd[ibmap_idx].inode_id *
1040 (uint64_t)fs->blksz)), zero_buffer,
1042 bgd[ibmap_idx].bg_flags =
1043 bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT;
1044 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1048 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1049 fs->inode_bmaps[ibmap_idx],
1051 debug("going for restart for the block no %d %u\n",
1052 fs->curr_inode_no, ibmap_idx);
1056 /* journal backup */
1057 if (prev_inode_bitmap_index != ibmap_idx) {
1058 memset(journal_buffer, '\0', fs->blksz);
1059 status = ext4fs_devread((lbaint_t)
1060 bgd[ibmap_idx].inode_id
1062 0, fs->blksz, journal_buffer);
1065 if (ext4fs_log_journal(journal_buffer,
1066 bgd[ibmap_idx].inode_id))
1068 prev_inode_bitmap_index = ibmap_idx;
1070 if (bgd[ibmap_idx].bg_itable_unused !=
1071 bgd[ibmap_idx].free_inodes)
1072 bgd[ibmap_idx].bg_itable_unused =
1073 bgd[ibmap_idx].free_inodes;
1074 bgd[ibmap_idx].free_inodes--;
1075 bgd[ibmap_idx].bg_itable_unused--;
1076 fs->sb->free_inodes--;
1081 free(journal_buffer);
1084 return fs->curr_inode_no;
1086 free(journal_buffer);
1094 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1095 unsigned int *total_remaining_blocks,
1096 unsigned int *no_blks_reqd)
1100 long int actual_block_no;
1101 long int si_blockno;
1102 /* si :single indirect */
1103 unsigned int *si_buffer = NULL;
1104 unsigned int *si_start_addr = NULL;
1105 struct ext_filesystem *fs = get_fs();
1107 if (*total_remaining_blocks != 0) {
1108 si_buffer = zalloc(fs->blksz);
1110 printf("No Memory\n");
1113 si_start_addr = si_buffer;
1114 si_blockno = ext4fs_get_new_blk_no();
1115 if (si_blockno == -1) {
1116 printf("no block left to assign\n");
1120 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1122 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1123 0, fs->blksz, (char *)si_buffer);
1124 memset(si_buffer, '\0', fs->blksz);
1128 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1129 actual_block_no = ext4fs_get_new_blk_no();
1130 if (actual_block_no == -1) {
1131 printf("no block left to assign\n");
1134 *si_buffer = actual_block_no;
1135 debug("SIAB %u: %u\n", *si_buffer,
1136 *total_remaining_blocks);
1139 (*total_remaining_blocks)--;
1140 if (*total_remaining_blocks == 0)
1144 /* write the block to disk */
1145 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1146 si_start_addr, fs->blksz);
1147 file_inode->b.blocks.indir_block = si_blockno;
1150 free(si_start_addr);
1153 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1154 unsigned int *total_remaining_blocks,
1155 unsigned int *no_blks_reqd)
1160 long int actual_block_no;
1161 /* di:double indirect */
1162 long int di_blockno_parent;
1163 long int di_blockno_child;
1164 unsigned int *di_parent_buffer = NULL;
1165 unsigned int *di_child_buff = NULL;
1166 unsigned int *di_block_start_addr = NULL;
1167 unsigned int *di_child_buff_start = NULL;
1168 struct ext_filesystem *fs = get_fs();
1170 if (*total_remaining_blocks != 0) {
1171 /* double indirect parent block connecting to inode */
1172 di_blockno_parent = ext4fs_get_new_blk_no();
1173 if (di_blockno_parent == -1) {
1174 printf("no block left to assign\n");
1177 di_parent_buffer = zalloc(fs->blksz);
1178 if (!di_parent_buffer)
1181 di_block_start_addr = di_parent_buffer;
1183 debug("DIPB %ld: %u\n", di_blockno_parent,
1184 *total_remaining_blocks);
1186 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1188 fs->blksz, (char *)di_parent_buffer);
1191 printf("%s: Device read error!\n", __func__);
1194 memset(di_parent_buffer, '\0', fs->blksz);
1197 * start:for each double indirect parent
1198 * block create one more block
1200 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1201 di_blockno_child = ext4fs_get_new_blk_no();
1202 if (di_blockno_child == -1) {
1203 printf("no block left to assign\n");
1206 di_child_buff = zalloc(fs->blksz);
1210 di_child_buff_start = di_child_buff;
1211 *di_parent_buffer = di_blockno_child;
1214 debug("DICB %ld: %u\n", di_blockno_child,
1215 *total_remaining_blocks);
1217 status = ext4fs_devread((lbaint_t)di_blockno_child *
1220 (char *)di_child_buff);
1223 printf("%s: Device read error!\n", __func__);
1226 memset(di_child_buff, '\0', fs->blksz);
1227 /* filling of actual datablocks for each child */
1228 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1229 actual_block_no = ext4fs_get_new_blk_no();
1230 if (actual_block_no == -1) {
1231 printf("no block left to assign\n");
1234 *di_child_buff = actual_block_no;
1235 debug("DIAB %ld: %u\n", actual_block_no,
1236 *total_remaining_blocks);
1239 (*total_remaining_blocks)--;
1240 if (*total_remaining_blocks == 0)
1243 /* write the block table */
1244 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1245 di_child_buff_start, fs->blksz);
1246 free(di_child_buff_start);
1247 di_child_buff_start = NULL;
1249 if (*total_remaining_blocks == 0)
1252 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1253 di_block_start_addr, fs->blksz);
1254 file_inode->b.blocks.double_indir_block = di_blockno_parent;
1257 free(di_block_start_addr);
1260 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1261 unsigned int *total_remaining_blocks,
1262 unsigned int *no_blks_reqd)
1267 long int actual_block_no;
1268 /* ti: Triple Indirect */
1269 long int ti_gp_blockno;
1270 long int ti_parent_blockno;
1271 long int ti_child_blockno;
1272 unsigned int *ti_gp_buff = NULL;
1273 unsigned int *ti_parent_buff = NULL;
1274 unsigned int *ti_child_buff = NULL;
1275 unsigned int *ti_gp_buff_start_addr = NULL;
1276 unsigned int *ti_pbuff_start_addr = NULL;
1277 unsigned int *ti_cbuff_start_addr = NULL;
1278 struct ext_filesystem *fs = get_fs();
1279 if (*total_remaining_blocks != 0) {
1280 /* triple indirect grand parent block connecting to inode */
1281 ti_gp_blockno = ext4fs_get_new_blk_no();
1282 if (ti_gp_blockno == -1) {
1283 printf("no block left to assign\n");
1286 ti_gp_buff = zalloc(fs->blksz);
1290 ti_gp_buff_start_addr = ti_gp_buff;
1292 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1293 *total_remaining_blocks);
1295 /* for each 4 byte grand parent entry create one more block */
1296 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1297 ti_parent_blockno = ext4fs_get_new_blk_no();
1298 if (ti_parent_blockno == -1) {
1299 printf("no block left to assign\n");
1302 ti_parent_buff = zalloc(fs->blksz);
1303 if (!ti_parent_buff)
1306 ti_pbuff_start_addr = ti_parent_buff;
1307 *ti_gp_buff = ti_parent_blockno;
1310 debug("TIPB %ld: %u\n", ti_parent_blockno,
1311 *total_remaining_blocks);
1313 /* for each 4 byte entry parent create one more block */
1314 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1315 ti_child_blockno = ext4fs_get_new_blk_no();
1316 if (ti_child_blockno == -1) {
1317 printf("no block left assign\n");
1320 ti_child_buff = zalloc(fs->blksz);
1324 ti_cbuff_start_addr = ti_child_buff;
1325 *ti_parent_buff = ti_child_blockno;
1328 debug("TICB %ld: %u\n", ti_parent_blockno,
1329 *total_remaining_blocks);
1331 /* fill actual datablocks for each child */
1332 for (k = 0; k < (fs->blksz / sizeof(int));
1335 ext4fs_get_new_blk_no();
1336 if (actual_block_no == -1) {
1337 printf("no block left\n");
1340 *ti_child_buff = actual_block_no;
1341 debug("TIAB %ld: %u\n", actual_block_no,
1342 *total_remaining_blocks);
1345 (*total_remaining_blocks)--;
1346 if (*total_remaining_blocks == 0)
1349 /* write the child block */
1350 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1351 (uint64_t)fs->blksz)),
1352 ti_cbuff_start_addr, fs->blksz);
1353 free(ti_cbuff_start_addr);
1355 if (*total_remaining_blocks == 0)
1358 /* write the parent block */
1359 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1360 ti_pbuff_start_addr, fs->blksz);
1361 free(ti_pbuff_start_addr);
1363 if (*total_remaining_blocks == 0)
1366 /* write the grand parent block */
1367 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1368 ti_gp_buff_start_addr, fs->blksz);
1369 file_inode->b.blocks.triple_indir_block = ti_gp_blockno;
1372 free(ti_gp_buff_start_addr);
1375 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1376 unsigned int total_remaining_blocks,
1377 unsigned int *total_no_of_block)
1380 long int direct_blockno;
1381 unsigned int no_blks_reqd = 0;
1383 /* allocation of direct blocks */
1384 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1385 direct_blockno = ext4fs_get_new_blk_no();
1386 if (direct_blockno == -1) {
1387 printf("no block left to assign\n");
1390 file_inode->b.blocks.dir_blocks[i] = direct_blockno;
1391 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1393 total_remaining_blocks--;
1396 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1398 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1400 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1402 *total_no_of_block += no_blks_reqd;
1407 static struct ext4_extent_header *ext4fs_get_extent_block
1408 (struct ext2_data *data, char *buf,
1409 struct ext4_extent_header *ext_block,
1410 uint32_t fileblock, int log2_blksz)
1412 struct ext4_extent_idx *index;
1413 unsigned long long block;
1414 int blksz = EXT2_BLOCK_SIZE(data);
1418 index = (struct ext4_extent_idx *)(ext_block + 1);
1420 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1423 if (ext_block->eh_depth == 0)
1428 if (i >= le16_to_cpu(ext_block->eh_entries))
1430 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1435 block = le16_to_cpu(index[i].ei_leaf_hi);
1436 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1438 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1440 ext_block = (struct ext4_extent_header *)buf;
1446 static int ext4fs_blockgroup
1447 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1450 unsigned int blkoff, desc_per_blk;
1451 int log2blksz = get_fs()->dev_desc->log2blksz;
1453 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1455 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
1456 group / desc_per_blk;
1457 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1459 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1460 group, blkno, blkoff);
1462 return ext4fs_devread((lbaint_t)blkno <<
1463 (LOG2_BLOCK_SIZE(data) - log2blksz),
1464 blkoff, sizeof(struct ext2_block_group),
1468 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1470 struct ext2_block_group blkgrp;
1471 struct ext2_sblock *sblock = &data->sblock;
1472 struct ext_filesystem *fs = get_fs();
1473 int log2blksz = get_fs()->dev_desc->log2blksz;
1474 int inodes_per_block, status;
1476 unsigned int blkoff;
1478 /* It is easier to calculate if the first inode is 0. */
1480 status = ext4fs_blockgroup(data, ino / __le32_to_cpu
1481 (sblock->inodes_per_group), &blkgrp);
1485 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1486 blkno = __le32_to_cpu(blkgrp.inode_table_id) +
1487 (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1488 blkoff = (ino % inodes_per_block) * fs->inodesz;
1489 /* Read the inode. */
1490 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1492 sizeof(struct ext2_inode), (char *)inode);
1499 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1506 long int perblock_parent;
1507 long int perblock_child;
1508 unsigned long long start;
1509 /* get the blocksize of the filesystem */
1510 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1511 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1512 - get_fs()->dev_desc->log2blksz;
1514 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1515 char *buf = zalloc(blksz);
1518 struct ext4_extent_header *ext_block;
1519 struct ext4_extent *extent;
1522 ext4fs_get_extent_block(ext4fs_root, buf,
1523 (struct ext4_extent_header *)
1524 inode->b.blocks.dir_blocks,
1525 fileblock, log2_blksz);
1527 printf("invalid extent block\n");
1532 extent = (struct ext4_extent *)(ext_block + 1);
1536 if (i >= le16_to_cpu(ext_block->eh_entries))
1538 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1540 fileblock -= le32_to_cpu(extent[i].ee_block);
1541 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1546 start = le16_to_cpu(extent[i].ee_start_hi);
1547 start = (start << 32) +
1548 le32_to_cpu(extent[i].ee_start_lo);
1550 return fileblock + start;
1553 printf("Extent Error\n");
1558 /* Direct blocks. */
1559 if (fileblock < INDIRECT_BLOCKS)
1560 blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1563 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1564 if (ext4fs_indir1_block == NULL) {
1565 ext4fs_indir1_block = zalloc(blksz);
1566 if (ext4fs_indir1_block == NULL) {
1567 printf("** SI ext2fs read block (indir 1)"
1568 "malloc failed. **\n");
1571 ext4fs_indir1_size = blksz;
1572 ext4fs_indir1_blkno = -1;
1574 if (blksz != ext4fs_indir1_size) {
1575 free(ext4fs_indir1_block);
1576 ext4fs_indir1_block = NULL;
1577 ext4fs_indir1_size = 0;
1578 ext4fs_indir1_blkno = -1;
1579 ext4fs_indir1_block = zalloc(blksz);
1580 if (ext4fs_indir1_block == NULL) {
1581 printf("** SI ext2fs read block (indir 1):"
1582 "malloc failed. **\n");
1585 ext4fs_indir1_size = blksz;
1587 if ((__le32_to_cpu(inode->b.blocks.indir_block) <<
1588 log2_blksz) != ext4fs_indir1_blkno) {
1590 ext4fs_devread((lbaint_t)__le32_to_cpu
1592 indir_block) << log2_blksz, 0,
1593 blksz, (char *)ext4fs_indir1_block);
1595 printf("** SI ext2fs read block (indir 1)"
1599 ext4fs_indir1_blkno =
1600 __le32_to_cpu(inode->b.blocks.
1601 indir_block) << log2_blksz;
1603 blknr = __le32_to_cpu(ext4fs_indir1_block
1604 [fileblock - INDIRECT_BLOCKS]);
1606 /* Double indirect. */
1607 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1608 (blksz / 4 + 1)))) {
1610 long int perblock = blksz / 4;
1611 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1613 if (ext4fs_indir1_block == NULL) {
1614 ext4fs_indir1_block = zalloc(blksz);
1615 if (ext4fs_indir1_block == NULL) {
1616 printf("** DI ext2fs read block (indir 2 1)"
1617 "malloc failed. **\n");
1620 ext4fs_indir1_size = blksz;
1621 ext4fs_indir1_blkno = -1;
1623 if (blksz != ext4fs_indir1_size) {
1624 free(ext4fs_indir1_block);
1625 ext4fs_indir1_block = NULL;
1626 ext4fs_indir1_size = 0;
1627 ext4fs_indir1_blkno = -1;
1628 ext4fs_indir1_block = zalloc(blksz);
1629 if (ext4fs_indir1_block == NULL) {
1630 printf("** DI ext2fs read block (indir 2 1)"
1631 "malloc failed. **\n");
1634 ext4fs_indir1_size = blksz;
1636 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) <<
1637 log2_blksz) != ext4fs_indir1_blkno) {
1639 ext4fs_devread((lbaint_t)__le32_to_cpu
1641 double_indir_block) << log2_blksz,
1643 (char *)ext4fs_indir1_block);
1645 printf("** DI ext2fs read block (indir 2 1)"
1649 ext4fs_indir1_blkno =
1650 __le32_to_cpu(inode->b.blocks.double_indir_block) <<
1654 if (ext4fs_indir2_block == NULL) {
1655 ext4fs_indir2_block = zalloc(blksz);
1656 if (ext4fs_indir2_block == NULL) {
1657 printf("** DI ext2fs read block (indir 2 2)"
1658 "malloc failed. **\n");
1661 ext4fs_indir2_size = blksz;
1662 ext4fs_indir2_blkno = -1;
1664 if (blksz != ext4fs_indir2_size) {
1665 free(ext4fs_indir2_block);
1666 ext4fs_indir2_block = NULL;
1667 ext4fs_indir2_size = 0;
1668 ext4fs_indir2_blkno = -1;
1669 ext4fs_indir2_block = zalloc(blksz);
1670 if (ext4fs_indir2_block == NULL) {
1671 printf("** DI ext2fs read block (indir 2 2)"
1672 "malloc failed. **\n");
1675 ext4fs_indir2_size = blksz;
1677 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1678 log2_blksz) != ext4fs_indir2_blkno) {
1679 status = ext4fs_devread((lbaint_t)__le32_to_cpu
1680 (ext4fs_indir1_block
1682 perblock]) << log2_blksz, 0,
1684 (char *)ext4fs_indir2_block);
1686 printf("** DI ext2fs read block (indir 2 2)"
1690 ext4fs_indir2_blkno =
1691 __le32_to_cpu(ext4fs_indir1_block[rblock
1696 blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1698 /* Tripple indirect. */
1700 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1701 (blksz / 4 * blksz / 4));
1702 perblock_child = blksz / 4;
1703 perblock_parent = ((blksz / 4) * (blksz / 4));
1705 if (ext4fs_indir1_block == NULL) {
1706 ext4fs_indir1_block = zalloc(blksz);
1707 if (ext4fs_indir1_block == NULL) {
1708 printf("** TI ext2fs read block (indir 2 1)"
1709 "malloc failed. **\n");
1712 ext4fs_indir1_size = blksz;
1713 ext4fs_indir1_blkno = -1;
1715 if (blksz != ext4fs_indir1_size) {
1716 free(ext4fs_indir1_block);
1717 ext4fs_indir1_block = NULL;
1718 ext4fs_indir1_size = 0;
1719 ext4fs_indir1_blkno = -1;
1720 ext4fs_indir1_block = zalloc(blksz);
1721 if (ext4fs_indir1_block == NULL) {
1722 printf("** TI ext2fs read block (indir 2 1)"
1723 "malloc failed. **\n");
1726 ext4fs_indir1_size = blksz;
1728 if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1729 log2_blksz) != ext4fs_indir1_blkno) {
1730 status = ext4fs_devread
1732 __le32_to_cpu(inode->b.blocks.triple_indir_block)
1733 << log2_blksz, 0, blksz,
1734 (char *)ext4fs_indir1_block);
1736 printf("** TI ext2fs read block (indir 2 1)"
1740 ext4fs_indir1_blkno =
1741 __le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1745 if (ext4fs_indir2_block == NULL) {
1746 ext4fs_indir2_block = zalloc(blksz);
1747 if (ext4fs_indir2_block == NULL) {
1748 printf("** TI ext2fs read block (indir 2 2)"
1749 "malloc failed. **\n");
1752 ext4fs_indir2_size = blksz;
1753 ext4fs_indir2_blkno = -1;
1755 if (blksz != ext4fs_indir2_size) {
1756 free(ext4fs_indir2_block);
1757 ext4fs_indir2_block = NULL;
1758 ext4fs_indir2_size = 0;
1759 ext4fs_indir2_blkno = -1;
1760 ext4fs_indir2_block = zalloc(blksz);
1761 if (ext4fs_indir2_block == NULL) {
1762 printf("** TI ext2fs read block (indir 2 2)"
1763 "malloc failed. **\n");
1766 ext4fs_indir2_size = blksz;
1768 if ((__le32_to_cpu(ext4fs_indir1_block[rblock /
1769 perblock_parent]) <<
1771 != ext4fs_indir2_blkno) {
1772 status = ext4fs_devread((lbaint_t)__le32_to_cpu
1773 (ext4fs_indir1_block
1775 perblock_parent]) <<
1776 log2_blksz, 0, blksz,
1777 (char *)ext4fs_indir2_block);
1779 printf("** TI ext2fs read block (indir 2 2)"
1783 ext4fs_indir2_blkno =
1784 __le32_to_cpu(ext4fs_indir1_block[rblock /
1789 if (ext4fs_indir3_block == NULL) {
1790 ext4fs_indir3_block = zalloc(blksz);
1791 if (ext4fs_indir3_block == NULL) {
1792 printf("** TI ext2fs read block (indir 2 2)"
1793 "malloc failed. **\n");
1796 ext4fs_indir3_size = blksz;
1797 ext4fs_indir3_blkno = -1;
1799 if (blksz != ext4fs_indir3_size) {
1800 free(ext4fs_indir3_block);
1801 ext4fs_indir3_block = NULL;
1802 ext4fs_indir3_size = 0;
1803 ext4fs_indir3_blkno = -1;
1804 ext4fs_indir3_block = zalloc(blksz);
1805 if (ext4fs_indir3_block == NULL) {
1806 printf("** TI ext2fs read block (indir 2 2)"
1807 "malloc failed. **\n");
1810 ext4fs_indir3_size = blksz;
1812 if ((__le32_to_cpu(ext4fs_indir2_block[rblock
1815 log2_blksz) != ext4fs_indir3_blkno) {
1817 ext4fs_devread((lbaint_t)__le32_to_cpu
1818 (ext4fs_indir2_block
1819 [(rblock / perblock_child)
1820 % (blksz / 4)]) << log2_blksz, 0,
1821 blksz, (char *)ext4fs_indir3_block);
1823 printf("** TI ext2fs read block (indir 2 2)"
1827 ext4fs_indir3_blkno =
1828 __le32_to_cpu(ext4fs_indir2_block[(rblock /
1835 blknr = __le32_to_cpu(ext4fs_indir3_block
1836 [rblock % perblock_child]);
1838 debug("read_allocated_block %ld\n", blknr);
1844 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1847 * This function assures that for a file with the same name but different size
1848 * the sequential store on the ext4 filesystem will be correct.
1850 * In this function the global data, responsible for internal representation
1851 * of the ext4 data are initialized to the reset state. Without this, during
1852 * replacement of the smaller file with the bigger truncation of new file was
1855 void ext4fs_reinit_global(void)
1857 if (ext4fs_indir1_block != NULL) {
1858 free(ext4fs_indir1_block);
1859 ext4fs_indir1_block = NULL;
1860 ext4fs_indir1_size = 0;
1861 ext4fs_indir1_blkno = -1;
1863 if (ext4fs_indir2_block != NULL) {
1864 free(ext4fs_indir2_block);
1865 ext4fs_indir2_block = NULL;
1866 ext4fs_indir2_size = 0;
1867 ext4fs_indir2_blkno = -1;
1869 if (ext4fs_indir3_block != NULL) {
1870 free(ext4fs_indir3_block);
1871 ext4fs_indir3_block = NULL;
1872 ext4fs_indir3_size = 0;
1873 ext4fs_indir3_blkno = -1;
1876 void ext4fs_close(void)
1878 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1879 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1882 if (ext4fs_root != NULL) {
1887 ext4fs_reinit_global();
1890 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1891 struct ext2fs_node **fnode, int *ftype)
1893 unsigned int fpos = 0;
1896 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1900 printf("Iterate dir %s\n", name);
1901 #endif /* of DEBUG */
1902 if (!diro->inode_read) {
1903 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1907 /* Search the file. */
1908 while (fpos < __le32_to_cpu(diro->inode.size)) {
1909 struct ext2_dirent dirent;
1911 status = ext4fs_read_file(diro, fpos,
1912 sizeof(struct ext2_dirent),
1913 (char *)&dirent, &actread);
1917 if (dirent.namelen != 0) {
1918 char filename[dirent.namelen + 1];
1919 struct ext2fs_node *fdiro;
1920 int type = FILETYPE_UNKNOWN;
1922 status = ext4fs_read_file(diro,
1924 sizeof(struct ext2_dirent),
1925 dirent.namelen, filename,
1930 fdiro = zalloc(sizeof(struct ext2fs_node));
1934 fdiro->data = diro->data;
1935 fdiro->ino = __le32_to_cpu(dirent.inode);
1937 filename[dirent.namelen] = '\0';
1939 if (dirent.filetype != FILETYPE_UNKNOWN) {
1940 fdiro->inode_read = 0;
1942 if (dirent.filetype == FILETYPE_DIRECTORY)
1943 type = FILETYPE_DIRECTORY;
1944 else if (dirent.filetype == FILETYPE_SYMLINK)
1945 type = FILETYPE_SYMLINK;
1946 else if (dirent.filetype == FILETYPE_REG)
1947 type = FILETYPE_REG;
1949 status = ext4fs_read_inode(diro->data,
1957 fdiro->inode_read = 1;
1959 if ((__le16_to_cpu(fdiro->inode.mode) &
1960 FILETYPE_INO_MASK) ==
1961 FILETYPE_INO_DIRECTORY) {
1962 type = FILETYPE_DIRECTORY;
1963 } else if ((__le16_to_cpu(fdiro->inode.mode)
1964 & FILETYPE_INO_MASK) ==
1965 FILETYPE_INO_SYMLINK) {
1966 type = FILETYPE_SYMLINK;
1967 } else if ((__le16_to_cpu(fdiro->inode.mode)
1968 & FILETYPE_INO_MASK) ==
1970 type = FILETYPE_REG;
1974 printf("iterate >%s<\n", filename);
1975 #endif /* of DEBUG */
1976 if ((name != NULL) && (fnode != NULL)
1977 && (ftype != NULL)) {
1978 if (strcmp(filename, name) == 0) {
1984 if (fdiro->inode_read == 0) {
1985 status = ext4fs_read_inode(diro->data,
1993 fdiro->inode_read = 1;
1996 case FILETYPE_DIRECTORY:
1999 case FILETYPE_SYMLINK:
2010 __le32_to_cpu(fdiro->inode.size),
2015 fpos += __le16_to_cpu(dirent.direntlen);
2020 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2023 struct ext2fs_node *diro = node;
2027 if (!diro->inode_read) {
2028 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2032 symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1);
2036 if (__le32_to_cpu(diro->inode.size) <= 60) {
2037 strncpy(symlink, diro->inode.b.symlink,
2038 __le32_to_cpu(diro->inode.size));
2040 status = ext4fs_read_file(diro, 0,
2041 __le32_to_cpu(diro->inode.size),
2048 symlink[__le32_to_cpu(diro->inode.size)] = '\0';
2052 static int ext4fs_find_file1(const char *currpath,
2053 struct ext2fs_node *currroot,
2054 struct ext2fs_node **currfound, int *foundtype)
2056 char fpath[strlen(currpath) + 1];
2060 int type = FILETYPE_DIRECTORY;
2061 struct ext2fs_node *currnode = currroot;
2062 struct ext2fs_node *oldnode = currroot;
2064 strncpy(fpath, currpath, strlen(currpath) + 1);
2066 /* Remove all leading slashes. */
2067 while (*name == '/')
2071 *currfound = currnode;
2078 /* Extract the actual part from the pathname. */
2079 next = strchr(name, '/');
2081 /* Remove all leading slashes. */
2082 while (*next == '/')
2086 if (type != FILETYPE_DIRECTORY) {
2087 ext4fs_free_node(currnode, currroot);
2093 /* Iterate over the directory. */
2094 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2101 /* Read in the symlink and follow it. */
2102 if (type == FILETYPE_SYMLINK) {
2105 /* Test if the symlink does not loop. */
2106 if (++symlinknest == 8) {
2107 ext4fs_free_node(currnode, currroot);
2108 ext4fs_free_node(oldnode, currroot);
2112 symlink = ext4fs_read_symlink(currnode);
2113 ext4fs_free_node(currnode, currroot);
2116 ext4fs_free_node(oldnode, currroot);
2120 debug("Got symlink >%s<\n", symlink);
2122 if (symlink[0] == '/') {
2123 ext4fs_free_node(oldnode, currroot);
2124 oldnode = &ext4fs_root->diropen;
2127 /* Lookup the node the symlink points to. */
2128 status = ext4fs_find_file1(symlink, oldnode,
2134 ext4fs_free_node(oldnode, currroot);
2139 ext4fs_free_node(oldnode, currroot);
2141 /* Found the node! */
2142 if (!next || *next == '\0') {
2143 *currfound = currnode;
2152 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2153 struct ext2fs_node **foundnode, int expecttype)
2156 int foundtype = FILETYPE_DIRECTORY;
2162 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2166 /* Check if the node that was found was of the expected type. */
2167 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2169 else if ((expecttype == FILETYPE_DIRECTORY)
2170 && (foundtype != expecttype))
2176 int ext4fs_open(const char *filename, loff_t *len)
2178 struct ext2fs_node *fdiro = NULL;
2181 if (ext4fs_root == NULL)
2185 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2190 if (!fdiro->inode_read) {
2191 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2196 *len = __le32_to_cpu(fdiro->inode.size);
2197 ext4fs_file = fdiro;
2201 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2206 int ext4fs_mount(unsigned part_length)
2208 struct ext2_data *data;
2210 struct ext_filesystem *fs = get_fs();
2211 data = zalloc(SUPERBLOCK_SIZE);
2215 /* Read the superblock. */
2216 status = ext4_read_superblock((char *)&data->sblock);
2221 /* Make sure this is an ext2 filesystem. */
2222 if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2225 if (__le32_to_cpu(data->sblock.revision_level == 0))
2228 fs->inodesz = __le16_to_cpu(data->sblock.inode_size);
2230 debug("EXT2 rev %d, inode_size %d\n",
2231 __le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2233 data->diropen.data = data;
2234 data->diropen.ino = 2;
2235 data->diropen.inode_read = 1;
2236 data->inode = &data->diropen.inode;
2238 status = ext4fs_read_inode(data, 2, data->inode);
2246 printf("Failed to mount ext2 filesystem...\n");