MOS Source Code
Loading...
Searching...
No Matches
vfs_types.h
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.h"
8#include "mos/mm/slab.h"
9
10#include <abi-bits/stat.h>
12#include <mos/io/io.h>
13#include <mos/io/io_types.h>
19#include <mos/types.h>
20
21#define FILESYSTEM_DEFINE(var, fsname, mountfn, unmountfn) \
22 filesystem_t var = { \
23 .list_node = LIST_HEAD_INIT(var.list_node), \
24 .name = fsname, \
25 .mount = mountfn, \
26 .unmount = unmountfn, \
27 }
28
29#define FILESYSTEM_AUTOREGISTER(fs) \
30 static void __register_##fs() \
31 { \
32 vfs_register_filesystem(&fs); \
33 } \
34 MOS_INIT(VFS, __register_##fs)
35
36typedef struct _dentry dentry_t;
37typedef struct _inode_cache inode_cache_t;
38typedef struct _inode inode_t;
39typedef struct _mount mount_t;
40typedef struct _superblock superblock_t;
41typedef struct _filesystem filesystem_t;
42typedef struct _file file_t;
43
44typedef struct
45{
47 ino_t ino;
48 const char *name;
49 size_t name_len;
52
53typedef struct
54{
56 size_t n_count;
57 size_t read_offset;
59
60typedef void(dentry_iterator_op)(vfs_listdir_state_t *state, u64 ino, const char *name, size_t name_len, file_type_t type);
61
62typedef struct
63{
65 bool (*hardlink)(dentry_t *old_dentry, inode_t *dir, dentry_t *new_dentry);
67 void (*iterate_dir)(dentry_t *dentry, vfs_listdir_state_t *iterator_state, dentry_iterator_op op);
69 bool (*lookup)(inode_t *dir, dentry_t *dentry);
71 bool (*mkdir)(inode_t *dir, dentry_t *dentry, file_perm_t perm);
73 bool (*mknode)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm, dev_t dev);
75 bool (*newfile)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm);
77 size_t (*readlink)(dentry_t *dentry, char *buffer, size_t buflen);
79 bool (*rename)(inode_t *old_dir, dentry_t *old_dentry, inode_t *new_dir, dentry_t *new_dentry);
81 bool (*rmdir)(inode_t *dir, dentry_t *dentry);
83 bool (*symlink)(inode_t *dir, dentry_t *dentry, const char *symname);
85 bool (*unlink)(inode_t *dir, dentry_t *dentry);
87
88typedef struct
89{
90 bool (*open)(inode_t *inode, file_t *file, bool created);
91 ssize_t (*read)(const file_t *file, void *buf, size_t size, off_t offset);
92 ssize_t (*write)(const file_t *file, const void *buf, size_t size, off_t offset);
93 void (*release)(file_t *file);
94 off_t (*seek)(file_t *file, off_t offset, io_seek_whence_t whence);
95 bool (*mmap)(file_t *file, vmap_t *vmap, off_t offset);
96 bool (*munmap)(file_t *file, vmap_t *vmap, bool *unmapped);
98
99typedef struct
100{
101 bool (*drop_inode)(inode_t *inode);
102 long (*sync_inode)(inode_t *inode);
104
105typedef struct _superblock
106{
111
112typedef struct _dentry
113{
116 atomic_t refcount;
118 const char *name; // for a mounted root, this is NULL
119 superblock_t *superblock; // The superblock of the dentry
121} dentry_t;
122
123extern dentry_t *root_dentry;
124
125#define dentry_name(dentry) \
126 __extension__({ \
127 const char *__name = (dentry)->name; \
128 __name ? __name : (dentry == root_dentry ? "<root>" : "<NULL>"); \
129 })
130
131typedef struct _inode_cache_ops
132{
136 phyframe_t *(*fill_cache)(inode_cache_t *cache, off_t pgoff);
137
138 bool (*page_write_begin)(inode_cache_t *cache, off_t file_offset, size_t inpage_size, phyframe_t **page_out, void **data);
139 void (*page_write_end)(inode_cache_t *cache, off_t file_offset, size_t inpage_size, phyframe_t *page, void *data);
140
144 long (*flush_page)(inode_cache_t *cache, off_t pgoff, phyframe_t *page);
146
147typedef struct _inode_cache
148{
151 hashmap_t pages; // page index -> phyframe_t *
154
155typedef struct _inode
156{
160 size_t size;
163 bool sticky;
164 bool suid;
165 bool sgid;
166 ssize_t nlinks; // number of hard links to this inode
170
171 superblock_t *superblock; // superblock of this inode
172 const inode_ops_t *ops; // operations on this inode
173 const file_ops_t *file_ops; // operations on files of this inode
174 void *private_data; // private data
175 inode_cache_t cache; // page cache for this inode
176
177 atomic_t refcount;
178} inode_t;
179
180typedef struct _filesystem
181{
183 const char *name;
184 dentry_t *(*mount)(filesystem_t *fs, const char *dev_name, const char *mount_options);
185 void (*unmount)(filesystem_t *fs, dentry_t *mountpoint); // called when the mountpoint is unmounted
187
188typedef struct _mount
189{
191 dentry_t *root; // root of the mounted tree
192 dentry_t *mountpoint; // where the tree is mounted
195} mount_t;
196
197typedef struct _file
198{
199 io_t io; // refcount is tracked by the io_t
201 spinlock_t offset_lock; // protects the offset field
202 size_t offset; // tracks the current position in the file
204} file_t;
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.h:23
dentry_t * root_dentry
Definition vfs.c:36
io_seek_whence_t
Definition io_types.h:6
bool munmap(ptr_t addr, size_t size)
Unmap a page from the current process's address space.
Definition mmap.c:111
futex_word_t mutex_t
Definition mutex.h:8
int bool
Definition pb_syshdr.h:57
uint32_t size_t
Definition pb_syshdr.h:42
size_t size
Definition slab.c:30
const char * name
Definition slab.c:31
superblock_t * superblock
Definition vfs_types.h:119
bool is_mountpoint
Definition vfs_types.h:120
spinlock_t lock
Definition vfs_types.h:115
const char * name
Definition vfs_types.h:118
atomic_t refcount
Definition vfs_types.h:116
inode_t * inode
Definition vfs_types.h:117
spinlock_t offset_lock
Definition vfs_types.h:201
io_t io
Definition vfs_types.h:199
size_t offset
Definition vfs_types.h:202
void * private_data
Definition vfs_types.h:203
dentry_t * dentry
Definition vfs_types.h:200
const char * name
Definition vfs_types.h:183
inode_t * owner
Definition vfs_types.h:150
const inode_cache_ops_t * ops
Definition vfs_types.h:152
hashmap_t pages
Definition vfs_types.h:151
mutex_t lock
Definition vfs_types.h:149
superblock_t * superblock
Definition vfs_types.h:171
uid_t uid
Definition vfs_types.h:161
u64 modified
Definition vfs_types.h:169
u64 accessed
Definition vfs_types.h:167
u64 created
Definition vfs_types.h:168
file_perm_t perm
Definition vfs_types.h:159
const inode_ops_t * ops
Definition vfs_types.h:172
bool sgid
Definition vfs_types.h:165
size_t size
Definition vfs_types.h:160
file_type_t type
Definition vfs_types.h:158
const file_ops_t * file_ops
Definition vfs_types.h:173
atomic_t refcount
number of references to this inode
Definition vfs_types.h:177
bool suid
Definition vfs_types.h:164
inode_cache_t cache
Definition vfs_types.h:175
bool sticky
Definition vfs_types.h:163
gid_t gid
Definition vfs_types.h:162
u64 ino
Definition vfs_types.h:157
void * private_data
Definition vfs_types.h:174
ssize_t nlinks
Definition vfs_types.h:166
Definition io.h:46
superblock_t * superblock
Definition vfs_types.h:193
dentry_t * root
Definition vfs_types.h:191
dentry_t * mountpoint
Definition vfs_types.h:192
filesystem_t * fs
Definition vfs_types.h:194
Definition slab.h:45
dentry_t * root
Definition vfs_types.h:107
const superblock_ops_t * ops
Definition vfs_types.h:109
filesystem_t * fs
Definition vfs_types.h:108
Definition vfs_types.h:45
as_linked_list
Definition vfs_types.h:46
size_t name_len
Definition vfs_types.h:49
file_type_t type
Definition vfs_types.h:50
const char * name
Definition vfs_types.h:48
ino_t ino
Definition vfs_types.h:47
size_t read_offset
user has read up to this offset, start from this offset when reading more entries
Definition vfs_types.h:57
size_t n_count
number of entries in the list
Definition vfs_types.h:56
list_head entries
Definition vfs_types.h:55
Definition mm.h:58
static char buffer[2048]
Definition test_printf.c:7
ssize_t off_t
Definition types.h:84
u32 uid_t
Definition types.h:75
u32 gid_t
Definition types.h:76
unsigned long long u64
Definition types.h:23
signed long ssize_t
Definition types.h:83
struct _file file_t
Definition vfs_types.h:42
struct _filesystem filesystem_t
Definition vfs_types.h:41
struct _inode inode_t
Definition vfs_types.h:38
slab_t * superblock_cache
Definition vfs.c:38
struct _dentry dentry_t
Definition vfs_types.h:36
void dentry_iterator_op(vfs_listdir_state_t *state, u64 ino, const char *name, size_t name_len, file_type_t type)
Definition vfs_types.h:60
struct _mount mount_t
Definition vfs_types.h:39
slab_t * file_cache
Definition vfs_types.h:206
slab_t * mount_cache
Definition vfs_types.h:206
struct _superblock superblock_t
Definition vfs_types.h:40
struct _inode_cache inode_cache_t
Definition vfs_types.h:37