Commit 4c06ce9b authored by Daniel Verkamp's avatar Daniel Verkamp Committed by Jim Harris
Browse files

util: add uuid.h to wrap libuuid



This lets us have a common place to put definitions like the length of
the UUID string, as well as abstract away some of the API warts in
libuuid (non-const values, no size checking for uuid_unparse, etc.).

Change-Id: I80607fcd21ce57fdbb8729442fbb721bc71ccb98
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/402176


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
parent 2532fd8f
Loading
Loading
Loading
Loading

include/spdk/uuid.h

0 → 100644
+94 −0
Original line number Diff line number Diff line
/*-
 *   BSD LICENSE
 *
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** \file
 * UUID types and functions
 */

#ifndef SPDK_UUID_H
#define SPDK_UUID_H

#include "spdk/stdinc.h"

#include "spdk/assert.h"

#ifdef __cplusplus
extern "C" {
#endif

struct spdk_uuid {
	union {
		uint8_t raw[16];
	} u;
};
SPDK_STATIC_ASSERT(sizeof(struct spdk_uuid) == 16, "Incorrect size");

#define SPDK_UUID_STRING_LEN 37 /* 36 characters + null terminator */

/**
 * Convert UUID in textual format into a spdk_uuid.
 *
 * \param uuid[output] User-provided UUID buffer.
 * \param uuid_str UUID in textual format in C string.
 * \return 0 on success, or negated errno on failure.
 */
int spdk_uuid_parse(struct spdk_uuid *uuid, const char *uuid_str);

/**
 * Convert UUID in spdk_uuid into lowercase textual format.
 *
 * \param uuid_str User-provided string buffer to write the textual format into.
 * \param uuid_str_size Size of uuid_str buffer.  Must be at least SPDK_UUID_STRING_LEN.
 * \param uuid UUID to convert to textual format.
 */
int spdk_uuid_fmt_lower(char *uuid_str, size_t uuid_str_size, const struct spdk_uuid *uuid);

/**
 * Compare two UUIDs.
 *
 * \return 0 if u1 == u2, less than 0 if u1 < u2, greater than 0 if u1 > u2
 */
int spdk_uuid_compare(const struct spdk_uuid *u1, const struct spdk_uuid *u2);

/**
 * Generate a new UUID.
 *
 * \param uuid[out] User-provided UUID buffer to fill.
 */
void spdk_uuid_generate(struct spdk_uuid *uuid);

#ifdef __cplusplus
}
#endif

#endif
+2 −6
Original line number Diff line number Diff line
@@ -36,16 +36,12 @@

#include "spdk/blob.h"
#include "spdk/lvol.h"
#include "spdk/uuid.h"
#include "spdk_internal/bdev.h"

#include <uuid/uuid.h>

/* Default size of blobstore cluster */
#define SPDK_LVS_OPTS_CLUSTER_SZ (4 * 1024 * 1024)

/* Length of string returned from uuid_unparse() */
#define UUID_STRING_LEN 37

