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

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

commit 9a57116e78f33a223e7bee126b9a1200d2c5233a
Author: p-szafonimateusz <[email protected]>
AuthorDate: Thu Jul 23 12:01:13 2026 +0200

    fs/userfs: Forward record locks and watch life cycle to the daemon
    
    fcntl(F_GETLK/F_SETLK/F_SETLKW) reaches file systems as FIOC_* ioctl
    commands, but userfs forwarded them verbatim to the daemon, which
    cannot dereference the struct flock pointer in the argument.
    
    Intercept the lock ioctls in userfs_ioctl() and marshal them over new
    USERFS_REQ_GETLK/USERFS_REQ_SETLK requests to the optional getlk/setlk
    daemon methods.  A daemon without the methods answers -ENOSYS and the
    OS local lock table is used, as before.  FIOC_SETLKW is retried on
    -EAGAIN.  Also add the optional notify method (USERFS_REQ_NOTIFY)
    reporting the inotify watch life cycle, and bump USERFS_REQ_MAXSIZE
    for the flock request.
    
    Signed-off-by: p-szafonimateusz <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/userfs/fs_userfs.c         | 229 +++++++++++++++++++++++++++++++++++++++++-
 include/nuttx/fs/userfs.h     |  69 ++++++++++++-
 libs/libc/userfs/lib_userfs.c |  88 ++++++++++++++++
 3 files changed, 380 insertions(+), 6 deletions(-)

diff --git a/fs/userfs/fs_userfs.c b/fs/userfs/fs_userfs.c
index 515a1141b9c..8295af02126 100644
--- a/fs/userfs/fs_userfs.c
+++ b/fs/userfs/fs_userfs.c
@@ -48,6 +48,7 @@
 #include <nuttx/fs/ioctl.h>
 #include <nuttx/net/net.h>
 #include <nuttx/mutex.h>
+#include <nuttx/signal.h>
 
 #include "fs_heap.h"
 
@@ -57,6 +58,10 @@
 
 #define IOBUFFER_SIZE(p) (USERFS_REQ_MAXSIZE + (p)->mxwrite)
 
