Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subsys: fs: Support file open flags to fs and POSIX API #26305

Merged
merged 11 commits into from Jul 30, 2020

Conversation

de-nordic
Copy link
Collaborator

@de-nordic de-nordic commented Jun 19, 2020

{DNM] Due to required update to mcumgr, that is reviewed here: apache/mynewt-mcumgr#90
and requires update of https://github.com/zephyrproject-rtos/mcumgr

Anyone that is able to review apache/mynewt-mcumgr#90 please do so!

This series of commits adds support for file open flags (CREATE, APPEND, etc.) and fs_open_ex function to support them.
POSIX API was updated accordingly.

  • FAT FS tests
  • LIttleFS test
  • Posix tests

RFC Issue: #26833
Resolves: #16638

Requires changes to mcumgr: apache/mynewt-mcumgr#90
Requires changes to mcumgr, for the regression to pass, brought by snapshot update: zephyrproject-rtos/mcumgr#26

@zephyrbot zephyrbot added area: C Library C Standard Library area: POSIX POSIX API Library area: File System area: API Changes to public APIs area: Tests Issues related to a particular existing or missing test labels Jun 19, 2020
@jimparis
Copy link
Collaborator

POSIX is an API specification, so "adding to posix API" does not make sense, because then it's not posix anymore. I think we should instead fix open() (by not unconditionally adding O_CREAT) and not introduce open2() at all.

Copy link
Collaborator

@jimparis jimparis 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 we should fix open() (by not unconditionally adding O_CREAT) and not introduce open2() at all.

lib/posix/fs.c Outdated Show resolved Hide resolved
Comment on lines 576 to 570
/* NOTE: The Zephyr fs_open_ex flags allow opening existing files
* with READ/WRITE access, as it happens in fs_open_ex above, but
* the LittleFS assumes READ/WRITE access by default if no access
* is selected which same as requesting READ ONLY and WRITE ONLY
* access at the same time (bug?).
* From Zephyr API point of view below tests should fail but they
* will succeed, which is expected (?) behaviour of LittleFS;
* that is why this test is left here commented out.
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I'm understanding you right, you are saying that littlefs requires flags to be either LFS_O_RDONLY, LFS_O_WRONLY, or LFS_O_RDWR, and passing a flag value of 0 is treated the same as LFS_O_RDWR. If that's true, shouldn't it be the job of lfs_flags_from_zephyr to never pass a flag value of 0 to lfs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

lfs_flags_from_zephyr is supposed to translate flags to obtain, from fs backend, specific behavior. I guess that the READONLY would be closest to expected.

@de-nordic
Copy link
Collaborator Author

POSIX is an API specification, so "adding to posix API" does not make sense, because then it's not posix anymore. I think we should instead fix open() (by not unconditionally adding O_CREAT) and not introduce open2() at all.

I am not sure I can do that immediately because someone may be using current implementation the way it is. If it just gets changed then the code will stop working. Maybe there should be some Kconfig added that would enable compatibility with the original behavior for some time.

@zephyrbot zephyrbot added the area: native port Host native arch port (native_posix) label Jun 22, 2020
@de-nordic de-nordic force-pushed the fs-api-add-open-ex branch 3 times, most recently from daea928 to 51317e4 Compare June 25, 2020 07:54
@de-nordic de-nordic requested a review from jimparis June 25, 2020 07:55
@de-nordic de-nordic requested a review from PerMac June 25, 2020 07:59
lib/posix/Kconfig Outdated Show resolved Hide resolved
Copy link
Collaborator

@jimparis jimparis left a comment

Choose a reason for hiding this comment

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

I dislike FILE_SYSTEM_LEGACY_OPEN. I suggest using some macro tricks like the ones described at https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros, which will let users continue to pass either 2 or 3 arguments to fs_open, so that both existing code and new code can coexist and work correctly. For example, this test appears to do the trick:

#include <stdio.h>

#define FO_READ   0x01
#define FO_WRITE  0x02
#define FO_RDWR   (FO_READ | FO_WRITE)
#define FO_CREATE 0x10

void real_fs_open(void *p, const char *path, int flags) {
        printf("open(p=%p, path=\"%s\", flags=0x%02x)\n", p, path, flags);
}

void legacy_fs_open(void *p, const char *path) {
        return real_fs_open(p, path, FO_CREATE | FO_RDWR);
}

/* With 0-2 arguments, fs_open(...) resolves to legacy_fs_open(...)
   With 3   arguments, fs_open(...) resolves to real_fs_open(...)
   More arguments are not handled.
*/
#define GET_ARG4(arg1, arg2, arg3, arg4, ...) arg4
#define OPEN_CHOOSE(...) GET_ARG4(                                      \
                __VA_ARGS__,  real_fs_open,                             \
                legacy_fs_open, legacy_fs_open, legacy_fs_open)
#define fs_open(...) OPEN_CHOOSE(__VA_ARGS__)(__VA_ARGS__)

/* Use example: */
main()
{
        char p;

        fs_open(&p, "/no-flags");
        fs_open(&p, "/with-flags", FO_READ);

        return 0;
}

Output:

$ gcc -Wall -pedantic -o test test.c
$ ./test
open(p=0x7fff85ee440f, path="/no-flags", flags=0x13)
open(p=0x7fff85ee440f, path="/with-flags", flags=0x01)

As for the POSIX side, I would just fix it and not support the old broken behavior at all. If it's still made configurable, I would strongly suggest making it default to the correct open() API; otherwise it doesn't make sense to call it POSIX at all, and I am certain that anyone attempting to port POSIX code to Zephyr would welcome the fix.

@de-nordic
Copy link
Collaborator Author

