ext2.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <title>Rob's ext2 documentation</title>
  2. <p>This page focuses on the ext2 on-disk format. The Linux kernel's filesystem
  3. implementation (the code to read and write it) is documented in the kernel
  4. source, Documentation/filesystems/ext2.txt.</p>
  5. <p>Note: for our purposes, ext3 and ext4 are just ext2 with some extra data
  6. fields.</p>
  7. <h2>Overview</h2>
  8. <h2>Blocks and Block Groups</h2>
  9. <p>Every ext2 filesystem consists of blocks, which are divided into block
  10. groups. Blocks can be 1k, 2k, or 4k in length.<super><a href="#1">[1]</a></super>
  11. All ext2 disk layout is done in terms of these logical blocks, never in
  12. terms of 512-byte logical blocks.</p>
  13. <p>Each block group contains as many blocks as one block can hold a
  14. bitmap for, so at a 1k block size a block group contains 8192 blocks (1024
  15. bytes * 8 bits), and at 4k block size a block group contains 32768 blocks.
  16. Groups are numbered starting at 0, and occur one after another on disk,
  17. in order, with no gaps between them.</p>
  18. <p>Block groups contain the following structures, in order:</p>
  19. <ul>
  20. <li>Superblock (sometimes)</li>
  21. <li>Group table (sometimes)</li>
  22. <li>Block bitmap</li>
  23. <li>Inode bitmap</li>
  24. <li>Inode table</li>
  25. <li>Data blocks</li>
  26. </ul>
  27. <p>Not all block groups contain all structures. Specifically, the first two
  28. (superblock and group table) only occur in some groups, and other block
  29. groups start with the block bitmap and go from there. This frees up more
  30. data blocks to hold actual file and directory data, see the superblock
  31. description for details.</p>
  32. <p>Each structure in this list is stored in its' own block (or blocks in the
  33. case of the group and inode tables), and doesn't share blocks with any other
  34. structure. This can involve padding the end of the block with zeroes, or
  35. extending tables with extra entries to fill up the rest of the block.</p>
  36. <p>The linux/ext2_fs.h #include file defines struct ext2_super_block,
  37. struct ext2_group_desc, struct ext2_inode, struct ext2_dir_entry_2, and a lot
  38. of constants. Toybox doesn't use this file directly, instead it has an e2fs.h
  39. include of its own containting cleaned-up versions of the data it needs.</p>
  40. <h2>Superblock</h2>
  41. <p>The superblock contains a 1024 byte structure, which toybox calls
  42. "struct ext2_superblock". Where exactly this structure is to be found is
  43. a bit complicated for historical reasons.</p>
  44. <p>For copies of the superblock stored in block groups after the first,
  45. the superblock structure starts at the beginning of the first block of the
  46. group, with zero padding afterwards if necessary (I.E. if the block size is
  47. larger than 1k). In modern "sparse superblock" filesystems (everything
  48. anyone still cares about), the superblock occurs in group 0 and in later groups
  49. that are powers of 3, 5, and 7. (So groups 0, 1, 3, 5, 7, 9, 25, 27, 49, 81,
  50. 125, 243, 343...) Any block group starting with a superblock will also
  51. have a group descriptor table, and ones that don't won't.</p>
  52. <p>The very first superblock is weird. This is because if you format an entire
  53. block device (rather than a partition), you stomp the very start of the disk
  54. which contains the boot sector and the partition table. Back when ext2 on
  55. floppies was common, this was a big deal.</p>
  56. <p>So the very first 1024 bytes of the very first block are always left alone.
  57. When the block size is 1024 bytes, then that block is left alone and the
  58. superblock is stored in the second block instead<super><a href="#2">[2]</a>.
  59. When the block size is larger than 1024 bytes, the first superblock starts
  60. 1024 bytes into the block, with the original data preserved by mke2fs and
  61. appropriate zero padding added to the end of the block (if necessary).</p>
  62. <h2>Group descriptor table</h2>
  63. <h2>Block bitmap</h2>
  64. <h2>Inode bitmap</h2>
  65. <h2>Inode table</h2>
  66. <h2>Data blocks</h2>
  67. <h2>Directories</h2>
  68. <p>For performance reasons, directory entries are 4-byte aligned (rec_len is
  69. a multiple of 4), so up to 3 bytes of padding (zeroes) can be added at the end
  70. of each name. (This affects rec_len but not the name_len.)</p>
  71. <p>The last directory entry in each block is padded up to block size. If there
  72. isn't enough space for another struct ext2_dentry the last </p>
  73. <p>Question: is the length stored in the inode also padded up to block size?</p>
  74. <hr />
  75. <p><a name="1" />Footnote 1: On some systems blocks can be larger than 4k, but
  76. for implementation reasons not larger than PAGE_SIZE. So the Alpha can have
  77. 8k blocks but most other systems couldn't mount them, thus you don't see this
  78. out in the wild much anymore.</p>
  79. <p><a name="2" />Footnote 2: In this case, the first_data_block field in the
  80. superblock structure will be set to 1. Otherwise it's always 0. How this
  81. could POSSIBLY be useful information is an open question, since A) you have to
  82. read the superblock before you can get this information, so you know where
  83. it came from, B) the first copy of the superblock always starts at offset 1024
  84. no matter what, and if your block size is 1024 you already know you skipped the
  85. first block.</p>