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: Implementation of ext2 file system #55152

Merged
merged 30 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9a7610c
fs: ext2: Implementation of basic operations
fzdobylak Feb 24, 2023
5d03d8e
tests: fs: ext2: Mount tests
fzdobylak Feb 24, 2023
8192ee0
fs: ext2: create dir and file operations
fzdobylak Feb 24, 2023
2082cdf
tests: fs: ext2: create file and directory tests
fzdobylak Feb 24, 2023
3b23e0f
tests: fs: Make mount flags test common
fzdobylak Feb 24, 2023
5d6cd72
tests: fs: ext2: Mount flags test
fzdobylak Feb 24, 2023
de2590a
fs: ext2: implement fs_stat
fzdobylak Feb 24, 2023
2b94817
tests: fs: ext2: use fs_stat in directory tests
fzdobylak Feb 24, 2023
5346445
fs: ext2: File operations
fzdobylak Feb 24, 2023
9667060
fs: ext2: implement fs_truncate
fzdobylak Feb 24, 2023
8448152
fs: ext2: implement unlink
fzdobylak Feb 24, 2023
fb031d1
tests: fs: Move testfs_utils to common directory
fzdobylak Feb 24, 2023
e235ced
tests: fs: ext2: Open flags test
fzdobylak Feb 24, 2023
9b1bb0c
fs: ext2: implement rename
fzdobylak Feb 24, 2023
36ce48d
fs: ext2: implement rest of directory operations
fzdobylak Feb 24, 2023
76c20b5
tests: fs: Make common dirops test
fzdobylak Feb 24, 2023
0f132bd
tests: fs: ext2: Add dirops test
fzdobylak Feb 24, 2023
a847de5
fs: ext2: impl fs_sync
fzdobylak Feb 24, 2023
16efff0
tests: fs: Make common fs_basic test
fzdobylak Feb 24, 2023
96245e0
tests: fs: ext2: Add basic fs test
fzdobylak Feb 24, 2023
2ff1bec
fs: ext2: create correct file system in mkfs
fzdobylak Mar 13, 2023
40aeca0
tests: fs: ext2: fix after changes in mkfs
fzdobylak Mar 13, 2023
c447aef
fs: ext2: fix blocks and inodes handling
fzdobylak Mar 17, 2023
856ac75
tests: fs: ext2: Add ext specific tests
fzdobylak Feb 24, 2023
45e54cf
fs: ext2: Interpret s_errors field in superblocks
fzdobylak Mar 17, 2023
9790185
fs: ext2: Update memory writes
fzdobylak Apr 14, 2023
aaa6bc4
samples: subsys: fs: create common fs sample
fzdobylak May 17, 2023
24f0112
fs: ext2: update how memory is allocated in ext2
fzdobylak May 19, 2023
e95373f
fs: ext2: Use disk structs to access data on disk
fzdobylak May 22, 2023
5d93403
doc: release: 3.5: Add notes about ext2 fs
fzdobylak Jul 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/releases/release-notes-3.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ Libraries / Subsystems
:c:struct:`mgmt_group` when registering a transport. See
:c:type:`smp_translate_error_fn` for function details.

* File systems

* Added support for ext2 file system.

HALs
****

Expand Down Expand Up @@ -313,5 +317,8 @@ Documentation
Tests and Samples
*****************

* Created common sample for file systems (`fs_sample`). It originates from sample for FAT
(`fat_fs`) and supports both FAT and ext2 file systems.

Known Issues
************
45 changes: 45 additions & 0 deletions include/zephyr/fs/ext2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2023 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_FS_EXT2_H_
#define ZEPHYR_INCLUDE_FS_EXT2_H_

#include <stdint.h>

/** @brief Configuration used to format ext2 file system.
*
* If a field is set to 0 then default value is used.
* (In volume name the first cell of an array must be 0 to use default value.)
*
* @param block_size Requested size of block.
* @param fs_size Requested size of file system. If 0 then whole available memory is used.
* @param bytes_per_inode Requested memory for one inode. It is used to calculate number of inodes
* in created file system.
* @param uuid UUID for created file system. Used when set_uuid is true.
* @param volume_name Name for created file system.
* @param set_uuid If true then UUID from that structure is used in created file system.
* If false then UUID (ver4) is generated.
*/
struct ext2_cfg {
uint32_t block_size;
uint32_t fs_size; /* Number of blocks that we want to take. */
uint32_t bytes_per_inode;
uint8_t uuid[16];
uint8_t volume_name[17]; /* If first byte is 0 then name ext2" is given. */
bool set_uuid;
};

