PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
filesystem.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 Filesystem interface.
21 * @defgroup kernel_fs_filesystem Filesystem.
22 * @ingroup kernel_fs
23 *
24 * The filesystem interface represents a filesystem type, e.g. fat32, tmpfs, devfs, etc. Each filesystem is exposed in a
25 * directory within the `fs` sysfs directory named after the filesystem.
26 *
27 * The directory itself can be used to mount instances of that filesystem type.
28 *
29 * Within each filesystem directory are readable files representing each mounted instance of that filesystem type, named
30 * after the superblock ID, containing the following information:
31 *
32 * ```
33 * id: %llu
34 * block_size: %llu
35 * max_file_size: %llu
36 *
37 * ```
38 *
39 * Where the `id` is the superblock ID, `block_size` is the block size of the superblock, and `max_file_size` is the
40 * maximum size of a file on this superblock.
41 *
42 * @see kernel_fs_sysfs
43 *
44 * @{
45 */
46
47/**
48 * @brief Filesystem structure, represents a filesystem type, e.g. fat32, tmpfs, devfs, etc.
49 *
50 * @todo Add safety for if a module defining a filesystem is unloaded.
51 */
52typedef struct filesystem
53{
54 list_entry_t entry; ///< Used internally.
55 map_entry_t mapEntry; ///< Used internally.
56 list_t superblocks; ///< Used internally.
57 rwlock_t lock; ///< Used internally.
58 const char* name;
59 /**
60 * @brief Mount a filesystem.
61 *
62 * @param fs The filesystem to mount.
63 * @param details A string containing filesystem defined `key=value` pairs, with multiple options separated by
64 * commas, or `NULL`.
65 * @param private Private data for the filesystem's mount function.
66 * @return On success, the root dentry of the mounted filesystem. On failure, `NULL` and `errno` is set.
67 */
68 dentry_t* (*mount)(filesystem_t* fs, const char* details, void* data);
70
71/**s
72 * @brief Exposes the sysfs `fs` directory.
73 *
74 * Must be called before `filesystem_get_by_path()` can be used.
75 */
76void filesystem_expose(void);
77
78/**
79 * @brief Registers a filesystem.
80 *
81 * @param fs The filesystem to register.
82 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
83 * - `EINVAL`: Invalid parameters.
84 * - Other values from `map_insert()`.
85 */
87
88/**
89 * @brief Unregisters a filesystem.
90 *
91 * @param fs The filesystem to unregister, or `NULL` for no-op.
92 */
94
95/**
96 * @brief Gets a filesystem by name.
97 *
98 * @param name The name of the filesystem.
99 * @return On success, the filesystem. On failure, returns `NULL`.
100 */
101filesystem_t* filesystem_get_by_name(const char* name);
102
103/**
104 * @brief Gets a filesystem by path.
105 *
106 * The path should point to a directory in the `fs` sysfs directory.
107 *
108 * @param path The path to check.
109 * @param process The process whose namespace to use.
110 * @return On success, the filesystem. On failure, returns `NULL` and `errno` is set to:
111 * - `ENOENT`: The path does not exist.
112 * - `ENOMEM`: Out of memory.
113 * - `EINVAL`: The path is not a directory in the `fs` sysfs directory.
114 */
115filesystem_t* filesystem_get_by_path(const char* path, process_t* process);
116
117/**
118 * @brief Helper function for iterating over options passed to a filesystem mount operation.
119 *
120 * Each helper option is specified as `key=value` pairs, with multiple options separated by commas.
121 *
122 * @param iter Pointer to the current iterator position. Updated on each call.
123 * @param buffer Buffer to store the current option.
124 * @param size Size of the buffer.
125 * @param key Pointer to store the key of the current option.
126 * @param value Pointer to store the value of the current option.
127 * @return `1` if an option was found, `0` if no more options are available.
128 */
129bool options_next(const char** iter, char* buffer, size_t size, char** key, char** value);
130
131/**
132 * @brief Helper macro for iterating over options passed to a filesystem mount operation.
133 *
134 * Each helper option is specified as `key=value` pairs, with multiple options separated by commas.
135 *
136 * @param options The options string.
137 * @param key The key variable.
138 * @param value The value variable.
139 */
140#define OPTIONS_FOR_EACH(options, key, value) \
141 for (struct { \
142 const char* iter; \
143 char buf[256]; \
144 } _state = {(options), {0}}; \
145 options_next(&_state.iter, _state.buf, sizeof(_state.buf), &(key), &(value));)
146
147/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
static fd_t data
Definition dwm.c:21
void filesystem_expose(void)
Exposes the sysfs fs directory.
Definition filesystem.c:215
filesystem_t * filesystem_get_by_path(const char *path, process_t *process)
Gets a filesystem by path.
Definition filesystem.c:284
uint64_t filesystem_register(filesystem_t *fs)
Registers a filesystem.
Definition filesystem.c:233
bool options_next(const char **iter, char *buffer, size_t size, char **key, char **value)
Helper function for iterating over options passed to a filesystem mount operation.
Definition filesystem.c:327
filesystem_t * filesystem_get_by_name(const char *name)
Gets a filesystem by name.
Definition filesystem.c:276
void filesystem_unregister(filesystem_t *fs)
Unregisters a filesystem.
Definition filesystem.c:259
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry structure.
Definition dentry.h:155
Filesystem structure, represents a filesystem type, e.g. fat32, tmpfs, devfs, etc.
Definition filesystem.h:53
list_entry_t entry
Used internally.
Definition filesystem.h:54
const char * name
Definition filesystem.h:58
map_entry_t mapEntry
Used internally.
Definition filesystem.h:55
rwlock_t lock
Used internally.
Definition filesystem.h:57
list_t superblocks
Used internally.
Definition filesystem.h:56
A entry in a doubly linked list.
Definition list.h:37
A doubly linked list.
Definition list.h:46
Map entry structure.
Definition map.h:69
Process structure.
Definition process.h:76
Read-Write Ticket Lock structure.
Definition rwlock.h:66