LCOV - code coverage report
Current view: top level - spdk/module/bdev/error - vbdev_error.c (source / functions) Hit Total Coverage
Test: Combined Lines: 169 253 66.8 %
Date: 2024-07-15 10:08:26 Functions: 20 24 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 65 126 51.6 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : /*
       7                 :            :  * This is a module for test purpose which will simulate error cases for bdev.
       8                 :            :  */
       9                 :            : 
      10                 :            : #include "spdk/stdinc.h"
      11                 :            : #include "spdk/rpc.h"
      12                 :            : #include "spdk/util.h"
      13                 :            : #include "spdk/endian.h"
      14                 :            : #include "spdk/nvme_spec.h"
      15                 :            : #include "spdk/string.h"
      16                 :            : 
      17                 :            : #include "spdk/bdev_module.h"
      18                 :            : #include "spdk/log.h"
      19                 :            : 
      20                 :            : #include "vbdev_error.h"
      21                 :            : 
      22                 :            : struct spdk_vbdev_error_config {
      23                 :            :         char *base_bdev;
      24                 :            :         struct spdk_uuid uuid;
      25                 :            :         TAILQ_ENTRY(spdk_vbdev_error_config) tailq;
      26                 :            : };
      27                 :            : 
      28                 :            : static TAILQ_HEAD(, spdk_vbdev_error_config) g_error_config
      29                 :            :         = TAILQ_HEAD_INITIALIZER(g_error_config);
      30                 :            : 
      31                 :            : struct vbdev_error_info {
      32                 :            :         uint32_t                        error_type;
      33                 :            :         uint32_t                        error_num;
      34                 :            :         uint64_t                        error_qd;
      35                 :            :         uint64_t                        corrupt_offset;
      36                 :            :         uint8_t                         corrupt_value;
      37                 :            : };
      38                 :            : 
      39                 :            : struct error_io {
      40                 :            :         enum vbdev_error_type error_type;
      41                 :            :         TAILQ_ENTRY(error_io) link;
      42                 :            : };
      43                 :            : 
      44                 :            : /* Context for each error bdev */
      45                 :            : struct error_disk {
      46                 :            :         struct spdk_bdev_part           part;
      47                 :            :         struct vbdev_error_info         error_vector[SPDK_BDEV_IO_TYPE_RESET];
      48                 :            :         TAILQ_HEAD(, error_io)  pending_ios;
      49                 :            : };
      50                 :            : 
      51                 :            : struct error_channel {
      52                 :            :         struct spdk_bdev_part_channel   part_ch;
      53                 :            :         uint64_t                        io_inflight;
      54                 :            : };
      55                 :            : 
      56                 :            : static pthread_mutex_t g_vbdev_error_mutex = PTHREAD_MUTEX_INITIALIZER;
      57                 :            : static SPDK_BDEV_PART_TAILQ g_error_disks = TAILQ_HEAD_INITIALIZER(g_error_disks);
      58                 :            : 
      59                 :            : static int vbdev_error_init(void);
      60                 :            : static void vbdev_error_fini(void);
      61                 :            : 
      62                 :            : static void vbdev_error_examine(struct spdk_bdev *bdev);
      63                 :            : static int vbdev_error_config_json(struct spdk_json_write_ctx *w);
      64                 :            : 
      65                 :            : static int vbdev_error_config_add(const char *base_bdev_name, const struct spdk_uuid *uuid);
      66                 :            : static int vbdev_error_config_remove(const char *base_bdev_name);
      67                 :            : 
      68                 :            : static int
      69                 :       2140 : vbdev_error_get_ctx_size(void)
      70                 :            : {
      71                 :       2140 :         return sizeof(struct error_io);
      72                 :            : }
      73                 :            : 
      74                 :            : static struct spdk_bdev_module error_if = {
      75                 :            :         .name = "error",
      76                 :            :         .module_init = vbdev_error_init,
      77                 :            :         .module_fini = vbdev_error_fini,
      78                 :            :         .examine_config = vbdev_error_examine,
      79                 :            :         .config_json = vbdev_error_config_json,
      80                 :            :         .get_ctx_size = vbdev_error_get_ctx_size,
      81                 :            : 
      82                 :            : };
      83                 :            : 
      84                 :       2330 : SPDK_BDEV_MODULE_REGISTER(error, &error_if)
      85                 :            : 
      86                 :            : static void
      87                 :          0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
      88                 :            : {
      89                 :          0 : }
      90                 :            : 
      91                 :            : int
      92                 :        101 : vbdev_error_inject_error(char *name, const struct vbdev_error_inject_opts *opts)
      93                 :            : {
      94                 :         80 :         struct spdk_bdev_desc *desc;
      95                 :            :         struct spdk_bdev *bdev;
      96                 :            :         struct spdk_bdev_part *part;
      97                 :        101 :         struct error_disk *error_disk = NULL;
      98                 :            :         uint32_t i;
      99                 :        101 :         int rc = 0;
     100                 :            : 
     101         [ -  + ]:        101 :         if (opts->error_type == VBDEV_IO_CORRUPT_DATA) {
     102         [ #  # ]:          0 :                 if (opts->corrupt_value == 0) {
     103                 :            :                         /* If corrupt_value is 0, XOR cannot cause data corruption. */
     104                 :          0 :                         SPDK_ERRLOG("corrupt_value should be non-zero.\n");
     105                 :          0 :                         return -EINVAL;
     106                 :            :                 }
     107                 :            :         }
     108                 :            : 
     109         [ -  + ]:        101 :         pthread_mutex_lock(&g_vbdev_error_mutex);
     110                 :            : 
     111                 :        101 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &desc);
     112         [ -  + ]:        101 :         if (rc != 0) {
     113                 :          0 :                 SPDK_ERRLOG("Could not open ErrorInjection bdev %s\n", name);
     114         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_vbdev_error_mutex);
     115                 :          0 :                 return rc;
     116                 :            :         }
     117                 :            : 
     118                 :        101 :         bdev = spdk_bdev_desc_get_bdev(desc);
     119                 :            : 
     120         [ +  - ]:        101 :         TAILQ_FOREACH(part, &g_error_disks, tailq) {
     121         [ +  - ]:        101 :                 if (bdev == spdk_bdev_part_get_bdev(part)) {
     122                 :        101 :                         error_disk = (struct error_disk *)part;
     123                 :        101 :                         break;
     124                 :            :                 }
     125                 :            :         }
     126                 :            : 
     127         [ -  + ]:        101 :         if (error_disk == NULL) {
     128                 :          0 :                 SPDK_ERRLOG("Could not find ErrorInjection bdev %s\n", name);
     129                 :          0 :                 rc = -ENODEV;
     130                 :          0 :                 goto exit;
     131                 :            :         }
     132                 :            : 
     133         [ +  + ]:        101 :         if (0xffffffff == opts->io_type) {
     134         [ +  + ]:         60 :                 for (i = 0; i < SPDK_COUNTOF(error_disk->error_vector); i++) {
     135                 :         50 :                         error_disk->error_vector[i].error_type = opts->error_type;
     136                 :         50 :                         error_disk->error_vector[i].error_num = opts->error_num;
     137                 :         50 :                         error_disk->error_vector[i].error_qd = opts->error_qd;
     138                 :         50 :                         error_disk->error_vector[i].corrupt_offset = opts->corrupt_offset;
     139                 :         50 :                         error_disk->error_vector[i].corrupt_value = opts->corrupt_value;
     140                 :            :                 }
     141         [ -  + ]:         91 :         } else if (0 == opts->io_type) {
     142         [ #  # ]:          0 :                 for (i = 0; i < SPDK_COUNTOF(error_disk->error_vector); i++) {
     143                 :          0 :                         error_disk->error_vector[i].error_num = 0;
     144                 :            :                 }
     145                 :            :         } else {
     146                 :         91 :                 error_disk->error_vector[opts->io_type].error_type = opts->error_type;
     147                 :         91 :                 error_disk->error_vector[opts->io_type].error_num = opts->error_num;
     148                 :         91 :                 error_disk->error_vector[opts->io_type].error_qd = opts->error_qd;
     149                 :         91 :                 error_disk->error_vector[opts->io_type].corrupt_offset = opts->corrupt_offset;
     150                 :         91 :                 error_disk->error_vector[opts->io_type].corrupt_value = opts->corrupt_value;
     151                 :            :         }
     152                 :            : 
     153                 :        101 : exit:
     154                 :        101 :         spdk_bdev_close(desc);
     155         [ -  + ]:        101 :         pthread_mutex_unlock(&g_vbdev_error_mutex);
     156                 :        101 :         return rc;
     157                 :            : }
     158                 :            : 
     159                 :            : static void
     160                 :          0 : vbdev_error_reset(struct error_disk *error_disk, struct spdk_bdev_io *bdev_io)
     161                 :            : {
     162                 :            :         struct error_io *pending_io, *tmp;
     163                 :            : 
     164         [ #  # ]:          0 :         TAILQ_FOREACH_SAFE(pending_io, &error_disk->pending_ios, link, tmp) {
     165         [ #  # ]:          0 :                 TAILQ_REMOVE(&error_disk->pending_ios, pending_io, link);
     166                 :          0 :                 spdk_bdev_io_complete(spdk_bdev_io_from_ctx(pending_io), SPDK_BDEV_IO_STATUS_FAILED);
     167                 :            :         }
     168                 :          0 :         spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
     169                 :          0 : }
     170                 :            : 
     171                 :            : static uint32_t
     172                 :    7787478 : vbdev_error_get_error_type(struct error_disk *error_disk, struct error_channel *ch,
     173                 :            :                            uint32_t io_type)
     174                 :            : {
     175                 :    6049701 :         uint32_t error_num;
     176                 :            :         struct vbdev_error_info *error_info;
     177                 :            : 
     178         [ +  - ]:    7787478 :         switch (io_type) {
     179                 :    7787478 :         case SPDK_BDEV_IO_TYPE_READ:
     180                 :            :         case SPDK_BDEV_IO_TYPE_WRITE:
     181                 :            :         case SPDK_BDEV_IO_TYPE_UNMAP:
     182                 :            :         case SPDK_BDEV_IO_TYPE_FLUSH:
     183                 :    7787478 :                 break;
     184                 :          0 :         default:
     185                 :          0 :                 return VBDEV_IO_NO_ERROR;
     186                 :            :         }
     187                 :            : 
     188                 :    7787478 :         error_info = &error_disk->error_vector[io_type];
     189                 :            : 
     190         [ +  + ]:    7787478 :         if (ch->io_inflight < error_info->error_qd) {
     191                 :     229803 :                 return VBDEV_IO_NO_ERROR;
     192                 :            :         }
     193                 :            : 
     194                 :    7557675 :         error_num = error_info->error_num;
     195                 :            :         do {
     196         [ +  + ]:    7557675 :                 if (error_num == 0) {
     197                 :    7557538 :                         return VBDEV_IO_NO_ERROR;
     198                 :            :                 }
     199         [ -  + ]:        145 :         } while (!__atomic_compare_exchange_n(&error_info->error_num,
     200         [ -  + ]:        145 :                                               &error_num, error_num - 1,
     201         [ -  + ]:         56 :                                               false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
     202                 :            : 
     203                 :        145 :         return error_info->error_type;
     204                 :            : }
     205                 :            : 
     206                 :            : static void
     207                 :          0 : vbdev_error_corrupt_io_data(struct spdk_bdev_io *bdev_io, uint64_t corrupt_offset,
     208                 :            :                             uint8_t corrupt_value)
     209                 :            : {
     210                 :            :         uint8_t *buf;
     211                 :            :         int i;
     212                 :            : 
     213   [ #  #  #  # ]:          0 :         if (bdev_io->u.bdev.iovs == NULL || bdev_io->u.bdev.iovs[0].iov_base == NULL) {
     214                 :          0 :                 return;
     215                 :            :         }
     216                 :            : 
     217         [ #  # ]:          0 :         for (i = 0; i < bdev_io->u.bdev.iovcnt; i++) {
     218         [ #  # ]:          0 :                 if (bdev_io->u.bdev.iovs[i].iov_len > corrupt_offset) {
     219                 :          0 :                         buf = (uint8_t *)bdev_io->u.bdev.iovs[i].iov_base;
     220                 :            : 
     221                 :          0 :                         buf[corrupt_offset] ^= corrupt_value;
     222                 :          0 :                         break;
     223                 :            :                 }
     224                 :            : 
     225                 :          0 :                 corrupt_offset -= bdev_io->u.bdev.iovs[i].iov_len;
     226                 :            :         }
     227                 :            : }
     228                 :            : 
     229                 :            : static void
     230                 :    7787341 : vbdev_error_complete_request(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
     231                 :            : {
     232                 :    7787341 :         struct error_io *error_io = (struct error_io *)bdev_io->driver_ctx;
     233         [ +  - ]:    7787341 :         int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
     234                 :    7787341 :         struct error_disk *error_disk = bdev_io->bdev->ctxt;
     235                 :    7787341 :         struct error_channel *ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
     236                 :            : 
     237         [ -  + ]:    7787341 :         assert(ch->io_inflight > 0);
     238                 :    7787341 :         ch->io_inflight--;
     239                 :            : 
     240   [ +  -  +  + ]:    7787341 :         if (success && bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
     241         [ -  + ]:    3468071 :                 if (error_io->error_type == VBDEV_IO_CORRUPT_DATA) {
     242                 :          0 :                         vbdev_error_corrupt_io_data(bdev_io,
     243                 :          0 :                                                     error_disk->error_vector[bdev_io->type].corrupt_offset,
     244                 :          0 :                                                     error_disk->error_vector[bdev_io->type].corrupt_value);
     245                 :            :                 }
     246                 :            :         }
     247                 :            : 
     248                 :    7787341 :         spdk_bdev_io_complete(bdev_io, status);
     249                 :    7787341 : }
     250                 :            : 
     251                 :            : static void
     252                 :    7787478 : vbdev_error_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
     253                 :            : {
     254                 :    7787478 :         struct error_io *error_io = (struct error_io *)bdev_io->driver_ctx;
     255                 :    7787478 :         struct error_channel *ch = spdk_io_channel_get_ctx(_ch);
     256                 :    7787478 :         struct error_disk *error_disk = bdev_io->bdev->ctxt;
     257                 :            :         int rc;
     258                 :            : 
     259         [ -  + ]:    7787478 :         if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) {
     260                 :          0 :                 vbdev_error_reset(error_disk, bdev_io);
     261                 :          0 :                 return;
     262                 :            :         }
     263                 :            : 
     264                 :    7787478 :         error_io->error_type = vbdev_error_get_error_type(error_disk, ch, bdev_io->type);
     265                 :            : 
     266   [ +  +  -  -  :    7787478 :         switch (error_io->error_type) {
                   +  - ]
     267                 :        140 :         case VBDEV_IO_FAILURE:
     268                 :        140 :                 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
     269                 :        140 :                 break;
     270                 :          5 :         case VBDEV_IO_NOMEM:
     271                 :          5 :                 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
     272                 :          5 :                 break;
     273                 :          0 :         case VBDEV_IO_PENDING:
     274                 :          0 :                 TAILQ_INSERT_TAIL(&error_disk->pending_ios, error_io, link);
     275                 :          0 :                 break;
     276                 :          0 :         case VBDEV_IO_CORRUPT_DATA:
     277         [ #  # ]:          0 :                 if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
     278                 :          0 :                         vbdev_error_corrupt_io_data(bdev_io,
     279                 :          0 :                                                     error_disk->error_vector[bdev_io->type].corrupt_offset,
     280                 :          0 :                                                     error_disk->error_vector[bdev_io->type].corrupt_value);
     281                 :            :                 }
     282                 :            :         /* fallthrough */
     283                 :            :         case VBDEV_IO_NO_ERROR:
     284                 :    7787341 :                 rc = spdk_bdev_part_submit_request_ext(&ch->part_ch, bdev_io,
     285                 :            :                                                        vbdev_error_complete_request);
     286                 :            : 
     287         [ -  + ]:    7787341 :                 if (rc) {
     288                 :          0 :                         SPDK_ERRLOG("bdev_error: submit request failed, rc=%d\n", rc);
     289                 :          0 :                         spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
     290                 :            :                 }
     291                 :    7787341 :                 ch->io_inflight++;
     292                 :    7787341 :                 break;
     293                 :          0 :         default:
     294                 :          0 :                 assert(false);
     295                 :            :                 break;
     296                 :            :         }
     297                 :            : }
     298                 :            : 
     299                 :            : static int
     300                 :        287 : vbdev_error_destruct(void *ctx)
     301                 :            : {
     302                 :        287 :         struct error_disk *error_disk = ctx;
     303                 :        287 :         struct spdk_bdev *base_bdev = spdk_bdev_part_get_base_bdev(&error_disk->part);
     304                 :            :         int rc;
     305                 :            : 
     306                 :        287 :         rc = vbdev_error_config_remove(base_bdev->name);
     307         [ -  + ]:        287 :         if (rc != 0) {
     308                 :          0 :                 SPDK_ERRLOG("vbdev_error_config_remove() failed\n");
     309                 :            :         }
     310                 :            : 
     311                 :        287 :         return spdk_bdev_part_free(&error_disk->part);
     312                 :            : }
     313                 :            : 
     314                 :            : static int
     315                 :       1680 : vbdev_error_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
     316                 :            : {
     317                 :       1680 :         struct error_disk *error_disk = ctx;
     318                 :       1680 :         struct spdk_bdev *base_bdev = spdk_bdev_part_get_base_bdev(&error_disk->part);
     319                 :            : 
     320                 :       1680 :         spdk_json_write_named_object_begin(w, "error_disk");
     321                 :            : 
     322                 :       1680 :         spdk_json_write_named_string(w, "base_bdev", base_bdev->name);
     323                 :            : 
     324                 :       1680 :         spdk_json_write_object_end(w);
     325                 :            : 
     326                 :       1680 :         return 0;
     327                 :            : }
     328                 :            : 
     329                 :            : static void
     330                 :          8 : vbdev_error_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
     331                 :            : {
     332                 :            :         /* No config per bdev. */
     333                 :          8 : }
     334                 :            : 
     335                 :            : 
     336                 :            : static struct spdk_bdev_fn_table vbdev_error_fn_table = {
     337                 :            :         .destruct               = vbdev_error_destruct,
     338                 :            :         .submit_request         = vbdev_error_submit_request,
     339                 :            :         .dump_info_json         = vbdev_error_dump_info_json,
     340                 :            :         .write_config_json      = vbdev_error_write_config_json
     341                 :            : };
     342                 :            : 
     343                 :            : static void
     344                 :          0 : vbdev_error_base_bdev_hotremove_cb(void *_part_base)
     345                 :            : {
     346                 :          0 :         struct spdk_bdev_part_base *part_base = _part_base;
     347                 :            : 
     348                 :          0 :         spdk_bdev_part_base_hotremove(part_base, &g_error_disks);
     349                 :          0 : }
     350                 :            : 
     351                 :            : static int
     352                 :        291 : _vbdev_error_create(const char *base_bdev_name, const struct spdk_uuid *uuid)
     353                 :            : {
     354                 :        291 :         struct spdk_bdev_part_base *base = NULL;
     355                 :        291 :         struct error_disk *disk = NULL;
     356                 :            :         struct spdk_bdev *base_bdev, *bdev;
     357                 :            :         char *name;
     358                 :            :         int rc;
     359                 :            : 
     360                 :        291 :         rc = spdk_bdev_part_base_construct_ext(base_bdev_name,
     361                 :            :                                                vbdev_error_base_bdev_hotremove_cb,
     362                 :            :                                                &error_if, &vbdev_error_fn_table, &g_error_disks,
     363                 :            :                                                NULL, NULL, sizeof(struct error_channel),
     364                 :            :                                                NULL, NULL, &base);
     365         [ +  + ]:        291 :         if (rc != 0) {
     366         [ -  + ]:          4 :                 if (rc != -ENODEV) {
     367                 :          0 :                         SPDK_ERRLOG("could not construct part base for bdev %s\n", base_bdev_name);
     368                 :            :                 }
     369                 :          4 :                 return rc;
     370                 :            :         }
     371                 :            : 
     372                 :        287 :         base_bdev = spdk_bdev_part_base_get_bdev(base);
     373                 :            : 
     374                 :        287 :         disk = calloc(1, sizeof(*disk));
     375         [ -  + ]:        287 :         if (!disk) {
     376                 :          0 :                 SPDK_ERRLOG("Memory allocation failure\n");
     377                 :          0 :                 spdk_bdev_part_base_free(base);
     378                 :          0 :                 return -ENOMEM;
     379                 :            :         }
     380                 :            : 
     381                 :        287 :         name = spdk_sprintf_alloc("EE_%s", base_bdev_name);
     382         [ -  + ]:        287 :         if (!name) {
     383                 :          0 :                 SPDK_ERRLOG("name allocation failure\n");
     384                 :          0 :                 spdk_bdev_part_base_free(base);
     385                 :          0 :                 free(disk);
     386                 :          0 :                 return -ENOMEM;
     387                 :            :         }
     388                 :            : 
     389         [ -  + ]:        287 :         if (!spdk_uuid_is_null(uuid)) {
     390                 :          0 :                 bdev = spdk_bdev_part_get_bdev(&disk->part);
     391                 :          0 :                 spdk_uuid_copy(&bdev->uuid, uuid);
     392                 :            :         }
     393                 :            : 
     394                 :        287 :         rc = spdk_bdev_part_construct(&disk->part, base, name, 0, base_bdev->blockcnt,
     395                 :            :                                       "Error Injection Disk");
     396                 :        287 :         free(name);
     397         [ -  + ]:        287 :         if (rc) {
     398                 :          0 :                 SPDK_ERRLOG("could not construct part for bdev %s\n", base_bdev_name);
     399                 :            :                 /* spdk_bdev_part_construct will free name on failure */
     400                 :          0 :                 spdk_bdev_part_base_free(base);
     401                 :          0 :                 free(disk);
     402                 :          0 :                 return rc;
     403                 :            :         }
     404                 :            : 
     405                 :        287 :         TAILQ_INIT(&disk->pending_ios);
     406                 :            : 
     407                 :        287 :         return 0;
     408                 :            : }
     409                 :            : 
     410                 :            : int
     411                 :        287 : vbdev_error_create(const char *base_bdev_name, const struct spdk_uuid *uuid)
     412                 :            : {
     413                 :            :         int rc;
     414                 :            : 
     415                 :        287 :         rc = vbdev_error_config_add(base_bdev_name, uuid);
     416         [ -  + ]:        287 :         if (rc != 0) {
     417                 :          0 :                 SPDK_ERRLOG("Adding config for ErrorInjection bdev %s failed (rc=%d)\n",
     418                 :            :                             base_bdev_name, rc);
     419                 :          0 :                 return rc;
     420                 :            :         }
     421                 :            : 
     422                 :        287 :         rc = _vbdev_error_create(base_bdev_name, uuid);
     423         [ +  + ]:        287 :         if (rc == -ENODEV) {
     424                 :          4 :                 rc = 0;
     425         [ -  + ]:        283 :         } else if (rc != 0) {
     426                 :          0 :                 vbdev_error_config_remove(base_bdev_name);
     427                 :          0 :                 SPDK_ERRLOG("Could not create ErrorInjection bdev %s (rc=%d)\n",
     428                 :            :                             base_bdev_name, rc);
     429                 :            :         }
     430                 :            : 
     431                 :        287 :         return rc;
     432                 :            : }
     433                 :            : 
     434                 :            : void
     435                 :         11 : vbdev_error_delete(const char *error_vbdev_name, spdk_delete_error_complete cb_fn, void *cb_arg)
     436                 :            : {
     437                 :            :         int rc;
     438                 :            : 
     439                 :         11 :         rc = spdk_bdev_unregister_by_name(error_vbdev_name, &error_if, cb_fn, cb_arg);
     440         [ -  + ]:         11 :         if (rc != 0) {
     441                 :          0 :                 cb_fn(cb_arg, rc);
     442                 :            :         }
     443                 :         11 : }
     444                 :            : 
     445                 :            : static void
     446                 :       2140 : vbdev_error_clear_config(void)
     447                 :            : {
     448                 :            :         struct spdk_vbdev_error_config *cfg;
     449                 :            : 
     450         [ -  + ]:       2140 :         while ((cfg = TAILQ_FIRST(&g_error_config))) {
     451         [ #  # ]:          0 :                 TAILQ_REMOVE(&g_error_config, cfg, tailq);
     452                 :          0 :                 free(cfg->base_bdev);
     453                 :          0 :                 free(cfg);
     454                 :            :         }
     455                 :       2140 : }
     456                 :            : 
     457                 :            : static struct spdk_vbdev_error_config *
     458                 :       8461 : vbdev_error_config_find_by_base_name(const char *base_bdev_name)
     459                 :            : {
     460                 :            :         struct spdk_vbdev_error_config *cfg;
     461                 :            : 
     462         [ +  + ]:      10891 :         TAILQ_FOREACH(cfg, &g_error_config, tailq) {
     463   [ +  +  -  +  :       2721 :                 if (strcmp(cfg->base_bdev, base_bdev_name) == 0) {
                   +  + ]
     464                 :        291 :                         return cfg;
     465                 :            :                 }
     466                 :            :         }
     467                 :            : 
     468                 :       8170 :         return NULL;
     469                 :            : }
     470                 :            : 
     471                 :            : static int
     472                 :        287 : vbdev_error_config_add(const char *base_bdev_name, const struct spdk_uuid *uuid)
     473                 :            : {
     474                 :            :         struct spdk_vbdev_error_config *cfg;
     475                 :            : 
     476                 :        287 :         cfg = vbdev_error_config_find_by_base_name(base_bdev_name);
     477         [ -  + ]:        287 :         if (cfg) {
     478                 :          0 :                 SPDK_ERRLOG("vbdev_error_config for bdev %s already exists\n",
     479                 :            :                             base_bdev_name);
     480                 :          0 :                 return -EEXIST;
     481                 :            :         }
     482                 :            : 
     483                 :        287 :         cfg = calloc(1, sizeof(*cfg));
     484         [ -  + ]:        287 :         if (!cfg) {
     485                 :          0 :                 SPDK_ERRLOG("calloc() failed for vbdev_error_config\n");
     486                 :          0 :                 return -ENOMEM;
     487                 :            :         }
     488                 :            : 
     489         [ -  + ]:        287 :         cfg->base_bdev = strdup(base_bdev_name);
     490         [ -  + ]:        287 :         if (!cfg->base_bdev) {
     491                 :          0 :                 free(cfg);
     492                 :          0 :                 SPDK_ERRLOG("strdup() failed for base_bdev_name\n");
     493                 :          0 :                 return -ENOMEM;
     494                 :            :         }
     495                 :            : 
     496                 :        287 :         spdk_uuid_copy(&cfg->uuid, uuid);
     497                 :        287 :         TAILQ_INSERT_TAIL(&g_error_config, cfg, tailq);
     498                 :            : 
     499                 :        287 :         return 0;
     500                 :            : }
     501                 :            : 
     502                 :            : static int
     503                 :        287 : vbdev_error_config_remove(const char *base_bdev_name)
     504                 :            : {
     505                 :            :         struct spdk_vbdev_error_config *cfg;
     506                 :            : 
     507                 :        287 :         cfg = vbdev_error_config_find_by_base_name(base_bdev_name);
     508         [ -  + ]:        287 :         if (!cfg) {
     509                 :          0 :                 return -ENOENT;
     510                 :            :         }
     511                 :            : 
     512         [ +  + ]:        287 :         TAILQ_REMOVE(&g_error_config, cfg, tailq);
     513                 :        287 :         free(cfg->base_bdev);
     514                 :        287 :         free(cfg);
     515                 :        287 :         return 0;
     516                 :            : }
     517                 :            : 
     518                 :            : static int
     519                 :       2140 : vbdev_error_init(void)
     520                 :            : {
     521                 :       2140 :         return 0;
     522                 :            : }
     523                 :            : 
     524                 :            : static void
     525                 :       2140 : vbdev_error_fini(void)
     526                 :            : {
     527                 :       2140 :         vbdev_error_clear_config();
     528                 :       2140 : }
     529                 :            : 
     530                 :            : static void
     531                 :       7887 : vbdev_error_examine(struct spdk_bdev *bdev)
     532                 :            : {
     533                 :            :         struct spdk_vbdev_error_config *cfg;
     534                 :            :         int rc;
     535                 :            : 
     536                 :       7887 :         cfg = vbdev_error_config_find_by_base_name(bdev->name);
     537         [ +  + ]:       7887 :         if (cfg != NULL) {
     538                 :          4 :                 rc = _vbdev_error_create(bdev->name, &cfg->uuid);
     539         [ -  + ]:          4 :                 if (rc != 0) {
     540                 :          0 :                         SPDK_ERRLOG("could not create error vbdev for bdev %s at examine\n",
     541                 :            :                                     bdev->name);
     542                 :            :                 }
     543                 :            :         }
     544                 :            : 
     545                 :       7887 :         spdk_bdev_module_examine_done(&error_if);
     546                 :       7887 : }
     547                 :            : 
     548                 :            : static int
     549                 :        169 : vbdev_error_config_json(struct spdk_json_write_ctx *w)
     550                 :            : {
     551                 :            :         struct spdk_vbdev_error_config *cfg;
     552                 :            : 
     553         [ +  + ]:        177 :         TAILQ_FOREACH(cfg, &g_error_config, tailq) {
     554                 :          8 :                 spdk_json_write_object_begin(w);
     555                 :            : 
     556                 :          8 :                 spdk_json_write_named_string(w, "method", "bdev_error_create");
     557                 :          8 :                 spdk_json_write_named_object_begin(w, "params");
     558                 :          8 :                 spdk_json_write_named_string(w, "base_name", cfg->base_bdev);
     559         [ -  + ]:          8 :                 if (!spdk_uuid_is_null(&cfg->uuid)) {
     560                 :          0 :                         spdk_json_write_named_uuid(w, "uuid", &cfg->uuid);
     561                 :            :                 }
     562                 :          8 :                 spdk_json_write_object_end(w);
     563                 :            : 
     564                 :          8 :                 spdk_json_write_object_end(w);
     565                 :            :         }
     566                 :            : 
     567                 :        169 :         return 0;
     568                 :            : }

Generated by: LCOV version 1.14