Added buffers management to VFS.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15223 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2021-12-09 11:17:34 +00:00
parent 4dac93cf65
commit 613dec7ec7
16 changed files with 270 additions and 23 deletions

View File

@ -38,8 +38,22 @@
/**
* @brief Maximum filename length.
*/
#if !defined(VFS_CFG_MAX_NAMELEN) || defined(__DOXYGEN__)
#define VFS_CFG_MAX_NAMELEN 15
#if !defined(VFS_CFG_NAMELEN_MAX) || defined(__DOXYGEN__)
#define VFS_CFG_NAMELEN_MAX 15
#endif
/**
* @brief Maximum paths length.
*/
#if !defined(VFS_CFG_PATHLEN_MAX) || defined(__DOXYGEN__)
#define VFS_CFG_PATHLEN_MAX 1023
#endif
/**
* @brief Number of shared path buffers.
*/
#if !defined(VFS_CFG_PATHBUFS_NUM) || defined(__DOXYGEN__)
#define VFS_CFG_PATHBUFS_NUM 1
#endif
/** @} */

View File

@ -38,8 +38,22 @@
/**
* @brief Maximum filename length.
*/
#if !defined(VFS_CFG_MAX_NAMELEN) || defined(__DOXYGEN__)
#define VFS_CFG_MAX_NAMELEN 15
#if !defined(VFS_CFG_NAMELEN_MAX) || defined(__DOXYGEN__)
#define VFS_CFG_NAMELEN_MAX 15
#endif
/**
* @brief Maximum paths length.
*/
#if !defined(VFS_CFG_PATHLEN_MAX) || defined(__DOXYGEN__)
#define VFS_CFG_PATHLEN_MAX 1023
#endif
/**
* @brief Number of shared path buffers.
*/
#if !defined(VFS_CFG_PATHBUFS_NUM) || defined(__DOXYGEN__)
#define VFS_CFG_PATHBUFS_NUM 1
#endif
/** @} */

View File

@ -274,11 +274,13 @@ int main(void) {
/* Initializing an overlay VFS object overlaying a FatFS driver,
no need for names, both are root.*/
drvOverlayObjectInit(&root_overlay_driver,
drvFatFSObjectInit(&root_driver, ""), "");
drvFatFSObjectInit(&root_driver, ""),
NULL,
"");
#else
/* Initializing an overlay VFS object as a root, no overlaid driver,
no need for a name.*/
drvOverlayObjectInit(&root_overlay_driver, NULL, "");
drvOverlayObjectInit(&root_overlay_driver, NULL, NULL, "");
#endif
/* Registering a streams VFS driver on the VFS overlay root as "/dev".*/

View File

@ -325,8 +325,8 @@ static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) {
else {
nip->attr = (vfs_nodeattr_t)fip->fattrib;
nip->size = (vfs_offset_t)fip->fsize;
strncpy(nip->name, fip->fname, VFS_CFG_MAX_NAMELEN);
nip->name[VFS_CFG_MAX_NAMELEN] = '\0';
strncpy(nip->name, fip->fname, VFS_CFG_NAMELEN_MAX);
nip->name[VFS_CFG_NAMELEN_MAX] = '\0';
err = VFS_RET_SUCCESS;
}
}

View File

