1 ext4: import journal chapter from wiki page
3 From: Darrick J. Wong <darrick.wong@oracle.com>
5 Import the chapter about the journal from the on-disk format wiki
6 page into the kernel documentation.
8 Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
9 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
11 Documentation/filesystems/ext4/ondisk/globals.rst | 1
12 Documentation/filesystems/ext4/ondisk/journal.rst | 611 +++++++++++++++++++++
13 2 files changed, 612 insertions(+)
14 create mode 100644 Documentation/filesystems/ext4/ondisk/journal.rst
17 diff --git a/Documentation/filesystems/ext4/ondisk/globals.rst b/Documentation/filesystems/ext4/ondisk/globals.rst
18 index fe6e107bf515..368bf7662b96 100644
19 --- a/Documentation/filesystems/ext4/ondisk/globals.rst
20 +++ b/Documentation/filesystems/ext4/ondisk/globals.rst
21 @@ -10,3 +10,4 @@ have static metadata at fixed locations.
22 .. include:: group_descr.rst
23 .. include:: bitmaps.rst
25 +.. include:: journal.rst
26 diff --git a/Documentation/filesystems/ext4/ondisk/journal.rst b/Documentation/filesystems/ext4/ondisk/journal.rst
28 index 000000000000..e7031af86876
30 +++ b/Documentation/filesystems/ext4/ondisk/journal.rst
32 +.. SPDX-License-Identifier: GPL-2.0
37 +Introduced in ext3, the ext4 filesystem employs a journal to protect the
38 +filesystem against corruption in the case of a system crash. A small
39 +continuous region of disk (default 128MiB) is reserved inside the
40 +filesystem as a place to land “important” data writes on-disk as quickly
41 +as possible. Once the important data transaction is fully written to the
42 +disk and flushed from the disk write cache, a record of the data being
43 +committed is also written to the journal. At some later point in time,
44 +the journal code writes the transactions to their final locations on
45 +disk (this could involve a lot of seeking or a lot of small
46 +read-write-erases) before erasing the commit record. Should the system
47 +crash during the second slow write, the journal can be replayed all the
48 +way to the latest commit record, guaranteeing the atomicity of whatever
49 +gets written through the journal to the disk. The effect of this is to
50 +guarantee that the filesystem does not become stuck midway through a
53 +For performance reasons, ext4 by default only writes filesystem metadata
54 +through the journal. This means that file data blocks are /not/
55 +guaranteed to be in any consistent state after a crash. If this default
56 +guarantee level (``data=ordered``) is not satisfactory, there is a mount
57 +option to control journal behavior. If ``data=journal``, all data and
58 +metadata are written to disk through the journal. This is slower but
59 +safest. If ``data=writeback``, dirty data blocks are not flushed to the
60 +disk before the metadata are written to disk through the journal.
62 +The journal inode is typically inode 8. The first 68 bytes of the
63 +journal inode are replicated in the ext4 superblock. The journal itself
64 +is normal (but hidden) file within the filesystem. The file usually
65 +consumes an entire block group, though mke2fs tries to put it in the
68 +All fields in jbd2 are written to disk in big-endian order. This is the
71 +NOTE: Both ext4 and ocfs2 use jbd2.
73 +The maximum size of a journal embedded in an ext4 filesystem is 2^32
74 +blocks. jbd2 itself does not seem to care.
79 +Generally speaking, the journal has this format:
86 + - descriptor\_block (data\_blocks or revocation\_block) [more data or
87 + revocations] commmit\_block
88 + - [more transactions...]
93 +Notice that a transaction begins with either a descriptor and some data,
94 +or a block revocation list. A finished transaction always ends with a
95 +commit. If there is no commit record (or the checksums don't match), the
96 +transaction will be discarded during replay.
101 +Optionally, an ext4 filesystem can be created with an external journal
102 +device (as opposed to an internal journal, which uses a reserved inode).
103 +In this case, on the filesystem device, ``s_journal_inum`` should be
104 +zero and ``s_journal_uuid`` should be set. On the journal device there
105 +will be an ext4 super block in the usual place, with a matching UUID.
106 +The journal superblock will be in the next full block after the
110 + :widths: 1 1 1 1 76
113 + * - 1024 bytes of padding
115 + - Journal Superblock
116 + - descriptor\_block (data\_blocks or revocation\_block) [more data or
117 + revocations] commmit\_block
118 + - [more transactions...]
128 +Every block in the journal starts with a common 12-byte header
129 +``struct journal_header_s``:
142 + - jbd2 magic number, 0xC03B3998.
146 + - Description of what this block contains. See the jbd2_blocktype_ table
151 + - The transaction ID that goes with this block.
155 +The journal block type can be any one of:
164 + - Descriptor. This block precedes a series of data blocks that were
165 + written through the journal during a transaction.
167 + - Block commit record. This block signifies the completion of a
170 + - Journal superblock, v1.
172 + - Journal superblock, v2.
174 + - Block revocation records. This speeds up recovery by enabling the
175 + journal to skip writing blocks that were subsequently rewritten.
180 +The super block for the journal is much simpler as compared to ext4's.
181 +The key data kept within are size of the journal, and where to find the
182 +start of the log of transactions.
184 +The journal superblock is recorded as ``struct journal_superblock_s``,
185 +which is 1024 bytes long:
198 + - Static information describing the journal.
200 + - journal\_header\_t (12 bytes)
202 + - Common header identifying this as a superblock.
206 + - Journal device block size.
210 + - Total number of blocks in this journal.
214 + - First block of log information.
218 + - Dynamic information describing the current state of the log.
222 + - First commit ID expected in log.
226 + - Block number of the start of log. Contrary to the comments, this field
227 + being zero does not imply that the journal is clean!
231 + - Error value, as set by jbd2\_journal\_abort().
235 + - The remaining fields are only valid in a v2 superblock.
238 + - s\_feature\_compat;
239 + - Compatible feature set. See the table jbd2_compat_ below.
242 + - s\_feature\_incompat
243 + - Incompatible feature set. See the table jbd2_incompat_ below.
246 + - s\_feature\_ro\_compat
247 + - Read-only compatible feature set. There aren't any of these currently.
251 + - 128-bit uuid for journal. This is compared against the copy in the ext4
252 + super block at mount time.
256 + - Number of file systems sharing this journal.
260 + - Location of dynamic super block copy. (Not used?)
263 + - s\_max\_transaction
264 + - Limit of journal blocks per transaction. (Not used?)
267 + - s\_max\_trans\_data
268 + - Limit of data blocks per transaction. (Not used?)
271 + - s\_checksum\_type
272 + - Checksum algorithm used for the journal. See jbd2_checksum_type_ for
285 + - Checksum of the entire superblock, with this field set to zero.
289 + - ids of all file systems sharing the log. e2fsprogs/Linux don't allow
290 + shared external journals, but I imagine Lustre (or ocfs2?), which use
291 + the jbd2 code, might.
295 +The journal compat features are any combination of the following:
304 + - Journal maintains checksums on the data blocks.
305 + (JBD2\_FEATURE\_COMPAT\_CHECKSUM)
309 +The journal incompat features are any combination of the following:
318 + - Journal has block revocation records. (JBD2\_FEATURE\_INCOMPAT\_REVOKE)
320 + - Journal can deal with 64-bit block numbers.
321 + (JBD2\_FEATURE\_INCOMPAT\_64BIT)
323 + - Journal commits asynchronously. (JBD2\_FEATURE\_INCOMPAT\_ASYNC\_COMMIT)
325 + - This journal uses v2 of the checksum on-disk format. Each journal
326 + metadata block gets its own checksum, and the block tags in the
327 + descriptor table contain checksums for each of the data blocks in the
328 + journal. (JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2)
330 + - This journal uses v3 of the checksum on-disk format. This is the same as
331 + v2, but the journal block tag size is fixed regardless of the size of
332 + block numbers. (JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3)
334 +.. _jbd2_checksum_type:
336 +Journal checksum type codes are one of the following. crc32 or crc32c are the
337 +most likely choices.
357 +The descriptor block contains an array of journal block tags that
358 +describe the final locations of the data blocks that follow in the
359 +journal. Descriptor blocks are open-coded instead of being completely
360 +described by a data structure, but here is the block structure anyway.
361 +Descriptor blocks consume at least 36 bytes, but use a full block:
372 + - journal\_header\_t
374 + - Common block header.
376 + - struct journal\_block\_tag\_s
377 + - open coded array[]
378 + - Enough tags either to fill up the block or to describe all the data
379 + blocks that follow this descriptor block.
381 +Journal block tags have any of the following formats, depending on which
382 +journal feature and block tag flags are set.
384 +If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 is set, the journal block tag is
385 +defined as ``struct journal_block_tag3_s``, which looks like the
386 +following. The size is 16 or 32 bytes.
399 + - Lower 32-bits of the location of where the corresponding data block
400 + should end up on disk.
404 + - Flags that go with the descriptor. See the table jbd2_tag_flags_ for
409 + - Upper 32-bits of the location of where the corresponding data block
410 + should end up on disk. This is zero if JBD2\_FEATURE\_INCOMPAT\_64BIT is
415 + - Checksum of the journal UUID, the sequence number, and the data block.
419 + - This field appears to be open coded. It always comes at the end of the
420 + tag, after t_checksum. This field is not present if the "same UUID" flag
425 + - A UUID to go with this tag. This field appears to be copied from the
426 + ``j_uuid`` field in ``struct journal_s``, but only tune2fs touches that
431 +The journal tag flags are any combination of the following:
440 + - On-disk block is escaped. The first four bytes of the data block just
441 + happened to match the jbd2 magic number.
443 + - This block has the same UUID as previous, therefore the UUID field is
446 + - The data block was deleted by the transaction. (Not used?)
448 + - This is the last tag in this descriptor block.
450 +If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 is NOT set, the journal block tag
451 +is defined as ``struct journal_block_tag_s``, which looks like the
452 +following. The size is 8, 12, 24, or 28 bytes:
465 + - Lower 32-bits of the location of where the corresponding data block
466 + should end up on disk.
470 + - Checksum of the journal UUID, the sequence number, and the data block.
471 + Note that only the lower 16 bits are stored.
475 + - Flags that go with the descriptor. See the table jbd2_tag_flags_ for
480 + - This next field is only present if the super block indicates support for
481 + 64-bit block numbers.
485 + - Upper 32-bits of the location of where the corresponding data block
486 + should end up on disk.
490 + - This field appears to be open coded. It always comes at the end of the
491 + tag, after t_flags or t_blocknr_high. This field is not present if the
492 + "same UUID" flag is set.
496 + - A UUID to go with this tag. This field appears to be copied from the
497 + ``j_uuid`` field in ``struct journal_s``, but only tune2fs touches that
500 +If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2 or
501 +JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 are set, the end of the block is a
502 +``struct jbd2_journal_block_tail``, which looks like this:
515 + - Checksum of the journal UUID + the descriptor block, with this field set
521 +In general, the data blocks being written to disk through the journal
522 +are written verbatim into the journal file after the descriptor block.
523 +However, if the first four bytes of the block match the jbd2 magic
524 +number then those four bytes are replaced with zeroes and the “escaped”
525 +flag is set in the descriptor block tag.
530 +A revocation block is used to prevent replay of a block in an earlier
531 +transaction. This is used to mark blocks that were journalled at one
532 +time but are no longer journalled. Typically this happens if a metadata
533 +block is freed and re-allocated as a file data block; in this case, a
534 +journal replay after the file block was written to disk will cause
537 +**NOTE**: This mechanism is NOT used to express “this journal block is
538 +superseded by this other journal block”, as the author (djwong)
539 +mistakenly thought. Any block being added to a transaction will cause
540 +the removal of all existing revocation records for that block.
542 +Revocation blocks are described in
543 +``struct jbd2_journal_revoke_header_s``, are at least 16 bytes in
544 +length, but use a full block:
555 + - journal\_header\_t
557 + - Common block header.
561 + - Number of bytes used in this block.
563 + - \_\_be32 or \_\_be64
565 + - Blocks to revoke.
567 +After r\_count is a linear array of block numbers that are effectively
568 +revoked by this transaction. The size of each block number is 8 bytes if
569 +the superblock advertises 64-bit block number support, or 4 bytes
572 +If JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2 or
573 +JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3 are set, the end of the revocation
574 +block is a ``struct jbd2_journal_revoke_tail``, which has this format:
587 + - Checksum of the journal UUID + revocation block
592 +The commit block is a sentry that indicates that a transaction has been
593 +completely written to the journal. Once this commit block reaches the
594 +journal, the data stored with this transaction can be written to their
595 +final locations on disk.
597 +The commit block is described by ``struct commit_header``, which is 32
598 +bytes long (but uses a full block):
609 + - journal\_header\_s
611 + - Common block header.
615 + - The type of checksum to use to verify the integrity of the data blocks
616 + in the transaction. See jbd2_checksum_type_ for more info.
620 + - The number of bytes used by the checksum. Most likely 4.
627 + - h\_chksum[JBD2\_CHECKSUM\_BYTES]
628 + - 32 bytes of space to store checksums. If
629 + JBD2\_FEATURE\_INCOMPAT\_CSUM\_V2 or JBD2\_FEATURE\_INCOMPAT\_CSUM\_V3
630 + are set, the first ``__be32`` is the checksum of the journal UUID and
631 + the entire commit block, with this field zeroed. If
632 + JBD2\_FEATURE\_COMPAT\_CHECKSUM is set, the first ``__be32`` is the
633 + crc32 of all the blocks already written to the transaction.
637 + - The time that the transaction was committed, in seconds since the epoch.
641 + - Nanoseconds component of the above timestamp.