Commit Graph

212 Commits

Author SHA1 Message Date
Dominik Ermel d0017e9e5e fs: shell: Remove unneeded flags from fs_open calls
The cmd_write and cmd_ctunc have been opening/creating file for
read/write operation.
The commit changes cmd_write to open/create file for write only,
and cmd_trunc to only open file for write.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-03-04 06:10:58 -05:00
Dominik Ermel bbacab82f1 fs: shell: cmd_read does should open file for read-only
The cmd_read function uses fs_open with FS_O_CREATE | FS_O_RDWR flags
to open file it will only read; the flags has been changed
to FS_O_READ.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-03-01 12:57:55 -06:00
Peter Bigot 9519140270 fs: littlefs: fix mis-use of preprocessor
llvm 11 doesn't like having preprocessor directives in the expansion
of a macro used as a preprocessor conditional expression.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-02-08 11:36:24 -05:00
Andrzej Puzdrowski 6fb7ac233c fs: Fix fs_opendir resource leak when invoked on fs_dir_t object in use
Fixes problem when fs_opendir invoked on fs_dir_t object,
which is already holding information on opened directory,
overwrites references to other memory objects within
the fs_dir_t object causing resource leak.
If fs_opendir is invoked on already used fs_dir_t object,
it will return
-EBUSY.

Note: The change requires that all fs_dir_t objects should be
initialized to 0.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2021-02-05 07:32:20 -05:00
Andrzej Puzdrowski 7a4940d270 fs: Add fs_dir_t type variable initializations
The commit adds initializations of fs_dir_t variables in preparation
for fs_opendir function change that will require fs_dir_t object, passed
to the function, to be initialized before first usage.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2021-02-05 07:32:20 -05:00
Peter Bigot 487b0002cb tests: fs: littlefs: update filecache configuration
The mem_pool Kconfig API is deprecated as allocation now uses a k_heap.
Update to allocate a heap with the same amount of memory as was
defaulted with mem_pool customization.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-02-02 07:15:17 -05:00
Peter Bigot 8ea2956ca3 fs: littlefs: revise how per-file cache memory is allocated
Originally the file cache used a mem_pool, but that data structure has
been deprecated and replaced by a heap that includes metadata in the
heap area.  As a result attempts to allocate all blocks will fail
because some of the reservation intended for cache data is now holding
metadata instead.

It's not immediately clear how to adjust the required heap size to
support this metadata as it depends on heap chunk units and data
structures that are not visible to the application.  Experimentally a
value of 24 bytes works, while smaller values do not.

Further the previous Kconfig API to configure the allocation pool is
completely inappropriate with the new heap data structure which has
such different behavior.

So: Deprecate the old Kconfig API.  Add a new Kconfig option to
directly control the cache size.  Infer a default cache size that
works with the old mem_pool parameters assuming a per-block overhead.
But to avoid wasted memory use the heap allocation only when the
application customizes the size, and use a slab in other cases.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-02-02 07:15:17 -05:00
Dominik Ermel d4666f537c fs: Fix fs_open resource leak when invoked on fs_file_t object in use
Fixes problem when fs_open invoked on fs_file_t object, which is already
holding information on opened file, overwrites references to other
memory objects within the fs_file_t object causing resource leak.
If fs_open is invoked on already used fs_file_t object, it will return
-EBUSY.

Note: The change requires that all fs_file_t objects should be
initialized to 0.

Fixes: #29478

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-01-29 08:04:51 -05:00
Dominik Ermel fd610846ef native_posix: fuse: Add fs_file_t type variable initializations
The commit adds initializations of fs_file_t variables in preparation
for fs_open function change that will require fs_file_t object, passed
to the function, to be initialized before first usage.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-01-29 08:04:51 -05:00
Dominik Ermel 512a6f8cb6 fs: shell: Add fs_file_t type variable initializations
The commit adds initializations of fs_file_t variables in preparation
for fs_open function change that will require fs_file_t object, passed
to the function, to be initialized before first usage.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-01-29 08:04:51 -05:00
Mattias Säteri cf4295d106 native_posix: Add flags to fs_open invocation
Added flags that were missing from fs_open() call.

Signed-off-by: Mattias Säteri <mattias.sateri@flir.com>
2021-01-22 10:37:57 -05:00
Fredrik Gihl 49031cf6e2 fs: littlefs: Fix transient string in LOG_ messages
Fix transient string errors found when enable
CONFIG_LOG_DETECT_MISSED_STRDUP.

