My Kernel v0.1.0
vfs.h
Go to the documentation of this file.
1#pragma once
2
39#include <kernel/error.h>
40#include <kernel/file.h>
41#include <kernel/types.h>
42#include <kernel/user.h>
43#include <uapi/fcntl.h>
44
45#include <lib/path.h>
46#include <libalgo/linked_list.h>
47#include <utils/compiler.h>
48#include <utils/stringify.h>
49
50#include <sys/stat.h>
51
52struct block_device;
53
54typedef struct vfs vfs_t;
55typedef struct vnode vnode_t;
56
57typedef enum vnode_type vnode_type;
58
66typedef struct vfs_operations {
68 vnode_t *(*root)(vfs_t *);
72 void (*delete)(vfs_t *);
73} vfs_ops_t;
74
79typedef struct vfs {
80 node_t this;
83 void *pdata;
84 struct block_device *blkdev;
85} vfs_t;
86
100error_t vfs_mount(const char *path, const char *fs_type, struct block_device *);
101
111error_t vfs_mount_root(const char *fs_type, struct block_device *);
112
118error_t vfs_unmount(const char *path);
119
128vnode_t *vfs_find_by_path(const char *path);
129
139vnode_t *vfs_create(const char *path, vnode_type type);
140
145error_t vfs_remove(const char *path);
146
154struct file *vfs_open(const char *path, int oflags);
155
165typedef enum vnode_type {
166 VNODE_FIFO = S_IFIFO,
168 VNODE_DIRECTORY = S_IFDIR,
170 VNODE_FILE = S_IFREG,
171 VNODE_SYMLINK = S_IFLNK,
172 VNODE_SOCKET = S_IFSOCK,
173} vnode_type;
174
178typedef struct vnode_operations {
179
184 vnode_t *(*lookup)(vnode_t *, const path_segment_t *);
185
187 vnode_t *(*create)(vnode_t *node, const char *name, vnode_type);
188
190 error_t (*remove)(vnode_t *node, const char *child);
191
193 struct file *(*open)(vnode_t *vnode);
194
199 void (*release)(vnode_t *node);
200
202
206struct vnode {
211 void *pdata;
213 struct stat stat;
215};
216
228
236
248bool vfs_vnode_check_creds(const struct vnode *, const struct user_creds *,
249 int oflags);
250
254static inline bool vfs_exist(const char *path)
255{
256 struct vnode *vnode = vfs_find_by_path(path);
257 if (IS_ERR(vnode))
258 return false;
260 return true;
261}
262
268typedef struct vfs_fs {
269 const char *const name;
270 vfs_t *(*new)(struct block_device *);
272} vfs_fs_t;
273
282#define DECLARE_FILESYSTEM(fs_name, fs_new) \
283 SECTION(".data.vfs.filesystems") \
284 MAYBE_UNUSED \
285 static vfs_fs_t fs_name##_fs_declaration = { \
286 .name = stringify(fs_name), \
287 .new = fs_new, \
288 }
289
error_t vfs_remove(const char *path)
Remove the file located at the given path.
Definition: vfs.c:307
struct file * vfs_open(const char *path, int oflags)
Open the file located at the given path.
Definition: vfs.c:406
error_t vfs_mount(const char *path, const char *fs_type, struct block_device *)
Mount a filesystem of the given type at a given path.
Definition: vfs.c:87
error_t vfs_unmount(const char *path)
Unmount the filesystem present at the given path.
Definition: vfs.c:117
vnode_t * vfs_create(const char *path, vnode_type type)
Create a new file at the given path.
Definition: vfs.c:266
vnode_t * vfs_find_by_path(const char *path)
Retreive the vnode corresponding to a path.
Definition: vfs.c:189
error_t vfs_mount_root(const char *fs_type, struct block_device *)
Mount a filesystem at the root of the VFS.
Definition: vfs.c:78
vnode_t * vfs_vnode_acquire(vnode_t *, bool *)
Increment the refcount of a vnode.
Definition: vfs.c:442
bool vfs_vnode_check_creds(const struct vnode *, const struct user_creds *, int oflags)
Check if a process has the right to access a vnode based on its credentials.
Definition: vfs.c:482
vnode_type
The different existing types of vnodes.
Definition: vfs.h:165
vnode_t * vfs_vnode_release(vnode_t *)
Decrease the refcount of a vnode.
Definition: vfs.c:463
@ VNODE_SOCKET
Socket file.
Definition: vfs.h:172
@ VNODE_SYMLINK
Symbolic link.
Definition: vfs.h:171
@ VNODE_FIFO
FIFO.
Definition: vfs.h:166
@ VNODE_BLOCKDEVICE
Block device.
Definition: vfs.h:169
@ VNODE_FILE
Regular file.
Definition: vfs.h:170
@ VNODE_CHARDEVICE
Character device.
Definition: vfs.h:167
@ VNODE_DIRECTORY
Regular directory.
Definition: vfs.h:168
#define IS_ERR(_x)
Check if an integer can be interpreted as an error.
Definition: error.h:87
static bool vfs_exist(const char *path)
Definition: vfs.h:254
Opened file description.
Definition: file.h:45
A single path component.
Definition: path.h:107
Intrusive doubly-linked list node.
Definition: linked_list.h:27
Spinlock.
Definition: spinlock.h:29
User credentials.
Definition: user.h:20
Represents a file system format.
Definition: vfs.h:268
const char *const name
Name of the filesystem.
Definition: vfs.h:269
Vector Table for operations on a filesystems.
Definition: vfs.h:66
represents a single virtual filesystem
Definition: vfs.h:79
struct block_device * blkdev
Block device the filesystem resides on.
Definition: vfs.h:84
vnode_t * node
vnode on which this FS is mounted
Definition: vfs.h:82
vfs_ops_t * operations
vfs_operations
Definition: vfs.h:81
void * pdata
Private FS dependent data.
Definition: vfs.h:83
Vector table for operations performed on a single virtual node.
Definition: vfs.h:178
void(* release)(vnode_t *node)
Called by the VFS driver before deleting a vnode (optional).
Definition: vfs.h:199
error_t(* remove)(vnode_t *node, const char *child)
Remove a child from a directory.
Definition: vfs.h:190
represents a single virtual node
Definition: vfs.h:206
vfs_t * fs
Filesystem to which this node belong.
Definition: vfs.h:207
vnode_ops_t * operations
vnode_operations
Definition: vfs.h:210
u16 refcount
Number of references hold to that node.
Definition: vfs.h:209
void * pdata
Private node data.
Definition: vfs.h:211
vfs_t * mounted_here
Potential filesystem mounted over this node.
Definition: vfs.h:212
struct stat stat
File statistics.
Definition: vfs.h:213
vnode_type type
Type of the node.
Definition: vfs.h:208
spinlock_t lock
Must be held when accessing the node's data.
Definition: vfs.h:214
Userland control mechanisms.