@ -91,7 +91,7 @@ static struct {
static msg_t match_driver(vfs_overlay_driver_c *odp,
const char **pathp,
vfs_driver_c **vdpp) {
char fname[VFS_CFG_MAX_NAMELEN + 1];
char fname[VFS_CFG_NAMELEN_MAX + 1];
msg_t err;
vfs_driver_c **pp;
@ -102,7 +102,7 @@ static msg_t match_driver(vfs_overlay_driver_c *odp,
/* Searching among registered drivers.*/
pp = &odp->drivers[0];
while (pp < &odp->drivers[odp->next_driver]) {
if (strncmp(fname, (*pp)->rootname, VFS_CFG_MAX_NAMELEN) == 0) {
if (strncmp(fname, (*pp)->rootname, VFS_CFG_NAMELEN_MAX) == 0) {
*vdpp = *pp;
return VFS_RET_SUCCESS;
}
@ -300,6 +300,7 @@ void __drv_overlay_init(void) {
*
* @param[out] vodp pointer to a @p vfs_overlay_driver_c structure
* @param[out] overlaid_drv pointer to a driver to be overlaid
* @param[out] path_prefix prefix to be added to the paths or @p NULL
* @param[in] rootname name to be attributed to this object
* @return A pointer to this initialized object.
*
@ -307,11 +308,13 @@ void __drv_overlay_init(void) {
*/
vfs_driver_c *drvOverlayObjectInit(vfs_overlay_driver_c *vodp,
vfs_driver_c *overlaid_drv,
const char *path_prefix,
const char *rootname) {
__base_object_objinit_impl(vodp, &driver_vmt);
vodp->rootname = rootname;
vodp->overlaid_drv = overlaid_drv;
vodp->path_prefix = path_prefix;
vodp->next_driver = 0U;
return (vfs_driver_c *)vodp;
@ -320,8 +323,8 @@ vfs_driver_c *drvOverlayObjectInit(vfs_overlay_driver_c *vodp,
/**
* @brief Registers a VFS driver as an overlay.
*
* @param[in] vodp pointer to a @p vfs_overlay_driver_c structure
* @return The operation result.
* @param[in] vodp pointer to a @p vfs_overlay_driver_c structure
* @return The operation result.
*
* @api
*/

View File

@ -109,6 +109,8 @@ typedef struct vfs_overlay_dir_node {
__vfs_driver_data \
/* Driver to be overlaid or NULL.*/ \
vfs_driver_c *overlaid_drv; \
/* Path prefix for the overlaid driver or NULL.*/ \
const char *path_prefix ; \
/* Next registration slot.*/ \
unsigned next_driver; \
/* Registration slots.*/ \
@ -146,6 +148,7 @@ extern "C" {
void __drv_overlay_init(void);
vfs_driver_c *drvOverlayObjectInit(vfs_overlay_driver_c *vodp,
vfs_driver_c *overlaid_drv,
const char *path_prefix,
const char *rootname);
msg_t drvOverlayRegisterDriver(vfs_overlay_driver_c *vodp,
vfs_driver_c *vdp);

View File

@ -159,7 +159,7 @@ static msg_t drv_open_file(void *instance,
(void)oflag;
do {
char fname[VFS_CFG_MAX_NAMELEN + 1];
char fname[VFS_CFG_NAMELEN_MAX + 1];
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
@ -172,7 +172,7 @@ static msg_t drv_open_file(void *instance,
dsep = &drvp->streams[0];
while (dsep->name != NULL) {
if (strncmp(fname, dsep->name, VFS_CFG_MAX_NAMELEN) == 0) {
if (strncmp(fname, dsep->name, VFS_CFG_NAMELEN_MAX) == 0) {
vfs_streams_file_node_c *sfnp;
sfnp = chPoolAlloc(&vfs_streams_driver_static.file_nodes_pool);

View File

@ -67,7 +67,7 @@
#include <unistd.h>
/* Dependencies.*/
#include "osal.h"
#include "ch.h"
#include "oop_base_object.h"
#include "oop_referenced_object.h"
#include "oop_synchronized_object.h"
@ -84,6 +84,7 @@
/* Base VFS headers.*/
#include "vfserrors.h"
#include "vfsparser.h"
#include "vfsbuffers.h"
#include "vfsnodes.h"
#include "vfsdrivers.h"

View File

@ -0,0 +1,72 @@
/*
ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
This file is part of ChibiOS.
ChibiOS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
ChibiOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file vfs/include/vfsbuffers.h
* @brief VFS header file.
* @details VFS shared path buffers header file.
*
* @addtogroup VFS_BUFFERS
* @{
*/
#ifndef VFS_BUFFERS_H
#define VFS_BUFFERS_H
/*===========================================================================*/
/* Module constants. */
/*===========================================================================*/
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Module data structures and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
void __vfs_buffers_init(void);
char *vfs_buffer_take(void);
void vfs_buffer_release(char *buf);
#ifdef __cplusplus
}
#endif
/*===========================================================================*/
/* Module inline functions. */
/*===========================================================================*/
#endif /* VFS_BUFFERS_H */
/** @} */

View File

@ -49,12 +49,28 @@
#endif
/* Configuration options checks.*/
#if !defined(VFS_CFG_MAX_NAMELEN)
#error "VFS_CFG_MAX_NAMELEN not defined in vfsconf.h"
#if !defined(VFS_CFG_NAMELEN_MAX)
#error "VFS_CFG_NAMELEN_MAX not defined in vfsconf.h"
#endif
#if VFS_CFG_MAX_NAMELEN < 12
#error "invalid value for VFS_CFG_MAX_NAMELEN"
#if VFS_CFG_NAMELEN_MAX < 12
#error "invalid value for VFS_CFG_NAMELEN_MAX"
#endif
#if !defined(VFS_CFG_PATHLEN_MAX)
#error "VFS_CFG_PATHLEN_MAX not defined in vfsconf.h"
#endif
#if VFS_CFG_PATHLEN_MAX < 63
#error "invalid value for VFS_CFG_PATHLEN_MAX"
#endif
#if !defined(VFS_CFG_PATHBUFS_NUM)
#error "VFS_CFG_PATHBUFS_NUM not defined in vfsconf.h"
#endif
#if VFS_CFG_PATHBUFS_NUM < 1
#error "invalid value for VFS_CFG_PATHBUFS_NUM"
#endif
#if !defined(VFS_CFG_ENABLE_DRV_OVERLAY)

View File

@ -85,7 +85,7 @@ typedef struct vfs_node_info {
/**
* @brief Name of the node.
*/
char name[VFS_CFG_MAX_NAMELEN + 1];
char name[VFS_CFG_NAMELEN_MAX + 1];
} vfs_node_info_t;
/**

View File

@ -60,6 +60,9 @@
*/
void vfsInit(void) {
/* Shared buffers manager initialization.*/
__vfs_buffers_init();
#if VFS_CFG_ENABLE_DRV_OVERLAY == TRUE
__drv_overlay_init();
#endif

104
os/vfs/src/vfsbuffers.c Normal file
View File

@ -0,0 +1,104 @@
/*
ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
This file is part of ChibiOS.
ChibiOS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
ChibiOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file vfs/src/vfsbuffers.c
* @brief VFS shared path buffers code.
*
* @addtogroup VFS_BUFFERS
* @{
*/
#include "vfs.h"
/*===========================================================================*/
/* Module local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Module exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Module local types. */
/*===========================================================================*/
/*===========================================================================*/
/* Module local variables. */
/*===========================================================================*/
/**
* @brief VFS static data.
*/
static struct {
/**
* @brief Guarded pool of path buffers.
*/
guarded_memory_pool_t path_buffers_pool;
/**
* @brief Shared path buffers.
*/
char path_buffers[VFS_CFG_PATHBUFS_NUM]
[VFS_CFG_PATHLEN_MAX + 1];
} vfs_buffers_static;
/*===========================================================================*/
/* Module local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Module exported functions. */
/*===========================================================================*/
/**
* @brief VFS initialization.
*
* @init
*/
void __vfs_buffers_init(void) {
chGuardedPoolObjectInit(&vfs_buffers_static.path_buffers_pool,
VFS_CFG_PATHLEN_MAX + 1);
chGuardedPoolLoadArray(&vfs_buffers_static.path_buffers_pool,
&vfs_buffers_static.path_buffers[0],
VFS_CFG_PATHBUFS_NUM);
}
/**
* @brief Claims a path buffer, waiting if not available.
*
* @return Pointer to the taken buffer.
*/
char *vfs_buffer_take(void) {
return (char *)chGuardedPoolAllocTimeout(&vfs_buffers_static.path_buffers_pool,
TIME_INFINITE);
}
/**
* @brief Releases a path buffer.
*
* @param[in] buf Buffer to be released.
*/
void vfs_buffer_release(char *buf) {
chGuardedPoolFree(&vfs_buffers_static.path_buffers_pool, (void *)buf);
}
/** @} */

View File

@ -128,7 +128,7 @@ msg_t vfs_parse_filename(const char **pathp, char *fname) {
return VFS_RET_ENOENT;
}
if (n > VFS_CFG_MAX_NAMELEN) {
if (n > VFS_CFG_NAMELEN_MAX) {
return VFS_RET_ENOENT;
}

View File

@ -38,8 +38,22 @@
/**
* @brief Maximum filename length.
*/
#if !defined(VFS_CFG_MAX_NAMELEN) || defined(__DOXYGEN__)
#define VFS_CFG_MAX_NAMELEN 15
#if !defined(VFS_CFG_NAMELEN_MAX) || defined(__DOXYGEN__)
#define VFS_CFG_NAMELEN_MAX 15
#endif
/**
* @brief Maximum paths length.
*/
#if !defined(VFS_CFG_PATHLEN_MAX) || defined(__DOXYGEN__)
#define VFS_CFG_PATHLEN_MAX 1023
#endif
/**
* @brief Number of shared path buffers.
*/
#if !defined(VFS_CFG_PATHBUFS_NUM) || defined(__DOXYGEN__)
#define VFS_CFG_PATHBUFS_NUM 1
#endif
/** @} */

View File

@ -1,5 +1,6 @@
# List of all the ChibiOS/VFS files.
VFSSRC := $(CHIBIOS)/os/vfs/src/vfsparser.c \
$(CHIBIOS)/os/vfs/src/vfsbuffers.c \
$(CHIBIOS)/os/vfs/src/vfs.c \
$(CHIBIOS)/os/vfs/drivers/fatfs/drvfatfs.c \
$(CHIBIOS)/os/vfs/drivers/overlay/drvoverlay.c \