Commit 3df67370 authored by Vasilii Ivanov's avatar Vasilii Ivanov Committed by Tomasz Zawadzki
Browse files

lib/bdev: Add spdk_for_each_bdev_by_name to iterate over array of bdev names



External application might store array of created bdevs, and it might be
useful to iterate over concrete bdevs. Currently possible solutions would
be either iterate over all bdevs and filter bdevs by ourselfs, or
iterate over needed bdevs one by one using spdk_bdev_open_ext.
Patch introduces API to path the array of bdev names to iteration that
allows to simplify described process.

Change-Id: I37e1ba61f1dcf706d5d837deff4b0ea2c5e4e726
Signed-off-by: default avatarVasilii Ivanov <iwanovvvasilij@gmail.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/26941


Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
Reviewed-by: default avatarJacek Kalwas <jacek.kalwas@nutanix.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
parent ae9e5441
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -613,6 +613,23 @@ int spdk_for_each_bdev(void *ctx, spdk_for_each_bdev_fn fn);
 */
int spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn);

/**
 * Call the provided callback function on block devices with provided names.
 *
 * spdk_for_each_bdev_by_name() stops iteration if bdev with one of provided names does not exist,
 * or if fn returns negated errno.
 *
 * \param ctx Context passed to the callback function.
 * \param fn Callback function for each block device.
 * \param names Array of bdev names to iterate, all of them should exist to finish iteration successfully.
 * \param count Count of bdevs to iterate.
 *
 * \return 0 if operation is successful, or suitable errno value one of the
 * callback returned or -ENODEV if bdev with passed name is not present.
 */
int spdk_for_each_bdev_by_name(void *ctx, spdk_for_each_bdev_fn fn, const char **names,
			       size_t count);

/**
 * Get the bdev associated with a bdev descriptor.
 *
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

SO_VER := 18
SO_MINOR := 0
SO_MINOR := 1

C_SRCS = bdev.c bdev_rpc.c bdev_zone.c part.c scsi_nvme.c
C_SRCS-$(CONFIG_VTUNE) += vtune.c
+27 −0
Original line number Diff line number Diff line
@@ -9762,6 +9762,33 @@ spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn)
	return rc;
}

int
spdk_for_each_bdev_by_name(void *ctx, spdk_for_each_bdev_fn fn, const char **names, size_t count)
{
	struct spdk_bdev *bdev;
	struct spdk_bdev_desc *desc;
	int rc = 0;
	size_t i = 0;

	assert(fn != NULL);

	for (i = 0; i < count; i++) {
		rc = spdk_bdev_open_ext(names[i], false, _tmp_bdev_event_cb, NULL, &desc);
		if (rc != 0) {
			SPDK_DEBUGLOG(bdev, "Failed to open bdev '%s': %d\n", names[i], rc);
			break;
		}
		bdev = spdk_bdev_desc_get_bdev(desc);
		rc = fn(ctx, bdev);
		spdk_bdev_close(desc);
		if (rc != 0) {
			break;
		}
	}

	return rc;
}

void
spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp)
{
+1 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
	spdk_bdev_next_leaf;
	spdk_for_each_bdev;
	spdk_for_each_bdev_leaf;
	spdk_for_each_bdev_by_name;
	spdk_bdev_open_ext;
	spdk_bdev_open_ext_v2;
	spdk_bdev_open_opts_init;
+11 −0
Original line number Diff line number Diff line
@@ -6442,6 +6442,8 @@ static void
for_each_bdev_test(void)
{
	struct spdk_bdev *bdev[8];
	const char *not_existing_bdev_names[4] = {"bdev1", "bdev2", "bdev100", "bdev4"};
	const char *existing_bdev_names[4] = {"bdev1", "bdev2", "bdev3", "bdev4"};
	int rc, count;

	bdev[0] = allocate_bdev("bdev0");
@@ -6477,6 +6479,15 @@ for_each_bdev_test(void)
	CU_ASSERT(rc == 0);
	CU_ASSERT(count == 4);

	count = 0;
	rc = spdk_for_each_bdev_by_name(&count, count_bdevs, not_existing_bdev_names, 4);
	CU_ASSERT(rc == -ENODEV);

	count = 0;
	rc = spdk_for_each_bdev_by_name(&count, count_bdevs, existing_bdev_names, 4);
	CU_ASSERT(rc == 0);
	CU_ASSERT(count == 4);

	bdev[0]->internal.status = SPDK_BDEV_STATUS_READY;
	free_bdev(bdev[0]);
	free_bdev(bdev[1]);