add vnode/dentry ref
This commit is contained in:
parent
d33afe7f1b
commit
04b4889b31
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -638,7 +638,7 @@ 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;
|
||||
}
|
||||
|
|
|
@ -86,7 +86,11 @@ static int _try_readlink(const char *path, struct dfs_mnt *mnt, char *link)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dentry)
|
||||
{
|
||||
dfs_dentry_unref(dentry);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -256,7 +260,7 @@ static void _dfs_file_release(struct rt_ref *ref)
|
|||
|
||||
if (file->vnode)
|
||||
{
|
||||
dfs_vnode_unref(file->vnode);
|
||||
//dfs_vnode_unref(file->vnode);
|
||||
}
|
||||
|
||||
if (file->mmap_context)
|
||||
|
@ -720,7 +724,7 @@ int dfs_file_close(struct dfs_file *file)
|
|||
{
|
||||
DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->close(file)");
|
||||
#ifdef RT_USING_PAGECACHE
|
||||
if (file->vnode->aspace)
|
||||
if (file->vnode && file->vnode->aspace)
|
||||
{
|
||||
ret = dfs_aspace_flush(file->vnode->aspace);
|
||||
}
|
||||
|
@ -1269,6 +1273,8 @@ int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
|
|||
ret = -EBADF;
|
||||
}
|
||||
|
||||
dfs_file_put(file);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ static struct dfs_page *dfs_page_lookup(struct dfs_file *file, off_t pos);
|
|||
static void dfs_page_ref(struct dfs_page *page);
|
||||
static int dfs_page_inactive(struct dfs_page *page);
|
||||
static int dfs_page_remove(struct dfs_page *page);
|
||||
static void dfs_page_release(struct dfs_page *page);
|
||||
static void dfs_page_unref(struct dfs_page *page);
|
||||
static int dfs_page_dirty(struct dfs_page *page);
|
||||
|
||||
static int dfs_aspace_release(struct dfs_aspace *aspace);
|
||||
|
@ -86,8 +86,8 @@ static int dfs_aspace_gc(struct dfs_aspace *aspace, int count)
|
|||
{
|
||||
int cnt = count;
|
||||
|
||||
if (aspace)
|
||||
{
|
||||
RT_ASSERT(aspace);
|
||||
|
||||
dfs_aspace_lock(aspace);
|
||||
|
||||
if (aspace->pages_count > 0)
|
||||
|
@ -118,7 +118,6 @@ static int dfs_aspace_gc(struct dfs_aspace *aspace, int count)
|
|||
}
|
||||
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
|
||||
return count - cnt;
|
||||
}
|
||||
|
@ -284,7 +283,7 @@ static void dfs_pcache_thread(void *parameter)
|
|||
}
|
||||
}
|
||||
}
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
else
|
||||
|
@ -381,12 +380,10 @@ static struct dfs_aspace *dfs_aspace_hash_lookup(struct dfs_dentry *dentry, cons
|
|||
dfs_pcache_lock();
|
||||
rt_list_for_each_entry(aspace, &__pcache.head[dfs_aspace_hash(dentry->mnt, dentry->pathname)], hash_node)
|
||||
{
|
||||
|
||||
if (aspace->mnt == dentry->mnt
|
||||
&& aspace->ops == ops
|
||||
&& !strcmp(aspace->pathname, dentry->pathname))
|
||||
{
|
||||
rt_atomic_add(&aspace->ref_count, 1);
|
||||
dfs_pcache_unlock();
|
||||
return aspace;
|
||||
}
|
||||
|
@ -403,7 +400,6 @@ static void dfs_aspace_insert(struct dfs_aspace *aspace)
|
|||
val = dfs_aspace_hash(aspace->mnt, aspace->pathname);
|
||||
|
||||
dfs_pcache_lock();
|
||||
rt_atomic_add(&aspace->ref_count, 1);
|
||||
rt_list_insert_after(&__pcache.head[val], &aspace->hash_node);
|
||||
rt_list_insert_before(&__pcache.list_inactive, &aspace->cache_node);
|
||||
dfs_pcache_unlock();
|
||||
|
@ -463,7 +459,6 @@ static struct dfs_aspace *_dfs_aspace_create(struct dfs_dentry *dentry,
|
|||
aspace->avl_page = 0;
|
||||
|
||||
rt_mutex_init(&aspace->lock, rt_thread_self()->parent.name, RT_IPC_FLAG_PRIO);
|
||||
rt_atomic_store(&aspace->ref_count, 1);
|
||||
|
||||
aspace->pages_count = 0;
|
||||
aspace->vnode = vnode;
|
||||
|
@ -510,36 +505,34 @@ struct dfs_aspace *dfs_aspace_create(struct dfs_dentry *dentry,
|
|||
|
||||
int dfs_aspace_destroy(struct dfs_aspace *aspace)
|
||||
{
|
||||
int ret = -EINVAL;
|
||||
int ret = 0;
|
||||
|
||||
RT_ASSERT(aspace);
|
||||
|
||||
if (aspace)
|
||||
{
|
||||
dfs_pcache_lock();
|
||||
dfs_aspace_lock(aspace);
|
||||
rt_atomic_sub(&aspace->ref_count, 1);
|
||||
RT_ASSERT(rt_atomic_load(&aspace->ref_count) > 0);
|
||||
dfs_aspace_inactive(aspace);
|
||||
aspace->vnode = RT_NULL;
|
||||
dfs_aspace_flush(aspace);
|
||||
dfs_aspace_gc(aspace, aspace->pages_count);
|
||||
RT_ASSERT(aspace->pages_count == 0);
|
||||
if (dfs_aspace_release(aspace) != 0)
|
||||
{
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
dfs_pcache_unlock();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int dfs_aspace_release(struct dfs_aspace *aspace)
|
||||
{
|
||||
int ret = -1;
|
||||
int ret = 0;
|
||||
|
||||
RT_ASSERT(aspace);
|
||||
|
||||
if (aspace)
|
||||
{
|
||||
dfs_pcache_lock();
|
||||
dfs_aspace_lock(aspace);
|
||||
|
||||
if (rt_atomic_load(&aspace->ref_count) == 1 && aspace->pages_count == 0)
|
||||
if (aspace->pages_count == 0)
|
||||
{
|
||||
dfs_aspace_remove(aspace);
|
||||
if (aspace->fullpath)
|
||||
|
@ -552,22 +545,20 @@ static int dfs_aspace_release(struct dfs_aspace *aspace)
|
|||
}
|
||||
rt_mutex_detach(&aspace->lock);
|
||||
rt_free(aspace);
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
dfs_pcache_unlock();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int _dfs_aspace_dump(struct dfs_aspace *aspace, int is_dirty)
|
||||
{
|
||||
if (aspace)
|
||||
{
|
||||
RT_ASSERT(aspace);
|
||||
|
||||
rt_list_t *next;
|
||||
struct dfs_page *page;
|
||||
|
||||
|
@ -595,7 +586,7 @@ static int _dfs_aspace_dump(struct dfs_aspace *aspace, int is_dirty)
|
|||
rt_kprintf(" pages >> empty\n");
|
||||
}
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -724,7 +715,7 @@ static void dfs_page_ref(struct dfs_page *page)
|
|||
rt_atomic_add(&(page->ref_count), 1);
|
||||
}
|
||||
|
||||
static void dfs_page_release(struct dfs_page *page)
|
||||
static void dfs_page_unref(struct dfs_page *page)
|
||||
{
|
||||
struct dfs_aspace *aspace = page->aspace;
|
||||
|
||||
|
@ -873,7 +864,7 @@ static int dfs_page_remove(struct dfs_page *page)
|
|||
|
||||
rt_atomic_sub(&(__pcache.pages_count), 1);
|
||||
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
|
@ -1032,7 +1023,7 @@ static struct dfs_page *dfs_page_lookup(struct dfs_file *file, off_t pos)
|
|||
}
|
||||
else
|
||||
{
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -1044,7 +1035,7 @@ static struct dfs_page *dfs_page_lookup(struct dfs_file *file, off_t pos)
|
|||
page = dfs_page_search(aspace, fpos);
|
||||
if (page)
|
||||
{
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
}
|
||||
count --;
|
||||
|
||||
|
@ -1116,11 +1107,11 @@ int dfs_aspace_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
|
|||
}
|
||||
else
|
||||
{
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
dfs_aspace_unlock(aspace);
|
||||
break;
|
||||
}
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
else
|
||||
|
@ -1189,7 +1180,7 @@ int dfs_aspace_write(struct dfs_file *file, const void *buf, size_t count, off_t
|
|||
dfs_page_dirty(page);
|
||||
}
|
||||
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
else
|
||||
|
@ -1204,8 +1195,8 @@ int dfs_aspace_write(struct dfs_file *file, const void *buf, size_t count, off_t
|
|||
|
||||
int dfs_aspace_flush(struct dfs_aspace *aspace)
|
||||
{
|
||||
if (aspace)
|
||||
{
|
||||
RT_ASSERT(aspace);
|
||||
|
||||
rt_list_t *next;
|
||||
struct dfs_page *page;
|
||||
|
||||
|
@ -1239,14 +1230,14 @@ int dfs_aspace_flush(struct dfs_aspace *aspace)
|
|||
}
|
||||
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dfs_aspace_clean(struct dfs_aspace *aspace)
|
||||
{
|
||||
if (aspace)
|
||||
{
|
||||
RT_ASSERT(aspace);
|
||||
|
||||
dfs_aspace_lock(aspace);
|
||||
|
||||
if (aspace->pages_count > 0)
|
||||
|
@ -1268,7 +1259,6 @@ int dfs_aspace_clean(struct dfs_aspace *aspace)
|
|||
}
|
||||
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1312,18 +1302,18 @@ void *dfs_aspace_mmap(struct dfs_file *file, struct rt_varea *varea, void *vaddr
|
|||
map->vaddr = vaddr;
|
||||
dfs_aspace_lock(aspace);
|
||||
rt_list_insert_after(&page->mmap_head, &map->mmap_node);
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
dfs_aspace_unlock(aspace);
|
||||
}
|
||||
else
|
||||
{
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
rt_free(map);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1437,7 +1427,7 @@ int dfs_aspace_page_unmap(struct dfs_file *file, struct rt_varea *varea, void *v
|
|||
}
|
||||
}
|
||||
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
}
|
||||
|
||||
dfs_aspace_unlock(aspace);
|
||||
|
@ -1459,7 +1449,7 @@ int dfs_aspace_page_dirty(struct dfs_file *file, struct rt_varea *varea, void *v
|
|||
if (page)
|
||||
{
|
||||
dfs_page_dirty(page);
|
||||
dfs_page_release(page);
|
||||
dfs_page_unref(page);
|
||||
}
|
||||
|
||||
dfs_aspace_unlock(aspace);
|
||||
|
|
|
@ -53,11 +53,10 @@ int dfs_vnode_destroy(struct dfs_vnode* vnode)
|
|||
rt_err_t ret = RT_EOK;
|
||||
|
||||
RT_ASSERT(vnode);
|
||||
RT_ASSERT(rt_atomic_dec_and_test(&vnode->ref_count));
|
||||
|
||||
ret = dfs_file_lock();
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
if (rt_atomic_load(&(vnode->ref_count)) == 1)
|
||||
{
|
||||
LOG_I("free a vnode: %p", vnode);
|
||||
#ifdef RT_USING_PAGECACHE
|
||||
|
@ -80,11 +79,6 @@ int dfs_vnode_destroy(struct dfs_vnode* vnode)
|
|||
|
||||
rt_free(vnode);
|
||||
}
|
||||
else
|
||||
{
|
||||
dfs_file_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -106,19 +100,20 @@ void dfs_vnode_unref(struct dfs_vnode *vnode)
|
|||
|
||||
RT_ASSERT(vnode);
|
||||
|
||||
DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
|
||||
|
||||
if (rt_atomic_dec_and_test(&vnode->ref_count))
|
||||
{
|
||||
ret = dfs_file_lock();
|
||||
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
rt_atomic_sub(&(vnode->ref_count), 1);
|
||||
DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
|
||||
#ifdef RT_USING_PAGECACHE
|
||||
if (vnode->aspace)
|
||||
{
|
||||
dfs_aspace_destroy(vnode->aspace);
|
||||
}
|
||||
#endif
|
||||
if (rt_atomic_load(&(vnode->ref_count)) == 0)
|
||||
{
|
||||
LOG_I("free a vnode: %p", vnode);
|
||||
DLOG(msg, "vnode", "vnode", DLOG_MSG, "free vnode, ref_count=0");
|
||||
|
||||
|
@ -132,11 +127,6 @@ void dfs_vnode_unref(struct dfs_vnode *vnode)
|
|||
|
||||
rt_free(vnode);
|
||||
}
|
||||
else
|
||||
{
|
||||
dfs_file_unlock();
|
||||
DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
|
|
|
@ -93,7 +93,7 @@ static int dfs_net_close(struct dfs_file* file)
|
|||
int socket;
|
||||
int ret = 0;
|
||||
|
||||
if (file->vnode->ref_count == 1)
|
||||
if (file->vnode && file->vnode->ref_count == 1)
|
||||
{
|
||||
socket = (int)(size_t)file->vnode->data;
|
||||
ret = sal_closesocket(socket);
|
||||
|
|
|
@ -238,7 +238,13 @@ int socket(int domain, int type, int protocol)
|
|||
d->fops = dfs_net_get_fops();
|
||||
#endif
|
||||
|
||||
/* create socket and then put it to the dfs_file */
|
||||
socket = sal_socket(domain, type, protocol);
|
||||
|
||||
if (socket >= 0)
|
||||
{
|
||||
d->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
|
||||
|
||||
if (!d->vnode)
|
||||
{
|
||||
#ifdef RT_USING_DFS_V2
|
||||
|
@ -250,10 +256,6 @@ int socket(int domain, int type, int protocol)
|
|||
return -1;
|
||||
}
|
||||
|
||||
/* create socket and then put it to the dfs_file */
|
||||
socket = sal_socket(domain, type, protocol);
|
||||
if (socket >= 0)
|
||||
{
|
||||
dfs_vnode_init(d->vnode, FT_SOCKET, dfs_net_get_fops());
|
||||
d->flags = O_RDWR; /* set flags as read and write */
|
||||
|
||||
|
|
Loading…
Reference in New Issue