OP-TEE Storage Synchronization Mechanisms - Analysis
Overview
This document analyzes OP-TEE’s storage synchronization mechanisms, including commit protocols, atomic write operations, consistency checking, and recovery mechanisms that ensure data integrity across storage operations.Atomic Commit Protocols
Hash Tree Synchronization
- Location:
/home/dzb/optee/optee_os/core/tee/fs_htree.c - Function:
tee_fs_htree_sync_to_storage() - Strategy: Two-phase commit with versioning for atomic updates
Dual-Version Storage Strategy
Counter-Based Version Selection
Write-Ahead Logging (WAL) Implementation
Node-Level Versioning
Each hash tree node maintains two versions, switching between them during updates:Post-Order Traversal for Consistency
Changes committed from leaves to root, ensuring parent hashes reflect child changes:Directory File Synchronization
Commit Protocol
- Location:
/home/dzb/optee/optee_os/core/tee/fs_dirfile.c - Function:
tee_fs_dirfile_commit_writes() - Integration: Calls underlying hash tree commit mechanism
REE File System Integration
- Location:
/home/dzb/optee/optee_os/core/tee/tee_ree_fs.c - Counter Management: Monotonic counter synchronization with normal world
Consistency Checking Mechanisms
Hash Tree Verification
Complete tree verification during open operations:Authenticated Encryption Verification
Root metadata protected with AES-GCM:Recovery After Power Failure
Rollback-Safe Design
The dual-version storage ensures atomic commits:- Phase 1: Write new versions of all changed nodes
- Phase 2: Update header counter to commit transaction
- Recovery: On restart, select valid version based on counter
Counter Validation
RPMB File System Synchronization
Write Counter Integration
- Location:
/home/dzb/optee/optee_os/core/tee/tee_rpmb_fs.c - Version: FS_VERSION constant for format compatibility
- Counter: RPMB hardware write counter for anti-rollback
Atomic RPMB Operations
RPMB operations are inherently atomic due to hardware write counter:Thread Safety and Concurrency
Mutex Protection
Global mutex protects directory operations:Reference Counting
Directory handle shared across operations with reference counting:Error Handling and Cleanup
Transactional Cleanup
Failed operations trigger automatic cleanup:Performance Considerations
Batched Operations
- Node Updates: All dirty nodes committed in single traversal
- I/O Optimization: Minimal number of storage operations
- Memory Efficiency: Temporary blocks reused across operations
Lazy Synchronization
- Dirty Tracking: Only modified nodes synchronized to storage
- Deferred Writes: Changes batched until explicit sync
- Reference Counting: Shared resources minimize overhead