md-cluster.c 37.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2015, SUSE
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 */


#include <linux/module.h>
13
#include <linux/kthread.h>
14 15
#include <linux/dlm.h>
#include <linux/sched.h>
16
#include <linux/raid/md_p.h>
17
#include "md.h"
18
#include "bitmap.h"
19
#include "md-cluster.h"
20 21

#define LVB_SIZE	64
22
#define NEW_DEV_TIMEOUT 5000
23 24 25 26 27 28

struct dlm_lock_resource {
	dlm_lockspace_t *ls;
	struct dlm_lksb lksb;
	char *name; /* lock name. */
	uint32_t flags; /* flags to pass to dlm_lock() */
29 30
	wait_queue_head_t sync_locking; /* wait queue for synchronized locking */
	bool sync_locking_done;
G
Goldwyn Rodrigues 已提交
31 32
	void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
	struct mddev *mddev; /* pointing back to mddev. */
33
	int mode;
G
Goldwyn Rodrigues 已提交
34 35
};

36 37 38 39 40 41 42 43 44 45 46 47
struct suspend_info {
	int slot;
	sector_t lo;
	sector_t hi;
	struct list_head list;
};

struct resync_info {
	__le64 lo;
	__le64 hi;
};

48 49
/* md_cluster_info flags */
#define		MD_CLUSTER_WAITING_FOR_NEWDISK		1
50
#define		MD_CLUSTER_SUSPEND_READ_BALANCING	2
51
#define		MD_CLUSTER_BEGIN_JOIN_CLUSTER		3
52

53 54 55 56 57
/* Lock the send communication. This is done through
 * bit manipulation as opposed to a mutex in order to
 * accomodate lock and hold. See next comment.
 */
#define		MD_CLUSTER_SEND_LOCK			4
58 59 60 61 62 63
/* If cluster operations (such as adding a disk) must lock the
 * communication channel, so as to perform extra operations
 * (update metadata) and no other operation is allowed on the
 * MD. Token needs to be locked and held until the operation
 * completes witha md_update_sb(), which would eventually release
 * the lock.
64 65
 */
#define		MD_CLUSTER_SEND_LOCKED_ALREADY		5
66 67 68 69
/* We should receive message after node joined cluster and
 * set up all the related infos such as bitmap and personality */
#define		MD_CLUSTER_ALREADY_IN_CLUSTER		6
#define		MD_CLUSTER_PENDING_RECV_EVENT		7
70
#define 	MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD		8
71

G
Goldwyn Rodrigues 已提交
72
struct md_cluster_info {
73
	struct mddev *mddev; /* the md device which md_cluster_info belongs to */
G
Goldwyn Rodrigues 已提交
74 75
	/* dlm lock space and resources for clustered raid. */
	dlm_lockspace_t *lockspace;
G
Goldwyn Rodrigues 已提交
76 77
	int slot_number;
	struct completion completion;
78
	struct mutex recv_mutex;
79
	struct dlm_lock_resource *bitmap_lockres;
80
	struct dlm_lock_resource **other_bitmap_lockres;
81
	struct dlm_lock_resource *resync_lockres;
82 83
	struct list_head suspend_list;
	spinlock_t suspend_lock;
84 85
	struct md_thread *recovery_thread;
	unsigned long recovery_map;
86 87 88 89
	/* communication loc resources */
	struct dlm_lock_resource *ack_lockres;
	struct dlm_lock_resource *message_lockres;
	struct dlm_lock_resource *token_lockres;
90
	struct dlm_lock_resource *no_new_dev_lockres;
91
	struct md_thread *recv_thread;
92
	struct completion newdisk_completion;
93
	wait_queue_head_t wait;
94
	unsigned long state;
95 96 97
	/* record the region in RESYNCING message */
	sector_t sync_low;
	sector_t sync_hi;
98 99 100 101 102
};

enum msg_type {
	METADATA_UPDATED = 0,
	RESYNCING,
103
	NEWDISK,
104
	REMOVE,
105
	RE_ADD,
106
	BITMAP_NEEDS_SYNC,
107 108 109
};

struct cluster_msg {
110 111
	__le32 type;
	__le32 slot;
112
	/* TODO: Unionize this for smaller footprint */
113 114
	__le64 low;
	__le64 high;
115
	char uuid[16];
116
	__le32 raid_slot;
117 118 119 120 121 122
};

static void sync_ast(void *arg)
{
	struct dlm_lock_resource *res;

123
	res = arg;
124 125
	res->sync_locking_done = true;
	wake_up(&res->sync_locking);
126 127 128 129 130 131 132 133 134 135 136
}

static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
{
	int ret = 0;

	ret = dlm_lock(res->ls, mode, &res->lksb,
			res->flags, res->name, strlen(res->name),
			0, sync_ast, res, res->bast);
	if (ret)
		return ret;
137 138
	wait_event(res->sync_locking, res->sync_locking_done);
	res->sync_locking_done = false;
139 140
	if (res->lksb.sb_status == 0)
		res->mode = mode;
141 142 143 144 145 146 147 148
	return res->lksb.sb_status;
}

