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