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 * SPDX-License-Identifier: GPL-2.0+
27 #include <linux/stat.h>
29 #include "ext4_common.h"
31 static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock
*sb
)
33 sb
->free_inodes
= cpu_to_le32(le32_to_cpu(sb
->free_inodes
) + 1);
36 static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock
*sb
)
38 sb
->free_blocks
= cpu_to_le32(le32_to_cpu(sb
->free_blocks
) + 1);
41 static inline void ext4fs_bg_free_inodes_inc(struct ext2_block_group
*bg
)
43 bg
->free_inodes
= cpu_to_le16(le16_to_cpu(bg
->free_inodes
) + 1);
46 static inline void ext4fs_bg_free_blocks_inc(struct ext2_block_group
*bg
)
48 bg
->free_blocks
= cpu_to_le16(le16_to_cpu(bg
->free_blocks
) + 1);
51 static void ext4fs_update(void)
54 ext4fs_update_journal();
55 struct ext_filesystem
*fs
= get_fs();
57 /* update super block */
58 put_ext4((uint64_t)(SUPERBLOCK_SIZE
),
59 (struct ext2_sblock
*)fs
->sb
, (uint32_t)SUPERBLOCK_SIZE
);
61 /* update block groups */
62 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
63 fs
->bgd
[i
].bg_checksum
= cpu_to_le16(ext4fs_checksum_update(i
));
64 put_ext4((uint64_t)le32_to_cpu(fs
->bgd
[i
].block_id
) * fs
->blksz
,
65 fs
->blk_bmaps
[i
], fs
->blksz
);
68 /* update inode table groups */
69 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
70 put_ext4((uint64_t)le32_to_cpu(fs
->bgd
[i
].inode_id
) * fs
->blksz
,
71 fs
->inode_bmaps
[i
], fs
->blksz
);
74 /* update the block group descriptor table */
75 put_ext4((uint64_t)((uint64_t)fs
->gdtable_blkno
* (uint64_t)fs
->blksz
),
76 (struct ext2_block_group
*)fs
->gdtable
,
77 (fs
->blksz
* fs
->no_blk_pergdt
));
79 ext4fs_dump_metadata();
85 int ext4fs_get_bgdtable(void)
89 struct ext_filesystem
*fs
= get_fs();
90 grp_desc_size
= sizeof(struct ext2_block_group
);
91 fs
->no_blk_pergdt
= (fs
->no_blkgrp
* grp_desc_size
) / fs
->blksz
;
92 if ((fs
->no_blkgrp
* grp_desc_size
) % fs
->blksz
)
95 /* allocate memory for gdtable */
96 fs
->gdtable
= zalloc(fs
->blksz
* fs
->no_blk_pergdt
);
99 /* read the group descriptor table */
100 status
= ext4fs_devread((lbaint_t
)fs
->gdtable_blkno
* fs
->sect_perblk
,
101 0, fs
->blksz
* fs
->no_blk_pergdt
, fs
->gdtable
);
105 if (ext4fs_log_gdt(fs
->gdtable
)) {
106 printf("Error in ext4fs_log_gdt\n");
118 static void delete_single_indirect_block(struct ext2_inode
*inode
)
120 struct ext2_block_group
*bgd
= NULL
;
121 static int prev_bg_bmap_idx
= -1;
126 uint32_t blk_per_grp
= le32_to_cpu(ext4fs_root
->sblock
.blocks_per_group
);
127 struct ext_filesystem
*fs
= get_fs();
128 char *journal_buffer
= zalloc(fs
->blksz
);
129 if (!journal_buffer
) {
130 printf("No memory\n");
133 /* get block group descriptor table */
134 bgd
= (struct ext2_block_group
*)fs
->gdtable
;
136 /* deleting the single indirect block associated with inode */
137 if (inode
->b
.blocks
.indir_block
!= 0) {
138 blknr
= le32_to_cpu(inode
->b
.blocks
.indir_block
);
139 debug("SIPB releasing %u\n", blknr
);
140 bg_idx
= blknr
/ blk_per_grp
;
141 if (fs
->blksz
== 1024) {
142 remainder
= blknr
% blk_per_grp
;
146 ext4fs_reset_block_bmap(blknr
, fs
->blk_bmaps
[bg_idx
], bg_idx
);
147 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
148 ext4fs_sb_free_blocks_inc(fs
->sb
);
150 if (prev_bg_bmap_idx
!= bg_idx
) {
151 status
= ext4fs_devread(
152 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
) *
153 fs
->sect_perblk
, 0, fs
->blksz
,
157 if (ext4fs_log_journal
158 (journal_buffer
, le32_to_cpu(bgd
[bg_idx
].block_id
)))
160 prev_bg_bmap_idx
= bg_idx
;
164 free(journal_buffer
);
167 static void delete_double_indirect_block(struct ext2_inode
*inode
)
171 static int prev_bg_bmap_idx
= -1;
175 uint32_t blk_per_grp
= le32_to_cpu(ext4fs_root
->sblock
.blocks_per_group
);
176 __le32
*di_buffer
= NULL
;
177 void *dib_start_addr
= NULL
;
178 struct ext2_block_group
*bgd
= NULL
;
179 struct ext_filesystem
*fs
= get_fs();
180 char *journal_buffer
= zalloc(fs
->blksz
);
181 if (!journal_buffer
) {
182 printf("No memory\n");
185 /* get the block group descriptor table */
186 bgd
= (struct ext2_block_group
*)fs
->gdtable
;
188 if (inode
->b
.blocks
.double_indir_block
!= 0) {
189 di_buffer
= zalloc(fs
->blksz
);
191 printf("No memory\n");
194 dib_start_addr
= di_buffer
;
195 blknr
= le32_to_cpu(inode
->b
.blocks
.double_indir_block
);
196 status
= ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
, 0,
197 fs
->blksz
, (char *)di_buffer
);
198 for (i
= 0; i
< fs
->blksz
/ sizeof(int); i
++) {
202 debug("DICB releasing %u\n", *di_buffer
);
203 bg_idx
= le32_to_cpu(*di_buffer
) / blk_per_grp
;
204 if (fs
->blksz
== 1024) {
205 remainder
= le32_to_cpu(*di_buffer
) % blk_per_grp
;
209 ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer
),
210 fs
->blk_bmaps
[bg_idx
], bg_idx
);
212 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
213 ext4fs_sb_free_blocks_inc(fs
->sb
);
215 if (prev_bg_bmap_idx
!= bg_idx
) {
216 status
= ext4fs_devread(
217 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
)
218 * fs
->sect_perblk
, 0,
224 if (ext4fs_log_journal(journal_buffer
,
225 le32_to_cpu(bgd
[bg_idx
].block_id
)))
227 prev_bg_bmap_idx
= bg_idx
;
231 /* removing the parent double indirect block */
232 blknr
= le32_to_cpu(inode
->b
.blocks
.double_indir_block
);
233 bg_idx
= blknr
/ blk_per_grp
;
234 if (fs
->blksz
== 1024) {
235 remainder
= blknr
% blk_per_grp
;
239 ext4fs_reset_block_bmap(blknr
, fs
->blk_bmaps
[bg_idx
], bg_idx
);
240 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
241 ext4fs_sb_free_blocks_inc(fs
->sb
);
243 if (prev_bg_bmap_idx
!= bg_idx
) {
244 memset(journal_buffer
, '\0', fs
->blksz
);
245 status
= ext4fs_devread((lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
) *
246 fs
->sect_perblk
, 0, fs
->blksz
,
251 if (ext4fs_log_journal(journal_buffer
,
252 le32_to_cpu(bgd
[bg_idx
].block_id
)))
254 prev_bg_bmap_idx
= bg_idx
;
256 debug("DIPB releasing %d\n", blknr
);
259 free(dib_start_addr
);
260 free(journal_buffer
);
263 static void delete_triple_indirect_block(struct ext2_inode
*inode
)
267 static int prev_bg_bmap_idx
= -1;
271 uint32_t blk_per_grp
= le32_to_cpu(ext4fs_root
->sblock
.blocks_per_group
);
272 __le32
*tigp_buffer
= NULL
;
273 void *tib_start_addr
= NULL
;
274 __le32
*tip_buffer
= NULL
;
275 void *tipb_start_addr
= NULL
;
276 struct ext2_block_group
*bgd
= NULL
;
277 struct ext_filesystem
*fs
= get_fs();
278 char *journal_buffer
= zalloc(fs
->blksz
);
279 if (!journal_buffer
) {
280 printf("No memory\n");
283 /* get block group descriptor table */
284 bgd
= (struct ext2_block_group
*)fs
->gdtable
;
286 if (inode
->b
.blocks
.triple_indir_block
!= 0) {
287 tigp_buffer
= zalloc(fs
->blksz
);
289 printf("No memory\n");
292 tib_start_addr
= tigp_buffer
;
293 blknr
= le32_to_cpu(inode
->b
.blocks
.triple_indir_block
);
294 status
= ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
, 0,
295 fs
->blksz
, (char *)tigp_buffer
);
296 for (i
= 0; i
< fs
->blksz
/ sizeof(int); i
++) {
297 if (*tigp_buffer
== 0)
299 debug("tigp buffer releasing %u\n", *tigp_buffer
);
301 tip_buffer
= zalloc(fs
->blksz
);
304 tipb_start_addr
= tip_buffer
;
305 status
= ext4fs_devread((lbaint_t
)le32_to_cpu(*tigp_buffer
) *
306 fs
->sect_perblk
, 0, fs
->blksz
,
308 for (j
= 0; j
< fs
->blksz
/ sizeof(int); j
++) {
309 if (le32_to_cpu(*tip_buffer
) == 0)
311 bg_idx
= le32_to_cpu(*tip_buffer
) / blk_per_grp
;
312 if (fs
->blksz
== 1024) {
313 remainder
= le32_to_cpu(*tip_buffer
) % blk_per_grp
;
318 ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer
),
319 fs
->blk_bmaps
[bg_idx
],
323 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
324 ext4fs_sb_free_blocks_inc(fs
->sb
);
326 if (prev_bg_bmap_idx
!= bg_idx
) {
329 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
) *
336 if (ext4fs_log_journal(journal_buffer
,
337 le32_to_cpu(bgd
[bg_idx
].block_id
)))
339 prev_bg_bmap_idx
= bg_idx
;
342 free(tipb_start_addr
);
343 tipb_start_addr
= NULL
;
346 * removing the grand parent blocks
347 * which is connected to inode
349 bg_idx
= le32_to_cpu(*tigp_buffer
) / blk_per_grp
;
350 if (fs
->blksz
== 1024) {
351 remainder
= le32_to_cpu(*tigp_buffer
) % blk_per_grp
;
355 ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer
),
356 fs
->blk_bmaps
[bg_idx
], bg_idx
);
359 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
360 ext4fs_sb_free_blocks_inc(fs
->sb
);
362 if (prev_bg_bmap_idx
!= bg_idx
) {
363 memset(journal_buffer
, '\0', fs
->blksz
);
366 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
) *
368 fs
->blksz
, journal_buffer
);
372 if (ext4fs_log_journal(journal_buffer
,
373 le32_to_cpu(bgd
[bg_idx
].block_id
)))
375 prev_bg_bmap_idx
= bg_idx
;
379 /* removing the grand parent triple indirect block */
380 blknr
= le32_to_cpu(inode
->b
.blocks
.triple_indir_block
);
381 bg_idx
= blknr
/ blk_per_grp
;
382 if (fs
->blksz
== 1024) {
383 remainder
= blknr
% blk_per_grp
;
387 ext4fs_reset_block_bmap(blknr
, fs
->blk_bmaps
[bg_idx
], bg_idx
);
388 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
389 ext4fs_sb_free_blocks_inc(fs
->sb
);
391 if (prev_bg_bmap_idx
!= bg_idx
) {
392 memset(journal_buffer
, '\0', fs
->blksz
);
393 status
= ext4fs_devread(
394 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
) *
395 fs
->sect_perblk
, 0, fs
->blksz
,
400 if (ext4fs_log_journal(journal_buffer
,
401 le32_to_cpu(bgd
[bg_idx
].block_id
)))
403 prev_bg_bmap_idx
= bg_idx
;
405 debug("tigp buffer itself releasing %d\n", blknr
);
408 free(tib_start_addr
);
409 free(tipb_start_addr
);
410 free(journal_buffer
);
413 static int ext4fs_delete_file(int inodeno
)
415 struct ext2_inode inode
;
422 char *read_buffer
= NULL
;
423 char *start_block_address
= NULL
;
426 static int prev_bg_bmap_idx
= -1;
427 unsigned int inodes_per_block
;
430 uint32_t blk_per_grp
= le32_to_cpu(ext4fs_root
->sblock
.blocks_per_group
);
431 uint32_t inode_per_grp
= le32_to_cpu(ext4fs_root
->sblock
.inodes_per_group
);
432 struct ext2_inode
*inode_buffer
= NULL
;
433 struct ext2_block_group
*bgd
= NULL
;
434 struct ext_filesystem
*fs
= get_fs();
435 char *journal_buffer
= zalloc(fs
->blksz
);
438 /* get the block group descriptor table */
439 bgd
= (struct ext2_block_group
*)fs
->gdtable
;
440 status
= ext4fs_read_inode(ext4fs_root
, inodeno
, &inode
);
444 /* read the block no allocated to a file */
445 no_blocks
= le32_to_cpu(inode
.size
) / fs
->blksz
;
446 if (le32_to_cpu(inode
.size
) % fs
->blksz
)
449 if (le32_to_cpu(inode
.flags
) & EXT4_EXTENTS_FL
) {
450 struct ext2fs_node
*node_inode
=
451 zalloc(sizeof(struct ext2fs_node
));
454 node_inode
->data
= ext4fs_root
;
455 node_inode
->ino
= inodeno
;
456 node_inode
->inode_read
= 0;
457 memcpy(&(node_inode
->inode
), &inode
, sizeof(struct ext2_inode
));
459 for (i
= 0; i
< no_blocks
; i
++) {
460 blknr
= read_allocated_block(&(node_inode
->inode
), i
);
461 bg_idx
= blknr
/ blk_per_grp
;
462 if (fs
->blksz
== 1024) {
463 remainder
= blknr
% blk_per_grp
;
467 ext4fs_reset_block_bmap(blknr
, fs
->blk_bmaps
[bg_idx
],
469 debug("EXT4_EXTENTS Block releasing %ld: %d\n",
472 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
473 ext4fs_sb_free_blocks_inc(fs
->sb
);
476 if (prev_bg_bmap_idx
!= bg_idx
) {
479 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
) *
481 fs
->blksz
, journal_buffer
);
484 if (ext4fs_log_journal(journal_buffer
,
485 le32_to_cpu(bgd
[bg_idx
].block_id
)))
487 prev_bg_bmap_idx
= bg_idx
;
496 delete_single_indirect_block(&inode
);
497 delete_double_indirect_block(&inode
);
498 delete_triple_indirect_block(&inode
);
500 /* read the block no allocated to a file */
501 no_blocks
= le32_to_cpu(inode
.size
) / fs
->blksz
;
502 if (le32_to_cpu(inode
.size
) % fs
->blksz
)
504 for (i
= 0; i
< no_blocks
; i
++) {
505 blknr
= read_allocated_block(&inode
, i
);
506 bg_idx
= blknr
/ blk_per_grp
;
507 if (fs
->blksz
== 1024) {
508 remainder
= blknr
% blk_per_grp
;
512 ext4fs_reset_block_bmap(blknr
, fs
->blk_bmaps
[bg_idx
],
514 debug("ActualB releasing %ld: %d\n", blknr
, bg_idx
);
516 ext4fs_bg_free_blocks_inc(&bgd
[bg_idx
]);
517 ext4fs_sb_free_blocks_inc(fs
->sb
);
519 if (prev_bg_bmap_idx
!= bg_idx
) {
520 memset(journal_buffer
, '\0', fs
->blksz
);
521 status
= ext4fs_devread(
522 (lbaint_t
)le32_to_cpu(bgd
[bg_idx
].block_id
)
528 if (ext4fs_log_journal(journal_buffer
,
529 le32_to_cpu(bgd
[bg_idx
].block_id
)))
531 prev_bg_bmap_idx
= bg_idx
;
536 /* from the inode no to blockno */
537 inodes_per_block
= fs
->blksz
/ fs
->inodesz
;
538 ibmap_idx
= inodeno
/ inode_per_grp
;
540 /* get the block no */
542 blkno
= le32_to_cpu(bgd
[ibmap_idx
].inode_table_id
) +
543 (inodeno
% inode_per_grp
) / inodes_per_block
;
545 /* get the offset of the inode */
546 blkoff
= ((inodeno
) % inodes_per_block
) * fs
->inodesz
;
548 /* read the block no containing the inode */
549 read_buffer
= zalloc(fs
->blksz
);
552 start_block_address
= read_buffer
;
553 status
= ext4fs_devread((lbaint_t
)blkno
* fs
->sect_perblk
,
554 0, fs
->blksz
, read_buffer
);
558 if (ext4fs_log_journal(read_buffer
, blkno
))
561 read_buffer
= read_buffer
+ blkoff
;
562 inode_buffer
= (struct ext2_inode
*)read_buffer
;
563 memset(inode_buffer
, '\0', sizeof(struct ext2_inode
));
565 /* write the inode to original position in inode table */
566 if (ext4fs_put_metadata(start_block_address
, blkno
))
569 /* update the respective inode bitmaps */
571 ext4fs_reset_inode_bmap(inodeno
, fs
->inode_bmaps
[ibmap_idx
], ibmap_idx
);
572 ext4fs_bg_free_inodes_inc(&bgd
[ibmap_idx
]);
573 ext4fs_sb_free_inodes_inc(fs
->sb
);
575 memset(journal_buffer
, '\0', fs
->blksz
);
576 status
= ext4fs_devread((lbaint_t
)le32_to_cpu(bgd
[ibmap_idx
].inode_id
) *
577 fs
->sect_perblk
, 0, fs
->blksz
, journal_buffer
);
580 if (ext4fs_log_journal(journal_buffer
, le32_to_cpu(bgd
[ibmap_idx
].inode_id
)))
585 ext4fs_reinit_global();
587 if (ext4fs_init() != 0) {
588 printf("error in File System init\n");
592 free(start_block_address
);
593 free(journal_buffer
);
597 free(start_block_address
);
598 free(journal_buffer
);
603 int ext4fs_init(void)
607 uint32_t real_free_blocks
= 0;
608 struct ext_filesystem
*fs
= get_fs();
611 fs
->blksz
= EXT2_BLOCK_SIZE(ext4fs_root
);
612 fs
->inodesz
= INODE_SIZE_FILESYSTEM(ext4fs_root
);
613 fs
->sect_perblk
= fs
->blksz
>> fs
->dev_desc
->log2blksz
;
615 /* get the superblock */
616 fs
->sb
= zalloc(SUPERBLOCK_SIZE
);
619 if (!ext4_read_superblock((char *)fs
->sb
))
623 if (ext4fs_init_journal())
626 /* get total no of blockgroups */
627 fs
->no_blkgrp
= (uint32_t)ext4fs_div_roundup(
628 le32_to_cpu(ext4fs_root
->sblock
.total_blocks
)
629 - le32_to_cpu(ext4fs_root
->sblock
.first_data_block
),
630 le32_to_cpu(ext4fs_root
->sblock
.blocks_per_group
));
632 /* get the block group descriptor table */
633 fs
->gdtable_blkno
= ((EXT2_MIN_BLOCK_SIZE
== fs
->blksz
) + 1);
634 if (ext4fs_get_bgdtable() == -1) {
635 printf("Error in getting the block group descriptor table\n");
638 fs
->bgd
= (struct ext2_block_group
*)fs
->gdtable
;
640 /* load all the available bitmap block of the partition */
641 fs
->blk_bmaps
= zalloc(fs
->no_blkgrp
* sizeof(char *));
644 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
645 fs
->blk_bmaps
[i
] = zalloc(fs
->blksz
);
646 if (!fs
->blk_bmaps
[i
])
650 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
653 (lbaint_t
)le32_to_cpu(fs
->bgd
[i
].block_id
) *
655 fs
->blksz
, (char *)fs
->blk_bmaps
[i
]);
660 /* load all the available inode bitmap of the partition */
661 fs
->inode_bmaps
= zalloc(fs
->no_blkgrp
* sizeof(unsigned char *));
662 if (!fs
->inode_bmaps
)
664 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
665 fs
->inode_bmaps
[i
] = zalloc(fs
->blksz
);
666 if (!fs
->inode_bmaps
[i
])
670 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
671 status
= ext4fs_devread(
672 (lbaint_t
)le32_to_cpu(fs
->bgd
[i
].inode_id
) *
675 (char *)fs
->inode_bmaps
[i
]);
681 * check filesystem consistency with free blocks of file system
682 * some time we observed that superblock freeblocks does not match
683 * with the blockgroups freeblocks when improper
684 * reboot of a linux kernel
686 for (i
= 0; i
< fs
->no_blkgrp
; i
++)
687 real_free_blocks
= real_free_blocks
+ le16_to_cpu(fs
->bgd
[i
].free_blocks
);
688 if (real_free_blocks
!= le32_to_cpu(fs
->sb
->free_blocks
))
689 fs
->sb
->free_blocks
= cpu_to_le32(real_free_blocks
);
698 void ext4fs_deinit(void)
701 struct ext2_inode inode_journal
;
702 struct journal_superblock_t
*jsb
;
704 struct ext_filesystem
*fs
= get_fs();
705 uint32_t new_feature_incompat
;
708 char *temp_buff
= zalloc(fs
->blksz
);
710 ext4fs_read_inode(ext4fs_root
, EXT2_JOURNAL_INO
,
712 blknr
= read_allocated_block(&inode_journal
,
713 EXT2_JOURNAL_SUPERBLOCK
);
714 ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
, 0, fs
->blksz
,
716 jsb
= (struct journal_superblock_t
*)temp_buff
;
718 put_ext4((uint64_t) ((uint64_t)blknr
* (uint64_t)fs
->blksz
),
719 (struct journal_superblock_t
*)temp_buff
, fs
->blksz
);
722 ext4fs_free_journal();
724 /* get the superblock */
725 ext4_read_superblock((char *)fs
->sb
);
726 new_feature_incompat
= le32_to_cpu(fs
->sb
->feature_incompat
);
727 new_feature_incompat
&= ~EXT3_FEATURE_INCOMPAT_RECOVER
;
728 fs
->sb
->feature_incompat
= cpu_to_le32(new_feature_incompat
);
729 put_ext4((uint64_t)(SUPERBLOCK_SIZE
),
730 (struct ext2_sblock
*)fs
->sb
, (uint32_t)SUPERBLOCK_SIZE
);
735 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
736 free(fs
->blk_bmaps
[i
]);
737 fs
->blk_bmaps
[i
] = NULL
;
740 fs
->blk_bmaps
= NULL
;
743 if (fs
->inode_bmaps
) {
744 for (i
= 0; i
< fs
->no_blkgrp
; i
++) {
745 free(fs
->inode_bmaps
[i
]);
746 fs
->inode_bmaps
[i
] = NULL
;
748 free(fs
->inode_bmaps
);
749 fs
->inode_bmaps
= NULL
;
757 * reinitiliazed the global inode and
758 * block bitmap first execution check variables
760 fs
->first_pass_ibmap
= 0;
761 fs
->first_pass_bbmap
= 0;
762 fs
->curr_inode_no
= 0;
766 static int ext4fs_write_file(struct ext2_inode
*file_inode
,
767 int pos
, unsigned int len
, char *buf
)
771 uint32_t filesize
= le32_to_cpu(file_inode
->size
);
772 struct ext_filesystem
*fs
= get_fs();
773 int log2blksz
= fs
->dev_desc
->log2blksz
;
774 int log2_fs_blocksize
= LOG2_BLOCK_SIZE(ext4fs_root
) - log2blksz
;
775 int previous_block_number
= -1;
776 int delayed_start
= 0;
777 int delayed_extent
= 0;
778 int delayed_next
= 0;
779 char *delayed_buf
= NULL
;
781 /* Adjust len so it we can't read past the end of the file. */
785 blockcnt
= ((len
+ pos
) + fs
->blksz
- 1) / fs
->blksz
;
787 for (i
= pos
/ fs
->blksz
; i
< blockcnt
; i
++) {
789 int blockend
= fs
->blksz
;
791 blknr
= read_allocated_block(file_inode
, i
);
795 blknr
= blknr
<< log2_fs_blocksize
;
798 if (previous_block_number
!= -1) {
799 if (delayed_next
== blknr
) {
800 delayed_extent
+= blockend
;
801 delayed_next
+= blockend
>> log2blksz
;
804 ((uint64_t)delayed_start
<< log2blksz
),
806 (uint32_t) delayed_extent
);
807 previous_block_number
= blknr
;
808 delayed_start
= blknr
;
809 delayed_extent
= blockend
;
811 delayed_next
= blknr
+
812 (blockend
>> log2blksz
);
815 previous_block_number
= blknr
;
816 delayed_start
= blknr
;
817 delayed_extent
= blockend
;
819 delayed_next
= blknr
+
820 (blockend
>> log2blksz
);
823 if (previous_block_number
!= -1) {
825 put_ext4((uint64_t) ((uint64_t)delayed_start
<<
828 (uint32_t) delayed_extent
);
829 previous_block_number
= -1;
831 memset(buf
, 0, fs
->blksz
- skipfirst
);
833 buf
+= fs
->blksz
- skipfirst
;
835 if (previous_block_number
!= -1) {
837 put_ext4((uint64_t) ((uint64_t)delayed_start
<< log2blksz
),
838 delayed_buf
, (uint32_t) delayed_extent
);
839 previous_block_number
= -1;
845 int ext4fs_write(const char *fname
, unsigned char *buffer
,
846 unsigned long sizebytes
)
849 struct ext2_inode
*file_inode
= NULL
;
850 unsigned char *inode_buffer
= NULL
;
853 time_t timestamp
= 0;
855 uint64_t bytes_reqd_for_file
;
856 unsigned int blks_reqd_for_file
;
857 unsigned int blocks_remaining
;
858 int existing_file_inodeno
;
859 char *temp_ptr
= NULL
;
860 long int itable_blkno
;
861 long int parent_itable_blkno
;
863 struct ext2_sblock
*sblock
= &(ext4fs_root
->sblock
);
864 unsigned int inodes_per_block
;
865 unsigned int ibmap_idx
;
866 struct ext_filesystem
*fs
= get_fs();
867 ALLOC_CACHE_ALIGN_BUFFER(char, filename
, 256);
868 memset(filename
, 0x00, 256);
870 g_parent_inode
= zalloc(sizeof(struct ext2_inode
));
874 if (ext4fs_init() != 0) {
875 printf("error in File System init\n");
878 inodes_per_block
= fs
->blksz
/ fs
->inodesz
;
879 parent_inodeno
= ext4fs_get_parent_inode_num(fname
, filename
, F_FILE
);
880 if (parent_inodeno
== -1)
882 if (ext4fs_iget(parent_inodeno
, g_parent_inode
))
884 /* do not mess up a directory using hash trees */
885 if (le32_to_cpu(g_parent_inode
->flags
) & EXT4_INDEX_FL
) {
886 printf("hash tree directory\n");
889 /* check if the filename is already present in root */
890 existing_file_inodeno
= ext4fs_filename_unlink(filename
);
891 if (existing_file_inodeno
!= -1) {
892 ret
= ext4fs_delete_file(existing_file_inodeno
);
893 fs
->first_pass_bbmap
= 0;
896 fs
->first_pass_ibmap
= 0;
897 fs
->curr_inode_no
= 0;
901 /* calucalate how many blocks required */
902 bytes_reqd_for_file
= sizebytes
;
903 blks_reqd_for_file
= lldiv(bytes_reqd_for_file
, fs
->blksz
);
904 if (do_div(bytes_reqd_for_file
, fs
->blksz
) != 0) {
905 blks_reqd_for_file
++;
906 debug("total bytes for a file %u\n", blks_reqd_for_file
);
908 blocks_remaining
= blks_reqd_for_file
;
909 /* test for available space in partition */
910 if (le32_to_cpu(fs
->sb
->free_blocks
) < blks_reqd_for_file
) {
911 printf("Not enough space on partition !!!\n");
915 inodeno
= ext4fs_update_parent_dentry(filename
, FILETYPE_REG
);
918 /* prepare file inode */
919 inode_buffer
= zalloc(fs
->inodesz
);
922 file_inode
= (struct ext2_inode
*)inode_buffer
;
923 file_inode
->mode
= cpu_to_le16(S_IFREG
| S_IRWXU
|
924 S_IRGRP
| S_IROTH
| S_IXGRP
| S_IXOTH
);
925 /* ToDo: Update correct time */
926 file_inode
->mtime
= cpu_to_le32(timestamp
);
927 file_inode
->atime
= cpu_to_le32(timestamp
);
928 file_inode
->ctime
= cpu_to_le32(timestamp
);
929 file_inode
->nlinks
= cpu_to_le16(1);
930 file_inode
->size
= cpu_to_le32(sizebytes
);
932 /* Allocate data blocks */
933 ext4fs_allocate_blocks(file_inode
, blocks_remaining
,
934 &blks_reqd_for_file
);
935 file_inode
->blockcnt
= cpu_to_le32((blks_reqd_for_file
* fs
->blksz
) >>
936 fs
->dev_desc
->log2blksz
);
938 temp_ptr
= zalloc(fs
->blksz
);
941 ibmap_idx
= inodeno
/ le32_to_cpu(ext4fs_root
->sblock
.inodes_per_group
);
943 itable_blkno
= le32_to_cpu(fs
->bgd
[ibmap_idx
].inode_table_id
) +
944 (inodeno
% le32_to_cpu(sblock
->inodes_per_group
)) /
946 blkoff
= (inodeno
% inodes_per_block
) * fs
->inodesz
;
947 ext4fs_devread((lbaint_t
)itable_blkno
* fs
->sect_perblk
, 0, fs
->blksz
,
949 if (ext4fs_log_journal(temp_ptr
, itable_blkno
))
952 memcpy(temp_ptr
+ blkoff
, inode_buffer
, fs
->inodesz
);
953 if (ext4fs_put_metadata(temp_ptr
, itable_blkno
))
955 /* copy the file content into data blocks */
956 if (ext4fs_write_file(file_inode
, 0, sizebytes
, (char *)buffer
) == -1) {
957 printf("Error in copying content\n");
960 ibmap_idx
= parent_inodeno
/ le32_to_cpu(ext4fs_root
->sblock
.inodes_per_group
);
962 parent_itable_blkno
= le32_to_cpu(fs
->bgd
[ibmap_idx
].inode_table_id
) +
964 le32_to_cpu(sblock
->inodes_per_group
)) / inodes_per_block
;
965 blkoff
= (parent_inodeno
% inodes_per_block
) * fs
->inodesz
;
966 if (parent_itable_blkno
!= itable_blkno
) {
967 memset(temp_ptr
, '\0', fs
->blksz
);
968 ext4fs_devread((lbaint_t
)parent_itable_blkno
* fs
->sect_perblk
,
969 0, fs
->blksz
, temp_ptr
);
970 if (ext4fs_log_journal(temp_ptr
, parent_itable_blkno
))
973 memcpy(temp_ptr
+ blkoff
, g_parent_inode
,
974 sizeof(struct ext2_inode
));
975 if (ext4fs_put_metadata(temp_ptr
, parent_itable_blkno
))
980 * If parent and child fall in same inode table block
981 * both should be kept in 1 buffer
983 memcpy(temp_ptr
+ blkoff
, g_parent_inode
,
984 sizeof(struct ext2_inode
));
986 if (ext4fs_put_metadata(temp_ptr
, itable_blkno
))
993 fs
->first_pass_bbmap
= 0;
995 fs
->first_pass_ibmap
= 0;
996 fs
->curr_inode_no
= 0;
998 free(g_parent_inode
);
999 g_parent_inode
= NULL
;
1005 free(g_parent_inode
);
1006 g_parent_inode
= NULL
;
1011 int ext4_write_file(const char *filename
, void *buf
, loff_t offset
,
1012 loff_t len
, loff_t
*actwrite
)
1017 printf("** Cannot support non-zero offset **\n");
1021 ret
= ext4fs_write(filename
, buf
, len
);
1023 printf("** Error ext4fs_write() **\n");