+/* FIOC_SETLKW retry period (microseconds) */
+
+#define USERFS_SETLK_RETRY_USEC 20000
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -106,6 +111,10 @@ static ssize_t userfs_write(FAR struct file *filep, FAR 
const char *buffer,
 static off_t   userfs_seek(FAR struct file *filep, off_t offset, int whence);
 static int     userfs_ioctl(FAR struct file *filep, int cmd,
                  unsigned long arg);
+#ifdef CONFIG_FS_NOTIFY
+static int     userfs_notify(FAR struct inode *mountpt, int wd,
+                 FAR const char *relpath, int action, uint32_t mask);
+#endif
 
 static int     userfs_sync(FAR struct file *filep);
 static int     userfs_dup(FAR const struct file *oldp,
@@ -189,7 +198,19 @@ const struct mountpt_operations g_userfs_operations =
   userfs_rmdir,      /* rmdir */
   userfs_rename,     /* rename */
   userfs_stat,       /* stat */
-  userfs_chstat      /* chstat */
+  userfs_chstat,     /* chstat */
+  NULL,              /* syncfs */
+  NULL,              /* ioctldir */
+  NULL,              /* permission */
+#ifdef CONFIG_FS_LINKS
+  NULL,              /* link */
+  NULL,              /* symlink */
+  NULL,              /* readlink */
+  NULL,              /* lstat */
+#endif
+#ifdef CONFIG_FS_NOTIFY
+  userfs_notify,     /* notify */
+#endif
 };
 
 /****************************************************************************
@@ -661,6 +682,100 @@ static off_t userfs_seek(FAR struct file *filep, off_t 
offset, int whence)
   return off_ret;
 }
 
+/****************************************************************************
+ * Name: userfs_flock
+ ****************************************************************************/
+
+static int userfs_flock(FAR struct file *filep, int request,
+                        FAR struct flock *fl)
+{
+  FAR struct userfs_state_s *priv;
+  FAR struct userfs_flock_request_s *req;
+  FAR struct userfs_flock_response_s *resp;
+  uint8_t expected;
+  ssize_t nsent;
+  ssize_t nrecvd;
+  int ret;
+
+  DEBUGASSERT(filep->f_inode != NULL &&
+              filep->f_inode->i_private != NULL);
+  priv = filep->f_inode->i_private;
+
+  /* Get exclusive access */
+
+  ret = nxmutex_lock(&priv->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Construct and send the request (FIOC_SETLKW is sent as a
+   * non-blocking set; the caller retries on -EAGAIN).
+   */
+
+  req           = (FAR struct userfs_flock_request_s *)priv->iobuffer;
+  req->openinfo = filep->f_priv;
+  memcpy(&req->lock, fl, sizeof(req->lock));
+
+  if (request == FIOC_GETLK)
+    {
+      req->req = USERFS_REQ_GETLK;
+      expected = USERFS_RESP_GETLK;
+    }
+  else
+    {
+      req->req = USERFS_REQ_SETLK;
+      expected = USERFS_RESP_SETLK;
+    }
+
+  nsent = psock_sendto(&priv->psock, priv->iobuffer,
+                       sizeof(*req), 0,
+                       (FAR struct sockaddr *)&priv->server,
+                       sizeof(priv->server));
+  if (nsent < 0)
+    {
+      ferr("ERROR: psock_sendto failed: %zd\n", nsent);
+      nxmutex_unlock(&priv->lock);
+      return nsent;
+    }
+
+  /* Then get the response from the server */
+
+  nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
+                          0, NULL, NULL);
+  nxmutex_unlock(&priv->lock);
+
+  if (nrecvd < 0)
+    {
+      ferr("ERROR: psock_recvfrom failed: %zd\n", nrecvd);
+      return nrecvd;
+    }
+
+  if (nrecvd != sizeof(*resp))
+    {
+      ferr("ERROR: Response size incorrect: %zd\n", nrecvd);
+      return -EIO;
+    }
+
+  resp = (FAR struct userfs_flock_response_s *)priv->iobuffer;
+  if (resp->resp != expected)
+    {
+      ferr("ERROR: Incorrect response: %u\n", resp->resp);
+      return -EIO;
+    }
+
+  if (request == FIOC_GETLK && resp->ret >= 0)
+    {
+      memcpy(fl, &resp->lock, sizeof(*fl));
+    }
+
+  /* A daemon without the lock methods answers -ENOSYS: report -ENOTTY
+   * so the VFS falls back to the local lock table.
+   */
+
+  return resp->ret == -ENOSYS ? -ENOTTY : resp->ret;
+}
+
 /****************************************************************************
  * Name: userfs_ioctl
  ****************************************************************************/
@@ -676,6 +791,30 @@ static int userfs_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
 
   finfo("cmd: %d arg: %08lx\n", cmd, arg);
 
+  /* Advisory locks are marshaled explicitly: the daemon cannot
+   * dereference the struct flock pointer in arg.
+   */
+
+  if (cmd == FIOC_GETLK || cmd == FIOC_SETLK || cmd == FIOC_SETLKW)
+    {
+      FAR struct flock *fl = (FAR struct flock *)(uintptr_t)arg;
+
+      for (; ; )
+        {
+          ret = userfs_flock(filep, cmd, fl);
+          if (cmd != FIOC_SETLKW || ret != -EAGAIN)
+            {
+              return ret;
+            }
+
+          ret = nxsig_usleep(USERFS_SETLK_RETRY_USEC);
+          if (ret < 0)
+            {
+              return ret;
+            }
+        }
+    }
+
   DEBUGASSERT(
               filep->f_inode != NULL &&
               filep->f_inode->i_private != NULL);
@@ -742,6 +881,94 @@ static int userfs_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
   return ret;
 }
 
+#ifdef CONFIG_FS_NOTIFY
+
+/****************************************************************************
+ * Name: userfs_notify
+ ****************************************************************************/
+
+static int userfs_notify(FAR struct inode *mountpt, int wd,
+                         FAR const char *relpath, int action,
+                         uint32_t mask)
+{
+  FAR struct userfs_state_s *priv;
+  FAR struct userfs_notify_request_s *req;
+  FAR struct userfs_notify_response_s *resp;
+  ssize_t nsent;
+  ssize_t nrecvd;
+  int pathlen;
+  int ret;
+
+  DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
+  priv = mountpt->i_private;
+
+  /* Check the path length */
+
+  DEBUGASSERT(relpath != NULL);
+  pathlen = strlen(relpath) + 1;
+  if (pathlen > priv->mxwrite)
+    {
+      return -E2BIG;
+    }
+
+  /* Get exclusive access */
+
+  ret = nxmutex_lock(&priv->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Construct and send the request to the server */
+
+  req         = (FAR struct userfs_notify_request_s *)priv->iobuffer;
+  req->req    = USERFS_REQ_NOTIFY;
+  req->action = action;
+  req->wd     = wd;
+  req->mask   = mask;
+
+  memcpy(req->relpath, relpath, pathlen);
+
+  nsent = psock_sendto(&priv->psock, priv->iobuffer,
+                       SIZEOF_USERFS_NOTIFY_REQUEST_S(pathlen), 0,
+                       (FAR struct sockaddr *)&priv->server,
+                       sizeof(priv->server));
+  if (nsent < 0)
+    {
+      ferr("ERROR: psock_sendto failed: %zd\n", nsent);
+      nxmutex_unlock(&priv->lock);
+      return nsent;
+    }
+
+  /* Then get the response from the server */
+
+  nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
+                          0, NULL, NULL);
+  nxmutex_unlock(&priv->lock);
+
+  if (nrecvd < 0)
+    {
+      ferr("ERROR: psock_recvfrom failed: %zd\n", nrecvd);
+      return nrecvd;
+    }
+
+  if (nrecvd != sizeof(*resp))
+    {
+      ferr("ERROR: Response size incorrect: %zd\n", nrecvd);
+      return -EIO;
+    }
+
+  resp = (FAR struct userfs_notify_response_s *)priv->iobuffer;
+  if (resp->resp != USERFS_RESP_NOTIFY)
+    {
+      ferr("ERROR: Incorrect response: %u\n", resp->resp);
+      return -EIO;
+    }
+
+  return resp->ret;
+}
+#endif /* CONFIG_FS_NOTIFY */
+
 /****************************************************************************
  * Name: userfs_sync
  ****************************************************************************/
diff --git a/include/nuttx/fs/userfs.h b/include/nuttx/fs/userfs.h
index 69e56dc13b5..ade47b61593 100644
--- a/include/nuttx/fs/userfs.h
+++ b/include/nuttx/fs/userfs.h
@@ -73,6 +73,7 @@
 #include <sys/statfs.h>
 #include <sys/stat.h>
 #include <dirent.h>
+#include <fcntl.h>
 
 #include <nuttx/fs/fs.h>
 
@@ -102,11 +103,11 @@
 
 #define USERFS_SERVER_PORTBASE 0x8300
 
-/* It looks like the maximum size of a request is 16 bytes.  We will allow a
- * little more for the maximum size of a request structure.
+/* The fixed part of the largest request structure is the flock request
+ * which embeds a struct flock.  Allow a little more than that.
  */
 
-#define USERFS_REQ_MAXSIZE   (32)
+#define USERFS_REQ_MAXSIZE   (64)
 
 /****************************************************************************
  * Public Data
@@ -140,7 +141,12 @@ enum userfs_req_e
   USERFS_REQ_STAT,
   USERFS_REQ_DESTROY,
   USERFS_REQ_FCHSTAT,
-  USERFS_REQ_CHSTAT
+  USERFS_REQ_CHSTAT,
+  USERFS_REQ_GETLK,
+  USERFS_REQ_SETLK,
+#ifdef CONFIG_FS_NOTIFY
+  USERFS_REQ_NOTIFY
+#endif
 };
 
 /* This enumeration provides the type of each response returned from the
@@ -171,7 +177,12 @@ enum userfs_resp_e
   USERFS_RESP_STAT,
   USERFS_RESP_DESTROY,
   USERFS_RESP_FCHSTAT,
-  USERFS_RESP_CHSTAT
+  USERFS_RESP_CHSTAT,
+  USERFS_RESP_GETLK,
+  USERFS_RESP_SETLK,
+#ifdef CONFIG_FS_NOTIFY
+  USERFS_RESP_NOTIFY
+#endif
 };
 
 /* These structures are used by internal UserFS implementation and should not
@@ -229,6 +240,20 @@ struct userfs_operations_s
             FAR const struct stat *buf, int flags);
   int     (*chstat)(FAR void *volinfo, FAR const char *relpath,
             FAR const struct stat *buf, int flags);
+  /* Optional methods; NULL or -ENOSYS falls back to the OS defaults.
+   * setlk is always non-blocking: return -EAGAIN and the VFS retries.
+   * notify reports the watch life cycle; events are injected back with
+   * userfs_notify_event().
+   */
+
+  int     (*getlk)(FAR void *volinfo, FAR void *openinfo,
+            FAR struct flock *fl);
+  int     (*setlk)(FAR void *volinfo, FAR void *openinfo,
+            FAR struct flock *fl);
+#ifdef CONFIG_FS_NOTIFY
+  int     (*notify)(FAR void *volinfo, int wd, FAR const char *relpath,
+            int action, uint32_t mask);
+#endif
 };
 
 /* The following structures describe the header on the marshaled data sent
@@ -554,6 +579,40 @@ struct userfs_chstat_response_s
   int ret;                  /* Result of the operation */
 };
 