static int dlm_unlock_sync(struct dlm_lock_resource *res)
{
	return dlm_lock_sync(res, DLM_LOCK_NL);
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
 * An variation of dlm_lock_sync, which make lock request could
 * be interrupted
 */
static int dlm_lock_sync_interruptible(struct dlm_lock_resource *res, int mode,
				       struct mddev *mddev)
{
	int ret = 0;

	ret = dlm_lock(res->ls, mode, &res->lksb,
			res->flags, res->name, strlen(res->name),
			0, sync_ast, res, res->bast);
	if (ret)
		return ret;

	wait_event(res->sync_locking, res->sync_locking_done
165 166
				      || kthread_should_stop()
				      || test_bit(MD_CLOSING, &mddev->flags));
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	if (!res->sync_locking_done) {
		/*
		 * the convert queue contains the lock request when request is
		 * interrupted, and sync_ast could still be run, so need to
		 * cancel the request and reset completion
		 */
		ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_CANCEL,
			&res->lksb, res);
		res->sync_locking_done = false;
		if (unlikely(ret != 0))
			pr_info("failed to cancel previous lock request "
				 "%s return %d\n", res->name, ret);
		return -EPERM;
	} else
		res->sync_locking_done = false;
	if (res->lksb.sb_status == 0)
		res->mode = mode;
	return res->lksb.sb_status;
}

G
Goldwyn Rodrigues 已提交
187
static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
188 189 190 191
		char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
{
	struct dlm_lock_resource *res = NULL;
	int ret, namelen;
G
Goldwyn Rodrigues 已提交
192
	struct md_cluster_info *cinfo = mddev->cluster_info;
193 194 195 196

	res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
	if (!res)
		return NULL;
197 198
	init_waitqueue_head(&res->sync_locking);
	res->sync_locking_done = false;
G
Goldwyn Rodrigues 已提交
199 200
	res->ls = cinfo->lockspace;
	res->mddev = mddev;
201
	res->mode = DLM_LOCK_IV;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	namelen = strlen(name);
	res->name = kzalloc(namelen + 1, GFP_KERNEL);
	if (!res->name) {
		pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
		goto out_err;
	}
	strlcpy(res->name, name, namelen + 1);
	if (with_lvb) {
		res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
		if (!res->lksb.sb_lvbptr) {
			pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
			goto out_err;
		}
		res->flags = DLM_LKF_VALBLK;
	}

	if (bastfn)
		res->bast = bastfn;

	res->flags |= DLM_LKF_EXPEDITE;

	ret = dlm_lock_sync(res, DLM_LOCK_NL);
	if (ret) {
		pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
		goto out_err;
	}
	res->flags &= ~DLM_LKF_EXPEDITE;
	res->flags |= DLM_LKF_CONVERT;

	return res;
out_err:
	kfree(res->lksb.sb_lvbptr);
	kfree(res->name);
	kfree(res);
	return NULL;
}

static void lockres_free(struct dlm_lock_resource *res)
{
241
	int ret = 0;
242

243 244 245
	if (!res)
		return;

246 247 248 249 250 251 252 253 254
	/*
	 * use FORCEUNLOCK flag, so we can unlock even the lock is on the
	 * waiting or convert queue
	 */
	ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_FORCEUNLOCK,
		&res->lksb, res);
	if (unlikely(ret != 0))
		pr_err("failed to unlock %s return %d\n", res->name, ret);
	else
255
		wait_event(res->sync_locking, res->sync_locking_done);
256 257 258 259 260

	kfree(res->name);
	kfree(res->lksb.sb_lvbptr);
	kfree(res);
}
261

262 263
static void add_resync_info(struct dlm_lock_resource *lockres,
			    sector_t lo, sector_t hi)
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
	struct resync_info *ri;

	ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
	ri->lo = cpu_to_le64(lo);
	ri->hi = cpu_to_le64(hi);
}

static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
{
	struct resync_info ri;
	struct suspend_info *s = NULL;
	sector_t hi = 0;

	dlm_lock_sync(lockres, DLM_LOCK_CR);
	memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
	hi = le64_to_cpu(ri.hi);
281
	if (hi > 0) {
282 283 284 285 286 287 288 289 290 291 292
		s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
		if (!s)
			goto out;
		s->hi = hi;
		s->lo = le64_to_cpu(ri.lo);
	}
	dlm_unlock_sync(lockres);
out:
	return s;
}

293
static void recover_bitmaps(struct md_thread *thread)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
{
	struct mddev *mddev = thread->mddev;
	struct md_cluster_info *cinfo = mddev->cluster_info;
	struct dlm_lock_resource *bm_lockres;
	char str[64];
	int slot, ret;
	struct suspend_info *s, *tmp;
	sector_t lo, hi;

	while (cinfo->recovery_map) {
		slot = fls64((u64)cinfo->recovery_map) - 1;

		/* Clear suspend_area associated with the bitmap */
		spin_lock_irq(&cinfo->suspend_lock);
		list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
			if (slot == s->slot) {
				list_del(&s->list);
				kfree(s);
			}
		spin_unlock_irq(&cinfo->suspend_lock);

		snprintf(str, 64, "bitmap%04d", slot);
		bm_lockres = lockres_init(mddev, str, NULL, 1);
		if (!bm_lockres) {
			pr_err("md-cluster: Cannot initialize bitmaps\n");
			goto clear_bit;
		}

322
		ret = dlm_lock_sync_interruptible(bm_lockres, DLM_LOCK_PW, mddev);
323 324 325 326 327
		if (ret) {
			pr_err("md-cluster: Could not DLM lock %s: %d\n",
					str, ret);
			goto clear_bit;
		}
328
		ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
329
		if (ret) {
330
			pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
331
			goto clear_bit;
332 333 334 335
		}
		if (hi > 0) {
			if (lo < mddev->recovery_cp)
				mddev->recovery_cp = lo;
336 337 338 339 340 341
			/* wake up thread to continue resync in case resync
			 * is not finished */
			if (mddev->recovery_cp != MaxSector) {
			    set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
			    md_wakeup_thread(mddev->thread);
			}
342
		}
343
clear_bit:
S
Shaohua Li 已提交
344
		lockres_free(bm_lockres);
345 346 347 348
		clear_bit(slot, &cinfo->recovery_map);
	}
}

