The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
vfs.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/error.h"
4#include "kernel/fs/fs.h"
5
6// maximum length of an absolute path (including the null terminator).
7#define VFS_PATH_MAX 256
8
9// whence values for vfs_lseek (ABI: SEEK_SET, SEEK_CUR, SEEK_END)
10#define VFS_SEEK_SET 0
11#define VFS_SEEK_CUR 1
12#define VFS_SEEK_END 2
13
15
16/**
17 * virtual file system file handle structure representing an open file or directory.
18 * this structure is used to abstract the underlying filesystem implementation.
19 */
20typedef struct
21{
25
26/**
27 * mount a filesystem as the root filesystem.
28 * convenience wrapper for vfs_mount("/", mount).
29 * @param mount pointer to the filesystem mount structure to be used as the root filesystem.
30 * @return KERR_OK on success, or an error code on failure.
31 */
33
34/**
35 * mount a filesystem at a path prefix.
36 * a later, more specific prefix takes precedence over a shorter one during
37 * path resolution (e.g. "/dev" wins over "/"). remounting an existing prefix
38 * replaces its backing filesystem.
39 * @param prefix absolute mount point, e.g. "/" or "/dev".
40 * @param mount the filesystem to mount there (must already be mounted).
41 * @return KERR_OK on success, KERR_INVAL on bad input, KERR_NOSPC if full.
42 */
43kerr_t vfs_mount(const char *prefix, fs_mount_t *mount);
44
45/**
46 * a mounted filesystem, by its place in the mount table (for /proc/mounts).
47 * @param index 0, 1, ... the entries in the order they were mounted.
48 * @param prefix_out gets the mount point.
49 * @param mount_out gets the filesystem.
50 * @return false when index is past the last entry.
51 */
52bool vfs_mount_at(uint32_t index, char *prefix_out, uint32_t prefix_size, fs_mount_t **mount_out);
53
54/**
55 * unmount the filesystem at an exact path prefix.
56 * @param prefix the mount point to remove.
57 * @return KERR_OK on success, KERR_NOENT if nothing is mounted there.
58 */
59kerr_t vfs_umount(const char *prefix);
60
61/**
62 * find the mounted filesystem responsible for an absolute path, and the
63 * sub-path relative to that mount's prefix.
64 *
65 * the backing filesystem is handed a path relative to its own mount point, not
66 * the full absolute path: the root mount "/" sees the path unchanged (still
67 * leading '/'), while a sub-mount at "/dev" sees "/fb0" for "/dev/fb0" and "/"
68 * for "/dev" itself. the relative path always keeps a leading '/'.
69 * @param path absolute path to resolve.
70 * @param rel_out if non-NULL, receives a pointer into @p path (or a static "/")
71 * for the mount-relative sub-path. only valid while @p path is.
72 * @return the backing mount, or NULL if no filesystem covers the path.
73 */
74fs_mount_t *vfs_resolve_mount_path(const char *path, const char **rel_out);
75
76/**
77 * replace the symbolic links along a canonical absolute path by their
78 * targets (vfs_link.c).
79 * @param path the real path, changed in place.
80 * @param size the size of the path buffer.
81 * @param follow_last also the last name, when it is a link.
82 * @param root the process's root directory ("/" for the real one): an
83 * absolute target starts there, and ".." stops there.
84 * @return KERR_OK (a missing name is left for the operation to report),
85 * KERR_LOOP (too many links), KERR_RANGE (the path got too long).
86 */
87kerr_t vfs_follow(char *path, uint32_t size, bool follow_last, const char *root);
88
89/**
90 * a real path as a process with this root directory sees it.
91 * @return a pointer into path, or "/"; path itself when it is not under
92 * the root.
93 */
94const char *vfs_inside_root(const char *path, const char *root);
95
96/**
97 * the real path of a path as a process with this root sees it.
98 * @return false when it does not fit in size.
99 */
100bool vfs_join_root(const char *root, const char *virtual_path, char *out, uint32_t size);
101
102/**
103 * read the target of the symbolic link at path, with a NUL.
104 * @return KERR_OK, KERR_INVAL (not a link), KERR_RANGE, KERR_NOTFOUND.
105 */
106kerr_t vfs_readlink(const char *path, char *target, uint32_t size);
107
108/** make a symbolic link at path to target. */
109kerr_t vfs_symlink(const char *path, const char *target);
110
111/**
112 * give a file one more name (both paths on one mount, else KERR_INVAL).
113 * @return KERR_OK, KERR_ISDIR, KERR_EXIST, KERR_RANGE, KERR_NOTFOUND.
114 */
115kerr_t vfs_link(const char *old_path, const char *new_path);
116
117/**
118 * make a named pipe or a device node (fs_ops_t mknod).
119 */
120kerr_t vfs_mknod(const char *path, uint32_t mode, uint32_t rdev);
121
122/**
123 * open a file or directory by absolute path in the virtual file system.
124 * @param path absolute path to the file or directory.
125 * @param file pointer to a vfs_file_t structure that will be initialized with the opened file
126 * @return KERR_OK on success, or an error code on failure.
127 */
129 const char *path,
131
132/**
133 * create a regular file by absolute path.
134 * @param path absolute path to the new file.
135 * @return KERR_OK on success, or an error code on failure.
136 */
137kerr_t vfs_create(const char *path);
138
139/**
140 * create a directory by absolute path.
141 * @param path absolute path to the new directory.
142 * @return KERR_OK on success, or an error code on failure.
143 */
144kerr_t vfs_mkdir(const char *path);
145
146/**
147 * remove a file by absolute path.
148 * @param path absolute path to the file to remove.
149 * @return KERR_OK on success, or an error code on failure.
150 */
151kerr_t vfs_remove(const char *path);
152
153/**
154 * remove a directory by absolute path.
155 * @param path absolute path to the directory to remove.
156 * @return KERR_OK on success, or an error code on failure.
157 */
158kerr_t vfs_rmdir(const char *path);
159
160/**
161 * rename a file or directory by absolute paths.
162 * @param old_path absolute path to the existing file or directory.
163 * @param new_path absolute path to the new name for the file or directory.
164 * @return KERR_OK on success, or an error code on failure.
165 */
166kerr_t vfs_rename(const char *old_path, const char *new_path);
167
168/**
169 * stat a file or directory by absolute path.
170 * @param path absolute path to the file or directory.
171 * @param stat_out pointer to a vfs_stat_t structure that will be filled with the file's metadata.
172 * @return KERR_OK on success, or an error code on failure.
173 */
174kerr_t vfs_stat(const char *path, vfs_stat_t *stat_out);
175
176/**
177 * resolve an input path against the current working directory.
178 * absolute paths are returned as-is, relative paths are prefixed with cwd.
179 * @param cwd current working directory.
180 * @param input user-provided path.
181 * @param output destination buffer for resolved absolute path.
182 * @param size size of destination buffer in bytes.
183 * @return true on success, false if inputs are invalid or output would overflow.
184 */
186 const char *cwd,
187 const char *input,
188 char *output,
189 size_t size);
190
191/**
192 * join two path components into an output buffer.
193 * avoids duplicate separators when base already ends with '/'.
194 * @param base base path component.
195 * @param name child path component.
196 * @param out destination buffer.
197 * @param size destination buffer size in bytes.
198 * @return true on success, false on invalid input or insufficient output size.
199 */
200bool vfs_join_path(
201 const char *base,
202 const char *name,
203 char *out,
204 size_t size);
205
206/**
207 * read data from an open file in the virtual file system.
208 * @param file pointer to the vfs_file_t structure representing the open file.
209 * @param size number of bytes to read.
210 * @param buffer pointer to the buffer where the read data will be stored.
211 * @return number of bytes actually read, or 0 on failure.
212 */
215 uint32_t size,
216 void *buffer);
217
218/**
219 * write data to an open file in the virtual file system.
220 * @param file pointer to the vfs_file_t structure representing the open file.
221 * @param size number of bytes to write.
222 * @param buffer pointer to the source data.
223 * @return number of bytes actually written, or 0 on failure.
224 */
227 uint32_t size,
228 const void *buffer);
229
230/**
231 * cut or grow an open file to length bytes (growth reads as zeros).
232 * @return KERR_OK on success, or an error code on failure.
233 */
235
236/**
237 * read a directory entry from an open directory in the virtual file system.
238 * @param file pointer to the vfs_file_t structure representing the open directory.
239 * @param entryOut pointer to output directory entry.
240 * @return KERR_OK on success, or an error code on failure.
241 */
244 fs_dirent_t *entryOut);
245
246/**
247 * close an open file in the virtual file system.
248 * @param file pointer to the vfs_file_t structure representing the open file.
249 * @return void
250 */
251void vfs_close(
253
254/**
255 * get the type of an open file in the virtual file system.
256 * @param file pointer to the vfs_file_t structure representing the open file.
257 * @return file type (FS_TYPE_FILE, FS_TYPE_DIR, or FS_TYPE_UNKNOWN).
258 */
261
262/**
263 * issue a device-control (ioctl) request on an open file.
264 * dispatches through the backing filesystem; only special filesystems (devfs)
265 * implement it, forwarding to the device. others return -ENOTTY.
266 * @param file pointer to the vfs_file_t representing the open file.
267 * @param request driver-defined request code.
268 * @param arg request-specific argument (typically a bounds-checked pointer).
269 * @return 0 on success, or a negative errno on failure.
270 */
271int32_t vfs_ioctl(vfs_file_t *file, uint32_t request, void *arg);
272
273/**
274 * map an open file's memory into a process address space.
275 * dispatches through the backing filesystem; only device nodes (devfs) that
276 * forward to a mappable device succeed. others return -ENODEV.
277 * @param file pointer to the vfs_file_t representing the open file.
278 * @param page_directory physical address of the target process page directory.
279 * @param virt page-aligned destination virtual address.
280 * @param length bytes to map (page-multiple).
281 * @param prot PROT_* bits.
282 * @param offset page-aligned byte offset into the mapped object.
283 * @return 0 on success, or a negative errno on failure.
284 */
286 size_t length, int prot, uint64_t offset);
287
288/**
289 * move an open file's offset, as lseek does: through the filesystem's own
290 * seek op if it has one (devfs), else the ext2 file cursor.
291 * @param file the open file.
292 * @param offset bytes relative to whence.
293 * @param whence VFS_SEEK_SET, VFS_SEEK_CUR or VFS_SEEK_END.
294 * @return the new offset, or a negative errno (EINVAL for a bad whence or a
295 * negative result).
296 */
297int64_t vfs_lseek(vfs_file_t *file, int64_t offset, int whence);
298
299/**
300 * move an open file's offset to its end (O_APPEND writes).
301 * @param file the open file.
302 */
304
305/**
306 * stat an open file or directory.
307 * @param file the open file.
308 * @param stat_out receives its metadata.
309 * @return KERR_OK, or an error code.
310 */
312
313/**
314 * change the permission bits and/or owner of a file by path, or of an
315 * open file.
316 * @return KERR_OK, KERR_PERM if the filesystem keeps no owners, or the
317 * error of opening the path.
318 */
319kerr_t vfs_setattr(const char *path, const fs_attr_t *attr);
uintptr_t addr_space_t
an address space handle.
Definition arch.h:105
kerr_t
unified kernel error codes.
Definition error.h:14
struct fs_mount fs_mount_t
filesystem operations vtable.
Definition ext2_ops.h:7
struct fs_stat fs_stat_t
Definition fs.h:37
struct fs_dirent fs_dirent_t
Definition fs.h:36
struct fs_file fs_file_t
Definition fs.h:35
uint32_t base
Definition gdt.c:1
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
__INT64_TYPE__ int64_t
Definition stdint.h:16
an open-file description.
Definition file.h:64
a change of a file's permissions or owner (setattr).
Definition fs.h:49
virtual file system file handle structure representing an open file or directory.
Definition vfs.h:21
fs_file_t file
Definition vfs.h:23
fs_mount_t * mount
Definition vfs.h:22
kerr_t vfs_fstat(vfs_file_t *file, vfs_stat_t *stat_out)
stat an open file or directory.
Definition vfs_ops.c:270
bool vfs_join_root(const char *root, const char *virtual_path, char *out, uint32_t size)
the real path of a path as a process with this root sees it.
Definition vfs_link.c:42
kerr_t vfs_mount(const char *prefix, fs_mount_t *mount)
mount a filesystem at a path prefix.
Definition vfs_mount.c:69
kerr_t vfs_mknod(const char *path, uint32_t mode, uint32_t rdev)
make a named pipe or a device node (fs_ops_t mknod).
Definition vfs_link.c:173
uint8_t vfs_file_type(vfs_file_t *file)
get the type of an open file in the virtual file system.
Definition vfs_ops.c:209
bool vfs_join_path(const char *base, const char *name, char *out, size_t size)
join two path components into an output buffer.
Definition vfs_path.c:230
kerr_t vfs_mount_root(fs_mount_t *mount)
mount a filesystem as the root filesystem.
Definition vfs_mount.c:212
kerr_t vfs_read_entry(vfs_file_t *file, fs_dirent_t *entryOut)
read a directory entry from an open directory in the virtual file system.
Definition vfs_ops.c:185
kerr_t vfs_create(const char *path)
create a regular file by absolute path.
Definition vfs_ops.c:32
kerr_t vfs_fsetattr(vfs_file_t *file, const fs_attr_t *attr)
Definition vfs_ops.c:292
uint32_t vfs_write(vfs_file_t *file, uint32_t size, const void *buffer)
write data to an open file in the virtual file system.
Definition vfs_ops.c:159
kerr_t vfs_truncate(vfs_file_t *file, uint64_t length)
cut or grow an open file to length bytes (growth reads as zeros).
Definition vfs_ops.c:175
kerr_t vfs_follow(char *path, uint32_t size, bool follow_last, const char *root)
replace the symbolic links along a canonical absolute path by their targets (vfs_link....
Definition vfs_link.c:60
kerr_t vfs_symlink(const char *path, const char *target)
make a symbolic link at path to target.
Definition vfs_link.c:150
int32_t vfs_ioctl(vfs_file_t *file, uint32_t request, void *arg)
issue a device-control (ioctl) request on an open file.
Definition vfs_ops.c:220
kerr_t vfs_mkdir(const char *path)
create a directory by absolute path.
Definition vfs_ops.c:49
kerr_t vfs_setattr(const char *path, const fs_attr_t *attr)
change the permission bits and/or owner of a file by path, or of an open file.
Definition vfs_ops.c:279
fs_stat_t vfs_stat_t
Definition vfs.h:14
kerr_t vfs_stat(const char *path, vfs_stat_t *stat_out)
stat a file or directory by absolute path.
Definition vfs_ops.c:126
fs_mount_t * vfs_resolve_mount_path(const char *path, const char **rel_out)
find the mounted filesystem responsible for an absolute path, and the sub-path relative to that mount...
Definition vfs_mount.c:151
kerr_t vfs_rmdir(const char *path)
remove a directory by absolute path.
Definition vfs_ops.c:83
void vfs_close(vfs_file_t *file)
close an open file in the virtual file system.
Definition vfs_ops.c:197
void vfs_seek_end(vfs_file_t *file)
move an open file's offset to its end (O_APPEND writes).
Definition vfs_ops.c:265
kerr_t vfs_readlink(const char *path, char *target, uint32_t size)
read the target of the symbolic link at path, with a NUL.
Definition vfs_link.c:139
kerr_t vfs_link(const char *old_path, const char *new_path)
give a file one more name (both paths on one mount, else KERR_INVAL).
Definition vfs_link.c:161
kerr_t vfs_remove(const char *path)
remove a file by absolute path.
Definition vfs_ops.c:66
int64_t vfs_lseek(vfs_file_t *file, int64_t offset, int whence)
move an open file's offset, as lseek does: through the filesystem's own seek op if it has one (devfs)...
Definition vfs_ops.c:241
const char * vfs_inside_root(const char *path, const char *root)
a real path as a process with this root directory sees it.
Definition vfs_link.c:28
kerr_t vfs_rename(const char *old_path, const char *new_path)
rename a file or directory by absolute paths.
Definition vfs_ops.c:100
uint32_t vfs_read(vfs_file_t *file, uint32_t size, void *buffer)
read data from an open file in the virtual file system.
Definition vfs_ops.c:143
bool vfs_mount_at(uint32_t index, char *prefix_out, uint32_t prefix_size, fs_mount_t **mount_out)
a mounted filesystem, by its place in the mount table (for /proc/mounts).
Definition vfs_mount.c:128
bool vfs_resolve_path(const char *cwd, const char *input, char *output, size_t size)
resolve an input path against the current working directory.
Definition vfs_path.c:120
kerr_t vfs_open(const char *path, vfs_file_t *file)
open a file or directory by absolute path in the virtual file system.
Definition vfs_ops.c:6
kerr_t vfs_umount(const char *prefix)
unmount the filesystem at an exact path prefix.
Definition vfs_mount.c:108
int32_t vfs_mmap(vfs_file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
map an open file's memory into a process address space.
Definition vfs_ops.c:230