MOS Source Code
Loading...
Searching...
No Matches
sysfs.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-only
2
3#define pr_fmt(fmt) "sysfs: " fmt
4
6
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
17#include <mos/io/io_types.h>
19#include <mos/types.h>
20#include <mos_stdio.h>
21#include <mos_stdlib.h>
22#include <mos_string.h>
23
34
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
49{
52 pr_dinfo2(sysfs, "registering '%s'", dir->name);
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(buffer->buf_npages);
65
66 if (oldbuf_page)
67 {
68 memcpy((char *) phyframe_va(buffer->buf_page), (char *) phyframe_va(oldbuf_page), 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((char *) phyframe_va(file->buf_page) + file->buf_head_offset, spaces_left, fmt, args);
81 va_end(args);
82
83 if (should_write >= spaces_left)
84 {
85 sysfs_expand_buffer(file, 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(file, file->buf_npages + (count - spaces_left) / MOS_PAGE_SIZE + 1);
99
100 memcpy((char *) phyframe_va(file->buf_page) + file->buf_head_offset, data, 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
111{
112 return file->data;
113}
114
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)
136 kfree(f);
137}
138
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((char *) buf, buf_va + begin, 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
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
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(dentry, state, 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(&dir->_dynamic_items))
259 {
260 vfs_generic_iterate_dir(dentry, state, 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(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(&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(&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
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(dev, "none") != 0)
338 {
339 mos_warn("device not supported");
340 return NULL;
341 }
342
343 if (options && strlen(options) != 0 && strcmp(options, "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{
363 dir_i->perm = sysfs_dir_perm;
364 dir_i->ops = &sysfs_dir_i_ops;
366 dir_i->private_data = sysfs_dir;
367
368 dentry_t *vfs_dir = dentry_get_from_parent(sysfs_sb, sysfs_sb->root, sysfs_dir->name);
369 dentry_attach(vfs_dir, 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, &sysfs_dir->items[i]);
374}
375
377{
378 inode_t *inode = inode_create(sysfs_sb, sysfs_get_ino(), type);
379 inode->private_data = data;
380 return inode;
381}
382
384{
385 if (item->type == _SYSFS_INVALID)
386 return;
387
388 if (item->type == SYSFS_DYN)
389 {
390 MOS_ASSERT(item->dyn.iterate);
392 list_node_append(&sysfs_dir->_dynamic_items, list_node(&item->dyn));
393 return;
394 }
395
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 {
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(sysfs_sb, target_dentry, item->name);
420 dentry_attach(d, file_i);
421}
422
423MOS_INIT(VFS, register_sysfs)
424{
426
427 sysfs_sb = kmalloc(superblock_cache);
428 sysfs_sb->fs = &fs_sysfs;
431 sysfs_root_inode->perm = PERM_READ | PERM_EXEC;
432 sysfs_root_inode->file_ops = &sysfs_dir_file_ops;
433 dentry_attach(sysfs_sb->root, sysfs_root_inode);
435}
#define MOS_ASSERT_X(cond, msg,...)
Definition assert.h:15
#define MOS_ASSERT(cond)
Definition assert.h:14
#define MOS_UNREACHABLE()
Definition assert.h:11
u32 const char * fmt
Definition assert.h:36
#define mos_warn(fmt,...)
Definition assert.h:23
#define MOS_PAGE_SIZE
Definition autoconf.h:6
char args[3][16]
Definition avr_io.c:16
#define PERM_WRITE
Definition fs_types.h:58
#define PERM_READ
Definition fs_types.h:57
u16 file_perm_t
Definition fs_types.h:52
#define PERM_EXEC
Definition fs_types.h:59
file_type_t
Definition fs_types.h:14
@ FILE_TYPE_REGULAR
Definition fs_types.h:15
@ FILE_TYPE_DIRECTORY
Definition fs_types.h:16
void dentry_attach(dentry_t *d, inode_t *inode)
Attach an inode to a dentry.
Definition dentry.c:305
should_inline dentry_t * dentry_parent(const dentry_t *dentry)
Definition dentry.h:65
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
MOSAPI int vsnprintf(char *__restrict buf, size_t size, const char *__restrict format, va_list args)
#define MIN(a, b)
Definition mos_stdlib.h:30
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition list.c:15
#define LIST_HEAD_INIT(container)
Definition list.h:38
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.c:68
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.h:68
#define list_node_foreach(v, h)
Definition list.h:89
list_node_t list_head
A linked list head.
Definition list.h:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.c:21
#define phyframe_va(frame)
Definition mm.h:79
#define mm_free_pages(frame, npages)
Definition mm.h:89
phyframe_t * mm_get_free_pages(size_t npages)
Definition mm.c:55
void vfs_register_filesystem(filesystem_t *fs)
Definition vfs.c:452
inode_t * inode_create(superblock_t *sb, u64 ino, file_type_t type)
Definition inode.c:65
io_seek_whence_t
Definition io_types.h:6
@ IO_SEEK_DATA
Definition io_types.h:10
@ IO_SEEK_SET
Definition io_types.h:9
@ IO_SEEK_HOLE
Definition io_types.h:11
#define __nodiscard
Definition mos_global.h:35
#define MOS_UNUSED(x)
Definition mos_global.h:64
#define unlikely(x)
Definition mos_global.h:40
#define container_of(ptr, type, member)
Definition mos_global.h:50
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
#define pr_warn(fmt,...)
Definition printk.h:38
#define pr_dinfo2(feat, fmt,...)
Definition printk.h:27
#define MOS_INIT(_comp, _fn)
Definition setup.h:40
size_t size
Definition slab.c:30
const char * name
Definition vfs_types.h:118
inode_t * inode
Definition vfs_types.h:117
void * private_data
Definition vfs_types.h:203
dentry_t * dentry
Definition vfs_types.h:200
file_perm_t perm
Definition vfs_types.h:159
const inode_ops_t * ops
Definition vfs_types.h:172
size_t size
Definition vfs_types.h:160
const file_ops_t * file_ops
Definition vfs_types.h:173
u64 ino
Definition vfs_types.h:157
void * private_data
Definition vfs_types.h:174
A node in a linked list.
Definition list.h:27
dentry_t * root
Definition vfs_types.h:107
filesystem_t * fs
Definition vfs_types.h:108
const size_t num_items
Definition sysfs.h:80
sysfs_item_t *const items
Definition sysfs.h:79
list_head _dynamic_items
for internal use only
Definition sysfs.h:82
dentry_t * _dentry
for internal use only
Definition sysfs.h:81
const char * name
Definition sysfs.h:78
ssize_t buf_head_offset
Definition sysfs.c:29
ssize_t buf_npages
Definition sysfs.c:30
void * data
Definition sysfs.c:32
sysfs_item_t * item
Definition sysfs.c:26
phyframe_t * buf_page
Definition sysfs.c:28
struct sysfs_item_t::@8::@11 dyn
sysfs_item_type_t type
Definition sysfs.h:23
size_t(* store)(sysfs_file_t *file, const char *buf, size_t count, off_t offset)
Definition sysfs.h:25
bool(* mmap)(sysfs_file_t *file, vmap_t *vmap, off_t offset)
Definition sysfs.h:32
bool(* lookup)(inode_t *parent_dir, dentry_t *dentry)
Definition sysfs.h:41
bool(* create)(inode_t *parent_dir, dentry_t *dentry, file_type_t type, file_perm_t perm)
Definition sysfs.h:42
size_t size
Definition sysfs.h:34
const char * name
Definition sysfs.h:22
bool(* munmap)(sysfs_file_t *file, vmap_t *vmap, bool *unmapped)
Definition sysfs.h:33
struct sysfs_item_t::@8::@10 mem
ino_t ino
Definition sysfs.h:26
void(* iterate)(struct _sysfs_item *item, dentry_t *dentry, vfs_listdir_state_t *iterator_state, dentry_iterator_op op)
Definition sysfs.h:40
bool(* show)(sysfs_file_t *file)
Definition sysfs.h:24
Definition mm.h:58
static __nodiscard bool sysfs_file_ensure_ready(const file_t *file)
Definition sysfs.c:139
void sysfs_file_set_data(sysfs_file_t *file, void *data)
Definition sysfs.c:105
static const file_perm_t sysfs_dir_perm
Definition sysfs.c:358
void sysfs_register(sysfs_dir_t *dir)
Register a sysfs directory.
Definition sysfs.c:48
void sysfs_register_file(sysfs_dir_t *sysfs_dir, sysfs_item_t *item)
Register an entry in a sysfs directory.
Definition sysfs.c:383
static list_head sysfs_dirs
Definition sysfs.c:35
ssize_t sysfs_printf(sysfs_file_t *file, const char *fmt,...)
Definition sysfs.c:73
static void sysfs_expand_buffer(sysfs_file_t *buffer, size_t new_npages)
Definition sysfs.c:57
static off_t sysfs_fops_seek(file_t *file, off_t offset, io_seek_whence_t whence)
Definition sysfs.c:179
inode_t * sysfs_create_inode(file_type_t type, void *data)
Definition sysfs.c:376
bool sysfs_fops_munmap(file_t *file, vmap_t *vmap, bool *unmapped)
Definition sysfs.c:223
static const inode_ops_t sysfs_dir_i_ops
Definition sysfs.c:328
ssize_t sysfs_put_data(sysfs_file_t *file, const void *data, size_t count)
Definition sysfs.c:93
static ssize_t sysfs_fops_write(const file_t *file, const void *buf, size_t size, off_t offset)
Definition sysfs.c:171
static void sysfs_fops_release(file_t *file)
Definition sysfs.c:130
static filesystem_t fs_sysfs
Definition sysfs.c:36
static bool sysfs_fops_open(inode_t *inode, file_t *file, bool created)
Definition sysfs.c:120
static void sysfs_iops_iterate_dir(dentry_t *dentry, vfs_listdir_state_t *state, dentry_iterator_op add_record)
Definition sysfs.c:244
static superblock_t * sysfs_sb
Definition sysfs.c:37
bool sysfs_fops_mmap(file_t *file, vmap_t *vmap, off_t offset)
Definition sysfs.c:201
static const file_ops_t sysfs_dir_file_ops
Definition sysfs.c:38
static const file_ops_t sysfs_file_ops
Definition sysfs.c:234
static bool sysfs_iops_create(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm)
Definition sysfs.c:307
sysfs_item_t * sysfs_file_get_item(sysfs_file_t *file)
Definition sysfs.c:115
static u64 sysfs_get_ino(void)
Definition sysfs.c:42
void * sysfs_file_get_data(sysfs_file_t *file)
Definition sysfs.c:110
static dentry_t * sysfs_fsop_mount(filesystem_t *fs, const char *dev, const char *options)
Definition sysfs.c:334
static void sysfs_do_register(sysfs_dir_t *sysfs_dir)
Definition sysfs.c:360
static ssize_t sysfs_fops_read(const file_t *file, void *buf, size_t size, off_t offset)
Definition sysfs.c:151
static bool sysfs_iops_lookup(inode_t *dir, dentry_t *dentry)
Definition sysfs.c:281
@ SYSFS_RW
Definition sysfs.h:14
@ SYSFS_MEM
memory-backed file
Definition sysfs.h:16
@ SYSFS_DYN
dynamic directory items
Definition sysfs.h:17
@ _SYSFS_INVALID
Definition sysfs.h:12
@ SYSFS_RO
Definition sysfs.h:13
@ SYSFS_WO
Definition sysfs.h:15
struct _sysfs_file sysfs_file_t
Definition sysfs.h:8
static char buffer[2048]
Definition test_printf.c:7
ssize_t off_t
Definition types.h:84
unsigned long long u64
Definition types.h:23
signed long ssize_t
Definition types.h:83
slab_t * superblock_cache
Definition vfs.c:38
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
void vfs_generic_iterate_dir(const dentry_t *dir, vfs_listdir_state_t *state, dentry_iterator_op add_record)
Definition vfs_utils.c:120
dentry_t * dentry_get_from_parent(superblock_t *sb, dentry_t *parent, const char *name)
Create a new dentry with the given name and parent.
Definition vfs_utils.c:40