G
Goldwyn Rodrigues 已提交
349 350
static void recover_prep(void *arg)
{
351 352 353
	struct mddev *mddev = arg;
	struct md_cluster_info *cinfo = mddev->cluster_info;
	set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
G
Goldwyn Rodrigues 已提交
354 355
}

356
static void __recover_slot(struct mddev *mddev, int slot)
G
Goldwyn Rodrigues 已提交
357 358 359
{
	struct md_cluster_info *cinfo = mddev->cluster_info;

360
	set_bit(slot, &cinfo->recovery_map);
361 362 363 364 365 366 367 368 369
	if (!cinfo->recovery_thread) {
		cinfo->recovery_thread = md_register_thread(recover_bitmaps,
				mddev, "recover");
		if (!cinfo->recovery_thread) {
			pr_warn("md-cluster: Could not create recovery thread\n");
			return;
		}
	}
	md_wakeup_thread(cinfo->recovery_thread);
G
Goldwyn Rodrigues 已提交
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385
static void recover_slot(void *arg, struct dlm_slot *slot)
{
	struct mddev *mddev = arg;
	struct md_cluster_info *cinfo = mddev->cluster_info;

	pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
			mddev->bitmap_info.cluster_name,
			slot->nodeid, slot->slot,
			cinfo->slot_number);
	/* deduct one since dlm slot starts from one while the num of
	 * cluster-md begins with 0 */
	__recover_slot(mddev, slot->slot - 1);
}

G
Goldwyn Rodrigues 已提交
386 387 388 389 390 391 392 393
static void recover_done(void *arg, struct dlm_slot *slots,
		int num_slots, int our_slot,
		uint32_t generation)
{
	struct mddev *mddev = arg;
	struct md_cluster_info *cinfo = mddev->cluster_info;

	cinfo->slot_number = our_slot;
394 395 396 397 398 399
	/* completion is only need to be complete when node join cluster,
	 * it doesn't need to run during another node's failure */
	if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
		complete(&cinfo->completion);
		clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
	}
400
	clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
G
Goldwyn Rodrigues 已提交
401 402
}

403 404
/* the ops is called when node join the cluster, and do lock recovery
 * if node failure occurs */
G
Goldwyn Rodrigues 已提交
405 406 407 408 409 410
static const struct dlm_lockspace_ops md_ls_ops = {
	.recover_prep = recover_prep,
	.recover_slot = recover_slot,
	.recover_done = recover_done,
};

411 412 413 414 415 416 417
/*
 * The BAST function for the ack lock resource
 * This function wakes up the receive thread in
 * order to receive and process the message.
 */
static void ack_bast(void *arg, int mode)
{
418
	struct dlm_lock_resource *res = arg;
419 420
	struct md_cluster_info *cinfo = res->mddev->cluster_info;

421 422 423 424 425 426
	if (mode == DLM_LOCK_EX) {
		if (test_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state))
			md_wakeup_thread(cinfo->recv_thread);
		else
			set_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state);
	}
427 428
}

G
Goldwyn Rodrigues 已提交
429 430 431 432 433 434 435 436 437 438 439 440
static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
{
	struct suspend_info *s, *tmp;

	list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
		if (slot == s->slot) {
			list_del(&s->list);
			kfree(s);
			break;
		}
}

441
static void remove_suspend_info(struct mddev *mddev, int slot)
G
Goldwyn Rodrigues 已提交
442
{
443
	struct md_cluster_info *cinfo = mddev->cluster_info;
G
Goldwyn Rodrigues 已提交
444 445 446
	spin_lock_irq(&cinfo->suspend_lock);
	__remove_suspend_info(cinfo, slot);
	spin_unlock_irq(&cinfo->suspend_lock);
447
	mddev->pers->quiesce(mddev, 2);
G
Goldwyn Rodrigues 已提交
448 449 450
}


451
static void process_suspend_info(struct mddev *mddev,
G
Goldwyn Rodrigues 已提交
452 453
		int slot, sector_t lo, sector_t hi)
{
454
	struct md_cluster_info *cinfo = mddev->cluster_info;
G
Goldwyn Rodrigues 已提交
455 456 457
	struct suspend_info *s;

	if (!hi) {
458
		remove_suspend_info(mddev, slot);
459 460
		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
		md_wakeup_thread(mddev->thread);
G
Goldwyn Rodrigues 已提交
461 462
		return;
	}
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

	/*
	 * The bitmaps are not same for different nodes
	 * if RESYNCING is happening in one node, then
	 * the node which received the RESYNCING message
	 * probably will perform resync with the region
	 * [lo, hi] again, so we could reduce resync time
	 * a lot if we can ensure that the bitmaps among
	 * different nodes are match up well.
	 *
	 * sync_low/hi is used to record the region which
	 * arrived in the previous RESYNCING message,
	 *
	 * Call bitmap_sync_with_cluster to clear
	 * NEEDED_MASK and set RESYNC_MASK since
	 * resync thread is running in another node,
	 * so we don't need to do the resync again
	 * with the same section */
	bitmap_sync_with_cluster(mddev, cinfo->sync_low,
					cinfo->sync_hi,
					lo, hi);
	cinfo->sync_low = lo;
	cinfo->sync_hi = hi;

G
Goldwyn Rodrigues 已提交
487 488 489 490 491 492
	s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
	if (!s)
		return;
	s->slot = slot;
	s->lo = lo;
	s->hi = hi;
493 494
	mddev->pers->quiesce(mddev, 1);
	mddev->pers->quiesce(mddev, 0);
G
Goldwyn Rodrigues 已提交
495 496 497 498 499
	spin_lock_irq(&cinfo->suspend_lock);
	/* Remove existing entry (if exists) before adding */
	__remove_suspend_info(cinfo, slot);
	list_add(&s->list, &cinfo->suspend_list);
	spin_unlock_irq(&cinfo->suspend_lock);
500
	mddev->pers->quiesce(mddev, 2);
G
Goldwyn Rodrigues 已提交
501 502
}

