1// SPDX-License-Identifier: GPL-3.0-only
2
3#define pr_fmt(fmt) "sysfs: " fmt
4
5#include "mos/filesystem/sysfs/sysfs.h"
6
7#include "mos/filesystem/dentry.h"
8#include "mos/filesystem/vfs.h"
9#include "mos/filesystem/vfs_types.h"
10#include "mos/filesystem/vfs_utils.h"
11#include "mos/misc/setup.h"
12#include "mos/mm/mm.h"
13#include "mos/mm/physical/pmm.h"
14#include "mos/syslog/printk.h"
15
16#include <mos/filesystem/fs_types.h>
17#include <mos/io/io_types.h>
18#include <mos/lib/structures/list.h>
19#include <mos/types.h>
20#include <mos_stdio.h>
21#include <mos_stdlib.h>
22#include <mos_string.h>
23
24typedef struct _sysfs_file
25{
26 sysfs_item_t *item;
27
28 phyframe_t *buf_page;
29 ssize_t buf_head_offset;
30 ssize_t buf_npages;
31
32 void *data;
33} sysfs_file_t;
34
35static list_head sysfs_dirs = LIST_HEAD_INIT(sysfs_dirs);
36static filesystem_t fs_sysfs;
37static superblock_t *sysfs_sb = NULL;
38static const file_ops_t sysfs_dir_file_ops = { 0 };
39
40static void sysfs_do_register(sysfs_dir_t *sysfs_dir);
41
42static u64 sysfs_get_ino(void)
43{
44 static u64 ino = 1;
45 return ino++;
46}
47
48void sysfs_register(sysfs_dir_t *dir)
49{
50 linked_list_init(list_node(dir));
51 list_node_append(head: &sysfs_dirs, list_node(dir));
52 pr_dinfo2(sysfs, "registering '%s'", dir->name);
53 MOS_ASSERT(sysfs_sb);
54 sysfs_do_register(sysfs_dir: dir);
55}
56
57static void sysfs_expand_buffer(sysfs_file_t *buffer, size_t new_npages)
58{
59 phyframe_t *oldbuf_page = buffer->buf_page;
60 const size_t oldbuf_npages = buffer->buf_npages;
61
62 // We need to allocate more pages
63 buffer->buf_npages = new_npages;
64 buffer->buf_page = mm_get_free_pages(npages: buffer->buf_npages);
65
66 if (oldbuf_page)
67 {
68 memcpy(dest: (char *) phyframe_va(buffer->buf_page), src: (char *) phyframe_va(oldbuf_page), n: oldbuf_npages * MOS_PAGE_SIZE);
69 mm_free_pages(oldbuf_page, oldbuf_npages);
70 }
71}
72
73ssize_t sysfs_printf(sysfs_file_t *file, const char *fmt, ...)
74{
75retry_printf:;
76 const size_t spaces_left = file->buf_npages * MOS_PAGE_SIZE - file->buf_head_offset;
77
78 va_list args;
79 va_start(args, fmt);
80 const size_t should_write = vsnprintf(buf: (char *) phyframe_va(file->buf_page) + file->buf_head_offset, size: spaces_left, format: fmt, args);
81 va_end(args);
82
83 if (should_write >= spaces_left)
84 {
85 sysfs_expand_buffer(buffer: file, new_npages: file->buf_npages + 1);
86 goto retry_printf;
87 }
88
89 file->buf_head_offset += should_write;
90 return should_write;
91}
92
93ssize_t sysfs_put_data(sysfs_file_t *file, const void *data, size_t count)
94{
95 const size_t spaces_left = file->buf_npages * MOS_PAGE_SIZE - file->buf_head_offset;
96
97 if (count > spaces_left)
98 sysfs_expand_buffer(buffer: file, new_npages: file->buf_npages + (count - spaces_left) / MOS_PAGE_SIZE + 1);
99
100 memcpy(dest: (char *) phyframe_va(file->buf_page) + file->buf_head_offset, src: data, n: count);
101 file->buf_head_offset += count;
102 return count;
103}
104
105void sysfs_file_set_data(sysfs_file_t *file, void *data)
106{
107 file->data = data;
108}
109
110void *sysfs_file_get_data(sysfs_file_t *file)
111{
112 return file->data;
113}
114
115sysfs_item_t *sysfs_file_get_item(sysfs_file_t *file)
116{
117 return file->item;
118}
119
120static bool sysfs_fops_open(inode_t *inode, file_t *file, bool created)
121{
122 MOS_UNUSED(created);
123 sysfs_file_t *sysfs_file = kmalloc(sizeof(sysfs_file_t));
124 sysfs_file->item = inode->private_data;
125
126 file->private_data = sysfs_file;
127 return true;
128}
129
130static void sysfs_fops_release(file_t *file)
131{
132 pr_dinfo2(sysfs, "closing %s in %s", file->dentry->name, dentry_parent(file->dentry)->name);
133 sysfs_file_t *f = file->private_data;
134 if (f->buf_page)
135 mm_free_pages(f->buf_page, f->buf_npages), f->buf_page = NULL, f->buf_npages = 0, f->buf_head_offset = 0;
136 kfree(ptr: f);
137}
138
139__nodiscard static bool sysfs_file_ensure_ready(const file_t *file)
140{
141 sysfs_file_t *f = file->private_data;
142 if (f->buf_head_offset == 0)
143 {
144 if (!f->item->show(f))
145 return false;
146 }
147
148 return true;
149}
150
151static ssize_t sysfs_fops_read(const file_t *file, void *buf, size_t size, off_t offset)
152{
153 sysfs_file_t *f = file->private_data;
154 if (f->item->type != SYSFS_RO && f->item->type != SYSFS_RW)
155 return -ENOTSUP;
156
157 if (!sysfs_file_ensure_ready(file))
158 return -ETXTBSY;
159
160 if (offset >= f->buf_head_offset)
161 return 0;
162
163 const char *const buf_va = (char *) phyframe_va(f->buf_page);
164 const size_t begin = offset;
165 const size_t end = MIN(offset + size, (size_t) f->buf_head_offset);
166
167 memcpy(dest: (char *) buf, src: buf_va + begin, n: end - begin);
168 return end - begin;
169}
170
171static ssize_t sysfs_fops_write(const file_t *file, const void *buf, size_t size, off_t offset)
172{
173 sysfs_file_t *f = file->private_data;
174 if (f->item->type != SYSFS_WO && f->item->type != SYSFS_RW)
175 return -ENOTSUP;
176 return f->item->store(f, buf, size, offset);
177}
178
179static off_t sysfs_fops_seek(file_t *file, off_t offset, io_seek_whence_t whence)
180{
181 if (offset != 0)
182 return -1; // cannot change its internal buffer state
183
184 if (whence == IO_SEEK_DATA || whence == IO_SEEK_HOLE)
185 return -1; // not supported
186
187 if (whence == IO_SEEK_SET)
188 return -1;
189
190 sysfs_file_t *f = file->private_data;
191
192 if (f->item->type == SYSFS_MEM)
193 return -1;
194
195 if (!sysfs_file_ensure_ready(file))
196 return -1;
197
198 return f->buf_head_offset;
199}
200
201bool sysfs_fops_mmap(file_t *file, vmap_t *vmap, off_t offset)
202{
203 MOS_UNUSED(vmap);
204 MOS_UNUSED(offset);
205
206 sysfs_file_t *f = file->private_data;
207 if (f->item->type == SYSFS_MEM)
208 {
209 return f->item->mem.mmap(f, vmap, offset);
210 }
211 else
212 {
213 if (!sysfs_file_ensure_ready(file))
214 return false;
215
216 if (offset > f->buf_head_offset)
217 return false; // cannot map past the end of the file
218 }
219
220 return true;
221}
222
223bool sysfs_fops_munmap(file_t *file, vmap_t *vmap, bool *unmapped)
224{
225 sysfs_file_t *f = file->private_data;
226 if (f->item->type == SYSFS_MEM)
227 {
228 return f->item->mem.munmap(f, vmap, unmapped);
229 }
230
231 return true;
232}
233
234static const file_ops_t sysfs_file_ops = {
235 .open = sysfs_fops_open,
236 .release = sysfs_fops_release,
237 .read = sysfs_fops_read,
238 .write = sysfs_fops_write,
239 .seek = sysfs_fops_seek,
240 .mmap = sysfs_fops_mmap,
241 .munmap = sysfs_fops_munmap,
242};
243
244static void sysfs_iops_iterate_dir(dentry_t *dentry, vfs_listdir_state_t *state, dentry_iterator_op add_record)
245{
246 // root directory
247 if (dentry->inode == sysfs_sb->root->inode)
248 {
249 // list all the directories in the dcache
250 vfs_generic_iterate_dir(dir: dentry, state, op: add_record);
251 return;
252 }
253
254 sysfs_dir_t *dir = dentry->inode->private_data;
255 MOS_ASSERT_X(dir, "invalid sysfs entry, possibly a VFS bug");
256
257 // non-dynamic directory
258 if (list_is_empty(head: &dir->_dynamic_items))
259 {
260 vfs_generic_iterate_dir(dir: dentry, state, op: add_record);
261 return;
262 }
263
264 for (size_t j = 0; j < dir->num_items; j++)
265 {
266 sysfs_item_t *item = &dir->items[j];
267 if (item->type == _SYSFS_INVALID || item->type == SYSFS_DYN)
268 continue;
269
270 add_record(state, item->ino, item->name, strlen(str: item->name), FILE_TYPE_REGULAR);
271 }
272
273 // iterate the dynamic items
274 list_node_foreach(item_node, &dir->_dynamic_items)
275 {
276 sysfs_item_t *const dynitem = container_of(item_node, sysfs_item_t, dyn.list_node);
277 dynitem->dyn.iterate(dynitem, dentry, state, add_record);
278 }
279}
280
281static bool sysfs_iops_lookup(inode_t *dir, dentry_t *dentry)
282{
283 // if we get here, it means the expected dentry cannpt be found in the dentry cache
284 // that means either it's a dynamic item, or the user is trying to access a file that doesn't exist
285
286 sysfs_dir_t *sysfs_dir = dir->private_data;
287
288 // if it's NULL, it implies that the dir must be the root directory /sys
289 MOS_ASSERT_X(sysfs_dir || dir == sysfs_sb->root->inode, "invalid sysfs entry, possibly a VFS bug");
290
291 if (!sysfs_dir)
292 return false; // sysfs root dir doesn't have any dynamic items
293
294 if (list_is_empty(head: &sysfs_dir->_dynamic_items))
295 return false;
296
297 list_node_foreach(item_node, &sysfs_dir->_dynamic_items)
298 {
299 sysfs_item_t *const dynitem = container_of(item_node, sysfs_item_t, dyn.list_node);
300 if (dynitem->dyn.lookup(dir, dentry))
301 return true;
302 }
303
304 return false;
305}
306
307static bool sysfs_iops_create(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm)
308{
309 sysfs_dir_t *sysfs_dir = dir->private_data;
310 MOS_ASSERT_X(sysfs_dir || dir == sysfs_sb->root->inode, "invalid sysfs entry, possibly a VFS bug");
311
312 if (!sysfs_dir)
313 return false; // sysfs root dir doesn't have any dynamic items
314
315 if (list_is_empty(head: &sysfs_dir->_dynamic_items))
316 return false;
317
318 list_node_foreach(item_node, &sysfs_dir->_dynamic_items)
319 {
320 sysfs_item_t *const dynitem = container_of(item_node, sysfs_item_t, dyn.list_node);
321 if (dynitem->dyn.create(dir, dentry, type, perm))
322 return true;
323 }
324
325 return false;
326}
327
328static const inode_ops_t sysfs_dir_i_ops = {
329 .iterate_dir = sysfs_iops_iterate_dir,
330 .lookup = sysfs_iops_lookup,
331 .newfile = sysfs_iops_create,
332};
333
334static dentry_t *sysfs_fsop_mount(filesystem_t *fs, const char *dev, const char *options)
335{
336 MOS_ASSERT(fs == &fs_sysfs);
337 if (strcmp(str1: dev, str2: "none") != 0)
338 {
339 mos_warn("device not supported");
340 return NULL;
341 }
342
343 if (options && strlen(str: options) != 0 && strcmp(str1: options, str2: "defaults") != 0)
344 {
345 mos_warn("options '%s' not supported", options);
346 return NULL;
347 }
348
349 return sysfs_sb->root;
350}
351
352static filesystem_t fs_sysfs = {
353 .list_node = LIST_HEAD_INIT(fs_sysfs.list_node),
354 .name = "sysfs",
355 .mount = sysfs_fsop_mount,
356};
357
358static const file_perm_t sysfs_dir_perm = PERM_READ | PERM_EXEC; // r-xr-xr-x
359
360static void sysfs_do_register(sysfs_dir_t *sysfs_dir)
361{
362 inode_t *dir_i = inode_create(sb: sysfs_sb, ino: sysfs_get_ino(), type: FILE_TYPE_DIRECTORY);
363 dir_i->perm = sysfs_dir_perm;
364 dir_i->ops = &sysfs_dir_i_ops;
365 dir_i->file_ops = &sysfs_dir_file_ops;
366 dir_i->private_data = sysfs_dir; ///< for convenience
367
368 dentry_t *vfs_dir = dentry_get_from_parent(sb: sysfs_sb, parent: sysfs_sb->root, name: sysfs_dir->name);
369 dentry_attach(d: vfs_dir, inode: dir_i);
370 sysfs_dir->_dentry = vfs_dir;
371
372 for (size_t i = 0; i < sysfs_dir->num_items; i++)
373 sysfs_register_file(sysfs_dir, item: &sysfs_dir->items[i]);
374}
375
376inode_t *sysfs_create_inode(file_type_t type, void *data)
377{
378 inode_t *inode = inode_create(sb: sysfs_sb, ino: sysfs_get_ino(), type);
379 inode->private_data = data;
380 return inode;
381}
382
383void sysfs_register_file(sysfs_dir_t *sysfs_dir, sysfs_item_t *item)
384{
385 if (item->type == _SYSFS_INVALID)
386 return;
387
388 if (item->type == SYSFS_DYN)
389 {
390 MOS_ASSERT(item->dyn.iterate);
391 linked_list_init(list_node(&item->dyn));
392 list_node_append(head: &sysfs_dir->_dynamic_items, list_node(&item->dyn));
393 return;
394 }
395
396 inode_t *file_i = inode_create(sb: sysfs_sb, ino: sysfs_get_ino(), type: FILE_TYPE_REGULAR);
397 file_i->file_ops = &sysfs_file_ops;
398 file_i->private_data = item;
399 item->ino = file_i->ino;
400
401 switch (item->type)
402 {
403 case _SYSFS_INVALID: MOS_UNREACHABLE();
404 case SYSFS_DYN: MOS_UNREACHABLE();
405 case SYSFS_RO: file_i->perm |= PERM_READ; break;
406 case SYSFS_RW: file_i->perm |= PERM_READ | PERM_WRITE; break;
407 case SYSFS_WO: file_i->perm |= PERM_WRITE; break;
408 case SYSFS_MEM:
409 file_i->perm |= PERM_READ | PERM_WRITE | PERM_EXEC;
410 file_i->size = item->mem.size;
411 break;
412 }
413
414 if (unlikely(!item->name || strlen(item->name) == 0))
415 pr_warn("no name specified for sysfs entry '%s'", sysfs_dir ? sysfs_dir->name : "/");
416
417 dentry_t *const target_dentry = sysfs_dir ? sysfs_dir->_dentry : sysfs_sb->root;
418 MOS_ASSERT_X(target_dentry, "registering sysfs entry '%s' failed", item->name);
419 dentry_t *d = dentry_get_from_parent(sb: sysfs_sb, parent: target_dentry, name: item->name);
420 dentry_attach(d, inode: file_i);
421}
422
423MOS_INIT(VFS, register_sysfs)
424{
425 vfs_register_filesystem(fs: &fs_sysfs);
426
427 sysfs_sb = kmalloc(superblock_cache);
428 sysfs_sb->fs = &fs_sysfs;
429 sysfs_sb->root = dentry_get_from_parent(sb: sysfs_sb, NULL, NULL);
430 inode_t *sysfs_root_inode = inode_create(sb: sysfs_sb, ino: sysfs_get_ino(), type: FILE_TYPE_DIRECTORY);
431 sysfs_root_inode->perm = PERM_READ | PERM_EXEC;
432 sysfs_root_inode->file_ops = &sysfs_dir_file_ops;
433 dentry_attach(d: sysfs_sb->root, inode: sysfs_root_inode);
434 sysfs_sb->root->inode->ops = &sysfs_dir_i_ops;
435}
436