Repository navigation
Description
WasmFS currently does not cache directory children with the expectation that future caching backends will optionally handle that caching as a configurable layer not built into the core system. The lack of cached directory children causes two problems, however:
-
Backend files do not correspond 1:1 with
Fileobjects. For example, every timegetChildis called on a directory in the Node backend, it has to allocate a newDataFile,Directory, orSymlinkto return, since the system has no record of any previously returned child. With multipleFileobjects pointing to the same underlying file, it is easy to get into a situation where the file metadata is incorrect or inconsistent between different references to what should be the same file. Most importantly, the cached parent pointers can get out of sync when a file is moved or unlinked and one of its correspondingFileobjects is updated accordingly but the others are not. That could lead to the backend receiving read and write requests on files it had thought had already been closed and deleted. It is also the reason that the Node backend cannot currently pass the fullother.test_unlinktest. -
It is not possible to mount backends together arbitrarily as it is in Unix file systems. Right now we only support mounting backends under the in-memory backend, which explicitly owns
Fileobjects and keeps them alive for the lifetime of the files. The mount points for other backends are just moreFileobjects that the in-memory backend treats the same as any other files. In contrast, mounting a backend under e.g. the Node backend would require actually storing some kind of handle to the other backend in the Node file system. If the mounted backend is an in-memory backend, then this materialized mount handle would outlive the backend it points to when the application is shut down. This all gets so complex that we have given up on supporting arbitrary backend mounting for now.
The Linux virtual file system layer does not have these problems because it caches directory structure in nodes called "dcache entries" or "dentries" (docs). These are in-memory cached handles to the underlying inodes and are used for path traversal. They are roughly equivalent to our File objects. Although dentries do not necessarily correspond 1:1 with inodes in Linux, the exceptions seem to be for hard links, which we do not support. Mounting a file system in Linux involves returning the dentry for the root of the mounted system and inserting it as a special entry in the parent dentry; the mounting does not actually write anything back to the inode of the parent dentry AFAICT.
To fix these problems, I plan to add a mapping from name to (isMount: bool, std::shared_ptr<File>) to Directory. This will also pave the path toward letting us allow users to set their own backends as the root backend (since we will be able to mount /proc under their arbitrary backends). The downside will be that fewer external modifications to files in node backends will be picked up automatically, but it was already the case that WasmFS would not always synchronize with such external modifications.
Activity
tlively commentedon Mar 29, 2022
cc @kripken @goldwaving
kripken commentedon Mar 30, 2022
Is 2 a problem? I think it's a limitation we can live with: MemoryBackend is the "skeleton" of everything else. Interesting backends are mounted on top of it at various places. And interesting backends cannot be mounted in each other - what would it even mean, to mount an IDBFS backend inside an OPFS backend?
I also did not understand the connection of 2 to 1.
I'm not sure what isMount does there? Probably related to my misunderstanding about 2.
Otherwise, a map/cache sounds like the right approach in Directory for the default behavior.
tlively commentedon Mar 30, 2022
Arbitrary mounting is a problem if we want to support custom root backends, since presumably we would still want to mount
/dev/*and friends, which would not exist in the custom root backend. It would also be nice if custom root backends did not preclude using additional backends for some subtrees like you can today with an in-memory root backend.Arbitrary mounting like this works fine in Unix file systems, which is what I was trying to describe above. If I have an OPFS backend mounted at
/fooand an IDBFS backend mounted at/foo/barand I also have regular files/foo/data1.txtand/foo/bar/data2.txt, thendata1.txtlives in the OPFS backend anddata2.txtlives in the IDBFS backend. The actual mount point/foo/bardoes not exist in the underlying OPFS storage; instead, it is a special entry in the directory cache that allows path parsing to switch from one backend to another and is never written out to a backend. Marking those special mount point entries is what theisMountbool in the directory cache entry is for.kripken commentedon Mar 30, 2022
I think we could be ok not supporting
/dev/etc. with a custom root backend. The current FS can't replace the root, except in the case of NODERAWFS, which replaces everything. I think that's reasonable: normally you don't replace the root and put custom backends in their own dirs. And when, rarely, you really want to replace it all from the root, then you don't need other stuff.Very interesting about Unix... I did not know that. Are you saying that in that example OPFS would literally not be aware of the IDBFS that looks as if it is nested inside it? That seems confusing. Or would it see it but ignore it when persisting stuff etc.? That seems like a source of possible bugs. I'd prefer to avoid this complexity entirely, if we don't have a use case for it.
tlively commentedon Mar 30, 2022
That's correct. The cache entry in the
Directoryobject would contain a pointer to the IDBFS mount root, but that would be private and not visible at all to the OPFS backend.I'm not worried about extra complexity here—we get arbitrary mounting basically for free once we have the in-memory directory cache as a solution to problem 1 anyway. But there's also no need to implement it right away.
goldwaving commentedon Mar 31, 2022
In IDBFS I keep a map of inodes in the backend so that the same file cannot be allocated twice. I think having that handled at a higher level would be a better option and improve performance. It'll be one less thing backend developers need to worry about.
tlively commentedon Mar 31, 2022
@goldwaving, can you explain that in more detail?
goldwaving commentedon Mar 31, 2022
Every file in IDBFS has a unique inode index. A map of allocated indexes is held in the backend class. When getChild is called, the inode index of the file is retrieved from the database and then the map is checked before creating a new file. If the index is in the map, the associated file is returned otherwise a new file is allocated and is added to the map. When the file is delete, it is removed from the map.
tlively commentedon Apr 1, 2022
Ah yes, it does sound like this change would reduce the amount of code in your backend, then.
kripken commentedon Apr 1, 2022
How would that work with a custom Directory class of a custom backend?
tlively commentedon Apr 1, 2022
This would all be handled in the base
Directoryclass. Backends override that class, but they would not be privy to these details.kripken commentedon Apr 1, 2022
@tlively I see, sgtm!
kripken commentedon Apr 1, 2022
(With that said, if we don't actually need this feature then I think we can live without it. Cleanly separating backends by directory file structure has been enough in the old FS as I mentioned before.)
[WasmFS] Refactor the backend Directory API
[WasmFS] Refactor the backend Directory API (#16739)