PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
vfs.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/dentry.h>
4#include <kernel/fs/devfs.h>
5#include <kernel/fs/file.h>
6#include <kernel/fs/mount.h>
7#include <kernel/fs/path.h>
9#include <kernel/fs/vnode.h>
10#include <kernel/proc/process.h>
11#include <kernel/sync/rwlock.h>
12#include <kernel/utils/map.h>
13
14#include <sys/fs.h>
15#include <sys/list.h>
16#include <sys/math.h>
17#include <sys/proc.h>
18
19/**
20 * @brief Virtual File System.
21 * @defgroup kernel_vfs Virtual File System
22 * @ingroup kernel_fs
23 *
24 * The Virtual File System (VFS) provides a single unified interface for any and all filesystems, including virtual
25 * filesystems used to expose kernel resources to user space.
26 *
27 * @todo Most of this is going to be removed when the new IRP system is fully implemented, but for now its usefull to
28 * keep it around during the refactor.
29 *
30 * @{
31 */
32
33/**
34 * @brief Open a file.
35 *
36 * @param pathname The pathname of the file to open.
37 * @param process The process opening the file.
38 * @return On success, the opened file. On failure, returns `NULL` and `errno` is set.
39 */
40file_t* vfs_open(const pathname_t* pathname, process_t* process);
41
42/**
43 * @brief Open one file, returning two file handles.
44 *
45 * Used primarily to implement pipes.
46 *
47 * @param pathname The pathname of the file to open.
48 * @param files The output array of two file pointers.
49 * @param process The process opening the file.
50 * @return On success, `0`. On failure, `ERR` and `errno` is set.
51 */
52uint64_t vfs_open2(const pathname_t* pathname, file_t* files[2], process_t* process);
53
54/**
55 * @brief Open a file relative to another path.
56 *
57 * @param from The path to open the file relative to, or `NULL` to use the process's current working directory.
58 * @param pathname The pathname of the file to open.
59 * @param process The process opening the file.
60 * @return On success, the opened file. On failure, returns `NULL` and `errno` is set.
61 */
62file_t* vfs_openat(const path_t* from, const pathname_t* pathname, process_t* process);
63
64/**
65 * @brief Read from a file.
66 *
67 * Follows POSIX semantics.
68 *
69 * @param file The file to read from.
70 * @param buffer The buffer to read into.
71 * @param count The number of bytes to read.
72 * @return On success, the number of bytes read. On failure, `ERR` and `errno` is set.
73 */
74size_t vfs_read(file_t* file, void* buffer, size_t count);
75
76/**
77 * @brief Write to a file.
78 *
79 * Follows POSIX semantics.
80 *
81 * @param file The file to write to.
82 * @param buffer The buffer to write from.
83 * @param count The number of bytes to write.
84 * @return On success, the number of bytes written. On failure, `ERR` and `errno` is set.
85 */
86size_t vfs_write(file_t* file, const void* buffer, size_t count);
87
88/**
89 * @brief Seek in a file.
90 *
91 * Follows POSIX semantics.
92 *
93 * @param file The file to seek in.
94 * @param offset The offset to seek to.
95 * @param origin The origin to seek from.
96 * @return On success, the new file position. On failure, `ERR` and `errno` is set.
97 */
98size_t vfs_seek(file_t* file, ssize_t offset, seek_origin_t origin);
99
100/**
101 * @brief Perform an ioctl operation on a file.
102 *
103 * @param file The file to perform the ioctl on.
104 * @param request The ioctl request.
105 * @param argp The argument pointer.
106 * @param size The size of the argument.
107 * @return On success, the result of the ioctl. On failure, `ERR` and `errno` is set.
108 */
109uint64_t vfs_ioctl(file_t* file, uint64_t request, void* argp, size_t size);
110
111/**
112 * @brief Memory map a file.
113 *
114 * @param file The file to memory map.
115 * @param address The address to map to, or `NULL` to let the kernel choose.
116 * @param length The length to map.
117 * @param flags The page table flags for the mapping.
118 * @return On success, the mapped address. On failure, returns `NULL` and `errno` is set.
119 */
120void* vfs_mmap(file_t* file, void* address, size_t length, pml_flags_t flags);
121
122/**
123 * @brief Poll multiple files.
124 *
125 * @param files The array of files to poll.
126 * @param amount The number of files in the array.
127 * @param timeout The timeout in clock ticks, or `CLOCKS_NEVER` to wait indefinitely.
128 * @return On success, the number of files that are ready. On failure, `ERR` and `errno` is set.
129 */
131
132/**
133 * @brief Get directory entries from a directory file.
134 *
135 * @param file The directory file to read from.
136 * @param buffer The buffer to read into.
137 * @param count The number of bytes to read.
138 * @return On success, the number of bytes read. On failure, `ERR` and `errno` is set.
139 */
140size_t vfs_getdents(file_t* file, dirent_t* buffer, size_t count);
141
142/**
143 * @brief Get file information.
144 *
145 * @param pathname The pathname of the file to get information about.
146 * @param buffer The buffer to store the file information in.
147 * @param process The process performing the stat.
148 * @return On success, `0`. On failure, `ERR` and `errno` is set.
149 */
150uint64_t vfs_stat(const pathname_t* pathname, stat_t* buffer, process_t* process);
151
152/**
153 * @brief Make the same file appear twice in the filesystem.
154 *
155 * @param oldPathname The existing file.
156 * @param newPathname The new link to create, must not exist and be in the same filesystem as the oldPathname.
157 * @param process The process performing the linking.
158 * @return On success, `0`. On failure, `ERR` and `errno` is set.
159 */
160uint64_t vfs_link(const pathname_t* oldPathname, const pathname_t* newPathname, process_t* process);
161
162/**
163 * @brief Read the path in a symbolic link.
164 *
165 * @param symlink The symbolic link vnode.
166 * @param buffer The buffer to store the path in.
167 * @param size The size of the buffer.
168 * @param process The process performing the readlink.
169 * @return On success, the number of bytes read. On failure, `ERR` and `errno` is set.
170 */
171size_t vfs_readlink(vnode_t* symlink, char* buffer, size_t size);
172
173/**
174 * @brief Create a symbolic link.
175 *
176 * @param target The pathname to which the symbolic link will point.
177 * @param linkpath The pathname of the symbolic link to create.
178 * @param process The process performing the symlink creation.
179 * @return On success, `0`. On failure, `ERR` and `errno` is set.
180 */
181uint64_t vfs_symlink(const pathname_t* target, const pathname_t* linkpath, process_t* process);
182
183/**
184 * @brief Remove a file or directory.
185 *
186 * @param pathname The pathname of the file or directory to remove.
187 * @param process The process performing the removal.
188 * @return On success, `0`. On failure, `ERR` and `errno` is set.
189 */
190uint64_t vfs_remove(const pathname_t* pathname, process_t* process);
191
192/**
193 * @brief Generates a new unique ID, to be used for any VFS object.
194 *
195 * @return A new unique ID.
196 */
197uint64_t vfs_id_get(void);
198
199/**
200 * @brief Helper macros for implementing file operations dealing with simple buffers.
201 *
202 * @param buffer The destination buffer.
203 * @param count The number of bytes to read/write.
204 * @param offset A pointer to the current offset, will be updated.
205 * @param src The source buffer.
206 * @param size The size of the source buffer.
207 * @return The number of bytes read/written.
208 */
209#define BUFFER_READ(buffer, count, offset, src, size) \
210 ({ \
211 size_t readCount = (*(offset) <= (size)) ? MIN((count), (size) - *(offset)) : 0; \
212 memcpy((buffer), (src) + *(offset), readCount); \
213 *(offset) += readCount; \
214 readCount; \
215 })
216
217/**
218 * @brief Helper macro for implementing file operations dealing with simple buffer writes.
219 *
220 * @param buffer The destination buffer.
221 * @param count The number of bytes to write.
222 * @param offset A pointer to the current offset, will be updated.
223 * @param dest The destination buffer.
224 * @param size The size of the destination buffer.
225 * @return The number of bytes written.
226 */
227#define BUFFER_WRITE(buffer, count, offset, dest, size) \
228 ({ \
229 size_t writeCount = (*(offset) <= (size)) ? MIN((count), (size) - *(offset)) : 0; \
230 memcpy((dest) + *(offset), (buffer), writeCount); \
231 *(offset) += writeCount; \
232 writeCount; \
233 })
234
235/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:96
size_t vfs_seek(file_t *file, ssize_t offset, seek_origin_t origin)
Seek in a file.
Definition vfs.c:355
uint64_t vfs_poll(poll_file_t *files, uint64_t amount, clock_t timeout)
Poll multiple files.
Definition vfs.c:503
size_t vfs_write(file_t *file, const void *buffer, size_t count)
Write to a file.
Definition vfs.c:313
size_t vfs_read(file_t *file, void *buffer, size_t count)
Read from a file.
Definition vfs.c:279
file_t * vfs_open(const pathname_t *pathname, process_t *process)
Open a file.
Definition vfs.c:156
size_t vfs_readlink(vnode_t *symlink, char *buffer, size_t size)
Read the path in a symbolic link.
Definition vfs.c:1080
uint64_t vfs_symlink(const pathname_t *target, const pathname_t *linkpath, process_t *process)
Create a symbolic link.
Definition vfs.c:1104
size_t vfs_getdents(file_t *file, dirent_t *buffer, size_t count)
Get directory entries from a directory file.
Definition vfs.c:845
uint64_t vfs_remove(const pathname_t *pathname, process_t *process)
Remove a file or directory.
Definition vfs.c:1160
uint64_t vfs_stat(const pathname_t *pathname, stat_t *buffer, process_t *process)
Get file information.
Definition vfs.c:925
file_t * vfs_openat(const path_t *from, const pathname_t *pathname, process_t *process)
Open a file relative to another path.
Definition vfs.c:223
void * vfs_mmap(file_t *file, void *address, size_t length, pml_flags_t flags)
Memory map a file.
Definition vfs.c:397
uint64_t vfs_link(const pathname_t *oldPathname, const pathname_t *newPathname, process_t *process)
Make the same file appear twice in the filesystem.
Definition vfs.c:991
uint64_t vfs_open2(const pathname_t *pathname, file_t *files[2], process_t *process)
Open one file, returning two file handles.
Definition vfs.c:167
uint64_t vfs_ioctl(file_t *file, uint64_t request, void *argp, size_t size)
Perform an ioctl operation on a file.
Definition vfs.c:373
uint64_t vfs_id_get(void)
Generates a new unique ID, to be used for any VFS object.
Definition vfs.c:1236
uint64_t symlink(const char *target, const char *linkpath)
System call for creating a symbolic link.
Definition symlink.c:8
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition fs.h:260
__INT64_TYPE__ ssize_t
Signed size type.
Definition ssize_t.h:11
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static uint64_t offset
Definition screen.c:19
static list_t files
Definition file.c:9
static const path_flag_t flags[]
Definition path.c:47
static atomic_long count
Definition main.c:11
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry struct.
Definition fs.h:455
File structure.
Definition file.h:39
Path structure.
Definition path.h:127
Pathname structure.
Definition path.h:139
A entry in a page table without a specified address or callback ID.
Structure for polling multiple files.
Definition file.h:71
Process structure.
Definition process.h:76
Stat type.
Definition fs.h:378
vnode structure.
Definition vnode.h:48