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 * Journal data structures and headers for Journaling feature of ext4
8 * have been referred from JBD2 (Journaling Block device 2)
9 * implementation in Linux Kernel.
10 * Written by Stephen C. Tweedie <sct@redhat.com>
12 * Copyright 1998-2000 Red Hat, Inc --- All Rights Reserved
13 * SPDX-License-Identifier: GPL-2.0+
19 #include <ext_common.h>
20 #include "ext4_common.h"
22 static struct revoke_blk_list
*revk_blk_list
;
23 static struct revoke_blk_list
*prev_node
;
24 static int first_node
= true;
29 struct journal_log
*journal_ptr
[MAX_JOURNAL_ENTRIES
];
30 struct dirty_blocks
*dirty_block_ptr
[MAX_JOURNAL_ENTRIES
];
32 int ext4fs_init_journal(void)
36 struct ext_filesystem
*fs
= get_fs();
45 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
46 journal_ptr
[i
] = zalloc(sizeof(struct journal_log
));
49 dirty_block_ptr
[i
] = zalloc(sizeof(struct dirty_blocks
));
50 if (!dirty_block_ptr
[i
])
52 journal_ptr
[i
]->buf
= NULL
;
53 journal_ptr
[i
]->blknr
= -1;
55 dirty_block_ptr
[i
]->buf
= NULL
;
56 dirty_block_ptr
[i
]->blknr
= -1;
59 if (fs
->blksz
== 4096) {
60 temp
= zalloc(fs
->blksz
);
63 journal_ptr
[gindex
]->buf
= zalloc(fs
->blksz
);
64 if (!journal_ptr
[gindex
]->buf
)
66 ext4fs_devread(0, 0, fs
->blksz
, temp
);
67 memcpy(temp
+ SUPERBLOCK_SIZE
, fs
->sb
, SUPERBLOCK_SIZE
);
68 memcpy(journal_ptr
[gindex
]->buf
, temp
, fs
->blksz
);
69 journal_ptr
[gindex
++]->blknr
= 0;
72 journal_ptr
[gindex
]->buf
= zalloc(fs
->blksz
);
73 if (!journal_ptr
[gindex
]->buf
)
75 memcpy(journal_ptr
[gindex
]->buf
, fs
->sb
, SUPERBLOCK_SIZE
);
76 journal_ptr
[gindex
++]->blknr
= 1;
79 /* Check the file system state using journal super block */
80 if (ext4fs_check_journal_state(SCAN
))
82 /* Check the file system state using journal super block */
83 if (ext4fs_check_journal_state(RECOVER
))
91 void ext4fs_dump_metadata(void)
93 struct ext_filesystem
*fs
= get_fs();
95 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
96 if (dirty_block_ptr
[i
]->blknr
== -1)
98 put_ext4((uint64_t) ((uint64_t)dirty_block_ptr
[i
]->blknr
*
99 (uint64_t)fs
->blksz
), dirty_block_ptr
[i
]->buf
,
104 void ext4fs_free_journal(void)
107 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
108 if (dirty_block_ptr
[i
]->blknr
== -1)
110 if (dirty_block_ptr
[i
]->buf
)
111 free(dirty_block_ptr
[i
]->buf
);
114 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
115 if (journal_ptr
[i
]->blknr
== -1)
117 if (journal_ptr
[i
]->buf
)
118 free(journal_ptr
[i
]->buf
);
121 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
123 free(journal_ptr
[i
]);
124 if (dirty_block_ptr
[i
])
125 free(dirty_block_ptr
[i
]);
132 int ext4fs_log_gdt(char *gd_table
)
134 struct ext_filesystem
*fs
= get_fs();
136 long int var
= fs
->gdtable_blkno
;
137 for (i
= 0; i
< fs
->no_blk_pergdt
; i
++) {
138 journal_ptr
[gindex
]->buf
= zalloc(fs
->blksz
);
139 if (!journal_ptr
[gindex
]->buf
)
141 memcpy(journal_ptr
[gindex
]->buf
, gd_table
, fs
->blksz
);
142 gd_table
+= fs
->blksz
;
143 journal_ptr
[gindex
++]->blknr
= var
++;
150 * This function stores the backup copy of meta data in RAM
151 * journal_buffer -- Buffer containing meta data
152 * blknr -- Block number on disk of the meta data buffer
154 int ext4fs_log_journal(char *journal_buffer
, uint32_t blknr
)
156 struct ext_filesystem
*fs
= get_fs();
159 if (!journal_buffer
) {
160 printf("Invalid input arguments %s\n", __func__
);
164 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
165 if (journal_ptr
[i
]->blknr
== -1)
167 if (journal_ptr
[i
]->blknr
== blknr
)
171 journal_ptr
[gindex
]->buf
= zalloc(fs
->blksz
);
172 if (!journal_ptr
[gindex
]->buf
)
175 memcpy(journal_ptr
[gindex
]->buf
, journal_buffer
, fs
->blksz
);
176 journal_ptr
[gindex
++]->blknr
= blknr
;
182 * This function stores the modified meta data in RAM
183 * metadata_buffer -- Buffer containing meta data
184 * blknr -- Block number on disk of the meta data buffer
186 int ext4fs_put_metadata(char *metadata_buffer
, uint32_t blknr
)
188 struct ext_filesystem
*fs
= get_fs();
189 if (!metadata_buffer
) {
190 printf("Invalid input arguments %s\n", __func__
);
193 dirty_block_ptr
[gd_index
]->buf
= zalloc(fs
->blksz
);
194 if (!dirty_block_ptr
[gd_index
]->buf
)
196 memcpy(dirty_block_ptr
[gd_index
]->buf
, metadata_buffer
, fs
->blksz
);
197 dirty_block_ptr
[gd_index
++]->blknr
= blknr
;
202 void print_revoke_blks(char *revk_blk
)
207 struct journal_revoke_header_t
*header
;
209 if (revk_blk
== NULL
)
212 header
= (struct journal_revoke_header_t
*) revk_blk
;
213 offset
= sizeof(struct journal_revoke_header_t
);
214 max
= be32_to_cpu(header
->r_count
);
215 printf("total bytes %d\n", max
);
217 while (offset
< max
) {
218 blocknr
= be32_to_cpu(*((__be32
*)(revk_blk
+ offset
)));
219 printf("revoke blknr is %ld\n", blocknr
);
224 static struct revoke_blk_list
*_get_node(void)
226 struct revoke_blk_list
*tmp_node
;
227 tmp_node
= zalloc(sizeof(struct revoke_blk_list
));
228 if (tmp_node
== NULL
)
230 tmp_node
->content
= NULL
;
231 tmp_node
->next
= NULL
;
236 void ext4fs_push_revoke_blk(char *buffer
)
238 struct revoke_blk_list
*node
= NULL
;
239 struct ext_filesystem
*fs
= get_fs();
240 if (buffer
== NULL
) {
241 printf("buffer ptr is NULL\n");
246 printf("_get_node: malloc failed\n");
250 node
->content
= zalloc(fs
->blksz
);
251 if (node
->content
== NULL
)
253 memcpy(node
->content
, buffer
, fs
->blksz
);
255 if (first_node
== true) {
256 revk_blk_list
= node
;
260 prev_node
->next
= node
;
265 void ext4fs_free_revoke_blks(void)
267 struct revoke_blk_list
*tmp_node
= revk_blk_list
;
268 struct revoke_blk_list
*next_node
= NULL
;
270 while (tmp_node
!= NULL
) {
271 if (tmp_node
->content
)
272 free(tmp_node
->content
);
273 tmp_node
= tmp_node
->next
;
276 tmp_node
= revk_blk_list
;
277 while (tmp_node
!= NULL
) {
278 next_node
= tmp_node
->next
;
280 tmp_node
= next_node
;
283 revk_blk_list
= NULL
;
288 int check_blknr_for_revoke(long int blknr
, int sequence_no
)
290 struct journal_revoke_header_t
*header
;
295 struct revoke_blk_list
*tmp_revk_node
= revk_blk_list
;
296 while (tmp_revk_node
!= NULL
) {
297 revk_blk
= tmp_revk_node
->content
;
299 header
= (struct journal_revoke_header_t
*) revk_blk
;
300 if (sequence_no
< be32_to_cpu(header
->r_header
.h_sequence
)) {
301 offset
= sizeof(struct journal_revoke_header_t
);
302 max
= be32_to_cpu(header
->r_count
);
304 while (offset
< max
) {
305 blocknr
= be32_to_cpu(*((__be32
*)
306 (revk_blk
+ offset
)));
307 if (blocknr
== blknr
)
312 tmp_revk_node
= tmp_revk_node
->next
;
322 * This function parses the journal blocks and replays the
323 * suceessful transactions. A transaction is successfull
324 * if commit block is found for a descriptor block
325 * The tags in descriptor block contain the disk block
326 * numbers of the metadata to be replayed
328 void recover_transaction(int prev_desc_logical_no
)
330 struct ext2_inode inode_journal
;
331 struct ext_filesystem
*fs
= get_fs();
332 struct journal_header_t
*jdb
;
337 struct ext3_journal_block_tag
*tag
;
338 char *temp_buff
= zalloc(fs
->blksz
);
339 char *metadata_buff
= zalloc(fs
->blksz
);
340 if (!temp_buff
|| !metadata_buff
)
342 i
= prev_desc_logical_no
;
343 ext4fs_read_inode(ext4fs_root
, EXT2_JOURNAL_INO
,
344 (struct ext2_inode
*)&inode_journal
);
345 blknr
= read_allocated_block((struct ext2_inode
*)
347 ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
, 0, fs
->blksz
,
349 p_jdb
= (char *)temp_buff
;
350 jdb
= (struct journal_header_t
*) temp_buff
;
351 ofs
= sizeof(struct journal_header_t
);
354 tag
= (struct ext3_journal_block_tag
*)&p_jdb
[ofs
];
355 ofs
+= sizeof(struct ext3_journal_block_tag
);
360 flags
= be32_to_cpu(tag
->flags
);
361 if (!(flags
& EXT3_JOURNAL_FLAG_SAME_UUID
))
365 debug("\t\ttag %u\n", be32_to_cpu(tag
->block
));
366 if (revk_blk_list
!= NULL
) {
367 if (check_blknr_for_revoke(be32_to_cpu(tag
->block
),
368 be32_to_cpu(jdb
->h_sequence
)) == 0)
371 blknr
= read_allocated_block(&inode_journal
, i
);
372 ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
, 0,
373 fs
->blksz
, metadata_buff
);
374 put_ext4((uint64_t)((uint64_t)be32_to_cpu(tag
->block
) * (uint64_t)fs
->blksz
),
375 metadata_buff
, (uint32_t) fs
->blksz
);
376 } while (!(flags
& EXT3_JOURNAL_FLAG_LAST_TAG
));
382 void print_jrnl_status(int recovery_flag
)
384 if (recovery_flag
== RECOVER
)
385 printf("Journal Recovery Completed\n");
387 printf("Journal Scan Completed\n");
390 int ext4fs_check_journal_state(int recovery_flag
)
395 int transaction_state
= TRANSACTION_COMPLETE
;
396 int prev_desc_logical_no
= 0;
397 int curr_desc_logical_no
= 0;
399 struct ext2_inode inode_journal
;
400 struct journal_superblock_t
*jsb
= NULL
;
401 struct journal_header_t
*jdb
= NULL
;
403 struct ext3_journal_block_tag
*tag
= NULL
;
404 char *temp_buff
= NULL
;
405 char *temp_buff1
= NULL
;
406 struct ext_filesystem
*fs
= get_fs();
408 temp_buff
= zalloc(fs
->blksz
);
411 temp_buff1
= zalloc(fs
->blksz
);
417 ext4fs_read_inode(ext4fs_root
, EXT2_JOURNAL_INO
, &inode_journal
);
418 blknr
= read_allocated_block(&inode_journal
, EXT2_JOURNAL_SUPERBLOCK
);
419 ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
, 0, fs
->blksz
,
421 jsb
= (struct journal_superblock_t
*) temp_buff
;
423 if (le32_to_cpu(fs
->sb
->feature_incompat
) & EXT3_FEATURE_INCOMPAT_RECOVER
) {
424 if (recovery_flag
== RECOVER
)
425 printf("Recovery required\n");
427 if (recovery_flag
== RECOVER
)
428 printf("File System is consistent\n");
432 if (be32_to_cpu(jsb
->s_start
) == 0)
435 if (!(jsb
->s_feature_compat
&
436 cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM
)))
437 jsb
->s_feature_compat
|=
438 cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM
);
440 i
= be32_to_cpu(jsb
->s_first
);
442 blknr
= read_allocated_block(&inode_journal
, i
);
443 memset(temp_buff1
, '\0', fs
->blksz
);
444 ext4fs_devread((lbaint_t
)blknr
* fs
->sect_perblk
,
445 0, fs
->blksz
, temp_buff1
);
446 jdb
= (struct journal_header_t
*) temp_buff1
;
448 if (be32_to_cpu(jdb
->h_blocktype
) ==
449 EXT3_JOURNAL_DESCRIPTOR_BLOCK
) {
450 if (be32_to_cpu(jdb
->h_sequence
) !=
451 be32_to_cpu(jsb
->s_sequence
)) {
452 print_jrnl_status(recovery_flag
);
456 curr_desc_logical_no
= i
;
457 if (transaction_state
== TRANSACTION_COMPLETE
)
458 transaction_state
= TRANSACTION_RUNNING
;
461 p_jdb
= (char *)temp_buff1
;
462 ofs
= sizeof(struct journal_header_t
);
464 tag
= (struct ext3_journal_block_tag
*)
466 ofs
+= sizeof(struct ext3_journal_block_tag
);
469 flags
= be32_to_cpu(tag
->flags
);
470 if (!(flags
& EXT3_JOURNAL_FLAG_SAME_UUID
))
473 debug("\t\ttag %u\n", be32_to_cpu(tag
->block
));
474 } while (!(flags
& EXT3_JOURNAL_FLAG_LAST_TAG
));
477 } else if (be32_to_cpu(jdb
->h_blocktype
) ==
478 EXT3_JOURNAL_COMMIT_BLOCK
) {
479 if (be32_to_cpu(jdb
->h_sequence
) !=
480 be32_to_cpu(jsb
->s_sequence
)) {
481 print_jrnl_status(recovery_flag
);
485 if (transaction_state
== TRANSACTION_RUNNING
||
487 transaction_state
= TRANSACTION_COMPLETE
;
490 cpu_to_be32(be32_to_cpu(
491 jsb
->s_sequence
) + 1);
493 prev_desc_logical_no
= curr_desc_logical_no
;
494 if ((recovery_flag
== RECOVER
) && (DB_FOUND
== YES
))
495 recover_transaction(prev_desc_logical_no
);
498 } else if (be32_to_cpu(jdb
->h_blocktype
) ==
499 EXT3_JOURNAL_REVOKE_BLOCK
) {
500 if (be32_to_cpu(jdb
->h_sequence
) !=
501 be32_to_cpu(jsb
->s_sequence
)) {
502 print_jrnl_status(recovery_flag
);
505 if (recovery_flag
== SCAN
)
506 ext4fs_push_revoke_blk((char *)jdb
);
509 debug("Else Case\n");
510 if (be32_to_cpu(jdb
->h_sequence
) !=
511 be32_to_cpu(jsb
->s_sequence
)) {
512 print_jrnl_status(recovery_flag
);
519 if (recovery_flag
== RECOVER
) {
520 uint32_t new_feature_incompat
;
521 jsb
->s_start
= cpu_to_be32(1);
522 jsb
->s_sequence
= cpu_to_be32(be32_to_cpu(jsb
->s_sequence
) + 1);
523 /* get the superblock */
524 ext4_read_superblock((char *)fs
->sb
);
525 new_feature_incompat
= le32_to_cpu(fs
->sb
->feature_incompat
);
526 new_feature_incompat
|= EXT3_FEATURE_INCOMPAT_RECOVER
;
527 fs
->sb
->feature_incompat
= cpu_to_le32(new_feature_incompat
);
529 /* Update the super block */
530 put_ext4((uint64_t) (SUPERBLOCK_SIZE
),
531 (struct ext2_sblock
*)fs
->sb
,
532 (uint32_t) SUPERBLOCK_SIZE
);
533 ext4_read_superblock((char *)fs
->sb
);
535 blknr
= read_allocated_block(&inode_journal
,
536 EXT2_JOURNAL_SUPERBLOCK
);
537 put_ext4((uint64_t) ((uint64_t)blknr
* (uint64_t)fs
->blksz
),
538 (struct journal_superblock_t
*)temp_buff
,
539 (uint32_t) fs
->blksz
);
540 ext4fs_free_revoke_blks();
548 static void update_descriptor_block(long int blknr
)
552 struct journal_header_t jdb
;
553 struct ext3_journal_block_tag tag
;
554 struct ext2_inode inode_journal
;
555 struct journal_superblock_t
*jsb
= NULL
;
558 struct ext_filesystem
*fs
= get_fs();
559 char *temp_buff
= zalloc(fs
->blksz
);
563 ext4fs_read_inode(ext4fs_root
, EXT2_JOURNAL_INO
, &inode_journal
);
564 jsb_blknr
= read_allocated_block(&inode_journal
,
565 EXT2_JOURNAL_SUPERBLOCK
);
566 ext4fs_devread((lbaint_t
)jsb_blknr
* fs
->sect_perblk
, 0, fs
->blksz
,
568 jsb
= (struct journal_superblock_t
*) temp_buff
;
570 jdb
.h_blocktype
= cpu_to_be32(EXT3_JOURNAL_DESCRIPTOR_BLOCK
);
571 jdb
.h_magic
= cpu_to_be32(EXT3_JOURNAL_MAGIC_NUMBER
);
572 jdb
.h_sequence
= jsb
->s_sequence
;
573 buf
= zalloc(fs
->blksz
);
579 memcpy(buf
, &jdb
, sizeof(struct journal_header_t
));
580 temp
+= sizeof(struct journal_header_t
);
582 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
583 if (journal_ptr
[i
]->blknr
== -1)
586 tag
.block
= cpu_to_be32(journal_ptr
[i
]->blknr
);
587 tag
.flags
= cpu_to_be32(EXT3_JOURNAL_FLAG_SAME_UUID
);
588 memcpy(temp
, &tag
, sizeof(struct ext3_journal_block_tag
));
589 temp
= temp
+ sizeof(struct ext3_journal_block_tag
);
592 tag
.block
= cpu_to_be32(journal_ptr
[--i
]->blknr
);
593 tag
.flags
= cpu_to_be32(EXT3_JOURNAL_FLAG_LAST_TAG
);
594 memcpy(temp
- sizeof(struct ext3_journal_block_tag
), &tag
,
595 sizeof(struct ext3_journal_block_tag
));
596 put_ext4((uint64_t) ((uint64_t)blknr
* (uint64_t)fs
->blksz
), buf
, (uint32_t) fs
->blksz
);
602 static void update_commit_block(long int blknr
)
604 struct journal_header_t jdb
;
605 struct ext_filesystem
*fs
= get_fs();
607 struct ext2_inode inode_journal
;
608 struct journal_superblock_t
*jsb
;
610 char *temp_buff
= zalloc(fs
->blksz
);
614 ext4fs_read_inode(ext4fs_root
, EXT2_JOURNAL_INO
,
616 jsb_blknr
= read_allocated_block(&inode_journal
,
617 EXT2_JOURNAL_SUPERBLOCK
);
618 ext4fs_devread((lbaint_t
)jsb_blknr
* fs
->sect_perblk
, 0, fs
->blksz
,
620 jsb
= (struct journal_superblock_t
*) temp_buff
;
622 jdb
.h_blocktype
= cpu_to_be32(EXT3_JOURNAL_COMMIT_BLOCK
);
623 jdb
.h_magic
= cpu_to_be32(EXT3_JOURNAL_MAGIC_NUMBER
);
624 jdb
.h_sequence
= jsb
->s_sequence
;
625 buf
= zalloc(fs
->blksz
);
630 memcpy(buf
, &jdb
, sizeof(struct journal_header_t
));
631 put_ext4((uint64_t) ((uint64_t)blknr
* (uint64_t)fs
->blksz
), buf
, (uint32_t) fs
->blksz
);
637 void ext4fs_update_journal(void)
639 struct ext2_inode inode_journal
;
640 struct ext_filesystem
*fs
= get_fs();
643 ext4fs_read_inode(ext4fs_root
, EXT2_JOURNAL_INO
, &inode_journal
);
644 blknr
= read_allocated_block(&inode_journal
, jrnl_blk_idx
++);
645 update_descriptor_block(blknr
);
646 for (i
= 0; i
< MAX_JOURNAL_ENTRIES
; i
++) {
647 if (journal_ptr
[i
]->blknr
== -1)
649 blknr
= read_allocated_block(&inode_journal
, jrnl_blk_idx
++);
650 put_ext4((uint64_t) ((uint64_t)blknr
* (uint64_t)fs
->blksz
),
651 journal_ptr
[i
]->buf
, fs
->blksz
);
653 blknr
= read_allocated_block(&inode_journal
, jrnl_blk_idx
++);
654 update_commit_block(blknr
);
655 printf("update journal finished\n");