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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This is a module for test purpose which will simulate error cases for bdev.
*/
#include "spdk/stdinc.h"
#include "spdk/rpc.h"
#include "spdk/conf.h"
#include "spdk/util.h"
#include "spdk/endian.h"
#include "spdk/nvme_spec.h"
#include "spdk/string.h"
#include "spdk/bdev_module.h"
#include "spdk_internal/log.h"
#include "vbdev_error.h"
struct spdk_vbdev_error_config {
char *base_bdev;
TAILQ_ENTRY(spdk_vbdev_error_config) tailq;
};
static TAILQ_HEAD(, spdk_vbdev_error_config) g_error_config
= TAILQ_HEAD_INITIALIZER(g_error_config);
struct vbdev_error_info {
uint32_t error_type;
uint32_t error_num;
};
/* Context for each error bdev */
struct error_disk {
struct spdk_bdev_part part;
struct vbdev_error_info error_vector[SPDK_BDEV_IO_TYPE_RESET];
TAILQ_HEAD(, spdk_bdev_io) pending_ios;
};
struct error_channel {
struct spdk_bdev_part_channel part_ch;
};
static pthread_mutex_t g_vbdev_error_mutex = PTHREAD_MUTEX_INITIALIZER;
static SPDK_BDEV_PART_TAILQ g_error_disks = TAILQ_HEAD_INITIALIZER(g_error_disks);
static int vbdev_error_init(void);
static void vbdev_error_fini(void);
static void vbdev_error_examine(struct spdk_bdev *bdev);
static int vbdev_error_config_json(struct spdk_json_write_ctx *w);
static int vbdev_error_config_add(const char *base_bdev_name);
static int vbdev_error_config_remove(const char *base_bdev_name);
static struct spdk_bdev_module error_if = {
.name = "error",
.module_init = vbdev_error_init,
.module_fini = vbdev_error_fini,
.examine_config = vbdev_error_examine,
.config_json = vbdev_error_config_json,
};
SPDK_BDEV_MODULE_REGISTER(error, &error_if)
int
vbdev_error_inject_error(char *name, uint32_t io_type, uint32_t error_type, uint32_t error_num)
{
struct spdk_bdev *bdev;
struct spdk_bdev_part *part;
struct error_disk *error_disk = NULL;
uint32_t i;
pthread_mutex_lock(&g_vbdev_error_mutex);
bdev = spdk_bdev_get_by_name(name);
if (!bdev) {
SPDK_ERRLOG("Could not find ErrorInjection bdev %s\n", name);
pthread_mutex_unlock(&g_vbdev_error_mutex);
return -ENODEV;
}
TAILQ_FOREACH(part, &g_error_disks, tailq) {
if (bdev == spdk_bdev_part_get_bdev(part)) {
error_disk = (struct error_disk *)part;
break;
}
}
if (error_disk == NULL) {
SPDK_ERRLOG("Could not find ErrorInjection bdev %s\n", name);
pthread_mutex_unlock(&g_vbdev_error_mutex);
return -ENODEV;
}
if (0xffffffff == io_type) {
for (i = 0; i < SPDK_COUNTOF(error_disk->error_vector); i++) {
error_disk->error_vector[i].error_type = error_type;
error_disk->error_vector[i].error_num = error_num;
}
} else if (0 == io_type) {
for (i = 0; i < SPDK_COUNTOF(error_disk->error_vector); i++) {
error_disk->error_vector[i].error_num = 0;
}
} else {
error_disk->error_vector[io_type].error_type = error_type;
error_disk->error_vector[io_type].error_num = error_num;
}
pthread_mutex_unlock(&g_vbdev_error_mutex);
return 0;
}
static void
vbdev_error_reset(struct error_disk *error_disk, struct spdk_bdev_io *bdev_io)
{
struct spdk_bdev_io *pending_io, *tmp;
TAILQ_FOREACH_SAFE(pending_io, &error_disk->pending_ios, module_link, tmp) {
TAILQ_REMOVE(&error_disk->pending_ios, pending_io, module_link);
spdk_bdev_io_complete(pending_io, SPDK_BDEV_IO_STATUS_FAILED);
}
spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
}
static uint32_t
vbdev_error_get_error_type(struct error_disk *error_disk, uint32_t io_type)
{
if (error_disk->error_vector[io_type].error_num) {
return error_disk->error_vector[io_type].error_type;
}
return 0;
}
static void
vbdev_error_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
{
struct error_channel *ch = spdk_io_channel_get_ctx(_ch);
struct error_disk *error_disk = bdev_io->bdev->ctxt;
uint32_t error_type;
switch (bdev_io->type) {
case SPDK_BDEV_IO_TYPE_READ:
case SPDK_BDEV_IO_TYPE_WRITE:
case SPDK_BDEV_IO_TYPE_UNMAP:
case SPDK_BDEV_IO_TYPE_FLUSH:
break;
case SPDK_BDEV_IO_TYPE_RESET:
vbdev_error_reset(error_disk, bdev_io);
return;
default:
SPDK_ERRLOG("Error Injection: unknown I/O type %d\n", bdev_io->type);
spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
return;
}
error_type = vbdev_error_get_error_type(error_disk, bdev_io->type);
if (error_type == 0) {
int rc = spdk_bdev_part_submit_request(&ch->part_ch, bdev_io);
if (rc) {
SPDK_ERRLOG("bdev_error: submit request failed, rc=%d\n", rc);
spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
}
return;
} else if (error_type == VBDEV_IO_FAILURE) {
error_disk->error_vector[bdev_io->type].error_num--;
spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
} else if (error_type == VBDEV_IO_PENDING) {
TAILQ_INSERT_TAIL(&error_disk->pending_ios, bdev_io, module_link);
error_disk->error_vector[bdev_io->type].error_num--;
}
}
static int
vbdev_error_destruct(void *ctx)
{
struct error_disk *error_disk = ctx;
struct spdk_bdev *base_bdev = spdk_bdev_part_get_base_bdev(&error_disk->part);
int rc;
rc = vbdev_error_config_remove(base_bdev->name);
if (rc != 0) {
SPDK_ERRLOG("vbdev_error_config_remove() failed\n");
}
return spdk_bdev_part_free(&error_disk->part);
}
static int
vbdev_error_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
{
struct error_disk *error_disk = ctx;
struct spdk_bdev *base_bdev = spdk_bdev_part_get_base_bdev(&error_disk->part);
spdk_json_write_named_object_begin(w, "error_disk");
spdk_json_write_named_string(w, "base_bdev", base_bdev->name);
spdk_json_write_object_end(w);
return 0;
}
static void
vbdev_error_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
{
/* No config per bdev. */
}
static struct spdk_bdev_fn_table vbdev_error_fn_table = {
.destruct = vbdev_error_destruct,
.submit_request = vbdev_error_submit_request,
.dump_info_json = vbdev_error_dump_info_json,
.write_config_json = vbdev_error_write_config_json
};
static void
vbdev_error_base_bdev_hotremove_cb(void *_part_base)
{
struct spdk_bdev_part_base *part_base = _part_base;
spdk_bdev_part_base_hotremove(part_base, &g_error_disks);
}
static int
_vbdev_error_create(struct spdk_bdev *base_bdev)
{
struct spdk_bdev_part_base *base = NULL;
struct error_disk *disk = NULL;
char *name;
int rc;
base = spdk_bdev_part_base_construct(base_bdev,
vbdev_error_base_bdev_hotremove_cb,
&error_if, &vbdev_error_fn_table, &g_error_disks,
NULL, NULL, sizeof(struct error_channel),
NULL, NULL);
if (!base) {
SPDK_ERRLOG("could not construct part base for bdev %s\n", spdk_bdev_get_name(base_bdev));
return -ENOMEM;
}
disk = calloc(1, sizeof(*disk));
if (!disk) {
SPDK_ERRLOG("Memory allocation failure\n");
spdk_bdev_part_base_free(base);
return -ENOMEM;
}
name = spdk_sprintf_alloc("EE_%s", spdk_bdev_get_name(base_bdev));
if (!name) {
SPDK_ERRLOG("name allocation failure\n");
spdk_bdev_part_base_free(base);
free(disk);
return -ENOMEM;
}
rc = spdk_bdev_part_construct(&disk->part, base, name, 0, base_bdev->blockcnt,
"Error Injection Disk");
free(name);
if (rc) {
SPDK_ERRLOG("could not construct part for bdev %s\n", spdk_bdev_get_name(base_bdev));
/* spdk_bdev_part_construct will free name on failure */
spdk_bdev_part_base_free(base);
free(disk);
return rc;
}
TAILQ_INIT(&disk->pending_ios);
return 0;
}
int
vbdev_error_create(const char *base_bdev_name)
{
int rc;
struct spdk_bdev *base_bdev;
rc = vbdev_error_config_add(base_bdev_name);
if (rc != 0) {
SPDK_ERRLOG("Adding config for ErrorInjection bdev %s failed (rc=%d)\n",
base_bdev_name, rc);
return rc;
}
base_bdev = spdk_bdev_get_by_name(base_bdev_name);
if (!base_bdev) {
return 0;
}
rc = _vbdev_error_create(base_bdev);
if (rc != 0) {
vbdev_error_config_remove(base_bdev_name);
SPDK_ERRLOG("Could not create ErrorInjection bdev %s (rc=%d)\n",
base_bdev_name, rc);
}
return rc;
}
void
vbdev_error_delete(struct spdk_bdev *vbdev, spdk_delete_error_complete cb_fn, void *cb_arg)
{
if (!vbdev || vbdev->module != &error_if) {
cb_fn(cb_arg, -ENODEV);
return;
}
spdk_bdev_unregister(vbdev, cb_fn, cb_arg);
}
static void
vbdev_error_clear_config(void)
{
struct spdk_vbdev_error_config *cfg;
while ((cfg = TAILQ_FIRST(&g_error_config))) {
TAILQ_REMOVE(&g_error_config, cfg, tailq);
free(cfg->base_bdev);
free(cfg);
}
}
static struct spdk_vbdev_error_config *
vbdev_error_config_find_by_base_name(const char *base_bdev_name)
{
struct spdk_vbdev_error_config *cfg;
TAILQ_FOREACH(cfg, &g_error_config, tailq) {
if (strcmp(cfg->base_bdev, base_bdev_name) == 0) {
return cfg;
}
}
return NULL;
}
static int
vbdev_error_config_add(const char *base_bdev_name)
{
struct spdk_vbdev_error_config *cfg;
cfg = vbdev_error_config_find_by_base_name(base_bdev_name);
if (cfg) {
SPDK_ERRLOG("vbdev_error_config for bdev %s already exists\n",
base_bdev_name);
return -EEXIST;
}
cfg = calloc(1, sizeof(*cfg));
if (!cfg) {
SPDK_ERRLOG("calloc() failed for vbdev_error_config\n");
return -ENOMEM;
}
cfg->base_bdev = strdup(base_bdev_name);
if (!cfg->base_bdev) {
free(cfg);
SPDK_ERRLOG("strdup() failed for base_bdev_name\n");
return -ENOMEM;
}
TAILQ_INSERT_TAIL(&g_error_config, cfg, tailq);
return 0;
}
static int
vbdev_error_config_remove(const char *base_bdev_name)
{
struct spdk_vbdev_error_config *cfg;
cfg = vbdev_error_config_find_by_base_name(base_bdev_name);
if (!cfg) {
return -ENOENT;
}
TAILQ_REMOVE(&g_error_config, cfg, tailq);
free(cfg->base_bdev);
free(cfg);
return 0;
}
static int
vbdev_error_init(void)
{
struct spdk_conf_section *sp;
struct spdk_vbdev_error_config *cfg;
const char *base_bdev_name;
int i, rc;
sp = spdk_conf_find_section(NULL, "BdevError");
if (sp == NULL) {
return 0;
}
for (i = 0; ; i++) {
if (!spdk_conf_section_get_nval(sp, "BdevError", i)) {
break;
}
base_bdev_name = spdk_conf_section_get_nmval(sp, "BdevError", i, 0);
if (!base_bdev_name) {
SPDK_ERRLOG("ErrorInjection configuration missing bdev name\n");
rc = -EINVAL;
goto error;
}
cfg = calloc(1, sizeof(*cfg));
if (!cfg) {
SPDK_ERRLOG("calloc() failed for vbdev_error_config\n");
rc = -ENOMEM;
goto error;
}
cfg->base_bdev = strdup(base_bdev_name);
if (!cfg->base_bdev) {
free(cfg);
SPDK_ERRLOG("strdup() failed for bdev name\n");
rc = -ENOMEM;
goto error;
}
TAILQ_INSERT_TAIL(&g_error_config, cfg, tailq);
}
return 0;
error:
vbdev_error_clear_config();
return rc;
}
static void
vbdev_error_fini(void)
{
vbdev_error_clear_config();
}
static void
vbdev_error_examine(struct spdk_bdev *bdev)
{
struct spdk_vbdev_error_config *cfg;
int rc;
cfg = vbdev_error_config_find_by_base_name(bdev->name);
if (cfg != NULL) {
rc = _vbdev_error_create(bdev);
if (rc != 0) {
SPDK_ERRLOG("could not create error vbdev for bdev %s at examine\n",
bdev->name);
}
}
spdk_bdev_module_examine_done(&error_if);
}
static int
vbdev_error_config_json(struct spdk_json_write_ctx *w)
{
struct spdk_vbdev_error_config *cfg;
TAILQ_FOREACH(cfg, &g_error_config, tailq) {
spdk_json_write_object_begin(w);
spdk_json_write_named_string(w, "method", "bdev_error_create");
spdk_json_write_named_object_begin(w, "params");
spdk_json_write_named_string(w, "base_name", cfg->base_bdev);
spdk_json_write_object_end(w);
spdk_json_write_object_end(w);
}
return 0;
}
|