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

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

commit ae4dc77d8f87816c79ce6250e06ab3ba4ecb581c
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon Aug 24 15:20:25 2026 +0200

    fs/rpmsgfs: Forward inotify watches and remote events
    
    Add RPMSGFS_WATCH (ADD answers with the server watch descriptor, RM
    carries it back and is not answered) and RPMSGFS_NOTIFY (server to
    client, unsolicited, watch descriptor + directory entry name).  The
    server only keeps its inotify instance on a kernel thread; the client
    translates descriptors back to mountpoint paths and injects the events
    with notify_event() from the low priority work queue (the rpmsg rx
    thread must not block on the inotify locks).  Gated by
    CONFIG_FS_RPMSGFS_SERVER_NOTIFY on the server side; without it the
    server answers RPMSGFS_WATCH with -ENOSYS and the client keeps a
    local-only watch.
    
    Signed-off-by: p-szafonimateusz <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 Documentation/os/filesystem/rpmsgfs.rst |   8 ++
 fs/rpmsgfs/Kconfig                      |   8 ++
 fs/rpmsgfs/rpmsgfs.c                    | 232 +++++++++++++++++++++++++++++++-
 fs/rpmsgfs/rpmsgfs.h                    |  39 ++++++
 fs/rpmsgfs/rpmsgfs_client.c             | 111 ++++++++++++++-
 fs/rpmsgfs/rpmsgfs_server.c             | 143 +++++++++++++++++++-
 6 files changed, 532 insertions(+), 9 deletions(-)

diff --git a/Documentation/os/filesystem/rpmsgfs.rst 
b/Documentation/os/filesystem/rpmsgfs.rst
index 6571bbe7bf1..891ed8a761c 100644
--- a/Documentation/os/filesystem/rpmsgfs.rst
+++ b/Documentation/os/filesystem/rpmsgfs.rst
@@ -30,3 +30,11 @@ Using the following command to mount the master's ``/proc`` 
file system to ``/pr
 
 Note the ``-o cpu=master,fs=/proc`` specifies the ``master`` node's ``/proc`` 
path as the source, the ``/proc.master`` is the mount point at remote side. All 
files under that mount point is actually hosted at the master side. The ``-t 
rpmsgfs`` selects the RPMsg file system driver to serve the operation.
 
+
+inotify
+=======
+
+With ``CONFIG_FS_NOTIFY`` on the client and
+``CONFIG_FS_RPMSGFS_SERVER_NOTIFY`` on the server, inotify watches are
+forwarded to the server and server-side changes to watched files are
+reported back to local watchers.
diff --git a/fs/rpmsgfs/Kconfig b/fs/rpmsgfs/Kconfig
index 1faa19fc309..4e081671716 100644
--- a/fs/rpmsgfs/Kconfig
+++ b/fs/rpmsgfs/Kconfig
@@ -38,3 +38,11 @@ config FS_RPMSGFS_SERVER
        depends on RPMSG
        ---help---
                Initialize RPMSG file system server automatically.
+
+config FS_RPMSGFS_SERVER_NOTIFY
+       bool "RPMSG File Server inotify forwarding"
+       default y
+       depends on FS_RPMSGFS_SERVER && FS_NOTIFY
+       ---help---
+               Watch the paths requested by the rpmsgfs client and forward
+               the inotify events back to the client core.
diff --git a/fs/rpmsgfs/rpmsgfs.c b/fs/rpmsgfs/rpmsgfs.c
index 7767d32c755..b3cf8cb31fe 100644
--- a/fs/rpmsgfs/rpmsgfs.c
+++ b/fs/rpmsgfs/rpmsgfs.c
@@ -39,10 +39,13 @@
 #include <limits.h>
 
 #include <nuttx/lib/lib.h>
+#include <nuttx/list.h>
 #include <nuttx/mutex.h>
+#include <nuttx/nuttx.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/fs/ioctl.h>
 #include <nuttx/signal.h>
