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);
90 taskfs_atop(const char* str)
95 while ((c = str[i++])) {
96 if ('0' > c || c > '9') {
99 t = t * 10 + (c - '0');
105 taskfs_dirlookup(struct v_inode* this, struct v_dnode* dnode)
107 pid_t pid = this->id >> 16;
108 struct proc_info* proc;
111 struct task_attribute* tattr = taskfs_get_attr(&dnode->name);
112 if (!tattr || !(proc = get_process(pid)))
116 taskfs_mknod(dnode, pid, taskfs_next_counter(), VFS_IFSEQDEV);
118 tattr->map_file->data = proc;
119 dnode->inode->data = tattr->map_file;
120 dnode->inode->default_fops = &twimap_file_ops;
125 pid = taskfs_atop(dnode->name.value);
127 if (pid <= 0 || !(proc = get_process(pid))) {
131 return taskfs_mknod(dnode, pid, 0, VFS_IFDIR);
134 static struct v_file_ops taskfs_file_ops = { .close = default_file_close,
135 .read = default_file_read,
136 .write = default_file_write,
137 .readdir = taskfs_readdir,
138 .seek = default_file_seek };
139 static struct v_inode_ops taskfs_inode_ops = { .dir_lookup = taskfs_dirlookup,
140 .open = default_inode_open,
141 .mkdir = default_inode_mkdir,
142 .rmdir = default_inode_rmdir,
143 .rename = default_inode_rename };
146 taskfs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
148 inode->default_fops = &taskfs_file_ops;
149 inode->ops = &taskfs_inode_ops;
152 static volatile struct v_superblock* taskfs_sb;
155 taskfs_mount(struct v_superblock* vsb, struct v_dnode* mount_point)
158 vsb->ops.init_inode = taskfs_init_inode;
160 return taskfs_mknod(mount_point, 0, 0, VFS_IFDIR);
164 taskfs_invalidate(pid_t pid)
166 struct v_dnode *pos, *n;
167 struct v_inode* inode = vfs_i_find(taskfs_sb, taskfs_inode_id(pid, 0));
172 llist_for_each(pos, n, &inode->aka_dnodes, aka_list)
174 if (pos->ref_count > 1) {
181 #define ATTR_TABLE_LEN 16
184 taskfs_export_attr(const char* key, struct twimap* attr_map_file)
186 struct task_attribute* tattr = valloc(sizeof(*tattr));
188 tattr->map_file = attr_map_file;
189 tattr->key = HSTR(tattr->key_val, 0);
190 strncpy(tattr->key_val, key, 32);
191 hstr_rehash(&tattr->key, HSTR_FULL_HASH);
193 struct hbucket* slot = &attr_export_table[tattr->key.hash % ATTR_TABLE_LEN];
194 hlist_add(&slot->head, &tattr->attrs);
195 llist_append(&attributes, &tattr->siblings);
198 struct task_attribute*
199 taskfs_get_attr(struct hstr* key)
201 struct hbucket* slot = &attr_export_table[key->hash % ATTR_TABLE_LEN];
202 struct task_attribute *pos, *n;
203 hashtable_bucket_foreach(slot, pos, n, attrs)
205 if (HSTR_EQ(&pos->key, key)) {
218 struct filesystem* taskfs = fsm_new_fs("taskfs", 5);
219 taskfs->mount = taskfs_mount;
221 fsm_register(taskfs);
223 attr_export_table = vcalloc(ATTR_TABLE_LEN, sizeof(struct hbucket));