vfs: refcount file structs used in fdescs

This commit is contained in:
Michael Shaw 2024-08-21 15:44:50 -04:00
commit 02c0a70a30
5 changed files with 22 additions and 4 deletions

View file

@ -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;

View file

@ -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 */

View file

@ -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;
}

View file

@ -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;

View file

@ -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);
}