Skip to content

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented Jul 2, 2020

Contribution description

This hooks up vfs_format to f_mkfs().
fatfs also provides a f_fdisk() function to create a partition table, but this doesn't seem to be necessary.

Since pulling in the format logic adds 3.5k .text and will not be needed on many applications, it is added as a fatfs_vfs_format pseudo-module.

Testing procedure

tests/pkg_fatfs_vfs will now format the storage before testing, so beware 😉
This is consistent with the other file-system tests though.

Issues/PRs references

@benpicco benpicco added Type: new feature The issue requests / The PR implemements a new feature for RIOT Area: pkg Area: External package ports Area: fs Area: File systems labels Jul 2, 2020
@benpicco benpicco requested a review from chrysn July 2, 2020 17:23
@benpicco benpicco requested a review from vincent-d July 2, 2020 17:23
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jul 8, 2020
@benpicco benpicco added the Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer label Sep 8, 2020
@benpicco benpicco added State: waiting for CI update State: The PR requires an Update to CI to be performed first and removed State: waiting for CI update State: The PR requires an Update to CI to be performed first labels Sep 10, 2020
@benpicco benpicco requested a review from maribu September 25, 2020 13:26
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, two remarks inline

@benpicco benpicco removed the Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer label Oct 7, 2020
@benpicco benpicco requested a review from kaspar030 October 24, 2020 19:48
Copy link
Member

@bergzand bergzand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with an sdcard on the nucleo-f401re. Works as advertised!

@bergzand bergzand added the Reviewed: 3-testing The PR was tested according to the maintainer guidelines label Nov 17, 2020
@bergzand bergzand self-assigned this Nov 17, 2020
fatfs_desc_t *fs_desc = mountp->private_data;
char volume_str[TEST_FATFS_MAX_VOL_STR_LEN];

