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>
27 #include <linux/stat.h>
28 #include <linux/time.h>
29 #include <linux/list.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 struct ext4_extent_node {
54 static LIST_HEAD(ext4_extent_lh);
56 #if defined(CONFIG_EXT4_WRITE)
57 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
59 uint32_t res = size / n;
66 void put_ext4(uint64_t off, void *buf, uint32_t size)
70 unsigned char *temp_ptr = NULL;
71 struct ext_filesystem *fs = get_fs();
72 int log2blksz = fs->dev_desc->log2blksz;
73 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
75 startblock = off >> log2blksz;
76 startblock += part_offset;
77 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
79 if (fs->dev_desc == NULL)
82 if ((startblock + (size >> log2blksz)) >
83 (part_offset + fs->total_sect)) {
84 printf("part_offset is " LBAFU "\n", part_offset);
85 printf("total_sector is %llu\n", fs->total_sect);
86 printf("error: overflow occurs\n");
91 if (fs->dev_desc->block_read) {
92 fs->dev_desc->block_read(fs->dev_desc->dev,
93 startblock, 1, sec_buf);
95 memcpy((temp_ptr + remainder),
96 (unsigned char *)buf, size);
97 fs->dev_desc->block_write(fs->dev_desc->dev,
98 startblock, 1, sec_buf);
101 if (size >> log2blksz != 0) {
102 fs->dev_desc->block_write(fs->dev_desc->dev,
105 (unsigned long *)buf);
107 fs->dev_desc->block_read(fs->dev_desc->dev,
108 startblock, 1, sec_buf);
110 memcpy(temp_ptr, buf, size);
111 fs->dev_desc->block_write(fs->dev_desc->dev,
113 (unsigned long *)sec_buf);
118 static int _get_new_inode_no(unsigned char *buffer)
120 struct ext_filesystem *fs = get_fs();
126 /* get the blocksize of the filesystem */
127 unsigned char *ptr = buffer;
128 while (*ptr == 255) {
131 if (count > ext4fs_root->sblock.inodes_per_group)
135 for (j = 0; j < fs->blksz; j++) {
140 status = input & operand;
155 static int _get_new_blk_no(unsigned char *buffer)
161 unsigned char *ptr = buffer;
162 struct ext_filesystem *fs = get_fs();
164 if (fs->blksz != 1024)
169 while (*ptr == 255) {
172 if (count == (fs->blksz * 8))
176 for (j = 0; j < fs->blksz; j++) {
181 status = input & operand;
196 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
198 int i, remainder, status;
199 unsigned char *ptr = buffer;
200 unsigned char operand;
202 remainder = blockno % 8;
203 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
205 i = i - (index * blocksize);
206 if (blocksize != 1024) {
208 operand = 1 << remainder;
209 status = *ptr & operand;
213 *ptr = *ptr | operand;
216 if (remainder == 0) {
221 operand = (1 << (remainder - 1));
223 status = *ptr & operand;
227 *ptr = *ptr | operand;
232 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
234 int i, remainder, status;
235 unsigned char *ptr = buffer;
236 unsigned char operand;
238 remainder = blockno % 8;
239 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
241 i = i - (index * blocksize);
242 if (blocksize != 1024) {
244 operand = (1 << remainder);
245 status = *ptr & operand;
247 *ptr = *ptr & ~(operand);
249 if (remainder == 0) {
254 operand = (1 << (remainder - 1));
256 status = *ptr & operand;
258 *ptr = *ptr & ~(operand);
262 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
264 int i, remainder, status;
265 unsigned char *ptr = buffer;
266 unsigned char operand;
268 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
270 remainder = inode_no % 8;
271 if (remainder == 0) {
276 operand = (1 << (remainder - 1));
278 status = *ptr & operand;
282 *ptr = *ptr | operand;
287 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
289 int i, remainder, status;
290 unsigned char *ptr = buffer;
291 unsigned char operand;
293 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
295 remainder = inode_no % 8;
296 if (remainder == 0) {
301 operand = (1 << (remainder - 1));
303 status = *ptr & operand;
305 *ptr = *ptr & ~(operand);
308 int ext4fs_checksum_update(unsigned int i)
310 struct ext2_block_group *desc;
311 struct ext_filesystem *fs = get_fs();
314 desc = (struct ext2_block_group *)&fs->bgd[i];
315 if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
316 int offset = offsetof(struct ext2_block_group, bg_checksum);
318 crc = ext2fs_crc16(~0, fs->sb->unique_id,
319 sizeof(fs->sb->unique_id));
320 crc = ext2fs_crc16(crc, &i, sizeof(i));
321 crc = ext2fs_crc16(crc, desc, offset);
322 offset += sizeof(desc->bg_checksum); /* skip checksum */
323 assert(offset == sizeof(*desc));
329 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
332 int sizeof_void_space;
333 int new_entry_byte_reqd;
334 short padding_factor = 0;
336 if (dir->namelen % 4 != 0)
337 padding_factor = 4 - (dir->namelen % 4);
339 dentry_length = sizeof(struct ext2_dirent) +
340 dir->namelen + padding_factor;
341 sizeof_void_space = dir->direntlen - dentry_length;
342 if (sizeof_void_space == 0)
346 if (strlen(filename) % 4 != 0)
347 padding_factor = 4 - (strlen(filename) % 4);
349 new_entry_byte_reqd = strlen(filename) +
350 sizeof(struct ext2_dirent) + padding_factor;
351 if (sizeof_void_space >= new_entry_byte_reqd) {
352 dir->direntlen = dentry_length;
353 return sizeof_void_space;
359 void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
361 unsigned int *zero_buffer = NULL;
362 char *root_first_block_buffer = NULL;
365 long int first_block_no_of_root = 0;
366 long int previous_blknr = -1;
368 short int padding_factor = 0;
369 unsigned int new_entry_byte_reqd;
370 unsigned int last_entry_dirlen;
371 int sizeof_void_space = 0;
375 struct ext_filesystem *fs = get_fs();
376 /* directory entry */
377 struct ext2_dirent *dir;
378 char *temp_dir = NULL;
380 zero_buffer = zalloc(fs->blksz);
382 printf("No Memory\n");
385 root_first_block_buffer = zalloc(fs->blksz);
386 if (!root_first_block_buffer) {
388 printf("No Memory\n");
393 /* read the block no allocated to a file */
394 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
396 root_blknr = read_allocated_block(g_parent_inode,
398 if (root_blknr == 0) {
399 first_block_no_of_root = previous_blknr;
402 previous_blknr = root_blknr;
405 status = ext4fs_devread((lbaint_t)first_block_no_of_root
407 0, fs->blksz, root_first_block_buffer);
411 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
413 dir = (struct ext2_dirent *)root_first_block_buffer;
415 while (dir->direntlen > 0) {
417 * blocksize-totalbytes because last directory length
418 * i.e. dir->direntlen is free availble space in the
419 * block that means it is a last entry of directory
423 /* traversing the each directory entry */
424 if (fs->blksz - totalbytes == dir->direntlen) {
425 if (strlen(filename) % 4 != 0)
426 padding_factor = 4 - (strlen(filename) % 4);
428 new_entry_byte_reqd = strlen(filename) +
429 sizeof(struct ext2_dirent) + padding_factor;
432 * update last directory entry length to its
433 * length because we are creating new directory
436 if (dir->namelen % 4 != 0)
437 padding_factor = 4 - (dir->namelen % 4);
439 last_entry_dirlen = dir->namelen +
440 sizeof(struct ext2_dirent) + padding_factor;
441 if ((fs->blksz - totalbytes - last_entry_dirlen) <
442 new_entry_byte_reqd) {
443 printf("1st Block Full:Allocate new block\n");
445 if (direct_blk_idx == INDIRECT_BLOCKS - 1) {
446 printf("Directory exceeds limit\n");
449 g_parent_inode->b.blocks.dir_blocks
450 [direct_blk_idx] = ext4fs_get_new_blk_no();
451 if (g_parent_inode->b.blocks.dir_blocks
452 [direct_blk_idx] == -1) {
453 printf("no block left to assign\n");
457 ((uint64_t)g_parent_inode->b.
458 blocks.dir_blocks[direct_blk_idx] *
459 (uint64_t)fs->blksz)), zero_buffer, fs->blksz);
460 g_parent_inode->size =
461 g_parent_inode->size + fs->blksz;
462 g_parent_inode->blockcnt =
463 g_parent_inode->blockcnt + fs->sect_perblk;
464 if (ext4fs_put_metadata
465 (root_first_block_buffer,
466 first_block_no_of_root))
470 dir->direntlen = last_entry_dirlen;
474 templength = dir->direntlen;
475 totalbytes = totalbytes + templength;
476 sizeof_void_space = check_void_in_dentry(dir, filename);
477 if (sizeof_void_space)
480 dir = (struct ext2_dirent *)((char *)dir + templength);
483 /* make a pointer ready for creating next directory entry */
484 templength = dir->direntlen;
485 totalbytes = totalbytes + templength;
486 dir = (struct ext2_dirent *)((char *)dir + templength);
488 /* get the next available inode number */
489 inodeno = ext4fs_get_new_inode_no();
491 printf("no inode left to assign\n");
494 dir->inode = inodeno;
495 if (sizeof_void_space)
496 dir->direntlen = sizeof_void_space;
498 dir->direntlen = fs->blksz - totalbytes;
500 dir->namelen = strlen(filename);
501 dir->filetype = FILETYPE_REG; /* regular file */
502 temp_dir = (char *)dir;
503 temp_dir = temp_dir + sizeof(struct ext2_dirent);
504 memcpy(temp_dir, filename, strlen(filename));
508 /* update or write the 1st block of root inode */
509 if (ext4fs_put_metadata(root_first_block_buffer,
510 first_block_no_of_root))
515 free(root_first_block_buffer);
518 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
528 unsigned char *block_buffer = NULL;
529 struct ext2_dirent *dir = NULL;
530 struct ext2_dirent *previous_dir = NULL;
531 struct ext_filesystem *fs = get_fs();
533 /* read the block no allocated to a file */
534 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
536 blknr = read_allocated_block(parent_inode, direct_blk_idx);
540 /* read the blocks of parenet inode */
541 block_buffer = zalloc(fs->blksz);
545 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
546 0, fs->blksz, (char *)block_buffer);
550 dir = (struct ext2_dirent *)block_buffer;
553 while (dir->direntlen >= 0) {
555 * blocksize-totalbytes because last directory
556 * length i.e.,*dir->direntlen is free availble
557 * space in the block that means
558 * it is a last entry of directory entry
560 if (strlen(dirname) == dir->namelen) {
561 if (strncmp(dirname, ptr +
562 sizeof(struct ext2_dirent),
563 dir->namelen) == 0) {
564 previous_dir->direntlen +=
566 inodeno = dir->inode;
573 if (fs->blksz - totalbytes == dir->direntlen)
576 /* traversing the each directory entry */
577 templength = dir->direntlen;
578 totalbytes = totalbytes + templength;
580 dir = (struct ext2_dirent *)((char *)dir + templength);
600 static int find_dir_depth(char *dirname)
602 char *token = strtok(dirname, "/");
604 while (token != NULL) {
605 token = strtok(NULL, "/");
608 return count + 1 + 1;
610 * for example for string /home/temp
611 * depth=home(1)+temp(1)+1 extra for NULL;
616 static int parse_path(char **arr, char *dirname)
618 char *token = strtok(dirname, "/");
622 arr[i] = zalloc(strlen("/") + 1);
628 /* add each path entry after root */
629 while (token != NULL) {
630 arr[i] = zalloc(strlen(token) + 1);
633 memcpy(arr[i++], token, strlen(token));
634 token = strtok(NULL, "/");
641 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
643 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
650 * Function: ext4fs_get_parent_inode_num
651 * Return Value: inode Number of the parent directory of file/Directory to be
653 * dirname : Input parmater, input path name of the file/directory to be created
654 * dname : Output parameter, to be filled with the name of the directory
655 * extracted from dirname
657 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
661 int matched_inode_no;
662 int result_inode_no = -1;
664 char *depth_dirname = NULL;
665 char *parse_dirname = NULL;
666 struct ext2_inode *parent_inode = NULL;
667 struct ext2_inode *first_inode = NULL;
668 struct ext2_inode temp_inode;
670 if (*dirname != '/') {
671 printf("Please supply Absolute path\n");
675 /* TODO: input validation make equivalent to linux */
676 depth_dirname = zalloc(strlen(dirname) + 1);
680 memcpy(depth_dirname, dirname, strlen(dirname));
681 depth = find_dir_depth(depth_dirname);
682 parse_dirname = zalloc(strlen(dirname) + 1);
685 memcpy(parse_dirname, dirname, strlen(dirname));
687 /* allocate memory for each directory level */
688 ptr = zalloc((depth) * sizeof(char *));
691 if (parse_path(ptr, parse_dirname))
693 parent_inode = zalloc(sizeof(struct ext2_inode));
696 first_inode = zalloc(sizeof(struct ext2_inode));
699 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
700 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
702 result_inode_no = EXT2_ROOT_INO;
703 for (i = 1; i < depth; i++) {
704 matched_inode_no = search_dir(parent_inode, ptr[i]);
705 if (matched_inode_no == -1) {
706 if (ptr[i + 1] == NULL && i == 1) {
707 result_inode_no = EXT2_ROOT_INO;
710 if (ptr[i + 1] == NULL)
712 printf("Invalid path\n");
713 result_inode_no = -1;
717 if (ptr[i + 1] != NULL) {
718 memset(parent_inode, '\0',
719 sizeof(struct ext2_inode));
720 if (ext4fs_iget(matched_inode_no,
722 result_inode_no = -1;
725 result_inode_no = matched_inode_no;
734 matched_inode_no = search_dir(first_inode, ptr[i]);
736 matched_inode_no = search_dir(parent_inode, ptr[i]);
738 if (matched_inode_no != -1) {
739 ext4fs_iget(matched_inode_no, &temp_inode);
740 if (temp_inode.mode & S_IFDIR) {
741 printf("It is a Directory\n");
742 result_inode_no = -1;
747 if (strlen(ptr[i]) > 256) {
748 result_inode_no = -1;
751 memcpy(dname, ptr[i], strlen(ptr[i]));
760 return result_inode_no;
763 static int check_filename(char *filename, unsigned int blknr)
765 unsigned int first_block_no_of_root;
770 char *root_first_block_buffer = NULL;
771 char *root_first_block_addr = NULL;
772 struct ext2_dirent *dir = NULL;
773 struct ext2_dirent *previous_dir = NULL;
775 struct ext_filesystem *fs = get_fs();
777 /* get the first block of root */
778 first_block_no_of_root = blknr;
779 root_first_block_buffer = zalloc(fs->blksz);
780 if (!root_first_block_buffer)
782 root_first_block_addr = root_first_block_buffer;
783 status = ext4fs_devread((lbaint_t)first_block_no_of_root *
785 fs->blksz, root_first_block_buffer);
789 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
791 dir = (struct ext2_dirent *)root_first_block_buffer;
794 while (dir->direntlen >= 0) {
796 * blocksize-totalbytes because last
797 * directory length i.e., *dir->direntlen
798 * is free availble space in the block that
799 * means it is a last entry of directory entry
801 if (strlen(filename) == dir->namelen) {
802 if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
803 dir->namelen) == 0) {
804 printf("file found deleting\n");
805 previous_dir->direntlen += dir->direntlen;
806 inodeno = dir->inode;
813 if (fs->blksz - totalbytes == dir->direntlen)
816 /* traversing the each directory entry */
817 templength = dir->direntlen;
818 totalbytes = totalbytes + templength;
820 dir = (struct ext2_dirent *)((char *)dir + templength);
826 if (ext4fs_put_metadata(root_first_block_addr,
827 first_block_no_of_root))
832 free(root_first_block_buffer);
837 int ext4fs_filename_check(char *filename)
839 short direct_blk_idx = 0;
843 /* read the block no allocated to a file */
844 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
846 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
849 inodeno = check_filename(filename, blknr);
857 long int ext4fs_get_new_blk_no(void)
863 static int prev_bg_bitmap_index = -1;
864 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
865 struct ext_filesystem *fs = get_fs();
866 char *journal_buffer = zalloc(fs->blksz);
867 char *zero_buffer = zalloc(fs->blksz);
868 if (!journal_buffer || !zero_buffer)
870 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
872 if (fs->first_pass_bbmap == 0) {
873 for (i = 0; i < fs->no_blkgrp; i++) {
874 if (bgd[i].free_blocks) {
875 if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
876 put_ext4(((uint64_t) ((uint64_t)bgd[i].block_id *
877 (uint64_t)fs->blksz)),
878 zero_buffer, fs->blksz);
881 bg_flags & ~EXT4_BG_BLOCK_UNINIT;
882 memcpy(fs->blk_bmaps[i], zero_buffer,
886 _get_new_blk_no(fs->blk_bmaps[i]);
887 if (fs->curr_blkno == -1)
888 /* if block bitmap is completely fill */
890 fs->curr_blkno = fs->curr_blkno +
892 fs->first_pass_bbmap++;
893 bgd[i].free_blocks--;
894 fs->sb->free_blocks--;
895 status = ext4fs_devread((lbaint_t)
902 if (ext4fs_log_journal(journal_buffer,
907 debug("no space left on block group %d\n", i);
915 /* get the blockbitmap index respective to blockno */
916 if (fs->blksz != 1024) {
917 bg_idx = fs->curr_blkno / blk_per_grp;
919 bg_idx = fs->curr_blkno / blk_per_grp;
920 remainder = fs->curr_blkno % blk_per_grp;
926 * To skip completely filled block group bitmaps
927 * Optimize the block allocation
929 if (bg_idx >= fs->no_blkgrp)
932 if (bgd[bg_idx].free_blocks == 0) {
933 debug("block group %u is full. Skipping\n", bg_idx);
934 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
939 if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) {
940 memset(zero_buffer, '\0', fs->blksz);
941 put_ext4(((uint64_t) ((uint64_t)bgd[bg_idx].block_id *
942 (uint64_t)fs->blksz)), zero_buffer, fs->blksz);
943 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
944 bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags &
945 ~EXT4_BG_BLOCK_UNINIT;
948 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
950 debug("going for restart for the block no %ld %u\n",
951 fs->curr_blkno, bg_idx);
956 if (prev_bg_bitmap_index != bg_idx) {
957 memset(journal_buffer, '\0', fs->blksz);
958 status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id
960 0, fs->blksz, journal_buffer);
963 if (ext4fs_log_journal(journal_buffer,
964 bgd[bg_idx].block_id))
967 prev_bg_bitmap_index = bg_idx;
969 bgd[bg_idx].free_blocks--;
970 fs->sb->free_blocks--;
974 free(journal_buffer);
977 return fs->curr_blkno;
979 free(journal_buffer);
985 int ext4fs_get_new_inode_no(void)
989 unsigned int ibmap_idx;
990 static int prev_inode_bitmap_index = -1;
991 unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group;
992 struct ext_filesystem *fs = get_fs();
993 char *journal_buffer = zalloc(fs->blksz);
994 char *zero_buffer = zalloc(fs->blksz);
995 if (!journal_buffer || !zero_buffer)
997 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
999 if (fs->first_pass_ibmap == 0) {
1000 for (i = 0; i < fs->no_blkgrp; i++) {
1001 if (bgd[i].free_inodes) {
1002 if (bgd[i].bg_itable_unused !=
1004 bgd[i].bg_itable_unused =
1006 if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) {
1007 put_ext4(((uint64_t)
1008 ((uint64_t)bgd[i].inode_id *
1009 (uint64_t)fs->blksz)),
1010 zero_buffer, fs->blksz);
1011 bgd[i].bg_flags = bgd[i].bg_flags &
1012 ~EXT4_BG_INODE_UNINIT;
1013 memcpy(fs->inode_bmaps[i],
1014 zero_buffer, fs->blksz);
1017 _get_new_inode_no(fs->inode_bmaps[i]);
1018 if (fs->curr_inode_no == -1)
1019 /* if block bitmap is completely fill */
1021 fs->curr_inode_no = fs->curr_inode_no +
1022 (i * inodes_per_grp);
1023 fs->first_pass_ibmap++;
1024 bgd[i].free_inodes--;
1025 bgd[i].bg_itable_unused--;
1026 fs->sb->free_inodes--;
1027 status = ext4fs_devread((lbaint_t)
1034 if (ext4fs_log_journal(journal_buffer,
1039 debug("no inode left on block group %d\n", i);
1044 fs->curr_inode_no++;
1045 /* get the blockbitmap index respective to blockno */
1046 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1047 if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) {
1048 memset(zero_buffer, '\0', fs->blksz);
1049 put_ext4(((uint64_t) ((uint64_t)bgd[ibmap_idx].inode_id *
1050 (uint64_t)fs->blksz)), zero_buffer,
1052 bgd[ibmap_idx].bg_flags =
1053 bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT;
1054 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1058 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1059 fs->inode_bmaps[ibmap_idx],
1061 debug("going for restart for the block no %d %u\n",
1062 fs->curr_inode_no, ibmap_idx);
1066 /* journal backup */
1067 if (prev_inode_bitmap_index != ibmap_idx) {
1068 memset(journal_buffer, '\0', fs->blksz);
1069 status = ext4fs_devread((lbaint_t)
1070 bgd[ibmap_idx].inode_id
1072 0, fs->blksz, journal_buffer);
1075 if (ext4fs_log_journal(journal_buffer,
1076 bgd[ibmap_idx].inode_id))
1078 prev_inode_bitmap_index = ibmap_idx;
1080 if (bgd[ibmap_idx].bg_itable_unused !=
1081 bgd[ibmap_idx].free_inodes)
1082 bgd[ibmap_idx].bg_itable_unused =
1083 bgd[ibmap_idx].free_inodes;
1084 bgd[ibmap_idx].free_inodes--;
1085 bgd[ibmap_idx].bg_itable_unused--;
1086 fs->sb->free_inodes--;
1091 free(journal_buffer);
1094 return fs->curr_inode_no;
1096 free(journal_buffer);
1104 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1105 unsigned int *total_remaining_blocks,
1106 unsigned int *no_blks_reqd)
1110 long int actual_block_no;
1111 long int si_blockno;
1112 /* si :single indirect */
1113 unsigned int *si_buffer = NULL;
1114 unsigned int *si_start_addr = NULL;
1115 struct ext_filesystem *fs = get_fs();
1117 if (*total_remaining_blocks != 0) {
1118 si_buffer = zalloc(fs->blksz);
1120 printf("No Memory\n");
1123 si_start_addr = si_buffer;
1124 si_blockno = ext4fs_get_new_blk_no();
1125 if (si_blockno == -1) {
1126 printf("no block left to assign\n");
1130 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1132 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1133 0, fs->blksz, (char *)si_buffer);
1134 memset(si_buffer, '\0', fs->blksz);
1138 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1139 actual_block_no = ext4fs_get_new_blk_no();
1140 if (actual_block_no == -1) {
1141 printf("no block left to assign\n");
1144 *si_buffer = actual_block_no;
1145 debug("SIAB %u: %u\n", *si_buffer,
1146 *total_remaining_blocks);
1149 (*total_remaining_blocks)--;
1150 if (*total_remaining_blocks == 0)
1154 /* write the block to disk */
1155 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1156 si_start_addr, fs->blksz);
1157 file_inode->b.blocks.indir_block = si_blockno;
1160 free(si_start_addr);
1163 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1164 unsigned int *total_remaining_blocks,
1165 unsigned int *no_blks_reqd)
1170 long int actual_block_no;
1171 /* di:double indirect */
1172 long int di_blockno_parent;
1173 long int di_blockno_child;
1174 unsigned int *di_parent_buffer = NULL;
1175 unsigned int *di_child_buff = NULL;
1176 unsigned int *di_block_start_addr = NULL;
1177 unsigned int *di_child_buff_start = NULL;
1178 struct ext_filesystem *fs = get_fs();
1180 if (*total_remaining_blocks != 0) {
1181 /* double indirect parent block connecting to inode */
1182 di_blockno_parent = ext4fs_get_new_blk_no();
1183 if (di_blockno_parent == -1) {
1184 printf("no block left to assign\n");
1187 di_parent_buffer = zalloc(fs->blksz);
1188 if (!di_parent_buffer)
1191 di_block_start_addr = di_parent_buffer;
1193 debug("DIPB %ld: %u\n", di_blockno_parent,
1194 *total_remaining_blocks);
1196 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1198 fs->blksz, (char *)di_parent_buffer);
1201 printf("%s: Device read error!\n", __func__);
1204 memset(di_parent_buffer, '\0', fs->blksz);
1207 * start:for each double indirect parent
1208 * block create one more block
1210 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1211 di_blockno_child = ext4fs_get_new_blk_no();
1212 if (di_blockno_child == -1) {
1213 printf("no block left to assign\n");
1216 di_child_buff = zalloc(fs->blksz);
1220 di_child_buff_start = di_child_buff;
1221 *di_parent_buffer = di_blockno_child;
1224 debug("DICB %ld: %u\n", di_blockno_child,
1225 *total_remaining_blocks);
1227 status = ext4fs_devread((lbaint_t)di_blockno_child *
1230 (char *)di_child_buff);
1233 printf("%s: Device read error!\n", __func__);
1236 memset(di_child_buff, '\0', fs->blksz);
1237 /* filling of actual datablocks for each child */
1238 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1239 actual_block_no = ext4fs_get_new_blk_no();
1240 if (actual_block_no == -1) {
1241 printf("no block left to assign\n");
1244 *di_child_buff = actual_block_no;
1245 debug("DIAB %ld: %u\n", actual_block_no,
1246 *total_remaining_blocks);
1249 (*total_remaining_blocks)--;
1250 if (*total_remaining_blocks == 0)
1253 /* write the block table */
1254 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1255 di_child_buff_start, fs->blksz);
1256 free(di_child_buff_start);
1257 di_child_buff_start = NULL;
1259 if (*total_remaining_blocks == 0)
1262 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1263 di_block_start_addr, fs->blksz);
1264 file_inode->b.blocks.double_indir_block = di_blockno_parent;
1267 free(di_block_start_addr);
1270 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1271 unsigned int *total_remaining_blocks,
1272 unsigned int *no_blks_reqd)
1277 long int actual_block_no;
1278 /* ti: Triple Indirect */
1279 long int ti_gp_blockno;
1280 long int ti_parent_blockno;
1281 long int ti_child_blockno;
1282 unsigned int *ti_gp_buff = NULL;
1283 unsigned int *ti_parent_buff = NULL;
1284 unsigned int *ti_child_buff = NULL;
1285 unsigned int *ti_gp_buff_start_addr = NULL;
1286 unsigned int *ti_pbuff_start_addr = NULL;
1287 unsigned int *ti_cbuff_start_addr = NULL;
1288 struct ext_filesystem *fs = get_fs();
1289 if (*total_remaining_blocks != 0) {
1290 /* triple indirect grand parent block connecting to inode */
1291 ti_gp_blockno = ext4fs_get_new_blk_no();
1292 if (ti_gp_blockno == -1) {
1293 printf("no block left to assign\n");
1296 ti_gp_buff = zalloc(fs->blksz);
1300 ti_gp_buff_start_addr = ti_gp_buff;
1302 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1303 *total_remaining_blocks);
1305 /* for each 4 byte grand parent entry create one more block */
1306 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1307 ti_parent_blockno = ext4fs_get_new_blk_no();
1308 if (ti_parent_blockno == -1) {
1309 printf("no block left to assign\n");
1312 ti_parent_buff = zalloc(fs->blksz);
1313 if (!ti_parent_buff)
1316 ti_pbuff_start_addr = ti_parent_buff;
1317 *ti_gp_buff = ti_parent_blockno;
1320 debug("TIPB %ld: %u\n", ti_parent_blockno,
1321 *total_remaining_blocks);
1323 /* for each 4 byte entry parent create one more block */
1324 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1325 ti_child_blockno = ext4fs_get_new_blk_no();
1326 if (ti_child_blockno == -1) {
1327 printf("no block left assign\n");
1330 ti_child_buff = zalloc(fs->blksz);
1334 ti_cbuff_start_addr = ti_child_buff;
1335 *ti_parent_buff = ti_child_blockno;
1338 debug("TICB %ld: %u\n", ti_parent_blockno,
1339 *total_remaining_blocks);
1341 /* fill actual datablocks for each child */
1342 for (k = 0; k < (fs->blksz / sizeof(int));
1345 ext4fs_get_new_blk_no();
1346 if (actual_block_no == -1) {
1347 printf("no block left\n");
1350 *ti_child_buff = actual_block_no;
1351 debug("TIAB %ld: %u\n", actual_block_no,
1352 *total_remaining_blocks);
1355 (*total_remaining_blocks)--;
1356 if (*total_remaining_blocks == 0)
1359 /* write the child block */
1360 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1361 (uint64_t)fs->blksz)),
1362 ti_cbuff_start_addr, fs->blksz);
1363 free(ti_cbuff_start_addr);
1365 if (*total_remaining_blocks == 0)
1368 /* write the parent block */
1369 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1370 ti_pbuff_start_addr, fs->blksz);
1371 free(ti_pbuff_start_addr);
1373 if (*total_remaining_blocks == 0)
1376 /* write the grand parent block */
1377 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1378 ti_gp_buff_start_addr, fs->blksz);
1379 file_inode->b.blocks.triple_indir_block = ti_gp_blockno;
1382 free(ti_gp_buff_start_addr);
1385 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1386 unsigned int total_remaining_blocks,
1387 unsigned int *total_no_of_block)
1390 long int direct_blockno;
1391 unsigned int no_blks_reqd = 0;
1393 /* allocation of direct blocks */
1394 for (i = 0; i < INDIRECT_BLOCKS; i++) {
1395 direct_blockno = ext4fs_get_new_blk_no();
1396 if (direct_blockno == -1) {
1397 printf("no block left to assign\n");
1400 file_inode->b.blocks.dir_blocks[i] = direct_blockno;
1401 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1403 total_remaining_blocks--;
1404 if (total_remaining_blocks == 0)
1408 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1410 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1412 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1414 *total_no_of_block += no_blks_reqd;
1419 static void ext4fs_extent_cache_insert(struct ext4_extent_node *new)
1421 struct ext4_extent_node *node;
1423 list_for_each_entry(node, &ext4_extent_lh, lh)
1424 if (node->block > new->block) {
1425 list_add_tail(&new->lh, &node->lh);
1428 list_add_tail(&new->lh, &ext4_extent_lh);
1431 static int __ext4fs_build_extent_cache(struct ext2_data *data,
1432 struct ext4_extent_header *ext_block)
1434 int blksz = EXT2_BLOCK_SIZE(data);
1435 int log2_blksz = LOG2_BLOCK_SIZE(data)
1436 - get_fs()->dev_desc->log2blksz;
1437 struct ext4_extent_node *node;
1438 struct ext4_extent_idx *index;
1439 struct ext4_extent *extent;
1440 unsigned long long block;
1444 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1447 if (ext_block->eh_depth == 0) {
1448 extent = (struct ext4_extent *)(ext_block + 1);
1449 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1450 node = malloc(sizeof(*node));
1453 node->block = le32_to_cpu(extent[i].ee_block);
1454 node->len = le16_to_cpu(extent[i].ee_len);
1455 node->start = le16_to_cpu(extent[i].ee_start_hi);
1456 node->start = (node->start << 32) +
1457 le32_to_cpu(extent[i].ee_start_lo);
1458 ext4fs_extent_cache_insert(node);
1463 index = (struct ext4_extent_idx *)(ext_block + 1);
1464 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1465 buf = malloc(blksz);
1469 block = le16_to_cpu(index[i].ei_leaf_hi);
1470 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1472 if (!ext4fs_devread(block << log2_blksz, 0, blksz, buf)) {
1477 err = __ext4fs_build_extent_cache(data,
1478 (struct ext4_extent_header *) buf);
1487 int ext4fs_build_extent_cache(struct ext2_inode *inode)
1489 return __ext4fs_build_extent_cache(ext4fs_root,
1490 (struct ext4_extent_header *)
1491 inode->b.blocks.dir_blocks);
1494 void ext4fs_free_extent_cache(void)
1496 struct ext4_extent_node *node, *tmp;
1498 list_for_each_entry_safe(node, tmp, &ext4_extent_lh, lh) {
1499 list_del(&node->lh);
1504 static struct ext4_extent_node *ext4fs_extent_cache_get(uint32_t block)
1506 struct ext4_extent_node *node;
1508 list_for_each_entry(node, &ext4_extent_lh, lh)
1509 if (block >= node->block && block < node->block + node->len)
1515 static int ext4fs_blockgroup
1516 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1519 unsigned int blkoff, desc_per_blk;
1520 int log2blksz = get_fs()->dev_desc->log2blksz;
1522 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1524 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
1525 group / desc_per_blk;
1526 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1528 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1529 group, blkno, blkoff);
1531 return ext4fs_devread((lbaint_t)blkno <<
1532 (LOG2_BLOCK_SIZE(data) - log2blksz),
1533 blkoff, sizeof(struct ext2_block_group),
1537 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1539 struct ext2_block_group blkgrp;
1540 struct ext2_sblock *sblock = &data->sblock;
1541 struct ext_filesystem *fs = get_fs();
1542 int log2blksz = get_fs()->dev_desc->log2blksz;
1543 int inodes_per_block, status;
1545 unsigned int blkoff;
1547 /* It is easier to calculate if the first inode is 0. */
1549 status = ext4fs_blockgroup(data, ino / __le32_to_cpu
1550 (sblock->inodes_per_group), &blkgrp);
1554 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1555 blkno = __le32_to_cpu(blkgrp.inode_table_id) +
1556 (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1557 blkoff = (ino % inodes_per_block) * fs->inodesz;
1558 /* Read the inode. */
1559 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1561 sizeof(struct ext2_inode), (char *)inode);
1568 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1575 long int perblock_parent;
1576 long int perblock_child;
1578 /* get the blocksize of the filesystem */
1579 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1580 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1581 - get_fs()->dev_desc->log2blksz;
1583 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1584 struct ext4_extent_node *node;
1586 node = ext4fs_extent_cache_get(fileblock);
1588 printf("Extent Error\n");
1592 return fileblock - node->block + node->start;
1595 /* Direct blocks. */
1596 if (fileblock < INDIRECT_BLOCKS)
1597 blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1600 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1601 if (ext4fs_indir1_block == NULL) {
1602 ext4fs_indir1_block = zalloc(blksz);
1603 if (ext4fs_indir1_block == NULL) {
1604 printf("** SI ext2fs read block (indir 1)"
1605 "malloc failed. **\n");
1608 ext4fs_indir1_size = blksz;
1609 ext4fs_indir1_blkno = -1;
1611 if (blksz != ext4fs_indir1_size) {
1612 free(ext4fs_indir1_block);
1613 ext4fs_indir1_block = NULL;
1614 ext4fs_indir1_size = 0;
1615 ext4fs_indir1_blkno = -1;
1616 ext4fs_indir1_block = zalloc(blksz);
1617 if (ext4fs_indir1_block == NULL) {
1618 printf("** SI ext2fs read block (indir 1):"
1619 "malloc failed. **\n");
1622 ext4fs_indir1_size = blksz;
1624 if ((__le32_to_cpu(inode->b.blocks.indir_block) <<
1625 log2_blksz) != ext4fs_indir1_blkno) {
1627 ext4fs_devread((lbaint_t)__le32_to_cpu
1629 indir_block) << log2_blksz, 0,
1630 blksz, (char *)ext4fs_indir1_block);
1632 printf("** SI ext2fs read block (indir 1)"
1636 ext4fs_indir1_blkno =
1637 __le32_to_cpu(inode->b.blocks.
1638 indir_block) << log2_blksz;
1640 blknr = __le32_to_cpu(ext4fs_indir1_block
1641 [fileblock - INDIRECT_BLOCKS]);
1643 /* Double indirect. */
1644 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1645 (blksz / 4 + 1)))) {
1647 long int perblock = blksz / 4;
1648 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1650 if (ext4fs_indir1_block == NULL) {
1651 ext4fs_indir1_block = zalloc(blksz);
1652 if (ext4fs_indir1_block == NULL) {
1653 printf("** DI ext2fs read block (indir 2 1)"
1654 "malloc failed. **\n");
1657 ext4fs_indir1_size = blksz;
1658 ext4fs_indir1_blkno = -1;
1660 if (blksz != ext4fs_indir1_size) {
1661 free(ext4fs_indir1_block);
1662 ext4fs_indir1_block = NULL;
1663 ext4fs_indir1_size = 0;
1664 ext4fs_indir1_blkno = -1;
1665 ext4fs_indir1_block = zalloc(blksz);
1666 if (ext4fs_indir1_block == NULL) {
1667 printf("** DI ext2fs read block (indir 2 1)"
1668 "malloc failed. **\n");
1671 ext4fs_indir1_size = blksz;
1673 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) <<
1674 log2_blksz) != ext4fs_indir1_blkno) {
1676 ext4fs_devread((lbaint_t)__le32_to_cpu
1678 double_indir_block) << log2_blksz,
1680 (char *)ext4fs_indir1_block);
1682 printf("** DI ext2fs read block (indir 2 1)"
1686 ext4fs_indir1_blkno =
1687 __le32_to_cpu(inode->b.blocks.double_indir_block) <<
1691 if (ext4fs_indir2_block == NULL) {
1692 ext4fs_indir2_block = zalloc(blksz);
1693 if (ext4fs_indir2_block == NULL) {
1694 printf("** DI ext2fs read block (indir 2 2)"
1695 "malloc failed. **\n");
1698 ext4fs_indir2_size = blksz;
1699 ext4fs_indir2_blkno = -1;
1701 if (blksz != ext4fs_indir2_size) {
1702 free(ext4fs_indir2_block);
1703 ext4fs_indir2_block = NULL;
1704 ext4fs_indir2_size = 0;
1705 ext4fs_indir2_blkno = -1;
1706 ext4fs_indir2_block = zalloc(blksz);
1707 if (ext4fs_indir2_block == NULL) {
1708 printf("** DI ext2fs read block (indir 2 2)"
1709 "malloc failed. **\n");
1712 ext4fs_indir2_size = blksz;
1714 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1715 log2_blksz) != ext4fs_indir2_blkno) {
1716 status = ext4fs_devread((lbaint_t)__le32_to_cpu
1717 (ext4fs_indir1_block
1719 perblock]) << log2_blksz, 0,
1721 (char *)ext4fs_indir2_block);
1723 printf("** DI ext2fs read block (indir 2 2)"
1727 ext4fs_indir2_blkno =
1728 __le32_to_cpu(ext4fs_indir1_block[rblock
1733 blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1735 /* Tripple indirect. */
1737 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1738 (blksz / 4 * blksz / 4));
1739 perblock_child = blksz / 4;
1740 perblock_parent = ((blksz / 4) * (blksz / 4));
1742 if (ext4fs_indir1_block == NULL) {
1743 ext4fs_indir1_block = zalloc(blksz);
1744 if (ext4fs_indir1_block == NULL) {
1745 printf("** TI ext2fs read block (indir 2 1)"
1746 "malloc failed. **\n");
1749 ext4fs_indir1_size = blksz;
1750 ext4fs_indir1_blkno = -1;
1752 if (blksz != ext4fs_indir1_size) {
1753 free(ext4fs_indir1_block);
1754 ext4fs_indir1_block = NULL;
1755 ext4fs_indir1_size = 0;
1756 ext4fs_indir1_blkno = -1;
1757 ext4fs_indir1_block = zalloc(blksz);
1758 if (ext4fs_indir1_block == NULL) {
1759 printf("** TI ext2fs read block (indir 2 1)"
1760 "malloc failed. **\n");
1763 ext4fs_indir1_size = blksz;
1765 if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1766 log2_blksz) != ext4fs_indir1_blkno) {
1767 status = ext4fs_devread
1769 __le32_to_cpu(inode->b.blocks.triple_indir_block)
1770 << log2_blksz, 0, blksz,
1771 (char *)ext4fs_indir1_block);
1773 printf("** TI ext2fs read block (indir 2 1)"
1777 ext4fs_indir1_blkno =
1778 __le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1782 if (ext4fs_indir2_block == NULL) {
1783 ext4fs_indir2_block = zalloc(blksz);
1784 if (ext4fs_indir2_block == NULL) {
1785 printf("** TI ext2fs read block (indir 2 2)"
1786 "malloc failed. **\n");
1789 ext4fs_indir2_size = blksz;
1790 ext4fs_indir2_blkno = -1;
1792 if (blksz != ext4fs_indir2_size) {
1793 free(ext4fs_indir2_block);
1794 ext4fs_indir2_block = NULL;
1795 ext4fs_indir2_size = 0;
1796 ext4fs_indir2_blkno = -1;
1797 ext4fs_indir2_block = zalloc(blksz);
1798 if (ext4fs_indir2_block == NULL) {
1799 printf("** TI ext2fs read block (indir 2 2)"
1800 "malloc failed. **\n");
1803 ext4fs_indir2_size = blksz;
1805 if ((__le32_to_cpu(ext4fs_indir1_block[rblock /
1806 perblock_parent]) <<
1808 != ext4fs_indir2_blkno) {
1809 status = ext4fs_devread((lbaint_t)__le32_to_cpu
1810 (ext4fs_indir1_block
1812 perblock_parent]) <<
1813 log2_blksz, 0, blksz,
1814 (char *)ext4fs_indir2_block);
1816 printf("** TI ext2fs read block (indir 2 2)"
1820 ext4fs_indir2_blkno =
1821 __le32_to_cpu(ext4fs_indir1_block[rblock /
1826 if (ext4fs_indir3_block == NULL) {
1827 ext4fs_indir3_block = zalloc(blksz);
1828 if (ext4fs_indir3_block == NULL) {
1829 printf("** TI ext2fs read block (indir 2 2)"
1830 "malloc failed. **\n");
1833 ext4fs_indir3_size = blksz;
1834 ext4fs_indir3_blkno = -1;
1836 if (blksz != ext4fs_indir3_size) {
1837 free(ext4fs_indir3_block);
1838 ext4fs_indir3_block = NULL;
1839 ext4fs_indir3_size = 0;
1840 ext4fs_indir3_blkno = -1;
1841 ext4fs_indir3_block = zalloc(blksz);
1842 if (ext4fs_indir3_block == NULL) {
1843 printf("** TI ext2fs read block (indir 2 2)"
1844 "malloc failed. **\n");
1847 ext4fs_indir3_size = blksz;
1849 if ((__le32_to_cpu(ext4fs_indir2_block[rblock
1852 log2_blksz) != ext4fs_indir3_blkno) {
1854 ext4fs_devread((lbaint_t)__le32_to_cpu
1855 (ext4fs_indir2_block
1856 [(rblock / perblock_child)
1857 % (blksz / 4)]) << log2_blksz, 0,
1858 blksz, (char *)ext4fs_indir3_block);
1860 printf("** TI ext2fs read block (indir 2 2)"
1864 ext4fs_indir3_blkno =
1865 __le32_to_cpu(ext4fs_indir2_block[(rblock /
1872 blknr = __le32_to_cpu(ext4fs_indir3_block
1873 [rblock % perblock_child]);
1875 debug("read_allocated_block %ld\n", blknr);
1880 void ext4fs_close(void)
1882 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1883 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1886 if (ext4fs_root != NULL) {
1890 if (ext4fs_indir1_block != NULL) {
1891 free(ext4fs_indir1_block);
1892 ext4fs_indir1_block = NULL;
1893 ext4fs_indir1_size = 0;
1894 ext4fs_indir1_blkno = -1;
1896 if (ext4fs_indir2_block != NULL) {
1897 free(ext4fs_indir2_block);
1898 ext4fs_indir2_block = NULL;
1899 ext4fs_indir2_size = 0;
1900 ext4fs_indir2_blkno = -1;
1902 if (ext4fs_indir3_block != NULL) {
1903 free(ext4fs_indir3_block);
1904 ext4fs_indir3_block = NULL;
1905 ext4fs_indir3_size = 0;
1906 ext4fs_indir3_blkno = -1;
1910 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1911 struct ext2fs_node **fnode, int *ftype)
1913 unsigned int fpos = 0;
1915 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1919 printf("Iterate dir %s\n", name);
1920 #endif /* of DEBUG */
1921 if (!diro->inode_read) {
1922 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1926 /* Search the file. */
1927 while (fpos < __le32_to_cpu(diro->inode.size)) {
1928 struct ext2_dirent dirent;
1930 status = ext4fs_read_file(diro, fpos,
1931 sizeof(struct ext2_dirent),
1936 if (dirent.namelen != 0) {
1937 char filename[dirent.namelen + 1];
1938 struct ext2fs_node *fdiro;
1939 int type = FILETYPE_UNKNOWN;
1941 status = ext4fs_read_file(diro,
1943 sizeof(struct ext2_dirent),
1944 dirent.namelen, filename);
1948 fdiro = zalloc(sizeof(struct ext2fs_node));
1952 fdiro->data = diro->data;
1953 fdiro->ino = __le32_to_cpu(dirent.inode);
1955 filename[dirent.namelen] = '\0';
1957 if (dirent.filetype != FILETYPE_UNKNOWN) {
1958 fdiro->inode_read = 0;
1960 if (dirent.filetype == FILETYPE_DIRECTORY)
1961 type = FILETYPE_DIRECTORY;
1962 else if (dirent.filetype == FILETYPE_SYMLINK)
1963 type = FILETYPE_SYMLINK;
1964 else if (dirent.filetype == FILETYPE_REG)
1965 type = FILETYPE_REG;
1967 status = ext4fs_read_inode(diro->data,
1975 fdiro->inode_read = 1;
1977 if ((__le16_to_cpu(fdiro->inode.mode) &
1978 FILETYPE_INO_MASK) ==
1979 FILETYPE_INO_DIRECTORY) {
1980 type = FILETYPE_DIRECTORY;
1981 } else if ((__le16_to_cpu(fdiro->inode.mode)
1982 & FILETYPE_INO_MASK) ==
1983 FILETYPE_INO_SYMLINK) {
1984 type = FILETYPE_SYMLINK;
1985 } else if ((__le16_to_cpu(fdiro->inode.mode)
1986 & FILETYPE_INO_MASK) ==
1988 type = FILETYPE_REG;
1992 printf("iterate >%s<\n", filename);
1993 #endif /* of DEBUG */
1994 if ((name != NULL) && (fnode != NULL)
1995 && (ftype != NULL)) {
1996 if (strcmp(filename, name) == 0) {
2002 if (fdiro->inode_read == 0) {
2003 status = ext4fs_read_inode(diro->data,
2011 fdiro->inode_read = 1;
2014 case FILETYPE_DIRECTORY:
2017 case FILETYPE_SYMLINK:
2028 __le32_to_cpu(fdiro->inode.size),
2033 fpos += __le16_to_cpu(dirent.direntlen);
2038 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2041 struct ext2fs_node *diro = node;
2044 if (!diro->inode_read) {
2045 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2049 symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1);
2053 if (__le32_to_cpu(diro->inode.size) <= 60) {
2054 strncpy(symlink, diro->inode.b.symlink,
2055 __le32_to_cpu(diro->inode.size));
2057 status = ext4fs_read_file(diro, 0,
2058 __le32_to_cpu(diro->inode.size),
2065 symlink[__le32_to_cpu(diro->inode.size)] = '\0';
2069 static int ext4fs_find_file1(const char *currpath,
2070 struct ext2fs_node *currroot,
2071 struct ext2fs_node **currfound, int *foundtype)
2073 char fpath[strlen(currpath) + 1];
2077 int type = FILETYPE_DIRECTORY;
2078 struct ext2fs_node *currnode = currroot;
2079 struct ext2fs_node *oldnode = currroot;
2081 strncpy(fpath, currpath, strlen(currpath) + 1);
2083 /* Remove all leading slashes. */
2084 while (*name == '/')
2088 *currfound = currnode;
2095 /* Extract the actual part from the pathname. */
2096 next = strchr(name, '/');
2098 /* Remove all leading slashes. */
2099 while (*next == '/')
2103 if (type != FILETYPE_DIRECTORY) {
2104 ext4fs_free_node(currnode, currroot);
2110 /* Iterate over the directory. */
2111 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2118 /* Read in the symlink and follow it. */
2119 if (type == FILETYPE_SYMLINK) {
2122 /* Test if the symlink does not loop. */
2123 if (++symlinknest == 8) {
2124 ext4fs_free_node(currnode, currroot);
2125 ext4fs_free_node(oldnode, currroot);
2129 symlink = ext4fs_read_symlink(currnode);
2130 ext4fs_free_node(currnode, currroot);
2133 ext4fs_free_node(oldnode, currroot);
2137 debug("Got symlink >%s<\n", symlink);
2139 if (symlink[0] == '/') {
2140 ext4fs_free_node(oldnode, currroot);
2141 oldnode = &ext4fs_root->diropen;
2144 /* Lookup the node the symlink points to. */
2145 status = ext4fs_find_file1(symlink, oldnode,
2151 ext4fs_free_node(oldnode, currroot);
2156 ext4fs_free_node(oldnode, currroot);
2158 /* Found the node! */
2159 if (!next || *next == '\0') {
2160 *currfound = currnode;
2169 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2170 struct ext2fs_node **foundnode, int expecttype)
2173 int foundtype = FILETYPE_DIRECTORY;
2179 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2183 /* Check if the node that was found was of the expected type. */
2184 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2186 else if ((expecttype == FILETYPE_DIRECTORY)
2187 && (foundtype != expecttype))
2193 int ext4fs_open(const char *filename)
2195 struct ext2fs_node *fdiro = NULL;
2199 if (ext4fs_root == NULL)
2203 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2208 if (!fdiro->inode_read) {
2209 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2214 len = __le32_to_cpu(fdiro->inode.size);
2215 ext4fs_file = fdiro;
2219 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2224 int ext4fs_mount(unsigned part_length)
2226 struct ext2_data *data;
2228 struct ext_filesystem *fs = get_fs();
2229 data = zalloc(SUPERBLOCK_SIZE);
2233 /* Read the superblock. */
2234 status = ext4_read_superblock((char *)&data->sblock);
2239 /* Make sure this is an ext2 filesystem. */
2240 if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2243 if (__le32_to_cpu(data->sblock.revision_level == 0))
2246 fs->inodesz = __le16_to_cpu(data->sblock.inode_size);
2248 debug("EXT2 rev %d, inode_size %d\n",
2249 __le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2251 data->diropen.data = data;
2252 data->diropen.ino = 2;
2253 data->diropen.inode_read = 1;
2254 data->inode = &data->diropen.inode;
2256 status = ext4fs_read_inode(data, 2, data->inode);
2264 printf("Failed to mount ext2 filesystem...\n");