Skip to content

[WasmFS] Caching directory structure #16630

Closed

Description

@tlively

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:

  1. Backend files do not correspond 1:1 with File objects. For example, every time getChild is called on a directory in the Node backend, it has to allocate a new DataFile, Directory, or Symlink to return, since the system has no record of any previously returned child. With multiple File objects 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 corresponding File objects 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 full other.test_unlink test.

  2. 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 File objects and keeps them alive for the lifetime of the files. The mount points for other backends are just more File objects 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

  1. tlively commented on Mar 29, 2022

    @tlively
    MemberAuthor
  2. kripken commented on Mar 30, 2022

    @kripken
    Member

    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.

    To fix these problems, I plan to add a mapping from name to (isMount: bool, std::shared_ptr) to Directory

    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.

  3. tlively commented on Mar 30, 2022

    @tlively
    MemberAuthor

    Is 2 a problem?

    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.

    And interesting backends cannot be mounted in each other - what would it even mean, to mount an IDBFS backend inside an OPFS 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 /foo and an IDBFS backend mounted at /foo/bar and I also have regular files /foo/data1.txt and /foo/bar/data2.txt, then data1.txt lives in the OPFS backend and data2.txt lives in the IDBFS backend. The actual mount point /foo/bar does 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 the isMount bool in the directory cache entry is for.

  4. kripken commented on Mar 30, 2022

    @kripken
    Member

    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.

  5. tlively commented on Mar 30, 2022

    @tlively
    MemberAuthor

    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's correct. The cache entry in the Directory object 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.

  6. goldwaving commented on Mar 31, 2022

    @goldwaving
    Contributor

    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.

  7. tlively commented on Mar 31, 2022

    @tlively
    MemberAuthor

    @goldwaving, can you explain that in more detail?

  8. goldwaving commented on Mar 31, 2022

    @goldwaving
    Contributor

    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.

  9. tlively commented on Apr 1, 2022

    @tlively
    MemberAuthor

    Ah yes, it does sound like this change would reduce the amount of code in your backend, then.

  10. kripken commented on Apr 1, 2022

    @kripken
    Member

    That's correct. The cache entry in the Directory object would contain a pointer to the IDBFS mount root, but that would be private and not visible at all to the OPFS backend.

    How would that work with a custom Directory class of a custom backend?

  11. tlively commented on Apr 1, 2022

    @tlively
    MemberAuthor

    That's correct. The cache entry in the Directory object would contain a pointer to the IDBFS mount root, but that would be private and not visible at all to the OPFS backend.

    How would that work with a custom Directory class of a custom backend?

    This would all be handled in the base Directory class. Backends override that class, but they would not be privy to these details.

  12. kripken commented on Apr 1, 2022

    @kripken
    Member

    @tlively I see, sgtm!

  13. kripken commented on Apr 1, 2022

    @kripken
    Member

    (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.)

  14. added a commit that references this issue on Apr 16, 2022
    fdca5c7
  15. added a commit that references this issue on Apr 20, 2022
    6e1d3bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions