Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dfs][dfsv2]standardize refcount usage in dfsv2 #9286

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/dfs/dfs_v2/include/dfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ int dfs_fdtable_drop_fd(struct dfs_fdtable *fdtab, int fd);
/* FD APIs */
int fdt_fd_new(struct dfs_fdtable *fdt);
struct dfs_file *fdt_get_file(struct dfs_fdtable* fdt, int fd);
void fdt_fd_release(struct dfs_fdtable* fdt, int fd);
rt_err_t fdt_fd_release(struct dfs_fdtable* fdt, int fd);
int fd_new(void);
int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file);
int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file, rt_bool_t replace);
struct dfs_file *fd_get(int fd);
void fd_release(int fd);
rt_err_t fd_release(int fd);

void fd_init(struct dfs_file *fd);

Expand Down
9 changes: 8 additions & 1 deletion components/dfs/dfs_v2/include/dfs_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <dfs.h>
#include <dfs_fs.h>
#include <ref.h>

#ifdef __cplusplus
extern "C"
Expand Down Expand Up @@ -88,7 +89,8 @@ struct dfs_file
uint16_t mode;

uint32_t flags;
rt_atomic_t ref_count;
struct rt_ref ref_count; /* when to release the file struct */
rt_atomic_t open_count; /* when to do real close of the file */

off_t fpos;
struct rt_mutex pos_lock;
Expand Down Expand Up @@ -146,6 +148,11 @@ struct dfs_mmap2_args
void dfs_file_init(struct dfs_file *file);
void dfs_file_deinit(struct dfs_file *file);

struct dfs_file *dfs_file_create(void);

struct dfs_file *dfs_file_get(struct dfs_file *file);
struct dfs_file *dfs_file_put(struct dfs_file *file);

int dfs_file_open(struct dfs_file *file, const char *path, int flags, mode_t mode);
int dfs_file_close(struct dfs_file *file);

Expand Down
1 change: 0 additions & 1 deletion components/dfs/dfs_v2/include/dfs_pcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ struct dfs_aspace
rt_bool_t is_active;

struct rt_mutex lock;
rt_atomic_t ref_count;

struct dfs_vnode *vnode;
const struct dfs_aspace_ops *ops;
Expand Down
109 changes: 42 additions & 67 deletions components/dfs/dfs_v2/src/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,16 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
}
else if (!fdt->fds[idx])
{
struct dfs_file *file;

file = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
struct dfs_file *file = dfs_file_create();

if (file)
{
file->magic = DFS_FD_MAGIC;
file->ref_count = 1;
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
fdt->fds[idx] = file;

fdt_fd_associate_file(fdt, idx, file, RT_FALSE);
dfs_file_put(file);
LOG_D("allocate a new fd @ %d", idx);
}
else
{
fdt->fds[idx] = RT_NULL;
idx = -1;
}
}
Expand All @@ -245,32 +239,28 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
return idx;
}

void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
rt_err_t fdt_fd_release(struct dfs_fdtable *fdt, int fd)
{
rt_err_t ret = -EBADF;

if (fd < fdt->maxfd)
{
struct dfs_file *file;

file = fdt_get_file(fdt, fd);
struct dfs_file *file = fdt_get_file(fdt, fd);

if (file && file->ref_count == 1)
if (file)
{
rt_mutex_detach(&file->pos_lock);
ret = dfs_file_close(file);

if (file->mmap_context)
if (ret == 0)
{
rt_free(file->mmap_context);
fdt->fds[fd] = RT_NULL;
dfs_file_put(file); /* put fdtable's ref to file */
dfs_file_put(file); /* put this function's ref to file */
}

rt_free(file);
}
else
{
rt_atomic_sub(&(file->ref_count), 1);
}

fdt->fds[fd] = RT_NULL;
}

return ret;
}

/**
Expand Down Expand Up @@ -300,10 +290,12 @@ struct dfs_file *fdt_get_file(struct dfs_fdtable *fdt, int fd)
return NULL;
}

dfs_file_get(f);

return f;
}

int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file, rt_bool_t replace)
{
int retfd = -1;

Expand All @@ -329,11 +321,19 @@ int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file

if (fdt->fds[fd])
{
goto exit;
if (replace == RT_FALSE)
{
goto exit;
}
else
{
dfs_file_close(fdt->fds[fd]);
dfs_file_put(fdt->fds[fd]);
}
}

/* inc ref_count */
rt_atomic_add(&(file->ref_count), 1);
dfs_file_get(file);
rt_atomic_add(&(file->open_count), 1);
fdt->fds[fd] = file;
retfd = fd;

