@@ -251,13 +251,14 @@ static int lfs_bd_crc(lfs_t *lfs,
251251}
252252
253253#ifndef LFS_READONLY
254- static int lfs_bd_flush (lfs_t * lfs ,
255- lfs_cache_t * pcache , lfs_cache_t * rcache , bool validate ) {
256- if (pcache -> block != LFS_BLOCK_NULL && pcache -> block != LFS_BLOCK_INLINE ) {
257- LFS_ASSERT (pcache -> block < lfs -> block_count );
258- lfs_size_t diff = lfs_alignup (pcache -> size , lfs -> cfg -> prog_size );
259- int err = lfs -> cfg -> prog (lfs -> cfg , pcache -> block ,
260- pcache -> off , pcache -> buffer , diff );
254+ static int lfs_bd_flush_direct (lfs_t * lfs ,
255+ lfs_cache_t * rcache , lfs_block_t block ,
256+ lfs_off_t off , const void * buffer , lfs_size_t size , bool validate ) {
257+ if (block != LFS_BLOCK_NULL && block != LFS_BLOCK_INLINE ) {
258+ LFS_ASSERT (block < lfs -> block_count );
259+ lfs_size_t diff = lfs_alignup (size , lfs -> cfg -> prog_size );
260+ int err = lfs -> cfg -> prog (lfs -> cfg , block ,
261+ off , buffer , diff );
261262 LFS_ASSERT (err <= 0 );
262263 if (err ) {
263264 return err ;
@@ -268,7 +269,7 @@ static int lfs_bd_flush(lfs_t *lfs,
268269 lfs_cache_drop (lfs , rcache );
269270 int res = lfs_bd_cmp (lfs ,
270271 NULL , rcache , diff ,
271- pcache -> block , pcache -> off , pcache -> buffer , diff );
272+ block , off , buffer , diff );
272273 if (res < 0 ) {
273274 return res ;
274275 }
@@ -278,11 +279,23 @@ static int lfs_bd_flush(lfs_t *lfs,
278279 }
279280 }
280281
281- lfs_cache_zero (lfs , pcache );
282282 }
283283
284284 return 0 ;
285285}
286+
287+ static int lfs_bd_flush (lfs_t * lfs ,
288+ lfs_cache_t * pcache , lfs_cache_t * rcache , bool validate ) {
289+ int ret = 0 ;
290+ if (pcache -> block != LFS_BLOCK_NULL && pcache -> block != LFS_BLOCK_INLINE ) {
291+ ret = lfs_bd_flush_direct (lfs , rcache , pcache -> block , pcache -> off ,
292+ pcache -> buffer , pcache -> size , validate );
293+ if (ret == 0 )
294+ lfs_cache_zero (lfs , pcache );
295+ }
296+ return ret ;
297+ }
298+
286299#endif
287300
288301#ifndef LFS_READONLY
@@ -339,6 +352,16 @@ static int lfs_bd_prog(lfs_t *lfs,
339352 // entire block or manually flushing the pcache
340353 LFS_ASSERT (pcache -> block == LFS_BLOCK_NULL );
341354
355+ if (size >= lfs -> cfg -> cache_size ) {
356+ /* data do not fit in cache, direct write */
357+ int err = lfs_bd_flush_direct (lfs , rcache , block , off ,
358+ data , size , validate );
359+ if (err ) {
360+ return err ;
361+ }
362+ break ;
363+ }
364+
342365 // prepare pcache, first condition can no longer fail
343366 pcache -> block = block ;
344367 pcache -> off = lfs_aligndown (off , lfs -> cfg -> prog_size );
0 commit comments