503 504 505 506 507 508 509 510 511 512
static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
{
	char disk_uuid[64];
	struct md_cluster_info *cinfo = mddev->cluster_info;
	char event_name[] = "EVENT=ADD_DEVICE";
	char raid_slot[16];
	char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
	int len;

	len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
513
	sprintf(disk_uuid + len, "%pU", cmsg->uuid);
514
	snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
515 516
	pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
	init_completion(&cinfo->newdisk_completion);
517
	set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
518 519 520
	kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
	wait_for_completion_timeout(&cinfo->newdisk_completion,
			NEW_DEV_TIMEOUT);
521
	clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
522 523 524 525 526
}


static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
{
527
	int got_lock = 0;
528
	struct md_cluster_info *cinfo = mddev->cluster_info;
529
	mddev->good_device_nr = le32_to_cpu(msg->raid_slot);
530

531
	dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
532 533 534 535 536 537
	wait_event(mddev->thread->wqueue,
		   (got_lock = mddev_trylock(mddev)) ||
		    test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state));
	md_reload_sb(mddev, mddev->good_device_nr);
	if (got_lock)
		mddev_unlock(mddev);
538 539
}

540 541
static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
{
542
	struct md_rdev *rdev;
543

544 545
	rcu_read_lock();
	rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
546 547 548 549 550
	if (rdev) {
		set_bit(ClusterRemove, &rdev->flags);
		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
		md_wakeup_thread(mddev->thread);
	}
551
	else
552 553
		pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
			__func__, __LINE__, le32_to_cpu(msg->raid_slot));
554
	rcu_read_unlock();
555 556
}

557 558
static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
{
559
	struct md_rdev *rdev;
560

561 562
	rcu_read_lock();
	rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
563 564 565
	if (rdev && test_bit(Faulty, &rdev->flags))
		clear_bit(Faulty, &rdev->flags);
	else
566 567
		pr_warn("%s: %d Could not find disk(%d) which is faulty",
			__func__, __LINE__, le32_to_cpu(msg->raid_slot));
568
	rcu_read_unlock();
569 570
}

571
static int process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
572
{
573 574
	int ret = 0;

575 576
	if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
		"node %d received it's own msg\n", le32_to_cpu(msg->slot)))
577
		return -1;
578
	switch (le32_to_cpu(msg->type)) {
579
	case METADATA_UPDATED:
580
		process_metadata_update(mddev, msg);
581 582
		break;
	case RESYNCING:
583 584 585
		process_suspend_info(mddev, le32_to_cpu(msg->slot),
				     le64_to_cpu(msg->low),
				     le64_to_cpu(msg->high));
586
		break;
587 588
	case NEWDISK:
		process_add_new_disk(mddev, msg);
589 590 591 592
		break;
	case REMOVE:
		process_remove_disk(mddev, msg);
		break;
593 594 595
	case RE_ADD:
		process_readd_disk(mddev, msg);
		break;
596
	case BITMAP_NEEDS_SYNC:
597
		__recover_slot(mddev, le32_to_cpu(msg->slot));
598
		break;
599
	default:
600
		ret = -1;
601 602
		pr_warn("%s:%d Received unknown message from %d\n",
			__func__, __LINE__, msg->slot);
603
	}
604
	return ret;
605 606 607 608 609 610 611 612 613 614 615
}

/*
 * thread for receiving message
 */
static void recv_daemon(struct md_thread *thread)
{
	struct md_cluster_info *cinfo = thread->mddev->cluster_info;
	struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
	struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
	struct cluster_msg msg;
616
	int ret;
617

618
	mutex_lock(&cinfo->recv_mutex);
619 620 621
	/*get CR on Message*/
	if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
		pr_err("md/raid1:failed to get CR on MESSAGE\n");
622
		mutex_unlock(&cinfo->recv_mutex);
623 624 625 626 627
		return;
	}

	/* read lvb and wake up thread to process this message_lockres */
	memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
628 629 630
	ret = process_recvd_msg(thread->mddev, &msg);
	if (ret)
		goto out;
631 632

	/*release CR on ack_lockres*/
633 634 635
	ret = dlm_unlock_sync(ack_lockres);
	if (unlikely(ret != 0))
		pr_info("unlock ack failed return %d\n", ret);
636
	/*up-convert to PR on message_lockres*/
637 638 639
	ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
	if (unlikely(ret != 0))
		pr_info("lock PR on msg failed return %d\n", ret);
640
	/*get CR on ack_lockres again*/
641 642 643
	ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
	if (unlikely(ret != 0))
		pr_info("lock CR on ack failed return %d\n", ret);
644
out:
645
	/*release CR on message_lockres*/
646 647 648
	ret = dlm_unlock_sync(message_lockres);
	if (unlikely(ret != 0))
		pr_info("unlock msg failed return %d\n", ret);
649
	mutex_unlock(&cinfo->recv_mutex);
650 651
}

652
/* lock_token()
653 654 655
 * Takes the lock on the TOKEN lock resource so no other
 * node can communicate while the operation is underway.
 */
