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 = (struct ext2_block_group *)&fs->bgd[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)
936 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
938 if (fs->first_pass_bbmap == 0) {
939 for (i = 0; i < fs->no_blkgrp; i++) {
940 if (le16_to_cpu(bgd[i].free_blocks)) {
941 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
943 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
944 zero_buffer, fs->blksz);
945 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
946 bgd[i].bg_flags = cpu_to_le16(new_flags);
947 memcpy(fs->blk_bmaps[i], zero_buffer,
951 _get_new_blk_no(fs->blk_bmaps[i]);
952 if (fs->curr_blkno == -1)
953 /* if block bitmap is completely fill */
955 fs->curr_blkno = fs->curr_blkno +
957 fs->first_pass_bbmap++;
958 ext4fs_bg_free_blocks_dec(&bgd[i]);
959 ext4fs_sb_free_blocks_dec(fs->sb);
960 status = ext4fs_devread(
961 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
967 if (ext4fs_log_journal(journal_buffer,
968 le32_to_cpu(bgd[i].block_id)))
972 debug("no space left on block group %d\n", i);
980 /* get the blockbitmap index respective to blockno */
981 bg_idx = fs->curr_blkno / blk_per_grp;
982 if (fs->blksz == 1024) {
983 remainder = fs->curr_blkno % blk_per_grp;
989 * To skip completely filled block group bitmaps
990 * Optimize the block allocation
992 if (bg_idx >= fs->no_blkgrp)
995 if (bgd[bg_idx].free_blocks == 0) {
996 debug("block group %u is full. Skipping\n", bg_idx);
997 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
998 if (fs->blksz == 1024)
1003 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
1005 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
1006 zero_buffer, fs->blksz);
1007 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1008 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
1009 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
1012 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1014 debug("going for restart for the block no %ld %u\n",
1015 fs->curr_blkno, bg_idx);
1020 /* journal backup */
1021 if (prev_bg_bitmap_index != bg_idx) {
1022 status = ext4fs_devread(
1023 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
1025 0, fs->blksz, journal_buffer);
1028 if (ext4fs_log_journal(journal_buffer,
1029 le32_to_cpu(bgd[bg_idx].block_id)))
1032 prev_bg_bitmap_index = bg_idx;
1034 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
1035 ext4fs_sb_free_blocks_dec(fs->sb);
1039 free(journal_buffer);
1042 return fs->curr_blkno;
1044 free(journal_buffer);
1050 int ext4fs_get_new_inode_no(void)
1054 unsigned int ibmap_idx;
1055 static int prev_inode_bitmap_index = -1;
1056 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1057 struct ext_filesystem *fs = get_fs();
1058 char *journal_buffer = zalloc(fs->blksz);
1059 char *zero_buffer = zalloc(fs->blksz);
1060 if (!journal_buffer || !zero_buffer)
1062 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
1063 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1064 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
1066 if (fs->first_pass_ibmap == 0) {
1067 for (i = 0; i < fs->no_blkgrp; i++) {
1068 if (bgd[i].free_inodes) {
1070 bgd[i].bg_itable_unused =
1072 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1074 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
1075 zero_buffer, fs->blksz);
1076 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1077 bgd[i].bg_flags = cpu_to_le16(new_flags);
1078 memcpy(fs->inode_bmaps[i],
1079 zero_buffer, fs->blksz);
1082 _get_new_inode_no(fs->inode_bmaps[i]);
1083 if (fs->curr_inode_no == -1)
1084 /* if block bitmap is completely fill */
1086 fs->curr_inode_no = fs->curr_inode_no +
1087 (i * inodes_per_grp);
1088 fs->first_pass_ibmap++;
1089 ext4fs_bg_free_inodes_dec(&bgd[i]);
1091 ext4fs_bg_itable_unused_dec(&bgd[i]);
1092 ext4fs_sb_free_inodes_dec(fs->sb);
1093 status = ext4fs_devread(
1094 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
1100 if (ext4fs_log_journal(journal_buffer,
1101 le32_to_cpu(bgd[i].inode_id)))
1105 debug("no inode left on block group %d\n", i);
1110 fs->curr_inode_no++;
1111 /* get the blockbitmap index respective to blockno */
1112 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1113 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1115 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1116 zero_buffer, fs->blksz);
1117 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1118 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
1119 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1123 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1124 fs->inode_bmaps[ibmap_idx],
1126 debug("going for restart for the block no %d %u\n",
1127 fs->curr_inode_no, ibmap_idx);
1131 /* journal backup */
1132 if (prev_inode_bitmap_index != ibmap_idx) {
1133 memset(journal_buffer, '\0', fs->blksz);
1134 status = ext4fs_devread(
1135 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
1137 0, fs->blksz, journal_buffer);
1140 if (ext4fs_log_journal(journal_buffer,
1141 le32_to_cpu(bgd[ibmap_idx].inode_id)))
1143 prev_inode_bitmap_index = ibmap_idx;
1145 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1147 bgd[ibmap_idx].bg_itable_unused =
1148 bgd[ibmap_idx].free_inodes;
1149 ext4fs_sb_free_inodes_dec(fs->sb);
1154 free(journal_buffer);
1157 return fs->curr_inode_no;
1159 free(journal_buffer);
1167 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1168 unsigned int *total_remaining_blocks,
1169 unsigned int *no_blks_reqd)
1173 long int actual_block_no;
1174 long int si_blockno;
1175 /* si :single indirect */
1176 __le32 *si_buffer = NULL;
1177 __le32 *si_start_addr = NULL;
1178 struct ext_filesystem *fs = get_fs();
1180 if (*total_remaining_blocks != 0) {
1181 si_buffer = zalloc(fs->blksz);
1183 printf("No Memory\n");
1186 si_start_addr = si_buffer;
1187 si_blockno = ext4fs_get_new_blk_no();
1188 if (si_blockno == -1) {
1189 printf("no block left to assign\n");
1193 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1195 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1196 0, fs->blksz, (char *)si_buffer);
1197 memset(si_buffer, '\0', fs->blksz);
1201 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1202 actual_block_no = ext4fs_get_new_blk_no();
1203 if (actual_block_no == -1) {
1204 printf("no block left to assign\n");
1207 *si_buffer = cpu_to_le32(actual_block_no);
1208 debug("SIAB %u: %u\n", *si_buffer,
1209 *total_remaining_blocks);
1212 (*total_remaining_blocks)--;
1213 if (*total_remaining_blocks == 0)
1217 /* write the block to disk */
1218 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1219 si_start_addr, fs->blksz);
1220 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1223 free(si_start_addr);
1226 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1227 unsigned int *total_remaining_blocks,
1228 unsigned int *no_blks_reqd)
1233 long int actual_block_no;
1234 /* di:double indirect */
1235 long int di_blockno_parent;
1236 long int di_blockno_child;
1237 __le32 *di_parent_buffer = NULL;
1238 __le32 *di_child_buff = NULL;
1239 __le32 *di_block_start_addr = NULL;
1240 __le32 *di_child_buff_start = NULL;
1241 struct ext_filesystem *fs = get_fs();
1243 if (*total_remaining_blocks != 0) {
1244 /* double indirect parent block connecting to inode */
1245 di_blockno_parent = ext4fs_get_new_blk_no();
1246 if (di_blockno_parent == -1) {
1247 printf("no block left to assign\n");
1250 di_parent_buffer = zalloc(fs->blksz);
1251 if (!di_parent_buffer)
1254 di_block_start_addr = di_parent_buffer;
1256 debug("DIPB %ld: %u\n", di_blockno_parent,
1257 *total_remaining_blocks);
1259 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1261 fs->blksz, (char *)di_parent_buffer);
1264 printf("%s: Device read error!\n", __func__);
1267 memset(di_parent_buffer, '\0', fs->blksz);
1270 * start:for each double indirect parent
1271 * block create one more block
1273 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1274 di_blockno_child = ext4fs_get_new_blk_no();
1275 if (di_blockno_child == -1) {
1276 printf("no block left to assign\n");
1279 di_child_buff = zalloc(fs->blksz);
1283 di_child_buff_start = di_child_buff;
1284 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1287 debug("DICB %ld: %u\n", di_blockno_child,
1288 *total_remaining_blocks);
1290 status = ext4fs_devread((lbaint_t)di_blockno_child *
1293 (char *)di_child_buff);
1296 printf("%s: Device read error!\n", __func__);
1299 memset(di_child_buff, '\0', fs->blksz);
1300 /* filling of actual datablocks for each child */
1301 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1302 actual_block_no = ext4fs_get_new_blk_no();
1303 if (actual_block_no == -1) {
1304 printf("no block left to assign\n");
1307 *di_child_buff = cpu_to_le32(actual_block_no);
1308 debug("DIAB %ld: %u\n", actual_block_no,
1309 *total_remaining_blocks);
1312 (*total_remaining_blocks)--;
1313 if (*total_remaining_blocks == 0)
1316 /* write the block table */
1317 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1318 di_child_buff_start, fs->blksz);
1319 free(di_child_buff_start);
1320 di_child_buff_start = NULL;
1322 if (*total_remaining_blocks == 0)
1325 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1326 di_block_start_addr, fs->blksz);
1327 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1330 free(di_block_start_addr);
1333 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1334 unsigned int *total_remaining_blocks,
1335 unsigned int *no_blks_reqd)
1340 long int actual_block_no;
1341 /* ti: Triple Indirect */
1342 long int ti_gp_blockno;
1343 long int ti_parent_blockno;
1344 long int ti_child_blockno;
1345 __le32 *ti_gp_buff = NULL;
1346 __le32 *ti_parent_buff = NULL;
1347 __le32 *ti_child_buff = NULL;
1348 __le32 *ti_gp_buff_start_addr = NULL;
1349 __le32 *ti_pbuff_start_addr = NULL;
1350 __le32 *ti_cbuff_start_addr = NULL;
1351 struct ext_filesystem *fs = get_fs();
1352 if (*total_remaining_blocks != 0) {
1353 /* triple indirect grand parent block connecting to inode */
1354 ti_gp_blockno = ext4fs_get_new_blk_no();
1355 if (ti_gp_blockno == -1) {
1356 printf("no block left to assign\n");
1359 ti_gp_buff = zalloc(fs->blksz);
1363 ti_gp_buff_start_addr = ti_gp_buff;
1365 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1366 *total_remaining_blocks);
1368 /* for each 4 byte grand parent entry create one more block */
1369 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1370 ti_parent_blockno = ext4fs_get_new_blk_no();
1371 if (ti_parent_blockno == -1) {
1372 printf("no block left to assign\n");
1375 ti_parent_buff = zalloc(fs->blksz);
1376 if (!ti_parent_buff)
1379 ti_pbuff_start_addr = ti_parent_buff;
1380 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1383 debug("TIPB %ld: %u\n", ti_parent_blockno,
1384 *total_remaining_blocks);
1386 /* for each 4 byte entry parent create one more block */
1387 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1388 ti_child_blockno = ext4fs_get_new_blk_no();
1389 if (ti_child_blockno == -1) {
1390 printf("no block left assign\n");
1393 ti_child_buff = zalloc(fs->blksz);
1397 ti_cbuff_start_addr = ti_child_buff;
1398 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1401 debug("TICB %ld: %u\n", ti_parent_blockno,
1402 *total_remaining_blocks);
1404 /* fill actual datablocks for each child */
1405 for (k = 0; k < (fs->blksz / sizeof(int));
1408 ext4fs_get_new_blk_no();
1409 if (actual_block_no == -1) {
1410 printf("no block left\n");
1411 free(ti_cbuff_start_addr);
1414 *ti_child_buff = cpu_to_le32(actual_block_no);
1415 debug("TIAB %ld: %u\n", actual_block_no,
1416 *total_remaining_blocks);
1419 (*total_remaining_blocks)--;
1420 if (*total_remaining_blocks == 0)
1423 /* write the child block */
1424 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1425 (uint64_t)fs->blksz)),
1426 ti_cbuff_start_addr, fs->blksz);
1427 free(ti_cbuff_start_addr);
1429 if (*total_remaining_blocks == 0)
1432 /* write the parent block */
1433 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1434 ti_pbuff_start_addr, fs->blksz);
1435 free(ti_pbuff_start_addr);
1437 if (*total_remaining_blocks == 0)
1440 /* write the grand parent block */
1441 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1442 ti_gp_buff_start_addr, fs->blksz);
1443 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1444 free(ti_gp_buff_start_addr);
1448 free(ti_pbuff_start_addr);
1450 free(ti_gp_buff_start_addr);
1453 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1454 unsigned int total_remaining_blocks,
1455 unsigned int *total_no_of_block)
1458 long int direct_blockno;
1459 unsigned int no_blks_reqd = 0;
1461 /* allocation of direct blocks */
1462 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1463 direct_blockno = ext4fs_get_new_blk_no();
1464 if (direct_blockno == -1) {
1465 printf("no block left to assign\n");
1468 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1469 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1471 total_remaining_blocks--;
1474 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1476 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1478 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1480 *total_no_of_block += no_blks_reqd;
1485 static struct ext4_extent_header *ext4fs_get_extent_block
1486 (struct ext2_data *data, char *buf,
1487 struct ext4_extent_header *ext_block,
1488 uint32_t fileblock, int log2_blksz)
1490 struct ext4_extent_idx *index;
1491 unsigned long long block;
1492 int blksz = EXT2_BLOCK_SIZE(data);
1496 index = (struct ext4_extent_idx *)(ext_block + 1);
1498 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1501 if (ext_block->eh_depth == 0)
1506 if (i >= le16_to_cpu(ext_block->eh_entries))
1508 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1513 block = le16_to_cpu(index[i].ei_leaf_hi);
1514 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1516 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1518 ext_block = (struct ext4_extent_header *)buf;
1524 static int ext4fs_blockgroup
1525 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1528 unsigned int blkoff, desc_per_blk;
1529 int log2blksz = get_fs()->dev_desc->log2blksz;
1531 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1533 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1534 group / desc_per_blk;
1535 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1537 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1538 group, blkno, blkoff);
1540 return ext4fs_devread((lbaint_t)blkno <<
1541 (LOG2_BLOCK_SIZE(data) - log2blksz),
1542 blkoff, sizeof(struct ext2_block_group),
1546 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1548 struct ext2_block_group blkgrp;
1549 struct ext2_sblock *sblock = &data->sblock;
1550 struct ext_filesystem *fs = get_fs();
1551 int log2blksz = get_fs()->dev_desc->log2blksz;
1552 int inodes_per_block, status;
1554 unsigned int blkoff;
1556 /* It is easier to calculate if the first inode is 0. */
1558 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1559 (sblock->inodes_per_group), &blkgrp);
1563 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1564 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1565 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1566 blkoff = (ino % inodes_per_block) * fs->inodesz;
1567 /* Read the inode. */
1568 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1570 sizeof(struct ext2_inode), (char *)inode);
1577 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1584 long int perblock_parent;
1585 long int perblock_child;
1586 unsigned long long start;
1587 /* get the blocksize of the filesystem */
1588 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1589 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1590 - get_fs()->dev_desc->log2blksz;
1592 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1593 char *buf = zalloc(blksz);
1596 struct ext4_extent_header *ext_block;
1597 struct ext4_extent *extent;
1600 ext4fs_get_extent_block(ext4fs_root, buf,
1601 (struct ext4_extent_header *)
1602 inode->b.blocks.dir_blocks,
1603 fileblock, log2_blksz);
1605 printf("invalid extent block\n");
1610 extent = (struct ext4_extent *)(ext_block + 1);
1614 if (i >= le16_to_cpu(ext_block->eh_entries))
1616 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1618 fileblock -= le32_to_cpu(extent[i].ee_block);
1619 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1624 start = le16_to_cpu(extent[i].ee_start_hi);
1625 start = (start << 32) +
1626 le32_to_cpu(extent[i].ee_start_lo);
1628 return fileblock + start;
1631 printf("Extent Error\n");
1636 /* Direct blocks. */
1637 if (fileblock < INDIRECT_BLOCKS)
1638 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1641 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1642 if (ext4fs_indir1_block == NULL) {
1643 ext4fs_indir1_block = zalloc(blksz);
1644 if (ext4fs_indir1_block == NULL) {
1645 printf("** SI ext2fs read block (indir 1)"
1646 "malloc failed. **\n");
1649 ext4fs_indir1_size = blksz;
1650 ext4fs_indir1_blkno = -1;
1652 if (blksz != ext4fs_indir1_size) {
1653 free(ext4fs_indir1_block);
1654 ext4fs_indir1_block = NULL;
1655 ext4fs_indir1_size = 0;
1656 ext4fs_indir1_blkno = -1;
1657 ext4fs_indir1_block = zalloc(blksz);
1658 if (ext4fs_indir1_block == NULL) {
1659 printf("** SI ext2fs read block (indir 1):"
1660 "malloc failed. **\n");
1663 ext4fs_indir1_size = blksz;
1665 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1666 log2_blksz) != ext4fs_indir1_blkno) {
1668 ext4fs_devread((lbaint_t)le32_to_cpu
1670 indir_block) << log2_blksz, 0,
1671 blksz, (char *)ext4fs_indir1_block);
1673 printf("** SI ext2fs read block (indir 1)"
1677 ext4fs_indir1_blkno =
1678 le32_to_cpu(inode->b.blocks.
1679 indir_block) << log2_blksz;
1681 blknr = le32_to_cpu(ext4fs_indir1_block
1682 [fileblock - INDIRECT_BLOCKS]);
1684 /* Double indirect. */
1685 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1686 (blksz / 4 + 1)))) {
1688 long int perblock = blksz / 4;
1689 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1691 if (ext4fs_indir1_block == NULL) {
1692 ext4fs_indir1_block = zalloc(blksz);
1693 if (ext4fs_indir1_block == NULL) {
1694 printf("** DI ext2fs read block (indir 2 1)"
1695 "malloc failed. **\n");
1698 ext4fs_indir1_size = blksz;
1699 ext4fs_indir1_blkno = -1;
1701 if (blksz != ext4fs_indir1_size) {
1702 free(ext4fs_indir1_block);
1703 ext4fs_indir1_block = NULL;
1704 ext4fs_indir1_size = 0;
1705 ext4fs_indir1_blkno = -1;
1706 ext4fs_indir1_block = zalloc(blksz);
1707 if (ext4fs_indir1_block == NULL) {
1708 printf("** DI ext2fs read block (indir 2 1)"
1709 "malloc failed. **\n");
1712 ext4fs_indir1_size = blksz;
1714 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1715 log2_blksz) != ext4fs_indir1_blkno) {
1717 ext4fs_devread((lbaint_t)le32_to_cpu
1719 double_indir_block) << log2_blksz,
1721 (char *)ext4fs_indir1_block);
1723 printf("** DI ext2fs read block (indir 2 1)"
1727 ext4fs_indir1_blkno =
1728 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1732 if (ext4fs_indir2_block == NULL) {
1733 ext4fs_indir2_block = zalloc(blksz);
1734 if (ext4fs_indir2_block == NULL) {
1735 printf("** DI ext2fs read block (indir 2 2)"
1736 "malloc failed. **\n");
1739 ext4fs_indir2_size = blksz;
1740 ext4fs_indir2_blkno = -1;
1742 if (blksz != ext4fs_indir2_size) {
1743 free(ext4fs_indir2_block);
1744 ext4fs_indir2_block = NULL;
1745 ext4fs_indir2_size = 0;
1746 ext4fs_indir2_blkno = -1;
1747 ext4fs_indir2_block = zalloc(blksz);
1748 if (ext4fs_indir2_block == NULL) {
1749 printf("** DI ext2fs read block (indir 2 2)"
1750 "malloc failed. **\n");
1753 ext4fs_indir2_size = blksz;
1755 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1756 log2_blksz) != ext4fs_indir2_blkno) {
1757 status = ext4fs_devread((lbaint_t)le32_to_cpu
1758 (ext4fs_indir1_block
1760 perblock]) << log2_blksz, 0,
1762 (char *)ext4fs_indir2_block);
1764 printf("** DI ext2fs read block (indir 2 2)"
1768 ext4fs_indir2_blkno =
1769 le32_to_cpu(ext4fs_indir1_block[rblock
1774 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1776 /* Tripple indirect. */
1778 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1779 (blksz / 4 * blksz / 4));
1780 perblock_child = blksz / 4;
1781 perblock_parent = ((blksz / 4) * (blksz / 4));
1783 if (ext4fs_indir1_block == NULL) {
1784 ext4fs_indir1_block = zalloc(blksz);
1785 if (ext4fs_indir1_block == NULL) {
1786 printf("** TI ext2fs read block (indir 2 1)"
1787 "malloc failed. **\n");
1790 ext4fs_indir1_size = blksz;
1791 ext4fs_indir1_blkno = -1;
1793 if (blksz != ext4fs_indir1_size) {
1794 free(ext4fs_indir1_block);
1795 ext4fs_indir1_block = NULL;
1796 ext4fs_indir1_size = 0;
1797 ext4fs_indir1_blkno = -1;
1798 ext4fs_indir1_block = zalloc(blksz);
1799 if (ext4fs_indir1_block == NULL) {
1800 printf("** TI ext2fs read block (indir 2 1)"
1801 "malloc failed. **\n");
1804 ext4fs_indir1_size = blksz;
1806 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1807 log2_blksz) != ext4fs_indir1_blkno) {
1808 status = ext4fs_devread
1810 le32_to_cpu(inode->b.blocks.triple_indir_block)
1811 << log2_blksz, 0, blksz,
1812 (char *)ext4fs_indir1_block);
1814 printf("** TI ext2fs read block (indir 2 1)"
1818 ext4fs_indir1_blkno =
1819 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1823 if (ext4fs_indir2_block == NULL) {
1824 ext4fs_indir2_block = zalloc(blksz);
1825 if (ext4fs_indir2_block == NULL) {
1826 printf("** TI ext2fs read block (indir 2 2)"
1827 "malloc failed. **\n");
1830 ext4fs_indir2_size = blksz;
1831 ext4fs_indir2_blkno = -1;
1833 if (blksz != ext4fs_indir2_size) {
1834 free(ext4fs_indir2_block);
1835 ext4fs_indir2_block = NULL;
1836 ext4fs_indir2_size = 0;
1837 ext4fs_indir2_blkno = -1;
1838 ext4fs_indir2_block = zalloc(blksz);
1839 if (ext4fs_indir2_block == NULL) {
1840 printf("** TI ext2fs read block (indir 2 2)"
1841 "malloc failed. **\n");
1844 ext4fs_indir2_size = blksz;
1846 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1847 perblock_parent]) <<
1849 != ext4fs_indir2_blkno) {
1850 status = ext4fs_devread((lbaint_t)le32_to_cpu
1851 (ext4fs_indir1_block
1853 perblock_parent]) <<
1854 log2_blksz, 0, blksz,
1855 (char *)ext4fs_indir2_block);
1857 printf("** TI ext2fs read block (indir 2 2)"
1861 ext4fs_indir2_blkno =
1862 le32_to_cpu(ext4fs_indir1_block[rblock /
1867 if (ext4fs_indir3_block == NULL) {
1868 ext4fs_indir3_block = zalloc(blksz);
1869 if (ext4fs_indir3_block == NULL) {
1870 printf("** TI ext2fs read block (indir 2 2)"
1871 "malloc failed. **\n");
1874 ext4fs_indir3_size = blksz;
1875 ext4fs_indir3_blkno = -1;
1877 if (blksz != ext4fs_indir3_size) {
1878 free(ext4fs_indir3_block);
1879 ext4fs_indir3_block = NULL;
1880 ext4fs_indir3_size = 0;
1881 ext4fs_indir3_blkno = -1;
1882 ext4fs_indir3_block = zalloc(blksz);
1883 if (ext4fs_indir3_block == NULL) {
1884 printf("** TI ext2fs read block (indir 2 2)"
1885 "malloc failed. **\n");
1888 ext4fs_indir3_size = blksz;
1890 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1893 log2_blksz) != ext4fs_indir3_blkno) {
1895 ext4fs_devread((lbaint_t)le32_to_cpu
1896 (ext4fs_indir2_block
1897 [(rblock / perblock_child)
1898 % (blksz / 4)]) << log2_blksz, 0,
1899 blksz, (char *)ext4fs_indir3_block);
1901 printf("** TI ext2fs read block (indir 2 2)"
1905 ext4fs_indir3_blkno =
1906 le32_to_cpu(ext4fs_indir2_block[(rblock /
1913 blknr = le32_to_cpu(ext4fs_indir3_block
1914 [rblock % perblock_child]);
1916 debug("read_allocated_block %ld\n", blknr);
1922 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1925 * This function assures that for a file with the same name but different size
1926 * the sequential store on the ext4 filesystem will be correct.
1928 * In this function the global data, responsible for internal representation
1929 * of the ext4 data are initialized to the reset state. Without this, during
1930 * replacement of the smaller file with the bigger truncation of new file was
1933 void ext4fs_reinit_global(void)
1935 if (ext4fs_indir1_block != NULL) {
1936 free(ext4fs_indir1_block);
1937 ext4fs_indir1_block = NULL;
1938 ext4fs_indir1_size = 0;
1939 ext4fs_indir1_blkno = -1;
1941 if (ext4fs_indir2_block != NULL) {
1942 free(ext4fs_indir2_block);
1943 ext4fs_indir2_block = NULL;
1944 ext4fs_indir2_size = 0;
1945 ext4fs_indir2_blkno = -1;
1947 if (ext4fs_indir3_block != NULL) {
1948 free(ext4fs_indir3_block);
1949 ext4fs_indir3_block = NULL;
1950 ext4fs_indir3_size = 0;
1951 ext4fs_indir3_blkno = -1;
1954 void ext4fs_close(void)
1956 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1957 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1960 if (ext4fs_root != NULL) {
1965 ext4fs_reinit_global();
1968 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1969 struct ext2fs_node **fnode, int *ftype)
1971 unsigned int fpos = 0;
1974 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1978 printf("Iterate dir %s\n", name);
1979 #endif /* of DEBUG */
1980 if (!diro->inode_read) {
1981 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1985 /* Search the file. */
1986 while (fpos < le32_to_cpu(diro->inode.size)) {
1987 struct ext2_dirent dirent;
1989 status = ext4fs_read_file(diro, fpos,
1990 sizeof(struct ext2_dirent),
1991 (char *)&dirent, &actread);
1995 if (dirent.direntlen == 0) {
1996 printf("Failed to iterate over directory %s\n", name);
2000 if (dirent.namelen != 0) {
2001 char filename[dirent.namelen + 1];
2002 struct ext2fs_node *fdiro;
2003 int type = FILETYPE_UNKNOWN;
2005 status = ext4fs_read_file(diro,
2007 sizeof(struct ext2_dirent),
2008 dirent.namelen, filename,
2013 fdiro = zalloc(sizeof(struct ext2fs_node));
2017 fdiro->data = diro->data;
2018 fdiro->ino = le32_to_cpu(dirent.inode);
2020 filename[dirent.namelen] = '\0';
2022 if (dirent.filetype != FILETYPE_UNKNOWN) {
2023 fdiro->inode_read = 0;
2025 if (dirent.filetype == FILETYPE_DIRECTORY)
2026 type = FILETYPE_DIRECTORY;
2027 else if (dirent.filetype == FILETYPE_SYMLINK)
2028 type = FILETYPE_SYMLINK;
2029 else if (dirent.filetype == FILETYPE_REG)
2030 type = FILETYPE_REG;
2032 status = ext4fs_read_inode(diro->data,
2040 fdiro->inode_read = 1;
2042 if ((le16_to_cpu(fdiro->inode.mode) &
2043 FILETYPE_INO_MASK) ==
2044 FILETYPE_INO_DIRECTORY) {
2045 type = FILETYPE_DIRECTORY;
2046 } else if ((le16_to_cpu(fdiro->inode.mode)
2047 & FILETYPE_INO_MASK) ==
2048 FILETYPE_INO_SYMLINK) {
2049 type = FILETYPE_SYMLINK;
2050 } else if ((le16_to_cpu(fdiro->inode.mode)
2051 & FILETYPE_INO_MASK) ==
2053 type = FILETYPE_REG;
2057 printf("iterate >%s<\n", filename);
2058 #endif /* of DEBUG */
2059 if ((name != NULL) && (fnode != NULL)
2060 && (ftype != NULL)) {
2061 if (strcmp(filename, name) == 0) {
2067 if (fdiro->inode_read == 0) {
2068 status = ext4fs_read_inode(diro->data,
2076 fdiro->inode_read = 1;
2079 case FILETYPE_DIRECTORY:
2082 case FILETYPE_SYMLINK:
2093 le32_to_cpu(fdiro->inode.size),
2098 fpos += le16_to_cpu(dirent.direntlen);
2103 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2106 struct ext2fs_node *diro = node;
2110 if (!diro->inode_read) {
2111 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2115 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2119 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2120 strncpy(symlink, diro->inode.b.symlink,
2121 le32_to_cpu(diro->inode.size));
2123 status = ext4fs_read_file(diro, 0,
2124 le32_to_cpu(diro->inode.size),
2126 if ((status < 0) || (actread == 0)) {
2131 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2135 static int ext4fs_find_file1(const char *currpath,
2136 struct ext2fs_node *currroot,
2137 struct ext2fs_node **currfound, int *foundtype)
2139 char fpath[strlen(currpath) + 1];
2143 int type = FILETYPE_DIRECTORY;
2144 struct ext2fs_node *currnode = currroot;
2145 struct ext2fs_node *oldnode = currroot;
2147 strncpy(fpath, currpath, strlen(currpath) + 1);
2149 /* Remove all leading slashes. */
2150 while (*name == '/')
2154 *currfound = currnode;
2161 /* Extract the actual part from the pathname. */
2162 next = strchr(name, '/');
2164 /* Remove all leading slashes. */
2165 while (*next == '/')
2169 if (type != FILETYPE_DIRECTORY) {
2170 ext4fs_free_node(currnode, currroot);
2176 /* Iterate over the directory. */
2177 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2184 /* Read in the symlink and follow it. */
2185 if (type == FILETYPE_SYMLINK) {
2188 /* Test if the symlink does not loop. */
2189 if (++symlinknest == 8) {
2190 ext4fs_free_node(currnode, currroot);
2191 ext4fs_free_node(oldnode, currroot);
2195 symlink = ext4fs_read_symlink(currnode);
2196 ext4fs_free_node(currnode, currroot);
2199 ext4fs_free_node(oldnode, currroot);
2203 debug("Got symlink >%s<\n", symlink);
2205 if (symlink[0] == '/') {
2206 ext4fs_free_node(oldnode, currroot);
2207 oldnode = &ext4fs_root->diropen;
2210 /* Lookup the node the symlink points to. */
2211 status = ext4fs_find_file1(symlink, oldnode,
2217 ext4fs_free_node(oldnode, currroot);
2222 ext4fs_free_node(oldnode, currroot);
2224 /* Found the node! */
2225 if (!next || *next == '\0') {
2226 *currfound = currnode;
2235 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2236 struct ext2fs_node **foundnode, int expecttype)
2239 int foundtype = FILETYPE_DIRECTORY;
2245 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2249 /* Check if the node that was found was of the expected type. */
2250 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2252 else if ((expecttype == FILETYPE_DIRECTORY)
2253 && (foundtype != expecttype))
2259 int ext4fs_open(const char *filename, loff_t *len)
2261 struct ext2fs_node *fdiro = NULL;
2264 if (ext4fs_root == NULL)
2268 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2273 if (!fdiro->inode_read) {
2274 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2279 *len = le32_to_cpu(fdiro->inode.size);
2280 ext4fs_file = fdiro;
2284 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2289 int ext4fs_mount(unsigned part_length)
2291 struct ext2_data *data;
2293 struct ext_filesystem *fs = get_fs();
2294 data = zalloc(SUPERBLOCK_SIZE);
2298 /* Read the superblock. */
2299 status = ext4_read_superblock((char *)&data->sblock);
2304 /* Make sure this is an ext2 filesystem. */
2305 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2309 * The 64bit feature was enabled when metadata_csum was enabled
2310 * and we do not support metadata_csum (and cannot reliably find
2311 * files when it is set. Refuse to mount.
2313 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
2314 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2318 if (le32_to_cpu(data->sblock.revision_level) == 0) {
2321 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2322 __le32_to_cpu(data->sblock.feature_compatibility),
2323 __le32_to_cpu(data->sblock.feature_incompat),
2324 __le32_to_cpu(data->sblock.feature_ro_compat));
2326 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2327 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2328 EXT4_FEATURE_INCOMPAT_64BIT ?
2329 le16_to_cpu(data->sblock.descriptor_size) : 32;
2332 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2333 le32_to_cpu(data->sblock.revision_level),
2334 fs->inodesz, fs->gdsize);
2336 data->diropen.data = data;
2337 data->diropen.ino = 2;
2338 data->diropen.inode_read = 1;
2339 data->inode = &data->diropen.inode;
2341 status = ext4fs_read_inode(data, 2, data->inode);
2349 printf("Failed to mount ext2 filesystem...\n");