18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ciThe Contents of inode.i\_block
48c2ecf20Sopenharmony_ci------------------------------
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ciDepending on the type of file an inode describes, the 60 bytes of
78c2ecf20Sopenharmony_cistorage in ``inode.i_block`` can be used in different ways. In general,
88c2ecf20Sopenharmony_ciregular files and directories will use it for file block indexing
98c2ecf20Sopenharmony_ciinformation, and special files will use it for special purposes.
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciSymbolic Links
128c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciThe target of a symbolic link will be stored in this field if the target
158c2ecf20Sopenharmony_cistring is less than 60 bytes long. Otherwise, either extents or block
168c2ecf20Sopenharmony_cimaps will be used to allocate data blocks to store the link target.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciDirect/Indirect Block Addressing
198c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciIn ext2/3, file block numbers were mapped to logical block numbers by
228c2ecf20Sopenharmony_cimeans of an (up to) three level 1-1 block map. To find the logical block
238c2ecf20Sopenharmony_cithat stores a particular file block, the code would navigate through
248c2ecf20Sopenharmony_cithis increasingly complicated structure. Notice that there is neither a
258c2ecf20Sopenharmony_cimagic number nor a checksum to provide any level of confidence that the
268c2ecf20Sopenharmony_ciblock isn't full of garbage.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci.. ifconfig:: builder != 'latex'
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci   .. include:: blockmap.rst
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci.. ifconfig:: builder == 'latex'
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci   [Table omitted because LaTeX doesn't support nested tables.]
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciNote that with this block mapping scheme, it is necessary to fill out a
378c2ecf20Sopenharmony_cilot of mapping data even for a large contiguous file! This inefficiency
388c2ecf20Sopenharmony_ciled to the creation of the extent mapping scheme, discussed below.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciNotice also that a file using this mapping scheme cannot be placed
418c2ecf20Sopenharmony_cihigher than 2^32 blocks.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciExtent Tree
448c2ecf20Sopenharmony_ci~~~~~~~~~~~
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciIn ext4, the file to logical block map has been replaced with an extent
478c2ecf20Sopenharmony_citree. Under the old scheme, allocating a contiguous run of 1,000 blocks
488c2ecf20Sopenharmony_cirequires an indirect block to map all 1,000 entries; with extents, the
498c2ecf20Sopenharmony_cimapping is reduced to a single ``struct ext4_extent`` with
508c2ecf20Sopenharmony_ci``ee_len = 1000``. If flex\_bg is enabled, it is possible to allocate
518c2ecf20Sopenharmony_civery large files with a single extent, at a considerable reduction in
528c2ecf20Sopenharmony_cimetadata block use, and some improvement in disk efficiency. The inode
538c2ecf20Sopenharmony_cimust have the extents flag (0x80000) flag set for this feature to be in
548c2ecf20Sopenharmony_ciuse.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciExtents are arranged as a tree. Each node of the tree begins with a
578c2ecf20Sopenharmony_ci``struct ext4_extent_header``. If the node is an interior node
588c2ecf20Sopenharmony_ci(``eh.eh_depth`` > 0), the header is followed by ``eh.eh_entries``
598c2ecf20Sopenharmony_ciinstances of ``struct ext4_extent_idx``; each of these index entries
608c2ecf20Sopenharmony_cipoints to a block containing more nodes in the extent tree. If the node
618c2ecf20Sopenharmony_ciis a leaf node (``eh.eh_depth == 0``), then the header is followed by
628c2ecf20Sopenharmony_ci``eh.eh_entries`` instances of ``struct ext4_extent``; these instances
638c2ecf20Sopenharmony_cipoint to the file's data blocks. The root node of the extent tree is
648c2ecf20Sopenharmony_cistored in ``inode.i_block``, which allows for the first four extents to
658c2ecf20Sopenharmony_cibe recorded without the use of extra metadata blocks.
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciThe extent tree header is recorded in ``struct ext4_extent_header``,
688c2ecf20Sopenharmony_ciwhich is 12 bytes long:
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci.. list-table::
718c2ecf20Sopenharmony_ci   :widths: 8 8 24 40
728c2ecf20Sopenharmony_ci   :header-rows: 1
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci   * - Offset
758c2ecf20Sopenharmony_ci     - Size
768c2ecf20Sopenharmony_ci     - Name
778c2ecf20Sopenharmony_ci     - Description
788c2ecf20Sopenharmony_ci   * - 0x0
798c2ecf20Sopenharmony_ci     - \_\_le16
808c2ecf20Sopenharmony_ci     - eh\_magic
818c2ecf20Sopenharmony_ci     - Magic number, 0xF30A.
828c2ecf20Sopenharmony_ci   * - 0x2
838c2ecf20Sopenharmony_ci     - \_\_le16
848c2ecf20Sopenharmony_ci     - eh\_entries
858c2ecf20Sopenharmony_ci     - Number of valid entries following the header.
868c2ecf20Sopenharmony_ci   * - 0x4
878c2ecf20Sopenharmony_ci     - \_\_le16
888c2ecf20Sopenharmony_ci     - eh\_max
898c2ecf20Sopenharmony_ci     - Maximum number of entries that could follow the header.
908c2ecf20Sopenharmony_ci   * - 0x6
918c2ecf20Sopenharmony_ci     - \_\_le16
928c2ecf20Sopenharmony_ci     - eh\_depth
938c2ecf20Sopenharmony_ci     - Depth of this extent node in the extent tree. 0 = this extent node
948c2ecf20Sopenharmony_ci       points to data blocks; otherwise, this extent node points to other
958c2ecf20Sopenharmony_ci       extent nodes. The extent tree can be at most 5 levels deep: a logical
968c2ecf20Sopenharmony_ci       block number can be at most ``2^32``, and the smallest ``n`` that
978c2ecf20Sopenharmony_ci       satisfies ``4*(((blocksize - 12)/12)^n) >= 2^32`` is 5.
988c2ecf20Sopenharmony_ci   * - 0x8
998c2ecf20Sopenharmony_ci     - \_\_le32
1008c2ecf20Sopenharmony_ci     - eh\_generation
1018c2ecf20Sopenharmony_ci     - Generation of the tree. (Used by Lustre, but not standard ext4).
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciInternal nodes of the extent tree, also known as index nodes, are
1048c2ecf20Sopenharmony_cirecorded as ``struct ext4_extent_idx``, and are 12 bytes long:
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci.. list-table::
1078c2ecf20Sopenharmony_ci   :widths: 8 8 24 40
1088c2ecf20Sopenharmony_ci   :header-rows: 1
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci   * - Offset
1118c2ecf20Sopenharmony_ci     - Size
1128c2ecf20Sopenharmony_ci     - Name
1138c2ecf20Sopenharmony_ci     - Description
1148c2ecf20Sopenharmony_ci   * - 0x0
1158c2ecf20Sopenharmony_ci     - \_\_le32
1168c2ecf20Sopenharmony_ci     - ei\_block
1178c2ecf20Sopenharmony_ci     - This index node covers file blocks from 'block' onward.
1188c2ecf20Sopenharmony_ci   * - 0x4
1198c2ecf20Sopenharmony_ci     - \_\_le32
1208c2ecf20Sopenharmony_ci     - ei\_leaf\_lo
1218c2ecf20Sopenharmony_ci     - Lower 32-bits of the block number of the extent node that is the next
1228c2ecf20Sopenharmony_ci       level lower in the tree. The tree node pointed to can be either another
1238c2ecf20Sopenharmony_ci       internal node or a leaf node, described below.
1248c2ecf20Sopenharmony_ci   * - 0x8
1258c2ecf20Sopenharmony_ci     - \_\_le16
1268c2ecf20Sopenharmony_ci     - ei\_leaf\_hi
1278c2ecf20Sopenharmony_ci     - Upper 16-bits of the previous field.
1288c2ecf20Sopenharmony_ci   * - 0xA
1298c2ecf20Sopenharmony_ci     - \_\_u16
1308c2ecf20Sopenharmony_ci     - ei\_unused
1318c2ecf20Sopenharmony_ci     -
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciLeaf nodes of the extent tree are recorded as ``struct ext4_extent``,
1348c2ecf20Sopenharmony_ciand are also 12 bytes long:
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci.. list-table::
1378c2ecf20Sopenharmony_ci   :widths: 8 8 24 40
1388c2ecf20Sopenharmony_ci   :header-rows: 1
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci   * - Offset
1418c2ecf20Sopenharmony_ci     - Size
1428c2ecf20Sopenharmony_ci     - Name
1438c2ecf20Sopenharmony_ci     - Description
1448c2ecf20Sopenharmony_ci   * - 0x0
1458c2ecf20Sopenharmony_ci     - \_\_le32
1468c2ecf20Sopenharmony_ci     - ee\_block
1478c2ecf20Sopenharmony_ci     - First file block number that this extent covers.
1488c2ecf20Sopenharmony_ci   * - 0x4
1498c2ecf20Sopenharmony_ci     - \_\_le16
1508c2ecf20Sopenharmony_ci     - ee\_len
1518c2ecf20Sopenharmony_ci     - Number of blocks covered by extent. If the value of this field is <=
1528c2ecf20Sopenharmony_ci       32768, the extent is initialized. If the value of the field is > 32768,
1538c2ecf20Sopenharmony_ci       the extent is uninitialized and the actual extent length is ``ee_len`` -
1548c2ecf20Sopenharmony_ci       32768. Therefore, the maximum length of a initialized extent is 32768
1558c2ecf20Sopenharmony_ci       blocks, and the maximum length of an uninitialized extent is 32767.
1568c2ecf20Sopenharmony_ci   * - 0x6
1578c2ecf20Sopenharmony_ci     - \_\_le16
1588c2ecf20Sopenharmony_ci     - ee\_start\_hi
1598c2ecf20Sopenharmony_ci     - Upper 16-bits of the block number to which this extent points.
1608c2ecf20Sopenharmony_ci   * - 0x8
1618c2ecf20Sopenharmony_ci     - \_\_le32
1628c2ecf20Sopenharmony_ci     - ee\_start\_lo
1638c2ecf20Sopenharmony_ci     - Lower 32-bits of the block number to which this extent points.
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ciPrior to the introduction of metadata checksums, the extent header +
1668c2ecf20Sopenharmony_ciextent entries always left at least 4 bytes of unallocated space at the
1678c2ecf20Sopenharmony_ciend of each extent tree data block (because (2^x % 12) >= 4). Therefore,
1688c2ecf20Sopenharmony_cithe 32-bit checksum is inserted into this space. The 4 extents in the
1698c2ecf20Sopenharmony_ciinode do not need checksumming, since the inode is already checksummed.
1708c2ecf20Sopenharmony_ciThe checksum is calculated against the FS UUID, the inode number, the
1718c2ecf20Sopenharmony_ciinode generation, and the entire extent block leading up to (but not
1728c2ecf20Sopenharmony_ciincluding) the checksum itself.
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci``struct ext4_extent_tail`` is 4 bytes long:
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci.. list-table::
1778c2ecf20Sopenharmony_ci   :widths: 8 8 24 40
1788c2ecf20Sopenharmony_ci   :header-rows: 1
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci   * - Offset
1818c2ecf20Sopenharmony_ci     - Size
1828c2ecf20Sopenharmony_ci     - Name
1838c2ecf20Sopenharmony_ci     - Description
1848c2ecf20Sopenharmony_ci   * - 0x0
1858c2ecf20Sopenharmony_ci     - \_\_le32
1868c2ecf20Sopenharmony_ci     - eb\_checksum
1878c2ecf20Sopenharmony_ci     - Checksum of the extent block, crc32c(uuid+inum+igeneration+extentblock)
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ciInline Data
1908c2ecf20Sopenharmony_ci~~~~~~~~~~~
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciIf the inline data feature is enabled for the filesystem and the flag is
1938c2ecf20Sopenharmony_ciset for the inode, it is possible that the first 60 bytes of the file
1948c2ecf20Sopenharmony_cidata are stored here.
195