In the previous article, we introduced the Ext3 file system log as a file that is managed by JBD. This naturally leads to the following questions:
1) How do I locate ext3 log files and view the bare data of log files?
2) How is the ext3 log file data physically laid out?
3) JBD How to use log file for data recovery?
Take these questions and embark on this journey of discovery.
JBD Log Location
First find the inode for the ext3 log file
/#/DUMPE2FS/DEV/SDA1DUMPE2FS 1.42 (29-nov-2011) Filesystem volume name:shouxian<none>last mounted on: <not Available>filesystem Uuid:513c186a-108b-4840-ad98-aa8e8f62d4c8filesystem Magic Number:0xef53filesys TEM Revision #: 1 (dynamic) Filesystem features:has_journal ext_attr resize_inode dir_index filetype sparse_super Large_filefilesystem Flags:signed_directory_hashdefault Mount Options:user_xattr aclfilesystem State: Cleanerrors Behavior:continuefilesystem OS type:linuxinode count:513072block Count: 2048276Reserved Block Count:102413free blocks:1978758free Inodes:513061first block : 0Block size:4096fragment size:4096reserved GDT blocks:500blocks per group: 32768Fragments per group:32768inodes per Group:8144inode blocks per Group:509filesystem created : Sat Mar 21 11:39: 2015Last Mount Time:n/alast write Time:sat Mar 11:39:14 2015Mount count:0maximum Mount Count: -1last checked:sat Mar 11:39:13 2015Check interval:0 (<none>) Reserved b Locks uid:0 (user root) Reserved blocks gid:0 (group root) First Inode:11inode size: 256Required extra isize:28desired extra isize:28journal Inode:8default directory hash:half_md4d Irectory Hash seed:08091c3c-7a35-4f5b-8477-7af597c5eb50journal backup:inode blocksjournal Features: (none) Journal size:128mjournal length:32768journal sequence:0x00000001journal start: 0
and find the data block where the inode resides.
#/DEBUGFS-R ' IMAP <8> '/dev/sda1debugfs 1.42 (29-nov-2011) Inode 8 is part of the block group 0 located at block 50 4, offset 0x0700
The physical offset (in 256B) of the data block in which the Inode resides is calculated based on the block number, block size, and offset.
>>> (504*4096+0x700)/256.8071.0
read out this inode data.
# dd if=/dev/sda1 of=inode.8 bs=256 count=1 skip=80711+0 Records in1+0 records out256 bytes (256B) copied, 0.000955 second S, 261.8kb/s/usr/lib # hexdump inode.80000000 8081 0000 0000 0008 6258 0d55 6258 0d550000010 6258 0d55 0000 0000 0000 0100 0801 04000000020 0000 0000 0000 0000 ff01 0f00 0002 0f000000030 0102 0f00 0202 0f00 0302 0f00 0402 0f000000040 0502 0f00 0602 0f00 0702 0f00 0802 0f000000050 0902 0f00 0a02 0f00 0b02 0f00 0c06 0f000000060 0000 0000 0000 0000 0000 0000 0000 000 0*0000080 1c00 0000 0000 0000 0000 0000 0000 00000000090 6258 0d55 0000 0000 0000 0000 0000 000000000a0 0000 0000 0000 000 0 0000 0000 0000 0000*0000100
Control the Inode data structure, find its managed block number (Blue flag), the first 12 map slots are direct mapping, directly managed block numbers from 0x0f01ff (983551) to 0x0f020b, belong to group 30.
Group: (Blocks 983040-1015807) Block bitmap at 983040 (+0), Inode bitmap at 983041 (+1) inode table at 983042-9 83550 (+2) 0 free blocks, 8144 free inodes, 0 directories free blocks:
At this point, you can finally dump the contents of the log file data block.
# dd if=/dev/sda1 of=inode.8.data bs=4096 count=12 skip=9835511+0 Records in1+0 records out4096 bytes (4.0KB) copied, 0.00 0942 seconds, 4.1mb/s0000000 c03b 3998 0000 0004 0000 0000 0000 10000000010 0000 8000 0000 0001 0000 0002 0000 00010000020 0000 0000 0000 0000 0000 0000 0000 00000000030 513c 186a 108b 4840 ad98 aa8e 8f62 d4c80000040 0000 0001 0000 0000 0000 00 00 0000 00000000050 0000 0000 0000 0000 0000 0000 0000 0000*0001000 c03b 3998 0000 0001 0000 0002 0000 01f80001010 0000 00 08 0000 0000 0000 0000 0000 00000001020 0000 0000 0000 0000 0000 0000 0000 0000*0002000 0000 0000 0000 0000 6158 0d55 6158 0d550002010 6158 0d55 0000 0000 0000 0000 0000 00000002020 0000 0000 0000 0000 0000 0000 0000 0000*0002100 ed41 0000 0010 0000 d75d 0d55 6158 0d55 0002110 6158 0d55 0000 0000 0000 0300 0800 00000002120 0000 0000 0000 0000 f503 0000 0000 000 00002130 0000 0000 0000 0000 0000 0000 0000 0000*0002180 1c00 0000 0000 0000 0000 0000 0000 00000002190 6158 0d55 0000 000 0 0000 0000) 0000 000000021a0 0000 0000 0000 0000 0000 0000 0000 0000*0002600 8081 0000 00c0 4000 6158 0d55 6158 0d550002610 6158 0d55 0000 0000 00 00 0100 a88c 00000002620 0000 0000 0000 0000 0000 0000 0000 0000*0002650 0000 0000 0000 0000 0000 0000 fa03 00000002660 00 xx 0000 0000 0000 0000 0000 0100 00000002670 0000 0000 0000 0000 0000 0000 0000 00000002680 1c00 0000 0000 0000 0000 0000 0000 00000002690 6158 0d55 0000 0000 0000 0000 0000 000000026a0 0000 0000 0000 0000 0000 0000 0000 0000*0002700 8081 0000 0000 0008 6258 0d55 6258 0d550002710 6258 0d55 0000 0000 0000 0100 0801 04000002720 0000 0000 0000 0000 ff01 0f00 0002 0f0 00002730 0102 0f00 0202 0f00 0302 0f00 0402 0f000002740 0502 0f00 0602 0f00 0702 0f00 0802 0f000002750 0902 0f00 0a02 0f00 0B02 0f00 0c06 0f000002760 0000 0000 0000 0000 0000 0000 0000 0000*0002780 1c00 0000 0000 0000 0000 0000 0000 00000002790 6258 0d55 0000 0000 0000 0000 0000 000000027a0 0000 0000 0000 0000 0000 0000 0000 0000*0002a00 c041 0000 0040 0000 6158 0 D55 6158 0d550002a10 6158 0d55 0000 0000 0000 0200 0000 00000002a20 0000 0000 0000 0000 f603 0000 f703 00000002a30 f803 0000 f903 0000 0000 0 000 00000002a40 0000 0000 0000 0000 0000 0000 0000 0000*0002a80 1c00 0000 0000 0000 0000 0000 0000 00000002a90 6158 0d55 0 0000 0000 0000 0000 00000002aa0 0000 0000 0000 0000 0000 0000 0000 0000*0003000 c03b 3998 0000 0002 0000 0002 0000 000 0 0003010 0000 0000 0000 0000 0000 0000 0000 0000* Below
JBD Log Analysis
0000000--0001000 data blocks:
Log Super block segment, storage is journal_superblock_t data, from which you can see the block type is JFS_SUPERBLOCK_V2 (4), log block size: 0x1000; Number of log blocks: 0x8000. The total log size can then be calculated as: 0x1000 * 0x8000 = 128M.
/** standard headers for all descriptor blocks:*/typedef struct JOURNAL_HEADER_S{__BE32H_MAGIC;__BE32H_BLOCKTYPE;__ Be32h_sequence;} journal_header_t;/* * The journal Superblock. All fields is in Big-endian byte order. */typedef struct journal_superblock_s{/* 0x0000 */journal_header_t s_header;/* 0x000c *//* Static Information describing The journal */__be32s_blocksize;/* Journal device blocksize */__be32s_maxlen;/* total blocks in Journal file */__be32s_fir st;/* first block of log information *//* 0x0018 *//* Dynamic Information describing the current state of the log */__be32 s_sequence;/* first commit ID expected in log */__be32s_start;/* BLOCKNR of start of log *//* 0x0020 *//* Error value, as Set by Journal_abort (). */__be32s_errno;/* 0x0024 *//* Remaining fields is only valid in a version-2 superblock */__be32s_feature_compat;/* compa Tible feature set */__be32s_feature_incompat;/* incompatible feature set */__be32s_feature_ro_compat;/* Readonly-compatible Feature set *//* 0x0030*/__u8s_uuid[16];/* 128-bit uuid for journal *//* 0x0040 */__be32s_nr_users;/* nr of filesystems sharing log */__be32s_dyn super;/* BLOCKNR of dynamic superblock copy*//* 0x0048 */__be32s_max_transaction;/* Limit of journal blocks per trans.*/__ be32s_max_trans_data;/* Limit of data blocks per trans. *//* 0x0050 */__u32s_padding[44];/* 0x0100 */__u8s_users[16*48];/ * IDs of all FS ' es sharing the log *//* 0x0400 */} journal_superblock_t;
0001000--0002000 data blocks:
The log descriptor block, at the beginning, stores the journal_header_t, followed by the journal_block_tag_t data table. From this you can see that the block type is Jfs_descriptor_block (1) and the transaction ID is 2. Only one Tag,tag block number is 0x1f8, and FLAG is Jfs_flag_last_tag (8). Additionally, the first tag is followed by an additional 16-byte j_uuid, with the same UUID tag labeled Jfs_flag_same_uuid (2), and the last tag FLAG marked with the Jfs_flag_last_tag position.
/* * The block tag:used to describe a single buffer in the journal */typedef struct journal_block_tag_s{__be32t_blocknr;/ * The On-disk block number */__be32t_flags;/* see below */} journal_block_tag_t;
0002000--0003000 data blocks:
Meta-data block, followed by Descriptor_block, a tag corresponding to a block, the number of blocks is determined by the number of tags. There is only one tag in this example, so there is only one block of data. The data in this block is written to the real physical block of the storage device's file system (that is, the tag's T_BLOCKNR) when the log is restored or journal load.
0003000--0004000 data blocks:
The log submission block. Represents a complete transaction. You can see that the block type is Jfs_commit_block (2), the transaction ID is 2, and the previous log description block matches.
JBD Log Recovery
If the file system crashes or the unclean Umount restarts causing file system corruption, when the log file system is mount again, the Ext3 file system will perform a log recovery action based on the JBD log file.
JBD Log recovery is divided into 3 stages:
The first phase of Pass_scan:
Complete log consistency check, find the tail of the log, get start_transaction and End_transaction ID and other information;
The second phase of Pass_revoke:
Search Revoke block (revoke block in the storage needs to discard the block number), statistics nr_revokes number, constructs revoke block hash table, avoids the replay revoke block, destroys the data block content, causes the file system corruption;
The third phase of Pass_replay:
Replay the data blocks that meet the criteria in the log, and copy the data blocks in the log to the real location of the file system:
1) block number is not in the REVOKE block hash table;
2) the description block containing the data block commit ID is greater than the commit ID of the revoke block, that is, after the revoke block;
Then set the j_transaction_sequence of the log as the next transaction ID to ensure that the log records already exist in the log are invalid;
Empty the REVOKE block list, synchronize the storage device, and ext3 the log to complete the recovery.
--eof--
JBD log location, analysis, and recovery