drivers/mtd/filemtd: open the backing file O_RDWR - #20001
Open
AlmAck wants to merge 1 commit into
Open
Conversation
filemtd_initialize() opens its backing file with mode = O_RDONLY | O_WRONLY | O_CLOEXEC; Commit 6161c73 introduced this when it replaced the non-standard O_RDOK | O_WROK pair, describing the change as a pure text substitution. That held while the access mode was a genuine bitmask: O_RDONLY was (1 << 0), O_WRONLY was (1 << 1), and O_RDWR was both bits, so the OR produced O_RDWR. O_ACCMODE was defined as an alias for O_RDWR. Commit 9e141ac then aligned the flags with Linux. The low two bits became an enumeration -- O_RDONLY 0, O_WRONLY 1, O_RDWR 2 -- and O_ACCMODE stopped being an alias for O_RDWR and became an independent mask of 3. OR-ing two members of that enumeration is no longer meaningful: O_RDONLY | O_WRONLY evaluates to 1, and masking it with O_ACCMODE yields O_WRONLY. The file is therefore opened write-only, and fs_read.c rejects every read on it with -EACCES. The failure does not name filemtd: on the simulator it surfaces as a LittleFS mount of a filemtd-backed partition returning -ENOSPC, after which nothing on that volume works. This appears to be an isolated miss rather than a pattern. Grepping the tree for the same construct -- two access-mode constants OR-ed together -- finds only this one call site. The one remaining place that bit-tests the mode, fs/xipfs/xipfs_vfs.c:606, happens to be correct under the new values (and would have been wrong under the old ones). The hostfs NUTTX_O_* mirror and host_oflags_convert() were updated in lockstep and switch on the masked value. Signed-off-by: AlmAck <gluca86@gmail.com>
xiaoxiang781216
approved these changes
Aug 29, 2026
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.
Summary
filemtd_initialize()opens its backing file withCommit 6161c73 ("include/fcntl.h: remove O_RDOK/O_WROK aliases")
introduced this when it replaced the non-standard
O_RDOK | O_WROKpair, describing the change as a pure text substitution. That held while
the access mode was a genuine bitmask:
O_RDONLYwas(1 << 0),O_WRONLYwas(1 << 1),O_RDWRwas both bits, andO_ACCMODEwasdefined as an alias for
O_RDWR. OR-ing the two was meaningful andproduced
O_RDWR.Commit 9e141ac ("include/fcntl.h: align open flags with Linux
values") then made the low two bits an enumeration —
O_RDONLY0,O_WRONLY1,O_RDWR2 — andO_ACCMODEstopped being an alias forO_RDWR, becoming an independent mask of 3. OR-ing two members of thatenumeration is no longer meaningful:
The file is opened write-only, and
fs/vfs/fs_read.c:202rejects everyread on it with
-EACCES.Is this a pattern?
It looks like an isolated miss rather than a systematic one. Grepping
the tree:
fs/xipfs/xipfs_vfs.c:606, which happens to be correct under the newvalues (and would have been wrong under the old ones, so it was
clearly written after the change).
NUTTX_O_*mirror ininclude/nuttx/fs/hostfs.hwas updated in lockstep andhost_oflags_convert()switches onflags & NUTTX_O_ACCMODE.Impact
Affects every user of
filemtd_initialize()— the simulator'sfile-backed MTD,
testing/fs, and any board that layers an MTD over afile. Reads through the device fail, so any filesystem mounted on it
fails to come up.
On the simulator it surfaces as a LittleFS mount of a filemtd-backed
partition returning
-ENOSPC, after which nothing that lives on thatvolume works: the resource pack cannot be read, fonts load with zero
metrics, and a FlashDB partition on the same device logs out-of-bound
writes. None of those point at the open mode.
Testing
Evidence available (captured on this tree):
Host: Linux x86_64, GCC 15. Board:
sim, configuration with a 4 MiBfile-backed MTD (
filemtd_initialize) partitioned into an mtdconfigpartition, a LittleFS volume and a third raw partition.
Before, mounting the LittleFS volume on the filemtd partition:
After, same binary and same backing file, only this patch applied:
The volume mounts, autoformat works, files read back, and the third
partition writes cleanly.