This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new d9270f9ea8 bch: add BIOC_DISCARD ioctl that discards cached sector
d9270f9ea8 is described below

commit d9270f9ea8fb658800edef1e83d54dd5bc4aed4f
Author: Michal Lenc <michall...@seznam.cz>
AuthorDate: Wed Jan 22 11:35:52 2025 +0100

    bch: add BIOC_DISCARD ioctl that discards cached sector
    
    This forces the bch layer to read the sector from the physical device
    instead of using the cached values. It is necessary to call when the
    device is updated from the different source than bch, for example
    erased by the MTD ioctl command.
    
    It also has to invalidate readahead buffer from FTL if option
    CONFIG_DRVR_READAHEAD is set.
    
    Signed-off-by: Michal Lenc <michall...@seznam.cz>
---
 drivers/bch/bchdev_driver.c      | 12 +++++++++++-
 drivers/misc/rwbuffer.c          | 26 ++++++++++++++++++++++++++
 drivers/mtd/ftl.c                |  7 +++++++
 include/nuttx/drivers/rwbuffer.h |  4 ++++
 include/nuttx/fs/ioctl.h         |  4 ++++
 5 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/drivers/bch/bchdev_driver.c b/drivers/bch/bchdev_driver.c
index 7032908a5b..59c97a6ea8 100644
--- a/drivers/bch/bchdev_driver.c
+++ b/drivers/bch/bchdev_driver.c
@@ -435,6 +435,14 @@ static int bch_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
         break;
 #endif
 
+      case BIOC_DISCARD:
+        {
+          /* Invalidate the sector so next read is from the device- */
+
+          bch->sector = (size_t)-1;
+          goto ioctl_default;
+        }
+
       case BIOC_FLUSH:
         {
           /* Flush any dirty pages remaining in the cache */
@@ -450,6 +458,7 @@ static int bch_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
 
       /* Pass the IOCTL command on to the contained block driver. */
 
+ioctl_default:
       default:
         {
           FAR struct inode *bchinode = bch->inode;
@@ -462,7 +471,8 @@ static int bch_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
 
               /* Drivers may not support command BIOC_FLUSH */
 
-              if (ret == -ENOTTY && cmd == BIOC_FLUSH)
+              if (ret == -ENOTTY && (cmd == BIOC_FLUSH ||
+                  cmd == BIOC_DISCARD))
                 {
                   ret = 0;
                 }
diff --git a/drivers/misc/rwbuffer.c b/drivers/misc/rwbuffer.c
index 91e6307fde..9a357a4ab7 100644
--- a/drivers/misc/rwbuffer.c
+++ b/drivers/misc/rwbuffer.c
@@ -1279,4 +1279,30 @@ int rwb_flush(FAR struct rwbuffer_s *rwb)
 }
 #endif
 
+/****************************************************************************
+ * Name: rwb_flush
+ *
+ * Description:
+ *   Flush the write buffer
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_DRVR_READAHEAD
+int rwb_discard(FAR struct rwbuffer_s *rwb)
+{
+  int ret;
+
+  ret = rwb_lock(&rwb->rhlock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  rwb_resetrhbuffer(rwb);
+  rwb_unlock(&rwb->rhlock);
+
+  return ret;
+}
+#endif
+
 #endif /* CONFIG_DRVR_WRITEBUFFER || CONFIG_DRVR_READAHEAD */
diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
index 04f9fc7592..3b0e0a29b7 100644
--- a/drivers/mtd/ftl.c
+++ b/drivers/mtd/ftl.c
@@ -716,6 +716,13 @@ static int ftl_ioctl(FAR struct inode *inode, int cmd, 
unsigned long arg)
 
   dev = inode->i_private;
 
+  if (cmd == BIOC_DISCARD)
+    {
+#ifdef CONFIG_FTL_READAHEAD
+      rwb_discard(&dev->rwb);
+#endif
+    }
+
   if (cmd == BIOC_FLUSH)
     {
 #ifdef CONFIG_FTL_WRITEBUFFER
diff --git a/include/nuttx/drivers/rwbuffer.h b/include/nuttx/drivers/rwbuffer.h
index 32ce6db0e0..30b1463e33 100644
--- a/include/nuttx/drivers/rwbuffer.h
+++ b/include/nuttx/drivers/rwbuffer.h
@@ -198,6 +198,10 @@ int rwb_invalidate(FAR struct rwbuffer_s *rwb,
 int rwb_flush(FAR struct rwbuffer_s *rwb);
 #endif
 
+#ifdef CONFIG_DRVR_READAHEAD
+int rwb_discard(FAR struct rwbuffer_s *rwb);
+#endif
+
 #undef EXTERN
 #if defined(__cplusplus)
 }
diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h
index 67b7304efd..ed2d62bb67 100644
--- a/include/nuttx/fs/ioctl.h
+++ b/include/nuttx/fs/ioctl.h
@@ -348,6 +348,10 @@
                                            *      to return sector numbers.
                                            * OUT: Data return in user-provided
                                            *      buffer. */
+#define BIOC_DISCARD    _BIOC(0x0011)     /* Discards the block device read 
buffer
+                                           * IN:  None
+                                           * OUT: None (ioctl return value 
provides
+                                           *      success/failure indication). 
*/
 
 /* NuttX MTD driver ioctl definitions ***************************************/
 

Reply via email to