-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-passthru.c
257 lines (208 loc) · 6.55 KB
/
dm-passthru.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
#include <linux/bio.h>
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/blkdev.h>
#include <linux/device-mapper.h>
#include <linux/init.h>
#include <linux/mempool.h>
#include <linux/module.h>
#include <linux/slab.h>
#define DM_MSG_PREFIX "passthru"
/*
* Passthru: A passthrough target that performs reads and writes via a
* workqueue.
*/
struct passthru_c {
struct dm_dev *dev;
mempool_t *io_pool; /* For per bio private data. */
struct bio_set *bs; /* For cloned bios. */
struct workqueue_struct *io_queue;
};
struct passthru_io {
struct passthru_c *pc;
struct bio *base_bio;
struct work_struct work;
int error;
atomic_t io_pending;
};
#define MIN_IOS 16
#define MIN_POOL_PAGES 32
static struct kmem_cache *_passthru_io_pool;
static void passthru_dtr(struct dm_target *ti)
{
struct passthru_c *pc = (struct passthru_c *) ti->private;
DMERR("Passthru: destructing...");
ti->private = NULL;
if (!pc)
return;
if (pc->io_queue)
destroy_workqueue(pc->io_queue);
if (pc->bs)
bioset_free(pc->bs);
if (pc->io_pool)
mempool_destroy(pc->io_pool);
if (pc->dev)
dm_put_device(ti, pc->dev);
kfree(pc);
}
/*
* Construct a passthru mapping: <dev_path>
*/
static int passthru_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
struct passthru_c *pc;
int ret;
DMERR("Passthru: constructing...");
if (argc != 1) {
ti->error = "Invalid argument count";
return -EINVAL;
}
pc = kmalloc(sizeof(*pc), GFP_KERNEL);
if (!pc) {
ti->error = "dm-passthru: Cannot allocate passthru context";
return -ENOMEM;
}
ti->private = pc;
ret = -ENOMEM;
pc->io_pool = mempool_create_slab_pool(MIN_IOS, _passthru_io_pool);
if (!pc->io_pool) {
ti->error = "Cannot allocate passthru io mempool";
goto bad;
}
pc->bs = bioset_create(MIN_IOS, 0);
if (!pc->bs) {
ti->error = "Cannot allocate crypt bioset";
goto bad;
}
pc->io_queue = alloc_workqueue("passthrud_io",
WQ_NON_REENTRANT|WQ_MEM_RECLAIM,
1);
if (!pc->io_queue) {
ti->error = "Cannot allocated passthrud io queue";
goto bad;
}
ret = -EINVAL;
if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pc->dev)) {
ti->error = "dm-passthru: Device lookup failed";
goto bad;
}
ti->num_flush_bios = 1;
ti->num_discard_bios = 1;
ti->num_write_same_bios = 1;
return 0;
bad:
passthru_dtr(ti);
return ret;
}
static struct passthru_io *passthru_io_alloc(struct passthru_c *pc,
struct bio *bio)
{
struct passthru_io *io;
io = mempool_alloc(pc->io_pool, GFP_NOIO);
io->pc = pc;
io->base_bio = bio;
io->error = 0;
atomic_set(&io->io_pending, 1);
return io;
}
static void passthru_io_release(struct passthru_io *io)
{
struct passthru_c *pc = io->pc;
struct bio *base_bio = io->base_bio;
int error = io->error;
BUG_ON(!atomic_dec_and_test(io->io_pending));
mempool_free(io, pc->io_pool);
bio_endio(base_bio, error);
}
static void passthru_endio(struct bio *clone, int error)
{
struct passthru_io *io = clone->bi_private;
if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
io->error = -EIO;
DMERR("%s %c %lu",
(io->error ? "!!" : "OK"),
(bio_data_dir(clone) == READ ? 'R' : 'W'),
clone->bi_sector);
bio_put(clone);
passthru_io_release(io);
}
static void clone_init(struct passthru_io *io, struct bio *clone)
{
struct passthru_c *pc = io->pc;
clone->bi_private = io;
clone->bi_end_io = passthru_endio;
clone->bi_bdev = pc->dev->bdev;
}
static void passthrud_io(struct work_struct *work)
{
struct passthru_io *io = container_of(work, struct passthru_io, work);
struct passthru_c *pc = io->pc;
struct bio *base_bio = io->base_bio;
struct bio *clone;
clone = bio_clone_bioset(base_bio, GFP_NOIO, pc->bs);
if (!clone) {
io->error = -ENOMEM;
passthru_io_release(io);
return;
}
clone_init(io, clone);
generic_make_request(clone);
}
static void passthru_queue_io(struct passthru_io *io)
{
struct passthru_c *pc = io->pc;
INIT_WORK(&io->work, passthrud_io);
queue_work(pc->io_queue, &io->work);
}
static int passthru_map(struct dm_target *ti, struct bio *bio)
{
struct passthru_io *io;
struct passthru_c *pc = ti->private;
io = passthru_io_alloc(pc, bio);
passthru_queue_io(io);
return DM_MAPIO_SUBMITTED;
}
static void passthru_status(struct dm_target *ti, status_type_t type,
unsigned status_flags, char *result, unsigned maxlen)
{
struct passthru_c *pc = (struct passthru_c *) ti->private;
switch (type) {
case STATUSTYPE_INFO:
result[0] = '\0';
break;
case STATUSTYPE_TABLE:
snprintf(result, maxlen, "%s", pc->dev->name);
break;
}
}
static struct target_type passthru_target = {
.name = "passthru",
.version = {1, 2, 1},
.module = THIS_MODULE,
.ctr = passthru_ctr,
.dtr = passthru_dtr,
.map = passthru_map,
.status = passthru_status,
};
int __init dm_passthru_init(void)
{
int r;
_passthru_io_pool = KMEM_CACHE(passthru_io, 0);
if (!_passthru_io_pool)
return -ENOMEM;
r = dm_register_target(&passthru_target);
if (r < 0) {
DMERR("register failed %d", r);
kmem_cache_destroy(_passthru_io_pool);
}
return r;
}
void dm_passthru_exit(void)
{
dm_unregister_target(&passthru_target);
}
module_init(dm_passthru_init)
module_exit(dm_passthru_exit)
MODULE_AUTHOR("Abutalib Aghayev <[email protected]>");
MODULE_DESCRIPTION(DM_NAME " passthrough target with workqueues.");
MODULE_LICENSE("GPL");