# CodaFS Filesystem and Storage Relevant source files - [src/kernel/fs/coda_fs.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig) ## Purpose and Scope This section provides a high-level overview of the CadenzaOS storage stack, bridging raw hardware input/output up to the CodaFS filesystem and binary program loaders. CodaFS is designed as a direct, synchronous, block-aligned filesystem with no internal VFS or caching layers [src/kernel/fs/coda_fs.zig12-13](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L12-L13) As a parent page, this document outlines the macro architecture of the storage system. Detailed technical specifications are delegated to the following child pages: - [On-Disk Format and Operations](/GlenMeehan/CadenzaOS/5.1-on-disk-format-and-operations) - [Space Manager and Block Devices](/GlenMeehan/CadenzaOS/5.2-space-manager-and-block-devices) - [Binary Format and Program Loading](/GlenMeehan/CadenzaOS/5.3-binary-format-and-program-loading) Title: CodaFS Storage Stack Architecture ![diagram](mermaids/5-codafs-filesystem-and-storage-diagram-1.png) Sources: `src/kernel/fs/coda_fs.zig:1-30`, `src/kernel/fs/coda_sm.zig:1-20`, `src/kernel/drivers/ata.zig:1-18`, `src/kernel/fs/ata_block_device.zig:1-15` --- ## 5.1 On-Disk Format and Operations CodaFS relies on a strict on-disk layout anchored by a primary `Superblock` located at configuration-defined LBA constants (`conf.SB_LBA`) [src/kernel/fs/coda_fs.zig53-86](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L53-L86) The superblock records structural invariants such as filesystem magic (`CODA_MAGIC`), block dimensions, space manager boundaries, and the physical extent of the root directory [src/kernel/fs/coda_fs.zig56-63](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L56-L63) It also embeds a `SystemPolicy` which influences telemetry thresholds and future AI-guided behavior [src/kernel/fs/coda_fs.zig50-51](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L50-L51) File metadata (`FileMeta`) and directory entries (`DirEntry`) describe objects on disk, tracking physical storage ranges per file without complex indirection layers [src/kernel/fs/coda_file.zig19-64](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_file.zig#L19-L64) Directory structures support lookup, addition, and renaming operations directly in-memory [src/kernel/fs/coda_file.zig73-118](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_file.zig#L73-L118) The `CodaFs` struct represents a mounted filesystem, holding the `Superblock`, `SpaceManager`, and a handle to the root directory [src/kernel/fs/coda_fs.zig99-117](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L99-L117) For comprehensive coverage of superblock validation, `mkfs` formatting, path resolution, and directory mutations, see the child page [On-Disk Format and Operations](/GlenMeehan/CadenzaOS/5.1-on-disk-format-and-operations). Sources: `src/kernel/fs/coda_fs.zig:53-86`, `src/kernel/fs/coda_fs.zig:50-51`, `src/kernel/fs/coda_file.zig:19-64`, `src/kernel/fs/coda_file.zig:73-118`, `src/kernel/fs/coda_fs.zig:99-117` --- ## 5.2 Space Manager and Block Devices The storage subsystem decouples filesystem logic from physical hardware via the generic `BlockDevice` interface [src/kernel/fs/block_device.zig1-25](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/block_device.zig#L1-L25) Hardware drivers like the ATA PIO driver (`ata.zig`) [src/kernel/drivers/ata.zig1-18](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/drivers/ata.zig#L1-L18) are wrapped by partition adapters (`ata_block_device.zig`) to translate filesystem-relative blocks into absolute disk sectors [src/kernel/fs/ata_block_device.zig31-61](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/ata_block_device.zig#L31-L61) Additionally, RAM-backed block devices (`ramdisk.zig`) provide write-through caching over memory and physical storage [src/kernel/fs/ramdisk.zig5-18](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/ramdisk.zig#L5-L18) Disk space allocation is managed by `SpaceManager` (`coda_sm.zig`), which maintains free-space extents (`Extent`) and persists allocator state via an on-disk header [src/kernel/fs/coda_sm.zig37-61](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_sm.zig#L37-L61) The `SpaceManager` is initialized either freshly during `mkfs` or from disk during `mount`[src/kernel/fs/coda_fs.zig138-142](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L138-L142)[src/kernel/fs/coda_fs.zig173-178](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/fs/coda_fs.zig#L173-L178) Title: Block Device Hierarchy ![diagram](mermaids/5-codafs-filesystem-and-storage-diagram-2.png) Sources: `src/kernel/fs/coda_fs.zig:78-83`, `src/kernel/fs/coda_sm.zig:37-61`, `src/kernel/fs/block_device.zig:1-25`, `src/kernel/fs/ata_block_device.zig:31-61`, `src/kernel/fs/ramdisk.zig:5-18`, `src/kernel/drivers/ata.zig:69-84`, `src/kernel/fs/coda_fs.zig:138-142`, `src/kernel/fs/coda_fs.zig:173-178` For complete details on first-fit extent allocation, RAM disk drivers, and ATA polling routines, see the child page [Space Manager and Block Devices](/GlenMeehan/CadenzaOS/5.2-space-manager-and-block-devices). Sources: `src/kernel/fs/coda_sm.zig:37-61`, `src/kernel/fs/block_device.zig:1-25`, `src/kernel/fs/ata_block_device.zig:31-61`, `src/kernel/fs/ramdisk.zig:5-18`, `src/kernel/drivers/ata.zig:1-18` --- ## 5.3 Binary Format and Program Loading Userland and system executables stored within CodaFS conform to the custom CDZP flat binary format. Every program image begins with a structured binary header containing magic values ('CDZP'), version metadata, image sizes, and entry point offsets. Build-time tools (`tools/pack.sh`) bundle compiled binaries into storable disk images, while kernel loaders handle embedded application installation and spawn-time memory preparation. For technical breakdowns of header validation rules, linker script integration, and process image loading, see the child page [Binary Format and Program Loading](/GlenMeehan/CadenzaOS/5.3-binary-format-and-program-loading). Sources: `src/kernel/fs/coda_fs.zig:1-30`