PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
9p.c
Go to the documentation of this file.
3
4#include <errno.h>
6#include <stdlib.h>
7#include <string.h>
8
9/**
10 * @brief 9P Filesystems.
11 * @defgroup kernel_fs_9p 9P Filesystem
12 * @ingroup kernel_fs
13 *
14 * This module provides an implementation of the 9P filesystem protocol where the kernel acts as a client to a 9P
15 * server, allowing the 9P server to be mounted as a filesystem within the kernel's VFS.
16 *
17 * The 9p filesystem supports the following options:
18 * - `in`: The file descriptor to read 9P messages from.
19 * - `out`: The file descriptor to write 9P messages to.
20 * - `version`: The 9P protocol version to use, currently only `9P2000` is supported, the default is `9P2000`.
21 *
22 * @see libstd_sys_9p for the 9P protocol definitions.
23 * @see http://rfc.nop.hu/plan9/rfc9p.pdf for the 9P protocol specification.
24 *
25 * @{
26 */
27
28typedef struct
29{
32} ninep_t;
33
35{
36 ninep_t* ninep = sb->data;
37 if (ninep == NULL)
38 {
39 return;
40 }
41
42 UNREF(ninep->in);
43 UNREF(ninep->out);
44 free(ninep);
45 sb->data = NULL;
46}
47
51
52static dentry_t* ninep_mount(filesystem_t* fs, const char* options, void* data)
53{
54 UNUSED(data);
55
56 if (options == NULL)
57 {
58 errno = EINVAL;
59 return NULL;
60 }
61
62 fd_t in = ERR;
63 fd_t out = ERR;
64 const char* version = "9P2000";
65
66 char* key;
67 char* value;
68 OPTIONS_FOR_EACH(options, key, value)
69 {
70 if (strcmp(key, "in") == 0)
71 {
72 in = atoi(value);
73 }
74 else if (strcmp(key, "out") == 0)
75 {
76 out = atoi(value);
77 }
78 else if (strcmp(key, "version") == 0)
79 {
80 version = value;
81 }
82 else
83 {
84 errno = EINVAL;
85 return NULL;
86 }
87 }
88
89 if (in == ERR || out == ERR)
90 {
91 errno = EINVAL;
92 return NULL;
93 }
94
95 if (strcmp(version, "9P2000") != 0)
96 {
97 errno = ENOSYS;
98 return NULL;
99 }
100
101 superblock_t* superblock = superblock_new(fs, &superOps, NULL);
102 if (superblock == NULL)
103 {
104 return NULL;
105 }
106 UNREF_DEFER(superblock);
107
108 ninep_t* ninep = malloc(sizeof(ninep_t));
109 if (ninep == NULL)
110 {
111 errno = ENOMEM;
112 return NULL;
113 }
114
115 process_t* process = process_current();
116 assert(process != NULL);
117
118 ninep->in = file_table_get(&process->fileTable, in);
119 if (ninep->in == NULL)
120 {
121 free(ninep);
122 return NULL;
123 }
124 ninep->out = file_table_get(&process->fileTable, out);
125 if (ninep->out == NULL)
126 {
127 UNREF(ninep->in);
128 return NULL;
129 }
130
131 superblock->data = ninep;
132
133 vnode_t* vnode = vnode_new(superblock, VDIR, NULL, NULL);
134 if (vnode == NULL)
135 {
136 return NULL;
137 }
138 UNREF_DEFER(vnode);
139
140 dentry_t* dentry = dentry_new(superblock, NULL, NULL);
141 if (dentry == NULL)
142 {
143 return NULL;
144 }
145
146 dentry_make_positive(dentry, vnode);
147
148 superblock->root = dentry;
149 return superblock->root;
150}
151
153 .name = "9p",
154 .mount = ninep_mount,
155};
156
157/** @} */
158
160{
161 switch (event->type)
162 {
165 {
166 return ERR;
167 }
168 break;
171 break;
172 default:
173 break;
174 }
175 return 0;
176}
177
178MODULE_INFO("9P Filesystem", "Kai Norberg", "A implementation of the 9P filesystem", OS_VERSION, "MIT", "BOOT_ALWAYS");
uint64_t _module_procedure(const module_event_t *event)
Definition 9p.c:159
#define assert(expression)
Definition assert.h:29
static fd_t data
Definition dwm.c:21
static void ninep_super_cleanup(superblock_t *sb)
Definition 9p.c:34
static dentry_t * ninep_mount(filesystem_t *fs, const char *options, void *data)
Definition 9p.c:52
static superblock_ops_t superOps
Definition 9p.c:48
static filesystem_t ninep
Definition 9p.c:152
dentry_t * dentry_new(superblock_t *superblock, dentry_t *parent, const char *name)
Create a new dentry.
Definition dentry.c:134
void dentry_make_positive(dentry_t *dentry, vnode_t *vnode)
Make a dentry positive by associating it with an vnode.
Definition dentry.c:289
file_t * file_table_get(file_table_t *table, fd_t fd)
Get a file from its file descriptor.
Definition file_table.c:33
uint64_t filesystem_register(filesystem_t *fs)
Registers a filesystem.
Definition filesystem.c:233
#define OPTIONS_FOR_EACH(options, key, value)
Helper macro for iterating over options passed to a filesystem mount operation.
Definition filesystem.h:140
void filesystem_unregister(filesystem_t *fs)
Unregisters a filesystem.
Definition filesystem.c:259
superblock_t * superblock_new(filesystem_t *fs, const superblock_ops_t *ops, const dentry_ops_t *dentryOps)
Create a new superblock.
Definition superblock.c:33
vnode_t * vnode_new(superblock_t *superblock, vtype_t type, const vnode_ops_t *ops, const file_ops_t *fileOps)
Create a new vnode.
Definition vnode.c:51
#define MODULE_INFO(_name, _author, _description, _version, _licence, _deviceTypes)
Macro to define module information.
Definition module.h:200
@ MODULE_EVENT_LOAD
Definition module.h:239
@ MODULE_EVENT_UNLOAD
Definition module.h:245
static process_t * process_current(void)
Retrieves the process of the currently running thread.
Definition process.h:131
#define UNREF_DEFER(ptr)
RAII-style cleanup for scoped references.
Definition ref.h:122
#define UNREF(ptr)
Decrement reference count.
Definition ref.h:109
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOSYS
Function not implemented.
Definition errno.h:222
#define ENOMEM
Out of memory.
Definition errno.h:92
#define errno
Error number variable.
Definition errno.h:27
#define UNUSED(x)
Mark a variable as unused.
Definition defs.h:96
@ VDIR
Is a directory.
Definition fs.h:344
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
File descriptor type.
Definition fd_t.h:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define atoi(nptr)
Definition stdlib.h:34
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
Directory entry structure.
Definition dentry.h:155
File structure.
Definition file.h:39
Filesystem structure, represents a filesystem type, e.g. fat32, tmpfs, devfs, etc.
Definition filesystem.h:53
const char * name
Definition filesystem.h:58
module_event_type_t type
Definition module.h:268
Definition 9p.c:29
file_t * out
Definition 9p.c:31
file_t * in
Definition 9p.c:30
Process structure.
Definition process.h:76
file_table_t fileTable
Definition process.h:88
Superblock operations structure.
Definition superblock.h:59
void(* cleanup)(superblock_t *superblock)
Definition superblock.h:64
Superblock structure.
Definition superblock.h:33
dentry_t * root
Root dentry of the filesystem, should not take a reference.
Definition superblock.h:40
void * data
Definition superblock.h:39
vnode structure.
Definition vnode.h:48