struct spdk_lvs_req {
	spdk_lvs_op_complete    cb_fn;
	void                    *cb_arg;
@@ -84,7 +80,7 @@ struct spdk_lvol_store {
	struct spdk_blob_store		*blobstore;
	struct spdk_blob		*super_blob;
	spdk_blob_id			super_blob_id;
	uuid_t				uuid;
	struct spdk_uuid		uuid;
	struct spdk_lvs_req		*destruct_req;
	int				lvol_count;
	int				lvols_opened;
+8 −7
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include "spdk_internal/bdev.h"
#include "spdk_internal/log.h"
#include "spdk/string.h"
#include "spdk/uuid.h"

#include "vbdev_lvol.h"

@@ -374,14 +375,14 @@ vbdev_lvol_store_next(struct lvol_store_bdev *prev)
}

static struct spdk_lvol_store *
_vbdev_get_lvol_store_by_uuid(uuid_t uuid)
_vbdev_get_lvol_store_by_uuid(const struct spdk_uuid *uuid)
{
	struct spdk_lvol_store *lvs = NULL;
	struct lvol_store_bdev *lvs_bdev = vbdev_lvol_store_first();

	while (lvs_bdev != NULL) {
		lvs = lvs_bdev->lvs;
		if (uuid_compare(lvs->uuid, uuid) == 0) {
		if (spdk_uuid_compare(&lvs->uuid, uuid) == 0) {
			return lvs;
		}
		lvs_bdev = vbdev_lvol_store_next(lvs_bdev);
@@ -392,13 +393,13 @@ _vbdev_get_lvol_store_by_uuid(uuid_t uuid)
struct spdk_lvol_store *
vbdev_get_lvol_store_by_uuid(const char *uuid_str)
{
	uuid_t uuid;
	struct spdk_uuid uuid;

	if (uuid_parse(uuid_str, uuid)) {
	if (spdk_uuid_parse(&uuid, uuid_str)) {
		return NULL;
	}

	return _vbdev_get_lvol_store_by_uuid(uuid);
	return _vbdev_get_lvol_store_by_uuid(&uuid);
}

struct spdk_lvol_store *
@@ -524,7 +525,7 @@ vbdev_lvol_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
	struct spdk_lvol *lvol = ctx;
	struct lvol_store_bdev *lvs_bdev;
	struct spdk_bdev *bdev;
	char lvol_store_uuid[UUID_STRING_LEN];
	char lvol_store_uuid[SPDK_UUID_STRING_LEN];

	spdk_json_write_name(w, "lvol");
	spdk_json_write_object_begin(w);
@@ -532,7 +533,7 @@ vbdev_lvol_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
	lvs_bdev = vbdev_get_lvs_bdev_by_lvs(lvol->lvol_store);
	bdev = lvs_bdev->bdev;

	uuid_unparse(lvol->lvol_store->uuid, lvol_store_uuid);
	spdk_uuid_fmt_lower(lvol_store_uuid, sizeof(lvol_store_uuid), &lvol->lvol_store->uuid);
	spdk_json_write_name(w, "lvol_store_uuid");
	spdk_json_write_string(w, lvol_store_uuid);

+4 −4
Original line number Diff line number Diff line
@@ -93,14 +93,14 @@ static void
_spdk_rpc_lvol_store_construct_cb(void *cb_arg, struct spdk_lvol_store *lvol_store, int lvserrno)
{
	struct spdk_json_write_ctx *w;
	char lvol_store_uuid[UUID_STRING_LEN];
	char lvol_store_uuid[SPDK_UUID_STRING_LEN];
	struct spdk_jsonrpc_request *request = cb_arg;

	if (lvserrno != 0) {
		goto invalid;
	}

	uuid_unparse(lvol_store->uuid, lvol_store_uuid);
	spdk_uuid_fmt_lower(lvol_store_uuid, sizeof(lvol_store_uuid), &lvol_store->uuid);

	w = spdk_jsonrpc_begin_result(request);
	if (w == NULL) {
@@ -530,7 +530,7 @@ spdk_rpc_dump_lvol_store_info(struct spdk_json_write_ctx *w, struct lvol_store_b
{
	struct spdk_blob_store *bs;
	uint64_t cluster_size, block_size;
	char uuid[UUID_STRING_LEN];
	char uuid[SPDK_UUID_STRING_LEN];

	bs = lvs_bdev->lvs->blobstore;
	cluster_size = spdk_bs_get_cluster_size(bs);
@@ -539,7 +539,7 @@ spdk_rpc_dump_lvol_store_info(struct spdk_json_write_ctx *w, struct lvol_store_b

	spdk_json_write_object_begin(w);

	uuid_unparse(lvs_bdev->lvs->uuid, uuid);
	spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvs_bdev->lvs->uuid);
	spdk_json_write_name(w, "uuid");
	spdk_json_write_string(w, uuid);

+10 −13
Original line number Diff line number Diff line
@@ -37,9 +37,6 @@
#include "spdk/io_channel.h"
#include "spdk/blob_bdev.h"

/* Length of string returned from uuid_unparse() */
#define UUID_STRING_LEN 37

/* Default blob channel opts for lvol */
#define SPDK_LVOL_BLOB_OPTS_CHANNEL_OPS 512

@@ -160,7 +157,7 @@ _spdk_load_next_lvol(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
	struct spdk_blob_store *bs = lvs->blobstore;
	struct spdk_lvol *lvol, *tmp;
	spdk_blob_id blob_id;
	char uuid[UUID_STRING_LEN];
	char uuid[SPDK_UUID_STRING_LEN];
	const char *attr;
	size_t value_len;
	int rc;
@@ -196,7 +193,7 @@ _spdk_load_next_lvol(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
	lvol->lvol_store = lvs;
	lvol->num_clusters = spdk_blob_get_num_clusters(blob);
	lvol->close_only = false;
	uuid_unparse(lvol->lvol_store->uuid, uuid);
	spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvol->lvol_store->uuid);
	lvol->unique_id = spdk_sprintf_alloc("%s_%"PRIu64, uuid, (uint64_t)blob_id);
	if (!lvol->unique_id) {
		SPDK_ERRLOG("Cannot assign lvol name\n");
@@ -287,14 +284,14 @@ _spdk_lvs_read_uuid(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
	}

	rc = spdk_blob_get_xattr_value(blob, "uuid", (const void **)&attr, &value_len);
	if (rc != 0 || value_len != UUID_STRING_LEN || attr[UUID_STRING_LEN - 1] != '\0') {
	if (rc != 0 || value_len != SPDK_UUID_STRING_LEN || attr[SPDK_UUID_STRING_LEN - 1] != '\0') {
		SPDK_INFOLOG(SPDK_LOG_LVOL, "missing or incorrect UUID\n");
		req->lvserrno = -EINVAL;
		spdk_blob_close(blob, _spdk_close_super_blob_with_error_cb, req);
		return;
	}

	if (uuid_parse(attr, lvs->uuid)) {
	if (spdk_uuid_parse(&lvs->uuid, attr)) {
		SPDK_INFOLOG(SPDK_LOG_LVOL, "incorrect UUID '%s'\n", attr);
		req->lvserrno = -EINVAL;
		spdk_blob_close(blob, _spdk_close_super_blob_with_error_cb, req);
@@ -450,7 +447,7 @@ _spdk_super_blob_init_cb(void *cb_arg, int lvolerrno)
	struct spdk_lvs_with_handle_req *req = cb_arg;
	struct spdk_lvol_store *lvs = req->lvol_store;
	struct spdk_blob *blob = lvs->super_blob;
	char uuid[UUID_STRING_LEN];
	char uuid[SPDK_UUID_STRING_LEN];

	if (lvolerrno < 0) {
		req->cb_fn(req->cb_arg, NULL, lvolerrno);
@@ -460,9 +457,9 @@ _spdk_super_blob_init_cb(void *cb_arg, int lvolerrno)
		return;
	}

	uuid_unparse(lvs->uuid, uuid);
	spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvs->uuid);

	spdk_blob_set_xattr(blob, "uuid", uuid, UUID_STRING_LEN);
	spdk_blob_set_xattr(blob, "uuid", uuid, sizeof(uuid));
	spdk_blob_set_xattr(blob, "name", lvs->name, strnlen(lvs->name, SPDK_LVS_NAME_MAX) + 1);
	spdk_blob_sync_md(blob, _spdk_super_blob_set_cb, req);
}
@@ -584,7 +581,7 @@ spdk_lvs_init(struct spdk_bs_dev *bs_dev, struct spdk_lvs_opts *o,
		return -ENOMEM;
	}

	uuid_generate(lvs->uuid);
	spdk_uuid_generate(&lvs->uuid);
	strncpy(lvs->name, o->name, SPDK_LVS_NAME_MAX);

	rc = _spdk_add_lvs_to_list(lvs);
@@ -850,7 +847,7 @@ _spdk_lvol_create_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno)
	struct spdk_lvol_with_handle_req *req = cb_arg;
	spdk_blob_id blob_id = spdk_blob_get_id(blob);
	struct spdk_lvol *lvol = req->lvol;
	char uuid[UUID_STRING_LEN];
	char uuid[SPDK_UUID_STRING_LEN];

	if (lvolerrno < 0) {
		free(lvol);
@@ -864,7 +861,7 @@ _spdk_lvol_create_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno)

	TAILQ_INSERT_TAIL(&lvol->lvol_store->lvols, lvol, link);

	uuid_unparse(lvol->lvol_store->uuid, uuid);
	spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvol->lvol_store->uuid);
	lvol->unique_id = spdk_sprintf_alloc("%s_%"PRIu64, uuid, (uint64_t)blob_id);
	if (!lvol->unique_id) {
		SPDK_ERRLOG("Cannot alloc memory for lvol name\n");
Loading