Signed-off-by: Fredrik Gihl <fredrik.gihl@flir.se>
2021-01-18 18:47:41 +01:00
Markus Fuchs ea6f7a4292 disk: disk_access_flash: Make sector size configurable in Kconfig
Make sector size used by flash disk configurable and expose new disk and
fatfs configurations to Kconfig.

Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
2021-01-15 11:21:01 -05:00
Peter Bigot 5b544e115b fs: littlefs: define mount point structures for file systems
Use the devicetree filesystem bindings to populate an fs_mount_t
object that is preconfigured for a particular set of file system
properties on a specified partition.

At this time the mount point data is accessed by reference using the
partition's devicetree node identifier.

Note: While a file system can register itself before its devices
are available, it cannot do the automount.  In this commit the
initialization priority is increased to compensate, but that's not
a long-term solution.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-12-27 18:16:20 +01:00
Andy Ross fcd392f6ce kernel: subsys: lib: drivers: Use k_heap instead of z_mem_pool wrappers
Use the core k_heap API pervasively within our tree instead of the
z_mem_pool wrapper that provided compatibility with the older mempool
implementation.

Almost all of this is straightforward swapping of one alloc/free call
for another.  In a few cases where code was holding onto an old-style
"mem_block" a local compatibility struct with a single field has been
swapped in to keep the invasiveness of the changes down.

Note that not all the relevant changes in this patch have in-tree test
coverage, though I validated that it all builds.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Andy Ross 6965cf526d kernel: Deprecate k_mem_pool APIs
Mark all k_mem_pool APIs deprecated for future code.  Remaining
internal usage now uses equivalent "z_mem_pool" symbols instead.

Fixes #24358

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-12-07 21:50:14 -05:00
Dominik Ermel 4832fe43e0 fs/fcb: Enable FCB to work with non-0xff erase value flash
The FCB has been strongly tied to 0xff erased flash devices and this
commit adds support for other erase values.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-16 13:39:04 +01:00
Dominik Ermel e8fa66f951 tests/fs/fatfs: Add tests for FS_MOUNT_FLAG_*
Adds various tests for mounting ELM FAT FS with FS_MOUNT_FLAG_NO_FORMAT
and FS_MOUNT_FLAG_READ_ONLY, and operations on read-only mounted
file system.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-06 13:10:13 -05:00
Dominik Ermel 5c2a004c0b tests/fs/littlefs: Add tests for FS_MOUNT_FLAG_*
Adds various tests for mounting LittleFS with FS_MOUNT_FLAG_NO_FORMAT
and FS_MOUNT_FLAG_READ_ONLY, and operations on read-only mounted
file system.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-06 13:10:13 -05:00
Dominik Ermel aa0fd027fc fs: Add support for mount flags
The flags field has been added to fs_mount_t structure, accompanied
with two new flags:
  FS_MOUNT_FLAG_READ_ONLY -- mount fs as read only
  FS_MOUNT_FLAG_NO_FORMAT -- do not format volume when system not found

Code supporting the flags has been added to FS layer and drivers for
LittleFS and FAT FS.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-06 13:10:13 -05:00
Dominik Ermel e43ec70d4f subsys/fs: Make FAT FS write support optional
The commit adds FS_FATFS_READ_ONLY Kconfig option; the option, when
selected, excludes write supporting code within ELM FAT driver.

When write support to FAT FS volumes is not desired, this option may be
selected to slightly reduce code size.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-06 13:10:13 -05:00
Dominik Ermel c31d659f70 subsys/fs: Make FAT FS formatting code optional
The commit adds FS_FAFTFS_MKFS Kconfig option; the option, y by default,
allows to include mkfs supporting code, within fs_mount callback,
for ELM FAT file system driver.

When formatting of FAT FS volumes is not desired, this option may be
deselected to slightly reduce code size.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-06 13:10:13 -05:00
Dominik Ermel 045900eedf fs: Make fs API callback runt-time check optional
The commit changes run-time checks of file system driver's callbacks
pointers, against null, optional by replacing `if` with `CHECKIF` macro,
which means that they can be removed from compilation with Kconfig
option CONFIG_NO_RUNTIME_CHECKS.
Additionally the commit allows the same checks to fail hard, with
assertion, when CONFIG_ASSERT_ON_ERRORS option is selected.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-11-03 11:08:25 +01:00
Dominik Ermel e7886c6634 fs: fs_unmount will return -ENOTSUP if not implemented by driver
The commit changes error handling by fs_unmount; the function will
return -EINVAL if mount point, described by mp, is not mounted or
-ENNOTSUP when unmounting is not supported by the driver; in the second
case it will also log error.

