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
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/bio.h>
#include <linux/bvec.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/stat.h>
#include <linux/slab.h>
#include <linux/numa.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
#include <linux/moduleparam.h>
#include <linux/spinlock_types.h>
#define SBDD_NAME "sbdd"
#define SBDD_BDEV_MODE (FMODE_READ | FMODE_WRITE)
struct sbdd {
wait_queue_head_t exitwait;
spinlock_t datalock;
atomic_t deleting;
atomic_t refs_cnt;
sector_t capacity;
struct gendisk *gd;
struct request_queue *q;
struct block_device *bdev;
};
static struct sbdd __sbdd;
static int __sbdd_major = 0;
static char *__sbdd_disk = "/dev/disk/by-id/ata-QEMU_HARDDISK_QM00001";
/*static sector_t sbdd_xfer(struct bio_vec* bvec, sector_t pos, int dir)
{
void *buff = page_address(bvec->bv_page) + bvec->bv_offset;
sector_t len = bvec->bv_len >> SBDD_SECTOR_SHIFT;
size_t offset;
size_t nbytes;
if (pos + len > __sbdd.capacity)
len = __sbdd.capacity - pos;
offset = pos << SBDD_SECTOR_SHIFT;
nbytes = len << SBDD_SECTOR_SHIFT;
spin_lock(&__sbdd.datalock);
if (dir)
memcpy(__sbdd.data + offset, buff, nbytes);
else
memcpy(buff, __sbdd.data + offset, nbytes);
spin_unlock(&__sbdd.datalock);
pr_debug("pos=%6llu len=%4llu %s\n", pos, len, dir ? "written" : "read");
return len;
}*/
static void sbdd_xfer_bio(struct bio *bio)
{
/* TODO: send request to another device */
}
static blk_qc_t sbdd_make_request(struct request_queue *q, struct bio *bio)
{
if (atomic_read(&__sbdd.deleting)) {
pr_err("unable to process bio while deleting\n");
bio_io_error(bio);
return BLK_STS_IOERR;
}
atomic_inc(&__sbdd.refs_cnt);
sbdd_xfer_bio(bio);
bio_endio(bio);
if (atomic_dec_and_test(&__sbdd.refs_cnt))
wake_up(&__sbdd.exitwait);
return BLK_STS_OK;
}
/*
There are no read or write operations. These operations are performed by
the request() function associated with the request queue of the disk.
*/
static struct block_device_operations const __sbdd_bdev_ops = {
.owner = THIS_MODULE,
};
static int sbdd_create(void)
{
int ret = 0;
unsigned short lblock_size;
unsigned int max_sectors;
/*
This call is somewhat redundant, but used anyways by tradition.
The number is to be displayed in /proc/devices (0 for auto).
*/
pr_info("registering blkdev\n");
__sbdd_major = register_blkdev(0, SBDD_NAME);
if (__sbdd_major < 0) {
pr_err("call register_blkdev() failed with %d\n", __sbdd_major);
return -EBUSY;
}
memset(&__sbdd, 0, sizeof(struct sbdd));
spin_lock_init(&__sbdd.datalock);
init_waitqueue_head(&__sbdd.exitwait);
pr_info("allocating queue\n");
__sbdd.q = blk_alloc_queue(GFP_KERNEL);
if (!__sbdd.q) {
pr_err("call blk_alloc_queue() failed\n");
return -EINVAL;
}
blk_queue_make_request(__sbdd.q, sbdd_make_request);
/* A disk must have at least one minor */
pr_info("allocating disk\n");
__sbdd.gd = alloc_disk(1);
/* Get a handle on the device */
pr_info("opening %s\n", __sbdd_disk);
__sbdd.bdev = blkdev_get_by_path(__sbdd_disk, SBDD_BDEV_MODE, THIS_MODULE);
if (!__sbdd.bdev || IS_ERR(__sbdd.bdev)) {
pr_err("blkdev_get_by_path(\"%s\") failed with %ld\n",
__sbdd_disk, PTR_ERR(__sbdd.bdev));
return -ENOENT;
}
/* Configure queue */
lblock_size = bdev_logical_block_size(__sbdd.bdev);
blk_queue_logical_block_size(__sbdd.q, lblock_size);
pr_info("\tlogical block size: %u\n", lblock_size);
/* Configure gendisk */
__sbdd.gd->queue = __sbdd.q;
__sbdd.gd->major = __sbdd_major;
__sbdd.gd->first_minor = 0;
__sbdd.gd->fops = &__sbdd_bdev_ops;
/* Represents name in /proc/partitions and /sys/block */
scnprintf(__sbdd.gd->disk_name, DISK_NAME_LEN, SBDD_NAME);
__sbdd.capacity = get_capacity(__sbdd.bdev->bd_disk);
set_capacity(__sbdd.gd, __sbdd.capacity);
pr_info("\tdevice capacity: %llu\n", __sbdd.capacity);
max_sectors = queue_max_hw_sectors(bdev_get_queue(__sbdd.bdev));
blk_queue_max_hw_sectors(__sbdd.q, max_sectors);
pr_info("\tmax sectors: %u\n", max_sectors);
/*
Allocating gd does not make it available, add_disk() required.
After this call, gd methods can be called at any time. Should not be
called before the driver is fully initialized and ready to process reqs.
*/
pr_info("adding disk\n");
add_disk(__sbdd.gd);
return ret;
}
static void sbdd_delete(void)
{
atomic_set(&__sbdd.deleting, 1);
wait_event(__sbdd.exitwait, !atomic_read(&__sbdd.refs_cnt));
/* gd will be removed only after the last reference put */
if (__sbdd.gd) {
pr_info("deleting disk\n");
del_gendisk(__sbdd.gd);
}
if (__sbdd.bdev) {
pr_info("release a handle on the %s\n", __sbdd_disk);
blkdev_put(__sbdd.bdev, SBDD_BDEV_MODE);
}
if (__sbdd.q) {
pr_info("cleaning up queue\n");
blk_cleanup_queue(__sbdd.q);
}
if (__sbdd.gd)
put_disk(__sbdd.gd);
memset(&__sbdd, 0, sizeof(struct sbdd));
if (__sbdd_major > 0) {
pr_info("unregistering blkdev\n");
unregister_blkdev(__sbdd_major, SBDD_NAME);
__sbdd_major = 0;
}
}
/*
Note __init is for the kernel to drop this function after
initialization complete making its memory available for other uses.
There is also __initdata note, same but used for variables.
*/
static int __init sbdd_init(void)
{
int ret = 0;
pr_info("starting initialization...\n");
ret = sbdd_create();
if (ret) {
pr_warn("initialization failed\n");
sbdd_delete();
} else {
pr_info("initialization complete\n");
}
return ret;
}
/*
Note __exit is for the compiler to place this code in a special ELF section.
Sometimes such functions are simply discarded (e.g. when module is built
directly into the kernel). There is also __exitdata note.
*/
static void __exit sbdd_exit(void)
{
pr_info("exiting...\n");
sbdd_delete();
pr_info("exiting complete\n");
}
/* Called on module loading. Is mandatory. */
module_init(sbdd_init);
/* Called on module unloading. Unloading module is not allowed without it. */
module_exit(sbdd_exit);
/* Set desired target disk with insmod */
module_param_named(disk, __sbdd_disk, charp, S_IRUGO);
/* Note for the kernel: a free license module. A warning will be outputted without it. */
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Block Device Driver");
|