Commit 891c12a6 authored by Pawel Wodkowski's avatar Pawel Wodkowski Committed by Jim Harris
Browse files

util: add spdk_strerror() wrapper with TLS support



This patch remove need for additional buffer when translating error code
to string.

Change-Id: Iaa60088b5c450581d3cdddbb425119b17d55a44b
Signed-off-by: default avatarPawel Wodkowski <pawelx.wodkowski@intel.com>
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/386114


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 4ebe8214
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -98,6 +98,16 @@ char *spdk_str_trim(char *s);
 */
void spdk_strerror_r(int errnum, char *buf, size_t buflen);

/**
 * Return the string version of an error from a static, thread-local buffer.
 * This function is thread safe.
 *
 * \param errnum Error code
 *
 * \return pointer to buffer upon success.
 */
const char *spdk_strerror(int errnum);

/**
 * Remove trailing newlines from the end of a string in place.
 *
+2 −6
Original line number Diff line number Diff line
@@ -66,16 +66,14 @@ static int
bdev_aio_open(struct file_disk *disk)
{
	int fd;
	char buf[64];

	fd = open(disk->filename, O_RDWR | O_DIRECT);
	if (fd < 0) {
		/* Try without O_DIRECT for non-disk files */
		fd = open(disk->filename, O_RDWR);
		if (fd < 0) {
			spdk_strerror_r(errno, buf, sizeof(buf));
			SPDK_ERRLOG("open() failed (file:%s), errno %d: %s\n",
				    disk->filename, errno, buf);
				    disk->filename, errno, spdk_strerror(errno));
			disk->fd = -1;
			return -1;
		}
