xref: /OK3568_Linux_fs/kernel/Documentation/filesystems/ext4/checksums.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunChecksums
4*4882a593Smuzhiyun---------
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunStarting in early 2012, metadata checksums were added to all major ext4
7*4882a593Smuzhiyunand jbd2 data structures. The associated feature flag is metadata\_csum.
8*4882a593SmuzhiyunThe desired checksum algorithm is indicated in the superblock, though as
9*4882a593Smuzhiyunof October 2012 the only supported algorithm is crc32c. Some data
10*4882a593Smuzhiyunstructures did not have space to fit a full 32-bit checksum, so only the
11*4882a593Smuzhiyunlower 16 bits are stored. Enabling the 64bit feature increases the data
12*4882a593Smuzhiyunstructure size so that full 32-bit checksums can be stored for many data
13*4882a593Smuzhiyunstructures. However, existing 32-bit filesystems cannot be extended to
14*4882a593Smuzhiyunenable 64bit mode, at least not without the experimental resize2fs
15*4882a593Smuzhiyunpatches to do so.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunExisting filesystems can have checksumming added by running
18*4882a593Smuzhiyun``tune2fs -O metadata_csum`` against the underlying device. If tune2fs
19*4882a593Smuzhiyunencounters directory blocks that lack sufficient empty space to add a
20*4882a593Smuzhiyunchecksum, it will request that you run ``e2fsck -D`` to have the
21*4882a593Smuzhiyundirectories rebuilt with checksums. This has the added benefit of
22*4882a593Smuzhiyunremoving slack space from the directory files and rebalancing the htree
23*4882a593Smuzhiyunindexes. If you \_ignore\_ this step, your directories will not be
24*4882a593Smuzhiyunprotected by a checksum!
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunThe following table describes the data elements that go into each type
27*4882a593Smuzhiyunof checksum. The checksum function is whatever the superblock describes
28*4882a593Smuzhiyun(crc32c as of October 2013) unless noted otherwise.
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun.. list-table::
31*4882a593Smuzhiyun   :widths: 20 8 50
32*4882a593Smuzhiyun   :header-rows: 1
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun   * - Metadata
35*4882a593Smuzhiyun     - Length
36*4882a593Smuzhiyun     - Ingredients
37*4882a593Smuzhiyun   * - Superblock
38*4882a593Smuzhiyun     - \_\_le32
39*4882a593Smuzhiyun     - The entire superblock up to the checksum field. The UUID lives inside
40*4882a593Smuzhiyun       the superblock.
41*4882a593Smuzhiyun   * - MMP
42*4882a593Smuzhiyun     - \_\_le32
43*4882a593Smuzhiyun     - UUID + the entire MMP block up to the checksum field.
44*4882a593Smuzhiyun   * - Extended Attributes
45*4882a593Smuzhiyun     - \_\_le32
46*4882a593Smuzhiyun     - UUID + the entire extended attribute block. The checksum field is set to
47*4882a593Smuzhiyun       zero.
48*4882a593Smuzhiyun   * - Directory Entries
49*4882a593Smuzhiyun     - \_\_le32
50*4882a593Smuzhiyun     - UUID + inode number + inode generation + the directory block up to the
51*4882a593Smuzhiyun       fake entry enclosing the checksum field.
52*4882a593Smuzhiyun   * - HTREE Nodes
53*4882a593Smuzhiyun     - \_\_le32
54*4882a593Smuzhiyun     - UUID + inode number + inode generation + all valid extents + HTREE tail.
55*4882a593Smuzhiyun       The checksum field is set to zero.
56*4882a593Smuzhiyun   * - Extents
57*4882a593Smuzhiyun     - \_\_le32
58*4882a593Smuzhiyun     - UUID + inode number + inode generation + the entire extent block up to
59*4882a593Smuzhiyun       the checksum field.
60*4882a593Smuzhiyun   * - Bitmaps
61*4882a593Smuzhiyun     - \_\_le32 or \_\_le16
62*4882a593Smuzhiyun     - UUID + the entire bitmap. Checksums are stored in the group descriptor,
63*4882a593Smuzhiyun       and truncated if the group descriptor size is 32 bytes (i.e. ^64bit)
64*4882a593Smuzhiyun   * - Inodes
65*4882a593Smuzhiyun     - \_\_le32
66*4882a593Smuzhiyun     - UUID + inode number + inode generation + the entire inode. The checksum
67*4882a593Smuzhiyun       field is set to zero. Each inode has its own checksum.
68*4882a593Smuzhiyun   * - Group Descriptors
69*4882a593Smuzhiyun     - \_\_le16
70*4882a593Smuzhiyun     - If metadata\_csum, then UUID + group number + the entire descriptor;
71*4882a593Smuzhiyun       else if gdt\_csum, then crc16(UUID + group number + the entire
72*4882a593Smuzhiyun       descriptor). In all cases, only the lower 16 bits are stored.
73*4882a593Smuzhiyun
74