AquilaOS documentation
How it works, and its code
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
18MALLOC_DEFINE(M_VNODE, "vnode", "vnode structure");
19MALLOC_DEFINE(M_FS_LIST, "fs-list", "filesystems list");
20
22LOGGER_DEFINE(vfs, vfs_log, vfs_log_level)
23
24
26
27/* Mounts */
28
30
38{
40 return 0;
41}
42
53int 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
74void vfs_init(void)
75{
76 vfs_log(LOG_INFO, "initializing\n");
77}
78
85int 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
114int 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
139int 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}
static int shift
Definition keyboard.c:101
#define MALLOC_DEFINE(type, _name, _desc)
Definition kvmem.h:30
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
#define LOG_NONE
Definition printk.h:11
#define LOGGER_DEFINE(module, name, _level)
Definition printk.h:28
#define LOG_INFO
Definition printk.h:18
#define NULL
Definition stddef.h:27
An open file (POSIX's "open file description", fs/file.c).
Definition vfs.h:157
int flags
Definition vfs.h:164
The list of registered filesystem types.
Definition vfs.h:187
struct fs_list * next
the next entry of the list
Definition vfs.h:195
const char * name
the type's name
Definition vfs.h:189
struct fs * fs
the type
Definition vfs.h:192
A filesystem type.
Definition vfs.h:98
char * name
Definition vfs.h:99
Who does a filesystem operation: where its paths start, and its credentials and umask.
Definition vfs.h:60
gid_t gid
Definition vfs.h:64
uid_t uid
Definition vfs.h:63
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
struct vnode * covered
Definition vfs.h:149
size_t ref
how many references there are to it
Definition vfs.h:133
gid_t gid
Definition vfs.h:120
mode_t mode
Definition vfs.h:118
uid_t uid
Definition vfs.h:119
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
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
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
struct vnode * vfs_root
Definition vfs.c:29
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
int vfs_mount_root(struct vnode *vnode)
Makes a vnode the root of the filesystem tree.
Definition vfs.c:37
struct fs_list * registered_fs
The installed filesystems.
Definition vfs.c:25
static int vfs_log_level
Definition vfs.c:21
int vfs_install(struct fs *fs)
Installs a filesystem, so that it can be mounted by name.
Definition vfs.c:85
void vfs_init(void)
Sets up the VFS at boot.
Definition vfs.c:74
#define VFS_W
Definition vfs.h:296
#define VFS_X
Definition vfs.h:297
#define VFS_R
Definition vfs.h:295