Filesystem Layers
Mount System
The mount system manages different filesystem types:fs/mount.c:9
Finding Mounts
Mounts are stored in order of decreasing path length, so the most specific mount is found first:fs/mount.c:26
Fake Filesystem
The “fake” filesystem uses a SQLite database to store metadata (permissions, ownership, inode numbers) while storing actual file data in the real iOS filesystem.Why Fake FS?
Problem: iOS Limitations
Problem: iOS Limitations
iOS filesystems don’t support:
- UNIX file permissions (chmod, chown)
- Hard links
- Device nodes
- Named pipes (FIFOs)
- Arbitrary inode numbers
Solution: Metadata Database
Solution: Metadata Database
Store metadata in SQLite:
- Full UNIX permissions and ownership
- Hard link reference counting
- Device major/minor numbers
- Custom inode numbers
- Leverage iOS storage
- Support file sharing with iOS apps
- Enable iOS backup/restore
Database Structure
fs/fake-db.h:8
Key Operations
- Path Lookup
- File Creation
- Inode Operations
- Hard Links
Transactions
Database operations use transactions for atomicity:Real Filesystem
The “real” filesystem provides direct passthrough to iOS files:fs/real.c:62
Flag Translation
Linux and iOS use different flag values:fs/real.c:36
Stat Translation
File metadata is translated between formats:fs/real.c:87
File Descriptors
iSH maintains its own file descriptor table:fs/fd.h:14
FD Operations
fs/fd.h:131
FD Table
fs/fd.h:171
Special Filesystems
/proc - Process Information
fs/proc.c:8
/proc/self→ current process/proc/[pid]/→ process-specific info/proc/[pid]/stat→ process statistics/proc/[pid]/maps→ memory mappings/proc/[pid]/fd/→ open file descriptors
/dev - Device Nodes
fs/dev.c:9
/tmp - Temporary Files
Implemented entirely in RAM - files are lost on app restart.TTY/PTY Support
Terminal and pseudo-terminal support:fs/tty.c:21
- Console TTY (stdin/stdout/stderr)
- PTY master/slave pairs (for ssh, tmux, etc.)
Filesystem Operations
- Open
- Read/Write
- Stat
Performance Considerations
Database Overhead
Database Overhead
SQLite operations add overhead to metadata-heavy operations (stat, readdir). The database uses prepared statements and transactions to minimize overhead.
Memory Copies
Memory Copies
Data must be copied between user memory and kernel buffers, then to/from iOS files. This is slower than native file I/O.
Mount Lookup
Mount Lookup
Each path operation requires a mount lookup. Mounts are sorted by length to optimize this.
Inode Caching
Inode Caching
Frequently accessed inodes are cached to avoid repeated database queries.
Related Topics
Syscalls
How filesystem operations are invoked
Emulation
Memory management for file I/O