Blame SOURCES/0180-multipath-tools-Add-rbd-checker.patch

c896fb
From e28c340ed961409700d46a1cb9a820a8b7a4d016 Mon Sep 17 00:00:00 2001
c896fb
From: Mike Christie <mchristi@redhat.com>
c896fb
Date: Thu, 11 Aug 2016 02:12:12 -0500
c896fb
Subject: [PATCH 04/11] multipath-tools: Add rbd checker.
c896fb
c896fb
For BZ 1348372 from upstream commit:
c896fb
c896fb
commit d1cad5649b6fcf9027d43ca0405c900080133e32
c896fb
Author: Mike Christie <mchristi@redhat.com>
c896fb
Date:   Mon Aug 8 07:01:49 2016 -0500
c896fb
c896fb
    multipath-tools: Add rbd checker.
c896fb
c896fb
    This checker currently only handles the case where a path is failed
c896fb
    due to it being blacklisted by the ceph cluster. The specific use
c896fb
    case for me is when LIO exports rbd images through multiple LIO
c896fb
    instances.
c896fb
c896fb
    The problem it handles is when rbd instance1 has the exclusive lock,
c896fb
    but becomes unreachable another host in the cluster will take over
c896fb
    and blacklist the instance1. This prevents it from sending stale IO
c896fb
    and corrupting data.
c896fb
c896fb
    Later, when the host is reachable, we will want to failback to it.
c896fb
    To this, the checker will detect we were blacklisted, unmap the old
c896fb
    image which will make sure old IO is failed, and then remap the
c896fb
image
c896fb
    and unblacklist the host. multipathd will then handle this like a
c896fb
    path being removed and re-added.
c896fb
c896fb
--------
c896fb
c896fb
Porting notes:
c896fb
Added rbd to multipath.conf.annotated.
c896fb
c896fb
Signed-off-by: Mike Christie <mchristi@redhat.com>
c896fb
---
c896fb
 libmultipath/checkers/Makefile |    7 
c896fb
 libmultipath/checkers/rbd.c    |  639 +++++++++++++++++++++++++++++++++++++++++
c896fb
 multipath.conf.annotated       |    4 
c896fb
 multipath/multipath.conf.5     |    3 
c896fb
 4 files changed, 651 insertions(+), 2 deletions(-)
c896fb
 create mode 100644 libmultipath/checkers/rbd.c
c896fb
c896fb
Index: multipath-tools-130222/libmultipath/checkers/Makefile
c896fb
===================================================================
c896fb
--- multipath-tools-130222.orig/libmultipath/checkers/Makefile
c896fb
+++ multipath-tools-130222/libmultipath/checkers/Makefile
c896fb
@@ -14,10 +14,17 @@ LIBS= \
c896fb
 	libcheckhp_sw.so \
c896fb
 	libcheckrdac.so
c896fb
 
c896fb
+ifeq ($(shell test -r /usr/include/rados/librados.h && echo 1),1)
c896fb
+LIBS += libcheckrbd.so
c896fb
+endif
c896fb
+
c896fb
 CFLAGS += -fPIC -I..
c896fb
 
c896fb
 all: $(LIBS)
c896fb
 
c896fb
+libcheckrbd.so: rbd.o
c896fb
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lrados -ludev
c896fb
+
c896fb
 libcheckdirectio.so: libsg.o directio.o
c896fb
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -laio
c896fb
 