656
static int lock_token(struct md_cluster_info *cinfo, bool mddev_locked)
657
{
658 659
	int error, set_bit = 0;
	struct mddev *mddev = cinfo->mddev;
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674
	/*
	 * If resync thread run after raid1d thread, then process_metadata_update
	 * could not continue if raid1d held reconfig_mutex (and raid1d is blocked
	 * since another node already got EX on Token and waitting the EX of Ack),
	 * so let resync wake up thread in case flag is set.
	 */
	if (mddev_locked && !test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
				      &cinfo->state)) {
		error = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
					      &cinfo->state);
		WARN_ON_ONCE(error);
		md_wakeup_thread(mddev->thread);
		set_bit = 1;
	}
675
	error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
676 677 678
	if (set_bit)
		clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);

679 680 681
	if (error)
		pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
				__func__, __LINE__, error);
682 683 684

	/* Lock the receive sequence */
	mutex_lock(&cinfo->recv_mutex);
685 686 687
	return error;
}

688 689 690
/* lock_comm()
 * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
 */
691
static int lock_comm(struct md_cluster_info *cinfo, bool mddev_locked)
692 693 694 695
{
	wait_event(cinfo->wait,
		   !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));

696
	return lock_token(cinfo, mddev_locked);
697 698
}

699 700
static void unlock_comm(struct md_cluster_info *cinfo)
{
701
	WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
702
	mutex_unlock(&cinfo->recv_mutex);
703
	dlm_unlock_sync(cinfo->token_lockres);
704 705
	clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
	wake_up(&cinfo->wait);
706 707 708 709 710 711 712 713
}

/* __sendmsg()
 * This function performs the actual sending of the message. This function is
 * usually called after performing the encompassing operation
 * The function:
 * 1. Grabs the message lockresource in EX mode
 * 2. Copies the message to the message LVB
714
 * 3. Downconverts message lockresource to CW
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
 *    and the other nodes read the message. The thread will wait here until all other
 *    nodes have released ack lock resource.
 * 5. Downconvert ack lockresource to CR
 */
static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
{
	int error;
	int slot = cinfo->slot_number - 1;

	cmsg->slot = cpu_to_le32(slot);
	/*get EX on Message*/
	error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
	if (error) {
		pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
		goto failed_message;
	}

	memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
			sizeof(struct cluster_msg));
735 736
	/*down-convert EX to CW on Message*/
	error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
737
	if (error) {
738
		pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
739
				error);
740
		goto failed_ack;
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	}

	/*up-convert CR to EX on Ack*/
	error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
	if (error) {
		pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
				error);
		goto failed_ack;
	}

	/*down-convert EX to CR on Ack*/
	error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
	if (error) {
		pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
				error);
		goto failed_ack;
	}

failed_ack:
760 761 762 763 764 765 766
	error = dlm_unlock_sync(cinfo->message_lockres);
	if (unlikely(error != 0)) {
		pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
			error);
		/* in case the message can't be released due to some reason */
		goto failed_ack;
	}
767 768 769 770
failed_message:
	return error;
}

771 772
static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg,
		   bool mddev_locked)
773 774 775
{
	int ret;

776
	lock_comm(cinfo, mddev_locked);
777 778 779 780 781
	ret = __sendmsg(cinfo, cmsg);
	unlock_comm(cinfo);
	return ret;
}

782 783 784 785 786 787 788
static int gather_all_resync_info(struct mddev *mddev, int total_slots)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	int i, ret = 0;
	struct dlm_lock_resource *bm_lockres;
	struct suspend_info *s;
	char str[64];
789
	sector_t lo, hi;
790 791 792 793 794 795 796 797


	for (i = 0; i < total_slots; i++) {
		memset(str, '\0', 64);
		snprintf(str, 64, "bitmap%04d", i);
		bm_lockres = lockres_init(mddev, str, NULL, 1);
		if (!bm_lockres)
			return -ENOMEM;
S
Shaohua Li 已提交
798 799
		if (i == (cinfo->slot_number - 1)) {
			lockres_free(bm_lockres);
800
			continue;
S
Shaohua Li 已提交
801
		}
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

		bm_lockres->flags |= DLM_LKF_NOQUEUE;
		ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
		if (ret == -EAGAIN) {
			s = read_resync_info(mddev, bm_lockres);
			if (s) {
				pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
						__func__, __LINE__,
						(unsigned long long) s->lo,
						(unsigned long long) s->hi, i);
				spin_lock_irq(&cinfo->suspend_lock);
				s->slot = i;
				list_add(&s->list, &cinfo->suspend_list);
				spin_unlock_irq(&cinfo->suspend_lock);
			}
			ret = 0;
			lockres_free(bm_lockres);
			continue;
		}
821 822
		if (ret) {
			lockres_free(bm_lockres);
823
			goto out;
824
		}
825 826 827 828 829 830 831 832 833 834 835 836 837 838

		/* Read the disk bitmap sb and check if it needs recovery */
		ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
		if (ret) {
			pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
			lockres_free(bm_lockres);
			continue;
		}
		if ((hi > 0) && (lo < mddev->recovery_cp)) {
			set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
			mddev->recovery_cp = lo;
			md_check_recovery(mddev);
		}

839 840 841 842 843 844
		lockres_free(bm_lockres);
	}
out:
	return ret;
}

