1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 - 2012 Samsung Electronics
4 * EXT4 filesystem implementation in Uboot by
5 * Uma Shankar <uma.shankar@samsung.com>
6 * Manjunatha C Achar <a.manjunatha@samsung.com>
8 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
15 * GRUB -- GRand Unified Bootloader
16 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
18 * ext4write : Based on generic ext4 protocol.
22 #include <ext_common.h>
27 #include <linux/stat.h>
28 #include <linux/time.h>
29 #include <asm/byteorder.h>
30 #include "ext4_common.h"
32 struct ext2_data *ext4fs_root;
33 struct ext2fs_node *ext4fs_file;
34 __le32 *ext4fs_indir1_block;
35 int ext4fs_indir1_size;
36 int ext4fs_indir1_blkno = -1;
37 __le32 *ext4fs_indir2_block;
38 int ext4fs_indir2_size;
39 int ext4fs_indir2_blkno = -1;
41 __le32 *ext4fs_indir3_block;
42 int ext4fs_indir3_size;
43 int ext4fs_indir3_blkno = -1;
44 struct ext2_inode *g_parent_inode;
45 static int symlinknest;
47 #if defined(CONFIG_EXT4_WRITE)
48 struct ext2_block_group *ext4fs_get_group_descriptor
49 (const struct ext_filesystem *fs, uint32_t bg_idx)
51 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
54 static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
56 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
59 static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
61 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
62 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
65 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
66 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
69 static inline void ext4fs_bg_free_inodes_dec
70 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
72 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
74 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
77 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
79 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
82 static inline void ext4fs_bg_free_blocks_dec
83 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
85 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
87 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
90 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
92 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
95 static inline void ext4fs_bg_itable_unused_dec
96 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
98 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
100 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
103 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
104 if (fs->gdsize == 64)
105 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
108 uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
110 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
111 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
115 void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
117 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
118 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
121 uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
122 const struct ext_filesystem *fs)
124 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
125 if (fs->gdsize == 64)
126 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
131 uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
132 const struct ext_filesystem *fs)
134 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
135 if (fs->gdsize == 64)
136 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
140 static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
142 return le16_to_cpu(bg->bg_flags);
145 static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
148 bg->bg_flags = cpu_to_le16(flags);
151 /* Block number of the block bitmap */
152 uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
153 const struct ext_filesystem *fs)
155 uint64_t block_nr = le32_to_cpu(bg->block_id);
156 if (fs->gdsize == 64)
157 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
161 /* Block number of the inode bitmap */
162 uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
163 const struct ext_filesystem *fs)
165 uint64_t block_nr = le32_to_cpu(bg->inode_id);
166 if (fs->gdsize == 64)
167 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
172 /* Block number of the inode table */
173 uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
174 const struct ext_filesystem *fs)
176 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
177 if (fs->gdsize == 64)
179 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
183 #if defined(CONFIG_EXT4_WRITE)
184 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
186 uint32_t res = size / n;
193 void put_ext4(uint64_t off, const void *buf, uint32_t size)
197 unsigned char *temp_ptr = NULL;
198 struct ext_filesystem *fs = get_fs();
199 int log2blksz = fs->dev_desc->log2blksz;
200 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
202 startblock = off >> log2blksz;
203 startblock += part_offset;
204 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
206 if (fs->dev_desc == NULL)
209 if ((startblock + (size >> log2blksz)) >
210 (part_offset + fs->total_sect)) {
211 printf("part_offset is " LBAFU "\n", part_offset);
212 printf("total_sector is %llu\n", fs->total_sect);
213 printf("error: overflow occurs\n");
218 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
220 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
221 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
223 if (size >> log2blksz != 0) {
224 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
225 (unsigned long *)buf);
227 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
229 memcpy(temp_ptr, buf, size);
230 blk_dwrite(fs->dev_desc, startblock, 1,
231 (unsigned long *)sec_buf);
236 static int _get_new_inode_no(unsigned char *buffer)
238 struct ext_filesystem *fs = get_fs();
244 /* get the blocksize of the filesystem */
245 unsigned char *ptr = buffer;
246 while (*ptr == 255) {
249 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
253 for (j = 0; j < fs->blksz; j++) {
258 status = input & operand;
273 static int _get_new_blk_no(unsigned char *buffer)
278 unsigned char *ptr = buffer;
279 struct ext_filesystem *fs = get_fs();
281 while (*ptr == 255) {
284 if (count == (fs->blksz * 8))
288 if (fs->blksz == 1024)
291 for (i = 0; i <= 7; i++) {
293 if (*ptr & operand) {
304 int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
306 int i, remainder, status;
307 unsigned char *ptr = buffer;
308 unsigned char operand;
310 remainder = blockno % 8;
311 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
313 i = i - (index * blocksize);
314 if (blocksize != 1024) {
316 operand = 1 << remainder;
317 status = *ptr & operand;
321 *ptr = *ptr | operand;
324 if (remainder == 0) {
329 operand = (1 << (remainder - 1));
331 status = *ptr & operand;
335 *ptr = *ptr | operand;
340 void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
342 int i, remainder, status;
343 unsigned char *ptr = buffer;
344 unsigned char operand;
346 remainder = blockno % 8;
347 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
349 i = i - (index * blocksize);
350 if (blocksize != 1024) {
352 operand = (1 << remainder);
353 status = *ptr & operand;
355 *ptr = *ptr & ~(operand);
357 if (remainder == 0) {
362 operand = (1 << (remainder - 1));
364 status = *ptr & operand;
366 *ptr = *ptr & ~(operand);
370 int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
372 int i, remainder, status;
373 unsigned char *ptr = buffer;
374 unsigned char operand;
376 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
378 remainder = inode_no % 8;
379 if (remainder == 0) {
384 operand = (1 << (remainder - 1));
386 status = *ptr & operand;
390 *ptr = *ptr | operand;
395 void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
397 int i, remainder, status;
398 unsigned char *ptr = buffer;
399 unsigned char operand;
401 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
403 remainder = inode_no % 8;
404 if (remainder == 0) {
409 operand = (1 << (remainder - 1));
411 status = *ptr & operand;
413 *ptr = *ptr & ~(operand);
416 uint16_t ext4fs_checksum_update(uint32_t i)
418 struct ext2_block_group *desc;
419 struct ext_filesystem *fs = get_fs();
421 __le32 le32_i = cpu_to_le32(i);
423 desc = ext4fs_get_group_descriptor(fs, i);
424 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
425 int offset = offsetof(struct ext2_block_group, bg_checksum);
427 crc = ext2fs_crc16(~0, fs->sb->unique_id,
428 sizeof(fs->sb->unique_id));
429 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
430 crc = ext2fs_crc16(crc, desc, offset);
431 offset += sizeof(desc->bg_checksum); /* skip checksum */
432 assert(offset == sizeof(*desc));
433 if (offset < fs->gdsize) {
434 crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
435 fs->gdsize - offset);
442 static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
445 int sizeof_void_space;
446 int new_entry_byte_reqd;
447 short padding_factor = 0;
449 if (dir->namelen % 4 != 0)
450 padding_factor = 4 - (dir->namelen % 4);
452 dentry_length = sizeof(struct ext2_dirent) +
453 dir->namelen + padding_factor;
454 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
455 if (sizeof_void_space == 0)
459 if (strlen(filename) % 4 != 0)
460 padding_factor = 4 - (strlen(filename) % 4);
462 new_entry_byte_reqd = strlen(filename) +
463 sizeof(struct ext2_dirent) + padding_factor;
464 if (sizeof_void_space >= new_entry_byte_reqd) {
465 dir->direntlen = cpu_to_le16(dentry_length);
466 return sizeof_void_space;
472 int ext4fs_update_parent_dentry(char *filename, int file_type)
474 unsigned int *zero_buffer = NULL;
475 char *root_first_block_buffer = NULL;
477 long int first_block_no_of_root = 0;
479 unsigned int new_entry_byte_reqd;
480 int sizeof_void_space = 0;
484 struct ext_filesystem *fs = get_fs();
485 /* directory entry */
486 struct ext2_dirent *dir;
487 char *temp_dir = NULL;
490 uint32_t new_blockcnt;
491 uint32_t directory_blocks;
493 zero_buffer = zalloc(fs->blksz);
495 printf("No Memory\n");
498 root_first_block_buffer = zalloc(fs->blksz);
499 if (!root_first_block_buffer) {
501 printf("No Memory\n");
504 new_entry_byte_reqd = ROUND(strlen(filename) +
505 sizeof(struct ext2_dirent), 4);
507 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
508 LOG2_BLOCK_SIZE(ext4fs_root);
509 blk_idx = directory_blocks - 1;
512 /* read the block no allocated to a file */
513 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
515 if (first_block_no_of_root <= 0)
518 status = ext4fs_devread((lbaint_t)first_block_no_of_root
520 0, fs->blksz, root_first_block_buffer);
524 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
526 dir = (struct ext2_dirent *)root_first_block_buffer;
529 while (le16_to_cpu(dir->direntlen) > 0) {
530 unsigned short used_len = ROUND(dir->namelen +
531 sizeof(struct ext2_dirent), 4);
533 /* last entry of block */
534 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
536 /* check if new entry fits */
537 if ((used_len + new_entry_byte_reqd) <=
538 le16_to_cpu(dir->direntlen)) {
539 dir->direntlen = cpu_to_le16(used_len);
543 printf("Block full, trying previous\n");
547 printf("All blocks full: Allocate new\n");
549 if (le32_to_cpu(g_parent_inode->flags) &
551 printf("Directory uses extents\n");
554 if (directory_blocks >= INDIRECT_BLOCKS) {
555 printf("Directory exceeds limit\n");
558 new_blk_no = ext4fs_get_new_blk_no();
559 if (new_blk_no == -1) {
560 printf("no block left to assign\n");
563 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
564 g_parent_inode->b.blocks.
565 dir_blocks[directory_blocks] =
566 cpu_to_le32(new_blk_no);
568 new_size = le32_to_cpu(g_parent_inode->size);
569 new_size += fs->blksz;
570 g_parent_inode->size = cpu_to_le32(new_size);
572 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
573 new_blockcnt += fs->sect_perblk;
574 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
576 if (ext4fs_put_metadata
577 (root_first_block_buffer,
578 first_block_no_of_root))
584 templength = le16_to_cpu(dir->direntlen);
585 totalbytes = totalbytes + templength;
586 sizeof_void_space = check_void_in_dentry(dir, filename);
587 if (sizeof_void_space)
590 dir = (struct ext2_dirent *)((char *)dir + templength);
593 /* make a pointer ready for creating next directory entry */
594 templength = le16_to_cpu(dir->direntlen);
595 totalbytes = totalbytes + templength;
596 dir = (struct ext2_dirent *)((char *)dir + templength);
598 /* get the next available inode number */
599 inodeno = ext4fs_get_new_inode_no();
601 printf("no inode left to assign\n");
604 dir->inode = cpu_to_le32(inodeno);
605 if (sizeof_void_space)
606 dir->direntlen = cpu_to_le16(sizeof_void_space);
608 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
610 dir->namelen = strlen(filename);
611 dir->filetype = FILETYPE_REG; /* regular file */
612 temp_dir = (char *)dir;
613 temp_dir = temp_dir + sizeof(struct ext2_dirent);
614 memcpy(temp_dir, filename, strlen(filename));
616 /* update or write the 1st block of root inode */
617 if (ext4fs_put_metadata(root_first_block_buffer,
618 first_block_no_of_root))
623 free(root_first_block_buffer);
628 static int search_dir(struct ext2_inode *parent_inode, char *dirname)
635 char *block_buffer = NULL;
636 struct ext2_dirent *dir = NULL;
637 struct ext_filesystem *fs = get_fs();
638 uint32_t directory_blocks;
641 directory_blocks = le32_to_cpu(parent_inode->size) >>
642 LOG2_BLOCK_SIZE(ext4fs_root);
644 block_buffer = zalloc(fs->blksz);
648 /* get the block no allocated to a file */
649 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
650 blknr = read_allocated_block(parent_inode, blk_idx, NULL);
654 /* read the directory block */
655 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
656 0, fs->blksz, (char *)block_buffer);
663 printf("Badly aligned ext2_dirent\n");
667 dir = (struct ext2_dirent *)(block_buffer + offset);
668 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
670 int direntlen = le16_to_cpu(dir->direntlen);
671 if (direntlen < sizeof(struct ext2_dirent))
674 if (dir->inode && (strlen(dirname) == dir->namelen) &&
675 (strncmp(dirname, direntname, dir->namelen) == 0)) {
676 inodeno = le32_to_cpu(dir->inode);
682 } while (offset < fs->blksz);
696 static int find_dir_depth(char *dirname)
698 char *token = strtok(dirname, "/");
700 while (token != NULL) {
701 token = strtok(NULL, "/");
704 return count + 1 + 1;
706 * for example for string /home/temp
707 * depth=home(1)+temp(1)+1 extra for NULL;
712 static int parse_path(char **arr, char *dirname)
714 char *token = strtok(dirname, "/");
718 arr[i] = zalloc(strlen("/") + 1);
721 memcpy(arr[i++], "/", strlen("/"));
723 /* add each path entry after root */
724 while (token != NULL) {
725 arr[i] = zalloc(strlen(token) + 1);
728 memcpy(arr[i++], token, strlen(token));
729 token = strtok(NULL, "/");
736 int ext4fs_iget(int inode_no, struct ext2_inode *inode)
738 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
745 * Function: ext4fs_get_parent_inode_num
746 * Return Value: inode Number of the parent directory of file/Directory to be
748 * dirname : Input parmater, input path name of the file/directory to be created
749 * dname : Output parameter, to be filled with the name of the directory
750 * extracted from dirname
752 int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
756 int matched_inode_no;
757 int result_inode_no = -1;
759 char *depth_dirname = NULL;
760 char *parse_dirname = NULL;
761 struct ext2_inode *parent_inode = NULL;
762 struct ext2_inode *first_inode = NULL;
763 struct ext2_inode temp_inode;
765 if (*dirname != '/') {
766 printf("Please supply Absolute path\n");
770 /* TODO: input validation make equivalent to linux */
771 depth_dirname = zalloc(strlen(dirname) + 1);
775 memcpy(depth_dirname, dirname, strlen(dirname));
776 depth = find_dir_depth(depth_dirname);
777 parse_dirname = zalloc(strlen(dirname) + 1);
780 memcpy(parse_dirname, dirname, strlen(dirname));
782 /* allocate memory for each directory level */
783 ptr = zalloc((depth) * sizeof(char *));
786 if (parse_path(ptr, parse_dirname))
788 parent_inode = zalloc(sizeof(struct ext2_inode));
791 first_inode = zalloc(sizeof(struct ext2_inode));
794 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
795 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
797 result_inode_no = EXT2_ROOT_INO;
798 for (i = 1; i < depth; i++) {
799 matched_inode_no = search_dir(parent_inode, ptr[i]);
800 if (matched_inode_no == -1) {
801 if (ptr[i + 1] == NULL && i == 1) {
802 result_inode_no = EXT2_ROOT_INO;
805 if (ptr[i + 1] == NULL)
807 printf("Invalid path\n");
808 result_inode_no = -1;
812 if (ptr[i + 1] != NULL) {
813 memset(parent_inode, '\0',
814 sizeof(struct ext2_inode));
815 if (ext4fs_iget(matched_inode_no,
817 result_inode_no = -1;
820 result_inode_no = matched_inode_no;
829 matched_inode_no = search_dir(first_inode, ptr[i]);
831 matched_inode_no = search_dir(parent_inode, ptr[i]);
833 if (matched_inode_no != -1) {
834 ext4fs_iget(matched_inode_no, &temp_inode);
835 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
836 printf("It is a Directory\n");
837 result_inode_no = -1;
842 if (strlen(ptr[i]) > 256) {
843 result_inode_no = -1;
846 memcpy(dname, ptr[i], strlen(ptr[i]));
851 for (i = 0; i < depth; i++) {
860 return result_inode_no;
863 static int unlink_filename(char *filename, unsigned int blknr)
868 char *block_buffer = NULL;
869 struct ext2_dirent *dir = NULL;
870 struct ext2_dirent *previous_dir;
871 struct ext_filesystem *fs = get_fs();
875 block_buffer = zalloc(fs->blksz);
879 /* read the directory block */
880 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
881 fs->blksz, block_buffer);
888 printf("Badly aligned ext2_dirent\n");
893 dir = (struct ext2_dirent *)(block_buffer + offset);
894 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
896 int direntlen = le16_to_cpu(dir->direntlen);
897 if (direntlen < sizeof(struct ext2_dirent))
900 if (dir->inode && (strlen(filename) == dir->namelen) &&
901 (strncmp(direntname, filename, dir->namelen) == 0)) {
902 inodeno = le32_to_cpu(dir->inode);
908 } while (offset < fs->blksz);
911 printf("file found, deleting\n");
912 if (ext4fs_log_journal(block_buffer, blknr))
916 /* merge dir entry with predecessor */
918 new_len = le16_to_cpu(previous_dir->direntlen);
919 new_len += le16_to_cpu(dir->direntlen);
920 previous_dir->direntlen = cpu_to_le16(new_len);
922 /* invalidate dir entry */
925 if (ext4fs_put_metadata(block_buffer, blknr))
935 int ext4fs_filename_unlink(char *filename)
940 uint32_t directory_blocks;
942 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
943 LOG2_BLOCK_SIZE(ext4fs_root);
945 /* read the block no allocated to a file */
946 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
947 blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
950 inodeno = unlink_filename(filename, blknr);
958 uint32_t ext4fs_get_new_blk_no(void)
964 static int prev_bg_bitmap_index = -1;
965 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
966 struct ext_filesystem *fs = get_fs();
967 char *journal_buffer = zalloc(fs->blksz);
968 char *zero_buffer = zalloc(fs->blksz);
969 if (!journal_buffer || !zero_buffer)
972 if (fs->first_pass_bbmap == 0) {
973 for (i = 0; i < fs->no_blkgrp; i++) {
974 struct ext2_block_group *bgd = NULL;
975 bgd = ext4fs_get_group_descriptor(fs, i);
976 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
977 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
978 uint64_t b_bitmap_blk =
979 ext4fs_bg_get_block_id(bgd, fs);
980 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
981 memcpy(fs->blk_bmaps[i], zero_buffer,
983 put_ext4(b_bitmap_blk * fs->blksz,
984 fs->blk_bmaps[i], fs->blksz);
985 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
986 ext4fs_bg_set_flags(bgd, bg_flags);
989 _get_new_blk_no(fs->blk_bmaps[i]);
990 if (fs->curr_blkno == -1)
991 /* block bitmap is completely filled */
993 fs->curr_blkno = fs->curr_blkno +
995 fs->first_pass_bbmap++;
996 ext4fs_bg_free_blocks_dec(bgd, fs);
997 ext4fs_sb_free_blocks_dec(fs->sb);
998 status = ext4fs_devread(b_bitmap_blk *
1004 if (ext4fs_log_journal(journal_buffer,
1009 debug("no space left on block group %d\n", i);
1017 /* get the blockbitmap index respective to blockno */
1018 bg_idx = fs->curr_blkno / blk_per_grp;
1019 if (fs->blksz == 1024) {
1020 remainder = fs->curr_blkno % blk_per_grp;
1026 * To skip completely filled block group bitmaps
1027 * Optimize the block allocation
1029 if (bg_idx >= fs->no_blkgrp)
1032 struct ext2_block_group *bgd = NULL;
1033 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1034 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
1035 debug("block group %u is full. Skipping\n", bg_idx);
1036 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1037 if (fs->blksz == 1024)
1038 fs->curr_blkno += 1;
1042 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1043 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1044 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
1045 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1046 put_ext4(b_bitmap_blk * fs->blksz,
1047 zero_buffer, fs->blksz);
1048 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1049 ext4fs_bg_set_flags(bgd, bg_flags);
1052 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1054 debug("going for restart for the block no %ld %u\n",
1055 fs->curr_blkno, bg_idx);
1060 /* journal backup */
1061 if (prev_bg_bitmap_index != bg_idx) {
1062 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
1063 0, fs->blksz, journal_buffer);
1066 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
1069 prev_bg_bitmap_index = bg_idx;
1071 ext4fs_bg_free_blocks_dec(bgd, fs);
1072 ext4fs_sb_free_blocks_dec(fs->sb);
1076 free(journal_buffer);
1079 return fs->curr_blkno;
1081 free(journal_buffer);
1087 int ext4fs_get_new_inode_no(void)
1091 unsigned int ibmap_idx;
1092 static int prev_inode_bitmap_index = -1;
1093 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
1094 struct ext_filesystem *fs = get_fs();
1095 char *journal_buffer = zalloc(fs->blksz);
1096 char *zero_buffer = zalloc(fs->blksz);
1097 if (!journal_buffer || !zero_buffer)
1099 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1100 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
1102 if (fs->first_pass_ibmap == 0) {
1103 for (i = 0; i < fs->no_blkgrp; i++) {
1104 uint32_t free_inodes;
1105 struct ext2_block_group *bgd = NULL;
1106 bgd = ext4fs_get_group_descriptor(fs, i);
1107 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1109 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1110 uint64_t i_bitmap_blk =
1111 ext4fs_bg_get_inode_id(bgd, fs);
1113 bgd->bg_itable_unused = free_inodes;
1114 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1115 put_ext4(i_bitmap_blk * fs->blksz,
1116 zero_buffer, fs->blksz);
1117 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1118 ext4fs_bg_set_flags(bgd, bg_flags);
1119 memcpy(fs->inode_bmaps[i],
1120 zero_buffer, fs->blksz);
1123 _get_new_inode_no(fs->inode_bmaps[i]);
1124 if (fs->curr_inode_no == -1)
1125 /* inode bitmap is completely filled */
1127 fs->curr_inode_no = fs->curr_inode_no +
1128 (i * inodes_per_grp);
1129 fs->first_pass_ibmap++;
1130 ext4fs_bg_free_inodes_dec(bgd, fs);
1132 ext4fs_bg_itable_unused_dec(bgd, fs);
1133 ext4fs_sb_free_inodes_dec(fs->sb);
1134 status = ext4fs_devread(i_bitmap_blk *
1140 if (ext4fs_log_journal(journal_buffer,
1145 debug("no inode left on block group %d\n", i);
1150 fs->curr_inode_no++;
1151 /* get the blockbitmap index respective to blockno */
1152 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1153 struct ext2_block_group *bgd =
1154 ext4fs_get_group_descriptor(fs, ibmap_idx);
1155 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1156 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1158 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1159 put_ext4(i_bitmap_blk * fs->blksz,
1160 zero_buffer, fs->blksz);
1161 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1162 ext4fs_bg_set_flags(bgd, bg_flags);
1163 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1167 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1168 fs->inode_bmaps[ibmap_idx],
1170 debug("going for restart for the block no %d %u\n",
1171 fs->curr_inode_no, ibmap_idx);
1175 /* journal backup */
1176 if (prev_inode_bitmap_index != ibmap_idx) {
1177 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
1178 0, fs->blksz, journal_buffer);
1181 if (ext4fs_log_journal(journal_buffer,
1182 le32_to_cpu(bgd->inode_id)))
1184 prev_inode_bitmap_index = ibmap_idx;
1186 ext4fs_bg_free_inodes_dec(bgd, fs);
1188 bgd->bg_itable_unused = bgd->free_inodes;
1189 ext4fs_sb_free_inodes_dec(fs->sb);
1194 free(journal_buffer);
1197 return fs->curr_inode_no;
1199 free(journal_buffer);
1207 static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1208 unsigned int *total_remaining_blocks,
1209 unsigned int *no_blks_reqd)
1213 long int actual_block_no;
1214 long int si_blockno;
1215 /* si :single indirect */
1216 __le32 *si_buffer = NULL;
1217 __le32 *si_start_addr = NULL;
1218 struct ext_filesystem *fs = get_fs();
1220 if (*total_remaining_blocks != 0) {
1221 si_buffer = zalloc(fs->blksz);
1223 printf("No Memory\n");
1226 si_start_addr = si_buffer;
1227 si_blockno = ext4fs_get_new_blk_no();
1228 if (si_blockno == -1) {
1229 printf("no block left to assign\n");
1233 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1235 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
1236 0, fs->blksz, (char *)si_buffer);
1237 memset(si_buffer, '\0', fs->blksz);
1241 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1242 actual_block_no = ext4fs_get_new_blk_no();
1243 if (actual_block_no == -1) {
1244 printf("no block left to assign\n");
1247 *si_buffer = cpu_to_le32(actual_block_no);
1248 debug("SIAB %u: %u\n", *si_buffer,
1249 *total_remaining_blocks);
1252 (*total_remaining_blocks)--;
1253 if (*total_remaining_blocks == 0)
1257 /* write the block to disk */
1258 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
1259 si_start_addr, fs->blksz);
1260 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
1263 free(si_start_addr);
1266 static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1267 unsigned int *total_remaining_blocks,
1268 unsigned int *no_blks_reqd)
1273 long int actual_block_no;
1274 /* di:double indirect */
1275 long int di_blockno_parent;
1276 long int di_blockno_child;
1277 __le32 *di_parent_buffer = NULL;
1278 __le32 *di_child_buff = NULL;
1279 __le32 *di_block_start_addr = NULL;
1280 __le32 *di_child_buff_start = NULL;
1281 struct ext_filesystem *fs = get_fs();
1283 if (*total_remaining_blocks != 0) {
1284 /* double indirect parent block connecting to inode */
1285 di_blockno_parent = ext4fs_get_new_blk_no();
1286 if (di_blockno_parent == -1) {
1287 printf("no block left to assign\n");
1290 di_parent_buffer = zalloc(fs->blksz);
1291 if (!di_parent_buffer)
1294 di_block_start_addr = di_parent_buffer;
1296 debug("DIPB %ld: %u\n", di_blockno_parent,
1297 *total_remaining_blocks);
1299 status = ext4fs_devread((lbaint_t)di_blockno_parent *
1301 fs->blksz, (char *)di_parent_buffer);
1304 printf("%s: Device read error!\n", __func__);
1307 memset(di_parent_buffer, '\0', fs->blksz);
1310 * start:for each double indirect parent
1311 * block create one more block
1313 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1314 di_blockno_child = ext4fs_get_new_blk_no();
1315 if (di_blockno_child == -1) {
1316 printf("no block left to assign\n");
1319 di_child_buff = zalloc(fs->blksz);
1323 di_child_buff_start = di_child_buff;
1324 *di_parent_buffer = cpu_to_le32(di_blockno_child);
1327 debug("DICB %ld: %u\n", di_blockno_child,
1328 *total_remaining_blocks);
1330 status = ext4fs_devread((lbaint_t)di_blockno_child *
1333 (char *)di_child_buff);
1336 printf("%s: Device read error!\n", __func__);
1339 memset(di_child_buff, '\0', fs->blksz);
1340 /* filling of actual datablocks for each child */
1341 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1342 actual_block_no = ext4fs_get_new_blk_no();
1343 if (actual_block_no == -1) {
1344 printf("no block left to assign\n");
1347 *di_child_buff = cpu_to_le32(actual_block_no);
1348 debug("DIAB %ld: %u\n", actual_block_no,
1349 *total_remaining_blocks);
1352 (*total_remaining_blocks)--;
1353 if (*total_remaining_blocks == 0)
1356 /* write the block table */
1357 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
1358 di_child_buff_start, fs->blksz);
1359 free(di_child_buff_start);
1360 di_child_buff_start = NULL;
1362 if (*total_remaining_blocks == 0)
1365 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
1366 di_block_start_addr, fs->blksz);
1367 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
1370 free(di_block_start_addr);
1373 static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1374 unsigned int *total_remaining_blocks,
1375 unsigned int *no_blks_reqd)
1380 long int actual_block_no;
1381 /* ti: Triple Indirect */
1382 long int ti_gp_blockno;
1383 long int ti_parent_blockno;
1384 long int ti_child_blockno;
1385 __le32 *ti_gp_buff = NULL;
1386 __le32 *ti_parent_buff = NULL;
1387 __le32 *ti_child_buff = NULL;
1388 __le32 *ti_gp_buff_start_addr = NULL;
1389 __le32 *ti_pbuff_start_addr = NULL;
1390 __le32 *ti_cbuff_start_addr = NULL;
1391 struct ext_filesystem *fs = get_fs();
1392 if (*total_remaining_blocks != 0) {
1393 /* triple indirect grand parent block connecting to inode */
1394 ti_gp_blockno = ext4fs_get_new_blk_no();
1395 if (ti_gp_blockno == -1) {
1396 printf("no block left to assign\n");
1399 ti_gp_buff = zalloc(fs->blksz);
1403 ti_gp_buff_start_addr = ti_gp_buff;
1405 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1406 *total_remaining_blocks);
1408 /* for each 4 byte grand parent entry create one more block */
1409 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1410 ti_parent_blockno = ext4fs_get_new_blk_no();
1411 if (ti_parent_blockno == -1) {
1412 printf("no block left to assign\n");
1415 ti_parent_buff = zalloc(fs->blksz);
1416 if (!ti_parent_buff)
1419 ti_pbuff_start_addr = ti_parent_buff;
1420 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
1423 debug("TIPB %ld: %u\n", ti_parent_blockno,
1424 *total_remaining_blocks);
1426 /* for each 4 byte entry parent create one more block */
1427 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1428 ti_child_blockno = ext4fs_get_new_blk_no();
1429 if (ti_child_blockno == -1) {
1430 printf("no block left assign\n");
1433 ti_child_buff = zalloc(fs->blksz);
1437 ti_cbuff_start_addr = ti_child_buff;
1438 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
1441 debug("TICB %ld: %u\n", ti_parent_blockno,
1442 *total_remaining_blocks);
1444 /* fill actual datablocks for each child */
1445 for (k = 0; k < (fs->blksz / sizeof(int));
1448 ext4fs_get_new_blk_no();
1449 if (actual_block_no == -1) {
1450 printf("no block left\n");
1451 free(ti_cbuff_start_addr);
1454 *ti_child_buff = cpu_to_le32(actual_block_no);
1455 debug("TIAB %ld: %u\n", actual_block_no,
1456 *total_remaining_blocks);
1459 (*total_remaining_blocks)--;
1460 if (*total_remaining_blocks == 0)
1463 /* write the child block */
1464 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1465 (uint64_t)fs->blksz)),
1466 ti_cbuff_start_addr, fs->blksz);
1467 free(ti_cbuff_start_addr);
1469 if (*total_remaining_blocks == 0)
1472 /* write the parent block */
1473 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
1474 ti_pbuff_start_addr, fs->blksz);
1475 free(ti_pbuff_start_addr);
1477 if (*total_remaining_blocks == 0)
1480 /* write the grand parent block */
1481 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
1482 ti_gp_buff_start_addr, fs->blksz);
1483 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1484 free(ti_gp_buff_start_addr);
1488 free(ti_pbuff_start_addr);
1490 free(ti_gp_buff_start_addr);
1493 void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1494 unsigned int total_remaining_blocks,
1495 unsigned int *total_no_of_block)
1498 long int direct_blockno;
1499 unsigned int no_blks_reqd = 0;
1501 /* allocation of direct blocks */
1502 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1503 direct_blockno = ext4fs_get_new_blk_no();
1504 if (direct_blockno == -1) {
1505 printf("no block left to assign\n");
1508 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
1509 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1511 total_remaining_blocks--;
1514 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1516 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1518 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1520 *total_no_of_block += no_blks_reqd;
1525 static struct ext4_extent_header *ext4fs_get_extent_block
1526 (struct ext2_data *data, struct ext_block_cache *cache,
1527 struct ext4_extent_header *ext_block,
1528 uint32_t fileblock, int log2_blksz)
1530 struct ext4_extent_idx *index;
1531 unsigned long long block;
1532 int blksz = EXT2_BLOCK_SIZE(data);
1536 index = (struct ext4_extent_idx *)(ext_block + 1);
1538 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1541 if (ext_block->eh_depth == 0)
1546 if (i >= le16_to_cpu(ext_block->eh_entries))
1548 } while (fileblock >= le32_to_cpu(index[i].ei_block));
1553 block = le16_to_cpu(index[i].ei_leaf_hi);
1554 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1555 block <<= log2_blksz;
1556 if (!ext_cache_read(cache, (lbaint_t)block, blksz))
1558 ext_block = (struct ext4_extent_header *)cache->buf;
1562 static int ext4fs_blockgroup
1563 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1566 unsigned int blkoff, desc_per_blk;
1567 int log2blksz = get_fs()->dev_desc->log2blksz;
1568 int desc_size = get_fs()->gdsize;
1570 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
1572 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
1573 group / desc_per_blk;
1574 blkoff = (group % desc_per_blk) * desc_size;
1576 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1577 group, blkno, blkoff);
1579 return ext4fs_devread((lbaint_t)blkno <<
1580 (LOG2_BLOCK_SIZE(data) - log2blksz),
1581 blkoff, desc_size, (char *)blkgrp);
1584 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1586 struct ext2_block_group blkgrp;
1587 struct ext2_sblock *sblock = &data->sblock;
1588 struct ext_filesystem *fs = get_fs();
1589 int log2blksz = get_fs()->dev_desc->log2blksz;
1590 int inodes_per_block, status;
1592 unsigned int blkoff;
1594 /* It is easier to calculate if the first inode is 0. */
1596 status = ext4fs_blockgroup(data, ino / le32_to_cpu
1597 (sblock->inodes_per_group), &blkgrp);
1601 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1602 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
1603 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1604 blkoff = (ino % inodes_per_block) * fs->inodesz;
1605 /* Read the inode. */
1606 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1608 sizeof(struct ext2_inode), (char *)inode);
1615 long int read_allocated_block(struct ext2_inode *inode, int fileblock,
1616 struct ext_block_cache *cache)
1623 long int perblock_parent;
1624 long int perblock_child;
1625 unsigned long long start;
1626 /* get the blocksize of the filesystem */
1627 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1628 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1629 - get_fs()->dev_desc->log2blksz;
1631 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1632 long int startblock, endblock;
1633 struct ext_block_cache *c, cd;
1634 struct ext4_extent_header *ext_block;
1635 struct ext4_extent *extent;
1645 ext4fs_get_extent_block(ext4fs_root, c,
1646 (struct ext4_extent_header *)
1647 inode->b.blocks.dir_blocks,
1648 fileblock, log2_blksz);
1650 printf("invalid extent block\n");
1656 extent = (struct ext4_extent *)(ext_block + 1);
1658 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1659 startblock = le32_to_cpu(extent[i].ee_block);
1660 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1662 if (startblock > fileblock) {
1668 } else if (fileblock < endblock) {
1669 start = le16_to_cpu(extent[i].ee_start_hi);
1670 start = (start << 32) +
1671 le32_to_cpu(extent[i].ee_start_lo);
1674 return (fileblock - startblock) + start;
1683 /* Direct blocks. */
1684 if (fileblock < INDIRECT_BLOCKS)
1685 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1688 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1689 if (ext4fs_indir1_block == NULL) {
1690 ext4fs_indir1_block = zalloc(blksz);
1691 if (ext4fs_indir1_block == NULL) {
1692 printf("** SI ext2fs read block (indir 1)"
1693 "malloc failed. **\n");
1696 ext4fs_indir1_size = blksz;
1697 ext4fs_indir1_blkno = -1;
1699 if (blksz != ext4fs_indir1_size) {
1700 free(ext4fs_indir1_block);
1701 ext4fs_indir1_block = NULL;
1702 ext4fs_indir1_size = 0;
1703 ext4fs_indir1_blkno = -1;
1704 ext4fs_indir1_block = zalloc(blksz);
1705 if (ext4fs_indir1_block == NULL) {
1706 printf("** SI ext2fs read block (indir 1):"
1707 "malloc failed. **\n");
1710 ext4fs_indir1_size = blksz;
1712 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
1713 log2_blksz) != ext4fs_indir1_blkno) {
1715 ext4fs_devread((lbaint_t)le32_to_cpu
1717 indir_block) << log2_blksz, 0,
1718 blksz, (char *)ext4fs_indir1_block);
1720 printf("** SI ext2fs read block (indir 1)"
1724 ext4fs_indir1_blkno =
1725 le32_to_cpu(inode->b.blocks.
1726 indir_block) << log2_blksz;
1728 blknr = le32_to_cpu(ext4fs_indir1_block
1729 [fileblock - INDIRECT_BLOCKS]);
1731 /* Double indirect. */
1732 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1733 (blksz / 4 + 1)))) {
1735 long int perblock = blksz / 4;
1736 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1738 if (ext4fs_indir1_block == NULL) {
1739 ext4fs_indir1_block = zalloc(blksz);
1740 if (ext4fs_indir1_block == NULL) {
1741 printf("** DI ext2fs read block (indir 2 1)"
1742 "malloc failed. **\n");
1745 ext4fs_indir1_size = blksz;
1746 ext4fs_indir1_blkno = -1;
1748 if (blksz != ext4fs_indir1_size) {
1749 free(ext4fs_indir1_block);
1750 ext4fs_indir1_block = NULL;
1751 ext4fs_indir1_size = 0;
1752 ext4fs_indir1_blkno = -1;
1753 ext4fs_indir1_block = zalloc(blksz);
1754 if (ext4fs_indir1_block == NULL) {
1755 printf("** DI ext2fs read block (indir 2 1)"
1756 "malloc failed. **\n");
1759 ext4fs_indir1_size = blksz;
1761 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
1762 log2_blksz) != ext4fs_indir1_blkno) {
1764 ext4fs_devread((lbaint_t)le32_to_cpu
1766 double_indir_block) << log2_blksz,
1768 (char *)ext4fs_indir1_block);
1770 printf("** DI ext2fs read block (indir 2 1)"
1774 ext4fs_indir1_blkno =
1775 le32_to_cpu(inode->b.blocks.double_indir_block) <<
1779 if (ext4fs_indir2_block == NULL) {
1780 ext4fs_indir2_block = zalloc(blksz);
1781 if (ext4fs_indir2_block == NULL) {
1782 printf("** DI ext2fs read block (indir 2 2)"
1783 "malloc failed. **\n");
1786 ext4fs_indir2_size = blksz;
1787 ext4fs_indir2_blkno = -1;
1789 if (blksz != ext4fs_indir2_size) {
1790 free(ext4fs_indir2_block);
1791 ext4fs_indir2_block = NULL;
1792 ext4fs_indir2_size = 0;
1793 ext4fs_indir2_blkno = -1;
1794 ext4fs_indir2_block = zalloc(blksz);
1795 if (ext4fs_indir2_block == NULL) {
1796 printf("** DI ext2fs read block (indir 2 2)"
1797 "malloc failed. **\n");
1800 ext4fs_indir2_size = blksz;
1802 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1803 log2_blksz) != ext4fs_indir2_blkno) {
1804 status = ext4fs_devread((lbaint_t)le32_to_cpu
1805 (ext4fs_indir1_block
1807 perblock]) << log2_blksz, 0,
1809 (char *)ext4fs_indir2_block);
1811 printf("** DI ext2fs read block (indir 2 2)"
1815 ext4fs_indir2_blkno =
1816 le32_to_cpu(ext4fs_indir1_block[rblock
1821 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1823 /* Tripple indirect. */
1825 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1826 (blksz / 4 * blksz / 4));
1827 perblock_child = blksz / 4;
1828 perblock_parent = ((blksz / 4) * (blksz / 4));
1830 if (ext4fs_indir1_block == NULL) {
1831 ext4fs_indir1_block = zalloc(blksz);
1832 if (ext4fs_indir1_block == NULL) {
1833 printf("** TI ext2fs read block (indir 2 1)"
1834 "malloc failed. **\n");
1837 ext4fs_indir1_size = blksz;
1838 ext4fs_indir1_blkno = -1;
1840 if (blksz != ext4fs_indir1_size) {
1841 free(ext4fs_indir1_block);
1842 ext4fs_indir1_block = NULL;
1843 ext4fs_indir1_size = 0;
1844 ext4fs_indir1_blkno = -1;
1845 ext4fs_indir1_block = zalloc(blksz);
1846 if (ext4fs_indir1_block == NULL) {
1847 printf("** TI ext2fs read block (indir 2 1)"
1848 "malloc failed. **\n");
1851 ext4fs_indir1_size = blksz;
1853 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1854 log2_blksz) != ext4fs_indir1_blkno) {
1855 status = ext4fs_devread
1857 le32_to_cpu(inode->b.blocks.triple_indir_block)
1858 << log2_blksz, 0, blksz,
1859 (char *)ext4fs_indir1_block);
1861 printf("** TI ext2fs read block (indir 2 1)"
1865 ext4fs_indir1_blkno =
1866 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1870 if (ext4fs_indir2_block == NULL) {
1871 ext4fs_indir2_block = zalloc(blksz);
1872 if (ext4fs_indir2_block == NULL) {
1873 printf("** TI ext2fs read block (indir 2 2)"
1874 "malloc failed. **\n");
1877 ext4fs_indir2_size = blksz;
1878 ext4fs_indir2_blkno = -1;
1880 if (blksz != ext4fs_indir2_size) {
1881 free(ext4fs_indir2_block);
1882 ext4fs_indir2_block = NULL;
1883 ext4fs_indir2_size = 0;
1884 ext4fs_indir2_blkno = -1;
1885 ext4fs_indir2_block = zalloc(blksz);
1886 if (ext4fs_indir2_block == NULL) {
1887 printf("** TI ext2fs read block (indir 2 2)"
1888 "malloc failed. **\n");
1891 ext4fs_indir2_size = blksz;
1893 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
1894 perblock_parent]) <<
1896 != ext4fs_indir2_blkno) {
1897 status = ext4fs_devread((lbaint_t)le32_to_cpu
1898 (ext4fs_indir1_block
1900 perblock_parent]) <<
1901 log2_blksz, 0, blksz,
1902 (char *)ext4fs_indir2_block);
1904 printf("** TI ext2fs read block (indir 2 2)"
1908 ext4fs_indir2_blkno =
1909 le32_to_cpu(ext4fs_indir1_block[rblock /
1914 if (ext4fs_indir3_block == NULL) {
1915 ext4fs_indir3_block = zalloc(blksz);
1916 if (ext4fs_indir3_block == NULL) {
1917 printf("** TI ext2fs read block (indir 2 2)"
1918 "malloc failed. **\n");
1921 ext4fs_indir3_size = blksz;
1922 ext4fs_indir3_blkno = -1;
1924 if (blksz != ext4fs_indir3_size) {
1925 free(ext4fs_indir3_block);
1926 ext4fs_indir3_block = NULL;
1927 ext4fs_indir3_size = 0;
1928 ext4fs_indir3_blkno = -1;
1929 ext4fs_indir3_block = zalloc(blksz);
1930 if (ext4fs_indir3_block == NULL) {
1931 printf("** TI ext2fs read block (indir 2 2)"
1932 "malloc failed. **\n");
1935 ext4fs_indir3_size = blksz;
1937 if ((le32_to_cpu(ext4fs_indir2_block[rblock
1940 log2_blksz) != ext4fs_indir3_blkno) {
1942 ext4fs_devread((lbaint_t)le32_to_cpu
1943 (ext4fs_indir2_block
1944 [(rblock / perblock_child)
1945 % (blksz / 4)]) << log2_blksz, 0,
1946 blksz, (char *)ext4fs_indir3_block);
1948 printf("** TI ext2fs read block (indir 2 2)"
1952 ext4fs_indir3_blkno =
1953 le32_to_cpu(ext4fs_indir2_block[(rblock /
1960 blknr = le32_to_cpu(ext4fs_indir3_block
1961 [rblock % perblock_child]);
1963 debug("read_allocated_block %ld\n", blknr);
1969 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1972 * This function assures that for a file with the same name but different size
1973 * the sequential store on the ext4 filesystem will be correct.
1975 * In this function the global data, responsible for internal representation
1976 * of the ext4 data are initialized to the reset state. Without this, during
1977 * replacement of the smaller file with the bigger truncation of new file was
1980 void ext4fs_reinit_global(void)
1982 if (ext4fs_indir1_block != NULL) {
1983 free(ext4fs_indir1_block);
1984 ext4fs_indir1_block = NULL;
1985 ext4fs_indir1_size = 0;
1986 ext4fs_indir1_blkno = -1;
1988 if (ext4fs_indir2_block != NULL) {
1989 free(ext4fs_indir2_block);
1990 ext4fs_indir2_block = NULL;
1991 ext4fs_indir2_size = 0;
1992 ext4fs_indir2_blkno = -1;
1994 if (ext4fs_indir3_block != NULL) {
1995 free(ext4fs_indir3_block);
1996 ext4fs_indir3_block = NULL;
1997 ext4fs_indir3_size = 0;
1998 ext4fs_indir3_blkno = -1;
2001 void ext4fs_close(void)
2003 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
2004 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
2007 if (ext4fs_root != NULL) {
2012 ext4fs_reinit_global();
2015 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2016 struct ext2fs_node **fnode, int *ftype)
2018 unsigned int fpos = 0;
2021 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2025 printf("Iterate dir %s\n", name);
2026 #endif /* of DEBUG */
2027 if (!diro->inode_read) {
2028 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2032 /* Search the file. */
2033 while (fpos < le32_to_cpu(diro->inode.size)) {
2034 struct ext2_dirent dirent;
2036 status = ext4fs_read_file(diro, fpos,
2037 sizeof(struct ext2_dirent),
2038 (char *)&dirent, &actread);
2042 if (dirent.direntlen == 0) {
2043 printf("Failed to iterate over directory %s\n", name);
2047 if (dirent.namelen != 0) {
2048 char filename[dirent.namelen + 1];
2049 struct ext2fs_node *fdiro;
2050 int type = FILETYPE_UNKNOWN;
2052 status = ext4fs_read_file(diro,
2054 sizeof(struct ext2_dirent),
2055 dirent.namelen, filename,
2060 fdiro = zalloc(sizeof(struct ext2fs_node));
2064 fdiro->data = diro->data;
2065 fdiro->ino = le32_to_cpu(dirent.inode);
2067 filename[dirent.namelen] = '\0';
2069 if (dirent.filetype != FILETYPE_UNKNOWN) {
2070 fdiro->inode_read = 0;
2072 if (dirent.filetype == FILETYPE_DIRECTORY)
2073 type = FILETYPE_DIRECTORY;
2074 else if (dirent.filetype == FILETYPE_SYMLINK)
2075 type = FILETYPE_SYMLINK;
2076 else if (dirent.filetype == FILETYPE_REG)
2077 type = FILETYPE_REG;
2079 status = ext4fs_read_inode(diro->data,
2087 fdiro->inode_read = 1;
2089 if ((le16_to_cpu(fdiro->inode.mode) &
2090 FILETYPE_INO_MASK) ==
2091 FILETYPE_INO_DIRECTORY) {
2092 type = FILETYPE_DIRECTORY;
2093 } else if ((le16_to_cpu(fdiro->inode.mode)
2094 & FILETYPE_INO_MASK) ==
2095 FILETYPE_INO_SYMLINK) {
2096 type = FILETYPE_SYMLINK;
2097 } else if ((le16_to_cpu(fdiro->inode.mode)
2098 & FILETYPE_INO_MASK) ==
2100 type = FILETYPE_REG;
2104 printf("iterate >%s<\n", filename);
2105 #endif /* of DEBUG */
2106 if ((name != NULL) && (fnode != NULL)
2107 && (ftype != NULL)) {
2108 if (strcmp(filename, name) == 0) {
2114 if (fdiro->inode_read == 0) {
2115 status = ext4fs_read_inode(diro->data,
2123 fdiro->inode_read = 1;
2126 case FILETYPE_DIRECTORY:
2129 case FILETYPE_SYMLINK:
2140 le32_to_cpu(fdiro->inode.size),
2145 fpos += le16_to_cpu(dirent.direntlen);
2150 static char *ext4fs_read_symlink(struct ext2fs_node *node)
2153 struct ext2fs_node *diro = node;
2157 if (!diro->inode_read) {
2158 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2162 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
2166 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
2167 strncpy(symlink, diro->inode.b.symlink,
2168 le32_to_cpu(diro->inode.size));
2170 status = ext4fs_read_file(diro, 0,
2171 le32_to_cpu(diro->inode.size),
2173 if ((status < 0) || (actread == 0)) {
2178 symlink[le32_to_cpu(diro->inode.size)] = '\0';
2182 static int ext4fs_find_file1(const char *currpath,
2183 struct ext2fs_node *currroot,
2184 struct ext2fs_node **currfound, int *foundtype)
2186 char fpath[strlen(currpath) + 1];
2190 int type = FILETYPE_DIRECTORY;
2191 struct ext2fs_node *currnode = currroot;
2192 struct ext2fs_node *oldnode = currroot;
2194 strncpy(fpath, currpath, strlen(currpath) + 1);
2196 /* Remove all leading slashes. */
2197 while (*name == '/')
2201 *currfound = currnode;
2208 /* Extract the actual part from the pathname. */
2209 next = strchr(name, '/');
2211 /* Remove all leading slashes. */
2212 while (*next == '/')
2216 if (type != FILETYPE_DIRECTORY) {
2217 ext4fs_free_node(currnode, currroot);
2223 /* Iterate over the directory. */
2224 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2231 /* Read in the symlink and follow it. */
2232 if (type == FILETYPE_SYMLINK) {
2235 /* Test if the symlink does not loop. */
2236 if (++symlinknest == 8) {
2237 ext4fs_free_node(currnode, currroot);
2238 ext4fs_free_node(oldnode, currroot);
2242 symlink = ext4fs_read_symlink(currnode);
2243 ext4fs_free_node(currnode, currroot);
2246 ext4fs_free_node(oldnode, currroot);
2250 debug("Got symlink >%s<\n", symlink);
2252 if (symlink[0] == '/') {
2253 ext4fs_free_node(oldnode, currroot);
2254 oldnode = &ext4fs_root->diropen;
2257 /* Lookup the node the symlink points to. */
2258 status = ext4fs_find_file1(symlink, oldnode,
2264 ext4fs_free_node(oldnode, currroot);
2269 ext4fs_free_node(oldnode, currroot);
2271 /* Found the node! */
2272 if (!next || *next == '\0') {
2273 *currfound = currnode;
2282 int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2283 struct ext2fs_node **foundnode, int expecttype)
2286 int foundtype = FILETYPE_DIRECTORY;
2292 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2296 /* Check if the node that was found was of the expected type. */
2297 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2299 else if ((expecttype == FILETYPE_DIRECTORY)
2300 && (foundtype != expecttype))
2306 int ext4fs_open(const char *filename, loff_t *len)
2308 struct ext2fs_node *fdiro = NULL;
2311 if (ext4fs_root == NULL)
2315 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2320 if (!fdiro->inode_read) {
2321 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2326 *len = le32_to_cpu(fdiro->inode.size);
2327 ext4fs_file = fdiro;
2331 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2336 int ext4fs_mount(unsigned part_length)
2338 struct ext2_data *data;
2340 struct ext_filesystem *fs = get_fs();
2341 data = zalloc(SUPERBLOCK_SIZE);
2345 /* Read the superblock. */
2346 status = ext4_read_superblock((char *)&data->sblock);
2351 /* Make sure this is an ext2 filesystem. */
2352 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2356 if (le32_to_cpu(data->sblock.revision_level) == 0) {
2360 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2361 __le32_to_cpu(data->sblock.feature_compatibility),
2362 __le32_to_cpu(data->sblock.feature_incompat),
2363 __le32_to_cpu(data->sblock.feature_ro_compat));
2365 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2366 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2367 EXT4_FEATURE_INCOMPAT_64BIT ?
2368 le16_to_cpu(data->sblock.descriptor_size) : 32;
2371 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2372 le32_to_cpu(data->sblock.revision_level),
2373 fs->inodesz, fs->gdsize);
2375 data->diropen.data = data;
2376 data->diropen.ino = 2;
2377 data->diropen.inode_read = 1;
2378 data->inode = &data->diropen.inode;
2380 status = ext4fs_read_inode(data, 2, data->inode);
2388 printf("Failed to mount ext2 filesystem...\n");