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

{bp-Fs} bulk FS fixes #14042

Merged
merged 10 commits into from
Oct 10, 2024
6 changes: 6 additions & 0 deletions fs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
# the License.
#
# ##############################################################################

if(NOT "${CONFIG_FS_HEAPBUF_SECTION}" STREQUAL "")
target_compile_definitions(
fs PRIVATE FS_HEAPBUF_SECTION=${CONFIG_FS_HEAPBUF_SECTION})
endif()

nuttx_add_kernel_library(fs fs_initialize.c fs_heap.c)
nuttx_add_subdirectory()
target_include_directories(fs PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
10 changes: 9 additions & 1 deletion fs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,21 @@ config SENDFILE_BUFSIZE
Size of the I/O buffer to allocate in sendfile(). Default: 512b

config FS_HEAPSIZE
int "Independent heap bytes used by shm/tmpfs/pseudofile"
int "Independent heap bytes"
default 0
depends on FS_SHMFS || FS_TMPFS || PSEUDOFS_FILE
---help---
Support for shm/tmpfs/fs_pseudofile.c ram based fs memory.
default 0 to use kmm directly. independent heap disabled

config FS_HEAPBUF_SECTION
string "FS heap use Userheap section"
depends on FS_HEAPSIZE > 0
default ""
---help---
Allocated fs heap from the specified section. If not
specified, it will alloc from kernel heap.

config FS_REFCOUNT
bool "File reference count"
default !DEFAULT_SMALL
Expand Down
4 changes: 4 additions & 0 deletions fs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ include $(TOPDIR)/Make.defs

CSRCS = fs_initialize.c fs_heap.c

ifneq ($(CONFIG_FS_HEAPBUF_SECTION),"")
CFLAGS += ${DEFINE_PREFIX}FS_HEAPBUF_SECTION=CONFIG_FS_HEAPBUF_SECTION
endif

include inode/Make.defs
include vfs/Make.defs
include driver/Make.defs
Expand Down
5 changes: 3 additions & 2 deletions fs/binfs/fs_binfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <nuttx/lib/builtin.h>

#include "inode/inode.h"
#include "fs_heap.h"

#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_BINFS)

Expand Down Expand Up @@ -305,7 +306,7 @@ static int binfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
return -ENOENT;
}

bdir = kmm_zalloc(sizeof(*bdir));
bdir = fs_heap_zalloc(sizeof(*bdir));
if (bdir == NULL)
{
return -ENOMEM;
Expand All @@ -330,7 +331,7 @@ static int binfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
DEBUGASSERT(dir);
kmm_free(dir);
fs_heap_free(dir);
return 0;
}

Expand Down
21 changes: 11 additions & 10 deletions fs/cromfs/fs_cromfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <nuttx/fs/ioctl.h>

#include "cromfs.h"
#include "fs_heap.h"

#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_CROMFS)

Expand Down Expand Up @@ -785,18 +786,18 @@ static int cromfs_open(FAR struct file *filep, FAR const char *relpath,
* file.
*/

ff = kmm_zalloc(sizeof(struct cromfs_file_s));
ff = fs_heap_zalloc(sizeof(struct cromfs_file_s));
if (ff == NULL)
{
return -ENOMEM;
}

/* Create a file buffer to support partial sector accesses */

ff->ff_buffer = kmm_malloc(fs->cv_bsize);
ff->ff_buffer = fs_heap_malloc(fs->cv_bsize);
if (!ff->ff_buffer)
{
kmm_free(ff);
fs_heap_free(ff);
return -ENOMEM;
}

Expand Down Expand Up @@ -829,8 +830,8 @@ static int cromfs_close(FAR struct file *filep)

/* Free all resources consumed by the opened file */

kmm_free(ff->ff_buffer);
kmm_free(ff);
fs_heap_free(ff->ff_buffer);
fs_heap_free(ff);

return OK;
}
Expand Down Expand Up @@ -1118,18 +1119,18 @@ static int cromfs_dup(FAR const struct file *oldp, FAR struct file *newp)
* same node.
*/

newff = kmm_zalloc(sizeof(struct cromfs_file_s));
newff = fs_heap_zalloc(sizeof(struct cromfs_file_s));
if (newff == NULL)
{
return -ENOMEM;
}

