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;
375 long int first_block_no_of_root = 0;
376 long int previous_blknr = -1;
378 short int padding_factor = 0;
379 unsigned int new_entry_byte_reqd;
380 unsigned int last_entry_dirlen;
381 int sizeof_void_space = 0;
385 struct ext_filesystem *fs = get_fs();
386 /* directory entry */
387 struct ext2_dirent *dir;
388 char *temp_dir = NULL;
391 uint32_t new_blockcnt;
393 zero_buffer = zalloc(fs->blksz);
395 printf("No Memory\n");
398 root_first_block_buffer = zalloc(fs->blksz);
399 if (!root_first_block_buffer) {
401 printf("No Memory\n");
406 /* read the block no allocated to a file */
407 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
409 root_blknr = read_allocated_block(g_parent_inode,
411 if (root_blknr == 0) {
412 first_block_no_of_root = previous_blknr;
415 previous_blknr = root_blknr;
418 status = ext4fs_devread((lbaint_t)first_block_no_of_root
420 0, fs->blksz, root_first_block_buffer);
424 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
426 dir = (struct ext2_dirent *)root_first_block_buffer;
428 while (le16_to_cpu(dir->direntlen) > 0) {
430 * blocksize-totalbytes because last directory length
431 * i.e. dir->direntlen is free availble space in the
432 * block that means it is a last entry of directory
436 /* traversing the each directory entry */
437 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
438 if (strlen(filename) % 4 != 0)
439 padding_factor = 4 - (strlen(filename) % 4);
441 new_entry_byte_reqd = strlen(filename) +
442 sizeof(struct ext2_dirent) + padding_factor;
445 * update last directory entry length to its
446 * length because we are creating new directory
449 if (dir->namelen % 4 != 0)
450 padding_factor = 4 - (dir->namelen % 4);
452 last_entry_dirlen = dir->namelen +
453 sizeof(struct ext2_dirent) + padding_factor;
454 if ((fs->blksz - totalbytes - last_entry_dirlen) <
455 new_entry_byte_reqd) {
456 printf("Last Block Full:Allocate new block\n");
458 if (le32_to_cpu(g_parent_inode->flags) &
460 printf("Directory uses extents\n");
463 if (direct_blk_idx == INDIRECT_BLOCKS - 1) {
464 printf("Directory exceeds limit\n");
467 new_blk_no = ext4fs_get_new_blk_no();
468 if (new_blk_no == -1) {
469 printf("no block left to assign\n");
472 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
473 g_parent_inode->b.blocks.dir_blocks[direct_blk_idx] =
474 cpu_to_le32(new_blk_no);
476 new_size = le32_to_cpu(g_parent_inode->size);
477 new_size += fs->blksz;
478 g_parent_inode->size = cpu_to_le32(new_size);
480 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
481 new_blockcnt += fs->sect_perblk;
482 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
484 if (ext4fs_put_metadata
485 (root_first_block_buffer,
486 first_block_no_of_root))
490 dir->direntlen = cpu_to_le16(last_entry_dirlen);
494 templength = le16_to_cpu(dir->direntlen);
495 totalbytes = totalbytes + templength;
496 sizeof_void_space = check_void_in_dentry(dir, filename);
497 if (sizeof_void_space)
500 dir = (struct ext2_dirent *)((char *)dir + templength);
503 /* make a pointer ready for creating next directory entry */
504 templength = le16_to_cpu(dir->direntlen);
505 totalbytes = totalbytes + templength;
506 dir = (struct ext2_dirent *)((char *)dir + templength);
508 /* get the next available inode number */
509 inodeno = ext4fs_get_new_inode_no();
511 printf("no inode left to assign\n");
514 dir->inode = cpu_to_le32(inodeno);
515 if (sizeof_void_space)
516 dir->direntlen = cpu_to_le16(sizeof_void_space);
518 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
520 dir->namelen = strlen(filename);
521 dir->filetype = FILETYPE_REG; /* regular file */
522 temp_dir = (char *)dir;
523 temp_dir = temp_dir + sizeof(struct ext2_dirent);
524 memcpy(temp_dir, filename, strlen(filename));
526 /* update or write the 1st block of root inode */
527 if (ext4fs_put_metadata(root_first_block_buffer,
528 first_block_no_of_root))
533 free(root_first_block_buffer);
538 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
547 unsigned char *block_buffer = NULL;
548 struct ext2_dirent *dir = NULL;
549 struct ext_filesystem *fs = get_fs();
551 /* read the block no allocated to a file */
552 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
554 blknr = read_allocated_block(parent_inode, direct_blk_idx);
558 /* read the blocks of parent inode */
559 block_buffer = zalloc(fs->blksz);
563 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
564 0, fs->blksz, (char *)block_buffer);
568 dir = (struct ext2_dirent *)block_buffer;
571 while (le16_to_cpu(dir->direntlen) >= 0) {
573 * blocksize-totalbytes because last directory
574 * length i.e.,*dir->direntlen is free availble
575 * space in the block that means
576 * it is a last entry of directory entry
578 if (dir->inode && (strlen(dirname) == dir->namelen)) {
579 if (strncmp(dirname, ptr + sizeof(struct ext2_dirent), dir->namelen) == 0) {
580 inodeno = le32_to_cpu(dir->inode);
585 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
588 /* traversing the each directory entry */
589 templength = le16_to_cpu(dir->direntlen);
590 totalbytes = totalbytes + templength;
591 dir = (struct ext2_dirent *)((char *)dir + templength);
608 static int find_dir_depth(char *dirname)
610 char *token = strtok(dirname, "/");
612 while (token != NULL) {
613 token = strtok(NULL, "/");
616 return count + 1 + 1;
618 * for example for string /home/temp
619 * depth=home(1)+temp(1)+1 extra for NULL;
624 static int parse_path(char **arr, char *dirname)
626 char *token = strtok(dirname, "/");
630 arr[i] = zalloc(strlen("/") + 1);
633 memcpy(arr[i++], "/", strlen("/"));
635 /* add each path entry after root */
636 while (token != NULL) {
637 arr[i] = zalloc(strlen(token) + 1);
640 memcpy(arr[i++], token, strlen(token));
641 token = strtok(NULL, "/");
648 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
650 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
657 * Function: ext4fs_get_parent_inode_num
658 * Return Value: inode Number of the parent directory of file/Directory to be
660 * dirname : Input parmater, input path name of the file/directory to be created
661 * dname : Output parameter, to be filled with the name of the directory
662 * extracted from dirname
664 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
668 int matched_inode_no;
669 int result_inode_no = -1;
671 char *depth_dirname = NULL;
672 char *parse_dirname = NULL;
673 struct ext2_inode *parent_inode = NULL;
674 struct ext2_inode *first_inode = NULL;
675 struct ext2_inode temp_inode;
677 if (*dirname != '/') {
678 printf("Please supply Absolute path\n");
682 /* TODO: input validation make equivalent to linux */
683 depth_dirname = zalloc(strlen(dirname) + 1);
687 memcpy(depth_dirname, dirname, strlen(dirname));
688 depth = find_dir_depth(depth_dirname);
689 parse_dirname = zalloc(strlen(dirname) + 1);
692 memcpy(parse_dirname, dirname, strlen(dirname));
694 /* allocate memory for each directory level */
695 ptr = zalloc((depth) * sizeof(char *));
698 if (parse_path(ptr, parse_dirname))
700 parent_inode = zalloc(sizeof(struct ext2_inode));
703 first_inode = zalloc(sizeof(struct ext2_inode));
706 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
707 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
709 result_inode_no = EXT2_ROOT_INO;
710 for (i = 1; i < depth; i++) {
711 matched_inode_no = search_dir(parent_inode, ptr[i]);
712 if (matched_inode_no == -1) {
713 if (ptr[i + 1] == NULL && i == 1) {
714 result_inode_no = EXT2_ROOT_INO;
717 if (ptr[i + 1] == NULL)
719 printf("Invalid path\n");
720 result_inode_no = -1;
724 if (ptr[i + 1] != NULL) {
725 memset(parent_inode, '\0',
726 sizeof(struct ext2_inode));
727 if (ext4fs_iget(matched_inode_no,
729 result_inode_no = -1;
732 result_inode_no = matched_inode_no;
741 matched_inode_no = search_dir(first_inode, ptr[i]);
743 matched_inode_no = search_dir(parent_inode, ptr[i]);
745 if (matched_inode_no != -1) {
746 ext4fs_iget(matched_inode_no, &temp_inode);
747 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
748 printf("It is a Directory\n");
749 result_inode_no = -1;
754 if (strlen(ptr[i]) > 256) {
755 result_inode_no = -1;
758 memcpy(dname, ptr[i], strlen(ptr[i]));
763 for (i = 0; i < depth; i++) {
772 return result_inode_no;
775 static int unlink_filename(char *filename, unsigned int blknr)
781 char *root_first_block_buffer = NULL;
782 struct ext2_dirent *dir = NULL;
783 struct ext2_dirent *previous_dir = NULL;
785 struct ext_filesystem *fs = get_fs();
788 /* get the first block of root */
789 root_first_block_buffer = zalloc(fs->blksz);
790 if (!root_first_block_buffer)
792 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
793 fs->blksz, root_first_block_buffer);
797 if (ext4fs_log_journal(root_first_block_buffer, blknr))
799 dir = (struct ext2_dirent *)root_first_block_buffer;
802 while (le16_to_cpu(dir->direntlen) >= 0) {
804 * blocksize-totalbytes because last
805 * directory length i.e., *dir->direntlen
806 * is free availble space in the block that
807 * means it is a last entry of directory entry
809 if (dir->inode && (strlen(filename) == dir->namelen) &&
810 (strncmp(ptr + sizeof(struct ext2_dirent),
811 filename, dir->namelen) == 0)) {
812 printf("file found, deleting\n");
813 inodeno = le32_to_cpu(dir->inode);
816 new_len = le16_to_cpu(previous_dir->direntlen);
817 new_len += le16_to_cpu(dir->direntlen);
818 previous_dir->direntlen = cpu_to_le16(new_len);
826 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
829 /* traversing the each directory entry */
830 templength = le16_to_cpu(dir->direntlen);
831 totalbytes = totalbytes + templength;
833 dir = (struct ext2_dirent *)((char *)dir + templength);
839 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
844 free(root_first_block_buffer);
849 int ext4fs_filename_unlink(char *filename)
851 short direct_blk_idx = 0;
855 /* read the block no allocated to a file */
856 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
858 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
861 inodeno = unlink_filename(filename, blknr);
869 uint32_t ext4fs_get_new_blk_no(void)
875 static int prev_bg_bitmap_index = -1;
876 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
877 struct ext_filesystem *fs = get_fs();
878 char *journal_buffer = zalloc(fs->blksz);
879 char *zero_buffer = zalloc(fs->blksz);
880 if (!journal_buffer || !zero_buffer)
882 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
884 if (fs->first_pass_bbmap == 0) {
885 for (i = 0; i < fs->no_blkgrp; i++) {
886 if (le16_to_cpu(bgd[i].free_blocks)) {
887 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
889 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
890 zero_buffer, fs->blksz);
891 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
892 bgd[i].bg_flags = cpu_to_le16(new_flags);
893 memcpy(fs->blk_bmaps[i], zero_buffer,
897 _get_new_blk_no(fs->blk_bmaps[i]);
898 if (fs->curr_blkno == -1)
899 /* if block bitmap is completely fill */
901 fs->curr_blkno = fs->curr_blkno +
903 fs->first_pass_bbmap++;
904 ext4fs_bg_free_blocks_dec(&bgd[i]);
905 ext4fs_sb_free_blocks_dec(fs->sb);
906 status = ext4fs_devread(
907 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
913 if (ext4fs_log_journal(journal_buffer,
914 le32_to_cpu(bgd[i].block_id)))
918 debug("no space left on block group %d\n", i);
926 /* get the blockbitmap index respective to blockno */
927 bg_idx = fs->curr_blkno / blk_per_grp;
928 if (fs->blksz == 1024) {
929 remainder = fs->curr_blkno % blk_per_grp;
935 * To skip completely filled block group bitmaps
936 * Optimize the block allocation
938 if (bg_idx >= fs->no_blkgrp)
941 if (bgd[bg_idx].free_blocks == 0) {
942 debug("block group %u is full. Skipping\n", bg_idx);
943 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
948 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
950 memset(zero_buffer, '\0', fs->blksz);
951 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
952 zero_buffer, fs->blksz);
953 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
954 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
955 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
958 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
960 debug("going for restart for the block no %ld %u\n",
961 fs->curr_blkno, bg_idx);
966 if (prev_bg_bitmap_index != bg_idx) {
967 memset(journal_buffer, '\0', fs->blksz);
968 status = ext4fs_devread(
969 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
971 0, fs->blksz, journal_buffer);
974 if (ext4fs_log_journal(journal_buffer,
975 le32_to_cpu(bgd[bg_idx].block_id)))
978 prev_bg_bitmap_index = bg_idx;
980 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
981 ext4fs_sb_free_blocks_dec(fs->sb);
985 free(journal_buffer);
988 return fs->curr_blkno;
990 free(journal_buffer);
996 int ext4fs_get_new_inode_no(void)
1000 unsigned int ibmap_idx;
1001 static int prev_inode_bitmap_index = -1;
1002 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1003 struct ext_filesystem *fs = get_fs();
1004 char *journal_buffer = zalloc(fs->blksz);
1005 char *zero_buffer = zalloc(fs->blksz);
1006 if (!journal_buffer || !zero_buffer)
1008 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
1010 if (fs->first_pass_ibmap == 0) {
1011 for (i = 0; i < fs->no_blkgrp; i++) {
1012 if (bgd[i].free_inodes) {
1013 if (bgd[i].bg_itable_unused !=
1015 bgd[i].bg_itable_unused =
1017 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1019 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
1020 zero_buffer, fs->blksz);
1021 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1022 bgd[i].bg_flags = cpu_to_le16(new_flags);
1023 memcpy(fs->inode_bmaps[i],
1024 zero_buffer, fs->blksz);
1027 _get_new_inode_no(fs->inode_bmaps[i]);
1028 if (fs->curr_inode_no == -1)
1029 /* if block bitmap is completely fill */
1031 fs->curr_inode_no = fs->curr_inode_no +
1032 (i * inodes_per_grp);
1033 fs->first_pass_ibmap++;
1034 ext4fs_bg_free_inodes_dec(&bgd[i]);
1035 ext4fs_bg_itable_unused_dec(&bgd[i]);
1036 ext4fs_sb_free_inodes_dec(fs->sb);
1037 status = ext4fs_devread(
1038 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
1044 if (ext4fs_log_journal(journal_buffer,
1045 le32_to_cpu(bgd[i].inode_id)))
1049 debug("no inode left on block group %d\n", i);
1054 fs->curr_inode_no++;
1055 /* get the blockbitmap index respective to blockno */
1056 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1057 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1059 memset(zero_buffer, '\0', fs->blksz);
1060 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1061 zero_buffer, fs->blksz);
1062 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1063 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
1064 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1068 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1069 fs->inode_bmaps[ibmap_idx],
1071 debug("going for restart for the block no %d %u\n",
1072 fs->curr_inode_no, ibmap_idx);
1076 /* journal backup */
1077 if (prev_inode_bitmap_index != ibmap_idx) {
1078 memset(journal_buffer, '\0', fs->blksz);
1079 status = ext4fs_devread(
1080 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
1082 0, fs->blksz, journal_buffer);
1085 if (ext4fs_log_journal(journal_buffer,
1086 le32_to_cpu(bgd[ibmap_idx].inode_id)))
1088 prev_inode_bitmap_index = ibmap_idx;
1090 if (bgd[ibmap_idx].bg_itable_unused !=
1091 bgd[ibmap_idx].free_inodes)
1092 bgd[ibmap_idx].bg_itable_unused =
1093 bgd[ibmap_idx].free_inodes;
1094 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1095 ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
1096 ext4fs_sb_free_inodes_dec(fs->sb);
1101 free(journal_buffer);
1104 return fs->curr_inode_no;
1106 free(journal_buffer);
1114 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1115 unsigned int *total_remaining_blocks,
1116 unsigned int *no_blks_reqd)
1120 long int actual_block_no;
1121 long int si_blockno;
1122 /* si :single indirect */
1123 __le32 *si_buffer = NULL;
1124 __le32 *si_start_addr = NULL;
1125 struct ext_filesystem *fs = get_fs();
1127 if (*total_remaining_blocks != 0) {
1128 si_buffer = zalloc(fs->blksz);
1130 printf("No Memory\n");
1133 si_start_addr = si_buffer;
1134 si_blockno = ext4fs_get_new_blk_no();
1135 if (si_blockno == -1) {
1136 printf("no block left to assign\n");
1140 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1142 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1143 0, fs->blksz, (char *)si_buffer);
1144 memset(si_buffer, '\0', fs->blksz);
1148 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1149 actual_block_no = ext4fs_get_new_blk_no();
1150 if (actual_block_no == -1) {
1151 printf("no block left to assign\n");
1154 *si_buffer = cpu_to_le32(actual_block_no);
1155 debug("SIAB %u: %u\n", *si_buffer,
1156 *total_remaining_blocks);
1159 (*total_remaining_blocks)--;
1160 if (*total_remaining_blocks == 0)
1164 /* write the block to disk */
1165 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1166 si_start_addr, fs->blksz);
1167 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1170 free(si_start_addr);
1173 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1174 unsigned int *total_remaining_blocks,
1175 unsigned int *no_blks_reqd)
1180 long int actual_block_no;
1181 /* di:double indirect */
1182 long int di_blockno_parent;
1183 long int di_blockno_child;
1184 __le32 *di_parent_buffer = NULL;
1185 __le32 *di_child_buff = NULL;
1186 __le32 *di_block_start_addr = NULL;
1187 __le32 *di_child_buff_start = NULL;
1188 struct ext_filesystem *fs = get_fs();
1190 if (*total_remaining_blocks != 0) {
1191 /* double indirect parent block connecting to inode */
1192 di_blockno_parent = ext4fs_get_new_blk_no();
1193 if (di_blockno_parent == -1) {
1194 printf("no block left to assign\n");
1197 di_parent_buffer = zalloc(fs->blksz);
1198 if (!di_parent_buffer)
1201 di_block_start_addr = di_parent_buffer;
1203 debug("DIPB %ld: %u\n", di_blockno_parent,
1204 *total_remaining_blocks);
1206 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1208 fs->blksz, (char *)di_parent_buffer);
1211 printf("%s: Device read error!\n", __func__);
1214 memset(di_parent_buffer, '\0', fs->blksz);
1217 * start:for each double indirect parent
1218 * block create one more block
1220 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1221 di_blockno_child = ext4fs_get_new_blk_no();
1222 if (di_blockno_child == -1) {
1223 printf("no block left to assign\n");
1226 di_child_buff = zalloc(fs->blksz);
1230 di_child_buff_start = di_child_buff;
1231 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1234 debug("DICB %ld: %u\n", di_blockno_child,
1235 *total_remaining_blocks);
1237 status = ext4fs_devread((lbaint_t)di_blockno_child *
1240 (char *)di_child_buff);
1243 printf("%s: Device read error!\n", __func__);
1246 memset(di_child_buff, '\0', fs->blksz);
1247 /* filling of actual datablocks for each child */
1248 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1249 actual_block_no = ext4fs_get_new_blk_no();
1250 if (actual_block_no == -1) {
1251 printf("no block left to assign\n");
1254 *di_child_buff = cpu_to_le32(actual_block_no);
1255 debug("DIAB %ld: %u\n", actual_block_no,
1256 *total_remaining_blocks);
1259 (*total_remaining_blocks)--;
1260 if (*total_remaining_blocks == 0)
1263 /* write the block table */
1264 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1265 di_child_buff_start, fs->blksz);
1266 free(di_child_buff_start);
1267 di_child_buff_start = NULL;
1269 if (*total_remaining_blocks == 0)
1272 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1273 di_block_start_addr, fs->blksz);
1274 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1277 free(di_block_start_addr);
1280 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1281 unsigned int *total_remaining_blocks,
1282 unsigned int *no_blks_reqd)
1287 long int actual_block_no;
1288 /* ti: Triple Indirect */
1289 long int ti_gp_blockno;
1290 long int ti_parent_blockno;
1291 long int ti_child_blockno;
1292 __le32 *ti_gp_buff = NULL;
1293 __le32 *ti_parent_buff = NULL;
1294 __le32 *ti_child_buff = NULL;
1295 __le32 *ti_gp_buff_start_addr = NULL;
1296 __le32 *ti_pbuff_start_addr = NULL;
1297 __le32 *ti_cbuff_start_addr = NULL;
1298 struct ext_filesystem *fs = get_fs();
1299 if (*total_remaining_blocks != 0) {
1300 /* triple indirect grand parent block connecting to inode */
1301 ti_gp_blockno = ext4fs_get_new_blk_no();
1302 if (ti_gp_blockno == -1) {
1303 printf("no block left to assign\n");
1306 ti_gp_buff = zalloc(fs->blksz);
1310 ti_gp_buff_start_addr = ti_gp_buff;
1312 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1313 *total_remaining_blocks);
1315 /* for each 4 byte grand parent entry create one more block */
1316 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1317 ti_parent_blockno = ext4fs_get_new_blk_no();
1318 if (ti_parent_blockno == -1) {
1319 printf("no block left to assign\n");
1322 ti_parent_buff = zalloc(fs->blksz);
1323 if (!ti_parent_buff)
1326 ti_pbuff_start_addr = ti_parent_buff;
1327 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1330 debug("TIPB %ld: %u\n", ti_parent_blockno,
1331 *total_remaining_blocks);
1333 /* for each 4 byte entry parent create one more block */
1334 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1335 ti_child_blockno = ext4fs_get_new_blk_no();
1336 if (ti_child_blockno == -1) {
1337 printf("no block left assign\n");
1340 ti_child_buff = zalloc(fs->blksz);
1344 ti_cbuff_start_addr = ti_child_buff;
1345 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1348 debug("TICB %ld: %u\n", ti_parent_blockno,
1349 *total_remaining_blocks);
1351 /* fill actual datablocks for each child */
1352 for (k = 0; k < (fs->blksz / sizeof(int));
1355 ext4fs_get_new_blk_no();
1356 if (actual_block_no == -1) {
1357 printf("no block left\n");
1358 free(ti_cbuff_start_addr);
1361 *ti_child_buff = cpu_to_le32(actual_block_no);
1362 debug("TIAB %ld: %u\n", actual_block_no,
1363 *total_remaining_blocks);
1366 (*total_remaining_blocks)--;
1367 if (*total_remaining_blocks == 0)
1370 /* write the child block */
1371 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1372 (uint64_t)fs->blksz)),
1373 ti_cbuff_start_addr, fs->blksz);
1374 free(ti_cbuff_start_addr);
1376 if (*total_remaining_blocks == 0)
1379 /* write the parent block */
1380 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1381 ti_pbuff_start_addr, fs->blksz);
1382 free(ti_pbuff_start_addr);
1384 if (*total_remaining_blocks == 0)
1387 /* write the grand parent block */
1388 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1389 ti_gp_buff_start_addr, fs->blksz);
1390 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1391 free(ti_gp_buff_start_addr);
1395 free(ti_pbuff_start_addr);
1397 free(ti_gp_buff_start_addr);
1400 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1401 unsigned int total_remaining_blocks,
1402 unsigned int *total_no_of_block)
1405 long int direct_blockno;
1406 unsigned int no_blks_reqd = 0;
1408 /* allocation of direct blocks */
1409 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1410 direct_blockno = ext4fs_get_new_blk_no();
1411 if (direct_blockno == -1) {
1412 printf("no block left to assign\n");
1415 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1416 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1418 total_remaining_blocks--;
1421 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1423 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1425 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1427 *total_no_of_block += no_blks_reqd;
1432 static struct ext4_extent_header *ext4fs_get_extent_block
1433 (struct ext2_data *data, char *buf,
1434 struct ext4_extent_header *ext_block,
1435 uint32_t fileblock, int log2_blksz)
1437 struct ext4_extent_idx *index;
1438 unsigned long long block;
1439 int blksz = EXT2_BLOCK_SIZE(data);
1443 index = (struct ext4_extent_idx *)(ext_block + 1);
1445 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1448 if (ext_block->eh_depth == 0)
1453 if (i >= le16_to_cpu(ext_block->eh_entries))
1455 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1460 block = le16_to_cpu(index[i].ei_leaf_hi);
1461 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1463 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1465 ext_block = (struct ext4_extent_header *)buf;
1471 static int ext4fs_blockgroup
1472 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1475 unsigned int blkoff, desc_per_blk;
1476 int log2blksz = get_fs()->dev_desc->log2blksz;
1478 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1480 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1481 group / desc_per_blk;
1482 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1484 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1485 group, blkno, blkoff);
1487 return ext4fs_devread((lbaint_t)blkno <<
1488 (LOG2_BLOCK_SIZE(data) - log2blksz),
1489 blkoff, sizeof(struct ext2_block_group),
1493 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1495 struct ext2_block_group blkgrp;
1496 struct ext2_sblock *sblock = &data->sblock;
1497 struct ext_filesystem *fs = get_fs();
1498 int log2blksz = get_fs()->dev_desc->log2blksz;
1499 int inodes_per_block, status;
1501 unsigned int blkoff;
1503 /* It is easier to calculate if the first inode is 0. */
1505 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1506 (sblock->inodes_per_group), &blkgrp);
1510 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1511 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1512 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1513 blkoff = (ino % inodes_per_block) * fs->inodesz;
1514 /* Read the inode. */
1515 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1517 sizeof(struct ext2_inode), (char *)inode);
1524 long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1531 long int perblock_parent;
1532 long int perblock_child;
1533 unsigned long long start;
1534 /* get the blocksize of the filesystem */
1535 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1536 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1537 - get_fs()->dev_desc->log2blksz;
1539 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1540 char *buf = zalloc(blksz);
1543 struct ext4_extent_header *ext_block;
1544 struct ext4_extent *extent;
1547 ext4fs_get_extent_block(ext4fs_root, buf,
1548 (struct ext4_extent_header *)
1549 inode->b.blocks.dir_blocks,
1550 fileblock, log2_blksz);
1552 printf("invalid extent block\n");
1557 extent = (struct ext4_extent *)(ext_block + 1);
1561 if (i >= le16_to_cpu(ext_block->eh_entries))
1563 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1565 fileblock -= le32_to_cpu(extent[i].ee_block);
1566 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1571 start = le16_to_cpu(extent[i].ee_start_hi);
1572 start = (start << 32) +
1573 le32_to_cpu(extent[i].ee_start_lo);
1575 return fileblock + start;
1578 printf("Extent Error\n");
1583 /* Direct blocks. */
1584 if (fileblock < INDIRECT_BLOCKS)
1585 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1588 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1589 if (ext4fs_indir1_block == NULL) {
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;
1597 ext4fs_indir1_blkno = -1;
1599 if (blksz != ext4fs_indir1_size) {
1600 free(ext4fs_indir1_block);
1601 ext4fs_indir1_block = NULL;
1602 ext4fs_indir1_size = 0;
1603 ext4fs_indir1_blkno = -1;
1604 ext4fs_indir1_block = zalloc(blksz);
1605 if (ext4fs_indir1_block == NULL) {
1606 printf("** SI ext2fs read block (indir 1):"
1607 "malloc failed. **\n");
1610 ext4fs_indir1_size = blksz;
1612 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1613 log2_blksz) != ext4fs_indir1_blkno) {
1615 ext4fs_devread((lbaint_t)le32_to_cpu
1617 indir_block) << log2_blksz, 0,
1618 blksz, (char *)ext4fs_indir1_block);
1620 printf("** SI ext2fs read block (indir 1)"
1624 ext4fs_indir1_blkno =
1625 le32_to_cpu(inode->b.blocks.
1626 indir_block) << log2_blksz;
1628 blknr = le32_to_cpu(ext4fs_indir1_block
1629 [fileblock - INDIRECT_BLOCKS]);
1631 /* Double indirect. */
1632 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1633 (blksz / 4 + 1)))) {
1635 long int perblock = blksz / 4;
1636 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1638 if (ext4fs_indir1_block == NULL) {
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;
1646 ext4fs_indir1_blkno = -1;
1648 if (blksz != ext4fs_indir1_size) {
1649 free(ext4fs_indir1_block);
1650 ext4fs_indir1_block = NULL;
1651 ext4fs_indir1_size = 0;
1652 ext4fs_indir1_blkno = -1;
1653 ext4fs_indir1_block = zalloc(blksz);
1654 if (ext4fs_indir1_block == NULL) {
1655 printf("** DI ext2fs read block (indir 2 1)"
1656 "malloc failed. **\n");
1659 ext4fs_indir1_size = blksz;
1661 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1662 log2_blksz) != ext4fs_indir1_blkno) {
1664 ext4fs_devread((lbaint_t)le32_to_cpu
1666 double_indir_block) << log2_blksz,
1668 (char *)ext4fs_indir1_block);
1670 printf("** DI ext2fs read block (indir 2 1)"
1674 ext4fs_indir1_blkno =
1675 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1679 if (ext4fs_indir2_block == NULL) {
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;
1687 ext4fs_indir2_blkno = -1;
1689 if (blksz != ext4fs_indir2_size) {
1690 free(ext4fs_indir2_block);
1691 ext4fs_indir2_block = NULL;
1692 ext4fs_indir2_size = 0;
1693 ext4fs_indir2_blkno = -1;
1694 ext4fs_indir2_block = zalloc(blksz);
1695 if (ext4fs_indir2_block == NULL) {
1696 printf("** DI ext2fs read block (indir 2 2)"
1697 "malloc failed. **\n");
1700 ext4fs_indir2_size = blksz;
1702 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1703 log2_blksz) != ext4fs_indir2_blkno) {
1704 status = ext4fs_devread((lbaint_t)le32_to_cpu
1705 (ext4fs_indir1_block
1707 perblock]) << log2_blksz, 0,
1709 (char *)ext4fs_indir2_block);
1711 printf("** DI ext2fs read block (indir 2 2)"
1715 ext4fs_indir2_blkno =
1716 le32_to_cpu(ext4fs_indir1_block[rblock
1721 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1723 /* Tripple indirect. */
1725 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1726 (blksz / 4 * blksz / 4));
1727 perblock_child = blksz / 4;
1728 perblock_parent = ((blksz / 4) * (blksz / 4));
1730 if (ext4fs_indir1_block == NULL) {
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;
1738 ext4fs_indir1_blkno = -1;
1740 if (blksz != ext4fs_indir1_size) {
1741 free(ext4fs_indir1_block);
1742 ext4fs_indir1_block = NULL;
1743 ext4fs_indir1_size = 0;
1744 ext4fs_indir1_blkno = -1;
1745 ext4fs_indir1_block = zalloc(blksz);
1746 if (ext4fs_indir1_block == NULL) {
1747 printf("** TI ext2fs read block (indir 2 1)"
1748 "malloc failed. **\n");
1751 ext4fs_indir1_size = blksz;
1753 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1754 log2_blksz) != ext4fs_indir1_blkno) {
1755 status = ext4fs_devread
1757 le32_to_cpu(inode->b.blocks.triple_indir_block)
1758 << log2_blksz, 0, blksz,
1759 (char *)ext4fs_indir1_block);
1761 printf("** TI ext2fs read block (indir 2 1)"
1765 ext4fs_indir1_blkno =
1766 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1770 if (ext4fs_indir2_block == NULL) {
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;
1778 ext4fs_indir2_blkno = -1;
1780 if (blksz != ext4fs_indir2_size) {
1781 free(ext4fs_indir2_block);
1782 ext4fs_indir2_block = NULL;
1783 ext4fs_indir2_size = 0;
1784 ext4fs_indir2_blkno = -1;
1785 ext4fs_indir2_block = zalloc(blksz);
1786 if (ext4fs_indir2_block == NULL) {
1787 printf("** TI ext2fs read block (indir 2 2)"
1788 "malloc failed. **\n");
1791 ext4fs_indir2_size = blksz;
1793 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1794 perblock_parent]) <<
1796 != ext4fs_indir2_blkno) {
1797 status = ext4fs_devread((lbaint_t)le32_to_cpu
1798 (ext4fs_indir1_block
1800 perblock_parent]) <<
1801 log2_blksz, 0, blksz,
1802 (char *)ext4fs_indir2_block);
1804 printf("** TI ext2fs read block (indir 2 2)"
1808 ext4fs_indir2_blkno =
1809 le32_to_cpu(ext4fs_indir1_block[rblock /
1814 if (ext4fs_indir3_block == NULL) {
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;
1822 ext4fs_indir3_blkno = -1;
1824 if (blksz != ext4fs_indir3_size) {
1825 free(ext4fs_indir3_block);
1826 ext4fs_indir3_block = NULL;
1827 ext4fs_indir3_size = 0;
1828 ext4fs_indir3_blkno = -1;
1829 ext4fs_indir3_block = zalloc(blksz);
1830 if (ext4fs_indir3_block == NULL) {
1831 printf("** TI ext2fs read block (indir 2 2)"
1832 "malloc failed. **\n");
1835 ext4fs_indir3_size = blksz;
1837 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1840 log2_blksz) != ext4fs_indir3_blkno) {
1842 ext4fs_devread((lbaint_t)le32_to_cpu
1843 (ext4fs_indir2_block
1844 [(rblock / perblock_child)
1845 % (blksz / 4)]) << log2_blksz, 0,
1846 blksz, (char *)ext4fs_indir3_block);
1848 printf("** TI ext2fs read block (indir 2 2)"
1852 ext4fs_indir3_blkno =
1853 le32_to_cpu(ext4fs_indir2_block[(rblock /
1860 blknr = le32_to_cpu(ext4fs_indir3_block
1861 [rblock % perblock_child]);
1863 debug("read_allocated_block %ld\n", blknr);
1869 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1872 * This function assures that for a file with the same name but different size
1873 * the sequential store on the ext4 filesystem will be correct.
1875 * In this function the global data, responsible for internal representation
1876 * of the ext4 data are initialized to the reset state. Without this, during
1877 * replacement of the smaller file with the bigger truncation of new file was
1880 void ext4fs_reinit_global(void)
1882 if (ext4fs_indir1_block != NULL) {
1883 free(ext4fs_indir1_block);
1884 ext4fs_indir1_block = NULL;
1885 ext4fs_indir1_size = 0;
1886 ext4fs_indir1_blkno = -1;
1888 if (ext4fs_indir2_block != NULL) {
1889 free(ext4fs_indir2_block);
1890 ext4fs_indir2_block = NULL;
1891 ext4fs_indir2_size = 0;
1892 ext4fs_indir2_blkno = -1;
1894 if (ext4fs_indir3_block != NULL) {
1895 free(ext4fs_indir3_block);
1896 ext4fs_indir3_block = NULL;
1897 ext4fs_indir3_size = 0;
1898 ext4fs_indir3_blkno = -1;
1901 void ext4fs_close(void)
1903 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1904 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1907 if (ext4fs_root != NULL) {
1912 ext4fs_reinit_global();
1915 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1916 struct ext2fs_node **fnode, int *ftype)
1918 unsigned int fpos = 0;
1921 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1925 printf("Iterate dir %s\n", name);
1926 #endif /* of DEBUG */
1927 if (!diro->inode_read) {
1928 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1932 /* Search the file. */
1933 while (fpos < le32_to_cpu(diro->inode.size)) {
1934 struct ext2_dirent dirent;
1936 status = ext4fs_read_file(diro, fpos,
1937 sizeof(struct ext2_dirent),
1938 (char *)&dirent, &actread);
1942 if (dirent.direntlen == 0) {
1943 printf("Failed to iterate over directory %s\n", name);
1947 if (dirent.namelen != 0) {
1948 char filename[dirent.namelen + 1];
1949 struct ext2fs_node *fdiro;
1950 int type = FILETYPE_UNKNOWN;
1952 status = ext4fs_read_file(diro,
1954 sizeof(struct ext2_dirent),
1955 dirent.namelen, filename,
1960 fdiro = zalloc(sizeof(struct ext2fs_node));
1964 fdiro->data = diro->data;
1965 fdiro->ino = le32_to_cpu(dirent.inode);
1967 filename[dirent.namelen] = '\0';
1969 if (dirent.filetype != FILETYPE_UNKNOWN) {
1970 fdiro->inode_read = 0;
1972 if (dirent.filetype == FILETYPE_DIRECTORY)
1973 type = FILETYPE_DIRECTORY;
1974 else if (dirent.filetype == FILETYPE_SYMLINK)
1975 type = FILETYPE_SYMLINK;
1976 else if (dirent.filetype == FILETYPE_REG)
1977 type = FILETYPE_REG;
1979 status = ext4fs_read_inode(diro->data,
1987 fdiro->inode_read = 1;
1989 if ((le16_to_cpu(fdiro->inode.mode) &
1990 FILETYPE_INO_MASK) ==
1991 FILETYPE_INO_DIRECTORY) {
1992 type = FILETYPE_DIRECTORY;
1993 } else if ((le16_to_cpu(fdiro->inode.mode)
1994 & FILETYPE_INO_MASK) ==
1995 FILETYPE_INO_SYMLINK) {
1996 type = FILETYPE_SYMLINK;
1997 } else if ((le16_to_cpu(fdiro->inode.mode)
1998 & FILETYPE_INO_MASK) ==
2000 type = FILETYPE_REG;
2004 printf("iterate >%s<\n", filename);
2005 #endif /* of DEBUG */
2006 if ((name != NULL) && (fnode != NULL)
2007 && (ftype != NULL)) {
2008 if (strcmp(filename, name) == 0) {
2014 if (fdiro->inode_read == 0) {
2015 status = ext4fs_read_inode(diro->data,
2023 fdiro->inode_read = 1;
2026 case FILETYPE_DIRECTORY:
2029 case FILETYPE_SYMLINK:
2040 le32_to_cpu(fdiro->inode.size),
2045 fpos += le16_to_cpu(dirent.direntlen);
2050 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2053 struct ext2fs_node *diro = node;
2057 if (!diro->inode_read) {
2058 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2062 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2066 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2067 strncpy(symlink, diro->inode.b.symlink,
2068 le32_to_cpu(diro->inode.size));
2070 status = ext4fs_read_file(diro, 0,
2071 le32_to_cpu(diro->inode.size),
2073 if ((status < 0) || (actread == 0)) {
2078 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2082 static int ext4fs_find_file1(const char *currpath,
2083 struct ext2fs_node *currroot,
2084 struct ext2fs_node **currfound, int *foundtype)
2086 char fpath[strlen(currpath) + 1];
2090 int type = FILETYPE_DIRECTORY;
2091 struct ext2fs_node *currnode = currroot;
2092 struct ext2fs_node *oldnode = currroot;
2094 strncpy(fpath, currpath, strlen(currpath) + 1);
2096 /* Remove all leading slashes. */
2097 while (*name == '/')
2101 *currfound = currnode;
2108 /* Extract the actual part from the pathname. */
2109 next = strchr(name, '/');
2111 /* Remove all leading slashes. */
2112 while (*next == '/')
2116 if (type != FILETYPE_DIRECTORY) {
2117 ext4fs_free_node(currnode, currroot);
2123 /* Iterate over the directory. */
2124 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2131 /* Read in the symlink and follow it. */
2132 if (type == FILETYPE_SYMLINK) {
2135 /* Test if the symlink does not loop. */
2136 if (++symlinknest == 8) {
2137 ext4fs_free_node(currnode, currroot);
2138 ext4fs_free_node(oldnode, currroot);
2142 symlink = ext4fs_read_symlink(currnode);
2143 ext4fs_free_node(currnode, currroot);
2146 ext4fs_free_node(oldnode, currroot);
2150 debug("Got symlink >%s<\n", symlink);
2152 if (symlink[0] == '/') {
2153 ext4fs_free_node(oldnode, currroot);
2154 oldnode = &ext4fs_root->diropen;
2157 /* Lookup the node the symlink points to. */
2158 status = ext4fs_find_file1(symlink, oldnode,
2164 ext4fs_free_node(oldnode, currroot);
2169 ext4fs_free_node(oldnode, currroot);
2171 /* Found the node! */
2172 if (!next || *next == '\0') {
2173 *currfound = currnode;
2182 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2183 struct ext2fs_node **foundnode, int expecttype)
2186 int foundtype = FILETYPE_DIRECTORY;
2192 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2196 /* Check if the node that was found was of the expected type. */
2197 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2199 else if ((expecttype == FILETYPE_DIRECTORY)
2200 && (foundtype != expecttype))
2206 int ext4fs_open(const char *filename, loff_t *len)
2208 struct ext2fs_node *fdiro = NULL;
2211 if (ext4fs_root == NULL)
2215 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2220 if (!fdiro->inode_read) {
2221 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2226 *len = le32_to_cpu(fdiro->inode.size);
2227 ext4fs_file = fdiro;
2231 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2236 int ext4fs_mount(unsigned part_length)
2238 struct ext2_data *data;
2240 struct ext_filesystem *fs = get_fs();
2241 data = zalloc(SUPERBLOCK_SIZE);
2245 /* Read the superblock. */
2246 status = ext4_read_superblock((char *)&data->sblock);
2251 /* Make sure this is an ext2 filesystem. */
2252 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2256 * The 64bit feature was enabled when metadata_csum was enabled
2257 * and we do not support metadata_csum (and cannot reliably find
2258 * files when it is set. Refuse to mount.
2260 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
2261 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2265 if (le32_to_cpu(data->sblock.revision_level) == 0)
2268 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2270 debug("EXT2 rev %d, inode_size %d\n",
2271 le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2273 data->diropen.data = data;
2274 data->diropen.ino = 2;
2275 data->diropen.inode_read = 1;
2276 data->inode = &data->diropen.inode;
2278 status = ext4fs_read_inode(data, 2, data->inode);
2286 printf("Failed to mount ext2 filesystem...\n");