-
Notifications
You must be signed in to change notification settings - Fork 0
/
devfs.c
148 lines (120 loc) · 3.01 KB
/
devfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "devfs.h"
#include "common.h"
#include "kmalloc.h"
#include "log.h"
#include "string.h"
struct devfs_inode {
struct devfs_inode *next;
char const *name;
vnode_t *node;
};
typedef struct devfs_inode devfs_inode_t;
static devfs_inode_t *devices = NULL;
static vnode_t root;
static vnodeops_t vnodeops;
static vfsops_t vfsops;
static int devfs_root(vfs_t *vfs, vnode_t *out)
{
UNUSED_ARGUMENT(vfs);
out->v_op = root.v_op;
out->v_data = root.v_data;
return 0;
}
static int devfs_open(vnode_t *n)
{
UNUSED_ARGUMENT(n);
return -1;
}
static int devfs_read(vnode_t *node, void *buf, uint32_t count)
{
UNUSED_ARGUMENT(node);
UNUSED_ARGUMENT(buf);
UNUSED_ARGUMENT(count);
return -1;
}
static int devfs_write(vnode_t *n, char const *s, size_t l)
{
UNUSED_ARGUMENT(n);
UNUSED_ARGUMENT(s);
UNUSED_ARGUMENT(l);
return -1;
}
static int devfs_getattr(vnode_t *n, vattr_t *attr)
{
UNUSED_ARGUMENT(n);
attr->file_size = 0;
return 0;
}
static int devfs_lookup(vnode_t *dir, char const *name, vnode_t *out)
{
UNUSED_ARGUMENT(dir);
if (name == NULL) {
log_error("devfs_lookup",
"Trying to lookup a null path\n");
return -1;
}
devfs_inode_t *i;
for (i = devices; i != NULL; i = i->next) {
if (strcmp(i->name, name) == 0) {
out->v_op = i->node->v_op;
out->v_data = i->node->v_data;
return 0;
}
}
return -1;
}
int devfs_add_device(char const *path, vnode_t *node)
{
if (path == NULL || strlen(path) == 0) {
log_error("devfs_add_device",
"path is empty or NULL\n");
return -1;
}
devfs_inode_t *i;
for (i = devices; i != NULL; i = i->next) {
if (strcmp(i->name, path) == 0) {
log_error("devfs_add_device",
"Trying to add a device that is already added: %s\n",
path);
return -1;
}
}
int len = strlen(path) + 1;
char *copy = kmalloc(len);
if (copy == NULL) {
log_error("devfs_add_device",
"Could not allocated memory for path name %s\n",
path);
return -1;
}
memcpy(copy, path, len);
devfs_inode_t *inode = kmalloc(sizeof(devfs_inode_t));
if (inode == NULL) {
log_error("devfs_add_device",
"Could not allocated memory for devfs_inode_t struct\n");
kfree(copy);
return -1;
}
inode->next = NULL;
inode->name = copy;
inode->node = node;
if (devices == NULL) {
devices = inode;
} else {
devices->next = inode;
}
return 0;
}
int devfs_init(vfs_t *vfs)
{
vnodeops.vn_open = &devfs_open;
vnodeops.vn_getattr = &devfs_getattr;
vnodeops.vn_read = &devfs_read;
vnodeops.vn_lookup = &devfs_lookup;
vnodeops.vn_write = &devfs_write;
root.v_op = &vnodeops;
vfsops.vfs_root = &devfs_root;
vfs->vfs_op = &vfsops;
vfs->vfs_data = 0;
return 0;
}