PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
vnode.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/path.h>
4#include <kernel/io/irp.h>
5#include <kernel/sync/mutex.h>
6#include <kernel/sync/rcu.h>
7#include <kernel/utils/map.h>
8#include <kernel/utils/ref.h>
9
10#include <stdatomic.h>
11#include <stdint.h>
12#include <sys/fs.h>
13#include <sys/proc.h>
14#include <time.h>
15
16typedef struct vnode vnode_t;
17typedef struct vnode_ops vnode_ops_t;
18typedef struct superblock superblock_t;
19typedef struct file_ops file_ops_t;
20typedef struct dentry dentry_t;
21
22/**
23 * @brief Virtual node.
24 * @defgroup kernel_fs_vnode Vnode
25 * @ingroup kernel_fs
26 *
27 * A vnode represents the actual data and metadata of a file. It is referenced by dentries, which represent the name or
28 * "location" of the file but a vnode can appear in multiple dentries due to hardlinks or mounts.
29 *
30 * @note Despite the name vnodes are in no way "nodes" in any kind of tree structure, that would be the dentries.
31 *
32 * ## Synchronization
33 *
34 * Vnodes have an additional purpose within the Virtual File System (VFS) as they act as the primary means of
35 * synchronization. All dentries synchronize upon their vnodes mutex, open files synchronize upon the mutex of the
36 * underlying vnode and operations like create, remove, etc synchronize upon the vnode mutex of the parent directory.
37 *
38 * @{
39 */
40
41/**
42 * @brief vnode structure.
43 * @struct vnode_t
44 *
45 * vnodes are owned by the filesystem, not the VFS.
46 */
47typedef struct vnode
48{
51 _Atomic(uint64_t) dentryCount; ///< The number of dentries pointing to this vnode.
52 void* data; ///< Filesystem defined data.
53 uint64_t size; ///< Used for convenience by certain filesystems, does not represent the file size.
60} vnode_t;
61
62/**
63 * @brief vnode operations structure.
64 * @struct vnode_ops_t
65 *
66 * Note that the vnodes mutex will be acquired by the vfs.
67 */
68typedef struct vnode_ops
69{
70 /**
71 * @brief Look up a dentry in a directory vnode.
72 *
73 * Should set the target dentry to be positive (give it an vnode), if the entry does not exist the operation
74 * should still return success but leave the dentry negative.
75 *
76 * @param dir The directory vnode to look in.
77 * @param target The dentry to look up.
78 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
79 */
80 uint64_t (*lookup)(vnode_t* dir, dentry_t* target);
81 /**
82 * @brief Handles both directories and files depending on mode.
83 *
84 * Takes in a negative dentry and creates the corresponding vnode to make the dentry positive.
85 *
86 * @param dir The directory vnode to create the entry in.
87 * @param target The negative dentry to create.
88 * @param mode The mode to create the entry with.
89 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
90 */
91 uint64_t (*create)(vnode_t* dir, dentry_t* target, mode_t mode);
92 /**
93 * @brief Set the vnode size to zero.
94 *
95 * @param target The vnode to truncate.
96 */
97 void (*truncate)(vnode_t* target);
98 /**
99 * @brief Make the same file vnode appear twice in the filesystem.
100 *
101 * @param dir The directory vnode to create the link in.
102 * @param old The existing dentry containing the vnode to link to.
103 * @param new The negative dentry to store the same vnode as old.
104 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
105 */
107 /**
108 * @brief Retrieve the path of the symbolic link.
109 *
110 * @param vnode The symbolic link vnode.
111 * @param buffer The buffer to store the path in.
112 * @param size The size of the buffer.
113 * @return On success, the number of bytes read. On failure, returns `ERR` and `errno` is set.
114 */
115 uint64_t (*readlink)(vnode_t* vnode, char* buffer, uint64_t size);
116 /**
117 * @brief Create a symbolic link.
118 *
119 * @param dir The directory vnode to create the symbolic link in.
120 * @param target The negative dentry to create.
121 * @param dest The path to which the symbolic link will point.
122 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
123 */
124 uint64_t (*symlink)(vnode_t* dir, dentry_t* target, const char* dest);
125 /**
126 * @brief Remove a file or directory.
127 *
128 * @param dir The directory vnode containing the target.
129 * @param target The dentry to remove.
130 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
131 */
133 /**
134 * @brief Cleanup function called when the vnode is being freed.
135 *
136 * @param vnode The vnode being freed.
137 */
138 void (*cleanup)(vnode_t* vnode);
140
141/**
142 * @brief Create a new vnode.
143 *
144 * Does not associate the vnode with a dentry, that is done when a dentry is made positive with
145 * `dentry_make_positive()`.
146 *
147 * There is no `vnode_free()` instead use `UNREF()`.
148 *
149 * @param superblock The superblock the vnode belongs to.
150 * @param type The vnode type.
151 * @param ops The vnode operations.
152 * @param fileOps The file operations for files opened on this vnode.
153 * @return On success, the new vnode. On failure, returns `NULL` and `errno` is set.
154 */
155vnode_t* vnode_new(superblock_t* superblock, vtype_t type, const vnode_ops_t* ops, const file_ops_t* fileOps);
156
157/**
158 * @brief Truncate the vnode.
159 *
160 * The filesystem should implement the actual truncation in the vnode ops truncate function, this is just a helper to
161 * call it.
162 *
163 * @param vnode The vnode to truncate.
164 */
165void vnode_truncate(vnode_t* vnode);
166
167/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
static dentry_t * dir
Definition fb.c:16
static fb_ops_t ops
Definition gop.c:80
mode_t
Path flags and permissions.
Definition path.h:79
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
vtype_t
Vnode type enum.
Definition fs.h:342
size_t readlink(const char *path, char *buffer, uint64_t count)
System call for reading the target of a symbolic link.
Definition readlink.c:8
uint64_t symlink(const char *target, const char *linkpath)
System call for creating a symbolic link.
Definition symlink.c:8
uint64_t link(const char *oldPath, const char *newPath)
System call for creating a hardlink.
Definition link.c:8
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int remove(const char *filename)
Definition remove.c:6
Directory entry structure.
Definition dentry.h:155
File operations structure.
Definition file.h:54
IRP vtable structure.
Definition irp.h:234
Mutex structure.
Definition mutex.h:46
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
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
_Atomic(uint64_t) dentryCount
The number of dentries pointing to this vnode.
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