2009-07-03 06:48:23 +08:00
|
|
|
/*
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Project : Device Filesystem
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Copyright 2004, 2005 www.fayfayspace.org.
|
|
|
|
| All rights reserved.
|
|
|
|
|------------------------------------------------------------------------------
|
|
|
|
| File : dfs_fs.c, the filesystem related implementations of Device FileSystem
|
|
|
|
|------------------------------------------------------------------------------
|
|
|
|
| Chang Logs:
|
|
|
|
| Date Author Notes
|
2009-10-18 20:32:10 +08:00
|
|
|
| 2005-02-22 ffxz The first version.
|
2009-07-03 06:48:23 +08:00
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <dfs_fs.h>
|
|
|
|
#include <dfs_raw.h>
|
|
|
|
#include <dfs_util.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Function : dfs_register
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Description : registers a filesystem
|
|
|
|
|
|
|
|
|
| Parameters : ops, the implementation of filesystem
|
|
|
|
| Returns : the status of register
|
|
|
|
| 0 , successful
|
|
|
|
| -1, failed
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int dfs_register(struct dfs_filesystem_operation* ops)
|
|
|
|
{
|
2009-10-26 07:24:51 +08:00
|
|
|
int index, result;
|
|
|
|
|
|
|
|
result = 0;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-26 07:24:51 +08:00
|
|
|
/* lock filesystem */
|
|
|
|
dfs_lock();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
/* check if this filesystem was already registered */
|
|
|
|
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
|
|
|
|
{
|
|
|
|
if (filesystem_operation_table[index] != RT_NULL &&
|
|
|
|
strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
|
2009-10-26 07:24:51 +08:00
|
|
|
{
|
|
|
|
result = -1;
|
2009-10-18 20:32:10 +08:00
|
|
|
goto err;
|
2009-10-26 07:24:51 +08:00
|
|
|
}
|
2009-10-18 20:32:10 +08:00
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
/* find out an empty filesystem type entry */
|
|
|
|
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
|
|
|
|
index++) ;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
/* filesystem type table full */
|
2009-10-26 07:24:51 +08:00
|
|
|
if (index == DFS_FILESYSTEM_TYPES_MAX)
|
|
|
|
{
|
|
|
|
result = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
/* save the filesystem's operations */
|
|
|
|
filesystem_operation_table[index] = ops;
|
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
err:
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
|
|
|
return result;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Function : dfs_filesystem_lookup
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Description : lookup the mounted filesystem on the specified path
|
|
|
|
|
|
|
|
|
| Parameters : path, the specified path string
|
|
|
|
| Returns : the found filesystem
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
|
|
|
|
{
|
2009-10-18 20:32:10 +08:00
|
|
|
struct dfs_filesystem* fs;
|
|
|
|
rt_uint32_t index, fspath, prefixlen;
|
|
|
|
|
|
|
|
fs = RT_NULL;
|
|
|
|
prefixlen = 0;
|
|
|
|
|
2009-10-26 07:24:51 +08:00
|
|
|
/* lock filesystem */
|
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
|
|
|
|
/* lookup it in the filesystem table */
|
|
|
|
for (index = 0; index < DFS_FILESYSTEMS_MAX + 1; index++)
|
|
|
|
{
|
|
|
|
fspath = strlen(filesystem_table[index].path);
|
|
|
|
if (fspath < prefixlen) continue;
|
|
|
|
|
|
|
|
if ((filesystem_table[index].ops != RT_NULL) &&
|
|
|
|
str_is_prefix(filesystem_table[index].path, path) == 0)
|
|
|
|
{
|
|
|
|
fs = &filesystem_table[index];
|
|
|
|
prefixlen = fspath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
return fs;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex)
|
|
|
|
{
|
|
|
|
#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
|
|
|
|
#define DPT_ITEM_SIZE 16 /* partition item size */
|
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
rt_uint8_t* dpt;
|
|
|
|
rt_uint8_t type;
|
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
RT_ASSERT(part != RT_NULL);
|
|
|
|
RT_ASSERT(buf != RT_NULL);
|
|
|
|
|
|
|
|
result = RT_EOK;
|
|
|
|
|
|
|
|
dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
|
|
|
|
|
|
|
|
if ((*dpt != 0x80) && (*dpt != 0x00))
|
|
|
|
{
|
|
|
|
/* which is not a partition table */
|
|
|
|
result = -RT_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get partition type */
|
|
|
|
type = *(dpt+4);
|
|
|
|
|
|
|
|
if (type != 0)
|
|
|
|
{
|
|
|
|
/* set partition type */
|
|
|
|
part->type = type;
|
|
|
|
|
|
|
|
/* get partition offset and size */
|
|
|
|
part->offset = *(dpt+ 8) | *(dpt+ 9) << 8 |
|
|
|
|
*(dpt+10) << 16 | *(dpt+11) << 24;
|
|
|
|
part->size = *(dpt+12) | *(dpt+13) << 8 |
|
|
|
|
*(dpt+14) << 16 | *(dpt+15) << 24;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
2009-10-18 20:32:10 +08:00
|
|
|
rt_kprintf("part[%d], begin: %d, size: ",
|
|
|
|
pindex, part->offset * 512);
|
|
|
|
if ( (part->size>>11) > 0 ) /* MB */
|
|
|
|
{
|
|
|
|
unsigned int part_size;
|
|
|
|
part_size = part->size>>11;/* MB */
|
|
|
|
if ( (part_size>>10) > 0) /* GB */
|
|
|
|
{
|
|
|
|
rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
2009-10-18 20:32:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
return result;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Function : dfs_mount
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Description : mount a filesystem to specified path
|
|
|
|
|
|
|
|
|
| Parameters : device_name, the implementation of filesystem
|
2009-10-18 20:32:10 +08:00
|
|
|
| path,
|
2009-07-03 06:48:23 +08:00
|
|
|
| filesystemtype,
|
|
|
|
| rwflag,
|
|
|
|
| data,
|
|
|
|
| Returns : the status of register
|
|
|
|
| 0 , successful
|
|
|
|
| -1, failed
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int dfs_mount(const char* device_name, const char* path,
|
2009-10-18 20:32:10 +08:00
|
|
|
const char* filesystemtype, unsigned long rwflag, const
|
|
|
|
void* data)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2009-10-18 20:32:10 +08:00
|
|
|
struct dfs_filesystem_operation* ops;
|
|
|
|
struct dfs_filesystem* fs;
|
|
|
|
char *fullpath=RT_NULL;
|
2009-10-26 07:24:51 +08:00
|
|
|
#ifdef DFS_USING_WORKDIR
|
2009-10-18 20:32:10 +08:00
|
|
|
char full_path[DFS_PATH_MAX + 1];
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
2009-10-18 20:32:10 +08:00
|
|
|
rt_device_t dev_id;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
/* open specific device */
|
2010-04-22 17:33:25 +08:00
|
|
|
if (device_name != RT_NULL)
|
|
|
|
{
|
|
|
|
dev_id = rt_device_find(device_name);
|
|
|
|
if (dev_id == RT_NULL)
|
|
|
|
{
|
|
|
|
/* no this device */
|
|
|
|
rt_set_errno(-DFS_STATUS_ENODEV);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* which is a non-device filesystem mount */
|
|
|
|
dev_id = RT_NULL;
|
|
|
|
}
|
2009-10-18 20:32:10 +08:00
|
|
|
|
|
|
|
/* find out specific filesystem */
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ )
|
|
|
|
{
|
|
|
|
if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can't find filesystem */
|
|
|
|
if ( index == DFS_FILESYSTEM_TYPES_MAX )
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENODEV);
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-10-18 20:32:10 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ops = filesystem_operation_table[index];
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-10-18 20:32:10 +08:00
|
|
|
|
|
|
|
/* make full path for special file */
|
|
|
|
fullpath = (char*)path;
|
|
|
|
if ( fullpath[0] != '/') /* not an abstract path */
|
|
|
|
{
|
2009-10-26 07:24:51 +08:00
|
|
|
#ifdef DFS_USING_WORKDIR
|
2009-10-18 20:32:10 +08:00
|
|
|
/* build full path */
|
|
|
|
fullpath = full_path;
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
build_fullpath(working_directory, path, fullpath);
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-07-03 06:48:23 +08:00
|
|
|
#else
|
2009-10-18 20:32:10 +08:00
|
|
|
rt_set_errno(-DFS_STATUS_ENOTDIR);
|
|
|
|
return -1;
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
2009-10-18 20:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the path exists or not, raw APIs call, fixme */
|
|
|
|
if ( (strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0) )
|
|
|
|
{
|
|
|
|
struct dfs_fd fd;
|
|
|
|
|
|
|
|
if ( dfile_raw_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0 )
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOTDIR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dfile_raw_close(&fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check whether the file system mounted or not */
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
|
|
|
|
{
|
|
|
|
if ( filesystem_table[index].ops != RT_NULL &&
|
|
|
|
strcmp(filesystem_table[index].path, path) == 0 )
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_EINVAL);
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find out en empty filesystem table entry */
|
|
|
|
for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
|
|
|
|
index++) ;
|
|
|
|
if ( index == DFS_FILESYSTEMS_MAX ) /* can't find en empty filesystem table entry */
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_EMMOUNT);
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register file system */
|
|
|
|
fs = &(filesystem_table[index]);
|
|
|
|
strncpy(fs->path, fullpath, strlen(fullpath));
|
|
|
|
fs->ops = ops;
|
|
|
|
fs->dev_id = dev_id;
|
|
|
|
/* release filesystem_table lock */
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-10-18 20:32:10 +08:00
|
|
|
|
2010-04-22 17:33:25 +08:00
|
|
|
/* open device, but do not check the status of device */
|
|
|
|
if (dev_id != RT_NULL) rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
|
2009-10-18 20:32:10 +08:00
|
|
|
|
2010-04-22 17:33:25 +08:00
|
|
|
if (ops->mount == RT_NULL) /* there is no mount implementation */
|
2009-10-18 20:32:10 +08:00
|
|
|
{
|
2010-04-22 17:33:25 +08:00
|
|
|
if (dev_id != RT_NULL) rt_device_close(dev_id);
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
/* clear filesystem table entry */
|
|
|
|
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-10-18 20:32:10 +08:00
|
|
|
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* call mount of this filesystem */
|
2010-04-22 17:33:25 +08:00
|
|
|
else if (ops->mount(fs, rwflag, data) < 0)
|
2009-10-18 20:32:10 +08:00
|
|
|
{
|
|
|
|
/* close device */
|
2010-04-22 17:33:25 +08:00
|
|
|
if (dev_id != RT_NULL) rt_device_close(fs->dev_id);
|
2009-10-18 20:32:10 +08:00
|
|
|
|
2010-04-22 17:33:25 +08:00
|
|
|
/* mount failed */
|
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
/* clear filesystem table entry */
|
|
|
|
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2010-04-22 17:33:25 +08:00
|
|
|
|
|
|
|
return -1;
|
2009-10-18 20:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
err1:
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-10-18 20:32:10 +08:00
|
|
|
return -1;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Function : dfs_unmount
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
| Description : unmount a filesystem
|
|
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
| Parameters :
|
2009-07-03 06:48:23 +08:00
|
|
|
| Returns : the status of register
|
|
|
|
| 0 , successful
|
|
|
|
| -1, failed
|
|
|
|
+------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int dfs_unmount(const char *specialfile)
|
|
|
|
{
|
2009-10-18 20:32:10 +08:00
|
|
|
char *fullpath;
|
2009-10-26 07:24:51 +08:00
|
|
|
#ifdef DFS_USING_WORKDIR
|
2009-10-18 20:32:10 +08:00
|
|
|
char full_path[DFS_PATH_MAX + 1];
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
2009-10-18 20:32:10 +08:00
|
|
|
struct dfs_filesystem* fs = RT_NULL;
|
|
|
|
|
|
|
|
fullpath = (char*)specialfile;
|
|
|
|
if ( fullpath[0] != '/')
|
|
|
|
{
|
2009-10-26 07:24:51 +08:00
|
|
|
#ifdef DFS_USING_WORKDIR
|
2009-10-18 20:32:10 +08:00
|
|
|
/* build full path */
|
|
|
|
fullpath = full_path;
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_lock();
|
2009-10-18 20:32:10 +08:00
|
|
|
build_fullpath(working_directory, specialfile, fullpath);
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-07-03 06:48:23 +08:00
|
|
|
#else
|
2009-10-18 20:32:10 +08:00
|
|
|
rt_set_errno(-DFS_STATUS_ENOTDIR);
|
|
|
|
return -1;
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
2009-10-18 20:32:10 +08:00
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-26 07:24:51 +08:00
|
|
|
/* lock filesystem */
|
|
|
|
dfs_lock();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
fs = dfs_filesystem_lookup(fullpath);
|
|
|
|
if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
|
|
|
|
{
|
|
|
|
goto err1;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
/* close device, but do not check the status of device */
|
|
|
|
rt_device_close(fs->dev_id);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
/* clear this filesystem table entry */
|
|
|
|
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
return 0;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
err1:
|
2009-10-26 07:24:51 +08:00
|
|
|
dfs_unlock();
|
|
|
|
|
2009-10-18 20:32:10 +08:00
|
|
|
return -1;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|