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 4ef74d319080e8ff2a903a046f925a110314e46f Author: zhengyu16 <[email protected]> AuthorDate: Thu Nov 12 17:25:37 2026 +0800 fs/nfs: add link, symlink, readlink and lstat NFS has no link operations, so symlink(), link(), readlink() and lstat() on an NFS mount are not supported. Implement them with the NFSv3 SYMLINK, LINK and READLINK procedures; lstat() returns the attributes of the link itself. Add the new NFSv3 structure names to the nxstyle whitelist. Signed-off-by: zhengyu16 <[email protected]> Signed-off-by: jingfei <[email protected]> --- fs/nfs/nfs_mount.h | 2 + fs/nfs/nfs_proto.h | 34 +++++ fs/nfs/nfs_vfsops.c | 382 +++++++++++++++++++++++++++++++++++++++++++++++++++- fs/nfs/rpc.h | 24 ++++ tools/nxstyle.c | 4 + 5 files changed, 445 insertions(+), 1 deletion(-) diff --git a/fs/nfs/nfs_mount.h b/fs/nfs/nfs_mount.h index ad024ff469d..cca54a52b45 100644 --- a/fs/nfs/nfs_mount.h +++ b/fs/nfs/nfs_mount.h @@ -100,7 +100,9 @@ struct nfsmount struct rpc_call_setattr setattr; struct rpc_call_fs fsinfo; struct rpc_reply_write write; + struct rpc_call_symlink symlink; struct rpc_call_readlink readlink; + struct rpc_call_link link; } nm_msgbuffer; /* I/O buffer (must be a aligned to 32-bit boundaries). This buffer used diff --git a/fs/nfs/nfs_proto.h b/fs/nfs/nfs_proto.h index 1f971a2c345..165cd852696 100644 --- a/fs/nfs/nfs_proto.h +++ b/fs/nfs/nfs_proto.h @@ -556,6 +556,25 @@ struct FS3args struct file_handle fsroot; }; +/* SYMLINK structures */ + +struct SYMLINK3args +{ + struct diropargs3 where; /* Variable length */ + struct nfsv3_sattr symlink_attributes; /* Variable length */ + uint32_t target_len; /* Length of target path */ + uint32_t target_path[(NFS_MAXPATHLEN + 3) >> 2]; /* Variable length */ +}; + +struct SYMLINK3resok +{ + uint32_t handle_follows; + struct file_handle fhandle; /* Variable length */ + uint32_t attributes_follows; + struct nfs_fattr attributes; + struct wcc_data dir_wcc; +}; + /* READLINK structures */ struct READLINK3args @@ -574,4 +593,19 @@ struct READLINK3resok #define SIZEOF_READLINK3resok(n) \ (sizeof(uint32_t) + sizeof(struct nfs_fattr) + sizeof(uint32_t) + (((n) + 3) & ~3)) +/* LINK structures */ + +struct LINK3args +{ + struct file_handle file; /* Variable length */ + struct diropargs3 link; /* Variable length */ +}; + +struct LINK3resok +{ + uint32_t attributes_follow; + struct nfs_fattr file_attributes; + struct wcc_data linkdir_wcc; +}; + #endif /* __FS_NFS_NFS_PROTO_H */ diff --git a/fs/nfs/nfs_vfsops.c b/fs/nfs/nfs_vfsops.c index b16c97e9c7e..13245ad8bdb 100644 --- a/fs/nfs/nfs_vfsops.c +++ b/fs/nfs/nfs_vfsops.c @@ -68,6 +68,7 @@ #include <nuttx/fs/fs.h> #include <nuttx/fs/nfs.h> #include <nuttx/net/netconfig.h> +#include <nuttx/lib/lib.h> #include <net/if.h> #include <netinet/in.h> @@ -181,6 +182,16 @@ static int nfs_stat(FAR struct inode *mountpt, FAR const char *relpath, FAR struct stat *buf); static int nfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, FAR const struct stat *buf, int flags); +#ifdef CONFIG_FS_LINKS +static int nfs_symlink(FAR struct inode *mountpt, + FAR const char *target, FAR const char *relpath); +static ssize_t nfs_readlink(FAR struct inode *mountpt, + FAR const char *relpath, FAR char *buf, size_t bufsize); +static int nfs_link(FAR struct inode *mountpt, + FAR const char *relpath1, FAR const char *relpath2); +static int nfs_lstat(FAR struct inode *mountpt, + FAR const char *relpath, FAR struct stat *buf); +#endif /**************************************************************************** * Public Data @@ -225,7 +236,16 @@ const struct mountpt_operations g_nfs_operations = nfs_rmdir, /* rmdir */ nfs_rename, /* rename */ nfs_stat, /* stat */ - nfs_chstat /* chstat */ + nfs_chstat, /* chstat */ + NULL, /* syncfs */ + NULL, /* ioctldir */ + NULL, /* permission */ +#ifdef CONFIG_FS_LINKS + nfs_link, /* link */ + nfs_symlink, /* symlink */ + nfs_readlink, /* readlink */ + nfs_lstat /* lstat */ +#endif }; /**************************************************************************** @@ -3027,3 +3047,363 @@ errout_with_lock: nxmutex_unlock(&nmp->nm_lock); return ret; } + +#ifdef CONFIG_FS_LINKS +/**************************************************************************** + * Name: nfs_symlink + * + * Description: + * Create a symbolic link at 'relpath' pointing to 'target' + * + * Returned Value: + * 0 on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int nfs_symlink(FAR struct inode *mountpt, + FAR const char *target, FAR const char *relpath) +{ + FAR struct nfsmount *nmp; + struct file_handle fhandle; + FAR char *filename; + struct nfs_fattr fattr; + FAR uint32_t *ptr; + int namelen; + int reqlen; + int ret; + + /* Sanity checks */ + + DEBUGASSERT(mountpt && mountpt->i_private && target && relpath); + + /* Get the mountpoint private data from the inode structure */ + + nmp = mountpt->i_private; + + /* Allocate buffer for filename */ + + filename = lib_get_tempbuffer(NAME_MAX + 1); + if (filename == NULL) + { + ferr("ERROR: Failed to allocate buffer for filename\n"); + return -ENOMEM; + } + + ret = nxmutex_lock(&nmp->nm_lock); + if (ret < 0) + { + goto errout_with_filename; + } + + /* Find the parent directory of the symlink */ + + ret = nfs_finddir(nmp, relpath, &fhandle, &fattr, filename); + if (ret != OK) + { + ferr("ERROR: nfs_finddir failed: %d\n", ret); + goto errout_with_lock; + } + + /* Create the SYMLINK RPC call arguments */ + + ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.symlink.symlink; + reqlen = 0; + + /* Copy the variable length, directory file handle */ + + *ptr++ = txdr_unsigned(fhandle.length); + reqlen += sizeof(uint32_t); + + memcpy(ptr, &fhandle.handle, fhandle.length); + reqlen += uint32_alignup(fhandle.length); + ptr += uint32_increment(fhandle.length); + + /* Copy the variable-length symlink name */ + + namelen = strlen(filename); + *ptr++ = txdr_unsigned(namelen); + reqlen += sizeof(uint32_t); + + memcpy(ptr, filename, namelen); + ptr += uint32_increment(namelen); + reqlen += uint32_alignup(namelen); + + /* Set symlink attributes - mode 0777 */ + + *ptr++ = nfs_true; + *ptr++ = txdr_unsigned(0777); + reqlen += 2 * sizeof(uint32_t); + + /* Other attributes: don't set */ + + *ptr++ = nfs_false; + *ptr++ = nfs_false; + *ptr++ = nfs_false; + *ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE); + *ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE); + reqlen += 5 * sizeof(uint32_t); + + /* Copy the target path */ + + namelen = strlen(target); + *ptr++ = txdr_unsigned(namelen); + reqlen += sizeof(uint32_t); + + memcpy(ptr, target, namelen); + reqlen += uint32_alignup(namelen); + + /* Perform the SYMLINK RPC */ + + nfs_statistics(NFSPROC_SYMLINK); + ret = nfs_request(nmp, NFSPROC_SYMLINK, + &nmp->nm_msgbuffer.symlink, reqlen, + nmp->nm_iobuffer, nmp->nm_buflen); + +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); + +errout_with_filename: + lib_put_tempbuffer(filename); + return ret; +} + +/**************************************************************************** + * Name: nfs_readlink + * + * Description: + * Read the target of a symbolic link + * + * Returned Value: + * The number of bytes placed in the buffer on success; a negated errno + * value on failure. + * + ****************************************************************************/ + +static ssize_t nfs_readlink(FAR struct inode *mountpt, + FAR const char *relpath, + FAR char *buf, size_t bufsize) +{ + FAR struct nfsmount *nmp; + struct file_handle fhandle; + struct nfs_fattr attributes; + int ret; + + /* Sanity checks */ + + DEBUGASSERT(mountpt && mountpt->i_private); + + /* Get the mountpoint private data from the inode structure */ + + nmp = mountpt->i_private; + DEBUGASSERT(nmp && buf); + + ret = nxmutex_lock(&nmp->nm_lock); + if (ret < 0) + { + return ret; + } + + /* Find the symlink (don't follow it) */ + + ret = nfs_findnode(nmp, relpath, &fhandle, &attributes, NULL, true); + if (ret != OK) + { + ferr("ERROR: nfs_findnode failed: %d\n", ret); + goto errout_with_lock; + } + + /* Read the symlink target using the internal helper */ + + ret = nfs_readlink_internal(nmp, &fhandle, buf, bufsize); + +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); + return ret; +} + +/**************************************************************************** + * Name: nfs_link + * + * Description: + * Create a hard link at 'relpath2' pointing to 'relpath1' + * + * Returned Value: + * 0 on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int nfs_link(FAR struct inode *mountpt, + FAR const char *relpath1, FAR const char *relpath2) +{ + FAR struct nfsmount *nmp; + struct file_handle target_fhandle; + struct file_handle link_fhandle; + struct nfs_fattr fattr; + FAR char *filename; + FAR uint32_t *ptr; + int namelen; + int reqlen; + int ret; + + /* Sanity checks */ + + DEBUGASSERT(mountpt && mountpt->i_private); + + /* Get the mountpoint private data from the inode structure */ + + nmp = mountpt->i_private; + DEBUGASSERT(nmp); + + /* Allocate buffer for filename */ + + filename = lib_get_tempbuffer(NAME_MAX + 1); + if (filename == NULL) + { + ferr("ERROR: Failed to allocate buffer for filename\n"); + return -ENOMEM; + } + + ret = nxmutex_lock(&nmp->nm_lock); + if (ret < 0) + { + goto errout_with_filename; + } + + /* Find the target file */ + + ret = nfs_findnode(nmp, relpath1, &target_fhandle, &fattr, NULL, false); + if (ret != OK) + { + ferr("ERROR: nfs_findnode (relpath1) failed: %d\n", ret); + goto errout_with_lock; + } + + /* Find the parent directory of the link */ + + ret = nfs_finddir(nmp, relpath2, &link_fhandle, &fattr, filename); + if (ret != OK) + { + ferr("ERROR: nfs_finddir (relpath2) failed: %d\n", ret); + goto errout_with_lock; + } + + /* Create the LINK RPC call arguments */ + + ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.link.link; + reqlen = 0; + + /* Copy the target file handle */ + + *ptr++ = txdr_unsigned(target_fhandle.length); + reqlen += sizeof(uint32_t); + + memcpy(ptr, &target_fhandle.handle, target_fhandle.length); + reqlen += uint32_alignup(target_fhandle.length); + ptr += uint32_increment(target_fhandle.length); + + /* Copy the link directory file handle */ + + *ptr++ = txdr_unsigned(link_fhandle.length); + reqlen += sizeof(uint32_t); + + memcpy(ptr, &link_fhandle.handle, link_fhandle.length); + reqlen += uint32_alignup(link_fhandle.length); + ptr += uint32_increment(link_fhandle.length); + + /* Copy the link name */ + + namelen = strlen(filename); + *ptr++ = txdr_unsigned(namelen); + reqlen += sizeof(uint32_t); + + memcpy(ptr, filename, namelen); + reqlen += uint32_alignup(namelen); + + /* Perform the LINK RPC */ + + nfs_statistics(NFSPROC_LINK); + ret = nfs_request(nmp, NFSPROC_LINK, + &nmp->nm_msgbuffer.link, reqlen, + nmp->nm_iobuffer, nmp->nm_buflen); + +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); + +errout_with_filename: + lib_put_tempbuffer(filename); + return ret; +} + +/**************************************************************************** + * Name: nfs_lstat + * + * Description: + * Get file status without following symbolic links + * + * Returned Value: + * 0 on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int nfs_lstat(FAR struct inode *mountpt, FAR const char *relpath, + FAR struct stat *buf) +{ + FAR struct nfsmount *nmp; + struct file_handle fhandle; + struct nfs_fattr attributes; + struct timespec ts; + int ret; + + finfo("relpath=%s\n", relpath); + + /* Sanity checks */ + + DEBUGASSERT(mountpt && mountpt->i_private); + + /* Get the mountpoint private data from the inode structure */ + + nmp = mountpt->i_private; + DEBUGASSERT(nmp && buf); + + memset(buf, 0, sizeof(*buf)); + + ret = nxmutex_lock(&nmp->nm_lock); + if (ret < 0) + { + return ret; + } + + /* Get the file handle attributes of the requested node (don't follow) */ + + ret = nfs_findnode(nmp, relpath, &fhandle, &attributes, NULL, true); + if (ret != OK) + { + ferr("ERROR: nfs_findnode failed: %d\n", ret); + goto errout_with_lock; + } + + /* Note: We don't call GETATTR here because findnode already returned + * the attributes, and for symlinks we want the attributes of the link + * itself, not the target. + */ + + buf->st_mode = nfs_stat_mode(fxdr_unsigned(uint16_t, attributes.fa_mode), + fxdr_unsigned(uint8_t, attributes.fa_type)); + buf->st_size = fxdr_hyper(&attributes.fa_size); + + /* Extract time values as type time_t in units of seconds */ + + fxdr_nfsv3time(&attributes.fa_mtime, &ts); + buf->st_mtime = ts.tv_sec; + + fxdr_nfsv3time(&attributes.fa_atime, &ts); + buf->st_atime = ts.tv_sec; + + fxdr_nfsv3time(&attributes.fa_ctime, &ts); + buf->st_ctime = ts.tv_sec; + +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); + return ret; +} +#endif /* CONFIG_FS_LINKS */ diff --git a/fs/nfs/rpc.h b/fs/nfs/rpc.h index 8fe0e5cfb5f..92b358f1512 100644 --- a/fs/nfs/rpc.h +++ b/fs/nfs/rpc.h @@ -311,12 +311,24 @@ struct rpc_call_rmdir struct RMDIR3args rmdir; }; +struct rpc_call_symlink +{ + struct rpc_call_header ch; + struct SYMLINK3args symlink; +}; + struct rpc_call_readlink { struct rpc_call_header ch; struct READLINK3args readlink; }; +struct rpc_call_link +{ + struct rpc_call_header ch; + struct LINK3args link; +}; + struct rpc_call_readdir { struct rpc_call_header ch; @@ -453,6 +465,12 @@ struct rpc_reply_setattr struct SETATTR3resok setattr; }; +struct rpc_reply_symlink +{ + struct nfs_reply_header rh; + struct SYMLINK3resok symlink; +}; + struct rpc_reply_readlink { struct nfs_reply_header rh; @@ -462,6 +480,12 @@ struct rpc_reply_readlink #define SIZEOF_rpc_reply_readlink(n) \ (sizeof(struct nfs_reply_header) + SIZEOF_READLINK3resok(n)) +struct rpc_reply_link +{ + struct nfs_reply_header rh; + struct LINK3resok link; +}; + struct rpcclnt { nfsfh_t rc_fh; /* File handle of the root directory */ diff --git a/tools/nxstyle.c b/tools/nxstyle.c index 70cacdd2131..a650ae6e7dc 100644 --- a/tools/nxstyle.c +++ b/tools/nxstyle.c @@ -508,8 +508,12 @@ static const char *g_white_content_list[] = "SETATTR3args", "SETATTR3resok", "FS3args", + "SYMLINK3args", + "SYMLINK3resok", "READLINK3args", "READLINK3resok", + "LINK3args", + "LINK3resok", "SIZEOF_rpc_reply_read", "SIZEOF_rpc_call_write", "SIZEOF_rpc_reply_readdir",
