Add non-mmap fallback for unsupported file systems (jffs2) - #1
Open
abcfy2 wants to merge 1 commit into
Open
Conversation
Port the mmap fallback implementation from MetaCubeX/bbolt (commit d4ec34a) to support file systems that do not provide mmap, such as jffs2 on Asus Merlin routers. When mmap fails with ENOSYS/ENODEV/EOPNOTSUPP/ENOTSUP/EINVAL, bbolt now transparently falls back to a heap-backed buffer that mirrors the data file, instead of returning a fatal error. All read/write semantics remain identical; the fallback is automatic and requires no API changes for callers. Fixes SagerNet/sing-box#4251
Author
|
@nekohasekai PLS review this PR. Thanks. After merge this PR, then upgrade dependencies of sing-box will resolve mmap issue. |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
Resolves SagerNet/sing-box#4251.
On embedded devices where the cache file lives on a file system that does not support
mmap(e.g.jffs2on Asus Merlin routers, or certain OpenWrt MTD-backed partitions),bbolt.Open()fails with:The root cause:
jffs2rejectsmmap(PROT_READ, MAP_SHARED)for files openedO_RDWRwithEINVAL, because jffs2 only supports read-only shared mappings. bbolt's entire design assumes mmap, so this is fatal.Solution
Port the mmap fallback implementation from MetaCubeX/bbolt (commit
d4ec34a, authored by @wwqgtxx).When
mmap()fails with an error indicating the file system does not support memory mapping (ENOSYS,ENODEV,EOPNOTSUPP,ENOTSUP,EINVAL), bbolt transparently falls back to a heap-backed buffer that mirrors the data file viaReadAt/WriteAt. This preserves all existing read/write semantics with zero API changes — callers like sing-box need no modifications.Changes
mmap_fallback.gommapFallback(),munmapFallback(),copyToMmapFallback()mmap_error_unix.goisMmapUnsupported()for Linux/macOS/BSD (EINVAL, ENOSYS, ENODEV, EOPNOTSUPP, ENOTSUP)mmap_error_windows.goisMmapUnsupported()for Windowsdb.gommapFallbackfield; mmap fail → fallback branch;munmap()dispatch; newwriteAt()method that mirrors writes to heap buffer in fallback modetx.godb.writeAt()(2 call sites) so heap mirror stays consistentbolt_openbsd.gomsync()in fallback mode (heap buffer doesn't need it)All new symbols are unexported (internal to the package). No public API changes.
Adaptations from upstream Mihomo patch
maxMapSizeinstead ofcommon.MaxMapSize(sagernet fork keeps arch constants inbolt_<arch>.go)Loggercalls (sagernet fork has noLogger()method)MaxSizefield checks (not present in this fork)db.writeAt()method + routetx.gothrough it (sagernet fork lacked this abstraction; Mihomo already had it)Verification
Unit behavior
go build ./...passesgo vet ./...passesEnd-to-end on simulated jffs2
Tested with
mtdram+jffs2mount (the same method documented in MetaCubeX/mihomo#2922):Control (original bbolt, no patch):
Patched bbolt (with fallback):
Data persistence (selector switch → restart → verify):
direct → blockvia Clash APIcache.dbon jffs2block✅The jffs2
mmaprejection (errno=22 EINVAL) was confirmed before testing: