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 3b8c772d5c51b72d287c9d8010147f643f2cf993 Author: xiaoxiang <[email protected]> AuthorDate: Sat Feb 28 14:33:12 2026 +0800 fs/userfs: add syncfs and link operations support Add support for the following mountpt_operations that were missing from the UserFS implementation: - syncfs: sync the entire file system - link: create a hard link (under CONFIG_FS_LINKS) - symlink: create a symbolic link (under CONFIG_FS_LINKS) - readlink: read the target of a symbolic link (under CONFIG_FS_LINKS) - lstat: get file status without following symlinks (under CONFIG_FS_LINKS) This includes changes to the protocol header (userfs.h) with new request/response enums and structures, the kernel-side proxy (fs_userfs.c), and the user-space dispatcher (lib_userfs.c). The daemon methods are optional: a daemon without one answers -ENOSYS, and lstat then falls back to stat. Signed-off-by: Xiang Xiao <[email protected]> Signed-off-by: jingfei <[email protected]> --- fs/userfs/fs_userfs.c | 481 +++++++++++++++++++++++++++++++++++++++++- include/nuttx/fs/userfs.h | 102 +++++++++ libs/libc/userfs/lib_userfs.c | 299 ++++++++++++++++++++++++++ 3 files changed, 877 insertions(+), 5 deletions(-) diff --git a/fs/userfs/fs_userfs.c b/fs/userfs/fs_userfs.c index 8295af02126..2932563c807 100644 --- a/fs/userfs/fs_userfs.c +++ b/fs/userfs/fs_userfs.c @@ -155,6 +155,17 @@ static int userfs_fchstat(FAR const struct file *filep, static int userfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, FAR const struct stat *buf, int flags); +static int userfs_syncfs(FAR struct inode *mountpt); +#ifdef CONFIG_FS_LINKS +static int userfs_link(FAR struct inode *mountpt, + FAR const char *relpath1, FAR const char *relpath2); +static int userfs_symlink(FAR struct inode *mountpt, + FAR const char *path1, FAR const char *relpath2); +static ssize_t userfs_readlink(FAR struct inode *mountpt, + FAR const char *relpath, FAR char *buf, size_t bufsize); +static int userfs_lstat(FAR struct inode *mountpt, + FAR const char *relpath, FAR struct stat *buf); +#endif /**************************************************************************** * Public Data @@ -199,14 +210,14 @@ const struct mountpt_operations g_userfs_operations = userfs_rename, /* rename */ userfs_stat, /* stat */ userfs_chstat, /* chstat */ - NULL, /* syncfs */ + userfs_syncfs, /* syncfs */ NULL, /* ioctldir */ NULL, /* permission */ #ifdef CONFIG_FS_LINKS - NULL, /* link */ - NULL, /* symlink */ - NULL, /* readlink */ - NULL, /* lstat */ + userfs_link, /* link */ + userfs_symlink, /* symlink */ + userfs_readlink, /* readlink */ + userfs_lstat, /* lstat */ #endif #ifdef CONFIG_FS_NOTIFY userfs_notify, /* notify */ @@ -2583,6 +2594,466 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, return ret; } +/**************************************************************************** + * Name: userfs_syncfs + * + * Description: + * Sync the file system + * + ****************************************************************************/ + +static int userfs_syncfs(FAR struct inode *mountpt) +{ + FAR struct userfs_state_s *priv; + FAR struct userfs_syncfs_request_s *req; + FAR struct userfs_syncfs_response_s *resp; + ssize_t nsent; + ssize_t nrecvd; + int ret; + + DEBUGASSERT(mountpt != NULL && + mountpt->i_private != NULL); + priv = mountpt->i_private; + + /* 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_syncfs_request_s *)priv->iobuffer; + req->req = USERFS_REQ_SYNCFS; + + nsent = psock_sendto(&priv->psock, priv->iobuffer, + sizeof(struct userfs_syncfs_request_s), 0, + (FAR struct sockaddr *)&priv->server, + sizeof(struct sockaddr_in)); + if (nsent < 0) + { + ferr("ERROR: psock_sendto failed: %zd\n", nsent); + nxmutex_unlock(&priv->lock); + return (int)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 (int)nrecvd; + } + + if (nrecvd != sizeof(struct userfs_syncfs_response_s)) + { + ferr("ERROR: Response size incorrect: %zd\n", nrecvd); + return -EIO; + } + + resp = (FAR struct userfs_syncfs_response_s *)priv->iobuffer; + if (resp->resp != USERFS_RESP_SYNCFS) + { + ferr("ERROR: Incorrect response: %u\n", resp->resp); + return -EIO; + } + + return resp->ret; +} + +#ifdef CONFIG_FS_LINKS + +/**************************************************************************** + * Name: userfs_link + * + * Description: + * Create a hard link + * + ****************************************************************************/ + +static int userfs_link(FAR struct inode *mountpt, + FAR const char *relpath1, + FAR const char *relpath2) +{ + FAR struct userfs_state_s *priv; + FAR struct userfs_link_request_s *req; + FAR struct userfs_link_response_s *resp; + int path1len; + int path2len; + ssize_t nsent; + ssize_t nrecvd; + int ret; + + DEBUGASSERT(mountpt != NULL && + mountpt->i_private != NULL); + priv = mountpt->i_private; + + /* Check the path lengths */ + + DEBUGASSERT(relpath1 != NULL && relpath2 != NULL); + path1len = strlen(relpath1) + 1; + path2len = strlen(relpath2) + 1; + + if ((path1len + path2len) > 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_link_request_s *)priv->iobuffer; + req->req = USERFS_REQ_LINK; + req->newoffset = path1len; + + strlcpy(req->relpath1, relpath1, path1len); + strlcpy(&req->relpath1[path1len], relpath2, path2len); + + nsent = psock_sendto(&priv->psock, priv->iobuffer, + SIZEOF_USERFS_LINK_REQUEST_S(path1len, path2len), + 0, (FAR struct sockaddr *)&priv->server, + sizeof(struct sockaddr_in)); + if (nsent < 0) + { + ferr("ERROR: psock_sendto failed: %zd\n", nsent); + nxmutex_unlock(&priv->lock); + return (int)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 (int)nrecvd; + } + + if (nrecvd != sizeof(struct userfs_link_response_s)) + { + ferr("ERROR: Response size incorrect: %zd\n", nrecvd); + return -EIO; + } + + resp = (FAR struct userfs_link_response_s *)priv->iobuffer; + if (resp->resp != USERFS_RESP_LINK) + { + ferr("ERROR: Incorrect response: %u\n", resp->resp); + return -EIO; + } + + return resp->ret; +} + +/**************************************************************************** + * Name: userfs_symlink + * + * Description: + * Create a symbolic link + * + ****************************************************************************/ + +static int userfs_symlink(FAR struct inode *mountpt, + FAR const char *path1, + FAR const char *relpath2) +{ + FAR struct userfs_state_s *priv; + FAR struct userfs_symlink_request_s *req; + FAR struct userfs_symlink_response_s *resp; + int path1len; + int path2len; + ssize_t nsent; + ssize_t nrecvd; + int ret; + + DEBUGASSERT(mountpt != NULL && + mountpt->i_private != NULL); + priv = mountpt->i_private; + + /* Check the path lengths */ + + DEBUGASSERT(path1 != NULL && relpath2 != NULL); + path1len = strlen(path1) + 1; + path2len = strlen(relpath2) + 1; + + if ((path1len + path2len) > 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_symlink_request_s *)priv->iobuffer; + req->req = USERFS_REQ_SYMLINK; + req->newoffset = path1len; + + strlcpy(req->path1, path1, path1len); + strlcpy(&req->path1[path1len], relpath2, path2len); + + nsent = psock_sendto(&priv->psock, priv->iobuffer, + SIZEOF_USERFS_SYMLINK_REQUEST_S(path1len, path2len), + 0, (FAR struct sockaddr *)&priv->server, + sizeof(struct sockaddr_in)); + if (nsent < 0) + { + ferr("ERROR: psock_sendto failed: %zd\n", nsent); + nxmutex_unlock(&priv->lock); + return (int)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 (int)nrecvd; + } + + if (nrecvd != sizeof(struct userfs_symlink_response_s)) + { + ferr("ERROR: Response size incorrect: %zd\n", nrecvd); + return -EIO; + } + + resp = (FAR struct userfs_symlink_response_s *)priv->iobuffer; + if (resp->resp != USERFS_RESP_SYMLINK) + { + ferr("ERROR: Incorrect response: %u\n", resp->resp); + return -EIO; + } + + return resp->ret; +} + +/**************************************************************************** + * Name: userfs_readlink + * + * Description: + * Read the target of a symbolic link + * + ****************************************************************************/ + +static ssize_t userfs_readlink(FAR struct inode *mountpt, + FAR const char *relpath, + FAR char *buf, size_t bufsize) +{ + FAR struct userfs_state_s *priv; + FAR struct userfs_readlink_request_s *req; + FAR struct userfs_readlink_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); + 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_readlink_request_s *)priv->iobuffer; + req->req = USERFS_REQ_READLINK; + req->bufsize = bufsize; + + strlcpy(req->relpath, relpath, priv->mxwrite); + + nsent = psock_sendto(&priv->psock, priv->iobuffer, + SIZEOF_USERFS_READLINK_REQUEST_S(pathlen + 1), 0, + (FAR struct sockaddr *)&priv->server, + sizeof(struct sockaddr_in)); + 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_USERFS_READLINK_RESPONSE_S(0)) + { + ferr("ERROR: Response too small: %zd\n", nrecvd); + return -EIO; + } + + resp = (FAR struct userfs_readlink_response_s *)priv->iobuffer; + if (resp->resp != USERFS_RESP_READLINK) + { + ferr("ERROR: Incorrect response: %u\n", resp->resp); + return -EIO; + } + + if (resp->nread < 0) + { + return resp->nread; + } + + if (resp->nread > bufsize) + { + ferr("ERROR: Response size too large: %zd\n", nrecvd); + return -EIO; + } + + /* Copy the link target data to the user buffer */ + + memcpy(buf, resp->rddata, resp->nread); + return resp->nread; +} + +/**************************************************************************** + * Name: userfs_lstat + * + * Description: + * Return information about a file or directory (not following symlinks) + * + ****************************************************************************/ + +static int userfs_lstat(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct stat *buf) +{ + FAR struct userfs_state_s *priv; + FAR struct userfs_lstat_request_s *req; + FAR struct userfs_lstat_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); + 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_lstat_request_s *)priv->iobuffer; + req->req = USERFS_REQ_LSTAT; + + strlcpy(req->relpath, relpath, priv->mxwrite); + + nsent = psock_sendto(&priv->psock, priv->iobuffer, + SIZEOF_USERFS_LSTAT_REQUEST_S(pathlen + 1), 0, + (FAR struct sockaddr *)&priv->server, + sizeof(struct sockaddr_in)); + if (nsent < 0) + { + ferr("ERROR: psock_sendto failed: %zd\n", nsent); + nxmutex_unlock(&priv->lock); + return (int)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 (int)nrecvd; + } + + if (nrecvd != sizeof(struct userfs_lstat_response_s)) + { + ferr("ERROR: Response size incorrect: %zd\n", nrecvd); + return -EIO; + } + + resp = (FAR struct userfs_lstat_response_s *)priv->iobuffer; + if (resp->resp != USERFS_RESP_LSTAT) + { + ferr("ERROR: Incorrect response: %u\n", resp->resp); + return -EIO; + } + + /* A daemon without lstat answers -ENOSYS: use stat instead */ + + ret = resp->ret; + if (ret == -ENOSYS) + { + return userfs_stat(mountpt, relpath, buf); + } + + DEBUGASSERT(buf != NULL); + memcpy(buf, &resp->buf, sizeof(struct stat)); + return ret; +} + +#endif /* CONFIG_FS_LINKS */ + /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/include/nuttx/fs/userfs.h b/include/nuttx/fs/userfs.h index 1d436941f01..029132f250f 100644 --- a/include/nuttx/fs/userfs.h +++ b/include/nuttx/fs/userfs.h @@ -144,6 +144,13 @@ enum userfs_req_e USERFS_REQ_CHSTAT, USERFS_REQ_GETLK, USERFS_REQ_SETLK, + USERFS_REQ_SYNCFS, +#ifdef CONFIG_FS_LINKS + USERFS_REQ_LINK, + USERFS_REQ_SYMLINK, + USERFS_REQ_READLINK, + USERFS_REQ_LSTAT, +#endif #ifdef CONFIG_FS_NOTIFY USERFS_REQ_NOTIFY #endif @@ -180,6 +187,13 @@ enum userfs_resp_e USERFS_RESP_CHSTAT, USERFS_RESP_GETLK, USERFS_RESP_SETLK, + USERFS_RESP_SYNCFS, +#ifdef CONFIG_FS_LINKS + USERFS_RESP_LINK, + USERFS_RESP_SYMLINK, + USERFS_RESP_READLINK, + USERFS_RESP_LSTAT, +#endif #ifdef CONFIG_FS_NOTIFY USERFS_RESP_NOTIFY #endif @@ -242,6 +256,7 @@ struct userfs_operations_s 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. + * Without lstat the OS uses stat. * notify reports the watch life cycle; events are injected back with * userfs_notify_event(). */ @@ -250,6 +265,17 @@ struct userfs_operations_s FAR struct flock *fl); int (*setlk)(FAR void *volinfo, FAR void *openinfo, FAR struct flock *fl); + int (*syncfs)(FAR void *volinfo); +#ifdef CONFIG_FS_LINKS + int (*link)(FAR void *volinfo, FAR const char *relpath1, + FAR const char *relpath2); + int (*symlink)(FAR void *volinfo, FAR const char *path1, + FAR const char *relpath2); + ssize_t (*readlink)(FAR void *volinfo, FAR const char *relpath, + FAR char *buf, size_t bufsize); + int (*lstat)(FAR void *volinfo, FAR const char *relpath, + FAR struct stat *buf); +#endif #ifdef CONFIG_FS_NOTIFY int (*notify)(FAR void *volinfo, int wd, FAR const char *relpath, int action, uint32_t mask); @@ -613,6 +639,82 @@ struct userfs_notify_response_s }; #endif /* CONFIG_FS_NOTIFY */ +struct userfs_syncfs_request_s +{ + uint8_t req; /* Must be USERFS_REQ_SYNCFS */ +}; + +struct userfs_syncfs_response_s +{ + uint8_t resp; /* Must be USERFS_RESP_SYNCFS */ + int ret; /* Result of the operation */ +}; + +#ifdef CONFIG_FS_LINKS +struct userfs_link_request_s +{ + uint8_t req; /* Must be USERFS_REQ_LINK */ + uint16_t newoffset; /* Offset from path1 to path2 */ + char relpath1[1]; /* Source relative path */ +}; + +#define SIZEOF_USERFS_LINK_REQUEST_S(o,n) (sizeof(struct userfs_link_request_s) + (o) + (n)) + +struct userfs_link_response_s +{ + uint8_t resp; /* Must be USERFS_RESP_LINK */ + int ret; /* Result of the operation */ +}; + +struct userfs_symlink_request_s +{ + uint8_t req; /* Must be USERFS_REQ_SYMLINK */ + uint16_t newoffset; /* Offset from path1 to relpath2 */ + char path1[1]; /* Target path */ +}; + +#define SIZEOF_USERFS_SYMLINK_REQUEST_S(o,n) (sizeof(struct userfs_symlink_request_s) + (o) + (n)) + +struct userfs_symlink_response_s +{ + uint8_t resp; /* Must be USERFS_RESP_SYMLINK */ + int ret; /* Result of the operation */ +}; + +struct userfs_readlink_request_s +{ + uint8_t req; /* Must be USERFS_REQ_READLINK */ + size_t bufsize; /* Maximum number of bytes to read */ + char relpath[1]; /* Relative path to the symlink */ +}; + +#define SIZEOF_USERFS_READLINK_REQUEST_S(n) (sizeof(struct userfs_readlink_request_s) + (n) - 1) + +struct userfs_readlink_response_s +{ + uint8_t resp; /* Must be USERFS_RESP_READLINK */ + ssize_t nread; /* Result of the operation */ + char rddata[1]; /* Link target data. Actual size is nread */ +}; + +#define SIZEOF_USERFS_READLINK_RESPONSE_S(n) (sizeof(struct userfs_readlink_response_s) + (n) - 1) + +struct userfs_lstat_request_s +{ + uint8_t req; /* Must be USERFS_REQ_LSTAT */ + char relpath[1]; /* Relative path to the entry to query */ +}; + +#define SIZEOF_USERFS_LSTAT_REQUEST_S(n) (sizeof(struct userfs_lstat_request_s) + (n) - 1) + +struct userfs_lstat_response_s +{ + uint8_t resp; /* Must be USERFS_RESP_LSTAT */ + int ret; /* Result of the operation */ + struct stat buf; /* Returned status */ +}; +#endif /* CONFIG_FS_LINKS */ + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/libs/libc/userfs/lib_userfs.c b/libs/libc/userfs/lib_userfs.c index 7a99467fe54..76d727b2fc0 100644 --- a/libs/libc/userfs/lib_userfs.c +++ b/libs/libc/userfs/lib_userfs.c @@ -1017,6 +1017,273 @@ static inline int userfs_notify_dispatch(FAR struct userfs_info_s *info, } #endif /* CONFIG_FS_NOTIFY */ +static inline int userfs_syncfs_dispatch(FAR struct userfs_info_s *info, + FAR struct userfs_syncfs_request_s *req, size_t reqlen) +{ + struct userfs_syncfs_response_s resp; + ssize_t nsent; + + /* Verify the request size */ + + if (reqlen != sizeof(struct userfs_syncfs_request_s)) + { + return -EINVAL; + } + + /* Dispatch the request */ + + if (info->userops->syncfs == NULL) + { + resp.ret = -ENOSYS; + } + else + { + resp.ret = info->userops->syncfs(info->volinfo); + } + + /* Send the response */ + + resp.resp = USERFS_RESP_SYNCFS; + nsent = sendto(info->sockfd, &resp, + sizeof(struct userfs_syncfs_response_s), + 0, (FAR struct sockaddr *)&info->client, + sizeof(struct sockaddr_in)); + return nsent < 0 ? nsent : OK; +} + +#ifdef CONFIG_FS_LINKS +static inline int userfs_link_dispatch(FAR struct userfs_info_s *info, + FAR struct userfs_link_request_s *req, size_t reqlen) +{ + struct userfs_link_response_s resp; + FAR char *relpath2; + unsigned int newoffset; + int path1len; + int path2len; + size_t expected; + ssize_t nsent; + + /* Verify the request size */ + + if (reqlen < SIZEOF_USERFS_LINK_REQUEST_S(0, 0)) + { + return -EINVAL; + } + + path1len = strlen(req->relpath1); + newoffset = req->newoffset; + + if (path1len >= newoffset) + { + return -EINVAL; + } + + relpath2 = &req->relpath1[newoffset]; + path2len = strlen(relpath2); + + if ((path2len + newoffset) > info->mxwrite) + { + return -EINVAL; + } + + expected = SIZEOF_USERFS_LINK_REQUEST_S(newoffset, path2len); + if (expected >= reqlen) + { + return -EINVAL; + } + + /* Dispatch the request */ + + if (info->userops->link == NULL) + { + resp.ret = -ENOSYS; + } + else + { + resp.ret = info->userops->link(info->volinfo, req->relpath1, relpath2); + } + + /* Send the response */ + + resp.resp = USERFS_RESP_LINK; + nsent = sendto(info->sockfd, &resp, + sizeof(struct userfs_link_response_s), + 0, (FAR struct sockaddr *)&info->client, + sizeof(struct sockaddr_in)); + return nsent < 0 ? nsent : OK; +} + +static inline int userfs_symlink_dispatch(FAR struct userfs_info_s *info, + FAR struct userfs_symlink_request_s *req, size_t reqlen) +{ + struct userfs_symlink_response_s resp; + FAR char *relpath2; + unsigned int newoffset; + int path1len; + int path2len; + size_t expected; + ssize_t nsent; + + /* Verify the request size */ + + if (reqlen < SIZEOF_USERFS_SYMLINK_REQUEST_S(0, 0)) + { + return -EINVAL; + } + + path1len = strlen(req->path1); + newoffset = req->newoffset; + + if (path1len >= newoffset) + { + return -EINVAL; + } + + relpath2 = &req->path1[newoffset]; + path2len = strlen(relpath2); + + if ((path2len + newoffset) > info->mxwrite) + { + return -EINVAL; + } + + expected = SIZEOF_USERFS_SYMLINK_REQUEST_S(newoffset, path2len); + if (expected >= reqlen) + { + return -EINVAL; + } + + /* Dispatch the request */ + + if (info->userops->symlink == NULL) + { + resp.ret = -ENOSYS; + } + else + { + resp.ret = info->userops->symlink(info->volinfo, req->path1, relpath2); + } + + /* Send the response */ + + resp.resp = USERFS_RESP_SYMLINK; + nsent = sendto(info->sockfd, &resp, + sizeof(struct userfs_symlink_response_s), + 0, (FAR struct sockaddr *)&info->client, + sizeof(struct sockaddr_in)); + return nsent < 0 ? nsent : OK; +} + +static inline int userfs_readlink_dispatch(FAR struct userfs_info_s *info, + FAR struct userfs_readlink_request_s *req, size_t reqlen) +{ + FAR struct userfs_readlink_response_s *resp; + int pathlen; + size_t readlen; + size_t resplen; + size_t expected; + ssize_t nsent; + + /* Verify the request size */ + + if (reqlen < SIZEOF_USERFS_READLINK_REQUEST_S(0)) + { + return -EINVAL; + } + + pathlen = strlen(req->relpath); + if (pathlen > info->mxwrite) + { + return -EINVAL; + } + + expected = SIZEOF_USERFS_READLINK_REQUEST_S(pathlen); + if (expected >= reqlen) + { + return -EINVAL; + } + + /* Dispatch the request */ + + readlen = req->bufsize; + if (readlen > info->mxwrite) + { + readlen = info->mxwrite; + } + + resp = (FAR struct userfs_readlink_response_s *)info->iobuffer; + + if (info->userops->readlink == NULL) + { + resp->nread = -ENOSYS; + } + else + { + resp->nread = info->userops->readlink(info->volinfo, req->relpath, + resp->rddata, readlen); + } + + /* Send the response */ + + resp->resp = USERFS_RESP_READLINK; + resplen = SIZEOF_USERFS_READLINK_RESPONSE_S( + resp->nread > 0 ? resp->nread : 0); + nsent = sendto(info->sockfd, resp, resplen, 0, + (FAR struct sockaddr *)&info->client, + sizeof(struct sockaddr_in)); + return nsent < 0 ? nsent : OK; +} + +static inline int userfs_lstat_dispatch(FAR struct userfs_info_s *info, + FAR struct userfs_lstat_request_s *req, size_t reqlen) +{ + struct userfs_lstat_response_s resp; + int pathlen; + size_t expected; + ssize_t nsent; + + /* Verify the request size */ + + if (reqlen < SIZEOF_USERFS_LSTAT_REQUEST_S(0)) + { + return -EINVAL; + } + + pathlen = strlen(req->relpath); + if (pathlen > info->mxwrite) + { + return -EINVAL; + } + + expected = SIZEOF_USERFS_LSTAT_REQUEST_S(pathlen); + if (expected >= reqlen) + { + return -EINVAL; + } + + /* Dispatch the request */ + + if (info->userops->lstat == NULL) + { + resp.ret = -ENOSYS; + } + else + { + resp.ret = info->userops->lstat(info->volinfo, req->relpath, + &resp.buf); + } + + /* Send the response */ + + resp.resp = USERFS_RESP_LSTAT; + nsent = sendto(info->sockfd, &resp, + sizeof(struct userfs_lstat_response_s), + 0, (FAR struct sockaddr *)&info->client, + sizeof(struct sockaddr_in)); + return nsent < 0 ? nsent : OK; +} +#endif /* CONFIG_FS_LINKS */ + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -1308,6 +1575,38 @@ int userfs_run(FAR const char *mountpt, nread); break; + case USERFS_REQ_SYNCFS: + ret = userfs_syncfs_dispatch(info, + (FAR struct userfs_syncfs_request_s *)info->iobuffer, + nread); + break; + +#ifdef CONFIG_FS_LINKS + case USERFS_REQ_LINK: + ret = userfs_link_dispatch(info, + (FAR struct userfs_link_request_s *)info->iobuffer, + nread); + break; + + case USERFS_REQ_SYMLINK: + ret = userfs_symlink_dispatch(info, + (FAR struct userfs_symlink_request_s *)info->iobuffer, + nread); + break; + + case USERFS_REQ_READLINK: + ret = userfs_readlink_dispatch(info, + (FAR struct userfs_readlink_request_s *)info->iobuffer, + nread); + break; + + case USERFS_REQ_LSTAT: + ret = userfs_lstat_dispatch(info, + (FAR struct userfs_lstat_request_s *)info->iobuffer, + nread); + break; +#endif + #ifdef CONFIG_FS_NOTIFY case USERFS_REQ_NOTIFY: ret = userfs_notify_dispatch(info,
