blockgroup.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Layout
  3. ------
  4. The layout of a standard block group is approximately as follows (each
  5. of these fields is discussed in a separate section below):
  6. .. list-table::
  7. :widths: 1 1 1 1 1 1 1 1
  8. :header-rows: 1
  9. * - Group 0 Padding
  10. - ext4 Super Block
  11. - Group Descriptors
  12. - Reserved GDT Blocks
  13. - Data Block Bitmap
  14. - inode Bitmap
  15. - inode Table
  16. - Data Blocks
  17. * - 1024 bytes
  18. - 1 block
  19. - many blocks
  20. - many blocks
  21. - 1 block
  22. - 1 block
  23. - many blocks
  24. - many more blocks
  25. For the special case of block group 0, the first 1024 bytes are unused,
  26. to allow for the installation of x86 boot sectors and other oddities.
  27. The superblock will start at offset 1024 bytes, whichever block that
  28. happens to be (usually 0). However, if for some reason the block size =
  29. 1024, then block 0 is marked in use and the superblock goes in block 1.
  30. For all other block groups, there is no padding.
  31. The ext4 driver primarily works with the superblock and the group
  32. descriptors that are found in block group 0. Redundant copies of the
  33. superblock and group descriptors are written to some of the block groups
  34. across the disk in case the beginning of the disk gets trashed, though
  35. not all block groups necessarily host a redundant copy (see following
  36. paragraph for more details). If the group does not have a redundant
  37. copy, the block group begins with the data block bitmap. Note also that
  38. when the filesystem is freshly formatted, mkfs will allocate “reserve
  39. GDT block” space after the block group descriptors and before the start
  40. of the block bitmaps to allow for future expansion of the filesystem. By
  41. default, a filesystem is allowed to increase in size by a factor of
  42. 1024x over the original filesystem size.
  43. The location of the inode table is given by ``grp.bg_inode_table_*``. It
  44. is continuous range of blocks large enough to contain
  45. ``sb.s_inodes_per_group * sb.s_inode_size`` bytes.
  46. As for the ordering of items in a block group, it is generally
  47. established that the super block and the group descriptor table, if
  48. present, will be at the beginning of the block group. The bitmaps and
  49. the inode table can be anywhere, and it is quite possible for the
  50. bitmaps to come after the inode table, or for both to be in different
  51. groups (flex_bg). Leftover space is used for file data blocks, indirect
  52. block maps, extent tree blocks, and extended attributes.
  53. Flexible Block Groups
  54. ---------------------
  55. Starting in ext4, there is a new feature called flexible block groups
  56. (flex_bg). In a flex_bg, several block groups are tied together as one
  57. logical block group; the bitmap spaces and the inode table space in the
  58. first block group of the flex_bg are expanded to include the bitmaps
  59. and inode tables of all other block groups in the flex_bg. For example,
  60. if the flex_bg size is 4, then group 0 will contain (in order) the
  61. superblock, group descriptors, data block bitmaps for groups 0-3, inode
  62. bitmaps for groups 0-3, inode tables for groups 0-3, and the remaining
  63. space in group 0 is for file data. The effect of this is to group the
  64. block group metadata close together for faster loading, and to enable
  65. large files to be continuous on disk. Backup copies of the superblock
  66. and group descriptors are always at the beginning of block groups, even
  67. if flex_bg is enabled. The number of block groups that make up a
  68. flex_bg is given by 2 ^ ``sb.s_log_groups_per_flex``.
  69. Meta Block Groups
  70. -----------------
  71. Without the option META_BG, for safety concerns, all block group
  72. descriptors copies are kept in the first block group. Given the default
  73. 128MiB(2^27 bytes) block group size and 64-byte group descriptors, ext4
  74. can have at most 2^27/64 = 2^21 block groups. This limits the entire
  75. filesystem size to 2^21 * 2^27 = 2^48bytes or 256TiB.
  76. The solution to this problem is to use the metablock group feature
  77. (META_BG), which is already in ext3 for all 2.6 releases. With the
  78. META_BG feature, ext4 filesystems are partitioned into many metablock
  79. groups. Each metablock group is a cluster of block groups whose group
  80. descriptor structures can be stored in a single disk block. For ext4
  81. filesystems with 4 KB block size, a single metablock group partition
  82. includes 64 block groups, or 8 GiB of disk space. The metablock group
  83. feature moves the location of the group descriptors from the congested
  84. first block group of the whole filesystem into the first group of each
  85. metablock group itself. The backups are in the second and last group of
  86. each metablock group. This increases the 2^21 maximum block groups limit
  87. to the hard limit 2^32, allowing support for a 512PiB filesystem.
  88. The change in the filesystem format replaces the current scheme where
  89. the superblock is followed by a variable-length set of block group
  90. descriptors. Instead, the superblock and a single block group descriptor
  91. block is placed at the beginning of the first, second, and last block
  92. groups in a meta-block group. A meta-block group is a collection of
  93. block groups which can be described by a single block group descriptor
  94. block. Since the size of the block group descriptor structure is 64
  95. bytes, a meta-block group contains 16 block groups for filesystems with
  96. a 1KB block size, and 64 block groups for filesystems with a 4KB
  97. blocksize. Filesystems can either be created using this new block group
  98. descriptor layout, or existing filesystems can be resized on-line, and
  99. the field s_first_meta_bg in the superblock will indicate the first
  100. block group using this new layout.
  101. Please see an important note about ``BLOCK_UNINIT`` in the section about
  102. block and inode bitmaps.
  103. Lazy Block Group Initialization
  104. -------------------------------
  105. A new feature for ext4 are three block group descriptor flags that
  106. enable mkfs to skip initializing other parts of the block group
  107. metadata. Specifically, the INODE_UNINIT and BLOCK_UNINIT flags mean
  108. that the inode and block bitmaps for that group can be calculated and
  109. therefore the on-disk bitmap blocks are not initialized. This is
  110. generally the case for an empty block group or a block group containing
  111. only fixed-location block group metadata. The INODE_ZEROED flag means
  112. that the inode table has been initialized; mkfs will unset this flag and
  113. rely on the kernel to initialize the inode tables in the background.
  114. By not writing zeroes to the bitmaps and inode table, mkfs time is
  115. reduced considerably. Note the feature flag is RO_COMPAT_GDT_CSUM,
  116. but the dumpe2fs output prints this as “uninit_bg”. They are the same
  117. thing.