-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_disk.c
393 lines (348 loc) · 10 KB
/
my_disk.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/fs.h>
#include <linux/spinlock.h>
#include <linux/genhd.h> // For basic block driver framework
#include <linux/blkdev.h> // For at least, struct block_device_operations
#include <linux/hdreg.h>
#include <linux/types.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/errno.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tanya");
MODULE_DESCRIPTION("DISK ON RAM");
MODULE_ALIAS_BLOCKDEV_MAJOR(my_disk_major);
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) //macro defined to give size of array
#define SECTOR_SIZE 512
#define MBR_SIZE 512
#define MBR_DISK_SIGNATURE_OFFSET 440
#define MBR_DISK_SIGNATURE_SIZE 4
#define PARTITION_TABLE_OFFSET 446
#define PARTITION_ENTRY_SIZE 16
#define PARTITION_TABLE_SIZE 64
#define MBR_SIGNATURE_OFFSET 510
#define MBR_SIGNATURE_SIZE 2
#define MBR_SIGNATURE 0xAA55
#define BR_SIZE SECTOR_SIZE
#define BR_SIGNATURE_OFFSET 510
#define BR_SIGNATURE_SIZE 2
#define BR_SIGNATURE 0xAA55
#define MY_DISK_SECTOR_SIZE 512
#define MY_DISK_DEVICE_SIZE 1024 //no. of secotrs, so total size becomes = 1024 * 512 = 512KiB
#define MY_DISK_FIRST_MINOR 0
#define MY_DISK_MINOR_COUNT 16
static u_int my_disk_major = 0;
static u8 *dev_data;
typedef struct
{
unsigned char boot_type;
unsigned char start_head;
unsigned char start_sec:6;
unsigned char start_cyl_hi:2;
unsigned char start_cyl;
unsigned char part_type;
unsigned char end_head;
unsigned char end_sec:6;
unsigned char end_cyl_hi:2;
unsigned char end_cyl;
unsigned int abs_start_sec;
unsigned int sec_in_part;
} PartEntry;
typedef PartEntry PartTable[4];
static PartTable def_part_table =
{
{
boot_type: 0x00, //primary partition 1 which is inactive (0x00)
start_head: 0x00,
start_sec: 0x2,
start_cyl: 0x00,
part_type: 0x83, //partiton type = linux
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x09,
abs_start_sec: 0x00000001, // start of sector from 1
sec_in_part: 0x0000013F //tottal no of sectors in this partition = 319
},
{
boot_type: 0x00, //extended partition 2 which is inactive (0x00)
start_head: 0x00,
start_sec: 0x1,
start_cyl: 0x0A,
part_type: 0x05, //partiton type = linux
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x13,
abs_start_sec: 0x00000140, // start of sector from 320
sec_in_part: 0x00000140 //tottal no of sectors in this partition = 320
},
{
boot_type: 0x00, //primary partition 3 which is inactive (0x00)
start_head: 0x00,
start_sec: 0x1,
start_cyl: 0x14,
part_type: 0x07, //partiton type = NTFS
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x1F,
abs_start_sec: 0x00000280, // start of sector from 640
sec_in_part: 0x00000107 //tottal no of sectors in this partition = 263
},
{
boot_type: 0x00, //primary partition 4 which is inactive (0x00)
start_head: 0x00,
start_sec: 0x1,
start_cyl: 0x15,
part_type: 0x83, //partiton type = linux
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x1F,
abs_start_sec: 0x00000387, // start of sector from 903
sec_in_part: 0x00000079 //tottal no of sectors in this partition = 121
}
};
static unsigned int def_log_part_br_cyl[] = {0x0A, 0x0E, 0x12};
static const PartTable def_log_part_table[] =
{
{
{
boot_type: 0x00, //logical parition 1
start_head: 0x00,
start_sec: 0x2,
start_cyl: 0x0A,
part_type: 0x83, //partition type = linux
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x0D,
abs_start_sec: 0x00000001,
sec_in_part: 0x0000006F
},
{
boot_type: 0x00,
start_head: 0x00,
start_sec: 0x1,
start_cyl: 0x0E,
part_type: 0x05, //parition type is extended. This is for the next logical partiton
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x11,
abs_start_sec: 0x00000080,
sec_in_part: 0x00000080
},
},
{
{
boot_type: 0x00,
start_head: 0x00,
start_sec: 0x2,
start_cyl: 0x0E,
part_type: 0x24,
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x11,
abs_start_sec: 0x00000001,
sec_in_part: 0x0000006F
},
{
boot_type: 0x00,
start_head: 0x00,
start_sec: 0x1,
start_cyl: 0x12,
part_type: 0x05,
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x13,
abs_start_sec: 0x00000100,
sec_in_part: 0x00000040
},
},
{
{
boot_type: 0x00,
start_head: 0x00,
start_sec: 0x2,
start_cyl: 0x12,
part_type: 0x82,
end_head: 0x00,
end_sec: 0x20,
end_cyl: 0x13,
abs_start_sec: 0x00000001,
sec_in_part: 0x0000003F
},
}
};
static void copy_mbr(u8 *disk)
{
memset(disk, 0x0, MBR_SIZE);
*(unsigned long *)(disk + MBR_DISK_SIGNATURE_OFFSET) = 0x36E5756D;
memcpy(disk + PARTITION_TABLE_OFFSET, &def_part_table, PARTITION_TABLE_SIZE);
*(unsigned short *)(disk + MBR_SIGNATURE_OFFSET) = MBR_SIGNATURE;
}
static void copy_br(u8 *disk, int start_cylinder, const PartTable *part_table)
{
disk += (start_cylinder * 32 * SECTOR_SIZE);
memset(disk, 0x0, BR_SIZE);
memcpy(disk + PARTITION_TABLE_OFFSET, part_table,
PARTITION_TABLE_SIZE);
*(unsigned short *)(disk + BR_SIGNATURE_OFFSET) = BR_SIGNATURE;
}
void copy_mbr_n_br(u8 *disk)
{
int i;
copy_mbr(disk);
for (i = 0; i < ARRAY_SIZE(def_log_part_table); i++)
{
copy_br(disk, def_log_part_br_cyl[i], &def_log_part_table[i]);
}
}
static struct my_disk_device
{
unsigned int size; //size of the device in sectors
spinlock_t lock; //concurrent protection of request queue
short users; //how many users
struct request_queue *my_disk_queue; //device request queue
struct gendisk *my_disk_disk; //gendisk structure
}my_disk_dev;
/*
static int my_disk_open(struct block_device *bdev, fmode_t mode)
{
unsigned unit = iminor(bdev->bd_inode);
printk(KERN_INFO "MY_DISK: device opened\n");
printk(KERN_INFO "MY_DISK: inode number is %d\n", unit);
spin_lock(&my_disk_dev.lock);
if(unit > MY_DISK_MINOR_COUNT)
return -ENOMEM;
my_disk_dev.users++;
spin_unlock(&my_disk_dev.lock);
return 0;
}
static void my_disk_release(struct gendisk *disk, fmode_t mode)
{
printk(KERN_INFO "MY_DISK: device closed\n");
spin_lock(&my_disk_dev.lock);
my_disk_dev.users--;
spin_unlock(&my_disk_dev.lock);
}
static int my_disk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
geo->heads = 1;
geo->cylinders = 32;
geo->sectors = 32;
geo->start = 0;
return 0;
}
*/
static int my_disk_transfer(struct request *req, sector_t start_sector, unsigned int sector_cnt ,int write)
{
#define BV_PAGE(bv) ((bv).bv_page)
#define BV_OFFSET(bv) ((bv).bv_offset)
#define BV_LEN(bv) ((bv).bv_len)
struct bio_vec bv;
struct req_iterator iter;
sector_t sector_offset;
unsigned int sectors;
u8 *buffer;
int ret = 0;
sector_offset = 0;
rq_for_each_segment(bv, req, iter)
{
buffer = page_address(BV_PAGE(bv)) + BV_OFFSET(bv);
if(BV_LEN(bv) % MY_DISK_SECTOR_SIZE != 0)
{
printk(KERN_INFO "operation cannot happen because bio size (%d) is not a multiple of MY_DISK_SECTOR_SIZE(%d)\n", BV_LEN(bv), MY_DISK_SECTOR_SIZE);
ret = -EIO;
}
sectors = BV_LEN(bv) / MY_DISK_SECTOR_SIZE;
printk(KERN_INFO "MY_DISK: %llu, sector offset: %llu, buffer: %p, length: %u sectors\n", (unsigned long long)(start_sector), (unsigned long long)(sector_offset), buffer, sectors);
if(write)
memcpy(dev_data + (start_sector + sector_offset) * MY_DISK_SECTOR_SIZE, buffer, sectors * MY_DISK_SECTOR_SIZE);
else
memcpy(buffer, dev_data + (start_sector + sector_offset) * MY_DISK_SECTOR_SIZE, sectors * MY_DISK_SECTOR_SIZE);
sector_offset += sectors;
}
if(sector_offset != sector_cnt)
{
printk(KERN_ERR "MY_DISK: the required bio information doesnot matches with the request information");
ret = -EIO;
}
return ret;
}
static void my_disk_request(struct request_queue *q)
{
struct request *req;
int ret;
while((req = blk_fetch_request(q)) != NULL)
{
#if 0
if(!blk_fs_request(req))
{
printk(KERN_INFO "MY_DISK: skip non-fs request\n");
__blk_end_request_all(req, 0);
continue;
}
#endif
ret = my_disk_transfer(req, blk_rq_pos(req), blk_rq_sectors(req), rq_data_dir(req));
__blk_end_request_all(req, ret);
}
}
static struct block_device_operations my_disk_fops =
{
.owner = THIS_MODULE,
//.open = my_disk_open,
//.release= my_disk_release,
//.getgeo = my_disk_getgeo,
};
static int __init mydisk_init(void)
{
dev_data = vmalloc(MY_DISK_SECTOR_SIZE * MY_DISK_DEVICE_SIZE);
if(dev_data == NULL)
return -ENOMEM;
copy_mbr_n_br(dev_data);
my_disk_dev.size = MY_DISK_DEVICE_SIZE;
my_disk_major = register_blkdev(my_disk_major, "my_disk");
if(my_disk_major <= 0)
{
printk(KERN_ERR "MY_DISK: registeration failed\n");
vfree(dev_data);
return -EBUSY;
}
spin_lock_init(&my_disk_dev.lock);
my_disk_dev.my_disk_queue = blk_init_queue(my_disk_request, &my_disk_dev.lock);
if(my_disk_dev.my_disk_queue == NULL)
{
printk(KERN_ERR "MY_DISK: blk_init_queue failure\n");
unregister_blkdev(my_disk_major, "my_disk");
vfree(dev_data);
return -ENOMEM;
}
my_disk_dev.my_disk_disk = alloc_disk(MY_DISK_MINOR_COUNT);
if(!my_disk_dev.my_disk_disk)
{
printk(KERN_ERR "MY_DISK: alloc_disk failure\n");
blk_cleanup_queue(my_disk_dev.my_disk_queue);
unregister_blkdev(my_disk_major, "my_disk");
vfree(dev_data);
return -ENOMEM;
}
my_disk_dev.my_disk_disk->major = my_disk_major;
my_disk_dev.my_disk_disk->first_minor = MY_DISK_FIRST_MINOR;
my_disk_dev.my_disk_disk->fops = &my_disk_fops;
my_disk_dev.my_disk_disk->private_data = &my_disk_dev;
my_disk_dev.my_disk_disk->queue = my_disk_dev.my_disk_queue;
sprintf(my_disk_dev.my_disk_disk->disk_name, "my_disk");
set_capacity(my_disk_dev.my_disk_disk, my_disk_dev.size);
add_disk(my_disk_dev.my_disk_disk);
printk(KERN_INFO "MY_DISK: MY_DISK Block Driver initialised (%d sectors; %d bytes)\n",my_disk_dev.size, my_disk_dev.size * MY_DISK_SECTOR_SIZE);
return 0;
}
static void __exit mydisk_exit(void)
{
del_gendisk(my_disk_dev.my_disk_disk);
put_disk(my_disk_dev.my_disk_disk);
blk_cleanup_queue(my_disk_dev.my_disk_queue);
unregister_blkdev(my_disk_major, "my_disk");
vfree(dev_data);
}
module_init(mydisk_init);
module_exit(mydisk_exit);