-
Notifications
You must be signed in to change notification settings - Fork 0
/
mull.c
160 lines (129 loc) · 3.97 KB
/
mull.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
/**
* This module create /dev/mull (null + mem)
* It didn't save anything to memory, just test speed.
*
* It use circular buffer.
*
* Links:
* http://www.tldp.org/LDP/lkmpg/2.6/html/x569.html
* http://www.imada.sdu.dk/~daniel/DM510/assignment2/ldd-examples/scull/main.c
*
* TODO:
* http://www.kernelthread.com/publications/faq/335.html#t2
* http://www.mjmwired.net/kernel/Documentation/trace/ftrace.txt
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <asm/uaccess.h> /* for copy_from_user */
#include <linux/time.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Azat Khuzhin <[email protected]>");
// Move to header
static int mullInit(void) __init;
static void mullExit(void) __exit;
static int deviceOpen(struct inode *inode, struct file *file);
static int deviceRelease(struct inode *inode, struct file *file);
static ssize_t deviceRead(struct file *filePtr, char *buffer,
size_t length, loff_t *offset);
static ssize_t deviceWrite(struct file *filePtr, const char *buffer,
size_t length, loff_t *offset);
static struct file_operations deviceOperations = {
.read = deviceRead,
.write = deviceWrite,
.open = deviceOpen,
.release = deviceRelease
};
module_init(mullInit);
module_exit(mullExit);
#define SUCCESS 0
#define DEVICE_NAME "mull"
static int majorNumber;
static int deviceOpened = 0; /* prevent multiple access to device */
static struct timeval start;
static ulong bytesRecieved;
static ulong memoryBufferSize = 1 << 10;
module_param(memoryBufferSize, ulong, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myshort, "Installs in-memory buffer size (default 1024)");
static void *circularBuffer;
/**
* Add MKDEV()
*/
static int __init mullInit(void)
{
circularBuffer = kmalloc(memoryBufferSize, GFP_ATOMIC);
majorNumber = register_chrdev(0, DEVICE_NAME, &deviceOperations);
if (majorNumber < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", majorNumber);
return majorNumber;
}
printk(KERN_INFO "mull: loaded (buffer size: %lu, major number: %d)",
memoryBufferSize, majorNumber);
return SUCCESS;
}
static void __exit mullExit(void)
{
kfree(circularBuffer);
printk(KERN_INFO "mull: exit\n");
}
static int deviceOpen(struct inode *inode, struct file *file)
{
if (deviceOpened) {
return -EBUSY;
}
deviceOpened++;
try_module_get(THIS_MODULE);
do_gettimeofday(&start);
bytesRecieved = 0;
printk(KERN_INFO "mull: device is opened\n");
return SUCCESS;
}
static int deviceRelease(struct inode *inode, struct file *file)
{
struct timeval end;
int secondsRunning;
ulong mebibitsRecieved;
do_gettimeofday(&end);
deviceOpened--;
module_put(THIS_MODULE);
/**
* TODO: write this in normal way
*/
secondsRunning = end.tv_sec - start.tv_sec;
if (!secondsRunning) {
secondsRunning = 1;
}
mebibitsRecieved = (bytesRecieved / 1024 /*K*/ / 1024 /*M*/);
if (!mebibitsRecieved) {
mebibitsRecieved = 1;
}
printk(KERN_INFO "mull: device is released (size: %lu MiB, speed: %ld MiB/s)\n",
mebibitsRecieved, (long)((double)mebibitsRecieved / secondsRunning));
return SUCCESS;
}
static ssize_t deviceRead(struct file *filePtr, char *buffer,
size_t length, loff_t *offset)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}
/**
* TODO: circular buffer
*/
static ssize_t deviceWrite(struct file *filePtr, const char *buffer,
size_t length, loff_t *offset)
{
if (length > memoryBufferSize) {
length = memoryBufferSize;
}
if (copy_from_user(circularBuffer, buffer, length)) {
return -EFAULT;
}
bytesRecieved += length;
return length;
}