-
Notifications
You must be signed in to change notification settings - Fork 14
/
mini_block.c
303 lines (249 loc) · 7.57 KB
/
mini_block.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Mini-block driver.
*
* This code is based on the example of LWN.NET: http://lwn.net/Articles/31513/
*
* Copyright 2003 Eklektix, Inc. Redistributable under the terms
* of the GNU GPL.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>
#include <linux/buffer_head.h> /* invalidate_bdev */
#include <linux/bio.h>
MODULE_LICENSE("Dual BSD/GPL");
#define KERNEL_SECTOR_SIZE 512
#define MAJOR_NUM 240 /* free mayor number, see devices.txt */
#define MINIBD_MINORS 16 /* maximum partitions availables */
#define N_SECTORS 3 /* number of sectors of our block device */
#define MINIBLOCK_SECTOR_SIZE 512 /* our sector size */
/*
* miniblock request queue.
*/
static struct request_queue *miniblock_queue;
/*
* Device private structure. The internal
* representation of our device.
*/
static struct miniblock_device {
unsigned long size;
spinlock_t lock;
u8 *data;
struct gendisk *gd;
};
static struct miniblock_device *Miniblock = NULL;
/*
* Real handler of I/O requests.
*/
static void miniblock_transfer(struct miniblock_device *dev, unsigned long sector,
unsigned long nsect, char *buffer, int write)
{
unsigned long offset = sector*MINIBLOCK_SECTOR_SIZE;
unsigned long nbytes = nsect*MINIBLOCK_SECTOR_SIZE;
if ((offset + nbytes) > dev->size) {
printk (KERN_NOTICE "minibd: Beyond-end write (%ld %ld)\n", offset, nbytes);
return;
}
if (write)
memcpy(dev->data + offset, buffer, nbytes);
else
memcpy(buffer, dev->data + offset, nbytes);
}
/*
* I/O request handler.
*/
static void miniblock_request( struct request_queue *q)
{
#if 0
struct request *rq = blk_fetch_request( q );
printk( KERN_DEBUG " %s:%d\n", __FILE__, __LINE__);
for( ; rq ; )
{
if( rq->cmd_type != REQ_TYPE_FS )
{
printk( KERN_DEBUG "minibd: cmd type = %d\n",rq->cmd_type);
__blk_end_request_all( rq, 0 );
break;
// continue;
}
bool do_write = (rq_data_dir(rq) == WRITE );
int size = blk_rq_bytes( rq );
printk( KERN_DEBUG " minibf: w/r=%s 0x%x at 0x%llx\n", (do_write ? "write" : "read"), size, blk_rq_pos(rq)*512ULL );
__blk_end_request_all( rq, 1 );
break;
rq = blk_fetch_request(q) ;
}
// __blk_end_request_all( rq, 0 );
#endif
struct request *req = blk_fetch_request(q);
while (req) {
unsigned block = blk_rq_pos(req);
unsigned count = blk_rq_cur_sectors(req);
// XD_INFO *disk = req->rq_disk->private_data;
int res = -EIO;
int retry;
if (req->cmd_type != REQ_TYPE_FS)
goto done;
if (block + count > get_capacity(req->rq_disk))
goto done;
bool do_write = (rq_data_dir(req) == WRITE );
int size = blk_rq_bytes( req );
printk( KERN_DEBUG " minibf: w/r=%s 0x%x at 0x%llx\n", (do_write ? "write" : "read"), size, blk_rq_pos(req)*512ULL );
if( rq_data_dir( req ) == READ)
{
struct req_iterator iter;
struct bio_vec *bvec;
/*
* we are really probing at internals to determine
* whether to set MSG_MORE or not...
*/
rq_for_each_segment(bvec, req, iter) {
int result = 0 , idx = 0, counter;
char *kaddr = kmap_atomic(bvec->bv_page, KM_USER0)+bvec->bv_offset;
printk( KERN_DEBUG " bvec lenth = %d, offset %d \n", bvec->bv_len, bvec->bv_offset );
// result = sock_xmit(lo, 1, kaddr + bvec->bv_offset, bvec->bv_len, flags);
//
for( idx = 0, counter = 1 ; idx < bvec->bv_len ; idx ++,counter ++ )
{
//((char*)kaddr)[ bvec->bv_offset + idx ] = counter;
((char*)kaddr)[ idx ] = counter;
}
// kunmap(bvec->bv_page);
// kaddr = 0;
kunmap_atomic(kaddr, KM_USER0);
}
printk( KERN_DEBUG " finish read \n" );
}else
{
printk( KERN_DEBUG " not support write or > 512\n" );
}
/*
for (retry = 0; (retry < XD_RETRIES) && !res; retry++)
res = xd_readwrite(rq_data_dir(req), disk, req->buffer,
block, count);
*/
res = 0;
done:
/* wrap up, 0 = success, -errno = fail */
if (!__blk_end_request_cur(req, res))
req = blk_fetch_request(q);
}
}
int miniblock_ioctl (struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
long size;
struct hd_geometry geo;
switch(cmd) {
/*
* The only command we need to interpret is HDIO_GETGEO, since
* we can't partition the drive otherwise. We have no real
* geometry, of course, so make something up.
*/
case HDIO_GETGEO:
size = Miniblock->size*(MINIBLOCK_SECTOR_SIZE/KERNEL_SECTOR_SIZE);
geo.cylinders = (size & ~0x3f) >> 6;
geo.heads = 4;
geo.sectors = 16;
geo.start = 4;
if (copy_to_user((void *) arg, &geo, sizeof(geo)))
return -EFAULT;
return 0;
}
return -ENOTTY;
}
/*
* Device operations.
*/
static struct block_device_operations miniblock_ops = {
.owner = THIS_MODULE,
.ioctl = miniblock_ioctl
};
/*
* LKM stuff.
*/
static int __init miniblock_init(void)
{
static int ret;
ret = register_blkdev(MAJOR_NUM, "minibd");
if (ret < 0) {
printk(KERN_WARNING "minibd: unable to get major number\n");
return -EBUSY;
}
printk(KERN_DEBUG "minibd: register success\n");
Miniblock = kmalloc(sizeof(struct miniblock_device), GFP_KERNEL);
if (Miniblock == NULL) {
printk(KERN_WARNING "minidb: unable to get memory with kmalloc\n");
goto out_unregister;
}
/*
* Device initialization
*/
memset(Miniblock, 0, sizeof(struct miniblock_device));
Miniblock->size = N_SECTORS*MINIBLOCK_SECTOR_SIZE;
Miniblock->data = vmalloc(Miniblock->size);
if (Miniblock->data == NULL) {
printk(KERN_WARNING "minidb: unable to get memory with vmalloc\n");
kfree(Miniblock);
goto out_unregister;
}
spin_lock_init(&Miniblock->lock);
/*
* request queue creation (one request per block device).
*/
miniblock_queue = blk_init_queue(miniblock_request, &Miniblock->lock);
/*
miniblock_queue = blk_alloc_queue( GFP_KERNEL );
blk_queue_make_request( miniblock_queue, miniblock_request );
blk_queue_hardsect_size( miniblock_queue, MINIBLOCK_SECTOR_SIZE);
blk_queue_max_sectors(miniblock_queue, N_SECTORS);
*/
if (miniblock_queue == NULL) {
printk(KERN_WARNING "minibd: error in blk_init_queue\n");
goto out_free;
}
//blk_queue_hardsect_size(miniblock_queue, MINIBLOCK_SECTOR_SIZE);
/*
* fill gendisk structure.
*/
Miniblock->gd = alloc_disk(MINIBD_MINORS);
if (!Miniblock->gd) {
printk(KERN_WARNING "minibd: error in alloc_disk\n");
goto out_free;
}
Miniblock->gd->major = MAJOR_NUM;
Miniblock->gd->first_minor = 0;
Miniblock->gd->fops = &miniblock_ops;
Miniblock->gd->private_data = Miniblock;
snprintf(Miniblock->gd->disk_name, 10, "%s", "minibd0");
set_capacity(Miniblock->gd, N_SECTORS*(MINIBLOCK_SECTOR_SIZE/KERNEL_SECTOR_SIZE));
Miniblock->gd->queue = miniblock_queue;
add_disk(Miniblock->gd);
return 0;
out_free:
kfree(Miniblock);
vfree(Miniblock->data);
out_unregister:
unregister_blkdev(MAJOR_NUM, "minibd");
return -ENOMEM;
}
static void __exit miniblock_exit(void)
{
del_gendisk(Miniblock->gd);
put_disk(Miniblock->gd);
unregister_blkdev(MAJOR_NUM, "minibd");
blk_cleanup_queue(miniblock_queue);
vfree(Miniblock->data);
kfree(Miniblock);
printk(KERN_DEBUG "minibd: ownload with succes!\n");
}
module_init(miniblock_init);
module_exit(miniblock_exit);