AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
file.c
Go to the documentation of this file.
1
17
18#include <core/system.h>
19#include <core/panic.h>
20#include <fs/vfs.h>
21#include <sys/proc.h>
22#include <sys/thread.h>
23#include <sys/sched.h>
24
25MALLOC_DEFINE(M_FILE, "file", "open file");
26
32struct file *file_new(void)
33{
34 struct file *file = kmalloc(sizeof(struct file), &M_FILE, M_ZERO);
35
36 if (file)
37 file->ref = 1;
38
39 return file;
40}
41
48struct file *file_ref(struct file *file)
49{
50 file->ref++;
51 return file;
52}
53
60int file_put(struct file *file)
61{
62 if (!file->ref)
63 panic("file_put: a file no one refers to");
64
65 if (--file->ref)
66 return 0;
67
68 /* a file that was set up only halfway (its open failed) has
69 * nothing to close */
70 int err = file->vnode ? vfs_file_close(file) : 0;
71
72 kfree(file);
73 return err;
74}
75
82struct file *file_hold(struct file *file)
83{
84 struct thread *thread = curthread;
85
87 panic("file_hold: a system call holds too many files");
88
90 return file;
91}
92
100{
101 while (thread->files_held)
103}
#define panic(s)
Definition panic.h:15
struct file * file_hold(struct file *file)
Holds an open file for the current system call, until it returns.
Definition file.c:82
struct file * file_new(void)
Makes a new open file, with one reference.
Definition file.c:32
int file_put(struct file *file)
Drops a reference to an open file.
Definition file.c:60
void thread_files_release(struct thread *thread)
Lets go of the open files a thread's system call held, when the call returns or the thread is killed.
Definition file.c:99
struct file * file_ref(struct file *file)
Adds a reference to an open file.
Definition file.c:48
ssize_t vfs_file_close(struct file *file)
Closes an open file, when its last reference goes: its lock is released, then its socket,...
Definition fops.c:183
#define MALLOC_DEFINE(type, _name, _desc)
Definition kvmem.h:30
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
#define M_ZERO
Definition kvmem.h:26
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
#define curthread
Definition sched.h:12
An open file (POSIX's "open file description", fs/file.c).
Definition vfs.h:157
int ref
Definition vfs.h:170
A thread.
Definition thread.h:23
int files_held
Definition thread.h:81
struct file * files[THREAD_FILES_HELD]
Definition thread.h:80
#define THREAD_FILES_HELD
the open files its system call holds (file_hold()), let go of when the call returns,...
Definition thread.h:79