845 846
static int join(struct mddev *mddev, int nodes)
{
G
Goldwyn Rodrigues 已提交
847
	struct md_cluster_info *cinfo;
G
Goldwyn Rodrigues 已提交
848
	int ret, ops_rv;
G
Goldwyn Rodrigues 已提交
849 850 851 852 853 854
	char str[64];

	cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
	if (!cinfo)
		return -ENOMEM;

855 856
	INIT_LIST_HEAD(&cinfo->suspend_list);
	spin_lock_init(&cinfo->suspend_lock);
G
Goldwyn Rodrigues 已提交
857
	init_completion(&cinfo->completion);
858
	set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
859 860
	init_waitqueue_head(&cinfo->wait);
	mutex_init(&cinfo->recv_mutex);
G
Goldwyn Rodrigues 已提交
861 862

	mddev->cluster_info = cinfo;
863
	cinfo->mddev = mddev;
G
Goldwyn Rodrigues 已提交
864

G
Goldwyn Rodrigues 已提交
865
	memset(str, 0, 64);
866
	sprintf(str, "%pU", mddev->uuid);
G
Goldwyn Rodrigues 已提交
867 868 869
	ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
				DLM_LSFL_FS, LVB_SIZE,
				&md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
G
Goldwyn Rodrigues 已提交
870 871
	if (ret)
		goto err;
G
Goldwyn Rodrigues 已提交
872
	wait_for_completion(&cinfo->completion);
873 874 875
	if (nodes < cinfo->slot_number) {
		pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
			cinfo->slot_number, nodes);
876 877 878
		ret = -ERANGE;
		goto err;
	}
879 880 881 882 883 884 885 886 887 888 889 890 891
	/* Initiate the communication resources */
	ret = -ENOMEM;
	cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
	if (!cinfo->recv_thread) {
		pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
		goto err;
	}
	cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
	if (!cinfo->message_lockres)
		goto err;
	cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
	if (!cinfo->token_lockres)
		goto err;
892 893 894 895
	cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
	if (!cinfo->no_new_dev_lockres)
		goto err;

896 897 898 899 900 901 902
	ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
	if (ret) {
		ret = -EAGAIN;
		pr_err("md-cluster: can't join cluster to avoid lock issue\n");
		goto err;
	}
	cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
903 904
	if (!cinfo->ack_lockres) {
		ret = -ENOMEM;
905
		goto err;
906
	}
907 908 909 910
	/* get sync CR lock on ACK. */
	if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
		pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
				ret);
911
	dlm_unlock_sync(cinfo->token_lockres);
912 913 914 915
	/* get sync CR lock on no-new-dev. */
	if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
		pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);

916 917 918 919

	pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
	snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
	cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
920 921
	if (!cinfo->bitmap_lockres) {
		ret = -ENOMEM;
922
		goto err;
923
	}
924 925 926 927 928 929
	if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
		pr_err("Failed to get bitmap lock\n");
		ret = -EINVAL;
		goto err;
	}

930
	cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
931 932
	if (!cinfo->resync_lockres) {
		ret = -ENOMEM;
933
		goto err;
934
	}
935

936
	return 0;
G
Goldwyn Rodrigues 已提交
937
err:
938
	set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
939 940
	md_unregister_thread(&cinfo->recovery_thread);
	md_unregister_thread(&cinfo->recv_thread);
941 942 943
	lockres_free(cinfo->message_lockres);
	lockres_free(cinfo->token_lockres);
	lockres_free(cinfo->ack_lockres);
944
	lockres_free(cinfo->no_new_dev_lockres);
945
	lockres_free(cinfo->resync_lockres);
946
	lockres_free(cinfo->bitmap_lockres);
G
Goldwyn Rodrigues 已提交
947 948
	if (cinfo->lockspace)
		dlm_release_lockspace(cinfo->lockspace, 2);
G
Goldwyn Rodrigues 已提交
949
	mddev->cluster_info = NULL;
G
Goldwyn Rodrigues 已提交
950 951
	kfree(cinfo);
	return ret;
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966
static void load_bitmaps(struct mddev *mddev, int total_slots)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;

	/* load all the node's bitmap info for resync */
	if (gather_all_resync_info(mddev, total_slots))
		pr_err("md-cluster: failed to gather all resyn infos\n");
	set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state);
	/* wake up recv thread in case something need to be handled */
	if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
		md_wakeup_thread(cinfo->recv_thread);
}

967 968 969 970 971 972 973
static void resync_bitmap(struct mddev *mddev)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	struct cluster_msg cmsg = {0};
	int err;

	cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
974
	err = sendmsg(cinfo, &cmsg, 1);
975 976 977 978 979
	if (err)
		pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
			__func__, __LINE__, err);
}

980
static void unlock_all_bitmaps(struct mddev *mddev);
981 982
static int leave(struct mddev *mddev)
{
G
Goldwyn Rodrigues 已提交
983 984 985 986
	struct md_cluster_info *cinfo = mddev->cluster_info;

	if (!cinfo)
		return 0;
987 988 989 990 991 992 993

	/* BITMAP_NEEDS_SYNC message should be sent when node
	 * is leaving the cluster with dirty bitmap, also we
	 * can only deliver it when dlm connection is available */
	if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
		resync_bitmap(mddev);

994
	set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
995
	md_unregister_thread(&cinfo->recovery_thread);
996 997 998 999
	md_unregister_thread(&cinfo->recv_thread);
	lockres_free(cinfo->message_lockres);
	lockres_free(cinfo->token_lockres);
	lockres_free(cinfo->ack_lockres);
1000
	lockres_free(cinfo->no_new_dev_lockres);
S
Shaohua Li 已提交
1001
	lockres_free(cinfo->resync_lockres);
1002
	lockres_free(cinfo->bitmap_lockres);
1003
	unlock_all_bitmaps(mddev);
G
Goldwyn Rodrigues 已提交
1004
	dlm_release_lockspace(cinfo->lockspace, 2);
1005
	kfree(cinfo);
1006 1007 1008
	return 0;
}

