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 #if defined(CONFIG_CMD_EXT4_WRITE)
60 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
62 uint32_t res = size / n;
69 void put_ext4(uint64_t off, void *buf, uint32_t size)
73 unsigned char *temp_ptr = NULL;
74 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, SECTOR_SIZE);
75 struct ext_filesystem *fs = get_fs();
77 startblock = off / (uint64_t)SECTOR_SIZE;
78 startblock += part_offset;
79 remainder = off % (uint64_t)SECTOR_SIZE;
80 remainder &= SECTOR_SIZE - 1;
82 if (fs->dev_desc == NULL)
85 if ((startblock + (size / SECTOR_SIZE)) >
86 (part_offset + fs->total_sect)) {
87 printf("part_offset is %lu\n", part_offset);
88 printf("total_sector is %llu\n", fs->total_sect);
89 printf("error: overflow occurs\n");
94 if (fs->dev_desc->block_read) {
95 fs->dev_desc->block_read(fs->dev_desc->dev,
96 startblock, 1, sec_buf);
98 memcpy((temp_ptr + remainder),
99 (unsigned char *)buf, size);
100 fs->dev_desc->block_write(fs->dev_desc->dev,
101 startblock, 1, sec_buf);
104 if (size / SECTOR_SIZE != 0) {
105 fs->dev_desc->block_write(fs->dev_desc->dev,
108 (unsigned long *)buf);
110 fs->dev_desc->block_read(fs->dev_desc->dev,
111 startblock, 1, sec_buf);
113 memcpy(temp_ptr, buf, size);
114 fs->dev_desc->block_write(fs->dev_desc->dev,
116 (unsigned long *)sec_buf);
121 static int _get_new_inode_no(unsigned char *buffer)
123 struct ext_filesystem *fs = get_fs();
129 /* get the blocksize of the filesystem */
130 unsigned char *ptr = buffer;
131 while (*ptr == 255) {
134 if (count > ext4fs_root->sblock.inodes_per_group)
138 for (j = 0; j < fs->blksz; j++) {
143 status = input & operand;
158 static int _get_new_blk_no(unsigned char *buffer)
164 unsigned char *ptr = buffer;
165 struct ext_filesystem *fs = get_fs();
167 if (fs->blksz != 1024)
172 while (*ptr == 255) {
175 if (count == (fs->blksz * 8))
179 for (j = 0; j < fs->blksz; j++) {
184 status = input & operand;
199 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
201 int i, remainder, status;
202 unsigned char *ptr = buffer;
203 unsigned char operand;
205 remainder = blockno % 8;
206 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
208 i = i - (index * blocksize);
209 if (blocksize != 1024) {
211 operand = 1 << remainder;
212 status = *ptr & operand;
216 *ptr = *ptr | operand;
219 if (remainder == 0) {
224 operand = (1 << (remainder - 1));
226 status = *ptr & operand;
230 *ptr = *ptr | operand;
235 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
237 int i, remainder, status;
238 unsigned char *ptr = buffer;
239 unsigned char operand;
241 remainder = blockno % 8;
242 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
244 i = i - (index * blocksize);
245 if (blocksize != 1024) {
247 operand = (1 << remainder);
248 status = *ptr & operand;
250 *ptr = *ptr & ~(operand);
252 if (remainder == 0) {
257 operand = (1 << (remainder - 1));
259 status = *ptr & operand;
261 *ptr = *ptr & ~(operand);
265 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
267 int i, remainder, status;
268 unsigned char *ptr = buffer;
269 unsigned char operand;
271 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
273 remainder = inode_no % 8;
274 if (remainder == 0) {
279 operand = (1 << (remainder - 1));
281 status = *ptr & operand;
285 *ptr = *ptr | operand;
290 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
292 int i, remainder, status;
293 unsigned char *ptr = buffer;
294 unsigned char operand;
296 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
298 remainder = inode_no % 8;
299 if (remainder == 0) {
304 operand = (1 << (remainder - 1));
306 status = *ptr & operand;
308 *ptr = *ptr & ~(operand);
311 int ext4fs_checksum_update(unsigned int i)
313 struct ext2_block_group *desc;
314 struct ext_filesystem *fs = get_fs();
317 desc = (struct ext2_block_group *)&fs->gd[i];
318 if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
319 int offset = offsetof(struct ext2_block_group, bg_checksum);
321 crc = ext2fs_crc16(~0, fs->sb->unique_id,
322 sizeof(fs->sb->unique_id));
323 crc = ext2fs_crc16(crc, &i, sizeof(i));
324 crc = ext2fs_crc16(crc, desc, offset);
325 offset += sizeof(desc->bg_checksum); /* skip checksum */
326 assert(offset == sizeof(*desc));
332 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
335 int sizeof_void_space;
336 int new_entry_byte_reqd;
337 short padding_factor = 0;
339 if (dir->namelen % 4 != 0)
340 padding_factor = 4 - (dir->namelen % 4);
342 dentry_length = sizeof(struct ext2_dirent) +
343 dir->namelen + padding_factor;
344 sizeof_void_space = dir->direntlen - dentry_length;
345 if (sizeof_void_space == 0)
349 if (strlen(filename) % 4 != 0)
350 padding_factor = 4 - (strlen(filename) % 4);
352 new_entry_byte_reqd = strlen(filename) +
353 sizeof(struct ext2_dirent) + padding_factor;
354 if (sizeof_void_space >= new_entry_byte_reqd) {
355 dir->direntlen = dentry_length;
356 return sizeof_void_space;
362 void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
364 unsigned int *zero_buffer = NULL;
365 char *root_first_block_buffer = NULL;
368 long int first_block_no_of_root = 0;
369 long int previous_blknr = -1;
371 short int padding_factor = 0;
372 unsigned int new_entry_byte_reqd;
373 unsigned int last_entry_dirlen;
374 int sizeof_void_space = 0;
378 struct ext_filesystem *fs = get_fs();
379 /* directory entry */
380 struct ext2_dirent *dir;
382 char *temp_dir = NULL;
384 zero_buffer = zalloc(fs->blksz);
386 printf("No Memory\n");
389 root_first_block_buffer = zalloc(fs->blksz);
390 if (!root_first_block_buffer) {
392 printf("No Memory\n");
397 /* read the block no allocated to a file */
398 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
400 root_blknr = read_allocated_block(g_parent_inode,
402 if (root_blknr == 0) {
403 first_block_no_of_root = previous_blknr;
406 previous_blknr = root_blknr;
409 status = ext4fs_devread(first_block_no_of_root
411 0, fs->blksz, root_first_block_buffer);
415 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
417 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);
489 /* make a pointer ready for creating next directory entry */
490 templength = dir->direntlen;
491 totalbytes = totalbytes + templength;
492 dir = (struct ext2_dirent *)((char *)dir + templength);
495 /* get the next available inode number */
496 inodeno = ext4fs_get_new_inode_no();
498 printf("no inode left to assign\n");
501 dir->inode = inodeno;
502 if (sizeof_void_space)
503 dir->direntlen = sizeof_void_space;
505 dir->direntlen = fs->blksz - totalbytes;
507 dir->namelen = strlen(filename);
508 dir->filetype = FILETYPE_REG; /* regular file */
509 temp_dir = (char *)dir;
510 temp_dir = temp_dir + sizeof(struct ext2_dirent);
511 memcpy(temp_dir, filename, strlen(filename));
515 /* update or write the 1st block of root inode */
516 if (ext4fs_put_metadata(root_first_block_buffer,
517 first_block_no_of_root))
522 free(root_first_block_buffer);
525 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
535 unsigned char *block_buffer = NULL;
536 struct ext2_dirent *dir = NULL;
537 struct ext2_dirent *previous_dir = NULL;
538 struct ext_filesystem *fs = get_fs();
540 /* read the block no allocated to a file */
541 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
543 blknr = read_allocated_block(parent_inode, direct_blk_idx);
547 /* read the blocks of parenet inode */
548 block_buffer = zalloc(fs->blksz);
552 status = ext4fs_devread(blknr * fs->sect_perblk,
553 0, fs->blksz, (char *)block_buffer);
557 dir = (struct ext2_dirent *)block_buffer;
560 while (dir->direntlen >= 0) {
562 * blocksize-totalbytes because last directory
563 * length i.e.,*dir->direntlen is free availble
564 * space in the block that means
565 * it is a last entry of directory entry
567 if (strlen(dirname) == dir->namelen) {
568 if (strncmp(dirname, ptr +
569 sizeof(struct ext2_dirent),
570 dir->namelen) == 0) {
571 previous_dir->direntlen +=
573 inodeno = dir->inode;
580 if (fs->blksz - totalbytes == dir->direntlen)
583 /* traversing the each directory entry */
584 templength = dir->direntlen;
585 totalbytes = totalbytes + templength;
587 dir = (struct ext2_dirent *)((char *)dir + templength);
607 static int find_dir_depth(char *dirname)
609 char *token = strtok(dirname, "/");
611 while (token != NULL) {
612 token = strtok(NULL, "/");
615 return count + 1 + 1;
617 * for example for string /home/temp
618 * depth=home(1)+temp(1)+1 extra for NULL;
623 static int parse_path(char **arr, char *dirname)
625 char *token = strtok(dirname, "/");
629 arr[i] = zalloc(strlen("/") + 1);
635 /* add each path entry after root */
636 while (token != NULL) {
637 arr[i] = zalloc(strlen(token) + 1);
640 memcpy(arr[i++], token, strlen(token));
641 token = strtok(NULL, "/");
648 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
650 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
657 * Function: ext4fs_get_parent_inode_num
658 * Return Value: inode Number of the parent directory of file/Directory to be
660 * dirname : Input parmater, input path name of the file/directory to be created
661 * dname : Output parameter, to be filled with the name of the directory
662 * extracted from dirname
664 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
668 int matched_inode_no;
669 int result_inode_no = -1;
671 char *depth_dirname = NULL;
672 char *parse_dirname = NULL;
673 struct ext2_inode *parent_inode = NULL;
674 struct ext2_inode *first_inode = NULL;
675 struct ext2_inode temp_inode;
677 if (*dirname != '/') {
678 printf("Please supply Absolute path\n");
682 /* TODO: input validation make equivalent to linux */
683 depth_dirname = zalloc(strlen(dirname) + 1);
687 memcpy(depth_dirname, dirname, strlen(dirname));
688 depth = find_dir_depth(depth_dirname);
689 parse_dirname = zalloc(strlen(dirname) + 1);
692 memcpy(parse_dirname, dirname, strlen(dirname));
694 /* allocate memory for each directory level */
695 ptr = zalloc((depth) * sizeof(char *));
698 if (parse_path(ptr, parse_dirname))
700 parent_inode = zalloc(sizeof(struct ext2_inode));
703 first_inode = zalloc(sizeof(struct ext2_inode));
706 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
707 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
709 result_inode_no = EXT2_ROOT_INO;
710 for (i = 1; i < depth; i++) {
711 matched_inode_no = search_dir(parent_inode, ptr[i]);
712 if (matched_inode_no == -1) {
713 if (ptr[i + 1] == NULL && i == 1) {
714 result_inode_no = EXT2_ROOT_INO;
717 if (ptr[i + 1] == NULL)
719 printf("Invalid path\n");
720 result_inode_no = -1;
724 if (ptr[i + 1] != NULL) {
725 memset(parent_inode, '\0',
726 sizeof(struct ext2_inode));
727 if (ext4fs_iget(matched_inode_no,
729 result_inode_no = -1;
732 result_inode_no = matched_inode_no;
741 matched_inode_no = search_dir(first_inode, ptr[i]);
743 matched_inode_no = search_dir(parent_inode, ptr[i]);
745 if (matched_inode_no != -1) {
746 ext4fs_iget(matched_inode_no, &temp_inode);
747 if (temp_inode.mode & S_IFDIR) {
748 printf("It is a Directory\n");
749 result_inode_no = -1;
754 if (strlen(ptr[i]) > 256) {
755 result_inode_no = -1;
758 memcpy(dname, ptr[i], strlen(ptr[i]));
767 return result_inode_no;
770 static int check_filename(char *filename, unsigned int blknr)
772 unsigned int first_block_no_of_root;
777 char *root_first_block_buffer = NULL;
778 char *root_first_block_addr = NULL;
779 struct ext2_dirent *dir = NULL;
780 struct ext2_dirent *previous_dir = NULL;
782 struct ext_filesystem *fs = get_fs();
784 /* get the first block of root */
785 first_block_no_of_root = blknr;
786 root_first_block_buffer = zalloc(fs->blksz);
787 if (!root_first_block_buffer)
789 root_first_block_addr = root_first_block_buffer;
790 status = ext4fs_devread(first_block_no_of_root *
792 fs->blksz, root_first_block_buffer);
796 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
798 dir = (struct ext2_dirent *)root_first_block_buffer;
801 while (dir->direntlen >= 0) {
803 * blocksize-totalbytes because last
804 * directory length i.e., *dir->direntlen
805 * is free availble space in the block that
806 * means it is a last entry of directory entry
808 if (strlen(filename) == dir->namelen) {
809 if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
810 dir->namelen) == 0) {
811 printf("file found deleting\n");
812 previous_dir->direntlen += dir->direntlen;
813 inodeno = dir->inode;
820 if (fs->blksz - totalbytes == dir->direntlen)
823 /* traversing the each directory entry */
824 templength = dir->direntlen;
825 totalbytes = totalbytes + templength;
827 dir = (struct ext2_dirent *)((char *)dir + templength);
833 if (ext4fs_put_metadata(root_first_block_addr,
834 first_block_no_of_root))
839 free(root_first_block_buffer);
844 int ext4fs_filename_check(char *filename)
846 short direct_blk_idx = 0;
850 /* read the block no allocated to a file */
851 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
853 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
856 inodeno = check_filename(filename, blknr);
864 long int ext4fs_get_new_blk_no(void)
870 static int prev_bg_bitmap_index = -1;
871 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
872 struct ext_filesystem *fs = get_fs();
873 char *journal_buffer = zalloc(fs->blksz);
874 char *zero_buffer = zalloc(fs->blksz);
875 if (!journal_buffer || !zero_buffer)
877 struct ext2_block_group *gd = (struct ext2_block_group *)fs->gdtable;
879 if (fs->first_pass_bbmap == 0) {
880 for (i = 0; i < fs->no_blkgrp; i++) {
881 if (gd[i].free_blocks) {
882 if (gd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
883 put_ext4(((uint64_t) (gd[i].block_id *
885 zero_buffer, fs->blksz);
888 bg_flags & ~EXT4_BG_BLOCK_UNINIT;
889 memcpy(fs->blk_bmaps[i], zero_buffer,
893 _get_new_blk_no(fs->blk_bmaps[i]);
894 if (fs->curr_blkno == -1)
895 /* if block bitmap is completely fill */
897 fs->curr_blkno = fs->curr_blkno +
899 fs->first_pass_bbmap++;
901 fs->sb->free_blocks--;
902 status = ext4fs_devread(gd[i].block_id *
908 if (ext4fs_log_journal(journal_buffer,
913 debug("no space left on block group %d\n", i);
921 /* get the blockbitmap index respective to blockno */
922 if (fs->blksz != 1024) {
923 bg_idx = fs->curr_blkno / blk_per_grp;
925 bg_idx = fs->curr_blkno / blk_per_grp;
926 remainder = fs->curr_blkno % blk_per_grp;
932 * To skip completely filled block group bitmaps
933 * Optimize the block allocation
935 if (bg_idx >= fs->no_blkgrp)
938 if (gd[bg_idx].free_blocks == 0) {
939 debug("block group %u is full. Skipping\n", bg_idx);
940 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
945 if (gd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) {
946 memset(zero_buffer, '\0', fs->blksz);
947 put_ext4(((uint64_t) (gd[bg_idx].block_id * fs->blksz)),
948 zero_buffer, fs->blksz);
949 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
950 gd[bg_idx].bg_flags = gd[bg_idx].bg_flags &
951 ~EXT4_BG_BLOCK_UNINIT;
954 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
956 debug("going for restart for the block no %ld %u\n",
957 fs->curr_blkno, bg_idx);
962 if (prev_bg_bitmap_index != bg_idx) {
963 memset(journal_buffer, '\0', fs->blksz);
964 status = ext4fs_devread(gd[bg_idx].block_id
966 0, fs->blksz, journal_buffer);
969 if (ext4fs_log_journal(journal_buffer,
970 gd[bg_idx].block_id))
973 prev_bg_bitmap_index = bg_idx;
975 gd[bg_idx].free_blocks--;
976 fs->sb->free_blocks--;
980 free(journal_buffer);
983 return fs->curr_blkno;
985 free(journal_buffer);
991 int ext4fs_get_new_inode_no(void)
995 unsigned int ibmap_idx;
996 static int prev_inode_bitmap_index = -1;
997 unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group;
998 struct ext_filesystem *fs = get_fs();
999 char *journal_buffer = zalloc(fs->blksz);
1000 char *zero_buffer = zalloc(fs->blksz);
1001 if (!journal_buffer || !zero_buffer)
1003 struct ext2_block_group *gd = (struct ext2_block_group *)fs->gdtable;
1005 if (fs->first_pass_ibmap == 0) {
1006 for (i = 0; i < fs->no_blkgrp; i++) {
1007 if (gd[i].free_inodes) {
1008 if (gd[i].bg_itable_unused != gd[i].free_inodes)
1009 gd[i].bg_itable_unused =
1011 if (gd[i].bg_flags & EXT4_BG_INODE_UNINIT) {
1012 put_ext4(((uint64_t)
1013 (gd[i].inode_id * fs->blksz)),
1014 zero_buffer, fs->blksz);
1015 gd[i].bg_flags = gd[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 gd[i].free_inodes--;
1029 gd[i].bg_itable_unused--;
1030 fs->sb->free_inodes--;
1031 status = ext4fs_devread(gd[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 (gd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) {
1051 memset(zero_buffer, '\0', fs->blksz);
1052 put_ext4(((uint64_t) (gd[ibmap_idx].inode_id *
1053 fs->blksz)), zero_buffer,
1055 gd[ibmap_idx].bg_flags =
1056 gd[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(gd[ibmap_idx].inode_id
1074 0, fs->blksz, journal_buffer);
1077 if (ext4fs_log_journal(journal_buffer,
1078 gd[ibmap_idx].inode_id))
1080 prev_inode_bitmap_index = ibmap_idx;
1082 if (gd[ibmap_idx].bg_itable_unused != gd[ibmap_idx].free_inodes)
1083 gd[ibmap_idx].bg_itable_unused =
1084 gd[ibmap_idx].free_inodes;
1085 gd[ibmap_idx].free_inodes--;
1086 gd[ibmap_idx].bg_itable_unused--;
1087 fs->sb->free_inodes--;
1092 free(journal_buffer);
1095 return fs->curr_inode_no;
1097 free(journal_buffer);
1105 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1106 unsigned int *total_remaining_blocks,
1107 unsigned int *no_blks_reqd)
1111 long int actual_block_no;
1112 long int si_blockno;
1113 /* si :single indirect */
1114 unsigned int *si_buffer = NULL;
1115 unsigned int *si_start_addr = NULL;
1116 struct ext_filesystem *fs = get_fs();
1118 if (*total_remaining_blocks != 0) {
1119 si_buffer = zalloc(fs->blksz);
1121 printf("No Memory\n");
1124 si_start_addr = si_buffer;
1125 si_blockno = ext4fs_get_new_blk_no();
1126 if (si_blockno == -1) {
1127 printf("no block left to assign\n");
1131 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1133 status = ext4fs_devread(si_blockno * fs->sect_perblk,
1134 0, fs->blksz, (char *)si_buffer);
1135 memset(si_buffer, '\0', fs->blksz);
1139 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1140 actual_block_no = ext4fs_get_new_blk_no();
1141 if (actual_block_no == -1) {
1142 printf("no block left to assign\n");
1145 *si_buffer = actual_block_no;
1146 debug("SIAB %u: %u\n", *si_buffer,
1147 *total_remaining_blocks);
1150 (*total_remaining_blocks)--;
1151 if (*total_remaining_blocks == 0)
1155 /* write the block to disk */
1156 put_ext4(((uint64_t) (si_blockno * fs->blksz)),
1157 si_start_addr, fs->blksz);
1158 file_inode->b.blocks.indir_block = si_blockno;
1161 free(si_start_addr);
1164 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1165 unsigned int *total_remaining_blocks,
1166 unsigned int *no_blks_reqd)
1171 long int actual_block_no;
1172 /* di:double indirect */
1173 long int di_blockno_parent;
1174 long int di_blockno_child;
1175 unsigned int *di_parent_buffer = NULL;
1176 unsigned int *di_child_buff = NULL;
1177 unsigned int *di_block_start_addr = NULL;
1178 unsigned int *di_child_buff_start = NULL;
1179 struct ext_filesystem *fs = get_fs();
1181 if (*total_remaining_blocks != 0) {
1182 /* double indirect parent block connecting to inode */
1183 di_blockno_parent = ext4fs_get_new_blk_no();
1184 if (di_blockno_parent == -1) {
1185 printf("no block left to assign\n");
1188 di_parent_buffer = zalloc(fs->blksz);
1189 if (!di_parent_buffer)
1192 di_block_start_addr = di_parent_buffer;
1194 debug("DIPB %ld: %u\n", di_blockno_parent,
1195 *total_remaining_blocks);
1197 status = ext4fs_devread(di_blockno_parent *
1199 fs->blksz, (char *)di_parent_buffer);
1200 memset(di_parent_buffer, '\0', fs->blksz);
1203 * start:for each double indirect parent
1204 * block create one more block
1206 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1207 di_blockno_child = ext4fs_get_new_blk_no();
1208 if (di_blockno_child == -1) {
1209 printf("no block left to assign\n");
1212 di_child_buff = zalloc(fs->blksz);
1216 di_child_buff_start = di_child_buff;
1217 *di_parent_buffer = di_blockno_child;
1220 debug("DICB %ld: %u\n", di_blockno_child,
1221 *total_remaining_blocks);
1223 status = ext4fs_devread(di_blockno_child *
1226 (char *)di_child_buff);
1227 memset(di_child_buff, '\0', fs->blksz);
1228 /* filling of actual datablocks for each child */
1229 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1230 actual_block_no = ext4fs_get_new_blk_no();
1231 if (actual_block_no == -1) {
1232 printf("no block left to assign\n");
1235 *di_child_buff = actual_block_no;
1236 debug("DIAB %ld: %u\n", actual_block_no,
1237 *total_remaining_blocks);
1240 (*total_remaining_blocks)--;
1241 if (*total_remaining_blocks == 0)
1244 /* write the block table */
1245 put_ext4(((uint64_t) (di_blockno_child * fs->blksz)),
1246 di_child_buff_start, fs->blksz);
1247 free(di_child_buff_start);
1248 di_child_buff_start = NULL;
1250 if (*total_remaining_blocks == 0)
1253 put_ext4(((uint64_t) (di_blockno_parent * fs->blksz)),
1254 di_block_start_addr, fs->blksz);
1255 file_inode->b.blocks.double_indir_block = di_blockno_parent;
1258 free(di_block_start_addr);
1261 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1262 unsigned int *total_remaining_blocks,
1263 unsigned int *no_blks_reqd)
1268 long int actual_block_no;
1269 /* ti: Triple Indirect */
1270 long int ti_gp_blockno;
1271 long int ti_parent_blockno;
1272 long int ti_child_blockno;
1273 unsigned int *ti_gp_buff = NULL;
1274 unsigned int *ti_parent_buff = NULL;
1275 unsigned int *ti_child_buff = NULL;
1276 unsigned int *ti_gp_buff_start_addr = NULL;
1277 unsigned int *ti_pbuff_start_addr = NULL;
1278 unsigned int *ti_cbuff_start_addr = NULL;
1279 struct ext_filesystem *fs = get_fs();
1280 if (*total_remaining_blocks != 0) {
1281 /* triple indirect grand parent block connecting to inode */
1282 ti_gp_blockno = ext4fs_get_new_blk_no();
1283 if (ti_gp_blockno == -1) {
1284 printf("no block left to assign\n");
1287 ti_gp_buff = zalloc(fs->blksz);
1291 ti_gp_buff_start_addr = ti_gp_buff;
1293 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1294 *total_remaining_blocks);
1296 /* for each 4 byte grand parent entry create one more block */
1297 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1298 ti_parent_blockno = ext4fs_get_new_blk_no();
1299 if (ti_parent_blockno == -1) {
1300 printf("no block left to assign\n");
1303 ti_parent_buff = zalloc(fs->blksz);
1304 if (!ti_parent_buff)
1307 ti_pbuff_start_addr = ti_parent_buff;
1308 *ti_gp_buff = ti_parent_blockno;
1311 debug("TIPB %ld: %u\n", ti_parent_blockno,
1312 *total_remaining_blocks);
1314 /* for each 4 byte entry parent create one more block */
1315 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1316 ti_child_blockno = ext4fs_get_new_blk_no();
1317 if (ti_child_blockno == -1) {
1318 printf("no block left assign\n");
1321 ti_child_buff = zalloc(fs->blksz);
1325 ti_cbuff_start_addr = ti_child_buff;
1326 *ti_parent_buff = ti_child_blockno;
1329 debug("TICB %ld: %u\n", ti_parent_blockno,
1330 *total_remaining_blocks);
1332 /* fill actual datablocks for each child */
1333 for (k = 0; k < (fs->blksz / sizeof(int));
1336 ext4fs_get_new_blk_no();
1337 if (actual_block_no == -1) {
1338 printf("no block left\n");
1341 *ti_child_buff = actual_block_no;
1342 debug("TIAB %ld: %u\n", actual_block_no,
1343 *total_remaining_blocks);
1346 (*total_remaining_blocks)--;
1347 if (*total_remaining_blocks == 0)
1350 /* write the child block */
1351 put_ext4(((uint64_t) (ti_child_blockno *
1353 ti_cbuff_start_addr, fs->blksz);
1354 free(ti_cbuff_start_addr);
1356 if (*total_remaining_blocks == 0)
1359 /* write the parent block */
1360 put_ext4(((uint64_t) (ti_parent_blockno * fs->blksz)),
1361 ti_pbuff_start_addr, fs->blksz);
1362 free(ti_pbuff_start_addr);
1364 if (*total_remaining_blocks == 0)
1367 /* write the grand parent block */
1368 put_ext4(((uint64_t) (ti_gp_blockno * fs->blksz)),
1369 ti_gp_buff_start_addr, fs->blksz);
1370 file_inode->b.blocks.triple_indir_block = ti_gp_blockno;
1373 free(ti_gp_buff_start_addr);
1376 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1377 unsigned int total_remaining_blocks,
1378 unsigned int *total_no_of_block)
1381 long int direct_blockno;
1382 unsigned int no_blks_reqd = 0;
1384 /* allocation of direct blocks */
1385 for (i = 0; i < INDIRECT_BLOCKS; i++) {
1386 direct_blockno = ext4fs_get_new_blk_no();
1387 if (direct_blockno == -1) {
1388 printf("no block left to assign\n");
1391 file_inode->b.blocks.dir_blocks[i] = direct_blockno;
1392 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1394 total_remaining_blocks--;
1395 if (total_remaining_blocks == 0)
1399 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1401 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1403 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1405 *total_no_of_block += no_blks_reqd;
1410 static struct ext4_extent_header *ext4fs_get_extent_block
1411 (struct ext2_data *data, char *buf,
1412 struct ext4_extent_header *ext_block,
1413 uint32_t fileblock, int log2_blksz)
1415 struct ext4_extent_idx *index;
1416 unsigned long long block;
1417 struct ext_filesystem *fs = get_fs();
1421 index = (struct ext4_extent_idx *)(ext_block + 1);
1423 if (le32_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1426 if (ext_block->eh_depth == 0)
1431 if (i >= le32_to_cpu(ext_block->eh_entries))
1433 } while (fileblock > le32_to_cpu(index[i].ei_block));
1438 block = le32_to_cpu(index[i].ei_leaf_hi);
1439 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1441 if (ext4fs_devread(block << log2_blksz, 0, fs->blksz, buf))
1442 ext_block = (struct ext4_extent_header *)buf;
1448 static int ext4fs_blockgroup
1449 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1452 unsigned int blkoff, desc_per_blk;
1454 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1456 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
1457 group / desc_per_blk;
1458 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1460 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1461 group, blkno, blkoff);
1463 return ext4fs_devread(blkno << LOG2_EXT2_BLOCK_SIZE(data),
1464 blkoff, sizeof(struct ext2_block_group),
1468 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1470 struct ext2_block_group blkgrp;
1471 struct ext2_sblock *sblock = &data->sblock;
1472 struct ext_filesystem *fs = get_fs();
1473 int inodes_per_block, status;
1475 unsigned int blkoff;
1477 /* It is easier to calculate if the first inode is 0. */
1479 status = ext4fs_blockgroup(data, ino / __le32_to_cpu
1480 (sblock->inodes_per_group), &blkgrp);
1484 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1485 blkno = __le32_to_cpu(blkgrp.inode_table_id) +
1486 (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1487 blkoff = (ino % inodes_per_block) * fs->inodesz;
1488 /* Read the inode. */
1489 status = ext4fs_devread(blkno << LOG2_EXT2_BLOCK_SIZE(data), blkoff,
1490 sizeof(struct ext2_inode), (char *)inode);
1497 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1504 long int perblock_parent;
1505 long int perblock_child;
1506 unsigned long long start;
1507 /* get the blocksize of the filesystem */
1508 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1509 log2_blksz = LOG2_EXT2_BLOCK_SIZE(ext4fs_root);
1510 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1511 char *buf = zalloc(blksz);
1514 struct ext4_extent_header *ext_block;
1515 struct ext4_extent *extent;
1517 ext_block = ext4fs_get_extent_block(ext4fs_root, buf,
1518 (struct ext4_extent_header
1521 fileblock, log2_blksz);
1523 printf("invalid extent block\n");
1528 extent = (struct ext4_extent *)(ext_block + 1);
1532 if (i >= le32_to_cpu(ext_block->eh_entries))
1534 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1536 fileblock -= le32_to_cpu(extent[i].ee_block);
1537 if (fileblock >= le32_to_cpu(extent[i].ee_len)) {
1542 start = le32_to_cpu(extent[i].ee_start_hi);
1543 start = (start << 32) +
1544 le32_to_cpu(extent[i].ee_start_lo);
1546 return fileblock + start;
1549 printf("Extent Error\n");
1554 /* Direct blocks. */
1555 if (fileblock < INDIRECT_BLOCKS)
1556 blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1559 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1560 if (ext4fs_indir1_block == NULL) {
1561 ext4fs_indir1_block = zalloc(blksz);
1562 if (ext4fs_indir1_block == NULL) {
1563 printf("** SI ext2fs read block (indir 1)"
1564 "malloc failed. **\n");
1567 ext4fs_indir1_size = blksz;
1568 ext4fs_indir1_blkno = -1;
1570 if (blksz != ext4fs_indir1_size) {
1571 free(ext4fs_indir1_block);
1572 ext4fs_indir1_block = NULL;
1573 ext4fs_indir1_size = 0;
1574 ext4fs_indir1_blkno = -1;
1575 ext4fs_indir1_block = zalloc(blksz);
1576 if (ext4fs_indir1_block == NULL) {
1577 printf("** SI ext2fs read block (indir 1):"
1578 "malloc failed. **\n");
1581 ext4fs_indir1_size = blksz;
1583 if ((__le32_to_cpu(inode->b.blocks.indir_block) <<
1584 log2_blksz) != ext4fs_indir1_blkno) {
1586 ext4fs_devread(__le32_to_cpu
1588 indir_block) << log2_blksz, 0,
1589 blksz, (char *)ext4fs_indir1_block);
1591 printf("** SI ext2fs read block (indir 1)"
1595 ext4fs_indir1_blkno =
1596 __le32_to_cpu(inode->b.blocks.
1597 indir_block) << log2_blksz;
1599 blknr = __le32_to_cpu(ext4fs_indir1_block
1600 [fileblock - INDIRECT_BLOCKS]);
1602 /* Double indirect. */
1603 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1604 (blksz / 4 + 1)))) {
1606 long int perblock = blksz / 4;
1607 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1609 if (ext4fs_indir1_block == NULL) {
1610 ext4fs_indir1_block = zalloc(blksz);
1611 if (ext4fs_indir1_block == NULL) {
1612 printf("** DI ext2fs read block (indir 2 1)"
1613 "malloc failed. **\n");
1616 ext4fs_indir1_size = blksz;
1617 ext4fs_indir1_blkno = -1;
1619 if (blksz != ext4fs_indir1_size) {
1620 free(ext4fs_indir1_block);
1621 ext4fs_indir1_block = NULL;
1622 ext4fs_indir1_size = 0;
1623 ext4fs_indir1_blkno = -1;
1624 ext4fs_indir1_block = zalloc(blksz);
1625 if (ext4fs_indir1_block == NULL) {
1626 printf("** DI ext2fs read block (indir 2 1)"
1627 "malloc failed. **\n");
1630 ext4fs_indir1_size = blksz;
1632 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) <<
1633 log2_blksz) != ext4fs_indir1_blkno) {
1635 ext4fs_devread(__le32_to_cpu
1637 double_indir_block) << log2_blksz,
1639 (char *)ext4fs_indir1_block);
1641 printf("** DI ext2fs read block (indir 2 1)"
1645 ext4fs_indir1_blkno =
1646 __le32_to_cpu(inode->b.blocks.double_indir_block) <<
1650 if (ext4fs_indir2_block == NULL) {
1651 ext4fs_indir2_block = zalloc(blksz);
1652 if (ext4fs_indir2_block == NULL) {
1653 printf("** DI ext2fs read block (indir 2 2)"
1654 "malloc failed. **\n");
1657 ext4fs_indir2_size = blksz;
1658 ext4fs_indir2_blkno = -1;
1660 if (blksz != ext4fs_indir2_size) {
1661 free(ext4fs_indir2_block);
1662 ext4fs_indir2_block = NULL;
1663 ext4fs_indir2_size = 0;
1664 ext4fs_indir2_blkno = -1;
1665 ext4fs_indir2_block = zalloc(blksz);
1666 if (ext4fs_indir2_block == NULL) {
1667 printf("** DI ext2fs read block (indir 2 2)"
1668 "malloc failed. **\n");
1671 ext4fs_indir2_size = blksz;
1673 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1674 log2_blksz) != ext4fs_indir2_blkno) {
1675 status = ext4fs_devread(__le32_to_cpu
1676 (ext4fs_indir1_block
1678 perblock]) << log2_blksz, 0,
1680 (char *)ext4fs_indir2_block);
1682 printf("** DI ext2fs read block (indir 2 2)"
1686 ext4fs_indir2_blkno =
1687 __le32_to_cpu(ext4fs_indir1_block[rblock
1692 blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1694 /* Tripple indirect. */
1696 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1697 (blksz / 4 * blksz / 4));
1698 perblock_child = blksz / 4;
1699 perblock_parent = ((blksz / 4) * (blksz / 4));
1701 if (ext4fs_indir1_block == NULL) {
1702 ext4fs_indir1_block = zalloc(blksz);
1703 if (ext4fs_indir1_block == NULL) {
1704 printf("** TI ext2fs read block (indir 2 1)"
1705 "malloc failed. **\n");
1708 ext4fs_indir1_size = blksz;
1709 ext4fs_indir1_blkno = -1;
1711 if (blksz != ext4fs_indir1_size) {
1712 free(ext4fs_indir1_block);
1713 ext4fs_indir1_block = NULL;
1714 ext4fs_indir1_size = 0;
1715 ext4fs_indir1_blkno = -1;
1716 ext4fs_indir1_block = zalloc(blksz);
1717 if (ext4fs_indir1_block == NULL) {
1718 printf("** TI ext2fs read block (indir 2 1)"
1719 "malloc failed. **\n");
1722 ext4fs_indir1_size = blksz;
1724 if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1725 log2_blksz) != ext4fs_indir1_blkno) {
1726 status = ext4fs_devread
1727 (__le32_to_cpu(inode->b.blocks.triple_indir_block)
1728 << log2_blksz, 0, blksz,
1729 (char *)ext4fs_indir1_block);
1731 printf("** TI ext2fs read block (indir 2 1)"
1735 ext4fs_indir1_blkno =
1736 __le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1740 if (ext4fs_indir2_block == NULL) {
1741 ext4fs_indir2_block = zalloc(blksz);
1742 if (ext4fs_indir2_block == NULL) {
1743 printf("** TI ext2fs read block (indir 2 2)"
1744 "malloc failed. **\n");
1747 ext4fs_indir2_size = blksz;
1748 ext4fs_indir2_blkno = -1;
1750 if (blksz != ext4fs_indir2_size) {
1751 free(ext4fs_indir2_block);
1752 ext4fs_indir2_block = NULL;
1753 ext4fs_indir2_size = 0;
1754 ext4fs_indir2_blkno = -1;
1755 ext4fs_indir2_block = zalloc(blksz);
1756 if (ext4fs_indir2_block == NULL) {
1757 printf("** TI ext2fs read block (indir 2 2)"
1758 "malloc failed. **\n");
1761 ext4fs_indir2_size = blksz;
1763 if ((__le32_to_cpu(ext4fs_indir1_block[rblock /
1764 perblock_parent]) <<
1766 != ext4fs_indir2_blkno) {
1767 status = ext4fs_devread(__le32_to_cpu
1768 (ext4fs_indir1_block
1770 perblock_parent]) <<
1771 log2_blksz, 0, blksz,
1772 (char *)ext4fs_indir2_block);
1774 printf("** TI ext2fs read block (indir 2 2)"
1778 ext4fs_indir2_blkno =
1779 __le32_to_cpu(ext4fs_indir1_block[rblock /
1784 if (ext4fs_indir3_block == NULL) {
1785 ext4fs_indir3_block = zalloc(blksz);
1786 if (ext4fs_indir3_block == NULL) {
1787 printf("** TI ext2fs read block (indir 2 2)"
1788 "malloc failed. **\n");
1791 ext4fs_indir3_size = blksz;
1792 ext4fs_indir3_blkno = -1;
1794 if (blksz != ext4fs_indir3_size) {
1795 free(ext4fs_indir3_block);
1796 ext4fs_indir3_block = NULL;
1797 ext4fs_indir3_size = 0;
1798 ext4fs_indir3_blkno = -1;
1799 ext4fs_indir3_block = zalloc(blksz);
1800 if (ext4fs_indir3_block == NULL) {
1801 printf("** TI ext2fs read block (indir 2 2)"
1802 "malloc failed. **\n");
1805 ext4fs_indir3_size = blksz;
1807 if ((__le32_to_cpu(ext4fs_indir2_block[rblock
1810 log2_blksz) != ext4fs_indir3_blkno) {
1812 ext4fs_devread(__le32_to_cpu
1813 (ext4fs_indir2_block
1814 [(rblock / perblock_child)
1815 % (blksz / 4)]) << log2_blksz, 0,
1816 blksz, (char *)ext4fs_indir3_block);
1818 printf("** TI ext2fs read block (indir 2 2)"
1822 ext4fs_indir3_blkno =
1823 __le32_to_cpu(ext4fs_indir2_block[(rblock /
1830 blknr = __le32_to_cpu(ext4fs_indir3_block
1831 [rblock % perblock_child]);
1833 debug("ext4fs_read_block %ld\n", blknr);
1838 void ext4fs_close(void)
1840 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1841 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1844 if (ext4fs_root != NULL) {
1848 if (ext4fs_indir1_block != NULL) {
1849 free(ext4fs_indir1_block);
1850 ext4fs_indir1_block = NULL;
1851 ext4fs_indir1_size = 0;
1852 ext4fs_indir1_blkno = -1;
1854 if (ext4fs_indir2_block != NULL) {
1855 free(ext4fs_indir2_block);
1856 ext4fs_indir2_block = NULL;
1857 ext4fs_indir2_size = 0;
1858 ext4fs_indir2_blkno = -1;
1860 if (ext4fs_indir3_block != NULL) {
1861 free(ext4fs_indir3_block);
1862 ext4fs_indir3_block = NULL;
1863 ext4fs_indir3_size = 0;
1864 ext4fs_indir3_blkno = -1;
1868 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1869 struct ext2fs_node **fnode, int *ftype)
1871 unsigned int fpos = 0;
1873 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1877 printf("Iterate dir %s\n", name);
1878 #endif /* of DEBUG */
1879 if (!diro->inode_read) {
1880 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1884 /* Search the file. */
1885 while (fpos < __le32_to_cpu(diro->inode.size)) {
1886 struct ext2_dirent dirent;
1888 status = ext4fs_read_file(diro, fpos,
1889 sizeof(struct ext2_dirent),
1894 if (dirent.namelen != 0) {
1895 char filename[dirent.namelen + 1];
1896 struct ext2fs_node *fdiro;
1897 int type = FILETYPE_UNKNOWN;
1899 status = ext4fs_read_file(diro,
1901 sizeof(struct ext2_dirent),
1902 dirent.namelen, filename);
1906 fdiro = zalloc(sizeof(struct ext2fs_node));
1910 fdiro->data = diro->data;
1911 fdiro->ino = __le32_to_cpu(dirent.inode);
1913 filename[dirent.namelen] = '\0';
1915 if (dirent.filetype != FILETYPE_UNKNOWN) {
1916 fdiro->inode_read = 0;
1918 if (dirent.filetype == FILETYPE_DIRECTORY)
1919 type = FILETYPE_DIRECTORY;
1920 else if (dirent.filetype == FILETYPE_SYMLINK)
1921 type = FILETYPE_SYMLINK;
1922 else if (dirent.filetype == FILETYPE_REG)
1923 type = FILETYPE_REG;
1925 status = ext4fs_read_inode(diro->data,
1933 fdiro->inode_read = 1;
1935 if ((__le16_to_cpu(fdiro->inode.mode) &
1936 FILETYPE_INO_MASK) ==
1937 FILETYPE_INO_DIRECTORY) {
1938 type = FILETYPE_DIRECTORY;
1939 } else if ((__le16_to_cpu(fdiro->inode.mode)
1940 & FILETYPE_INO_MASK) ==
1941 FILETYPE_INO_SYMLINK) {
1942 type = FILETYPE_SYMLINK;
1943 } else if ((__le16_to_cpu(fdiro->inode.mode)
1944 & FILETYPE_INO_MASK) ==
1946 type = FILETYPE_REG;
1950 printf("iterate >%s<\n", filename);
1951 #endif /* of DEBUG */
1952 if ((name != NULL) && (fnode != NULL)
1953 && (ftype != NULL)) {
1954 if (strcmp(filename, name) == 0) {
1960 if (fdiro->inode_read == 0) {
1961 status = ext4fs_read_inode(diro->data,
1969 fdiro->inode_read = 1;
1972 case FILETYPE_DIRECTORY:
1975 case FILETYPE_SYMLINK:
1986 __le32_to_cpu(fdiro->inode.size),
1991 fpos += __le16_to_cpu(dirent.direntlen);
1996 static char *ext4fs_read_symlink(struct ext2fs_node *node)
1999 struct ext2fs_node *diro = node;
2002 if (!diro->inode_read) {
2003 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2007 symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1);
2011 if (__le32_to_cpu(diro->inode.size) <= 60) {
2012 strncpy(symlink, diro->inode.b.symlink,
2013 __le32_to_cpu(diro->inode.size));
2015 status = ext4fs_read_file(diro, 0,
2016 __le32_to_cpu(diro->inode.size),
2023 symlink[__le32_to_cpu(diro->inode.size)] = '\0';
2027 static int ext4fs_find_file1(const char *currpath,
2028 struct ext2fs_node *currroot,
2029 struct ext2fs_node **currfound, int *foundtype)
2031 char fpath[strlen(currpath) + 1];
2035 int type = FILETYPE_DIRECTORY;
2036 struct ext2fs_node *currnode = currroot;
2037 struct ext2fs_node *oldnode = currroot;
2039 strncpy(fpath, currpath, strlen(currpath) + 1);
2041 /* Remove all leading slashes. */
2042 while (*name == '/')
2046 *currfound = currnode;
2053 /* Extract the actual part from the pathname. */
2054 next = strchr(name, '/');
2056 /* Remove all leading slashes. */
2057 while (*next == '/')
2061 if (type != FILETYPE_DIRECTORY) {
2062 ext4fs_free_node(currnode, currroot);
2068 /* Iterate over the directory. */
2069 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2076 /* Read in the symlink and follow it. */
2077 if (type == FILETYPE_SYMLINK) {
2080 /* Test if the symlink does not loop. */
2081 if (++symlinknest == 8) {
2082 ext4fs_free_node(currnode, currroot);
2083 ext4fs_free_node(oldnode, currroot);
2087 symlink = ext4fs_read_symlink(currnode);
2088 ext4fs_free_node(currnode, currroot);
2091 ext4fs_free_node(oldnode, currroot);
2095 debug("Got symlink >%s<\n", symlink);
2097 if (symlink[0] == '/') {
2098 ext4fs_free_node(oldnode, currroot);
2099 oldnode = &ext4fs_root->diropen;
2102 /* Lookup the node the symlink points to. */
2103 status = ext4fs_find_file1(symlink, oldnode,
2109 ext4fs_free_node(oldnode, currroot);
2114 ext4fs_free_node(oldnode, currroot);
2116 /* Found the node! */
2117 if (!next || *next == '\0') {
2118 *currfound = currnode;
2127 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2128 struct ext2fs_node **foundnode, int expecttype)
2131 int foundtype = FILETYPE_DIRECTORY;
2137 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2141 /* Check if the node that was found was of the expected type. */
2142 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2144 else if ((expecttype == FILETYPE_DIRECTORY)
2145 && (foundtype != expecttype))
2151 int ext4fs_open(const char *filename)
2153 struct ext2fs_node *fdiro = NULL;
2157 if (ext4fs_root == NULL)
2161 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2166 if (!fdiro->inode_read) {
2167 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2172 len = __le32_to_cpu(fdiro->inode.size);
2173 ext4fs_file = fdiro;
2177 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2182 int ext4fs_mount(unsigned part_length)
2184 struct ext2_data *data;
2186 struct ext_filesystem *fs = get_fs();
2187 data = zalloc(sizeof(struct ext2_data));
2191 /* Read the superblock. */
2192 status = ext4fs_devread(1 * 2, 0, sizeof(struct ext2_sblock),
2193 (char *)&data->sblock);
2198 /* Make sure this is an ext2 filesystem. */
2199 if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2202 if (__le32_to_cpu(data->sblock.revision_level == 0))
2205 fs->inodesz = __le16_to_cpu(data->sblock.inode_size);
2207 debug("EXT2 rev %d, inode_size %d\n",
2208 __le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2210 data->diropen.data = data;
2211 data->diropen.ino = 2;
2212 data->diropen.inode_read = 1;
2213 data->inode = &data->diropen.inode;
2215 status = ext4fs_read_inode(data, 2, data->inode);
2223 printf("Failed to mount ext2 filesystem...\n");