1 .. SPDX-License-Identifier: GPL-2.0
6 Introduced in ext3, the ext4 filesystem employs a journal to protect the
7 filesystem against corruption in the case of a system crash. A small
8 continuous region of disk (default 128MiB) is reserved inside the
9 filesystem as a place to land “important” data writes on-disk as quickly
10 as possible. Once the important data transaction is fully written to the
11 disk and flushed from the disk write cache, a record of the data being
12 committed is also written to the journal. At some later point in time,
13 the journal code writes the transactions to their final locations on
14 disk (this could involve a lot of seeking or a lot of small
15 read-write-erases) before erasing the commit record. Should the system
16 crash during the second slow write, the journal can be replayed all the
17 way to the latest commit record, guaranteeing the atomicity of whatever
18 gets written through the journal to the disk. The effect of this is to
19 guarantee that the filesystem does not become stuck midway through a
22 For performance reasons, ext4 by default only writes filesystem metadata
23 through the journal. This means that file data blocks are /not/
24 guaranteed to be in any consistent state after a crash. If this default
25 guarantee level (``data=ordered``) is not satisfactory, there is a mount
26 option to control journal behavior. If ``data=journal``, all data and
27 metadata are written to disk through the journal. This is slower but
28 safest. If ``data=writeback``, dirty data blocks are not flushed to the
29 disk before the metadata are written to disk through the journal.
31 The journal inode is typically inode 8. The first 68 bytes of the
32 journal inode are replicated in the ext4 superblock. The journal itself
33 is normal (but hidden) file within the filesystem. The file usually
34 consumes an entire block group, though mke2fs tries to put it in the
37 All fields in jbd2 are written to disk in big-endian order. This is the
40 NOTE: Both ext4 and ocfs2 use jbd2.
42 The maximum size of a journal embedded in an ext4 filesystem is 2^32
43 blocks. jbd2 itself does not seem to care.
48 Generally speaking, the journal has this format:
55 - descriptor\_block (data\_blocks or revocation\_block) [more data or
56 revocations] commmit\_block
57 - [more transactions...]
62 Notice that a transaction begins with either a descriptor and some data,
63 or a block revocation list. A finished transaction always ends with a
64 commit. If there is no commit record (or the checksums don't match), the
65 transaction will be discarded during replay.
70 Optionally, an ext4 filesystem can be created with an external journal
71 device (as opposed to an internal journal, which uses a reserved inode).
72 In this case, on the filesystem device, ``s_journal_inum`` should be
73 zero and ``s_journal_uuid`` should be set. On the journal device there
74 will be an ext4 super block in the usual place, with a matching UUID.
75 The journal superblock will be in the next full block after the
82 * - 1024 bytes of padding
85 - descriptor\_block (data\_blocks or revocation\_block) [more data or
86 revocations] commmit\_block
87 - [more transactions...]
97 Every block in the journal starts with a common 12-byte header
98 ``struct journal_header_s``:
111 - jbd2 magic number, 0xC03B3998.
115 - Description of what this block contains. See the jbd2_blocktype_ table
120 - The transaction ID that goes with this block.
124 The journal block type can be any one of:
133 - Descriptor. This block precedes a series of data blocks that were
134 written through the journal during a transaction.
136 - Block commit record. This block signifies the completion of a
139 - Journal superblock, v1.
141 - Journal superblock, v2.
143 - Block revocation records. This speeds up recovery by enabling the
144 journal to skip writing blocks that were subsequently rewritten.
149 The super block for the journal is much simpler as compared to ext4's.
150 The key data kept within are size of the journal, and where to find the
151 start of the log of transactions.
153 The journal superblock is recorded as ``struct journal_superblock_s``,
154 which is 1024 bytes long:
167 - Static information describing the journal.
169 - journal\_header\_t (12 bytes)
171 - Common header identifying this as a superblock.
175 - Journal device block size.
179 - Total number of blocks in this journal.
183 - First block of log information.
187 - Dynamic information describing the current state of the log.
191 - First commit ID expected in log.
195 - Block number of the start of log. Contrary to the comments, this field
196 being zero does not imply that the journal is clean!
200 - Error value, as set by jbd2\_journal\_abort().
204 - The remaining fields are only valid in a v2 superblock.
207 - s\_feature\_compat;
208 - Compatible feature set. See the table jbd2_compat_ below.
211 - s\_feature\_incompat
212 - Incompatible feature set. See the table jbd2_incompat_ below.
215 - s\_feature\_ro\_compat
216 - Read-only compatible feature set. There aren't any of these currently.
220 - 128-bit uuid for journal. This is compared against the copy in the ext4
221 super block at mount time.
225 - Number of file systems sharing this journal.
229 - Location of dynamic super block copy. (Not used?)
232 - s\_max\_transaction
233 - Limit of journal blocks per transaction. (Not used?)
236 - s\_max\_trans\_data
237 - Limit of data blocks per transaction. (Not used?)
241 - Checksum algorithm used for the journal. See jbd2_checksum_type_ for
254 - Checksum of the entire superblock, with this field set to zero.
258 - ids of all file systems sharing the log. e2fsprogs/Linux don't allow
259 shared external journals, but I imagine Lustre (or ocfs2?), which use
260 the jbd2 code, might.
264 The journal compat features are any combination of the following:
273 - Journal maintains checksums on the data blocks.
274 (JBD2\_FEATURE\_COMPAT\_CHECKSUM)
278 The journal incompat features are any combination of the following:
287 - Journal has block revocation records. (JBD2\_FEATURE\_INCOMPAT\_REVOKE)
289 - Journal can deal with 64-bit block numbers.
290 (JBD2\_FEATURE\_INCOMPAT\_64BIT)
292 - Journal commits asynchronously. (JBD2\_FEATURE\_INCOMPAT\_ASYNC\_COMMIT)
294 - This journal uses v2 of the checksum on-disk format. Each journal
295 metadata block gets its own checksum, and the block tags in the
296 descriptor table contain checksums for each of the data blocks in the
297 journal. (JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2)
299 - This journal uses v3 of the checksum on-disk format. This is the same as
300 v2, but the journal block tag size is fixed regardless of the size of
301 block numbers. (JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3)
303 .. _jbd2_checksum_type:
305 Journal checksum type codes are one of the following. crc32 or crc32c are the
326 The descriptor block contains an array of journal block tags that
327 describe the final locations of the data blocks that follow in the
328 journal. Descriptor blocks are open-coded instead of being completely
329 described by a data structure, but here is the block structure anyway.
330 Descriptor blocks consume at least 36 bytes, but use a full block:
343 - Common block header.
345 - struct journal\_block\_tag\_s
347 - Enough tags either to fill up the block or to describe all the data
348 blocks that follow this descriptor block.
350 Journal block tags have any of the following formats, depending on which
351 journal feature and block tag flags are set.
353 If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 is set, the journal block tag is
354 defined as ``struct journal_block_tag3_s``, which looks like the
355 following. The size is 16 or 32 bytes.
368 - Lower 32-bits of the location of where the corresponding data block
369 should end up on disk.
373 - Flags that go with the descriptor. See the table jbd2_tag_flags_ for
378 - Upper 32-bits of the location of where the corresponding data block
379 should end up on disk. This is zero if JBD2\_FEATURE\_INCOMPAT\_64BIT is
384 - Checksum of the journal UUID, the sequence number, and the data block.
388 - This field appears to be open coded. It always comes at the end of the
389 tag, after t_checksum. This field is not present if the "same UUID" flag
394 - A UUID to go with this tag. This field appears to be copied from the
395 ``j_uuid`` field in ``struct journal_s``, but only tune2fs touches that
400 The journal tag flags are any combination of the following:
409 - On-disk block is escaped. The first four bytes of the data block just
410 happened to match the jbd2 magic number.
412 - This block has the same UUID as previous, therefore the UUID field is
415 - The data block was deleted by the transaction. (Not used?)
417 - This is the last tag in this descriptor block.
419 If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 is NOT set, the journal block tag
420 is defined as ``struct journal_block_tag_s``, which looks like the
421 following. The size is 8, 12, 24, or 28 bytes:
434 - Lower 32-bits of the location of where the corresponding data block
435 should end up on disk.
439 - Checksum of the journal UUID, the sequence number, and the data block.
440 Note that only the lower 16 bits are stored.
444 - Flags that go with the descriptor. See the table jbd2_tag_flags_ for
449 - This next field is only present if the super block indicates support for
450 64-bit block numbers.
454 - Upper 32-bits of the location of where the corresponding data block
455 should end up on disk.
459 - This field appears to be open coded. It always comes at the end of the
460 tag, after t_flags or t_blocknr_high. This field is not present if the
461 "same UUID" flag is set.
465 - A UUID to go with this tag. This field appears to be copied from the
466 ``j_uuid`` field in ``struct journal_s``, but only tune2fs touches that
469 If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2 or
470 JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 are set, the end of the block is a
471 ``struct jbd2_journal_block_tail``, which looks like this:
484 - Checksum of the journal UUID + the descriptor block, with this field set
490 In general, the data blocks being written to disk through the journal
491 are written verbatim into the journal file after the descriptor block.
492 However, if the first four bytes of the block match the jbd2 magic
493 number then those four bytes are replaced with zeroes and the “escaped”
494 flag is set in the descriptor block tag.
499 A revocation block is used to prevent replay of a block in an earlier
500 transaction. This is used to mark blocks that were journalled at one
501 time but are no longer journalled. Typically this happens if a metadata
502 block is freed and re-allocated as a file data block; in this case, a
503 journal replay after the file block was written to disk will cause
506 **NOTE**: This mechanism is NOT used to express “this journal block is
507 superseded by this other journal block”, as the author (djwong)
508 mistakenly thought. Any block being added to a transaction will cause
509 the removal of all existing revocation records for that block.
511 Revocation blocks are described in
512 ``struct jbd2_journal_revoke_header_s``, are at least 16 bytes in
513 length, but use a full block:
526 - Common block header.
530 - Number of bytes used in this block.
532 - \_\_be32 or \_\_be64
536 After r\_count is a linear array of block numbers that are effectively
537 revoked by this transaction. The size of each block number is 8 bytes if
538 the superblock advertises 64-bit block number support, or 4 bytes
541 If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2 or
542 JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 are set, the end of the revocation
543 block is a ``struct jbd2_journal_revoke_tail``, which has this format:
556 - Checksum of the journal UUID + revocation block
561 The commit block is a sentry that indicates that a transaction has been
562 completely written to the journal. Once this commit block reaches the
563 journal, the data stored with this transaction can be written to their
564 final locations on disk.
566 The commit block is described by ``struct commit_header``, which is 32
567 bytes long (but uses a full block):
580 - Common block header.
584 - The type of checksum to use to verify the integrity of the data blocks
585 in the transaction. See jbd2_checksum_type_ for more info.
589 - The number of bytes used by the checksum. Most likely 4.
596 - h\_chksum[JBD2\_CHECKSUM\_BYTES]
597 - 32 bytes of space to store checksums. If
598 JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2 or JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3
599 are set, the first ``__be32`` is the checksum of the journal UUID and
600 the entire commit block, with this field zeroed. If
601 JBD2\_FEATURE\_COMPAT\_CHECKSUM is set, the first ``__be32`` is the
602 crc32 of all the blocks already written to the transaction.
606 - The time that the transaction was committed, in seconds since the epoch.
610 - Nanoseconds component of the above timestamp.