-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmp.c
140 lines (111 loc) · 3.32 KB
/
dmp.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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/bio.h>
#include <linux/device-mapper.h>
#include <linux/kobject.h>
#include <linux/string.h>
#include "dmp_stat.h"
static struct dmp_stat stat = {
.r_count = 0,
.w_count = 0,
.r_avg_size = 0,
.w_avg_size = 0,
.t_avg_size = 0,
};
static void change_stat(struct bio *bio)
{
ulong mult_avg;
if (bio_op(bio) == REQ_OP_READ) {
mult_avg = stat.r_count * stat.r_avg_size;
stat.r_count += 1;
mult_avg += bio->bi_iter.bi_size;
stat.r_avg_size = (mult_avg + (stat.r_count / 2)) / stat.r_count;
} else {
mult_avg = stat.w_count * stat.w_avg_size;
stat.w_count += 1;
mult_avg += bio->bi_iter.bi_size;
stat.w_avg_size = (mult_avg + (stat.w_count / 2)) / stat.w_count;
}
stat.t_avg_size = (stat.r_avg_size + stat.w_avg_size + 1) / 2;
}
static int dmp_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
struct dm_dev **h_dm_pointer;
if (argc != 1) {
printk(KERN_CRIT "Invalid args number\n");
ti->error = "Invalid args number";
return -EINVAL;
}
h_dm_pointer = kmalloc(sizeof(struct dm_dev *), GFP_KERNEL);
if (h_dm_pointer == NULL) {
printk(KERN_CRIT "No memory for dmp pointer\n");
ti->error = "No memory for dmp pointer";
return -ENOMEM;
}
if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), h_dm_pointer)) {
printk(KERN_CRIT "Block device hasn't been found\n");
ti->error = "Block device hasn't been found";
return -EINVAL;
}
ti->private = h_dm_pointer;
return 0;
}
static int dmp_map(struct dm_target *ti, struct bio *bio)
{
struct dm_dev **h_dm_pointer = ti->private;
if ((bio_op(bio) == REQ_OP_WRITE) | (bio_op(bio) == REQ_OP_READ))
change_stat(bio);
bio_set_dev(bio, (*h_dm_pointer)->bdev);
submit_bio(bio);
return DM_MAPIO_SUBMITTED;
}
static void dmp_dtr(struct dm_target *ti)
{
struct dm_dev **h_dm_pointer = ti->private;
dm_put_device(ti, *h_dm_pointer);
kfree(h_dm_pointer);
}
static struct target_type dmp = {
.name = "dmp",
.version = {1, 0, 0},
.module = THIS_MODULE,
.ctr = dmp_ctr,
.dtr = dmp_dtr,
.map = dmp_map
};
static ssize_t show_stat(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "read:\n reqs: %lu\n avg size: %lu\nwrite:\n reqs: %lu\n avg size: %lu\ntotal:\n reqs: %lu\n avg size: %lu\n",
stat.r_count, stat.r_avg_size, stat.w_count, stat.w_avg_size, stat.r_count + stat.w_count, stat.t_avg_size);
}
static ssize_t store_stat(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
return count;
}
struct kobject module_dir;
struct kobject *stat_dir, *stat_file;
struct kobj_attribute dmp_attr = __ATTR(volume, 0664, show_stat, store_stat);
static int init_dmp_module(void)
{
int result;
result = dm_register_target(&dmp);
if (result < 0)
printk(KERN_CRIT "DMP register error\n");
module_dir = (((struct module *)(THIS_MODULE))->mkobj).kobj;
stat_dir = kobject_create_and_add("stat", &module_dir);
if (stat_dir == NULL)
printk(KERN_CRIT "Can't create dir for stat file\n");
if (sysfs_create_file(stat_dir, &dmp_attr.attr) < 0)
printk(KERN_CRIT "Can't create sysfs stat file\n");
return 0;
}
static void exit_dmp_module(void)
{
kobject_put(stat_file);
kobject_put(stat_dir);
dm_unregister_target(&dmp);
}
module_init(init_dmp_module);
module_exit(exit_dmp_module);
MODULE_LICENSE("GPL");