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 and load support in Uboot.
8 * Ext4 read optimization taken from Open-Moko
12 * esd gmbh <www.esd-electronics.com>
13 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
15 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
16 * GRUB -- GRand Unified Bootloader
17 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
19 * ext4write : Based on generic ext4 protocol.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <ext_common.h>
40 #include <linux/stat.h>
41 #include <linux/time.h>
42 #include <asm/byteorder.h>
43 #include "ext4_common.h"
45 int ext4fs_symlinknest;
46 block_dev_desc_t *ext4_dev_desc;
48 struct ext_filesystem *get_fs(void)
50 if (ext4_dev_desc == NULL || ext4_dev_desc->priv == NULL)
51 printf("Invalid Input Arguments %s\n", __func__);
53 return ext4_dev_desc->priv;
56 int init_fs(block_dev_desc_t *dev_desc)
58 struct ext_filesystem *fs;
59 if (dev_desc == NULL) {
60 printf("Invalid Input Arguments %s\n", __func__);
64 fs = zalloc(sizeof(struct ext_filesystem));
66 printf("malloc failed: %s\n", __func__);
70 fs->dev_desc = dev_desc;
76 void deinit_fs(block_dev_desc_t *dev_desc)
78 if (dev_desc == NULL) {
79 printf("Invalid Input Arguments %s\n", __func__);
83 dev_desc->priv = NULL;
86 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
88 if ((node != &ext4fs_root->diropen) && (node != currroot))
93 * Taken from openmoko-kernel mailing list: By Andy green
94 * Optimized read file API : collects and defers contiguous sector
95 * reads into one potentially more efficient larger sequential read action
97 int ext4fs_read_file(struct ext2fs_node *node, int pos,
98 unsigned int len, char *buf)
102 int log2blocksize = LOG2_EXT2_BLOCK_SIZE(node->data);
103 int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
104 unsigned int filesize = __le32_to_cpu(node->inode.size);
105 int previous_block_number = -1;
106 int delayed_start = 0;
107 int delayed_extent = 0;
108 int delayed_skipfirst = 0;
109 int delayed_next = 0;
110 char *delayed_buf = NULL;
113 /* Adjust len so it we can't read past the end of the file. */
117 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
119 for (i = pos / blocksize; i < blockcnt; i++) {
121 int blockoff = pos % blocksize;
122 int blockend = blocksize;
124 blknr = read_allocated_block(&(node->inode), i);
128 blknr = blknr << log2blocksize;
131 if (i == blockcnt - 1) {
132 blockend = (len + pos) % blocksize;
134 /* The last portion is exactly blocksize. */
136 blockend = blocksize;
140 if (i == pos / blocksize) {
141 skipfirst = blockoff;
142 blockend -= skipfirst;
147 if (previous_block_number != -1) {
148 if (delayed_next == blknr) {
149 delayed_extent += blockend;
150 delayed_next += blockend >> SECTOR_BITS;
152 status = ext4fs_devread(delayed_start,
158 previous_block_number = blknr;
159 delayed_start = blknr;
160 delayed_extent = blockend;
161 delayed_skipfirst = skipfirst;
163 delayed_next = blknr +
164 (blockend >> SECTOR_BITS);
167 previous_block_number = blknr;
168 delayed_start = blknr;
169 delayed_extent = blockend;
170 delayed_skipfirst = skipfirst;
172 delayed_next = blknr +
173 (blockend >> SECTOR_BITS);
176 if (previous_block_number != -1) {
178 status = ext4fs_devread(delayed_start,
184 previous_block_number = -1;
186 memset(buf, 0, blocksize - skipfirst);
188 buf += blocksize - skipfirst;
190 if (previous_block_number != -1) {
192 status = ext4fs_devread(delayed_start,
193 delayed_skipfirst, delayed_extent,
197 previous_block_number = -1;
203 int ext4fs_ls(const char *dirname)
205 struct ext2fs_node *dirnode;
211 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
214 printf("** Can not find directory. **\n");
218 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
219 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
224 int ext4fs_read(char *buf, unsigned len)
226 if (ext4fs_root == NULL || ext4fs_file == NULL)
229 return ext4fs_read_file(ext4fs_file, 0, len, buf);
232 #if defined(CONFIG_CMD_EXT4_WRITE)
233 static void ext4fs_update(void)
236 ext4fs_update_journal();
237 struct ext_filesystem *fs = get_fs();
239 /* update super block */
240 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
241 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
243 /* update block groups */
244 for (i = 0; i < fs->no_blkgrp; i++) {
245 fs->gd[i].bg_checksum = ext4fs_checksum_update(i);
246 put_ext4((uint64_t)(fs->gd[i].block_id * fs->blksz),
247 fs->blk_bmaps[i], fs->blksz);
250 /* update inode table groups */
251 for (i = 0; i < fs->no_blkgrp; i++) {
252 put_ext4((uint64_t) (fs->gd[i].inode_id * fs->blksz),
253 fs->inode_bmaps[i], fs->blksz);
256 /* update the block group descriptor table */
257 put_ext4((uint64_t)(fs->gdtable_blkno * fs->blksz),
258 (struct ext2_block_group *)fs->gdtable,
259 (fs->blksz * fs->no_blk_pergdt));
261 ext4fs_dump_metadata();
267 int ext4fs_get_bgdtable(void)
271 struct ext_filesystem *fs = get_fs();
272 grp_desc_size = sizeof(struct ext2_block_group);
273 fs->no_blk_pergdt = (fs->no_blkgrp * grp_desc_size) / fs->blksz;
274 if ((fs->no_blkgrp * grp_desc_size) % fs->blksz)
277 /* allocate memory for gdtable */
278 fs->gdtable = zalloc(fs->blksz * fs->no_blk_pergdt);
281 /* read the group descriptor table */
282 status = ext4fs_devread(fs->gdtable_blkno * fs->sect_perblk, 0,
283 fs->blksz * fs->no_blk_pergdt, fs->gdtable);
287 if (ext4fs_log_gdt(fs->gdtable)) {
288 printf("Error in ext4fs_log_gdt\n");
300 static void delete_single_indirect_block(struct ext2_inode *inode)
302 struct ext2_block_group *gd = NULL;
303 static int prev_bg_bmap_idx = -1;
308 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
309 struct ext_filesystem *fs = get_fs();
310 char *journal_buffer = zalloc(fs->blksz);
311 if (!journal_buffer) {
312 printf("No memory\n");
315 /* get block group descriptor table */
316 gd = (struct ext2_block_group *)fs->gdtable;
318 /* deleting the single indirect block associated with inode */
319 if (inode->b.blocks.indir_block != 0) {
320 debug("SIPB releasing %u\n", inode->b.blocks.indir_block);
321 blknr = inode->b.blocks.indir_block;
322 if (fs->blksz != 1024) {
323 bg_idx = blknr / blk_per_grp;
325 bg_idx = blknr / blk_per_grp;
326 remainder = blknr % blk_per_grp;
330 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
331 gd[bg_idx].free_blocks++;
332 fs->sb->free_blocks++;
334 if (prev_bg_bmap_idx != bg_idx) {
336 ext4fs_devread(gd[bg_idx].block_id *
337 fs->sect_perblk, 0, fs->blksz,
341 if (ext4fs_log_journal
342 (journal_buffer, gd[bg_idx].block_id))
344 prev_bg_bmap_idx = bg_idx;
348 free(journal_buffer);
351 static void delete_double_indirect_block(struct ext2_inode *inode)
355 static int prev_bg_bmap_idx = -1;
359 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
360 unsigned int *di_buffer = NULL;
361 unsigned int *DIB_start_addr = NULL;
362 struct ext2_block_group *gd = NULL;
363 struct ext_filesystem *fs = get_fs();
364 char *journal_buffer = zalloc(fs->blksz);
365 if (!journal_buffer) {
366 printf("No memory\n");
369 /* get the block group descriptor table */
370 gd = (struct ext2_block_group *)fs->gdtable;
372 if (inode->b.blocks.double_indir_block != 0) {
373 di_buffer = zalloc(fs->blksz);
375 printf("No memory\n");
378 DIB_start_addr = (unsigned int *)di_buffer;
379 blknr = inode->b.blocks.double_indir_block;
380 status = ext4fs_devread(blknr * fs->sect_perblk, 0, fs->blksz,
382 for (i = 0; i < fs->blksz / sizeof(int); i++) {
386 debug("DICB releasing %u\n", *di_buffer);
387 if (fs->blksz != 1024) {
388 bg_idx = (*di_buffer) / blk_per_grp;
390 bg_idx = (*di_buffer) / blk_per_grp;
391 remainder = (*di_buffer) % blk_per_grp;
395 ext4fs_reset_block_bmap(*di_buffer,
396 fs->blk_bmaps[bg_idx], bg_idx);
398 gd[bg_idx].free_blocks++;
399 fs->sb->free_blocks++;
401 if (prev_bg_bmap_idx != bg_idx) {
402 status = ext4fs_devread(gd[bg_idx].block_id
403 * fs->sect_perblk, 0,
409 if (ext4fs_log_journal(journal_buffer,
410 gd[bg_idx].block_id))
412 prev_bg_bmap_idx = bg_idx;
416 /* removing the parent double indirect block */
417 blknr = inode->b.blocks.double_indir_block;
418 if (fs->blksz != 1024) {
419 bg_idx = blknr / blk_per_grp;
421 bg_idx = blknr / blk_per_grp;
422 remainder = blknr % blk_per_grp;
426 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
427 gd[bg_idx].free_blocks++;
428 fs->sb->free_blocks++;
430 if (prev_bg_bmap_idx != bg_idx) {
431 memset(journal_buffer, '\0', fs->blksz);
432 status = ext4fs_devread(gd[bg_idx].block_id *
433 fs->sect_perblk, 0, fs->blksz,
438 if (ext4fs_log_journal(journal_buffer,
439 gd[bg_idx].block_id))
441 prev_bg_bmap_idx = bg_idx;
443 debug("DIPB releasing %ld\n", blknr);
446 free(DIB_start_addr);
447 free(journal_buffer);
450 static void delete_triple_indirect_block(struct ext2_inode *inode)
454 static int prev_bg_bmap_idx = -1;
458 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
459 unsigned int *tigp_buffer = NULL;
460 unsigned int *tib_start_addr = NULL;
461 unsigned int *tip_buffer = NULL;
462 unsigned int *tipb_start_addr = NULL;
463 struct ext2_block_group *gd = NULL;
464 struct ext_filesystem *fs = get_fs();
465 char *journal_buffer = zalloc(fs->blksz);
466 if (!journal_buffer) {
467 printf("No memory\n");
470 /* get block group descriptor table */
471 gd = (struct ext2_block_group *)fs->gdtable;
473 if (inode->b.blocks.triple_indir_block != 0) {
474 tigp_buffer = zalloc(fs->blksz);
476 printf("No memory\n");
479 tib_start_addr = (unsigned int *)tigp_buffer;
480 blknr = inode->b.blocks.triple_indir_block;
481 status = ext4fs_devread(blknr * fs->sect_perblk, 0, fs->blksz,
482 (char *)tigp_buffer);
483 for (i = 0; i < fs->blksz / sizeof(int); i++) {
484 if (*tigp_buffer == 0)
486 debug("tigp buffer releasing %u\n", *tigp_buffer);
488 tip_buffer = zalloc(fs->blksz);
491 tipb_start_addr = (unsigned int *)tip_buffer;
492 status = ext4fs_devread((*tigp_buffer) *
493 fs->sect_perblk, 0, fs->blksz,
495 for (j = 0; j < fs->blksz / sizeof(int); j++) {
496 if (*tip_buffer == 0)
498 if (fs->blksz != 1024) {
499 bg_idx = (*tip_buffer) / blk_per_grp;
501 bg_idx = (*tip_buffer) / blk_per_grp;
503 remainder = (*tip_buffer) % blk_per_grp;
508 ext4fs_reset_block_bmap(*tip_buffer,
509 fs->blk_bmaps[bg_idx],
513 gd[bg_idx].free_blocks++;
514 fs->sb->free_blocks++;
516 if (prev_bg_bmap_idx != bg_idx) {
518 ext4fs_devread(gd[bg_idx].block_id *
525 if (ext4fs_log_journal(journal_buffer,
529 prev_bg_bmap_idx = bg_idx;
532 free(tipb_start_addr);
533 tipb_start_addr = NULL;
536 * removing the grand parent blocks
537 * which is connected to inode
539 if (fs->blksz != 1024) {
540 bg_idx = (*tigp_buffer) / blk_per_grp;
542 bg_idx = (*tigp_buffer) / blk_per_grp;
544 remainder = (*tigp_buffer) % blk_per_grp;
548 ext4fs_reset_block_bmap(*tigp_buffer,
549 fs->blk_bmaps[bg_idx], bg_idx);
552 gd[bg_idx].free_blocks++;
553 fs->sb->free_blocks++;
555 if (prev_bg_bmap_idx != bg_idx) {
556 memset(journal_buffer, '\0', fs->blksz);
558 ext4fs_devread(gd[bg_idx].block_id *
560 fs->blksz, journal_buffer);
564 if (ext4fs_log_journal(journal_buffer,
565 gd[bg_idx].block_id))
567 prev_bg_bmap_idx = bg_idx;
571 /* removing the grand parent triple indirect block */
572 blknr = inode->b.blocks.triple_indir_block;
573 if (fs->blksz != 1024) {
574 bg_idx = blknr / blk_per_grp;
576 bg_idx = blknr / blk_per_grp;
577 remainder = blknr % blk_per_grp;
581 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
582 gd[bg_idx].free_blocks++;
583 fs->sb->free_blocks++;
585 if (prev_bg_bmap_idx != bg_idx) {
586 memset(journal_buffer, '\0', fs->blksz);
587 status = ext4fs_devread(gd[bg_idx].block_id *
588 fs->sect_perblk, 0, fs->blksz,
593 if (ext4fs_log_journal(journal_buffer,
594 gd[bg_idx].block_id))
596 prev_bg_bmap_idx = bg_idx;
598 debug("tigp buffer itself releasing %ld\n", blknr);
601 free(tib_start_addr);
602 free(tipb_start_addr);
603 free(journal_buffer);
606 static int ext4fs_delete_file(int inodeno)
608 struct ext2_inode inode;
615 char *read_buffer = NULL;
616 char *start_block_address = NULL;
617 unsigned int no_blocks;
619 static int prev_bg_bmap_idx = -1;
620 unsigned int inodes_per_block;
623 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
624 unsigned int inode_per_grp = ext4fs_root->sblock.inodes_per_group;
625 struct ext2_inode *inode_buffer = NULL;
626 struct ext2_block_group *gd = NULL;
627 struct ext_filesystem *fs = get_fs();
628 char *journal_buffer = zalloc(fs->blksz);
631 /* get the block group descriptor table */
632 gd = (struct ext2_block_group *)fs->gdtable;
633 status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
637 /* read the block no allocated to a file */
638 no_blocks = inode.size / fs->blksz;
639 if (inode.size % fs->blksz)
642 if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
643 struct ext2fs_node *node_inode =
644 zalloc(sizeof(struct ext2fs_node));
647 node_inode->data = ext4fs_root;
648 node_inode->ino = inodeno;
649 node_inode->inode_read = 0;
650 memcpy(&(node_inode->inode), &inode, sizeof(struct ext2_inode));
652 for (i = 0; i < no_blocks; i++) {
653 blknr = read_allocated_block(&(node_inode->inode), i);
654 if (fs->blksz != 1024) {
655 bg_idx = blknr / blk_per_grp;
657 bg_idx = blknr / blk_per_grp;
658 remainder = blknr % blk_per_grp;
662 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
664 debug("EXT4_EXTENTS Block releasing %ld: %d\n",
667 gd[bg_idx].free_blocks++;
668 fs->sb->free_blocks++;
671 if (prev_bg_bmap_idx != bg_idx) {
673 ext4fs_devread(gd[bg_idx].block_id *
675 fs->blksz, journal_buffer);
678 if (ext4fs_log_journal(journal_buffer,
679 gd[bg_idx].block_id))
681 prev_bg_bmap_idx = bg_idx;
690 delete_single_indirect_block(&inode);
691 delete_double_indirect_block(&inode);
692 delete_triple_indirect_block(&inode);
694 /* read the block no allocated to a file */
695 no_blocks = inode.size / fs->blksz;
696 if (inode.size % fs->blksz)
698 for (i = 0; i < no_blocks; i++) {
699 blknr = read_allocated_block(&inode, i);
700 if (fs->blksz != 1024) {
701 bg_idx = blknr / blk_per_grp;
703 bg_idx = blknr / blk_per_grp;
704 remainder = blknr % blk_per_grp;
708 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
710 debug("ActualB releasing %ld: %d\n", blknr, bg_idx);
712 gd[bg_idx].free_blocks++;
713 fs->sb->free_blocks++;
715 if (prev_bg_bmap_idx != bg_idx) {
716 memset(journal_buffer, '\0', fs->blksz);
717 status = ext4fs_devread(gd[bg_idx].block_id
723 if (ext4fs_log_journal(journal_buffer,
724 gd[bg_idx].block_id))
726 prev_bg_bmap_idx = bg_idx;
731 /* from the inode no to blockno */
732 inodes_per_block = fs->blksz / fs->inodesz;
733 ibmap_idx = inodeno / inode_per_grp;
735 /* get the block no */
737 blkno = __le32_to_cpu(gd[ibmap_idx].inode_table_id) +
738 (inodeno % __le32_to_cpu(inode_per_grp)) / inodes_per_block;
740 /* get the offset of the inode */
741 blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
743 /* read the block no containing the inode */
744 read_buffer = zalloc(fs->blksz);
747 start_block_address = read_buffer;
748 status = ext4fs_devread(blkno * fs->sect_perblk,
749 0, fs->blksz, read_buffer);
753 if (ext4fs_log_journal(read_buffer, blkno))
756 read_buffer = read_buffer + blkoff;
757 inode_buffer = (struct ext2_inode *)read_buffer;
758 memset(inode_buffer, '\0', sizeof(struct ext2_inode));
760 /* write the inode to original position in inode table */
761 if (ext4fs_put_metadata(start_block_address, blkno))
764 /* update the respective inode bitmaps */
766 ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
767 gd[ibmap_idx].free_inodes++;
768 fs->sb->free_inodes++;
770 memset(journal_buffer, '\0', fs->blksz);
771 status = ext4fs_devread(gd[ibmap_idx].inode_id *
772 fs->sect_perblk, 0, fs->blksz, journal_buffer);
775 if (ext4fs_log_journal(journal_buffer, gd[ibmap_idx].inode_id))
781 if (ext4fs_init() != 0) {
782 printf("error in File System init\n");
786 free(start_block_address);
787 free(journal_buffer);
791 free(start_block_address);
792 free(journal_buffer);
797 int ext4fs_init(void)
801 unsigned int real_free_blocks = 0;
802 struct ext_filesystem *fs = get_fs();
805 fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root);
806 fs->inodesz = INODE_SIZE_FILESYSTEM(ext4fs_root);
807 fs->sect_perblk = fs->blksz / SECTOR_SIZE;
809 /* get the superblock */
810 fs->sb = zalloc(SUPERBLOCK_SIZE);
813 if (!ext4fs_devread(SUPERBLOCK_SECTOR, 0, SUPERBLOCK_SIZE,
818 if (ext4fs_init_journal())
821 /* get total no of blockgroups */
822 fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
823 (ext4fs_root->sblock.total_blocks -
824 ext4fs_root->sblock.first_data_block),
825 ext4fs_root->sblock.blocks_per_group);
827 /* get the block group descriptor table */
828 fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
829 if (ext4fs_get_bgdtable() == -1) {
830 printf("Error in getting the block group descriptor table\n");
833 fs->gd = (struct ext2_block_group *)fs->gdtable;
835 /* load all the available bitmap block of the partition */
836 fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
839 for (i = 0; i < fs->no_blkgrp; i++) {
840 fs->blk_bmaps[i] = zalloc(fs->blksz);
841 if (!fs->blk_bmaps[i])
845 for (i = 0; i < fs->no_blkgrp; i++) {
847 ext4fs_devread(fs->gd[i].block_id * fs->sect_perblk, 0,
848 fs->blksz, (char *)fs->blk_bmaps[i]);
853 /* load all the available inode bitmap of the partition */
854 fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *));
855 if (!fs->inode_bmaps)
857 for (i = 0; i < fs->no_blkgrp; i++) {
858 fs->inode_bmaps[i] = zalloc(fs->blksz);
859 if (!fs->inode_bmaps[i])
863 for (i = 0; i < fs->no_blkgrp; i++) {
864 status = ext4fs_devread(fs->gd[i].inode_id * fs->sect_perblk,
866 (char *)fs->inode_bmaps[i]);
872 * check filesystem consistency with free blocks of file system
873 * some time we observed that superblock freeblocks does not match
874 * with the blockgroups freeblocks when improper
875 * reboot of a linux kernel
877 for (i = 0; i < fs->no_blkgrp; i++)
878 real_free_blocks = real_free_blocks + fs->gd[i].free_blocks;
879 if (real_free_blocks != fs->sb->free_blocks)
880 fs->sb->free_blocks = real_free_blocks;
889 void ext4fs_deinit(void)
892 struct ext2_inode inode_journal;
893 struct journal_superblock_t *jsb;
895 struct ext_filesystem *fs = get_fs();
898 char *temp_buff = zalloc(fs->blksz);
900 ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
902 blknr = read_allocated_block(&inode_journal,
903 EXT2_JOURNAL_SUPERBLOCK);
904 ext4fs_devread(blknr * fs->sect_perblk, 0, fs->blksz,
906 jsb = (struct journal_superblock_t *)temp_buff;
907 jsb->s_start = cpu_to_be32(0);
908 put_ext4((uint64_t) (blknr * fs->blksz),
909 (struct journal_superblock_t *)temp_buff, fs->blksz);
912 ext4fs_free_journal();
914 /* get the superblock */
915 ext4fs_devread(SUPERBLOCK_SECTOR, 0, SUPERBLOCK_SIZE, (char *)fs->sb);
916 fs->sb->feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
917 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
918 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
923 for (i = 0; i < fs->no_blkgrp; i++) {
924 free(fs->blk_bmaps[i]);
925 fs->blk_bmaps[i] = NULL;
928 fs->blk_bmaps = NULL;
931 if (fs->inode_bmaps) {
932 for (i = 0; i < fs->no_blkgrp; i++) {
933 free(fs->inode_bmaps[i]);
934 fs->inode_bmaps[i] = NULL;
936 free(fs->inode_bmaps);
937 fs->inode_bmaps = NULL;
945 * reinitiliazed the global inode and
946 * block bitmap first execution check variables
948 fs->first_pass_ibmap = 0;
949 fs->first_pass_bbmap = 0;
950 fs->curr_inode_no = 0;
954 static int ext4fs_write_file(struct ext2_inode *file_inode,
955 int pos, unsigned int len, char *buf)
959 int log2blocksize = LOG2_EXT2_BLOCK_SIZE(ext4fs_root);
960 unsigned int filesize = __le32_to_cpu(file_inode->size);
961 struct ext_filesystem *fs = get_fs();
962 int previous_block_number = -1;
963 int delayed_start = 0;
964 int delayed_extent = 0;
965 int delayed_skipfirst = 0;
966 int delayed_next = 0;
967 char *delayed_buf = NULL;
969 /* Adjust len so it we can't read past the end of the file. */
973 blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz;
975 for (i = pos / fs->blksz; i < blockcnt; i++) {
977 int blockend = fs->blksz;
979 blknr = read_allocated_block(file_inode, i);
983 blknr = blknr << log2blocksize;
986 if (previous_block_number != -1) {
987 if (delayed_next == blknr) {
988 delayed_extent += blockend;
989 delayed_next += blockend >> SECTOR_BITS;
991 put_ext4((uint64_t) (delayed_start *
994 (uint32_t) delayed_extent);
995 previous_block_number = blknr;
996 delayed_start = blknr;
997 delayed_extent = blockend;
998 delayed_skipfirst = skipfirst;
1000 delayed_next = blknr +
1001 (blockend >> SECTOR_BITS);
1004 previous_block_number = blknr;
1005 delayed_start = blknr;
1006 delayed_extent = blockend;
1007 delayed_skipfirst = skipfirst;
1009 delayed_next = blknr +
1010 (blockend >> SECTOR_BITS);
1013 if (previous_block_number != -1) {
1015 put_ext4((uint64_t) (delayed_start *
1016 SECTOR_SIZE), delayed_buf,
1017 (uint32_t) delayed_extent);
1018 previous_block_number = -1;
1020 memset(buf, 0, fs->blksz - skipfirst);
1022 buf += fs->blksz - skipfirst;
1024 if (previous_block_number != -1) {
1026 put_ext4((uint64_t) (delayed_start * SECTOR_SIZE),
1027 delayed_buf, (uint32_t) delayed_extent);
1028 previous_block_number = -1;
1034 int ext4fs_write(const char *fname, unsigned char *buffer,
1035 unsigned long sizebytes)
1038 struct ext2_inode *file_inode = NULL;
1039 unsigned char *inode_buffer = NULL;
1042 time_t timestamp = 0;
1044 uint64_t bytes_reqd_for_file;
1045 unsigned int blks_reqd_for_file;
1046 unsigned int blocks_remaining;
1047 int existing_file_inodeno;
1050 char *temp_ptr = NULL;
1051 long int itable_blkno;
1052 long int parent_itable_blkno;
1054 struct ext2_sblock *sblock = &(ext4fs_root->sblock);
1055 unsigned int inodes_per_block;
1056 unsigned int ibmap_idx;
1057 struct ext_filesystem *fs = get_fs();
1058 g_parent_inode = zalloc(sizeof(struct ext2_inode));
1059 if (!g_parent_inode)
1062 if (ext4fs_init() != 0) {
1063 printf("error in File System init\n");
1066 inodes_per_block = fs->blksz / fs->inodesz;
1067 parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE);
1068 if (parent_inodeno == -1)
1070 if (ext4fs_iget(parent_inodeno, g_parent_inode))
1072 /* check if the filename is already present in root */
1073 existing_file_inodeno = ext4fs_filename_check(filename);
1074 if (existing_file_inodeno != -1) {
1075 ret = ext4fs_delete_file(existing_file_inodeno);
1076 fs->first_pass_bbmap = 0;
1079 fs->first_pass_ibmap = 0;
1080 fs->curr_inode_no = 0;
1084 /* calucalate how many blocks required */
1085 bytes_reqd_for_file = sizebytes;
1086 blks_reqd_for_file = bytes_reqd_for_file / fs->blksz;
1087 if (bytes_reqd_for_file % fs->blksz != 0) {
1088 blks_reqd_for_file++;
1089 debug("total bytes for a file %u\n", blks_reqd_for_file);
1091 blocks_remaining = blks_reqd_for_file;
1092 /* test for available space in partition */
1093 if (fs->sb->free_blocks < blks_reqd_for_file) {
1094 printf("Not enough space on partition !!!\n");
1098 ext4fs_update_parent_dentry(filename, &inodeno, FILETYPE_REG);
1099 /* prepare file inode */
1100 inode_buffer = zalloc(fs->inodesz);
1103 file_inode = (struct ext2_inode *)inode_buffer;
1104 file_inode->mode = S_IFREG | S_IRWXU |
1105 S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH;
1106 /* ToDo: Update correct time */
1107 file_inode->mtime = timestamp;
1108 file_inode->atime = timestamp;
1109 file_inode->ctime = timestamp;
1110 file_inode->nlinks = 1;
1111 file_inode->size = sizebytes;
1113 /* Allocate data blocks */
1114 ext4fs_allocate_blocks(file_inode, blocks_remaining,
1115 &blks_reqd_for_file);
1116 file_inode->blockcnt = (blks_reqd_for_file * fs->blksz) / SECTOR_SIZE;
1118 temp_ptr = zalloc(fs->blksz);
1121 ibmap_idx = inodeno / ext4fs_root->sblock.inodes_per_group;
1123 itable_blkno = __le32_to_cpu(fs->gd[ibmap_idx].inode_table_id) +
1124 (inodeno % __le32_to_cpu(sblock->inodes_per_group)) /
1126 blkoff = (inodeno % inodes_per_block) * fs->inodesz;
1127 ext4fs_devread(itable_blkno * fs->sect_perblk, 0, fs->blksz, temp_ptr);
1128 if (ext4fs_log_journal(temp_ptr, itable_blkno))
1131 memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz);
1132 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
1134 /* copy the file content into data blocks */
1135 if (ext4fs_write_file(file_inode, 0, sizebytes, (char *)buffer) == -1) {
1136 printf("Error in copying content\n");
1139 ibmap_idx = parent_inodeno / ext4fs_root->sblock.inodes_per_group;
1141 parent_itable_blkno = __le32_to_cpu(fs->gd[ibmap_idx].inode_table_id) +
1143 __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1144 blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
1145 if (parent_itable_blkno != itable_blkno) {
1146 memset(temp_ptr, '\0', fs->blksz);
1147 ext4fs_devread(parent_itable_blkno * fs->sect_perblk,
1148 0, fs->blksz, temp_ptr);
1149 if (ext4fs_log_journal(temp_ptr, parent_itable_blkno))
1152 memcpy(temp_ptr + blkoff, g_parent_inode,
1153 sizeof(struct ext2_inode));
1154 if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno))
1159 * If parent and child fall in same inode table block
1160 * both should be kept in 1 buffer
1162 memcpy(temp_ptr + blkoff, g_parent_inode,
1163 sizeof(struct ext2_inode));
1165 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
1172 fs->first_pass_bbmap = 0;
1174 fs->first_pass_ibmap = 0;
1175 fs->curr_inode_no = 0;
1177 free(g_parent_inode);
1178 g_parent_inode = NULL;
1184 free(g_parent_inode);
1185 g_parent_inode = NULL;