Skip to content

Commit

Permalink
sync update dfs v2. (#8336)
Browse files Browse the repository at this point in the history
  • Loading branch information
geniusgogo authored Dec 12, 2023
1 parent a1df90d commit 304ce59
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 87 deletions.
288 changes: 229 additions & 59 deletions components/dfs/dfs_v2/filesystems/devfs/devfs.c

Large diffs are not rendered by default.

57 changes: 32 additions & 25 deletions components/dfs/dfs_v2/filesystems/elmfat/dfs_elm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@


static int dfs_elm_free_vnode(struct dfs_vnode *vnode);
static int dfs_elm_truncate(struct dfs_file *file, off_t offset);

#ifdef RT_USING_PAGECACHE
static ssize_t dfs_elm_page_read(struct dfs_file *file, struct dfs_page *page);
Expand Down Expand Up @@ -547,33 +548,14 @@ int dfs_elm_ioctl(struct dfs_file *file, int cmd, void *args)
switch (cmd)
{
case RT_FIOFTRUNCATE:
{
FIL *fd;
FSIZE_t fptr, length;
FRESULT result = FR_OK;
fd = (FIL *)(file->vnode->data);
RT_ASSERT(fd != RT_NULL);

/* save file read/write point */
fptr = fd->fptr;
length = *(off_t*)args;
if (length <= fd->obj.objsize)
{
fd->fptr = length;
result = f_truncate(fd);
}
else
{
result = f_lseek(fd, length);
}
/* restore file read/write point */
fd->fptr = fptr;
return elm_result_to_dfs(result);
}
{
off_t offset = (off_t)(size_t)(args);
return dfs_elm_truncate(file, offset);
}
case F_GETLK:
return 0;
return 0;
case F_SETLK:
return 0;
return 0;
}
return -ENOSYS;
}
Expand Down Expand Up @@ -701,6 +683,30 @@ off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece)
return elm_result_to_dfs(result);
}

static int dfs_elm_truncate(struct dfs_file *file, off_t offset)
{
FIL *fd;
FSIZE_t fptr;
FRESULT result = FR_OK;
fd = (FIL *)(file->vnode->data);
RT_ASSERT(fd != RT_NULL);

/* save file read/write point */
fptr = fd->fptr;
if (offset <= fd->obj.objsize)
{
fd->fptr = offset;
result = f_truncate(fd);
}
else
{
result = f_lseek(fd, offset);
}
/* restore file read/write point */
fd->fptr = fptr;
return elm_result_to_dfs(result);
}

int dfs_elm_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
{
DIR *dir;
Expand Down Expand Up @@ -1065,6 +1071,7 @@ static const struct dfs_file_ops dfs_elm_fops =
.write = dfs_elm_write,
.flush = dfs_elm_flush,
.lseek = dfs_elm_lseek,
.truncate = dfs_elm_truncate,
.getdents = dfs_elm_getdents,
};

Expand Down
3 changes: 3 additions & 0 deletions components/dfs/dfs_v2/include/dfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ void dfs_unlock(void);
rt_err_t dfs_file_lock(void);
void dfs_file_unlock(void);

int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src);
int dfs_fdtable_drop_fd(struct dfs_fdtable *fdtab, int fd);

#ifdef DFS_USING_POSIX
/* FD APIs */
int fdt_fd_new(struct dfs_fdtable *fdt);
Expand Down
2 changes: 2 additions & 0 deletions components/dfs/dfs_v2/include/dfs_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2);
int dfs_file_mmap(struct dfs_file *file, struct dfs_mmap2_args *mmap2);
#endif

char *dfs_nolink_path(struct dfs_mnt **mnt, char *fullpath, int mode);

/* 0x5254 is just a magic number to make these relatively unique ("RT") */
#define RT_FIOFTRUNCATE 0x52540000U
#define RT_FIOGETADDR 0x52540001U
Expand Down
93 changes: 93 additions & 0 deletions components/dfs/dfs_v2/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,99 @@ struct dfs_fdtable *dfs_fdtable_get_global(void)
return &_fdtab;
}


/**
* @brief Dup the specified fd_src from fdt_src to fdt_dst.
*
* @param fdt_dst is the fd table for destination, if empty, use global (_fdtab).
*
* @param fdt_src is the fd table for source, if empty, use global (_fdtab).
*
* @param fd_src is the fd in the designate fdt_src table.
*
* @return -1 on failed or the allocated file descriptor.
*/
int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src)
{
int newfd = -1;

dfs_file_lock();

if (fdt_src == NULL)
{
fdt_src = &_fdtab;
}

if (fdt_dst == NULL)
{
fdt_dst = &_fdtab;
}

/* check fd */
if ((fd_src < 0) || (fd_src >= fdt_src->maxfd))
{
goto _EXIT;
}
if (!fdt_src->fds[fd_src])
{
goto _EXIT;
}

/* get a new fd*/
newfd = fdt_fd_new(fdt_dst);
if (newfd >= 0)
{
fdt_dst->fds[newfd]->mode = fdt_src->fds[fd_src]->mode;
fdt_dst->fds[newfd]->flags = fdt_src->fds[fd_src]->flags;
fdt_dst->fds[newfd]->fops = fdt_src->fds[fd_src]->fops;
fdt_dst->fds[newfd]->dentry = dfs_dentry_ref(fdt_src->fds[fd_src]->dentry);
fdt_dst->fds[newfd]->vnode = fdt_src->fds[fd_src]->vnode;
fdt_dst->fds[newfd]->mmap_context = RT_NULL;
fdt_dst->fds[newfd]->data = fdt_src->fds[fd_src]->data;

/*
* dma-buf/socket fd is without dentry, so should used the vnode reference.
*/
if (!fdt_dst->fds[newfd]->dentry)
{
rt_atomic_add(&(fdt_dst->fds[newfd]->vnode->ref_count), 1);
}
}
_EXIT:
dfs_file_unlock();

return newfd;
}

