-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths2fs_part4.c
280 lines (213 loc) · 6.49 KB
/
s2fs_part4.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pagemap.h>
#include <linux/fs.h>
#include <asm/atomic.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/slab.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jonathan Corbet");
#define S2FS_MAGIC 0x19920342
#define TMPSIZE 20
static struct inode *s2fs_make_inode(struct super_block *sb, int mode, const struct file_operations* fops)
{
struct inode* inode;
inode = new_inode(sb);
if (!inode) {
return NULL;
}
inode->i_mode = mode;
inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
inode->i_fop = fops;
inode->i_ino = get_next_ino();
return inode;
}
int get_task_info(int pid, char* data) {
struct task_struct *task;
struct pid *pid_struct;
int offset = 0;
pid_struct = find_get_pid(pid);
if(pid_struct == NULL) { offset+= sprintf(data, "\nTask no longer exists."); goto exit; }
task = pid_task(pid_struct, PIDTYPE_PID);
if(task == NULL) { offset+= sprintf(data, "\nTask no longer exists."); goto exit; }
offset = sprintf(data, "Task name: %s \nTask State: %ld \nProcess Id: %d \nCPU Id: %u \nTGID: %d"
"\nParent ID: %d \nStart Time: %llu \nDynamic priority: %d \nStatic Prio: %d \nNormal Priority: %d"
"\nRT Priority: %d", task->comm, task->state, pid, task->cpu, task->tgid, task->real_parent->pid,
task->start_time, task->prio, task->static_prio, task->normal_prio, task->rt_priority );
/* null checks */
if(task->active_mm == NULL) {
printk(KERN_INFO "active mm is null!");
offset += sprintf(data+strlen(data), "\nactive mm struct is null!"); goto exit;
}
printk(KERN_INFO "Memory Map Base: %lu\n", task->active_mm->mmap_base);
printk(KERN_INFO "No. of vmem address %d\n", task->active_mm->map_count);
printk(KERN_INFO "Total pages mapped: %lu\n", task->active_mm->total_vm);
printk(KERN_INFO "Virtual Memory Usage: %llu\n", task->acct_vm_mem1);
printk(KERN_INFO "Virtual mem space: %lu\n",task->active_mm->task_size);
offset += sprintf(data + strlen(data),"\nMemory Map Base: %lu \nNo.of vmem address %d \nTotal pages mapped: %lu"
"\nVirtual Memory Usage: %llu \nVirtual mem space: %lu\n", task->active_mm->mmap_base, task->active_mm->map_count,
task->active_mm->total_vm, task->acct_vm_mem1, task->active_mm->task_size);
exit:
return offset;
}
static int s2fs_open(struct inode *inode, struct file *filp)
{
filp->private_data = inode->i_private;
return 0;
}
static ssize_t s2fs_read_file(struct file *filp, char *buf, size_t count, loff_t *offset)
{
char *tmp;
int *pid;
int len;
pid = filp->private_data;
tmp = (char *)kmalloc(500, GFP_KERNEL);
len = get_task_info(*pid, tmp);
if (*offset > len)
return 0;
if (count > len - *offset)
count = len - *offset;
if (copy_to_user(buf, tmp + *offset, count))
return -EFAULT;
*offset += count;
return count;
// return simple_read_from_buffer(buf, len, offset, tmp, len);
}
static ssize_t s2fs_write_file(struct file *filp, const char *buf,
size_t count, loff_t *offset) {
return 0;
}
static struct file_operations s2fs_file_ops = {
.open = s2fs_open,
.read = s2fs_read_file,
.write = s2fs_write_file,
};
const struct inode_operations lwfs_inode_operations = {
.setattr = simple_setattr,
.getattr = simple_getattr,
};
static struct dentry *s2fs_create_file (struct super_block *sb,
struct dentry *dir, const char *name)
{
struct dentry *dentry;
struct inode *inode;
int pid, ret;
int *int_pid;
dentry = d_alloc_name(dir, name);
if (! dentry)
goto out;
inode = s2fs_make_inode(sb, S_IFREG | 0644, &s2fs_file_ops);
if (! inode)
goto out_dput;
ret = kstrtoint(name, 10, &pid);
int_pid = (int*)kmalloc(sizeof(int), GFP_KERNEL);
*int_pid = pid;
inode->i_private = int_pid;
d_add(dentry, inode);
return dentry;
out_dput:
dput(dentry);
out:
return 0;
}
static struct dentry *s2fs_create_dir (struct super_block *sb,
struct dentry *parent, const char *name)
{
struct dentry *dentry;
struct inode *inode;
int pid, ret;
int *int_pid;
dentry = d_alloc_name(parent, name);
if (! dentry)
goto out;
inode = s2fs_make_inode(sb, S_IFDIR | 0755, &simple_dir_operations);
if (! inode)
goto out_dput;
inode->i_op = &simple_dir_inode_operations;
ret = kstrtoint(name, 10, &pid);
int_pid = (int*)kmalloc(sizeof(int), GFP_KERNEL);
*int_pid = pid;
inode->i_private = int_pid;
d_add(dentry, inode);
return dentry;
out_dput:
dput(dentry);
out:
return 0;
}
static struct super_operations s2fs_s_ops = {
.statfs = simple_statfs,
.drop_inode = generic_delete_inode,
};
void traverse(struct super_block *sb, struct dentry *parent, struct task_struct *task)
{
struct dentry *dir;
char str_pid[6];
struct list_head *list;
struct task_struct *task_child;
snprintf(str_pid, 6, "%ld", (long)task->pid);
if(!list_empty(&task->children)) {
dir = s2fs_create_dir(sb, parent, str_pid);
s2fs_create_file(sb, dir, str_pid);
}
else
s2fs_create_file(sb, parent, str_pid);
list_for_each(list, &task->children) {
task_child = list_entry( list, struct task_struct, sibling );
if(task_child) {
traverse(sb, dir, task_child);
}
}
}
static int s2fs_fill_super (struct super_block *sb, void *data, int silent)
{
struct inode *root;
struct dentry *root_dentry;
struct task_struct *task;
sb->s_blocksize = VMACACHE_SIZE;
sb->s_blocksize_bits = VMACACHE_SIZE;
sb->s_magic = S2FS_MAGIC;
sb->s_op = &s2fs_s_ops;
root = s2fs_make_inode (sb, S_IFDIR | 0755, &simple_dir_operations);
inode_init_owner(root, NULL, S_IFDIR | 0755);
if (! root)
goto out;
root->i_op = &simple_dir_inode_operations;
// root->i_fop = &simple_dir_operations;
set_nlink(root, 2);
root_dentry = d_make_root(root);
if (! root_dentry)
goto out_iput;
task = &init_task;
traverse(sb, root_dentry, task);
sb->s_root = root_dentry;
return 0;
out_iput:
iput(root);
out:
return -ENOMEM;
}
static struct dentry *s2fs_get_super(struct file_system_type *fst,
int flags, const char *devname, void *data)
{
return mount_nodev(fst, flags, data, s2fs_fill_super);
}
static struct file_system_type s2fs_type = {
.owner = THIS_MODULE,
.name = "s2fs_part4",
.mount = s2fs_get_super,
.kill_sb = kill_litter_super,
};
static int __init s2fs_init(void)
{
return register_filesystem(&s2fs_type);
}
static void __exit s2fs_exit(void)
{
unregister_filesystem(&s2fs_type);
}
module_init(s2fs_init);
module_exit(s2fs_exit);