+#include <nuttx/spinlock.h>
 
 #include "rpmsgfs.h"
 #include "fs_heap.h"
@@ -87,7 +90,23 @@ struct rpmsgfs_mountpt_s
   char                       fs_root[PATH_MAX];
   void                       *handle;
   int                        timeout;  /* Connect timeout */
+#ifdef CONFIG_FS_NOTIFY
+  FAR struct inode           *mountpt;     /* For notify_event() */
+  struct list_node           watch_list;   /* wd to path translations */
+  spinlock_t                 watch_lock;   /* Protects watch_list */
+#endif
+};
+
+#ifdef CONFIG_FS_NOTIFY
+/* One watched path: translates the server watch descriptor back */
+
+struct rpmsgfs_watchmap_s
+{
+  struct list_node node;
+  int              wd;
+  char             relpath[1];
 };
+#endif
 
 /****************************************************************************
  * Private Function Prototypes
@@ -104,7 +123,6 @@ static off_t   rpmsgfs_seek(FAR struct file *filep, off_t 
offset,
                             int whence);
 static int     rpmsgfs_ioctl(FAR struct file *filep, int cmd,
                              unsigned long arg);
-
 static int     rpmsgfs_sync(FAR struct file *filep);
 static int     rpmsgfs_dup(FAR const struct file *oldp,
                            FAR struct file *newp);
@@ -162,6 +180,12 @@ static int     rpmsgfs_lstat(FAR struct inode *mountpt,
                              FAR struct stat *buf);
 #endif
 
+#ifdef CONFIG_FS_NOTIFY
+static int     rpmsgfs_notify(FAR struct inode *mountpt, int wd,
+                              FAR const char *relpath, int action,
+                              uint32_t mask);
+#endif
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -215,6 +239,10 @@ const struct mountpt_operations g_rpmsgfs_operations =
   rpmsgfs_readlink,      /* readlink */
   rpmsgfs_lstat,         /* lstat */
 #endif
