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
5
#include "
mos/lib/sync/mutex.h
"
6
#include "
mos/mm/mm.h
"
7
#include "
mos/mm/physical/pmm.h
"
8
#include "
mos/mm/slab.h
"
9
10
#include <abi-bits/stat.h>
11
#include <
mos/filesystem/fs_types.h
>
12
#include <
mos/io/io.h
>
13
#include <
mos/io/io_types.h
>
14
#include <
mos/lib/structures/hashmap.h
>
15
#include <
mos/lib/structures/list.h
>
16
#include <
mos/lib/structures/tree.h
>
17
#include <
mos/lib/sync/spinlock.h
>
18
#include <
mos/platform/platform.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
36
typedef
struct
_dentry
dentry_t
;
37
typedef
struct
_inode_cache
inode_cache_t
;
38
typedef
struct
_inode
inode_t
;
39
typedef
struct
_mount
mount_t
;
40
typedef
struct
_superblock
superblock_t
;
41
typedef
struct
_filesystem
filesystem_t
;
42
typedef
struct
_file
file_t
;
43
44
typedef
struct
45
{
46
as_linked_list
;
47
ino_t
ino
;
48
const
char
*
name
;
49
size_t
name_len
;
50
file_type_t
type
;
51
}
vfs_listdir_entry_t
;
52
53
typedef
struct
54
{
55
list_head
entries
;
56
size_t
n_count
;
57
size_t
read_offset
;
58
}
vfs_listdir_state_t
;
59
60
typedef
void
(
dentry_iterator_op
)(
vfs_listdir_state_t
*state,
u64
ino,
const
char
*
name
,
size_t
name_len,
file_type_t
type);
61
62
typedef
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);
86
}
inode_ops_t
;
87
88
typedef
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);
97
}
file_ops_t
;
98
99
typedef
struct
100
{
101
bool
(*drop_inode)(
inode_t
*inode);
102
long (*sync_inode)(
inode_t
*inode);
103
}
superblock_ops_t
;
104
105
typedef
struct
_superblock
106
{
107
dentry_t
*
root
;
108
filesystem_t
*
fs
;
109
const
superblock_ops_t
*
ops
;
110
}
superblock_t
;
111
112
typedef
struct
_dentry
113
{
114
as_tree
;
115
spinlock_t
lock
;
116
atomic_t
refcount
;
117
inode_t
*
inode
;
118
const
char
*
name
;
// for a mounted root, this is NULL
119
superblock_t
*
superblock
;
// The superblock of the dentry
120
bool
is_mountpoint
;
121
}
dentry_t
;
122
123
extern
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
131
typedef
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);
145
}
inode_cache_ops_t
;
146
147
typedef
struct
_inode_cache
148
{
149
mutex_t
lock
;
150
inode_t
*
owner
;
151
hashmap_t
pages
;
// page index -> phyframe_t *
152
const
inode_cache_ops_t
*
ops
;
153
}
inode_cache_t
;
154
155
typedef
struct
_inode
156
{
157
u64
ino
;
158
file_type_t
type
;
159
file_perm_t
perm
;
160
size_t
size
;
161
uid_t
uid
;
162
gid_t
gid
;
163
bool
sticky
;
164
bool
suid
;
165
bool
sgid
;
166
ssize_t
nlinks
;
// number of hard links to this inode
167
u64
accessed
;
168
u64
created
;
169
u64
modified
;
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
180
typedef
struct
_filesystem
181
{
182
as_linked_list
;
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
186
}
filesystem_t
;
187
188
typedef
struct
_mount
189
{
190
as_linked_list
;
191
dentry_t
*
root
;
// root of the mounted tree
192
dentry_t
*
mountpoint
;
// where the tree is mounted
193
superblock_t
*
superblock
;
194
filesystem_t
*
fs
;
195
}
mount_t
;
196
197
typedef
struct
_file
198
{
199
io_t
io
;
// refcount is tracked by the io_t
200
dentry_t
*
dentry
;
201
spinlock_t
offset_lock
;
// protects the offset field
202
size_t
offset
;
// tracks the current position in the file
203
void
*
private_data
;
204
}
file_t
;
205
206
extern
slab_t
*
superblock_cache
, *
mount_cache
, *
file_cache
;
fs_types.h
file_perm_t
u16 file_perm_t
Definition
fs_types.h:52
file_type_t
file_type_t
Definition
fs_types.h:14
void
MOSAPI void(1, 2) fatal_abort(const char *fmt
list_head
list_node_t list_head
A linked list head.
Definition
list.h:23
root_dentry
dentry_t * root_dentry
Definition
vfs.c:36
hashmap.h
io.h
io_types.h
io_seek_whence_t
io_seek_whence_t
Definition
io_types.h:6
list.h
mm.h
munmap
bool munmap(ptr_t addr, size_t size)
Unmap a page from the current process's address space.
Definition
mmap.c:111
mutex.h
mutex_t
futex_word_t mutex_t
Definition
mutex.h:8
bool
int bool
Definition
pb_syshdr.h:57
size_t
uint32_t size_t
Definition
pb_syshdr.h:42
platform.h
pmm.h
size
size_t size
Definition
slab.c:30
name
const char * name
Definition
slab.c:31
slab.h
spinlock.h
dentry_t
Definition
vfs_types.h:113
dentry_t::superblock
superblock_t * superblock
Definition
vfs_types.h:119
dentry_t::is_mountpoint
bool is_mountpoint
Definition
vfs_types.h:120
dentry_t::lock
spinlock_t lock
Definition
vfs_types.h:115
dentry_t::as_tree
as_tree
Definition
vfs_types.h:114
dentry_t::name
const char * name
Definition
vfs_types.h:118
dentry_t::refcount
atomic_t refcount
Definition
vfs_types.h:116
dentry_t::inode
inode_t * inode
Definition
vfs_types.h:117
file_ops_t
Definition
vfs_types.h:89
file_t
Definition
vfs_types.h:198
file_t::offset_lock
spinlock_t offset_lock
Definition
vfs_types.h:201
file_t::io
io_t io
Definition
vfs_types.h:199
file_t::offset
size_t offset
Definition
vfs_types.h:202
file_t::private_data
void * private_data
Definition
vfs_types.h:203
file_t::dentry
dentry_t * dentry
Definition
vfs_types.h:200
filesystem_t
Definition
vfs_types.h:181
filesystem_t::as_linked_list
as_linked_list
Definition
vfs_types.h:182
filesystem_t::name
const char * name
Definition
vfs_types.h:183
hashmap_t
Definition
hashmap.h:23
inode_cache_ops_t
Definition
vfs_types.h:132
inode_cache_t
Definition
vfs_types.h:148
inode_cache_t::owner
inode_t * owner
Definition
vfs_types.h:150
inode_cache_t::ops
const inode_cache_ops_t * ops
Definition
vfs_types.h:152
inode_cache_t::pages
hashmap_t pages
Definition
vfs_types.h:151
inode_cache_t::lock
mutex_t lock
Definition
vfs_types.h:149
inode_ops_t
Definition
vfs_types.h:63
inode_t
Definition
vfs_types.h:156
inode_t::superblock
superblock_t * superblock
Definition
vfs_types.h:171
inode_t::uid
uid_t uid
Definition
vfs_types.h:161
inode_t::modified
u64 modified
Definition
vfs_types.h:169
inode_t::accessed
u64 accessed
Definition
vfs_types.h:167
inode_t::created
u64 created
Definition
vfs_types.h:168
inode_t::perm
file_perm_t perm
Definition
vfs_types.h:159
inode_t::ops
const inode_ops_t * ops
Definition
vfs_types.h:172
inode_t::sgid
bool sgid
Definition
vfs_types.h:165
inode_t::size
size_t size
Definition
vfs_types.h:160
inode_t::type
file_type_t type
Definition
vfs_types.h:158
inode_t::file_ops
const file_ops_t * file_ops
Definition
vfs_types.h:173
inode_t::refcount
atomic_t refcount
number of references to this inode
Definition
vfs_types.h:177
inode_t::suid
bool suid
Definition
vfs_types.h:164
inode_t::cache
inode_cache_t cache
Definition
vfs_types.h:175
inode_t::sticky
bool sticky
Definition
vfs_types.h:163
inode_t::gid
gid_t gid
Definition
vfs_types.h:162
inode_t::ino
u64 ino
Definition
vfs_types.h:157
inode_t::private_data
void * private_data
Definition
vfs_types.h:174
inode_t::nlinks
ssize_t nlinks
Definition
vfs_types.h:166
io_t
Definition
io.h:46
mount_t
Definition
vfs_types.h:189
mount_t::superblock
superblock_t * superblock
Definition
vfs_types.h:193
mount_t::as_linked_list
as_linked_list
Definition
vfs_types.h:190
mount_t::root
dentry_t * root
Definition
vfs_types.h:191
mount_t::mountpoint
dentry_t * mountpoint
Definition
vfs_types.h:192
mount_t::fs
filesystem_t * fs
Definition
vfs_types.h:194
phyframe_t
Definition
pmm.h:27
slab_t
Definition
slab.h:45
spinlock_t
Definition
spinlock.h:13
superblock_ops_t
Definition
vfs_types.h:100
superblock_t
Definition
vfs_types.h:106
superblock_t::root
dentry_t * root
Definition
vfs_types.h:107
superblock_t::ops
const superblock_ops_t * ops
Definition
vfs_types.h:109
superblock_t::fs
filesystem_t * fs
Definition
vfs_types.h:108
vfs_listdir_entry_t
Definition
vfs_types.h:45
vfs_listdir_entry_t::as_linked_list
as_linked_list
Definition
vfs_types.h:46
vfs_listdir_entry_t::name_len
size_t name_len
Definition
vfs_types.h:49
vfs_listdir_entry_t::type
file_type_t type
Definition
vfs_types.h:50
vfs_listdir_entry_t::name
const char * name
Definition
vfs_types.h:48
vfs_listdir_entry_t::ino
ino_t ino
Definition
vfs_types.h:47
vfs_listdir_state_t
Definition
vfs_types.h:54
vfs_listdir_state_t::read_offset
size_t read_offset
user has read up to this offset, start from this offset when reading more entries
Definition
vfs_types.h:57
vfs_listdir_state_t::n_count
size_t n_count
number of entries in the list
Definition
vfs_types.h:56
vfs_listdir_state_t::entries
list_head entries
Definition
vfs_types.h:55
vmap_t
Definition
mm.h:58
buffer
static char buffer[2048]
Definition
test_printf.c:7
tree.h
types.h
off_t
ssize_t off_t
Definition
types.h:84
uid_t
u32 uid_t
Definition
types.h:75
gid_t
u32 gid_t
Definition
types.h:76
u64
unsigned long long u64
Definition
types.h:23
ssize_t
signed long ssize_t
Definition
types.h:83
file_t
struct _file file_t
Definition
vfs_types.h:42
filesystem_t
struct _filesystem filesystem_t
Definition
vfs_types.h:41
inode_t
struct _inode inode_t
Definition
vfs_types.h:38
superblock_cache
slab_t * superblock_cache
Definition
vfs.c:38
dentry_t
struct _dentry dentry_t
Definition
vfs_types.h:36
dentry_iterator_op
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
mount_t
struct _mount mount_t
Definition
vfs_types.h:39
file_cache
slab_t * file_cache
Definition
vfs_types.h:206
mount_cache
slab_t * mount_cache
Definition
vfs_types.h:206
superblock_t
struct _superblock superblock_t
Definition
vfs_types.h:40
inode_cache_t
struct _inode_cache inode_cache_t
Definition
vfs_types.h:37
kernel
include
private
mos
filesystem
vfs_types.h
Generated on Sun Sep 1 2024 18:22:52 for MOS Source Code by
1.12.0