vfs: refcount file structs used in fdescs
This commit is contained in:
parent
744c3b266f
commit
02c0a70a30
5 changed files with 22 additions and 4 deletions
1
fs/vfs.c
1
fs/vfs.c
|
|
@ -65,6 +65,7 @@ struct file *vfs_open(const char *pathname, int *err)
|
|||
{
|
||||
/* allocate a file */
|
||||
struct file *file = slab_alloc(file_slab);
|
||||
|
||||
if (!file) {
|
||||
kprintf(LOG_ERROR "Failed to allocate file struct\n");
|
||||
return NULL;
|
||||
|
|
|
|||
|
|
@ -91,6 +91,9 @@ struct dirent {
|
|||
struct file {
|
||||
struct vnode *vnode;
|
||||
|
||||
spinlock_t ref_lock;
|
||||
uint64_t refcount;
|
||||
|
||||
ino_t ino_num; /* inode number */
|
||||
uint32_t type; /* type of the file */
|
||||
uint64_t size; /* size of the file */
|
||||
|
|
|
|||
14
sched/proc.c
14
sched/proc.c
|
|
@ -84,12 +84,15 @@ void proc_term(pid_t pid)
|
|||
{
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||
|
||||
|
||||
struct proc_block_node *cur = proc_block_queue->head;
|
||||
|
||||
struct proc_block_node *next;
|
||||
while (cur) {
|
||||
if (cur->proc->pid == pid) {
|
||||
next = cur->next;
|
||||
proc_block_remove(cur);
|
||||
cur = cur->next;
|
||||
kfree(cur);
|
||||
cur = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -113,7 +116,12 @@ void proc_term(pid_t pid)
|
|||
while (node) {
|
||||
struct rbnode *next = rbt_successor(node);
|
||||
struct file_descriptor *fdesc = (void *)node->value;
|
||||
vfs_close(fdesc->file);
|
||||
|
||||
spinlock_acquire(&fdesc->file->ref_lock);
|
||||
fdesc->file->refcount--;
|
||||
if (fdesc->file->refcount == 0)
|
||||
vfs_close(fdesc->file);
|
||||
spinlock_release(&fdesc->file->ref_lock);
|
||||
slab_free(fd_slab, fdesc);
|
||||
node = next;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,6 +126,10 @@ int sys_open(const char *pathname, int flags)
|
|||
fdesc->pos = 0;
|
||||
fdesc->flags = flags;
|
||||
|
||||
spinlock_acquire(&file->ref_lock);
|
||||
file->refcount++;
|
||||
spinlock_release(&file->ref_lock);
|
||||
|
||||
struct rbnode *node = rbt_insert(&proc->fd_map, fdno);
|
||||
node->value = (uintptr_t)fdesc;
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,9 @@ void _start()
|
|||
write(fd, "attempting to fork\n", 19);
|
||||
if (fork() == 0) {
|
||||
write(fd, "child\n", 6);
|
||||
exit(0);
|
||||
|
||||
while (1)
|
||||
;
|
||||
} else {
|
||||
write(fd, "parent\n", 7);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue