2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
7 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
10 * esd gmbh <www.esd-electronics.com>
11 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
14 * GRUB -- GRand Unified Bootloader
15 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
17 * ext4write : Based on generic ext4 protocol.
19 * SPDX-License-Identifier: GPL-2.0+
23 #include <ext_common.h>
29 #include <linux/stat.h>
30 #include <linux/time.h>
31 #include <asm/byteorder.h>
32 #include "ext4_common.h"
34 struct ext2_data
*ext4fs_root
;
35 struct ext2fs_node
*ext4fs_file
;
36 uint32_t *ext4fs_indir1_block
;
37 int ext4fs_indir1_size
;
38 int ext4fs_indir1_blkno
= -1;
39 uint32_t *ext4fs_indir2_block
;
40 int ext4fs_indir2_size
;
41 int ext4fs_indir2_blkno
= -1;
43 uint32_t *ext4fs_indir3_block
;
44 int ext4fs_indir3_size
;
45 int ext4fs_indir3_blkno
= -1;
46 struct ext2_inode
*g_parent_inode
;
47 static int symlinknest
;
49 #if defined(CONFIG_EXT4_WRITE)
50 uint32_t ext4fs_div_roundup(uint32_t size
, uint32_t n
)
52 uint32_t res
= size
/ n
;
59 void put_ext4(uint64_t off
, void *buf
, uint32_t size
)
63 unsigned char *temp_ptr
= NULL
;
64 struct ext_filesystem
*fs
= get_fs();
65 int log2blksz
= fs
->dev_desc
->log2blksz
;
66 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf
, fs
->dev_desc
->blksz
);
68 startblock
= off
>> log2blksz
;
69 startblock
+= part_offset
;
70 remainder
= off
& (uint64_t)(fs
->dev_desc
->blksz
- 1);
72 if (fs
->dev_desc
== NULL
)
75 if ((startblock
+ (size
>> log2blksz
)) >
76 (part_offset
+ fs
->total_sect
)) {
77 printf("part_offset is " LBAFU
"\n", part_offset
);
78 printf("total_sector is %" PRIu64
"\n", fs
->total_sect
);
79 printf("error: overflow occurs\n");
84 blk_dread(fs
->dev_desc
, startblock
, 1, sec_buf
);
86 memcpy((temp_ptr
+ remainder
), (unsigned char *)buf
, size
);
87 blk_dwrite(fs
->dev_desc
, startblock
, 1, sec_buf
);
89 if (size
>> log2blksz
!= 0) {
90 blk_dwrite(fs
->dev_desc
, startblock
, size
>> log2blksz
,
91 (unsigned long *)buf
);
93 blk_dread(fs
->dev_desc
, startblock
, 1, sec_buf
);
95 memcpy(temp_ptr
, buf
, size
);
96 blk_dwrite(fs
->dev_desc
, startblock
, 1,
97 (unsigned long *)sec_buf
);
102 static int _get_new_inode_no(unsigned char *buffer
)
104 struct ext_filesystem
*fs
= get_fs();
110 /* get the blocksize of the filesystem */
111 unsigned char *ptr
= buffer
;
112 while (*ptr
== 255) {
115 if (count
> ext4fs_root
->sblock
.inodes_per_group
)
119 for (j
= 0; j
< fs
->blksz
; j
++) {
124 status
= input
& operand
;
139 static int _get_new_blk_no(unsigned char *buffer
)
145 unsigned char *ptr
= buffer
;
146 struct ext_filesystem
*fs
= get_fs();
148 if (fs
->blksz
!= 1024)
153 while (*ptr
== 255) {
156 if (count
== (fs
->blksz
* 8))
160 for (j
= 0; j
< fs
->blksz
; j
++) {
165 status
= input
& operand
;
180 int ext4fs_set_block_bmap(long int blockno
, unsigned char *buffer
, int index
)
182 int i
, remainder
, status
;
183 unsigned char *ptr
= buffer
;
184 unsigned char operand
;
186 remainder
= blockno
% 8;
187 int blocksize
= EXT2_BLOCK_SIZE(ext4fs_root
);
189 i
= i
- (index
* blocksize
);
190 if (blocksize
!= 1024) {
192 operand
= 1 << remainder
;
193 status
= *ptr
& operand
;
197 *ptr
= *ptr
| operand
;
200 if (remainder
== 0) {
205 operand
= (1 << (remainder
- 1));
207 status
= *ptr
& operand
;
211 *ptr
= *ptr
| operand
;
216 void ext4fs_reset_block_bmap(long int blockno
, unsigned char *buffer
, int index
)
218 int i
, remainder
, status
;
219 unsigned char *ptr
= buffer
;
220 unsigned char operand
;
222 remainder
= blockno
% 8;
223 int blocksize
= EXT2_BLOCK_SIZE(ext4fs_root
);
225 i
= i
- (index
* blocksize
);
226 if (blocksize
!= 1024) {
228 operand
= (1 << remainder
);
229 status
= *ptr
& operand
;
231 *ptr
= *ptr
& ~(operand
);
233 if (remainder
== 0) {
238 operand
= (1 << (remainder
- 1));
240 status
= *ptr
& operand
;
242 *ptr
= *ptr
& ~(operand
);
246 int ext4fs_set_inode_bmap(int inode_no
, unsigned char *buffer
, int index
)
248 int i
, remainder
, status
;
249 unsigned char *ptr
= buffer
;
250 unsigned char operand
;
252 inode_no
-= (index
* ext4fs_root
->sblock
.inodes_per_group
);
254 remainder
= inode_no
% 8;
255 if (remainder
== 0) {
260 operand
= (1 << (remainder
- 1));
262 status
= *ptr
& operand
;
266 *ptr
= *ptr
| operand
;
271 void ext4fs_reset_inode_bmap(int inode_no
, unsigned char *buffer
, int index
)
273 int i
, remainder
, status
;
274 unsigned char *ptr
= buffer
;
275 unsigned char operand
;
277 inode_no
-= (index
* ext4fs_root
->sblock
.inodes_per_group
);
279 remainder
= inode_no
% 8;
280 if (remainder
== 0) {
285 operand
= (1 << (remainder
- 1));
287 status
= *ptr
& operand
;
289 *ptr
= *ptr
& ~(operand
);
292 int ext4fs_checksum_update(unsigned int i
)
294 struct ext2_block_group
*desc
;
295 struct ext_filesystem
*fs
= get_fs();
298 desc
= (struct ext2_block_group
*)&fs
->bgd
[i
];
299 if (fs
->sb
->feature_ro_compat
& EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) {
300 int offset
= offsetof(struct ext2_block_group
, bg_checksum
);
302 crc
= ext2fs_crc16(~0, fs
->sb
->unique_id
,
303 sizeof(fs
->sb
->unique_id
));
304 crc
= ext2fs_crc16(crc
, &i
, sizeof(i
));
305 crc
= ext2fs_crc16(crc
, desc
, offset
);
306 offset
+= sizeof(desc
->bg_checksum
); /* skip checksum */
307 assert(offset
== sizeof(*desc
));
313 static int check_void_in_dentry(struct ext2_dirent
*dir
, char *filename
)
316 int sizeof_void_space
;
317 int new_entry_byte_reqd
;
318 short padding_factor
= 0;
320 if (dir
->namelen
% 4 != 0)
321 padding_factor
= 4 - (dir
->namelen
% 4);
323 dentry_length
= sizeof(struct ext2_dirent
) +
324 dir
->namelen
+ padding_factor
;
325 sizeof_void_space
= dir
->direntlen
- dentry_length
;
326 if (sizeof_void_space
== 0)
330 if (strlen(filename
) % 4 != 0)
331 padding_factor
= 4 - (strlen(filename
) % 4);
333 new_entry_byte_reqd
= strlen(filename
) +
334 sizeof(struct ext2_dirent
) + padding_factor
;
335 if (sizeof_void_space
>= new_entry_byte_reqd
) {
336 dir
->direntlen
= dentry_length
;
337 return sizeof_void_space
;
343 void ext4fs_update_parent_dentry(char *filename
, int *p_ino
, int file_type
)
345 unsigned int *zero_buffer
= NULL
;
346 char *root_first_block_buffer
= NULL
;
349 long int first_block_no_of_root
= 0;
350 long int previous_blknr
= -1;
352 short int padding_factor
= 0;
353 unsigned int new_entry_byte_reqd
;
354 unsigned int last_entry_dirlen
;
355 int sizeof_void_space
= 0;
359 struct ext_filesystem
*fs
= get_fs();
360 /* directory entry */
361 struct ext2_dirent
*dir
;
362 char *temp_dir
= NULL
;
364 zero_buffer
= zalloc(fs
->blksz
);
366 printf("No Memory\n");
369 root_first_block_buffer
= zalloc(fs
->blksz
);
370 if (!root_first_block_buffer
) {
372 printf("No Memory\n");
377 /* read the block no allocated to a file */
378 for (direct_blk_idx
= 0; direct_blk_idx
< INDIRECT_BLOCKS
;
380 root_blknr
= read_allocated_block(g_parent_inode
,
382 if (root_blknr
== 0) {
383 first_block_no_of_root
= previous_blknr
;
386 previous_blknr
= root_blknr
;
389 status
= ext4fs_devread((lbaint_t
)first_block_no_of_root
391 0, fs
->blksz
, root_first_block_buffer
);
395 if (ext4fs_log_journal(root_first_block_buffer
, first_block_no_of_root
))
397 dir
= (struct ext2_dirent
*)root_first_block_buffer
;
399 while (dir
->direntlen
> 0) {
401 * blocksize-totalbytes because last directory length
402 * i.e. dir->direntlen is free availble space in the
403 * block that means it is a last entry of directory
407 /* traversing the each directory entry */
408 if (fs
->blksz
- totalbytes
== dir
->direntlen
) {
409 if (strlen(filename
) % 4 != 0)
410 padding_factor
= 4 - (strlen(filename
) % 4);
412 new_entry_byte_reqd
= strlen(filename
) +
413 sizeof(struct ext2_dirent
) + padding_factor
;
416 * update last directory entry length to its
417 * length because we are creating new directory
420 if (dir
->namelen
% 4 != 0)
421 padding_factor
= 4 - (dir
->namelen
% 4);
423 last_entry_dirlen
= dir
->namelen
+
424 sizeof(struct ext2_dirent
) + padding_factor
;
425 if ((fs
->blksz
- totalbytes
- last_entry_dirlen
) <
426 new_entry_byte_reqd
) {
427 printf("1st Block Full:Allocate new block\n");
429 if (direct_blk_idx
== INDIRECT_BLOCKS
- 1) {
430 printf("Directory exceeds limit\n");
433 g_parent_inode
->b
.blocks
.dir_blocks
434 [direct_blk_idx
] = ext4fs_get_new_blk_no();
435 if (g_parent_inode
->b
.blocks
.dir_blocks
436 [direct_blk_idx
] == -1) {
437 printf("no block left to assign\n");
441 ((uint64_t)g_parent_inode
->b
.
442 blocks
.dir_blocks
[direct_blk_idx
] *
443 (uint64_t)fs
->blksz
)), zero_buffer
, fs
->blksz
);
444 g_parent_inode
->size
=
445 g_parent_inode
->size
+ fs
->blksz
;
446 g_parent_inode
->blockcnt
=
447 g_parent_inode
->blockcnt
+ fs
->sect_perblk
;
448 if (ext4fs_put_metadata
449 (root_first_block_buffer
,
450 first_block_no_of_root
))
454 dir
->direntlen
= last_entry_dirlen
;
458 templength
= dir
->direntlen
;
459 totalbytes
= totalbytes
+ templength
;
460 sizeof_void_space
= check_void_in_dentry(dir
, filename
);
461 if (sizeof_void_space
)
464 dir
= (struct ext2_dirent
*)((char *)dir
+ templength
);
467 /* make a pointer ready for creating next directory entry */
468 templength
= dir
->direntlen
;
469 totalbytes
= totalbytes
+ templength
;
470 dir
= (struct ext2_dirent
*)((char *)dir
+ templength
);
472 /* get the next available inode number */
473 inodeno
= ext4fs_get_new_inode_no();
475 printf("no inode left to assign\n");
478 dir
->inode
= inodeno
;
479 if (sizeof_void_space
)
480 dir
->direntlen
= sizeof_void_space
;
482 dir
->direntlen
= fs
->blksz
- totalbytes
;
484 dir
->namelen
= strlen(filename
);
485 dir
->filetype
= FILETYPE_REG
; /* regular file */
486 temp_dir
= (char *)dir
;
487 temp_dir
= temp_dir
+ sizeof(struct ext2_dirent
);
488 memcpy(temp_dir
, filename
, strlen(filename
));
492 /* update or write the 1st block of root inode */
493 if (ext4fs_put_metadata(root_first_block_buffer
,
494 first_block_no_of_root
))
499 free(root_first_block_buffer
);
502 static int search_dir(struct ext2_inode
*parent_inode
, char *dirname
)
512 unsigned char *block_buffer
= NULL
;
513 struct ext2_dirent
*dir
= NULL
;
514 struct ext2_dirent
*previous_dir
= NULL
;
515 struct ext_filesystem
*fs
= get_fs();
517 /* read the block no allocated to a file */
518 for (direct_blk_idx
= 0; direct_blk_idx
< INDIRECT_BLOCKS
;
520 blknr
= read_allocated_block(parent_inode
, direct_blk_idx
);
524 /* read the blocks of parenet inode */
525 block_buffer
= zalloc(fs
->blksz
);
529 status
= ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
,
530 0, fs
->blksz
, (char *)block_buffer
);
534 dir
= (struct ext2_dirent
*)block_buffer
;
537 while (dir
->direntlen
>= 0) {
539 * blocksize-totalbytes because last directory
540 * length i.e.,*dir->direntlen is free availble
541 * space in the block that means
542 * it is a last entry of directory entry
544 if (strlen(dirname
) == dir
->namelen
) {
545 if (strncmp(dirname
, ptr
+
546 sizeof(struct ext2_dirent
),
547 dir
->namelen
) == 0) {
548 previous_dir
->direntlen
+=
550 inodeno
= dir
->inode
;
557 if (fs
->blksz
- totalbytes
== dir
->direntlen
)
560 /* traversing the each directory entry */
561 templength
= dir
->direntlen
;
562 totalbytes
= totalbytes
+ templength
;
564 dir
= (struct ext2_dirent
*)((char *)dir
+ templength
);
584 static int find_dir_depth(char *dirname
)
586 char *token
= strtok(dirname
, "/");
588 while (token
!= NULL
) {
589 token
= strtok(NULL
, "/");
592 return count
+ 1 + 1;
594 * for example for string /home/temp
595 * depth=home(1)+temp(1)+1 extra for NULL;
600 static int parse_path(char **arr
, char *dirname
)
602 char *token
= strtok(dirname
, "/");
606 arr
[i
] = zalloc(strlen("/") + 1);
609 memcpy(arr
[i
++], "/", strlen("/"));
611 /* add each path entry after root */
612 while (token
!= NULL
) {
613 arr
[i
] = zalloc(strlen(token
) + 1);
616 memcpy(arr
[i
++], token
, strlen(token
));
617 token
= strtok(NULL
, "/");
624 int ext4fs_iget(int inode_no
, struct ext2_inode
*inode
)
626 if (ext4fs_read_inode(ext4fs_root
, inode_no
, inode
) == 0)
633 * Function: ext4fs_get_parent_inode_num
634 * Return Value: inode Number of the parent directory of file/Directory to be
636 * dirname : Input parmater, input path name of the file/directory to be created
637 * dname : Output parameter, to be filled with the name of the directory
638 * extracted from dirname
640 int ext4fs_get_parent_inode_num(const char *dirname
, char *dname
, int flags
)
644 int matched_inode_no
;
645 int result_inode_no
= -1;
647 char *depth_dirname
= NULL
;
648 char *parse_dirname
= NULL
;
649 struct ext2_inode
*parent_inode
= NULL
;
650 struct ext2_inode
*first_inode
= NULL
;
651 struct ext2_inode temp_inode
;
653 if (*dirname
!= '/') {
654 printf("Please supply Absolute path\n");
658 /* TODO: input validation make equivalent to linux */
659 depth_dirname
= zalloc(strlen(dirname
) + 1);
663 memcpy(depth_dirname
, dirname
, strlen(dirname
));
664 depth
= find_dir_depth(depth_dirname
);
665 parse_dirname
= zalloc(strlen(dirname
) + 1);
668 memcpy(parse_dirname
, dirname
, strlen(dirname
));
670 /* allocate memory for each directory level */
671 ptr
= zalloc((depth
) * sizeof(char *));
674 if (parse_path(ptr
, parse_dirname
))
676 parent_inode
= zalloc(sizeof(struct ext2_inode
));
679 first_inode
= zalloc(sizeof(struct ext2_inode
));
682 memcpy(parent_inode
, ext4fs_root
->inode
, sizeof(struct ext2_inode
));
683 memcpy(first_inode
, parent_inode
, sizeof(struct ext2_inode
));
685 result_inode_no
= EXT2_ROOT_INO
;
686 for (i
= 1; i
< depth
; i
++) {
687 matched_inode_no
= search_dir(parent_inode
, ptr
[i
]);
688 if (matched_inode_no
== -1) {
689 if (ptr
[i
+ 1] == NULL
&& i
== 1) {
690 result_inode_no
= EXT2_ROOT_INO
;
693 if (ptr
[i
+ 1] == NULL
)
695 printf("Invalid path\n");
696 result_inode_no
= -1;
700 if (ptr
[i
+ 1] != NULL
) {
701 memset(parent_inode
, '\0',
702 sizeof(struct ext2_inode
));
703 if (ext4fs_iget(matched_inode_no
,
705 result_inode_no
= -1;
708 result_inode_no
= matched_inode_no
;
717 matched_inode_no
= search_dir(first_inode
, ptr
[i
]);
719 matched_inode_no
= search_dir(parent_inode
, ptr
[i
]);
721 if (matched_inode_no
!= -1) {
722 ext4fs_iget(matched_inode_no
, &temp_inode
);
723 if (temp_inode
.mode
& S_IFDIR
) {
724 printf("It is a Directory\n");
725 result_inode_no
= -1;
730 if (strlen(ptr
[i
]) > 256) {
731 result_inode_no
= -1;
734 memcpy(dname
, ptr
[i
], strlen(ptr
[i
]));
739 for (i
= 0; i
< depth
; i
++) {
748 return result_inode_no
;
751 static int check_filename(char *filename
, unsigned int blknr
)
753 unsigned int first_block_no_of_root
;
758 char *root_first_block_buffer
= NULL
;
759 char *root_first_block_addr
= NULL
;
760 struct ext2_dirent
*dir
= NULL
;
761 struct ext2_dirent
*previous_dir
= NULL
;
763 struct ext_filesystem
*fs
= get_fs();
766 /* get the first block of root */
767 first_block_no_of_root
= blknr
;
768 root_first_block_buffer
= zalloc(fs
->blksz
);
769 if (!root_first_block_buffer
)
771 root_first_block_addr
= root_first_block_buffer
;
772 status
= ext4fs_devread((lbaint_t
)first_block_no_of_root
*
774 fs
->blksz
, root_first_block_buffer
);
778 if (ext4fs_log_journal(root_first_block_buffer
, first_block_no_of_root
))
780 dir
= (struct ext2_dirent
*)root_first_block_buffer
;
783 while (dir
->direntlen
>= 0) {
785 * blocksize-totalbytes because last
786 * directory length i.e., *dir->direntlen
787 * is free availble space in the block that
788 * means it is a last entry of directory entry
790 if (strlen(filename
) == dir
->namelen
) {
791 if (strncmp(filename
, ptr
+ sizeof(struct ext2_dirent
),
792 dir
->namelen
) == 0) {
793 printf("file found deleting\n");
794 previous_dir
->direntlen
+= dir
->direntlen
;
795 inodeno
= dir
->inode
;
802 if (fs
->blksz
- totalbytes
== dir
->direntlen
)
805 /* traversing the each directory entry */
806 templength
= dir
->direntlen
;
807 totalbytes
= totalbytes
+ templength
;
809 dir
= (struct ext2_dirent
*)((char *)dir
+ templength
);
815 if (ext4fs_put_metadata(root_first_block_addr
,
816 first_block_no_of_root
))
821 free(root_first_block_buffer
);
826 int ext4fs_filename_check(char *filename
)
828 short direct_blk_idx
= 0;
832 /* read the block no allocated to a file */
833 for (direct_blk_idx
= 0; direct_blk_idx
< INDIRECT_BLOCKS
;
835 blknr
= read_allocated_block(g_parent_inode
, direct_blk_idx
);
838 inodeno
= check_filename(filename
, blknr
);
846 long int ext4fs_get_new_blk_no(void)
852 static int prev_bg_bitmap_index
= -1;
853 unsigned int blk_per_grp
= ext4fs_root
->sblock
.blocks_per_group
;
854 struct ext_filesystem
*fs
= get_fs();
855 char *journal_buffer
= zalloc(fs
->blksz
);
856 char *zero_buffer
= zalloc(fs
->blksz
);
857 if (!journal_buffer
|| !zero_buffer
)
859 struct ext2_block_group
*bgd
= (struct ext2_block_group
*)fs
->gdtable
;
861 if (fs
->first_pass_bbmap
== 0) {
862 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
863 if (bgd
[i
].free_blocks
) {
864 if (bgd
[i
].bg_flags
& EXT4_BG_BLOCK_UNINIT
) {
865 put_ext4(((uint64_t) ((uint64_t)bgd
[i
].block_id
*
866 (uint64_t)fs
->blksz
)),
867 zero_buffer
, fs
->blksz
);
870 bg_flags
& ~EXT4_BG_BLOCK_UNINIT
;
871 memcpy(fs
->blk_bmaps
[i
], zero_buffer
,
875 _get_new_blk_no(fs
->blk_bmaps
[i
]);
876 if (fs
->curr_blkno
== -1)
877 /* if block bitmap is completely fill */
879 fs
->curr_blkno
= fs
->curr_blkno
+
881 fs
->first_pass_bbmap
++;
882 bgd
[i
].free_blocks
--;
883 fs
->sb
->free_blocks
--;
884 status
= ext4fs_devread((lbaint_t
)
891 if (ext4fs_log_journal(journal_buffer
,
896 debug("no space left on block group %d\n", i
);
904 /* get the blockbitmap index respective to blockno */
905 bg_idx
= fs
->curr_blkno
/ blk_per_grp
;
906 if (fs
->blksz
== 1024) {
907 remainder
= fs
->curr_blkno
% blk_per_grp
;
913 * To skip completely filled block group bitmaps
914 * Optimize the block allocation
916 if (bg_idx
>= fs
->no_blkgrp
)
919 if (bgd
[bg_idx
].free_blocks
== 0) {
920 debug("block group %u is full. Skipping\n", bg_idx
);
921 fs
->curr_blkno
= fs
->curr_blkno
+ blk_per_grp
;
926 if (bgd
[bg_idx
].bg_flags
& EXT4_BG_BLOCK_UNINIT
) {
927 memset(zero_buffer
, '\0', fs
->blksz
);
928 put_ext4(((uint64_t) ((uint64_t)bgd
[bg_idx
].block_id
*
929 (uint64_t)fs
->blksz
)), zero_buffer
, fs
->blksz
);
930 memcpy(fs
->blk_bmaps
[bg_idx
], zero_buffer
, fs
->blksz
);
931 bgd
[bg_idx
].bg_flags
= bgd
[bg_idx
].bg_flags
&
932 ~EXT4_BG_BLOCK_UNINIT
;
935 if (ext4fs_set_block_bmap(fs
->curr_blkno
, fs
->blk_bmaps
[bg_idx
],
937 debug("going for restart for the block no %ld %u\n",
938 fs
->curr_blkno
, bg_idx
);
943 if (prev_bg_bitmap_index
!= bg_idx
) {
944 memset(journal_buffer
, '\0', fs
->blksz
);
945 status
= ext4fs_devread((lbaint_t
)bgd
[bg_idx
].block_id
947 0, fs
->blksz
, journal_buffer
);
950 if (ext4fs_log_journal(journal_buffer
,
951 bgd
[bg_idx
].block_id
))
954 prev_bg_bitmap_index
= bg_idx
;
956 bgd
[bg_idx
].free_blocks
--;
957 fs
->sb
->free_blocks
--;
961 free(journal_buffer
);
964 return fs
->curr_blkno
;
966 free(journal_buffer
);
972 int ext4fs_get_new_inode_no(void)
976 unsigned int ibmap_idx
;
977 static int prev_inode_bitmap_index
= -1;
978 unsigned int inodes_per_grp
= ext4fs_root
->sblock
.inodes_per_group
;
979 struct ext_filesystem
*fs
= get_fs();
980 char *journal_buffer
= zalloc(fs
->blksz
);
981 char *zero_buffer
= zalloc(fs
->blksz
);
982 if (!journal_buffer
|| !zero_buffer
)
984 struct ext2_block_group
*bgd
= (struct ext2_block_group
*)fs
->gdtable
;
986 if (fs
->first_pass_ibmap
== 0) {
987 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
988 if (bgd
[i
].free_inodes
) {
989 if (bgd
[i
].bg_itable_unused
!=
991 bgd
[i
].bg_itable_unused
=
993 if (bgd
[i
].bg_flags
& EXT4_BG_INODE_UNINIT
) {
995 ((uint64_t)bgd
[i
].inode_id
*
996 (uint64_t)fs
->blksz
)),
997 zero_buffer
, fs
->blksz
);
998 bgd
[i
].bg_flags
= bgd
[i
].bg_flags
&
999 ~EXT4_BG_INODE_UNINIT
;
1000 memcpy(fs
->inode_bmaps
[i
],
1001 zero_buffer
, fs
->blksz
);
1004 _get_new_inode_no(fs
->inode_bmaps
[i
]);
1005 if (fs
->curr_inode_no
== -1)
1006 /* if block bitmap is completely fill */
1008 fs
->curr_inode_no
= fs
->curr_inode_no
+
1009 (i
* inodes_per_grp
);
1010 fs
->first_pass_ibmap
++;
1011 bgd
[i
].free_inodes
--;
1012 bgd
[i
].bg_itable_unused
--;
1013 fs
->sb
->free_inodes
--;
1014 status
= ext4fs_devread((lbaint_t
)
1021 if (ext4fs_log_journal(journal_buffer
,
1026 debug("no inode left on block group %d\n", i
);
1031 fs
->curr_inode_no
++;
1032 /* get the blockbitmap index respective to blockno */
1033 ibmap_idx
= fs
->curr_inode_no
/ inodes_per_grp
;
1034 if (bgd
[ibmap_idx
].bg_flags
& EXT4_BG_INODE_UNINIT
) {
1035 memset(zero_buffer
, '\0', fs
->blksz
);
1036 put_ext4(((uint64_t) ((uint64_t)bgd
[ibmap_idx
].inode_id
*
1037 (uint64_t)fs
->blksz
)), zero_buffer
,
1039 bgd
[ibmap_idx
].bg_flags
=
1040 bgd
[ibmap_idx
].bg_flags
& ~EXT4_BG_INODE_UNINIT
;
1041 memcpy(fs
->inode_bmaps
[ibmap_idx
], zero_buffer
,
1045 if (ext4fs_set_inode_bmap(fs
->curr_inode_no
,
1046 fs
->inode_bmaps
[ibmap_idx
],
1048 debug("going for restart for the block no %d %u\n",
1049 fs
->curr_inode_no
, ibmap_idx
);
1053 /* journal backup */
1054 if (prev_inode_bitmap_index
!= ibmap_idx
) {
1055 memset(journal_buffer
, '\0', fs
->blksz
);
1056 status
= ext4fs_devread((lbaint_t
)
1057 bgd
[ibmap_idx
].inode_id
1059 0, fs
->blksz
, journal_buffer
);
1062 if (ext4fs_log_journal(journal_buffer
,
1063 bgd
[ibmap_idx
].inode_id
))
1065 prev_inode_bitmap_index
= ibmap_idx
;
1067 if (bgd
[ibmap_idx
].bg_itable_unused
!=
1068 bgd
[ibmap_idx
].free_inodes
)
1069 bgd
[ibmap_idx
].bg_itable_unused
=
1070 bgd
[ibmap_idx
].free_inodes
;
1071 bgd
[ibmap_idx
].free_inodes
--;
1072 bgd
[ibmap_idx
].bg_itable_unused
--;
1073 fs
->sb
->free_inodes
--;
1078 free(journal_buffer
);
1081 return fs
->curr_inode_no
;
1083 free(journal_buffer
);
1091 static void alloc_single_indirect_block(struct ext2_inode
*file_inode
,
1092 unsigned int *total_remaining_blocks
,
1093 unsigned int *no_blks_reqd
)
1097 long int actual_block_no
;
1098 long int si_blockno
;
1099 /* si :single indirect */
1100 unsigned int *si_buffer
= NULL
;
1101 unsigned int *si_start_addr
= NULL
;
1102 struct ext_filesystem
*fs
= get_fs();
1104 if (*total_remaining_blocks
!= 0) {
1105 si_buffer
= zalloc(fs
->blksz
);
1107 printf("No Memory\n");
1110 si_start_addr
= si_buffer
;
1111 si_blockno
= ext4fs_get_new_blk_no();
1112 if (si_blockno
== -1) {
1113 printf("no block left to assign\n");
1117 debug("SIPB %ld: %u\n", si_blockno
, *total_remaining_blocks
);
1119 status
= ext4fs_devread((lbaint_t
)si_blockno
* fs
->sect_perblk
,
1120 0, fs
->blksz
, (char *)si_buffer
);
1121 memset(si_buffer
, '\0', fs
->blksz
);
1125 for (i
= 0; i
< (fs
->blksz
/ sizeof(int)); i
++) {
1126 actual_block_no
= ext4fs_get_new_blk_no();
1127 if (actual_block_no
== -1) {
1128 printf("no block left to assign\n");
1131 *si_buffer
= actual_block_no
;
1132 debug("SIAB %u: %u\n", *si_buffer
,
1133 *total_remaining_blocks
);
1136 (*total_remaining_blocks
)--;
1137 if (*total_remaining_blocks
== 0)
1141 /* write the block to disk */
1142 put_ext4(((uint64_t) ((uint64_t)si_blockno
* (uint64_t)fs
->blksz
)),
1143 si_start_addr
, fs
->blksz
);
1144 file_inode
->b
.blocks
.indir_block
= si_blockno
;
1147 free(si_start_addr
);
1150 static void alloc_double_indirect_block(struct ext2_inode
*file_inode
,
1151 unsigned int *total_remaining_blocks
,
1152 unsigned int *no_blks_reqd
)
1157 long int actual_block_no
;
1158 /* di:double indirect */
1159 long int di_blockno_parent
;
1160 long int di_blockno_child
;
1161 unsigned int *di_parent_buffer
= NULL
;
1162 unsigned int *di_child_buff
= NULL
;
1163 unsigned int *di_block_start_addr
= NULL
;
1164 unsigned int *di_child_buff_start
= NULL
;
1165 struct ext_filesystem
*fs
= get_fs();
1167 if (*total_remaining_blocks
!= 0) {
1168 /* double indirect parent block connecting to inode */
1169 di_blockno_parent
= ext4fs_get_new_blk_no();
1170 if (di_blockno_parent
== -1) {
1171 printf("no block left to assign\n");
1174 di_parent_buffer
= zalloc(fs
->blksz
);
1175 if (!di_parent_buffer
)
1178 di_block_start_addr
= di_parent_buffer
;
1180 debug("DIPB %ld: %u\n", di_blockno_parent
,
1181 *total_remaining_blocks
);
1183 status
= ext4fs_devread((lbaint_t
)di_blockno_parent
*
1185 fs
->blksz
, (char *)di_parent_buffer
);
1188 printf("%s: Device read error!\n", __func__
);
1191 memset(di_parent_buffer
, '\0', fs
->blksz
);
1194 * start:for each double indirect parent
1195 * block create one more block
1197 for (i
= 0; i
< (fs
->blksz
/ sizeof(int)); i
++) {
1198 di_blockno_child
= ext4fs_get_new_blk_no();
1199 if (di_blockno_child
== -1) {
1200 printf("no block left to assign\n");
1203 di_child_buff
= zalloc(fs
->blksz
);
1207 di_child_buff_start
= di_child_buff
;
1208 *di_parent_buffer
= di_blockno_child
;
1211 debug("DICB %ld: %u\n", di_blockno_child
,
1212 *total_remaining_blocks
);
1214 status
= ext4fs_devread((lbaint_t
)di_blockno_child
*
1217 (char *)di_child_buff
);
1220 printf("%s: Device read error!\n", __func__
);
1223 memset(di_child_buff
, '\0', fs
->blksz
);
1224 /* filling of actual datablocks for each child */
1225 for (j
= 0; j
< (fs
->blksz
/ sizeof(int)); j
++) {
1226 actual_block_no
= ext4fs_get_new_blk_no();
1227 if (actual_block_no
== -1) {
1228 printf("no block left to assign\n");
1231 *di_child_buff
= actual_block_no
;
1232 debug("DIAB %ld: %u\n", actual_block_no
,
1233 *total_remaining_blocks
);
1236 (*total_remaining_blocks
)--;
1237 if (*total_remaining_blocks
== 0)
1240 /* write the block table */
1241 put_ext4(((uint64_t) ((uint64_t)di_blockno_child
* (uint64_t)fs
->blksz
)),
1242 di_child_buff_start
, fs
->blksz
);
1243 free(di_child_buff_start
);
1244 di_child_buff_start
= NULL
;
1246 if (*total_remaining_blocks
== 0)
1249 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent
* (uint64_t)fs
->blksz
)),
1250 di_block_start_addr
, fs
->blksz
);
1251 file_inode
->b
.blocks
.double_indir_block
= di_blockno_parent
;
1254 free(di_block_start_addr
);
1257 static void alloc_triple_indirect_block(struct ext2_inode
*file_inode
,
1258 unsigned int *total_remaining_blocks
,
1259 unsigned int *no_blks_reqd
)
1264 long int actual_block_no
;
1265 /* ti: Triple Indirect */
1266 long int ti_gp_blockno
;
1267 long int ti_parent_blockno
;
1268 long int ti_child_blockno
;
1269 unsigned int *ti_gp_buff
= NULL
;
1270 unsigned int *ti_parent_buff
= NULL
;
1271 unsigned int *ti_child_buff
= NULL
;
1272 unsigned int *ti_gp_buff_start_addr
= NULL
;
1273 unsigned int *ti_pbuff_start_addr
= NULL
;
1274 unsigned int *ti_cbuff_start_addr
= NULL
;
1275 struct ext_filesystem
*fs
= get_fs();
1276 if (*total_remaining_blocks
!= 0) {
1277 /* triple indirect grand parent block connecting to inode */
1278 ti_gp_blockno
= ext4fs_get_new_blk_no();
1279 if (ti_gp_blockno
== -1) {
1280 printf("no block left to assign\n");
1283 ti_gp_buff
= zalloc(fs
->blksz
);
1287 ti_gp_buff_start_addr
= ti_gp_buff
;
1289 debug("TIGPB %ld: %u\n", ti_gp_blockno
,
1290 *total_remaining_blocks
);
1292 /* for each 4 byte grand parent entry create one more block */
1293 for (i
= 0; i
< (fs
->blksz
/ sizeof(int)); i
++) {
1294 ti_parent_blockno
= ext4fs_get_new_blk_no();
1295 if (ti_parent_blockno
== -1) {
1296 printf("no block left to assign\n");
1299 ti_parent_buff
= zalloc(fs
->blksz
);
1300 if (!ti_parent_buff
)
1303 ti_pbuff_start_addr
= ti_parent_buff
;
1304 *ti_gp_buff
= ti_parent_blockno
;
1307 debug("TIPB %ld: %u\n", ti_parent_blockno
,
1308 *total_remaining_blocks
);
1310 /* for each 4 byte entry parent create one more block */
1311 for (j
= 0; j
< (fs
->blksz
/ sizeof(int)); j
++) {
1312 ti_child_blockno
= ext4fs_get_new_blk_no();
1313 if (ti_child_blockno
== -1) {
1314 printf("no block left assign\n");
1317 ti_child_buff
= zalloc(fs
->blksz
);
1321 ti_cbuff_start_addr
= ti_child_buff
;
1322 *ti_parent_buff
= ti_child_blockno
;
1325 debug("TICB %ld: %u\n", ti_parent_blockno
,
1326 *total_remaining_blocks
);
1328 /* fill actual datablocks for each child */
1329 for (k
= 0; k
< (fs
->blksz
/ sizeof(int));
1332 ext4fs_get_new_blk_no();
1333 if (actual_block_no
== -1) {
1334 printf("no block left\n");
1335 free(ti_cbuff_start_addr
);
1338 *ti_child_buff
= actual_block_no
;
1339 debug("TIAB %ld: %u\n", actual_block_no
,
1340 *total_remaining_blocks
);
1343 (*total_remaining_blocks
)--;
1344 if (*total_remaining_blocks
== 0)
1347 /* write the child block */
1348 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno
*
1349 (uint64_t)fs
->blksz
)),
1350 ti_cbuff_start_addr
, fs
->blksz
);
1351 free(ti_cbuff_start_addr
);
1353 if (*total_remaining_blocks
== 0)
1356 /* write the parent block */
1357 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno
* (uint64_t)fs
->blksz
)),
1358 ti_pbuff_start_addr
, fs
->blksz
);
1359 free(ti_pbuff_start_addr
);
1361 if (*total_remaining_blocks
== 0)
1364 /* write the grand parent block */
1365 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno
* (uint64_t)fs
->blksz
)),
1366 ti_gp_buff_start_addr
, fs
->blksz
);
1367 file_inode
->b
.blocks
.triple_indir_block
= ti_gp_blockno
;
1368 free(ti_gp_buff_start_addr
);
1372 free(ti_pbuff_start_addr
);
1374 free(ti_gp_buff_start_addr
);
1377 void ext4fs_allocate_blocks(struct ext2_inode
*file_inode
,
1378 unsigned int total_remaining_blocks
,
1379 unsigned int *total_no_of_block
)
1382 long int direct_blockno
;
1383 unsigned int no_blks_reqd
= 0;
1385 /* allocation of direct blocks */
1386 for (i
= 0; total_remaining_blocks
&& i
< INDIRECT_BLOCKS
; i
++) {
1387 direct_blockno
= ext4fs_get_new_blk_no();
1388 if (direct_blockno
== -1) {
1389 printf("no block left to assign\n");
1392 file_inode
->b
.blocks
.dir_blocks
[i
] = direct_blockno
;
1393 debug("DB %ld: %u\n", direct_blockno
, total_remaining_blocks
);
1395 total_remaining_blocks
--;
1398 alloc_single_indirect_block(file_inode
, &total_remaining_blocks
,
1400 alloc_double_indirect_block(file_inode
, &total_remaining_blocks
,
1402 alloc_triple_indirect_block(file_inode
, &total_remaining_blocks
,
1404 *total_no_of_block
+= no_blks_reqd
;
1409 static struct ext4_extent_header
*ext4fs_get_extent_block
1410 (struct ext2_data
*data
, char *buf
,
1411 struct ext4_extent_header
*ext_block
,
1412 uint32_t fileblock
, int log2_blksz
)
1414 struct ext4_extent_idx
*index
;
1415 unsigned long long block
;
1416 int blksz
= EXT2_BLOCK_SIZE(data
);
1420 index
= (struct ext4_extent_idx
*)(ext_block
+ 1);
1422 if (le16_to_cpu(ext_block
->eh_magic
) != EXT4_EXT_MAGIC
)
1425 if (ext_block
->eh_depth
== 0)
1430 if (i
>= le16_to_cpu(ext_block
->eh_entries
))
1432 } while (fileblock
>= le32_to_cpu(index
[i
].ei_block
));
1437 block
= le16_to_cpu(index
[i
].ei_leaf_hi
);
1438 block
= (block
<< 32) + le32_to_cpu(index
[i
].ei_leaf_lo
);
1440 if (ext4fs_devread((lbaint_t
)block
<< log2_blksz
, 0, blksz
,
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
;
1453 int log2blksz
= get_fs()->dev_desc
->log2blksz
;
1455 desc_per_blk
= EXT2_BLOCK_SIZE(data
) / sizeof(struct ext2_block_group
);
1457 blkno
= __le32_to_cpu(data
->sblock
.first_data_block
) + 1 +
1458 group
/ desc_per_blk
;
1459 blkoff
= (group
% desc_per_blk
) * sizeof(struct ext2_block_group
);
1461 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1462 group
, blkno
, blkoff
);
1464 return ext4fs_devread((lbaint_t
)blkno
<<
1465 (LOG2_BLOCK_SIZE(data
) - log2blksz
),
1466 blkoff
, sizeof(struct ext2_block_group
),
1470 int ext4fs_read_inode(struct ext2_data
*data
, int ino
, struct ext2_inode
*inode
)
1472 struct ext2_block_group blkgrp
;
1473 struct ext2_sblock
*sblock
= &data
->sblock
;
1474 struct ext_filesystem
*fs
= get_fs();
1475 int log2blksz
= get_fs()->dev_desc
->log2blksz
;
1476 int inodes_per_block
, status
;
1478 unsigned int blkoff
;
1480 /* It is easier to calculate if the first inode is 0. */
1482 status
= ext4fs_blockgroup(data
, ino
/ __le32_to_cpu
1483 (sblock
->inodes_per_group
), &blkgrp
);
1487 inodes_per_block
= EXT2_BLOCK_SIZE(data
) / fs
->inodesz
;
1488 blkno
= __le32_to_cpu(blkgrp
.inode_table_id
) +
1489 (ino
% __le32_to_cpu(sblock
->inodes_per_group
)) / inodes_per_block
;
1490 blkoff
= (ino
% inodes_per_block
) * fs
->inodesz
;
1491 /* Read the inode. */
1492 status
= ext4fs_devread((lbaint_t
)blkno
<< (LOG2_BLOCK_SIZE(data
) -
1494 sizeof(struct ext2_inode
), (char *)inode
);
1501 long int read_allocated_block(struct ext2_inode
*inode
, int fileblock
)
1508 long int perblock_parent
;
1509 long int perblock_child
;
1510 unsigned long long start
;
1511 /* get the blocksize of the filesystem */
1512 blksz
= EXT2_BLOCK_SIZE(ext4fs_root
);
1513 log2_blksz
= LOG2_BLOCK_SIZE(ext4fs_root
)
1514 - get_fs()->dev_desc
->log2blksz
;
1516 if (le32_to_cpu(inode
->flags
) & EXT4_EXTENTS_FL
) {
1517 char *buf
= zalloc(blksz
);
1520 struct ext4_extent_header
*ext_block
;
1521 struct ext4_extent
*extent
;
1524 ext4fs_get_extent_block(ext4fs_root
, buf
,
1525 (struct ext4_extent_header
*)
1526 inode
->b
.blocks
.dir_blocks
,
1527 fileblock
, log2_blksz
);
1529 printf("invalid extent block\n");
1534 extent
= (struct ext4_extent
*)(ext_block
+ 1);
1538 if (i
>= le16_to_cpu(ext_block
->eh_entries
))
1540 } while (fileblock
>= le32_to_cpu(extent
[i
].ee_block
));
1542 fileblock
-= le32_to_cpu(extent
[i
].ee_block
);
1543 if (fileblock
>= le16_to_cpu(extent
[i
].ee_len
)) {
1548 start
= le16_to_cpu(extent
[i
].ee_start_hi
);
1549 start
= (start
<< 32) +
1550 le32_to_cpu(extent
[i
].ee_start_lo
);
1552 return fileblock
+ start
;
1555 printf("Extent Error\n");
1560 /* Direct blocks. */
1561 if (fileblock
< INDIRECT_BLOCKS
)
1562 blknr
= __le32_to_cpu(inode
->b
.blocks
.dir_blocks
[fileblock
]);
1565 else if (fileblock
< (INDIRECT_BLOCKS
+ (blksz
/ 4))) {
1566 if (ext4fs_indir1_block
== NULL
) {
1567 ext4fs_indir1_block
= zalloc(blksz
);
1568 if (ext4fs_indir1_block
== NULL
) {
1569 printf("** SI ext2fs read block (indir 1)"
1570 "malloc failed. **\n");
1573 ext4fs_indir1_size
= blksz
;
1574 ext4fs_indir1_blkno
= -1;
1576 if (blksz
!= ext4fs_indir1_size
) {
1577 free(ext4fs_indir1_block
);
1578 ext4fs_indir1_block
= NULL
;
1579 ext4fs_indir1_size
= 0;
1580 ext4fs_indir1_blkno
= -1;
1581 ext4fs_indir1_block
= zalloc(blksz
);
1582 if (ext4fs_indir1_block
== NULL
) {
1583 printf("** SI ext2fs read block (indir 1):"
1584 "malloc failed. **\n");
1587 ext4fs_indir1_size
= blksz
;
1589 if ((__le32_to_cpu(inode
->b
.blocks
.indir_block
) <<
1590 log2_blksz
) != ext4fs_indir1_blkno
) {
1592 ext4fs_devread((lbaint_t
)__le32_to_cpu
1594 indir_block
) << log2_blksz
, 0,
1595 blksz
, (char *)ext4fs_indir1_block
);
1597 printf("** SI ext2fs read block (indir 1)"
1601 ext4fs_indir1_blkno
=
1602 __le32_to_cpu(inode
->b
.blocks
.
1603 indir_block
) << log2_blksz
;
1605 blknr
= __le32_to_cpu(ext4fs_indir1_block
1606 [fileblock
- INDIRECT_BLOCKS
]);
1608 /* Double indirect. */
1609 else if (fileblock
< (INDIRECT_BLOCKS
+ (blksz
/ 4 *
1610 (blksz
/ 4 + 1)))) {
1612 long int perblock
= blksz
/ 4;
1613 long int rblock
= fileblock
- (INDIRECT_BLOCKS
+ blksz
/ 4);
1615 if (ext4fs_indir1_block
== NULL
) {
1616 ext4fs_indir1_block
= zalloc(blksz
);
1617 if (ext4fs_indir1_block
== NULL
) {
1618 printf("** DI ext2fs read block (indir 2 1)"
1619 "malloc failed. **\n");
1622 ext4fs_indir1_size
= blksz
;
1623 ext4fs_indir1_blkno
= -1;
1625 if (blksz
!= ext4fs_indir1_size
) {
1626 free(ext4fs_indir1_block
);
1627 ext4fs_indir1_block
= NULL
;
1628 ext4fs_indir1_size
= 0;
1629 ext4fs_indir1_blkno
= -1;
1630 ext4fs_indir1_block
= zalloc(blksz
);
1631 if (ext4fs_indir1_block
== NULL
) {
1632 printf("** DI ext2fs read block (indir 2 1)"
1633 "malloc failed. **\n");
1636 ext4fs_indir1_size
= blksz
;
1638 if ((__le32_to_cpu(inode
->b
.blocks
.double_indir_block
) <<
1639 log2_blksz
) != ext4fs_indir1_blkno
) {
1641 ext4fs_devread((lbaint_t
)__le32_to_cpu
1643 double_indir_block
) << log2_blksz
,
1645 (char *)ext4fs_indir1_block
);
1647 printf("** DI ext2fs read block (indir 2 1)"
1651 ext4fs_indir1_blkno
=
1652 __le32_to_cpu(inode
->b
.blocks
.double_indir_block
) <<
1656 if (ext4fs_indir2_block
== NULL
) {
1657 ext4fs_indir2_block
= zalloc(blksz
);
1658 if (ext4fs_indir2_block
== NULL
) {
1659 printf("** DI ext2fs read block (indir 2 2)"
1660 "malloc failed. **\n");
1663 ext4fs_indir2_size
= blksz
;
1664 ext4fs_indir2_blkno
= -1;
1666 if (blksz
!= ext4fs_indir2_size
) {
1667 free(ext4fs_indir2_block
);
1668 ext4fs_indir2_block
= NULL
;
1669 ext4fs_indir2_size
= 0;
1670 ext4fs_indir2_blkno
= -1;
1671 ext4fs_indir2_block
= zalloc(blksz
);
1672 if (ext4fs_indir2_block
== NULL
) {
1673 printf("** DI ext2fs read block (indir 2 2)"
1674 "malloc failed. **\n");
1677 ext4fs_indir2_size
= blksz
;
1679 if ((__le32_to_cpu(ext4fs_indir1_block
[rblock
/ perblock
]) <<
1680 log2_blksz
) != ext4fs_indir2_blkno
) {
1681 status
= ext4fs_devread((lbaint_t
)__le32_to_cpu
1682 (ext4fs_indir1_block
1684 perblock
]) << log2_blksz
, 0,
1686 (char *)ext4fs_indir2_block
);
1688 printf("** DI ext2fs read block (indir 2 2)"
1692 ext4fs_indir2_blkno
=
1693 __le32_to_cpu(ext4fs_indir1_block
[rblock
1698 blknr
= __le32_to_cpu(ext4fs_indir2_block
[rblock
% perblock
]);
1700 /* Tripple indirect. */
1702 rblock
= fileblock
- (INDIRECT_BLOCKS
+ blksz
/ 4 +
1703 (blksz
/ 4 * blksz
/ 4));
1704 perblock_child
= blksz
/ 4;
1705 perblock_parent
= ((blksz
/ 4) * (blksz
/ 4));
1707 if (ext4fs_indir1_block
== NULL
) {
1708 ext4fs_indir1_block
= zalloc(blksz
);
1709 if (ext4fs_indir1_block
== NULL
) {
1710 printf("** TI ext2fs read block (indir 2 1)"
1711 "malloc failed. **\n");
1714 ext4fs_indir1_size
= blksz
;
1715 ext4fs_indir1_blkno
= -1;
1717 if (blksz
!= ext4fs_indir1_size
) {
1718 free(ext4fs_indir1_block
);
1719 ext4fs_indir1_block
= NULL
;
1720 ext4fs_indir1_size
= 0;
1721 ext4fs_indir1_blkno
= -1;
1722 ext4fs_indir1_block
= zalloc(blksz
);
1723 if (ext4fs_indir1_block
== NULL
) {
1724 printf("** TI ext2fs read block (indir 2 1)"
1725 "malloc failed. **\n");
1728 ext4fs_indir1_size
= blksz
;
1730 if ((__le32_to_cpu(inode
->b
.blocks
.triple_indir_block
) <<
1731 log2_blksz
) != ext4fs_indir1_blkno
) {
1732 status
= ext4fs_devread
1734 __le32_to_cpu(inode
->b
.blocks
.triple_indir_block
)
1735 << log2_blksz
, 0, blksz
,
1736 (char *)ext4fs_indir1_block
);
1738 printf("** TI ext2fs read block (indir 2 1)"
1742 ext4fs_indir1_blkno
=
1743 __le32_to_cpu(inode
->b
.blocks
.triple_indir_block
) <<
1747 if (ext4fs_indir2_block
== NULL
) {
1748 ext4fs_indir2_block
= zalloc(blksz
);
1749 if (ext4fs_indir2_block
== NULL
) {
1750 printf("** TI ext2fs read block (indir 2 2)"
1751 "malloc failed. **\n");
1754 ext4fs_indir2_size
= blksz
;
1755 ext4fs_indir2_blkno
= -1;
1757 if (blksz
!= ext4fs_indir2_size
) {
1758 free(ext4fs_indir2_block
);
1759 ext4fs_indir2_block
= NULL
;
1760 ext4fs_indir2_size
= 0;
1761 ext4fs_indir2_blkno
= -1;
1762 ext4fs_indir2_block
= zalloc(blksz
);
1763 if (ext4fs_indir2_block
== NULL
) {
1764 printf("** TI ext2fs read block (indir 2 2)"
1765 "malloc failed. **\n");
1768 ext4fs_indir2_size
= blksz
;
1770 if ((__le32_to_cpu(ext4fs_indir1_block
[rblock
/
1771 perblock_parent
]) <<
1773 != ext4fs_indir2_blkno
) {
1774 status
= ext4fs_devread((lbaint_t
)__le32_to_cpu
1775 (ext4fs_indir1_block
1777 perblock_parent
]) <<
1778 log2_blksz
, 0, blksz
,
1779 (char *)ext4fs_indir2_block
);
1781 printf("** TI ext2fs read block (indir 2 2)"
1785 ext4fs_indir2_blkno
=
1786 __le32_to_cpu(ext4fs_indir1_block
[rblock
/
1791 if (ext4fs_indir3_block
== NULL
) {
1792 ext4fs_indir3_block
= zalloc(blksz
);
1793 if (ext4fs_indir3_block
== NULL
) {
1794 printf("** TI ext2fs read block (indir 2 2)"
1795 "malloc failed. **\n");
1798 ext4fs_indir3_size
= blksz
;
1799 ext4fs_indir3_blkno
= -1;
1801 if (blksz
!= ext4fs_indir3_size
) {
1802 free(ext4fs_indir3_block
);
1803 ext4fs_indir3_block
= NULL
;
1804 ext4fs_indir3_size
= 0;
1805 ext4fs_indir3_blkno
= -1;
1806 ext4fs_indir3_block
= zalloc(blksz
);
1807 if (ext4fs_indir3_block
== NULL
) {
1808 printf("** TI ext2fs read block (indir 2 2)"
1809 "malloc failed. **\n");
1812 ext4fs_indir3_size
= blksz
;
1814 if ((__le32_to_cpu(ext4fs_indir2_block
[rblock
1817 log2_blksz
) != ext4fs_indir3_blkno
) {
1819 ext4fs_devread((lbaint_t
)__le32_to_cpu
1820 (ext4fs_indir2_block
1821 [(rblock
/ perblock_child
)
1822 % (blksz
/ 4)]) << log2_blksz
, 0,
1823 blksz
, (char *)ext4fs_indir3_block
);
1825 printf("** TI ext2fs read block (indir 2 2)"
1829 ext4fs_indir3_blkno
=
1830 __le32_to_cpu(ext4fs_indir2_block
[(rblock
/
1837 blknr
= __le32_to_cpu(ext4fs_indir3_block
1838 [rblock
% perblock_child
]);
1840 debug("read_allocated_block %ld\n", blknr
);
1846 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1849 * This function assures that for a file with the same name but different size
1850 * the sequential store on the ext4 filesystem will be correct.
1852 * In this function the global data, responsible for internal representation
1853 * of the ext4 data are initialized to the reset state. Without this, during
1854 * replacement of the smaller file with the bigger truncation of new file was
1857 void ext4fs_reinit_global(void)
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;
1878 void ext4fs_close(void)
1880 if ((ext4fs_file
!= NULL
) && (ext4fs_root
!= NULL
)) {
1881 ext4fs_free_node(ext4fs_file
, &ext4fs_root
->diropen
);
1884 if (ext4fs_root
!= NULL
) {
1889 ext4fs_reinit_global();
1892 int ext4fs_iterate_dir(struct ext2fs_node
*dir
, char *name
,
1893 struct ext2fs_node
**fnode
, int *ftype
)
1895 unsigned int fpos
= 0;
1898 struct ext2fs_node
*diro
= (struct ext2fs_node
*) dir
;
1902 printf("Iterate dir %s\n", name
);
1903 #endif /* of DEBUG */
1904 if (!diro
->inode_read
) {
1905 status
= ext4fs_read_inode(diro
->data
, diro
->ino
, &diro
->inode
);
1909 /* Search the file. */
1910 while (fpos
< __le32_to_cpu(diro
->inode
.size
)) {
1911 struct ext2_dirent dirent
;
1913 status
= ext4fs_read_file(diro
, fpos
,
1914 sizeof(struct ext2_dirent
),
1915 (char *)&dirent
, &actread
);
1919 if (dirent
.direntlen
== 0) {
1920 printf("Failed to iterate over directory %s\n", name
);
1924 if (dirent
.namelen
!= 0) {
1925 char filename
[dirent
.namelen
+ 1];
1926 struct ext2fs_node
*fdiro
;
1927 int type
= FILETYPE_UNKNOWN
;
1929 status
= ext4fs_read_file(diro
,
1931 sizeof(struct ext2_dirent
),
1932 dirent
.namelen
, filename
,
1937 fdiro
= zalloc(sizeof(struct ext2fs_node
));
1941 fdiro
->data
= diro
->data
;
1942 fdiro
->ino
= __le32_to_cpu(dirent
.inode
);
1944 filename
[dirent
.namelen
] = '\0';
1946 if (dirent
.filetype
!= FILETYPE_UNKNOWN
) {
1947 fdiro
->inode_read
= 0;
1949 if (dirent
.filetype
== FILETYPE_DIRECTORY
)
1950 type
= FILETYPE_DIRECTORY
;
1951 else if (dirent
.filetype
== FILETYPE_SYMLINK
)
1952 type
= FILETYPE_SYMLINK
;
1953 else if (dirent
.filetype
== FILETYPE_REG
)
1954 type
= FILETYPE_REG
;
1956 status
= ext4fs_read_inode(diro
->data
,
1964 fdiro
->inode_read
= 1;
1966 if ((__le16_to_cpu(fdiro
->inode
.mode
) &
1967 FILETYPE_INO_MASK
) ==
1968 FILETYPE_INO_DIRECTORY
) {
1969 type
= FILETYPE_DIRECTORY
;
1970 } else if ((__le16_to_cpu(fdiro
->inode
.mode
)
1971 & FILETYPE_INO_MASK
) ==
1972 FILETYPE_INO_SYMLINK
) {
1973 type
= FILETYPE_SYMLINK
;
1974 } else if ((__le16_to_cpu(fdiro
->inode
.mode
)
1975 & FILETYPE_INO_MASK
) ==
1977 type
= FILETYPE_REG
;
1981 printf("iterate >%s<\n", filename
);
1982 #endif /* of DEBUG */
1983 if ((name
!= NULL
) && (fnode
!= NULL
)
1984 && (ftype
!= NULL
)) {
1985 if (strcmp(filename
, name
) == 0) {
1991 if (fdiro
->inode_read
== 0) {
1992 status
= ext4fs_read_inode(diro
->data
,
2000 fdiro
->inode_read
= 1;
2003 case FILETYPE_DIRECTORY
:
2006 case FILETYPE_SYMLINK
:
2017 __le32_to_cpu(fdiro
->inode
.size
),
2022 fpos
+= __le16_to_cpu(dirent
.direntlen
);
2027 static char *ext4fs_read_symlink(struct ext2fs_node
*node
)
2030 struct ext2fs_node
*diro
= node
;
2034 if (!diro
->inode_read
) {
2035 status
= ext4fs_read_inode(diro
->data
, diro
->ino
, &diro
->inode
);
2039 symlink
= zalloc(__le32_to_cpu(diro
->inode
.size
) + 1);
2043 if (__le32_to_cpu(diro
->inode
.size
) < sizeof(diro
->inode
.b
.symlink
)) {
2044 strncpy(symlink
, diro
->inode
.b
.symlink
,
2045 __le32_to_cpu(diro
->inode
.size
));
2047 status
= ext4fs_read_file(diro
, 0,
2048 __le32_to_cpu(diro
->inode
.size
),
2050 if ((status
< 0) || (actread
== 0)) {
2055 symlink
[__le32_to_cpu(diro
->inode
.size
)] = '\0';
2059 static int ext4fs_find_file1(const char *currpath
,
2060 struct ext2fs_node
*currroot
,
2061 struct ext2fs_node
**currfound
, int *foundtype
)
2063 char fpath
[strlen(currpath
) + 1];
2067 int type
= FILETYPE_DIRECTORY
;
2068 struct ext2fs_node
*currnode
= currroot
;
2069 struct ext2fs_node
*oldnode
= currroot
;
2071 strncpy(fpath
, currpath
, strlen(currpath
) + 1);
2073 /* Remove all leading slashes. */
2074 while (*name
== '/')
2078 *currfound
= currnode
;
2085 /* Extract the actual part from the pathname. */
2086 next
= strchr(name
, '/');
2088 /* Remove all leading slashes. */
2089 while (*next
== '/')
2093 if (type
!= FILETYPE_DIRECTORY
) {
2094 ext4fs_free_node(currnode
, currroot
);
2100 /* Iterate over the directory. */
2101 found
= ext4fs_iterate_dir(currnode
, name
, &currnode
, &type
);
2108 /* Read in the symlink and follow it. */
2109 if (type
== FILETYPE_SYMLINK
) {
2112 /* Test if the symlink does not loop. */
2113 if (++symlinknest
== 8) {
2114 ext4fs_free_node(currnode
, currroot
);
2115 ext4fs_free_node(oldnode
, currroot
);
2119 symlink
= ext4fs_read_symlink(currnode
);
2120 ext4fs_free_node(currnode
, currroot
);
2123 ext4fs_free_node(oldnode
, currroot
);
2127 debug("Got symlink >%s<\n", symlink
);
2129 if (symlink
[0] == '/') {
2130 ext4fs_free_node(oldnode
, currroot
);
2131 oldnode
= &ext4fs_root
->diropen
;
2134 /* Lookup the node the symlink points to. */
2135 status
= ext4fs_find_file1(symlink
, oldnode
,
2141 ext4fs_free_node(oldnode
, currroot
);
2146 ext4fs_free_node(oldnode
, currroot
);
2148 /* Found the node! */
2149 if (!next
|| *next
== '\0') {
2150 *currfound
= currnode
;
2159 int ext4fs_find_file(const char *path
, struct ext2fs_node
*rootnode
,
2160 struct ext2fs_node
**foundnode
, int expecttype
)
2163 int foundtype
= FILETYPE_DIRECTORY
;
2169 status
= ext4fs_find_file1(path
, rootnode
, foundnode
, &foundtype
);
2173 /* Check if the node that was found was of the expected type. */
2174 if ((expecttype
== FILETYPE_REG
) && (foundtype
!= expecttype
))
2176 else if ((expecttype
== FILETYPE_DIRECTORY
)
2177 && (foundtype
!= expecttype
))
2183 int ext4fs_open(const char *filename
, loff_t
*len
)
2185 struct ext2fs_node
*fdiro
= NULL
;
2188 if (ext4fs_root
== NULL
)
2192 status
= ext4fs_find_file(filename
, &ext4fs_root
->diropen
, &fdiro
,
2197 if (!fdiro
->inode_read
) {
2198 status
= ext4fs_read_inode(fdiro
->data
, fdiro
->ino
,
2203 *len
= __le32_to_cpu(fdiro
->inode
.size
);
2204 ext4fs_file
= fdiro
;
2208 ext4fs_free_node(fdiro
, &ext4fs_root
->diropen
);
2213 int ext4fs_mount(unsigned part_length
)
2215 struct ext2_data
*data
;
2217 struct ext_filesystem
*fs
= get_fs();
2218 data
= zalloc(SUPERBLOCK_SIZE
);
2222 /* Read the superblock. */
2223 status
= ext4_read_superblock((char *)&data
->sblock
);
2228 /* Make sure this is an ext2 filesystem. */
2229 if (__le16_to_cpu(data
->sblock
.magic
) != EXT2_MAGIC
)
2233 * The 64bit feature was enabled when metadata_csum was enabled
2234 * and we do not support metadata_csum (and cannot reliably find
2235 * files when it is set. Refuse to mount.
2237 if (data
->sblock
.feature_incompat
& EXT4_FEATURE_INCOMPAT_64BIT
) {
2238 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2242 if (__le32_to_cpu(data
->sblock
.revision_level
== 0))
2245 fs
->inodesz
= __le16_to_cpu(data
->sblock
.inode_size
);
2247 debug("EXT2 rev %d, inode_size %d\n",
2248 __le32_to_cpu(data
->sblock
.revision_level
), fs
->inodesz
);
2250 data
->diropen
.data
= data
;
2251 data
->diropen
.ino
= 2;
2252 data
->diropen
.inode_read
= 1;
2253 data
->inode
= &data
->diropen
.inode
;
2255 status
= ext4fs_read_inode(data
, 2, data
->inode
);
2263 printf("Failed to mount ext2 filesystem...\n");