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 static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
52 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
55 static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
57 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
60 static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
62 bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
65 static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
67 bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
70 static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
72 bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
75 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
77 uint32_t res = size / n;
84 void put_ext4(uint64_t off, void *buf, uint32_t size)
88 unsigned char *temp_ptr = NULL;
89 struct ext_filesystem *fs = get_fs();
90 int log2blksz = fs->dev_desc->log2blksz;
91 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
93 startblock = off >> log2blksz;
94 startblock += part_offset;
95 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
97 if (fs->dev_desc == NULL)
100 if ((startblock + (size >> log2blksz)) >
101 (part_offset + fs->total_sect)) {
102 printf("part_offset is " LBAFU "\n", part_offset);
103 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
104 printf("error: overflow occurs\n");
109 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
111 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
112 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
114 if (size >> log2blksz != 0) {
115 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
116 (unsigned long *)buf);
118 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
120 memcpy(temp_ptr, buf, size);
121 blk_dwrite(fs->dev_desc, startblock, 1,
122 (unsigned long *)sec_buf);
127 static int _get_new_inode_no(unsigned char *buffer)
129 struct ext_filesystem *fs = get_fs();
135 /* get the blocksize of the filesystem */
136 unsigned char *ptr = buffer;
137 while (*ptr == 255) {
140 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
144 for (j = 0; j < fs->blksz; j++) {
149 status = input & operand;
164 static int _get_new_blk_no(unsigned char *buffer)
170 unsigned char *ptr = buffer;
171 struct ext_filesystem *fs = get_fs();
173 if (fs->blksz != 1024)
178 while (*ptr == 255) {
181 if (count == (fs->blksz * 8))
185 for (j = 0; j < fs->blksz; j++) {
190 status = input & operand;
205 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
207 int i, remainder, status;
208 unsigned char *ptr = buffer;
209 unsigned char operand;
211 remainder = blockno % 8;
212 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
214 i = i - (index * blocksize);
215 if (blocksize != 1024) {
217 operand = 1 << remainder;
218 status = *ptr & operand;
222 *ptr = *ptr | operand;
225 if (remainder == 0) {
230 operand = (1 << (remainder - 1));
232 status = *ptr & operand;
236 *ptr = *ptr | operand;
241 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
243 int i, remainder, status;
244 unsigned char *ptr = buffer;
245 unsigned char operand;
247 remainder = blockno % 8;
248 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
250 i = i - (index * blocksize);
251 if (blocksize != 1024) {
253 operand = (1 << remainder);
254 status = *ptr & operand;
256 *ptr = *ptr & ~(operand);
258 if (remainder == 0) {
263 operand = (1 << (remainder - 1));
265 status = *ptr & operand;
267 *ptr = *ptr & ~(operand);
271 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
273 int i, remainder, status;
274 unsigned char *ptr = buffer;
275 unsigned char operand;
277 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
279 remainder = inode_no % 8;
280 if (remainder == 0) {
285 operand = (1 << (remainder - 1));
287 status = *ptr & operand;
291 *ptr = *ptr | operand;
296 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
298 int i, remainder, status;
299 unsigned char *ptr = buffer;
300 unsigned char operand;
302 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
304 remainder = inode_no % 8;
305 if (remainder == 0) {
310 operand = (1 << (remainder - 1));
312 status = *ptr & operand;
314 *ptr = *ptr & ~(operand);
317 uint16_t ext4fs_checksum_update(uint32_t i)
319 struct ext2_block_group *desc;
320 struct ext_filesystem *fs = get_fs();
322 __le32 le32_i = cpu_to_le32(i);
324 desc = (struct ext2_block_group *)&fs->bgd[i];
325 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
326 int offset = offsetof(struct ext2_block_group, bg_checksum);
328 crc = ext2fs_crc16(~0, fs->sb->unique_id,
329 sizeof(fs->sb->unique_id));
330 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
331 crc = ext2fs_crc16(crc, desc, offset);
332 offset += sizeof(desc->bg_checksum); /* skip checksum */
333 assert(offset == sizeof(*desc));
339 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
342 int sizeof_void_space;
343 int new_entry_byte_reqd;
344 short padding_factor = 0;
346 if (dir->namelen % 4 != 0)
347 padding_factor = 4 - (dir->namelen % 4);
349 dentry_length = sizeof(struct ext2_dirent) +
350 dir->namelen + padding_factor;
351 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
352 if (sizeof_void_space == 0)
356 if (strlen(filename) % 4 != 0)
357 padding_factor = 4 - (strlen(filename) % 4);
359 new_entry_byte_reqd = strlen(filename) +
360 sizeof(struct ext2_dirent) + padding_factor;
361 if (sizeof_void_space >= new_entry_byte_reqd) {
362 dir->direntlen = cpu_to_le16(dentry_length);
363 return sizeof_void_space;
369 void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
371 unsigned int *zero_buffer = NULL;
372 char *root_first_block_buffer = NULL;
375 long int first_block_no_of_root = 0;
376 long int previous_blknr = -1;
378 short int padding_factor = 0;
379 unsigned int new_entry_byte_reqd;
380 unsigned int last_entry_dirlen;
381 int sizeof_void_space = 0;
385 struct ext_filesystem *fs = get_fs();
386 /* directory entry */
387 struct ext2_dirent *dir;
388 char *temp_dir = NULL;
391 uint32_t new_blockcnt;
393 zero_buffer = zalloc(fs->blksz);
395 printf("No Memory\n");
398 root_first_block_buffer = zalloc(fs->blksz);
399 if (!root_first_block_buffer) {
401 printf("No Memory\n");
406 /* read the block no allocated to a file */
407 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
409 root_blknr = read_allocated_block(g_parent_inode,
411 if (root_blknr == 0) {
412 first_block_no_of_root = previous_blknr;
415 previous_blknr = root_blknr;
418 status = ext4fs_devread((lbaint_t)first_block_no_of_root
420 0, fs->blksz, root_first_block_buffer);
424 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
426 dir = (struct ext2_dirent *)root_first_block_buffer;
428 while (le16_to_cpu(dir->direntlen) > 0) {
430 * blocksize-totalbytes because last directory length
431 * i.e. dir->direntlen is free availble space in the
432 * block that means it is a last entry of directory
436 /* traversing the each directory entry */
437 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
438 if (strlen(filename) % 4 != 0)
439 padding_factor = 4 - (strlen(filename) % 4);
441 new_entry_byte_reqd = strlen(filename) +
442 sizeof(struct ext2_dirent) + padding_factor;
445 * update last directory entry length to its
446 * length because we are creating new directory
449 if (dir->namelen % 4 != 0)
450 padding_factor = 4 - (dir->namelen % 4);
452 last_entry_dirlen = dir->namelen +
453 sizeof(struct ext2_dirent) + padding_factor;
454 if ((fs->blksz - totalbytes - last_entry_dirlen) <
455 new_entry_byte_reqd) {
456 printf("1st Block Full:Allocate new block\n");
458 if (direct_blk_idx == INDIRECT_BLOCKS - 1) {
459 printf("Directory exceeds limit\n");
462 new_blk_no = ext4fs_get_new_blk_no();
463 if (new_blk_no == -1) {
464 printf("no block left to assign\n");
467 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
468 g_parent_inode->b.blocks.dir_blocks[direct_blk_idx] =
469 cpu_to_le32(new_blk_no);
471 new_size = le32_to_cpu(g_parent_inode->size);
472 new_size += fs->blksz;
473 g_parent_inode->size = cpu_to_le32(new_size);
475 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
476 new_blockcnt += fs->sect_perblk;
477 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
479 if (ext4fs_put_metadata
480 (root_first_block_buffer,
481 first_block_no_of_root))
485 dir->direntlen = cpu_to_le16(last_entry_dirlen);
489 templength = le16_to_cpu(dir->direntlen);
490 totalbytes = totalbytes + templength;
491 sizeof_void_space = check_void_in_dentry(dir, filename);
492 if (sizeof_void_space)
495 dir = (struct ext2_dirent *)((char *)dir + templength);
498 /* make a pointer ready for creating next directory entry */
499 templength = le16_to_cpu(dir->direntlen);
500 totalbytes = totalbytes + templength;
501 dir = (struct ext2_dirent *)((char *)dir + templength);
503 /* get the next available inode number */
504 inodeno = ext4fs_get_new_inode_no();
506 printf("no inode left to assign\n");
509 dir->inode = cpu_to_le32(inodeno);
510 if (sizeof_void_space)
511 dir->direntlen = cpu_to_le16(sizeof_void_space);
513 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
515 dir->namelen = strlen(filename);
516 dir->filetype = FILETYPE_REG; /* regular file */
517 temp_dir = (char *)dir;
518 temp_dir = temp_dir + sizeof(struct ext2_dirent);
519 memcpy(temp_dir, filename, strlen(filename));
523 /* update or write the 1st block of root inode */
524 if (ext4fs_put_metadata(root_first_block_buffer,
525 first_block_no_of_root))
530 free(root_first_block_buffer);
533 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
543 unsigned char *block_buffer = NULL;
544 struct ext2_dirent *dir = NULL;
545 struct ext2_dirent *previous_dir = NULL;
546 struct ext_filesystem *fs = get_fs();
548 /* read the block no allocated to a file */
549 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
551 blknr = read_allocated_block(parent_inode, direct_blk_idx);
555 /* read the blocks of parenet inode */
556 block_buffer = zalloc(fs->blksz);
560 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
561 0, fs->blksz, (char *)block_buffer);
565 dir = (struct ext2_dirent *)block_buffer;
568 while (le16_to_cpu(dir->direntlen) >= 0) {
570 * blocksize-totalbytes because last directory
571 * length i.e.,*dir->direntlen is free availble
572 * space in the block that means
573 * it is a last entry of directory entry
575 if (strlen(dirname) == dir->namelen) {
576 if (strncmp(dirname, ptr + sizeof(struct ext2_dirent), dir->namelen) == 0) {
578 new_len = le16_to_cpu(previous_dir->direntlen);
579 new_len += le16_to_cpu(dir->direntlen);
580 previous_dir->direntlen = cpu_to_le16(new_len);
581 inodeno = le32_to_cpu(dir->inode);
588 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
591 /* traversing the each directory entry */
592 templength = le16_to_cpu(dir->direntlen);
593 totalbytes = totalbytes + templength;
595 dir = (struct ext2_dirent *)((char *)dir + templength);
615 static int find_dir_depth(char *dirname)
617 char *token = strtok(dirname, "/");
619 while (token != NULL) {
620 token = strtok(NULL, "/");
623 return count + 1 + 1;
625 * for example for string /home/temp
626 * depth=home(1)+temp(1)+1 extra for NULL;
631 static int parse_path(char **arr, char *dirname)
633 char *token = strtok(dirname, "/");
637 arr[i] = zalloc(strlen("/") + 1);
640 memcpy(arr[i++], "/", strlen("/"));
642 /* add each path entry after root */
643 while (token != NULL) {
644 arr[i] = zalloc(strlen(token) + 1);
647 memcpy(arr[i++], token, strlen(token));
648 token = strtok(NULL, "/");
655 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
657 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
664 * Function: ext4fs_get_parent_inode_num
665 * Return Value: inode Number of the parent directory of file/Directory to be
667 * dirname : Input parmater, input path name of the file/directory to be created
668 * dname : Output parameter, to be filled with the name of the directory
669 * extracted from dirname
671 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
675 int matched_inode_no;
676 int result_inode_no = -1;
678 char *depth_dirname = NULL;
679 char *parse_dirname = NULL;
680 struct ext2_inode *parent_inode = NULL;
681 struct ext2_inode *first_inode = NULL;
682 struct ext2_inode temp_inode;
684 if (*dirname != '/') {
685 printf("Please supply Absolute path\n");
689 /* TODO: input validation make equivalent to linux */
690 depth_dirname = zalloc(strlen(dirname) + 1);
694 memcpy(depth_dirname, dirname, strlen(dirname));
695 depth = find_dir_depth(depth_dirname);
696 parse_dirname = zalloc(strlen(dirname) + 1);
699 memcpy(parse_dirname, dirname, strlen(dirname));
701 /* allocate memory for each directory level */
702 ptr = zalloc((depth) * sizeof(char *));
705 if (parse_path(ptr, parse_dirname))
707 parent_inode = zalloc(sizeof(struct ext2_inode));
710 first_inode = zalloc(sizeof(struct ext2_inode));
713 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
714 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
716 result_inode_no = EXT2_ROOT_INO;
717 for (i = 1; i < depth; i++) {
718 matched_inode_no = search_dir(parent_inode, ptr[i]);
719 if (matched_inode_no == -1) {
720 if (ptr[i + 1] == NULL && i == 1) {
721 result_inode_no = EXT2_ROOT_INO;
724 if (ptr[i + 1] == NULL)
726 printf("Invalid path\n");
727 result_inode_no = -1;
731 if (ptr[i + 1] != NULL) {
732 memset(parent_inode, '\0',
733 sizeof(struct ext2_inode));
734 if (ext4fs_iget(matched_inode_no,
736 result_inode_no = -1;
739 result_inode_no = matched_inode_no;
748 matched_inode_no = search_dir(first_inode, ptr[i]);
750 matched_inode_no = search_dir(parent_inode, ptr[i]);
752 if (matched_inode_no != -1) {
753 ext4fs_iget(matched_inode_no, &temp_inode);
754 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
755 printf("It is a Directory\n");
756 result_inode_no = -1;
761 if (strlen(ptr[i]) > 256) {
762 result_inode_no = -1;
765 memcpy(dname, ptr[i], strlen(ptr[i]));
770 for (i = 0; i < depth; i++) {
779 return result_inode_no;
782 static int check_filename(char *filename, unsigned int blknr)
784 unsigned int first_block_no_of_root;
789 char *root_first_block_buffer = NULL;
790 char *root_first_block_addr = NULL;
791 struct ext2_dirent *dir = NULL;
792 struct ext2_dirent *previous_dir = NULL;
794 struct ext_filesystem *fs = get_fs();
797 /* get the first block of root */
798 first_block_no_of_root = blknr;
799 root_first_block_buffer = zalloc(fs->blksz);
800 if (!root_first_block_buffer)
802 root_first_block_addr = root_first_block_buffer;
803 status = ext4fs_devread((lbaint_t)first_block_no_of_root *
805 fs->blksz, root_first_block_buffer);
809 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
811 dir = (struct ext2_dirent *)root_first_block_buffer;
814 while (le16_to_cpu(dir->direntlen) >= 0) {
816 * blocksize-totalbytes because last
817 * directory length i.e., *dir->direntlen
818 * is free availble space in the block that
819 * means it is a last entry of directory entry
821 if (strlen(filename) == dir->namelen) {
822 if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
823 dir->namelen) == 0) {
825 printf("file found deleting\n");
826 new_len = le16_to_cpu(previous_dir->direntlen);
827 new_len += le16_to_cpu(dir->direntlen);
828 previous_dir->direntlen = cpu_to_le16(new_len);
829 inodeno = le32_to_cpu(dir->inode);
836 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
839 /* traversing the each directory entry */
840 templength = le16_to_cpu(dir->direntlen);
841 totalbytes = totalbytes + templength;
843 dir = (struct ext2_dirent *)((char *)dir + templength);
849 if (ext4fs_put_metadata(root_first_block_addr,
850 first_block_no_of_root))
855 free(root_first_block_buffer);
860 int ext4fs_filename_check(char *filename)
862 short direct_blk_idx = 0;
866 /* read the block no allocated to a file */
867 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
869 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
872 inodeno = check_filename(filename, blknr);
880 uint32_t ext4fs_get_new_blk_no(void)
886 static int prev_bg_bitmap_index = -1;
887 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
888 struct ext_filesystem *fs = get_fs();
889 char *journal_buffer = zalloc(fs->blksz);
890 char *zero_buffer = zalloc(fs->blksz);
891 if (!journal_buffer || !zero_buffer)
893 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
895 if (fs->first_pass_bbmap == 0) {
896 for (i = 0; i < fs->no_blkgrp; i++) {
897 if (le16_to_cpu(bgd[i].free_blocks)) {
898 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
900 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
901 zero_buffer, fs->blksz);
902 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
903 bgd[i].bg_flags = cpu_to_le16(new_flags);
904 memcpy(fs->blk_bmaps[i], zero_buffer,
908 _get_new_blk_no(fs->blk_bmaps[i]);
909 if (fs->curr_blkno == -1)
910 /* if block bitmap is completely fill */
912 fs->curr_blkno = fs->curr_blkno +
914 fs->first_pass_bbmap++;
915 ext4fs_bg_free_blocks_dec(&bgd[i]);
916 ext4fs_sb_free_blocks_dec(fs->sb);
917 status = ext4fs_devread(
918 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
924 if (ext4fs_log_journal(journal_buffer,
925 le32_to_cpu(bgd[i].block_id)))
929 debug("no space left on block group %d\n", i);
937 /* get the blockbitmap index respective to blockno */
938 bg_idx = fs->curr_blkno / blk_per_grp;
939 if (fs->blksz == 1024) {
940 remainder = fs->curr_blkno % blk_per_grp;
946 * To skip completely filled block group bitmaps
947 * Optimize the block allocation
949 if (bg_idx >= fs->no_blkgrp)
952 if (bgd[bg_idx].free_blocks == 0) {
953 debug("block group %u is full. Skipping\n", bg_idx);
954 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
959 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
961 memset(zero_buffer, '\0', fs->blksz);
962 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
963 zero_buffer, fs->blksz);
964 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
965 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
966 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
969 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
971 debug("going for restart for the block no %ld %u\n",
972 fs->curr_blkno, bg_idx);
977 if (prev_bg_bitmap_index != bg_idx) {
978 memset(journal_buffer, '\0', fs->blksz);
979 status = ext4fs_devread(
980 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
982 0, fs->blksz, journal_buffer);
985 if (ext4fs_log_journal(journal_buffer,
986 le32_to_cpu(bgd[bg_idx].block_id)))
989 prev_bg_bitmap_index = bg_idx;
991 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
992 ext4fs_sb_free_blocks_dec(fs->sb);
996 free(journal_buffer);
999 return fs->curr_blkno;
1001 free(journal_buffer);
1007 int ext4fs_get_new_inode_no(void)
1011 unsigned int ibmap_idx;
1012 static int prev_inode_bitmap_index = -1;
1013 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1014 struct ext_filesystem *fs = get_fs();
1015 char *journal_buffer = zalloc(fs->blksz);
1016 char *zero_buffer = zalloc(fs->blksz);
1017 if (!journal_buffer || !zero_buffer)
1019 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
1021 if (fs->first_pass_ibmap == 0) {
1022 for (i = 0; i < fs->no_blkgrp; i++) {
1023 if (bgd[i].free_inodes) {
1024 if (bgd[i].bg_itable_unused !=
1026 bgd[i].bg_itable_unused =
1028 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1030 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
1031 zero_buffer, fs->blksz);
1032 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1033 bgd[i].bg_flags = cpu_to_le16(new_flags);
1034 memcpy(fs->inode_bmaps[i],
1035 zero_buffer, fs->blksz);
1038 _get_new_inode_no(fs->inode_bmaps[i]);
1039 if (fs->curr_inode_no == -1)
1040 /* if block bitmap is completely fill */
1042 fs->curr_inode_no = fs->curr_inode_no +
1043 (i * inodes_per_grp);
1044 fs->first_pass_ibmap++;
1045 ext4fs_bg_free_inodes_dec(&bgd[i]);
1046 ext4fs_bg_itable_unused_dec(&bgd[i]);
1047 ext4fs_sb_free_inodes_dec(fs->sb);
1048 status = ext4fs_devread(
1049 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
1055 if (ext4fs_log_journal(journal_buffer,
1056 le32_to_cpu(bgd[i].inode_id)))
1060 debug("no inode left on block group %d\n", i);
1065 fs->curr_inode_no++;
1066 /* get the blockbitmap index respective to blockno */
1067 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1068 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1070 memset(zero_buffer, '\0', fs->blksz);
1071 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1072 zero_buffer, fs->blksz);
1073 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1074 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
1075 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1079 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1080 fs->inode_bmaps[ibmap_idx],
1082 debug("going for restart for the block no %d %u\n",
1083 fs->curr_inode_no, ibmap_idx);
1087 /* journal backup */
1088 if (prev_inode_bitmap_index != ibmap_idx) {
1089 memset(journal_buffer, '\0', fs->blksz);
1090 status = ext4fs_devread(
1091 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
1093 0, fs->blksz, journal_buffer);
1096 if (ext4fs_log_journal(journal_buffer,
1097 le32_to_cpu(bgd[ibmap_idx].inode_id)))
1099 prev_inode_bitmap_index = ibmap_idx;
1101 if (bgd[ibmap_idx].bg_itable_unused !=
1102 bgd[ibmap_idx].free_inodes)
1103 bgd[ibmap_idx].bg_itable_unused =
1104 bgd[ibmap_idx].free_inodes;
1105 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1106 ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
1107 ext4fs_sb_free_inodes_dec(fs->sb);
1112 free(journal_buffer);
1115 return fs->curr_inode_no;
1117 free(journal_buffer);
1125 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1126 unsigned int *total_remaining_blocks,
1127 unsigned int *no_blks_reqd)
1131 long int actual_block_no;
1132 long int si_blockno;
1133 /* si :single indirect */
1134 __le32 *si_buffer = NULL;
1135 __le32 *si_start_addr = NULL;
1136 struct ext_filesystem *fs = get_fs();
1138 if (*total_remaining_blocks != 0) {
1139 si_buffer = zalloc(fs->blksz);
1141 printf("No Memory\n");
1144 si_start_addr = si_buffer;
1145 si_blockno = ext4fs_get_new_blk_no();
1146 if (si_blockno == -1) {
1147 printf("no block left to assign\n");
1151 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1153 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1154 0, fs->blksz, (char *)si_buffer);
1155 memset(si_buffer, '\0', fs->blksz);
1159 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1160 actual_block_no = ext4fs_get_new_blk_no();
1161 if (actual_block_no == -1) {
1162 printf("no block left to assign\n");
1165 *si_buffer = cpu_to_le32(actual_block_no);
1166 debug("SIAB %u: %u\n", *si_buffer,
1167 *total_remaining_blocks);
1170 (*total_remaining_blocks)--;
1171 if (*total_remaining_blocks == 0)
1175 /* write the block to disk */
1176 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1177 si_start_addr, fs->blksz);
1178 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1181 free(si_start_addr);
1184 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1185 unsigned int *total_remaining_blocks,
1186 unsigned int *no_blks_reqd)
1191 long int actual_block_no;
1192 /* di:double indirect */
1193 long int di_blockno_parent;
1194 long int di_blockno_child;
1195 __le32 *di_parent_buffer = NULL;
1196 __le32 *di_child_buff = NULL;
1197 __le32 *di_block_start_addr = NULL;
1198 __le32 *di_child_buff_start = NULL;
1199 struct ext_filesystem *fs = get_fs();
1201 if (*total_remaining_blocks != 0) {
1202 /* double indirect parent block connecting to inode */
1203 di_blockno_parent = ext4fs_get_new_blk_no();
1204 if (di_blockno_parent == -1) {
1205 printf("no block left to assign\n");
1208 di_parent_buffer = zalloc(fs->blksz);
1209 if (!di_parent_buffer)
1212 di_block_start_addr = di_parent_buffer;
1214 debug("DIPB %ld: %u\n", di_blockno_parent,
1215 *total_remaining_blocks);
1217 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1219 fs->blksz, (char *)di_parent_buffer);
1222 printf("%s: Device read error!\n", __func__);
1225 memset(di_parent_buffer, '\0', fs->blksz);
1228 * start:for each double indirect parent
1229 * block create one more block
1231 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1232 di_blockno_child = ext4fs_get_new_blk_no();
1233 if (di_blockno_child == -1) {
1234 printf("no block left to assign\n");
1237 di_child_buff = zalloc(fs->blksz);
1241 di_child_buff_start = di_child_buff;
1242 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1245 debug("DICB %ld: %u\n", di_blockno_child,
1246 *total_remaining_blocks);
1248 status = ext4fs_devread((lbaint_t)di_blockno_child *
1251 (char *)di_child_buff);
1254 printf("%s: Device read error!\n", __func__);
1257 memset(di_child_buff, '\0', fs->blksz);
1258 /* filling of actual datablocks for each child */
1259 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1260 actual_block_no = ext4fs_get_new_blk_no();
1261 if (actual_block_no == -1) {
1262 printf("no block left to assign\n");
1265 *di_child_buff = cpu_to_le32(actual_block_no);
1266 debug("DIAB %ld: %u\n", actual_block_no,
1267 *total_remaining_blocks);
1270 (*total_remaining_blocks)--;
1271 if (*total_remaining_blocks == 0)
1274 /* write the block table */
1275 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1276 di_child_buff_start, fs->blksz);
1277 free(di_child_buff_start);
1278 di_child_buff_start = NULL;
1280 if (*total_remaining_blocks == 0)
1283 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1284 di_block_start_addr, fs->blksz);
1285 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1288 free(di_block_start_addr);
1291 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1292 unsigned int *total_remaining_blocks,
1293 unsigned int *no_blks_reqd)
1298 long int actual_block_no;
1299 /* ti: Triple Indirect */
1300 long int ti_gp_blockno;
1301 long int ti_parent_blockno;
1302 long int ti_child_blockno;
1303 __le32 *ti_gp_buff = NULL;
1304 __le32 *ti_parent_buff = NULL;
1305 __le32 *ti_child_buff = NULL;
1306 __le32 *ti_gp_buff_start_addr = NULL;
1307 __le32 *ti_pbuff_start_addr = NULL;
1308 __le32 *ti_cbuff_start_addr = NULL;
1309 struct ext_filesystem *fs = get_fs();
1310 if (*total_remaining_blocks != 0) {
1311 /* triple indirect grand parent block connecting to inode */
1312 ti_gp_blockno = ext4fs_get_new_blk_no();
1313 if (ti_gp_blockno == -1) {
1314 printf("no block left to assign\n");
1317 ti_gp_buff = zalloc(fs->blksz);
1321 ti_gp_buff_start_addr = ti_gp_buff;
1323 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1324 *total_remaining_blocks);
1326 /* for each 4 byte grand parent entry create one more block */
1327 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1328 ti_parent_blockno = ext4fs_get_new_blk_no();
1329 if (ti_parent_blockno == -1) {
1330 printf("no block left to assign\n");
1333 ti_parent_buff = zalloc(fs->blksz);
1334 if (!ti_parent_buff)
1337 ti_pbuff_start_addr = ti_parent_buff;
1338 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1341 debug("TIPB %ld: %u\n", ti_parent_blockno,
1342 *total_remaining_blocks);
1344 /* for each 4 byte entry parent create one more block */
1345 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1346 ti_child_blockno = ext4fs_get_new_blk_no();
1347 if (ti_child_blockno == -1) {
1348 printf("no block left assign\n");
1351 ti_child_buff = zalloc(fs->blksz);
1355 ti_cbuff_start_addr = ti_child_buff;
1356 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1359 debug("TICB %ld: %u\n", ti_parent_blockno,
1360 *total_remaining_blocks);
1362 /* fill actual datablocks for each child */
1363 for (k = 0; k < (fs->blksz / sizeof(int));
1366 ext4fs_get_new_blk_no();
1367 if (actual_block_no == -1) {
1368 printf("no block left\n");
1369 free(ti_cbuff_start_addr);
1372 *ti_child_buff = cpu_to_le32(actual_block_no);
1373 debug("TIAB %ld: %u\n", actual_block_no,
1374 *total_remaining_blocks);
1377 (*total_remaining_blocks)--;
1378 if (*total_remaining_blocks == 0)
1381 /* write the child block */
1382 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1383 (uint64_t)fs->blksz)),
1384 ti_cbuff_start_addr, fs->blksz);
1385 free(ti_cbuff_start_addr);
1387 if (*total_remaining_blocks == 0)
1390 /* write the parent block */
1391 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1392 ti_pbuff_start_addr, fs->blksz);
1393 free(ti_pbuff_start_addr);
1395 if (*total_remaining_blocks == 0)
1398 /* write the grand parent block */
1399 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1400 ti_gp_buff_start_addr, fs->blksz);
1401 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1402 free(ti_gp_buff_start_addr);
1406 free(ti_pbuff_start_addr);
1408 free(ti_gp_buff_start_addr);
1411 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1412 unsigned int total_remaining_blocks,
1413 unsigned int *total_no_of_block)
1416 long int direct_blockno;
1417 unsigned int no_blks_reqd = 0;
1419 /* allocation of direct blocks */
1420 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1421 direct_blockno = ext4fs_get_new_blk_no();
1422 if (direct_blockno == -1) {
1423 printf("no block left to assign\n");
1426 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1427 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1429 total_remaining_blocks--;
1432 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1434 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1436 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1438 *total_no_of_block += no_blks_reqd;
1443 static struct ext4_extent_header *ext4fs_get_extent_block
1444 (struct ext2_data *data, char *buf,
1445 struct ext4_extent_header *ext_block,
1446 uint32_t fileblock, int log2_blksz)
1448 struct ext4_extent_idx *index;
1449 unsigned long long block;
1450 int blksz = EXT2_BLOCK_SIZE(data);
1454 index = (struct ext4_extent_idx *)(ext_block + 1);
1456 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1459 if (ext_block->eh_depth == 0)
1464 if (i >= le16_to_cpu(ext_block->eh_entries))
1466 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1471 block = le16_to_cpu(index[i].ei_leaf_hi);
1472 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1474 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1476 ext_block = (struct ext4_extent_header *)buf;
1482 static int ext4fs_blockgroup
1483 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1486 unsigned int blkoff, desc_per_blk;
1487 int log2blksz = get_fs()->dev_desc->log2blksz;
1489 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1491 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1492 group / desc_per_blk;
1493 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1495 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1496 group, blkno, blkoff);
1498 return ext4fs_devread((lbaint_t)blkno <<
1499 (LOG2_BLOCK_SIZE(data) - log2blksz),
1500 blkoff, sizeof(struct ext2_block_group),
1504 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1506 struct ext2_block_group blkgrp;
1507 struct ext2_sblock *sblock = &data->sblock;
1508 struct ext_filesystem *fs = get_fs();
1509 int log2blksz = get_fs()->dev_desc->log2blksz;
1510 int inodes_per_block, status;
1512 unsigned int blkoff;
1514 /* It is easier to calculate if the first inode is 0. */
1516 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1517 (sblock->inodes_per_group), &blkgrp);
1521 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1522 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1523 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1524 blkoff = (ino % inodes_per_block) * fs->inodesz;
1525 /* Read the inode. */
1526 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1528 sizeof(struct ext2_inode), (char *)inode);
1535 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1542 long int perblock_parent;
1543 long int perblock_child;
1544 unsigned long long start;
1545 /* get the blocksize of the filesystem */
1546 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1547 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1548 - get_fs()->dev_desc->log2blksz;
1550 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1551 char *buf = zalloc(blksz);
1554 struct ext4_extent_header *ext_block;
1555 struct ext4_extent *extent;
1558 ext4fs_get_extent_block(ext4fs_root, buf,
1559 (struct ext4_extent_header *)
1560 inode->b.blocks.dir_blocks,
1561 fileblock, log2_blksz);
1563 printf("invalid extent block\n");
1568 extent = (struct ext4_extent *)(ext_block + 1);
1572 if (i >= le16_to_cpu(ext_block->eh_entries))
1574 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1576 fileblock -= le32_to_cpu(extent[i].ee_block);
1577 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1582 start = le16_to_cpu(extent[i].ee_start_hi);
1583 start = (start << 32) +
1584 le32_to_cpu(extent[i].ee_start_lo);
1586 return fileblock + start;
1589 printf("Extent Error\n");
1594 /* Direct blocks. */
1595 if (fileblock < INDIRECT_BLOCKS)
1596 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1599 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1600 if (ext4fs_indir1_block == NULL) {
1601 ext4fs_indir1_block = zalloc(blksz);
1602 if (ext4fs_indir1_block == NULL) {
1603 printf("** SI ext2fs read block (indir 1)"
1604 "malloc failed. **\n");
1607 ext4fs_indir1_size = blksz;
1608 ext4fs_indir1_blkno = -1;
1610 if (blksz != ext4fs_indir1_size) {
1611 free(ext4fs_indir1_block);
1612 ext4fs_indir1_block = NULL;
1613 ext4fs_indir1_size = 0;
1614 ext4fs_indir1_blkno = -1;
1615 ext4fs_indir1_block = zalloc(blksz);
1616 if (ext4fs_indir1_block == NULL) {
1617 printf("** SI ext2fs read block (indir 1):"
1618 "malloc failed. **\n");
1621 ext4fs_indir1_size = blksz;
1623 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1624 log2_blksz) != ext4fs_indir1_blkno) {
1626 ext4fs_devread((lbaint_t)le32_to_cpu
1628 indir_block) << log2_blksz, 0,
1629 blksz, (char *)ext4fs_indir1_block);
1631 printf("** SI ext2fs read block (indir 1)"
1635 ext4fs_indir1_blkno =
1636 le32_to_cpu(inode->b.blocks.
1637 indir_block) << log2_blksz;
1639 blknr = le32_to_cpu(ext4fs_indir1_block
1640 [fileblock - INDIRECT_BLOCKS]);
1642 /* Double indirect. */
1643 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1644 (blksz / 4 + 1)))) {
1646 long int perblock = blksz / 4;
1647 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1649 if (ext4fs_indir1_block == NULL) {
1650 ext4fs_indir1_block = zalloc(blksz);
1651 if (ext4fs_indir1_block == NULL) {
1652 printf("** DI ext2fs read block (indir 2 1)"
1653 "malloc failed. **\n");
1656 ext4fs_indir1_size = blksz;
1657 ext4fs_indir1_blkno = -1;
1659 if (blksz != ext4fs_indir1_size) {
1660 free(ext4fs_indir1_block);
1661 ext4fs_indir1_block = NULL;
1662 ext4fs_indir1_size = 0;
1663 ext4fs_indir1_blkno = -1;
1664 ext4fs_indir1_block = zalloc(blksz);
1665 if (ext4fs_indir1_block == NULL) {
1666 printf("** DI ext2fs read block (indir 2 1)"
1667 "malloc failed. **\n");
1670 ext4fs_indir1_size = blksz;
1672 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1673 log2_blksz) != ext4fs_indir1_blkno) {
1675 ext4fs_devread((lbaint_t)le32_to_cpu
1677 double_indir_block) << log2_blksz,
1679 (char *)ext4fs_indir1_block);
1681 printf("** DI ext2fs read block (indir 2 1)"
1685 ext4fs_indir1_blkno =
1686 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1690 if (ext4fs_indir2_block == NULL) {
1691 ext4fs_indir2_block = zalloc(blksz);
1692 if (ext4fs_indir2_block == NULL) {
1693 printf("** DI ext2fs read block (indir 2 2)"
1694 "malloc failed. **\n");
1697 ext4fs_indir2_size = blksz;
1698 ext4fs_indir2_blkno = -1;
1700 if (blksz != ext4fs_indir2_size) {
1701 free(ext4fs_indir2_block);
1702 ext4fs_indir2_block = NULL;
1703 ext4fs_indir2_size = 0;
1704 ext4fs_indir2_blkno = -1;
1705 ext4fs_indir2_block = zalloc(blksz);
1706 if (ext4fs_indir2_block == NULL) {
1707 printf("** DI ext2fs read block (indir 2 2)"
1708 "malloc failed. **\n");
1711 ext4fs_indir2_size = blksz;
1713 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1714 log2_blksz) != ext4fs_indir2_blkno) {
1715 status = ext4fs_devread((lbaint_t)le32_to_cpu
1716 (ext4fs_indir1_block
1718 perblock]) << log2_blksz, 0,
1720 (char *)ext4fs_indir2_block);
1722 printf("** DI ext2fs read block (indir 2 2)"
1726 ext4fs_indir2_blkno =
1727 le32_to_cpu(ext4fs_indir1_block[rblock
1732 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1734 /* Tripple indirect. */
1736 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1737 (blksz / 4 * blksz / 4));
1738 perblock_child = blksz / 4;
1739 perblock_parent = ((blksz / 4) * (blksz / 4));
1741 if (ext4fs_indir1_block == NULL) {
1742 ext4fs_indir1_block = zalloc(blksz);
1743 if (ext4fs_indir1_block == NULL) {
1744 printf("** TI ext2fs read block (indir 2 1)"
1745 "malloc failed. **\n");
1748 ext4fs_indir1_size = blksz;
1749 ext4fs_indir1_blkno = -1;
1751 if (blksz != ext4fs_indir1_size) {
1752 free(ext4fs_indir1_block);
1753 ext4fs_indir1_block = NULL;
1754 ext4fs_indir1_size = 0;
1755 ext4fs_indir1_blkno = -1;
1756 ext4fs_indir1_block = zalloc(blksz);
1757 if (ext4fs_indir1_block == NULL) {
1758 printf("** TI ext2fs read block (indir 2 1)"
1759 "malloc failed. **\n");
1762 ext4fs_indir1_size = blksz;
1764 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1765 log2_blksz) != ext4fs_indir1_blkno) {
1766 status = ext4fs_devread
1768 le32_to_cpu(inode->b.blocks.triple_indir_block)
1769 << log2_blksz, 0, blksz,
1770 (char *)ext4fs_indir1_block);
1772 printf("** TI ext2fs read block (indir 2 1)"
1776 ext4fs_indir1_blkno =
1777 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1781 if (ext4fs_indir2_block == NULL) {
1782 ext4fs_indir2_block = zalloc(blksz);
1783 if (ext4fs_indir2_block == NULL) {
1784 printf("** TI ext2fs read block (indir 2 2)"
1785 "malloc failed. **\n");
1788 ext4fs_indir2_size = blksz;
1789 ext4fs_indir2_blkno = -1;
1791 if (blksz != ext4fs_indir2_size) {
1792 free(ext4fs_indir2_block);
1793 ext4fs_indir2_block = NULL;
1794 ext4fs_indir2_size = 0;
1795 ext4fs_indir2_blkno = -1;
1796 ext4fs_indir2_block = zalloc(blksz);
1797 if (ext4fs_indir2_block == NULL) {
1798 printf("** TI ext2fs read block (indir 2 2)"
1799 "malloc failed. **\n");
1802 ext4fs_indir2_size = blksz;
1804 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1805 perblock_parent]) <<
1807 != ext4fs_indir2_blkno) {
1808 status = ext4fs_devread((lbaint_t)le32_to_cpu
1809 (ext4fs_indir1_block
1811 perblock_parent]) <<
1812 log2_blksz, 0, blksz,
1813 (char *)ext4fs_indir2_block);
1815 printf("** TI ext2fs read block (indir 2 2)"
1819 ext4fs_indir2_blkno =
1820 le32_to_cpu(ext4fs_indir1_block[rblock /
1825 if (ext4fs_indir3_block == NULL) {
1826 ext4fs_indir3_block = zalloc(blksz);
1827 if (ext4fs_indir3_block == NULL) {
1828 printf("** TI ext2fs read block (indir 2 2)"
1829 "malloc failed. **\n");
1832 ext4fs_indir3_size = blksz;
1833 ext4fs_indir3_blkno = -1;
1835 if (blksz != ext4fs_indir3_size) {
1836 free(ext4fs_indir3_block);
1837 ext4fs_indir3_block = NULL;
1838 ext4fs_indir3_size = 0;
1839 ext4fs_indir3_blkno = -1;
1840 ext4fs_indir3_block = zalloc(blksz);
1841 if (ext4fs_indir3_block == NULL) {
1842 printf("** TI ext2fs read block (indir 2 2)"
1843 "malloc failed. **\n");
1846 ext4fs_indir3_size = blksz;
1848 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1851 log2_blksz) != ext4fs_indir3_blkno) {
1853 ext4fs_devread((lbaint_t)le32_to_cpu
1854 (ext4fs_indir2_block
1855 [(rblock / perblock_child)
1856 % (blksz / 4)]) << log2_blksz, 0,
1857 blksz, (char *)ext4fs_indir3_block);
1859 printf("** TI ext2fs read block (indir 2 2)"
1863 ext4fs_indir3_blkno =
1864 le32_to_cpu(ext4fs_indir2_block[(rblock /
1871 blknr = le32_to_cpu(ext4fs_indir3_block
1872 [rblock % perblock_child]);
1874 debug("read_allocated_block %ld\n", blknr);
1880 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1883 * This function assures that for a file with the same name but different size
1884 * the sequential store on the ext4 filesystem will be correct.
1886 * In this function the global data, responsible for internal representation
1887 * of the ext4 data are initialized to the reset state. Without this, during
1888 * replacement of the smaller file with the bigger truncation of new file was
1891 void ext4fs_reinit_global(void)
1893 if (ext4fs_indir1_block != NULL) {
1894 free(ext4fs_indir1_block);
1895 ext4fs_indir1_block = NULL;
1896 ext4fs_indir1_size = 0;
1897 ext4fs_indir1_blkno = -1;
1899 if (ext4fs_indir2_block != NULL) {
1900 free(ext4fs_indir2_block);
1901 ext4fs_indir2_block = NULL;
1902 ext4fs_indir2_size = 0;
1903 ext4fs_indir2_blkno = -1;
1905 if (ext4fs_indir3_block != NULL) {
1906 free(ext4fs_indir3_block);
1907 ext4fs_indir3_block = NULL;
1908 ext4fs_indir3_size = 0;
1909 ext4fs_indir3_blkno = -1;
1912 void ext4fs_close(void)
1914 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1915 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1918 if (ext4fs_root != NULL) {
1923 ext4fs_reinit_global();
1926 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1927 struct ext2fs_node **fnode, int *ftype)
1929 unsigned int fpos = 0;
1932 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1936 printf("Iterate dir %s\n", name);
1937 #endif /* of DEBUG */
1938 if (!diro->inode_read) {
1939 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1943 /* Search the file. */
1944 while (fpos < le32_to_cpu(diro->inode.size)) {
1945 struct ext2_dirent dirent;
1947 status = ext4fs_read_file(diro, fpos,
1948 sizeof(struct ext2_dirent),
1949 (char *)&dirent, &actread);
1953 if (dirent.direntlen == 0) {
1954 printf("Failed to iterate over directory %s\n", name);
1958 if (dirent.namelen != 0) {
1959 char filename[dirent.namelen + 1];
1960 struct ext2fs_node *fdiro;
1961 int type = FILETYPE_UNKNOWN;
1963 status = ext4fs_read_file(diro,
1965 sizeof(struct ext2_dirent),
1966 dirent.namelen, filename,
1971 fdiro = zalloc(sizeof(struct ext2fs_node));
1975 fdiro->data = diro->data;
1976 fdiro->ino = le32_to_cpu(dirent.inode);
1978 filename[dirent.namelen] = '\0';
1980 if (dirent.filetype != FILETYPE_UNKNOWN) {
1981 fdiro->inode_read = 0;
1983 if (dirent.filetype == FILETYPE_DIRECTORY)
1984 type = FILETYPE_DIRECTORY;
1985 else if (dirent.filetype == FILETYPE_SYMLINK)
1986 type = FILETYPE_SYMLINK;
1987 else if (dirent.filetype == FILETYPE_REG)
1988 type = FILETYPE_REG;
1990 status = ext4fs_read_inode(diro->data,
1998 fdiro->inode_read = 1;
2000 if ((le16_to_cpu(fdiro->inode.mode) &
2001 FILETYPE_INO_MASK) ==
2002 FILETYPE_INO_DIRECTORY) {
2003 type = FILETYPE_DIRECTORY;
2004 } else if ((le16_to_cpu(fdiro->inode.mode)
2005 & FILETYPE_INO_MASK) ==
2006 FILETYPE_INO_SYMLINK) {
2007 type = FILETYPE_SYMLINK;
2008 } else if ((le16_to_cpu(fdiro->inode.mode)
2009 & FILETYPE_INO_MASK) ==
2011 type = FILETYPE_REG;
2015 printf("iterate >%s<\n", filename);
2016 #endif /* of DEBUG */
2017 if ((name != NULL) && (fnode != NULL)
2018 && (ftype != NULL)) {
2019 if (strcmp(filename, name) == 0) {
2025 if (fdiro->inode_read == 0) {
2026 status = ext4fs_read_inode(diro->data,
2034 fdiro->inode_read = 1;
2037 case FILETYPE_DIRECTORY:
2040 case FILETYPE_SYMLINK:
2051 le32_to_cpu(fdiro->inode.size),
2056 fpos += le16_to_cpu(dirent.direntlen);
2061 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2064 struct ext2fs_node *diro = node;
2068 if (!diro->inode_read) {
2069 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2073 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2077 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2078 strncpy(symlink, diro->inode.b.symlink,
2079 le32_to_cpu(diro->inode.size));
2081 status = ext4fs_read_file(diro, 0,
2082 le32_to_cpu(diro->inode.size),
2084 if ((status < 0) || (actread == 0)) {
2089 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2093 static int ext4fs_find_file1(const char *currpath,
2094 struct ext2fs_node *currroot,
2095 struct ext2fs_node **currfound, int *foundtype)
2097 char fpath[strlen(currpath) + 1];
2101 int type = FILETYPE_DIRECTORY;
2102 struct ext2fs_node *currnode = currroot;
2103 struct ext2fs_node *oldnode = currroot;
2105 strncpy(fpath, currpath, strlen(currpath) + 1);
2107 /* Remove all leading slashes. */
2108 while (*name == '/')
2112 *currfound = currnode;
2119 /* Extract the actual part from the pathname. */
2120 next = strchr(name, '/');
2122 /* Remove all leading slashes. */
2123 while (*next == '/')
2127 if (type != FILETYPE_DIRECTORY) {
2128 ext4fs_free_node(currnode, currroot);
2134 /* Iterate over the directory. */
2135 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2142 /* Read in the symlink and follow it. */
2143 if (type == FILETYPE_SYMLINK) {
2146 /* Test if the symlink does not loop. */
2147 if (++symlinknest == 8) {
2148 ext4fs_free_node(currnode, currroot);
2149 ext4fs_free_node(oldnode, currroot);
2153 symlink = ext4fs_read_symlink(currnode);
2154 ext4fs_free_node(currnode, currroot);
2157 ext4fs_free_node(oldnode, currroot);
2161 debug("Got symlink >%s<\n", symlink);
2163 if (symlink[0] == '/') {
2164 ext4fs_free_node(oldnode, currroot);
2165 oldnode = &ext4fs_root->diropen;
2168 /* Lookup the node the symlink points to. */
2169 status = ext4fs_find_file1(symlink, oldnode,
2175 ext4fs_free_node(oldnode, currroot);
2180 ext4fs_free_node(oldnode, currroot);
2182 /* Found the node! */
2183 if (!next || *next == '\0') {
2184 *currfound = currnode;
2193 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2194 struct ext2fs_node **foundnode, int expecttype)
2197 int foundtype = FILETYPE_DIRECTORY;
2203 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2207 /* Check if the node that was found was of the expected type. */
2208 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2210 else if ((expecttype == FILETYPE_DIRECTORY)
2211 && (foundtype != expecttype))
2217 int ext4fs_open(const char *filename, loff_t *len)
2219 struct ext2fs_node *fdiro = NULL;
2222 if (ext4fs_root == NULL)
2226 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2231 if (!fdiro->inode_read) {
2232 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2237 *len = le32_to_cpu(fdiro->inode.size);
2238 ext4fs_file = fdiro;
2242 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2247 int ext4fs_mount(unsigned part_length)
2249 struct ext2_data *data;
2251 struct ext_filesystem *fs = get_fs();
2252 data = zalloc(SUPERBLOCK_SIZE);
2256 /* Read the superblock. */
2257 status = ext4_read_superblock((char *)&data->sblock);
2262 /* Make sure this is an ext2 filesystem. */
2263 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2267 * The 64bit feature was enabled when metadata_csum was enabled
2268 * and we do not support metadata_csum (and cannot reliably find
2269 * files when it is set. Refuse to mount.
2271 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
2272 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2276 if (le32_to_cpu(data->sblock.revision_level == 0))
2279 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2281 debug("EXT2 rev %d, inode_size %d\n",
2282 le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2284 data->diropen.data = data;
2285 data->diropen.ino = 2;
2286 data->diropen.inode_read = 1;
2287 data->inode = &data->diropen.inode;
2289 status = ext4fs_read_inode(data, 2, data->inode);
2297 printf("Failed to mount ext2 filesystem...\n");