AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
vfs.c
Go to the documentation of this file.
1
6
7
#include <
core/system.h
>
8
#include <
core/string.h
>
9
#include <
mm/mm.h
>
10
#include <
fs/vfs.h
>
11
#include <
sys/proc.h
>
12
#include <
sys/sched.h
>
13
#include <aquila/fcntl.h>
14
#include <aquila/errno.h>
15
#include <
dev/dev.h
>
16
#include <
net/socket.h
>
17
18
MALLOC_DEFINE
(M_VNODE,
"vnode"
,
"vnode structure"
);
19
MALLOC_DEFINE
(M_FS_LIST,
"fs-list"
,
"filesystems list"
);
20
21
static
int
vfs_log_level
=
LOG_NONE
;
22
LOGGER_DEFINE
(vfs, vfs_log,
vfs_log_level
)
23
24
25
struct
fs_list
*
registered_fs
=
NULL
;
26
27
/* Mounts */
28
29
struct
vnode
*
vfs_root
=
NULL
;
30
37
int
vfs_mount_root
(struct
vnode
*
vnode
)
38
{
39
vfs_root
=
vnode
;
40
return
0;
41
}
42
53
int
vfs_bind
(
struct
vnode
*dir,
struct
vnode
*root)
54
{
55
if
(!dir || !root)
56
return
-EINVAL;
57
58
if
(dir ==
vfs_root
)
59
return
vfs_mount_root
(root);
60
61
if
(dir->
mounted
|| root->
covered
)
62
return
-EBUSY;
63
64
dir->
ref
++;
65
dir->
mounted
= root;
66
root->
covered
= dir;
67
68
return
0;
69
}
70
74
void
vfs_init
(
void
)
75
{
76
vfs_log(
LOG_INFO
,
"initializing\n"
);
77
}
78
85
int
vfs_install
(
struct
fs
*
fs
)
86
{
87
struct
fs_list
*node =
kmalloc
(
sizeof
(
struct
fs_list
), &M_FS_LIST, 0);
88
if
(!node)
return
-ENOMEM;
89
90
node->
name
=
fs
->
name
;
91
node->
fs
=
fs
;
92
93
node->
next
=
registered_fs
;
94
registered_fs
= node;
95
96
vfs_log(
LOG_INFO
,
"registered filesystem %s\n"
,
fs
->
name
);
97
98
return
0;
99
}
100
101
/* Permissions */
102
114
int
vfs_access
(
struct
vnode
*
vnode
,
struct
uio
*
uio
,
int
mode)
115
{
116
/* root may do anything, but only execute files that have an execute bit */
117
if
(
uio
->
uid
== 0) {
118
if
((mode &
VFS_X
) && !S_ISDIR(
vnode
->
mode
) && !(
vnode
->
mode
& 0111))
119
return
-EACCES;
120
121
return
0;
122
}
123
124
/* the owner's permissions apply to the owner, then the group's */
125
int
shift
=
vnode
->
uid
==
uio
->
uid
? 6 :
vnode
->
gid
==
uio
->
gid
? 3 : 0;
126
int
allowed = (
vnode
->
mode
>>
shift
) & 7;
127
128
return
(mode & ~allowed) ? -EACCES : 0;
129
}
130
139
int
vfs_perms_check
(
struct
file
*
file
,
struct
uio
*
uio
)
140
{
141
int
mode = 0;
142
int
accmode =
file
->
flags
& O_ACCMODE;
143
144
if
(accmode == O_RDONLY || accmode == O_RDWR)
145
mode |=
VFS_R
;
146
147
if
(
file
->
flags
& (O_WRONLY | O_RDWR))
148
mode |=
VFS_W
;
149
150
if
(
file
->
flags
& O_EXEC)
151
mode |=
VFS_X
;
152
153
return
vfs_access
(
file
->vnode,
uio
, mode);
154
}
dev.h
system.h
mm.h
shift
static int shift
Definition
keyboard.c:101
MALLOC_DEFINE
#define MALLOC_DEFINE(type, _name, _desc)
Definition
kvmem.h:30
kmalloc
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition
kvmem.c:123
LOG_NONE
#define LOG_NONE
Definition
printk.h:11
LOGGER_DEFINE
#define LOGGER_DEFINE(module, name, _level)
Definition
printk.h:28
LOG_INFO
#define LOG_INFO
Definition
printk.h:18
proc.h
sched.h
socket.h
NULL
#define NULL
Definition
stddef.h:27
string.h
file
An open file (POSIX's "open file description", fs/file.c).
Definition
vfs.h:157
file::flags
int flags
Definition
vfs.h:164
fs_list
The list of registered filesystem types.
Definition
vfs.h:187
fs_list::next
struct fs_list * next
the next entry of the list
Definition
vfs.h:195
fs_list::name
const char * name
the type's name
Definition
vfs.h:189
fs_list::fs
struct fs * fs
the type
Definition
vfs.h:192
fs
A filesystem type.
Definition
vfs.h:98
fs::name
char * name
Definition
vfs.h:99
uio
Who does a filesystem operation: where its paths start, and its credentials and umask.
Definition
vfs.h:60
uio::gid
gid_t gid
Definition
vfs.h:64
uio::uid
uid_t uid
Definition
vfs.h:63
vnode
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition
vfs.h:113
vnode::covered
struct vnode * covered
Definition
vfs.h:149
vnode::ref
size_t ref
how many references there are to it
Definition
vfs.h:133
vnode::gid
gid_t gid
Definition
vfs.h:120
vnode::mode
mode_t mode
Definition
vfs.h:118
vnode::uid
uid_t uid
Definition
vfs.h:119
vnode::mounted
struct vnode * mounted
for a directory that a filesystem is mounted on, that filesystem's root; for a mounted filesystem's r...
Definition
vfs.h:148
vfs_bind
int vfs_bind(struct vnode *dir, struct vnode *root)
Mounts a filesystem on a directory: a lookup that reaches dir goes on in root instead.
Definition
vfs.c:53
vfs_access
int vfs_access(struct vnode *vnode, struct uio *uio, int mode)
Checks whether a user may access a vnode in a way: the owner's permissions apply to its owner,...
Definition
vfs.c:114
vfs_root
struct vnode * vfs_root
Definition
vfs.c:29
vfs_perms_check
int vfs_perms_check(struct file *file, struct uio *uio)
Checks whether a user may open a file the way it's being opened (read, write, execute,...
Definition
vfs.c:139
vfs_mount_root
int vfs_mount_root(struct vnode *vnode)
Makes a vnode the root of the filesystem tree.
Definition
vfs.c:37
registered_fs
struct fs_list * registered_fs
The installed filesystems.
Definition
vfs.c:25
vfs_log_level
static int vfs_log_level
Definition
vfs.c:21
vfs_install
int vfs_install(struct fs *fs)
Installs a filesystem, so that it can be mounted by name.
Definition
vfs.c:85
vfs_init
void vfs_init(void)
Sets up the VFS at boot.
Definition
vfs.c:74
vfs.h
VFS_W
#define VFS_W
Definition
vfs.h:296
VFS_X
#define VFS_X
Definition
vfs.h:297
VFS_R
#define VFS_R
Definition
vfs.h:295
kernel
fs
vfs.c
Generated by
1.18.0