10f66f451Sopenharmony_ci<title>Rob's ext2 documentation</title> 20f66f451Sopenharmony_ci 30f66f451Sopenharmony_ci<p>This page focuses on the ext2 on-disk format. The Linux kernel's filesystem 40f66f451Sopenharmony_ciimplementation (the code to read and write it) is documented in the kernel 50f66f451Sopenharmony_cisource, Documentation/filesystems/ext2.txt.</p> 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ci<p>Note: for our purposes, ext3 and ext4 are just ext2 with some extra data 80f66f451Sopenharmony_cifields.</p> 90f66f451Sopenharmony_ci 100f66f451Sopenharmony_ci<h2>Overview</h2> 110f66f451Sopenharmony_ci 120f66f451Sopenharmony_ci<h2>Blocks and Block Groups</h2> 130f66f451Sopenharmony_ci 140f66f451Sopenharmony_ci<p>Every ext2 filesystem consists of blocks, which are divided into block 150f66f451Sopenharmony_cigroups. Blocks can be 1k, 2k, or 4k in length.<super><a href="#1">[1]</a></super> 160f66f451Sopenharmony_ciAll ext2 disk layout is done in terms of these logical blocks, never in 170f66f451Sopenharmony_citerms of 512-byte logical blocks.</p> 180f66f451Sopenharmony_ci 190f66f451Sopenharmony_ci<p>Each block group contains as many blocks as one block can hold a 200f66f451Sopenharmony_cibitmap for, so at a 1k block size a block group contains 8192 blocks (1024 210f66f451Sopenharmony_cibytes * 8 bits), and at 4k block size a block group contains 32768 blocks. 220f66f451Sopenharmony_ciGroups are numbered starting at 0, and occur one after another on disk, 230f66f451Sopenharmony_ciin order, with no gaps between them.</p> 240f66f451Sopenharmony_ci 250f66f451Sopenharmony_ci<p>Block groups contain the following structures, in order:</p> 260f66f451Sopenharmony_ci 270f66f451Sopenharmony_ci<ul> 280f66f451Sopenharmony_ci<li>Superblock (sometimes)</li> 290f66f451Sopenharmony_ci<li>Group table (sometimes)</li> 300f66f451Sopenharmony_ci<li>Block bitmap</li> 310f66f451Sopenharmony_ci<li>Inode bitmap</li> 320f66f451Sopenharmony_ci<li>Inode table</li> 330f66f451Sopenharmony_ci<li>Data blocks</li> 340f66f451Sopenharmony_ci</ul> 350f66f451Sopenharmony_ci 360f66f451Sopenharmony_ci<p>Not all block groups contain all structures. Specifically, the first two 370f66f451Sopenharmony_ci(superblock and group table) only occur in some groups, and other block 380f66f451Sopenharmony_cigroups start with the block bitmap and go from there. This frees up more 390f66f451Sopenharmony_cidata blocks to hold actual file and directory data, see the superblock 400f66f451Sopenharmony_cidescription for details.</p> 410f66f451Sopenharmony_ci 420f66f451Sopenharmony_ci<p>Each structure in this list is stored in its' own block (or blocks in the 430f66f451Sopenharmony_cicase of the group and inode tables), and doesn't share blocks with any other 440f66f451Sopenharmony_cistructure. This can involve padding the end of the block with zeroes, or 450f66f451Sopenharmony_ciextending tables with extra entries to fill up the rest of the block.</p> 460f66f451Sopenharmony_ci 470f66f451Sopenharmony_ci<p>The linux/ext2_fs.h #include file defines struct ext2_super_block, 480f66f451Sopenharmony_cistruct ext2_group_desc, struct ext2_inode, struct ext2_dir_entry_2, and a lot 490f66f451Sopenharmony_ciof constants. Toybox doesn't use this file directly, instead it has an e2fs.h 500f66f451Sopenharmony_ciinclude of its own containting cleaned-up versions of the data it needs.</p> 510f66f451Sopenharmony_ci 520f66f451Sopenharmony_ci<h2>Superblock</h2> 530f66f451Sopenharmony_ci 540f66f451Sopenharmony_ci<p>The superblock contains a 1024 byte structure, which toybox calls 550f66f451Sopenharmony_ci"struct ext2_superblock". Where exactly this structure is to be found is 560f66f451Sopenharmony_cia bit complicated for historical reasons.</p> 570f66f451Sopenharmony_ci 580f66f451Sopenharmony_ci<p>For copies of the superblock stored in block groups after the first, 590f66f451Sopenharmony_cithe superblock structure starts at the beginning of the first block of the 600f66f451Sopenharmony_cigroup, with zero padding afterwards if necessary (I.E. if the block size is 610f66f451Sopenharmony_cilarger than 1k). In modern "sparse superblock" filesystems (everything 620f66f451Sopenharmony_cianyone still cares about), the superblock occurs in group 0 and in later groups 630f66f451Sopenharmony_cithat are powers of 3, 5, and 7. (So groups 0, 1, 3, 5, 7, 9, 25, 27, 49, 81, 640f66f451Sopenharmony_ci125, 243, 343...) Any block group starting with a superblock will also 650f66f451Sopenharmony_cihave a group descriptor table, and ones that don't won't.</p> 660f66f451Sopenharmony_ci 670f66f451Sopenharmony_ci<p>The very first superblock is weird. This is because if you format an entire 680f66f451Sopenharmony_ciblock device (rather than a partition), you stomp the very start of the disk 690f66f451Sopenharmony_ciwhich contains the boot sector and the partition table. Back when ext2 on 700f66f451Sopenharmony_cifloppies was common, this was a big deal.</p> 710f66f451Sopenharmony_ci 720f66f451Sopenharmony_ci<p>So the very first 1024 bytes of the very first block are always left alone. 730f66f451Sopenharmony_ciWhen the block size is 1024 bytes, then that block is left alone and the 740f66f451Sopenharmony_cisuperblock is stored in the second block instead<super><a href="#2">[2]</a>. 750f66f451Sopenharmony_ciWhen the block size is larger than 1024 bytes, the first superblock starts 760f66f451Sopenharmony_ci1024 bytes into the block, with the original data preserved by mke2fs and 770f66f451Sopenharmony_ciappropriate zero padding added to the end of the block (if necessary).</p> 780f66f451Sopenharmony_ci 790f66f451Sopenharmony_ci<h2>Group descriptor table</h2> 800f66f451Sopenharmony_ci<h2>Block bitmap</h2> 810f66f451Sopenharmony_ci<h2>Inode bitmap</h2> 820f66f451Sopenharmony_ci<h2>Inode table</h2> 830f66f451Sopenharmony_ci<h2>Data blocks</h2> 840f66f451Sopenharmony_ci 850f66f451Sopenharmony_ci<h2>Directories</h2> 860f66f451Sopenharmony_ci 870f66f451Sopenharmony_ci<p>For performance reasons, directory entries are 4-byte aligned (rec_len is 880f66f451Sopenharmony_cia multiple of 4), so up to 3 bytes of padding (zeroes) can be added at the end 890f66f451Sopenharmony_ciof each name. (This affects rec_len but not the name_len.)</p> 900f66f451Sopenharmony_ci 910f66f451Sopenharmony_ci<p>The last directory entry in each block is padded up to block size. If there 920f66f451Sopenharmony_ciisn't enough space for another struct ext2_dentry the last </p> 930f66f451Sopenharmony_ci 940f66f451Sopenharmony_ci<p>Question: is the length stored in the inode also padded up to block size?</p> 950f66f451Sopenharmony_ci 960f66f451Sopenharmony_ci<hr /> 970f66f451Sopenharmony_ci<p><a name="1" />Footnote 1: On some systems blocks can be larger than 4k, but 980f66f451Sopenharmony_cifor implementation reasons not larger than PAGE_SIZE. So the Alpha can have 990f66f451Sopenharmony_ci8k blocks but most other systems couldn't mount them, thus you don't see this 1000f66f451Sopenharmony_ciout in the wild much anymore.</p> 1010f66f451Sopenharmony_ci 1020f66f451Sopenharmony_ci<p><a name="2" />Footnote 2: In this case, the first_data_block field in the 1030f66f451Sopenharmony_cisuperblock structure will be set to 1. Otherwise it's always 0. How this 1040f66f451Sopenharmony_cicould POSSIBLY be useful information is an open question, since A) you have to 1050f66f451Sopenharmony_ciread the superblock before you can get this information, so you know where 1060f66f451Sopenharmony_ciit came from, B) the first copy of the superblock always starts at offset 1024 1070f66f451Sopenharmony_cino matter what, and if your block size is 1024 you already know you skipped the 1080f66f451Sopenharmony_cifirst block.</p> 109