MOS Source Code
Loading...
Searching...
No Matches
vfs_types.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
6#include "mos/mm/mm.hpp"
8
9#include <abi-bits/stat.h>
10#include <cstddef>
11#include <mos/allocator.hpp>
13#include <mos/hashmap.hpp>
14#include <mos/io/io.hpp>
15#include <mos/io/io_types.h>
21#include <mos/string.hpp>
22#include <mos/types.hpp>
23
24#define FILESYSTEM_DEFINE(var, fsname, mountfn, unmountfn) \
25 filesystem_t var = { \
26 .name = fsname, \
27 .mount = mountfn, \
28 .unmount = unmountfn, \
29 }
30
31#define FILESYSTEM_AUTOREGISTER(fs) \
32 static void __register_##fs() \
33 { \
34 vfs_register_filesystem(&fs); \
35 } \
36 MOS_INIT(VFS, __register_##fs)
37
38struct dentry_t;
39typedef struct _inode_cache inode_cache_t;
40struct inode_t;
41struct mount_t;
42struct superblock_t;
43struct filesystem_t;
44struct file_t;
45
53
54struct vfs_listdir_state_t : mos::NamedType<"VFS.ListDir.State">
55{
57 size_t n_count;
58 size_t read_offset;
59};
60
62
63typedef struct
64{
66 bool (*hardlink)(dentry_t *old_dentry, inode_t *dir, dentry_t *new_dentry);
70 bool (*lookup)(inode_t *dir, dentry_t *dentry);
72 bool (*mkdir)(inode_t *dir, dentry_t *dentry, file_perm_t perm);
74 bool (*mknode)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm, dev_t dev);
76 bool (*newfile)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm);
78 size_t (*readlink)(dentry_t *dentry, char *buffer, size_t buflen);
80 bool (*rename)(inode_t *old_dir, dentry_t *old_dentry, inode_t *new_dir, dentry_t *new_dentry);
82 bool (*rmdir)(inode_t *dir, dentry_t *dentry);
84 bool (*symlink)(inode_t *dir, dentry_t *dentry, const char *symname);
86 bool (*unlink)(inode_t *dir, dentry_t *dentry);
88
89typedef struct
90{
91 bool (*open)(inode_t *inode, file_t *file, bool created);
92 ssize_t (*read)(const file_t *file, void *buf, size_t size, off_t offset);
93 ssize_t (*write)(const file_t *file, const void *buf, size_t size, off_t offset);
94 void (*release)(file_t *file);
95 off_t (*seek)(file_t *file, off_t offset, io_seek_whence_t whence);
96 bool (*mmap)(file_t *file, vmap_t *vmap, off_t offset);
97 bool (*munmap)(file_t *file, vmap_t *vmap, bool *unmapped);
99
100typedef struct
101{
103 long (*sync_inode)(inode_t *inode);
105
106struct superblock_t final : mos::NamedType<"superblock">
107{
111};
112
113struct dentry_t final : mos::NamedType<"dentry">
114{
119 mos::string name; // for a mounted root, this is EMPTY
120 superblock_t *superblock; // The superblock of the dentry
122};
123
124extern dentry_t *root_dentry;
125
126inline mos::string dentry_name(const dentry_t *dentry)
127{
128 const auto name = (dentry)->name;
129 return name.empty() ? (dentry == root_dentry ? "<root>" : "<NULL>") : name;
130}
131
132typedef struct _inode_cache_ops
133{
138
139 bool (*page_write_begin)(inode_cache_t *cache, off_t file_offset, size_t inpage_size, phyframe_t **page_out, void **data);
140 void (*page_write_end)(inode_cache_t *cache, off_t file_offset, size_t inpage_size, phyframe_t *page, void *data);
141
145 long (*flush_page)(inode_cache_t *cache, uint64_t pgoff, phyframe_t *page);
147
148typedef struct _inode_cache
149{
152 mos::HashMap<size_t, phyframe_t *> pages; // page index -> phyframe_t *
155
156struct inode_t final : mos::NamedType<"inode">
157{
161 size_t size;
164 bool sticky;
165 bool suid;
166 bool sgid;
167 ssize_t nlinks; // number of hard links to this inode
171
172 superblock_t *superblock; // superblock of this inode
173 const inode_ops_t *ops; // operations on this inode
174 const file_ops_t *file_ops; // operations on files of this inode
175 void *private_data; // private data
176 inode_cache_t cache; // page cache for this inode
177
179};
180
181struct filesystem_t final : mos::NamedType<"filesystem">
182{
185 PtrResult<dentry_t> (*mount)(filesystem_t *fs, const char *dev_name, const char *mount_options);
186 void (*unmount)(filesystem_t *fs, dentry_t *mountpoint); // called when the mountpoint is unmounted
187};
188
189struct mount_t final : mos::NamedType<"mount">
190{
192 dentry_t *root; // root of the mounted tree
193 dentry_t *mountpoint; // where the tree is mounted
196};
197
198struct file_t final : mos::NamedType<"file">
199{
200 io_t io; // refcount is tracked by the io_t
202 spinlock_t offset_lock; // protects the offset field
203 size_t offset; // tracks the current position in the file
205};
u16 file_perm_t
Definition fs_types.h:52
file_type_t
Definition fs_types.h:14
MOSAPI void(1, 2) fatal_abort(const char *fmt
list_node_t list_head
A linked list head.
Definition list.hpp:23
dentry_t * root_dentry
Definition vfs.cpp:35
io_seek_whence_t
Definition io_types.h:6
futex_word_t mutex_t
Definition mutex.hpp:8
basic_string_view< char > string_view
mos::basic_string< char, mos::default_allocator > string
Definition string.hpp:336
#define uint64_t
int bool
Definition pb_syshdr.h:57
uint32_t size_t
Definition pb_syshdr.h:42
size_t size
Definition slab.cpp:34
const char * name
Definition slab.cpp:35
superblock_t * superblock
mos::string name
bool is_mountpoint
spinlock_t lock
atomic_t refcount
inode_t * inode
bool(* open)(inode_t *inode, file_t *file, bool created)
called when a file is opened, or created
Definition vfs_types.hpp:91
off_t(* seek)(file_t *file, off_t offset, io_seek_whence_t whence)
seek to a new position in the file
Definition vfs_types.hpp:95
bool(* munmap)(file_t *file, vmap_t *vmap, bool *unmapped)
unmap the file from memory
Definition vfs_types.hpp:97
void(* release)(file_t *file)
called when the last reference to the file is dropped
Definition vfs_types.hpp:94
ssize_t(* read)(const file_t *file, void *buf, size_t size, off_t offset)
read from the file
Definition vfs_types.hpp:92
ssize_t(* write)(const file_t *file, const void *buf, size_t size, off_t offset)
write to the file
Definition vfs_types.hpp:93
bool(* mmap)(file_t *file, vmap_t *vmap, off_t offset)
map the file into memory
Definition vfs_types.hpp:96
spinlock_t offset_lock
io_t io
size_t offset
void * private_data
dentry_t * dentry
void(* unmount)(filesystem_t *fs, dentry_t *mountpoint)
mos::string name
PtrResult< dentry_t >(* mount)(filesystem_t *fs, const char *dev_name, const char *mount_options)
PtrResult< phyframe_t >(* fill_cache)(inode_cache_t *cache, uint64_t pgoff)
Read a page from the underlying storage, at file offset pgoff * MOS_PAGE_SIZE.
long(* flush_page)(inode_cache_t *cache, uint64_t pgoff, phyframe_t *page)
Flush a page to the underlying storage.
bool(* page_write_begin)(inode_cache_t *cache, off_t file_offset, size_t inpage_size, phyframe_t **page_out, void **data)
void(* page_write_end)(inode_cache_t *cache, off_t file_offset, size_t inpage_size, phyframe_t *page, void *data)
inode_t * owner
mos::HashMap< size_t, phyframe_t * > pages
const inode_cache_ops_t * ops
bool(* mkdir)(inode_t *dir, dentry_t *dentry, file_perm_t perm)
create a new directory
Definition vfs_types.hpp:72
size_t(* readlink)(dentry_t *dentry, char *buffer, size_t buflen)
read the contents of a symbolic link
Definition vfs_types.hpp:78
bool(* unlink)(inode_t *dir, dentry_t *dentry)
remove a file name, this is called after nlinks is decremented
Definition vfs_types.hpp:86
bool(* hardlink)(dentry_t *old_dentry, inode_t *dir, dentry_t *new_dentry)
create a hard link
Definition vfs_types.hpp:66
bool(* newfile)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm)
create a new file
Definition vfs_types.hpp:76
bool(* rmdir)(inode_t *dir, dentry_t *dentry)
remove a directory
Definition vfs_types.hpp:82
bool(* lookup)(inode_t *dir, dentry_t *dentry)
lookup a file in a directory, if it's unset for a directory, the VFS will use the default lookup
Definition vfs_types.hpp:70
void(* iterate_dir)(dentry_t *dentry, vfs_listdir_state_t *iterator_state, dentry_iterator_op op)
iterate over the contents of a directory
Definition vfs_types.hpp:68
bool(* symlink)(inode_t *dir, dentry_t *dentry, const char *symname)
create a symbolic link
Definition vfs_types.hpp:84
bool(* rename)(inode_t *old_dir, dentry_t *old_dentry, inode_t *new_dir, dentry_t *new_dentry)
rename a file
Definition vfs_types.hpp:80
bool(* mknode)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm, dev_t dev)
create a new device file
Definition vfs_types.hpp:74
superblock_t * superblock
uid_t uid
u64 modified
u64 accessed
u64 created
file_perm_t perm
const inode_ops_t * ops
bool sgid
size_t size
file_type_t type
const file_ops_t * file_ops
atomic_t refcount
number of references to this inode
bool suid
inode_cache_t cache
bool sticky
gid_t gid
void * private_data
ssize_t nlinks
Definition io.hpp:48
superblock_t * superblock
dentry_t * root
dentry_t * mountpoint
filesystem_t * fs
bool(* drop_inode)(inode_t *inode)
The inode has zero links and the last reference to the file has been dropped.
long(* sync_inode)(inode_t *inode)
flush the inode to disk
dentry_t * root
const superblock_ops_t * ops
filesystem_t * fs
Definition vfs_types.hpp:47
mos::string name
Definition vfs_types.hpp:50
as_linked_list
Definition vfs_types.hpp:48
file_type_t type
Definition vfs_types.hpp:51
ino_t ino
Definition vfs_types.hpp:49
size_t read_offset
user has read up to this offset, start from this offset when reading more entries
Definition vfs_types.hpp:58
size_t n_count
number of entries in the list
Definition vfs_types.hpp:57
Definition mm.hpp:59
static char buffer[2048]
ssize_t off_t
Definition types.h:80
u32 uid_t
Definition types.h:71
u32 gid_t
Definition types.h:72
unsigned long long u64
Definition types.h:19
signed long ssize_t
Definition types.h:79
std::atomic_size_t atomic_t
Definition types.hpp:11
mos::string dentry_name(const dentry_t *dentry)
void dentry_iterator_op(vfs_listdir_state_t *state, u64 ino, mos::string_view name, file_type_t type)
Definition vfs_types.hpp:61