2011-12-23 10:11:55 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2011-12-23 10:11:55 +08:00
|
|
|
*
|
2018-10-15 01:46:07 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-12-23 10:11:55 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
*/
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2010-05-20 08:10:15 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs_fs.h>
|
2017-10-17 13:53:39 +08:00
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_file.h>
|
2010-05-20 08:10:15 +08:00
|
|
|
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
|
|
|
|
#include "mount.h"
|
|
|
|
#include "nfs.h"
|
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
#define NAME_MAX 64
|
2012-12-15 19:37:41 +08:00
|
|
|
#define DFS_NFS_MAX_MTU 1024
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define strtok_r strtok_s
|
|
|
|
#endif
|
2010-05-20 08:10:15 +08:00
|
|
|
|
|
|
|
struct nfs_file
|
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_fh3 handle; /* handle */
|
|
|
|
size_t offset; /* current offset */
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
size_t size; /* total size */
|
|
|
|
bool_t eof; /* end of file */
|
2010-05-20 08:10:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nfs_dir
|
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_fh3 handle;
|
|
|
|
cookie3 cookie;
|
|
|
|
cookieverf3 cookieverf;
|
|
|
|
entry3 *entry;
|
|
|
|
bool_t eof;
|
|
|
|
READDIR3res res;
|
2010-05-20 08:10:15 +08:00
|
|
|
};
|
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
#define HOST_LENGTH 32
|
|
|
|
#define EXPORT_PATH_LENGTH 32
|
2017-10-17 13:53:39 +08:00
|
|
|
|
2010-05-20 08:10:15 +08:00
|
|
|
struct nfs_filesystem
|
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_fh3 root_handle;
|
|
|
|
nfs_fh3 current_handle;
|
|
|
|
CLIENT *nfs_client;
|
|
|
|
CLIENT *mount_client;
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
char host[HOST_LENGTH];
|
|
|
|
char export[EXPORT_PATH_LENGTH];
|
2017-10-17 13:53:39 +08:00
|
|
|
void *data; /* nfs_file or nfs_dir */
|
2010-05-20 08:10:15 +08:00
|
|
|
};
|
2017-10-17 13:53:39 +08:00
|
|
|
|
|
|
|
typedef struct nfs_filesystem nfs_filesystem;
|
2010-05-20 08:10:15 +08:00
|
|
|
typedef struct nfs_file nfs_file;
|
|
|
|
typedef struct nfs_dir nfs_dir;
|
2017-10-17 13:53:39 +08:00
|
|
|
|
|
|
|
nfs_dir *nfs_opendir(nfs_filesystem *nfs, const char *path);
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int nfs_parse_host_export(const char *host_export,
|
2013-06-26 22:30:40 +08:00
|
|
|
char *host,
|
2019-04-03 18:09:52 +08:00
|
|
|
size_t host_len,
|
2013-06-26 22:30:40 +08:00
|
|
|
char *export,
|
|
|
|
size_t export_len)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
int index;
|
|
|
|
|
|
|
|
for (index = 0; index < host_len; index ++)
|
|
|
|
{
|
|
|
|
/* it's end of string, failed */
|
|
|
|
if (host_export[index] == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* copy to host buffer */
|
|
|
|
if (host_export[index] != ':')
|
|
|
|
host[index] = host_export[index];
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* host buffer is not enough, failed */
|
|
|
|
if (index == host_len)
|
|
|
|
return -1;
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
/* make NULL */
|
2013-06-26 22:30:40 +08:00
|
|
|
host_len = index;
|
|
|
|
host[host_len] = '\0';
|
|
|
|
|
|
|
|
host_len ++;
|
|
|
|
|
|
|
|
/* copy export path */
|
|
|
|
for (index = host_len; index < host_len + export_len; index ++)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
if (host_export[index] == 0)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
export[index - host_len] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export[index - host_len] = host_export[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_handle(nfs_fh3 *dest, const nfs_fh3 *source)
|
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
dest->data.data_len = source->data.data_len;
|
|
|
|
dest->data.data_val = rt_malloc(dest->data.data_len);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (dest->data.data_val == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
dest->data.data_len = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dest->data.data_val, source->data.data_val, dest->data.data_len);
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
static nfs_fh3 *get_handle(nfs_filesystem *nfs, const char *name)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_fh3 *handle = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
char *file;
|
|
|
|
char *path;
|
|
|
|
char *init;
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
init = path = rt_malloc(strlen(name) + 1);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (init == NULL)
|
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
memcpy(init, name, strlen(name) + 1);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
handle = rt_malloc(sizeof(nfs_fh3));
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_free(init);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (path[0] == '/')
|
|
|
|
{
|
|
|
|
path ++;
|
|
|
|
copy_handle(handle, &nfs->root_handle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copy_handle(handle, &nfs->current_handle);
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
while ((file = strtok_r(NULL, "/", &path)) != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
LOOKUP3args args;
|
|
|
|
LOOKUP3res res;
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
copy_handle(&args.what.dir, handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
args.what.name = file;
|
|
|
|
|
|
|
|
if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Lookup failed\n");
|
|
|
|
rt_free(init);
|
|
|
|
rt_free(handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Lookup failed: %d\n", res.status);
|
|
|
|
rt_free(init);
|
|
|
|
rt_free(handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
|
|
|
|
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
copy_handle(handle, &res.LOOKUP3res_u.resok.object);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
|
|
|
|
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(init);
|
|
|
|
|
|
|
|
return handle;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
static nfs_fh3 *get_dir_handle(nfs_filesystem *nfs, const char *name)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_fh3 *handle = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
char *file;
|
|
|
|
char *path;
|
|
|
|
char *init;
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
init = path = rt_malloc(strlen(name) + 1);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (init == NULL)
|
|
|
|
return NULL;
|
2019-04-03 18:09:52 +08:00
|
|
|
memcpy(init, name, strlen(name) + 1);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
handle = rt_malloc(sizeof(nfs_fh3));
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_free(init);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (path[0] == '/')
|
|
|
|
{
|
|
|
|
path ++;
|
|
|
|
copy_handle(handle, &nfs->root_handle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copy_handle(handle, &nfs->current_handle);
|
|
|
|
}
|
|
|
|
|
2019-06-26 11:33:41 +08:00
|
|
|
while ((file = strtok_r(NULL, "/", &path)) != NULL && path && path[0] != 0)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
LOOKUP3args args;
|
|
|
|
LOOKUP3res res;
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
copy_handle(&args.what.dir, handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
args.what.name = file;
|
|
|
|
|
|
|
|
if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Lookup failed\n");
|
|
|
|
rt_free(init);
|
|
|
|
rt_free(handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Lookup failed: %d\n", res.status);
|
|
|
|
rt_free(init);
|
|
|
|
rt_free(handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
|
|
|
|
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
copy_handle(handle, &res.LOOKUP3res_u.resok.object);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
|
|
|
|
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(init);
|
|
|
|
|
|
|
|
return handle;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
static size_t nfs_get_filesize(nfs_filesystem *nfs, nfs_fh3 *handle)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
GETATTR3args args;
|
|
|
|
GETATTR3res res;
|
|
|
|
fattr3 *info;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
args.object = *handle;
|
|
|
|
|
|
|
|
memset(&res, '\0', sizeof(res));
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) ||
|
2013-06-26 22:30:40 +08:00
|
|
|
res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("GetAttr failed: %d\n", res.status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = &res.GETATTR3res_u.resok.obj_attributes;
|
|
|
|
size = info->size;
|
|
|
|
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
return size;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
rt_bool_t nfs_is_directory(nfs_filesystem *nfs, const char *name)
|
2010-05-22 22:57:51 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
GETATTR3args args;
|
|
|
|
GETATTR3res res;
|
|
|
|
fattr3 *info;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
rt_bool_t result;
|
|
|
|
|
|
|
|
result = RT_FALSE;
|
|
|
|
handle = get_handle(nfs, name);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return RT_FALSE;
|
|
|
|
|
|
|
|
args.object = *handle;
|
|
|
|
|
|
|
|
memset(&res, '\0', sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("GetAttr failed\n");
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Getattr failed: %d\n", res.status);
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
info = &res.GETATTR3res_u.resok.obj_attributes;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
if (info->type == NFS3DIR)
|
|
|
|
result = RT_TRUE;
|
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
return result;
|
2010-05-22 22:57:51 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
int nfs_create(nfs_filesystem *nfs, const char *name, mode_t mode)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
CREATE3args args;
|
|
|
|
CREATE3res res;
|
|
|
|
int ret = 0;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = get_dir_handle(nfs, name);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
args.where.dir = *handle;
|
|
|
|
args.where.name = strrchr(name, '/') + 1;
|
2017-10-17 13:53:39 +08:00
|
|
|
if (args.where.name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
args.where.name = (char *)name;
|
|
|
|
}
|
|
|
|
args.how.mode = GUARDED;
|
|
|
|
|
|
|
|
args.how.createhow3_u.obj_attributes.mode.set_it = TRUE;
|
|
|
|
args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = mode;
|
|
|
|
args.how.createhow3_u.obj_attributes.uid.set_it = FALSE;
|
|
|
|
args.how.createhow3_u.obj_attributes.gid.set_it = FALSE;
|
|
|
|
args.how.createhow3_u.obj_attributes.size.set_it = FALSE;
|
|
|
|
args.how.createhow3_u.obj_attributes.atime.set_it = DONT_CHANGE;
|
|
|
|
args.how.createhow3_u.obj_attributes.mtime.set_it = DONT_CHANGE;
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_create_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Create failed\n");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Create failed: %d\n", res.status);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
xdr_free((xdrproc_t)xdr_CREATE3res, (char *)&res);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
|
|
|
|
|
|
|
return ret;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
int nfs_mkdir(nfs_filesystem *nfs, const char *name, mode_t mode)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
MKDIR3args args;
|
|
|
|
MKDIR3res res;
|
|
|
|
int ret = 0;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
handle = get_dir_handle(nfs, name);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
args.where.dir = *handle;
|
|
|
|
args.where.name = strrchr(name, '/') + 1;
|
2017-10-17 13:53:39 +08:00
|
|
|
if (args.where.name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
args.where.name = (char *)name;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.attributes.mode.set_it = TRUE;
|
|
|
|
args.attributes.mode.set_mode3_u.mode = mode;
|
|
|
|
args.attributes.uid.set_it = FALSE;
|
|
|
|
args.attributes.gid.set_it = FALSE;
|
|
|
|
args.attributes.size.set_it = FALSE;
|
|
|
|
args.attributes.atime.set_it = DONT_CHANGE;
|
|
|
|
args.attributes.mtime.set_it = DONT_CHANGE;
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_mkdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Mkdir failed\n");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Mkdir failed: %d\n", res.status);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
return ret;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
/* mount(NULL, "/mnt", "nfs", 0, "192.168.1.1:/export") */
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
mountres3 res;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs = (nfs_filesystem *)rt_malloc(sizeof(nfs_filesystem));
|
|
|
|
memset(nfs, 0, sizeof(nfs_filesystem));
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
if (nfs_parse_host_export((const char *)data, nfs->host, HOST_LENGTH,
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs->export, EXPORT_PATH_LENGTH) < 0)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("host or export path error\n");
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs->mount_client = clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp");
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->mount_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("create mount client failed\n");
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&res, '\0', sizeof(mountres3));
|
|
|
|
if (mountproc3_mnt_3((char *)nfs->export, &res, nfs->mount_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("nfs mount failed\n");
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
else if (res.fhs_status != MNT3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("nfs mount failed\n");
|
|
|
|
goto __return;
|
|
|
|
}
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs->nfs_client = clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp");
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("creat nfs client failed\n");
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
copy_handle(&nfs->root_handle, (nfs_fh3 *)&res.mountres3_u.mountinfo.fhandle);
|
|
|
|
copy_handle(&nfs->current_handle, &nfs->root_handle);
|
|
|
|
|
|
|
|
nfs->nfs_client->cl_auth = authnone_create();
|
|
|
|
fs->data = nfs;
|
|
|
|
|
|
|
|
return 0;
|
2010-05-20 08:10:15 +08:00
|
|
|
|
|
|
|
__return:
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->mount_client != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
clnt_destroy(nfs->mount_client);
|
|
|
|
}
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client->cl_auth != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
auth_destroy(nfs->nfs_client->cl_auth);
|
|
|
|
}
|
|
|
|
clnt_destroy(nfs->nfs_client);
|
|
|
|
}
|
|
|
|
rt_free(nfs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_unmount(struct dfs_filesystem *fs)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
RT_ASSERT(fs != NULL);
|
|
|
|
RT_ASSERT(fs->data != NULL);
|
|
|
|
nfs = (nfs_filesystem *)fs->data;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->mount_client != NULL &&
|
|
|
|
mountproc3_umnt_3((char *)nfs->export, NULL, nfs->mount_client) != RPC_SUCCESS)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
rt_kprintf("unmount failed\n");
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy nfs client */
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client->cl_auth != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
auth_destroy(nfs->nfs_client->cl_auth);
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->nfs_client->cl_auth = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
clnt_destroy(nfs->nfs_client);
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->nfs_client = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy mount client */
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->mount_client != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->mount_client->cl_auth != NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
auth_destroy(nfs->mount_client->cl_auth);
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->mount_client->cl_auth = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
clnt_destroy(nfs->mount_client);
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->mount_client = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(nfs);
|
2017-10-17 13:53:39 +08:00
|
|
|
fs->data = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
return 0;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
int nfs_ioctl(struct dfs_fd *file, int cmd, void *args)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
return -ENOSYS;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
int nfs_read(struct dfs_fd *file, void *buf, size_t count)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
READ3args args;
|
|
|
|
READ3res res;
|
2019-04-03 18:09:52 +08:00
|
|
|
ssize_t bytes, total = 0;
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_file *fd;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EISDIR;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->data != NULL);
|
|
|
|
struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->data));
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs = (struct nfs_filesystem *)(dfs_nfs->data);
|
2017-10-17 13:53:39 +08:00
|
|
|
fd = (nfs_file *)(nfs->data);
|
|
|
|
RT_ASSERT(fd != NULL);
|
|
|
|
|
|
|
|
if (nfs->nfs_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* end of file */
|
|
|
|
if (fd->eof == TRUE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
args.file = fd->handle;
|
2019-04-03 18:09:52 +08:00
|
|
|
do
|
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
args.offset = fd->offset;
|
|
|
|
args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
|
|
|
|
count -= args.count;
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Read failed\n");
|
|
|
|
total = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Read failed: %d\n", res.status);
|
|
|
|
total = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes = res.READ3res_u.resok.count;
|
|
|
|
total += bytes;
|
|
|
|
fd->offset += bytes;
|
|
|
|
/* update current position */
|
|
|
|
file->pos = fd->offset;
|
|
|
|
memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
|
|
|
|
buf = (void *)((char *)buf + args.count);
|
|
|
|
if (res.READ3res_u.resok.eof)
|
|
|
|
{
|
|
|
|
/* something should probably be here */
|
|
|
|
fd->eof = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
|
2019-04-03 18:09:52 +08:00
|
|
|
}
|
|
|
|
while (count > 0);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
|
|
|
|
|
|
|
|
return total;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
int nfs_write(struct dfs_fd *file, const void *buf, size_t count)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
WRITE3args args;
|
|
|
|
WRITE3res res;
|
2019-04-03 18:09:52 +08:00
|
|
|
ssize_t bytes, total = 0;
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_file *fd;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EISDIR;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->data != NULL);
|
|
|
|
struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->data));
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs = (struct nfs_filesystem *)(dfs_nfs->data);
|
2017-10-17 13:53:39 +08:00
|
|
|
fd = (nfs_file *)(nfs->data);
|
|
|
|
RT_ASSERT(fd != NULL);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
args.file = fd->handle;
|
|
|
|
args.stable = FILE_SYNC;
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
do
|
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
args.offset = fd->offset;
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
2019-04-03 18:09:52 +08:00
|
|
|
args.data.data_val = (void *)buf;
|
2013-06-26 22:30:40 +08:00
|
|
|
args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
|
|
|
|
args.data.data_len = args.count;
|
|
|
|
count -= args.count;
|
|
|
|
buf = (const void *)((char *)buf + args.count);
|
|
|
|
|
|
|
|
if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Write failed\n");
|
|
|
|
total = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Write failed: %d\n", res.status);
|
|
|
|
total = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes = res.WRITE3res_u.resok.count;
|
|
|
|
fd->offset += bytes;
|
|
|
|
total += bytes;
|
|
|
|
/* update current position */
|
|
|
|
file->pos = fd->offset;
|
2014-02-01 21:35:23 +08:00
|
|
|
/* update file size */
|
2017-12-15 17:15:13 +08:00
|
|
|
if (fd->size < fd->offset) fd->size = fd->offset;
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->size = fd->size;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
|
2022-12-03 12:07:44 +08:00
|
|
|
} while (count > 0);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
|
|
|
|
|
|
|
|
return total;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
int nfs_lseek(struct dfs_fd *file, off_t offset)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_file *fd;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EISDIR;
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->data != NULL);
|
|
|
|
struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->data));
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs = (struct nfs_filesystem *)(dfs_nfs->data);
|
2017-10-17 13:53:39 +08:00
|
|
|
fd = (nfs_file *)(nfs->data);
|
|
|
|
RT_ASSERT(fd != NULL);
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
if (offset <= fd->size)
|
|
|
|
{
|
|
|
|
fd->offset = offset;
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EIO;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_close(struct dfs_fd *file)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->data != NULL);
|
|
|
|
struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->data));
|
|
|
|
|
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs = (struct nfs_filesystem *)(dfs_nfs->data);
|
2017-10-17 13:53:39 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
struct nfs_dir *dir;
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
dir = (struct nfs_dir *)nfs->data;
|
2013-06-26 22:30:40 +08:00
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
|
|
|
|
rt_free(dir);
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
else if (file->vnode->type == FT_REGULAR)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
struct nfs_file *fd;
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
fd = (struct nfs_file *)nfs->data;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
|
|
|
|
rt_free(fd);
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->data = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
return 0;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_open(struct dfs_fd *file)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->data != NULL);
|
|
|
|
struct dfs_filesystem *dfs_nfs = file->vnode->fs;
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs = (struct nfs_filesystem *)(dfs_nfs->data);
|
2019-06-26 11:33:41 +08:00
|
|
|
RT_ASSERT(nfs != NULL);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
if (file->vnode->type == FT_DIRECTORY
|
|
|
|
&& !(file->flags & O_DIRECTORY))
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
file->pos = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (file->flags & O_DIRECTORY)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
nfs_dir *dir;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (file->flags & O_CREAT)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (nfs_mkdir(nfs, file->vnode->path, 0755) < 0)
|
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EAGAIN;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open directory */
|
2022-12-03 12:07:44 +08:00
|
|
|
dir = nfs_opendir(nfs, file->vnode->path);
|
|
|
|
if (dir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
file->vnode->type = FT_DIRECTORY;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->data = dir;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nfs_file *fp;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
|
|
|
|
/* create file */
|
2017-10-17 13:53:39 +08:00
|
|
|
if (file->flags & O_CREAT)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (nfs_create(nfs, file->vnode->path, 0664) < 0)
|
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EAGAIN;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open file (get file handle ) */
|
|
|
|
fp = rt_malloc(sizeof(nfs_file));
|
2017-10-17 13:53:39 +08:00
|
|
|
if (fp == NULL)
|
|
|
|
return -ENOMEM;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
handle = get_handle(nfs, file->vnode->path);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_free(fp);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return -ENOENT;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get size of file */
|
|
|
|
fp->size = nfs_get_filesize(nfs, handle);
|
|
|
|
fp->offset = 0;
|
|
|
|
fp->eof = FALSE;
|
|
|
|
|
|
|
|
copy_handle(&fp->handle, handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (file->flags & O_APPEND)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
fp->offset = fp->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set private file */
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs->data = fp;
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->size = fp->size;
|
|
|
|
file->vnode->type = FT_REGULAR;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
GETATTR3args args;
|
|
|
|
GETATTR3res res;
|
|
|
|
fattr3 *info;
|
|
|
|
nfs_fh3 *handle;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
RT_ASSERT(fs != NULL);
|
|
|
|
RT_ASSERT(fs->data != NULL);
|
|
|
|
nfs = (nfs_filesystem *)fs->data;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
handle = get_handle(nfs, path);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
args.object = *handle;
|
|
|
|
|
|
|
|
memset(&res, '\0', sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("GetAttr failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Getattr failed: %d\n", res.status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = &res.GETATTR3res_u.resok.obj_attributes;
|
|
|
|
|
|
|
|
st->st_dev = 0;
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
|
2013-06-26 22:30:40 +08:00
|
|
|
if (info->type == NFS3DIR)
|
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
st->st_mode &= ~S_IFREG;
|
|
|
|
st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
st->st_size = info->size;
|
|
|
|
st->st_mtime = info->mtime.seconds;
|
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
|
|
|
|
|
|
|
return 0;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_dir *nfs_opendir(nfs_filesystem *nfs, const char *path)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_dir *dir;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
|
|
|
|
dir = rt_malloc(sizeof(nfs_dir));
|
2017-10-17 13:53:39 +08:00
|
|
|
if (dir == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handle = get_handle(nfs, path);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
rt_free(dir);
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
copy_handle(&dir->handle, handle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
|
|
|
|
|
|
|
dir->cookie = 0;
|
|
|
|
memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
|
2017-10-17 13:53:39 +08:00
|
|
|
dir->entry = NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
dir->eof = FALSE;
|
|
|
|
memset(&dir->res, '\0', sizeof(dir->res));
|
|
|
|
|
|
|
|
return dir;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
char *nfs_readdir(nfs_filesystem *nfs, nfs_dir *dir)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
static char name[NAME_MAX];
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client == NULL || dir == NULL)
|
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (dir->entry == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
READDIR3args args;
|
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
|
|
|
|
memset(&dir->res, '\0', sizeof(dir->res));
|
|
|
|
|
|
|
|
args.dir = dir->handle;
|
|
|
|
args.cookie = dir->cookie;
|
|
|
|
memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
|
|
|
|
args.count = 1024;
|
|
|
|
|
|
|
|
if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Readdir failed\n");
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
else if (dir->res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Readdir failed: %d\n", dir->res.status);
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
|
|
|
|
dir->eof = dir->res.READDIR3res_u.resok.reply.eof;
|
|
|
|
dir->entry = dir->res.READDIR3res_u.resok.reply.entries;
|
|
|
|
}
|
2017-10-17 13:53:39 +08:00
|
|
|
if (dir->eof == TRUE && dir->entry == NULL)
|
|
|
|
return NULL;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
dir->cookie = dir->entry->cookie;
|
2019-04-03 18:09:52 +08:00
|
|
|
strncpy(name, dir->entry->name, NAME_MAX - 1);
|
2013-06-26 22:30:40 +08:00
|
|
|
dir->entry = dir->entry->nextentry;
|
|
|
|
name[NAME_MAX - 1] = '\0';
|
|
|
|
|
|
|
|
return name;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_unlink(struct dfs_filesystem *fs, const char *path)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
int ret = 0;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
RT_ASSERT(fs != NULL);
|
|
|
|
RT_ASSERT(fs->data != NULL);
|
|
|
|
nfs = (nfs_filesystem *)fs->data;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
if (nfs_is_directory(nfs, path) == RT_FALSE)
|
|
|
|
{
|
|
|
|
/* remove file */
|
|
|
|
REMOVE3args args;
|
|
|
|
REMOVE3res res;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
|
|
|
|
handle = get_dir_handle(nfs, path);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
args.object.dir = *handle;
|
|
|
|
args.object.name = strrchr(path, '/') + 1;
|
2017-10-17 13:53:39 +08:00
|
|
|
if (args.object.name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
args.object.name = (char *)path;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Remove failed\n");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Remove failed: %d\n", res.status);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
|
|
|
rt_free(handle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* remove directory */
|
|
|
|
RMDIR3args args;
|
|
|
|
RMDIR3res res;
|
|
|
|
nfs_fh3 *handle;
|
|
|
|
|
|
|
|
handle = get_dir_handle(nfs, path);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (handle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
args.object.dir = *handle;
|
|
|
|
args.object.name = strrchr(path, '/') + 1;
|
2017-10-17 13:53:39 +08:00
|
|
|
if (args.object.name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
{
|
|
|
|
args.object.name = (char *)path;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Rmdir failed\n");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Rmdir failed: %d\n", res.status);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
|
2019-04-03 18:09:52 +08:00
|
|
|
rt_free(handle);
|
2013-06-26 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 10:11:55 +08:00
|
|
|
int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
RENAME3args args;
|
|
|
|
RENAME3res res;
|
|
|
|
nfs_fh3 *sHandle;
|
|
|
|
nfs_fh3 *dHandle;
|
|
|
|
int ret = 0;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
RT_ASSERT(fs != NULL);
|
|
|
|
RT_ASSERT(fs->data != NULL);
|
|
|
|
nfs = (nfs_filesystem *)fs->data;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
if (nfs->nfs_client == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
sHandle = get_dir_handle(nfs, src);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (sHandle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
dHandle = get_dir_handle(nfs, dest);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (dHandle == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
args.from.dir = *sHandle;
|
|
|
|
args.from.name = strrchr(src, '/') + 1;
|
2017-10-17 13:53:39 +08:00
|
|
|
if (args.from.name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
args.from.name = (char *)src;
|
|
|
|
|
|
|
|
args.to.dir = *dHandle;
|
|
|
|
args.to.name = strrchr(src, '/') + 1;
|
2017-10-17 13:53:39 +08:00
|
|
|
if (args.to.name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
args.to.name = (char *)dest;
|
|
|
|
|
|
|
|
memset(&res, '\0', sizeof(res));
|
|
|
|
|
|
|
|
if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
|
|
|
|
{
|
|
|
|
rt_kprintf("Rename failed\n");
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else if (res.status != NFS3_OK)
|
|
|
|
{
|
|
|
|
rt_kprintf("Rename failed: %d\n", res.status);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
|
|
|
|
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
|
|
|
|
xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
|
|
|
|
|
|
|
|
return ret;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_dir *dir;
|
|
|
|
rt_uint32_t index;
|
|
|
|
struct dirent *d;
|
2017-10-17 13:53:39 +08:00
|
|
|
nfs_filesystem *nfs;
|
2013-06-26 22:30:40 +08:00
|
|
|
char *name;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->data != NULL);
|
|
|
|
struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->data));
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs = (struct nfs_filesystem *)(dfs_nfs->data);
|
2017-10-17 13:53:39 +08:00
|
|
|
dir = (nfs_dir *)(nfs->data);
|
|
|
|
RT_ASSERT(dir != NULL);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
/* make integer count */
|
|
|
|
count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
|
|
|
|
if (count == 0)
|
2017-10-17 13:53:39 +08:00
|
|
|
return -EINVAL;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
index = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
d = dirp + index;
|
|
|
|
|
|
|
|
name = nfs_readdir(nfs, dir);
|
2017-10-17 13:53:39 +08:00
|
|
|
if (name == NULL)
|
2013-06-26 22:30:40 +08:00
|
|
|
break;
|
|
|
|
|
2019-06-26 11:33:41 +08:00
|
|
|
if (rt_strcmp(name, ".") == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (rt_strcmp(name, "..") == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
d->d_type = DT_REG;
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
d->d_namlen = rt_strlen(name);
|
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
2021-03-27 01:24:32 +08:00
|
|
|
rt_strncpy(d->d_name, name, DFS_PATH_MAX);
|
2013-06-26 22:30:40 +08:00
|
|
|
|
|
|
|
index ++;
|
|
|
|
if (index * sizeof(struct dirent) >= count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index * sizeof(struct dirent);
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:53:39 +08:00
|
|
|
static const struct dfs_file_ops nfs_fops =
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs_open,
|
|
|
|
nfs_close,
|
|
|
|
nfs_ioctl,
|
|
|
|
nfs_read,
|
|
|
|
nfs_write,
|
|
|
|
NULL, /* flush */
|
|
|
|
nfs_lseek,
|
|
|
|
nfs_getdents,
|
|
|
|
NULL, /* poll */
|
2017-10-17 13:53:39 +08:00
|
|
|
};
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static const struct dfs_filesystem_ops _nfs =
|
2010-05-20 08:10:15 +08:00
|
|
|
{
|
2013-06-26 22:30:40 +08:00
|
|
|
"nfs",
|
2017-10-17 13:53:39 +08:00
|
|
|
DFS_FS_FLAG_DEFAULT,
|
|
|
|
&nfs_fops,
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_mount,
|
|
|
|
nfs_unmount,
|
2017-10-17 13:53:39 +08:00
|
|
|
NULL, /* mkfs */
|
|
|
|
NULL, /* statfs */
|
2019-04-03 18:09:52 +08:00
|
|
|
nfs_unlink,
|
2013-06-26 22:30:40 +08:00
|
|
|
nfs_stat,
|
|
|
|
nfs_rename,
|
2010-07-19 00:35:56 +08:00
|
|
|
};
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2010-07-19 00:35:56 +08:00
|
|
|
int nfs_init(void)
|
|
|
|
{
|
2017-10-17 13:53:39 +08:00
|
|
|
/* register nfs file system */
|
2013-06-26 22:30:40 +08:00
|
|
|
dfs_register(&_nfs);
|
2010-05-20 08:10:15 +08:00
|
|
|
|
2013-06-26 22:30:40 +08:00
|
|
|
return RT_EOK;
|
2010-05-20 08:10:15 +08:00
|
|
|
}
|
2017-11-03 11:44:37 +08:00
|
|
|
INIT_COMPONENT_EXPORT(nfs_init);
|
|
|
|
|