Repository navigation
[WasmFS] Refactor the backend Directory API - #16739
Conversation
Previously new entries were inserted into the file system by calling `Backend::createDirectory` or `Backend::createFile`, etc. then passing the result to `Directory::insertChild`. This was suboptimal because it forced `File` objects to be created with no context and separately from the creation of the underlying files. Backends would have to create some reasonable default `File` and then later overwrite its state in the implementation of `insertChild`. To avoid this spooky action at a distance, simplify the control flow, and reduce statefulness in the API, replace `insertChild` with separate `insertDataFile`, `insertDirectory`, and `insertSymlink` methods that both create the underlying files and allocate and return the corresponding `File` objects. But removing `insertChild` from the backend API makes it no longer possible for a backend to contain a reference to a file on a different backend, a capability we need at least for the root in-memory backend. To fix this (and also to improve performance and also to fix an entire class of bugs stemming from having multiple `File` objects corresponding to the same underlying file), add a directory entry cache inspired by the dcache in Linux's [virtual file system](https://github.com/torvalds/linux/blob/master/Documentation/filesystems/vfs.rst). This directory entry cache lives in the base `Directory` class and is manipulated solely by the `Directory::Handle` layer sitting between the syscall implementations and the backends. A new method, `Directory::Handle::mountChild`, can insert `File` objects into this cache without inserting them into the underlying backend, allowing directories to contain children in different backends without backends having to worry about how to represent such children. Finally, add another method, `Directory::insertMove`, that implements an atomic rename to replace the combination of `removeChild` followed by `insertChild` that was previously used to implement rename. Although the previous call sequence worked fine for the in-memory backend, it would have been extremely awkward to support in any persistent backend and was in general not capable of gracefully handling rename errors without data loss. After all these updates to the directory API, there was one additional fix necessary to keep the tests passing. Now that there is a unique `File` corresponding to each underlying file in the Node backend, multiple `open`s of a file will share an underlying file descriptor. But if a file is originally opened for reading only, for example, and later opened for writing, writes will fail because the underlying Node file descriptor was only opened for reading. One possible solution would have been to always have the Node backend open files for both reading and writing, but that would introduce spurious failures when the user only has permissions for one or the other on the underlying file. Instead, detect when the permission on the current underlying file descriptor are insufficient and dynamically try to increase permissions on demand. Resolves #16630.
kripken
left a comment
There was a problem hiding this comment.
Some initial comments - still not sure I get the overall direction here.
|
The remaining test failures seem to be due to the default implementations of |
Since MemoryDirectory is re-used for multiple kinds of in-memory backend, it cannot hard code `MemoryFile` into `createDataFile`. Instead, call out to the backend to initialize a new file. Although the backend methods are meant for creating fresh mount roots, for in-memory backends there is usually no different between a fresh mount root and any other kind of fresh file, so it is reasonable to use those methods here. However, using MemoryDirectory and friends makes no sense for persistent backends, so also update `Backend` to no longer default to using them.
kripken
left a comment
There was a problem hiding this comment.
This is starting to make a lot of sense to me, nice!
| entries.erase(entry); | ||
| } | ||
| return {}; | ||
| return true; |
There was a problem hiding this comment.
It's not clear to me why MemoryBackend needs to specify anything here? That is, all this backend has is the dcache, which is already handled. So the default behavior, with no extensions, should be all we need for removeChild, insertMove.
There was a problem hiding this comment.
Yeah, originally I wanted MemoryFile to do nothing like you're thinking. Unfortunately the getEntries() method makes that difficult. Since it can't assume all the entries are already in the cache, it has to unconditionally query the backend for the directory entries, which means that the backend needs to know what the entries are. Unfortunately that means MemoryDirectory cannot completely abdicate responsibility for keeping track of the entries.
There was a problem hiding this comment.
Since it can't assume all the entries are already in the cache
Hmm, why can't it assume that for MemoryBackend?
My understanding was that the dcache is all there is in that backend - there are no extra things not tracked in the dcache. So MemoryBackend::getEntries() can return an empty set, etc.
There was a problem hiding this comment.
As a special case, it could assume that all entries are in the cache for MemoryBackend, but there is no way to cleanly implement that special case. In the general case across all backends it cannot assume all entries are in the cache because a persistent backend may have entries that have never been requested before.
There was a problem hiding this comment.
True, but can't we remove std::vector<ChildEntry> entries; from MemoryDirectory and then not implement anything? If the default behavior of insert/remove/etc. is to use the dcache, and if not in the dcache to look in the child, then the memory backend would simply do nothing in the latter hooks. And then the dcache would be all there is there.
There was a problem hiding this comment.
How would you implement getEntries() if you did that?
Here's the logic for arriving at the current implementation of getEntries, considering a general persistent backend:
- We can't know that the cache already contains all the entries, so we have to call out to the backend in
getEntries(). - The backend doesn't know what's in the cache, so it returns all the entries.
- The backend can't have included the mount points, so we append them to the entries returned by the backend. We don't append any other cached entries because the backend would already have included them.
The standard contract of Directory::getEntries() is that the backend returns all the entries. The in-memory backend has to do that as well; it doesn't get to be special.
There was a problem hiding this comment.
I agree getEntries() should return all the entries. I would imagine it could work like this: it makes a list of the dcache entries. It then appends to that list the results of a call to the backend's getExtraEntries() or something like that - where that function only adds "extra" things not in the dcache. In other words, Directory::getEntries() would not be virtual, and would call a backend hook that is. Does that make sense? I'm not opposed to calling out to the backend, I just expect to see the backend only add the backend-specific stuff, and so in particular MemoryBackend would have nothing to add.
There was a problem hiding this comment.
Having something like getExtraEntries() would require every other backend to keep track of which entries are already in the dcache, which is not a good trade off. Another option would be to have Directory::Handle::getEntries create entries from the dcache then query all the results from the backend and deduplicate the results. I'm not a huge fan of that either because it's a bunch of extra work that is only useful for the in-memory backend.
There was a problem hiding this comment.
Ah, I see what you mean, when something moved into the dcache it would need to be removed from the "non-dcache" list. That does sound like not the best thing.
|
@kripken I believe all the feedback has now been addressed. |
| // directory with the new `name`, possibly overwriting another file that | ||
| // already exists with that name. The old directory may be the same as this | ||
| // directory. On success, return `true`. Otherwise return `false` without | ||
| // changing any underlying state. |
There was a problem hiding this comment.
I think in another thread we discussed that this is only ever called from rename. Maybe that's worth mentioning here, plus that we assume the relevant locks have already been taken, as rename does?
Previously new entries were inserted into the file system by calling
Backend::createDirectoryorBackend::createFile, etc. then passing theresult to
Directory::insertChild. This was suboptimal because it forcedFileobjects to be created with no context and separately from the creation of the
underlying files. Backends would have to create some reasonable default
Fileand then later overwrite its state in the implementation of
insertChild.To avoid this spooky action at a distance, simplify the control flow, and reduce
statefulness in the API, replace
insertChildwith separateinsertDataFile,insertDirectory, andinsertSymlinkmethods that both create the underlyingfiles and allocate and return the corresponding
Fileobjects.But removing
insertChildfrom the backend API makes it no longer possible fora backend to contain a reference to a file on a different backend, a capability
we need at least for the root in-memory backend. To fix this (and also to
improve performance and also to fix an entire class of bugs stemming from having
multiple
Fileobjects corresponding to the same underlying file), add adirectory entry cache inspired by the dcache in Linux's virtual file
system.
This directory entry cache lives in the base
Directoryclass and ismanipulated solely by the
Directory::Handlelayer sitting between the syscallimplementations and the backends. A new method,
Directory::Handle::mountChild,can insert
Fileobjects into this cache without inserting them into theunderlying backend, allowing directories to contain children in different
backends without backends having to worry about how to represent such children.
Finally, add another method,
Directory::insertMove, that implements an atomicrename to replace the combination of
removeChildfollowed byinsertChildthat was previously used to implement rename. Although the previous call
sequence worked fine for the in-memory backend, it would have been extremely
awkward to support in any persistent backend and was in general not capable of
gracefully handling rename errors without data loss.
After all these updates to the directory API, there was one additional fix
necessary to keep the tests passing. Now that there is a unique
Filecorresponding to each underlying file in the Node backend, multiple
opens of afile will share an underlying file descriptor. But if a file is originally
opened for reading only, for example, and later opened for writing, writes will
fail because the underlying Node file descriptor was only opened for reading.
One possible solution would have been to always have the Node backend open files
for both reading and writing, but that would introduce spurious failures when
the user only has permissions for one or the other on the underlying file.
Instead, detect when the permission on the current underlying file descriptor
are insufficient and dynamically try to increase permissions on demand.
Resolves #16630.