ENع
←Inodes and data blocks
Lesson 41 · File systems

Inodes and data blocks

15 min11 steps5 questionsAdvanced
About this lesson

A disk only knows numbered blocks, yet you open files by name, read from the middle of them and make them grow. How? A Unix-style file system splits every file in three: a name in a directory, an inode holding the file's facts and block pointers, and the data blocks themselves. Follow open, pread and pwrite on a small 64-block disk as the kernel walks from the root inode to a directory, to the file's inode, to the right data block, and takes a free block from the bitmap when the file grows. The Linux lessons The file system and Disks, partitions and mounts show the same disk from the user's side.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 41 · Interactive
01 / 11

A file is three things

On disk a file is split in three. Its name sits in a directory. Its inode, a small fixed-size record, holds the facts about it and where its bytes are. The bytes themselves sit in data blocks of 4096 bytes.

name     in a directory:   "notes" -> inode 20
inode    number 20: type, size, owner, times, links,
         and pointers to its data blocks
data     blocks 9 and 10: the 6000 bytes of the file

the inode number is the file's real identity;
the name is just an entry that points to it
Loading figure…
or ← →
Read the lesson step by step
  1. A file is three things

    On disk a file is split in three. Its name sits in a directory. Its inode, a small fixed-size record, holds the facts about it and where its bytes are. The bytes themselves sit in data blocks of 4096 bytes.

    name     in a directory:   "notes" -> inode 20
    inode    number 20: type, size, owner, times, links,
             and pointers to its data blocks
    data     blocks 9 and 10: the 6000 bytes of the file
    
    the inode number is the file's real identity;
    the name is just an entry that points to it
  2. The disk's map

    Our small disk has 64 blocks of 4 KB. Block 0 is the superblock, block 1 the inode bitmap, block 2 the data bitmap. Blocks 3 to 7 hold 80 inodes of 256 bytes. Blocks 8 to 63 hold data.

    block    what                     (OSTEP's vsfs)
    0        superblock   sizes and where each region starts
    1        inode bitmap one bit per inode,  1 = used
    2        data bitmap  one bit per data block
    3-7      inode table  80 inodes x 256 B, 16 per block
    8-63     data region  file and directory contents
    
    data bitmap, blocks 8-15:  1110 0000
  3. Inside an inode

    An inode records the file's type, size, owner and permissions, times, link count, and a list of block pointers. Pointer 0 names the disk block holding bytes 0 to 4095, pointer 1 the next 4096, and so on. It never stores the name.

    inode 20   (256 bytes in block 4)
      type     regular file
      size     6000 bytes
      mode     owner, permissions
      times    accessed, modified, changed
      links    1      (names pointing here)
      ptr 0    block 9   bytes    0 - 4095
      ptr 1    block 10  bytes 4096 - 8191
      ptr 2    -        (empty)
      ...      no file name anywhere
  4. open() starts at the root

    open() gets a path, but the disk finds files by inode number. Every path starts at the root directory, whose inode number is always known: 2. So the kernel reads inode 2 from block 3.

    line 1   fd = open("/notes", O_RDWR);
    
    root inode   2   (fixed, known when the disk is mounted)
    where        2 x 256 = 512 bytes into the table
                 (this toy counts slots from 0; real ext4 counts from 1)
                 -> block 3, byte 512
    inode 2      type dir, ptr 0 = block 8
  5. Read the directory

    A directory is a file whose data is a list of name and inode number pairs. Inode 2's pointer leads to block 8. The kernel scans it for "notes" and finds inode 20.

    block 8   the root directory's entries
      name      inode
      .         2
      ..        2
      notes     20     <- found
  6. Fetch inode 20

    Inodes are numbered slots, so finding one is arithmetic: 20 × 256 = 5120 bytes into the table, which is block 4, byte 1024. The kernel reads it, checks the permissions and returns file descriptor 3.

    byte    = 20 x 256 = 5120
    block   = 3 + 5120 / 4096 = 3 + 1 = 4
    offset  = 5120 mod 4096 = 1024
    
    permissions ok -> open returns the lowest free fd
    fd = 3      (0, 1 and 2 are already taken)
    reads so far: inode 2, block 8, inode 20 (3 reads)
  7. Which block holds byte 5000?

    pread asks for 100 bytes starting at byte 5000. 5000 ÷ 4096 is 1 remainder 904, so pointer 1 tells the kernel: block 10. It reads that block and copies bytes 904 to 1003 into the buffer.

    line 2   pread(fd, b, 100, 5000);
    
    index    = 5000 / 4096 = 1
    offset   = 5000 mod 4096 = 904
    ptr 1    -> block 10
    copy     block 10, bytes 904-1003  ->  b
    returns  100
    bitmaps  not touched: reading allocates nothing
  8. The file must grow

    pwrite puts 4000 bytes at byte 6000, so the last byte is 9999. 9999 ÷ 4096 is 2, but pointer 2 is empty. The kernel scans the data bitmap for the first 0, finds block 11 free, and sets its bit to 1.

    line 3   pwrite(fd, b, 4000, 6000);
    
    bytes    6000 - 9999
    last     9999 / 4096 = 2   ptr 2 empty
    bitmap   1110 0000   (blocks 8-15)
                ^ first 0: block 11
    bitmap   1111 0000
  9. Write data, then the inode

    The bytes go into block 10 (2192 bytes) and block 11 (1808). Then the inode changes: pointer 2 = 11, size = 10000. One call wrote four blocks: bitmap, two data blocks, inode. A crash midway leaves them disagreeing.

    ptr 1  block 10   bytes 1904-4095 of it   (2192 bytes)
    ptr 2  block 11   bytes 0-1807 of it   (1808 bytes)
    inode 20  ptr 2 = 11   size 6000 -> 10000
    returns   4000
    
    blocks written: data bitmap (2), data 10, data 11,
    inode table block 4. If the power fails between them,
    the disk is inconsistent: see crash consistency
    and journaling, later in this module.
  10. Big files: indirect blocks

    An inode has room for few pointers. ext2 and ext3 keep 15: twelve direct ones cover 48 KB. The 13th points to an indirect block holding 1024 more pointers, about 4 MB. A double indirect block passes 4 GB. ext4 uses extents instead.

    ext2/ext3 inode: 15 pointers of 4 bytes, 4 KB blocks
      ptr 0-11   direct            12 x 4 KB =   48 KB
      ptr 12     indirect block  1024 x 4 KB =    4 MB
      ptr 13     double indirect 1024^2 x 4 KB =  4 GB
      ptr 14     triple indirect 1024^3 x 4 KB =  4 TB
    
    (12 + 1024) x 4 KB = 4144 KB with one indirect block
    ext4: extents, "start block + length" runs instead
  11. Why split name and inode?

    Because names and files are separate, two names can point to one inode (a hard link; the link count says how many). Fixed-size inodes are found by arithmetic, and pointers let a file's blocks sit anywhere on the disk.

    the whole walk as a Python simulation:
    
    BLOCK, INODE = 4096, 256
    dirs = {8: {".": 2, "..": 2, "notes": 20}}      # block 8: the root directory
    inodes = {2: {"size": 4096, "ptr": [8]},
              20: {"size": 6000, "ptr": [9, 10]}}
    bitmap = [1, 1, 1, 0, 0, 0, 0, 0]               # data blocks 8..15
    
    def where(inum):                  # inode number -> disk block, byte
        byte = inum * INODE
        return 3 + byte // BLOCK, byte % BLOCK
    
    def block_of(ino, offset):        # file offset -> disk block, byte
        return ino["ptr"][offset // BLOCK], offset % BLOCK
    
    root = inodes[2]                  # the root is always inode 2
    inum = dirs[root["ptr"][0]]["notes"]
    print("notes is inode", inum, "in block %d at byte %d" % where(inum))
    ino = inodes[inum]
    print("byte 5000 is in block %d at byte %d" % block_of(ino, 5000))
    end = 6000 + 4000
    while len(ino["ptr"]) * BLOCK < end:     # grow: first free block
        i = bitmap.index(0)
        bitmap[i] = 1
        ino["ptr"].append(8 + i)
    ino["size"] = max(ino["size"], end)
    print("pointers", ino["ptr"], "size", ino["size"])
    
    prints:
    notes is inode 20 in block 4 at byte 1024
    byte 5000 is in block 10 at byte 904
    pointers [9, 10, 11] size 10000

Quiz Unlocks after the last step.