2011-10-08 22:50:08 +08:00
|
|
|
/*
|
|
|
|
* File : dfs_fs.c
|
|
|
|
* This file is part of Device File System in RT-Thread RTOS
|
2012-07-10 17:09:42 +08:00
|
|
|
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
2013-06-26 22:30:40 +08:00
|
|
|
* 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.
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2005-02-22 Bernard The first version.
|
|
|
|
* 2010-06-30 Bernard Optimize for RT-Thread RTOS
|
|
|
|
* 2011-03-12 Bernard fix the filesystem lookup issue.
|
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
|
2011-10-08 22:50:08 +08:00
|
|
|
#include <dfs_fs.h>
|
|
|
|
#include <dfs_file.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup FsApi
|
|
|
|
*/
|
|
|
|
/*@{*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will register a file system instance to device file system.
|
|
|
|
*
|
|
|
|
* @param ops the file system instance to be registered.
|
|
|
|
*
|
2013-12-19 13:08:37 +08:00
|
|
|
* @return RT_EOK on successful, -RT_ERROR on failed.
|
2011-10-08 22:50:08 +08:00
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
int dfs_register(const struct dfs_filesystem_operation *ops)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
int ret = RT_EOK;
|
|
|
|
const struct dfs_filesystem_operation **empty = RT_NULL;
|
|
|
|
const struct dfs_filesystem_operation **iter;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* lock filesystem */
|
|
|
|
dfs_lock();
|
|
|
|
/* check if this filesystem was already registered */
|
2013-12-19 13:08:37 +08:00
|
|
|
for (iter = &filesystem_operation_table[0];
|
|
|
|
iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
/* find out an empty filesystem type entry */
|
|
|
|
if (*iter == RT_NULL)
|
|
|
|
(empty == RT_NULL) ? (empty = iter) : 0;
|
|
|
|
else if (strcmp((*iter)->name, ops->name) == 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
ret = -1;
|
|
|
|
break;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the filesystem's operations */
|
2013-12-19 13:08:37 +08:00
|
|
|
if ((ret == RT_EOK) && (empty != RT_NULL))
|
|
|
|
*empty = ops;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_unlock();
|
2013-12-19 13:08:37 +08:00
|
|
|
return ret;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will return the file system mounted on specified path.
|
|
|
|
*
|
|
|
|
* @param path the specified path string.
|
|
|
|
*
|
|
|
|
* @return the found file system or NULL if no file system mounted on
|
|
|
|
* specified path
|
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
struct dfs_filesystem *iter;
|
2013-12-21 10:58:52 +08:00
|
|
|
struct dfs_filesystem *fs = RT_NULL;
|
2013-12-19 13:08:37 +08:00
|
|
|
rt_uint32_t fspath, prefixlen;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
prefixlen = 0;
|
|
|
|
|
2014-04-12 16:35:07 +08:00
|
|
|
RT_ASSERT(path);
|
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
/* lock filesystem */
|
|
|
|
dfs_lock();
|
|
|
|
|
|
|
|
/* lookup it in the filesystem table */
|
2013-12-19 13:08:37 +08:00
|
|
|
for (iter = &filesystem_table[0];
|
|
|
|
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
if ((iter->path == RT_NULL) || (iter->ops == RT_NULL))
|
2012-12-26 09:12:13 +08:00
|
|
|
continue;
|
|
|
|
|
2013-12-19 13:08:37 +08:00
|
|
|
fspath = strlen(iter->path);
|
2013-12-19 00:01:52 +08:00
|
|
|
if ((fspath < prefixlen)
|
2013-12-19 13:08:37 +08:00
|
|
|
|| (strncmp(iter->path, path, fspath) != 0))
|
2013-12-19 00:01:52 +08:00
|
|
|
continue;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2013-12-19 00:01:52 +08:00
|
|
|
/* check next path separator */
|
|
|
|
if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
|
|
|
|
continue;
|
|
|
|
|
2013-12-21 10:58:52 +08:00
|
|
|
fs = iter;
|
2013-12-19 00:01:52 +08:00
|
|
|
prefixlen = fspath;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dfs_unlock();
|
|
|
|
|
2013-12-21 10:58:52 +08:00
|
|
|
return fs;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 13:59:26 +08:00
|
|
|
/**
|
|
|
|
* this function will return the mounted path for specified device.
|
|
|
|
*
|
|
|
|
* @param device the device object which is mounted.
|
|
|
|
*
|
|
|
|
* @return the mounted path or RT_NULL if none device mounted.
|
|
|
|
*/
|
|
|
|
const char* dfs_filesystem_get_mounted_path(struct rt_device* device)
|
|
|
|
{
|
|
|
|
const char* path = RT_NULL;
|
|
|
|
struct dfs_filesystem *iter;
|
|
|
|
|
|
|
|
dfs_lock();
|
|
|
|
for (iter = &filesystem_table[0];
|
|
|
|
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
|
|
|
|
{
|
|
|
|
/* fint the mounted device */
|
|
|
|
if (iter->ops == RT_NULL) continue;
|
|
|
|
else if (iter->dev_id == device)
|
|
|
|
{
|
|
|
|
path = iter->path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release filesystem_table lock */
|
|
|
|
dfs_unlock();
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:50:08 +08:00
|
|
|
/**
|
|
|
|
* this function will fetch the partition table on specified buffer.
|
|
|
|
*
|
|
|
|
* @param part the returned partition structure.
|
|
|
|
* @param buf the buffer contains partition table.
|
|
|
|
* @param pindex the index of partition table to fetch.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful or -RT_ERROR on failed.
|
|
|
|
*/
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part,
|
|
|
|
rt_uint8_t *buf,
|
|
|
|
rt_uint32_t pindex)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
|
|
|
|
#define DPT_ITEM_SIZE 16 /* partition item size */
|
|
|
|
|
|
|
|
rt_uint8_t *dpt;
|
|
|
|
rt_uint8_t type;
|
|
|
|
|
|
|
|
RT_ASSERT(part != RT_NULL);
|
|
|
|
RT_ASSERT(buf != RT_NULL);
|
|
|
|
|
|
|
|
dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
|
|
|
|
|
2013-12-19 00:01:52 +08:00
|
|
|
/* check if it is a valid partition table */
|
2012-12-26 09:12:13 +08:00
|
|
|
if ((*dpt != 0x80) && (*dpt != 0x00))
|
2013-12-19 00:01:52 +08:00
|
|
|
return -RT_ERROR;
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* get partition type */
|
|
|
|
type = *(dpt+4);
|
2013-12-19 00:01:52 +08:00
|
|
|
if (type == 0)
|
|
|
|
return -RT_ERROR;
|
|
|
|
|
|
|
|
/* set partition information
|
|
|
|
* size is the number of 512-Byte */
|
|
|
|
part->type = type;
|
|
|
|
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;
|
|
|
|
|
|
|
|
rt_kprintf("found part[%d], begin: %d, size: ",
|
|
|
|
pindex, part->offset*512);
|
|
|
|
if ((part->size>>11) == 0)
|
|
|
|
rt_kprintf("%d%s",part->size>>1,"KB\n"); /* KB */
|
2012-12-26 09:12:13 +08:00
|
|
|
else
|
|
|
|
{
|
2013-12-19 00:01:52 +08:00
|
|
|
unsigned int part_size;
|
|
|
|
part_size = part->size >> 11; /* MB */
|
|
|
|
if ((part_size>>10) == 0)
|
|
|
|
rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\n");
|
|
|
|
else
|
|
|
|
rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\n");
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 00:01:52 +08:00
|
|
|
return RT_EOK;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will mount a file system on a specified path.
|
|
|
|
*
|
|
|
|
* @param device_name the name of device which includes a file system.
|
|
|
|
* @param path the path to mount a file system
|
|
|
|
* @param filesystemtype the file system type
|
|
|
|
* @param rwflag the read/write etc. flag.
|
|
|
|
* @param data the private data(parameter) for this file system.
|
|
|
|
*
|
|
|
|
* @return 0 on successful or -1 on failed.
|
|
|
|
*/
|
2012-12-26 09:12:13 +08:00
|
|
|
int dfs_mount(const char *device_name,
|
|
|
|
const char *path,
|
|
|
|
const char *filesystemtype,
|
|
|
|
unsigned long rwflag,
|
|
|
|
const void *data)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
const struct dfs_filesystem_operation **ops;
|
|
|
|
struct dfs_filesystem *iter;
|
|
|
|
struct dfs_filesystem *fs = RT_NULL;
|
|
|
|
char *fullpath = RT_NULL;
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_device_t dev_id;
|
|
|
|
|
|
|
|
/* open specific device */
|
2013-12-19 00:01:52 +08:00
|
|
|
if (device_name == RT_NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 00:01:52 +08:00
|
|
|
/* which is a non-device filesystem mount */
|
|
|
|
dev_id = NULL;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
2013-12-19 00:01:52 +08:00
|
|
|
else if ((dev_id = rt_device_find(device_name)) == RT_NULL)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 00:01:52 +08:00
|
|
|
/* no this device */
|
|
|
|
rt_set_errno(-DFS_STATUS_ENODEV);
|
|
|
|
return -1;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 13:08:37 +08:00
|
|
|
/* find out the specific filesystem */
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_lock();
|
2013-04-01 02:53:59 +08:00
|
|
|
|
2013-12-19 13:08:37 +08:00
|
|
|
for (ops = &filesystem_operation_table[0];
|
|
|
|
ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++)
|
|
|
|
if ((ops != RT_NULL) && (strcmp((*ops)->name, filesystemtype) == 0))
|
2012-12-26 09:12:13 +08:00
|
|
|
break;
|
2013-12-19 13:08:37 +08:00
|
|
|
|
2013-04-01 02:53:59 +08:00
|
|
|
dfs_unlock();
|
2012-12-26 09:12:13 +08:00
|
|
|
|
2013-12-19 13:08:37 +08:00
|
|
|
if (ops == &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX])
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
/* can't find filesystem */
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_set_errno(-DFS_STATUS_ENODEV);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-19 00:01:52 +08:00
|
|
|
|
|
|
|
/* check if there is mount implementation */
|
2013-12-19 13:08:37 +08:00
|
|
|
if ((*ops == NULL) || ((*ops)->mount == NULL))
|
2013-12-19 00:01:52 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* make full path for special file */
|
|
|
|
fullpath = dfs_normalize_path(RT_NULL, path);
|
|
|
|
if (fullpath == RT_NULL) /* not an abstract path */
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOTDIR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the path exists or not, raw APIs call, fixme */
|
|
|
|
if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
|
|
|
|
{
|
|
|
|
struct dfs_fd fd;
|
|
|
|
|
|
|
|
if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0)
|
|
|
|
{
|
|
|
|
rt_free(fullpath);
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOTDIR);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dfs_file_close(&fd);
|
|
|
|
}
|
|
|
|
|
2013-12-19 13:08:37 +08:00
|
|
|
/* check whether the file system mounted or not in the filesystem table
|
|
|
|
* if it is unmounted yet, find out an empty entry */
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_lock();
|
2013-12-19 13:08:37 +08:00
|
|
|
|
|
|
|
for (iter = &filesystem_table[0];
|
|
|
|
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-12-19 13:08:37 +08:00
|
|
|
/* check if it is an empty filesystem table entry? if it is, save fs */
|
|
|
|
if (iter->ops == RT_NULL)
|
|
|
|
(fs == RT_NULL) ? (fs = iter) : 0;
|
2013-12-19 00:01:52 +08:00
|
|
|
/* check if the PATH is mounted */
|
2013-12-19 13:08:37 +08:00
|
|
|
else if (strcmp(iter->path, path) == 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_EINVAL);
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 13:08:37 +08:00
|
|
|
if ((fs == RT_NULL) && (iter == &filesystem_table[DFS_FILESYSTEMS_MAX]))
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOSPC);
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register file system */
|
|
|
|
fs->path = fullpath;
|
2013-12-19 13:08:37 +08:00
|
|
|
fs->ops = *ops;
|
2012-12-26 09:12:13 +08:00
|
|
|
fs->dev_id = dev_id;
|
|
|
|
/* release filesystem_table lock */
|
|
|
|
dfs_unlock();
|
|
|
|
|
|
|
|
/* open device, but do not check the status of device */
|
|
|
|
if (dev_id != RT_NULL)
|
2013-12-21 12:09:28 +08:00
|
|
|
{
|
|
|
|
if (rt_device_open(fs->dev_id,
|
|
|
|
RT_DEVICE_OFLAG_RDWR) != RT_EOK)
|
|
|
|
{
|
|
|
|
/* The underlaying device has error, clear the entry. */
|
|
|
|
dfs_lock();
|
|
|
|
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
}
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* call mount of this filesystem */
|
2013-12-19 13:08:37 +08:00
|
|
|
if ((*ops)->mount(fs, rwflag, data) < 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
|
|
|
/* close device */
|
|
|
|
if (dev_id != RT_NULL)
|
|
|
|
rt_device_close(fs->dev_id);
|
|
|
|
|
|
|
|
/* mount failed */
|
|
|
|
dfs_lock();
|
|
|
|
/* clear filesystem table entry */
|
|
|
|
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
|
2013-12-19 00:01:52 +08:00
|
|
|
goto err1;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
|
|
|
err1:
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_unlock();
|
2013-12-19 00:01:52 +08:00
|
|
|
rt_free(fullpath);
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return -1;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-10 17:09:42 +08:00
|
|
|
* this function will unmount a file system on specified path.
|
2011-10-08 22:50:08 +08:00
|
|
|
*
|
|
|
|
* @param specialfile the specified path which mounted a file system.
|
|
|
|
*
|
|
|
|
* @return 0 on successful or -1 on failed.
|
|
|
|
*/
|
|
|
|
int dfs_unmount(const char *specialfile)
|
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
char *fullpath;
|
2013-12-21 11:35:09 +08:00
|
|
|
struct dfs_filesystem *iter;
|
2012-12-26 09:12:13 +08:00
|
|
|
struct dfs_filesystem *fs = RT_NULL;
|
2011-12-23 10:11:55 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
fullpath = dfs_normalize_path(RT_NULL, specialfile);
|
|
|
|
if (fullpath == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOTDIR);
|
2011-12-23 10:11:55 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2011-12-23 10:11:55 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
/* lock filesystem */
|
|
|
|
dfs_lock();
|
2011-12-23 10:11:55 +08:00
|
|
|
|
2013-12-21 11:35:09 +08:00
|
|
|
for (iter = &filesystem_table[0];
|
|
|
|
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
|
|
|
|
{
|
|
|
|
/* check if the PATH is mounted */
|
|
|
|
if ((iter->path != NULL) && (strcmp(iter->path, fullpath) == 0))
|
|
|
|
{
|
|
|
|
fs = iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
if (fs == RT_NULL ||
|
|
|
|
fs->ops->unmount == RT_NULL ||
|
|
|
|
fs->ops->unmount(fs) < 0)
|
|
|
|
{
|
|
|
|
goto err1;
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
/* close device, but do not check the status of device */
|
|
|
|
if (fs->dev_id != RT_NULL)
|
|
|
|
rt_device_close(fs->dev_id);
|
2012-03-17 19:22:51 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
if (fs->path != RT_NULL)
|
|
|
|
rt_free(fs->path);
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
/* clear this filesystem table entry */
|
|
|
|
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_unlock();
|
|
|
|
rt_free(fullpath);
|
|
|
|
|
|
|
|
return 0;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
|
|
|
err1:
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_unlock();
|
|
|
|
rt_free(fullpath);
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return -1;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make a file system on the special device
|
|
|
|
*
|
|
|
|
* @param fs_name the file system name
|
|
|
|
* @param device_name the special device name
|
|
|
|
*
|
|
|
|
* @return 0 on successful, otherwise failed.
|
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
int dfs_mkfs(const char *fs_name, const char *device_name)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
int index;
|
2013-12-19 00:01:52 +08:00
|
|
|
rt_device_t dev_id = RT_NULL;
|
2013-01-08 18:58:45 +08:00
|
|
|
|
|
|
|
/* check device name, and it should not be NULL */
|
2013-12-19 00:01:52 +08:00
|
|
|
if (device_name != RT_NULL)
|
2013-01-08 18:58:45 +08:00
|
|
|
dev_id = rt_device_find(device_name);
|
|
|
|
|
|
|
|
if (dev_id == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENODEV);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-26 09:12:13 +08:00
|
|
|
|
|
|
|
/* lock file system */
|
|
|
|
dfs_lock();
|
|
|
|
/* find the file system operations */
|
|
|
|
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++)
|
|
|
|
{
|
|
|
|
if (filesystem_operation_table[index] != RT_NULL &&
|
|
|
|
strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dfs_unlock();
|
|
|
|
|
2013-12-19 00:01:52 +08:00
|
|
|
if (index < DFS_FILESYSTEM_TYPES_MAX)
|
|
|
|
{
|
|
|
|
/* find file system operation */
|
|
|
|
const struct dfs_filesystem_operation *ops = filesystem_operation_table[index];
|
|
|
|
if (ops->mkfs == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->mkfs(dev_id);
|
|
|
|
}
|
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
rt_kprintf("Can not find the file system which named as %s.\n", fs_name);
|
|
|
|
return -1;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function will return the information about a mounted file system.
|
|
|
|
*
|
|
|
|
* @param path the path which mounted file system.
|
|
|
|
* @param buffer the buffer to save the returned information.
|
|
|
|
*
|
|
|
|
* @return 0 on successful, others on failed.
|
|
|
|
*/
|
2011-12-23 10:11:55 +08:00
|
|
|
int dfs_statfs(const char *path, struct statfs *buffer)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
struct dfs_filesystem *fs;
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
fs = dfs_filesystem_lookup(path);
|
|
|
|
if (fs != RT_NULL)
|
|
|
|
{
|
|
|
|
if (fs->ops->statfs != RT_NULL)
|
|
|
|
return fs->ops->statfs(fs, buffer);
|
|
|
|
}
|
2011-10-08 22:50:08 +08:00
|
|
|
|
2012-12-26 09:12:13 +08:00
|
|
|
return -1;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 07:46:10 +08:00
|
|
|
#ifdef RT_USING_DFS_MNTTABLE
|
|
|
|
int dfs_mount_table(void)
|
|
|
|
{
|
2013-07-24 07:33:48 +08:00
|
|
|
int index = 0;
|
2013-07-22 07:46:10 +08:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (mount_table[index].path == RT_NULL) break;
|
2013-07-24 07:33:48 +08:00
|
|
|
|
2013-07-22 07:46:10 +08:00
|
|
|
if (dfs_mount(mount_table[index].device_name,
|
|
|
|
mount_table[index].path,
|
|
|
|
mount_table[index].filesystemtype,
|
|
|
|
mount_table[index].rwflag,
|
|
|
|
mount_table[index].data) != 0)
|
|
|
|
{
|
2013-12-19 00:01:52 +08:00
|
|
|
rt_kprintf("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype,
|
2013-07-22 07:46:10 +08:00
|
|
|
mount_table[index].path);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
index ++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_ENV_EXPORT(dfs_mount_table);
|
|
|
|
#endif
|
|
|
|
|
2011-10-08 22:50:08 +08:00
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
#include <finsh.h>
|
2011-12-23 10:11:55 +08:00
|
|
|
void mkfs(const char *fs_name, const char *device_name)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
dfs_mkfs(fs_name, device_name);
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(mkfs, make a file system);
|
|
|
|
|
2013-01-25 12:15:34 +08:00
|
|
|
int df(const char *path)
|
2011-10-08 22:50:08 +08:00
|
|
|
{
|
2012-12-26 09:12:13 +08:00
|
|
|
int result;
|
2013-01-25 12:15:34 +08:00
|
|
|
long long cap;
|
2012-12-26 09:12:13 +08:00
|
|
|
struct statfs buffer;
|
|
|
|
|
2013-12-19 00:01:52 +08:00
|
|
|
result = dfs_statfs(path ? path : RT_NULL, &buffer);
|
2013-01-25 12:15:34 +08:00
|
|
|
if (result != 0)
|
2012-12-26 09:12:13 +08:00
|
|
|
{
|
2013-01-25 12:15:34 +08:00
|
|
|
rt_kprintf("dfs_statfs failed.\n");
|
|
|
|
return -1;
|
2012-12-26 09:12:13 +08:00
|
|
|
}
|
2013-01-25 12:15:34 +08:00
|
|
|
|
|
|
|
cap = buffer.f_bsize * buffer.f_bfree / 1024;
|
|
|
|
rt_kprintf("disk free: %d KB [ %d block, %d bytes per block ]\n",
|
|
|
|
(unsigned long)cap, buffer.f_bfree, buffer.f_bsize);
|
|
|
|
return 0;
|
2011-10-08 22:50:08 +08:00
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(df, get disk free);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* @} */
|