I dislike FILE_SYSTEM_LEGACY_OPEN. I suggest using some macro tricks (...)

OK I can do that. I just hoped to deprecate the old function signature with time and leave the 3 parameter thing only.

As for the POSIX side, I would just fix it and not support the old broken behavior at all (...)

I do not know how many users have been using this in the form it is. The "legacy" behavior is supposed to be deprecated after some time. The programmers should be given time to adjust code. If we will keep on breaking their applications without giving time to adjust to changes, they will be reluctant to update Zephyr within their projects.

@de-nordic de-nordic force-pushed the fs-api-add-open-ex branch 2 times, most recently from 9d7ec7b to 3650ef7 Compare June 26, 2020 13:18
@jimparis
Copy link
Collaborator

I dislike FILE_SYSTEM_LEGACY_OPEN. I suggest using some macro tricks (...)

OK I can do that. I just hoped to deprecate the old function signature with time and leave the 3 parameter thing only.

That sounds good. You could use the macro and mark the old signature with __attribute__((deprecated)) so GCC will warn if it gets used.

As for the POSIX side, I would just fix it and not support the old broken behavior at all (...)

I do not know how many users have been using this in the form it is. The "legacy" behavior is supposed to be deprecated after some time. The programmers should be given time to adjust code. If we will keep on breaking their applications without giving time to adjust to changes, they will be reluctant to update Zephyr within their projects.

I don't think it's a big deal either way, but I would still learn towards fixing it because:

  • the POSIX API is clear
  • the open() function signature isn't changing; i.e. users have always had to pass flags to it
  • Previously, the flags were just ignored
  • Now, the flags are interpreted correctly

So basically the only case where someone would get bitten by the change is if they were previously passing flags that didn't match the behavior they actually wanted -- ie. if they were passing "O_RDONLY" and then expecting to be able to write the file.

I would suggest naming the configuration option POSIX_FS_LEGACY_OPEN_IGNORE_FLAGS with a description like:

With this option set, the POSIX open() call will ignore the user-supplied flags, and act as if O_CREAT|O_RDWR were passed. For backwards compatibility with Zephyr < A.B.C. This option will default to n starting in Zephyr X.Y.Z.

Unfortunately, because open() already took flags, it's harder to help the user out with compile-time warnings about the upcoming deprecation. Maybe use __builtin_constant_p in a macro to issue a warning if the passed flags are something other than O_CREAT|O_RDWR, with the legacy option? It wouldn't warn for non-constant flags, but should still cover some use cases.

Copy link
Collaborator

@pabigot pabigot left a comment

Choose a reason for hiding this comment

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

One style change, one spelling fix.

include/fs/fs.h Outdated Show resolved Hide resolved
lib/posix/fs.c Outdated Show resolved Hide resolved
subsys/fs/littlefs_fs.c Outdated Show resolved Hide resolved
@de-nordic
Copy link
Collaborator Author

@pabigot Thanks!

@de-nordic de-nordic dismissed jimparis’s stale review July 27, 2020 14:27

This is no longer relevant because I has been decided to drop compatibility with two parameter ffs_open.

west.yml Outdated
@@ -92,7 +92,7 @@ manifest:
revision: d52aff5065ab5995f785877a834112461ff93526
path: bootloader/mcuboot
- name: mcumgr
revision: 898a5a7f5224be8345e58eca3b9a84389ed61d15
revision: pull/26/head
Copy link
Member

Choose a reason for hiding this comment

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

Use cfe5eb98a9493017448846fd1a44a9340bd0a22f

The fs_open has been extended with support for open flags.
Currently supported flags are:
  FS_O_READ -- open for read
  FS_O_WRITE -- open for write
  FS_O_CREATE -- create file if it does not exist
  FS_O_APPEND -- move to the end of file before each write

The FAT FS and LittleFS front-ends within the Zephyr has also been
modified to utilize the flags.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
Now fs_open supports open flags and the lvgl_fs code requires update.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
Now fs_open supports open flags and the fuse requires update.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
Common procedure, to use with fs back-ends, have been added for testing
various fs_open flags combinations.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
The addition of support for open flags, within fs_open, requires
addition of new test cases to FAT FS back-end tests.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
The addition of support for open flags, within fs_open, requires
addition of new test cases.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
The commit changes signature of open function from:
  int open(const char *name, int flags)
to
  int open(const char *name, int flags, ...)

Currently existing two argument invocations should not require any
rework.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
The fs_open flags has been changed to accept open flags, which requires
changes to open(...) to support the new flags.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
Add new tests that will utilize open mode flags with the open
function.

A few test functions which assumed O_CREAT behavior are updated
to specify it explicitly.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
Update release notes API change section with information on change to
fs_open parameter list.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit updates mcumgr with latest snapshot from the upstream.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
@de-nordic de-nordic changed the title [DNM] subsys: fs: Support file open flags to fs and POSIX API subsys: fs: Support file open flags to fs and POSIX API Jul 30, 2020
@de-nordic de-nordic removed the DNM This PR should not be merged (Do Not Merge) label Jul 30, 2020
@carlescufi carlescufi merged commit c123efb into zephyrproject-rtos:master Jul 30, 2020
Architecture Project automation moved this from In Progress to Done Jul 30, 2020
@de-nordic de-nordic deleted the fs-api-add-open-ex branch September 14, 2020 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: C Library C Standard Library area: Documentation area: File System area: native port Host native arch port (native_posix) area: POSIX POSIX API Library area: Samples Samples area: Settings Settings subsystem area: Tests Issues related to a particular existing or missing test Breaking API Change Breaking changes to stable, public APIs
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Filesystem API is missing fs_open() flags
9 participants