# littlefs-project/littlefs architecture

> Architecture diagram of the GitHub repository littlefs-project/littlefs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/littlefs-project/littlefs
- Repository: https://github.com/littlefs-project/littlefs
- Video tour (about a minute): https://gitdiagram.com/littlefs-project/littlefs/video

## Overview

##### Project Type and Purpose
- littlefs is a filesystem implementation written in C (C99 compliant) that targets embedded environments. Its focus is on power-loss resilience, dynamic wear leveling, and low resource (RAM/ROM) consumption.
- The README shows that the filesystem uses two main architectural ideas—a small log for fast metadata updates, and larger copy-on-write (COW) structures for file data storage. There is an emphasis on safety (ensuring atomic operations and integrity even after power interruption).

##### Main Components
a. Filesystem Core:
– The core of littlefs is located in files like lfs.c and lfs.h, and supporting operations in lfs_util.c/h.
– This core manages mounting, formatting, file and directory operations, and synchronizing file operations.
– Represent the “Filesystem Engine” as a central component that processes file operations by using state structures such as the lfs_t type and configuration struct (which supplies block device callbacks).

b. Block Device Abstraction:
– The bd/ directory provides multiple emulated block device implementations (e.g., lfs_emubd, lfs_filebd, lfs_rambd). This abstraction is essential for interacting with underlying storage hardware.

c. Data Structures / Allocation Strategy:
– Reflect the two-tiered storage used by littlefs: the small metadata logs (metadata pairs) and the COW structures for file data.
– Include a “Block Allocator” component that feeds both layers, provides dynamic wear leveling and controls the number of erases per block.

d. Configuration and Callback Interface:
– The configuration structure (as described in the README) is provided by the user and includes function pointers to the block device’s low-level operations.
– Include a “Configuration Interface” that feeds initialization settings to the Filesystem Core.

e. Testing and Benchmarking Infrastructure:
– The branches for tests (files under tests/ and runners/) and benchmark scripts (benches/ and scripts/) represent how littlefs is verified and profiled.
– Show these as peripheral modules (e.g., “Test Suite Module” and “Benchmark/Script Tools”) that interact with the filesystem core for validation and performance testing.

##### Relationships and Data Flow
- The user (or higher-level application) initializes the filesystem by providing a configuration structure. This configuration is passed to the Filesystem Core.
- The Filesystem Core uses the provided block device callbacks to perform low-level storage operations.
- The block allocator interacts with both the metadata logging mechanism and the COW data structure components.
- Data flow: When a file is accessed, the Filesystem Core routes the request through the configuration layer to the Block Device Abstraction. The read/write operations are then channeled back, with integrity checks and wear leveling applied.
- Testing components such as test runners and script utilities drive simulations by activating the filesystem core with various configurations (as seen in the tests/ toml files and scripts in the scripts/ directory).

##### Architectural Patterns and Design Principles
- Copy-on-Write (COW): Highlight how file data is updated safely by drafting a separate component that symbolically represents this methodology.
- Two-layered Cake Architecture: Visually separate the metadata log layer (small logs for updates) from the COW storage layer.
- Abstraction: The block device operations provide a clear abstraction layer, so this should be drawn as a distinctly separated “external” component.
- Resilience: Show that every file operation (mount, read, write, sync) interacts with a robust error-checking path (as indicated by the return codes and error handling in the README).

##### Essential Elements
- Clear labels for components: "Filesystem Core (lfs.c/h)", "Configuration Struct", "Block Device Abstraction (bd/)", "Block Allocator", "Data Structures (COW & Metadata Log)", "Test Suite & Benchmark Tools".
- From the configuration input to Filesystem Core.
- From Filesystem Core to the Test/Runners/Benchmark modules.
- An outline of the “two-layered” design (metadata logs & COW file data) either as a nested structure or as side-by-side subcomponents within the Filesystem Core.

## Diagram (Mermaid)

```mermaid
flowchart TD
    User("User/Application (provides configuration)"):::external
    User -->|"initialize"| FS_Engine

    subgraph "Filesystem Core"
        FS_Engine("Filesystem Engine (lfs Core)"):::core
        FS_Engine -->|"manages"| BlockAllocator("Block Allocator (Metadata Log & COW)"):::core
    end

    FS_Engine -->|"blockOps"| BlockDevice("Block Device Abstraction (bd/)"):::external
    BlockDevice -->|"feedback"| FS_Engine

    FS_Engine -->|"validate"| TestSuite("Test Suite (tests/)"):::utility
    FS_Engine -->|"execute"| TestRunners("Test Runners (runners/)"):::utility
    FS_Engine -->|"profile"| Benchmarks("Benchmark Files (benches/)"):::utility
    FS_Engine -->|"support"| ScriptTools("Script Tools (scripts/)"):::utility

    click FS_Engine "https://github.com/littlefs-project/littlefs/blob/master/lfs.c"
    click FS_Engine "https://github.com/littlefs-project/littlefs/blob/master/lfs.h"
    click FS_Engine "https://github.com/littlefs-project/littlefs/blob/master/lfs_util.c"
    click FS_Engine "https://github.com/littlefs-project/littlefs/blob/master/lfs_util.h"
    click BlockDevice "https://github.com/littlefs-project/littlefs/tree/master/bd/"
    click TestSuite "https://github.com/littlefs-project/littlefs/tree/master/tests/"
    click TestRunners "https://github.com/littlefs-project/littlefs/tree/master/runners/"
    click Benchmarks "https://github.com/littlefs-project/littlefs/tree/master/benches/"
    click ScriptTools "https://github.com/littlefs-project/littlefs/tree/master/scripts/"

    classDef core fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef external fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef utility fill:#fff3cd,stroke:#856404,stroke-width:2px;
```