G
Goldwyn Rodrigues 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
/* slot_number(): Returns the MD slot number to use
 * DLM starts the slot numbers from 1, wheras cluster-md
 * wants the number to be from zero, so we deduct one
 */
static int slot_number(struct mddev *mddev)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;

	return cinfo->slot_number - 1;
}

1020 1021 1022 1023 1024 1025
/*
 * Check if the communication is already locked, else lock the communication
 * channel.
 * If it is already locked, token is in EX mode, and hence lock_token()
 * should not be called.
 */
1026 1027
static int metadata_update_start(struct mddev *mddev)
{
1028
	struct md_cluster_info *cinfo = mddev->cluster_info;
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	int ret;

	/*
	 * metadata_update_start is always called with the protection of
	 * reconfig_mutex, so set WAITING_FOR_TOKEN here.
	 */
	ret = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
				    &cinfo->state);
	WARN_ON_ONCE(ret);
	md_wakeup_thread(mddev->thread);
1039 1040 1041 1042 1043 1044

	wait_event(cinfo->wait,
		   !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
		   test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));

	/* If token is already locked, return 0 */
1045 1046
	if (cinfo->token_lockres->mode == DLM_LOCK_EX) {
		clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
1047
		return 0;
1048
	}
1049

1050 1051 1052
	ret = lock_token(cinfo, 1);
	clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
	return ret;
1053 1054 1055 1056 1057 1058
}

static int metadata_update_finish(struct mddev *mddev)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	struct cluster_msg cmsg;
1059 1060
	struct md_rdev *rdev;
	int ret = 0;
1061
	int raid_slot = -1;
1062 1063 1064

	memset(&cmsg, 0, sizeof(cmsg));
	cmsg.type = cpu_to_le32(METADATA_UPDATED);
1065 1066 1067 1068
	/* Pick up a good active device number to send.
	 */
	rdev_for_each(rdev, mddev)
		if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
1069
			raid_slot = rdev->desc_nr;
1070 1071
			break;
		}
1072 1073
	if (raid_slot >= 0) {
		cmsg.raid_slot = cpu_to_le32(raid_slot);
1074
		ret = __sendmsg(cinfo, &cmsg);
1075
	} else
1076
		pr_warn("md-cluster: No good device id found to send\n");
1077
	clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1078 1079 1080 1081
	unlock_comm(cinfo);
	return ret;
}

1082
static void metadata_update_cancel(struct mddev *mddev)
1083 1084
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
1085
	clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1086
	unlock_comm(cinfo);
1087 1088
}

1089 1090 1091
static int resync_start(struct mddev *mddev)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
1092
	return dlm_lock_sync_interruptible(cinfo->resync_lockres, DLM_LOCK_EX, mddev);
1093 1094
}

1095
static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
1096 1097
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
1098
	struct resync_info ri;
1099
	struct cluster_msg cmsg = {0};
1100

1101 1102 1103 1104 1105 1106 1107
	/* do not send zero again, if we have sent before */
	if (hi == 0) {
		memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
		if (le64_to_cpu(ri.hi) == 0)
			return 0;
	}

1108
	add_resync_info(cinfo->bitmap_lockres, lo, hi);
1109 1110 1111
	/* Re-acquire the lock to refresh LVB */
	dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
	cmsg.type = cpu_to_le32(RESYNCING);
1112 1113
	cmsg.low = cpu_to_le64(lo);
	cmsg.high = cpu_to_le64(hi);
1114

1115 1116 1117 1118 1119 1120 1121 1122
	/*
	 * mddev_lock is held if resync_info_update is called from
	 * resync_finish (md_reap_sync_thread -> resync_finish)
	 */
	if (lo == 0 && hi == 0)
		return sendmsg(cinfo, &cmsg, 1);
	else
		return sendmsg(cinfo, &cmsg, 0);
1123 1124
}

1125 1126 1127 1128 1129 1130 1131
static int resync_finish(struct mddev *mddev)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	dlm_unlock_sync(cinfo->resync_lockres);
	return resync_info_update(mddev, 0, 0);
}

1132 1133
static int area_resyncing(struct mddev *mddev, int direction,
		sector_t lo, sector_t hi)
1134 1135 1136 1137 1138
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	int ret = 0;
	struct suspend_info *s;

1139 1140 1141 1142
	if ((direction == READ) &&
		test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
		return 1;

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	spin_lock_irq(&cinfo->suspend_lock);
	if (list_empty(&cinfo->suspend_list))
		goto out;
	list_for_each_entry(s, &cinfo->suspend_list, list)
		if (hi > s->lo && lo < s->hi) {
			ret = 1;
			break;
		}
out:
	spin_unlock_irq(&cinfo->suspend_lock);
	return ret;
}

1156 1157 1158 1159 1160
/* add_new_disk() - initiates a disk add
 * However, if this fails before writing md_update_sb(),
 * add_new_disk_cancel() must be called to release token lock
 */
static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	struct cluster_msg cmsg;
	int ret = 0;
	struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
	char *uuid = sb->device_uuid;

	memset(&cmsg, 0, sizeof(cmsg));
	cmsg.type = cpu_to_le32(NEWDISK);
	memcpy(cmsg.uuid, uuid, 16);
1171
	cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1172
	lock_comm(cinfo, 1);
1173 1174 1175 1176 1177 1178 1179 1180 1181
	ret = __sendmsg(cinfo, &cmsg);
	if (ret)
		return ret;
	cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
	ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
	cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
	/* Some node does not "see" the device */
	if (ret == -EAGAIN)
		ret = -ENOENT;