Expand All @@ -356,12 +356,12 @@ int fd_new(void)
*
* This function will put the file descriptor.
*/
void fd_release(int fd)
rt_err_t fd_release(int fd)
{
struct dfs_fdtable *fdt;

fdt = dfs_fdtable_get();
fdt_fd_release(fdt, fd);
return fdt_fd_release(fdt, fd);
}

struct dfs_file *fd_get(int fd)
Expand Down Expand Up @@ -470,7 +470,7 @@ int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, in
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]->vnode = dfs_vnode_ref(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;

Expand Down Expand Up @@ -512,11 +512,7 @@ int dfs_fdtable_drop_fd(struct dfs_fdtable *fdt, int fd)
return -RT_ENOSYS;
}

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

dfs_file_unlock();

Expand Down Expand Up @@ -547,10 +543,7 @@ int dfs_dup(int oldfd, int startfd)
newfd = _fdt_slot_alloc(fdt, startfd);
if (newfd >= 0)
{
fdt->fds[newfd] = fdt->fds[oldfd];

/* inc ref_count */
rt_atomic_add(&(fdt->fds[newfd]->ref_count), 1);
newfd = fdt_fd_associate_file(fdt, newfd, fdt->fds[oldfd], RT_FALSE);
}
exit:
dfs_file_unlock();
Expand Down Expand Up @@ -595,10 +588,7 @@ int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
newfd = _fdt_slot_alloc(fdtab, DFS_STDIO_OFFSET);
if (newfd >= 0)
{
fdtab->fds[newfd] = fdt->fds[oldfd];

/* inc ref_count */
rt_atomic_add(&(fdtab->fds[newfd]->ref_count), 1);
newfd = fdt_fd_associate_file(fdtab, newfd, fdt->fds[oldfd], RT_FALSE);
}
exit:
dfs_file_unlock();
Expand Down Expand Up @@ -648,13 +638,11 @@ int dfs_dup_from(int oldfd, struct dfs_fdtable *fdtab)
file->flags = fdtab->fds[oldfd]->flags;
file->fops = fdtab->fds[oldfd]->fops;
file->dentry = dfs_dentry_ref(fdtab->fds[oldfd]->dentry);
file->vnode = fdtab->fds[oldfd]->vnode;
file->vnode = dfs_vnode_ref(fdtab->fds[oldfd]->vnode);
file->mmap_context = RT_NULL;
file->data = fdtab->fds[oldfd]->data;
}

dfs_file_close(fdtab->fds[oldfd]);

exit:
fdt_fd_release(fdtab, oldfd);
dfs_file_unlock();
Expand All @@ -680,7 +668,6 @@ int sys_dup(int oldfd)
rt_err_t sys_dup2(int oldfd, int newfd)
{
struct dfs_fdtable *fdt = NULL;
int ret = 0;
int retfd = -1;

if (dfs_file_lock() != RT_EOK)
Expand Down Expand Up @@ -717,20 +704,8 @@ rt_err_t sys_dup2(int oldfd, int newfd)
goto exit;
}

if (fdt->fds[newfd])
{
ret = dfs_file_close(fdt->fds[newfd]);
if (ret < 0)
{
goto exit;
}
fd_release(newfd);
}
retfd = fdt_fd_associate_file(fdt, newfd, fdt->fds[oldfd], RT_TRUE);

fdt->fds[newfd] = fdt->fds[oldfd];
/* inc ref_count */
rt_atomic_add(&(fdt->fds[newfd]->ref_count), 1);
retfd = newfd;
exit:
dfs_file_unlock();
return retfd;
Expand Down Expand Up @@ -938,7 +913,7 @@ int list_fd(void)
else if (file->vnode->type == FT_USER) rt_kprintf("%-7.7s ", "user");
else if (file->vnode->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device");
else rt_kprintf("%-8.8s ", "unknown");
rt_kprintf("%3d ", file->ref_count);
rt_kprintf("%3d ", file->open_count);
rt_kprintf("%04x ", file->magic);

if (file->dentry)
Expand Down Expand Up @@ -974,14 +949,14 @@ int dfs_fd_dump(int argc, char** argv)
char* fullpath = dfs_dentry_full_path(file->dentry);
if (fullpath)
{
printf("[%d] - %s, ref_count %zd\n", index,
fullpath, (size_t)rt_atomic_load(&(file->ref_count)));
printf("[%d] - %s, open_count %zd\n", index,
fullpath, (size_t)rt_atomic_load(&(file->open_count)));
rt_free(fullpath);
}
else
{
printf("[%d] - %s, ref_count %zd\n", index,
file->dentry->pathname, (size_t)rt_atomic_load(&(file->ref_count)));
printf("[%d] - %s, open_count %zd\n", index,
file->dentry->pathname, (size_t)rt_atomic_load(&(file->open_count)));
}
}
}
Expand Down
Loading
Loading