feat: taskfs for export process to filesystem
[lunaix-os.git] / lunaix-os / kernel / process / taskfs.c
1 #include <lunaix/dirent.h>
2 #include <lunaix/fs/taskfs.h>
3 #include <lunaix/fs/twimap.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/process.h>
6 #include <lunaix/sched.h>
7
8 #include <klibc/stdio.h>
9 #include <klibc/string.h>
10
11 #define COUNTER_MASK ((1 << 16) - 1)
12
13 static struct hbucket* attr_export_table;
14 static DEFINE_LLIST(attributes);
15 static volatile int ino_cnt = 1;
16
17 int
18 taskfs_next_counter()
19 {
20     return (ino_cnt = ((ino_cnt + 1) & COUNTER_MASK) + 1);
21 }
22
23 inode_t
24 taskfs_inode_id(pid_t pid, int sub_counter)
25 {
26     return ((pid & (MAX_PROCESS - 1)) << 16) | (sub_counter & COUNTER_MASK);
27 }
28
29 int
30 taskfs_mknod(struct v_dnode* dnode, pid_t pid, int sub_counter, int itype)
31 {
32     inode_t ino = taskfs_inode_id(pid, sub_counter);
33
34     struct v_superblock* vsb = dnode->super_block;
35     struct v_inode* inode = vfs_i_find(vsb, ino);
36     if (!inode) {
37         if (!(inode = vfs_i_alloc(vsb))) {
38             return ENOMEM;
39         }
40         inode->id = ino;
41         inode->itype = itype;
42         vfs_i_addhash(inode);
43     }
44
45     vfs_assign_inode(dnode, inode);
46     return 0;
47 }
48
49 int
50 taskfs_readdir(struct v_file* file, struct dir_context* dctx)
51 {
52     struct v_inode* inode = file->inode;
53     pid_t pid = inode->id >> 16;
54     int counter = 0;
55
56     if ((inode->id & COUNTER_MASK)) {
57         return ENOTDIR;
58     }
59
60     if (pid) {
61         struct task_attribute *pos, *n;
62         llist_for_each(pos, n, &attributes, siblings)
63         {
64             if (counter == dctx->index) {
65                 dctx->read_complete_callback(
66                   dctx, pos->key_val, VFS_NAME_MAXLEN, DT_FILE);
67                 return 1;
68             }
69             counter++;
70         }
71         return 0;
72     }
73
74     char name[VFS_NAME_MAXLEN];
75     struct proc_info *root = get_process(pid), *pos, *n;
76     llist_for_each(pos, n, &root->tasks, tasks)
77     {
78         if (counter == dctx->index) {
79             ksnprintf(name, VFS_NAME_MAXLEN, "%d", pos->pid);
80             dctx->read_complete_callback(dctx, name, VFS_NAME_MAXLEN, DT_DIR);
81             return 1;
82         }
83         counter++;
84     }
85     return 0;
86 }
87
88 pid_t
89 taskfs_atop(const char* str)
90 {
91     pid_t t = 0;
92     int i = 0;
93     char c;
94     while ((c = str[i++])) {
95         if ('0' > c || c > '9') {
96             return -1;
97         }
98         t = t * 10 + (c - '0');
99     }
100     return t;
101 }
102
103 int
104 taskfs_dirlookup(struct v_inode* this, struct v_dnode* dnode)
105 {
106     pid_t pid = this->id >> 16;
107     struct proc_info* proc;
108
109     if (pid) {
110         struct task_attribute* tattr = taskfs_get_attr(&dnode->name);
111         if (!tattr || !(proc = get_process(pid)))
112             return ENOENT;
113
114         int errno = taskfs_mknod(dnode, pid, taskfs_next_counter(), VFS_IFFILE);
115         if (!errno) {
116             tattr->map_file->data = proc;
117             dnode->inode->data = tattr->map_file;
118             dnode->inode->default_fops = &twimap_file_ops;
119         }
120         return errno;
121     }
122
123     pid = taskfs_atop(dnode->name.value);
124
125     if (pid <= 0 || !(proc = get_process(pid))) {
126         return ENOENT;
127     }
128
129     return taskfs_mknod(dnode, pid, 0, VFS_IFDIR);
130 }
131
132 static struct v_file_ops taskfs_file_ops = { .close = default_file_close,
133                                              .read = default_file_read,
134                                              .write = default_file_write,
135                                              .readdir = taskfs_readdir,
136                                              .seek = default_file_seek };
137 static struct v_inode_ops taskfs_inode_ops = { .dir_lookup = taskfs_dirlookup,
138                                                .open = default_inode_open,
139                                                .mkdir = default_inode_mkdir,
140                                                .rmdir = default_inode_rmdir,
141                                                .rename = default_inode_rename };
142
143 void
144 taskfs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
145 {
146     inode->default_fops = &taskfs_file_ops;
147     inode->ops = &taskfs_inode_ops;
148 }
149
150 int
151 taskfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
152 {
153     vsb->ops.init_inode = taskfs_init_inode;
154
155     return taskfs_mknod(mount_point, 0, 0, VFS_IFDIR);
156 }
157
158 #define ATTR_TABLE_LEN 16
159
160 void
161 taskfs_export_attr(const char* key, struct twimap* attr_map_file)
162 {
163     struct task_attribute* tattr = valloc(sizeof(*tattr));
164
165     tattr->map_file = attr_map_file;
166     tattr->key = HSTR(tattr->key_val, 0);
167     strncpy(tattr->key_val, key, 32);
168     hstr_rehash(&tattr->key, HSTR_FULL_HASH);
169
170     struct hbucket* slot = &attr_export_table[tattr->key.hash % ATTR_TABLE_LEN];
171     hlist_add(&slot->head, &tattr->attrs);
172     llist_append(&attributes, &tattr->siblings);
173 }
174
175 struct task_attribute*
176 taskfs_get_attr(struct hstr* key)
177 {
178     struct hbucket* slot = &attr_export_table[key->hash % ATTR_TABLE_LEN];
179     struct task_attribute *pos, *n;
180     hashtable_bucket_foreach(slot, pos, n, attrs)
181     {
182         if (HSTR_EQ(&pos->key, key)) {
183             return pos;
184         }
185     }
186     return NULL;
187 }
188
189 extern void
190 export_task_attr();
191
192 void
193 taskfs_init()
194 {
195     struct filesystem* taskfs = fsm_new_fs("taskfs", 5);
196     taskfs->mount = taskfs_mount;
197
198     fsm_register(taskfs);
199
200     attr_export_table = vcalloc(ATTR_TABLE_LEN, sizeof(struct hbucket));
201
202     export_task_attr();
203 }