Additionally to the above changes, checks for correct mnt_path and
mnt_path, within fs_unmount, have been removed as they are not needed;
only the fs_mount_t->fs pointer is needed to decide whether system is
mounted or not.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-30 15:54:04 +01:00
Dominik Ermel 5faf3750bc fs: fs_mount parameter verification before mutex locked block
The commit moves fs_mount parameter verification above mutex lock.
The list of mount points is now checked before attempting to obtain
file system API pointer.
All modifications to mount point data structure, given as a parameter
to the fs_mount, are only applied after every other operation needed
have completed successfully, immediately before adding the mount point
to the list of mount points.
The fs_mount will a warning when mounted file system does not support
unmount.
When a file system does not provide mount function, the -ENOTSUP error
will be returned instead of -EINVAL.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-30 15:54:04 +01:00
Dominik Ermel 1241e67790 fs: Move fs_file_system_t declaration out of fs.h
The struct fs_file_system_t is only useful when defining file system
drivers and is not required for typical application development,
that is why it has been moved to separate file fs_sys.h.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-23 12:55:41 +02:00
Dominik Ermel e7a21e83f2 fs: Move FAT FS Kconfig options to Kconfig.fatfs
All ELM FAT related Kconfig options have been moved to Kconfig.fatfs.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-14 13:19:47 -05:00
Dominik Ermel bbd38ed6ff fs: Move all LittleFS options to Kconfig.littlefs
CONFIG_FILE_SYSTEM_LITTLEFS has been moved to Kconfig.littlefs.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-14 13:19:47 -05:00
Dominik Ermel 81dc347b84 fs: Improve documentation for FS API
Simple improvements in text and formatting of the documentation
the the FS API.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-13 13:16:02 +02:00
Dominik Ermel d795f2dbfe fs: fs_seek and fs_tell return -ENOTSUP when not implemented
Simple change that makes fs_seek and fs_tell return -ENOTSUP when
file system does not implement the seek/tell.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-10-07 14:51:39 +02:00
Ievgenii Meshcheriakov cf523e449e drivers: flash: Pass bool to flash_write_protection_set()
The second argument of this foonction is a bool, so passing 0 and 1
is incorrect.

Coccinelle script:

    @@
    expression e;
    @@
    flash_write_protection_set(e,
    (
    - 0
    + false
    |
    - 1
    + true
    )
     )

Signed-off-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@nordicsemi.no>
2020-10-02 12:06:28 -04:00
Peter A. Bigot ad782596ea fs: allow setting max file name explicitly
Zephyr allows an application to register additional file systems,
which may have maximum filename lengths different from the in-tree
FatFs and littlefs options.  Provide a Kconfig variable that allows
the default inference to be overridden.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2020-09-03 21:49:34 +02:00
Peter A. Bigot d0d3c4a3a7 fs: allow external file system implementations
The documentation claims that Zephyr supports external file system
implementations, and there's no reason not to do so.  Rework the API
to allow this.

Note that the file system type cannot legally be an enum anymore,
since we need to support file system types that don't have an
identifier assigned in that enum.  Rely on the implicit conversion of
enum values to int to preserve backwards compatibility.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2020-09-03 21:49:34 +02:00
Peter A. Bigot 353336d632 fs: make file system description const
There's no reason the table of pointers to file system functions needs
to be mutable at runtime.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2020-09-03 21:49:34 +02:00
Tomasz Bursztyka e18fcbba5a device: Const-ify all device driver instance pointers
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.

A coccinelle rule is used for this:

@r_const_dev_1
  disable optional_qualifier
@
@@
-struct device *
+const struct device *

@r_const_dev_2
 disable optional_qualifier
@
@@
-struct device * const
+const struct device *

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00
Jui-Chou Chung fa29e1a1f8 subsys/fs/fatfs: Add kconfig option for enabling exFAT support
Expose the exFAT support option to Kconfig, so developer can
enable/disable the exFAT support on FatFs through Kconfig, no need to
touch ffconf.h.

Signed-off-by: Jui-Chou Chung <jui-chou.chung@nordicsemi.no>
2020-08-13 14:15:40 +02:00
Dominik Ermel b74ed8fecb kconfig: Move FAT FS options closer to enable
When FAT FS option is enabled, then the FAT FS sub-menu will appear
next to it, instead of appearing far down the list.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-08-11 07:47:08 -04:00
Dominik Ermel babc322e09 native_posix: Add flags argument to fs_open invocations
Now fs_open supports open flags and the fuse requires update.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-30 16:33:18 +02:00
Dominik Ermel cac07629bf subsys/fs: Add open flags parameter to fs_open
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>
2020-07-30 16:33:18 +02:00
Dominik Ermel a5b3e86339 subsys/fs: Fix fs_* operations crashing when file closed
Attempt to perform fs_read, fs_write, fs_seek, fs_tell, fs_truncate
and fs_sync on file that has been closed, prior to attempt, would cause
NULL pointer dereference.
With this fix, such operations would instead return -EBADF.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-03 12:16:11 -04:00
Dominik Ermel 54492c2748 subsys/fs: Fix fs_close on closed file causing NULL dereference
If fs_close gets invoked on closed file (e.g. double close) it would
cause NULL dereference and system crash. Instead now it will just
return with success, as the file is already closed anyway.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-07-03 12:16:11 -04:00
Tobias Svehagen 50ed105d05 fs: nvs: Fix handling of corrupt ate's in garbage collector
nvs_gc does not verify the crc8 of close_ate before using
close_ate.offset.  This means that close_ate.offset could contain an
offset that points beyond valid ate's in the sector. For example, there
might be a valid ate at offset 0x100 but close_ate.offset is 0x200.
If that is the case that value will not be moved and so it will be lost.

