-
Notifications
You must be signed in to change notification settings - Fork 5
/
dm-fake-discard.c
278 lines (224 loc) · 6.01 KB
/
dm-fake-discard.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
/*
* Copyright (C) 2012 RedHat.
*
* This file is released under the GPL.
*/
#include "dm.h"
#include <linux/module.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/slab.h>
#include <linux/device-mapper.h>
#define DM_MSG_PREFIX "fake-discard"
struct fake_discard {
struct dm_dev *dev;
sector_t offset;
bool supports_discard;
bool discard_zeroes_data;
unsigned discard_granularity;
unsigned max_discard_sectors;
};
/*----------------------------------------------------------------*/
static int parse_features(struct dm_arg_set *as, struct fake_discard *fd,
struct dm_target *ti)
{
int r;
unsigned argc;
const char *arg_name;
static struct dm_arg _args[] = {
{0, 2, "Invalid number of feature arguments"}
};
fd->supports_discard = true;
fd->discard_zeroes_data = false;
if (!as->argc)
return 0;
r = dm_read_arg_group(_args, as, &argc, &ti->error);
if (r)
return r;
while (argc && !r) {
arg_name = dm_shift_arg(as);
argc--;
if (!strcasecmp(arg_name, "no_discard_support"))
fd->supports_discard = false;
else if (!strcasecmp(arg_name, "discard_zeroes_data"))
fd->discard_zeroes_data = true;
else {
ti->error = "Unrecognised feature requested";
r = -EINVAL;
break;
}
}
return r;
}
/*
* fake-discard <dev path>
* <offset>
* <granularity>
* <max discard sectors>
* [<#feature args> <arg>*]
*
* Optional feature arguments are:
* no_discard_support
* discard_zeroes_data
*/
static int discard_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
struct fake_discard *fd;
unsigned long long tmp;
struct dm_arg_set as;
char dummy;
if (argc < 4) {
ti->error = "Invalid argument count";
return -EINVAL;
}
fd = kmalloc(sizeof(*fd), GFP_KERNEL);
if (!fd) {
ti->error = "out of memory";
return -ENOMEM;
}
if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1) {
ti->error = "Invalid offset sector";
goto bad;
}
fd->offset = tmp;
if (sscanf(argv[2], "%u%c", &fd->discard_granularity, &dummy) != 1) {
ti->error = "Invalid discard granularity";
goto bad;
}
if (!is_power_of_2(fd->discard_granularity)) {
ti->error = "Discard granularity must be a power of 2";
goto bad;
}
if (sscanf(argv[3], "%u%c", &fd->max_discard_sectors, &dummy) != 1) {
ti->error = "Invalid max discard sectors";
goto bad;
}
if (fd->discard_granularity > fd->max_discard_sectors) {
ti->error = "Discard granularity cannot be larger than max discard sectors";
goto bad;
}
as.argc = argc;
as.argv = argv;
dm_consume_args(&as, 4);
if (parse_features(&as, fd, ti))
goto bad;
if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &fd->dev)) {
ti->error = "dm-linear: Device lookup failed";
goto bad;
}
if (fd->supports_discard) {
pr_alert("fake_discard: discards supported\n");
ti->discards_supported = true;
ti->discards_unsupported = false;
ti->num_discard_bios = 1;
} else {
pr_alert("fake_discard: discards unsupported\n");
ti->discards_supported = false;
ti->discards_unsupported = true;
ti->num_discard_bios = 0;
}
if (fd->discard_zeroes_data) {
ti->pretend_discard_zeroes_data = true;
ti->discard_zeroes_data_unsupported = false;
} else {
ti->pretend_discard_zeroes_data = false;
ti->discard_zeroes_data_unsupported = true;
}
ti->num_flush_bios = 1;
ti->private = fd;
return 0;
bad:
kfree(fd);
return -EINVAL;
}
static void discard_dtr(struct dm_target *ti)
{
struct fake_discard *fd = ti->private;
dm_put_device(ti, fd->dev);
kfree(fd);
}
static int discard_map(struct dm_target *ti, struct bio *bio)
{
struct fake_discard *fd = ti->private;
if (bio->bi_rw & REQ_DISCARD) {
if (fd->supports_discard) {
bio_endio(bio, 0);
return DM_MAPIO_SUBMITTED;
} else
bio_endio(bio, -ENOTSUPP);
} else {
bio->bi_bdev = fd->dev->bdev;
bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector) + fd->offset;
}
return DM_MAPIO_REMAPPED;
}
static void discard_status(struct dm_target *ti, status_type_t type,
unsigned status_flags, char *result, unsigned maxlen)
{
struct fake_discard *fd = ti->private;
switch (type) {
case STATUSTYPE_INFO:
result[0] = '\0';
break;
case STATUSTYPE_TABLE:
snprintf(result, maxlen, "%s %llu %u %u %u",
fd->dev->name,
(unsigned long long) fd->offset,
fd->discard_granularity,
fd->max_discard_sectors,
fd->discard_zeroes_data);
break;
}
}
static int discard_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
struct bio_vec *biovec, int max_size)
{
struct fake_discard *fd = ti->private;
struct request_queue *q = bdev_get_queue(fd->dev->bdev);
if (!q->merge_bvec_fn)
return max_size;
bvm->bi_bdev = fd->dev->bdev;
bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector) + fd->offset;
return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
}
static int discard_iterate_devices(struct dm_target *ti,
iterate_devices_callout_fn fn, void *data)
{
struct fake_discard *fd = ti->private;
return fn(ti, fd->dev, fd->offset, ti->len, data);
}
static void discard_io_hints(struct dm_target *ti, struct queue_limits *limits)
{
struct fake_discard *fd = ti->private;
limits->discard_granularity = fd->discard_granularity << SECTOR_SHIFT;
limits->max_discard_sectors = fd->max_discard_sectors;
}
static struct target_type fake_discard_target = {
.name = "fake-discard",
.version = {1, 0, 0},
.module = THIS_MODULE,
.ctr = discard_ctr,
.dtr = discard_dtr,
.map = discard_map,
.status = discard_status,
.merge = discard_merge,
.iterate_devices = discard_iterate_devices,
.io_hints = discard_io_hints,
};
static int __init dm_discard_init(void)
{
int r = dm_register_target(&fake_discard_target);
if (r < 0)
DMERR("register failed %d", r);
return r;
}
static void dm_discard_exit(void)
{
dm_unregister_target(&fake_discard_target);
}
module_init(dm_discard_init);
module_exit(dm_discard_exit);
MODULE_DESCRIPTION(DM_NAME " fake discard target");
MODULE_AUTHOR("Joe Thornber <joe.thornber@gmail.com>");
MODULE_LICENSE("GPL");