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 int ext4fs_update_parent_dentry(char *filename, int file_type)
371 unsigned int *zero_buffer = NULL;
372 char *root_first_block_buffer = NULL;
374 long int first_block_no_of_root = 0;
376 unsigned int new_entry_byte_reqd;
377 int sizeof_void_space = 0;
381 struct ext_filesystem *fs = get_fs();
382 /* directory entry */
383 struct ext2_dirent *dir;
384 char *temp_dir = NULL;
387 uint32_t new_blockcnt;
388 uint32_t directory_blocks;
390 zero_buffer = zalloc(fs->blksz);
392 printf("No Memory\n");
395 root_first_block_buffer = zalloc(fs->blksz);
396 if (!root_first_block_buffer) {
398 printf("No Memory\n");
401 new_entry_byte_reqd = ROUND(strlen(filename) +
402 sizeof(struct ext2_dirent), 4);
404 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
405 LOG2_BLOCK_SIZE(ext4fs_root);
406 blk_idx = directory_blocks - 1;
409 /* read the block no allocated to a file */
410 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
411 if (first_block_no_of_root <= 0)
414 status = ext4fs_devread((lbaint_t)first_block_no_of_root
416 0, fs->blksz, root_first_block_buffer);
420 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
422 dir = (struct ext2_dirent *)root_first_block_buffer;
425 while (le16_to_cpu(dir->direntlen) > 0) {
426 unsigned short used_len = ROUND(dir->namelen +
427 sizeof(struct ext2_dirent), 4);
429 /* last entry of block */
430 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
432 /* check if new entry fits */
433 if ((used_len + new_entry_byte_reqd) <=
434 le16_to_cpu(dir->direntlen)) {
435 dir->direntlen = cpu_to_le16(used_len);
439 printf("Block full, trying previous\n");
443 printf("All blocks full: Allocate new\n");
445 if (le32_to_cpu(g_parent_inode->flags) &
447 printf("Directory uses extents\n");
450 if (directory_blocks >= INDIRECT_BLOCKS) {
451 printf("Directory exceeds limit\n");
454 new_blk_no = ext4fs_get_new_blk_no();
455 if (new_blk_no == -1) {
456 printf("no block left to assign\n");
459 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
460 g_parent_inode->b.blocks.
461 dir_blocks[directory_blocks] =
462 cpu_to_le32(new_blk_no);
464 new_size = le32_to_cpu(g_parent_inode->size);
465 new_size += fs->blksz;
466 g_parent_inode->size = cpu_to_le32(new_size);
468 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
469 new_blockcnt += fs->sect_perblk;
470 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
472 if (ext4fs_put_metadata
473 (root_first_block_buffer,
474 first_block_no_of_root))
480 templength = le16_to_cpu(dir->direntlen);
481 totalbytes = totalbytes + templength;
482 sizeof_void_space = check_void_in_dentry(dir, filename);
483 if (sizeof_void_space)
486 dir = (struct ext2_dirent *)((char *)dir + templength);
489 /* make a pointer ready for creating next directory entry */
490 templength = le16_to_cpu(dir->direntlen);
491 totalbytes = totalbytes + templength;
492 dir = (struct ext2_dirent *)((char *)dir + templength);
494 /* get the next available inode number */
495 inodeno = ext4fs_get_new_inode_no();
497 printf("no inode left to assign\n");
500 dir->inode = cpu_to_le32(inodeno);
501 if (sizeof_void_space)
502 dir->direntlen = cpu_to_le16(sizeof_void_space);
504 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
506 dir->namelen = strlen(filename);
507 dir->filetype = FILETYPE_REG; /* regular file */
508 temp_dir = (char *)dir;
509 temp_dir = temp_dir + sizeof(struct ext2_dirent);
510 memcpy(temp_dir, filename, strlen(filename));
512 /* update or write the 1st block of root inode */
513 if (ext4fs_put_metadata(root_first_block_buffer,
514 first_block_no_of_root))
519 free(root_first_block_buffer);
524 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
533 unsigned char *block_buffer = NULL;
534 struct ext2_dirent *dir = NULL;
535 struct ext_filesystem *fs = get_fs();
537 /* read the block no allocated to a file */
538 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
540 blknr = read_allocated_block(parent_inode, direct_blk_idx);
544 /* read the blocks of parent inode */
545 block_buffer = zalloc(fs->blksz);
549 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
550 0, fs->blksz, (char *)block_buffer);
554 dir = (struct ext2_dirent *)block_buffer;
557 while (le16_to_cpu(dir->direntlen) >= 0) {
559 * blocksize-totalbytes because last directory
560 * length i.e.,*dir->direntlen is free availble
561 * space in the block that means
562 * it is a last entry of directory entry
564 if (dir->inode && (strlen(dirname) == dir->namelen)) {
565 if (strncmp(dirname, ptr + sizeof(struct ext2_dirent), dir->namelen) == 0) {
566 inodeno = le32_to_cpu(dir->inode);
571 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
574 /* traversing the each directory entry */
575 templength = le16_to_cpu(dir->direntlen);
576 totalbytes = totalbytes + templength;
577 dir = (struct ext2_dirent *)((char *)dir + templength);
594 static int find_dir_depth(char *dirname)
596 char *token = strtok(dirname, "/");
598 while (token != NULL) {
599 token = strtok(NULL, "/");
602 return count + 1 + 1;
604 * for example for string /home/temp
605 * depth=home(1)+temp(1)+1 extra for NULL;
610 static int parse_path(char **arr, char *dirname)
612 char *token = strtok(dirname, "/");
616 arr[i] = zalloc(strlen("/") + 1);
619 memcpy(arr[i++], "/", strlen("/"));
621 /* add each path entry after root */
622 while (token != NULL) {
623 arr[i] = zalloc(strlen(token) + 1);
626 memcpy(arr[i++], token, strlen(token));
627 token = strtok(NULL, "/");
634 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
636 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
643 * Function: ext4fs_get_parent_inode_num
644 * Return Value: inode Number of the parent directory of file/Directory to be
646 * dirname : Input parmater, input path name of the file/directory to be created
647 * dname : Output parameter, to be filled with the name of the directory
648 * extracted from dirname
650 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
654 int matched_inode_no;
655 int result_inode_no = -1;
657 char *depth_dirname = NULL;
658 char *parse_dirname = NULL;
659 struct ext2_inode *parent_inode = NULL;
660 struct ext2_inode *first_inode = NULL;
661 struct ext2_inode temp_inode;
663 if (*dirname != '/') {
664 printf("Please supply Absolute path\n");
668 /* TODO: input validation make equivalent to linux */
669 depth_dirname = zalloc(strlen(dirname) + 1);
673 memcpy(depth_dirname, dirname, strlen(dirname));
674 depth = find_dir_depth(depth_dirname);
675 parse_dirname = zalloc(strlen(dirname) + 1);
678 memcpy(parse_dirname, dirname, strlen(dirname));
680 /* allocate memory for each directory level */
681 ptr = zalloc((depth) * sizeof(char *));
684 if (parse_path(ptr, parse_dirname))
686 parent_inode = zalloc(sizeof(struct ext2_inode));
689 first_inode = zalloc(sizeof(struct ext2_inode));
692 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
693 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
695 result_inode_no = EXT2_ROOT_INO;
696 for (i = 1; i < depth; i++) {
697 matched_inode_no = search_dir(parent_inode, ptr[i]);
698 if (matched_inode_no == -1) {
699 if (ptr[i + 1] == NULL && i == 1) {
700 result_inode_no = EXT2_ROOT_INO;
703 if (ptr[i + 1] == NULL)
705 printf("Invalid path\n");
706 result_inode_no = -1;
710 if (ptr[i + 1] != NULL) {
711 memset(parent_inode, '\0',
712 sizeof(struct ext2_inode));
713 if (ext4fs_iget(matched_inode_no,
715 result_inode_no = -1;
718 result_inode_no = matched_inode_no;
727 matched_inode_no = search_dir(first_inode, ptr[i]);
729 matched_inode_no = search_dir(parent_inode, ptr[i]);
731 if (matched_inode_no != -1) {
732 ext4fs_iget(matched_inode_no, &temp_inode);
733 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
734 printf("It is a Directory\n");
735 result_inode_no = -1;
740 if (strlen(ptr[i]) > 256) {
741 result_inode_no = -1;
744 memcpy(dname, ptr[i], strlen(ptr[i]));
749 for (i = 0; i < depth; i++) {
758 return result_inode_no;
761 static int unlink_filename(char *filename, unsigned int blknr)
767 char *root_first_block_buffer = NULL;
768 struct ext2_dirent *dir = NULL;
769 struct ext2_dirent *previous_dir = NULL;
771 struct ext_filesystem *fs = get_fs();
774 /* get the first block of root */
775 root_first_block_buffer = zalloc(fs->blksz);
776 if (!root_first_block_buffer)
778 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
779 fs->blksz, root_first_block_buffer);
783 if (ext4fs_log_journal(root_first_block_buffer, blknr))
785 dir = (struct ext2_dirent *)root_first_block_buffer;
788 while (le16_to_cpu(dir->direntlen) >= 0) {
790 * blocksize-totalbytes because last
791 * directory length i.e., *dir->direntlen
792 * is free availble space in the block that
793 * means it is a last entry of directory entry
795 if (dir->inode && (strlen(filename) == dir->namelen) &&
796 (strncmp(ptr + sizeof(struct ext2_dirent),
797 filename, dir->namelen) == 0)) {
798 printf("file found, deleting\n");
799 inodeno = le32_to_cpu(dir->inode);
802 new_len = le16_to_cpu(previous_dir->direntlen);
803 new_len += le16_to_cpu(dir->direntlen);
804 previous_dir->direntlen = cpu_to_le16(new_len);
812 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
815 /* traversing the each directory entry */
816 templength = le16_to_cpu(dir->direntlen);
817 totalbytes = totalbytes + templength;
819 dir = (struct ext2_dirent *)((char *)dir + templength);
825 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
830 free(root_first_block_buffer);
835 int ext4fs_filename_unlink(char *filename)
837 short direct_blk_idx = 0;
841 /* read the block no allocated to a file */
842 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
844 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
847 inodeno = unlink_filename(filename, blknr);
855 uint32_t ext4fs_get_new_blk_no(void)
861 static int prev_bg_bitmap_index = -1;
862 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
863 struct ext_filesystem *fs = get_fs();
864 char *journal_buffer = zalloc(fs->blksz);
865 char *zero_buffer = zalloc(fs->blksz);
866 if (!journal_buffer || !zero_buffer)
868 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
870 if (fs->first_pass_bbmap == 0) {
871 for (i = 0; i < fs->no_blkgrp; i++) {
872 if (le16_to_cpu(bgd[i].free_blocks)) {
873 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
875 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
876 zero_buffer, fs->blksz);
877 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
878 bgd[i].bg_flags = cpu_to_le16(new_flags);
879 memcpy(fs->blk_bmaps[i], zero_buffer,
883 _get_new_blk_no(fs->blk_bmaps[i]);
884 if (fs->curr_blkno == -1)
885 /* if block bitmap is completely fill */
887 fs->curr_blkno = fs->curr_blkno +
889 fs->first_pass_bbmap++;
890 ext4fs_bg_free_blocks_dec(&bgd[i]);
891 ext4fs_sb_free_blocks_dec(fs->sb);
892 status = ext4fs_devread(
893 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
899 if (ext4fs_log_journal(journal_buffer,
900 le32_to_cpu(bgd[i].block_id)))
904 debug("no space left on block group %d\n", i);
912 /* get the blockbitmap index respective to blockno */
913 bg_idx = fs->curr_blkno / blk_per_grp;
914 if (fs->blksz == 1024) {
915 remainder = fs->curr_blkno % blk_per_grp;
921 * To skip completely filled block group bitmaps
922 * Optimize the block allocation
924 if (bg_idx >= fs->no_blkgrp)
927 if (bgd[bg_idx].free_blocks == 0) {
928 debug("block group %u is full. Skipping\n", bg_idx);
929 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
934 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
936 memset(zero_buffer, '\0', fs->blksz);
937 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
938 zero_buffer, fs->blksz);
939 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
940 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
941 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
944 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
946 debug("going for restart for the block no %ld %u\n",
947 fs->curr_blkno, bg_idx);
952 if (prev_bg_bitmap_index != bg_idx) {
953 memset(journal_buffer, '\0', fs->blksz);
954 status = ext4fs_devread(
955 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
957 0, fs->blksz, journal_buffer);
960 if (ext4fs_log_journal(journal_buffer,
961 le32_to_cpu(bgd[bg_idx].block_id)))
964 prev_bg_bitmap_index = bg_idx;
966 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
967 ext4fs_sb_free_blocks_dec(fs->sb);
971 free(journal_buffer);
974 return fs->curr_blkno;
976 free(journal_buffer);
982 int ext4fs_get_new_inode_no(void)
986 unsigned int ibmap_idx;
987 static int prev_inode_bitmap_index = -1;
988 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
989 struct ext_filesystem *fs = get_fs();
990 char *journal_buffer = zalloc(fs->blksz);
991 char *zero_buffer = zalloc(fs->blksz);
992 if (!journal_buffer || !zero_buffer)
994 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
996 if (fs->first_pass_ibmap == 0) {
997 for (i = 0; i < fs->no_blkgrp; i++) {
998 if (bgd[i].free_inodes) {
999 if (bgd[i].bg_itable_unused !=
1001 bgd[i].bg_itable_unused =
1003 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1005 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
1006 zero_buffer, fs->blksz);
1007 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1008 bgd[i].bg_flags = cpu_to_le16(new_flags);
1009 memcpy(fs->inode_bmaps[i],
1010 zero_buffer, fs->blksz);
1013 _get_new_inode_no(fs->inode_bmaps[i]);
1014 if (fs->curr_inode_no == -1)
1015 /* if block bitmap is completely fill */
1017 fs->curr_inode_no = fs->curr_inode_no +
1018 (i * inodes_per_grp);
1019 fs->first_pass_ibmap++;
1020 ext4fs_bg_free_inodes_dec(&bgd[i]);
1021 ext4fs_bg_itable_unused_dec(&bgd[i]);
1022 ext4fs_sb_free_inodes_dec(fs->sb);
1023 status = ext4fs_devread(
1024 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
1030 if (ext4fs_log_journal(journal_buffer,
1031 le32_to_cpu(bgd[i].inode_id)))
1035 debug("no inode left on block group %d\n", i);
1040 fs->curr_inode_no++;
1041 /* get the blockbitmap index respective to blockno */
1042 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1043 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1045 memset(zero_buffer, '\0', fs->blksz);
1046 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1047 zero_buffer, fs->blksz);
1048 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1049 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
1050 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1054 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1055 fs->inode_bmaps[ibmap_idx],
1057 debug("going for restart for the block no %d %u\n",
1058 fs->curr_inode_no, ibmap_idx);
1062 /* journal backup */
1063 if (prev_inode_bitmap_index != ibmap_idx) {
1064 memset(journal_buffer, '\0', fs->blksz);
1065 status = ext4fs_devread(
1066 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
1068 0, fs->blksz, journal_buffer);
1071 if (ext4fs_log_journal(journal_buffer,
1072 le32_to_cpu(bgd[ibmap_idx].inode_id)))
1074 prev_inode_bitmap_index = ibmap_idx;
1076 if (bgd[ibmap_idx].bg_itable_unused !=
1077 bgd[ibmap_idx].free_inodes)
1078 bgd[ibmap_idx].bg_itable_unused =
1079 bgd[ibmap_idx].free_inodes;
1080 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1081 ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
1082 ext4fs_sb_free_inodes_dec(fs->sb);
1087 free(journal_buffer);
1090 return fs->curr_inode_no;
1092 free(journal_buffer);
1100 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1101 unsigned int *total_remaining_blocks,
1102 unsigned int *no_blks_reqd)
1106 long int actual_block_no;
1107 long int si_blockno;
1108 /* si :single indirect */
1109 __le32 *si_buffer = NULL;
1110 __le32 *si_start_addr = NULL;
1111 struct ext_filesystem *fs = get_fs();
1113 if (*total_remaining_blocks != 0) {
1114 si_buffer = zalloc(fs->blksz);
1116 printf("No Memory\n");
1119 si_start_addr = si_buffer;
1120 si_blockno = ext4fs_get_new_blk_no();
1121 if (si_blockno == -1) {
1122 printf("no block left to assign\n");
1126 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1128 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1129 0, fs->blksz, (char *)si_buffer);
1130 memset(si_buffer, '\0', fs->blksz);
1134 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1135 actual_block_no = ext4fs_get_new_blk_no();
1136 if (actual_block_no == -1) {
1137 printf("no block left to assign\n");
1140 *si_buffer = cpu_to_le32(actual_block_no);
1141 debug("SIAB %u: %u\n", *si_buffer,
1142 *total_remaining_blocks);
1145 (*total_remaining_blocks)--;
1146 if (*total_remaining_blocks == 0)
1150 /* write the block to disk */
1151 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1152 si_start_addr, fs->blksz);
1153 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1156 free(si_start_addr);
1159 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1160 unsigned int *total_remaining_blocks,
1161 unsigned int *no_blks_reqd)
1166 long int actual_block_no;
1167 /* di:double indirect */
1168 long int di_blockno_parent;
1169 long int di_blockno_child;
1170 __le32 *di_parent_buffer = NULL;
1171 __le32 *di_child_buff = NULL;
1172 __le32 *di_block_start_addr = NULL;
1173 __le32 *di_child_buff_start = NULL;
1174 struct ext_filesystem *fs = get_fs();
1176 if (*total_remaining_blocks != 0) {
1177 /* double indirect parent block connecting to inode */
1178 di_blockno_parent = ext4fs_get_new_blk_no();
1179 if (di_blockno_parent == -1) {
1180 printf("no block left to assign\n");
1183 di_parent_buffer = zalloc(fs->blksz);
1184 if (!di_parent_buffer)
1187 di_block_start_addr = di_parent_buffer;
1189 debug("DIPB %ld: %u\n", di_blockno_parent,
1190 *total_remaining_blocks);
1192 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1194 fs->blksz, (char *)di_parent_buffer);
1197 printf("%s: Device read error!\n", __func__);
1200 memset(di_parent_buffer, '\0', fs->blksz);
1203 * start:for each double indirect parent
1204 * block create one more block
1206 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1207 di_blockno_child = ext4fs_get_new_blk_no();
1208 if (di_blockno_child == -1) {
1209 printf("no block left to assign\n");
1212 di_child_buff = zalloc(fs->blksz);
1216 di_child_buff_start = di_child_buff;
1217 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1220 debug("DICB %ld: %u\n", di_blockno_child,
1221 *total_remaining_blocks);
1223 status = ext4fs_devread((lbaint_t)di_blockno_child *
1226 (char *)di_child_buff);
1229 printf("%s: Device read error!\n", __func__);
1232 memset(di_child_buff, '\0', fs->blksz);
1233 /* filling of actual datablocks for each child */
1234 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1235 actual_block_no = ext4fs_get_new_blk_no();
1236 if (actual_block_no == -1) {
1237 printf("no block left to assign\n");
1240 *di_child_buff = cpu_to_le32(actual_block_no);
1241 debug("DIAB %ld: %u\n", actual_block_no,
1242 *total_remaining_blocks);
1245 (*total_remaining_blocks)--;
1246 if (*total_remaining_blocks == 0)
1249 /* write the block table */
1250 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1251 di_child_buff_start, fs->blksz);
1252 free(di_child_buff_start);
1253 di_child_buff_start = NULL;
1255 if (*total_remaining_blocks == 0)
1258 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1259 di_block_start_addr, fs->blksz);
1260 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1263 free(di_block_start_addr);
1266 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1267 unsigned int *total_remaining_blocks,
1268 unsigned int *no_blks_reqd)
1273 long int actual_block_no;
1274 /* ti: Triple Indirect */
1275 long int ti_gp_blockno;
1276 long int ti_parent_blockno;
1277 long int ti_child_blockno;
1278 __le32 *ti_gp_buff = NULL;
1279 __le32 *ti_parent_buff = NULL;
1280 __le32 *ti_child_buff = NULL;
1281 __le32 *ti_gp_buff_start_addr = NULL;
1282 __le32 *ti_pbuff_start_addr = NULL;
1283 __le32 *ti_cbuff_start_addr = NULL;
1284 struct ext_filesystem *fs = get_fs();
1285 if (*total_remaining_blocks != 0) {
1286 /* triple indirect grand parent block connecting to inode */
1287 ti_gp_blockno = ext4fs_get_new_blk_no();
1288 if (ti_gp_blockno == -1) {
1289 printf("no block left to assign\n");
1292 ti_gp_buff = zalloc(fs->blksz);
1296 ti_gp_buff_start_addr = ti_gp_buff;
1298 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1299 *total_remaining_blocks);
1301 /* for each 4 byte grand parent entry create one more block */
1302 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1303 ti_parent_blockno = ext4fs_get_new_blk_no();
1304 if (ti_parent_blockno == -1) {
1305 printf("no block left to assign\n");
1308 ti_parent_buff = zalloc(fs->blksz);
1309 if (!ti_parent_buff)
1312 ti_pbuff_start_addr = ti_parent_buff;
1313 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1316 debug("TIPB %ld: %u\n", ti_parent_blockno,
1317 *total_remaining_blocks);
1319 /* for each 4 byte entry parent create one more block */
1320 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1321 ti_child_blockno = ext4fs_get_new_blk_no();
1322 if (ti_child_blockno == -1) {
1323 printf("no block left assign\n");
1326 ti_child_buff = zalloc(fs->blksz);
1330 ti_cbuff_start_addr = ti_child_buff;
1331 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1334 debug("TICB %ld: %u\n", ti_parent_blockno,
1335 *total_remaining_blocks);
1337 /* fill actual datablocks for each child */
1338 for (k = 0; k < (fs->blksz / sizeof(int));
1341 ext4fs_get_new_blk_no();
1342 if (actual_block_no == -1) {
1343 printf("no block left\n");
1344 free(ti_cbuff_start_addr);
1347 *ti_child_buff = cpu_to_le32(actual_block_no);
1348 debug("TIAB %ld: %u\n", actual_block_no,
1349 *total_remaining_blocks);
1352 (*total_remaining_blocks)--;
1353 if (*total_remaining_blocks == 0)
1356 /* write the child block */
1357 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1358 (uint64_t)fs->blksz)),
1359 ti_cbuff_start_addr, fs->blksz);
1360 free(ti_cbuff_start_addr);
1362 if (*total_remaining_blocks == 0)
1365 /* write the parent block */
1366 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1367 ti_pbuff_start_addr, fs->blksz);
1368 free(ti_pbuff_start_addr);
1370 if (*total_remaining_blocks == 0)
1373 /* write the grand parent block */
1374 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1375 ti_gp_buff_start_addr, fs->blksz);
1376 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1377 free(ti_gp_buff_start_addr);
1381 free(ti_pbuff_start_addr);
1383 free(ti_gp_buff_start_addr);
1386 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1387 unsigned int total_remaining_blocks,
1388 unsigned int *total_no_of_block)
1391 long int direct_blockno;
1392 unsigned int no_blks_reqd = 0;
1394 /* allocation of direct blocks */
1395 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1396 direct_blockno = ext4fs_get_new_blk_no();
1397 if (direct_blockno == -1) {
1398 printf("no block left to assign\n");
1401 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1402 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1404 total_remaining_blocks--;
1407 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1409 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1411 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1413 *total_no_of_block += no_blks_reqd;
1418 static struct ext4_extent_header *ext4fs_get_extent_block
1419 (struct ext2_data *data, char *buf,
1420 struct ext4_extent_header *ext_block,
1421 uint32_t fileblock, int log2_blksz)
1423 struct ext4_extent_idx *index;
1424 unsigned long long block;
1425 int blksz = EXT2_BLOCK_SIZE(data);
1429 index = (struct ext4_extent_idx *)(ext_block + 1);
1431 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1434 if (ext_block->eh_depth == 0)
1439 if (i >= le16_to_cpu(ext_block->eh_entries))
1441 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1446 block = le16_to_cpu(index[i].ei_leaf_hi);
1447 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1449 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1451 ext_block = (struct ext4_extent_header *)buf;
1457 static int ext4fs_blockgroup
1458 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1461 unsigned int blkoff, desc_per_blk;
1462 int log2blksz = get_fs()->dev_desc->log2blksz;
1464 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1466 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1467 group / desc_per_blk;
1468 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1470 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1471 group, blkno, blkoff);
1473 return ext4fs_devread((lbaint_t)blkno <<
1474 (LOG2_BLOCK_SIZE(data) - log2blksz),
1475 blkoff, sizeof(struct ext2_block_group),
1479 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1481 struct ext2_block_group blkgrp;
1482 struct ext2_sblock *sblock = &data->sblock;
1483 struct ext_filesystem *fs = get_fs();
1484 int log2blksz = get_fs()->dev_desc->log2blksz;
1485 int inodes_per_block, status;
1487 unsigned int blkoff;
1489 /* It is easier to calculate if the first inode is 0. */
1491 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1492 (sblock->inodes_per_group), &blkgrp);
1496 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1497 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1498 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1499 blkoff = (ino % inodes_per_block) * fs->inodesz;
1500 /* Read the inode. */
1501 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1503 sizeof(struct ext2_inode), (char *)inode);
1510 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1517 long int perblock_parent;
1518 long int perblock_child;
1519 unsigned long long start;
1520 /* get the blocksize of the filesystem */
1521 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1522 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1523 - get_fs()->dev_desc->log2blksz;
1525 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1526 char *buf = zalloc(blksz);
1529 struct ext4_extent_header *ext_block;
1530 struct ext4_extent *extent;
1533 ext4fs_get_extent_block(ext4fs_root, buf,
1534 (struct ext4_extent_header *)
1535 inode->b.blocks.dir_blocks,
1536 fileblock, log2_blksz);
1538 printf("invalid extent block\n");
1543 extent = (struct ext4_extent *)(ext_block + 1);
1547 if (i >= le16_to_cpu(ext_block->eh_entries))
1549 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1551 fileblock -= le32_to_cpu(extent[i].ee_block);
1552 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1557 start = le16_to_cpu(extent[i].ee_start_hi);
1558 start = (start << 32) +
1559 le32_to_cpu(extent[i].ee_start_lo);
1561 return fileblock + start;
1564 printf("Extent Error\n");
1569 /* Direct blocks. */
1570 if (fileblock < INDIRECT_BLOCKS)
1571 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1574 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1575 if (ext4fs_indir1_block == NULL) {
1576 ext4fs_indir1_block = zalloc(blksz);
1577 if (ext4fs_indir1_block == NULL) {
1578 printf("** SI ext2fs read block (indir 1)"
1579 "malloc failed. **\n");
1582 ext4fs_indir1_size = blksz;
1583 ext4fs_indir1_blkno = -1;
1585 if (blksz != ext4fs_indir1_size) {
1586 free(ext4fs_indir1_block);
1587 ext4fs_indir1_block = NULL;
1588 ext4fs_indir1_size = 0;
1589 ext4fs_indir1_blkno = -1;
1590 ext4fs_indir1_block = zalloc(blksz);
1591 if (ext4fs_indir1_block == NULL) {
1592 printf("** SI ext2fs read block (indir 1):"
1593 "malloc failed. **\n");
1596 ext4fs_indir1_size = blksz;
1598 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1599 log2_blksz) != ext4fs_indir1_blkno) {
1601 ext4fs_devread((lbaint_t)le32_to_cpu
1603 indir_block) << log2_blksz, 0,
1604 blksz, (char *)ext4fs_indir1_block);
1606 printf("** SI ext2fs read block (indir 1)"
1610 ext4fs_indir1_blkno =
1611 le32_to_cpu(inode->b.blocks.
1612 indir_block) << log2_blksz;
1614 blknr = le32_to_cpu(ext4fs_indir1_block
1615 [fileblock - INDIRECT_BLOCKS]);
1617 /* Double indirect. */
1618 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1619 (blksz / 4 + 1)))) {
1621 long int perblock = blksz / 4;
1622 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1624 if (ext4fs_indir1_block == NULL) {
1625 ext4fs_indir1_block = zalloc(blksz);
1626 if (ext4fs_indir1_block == NULL) {
1627 printf("** DI ext2fs read block (indir 2 1)"
1628 "malloc failed. **\n");
1631 ext4fs_indir1_size = blksz;
1632 ext4fs_indir1_blkno = -1;
1634 if (blksz != ext4fs_indir1_size) {
1635 free(ext4fs_indir1_block);
1636 ext4fs_indir1_block = NULL;
1637 ext4fs_indir1_size = 0;
1638 ext4fs_indir1_blkno = -1;
1639 ext4fs_indir1_block = zalloc(blksz);
1640 if (ext4fs_indir1_block == NULL) {
1641 printf("** DI ext2fs read block (indir 2 1)"
1642 "malloc failed. **\n");
1645 ext4fs_indir1_size = blksz;
1647 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1648 log2_blksz) != ext4fs_indir1_blkno) {
1650 ext4fs_devread((lbaint_t)le32_to_cpu
1652 double_indir_block) << log2_blksz,
1654 (char *)ext4fs_indir1_block);
1656 printf("** DI ext2fs read block (indir 2 1)"
1660 ext4fs_indir1_blkno =
1661 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1665 if (ext4fs_indir2_block == NULL) {
1666 ext4fs_indir2_block = zalloc(blksz);
1667 if (ext4fs_indir2_block == NULL) {
1668 printf("** DI ext2fs read block (indir 2 2)"
1669 "malloc failed. **\n");
1672 ext4fs_indir2_size = blksz;
1673 ext4fs_indir2_blkno = -1;
1675 if (blksz != ext4fs_indir2_size) {
1676 free(ext4fs_indir2_block);
1677 ext4fs_indir2_block = NULL;
1678 ext4fs_indir2_size = 0;
1679 ext4fs_indir2_blkno = -1;
1680 ext4fs_indir2_block = zalloc(blksz);
1681 if (ext4fs_indir2_block == NULL) {
1682 printf("** DI ext2fs read block (indir 2 2)"
1683 "malloc failed. **\n");
1686 ext4fs_indir2_size = blksz;
1688 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1689 log2_blksz) != ext4fs_indir2_blkno) {
1690 status = ext4fs_devread((lbaint_t)le32_to_cpu
1691 (ext4fs_indir1_block
1693 perblock]) << log2_blksz, 0,
1695 (char *)ext4fs_indir2_block);
1697 printf("** DI ext2fs read block (indir 2 2)"
1701 ext4fs_indir2_blkno =
1702 le32_to_cpu(ext4fs_indir1_block[rblock
1707 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1709 /* Tripple indirect. */
1711 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1712 (blksz / 4 * blksz / 4));
1713 perblock_child = blksz / 4;
1714 perblock_parent = ((blksz / 4) * (blksz / 4));
1716 if (ext4fs_indir1_block == NULL) {
1717 ext4fs_indir1_block = zalloc(blksz);
1718 if (ext4fs_indir1_block == NULL) {
1719 printf("** TI ext2fs read block (indir 2 1)"
1720 "malloc failed. **\n");
1723 ext4fs_indir1_size = blksz;
1724 ext4fs_indir1_blkno = -1;
1726 if (blksz != ext4fs_indir1_size) {
1727 free(ext4fs_indir1_block);
1728 ext4fs_indir1_block = NULL;
1729 ext4fs_indir1_size = 0;
1730 ext4fs_indir1_blkno = -1;
1731 ext4fs_indir1_block = zalloc(blksz);
1732 if (ext4fs_indir1_block == NULL) {
1733 printf("** TI ext2fs read block (indir 2 1)"
1734 "malloc failed. **\n");
1737 ext4fs_indir1_size = blksz;
1739 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1740 log2_blksz) != ext4fs_indir1_blkno) {
1741 status = ext4fs_devread
1743 le32_to_cpu(inode->b.blocks.triple_indir_block)
1744 << log2_blksz, 0, blksz,
1745 (char *)ext4fs_indir1_block);
1747 printf("** TI ext2fs read block (indir 2 1)"
1751 ext4fs_indir1_blkno =
1752 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1756 if (ext4fs_indir2_block == NULL) {
1757 ext4fs_indir2_block = zalloc(blksz);
1758 if (ext4fs_indir2_block == NULL) {
1759 printf("** TI ext2fs read block (indir 2 2)"
1760 "malloc failed. **\n");
1763 ext4fs_indir2_size = blksz;
1764 ext4fs_indir2_blkno = -1;
1766 if (blksz != ext4fs_indir2_size) {
1767 free(ext4fs_indir2_block);
1768 ext4fs_indir2_block = NULL;
1769 ext4fs_indir2_size = 0;
1770 ext4fs_indir2_blkno = -1;
1771 ext4fs_indir2_block = zalloc(blksz);
1772 if (ext4fs_indir2_block == NULL) {
1773 printf("** TI ext2fs read block (indir 2 2)"
1774 "malloc failed. **\n");
1777 ext4fs_indir2_size = blksz;
1779 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1780 perblock_parent]) <<
1782 != ext4fs_indir2_blkno) {
1783 status = ext4fs_devread((lbaint_t)le32_to_cpu
1784 (ext4fs_indir1_block
1786 perblock_parent]) <<
1787 log2_blksz, 0, blksz,
1788 (char *)ext4fs_indir2_block);
1790 printf("** TI ext2fs read block (indir 2 2)"
1794 ext4fs_indir2_blkno =
1795 le32_to_cpu(ext4fs_indir1_block[rblock /
1800 if (ext4fs_indir3_block == NULL) {
1801 ext4fs_indir3_block = zalloc(blksz);
1802 if (ext4fs_indir3_block == NULL) {
1803 printf("** TI ext2fs read block (indir 2 2)"
1804 "malloc failed. **\n");
1807 ext4fs_indir3_size = blksz;
1808 ext4fs_indir3_blkno = -1;
1810 if (blksz != ext4fs_indir3_size) {
1811 free(ext4fs_indir3_block);
1812 ext4fs_indir3_block = NULL;
1813 ext4fs_indir3_size = 0;
1814 ext4fs_indir3_blkno = -1;
1815 ext4fs_indir3_block = zalloc(blksz);
1816 if (ext4fs_indir3_block == NULL) {
1817 printf("** TI ext2fs read block (indir 2 2)"
1818 "malloc failed. **\n");
1821 ext4fs_indir3_size = blksz;
1823 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1826 log2_blksz) != ext4fs_indir3_blkno) {
1828 ext4fs_devread((lbaint_t)le32_to_cpu
1829 (ext4fs_indir2_block
1830 [(rblock / perblock_child)
1831 % (blksz / 4)]) << log2_blksz, 0,
1832 blksz, (char *)ext4fs_indir3_block);
1834 printf("** TI ext2fs read block (indir 2 2)"
1838 ext4fs_indir3_blkno =
1839 le32_to_cpu(ext4fs_indir2_block[(rblock /
1846 blknr = le32_to_cpu(ext4fs_indir3_block
1847 [rblock % perblock_child]);
1849 debug("read_allocated_block %ld\n", blknr);
1855 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1858 * This function assures that for a file with the same name but different size
1859 * the sequential store on the ext4 filesystem will be correct.
1861 * In this function the global data, responsible for internal representation
1862 * of the ext4 data are initialized to the reset state. Without this, during
1863 * replacement of the smaller file with the bigger truncation of new file was
1866 void ext4fs_reinit_global(void)
1868 if (ext4fs_indir1_block != NULL) {
1869 free(ext4fs_indir1_block);
1870 ext4fs_indir1_block = NULL;
1871 ext4fs_indir1_size = 0;
1872 ext4fs_indir1_blkno = -1;
1874 if (ext4fs_indir2_block != NULL) {
1875 free(ext4fs_indir2_block);
1876 ext4fs_indir2_block = NULL;
1877 ext4fs_indir2_size = 0;
1878 ext4fs_indir2_blkno = -1;
1880 if (ext4fs_indir3_block != NULL) {
1881 free(ext4fs_indir3_block);
1882 ext4fs_indir3_block = NULL;
1883 ext4fs_indir3_size = 0;
1884 ext4fs_indir3_blkno = -1;
1887 void ext4fs_close(void)
1889 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1890 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1893 if (ext4fs_root != NULL) {
1898 ext4fs_reinit_global();
1901 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1902 struct ext2fs_node **fnode, int *ftype)
1904 unsigned int fpos = 0;
1907 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1911 printf("Iterate dir %s\n", name);
1912 #endif /* of DEBUG */
1913 if (!diro->inode_read) {
1914 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1918 /* Search the file. */
1919 while (fpos < le32_to_cpu(diro->inode.size)) {
1920 struct ext2_dirent dirent;
1922 status = ext4fs_read_file(diro, fpos,
1923 sizeof(struct ext2_dirent),
1924 (char *)&dirent, &actread);
1928 if (dirent.direntlen == 0) {
1929 printf("Failed to iterate over directory %s\n", name);
1933 if (dirent.namelen != 0) {
1934 char filename[dirent.namelen + 1];
1935 struct ext2fs_node *fdiro;
1936 int type = FILETYPE_UNKNOWN;
1938 status = ext4fs_read_file(diro,
1940 sizeof(struct ext2_dirent),
1941 dirent.namelen, filename,
1946 fdiro = zalloc(sizeof(struct ext2fs_node));
1950 fdiro->data = diro->data;
1951 fdiro->ino = le32_to_cpu(dirent.inode);
1953 filename[dirent.namelen] = '\0';
1955 if (dirent.filetype != FILETYPE_UNKNOWN) {
1956 fdiro->inode_read = 0;
1958 if (dirent.filetype == FILETYPE_DIRECTORY)
1959 type = FILETYPE_DIRECTORY;
1960 else if (dirent.filetype == FILETYPE_SYMLINK)
1961 type = FILETYPE_SYMLINK;
1962 else if (dirent.filetype == FILETYPE_REG)
1963 type = FILETYPE_REG;
1965 status = ext4fs_read_inode(diro->data,
1973 fdiro->inode_read = 1;
1975 if ((le16_to_cpu(fdiro->inode.mode) &
1976 FILETYPE_INO_MASK) ==
1977 FILETYPE_INO_DIRECTORY) {
1978 type = FILETYPE_DIRECTORY;
1979 } else if ((le16_to_cpu(fdiro->inode.mode)
1980 & FILETYPE_INO_MASK) ==
1981 FILETYPE_INO_SYMLINK) {
1982 type = FILETYPE_SYMLINK;
1983 } else if ((le16_to_cpu(fdiro->inode.mode)
1984 & FILETYPE_INO_MASK) ==
1986 type = FILETYPE_REG;
1990 printf("iterate >%s<\n", filename);
1991 #endif /* of DEBUG */
1992 if ((name != NULL) && (fnode != NULL)
1993 && (ftype != NULL)) {
1994 if (strcmp(filename, name) == 0) {
2000 if (fdiro->inode_read == 0) {
2001 status = ext4fs_read_inode(diro->data,
2009 fdiro->inode_read = 1;
2012 case FILETYPE_DIRECTORY:
2015 case FILETYPE_SYMLINK:
2026 le32_to_cpu(fdiro->inode.size),
2031 fpos += le16_to_cpu(dirent.direntlen);
2036 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2039 struct ext2fs_node *diro = node;
2043 if (!diro->inode_read) {
2044 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2048 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2052 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2053 strncpy(symlink, diro->inode.b.symlink,
2054 le32_to_cpu(diro->inode.size));
2056 status = ext4fs_read_file(diro, 0,
2057 le32_to_cpu(diro->inode.size),
2059 if ((status < 0) || (actread == 0)) {
2064 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2068 static int ext4fs_find_file1(const char *currpath,
2069 struct ext2fs_node *currroot,
2070 struct ext2fs_node **currfound, int *foundtype)
2072 char fpath[strlen(currpath) + 1];
2076 int type = FILETYPE_DIRECTORY;
2077 struct ext2fs_node *currnode = currroot;
2078 struct ext2fs_node *oldnode = currroot;
2080 strncpy(fpath, currpath, strlen(currpath) + 1);
2082 /* Remove all leading slashes. */
2083 while (*name == '/')
2087 *currfound = currnode;
2094 /* Extract the actual part from the pathname. */
2095 next = strchr(name, '/');
2097 /* Remove all leading slashes. */
2098 while (*next == '/')
2102 if (type != FILETYPE_DIRECTORY) {
2103 ext4fs_free_node(currnode, currroot);
2109 /* Iterate over the directory. */
2110 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2117 /* Read in the symlink and follow it. */
2118 if (type == FILETYPE_SYMLINK) {
2121 /* Test if the symlink does not loop. */
2122 if (++symlinknest == 8) {
2123 ext4fs_free_node(currnode, currroot);
2124 ext4fs_free_node(oldnode, currroot);
2128 symlink = ext4fs_read_symlink(currnode);
2129 ext4fs_free_node(currnode, currroot);
2132 ext4fs_free_node(oldnode, currroot);
2136 debug("Got symlink >%s<\n", symlink);
2138 if (symlink[0] == '/') {
2139 ext4fs_free_node(oldnode, currroot);
2140 oldnode = &ext4fs_root->diropen;
2143 /* Lookup the node the symlink points to. */
2144 status = ext4fs_find_file1(symlink, oldnode,
2150 ext4fs_free_node(oldnode, currroot);
2155 ext4fs_free_node(oldnode, currroot);
2157 /* Found the node! */
2158 if (!next || *next == '\0') {
2159 *currfound = currnode;
2168 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2169 struct ext2fs_node **foundnode, int expecttype)
2172 int foundtype = FILETYPE_DIRECTORY;
2178 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2182 /* Check if the node that was found was of the expected type. */
2183 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2185 else if ((expecttype == FILETYPE_DIRECTORY)
2186 && (foundtype != expecttype))
2192 int ext4fs_open(const char *filename, loff_t *len)
2194 struct ext2fs_node *fdiro = NULL;
2197 if (ext4fs_root == NULL)
2201 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2206 if (!fdiro->inode_read) {
2207 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2212 *len = le32_to_cpu(fdiro->inode.size);
2213 ext4fs_file = fdiro;
2217 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2222 int ext4fs_mount(unsigned part_length)
2224 struct ext2_data *data;
2226 struct ext_filesystem *fs = get_fs();
2227 data = zalloc(SUPERBLOCK_SIZE);
2231 /* Read the superblock. */
2232 status = ext4_read_superblock((char *)&data->sblock);
2237 /* Make sure this is an ext2 filesystem. */
2238 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2242 * The 64bit feature was enabled when metadata_csum was enabled
2243 * and we do not support metadata_csum (and cannot reliably find
2244 * files when it is set. Refuse to mount.
2246 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
2247 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2251 if (le32_to_cpu(data->sblock.revision_level) == 0)
2254 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2256 debug("EXT2 rev %d, inode_size %d\n",
2257 le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2259 data->diropen.data = data;
2260 data->diropen.ino = 2;
2261 data->diropen.inode_read = 1;
2262 data->inode = &data->diropen.inode;
2264 status = ext4fs_read_inode(data, 2, data->inode);
2272 printf("Failed to mount ext2 filesystem...\n");