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>
8 #include <klibc/stdio.h>
9 #include <klibc/string.h>
11 #define COUNTER_MASK ((1 << 16) - 1)
13 static struct hbucket* attr_export_table;
14 static DEFINE_LLIST(attributes);
15 static volatile int ino_cnt = 1;
20 return (ino_cnt = ((ino_cnt + 1) & COUNTER_MASK) + 1);
24 taskfs_inode_id(pid_t pid, int sub_counter)
26 return ((pid & (MAX_PROCESS - 1)) << 16) | (sub_counter & COUNTER_MASK);
30 taskfs_mknod(struct v_dnode* dnode, pid_t pid, int sub_counter, int itype)
32 inode_t ino = taskfs_inode_id(pid, sub_counter);
34 struct v_superblock* vsb = dnode->super_block;
35 struct v_inode* inode = vfs_i_find(vsb, ino);
37 if (!(inode = vfs_i_alloc(vsb))) {
45 vfs_assign_inode(dnode, inode);
50 taskfs_readdir(struct v_file* file, struct dir_context* dctx)
52 struct v_inode* inode = file->inode;
53 pid_t pid = inode->id >> 16;
56 if ((inode->id & COUNTER_MASK)) {
61 struct task_attribute *pos, *n;
62 llist_for_each(pos, n, &attributes, siblings)
64 if (counter == dctx->index) {
65 dctx->read_complete_callback(
66 dctx, pos->key_val, VFS_NAME_MAXLEN, DT_FILE);
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)
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);
89 taskfs_atop(const char* str)
94 while ((c = str[i++])) {
95 if ('0' > c || c > '9') {
98 t = t * 10 + (c - '0');
104 taskfs_dirlookup(struct v_inode* this, struct v_dnode* dnode)
106 pid_t pid = this->id >> 16;
107 struct proc_info* proc;
110 struct task_attribute* tattr = taskfs_get_attr(&dnode->name);
111 if (!tattr || !(proc = get_process(pid)))
114 int errno = taskfs_mknod(dnode, pid, taskfs_next_counter(), VFS_IFFILE);
116 tattr->map_file->data = proc;
117 dnode->inode->data = tattr->map_file;
118 dnode->inode->default_fops = &twimap_file_ops;
123 pid = taskfs_atop(dnode->name.value);
125 if (pid <= 0 || !(proc = get_process(pid))) {
129 return taskfs_mknod(dnode, pid, 0, VFS_IFDIR);
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 };
144 taskfs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
146 inode->default_fops = &taskfs_file_ops;
147 inode->ops = &taskfs_inode_ops;
151 taskfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
153 vsb->ops.init_inode = taskfs_init_inode;
155 return taskfs_mknod(mount_point, 0, 0, VFS_IFDIR);
158 #define ATTR_TABLE_LEN 16
161 taskfs_export_attr(const char* key, struct twimap* attr_map_file)
163 struct task_attribute* tattr = valloc(sizeof(*tattr));
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);
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);
175 struct task_attribute*
176 taskfs_get_attr(struct hstr* key)
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)
182 if (HSTR_EQ(&pos->key, key)) {
195 struct filesystem* taskfs = fsm_new_fs("taskfs", 5);
196 taskfs->mount = taskfs_mount;
198 fsm_register(taskfs);
200 attr_export_table = vcalloc(ATTR_TABLE_LEN, sizeof(struct hbucket));