LCOV - code coverage report
Current view: top level - spdk/module/bdev/compress - vbdev_compress_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 47 103 45.6 %
Date: 2024-12-14 14:09:26 Functions: 9 12 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 44 15.9 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2018 Intel Corporation.
       3                 :            :  *   Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       4                 :            :  *   All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "vbdev_compress.h"
       8                 :            : #include "spdk/rpc.h"
       9                 :            : #include "spdk/util.h"
      10                 :            : #include "spdk/string.h"
      11                 :            : #include "spdk/log.h"
      12                 :            : 
      13                 :            : struct rpc_bdev_compress_get_orphans {
      14                 :            :         char *name;
      15                 :            : };
      16                 :            : 
      17                 :            : static void
      18                 :          0 : free_rpc_bdev_compress_get_orphans(struct rpc_bdev_compress_get_orphans *r)
      19                 :            : {
      20                 :          0 :         free(r->name);
      21                 :          0 : }
      22                 :            : 
      23                 :            : static const struct spdk_json_object_decoder rpc_bdev_compress_get_orphans_decoders[] = {
      24                 :            :         {"name", offsetof(struct rpc_bdev_compress_get_orphans, name), spdk_json_decode_string, true},
      25                 :            : };
      26                 :            : 
      27                 :            : static void
      28                 :          0 : rpc_bdev_compress_get_orphans(struct spdk_jsonrpc_request *request,
      29                 :            :                               const struct spdk_json_val *params)
      30                 :            : {
      31                 :          0 :         struct rpc_bdev_compress_get_orphans req = {};
      32                 :            :         struct spdk_json_write_ctx *w;
      33                 :            :         struct vbdev_compress *comp_bdev;
      34                 :          0 :         bool found = false;
      35                 :            : 
      36                 :            : 
      37   [ #  #  #  # ]:          0 :         if (params && spdk_json_decode_object(params, rpc_bdev_compress_get_orphans_decoders,
      38                 :            :                                               SPDK_COUNTOF(rpc_bdev_compress_get_orphans_decoders),
      39                 :            :                                               &req)) {
      40                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
      41                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
      42                 :            :                                                  "spdk_json_decode_object failed");
      43                 :          0 :                 free_rpc_bdev_compress_get_orphans(&req);
      44                 :          0 :                 return;
      45                 :            :         }
      46                 :            : 
      47         [ #  # ]:          0 :         if (req.name) {
      48         [ #  # ]:          0 :                 if (compress_has_orphan(req.name) == false) {
      49                 :          0 :                         spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
      50                 :          0 :                         free_rpc_bdev_compress_get_orphans(&req);
      51                 :          0 :                         return;
      52                 :            :                 }
      53                 :          0 :                 found = true;
      54                 :            :         }
      55                 :            : 
      56                 :          0 :         w = spdk_jsonrpc_begin_result(request);
      57                 :          0 :         spdk_json_write_array_begin(w);
      58         [ #  # ]:          0 :         if (found) {
      59                 :          0 :                 spdk_json_write_string(w, req.name);
      60                 :            :         } else {
      61         [ #  # ]:          0 :                 for (comp_bdev = compress_bdev_first(); comp_bdev != NULL;
      62                 :          0 :                      comp_bdev = compress_bdev_next(comp_bdev)) {
      63         [ #  # ]:          0 :                         if (compress_has_orphan(compress_get_name(comp_bdev))) {
      64                 :          0 :                                 spdk_json_write_string(w, compress_get_name(comp_bdev));
      65                 :            :                         }
      66                 :            :                 }
      67                 :            :         }
      68                 :          0 :         spdk_json_write_array_end(w);
      69                 :          0 :         spdk_jsonrpc_end_result(request, w);
      70                 :          0 :         free_rpc_bdev_compress_get_orphans(&req);
      71                 :            : }
      72                 :        110 : SPDK_RPC_REGISTER("bdev_compress_get_orphans", rpc_bdev_compress_get_orphans, SPDK_RPC_RUNTIME)
      73                 :            : 
      74                 :            : /* Structure to hold the parameters for this RPC method. */
      75                 :            : struct rpc_construct_compress {
      76                 :            :         char *base_bdev_name;
      77                 :            :         char *pm_path;
      78                 :            :         uint32_t lb_size;
      79                 :            :         enum spdk_accel_comp_algo comp_algo;
      80                 :            :         uint32_t comp_level;
      81                 :            : };
      82                 :            : 
      83                 :            : static int
      84                 :          0 : rpc_decode_comp_algo(const struct spdk_json_val *val, void *out)
      85                 :            : {
      86                 :          0 :         enum spdk_accel_comp_algo *algo = out;
      87                 :          0 :         char *name = NULL;
      88                 :            :         int rc;
      89                 :            : 
      90                 :          0 :         rc = spdk_json_decode_string(val, &name);
      91         [ #  # ]:          0 :         if (rc != 0) {
      92                 :          0 :                 return rc;
      93                 :            :         }
      94                 :            : 
      95   [ #  #  #  # ]:          0 :         if (strcmp(name, "deflate") == 0) {
      96                 :          0 :                 *algo = SPDK_ACCEL_COMP_ALGO_DEFLATE;
      97   [ #  #  #  # ]:          0 :         } else if (strcmp(name, "lz4") == 0) {
      98                 :          0 :                 *algo = SPDK_ACCEL_COMP_ALGO_LZ4;
      99                 :            :         } else {
     100                 :          0 :                 rc = -EINVAL;
     101                 :            :         }
     102                 :            : 
     103                 :          0 :         free(name);
     104                 :            : 
     105                 :          0 :         return rc;
     106                 :            : }
     107                 :            : 
     108                 :            : struct rpc_bdev_compress_create_ctx {
     109                 :            :         struct rpc_construct_compress req;
     110                 :            :         struct spdk_jsonrpc_request *request;
     111                 :            : };
     112                 :            : 
     113                 :            : /* Free the allocated memory resource after the RPC handling. */
     114                 :            : static void
     115                 :         24 : free_rpc_construct_compress(struct rpc_bdev_compress_create_ctx *ctx)
     116                 :            : {
     117                 :            :         struct rpc_construct_compress *req;
     118                 :            : 
     119         [ -  + ]:         24 :         assert(ctx != NULL);
     120                 :            : 
     121                 :         24 :         req = &ctx->req;
     122                 :            : 
     123                 :         24 :         free(req->base_bdev_name);
     124                 :         24 :         free(req->pm_path);
     125                 :            : 
     126                 :         24 :         free(ctx);
     127                 :         24 : }
     128                 :            : 
     129                 :            : /* Structure to decode the input parameters for this RPC method. */
     130                 :            : static const struct spdk_json_object_decoder rpc_construct_compress_decoders[] = {
     131                 :            :         {"base_bdev_name", offsetof(struct rpc_construct_compress, base_bdev_name), spdk_json_decode_string},
     132                 :            :         {"pm_path", offsetof(struct rpc_construct_compress, pm_path), spdk_json_decode_string},
     133                 :            :         {"lb_size", offsetof(struct rpc_construct_compress, lb_size), spdk_json_decode_uint32, true},
     134                 :            :         {"comp_algo", offsetof(struct rpc_construct_compress, comp_algo), rpc_decode_comp_algo, true},
     135                 :            :         {"comp_level", offsetof(struct rpc_construct_compress, comp_level), spdk_json_decode_uint32, true},
     136                 :            : };
     137                 :            : 
     138                 :            : static void
     139                 :         24 : rpc_bdev_compress_create_cb(void *_ctx, int status)
     140                 :            : {
     141                 :         24 :         struct rpc_bdev_compress_create_ctx *ctx = _ctx;
     142                 :         24 :         struct rpc_construct_compress *req = &ctx->req;
     143                 :         24 :         struct spdk_jsonrpc_request *request = ctx->request;
     144                 :            :         struct spdk_json_write_ctx *w;
     145                 :            :         char *name;
     146                 :            : 
     147         [ -  + ]:         24 :         if (status != 0) {
     148                 :          0 :                 spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
     149                 :            :         } else {
     150                 :         24 :                 w = spdk_jsonrpc_begin_result(request);
     151                 :         24 :                 name = spdk_sprintf_alloc("COMP_%s", req->base_bdev_name);
     152                 :         24 :                 spdk_json_write_string(w, name);
     153                 :         24 :                 spdk_jsonrpc_end_result(request, w);
     154                 :         24 :                 free(name);
     155                 :            :         }
     156                 :            : 
     157                 :         24 :         free_rpc_construct_compress(ctx);
     158                 :         24 : }
     159                 :            : 
     160                 :            : /* Decode the parameters for this RPC method and properly construct the compress
     161                 :            :  * device. Error status returned in the failed cases.
     162                 :            :  */
     163                 :            : static void
     164                 :         24 : rpc_bdev_compress_create(struct spdk_jsonrpc_request *request,
     165                 :            :                          const struct spdk_json_val *params)
     166                 :            : {
     167                 :            :         struct rpc_bdev_compress_create_ctx *ctx;
     168                 :            :         struct rpc_construct_compress *req;
     169                 :            :         int rc;
     170                 :            : 
     171                 :         24 :         ctx = calloc(1, sizeof(*ctx));
     172         [ -  + ]:         24 :         if (ctx == NULL) {
     173                 :          0 :                 SPDK_ERRLOG("failed to alloc compress bdev creation contexts\n");
     174                 :          0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
     175                 :          0 :                 return;
     176                 :            :         }
     177                 :            : 
     178                 :         24 :         req = &ctx->req;
     179                 :         24 :         req->comp_algo = SPDK_ACCEL_COMP_ALGO_DEFLATE;
     180                 :         24 :         req->comp_level = 1;
     181         [ -  + ]:         24 :         if (spdk_json_decode_object(params, rpc_construct_compress_decoders,
     182                 :            :                                     SPDK_COUNTOF(rpc_construct_compress_decoders),
     183                 :            :                                     req)) {
     184   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(vbdev_compress, "spdk_json_decode_object failed\n");
     185                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
     186                 :            :                                                  "spdk_json_decode_object failed");
     187                 :          0 :                 goto cleanup;
     188                 :            :         }
     189                 :            : 
     190                 :         24 :         rc = create_compress_bdev(req->base_bdev_name, req->pm_path, req->lb_size, req->comp_algo,
     191                 :            :                                   req->comp_level, rpc_bdev_compress_create_cb, ctx);
     192         [ -  + ]:         24 :         if (rc != 0) {
     193         [ #  # ]:          0 :                 if (rc == -EBUSY) {
     194                 :          0 :                         spdk_jsonrpc_send_error_response(request, rc, "Base bdev already in use for compression.");
     195                 :            :                 } else {
     196                 :          0 :                         spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     197                 :            :                 }
     198                 :          0 :                 goto cleanup;
     199                 :            :         }
     200                 :            : 
     201                 :         24 :         ctx->request = request;
     202                 :         24 :         return;
     203                 :            : 
     204                 :          0 : cleanup:
     205                 :          0 :         free_rpc_construct_compress(ctx);
     206                 :            : }
     207                 :        110 : SPDK_RPC_REGISTER("bdev_compress_create", rpc_bdev_compress_create, SPDK_RPC_RUNTIME)
     208                 :            : 
     209                 :            : struct rpc_delete_compress {
     210                 :            :         char *name;
     211                 :            : };
     212                 :            : 
     213                 :            : static void
     214                 :         24 : free_rpc_delete_compress(struct rpc_delete_compress *req)
     215                 :            : {
     216                 :         24 :         free(req->name);
     217                 :         24 : }
     218                 :            : 
     219                 :            : static const struct spdk_json_object_decoder rpc_delete_compress_decoders[] = {
     220                 :            :         {"name", offsetof(struct rpc_delete_compress, name), spdk_json_decode_string},
     221                 :            : };
     222                 :            : 
     223                 :            : static void
     224                 :         24 : _rpc_bdev_compress_delete_cb(void *cb_arg, int bdeverrno)
     225                 :            : {
     226                 :         24 :         struct spdk_jsonrpc_request *request = cb_arg;
     227                 :            : 
     228         [ +  - ]:         24 :         if (bdeverrno == 0) {
     229                 :         24 :                 spdk_jsonrpc_send_bool_response(request, true);
     230                 :            :         } else {
     231                 :          0 :                 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
     232                 :            :         }
     233                 :         24 : }
     234                 :            : 
     235                 :            : static void
     236                 :         24 : rpc_bdev_compress_delete(struct spdk_jsonrpc_request *request,
     237                 :            :                          const struct spdk_json_val *params)
     238                 :            : {
     239                 :         24 :         struct rpc_delete_compress req = {NULL};
     240                 :            : 
     241         [ -  + ]:         24 :         if (spdk_json_decode_object(params, rpc_delete_compress_decoders,
     242                 :            :                                     SPDK_COUNTOF(rpc_delete_compress_decoders),
     243                 :            :                                     &req)) {
     244                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     245                 :            :                                                  "spdk_json_decode_object failed");
     246                 :            :         } else {
     247                 :         24 :                 bdev_compress_delete(req.name, _rpc_bdev_compress_delete_cb, request);
     248                 :            :         }
     249                 :            : 
     250                 :         24 :         free_rpc_delete_compress(&req);
     251                 :         24 : }
     252                 :        110 : SPDK_RPC_REGISTER("bdev_compress_delete", rpc_bdev_compress_delete, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.15