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 <uapi/fcntl.h>
43
44#include <lib/path.h>
45#include <libalgo/linked_list.h>
46#include <utils/compiler.h>
47#include <utils/stringify.h>
48
49#include <sys/stat.h>
50
51struct block_device;
52
53typedef struct vfs vfs_t;
54typedef struct vnode vnode_t;
55
56typedef enum vnode_type vnode_type;
57
65typedef struct vfs_operations {
67 vnode_t *(*root)(vfs_t *);
71 void (*delete)(vfs_t *);
72} vfs_ops_t;
73
78typedef struct vfs {
79 node_t this;
82 void *pdata;
83 struct block_device *blkdev;
84} vfs_t;
85
99error_t vfs_mount(const char *path, const char *fs_type, struct block_device *);
100
110error_t vfs_mount_root(const char *fs_type, struct block_device *);
111
117error_t vfs_unmount(const char *path);
118
127vnode_t *vfs_find_by_path(const char *path);
128
138vnode_t *vfs_create(const char *path, vnode_type type);
139
144error_t vfs_remove(const char *path);
145
153struct file *vfs_open(const char *path, int oflags);
154
164typedef enum vnode_type {
165 VNODE_FIFO = S_IFIFO,
167 VNODE_DIRECTORY = S_IFDIR,
169 VNODE_FILE = S_IFREG,
170 VNODE_SYMLINK = S_IFLNK,
171 VNODE_SOCKET = S_IFSOCK,
172} vnode_type;
173
177typedef struct vnode_operations {
178
183 vnode_t *(*lookup)(vnode_t *, const path_segment_t *);
184
186 vnode_t *(*create)(vnode_t *node, const char *name, vnode_type);
187
189 error_t (*remove)(vnode_t *node, const char *child);
190
192 struct file *(*open)(vnode_t *vnode);
193
198 void (*release)(vnode_t *node);
199
201
205struct vnode {
210 void *pdata;
212 struct stat stat;
214};
215
227
235
239static inline bool vfs_exist(const char *path)
240{
241 struct vnode *vnode = vfs_find_by_path(path);
242 if (IS_ERR(vnode))
243 return false;
245 return true;
246}
247
253typedef struct vfs_fs {
254 const char *const name;
255 vfs_t *(*new)(struct block_device *);
257} vfs_fs_t;
258
267#define DECLARE_FILESYSTEM(fs_name, fs_new) \
268 SECTION(".data.vfs.filesystems") \
269 MAYBE_UNUSED \
270 static vfs_fs_t fs_name##_fs_declaration = { \
271 .name = stringify(fs_name), \
272 .new = fs_new, \
273 }
274
error_t vfs_remove(const char *path)
Remove the file located at the given path.
Definition: vfs.c:245
struct file * vfs_open(const char *path, int oflags)
Open the file located at the given path.
Definition: vfs.c:296
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:79
error_t vfs_unmount(const char *path)
Unmount the filesystem present at the given path.
Definition: vfs.c:109
vnode_t * vfs_create(const char *path, vnode_type type)
Create a new file at the given path.
Definition: vfs.c:207
vnode_t * vfs_find_by_path(const char *path)
Retreive the vnode corresponding to a path.
Definition: vfs.c:128
error_t vfs_mount_root(const char *fs_type, struct block_device *)
Mount a filesystem at the root of the VFS.
Definition: vfs.c:70
vnode_t * vfs_vnode_acquire(vnode_t *, bool *)
Increment the refcount of a vnode.
Definition: vfs.c:306
vnode_type
The different existing types of vnodes.
Definition: vfs.h:164
vnode_t * vfs_vnode_release(vnode_t *)
Decrease the refcount of a vnode.
Definition: vfs.c:325
@ VNODE_SOCKET
Socket file.
Definition: vfs.h:171
@ VNODE_SYMLINK
Symbolic link.
Definition: vfs.h:170
@ VNODE_FIFO
FIFO.
Definition: vfs.h:165
@ VNODE_BLOCKDEVICE
Block device.
Definition: vfs.h:168
@ VNODE_FILE
Regular file.
Definition: vfs.h:169
@ VNODE_CHARDEVICE
Character device.
Definition: vfs.h:166
@ VNODE_DIRECTORY
Regular directory.
Definition: vfs.h:167
#define IS_ERR(_x)
Check if an integer can be interpreted as an error.
Definition: error.h:83
static bool vfs_exist(const char *path)
Definition: vfs.h:239
Opened file description.
Definition: file.h:29
A single path component.
Definition: path.h:107
Intrusive doubly-linked list node.
Definition: linked_list.h:27
Spinlock.
Definition: spinlock.h:29
Represents a file system format.
Definition: vfs.h:253
const char *const name
Name of the filesystem.
Definition: vfs.h:254
Vector Table for operations on a filesystems.
Definition: vfs.h:65
represents a single virtual filesystem
Definition: vfs.h:78
struct block_device * blkdev
Block device the filesystem resides on.
Definition: vfs.h:83
vnode_t * node
vnode on which this FS is mounted
Definition: vfs.h:81
vfs_ops_t * operations
vfs_operations
Definition: vfs.h:80
void * pdata
Private FS dependent data.
Definition: vfs.h:82
Vector table for operations performed on a single virtual node.
Definition: vfs.h:177
void(* release)(vnode_t *node)
Called by the VFS driver before deleting a vnode (optional).
Definition: vfs.h:198
error_t(* remove)(vnode_t *node, const char *child)
Remove a child from a directory.
Definition: vfs.h:189
represents a single virtual node
Definition: vfs.h:205
vfs_t * fs
Filesystem to which this node belong.
Definition: vfs.h:206
vnode_ops_t * operations
vnode_operations
Definition: vfs.h:209
u16 refcount
Number of references hold to that node.
Definition: vfs.h:208
void * pdata
Private node data.
Definition: vfs.h:210
vfs_t * mounted_here
Potential filesystem mounted over this node.
Definition: vfs.h:211
struct stat stat
File statistics.
Definition: vfs.h:212
vnode_type type
Type of the node.
Definition: vfs.h:207
spinlock_t lock
Must be held when accessing the node's data.
Definition: vfs.h:213