PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
vnode.c
Go to the documentation of this file.
1#include <kernel/fs/vnode.h>
2
3#include <kernel/fs/vfs.h>
4#include <kernel/mem/cache.h>
8
9#include <stdlib.h>
10
11static void vnode_free(vnode_t* vnode)
12{
13 if (vnode == NULL)
14 {
15 return;
16 }
17
18 if (vnode->ops != NULL && vnode->ops->cleanup != NULL)
19 {
20 vnode->ops->cleanup(vnode);
21 }
22 vnode->data = NULL;
23
24 if (vnode->superblock != NULL)
25 {
26 UNREF(vnode->superblock);
27 vnode->superblock = NULL;
28 }
29
30 rcu_call(&vnode->rcu, rcu_call_cache_free, vnode);
31}
32
33static void vnode_ctor(void* ptr)
34{
35 vnode_t* vnode = (vnode_t*)ptr;
36
37 vnode->ref = (ref_t){0};
38 vnode->type = 0;
39 atomic_init(&vnode->dentryCount, 0);
40 vnode->data = NULL;
41 vnode->size = 0;
42 vnode->superblock = NULL;
43 vnode->ops = NULL;
44 vnode->fileOps = NULL;
45 vnode->rcu = (rcu_entry_t){0};
46 mutex_init(&vnode->mutex);
47}
48
50
52{
53 if (superblock == NULL)
54 {
55 errno = EINVAL;
56 return NULL;
57 }
58
59 vnode_t* vnode = cache_alloc(&cache);
60 if (vnode == NULL)
61 {
62 errno = ENOMEM;
63 return NULL;
64 }
65
66 ref_init(&vnode->ref, vnode_free);
67 vnode->type = type;
68 vnode->superblock = REF(superblock);
69 vnode->ops = ops;
70 vnode->fileOps = fileOps;
71 vnode->vtable = NULL;
72 return vnode;
73}
74
76{
77 if (vnode == NULL)
78 {
79 return;
80 }
81
82 if (vnode->ops != NULL && vnode->ops->truncate != NULL)
83 {
84 MUTEX_SCOPE(&vnode->mutex);
86 vnode->ops->truncate(vnode);
87 }
88}
#define assert(expression)
Definition assert.h:29
static fb_ops_t ops
Definition gop.c:80
vnode_t * vnode_new(superblock_t *superblock, vtype_t type, const vnode_ops_t *ops, const file_ops_t *fileOps)
Create a new vnode.
Definition vnode.c:51
void vnode_truncate(vnode_t *vnode)
Truncate the vnode.
Definition vnode.c:75
#define CACHE_LINE
Cache line size in bytes.
Definition cache.h:75
void * cache_alloc(cache_t *cache)
Allocate an object from the cache.
Definition cache.c:109
#define CACHE_CREATE(_cache, _name, _size, _alignment, _ctor, _dtor)
Macro to create a cache initializer.
Definition cache.h:148
void mutex_init(mutex_t *mtx)
Initializes a mutex.
Definition mutex.c:14
#define MUTEX_SCOPE(mutex)
Acquires a mutex for the reminder of the current scope.
Definition mutex.h:23
void rcu_call_cache_free(void *arg)
Helper callback to free a cache object.
Definition rcu.c:193
void rcu_call(rcu_entry_t *entry, rcu_callback_t func, void *arg)
Add a callback to be executed after a grace period.
Definition rcu.c:72
#define REF(ptr)
Increment reference count.
Definition ref.h:82
static void ref_init(ref_t *ref, void *callback)
Initialize a reference counter.
Definition ref.h:130
#define UNREF(ptr)
Decrement reference count.
Definition ref.h:109
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOMEM
Out of memory.
Definition errno.h:92
#define errno
Error number variable.
Definition errno.h:27
vtype_t
Vnode type enum.
Definition fs.h:342
#define NULL
Pointer error value.
Definition NULL.h:25
#define RFLAGS_INTERRUPT_ENABLE
Definition regs.h:34
static uint64_t rflags_read(void)
Definition regs.h:80
#define atomic_init(obj, value)
Definition stdatomic.h:75
Cache structure.
Definition cache.h:123
File operations structure.
Definition file.h:54
Intrusive RCU head structure.
Definition rcu.h:65
Reference counting structure.
Definition ref.h:52
Superblock structure.
Definition superblock.h:33
vnode operations structure.
Definition vnode.h:69
void(* cleanup)(vnode_t *vnode)
Cleanup function called when the vnode is being freed.
Definition vnode.h:138
void(* truncate)(vnode_t *target)
Set the vnode size to zero.
Definition vnode.h:97
vnode structure.
Definition vnode.h:48
superblock_t * superblock
Definition vnode.h:54
const file_ops_t * fileOps
Definition vnode.h:56
mutex_t mutex
Definition vnode.h:59
const irp_vtable_t * vtable
Definition vnode.h:57
rcu_entry_t rcu
Definition vnode.h:58
uint64_t size
Used for convenience by certain filesystems, does not represent the file size.
Definition vnode.h:53
vtype_t type
Definition vnode.h:50
ref_t ref
Definition vnode.h:49
void * data
Filesystem defined data.
Definition vnode.h:52
const vnode_ops_t * ops
Definition vnode.h:55
static file_ops_t fileOps
Definition tmpfs.c:86
static void vnode_ctor(void *ptr)
Definition vnode.c:33
static cache_t cache
Definition vnode.c:49
static void vnode_free(vnode_t *vnode)
Definition vnode.c:11