LCOV - code coverage report
Current view: top level - spdk/module/bdev/compress - vbdev_compress_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 34 71 47.9 %
Date: 2024-07-15 17:41:13 Functions: 8 10 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 28 14.3 %

           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                 :        158 : 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                 :            : };
      80                 :            : 
      81                 :            : /* Free the allocated memory resource after the RPC handling. */
      82                 :            : static void
      83                 :          8 : free_rpc_construct_compress(struct rpc_construct_compress *r)
      84                 :            : {
      85                 :          8 :         free(r->base_bdev_name);
      86                 :          8 :         free(r->pm_path);
      87                 :          8 : }
      88                 :            : 
      89                 :            : /* Structure to decode the input parameters for this RPC method. */
      90                 :            : static const struct spdk_json_object_decoder rpc_construct_compress_decoders[] = {
      91                 :            :         {"base_bdev_name", offsetof(struct rpc_construct_compress, base_bdev_name), spdk_json_decode_string},
      92                 :            :         {"pm_path", offsetof(struct rpc_construct_compress, pm_path), spdk_json_decode_string},
      93                 :            :         {"lb_size", offsetof(struct rpc_construct_compress, lb_size), spdk_json_decode_uint32, true},
      94                 :            : };
      95                 :            : 
      96                 :            : /* Decode the parameters for this RPC method and properly construct the compress
      97                 :            :  * device. Error status returned in the failed cases.
      98                 :            :  */
      99                 :            : static void
     100                 :          8 : rpc_bdev_compress_create(struct spdk_jsonrpc_request *request,
     101                 :            :                          const struct spdk_json_val *params)
     102                 :            : {
     103                 :          8 :         struct rpc_construct_compress req = {NULL};
     104                 :            :         struct spdk_json_write_ctx *w;
     105                 :            :         char *name;
     106                 :            :         int rc;
     107                 :            : 
     108         [ -  + ]:          8 :         if (spdk_json_decode_object(params, rpc_construct_compress_decoders,
     109                 :            :                                     SPDK_COUNTOF(rpc_construct_compress_decoders),
     110                 :            :                                     &req)) {
     111   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(vbdev_compress, "spdk_json_decode_object failed\n");
     112                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_PARSE_ERROR,
     113                 :            :                                                  "spdk_json_decode_object failed");
     114                 :          0 :                 goto cleanup;
     115                 :            :         }
     116                 :            : 
     117                 :          8 :         rc = create_compress_bdev(req.base_bdev_name, req.pm_path, req.lb_size);
     118         [ -  + ]:          8 :         if (rc != 0) {
     119         [ #  # ]:          0 :                 if (rc == -EBUSY) {
     120                 :          0 :                         spdk_jsonrpc_send_error_response(request, rc, "Base bdev already in use for compression.");
     121                 :            :                 } else {
     122                 :          0 :                         spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     123                 :            :                 }
     124                 :          0 :                 goto cleanup;
     125                 :            :         }
     126                 :            : 
     127                 :          8 :         w = spdk_jsonrpc_begin_result(request);
     128                 :          8 :         name = spdk_sprintf_alloc("COMP_%s", req.base_bdev_name);
     129                 :          8 :         spdk_json_write_string(w, name);
     130                 :          8 :         spdk_jsonrpc_end_result(request, w);
     131                 :          8 :         free(name);
     132                 :            : 
     133                 :          8 : cleanup:
     134                 :          8 :         free_rpc_construct_compress(&req);
     135                 :          8 : }
     136                 :        158 : SPDK_RPC_REGISTER("bdev_compress_create", rpc_bdev_compress_create, SPDK_RPC_RUNTIME)
     137                 :            : 
     138                 :            : struct rpc_delete_compress {
     139                 :            :         char *name;
     140                 :            : };
     141                 :            : 
     142                 :            : static void
     143                 :          8 : free_rpc_delete_compress(struct rpc_delete_compress *req)
     144                 :            : {
     145                 :          8 :         free(req->name);
     146                 :          8 : }
     147                 :            : 
     148                 :            : static const struct spdk_json_object_decoder rpc_delete_compress_decoders[] = {
     149                 :            :         {"name", offsetof(struct rpc_delete_compress, name), spdk_json_decode_string},
     150                 :            : };
     151                 :            : 
     152                 :            : static void
     153                 :          8 : _rpc_bdev_compress_delete_cb(void *cb_arg, int bdeverrno)
     154                 :            : {
     155                 :          8 :         struct spdk_jsonrpc_request *request = cb_arg;
     156                 :            : 
     157         [ +  - ]:          8 :         if (bdeverrno == 0) {
     158                 :          8 :                 spdk_jsonrpc_send_bool_response(request, true);
     159                 :            :         } else {
     160                 :          0 :                 spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
     161                 :            :         }
     162                 :          8 : }
     163                 :            : 
     164                 :            : static void
     165                 :          8 : rpc_bdev_compress_delete(struct spdk_jsonrpc_request *request,
     166                 :            :                          const struct spdk_json_val *params)
     167                 :            : {
     168                 :          8 :         struct rpc_delete_compress req = {NULL};
     169                 :            : 
     170         [ -  + ]:          8 :         if (spdk_json_decode_object(params, rpc_delete_compress_decoders,
     171                 :            :                                     SPDK_COUNTOF(rpc_delete_compress_decoders),
     172                 :            :                                     &req)) {
     173                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     174                 :            :                                                  "spdk_json_decode_object failed");
     175                 :            :         } else {
     176                 :          8 :                 bdev_compress_delete(req.name, _rpc_bdev_compress_delete_cb, request);
     177                 :            :         }
     178                 :            : 
     179                 :          8 :         free_rpc_delete_compress(&req);
     180                 :          8 : }
     181                 :        158 : SPDK_RPC_REGISTER("bdev_compress_delete", rpc_bdev_compress_delete, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.14