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 __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 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
66 static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
68 bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
71 static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
73 bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
76 static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
78 bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
81 uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
83 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
84 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
88 void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
90 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
91 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
94 uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
95 const struct ext_filesystem *fs)
97 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
99 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
104 uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
105 const struct ext_filesystem *fs)
107 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
108 if (fs->gdsize == 64)
109 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
113 static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
115 return le16_to_cpu(bg->bg_flags);
118 static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
121 bg->bg_flags = cpu_to_le16(flags);
124 /* Block number of the block bitmap */
125 uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
126 const struct ext_filesystem *fs)
128 uint64_t block_nr = le32_to_cpu(bg->block_id);
129 if (fs->gdsize == 64)
130 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
134 /* Block number of the inode bitmap */
135 uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
136 const struct ext_filesystem *fs)
138 uint64_t block_nr = le32_to_cpu(bg->inode_id);
139 if (fs->gdsize == 64)
140 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
145 /* Block number of the inode table */
146 uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
147 const struct ext_filesystem *fs)
149 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
150 if (fs->gdsize == 64)
152 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
156 #if defined(CONFIG_EXT4_WRITE)
157 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
159 uint32_t res = size / n;
166 void put_ext4(uint64_t off, void *buf, uint32_t size)
170 unsigned char *temp_ptr = NULL;
171 struct ext_filesystem *fs = get_fs();
172 int log2blksz = fs->dev_desc->log2blksz;
173 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
175 startblock = off >> log2blksz;
176 startblock += part_offset;
177 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
179 if (fs->dev_desc == NULL)
182 if ((startblock + (size >> log2blksz)) >
183 (part_offset + fs->total_sect)) {
184 printf("part_offset is " LBAFU "\n", part_offset);
185 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
186 printf("error: overflow occurs\n");
191 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
193 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
194 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
196 if (size >> log2blksz != 0) {
197 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
198 (unsigned long *)buf);
200 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
202 memcpy(temp_ptr, buf, size);
203 blk_dwrite(fs->dev_desc, startblock, 1,
204 (unsigned long *)sec_buf);
209 static int _get_new_inode_no(unsigned char *buffer)
211 struct ext_filesystem *fs = get_fs();
217 /* get the blocksize of the filesystem */
218 unsigned char *ptr = buffer;
219 while (*ptr == 255) {
222 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
226 for (j = 0; j < fs->blksz; j++) {
231 status = input & operand;
246 static int _get_new_blk_no(unsigned char *buffer)
251 unsigned char *ptr = buffer;
252 struct ext_filesystem *fs = get_fs();
254 while (*ptr == 255) {
257 if (count == (fs->blksz * 8))
261 if (fs->blksz == 1024)
264 for (i = 0; i <= 7; i++) {
266 if (*ptr & operand) {
277 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
279 int i, remainder, status;
280 unsigned char *ptr = buffer;
281 unsigned char operand;
283 remainder = blockno % 8;
284 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
286 i = i - (index * blocksize);
287 if (blocksize != 1024) {
289 operand = 1 << remainder;
290 status = *ptr & operand;
294 *ptr = *ptr | operand;
297 if (remainder == 0) {
302 operand = (1 << (remainder - 1));
304 status = *ptr & operand;
308 *ptr = *ptr | operand;
313 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
315 int i, remainder, status;
316 unsigned char *ptr = buffer;
317 unsigned char operand;
319 remainder = blockno % 8;
320 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
322 i = i - (index * blocksize);
323 if (blocksize != 1024) {
325 operand = (1 << remainder);
326 status = *ptr & operand;
328 *ptr = *ptr & ~(operand);
330 if (remainder == 0) {
335 operand = (1 << (remainder - 1));
337 status = *ptr & operand;
339 *ptr = *ptr & ~(operand);
343 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
345 int i, remainder, status;
346 unsigned char *ptr = buffer;
347 unsigned char operand;
349 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
351 remainder = inode_no % 8;
352 if (remainder == 0) {
357 operand = (1 << (remainder - 1));
359 status = *ptr & operand;
363 *ptr = *ptr | operand;
368 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
370 int i, remainder, status;
371 unsigned char *ptr = buffer;
372 unsigned char operand;
374 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
376 remainder = inode_no % 8;
377 if (remainder == 0) {
382 operand = (1 << (remainder - 1));
384 status = *ptr & operand;
386 *ptr = *ptr & ~(operand);
389 uint16_t ext4fs_checksum_update(uint32_t i)
391 struct ext2_block_group *desc;
392 struct ext_filesystem *fs = get_fs();
394 __le32 le32_i = cpu_to_le32(i);
396 desc = ext4fs_get_group_descriptor(fs, i);
397 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
398 int offset = offsetof(struct ext2_block_group, bg_checksum);
400 crc = ext2fs_crc16(~0, fs->sb->unique_id,
401 sizeof(fs->sb->unique_id));
402 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
403 crc = ext2fs_crc16(crc, desc, offset);
404 offset += sizeof(desc->bg_checksum); /* skip checksum */
405 assert(offset == sizeof(*desc));
411 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
414 int sizeof_void_space;
415 int new_entry_byte_reqd;
416 short padding_factor = 0;
418 if (dir->namelen % 4 != 0)
419 padding_factor = 4 - (dir->namelen % 4);
421 dentry_length = sizeof(struct ext2_dirent) +
422 dir->namelen + padding_factor;
423 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
424 if (sizeof_void_space == 0)
428 if (strlen(filename) % 4 != 0)
429 padding_factor = 4 - (strlen(filename) % 4);
431 new_entry_byte_reqd = strlen(filename) +
432 sizeof(struct ext2_dirent) + padding_factor;
433 if (sizeof_void_space >= new_entry_byte_reqd) {
434 dir->direntlen = cpu_to_le16(dentry_length);
435 return sizeof_void_space;
441 int ext4fs_update_parent_dentry(char *filename, int file_type)
443 unsigned int *zero_buffer = NULL;
444 char *root_first_block_buffer = NULL;
446 long int first_block_no_of_root = 0;
448 unsigned int new_entry_byte_reqd;
449 int sizeof_void_space = 0;
453 struct ext_filesystem *fs = get_fs();
454 /* directory entry */
455 struct ext2_dirent *dir;
456 char *temp_dir = NULL;
459 uint32_t new_blockcnt;
460 uint32_t directory_blocks;
462 zero_buffer = zalloc(fs->blksz);
464 printf("No Memory\n");
467 root_first_block_buffer = zalloc(fs->blksz);
468 if (!root_first_block_buffer) {
470 printf("No Memory\n");
473 new_entry_byte_reqd = ROUND(strlen(filename) +
474 sizeof(struct ext2_dirent), 4);
476 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
477 LOG2_BLOCK_SIZE(ext4fs_root);
478 blk_idx = directory_blocks - 1;
481 /* read the block no allocated to a file */
482 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
483 if (first_block_no_of_root <= 0)
486 status = ext4fs_devread((lbaint_t)first_block_no_of_root
488 0, fs->blksz, root_first_block_buffer);
492 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
494 dir = (struct ext2_dirent *)root_first_block_buffer;
497 while (le16_to_cpu(dir->direntlen) > 0) {
498 unsigned short used_len = ROUND(dir->namelen +
499 sizeof(struct ext2_dirent), 4);
501 /* last entry of block */
502 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
504 /* check if new entry fits */
505 if ((used_len + new_entry_byte_reqd) <=
506 le16_to_cpu(dir->direntlen)) {
507 dir->direntlen = cpu_to_le16(used_len);
511 printf("Block full, trying previous\n");
515 printf("All blocks full: Allocate new\n");
517 if (le32_to_cpu(g_parent_inode->flags) &
519 printf("Directory uses extents\n");
522 if (directory_blocks >= INDIRECT_BLOCKS) {
523 printf("Directory exceeds limit\n");
526 new_blk_no = ext4fs_get_new_blk_no();
527 if (new_blk_no == -1) {
528 printf("no block left to assign\n");
531 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
532 g_parent_inode->b.blocks.
533 dir_blocks[directory_blocks] =
534 cpu_to_le32(new_blk_no);
536 new_size = le32_to_cpu(g_parent_inode->size);
537 new_size += fs->blksz;
538 g_parent_inode->size = cpu_to_le32(new_size);
540 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
541 new_blockcnt += fs->sect_perblk;
542 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
544 if (ext4fs_put_metadata
545 (root_first_block_buffer,
546 first_block_no_of_root))
552 templength = le16_to_cpu(dir->direntlen);
553 totalbytes = totalbytes + templength;
554 sizeof_void_space = check_void_in_dentry(dir, filename);
555 if (sizeof_void_space)
558 dir = (struct ext2_dirent *)((char *)dir + templength);
561 /* make a pointer ready for creating next directory entry */
562 templength = le16_to_cpu(dir->direntlen);
563 totalbytes = totalbytes + templength;
564 dir = (struct ext2_dirent *)((char *)dir + templength);
566 /* get the next available inode number */
567 inodeno = ext4fs_get_new_inode_no();
569 printf("no inode left to assign\n");
572 dir->inode = cpu_to_le32(inodeno);
573 if (sizeof_void_space)
574 dir->direntlen = cpu_to_le16(sizeof_void_space);
576 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
578 dir->namelen = strlen(filename);
579 dir->filetype = FILETYPE_REG; /* regular file */
580 temp_dir = (char *)dir;
581 temp_dir = temp_dir + sizeof(struct ext2_dirent);
582 memcpy(temp_dir, filename, strlen(filename));
584 /* update or write the 1st block of root inode */
585 if (ext4fs_put_metadata(root_first_block_buffer,
586 first_block_no_of_root))
591 free(root_first_block_buffer);
596 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
603 char *block_buffer = NULL;
604 struct ext2_dirent *dir = NULL;
605 struct ext_filesystem *fs = get_fs();
606 uint32_t directory_blocks;
609 directory_blocks = le32_to_cpu(parent_inode->size) >>
610 LOG2_BLOCK_SIZE(ext4fs_root);
612 block_buffer = zalloc(fs->blksz);
616 /* get the block no allocated to a file */
617 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
618 blknr = read_allocated_block(parent_inode, blk_idx);
622 /* read the directory block */
623 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
624 0, fs->blksz, (char *)block_buffer);
630 dir = (struct ext2_dirent *)(block_buffer + offset);
631 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
633 int direntlen = le16_to_cpu(dir->direntlen);
634 if (direntlen < sizeof(struct ext2_dirent))
637 if (dir->inode && (strlen(dirname) == dir->namelen) &&
638 (strncmp(dirname, direntname, dir->namelen) == 0)) {
639 inodeno = le32_to_cpu(dir->inode);
645 } while (offset < fs->blksz);
659 static int find_dir_depth(char *dirname)
661 char *token = strtok(dirname, "/");
663 while (token != NULL) {
664 token = strtok(NULL, "/");
667 return count + 1 + 1;
669 * for example for string /home/temp
670 * depth=home(1)+temp(1)+1 extra for NULL;
675 static int parse_path(char **arr, char *dirname)
677 char *token = strtok(dirname, "/");
681 arr[i] = zalloc(strlen("/") + 1);
684 memcpy(arr[i++], "/", strlen("/"));
686 /* add each path entry after root */
687 while (token != NULL) {
688 arr[i] = zalloc(strlen(token) + 1);
691 memcpy(arr[i++], token, strlen(token));
692 token = strtok(NULL, "/");
699 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
701 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
708 * Function: ext4fs_get_parent_inode_num
709 * Return Value: inode Number of the parent directory of file/Directory to be
711 * dirname : Input parmater, input path name of the file/directory to be created
712 * dname : Output parameter, to be filled with the name of the directory
713 * extracted from dirname
715 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
719 int matched_inode_no;
720 int result_inode_no = -1;
722 char *depth_dirname = NULL;
723 char *parse_dirname = NULL;
724 struct ext2_inode *parent_inode = NULL;
725 struct ext2_inode *first_inode = NULL;
726 struct ext2_inode temp_inode;
728 if (*dirname != '/') {
729 printf("Please supply Absolute path\n");
733 /* TODO: input validation make equivalent to linux */
734 depth_dirname = zalloc(strlen(dirname) + 1);
738 memcpy(depth_dirname, dirname, strlen(dirname));
739 depth = find_dir_depth(depth_dirname);
740 parse_dirname = zalloc(strlen(dirname) + 1);
743 memcpy(parse_dirname, dirname, strlen(dirname));
745 /* allocate memory for each directory level */
746 ptr = zalloc((depth) * sizeof(char *));
749 if (parse_path(ptr, parse_dirname))
751 parent_inode = zalloc(sizeof(struct ext2_inode));
754 first_inode = zalloc(sizeof(struct ext2_inode));
757 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
758 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
760 result_inode_no = EXT2_ROOT_INO;
761 for (i = 1; i < depth; i++) {
762 matched_inode_no = search_dir(parent_inode, ptr[i]);
763 if (matched_inode_no == -1) {
764 if (ptr[i + 1] == NULL && i == 1) {
765 result_inode_no = EXT2_ROOT_INO;
768 if (ptr[i + 1] == NULL)
770 printf("Invalid path\n");
771 result_inode_no = -1;
775 if (ptr[i + 1] != NULL) {
776 memset(parent_inode, '\0',
777 sizeof(struct ext2_inode));
778 if (ext4fs_iget(matched_inode_no,
780 result_inode_no = -1;
783 result_inode_no = matched_inode_no;
792 matched_inode_no = search_dir(first_inode, ptr[i]);
794 matched_inode_no = search_dir(parent_inode, ptr[i]);
796 if (matched_inode_no != -1) {
797 ext4fs_iget(matched_inode_no, &temp_inode);
798 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
799 printf("It is a Directory\n");
800 result_inode_no = -1;
805 if (strlen(ptr[i]) > 256) {
806 result_inode_no = -1;
809 memcpy(dname, ptr[i], strlen(ptr[i]));
814 for (i = 0; i < depth; i++) {
823 return result_inode_no;
826 static int unlink_filename(char *filename, unsigned int blknr)
832 char *root_first_block_buffer = NULL;
833 struct ext2_dirent *dir = NULL;
834 struct ext2_dirent *previous_dir = NULL;
836 struct ext_filesystem *fs = get_fs();
839 /* get the first block of root */
840 root_first_block_buffer = zalloc(fs->blksz);
841 if (!root_first_block_buffer)
843 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
844 fs->blksz, root_first_block_buffer);
848 if (ext4fs_log_journal(root_first_block_buffer, blknr))
850 dir = (struct ext2_dirent *)root_first_block_buffer;
853 while (le16_to_cpu(dir->direntlen) >= 0) {
855 * blocksize-totalbytes because last
856 * directory length i.e., *dir->direntlen
857 * is free availble space in the block that
858 * means it is a last entry of directory entry
860 if (dir->inode && (strlen(filename) == dir->namelen) &&
861 (strncmp(ptr + sizeof(struct ext2_dirent),
862 filename, dir->namelen) == 0)) {
863 printf("file found, deleting\n");
864 inodeno = le32_to_cpu(dir->inode);
867 new_len = le16_to_cpu(previous_dir->direntlen);
868 new_len += le16_to_cpu(dir->direntlen);
869 previous_dir->direntlen = cpu_to_le16(new_len);
877 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
880 /* traversing the each directory entry */
881 templength = le16_to_cpu(dir->direntlen);
882 totalbytes = totalbytes + templength;
884 dir = (struct ext2_dirent *)((char *)dir + templength);
890 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
895 free(root_first_block_buffer);
900 int ext4fs_filename_unlink(char *filename)
905 uint32_t directory_blocks;
907 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
908 LOG2_BLOCK_SIZE(ext4fs_root);
910 /* read the block no allocated to a file */
911 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
912 blknr = read_allocated_block(g_parent_inode, blk_idx);
915 inodeno = unlink_filename(filename, blknr);
923 uint32_t ext4fs_get_new_blk_no(void)
929 static int prev_bg_bitmap_index = -1;
930 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
931 struct ext_filesystem *fs = get_fs();
932 char *journal_buffer = zalloc(fs->blksz);
933 char *zero_buffer = zalloc(fs->blksz);
934 if (!journal_buffer || !zero_buffer)
937 if (fs->first_pass_bbmap == 0) {
938 for (i = 0; i < fs->no_blkgrp; i++) {
939 struct ext2_block_group *bgd = NULL;
940 bgd = ext4fs_get_group_descriptor(fs, i);
941 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
942 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
943 uint64_t b_bitmap_blk =
944 ext4fs_bg_get_block_id(bgd, fs);
945 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
946 memcpy(fs->blk_bmaps[i], zero_buffer,
948 put_ext4(b_bitmap_blk * fs->blksz,
949 fs->blk_bmaps[i], fs->blksz);
950 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
951 ext4fs_bg_set_flags(bgd, bg_flags);
954 _get_new_blk_no(fs->blk_bmaps[i]);
955 if (fs->curr_blkno == -1)
956 /* block bitmap is completely filled */
958 fs->curr_blkno = fs->curr_blkno +
960 fs->first_pass_bbmap++;
961 ext4fs_bg_free_blocks_dec(bgd);
962 ext4fs_sb_free_blocks_dec(fs->sb);
963 status = ext4fs_devread(b_bitmap_blk *
969 if (ext4fs_log_journal(journal_buffer,
974 debug("no space left on block group %d\n", i);
982 /* get the blockbitmap index respective to blockno */
983 bg_idx = fs->curr_blkno / blk_per_grp;
984 if (fs->blksz == 1024) {
985 remainder = fs->curr_blkno % blk_per_grp;
991 * To skip completely filled block group bitmaps
992 * Optimize the block allocation
994 if (bg_idx >= fs->no_blkgrp)
997 struct ext2_block_group *bgd = NULL;
998 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
999 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
1000 debug("block group %u is full. Skipping\n", bg_idx);
1001 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1002 if (fs->blksz == 1024)
1003 fs->curr_blkno += 1;
1007 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1008 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1009 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
1010 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1011 put_ext4(b_bitmap_blk * fs->blksz,
1012 zero_buffer, fs->blksz);
1013 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1014 ext4fs_bg_set_flags(bgd, bg_flags);
1017 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1019 debug("going for restart for the block no %ld %u\n",
1020 fs->curr_blkno, bg_idx);
1025 /* journal backup */
1026 if (prev_bg_bitmap_index != bg_idx) {
1027 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
1028 0, fs->blksz, journal_buffer);
1031 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
1034 prev_bg_bitmap_index = bg_idx;
1036 ext4fs_bg_free_blocks_dec(bgd);
1037 ext4fs_sb_free_blocks_dec(fs->sb);
1041 free(journal_buffer);
1044 return fs->curr_blkno;
1046 free(journal_buffer);
1052 int ext4fs_get_new_inode_no(void)
1056 unsigned int ibmap_idx;
1057 static int prev_inode_bitmap_index = -1;
1058 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1059 struct ext_filesystem *fs = get_fs();
1060 char *journal_buffer = zalloc(fs->blksz);
1061 char *zero_buffer = zalloc(fs->blksz);
1062 if (!journal_buffer || !zero_buffer)
1064 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1065 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
1067 if (fs->first_pass_ibmap == 0) {
1068 for (i = 0; i < fs->no_blkgrp; i++) {
1069 uint32_t free_inodes;
1070 struct ext2_block_group *bgd = NULL;
1071 bgd = ext4fs_get_group_descriptor(fs, i);
1072 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1074 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1075 uint64_t i_bitmap_blk =
1076 ext4fs_bg_get_inode_id(bgd, fs);
1078 bgd->bg_itable_unused = free_inodes;
1079 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1080 put_ext4(i_bitmap_blk * fs->blksz,
1081 zero_buffer, fs->blksz);
1082 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1083 ext4fs_bg_set_flags(bgd, bg_flags);
1084 memcpy(fs->inode_bmaps[i],
1085 zero_buffer, fs->blksz);
1088 _get_new_inode_no(fs->inode_bmaps[i]);
1089 if (fs->curr_inode_no == -1)
1090 /* inode bitmap is completely filled */
1092 fs->curr_inode_no = fs->curr_inode_no +
1093 (i * inodes_per_grp);
1094 fs->first_pass_ibmap++;
1095 ext4fs_bg_free_inodes_dec(bgd);
1097 ext4fs_bg_itable_unused_dec(bgd);
1098 ext4fs_sb_free_inodes_dec(fs->sb);
1099 status = ext4fs_devread(i_bitmap_blk *
1105 if (ext4fs_log_journal(journal_buffer,
1110 debug("no inode left on block group %d\n", i);
1115 fs->curr_inode_no++;
1116 /* get the blockbitmap index respective to blockno */
1117 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1118 struct ext2_block_group *bgd =
1119 ext4fs_get_group_descriptor(fs, ibmap_idx);
1120 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1121 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1123 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1124 put_ext4(i_bitmap_blk * fs->blksz,
1125 zero_buffer, fs->blksz);
1126 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1127 ext4fs_bg_set_flags(bgd, bg_flags);
1128 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1132 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1133 fs->inode_bmaps[ibmap_idx],
1135 debug("going for restart for the block no %d %u\n",
1136 fs->curr_inode_no, ibmap_idx);
1140 /* journal backup */
1141 if (prev_inode_bitmap_index != ibmap_idx) {
1142 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
1143 0, fs->blksz, journal_buffer);
1146 if (ext4fs_log_journal(journal_buffer,
1147 le32_to_cpu(bgd->inode_id)))
1149 prev_inode_bitmap_index = ibmap_idx;
1151 ext4fs_bg_free_inodes_dec(bgd);
1153 bgd->bg_itable_unused = bgd->free_inodes;
1154 ext4fs_sb_free_inodes_dec(fs->sb);
1159 free(journal_buffer);
1162 return fs->curr_inode_no;
1164 free(journal_buffer);
1172 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1173 unsigned int *total_remaining_blocks,
1174 unsigned int *no_blks_reqd)
1178 long int actual_block_no;
1179 long int si_blockno;
1180 /* si :single indirect */
1181 __le32 *si_buffer = NULL;
1182 __le32 *si_start_addr = NULL;
1183 struct ext_filesystem *fs = get_fs();
1185 if (*total_remaining_blocks != 0) {
1186 si_buffer = zalloc(fs->blksz);
1188 printf("No Memory\n");
1191 si_start_addr = si_buffer;
1192 si_blockno = ext4fs_get_new_blk_no();
1193 if (si_blockno == -1) {
1194 printf("no block left to assign\n");
1198 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1200 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1201 0, fs->blksz, (char *)si_buffer);
1202 memset(si_buffer, '\0', fs->blksz);
1206 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1207 actual_block_no = ext4fs_get_new_blk_no();
1208 if (actual_block_no == -1) {
1209 printf("no block left to assign\n");
1212 *si_buffer = cpu_to_le32(actual_block_no);
1213 debug("SIAB %u: %u\n", *si_buffer,
1214 *total_remaining_blocks);
1217 (*total_remaining_blocks)--;
1218 if (*total_remaining_blocks == 0)
1222 /* write the block to disk */
1223 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1224 si_start_addr, fs->blksz);
1225 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1228 free(si_start_addr);
1231 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1232 unsigned int *total_remaining_blocks,
1233 unsigned int *no_blks_reqd)
1238 long int actual_block_no;
1239 /* di:double indirect */
1240 long int di_blockno_parent;
1241 long int di_blockno_child;
1242 __le32 *di_parent_buffer = NULL;
1243 __le32 *di_child_buff = NULL;
1244 __le32 *di_block_start_addr = NULL;
1245 __le32 *di_child_buff_start = NULL;
1246 struct ext_filesystem *fs = get_fs();
1248 if (*total_remaining_blocks != 0) {
1249 /* double indirect parent block connecting to inode */
1250 di_blockno_parent = ext4fs_get_new_blk_no();
1251 if (di_blockno_parent == -1) {
1252 printf("no block left to assign\n");
1255 di_parent_buffer = zalloc(fs->blksz);
1256 if (!di_parent_buffer)
1259 di_block_start_addr = di_parent_buffer;
1261 debug("DIPB %ld: %u\n", di_blockno_parent,
1262 *total_remaining_blocks);
1264 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1266 fs->blksz, (char *)di_parent_buffer);
1269 printf("%s: Device read error!\n", __func__);
1272 memset(di_parent_buffer, '\0', fs->blksz);
1275 * start:for each double indirect parent
1276 * block create one more block
1278 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1279 di_blockno_child = ext4fs_get_new_blk_no();
1280 if (di_blockno_child == -1) {
1281 printf("no block left to assign\n");
1284 di_child_buff = zalloc(fs->blksz);
1288 di_child_buff_start = di_child_buff;
1289 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1292 debug("DICB %ld: %u\n", di_blockno_child,
1293 *total_remaining_blocks);
1295 status = ext4fs_devread((lbaint_t)di_blockno_child *
1298 (char *)di_child_buff);
1301 printf("%s: Device read error!\n", __func__);
1304 memset(di_child_buff, '\0', fs->blksz);
1305 /* filling of actual datablocks for each child */
1306 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1307 actual_block_no = ext4fs_get_new_blk_no();
1308 if (actual_block_no == -1) {
1309 printf("no block left to assign\n");
1312 *di_child_buff = cpu_to_le32(actual_block_no);
1313 debug("DIAB %ld: %u\n", actual_block_no,
1314 *total_remaining_blocks);
1317 (*total_remaining_blocks)--;
1318 if (*total_remaining_blocks == 0)
1321 /* write the block table */
1322 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1323 di_child_buff_start, fs->blksz);
1324 free(di_child_buff_start);
1325 di_child_buff_start = NULL;
1327 if (*total_remaining_blocks == 0)
1330 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1331 di_block_start_addr, fs->blksz);
1332 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1335 free(di_block_start_addr);
1338 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1339 unsigned int *total_remaining_blocks,
1340 unsigned int *no_blks_reqd)
1345 long int actual_block_no;
1346 /* ti: Triple Indirect */
1347 long int ti_gp_blockno;
1348 long int ti_parent_blockno;
1349 long int ti_child_blockno;
1350 __le32 *ti_gp_buff = NULL;
1351 __le32 *ti_parent_buff = NULL;
1352 __le32 *ti_child_buff = NULL;
1353 __le32 *ti_gp_buff_start_addr = NULL;
1354 __le32 *ti_pbuff_start_addr = NULL;
1355 __le32 *ti_cbuff_start_addr = NULL;
1356 struct ext_filesystem *fs = get_fs();
1357 if (*total_remaining_blocks != 0) {
1358 /* triple indirect grand parent block connecting to inode */
1359 ti_gp_blockno = ext4fs_get_new_blk_no();
1360 if (ti_gp_blockno == -1) {
1361 printf("no block left to assign\n");
1364 ti_gp_buff = zalloc(fs->blksz);
1368 ti_gp_buff_start_addr = ti_gp_buff;
1370 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1371 *total_remaining_blocks);
1373 /* for each 4 byte grand parent entry create one more block */
1374 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1375 ti_parent_blockno = ext4fs_get_new_blk_no();
1376 if (ti_parent_blockno == -1) {
1377 printf("no block left to assign\n");
1380 ti_parent_buff = zalloc(fs->blksz);
1381 if (!ti_parent_buff)
1384 ti_pbuff_start_addr = ti_parent_buff;
1385 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1388 debug("TIPB %ld: %u\n", ti_parent_blockno,
1389 *total_remaining_blocks);
1391 /* for each 4 byte entry parent create one more block */
1392 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1393 ti_child_blockno = ext4fs_get_new_blk_no();
1394 if (ti_child_blockno == -1) {
1395 printf("no block left assign\n");
1398 ti_child_buff = zalloc(fs->blksz);
1402 ti_cbuff_start_addr = ti_child_buff;
1403 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1406 debug("TICB %ld: %u\n", ti_parent_blockno,
1407 *total_remaining_blocks);
1409 /* fill actual datablocks for each child */
1410 for (k = 0; k < (fs->blksz / sizeof(int));
1413 ext4fs_get_new_blk_no();
1414 if (actual_block_no == -1) {
1415 printf("no block left\n");
1416 free(ti_cbuff_start_addr);
1419 *ti_child_buff = cpu_to_le32(actual_block_no);
1420 debug("TIAB %ld: %u\n", actual_block_no,
1421 *total_remaining_blocks);
1424 (*total_remaining_blocks)--;
1425 if (*total_remaining_blocks == 0)
1428 /* write the child block */
1429 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1430 (uint64_t)fs->blksz)),
1431 ti_cbuff_start_addr, fs->blksz);
1432 free(ti_cbuff_start_addr);
1434 if (*total_remaining_blocks == 0)
1437 /* write the parent block */
1438 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1439 ti_pbuff_start_addr, fs->blksz);
1440 free(ti_pbuff_start_addr);
1442 if (*total_remaining_blocks == 0)
1445 /* write the grand parent block */
1446 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1447 ti_gp_buff_start_addr, fs->blksz);
1448 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1449 free(ti_gp_buff_start_addr);
1453 free(ti_pbuff_start_addr);
1455 free(ti_gp_buff_start_addr);
1458 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1459 unsigned int total_remaining_blocks,
1460 unsigned int *total_no_of_block)
1463 long int direct_blockno;
1464 unsigned int no_blks_reqd = 0;
1466 /* allocation of direct blocks */
1467 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1468 direct_blockno = ext4fs_get_new_blk_no();
1469 if (direct_blockno == -1) {
1470 printf("no block left to assign\n");
1473 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1474 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1476 total_remaining_blocks--;
1479 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1481 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1483 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1485 *total_no_of_block += no_blks_reqd;
1490 static struct ext4_extent_header *ext4fs_get_extent_block
1491 (struct ext2_data *data, char *buf,
1492 struct ext4_extent_header *ext_block,
1493 uint32_t fileblock, int log2_blksz)
1495 struct ext4_extent_idx *index;
1496 unsigned long long block;
1497 int blksz = EXT2_BLOCK_SIZE(data);
1501 index = (struct ext4_extent_idx *)(ext_block + 1);
1503 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1506 if (ext_block->eh_depth == 0)
1511 if (i >= le16_to_cpu(ext_block->eh_entries))
1513 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1518 block = le16_to_cpu(index[i].ei_leaf_hi);
1519 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1521 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1523 ext_block = (struct ext4_extent_header *)buf;
1529 static int ext4fs_blockgroup
1530 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1533 unsigned int blkoff, desc_per_blk;
1534 int log2blksz = get_fs()->dev_desc->log2blksz;
1535 int desc_size = get_fs()->gdsize;
1537 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
1539 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1540 group / desc_per_blk;
1541 blkoff = (group % desc_per_blk) * desc_size;
1543 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1544 group, blkno, blkoff);
1546 return ext4fs_devread((lbaint_t)blkno <<
1547 (LOG2_BLOCK_SIZE(data) - log2blksz),
1548 blkoff, desc_size, (char *)blkgrp);
1551 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1553 struct ext2_block_group blkgrp;
1554 struct ext2_sblock *sblock = &data->sblock;
1555 struct ext_filesystem *fs = get_fs();
1556 int log2blksz = get_fs()->dev_desc->log2blksz;
1557 int inodes_per_block, status;
1559 unsigned int blkoff;
1561 /* It is easier to calculate if the first inode is 0. */
1563 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1564 (sblock->inodes_per_group), &blkgrp);
1568 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1569 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
1570 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1571 blkoff = (ino % inodes_per_block) * fs->inodesz;
1572 /* Read the inode. */
1573 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1575 sizeof(struct ext2_inode), (char *)inode);
1582 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1589 long int perblock_parent;
1590 long int perblock_child;
1591 unsigned long long start;
1592 /* get the blocksize of the filesystem */
1593 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1594 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1595 - get_fs()->dev_desc->log2blksz;
1597 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1598 char *buf = zalloc(blksz);
1601 struct ext4_extent_header *ext_block;
1602 struct ext4_extent *extent;
1605 ext4fs_get_extent_block(ext4fs_root, buf,
1606 (struct ext4_extent_header *)
1607 inode->b.blocks.dir_blocks,
1608 fileblock, log2_blksz);
1610 printf("invalid extent block\n");
1615 extent = (struct ext4_extent *)(ext_block + 1);
1619 if (i >= le16_to_cpu(ext_block->eh_entries))
1621 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1623 fileblock -= le32_to_cpu(extent[i].ee_block);
1624 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1629 start = le16_to_cpu(extent[i].ee_start_hi);
1630 start = (start << 32) +
1631 le32_to_cpu(extent[i].ee_start_lo);
1633 return fileblock + start;
1636 printf("Extent Error\n");
1641 /* Direct blocks. */
1642 if (fileblock < INDIRECT_BLOCKS)
1643 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1646 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1647 if (ext4fs_indir1_block == NULL) {
1648 ext4fs_indir1_block = zalloc(blksz);
1649 if (ext4fs_indir1_block == NULL) {
1650 printf("** SI ext2fs read block (indir 1)"
1651 "malloc failed. **\n");
1654 ext4fs_indir1_size = blksz;
1655 ext4fs_indir1_blkno = -1;
1657 if (blksz != ext4fs_indir1_size) {
1658 free(ext4fs_indir1_block);
1659 ext4fs_indir1_block = NULL;
1660 ext4fs_indir1_size = 0;
1661 ext4fs_indir1_blkno = -1;
1662 ext4fs_indir1_block = zalloc(blksz);
1663 if (ext4fs_indir1_block == NULL) {
1664 printf("** SI ext2fs read block (indir 1):"
1665 "malloc failed. **\n");
1668 ext4fs_indir1_size = blksz;
1670 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1671 log2_blksz) != ext4fs_indir1_blkno) {
1673 ext4fs_devread((lbaint_t)le32_to_cpu
1675 indir_block) << log2_blksz, 0,
1676 blksz, (char *)ext4fs_indir1_block);
1678 printf("** SI ext2fs read block (indir 1)"
1682 ext4fs_indir1_blkno =
1683 le32_to_cpu(inode->b.blocks.
1684 indir_block) << log2_blksz;
1686 blknr = le32_to_cpu(ext4fs_indir1_block
1687 [fileblock - INDIRECT_BLOCKS]);
1689 /* Double indirect. */
1690 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1691 (blksz / 4 + 1)))) {
1693 long int perblock = blksz / 4;
1694 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1696 if (ext4fs_indir1_block == NULL) {
1697 ext4fs_indir1_block = zalloc(blksz);
1698 if (ext4fs_indir1_block == NULL) {
1699 printf("** DI ext2fs read block (indir 2 1)"
1700 "malloc failed. **\n");
1703 ext4fs_indir1_size = blksz;
1704 ext4fs_indir1_blkno = -1;
1706 if (blksz != ext4fs_indir1_size) {
1707 free(ext4fs_indir1_block);
1708 ext4fs_indir1_block = NULL;
1709 ext4fs_indir1_size = 0;
1710 ext4fs_indir1_blkno = -1;
1711 ext4fs_indir1_block = zalloc(blksz);
1712 if (ext4fs_indir1_block == NULL) {
1713 printf("** DI ext2fs read block (indir 2 1)"
1714 "malloc failed. **\n");
1717 ext4fs_indir1_size = blksz;
1719 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1720 log2_blksz) != ext4fs_indir1_blkno) {
1722 ext4fs_devread((lbaint_t)le32_to_cpu
1724 double_indir_block) << log2_blksz,
1726 (char *)ext4fs_indir1_block);
1728 printf("** DI ext2fs read block (indir 2 1)"
1732 ext4fs_indir1_blkno =
1733 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1737 if (ext4fs_indir2_block == NULL) {
1738 ext4fs_indir2_block = zalloc(blksz);
1739 if (ext4fs_indir2_block == NULL) {
1740 printf("** DI ext2fs read block (indir 2 2)"
1741 "malloc failed. **\n");
1744 ext4fs_indir2_size = blksz;
1745 ext4fs_indir2_blkno = -1;
1747 if (blksz != ext4fs_indir2_size) {
1748 free(ext4fs_indir2_block);
1749 ext4fs_indir2_block = NULL;
1750 ext4fs_indir2_size = 0;
1751 ext4fs_indir2_blkno = -1;
1752 ext4fs_indir2_block = zalloc(blksz);
1753 if (ext4fs_indir2_block == NULL) {
1754 printf("** DI ext2fs read block (indir 2 2)"
1755 "malloc failed. **\n");
1758 ext4fs_indir2_size = blksz;
1760 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1761 log2_blksz) != ext4fs_indir2_blkno) {
1762 status = ext4fs_devread((lbaint_t)le32_to_cpu
1763 (ext4fs_indir1_block
1765 perblock]) << log2_blksz, 0,
1767 (char *)ext4fs_indir2_block);
1769 printf("** DI ext2fs read block (indir 2 2)"
1773 ext4fs_indir2_blkno =
1774 le32_to_cpu(ext4fs_indir1_block[rblock
1779 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1781 /* Tripple indirect. */
1783 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1784 (blksz / 4 * blksz / 4));
1785 perblock_child = blksz / 4;
1786 perblock_parent = ((blksz / 4) * (blksz / 4));
1788 if (ext4fs_indir1_block == NULL) {
1789 ext4fs_indir1_block = zalloc(blksz);
1790 if (ext4fs_indir1_block == NULL) {
1791 printf("** TI ext2fs read block (indir 2 1)"
1792 "malloc failed. **\n");
1795 ext4fs_indir1_size = blksz;
1796 ext4fs_indir1_blkno = -1;
1798 if (blksz != ext4fs_indir1_size) {
1799 free(ext4fs_indir1_block);
1800 ext4fs_indir1_block = NULL;
1801 ext4fs_indir1_size = 0;
1802 ext4fs_indir1_blkno = -1;
1803 ext4fs_indir1_block = zalloc(blksz);
1804 if (ext4fs_indir1_block == NULL) {
1805 printf("** TI ext2fs read block (indir 2 1)"
1806 "malloc failed. **\n");
1809 ext4fs_indir1_size = blksz;
1811 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1812 log2_blksz) != ext4fs_indir1_blkno) {
1813 status = ext4fs_devread
1815 le32_to_cpu(inode->b.blocks.triple_indir_block)
1816 << log2_blksz, 0, blksz,
1817 (char *)ext4fs_indir1_block);
1819 printf("** TI ext2fs read block (indir 2 1)"
1823 ext4fs_indir1_blkno =
1824 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1828 if (ext4fs_indir2_block == NULL) {
1829 ext4fs_indir2_block = zalloc(blksz);
1830 if (ext4fs_indir2_block == NULL) {
1831 printf("** TI ext2fs read block (indir 2 2)"
1832 "malloc failed. **\n");
1835 ext4fs_indir2_size = blksz;
1836 ext4fs_indir2_blkno = -1;
1838 if (blksz != ext4fs_indir2_size) {
1839 free(ext4fs_indir2_block);
1840 ext4fs_indir2_block = NULL;
1841 ext4fs_indir2_size = 0;
1842 ext4fs_indir2_blkno = -1;
1843 ext4fs_indir2_block = zalloc(blksz);
1844 if (ext4fs_indir2_block == NULL) {
1845 printf("** TI ext2fs read block (indir 2 2)"
1846 "malloc failed. **\n");
1849 ext4fs_indir2_size = blksz;
1851 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1852 perblock_parent]) <<
1854 != ext4fs_indir2_blkno) {
1855 status = ext4fs_devread((lbaint_t)le32_to_cpu
1856 (ext4fs_indir1_block
1858 perblock_parent]) <<
1859 log2_blksz, 0, blksz,
1860 (char *)ext4fs_indir2_block);
1862 printf("** TI ext2fs read block (indir 2 2)"
1866 ext4fs_indir2_blkno =
1867 le32_to_cpu(ext4fs_indir1_block[rblock /
1872 if (ext4fs_indir3_block == NULL) {
1873 ext4fs_indir3_block = zalloc(blksz);
1874 if (ext4fs_indir3_block == NULL) {
1875 printf("** TI ext2fs read block (indir 2 2)"
1876 "malloc failed. **\n");
1879 ext4fs_indir3_size = blksz;
1880 ext4fs_indir3_blkno = -1;
1882 if (blksz != ext4fs_indir3_size) {
1883 free(ext4fs_indir3_block);
1884 ext4fs_indir3_block = NULL;
1885 ext4fs_indir3_size = 0;
1886 ext4fs_indir3_blkno = -1;
1887 ext4fs_indir3_block = zalloc(blksz);
1888 if (ext4fs_indir3_block == NULL) {
1889 printf("** TI ext2fs read block (indir 2 2)"
1890 "malloc failed. **\n");
1893 ext4fs_indir3_size = blksz;
1895 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1898 log2_blksz) != ext4fs_indir3_blkno) {
1900 ext4fs_devread((lbaint_t)le32_to_cpu
1901 (ext4fs_indir2_block
1902 [(rblock / perblock_child)
1903 % (blksz / 4)]) << log2_blksz, 0,
1904 blksz, (char *)ext4fs_indir3_block);
1906 printf("** TI ext2fs read block (indir 2 2)"
1910 ext4fs_indir3_blkno =
1911 le32_to_cpu(ext4fs_indir2_block[(rblock /
1918 blknr = le32_to_cpu(ext4fs_indir3_block
1919 [rblock % perblock_child]);
1921 debug("read_allocated_block %ld\n", blknr);
1927 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1930 * This function assures that for a file with the same name but different size
1931 * the sequential store on the ext4 filesystem will be correct.
1933 * In this function the global data, responsible for internal representation
1934 * of the ext4 data are initialized to the reset state. Without this, during
1935 * replacement of the smaller file with the bigger truncation of new file was
1938 void ext4fs_reinit_global(void)
1940 if (ext4fs_indir1_block != NULL) {
1941 free(ext4fs_indir1_block);
1942 ext4fs_indir1_block = NULL;
1943 ext4fs_indir1_size = 0;
1944 ext4fs_indir1_blkno = -1;
1946 if (ext4fs_indir2_block != NULL) {
1947 free(ext4fs_indir2_block);
1948 ext4fs_indir2_block = NULL;
1949 ext4fs_indir2_size = 0;
1950 ext4fs_indir2_blkno = -1;
1952 if (ext4fs_indir3_block != NULL) {
1953 free(ext4fs_indir3_block);
1954 ext4fs_indir3_block = NULL;
1955 ext4fs_indir3_size = 0;
1956 ext4fs_indir3_blkno = -1;
1959 void ext4fs_close(void)
1961 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1962 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1965 if (ext4fs_root != NULL) {
1970 ext4fs_reinit_global();
1973 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1974 struct ext2fs_node **fnode, int *ftype)
1976 unsigned int fpos = 0;
1979 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1983 printf("Iterate dir %s\n", name);
1984 #endif /* of DEBUG */
1985 if (!diro->inode_read) {
1986 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1990 /* Search the file. */
1991 while (fpos < le32_to_cpu(diro->inode.size)) {
1992 struct ext2_dirent dirent;
1994 status = ext4fs_read_file(diro, fpos,
1995 sizeof(struct ext2_dirent),
1996 (char *)&dirent, &actread);
2000 if (dirent.direntlen == 0) {
2001 printf("Failed to iterate over directory %s\n", name);
2005 if (dirent.namelen != 0) {
2006 char filename[dirent.namelen + 1];
2007 struct ext2fs_node *fdiro;
2008 int type = FILETYPE_UNKNOWN;
2010 status = ext4fs_read_file(diro,
2012 sizeof(struct ext2_dirent),
2013 dirent.namelen, filename,
2018 fdiro = zalloc(sizeof(struct ext2fs_node));
2022 fdiro->data = diro->data;
2023 fdiro->ino = le32_to_cpu(dirent.inode);
2025 filename[dirent.namelen] = '\0';
2027 if (dirent.filetype != FILETYPE_UNKNOWN) {
2028 fdiro->inode_read = 0;
2030 if (dirent.filetype == FILETYPE_DIRECTORY)
2031 type = FILETYPE_DIRECTORY;
2032 else if (dirent.filetype == FILETYPE_SYMLINK)
2033 type = FILETYPE_SYMLINK;
2034 else if (dirent.filetype == FILETYPE_REG)
2035 type = FILETYPE_REG;
2037 status = ext4fs_read_inode(diro->data,
2045 fdiro->inode_read = 1;
2047 if ((le16_to_cpu(fdiro->inode.mode) &
2048 FILETYPE_INO_MASK) ==
2049 FILETYPE_INO_DIRECTORY) {
2050 type = FILETYPE_DIRECTORY;
2051 } else if ((le16_to_cpu(fdiro->inode.mode)
2052 & FILETYPE_INO_MASK) ==
2053 FILETYPE_INO_SYMLINK) {
2054 type = FILETYPE_SYMLINK;
2055 } else if ((le16_to_cpu(fdiro->inode.mode)
2056 & FILETYPE_INO_MASK) ==
2058 type = FILETYPE_REG;
2062 printf("iterate >%s<\n", filename);
2063 #endif /* of DEBUG */
2064 if ((name != NULL) && (fnode != NULL)
2065 && (ftype != NULL)) {
2066 if (strcmp(filename, name) == 0) {
2072 if (fdiro->inode_read == 0) {
2073 status = ext4fs_read_inode(diro->data,
2081 fdiro->inode_read = 1;
2084 case FILETYPE_DIRECTORY:
2087 case FILETYPE_SYMLINK:
2098 le32_to_cpu(fdiro->inode.size),
2103 fpos += le16_to_cpu(dirent.direntlen);
2108 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2111 struct ext2fs_node *diro = node;
2115 if (!diro->inode_read) {
2116 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2120 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2124 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2125 strncpy(symlink, diro->inode.b.symlink,
2126 le32_to_cpu(diro->inode.size));
2128 status = ext4fs_read_file(diro, 0,
2129 le32_to_cpu(diro->inode.size),
2131 if ((status < 0) || (actread == 0)) {
2136 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2140 static int ext4fs_find_file1(const char *currpath,
2141 struct ext2fs_node *currroot,
2142 struct ext2fs_node **currfound, int *foundtype)
2144 char fpath[strlen(currpath) + 1];
2148 int type = FILETYPE_DIRECTORY;
2149 struct ext2fs_node *currnode = currroot;
2150 struct ext2fs_node *oldnode = currroot;
2152 strncpy(fpath, currpath, strlen(currpath) + 1);
2154 /* Remove all leading slashes. */
2155 while (*name == '/')
2159 *currfound = currnode;
2166 /* Extract the actual part from the pathname. */
2167 next = strchr(name, '/');
2169 /* Remove all leading slashes. */
2170 while (*next == '/')
2174 if (type != FILETYPE_DIRECTORY) {
2175 ext4fs_free_node(currnode, currroot);
2181 /* Iterate over the directory. */
2182 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2189 /* Read in the symlink and follow it. */
2190 if (type == FILETYPE_SYMLINK) {
2193 /* Test if the symlink does not loop. */
2194 if (++symlinknest == 8) {
2195 ext4fs_free_node(currnode, currroot);
2196 ext4fs_free_node(oldnode, currroot);
2200 symlink = ext4fs_read_symlink(currnode);
2201 ext4fs_free_node(currnode, currroot);
2204 ext4fs_free_node(oldnode, currroot);
2208 debug("Got symlink >%s<\n", symlink);
2210 if (symlink[0] == '/') {
2211 ext4fs_free_node(oldnode, currroot);
2212 oldnode = &ext4fs_root->diropen;
2215 /* Lookup the node the symlink points to. */
2216 status = ext4fs_find_file1(symlink, oldnode,
2222 ext4fs_free_node(oldnode, currroot);
2227 ext4fs_free_node(oldnode, currroot);
2229 /* Found the node! */
2230 if (!next || *next == '\0') {
2231 *currfound = currnode;
2240 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2241 struct ext2fs_node **foundnode, int expecttype)
2244 int foundtype = FILETYPE_DIRECTORY;
2250 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2254 /* Check if the node that was found was of the expected type. */
2255 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2257 else if ((expecttype == FILETYPE_DIRECTORY)
2258 && (foundtype != expecttype))
2264 int ext4fs_open(const char *filename, loff_t *len)
2266 struct ext2fs_node *fdiro = NULL;
2269 if (ext4fs_root == NULL)
2273 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2278 if (!fdiro->inode_read) {
2279 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2284 *len = le32_to_cpu(fdiro->inode.size);
2285 ext4fs_file = fdiro;
2289 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2294 int ext4fs_mount(unsigned part_length)
2296 struct ext2_data *data;
2298 struct ext_filesystem *fs = get_fs();
2299 data = zalloc(SUPERBLOCK_SIZE);
2303 /* Read the superblock. */
2304 status = ext4_read_superblock((char *)&data->sblock);
2309 /* Make sure this is an ext2 filesystem. */
2310 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2314 * The 64bit feature was enabled when metadata_csum was enabled
2315 * and we do not support metadata_csum (and cannot reliably find
2316 * files when it is set. Refuse to mount.
2318 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
2319 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2323 if (le32_to_cpu(data->sblock.revision_level) == 0) {
2326 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2327 __le32_to_cpu(data->sblock.feature_compatibility),
2328 __le32_to_cpu(data->sblock.feature_incompat),
2329 __le32_to_cpu(data->sblock.feature_ro_compat));
2331 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2332 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2333 EXT4_FEATURE_INCOMPAT_64BIT ?
2334 le16_to_cpu(data->sblock.descriptor_size) : 32;
2337 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2338 le32_to_cpu(data->sblock.revision_level),
2339 fs->inodesz, fs->gdsize);
2341 data->diropen.data = data;
2342 data->diropen.ino = 2;
2343 data->diropen.inode_read = 1;
2344 data->inode = &data->diropen.inode;
2346 status = ext4fs_read_inode(data, 2, data->inode);
2354 printf("Failed to mount ext2 filesystem...\n");