2022-12-03 12:07:44 +08:00
|
|
|
/*
|
2023-06-10 21:35:25 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2022-12-03 12:07:44 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2022-10-24 flybreak the first version
|
2023-02-01 11:00:03 +08:00
|
|
|
* 2023-02-01 xqyjlj fix cannot open the same file repeatedly in 'w' mode
|
2023-09-20 15:22:09 +08:00
|
|
|
* 2023-09-20 zmq810150896 adds truncate functionality and standardized unlink adaptations
|
2022-12-03 12:07:44 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_fs.h>
|
2023-06-10 21:35:25 +08:00
|
|
|
#include <dfs_dentry.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <dfs_file.h>
|
2023-06-10 21:35:25 +08:00
|
|
|
#include <dfs_mnt.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <lwp.h>
|
|
|
|
#include <lwp_user_mm.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "dfs_tmpfs.h"
|
|
|
|
|
|
|
|
#define DBG_TAG "tmpfs"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
#include "dfs_pcache.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page);
|
|
|
|
static ssize_t dfs_tmp_page_write(struct dfs_page *page);
|
|
|
|
|
|
|
|
static struct dfs_aspace_ops dfs_tmp_aspace_ops =
|
|
|
|
{
|
|
|
|
.read = dfs_tmp_page_read,
|
|
|
|
.write = dfs_tmp_page_write,
|
|
|
|
};
|
|
|
|
#endif
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
static int _path_separate(const char *path, char *parent_path, char *file_name)
|
|
|
|
{
|
|
|
|
const char *path_p, *path_q;
|
|
|
|
|
|
|
|
RT_ASSERT(path[0] == '/');
|
|
|
|
|
|
|
|
file_name[0] = '\0';
|
|
|
|
path_p = path_q = &path[1];
|
|
|
|
__next_dir:
|
|
|
|
while (*path_q != '/' && *path_q != '\0')
|
|
|
|
{
|
|
|
|
path_q++;
|
|
|
|
}
|
|
|
|
if (path_q != path_p) /*sub dir*/
|
|
|
|
{
|
|
|
|
if (*path_q != '\0')
|
|
|
|
{
|
|
|
|
path_q++;
|
|
|
|
path_p = path_q;
|
|
|
|
goto __next_dir;
|
|
|
|
}
|
|
|
|
else /* Last level dir */
|
|
|
|
{
|
|
|
|
rt_memcpy(parent_path, path, path_p - path - 1);
|
|
|
|
parent_path[path_p - path - 1] = '\0';
|
|
|
|
rt_memcpy(file_name, path_p, path_q - path_p);
|
|
|
|
file_name[path_q - path_p] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parent_path[0] == 0)
|
|
|
|
{
|
|
|
|
parent_path[0] = '/';
|
|
|
|
parent_path[1] = '\0';
|
|
|
|
}
|
|
|
|
LOG_D("parent_path: %s", parent_path);
|
|
|
|
LOG_D("file_name: %s", file_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _get_subdir(const char *path, char *name)
|
|
|
|
{
|
|
|
|
const char *subpath = path;
|
|
|
|
while (*subpath == '/' && *subpath)
|
|
|
|
subpath ++;
|
|
|
|
while (*subpath != '/' && *subpath)
|
|
|
|
{
|
|
|
|
*name = *subpath;
|
|
|
|
name ++;
|
|
|
|
subpath ++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _free_subdir(struct tmpfs_file *dfile)
|
|
|
|
{
|
|
|
|
struct tmpfs_file *file;
|
|
|
|
rt_list_t *list, *temp_list;
|
2023-09-13 10:37:00 +08:00
|
|
|
struct tmpfs_sb *superblock;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
|
|
|
|
|
|
|
|
rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
|
|
|
|
{
|
|
|
|
file = rt_list_entry(list, struct tmpfs_file, sibling);
|
|
|
|
if (file->type == TMPFS_TYPE_DIR)
|
|
|
|
{
|
|
|
|
_free_subdir(file);
|
|
|
|
}
|
|
|
|
if (file->data != NULL)
|
|
|
|
{
|
|
|
|
/* TODO: fix for rt-smart */
|
|
|
|
rt_free(file->data);
|
|
|
|
}
|
2023-09-13 10:37:00 +08:00
|
|
|
|
|
|
|
superblock = file->sb;
|
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
|
|
|
|
rt_spin_lock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_list_remove(&(file->sibling));
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_free(file);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-11 13:12:40 +08:00
|
|
|
static int dfs_tmpfs_mount(struct dfs_mnt *mnt,
|
|
|
|
unsigned long rwflag,
|
|
|
|
const void *data)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
|
|
|
superblock = rt_calloc(1, sizeof(struct tmpfs_sb));
|
2023-06-10 21:35:25 +08:00
|
|
|
if (superblock)
|
|
|
|
{
|
|
|
|
superblock->df_size = sizeof(struct tmpfs_sb);
|
|
|
|
superblock->magic = TMPFS_MAGIC;
|
|
|
|
rt_list_init(&superblock->sibling);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
superblock->root.name[0] = '/';
|
|
|
|
superblock->root.sb = superblock;
|
|
|
|
superblock->root.type = TMPFS_TYPE_DIR;
|
|
|
|
rt_list_init(&superblock->root.sibling);
|
|
|
|
rt_list_init(&superblock->root.subdirs);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock_init(&superblock->lock);
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
mnt->data = superblock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
return RT_EOK;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
static int dfs_tmpfs_unmount(struct dfs_mnt *mnt)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
/* FIXME: don't unmount on busy. */
|
2023-06-10 21:35:25 +08:00
|
|
|
superblock = (struct tmpfs_sb *)mnt->data;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
mnt->data = NULL;
|
2022-12-03 12:07:44 +08:00
|
|
|
_free_subdir(&(superblock->root));
|
|
|
|
rt_free(superblock);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_tmpfs_statfs(struct dfs_mnt *mnt, struct statfs *buf)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
superblock = (struct tmpfs_sb *)mnt->data;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
RT_ASSERT(buf != NULL);
|
|
|
|
|
|
|
|
buf->f_bsize = 512;
|
|
|
|
buf->f_blocks = (superblock->df_size + 511) / 512;
|
|
|
|
buf->f_bfree = 1;
|
2023-03-29 14:37:42 +08:00
|
|
|
buf->f_bavail = buf->f_bfree;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_tmpfs_ioctl(struct dfs_file *file, int cmd, void *args)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
|
|
|
RT_ASSERT(d_file != NULL);
|
|
|
|
|
|
|
|
superblock = d_file->sb;
|
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
|
|
|
case RT_FIOMMAP2:
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
|
|
|
|
if (mmap2)
|
|
|
|
{
|
|
|
|
if (mmap2->length > file->vnode->size)
|
|
|
|
{
|
|
|
|
return -RT_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_D("tmpfile mmap ptr:%x , size:%d\n", d_file->data, mmap2->length);
|
|
|
|
mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, d_file->data, mmap2->length, 0);
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
|
|
|
|
const char *path,
|
|
|
|
rt_size_t *size)
|
|
|
|
{
|
|
|
|
const char *subpath, *curpath, *filename = RT_NULL;
|
|
|
|
char subdir_name[TMPFS_NAME_MAX];
|
|
|
|
struct tmpfs_file *file, *curfile;
|
|
|
|
rt_list_t *list;
|
|
|
|
|
|
|
|
subpath = path;
|
|
|
|
while (*subpath == '/' && *subpath)
|
|
|
|
subpath ++;
|
|
|
|
if (! *subpath) /* is root directory */
|
|
|
|
{
|
|
|
|
*size = 0;
|
|
|
|
return &(superblock->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
curpath = subpath;
|
|
|
|
curfile = &superblock->root;
|
|
|
|
|
|
|
|
find_subpath:
|
|
|
|
while (*subpath != '/' && *subpath)
|
|
|
|
subpath ++;
|
|
|
|
|
|
|
|
if (! *subpath) /* is last directory */
|
|
|
|
filename = curpath;
|
|
|
|
else
|
|
|
|
subpath ++; /* skip '/' */
|
|
|
|
|
|
|
|
memset(subdir_name, 0, TMPFS_NAME_MAX);
|
|
|
|
_get_subdir(curpath, subdir_name);
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
rt_list_for_each(list, &curfile->subdirs)
|
|
|
|
{
|
|
|
|
file = rt_list_entry(list, struct tmpfs_file, sibling);
|
|
|
|
if (filename) /* find file */
|
|
|
|
{
|
|
|
|
if (rt_strcmp(file->name, filename) == 0)
|
|
|
|
{
|
|
|
|
*size = file->size;
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rt_strcmp(file->name, subdir_name) == 0)
|
|
|
|
{
|
|
|
|
*size = file->size;
|
|
|
|
curpath = subpath;
|
|
|
|
curfile = file;
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
goto find_subpath;
|
|
|
|
}
|
|
|
|
}
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
/* not found */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
static ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
rt_size_t length;
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
|
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
|
|
|
RT_ASSERT(d_file != NULL);
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
|
2023-08-02 13:59:43 +08:00
|
|
|
if (count < file->vnode->size - *pos)
|
2022-12-03 12:07:44 +08:00
|
|
|
length = count;
|
|
|
|
else
|
2023-08-02 13:59:43 +08:00
|
|
|
length = file->vnode->size - *pos;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
if (length > 0)
|
2023-08-02 13:59:43 +08:00
|
|
|
memcpy(buf, &(d_file->data[*pos]), length);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
/* update file current position */
|
2023-08-02 13:59:43 +08:00
|
|
|
*pos += length;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
static ssize_t dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(d_file != NULL);
|
|
|
|
|
|
|
|
superblock = d_file->sb;
|
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
|
2023-08-02 13:59:43 +08:00
|
|
|
if (count + *pos > file->vnode->size)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t *ptr;
|
2023-08-02 13:59:43 +08:00
|
|
|
ptr = rt_realloc(d_file->data, *pos + count);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_set_errno(-ENOMEM);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock(&superblock->lock);
|
2023-08-02 13:59:43 +08:00
|
|
|
superblock->df_size += (*pos - d_file->size + count);
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
/* update d_file and file size */
|
|
|
|
d_file->data = ptr;
|
2023-08-02 13:59:43 +08:00
|
|
|
d_file->size = *pos + count;
|
2023-06-10 21:35:25 +08:00
|
|
|
file->vnode->size = d_file->size;
|
2022-12-03 12:07:44 +08:00
|
|
|
LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0)
|
2023-08-02 13:59:43 +08:00
|
|
|
memcpy(d_file->data + *pos, buf, count);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
/* update file current position */
|
2023-08-02 13:59:43 +08:00
|
|
|
*pos += count;
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
static off_t dfs_tmpfs_lseek(struct dfs_file *file, off_t offset, int wherece)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-08-02 13:59:43 +08:00
|
|
|
switch (wherece)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-08-02 13:59:43 +08:00
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset += file->fpos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
offset += file->vnode->size;
|
|
|
|
break;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-08-02 13:59:43 +08:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset <= (off_t)file->vnode->size)
|
|
|
|
{
|
|
|
|
return offset;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2023-06-11 13:12:40 +08:00
|
|
|
static int dfs_tmpfs_close(struct dfs_file *file)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-09-20 15:22:09 +08:00
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
2023-10-17 13:07:59 +08:00
|
|
|
|
2023-09-20 15:22:09 +08:00
|
|
|
if (file->vnode->ref_count != 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
|
|
|
|
|
|
|
if (d_file == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (d_file->fre_memory == RT_TRUE)
|
|
|
|
{
|
|
|
|
if (d_file->data != NULL)
|
|
|
|
{
|
|
|
|
rt_free(d_file->data);
|
|
|
|
d_file->data = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(d_file);
|
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_detach(&file->vnode->lock);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-06-11 13:12:40 +08:00
|
|
|
static int dfs_tmpfs_open(struct dfs_file *file)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
struct tmpfs_file *d_file;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
|
|
|
RT_ASSERT(d_file != RT_NULL);
|
2022-12-03 12:07:44 +08:00
|
|
|
/* Creates a new file.
|
2023-06-10 21:35:25 +08:00
|
|
|
* If the file is existing, it is truncated and overwritten.
|
|
|
|
*/
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->flags & O_TRUNC)
|
|
|
|
{
|
|
|
|
d_file->size = 0;
|
2023-06-10 21:35:25 +08:00
|
|
|
file->vnode->size = d_file->size;
|
|
|
|
file->fpos = file->vnode->size;
|
2022-12-03 12:07:44 +08:00
|
|
|
if (d_file->data != NULL)
|
|
|
|
{
|
|
|
|
/* ToDo: fix for rt-smart. */
|
|
|
|
rt_free(d_file->data);
|
|
|
|
d_file->data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file->flags & O_APPEND)
|
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos = file->vnode->size;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if(file->vnode->ref_count == 1)
|
|
|
|
{
|
|
|
|
rt_mutex_init(&file->vnode->lock, file->dentry->pathname, RT_IPC_FLAG_PRIO);
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
static int dfs_tmpfs_stat(struct dfs_dentry *dentry, struct stat *st)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
rt_size_t size;
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
superblock = (struct tmpfs_sb *)dentry->mnt->data;
|
|
|
|
d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
if (d_file == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
st->st_dev = (dev_t)(size_t)(dentry->mnt->dev_id);
|
|
|
|
st->st_ino = (ino_t)dfs_dentry_full_path_crc32(dentry);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (d_file->type == TMPFS_TYPE_DIR)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
st->st_mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st->st_mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
st->st_size = d_file->size;
|
|
|
|
st->st_mtime = 0;
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-06-11 13:12:40 +08:00
|
|
|
static int dfs_tmpfs_getdents(struct dfs_file *file,
|
2022-12-03 12:07:44 +08:00
|
|
|
struct dirent *dirp,
|
|
|
|
uint32_t count)
|
|
|
|
{
|
|
|
|
rt_size_t index, end;
|
|
|
|
struct dirent *d;
|
|
|
|
struct tmpfs_file *d_file, *n_file;
|
|
|
|
rt_list_t *list;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
|
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
superblock = d_file->sb;
|
|
|
|
RT_ASSERT(superblock != RT_NULL);
|
|
|
|
|
|
|
|
/* make integer count */
|
|
|
|
count = (count / sizeof(struct dirent));
|
|
|
|
if (count == 0)
|
2023-10-17 13:07:59 +08:00
|
|
|
{
|
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
return -EINVAL;
|
2023-10-17 13:07:59 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
end = file->fpos + count;
|
2022-12-03 12:07:44 +08:00
|
|
|
index = 0;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
rt_list_for_each(list, &d_file->subdirs)
|
|
|
|
{
|
|
|
|
n_file = rt_list_entry(list, struct tmpfs_file, sibling);
|
2023-06-10 21:35:25 +08:00
|
|
|
if (index >= (rt_size_t)file->fpos)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
d = dirp + count;
|
|
|
|
if (d_file->type == TMPFS_TYPE_FILE)
|
|
|
|
{
|
|
|
|
d->d_type = DT_REG;
|
|
|
|
}
|
|
|
|
if (d_file->type == TMPFS_TYPE_DIR)
|
|
|
|
{
|
|
|
|
d->d_type = DT_DIR;
|
|
|
|
}
|
|
|
|
d->d_namlen = RT_NAME_MAX;
|
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
|
|
|
rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
|
|
|
|
|
|
|
|
count += 1;
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos += 1;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
if (index >= end)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
return count * sizeof(struct dirent);
|
|
|
|
}
|
|
|
|
|
2023-06-11 13:12:40 +08:00
|
|
|
static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
rt_size_t size;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
superblock = (struct tmpfs_sb *)dentry->mnt->data;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (d_file == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_list_remove(&(d_file->sibling));
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-09-20 15:22:09 +08:00
|
|
|
if (rt_atomic_load(&(dentry->ref_count)) == 1)
|
|
|
|
{
|
|
|
|
if (d_file->data != NULL)
|
|
|
|
{
|
|
|
|
rt_free(d_file->data);
|
|
|
|
d_file->data = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_free(d_file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d_file->fre_memory = RT_TRUE;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-06-11 13:12:40 +08:00
|
|
|
static int dfs_tmpfs_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *new_dentry)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
struct tmpfs_file *d_file, *p_file;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
rt_size_t size;
|
2023-06-10 21:35:25 +08:00
|
|
|
char parent_path[DFS_PATH_MAX], file_name[TMPFS_NAME_MAX];
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
superblock = (struct tmpfs_sb *)old_dentry->mnt->data;
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(superblock != NULL);
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
d_file = dfs_tmpfs_lookup(superblock, new_dentry->pathname, &size);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (d_file != NULL)
|
|
|
|
return -EEXIST;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
d_file = dfs_tmpfs_lookup(superblock, old_dentry->pathname, &size);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (d_file == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
/* find parent file */
|
2023-06-10 21:35:25 +08:00
|
|
|
_path_separate(new_dentry->pathname, parent_path, file_name);
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file_name[0] == '\0') /* it's root dir */
|
|
|
|
return -ENOENT;
|
|
|
|
/* open parent directory */
|
|
|
|
p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
|
|
|
|
RT_ASSERT(p_file != NULL);
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_list_remove(&(d_file->sibling));
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
|
|
|
|
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
|
|
|
|
{
|
|
|
|
struct dfs_vnode *vnode = RT_NULL;
|
|
|
|
rt_size_t size;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
|
|
|
|
if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
superblock = (struct tmpfs_sb *)dentry->mnt->data;
|
|
|
|
|
|
|
|
d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
|
|
|
|
if (d_file)
|
|
|
|
{
|
|
|
|
vnode = dfs_vnode_create();
|
|
|
|
if (vnode)
|
|
|
|
{
|
|
|
|
if (d_file->type == TMPFS_TYPE_DIR)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_REGULAR;
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
|
|
|
|
#endif
|
2023-06-10 21:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vnode->mnt = dentry->mnt;
|
|
|
|
vnode->data = d_file;
|
|
|
|
vnode->size = d_file->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
|
|
|
|
{
|
|
|
|
struct dfs_vnode *vnode = RT_NULL;
|
|
|
|
rt_size_t size;
|
|
|
|
struct tmpfs_sb *superblock;
|
|
|
|
struct tmpfs_file *d_file, *p_file;
|
|
|
|
char parent_path[DFS_PATH_MAX], file_name[TMPFS_NAME_MAX];
|
|
|
|
|
|
|
|
if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
superblock = (struct tmpfs_sb *)dentry->mnt->data;
|
2023-09-13 10:37:00 +08:00
|
|
|
RT_ASSERT(superblock != NULL);
|
2023-06-10 21:35:25 +08:00
|
|
|
|
|
|
|
vnode = dfs_vnode_create();
|
|
|
|
if (vnode)
|
|
|
|
{
|
|
|
|
/* find parent file */
|
|
|
|
_path_separate(dentry->pathname, parent_path, file_name);
|
|
|
|
if (file_name[0] == '\0') /* it's root dir */
|
|
|
|
{
|
|
|
|
dfs_vnode_destroy(vnode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open parent directory */
|
|
|
|
p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
|
|
|
|
if (p_file == NULL)
|
|
|
|
{
|
|
|
|
dfs_vnode_destroy(vnode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a file entry */
|
|
|
|
d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
|
|
|
|
if (d_file == NULL)
|
|
|
|
{
|
|
|
|
dfs_vnode_destroy(vnode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
superblock->df_size += sizeof(struct tmpfs_file);
|
|
|
|
|
|
|
|
strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
|
|
|
|
|
|
|
|
rt_list_init(&(d_file->subdirs));
|
|
|
|
rt_list_init(&(d_file->sibling));
|
|
|
|
d_file->data = NULL;
|
|
|
|
d_file->size = 0;
|
|
|
|
d_file->sb = superblock;
|
2023-09-20 15:22:09 +08:00
|
|
|
d_file->fre_memory = RT_FALSE;
|
2023-06-10 21:35:25 +08:00
|
|
|
if (type == FT_DIRECTORY)
|
|
|
|
{
|
|
|
|
d_file->type = TMPFS_TYPE_DIR;
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d_file->type = TMPFS_TYPE_FILE;
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_REGULAR;
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
|
|
|
|
#endif
|
2023-06-10 21:35:25 +08:00
|
|
|
}
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_lock(&superblock->lock);
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
|
2023-09-13 10:37:00 +08:00
|
|
|
rt_spin_unlock(&superblock->lock);
|
2023-06-10 21:35:25 +08:00
|
|
|
|
|
|
|
vnode->mnt = dentry->mnt;
|
|
|
|
vnode->data = d_file;
|
|
|
|
vnode->size = d_file->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
|
|
|
|
{
|
|
|
|
/* nothing to be freed */
|
|
|
|
if (vnode && vnode->ref_count <= 1)
|
|
|
|
{
|
|
|
|
vnode->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page)
|
|
|
|
{
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
if (page->page)
|
|
|
|
{
|
|
|
|
off_t fpos = page->fpos;
|
|
|
|
ret = dfs_tmpfs_read(file, page->page, page->size, &fpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t dfs_tmp_page_write(struct dfs_page *page)
|
|
|
|
{
|
|
|
|
struct tmpfs_file *d_file;
|
|
|
|
|
|
|
|
if (page->aspace->vnode->type == FT_DIRECTORY)
|
|
|
|
{
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
d_file = (struct tmpfs_file *)(page->aspace->vnode->data);
|
|
|
|
RT_ASSERT(d_file != RT_NULL);
|
|
|
|
rt_mutex_take(&page->aspace->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
if (page->len > 0)
|
|
|
|
memcpy(d_file->data + page->fpos, page->page, page->len);
|
|
|
|
|
|
|
|
rt_mutex_release(&page->aspace->vnode->lock);
|
|
|
|
|
|
|
|
return F_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-20 15:22:09 +08:00
|
|
|
static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset)
|
|
|
|
{
|
|
|
|
struct tmpfs_file *d_file = RT_NULL;
|
|
|
|
struct tmpfs_sb *superblock = RT_NULL;
|
|
|
|
rt_uint8_t *ptr = RT_NULL;
|
|
|
|
|
|
|
|
d_file = (struct tmpfs_file *)file->vnode->data;
|
|
|
|
RT_ASSERT(d_file != RT_NULL);
|
|
|
|
|
|
|
|
superblock = d_file->sb;
|
|
|
|
RT_ASSERT(superblock != RT_NULL);
|
|
|
|
|
|
|
|
ptr = rt_realloc(d_file->data, offset);
|
|
|
|
if (ptr == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-ENOMEM);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_spin_lock(&superblock->lock);
|
|
|
|
superblock->df_size = offset;
|
|
|
|
rt_spin_unlock(&superblock->lock);
|
|
|
|
|
|
|
|
/* update d_file and file size */
|
|
|
|
d_file->data = ptr;
|
|
|
|
d_file->size = offset;
|
|
|
|
file->vnode->size = d_file->size;
|
|
|
|
LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
static const struct dfs_file_ops _tmp_fops =
|
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
.open = dfs_tmpfs_open,
|
|
|
|
.close = dfs_tmpfs_close,
|
|
|
|
.ioctl = dfs_tmpfs_ioctl,
|
|
|
|
.read = dfs_tmpfs_read,
|
|
|
|
.write = dfs_tmpfs_write,
|
|
|
|
.lseek = dfs_tmpfs_lseek,
|
|
|
|
.getdents = dfs_tmpfs_getdents,
|
2023-09-20 15:22:09 +08:00
|
|
|
.truncate = dfs_tmpfs_truncate,
|
2023-06-10 21:35:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dfs_filesystem_ops _tmpfs_ops =
|
|
|
|
{
|
|
|
|
.name = "tmp",
|
|
|
|
.flags = DFS_FS_FLAG_DEFAULT,
|
|
|
|
.default_fops = &_tmp_fops,
|
|
|
|
|
|
|
|
.mount = dfs_tmpfs_mount,
|
|
|
|
.umount = dfs_tmpfs_unmount,
|
|
|
|
.statfs = dfs_tmpfs_statfs,
|
|
|
|
|
|
|
|
.unlink = dfs_tmpfs_unlink,
|
|
|
|
.stat = dfs_tmpfs_stat,
|
|
|
|
.rename = dfs_tmpfs_rename,
|
|
|
|
.lookup = _dfs_tmpfs_lookup,
|
|
|
|
.create_vnode = dfs_tmpfs_create_vnode,
|
|
|
|
.free_vnode = dfs_tmpfs_free_vnode
|
2022-12-03 12:07:44 +08:00
|
|
|
};
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
static struct dfs_filesystem_type _tmpfs =
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
.fs_ops = &_tmpfs_ops,
|
2022-12-03 12:07:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int dfs_tmpfs_init(void)
|
|
|
|
{
|
|
|
|
/* register tmp file system */
|
|
|
|
dfs_register(&_tmpfs);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-10 21:35:25 +08:00
|
|
|
INIT_COMPONENT_EXPORT(dfs_tmpfs_init);
|