import RT-Thread@9217865c without bsp, libcpu and components/net

This commit is contained in:
Zihao Yu 2023-05-20 16:23:33 +08:00
commit e2376a3709
1414 changed files with 390370 additions and 0 deletions

View file

@ -0,0 +1,975 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2005-02-22 Bernard The first version.
* 2017-12-11 Bernard Use rt_free to instead of free in fd_is_open().
* 2018-03-20 Heyuanjie dynamic allocation FD
*/
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#include "dfs_private.h"
#ifdef RT_USING_SMART
#include <lwp.h>
#endif
#ifdef RT_USING_POSIX_STDIO
#include <libc.h>
#endif /* RT_USING_POSIX_STDIO */
/* Global variables */
const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
/* device filesystem lock */
static struct rt_mutex fslock;
static struct rt_mutex fdlock;
#ifdef DFS_USING_WORKDIR
char working_directory[DFS_PATH_MAX] = {"/"};
#endif
static struct dfs_fdtable _fdtab;
static int fd_alloc(struct dfs_fdtable *fdt, int startfd);
/**
* @addtogroup DFS
* @{
*/
/**
* this function will initialize device file system.
*/
int dfs_init(void)
{
static rt_bool_t init_ok = RT_FALSE;
if (init_ok)
{
rt_kprintf("dfs already init.\n");
return 0;
}
/* init vnode hash table */
dfs_vnode_mgr_init();
/* clear filesystem operations table */
rt_memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
/* clear filesystem table */
rt_memset(filesystem_table, 0, sizeof(filesystem_table));
/* clean fd table */
rt_memset(&_fdtab, 0, sizeof(_fdtab));
/* create device filesystem lock */
rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_PRIO);
rt_mutex_init(&fdlock, "fdlock", RT_IPC_FLAG_PRIO);
#ifdef DFS_USING_WORKDIR
/* set current working directory */
rt_memset(working_directory, 0, sizeof(working_directory));
working_directory[0] = '/';
#endif
#ifdef RT_USING_DFS_TMPFS
{
extern int dfs_tmpfs_init(void);
dfs_tmpfs_init();
}
#endif
#ifdef RT_USING_DFS_DEVFS
{
extern int devfs_init(void);
/* if enable devfs, initialize and mount it as soon as possible */
devfs_init();
dfs_mount(NULL, "/dev", "devfs", 0, 0);
}
#if defined(RT_USING_DEV_BUS) && defined(RT_USING_DFS_TMPFS)
mkdir("/dev/shm", 0x777);
if (dfs_mount(RT_NULL, "/dev/shm", "tmp", 0, 0) != 0)
{
rt_kprintf("Dir /dev/shm mount failed!\n");
}
#endif
#endif
init_ok = RT_TRUE;
return 0;
}
INIT_PREV_EXPORT(dfs_init);
/**
* this function will lock device file system.
*
* @note please don't invoke it on ISR.
*/
void dfs_lock(void)
{
rt_err_t result = -RT_EBUSY;
while (result == -RT_EBUSY)
{
result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
}
if (result != RT_EOK)
{
RT_ASSERT(0);
}
}
void dfs_file_lock(void)
{
rt_err_t result = -RT_EBUSY;
while (result == -RT_EBUSY)
{
result = rt_mutex_take(&fdlock, RT_WAITING_FOREVER);
}
if (result != RT_EOK)
{
RT_ASSERT(0);
}
}
/**
* this function will lock device file system.
*
* @note please don't invoke it on ISR.
*/
void dfs_unlock(void)
{
rt_mutex_release(&fslock);
}
#ifdef DFS_USING_POSIX
void dfs_file_unlock(void)
{
rt_mutex_release(&fdlock);
}
static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
{
int nr;
int index;
struct dfs_file **fds = NULL;
if (fd < fdt->maxfd)
{
return fd;
}
if (fd >= DFS_FD_MAX)
{
return -1;
}
nr = ((fd + 4) & ~3);
if (nr > DFS_FD_MAX)
{
nr = DFS_FD_MAX;
}
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
if (!fds)
{
return -1;
}
/* clean the new allocated fds */
for (index = fdt->maxfd; index < nr; index++)
{
fds[index] = NULL;
}
fdt->fds = fds;
fdt->maxfd = nr;
return fd;
}
static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
{
int idx;
/* find an empty fd slot */
for (idx = startfd; idx < (int)fdt->maxfd; idx++)
{
if (fdt->fds[idx] == RT_NULL)
{
return idx;
}
}
idx = fdt->maxfd;
if (idx < startfd)
{
idx = startfd;
}
if (fd_slot_expand(fdt, idx) < 0)
{
return -1;
}
return idx;
}
static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
{
int idx;
struct dfs_file *fd = NULL;
idx = fd_slot_alloc(fdt, startfd);
/* allocate 'struct dfs_file' */
if (idx < 0)
{
return -1;
}
fd = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
if (!fd)
{
return -1;
}
fd->ref_count = 1;
fd->magic = DFS_FD_MAGIC;
fd->vnode = NULL;
fdt->fds[idx] = fd;
return idx;
}
/**
* @ingroup Fd
* This function will allocate a file descriptor.
*
* @return -1 on failed or the allocated file descriptor.
*/
int fdt_fd_new(struct dfs_fdtable *fdt)
{
int idx;
/* lock filesystem */
dfs_file_lock();
/* find an empty fd entry */
idx = fd_alloc(fdt, DFS_STDIO_OFFSET);
/* can't find an empty fd entry */
if (idx < 0)
{
LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
}
dfs_file_unlock();
return idx;
}
int fd_new(void)
{
struct dfs_fdtable *fdt = NULL;
fdt = dfs_fdtable_get();
return fdt_fd_new(fdt);
}
/**
* @ingroup Fd
*
* This function will return a file descriptor structure according to file
* descriptor.
*
* @return NULL on on this file descriptor or the file descriptor structure
* pointer.
*/
struct dfs_file *fdt_fd_get(struct dfs_fdtable* fdt, int fd)
{
struct dfs_file *d;
if (fd < 0 || fd >= (int)fdt->maxfd)
{
return NULL;
}
dfs_file_lock();
d = fdt->fds[fd];
/* check dfs_file valid or not */
if ((d == NULL) || (d->magic != DFS_FD_MAGIC))
{
dfs_file_unlock();
return NULL;
}
dfs_file_unlock();
return d;
}
struct dfs_file *fd_get(int fd)
{
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
return fdt_fd_get(fdt, fd);
}
/**
* @ingroup Fd
*
* This function will put the file descriptor.
*/
void fdt_fd_release(struct dfs_fdtable* fdt, int fd)
{
struct dfs_file *fd_slot = NULL;
RT_ASSERT(fdt != NULL);
dfs_file_lock();
if ((fd < 0) || (fd >= fdt->maxfd))
{
dfs_file_unlock();
return;
}
fd_slot = fdt->fds[fd];
if (fd_slot == NULL)
{
dfs_file_unlock();
return;
}
fdt->fds[fd] = NULL;
/* check fd */
RT_ASSERT(fd_slot->magic == DFS_FD_MAGIC);
fd_slot->ref_count--;
/* clear this fd entry */
if (fd_slot->ref_count == 0)
{
struct dfs_vnode *vnode = fd_slot->vnode;
if (vnode)
{
vnode->ref_count--;
if(vnode->ref_count == 0)
{
rt_free(vnode);
fd_slot->vnode = RT_NULL;
}
}
rt_free(fd_slot);
}
dfs_file_unlock();
}
void fd_release(int fd)
{
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
fdt_fd_release(fdt, fd);
}
rt_err_t sys_dup(int oldfd)
{
int newfd = -1;
struct dfs_fdtable *fdt = NULL;
dfs_file_lock();
/* check old fd */
fdt = dfs_fdtable_get();
if ((oldfd < 0) || (oldfd >= fdt->maxfd))
{
goto exit;
}
if (!fdt->fds[oldfd])
{
goto exit;
}
/* get a new fd */
newfd = fd_slot_alloc(fdt, DFS_STDIO_OFFSET);
if (newfd >= 0)
{
fdt->fds[newfd] = fdt->fds[oldfd];
/* inc ref_count */
fdt->fds[newfd]->ref_count++;
}
exit:
dfs_file_unlock();
return newfd;
}
#endif /* DFS_USING_POSIX */
/**
* @ingroup Fd
*
* This function will return whether this file has been opend.
*
* @param pathname the file path name.
*
* @return 0 on file has been open successfully, -1 on open failed.
*/
int fd_is_open(const char *pathname)
{
char *fullpath;
unsigned int index;
struct dfs_filesystem *fs;
struct dfs_file *fd;
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
fullpath = dfs_normalize_path(NULL, pathname);
if (fullpath != NULL)
{
char *mountpath;
fs = dfs_filesystem_lookup(fullpath);
if (fs == NULL)
{
/* can't find mounted file system */
rt_free(fullpath);
return -1;
}
/* get file path name under mounted file system */
if (fs->path[0] == '/' && fs->path[1] == '\0')
mountpath = fullpath;
else
mountpath = fullpath + strlen(fs->path);
dfs_lock();
for (index = 0; index < fdt->maxfd; index++)
{
fd = fdt->fds[index];
if (fd == NULL || fd->vnode->fops == NULL || fd->vnode->path == NULL) continue;
if (fd->vnode->fs == fs && strcmp(fd->vnode->path, mountpath) == 0)
{
/* found file in file descriptor table */
rt_free(fullpath);
dfs_unlock();
return 0;
}
}
dfs_unlock();
rt_free(fullpath);
}
return -1;
}
rt_err_t sys_dup2(int oldfd, int newfd)
{
struct dfs_fdtable *fdt = NULL;
int ret = 0;
int retfd = -1;
dfs_file_lock();
/* check old fd */
fdt = dfs_fdtable_get();
if ((oldfd < 0) || (oldfd >= fdt->maxfd))
{
goto exit;
}
if (!fdt->fds[oldfd])
{
goto exit;
}
if (newfd < 0)
{
goto exit;
}
if (newfd >= fdt->maxfd)
{
newfd = fd_slot_expand(fdt, newfd);
if (newfd < 0)
{
goto exit;
}
}
if (fdt->fds[newfd] == fdt->fds[oldfd])
{
/* ok, return newfd */
retfd = newfd;
goto exit;
}
if (fdt->fds[newfd])
{
ret = dfs_file_close(fdt->fds[newfd]);
if (ret < 0)
{
goto exit;
}
fd_release(newfd);
}
fdt->fds[newfd] = fdt->fds[oldfd];
/* inc ref_count */
fdt->fds[newfd]->ref_count++;
retfd = newfd;
exit:
dfs_file_unlock();
return retfd;
}
static int fd_get_fd_index_form_fdt(struct dfs_fdtable *fdt, struct dfs_file *file)
{
int fd = -1;
if (file == RT_NULL)
{
return -1;
}
dfs_file_lock();
for(int index = 0; index < (int)fdt->maxfd; index++)
{
if(fdt->fds[index] == file)
{
fd = index;
break;
}
}
dfs_file_unlock();
return fd;
}
int fd_get_fd_index(struct dfs_file *file)
{
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
return fd_get_fd_index_form_fdt(fdt, file);
}
int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
{
int retfd = -1;
if (!file)
{
return retfd;
}
if (!fdt)
{
return retfd;
}
dfs_file_lock();
/* check old fd */
if ((fd < 0) || (fd >= fdt->maxfd))
{
goto exit;
}
if (fdt->fds[fd])
{
goto exit;
}
/* inc ref_count */
file->ref_count++;
fdt->fds[fd] = file;
retfd = fd;
exit:
dfs_file_unlock();
return retfd;
}
void fd_init(struct dfs_file *fd)
{
if (fd)
{
fd->magic = DFS_FD_MAGIC;
fd->ref_count = 1;
fd->pos = 0;
fd->vnode = NULL;
fd->data = NULL;
}
}
/**
* this function will return a sub-path name under directory.
*
* @param directory the parent directory.
* @param filename the filename.
*
* @return the subdir pointer in filename
*/
const char *dfs_subdir(const char *directory, const char *filename)
{
const char *dir;
if (strlen(directory) == strlen(filename)) /* it's a same path */
return NULL;
dir = filename + strlen(directory);
if ((*dir != '/') && (dir != filename))
{
dir --;
}
return dir;
}
RTM_EXPORT(dfs_subdir);
/**
* this function will normalize a path according to specified parent directory
* and file name.
*
* @param directory the parent path
* @param filename the file name
*
* @return the built full file path (absolute path)
*/
char *dfs_normalize_path(const char *directory, const char *filename)
{
char *fullpath;
char *dst0, *dst, *src;
/* check parameters */
RT_ASSERT(filename != NULL);
#ifdef DFS_USING_WORKDIR
if (directory == NULL) /* shall use working directory */
{
#ifdef RT_USING_SMART
directory = lwp_getcwd();
#else
directory = &working_directory[0];
#endif
}
#else
if ((directory == NULL) && (filename[0] != '/'))
{
rt_kprintf(NO_WORKING_DIR);
return NULL;
}
#endif
if (filename[0] != '/') /* it's a absolute path, use it directly */
{
fullpath = (char *)rt_malloc(strlen(directory) + strlen(filename) + 2);
if (fullpath == NULL)
return NULL;
/* join path and file name */
rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
"%s/%s", directory, filename);
}
else
{
fullpath = rt_strdup(filename); /* copy string */
if (fullpath == NULL)
return NULL;
}
src = fullpath;
dst = fullpath;
dst0 = dst;
while (1)
{
char c = *src;
if (c == '.')
{
if (!src[1]) src++; /* '.' and ends */
else if (src[1] == '/')
{
/* './' case */
src += 2;
while ((*src == '/') && (*src != '\0'))
src++;
continue;
}
else if (src[1] == '.')
{
if (!src[2])
{
/* '..' and ends case */
src += 2;
goto up_one;
}
else if (src[2] == '/')
{
/* '../' case */
src += 3;
while ((*src == '/') && (*src != '\0'))
src++;
goto up_one;
}
}
}
/* copy up the next '/' and erase all '/' */
while ((c = *src++) != '\0' && c != '/')
*dst++ = c;
if (c == '/')
{
*dst++ = '/';
while (c == '/')
c = *src++;
src--;
}
else if (!c)
break;
continue;
up_one:
/* keep the topmost root directory */
if (dst - dst0 != 1 || dst[-1] != '/')
{
dst--;
if (dst < dst0)
{
rt_free(fullpath);
return NULL;
}
}
while (dst0 < dst && dst[-1] != '/')
dst--;
}
*dst = '\0';
/* remove '/' in the end of path if exist */
dst--;
if ((dst != fullpath) && (*dst == '/'))
*dst = '\0';
/* final check fullpath is not empty, for the special path of lwext "/.." */
if ('\0' == fullpath[0])
{
fullpath[0] = '/';
fullpath[1] = '\0';
}
return fullpath;
}
RTM_EXPORT(dfs_normalize_path);
/**
* This function will get the file descriptor table of current process.
*/
struct dfs_fdtable *dfs_fdtable_get(void)
{
struct dfs_fdtable *fdt;
#ifdef RT_USING_SMART
struct rt_lwp *lwp;
lwp = (struct rt_lwp *)rt_thread_self()->lwp;
if (lwp)
fdt = &lwp->fdt;
else
fdt = &_fdtab;
#else
fdt = &_fdtab;
#endif
return fdt;
}
#ifdef RT_USING_SMART
struct dfs_fdtable *dfs_fdtable_get_pid(int pid)
{
struct rt_lwp *lwp = RT_NULL;
struct dfs_fdtable *fdt = RT_NULL;
lwp = lwp_from_pid(pid);
if (lwp)
{
fdt = &lwp->fdt;
}
return fdt;
}
#endif
struct dfs_fdtable *dfs_fdtable_get_global(void)
{
return &_fdtab;
}
#ifdef RT_USING_FINSH
int list_fd(void)
{
int index;
struct dfs_fdtable *fd_table;
fd_table = dfs_fdtable_get();
if (!fd_table) return -1;
dfs_lock();
rt_kprintf("fd type ref magic path\n");
rt_kprintf("-- ------ --- ----- ------\n");
for (index = 0; index < (int)fd_table->maxfd; index++)
{
struct dfs_file *fd = fd_table->fds[index];
if (fd && fd->vnode->fops)
{
rt_kprintf("%2d ", index);
if (fd->vnode->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir");
else if (fd->vnode->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
else if (fd->vnode->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket");
else if (fd->vnode->type == FT_USER) rt_kprintf("%-7.7s ", "user");
else if (fd->vnode->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device");
else rt_kprintf("%-8.8s ", "unknown");
rt_kprintf("%3d ", fd->vnode->ref_count);
rt_kprintf("%04x ", fd->magic);
if (fd->vnode->path)
{
rt_kprintf("%s\n", fd->vnode->path);
}
else
{
rt_kprintf("\n");
}
}
}
dfs_unlock();
return 0;
}
#ifdef RT_USING_SMART
static int lsofp(int pid)
{
int index;
struct dfs_fdtable *fd_table = RT_NULL;
if (pid == (-1))
{
fd_table = dfs_fdtable_get();
if (!fd_table) return -1;
}
else
{
fd_table = dfs_fdtable_get_pid(pid);
if (!fd_table)
{
rt_kprintf("PID %s is not a applet(lwp)\n", pid);
return -1;
}
}
rt_kprintf("--- -- ------ ------ ----- ---------- ---------- ---------- ------\n");
rt_enter_critical();
for (index = 0; index < (int)fd_table->maxfd; index++)
{
struct dfs_file *fd = fd_table->fds[index];
if (fd && fd->vnode->fops)
{
if(pid == (-1))
{
rt_kprintf(" K ");
}
else
{
rt_kprintf("%3d ", pid);
}
rt_kprintf("%2d ", index);
if (fd->vnode->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir");
else if (fd->vnode->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
else if (fd->vnode->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket");
else if (fd->vnode->type == FT_USER) rt_kprintf("%-7.7s ", "user");
else if (fd->vnode->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device");
else rt_kprintf("%-8.8s ", "unknown");
rt_kprintf("%6d ", fd->vnode->ref_count);
rt_kprintf("%04x 0x%.8x ", fd->magic, (int)(size_t)fd->vnode);
if(fd->vnode == RT_NULL)
{
rt_kprintf("0x%.8x 0x%.8x ", (int)0x00000000, (int)(size_t)fd);
}
else
{
rt_kprintf("0x%.8x 0x%.8x ", (int)(size_t)(fd->vnode->data), (int)(size_t)fd);
}
if (fd->vnode->path)
{
rt_kprintf("%s \n", fd->vnode->path);
}
else
{
rt_kprintf("\n");
}
}
}
rt_exit_critical();
return 0;
}
int lsof(int argc, char *argv[])
{
rt_kprintf("PID fd type fd-ref magic vnode vnode/data addr path \n");
if (argc == 1)
{
struct rt_list_node *node, *list;
struct lwp_avl_struct *pids = lwp_get_pid_ary();
lsofp(-1);
for (int index = 0; index < RT_LWP_MAX_NR; index++)
{
struct rt_lwp *lwp = (struct rt_lwp *)pids[index].data;
if (lwp)
{
list = &lwp->t_grp;
for (node = list->next; node != list; node = node->next)
{
lsofp(lwp_to_pid(lwp));
}
}
}
}
else if (argc == 3)
{
if (argv[1][0] == '-' && argv[1][1] == 'p')
{
int pid = atoi(argv[2]);
lsofp(pid);
}
}
return 0;
}
MSH_CMD_EXPORT(lsof, list open files);
#endif /* RT_USING_SMART */
#endif
/**@}*/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,657 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
* 2017-11-30 Bernard fix the filesystem_operation_table issue.
* 2017-12-05 Bernard fix the fs type search issue in mkfs.
*/
#include <dfs_fs.h>
#include <dfs_file.h>
#include "dfs_private.h"
/**
* @addtogroup FsApi
* @{
*/
/**
* this function will register a file system instance to device file system.
*
* @param ops the file system instance to be registered.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_register(const struct dfs_filesystem_ops *ops)
{
int ret = RT_EOK;
const struct dfs_filesystem_ops **empty = NULL;
const struct dfs_filesystem_ops **iter;
/* lock filesystem */
dfs_lock();
/* check if this filesystem was already registered */
for (iter = &filesystem_operation_table[0];
iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++)
{
/* find out an empty filesystem type entry */
if (*iter == NULL)
(empty == NULL) ? (empty = iter) : 0;
else if (strcmp((*iter)->name, ops->name) == 0)
{
rt_set_errno(-EEXIST);
ret = -1;
break;
}
}
/* save the filesystem's operations */
if (empty == NULL)
{
rt_set_errno(-ENOSPC);
LOG_E("There is no space to register this file system (%s).", ops->name);
ret = -1;
}
else if (ret == RT_EOK)
{
*empty = ops;
}
dfs_unlock();
return ret;
}
/**
* 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
*/
struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
{
struct dfs_filesystem *iter;
struct dfs_filesystem *fs = NULL;
uint32_t fspath, prefixlen;
prefixlen = 0;
RT_ASSERT(path);
/* lock filesystem */
dfs_lock();
/* lookup it in the filesystem table */
for (iter = &filesystem_table[0];
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
{
if ((iter->path == NULL) || (iter->ops == NULL))
continue;
fspath = strlen(iter->path);
if ((fspath < prefixlen)
|| (strncmp(iter->path, path, fspath) != 0))
continue;
/* check next path separator */
if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
continue;
fs = iter;
prefixlen = fspath;
}
dfs_unlock();
return fs;
}
/**
* this function will return the mounted path for specified device.
*
* @param device the device object which is mounted.
*
* @return the mounted path or NULL if none device mounted.
*/
const char *dfs_filesystem_get_mounted_path(struct rt_device *device)
{
const char *path = NULL;
struct dfs_filesystem *iter;
dfs_lock();
for (iter = &filesystem_table[0];
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
{
/* find the mounted device */
if (iter->ops == NULL) continue;
else if (iter->dev_id == device)
{
path = iter->path;
break;
}
}
/* release filesystem_table lock */
dfs_unlock();
return path;
}
/**
* 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.
*/
int dfs_filesystem_get_partition(struct dfs_partition *part,
uint8_t *buf,
uint32_t pindex)
{
#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
#define DPT_ITEM_SIZE 16 /* partition item size */
uint8_t *dpt;
uint8_t type;
RT_ASSERT(part != NULL);
RT_ASSERT(buf != NULL);
dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
/* check if it is a valid partition table */
if ((*dpt != 0x80) && (*dpt != 0x00))
return -EIO;
/* get partition type */
type = *(dpt + 4);
if (type == 0)
return -EIO;
/* 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 */
else
{
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");
}
return RT_EOK;
}
/**
* 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.
*/
int dfs_mount(const char *device_name,
const char *path,
const char *filesystemtype,
unsigned long rwflag,
const void *data)
{
const struct dfs_filesystem_ops **ops;
struct dfs_filesystem *iter;
struct dfs_filesystem *fs = NULL;
char *fullpath = NULL;
rt_device_t dev_id;
/* open specific device */
if (device_name == NULL)
{
/* which is a non-device filesystem mount */
dev_id = NULL;
}
else if ((dev_id = rt_device_find(device_name)) == NULL)
{
/* no this device */
rt_set_errno(-ENODEV);
return -1;
}
/* find out the specific filesystem */
dfs_lock();
for (ops = &filesystem_operation_table[0];
ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++)
if ((*ops != NULL) && (strncmp((*ops)->name, filesystemtype, strlen((*ops)->name)) == 0))
break;
dfs_unlock();
if (ops == &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX])
{
/* can't find filesystem */
rt_set_errno(-ENODEV);
return -1;
}
/* check if there is mount implementation */
if ((*ops == NULL) || ((*ops)->mount == NULL))
{
rt_set_errno(-ENOSYS);
return -1;
}
/* make full path for special file */
fullpath = dfs_normalize_path(NULL, path);
if (fullpath == NULL) /* not an abstract path */
{
rt_set_errno(-ENOTDIR);
return -1;
}
/* Check if the path exists or not, raw APIs call, fixme */
if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
{
struct dfs_file fd;
fd_init(&fd);
if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0)
{
rt_free(fullpath);
rt_set_errno(-ENOTDIR);
return -1;
}
dfs_file_close(&fd);
}
/* check whether the file system mounted or not in the filesystem table
* if it is unmounted yet, find out an empty entry */
dfs_lock();
for (iter = &filesystem_table[0];
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
{
/* check if it is an empty filesystem table entry? if it is, save fs */
if (iter->ops == NULL)
(fs == NULL) ? (fs = iter) : 0;
/* check if the PATH is mounted */
else if (strcmp(iter->path, path) == 0)
{
rt_set_errno(-EINVAL);
goto err1;
}
}
if ((fs == NULL) && (iter == &filesystem_table[DFS_FILESYSTEMS_MAX]))
{
rt_set_errno(-ENOSPC);
LOG_E("There is no space to mount this file system (%s).", filesystemtype);
goto err1;
}
/* register file system */
fs->path = fullpath;
fs->ops = *ops;
fs->dev_id = dev_id;
/* For UFS, record the real filesystem name */
fs->data = (void *) filesystemtype;
/* release filesystem_table lock */
dfs_unlock();
/* open device, but do not check the status of device */
if (dev_id != NULL)
{
if (rt_device_open(fs->dev_id,
RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{
/* The underlying device has error, clear the entry. */
dfs_lock();
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
goto err1;
}
}
/* call mount of this filesystem */
if ((*ops)->mount(fs, rwflag, data) < 0)
{
/* close device */
if (dev_id != NULL)
rt_device_close(fs->dev_id);
/* mount failed */
dfs_lock();
/* clear filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
goto err1;
}
return 0;
err1:
dfs_unlock();
rt_free(fullpath);
return -1;
}
/**
* this function will unmount a file system on specified path.
*
* @param specialfile the specified path which mounted a file system.
*
* @return 0 on successful or -1 on failed.
*/
int dfs_unmount(const char *specialfile)
{
char *fullpath;
struct dfs_filesystem *iter;
struct dfs_filesystem *fs = NULL;
fullpath = dfs_normalize_path(NULL, specialfile);
if (fullpath == NULL)
{
rt_set_errno(-ENOTDIR);
return -1;
}
/* lock filesystem */
dfs_lock();
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;
}
}
if (fs == NULL ||
fs->ops->unmount == NULL ||
fs->ops->unmount(fs) < 0)
{
goto err1;
}
/* close device, but do not check the status of device */
if (fs->dev_id != NULL)
rt_device_close(fs->dev_id);
if (fs->path != NULL)
rt_free(fs->path);
/* clear this filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock();
rt_free(fullpath);
return 0;
err1:
dfs_unlock();
rt_free(fullpath);
return -1;
}
/**
* 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.
*/
int dfs_mkfs(const char *fs_name, const char *device_name)
{
int index;
rt_device_t dev_id = NULL;
/* check device name, and it should not be NULL */
if (device_name != NULL)
dev_id = rt_device_find(device_name);
if (dev_id == NULL)
{
rt_set_errno(-ENODEV);
LOG_E("Device (%s) was not found", device_name);
return -1;
}
/* lock file system */
dfs_lock();
/* find the file system operations */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++)
{
if (filesystem_operation_table[index] != NULL &&
strncmp(filesystem_operation_table[index]->name, fs_name,
strlen(filesystem_operation_table[index]->name)) == 0)
break;
}
dfs_unlock();
if (index < DFS_FILESYSTEM_TYPES_MAX)
{
/* find file system operation */
const struct dfs_filesystem_ops *ops = filesystem_operation_table[index];
if (ops->mkfs == NULL)
{
LOG_E("The file system (%s) mkfs function was not implement", fs_name);
rt_set_errno(-ENOSYS);
return -1;
}
return ops->mkfs(dev_id, fs_name);
}
LOG_E("File system (%s) was not found.", fs_name);
return -1;
}
/**
* 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.
*/
int dfs_statfs(const char *path, struct statfs *buffer)
{
struct dfs_filesystem *fs;
fs = dfs_filesystem_lookup(path);
if (fs != NULL)
{
if (fs->ops->statfs != NULL)
return fs->ops->statfs(fs, buffer);
}
rt_set_errno(-ENOSYS);
return -1;
}
#ifdef RT_USING_DFS_MNTTABLE
int dfs_mount_table(void)
{
int index = 0;
while (1)
{
if (mount_table[index].path == NULL) break;
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)
{
LOG_E("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype,
mount_table[index].path);
return -RT_ERROR;
}
index ++;
}
return 0;
}
INIT_ENV_EXPORT(dfs_mount_table);
int dfs_mount_device(rt_device_t dev)
{
int index = 0;
if(dev == RT_NULL) {
rt_kprintf("the device is NULL to be mounted.\n");
return -RT_ERROR;
}
while (1)
{
if (mount_table[index].path == NULL) break;
if(strcmp(mount_table[index].device_name, dev->parent.name) == 0) {
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)
{
LOG_E("mount fs[%s] device[%s] to %s failed.\n", mount_table[index].filesystemtype, dev->parent.name,
mount_table[index].path);
return -RT_ERROR;
} else {
LOG_D("mount fs[%s] device[%s] to %s ok.\n", mount_table[index].filesystemtype, dev->parent.name,
mount_table[index].path);
return RT_EOK;
}
}
index ++;
}
rt_kprintf("can't find device:%s to be mounted.\n", dev->parent.name);
return -RT_ERROR;
}
int dfs_unmount_device(rt_device_t dev)
{
struct dfs_filesystem *iter;
struct dfs_filesystem *fs = NULL;
/* lock filesystem */
dfs_lock();
for (iter = &filesystem_table[0];
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
{
/* check if the PATH is mounted */
if (strcmp(iter->dev_id->parent.name, dev->parent.name) == 0)
{
fs = iter;
break;
}
}
if (fs == NULL ||
fs->ops->unmount == NULL ||
fs->ops->unmount(fs) < 0)
{
goto err1;
}
/* close device, but do not check the status of device */
if (fs->dev_id != NULL)
rt_device_close(fs->dev_id);
if (fs->path != NULL)
rt_free(fs->path);
/* clear this filesystem table entry */
rt_memset(fs, 0, sizeof(struct dfs_filesystem));
dfs_unlock();
return 0;
err1:
dfs_unlock();
return -1;
}
#endif
#ifdef RT_USING_FINSH
#include <finsh.h>
void mkfs(const char *fs_name, const char *device_name)
{
dfs_mkfs(fs_name, device_name);
}
FINSH_FUNCTION_EXPORT(mkfs, make a file system);
int df(const char *path)
{
int result;
int minor = 0;
long long cap;
struct statfs buffer;
int unit_index = 0;
char *unit_str[] = {"KB", "MB", "GB"};
result = dfs_statfs(path ? path : NULL, &buffer);
if (result != 0)
{
if (rt_get_errno() == -ENOSYS)
rt_kprintf("The function is not implemented.\n");
else
rt_kprintf("statfs failed: errno=%d.\n", rt_get_errno());
return -1;
}
cap = ((long long)buffer.f_bsize) * ((long long)buffer.f_bfree) / 1024LL;
for (unit_index = 0; unit_index < 2; unit_index ++)
{
if (cap < 1024) break;
minor = (cap % 1024) * 10 / 1024; /* only one decimal point */
cap = cap / 1024;
}
rt_kprintf("disk free: %d.%d %s [ %d block, %d bytes per block ]\n",
(unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize);
return 0;
}
FINSH_FUNCTION_EXPORT(df, get disk free);
#endif
/**@}*/

File diff suppressed because it is too large Load diff