bdev/rpc: use goto to make code reuse.

delte duplicate code and use goto to make code reuse.

Signed-off-by: sunshihao <sunshihao@huawei.com>
Change-Id: Ib5eae0d31699fa7e3a0e6147b1572d383208df0b
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5052
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
sunshihao520 2020-11-09 11:25:31 +08:00 committed by Jim Harris
parent d2a0706982
commit bf13dc4801

View File

@ -803,34 +803,25 @@ rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware)); firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware));
if (!firm_ctx->req) { if (!firm_ctx->req) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "Memory allocation error.");
"Memory allocation error."); goto err;
free(firm_ctx);
return;
} }
if (spdk_json_decode_object(params, rpc_apply_firmware_decoders, if (spdk_json_decode_object(params, rpc_apply_firmware_decoders,
SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) { SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "spdk_json_decode_object failed.");
"spdk_json_decode_object failed."); goto err;
free(firm_ctx->req);
free(firm_ctx);
return;
} }
if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) { if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) {
snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name); snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) { if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) {
snprintf(msg, sizeof(msg), "Controller information for %s were not found.", snprintf(msg, sizeof(msg), "Controller information for %s were not found.",
firm_ctx->req->bdev_name); firm_ctx->req->bdev_name);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
firm_ctx->ctrlr = ctrlr; firm_ctx->ctrlr = ctrlr;
@ -842,17 +833,13 @@ rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
if (!(opt = malloc(sizeof(struct open_descriptors)))) { if (!(opt = malloc(sizeof(struct open_descriptors)))) {
snprintf(msg, sizeof(msg), "Memory allocation error."); snprintf(msg, sizeof(msg), "Memory allocation error.");
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
if (spdk_bdev_open(bdev2, true, NULL, NULL, &desc) != 0) { if (spdk_bdev_open(bdev2, true, NULL, NULL, &desc) != 0) {
snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name); snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
free(opt); free(opt);
apply_firmware_cleanup(firm_ctx); goto err;
return;
} }
/* Save the thread where the base device is opened */ /* Save the thread where the base device is opened */
@ -875,61 +862,49 @@ rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
} }
if (!firm_ctx->desc) { if (!firm_ctx->desc) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "No descriptor were found.");
"No descriptor were found."); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc); firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc);
if (!firm_ctx->ch) { if (!firm_ctx->ch) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "No channels were found.");
"No channels were found."); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
fd = open(firm_ctx->req->filename, O_RDONLY); fd = open(firm_ctx->req->filename, O_RDONLY);
if (fd < 0) { if (fd < 0) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "open file failed."); snprintf(msg, sizeof(msg), "open file failed.");
apply_firmware_cleanup(firm_ctx); goto err;
return;
} }
rc = fstat(fd, &fw_stat); rc = fstat(fd, &fw_stat);
if (rc < 0) { if (rc < 0) {
close(fd); close(fd);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "fstat failed."); snprintf(msg, sizeof(msg), "fstat failed.");
apply_firmware_cleanup(firm_ctx); goto err;
return;
} }
firm_ctx->size = fw_stat.st_size; firm_ctx->size = fw_stat.st_size;
if (fw_stat.st_size % 4) { if (fw_stat.st_size % 4) {
close(fd); close(fd);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4.");
"Firmware image size is not multiple of 4."); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL, firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL,
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
if (!firm_ctx->fw_image) { if (!firm_ctx->fw_image) {
close(fd); close(fd);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "Memory allocation error.");
"Memory allocation error."); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
firm_ctx->p = firm_ctx->fw_image; firm_ctx->p = firm_ctx->fw_image;
if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) { if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) {
close(fd); close(fd);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "Read firmware image failed!");
"Read firmware image failed!"); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
close(fd); close(fd);
@ -939,10 +914,8 @@ rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
cmd = malloc(sizeof(struct spdk_nvme_cmd)); cmd = malloc(sizeof(struct spdk_nvme_cmd));
if (!cmd) { if (!cmd) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, snprintf(msg, sizeof(msg), "Memory allocation error.");
"Memory allocation error."); goto err;
apply_firmware_cleanup(firm_ctx);
return;
} }
memset(cmd, 0, sizeof(struct spdk_nvme_cmd)); memset(cmd, 0, sizeof(struct spdk_nvme_cmd));
cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
@ -952,13 +925,16 @@ rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p, rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p,
firm_ctx->transfer, apply_firmware_complete, firm_ctx); firm_ctx->transfer, apply_firmware_complete, firm_ctx);
if (rc) { if (rc == 0) {
free(cmd); /* normal return here. */
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
"Read firmware image failed!");
apply_firmware_cleanup(firm_ctx);
return; return;
} }
free(cmd);
snprintf(msg, sizeof(msg), "Read firmware image failed!");
err:
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
apply_firmware_cleanup(firm_ctx);
} }
SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME) SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME)
SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware) SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware)