LCOV - code coverage report
Current view: top level - lib/nbd - nbd_rpc.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 0 172 0.0 %
Date: 2024-11-05 10:06:02 Functions: 0 14 0.0 %

          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           0 : free_rpc_nbd_start_disk(struct rpc_nbd_start_disk *req)
      27             : {
      28           0 :         free(req->bdev_name);
      29           0 :         free(req->nbd_device);
      30           0 :         free(req);
      31           0 : }
      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           0 : check_available_nbd_disk(char *nbd_device)
      43             : {
      44           0 :         char nbd_block_path[256];
      45           0 :         char tail[2];
      46             :         int rc;
      47           0 :         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           0 :         rc = sscanf(nbd_device, "/dev/nbd%u%1s", &nbd_idx, tail);
      52           0 :         if (rc != 1) {
      53           0 :                 return -errno;
      54             :         }
      55             : 
      56             :         /* make sure nbd_device is not registered inside SPDK */
      57           0 :         nbd = nbd_disk_find_by_nbd_path(nbd_device);
      58           0 :         if (nbd) {
      59             :                 /* nbd_device is in use */
      60           0 :                 return -EBUSY;
      61             :         }
      62             : 
      63             :         /* A valid pid file in /sys/block indicates the device is in use */
      64           0 :         snprintf(nbd_block_path, 256, "/sys/block/nbd%u/pid", nbd_idx);
      65             : 
      66           0 :         rc = open(nbd_block_path, O_RDONLY);
      67           0 :         if (rc < 0) {
      68           0 :                 if (errno == ENOENT) {
      69             :                         /* nbd_device might be available */
      70           0 :                         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           0 : find_available_nbd_disk(int nbd_idx, int *next_nbd_idx)
      85             : {
      86             :         int i, rc;
      87           0 :         char nbd_device[20];
      88             : 
      89           0 :         for (i = nbd_idx; ; i++) {
      90           0 :                 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           0 :                 rc = access(nbd_device, F_OK);
      93           0 :                 if (rc != 0) {
      94           0 :                         break;
      95             :                 }
      96             : 
      97           0 :                 rc = check_available_nbd_disk(nbd_device);
      98           0 :                 if (rc == 0) {
      99           0 :                         if (next_nbd_idx != NULL) {
     100           0 :                                 *next_nbd_idx = i + 1;
     101             :                         }
     102             : 
     103           0 :                         return strdup(nbd_device);
     104             :                 }
     105             :         }
     106             : 
     107           0 :         return NULL;
     108             : }
     109             : 
     110             : static void
     111           0 : rpc_start_nbd_done(void *cb_arg, struct spdk_nbd_disk *nbd, int rc)
     112             : {
     113           0 :         struct rpc_nbd_start_disk *req = cb_arg;
     114           0 :         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           0 :         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           0 :         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           0 :         w = spdk_jsonrpc_begin_result(request);
     138           0 :         spdk_json_write_string(w, spdk_nbd_get_path(nbd));
     139           0 :         spdk_jsonrpc_end_result(request, w);
     140             : 
     141           0 :         free_rpc_nbd_start_disk(req);
     142             : }
     143             : 
     144             : static void
     145           0 : 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           0 :         req = calloc(1, sizeof(*req));
     152           0 :         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           0 :         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           0 :         if (req->bdev_name == NULL) {
     168           0 :                 goto invalid;
     169             :         }
     170             : 
     171           0 :         if (req->nbd_device != NULL) {
     172           0 :                 req->nbd_idx_specified = true;
     173           0 :                 rc = check_available_nbd_disk(req->nbd_device);
     174           0 :                 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           0 :                 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           0 :                 req->nbd_idx = 0;
     188           0 :                 req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
     189           0 :                 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           0 :         req->request = request;
     198           0 :         spdk_nbd_start(req->bdev_name, req->nbd_device,
     199             :                        rpc_start_nbd_done, req);
     200             : 
     201           0 :         return;
     202             : 
     203           0 : invalid:
     204           0 :         free_rpc_nbd_start_disk(req);
     205             : }
     206             : 
     207           0 : 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           0 : free_rpc_nbd_stop_disk(struct rpc_nbd_stop_disk *req)
     215             : {
     216           0 :         free(req->nbd_device);
     217           0 : }
     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           0 : nbd_disconnect_thread(void *arg)
     230             : {
     231           0 :         struct nbd_disconnect_arg *thd_arg = arg;
     232             : 
     233           0 :         spdk_unaffinitize_thread();
     234             : 
     235           0 :         nbd_disconnect(thd_arg->nbd);
     236             : 
     237           0 :         spdk_jsonrpc_send_bool_response(thd_arg->request, true);
     238             : 
     239           0 :         free(thd_arg);
     240           0 :         pthread_exit(NULL);
     241             : }
     242             : 
     243             : static void
     244           0 : rpc_nbd_stop_disk(struct spdk_jsonrpc_request *request,
     245             :                   const struct spdk_json_val *params)
     246             : {
     247           0 :         struct rpc_nbd_stop_disk req = {};
     248             :         struct spdk_nbd_disk *nbd;
     249           0 :         pthread_t tid;
     250           0 :         struct nbd_disconnect_arg *thd_arg = NULL;
     251             :         int rc;
     252             : 
     253           0 :         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           0 :         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           0 :         nbd = nbd_disk_find_by_nbd_path(req.nbd_device);
     269           0 :         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           0 :         thd_arg = malloc(sizeof(*thd_arg));
     279           0 :         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           0 :         thd_arg->request = request;
     286           0 :         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           0 :         rc = pthread_create(&tid, NULL, nbd_disconnect_thread, (void *)thd_arg);
     293           0 :         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           0 :         rc = pthread_detach(tid);
     301           0 :         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           0 : out:
     307           0 :         free_rpc_nbd_stop_disk(&req);
     308           0 : }
     309             : 
     310           0 : SPDK_RPC_REGISTER("nbd_stop_disk", rpc_nbd_stop_disk, SPDK_RPC_RUNTIME)
     311             : 
     312             : static void
     313           0 : rpc_dump_nbd_info(struct spdk_json_write_ctx *w,
     314             :                   struct spdk_nbd_disk *nbd)
     315             : {
     316           0 :         spdk_json_write_object_begin(w);
     317             : 
     318           0 :         spdk_json_write_named_string(w, "nbd_device", nbd_disk_get_nbd_path(nbd));
     319             : 
     320           0 :         spdk_json_write_named_string(w, "bdev_name", nbd_disk_get_bdev_name(nbd));
     321             : 
     322           0 :         spdk_json_write_object_end(w);
     323           0 : }
     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           0 : rpc_nbd_get_disks(struct spdk_jsonrpc_request *request,
     341             :                   const struct spdk_json_val *params)
     342             : {
     343           0 :         struct rpc_nbd_get_disks req = {};
     344             :         struct spdk_json_write_ctx *w;
     345           0 :         struct spdk_nbd_disk *nbd = NULL;
     346             : 
     347           0 :         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           0 :         w = spdk_jsonrpc_begin_result(request);
     370           0 :         spdk_json_write_array_begin(w);
     371             : 
     372           0 :         if (nbd != NULL) {
     373           0 :                 rpc_dump_nbd_info(w, nbd);
     374             :         } else {
     375           0 :                 for (nbd = nbd_disk_first(); nbd != NULL; nbd = nbd_disk_next(nbd)) {
     376           0 :                         rpc_dump_nbd_info(w, nbd);
     377             :                 }
     378             :         }
     379             : 
     380           0 :         spdk_json_write_array_end(w);
     381             : 
     382           0 :         spdk_jsonrpc_end_result(request, w);
     383             : 
     384           0 :         return;
     385             : 
     386           0 : invalid:
     387           0 :         free_rpc_nbd_get_disks(&req);
     388             : }
     389           0 : SPDK_RPC_REGISTER("nbd_get_disks", rpc_nbd_get_disks, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.15