#define FS_EXT2_DECLARE_DEFAULT_CONFIG(name) \
static struct ext2_cfg name = { \
.block_size = 1024, \
.fs_size = 0x800000, \
.bytes_per_inode = 4096, \
.volume_name = {'e', 'x', 't', '2', '\0'}, \
.set_uuid = false, \
}


#endif /* ZEPHYR_INCLUDE_FS_EXT2_H_ */
3 changes: 3 additions & 0 deletions include/zephyr/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ enum {
/** Identifier for in-tree LittleFS file system. */
FS_LITTLEFS,

/** Identifier for in-tree Ext2 file system. */
FS_EXT2,

/** Base identifier for external file systems. */
FS_TYPE_EXTERNAL_BASE,
};
Expand Down
21 changes: 17 additions & 4 deletions include/zephyr/fs/fs_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,33 @@ extern "C" {

#if (CONFIG_FILE_SYSTEM_MAX_FILE_NAME - 0) > 0
#define MAX_FILE_NAME CONFIG_FILE_SYSTEM_MAX_FILE_NAME

#else /* CONFIG_FILE_SYSTEM_MAX_FILE_NAME */
/* Select from enabled file systems */
#if defined(CONFIG_FILE_SYSTEM_LITTLEFS)
#define MAX_FILE_NAME 256
#elif defined(CONFIG_FAT_FILESYSTEM_ELM)

#if defined(CONFIG_FAT_FILESYSTEM_ELM)

#if defined(CONFIG_FS_FATFS_LFN)
#define MAX_FILE_NAME CONFIG_FS_FATFS_MAX_LFN
#else /* CONFIG_FS_FATFS_LFN */
#define MAX_FILE_NAME 12 /* Uses 8.3 SFN */
#endif /* CONFIG_FS_FATFS_LFN */
#else /* filesystem selection */

#endif

#if !defined(MAX_FILE_NAME) && defined(CONFIG_FILE_SYSTEM_EXT2)
#define MAX_FILE_NAME 255
fzdobylak marked this conversation as resolved.
Show resolved Hide resolved
#endif

#if !defined(MAX_FILE_NAME) && defined(CONFIG_FILE_SYSTEM_LITTLEFS)
#define MAX_FILE_NAME 256
#endif

#if !defined(MAX_FILE_NAME) /* filesystem selection */
/* Use standard 8.3 when no filesystem is explicitly selected */
#define MAX_FILE_NAME 12
#endif /* filesystem selection */

#endif /* CONFIG_FILE_SYSTEM_MAX_FILE_NAME */


Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#
# Copyright (c) 2023 Nordic Semiconductor ASA
# Copyright (c) 2023 Antmicro <www.antmicro.com>
#
# SPDX-License-Identifier: Apache-2.0
#

mainmenu "FAT Filesystem Sample Application"
mainmenu "Filesystems Sample Application"

config SAMPLE_FATFS_CREATE_SOME_ENTRIES
config FS_SAMPLE_CREATE_SOME_ENTRIES
bool "When no files are found on mounted partition create some"
default y
help
In case when no files could be listed, because there are none,
"some.dir" directory and "other.txt" file will be created
and list will run again to show them. This is useful when
showing how FAT works on non-SD devices like internal flash
or (Q)SPI connected memories, where it is not possible to
showing how file system works on non-SD devices like internal
flash or (Q)SPI connected memories, where it is not possible to
easily add files with use of other device.

source "Kconfig.zephyr"
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
.. _fat_fs:
.. _fs_sample:

FAT Filesystem Sample Application
Filesystems Sample Application
###################################

Overview
********

This sample app demonstrates use of the filesystem API and uses the FAT file
This sample app demonstrates use of the file system API and uses the FAT or Ext2 file
system driver with SDHC card, SoC flash or external flash chip.

To access device the sample uses :ref:`disk_access_api`.

Requirements for SD card support
********************************

This project requires SD card support and microSD card formatted with FAT filesystem.
See the :ref:`disk_access_api` documentation for Zephyr implementation details.
Boards that by default use SD card for storage:
``arduino_mkrzero``, ``esp_wrover_kit``, ``mimxrt1050_evk``, ``nrf52840_blip``
and ``olimexino_stm32``.
The sample should be able to run with any other board that has "zephyr,sdmmc-disk"
DT node enabled.
This project requires SD card support and microSD card formatted with proper file system
(FAT or Ext2) See the :ref:`disk_access_api` documentation for Zephyr implementation details.
Boards that by default use SD card for storage: ``arduino_mkrzero``, ``esp_wrover_kit``,
``mimxrt1050_evk``, ``nrf52840_blip`` and ``olimexino_stm32``. The sample should be able
to run with any other board that has "zephyr,sdmmc-disk" DT node enabled.

Requirements for setting up FAT FS on SoC flash
***********************************************
Expand All @@ -38,14 +36,14 @@ This type of configuration requires external flash device to be available
on DK board. Currently following boards support the configuration:
``nrf52840dk_nrf52840`` by ``nrf52840dk_nrf52840_qspi`` configuration.

Building and Running
********************
Building and Running FAT samples
********************************

Boards with default configurations, for example ``arduino_mkrzero`` or
``nrf52840dk_nrf52840`` using internal flash can be build using command:

.. zephyr-app-commands::
:zephyr-app: samples/subsys/fs/fat_fs
:zephyr-app: samples/subsys/fs/fs_sample
:board: nrf52840_blip
:goals: build
:compact:
Expand All @@ -57,7 +55,7 @@ for example ``nrf52840dk_nrf52840`` with MX25 device over QSPI, configuration
and DTS overlays need to be also selected. The command would look like this:

.. zephyr-app-commands::
:zephyr-app: samples/subsys/fs/fat_fs
:zephyr-app: samples/subsys/fs/fs_sample
:board: nrf52840dk_nrf52840
:gen-args: -DEXTRA_CONF_FILE=nrf52840dk_nrf52840_qspi.conf -DDTC_OVERLAY_FILE=nrf52840dk_nrf52840_qspi.overlay
:goals: build
Expand All @@ -70,3 +68,20 @@ sample lists them out on the debug serial output.
.. warning::
In case when mount fails the device may get re-formatted to FAT FS.
To disable this behaviour disable :kconfig:option:`CONFIG_FS_FATFS_MOUNT_MKFS` .

Building and Running EXT2 samples
*********************************

Ext2 sample can be build for ``hifive_unmatched`` or ``bl5340_dvk_cpuapp``. Because
FAT is default file system for this sample, additional flags must be passed to build
the sample.

.. zephyr-app-commands::
:zephyr-app: samples/subsys/fs/fs_sample
:board: hifive_unmatched
:gen-args: -DCONF_FILE=prj_ext.conf
:goals: build
:compact:

A microSD card must be present in a microSD card slot of the board, for the sample to execute.
After starting the sample a contents of a root directory should be printed on the console.
20 changes: 20 additions & 0 deletions samples/subsys/fs/fs_sample/boards/hifive_unmatched.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 Antmicro
*
* SPDX-License-Identifier: Apache-2.0
*/

&spi2 {
status = "okay";

sdhc0: sdhc@0 {
compatible = "zephyr,sdhc-spi-slot";
reg = <0>;
status = "okay";
mmc {
compatible = "zephyr,sdmmc-disk";
status = "okay";
};
spi-max-frequency = <20000000>;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ CONFIG_DISK_DRIVER_FLASH=y
# There may be no files on internal SoC flash, so this Kconfig
# options has ben enabled to create some if listing does not
# find in the first place.
CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES=y
CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES=y
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ CONFIG_DISK_DRIVER_FLASH=y
# There may be no files on internal SoC flash, so this Kconfig
# options has ben enabled to create some if listing does not
# find in the first place.
CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES=y
CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES=y
File renamed without changes.
20 changes: 20 additions & 0 deletions samples/subsys/fs/fs_sample/prj_ext.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2023 Antmicro <www.antmicro.com>
# SPDX-License-Identifier: Apache-2.0

CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y

CONFIG_MAIN_STACK_SIZE=2048

CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_EXT2=y

# Enable to allow formatting
# CONFIG_FILE_SYSTEM_MKFS=y
# CONFIG_TEST_RANDOM_GENERATOR=y

CONFIG_DISK_ACCESS=y
CONFIG_DISK_DRIVER_SDMMC=y

# First block of first partition after GPT
CONFIG_EXT2_DISK_STARTING_SECTOR=2082
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sample:
name: Fat filesystem sample
name: Filesystems sample
common:
tags: filesystem
modules:
Expand Down Expand Up @@ -42,3 +42,6 @@ tests:
filter: dt_compat_enabled("zephyr,sdmmc-disk")
integration_platforms:
- frdm_k64f
sample.filesystem.ext2:
extra_args: CONF_FILE="prj_ext.conf"
platform_allow: hifive_unmatched bl5340_dvk_cpuapp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2019 Tavish Naruka <tavishnaruka@gmail.com>
* Copyright (c) 2023 Nordic Semiconductor ASA
* Copyright (c) 2023 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -12,16 +13,17 @@
#include <zephyr/storage/disk_access.h>
#include <zephyr/logging/log.h>
#include <zephyr/fs/fs.h>
#include <ff.h>

LOG_MODULE_REGISTER(main);
#if defined(CONFIG_FAT_FILESYSTEM_ELM)

#include <ff.h>

/*
* Note the fatfs library is able to mount only strings inside _VOLUME_STRS
* in ffconf.h
*/
#define DISK_DRIVE_NAME "SD"
#define DISK_MOUNT_PT "/"DISK_DRIVE_NAME":"
#define MAX_PATH 128
#define SOME_FILE_NAME "some.dat"
#define SOME_DIR_NAME "some"
#define SOME_REQUIRED_LEN MAX(sizeof(SOME_FILE_NAME), sizeof(SOME_DIR_NAME))

static FATFS fat_fs;
/* mounting info */
Expand All @@ -30,8 +32,31 @@ static struct fs_mount_t mp = {
.fs_data = &fat_fs,
};

#elif defined(CONFIG_FILE_SYSTEM_EXT2)

#include <zephyr/fs/ext2.h>

#define DISK_DRIVE_NAME "SDMMC"
#define DISK_MOUNT_PT "/ext"

static struct fs_mount_t mp = {
.type = FS_EXT2,
.flags = FS_MOUNT_FLAG_NO_FORMAT,
.storage_dev = (void *)DISK_DRIVE_NAME,
.mnt_point = "/ext",
};

#endif

LOG_MODULE_REGISTER(main);

#define MAX_PATH 128
#define SOME_FILE_NAME "some.dat"
#define SOME_DIR_NAME "some"
#define SOME_REQUIRED_LEN MAX(sizeof(SOME_FILE_NAME), sizeof(SOME_DIR_NAME))

static int lsdir(const char *path);
#ifdef CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES
#ifdef CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES
static bool create_some_entries(const char *base_path)
{
char path[MAX_PATH];
Expand Down Expand Up @@ -71,10 +96,6 @@ static bool create_some_entries(const char *base_path)
}
#endif

/*
* Note the fatfs library is able to mount only strings inside _VOLUME_STRS
* in ffconf.h
*/
static const char *disk_mount_pt = DISK_MOUNT_PT;

int main(void)
Expand Down Expand Up @@ -113,10 +134,14 @@ int main(void)

int res = fs_mount(&mp);

#if defined(CONFIG_FAT_FILESYSTEM_ELM)
if (res == FR_OK) {
#else
if (res == 0) {
#endif
printk("Disk mounted.\n");
if (lsdir(disk_mount_pt) == 0) {
#ifdef CONFIG_SAMPLE_FATFS_CREATE_SOME_ENTRIES
#ifdef CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES
if (create_some_entries(disk_mount_pt)) {
lsdir(disk_mount_pt);
}
Expand All @@ -126,6 +151,8 @@ int main(void)
printk("Error mounting disk.\n");
}

fs_unmount(&mp);

while (1) {
k_sleep(K_MSEC(1000));
}
Expand Down