1182 1183
	if (ret)
		unlock_comm(cinfo);
1184
	else {
1185
		dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
		/* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
		 * will run soon after add_new_disk, the below path will be
		 * invoked:
		 *   md_wakeup_thread(mddev->thread)
		 *	-> conf->thread (raid1d)
		 *	-> md_check_recovery -> md_update_sb
		 *	-> metadata_update_start/finish
		 * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
		 *
		 * For other failure cases, metadata_update_cancel and
		 * add_new_disk_cancel also clear below bit as well.
		 * */
1198 1199 1200
		set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
		wake_up(&cinfo->wait);
	}
1201 1202 1203
	return ret;
}

1204
static void add_new_disk_cancel(struct mddev *mddev)
1205
{
1206
	struct md_cluster_info *cinfo = mddev->cluster_info;
1207
	clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1208
	unlock_comm(cinfo);
1209 1210
}

1211
static int new_disk_ack(struct mddev *mddev, bool ack)
1212 1213 1214
{
	struct md_cluster_info *cinfo = mddev->cluster_info;

1215 1216 1217 1218 1219
	if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
		pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
		return -EINVAL;
	}

1220 1221 1222
	if (ack)
		dlm_unlock_sync(cinfo->no_new_dev_lockres);
	complete(&cinfo->newdisk_completion);
1223
	return 0;
1224 1225
}

1226 1227
static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
{
1228
	struct cluster_msg cmsg = {0};
1229
	struct md_cluster_info *cinfo = mddev->cluster_info;
1230 1231
	cmsg.type = cpu_to_le32(REMOVE);
	cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1232
	return sendmsg(cinfo, &cmsg, 1);
1233 1234
}

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
static int lock_all_bitmaps(struct mddev *mddev)
{
	int slot, my_slot, ret, held = 1, i = 0;
	char str[64];
	struct md_cluster_info *cinfo = mddev->cluster_info;

	cinfo->other_bitmap_lockres = kzalloc((mddev->bitmap_info.nodes - 1) *
					     sizeof(struct dlm_lock_resource *),
					     GFP_KERNEL);
	if (!cinfo->other_bitmap_lockres) {
		pr_err("md: can't alloc mem for other bitmap locks\n");
		return 0;
	}

	my_slot = slot_number(mddev);
	for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
		if (slot == my_slot)
			continue;

		memset(str, '\0', 64);
		snprintf(str, 64, "bitmap%04d", slot);
		cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
		if (!cinfo->other_bitmap_lockres[i])
			return -ENOMEM;

		cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
		ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
		if (ret)
			held = -1;
		i++;
	}

	return held;
}

static void unlock_all_bitmaps(struct mddev *mddev)
{
	struct md_cluster_info *cinfo = mddev->cluster_info;
	int i;

	/* release other node's bitmap lock if they are existed */
	if (cinfo->other_bitmap_lockres) {
		for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
			if (cinfo->other_bitmap_lockres[i]) {
				lockres_free(cinfo->other_bitmap_lockres[i]);
			}
		}
		kfree(cinfo->other_bitmap_lockres);
	}
}

1286 1287 1288 1289
static int gather_bitmaps(struct md_rdev *rdev)
{
	int sn, err;
	sector_t lo, hi;
1290
	struct cluster_msg cmsg = {0};
1291 1292 1293
	struct mddev *mddev = rdev->mddev;
	struct md_cluster_info *cinfo = mddev->cluster_info;

1294 1295
	cmsg.type = cpu_to_le32(RE_ADD);
	cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1296
	err = sendmsg(cinfo, &cmsg, 1);
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	if (err)
		goto out;

	for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
		if (sn == (cinfo->slot_number - 1))
			continue;
		err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
		if (err) {
			pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
			goto out;
		}
		if ((hi > 0) && (lo < mddev->recovery_cp))
			mddev->recovery_cp = lo;
	}
out:
	return err;
}

1315 1316 1317
static struct md_cluster_operations cluster_ops = {
	.join   = join,
	.leave  = leave,
G
Goldwyn Rodrigues 已提交
1318
	.slot_number = slot_number,
1319 1320
	.resync_start = resync_start,
	.resync_finish = resync_finish,
1321
	.resync_info_update = resync_info_update,
1322 1323 1324
	.metadata_update_start = metadata_update_start,
	.metadata_update_finish = metadata_update_finish,
	.metadata_update_cancel = metadata_update_cancel,
1325
	.area_resyncing = area_resyncing,
1326 1327
	.add_new_disk = add_new_disk,
	.add_new_disk_cancel = add_new_disk_cancel,
1328
	.new_disk_ack = new_disk_ack,
1329
	.remove_disk = remove_disk,
1330
	.load_bitmaps = load_bitmaps,
1331
	.gather_bitmaps = gather_bitmaps,
1332 1333
	.lock_all_bitmaps = lock_all_bitmaps,
	.unlock_all_bitmaps = unlock_all_bitmaps,
1334 1335
};

1336 1337 1338 1339
static int __init cluster_init(void)
{
	pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
	pr_info("Registering Cluster MD functions\n");
1340
	register_md_cluster_operations(&cluster_ops, THIS_MODULE);
1341 1342 1343 1344 1345
	return 0;
}

static void cluster_exit(void)
{
1346
	unregister_md_cluster_operations();
1347 1348 1349 1350
}

module_init(cluster_init);
module_exit(cluster_exit);
1351
MODULE_AUTHOR("SUSE");
1352 1353
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Clustering support for MD");