@@ -90,7 +88,6 @@ static int
bdev_aio_close(struct file_disk *disk)
{
	int rc;
	char buf[64];

	if (disk->fd == -1) {
		return 0;
@@ -98,9 +95,8 @@ bdev_aio_close(struct file_disk *disk)

	rc = close(disk->fd);
	if (rc < 0) {
		spdk_strerror_r(errno, buf, sizeof(buf));
		SPDK_ERRLOG("close() failed (fd=%d), errno %d: %s\n",
			    disk->fd, errno, buf);
			    disk->fd, errno, spdk_strerror(errno));
		return -1;
	}

+16 −24
Original line number Diff line number Diff line
@@ -95,7 +95,6 @@ _spdk_rpc_lvol_store_construct_cb(void *cb_arg, struct spdk_lvol_store *lvol_sto
	struct spdk_json_write_ctx *w;
	char lvol_store_uuid[UUID_STRING_LEN];
	struct spdk_jsonrpc_request *request = cb_arg;
	char buf[64];

	if (lvserrno != 0) {
		goto invalid;
@@ -115,8 +114,8 @@ _spdk_rpc_lvol_store_construct_cb(void *cb_arg, struct spdk_lvol_store *lvol_sto
	return;

invalid:
	spdk_strerror_r(-lvserrno, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-lvserrno));
}

static void
@@ -126,7 +125,6 @@ spdk_rpc_construct_lvol_store(struct spdk_jsonrpc_request *request,
	struct rpc_construct_lvol_store req = {};
	struct spdk_bdev *bdev;
	int rc;
	char buf[64];

	if (spdk_json_decode_object(params, rpc_construct_lvol_store_decoders,
				    SPDK_COUNTOF(rpc_construct_lvol_store_decoders),
@@ -164,8 +162,8 @@ spdk_rpc_construct_lvol_store(struct spdk_jsonrpc_request *request,
	return;

invalid:
	spdk_strerror_r(-rc, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-rc));
	free_rpc_construct_lvol_store(&req);
}
SPDK_RPC_REGISTER("construct_lvol_store", spdk_rpc_construct_lvol_store)
@@ -192,7 +190,6 @@ _spdk_rpc_lvol_store_destroy_cb(void *cb_arg, int lvserrno)
{
	struct spdk_json_write_ctx *w;
	struct spdk_jsonrpc_request *request = cb_arg;
	char buf[64];

	if (lvserrno != 0) {
		goto invalid;
@@ -208,8 +205,8 @@ _spdk_rpc_lvol_store_destroy_cb(void *cb_arg, int lvserrno)
	return;

invalid:
	spdk_strerror_r(-lvserrno, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-lvserrno));
}

static void
@@ -219,7 +216,6 @@ spdk_rpc_destroy_lvol_store(struct spdk_jsonrpc_request *request,
	struct rpc_destroy_lvol_store req = {};
	struct spdk_lvol_store *lvs = NULL;
	int rc;
	char buf[64];

	if (spdk_json_decode_object(params, rpc_destroy_lvol_store_decoders,
				    SPDK_COUNTOF(rpc_destroy_lvol_store_decoders),
@@ -241,8 +237,8 @@ spdk_rpc_destroy_lvol_store(struct spdk_jsonrpc_request *request,
	return;

invalid:
	spdk_strerror_r(-rc, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-rc));
	free_rpc_destroy_lvol_store(&req);
}
SPDK_RPC_REGISTER("destroy_lvol_store", spdk_rpc_destroy_lvol_store)
@@ -274,7 +270,6 @@ _spdk_rpc_construct_lvol_bdev_cb(void *cb_arg, struct spdk_lvol *lvol, int lvole
{
	struct spdk_json_write_ctx *w;
	struct spdk_jsonrpc_request *request = cb_arg;
	char buf[64];

	if (lvolerrno != 0) {
		goto invalid;
@@ -292,8 +287,8 @@ _spdk_rpc_construct_lvol_bdev_cb(void *cb_arg, struct spdk_lvol *lvol, int lvole
	return;

invalid:
	spdk_strerror_r(-lvolerrno, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-lvolerrno));
}

static void
@@ -303,7 +298,6 @@ spdk_rpc_construct_lvol_bdev(struct spdk_jsonrpc_request *request,
	struct rpc_construct_lvol_bdev req = {};
	size_t sz;
	int rc;
	char buf[64];
	struct spdk_lvol_store *lvs = NULL;

	SPDK_INFOLOG(SPDK_LOG_LVOL_RPC, "Creating blob\n");
@@ -338,8 +332,8 @@ spdk_rpc_construct_lvol_bdev(struct spdk_jsonrpc_request *request,
	return;

invalid:
	spdk_strerror_r(-rc, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-rc));
	free_rpc_construct_lvol_bdev(&req);
}

@@ -366,7 +360,6 @@ _spdk_rpc_resize_lvol_bdev_cb(void *cb_arg, int lvolerrno)
{
	struct spdk_json_write_ctx *w;
	struct spdk_jsonrpc_request *request = cb_arg;
	char buf[64];

	if (lvolerrno != 0) {
		goto invalid;
@@ -382,8 +375,8 @@ _spdk_rpc_resize_lvol_bdev_cb(void *cb_arg, int lvolerrno)
	return;

invalid:
	spdk_strerror_r(-lvolerrno, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-lvolerrno));
}

static void __attribute__((unused))
@@ -392,7 +385,6 @@ spdk_rpc_resize_lvol_bdev(struct spdk_jsonrpc_request *request,
{
	struct rpc_resize_lvol_bdev req = {};
	int rc = 0;
	char buf[64];

	SPDK_INFOLOG(SPDK_LOG_LVOL_RPC, "Resizing lvol\n");

@@ -419,8 +411,8 @@ spdk_rpc_resize_lvol_bdev(struct spdk_jsonrpc_request *request,
	return;

invalid:
	spdk_strerror_r(-rc, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-rc));
	free_rpc_resize_lvol_bdev(&req);
}

+1 −3
Original line number Diff line number Diff line
@@ -63,7 +63,6 @@ spdk_rpc_construct_pmem_bdev(struct spdk_jsonrpc_request *request,
	struct rpc_construct_pmem req = {};
	struct spdk_json_write_ctx *w;
	struct spdk_bdev *bdev;
	char buf[64];
	int rc;

	if (spdk_json_decode_object(params, rpc_construct_pmem_decoders,
@@ -97,8 +96,7 @@ spdk_rpc_construct_pmem_bdev(struct spdk_jsonrpc_request *request,
	return;

invalid:
	spdk_strerror_r(rc, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(rc));
	free_rpc_construct_pmem_bdev(&req);
}
SPDK_RPC_REGISTER("construct_pmem_bdev", spdk_rpc_construct_pmem_bdev)
+4 −6
Original line number Diff line number Diff line
@@ -73,12 +73,11 @@ rpc_create_virtio_user_scsi_bdev_cb(void *ctx, int result, struct spdk_bdev **bd
{
	struct rpc_virtio_user_scsi_dev *req = ctx;
	struct spdk_json_write_ctx *w;
	char buf[64];
	size_t i;

	if (result) {
		spdk_strerror_r(-result, buf, sizeof(buf));
		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
						 spdk_strerror(-result));
		free_rpc_connect_virtio_user_scsi_dev(req);
		return;
	}
@@ -103,7 +102,6 @@ spdk_rpc_create_virtio_user_scsi_bdev(struct spdk_jsonrpc_request *request,
				      const struct spdk_json_val *params)
{
	struct rpc_virtio_user_scsi_dev *req;
	char buf[64];
	int rc;

	req = calloc(1, sizeof(*req));
@@ -132,8 +130,8 @@ spdk_rpc_create_virtio_user_scsi_bdev(struct spdk_jsonrpc_request *request,
	return;

invalid:
	spdk_strerror_r(-rc, buf, sizeof(buf));
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, buf);
	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
					 spdk_strerror(-rc));
	free_rpc_connect_virtio_user_scsi_dev(req);
}
SPDK_RPC_REGISTER("construct_virtio_user_scsi_bdev", spdk_rpc_create_virtio_user_scsi_bdev);
Loading