-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassthrough.c
executable file
·153 lines (119 loc) · 3.48 KB
/
passthrough.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
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/timer.h>
#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/hdreg.h> /* HDIO_GETGEO */
#include <linux/kdev_t.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
MODULE_LICENSE("Dual BSD/GPL");
#define TARGET_NAME "/dev/sdd"
struct pt_dev {
int major;
struct request_queue *queue;
struct gendisk *gd;
struct block_device *target_dev;
};
static struct pt_dev *passthrough;
static int passthrough_make_request(struct request_queue *q, struct bio *bio)
{
bio->bi_bdev = passthrough->target_dev;
return 1;
}
int pt_getgeo(struct block_device * block_device, struct hd_geometry * hg)
{
hg->heads = 255;
hg->sectors = 63;
hg->cylinders = get_capacity(block_device->bd_disk);
sector_div(hg->cylinders, hg->heads * hg->sectors);
return 0;
}
static struct block_device_operations pt_ops = {
.owner = THIS_MODULE,
.getgeo = pt_getgeo
};
static int setup_passthrough_device(struct pt_dev *dev, const char *target_name)
{
struct request_queue *q;
dev->queue = blk_alloc_queue(GFP_KERNEL);
if (dev->queue == NULL)
return -1;
blk_queue_make_request(dev->queue, passthrough_make_request);
blk_queue_flush(dev->queue, REQ_FLUSH | REQ_FUA);
dev->gd = alloc_disk(1);
if (! dev->gd) {
return -1;
}
dev->gd->major = passthrough->major;
dev->gd->first_minor = 0;
dev->gd->fops = &pt_ops;
dev->gd->queue = dev->queue;
dev->gd->private_data = dev;
dev->gd->flags |= GENHD_FL_EXT_DEVT;
dev->target_dev = blkdev_get_by_path(target_name, FMODE_READ|FMODE_WRITE|FMODE_EXCL, dev);
if(!dev->target_dev)
{
return -1;
}
if(!dev->target_dev->bd_disk)
{
return -1;
}
q = bdev_get_queue(dev->target_dev);
if(!q)
{
return -1;
}
dev->gd->queue->limits.max_hw_sectors = q->limits.max_hw_sectors;
dev->gd->queue->limits.max_sectors = q->limits.max_sectors;
dev->gd->queue->limits.max_segment_size = q->limits.max_segment_size;
dev->gd->queue->limits.max_segments = q->limits.max_segments;
dev->gd->queue->limits.logical_block_size = 512;
dev->gd->queue->limits.physical_block_size = 512;
set_bit(QUEUE_FLAG_NONROT, &dev->gd->queue->queue_flags);
snprintf (dev->gd->disk_name, 32, "passthrough");
set_capacity(dev->gd, get_capacity(dev->target_dev->bd_disk));
add_disk(dev->gd);
return 1;
}
static int __init pt_init(void)
{
passthrough = kzalloc(sizeof(struct pt_dev), GFP_KERNEL);
if(!passthrough)
{
return -ENOMEM;
}
passthrough->major = register_blkdev(0, "passthrough");
if (passthrough->major <= 0) {
return -EBUSY;
}
if(!setup_passthrough_device(passthrough, TARGET_NAME))
{
unregister_blkdev(passthrough->major, "passthrough");
return -ENOMEM;
}
return 0;
}
static void pt_exit(void)
{
if (passthrough->gd) {
del_gendisk(passthrough->gd);
put_disk(passthrough->gd);
}
if (passthrough->queue)
blk_cleanup_queue(passthrough->queue);
blkdev_put(passthrough->target_dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
unregister_blkdev(passthrough->major, "passthrough");
kfree(passthrough);
}
subsys_initcall(pt_init);
module_exit(pt_exit);