Skip to content

5.1 On Disk Format and Operations

Glen edited this page Nov 14, 2026 · 2 revisions

On-Disk Format and Operations

Relevant source files

Purpose and Scope

This section details the on-disk format, initialization, and core data manipulation routines of CodaFS, the synchronous, block-aligned filesystem designed for CadenzaOS. CodaFS operates directly on block devices without an intervening Virtual File System (VFS) layer, caching, or buffering abstractions src/kernel/fs/coda_fs.zig12-13 It defines explicit on-disk data structures for the superblock, file metadata (FileMeta), directory entries (DirEntry), and disk space management via extents.

Sources: src/kernel/fs/coda_fs.zig:1-13


Superblock Layout and Filesystem Constants

The filesystem superblock is read at mount time and written during formatting (mkfs) src/kernel/fs/coda_fs.zig53-54 It dictates global parameters including block dimensions, the extent allocator region, and the root directory's physical location.

Key magic numbers and structural definitions from src/kernel/fs/coda_fs.zig:

The Superblock structure layout is serialized verbatim to disk via block-writing routines:

pub const Superblock = struct {
    magic:        u64,
    flags:        u64,
    version:      u32,
    block_size:   u32,
    total_blocks: u64,
    policy:       conf.SystemPolicy,
    _pad:         [7]u8,
    latency_threshold_ns: u64,
    sm_start_block:       u64,
    sm_block_count:       u64,
    root_dir_extent_start:  u64,
    root_dir_extent_blocks: u64,
    reserved:     [128]u8,
};

src/kernel/fs/coda_fs.zig67-86

Sources: src/kernel/fs/coda_fs.zig:30-86


File Metadata and Extent Tracking

File and directory metadata are decoupled from directory index entries. A file is represented by a FileMeta structure pointing to up to MAX_EXTENTS contiguous physical block ranges (Extent).

Constants and structures in src/kernel/fs/coda_file.zig:

  • MAX_EXTENTS: 8 (maximum extents tracked per file)
  • MAX_NAME: 64 (maximum filename length in bytes)

FileType distinguishes between regular files and directories (FileType.File and FileType.Directory).

Sources: src/kernel/fs/coda_file.zig:1-48


Directory Structure and Entry Management

Directories are stored as flat arrays of DirEntry records residing within allocated data blocks.

A DirEntry binds a filename to its corresponding metadata location.

An in-memory view of a directory block is managed by the Directory struct, providing core lookup and mutation operations:

  • lookup(name): Iterates through entries to find a match where name_len and byte sequences align.
  • addEntry(entry): Inserts a new entry into the first available slot (name_len == 0).
  • renameEntry(old_name, new_name): Verifies existence, name constraints, and updates string buffers.
  • removeEntry(name): Unimplemented stub returning error.NotImplemented.

Diagram: Directory Operations Mapping

diagram

Sources: src/kernel/fs/coda_file.zig:49-150


Mount and Format Operations

The CodaFs struct maintains the runtime mount context, binding an underlying block device to the parsed superblock, space manager, and root directory src/kernel/fs/coda_fs.zig110-117

Mounting (CodaFs.mount)

  1. Reads the superblock from conf.SB_LBAsrc/kernel/fs/coda_fs.zig132-133
  2. Validates sb.magic == CODA_MAGICsrc/kernel/fs/coda_fs.zig135
  3. Initializes the SpaceManager via SpaceManager.initFromDisk(...)src/kernel/fs/coda_fs.zig137-142
  4. Constructs an unpopulated root directory handle src/kernel/fs/coda_fs.zig144-154

Formatting (CodaFs.mkfs)

  1. Validates device block sizes against conf.BASE_IO_BUF_SIZEsrc/kernel/fs/coda_fs.zig161
  2. Establishes layout offsets for the superblock, space manager region (sm_start_block, 16 blocks), and data storage src/kernel/fs/coda_fs.zig165-168
  3. Invokes SpaceManager.initFresh(...) to mark allocation bitmaps src/kernel/fs/coda_fs.zig173-178
  4. Allocates a 4-block root directory extent src/kernel/fs/coda_fs.zig180

Diagram: CodaFs Initialization and Mount Flow

diagram

Sources: src/kernel/fs/coda_fs.zig:110-186


Path Resolution and Block Operations

Path resolution traverses hierarchical directory structures block by block to map symbolic paths to physical PathResult records containing starting LBAs, block lengths, and type flags src/kernel/fs/coda_fs.zig88-93

Because CodaFS bypasses complex VFS caching layers, all read and write transactions execute synchronously against the underlying BlockDevice implementation using raw block offsets.

Sources: src/kernel/fs/coda_fs.zig:88-93, src/kernel/fs/coda_fs.zig:1-13

Clone this wiki locally