+
+#ifdef CONFIG_FS_NOTIFY
+  rpmsgfs_notify,        /* notify */
+#endif
 };
 
 /****************************************************************************
@@ -646,6 +674,176 @@ static int rpmsgfs_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
   return ret;
 }
 
+#ifdef CONFIG_FS_NOTIFY
+
+/****************************************************************************
+ * Name: rpmsgfs_notify_findmap
+ *
+ * Description: Translate a server watch descriptor back to its path.
+ *
+ ****************************************************************************/
+
+static FAR struct rpmsgfs_watchmap_s *
+rpmsgfs_notify_findmap(FAR struct rpmsgfs_mountpt_s *fs, int wd)
+{
+  FAR struct rpmsgfs_watchmap_s *map;
+
+  list_for_every_entry(&fs->watch_list, map, struct rpmsgfs_watchmap_s,
+                       node)
+    {
+      if (map->wd == wd)
+        {
+          return map;
+        }
+    }
+
+  return NULL;
+}
+
+static void rpmsgfs_notify_cb(FAR void *arg, int wd,
+                              FAR const char *name, uint32_t mask)
+{
+  FAR struct rpmsgfs_mountpt_s *fs = arg;
+  FAR struct rpmsgfs_watchmap_s *map;
+  FAR char *relpath;
+  irqstate_t flags;
+
+  /* Runs on the rpmsg rx thread.  The inotify lock is only ever held
+   * briefly and never across an rpmsgfs request, so injecting the
+   * event directly here cannot block the rx thread against a pending
+   * reply.
+   */
+
+  relpath = lib_get_pathbuffer();
+  if (relpath == NULL)
+    {
+      return;
+    }
+
+  flags = spin_lock_irqsave(&fs->watch_lock);
+  map = rpmsgfs_notify_findmap(fs, wd);
+  if (map != NULL)
+    {
+      if (name[0] != '\0')
+        {
+          snprintf(relpath, PATH_MAX, "%s/%s", map->relpath, name);
+        }
+      else
+        {
+          strlcpy(relpath, map->relpath, PATH_MAX);
+        }
+    }
+
+  spin_unlock_irqrestore(&fs->watch_lock, flags);
+
+  if (map != NULL && fs->mountpt != NULL)
+    {
+      notify_event(fs->mountpt, relpath, mask);
+    }
+
+  lib_put_pathbuffer(relpath);
+}
+
+/****************************************************************************
+ * Name: rpmsgfs_notify
+ *
+ * Description: Forward the inotify watch life cycle to the server.
+ *
+ ****************************************************************************/
+
+static int rpmsgfs_notify(FAR struct inode *mountpt, int wd,
+                          FAR const char *relpath, int action,
+                          uint32_t mask)
+{
+  FAR struct rpmsgfs_mountpt_s *fs;
+  FAR struct rpmsgfs_watchmap_s *map;
+  irqstate_t flags;
+  FAR char *path;
+  size_t len;
+  int ret;
+
+  fs = mountpt->i_private;
+  DEBUGASSERT(fs != NULL);
+
+  if (action == NOTIFY_WATCH_RM)
+    {
+      flags = spin_lock_irqsave(&fs->watch_lock);
+      map = rpmsgfs_notify_findmap(fs, wd);
+      if (map != NULL)
+        {
+          list_delete(&map->node);
+        }
+
+      spin_unlock_irqrestore(&fs->watch_lock, flags);
+      if (map != NULL)
+        {
+          fs_heap_free(map);
+        }
+
+      return rpmsgfs_client_watch_rm(fs->handle, wd);
+    }
+
+  len = strlen(relpath);
+  map = fs_heap_malloc(sizeof(*map) + len);
+  if (map == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  memcpy(map->relpath, relpath, len + 1);
+
+  path = lib_get_pathbuffer();
+  if (path == NULL)
+    {
+      fs_heap_free(map);
+      return -ENOMEM;
+    }
+
+  ret = nxmutex_lock(&fs->fs_lock);
+  if (ret < 0)
+    {
+      lib_put_pathbuffer(path);
+      fs_heap_free(map);
+      return ret;
+    }
+
+  /* Cache the mountpoint inode for notify_event() */
+
+  fs->mountpt = mountpt;
+
+  rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
+  ret = rpmsgfs_client_watch_add(fs->handle, path, mask);
+  nxmutex_unlock(&fs->fs_lock);
+  lib_put_pathbuffer(path);
+
+  if (ret < 0)
+    {
+      fs_heap_free(map);
+      return ret;
+    }
+
+  /* The server returns the same wd when the path is already watched
+   * (a wider mask reuses the handle); keep a single map per wd.
+   */
+
+  map->wd = ret;
+  flags = spin_lock_irqsave(&fs->watch_lock);
+  if (rpmsgfs_notify_findmap(fs, ret) == NULL)
+    {
+      list_add_tail(&fs->watch_list, &map->node);
+      map = NULL;
+    }
+
+  spin_unlock_irqrestore(&fs->watch_lock, flags);
+  if (map != NULL)
+    {
+      fs_heap_free(map);
+    }
+
+  return ret;
+}
+#endif /* CONFIG_FS_NOTIFY */
+
 /****************************************************************************
  * Name: rpmsgfs_sync
  *
@@ -1130,6 +1328,12 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR 
const void *data,
 
   nxmutex_init(&fs->fs_lock);
 
+#ifdef CONFIG_FS_NOTIFY
+  list_initialize(&fs->watch_list);
+  spin_lock_init(&fs->watch_lock);
+  rpmsgfs_client_set_notify(fs->handle, rpmsgfs_notify_cb, fs);
+#endif
+
   /* Initialize the allocated mountpt state structure.  The filesystem is
    * responsible for one reference ont the blkdriver inode and does not
    * have to addref() here (but does have to release in ubind().
@@ -1198,6 +1402,24 @@ static int rpmsgfs_unbind(FAR void *handle, FAR struct 
inode **blkdriver,
       return (flags != 0) ? -ENOSYS : -EBUSY;
     }
 
+#ifdef CONFIG_FS_NOTIFY
+  /* Stop remote event delivery and drop the watch translations */
+
+  rpmsgfs_client_set_notify(fs->handle, NULL, NULL);
+
+  for (; ; )
+    {
+      FAR struct list_node *node = list_remove_head(&fs->watch_list);
+
+      if (node == NULL)
+        {
+          break;
+        }
+
+      fs_heap_free(container_of(node, struct rpmsgfs_watchmap_s, node));
+    }
+#endif
+
   ret = rpmsgfs_client_unbind(fs->handle);
   nxmutex_unlock(&fs->fs_lock);
   if (ret < 0)
@@ -1680,7 +1902,7 @@ static ssize_t rpmsgfs_readlink(FAR struct inode *mountpt,
 
   fs = mountpt->i_private;
 
-  path = lib_get_tempbuffer(PATH_MAX);
+  path = lib_get_pathbuffer();
   if (path == NULL)
     {
       return -ENOMEM;
@@ -1703,7 +1925,7 @@ static ssize_t rpmsgfs_readlink(FAR struct inode *mountpt,
   nxmutex_unlock(&fs->fs_lock);
 
 errout_with_path:
-  lib_put_tempbuffer(path);
+  lib_put_pathbuffer(path);
   return ret;
 }
 
@@ -1730,7 +1952,7 @@ static int rpmsgfs_lstat(FAR struct inode *mountpt, FAR 
const char *relpath,
 
   fs = mountpt->i_private;
 
-  path = lib_get_tempbuffer(PATH_MAX);
+  path = lib_get_pathbuffer();
   if (path == NULL)
     {
       return -ENOMEM;
@@ -1753,7 +1975,7 @@ static int rpmsgfs_lstat(FAR struct inode *mountpt, FAR 
const char *relpath,
   nxmutex_unlock(&fs->fs_lock);
 
 errout_with_path:
-  lib_put_tempbuffer(path);
+  lib_put_pathbuffer(path);
   return ret;
 }
 #endif
diff --git a/fs/rpmsgfs/rpmsgfs.h b/fs/rpmsgfs/rpmsgfs.h
index 9ac9ff77760..5bffa9b104f 100644
--- a/fs/rpmsgfs/rpmsgfs.h
+++ b/fs/rpmsgfs/rpmsgfs.h
@@ -66,6 +66,8 @@
 #define RPMSGFS_READLINK        24
 #define RPMSGFS_LINK            25
 #define RPMSGFS_LSTAT           26
+#define RPMSGFS_WATCH           27
+#define RPMSGFS_NOTIFY          28
 
 /****************************************************************************
  * Public Types
@@ -227,6 +229,35 @@ begin_packed_struct struct rpmsgfs_fchstat_s
 
 #define rpmsgfs_chstat_s rpmsgfs_fchstat_s
 
+/* Client to server: start/stop watching a path.  ADD is answered with
+ * the server watch descriptor in header.result; RM carries it back in
+ * wd and is not answered.
+ */
+
+begin_packed_struct struct rpmsgfs_watch_s
+{
+  struct rpmsgfs_header_s header;
+  int32_t                 action;      /* NOTIFY_WATCH_ADD or _RM */
+  int32_t                 wd;          /* RM: server watch descriptor */
+  uint32_t                mask;        /* inotify event mask */
+  char                    pathname[0]; /* ADD: server absolute path */
+} end_packed_struct;
+
+/* Server to client, unsolicited: a watched path changed */
+
+begin_packed_struct struct rpmsgfs_notify_s
+{
+  struct rpmsgfs_header_s header;
+  int32_t                 wd;          /* Server watch descriptor */
+  uint32_t                mask;        /* inotify event mask */
+  char                    name[0];     /* Directory entry, may be empty */
+} end_packed_struct;
+
+/* Remote event callback; runs on the rpmsg rx thread, must not block */
+
+typedef CODE void (*rpmsgfs_notify_t)(FAR void *arg, int wd,
+                                      FAR const char *name, uint32_t mask);
+
 /****************************************************************************
  * Internal function prototypes
  ****************************************************************************/
@@ -278,6 +309,14 @@ int       rpmsgfs_client_link(FAR void *handle, FAR const 
char *path1,
 int       rpmsgfs_client_lstat(FAR void *handle, FAR const char *path,
                                FAR struct stat *buf);
 #endif
+#ifdef CONFIG_FS_NOTIFY
+int       rpmsgfs_client_watch_add(FAR void *handle, FAR const char *path,
+                                   uint32_t mask);
+int       rpmsgfs_client_watch_rm(FAR void *handle, int wd);
+void      rpmsgfs_client_set_notify(FAR void *handle,
+                                    rpmsgfs_notify_t callback,
+                                    FAR void *arg);
+#endif
 
 /****************************************************************************
  * Public Function Prototypes
diff --git a/fs/rpmsgfs/rpmsgfs_client.c b/fs/rpmsgfs/rpmsgfs_client.c
index 87d22a1bb68..e2687bf2ee8 100644
--- a/fs/rpmsgfs/rpmsgfs_client.c
+++ b/fs/rpmsgfs/rpmsgfs_client.c
@@ -34,7 +34,9 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <nuttx/nuttx.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
 #include <nuttx/nuttx.h>
 #include <nuttx/fs/ioctl.h>
 #include <nuttx/rpmsg/rpmsg.h>
@@ -52,6 +54,10 @@ struct rpmsgfs_s
   struct rpmsg_endpoint ept;
   char                  cpuname[RPMSG_NAME_SIZE];
   sem_t                 wait;
+#ifdef CONFIG_FS_NOTIFY
+  rpmsgfs_notify_t      notify_cb;
+  FAR void             *notify_arg;
+#endif
 };
 
 struct rpmsgfs_cookie_s
@@ -91,6 +97,11 @@ static int rpmsgfs_readlink_handler(FAR struct 
rpmsg_endpoint *ept,
                                     FAR void *data, size_t len,
                                     uint32_t src, FAR void *priv);
 #endif
+#ifdef CONFIG_FS_NOTIFY
+static int rpmsgfs_notify_handler(FAR struct rpmsg_endpoint *ept,
+                                  FAR void *data, size_t len,
+                                  uint32_t src, FAR void *priv);
+#endif
 static void rpmsgfs_device_created(struct rpmsg_device *rdev,
                                    FAR void *priv_);
 static void rpmsgfs_device_destroy(struct rpmsg_device *rdev,
@@ -139,6 +150,10 @@ static const rpmsg_ept_cb g_rpmsgfs_handler[] =
   [RPMSGFS_LINK]      = rpmsgfs_default_handler,
   [RPMSGFS_LSTAT]     = rpmsgfs_stat_handler,
 #endif
+#ifdef CONFIG_FS_NOTIFY
+  [RPMSGFS_WATCH]     = rpmsgfs_default_handler,
+  [RPMSGFS_NOTIFY]    = rpmsgfs_notify_handler,
+#endif
 };
 
 /****************************************************************************
@@ -328,6 +343,26 @@ static int rpmsgfs_readlink_handler(FAR struct 
rpmsg_endpoint *ept,
 }
 #endif
 
+#ifdef CONFIG_FS_NOTIFY
+static int rpmsgfs_notify_handler(FAR struct rpmsg_endpoint *ept,
+                                  FAR void *data, size_t len,
+                                  uint32_t src, FAR void *priv_)
+{
+  FAR struct rpmsgfs_s *priv =
+      container_of(ept, struct rpmsgfs_s, ept);
+  FAR struct rpmsgfs_notify_s *msg = data;
+
+  /* Unsolicited message; the callback must not block */
+
+  if (priv->notify_cb != NULL)
+    {
+      priv->notify_cb(priv->notify_arg, msg->wd, msg->name, msg->mask);
+    }
+
+  return 0;
+}
+#endif
+
 static FAR void *rpmsgfs_get_tx_payload_buffer(FAR struct rpmsgfs_s *priv,
                                                FAR uint32_t *len)
 {
@@ -395,7 +430,8 @@ static int rpmsgfs_ept_cb(FAR struct rpmsg_endpoint *ept,
   FAR struct rpmsgfs_header_s *header = data;
   uint32_t command = header->command;
 
-  if (command < nitems(g_rpmsgfs_handler))
+  if (command < nitems(g_rpmsgfs_handler) &&
+      g_rpmsgfs_handler[command] != NULL)
     {
       return g_rpmsgfs_handler[command](ept, data, len, src, priv);
     }
@@ -1189,3 +1225,76 @@ int rpmsgfs_client_lstat(FAR void *handle, FAR const 
char *path,
   return rpmsgfs_send_recv(priv, RPMSGFS_LSTAT, false,
           (struct rpmsgfs_header_s *)msg, len, buf);
 }
+
+#ifdef CONFIG_FS_NOTIFY
+int rpmsgfs_client_watch_add(FAR void *handle, FAR const char *path,
+                             uint32_t mask)
+{
+  FAR struct rpmsgfs_s *priv = handle;
+  FAR struct rpmsgfs_watch_s *msg;
+  uint32_t space;
+  size_t len;
+
+  len = sizeof(*msg) + strlen(path) + 1;
+
+  msg = rpmsgfs_get_tx_payload_buffer(priv, &space);
+  if (!msg)
+    {
+      return -ENOMEM;
+    }
+
+  DEBUGASSERT(len <= space);
+
+  msg->action = NOTIFY_WATCH_ADD;
+  msg->wd     = -1;
+  msg->mask   = mask;
+  strlcpy(msg->pathname, path, space - sizeof(*msg));
+
+  /* The response carries the server watch descriptor in result */
+
+  return rpmsgfs_send_recv(priv, RPMSGFS_WATCH, false,
+          (FAR struct rpmsgfs_header_s *)msg, len, NULL);
+}
+
+int rpmsgfs_client_watch_rm(FAR void *handle, int wd)
+{
+  FAR struct rpmsgfs_s *priv = handle;
+  FAR struct rpmsgfs_watch_s *msg;
+  uint32_t space;
+  int ret;
+
+  msg = rpmsgfs_get_tx_payload_buffer(priv, &space);
+  if (!msg)
+    {
+      return -ENOMEM;
+    }
+
+  /* Fire and forget: no response is expected */
+
+  msg->header.command = RPMSGFS_WATCH;
+  msg->header.result  = -ENXIO;
+  msg->header.cookie  = 0;
+  msg->action         = NOTIFY_WATCH_RM;
+  msg->wd             = wd;
+  msg->mask           = 0;
+
+  ret = rpmsg_send_nocopy(&priv->ept, msg, sizeof(*msg));
+  if (ret < 0)
+    {
+      rpmsg_release_tx_buffer(&priv->ept, msg);
+      return ret;
+    }
+
+  return OK;
+}
+
+void rpmsgfs_client_set_notify(FAR void *handle,
+                               rpmsgfs_notify_t callback,
+                               FAR void *arg)
+{
+  FAR struct rpmsgfs_s *priv = handle;
+
+  priv->notify_cb  = callback;
+  priv->notify_arg = arg;
+}
+#endif
diff --git a/fs/rpmsgfs/rpmsgfs_server.c b/fs/rpmsgfs/rpmsgfs_server.c
index a420cb40497..65b4b1d158a 100644
--- a/fs/rpmsgfs/rpmsgfs_server.c
+++ b/fs/rpmsgfs/rpmsgfs_server.c
@@ -27,16 +27,16 @@
 #include <nuttx/config.h>
 
 #include <dirent.h>
-#include <stdio.h>
 #include <string.h>
-#include <fcntl.h>
 #include <assert.h>
 #include <nuttx/debug.h>
 #include <errno.h>
+#include <sys/inotify.h>
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/list.h>
 #include <nuttx/mutex.h>
+#include <nuttx/nuttx.h>
 #include <nuttx/semaphore.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/rpmsg/rpmsg.h>
@@ -45,6 +45,10 @@
 #include "fs_heap.h"
 #include "vfs/vfs.h"
 
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -62,6 +66,10 @@ struct rpmsgfs_server_s
   sem_t                 lock_drained; /* Posted when lock_waiters drains */
   bool                  lock_closing; /* ept_release waits on lock_drained */
 #endif
+#ifdef CONFIG_FS_RPMSGFS_SERVER_NOTIFY
+  struct file           notify_file;  /* In-kernel inotify instance */
+  bool                  notify_open;  /* notify_file is initialized */
+#endif
 };
 
 #if CONFIG_FS_LOCK_BUCKET_SIZE > 0
@@ -163,6 +171,9 @@ static int rpmsgfs_lstat_handler(FAR struct rpmsg_endpoint 
*ept,
                                  FAR void *data, size_t len,
                                  uint32_t src, FAR void *priv);
 #endif
+static int rpmsgfs_watch_handler(FAR struct rpmsg_endpoint *ept,
+                                 FAR void *data, size_t len,
+                                 uint32_t src, FAR void *priv);
 static bool rpmsgfs_ns_match(FAR struct rpmsg_device *rdev,
                              FAR void *priv_, FAR const char *name,
                              uint32_t dest);
@@ -210,6 +221,7 @@ static const rpmsg_ept_cb g_rpmsgfs_handler[] =
   [RPMSGFS_LINK]      = rpmsgfs_link_handler,
   [RPMSGFS_LSTAT]     = rpmsgfs_lstat_handler,
 #endif
+  [RPMSGFS_WATCH]     = rpmsgfs_watch_handler,
 };
 
 /****************************************************************************
@@ -1125,6 +1137,123 @@ static int rpmsgfs_lstat_handler(FAR struct 
rpmsg_endpoint *ept,
 }
 #endif
 
+#ifdef CONFIG_FS_RPMSGFS_SERVER_NOTIFY
+
+/****************************************************************************
+ * Name: rpmsgfs_notify_cb
+ *
+ * Description: Forward one inotify event to the client (unsolicited).
+ *   Invoked by the in-kernel inotify instance in the context that
+ *   generated the event, so it must not block: a tx buffer is taken
+ *   without waiting and the notification is dropped if none is free.
+ *
+ ****************************************************************************/
+
+static void rpmsgfs_notify_cb(FAR void *arg, int wd, uint32_t mask,
+                              FAR const char *name)
+{
+  FAR struct rpmsgfs_server_s *priv = arg;
+  FAR struct rpmsgfs_notify_s *msg;
+  uint32_t space;
+  size_t len;
+
+  len = sizeof(*msg) + (name != NULL ? strlen(name) : 0) + 1;
+
+  msg = rpmsg_get_tx_payload_buffer(&priv->ept, &space, false);
+  if (msg == NULL || len > space)
+    {
+      return;
+    }
+
+  msg->header.command = RPMSGFS_NOTIFY;
+  msg->header.result  = 0;
+  msg->header.cookie  = 0;
+  msg->wd             = wd;
+  msg->mask           = mask;
+  strlcpy(msg->name, name != NULL ? name : "", space - sizeof(*msg));
+
+  if (rpmsg_send_nocopy(&priv->ept, msg, len) < 0)
+    {
+      rpmsg_release_tx_buffer(&priv->ept, msg);
+    }
+}
+
+/****************************************************************************
+ * Name: rpmsgfs_watch_handler
+ *
+ * Description: Add or remove a watch on the in-kernel inotify instance.
+ *   ADD is answered with the watch descriptor; RM is fire and forget.
+ *
+ ****************************************************************************/
+
+static int rpmsgfs_watch_handler(FAR struct rpmsg_endpoint *ept,
+                                 FAR void *data, size_t len,
+                                 uint32_t src, FAR void *priv_)
+{
+  FAR struct rpmsgfs_server_s *priv = priv_;
+  FAR struct rpmsgfs_watch_s *msg = data;
+  int ret;
+
+  if (msg->action != NOTIFY_WATCH_ADD)
+    {
+      nxmutex_lock(&priv->lock);
+      if (priv->notify_open)
+        {
+          file_notify_rm_watch(&priv->notify_file, msg->wd);
+        }
+
+      nxmutex_unlock(&priv->lock);
+      return 0;
+    }
+
+  nxmutex_lock(&priv->lock);
+  if (!priv->notify_open)
+    {
+      ret = file_notify_open(&priv->notify_file, rpmsgfs_notify_cb, priv);
+      if (ret < 0)
+        {
+          nxmutex_unlock(&priv->lock);
+          msg->header.result = ret;
+          return rpmsg_send(ept, msg, len);
+        }
+
+      priv->notify_open = true;
+    }
+
+  ret = file_notify_add_watch(&priv->notify_file, msg->pathname,
+                              msg->mask & IN_ALL_EVENTS);
+  nxmutex_unlock(&priv->lock);
+
+  msg->header.result = ret;
+  return rpmsg_send(ept, msg, len);
+}
+
+#else
+
+/****************************************************************************
+ * Name: rpmsgfs_watch_handler
+ *
+ * Description: Without inotify forwarding, answer ADD with -ENOSYS so that
+ *   the client keeps a local-only watch.  RM expects no reply.
+ *
+ ****************************************************************************/
+
+static int rpmsgfs_watch_handler(FAR struct rpmsg_endpoint *ept,
+                                 FAR void *data, size_t len,
+                                 uint32_t src, FAR void *priv)
+{
+  FAR struct rpmsgfs_watch_s *msg = data;
+
+  if (msg->action != NOTIFY_WATCH_ADD)
+    {
+      return 0;
+    }
+
+  msg->header.result = -ENOSYS;
+  return rpmsg_send(ept, msg, len);
+}
+#endif /* CONFIG_FS_RPMSGFS_SERVER_NOTIFY */
+
 static bool rpmsgfs_ns_match(FAR struct rpmsg_device *rdev,
                              FAR void *priv_, FAR const char *name,
                              uint32_t dest)
@@ -1173,6 +1302,13 @@ static void rpmsgfs_ept_release(FAR struct 
rpmsg_endpoint *ept)
   nxsem_destroy(&priv->lock_drained);
 #endif
 
+#ifdef CONFIG_FS_RPMSGFS_SERVER_NOTIFY
+  if (priv->notify_open)
+    {
+      file_notify_close(&priv->notify_file);
+    }
+#endif
+
   for (i = 0; i < priv->file_rows; i++)
     {
       for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++)
@@ -1243,7 +1379,8 @@ static int rpmsgfs_server_ept_cb(FAR struct 
rpmsg_endpoint *ept,
   uint32_t command = header->command;
   int ret;
 
-  if (command >= nitems(g_rpmsgfs_handler))
+  if (command >= nitems(g_rpmsgfs_handler) ||
+      g_rpmsgfs_handler[command] == NULL)
     {
       return -EINVAL;
     }

Reply via email to