struct rpc_start_nbd_disk {
char *bdev_name;
char *nbd_device;
+ /* Used to search one available nbd device */
+ int nbd_idx;
+ bool nbd_idx_specified;
+ struct spdk_jsonrpc_request *request;
};
static void
{
free(req->bdev_name);
free(req->nbd_device);
+ free(req);
}
static const struct spdk_json_object_decoder rpc_start_nbd_disk_decoders[] = {
{"bdev_name", offsetof(struct rpc_start_nbd_disk, bdev_name), spdk_json_decode_string},
- {"nbd_device", offsetof(struct rpc_start_nbd_disk, nbd_device), spdk_json_decode_string},
+ {"nbd_device", offsetof(struct rpc_start_nbd_disk, nbd_device), spdk_json_decode_string, true},
};
+/* Return 0 to indicate the nbd_device might be available,
+ * or non-zero to indicate the nbd_device is invalid or in using.
+ */
+static int
+check_available_nbd_disk(char *nbd_device)
+{
+ char nbd_block_path[256];
+ char tail[2];
+ int rc;
+ unsigned int nbd_idx;
+ struct spdk_nbd_disk *nbd;
+
+ /* nbd device path must be in format of /dev/nbd<num>, with no tail. */
+ rc = sscanf(nbd_device, "/dev/nbd%u%1s", &nbd_idx, tail);
+ if (rc != 1) {
+ return -1;
+ }
+
+ /* make sure nbd_device is not registered inside SPDK */
+ nbd = spdk_nbd_disk_find_by_nbd_path(nbd_device);
+ if (nbd) {
+ /* nbd_device is in using */
+ return 1;
+ }
+
+ /* A valid pid file in /sys/block indicates the device is in using */
+ snprintf(nbd_block_path, 256, "/sys/block/nbd%u/pid", nbd_idx);
+
+ rc = open(nbd_block_path, O_RDONLY);
+ if (rc < 0) {
+ if (errno == ENOENT) {
+ /* nbd_device might be available */
+ return 0;
+ } else {
+ SPDK_ERRLOG("Failed to check PID file %s: %s\n", nbd_block_path, spdk_strerror(errno));
+ return -1;
+ }
+ }
+
+ close(rc);
+
+ /* nbd_device is in using */
+ return 1;
+}
+
+static char *
+find_available_nbd_disk(int nbd_idx, int *next_nbd_idx)
+{
+ int i, rc;
+ char nbd_device[20];
+
+ for (i = nbd_idx; ; i++) {
+ snprintf(nbd_device, 20, "/dev/nbd%d", i);
+ /* Check whether an nbd device exists in order to reach the last one nbd device */
+ rc = access(nbd_device, F_OK);
+ if (rc != 0) {
+ break;
+ }
+
+ rc = check_available_nbd_disk(nbd_device);
+ if (rc == 0) {
+ if (next_nbd_idx != NULL) {
+ *next_nbd_idx = i + 1;
+ }
+
+ return strdup(nbd_device);
+ }
+ }
+
+ return NULL;
+}
+
+static void
+spdk_rpc_start_nbd_done(void *cb_arg, struct spdk_nbd_disk *nbd, int rc)
+{
+ struct rpc_start_nbd_disk *req = cb_arg;
+ struct spdk_jsonrpc_request *request = req->request;
+ struct spdk_json_write_ctx *w;
+
+ /* Check whether it's automatic nbd-device assignment */
+ if (rc == -EBUSY && req->nbd_idx_specified == false) {
+ free(req->nbd_device);
+
+ req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
+ if (req->nbd_device != NULL) {
+ spdk_nbd_start(req->bdev_name, req->nbd_device,
+ spdk_rpc_start_nbd_done, req);
+ return;
+ }
+
+ SPDK_INFOLOG(SPDK_LOG_NBD, "There is no available nbd device.\n");
+ }
+
+ if (rc) {
+ spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
+ return;
+ }
+
+ w = spdk_jsonrpc_begin_result(request);
+ if (w == NULL) {
+ return;
+ }
+
+ spdk_json_write_string(w, spdk_nbd_get_path(nbd));
+ spdk_jsonrpc_end_result(request, w);
+
+ free_rpc_start_nbd_disk(req);
+}
+
static void
spdk_rpc_start_nbd_disk(struct spdk_jsonrpc_request *request,
const struct spdk_json_val *params)
{
- struct rpc_start_nbd_disk req = {};
- struct spdk_json_write_ctx *w;
- struct spdk_nbd_disk *nbd;
+ struct rpc_start_nbd_disk *req;
+ int rc;
+
+ req = calloc(1, sizeof(*req));
+ if (req == NULL) {
+ SPDK_ERRLOG("could not allocate start_nbd_disk request.\n");
+ spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
+ return;
+ }
if (spdk_json_decode_object(params, rpc_start_nbd_disk_decoders,
SPDK_COUNTOF(rpc_start_nbd_disk_decoders),
- &req)) {
+ req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
}
- if (req.nbd_device == NULL || req.bdev_name == NULL) {
+ if (req->bdev_name == NULL) {
goto invalid;
}
- /* make sure nbd_device is not registered */
- nbd = spdk_nbd_disk_find_by_nbd_path(req.nbd_device);
- if (nbd) {
- goto invalid;
- }
+ if (req->nbd_device != NULL) {
+ req->nbd_idx_specified = true;
+ rc = check_available_nbd_disk(req->nbd_device);
+ if (rc == 1) {
+ SPDK_DEBUGLOG(SPDK_LOG_NBD, "NBD device %s is in using.\n", req->nbd_device);
+ goto invalid;
+ }
- nbd = spdk_nbd_start(req.bdev_name, req.nbd_device);
- if (!nbd) {
- goto invalid;
+ if (rc != 0) {
+ SPDK_DEBUGLOG(SPDK_LOG_NBD, "Illegal nbd_device %s.\n", req->nbd_device);
+ goto invalid;
+ }
+ } else {
+ req->nbd_idx = 0;
+ req->nbd_device = find_available_nbd_disk(req->nbd_idx, &req->nbd_idx);
+ if (req->nbd_device == NULL) {
+ SPDK_INFOLOG(SPDK_LOG_NBD, "There is no available nbd device.\n");
+ goto invalid;
+ }
}
- w = spdk_jsonrpc_begin_result(request);
- if (w == NULL) {
- free_rpc_start_nbd_disk(&req);
- return;
- }
+ req->request = request;
+ spdk_nbd_start(req->bdev_name, req->nbd_device,
+ spdk_rpc_start_nbd_done, req);
- spdk_json_write_string(w, req.nbd_device);
- spdk_jsonrpc_end_result(request, w);
- free_rpc_start_nbd_disk(&req);
return;
invalid:
- free_rpc_start_nbd_disk(&req);
+ free_rpc_start_nbd_disk(req);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
}
{
spdk_json_write_object_begin(w);
- spdk_json_write_name(w, "nbd_device");
- spdk_json_write_string(w, spdk_nbd_disk_get_nbd_path(nbd));
+ spdk_json_write_named_string(w, "nbd_device", spdk_nbd_disk_get_nbd_path(nbd));
- spdk_json_write_name(w, "bdev_name");
- spdk_json_write_string(w, spdk_nbd_disk_get_bdev_name(nbd));
+ spdk_json_write_named_string(w, "bdev_name", spdk_nbd_disk_get_bdev_name(nbd));
spdk_json_write_object_end(w);
}