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 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <ext_common.h>
39 #include <linux/stat.h>
40 #include <linux/time.h>
41 #include <asm/byteorder.h>
42 #include "ext4_common.h"
44 struct ext2_data *ext4fs_root;
45 struct ext2fs_node *ext4fs_file;
46 uint32_t *ext4fs_indir1_block;
47 int ext4fs_indir1_size;
48 int ext4fs_indir1_blkno = -1;
49 uint32_t *ext4fs_indir2_block;
50 int ext4fs_indir2_size;
51 int ext4fs_indir2_blkno = -1;
53 uint32_t *ext4fs_indir3_block;
54 int ext4fs_indir3_size;
55 int ext4fs_indir3_blkno = -1;
56 struct ext2_inode *g_parent_inode;
57 static int symlinknest;
59 extern unsigned long part_offset;
61 #if defined(CONFIG_EXT4_WRITE)
62 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
64 uint32_t res = size / n;
71 void put_ext4(uint64_t off, void *buf, uint32_t size)
75 unsigned char *temp_ptr = NULL;
76 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, SECTOR_SIZE);
77 struct ext_filesystem *fs = get_fs();
79 startblock = off / (uint64_t)SECTOR_SIZE;
80 startblock += part_offset;
81 remainder = off % (uint64_t)SECTOR_SIZE;
82 remainder &= SECTOR_SIZE - 1;
84 if (fs->dev_desc == NULL)
87 if ((startblock + (size / SECTOR_SIZE)) >
88 (part_offset + fs->total_sect)) {
89 printf("part_offset is %lu\n", part_offset);
90 printf("total_sector is %llu\n", fs->total_sect);
91 printf("error: overflow occurs\n");
96 if (fs->dev_desc->block_read) {
97 fs->dev_desc->block_read(fs->dev_desc->dev,
98 startblock, 1, sec_buf);
100 memcpy((temp_ptr + remainder),
101 (unsigned char *)buf, size);
102 fs->dev_desc->block_write(fs->dev_desc->dev,
103 startblock, 1, sec_buf);
106 if (size / SECTOR_SIZE != 0) {
107 fs->dev_desc->block_write(fs->dev_desc->dev,
110 (unsigned long *)buf);
112 fs->dev_desc->block_read(fs->dev_desc->dev,
113 startblock, 1, sec_buf);
115 memcpy(temp_ptr, buf, size);
116 fs->dev_desc->block_write(fs->dev_desc->dev,
118 (unsigned long *)sec_buf);
123 static int _get_new_inode_no(unsigned char *buffer)
125 struct ext_filesystem *fs = get_fs();
131 /* get the blocksize of the filesystem */
132 unsigned char *ptr = buffer;
133 while (*ptr == 255) {
136 if (count > ext4fs_root->sblock.inodes_per_group)
140 for (j = 0; j < fs->blksz; j++) {
145 status = input & operand;
160 static int _get_new_blk_no(unsigned char *buffer)
166 unsigned char *ptr = buffer;
167 struct ext_filesystem *fs = get_fs();
169 if (fs->blksz != 1024)
174 while (*ptr == 255) {
177 if (count == (fs->blksz * 8))
181 for (j = 0; j < fs->blksz; j++) {
186 status = input & operand;
201 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
203 int i, remainder, status;
204 unsigned char *ptr = buffer;
205 unsigned char operand;
207 remainder = blockno % 8;
208 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
210 i = i - (index * blocksize);
211 if (blocksize != 1024) {
213 operand = 1 << remainder;
214 status = *ptr & operand;
218 *ptr = *ptr | operand;
221 if (remainder == 0) {
226 operand = (1 << (remainder - 1));
228 status = *ptr & operand;
232 *ptr = *ptr | operand;
237 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
239 int i, remainder, status;
240 unsigned char *ptr = buffer;
241 unsigned char operand;
243 remainder = blockno % 8;
244 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
246 i = i - (index * blocksize);
247 if (blocksize != 1024) {
249 operand = (1 << remainder);
250 status = *ptr & operand;
252 *ptr = *ptr & ~(operand);
254 if (remainder == 0) {
259 operand = (1 << (remainder - 1));
261 status = *ptr & operand;
263 *ptr = *ptr & ~(operand);
267 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
269 int i, remainder, status;
270 unsigned char *ptr = buffer;
271 unsigned char operand;
273 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
275 remainder = inode_no % 8;
276 if (remainder == 0) {
281 operand = (1 << (remainder - 1));
283 status = *ptr & operand;
287 *ptr = *ptr | operand;
292 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
294 int i, remainder, status;
295 unsigned char *ptr = buffer;
296 unsigned char operand;
298 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
300 remainder = inode_no % 8;
301 if (remainder == 0) {
306 operand = (1 << (remainder - 1));
308 status = *ptr & operand;
310 *ptr = *ptr & ~(operand);
313 int ext4fs_checksum_update(unsigned int i)
315 struct ext2_block_group *desc;
316 struct ext_filesystem *fs = get_fs();
319 desc = (struct ext2_block_group *)&fs->bgd[i];
320 if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
321 int offset = offsetof(struct ext2_block_group, bg_checksum);
323 crc = ext2fs_crc16(~0, fs->sb->unique_id,
324 sizeof(fs->sb->unique_id));
325 crc = ext2fs_crc16(crc, &i, sizeof(i));
326 crc = ext2fs_crc16(crc, desc, offset);
327 offset += sizeof(desc->bg_checksum); /* skip checksum */
328 assert(offset == sizeof(*desc));
334 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
337 int sizeof_void_space;
338 int new_entry_byte_reqd;
339 short padding_factor = 0;
341 if (dir->namelen % 4 != 0)
342 padding_factor = 4 - (dir->namelen % 4);
344 dentry_length = sizeof(struct ext2_dirent) +
345 dir->namelen + padding_factor;
346 sizeof_void_space = dir->direntlen - dentry_length;
347 if (sizeof_void_space == 0)
351 if (strlen(filename) % 4 != 0)
352 padding_factor = 4 - (strlen(filename) % 4);
354 new_entry_byte_reqd = strlen(filename) +
355 sizeof(struct ext2_dirent) + padding_factor;
356 if (sizeof_void_space >= new_entry_byte_reqd) {
357 dir->direntlen = dentry_length;
358 return sizeof_void_space;
364 void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
366 unsigned int *zero_buffer = NULL;
367 char *root_first_block_buffer = NULL;
370 long int first_block_no_of_root = 0;
371 long int previous_blknr = -1;
373 short int padding_factor = 0;
374 unsigned int new_entry_byte_reqd;
375 unsigned int last_entry_dirlen;
376 int sizeof_void_space = 0;
380 struct ext_filesystem *fs = get_fs();
381 /* directory entry */
382 struct ext2_dirent *dir;
383 char *temp_dir = NULL;
385 zero_buffer = zalloc(fs->blksz);
387 printf("No Memory\n");
390 root_first_block_buffer = zalloc(fs->blksz);
391 if (!root_first_block_buffer) {
393 printf("No Memory\n");
398 /* read the block no allocated to a file */
399 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
401 root_blknr = read_allocated_block(g_parent_inode,
403 if (root_blknr == 0) {
404 first_block_no_of_root = previous_blknr;
407 previous_blknr = root_blknr;
410 status = ext4fs_devread(first_block_no_of_root
412 0, fs->blksz, root_first_block_buffer);
416 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
418 dir = (struct ext2_dirent *)root_first_block_buffer;
420 while (dir->direntlen > 0) {
422 * blocksize-totalbytes because last directory length
423 * i.e. dir->direntlen is free availble space in the
424 * block that means it is a last entry of directory
428 /* traversing the each directory entry */
429 if (fs->blksz - totalbytes == dir->direntlen) {
430 if (strlen(filename) % 4 != 0)
431 padding_factor = 4 - (strlen(filename) % 4);
433 new_entry_byte_reqd = strlen(filename) +
434 sizeof(struct ext2_dirent) + padding_factor;
437 * update last directory entry length to its
438 * length because we are creating new directory
441 if (dir->namelen % 4 != 0)
442 padding_factor = 4 - (dir->namelen % 4);
444 last_entry_dirlen = dir->namelen +
445 sizeof(struct ext2_dirent) + padding_factor;
446 if ((fs->blksz - totalbytes - last_entry_dirlen) <
447 new_entry_byte_reqd) {
448 printf("1st Block Full:Allocate new block\n");
450 if (direct_blk_idx == INDIRECT_BLOCKS - 1) {
451 printf("Directory exceeds limit\n");
454 g_parent_inode->b.blocks.dir_blocks
455 [direct_blk_idx] = ext4fs_get_new_blk_no();
456 if (g_parent_inode->b.blocks.dir_blocks
457 [direct_blk_idx] == -1) {
458 printf("no block left to assign\n");
463 blocks.dir_blocks[direct_blk_idx] *
464 fs->blksz)), zero_buffer, fs->blksz);
465 g_parent_inode->size =
466 g_parent_inode->size + fs->blksz;
467 g_parent_inode->blockcnt =
468 g_parent_inode->blockcnt + fs->sect_perblk;
469 if (ext4fs_put_metadata
470 (root_first_block_buffer,
471 first_block_no_of_root))
475 dir->direntlen = last_entry_dirlen;
479 templength = dir->direntlen;
480 totalbytes = totalbytes + templength;
481 sizeof_void_space = check_void_in_dentry(dir, filename);
482 if (sizeof_void_space)
485 dir = (struct ext2_dirent *)((char *)dir + templength);
488 /* make a pointer ready for creating next directory entry */
489 templength = dir->direntlen;
490 totalbytes = totalbytes + templength;
491 dir = (struct ext2_dirent *)((char *)dir + templength);
493 /* get the next available inode number */
494 inodeno = ext4fs_get_new_inode_no();
496 printf("no inode left to assign\n");
499 dir->inode = inodeno;
500 if (sizeof_void_space)
501 dir->direntlen = sizeof_void_space;
503 dir->direntlen = fs->blksz - totalbytes;
505 dir->namelen = strlen(filename);
506 dir->filetype = FILETYPE_REG; /* regular file */
507 temp_dir = (char *)dir;
508 temp_dir = temp_dir + sizeof(struct ext2_dirent);
509 memcpy(temp_dir, filename, strlen(filename));
513 /* update or write the 1st block of root inode */
514 if (ext4fs_put_metadata(root_first_block_buffer,
515 first_block_no_of_root))
520 free(root_first_block_buffer);
523 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
533 unsigned char *block_buffer = NULL;
534 struct ext2_dirent *dir = NULL;
535 struct ext2_dirent *previous_dir = NULL;
536 struct ext_filesystem *fs = get_fs();
538 /* read the block no allocated to a file */
539 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
541 blknr = read_allocated_block(parent_inode, direct_blk_idx);
545 /* read the blocks of parenet inode */
546 block_buffer = zalloc(fs->blksz);
550 status = ext4fs_devread(blknr * fs->sect_perblk,
551 0, fs->blksz, (char *)block_buffer);
555 dir = (struct ext2_dirent *)block_buffer;
558 while (dir->direntlen >= 0) {
560 * blocksize-totalbytes because last directory
561 * length i.e.,*dir->direntlen is free availble
562 * space in the block that means
563 * it is a last entry of directory entry
565 if (strlen(dirname) == dir->namelen) {
566 if (strncmp(dirname, ptr +
567 sizeof(struct ext2_dirent),
568 dir->namelen) == 0) {
569 previous_dir->direntlen +=
571 inodeno = dir->inode;
578 if (fs->blksz - totalbytes == dir->direntlen)
581 /* traversing the each directory entry */
582 templength = dir->direntlen;
583 totalbytes = totalbytes + templength;
585 dir = (struct ext2_dirent *)((char *)dir + templength);
605 static int find_dir_depth(char *dirname)
607 char *token = strtok(dirname, "/");
609 while (token != NULL) {
610 token = strtok(NULL, "/");
613 return count + 1 + 1;
615 * for example for string /home/temp
616 * depth=home(1)+temp(1)+1 extra for NULL;
621 static int parse_path(char **arr, char *dirname)
623 char *token = strtok(dirname, "/");
627 arr[i] = zalloc(strlen("/") + 1);
633 /* add each path entry after root */
634 while (token != NULL) {
635 arr[i] = zalloc(strlen(token) + 1);
638 memcpy(arr[i++], token, strlen(token));
639 token = strtok(NULL, "/");
646 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
648 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
655 * Function: ext4fs_get_parent_inode_num
656 * Return Value: inode Number of the parent directory of file/Directory to be
658 * dirname : Input parmater, input path name of the file/directory to be created
659 * dname : Output parameter, to be filled with the name of the directory
660 * extracted from dirname
662 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
666 int matched_inode_no;
667 int result_inode_no = -1;
669 char *depth_dirname = NULL;
670 char *parse_dirname = NULL;
671 struct ext2_inode *parent_inode = NULL;
672 struct ext2_inode *first_inode = NULL;
673 struct ext2_inode temp_inode;
675 if (*dirname != '/') {
676 printf("Please supply Absolute path\n");
680 /* TODO: input validation make equivalent to linux */
681 depth_dirname = zalloc(strlen(dirname) + 1);
685 memcpy(depth_dirname, dirname, strlen(dirname));
686 depth = find_dir_depth(depth_dirname);
687 parse_dirname = zalloc(strlen(dirname) + 1);
690 memcpy(parse_dirname, dirname, strlen(dirname));
692 /* allocate memory for each directory level */
693 ptr = zalloc((depth) * sizeof(char *));
696 if (parse_path(ptr, parse_dirname))
698 parent_inode = zalloc(sizeof(struct ext2_inode));
701 first_inode = zalloc(sizeof(struct ext2_inode));
704 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
705 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
707 result_inode_no = EXT2_ROOT_INO;
708 for (i = 1; i < depth; i++) {
709 matched_inode_no = search_dir(parent_inode, ptr[i]);
710 if (matched_inode_no == -1) {
711 if (ptr[i + 1] == NULL && i == 1) {
712 result_inode_no = EXT2_ROOT_INO;
715 if (ptr[i + 1] == NULL)
717 printf("Invalid path\n");
718 result_inode_no = -1;
722 if (ptr[i + 1] != NULL) {
723 memset(parent_inode, '\0',
724 sizeof(struct ext2_inode));
725 if (ext4fs_iget(matched_inode_no,
727 result_inode_no = -1;
730 result_inode_no = matched_inode_no;
739 matched_inode_no = search_dir(first_inode, ptr[i]);
741 matched_inode_no = search_dir(parent_inode, ptr[i]);
743 if (matched_inode_no != -1) {
744 ext4fs_iget(matched_inode_no, &temp_inode);
745 if (temp_inode.mode & S_IFDIR) {
746 printf("It is a Directory\n");
747 result_inode_no = -1;
752 if (strlen(ptr[i]) > 256) {
753 result_inode_no = -1;
756 memcpy(dname, ptr[i], strlen(ptr[i]));
765 return result_inode_no;
768 static int check_filename(char *filename, unsigned int blknr)
770 unsigned int first_block_no_of_root;
775 char *root_first_block_buffer = NULL;
776 char *root_first_block_addr = NULL;
777 struct ext2_dirent *dir = NULL;
778 struct ext2_dirent *previous_dir = NULL;
780 struct ext_filesystem *fs = get_fs();
782 /* get the first block of root */
783 first_block_no_of_root = blknr;
784 root_first_block_buffer = zalloc(fs->blksz);
785 if (!root_first_block_buffer)
787 root_first_block_addr = root_first_block_buffer;
788 status = ext4fs_devread(first_block_no_of_root *
790 fs->blksz, root_first_block_buffer);
794 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
796 dir = (struct ext2_dirent *)root_first_block_buffer;
799 while (dir->direntlen >= 0) {
801 * blocksize-totalbytes because last
802 * directory length i.e., *dir->direntlen
803 * is free availble space in the block that
804 * means it is a last entry of directory entry
806 if (strlen(filename) == dir->namelen) {
807 if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
808 dir->namelen) == 0) {
809 printf("file found deleting\n");
810 previous_dir->direntlen += dir->direntlen;
811 inodeno = dir->inode;
818 if (fs->blksz - totalbytes == dir->direntlen)
821 /* traversing the each directory entry */
822 templength = dir->direntlen;
823 totalbytes = totalbytes + templength;
825 dir = (struct ext2_dirent *)((char *)dir + templength);
831 if (ext4fs_put_metadata(root_first_block_addr,
832 first_block_no_of_root))
837 free(root_first_block_buffer);
842 int ext4fs_filename_check(char *filename)
844 short direct_blk_idx = 0;
848 /* read the block no allocated to a file */
849 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
851 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
854 inodeno = check_filename(filename, blknr);
862 long int ext4fs_get_new_blk_no(void)
868 static int prev_bg_bitmap_index = -1;
869 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
870 struct ext_filesystem *fs = get_fs();
871 char *journal_buffer = zalloc(fs->blksz);
872 char *zero_buffer = zalloc(fs->blksz);
873 if (!journal_buffer || !zero_buffer)
875 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
877 if (fs->first_pass_bbmap == 0) {
878 for (i = 0; i < fs->no_blkgrp; i++) {
879 if (bgd[i].free_blocks) {
880 if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
881 put_ext4(((uint64_t) (bgd[i].block_id *
883 zero_buffer, fs->blksz);
886 bg_flags & ~EXT4_BG_BLOCK_UNINIT;
887 memcpy(fs->blk_bmaps[i], zero_buffer,
891 _get_new_blk_no(fs->blk_bmaps[i]);
892 if (fs->curr_blkno == -1)
893 /* if block bitmap is completely fill */
895 fs->curr_blkno = fs->curr_blkno +
897 fs->first_pass_bbmap++;
898 bgd[i].free_blocks--;
899 fs->sb->free_blocks--;
900 status = ext4fs_devread(bgd[i].block_id *
906 if (ext4fs_log_journal(journal_buffer,
911 debug("no space left on block group %d\n", i);
919 /* get the blockbitmap index respective to blockno */
920 if (fs->blksz != 1024) {
921 bg_idx = fs->curr_blkno / blk_per_grp;
923 bg_idx = fs->curr_blkno / blk_per_grp;
924 remainder = fs->curr_blkno % blk_per_grp;
930 * To skip completely filled block group bitmaps
931 * Optimize the block allocation
933 if (bg_idx >= fs->no_blkgrp)
936 if (bgd[bg_idx].free_blocks == 0) {
937 debug("block group %u is full. Skipping\n", bg_idx);
938 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
943 if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) {
944 memset(zero_buffer, '\0', fs->blksz);
945 put_ext4(((uint64_t) (bgd[bg_idx].block_id *
946 fs->blksz)), zero_buffer, fs->blksz);
947 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
948 bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags &
949 ~EXT4_BG_BLOCK_UNINIT;
952 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
954 debug("going for restart for the block no %ld %u\n",
955 fs->curr_blkno, bg_idx);
960 if (prev_bg_bitmap_index != bg_idx) {
961 memset(journal_buffer, '\0', fs->blksz);
962 status = ext4fs_devread(bgd[bg_idx].block_id
964 0, fs->blksz, journal_buffer);
967 if (ext4fs_log_journal(journal_buffer,
968 bgd[bg_idx].block_id))
971 prev_bg_bitmap_index = bg_idx;
973 bgd[bg_idx].free_blocks--;
974 fs->sb->free_blocks--;
978 free(journal_buffer);
981 return fs->curr_blkno;
983 free(journal_buffer);
989 int ext4fs_get_new_inode_no(void)
993 unsigned int ibmap_idx;
994 static int prev_inode_bitmap_index = -1;
995 unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group;
996 struct ext_filesystem *fs = get_fs();
997 char *journal_buffer = zalloc(fs->blksz);
998 char *zero_buffer = zalloc(fs->blksz);
999 if (!journal_buffer || !zero_buffer)
1001 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
1003 if (fs->first_pass_ibmap == 0) {
1004 for (i = 0; i < fs->no_blkgrp; i++) {
1005 if (bgd[i].free_inodes) {
1006 if (bgd[i].bg_itable_unused !=
1008 bgd[i].bg_itable_unused =
1010 if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) {
1011 put_ext4(((uint64_t)
1014 zero_buffer, fs->blksz);
1015 bgd[i].bg_flags = bgd[i].bg_flags &
1016 ~EXT4_BG_INODE_UNINIT;
1017 memcpy(fs->inode_bmaps[i],
1018 zero_buffer, fs->blksz);
1021 _get_new_inode_no(fs->inode_bmaps[i]);
1022 if (fs->curr_inode_no == -1)
1023 /* if block bitmap is completely fill */
1025 fs->curr_inode_no = fs->curr_inode_no +
1026 (i * inodes_per_grp);
1027 fs->first_pass_ibmap++;
1028 bgd[i].free_inodes--;
1029 bgd[i].bg_itable_unused--;
1030 fs->sb->free_inodes--;
1031 status = ext4fs_devread(bgd[i].inode_id *
1037 if (ext4fs_log_journal(journal_buffer,
1042 debug("no inode left on block group %d\n", i);
1047 fs->curr_inode_no++;
1048 /* get the blockbitmap index respective to blockno */
1049 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1050 if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) {
1051 memset(zero_buffer, '\0', fs->blksz);
1052 put_ext4(((uint64_t) (bgd[ibmap_idx].inode_id *
1053 fs->blksz)), zero_buffer,
1055 bgd[ibmap_idx].bg_flags =
1056 bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT;
1057 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1061 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1062 fs->inode_bmaps[ibmap_idx],
1064 debug("going for restart for the block no %d %u\n",
1065 fs->curr_inode_no, ibmap_idx);
1069 /* journal backup */
1070 if (prev_inode_bitmap_index != ibmap_idx) {
1071 memset(journal_buffer, '\0', fs->blksz);
1072 status = ext4fs_devread(bgd[ibmap_idx].inode_id
1074 0, fs->blksz, journal_buffer);
1077 if (ext4fs_log_journal(journal_buffer,
1078 bgd[ibmap_idx].inode_id))
1080 prev_inode_bitmap_index = ibmap_idx;
1082 if (bgd[ibmap_idx].bg_itable_unused !=
1083 bgd[ibmap_idx].free_inodes)
1084 bgd[ibmap_idx].bg_itable_unused =
1085 bgd[ibmap_idx].free_inodes;
1086 bgd[ibmap_idx].free_inodes--;
1087 bgd[ibmap_idx].bg_itable_unused--;
1088 fs->sb->free_inodes--;
1093 free(journal_buffer);
1096 return fs->curr_inode_no;
1098 free(journal_buffer);
1106 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1107 unsigned int *total_remaining_blocks,
1108 unsigned int *no_blks_reqd)
1112 long int actual_block_no;
1113 long int si_blockno;
1114 /* si :single indirect */
1115 unsigned int *si_buffer = NULL;
1116 unsigned int *si_start_addr = NULL;
1117 struct ext_filesystem *fs = get_fs();
1119 if (*total_remaining_blocks != 0) {
1120 si_buffer = zalloc(fs->blksz);
1122 printf("No Memory\n");
1125 si_start_addr = si_buffer;
1126 si_blockno = ext4fs_get_new_blk_no();
1127 if (si_blockno == -1) {
1128 printf("no block left to assign\n");
1132 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1134 status = ext4fs_devread(si_blockno * fs->sect_perblk,
1135 0, fs->blksz, (char *)si_buffer);
1136 memset(si_buffer, '\0', fs->blksz);
1140 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1141 actual_block_no = ext4fs_get_new_blk_no();
1142 if (actual_block_no == -1) {
1143 printf("no block left to assign\n");
1146 *si_buffer = actual_block_no;
1147 debug("SIAB %u: %u\n", *si_buffer,
1148 *total_remaining_blocks);
1151 (*total_remaining_blocks)--;
1152 if (*total_remaining_blocks == 0)
1156 /* write the block to disk */
1157 put_ext4(((uint64_t) (si_blockno * fs->blksz)),
1158 si_start_addr, fs->blksz);
1159 file_inode->b.blocks.indir_block = si_blockno;
1162 free(si_start_addr);
1165 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1166 unsigned int *total_remaining_blocks,
1167 unsigned int *no_blks_reqd)
1172 long int actual_block_no;
1173 /* di:double indirect */
1174 long int di_blockno_parent;
1175 long int di_blockno_child;
1176 unsigned int *di_parent_buffer = NULL;
1177 unsigned int *di_child_buff = NULL;
1178 unsigned int *di_block_start_addr = NULL;
1179 unsigned int *di_child_buff_start = NULL;
1180 struct ext_filesystem *fs = get_fs();
1182 if (*total_remaining_blocks != 0) {
1183 /* double indirect parent block connecting to inode */
1184 di_blockno_parent = ext4fs_get_new_blk_no();
1185 if (di_blockno_parent == -1) {
1186 printf("no block left to assign\n");
1189 di_parent_buffer = zalloc(fs->blksz);
1190 if (!di_parent_buffer)
1193 di_block_start_addr = di_parent_buffer;
1195 debug("DIPB %ld: %u\n", di_blockno_parent,
1196 *total_remaining_blocks);
1198 status = ext4fs_devread(di_blockno_parent *
1200 fs->blksz, (char *)di_parent_buffer);
1203 printf("%s: Device read error!\n", __func__);
1206 memset(di_parent_buffer, '\0', fs->blksz);
1209 * start:for each double indirect parent
1210 * block create one more block
1212 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1213 di_blockno_child = ext4fs_get_new_blk_no();
1214 if (di_blockno_child == -1) {
1215 printf("no block left to assign\n");
1218 di_child_buff = zalloc(fs->blksz);
1222 di_child_buff_start = di_child_buff;
1223 *di_parent_buffer = di_blockno_child;
1226 debug("DICB %ld: %u\n", di_blockno_child,
1227 *total_remaining_blocks);
1229 status = ext4fs_devread(di_blockno_child *
1232 (char *)di_child_buff);
1235 printf("%s: Device read error!\n", __func__);
1238 memset(di_child_buff, '\0', fs->blksz);
1239 /* filling of actual datablocks for each child */
1240 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1241 actual_block_no = ext4fs_get_new_blk_no();
1242 if (actual_block_no == -1) {
1243 printf("no block left to assign\n");
1246 *di_child_buff = actual_block_no;
1247 debug("DIAB %ld: %u\n", actual_block_no,
1248 *total_remaining_blocks);
1251 (*total_remaining_blocks)--;
1252 if (*total_remaining_blocks == 0)
1255 /* write the block table */
1256 put_ext4(((uint64_t) (di_blockno_child * fs->blksz)),
1257 di_child_buff_start, fs->blksz);
1258 free(di_child_buff_start);
1259 di_child_buff_start = NULL;
1261 if (*total_remaining_blocks == 0)
1264 put_ext4(((uint64_t) (di_blockno_parent * fs->blksz)),
1265 di_block_start_addr, fs->blksz);
1266 file_inode->b.blocks.double_indir_block = di_blockno_parent;
1269 free(di_block_start_addr);
1272 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1273 unsigned int *total_remaining_blocks,
1274 unsigned int *no_blks_reqd)
1279 long int actual_block_no;
1280 /* ti: Triple Indirect */
1281 long int ti_gp_blockno;
1282 long int ti_parent_blockno;
1283 long int ti_child_blockno;
1284 unsigned int *ti_gp_buff = NULL;
1285 unsigned int *ti_parent_buff = NULL;
1286 unsigned int *ti_child_buff = NULL;
1287 unsigned int *ti_gp_buff_start_addr = NULL;
1288 unsigned int *ti_pbuff_start_addr = NULL;
1289 unsigned int *ti_cbuff_start_addr = NULL;
1290 struct ext_filesystem *fs = get_fs();
1291 if (*total_remaining_blocks != 0) {
1292 /* triple indirect grand parent block connecting to inode */
1293 ti_gp_blockno = ext4fs_get_new_blk_no();
1294 if (ti_gp_blockno == -1) {
1295 printf("no block left to assign\n");
1298 ti_gp_buff = zalloc(fs->blksz);
1302 ti_gp_buff_start_addr = ti_gp_buff;
1304 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1305 *total_remaining_blocks);
1307 /* for each 4 byte grand parent entry create one more block */
1308 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1309 ti_parent_blockno = ext4fs_get_new_blk_no();
1310 if (ti_parent_blockno == -1) {
1311 printf("no block left to assign\n");
1314 ti_parent_buff = zalloc(fs->blksz);
1315 if (!ti_parent_buff)
1318 ti_pbuff_start_addr = ti_parent_buff;
1319 *ti_gp_buff = ti_parent_blockno;
1322 debug("TIPB %ld: %u\n", ti_parent_blockno,
1323 *total_remaining_blocks);
1325 /* for each 4 byte entry parent create one more block */
1326 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1327 ti_child_blockno = ext4fs_get_new_blk_no();
1328 if (ti_child_blockno == -1) {
1329 printf("no block left assign\n");
1332 ti_child_buff = zalloc(fs->blksz);
1336 ti_cbuff_start_addr = ti_child_buff;
1337 *ti_parent_buff = ti_child_blockno;
1340 debug("TICB %ld: %u\n", ti_parent_blockno,
1341 *total_remaining_blocks);
1343 /* fill actual datablocks for each child */
1344 for (k = 0; k < (fs->blksz / sizeof(int));
1347 ext4fs_get_new_blk_no();
1348 if (actual_block_no == -1) {
1349 printf("no block left\n");
1352 *ti_child_buff = actual_block_no;
1353 debug("TIAB %ld: %u\n", actual_block_no,
1354 *total_remaining_blocks);
1357 (*total_remaining_blocks)--;
1358 if (*total_remaining_blocks == 0)
1361 /* write the child block */
1362 put_ext4(((uint64_t) (ti_child_blockno *
1364 ti_cbuff_start_addr, fs->blksz);
1365 free(ti_cbuff_start_addr);
1367 if (*total_remaining_blocks == 0)
1370 /* write the parent block */
1371 put_ext4(((uint64_t) (ti_parent_blockno * fs->blksz)),
1372 ti_pbuff_start_addr, fs->blksz);
1373 free(ti_pbuff_start_addr);
1375 if (*total_remaining_blocks == 0)
1378 /* write the grand parent block */
1379 put_ext4(((uint64_t) (ti_gp_blockno * fs->blksz)),
1380 ti_gp_buff_start_addr, fs->blksz);
1381 file_inode->b.blocks.triple_indir_block = ti_gp_blockno;
1384 free(ti_gp_buff_start_addr);
1387 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1388 unsigned int total_remaining_blocks,
1389 unsigned int *total_no_of_block)
1392 long int direct_blockno;
1393 unsigned int no_blks_reqd = 0;
1395 /* allocation of direct blocks */
1396 for (i = 0; i < INDIRECT_BLOCKS; i++) {
1397 direct_blockno = ext4fs_get_new_blk_no();
1398 if (direct_blockno == -1) {
1399 printf("no block left to assign\n");
1402 file_inode->b.blocks.dir_blocks[i] = direct_blockno;
1403 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1405 total_remaining_blocks--;
1406 if (total_remaining_blocks == 0)
1410 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1412 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1414 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1416 *total_no_of_block += no_blks_reqd;
1421 static struct ext4_extent_header *ext4fs_get_extent_block
1422 (struct ext2_data *data, char *buf,
1423 struct ext4_extent_header *ext_block,
1424 uint32_t fileblock, int log2_blksz)
1426 struct ext4_extent_idx *index;
1427 unsigned long long block;
1428 struct ext_filesystem *fs = get_fs();
1432 index = (struct ext4_extent_idx *)(ext_block + 1);
1434 if (le32_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1437 if (ext_block->eh_depth == 0)
1442 if (i >= le32_to_cpu(ext_block->eh_entries))
1444 } while (fileblock > le32_to_cpu(index[i].ei_block));
1449 block = le32_to_cpu(index[i].ei_leaf_hi);
1450 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1452 if (ext4fs_devread(block << log2_blksz, 0, fs->blksz, buf))
1453 ext_block = (struct ext4_extent_header *)buf;
1459 static int ext4fs_blockgroup
1460 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1463 unsigned int blkoff, desc_per_blk;
1465 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1467 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
1468 group / desc_per_blk;
1469 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1471 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1472 group, blkno, blkoff);
1474 return ext4fs_devread(blkno << LOG2_EXT2_BLOCK_SIZE(data),
1475 blkoff, sizeof(struct ext2_block_group),
1479 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1481 struct ext2_block_group blkgrp;
1482 struct ext2_sblock *sblock = &data->sblock;
1483 struct ext_filesystem *fs = get_fs();
1484 int inodes_per_block, status;
1486 unsigned int blkoff;
1488 /* It is easier to calculate if the first inode is 0. */
1490 status = ext4fs_blockgroup(data, ino / __le32_to_cpu
1491 (sblock->inodes_per_group), &blkgrp);
1495 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1496 blkno = __le32_to_cpu(blkgrp.inode_table_id) +
1497 (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1498 blkoff = (ino % inodes_per_block) * fs->inodesz;
1499 /* Read the inode. */
1500 status = ext4fs_devread(blkno << LOG2_EXT2_BLOCK_SIZE(data), blkoff,
1501 sizeof(struct ext2_inode), (char *)inode);
1508 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1515 long int perblock_parent;
1516 long int perblock_child;
1517 unsigned long long start;
1518 /* get the blocksize of the filesystem */
1519 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1520 log2_blksz = LOG2_EXT2_BLOCK_SIZE(ext4fs_root);
1521 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1522 char *buf = zalloc(blksz);
1525 struct ext4_extent_header *ext_block;
1526 struct ext4_extent *extent;
1528 ext_block = ext4fs_get_extent_block(ext4fs_root, buf,
1529 (struct ext4_extent_header
1532 fileblock, log2_blksz);
1534 printf("invalid extent block\n");
1539 extent = (struct ext4_extent *)(ext_block + 1);
1543 if (i >= le32_to_cpu(ext_block->eh_entries))
1545 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1547 fileblock -= le32_to_cpu(extent[i].ee_block);
1548 if (fileblock >= le32_to_cpu(extent[i].ee_len)) {
1553 start = le32_to_cpu(extent[i].ee_start_hi);
1554 start = (start << 32) +
1555 le32_to_cpu(extent[i].ee_start_lo);
1557 return fileblock + start;
1560 printf("Extent Error\n");
1565 /* Direct blocks. */
1566 if (fileblock < INDIRECT_BLOCKS)
1567 blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1570 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1571 if (ext4fs_indir1_block == NULL) {
1572 ext4fs_indir1_block = zalloc(blksz);
1573 if (ext4fs_indir1_block == NULL) {
1574 printf("** SI ext2fs read block (indir 1)"
1575 "malloc failed. **\n");
1578 ext4fs_indir1_size = blksz;
1579 ext4fs_indir1_blkno = -1;
1581 if (blksz != ext4fs_indir1_size) {
1582 free(ext4fs_indir1_block);
1583 ext4fs_indir1_block = NULL;
1584 ext4fs_indir1_size = 0;
1585 ext4fs_indir1_blkno = -1;
1586 ext4fs_indir1_block = zalloc(blksz);
1587 if (ext4fs_indir1_block == NULL) {
1588 printf("** SI ext2fs read block (indir 1):"
1589 "malloc failed. **\n");
1592 ext4fs_indir1_size = blksz;
1594 if ((__le32_to_cpu(inode->b.blocks.indir_block) <<
1595 log2_blksz) != ext4fs_indir1_blkno) {
1597 ext4fs_devread(__le32_to_cpu
1599 indir_block) << log2_blksz, 0,
1600 blksz, (char *)ext4fs_indir1_block);
1602 printf("** SI ext2fs read block (indir 1)"
1606 ext4fs_indir1_blkno =
1607 __le32_to_cpu(inode->b.blocks.
1608 indir_block) << log2_blksz;
1610 blknr = __le32_to_cpu(ext4fs_indir1_block
1611 [fileblock - INDIRECT_BLOCKS]);
1613 /* Double indirect. */
1614 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1615 (blksz / 4 + 1)))) {
1617 long int perblock = blksz / 4;
1618 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1620 if (ext4fs_indir1_block == NULL) {
1621 ext4fs_indir1_block = zalloc(blksz);
1622 if (ext4fs_indir1_block == NULL) {
1623 printf("** DI ext2fs read block (indir 2 1)"
1624 "malloc failed. **\n");
1627 ext4fs_indir1_size = blksz;
1628 ext4fs_indir1_blkno = -1;
1630 if (blksz != ext4fs_indir1_size) {
1631 free(ext4fs_indir1_block);
1632 ext4fs_indir1_block = NULL;
1633 ext4fs_indir1_size = 0;
1634 ext4fs_indir1_blkno = -1;
1635 ext4fs_indir1_block = zalloc(blksz);
1636 if (ext4fs_indir1_block == NULL) {
1637 printf("** DI ext2fs read block (indir 2 1)"
1638 "malloc failed. **\n");
1641 ext4fs_indir1_size = blksz;
1643 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) <<
1644 log2_blksz) != ext4fs_indir1_blkno) {
1646 ext4fs_devread(__le32_to_cpu
1648 double_indir_block) << log2_blksz,
1650 (char *)ext4fs_indir1_block);
1652 printf("** DI ext2fs read block (indir 2 1)"
1656 ext4fs_indir1_blkno =
1657 __le32_to_cpu(inode->b.blocks.double_indir_block) <<
1661 if (ext4fs_indir2_block == NULL) {
1662 ext4fs_indir2_block = zalloc(blksz);
1663 if (ext4fs_indir2_block == NULL) {
1664 printf("** DI ext2fs read block (indir 2 2)"
1665 "malloc failed. **\n");
1668 ext4fs_indir2_size = blksz;
1669 ext4fs_indir2_blkno = -1;
1671 if (blksz != ext4fs_indir2_size) {
1672 free(ext4fs_indir2_block);
1673 ext4fs_indir2_block = NULL;
1674 ext4fs_indir2_size = 0;
1675 ext4fs_indir2_blkno = -1;
1676 ext4fs_indir2_block = zalloc(blksz);
1677 if (ext4fs_indir2_block == NULL) {
1678 printf("** DI ext2fs read block (indir 2 2)"
1679 "malloc failed. **\n");
1682 ext4fs_indir2_size = blksz;
1684 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1685 log2_blksz) != ext4fs_indir2_blkno) {
1686 status = ext4fs_devread(__le32_to_cpu
1687 (ext4fs_indir1_block
1689 perblock]) << log2_blksz, 0,
1691 (char *)ext4fs_indir2_block);
1693 printf("** DI ext2fs read block (indir 2 2)"
1697 ext4fs_indir2_blkno =
1698 __le32_to_cpu(ext4fs_indir1_block[rblock
1703 blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1705 /* Tripple indirect. */
1707 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1708 (blksz / 4 * blksz / 4));
1709 perblock_child = blksz / 4;
1710 perblock_parent = ((blksz / 4) * (blksz / 4));
1712 if (ext4fs_indir1_block == NULL) {
1713 ext4fs_indir1_block = zalloc(blksz);
1714 if (ext4fs_indir1_block == NULL) {
1715 printf("** TI ext2fs read block (indir 2 1)"
1716 "malloc failed. **\n");
1719 ext4fs_indir1_size = blksz;
1720 ext4fs_indir1_blkno = -1;
1722 if (blksz != ext4fs_indir1_size) {
1723 free(ext4fs_indir1_block);
1724 ext4fs_indir1_block = NULL;
1725 ext4fs_indir1_size = 0;
1726 ext4fs_indir1_blkno = -1;
1727 ext4fs_indir1_block = zalloc(blksz);
1728 if (ext4fs_indir1_block == NULL) {
1729 printf("** TI ext2fs read block (indir 2 1)"
1730 "malloc failed. **\n");
1733 ext4fs_indir1_size = blksz;
1735 if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1736 log2_blksz) != ext4fs_indir1_blkno) {
1737 status = ext4fs_devread
1738 (__le32_to_cpu(inode->b.blocks.triple_indir_block)
1739 << log2_blksz, 0, blksz,
1740 (char *)ext4fs_indir1_block);
1742 printf("** TI ext2fs read block (indir 2 1)"
1746 ext4fs_indir1_blkno =
1747 __le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1751 if (ext4fs_indir2_block == NULL) {
1752 ext4fs_indir2_block = zalloc(blksz);
1753 if (ext4fs_indir2_block == NULL) {
1754 printf("** TI ext2fs read block (indir 2 2)"
1755 "malloc failed. **\n");
1758 ext4fs_indir2_size = blksz;
1759 ext4fs_indir2_blkno = -1;
1761 if (blksz != ext4fs_indir2_size) {
1762 free(ext4fs_indir2_block);
1763 ext4fs_indir2_block = NULL;
1764 ext4fs_indir2_size = 0;
1765 ext4fs_indir2_blkno = -1;
1766 ext4fs_indir2_block = zalloc(blksz);
1767 if (ext4fs_indir2_block == NULL) {
1768 printf("** TI ext2fs read block (indir 2 2)"
1769 "malloc failed. **\n");
1772 ext4fs_indir2_size = blksz;
1774 if ((__le32_to_cpu(ext4fs_indir1_block[rblock /
1775 perblock_parent]) <<
1777 != ext4fs_indir2_blkno) {
1778 status = ext4fs_devread(__le32_to_cpu
1779 (ext4fs_indir1_block
1781 perblock_parent]) <<
1782 log2_blksz, 0, blksz,
1783 (char *)ext4fs_indir2_block);
1785 printf("** TI ext2fs read block (indir 2 2)"
1789 ext4fs_indir2_blkno =
1790 __le32_to_cpu(ext4fs_indir1_block[rblock /
1795 if (ext4fs_indir3_block == NULL) {
1796 ext4fs_indir3_block = zalloc(blksz);
1797 if (ext4fs_indir3_block == NULL) {
1798 printf("** TI ext2fs read block (indir 2 2)"
1799 "malloc failed. **\n");
1802 ext4fs_indir3_size = blksz;
1803 ext4fs_indir3_blkno = -1;
1805 if (blksz != ext4fs_indir3_size) {
1806 free(ext4fs_indir3_block);
1807 ext4fs_indir3_block = NULL;
1808 ext4fs_indir3_size = 0;
1809 ext4fs_indir3_blkno = -1;
1810 ext4fs_indir3_block = zalloc(blksz);
1811 if (ext4fs_indir3_block == NULL) {
1812 printf("** TI ext2fs read block (indir 2 2)"
1813 "malloc failed. **\n");
1816 ext4fs_indir3_size = blksz;
1818 if ((__le32_to_cpu(ext4fs_indir2_block[rblock
1821 log2_blksz) != ext4fs_indir3_blkno) {
1823 ext4fs_devread(__le32_to_cpu
1824 (ext4fs_indir2_block
1825 [(rblock / perblock_child)
1826 % (blksz / 4)]) << log2_blksz, 0,
1827 blksz, (char *)ext4fs_indir3_block);
1829 printf("** TI ext2fs read block (indir 2 2)"
1833 ext4fs_indir3_blkno =
1834 __le32_to_cpu(ext4fs_indir2_block[(rblock /
1841 blknr = __le32_to_cpu(ext4fs_indir3_block
1842 [rblock % perblock_child]);
1844 debug("ext4fs_read_block %ld\n", blknr);
1849 void ext4fs_close(void)
1851 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1852 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1855 if (ext4fs_root != NULL) {
1859 if (ext4fs_indir1_block != NULL) {
1860 free(ext4fs_indir1_block);
1861 ext4fs_indir1_block = NULL;
1862 ext4fs_indir1_size = 0;
1863 ext4fs_indir1_blkno = -1;
1865 if (ext4fs_indir2_block != NULL) {
1866 free(ext4fs_indir2_block);
1867 ext4fs_indir2_block = NULL;
1868 ext4fs_indir2_size = 0;
1869 ext4fs_indir2_blkno = -1;
1871 if (ext4fs_indir3_block != NULL) {
1872 free(ext4fs_indir3_block);
1873 ext4fs_indir3_block = NULL;
1874 ext4fs_indir3_size = 0;
1875 ext4fs_indir3_blkno = -1;
1879 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1880 struct ext2fs_node **fnode, int *ftype)
1882 unsigned int fpos = 0;
1884 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1888 printf("Iterate dir %s\n", name);
1889 #endif /* of DEBUG */
1890 if (!diro->inode_read) {
1891 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1895 /* Search the file. */
1896 while (fpos < __le32_to_cpu(diro->inode.size)) {
1897 struct ext2_dirent dirent;
1899 status = ext4fs_read_file(diro, fpos,
1900 sizeof(struct ext2_dirent),
1905 if (dirent.namelen != 0) {
1906 char filename[dirent.namelen + 1];
1907 struct ext2fs_node *fdiro;
1908 int type = FILETYPE_UNKNOWN;
1910 status = ext4fs_read_file(diro,
1912 sizeof(struct ext2_dirent),
1913 dirent.namelen, filename);
1917 fdiro = zalloc(sizeof(struct ext2fs_node));
1921 fdiro->data = diro->data;
1922 fdiro->ino = __le32_to_cpu(dirent.inode);
1924 filename[dirent.namelen] = '\0';
1926 if (dirent.filetype != FILETYPE_UNKNOWN) {
1927 fdiro->inode_read = 0;
1929 if (dirent.filetype == FILETYPE_DIRECTORY)
1930 type = FILETYPE_DIRECTORY;
1931 else if (dirent.filetype == FILETYPE_SYMLINK)
1932 type = FILETYPE_SYMLINK;
1933 else if (dirent.filetype == FILETYPE_REG)
1934 type = FILETYPE_REG;
1936 status = ext4fs_read_inode(diro->data,
1944 fdiro->inode_read = 1;
1946 if ((__le16_to_cpu(fdiro->inode.mode) &
1947 FILETYPE_INO_MASK) ==
1948 FILETYPE_INO_DIRECTORY) {
1949 type = FILETYPE_DIRECTORY;
1950 } else if ((__le16_to_cpu(fdiro->inode.mode)
1951 & FILETYPE_INO_MASK) ==
1952 FILETYPE_INO_SYMLINK) {
1953 type = FILETYPE_SYMLINK;
1954 } else if ((__le16_to_cpu(fdiro->inode.mode)
1955 & FILETYPE_INO_MASK) ==
1957 type = FILETYPE_REG;
1961 printf("iterate >%s<\n", filename);
1962 #endif /* of DEBUG */
1963 if ((name != NULL) && (fnode != NULL)
1964 && (ftype != NULL)) {
1965 if (strcmp(filename, name) == 0) {
1971 if (fdiro->inode_read == 0) {
1972 status = ext4fs_read_inode(diro->data,
1980 fdiro->inode_read = 1;
1983 case FILETYPE_DIRECTORY:
1986 case FILETYPE_SYMLINK:
1997 __le32_to_cpu(fdiro->inode.size),
2002 fpos += __le16_to_cpu(dirent.direntlen);
2007 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2010 struct ext2fs_node *diro = node;
2013 if (!diro->inode_read) {
2014 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2018 symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1);
2022 if (__le32_to_cpu(diro->inode.size) <= 60) {
2023 strncpy(symlink, diro->inode.b.symlink,
2024 __le32_to_cpu(diro->inode.size));
2026 status = ext4fs_read_file(diro, 0,
2027 __le32_to_cpu(diro->inode.size),
2034 symlink[__le32_to_cpu(diro->inode.size)] = '\0';
2038 static int ext4fs_find_file1(const char *currpath,
2039 struct ext2fs_node *currroot,
2040 struct ext2fs_node **currfound, int *foundtype)
2042 char fpath[strlen(currpath) + 1];
2046 int type = FILETYPE_DIRECTORY;
2047 struct ext2fs_node *currnode = currroot;
2048 struct ext2fs_node *oldnode = currroot;
2050 strncpy(fpath, currpath, strlen(currpath) + 1);
2052 /* Remove all leading slashes. */
2053 while (*name == '/')
2057 *currfound = currnode;
2064 /* Extract the actual part from the pathname. */
2065 next = strchr(name, '/');
2067 /* Remove all leading slashes. */
2068 while (*next == '/')
2072 if (type != FILETYPE_DIRECTORY) {
2073 ext4fs_free_node(currnode, currroot);
2079 /* Iterate over the directory. */
2080 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2087 /* Read in the symlink and follow it. */
2088 if (type == FILETYPE_SYMLINK) {
2091 /* Test if the symlink does not loop. */
2092 if (++symlinknest == 8) {
2093 ext4fs_free_node(currnode, currroot);
2094 ext4fs_free_node(oldnode, currroot);
2098 symlink = ext4fs_read_symlink(currnode);
2099 ext4fs_free_node(currnode, currroot);
2102 ext4fs_free_node(oldnode, currroot);
2106 debug("Got symlink >%s<\n", symlink);
2108 if (symlink[0] == '/') {
2109 ext4fs_free_node(oldnode, currroot);
2110 oldnode = &ext4fs_root->diropen;
2113 /* Lookup the node the symlink points to. */
2114 status = ext4fs_find_file1(symlink, oldnode,
2120 ext4fs_free_node(oldnode, currroot);
2125 ext4fs_free_node(oldnode, currroot);
2127 /* Found the node! */
2128 if (!next || *next == '\0') {
2129 *currfound = currnode;
2138 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2139 struct ext2fs_node **foundnode, int expecttype)
2142 int foundtype = FILETYPE_DIRECTORY;
2148 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2152 /* Check if the node that was found was of the expected type. */
2153 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2155 else if ((expecttype == FILETYPE_DIRECTORY)
2156 && (foundtype != expecttype))
2162 int ext4fs_open(const char *filename)
2164 struct ext2fs_node *fdiro = NULL;
2168 if (ext4fs_root == NULL)
2172 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2177 if (!fdiro->inode_read) {
2178 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2183 len = __le32_to_cpu(fdiro->inode.size);
2184 ext4fs_file = fdiro;
2188 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2193 int ext4fs_mount(unsigned part_length)
2195 struct ext2_data *data;
2197 struct ext_filesystem *fs = get_fs();
2198 data = zalloc(sizeof(struct ext2_data));
2202 /* Read the superblock. */
2203 status = ext4fs_devread(1 * 2, 0, sizeof(struct ext2_sblock),
2204 (char *)&data->sblock);
2209 /* Make sure this is an ext2 filesystem. */
2210 if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2213 if (__le32_to_cpu(data->sblock.revision_level == 0))
2216 fs->inodesz = __le16_to_cpu(data->sblock.inode_size);
2218 debug("EXT2 rev %d, inode_size %d\n",
2219 __le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2221 data->diropen.data = data;
2222 data->diropen.ino = 2;
2223 data->diropen.inode_read = 1;
2224 data->inode = &data->diropen.inode;
2226 status = ext4fs_read_inode(data, 2, data->inode);
2234 printf("Failed to mount ext2 filesystem...\n");