v9fs:File system based on 9P2000L.

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
This commit is contained in:
chenrun1 2024-06-08 13:02:28 +08:00 committed by Alan Carvalho de Assis
parent af5d679e18
commit aee9125350
13 changed files with 3327 additions and 3 deletions

View File

@ -134,4 +134,5 @@ source "fs/userfs/Kconfig"
source "fs/hostfs/Kconfig"
source "fs/rpmsgfs/Kconfig"
source "fs/zipfs/Kconfig"
source "fs/mnemofs/Kconfig"
source "fs/mnemofs/Kconfig"
source "fs/v9fs/Kconfig"

View File

@ -58,7 +58,7 @@ include littlefs/Make.defs
include rpmsgfs/Make.defs
include zipfs/Make.defs
include mnemofs/Make.defs
include v9fs/Make.defs
endif
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs

View File

@ -153,6 +153,12 @@ FAR const char *fs_gettype(FAR struct statfs *statbuf)
break;
#endif
#ifdef CONFIG_FS_V9FS
case V9FS_MAGIC:
fstype = "v9fs";
break;
#endif
default:
fstype = "Unrecognized";
break;

View File

@ -68,7 +68,8 @@
defined(CONFIG_FS_PROCFS) || defined(CONFIG_NFS) || \
defined(CONFIG_FS_TMPFS) || defined(CONFIG_FS_USERFS) || \
defined(CONFIG_FS_CROMFS) || defined(CONFIG_FS_UNIONFS) || \
defined(CONFIG_FS_HOSTFS) || defined(CONFIG_FS_RPMSGFS)
defined(CONFIG_FS_HOSTFS) || defined(CONFIG_FS_RPMSGFS) || \
defined(CONFIG_FS_V9FS)
# define NODFS_SUPPORT
#endif
@ -190,6 +191,9 @@ extern const struct mountpt_operations g_rpmsgfs_operations;
#ifdef CONFIG_FS_ZIPFS
extern const struct mountpt_operations g_zipfs_operations;
#endif
#ifdef CONFIG_FS_V9FS
extern const struct mountpt_operations g_v9fs_operations;
#endif
static const struct fsmap_t g_nonbdfsmap[] =
{
@ -225,6 +229,9 @@ static const struct fsmap_t g_nonbdfsmap[] =
#endif
#ifdef CONFIG_FS_ZIPFS
{ "zipfs", &g_zipfs_operations},
#endif
#ifdef CONFIG_FS_V9FS
{ "v9fs", &g_v9fs_operations},
#endif
{ NULL, NULL },
};

29
fs/v9fs/CMakeLists.txt Normal file
View File

@ -0,0 +1,29 @@
# ##############################################################################
# fs/v9fs/CMakeLists.txt
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################
if(CONFIG_FS_V9FS)
set(V9FS client.c transport.c v9fs.c)
if(CONFIG_DRIVERS_VIRTIO_9P)
list(APPEND V9FS virtio_9p.c)
endif()
target_sources(fs PRIVATE ${V9FS})
endif()

24
fs/v9fs/Kconfig Normal file
View File

@ -0,0 +1,24 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config FS_V9FS
bool "V9FS file system"
default n
depends on !DISABLE_MOUNTPOINT
---help---
Enable V9FS filesystem support
if FS_V9FS
config V9FS_DEFAULT_MSIZE
int "V9FS Default message max size"
default 65536
config V9FS_VIRTIO_9P
bool "Virtio 9P support"
depends on DRIVERS_VIRTIO
default n
endif

35
fs/v9fs/Make.defs Normal file
View File

@ -0,0 +1,35 @@
############################################################################
# fs/v9fs/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifeq ($(CONFIG_FS_V9FS),y)
# Files required for V9FS file system support
CSRCS += client.c transport.c v9fs.c
ifeq ($(CONFIG_V9FS_VIRTIO_9P),y)
CSRCS += virtio_9p.c
endif
# Include V9FS build support
DEPPATH += --dep-path v9fs
VPATH += :v9fs
endif

1763
fs/v9fs/client.c Normal file

File diff suppressed because it is too large Load Diff

126
fs/v9fs/client.h Normal file
View File

