MOS Source Code
Loading...
Searching...
No Matches
memfd.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/ipc/memfd.h"
4
10#include "mos/io/io.h"
11#include "mos/misc/setup.h"
13#include "mos/syslog/printk.h"
14
16#include <mos_stdlib.h>
17#include <stdio.h>
18
19typedef struct
20{
21 int unused;
22} memfd_t;
23
25
28
30
31static void memfd_file_release(file_t *file)
32{
33 dentry_detach(file->dentry);
34}
35
36static const file_ops_t memfd_file_ops = {
37 .read = vfs_generic_read,
38 .write = vfs_generic_write,
39 .release = memfd_file_release,
40};
41
42io_t *memfd_create(const char *name)
43{
44 memfd_t *memfd = kmalloc(memfd_slab);
45 if (!memfd)
46 {
47 pr_emerg("Failed to allocate memfd");
48 return ERR_PTR(-ENOMEM);
49 }
50
52
54 {
55 pr_emerg("Failed to create file for memfd");
56 kfree(memfd);
57 kfree(dentry);
58 return ERR_PTR(-ENOMEM);
59 }
60
61 file_t *file = vfs_do_open_dentry(dentry, true, true, true, false, false);
62 if (IS_ERR(file))
63 {
64 pr_emerg("Failed to open file for memfd");
65 kfree(memfd);
66 kfree(dentry);
67 return ERR(file);
68 }
69
71 file->private_data = memfd;
74 return &file->io;
75}
76
77static void memfd_init()
78{
81 {
82 pr_emerg("Failed to mount memfd filesystem");
83 return;
84 }
85
89}
90
#define PERM_WRITE
Definition fs_types.h:58
#define PERM_READ
Definition fs_types.h:57
#define PERM_OWNER
Definition fs_types.h:54
@ FILE_TYPE_REGULAR
Definition fs_types.h:15
void dentry_detach(dentry_t *d)
Detach the inode from a dentry.
Definition dentry.c:318
dentry_t * dentry_ref(dentry_t *dentry)
Increment the reference count of a dentry.
file_t * vfs_do_open_dentry(dentry_t *entry, bool created, bool read, bool write, bool exec, bool truncate)
Open an directory dentry.
Definition vfs.c:413
bool inode_unlink(inode_t *dir, dentry_t *dentry)
Unlink a dentry from its parent inode.
Definition inode.c:86
io_t * memfd_create(const char *name)
Definition memfd.c:42
static dentry_t * memfd_root_dentry
Definition memfd.c:29
static void memfd_file_release(file_t *file)
Definition memfd.c:31
static void memfd_init()
Definition memfd.c:77
static const file_ops_t memfd_file_ops
Definition memfd.c:36
filesystem_t fs_tmpfs
static slab_t * memfd_slab
Definition memfd.c:26
#define NULL
Definition pb_syshdr.h:46
#define pr_emerg(fmt,...)
Definition printk.h:39
#define MOS_INIT(_comp, _fn)
Definition setup.h:40
const char * name
Definition slab.c:31
#define SLAB_AUTOINIT(name, var, type)
superblock_t * superblock
Definition vfs_types.h:119
bool is_mountpoint
Definition vfs_types.h:120
inode_t * inode
Definition vfs_types.h:117
io_t io
Definition vfs_types.h:199
void * private_data
Definition vfs_types.h:203
dentry_t * dentry
Definition vfs_types.h:200
dentry_t *(* mount)(filesystem_t *fs, const char *dev_name, const char *mount_options)
Definition vfs_types.h:184
bool(* newfile)(inode_t *dir, dentry_t *dentry, file_type_t type, file_perm_t perm)
create a new file
Definition vfs_types.h:75
const inode_ops_t * ops
Definition vfs_types.h:172
const file_ops_t * file_ops
Definition vfs_types.h:173
Definition io.h:46
int unused
Definition memfd.c:21
Definition slab.h:45
ssize_t vfs_generic_write(const file_t *file, const void *buf, size_t size, off_t offset)
Definition vfs_utils.c:105
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
ssize_t vfs_generic_read(const file_t *file, void *buf, size_t size, off_t offset)
Definition vfs_utils.c:95