/* Create a file buffer to support partial sector accesses */

newff->ff_buffer = kmm_malloc(fs->cv_bsize);
newff->ff_buffer = fs_heap_malloc(fs->cv_bsize);
if (newff->ff_buffer == NULL)
{
kmm_free(newff);
fs_heap_free(newff);
return -ENOMEM;
}

Expand Down Expand Up @@ -1236,7 +1237,7 @@ static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
return -ENOTDIR;
}

cdir = kmm_zalloc(sizeof(*cdir));
cdir = fs_heap_zalloc(sizeof(*cdir));
if (cdir == NULL)
{
return -ENOMEM;
Expand All @@ -1261,7 +1262,7 @@ static int cromfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
DEBUGASSERT(mountpt != NULL);
kmm_free(dir);
fs_heap_free(dir);
return 0;
}

Expand Down
7 changes: 4 additions & 3 deletions fs/driver/fs_blockpartition.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "driver/driver.h"
#include "inode/inode.h"
#include "fs_heap.h"

/****************************************************************************
* Private Types
Expand Down Expand Up @@ -284,7 +285,7 @@ static int part_unlink(FAR struct inode *inode)
FAR struct inode *parent = dev->parent;

inode_release(parent);
kmm_free(dev);
fs_heap_free(dev);

return OK;
}
Expand Down Expand Up @@ -332,7 +333,7 @@ int register_partition_with_inode(FAR const char *partition,

/* Allocate a partition device structure */

dev = kmm_zalloc(sizeof(*dev));
dev = fs_heap_zalloc(sizeof(*dev));
if (dev == NULL)
{
return -ENOMEM;
Expand Down Expand Up @@ -365,7 +366,7 @@ int register_partition_with_inode(FAR const char *partition,

errout_free:
inode_release(parent);
kmm_free(dev);
fs_heap_free(dev);
return ret;
}

Expand Down
28 changes: 14 additions & 14 deletions fs/fat/fs_fat32.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath,
* file.
*/

ff = kmm_zalloc(sizeof(struct fat_file_s));
ff = fs_heap_zalloc(sizeof(struct fat_file_s));
if (!ff)
{
ret = -ENOMEM;
Expand Down Expand Up @@ -379,7 +379,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath,
off_t offset = fat_seek(filep, ff->ff_size, SEEK_SET);
if (offset < 0)
{
kmm_free(ff);
fs_heap_free(ff);
return (int)offset;
}
}
Expand All @@ -391,7 +391,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath,
*/

errout_with_struct:
kmm_free(ff);
fs_heap_free(ff);

errout_with_lock:
nxmutex_unlock(&fs->fs_lock);
Expand Down Expand Up @@ -469,7 +469,7 @@ static int fat_close(FAR struct file *filep)

/* Then free the file structure itself. */

kmm_free(ff);
fs_heap_free(ff);
filep->f_priv = NULL;
return ret;
}
Expand Down Expand Up @@ -504,7 +504,7 @@ static int fat_zero_cluster(FAR struct fat_mountpt_s *fs, int cluster,
off_t end_sec = sector + DIV_ROUND_UP(end, fs->fs_hwsectorsize);
int ret;

buf = kmm_malloc(fs->fs_hwsectorsize);
buf = fs_heap_malloc(fs->fs_hwsectorsize);
if (!buf)
{
return -ENOMEM;
Expand Down Expand Up @@ -543,7 +543,7 @@ static int fat_zero_cluster(FAR struct fat_mountpt_s *fs, int cluster,
ret = OK;

out:
kmm_free(buf);
fs_heap_free(buf);

return ret;
}
Expand Down Expand Up @@ -1549,7 +1549,7 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp)
* dup'ed file.
*/

newff = kmm_malloc(sizeof(struct fat_file_s));
newff = fs_heap_malloc(sizeof(struct fat_file_s));
if (!newff)
{
ret = -ENOMEM;
Expand Down Expand Up @@ -1616,7 +1616,7 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp)
*/

errout_with_struct:
kmm_free(newff);
fs_heap_free(newff);

errout_with_lock:
nxmutex_unlock(&fs->fs_lock);
Expand Down Expand Up @@ -1647,7 +1647,7 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath,

fs = mountpt->i_private;

fdir = kmm_zalloc(sizeof(struct fat_dirent_s));
fdir = fs_heap_zalloc(sizeof(struct fat_dirent_s));
if (fdir == NULL)
{
return -ENOMEM;
Expand Down Expand Up @@ -1725,7 +1725,7 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath,
nxmutex_unlock(&fs->fs_lock);

errout_with_fdir:
kmm_free(fdir);
fs_heap_free(fdir);
return ret;
}

Expand All @@ -1740,7 +1740,7 @@ static int fat_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
DEBUGASSERT(dir);
kmm_free(dir);
fs_heap_free(dir);
return 0;
}