@ -0,0 +1,126 @@
/****************************************************************************
* fs/v9fs/client.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __FS_V9FS_CLIENT_H
#define __FS_V9FS_CLIENT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/idr.h>
#include <nuttx/list.h>
#include <nuttx/mutex.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/statfs.h>
/****************************************************************************
* Type Definitions
****************************************************************************/
struct v9fs_payload_s
{
FAR struct iovec *wiov;
FAR struct iovec *riov;
struct list_node node;
size_t wcount;
size_t rcount;
sem_t resp;
uint16_t tag;
int ret;
};
struct v9fs_transport_s
{
FAR const struct v9fs_transport_ops_s *ops;
/* The remainder of the structure is used by the "lower-half" driver
* for whatever state storage that it may need.
*/
};
struct v9fs_transport_ops_s
{
CODE int (*create)(FAR struct v9fs_transport_s **transport,
FAR const char *args);
CODE int (*request)(FAR struct v9fs_transport_s *transport,
FAR struct v9fs_payload_s *payload);
CODE void (*destroy)(FAR struct v9fs_transport_s *transport);
};
struct v9fs_client_s
{
FAR struct v9fs_transport_s *transport;
FAR struct idr_s *fids;
unsigned int msize;
uint32_t root_fid;
uint32_t tag_id;
mutex_t lock;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int v9fs_client_statfs(FAR struct v9fs_client_s *client,
FAR struct statfs *buf);
int v9fs_client_stat(FAR struct v9fs_client_s *client, uint32_t fid,
FAR struct stat *buf);
off_t v9fs_client_getsize(FAR struct v9fs_client_s *client, uint32_t fid);
int v9fs_client_chstat(FAR struct v9fs_client_s *client, uint32_t fid,
FAR const struct stat *buf, int flags);
ssize_t v9fs_client_read(FAR struct v9fs_client_s *client, uint32_t fid,
FAR void *buffer, off_t offset, size_t buflen);
ssize_t v9fs_client_convertdir(FAR const uint8_t *buffer, size_t bufsize,
off_t head, FAR off_t *offset,
FAR struct dirent *entry);
ssize_t v9fs_client_readdir(FAR struct v9fs_client_s *client, uint32_t fid,
FAR void *buffer, off_t offset, size_t buflen);
ssize_t v9fs_client_write(FAR struct v9fs_client_s *client, uint32_t fid,
FAR const void *buffer, off_t offset,
size_t buflen);
int v9fs_client_fsync(FAR struct v9fs_client_s *client, uint32_t fid);
int v9fs_client_rename(FAR struct v9fs_client_s *client, uint32_t fid,
uint32_t newfid, FAR const char *name);
int v9fs_client_remove(FAR struct v9fs_client_s *client, uint32_t fid);
int v9fs_client_unlink(FAR struct v9fs_client_s *client, uint32_t fid,
FAR const char *name, bool isdir);
int v9fs_client_mkdir(FAR struct v9fs_client_s *client, uint32_t fid,
FAR const char *name, int mode);
int v9fs_client_create(FAR struct v9fs_client_s *client, uint32_t fid,
FAR const char *name, int oflags, int mode);
int v9fs_client_open(FAR struct v9fs_client_s *client,
uint32_t fid, int oflags);
int v9fs_client_walk(FAR struct v9fs_client_s *client, FAR const char *path,
FAR const char **childname);
int v9fs_client_init(FAR struct v9fs_client_s *client, FAR const char *data);
int v9fs_client_uninit(FAR struct v9fs_client_s *client);
int v9fs_transport_create(FAR struct v9fs_transport_s **transport,
FAR const char *trans_type, FAR const char *data);
int v9fs_transport_request(FAR struct v9fs_transport_s *transport,
FAR struct v9fs_payload_s *payload);
void v9fs_transport_destroy(FAR struct v9fs_transport_s *transport);
void v9fs_transport_done(FAR struct v9fs_payload_s *cookie, int ret);
int v9fs_fid_put(FAR struct v9fs_client_s *client, uint32_t fid);
int v9fs_fid_get(FAR struct v9fs_client_s *client, uint32_t fid);
#endif /* __FS_V9FS_CLIENT_H */

103
fs/v9fs/transport.c Normal file
View File

@ -0,0 +1,103 @@
/****************************************************************************
* fs/v9fs/transport.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <string.h>
#include "client.h"
/****************************************************************************
* Private Types
****************************************************************************/
struct v9fs_transport_ops_map_s
{
FAR const char *type;
FAR const struct v9fs_transport_ops_s *ops;
};
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_V9FS_VIRTIO_9P
extern const struct v9fs_transport_ops_s g_virtio_9p_transport_ops;
#endif
static const struct v9fs_transport_ops_map_s g_transport_ops_map[] =
{
#ifdef CONFIG_V9FS_VIRTIO_9P
{ "virtio", &g_virtio_9p_transport_ops },
#endif
{ NULL, NULL},
};
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: v9fs_transport_request
****************************************************************************/
int v9fs_transport_request(FAR struct v9fs_transport_s *transport,
FAR struct v9fs_payload_s *payload)
{
return transport->ops->request(transport, payload);
}
/****************************************************************************
* Name: v9fs_transport_destroy
****************************************************************************/
void v9fs_transport_destroy(FAR struct v9fs_transport_s *transport)
{
transport->ops->destroy(transport);
}
/****************************************************************************
* Name: v9fs_register_transport
****************************************************************************/
int v9fs_transport_create(FAR struct v9fs_transport_s **transport,
FAR const char *trans_type, FAR const char *data)
{
FAR const struct v9fs_transport_ops_map_s *map;
/* Find the corresponding driver layer */
for (map = g_transport_ops_map; map->type; map++)
{
if (strcmp(trans_type, map->type) == 0)
{
break;
}
}
if (map->ops == NULL)
{
return -ENOENT;
}
return map->ops->create(transport, data);
}

961
fs/v9fs/v9fs.c Normal file
View File

@ -0,0 +1,961 @@
/****************************************************************************
* fs/v9fs/v9fs.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <libgen.h>
#include <string.h>
#include <nuttx/fs/fs.h>
#include <nuttx/kmalloc.h>
#include "inode/inode.h"
#include "client.h"
/****************************************************************************
* Private Type
****************************************************************************/
struct v9fs_vfs_file_s
{
uint32_t fid;
mutex_t lock;
};
struct v9fs_vfs_dirent_s
{
struct fs_dirent_s base;
uint32_t fid;
mutex_t lock;
off_t offset;
off_t head;
size_t size;
uint8_t buffer[1];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int v9fs_vfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int v9fs_vfs_close(FAR struct file *filep);
static ssize_t v9fs_vfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t v9fs_vfs_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen);
static off_t v9fs_vfs_seek(FAR struct file *filep, off_t offset,
int whence);
static int v9fs_vfs_sync(FAR struct file *filep);
static int v9fs_vfs_dup(FAR const struct file *oldp, FAR struct file *newp);
static int v9fs_vfs_fstat(FAR const struct file *filep,
FAR struct stat *buf);
static int v9fs_vfs_fchstat(FAR const struct file *filep,
FAR const struct stat *buf, int flags);
static int v9fs_vfs_truncate(FAR struct file *filep, off_t length);
static int v9fs_vfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath,
FAR struct fs_dirent_s **dir);
static int v9fs_vfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static int v9fs_vfs_readdir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir,
FAR struct dirent *entry);
static int v9fs_vfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static int v9fs_vfs_statfs(FAR struct inode *mountpt,
FAR struct statfs *buf);
static int v9fs_vfs_unlink(FAR struct inode *mountpt,
FAR const char *relpath);
static int v9fs_vfs_mkdir(FAR struct inode *mountpt,
FAR const char *relpath, mode_t mode);
static int v9fs_vfs_rmdir(FAR struct inode *mountpt,
FAR const char *relpath);
static int v9fs_vfs_rename(FAR struct inode *mountpt,
FAR const char *oldrelpath,
FAR const char *newrelpath);
static int v9fs_vfs_stat(FAR struct inode *mountpt,
FAR const char *relpath, FAR struct stat *buf);
static int v9fs_vfs_chstat(FAR struct inode *mountpt,
FAR const char *relpath,
FAR const struct stat *buf, int flags);
static int v9fs_vfs_bind(FAR struct inode *driver, FAR const void *data,
FAR void **handle);
static int v9fs_vfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags);
/****************************************************************************
* Public Data
****************************************************************************/
const struct mountpt_operations g_v9fs_operations =
{
v9fs_vfs_open, /* open */
v9fs_vfs_close, /* close */
v9fs_vfs_read, /* read */
v9fs_vfs_write, /* write */
v9fs_vfs_seek, /* seek */
NULL, /* ioctl */
NULL, /* mmap */
v9fs_vfs_truncate, /* truncate */
NULL, /* poll */
v9fs_vfs_sync, /* sync */
v9fs_vfs_dup, /* dup */
v9fs_vfs_fstat, /* fstat */
v9fs_vfs_fchstat, /* fchstat */
v9fs_vfs_opendir, /* opendir */
v9fs_vfs_closedir, /* closedir */
v9fs_vfs_readdir, /* readdir */
v9fs_vfs_rewinddir, /* rewinddir */
v9fs_vfs_bind, /* bind */
v9fs_vfs_unbind, /* unbind */
v9fs_vfs_statfs, /* statfs */
v9fs_vfs_unlink, /* unlink */
v9fs_vfs_mkdir, /* mkdir */
v9fs_vfs_rmdir, /* rmdir */
v9fs_vfs_rename, /* rename */
v9fs_vfs_stat, /* stat */
v9fs_vfs_chstat /* chstat */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: v9fs_vfs_open
****************************************************************************/
static int v9fs_vfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
int ret;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = kmm_zalloc(sizeof(struct v9fs_vfs_file_s));
if (file == NULL)
{
return -ENOMEM;
}
ret = v9fs_client_walk(client, relpath, NULL);
if (ret >= 0)
{
file->fid = ret;
ret = v9fs_client_open(client, file->fid, oflags);
if (ret < 0)
{
ferr("ERROR: Failed to open the fid: %d\n", ret);
goto err_put;
}
}
else if ((oflags & O_CREAT) != 0)
{
/* We expect to create this file */
ret = v9fs_client_walk(client, relpath, &relpath);
if (ret < 0)
{
ferr("ERROR: Can't find the parent fid of relpath: %d\n", ret);
goto err_free;
}
file->fid = ret;
ret = v9fs_client_create(client, file->fid, relpath, oflags, mode);
if (ret < 0)
{
ferr("ERROR: Failed to create the file: %d\n", ret);
goto err_put;
}
}
else
{
/* We expect to open this file */
ferr("ERROR: Can't find the fid of relpath: %d\n", ret);
ret = -ENOENT;
goto err_free;
}
if ((oflags & O_APPEND) != 0)
{
filep->f_pos = v9fs_client_getsize(client, file->fid);
if (filep->f_pos < 0)
{
ret = filep->f_pos;
ferr("ERROR: Failed to get the file status: %d\n", ret);
goto err_put;
}
}
nxmutex_init(&file->lock);
filep->f_priv = file;
return 0;
err_put:
v9fs_fid_put(client, file->fid);
err_free:
kmm_free(file);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_close
****************************************************************************/
static int v9fs_vfs_close(FAR struct file *filep)
{
FAR struct v9fs_client_s *client;
FAR struct v9fs_vfs_file_s *file;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = filep->f_priv;
v9fs_fid_put(client, file->fid);
nxmutex_destroy(&file->lock);
kmm_free(file);
return 0;
}
/****************************************************************************
* Name: v9fs_vfs_read
****************************************************************************/
static ssize_t v9fs_vfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
ssize_t ret;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = filep->f_priv;
nxmutex_lock(&file->lock);
ret = v9fs_client_read(client, file->fid, buffer, filep->f_pos, buflen);
if (ret > 0)
{
filep->f_pos += ret;
}
nxmutex_unlock(&file->lock);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_write
****************************************************************************/
static ssize_t v9fs_vfs_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
ssize_t ret;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = filep->f_priv;
nxmutex_lock(&file->lock);
ret = v9fs_client_write(client, file->fid, buffer, filep->f_pos, buflen);
if (ret > 0)
{
filep->f_pos += ret;
}
nxmutex_unlock(&file->lock);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_seek
****************************************************************************/
static off_t v9fs_vfs_seek(FAR struct file *filep, off_t offset, int whence)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
off_t ret;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = filep->f_priv;
nxmutex_lock(&file->lock);
switch (whence)
{
case SEEK_SET:
ret = offset;
break;
case SEEK_CUR:
ret = filep->f_pos + offset;
break;
case SEEK_END:
ret = v9fs_client_getsize(client, file->fid);
if (ret >= 0)
{
ret += offset;
}
break;
default:
ferr("ERROR: Invalid whence: %d\n", whence);
ret = -EINVAL;
break;
}
if (ret >= 0)
{
filep->f_pos = ret;
}
nxmutex_unlock(&file->lock);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_sync
****************************************************************************/
static int v9fs_vfs_sync(FAR struct file *filep)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
client = filep->f_inode->i_private;
file = filep->f_priv;
return v9fs_client_fsync(client, file->fid);
}
/****************************************************************************
* Name: v9fs_vfs_dup
****************************************************************************/
static int v9fs_vfs_dup(FAR const struct file *oldp, FAR struct file *newp)
{
FAR struct v9fs_vfs_file_s *newfile;
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
int ret;
/* Sanity checks */
DEBUGASSERT(oldp != NULL && oldp->f_inode != NULL &&
newp != NULL && newp->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = oldp->f_inode->i_private;
file = oldp->f_priv;
newfile = kmm_zalloc(sizeof(struct v9fs_vfs_file_s));
if (newfile == NULL)
{
return -ENOMEM;
}
ret = v9fs_fid_get(client, file->fid);
if (ret < 0)
{
kmm_free(newfile);
return ret;
}
nxmutex_init(&newfile->lock);
newfile->fid = file->fid;
newp->f_priv = newfile;
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_fstat
****************************************************************************/
static int v9fs_vfs_fstat(FAR const struct file *filep,
FAR struct stat *buf)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = filep->f_priv;
memset(buf, 0, sizeof(struct stat));
return v9fs_client_stat(client, file->fid, buf);
}
/****************************************************************************
* Name: v9fs_vfs_fchstat
****************************************************************************/
static int v9fs_vfs_fchstat(FAR const struct file *filep,
FAR const struct stat *buf, int flags)
{
FAR struct v9fs_vfs_file_s *file;
FAR struct v9fs_client_s *client;
/* Sanity checks */
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
/* Recover out private data from the struct file instance */
client = filep->f_inode->i_private;
file = filep->f_priv;
return v9fs_client_chstat(client, file->fid, buf, flags);
}
/****************************************************************************
* Name: v9fs_vfs_truncate
****************************************************************************/
static int v9fs_vfs_truncate(FAR struct file *filep, off_t length)
{
struct stat buf;
buf.st_size = length;
return v9fs_vfs_fchstat(filep, &buf, CH_STAT_SIZE);
}
/****************************************************************************
* Name: v9fs_vfs_opendir
****************************************************************************/
static int v9fs_vfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath,
FAR struct fs_dirent_s **dir)
{
FAR struct v9fs_vfs_dirent_s *fsdir;
FAR struct v9fs_client_s *client;
uint32_t fid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
fsdir = kmm_zalloc(sizeof(struct v9fs_vfs_dirent_s) + client->msize);
if (fsdir == NULL)
{
return -ENOMEM;
}
ret = v9fs_client_walk(client, relpath, NULL);
if (ret < 0)
{
ferr("ERROR: Can't find the fid of the relpath: %d\n", ret);
goto err;
}
fid = ret;
ret = v9fs_client_open(client, fid, 0);
if (ret < 0)
{
ferr("ERROR: Failed to open the fid: %d\n", ret);
v9fs_fid_put(client, fid);
goto err;
}
nxmutex_init(&fsdir->lock);
fsdir->fid = fid;
*dir = &fsdir->base;
return 0;
err:
kmm_free(fsdir);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_closedir
****************************************************************************/
static int v9fs_vfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
FAR struct v9fs_vfs_dirent_s *fsdir;
FAR struct v9fs_client_s *client;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
fsdir = (FAR struct v9fs_vfs_dirent_s *)dir;
client = mountpt->i_private;
v9fs_fid_put(client, fsdir->fid);
nxmutex_destroy(&fsdir->lock);
kmm_free(fsdir);
return 0;
}
/****************************************************************************
* Name: v9fs_readdir
****************************************************************************/
static int v9fs_vfs_readdir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir,
FAR struct dirent *entry)
{
FAR struct v9fs_vfs_dirent_s *fsdir;
FAR struct v9fs_client_s *client;
ssize_t ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
fsdir = (FAR struct v9fs_vfs_dirent_s *)dir;
client = mountpt->i_private;
nxmutex_lock(&fsdir->lock);
for (; ; )
{
if (fsdir->head == fsdir->size)
{
ret = v9fs_client_readdir(client, fsdir->fid, fsdir->buffer,
fsdir->offset, client->msize);
if (ret < 0)
{
break;
}
fsdir->head = 0;
fsdir->size = ret;
}
ret = v9fs_client_convertdir(fsdir->buffer, fsdir->size, fsdir->head,
&fsdir->offset, entry);
if (ret < 0)
{
break;
}
fsdir->head += ret;
if (strcmp(entry->d_name, ".") != 0 &&
strcmp(entry->d_name, "..") != 0)
{
break;
}
}
nxmutex_unlock(&fsdir->lock);
return ret < 0 ? ret : 0;
}
/****************************************************************************
* Name: v9fs_vfs_rewinddir
****************************************************************************/
static int v9fs_vfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
FAR struct v9fs_vfs_dirent_s *fsdir;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
fsdir = (FAR struct v9fs_vfs_dirent_s *)dir;
nxmutex_lock(&fsdir->lock);
fsdir->head = 0;
fsdir->offset = 0;
fsdir->size = 0;
nxmutex_unlock(&fsdir->lock);
return 0;
}
/****************************************************************************
* Name: v9fs_vfs_statfs
****************************************************************************/
static int v9fs_vfs_statfs(FAR struct inode *mountpt,
FAR struct statfs *buf)
{
FAR struct v9fs_client_s *client;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
memset(buf, 0, sizeof(struct statfs));
return v9fs_client_statfs(client, buf);
}
/****************************************************************************
* Name: v9fs_vfs_unlink
****************************************************************************/
static int v9fs_vfs_unlink(FAR struct inode *mountpt,
FAR const char *relpath)
{
FAR struct v9fs_client_s *client;
FAR const char *filename;
uint32_t fid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
ret = v9fs_client_walk(client, relpath, &filename);
if (ret < 0)
{
ferr("ERROR: Can't find the parent fid of relpath: %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_unlink(client, fid, filename, false);
v9fs_fid_put(client, fid);
if (ret == -EOPNOTSUPP)
{
/* Maybe the server does not support this method of deletion.
* Let's change the protocol to send
*/
ret = v9fs_client_walk(client, relpath, NULL);
if (ret < 0)
{
ferr("ERROR: Can't find the fid of relpath: %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_remove(client, fid);
v9fs_fid_put(client, fid);
}
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_mkdir
****************************************************************************/
static int v9fs_vfs_mkdir(FAR struct inode *mountpt,
FAR const char *relpath, mode_t mode)
{
FAR struct v9fs_client_s *client;
uint32_t fid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
ret = v9fs_client_walk(client, relpath, &relpath);
if (ret < 0)
{
ferr("ERROR: Can't find the parent fid of relpath: %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_mkdir(client, fid, relpath, mode);
v9fs_fid_put(client, fid);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_rmdir
****************************************************************************/
static int v9fs_vfs_rmdir(FAR struct inode *mountpt,
FAR const char *relpath)
{
FAR struct v9fs_client_s *client;
FAR const char *dirname;
uint32_t fid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
ret = v9fs_client_walk(client, relpath, &dirname);
if (ret < 0)
{
ferr("ERROR: Can't find the parent fid of relpath: %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_unlink(client, fid, dirname, true);
v9fs_fid_put(client, fid);
if (ret == -EOPNOTSUPP)
{
/* Maybe the server does not support this method of deletion.
* Let's change the protocol to send
*/
ret = v9fs_client_walk(client, relpath, NULL);
if (ret < 0)
{
ferr("ERROR: Can't find the fid of relpath: %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_remove(client, fid);
v9fs_fid_put(client, fid);
}
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_rename
****************************************************************************/
static int v9fs_vfs_rename(FAR struct inode *mountpt,
FAR const char *oldrelpath,
FAR const char *newrelpath)
{
FAR struct v9fs_client_s *client;
uint32_t oldfid;
uint32_t newpfid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
ret = v9fs_client_walk(client, oldrelpath, NULL);
if (ret < 0)
{
ferr("ERROR: Can't find the fid of the oldrelpath: %d\n", ret);
return ret;
}
oldfid = ret;
ret = v9fs_client_walk(client, newrelpath, &newrelpath);
if (ret < 0)
{
ferr("ERROR: Can't find the new parent fid of the newrelpath: %d\n",
ret);
v9fs_fid_put(client, oldfid);
return ret;
}
newpfid = ret;
ret = v9fs_client_rename(client, oldfid, newpfid, newrelpath);
v9fs_fid_put(client, oldfid);
v9fs_fid_put(client, newpfid);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_stat
****************************************************************************/
static int v9fs_vfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct stat *buf)
{
FAR struct v9fs_client_s *client;
uint32_t fid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
memset(buf, 0, sizeof(struct stat));
ret = v9fs_client_walk(client, relpath, NULL);
if (ret < 0)
{
ferr("ERROR: Can't find the fid of the relpath: %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_stat(client, fid, buf);
v9fs_fid_put(client, fid);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_chstat
****************************************************************************/
static int v9fs_vfs_chstat(FAR struct inode *mountpt,
FAR const char *relpath,
FAR const struct stat *buf, int flags)
{
FAR struct v9fs_client_s *client;
uint32_t fid;
int ret;
/* Sanity checks */
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
/* Recover out private data from the struct file instance */
client = mountpt->i_private;
ret = v9fs_client_walk(client, relpath, NULL);
if (ret < 0)
{
ferr("ERROR: Can't find the fid of the relpath %d\n", ret);
return ret;
}
fid = ret;
ret = v9fs_client_chstat(client, fid, buf, flags);
v9fs_fid_put(client, fid);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_unbind
****************************************************************************/
static int v9fs_vfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags)
{
FAR struct v9fs_client_s *client;
int ret;
client = handle;
ret = v9fs_client_uninit(client);
if (ret < 0)
{
ferr("ERROR: Failed to clunk root fid\n");
return ret;
}
kmm_free(client);
return ret;
}
/****************************************************************************
* Name: v9fs_vfs_bind
****************************************************************************/
static int v9fs_vfs_bind(FAR struct inode *driver, FAR const void *data,
FAR void **handle)
{
FAR struct v9fs_client_s *client;
int ret;
client = kmm_zalloc(sizeof(struct v9fs_client_s));
if (client == NULL)
{
return -ENOMEM;
}
ret = v9fs_client_init(client, data);
if (ret < 0)
{
ferr("ERROR: Failed to initialize for client: %d\n", ret);
kmm_free(client);
return ret;
}
*handle = client;
return ret;
}

268
fs/v9fs/virtio_9p.c Normal file
View File

@ -0,0 +1,268 @@
/****************************************************************************
* fs/v9fs/virtio_9p.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <errno.h>
#include <nuttx/spinlock.h>
#include <nuttx/virtio/virtio.h>
#include "client.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define VIRTIO_9P_MOUNT_TAG 0
/****************************************************************************
* Private Types
****************************************************************************/
struct virtio_9p_config_s
{
uint16_t tag_len;
char tag[NAME_MAX];
};
struct virtio_9p_priv_s
{
FAR struct virtio_device *vdev;
struct v9fs_transport_s transport;
struct virtio_driver vdrv;
spinlock_t lock;
char tag[0];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* VirtIO transport api for 9p */
static int virtio_9p_create(FAR struct v9fs_transport_s **transport,
FAR const char *args);
static void virtio_9p_destroy(FAR struct v9fs_transport_s *transport);
static int virtio_9p_request(FAR struct v9fs_transport_s *transport,
FAR struct v9fs_payload_s *payload);
static void virtio_9p_done(FAR struct virtqueue *vq);
static int virtio_9p_probe(FAR struct virtio_device *vdev);
static void virtio_9p_remove(FAR struct virtio_device *vdev);
/****************************************************************************
* Public Data
****************************************************************************/
const struct v9fs_transport_ops_s g_virtio_9p_transport_ops =
{
virtio_9p_create, /* create */
virtio_9p_request, /* request */
virtio_9p_destroy, /* close */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: virtio_9p_create
****************************************************************************/
static int virtio_9p_create(FAR struct v9fs_transport_s **transport,
FAR const char *args)
{
FAR struct virtio_9p_priv_s *priv;
FAR const char *start;
FAR const char *end;
size_t length;
int ret;
/* Parse device name */
start = strstr(args, "tag=");
if (start == NULL)
{
return -EINVAL;
}
start += 4;
end = strchr(start, ',');
length = end ? end - start + 1 : strlen(start) + 1;
priv = kmm_zalloc(sizeof(struct virtio_9p_priv_s) + length);
if (priv == NULL)
{
return -ENOMEM;
}
spin_lock_init(&priv->lock);
memcpy(priv->tag, start, length);
priv->vdrv.device = VIRTIO_ID_9P;
priv->vdrv.probe = virtio_9p_probe;
priv->vdrv.remove = virtio_9p_remove;
priv->transport.ops = &g_virtio_9p_transport_ops;
*transport = &priv->transport;
ret = virtio_register_driver(&priv->vdrv);
if (ret < 0)
{
kmm_free(priv);
}
return ret;
}
/****************************************************************************
* Name: virtio_9p_destroy
****************************************************************************/
static void virtio_9p_destroy(FAR struct v9fs_transport_s *transport)
{
FAR struct virtio_9p_priv_s *priv =
container_of(transport, struct virtio_9p_priv_s, transport);
virtio_unregister_driver(&priv->vdrv);
kmm_free(priv);
}
/****************************************************************************
* Name: virtio_9p_request
****************************************************************************/
static int virtio_9p_request(FAR struct v9fs_transport_s *transport,
FAR struct v9fs_payload_s *payload)
{
FAR struct virtio_9p_priv_s *priv =
container_of(transport, struct virtio_9p_priv_s, transport);
FAR struct virtqueue *vq = priv->vdev->vrings_info[0].vq;
struct virtqueue_buf vb[payload->wcount + payload->rcount];
irqstate_t flags;
size_t i;
int ret;
for (i = 0; i < payload->wcount; i++)
{
vb[i].buf = payload->wiov[i].iov_base;
vb[i].len = payload->wiov[i].iov_len;
}
for (i = 0; i < payload->rcount; i++)
{
vb[payload->wcount + i].buf = payload->riov[i].iov_base;
vb[payload->wcount + i].len = payload->riov[i].iov_len;
}
flags = spin_lock_irqsave(&priv->lock);
ret = virtqueue_add_buffer(vq, vb, payload->wcount, payload->rcount,
payload);
if (ret < 0)
{
vrterr("virtqueue_add_buffer failed, ret=%d\n", ret);
goto out;
}
virtqueue_kick(vq);
out:
spin_unlock_irqrestore(&priv->lock, flags);
return ret;
}
/****************************************************************************
* Name: virtio_9p_done
****************************************************************************/
static void virtio_9p_done(FAR struct virtqueue *vq)
{
FAR struct virtio_9p_priv_s *priv = vq->vq_dev->priv;
FAR struct v9fs_payload_s *payload;
for (; ; )
{
payload = virtqueue_get_buffer_lock(vq, NULL, NULL, &priv->lock);
if (payload == NULL)
{
break;
}
v9fs_transport_done(payload, 0);
}
}
/****************************************************************************
* Name: virtio_9p_probe
****************************************************************************/
static int virtio_9p_probe(FAR struct virtio_device *vdev)
{
FAR struct virtio_9p_priv_s *priv = container_of(vdev->priv,
struct virtio_9p_priv_s, vdrv);
struct virtio_9p_config_s config;
FAR const char *vqname[1];
vq_callback callback[1];
int ret;
virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER);
virtio_negotiate_features(vdev, 1 << VIRTIO_9P_MOUNT_TAG);
virtio_set_status(vdev, VIRTIO_CONFIG_FEATURES_OK);
if (!virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG))
{
virtio_reset_device(vdev);
return -EINVAL;
}
virtio_read_config_member(vdev, struct virtio_9p_config_s, tag_len,
&config.tag_len);
virtio_read_config(vdev, offsetof(struct virtio_9p_config_s, tag),
&config.tag, config.tag_len);
config.tag[config.tag_len] = '\0';
if (strcmp(config.tag, priv->tag))
{
virtio_reset_device(vdev);
return -ENOENT;
}
vqname[0] = "virtio_9p_vq";
callback[0] = virtio_9p_done;
ret = virtio_create_virtqueues(vdev, 0, 1, vqname, callback);
if (ret < 0)
{
vrterr("virtio_device_create_virtqueue failed, ret=%d\n", ret);
virtio_reset_device(vdev);
return ret;
}
priv->vdev = vdev;
vdev->priv = priv;
virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
virtqueue_enable_cb(vdev->vrings_info[0].vq);
return OK;
}
/****************************************************************************
* Name: virtio_9p_remove
****************************************************************************/
static void virtio_9p_remove(FAR struct virtio_device *vdev)
{
virtio_delete_virtqueues(vdev);
virtio_reset_device(vdev);
}

View File

@ -96,6 +96,7 @@
#define CROMFS_MAGIC 0x4d4f5243
#define RPMSGFS_MAGIC 0x54534f47
#define ZIPFS_MAGIC 0x504b
#define V9FS_MAGIC 0x01021997
#if defined(CONFIG_FS_LARGEFILE)
# define statfs64 statfs