vfs: add vfs_vnode_dec_ref
This commit is contained in:
parent
05486bbcc8
commit
74c8c21425
1 changed files with 25 additions and 22 deletions
47
fs/vfs.c
47
fs/vfs.c
|
|
@ -15,6 +15,9 @@ static slab_t *vnode_slab;
|
||||||
|
|
||||||
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
||||||
|
|
||||||
|
static void vfs_vnode_dec_ref(struct vnode *vnode);
|
||||||
|
static void vfs_vnode_dealloc(struct vnode *vnode);
|
||||||
|
|
||||||
static void vfs_vnode_dealloc(struct vnode *vnode)
|
static void vfs_vnode_dealloc(struct vnode *vnode)
|
||||||
{
|
{
|
||||||
if (!vnode)
|
if (!vnode)
|
||||||
|
|
@ -25,18 +28,15 @@ static void vfs_vnode_dealloc(struct vnode *vnode)
|
||||||
|
|
||||||
if (vnode->parent) {
|
if (vnode->parent) {
|
||||||
spinlock_acquire(&vnode->parent->lock);
|
spinlock_acquire(&vnode->parent->lock);
|
||||||
vnode->parent->refcount--;
|
|
||||||
spinlock_release(&vnode->parent->lock);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < vnode->parent->num_dirents; i++) {
|
for (size_t i = 0; i < vnode->parent->num_dirents; i++) {
|
||||||
if (vnode->parent->dirents[i].vnode == vnode) {
|
if (vnode->parent->dirents[i].vnode == vnode) {
|
||||||
vnode->parent->dirents[i].vnode = NULL;
|
vnode->parent->dirents[i].vnode = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
spinlock_release(&vnode->parent->lock);
|
||||||
|
|
||||||
if (vnode->parent->refcount == 0)
|
vfs_vnode_dec_ref(vnode->parent);
|
||||||
vfs_vnode_dealloc(vnode->parent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ATTEMPT_FREE(vnode->dirents);
|
ATTEMPT_FREE(vnode->dirents);
|
||||||
|
|
@ -44,6 +44,23 @@ static void vfs_vnode_dealloc(struct vnode *vnode)
|
||||||
slab_free(vnode_slab, vnode);
|
slab_free(vnode_slab, vnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void vfs_vnode_dec_ref(struct vnode *vnode)
|
||||||
|
{
|
||||||
|
if (!vnode)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (vnode->no_free)
|
||||||
|
return;
|
||||||
|
|
||||||
|
spinlock_acquire(&vnode->lock);
|
||||||
|
vnode->refcount--;
|
||||||
|
spinlock_release(&vnode->lock);
|
||||||
|
|
||||||
|
/* will recursively free all vnodes */
|
||||||
|
if (vnode->refcount == 0)
|
||||||
|
vfs_vnode_dealloc(vnode);
|
||||||
|
}
|
||||||
|
|
||||||
static void vfs_dealloc(struct fs *fs)
|
static void vfs_dealloc(struct fs *fs)
|
||||||
{
|
{
|
||||||
ATTEMPT_FREE(fs->ops);
|
ATTEMPT_FREE(fs->ops);
|
||||||
|
|
@ -128,14 +145,7 @@ struct file *vfs_open(const char *pathname, int *err)
|
||||||
*err = -ENOENT;
|
*err = -ENOENT;
|
||||||
|
|
||||||
/* clean up */
|
/* clean up */
|
||||||
spinlock_acquire(&cur_vnode->lock);
|
vfs_vnode_dec_ref(cur_vnode);
|
||||||
cur_vnode->refcount--;
|
|
||||||
spinlock_release(&cur_vnode->lock);
|
|
||||||
|
|
||||||
/* will recursively free all vnodes */
|
|
||||||
if (cur_vnode->refcount == 0)
|
|
||||||
vfs_vnode_dealloc(cur_vnode);
|
|
||||||
|
|
||||||
goto out_2;
|
goto out_2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,14 +170,7 @@ int vfs_close(struct file *file)
|
||||||
if (!file)
|
if (!file)
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
|
|
||||||
if (!file->vnode->no_free) {
|
vfs_vnode_dec_ref(file->vnode);
|
||||||
spinlock_acquire(&file->vnode->lock);
|
|
||||||
file->vnode->refcount--;
|
|
||||||
spinlock_release(&file->vnode->lock);
|
|
||||||
|
|
||||||
if (file->vnode->refcount == 0)
|
|
||||||
vfs_vnode_dealloc(file->vnode);
|
|
||||||
}
|
|
||||||
|
|
||||||
slab_free(file_slab, file);
|
slab_free(file_slab, file);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -221,7 +224,7 @@ DIR *opendir(const char *name)
|
||||||
int err;
|
int err;
|
||||||
struct file *file = vfs_open(name, &err);
|
struct file *file = vfs_open(name, &err);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
kprintf(LOG_ERROR "Failed to open directory %s: %s\n", name, strerror(err));
|
kprintf(LOG_ERROR "Failed to open directory %s: %s\n", name, strerror(-err));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue