From 7f16770b91e13a5bdd49c1e94cd19f0b1f89a3b4 Mon Sep 17 00:00:00 2001 From: guohao15 Date: Wed, 24 Jan 2024 18:05:38 +0800 Subject: [PATCH] bch:alloc bch->buffer when offset not aligned Signed-off-by: guohao15 --- drivers/bch/bchlib_cache.c | 17 ++++++++++++++++- drivers/bch/bchlib_setup.c | 15 --------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/drivers/bch/bchlib_cache.c b/drivers/bch/bchlib_cache.c index cce8224229..81583d3ab9 100644 --- a/drivers/bch/bchlib_cache.c +++ b/drivers/bch/bchlib_cache.c @@ -23,6 +23,7 @@ ****************************************************************************/ #include +#include #include #include @@ -112,7 +113,7 @@ int bchlib_flushsector(FAR struct bchlib_s *bch, bool discard) * media. */ - if (bch->dirty) + if (bch->dirty && bch->buffer != NULL) { inode = bch->inode; @@ -168,6 +169,20 @@ int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector) FAR struct inode *inode; ssize_t ret = OK; + if (bch->buffer == NULL) + { +#if CONFIG_BCH_BUFFER_ALIGNMENT != 0 + bch->buffer = kmm_memalign(CONFIG_BCH_BUFFER_ALIGNMENT, bch->sectsize); +#else + bch->buffer = kmm_malloc(bch->sectsize); +#endif + if (bch->buffer == NULL) + { + ferr("Failed to allocate sector buffer\n"); + return -ENOMEM; + } + } + if (bch->sector != sector) { inode = bch->inode; diff --git a/drivers/bch/bchlib_setup.c b/drivers/bch/bchlib_setup.c index 53ca6f0734..f0454aedf2 100644 --- a/drivers/bch/bchlib_setup.c +++ b/drivers/bch/bchlib_setup.c @@ -110,21 +110,6 @@ int bchlib_setup(const char *blkdev, bool readonly, FAR void **handle) bch->sectsize = geo.geo_sectorsize; bch->sector = (size_t)-1; bch->readonly = readonly; - - /* Allocate the sector I/O buffer */ - -#if CONFIG_BCH_BUFFER_ALIGNMENT != 0 - bch->buffer = kmm_memalign(CONFIG_BCH_BUFFER_ALIGNMENT, bch->sectsize); -#else - bch->buffer = kmm_malloc(bch->sectsize); -#endif - if (!bch->buffer) - { - ferr("ERROR: Failed to allocate sector buffer\n"); - ret = -ENOMEM; - goto errout_with_bch; - } - *handle = bch; return OK;