xref: /OK3568_Linux_fs/kernel/Documentation/filesystems/ext4/ifork.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunThe Contents of inode.i\_block
4*4882a593Smuzhiyun------------------------------
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunDepending on the type of file an inode describes, the 60 bytes of
7*4882a593Smuzhiyunstorage in ``inode.i_block`` can be used in different ways. In general,
8*4882a593Smuzhiyunregular files and directories will use it for file block indexing
9*4882a593Smuzhiyuninformation, and special files will use it for special purposes.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunSymbolic Links
12*4882a593Smuzhiyun~~~~~~~~~~~~~~
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThe target of a symbolic link will be stored in this field if the target
15*4882a593Smuzhiyunstring is less than 60 bytes long. Otherwise, either extents or block
16*4882a593Smuzhiyunmaps will be used to allocate data blocks to store the link target.
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunDirect/Indirect Block Addressing
19*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunIn ext2/3, file block numbers were mapped to logical block numbers by
22*4882a593Smuzhiyunmeans of an (up to) three level 1-1 block map. To find the logical block
23*4882a593Smuzhiyunthat stores a particular file block, the code would navigate through
24*4882a593Smuzhiyunthis increasingly complicated structure. Notice that there is neither a
25*4882a593Smuzhiyunmagic number nor a checksum to provide any level of confidence that the
26*4882a593Smuzhiyunblock isn't full of garbage.
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun.. ifconfig:: builder != 'latex'
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun   .. include:: blockmap.rst
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun.. ifconfig:: builder == 'latex'
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun   [Table omitted because LaTeX doesn't support nested tables.]
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunNote that with this block mapping scheme, it is necessary to fill out a
37*4882a593Smuzhiyunlot of mapping data even for a large contiguous file! This inefficiency
38*4882a593Smuzhiyunled to the creation of the extent mapping scheme, discussed below.
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunNotice also that a file using this mapping scheme cannot be placed
41*4882a593Smuzhiyunhigher than 2^32 blocks.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunExtent Tree
44*4882a593Smuzhiyun~~~~~~~~~~~
45*4882a593Smuzhiyun
46*4882a593SmuzhiyunIn ext4, the file to logical block map has been replaced with an extent
47*4882a593Smuzhiyuntree. Under the old scheme, allocating a contiguous run of 1,000 blocks
48*4882a593Smuzhiyunrequires an indirect block to map all 1,000 entries; with extents, the
49*4882a593Smuzhiyunmapping is reduced to a single ``struct ext4_extent`` with
50*4882a593Smuzhiyun``ee_len = 1000``. If flex\_bg is enabled, it is possible to allocate
51*4882a593Smuzhiyunvery large files with a single extent, at a considerable reduction in
52*4882a593Smuzhiyunmetadata block use, and some improvement in disk efficiency. The inode
53*4882a593Smuzhiyunmust have the extents flag (0x80000) flag set for this feature to be in
54*4882a593Smuzhiyunuse.
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunExtents are arranged as a tree. Each node of the tree begins with a
57*4882a593Smuzhiyun``struct ext4_extent_header``. If the node is an interior node
58*4882a593Smuzhiyun(``eh.eh_depth`` > 0), the header is followed by ``eh.eh_entries``
59*4882a593Smuzhiyuninstances of ``struct ext4_extent_idx``; each of these index entries
60*4882a593Smuzhiyunpoints to a block containing more nodes in the extent tree. If the node
61*4882a593Smuzhiyunis a leaf node (``eh.eh_depth == 0``), then the header is followed by
62*4882a593Smuzhiyun``eh.eh_entries`` instances of ``struct ext4_extent``; these instances
63*4882a593Smuzhiyunpoint to the file's data blocks. The root node of the extent tree is
64*4882a593Smuzhiyunstored in ``inode.i_block``, which allows for the first four extents to
65*4882a593Smuzhiyunbe recorded without the use of extra metadata blocks.
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunThe extent tree header is recorded in ``struct ext4_extent_header``,
68*4882a593Smuzhiyunwhich is 12 bytes long:
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun.. list-table::
71*4882a593Smuzhiyun   :widths: 8 8 24 40
72*4882a593Smuzhiyun   :header-rows: 1
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun   * - Offset
75*4882a593Smuzhiyun     - Size
76*4882a593Smuzhiyun     - Name
77*4882a593Smuzhiyun     - Description
78*4882a593Smuzhiyun   * - 0x0
79*4882a593Smuzhiyun     - \_\_le16
80*4882a593Smuzhiyun     - eh\_magic
81*4882a593Smuzhiyun     - Magic number, 0xF30A.
82*4882a593Smuzhiyun   * - 0x2
83*4882a593Smuzhiyun     - \_\_le16
84*4882a593Smuzhiyun     - eh\_entries
85*4882a593Smuzhiyun     - Number of valid entries following the header.
86*4882a593Smuzhiyun   * - 0x4
87*4882a593Smuzhiyun     - \_\_le16
88*4882a593Smuzhiyun     - eh\_max
89*4882a593Smuzhiyun     - Maximum number of entries that could follow the header.
90*4882a593Smuzhiyun   * - 0x6
91*4882a593Smuzhiyun     - \_\_le16
92*4882a593Smuzhiyun     - eh\_depth
93*4882a593Smuzhiyun     - Depth of this extent node in the extent tree. 0 = this extent node
94*4882a593Smuzhiyun       points to data blocks; otherwise, this extent node points to other
95*4882a593Smuzhiyun       extent nodes. The extent tree can be at most 5 levels deep: a logical
96*4882a593Smuzhiyun       block number can be at most ``2^32``, and the smallest ``n`` that
97*4882a593Smuzhiyun       satisfies ``4*(((blocksize - 12)/12)^n) >= 2^32`` is 5.
98*4882a593Smuzhiyun   * - 0x8
99*4882a593Smuzhiyun     - \_\_le32
100*4882a593Smuzhiyun     - eh\_generation
101*4882a593Smuzhiyun     - Generation of the tree. (Used by Lustre, but not standard ext4).
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunInternal nodes of the extent tree, also known as index nodes, are
104*4882a593Smuzhiyunrecorded as ``struct ext4_extent_idx``, and are 12 bytes long:
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun.. list-table::
107*4882a593Smuzhiyun   :widths: 8 8 24 40
108*4882a593Smuzhiyun   :header-rows: 1
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun   * - Offset
111*4882a593Smuzhiyun     - Size
112*4882a593Smuzhiyun     - Name
113*4882a593Smuzhiyun     - Description
114*4882a593Smuzhiyun   * - 0x0
115*4882a593Smuzhiyun     - \_\_le32
116*4882a593Smuzhiyun     - ei\_block
117*4882a593Smuzhiyun     - This index node covers file blocks from 'block' onward.
118*4882a593Smuzhiyun   * - 0x4
119*4882a593Smuzhiyun     - \_\_le32
120*4882a593Smuzhiyun     - ei\_leaf\_lo
121*4882a593Smuzhiyun     - Lower 32-bits of the block number of the extent node that is the next
122*4882a593Smuzhiyun       level lower in the tree. The tree node pointed to can be either another
123*4882a593Smuzhiyun       internal node or a leaf node, described below.
124*4882a593Smuzhiyun   * - 0x8
125*4882a593Smuzhiyun     - \_\_le16
126*4882a593Smuzhiyun     - ei\_leaf\_hi
127*4882a593Smuzhiyun     - Upper 16-bits of the previous field.
128*4882a593Smuzhiyun   * - 0xA
129*4882a593Smuzhiyun     - \_\_u16
130*4882a593Smuzhiyun     - ei\_unused
131*4882a593Smuzhiyun     -
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunLeaf nodes of the extent tree are recorded as ``struct ext4_extent``,
134*4882a593Smuzhiyunand are also 12 bytes long:
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun.. list-table::
137*4882a593Smuzhiyun   :widths: 8 8 24 40
138*4882a593Smuzhiyun   :header-rows: 1
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun   * - Offset
141*4882a593Smuzhiyun     - Size
142*4882a593Smuzhiyun     - Name
143*4882a593Smuzhiyun     - Description
144*4882a593Smuzhiyun   * - 0x0
145*4882a593Smuzhiyun     - \_\_le32
146*4882a593Smuzhiyun     - ee\_block
147*4882a593Smuzhiyun     - First file block number that this extent covers.
148*4882a593Smuzhiyun   * - 0x4
149*4882a593Smuzhiyun     - \_\_le16
150*4882a593Smuzhiyun     - ee\_len
151*4882a593Smuzhiyun     - Number of blocks covered by extent. If the value of this field is <=
152*4882a593Smuzhiyun       32768, the extent is initialized. If the value of the field is > 32768,
153*4882a593Smuzhiyun       the extent is uninitialized and the actual extent length is ``ee_len`` -
154*4882a593Smuzhiyun       32768. Therefore, the maximum length of a initialized extent is 32768
155*4882a593Smuzhiyun       blocks, and the maximum length of an uninitialized extent is 32767.
156*4882a593Smuzhiyun   * - 0x6
157*4882a593Smuzhiyun     - \_\_le16
158*4882a593Smuzhiyun     - ee\_start\_hi
159*4882a593Smuzhiyun     - Upper 16-bits of the block number to which this extent points.
160*4882a593Smuzhiyun   * - 0x8
161*4882a593Smuzhiyun     - \_\_le32
162*4882a593Smuzhiyun     - ee\_start\_lo
163*4882a593Smuzhiyun     - Lower 32-bits of the block number to which this extent points.
164*4882a593Smuzhiyun
165*4882a593SmuzhiyunPrior to the introduction of metadata checksums, the extent header +
166*4882a593Smuzhiyunextent entries always left at least 4 bytes of unallocated space at the
167*4882a593Smuzhiyunend of each extent tree data block (because (2^x % 12) >= 4). Therefore,
168*4882a593Smuzhiyunthe 32-bit checksum is inserted into this space. The 4 extents in the
169*4882a593Smuzhiyuninode do not need checksumming, since the inode is already checksummed.
170*4882a593SmuzhiyunThe checksum is calculated against the FS UUID, the inode number, the
171*4882a593Smuzhiyuninode generation, and the entire extent block leading up to (but not
172*4882a593Smuzhiyunincluding) the checksum itself.
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun``struct ext4_extent_tail`` is 4 bytes long:
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun.. list-table::
177*4882a593Smuzhiyun   :widths: 8 8 24 40
178*4882a593Smuzhiyun   :header-rows: 1
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun   * - Offset
181*4882a593Smuzhiyun     - Size
182*4882a593Smuzhiyun     - Name
183*4882a593Smuzhiyun     - Description
184*4882a593Smuzhiyun   * - 0x0
185*4882a593Smuzhiyun     - \_\_le32
186*4882a593Smuzhiyun     - eb\_checksum
187*4882a593Smuzhiyun     - Checksum of the extent block, crc32c(uuid+inum+igeneration+extentblock)
188*4882a593Smuzhiyun
189*4882a593SmuzhiyunInline Data
190*4882a593Smuzhiyun~~~~~~~~~~~
191*4882a593Smuzhiyun
192*4882a593SmuzhiyunIf the inline data feature is enabled for the filesystem and the flag is
193*4882a593Smuzhiyunset for the inode, it is possible that the first 60 bytes of the file
194*4882a593Smuzhiyundata are stored here.
195