+struct userfs_flock_request_s
+{
+  uint8_t req;              /* USERFS_REQ_GETLK or USERFS_REQ_SETLK */
+  FAR void *openinfo;       /* File open information */
+  struct flock lock;        /* Lock description */
+};
+
+struct userfs_flock_response_s
+{
+  uint8_t resp;             /* USERFS_RESP_GETLK or USERFS_RESP_SETLK */
+  int ret;                  /* Result of the operation */
+  struct flock lock;        /* GETLK: conflicting lock or F_UNLCK */
+};
+
+#ifdef CONFIG_FS_NOTIFY
+struct userfs_notify_request_s
+{
+  uint8_t req;              /* Must be USERFS_REQ_NOTIFY */
+  uint8_t action;           /* NOTIFY_WATCH_ADD or NOTIFY_WATCH_RM */
+  int wd;                   /* RM: the handle returned by ADD */
+  uint32_t mask;            /* inotify event mask */
+  char relpath[1];          /* Relative path of the watched object */
+};
+
+#define SIZEOF_USERFS_NOTIFY_REQUEST_S(n) \
+  (sizeof(struct userfs_notify_request_s) + (n) - 1)
+
+struct userfs_notify_response_s
+{
+  uint8_t resp;             /* Must be USERFS_RESP_NOTIFY */
+  int ret;                  /* ADD: daemon watch handle or error */
+};
+#endif /* CONFIG_FS_NOTIFY */
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
diff --git a/libs/libc/userfs/lib_userfs.c b/libs/libc/userfs/lib_userfs.c
index 4feee2c5ccf..90e11f30567 100644
--- a/libs/libc/userfs/lib_userfs.c
+++ b/libs/libc/userfs/lib_userfs.c
@@ -940,6 +940,79 @@ static inline int userfs_chstat_dispatch(FAR struct 
userfs_info_s *info,
   return nsent < 0 ? nsent : OK;
 }
 
+static inline int userfs_flock_dispatch(FAR struct userfs_info_s *info,
+                   FAR struct userfs_flock_request_s *req, size_t reqlen)
+{
+  struct userfs_flock_response_s resp;
+  ssize_t nsent;
+
+  /* Verify the request size */
+
+  if (reqlen != sizeof(struct userfs_flock_request_s))
+    {
+      return -EINVAL;
+    }
+
+  /* Dispatch the request.  A daemon without the method answers -ENOSYS
+   * and the OS uses its local lock table.
+   */
+
+  resp.lock = req->lock;
+  if (req->req == USERFS_REQ_GETLK)
+    {
+      resp.resp = USERFS_RESP_GETLK;
+      resp.ret  = info->userops->getlk == NULL ? -ENOSYS :
+                  info->userops->getlk(info->volinfo, req->openinfo,
+                                       &resp.lock);
+    }
+  else
+    {
+      resp.resp = USERFS_RESP_SETLK;
+      resp.ret  = info->userops->setlk == NULL ? -ENOSYS :
+                  info->userops->setlk(info->volinfo, req->openinfo,
+                                       &resp.lock);
+    }
+
+  /* Send the response */
+
+  nsent = sendto(info->sockfd, &resp,
+                 sizeof(struct userfs_flock_response_s),
+                 0, (FAR struct sockaddr *)&info->client,
+                 sizeof(struct sockaddr_in));
+  return nsent < 0 ? nsent : OK;
+}
+
+#ifdef CONFIG_FS_NOTIFY
+static inline int userfs_notify_dispatch(FAR struct userfs_info_s *info,
+                   FAR struct userfs_notify_request_s *req, size_t reqlen)
+{
+  struct userfs_notify_response_s resp;
+  ssize_t nsent;
+
+  /* Verify the request size */
+
+  if (reqlen < SIZEOF_USERFS_NOTIFY_REQUEST_S(1))
+    {
+      return -EINVAL;
+    }
+
+  /* Dispatch the request */
+
+  resp.ret = info->userops->notify == NULL ? -ENOSYS :
+             info->userops->notify(info->volinfo, req->wd, req->relpath,
+                                   req->action, req->mask);
+
+  /* Send the response */
+
+  resp.resp = USERFS_RESP_NOTIFY;
+  nsent     = sendto(info->sockfd, &resp,
+                     sizeof(struct userfs_notify_response_s),
+                     0, (FAR struct sockaddr *)&info->client,
+                     sizeof(struct sockaddr_in));
+  return nsent < 0 ? nsent : OK;
+}
+#endif /* CONFIG_FS_NOTIFY */
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -1224,6 +1297,21 @@ int userfs_run(FAR const char *mountpt,
                    nread);
             break;
 
+          case USERFS_REQ_GETLK:
+          case USERFS_REQ_SETLK:
+            ret = userfs_flock_dispatch(info,
+                   (FAR struct userfs_flock_request_s *)info->iobuffer,
+                   nread);
+            break;
+
+#ifdef CONFIG_FS_NOTIFY
+          case USERFS_REQ_NOTIFY:
+            ret = userfs_notify_dispatch(info,
+                   (FAR struct userfs_notify_request_s *)info->iobuffer,
+                   nread);
+            break;
+#endif
+
           default:
             ferr("ERROR: Unrecognized request received: %u\n",
                  *info->iobuffer);

Reply via email to