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>
30 #include <linux/stat.h>
31 #include <linux/time.h>
32 #include <asm/byteorder.h>
33 #include "ext4_common.h"
35 struct ext2_data *ext4fs_root;
36 struct ext2fs_node *ext4fs_file;
37 __le32 *ext4fs_indir1_block;
38 int ext4fs_indir1_size;
39 int ext4fs_indir1_blkno = -1;
40 __le32 *ext4fs_indir2_block;
41 int ext4fs_indir2_size;
42 int ext4fs_indir2_blkno = -1;
44 __le32 *ext4fs_indir3_block;
45 int ext4fs_indir3_size;
46 int ext4fs_indir3_blkno = -1;
47 struct ext2_inode *g_parent_inode;
48 static int symlinknest;
50 #if defined(CONFIG_EXT4_WRITE)
51 struct ext2_block_group *ext4fs_get_group_descriptor
52 (const struct ext_filesystem *fs, uint32_t bg_idx)
54 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
57 static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
59 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
62 static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
64 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
65 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
68 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
69 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
72 static inline void ext4fs_bg_free_inodes_dec
73 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
75 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
77 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
80 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
82 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
85 static inline void ext4fs_bg_free_blocks_dec
86 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
88 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
90 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
93 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
95 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
98 static inline void ext4fs_bg_itable_unused_dec
99 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
101 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
102 if (fs->gdsize == 64)
103 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
106 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
107 if (fs->gdsize == 64)
108 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
111 uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
113 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
114 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
118 void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
120 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
121 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
124 uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
125 const struct ext_filesystem *fs)
127 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
128 if (fs->gdsize == 64)
129 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
134 uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
135 const struct ext_filesystem *fs)
137 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
138 if (fs->gdsize == 64)
139 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
143 static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
145 return le16_to_cpu(bg->bg_flags);
148 static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
151 bg->bg_flags = cpu_to_le16(flags);
154 /* Block number of the block bitmap */
155 uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
156 const struct ext_filesystem *fs)
158 uint64_t block_nr = le32_to_cpu(bg->block_id);
159 if (fs->gdsize == 64)
160 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
164 /* Block number of the inode bitmap */
165 uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
166 const struct ext_filesystem *fs)
168 uint64_t block_nr = le32_to_cpu(bg->inode_id);
169 if (fs->gdsize == 64)
170 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
175 /* Block number of the inode table */
176 uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
177 const struct ext_filesystem *fs)
179 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
180 if (fs->gdsize == 64)
182 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
186 #if defined(CONFIG_EXT4_WRITE)
187 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
189 uint32_t res = size / n;
196 void put_ext4(uint64_t off, const void *buf, uint32_t size)
200 unsigned char *temp_ptr = NULL;
201 struct ext_filesystem *fs = get_fs();
202 int log2blksz = fs->dev_desc->log2blksz;
203 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
205 startblock = off >> log2blksz;
206 startblock += part_offset;
207 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
209 if (fs->dev_desc == NULL)
212 if ((startblock + (size >> log2blksz)) >
213 (part_offset + fs->total_sect)) {
214 printf("part_offset is " LBAFU "\n", part_offset);
215 printf("total_sector is %llu\n", fs->total_sect);
216 printf("error: overflow occurs\n");
221 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
223 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
224 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
226 if (size >> log2blksz != 0) {
227 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
228 (unsigned long *)buf);
230 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
232 memcpy(temp_ptr, buf, size);
233 blk_dwrite(fs->dev_desc, startblock, 1,
234 (unsigned long *)sec_buf);
239 static int _get_new_inode_no(unsigned char *buffer)
241 struct ext_filesystem *fs = get_fs();
247 /* get the blocksize of the filesystem */
248 unsigned char *ptr = buffer;
249 while (*ptr == 255) {
252 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
256 for (j = 0; j < fs->blksz; j++) {
261 status = input & operand;
276 static int _get_new_blk_no(unsigned char *buffer)
281 unsigned char *ptr = buffer;
282 struct ext_filesystem *fs = get_fs();
284 while (*ptr == 255) {
287 if (count == (fs->blksz * 8))
291 if (fs->blksz == 1024)
294 for (i = 0; i <= 7; i++) {
296 if (*ptr & operand) {
307 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
309 int i, remainder, status;
310 unsigned char *ptr = buffer;
311 unsigned char operand;
313 remainder = blockno % 8;
314 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
316 i = i - (index * blocksize);
317 if (blocksize != 1024) {
319 operand = 1 << remainder;
320 status = *ptr & operand;
324 *ptr = *ptr | operand;
327 if (remainder == 0) {
332 operand = (1 << (remainder - 1));
334 status = *ptr & operand;
338 *ptr = *ptr | operand;
343 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
345 int i, remainder, status;
346 unsigned char *ptr = buffer;
347 unsigned char operand;
349 remainder = blockno % 8;
350 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
352 i = i - (index * blocksize);
353 if (blocksize != 1024) {
355 operand = (1 << remainder);
356 status = *ptr & operand;
358 *ptr = *ptr & ~(operand);
360 if (remainder == 0) {
365 operand = (1 << (remainder - 1));
367 status = *ptr & operand;
369 *ptr = *ptr & ~(operand);
373 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
375 int i, remainder, status;
376 unsigned char *ptr = buffer;
377 unsigned char operand;
379 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
381 remainder = inode_no % 8;
382 if (remainder == 0) {
387 operand = (1 << (remainder - 1));
389 status = *ptr & operand;
393 *ptr = *ptr | operand;
398 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
400 int i, remainder, status;
401 unsigned char *ptr = buffer;
402 unsigned char operand;
404 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
406 remainder = inode_no % 8;
407 if (remainder == 0) {
412 operand = (1 << (remainder - 1));
414 status = *ptr & operand;
416 *ptr = *ptr & ~(operand);
419 uint16_t ext4fs_checksum_update(uint32_t i)
421 struct ext2_block_group *desc;
422 struct ext_filesystem *fs = get_fs();
424 __le32 le32_i = cpu_to_le32(i);
426 desc = ext4fs_get_group_descriptor(fs, i);
427 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
428 int offset = offsetof(struct ext2_block_group, bg_checksum);
430 crc = crc16(~0, (__u8 *)fs->sb->unique_id,
431 sizeof(fs->sb->unique_id));
432 crc = crc16(crc, (__u8 *)&le32_i, sizeof(le32_i));
433 crc = crc16(crc, (__u8 *)desc, offset);
434 offset += sizeof(desc->bg_checksum); /* skip checksum */
435 assert(offset == sizeof(*desc));
436 if (offset < fs->gdsize) {
437 crc = crc16(crc, (__u8 *)desc + offset,
438 fs->gdsize - offset);
445 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
448 int sizeof_void_space;
449 int new_entry_byte_reqd;
450 short padding_factor = 0;
452 if (dir->namelen % 4 != 0)
453 padding_factor = 4 - (dir->namelen % 4);
455 dentry_length = sizeof(struct ext2_dirent) +
456 dir->namelen + padding_factor;
457 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
458 if (sizeof_void_space == 0)
462 if (strlen(filename) % 4 != 0)
463 padding_factor = 4 - (strlen(filename) % 4);
465 new_entry_byte_reqd = strlen(filename) +
466 sizeof(struct ext2_dirent) + padding_factor;
467 if (sizeof_void_space >= new_entry_byte_reqd) {
468 dir->direntlen = cpu_to_le16(dentry_length);
469 return sizeof_void_space;
475 int ext4fs_update_parent_dentry(char *filename, int file_type)
477 unsigned int *zero_buffer = NULL;
478 char *root_first_block_buffer = NULL;
480 long int first_block_no_of_root = 0;
482 unsigned int new_entry_byte_reqd;
483 int sizeof_void_space = 0;
487 struct ext_filesystem *fs = get_fs();
488 /* directory entry */
489 struct ext2_dirent *dir;
490 char *temp_dir = NULL;
493 uint32_t new_blockcnt;
494 uint32_t directory_blocks;
496 zero_buffer = zalloc(fs->blksz);
498 printf("No Memory\n");
501 root_first_block_buffer = zalloc(fs->blksz);
502 if (!root_first_block_buffer) {
504 printf("No Memory\n");
507 new_entry_byte_reqd = ROUND(strlen(filename) +
508 sizeof(struct ext2_dirent), 4);
510 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
511 LOG2_BLOCK_SIZE(ext4fs_root);
512 blk_idx = directory_blocks - 1;
515 /* read the block no allocated to a file */
516 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
518 if (first_block_no_of_root <= 0)
521 status = ext4fs_devread((lbaint_t)first_block_no_of_root
523 0, fs->blksz, root_first_block_buffer);
527 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
529 dir = (struct ext2_dirent *)root_first_block_buffer;
532 while (le16_to_cpu(dir->direntlen) > 0) {
533 unsigned short used_len = ROUND(dir->namelen +
534 sizeof(struct ext2_dirent), 4);
536 /* last entry of block */
537 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
539 /* check if new entry fits */
540 if ((used_len + new_entry_byte_reqd) <=
541 le16_to_cpu(dir->direntlen)) {
542 dir->direntlen = cpu_to_le16(used_len);
546 printf("Block full, trying previous\n");
550 printf("All blocks full: Allocate new\n");
552 if (le32_to_cpu(g_parent_inode->flags) &
554 printf("Directory uses extents\n");
557 if (directory_blocks >= INDIRECT_BLOCKS) {
558 printf("Directory exceeds limit\n");
561 new_blk_no = ext4fs_get_new_blk_no();
562 if (new_blk_no == -1) {
563 printf("no block left to assign\n");
566 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
567 g_parent_inode->b.blocks.
568 dir_blocks[directory_blocks] =
569 cpu_to_le32(new_blk_no);
571 new_size = le32_to_cpu(g_parent_inode->size);
572 new_size += fs->blksz;
573 g_parent_inode->size = cpu_to_le32(new_size);
575 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
576 new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE;
577 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
579 if (ext4fs_put_metadata
580 (root_first_block_buffer,
581 first_block_no_of_root))
587 templength = le16_to_cpu(dir->direntlen);
588 totalbytes = totalbytes + templength;
589 sizeof_void_space = check_void_in_dentry(dir, filename);
590 if (sizeof_void_space)
593 dir = (struct ext2_dirent *)((char *)dir + templength);
596 /* make a pointer ready for creating next directory entry */
597 templength = le16_to_cpu(dir->direntlen);
598 totalbytes = totalbytes + templength;
599 dir = (struct ext2_dirent *)((char *)dir + templength);
601 /* get the next available inode number */
602 inodeno = ext4fs_get_new_inode_no();
604 printf("no inode left to assign\n");
607 dir->inode = cpu_to_le32(inodeno);
608 if (sizeof_void_space)
609 dir->direntlen = cpu_to_le16(sizeof_void_space);
611 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
613 dir->namelen = strlen(filename);
614 dir->filetype = file_type;
615 temp_dir = (char *)dir;
616 temp_dir = temp_dir + sizeof(struct ext2_dirent);
617 memcpy(temp_dir, filename, strlen(filename));
619 /* update or write the 1st block of root inode */
620 if (ext4fs_put_metadata(root_first_block_buffer,
621 first_block_no_of_root))
626 free(root_first_block_buffer);
631 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
638 char *block_buffer = NULL;
639 struct ext2_dirent *dir = NULL;
640 struct ext_filesystem *fs = get_fs();
641 uint32_t directory_blocks;
644 directory_blocks = le32_to_cpu(parent_inode->size) >>
645 LOG2_BLOCK_SIZE(ext4fs_root);
647 block_buffer = zalloc(fs->blksz);
651 /* get the block no allocated to a file */
652 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
653 blknr = read_allocated_block(parent_inode, blk_idx, NULL);
657 /* read the directory block */
658 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
659 0, fs->blksz, (char *)block_buffer);
666 printf("Badly aligned ext2_dirent\n");
670 dir = (struct ext2_dirent *)(block_buffer + offset);
671 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
673 int direntlen = le16_to_cpu(dir->direntlen);
674 if (direntlen < sizeof(struct ext2_dirent))
677 if (dir->inode && (strlen(dirname) == dir->namelen) &&
678 (strncmp(dirname, direntname, dir->namelen) == 0)) {
679 inodeno = le32_to_cpu(dir->inode);
685 } while (offset < fs->blksz);
699 static int find_dir_depth(char *dirname)
701 char *token = strtok(dirname, "/");
703 while (token != NULL) {
704 token = strtok(NULL, "/");
707 return count + 1 + 1;
709 * for example for string /home/temp
710 * depth=home(1)+temp(1)+1 extra for NULL;
715 static int parse_path(char **arr, char *dirname)
717 char *token = strtok(dirname, "/");
721 arr[i] = zalloc(strlen("/") + 1);
724 memcpy(arr[i++], "/", strlen("/"));
726 /* add each path entry after root */
727 while (token != NULL) {
728 arr[i] = zalloc(strlen(token) + 1);
731 memcpy(arr[i++], token, strlen(token));
732 token = strtok(NULL, "/");
739 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
741 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
748 * Function: ext4fs_get_parent_inode_num
749 * Return Value: inode Number of the parent directory of file/Directory to be
751 * dirname : Input parmater, input path name of the file/directory to be created
752 * dname : Output parameter, to be filled with the name of the directory
753 * extracted from dirname
755 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
759 int matched_inode_no;
760 int result_inode_no = -1;
762 char *depth_dirname = NULL;
763 char *parse_dirname = NULL;
764 struct ext2_inode *parent_inode = NULL;
765 struct ext2_inode *first_inode = NULL;
766 struct ext2_inode temp_inode;
768 if (*dirname != '/') {
769 printf("Please supply Absolute path\n");
773 /* TODO: input validation make equivalent to linux */
774 depth_dirname = zalloc(strlen(dirname) + 1);
778 memcpy(depth_dirname, dirname, strlen(dirname));
779 depth = find_dir_depth(depth_dirname);
780 parse_dirname = zalloc(strlen(dirname) + 1);
783 memcpy(parse_dirname, dirname, strlen(dirname));
785 /* allocate memory for each directory level */
786 ptr = zalloc((depth) * sizeof(char *));
789 if (parse_path(ptr, parse_dirname))
791 parent_inode = zalloc(sizeof(struct ext2_inode));
794 first_inode = zalloc(sizeof(struct ext2_inode));
797 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
798 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
800 result_inode_no = EXT2_ROOT_INO;
801 for (i = 1; i < depth; i++) {
802 matched_inode_no = search_dir(parent_inode, ptr[i]);
803 if (matched_inode_no == -1) {
804 if (ptr[i + 1] == NULL && i == 1) {
805 result_inode_no = EXT2_ROOT_INO;
808 if (ptr[i + 1] == NULL)
810 printf("Invalid path\n");
811 result_inode_no = -1;
815 if (ptr[i + 1] != NULL) {
816 memset(parent_inode, '\0',
817 sizeof(struct ext2_inode));
818 if (ext4fs_iget(matched_inode_no,
820 result_inode_no = -1;
823 result_inode_no = matched_inode_no;
832 matched_inode_no = search_dir(first_inode, ptr[i]);
834 matched_inode_no = search_dir(parent_inode, ptr[i]);
836 if (matched_inode_no != -1) {
837 ext4fs_iget(matched_inode_no, &temp_inode);
838 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
839 printf("It is a Directory\n");
840 result_inode_no = -1;
845 if (strlen(ptr[i]) > 256) {
846 result_inode_no = -1;
849 memcpy(dname, ptr[i], strlen(ptr[i]));
856 for (i = 0; i < depth; i++) {
868 return result_inode_no;
871 static int unlink_filename(char *filename, unsigned int blknr)
876 char *block_buffer = NULL;
877 struct ext2_dirent *dir = NULL;
878 struct ext2_dirent *previous_dir;
879 struct ext_filesystem *fs = get_fs();
883 block_buffer = zalloc(fs->blksz);
887 /* read the directory block */
888 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
889 fs->blksz, block_buffer);
896 printf("Badly aligned ext2_dirent\n");
901 dir = (struct ext2_dirent *)(block_buffer + offset);
902 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
904 int direntlen = le16_to_cpu(dir->direntlen);
905 if (direntlen < sizeof(struct ext2_dirent))
908 if (dir->inode && (strlen(filename) == dir->namelen) &&
909 (strncmp(direntname, filename, dir->namelen) == 0)) {
910 inodeno = le32_to_cpu(dir->inode);
916 } while (offset < fs->blksz);
919 printf("file found, deleting\n");
920 if (ext4fs_log_journal(block_buffer, blknr))
924 /* merge dir entry with predecessor */
926 new_len = le16_to_cpu(previous_dir->direntlen);
927 new_len += le16_to_cpu(dir->direntlen);
928 previous_dir->direntlen = cpu_to_le16(new_len);
930 /* invalidate dir entry */
933 if (ext4fs_put_metadata(block_buffer, blknr))
943 int ext4fs_filename_unlink(char *filename)
948 uint32_t directory_blocks;
950 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
951 LOG2_BLOCK_SIZE(ext4fs_root);
953 /* read the block no allocated to a file */
954 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
955 blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
958 inodeno = unlink_filename(filename, blknr);
966 uint32_t ext4fs_get_new_blk_no(void)
972 static int prev_bg_bitmap_index = -1;
973 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
974 struct ext_filesystem *fs = get_fs();
975 char *journal_buffer = zalloc(fs->blksz);
976 char *zero_buffer = zalloc(fs->blksz);
977 if (!journal_buffer || !zero_buffer)
980 if (fs->first_pass_bbmap == 0) {
981 for (i = 0; i < fs->no_blkgrp; i++) {
982 struct ext2_block_group *bgd = NULL;
983 bgd = ext4fs_get_group_descriptor(fs, i);
984 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
985 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
986 uint64_t b_bitmap_blk =
987 ext4fs_bg_get_block_id(bgd, fs);
988 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
989 memcpy(fs->blk_bmaps[i], zero_buffer,
991 put_ext4(b_bitmap_blk * fs->blksz,
992 fs->blk_bmaps[i], fs->blksz);
993 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
994 ext4fs_bg_set_flags(bgd, bg_flags);
997 _get_new_blk_no(fs->blk_bmaps[i]);
998 if (fs->curr_blkno == -1)
999 /* block bitmap is completely filled */
1001 fs->curr_blkno = fs->curr_blkno +
1002 (i * fs->blksz * 8);
1003 fs->first_pass_bbmap++;
1004 ext4fs_bg_free_blocks_dec(bgd, fs);
1005 ext4fs_sb_free_blocks_dec(fs->sb);
1006 status = ext4fs_devread(b_bitmap_blk *
1012 if (ext4fs_log_journal(journal_buffer,
1017 debug("no space left on block group %d\n", i);
1025 /* get the blockbitmap index respective to blockno */
1026 bg_idx = fs->curr_blkno / blk_per_grp;
1027 if (fs->blksz == 1024) {
1028 remainder = fs->curr_blkno % blk_per_grp;
1034 * To skip completely filled block group bitmaps
1035 * Optimize the block allocation
1037 if (bg_idx >= fs->no_blkgrp)
1040 struct ext2_block_group *bgd = NULL;
1041 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1042 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
1043 debug("block group %u is full. Skipping\n", bg_idx);
1044 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1045 if (fs->blksz == 1024)
1046 fs->curr_blkno += 1;
1050 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1051 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1052 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
1053 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1054 put_ext4(b_bitmap_blk * fs->blksz,
1055 zero_buffer, fs->blksz);
1056 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1057 ext4fs_bg_set_flags(bgd, bg_flags);
1060 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1062 debug("going for restart for the block no %ld %u\n",
1063 fs->curr_blkno, bg_idx);
1068 /* journal backup */
1069 if (prev_bg_bitmap_index != bg_idx) {
1070 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
1071 0, fs->blksz, journal_buffer);
1074 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
1077 prev_bg_bitmap_index = bg_idx;
1079 ext4fs_bg_free_blocks_dec(bgd, fs);
1080 ext4fs_sb_free_blocks_dec(fs->sb);
1084 free(journal_buffer);
1087 return fs->curr_blkno;
1089 free(journal_buffer);
1095 int ext4fs_get_new_inode_no(void)
1099 unsigned int ibmap_idx;
1100 static int prev_inode_bitmap_index = -1;
1101 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1102 struct ext_filesystem *fs = get_fs();
1103 char *journal_buffer = zalloc(fs->blksz);
1104 char *zero_buffer = zalloc(fs->blksz);
1105 if (!journal_buffer || !zero_buffer)
1107 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1108 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
1110 if (fs->first_pass_ibmap == 0) {
1111 for (i = 0; i < fs->no_blkgrp; i++) {
1112 uint32_t free_inodes;
1113 struct ext2_block_group *bgd = NULL;
1114 bgd = ext4fs_get_group_descriptor(fs, i);
1115 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1117 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1118 uint64_t i_bitmap_blk =
1119 ext4fs_bg_get_inode_id(bgd, fs);
1121 bgd->bg_itable_unused = free_inodes;
1122 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1123 put_ext4(i_bitmap_blk * fs->blksz,
1124 zero_buffer, fs->blksz);
1125 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1126 ext4fs_bg_set_flags(bgd, bg_flags);
1127 memcpy(fs->inode_bmaps[i],
1128 zero_buffer, fs->blksz);
1131 _get_new_inode_no(fs->inode_bmaps[i]);
1132 if (fs->curr_inode_no == -1)
1133 /* inode bitmap is completely filled */
1135 fs->curr_inode_no = fs->curr_inode_no +
1136 (i * inodes_per_grp);
1137 fs->first_pass_ibmap++;
1138 ext4fs_bg_free_inodes_dec(bgd, fs);
1140 ext4fs_bg_itable_unused_dec(bgd, fs);
1141 ext4fs_sb_free_inodes_dec(fs->sb);
1142 status = ext4fs_devread(i_bitmap_blk *
1148 if (ext4fs_log_journal(journal_buffer,
1153 debug("no inode left on block group %d\n", i);
1158 fs->curr_inode_no++;
1159 /* get the blockbitmap index respective to blockno */
1160 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1161 struct ext2_block_group *bgd =
1162 ext4fs_get_group_descriptor(fs, ibmap_idx);
1163 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1164 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1166 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1167 put_ext4(i_bitmap_blk * fs->blksz,
1168 zero_buffer, fs->blksz);
1169 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1170 ext4fs_bg_set_flags(bgd, bg_flags);
1171 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1175 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1176 fs->inode_bmaps[ibmap_idx],
1178 debug("going for restart for the block no %d %u\n",
1179 fs->curr_inode_no, ibmap_idx);
1183 /* journal backup */
1184 if (prev_inode_bitmap_index != ibmap_idx) {
1185 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
1186 0, fs->blksz, journal_buffer);
1189 if (ext4fs_log_journal(journal_buffer,
1190 le32_to_cpu(bgd->inode_id)))
1192 prev_inode_bitmap_index = ibmap_idx;
1194 ext4fs_bg_free_inodes_dec(bgd, fs);
1196 bgd->bg_itable_unused = bgd->free_inodes;
1197 ext4fs_sb_free_inodes_dec(fs->sb);
1202 free(journal_buffer);
1205 return fs->curr_inode_no;
1207 free(journal_buffer);
1215 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1216 unsigned int *total_remaining_blocks,
1217 unsigned int *no_blks_reqd)
1221 long int actual_block_no;
1222 long int si_blockno;
1223 /* si :single indirect */
1224 __le32 *si_buffer = NULL;
1225 __le32 *si_start_addr = NULL;
1226 struct ext_filesystem *fs = get_fs();
1228 if (*total_remaining_blocks != 0) {
1229 si_buffer = zalloc(fs->blksz);
1231 printf("No Memory\n");
1234 si_start_addr = si_buffer;
1235 si_blockno = ext4fs_get_new_blk_no();
1236 if (si_blockno == -1) {
1237 printf("no block left to assign\n");
1241 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1243 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1244 0, fs->blksz, (char *)si_buffer);
1245 memset(si_buffer, '\0', fs->blksz);
1249 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1250 actual_block_no = ext4fs_get_new_blk_no();
1251 if (actual_block_no == -1) {
1252 printf("no block left to assign\n");
1255 *si_buffer = cpu_to_le32(actual_block_no);
1256 debug("SIAB %u: %u\n", *si_buffer,
1257 *total_remaining_blocks);
1260 (*total_remaining_blocks)--;
1261 if (*total_remaining_blocks == 0)
1265 /* write the block to disk */
1266 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1267 si_start_addr, fs->blksz);
1268 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1271 free(si_start_addr);
1274 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1275 unsigned int *total_remaining_blocks,
1276 unsigned int *no_blks_reqd)
1281 long int actual_block_no;
1282 /* di:double indirect */
1283 long int di_blockno_parent;
1284 long int di_blockno_child;
1285 __le32 *di_parent_buffer = NULL;
1286 __le32 *di_child_buff = NULL;
1287 __le32 *di_block_start_addr = NULL;
1288 __le32 *di_child_buff_start = NULL;
1289 struct ext_filesystem *fs = get_fs();
1291 if (*total_remaining_blocks != 0) {
1292 /* double indirect parent block connecting to inode */
1293 di_blockno_parent = ext4fs_get_new_blk_no();
1294 if (di_blockno_parent == -1) {
1295 printf("no block left to assign\n");
1298 di_parent_buffer = zalloc(fs->blksz);
1299 if (!di_parent_buffer)
1302 di_block_start_addr = di_parent_buffer;
1304 debug("DIPB %ld: %u\n", di_blockno_parent,
1305 *total_remaining_blocks);
1307 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1309 fs->blksz, (char *)di_parent_buffer);
1312 printf("%s: Device read error!\n", __func__);
1315 memset(di_parent_buffer, '\0', fs->blksz);
1318 * start:for each double indirect parent
1319 * block create one more block
1321 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1322 di_blockno_child = ext4fs_get_new_blk_no();
1323 if (di_blockno_child == -1) {
1324 printf("no block left to assign\n");
1327 di_child_buff = zalloc(fs->blksz);
1331 di_child_buff_start = di_child_buff;
1332 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1335 debug("DICB %ld: %u\n", di_blockno_child,
1336 *total_remaining_blocks);
1338 status = ext4fs_devread((lbaint_t)di_blockno_child *
1341 (char *)di_child_buff);
1344 printf("%s: Device read error!\n", __func__);
1347 memset(di_child_buff, '\0', fs->blksz);
1348 /* filling of actual datablocks for each child */
1349 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1350 actual_block_no = ext4fs_get_new_blk_no();
1351 if (actual_block_no == -1) {
1352 printf("no block left to assign\n");
1355 *di_child_buff = cpu_to_le32(actual_block_no);
1356 debug("DIAB %ld: %u\n", actual_block_no,
1357 *total_remaining_blocks);
1360 (*total_remaining_blocks)--;
1361 if (*total_remaining_blocks == 0)
1364 /* write the block table */
1365 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1366 di_child_buff_start, fs->blksz);
1367 free(di_child_buff_start);
1368 di_child_buff_start = NULL;
1370 if (*total_remaining_blocks == 0)
1373 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1374 di_block_start_addr, fs->blksz);
1375 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1378 free(di_block_start_addr);
1381 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1382 unsigned int *total_remaining_blocks,
1383 unsigned int *no_blks_reqd)
1388 long int actual_block_no;
1389 /* ti: Triple Indirect */
1390 long int ti_gp_blockno;
1391 long int ti_parent_blockno;
1392 long int ti_child_blockno;
1393 __le32 *ti_gp_buff = NULL;
1394 __le32 *ti_parent_buff = NULL;
1395 __le32 *ti_child_buff = NULL;
1396 __le32 *ti_gp_buff_start_addr = NULL;
1397 __le32 *ti_pbuff_start_addr = NULL;
1398 __le32 *ti_cbuff_start_addr = NULL;
1399 struct ext_filesystem *fs = get_fs();
1400 if (*total_remaining_blocks != 0) {
1401 /* triple indirect grand parent block connecting to inode */
1402 ti_gp_blockno = ext4fs_get_new_blk_no();
1403 if (ti_gp_blockno == -1) {
1404 printf("no block left to assign\n");
1407 ti_gp_buff = zalloc(fs->blksz);
1411 ti_gp_buff_start_addr = ti_gp_buff;
1413 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1414 *total_remaining_blocks);
1416 /* for each 4 byte grand parent entry create one more block */
1417 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1418 ti_parent_blockno = ext4fs_get_new_blk_no();
1419 if (ti_parent_blockno == -1) {
1420 printf("no block left to assign\n");
1423 ti_parent_buff = zalloc(fs->blksz);
1424 if (!ti_parent_buff)
1427 ti_pbuff_start_addr = ti_parent_buff;
1428 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1431 debug("TIPB %ld: %u\n", ti_parent_blockno,
1432 *total_remaining_blocks);
1434 /* for each 4 byte entry parent create one more block */
1435 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1436 ti_child_blockno = ext4fs_get_new_blk_no();
1437 if (ti_child_blockno == -1) {
1438 printf("no block left assign\n");
1441 ti_child_buff = zalloc(fs->blksz);
1445 ti_cbuff_start_addr = ti_child_buff;
1446 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1449 debug("TICB %ld: %u\n", ti_parent_blockno,
1450 *total_remaining_blocks);
1452 /* fill actual datablocks for each child */
1453 for (k = 0; k < (fs->blksz / sizeof(int));
1456 ext4fs_get_new_blk_no();
1457 if (actual_block_no == -1) {
1458 printf("no block left\n");
1459 free(ti_cbuff_start_addr);
1462 *ti_child_buff = cpu_to_le32(actual_block_no);
1463 debug("TIAB %ld: %u\n", actual_block_no,
1464 *total_remaining_blocks);
1467 (*total_remaining_blocks)--;
1468 if (*total_remaining_blocks == 0)
1471 /* write the child block */
1472 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1473 (uint64_t)fs->blksz)),
1474 ti_cbuff_start_addr, fs->blksz);
1475 free(ti_cbuff_start_addr);
1477 if (*total_remaining_blocks == 0)
1480 /* write the parent block */
1481 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1482 ti_pbuff_start_addr, fs->blksz);
1483 free(ti_pbuff_start_addr);
1485 if (*total_remaining_blocks == 0)
1488 /* write the grand parent block */
1489 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1490 ti_gp_buff_start_addr, fs->blksz);
1491 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1492 free(ti_gp_buff_start_addr);
1496 free(ti_pbuff_start_addr);
1498 free(ti_gp_buff_start_addr);
1501 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1502 unsigned int total_remaining_blocks,
1503 unsigned int *total_no_of_block)
1506 long int direct_blockno;
1507 unsigned int no_blks_reqd = 0;
1509 /* allocation of direct blocks */
1510 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1511 direct_blockno = ext4fs_get_new_blk_no();
1512 if (direct_blockno == -1) {
1513 printf("no block left to assign\n");
1516 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1517 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1519 total_remaining_blocks--;
1522 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1524 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1526 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1528 *total_no_of_block += no_blks_reqd;
1533 static struct ext4_extent_header *ext4fs_get_extent_block
1534 (struct ext2_data *data, struct ext_block_cache *cache,
1535 struct ext4_extent_header *ext_block,
1536 uint32_t fileblock, int log2_blksz)
1538 struct ext4_extent_idx *index;
1539 unsigned long long block;
1540 int blksz = EXT2_BLOCK_SIZE(data);
1544 index = (struct ext4_extent_idx *)(ext_block + 1);
1546 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1549 if (ext_block->eh_depth == 0)
1554 if (i >= le16_to_cpu(ext_block->eh_entries))
1556 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1559 * If first logical block number is higher than requested fileblock,
1560 * it is a sparse file. This is handled on upper layer.
1565 block = le16_to_cpu(index[i].ei_leaf_hi);
1566 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1567 block <<= log2_blksz;
1568 if (!ext_cache_read(cache, (lbaint_t)block, blksz))
1570 ext_block = (struct ext4_extent_header *)cache->buf;
1574 static int ext4fs_blockgroup
1575 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1578 unsigned int blkoff, desc_per_blk;
1579 int log2blksz = get_fs()->dev_desc->log2blksz;
1580 int desc_size = get_fs()->gdsize;
1584 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
1586 if (desc_per_blk == 0)
1588 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1589 group / desc_per_blk;
1590 blkoff = (group % desc_per_blk) * desc_size;
1592 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1593 group, blkno, blkoff);
1595 return ext4fs_devread((lbaint_t)blkno <<
1596 (LOG2_BLOCK_SIZE(data) - log2blksz),
1597 blkoff, desc_size, (char *)blkgrp);
1600 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1602 struct ext2_block_group *blkgrp;
1603 struct ext2_sblock *sblock = &data->sblock;
1604 struct ext_filesystem *fs = get_fs();
1605 int log2blksz = get_fs()->dev_desc->log2blksz;
1606 int inodes_per_block, status;
1608 unsigned int blkoff;
1610 /* Allocate blkgrp based on gdsize (for 64-bit support). */
1611 blkgrp = zalloc(get_fs()->gdsize);
1615 /* It is easier to calculate if the first inode is 0. */
1617 if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
1621 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1622 (sblock->inodes_per_group), blkgrp);
1628 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1629 if ( inodes_per_block == 0 ) {
1633 blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
1634 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1635 blkoff = (ino % inodes_per_block) * fs->inodesz;
1637 /* Free blkgrp as it is no longer required. */
1640 /* Read the inode. */
1641 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1643 sizeof(struct ext2_inode), (char *)inode);
1650 long int read_allocated_block(struct ext2_inode *inode, int fileblock,
1651 struct ext_block_cache *cache)
1658 long int perblock_parent;
1659 long int perblock_child;
1660 unsigned long long start;
1661 /* get the blocksize of the filesystem */
1662 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1663 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1664 - get_fs()->dev_desc->log2blksz;
1666 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1667 long int startblock, endblock;
1668 struct ext_block_cache *c, cd;
1669 struct ext4_extent_header *ext_block;
1670 struct ext4_extent *extent;
1680 ext4fs_get_extent_block(ext4fs_root, c,
1681 (struct ext4_extent_header *)
1682 inode->b.blocks.dir_blocks,
1683 fileblock, log2_blksz);
1685 printf("invalid extent block\n");
1691 extent = (struct ext4_extent *)(ext_block + 1);
1693 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1694 startblock = le32_to_cpu(extent[i].ee_block);
1695 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1697 if (startblock > fileblock) {
1703 } else if (fileblock < endblock) {
1704 start = le16_to_cpu(extent[i].ee_start_hi);
1705 start = (start << 32) +
1706 le32_to_cpu(extent[i].ee_start_lo);
1709 return (fileblock - startblock) + start;
1718 /* Direct blocks. */
1719 if (fileblock < INDIRECT_BLOCKS)
1720 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1723 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1724 if (ext4fs_indir1_block == NULL) {
1725 ext4fs_indir1_block = zalloc(blksz);
1726 if (ext4fs_indir1_block == NULL) {
1727 printf("** SI ext2fs read block (indir 1)"
1728 "malloc failed. **\n");
1731 ext4fs_indir1_size = blksz;
1732 ext4fs_indir1_blkno = -1;
1734 if (blksz != ext4fs_indir1_size) {
1735 free(ext4fs_indir1_block);
1736 ext4fs_indir1_block = NULL;
1737 ext4fs_indir1_size = 0;
1738 ext4fs_indir1_blkno = -1;
1739 ext4fs_indir1_block = zalloc(blksz);
1740 if (ext4fs_indir1_block == NULL) {
1741 printf("** SI ext2fs read block (indir 1):"
1742 "malloc failed. **\n");
1745 ext4fs_indir1_size = blksz;
1747 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1748 log2_blksz) != ext4fs_indir1_blkno) {
1750 ext4fs_devread((lbaint_t)le32_to_cpu
1752 indir_block) << log2_blksz, 0,
1753 blksz, (char *)ext4fs_indir1_block);
1755 printf("** SI ext2fs read block (indir 1)"
1759 ext4fs_indir1_blkno =
1760 le32_to_cpu(inode->b.blocks.
1761 indir_block) << log2_blksz;
1763 blknr = le32_to_cpu(ext4fs_indir1_block
1764 [fileblock - INDIRECT_BLOCKS]);
1766 /* Double indirect. */
1767 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1768 (blksz / 4 + 1)))) {
1770 long int perblock = blksz / 4;
1771 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1773 if (ext4fs_indir1_block == NULL) {
1774 ext4fs_indir1_block = zalloc(blksz);
1775 if (ext4fs_indir1_block == NULL) {
1776 printf("** DI ext2fs read block (indir 2 1)"
1777 "malloc failed. **\n");
1780 ext4fs_indir1_size = blksz;
1781 ext4fs_indir1_blkno = -1;
1783 if (blksz != ext4fs_indir1_size) {
1784 free(ext4fs_indir1_block);
1785 ext4fs_indir1_block = NULL;
1786 ext4fs_indir1_size = 0;
1787 ext4fs_indir1_blkno = -1;
1788 ext4fs_indir1_block = zalloc(blksz);
1789 if (ext4fs_indir1_block == NULL) {
1790 printf("** DI ext2fs read block (indir 2 1)"
1791 "malloc failed. **\n");
1794 ext4fs_indir1_size = blksz;
1796 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1797 log2_blksz) != ext4fs_indir1_blkno) {
1799 ext4fs_devread((lbaint_t)le32_to_cpu
1801 double_indir_block) << log2_blksz,
1803 (char *)ext4fs_indir1_block);
1805 printf("** DI ext2fs read block (indir 2 1)"
1809 ext4fs_indir1_blkno =
1810 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1814 if (ext4fs_indir2_block == NULL) {
1815 ext4fs_indir2_block = zalloc(blksz);
1816 if (ext4fs_indir2_block == NULL) {
1817 printf("** DI ext2fs read block (indir 2 2)"
1818 "malloc failed. **\n");
1821 ext4fs_indir2_size = blksz;
1822 ext4fs_indir2_blkno = -1;
1824 if (blksz != ext4fs_indir2_size) {
1825 free(ext4fs_indir2_block);
1826 ext4fs_indir2_block = NULL;
1827 ext4fs_indir2_size = 0;
1828 ext4fs_indir2_blkno = -1;
1829 ext4fs_indir2_block = zalloc(blksz);
1830 if (ext4fs_indir2_block == NULL) {
1831 printf("** DI ext2fs read block (indir 2 2)"
1832 "malloc failed. **\n");
1835 ext4fs_indir2_size = blksz;
1837 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1838 log2_blksz) != ext4fs_indir2_blkno) {
1839 status = ext4fs_devread((lbaint_t)le32_to_cpu
1840 (ext4fs_indir1_block
1842 perblock]) << log2_blksz, 0,
1844 (char *)ext4fs_indir2_block);
1846 printf("** DI ext2fs read block (indir 2 2)"
1850 ext4fs_indir2_blkno =
1851 le32_to_cpu(ext4fs_indir1_block[rblock
1856 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1858 /* Tripple indirect. */
1860 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1861 (blksz / 4 * blksz / 4));
1862 perblock_child = blksz / 4;
1863 perblock_parent = ((blksz / 4) * (blksz / 4));
1865 if (ext4fs_indir1_block == NULL) {
1866 ext4fs_indir1_block = zalloc(blksz);
1867 if (ext4fs_indir1_block == NULL) {
1868 printf("** TI ext2fs read block (indir 2 1)"
1869 "malloc failed. **\n");
1872 ext4fs_indir1_size = blksz;
1873 ext4fs_indir1_blkno = -1;
1875 if (blksz != ext4fs_indir1_size) {
1876 free(ext4fs_indir1_block);
1877 ext4fs_indir1_block = NULL;
1878 ext4fs_indir1_size = 0;
1879 ext4fs_indir1_blkno = -1;
1880 ext4fs_indir1_block = zalloc(blksz);
1881 if (ext4fs_indir1_block == NULL) {
1882 printf("** TI ext2fs read block (indir 2 1)"
1883 "malloc failed. **\n");
1886 ext4fs_indir1_size = blksz;
1888 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1889 log2_blksz) != ext4fs_indir1_blkno) {
1890 status = ext4fs_devread
1892 le32_to_cpu(inode->b.blocks.triple_indir_block)
1893 << log2_blksz, 0, blksz,
1894 (char *)ext4fs_indir1_block);
1896 printf("** TI ext2fs read block (indir 2 1)"
1900 ext4fs_indir1_blkno =
1901 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1905 if (ext4fs_indir2_block == NULL) {
1906 ext4fs_indir2_block = zalloc(blksz);
1907 if (ext4fs_indir2_block == NULL) {
1908 printf("** TI ext2fs read block (indir 2 2)"
1909 "malloc failed. **\n");
1912 ext4fs_indir2_size = blksz;
1913 ext4fs_indir2_blkno = -1;
1915 if (blksz != ext4fs_indir2_size) {
1916 free(ext4fs_indir2_block);
1917 ext4fs_indir2_block = NULL;
1918 ext4fs_indir2_size = 0;
1919 ext4fs_indir2_blkno = -1;
1920 ext4fs_indir2_block = zalloc(blksz);
1921 if (ext4fs_indir2_block == NULL) {
1922 printf("** TI ext2fs read block (indir 2 2)"
1923 "malloc failed. **\n");
1926 ext4fs_indir2_size = blksz;
1928 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1929 perblock_parent]) <<
1931 != ext4fs_indir2_blkno) {
1932 status = ext4fs_devread((lbaint_t)le32_to_cpu
1933 (ext4fs_indir1_block
1935 perblock_parent]) <<
1936 log2_blksz, 0, blksz,
1937 (char *)ext4fs_indir2_block);
1939 printf("** TI ext2fs read block (indir 2 2)"
1943 ext4fs_indir2_blkno =
1944 le32_to_cpu(ext4fs_indir1_block[rblock /
1949 if (ext4fs_indir3_block == NULL) {
1950 ext4fs_indir3_block = zalloc(blksz);
1951 if (ext4fs_indir3_block == NULL) {
1952 printf("** TI ext2fs read block (indir 2 2)"
1953 "malloc failed. **\n");
1956 ext4fs_indir3_size = blksz;
1957 ext4fs_indir3_blkno = -1;
1959 if (blksz != ext4fs_indir3_size) {
1960 free(ext4fs_indir3_block);
1961 ext4fs_indir3_block = NULL;
1962 ext4fs_indir3_size = 0;
1963 ext4fs_indir3_blkno = -1;
1964 ext4fs_indir3_block = zalloc(blksz);
1965 if (ext4fs_indir3_block == NULL) {
1966 printf("** TI ext2fs read block (indir 2 2)"
1967 "malloc failed. **\n");
1970 ext4fs_indir3_size = blksz;
1972 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1975 log2_blksz) != ext4fs_indir3_blkno) {
1977 ext4fs_devread((lbaint_t)le32_to_cpu
1978 (ext4fs_indir2_block
1979 [(rblock / perblock_child)
1980 % (blksz / 4)]) << log2_blksz, 0,
1981 blksz, (char *)ext4fs_indir3_block);
1983 printf("** TI ext2fs read block (indir 2 2)"
1987 ext4fs_indir3_blkno =
1988 le32_to_cpu(ext4fs_indir2_block[(rblock /
1995 blknr = le32_to_cpu(ext4fs_indir3_block
1996 [rblock % perblock_child]);
1998 debug("read_allocated_block %ld\n", blknr);
2004 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
2007 * This function assures that for a file with the same name but different size
2008 * the sequential store on the ext4 filesystem will be correct.
2010 * In this function the global data, responsible for internal representation
2011 * of the ext4 data are initialized to the reset state. Without this, during
2012 * replacement of the smaller file with the bigger truncation of new file was
2015 void ext4fs_reinit_global(void)
2017 if (ext4fs_indir1_block != NULL) {
2018 free(ext4fs_indir1_block);
2019 ext4fs_indir1_block = NULL;
2020 ext4fs_indir1_size = 0;
2021 ext4fs_indir1_blkno = -1;
2023 if (ext4fs_indir2_block != NULL) {
2024 free(ext4fs_indir2_block);
2025 ext4fs_indir2_block = NULL;
2026 ext4fs_indir2_size = 0;
2027 ext4fs_indir2_blkno = -1;
2029 if (ext4fs_indir3_block != NULL) {
2030 free(ext4fs_indir3_block);
2031 ext4fs_indir3_block = NULL;
2032 ext4fs_indir3_size = 0;
2033 ext4fs_indir3_blkno = -1;
2036 void ext4fs_close(void)
2038 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
2039 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
2042 if (ext4fs_root != NULL) {
2047 ext4fs_reinit_global();
2050 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2051 struct ext2fs_node **fnode, int *ftype)
2053 unsigned int fpos = 0;
2056 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2060 printf("Iterate dir %s\n", name);
2061 #endif /* of DEBUG */
2062 if (!diro->inode_read) {
2063 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2067 /* Search the file. */
2068 while (fpos < le32_to_cpu(diro->inode.size)) {
2069 struct ext2_dirent dirent;
2071 status = ext4fs_read_file(diro, fpos,
2072 sizeof(struct ext2_dirent),
2073 (char *)&dirent, &actread);
2077 if (dirent.direntlen == 0) {
2078 printf("Failed to iterate over directory %s\n", name);
2082 if (dirent.namelen != 0) {
2083 char filename[dirent.namelen + 1];
2084 struct ext2fs_node *fdiro;
2085 int type = FILETYPE_UNKNOWN;
2087 status = ext4fs_read_file(diro,
2089 sizeof(struct ext2_dirent),
2090 dirent.namelen, filename,
2095 fdiro = zalloc(sizeof(struct ext2fs_node));
2099 fdiro->data = diro->data;
2100 fdiro->ino = le32_to_cpu(dirent.inode);
2102 filename[dirent.namelen] = '\0';
2104 if (dirent.filetype != FILETYPE_UNKNOWN) {
2105 fdiro->inode_read = 0;
2107 if (dirent.filetype == FILETYPE_DIRECTORY)
2108 type = FILETYPE_DIRECTORY;
2109 else if (dirent.filetype == FILETYPE_SYMLINK)
2110 type = FILETYPE_SYMLINK;
2111 else if (dirent.filetype == FILETYPE_REG)
2112 type = FILETYPE_REG;
2114 status = ext4fs_read_inode(diro->data,
2122 fdiro->inode_read = 1;
2124 if ((le16_to_cpu(fdiro->inode.mode) &
2125 FILETYPE_INO_MASK) ==
2126 FILETYPE_INO_DIRECTORY) {
2127 type = FILETYPE_DIRECTORY;
2128 } else if ((le16_to_cpu(fdiro->inode.mode)
2129 & FILETYPE_INO_MASK) ==
2130 FILETYPE_INO_SYMLINK) {
2131 type = FILETYPE_SYMLINK;
2132 } else if ((le16_to_cpu(fdiro->inode.mode)
2133 & FILETYPE_INO_MASK) ==
2135 type = FILETYPE_REG;
2139 printf("iterate >%s<\n", filename);
2140 #endif /* of DEBUG */
2141 if ((name != NULL) && (fnode != NULL)
2142 && (ftype != NULL)) {
2143 if (strcmp(filename, name) == 0) {
2149 if (fdiro->inode_read == 0) {
2150 status = ext4fs_read_inode(diro->data,
2158 fdiro->inode_read = 1;
2161 case FILETYPE_DIRECTORY:
2164 case FILETYPE_SYMLINK:
2175 le32_to_cpu(fdiro->inode.size),
2180 fpos += le16_to_cpu(dirent.direntlen);
2185 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2188 struct ext2fs_node *diro = node;
2192 if (!diro->inode_read) {
2193 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2197 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2201 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2202 strncpy(symlink, diro->inode.b.symlink,
2203 le32_to_cpu(diro->inode.size));
2205 status = ext4fs_read_file(diro, 0,
2206 le32_to_cpu(diro->inode.size),
2208 if ((status < 0) || (actread == 0)) {
2213 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2217 static int ext4fs_find_file1(const char *currpath,
2218 struct ext2fs_node *currroot,
2219 struct ext2fs_node **currfound, int *foundtype)
2221 char fpath[strlen(currpath) + 1];
2225 int type = FILETYPE_DIRECTORY;
2226 struct ext2fs_node *currnode = currroot;
2227 struct ext2fs_node *oldnode = currroot;
2229 strncpy(fpath, currpath, strlen(currpath) + 1);
2231 /* Remove all leading slashes. */
2232 while (*name == '/')
2236 *currfound = currnode;
2243 /* Extract the actual part from the pathname. */
2244 next = strchr(name, '/');
2246 /* Remove all leading slashes. */
2247 while (*next == '/')
2251 if (type != FILETYPE_DIRECTORY) {
2252 ext4fs_free_node(currnode, currroot);
2258 /* Iterate over the directory. */
2259 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2266 /* Read in the symlink and follow it. */
2267 if (type == FILETYPE_SYMLINK) {
2270 /* Test if the symlink does not loop. */
2271 if (++symlinknest == 8) {
2272 ext4fs_free_node(currnode, currroot);
2273 ext4fs_free_node(oldnode, currroot);
2277 symlink = ext4fs_read_symlink(currnode);
2278 ext4fs_free_node(currnode, currroot);
2281 ext4fs_free_node(oldnode, currroot);
2285 debug("Got symlink >%s<\n", symlink);
2287 if (symlink[0] == '/') {
2288 ext4fs_free_node(oldnode, currroot);
2289 oldnode = &ext4fs_root->diropen;
2292 /* Lookup the node the symlink points to. */
2293 status = ext4fs_find_file1(symlink, oldnode,
2299 ext4fs_free_node(oldnode, currroot);
2304 ext4fs_free_node(oldnode, currroot);
2306 /* Found the node! */
2307 if (!next || *next == '\0') {
2308 *currfound = currnode;
2317 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2318 struct ext2fs_node **foundnode, int expecttype)
2321 int foundtype = FILETYPE_DIRECTORY;
2327 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2331 /* Check if the node that was found was of the expected type. */
2332 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2334 else if ((expecttype == FILETYPE_DIRECTORY)
2335 && (foundtype != expecttype))
2341 int ext4fs_open(const char *filename, loff_t *len)
2343 struct ext2fs_node *fdiro = NULL;
2346 if (ext4fs_root == NULL)
2350 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2355 if (!fdiro->inode_read) {
2356 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2361 *len = le32_to_cpu(fdiro->inode.size);
2362 ext4fs_file = fdiro;
2366 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2371 int ext4fs_mount(unsigned part_length)
2373 struct ext2_data *data;
2375 struct ext_filesystem *fs = get_fs();
2377 if (part_length < SUPERBLOCK_SIZE)
2380 data = zalloc(SUPERBLOCK_SIZE);
2384 /* Read the superblock. */
2385 status = ext4_read_superblock((char *)&data->sblock);
2390 /* Make sure this is an ext2 filesystem. */
2391 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2395 if (le32_to_cpu(data->sblock.revision_level) == 0) {
2399 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2400 __le32_to_cpu(data->sblock.feature_compatibility),
2401 __le32_to_cpu(data->sblock.feature_incompat),
2402 __le32_to_cpu(data->sblock.feature_ro_compat));
2404 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2405 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2406 EXT4_FEATURE_INCOMPAT_64BIT ?
2407 le16_to_cpu(data->sblock.descriptor_size) : 32;
2410 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2411 le32_to_cpu(data->sblock.revision_level),
2412 fs->inodesz, fs->gdsize);
2414 data->diropen.data = data;
2415 data->diropen.ino = 2;
2416 data->diropen.inode_read = 1;
2417 data->inode = &data->diropen.inode;
2419 status = ext4fs_read_inode(data, 2, data->inode);
2427 log_debug("Failed to mount ext2 filesystem...\n");