AquilaOS documentation
How it works, and its code
Toggle main menu visibility
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
25
MALLOC_DEFINE
(M_FILE,
"file"
,
"open file"
);
26
32
struct
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
48
struct
file
*
file_ref
(
struct
file
*
file
)
49
{
50
file
->
ref
++;
51
return
file
;
52
}
53
60
int
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
82
struct
file
*
file_hold
(
struct
file
*
file
)
83
{
84
struct
thread
*
thread
=
curthread
;
85
86
if
(
thread
->
files_held
==
THREAD_FILES_HELD
)
87
panic
(
"file_hold: a system call holds too many files"
);
88
89
thread
->
files
[
thread
->
files_held
++] =
file_ref
(
file
);
90
return
file
;
91
}
92
99
void
thread_files_release
(
struct
thread
*
thread
)
100
{
101
while
(
thread
->
files_held
)
102
file_put
(
thread
->
files
[--
thread
->
files_held
]);
103
}
panic
#define panic(s)
Definition
panic.h:15
file_hold
struct file * file_hold(struct file *file)
Holds an open file for the current system call, until it returns.
Definition
file.c:82
file_new
struct file * file_new(void)
Makes a new open file, with one reference.
Definition
file.c:32
file_put
int file_put(struct file *file)
Drops a reference to an open file.
Definition
file.c:60
thread_files_release
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
file_ref
struct file * file_ref(struct file *file)
Adds a reference to an open file.
Definition
file.c:48
vfs_file_close
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
panic.h
system.h
MALLOC_DEFINE
#define MALLOC_DEFINE(type, _name, _desc)
Definition
kvmem.h:30
kmalloc
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition
kvmem.c:123
M_ZERO
#define M_ZERO
Definition
kvmem.h:26
kfree
void kfree(void *)
Frees an object that kmalloc() gave.
Definition
kvmem.c:146
proc.h
sched.h
curthread
#define curthread
Definition
sched.h:12
file
An open file (POSIX's "open file description", fs/file.c).
Definition
vfs.h:157
file::ref
int ref
Definition
vfs.h:170
thread
A thread.
Definition
thread.h:23
thread::files_held
int files_held
Definition
thread.h:81
thread::files
struct file * files[THREAD_FILES_HELD]
Definition
thread.h:80
thread.h
THREAD_FILES_HELD
#define THREAD_FILES_HELD
the open files its system call holds (file_hold()), let go of when the call returns,...
Definition
thread.h:79
vfs.h
kernel
fs
file.c
Generated by
1.18.0