c896fb
Index: multipath-tools-130222/libmultipath/checkers/rbd.c
c896fb
===================================================================
c896fb
--- /dev/null
c896fb
+++ multipath-tools-130222/libmultipath/checkers/rbd.c
c896fb
@@ -0,0 +1,639 @@
c896fb
+/*
c896fb
+ * Copyright (c) 2016 Red Hat
c896fb
+ * Copyright (c) 2004 Christophe Varoqui
c896fb
+ *
c896fb
+ * Code based off of tur.c and ceph's krbd.cc
c896fb
+ */
c896fb
+#define _GNU_SOURCE
c896fb
+#include <stdio.h>
c896fb
+#include <stdlib.h>
c896fb
+#include <string.h>
c896fb
+#include <unistd.h>
c896fb
+#include <fcntl.h>
c896fb
+#include <errno.h>
c896fb
+#include <pthread.h>
c896fb
+#include <libudev.h>
c896fb
+#include <ifaddrs.h>
c896fb
+#include <sys/types.h>
c896fb
+#include <sys/stat.h>
c896fb
+#include <sys/ioctl.h>
c896fb
+#include <sys/time.h>
c896fb
+#include <sys/wait.h>
c896fb
+
c896fb
+#include "rados/librados.h"
c896fb
+
c896fb
+#include "structs.h"
c896fb
+#include "checkers.h"
c896fb
+
c896fb
+#include "../libmultipath/debug.h"
c896fb
+#include "../libmultipath/uevent.h"
c896fb
+
c896fb
+struct rbd_checker_context;
c896fb
+typedef int (thread_fn)(struct rbd_checker_context *ct, char *msg);
c896fb
+
c896fb
+#define RBD_MSG(msg, fmt, args...) snprintf(msg, CHECKER_MSG_LEN, fmt, ##args);
c896fb
+
c896fb
+struct rbd_checker_context {
c896fb
+	int rbd_bus_id;
c896fb
+	char *client_addr;
c896fb
+	char *config_info;
c896fb
+	char *snap;
c896fb
+	char *pool;
c896fb
+	char *image;
c896fb
+	char *username;
c896fb
+	int remapped;
c896fb
+	int blacklisted;
c896fb
+
c896fb
+	rados_t cluster;
c896fb
+
c896fb
+	int state;
c896fb
+	int running;
c896fb
+	time_t time;
c896fb
+	thread_fn *fn;
c896fb
+	pthread_t thread;
c896fb
+	pthread_mutex_t lock;
c896fb
+	pthread_cond_t active;
c896fb
+	pthread_spinlock_t hldr_lock;
c896fb
+	int holders;
c896fb
+	char message[CHECKER_MSG_LEN];
c896fb
+};
c896fb
+
c896fb
+int libcheck_init(struct checker * c)
c896fb
+{
c896fb
+	struct rbd_checker_context *ct;
c896fb
+	struct udev_device *block_dev;
c896fb
+	struct udev_device *bus_dev;
c896fb
+	struct udev *udev;
c896fb
+	struct stat sb;
c896fb
+	const char *block_name, *addr, *config_info;
c896fb
+	const char *image, *pool, *snap, *username;
c896fb
+	char sysfs_path[PATH_SIZE];
c896fb
+	int ret;
c896fb
+
c896fb
+	ct = malloc(sizeof(struct rbd_checker_context));
c896fb
+	if (!ct)
c896fb
+		return 1;
c896fb
+	memset(ct, 0, sizeof(struct rbd_checker_context));
c896fb
+	ct->holders = 1;
c896fb
+	pthread_cond_init(&ct->active, NULL);
c896fb
+	pthread_mutex_init(&ct->lock, NULL);
c896fb
+	pthread_spin_init(&ct->hldr_lock, PTHREAD_PROCESS_PRIVATE);
c896fb
+	c->context = ct;
c896fb
+
c896fb
+	/*
c896fb
+	 * The rbd block layer sysfs device is not linked to the rbd bus
c896fb
+	 * device that we interact with, so figure that out now.
c896fb
+	 */
c896fb
+	if (fstat(c->fd, &sb) != 0)
c896fb
+		goto free_ct;
c896fb
+
c896fb
+	udev = udev_new();
c896fb
+	if (!udev)
c896fb
+		goto free_ct;
c896fb
+
c896fb
+	block_dev = udev_device_new_from_devnum(udev, 'b', sb.st_rdev);
c896fb
+	if (!block_dev)
c896fb
+		goto free_udev;
c896fb
+
c896fb
+	block_name  = udev_device_get_sysname(block_dev);
c896fb
+	ret = sscanf(block_name, "rbd%d", &ct->rbd_bus_id);
c896fb
+
c896fb
+	udev_device_unref(block_dev);
c896fb
+	if (ret != 1)
c896fb
+		goto free_udev;
c896fb
+
c896fb
+	snprintf(sysfs_path, sizeof(sysfs_path), "/sys/bus/rbd/devices/%d",
c896fb
+		 ct->rbd_bus_id);
c896fb
+	bus_dev = udev_device_new_from_syspath(udev, sysfs_path);
c896fb
+	if (!bus_dev)
c896fb
+		goto free_udev;
c896fb
+
c896fb
+	addr = udev_device_get_sysattr_value(bus_dev, "client_addr");
c896fb
+	if (!addr) {
c896fb
+		condlog(0, "Could not find client_addr in rbd sysfs. Try "
c896fb
+			"updating kernel");
c896fb
+		goto free_dev;
c896fb
+	}
c896fb
+
c896fb
+	ct->client_addr = strdup(addr);
c896fb
+	if (!ct->client_addr)
c896fb
+		goto free_dev;
c896fb
+
c896fb
+	config_info = udev_device_get_sysattr_value(bus_dev, "config_info");
c896fb
+	if (!config_info)
c896fb
+		goto free_addr;
c896fb
+
c896fb
+	ct->config_info = strdup(config_info);
c896fb
+	if (!ct->config_info)
c896fb
+		goto free_addr;
c896fb
+
c896fb
+	username = strstr(config_info, "name=");
c896fb
+	if (username) {
c896fb
+		char *end;
c896fb
+		int len;
c896fb
+
c896fb
+		username += 5;
c896fb
+		end = strchr(username, ',');
c896fb
+		if (!end)
c896fb
+			goto free_info;
c896fb
+		len = end - username;
c896fb
+
c896fb
+		ct->username = malloc(len + 1);
c896fb
+		if (!ct->username)
c896fb
+			goto free_info;
c896fb
+		strncpy(ct->username, username, len);
c896fb
+		ct->username[len] = '\0';
c896fb
+	}
c896fb
+
c896fb
+	image = udev_device_get_sysattr_value(bus_dev, "name");
c896fb
+	if (!image)
c896fb
+		goto free_username;
c896fb
+
c896fb
+	ct->image = strdup(image);
c896fb
+	if (!ct->image)
c896fb
+		goto free_info;
c896fb
+
c896fb
+	pool = udev_device_get_sysattr_value(bus_dev, "pool");
c896fb
+	if (!pool)
c896fb
+		goto free_image;
c896fb
+
c896fb
+	ct->pool = strdup(pool);
c896fb
+	if (!ct->pool)
c896fb
+		goto free_image;
c896fb
+
c896fb
+	snap = udev_device_get_sysattr_value(bus_dev, "current_snap");
c896fb
+	if (!snap)
c896fb
+		goto free_pool;
c896fb
+
c896fb
+	if (strcmp("-", snap)) {
c896fb
+		ct->snap = strdup(snap);
c896fb
+		if (!ct->snap)
c896fb
+			goto free_pool;
c896fb
+	}
c896fb
+
c896fb
+	if (rados_create(&ct->cluster, NULL) < 0) {
c896fb
+		condlog(0, "Could not create rados cluster");
c896fb
+		goto free_snap;
c896fb
+	}
c896fb
+
c896fb
+	if (rados_conf_read_file(ct->cluster, NULL) < 0) {
c896fb
+		condlog(0, "Could not read rados conf");
c896fb
+		goto shutdown_rados;
c896fb
+	}
c896fb
+
c896fb
+	ret = rados_connect(ct->cluster);
c896fb
+	if (ret < 0) {
c896fb
+		condlog(0, "Could not connect to rados cluster");
c896fb
+		goto shutdown_rados;
c896fb
+	}
c896fb
+
c896fb
+	udev_device_unref(bus_dev);
c896fb
+	udev_unref(udev);
c896fb
+
c896fb
+	condlog(3, "rbd%d checker init %s %s/%s@%s %s", ct->rbd_bus_id,
c896fb
+		ct->client_addr, ct->pool, ct->image, ct->snap ? ct->snap : "-",
c896fb
+		ct->username ? ct->username : "none");
c896fb
+	return 0;
c896fb
+
c896fb
+shutdown_rados:
c896fb
+	rados_shutdown(ct->cluster);
c896fb
+free_snap:
c896fb
+	if (ct->snap)
c896fb
+		free(ct->snap);
c896fb
+free_pool:
c896fb
+	free(ct->pool);
c896fb
+free_image:
c896fb
+	free(ct->image);
c896fb
+free_username:
c896fb
+	if (ct->username)
c896fb
+		free(ct->username);
c896fb
+free_info:
c896fb
+	free(ct->config_info);
c896fb
+free_addr:
c896fb
+	free(ct->client_addr);
c896fb
+free_dev:
c896fb
+	udev_device_unref(bus_dev);
c896fb
+free_udev:
c896fb
+	udev_unref(udev);
c896fb
+free_ct:
c896fb
+	free(ct);
c896fb
+	return 1;
c896fb
+}
c896fb
+
c896fb
+void cleanup_context(struct rbd_checker_context *ct)
c896fb
+{
c896fb
+	pthread_mutex_destroy(&ct->lock);
c896fb
+	pthread_cond_destroy(&ct->active);
c896fb
+	pthread_spin_destroy(&ct->hldr_lock);
c896fb
+
c896fb
+	rados_shutdown(ct->cluster);
c896fb
+
c896fb
+	if (ct->username)
c896fb
+		free(ct->username);
c896fb
+	if (ct->snap)
c896fb
+		free(ct->snap);
c896fb
+	free(ct->pool);
c896fb
+	free(ct->image);
c896fb
+	free(ct->config_info);
c896fb
+	free(ct->client_addr);
c896fb
+	free(ct);
c896fb
+}
c896fb
+
c896fb
+void libcheck_free(struct checker * c)
c896fb
+{
c896fb
+	if (c->context) {
c896fb
+		struct rbd_checker_context *ct = c->context;
c896fb
+		int holders;
c896fb
+		pthread_t thread;
c896fb
+
c896fb
+		pthread_spin_lock(&ct->hldr_lock);
c896fb
+		ct->holders--;
c896fb
+		holders = ct->holders;
c896fb
+		thread = ct->thread;
c896fb
+		pthread_spin_unlock(&ct->hldr_lock);
c896fb
+		if (holders)
c896fb
+			pthread_cancel(thread);
c896fb
+		else
c896fb
+			cleanup_context(ct);
c896fb
+		c->context = NULL;
c896fb
+	}
c896fb
+}
c896fb
+
c896fb
+static int rbd_is_blacklisted(struct rbd_checker_context *ct, char *msg)
c896fb
+{
c896fb
+	char *addr_tok, *start, *save;
c896fb
+	char *cmd[2];
c896fb
+	char *blklist, *stat;
c896fb
+	size_t blklist_len, stat_len;
c896fb
+	int ret;
c896fb
+	char *end;
c896fb
+
c896fb
+	cmd[0] = "{\"prefix\": \"osd blacklist ls\"}";
c896fb
+	cmd[1] = NULL;
c896fb
+
c896fb
+	ret = rados_mon_command(ct->cluster, (const char **)cmd, 1, "", 0,
c896fb
+				&blklist, &blklist_len, &stat, &stat_len);
c896fb
+	if (ret < 0) {
c896fb
+		RBD_MSG(msg, "rbd checker failed: mon command failed %d",
c896fb
+			ret);
c896fb
+		return ret;
c896fb
+	}
c896fb
+
c896fb
+	if (!blklist || !blklist_len)
c896fb
+		goto free_bufs;
c896fb
+
c896fb
+	/*
c896fb
+	 * parse list of addrs with the format
c896fb
+	 * ipv4:port/nonce date time\n
c896fb
+	 * or
c896fb
+	 * [ipv6]:port/nonce date time\n
c896fb
+	 */
c896fb
+	ret = 0;
c896fb
+	for (start = blklist; ; start = NULL) {
c896fb
+		addr_tok = strtok_r(start, "\n", &save);
c896fb
+		if (!addr_tok || !strlen(addr_tok))
c896fb
+			break;
c896fb
+
c896fb
+		end = strchr(addr_tok, ' ');
c896fb
+		if (!end) {
c896fb
+			RBD_MSG(msg, "rbd%d checker failed: invalid blacklist %s",
c896fb
+				 ct->rbd_bus_id, addr_tok);
c896fb
+			break;
c896fb
+		}
c896fb
+		*end = '\0';
c896fb
+
c896fb
+		if (!strcmp(addr_tok, ct->client_addr)) {
c896fb
+			ct->blacklisted = 1;
c896fb
+			RBD_MSG(msg, "rbd%d checker: %s is blacklisted",
c896fb
+				ct->rbd_bus_id, ct->client_addr);
c896fb
+			ret = 1;
c896fb
+			break;
c896fb
+		}
c896fb
+	}
c896fb
+
c896fb
+free_bufs:
c896fb
+	rados_buffer_free(blklist);
c896fb
+	rados_buffer_free(stat);
c896fb
+	return ret;
c896fb
+}
c896fb
+
c896fb
+int rbd_check(struct rbd_checker_context *ct, char *msg)
c896fb
+{
c896fb
+	if (ct->blacklisted || rbd_is_blacklisted(ct, msg) == 1)
c896fb
+		return PATH_DOWN;
c896fb
+
c896fb
+	RBD_MSG(msg, "rbd checker reports path is up");
c896fb
+	/*
c896fb
+	 * Path may have issues, but the ceph cluster is at least
c896fb
+	 * accepting IO, so we can attempt to do IO.
c896fb
+	 *
c896fb
+	 * TODO: in future versions, we can run other tests to
c896fb
+	 * verify OSDs and networks.
c896fb
+	 */
c896fb
+	return PATH_UP;
c896fb
+}
c896fb
+
c896fb
+int safe_write(int fd, const void *buf, size_t count)
c896fb
+{
c896fb
+	while (count > 0) {
c896fb
+		ssize_t r = write(fd, buf, count);
c896fb
+		if (r < 0) {
c896fb
+			if (errno == EINTR)
c896fb
+				continue;
c896fb
+			return -errno;
c896fb
+		}
c896fb
+		count -= r;
c896fb
+		buf = (char *)buf + r;
c896fb
+	}
c896fb
+	return 0;
c896fb
+}
c896fb
+
c896fb
+static int sysfs_write_rbd_bus(const char *which, const char *buf,
c896fb
+			       size_t buf_len)
c896fb
+{
c896fb
+	char sysfs_path[PATH_SIZE];
c896fb
+	int fd;
c896fb
+	int r;
c896fb
+
c896fb
+	/* we require newer kernels so single_major should alwayws be there */
c896fb
+	snprintf(sysfs_path, sizeof(sysfs_path),
c896fb
+		 "/sys/bus/rbd/%s_single_major", which);
c896fb
+	fd = open(sysfs_path, O_WRONLY);
c896fb
+	if (fd < 0)
c896fb
+		return -errno;
c896fb
+
c896fb
+	r = safe_write(fd, buf, buf_len);
c896fb
+	close(fd);
c896fb
+	return r;
c896fb
+}
c896fb
+
c896fb
+static int rbd_remap(struct rbd_checker_context *ct)
c896fb
+{
c896fb
+	char *argv[11];
c896fb
+	pid_t pid;
c896fb
+	int ret = 0, i = 0;
c896fb
+	int status;
c896fb
+
c896fb
+	pid = fork();
c896fb
+	switch (pid) {
c896fb
+	case 0:
c896fb
+		argv[i++] = "rbd";
c896fb
+		argv[i++] = "map";
c896fb
+		argv[i++] = "-o noshare";
c896fb
+		if (ct->username) {
c896fb
+			argv[i++] = "--id";
c896fb
+			argv[i++] = ct->username;
c896fb
+		}
c896fb
+		argv[i++] = "--pool";
c896fb
+		argv[i++] = ct->pool;
c896fb
+		if (ct->snap) {
c896fb
+			argv[i++] = "--snap";
c896fb
+			argv[i++] = ct->snap;
c896fb
+		}
c896fb
+		argv[i++] = ct->image;
c896fb
+		argv[i] = NULL;
c896fb
+
c896fb
+		ret = execvp(argv[0], argv);
c896fb
+		condlog(0, "Error executing rbd: %s", strerror(errno));
c896fb
+		exit(-1);
c896fb
+	case -1:
c896fb
+		condlog(0, "fork failed: %s", strerror(errno));
c896fb
+		return -1;
c896fb
+	default:
c896fb
+		ret = -1;
c896fb
+		wait(&status);
c896fb
+		if (WIFEXITED(status)) {
c896fb
+			status = WEXITSTATUS(status);
c896fb
+			if (status == 0)
c896fb
+				ret = 0;
c896fb
+			else
c896fb
+				condlog(0, "rbd failed with %d", status);
c896fb
+		}
c896fb
+	}
c896fb
+
c896fb
+	return ret;
c896fb
+}
c896fb
+
c896fb
+static int sysfs_write_rbd_remove(const char *buf, int buf_len)
c896fb
+{
c896fb
+	return sysfs_write_rbd_bus("remove", buf, buf_len);
c896fb
+}
c896fb
+
c896fb
+static int rbd_rm_blacklist(struct rbd_checker_context *ct)
c896fb
+{
c896fb
+	char *cmd[2];
c896fb
+	char *stat, *cmd_str;
c896fb
+	size_t stat_len;
c896fb
+	int ret;
c896fb
+
c896fb
+	ret = asprintf(&cmd_str, "{\"prefix\": \"osd blacklist\", \"blacklistop\": \"rm\", \"addr\": \"%s\"}",
c896fb
+		       ct->client_addr);
c896fb
+	if (ret == -1)
c896fb
+		return -ENOMEM;
c896fb
+
c896fb
+	cmd[0] = cmd_str;
c896fb
+	cmd[1] = NULL;
c896fb
+
c896fb
+	ret = rados_mon_command(ct->cluster, (const char **)cmd, 1, "", 0,
c896fb
+				NULL, 0, &stat, &stat_len);
c896fb
+	if (ret < 0) {
c896fb
+		condlog(1, "rbd%d repair failed to remove blacklist for %s %d",
c896fb
+			ct->rbd_bus_id, ct->client_addr, ret);
c896fb
+		goto free_cmd;
c896fb
+	}
c896fb
+
c896fb
+	condlog(1, "rbd%d repair rm blacklist for %s",
c896fb
+	       ct->rbd_bus_id, ct->client_addr);
c896fb
+	free(stat);
c896fb
+free_cmd:
c896fb
+	free(cmd_str);
c896fb
+	return ret;
c896fb
+}
c896fb
+
c896fb
+static int rbd_repair(struct rbd_checker_context *ct, char *msg)
c896fb
+{
c896fb
+	char del[17];
c896fb
+	int ret;
c896fb
+
c896fb
+	if (!ct->blacklisted)
c896fb
+		return PATH_UP;
c896fb
+
c896fb
+	if (!ct->remapped) {
c896fb
+		ret = rbd_remap(ct);
c896fb
+		if (ret) {
c896fb
+			RBD_MSG(msg, "rbd%d repair failed to remap. Err %d",
c896fb
+				ct->rbd_bus_id, ret);
c896fb
+			return PATH_DOWN;
c896fb
+		}
c896fb
+	}
c896fb
+	ct->remapped = 1;
c896fb
+
c896fb
+	snprintf(del, sizeof(del), "%d force", ct->rbd_bus_id);
c896fb
+	ret = sysfs_write_rbd_remove(del, strlen(del) + 1);
c896fb
+	if (ret) {
c896fb
+		RBD_MSG(msg, "rbd%d repair failed to clean up. Err %d",
c896fb
+			ct->rbd_bus_id, ret);
c896fb
+		return PATH_DOWN;
c896fb
+	}
c896fb
+
c896fb
+	ret = rbd_rm_blacklist(ct);
c896fb
+	if (ret) {
c896fb
+		RBD_MSG(msg, "rbd%d repair could not remove blacklist entry. Err %d",
c896fb
+			ct->rbd_bus_id, ret);
c896fb
+		return PATH_DOWN;
c896fb
+	}
c896fb
+
c896fb
+	ct->remapped = 0;
c896fb
+	ct->blacklisted = 0;
c896fb
+
c896fb
+	RBD_MSG(msg, "rbd%d has been repaired", ct->rbd_bus_id);
c896fb
+	return PATH_UP;
c896fb
+}
c896fb
+
c896fb
+#define rbd_thread_cleanup_push(ct) pthread_cleanup_push(cleanup_func, ct)
c896fb
+#define rbd_thread_cleanup_pop(ct) pthread_cleanup_pop(1)
c896fb
+
c896fb
+void cleanup_func(void *data)
c896fb
+{
c896fb
+	int holders;
c896fb
+	struct rbd_checker_context *ct = data;
c896fb
+	pthread_spin_lock(&ct->hldr_lock);
c896fb
+	ct->holders--;
c896fb
+	holders = ct->holders;
c896fb
+	ct->thread = 0;
c896fb
+	pthread_spin_unlock(&ct->hldr_lock);
c896fb
+	if (!holders)
c896fb
+		cleanup_context(ct);
c896fb
+}
c896fb
+
c896fb
+void *rbd_thread(void *ctx)
c896fb
+{
c896fb
+	struct rbd_checker_context *ct = ctx;
c896fb
+	int state;
c896fb
+
c896fb
+	condlog(3, "rbd%d thread starting up", ct->rbd_bus_id);
c896fb
+
c896fb
+	ct->message[0] = '\0';
c896fb
+	/* This thread can be canceled, so setup clean up */
c896fb
+	rbd_thread_cleanup_push(ct)
c896fb
+
c896fb
+	/* checker start up */
c896fb
+	pthread_mutex_lock(&ct->lock);
c896fb
+	ct->state = PATH_PENDING;
c896fb
+	pthread_mutex_unlock(&ct->lock);
c896fb
+
c896fb
+	state = ct->fn(ct, ct->message);
c896fb
+
c896fb
+	/* checker done */
c896fb
+	pthread_mutex_lock(&ct->lock);
c896fb
+	ct->state = state;
c896fb
+	pthread_mutex_unlock(&ct->lock);
c896fb
+	pthread_cond_signal(&ct->active);
c896fb
+
c896fb
+	condlog(3, "rbd%d thead finished, state %s", ct->rbd_bus_id,
c896fb
+		checker_state_name(state));
c896fb
+	rbd_thread_cleanup_pop(ct);
c896fb
+	return ((void *)0);
c896fb
+}
c896fb
+
c896fb
+static void rbd_timeout(struct timespec *tsp)
c896fb
+{
c896fb
+	struct timeval now;
c896fb
+
c896fb
+	gettimeofday(&now, NULL);
c896fb
+	tsp->tv_sec = now.tv_sec;
c896fb
+	tsp->tv_nsec = now.tv_usec * 1000;
c896fb
+	tsp->tv_nsec += 1000000; /* 1 millisecond */
c896fb
+}
c896fb
+
c896fb
+static int rbd_exec_fn(struct checker *c, thread_fn *fn)
c896fb
+{
c896fb
+	struct rbd_checker_context *ct = c->context;
c896fb
+	struct timespec tsp;
c896fb
+	pthread_attr_t attr;
c896fb
+	int rbd_status, r;
c896fb
+
c896fb
+	if (c->sync)
c896fb
+		return rbd_check(ct, c->message);
c896fb
+	/*
c896fb
+	 * Async mode
c896fb
+	 */
c896fb
+	r = pthread_mutex_lock(&ct->lock);
c896fb
+	if (r != 0) {
c896fb
+		condlog(2, "rbd%d mutex lock failed with %d", ct->rbd_bus_id,
c896fb
+			r);
c896fb
+		MSG(c, "rbd%d thread failed to initialize", ct->rbd_bus_id);
c896fb
+		return PATH_WILD;
c896fb
+	}
c896fb
+
c896fb
+	if (ct->running) {
c896fb
+		/* Check if checker is still running */
c896fb
+		if (ct->thread) {
c896fb
+			condlog(3, "rbd%d thread not finished", ct->rbd_bus_id);
c896fb
+			rbd_status = PATH_PENDING;
c896fb
+		} else {
c896fb
+			/* checker done */
c896fb
+			ct->running = 0;
c896fb
+			rbd_status = ct->state;
c896fb
+			strncpy(c->message, ct->message, CHECKER_MSG_LEN);
c896fb
+			c->message[CHECKER_MSG_LEN - 1] = '\0';
c896fb
+		}
c896fb
+		pthread_mutex_unlock(&ct->lock);
c896fb
+	} else {
c896fb
+		/* Start new checker */
c896fb
+		ct->state = PATH_UNCHECKED;
c896fb
+		ct->fn = fn;
c896fb
+		pthread_spin_lock(&ct->hldr_lock);
c896fb
+		ct->holders++;
c896fb
+		pthread_spin_unlock(&ct->hldr_lock);
c896fb
+		setup_thread_attr(&attr, 32 * 1024, 1);
c896fb
+		r = pthread_create(&ct->thread, &attr, rbd_thread, ct);
c896fb
+		if (r) {
c896fb
+			pthread_mutex_unlock(&ct->lock);
c896fb
+			ct->thread = 0;
c896fb
+			ct->holders--;
c896fb
+			condlog(3, "rbd%d failed to start rbd thread, using sync mode",
c896fb
+				ct->rbd_bus_id);
c896fb
+			return fn(ct, c->message);
c896fb
+		}
c896fb
+		pthread_attr_destroy(&attr);
c896fb
+		rbd_timeout(&tsp;;
c896fb
+		r = pthread_cond_timedwait(&ct->active, &ct->lock, &tsp;;
c896fb
+		rbd_status = ct->state;
c896fb
+		strncpy(c->message, ct->message,CHECKER_MSG_LEN);
c896fb
+		c->message[CHECKER_MSG_LEN -1] = '\0';
c896fb
+		pthread_mutex_unlock(&ct->lock);
c896fb
+
c896fb
+		if (ct->thread &&
c896fb
+		    (rbd_status == PATH_PENDING || rbd_status == PATH_UNCHECKED)) {
c896fb
+			condlog(3, "rbd%d thread still running",
c896fb
+				ct->rbd_bus_id);
c896fb
+			ct->running = 1;
c896fb
+			rbd_status = PATH_PENDING;
c896fb
+		}
c896fb
+	}
c896fb
+
c896fb
+	return rbd_status;
c896fb
+}
c896fb
+
c896fb
+void libcheck_repair(struct checker * c)
c896fb
+{
c896fb
+	struct rbd_checker_context *ct = c->context;
c896fb
+
c896fb
+	if (!ct || !ct->blacklisted)
c896fb
+		return;
c896fb
+	rbd_exec_fn(c, rbd_repair);
c896fb
+}
c896fb
+
c896fb
+int libcheck_check(struct checker * c)
c896fb
+{
c896fb
+	struct rbd_checker_context *ct = c->context;
c896fb
+
c896fb
+	if (!ct)
c896fb
+		return PATH_UNCHECKED;
c896fb
+
c896fb
+	if (ct->blacklisted)
c896fb
+		return PATH_DOWN;
c896fb
+
c896fb
+	return rbd_exec_fn(c, rbd_check);
c896fb
+}
c896fb
Index: multipath-tools-130222/multipath.conf.annotated
c896fb
===================================================================
c896fb
--- multipath-tools-130222.orig/multipath.conf.annotated
c896fb
+++ multipath-tools-130222/multipath.conf.annotated
c896fb
@@ -97,7 +97,7 @@
c896fb
 #	# scope   : multipath & multipathd
c896fb
 #	# desc    : the default method used to determine the paths' state
c896fb
 #	# values  : readsector0|tur|emc_clariion|hp_sw|directio|rdac|
c896fb
-#	            cciss_tur|hp_tur
c896fb
+#	            cciss_tur|hp_tur|rbd
c896fb
 #	# default : directio
c896fb
 #	#
c896fb
 #	path_checker	directio
c896fb
@@ -493,7 +493,7 @@
c896fb
 #		# scope   : multipathd & multipathd
c896fb
 #		# desc    : path checking algorithm to use to check path state
c896fb
 #		# values  : readsector0|tur|emc_clariion|hp_sw|directio|rdac|
c896fb
-#		#           cciss_tur|hp_tur
c896fb
+#		#           cciss_tur|hp_tur|rbd
c896fb
 #		#
c896fb
 #		path_checker		directio
c896fb
 #
c896fb
Index: multipath-tools-130222/multipath/multipath.conf.5
c896fb
===================================================================
c896fb
--- multipath-tools-130222.orig/multipath/multipath.conf.5
c896fb
+++ multipath-tools-130222/multipath/multipath.conf.5
c896fb
@@ -284,6 +284,9 @@ Check the path state for LSI/Engenio/Net
c896fb
 .B directio
c896fb
 Read the first sector with direct I/O.
c896fb
 .TP
c896fb
+.B rbd
c896fb
+Check if the path is in the Ceph blacklist.
c896fb
+.TP
c896fb
 Default value is \fIdirectio\fR.
c896fb
 .RE
c896fb
 .TP