PatchworkOS  966e257
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/file.h>
5#include <kernel/fs/inode.h>
6#include <kernel/fs/mount.h>
7#include <kernel/fs/path.h>
9#include <kernel/fs/sysfs.h>
10#include <kernel/proc/process.h>
11#include <kernel/sync/rwlock.h>
12#include <kernel/utils/map.h>
13
14#include <sys/io.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 filesystems used to expose kernel resources to user space.
25 *
26 * @{
27 */
28
29/**
30 * @brief The name of the root entry.
31 */
32#define VFS_ROOT_ENTRY_NAME "__root__"
33
34/**
35 * @brief The name used to indicate no device.
36 */
37#define VFS_DEVICE_NAME_NONE "__no_device__"
38
39/**
40 * @brief Open a file.
41 *
42 * @param pathname The pathname of the file to open.
43 * @param process The process opening the file.
44 * @return On success, the opened file. On failure, returns `NULL` and `errno` is set.
45 */
46file_t* vfs_open(const pathname_t* pathname, process_t* process);
47
48/**
49 * @brief Open one file, returning two file handles.
50 *
51 * Used primarily to implement pipes.
52 *
53 * @param pathname The pathname of the file to open.
54 * @param files The output array of two file pointers.
55 * @param process The process opening the file.
56 * @return On success, `0`. On failure, `ERR` and `errno` is set.
57 */
58uint64_t vfs_open2(const pathname_t* pathname, file_t* files[2], process_t* process);
59
60/**
61 * @brief Open a file relative to another path.
62 *
63 * @param from The path to open the file relative to.
64 * @param pathname The pathname of the file to open.
65 * @param process The process opening the file.
66 * @return On success, the opened file. On failure, returns `NULL` and `errno` is set.
67 */
68file_t* vfs_openat(const path_t* from, const pathname_t* pathname, process_t* process);
69
70/**
71 * @brief Read from a file.
72 *
73 * Follows POSIX semantics.
74 *
75 * @param file The file to read from.
76 * @param buffer The buffer to read into.
77 * @param count The number of bytes to read.
78 * @return On success, the number of bytes read. On failure, `ERR` and `errno` is set.
79 */
81
82/**
83 * @brief Write to a file.
84 *
85 * Follows POSIX semantics.
86 *
87 * @param file The file to write to.
88 * @param buffer The buffer to write from.
89 * @param count The number of bytes to write.
90 * @return On success, the number of bytes written. On failure, `ERR` and `errno` is set.
91 */
93
94/**
95 * @brief Seek in a file.
96 *
97 * Follows POSIX semantics.
98 *
99 * @param file The file to seek in.
100 * @param offset The offset to seek to.
101 * @param origin The origin to seek from.
102 * @return On success, the new file position. On failure, `ERR` and `errno` is set.
103 */
105
106/**
107 * @brief Perform an ioctl operation on a file.
108 *
109 * @param file The file to perform the ioctl on.
110 * @param request The ioctl request.
111 * @param argp The argument pointer.
112 * @param size The size of the argument.
113 * @return On success, the result of the ioctl. On failure, `ERR` and `errno` is set.
114 */
115uint64_t vfs_ioctl(file_t* file, uint64_t request, void* argp, uint64_t size);
116
117/**
118 * @brief Memory map a file.
119 *
120 * @param file The file to memory map.
121 * @param address The address to map to, or `NULL` to let the kernel choose.
122 * @param length The length to map.
123 * @param flags The page table flags for the mapping.
124 * @return On success, the mapped address. On failure, returns `NULL` and `errno` is set.
125 */
126void* vfs_mmap(file_t* file, void* address, uint64_t length, pml_flags_t flags);
127
128/**
129 * @brief Poll multiple files.
130 *
131 * @param files The array of files to poll.
132 * @param amount The number of files in the array.
133 * @param timeout The timeout in clock ticks, or `CLOCKS_NEVER` to wait indefinitely.
134 * @return On success, the number of files that are ready. On failure, `ERR` and `errno` is set.
135 */
137
138/**
139 * @brief Get directory entries from a directory file.
140 *
141 * @param file The directory file to read from.
142 * @param buffer The buffer to read into.
143 * @param count The number of bytes to read.
144 * @return On success, the number of bytes read. On failure, `ERR` and `errno` is set.
145 */
147
148/**
149 * @brief Get file information.
150 *
151 * @param pathname The pathname of the file to get information about.
152 * @param buffer The buffer to store the file information in.
153 * @param process The process performing the stat.
154 * @return On success, `0`. On failure, `ERR` and `errno` is set.
155 */
156uint64_t vfs_stat(const pathname_t* pathname, stat_t* buffer, process_t* process);
157
158/**
159 * @brief Make the same file appear twice in the filesystem.
160 *
161 * @param oldPathname The existing file.
162 * @param newPathname The new link to create, must not exist and be in the same filesystem as the oldPathname.
163 * @param process The process performing the linking.
164 * @return On success, `0`. On failure, `ERR` and `errno` is set.
165 */
166uint64_t vfs_link(const pathname_t* oldPathname, const pathname_t* newPathname, process_t* process);
167
168/**
169 * @brief Remove a file or directory.
170 *
171 * @param pathname The pathname of the file or directory to remove.
172 * @param process The process performing the removal.
173 * @return On success, `0`. On failure, `ERR` and `errno` is set.
174 */
175uint64_t vfs_remove(const pathname_t* pathname, process_t* process);
176
177/**
178 * @brief Generates a new unique ID, to be used for any VFS object.
179 *
180 * @return A new unique ID.
181 */
182uint64_t vfs_id_get(void);
183
184/**
185 * @brief Helper macros for implementing file operations dealing with simple buffers.
186 *
187 * @param buffer The destination buffer.
188 * @param count The number of bytes to read/write.
189 * @param offset A pointer to the current offset, will be updated.
190 * @param src The source buffer.
191 * @param size The size of the source buffer.
192 * @return The number of bytes read/written.
193 */
194#define BUFFER_READ(buffer, count, offset, src, size) \
195 ({ \
196 uint64_t readCount = (*(offset) <= (size)) ? MIN((count), (size) - *(offset)) : 0; \
197 memcpy((buffer), (src) + *(offset), readCount); \
198 *(offset) += readCount; \
199 readCount; \
200 })
201
202/**
203 * @brief Helper macro for implementing file operations dealing with simple buffer writes.
204 *
205 * @param buffer The destination buffer.
206 * @param count The number of bytes to write.
207 * @param offset A pointer to the current offset, will be updated.
208 * @param src The source buffer.
209 * @param size The size of the source buffer.
210 * @return The number of bytes written.
211 */
212#define BUFFER_WRITE(buffer, count, offset, src, size) \
213 ({ \
214 uint64_t writeCount = (*(offset) <= (size)) ? MIN((count), (size) - *(offset)) : 0; \
215 memcpy((buffer) + *(offset), (src), writeCount); \
216 *(offset) += writeCount; \
217 writeCount; \
218 })
219
220/** @} */
uint64_t vfs_ioctl(file_t *file, uint64_t request, void *argp, uint64_t size)
Perform an ioctl operation on a file.
Definition vfs.c:304
uint64_t vfs_seek(file_t *file, int64_t offset, seek_origin_t origin)
Seek in a file.
Definition vfs.c:286
void * vfs_mmap(file_t *file, void *address, uint64_t length, pml_flags_t flags)
Memory map a file.
Definition vfs.c:333
uint64_t vfs_poll(poll_file_t *files, uint64_t amount, clock_t timeout)
Poll multiple files.
Definition vfs.c:440
uint64_t vfs_getdents(file_t *file, dirent_t *buffer, uint64_t count)
Get directory entries from a directory file.
Definition vfs.c:515
uint64_t vfs_write(file_t *file, const void *buffer, uint64_t count)
Write to a file.
Definition vfs.c:239
file_t * vfs_open(const pathname_t *pathname, process_t *process)
Open a file.
Definition vfs.c:94
uint64_t vfs_remove(const pathname_t *pathname, process_t *process)
Remove a file or directory.
Definition vfs.c:698
uint64_t vfs_stat(const pathname_t *pathname, stat_t *buffer, process_t *process)
Get file information.
Definition vfs.c:556
uint64_t vfs_read(file_t *file, void *buffer, uint64_t count)
Read from a file.
Definition vfs.c:200
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:158
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:609
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:108
uint64_t vfs_id_get(void)
Generates a new unique ID, to be used for any VFS object.
Definition vfs.c:765
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition io.h:231
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
static list_t files
Definition file.c:9
static dentry_t * file
Definition log_file.c:22
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static const path_flag_t flags[]
Definition path.c:42
static atomic_long count
Definition main.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
Directory entry struct.
Definition io.h:393
File structure.
Definition file.h:39
Path structure.
Definition path.h:125
Pathname structure.
Definition path.h:137
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:205
Stat type.
Definition io.h:329