Solve this by refactoring the recovery loop from nvs_prev_ate into
nvs_recover_last_ate and use that function in nvs_gc if a corrupt
close_ate is found.

The crc8 of gc_ate is not checked before trying to find another ate
with the same id. If there are no valid ate with that id in the whole
fs the inner while(1)-loop will never stop since the break condition
includes a check for a correct crc8.

Solve this by skipping gc_ate's with an invalid crc8.

Fixes #26407

Signed-off-by: Tobias Svehagen <tobias.svehagen@gmail.com>
2020-06-26 12:07:13 -05:00
Dominik Ermel 4c122dd42a subsys/fs/fatfs: Fix missing memory free on error
fatfs_open and fatfs_opendir fail to free reserved memory on error
which may lead to memory exhaustion, when too many file/dir open errors
are caused, and will cause all subsequent open attempts to fail with
ENOMEM error.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-06-25 12:13:51 +02:00
Dominik Ermel 38f623d97c subsys/fs/nvs: Move write_block_size to flash_parameters
Pointer to flash_parameters have been added to nvs_fs structure and it
is no longer needed to store write_block_size within the nvs_fs.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-06-22 14:35:03 +02:00
Dominik Ermel 076282a117 subsys/fs/nvs: Use flash_get_parameters() to get erase value
Use new flash API call to obtain erase value instead of relaying on
hardcoded literals.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-06-22 14:35:03 +02:00
Markus Becker 2291de6abf subsys/fs/littlefs: initialize LittleFS at POST_KERNEL
Initialize LittleFS at POST_KERNEL and not at APPLICATION stage.

When using LittleFS as settings backend for OpenThread, LittleFS needs
to be available when net_init is performed.

Arguably, FS is an OS kernel component that should be ready when
applications are starting.

Possibly, the same change should be applied to FAT fs as well.

Signed-off-by: Markus Becker <markus.becker@tridonic.com>
2020-06-09 13:48:34 +02:00
Michael Hope ff50ca1974 fs: nvs: fail on >= 256 B block sizes
The current code truncates the size_t block size to a u8_t, which
causes a 256 or 512 byte block size to be truncated to zero and
passing the size check.

Check as a size_t then truncate once the check passes.

Signed-off-by: Michael Hope <mlhx@google.com>
2020-06-08 23:03:21 -04:00
Kumar Gala a1b77fd589 zephyr: replace zephyr integer types with C99 types
git grep -l 'u\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/u\(8\|16\|32\|64\)_t/uint\1_t/g"
	git grep -l 's\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/s\(8\|16\|32\|64\)_t/int\1_t/g"

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-06-08 08:23:57 -05:00
Kumar Gala ff579a3af3 flash: Convert DT_FLASH_AREA to FLASH_AREA macros
Convert with a combo of scripts and by hand fixups:

git grep -l DT_FLASH_AREA_.*_ID | \
 xargs sed -i -r 's/DT_FLASH_AREA_(.*)_ID/FLASH_AREA_ID(\L\1)/'

git grep -l DT_FLASH_AREA_.*_OFFSET | \
 xargs sed -i -r 's/DT_FLASH_AREA_(.*)_OFFSET/FLASH_AREA_OFFSET(\L\1)/'

git grep -l DT_FLASH_AREA_.*_SIZE | \
 xargs sed -i -r 's/DT_FLASH_AREA_(.*)_SIZE/FLASH_AREA_SIZE(\L\1)/'

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-05-13 21:22:53 +02:00
Tomasz Bursztyka 97326c0445 device: Fix structure attributes access
Since struct devconfig was merged earlier into struct device, let's fix
accessing config_info, name, ... attributes everywhere via:

grep -rlZ 'dev->config->' | xargs -0 sed -i 's/dev->config->/dev->/g'

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-05-08 23:07:44 +02:00