File System Layers Diagram

---
config:
  layout: dagre
---
flowchart RL
 subgraph MF["memory inode-table side"]
    direction TB
        LOCK_FUNCS["inode locking<br><br>ilock<br>iunlock"]
        ICACHE_FUNCS["inode cache / lifetime<br><br>iinit<br>iget<br>idup<br>iput<br>iunlockput<br>ireclaim"]
  end

 subgraph M["memory inode side"]
    direction LR
        ICACHE["icache / itable<br><br>active struct inode objects"]
        INODE["struct inode<br><br>dev<br>inum<br>ref<br>lock<br>valid<br>type<br>major / minor<br>nlink<br>size<br>addrs[]"]
        SB_MEM["struct superblock sb<br><br>magic<br>size<br>nblocks<br>ninodes<br>nlog<br>logstart<br>inodestart<br>bmapstart"]
  end

 subgraph BF["bridge: memory inode to disk fs"]
    direction TB
        PATH_FUNCS["pathname layer<br><br>skipelem<br>namex<br>namei<br>nameiparent"]
        DIR_FUNCS["directory layer<br><br>namecmp<br>dirlookup<br>dirlink"]
        DATA_FUNCS["inode data I/O<br><br>bmap<br>readi<br>writei"]
        META_FUNCS["inode metadata<br><br>ialloc<br>iupdate<br>itrunc<br>stati"]
  end

 subgraph LF["log / transaction layer"]
    direction TB
        LOG_FUNCS["transaction control<br><br>initlog<br>begin_op<br>end_op"]
        LOG_WRITE_FUNCS["logged write + recovery<br><br>log_write<br>commit<br>write_log<br>write_head<br>install_trans<br>recover_from_log<br>read_head"]
  end

 subgraph LM["in-memory log state"]
    direction LR
        LOG_STATE["struct log<br><br>lock<br>start<br>dev<br>outstanding<br>committing<br>lh"]
        LOG_HDR_MEM["log.lh<br><br>n<br>block[]"]
  end

 subgraph DF["disk filesystem side"]
    direction TB
        SUPER_FUNCS["superblock init<br><br>readsb<br>fsinit"]
        BLOCK_FUNCS["block allocation<br><br>bzero<br>balloc<br>bfree"]
  end

 subgraph BC["buffer cache layer"]
    direction TB
        BC_FUNCS["buffer cache API<br><br>binit<br>bread<br>bwrite<br>brelse<br>bpin<br>bunpin"]
        BCACHE["bcache<br><br>lock<br>buf[NBUF]<br>head"]
        BUF["struct buf<br><br>valid<br>disk<br>dev<br>blockno<br>refcnt<br>lock<br>prev / next<br>data[BSIZE]"]
  end

 subgraph D["disk side"]
    direction LR
        BOOT_BLOCK["boot block"]
        SUPER_DISK["superblock on disk<br><br>filesystem geometry"]
        BITMAP["bitmap blocks<br><br>free / used block map"]
        DINODE["struct dinode<br><br>type<br>major<br>minor<br>nlink<br>size<br>addrs[]"]
        DATA_BLOCKS["data blocks<br><br>file bytes<br>directory dirents<br>indirect blocks"]
        LOG_DISK["log region on disk<br><br>log header block<br>logged block copies"]
        DISK_DEVICE["block device<br><br>virtio disk"]
  end

    MF -- "manages in-memory inode objects" --> M
    BF -- uses struct inode and superblock --> M
    BF -- write-side operations use transactions / log_write --> LF
    BF -- reads and writes disk blocks through buffers --> BC
    DF -- block allocation updates are logged --> LF
    DF -- reads/writes bitmap and superblock blocks --> BC
    LF -- keeps transaction state in RAM --> LM
    LF -- copies committed blocks through buffer cache --> BC
    BC -- caches and transfers disk blocks --> D
    ICACHE -- contains active inodes --> INODE
    LOG_STATE -- contains current transaction header --> LOG_HDR_MEM
    BCACHE -- contains many cached buffers --> BUF
    BUF -- transferred by virtio_disk_rw --> DISK_DEVICE

     ICACHE_FUNCS:::memfunc
     LOCK_FUNCS:::memfunc
     SUPER_FUNCS:::diskfunc
     BLOCK_FUNCS:::diskfunc
     META_FUNCS:::mixedfunc
     DATA_FUNCS:::mixedfunc
     DIR_FUNCS:::mixedfunc
     PATH_FUNCS:::mixedfunc
     LOG_FUNCS:::logfunc
     LOG_WRITE_FUNCS:::logfunc
     BC_FUNCS:::buffunc
     BCACHE:::bufmem
     BUF:::bufmem
     SB_MEM:::mem
     ICACHE:::mem
     INODE:::mem
     LOG_STATE:::logmem
     LOG_HDR_MEM:::logmem
     BOOT_BLOCK:::disk
     SUPER_DISK:::disk
     BITMAP:::disk
     DINODE:::disk
     DATA_BLOCKS:::data
     LOG_DISK:::logdisk
     DISK_DEVICE:::device
    classDef memfunc fill:#FFFFFF,stroke:#111,stroke-width:3px,color:#111
    classDef diskfunc fill:#E9F1FF,stroke:#111,stroke-width:2px,color:#111
    classDef mixedfunc fill:#EDE7D4,stroke:#111,stroke-width:2px,color:#111
    classDef logfunc fill:#F5E6FF,stroke:#111,stroke-width:2px,color:#111
    classDef buffunc fill:#E6FFF2,stroke:#111,stroke-width:2px,color:#111
    classDef mem fill:#FFFFFF,stroke:#111,stroke-width:3px,color:#111
    classDef logmem fill:#F9F0FF,stroke:#111,stroke-width:2px,color:#111
    classDef bufmem fill:#F0FFF7,stroke:#111,stroke-width:2px,color:#111
    classDef disk fill:#E9F1FF,stroke:#111,stroke-width:2px,color:#111
    classDef logdisk fill:#EFE3FF,stroke:#111,stroke-width:2px,color:#111
    classDef data fill:#F8F8F8,stroke:#111,stroke-width:2px,color:#111
    classDef device fill:#EEEEEE,stroke:#111,stroke-width:2px,color:#111