ENع
→FAT and ext4
الدرس 42 · File systems

FAT and ext4: two on-disk layouts

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

A disk only stores numbered blocks. A file system is the map that says which blocks belong to which file. Save the same 10,000-byte file on two disks and compare the maps: FAT32 keeps a linked list in a table, one entry per cluster, while ext4 gives each file an inode whose extents say where whole runs of blocks start. Then read byte 9000 on both and watch FAT walk its chain while ext4 does one sum. The Linux lessons The file system and Disks, partitions and mounts show the user's side; Inodes and data blocks covers the inode itself.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 42 · تفاعلي
01 / 11

One file, two layouts

A disk is a long row of numbered blocks. A file system decides which blocks hold a file and how to find them again. Here notes.txt, 10,000 bytes, sits on two disks with 4 KiB blocks: FAT32 and ext4. It needs 3 blocks.

file      notes.txt
size      10000 bytes
block     4096 bytes (4 KiB) on both disks
blocks    ceil(10000 / 4096) = 3

left    FAT32: directory entry -> table of next pointers
right   ext4:  directory entry -> inode -> extents
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. One file, two layouts

    A disk is a long row of numbered blocks. A file system decides which blocks hold a file and how to find them again. Here notes.txt, 10,000 bytes, sits on two disks with 4 KiB blocks: FAT32 and ext4. It needs 3 blocks.

    file      notes.txt
    size      10000 bytes
    block     4096 bytes (4 KiB) on both disks
    blocks    ceil(10000 / 4096) = 3
    
    left    FAT32: directory entry -> table of next pointers
    right   ext4:  directory entry -> inode -> extents
  2. FAT32's layout

    FAT32 starts with a boot sector, then the file allocation table (FAT), then data clusters numbered from 2. The FAT has one entry per cluster. Each entry holds 0 (free), the next cluster of the same file, or end of chain.

    on disk, in order:
      boot sector + reserved sectors
      FAT (2 copies by default)
      data area: cluster 2, 3, 4, ...
    
    FAT entry n describes cluster n:
      0       free
      m       next cluster of the same file is m
      EOF     last cluster of the file (end of chain)
    FAT32 entries are 32 bits; the top 4 are reserved
  3. The directory points to cluster 5

    A FAT directory is a list of 32-byte entries. NOTES.TXT's entry holds its name, its size and its first cluster: 5. There is no inode: everything FAT knows about the file sits in this entry.

    32-byte directory entry (short name):
      name        NOTES    + TXT   (8 + 3 bytes)
      attributes  1 byte
      time, date
      first       cluster 5
      size        10000 bytes
    
    no inode: the entry itself is the metadata
  4. Following the chain

    For the rest, the kernel follows the chain in the FAT: entry 5 says 6, entry 6 says 9, entry 9 says end of chain. So the file lives in clusters 5, 6 and 9: a linked list kept in a table.

    FAT[5] = 6
    FAT[6] = 9
    FAT[9] = EOF    end of chain
    
    chain   5 -> 6 -> 9
    clusters 7 and 8 belong to other files or are free,
    so the file is in pieces (fragmented)
  5. Byte 9000: walk to it

    Now read byte 9000. 9000 ÷ 4096 = 2, so it is in the file's third cluster. FAT cannot jump there: it must follow 2 links, 5 to 6 to 9. In a big file, the walk grows with the offset.

    the program asks the same way on both disks (C99, POSIX):
    
    #define _POSIX_C_SOURCE 200809L
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void) {
        char c;
        int fd = open("notes.txt", O_RDONLY);
        if (fd < 0) { perror("open"); return 1; }
        ssize_t n = pread(fd, &c, 1, 9000);  /* one byte at offset 9000 */
        printf("read %zd byte\n", n);
        close(fd);
        return n == 1 ? 0 : 1;
    }
    
    block index   9000 / 4096 = 2  (third block, from 0)
    FAT walk      5 -> 6 -> 9     2 hops
    in cluster    byte 808
  6. ext4's layout

    ext4 splits the disk into block groups of 32,768 blocks (128 MiB). A group has a block bitmap, an inode bitmap, an inode table (inode 12 is one row in it) and data blocks. Some groups also keep a backup superblock and group descriptors.

    one block group (4 KiB blocks):
      super block           backup copy, only in some groups
      group descriptors     backup copy, only in some groups
      data block bitmap     1 block
      inode bitmap          1 block
      inode table           many blocks (inode 12 is one row)
      data blocks           the rest
    
    32768 blocks x 4096 bytes = 128 MiB
    with flex_bg (on by default) the bitmaps and inode
    tables of several groups sit together in the first one
  7. Name to inode number

    An ext4 directory entry holds just a name and an inode number: notes.txt is inode 12. The size, owner, times and block map live in the inode. So two names can share one inode, a hard link, which FAT cannot do.

    ext4_dir_entry_2:
      inode       12
      rec_len     length of this entry
      name_len    9
      file_type   regular file
      name        notes.txt
    
    inode 2 is the root directory; 11 is lost+found,
    the first ordinary inode, so files start at 12
  8. An extent: start and length

    Inode 12's record (256 bytes) has room for four extents. One is enough: logical block 0, length 3, starting at block 40960. An extent says these 3 blocks sit side by side, instead of one pointer per block.

    inode 12
      size          10000 bytes
      i_block       extent tree root (60 bytes):
                    header (magic 0xF30A) + up to 4 extents
      extent 0      ee_block = 0   first file block
                    ee_len   = 3   blocks
                    ee_start = 40960
    covers blocks   40960 .. 40962
  9. Byte 9000: one sum

    Byte 9000 is again block 2 of the file. The extent covers blocks 0 to 2, so the kernel adds: 40960 + 2 = 40962. No chain to walk, however far into the file the byte is.

    block index   9000 / 4096 = 2
    extent        0 <= 2 < 3   yes
    disk block    40960 + (2 - 0) = 40962
    in block      byte 808
    
    a file with many pieces has many extents, kept in a
    small tree; one extent covers up to 32,768 blocks
  10. Free space: a bitmap

    To find free blocks, ext4 reads the block bitmap: one bit per block, 1 used, 0 free. Here 40958 to 40962 are used and 40963 is free. FAT keeps the same fact inside its table: a 0 entry, like FAT[8].

    block bitmap (one bit per block in the group):
      40958   1   used
      40959   1   used
      40960   1   used
      40961   1   used
      40962   1   used
      40963   0   free
    
    FAT: free space is entries that hold 0
      FAT[8] = 0   cluster 8 is free
  11. Two maps of the same bytes

    FAT: the directory gives the first cluster, the table links the rest. Simple, and readable almost everywhere. ext4: name, then inode, then extents, plus bitmaps. Random access is one sum, and one inode can have many names.

    both seeks as a Python simulation:
    
    BLOCK = 4096
    fat = {2: "EOF", 3: 4, 4: "EOF", 5: 6, 6: 9, 7: "EOF", 8: 0, 9: "EOF"}
    extents = [(0, 3, 40960)]       # (first logical block, length, start)
    
    def fat_seek(first, offset):     # walk the chain, one entry per block
        c, hops = first, 0
        for _ in range(offset // BLOCK):
            c, hops = fat[c], hops + 1
        return c, hops
    
    def ext4_seek(offset):           # find the extent, then add
        n = offset // BLOCK
        for first, length, start in extents:
            if first <= n < first + length:
                return start + (n - first)
    
    print("FAT  cluster, hops:", fat_seek(5, 9000))
    print("ext4 block:", ext4_seek(9000))
    
    prints:
    FAT  cluster, hops: (9, 2)
    ext4 block: 40962

اختبار يتاح بعد الخطوة الأخيرة.