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)
531 char *block_buffer = NULL;
532 struct ext2_dirent *dir = NULL;
533 struct ext_filesystem *fs = get_fs();
534 uint32_t directory_blocks;
537 directory_blocks = le32_to_cpu(parent_inode->size) >>
538 LOG2_BLOCK_SIZE(ext4fs_root);
540 block_buffer = zalloc(fs->blksz);
544 /* get the block no allocated to a file */
545 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
546 blknr = read_allocated_block(parent_inode, blk_idx);
550 /* read the directory block */
551 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
552 0, fs->blksz, (char *)block_buffer);
558 dir = (struct ext2_dirent *)(block_buffer + offset);
559 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
561 int direntlen = le16_to_cpu(dir->direntlen);
562 if (direntlen < sizeof(struct ext2_dirent))
565 if (dir->inode && (strlen(dirname) == dir->namelen) &&
566 (strncmp(dirname, direntname, dir->namelen) == 0)) {
567 inodeno = le32_to_cpu(dir->inode);
573 } while (offset < fs->blksz);
587 static int find_dir_depth(char *dirname)
589 char *token = strtok(dirname, "/");
591 while (token != NULL) {
592 token = strtok(NULL, "/");
595 return count + 1 + 1;
597 * for example for string /home/temp
598 * depth=home(1)+temp(1)+1 extra for NULL;
603 static int parse_path(char **arr, char *dirname)
605 char *token = strtok(dirname, "/");
609 arr[i] = zalloc(strlen("/") + 1);
612 memcpy(arr[i++], "/", strlen("/"));
614 /* add each path entry after root */
615 while (token != NULL) {
616 arr[i] = zalloc(strlen(token) + 1);
619 memcpy(arr[i++], token, strlen(token));
620 token = strtok(NULL, "/");
627 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
629 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
636 * Function: ext4fs_get_parent_inode_num
637 * Return Value: inode Number of the parent directory of file/Directory to be
639 * dirname : Input parmater, input path name of the file/directory to be created
640 * dname : Output parameter, to be filled with the name of the directory
641 * extracted from dirname
643 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
647 int matched_inode_no;
648 int result_inode_no = -1;
650 char *depth_dirname = NULL;
651 char *parse_dirname = NULL;
652 struct ext2_inode *parent_inode = NULL;
653 struct ext2_inode *first_inode = NULL;
654 struct ext2_inode temp_inode;
656 if (*dirname != '/') {
657 printf("Please supply Absolute path\n");
661 /* TODO: input validation make equivalent to linux */
662 depth_dirname = zalloc(strlen(dirname) + 1);
666 memcpy(depth_dirname, dirname, strlen(dirname));
667 depth = find_dir_depth(depth_dirname);
668 parse_dirname = zalloc(strlen(dirname) + 1);
671 memcpy(parse_dirname, dirname, strlen(dirname));
673 /* allocate memory for each directory level */
674 ptr = zalloc((depth) * sizeof(char *));
677 if (parse_path(ptr, parse_dirname))
679 parent_inode = zalloc(sizeof(struct ext2_inode));
682 first_inode = zalloc(sizeof(struct ext2_inode));
685 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
686 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
688 result_inode_no = EXT2_ROOT_INO;
689 for (i = 1; i < depth; i++) {
690 matched_inode_no = search_dir(parent_inode, ptr[i]);
691 if (matched_inode_no == -1) {
692 if (ptr[i + 1] == NULL && i == 1) {
693 result_inode_no = EXT2_ROOT_INO;
696 if (ptr[i + 1] == NULL)
698 printf("Invalid path\n");
699 result_inode_no = -1;
703 if (ptr[i + 1] != NULL) {
704 memset(parent_inode, '\0',
705 sizeof(struct ext2_inode));
706 if (ext4fs_iget(matched_inode_no,
708 result_inode_no = -1;
711 result_inode_no = matched_inode_no;
720 matched_inode_no = search_dir(first_inode, ptr[i]);
722 matched_inode_no = search_dir(parent_inode, ptr[i]);
724 if (matched_inode_no != -1) {
725 ext4fs_iget(matched_inode_no, &temp_inode);
726 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
727 printf("It is a Directory\n");
728 result_inode_no = -1;
733 if (strlen(ptr[i]) > 256) {
734 result_inode_no = -1;
737 memcpy(dname, ptr[i], strlen(ptr[i]));
742 for (i = 0; i < depth; i++) {
751 return result_inode_no;
754 static int unlink_filename(char *filename, unsigned int blknr)
760 char *root_first_block_buffer = NULL;
761 struct ext2_dirent *dir = NULL;
762 struct ext2_dirent *previous_dir = NULL;
764 struct ext_filesystem *fs = get_fs();
767 /* get the first block of root */
768 root_first_block_buffer = zalloc(fs->blksz);
769 if (!root_first_block_buffer)
771 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
772 fs->blksz, root_first_block_buffer);
776 if (ext4fs_log_journal(root_first_block_buffer, blknr))
778 dir = (struct ext2_dirent *)root_first_block_buffer;
781 while (le16_to_cpu(dir->direntlen) >= 0) {
783 * blocksize-totalbytes because last
784 * directory length i.e., *dir->direntlen
785 * is free availble space in the block that
786 * means it is a last entry of directory entry
788 if (dir->inode && (strlen(filename) == dir->namelen) &&
789 (strncmp(ptr + sizeof(struct ext2_dirent),
790 filename, dir->namelen) == 0)) {
791 printf("file found, deleting\n");
792 inodeno = le32_to_cpu(dir->inode);
795 new_len = le16_to_cpu(previous_dir->direntlen);
796 new_len += le16_to_cpu(dir->direntlen);
797 previous_dir->direntlen = cpu_to_le16(new_len);
805 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
808 /* traversing the each directory entry */
809 templength = le16_to_cpu(dir->direntlen);
810 totalbytes = totalbytes + templength;
812 dir = (struct ext2_dirent *)((char *)dir + templength);
818 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
823 free(root_first_block_buffer);
828 int ext4fs_filename_unlink(char *filename)
833 uint32_t directory_blocks;
835 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
836 LOG2_BLOCK_SIZE(ext4fs_root);
838 /* read the block no allocated to a file */
839 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
840 blknr = read_allocated_block(g_parent_inode, blk_idx);
843 inodeno = unlink_filename(filename, blknr);
851 uint32_t ext4fs_get_new_blk_no(void)
857 static int prev_bg_bitmap_index = -1;
858 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
859 struct ext_filesystem *fs = get_fs();
860 char *journal_buffer = zalloc(fs->blksz);
861 char *zero_buffer = zalloc(fs->blksz);
862 if (!journal_buffer || !zero_buffer)
864 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
866 if (fs->first_pass_bbmap == 0) {
867 for (i = 0; i < fs->no_blkgrp; i++) {
868 if (le16_to_cpu(bgd[i].free_blocks)) {
869 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
871 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
872 zero_buffer, fs->blksz);
873 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
874 bgd[i].bg_flags = cpu_to_le16(new_flags);
875 memcpy(fs->blk_bmaps[i], zero_buffer,
879 _get_new_blk_no(fs->blk_bmaps[i]);
880 if (fs->curr_blkno == -1)
881 /* if block bitmap is completely fill */
883 fs->curr_blkno = fs->curr_blkno +
885 fs->first_pass_bbmap++;
886 ext4fs_bg_free_blocks_dec(&bgd[i]);
887 ext4fs_sb_free_blocks_dec(fs->sb);
888 status = ext4fs_devread(
889 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
895 if (ext4fs_log_journal(journal_buffer,
896 le32_to_cpu(bgd[i].block_id)))
900 debug("no space left on block group %d\n", i);
908 /* get the blockbitmap index respective to blockno */
909 bg_idx = fs->curr_blkno / blk_per_grp;
910 if (fs->blksz == 1024) {
911 remainder = fs->curr_blkno % blk_per_grp;
917 * To skip completely filled block group bitmaps
918 * Optimize the block allocation
920 if (bg_idx >= fs->no_blkgrp)
923 if (bgd[bg_idx].free_blocks == 0) {
924 debug("block group %u is full. Skipping\n", bg_idx);
925 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
930 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
932 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
933 zero_buffer, fs->blksz);
934 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
935 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
936 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
939 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
941 debug("going for restart for the block no %ld %u\n",
942 fs->curr_blkno, bg_idx);
947 if (prev_bg_bitmap_index != bg_idx) {
948 status = ext4fs_devread(
949 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
951 0, fs->blksz, journal_buffer);
954 if (ext4fs_log_journal(journal_buffer,
955 le32_to_cpu(bgd[bg_idx].block_id)))
958 prev_bg_bitmap_index = bg_idx;
960 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
961 ext4fs_sb_free_blocks_dec(fs->sb);
965 free(journal_buffer);
968 return fs->curr_blkno;
970 free(journal_buffer);
976 int ext4fs_get_new_inode_no(void)
980 unsigned int ibmap_idx;
981 static int prev_inode_bitmap_index = -1;
982 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
983 struct ext_filesystem *fs = get_fs();
984 char *journal_buffer = zalloc(fs->blksz);
985 char *zero_buffer = zalloc(fs->blksz);
986 if (!journal_buffer || !zero_buffer)
988 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
989 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
990 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
992 if (fs->first_pass_ibmap == 0) {
993 for (i = 0; i < fs->no_blkgrp; i++) {
994 if (bgd[i].free_inodes) {
996 bgd[i].bg_itable_unused =
998 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1000 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
1001 zero_buffer, fs->blksz);
1002 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1003 bgd[i].bg_flags = cpu_to_le16(new_flags);
1004 memcpy(fs->inode_bmaps[i],
1005 zero_buffer, fs->blksz);
1008 _get_new_inode_no(fs->inode_bmaps[i]);
1009 if (fs->curr_inode_no == -1)
1010 /* if block bitmap is completely fill */
1012 fs->curr_inode_no = fs->curr_inode_no +
1013 (i * inodes_per_grp);
1014 fs->first_pass_ibmap++;
1015 ext4fs_bg_free_inodes_dec(&bgd[i]);
1017 ext4fs_bg_itable_unused_dec(&bgd[i]);
1018 ext4fs_sb_free_inodes_dec(fs->sb);
1019 status = ext4fs_devread(
1020 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
1026 if (ext4fs_log_journal(journal_buffer,
1027 le32_to_cpu(bgd[i].inode_id)))
1031 debug("no inode left on block group %d\n", i);
1036 fs->curr_inode_no++;
1037 /* get the blockbitmap index respective to blockno */
1038 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1039 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1041 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1042 zero_buffer, fs->blksz);
1043 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1044 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
1045 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1049 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1050 fs->inode_bmaps[ibmap_idx],
1052 debug("going for restart for the block no %d %u\n",
1053 fs->curr_inode_no, ibmap_idx);
1057 /* journal backup */
1058 if (prev_inode_bitmap_index != ibmap_idx) {
1059 memset(journal_buffer, '\0', fs->blksz);
1060 status = ext4fs_devread(
1061 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
1063 0, fs->blksz, journal_buffer);
1066 if (ext4fs_log_journal(journal_buffer,
1067 le32_to_cpu(bgd[ibmap_idx].inode_id)))
1069 prev_inode_bitmap_index = ibmap_idx;
1071 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1073 bgd[ibmap_idx].bg_itable_unused =
1074 bgd[ibmap_idx].free_inodes;
1075 ext4fs_sb_free_inodes_dec(fs->sb);
1080 free(journal_buffer);
1083 return fs->curr_inode_no;
1085 free(journal_buffer);
1093 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1094 unsigned int *total_remaining_blocks,
1095 unsigned int *no_blks_reqd)
1099 long int actual_block_no;
1100 long int si_blockno;
1101 /* si :single indirect */
1102 __le32 *si_buffer = NULL;
1103 __le32 *si_start_addr = NULL;
1104 struct ext_filesystem *fs = get_fs();
1106 if (*total_remaining_blocks != 0) {
1107 si_buffer = zalloc(fs->blksz);
1109 printf("No Memory\n");
1112 si_start_addr = si_buffer;
1113 si_blockno = ext4fs_get_new_blk_no();
1114 if (si_blockno == -1) {
1115 printf("no block left to assign\n");
1119 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1121 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1122 0, fs->blksz, (char *)si_buffer);
1123 memset(si_buffer, '\0', fs->blksz);
1127 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1128 actual_block_no = ext4fs_get_new_blk_no();
1129 if (actual_block_no == -1) {
1130 printf("no block left to assign\n");
1133 *si_buffer = cpu_to_le32(actual_block_no);
1134 debug("SIAB %u: %u\n", *si_buffer,
1135 *total_remaining_blocks);
1138 (*total_remaining_blocks)--;
1139 if (*total_remaining_blocks == 0)
1143 /* write the block to disk */
1144 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1145 si_start_addr, fs->blksz);
1146 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1149 free(si_start_addr);
1152 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1153 unsigned int *total_remaining_blocks,
1154 unsigned int *no_blks_reqd)
1159 long int actual_block_no;
1160 /* di:double indirect */
1161 long int di_blockno_parent;
1162 long int di_blockno_child;
1163 __le32 *di_parent_buffer = NULL;
1164 __le32 *di_child_buff = NULL;
1165 __le32 *di_block_start_addr = NULL;
1166 __le32 *di_child_buff_start = NULL;
1167 struct ext_filesystem *fs = get_fs();
1169 if (*total_remaining_blocks != 0) {
1170 /* double indirect parent block connecting to inode */
1171 di_blockno_parent = ext4fs_get_new_blk_no();
1172 if (di_blockno_parent == -1) {
1173 printf("no block left to assign\n");
1176 di_parent_buffer = zalloc(fs->blksz);
1177 if (!di_parent_buffer)
1180 di_block_start_addr = di_parent_buffer;
1182 debug("DIPB %ld: %u\n", di_blockno_parent,
1183 *total_remaining_blocks);
1185 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1187 fs->blksz, (char *)di_parent_buffer);
1190 printf("%s: Device read error!\n", __func__);
1193 memset(di_parent_buffer, '\0', fs->blksz);
1196 * start:for each double indirect parent
1197 * block create one more block
1199 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1200 di_blockno_child = ext4fs_get_new_blk_no();
1201 if (di_blockno_child == -1) {
1202 printf("no block left to assign\n");
1205 di_child_buff = zalloc(fs->blksz);
1209 di_child_buff_start = di_child_buff;
1210 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1213 debug("DICB %ld: %u\n", di_blockno_child,
1214 *total_remaining_blocks);
1216 status = ext4fs_devread((lbaint_t)di_blockno_child *
1219 (char *)di_child_buff);
1222 printf("%s: Device read error!\n", __func__);
1225 memset(di_child_buff, '\0', fs->blksz);
1226 /* filling of actual datablocks for each child */
1227 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1228 actual_block_no = ext4fs_get_new_blk_no();
1229 if (actual_block_no == -1) {
1230 printf("no block left to assign\n");
1233 *di_child_buff = cpu_to_le32(actual_block_no);
1234 debug("DIAB %ld: %u\n", actual_block_no,
1235 *total_remaining_blocks);
1238 (*total_remaining_blocks)--;
1239 if (*total_remaining_blocks == 0)
1242 /* write the block table */
1243 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1244 di_child_buff_start, fs->blksz);
1245 free(di_child_buff_start);
1246 di_child_buff_start = NULL;
1248 if (*total_remaining_blocks == 0)
1251 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1252 di_block_start_addr, fs->blksz);
1253 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1256 free(di_block_start_addr);
1259 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1260 unsigned int *total_remaining_blocks,
1261 unsigned int *no_blks_reqd)
1266 long int actual_block_no;
1267 /* ti: Triple Indirect */
1268 long int ti_gp_blockno;
1269 long int ti_parent_blockno;
1270 long int ti_child_blockno;
1271 __le32 *ti_gp_buff = NULL;
1272 __le32 *ti_parent_buff = NULL;
1273 __le32 *ti_child_buff = NULL;
1274 __le32 *ti_gp_buff_start_addr = NULL;
1275 __le32 *ti_pbuff_start_addr = NULL;
1276 __le32 *ti_cbuff_start_addr = NULL;
1277 struct ext_filesystem *fs = get_fs();
1278 if (*total_remaining_blocks != 0) {
1279 /* triple indirect grand parent block connecting to inode */
1280 ti_gp_blockno = ext4fs_get_new_blk_no();
1281 if (ti_gp_blockno == -1) {
1282 printf("no block left to assign\n");
1285 ti_gp_buff = zalloc(fs->blksz);
1289 ti_gp_buff_start_addr = ti_gp_buff;
1291 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1292 *total_remaining_blocks);
1294 /* for each 4 byte grand parent entry create one more block */
1295 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1296 ti_parent_blockno = ext4fs_get_new_blk_no();
1297 if (ti_parent_blockno == -1) {
1298 printf("no block left to assign\n");
1301 ti_parent_buff = zalloc(fs->blksz);
1302 if (!ti_parent_buff)
1305 ti_pbuff_start_addr = ti_parent_buff;
1306 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1309 debug("TIPB %ld: %u\n", ti_parent_blockno,
1310 *total_remaining_blocks);
1312 /* for each 4 byte entry parent create one more block */
1313 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1314 ti_child_blockno = ext4fs_get_new_blk_no();
1315 if (ti_child_blockno == -1) {
1316 printf("no block left assign\n");
1319 ti_child_buff = zalloc(fs->blksz);
1323 ti_cbuff_start_addr = ti_child_buff;
1324 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1327 debug("TICB %ld: %u\n", ti_parent_blockno,
1328 *total_remaining_blocks);
1330 /* fill actual datablocks for each child */
1331 for (k = 0; k < (fs->blksz / sizeof(int));
1334 ext4fs_get_new_blk_no();
1335 if (actual_block_no == -1) {
1336 printf("no block left\n");
1337 free(ti_cbuff_start_addr);
1340 *ti_child_buff = cpu_to_le32(actual_block_no);
1341 debug("TIAB %ld: %u\n", actual_block_no,
1342 *total_remaining_blocks);
1345 (*total_remaining_blocks)--;
1346 if (*total_remaining_blocks == 0)
1349 /* write the child block */
1350 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1351 (uint64_t)fs->blksz)),
1352 ti_cbuff_start_addr, fs->blksz);
1353 free(ti_cbuff_start_addr);
1355 if (*total_remaining_blocks == 0)
1358 /* write the parent block */
1359 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1360 ti_pbuff_start_addr, fs->blksz);
1361 free(ti_pbuff_start_addr);
1363 if (*total_remaining_blocks == 0)
1366 /* write the grand parent block */
1367 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1368 ti_gp_buff_start_addr, fs->blksz);
1369 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1370 free(ti_gp_buff_start_addr);
1374 free(ti_pbuff_start_addr);
1376 free(ti_gp_buff_start_addr);
1379 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1380 unsigned int total_remaining_blocks,
1381 unsigned int *total_no_of_block)
1384 long int direct_blockno;
1385 unsigned int no_blks_reqd = 0;
1387 /* allocation of direct blocks */
1388 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1389 direct_blockno = ext4fs_get_new_blk_no();
1390 if (direct_blockno == -1) {
1391 printf("no block left to assign\n");
1394 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1395 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1397 total_remaining_blocks--;
1400 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1402 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1404 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1406 *total_no_of_block += no_blks_reqd;
1411 static struct ext4_extent_header *ext4fs_get_extent_block
1412 (struct ext2_data *data, char *buf,
1413 struct ext4_extent_header *ext_block,
1414 uint32_t fileblock, int log2_blksz)
1416 struct ext4_extent_idx *index;
1417 unsigned long long block;
1418 int blksz = EXT2_BLOCK_SIZE(data);
1422 index = (struct ext4_extent_idx *)(ext_block + 1);
1424 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1427 if (ext_block->eh_depth == 0)
1432 if (i >= le16_to_cpu(ext_block->eh_entries))
1434 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1439 block = le16_to_cpu(index[i].ei_leaf_hi);
1440 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1442 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1444 ext_block = (struct ext4_extent_header *)buf;
1450 static int ext4fs_blockgroup
1451 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1454 unsigned int blkoff, desc_per_blk;
1455 int log2blksz = get_fs()->dev_desc->log2blksz;
1457 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1459 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1460 group / desc_per_blk;
1461 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1463 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1464 group, blkno, blkoff);
1466 return ext4fs_devread((lbaint_t)blkno <<
1467 (LOG2_BLOCK_SIZE(data) - log2blksz),
1468 blkoff, sizeof(struct ext2_block_group),
1472 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1474 struct ext2_block_group blkgrp;
1475 struct ext2_sblock *sblock = &data->sblock;
1476 struct ext_filesystem *fs = get_fs();
1477 int log2blksz = get_fs()->dev_desc->log2blksz;
1478 int inodes_per_block, status;
1480 unsigned int blkoff;
1482 /* It is easier to calculate if the first inode is 0. */
1484 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1485 (sblock->inodes_per_group), &blkgrp);
1489 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1490 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1491 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1492 blkoff = (ino % inodes_per_block) * fs->inodesz;
1493 /* Read the inode. */
1494 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1496 sizeof(struct ext2_inode), (char *)inode);
1503 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1510 long int perblock_parent;
1511 long int perblock_child;
1512 unsigned long long start;
1513 /* get the blocksize of the filesystem */
1514 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1515 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1516 - get_fs()->dev_desc->log2blksz;
1518 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1519 char *buf = zalloc(blksz);
1522 struct ext4_extent_header *ext_block;
1523 struct ext4_extent *extent;
1526 ext4fs_get_extent_block(ext4fs_root, buf,
1527 (struct ext4_extent_header *)
1528 inode->b.blocks.dir_blocks,
1529 fileblock, log2_blksz);
1531 printf("invalid extent block\n");
1536 extent = (struct ext4_extent *)(ext_block + 1);
1540 if (i >= le16_to_cpu(ext_block->eh_entries))
1542 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1544 fileblock -= le32_to_cpu(extent[i].ee_block);
1545 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1550 start = le16_to_cpu(extent[i].ee_start_hi);
1551 start = (start << 32) +
1552 le32_to_cpu(extent[i].ee_start_lo);
1554 return fileblock + start;
1557 printf("Extent Error\n");
1562 /* Direct blocks. */
1563 if (fileblock < INDIRECT_BLOCKS)
1564 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1567 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1568 if (ext4fs_indir1_block == NULL) {
1569 ext4fs_indir1_block = zalloc(blksz);
1570 if (ext4fs_indir1_block == NULL) {
1571 printf("** SI ext2fs read block (indir 1)"
1572 "malloc failed. **\n");
1575 ext4fs_indir1_size = blksz;
1576 ext4fs_indir1_blkno = -1;
1578 if (blksz != ext4fs_indir1_size) {
1579 free(ext4fs_indir1_block);
1580 ext4fs_indir1_block = NULL;
1581 ext4fs_indir1_size = 0;
1582 ext4fs_indir1_blkno = -1;
1583 ext4fs_indir1_block = zalloc(blksz);
1584 if (ext4fs_indir1_block == NULL) {
1585 printf("** SI ext2fs read block (indir 1):"
1586 "malloc failed. **\n");
1589 ext4fs_indir1_size = blksz;
1591 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1592 log2_blksz) != ext4fs_indir1_blkno) {
1594 ext4fs_devread((lbaint_t)le32_to_cpu
1596 indir_block) << log2_blksz, 0,
1597 blksz, (char *)ext4fs_indir1_block);
1599 printf("** SI ext2fs read block (indir 1)"
1603 ext4fs_indir1_blkno =
1604 le32_to_cpu(inode->b.blocks.
1605 indir_block) << log2_blksz;
1607 blknr = le32_to_cpu(ext4fs_indir1_block
1608 [fileblock - INDIRECT_BLOCKS]);
1610 /* Double indirect. */
1611 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1612 (blksz / 4 + 1)))) {
1614 long int perblock = blksz / 4;
1615 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1617 if (ext4fs_indir1_block == NULL) {
1618 ext4fs_indir1_block = zalloc(blksz);
1619 if (ext4fs_indir1_block == NULL) {
1620 printf("** DI ext2fs read block (indir 2 1)"
1621 "malloc failed. **\n");
1624 ext4fs_indir1_size = blksz;
1625 ext4fs_indir1_blkno = -1;
1627 if (blksz != ext4fs_indir1_size) {
1628 free(ext4fs_indir1_block);
1629 ext4fs_indir1_block = NULL;
1630 ext4fs_indir1_size = 0;
1631 ext4fs_indir1_blkno = -1;
1632 ext4fs_indir1_block = zalloc(blksz);
1633 if (ext4fs_indir1_block == NULL) {
1634 printf("** DI ext2fs read block (indir 2 1)"
1635 "malloc failed. **\n");
1638 ext4fs_indir1_size = blksz;
1640 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1641 log2_blksz) != ext4fs_indir1_blkno) {
1643 ext4fs_devread((lbaint_t)le32_to_cpu
1645 double_indir_block) << log2_blksz,
1647 (char *)ext4fs_indir1_block);
1649 printf("** DI ext2fs read block (indir 2 1)"
1653 ext4fs_indir1_blkno =
1654 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1658 if (ext4fs_indir2_block == NULL) {
1659 ext4fs_indir2_block = zalloc(blksz);
1660 if (ext4fs_indir2_block == NULL) {
1661 printf("** DI ext2fs read block (indir 2 2)"
1662 "malloc failed. **\n");
1665 ext4fs_indir2_size = blksz;
1666 ext4fs_indir2_blkno = -1;
1668 if (blksz != ext4fs_indir2_size) {
1669 free(ext4fs_indir2_block);
1670 ext4fs_indir2_block = NULL;
1671 ext4fs_indir2_size = 0;
1672 ext4fs_indir2_blkno = -1;
1673 ext4fs_indir2_block = zalloc(blksz);
1674 if (ext4fs_indir2_block == NULL) {
1675 printf("** DI ext2fs read block (indir 2 2)"
1676 "malloc failed. **\n");
1679 ext4fs_indir2_size = blksz;
1681 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1682 log2_blksz) != ext4fs_indir2_blkno) {
1683 status = ext4fs_devread((lbaint_t)le32_to_cpu
1684 (ext4fs_indir1_block
1686 perblock]) << log2_blksz, 0,
1688 (char *)ext4fs_indir2_block);
1690 printf("** DI ext2fs read block (indir 2 2)"
1694 ext4fs_indir2_blkno =
1695 le32_to_cpu(ext4fs_indir1_block[rblock
1700 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1702 /* Tripple indirect. */
1704 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1705 (blksz / 4 * blksz / 4));
1706 perblock_child = blksz / 4;
1707 perblock_parent = ((blksz / 4) * (blksz / 4));
1709 if (ext4fs_indir1_block == NULL) {
1710 ext4fs_indir1_block = zalloc(blksz);
1711 if (ext4fs_indir1_block == NULL) {
1712 printf("** TI ext2fs read block (indir 2 1)"
1713 "malloc failed. **\n");
1716 ext4fs_indir1_size = blksz;
1717 ext4fs_indir1_blkno = -1;
1719 if (blksz != ext4fs_indir1_size) {
1720 free(ext4fs_indir1_block);
1721 ext4fs_indir1_block = NULL;
1722 ext4fs_indir1_size = 0;
1723 ext4fs_indir1_blkno = -1;
1724 ext4fs_indir1_block = zalloc(blksz);
1725 if (ext4fs_indir1_block == NULL) {
1726 printf("** TI ext2fs read block (indir 2 1)"
1727 "malloc failed. **\n");
1730 ext4fs_indir1_size = blksz;
1732 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1733 log2_blksz) != ext4fs_indir1_blkno) {
1734 status = ext4fs_devread
1736 le32_to_cpu(inode->b.blocks.triple_indir_block)
1737 << log2_blksz, 0, blksz,
1738 (char *)ext4fs_indir1_block);
1740 printf("** TI ext2fs read block (indir 2 1)"
1744 ext4fs_indir1_blkno =
1745 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1749 if (ext4fs_indir2_block == NULL) {
1750 ext4fs_indir2_block = zalloc(blksz);
1751 if (ext4fs_indir2_block == NULL) {
1752 printf("** TI ext2fs read block (indir 2 2)"
1753 "malloc failed. **\n");
1756 ext4fs_indir2_size = blksz;
1757 ext4fs_indir2_blkno = -1;
1759 if (blksz != ext4fs_indir2_size) {
1760 free(ext4fs_indir2_block);
1761 ext4fs_indir2_block = NULL;
1762 ext4fs_indir2_size = 0;
1763 ext4fs_indir2_blkno = -1;
1764 ext4fs_indir2_block = zalloc(blksz);
1765 if (ext4fs_indir2_block == NULL) {
1766 printf("** TI ext2fs read block (indir 2 2)"
1767 "malloc failed. **\n");
1770 ext4fs_indir2_size = blksz;
1772 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1773 perblock_parent]) <<
1775 != ext4fs_indir2_blkno) {
1776 status = ext4fs_devread((lbaint_t)le32_to_cpu
1777 (ext4fs_indir1_block
1779 perblock_parent]) <<
1780 log2_blksz, 0, blksz,
1781 (char *)ext4fs_indir2_block);
1783 printf("** TI ext2fs read block (indir 2 2)"
1787 ext4fs_indir2_blkno =
1788 le32_to_cpu(ext4fs_indir1_block[rblock /
1793 if (ext4fs_indir3_block == NULL) {
1794 ext4fs_indir3_block = zalloc(blksz);
1795 if (ext4fs_indir3_block == NULL) {
1796 printf("** TI ext2fs read block (indir 2 2)"
1797 "malloc failed. **\n");
1800 ext4fs_indir3_size = blksz;
1801 ext4fs_indir3_blkno = -1;
1803 if (blksz != ext4fs_indir3_size) {
1804 free(ext4fs_indir3_block);
1805 ext4fs_indir3_block = NULL;
1806 ext4fs_indir3_size = 0;
1807 ext4fs_indir3_blkno = -1;
1808 ext4fs_indir3_block = zalloc(blksz);
1809 if (ext4fs_indir3_block == NULL) {
1810 printf("** TI ext2fs read block (indir 2 2)"
1811 "malloc failed. **\n");
1814 ext4fs_indir3_size = blksz;
1816 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1819 log2_blksz) != ext4fs_indir3_blkno) {
1821 ext4fs_devread((lbaint_t)le32_to_cpu
1822 (ext4fs_indir2_block
1823 [(rblock / perblock_child)
1824 % (blksz / 4)]) << log2_blksz, 0,
1825 blksz, (char *)ext4fs_indir3_block);
1827 printf("** TI ext2fs read block (indir 2 2)"
1831 ext4fs_indir3_blkno =
1832 le32_to_cpu(ext4fs_indir2_block[(rblock /
1839 blknr = le32_to_cpu(ext4fs_indir3_block
1840 [rblock % perblock_child]);
1842 debug("read_allocated_block %ld\n", blknr);
1848 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1851 * This function assures that for a file with the same name but different size
1852 * the sequential store on the ext4 filesystem will be correct.
1854 * In this function the global data, responsible for internal representation
1855 * of the ext4 data are initialized to the reset state. Without this, during
1856 * replacement of the smaller file with the bigger truncation of new file was
1859 void ext4fs_reinit_global(void)
1861 if (ext4fs_indir1_block != NULL) {
1862 free(ext4fs_indir1_block);
1863 ext4fs_indir1_block = NULL;
1864 ext4fs_indir1_size = 0;
1865 ext4fs_indir1_blkno = -1;
1867 if (ext4fs_indir2_block != NULL) {
1868 free(ext4fs_indir2_block);
1869 ext4fs_indir2_block = NULL;
1870 ext4fs_indir2_size = 0;
1871 ext4fs_indir2_blkno = -1;
1873 if (ext4fs_indir3_block != NULL) {
1874 free(ext4fs_indir3_block);
1875 ext4fs_indir3_block = NULL;
1876 ext4fs_indir3_size = 0;
1877 ext4fs_indir3_blkno = -1;
1880 void ext4fs_close(void)
1882 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1883 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1886 if (ext4fs_root != NULL) {
1891 ext4fs_reinit_global();
1894 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1895 struct ext2fs_node **fnode, int *ftype)
1897 unsigned int fpos = 0;
1900 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1904 printf("Iterate dir %s\n", name);
1905 #endif /* of DEBUG */
1906 if (!diro->inode_read) {
1907 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1911 /* Search the file. */
1912 while (fpos < le32_to_cpu(diro->inode.size)) {
1913 struct ext2_dirent dirent;
1915 status = ext4fs_read_file(diro, fpos,
1916 sizeof(struct ext2_dirent),
1917 (char *)&dirent, &actread);
1921 if (dirent.direntlen == 0) {
1922 printf("Failed to iterate over directory %s\n", name);
1926 if (dirent.namelen != 0) {
1927 char filename[dirent.namelen + 1];
1928 struct ext2fs_node *fdiro;
1929 int type = FILETYPE_UNKNOWN;
1931 status = ext4fs_read_file(diro,
1933 sizeof(struct ext2_dirent),
1934 dirent.namelen, filename,
1939 fdiro = zalloc(sizeof(struct ext2fs_node));
1943 fdiro->data = diro->data;
1944 fdiro->ino = le32_to_cpu(dirent.inode);
1946 filename[dirent.namelen] = '\0';
1948 if (dirent.filetype != FILETYPE_UNKNOWN) {
1949 fdiro->inode_read = 0;
1951 if (dirent.filetype == FILETYPE_DIRECTORY)
1952 type = FILETYPE_DIRECTORY;
1953 else if (dirent.filetype == FILETYPE_SYMLINK)
1954 type = FILETYPE_SYMLINK;
1955 else if (dirent.filetype == FILETYPE_REG)
1956 type = FILETYPE_REG;
1958 status = ext4fs_read_inode(diro->data,
1966 fdiro->inode_read = 1;
1968 if ((le16_to_cpu(fdiro->inode.mode) &
1969 FILETYPE_INO_MASK) ==
1970 FILETYPE_INO_DIRECTORY) {
1971 type = FILETYPE_DIRECTORY;
1972 } else if ((le16_to_cpu(fdiro->inode.mode)
1973 & FILETYPE_INO_MASK) ==
1974 FILETYPE_INO_SYMLINK) {
1975 type = FILETYPE_SYMLINK;
1976 } else if ((le16_to_cpu(fdiro->inode.mode)
1977 & FILETYPE_INO_MASK) ==
1979 type = FILETYPE_REG;
1983 printf("iterate >%s<\n", filename);
1984 #endif /* of DEBUG */
1985 if ((name != NULL) && (fnode != NULL)
1986 && (ftype != NULL)) {
1987 if (strcmp(filename, name) == 0) {
1993 if (fdiro->inode_read == 0) {
1994 status = ext4fs_read_inode(diro->data,
2002 fdiro->inode_read = 1;
2005 case FILETYPE_DIRECTORY:
2008 case FILETYPE_SYMLINK:
2019 le32_to_cpu(fdiro->inode.size),
2024 fpos += le16_to_cpu(dirent.direntlen);
2029 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2032 struct ext2fs_node *diro = node;
2036 if (!diro->inode_read) {
2037 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2041 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2045 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2046 strncpy(symlink, diro->inode.b.symlink,
2047 le32_to_cpu(diro->inode.size));
2049 status = ext4fs_read_file(diro, 0,
2050 le32_to_cpu(diro->inode.size),
2052 if ((status < 0) || (actread == 0)) {
2057 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2061 static int ext4fs_find_file1(const char *currpath,
2062 struct ext2fs_node *currroot,
2063 struct ext2fs_node **currfound, int *foundtype)
2065 char fpath[strlen(currpath) + 1];
2069 int type = FILETYPE_DIRECTORY;
2070 struct ext2fs_node *currnode = currroot;
2071 struct ext2fs_node *oldnode = currroot;
2073 strncpy(fpath, currpath, strlen(currpath) + 1);
2075 /* Remove all leading slashes. */
2076 while (*name == '/')
2080 *currfound = currnode;
2087 /* Extract the actual part from the pathname. */
2088 next = strchr(name, '/');
2090 /* Remove all leading slashes. */
2091 while (*next == '/')
2095 if (type != FILETYPE_DIRECTORY) {
2096 ext4fs_free_node(currnode, currroot);
2102 /* Iterate over the directory. */
2103 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2110 /* Read in the symlink and follow it. */
2111 if (type == FILETYPE_SYMLINK) {
2114 /* Test if the symlink does not loop. */
2115 if (++symlinknest == 8) {
2116 ext4fs_free_node(currnode, currroot);
2117 ext4fs_free_node(oldnode, currroot);
2121 symlink = ext4fs_read_symlink(currnode);
2122 ext4fs_free_node(currnode, currroot);
2125 ext4fs_free_node(oldnode, currroot);
2129 debug("Got symlink >%s<\n", symlink);
2131 if (symlink[0] == '/') {
2132 ext4fs_free_node(oldnode, currroot);
2133 oldnode = &ext4fs_root->diropen;
2136 /* Lookup the node the symlink points to. */
2137 status = ext4fs_find_file1(symlink, oldnode,
2143 ext4fs_free_node(oldnode, currroot);
2148 ext4fs_free_node(oldnode, currroot);
2150 /* Found the node! */
2151 if (!next || *next == '\0') {
2152 *currfound = currnode;
2161 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2162 struct ext2fs_node **foundnode, int expecttype)
2165 int foundtype = FILETYPE_DIRECTORY;
2171 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2175 /* Check if the node that was found was of the expected type. */
2176 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2178 else if ((expecttype == FILETYPE_DIRECTORY)
2179 && (foundtype != expecttype))
2185 int ext4fs_open(const char *filename, loff_t *len)
2187 struct ext2fs_node *fdiro = NULL;
2190 if (ext4fs_root == NULL)
2194 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2199 if (!fdiro->inode_read) {
2200 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2205 *len = le32_to_cpu(fdiro->inode.size);
2206 ext4fs_file = fdiro;
2210 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2215 int ext4fs_mount(unsigned part_length)
2217 struct ext2_data *data;
2219 struct ext_filesystem *fs = get_fs();
2220 data = zalloc(SUPERBLOCK_SIZE);
2224 /* Read the superblock. */
2225 status = ext4_read_superblock((char *)&data->sblock);
2230 /* Make sure this is an ext2 filesystem. */
2231 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2235 * The 64bit feature was enabled when metadata_csum was enabled
2236 * and we do not support metadata_csum (and cannot reliably find
2237 * files when it is set. Refuse to mount.
2239 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
2240 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2244 if (le32_to_cpu(data->sblock.revision_level) == 0)
2247 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2249 debug("EXT2 rev %d, inode_size %d\n",
2250 le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2252 data->diropen.data = data;
2253 data->diropen.ino = 2;
2254 data->diropen.inode_read = 1;
2255 data->inode = &data->diropen.inode;
2257 status = ext4fs_read_inode(data, 2, data->inode);
2265 printf("Failed to mount ext2 filesystem...\n");