/**
* @brief drop fd from the fd table.
*
* @param fdt is the fd table, if empty, use global (_fdtab).
*
* @param fd is the fd in the designate fd table.
*
* @return -1 on failed the drop file descriptor.
*/
int dfs_fdtable_drop_fd(struct dfs_fdtable *fdt, int fd)
{
int err = 0;

if (fdt == NULL)
{
fdt = &_fdtab;
}

dfs_file_lock();
err = dfs_file_close(fdt->fds[fd]);
if (!err)
{
fdt_fd_release(fdt, fd);
}
dfs_file_unlock();

return err;
}

int dfs_dup(int oldfd, int startfd)
{
int newfd = -1;
Expand Down
3 changes: 2 additions & 1 deletion components/dfs/dfs_v2/src/dfs_dentry.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry)
LOG_I("free a dentry: %p", dentry);
rt_free(dentry->pathname);
rt_free(dentry);
dentry = RT_NULL;
}
else
{
Expand Down Expand Up @@ -268,7 +269,7 @@ char* dfs_dentry_full_path(struct dfs_dentry* dentry)
path = (char *) rt_malloc(mnt_len + path_len + 3);
if (path)
{
if (dentry->pathname[0] == '/')
if (dentry->pathname[0] == '/' || dentry->mnt->fullpath[mnt_len - 1] == '/')
{
rt_snprintf(path, mnt_len + path_len + 2, "%s%s", dentry->mnt->fullpath,
dentry->pathname);
Expand Down
2 changes: 0 additions & 2 deletions components/dfs/dfs_v2/src/dfs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ int dfs_file_open(struct dfs_file *file, const char *path, int oflags, mode_t mo
dentry = dfs_dentry_create(mnt, fullpath);
if (dentry)
{
mode &= ~S_IFMT;
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->create_vnode");

if (dfs_is_mounted(mnt) == 0)
Expand Down Expand Up @@ -565,7 +564,6 @@ int dfs_file_open(struct dfs_file *file, const char *path, int oflags, mode_t mo
{
ret = -EINVAL;
}

}
}

Expand Down
34 changes: 34 additions & 0 deletions components/dfs/dfs_v2/src/dfs_file_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,38 @@ rt_err_t on_varea_merge(struct rt_varea *merge_to, struct rt_varea *merge_from)
return -RT_ERROR;
}

void *on_varea_mremap(struct rt_varea *varea, rt_size_t new_size, int flags, void *new_address)
{
void *vaddr = RT_NULL;
struct dfs_file *file = dfs_mem_obj_get_file(varea->mem_obj);

#ifndef MREMAP_MAYMOVE
#define MREMAP_MAYMOVE 1
#endif

if (file && flags == MREMAP_MAYMOVE)
{
int ret;
rt_mem_obj_t mem_obj = dfs_get_mem_obj(file);

vaddr = new_address ? new_address : varea->start;
new_size = (new_size + ARCH_PAGE_SIZE - 1);
new_size &= ~ARCH_PAGE_MASK;
ret = rt_aspace_map(varea->aspace, &vaddr, new_size, varea->attr, varea->flag, mem_obj, varea->offset);
if (ret != RT_EOK)
{
LOG_E("failed to map %lx with size %lx with errno %d", vaddr, new_size, ret);
vaddr = RT_NULL;
}
else
{
LOG_I("old: %p size: %p new: %p size: %p", varea->start, varea->size, vaddr, new_size);
}
}

return vaddr;
}

static struct rt_mem_obj _mem_obj =
{
.hint_free = hint_free,
Expand All @@ -365,6 +397,8 @@ static struct rt_mem_obj _mem_obj =
.on_varea_expand = on_varea_expand,
.on_varea_split = on_varea_split,
.on_varea_merge = on_varea_merge,

.on_varea_mremap = on_varea_mremap,
};

struct dfs_mem_obj {
Expand Down
4 changes: 4 additions & 0 deletions components/dfs/dfs_v2/src/dfs_pcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,10 @@ int dfs_aspace_unmap(struct dfs_file *file, struct rt_varea *varea)

if (map && varea == map->varea)
{
void *vaddr = dfs_aspace_vaddr(map->varea, page->fpos);

rt_varea_unmap_page(map->varea, vaddr);

if (varea->attr == MMU_MAP_U_RWCB && page->fpos < page->aspace->vnode->size)
{
dfs_page_dirty(page);
Expand Down

0 comments on commit 304ce59

Please sign in to comment.