提交 773990ab 编写于 作者: Y yiyue.fang

Embedded GPLv2 license in dfs

上级 cb517cec
/* /*
* RT-Thread Console Device File * File : console.c
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/ */
#include <rtthread.h> #include <rtthread.h>
struct console_device struct console_device
......
/*
* File : devfs.c
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#include <rtthread.h> #include <rtthread.h>
#include <dfs.h> #include <dfs.h>
#include <dfs_fs.h> #include <dfs_fs.h>
...@@ -6,276 +29,276 @@ ...@@ -6,276 +29,276 @@
struct device_dirent struct device_dirent
{ {
rt_device_t *devices; rt_device_t *devices;
rt_uint16_t read_index; rt_uint16_t read_index;
rt_uint16_t device_count; rt_uint16_t device_count;
}; };
int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) int dfs_device_fs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args) int dfs_device_fs_ioctl(struct dfs_fd *file, int cmd, void *args)
{ {
rt_err_t result; rt_err_t result;
rt_device_t dev_id; rt_device_t dev_id;
RT_ASSERT(file != RT_NULL); RT_ASSERT(file != RT_NULL);
/* get device handler */ /* get device handler */
dev_id = (rt_device_t)file->data; dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL); RT_ASSERT(dev_id != RT_NULL);
/* close device handler */ /* close device handler */
result = rt_device_control(dev_id, cmd, args); result = rt_device_control(dev_id, cmd, args);
if (result == RT_EOK) if (result == RT_EOK)
return DFS_STATUS_OK; return DFS_STATUS_OK;
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
int dfs_device_fs_read(struct dfs_fd *file, void *buf, rt_size_t count) int dfs_device_fs_read(struct dfs_fd *file, void *buf, rt_size_t count)
{ {
int result; int result;
rt_device_t dev_id; rt_device_t dev_id;
RT_ASSERT(file != RT_NULL); RT_ASSERT(file != RT_NULL);
/* get device handler */ /* get device handler */
dev_id = (rt_device_t)file->data; dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL); RT_ASSERT(dev_id != RT_NULL);
/* read device data */ /* read device data */
result = rt_device_read(dev_id, file->pos, buf, count); result = rt_device_read(dev_id, file->pos, buf, count);
file->pos += result; file->pos += result;
return result; return result;
} }
int dfs_device_fs_write(struct dfs_fd *file, const void *buf, rt_size_t count) int dfs_device_fs_write(struct dfs_fd *file, const void *buf, rt_size_t count)
{ {
int result; int result;
rt_device_t dev_id; rt_device_t dev_id;
RT_ASSERT(file != RT_NULL); RT_ASSERT(file != RT_NULL);
/* get device handler */ /* get device handler */
dev_id = (rt_device_t)file->data; dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL); RT_ASSERT(dev_id != RT_NULL);
/* read device data */ /* read device data */
result = rt_device_write(dev_id, file->pos, buf, count); result = rt_device_write(dev_id, file->pos, buf, count);
file->pos += result; file->pos += result;
return result; return result;
} }
int dfs_device_fs_close(struct dfs_fd *file) int dfs_device_fs_close(struct dfs_fd *file)
{ {
rt_err_t result; rt_err_t result;
rt_device_t dev_id; rt_device_t dev_id;
RT_ASSERT(file != RT_NULL); RT_ASSERT(file != RT_NULL);
if (file->type == FT_DIRECTORY) if (file->type == FT_DIRECTORY)
{ {
struct device_dirent *root_dirent; struct device_dirent *root_dirent;
root_dirent = (struct device_dirent *)file->data; root_dirent = (struct device_dirent *)file->data;
RT_ASSERT(root_dirent != RT_NULL); RT_ASSERT(root_dirent != RT_NULL);
/* release dirent */ /* release dirent */
rt_free(root_dirent); rt_free(root_dirent);
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
/* get device handler */ /* get device handler */
dev_id = (rt_device_t)file->data; dev_id = (rt_device_t)file->data;
RT_ASSERT(dev_id != RT_NULL); RT_ASSERT(dev_id != RT_NULL);
/* close device handler */ /* close device handler */
result = rt_device_close(dev_id); result = rt_device_close(dev_id);
if (result == RT_EOK) if (result == RT_EOK)
{ {
file->data = RT_NULL; file->data = RT_NULL;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
int dfs_device_fs_open(struct dfs_fd *file) int dfs_device_fs_open(struct dfs_fd *file)
{ {
rt_device_t device; rt_device_t device;
if (file->flags & DFS_O_CREAT) if (file->flags & DFS_O_CREAT)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
/* open root directory */ /* open root directory */
if ((file->path[0] == '/') && (file->path[1] == '\0') && if ((file->path[0] == '/') && (file->path[1] == '\0') &&
(file->flags & DFS_O_DIRECTORY)) (file->flags & DFS_O_DIRECTORY))
{ {
struct rt_object *object; struct rt_object *object;
struct rt_list_node *node; struct rt_list_node *node;
struct rt_object_information *information; struct rt_object_information *information;
struct device_dirent *root_dirent; struct device_dirent *root_dirent;
rt_uint32_t count = 0; rt_uint32_t count = 0;
extern struct rt_object_information rt_object_container[]; extern struct rt_object_information rt_object_container[];
/* lock scheduler */ /* lock scheduler */
rt_enter_critical(); rt_enter_critical();
/* traverse device object */ /* traverse device object */
information = &rt_object_container[RT_Object_Class_Device]; information = &rt_object_container[RT_Object_Class_Device];
for (node = information->object_list.next; node != &(information->object_list); node = node->next) for (node = information->object_list.next; node != &(information->object_list); node = node->next)
{ {
count ++; count ++;
} }
root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) + root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
count * sizeof(rt_device_t)); count * sizeof(rt_device_t));
if (root_dirent != RT_NULL) if (root_dirent != RT_NULL)
{ {
root_dirent->devices = (rt_device_t *)(root_dirent + 1); root_dirent->devices = (rt_device_t *)(root_dirent + 1);
root_dirent->read_index = 0; root_dirent->read_index = 0;
root_dirent->device_count = count; root_dirent->device_count = count;
count = 0; count = 0;
/* get all device node */ /* get all device node */
for (node = information->object_list.next; node != &(information->object_list); node = node->next) for (node = information->object_list.next; node != &(information->object_list); node = node->next)
{ {
object = rt_list_entry(node, struct rt_object, list); object = rt_list_entry(node, struct rt_object, list);
root_dirent->devices[count] = (rt_device_t)object; root_dirent->devices[count] = (rt_device_t)object;
count ++; count ++;
} }
} }
rt_exit_critical(); rt_exit_critical();
/* set data */ /* set data */
file->data = root_dirent; file->data = root_dirent;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
device = rt_device_find(&file->path[1]); device = rt_device_find(&file->path[1]);
if (device == RT_NULL) if (device == RT_NULL)
return -DFS_STATUS_ENODEV; return -DFS_STATUS_ENODEV;
file->data = device; file->data = device;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{ {
/* stat root directory */ /* stat root directory */
if ((path[0] == '/') && (path[1] == '\0')) if ((path[0] == '/') && (path[1] == '\0'))
{ {
st->st_dev = 0; st->st_dev = 0;
st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
st->st_mode &= ~DFS_S_IFREG; st->st_mode &= ~DFS_S_IFREG;
st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
st->st_size = 0; st->st_size = 0;
st->st_mtime = 0; st->st_mtime = 0;
st->st_blksize = 512; st->st_blksize = 512;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
else else
{ {
rt_device_t dev_id; rt_device_t dev_id;
dev_id = rt_device_find(&path[1]); dev_id = rt_device_find(&path[1]);
if (dev_id != RT_NULL) if (dev_id != RT_NULL)
{ {
st->st_dev = 0; st->st_dev = 0;
st->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | st->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
if (dev_id->type == RT_Device_Class_Char) if (dev_id->type == RT_Device_Class_Char)
st->st_mode |= DFS_S_IFCHR; st->st_mode |= DFS_S_IFCHR;
else if (dev_id->type == RT_Device_Class_Block) else if (dev_id->type == RT_Device_Class_Block)
st->st_mode |= DFS_S_IFBLK; st->st_mode |= DFS_S_IFBLK;
else else
st->st_mode |= DFS_S_IFREG; st->st_mode |= DFS_S_IFREG;
st->st_size = 0; st->st_size = 0;
st->st_mtime = 0; st->st_mtime = 0;
st->st_blksize = 512; st->st_blksize = 512;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
} }
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
} }
int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count) int dfs_device_fs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
{ {
rt_uint32_t index; rt_uint32_t index;
rt_object_t object; rt_object_t object;
struct dirent *d; struct dirent *d;
struct device_dirent *root_dirent; struct device_dirent *root_dirent;
root_dirent = (struct device_dirent *)file->data; root_dirent = (struct device_dirent *)file->data;
RT_ASSERT(root_dirent != RT_NULL); RT_ASSERT(root_dirent != RT_NULL);
/* make integer count */ /* make integer count */
count = (count / sizeof(struct dirent)); count = (count / sizeof(struct dirent));
if (count == 0) if (count == 0)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count; for (index = 0; index < count && index + root_dirent->read_index < root_dirent->device_count;
index ++) index ++)
{ {
object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index]; object = (rt_object_t)root_dirent->devices[root_dirent->read_index + index];
d = dirp + index; d = dirp + index;
d->d_type = DFS_DT_REG; d->d_type = DFS_DT_REG;
d->d_namlen = RT_NAME_MAX; d->d_namlen = RT_NAME_MAX;
d->d_reclen = (rt_uint16_t)sizeof(struct dirent); d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, object->name, RT_NAME_MAX); rt_strncpy(d->d_name, object->name, RT_NAME_MAX);
} }
root_dirent->read_index += index; root_dirent->read_index += index;
return index * sizeof(struct dirent); return index * sizeof(struct dirent);
} }
static const struct dfs_filesystem_operation _device_fs = static const struct dfs_filesystem_operation _device_fs =
{ {
"devfs", "devfs",
DFS_FS_FLAG_DEFAULT, DFS_FS_FLAG_DEFAULT,
dfs_device_fs_mount, dfs_device_fs_mount,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
dfs_device_fs_open, dfs_device_fs_open,
dfs_device_fs_close, dfs_device_fs_close,
dfs_device_fs_ioctl, dfs_device_fs_ioctl,
dfs_device_fs_read, dfs_device_fs_read,
dfs_device_fs_write, dfs_device_fs_write,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
dfs_device_fs_getdents, dfs_device_fs_getdents,
RT_NULL, RT_NULL,
dfs_device_fs_stat, dfs_device_fs_stat,
RT_NULL, RT_NULL,
}; };
int devfs_init(void) int devfs_init(void)
{ {
/* register rom file system */ /* register rom file system */
dfs_register(&_device_fs); dfs_register(&_device_fs);
return 0; return 0;
} }
INIT_FS_EXPORT(devfs_init); INIT_FS_EXPORT(devfs_init);
/*
* File : devfs.h
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __DEVICE_FS_H__ #ifndef __DEVICE_FS_H__
#define __DEVICE_FS_H__ #define __DEVICE_FS_H__
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2008-2011, RT-Thread Development Team * COPYRIGHT (C) 2008-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
...@@ -21,7 +31,7 @@ ...@@ -21,7 +31,7 @@
#include "mount.h" #include "mount.h"
#include "nfs.h" #include "nfs.h"
#define NAME_MAX 64 #define NAME_MAX 64
#define DFS_NFS_MAX_MTU 1024 #define DFS_NFS_MAX_MTU 1024
#ifdef _WIN32 #ifdef _WIN32
...@@ -30,1080 +40,1106 @@ ...@@ -30,1080 +40,1106 @@
struct nfs_file struct nfs_file
{ {
nfs_fh3 handle; /* handle */ nfs_fh3 handle; /* handle */
size_t offset; /* current offset */ size_t offset; /* current offset */
size_t size; /* total size */ size_t size; /* total size */
bool_t eof; /* end of file */ bool_t eof; /* end of file */
}; };
struct nfs_dir struct nfs_dir
{ {
nfs_fh3 handle; nfs_fh3 handle;
cookie3 cookie; cookie3 cookie;
cookieverf3 cookieverf; cookieverf3 cookieverf;
entry3 *entry; entry3 *entry;
bool_t eof; bool_t eof;
READDIR3res res; READDIR3res res;
}; };
#define HOST_LENGTH 32 #define HOST_LENGTH 32
#define EXPORT_PATH_LENGTH 32 #define EXPORT_PATH_LENGTH 32
struct nfs_filesystem struct nfs_filesystem
{ {
nfs_fh3 root_handle; nfs_fh3 root_handle;
nfs_fh3 current_handle; nfs_fh3 current_handle;
CLIENT *nfs_client; CLIENT *nfs_client;
CLIENT *mount_client; CLIENT *mount_client;
char host[HOST_LENGTH]; char host[HOST_LENGTH];
char export[EXPORT_PATH_LENGTH]; char export[EXPORT_PATH_LENGTH];
}; };
typedef struct nfs_file nfs_file; typedef struct nfs_file nfs_file;
typedef struct nfs_dir nfs_dir; typedef struct nfs_dir nfs_dir;
nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path); nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path);
static int nfs_parse_host_export(const char *host_export, static int nfs_parse_host_export(const char *host_export,
char *host, size_t host_len, char *host,
char *export, size_t export_len) size_t host_len,
char *export,
size_t export_len)
{ {
int index; int index;
for (index = 0; index < host_len; index ++) for (index = 0; index < host_len; index ++)
{ {
/* it's end of string, failed */ /* it's end of string, failed */
if (host_export[index] == 0) if (host_export[index] == 0)
return -1; return -1;
/* copy to host buffer */ /* copy to host buffer */
if (host_export[index] != ':') if (host_export[index] != ':')
host[index] = host_export[index]; host[index] = host_export[index];
else else
break; break;
} }
/* host buffer is not enough, failed */ /* host buffer is not enough, failed */
if (index == host_len) if (index == host_len)
return -1; return -1;
/* make RT_NULL */ /* make RT_NULL */
host_len = index; host_len = index;
host[host_len] = '\0'; host[host_len] = '\0';
host_len ++; host_len ++;
/* copy export path */ /* copy export path */
for (index = host_len; index < host_len + export_len; index ++) for (index = host_len; index < host_len + export_len; index ++)
{ {
if (host_export[index] == 0) if (host_export[index] == 0)
{ {
export[index - host_len] = '\0'; export[index - host_len] = '\0';
return 0;
} return 0;
}
export[index - host_len] = host_export[index];
} export[index - host_len] = host_export[index];
}
return -1;
return -1;
} }
static void copy_handle(nfs_fh3 *dest, const nfs_fh3 *source) static void copy_handle(nfs_fh3 *dest, const nfs_fh3 *source)
{ {
dest->data.data_len = source->data.data_len; dest->data.data_len = source->data.data_len;
dest->data.data_val = rt_malloc(dest->data.data_len); dest->data.data_val = rt_malloc(dest->data.data_len);
if (dest->data.data_val == RT_NULL) if (dest->data.data_val == RT_NULL)
{ {
dest->data.data_len = 0; dest->data.data_len = 0;
return;
} return;
}
memcpy(dest->data.data_val, source->data.data_val, dest->data.data_len);
memcpy(dest->data.data_val, source->data.data_val, dest->data.data_len);
} }
static nfs_fh3 *get_handle(struct nfs_filesystem *nfs, const char *name) static nfs_fh3 *get_handle(struct nfs_filesystem *nfs, const char *name)
{ {
nfs_fh3 *handle = RT_NULL; nfs_fh3 *handle = RT_NULL;
char *file; char *file;
char *path; char *path;
char *init; char *init;
init = path = rt_malloc(strlen(name)+1); init = path = rt_malloc(strlen(name)+1);
if (init == RT_NULL) if (init == RT_NULL)
return RT_NULL; return RT_NULL;
memcpy(init, name, strlen(name)+1); memcpy(init, name, strlen(name)+1);
handle = rt_malloc(sizeof(nfs_fh3)); handle = rt_malloc(sizeof(nfs_fh3));
if (handle == RT_NULL) if (handle == RT_NULL)
{ {
rt_free(init); rt_free(init);
return RT_NULL;
} return RT_NULL;
}
if (path[0] == '/')
{ if (path[0] == '/')
path ++; {
copy_handle(handle, &nfs->root_handle); path ++;
} copy_handle(handle, &nfs->root_handle);
else }
{ else
copy_handle(handle, &nfs->current_handle); {
} copy_handle(handle, &nfs->current_handle);
}
while ((file = strtok_r(RT_NULL, "/", &path)) != RT_NULL)
{ while ((file = strtok_r(RT_NULL, "/", &path)) != RT_NULL)
LOOKUP3args args; {
LOOKUP3res res; LOOKUP3args args;
memset(&res, 0, sizeof(res)); LOOKUP3res res;
copy_handle(&args.what.dir, handle); memset(&res, 0, sizeof(res));
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); copy_handle(&args.what.dir, handle);
args.what.name = file; xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
args.what.name = file;
if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
rt_kprintf("Lookup failed\n"); {
rt_free(init); rt_kprintf("Lookup failed\n");
rt_free(handle); rt_free(init);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir); rt_free(handle);
return RT_NULL; xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
}
else if (res.status != NFS3_OK) return RT_NULL;
{ }
rt_kprintf("Lookup failed: %d\n", res.status); else if (res.status != NFS3_OK)
rt_free(init); {
rt_free(handle); rt_kprintf("Lookup failed: %d\n", res.status);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir); rt_free(init);
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res); rt_free(handle);
return RT_NULL; xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
} xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
copy_handle(handle, &res.LOOKUP3res_u.resok.object);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir); return RT_NULL;
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res); }
} copy_handle(handle, &res.LOOKUP3res_u.resok.object);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
rt_free(init); xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
return handle; }
rt_free(init);
return handle;
} }
static nfs_fh3 *get_dir_handle(struct nfs_filesystem *nfs, const char *name) static nfs_fh3 *get_dir_handle(struct nfs_filesystem *nfs, const char *name)
{ {
nfs_fh3 *handle = RT_NULL; nfs_fh3 *handle = RT_NULL;
char *file; char *file;
char *path; char *path;
char *init; char *init;
init = path = rt_malloc(strlen(name)+1); init = path = rt_malloc(strlen(name)+1);
if (init == RT_NULL) if (init == RT_NULL)
return RT_NULL; return RT_NULL;
memcpy(init, name, strlen(name)+1); memcpy(init, name, strlen(name)+1);
handle = rt_malloc(sizeof(nfs_fh3)); handle = rt_malloc(sizeof(nfs_fh3));
if (handle == RT_NULL) if (handle == RT_NULL)
{ {
rt_free(init); rt_free(init);
return RT_NULL;
} return RT_NULL;
}
if (path[0] == '/')
{ if (path[0] == '/')
path ++; {
copy_handle(handle, &nfs->root_handle); path ++;
} copy_handle(handle, &nfs->root_handle);
else }
{ else
copy_handle(handle, &nfs->current_handle); {
} copy_handle(handle, &nfs->current_handle);
}
while ((file = strtok_r(RT_NULL, "/", &path)) != RT_NULL && path != RT_NULL)
{ while ((file = strtok_r(RT_NULL, "/", &path)) != RT_NULL && path != RT_NULL)
LOOKUP3args args; {
LOOKUP3res res; LOOKUP3args args;
memset(&res, 0, sizeof(res)); LOOKUP3res res;
copy_handle(&args.what.dir, handle); memset(&res, 0, sizeof(res));
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); copy_handle(&args.what.dir, handle);
args.what.name = file; xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
args.what.name = file;
if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
rt_kprintf("Lookup failed\n"); {
rt_free(init); rt_kprintf("Lookup failed\n");
rt_free(handle); rt_free(init);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir); rt_free(handle);
return RT_NULL; xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
}
else if (res.status != NFS3_OK) return RT_NULL;
{ }
rt_kprintf("Lookup failed: %d\n", res.status); else if (res.status != NFS3_OK)
rt_free(init); {
rt_free(handle); rt_kprintf("Lookup failed: %d\n", res.status);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir); rt_free(init);
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res); rt_free(handle);
return RT_NULL; xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
} xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
copy_handle(handle, &res.LOOKUP3res_u.resok.object);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir); return RT_NULL;
xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res); }
} copy_handle(handle, &res.LOOKUP3res_u.resok.object);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
rt_free(init); xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
return handle; }
rt_free(init);
return handle;
} }
static size_t nfs_get_filesize(struct nfs_filesystem *nfs, nfs_fh3 *handle) static size_t nfs_get_filesize(struct nfs_filesystem *nfs, nfs_fh3 *handle)
{ {
GETATTR3args args; GETATTR3args args;
GETATTR3res res; GETATTR3res res;
fattr3 *info; fattr3 *info;
size_t size; size_t size;
args.object = *handle; args.object = *handle;
memset(&res, '\0', sizeof(res)); memset(&res, '\0', sizeof(res));
if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS) || if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS) ||
res.status != NFS3_OK) res.status != NFS3_OK)
{ {
rt_kprintf("GetAttr failed: %d\n", res.status); rt_kprintf("GetAttr failed: %d\n", res.status);
return 0;
} return 0;
}
info = &res.GETATTR3res_u.resok.obj_attributes;
size = info->size; info = &res.GETATTR3res_u.resok.obj_attributes;
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res); size = info->size;
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
return size;
return size;
} }
rt_bool_t nfs_is_directory(struct nfs_filesystem *nfs, const char *name) rt_bool_t nfs_is_directory(struct nfs_filesystem *nfs, const char *name)
{ {
GETATTR3args args; GETATTR3args args;
GETATTR3res res; GETATTR3res res;
fattr3 *info; fattr3 *info;
nfs_fh3 *handle; nfs_fh3 *handle;
rt_bool_t result; rt_bool_t result;
result = RT_FALSE; result = RT_FALSE;
handle = get_handle(nfs, name); handle = get_handle(nfs, name);
if (handle == RT_NULL) if (handle == RT_NULL)
return RT_FALSE; return RT_FALSE;
args.object = *handle; args.object = *handle;
memset(&res, '\0', sizeof(res)); memset(&res, '\0', sizeof(res));
if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("GetAttr failed\n"); rt_kprintf("GetAttr failed\n");
return RT_FALSE;
} return RT_FALSE;
else if (res.status != NFS3_OK) }
{ else if (res.status != NFS3_OK)
rt_kprintf("Getattr failed: %d\n", res.status); {
return RT_FALSE; rt_kprintf("Getattr failed: %d\n", res.status);
}
return RT_FALSE;
info=&res.GETATTR3res_u.resok.obj_attributes; }
if (info->type == NFS3DIR) info=&res.GETATTR3res_u.resok.obj_attributes;
result = RT_TRUE;
if (info->type == NFS3DIR)
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res); result = RT_TRUE;
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
return result; rt_free(handle);
return result;
} }
int nfs_create(struct nfs_filesystem *nfs, const char *name, mode_t mode) int nfs_create(struct nfs_filesystem *nfs, const char *name, mode_t mode)
{ {
CREATE3args args; CREATE3args args;
CREATE3res res; CREATE3res res;
int ret = 0; int ret = 0;
nfs_fh3 *handle; nfs_fh3 *handle;
if (nfs->nfs_client == RT_NULL) if (nfs->nfs_client == RT_NULL)
{ {
return -1; return -1;
} }
handle = get_dir_handle(nfs, name); handle = get_dir_handle(nfs, name);
if (handle == RT_NULL) if (handle == RT_NULL)
{ {
return -1; return -1;
} }
args.where.dir = *handle; args.where.dir = *handle;
args.where.name = strrchr(name, '/') + 1; args.where.name = strrchr(name, '/') + 1;
if (args.where.name == RT_NULL) if (args.where.name == RT_NULL)
{ {
args.where.name = (char *)name; args.where.name = (char *)name;
} }
args.how.mode = GUARDED; args.how.mode = GUARDED;
args.how.createhow3_u.obj_attributes.mode.set_it = TRUE; 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.mode.set_mode3_u.mode = mode;
args.how.createhow3_u.obj_attributes.uid.set_it = FALSE; 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.gid.set_it = FALSE;
args.how.createhow3_u.obj_attributes.size.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.atime.set_it = DONT_CHANGE;
args.how.createhow3_u.obj_attributes.mtime.set_it = DONT_CHANGE; args.how.createhow3_u.obj_attributes.mtime.set_it = DONT_CHANGE;
memset(&res, 0, sizeof(res)); memset(&res, 0, sizeof(res));
if (nfsproc3_create_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_create_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Create failed\n"); rt_kprintf("Create failed\n");
ret = -1; ret = -1;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Create failed: %d\n", res.status); rt_kprintf("Create failed: %d\n", res.status);
ret = -1; ret = -1;
} }
xdr_free((xdrproc_t)xdr_CREATE3res, (char *)&res); xdr_free((xdrproc_t)xdr_CREATE3res, (char *)&res);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); rt_free(handle);
return ret; return ret;
} }
int nfs_mkdir(struct nfs_filesystem *nfs, const char *name, mode_t mode) int nfs_mkdir(struct nfs_filesystem *nfs, const char *name, mode_t mode)
{ {
MKDIR3args args; MKDIR3args args;
MKDIR3res res; MKDIR3res res;
int ret = 0; int ret = 0;
nfs_fh3 *handle; nfs_fh3 *handle;
if (nfs->nfs_client == RT_NULL) if (nfs->nfs_client == RT_NULL)
return -1; return -1;
handle = get_dir_handle(nfs, name); handle = get_dir_handle(nfs, name);
if (handle == RT_NULL) if (handle == RT_NULL)
return -1; return -1;
args.where.dir = *handle; args.where.dir = *handle;
args.where.name = strrchr(name, '/') + 1; args.where.name = strrchr(name, '/') + 1;
if (args.where.name == RT_NULL) if (args.where.name == RT_NULL)
{ {
args.where.name = (char *)name; args.where.name = (char *)name;
} }
args.attributes.mode.set_it = TRUE; args.attributes.mode.set_it = TRUE;
args.attributes.mode.set_mode3_u.mode = mode; args.attributes.mode.set_mode3_u.mode = mode;
args.attributes.uid.set_it = FALSE; args.attributes.uid.set_it = FALSE;
args.attributes.gid.set_it = FALSE; args.attributes.gid.set_it = FALSE;
args.attributes.size.set_it = FALSE; args.attributes.size.set_it = FALSE;
args.attributes.atime.set_it = DONT_CHANGE; args.attributes.atime.set_it = DONT_CHANGE;
args.attributes.mtime.set_it = DONT_CHANGE; args.attributes.mtime.set_it = DONT_CHANGE;
memset(&res, 0, sizeof(res)); memset(&res, 0, sizeof(res));
if (nfsproc3_mkdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_mkdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Mkdir failed\n"); rt_kprintf("Mkdir failed\n");
ret = -1; ret = -1;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Mkdir failed: %d\n", res.status); rt_kprintf("Mkdir failed: %d\n", res.status);
ret = -1; ret = -1;
} }
xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res); xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); rt_free(handle);
return ret; return ret;
} }
/* mount(RT_NULL, "/mnt", "nfs", 0, "192.168.1.1:/export") */ /* mount(RT_NULL, "/mnt", "nfs", 0, "192.168.1.1:/export") */
int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{ {
mountres3 res; mountres3 res;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
nfs = (struct nfs_filesystem *)rt_malloc(sizeof(struct nfs_filesystem)); nfs = (struct nfs_filesystem *)rt_malloc(sizeof(struct nfs_filesystem));
memset(nfs, 0, sizeof(struct nfs_filesystem)); memset(nfs, 0, sizeof(struct nfs_filesystem));
if (nfs_parse_host_export((const char *)data, nfs->host, HOST_LENGTH, if (nfs_parse_host_export((const char *)data, nfs->host, HOST_LENGTH,
nfs->export, EXPORT_PATH_LENGTH) < 0) nfs->export, EXPORT_PATH_LENGTH) < 0)
{ {
rt_kprintf("host or export path error\n"); rt_kprintf("host or export path error\n");
goto __return; goto __return;
} }
nfs->mount_client=clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp"); nfs->mount_client=clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp");
if (nfs->mount_client == RT_NULL) if (nfs->mount_client == RT_NULL)
{ {
rt_kprintf("create mount client failed\n"); rt_kprintf("create mount client failed\n");
goto __return; goto __return;
} }
memset(&res, '\0', sizeof(mountres3)); memset(&res, '\0', sizeof(mountres3));
if (mountproc3_mnt_3((char *)nfs->export, &res, nfs->mount_client) != RPC_SUCCESS) if (mountproc3_mnt_3((char *)nfs->export, &res, nfs->mount_client) != RPC_SUCCESS)
{ {
rt_kprintf("nfs mount failed\n"); rt_kprintf("nfs mount failed\n");
goto __return; goto __return;
} }
else if (res.fhs_status != MNT3_OK) else if (res.fhs_status != MNT3_OK)
{ {
rt_kprintf("nfs mount failed\n"); rt_kprintf("nfs mount failed\n");
goto __return; goto __return;
} }
nfs->nfs_client=clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp"); nfs->nfs_client=clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp");
if (nfs->nfs_client == RT_NULL) if (nfs->nfs_client == RT_NULL)
{ {
rt_kprintf("creat nfs client failed\n"); rt_kprintf("creat nfs client failed\n");
goto __return; goto __return;
} }
copy_handle(&nfs->root_handle, (nfs_fh3 *)&res.mountres3_u.mountinfo.fhandle); copy_handle(&nfs->root_handle, (nfs_fh3 *)&res.mountres3_u.mountinfo.fhandle);
copy_handle(&nfs->current_handle, &nfs->root_handle); copy_handle(&nfs->current_handle, &nfs->root_handle);
nfs->nfs_client->cl_auth = authnone_create(); nfs->nfs_client->cl_auth = authnone_create();
fs->data = nfs; fs->data = nfs;
return 0; return 0;
__return: __return:
if (nfs != RT_NULL) if (nfs != RT_NULL)
{ {
if (nfs->mount_client != RT_NULL) if (nfs->mount_client != RT_NULL)
{ {
clnt_destroy(nfs->mount_client); clnt_destroy(nfs->mount_client);
} }
if (nfs->nfs_client != RT_NULL) if (nfs->nfs_client != RT_NULL)
{ {
if (nfs->nfs_client->cl_auth != RT_NULL) if (nfs->nfs_client->cl_auth != RT_NULL)
{ {
auth_destroy(nfs->nfs_client->cl_auth); auth_destroy(nfs->nfs_client->cl_auth);
} }
clnt_destroy(nfs->nfs_client); clnt_destroy(nfs->nfs_client);
} }
rt_free(nfs); rt_free(nfs);
} }
return -1; return -1;
} }
int nfs_unmount(struct dfs_filesystem *fs) int nfs_unmount(struct dfs_filesystem *fs)
{ {
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
RT_ASSERT(fs != RT_NULL); RT_ASSERT(fs != RT_NULL);
RT_ASSERT(fs->data != RT_NULL); RT_ASSERT(fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)fs->data; nfs = (struct nfs_filesystem *)fs->data;
if (nfs->mount_client != RT_NULL && if (nfs->mount_client != RT_NULL &&
mountproc3_umnt_3((char *)nfs->export, RT_NULL, nfs->mount_client) != RPC_SUCCESS) mountproc3_umnt_3((char *)nfs->export, RT_NULL, nfs->mount_client) != RPC_SUCCESS)
{ {
rt_kprintf("umount failed\n"); rt_kprintf("umount failed\n");
return -1;
} return -1;
}
/* destroy nfs client */
if (nfs->nfs_client != RT_NULL) /* destroy nfs client */
{ if (nfs->nfs_client != RT_NULL)
if (nfs->nfs_client->cl_auth != RT_NULL) {
{ if (nfs->nfs_client->cl_auth != RT_NULL)
auth_destroy(nfs->nfs_client->cl_auth); {
nfs->nfs_client->cl_auth = RT_NULL; auth_destroy(nfs->nfs_client->cl_auth);
} nfs->nfs_client->cl_auth = RT_NULL;
clnt_destroy(nfs->nfs_client); }
nfs->nfs_client = RT_NULL; clnt_destroy(nfs->nfs_client);
} nfs->nfs_client = RT_NULL;
}
/* destroy mount client */
if (nfs->mount_client != RT_NULL) /* destroy mount client */
{ if (nfs->mount_client != RT_NULL)
if (nfs->mount_client->cl_auth != RT_NULL) {
{ if (nfs->mount_client->cl_auth != RT_NULL)
auth_destroy(nfs->mount_client->cl_auth); {
nfs->mount_client->cl_auth = RT_NULL; auth_destroy(nfs->mount_client->cl_auth);
} nfs->mount_client->cl_auth = RT_NULL;
clnt_destroy(nfs->mount_client); }
nfs->mount_client = RT_NULL; clnt_destroy(nfs->mount_client);
} nfs->mount_client = RT_NULL;
}
rt_free(nfs);
fs->data = RT_NULL; rt_free(nfs);
fs->data = RT_NULL;
return 0;
return 0;
} }
int nfs_ioctl(struct dfs_fd *file, int cmd, void *args) int nfs_ioctl(struct dfs_fd *file, int cmd, void *args)
{ {
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;
} }
int nfs_read(struct dfs_fd *file, void *buf, rt_size_t count) int nfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
{ {
READ3args args; READ3args args;
READ3res res; READ3res res;
ssize_t bytes, total=0; ssize_t bytes, total=0;
nfs_file *fd; nfs_file *fd;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
if (file->type == FT_DIRECTORY) if (file->type == FT_DIRECTORY)
return -DFS_STATUS_EISDIR; return -DFS_STATUS_EISDIR;
fd = (nfs_file *)(file->data); fd = (nfs_file *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
RT_ASSERT(file->fs != RT_NULL); RT_ASSERT(file->fs != RT_NULL);
RT_ASSERT(file->fs->data != RT_NULL); RT_ASSERT(file->fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)file->fs->data; nfs = (struct nfs_filesystem *)file->fs->data;
if (nfs->nfs_client == RT_NULL) if (nfs->nfs_client == RT_NULL)
return -1; return -1;
/* end of file */ /* end of file */
if (fd->eof == TRUE) if (fd->eof == TRUE)
return 0; return 0;
args.file = fd->handle; args.file = fd->handle;
do { do {
args.offset = fd->offset; args.offset = fd->offset;
args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count; args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
count -= args.count; count -= args.count;
memset(&res, 0, sizeof(res)); memset(&res, 0, sizeof(res));
if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Read failed\n"); rt_kprintf("Read failed\n");
total = 0; total = 0;
break; break;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Read failed: %d\n", res.status); rt_kprintf("Read failed: %d\n", res.status);
total = 0; total = 0;
break; break;
} }
else else
{ {
bytes = res.READ3res_u.resok.count; bytes = res.READ3res_u.resok.count;
total += bytes; total += bytes;
fd->offset += bytes; fd->offset += bytes;
/* update current position */ /* update current position */
file->pos = fd->offset; file->pos = fd->offset;
memcpy(buf, res.READ3res_u.resok.data.data_val, bytes); memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
buf = (void *)((char *)buf + args.count); buf = (void *)((char *)buf + args.count);
if (res.READ3res_u.resok.eof) if (res.READ3res_u.resok.eof)
{ {
/* something should probably be here */ /* something should probably be here */
fd->eof = TRUE; fd->eof = TRUE;
break; break;
} }
} }
xdr_free((xdrproc_t)xdr_READ3res, (char *)&res); xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
} while(count > 0); } while(count > 0);
xdr_free((xdrproc_t)xdr_READ3res, (char *)&res); xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
return total;
return total;
} }
int nfs_write(struct dfs_fd *file, const void *buf, rt_size_t count) int nfs_write(struct dfs_fd *file, const void *buf, rt_size_t count)
{ {
WRITE3args args; WRITE3args args;
WRITE3res res; WRITE3res res;
ssize_t bytes, total=0; ssize_t bytes, total=0;
nfs_file *fd; nfs_file *fd;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
if (file->type == FT_DIRECTORY) if (file->type == FT_DIRECTORY)
return -DFS_STATUS_EISDIR; return -DFS_STATUS_EISDIR;
fd = (nfs_file *)(file->data); fd = (nfs_file *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
RT_ASSERT(file->fs != RT_NULL); RT_ASSERT(file->fs != RT_NULL);
RT_ASSERT(file->fs->data != RT_NULL); RT_ASSERT(file->fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)file->fs->data; nfs = (struct nfs_filesystem *)file->fs->data;
if (nfs->nfs_client == RT_NULL) if (nfs->nfs_client == RT_NULL)
return -1; return -1;
args.file = fd->handle; args.file = fd->handle;
args.stable = FILE_SYNC; args.stable = FILE_SYNC;
do { do {
args.offset = fd->offset; args.offset = fd->offset;
memset(&res, 0, sizeof(res)); memset(&res, 0, sizeof(res));
args.data.data_val=(void *)buf; args.data.data_val=(void *)buf;
args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count; args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
args.data.data_len = args.count; args.data.data_len = args.count;
count -= args.count; count -= args.count;
buf = (const void *)((char *)buf + args.count); buf = (const void *)((char *)buf + args.count);
if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Write failed\n"); rt_kprintf("Write failed\n");
total = 0; total = 0;
break; break;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Write failed: %d\n", res.status); rt_kprintf("Write failed: %d\n", res.status);
total = 0; total = 0;
break; break;
} }
else else
{ {
bytes = res.WRITE3res_u.resok.count; bytes = res.WRITE3res_u.resok.count;
fd->offset += bytes; fd->offset += bytes;
total += bytes; total += bytes;
/* update current position */ /* update current position */
file->pos = fd->offset; file->pos = fd->offset;
/* todo: update file size */ /* todo: update file size */
} }
xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
} while (count > 0); } while (count > 0);
xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
return total;
return total;
} }
int nfs_lseek(struct dfs_fd *file, rt_off_t offset) int nfs_lseek(struct dfs_fd *file, rt_off_t offset)
{ {
nfs_file *fd; nfs_file *fd;
if (file->type == FT_DIRECTORY) if (file->type == FT_DIRECTORY)
return -DFS_STATUS_EISDIR; return -DFS_STATUS_EISDIR;
fd = (nfs_file *)(file->data); fd = (nfs_file *)(file->data);
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
if (offset <= fd->size) if (offset <= fd->size)
{ {
fd->offset = offset; fd->offset = offset;
return offset;
}
return -DFS_STATUS_EIO; return offset;
}
return -DFS_STATUS_EIO;
} }
int nfs_close(struct dfs_fd *file) int nfs_close(struct dfs_fd *file)
{ {
if (file->type == FT_DIRECTORY) if (file->type == FT_DIRECTORY)
{ {
struct nfs_dir *dir; struct nfs_dir *dir;
dir = (struct nfs_dir *)file->data; dir = (struct nfs_dir *)file->data;
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res); xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
rt_free(dir); rt_free(dir);
} }
else if (file->type == FT_REGULAR) else if (file->type == FT_REGULAR)
{ {
struct nfs_file *fd; struct nfs_file *fd;
fd = (struct nfs_file *)file->data; fd = (struct nfs_file *)file->data;
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
rt_free(fd); rt_free(fd);
} }
file->data = RT_NULL; file->data = RT_NULL;
return 0; return 0;
} }
int nfs_open(struct dfs_fd *file) int nfs_open(struct dfs_fd *file)
{ {
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
RT_ASSERT(file->fs != RT_NULL); RT_ASSERT(file->fs != RT_NULL);
RT_ASSERT(file->fs->data != RT_NULL); RT_ASSERT(file->fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)file->fs->data; nfs = (struct nfs_filesystem *)file->fs->data;
if (file->flags & DFS_O_DIRECTORY) if (file->flags & DFS_O_DIRECTORY)
{ {
nfs_dir *dir; nfs_dir *dir;
if (file->flags & DFS_O_CREAT) if (file->flags & DFS_O_CREAT)
{ {
if (nfs_mkdir(nfs, file->path, 0755) < 0) if (nfs_mkdir(nfs, file->path, 0755) < 0)
return -1; return -1;
} }
/* open directory */ /* open directory */
dir = nfs_opendir(nfs, file->path); dir = nfs_opendir(nfs, file->path);
file->data = dir; file->data = dir;
} }
else else
{ {
nfs_file *fp; nfs_file *fp;
nfs_fh3 *handle; nfs_fh3 *handle;
/* create file */ /* create file */
if (file->flags & DFS_O_CREAT) if (file->flags & DFS_O_CREAT)
{ {
if (nfs_create(nfs, file->path, 0664) < 0) return -1; if (nfs_create(nfs, file->path, 0664) < 0)
} return -1;
}
/* open file (get file handle ) */
fp = rt_malloc(sizeof(nfs_file)); /* open file (get file handle ) */
if (fp == RT_NULL) fp = rt_malloc(sizeof(nfs_file));
return -1; if (fp == RT_NULL)
return -1;
handle = get_handle(nfs, file->path);
if (handle == RT_NULL) handle = get_handle(nfs, file->path);
{ if (handle == RT_NULL)
rt_free(fp); {
return -1; rt_free(fp);
}
return -1;
/* get size of file */ }
fp->size = nfs_get_filesize(nfs, handle);
fp->offset = 0; /* get size of file */
fp->eof = FALSE; fp->size = nfs_get_filesize(nfs, handle);
fp->offset = 0;
copy_handle(&fp->handle, handle); fp->eof = FALSE;
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); copy_handle(&fp->handle, handle);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
if (file->flags & DFS_O_APPEND) rt_free(handle);
{
fp->offset = fp->size; if (file->flags & DFS_O_APPEND)
} {
fp->offset = fp->size;
/* set private file */ }
file->data = fp;
} /* set private file */
file->data = fp;
return 0; }
return 0;
} }
int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{ {
GETATTR3args args; GETATTR3args args;
GETATTR3res res; GETATTR3res res;
fattr3 *info; fattr3 *info;
nfs_fh3 *handle; nfs_fh3 *handle;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
RT_ASSERT(fs != RT_NULL); RT_ASSERT(fs != RT_NULL);
RT_ASSERT(fs->data != RT_NULL); RT_ASSERT(fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)fs->data; nfs = (struct nfs_filesystem *)fs->data;
handle = get_handle(nfs, path); handle = get_handle(nfs, path);
if (handle == RT_NULL) if (handle == RT_NULL)
return -1; return -1;
args.object = *handle; args.object = *handle;
memset(&res, '\0', sizeof(res)); memset(&res, '\0', sizeof(res));
if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("GetAttr failed\n"); rt_kprintf("GetAttr failed\n");
return -1; return -1;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Getattr failed: %d\n", res.status); rt_kprintf("Getattr failed: %d\n", res.status);
return -1; return -1;
} }
info = &res.GETATTR3res_u.resok.obj_attributes; info = &res.GETATTR3res_u.resok.obj_attributes;
st->st_dev = 0; st->st_dev = 0;
st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
if (info->type == NFS3DIR) if (info->type == NFS3DIR)
{ {
st->st_mode &= ~DFS_S_IFREG; st->st_mode &= ~DFS_S_IFREG;
st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
} }
st->st_size = info->size; st->st_size = info->size;
st->st_mtime = info->mtime.seconds; st->st_mtime = info->mtime.seconds;
st->st_blksize = 512; st->st_blksize = 512;
xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res); xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); rt_free(handle);
return 0; return 0;
} }
nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path) nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path)
{ {
nfs_dir *dir; nfs_dir *dir;
nfs_fh3 *handle; nfs_fh3 *handle;
dir = rt_malloc(sizeof(nfs_dir)); dir = rt_malloc(sizeof(nfs_dir));
if (dir == RT_NULL) if (dir == RT_NULL)
{ {
return RT_NULL; return RT_NULL;
} }
handle = get_handle(nfs, path); handle = get_handle(nfs, path);
if (handle == RT_NULL) if (handle == RT_NULL)
{ {
rt_free(dir); rt_free(dir);
return RT_NULL;
} return RT_NULL;
}
copy_handle(&dir->handle, handle);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); copy_handle(&dir->handle, handle);
rt_free(handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle);
dir->cookie = 0;
memset(&dir->cookieverf, '\0', sizeof(cookieverf3)); dir->cookie = 0;
dir->entry = RT_NULL; memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
dir->eof = FALSE; dir->entry = RT_NULL;
memset(&dir->res, '\0', sizeof(dir->res)); dir->eof = FALSE;
memset(&dir->res, '\0', sizeof(dir->res));
return dir;
return dir;
} }
char *nfs_readdir(struct nfs_filesystem *nfs, nfs_dir *dir) char *nfs_readdir(struct nfs_filesystem *nfs, nfs_dir *dir)
{ {
static char name[NAME_MAX]; static char name[NAME_MAX];
if (nfs->nfs_client == RT_NULL || dir == RT_NULL) if (nfs->nfs_client == RT_NULL || dir == RT_NULL)
return RT_NULL; return RT_NULL;
if (dir->entry == RT_NULL) if (dir->entry == RT_NULL)
{ {
READDIR3args args; READDIR3args args;
xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res); xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
memset(&dir->res, '\0', sizeof(dir->res)); memset(&dir->res, '\0', sizeof(dir->res));
args.dir = dir->handle; args.dir = dir->handle;
args.cookie = dir->cookie; args.cookie = dir->cookie;
memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3)); memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
args.count = 1024; args.count = 1024;
if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Readdir failed\n"); rt_kprintf("Readdir failed\n");
return RT_NULL;
} return RT_NULL;
else if (dir->res.status != NFS3_OK) }
{ else if (dir->res.status != NFS3_OK)
rt_kprintf("Readdir failed: %d\n", dir->res.status); {
return RT_NULL; rt_kprintf("Readdir failed: %d\n", dir->res.status);
}
return RT_NULL;
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; memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
} dir->eof = dir->res.READDIR3res_u.resok.reply.eof;
if (dir->eof == TRUE && dir->entry == RT_NULL) dir->entry = dir->res.READDIR3res_u.resok.reply.entries;
return RT_NULL; }
if (dir->eof == TRUE && dir->entry == RT_NULL)
dir->cookie = dir->entry->cookie; return RT_NULL;
strncpy(name, dir->entry->name, NAME_MAX-1);
dir->entry = dir->entry->nextentry; dir->cookie = dir->entry->cookie;
name[NAME_MAX - 1] = '\0'; strncpy(name, dir->entry->name, NAME_MAX-1);
return name; dir->entry = dir->entry->nextentry;
name[NAME_MAX - 1] = '\0';
return name;
} }
int nfs_unlink(struct dfs_filesystem *fs, const char *path) int nfs_unlink(struct dfs_filesystem *fs, const char *path)
{ {
int ret = 0; int ret = 0;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
RT_ASSERT(fs != RT_NULL); RT_ASSERT(fs != RT_NULL);
RT_ASSERT(fs->data != RT_NULL); RT_ASSERT(fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)fs->data; nfs = (struct nfs_filesystem *)fs->data;
if (nfs_is_directory(nfs, path) == RT_FALSE) if (nfs_is_directory(nfs, path) == RT_FALSE)
{ {
/* remove file */ /* remove file */
REMOVE3args args; REMOVE3args args;
REMOVE3res res; REMOVE3res res;
nfs_fh3 *handle; nfs_fh3 *handle;
handle = get_dir_handle(nfs, path); handle = get_dir_handle(nfs, path);
if (handle == RT_NULL) if (handle == RT_NULL)
return -1; return -1;
args.object.dir = *handle; args.object.dir = *handle;
args.object.name = strrchr(path, '/') + 1; args.object.name = strrchr(path, '/') + 1;
if (args.object.name == RT_NULL) if (args.object.name == RT_NULL)
{ {
args.object.name = (char *)path; args.object.name = (char *)path;
} }
memset(&res, 0, sizeof(res)); memset(&res, 0, sizeof(res));
if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Remove failed\n"); rt_kprintf("Remove failed\n");
ret = -1; ret = -1;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Remove failed: %d\n", res.status); rt_kprintf("Remove failed: %d\n", res.status);
ret = -1; ret = -1;
} }
xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res); xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); rt_free(handle);
} }
else else
{ {
/* remove directory */ /* remove directory */
RMDIR3args args; RMDIR3args args;
RMDIR3res res; RMDIR3res res;
nfs_fh3 *handle; nfs_fh3 *handle;
handle = get_dir_handle(nfs, path); handle = get_dir_handle(nfs, path);
if (handle == RT_NULL) if (handle == RT_NULL)
return -1; return -1;
args.object.dir = *handle; args.object.dir = *handle;
args.object.name = strrchr(path, '/') + 1; args.object.name = strrchr(path, '/') + 1;
if (args.object.name == RT_NULL) if (args.object.name == RT_NULL)
{ {
args.object.name = (char *)path; args.object.name = (char *)path;
} }
memset(&res, 0, sizeof(res)); memset(&res, 0, sizeof(res));
if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Rmdir failed\n"); rt_kprintf("Rmdir failed\n");
ret = -1; ret = -1;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Rmdir failed: %d\n", res.status); rt_kprintf("Rmdir failed: %d\n", res.status);
ret = -1; ret = -1;
} }
xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res); xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
rt_free(handle); rt_free(handle);
} }
return ret; return ret;
} }
int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest) int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest)
{ {
RENAME3args args; RENAME3args args;
RENAME3res res; RENAME3res res;
nfs_fh3 *sHandle; nfs_fh3 *sHandle;
nfs_fh3 *dHandle; nfs_fh3 *dHandle;
int ret = 0; int ret = 0;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
RT_ASSERT(fs != RT_NULL); RT_ASSERT(fs != RT_NULL);
RT_ASSERT(fs->data != RT_NULL); RT_ASSERT(fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)fs->data; nfs = (struct nfs_filesystem *)fs->data;
if (nfs->nfs_client == RT_NULL) if (nfs->nfs_client == RT_NULL)
return -1; return -1;
sHandle = get_dir_handle(nfs, src); sHandle = get_dir_handle(nfs, src);
if (sHandle == RT_NULL) if (sHandle == RT_NULL)
return -1; return -1;
dHandle = get_dir_handle(nfs, dest); dHandle = get_dir_handle(nfs, dest);
if (dHandle == RT_NULL) if (dHandle == RT_NULL)
return -1; return -1;
args.from.dir = *sHandle; args.from.dir = *sHandle;
args.from.name = strrchr(src, '/') + 1; args.from.name = strrchr(src, '/') + 1;
if (args.from.name == RT_NULL) if (args.from.name == RT_NULL)
args.from.name = (char *)src; args.from.name = (char *)src;
args.to.dir = *dHandle; args.to.dir = *dHandle;
args.to.name = strrchr(src, '/') + 1; args.to.name = strrchr(src, '/') + 1;
if (args.to.name == RT_NULL) if (args.to.name == RT_NULL)
args.to.name = (char *)dest; args.to.name = (char *)dest;
memset(&res, '\0', sizeof(res)); memset(&res, '\0', sizeof(res));
if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
{ {
rt_kprintf("Rename failed\n"); rt_kprintf("Rename failed\n");
ret = -1; ret = -1;
} }
else if (res.status != NFS3_OK) else if (res.status != NFS3_OK)
{ {
rt_kprintf("Rename failed: %d\n", res.status); rt_kprintf("Rename failed: %d\n", res.status);
ret = -1; ret = -1;
} }
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res); xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
return ret;
return ret;
} }
int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count) int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
{ {
nfs_dir *dir; nfs_dir *dir;
rt_uint32_t index; rt_uint32_t index;
struct dirent *d; struct dirent *d;
struct nfs_filesystem *nfs; struct nfs_filesystem *nfs;
char *name; char *name;
dir = (nfs_dir *)(file->data); dir = (nfs_dir *)(file->data);
RT_ASSERT(dir != RT_NULL); RT_ASSERT(dir != RT_NULL);
RT_ASSERT(file->fs != RT_NULL); RT_ASSERT(file->fs != RT_NULL);
RT_ASSERT(file->fs->data != RT_NULL); RT_ASSERT(file->fs->data != RT_NULL);
nfs = (struct nfs_filesystem *)file->fs->data; nfs = (struct nfs_filesystem *)file->fs->data;
/* make integer count */ /* make integer count */
count = (count / sizeof(struct dirent)) * sizeof(struct dirent); count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
if (count == 0) if (count == 0)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
index = 0; index = 0;
while (1) while (1)
{ {
char *fn; char *fn;
d = dirp + index; d = dirp + index;
name = nfs_readdir(nfs, dir); name = nfs_readdir(nfs, dir);
if (name == RT_NULL) if (name == RT_NULL)
break; break;
d->d_type = DFS_DT_REG; d->d_type = DFS_DT_REG;
d->d_namlen = rt_strlen(name); d->d_namlen = rt_strlen(name);
d->d_reclen = (rt_uint16_t)sizeof(struct dirent); d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
rt_strncpy(d->d_name, name, rt_strlen(name) + 1); rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
index ++; index ++;
if (index * sizeof(struct dirent) >= count) if (index * sizeof(struct dirent) >= count)
break; break;
} }
return index * sizeof(struct dirent); return index * sizeof(struct dirent);
} }
static const struct dfs_filesystem_operation _nfs = static const struct dfs_filesystem_operation _nfs =
{ {
"nfs", "nfs",
DFS_FS_FLAG_DEFAULT, DFS_FS_FLAG_DEFAULT,
nfs_mount, nfs_mount,
nfs_unmount, nfs_unmount,
RT_NULL, /* mkfs */ RT_NULL, /* mkfs */
RT_NULL, /* statfs */ RT_NULL, /* statfs */
nfs_open, nfs_open,
nfs_close, nfs_close,
nfs_ioctl, nfs_ioctl,
nfs_read, nfs_read,
nfs_write, nfs_write,
RT_NULL, /* flush */ RT_NULL, /* flush */
nfs_lseek, nfs_lseek,
nfs_getdents, nfs_getdents,
nfs_unlink, nfs_unlink,
nfs_stat, nfs_stat,
nfs_rename, nfs_rename,
}; };
int nfs_init(void) int nfs_init(void)
{ {
/* register fatfs file system */ /* register fatfs file system */
dfs_register(&_nfs); dfs_register(&_nfs);
return RT_EOK; return RT_EOK;
} }
INIT_FS_EXPORT(nfs_init); INIT_FS_EXPORT(nfs_init);
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
/* /*
* File : dfs_ramfs.c * File : dfs_ramfs.c
* This file is part of RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2011, Shanghai Real-Thread Technology Co., Ltd * COPYRIGHT (C) 2004-2013, RT-Thread Development Team
* *
* All rights reserved. * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
...@@ -17,11 +29,14 @@ ...@@ -17,11 +29,14 @@
#include <dfs_fs.h> #include <dfs_fs.h>
#include "dfs_ramfs.h" #include "dfs_ramfs.h"
int dfs_ramfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) int dfs_ramfs_mount(struct dfs_filesystem *fs,
unsigned long rwflag,
const void *data)
{ {
struct dfs_ramfs* ramfs; struct dfs_ramfs* ramfs;
if (data == RT_NULL) return -DFS_STATUS_EIO; if (data == RT_NULL)
return -DFS_STATUS_EIO;
ramfs = (struct dfs_ramfs*) data; ramfs = (struct dfs_ramfs*) data;
fs->data = ramfs; fs->data = ramfs;
...@@ -55,7 +70,9 @@ int dfs_ramfs_ioctl(struct dfs_fd *file, int cmd, void *args) ...@@ -55,7 +70,9 @@ int dfs_ramfs_ioctl(struct dfs_fd *file, int cmd, void *args)
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
struct ramfs_dirent *dfs_ramfs_lookup(struct dfs_ramfs* ramfs, const char *path, rt_size_t *size) struct ramfs_dirent *dfs_ramfs_lookup(struct dfs_ramfs *ramfs,
const char *path,
rt_size_t *size)
{ {
const char *subpath; const char *subpath;
struct ramfs_dirent *dirent; struct ramfs_dirent *dirent;
...@@ -65,16 +82,18 @@ struct ramfs_dirent *dfs_ramfs_lookup(struct dfs_ramfs* ramfs, const char *path, ...@@ -65,16 +82,18 @@ struct ramfs_dirent *dfs_ramfs_lookup(struct dfs_ramfs* ramfs, const char *path,
if (! *subpath) /* is root directory */ if (! *subpath) /* is root directory */
{ {
*size = 0; *size = 0;
return &(ramfs->root); return &(ramfs->root);
} }
for (dirent = rt_list_entry(ramfs->root.list.next, struct ramfs_dirent, list); for (dirent = rt_list_entry(ramfs->root.list.next, struct ramfs_dirent, list);
dirent != &(ramfs->root); dirent != &(ramfs->root);
dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list)) dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list))
{ {
if (rt_strcmp(dirent->name, subpath) == 0) if (rt_strcmp(dirent->name, subpath) == 0)
{ {
*size = dirent->size; *size = dirent->size;
return dirent; return dirent;
} }
} }
...@@ -122,6 +141,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, rt_size_t count) ...@@ -122,6 +141,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, rt_size_t count)
if (ptr == RT_NULL) if (ptr == RT_NULL)
{ {
rt_set_errno(-RT_ENOMEM); rt_set_errno(-RT_ENOMEM);
return 0; return 0;
} }
...@@ -145,6 +165,7 @@ int dfs_ramfs_lseek(struct dfs_fd *file, rt_off_t offset) ...@@ -145,6 +165,7 @@ int dfs_ramfs_lseek(struct dfs_fd *file, rt_off_t offset)
if (offset <= (rt_off_t)file->size) if (offset <= (rt_off_t)file->size)
{ {
file->pos = offset; file->pos = offset;
return file->pos; return file->pos;
} }
...@@ -154,6 +175,7 @@ int dfs_ramfs_lseek(struct dfs_fd *file, rt_off_t offset) ...@@ -154,6 +175,7 @@ int dfs_ramfs_lseek(struct dfs_fd *file, rt_off_t offset)
int dfs_ramfs_close(struct dfs_fd *file) int dfs_ramfs_close(struct dfs_fd *file)
{ {
file->data = RT_NULL; file->data = RT_NULL;
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
...@@ -195,8 +217,7 @@ int dfs_ramfs_open(struct dfs_fd *file) ...@@ -195,8 +217,7 @@ int dfs_ramfs_open(struct dfs_fd *file)
if (dirent == RT_NULL) if (dirent == RT_NULL)
{ {
if (file->flags & DFS_O_CREAT || if (file->flags & DFS_O_CREAT || file->flags & DFS_O_WRONLY)
file->flags & DFS_O_WRONLY)
{ {
char *name_ptr; char *name_ptr;
...@@ -209,7 +230,8 @@ int dfs_ramfs_open(struct dfs_fd *file) ...@@ -209,7 +230,8 @@ int dfs_ramfs_open(struct dfs_fd *file)
/* remove '/' separator */ /* remove '/' separator */
name_ptr = file->path; name_ptr = file->path;
while (*name_ptr == '/' && *name_ptr) name_ptr ++; while (*name_ptr == '/' && *name_ptr)
name_ptr ++;
strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX); strncpy(dirent->name, name_ptr, RAMFS_NAME_MAX);
rt_list_init(&(dirent->list)); rt_list_init(&(dirent->list));
...@@ -218,7 +240,8 @@ int dfs_ramfs_open(struct dfs_fd *file) ...@@ -218,7 +240,8 @@ int dfs_ramfs_open(struct dfs_fd *file)
/* add to the root directory */ /* add to the root directory */
rt_list_insert_after(&(ramfs->root.list), &(dirent->list)); rt_list_insert_after(&(ramfs->root.list), &(dirent->list));
} }
else return -DFS_STATUS_ENOENT; else
return -DFS_STATUS_ENOENT;
} }
/* Creates a new file. If the file is existing, it is truncated and overwritten. */ /* Creates a new file. If the file is existing, it is truncated and overwritten. */
...@@ -282,8 +305,8 @@ int dfs_ramfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t cou ...@@ -282,8 +305,8 @@ int dfs_ramfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t cou
index = 0; index = 0;
count = 0; count = 0;
for (dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list); for (dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list);
dirent != &(ramfs->root) && index < end; dirent != &(ramfs->root) && index < end;
dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list)) dirent = rt_list_entry(dirent->list.next, struct ramfs_dirent, list))
{ {
if (index >= (rt_size_t)file->pos) if (index >= (rt_size_t)file->pos)
{ {
...@@ -312,7 +335,8 @@ int dfs_ramfs_unlink(struct dfs_filesystem *fs, const char *path) ...@@ -312,7 +335,8 @@ int dfs_ramfs_unlink(struct dfs_filesystem *fs, const char *path)
RT_ASSERT(ramfs != RT_NULL); RT_ASSERT(ramfs != RT_NULL);
dirent = dfs_ramfs_lookup(ramfs, path, &size); dirent = dfs_ramfs_lookup(ramfs, path, &size);
if (dirent == RT_NULL) return -DFS_STATUS_ENOENT; if (dirent == RT_NULL)
return -DFS_STATUS_ENOENT;
rt_list_remove(&(dirent->list)); rt_list_remove(&(dirent->list));
if (dirent->data != RT_NULL) if (dirent->data != RT_NULL)
...@@ -322,7 +346,9 @@ int dfs_ramfs_unlink(struct dfs_filesystem *fs, const char *path) ...@@ -322,7 +346,9 @@ int dfs_ramfs_unlink(struct dfs_filesystem *fs, const char *path)
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_ramfs_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath) int dfs_ramfs_rename(struct dfs_filesystem *fs,
const char *oldpath,
const char *newpath)
{ {
struct ramfs_dirent *dirent; struct ramfs_dirent *dirent;
struct dfs_ramfs *ramfs; struct dfs_ramfs *ramfs;
...@@ -332,10 +358,12 @@ int dfs_ramfs_rename(struct dfs_filesystem *fs, const char *oldpath, const char ...@@ -332,10 +358,12 @@ int dfs_ramfs_rename(struct dfs_filesystem *fs, const char *oldpath, const char
RT_ASSERT(ramfs != RT_NULL); RT_ASSERT(ramfs != RT_NULL);
dirent = dfs_ramfs_lookup(ramfs, newpath, &size); dirent = dfs_ramfs_lookup(ramfs, newpath, &size);
if (dirent != RT_NULL) return -DFS_STATUS_EEXIST; if (dirent != RT_NULL)
return -DFS_STATUS_EEXIST;
dirent = dfs_ramfs_lookup(ramfs, oldpath, &size); dirent = dfs_ramfs_lookup(ramfs, oldpath, &size);
if (dirent == RT_NULL) return -DFS_STATUS_ENOENT; if (dirent == RT_NULL)
return -DFS_STATUS_ENOENT;
strncpy(dirent->name, newpath, RAMFS_NAME_MAX); strncpy(dirent->name, newpath, RAMFS_NAME_MAX);
...@@ -368,10 +396,11 @@ int dfs_ramfs_init(void) ...@@ -368,10 +396,11 @@ int dfs_ramfs_init(void)
{ {
/* register ram file system */ /* register ram file system */
dfs_register(&_ramfs); dfs_register(&_ramfs);
return 0; return 0;
} }
struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t* pool, rt_size_t size) struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size)
{ {
struct dfs_ramfs *ramfs; struct dfs_ramfs *ramfs;
rt_uint8_t *data_ptr; rt_uint8_t *data_ptr;
...@@ -385,9 +414,10 @@ struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t* pool, rt_size_t size) ...@@ -385,9 +414,10 @@ struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t* pool, rt_size_t size)
size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
result = rt_memheap_init(&ramfs->memheap, "ramfs", data_ptr, size); result = rt_memheap_init(&ramfs->memheap, "ramfs", data_ptr, size);
if (result != RT_EOK) return RT_NULL; if (result != RT_EOK)
/* detach this memheap object from the system */ return RT_NULL;
rt_object_detach((rt_object_t)&(ramfs->memheap)); /* detach this memheap object from the system */
rt_object_detach((rt_object_t)&(ramfs->memheap));
/* initialize ramfs object */ /* initialize ramfs object */
ramfs->magic = RAMFS_MAGIC; ramfs->magic = RAMFS_MAGIC;
......
/* /*
* File : dfs_ramfs.h * File : dfs_ramfs.h
* This file is part of RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2011, Shanghai Real-Thread Technology Co., Ltd * COPYRIGHT (C) 2004-2013, RT-Thread Development Team
* *
* All rights reserved. * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
/*
* File : romfs.c
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#include <dfs_romfs.h> #include <dfs_romfs.h>
const static unsigned char _dummy_dummy_txt[] = { const static unsigned char _dummy_dummy_txt[] =
0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x69,0x6c,0x65,0x21,0x0d, {
0x0a, 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x69,0x6c,0x65,0x21,0x0d,0x0a,
}; };
const static struct romfs_dirent _dummy[] = { const static struct romfs_dirent _dummy[] =
{ROMFS_DIRENT_FILE, "dummy.txt", _dummy_dummy_txt, sizeof(_dummy_dummy_txt)}, {
{ROMFS_DIRENT_FILE, "dummy.txt", _dummy_dummy_txt, sizeof(_dummy_dummy_txt)},
}; };
const static unsigned char _dummy_txt[] = { const static unsigned char _dummy_txt[] =
0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x69,0x6c,0x65,0x21,0x0d, {
0x0a, 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x69,0x6c,0x65,0x21,0x0d,0x0a,
}; };
const struct romfs_dirent _root_dirent[] = { const struct romfs_dirent _root_dirent[] =
{ROMFS_DIRENT_DIR, "dummy", (rt_uint8_t*) _dummy, sizeof(_dummy)/sizeof(_dummy[0])}, {
{ROMFS_DIRENT_FILE, "dummy.txt", _dummy_txt, sizeof(_dummy_txt)}, {ROMFS_DIRENT_DIR, "dummy", (rt_uint8_t *)_dummy, sizeof(_dummy)/sizeof(_dummy[0])},
{ROMFS_DIRENT_FILE, "dummy.txt", _dummy_txt, sizeof(_dummy_txt)},
}; };
const struct romfs_dirent romfs_root = {ROMFS_DIRENT_DIR, "/", (rt_uint8_t*) _root_dirent, sizeof(_root_dirent)/sizeof(_root_dirent[0])}; const struct romfs_dirent romfs_root =
{
ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_root_dirent, sizeof(_root_dirent)/sizeof(_root_dirent[0])
};
/* /*
* A skeleton of file system in Device File System * File : skeleton.c
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/ */
#include <rtthread.h> #include <rtthread.h>
#include <dfs.h> #include <dfs.h>
#include <dfs_fs.h> #include <dfs_fs.h>
...@@ -9,74 +29,74 @@ ...@@ -9,74 +29,74 @@
int dfs_skt_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data) int dfs_skt_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_skt_unmount(struct dfs_filesystem* fs) int dfs_skt_unmount(struct dfs_filesystem* fs)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_skt_ioctl(struct dfs_fd* file, int cmd, void* args) int dfs_skt_ioctl(struct dfs_fd* file, int cmd, void* args)
{ {
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
int dfs_skt_read(struct dfs_fd* file, void *buf, rt_size_t count) int dfs_skt_read(struct dfs_fd* file, void *buf, rt_size_t count)
{ {
return count; return count;
} }
int dfs_skt_lseek(struct dfs_fd* file, rt_off_t offset) int dfs_skt_lseek(struct dfs_fd* file, rt_off_t offset)
{ {
return -DFS_STATUS_EIO; return -DFS_STATUS_EIO;
} }
int dfs_skt_close(struct dfs_fd* file) int dfs_skt_close(struct dfs_fd* file)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_skt_open(struct dfs_fd* file) int dfs_skt_open(struct dfs_fd* file)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_skt_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) int dfs_skt_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
{ {
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
int dfs_skt_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) int dfs_skt_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count)
{ {
return count * sizeof(struct dirent); return count * sizeof(struct dirent);
} }
static const struct dfs_filesystem_operation _skt_fs = static const struct dfs_filesystem_operation _skt_fs =
{ {
"skt", "skt",
DFS_FS_FLAG_DEFAULT, DFS_FS_FLAG_DEFAULT,
dfs_skt_mount, dfs_skt_mount,
dfs_skt_unmount, dfs_skt_unmount,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
dfs_skt_open, dfs_skt_open,
dfs_skt_close, dfs_skt_close,
dfs_skt_ioctl, dfs_skt_ioctl,
dfs_skt_read, dfs_skt_read,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
dfs_skt_lseek, dfs_skt_lseek,
dfs_skt_getdents, dfs_skt_getdents,
RT_NULL, RT_NULL,
dfs_skt_stat, dfs_skt_stat,
RT_NULL, RT_NULL,
}; };
int dfs_skt_init(void) int dfs_skt_init(void)
{ {
/* register rom file system */ /* register rom file system */
dfs_register(&_skt_fs); dfs_register(&_skt_fs);
return 0; return 0;
} }
/*
* File : skeleton.h
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
*/
#ifndef __SKELETON_H__ #ifndef __SKELETON_H__
#define __SKELETON_H__ #define __SKELETON_H__
......
/* /*
* File : rtthread.h * File : dfs_uffs.c
* This file is part of RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2006-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2011-10-22 prife the first version * 2011-10-22 prife the first version
* 2012-03-28 prife use mtd device interface * 2012-03-28 prife use mtd device interface
* 2012-04-05 prife update uffs with official repo and use uffs_UnMount/Mount * 2012-04-05 prife update uffs with official repo and use uffs_UnMount/Mount
*/ */
#include <rtthread.h> #include <rtthread.h>
#include <dfs_fs.h> #include <dfs_fs.h>
......
/* /*
* dfs_uffs.h * File : dfs_uffs.h
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* Created on: 2012-3-30 * This program is free software; you can redistribute it and/or modify
* Author: prife * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2012-03-30 prife the first version
*/ */
#ifndef DFS_UFFS_H_ #ifndef DFS_UFFS_H_
......
...@@ -8,338 +8,352 @@ ...@@ -8,338 +8,352 @@
static int nand_init_flash(uffs_Device *dev) static int nand_init_flash(uffs_Device *dev)
{ {
return UFFS_FLASH_NO_ERR; return UFFS_FLASH_NO_ERR;
} }
static int nand_release_flash(uffs_Device *dev) static int nand_release_flash(uffs_Device *dev)
{ {
return UFFS_FLASH_NO_ERR; return UFFS_FLASH_NO_ERR;
} }
static int nand_erase_block(uffs_Device *dev, unsigned block) static int nand_erase_block(uffs_Device *dev, unsigned block)
{ {
int res; int res;
res = rt_mtd_nand_erase_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR; res = rt_mtd_nand_erase_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR;
} }
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
static int nand_check_block(uffs_Device *dev, unsigned block) static int nand_check_block(uffs_Device *dev, unsigned block)
{ {
int res; int res;
res = rt_mtd_nand_check_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK; res = rt_mtd_nand_check_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
} }
static int nand_mark_badblock(uffs_Device *dev, unsigned block) static int nand_mark_badblock(uffs_Device *dev, unsigned block)
{ {
int res; int res;
res = rt_mtd_nand_mark_badblock(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR; res = rt_mtd_nand_mark_badblock(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR;
} }
#endif #endif
#if (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_NONE) || (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_SOFT) #if (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_NONE) || (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_SOFT)
static int nand_read_page(uffs_Device *dev,
static int nand_read_page( u32 block,
uffs_Device *dev, u32 page,
u32 block, u32 page, u8 *data,
u8 *data, int data_len, int data_len,
u8 * ecc, u8 *ecc,
rt_uint8_t *spare, int spare_len) rt_uint8_t *spare,
int spare_len)
{ {
int res; int res;
page = block * dev->attr->pages_per_block + page; page = block * dev->attr->pages_per_block + page;
if (data == NULL && spare == NULL) if (data == NULL && spare == NULL)
{ {
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here RT_ASSERT(0); //should not be here
#else #else
/* check block status: bad or good */ /* check block status: bad or good */
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE]; rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
rt_memset(spare, 0, UFFS_MAX_SPARE_SIZE);
rt_memset(spare, 0, UFFS_MAX_SPARE_SIZE); rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private), res = spare[dev->attr->block_status_offs] == 0xFF ?
page, RT_NULL, 0, UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
spare, dev->attr->spare_size);//dev->mem.spare_data_size
res = spare[dev->attr->block_status_offs] == 0xFF ? return res;
UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
return res;
#endif #endif
} }
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private), rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len); page, data, data_len, spare, spare_len);
return UFFS_FLASH_NO_ERR; return UFFS_FLASH_NO_ERR;
} }
static int nand_write_page( static int nand_write_page(uffs_Device *dev,
uffs_Device *dev, u32 block,
u32 block, u32 page, u32 page,
const u8 *data, int data_len, const u8 *data,
const u8 *spare, int spare_len) int data_len,
const u8 *spare,
int spare_len)
{ {
int res; int res;
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size); RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
page = block * dev->attr->pages_per_block + page; page = block * dev->attr->pages_per_block + page;
if (data == NULL && spare == NULL) if (data == NULL && spare == NULL)
{ {
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here RT_ASSERT(0); //should not be here
#else #else
/* mark bad block */ /* mark bad block */
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE]; rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE); rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE);
spare[dev->attr->block_status_offs] = 0x00; spare[dev->attr->block_status_offs] = 0x00;
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private), res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0, page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size spare, dev->attr->spare_size);//dev->mem.spare_data_size
if (res != RT_EOK) if (res != RT_EOK)
goto __error; goto __error;
#endif #endif
} }
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private), res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len); page, data, data_len, spare, spare_len);
if (res != RT_EOK) if (res != RT_EOK)
goto __error; goto __error;
return UFFS_FLASH_NO_ERR; return UFFS_FLASH_NO_ERR;
__error: __error:
return UFFS_FLASH_IO_ERR; return UFFS_FLASH_IO_ERR;
} }
const uffs_FlashOps nand_ops = const uffs_FlashOps nand_ops =
{ {
nand_init_flash, /* InitFlash() */ nand_init_flash, /* InitFlash() */
nand_release_flash, /* ReleaseFlash() */ nand_release_flash, /* ReleaseFlash() */
nand_read_page, /* ReadPage() */ nand_read_page, /* ReadPage() */
NULL, /* ReadPageWithLayout */ NULL, /* ReadPageWithLayout */
nand_write_page, /* WritePage() */ nand_write_page, /* WritePage() */
NULL, /* WirtePageWithLayout */ NULL, /* WirtePageWithLayout */
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
nand_check_block, nand_check_block,
nand_mark_badblock, nand_mark_badblock,
#else #else
NULL, /* IsBadBlock(), let UFFS take care of it. */ NULL, /* IsBadBlock(), let UFFS take care of it. */
NULL, /* MarkBadBlock(), let UFFS take care of it. */ NULL, /* MarkBadBlock(), let UFFS take care of it. */
#endif #endif
nand_erase_block, /* EraseBlock() */ nand_erase_block, /* EraseBlock() */
}; };
void uffs_setup_storage( void uffs_setup_storage(struct uffs_StorageAttrSt *attr,
struct uffs_StorageAttrSt *attr, struct rt_mtd_nand_device *nand)
struct rt_mtd_nand_device * nand)
{ {
rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt)); rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
// attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */ // attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */
attr->page_data_size = nand->page_size; /* page data size */ attr->page_data_size = nand->page_size; /* page data size */
attr->pages_per_block = nand->pages_per_block; /* pages per block */ attr->pages_per_block = nand->pages_per_block; /* pages per block */
attr->spare_size = nand->oob_size; /* page spare size */ attr->spare_size = nand->oob_size; /* page spare size */
attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */ attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */
attr->ecc_size = 0; /* ecc size is 0 , the uffs will calculate the ecc size*/ attr->ecc_size = 0; /* ecc size is 0 , the uffs will calculate the ecc size*/
attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */ attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */
attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */ attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */
} }
#elif RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_HW_AUTO #elif RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_HW_AUTO
static int WritePageWithLayout(uffs_Device *dev,
static int WritePageWithLayout( u32 block,
uffs_Device *dev, u32 block, u32 page, u32 page,
const u8 *data, int data_len, const u8 *data,
const u8 *ecc, //NULL int data_len,
const uffs_TagStore *ts) const u8 *ecc, //NULL
const uffs_TagStore *ts)
{ {
int res; int res;
int spare_len; int spare_len;
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE]; rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size); RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
page = block * dev->attr->pages_per_block + page; page = block * dev->attr->pages_per_block + page;
spare_len = dev->mem.spare_data_size; spare_len = dev->mem.spare_data_size;
if (data == NULL && ts == NULL) if (data == NULL && ts == NULL)
{ {
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here RT_ASSERT(0); //should not be here
#else #else
/* mark bad block */ /* mark bad block */
rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE); rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE);
spare[dev->attr->block_status_offs] = 0x00; spare[dev->attr->block_status_offs] = 0x00;
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private), res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0, page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size spare, dev->attr->spare_size);//dev->mem.spare_data_size
if (res != RT_EOK) if (res != RT_EOK)
goto __error; goto __error;
dev->st.io_write++; dev->st.io_write++;
return UFFS_FLASH_NO_ERR; return UFFS_FLASH_NO_ERR;
#endif #endif
} }
if (data != NULL && data_len != 0) if (data != NULL && data_len != 0)
{ {
RT_ASSERT(data_len == dev->attr->page_data_size); RT_ASSERT(data_len == dev->attr->page_data_size);
dev->st.page_write_count++; dev->st.page_write_count++;
dev->st.io_write += data_len; dev->st.io_write += data_len;
} }
if (ts != RT_NULL) if (ts != RT_NULL)
{ {
uffs_FlashMakeSpare(dev, ts, RT_NULL, (u8 *)spare); uffs_FlashMakeSpare(dev, ts, RT_NULL, (u8 *)spare);
dev->st.spare_write_count++; dev->st.spare_write_count++;
dev->st.io_write += spare_len; dev->st.io_write += spare_len;
} }
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private), res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len); page, data, data_len, spare, spare_len);
if (res != RT_EOK) if (res != RT_EOK)
goto __error; goto __error;
return UFFS_FLASH_NO_ERR; return UFFS_FLASH_NO_ERR;
__error: __error:
return UFFS_FLASH_IO_ERR; return UFFS_FLASH_IO_ERR;
} }
static URET ReadPageWithLayout( static URET ReadPageWithLayout(uffs_Device *dev,
uffs_Device *dev, u32 block, u32 page, u32 block,
u8* data, int data_len, u32 page,
u8 *ecc, //NULL u8 *data,
uffs_TagStore *ts, int data_len,
u8 *ecc_store) //NULL u8 *ecc, //NULL
uffs_TagStore *ts,
u8 *ecc_store) //NULL
{ {
int res = UFFS_FLASH_NO_ERR; int res = UFFS_FLASH_NO_ERR;
int spare_len; int spare_len;
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE]; rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size); RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
page = block * dev->attr->pages_per_block + page; page = block * dev->attr->pages_per_block + page;
spare_len = dev->mem.spare_data_size; spare_len = dev->mem.spare_data_size;
if (data == RT_NULL && ts == RT_NULL) if (data == RT_NULL && ts == RT_NULL)
{ {
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here RT_ASSERT(0); //should not be here
#else #else
/* check block good or bad */ /* check block good or bad */
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private), rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0, page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size spare, dev->attr->spare_size);//dev->mem.spare_data_size
dev->st.io_read++; dev->st.io_read++;
res = spare[dev->attr->block_status_offs] == 0xFF ? res = spare[dev->attr->block_status_offs] == 0xFF ?
UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK; UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
return res; return res;
#endif #endif
} }
if (data != RT_NULL) if (data != RT_NULL)
{ {
dev->st.io_read += data_len; dev->st.io_read += data_len;
dev->st.page_read_count++; dev->st.page_read_count++;
} }
res = rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private), res = rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len); page, data, data_len, spare, spare_len);
if (res == 0) if (res == 0)
res = UFFS_FLASH_NO_ERR; res = UFFS_FLASH_NO_ERR;
else if (res == -1) else if (res == -1)
{ {
//TODO ecc correct, add code to use hardware do ecc correct //TODO ecc correct, add code to use hardware do ecc correct
res = UFFS_FLASH_ECC_OK; res = UFFS_FLASH_ECC_OK;
} }
else else
res = UFFS_FLASH_ECC_FAIL; res = UFFS_FLASH_ECC_FAIL;
if (ts != RT_NULL) if (ts != RT_NULL)
{ {
// unload ts and ecc from spare, you can modify it if you like // unload ts and ecc from spare, you can modify it if you like
uffs_FlashUnloadSpare(dev, (const u8 *)spare, ts, RT_NULL); uffs_FlashUnloadSpare(dev, (const u8 *)spare, ts, RT_NULL);
if ((spare[spare_len - 1] == 0xFF) && (res == UFFS_FLASH_NO_ERR)) if ((spare[spare_len - 1] == 0xFF) && (res == UFFS_FLASH_NO_ERR))
res = UFFS_FLASH_NOT_SEALED; res = UFFS_FLASH_NOT_SEALED;
dev->st.io_read += spare_len; dev->st.io_read += spare_len;
dev->st.spare_read_count++; dev->st.spare_read_count++;
} }
return res; return res;
} }
const uffs_FlashOps nand_ops = const uffs_FlashOps nand_ops =
{ {
nand_init_flash, /* InitFlash() */ nand_init_flash, /* InitFlash() */
nand_release_flash, /* ReleaseFlash() */ nand_release_flash, /* ReleaseFlash() */
NULL, /* ReadPage() */ NULL, /* ReadPage() */
ReadPageWithLayout, /* ReadPageWithLayout */ ReadPageWithLayout, /* ReadPageWithLayout */
NULL, /* WritePage() */ NULL, /* WritePage() */
WritePageWithLayout,/* WirtePageWithLayout */ WritePageWithLayout,/* WirtePageWithLayout */
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
nand_check_block, nand_check_block,
nand_mark_badblock, nand_mark_badblock,
#else #else
NULL, /* IsBadBlock(), let UFFS take care of it. */ NULL, /* IsBadBlock(), let UFFS take care of it. */
NULL, /* MarkBadBlock(), let UFFS take care of it. */ NULL, /* MarkBadBlock(), let UFFS take care of it. */
#endif #endif
nand_erase_block, /* EraseBlock() */ nand_erase_block, /* EraseBlock() */
}; };
static rt_uint8_t hw_flash_data_layout[UFFS_SPARE_LAYOUT_SIZE] = static rt_uint8_t hw_flash_data_layout[UFFS_SPARE_LAYOUT_SIZE] =
{0x05, 0x08, 0xFF, 0x00}; {
0x05, 0x08, 0xFF, 0x00
};
static rt_uint8_t hw_flash_ecc_layout[UFFS_SPARE_LAYOUT_SIZE] = static rt_uint8_t hw_flash_ecc_layout[UFFS_SPARE_LAYOUT_SIZE] =
{0x00, 0x04, 0xFF, 0x00}; {
0x00, 0x04, 0xFF, 0x00
};
void uffs_setup_storage( void uffs_setup_storage(struct uffs_StorageAttrSt *attr,
struct uffs_StorageAttrSt *attr, struct rt_mtd_nand_device *nand)
struct rt_mtd_nand_device * nand)
{ {
rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt)); rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
// attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */ // attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */
attr->page_data_size = nand->page_size; /* page data size */ attr->page_data_size = nand->page_size; /* page data size */
attr->pages_per_block = nand->pages_per_block; /* pages per block */ attr->pages_per_block = nand->pages_per_block; /* pages per block */
attr->spare_size = nand->oob_size; /* page spare size */ attr->spare_size = nand->oob_size; /* page spare size */
attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */ attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */
attr->ecc_size = nand->oob_size-nand->oob_free; /* ecc size */ attr->ecc_size = nand->oob_size-nand->oob_free; /* ecc size */
attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */ attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */
attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */ attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */
/* calculate the ecc layout array */ /* calculate the ecc layout array */
hw_flash_data_layout[0] = attr->ecc_size + 1; /* ecc size + 1byte block status */ hw_flash_data_layout[0] = attr->ecc_size + 1; /* ecc size + 1byte block status */
hw_flash_data_layout[1] = 0x08; hw_flash_data_layout[1] = 0x08;
hw_flash_data_layout[2] = 0xFF; hw_flash_data_layout[2] = 0xFF;
hw_flash_data_layout[3] = 0x00; hw_flash_data_layout[3] = 0x00;
hw_flash_ecc_layout[0] = 0; hw_flash_ecc_layout[0] = 0;
hw_flash_ecc_layout[1] = attr->ecc_size; hw_flash_ecc_layout[1] = attr->ecc_size;
hw_flash_ecc_layout[2] = 0xFF; hw_flash_ecc_layout[2] = 0xFF;
hw_flash_ecc_layout[3] = 0x00; hw_flash_ecc_layout[3] = 0x00;
/* initialize _uffs_data_layout and _uffs_ecc_layout */ /* initialize _uffs_data_layout and _uffs_ecc_layout */
rt_memcpy(attr->_uffs_data_layout, hw_flash_data_layout, UFFS_SPARE_LAYOUT_SIZE); rt_memcpy(attr->_uffs_data_layout, hw_flash_data_layout, UFFS_SPARE_LAYOUT_SIZE);
rt_memcpy(attr->_uffs_ecc_layout, hw_flash_ecc_layout, UFFS_SPARE_LAYOUT_SIZE); rt_memcpy(attr->_uffs_ecc_layout, hw_flash_ecc_layout, UFFS_SPARE_LAYOUT_SIZE);
attr->data_layout = attr->_uffs_data_layout; attr->data_layout = attr->_uffs_data_layout;
attr->ecc_layout = attr->_uffs_ecc_layout; attr->ecc_layout = attr->_uffs_ecc_layout;
} }
#endif #endif
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2008-2012, RT-Thread Development Team * COPYRIGHT (C) 2008-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2011, RT-Thread Development Team * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
* This file is part of Device File System in RT-Thread RTOS * This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * This program is free software; you can redistribute it and/or modify
* found in the file LICENSE in this distribution or at * it under the terms of the GNU General Public License as published by
* http://www.rt-thread.org/license/LICENSE. * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册