1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 - 2012 Samsung Electronics
4 * EXT4 filesystem implementation in Uboot by
5 * Uma Shankar <uma.shankar@samsung.com>
6 * Manjunatha C Achar <a.manjunatha@samsung.com>
8 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
15 * GRUB -- GRand Unified Bootloader
16 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
18 * ext4write : Based on generic ext4 protocol.
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 __le32 *ext4fs_indir1_block;
37 int ext4fs_indir1_size;
38 int ext4fs_indir1_blkno = -1;
39 __le32 *ext4fs_indir2_block;
40 int ext4fs_indir2_size;
41 int ext4fs_indir2_blkno = -1;
43 __le32 *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 struct ext2_block_group *ext4fs_get_group_descriptor
51 (const struct ext_filesystem *fs, uint32_t bg_idx)
53 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
56 static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
58 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
61 static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
63 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
64 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
67 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
68 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
71 static inline void ext4fs_bg_free_inodes_dec
72 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
74 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
76 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
79 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
81 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
84 static inline void ext4fs_bg_free_blocks_dec
85 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
87 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
89 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
92 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
94 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
97 static inline void ext4fs_bg_itable_unused_dec
98 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
100 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
101 if (fs->gdsize == 64)
102 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
105 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
106 if (fs->gdsize == 64)
107 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
110 uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
112 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
113 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
117 void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
119 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
120 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
123 uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
124 const struct ext_filesystem *fs)
126 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
127 if (fs->gdsize == 64)
128 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
133 uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
134 const struct ext_filesystem *fs)
136 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
137 if (fs->gdsize == 64)
138 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
142 static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
144 return le16_to_cpu(bg->bg_flags);
147 static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
150 bg->bg_flags = cpu_to_le16(flags);
153 /* Block number of the block bitmap */
154 uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
155 const struct ext_filesystem *fs)
157 uint64_t block_nr = le32_to_cpu(bg->block_id);
158 if (fs->gdsize == 64)
159 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
163 /* Block number of the inode bitmap */
164 uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
165 const struct ext_filesystem *fs)
167 uint64_t block_nr = le32_to_cpu(bg->inode_id);
168 if (fs->gdsize == 64)
169 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
174 /* Block number of the inode table */
175 uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
176 const struct ext_filesystem *fs)
178 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
179 if (fs->gdsize == 64)
181 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
185 #if defined(CONFIG_EXT4_WRITE)
186 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
188 uint32_t res = size / n;
195 void put_ext4(uint64_t off, const void *buf, uint32_t size)
199 unsigned char *temp_ptr = NULL;
200 struct ext_filesystem *fs = get_fs();
201 int log2blksz = fs->dev_desc->log2blksz;
202 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
204 startblock = off >> log2blksz;
205 startblock += part_offset;
206 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
208 if (fs->dev_desc == NULL)
211 if ((startblock + (size >> log2blksz)) >
212 (part_offset + fs->total_sect)) {
213 printf("part_offset is " LBAFU "\n", part_offset);
214 printf("total_sector is %llu\n", fs->total_sect);
215 printf("error: overflow occurs\n");
220 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
222 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
223 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
225 if (size >> log2blksz != 0) {
226 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
227 (unsigned long *)buf);
229 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
231 memcpy(temp_ptr, buf, size);
232 blk_dwrite(fs->dev_desc, startblock, 1,
233 (unsigned long *)sec_buf);
238 static int _get_new_inode_no(unsigned char *buffer)
240 struct ext_filesystem *fs = get_fs();
246 /* get the blocksize of the filesystem */
247 unsigned char *ptr = buffer;
248 while (*ptr == 255) {
251 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
255 for (j = 0; j < fs->blksz; j++) {
260 status = input & operand;
275 static int _get_new_blk_no(unsigned char *buffer)
280 unsigned char *ptr = buffer;
281 struct ext_filesystem *fs = get_fs();
283 while (*ptr == 255) {
286 if (count == (fs->blksz * 8))
290 if (fs->blksz == 1024)
293 for (i = 0; i <= 7; i++) {
295 if (*ptr & operand) {
306 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
308 int i, remainder, status;
309 unsigned char *ptr = buffer;
310 unsigned char operand;
312 remainder = blockno % 8;
313 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
315 i = i - (index * blocksize);
316 if (blocksize != 1024) {
318 operand = 1 << remainder;
319 status = *ptr & operand;
323 *ptr = *ptr | operand;
326 if (remainder == 0) {
331 operand = (1 << (remainder - 1));
333 status = *ptr & operand;
337 *ptr = *ptr | operand;
342 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
344 int i, remainder, status;
345 unsigned char *ptr = buffer;
346 unsigned char operand;
348 remainder = blockno % 8;
349 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
351 i = i - (index * blocksize);
352 if (blocksize != 1024) {
354 operand = (1 << remainder);
355 status = *ptr & operand;
357 *ptr = *ptr & ~(operand);
359 if (remainder == 0) {
364 operand = (1 << (remainder - 1));
366 status = *ptr & operand;
368 *ptr = *ptr & ~(operand);
372 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
374 int i, remainder, status;
375 unsigned char *ptr = buffer;
376 unsigned char operand;
378 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
380 remainder = inode_no % 8;
381 if (remainder == 0) {
386 operand = (1 << (remainder - 1));
388 status = *ptr & operand;
392 *ptr = *ptr | operand;
397 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
399 int i, remainder, status;
400 unsigned char *ptr = buffer;
401 unsigned char operand;
403 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
405 remainder = inode_no % 8;
406 if (remainder == 0) {
411 operand = (1 << (remainder - 1));
413 status = *ptr & operand;
415 *ptr = *ptr & ~(operand);
418 uint16_t ext4fs_checksum_update(uint32_t i)
420 struct ext2_block_group *desc;
421 struct ext_filesystem *fs = get_fs();
423 __le32 le32_i = cpu_to_le32(i);
425 desc = ext4fs_get_group_descriptor(fs, i);
426 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
427 int offset = offsetof(struct ext2_block_group, bg_checksum);
429 crc = ext2fs_crc16(~0, fs->sb->unique_id,
430 sizeof(fs->sb->unique_id));
431 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
432 crc = ext2fs_crc16(crc, desc, offset);
433 offset += sizeof(desc->bg_checksum); /* skip checksum */
434 assert(offset == sizeof(*desc));
435 if (offset < fs->gdsize) {
436 crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
437 fs->gdsize - offset);
444 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
447 int sizeof_void_space;
448 int new_entry_byte_reqd;
449 short padding_factor = 0;
451 if (dir->namelen % 4 != 0)
452 padding_factor = 4 - (dir->namelen % 4);
454 dentry_length = sizeof(struct ext2_dirent) +
455 dir->namelen + padding_factor;
456 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
457 if (sizeof_void_space == 0)
461 if (strlen(filename) % 4 != 0)
462 padding_factor = 4 - (strlen(filename) % 4);
464 new_entry_byte_reqd = strlen(filename) +
465 sizeof(struct ext2_dirent) + padding_factor;
466 if (sizeof_void_space >= new_entry_byte_reqd) {
467 dir->direntlen = cpu_to_le16(dentry_length);
468 return sizeof_void_space;
474 int ext4fs_update_parent_dentry(char *filename, int file_type)
476 unsigned int *zero_buffer = NULL;
477 char *root_first_block_buffer = NULL;
479 long int first_block_no_of_root = 0;
481 unsigned int new_entry_byte_reqd;
482 int sizeof_void_space = 0;
486 struct ext_filesystem *fs = get_fs();
487 /* directory entry */
488 struct ext2_dirent *dir;
489 char *temp_dir = NULL;
492 uint32_t new_blockcnt;
493 uint32_t directory_blocks;
495 zero_buffer = zalloc(fs->blksz);
497 printf("No Memory\n");
500 root_first_block_buffer = zalloc(fs->blksz);
501 if (!root_first_block_buffer) {
503 printf("No Memory\n");
506 new_entry_byte_reqd = ROUND(strlen(filename) +
507 sizeof(struct ext2_dirent), 4);
509 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
510 LOG2_BLOCK_SIZE(ext4fs_root);
511 blk_idx = directory_blocks - 1;
514 /* read the block no allocated to a file */
515 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
517 if (first_block_no_of_root <= 0)
520 status = ext4fs_devread((lbaint_t)first_block_no_of_root
522 0, fs->blksz, root_first_block_buffer);
526 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
528 dir = (struct ext2_dirent *)root_first_block_buffer;
531 while (le16_to_cpu(dir->direntlen) > 0) {
532 unsigned short used_len = ROUND(dir->namelen +
533 sizeof(struct ext2_dirent), 4);
535 /* last entry of block */
536 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
538 /* check if new entry fits */
539 if ((used_len + new_entry_byte_reqd) <=
540 le16_to_cpu(dir->direntlen)) {
541 dir->direntlen = cpu_to_le16(used_len);
545 printf("Block full, trying previous\n");
549 printf("All blocks full: Allocate new\n");
551 if (le32_to_cpu(g_parent_inode->flags) &
553 printf("Directory uses extents\n");
556 if (directory_blocks >= INDIRECT_BLOCKS) {
557 printf("Directory exceeds limit\n");
560 new_blk_no = ext4fs_get_new_blk_no();
561 if (new_blk_no == -1) {
562 printf("no block left to assign\n");
565 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
566 g_parent_inode->b.blocks.
567 dir_blocks[directory_blocks] =
568 cpu_to_le32(new_blk_no);
570 new_size = le32_to_cpu(g_parent_inode->size);
571 new_size += fs->blksz;
572 g_parent_inode->size = cpu_to_le32(new_size);
574 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
575 new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE;
576 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
578 if (ext4fs_put_metadata
579 (root_first_block_buffer,
580 first_block_no_of_root))
586 templength = le16_to_cpu(dir->direntlen);
587 totalbytes = totalbytes + templength;
588 sizeof_void_space = check_void_in_dentry(dir, filename);
589 if (sizeof_void_space)
592 dir = (struct ext2_dirent *)((char *)dir + templength);
595 /* make a pointer ready for creating next directory entry */
596 templength = le16_to_cpu(dir->direntlen);
597 totalbytes = totalbytes + templength;
598 dir = (struct ext2_dirent *)((char *)dir + templength);
600 /* get the next available inode number */
601 inodeno = ext4fs_get_new_inode_no();
603 printf("no inode left to assign\n");
606 dir->inode = cpu_to_le32(inodeno);
607 if (sizeof_void_space)
608 dir->direntlen = cpu_to_le16(sizeof_void_space);
610 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
612 dir->namelen = strlen(filename);
613 dir->filetype = file_type;
614 temp_dir = (char *)dir;
615 temp_dir = temp_dir + sizeof(struct ext2_dirent);
616 memcpy(temp_dir, filename, strlen(filename));
618 /* update or write the 1st block of root inode */
619 if (ext4fs_put_metadata(root_first_block_buffer,
620 first_block_no_of_root))
625 free(root_first_block_buffer);
630 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
637 char *block_buffer = NULL;
638 struct ext2_dirent *dir = NULL;
639 struct ext_filesystem *fs = get_fs();
640 uint32_t directory_blocks;
643 directory_blocks = le32_to_cpu(parent_inode->size) >>
644 LOG2_BLOCK_SIZE(ext4fs_root);
646 block_buffer = zalloc(fs->blksz);
650 /* get the block no allocated to a file */
651 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
652 blknr = read_allocated_block(parent_inode, blk_idx, NULL);
656 /* read the directory block */
657 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
658 0, fs->blksz, (char *)block_buffer);
665 printf("Badly aligned ext2_dirent\n");
669 dir = (struct ext2_dirent *)(block_buffer + offset);
670 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
672 int direntlen = le16_to_cpu(dir->direntlen);
673 if (direntlen < sizeof(struct ext2_dirent))
676 if (dir->inode && (strlen(dirname) == dir->namelen) &&
677 (strncmp(dirname, direntname, dir->namelen) == 0)) {
678 inodeno = le32_to_cpu(dir->inode);
684 } while (offset < fs->blksz);
698 static int find_dir_depth(char *dirname)
700 char *token = strtok(dirname, "/");
702 while (token != NULL) {
703 token = strtok(NULL, "/");
706 return count + 1 + 1;
708 * for example for string /home/temp
709 * depth=home(1)+temp(1)+1 extra for NULL;
714 static int parse_path(char **arr, char *dirname)
716 char *token = strtok(dirname, "/");
720 arr[i] = zalloc(strlen("/") + 1);
723 memcpy(arr[i++], "/", strlen("/"));
725 /* add each path entry after root */
726 while (token != NULL) {
727 arr[i] = zalloc(strlen(token) + 1);
730 memcpy(arr[i++], token, strlen(token));
731 token = strtok(NULL, "/");
738 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
740 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
747 * Function: ext4fs_get_parent_inode_num
748 * Return Value: inode Number of the parent directory of file/Directory to be
750 * dirname : Input parmater, input path name of the file/directory to be created
751 * dname : Output parameter, to be filled with the name of the directory
752 * extracted from dirname
754 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
758 int matched_inode_no;
759 int result_inode_no = -1;
761 char *depth_dirname = NULL;
762 char *parse_dirname = NULL;
763 struct ext2_inode *parent_inode = NULL;
764 struct ext2_inode *first_inode = NULL;
765 struct ext2_inode temp_inode;
767 if (*dirname != '/') {
768 printf("Please supply Absolute path\n");
772 /* TODO: input validation make equivalent to linux */
773 depth_dirname = zalloc(strlen(dirname) + 1);
777 memcpy(depth_dirname, dirname, strlen(dirname));
778 depth = find_dir_depth(depth_dirname);
779 parse_dirname = zalloc(strlen(dirname) + 1);
782 memcpy(parse_dirname, dirname, strlen(dirname));
784 /* allocate memory for each directory level */
785 ptr = zalloc((depth) * sizeof(char *));
788 if (parse_path(ptr, parse_dirname))
790 parent_inode = zalloc(sizeof(struct ext2_inode));
793 first_inode = zalloc(sizeof(struct ext2_inode));
796 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
797 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
799 result_inode_no = EXT2_ROOT_INO;
800 for (i = 1; i < depth; i++) {
801 matched_inode_no = search_dir(parent_inode, ptr[i]);
802 if (matched_inode_no == -1) {
803 if (ptr[i + 1] == NULL && i == 1) {
804 result_inode_no = EXT2_ROOT_INO;
807 if (ptr[i + 1] == NULL)
809 printf("Invalid path\n");
810 result_inode_no = -1;
814 if (ptr[i + 1] != NULL) {
815 memset(parent_inode, '\0',
816 sizeof(struct ext2_inode));
817 if (ext4fs_iget(matched_inode_no,
819 result_inode_no = -1;
822 result_inode_no = matched_inode_no;
831 matched_inode_no = search_dir(first_inode, ptr[i]);
833 matched_inode_no = search_dir(parent_inode, ptr[i]);
835 if (matched_inode_no != -1) {
836 ext4fs_iget(matched_inode_no, &temp_inode);
837 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
838 printf("It is a Directory\n");
839 result_inode_no = -1;
844 if (strlen(ptr[i]) > 256) {
845 result_inode_no = -1;
848 memcpy(dname, ptr[i], strlen(ptr[i]));
853 for (i = 0; i < depth; i++) {
862 return result_inode_no;
865 static int unlink_filename(char *filename, unsigned int blknr)
870 char *block_buffer = NULL;
871 struct ext2_dirent *dir = NULL;
872 struct ext2_dirent *previous_dir;
873 struct ext_filesystem *fs = get_fs();
877 block_buffer = zalloc(fs->blksz);
881 /* read the directory block */
882 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
883 fs->blksz, block_buffer);
890 printf("Badly aligned ext2_dirent\n");
895 dir = (struct ext2_dirent *)(block_buffer + offset);
896 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
898 int direntlen = le16_to_cpu(dir->direntlen);
899 if (direntlen < sizeof(struct ext2_dirent))
902 if (dir->inode && (strlen(filename) == dir->namelen) &&
903 (strncmp(direntname, filename, dir->namelen) == 0)) {
904 inodeno = le32_to_cpu(dir->inode);
910 } while (offset < fs->blksz);
913 printf("file found, deleting\n");
914 if (ext4fs_log_journal(block_buffer, blknr))
918 /* merge dir entry with predecessor */
920 new_len = le16_to_cpu(previous_dir->direntlen);
921 new_len += le16_to_cpu(dir->direntlen);
922 previous_dir->direntlen = cpu_to_le16(new_len);
924 /* invalidate dir entry */
927 if (ext4fs_put_metadata(block_buffer, blknr))
937 int ext4fs_filename_unlink(char *filename)
942 uint32_t directory_blocks;
944 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
945 LOG2_BLOCK_SIZE(ext4fs_root);
947 /* read the block no allocated to a file */
948 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
949 blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
952 inodeno = unlink_filename(filename, blknr);
960 uint32_t ext4fs_get_new_blk_no(void)
966 static int prev_bg_bitmap_index = -1;
967 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
968 struct ext_filesystem *fs = get_fs();
969 char *journal_buffer = zalloc(fs->blksz);
970 char *zero_buffer = zalloc(fs->blksz);
971 if (!journal_buffer || !zero_buffer)
974 if (fs->first_pass_bbmap == 0) {
975 for (i = 0; i < fs->no_blkgrp; i++) {
976 struct ext2_block_group *bgd = NULL;
977 bgd = ext4fs_get_group_descriptor(fs, i);
978 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
979 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
980 uint64_t b_bitmap_blk =
981 ext4fs_bg_get_block_id(bgd, fs);
982 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
983 memcpy(fs->blk_bmaps[i], zero_buffer,
985 put_ext4(b_bitmap_blk * fs->blksz,
986 fs->blk_bmaps[i], fs->blksz);
987 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
988 ext4fs_bg_set_flags(bgd, bg_flags);
991 _get_new_blk_no(fs->blk_bmaps[i]);
992 if (fs->curr_blkno == -1)
993 /* block bitmap is completely filled */
995 fs->curr_blkno = fs->curr_blkno +
997 fs->first_pass_bbmap++;
998 ext4fs_bg_free_blocks_dec(bgd, fs);
999 ext4fs_sb_free_blocks_dec(fs->sb);
1000 status = ext4fs_devread(b_bitmap_blk *
1006 if (ext4fs_log_journal(journal_buffer,
1011 debug("no space left on block group %d\n", i);
1019 /* get the blockbitmap index respective to blockno */
1020 bg_idx = fs->curr_blkno / blk_per_grp;
1021 if (fs->blksz == 1024) {
1022 remainder = fs->curr_blkno % blk_per_grp;
1028 * To skip completely filled block group bitmaps
1029 * Optimize the block allocation
1031 if (bg_idx >= fs->no_blkgrp)
1034 struct ext2_block_group *bgd = NULL;
1035 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1036 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
1037 debug("block group %u is full. Skipping\n", bg_idx);
1038 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1039 if (fs->blksz == 1024)
1040 fs->curr_blkno += 1;
1044 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1045 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1046 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
1047 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1048 put_ext4(b_bitmap_blk * fs->blksz,
1049 zero_buffer, fs->blksz);
1050 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1051 ext4fs_bg_set_flags(bgd, bg_flags);
1054 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1056 debug("going for restart for the block no %ld %u\n",
1057 fs->curr_blkno, bg_idx);
1062 /* journal backup */
1063 if (prev_bg_bitmap_index != bg_idx) {
1064 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
1065 0, fs->blksz, journal_buffer);
1068 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
1071 prev_bg_bitmap_index = bg_idx;
1073 ext4fs_bg_free_blocks_dec(bgd, fs);
1074 ext4fs_sb_free_blocks_dec(fs->sb);
1078 free(journal_buffer);
1081 return fs->curr_blkno;
1083 free(journal_buffer);
1089 int ext4fs_get_new_inode_no(void)
1093 unsigned int ibmap_idx;
1094 static int prev_inode_bitmap_index = -1;
1095 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1096 struct ext_filesystem *fs = get_fs();
1097 char *journal_buffer = zalloc(fs->blksz);
1098 char *zero_buffer = zalloc(fs->blksz);
1099 if (!journal_buffer || !zero_buffer)
1101 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1102 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
1104 if (fs->first_pass_ibmap == 0) {
1105 for (i = 0; i < fs->no_blkgrp; i++) {
1106 uint32_t free_inodes;
1107 struct ext2_block_group *bgd = NULL;
1108 bgd = ext4fs_get_group_descriptor(fs, i);
1109 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1111 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1112 uint64_t i_bitmap_blk =
1113 ext4fs_bg_get_inode_id(bgd, fs);
1115 bgd->bg_itable_unused = free_inodes;
1116 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1117 put_ext4(i_bitmap_blk * fs->blksz,
1118 zero_buffer, fs->blksz);
1119 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1120 ext4fs_bg_set_flags(bgd, bg_flags);
1121 memcpy(fs->inode_bmaps[i],
1122 zero_buffer, fs->blksz);
1125 _get_new_inode_no(fs->inode_bmaps[i]);
1126 if (fs->curr_inode_no == -1)
1127 /* inode bitmap is completely filled */
1129 fs->curr_inode_no = fs->curr_inode_no +
1130 (i * inodes_per_grp);
1131 fs->first_pass_ibmap++;
1132 ext4fs_bg_free_inodes_dec(bgd, fs);
1134 ext4fs_bg_itable_unused_dec(bgd, fs);
1135 ext4fs_sb_free_inodes_dec(fs->sb);
1136 status = ext4fs_devread(i_bitmap_blk *
1142 if (ext4fs_log_journal(journal_buffer,
1147 debug("no inode left on block group %d\n", i);
1152 fs->curr_inode_no++;
1153 /* get the blockbitmap index respective to blockno */
1154 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1155 struct ext2_block_group *bgd =
1156 ext4fs_get_group_descriptor(fs, ibmap_idx);
1157 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1158 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1160 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1161 put_ext4(i_bitmap_blk * fs->blksz,
1162 zero_buffer, fs->blksz);
1163 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1164 ext4fs_bg_set_flags(bgd, bg_flags);
1165 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1169 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1170 fs->inode_bmaps[ibmap_idx],
1172 debug("going for restart for the block no %d %u\n",
1173 fs->curr_inode_no, ibmap_idx);
1177 /* journal backup */
1178 if (prev_inode_bitmap_index != ibmap_idx) {
1179 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
1180 0, fs->blksz, journal_buffer);
1183 if (ext4fs_log_journal(journal_buffer,
1184 le32_to_cpu(bgd->inode_id)))
1186 prev_inode_bitmap_index = ibmap_idx;
1188 ext4fs_bg_free_inodes_dec(bgd, fs);
1190 bgd->bg_itable_unused = bgd->free_inodes;
1191 ext4fs_sb_free_inodes_dec(fs->sb);
1196 free(journal_buffer);
1199 return fs->curr_inode_no;
1201 free(journal_buffer);
1209 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1210 unsigned int *total_remaining_blocks,
1211 unsigned int *no_blks_reqd)
1215 long int actual_block_no;
1216 long int si_blockno;
1217 /* si :single indirect */
1218 __le32 *si_buffer = NULL;
1219 __le32 *si_start_addr = NULL;
1220 struct ext_filesystem *fs = get_fs();
1222 if (*total_remaining_blocks != 0) {
1223 si_buffer = zalloc(fs->blksz);
1225 printf("No Memory\n");
1228 si_start_addr = si_buffer;
1229 si_blockno = ext4fs_get_new_blk_no();
1230 if (si_blockno == -1) {
1231 printf("no block left to assign\n");
1235 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1237 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1238 0, fs->blksz, (char *)si_buffer);
1239 memset(si_buffer, '\0', fs->blksz);
1243 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1244 actual_block_no = ext4fs_get_new_blk_no();
1245 if (actual_block_no == -1) {
1246 printf("no block left to assign\n");
1249 *si_buffer = cpu_to_le32(actual_block_no);
1250 debug("SIAB %u: %u\n", *si_buffer,
1251 *total_remaining_blocks);
1254 (*total_remaining_blocks)--;
1255 if (*total_remaining_blocks == 0)
1259 /* write the block to disk */
1260 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1261 si_start_addr, fs->blksz);
1262 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1265 free(si_start_addr);
1268 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1269 unsigned int *total_remaining_blocks,
1270 unsigned int *no_blks_reqd)
1275 long int actual_block_no;
1276 /* di:double indirect */
1277 long int di_blockno_parent;
1278 long int di_blockno_child;
1279 __le32 *di_parent_buffer = NULL;
1280 __le32 *di_child_buff = NULL;
1281 __le32 *di_block_start_addr = NULL;
1282 __le32 *di_child_buff_start = NULL;
1283 struct ext_filesystem *fs = get_fs();
1285 if (*total_remaining_blocks != 0) {
1286 /* double indirect parent block connecting to inode */
1287 di_blockno_parent = ext4fs_get_new_blk_no();
1288 if (di_blockno_parent == -1) {
1289 printf("no block left to assign\n");
1292 di_parent_buffer = zalloc(fs->blksz);
1293 if (!di_parent_buffer)
1296 di_block_start_addr = di_parent_buffer;
1298 debug("DIPB %ld: %u\n", di_blockno_parent,
1299 *total_remaining_blocks);
1301 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1303 fs->blksz, (char *)di_parent_buffer);
1306 printf("%s: Device read error!\n", __func__);
1309 memset(di_parent_buffer, '\0', fs->blksz);
1312 * start:for each double indirect parent
1313 * block create one more block
1315 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1316 di_blockno_child = ext4fs_get_new_blk_no();
1317 if (di_blockno_child == -1) {
1318 printf("no block left to assign\n");
1321 di_child_buff = zalloc(fs->blksz);
1325 di_child_buff_start = di_child_buff;
1326 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1329 debug("DICB %ld: %u\n", di_blockno_child,
1330 *total_remaining_blocks);
1332 status = ext4fs_devread((lbaint_t)di_blockno_child *
1335 (char *)di_child_buff);
1338 printf("%s: Device read error!\n", __func__);
1341 memset(di_child_buff, '\0', fs->blksz);
1342 /* filling of actual datablocks for each child */
1343 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1344 actual_block_no = ext4fs_get_new_blk_no();
1345 if (actual_block_no == -1) {
1346 printf("no block left to assign\n");
1349 *di_child_buff = cpu_to_le32(actual_block_no);
1350 debug("DIAB %ld: %u\n", actual_block_no,
1351 *total_remaining_blocks);
1354 (*total_remaining_blocks)--;
1355 if (*total_remaining_blocks == 0)
1358 /* write the block table */
1359 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1360 di_child_buff_start, fs->blksz);
1361 free(di_child_buff_start);
1362 di_child_buff_start = NULL;
1364 if (*total_remaining_blocks == 0)
1367 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1368 di_block_start_addr, fs->blksz);
1369 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1372 free(di_block_start_addr);
1375 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1376 unsigned int *total_remaining_blocks,
1377 unsigned int *no_blks_reqd)
1382 long int actual_block_no;
1383 /* ti: Triple Indirect */
1384 long int ti_gp_blockno;
1385 long int ti_parent_blockno;
1386 long int ti_child_blockno;
1387 __le32 *ti_gp_buff = NULL;
1388 __le32 *ti_parent_buff = NULL;
1389 __le32 *ti_child_buff = NULL;
1390 __le32 *ti_gp_buff_start_addr = NULL;
1391 __le32 *ti_pbuff_start_addr = NULL;
1392 __le32 *ti_cbuff_start_addr = NULL;
1393 struct ext_filesystem *fs = get_fs();
1394 if (*total_remaining_blocks != 0) {
1395 /* triple indirect grand parent block connecting to inode */
1396 ti_gp_blockno = ext4fs_get_new_blk_no();
1397 if (ti_gp_blockno == -1) {
1398 printf("no block left to assign\n");
1401 ti_gp_buff = zalloc(fs->blksz);
1405 ti_gp_buff_start_addr = ti_gp_buff;
1407 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1408 *total_remaining_blocks);
1410 /* for each 4 byte grand parent entry create one more block */
1411 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1412 ti_parent_blockno = ext4fs_get_new_blk_no();
1413 if (ti_parent_blockno == -1) {
1414 printf("no block left to assign\n");
1417 ti_parent_buff = zalloc(fs->blksz);
1418 if (!ti_parent_buff)
1421 ti_pbuff_start_addr = ti_parent_buff;
1422 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1425 debug("TIPB %ld: %u\n", ti_parent_blockno,
1426 *total_remaining_blocks);
1428 /* for each 4 byte entry parent create one more block */
1429 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1430 ti_child_blockno = ext4fs_get_new_blk_no();
1431 if (ti_child_blockno == -1) {
1432 printf("no block left assign\n");
1435 ti_child_buff = zalloc(fs->blksz);
1439 ti_cbuff_start_addr = ti_child_buff;
1440 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1443 debug("TICB %ld: %u\n", ti_parent_blockno,
1444 *total_remaining_blocks);
1446 /* fill actual datablocks for each child */
1447 for (k = 0; k < (fs->blksz / sizeof(int));
1450 ext4fs_get_new_blk_no();
1451 if (actual_block_no == -1) {
1452 printf("no block left\n");
1453 free(ti_cbuff_start_addr);
1456 *ti_child_buff = cpu_to_le32(actual_block_no);
1457 debug("TIAB %ld: %u\n", actual_block_no,
1458 *total_remaining_blocks);
1461 (*total_remaining_blocks)--;
1462 if (*total_remaining_blocks == 0)
1465 /* write the child block */
1466 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1467 (uint64_t)fs->blksz)),
1468 ti_cbuff_start_addr, fs->blksz);
1469 free(ti_cbuff_start_addr);
1471 if (*total_remaining_blocks == 0)
1474 /* write the parent block */
1475 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1476 ti_pbuff_start_addr, fs->blksz);
1477 free(ti_pbuff_start_addr);
1479 if (*total_remaining_blocks == 0)
1482 /* write the grand parent block */
1483 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1484 ti_gp_buff_start_addr, fs->blksz);
1485 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1486 free(ti_gp_buff_start_addr);
1490 free(ti_pbuff_start_addr);
1492 free(ti_gp_buff_start_addr);
1495 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1496 unsigned int total_remaining_blocks,
1497 unsigned int *total_no_of_block)
1500 long int direct_blockno;
1501 unsigned int no_blks_reqd = 0;
1503 /* allocation of direct blocks */
1504 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1505 direct_blockno = ext4fs_get_new_blk_no();
1506 if (direct_blockno == -1) {
1507 printf("no block left to assign\n");
1510 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1511 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1513 total_remaining_blocks--;
1516 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1518 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1520 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1522 *total_no_of_block += no_blks_reqd;
1527 static struct ext4_extent_header *ext4fs_get_extent_block
1528 (struct ext2_data *data, struct ext_block_cache *cache,
1529 struct ext4_extent_header *ext_block,
1530 uint32_t fileblock, int log2_blksz)
1532 struct ext4_extent_idx *index;
1533 unsigned long long block;
1534 int blksz = EXT2_BLOCK_SIZE(data);
1538 index = (struct ext4_extent_idx *)(ext_block + 1);
1540 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1543 if (ext_block->eh_depth == 0)
1548 if (i >= le16_to_cpu(ext_block->eh_entries))
1550 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1553 * If first logical block number is higher than requested fileblock,
1554 * it is a sparse file. This is handled on upper layer.
1559 block = le16_to_cpu(index[i].ei_leaf_hi);
1560 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1561 block <<= log2_blksz;
1562 if (!ext_cache_read(cache, (lbaint_t)block, blksz))
1564 ext_block = (struct ext4_extent_header *)cache->buf;
1568 static int ext4fs_blockgroup
1569 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1572 unsigned int blkoff, desc_per_blk;
1573 int log2blksz = get_fs()->dev_desc->log2blksz;
1574 int desc_size = get_fs()->gdsize;
1578 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
1580 if (desc_per_blk == 0)
1582 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1583 group / desc_per_blk;
1584 blkoff = (group % desc_per_blk) * desc_size;
1586 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1587 group, blkno, blkoff);
1589 return ext4fs_devread((lbaint_t)blkno <<
1590 (LOG2_BLOCK_SIZE(data) - log2blksz),
1591 blkoff, desc_size, (char *)blkgrp);
1594 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1596 struct ext2_block_group *blkgrp;
1597 struct ext2_sblock *sblock = &data->sblock;
1598 struct ext_filesystem *fs = get_fs();
1599 int log2blksz = get_fs()->dev_desc->log2blksz;
1600 int inodes_per_block, status;
1602 unsigned int blkoff;
1604 /* Allocate blkgrp based on gdsize (for 64-bit support). */
1605 blkgrp = zalloc(get_fs()->gdsize);
1609 /* It is easier to calculate if the first inode is 0. */
1611 if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
1615 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1616 (sblock->inodes_per_group), blkgrp);
1622 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1623 if ( inodes_per_block == 0 ) {
1627 blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
1628 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1629 blkoff = (ino % inodes_per_block) * fs->inodesz;
1631 /* Free blkgrp as it is no longer required. */
1634 /* Read the inode. */
1635 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1637 sizeof(struct ext2_inode), (char *)inode);
1644 long int read_allocated_block(struct ext2_inode *inode, int fileblock,
1645 struct ext_block_cache *cache)
1652 long int perblock_parent;
1653 long int perblock_child;
1654 unsigned long long start;
1655 /* get the blocksize of the filesystem */
1656 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1657 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1658 - get_fs()->dev_desc->log2blksz;
1660 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1661 long int startblock, endblock;
1662 struct ext_block_cache *c, cd;
1663 struct ext4_extent_header *ext_block;
1664 struct ext4_extent *extent;
1674 ext4fs_get_extent_block(ext4fs_root, c,
1675 (struct ext4_extent_header *)
1676 inode->b.blocks.dir_blocks,
1677 fileblock, log2_blksz);
1679 printf("invalid extent block\n");
1685 extent = (struct ext4_extent *)(ext_block + 1);
1687 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1688 startblock = le32_to_cpu(extent[i].ee_block);
1689 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1691 if (startblock > fileblock) {
1697 } else if (fileblock < endblock) {
1698 start = le16_to_cpu(extent[i].ee_start_hi);
1699 start = (start << 32) +
1700 le32_to_cpu(extent[i].ee_start_lo);
1703 return (fileblock - startblock) + start;
1712 /* Direct blocks. */
1713 if (fileblock < INDIRECT_BLOCKS)
1714 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1717 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1718 if (ext4fs_indir1_block == NULL) {
1719 ext4fs_indir1_block = zalloc(blksz);
1720 if (ext4fs_indir1_block == NULL) {
1721 printf("** SI ext2fs read block (indir 1)"
1722 "malloc failed. **\n");
1725 ext4fs_indir1_size = blksz;
1726 ext4fs_indir1_blkno = -1;
1728 if (blksz != ext4fs_indir1_size) {
1729 free(ext4fs_indir1_block);
1730 ext4fs_indir1_block = NULL;
1731 ext4fs_indir1_size = 0;
1732 ext4fs_indir1_blkno = -1;
1733 ext4fs_indir1_block = zalloc(blksz);
1734 if (ext4fs_indir1_block == NULL) {
1735 printf("** SI ext2fs read block (indir 1):"
1736 "malloc failed. **\n");
1739 ext4fs_indir1_size = blksz;
1741 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1742 log2_blksz) != ext4fs_indir1_blkno) {
1744 ext4fs_devread((lbaint_t)le32_to_cpu
1746 indir_block) << log2_blksz, 0,
1747 blksz, (char *)ext4fs_indir1_block);
1749 printf("** SI ext2fs read block (indir 1)"
1753 ext4fs_indir1_blkno =
1754 le32_to_cpu(inode->b.blocks.
1755 indir_block) << log2_blksz;
1757 blknr = le32_to_cpu(ext4fs_indir1_block
1758 [fileblock - INDIRECT_BLOCKS]);
1760 /* Double indirect. */
1761 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1762 (blksz / 4 + 1)))) {
1764 long int perblock = blksz / 4;
1765 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1767 if (ext4fs_indir1_block == NULL) {
1768 ext4fs_indir1_block = zalloc(blksz);
1769 if (ext4fs_indir1_block == NULL) {
1770 printf("** DI ext2fs read block (indir 2 1)"
1771 "malloc failed. **\n");
1774 ext4fs_indir1_size = blksz;
1775 ext4fs_indir1_blkno = -1;
1777 if (blksz != ext4fs_indir1_size) {
1778 free(ext4fs_indir1_block);
1779 ext4fs_indir1_block = NULL;
1780 ext4fs_indir1_size = 0;
1781 ext4fs_indir1_blkno = -1;
1782 ext4fs_indir1_block = zalloc(blksz);
1783 if (ext4fs_indir1_block == NULL) {
1784 printf("** DI ext2fs read block (indir 2 1)"
1785 "malloc failed. **\n");
1788 ext4fs_indir1_size = blksz;
1790 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1791 log2_blksz) != ext4fs_indir1_blkno) {
1793 ext4fs_devread((lbaint_t)le32_to_cpu
1795 double_indir_block) << log2_blksz,
1797 (char *)ext4fs_indir1_block);
1799 printf("** DI ext2fs read block (indir 2 1)"
1803 ext4fs_indir1_blkno =
1804 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1808 if (ext4fs_indir2_block == NULL) {
1809 ext4fs_indir2_block = zalloc(blksz);
1810 if (ext4fs_indir2_block == NULL) {
1811 printf("** DI ext2fs read block (indir 2 2)"
1812 "malloc failed. **\n");
1815 ext4fs_indir2_size = blksz;
1816 ext4fs_indir2_blkno = -1;
1818 if (blksz != ext4fs_indir2_size) {
1819 free(ext4fs_indir2_block);
1820 ext4fs_indir2_block = NULL;
1821 ext4fs_indir2_size = 0;
1822 ext4fs_indir2_blkno = -1;
1823 ext4fs_indir2_block = zalloc(blksz);
1824 if (ext4fs_indir2_block == NULL) {
1825 printf("** DI ext2fs read block (indir 2 2)"
1826 "malloc failed. **\n");
1829 ext4fs_indir2_size = blksz;
1831 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1832 log2_blksz) != ext4fs_indir2_blkno) {
1833 status = ext4fs_devread((lbaint_t)le32_to_cpu
1834 (ext4fs_indir1_block
1836 perblock]) << log2_blksz, 0,
1838 (char *)ext4fs_indir2_block);
1840 printf("** DI ext2fs read block (indir 2 2)"
1844 ext4fs_indir2_blkno =
1845 le32_to_cpu(ext4fs_indir1_block[rblock
1850 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1852 /* Tripple indirect. */
1854 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1855 (blksz / 4 * blksz / 4));
1856 perblock_child = blksz / 4;
1857 perblock_parent = ((blksz / 4) * (blksz / 4));
1859 if (ext4fs_indir1_block == NULL) {
1860 ext4fs_indir1_block = zalloc(blksz);
1861 if (ext4fs_indir1_block == NULL) {
1862 printf("** TI ext2fs read block (indir 2 1)"
1863 "malloc failed. **\n");
1866 ext4fs_indir1_size = blksz;
1867 ext4fs_indir1_blkno = -1;
1869 if (blksz != ext4fs_indir1_size) {
1870 free(ext4fs_indir1_block);
1871 ext4fs_indir1_block = NULL;
1872 ext4fs_indir1_size = 0;
1873 ext4fs_indir1_blkno = -1;
1874 ext4fs_indir1_block = zalloc(blksz);
1875 if (ext4fs_indir1_block == NULL) {
1876 printf("** TI ext2fs read block (indir 2 1)"
1877 "malloc failed. **\n");
1880 ext4fs_indir1_size = blksz;
1882 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1883 log2_blksz) != ext4fs_indir1_blkno) {
1884 status = ext4fs_devread
1886 le32_to_cpu(inode->b.blocks.triple_indir_block)
1887 << log2_blksz, 0, blksz,
1888 (char *)ext4fs_indir1_block);
1890 printf("** TI ext2fs read block (indir 2 1)"
1894 ext4fs_indir1_blkno =
1895 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1899 if (ext4fs_indir2_block == NULL) {
1900 ext4fs_indir2_block = zalloc(blksz);
1901 if (ext4fs_indir2_block == NULL) {
1902 printf("** TI ext2fs read block (indir 2 2)"
1903 "malloc failed. **\n");
1906 ext4fs_indir2_size = blksz;
1907 ext4fs_indir2_blkno = -1;
1909 if (blksz != ext4fs_indir2_size) {
1910 free(ext4fs_indir2_block);
1911 ext4fs_indir2_block = NULL;
1912 ext4fs_indir2_size = 0;
1913 ext4fs_indir2_blkno = -1;
1914 ext4fs_indir2_block = zalloc(blksz);
1915 if (ext4fs_indir2_block == NULL) {
1916 printf("** TI ext2fs read block (indir 2 2)"
1917 "malloc failed. **\n");
1920 ext4fs_indir2_size = blksz;
1922 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1923 perblock_parent]) <<
1925 != ext4fs_indir2_blkno) {
1926 status = ext4fs_devread((lbaint_t)le32_to_cpu
1927 (ext4fs_indir1_block
1929 perblock_parent]) <<
1930 log2_blksz, 0, blksz,
1931 (char *)ext4fs_indir2_block);
1933 printf("** TI ext2fs read block (indir 2 2)"
1937 ext4fs_indir2_blkno =
1938 le32_to_cpu(ext4fs_indir1_block[rblock /
1943 if (ext4fs_indir3_block == NULL) {
1944 ext4fs_indir3_block = zalloc(blksz);
1945 if (ext4fs_indir3_block == NULL) {
1946 printf("** TI ext2fs read block (indir 2 2)"
1947 "malloc failed. **\n");
1950 ext4fs_indir3_size = blksz;
1951 ext4fs_indir3_blkno = -1;
1953 if (blksz != ext4fs_indir3_size) {
1954 free(ext4fs_indir3_block);
1955 ext4fs_indir3_block = NULL;
1956 ext4fs_indir3_size = 0;
1957 ext4fs_indir3_blkno = -1;
1958 ext4fs_indir3_block = zalloc(blksz);
1959 if (ext4fs_indir3_block == NULL) {
1960 printf("** TI ext2fs read block (indir 2 2)"
1961 "malloc failed. **\n");
1964 ext4fs_indir3_size = blksz;
1966 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1969 log2_blksz) != ext4fs_indir3_blkno) {
1971 ext4fs_devread((lbaint_t)le32_to_cpu
1972 (ext4fs_indir2_block
1973 [(rblock / perblock_child)
1974 % (blksz / 4)]) << log2_blksz, 0,
1975 blksz, (char *)ext4fs_indir3_block);
1977 printf("** TI ext2fs read block (indir 2 2)"
1981 ext4fs_indir3_blkno =
1982 le32_to_cpu(ext4fs_indir2_block[(rblock /
1989 blknr = le32_to_cpu(ext4fs_indir3_block
1990 [rblock % perblock_child]);
1992 debug("read_allocated_block %ld\n", blknr);
1998 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
2001 * This function assures that for a file with the same name but different size
2002 * the sequential store on the ext4 filesystem will be correct.
2004 * In this function the global data, responsible for internal representation
2005 * of the ext4 data are initialized to the reset state. Without this, during
2006 * replacement of the smaller file with the bigger truncation of new file was
2009 void ext4fs_reinit_global(void)
2011 if (ext4fs_indir1_block != NULL) {
2012 free(ext4fs_indir1_block);
2013 ext4fs_indir1_block = NULL;
2014 ext4fs_indir1_size = 0;
2015 ext4fs_indir1_blkno = -1;
2017 if (ext4fs_indir2_block != NULL) {
2018 free(ext4fs_indir2_block);
2019 ext4fs_indir2_block = NULL;
2020 ext4fs_indir2_size = 0;
2021 ext4fs_indir2_blkno = -1;
2023 if (ext4fs_indir3_block != NULL) {
2024 free(ext4fs_indir3_block);
2025 ext4fs_indir3_block = NULL;
2026 ext4fs_indir3_size = 0;
2027 ext4fs_indir3_blkno = -1;
2030 void ext4fs_close(void)
2032 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
2033 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
2036 if (ext4fs_root != NULL) {
2041 ext4fs_reinit_global();
2044 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2045 struct ext2fs_node **fnode, int *ftype)
2047 unsigned int fpos = 0;
2050 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2054 printf("Iterate dir %s\n", name);
2055 #endif /* of DEBUG */
2056 if (!diro->inode_read) {
2057 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2061 /* Search the file. */
2062 while (fpos < le32_to_cpu(diro->inode.size)) {
2063 struct ext2_dirent dirent;
2065 status = ext4fs_read_file(diro, fpos,
2066 sizeof(struct ext2_dirent),
2067 (char *)&dirent, &actread);
2071 if (dirent.direntlen == 0) {
2072 printf("Failed to iterate over directory %s\n", name);
2076 if (dirent.namelen != 0) {
2077 char filename[dirent.namelen + 1];
2078 struct ext2fs_node *fdiro;
2079 int type = FILETYPE_UNKNOWN;
2081 status = ext4fs_read_file(diro,
2083 sizeof(struct ext2_dirent),
2084 dirent.namelen, filename,
2089 fdiro = zalloc(sizeof(struct ext2fs_node));
2093 fdiro->data = diro->data;
2094 fdiro->ino = le32_to_cpu(dirent.inode);
2096 filename[dirent.namelen] = '\0';
2098 if (dirent.filetype != FILETYPE_UNKNOWN) {
2099 fdiro->inode_read = 0;
2101 if (dirent.filetype == FILETYPE_DIRECTORY)
2102 type = FILETYPE_DIRECTORY;
2103 else if (dirent.filetype == FILETYPE_SYMLINK)
2104 type = FILETYPE_SYMLINK;
2105 else if (dirent.filetype == FILETYPE_REG)
2106 type = FILETYPE_REG;
2108 status = ext4fs_read_inode(diro->data,
2116 fdiro->inode_read = 1;
2118 if ((le16_to_cpu(fdiro->inode.mode) &
2119 FILETYPE_INO_MASK) ==
2120 FILETYPE_INO_DIRECTORY) {
2121 type = FILETYPE_DIRECTORY;
2122 } else if ((le16_to_cpu(fdiro->inode.mode)
2123 & FILETYPE_INO_MASK) ==
2124 FILETYPE_INO_SYMLINK) {
2125 type = FILETYPE_SYMLINK;
2126 } else if ((le16_to_cpu(fdiro->inode.mode)
2127 & FILETYPE_INO_MASK) ==
2129 type = FILETYPE_REG;
2133 printf("iterate >%s<\n", filename);
2134 #endif /* of DEBUG */
2135 if ((name != NULL) && (fnode != NULL)
2136 && (ftype != NULL)) {
2137 if (strcmp(filename, name) == 0) {
2143 if (fdiro->inode_read == 0) {
2144 status = ext4fs_read_inode(diro->data,
2152 fdiro->inode_read = 1;
2155 case FILETYPE_DIRECTORY:
2158 case FILETYPE_SYMLINK:
2169 le32_to_cpu(fdiro->inode.size),
2174 fpos += le16_to_cpu(dirent.direntlen);
2179 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2182 struct ext2fs_node *diro = node;
2186 if (!diro->inode_read) {
2187 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2191 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2195 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2196 strncpy(symlink, diro->inode.b.symlink,
2197 le32_to_cpu(diro->inode.size));
2199 status = ext4fs_read_file(diro, 0,
2200 le32_to_cpu(diro->inode.size),
2202 if ((status < 0) || (actread == 0)) {
2207 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2211 static int ext4fs_find_file1(const char *currpath,
2212 struct ext2fs_node *currroot,
2213 struct ext2fs_node **currfound, int *foundtype)
2215 char fpath[strlen(currpath) + 1];
2219 int type = FILETYPE_DIRECTORY;
2220 struct ext2fs_node *currnode = currroot;
2221 struct ext2fs_node *oldnode = currroot;
2223 strncpy(fpath, currpath, strlen(currpath) + 1);
2225 /* Remove all leading slashes. */
2226 while (*name == '/')
2230 *currfound = currnode;
2237 /* Extract the actual part from the pathname. */
2238 next = strchr(name, '/');
2240 /* Remove all leading slashes. */
2241 while (*next == '/')
2245 if (type != FILETYPE_DIRECTORY) {
2246 ext4fs_free_node(currnode, currroot);
2252 /* Iterate over the directory. */
2253 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2260 /* Read in the symlink and follow it. */
2261 if (type == FILETYPE_SYMLINK) {
2264 /* Test if the symlink does not loop. */
2265 if (++symlinknest == 8) {
2266 ext4fs_free_node(currnode, currroot);
2267 ext4fs_free_node(oldnode, currroot);
2271 symlink = ext4fs_read_symlink(currnode);
2272 ext4fs_free_node(currnode, currroot);
2275 ext4fs_free_node(oldnode, currroot);
2279 debug("Got symlink >%s<\n", symlink);
2281 if (symlink[0] == '/') {
2282 ext4fs_free_node(oldnode, currroot);
2283 oldnode = &ext4fs_root->diropen;
2286 /* Lookup the node the symlink points to. */
2287 status = ext4fs_find_file1(symlink, oldnode,
2293 ext4fs_free_node(oldnode, currroot);
2298 ext4fs_free_node(oldnode, currroot);
2300 /* Found the node! */
2301 if (!next || *next == '\0') {
2302 *currfound = currnode;
2311 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2312 struct ext2fs_node **foundnode, int expecttype)
2315 int foundtype = FILETYPE_DIRECTORY;
2321 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2325 /* Check if the node that was found was of the expected type. */
2326 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2328 else if ((expecttype == FILETYPE_DIRECTORY)
2329 && (foundtype != expecttype))
2335 int ext4fs_open(const char *filename, loff_t *len)
2337 struct ext2fs_node *fdiro = NULL;
2340 if (ext4fs_root == NULL)
2344 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2349 if (!fdiro->inode_read) {
2350 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2355 *len = le32_to_cpu(fdiro->inode.size);
2356 ext4fs_file = fdiro;
2360 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2365 int ext4fs_mount(unsigned part_length)
2367 struct ext2_data *data;
2369 struct ext_filesystem *fs = get_fs();
2370 data = zalloc(SUPERBLOCK_SIZE);
2374 /* Read the superblock. */
2375 status = ext4_read_superblock((char *)&data->sblock);
2380 /* Make sure this is an ext2 filesystem. */
2381 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2385 if (le32_to_cpu(data->sblock.revision_level) == 0) {
2389 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2390 __le32_to_cpu(data->sblock.feature_compatibility),
2391 __le32_to_cpu(data->sblock.feature_incompat),
2392 __le32_to_cpu(data->sblock.feature_ro_compat));
2394 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2395 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2396 EXT4_FEATURE_INCOMPAT_64BIT ?
2397 le16_to_cpu(data->sblock.descriptor_size) : 32;
2400 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2401 le32_to_cpu(data->sblock.revision_level),
2402 fs->inodesz, fs->gdsize);
2404 data->diropen.data = data;
2405 data->diropen.ino = 2;
2406 data->diropen.inode_read = 1;
2407 data->inode = &data->diropen.inode;
2409 status = ext4fs_read_inode(data, 2, data->inode);
2417 printf("Failed to mount ext2 filesystem...\n");