littlefs2 icon indicating copy to clipboard operation
littlefs2 copied to clipboard

Add Filesystem::try_mount

Open robin-nitrokey opened this issue 10 months ago • 3 comments

When mounting a filesystem, often code like this is used:

if !Filesystem::is_mountable(alloc, storage) {
    Filesystem::format(storage).ok();
}
Filesystem::mount(alloc, storage)

This mounts the filesystem twice because Filesystem::is_mountable is equivalent to Filesystem::mount(...).is_ok(). Depending on the storage implementation, mounting the filesystem can have significant cost. But directly calling Filesystem::mount and re-mounting in the error case is prohibited by the borrow checker.

This patch adds a try_mount method that accepts a callable that is called on mount error. Afterwards, mounting is re-tried.

robin-nitrokey avatar Apr 24 '24 17:04 robin-nitrokey

I’m not entirely happy with try_mount either, but I did not see a better alternative. mount_with_prepare sounds like it would always call the prepare function. Maybe mount_with_recover?

robin-nitrokey avatar Apr 25 '24 08:04 robin-nitrokey

Is this going to be used with anything other than format?

I would have initially just gone with mount_or_format.

sosthene-nitrokey avatar Apr 25 '24 08:04 sosthene-nitrokey

Even if it’s just called with format, I would like to be able to know whether the initial mount worked. This could also encoded in the return type, though it would be more complicated.

A more interesting case would be journaling. Here we first want to try to mount the filesystem, and if it fails, recover from the journal. I’m not sure if we will end up using this method for IFS recovery in the nitrokey-3-firmware, but I think it would make sense to support the use case.

robin-nitrokey avatar Apr 25 '24 08:04 robin-nitrokey

@sosthene-nitrokey How about mount_or_else?

robin-nitrokey avatar May 22 '24 14:05 robin-nitrokey

Looks good!

sosthene-nitrokey avatar May 22 '24 14:05 sosthene-nitrokey