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

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