Expand Down Expand Up @@ -2230,7 +2230,7 @@ static int fat_bind(FAR struct inode *blkdriver, FAR const void *data,

/* Create an instance of the mountpt state structure */

fs = kmm_zalloc(sizeof(struct fat_mountpt_s));
fs = fs_heap_zalloc(sizeof(struct fat_mountpt_s));
if (!fs)
{
return -ENOMEM;
Expand All @@ -2252,7 +2252,7 @@ static int fat_bind(FAR struct inode *blkdriver, FAR const void *data,
if (ret != 0)
{
nxmutex_destroy(&fs->fs_lock);
kmm_free(fs);
fs_heap_free(fs);
return ret;
}

Expand Down Expand Up @@ -2354,7 +2354,7 @@ static int fat_unbind(FAR void *handle, FAR struct inode **blkdriver,
}

nxmutex_destroy(&fs->fs_lock);
kmm_free(fs);
fs_heap_free(fs);
return OK;
}

Expand Down
6 changes: 4 additions & 2 deletions fs/fat/fs_fat32.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <nuttx/kmalloc.h>
#include <nuttx/mutex.h>

#include "fs_heap.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
Expand Down Expand Up @@ -845,8 +847,8 @@
# define fat_io_alloc(s) fat_dma_alloc(s)
# define fat_io_free(m,s) fat_dma_free(m,s)
#else
# define fat_io_alloc(s) kmm_malloc(s)
# define fat_io_free(m,s) kmm_free(m)
# define fat_io_alloc(s) fs_heap_malloc(s)
# define fat_io_free(m,s) fs_heap_free(m)
#endif

/****************************************************************************
Expand Down
10 changes: 10 additions & 0 deletions fs/fs_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ static FAR struct mm_heap_s *g_fs_heap;

void fs_heap_initialize(void)
{
#ifdef FS_HEAPBUF_SECTION
static uint8_t buf[CONFIG_FS_HEAPSIZE] locate_data(FS_HEAPBUF_SECTION);
#else
FAR void *buf = kmm_malloc(CONFIG_FS_HEAPSIZE);
#endif

DEBUGASSERT(buf != NULL);
g_fs_heap = mm_initialize("heapfs", buf, CONFIG_FS_HEAPSIZE);
}
Expand All @@ -48,6 +53,11 @@ FAR void *fs_heap_zalloc(size_t size)
return mm_zalloc(g_fs_heap, size);
}

FAR void *fs_heap_malloc(size_t size)
{
return mm_malloc(g_fs_heap, size);
}

size_t fs_heap_malloc_size(FAR void *mem)
{
return mm_malloc_size(g_fs_heap, mem);
Expand Down
2 changes: 2 additions & 0 deletions fs/fs_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
#if defined(CONFIG_FS_HEAPSIZE) && CONFIG_FS_HEAPSIZE > 0
void fs_heap_initialize(void);
FAR void *fs_heap_zalloc(size_t size);
FAR void *fs_heap_malloc(size_t size);
size_t fs_heap_malloc_size(FAR void *mem);
FAR void *fs_heap_realloc(FAR void *oldmem, size_t size);
void fs_heap_free(FAR void *mem);
#else
# define fs_heap_initialize()
# define fs_heap_zalloc kmm_zalloc
# define fs_heap_malloc kmm_malloc
# define fs_heap_malloc_size kmm_malloc_size
# define fs_heap_realloc kmm_realloc
# define fs_heap_free kmm_free
Expand Down
Loading
Loading