LCOV - code coverage report
Current view: top level - spdk/lib/nbd - nbd_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 105 172 61.0 %
Date: 2024-07-13 03:51:15 Functions: 13 14 92.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 34 88 38.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                 :            : #include "spdk/string.h"
       7                 :            : #include "spdk/env.h"
       8                 :            : #include "spdk/rpc.h"
       9                 :            : #include "spdk/util.h"
      10                 :            : 
      11                 :            : #include <linux/nbd.h>
      12                 :            : 
      13                 :            : #include "nbd_internal.h"
      14                 :            : #include "spdk/log.h"
      15                 :            : 
      16                 :            : struct rpc_nbd_start_disk {
      17                 :            :         char *bdev_name;
      18                 :            :         char *nbd_device;
      19                 :            :         /* Used to search one available nbd device */
      20                 :            :         int nbd_idx;
      21                 :            :         bool nbd_idx_specified;
      22                 :            :         struct spdk_jsonrpc_request *request;
      23                 :            : };
      24                 :            : 
      25                 :            : static void
      26                 :        579 : free_rpc_nbd_start_disk(struct rpc_nbd_start_disk *req)
      27                 :            : {
      28                 :        579 :         free(req->bdev_name);
      29                 :        579 :         free(req->nbd_device);
      30                 :        579 :         free(req);
      31                 :        579 : }
      32                 :            : 
      33                 :            : static const struct spdk_json_object_decoder rpc_nbd_start_disk_decoders[] = {
      34                 :            :         {"bdev_name", offsetof(struct rpc_nbd_start_disk, bdev_name), spdk_json_decode_string},
      35                 :            :         {"nbd_device", offsetof(struct rpc_nbd_start_disk, nbd_device), spdk_json_decode_string, true},
      36                 :            : };
      37                 :            : 
      38                 :            : /* Return 0 to indicate the nbd_device might be available,
      39                 :            :  * or non-zero to indicate the nbd_device is invalid or in use.
      40                 :            :  */
      41                 :            : static int
      42                 :       1131 : check_available_nbd_disk(char *nbd_device)
      43                 :            : {
      44                 :        809 :         char nbd_block_path[256];
      45                 :        809 :         char tail[2];
      46                 :            :         int rc;
      47                 :        809 :         unsigned int nbd_idx;
      48                 :            :         struct spdk_nbd_disk *nbd;
      49                 :            : 
      50                 :            :         /* nbd device path must be in format of /dev/nbd<num>, with no tail. */
      51                 :       1131 :         rc = sscanf(nbd_device, "/dev/nbd%u%1s", &nbd_idx, tail);
      52         [ -  + ]:       1131 :         if (rc != 1) {
      53                 :          0 :                 return -errno;
      54                 :            :         }
      55                 :            : 
      56                 :            :         /* make sure nbd_device is not registered inside SPDK */
      57                 :       1131 :         nbd = nbd_disk_find_by_nbd_path(nbd_device);
      58         [ +  + ]:       1131 :         if (nbd) {
      59                 :            :                 /* nbd_device is in use */
      60                 :        552 :                 return -EBUSY;
      61                 :            :         }
      62                 :            : 
      63                 :            :         /* A valid pid file in /sys/block indicates the device is in use */
      64                 :        579 :         snprintf(nbd_block_path, 256, "/sys/block/nbd%u/pid", nbd_idx);
      65                 :            : 
      66                 :        579 :         rc = open(nbd_block_path, O_RDONLY);
      67         [ +  - ]:        579 :         if (rc < 0) {
      68         [ +  - ]:        579 :                 if (errno == ENOENT) {
      69                 :            :                         /* nbd_device might be available */
      70                 :        579 :                         return 0;
      71                 :            :                 } else {
      72                 :          0 :                         SPDK_ERRLOG("Failed to check PID file %s: %s\n", nbd_block_path, spdk_strerror(errno));
      73                 :          0 :                         return -errno;
      74                 :            :                 }
      75                 :            :         }
      76                 :            : 
      77                 :          0 :         close(rc);
      78                 :            : 
      79                 :            :         /* nbd_device is in use */
      80                 :          0 :         return -EBUSY;
      81                 :            : }
      82                 :            : 
      83                 :            : static char *
      84                 :        113 : find_available_nbd_disk(int nbd_idx, int *next_nbd_idx)
      85                 :            : {
      86                 :            :         int i, rc;
      87                 :         75 :         char nbd_device[20];
      88                 :            : 
      89                 :        665 :         for (i = nbd_idx; ; i++) {
      90         [ -  + ]:        665 :                 snprintf(nbd_device, 20, "/dev/nbd%d", i);
      91                 :            :                 /* Check whether an nbd device exists in order to reach the last one nbd device */
      92         [ -  + ]:        665 :                 rc = access(nbd_device, F_OK);
      93         [ -  + ]:        665 :                 if (rc != 0) {
      94                 :          0 :                         break;
      95                 :            :                 }
      96                 :            : 
      97                 :        665 :                 rc = check_available_nbd_disk(nbd_device);
      98         [ +  + ]:        665 :                 if (rc == 0) {
      99         [ +  - ]:        113 :                         if (next_nbd_idx != NULL) {
     100                 :        113 :                                 *next_nbd_idx = i + 1;
     101                 :            :                         }
     102                 :            : 
     103         [ -  + ]:        113 :                         return strdup(nbd_device);
     104                 :            :                 }
     105                 :            :         }
     106                 :            : 
     107                 :          0 :         return NULL;
     108                 :            : }
     109                 :            : 
     110                 :            : static void
     111                 :        579 : rpc_start_nbd_done(void *cb_arg, struct spdk_nbd_disk *nbd, int rc)
     112                 :            : {
     113                 :        579 :         struct rpc_nbd_start_disk *req = cb_arg;
     114                 :        579 :         struct spdk_jsonrpc_request *request = req->request;
     115                 :            :         struct spdk_json_write_ctx *w;
     116                 :            : 
     117                 :            :         /* Check whether it's automatic nbd-device assignment */
     118   [ -  +  -  -  :        579 :         if (rc == -EBUSY && req->nbd_idx_specified == false) {
                   -  - ]
     119                 :          0 :                 free(req->nbd_device);
     120                 :            : 
     121                 :          0 :                 req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
     122         [ #  # ]:          0 :                 if (req->nbd_device != NULL) {
     123                 :          0 :                         spdk_nbd_start(req->bdev_name, req->nbd_device,
     124                 :            :                                        rpc_start_nbd_done, req);
     125                 :          0 :                         return;
     126                 :            :                 }
     127                 :            : 
     128   [ #  #  #  # ]:          0 :                 SPDK_INFOLOG(nbd, "There is no available nbd device.\n");
     129                 :            :         }
     130                 :            : 
     131         [ -  + ]:        579 :         if (rc) {
     132                 :          0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     133                 :          0 :                 free_rpc_nbd_start_disk(req);
     134                 :          0 :                 return;
     135                 :            :         }
     136                 :            : 
     137                 :        579 :         w = spdk_jsonrpc_begin_result(request);
     138                 :        579 :         spdk_json_write_string(w, spdk_nbd_get_path(nbd));
     139                 :        579 :         spdk_jsonrpc_end_result(request, w);
     140                 :            : 
     141                 :        579 :         free_rpc_nbd_start_disk(req);
     142                 :            : }
     143                 :            : 
     144                 :            : static void
     145                 :        579 : rpc_nbd_start_disk(struct spdk_jsonrpc_request *request,
     146                 :            :                    const struct spdk_json_val *params)
     147                 :            : {
     148                 :            :         struct rpc_nbd_start_disk *req;
     149                 :            :         int rc;
     150                 :            : 
     151                 :        579 :         req = calloc(1, sizeof(*req));
     152         [ -  + ]:        579 :         if (req == NULL) {
     153                 :          0 :                 SPDK_ERRLOG("could not allocate nbd_start_disk request.\n");
     154                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
     155                 :          0 :                 return;
     156                 :            :         }
     157                 :            : 
     158         [ -  + ]:        579 :         if (spdk_json_decode_object(params, rpc_nbd_start_disk_decoders,
     159                 :            :                                     SPDK_COUNTOF(rpc_nbd_start_disk_decoders),
     160                 :            :                                     req)) {
     161                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     162                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     163                 :            :                                                  "spdk_json_decode_object failed");
     164                 :          0 :                 goto invalid;
     165                 :            :         }
     166                 :            : 
     167         [ -  + ]:        579 :         if (req->bdev_name == NULL) {
     168                 :          0 :                 goto invalid;
     169                 :            :         }
     170                 :            : 
     171         [ +  + ]:        579 :         if (req->nbd_device != NULL) {
     172                 :        466 :                 req->nbd_idx_specified = true;
     173                 :        466 :                 rc = check_available_nbd_disk(req->nbd_device);
     174         [ -  + ]:        466 :                 if (rc == -EBUSY) {
     175   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(nbd, "NBD device %s is in use.\n", req->nbd_device);
     176                 :          0 :                         spdk_jsonrpc_send_error_response(request, -EBUSY, spdk_strerror(-rc));
     177                 :          0 :                         goto invalid;
     178                 :            :                 }
     179                 :            : 
     180         [ -  + ]:        466 :                 if (rc != 0) {
     181   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(nbd, "Illegal nbd_device %s.\n", req->nbd_device);
     182                 :          0 :                         spdk_jsonrpc_send_error_response_fmt(request, -ENODEV,
     183                 :            :                                                              "illegal nbd device %s", req->nbd_device);
     184                 :          0 :                         goto invalid;
     185                 :            :                 }
     186                 :            :         } else {
     187                 :        113 :                 req->nbd_idx = 0;
     188                 :        113 :                 req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
     189         [ -  + ]:        113 :                 if (req->nbd_device == NULL) {
     190   [ #  #  #  # ]:          0 :                         SPDK_INFOLOG(nbd, "There is no available nbd device.\n");
     191                 :          0 :                         spdk_jsonrpc_send_error_response(request, -ENODEV,
     192                 :            :                                                          "nbd device not found");
     193                 :          0 :                         goto invalid;
     194                 :            :                 }
     195                 :            :         }
     196                 :            : 
     197                 :        579 :         req->request = request;
     198                 :        579 :         spdk_nbd_start(req->bdev_name, req->nbd_device,
     199                 :            :                        rpc_start_nbd_done, req);
     200                 :            : 
     201                 :        579 :         return;
     202                 :            : 
     203                 :          0 : invalid:
     204                 :          0 :         free_rpc_nbd_start_disk(req);
     205                 :            : }
     206                 :            : 
     207                 :       1717 : SPDK_RPC_REGISTER("nbd_start_disk", rpc_nbd_start_disk, SPDK_RPC_RUNTIME)
     208                 :            : 
     209                 :            : struct rpc_nbd_stop_disk {
     210                 :            :         char *nbd_device;
     211                 :            : };
     212                 :            : 
     213                 :            : static void
     214                 :        578 : free_rpc_nbd_stop_disk(struct rpc_nbd_stop_disk *req)
     215                 :            : {
     216                 :        578 :         free(req->nbd_device);
     217                 :        578 : }
     218                 :            : 
     219                 :            : static const struct spdk_json_object_decoder rpc_nbd_stop_disk_decoders[] = {
     220                 :            :         {"nbd_device", offsetof(struct rpc_nbd_stop_disk, nbd_device), spdk_json_decode_string},
     221                 :            : };
     222                 :            : 
     223                 :            : struct nbd_disconnect_arg {
     224                 :            :         struct spdk_jsonrpc_request *request;
     225                 :            :         struct spdk_nbd_disk *nbd;
     226                 :            : };
     227                 :            : 
     228                 :            : static void *
     229                 :        578 : nbd_disconnect_thread(void *arg)
     230                 :            : {
     231                 :        578 :         struct nbd_disconnect_arg *thd_arg = arg;
     232                 :            : 
     233                 :        578 :         spdk_unaffinitize_thread();
     234                 :            : 
     235                 :        578 :         nbd_disconnect(thd_arg->nbd);
     236                 :            : 
     237                 :        578 :         spdk_jsonrpc_send_bool_response(thd_arg->request, true);
     238                 :            : 
     239                 :        578 :         free(thd_arg);
     240                 :        578 :         pthread_exit(NULL);
     241                 :            : }
     242                 :            : 
     243                 :            : static void
     244                 :        578 : rpc_nbd_stop_disk(struct spdk_jsonrpc_request *request,
     245                 :            :                   const struct spdk_json_val *params)
     246                 :            : {
     247                 :        578 :         struct rpc_nbd_stop_disk req = {};
     248                 :            :         struct spdk_nbd_disk *nbd;
     249                 :        395 :         pthread_t tid;
     250                 :        578 :         struct nbd_disconnect_arg *thd_arg = NULL;
     251                 :            :         int rc;
     252                 :            : 
     253         [ -  + ]:        578 :         if (spdk_json_decode_object(params, rpc_nbd_stop_disk_decoders,
     254                 :            :                                     SPDK_COUNTOF(rpc_nbd_stop_disk_decoders),
     255                 :            :                                     &req)) {
     256                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     257                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     258                 :            :                                                  "spdk_json_decode_object failed");
     259                 :          0 :                 goto out;
     260                 :            :         }
     261                 :            : 
     262         [ -  + ]:        578 :         if (req.nbd_device == NULL) {
     263                 :          0 :                 spdk_jsonrpc_send_error_response(request, -ENODEV, "invalid nbd device");
     264                 :          0 :                 goto out;
     265                 :            :         }
     266                 :            : 
     267                 :            :         /* make sure nbd_device is registered */
     268                 :        578 :         nbd = nbd_disk_find_by_nbd_path(req.nbd_device);
     269         [ -  + ]:        578 :         if (!nbd) {
     270                 :          0 :                 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
     271                 :          0 :                 goto out;
     272                 :            :         }
     273                 :            : 
     274                 :            :         /*
     275                 :            :          * thd_arg should be freed by created thread
     276                 :            :          * if thread is created successfully.
     277                 :            :          */
     278                 :        578 :         thd_arg = malloc(sizeof(*thd_arg));
     279         [ -  + ]:        578 :         if (!thd_arg) {
     280                 :          0 :                 SPDK_ERRLOG("could not allocate nbd disconnect thread arg\n");
     281                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
     282                 :          0 :                 goto out;
     283                 :            :         }
     284                 :            : 
     285                 :        578 :         thd_arg->request = request;
     286                 :        578 :         thd_arg->nbd = nbd;
     287                 :            : 
     288                 :            :         /*
     289                 :            :          * NBD ioctl of disconnect will block until data are flushed.
     290                 :            :          * Create separate thread to execute it.
     291                 :            :          */
     292   [ -  +  -  + ]:        578 :         rc = pthread_create(&tid, NULL, nbd_disconnect_thread, (void *)thd_arg);
     293         [ -  + ]:        578 :         if (rc != 0) {
     294                 :          0 :                 SPDK_ERRLOG("could not create nbd disconnect thread: %s\n", spdk_strerror(rc));
     295                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(rc));
     296                 :          0 :                 free(thd_arg);
     297                 :          0 :                 goto out;
     298                 :            :         }
     299                 :            : 
     300                 :        578 :         rc = pthread_detach(tid);
     301         [ +  - ]:        578 :         if (rc != 0) {
     302                 :          0 :                 SPDK_ERRLOG("could not detach nbd disconnect thread: %s\n", spdk_strerror(rc));
     303                 :          0 :                 goto out;
     304                 :            :         }
     305                 :            : 
     306                 :        578 : out:
     307                 :        578 :         free_rpc_nbd_stop_disk(&req);
     308                 :        578 : }
     309                 :            : 
     310                 :       1717 : SPDK_RPC_REGISTER("nbd_stop_disk", rpc_nbd_stop_disk, SPDK_RPC_RUNTIME)
     311                 :            : 
     312                 :            : static void
     313                 :        354 : rpc_dump_nbd_info(struct spdk_json_write_ctx *w,
     314                 :            :                   struct spdk_nbd_disk *nbd)
     315                 :            : {
     316                 :        354 :         spdk_json_write_object_begin(w);
     317                 :            : 
     318                 :        354 :         spdk_json_write_named_string(w, "nbd_device", nbd_disk_get_nbd_path(nbd));
     319                 :            : 
     320                 :        354 :         spdk_json_write_named_string(w, "bdev_name", nbd_disk_get_bdev_name(nbd));
     321                 :            : 
     322                 :        354 :         spdk_json_write_object_end(w);
     323                 :        354 : }
     324                 :            : 
     325                 :            : struct rpc_nbd_get_disks {
     326                 :            :         char *nbd_device;
     327                 :            : };
     328                 :            : 
     329                 :            : static void
     330                 :          0 : free_rpc_nbd_get_disks(struct rpc_nbd_get_disks *r)
     331                 :            : {
     332                 :          0 :         free(r->nbd_device);
     333                 :          0 : }
     334                 :            : 
     335                 :            : static const struct spdk_json_object_decoder rpc_nbd_get_disks_decoders[] = {
     336                 :            :         {"nbd_device", offsetof(struct rpc_nbd_get_disks, nbd_device), spdk_json_decode_string, true},
     337                 :            : };
     338                 :            : 
     339                 :            : static void
     340                 :        228 : rpc_nbd_get_disks(struct spdk_jsonrpc_request *request,
     341                 :            :                   const struct spdk_json_val *params)
     342                 :            : {
     343                 :        228 :         struct rpc_nbd_get_disks req = {};
     344                 :            :         struct spdk_json_write_ctx *w;
     345                 :        228 :         struct spdk_nbd_disk *nbd = NULL;
     346                 :            : 
     347         [ -  + ]:        228 :         if (params != NULL) {
     348         [ #  # ]:          0 :                 if (spdk_json_decode_object(params, rpc_nbd_get_disks_decoders,
     349                 :            :                                             SPDK_COUNTOF(rpc_nbd_get_disks_decoders),
     350                 :            :                                             &req)) {
     351                 :          0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
     352                 :          0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     353                 :            :                                                          "spdk_json_decode_object failed");
     354                 :          0 :                         goto invalid;
     355                 :            :                 }
     356                 :            : 
     357         [ #  # ]:          0 :                 if (req.nbd_device) {
     358                 :          0 :                         nbd = nbd_disk_find_by_nbd_path(req.nbd_device);
     359         [ #  # ]:          0 :                         if (nbd == NULL) {
     360                 :          0 :                                 SPDK_ERRLOG("nbd device '%s' does not exist\n", req.nbd_device);
     361                 :          0 :                                 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
     362                 :          0 :                                 goto invalid;
     363                 :            :                         }
     364                 :            : 
     365                 :          0 :                         free_rpc_nbd_get_disks(&req);
     366                 :            :                 }
     367                 :            :         }
     368                 :            : 
     369                 :        228 :         w = spdk_jsonrpc_begin_result(request);
     370                 :        228 :         spdk_json_write_array_begin(w);
     371                 :            : 
     372         [ -  + ]:        228 :         if (nbd != NULL) {
     373                 :          0 :                 rpc_dump_nbd_info(w, nbd);
     374                 :            :         } else {
     375         [ +  + ]:        582 :                 for (nbd = nbd_disk_first(); nbd != NULL; nbd = nbd_disk_next(nbd)) {
     376                 :        354 :                         rpc_dump_nbd_info(w, nbd);
     377                 :            :                 }
     378                 :            :         }
     379                 :            : 
     380                 :        228 :         spdk_json_write_array_end(w);
     381                 :            : 
     382                 :        228 :         spdk_jsonrpc_end_result(request, w);
     383                 :            : 
     384                 :        228 :         return;
     385                 :            : 
     386                 :          0 : invalid:
     387                 :          0 :         free_rpc_nbd_get_disks(&req);
     388                 :            : }
     389                 :       1717 : SPDK_RPC_REGISTER("nbd_get_disks", rpc_nbd_get_disks, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.14