BYTE *work = malloc(FF_MAX_SS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need malloc here?

Copy link
Contributor Author

@benpicco benpicco Dec 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FF_MAX_SS is 512 byte so I'd rather not put it on the stack

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general (or maybe my strict?) expectation of RIOT functions without any admonitions is that they don't use the heap, are reentrant / thread safe, and don't overexert the stack. This can't hold that, so we should put a warning around the general vfs_format function. So what'd that best be?

  • "Not thread safe! Do not call this concurrently on different (let alone the same) file system.": Similar to the warning already on vfs_iterate_mounts; would give license to use a static buffer.

    Advantages: Consistent with other vfs functions; memory limitations are visible (with nm showing the culprit) at build time if no heap allocation is used.

  • "On some file systems (currently \ref FatFS), this may resort to dynamic memory allocation, and return -ENOMEM if insufficient memory is available." This would justify the current behavior.

    Advantage: Works well on otherwise idle systems even with little memory.

  • "Beware that some file systems (currently \ref FatFS) have extensive stack requirements when running this". Very vague, and has bad error behavior.

Based on this, I'm softly leaning toward a const allocation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making it configurable? Allowing a static buffer to be provided, otherwise allocating one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added CONFIG_FATFS_FORMAT_ALLOC_STATIC

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good enough compromise @bergzand? (I would personally prefer that the default be avoiding malloc though)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After letting this brew for a year I think I'm fine with anything as long as it is documented so the user doesn't run into unexpected troubles. Having it configurable seems like a perfect solution to me

Copy link
Member

@chrysn chrysn Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now that the warning I sketched above is superfluous as mountpoint-related vfs stuff is behind a mutex anyway (but please verify, I'm on mobile and can't easily read long code here)

[edit because for some reason I can't reply to this particular comment:]

The warning should still be there, for vfs_format only blocks the mount mutex briefly, and releases it for the formatting operation.

Not thread safe! Do not call this concurrently on different (let alone the same) file system. (For example, FatFS uses static buffers during formatting in some configurations.)

@MrKevinWeiss MrKevinWeiss added this to the Release 2021.07 milestone Jun 21, 2021
@MrKevinWeiss MrKevinWeiss removed this from the Release 2021.07 milestone Jul 15, 2021
@stale stale bot removed the State: stale State: The issue / PR has no activity for >185 days label Jan 27, 2022
@benpicco benpicco removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 27, 2022
@github-actions github-actions bot added Area: build system Area: Build system Area: sys Area: System Area: tests Area: tests and testing framework labels Jan 27, 2022
@benpicco
Copy link
Contributor Author

benpicco commented Feb 9, 2022

So what's the verdict on this one?

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 9, 2022
@benpicco benpicco requested review from bergzand and chrysn and removed request for chrysn February 14, 2022 14:07
@chrysn
Copy link
Member

chrysn commented Feb 14, 2022

Can't do the full review right now, but seeing #17653 I noticed that (especially in combination) much of the unsightliness of the native/fatfs README can go away (because now fatfs can be formatted just like any other FS) -- to the point where after the two, the section could be gone completely (because all that's left is switching around the FS, and that should be obvious)

@@ -1,5 +1,9 @@
USEMODULE += fatfs_diskio_mtd
USEMODULE += mtd

ifneq (,$(filter vfs_auto_format,$(USEMODULE)))
USEMODULE += fatfs_vfs_format
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might suffice as d default module (maybe the FAT isn't automounted)

fatfs_desc_t *fs_desc = mountp->private_data;
char volume_str[TEST_FATFS_MAX_VOL_STR_LEN];

BYTE *work = malloc(FF_MAX_SS);
Copy link
Member

@chrysn chrysn Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now that the warning I sketched above is superfluous as mountpoint-related vfs stuff is behind a mutex anyway (but please verify, I'm on mobile and can't easily read long code here)

[edit because for some reason I can't reply to this particular comment:]

The warning should still be there, for vfs_format only blocks the mount mutex briefly, and releases it for the formatting operation.

Not thread safe! Do not call this concurrently on different (let alone the same) file system. (For example, FatFS uses static buffers during formatting in some configurations.)

@chrysn
Copy link
Member

chrysn commented Feb 16, 2022

Thanks. Please squash and rebase onto current master, which will enable the changes that I think are suitable in examples/filesystem/REAMDE mergable (probably best fit in a separate commit, but if you prefer squash them right in):

  • file system list in lines 10/11 could now include FatFS because it can be formatted
  • Everything down of line 80 can IMO go away now, because FatFS needs no more special treatment than the other FSs. (The tar unpacking was just added there because formatting was not available.

@benpicco benpicco requested a review from jia200x as a code owner February 16, 2022 13:36
@github-actions github-actions bot added Area: doc Area: Documentation Area: examples Area: Example Applications labels Feb 16, 2022
@benpicco
Copy link
Contributor Author

main(): This is RIOT! (Version: 2022.04-devel-398-g70d98-pkg/fatfs_format)
constfs mounted successfully
> mount
mount
Error while mounting /sda...try format
> format
format
/sda successfully formatted
> mount
mount
/sda successfully mounted
> ls /sda
ls /sda
total 0 files
> vfs df
vfs df
Mountpoint              Total         Used    Available     Capacity
/sda                     2048          102         1946       4%
/const                     27           27            0     100%

Copy link
Member

@chrysn chrysn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready to be merged. Thank you!

@benpicco benpicco enabled auto-merge February 16, 2022 14:07
@benpicco
Copy link
Contributor Author

Thank you for pushing this forward!

@benpicco benpicco merged commit 22a3fc7 into RIOT-OS:master Feb 16, 2022
@benpicco benpicco deleted the pkg/fatfs_format branch February 16, 2022 16:28
@OlegHahm OlegHahm added this to the Release 2022.04 milestone Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: doc Area: Documentation Area: examples Area: Example Applications Area: fs Area: File systems Area: pkg Area: External package ports Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 3-testing The PR was tested according to the maintainer guidelines Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants