xref: /openbmc/linux/drivers/md/md-cluster.c (revision 7eb8ff02)
18d7c56d0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28e854e9cSGoldwyn Rodrigues /*
38e854e9cSGoldwyn Rodrigues  * Copyright (C) 2015, SUSE
48e854e9cSGoldwyn Rodrigues  */
58e854e9cSGoldwyn Rodrigues 
68e854e9cSGoldwyn Rodrigues 
78e854e9cSGoldwyn Rodrigues #include <linux/module.h>
87bcda714SGuoqing Jiang #include <linux/kthread.h>
947741b7cSGoldwyn Rodrigues #include <linux/dlm.h>
1047741b7cSGoldwyn Rodrigues #include <linux/sched.h>
111aee41f6SGoldwyn Rodrigues #include <linux/raid/md_p.h>
1247741b7cSGoldwyn Rodrigues #include "md.h"
13935fe098SMike Snitzer #include "md-bitmap.h"
14edb39c9dSGoldwyn Rodrigues #include "md-cluster.h"
1547741b7cSGoldwyn Rodrigues 
1647741b7cSGoldwyn Rodrigues #define LVB_SIZE	64
171aee41f6SGoldwyn Rodrigues #define NEW_DEV_TIMEOUT 5000
1847741b7cSGoldwyn Rodrigues 
1947741b7cSGoldwyn Rodrigues struct dlm_lock_resource {
2047741b7cSGoldwyn Rodrigues 	dlm_lockspace_t *ls;
2147741b7cSGoldwyn Rodrigues 	struct dlm_lksb lksb;
2247741b7cSGoldwyn Rodrigues 	char *name; /* lock name. */
2347741b7cSGoldwyn Rodrigues 	uint32_t flags; /* flags to pass to dlm_lock() */
24fccb60a4SGuoqing Jiang 	wait_queue_head_t sync_locking; /* wait queue for synchronized locking */
25fccb60a4SGuoqing Jiang 	bool sync_locking_done;
26c4ce867fSGoldwyn Rodrigues 	void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
27c4ce867fSGoldwyn Rodrigues 	struct mddev *mddev; /* pointing back to mddev. */
28dbb64f86SGoldwyn Rodrigues 	int mode;
29c4ce867fSGoldwyn Rodrigues };
30c4ce867fSGoldwyn Rodrigues 
3196ae923aSGoldwyn Rodrigues struct resync_info {
3296ae923aSGoldwyn Rodrigues 	__le64 lo;
3396ae923aSGoldwyn Rodrigues 	__le64 hi;
3496ae923aSGoldwyn Rodrigues };
3596ae923aSGoldwyn Rodrigues 
36fa8259daSGoldwyn Rodrigues /* md_cluster_info flags */
37fa8259daSGoldwyn Rodrigues #define		MD_CLUSTER_WAITING_FOR_NEWDISK		1
3890382ed9SGoldwyn Rodrigues #define		MD_CLUSTER_SUSPEND_READ_BALANCING	2
39eece075cSGuoqing Jiang #define		MD_CLUSTER_BEGIN_JOIN_CLUSTER		3
40fa8259daSGoldwyn Rodrigues 
418b9277c8SGuoqing Jiang /* Lock the send communication. This is done through
428b9277c8SGuoqing Jiang  * bit manipulation as opposed to a mutex in order to
439e26728bSZhang Jiaming  * accommodate lock and hold. See next comment.
448b9277c8SGuoqing Jiang  */
458b9277c8SGuoqing Jiang #define		MD_CLUSTER_SEND_LOCK			4
46e19508faSGuoqing Jiang /* If cluster operations (such as adding a disk) must lock the
47e19508faSGuoqing Jiang  * communication channel, so as to perform extra operations
48e19508faSGuoqing Jiang  * (update metadata) and no other operation is allowed on the
49e19508faSGuoqing Jiang  * MD. Token needs to be locked and held until the operation
50e19508faSGuoqing Jiang  * completes witha md_update_sb(), which would eventually release
51e19508faSGuoqing Jiang  * the lock.
528b9277c8SGuoqing Jiang  */
538b9277c8SGuoqing Jiang #define		MD_CLUSTER_SEND_LOCKED_ALREADY		5
5451e453aeSGuoqing Jiang /* We should receive message after node joined cluster and
5551e453aeSGuoqing Jiang  * set up all the related infos such as bitmap and personality */
5651e453aeSGuoqing Jiang #define		MD_CLUSTER_ALREADY_IN_CLUSTER		6
5751e453aeSGuoqing Jiang #define		MD_CLUSTER_PENDING_RECV_EVENT		7
580ba95977SGuoqing Jiang #define 	MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD		8
59fa8259daSGoldwyn Rodrigues 
60c4ce867fSGoldwyn Rodrigues struct md_cluster_info {
610ba95977SGuoqing Jiang 	struct mddev *mddev; /* the md device which md_cluster_info belongs to */
62c4ce867fSGoldwyn Rodrigues 	/* dlm lock space and resources for clustered raid. */
63c4ce867fSGoldwyn Rodrigues 	dlm_lockspace_t *lockspace;
64cf921cc1SGoldwyn Rodrigues 	int slot_number;
65cf921cc1SGoldwyn Rodrigues 	struct completion completion;
668b9277c8SGuoqing Jiang 	struct mutex recv_mutex;
6754519c5fSGoldwyn Rodrigues 	struct dlm_lock_resource *bitmap_lockres;
68f6a2dc64SGuoqing Jiang 	struct dlm_lock_resource **other_bitmap_lockres;
69c186b128SGoldwyn Rodrigues 	struct dlm_lock_resource *resync_lockres;
7096ae923aSGoldwyn Rodrigues 	struct list_head suspend_list;
71ea89238cSGuoqing Jiang 
7296ae923aSGoldwyn Rodrigues 	spinlock_t suspend_lock;
73ea89238cSGuoqing Jiang 	/* record the region which write should be suspended */
74ea89238cSGuoqing Jiang 	sector_t suspend_lo;
75ea89238cSGuoqing Jiang 	sector_t suspend_hi;
76ea89238cSGuoqing Jiang 	int suspend_from; /* the slot which broadcast suspend_lo/hi */
77ea89238cSGuoqing Jiang 
7844693154SYu Kuai 	struct md_thread __rcu *recovery_thread;
79e94987dbSGoldwyn Rodrigues 	unsigned long recovery_map;
804664680cSGoldwyn Rodrigues 	/* communication loc resources */
814664680cSGoldwyn Rodrigues 	struct dlm_lock_resource *ack_lockres;
824664680cSGoldwyn Rodrigues 	struct dlm_lock_resource *message_lockres;
834664680cSGoldwyn Rodrigues 	struct dlm_lock_resource *token_lockres;
841aee41f6SGoldwyn Rodrigues 	struct dlm_lock_resource *no_new_dev_lockres;
8544693154SYu Kuai 	struct md_thread __rcu *recv_thread;
861aee41f6SGoldwyn Rodrigues 	struct completion newdisk_completion;
878b9277c8SGuoqing Jiang 	wait_queue_head_t wait;
88fa8259daSGoldwyn Rodrigues 	unsigned long state;
8918c9ff7fSGuoqing Jiang 	/* record the region in RESYNCING message */
9018c9ff7fSGuoqing Jiang 	sector_t sync_low;
9118c9ff7fSGuoqing Jiang 	sector_t sync_hi;
924664680cSGoldwyn Rodrigues };
934664680cSGoldwyn Rodrigues 
944664680cSGoldwyn Rodrigues enum msg_type {
954664680cSGoldwyn Rodrigues 	METADATA_UPDATED = 0,
964664680cSGoldwyn Rodrigues 	RESYNCING,
971aee41f6SGoldwyn Rodrigues 	NEWDISK,
9888bcfef7SGoldwyn Rodrigues 	REMOVE,
9997f6cd39SGoldwyn Rodrigues 	RE_ADD,
100dc737d7cSGuoqing Jiang 	BITMAP_NEEDS_SYNC,
1017da3d203SGuoqing Jiang 	CHANGE_CAPACITY,
102afd75628SGuoqing Jiang 	BITMAP_RESIZE,
1034664680cSGoldwyn Rodrigues };
1044664680cSGoldwyn Rodrigues 
1054664680cSGoldwyn Rodrigues struct cluster_msg {
106cf97a348SGuoqing Jiang 	__le32 type;
107cf97a348SGuoqing Jiang 	__le32 slot;
1081aee41f6SGoldwyn Rodrigues 	/* TODO: Unionize this for smaller footprint */
109cf97a348SGuoqing Jiang 	__le64 low;
110cf97a348SGuoqing Jiang 	__le64 high;
1111aee41f6SGoldwyn Rodrigues 	char uuid[16];
112cf97a348SGuoqing Jiang 	__le32 raid_slot;
11347741b7cSGoldwyn Rodrigues };
11447741b7cSGoldwyn Rodrigues 
sync_ast(void * arg)11547741b7cSGoldwyn Rodrigues static void sync_ast(void *arg)
11647741b7cSGoldwyn Rodrigues {
11747741b7cSGoldwyn Rodrigues 	struct dlm_lock_resource *res;
11847741b7cSGoldwyn Rodrigues 
1192e2a7cd9SNeilBrown 	res = arg;
120fccb60a4SGuoqing Jiang 	res->sync_locking_done = true;
121fccb60a4SGuoqing Jiang 	wake_up(&res->sync_locking);
12247741b7cSGoldwyn Rodrigues }
12347741b7cSGoldwyn Rodrigues 
dlm_lock_sync(struct dlm_lock_resource * res,int mode)12447741b7cSGoldwyn Rodrigues static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
12547741b7cSGoldwyn Rodrigues {
12647741b7cSGoldwyn Rodrigues 	int ret = 0;
12747741b7cSGoldwyn Rodrigues 
12847741b7cSGoldwyn Rodrigues 	ret = dlm_lock(res->ls, mode, &res->lksb,
12947741b7cSGoldwyn Rodrigues 			res->flags, res->name, strlen(res->name),
13047741b7cSGoldwyn Rodrigues 			0, sync_ast, res, res->bast);
13147741b7cSGoldwyn Rodrigues 	if (ret)
13247741b7cSGoldwyn Rodrigues 		return ret;
133fccb60a4SGuoqing Jiang 	wait_event(res->sync_locking, res->sync_locking_done);
134fccb60a4SGuoqing Jiang 	res->sync_locking_done = false;
135dbb64f86SGoldwyn Rodrigues 	if (res->lksb.sb_status == 0)
136dbb64f86SGoldwyn Rodrigues 		res->mode = mode;
13747741b7cSGoldwyn Rodrigues 	return res->lksb.sb_status;
13847741b7cSGoldwyn Rodrigues }
13947741b7cSGoldwyn Rodrigues 
dlm_unlock_sync(struct dlm_lock_resource * res)14047741b7cSGoldwyn Rodrigues static int dlm_unlock_sync(struct dlm_lock_resource *res)
14147741b7cSGoldwyn Rodrigues {
14247741b7cSGoldwyn Rodrigues 	return dlm_lock_sync(res, DLM_LOCK_NL);
14347741b7cSGoldwyn Rodrigues }
14447741b7cSGoldwyn Rodrigues 
1457bcda714SGuoqing Jiang /*
1467bcda714SGuoqing Jiang  * An variation of dlm_lock_sync, which make lock request could
1477bcda714SGuoqing Jiang  * be interrupted
1487bcda714SGuoqing Jiang  */
dlm_lock_sync_interruptible(struct dlm_lock_resource * res,int mode,struct mddev * mddev)1497bcda714SGuoqing Jiang static int dlm_lock_sync_interruptible(struct dlm_lock_resource *res, int mode,
1507bcda714SGuoqing Jiang 				       struct mddev *mddev)
1517bcda714SGuoqing Jiang {
1527bcda714SGuoqing Jiang 	int ret = 0;
1537bcda714SGuoqing Jiang 
1547bcda714SGuoqing Jiang 	ret = dlm_lock(res->ls, mode, &res->lksb,
1557bcda714SGuoqing Jiang 			res->flags, res->name, strlen(res->name),
1567bcda714SGuoqing Jiang 			0, sync_ast, res, res->bast);
1577bcda714SGuoqing Jiang 	if (ret)
1587bcda714SGuoqing Jiang 		return ret;
1597bcda714SGuoqing Jiang 
1607bcda714SGuoqing Jiang 	wait_event(res->sync_locking, res->sync_locking_done
161d6385db9SGuoqing Jiang 				      || kthread_should_stop()
162d6385db9SGuoqing Jiang 				      || test_bit(MD_CLOSING, &mddev->flags));
1637bcda714SGuoqing Jiang 	if (!res->sync_locking_done) {
1647bcda714SGuoqing Jiang 		/*
1657bcda714SGuoqing Jiang 		 * the convert queue contains the lock request when request is
1667bcda714SGuoqing Jiang 		 * interrupted, and sync_ast could still be run, so need to
1677bcda714SGuoqing Jiang 		 * cancel the request and reset completion
1687bcda714SGuoqing Jiang 		 */
1697bcda714SGuoqing Jiang 		ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_CANCEL,
1707bcda714SGuoqing Jiang 			&res->lksb, res);
1717bcda714SGuoqing Jiang 		res->sync_locking_done = false;
1727bcda714SGuoqing Jiang 		if (unlikely(ret != 0))
1737bcda714SGuoqing Jiang 			pr_info("failed to cancel previous lock request "
1747bcda714SGuoqing Jiang 				 "%s return %d\n", res->name, ret);
1757bcda714SGuoqing Jiang 		return -EPERM;
1767bcda714SGuoqing Jiang 	} else
1777bcda714SGuoqing Jiang 		res->sync_locking_done = false;
1787bcda714SGuoqing Jiang 	if (res->lksb.sb_status == 0)
1797bcda714SGuoqing Jiang 		res->mode = mode;
1807bcda714SGuoqing Jiang 	return res->lksb.sb_status;
1817bcda714SGuoqing Jiang }
1827bcda714SGuoqing Jiang 
lockres_init(struct mddev * mddev,char * name,void (* bastfn)(void * arg,int mode),int with_lvb)183c4ce867fSGoldwyn Rodrigues static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
18447741b7cSGoldwyn Rodrigues 		char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
18547741b7cSGoldwyn Rodrigues {
18647741b7cSGoldwyn Rodrigues 	struct dlm_lock_resource *res = NULL;
18747741b7cSGoldwyn Rodrigues 	int ret, namelen;
188c4ce867fSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
18947741b7cSGoldwyn Rodrigues 
19047741b7cSGoldwyn Rodrigues 	res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
19147741b7cSGoldwyn Rodrigues 	if (!res)
19247741b7cSGoldwyn Rodrigues 		return NULL;
193fccb60a4SGuoqing Jiang 	init_waitqueue_head(&res->sync_locking);
194fccb60a4SGuoqing Jiang 	res->sync_locking_done = false;
195c4ce867fSGoldwyn Rodrigues 	res->ls = cinfo->lockspace;
196c4ce867fSGoldwyn Rodrigues 	res->mddev = mddev;
197dbb64f86SGoldwyn Rodrigues 	res->mode = DLM_LOCK_IV;
19847741b7cSGoldwyn Rodrigues 	namelen = strlen(name);
19947741b7cSGoldwyn Rodrigues 	res->name = kzalloc(namelen + 1, GFP_KERNEL);
20047741b7cSGoldwyn Rodrigues 	if (!res->name) {
20147741b7cSGoldwyn Rodrigues 		pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
20247741b7cSGoldwyn Rodrigues 		goto out_err;
20347741b7cSGoldwyn Rodrigues 	}
20492d9aac9SHeming Zhao 	strscpy(res->name, name, namelen + 1);
20547741b7cSGoldwyn Rodrigues 	if (with_lvb) {
20647741b7cSGoldwyn Rodrigues 		res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
20747741b7cSGoldwyn Rodrigues 		if (!res->lksb.sb_lvbptr) {
20847741b7cSGoldwyn Rodrigues 			pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
20947741b7cSGoldwyn Rodrigues 			goto out_err;
21047741b7cSGoldwyn Rodrigues 		}
21147741b7cSGoldwyn Rodrigues 		res->flags = DLM_LKF_VALBLK;
21247741b7cSGoldwyn Rodrigues 	}
21347741b7cSGoldwyn Rodrigues 
21447741b7cSGoldwyn Rodrigues 	if (bastfn)
21547741b7cSGoldwyn Rodrigues 		res->bast = bastfn;
21647741b7cSGoldwyn Rodrigues 
21747741b7cSGoldwyn Rodrigues 	res->flags |= DLM_LKF_EXPEDITE;
21847741b7cSGoldwyn Rodrigues 
21947741b7cSGoldwyn Rodrigues 	ret = dlm_lock_sync(res, DLM_LOCK_NL);
22047741b7cSGoldwyn Rodrigues 	if (ret) {
22147741b7cSGoldwyn Rodrigues 		pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
22247741b7cSGoldwyn Rodrigues 		goto out_err;
22347741b7cSGoldwyn Rodrigues 	}
22447741b7cSGoldwyn Rodrigues 	res->flags &= ~DLM_LKF_EXPEDITE;
22547741b7cSGoldwyn Rodrigues 	res->flags |= DLM_LKF_CONVERT;
22647741b7cSGoldwyn Rodrigues 
22747741b7cSGoldwyn Rodrigues 	return res;
22847741b7cSGoldwyn Rodrigues out_err:
22947741b7cSGoldwyn Rodrigues 	kfree(res->lksb.sb_lvbptr);
23047741b7cSGoldwyn Rodrigues 	kfree(res->name);
23147741b7cSGoldwyn Rodrigues 	kfree(res);
23247741b7cSGoldwyn Rodrigues 	return NULL;
23347741b7cSGoldwyn Rodrigues }
23447741b7cSGoldwyn Rodrigues 
lockres_free(struct dlm_lock_resource * res)23547741b7cSGoldwyn Rodrigues static void lockres_free(struct dlm_lock_resource *res)
23647741b7cSGoldwyn Rodrigues {
237400cb454SGuoqing Jiang 	int ret = 0;
238b5ef5678SGuoqing Jiang 
23947741b7cSGoldwyn Rodrigues 	if (!res)
24047741b7cSGoldwyn Rodrigues 		return;
24147741b7cSGoldwyn Rodrigues 
242400cb454SGuoqing Jiang 	/*
243400cb454SGuoqing Jiang 	 * use FORCEUNLOCK flag, so we can unlock even the lock is on the
244400cb454SGuoqing Jiang 	 * waiting or convert queue
245400cb454SGuoqing Jiang 	 */
246400cb454SGuoqing Jiang 	ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_FORCEUNLOCK,
247400cb454SGuoqing Jiang 		&res->lksb, res);
248400cb454SGuoqing Jiang 	if (unlikely(ret != 0))
249400cb454SGuoqing Jiang 		pr_err("failed to unlock %s return %d\n", res->name, ret);
250400cb454SGuoqing Jiang 	else
251fccb60a4SGuoqing Jiang 		wait_event(res->sync_locking, res->sync_locking_done);
25247741b7cSGoldwyn Rodrigues 
25347741b7cSGoldwyn Rodrigues 	kfree(res->name);
25447741b7cSGoldwyn Rodrigues 	kfree(res->lksb.sb_lvbptr);
25547741b7cSGoldwyn Rodrigues 	kfree(res);
25647741b7cSGoldwyn Rodrigues }
2578e854e9cSGoldwyn Rodrigues 
add_resync_info(struct dlm_lock_resource * lockres,sector_t lo,sector_t hi)25830661b49SNeilBrown static void add_resync_info(struct dlm_lock_resource *lockres,
25996ae923aSGoldwyn Rodrigues 			    sector_t lo, sector_t hi)
26096ae923aSGoldwyn Rodrigues {
26196ae923aSGoldwyn Rodrigues 	struct resync_info *ri;
26296ae923aSGoldwyn Rodrigues 
26396ae923aSGoldwyn Rodrigues 	ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
26496ae923aSGoldwyn Rodrigues 	ri->lo = cpu_to_le64(lo);
26596ae923aSGoldwyn Rodrigues 	ri->hi = cpu_to_le64(hi);
26696ae923aSGoldwyn Rodrigues }
26796ae923aSGoldwyn Rodrigues 
read_resync_info(struct mddev * mddev,struct dlm_lock_resource * lockres)268ea89238cSGuoqing Jiang static int read_resync_info(struct mddev *mddev,
269ea89238cSGuoqing Jiang 			    struct dlm_lock_resource *lockres)
27096ae923aSGoldwyn Rodrigues {
27196ae923aSGoldwyn Rodrigues 	struct resync_info ri;
272ea89238cSGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
273ea89238cSGuoqing Jiang 	int ret = 0;
27496ae923aSGoldwyn Rodrigues 
27596ae923aSGoldwyn Rodrigues 	dlm_lock_sync(lockres, DLM_LOCK_CR);
27696ae923aSGoldwyn Rodrigues 	memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
277ea89238cSGuoqing Jiang 	if (le64_to_cpu(ri.hi) > 0) {
278ea89238cSGuoqing Jiang 		cinfo->suspend_hi = le64_to_cpu(ri.hi);
279ea89238cSGuoqing Jiang 		cinfo->suspend_lo = le64_to_cpu(ri.lo);
280ea89238cSGuoqing Jiang 		ret = 1;
28196ae923aSGoldwyn Rodrigues 	}
28296ae923aSGoldwyn Rodrigues 	dlm_unlock_sync(lockres);
283ea89238cSGuoqing Jiang 	return ret;
28496ae923aSGoldwyn Rodrigues }
28596ae923aSGoldwyn Rodrigues 
recover_bitmaps(struct md_thread * thread)2866dc69c9cSkbuild test robot static void recover_bitmaps(struct md_thread *thread)
287e94987dbSGoldwyn Rodrigues {
288e94987dbSGoldwyn Rodrigues 	struct mddev *mddev = thread->mddev;
289e94987dbSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
290e94987dbSGoldwyn Rodrigues 	struct dlm_lock_resource *bm_lockres;
291e94987dbSGoldwyn Rodrigues 	char str[64];
292e94987dbSGoldwyn Rodrigues 	int slot, ret;
293e94987dbSGoldwyn Rodrigues 	sector_t lo, hi;
294e94987dbSGoldwyn Rodrigues 
295e94987dbSGoldwyn Rodrigues 	while (cinfo->recovery_map) {
296e94987dbSGoldwyn Rodrigues 		slot = fls64((u64)cinfo->recovery_map) - 1;
297e94987dbSGoldwyn Rodrigues 
298e94987dbSGoldwyn Rodrigues 		snprintf(str, 64, "bitmap%04d", slot);
299e94987dbSGoldwyn Rodrigues 		bm_lockres = lockres_init(mddev, str, NULL, 1);
300e94987dbSGoldwyn Rodrigues 		if (!bm_lockres) {
301e94987dbSGoldwyn Rodrigues 			pr_err("md-cluster: Cannot initialize bitmaps\n");
302e94987dbSGoldwyn Rodrigues 			goto clear_bit;
303e94987dbSGoldwyn Rodrigues 		}
304e94987dbSGoldwyn Rodrigues 
3057bcda714SGuoqing Jiang 		ret = dlm_lock_sync_interruptible(bm_lockres, DLM_LOCK_PW, mddev);
306e94987dbSGoldwyn Rodrigues 		if (ret) {
307e94987dbSGoldwyn Rodrigues 			pr_err("md-cluster: Could not DLM lock %s: %d\n",
308e94987dbSGoldwyn Rodrigues 					str, ret);
309e94987dbSGoldwyn Rodrigues 			goto clear_bit;
310e94987dbSGoldwyn Rodrigues 		}
311e64e4018SAndy Shevchenko 		ret = md_bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
3124b26a08aSGoldwyn Rodrigues 		if (ret) {
313e94987dbSGoldwyn Rodrigues 			pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
314e3f924d3SGuoqing Jiang 			goto clear_bit;
3154b26a08aSGoldwyn Rodrigues 		}
316010228e4SGuoqing Jiang 
317010228e4SGuoqing Jiang 		/* Clear suspend_area associated with the bitmap */
318010228e4SGuoqing Jiang 		spin_lock_irq(&cinfo->suspend_lock);
319ea89238cSGuoqing Jiang 		cinfo->suspend_hi = 0;
320ea89238cSGuoqing Jiang 		cinfo->suspend_lo = 0;
321ea89238cSGuoqing Jiang 		cinfo->suspend_from = -1;
322010228e4SGuoqing Jiang 		spin_unlock_irq(&cinfo->suspend_lock);
323010228e4SGuoqing Jiang 
324cb9ee154SGuoqing Jiang 		/* Kick off a reshape if needed */
325cb9ee154SGuoqing Jiang 		if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
326cb9ee154SGuoqing Jiang 		    test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
327cb9ee154SGuoqing Jiang 		    mddev->reshape_position != MaxSector)
328cb9ee154SGuoqing Jiang 			md_wakeup_thread(mddev->sync_thread);
329cb9ee154SGuoqing Jiang 
3304b26a08aSGoldwyn Rodrigues 		if (hi > 0) {
3314b26a08aSGoldwyn Rodrigues 			if (lo < mddev->recovery_cp)
3324b26a08aSGoldwyn Rodrigues 				mddev->recovery_cp = lo;
333eb315cd0SGuoqing Jiang 			/* wake up thread to continue resync in case resync
334eb315cd0SGuoqing Jiang 			 * is not finished */
335eb315cd0SGuoqing Jiang 			if (mddev->recovery_cp != MaxSector) {
3360357ba27SGuoqing Jiang 				/*
3370357ba27SGuoqing Jiang 				 * clear the REMOTE flag since we will launch
3380357ba27SGuoqing Jiang 				 * resync thread in current node.
3390357ba27SGuoqing Jiang 				 */
3400357ba27SGuoqing Jiang 				clear_bit(MD_RESYNCING_REMOTE,
3410357ba27SGuoqing Jiang 					  &mddev->recovery);
342eb315cd0SGuoqing Jiang 				set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
343eb315cd0SGuoqing Jiang 				md_wakeup_thread(mddev->thread);
344eb315cd0SGuoqing Jiang 			}
3454b26a08aSGoldwyn Rodrigues 		}
346e94987dbSGoldwyn Rodrigues clear_bit:
3474ac7a65fSShaohua Li 		lockres_free(bm_lockres);
348e94987dbSGoldwyn Rodrigues 		clear_bit(slot, &cinfo->recovery_map);
349e94987dbSGoldwyn Rodrigues 	}
350e94987dbSGoldwyn Rodrigues }
351e94987dbSGoldwyn Rodrigues 
recover_prep(void * arg)352cf921cc1SGoldwyn Rodrigues static void recover_prep(void *arg)
353cf921cc1SGoldwyn Rodrigues {
35490382ed9SGoldwyn Rodrigues 	struct mddev *mddev = arg;
35590382ed9SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
35690382ed9SGoldwyn Rodrigues 	set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
357cf921cc1SGoldwyn Rodrigues }
358cf921cc1SGoldwyn Rodrigues 
__recover_slot(struct mddev * mddev,int slot)35905cd0e51SGuoqing Jiang static void __recover_slot(struct mddev *mddev, int slot)
360cf921cc1SGoldwyn Rodrigues {
361cf921cc1SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
362cf921cc1SGoldwyn Rodrigues 
36305cd0e51SGuoqing Jiang 	set_bit(slot, &cinfo->recovery_map);
364e94987dbSGoldwyn Rodrigues 	if (!cinfo->recovery_thread) {
36544693154SYu Kuai 		rcu_assign_pointer(cinfo->recovery_thread,
36644693154SYu Kuai 			md_register_thread(recover_bitmaps, mddev, "recover"));
367e94987dbSGoldwyn Rodrigues 		if (!cinfo->recovery_thread) {
368e94987dbSGoldwyn Rodrigues 			pr_warn("md-cluster: Could not create recovery thread\n");
369e94987dbSGoldwyn Rodrigues 			return;
370e94987dbSGoldwyn Rodrigues 		}
371e94987dbSGoldwyn Rodrigues 	}
372e94987dbSGoldwyn Rodrigues 	md_wakeup_thread(cinfo->recovery_thread);
373cf921cc1SGoldwyn Rodrigues }
374cf921cc1SGoldwyn Rodrigues 
recover_slot(void * arg,struct dlm_slot * slot)37505cd0e51SGuoqing Jiang static void recover_slot(void *arg, struct dlm_slot *slot)
37605cd0e51SGuoqing Jiang {
37705cd0e51SGuoqing Jiang 	struct mddev *mddev = arg;
37805cd0e51SGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
37905cd0e51SGuoqing Jiang 
38005cd0e51SGuoqing Jiang 	pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
38105cd0e51SGuoqing Jiang 			mddev->bitmap_info.cluster_name,
38205cd0e51SGuoqing Jiang 			slot->nodeid, slot->slot,
38305cd0e51SGuoqing Jiang 			cinfo->slot_number);
38405cd0e51SGuoqing Jiang 	/* deduct one since dlm slot starts from one while the num of
38505cd0e51SGuoqing Jiang 	 * cluster-md begins with 0 */
38605cd0e51SGuoqing Jiang 	__recover_slot(mddev, slot->slot - 1);
38705cd0e51SGuoqing Jiang }
38805cd0e51SGuoqing Jiang 
recover_done(void * arg,struct dlm_slot * slots,int num_slots,int our_slot,uint32_t generation)389cf921cc1SGoldwyn Rodrigues static void recover_done(void *arg, struct dlm_slot *slots,
390cf921cc1SGoldwyn Rodrigues 		int num_slots, int our_slot,
391cf921cc1SGoldwyn Rodrigues 		uint32_t generation)
392cf921cc1SGoldwyn Rodrigues {
393cf921cc1SGoldwyn Rodrigues 	struct mddev *mddev = arg;
394cf921cc1SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
395cf921cc1SGoldwyn Rodrigues 
396cf921cc1SGoldwyn Rodrigues 	cinfo->slot_number = our_slot;
397eece075cSGuoqing Jiang 	/* completion is only need to be complete when node join cluster,
398eece075cSGuoqing Jiang 	 * it doesn't need to run during another node's failure */
399eece075cSGuoqing Jiang 	if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
400cf921cc1SGoldwyn Rodrigues 		complete(&cinfo->completion);
401eece075cSGuoqing Jiang 		clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
402eece075cSGuoqing Jiang 	}
40390382ed9SGoldwyn Rodrigues 	clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
404cf921cc1SGoldwyn Rodrigues }
405cf921cc1SGoldwyn Rodrigues 
406eece075cSGuoqing Jiang /* the ops is called when node join the cluster, and do lock recovery
407eece075cSGuoqing Jiang  * if node failure occurs */
408cf921cc1SGoldwyn Rodrigues static const struct dlm_lockspace_ops md_ls_ops = {
409cf921cc1SGoldwyn Rodrigues 	.recover_prep = recover_prep,
410cf921cc1SGoldwyn Rodrigues 	.recover_slot = recover_slot,
411cf921cc1SGoldwyn Rodrigues 	.recover_done = recover_done,
412cf921cc1SGoldwyn Rodrigues };
413cf921cc1SGoldwyn Rodrigues 
4144664680cSGoldwyn Rodrigues /*
4154664680cSGoldwyn Rodrigues  * The BAST function for the ack lock resource
4164664680cSGoldwyn Rodrigues  * This function wakes up the receive thread in
4174664680cSGoldwyn Rodrigues  * order to receive and process the message.
4184664680cSGoldwyn Rodrigues  */
ack_bast(void * arg,int mode)4194664680cSGoldwyn Rodrigues static void ack_bast(void *arg, int mode)
4204664680cSGoldwyn Rodrigues {
4212e2a7cd9SNeilBrown 	struct dlm_lock_resource *res = arg;
4224664680cSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = res->mddev->cluster_info;
4234664680cSGoldwyn Rodrigues 
42451e453aeSGuoqing Jiang 	if (mode == DLM_LOCK_EX) {
42551e453aeSGuoqing Jiang 		if (test_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state))
4264664680cSGoldwyn Rodrigues 			md_wakeup_thread(cinfo->recv_thread);
42751e453aeSGuoqing Jiang 		else
42851e453aeSGuoqing Jiang 			set_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state);
42951e453aeSGuoqing Jiang 	}
4304664680cSGoldwyn Rodrigues }
4314664680cSGoldwyn Rodrigues 
remove_suspend_info(struct mddev * mddev,int slot)432b8ca846eSGoldwyn Rodrigues static void remove_suspend_info(struct mddev *mddev, int slot)
433e59721ccSGoldwyn Rodrigues {
434b8ca846eSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
435b03e0ccbSNeilBrown 	mddev->pers->quiesce(mddev, 1);
436e59721ccSGoldwyn Rodrigues 	spin_lock_irq(&cinfo->suspend_lock);
437ea89238cSGuoqing Jiang 	cinfo->suspend_hi = 0;
438ea89238cSGuoqing Jiang 	cinfo->suspend_lo = 0;
439e59721ccSGoldwyn Rodrigues 	spin_unlock_irq(&cinfo->suspend_lock);
440b03e0ccbSNeilBrown 	mddev->pers->quiesce(mddev, 0);
441e59721ccSGoldwyn Rodrigues }
442e59721ccSGoldwyn Rodrigues 
process_suspend_info(struct mddev * mddev,int slot,sector_t lo,sector_t hi)4439ed38ff5SGoldwyn Rodrigues static void process_suspend_info(struct mddev *mddev,
444e59721ccSGoldwyn Rodrigues 		int slot, sector_t lo, sector_t hi)
445e59721ccSGoldwyn Rodrigues {
4469ed38ff5SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
447cbce6863SGuoqing Jiang 	struct mdp_superblock_1 *sb = NULL;
448cbce6863SGuoqing Jiang 	struct md_rdev *rdev;
449e59721ccSGoldwyn Rodrigues 
450e59721ccSGoldwyn Rodrigues 	if (!hi) {
4510357ba27SGuoqing Jiang 		/*
4520357ba27SGuoqing Jiang 		 * clear the REMOTE flag since resync or recovery is finished
4530357ba27SGuoqing Jiang 		 * in remote node.
4540357ba27SGuoqing Jiang 		 */
4550357ba27SGuoqing Jiang 		clear_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
456b8ca846eSGoldwyn Rodrigues 		remove_suspend_info(mddev, slot);
457c186b128SGoldwyn Rodrigues 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
458c186b128SGoldwyn Rodrigues 		md_wakeup_thread(mddev->thread);
459e59721ccSGoldwyn Rodrigues 		return;
460e59721ccSGoldwyn Rodrigues 	}
46118c9ff7fSGuoqing Jiang 
462cbce6863SGuoqing Jiang 	rdev_for_each(rdev, mddev)
463cbce6863SGuoqing Jiang 		if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
464cbce6863SGuoqing Jiang 			sb = page_address(rdev->sb_page);
465cbce6863SGuoqing Jiang 			break;
466cbce6863SGuoqing Jiang 		}
467cbce6863SGuoqing Jiang 
46818c9ff7fSGuoqing Jiang 	/*
46918c9ff7fSGuoqing Jiang 	 * The bitmaps are not same for different nodes
47018c9ff7fSGuoqing Jiang 	 * if RESYNCING is happening in one node, then
47118c9ff7fSGuoqing Jiang 	 * the node which received the RESYNCING message
47218c9ff7fSGuoqing Jiang 	 * probably will perform resync with the region
47318c9ff7fSGuoqing Jiang 	 * [lo, hi] again, so we could reduce resync time
47418c9ff7fSGuoqing Jiang 	 * a lot if we can ensure that the bitmaps among
47518c9ff7fSGuoqing Jiang 	 * different nodes are match up well.
47618c9ff7fSGuoqing Jiang 	 *
47718c9ff7fSGuoqing Jiang 	 * sync_low/hi is used to record the region which
47818c9ff7fSGuoqing Jiang 	 * arrived in the previous RESYNCING message,
47918c9ff7fSGuoqing Jiang 	 *
480cbce6863SGuoqing Jiang 	 * Call md_bitmap_sync_with_cluster to clear NEEDED_MASK
481cbce6863SGuoqing Jiang 	 * and set RESYNC_MASK since  resync thread is running
482cbce6863SGuoqing Jiang 	 * in another node, so we don't need to do the resync
483cbce6863SGuoqing Jiang 	 * again with the same section.
484cbce6863SGuoqing Jiang 	 *
485cbce6863SGuoqing Jiang 	 * Skip md_bitmap_sync_with_cluster in case reshape
486cbce6863SGuoqing Jiang 	 * happening, because reshaping region is small and
487cbce6863SGuoqing Jiang 	 * we don't want to trigger lots of WARN.
488cbce6863SGuoqing Jiang 	 */
489cbce6863SGuoqing Jiang 	if (sb && !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE))
490cbce6863SGuoqing Jiang 		md_bitmap_sync_with_cluster(mddev, cinfo->sync_low,
491cbce6863SGuoqing Jiang 					    cinfo->sync_hi, lo, hi);
49218c9ff7fSGuoqing Jiang 	cinfo->sync_low = lo;
49318c9ff7fSGuoqing Jiang 	cinfo->sync_hi = hi;
49418c9ff7fSGuoqing Jiang 
4959ed38ff5SGoldwyn Rodrigues 	mddev->pers->quiesce(mddev, 1);
496e59721ccSGoldwyn Rodrigues 	spin_lock_irq(&cinfo->suspend_lock);
497ea89238cSGuoqing Jiang 	cinfo->suspend_from = slot;
498ea89238cSGuoqing Jiang 	cinfo->suspend_lo = lo;
499ea89238cSGuoqing Jiang 	cinfo->suspend_hi = hi;
500e59721ccSGoldwyn Rodrigues 	spin_unlock_irq(&cinfo->suspend_lock);
501b03e0ccbSNeilBrown 	mddev->pers->quiesce(mddev, 0);
502e59721ccSGoldwyn Rodrigues }
503e59721ccSGoldwyn Rodrigues 
process_add_new_disk(struct mddev * mddev,struct cluster_msg * cmsg)5041aee41f6SGoldwyn Rodrigues static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
5051aee41f6SGoldwyn Rodrigues {
5061aee41f6SGoldwyn Rodrigues 	char disk_uuid[64];
5071aee41f6SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
5081aee41f6SGoldwyn Rodrigues 	char event_name[] = "EVENT=ADD_DEVICE";
5091aee41f6SGoldwyn Rodrigues 	char raid_slot[16];
5101aee41f6SGoldwyn Rodrigues 	char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
5111aee41f6SGoldwyn Rodrigues 	int len;
5121aee41f6SGoldwyn Rodrigues 
5131aee41f6SGoldwyn Rodrigues 	len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
514b89f704aSGuoqing Jiang 	sprintf(disk_uuid + len, "%pU", cmsg->uuid);
515faeff83fSGuoqing Jiang 	snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
5161aee41f6SGoldwyn Rodrigues 	pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
5171aee41f6SGoldwyn Rodrigues 	init_completion(&cinfo->newdisk_completion);
518fa8259daSGoldwyn Rodrigues 	set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
5191aee41f6SGoldwyn Rodrigues 	kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
5201aee41f6SGoldwyn Rodrigues 	wait_for_completion_timeout(&cinfo->newdisk_completion,
5211aee41f6SGoldwyn Rodrigues 			NEW_DEV_TIMEOUT);
522fa8259daSGoldwyn Rodrigues 	clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
5231aee41f6SGoldwyn Rodrigues }
5241aee41f6SGoldwyn Rodrigues 
5251aee41f6SGoldwyn Rodrigues 
process_metadata_update(struct mddev * mddev,struct cluster_msg * msg)5261aee41f6SGoldwyn Rodrigues static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
5271aee41f6SGoldwyn Rodrigues {
5280ba95977SGuoqing Jiang 	int got_lock = 0;
52944693154SYu Kuai 	struct md_thread *thread;
5301aee41f6SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
53115858fa5SGuoqing Jiang 	mddev->good_device_nr = le32_to_cpu(msg->raid_slot);
5320ba95977SGuoqing Jiang 
5331aee41f6SGoldwyn Rodrigues 	dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
53444693154SYu Kuai 
53544693154SYu Kuai 	/* daemaon thread must exist */
53644693154SYu Kuai 	thread = rcu_dereference_protected(mddev->thread, true);
53744693154SYu Kuai 	wait_event(thread->wqueue,
5380ba95977SGuoqing Jiang 		   (got_lock = mddev_trylock(mddev)) ||
5390ba95977SGuoqing Jiang 		    test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state));
5400ba95977SGuoqing Jiang 	md_reload_sb(mddev, mddev->good_device_nr);
5410ba95977SGuoqing Jiang 	if (got_lock)
5420ba95977SGuoqing Jiang 		mddev_unlock(mddev);
5431aee41f6SGoldwyn Rodrigues }
5441aee41f6SGoldwyn Rodrigues 
process_remove_disk(struct mddev * mddev,struct cluster_msg * msg)54588bcfef7SGoldwyn Rodrigues static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
54688bcfef7SGoldwyn Rodrigues {
5475f0aa21dSGuoqing Jiang 	struct md_rdev *rdev;
54888bcfef7SGoldwyn Rodrigues 
5495f0aa21dSGuoqing Jiang 	rcu_read_lock();
5505f0aa21dSGuoqing Jiang 	rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
551659b254fSGuoqing Jiang 	if (rdev) {
552659b254fSGuoqing Jiang 		set_bit(ClusterRemove, &rdev->flags);
553659b254fSGuoqing Jiang 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
554659b254fSGuoqing Jiang 		md_wakeup_thread(mddev->thread);
555659b254fSGuoqing Jiang 	}
55688bcfef7SGoldwyn Rodrigues 	else
557faeff83fSGuoqing Jiang 		pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
558faeff83fSGuoqing Jiang 			__func__, __LINE__, le32_to_cpu(msg->raid_slot));
5595f0aa21dSGuoqing Jiang 	rcu_read_unlock();
56088bcfef7SGoldwyn Rodrigues }
56188bcfef7SGoldwyn Rodrigues 
process_readd_disk(struct mddev * mddev,struct cluster_msg * msg)56297f6cd39SGoldwyn Rodrigues static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
56397f6cd39SGoldwyn Rodrigues {
5645f0aa21dSGuoqing Jiang 	struct md_rdev *rdev;
56597f6cd39SGoldwyn Rodrigues 
5665f0aa21dSGuoqing Jiang 	rcu_read_lock();
5675f0aa21dSGuoqing Jiang 	rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
56897f6cd39SGoldwyn Rodrigues 	if (rdev && test_bit(Faulty, &rdev->flags))
56997f6cd39SGoldwyn Rodrigues 		clear_bit(Faulty, &rdev->flags);
57097f6cd39SGoldwyn Rodrigues 	else
571faeff83fSGuoqing Jiang 		pr_warn("%s: %d Could not find disk(%d) which is faulty",
572faeff83fSGuoqing Jiang 			__func__, __LINE__, le32_to_cpu(msg->raid_slot));
5735f0aa21dSGuoqing Jiang 	rcu_read_unlock();
57497f6cd39SGoldwyn Rodrigues }
57597f6cd39SGoldwyn Rodrigues 
process_recvd_msg(struct mddev * mddev,struct cluster_msg * msg)5761fa9a1adSGuoqing Jiang static int process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
5774664680cSGoldwyn Rodrigues {
5781fa9a1adSGuoqing Jiang 	int ret = 0;
5791fa9a1adSGuoqing Jiang 
580256f5b24SGuoqing Jiang 	if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
581dd3dc5f4SRandy Dunlap 		"node %d received its own msg\n", le32_to_cpu(msg->slot)))
5821fa9a1adSGuoqing Jiang 		return -1;
583cf97a348SGuoqing Jiang 	switch (le32_to_cpu(msg->type)) {
5844664680cSGoldwyn Rodrigues 	case METADATA_UPDATED:
5851aee41f6SGoldwyn Rodrigues 		process_metadata_update(mddev, msg);
5864664680cSGoldwyn Rodrigues 		break;
5877da3d203SGuoqing Jiang 	case CHANGE_CAPACITY:
5882c247c51SChristoph Hellwig 		set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
5897da3d203SGuoqing Jiang 		break;
5904664680cSGoldwyn Rodrigues 	case RESYNCING:
5910357ba27SGuoqing Jiang 		set_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
592cf97a348SGuoqing Jiang 		process_suspend_info(mddev, le32_to_cpu(msg->slot),
593cf97a348SGuoqing Jiang 				     le64_to_cpu(msg->low),
594cf97a348SGuoqing Jiang 				     le64_to_cpu(msg->high));
5954664680cSGoldwyn Rodrigues 		break;
5961aee41f6SGoldwyn Rodrigues 	case NEWDISK:
5971aee41f6SGoldwyn Rodrigues 		process_add_new_disk(mddev, msg);
59888bcfef7SGoldwyn Rodrigues 		break;
59988bcfef7SGoldwyn Rodrigues 	case REMOVE:
60088bcfef7SGoldwyn Rodrigues 		process_remove_disk(mddev, msg);
60188bcfef7SGoldwyn Rodrigues 		break;
60297f6cd39SGoldwyn Rodrigues 	case RE_ADD:
60397f6cd39SGoldwyn Rodrigues 		process_readd_disk(mddev, msg);
60497f6cd39SGoldwyn Rodrigues 		break;
605dc737d7cSGuoqing Jiang 	case BITMAP_NEEDS_SYNC:
606cf97a348SGuoqing Jiang 		__recover_slot(mddev, le32_to_cpu(msg->slot));
607dc737d7cSGuoqing Jiang 		break;
608afd75628SGuoqing Jiang 	case BITMAP_RESIZE:
609afd75628SGuoqing Jiang 		if (le64_to_cpu(msg->high) != mddev->pers->size(mddev, 0, 0))
610afd75628SGuoqing Jiang 			ret = md_bitmap_resize(mddev->bitmap,
611afd75628SGuoqing Jiang 					    le64_to_cpu(msg->high), 0, 0);
612afd75628SGuoqing Jiang 		break;
61388bcfef7SGoldwyn Rodrigues 	default:
6141fa9a1adSGuoqing Jiang 		ret = -1;
61588bcfef7SGoldwyn Rodrigues 		pr_warn("%s:%d Received unknown message from %d\n",
61688bcfef7SGoldwyn Rodrigues 			__func__, __LINE__, msg->slot);
61709dd1af2Skbuild test robot 	}
6181fa9a1adSGuoqing Jiang 	return ret;
6194664680cSGoldwyn Rodrigues }
6204664680cSGoldwyn Rodrigues 
6214664680cSGoldwyn Rodrigues /*
6224664680cSGoldwyn Rodrigues  * thread for receiving message
6234664680cSGoldwyn Rodrigues  */
recv_daemon(struct md_thread * thread)6244664680cSGoldwyn Rodrigues static void recv_daemon(struct md_thread *thread)
6254664680cSGoldwyn Rodrigues {
6264664680cSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = thread->mddev->cluster_info;
6274664680cSGoldwyn Rodrigues 	struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
6284664680cSGoldwyn Rodrigues 	struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
6294664680cSGoldwyn Rodrigues 	struct cluster_msg msg;
630b5ef5678SGuoqing Jiang 	int ret;
6314664680cSGoldwyn Rodrigues 
6328b9277c8SGuoqing Jiang 	mutex_lock(&cinfo->recv_mutex);
6334664680cSGoldwyn Rodrigues 	/*get CR on Message*/
6344664680cSGoldwyn Rodrigues 	if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
6354664680cSGoldwyn Rodrigues 		pr_err("md/raid1:failed to get CR on MESSAGE\n");
6368b9277c8SGuoqing Jiang 		mutex_unlock(&cinfo->recv_mutex);
6374664680cSGoldwyn Rodrigues 		return;
6384664680cSGoldwyn Rodrigues 	}
6394664680cSGoldwyn Rodrigues 
6404664680cSGoldwyn Rodrigues 	/* read lvb and wake up thread to process this message_lockres */
6414664680cSGoldwyn Rodrigues 	memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
6421fa9a1adSGuoqing Jiang 	ret = process_recvd_msg(thread->mddev, &msg);
6431fa9a1adSGuoqing Jiang 	if (ret)
6441fa9a1adSGuoqing Jiang 		goto out;
6454664680cSGoldwyn Rodrigues 
6464664680cSGoldwyn Rodrigues 	/*release CR on ack_lockres*/
647b5ef5678SGuoqing Jiang 	ret = dlm_unlock_sync(ack_lockres);
648b5ef5678SGuoqing Jiang 	if (unlikely(ret != 0))
649b5ef5678SGuoqing Jiang 		pr_info("unlock ack failed return %d\n", ret);
65066099bb0SGuoqing Jiang 	/*up-convert to PR on message_lockres*/
651b5ef5678SGuoqing Jiang 	ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
652b5ef5678SGuoqing Jiang 	if (unlikely(ret != 0))
653b5ef5678SGuoqing Jiang 		pr_info("lock PR on msg failed return %d\n", ret);
6544664680cSGoldwyn Rodrigues 	/*get CR on ack_lockres again*/
655b5ef5678SGuoqing Jiang 	ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
656b5ef5678SGuoqing Jiang 	if (unlikely(ret != 0))
657b5ef5678SGuoqing Jiang 		pr_info("lock CR on ack failed return %d\n", ret);
6581fa9a1adSGuoqing Jiang out:
6594664680cSGoldwyn Rodrigues 	/*release CR on message_lockres*/
660b5ef5678SGuoqing Jiang 	ret = dlm_unlock_sync(message_lockres);
661b5ef5678SGuoqing Jiang 	if (unlikely(ret != 0))
662b5ef5678SGuoqing Jiang 		pr_info("unlock msg failed return %d\n", ret);
6638b9277c8SGuoqing Jiang 	mutex_unlock(&cinfo->recv_mutex);
6644664680cSGoldwyn Rodrigues }
6654664680cSGoldwyn Rodrigues 
6668b9277c8SGuoqing Jiang /* lock_token()
667601b515cSGoldwyn Rodrigues  * Takes the lock on the TOKEN lock resource so no other
668601b515cSGoldwyn Rodrigues  * node can communicate while the operation is underway.
669601b515cSGoldwyn Rodrigues  */
lock_token(struct md_cluster_info * cinfo)670bca5b065SZhao Heming static int lock_token(struct md_cluster_info *cinfo)
671601b515cSGoldwyn Rodrigues {
672bca5b065SZhao Heming 	int error;
673bca5b065SZhao Heming 
674bca5b065SZhao Heming 	error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
675bca5b065SZhao Heming 	if (error) {
676bca5b065SZhao Heming 		pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
677bca5b065SZhao Heming 				__func__, __LINE__, error);
678bca5b065SZhao Heming 	} else {
679bca5b065SZhao Heming 		/* Lock the receive sequence */
680bca5b065SZhao Heming 		mutex_lock(&cinfo->recv_mutex);
681bca5b065SZhao Heming 	}
682bca5b065SZhao Heming 	return error;
683bca5b065SZhao Heming }
684bca5b065SZhao Heming 
685bca5b065SZhao Heming /* lock_comm()
686bca5b065SZhao Heming  * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
687bca5b065SZhao Heming  */
lock_comm(struct md_cluster_info * cinfo,bool mddev_locked)688bca5b065SZhao Heming static int lock_comm(struct md_cluster_info *cinfo, bool mddev_locked)
689bca5b065SZhao Heming {
690bca5b065SZhao Heming 	int rv, set_bit = 0;
6910ba95977SGuoqing Jiang 	struct mddev *mddev = cinfo->mddev;
692601b515cSGoldwyn Rodrigues 
6930ba95977SGuoqing Jiang 	/*
6940ba95977SGuoqing Jiang 	 * If resync thread run after raid1d thread, then process_metadata_update
6950ba95977SGuoqing Jiang 	 * could not continue if raid1d held reconfig_mutex (and raid1d is blocked
6969e26728bSZhang Jiaming 	 * since another node already got EX on Token and waiting the EX of Ack),
6970ba95977SGuoqing Jiang 	 * so let resync wake up thread in case flag is set.
6980ba95977SGuoqing Jiang 	 */
6990ba95977SGuoqing Jiang 	if (mddev_locked && !test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
7000ba95977SGuoqing Jiang 				      &cinfo->state)) {
701bca5b065SZhao Heming 		rv = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
7020ba95977SGuoqing Jiang 					      &cinfo->state);
703bca5b065SZhao Heming 		WARN_ON_ONCE(rv);
7040ba95977SGuoqing Jiang 		md_wakeup_thread(mddev->thread);
7050ba95977SGuoqing Jiang 		set_bit = 1;
7060ba95977SGuoqing Jiang 	}
7070ba95977SGuoqing Jiang 
7088b9277c8SGuoqing Jiang 	wait_event(cinfo->wait,
7098b9277c8SGuoqing Jiang 		   !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));
710bca5b065SZhao Heming 	rv = lock_token(cinfo);
711bca5b065SZhao Heming 	if (set_bit)
712bca5b065SZhao Heming 		clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
713bca5b065SZhao Heming 	return rv;
7148b9277c8SGuoqing Jiang }
7158b9277c8SGuoqing Jiang 
unlock_comm(struct md_cluster_info * cinfo)716601b515cSGoldwyn Rodrigues static void unlock_comm(struct md_cluster_info *cinfo)
717601b515cSGoldwyn Rodrigues {
718dbb64f86SGoldwyn Rodrigues 	WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
7198b9277c8SGuoqing Jiang 	mutex_unlock(&cinfo->recv_mutex);
720601b515cSGoldwyn Rodrigues 	dlm_unlock_sync(cinfo->token_lockres);
7218b9277c8SGuoqing Jiang 	clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
7228b9277c8SGuoqing Jiang 	wake_up(&cinfo->wait);
723601b515cSGoldwyn Rodrigues }
724601b515cSGoldwyn Rodrigues 
725601b515cSGoldwyn Rodrigues /* __sendmsg()
726601b515cSGoldwyn Rodrigues  * This function performs the actual sending of the message. This function is
727601b515cSGoldwyn Rodrigues  * usually called after performing the encompassing operation
728601b515cSGoldwyn Rodrigues  * The function:
729601b515cSGoldwyn Rodrigues  * 1. Grabs the message lockresource in EX mode
730601b515cSGoldwyn Rodrigues  * 2. Copies the message to the message LVB
73166099bb0SGuoqing Jiang  * 3. Downconverts message lockresource to CW
732601b515cSGoldwyn Rodrigues  * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
733601b515cSGoldwyn Rodrigues  *    and the other nodes read the message. The thread will wait here until all other
734601b515cSGoldwyn Rodrigues  *    nodes have released ack lock resource.
735601b515cSGoldwyn Rodrigues  * 5. Downconvert ack lockresource to CR
736601b515cSGoldwyn Rodrigues  */
__sendmsg(struct md_cluster_info * cinfo,struct cluster_msg * cmsg)737601b515cSGoldwyn Rodrigues static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
738601b515cSGoldwyn Rodrigues {
739601b515cSGoldwyn Rodrigues 	int error;
740601b515cSGoldwyn Rodrigues 	int slot = cinfo->slot_number - 1;
741601b515cSGoldwyn Rodrigues 
742601b515cSGoldwyn Rodrigues 	cmsg->slot = cpu_to_le32(slot);
743601b515cSGoldwyn Rodrigues 	/*get EX on Message*/
744601b515cSGoldwyn Rodrigues 	error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
745601b515cSGoldwyn Rodrigues 	if (error) {
746601b515cSGoldwyn Rodrigues 		pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
747601b515cSGoldwyn Rodrigues 		goto failed_message;
748601b515cSGoldwyn Rodrigues 	}
749601b515cSGoldwyn Rodrigues 
750601b515cSGoldwyn Rodrigues 	memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
751601b515cSGoldwyn Rodrigues 			sizeof(struct cluster_msg));
75266099bb0SGuoqing Jiang 	/*down-convert EX to CW on Message*/
75366099bb0SGuoqing Jiang 	error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
754601b515cSGoldwyn Rodrigues 	if (error) {
75566099bb0SGuoqing Jiang 		pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
756601b515cSGoldwyn Rodrigues 				error);
75766099bb0SGuoqing Jiang 		goto failed_ack;
758601b515cSGoldwyn Rodrigues 	}
759601b515cSGoldwyn Rodrigues 
760601b515cSGoldwyn Rodrigues 	/*up-convert CR to EX on Ack*/
761601b515cSGoldwyn Rodrigues 	error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
762601b515cSGoldwyn Rodrigues 	if (error) {
763601b515cSGoldwyn Rodrigues 		pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
764601b515cSGoldwyn Rodrigues 				error);
765601b515cSGoldwyn Rodrigues 		goto failed_ack;
766601b515cSGoldwyn Rodrigues 	}
767601b515cSGoldwyn Rodrigues 
768601b515cSGoldwyn Rodrigues 	/*down-convert EX to CR on Ack*/
769601b515cSGoldwyn Rodrigues 	error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
770601b515cSGoldwyn Rodrigues 	if (error) {
771601b515cSGoldwyn Rodrigues 		pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
772601b515cSGoldwyn Rodrigues 				error);
773601b515cSGoldwyn Rodrigues 		goto failed_ack;
774601b515cSGoldwyn Rodrigues 	}
775601b515cSGoldwyn Rodrigues 
776601b515cSGoldwyn Rodrigues failed_ack:
777b5ef5678SGuoqing Jiang 	error = dlm_unlock_sync(cinfo->message_lockres);
778b5ef5678SGuoqing Jiang 	if (unlikely(error != 0)) {
779b5ef5678SGuoqing Jiang 		pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
780b5ef5678SGuoqing Jiang 			error);
781b5ef5678SGuoqing Jiang 		/* in case the message can't be released due to some reason */
782b5ef5678SGuoqing Jiang 		goto failed_ack;
783b5ef5678SGuoqing Jiang 	}
784601b515cSGoldwyn Rodrigues failed_message:
785601b515cSGoldwyn Rodrigues 	return error;
786601b515cSGoldwyn Rodrigues }
787601b515cSGoldwyn Rodrigues 
sendmsg(struct md_cluster_info * cinfo,struct cluster_msg * cmsg,bool mddev_locked)7880ba95977SGuoqing Jiang static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg,
7890ba95977SGuoqing Jiang 		   bool mddev_locked)
790601b515cSGoldwyn Rodrigues {
791601b515cSGoldwyn Rodrigues 	int ret;
792601b515cSGoldwyn Rodrigues 
793bca5b065SZhao Heming 	ret = lock_comm(cinfo, mddev_locked);
794bca5b065SZhao Heming 	if (!ret) {
795601b515cSGoldwyn Rodrigues 		ret = __sendmsg(cinfo, cmsg);
796601b515cSGoldwyn Rodrigues 		unlock_comm(cinfo);
797bca5b065SZhao Heming 	}
798601b515cSGoldwyn Rodrigues 	return ret;
799601b515cSGoldwyn Rodrigues }
800601b515cSGoldwyn Rodrigues 
gather_all_resync_info(struct mddev * mddev,int total_slots)80196ae923aSGoldwyn Rodrigues static int gather_all_resync_info(struct mddev *mddev, int total_slots)
80296ae923aSGoldwyn Rodrigues {
80396ae923aSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
80496ae923aSGoldwyn Rodrigues 	int i, ret = 0;
80596ae923aSGoldwyn Rodrigues 	struct dlm_lock_resource *bm_lockres;
80696ae923aSGoldwyn Rodrigues 	char str[64];
807abb9b22aSGuoqing Jiang 	sector_t lo, hi;
80896ae923aSGoldwyn Rodrigues 
80996ae923aSGoldwyn Rodrigues 
81096ae923aSGoldwyn Rodrigues 	for (i = 0; i < total_slots; i++) {
81196ae923aSGoldwyn Rodrigues 		memset(str, '\0', 64);
81296ae923aSGoldwyn Rodrigues 		snprintf(str, 64, "bitmap%04d", i);
81396ae923aSGoldwyn Rodrigues 		bm_lockres = lockres_init(mddev, str, NULL, 1);
81496ae923aSGoldwyn Rodrigues 		if (!bm_lockres)
81596ae923aSGoldwyn Rodrigues 			return -ENOMEM;
8164ac7a65fSShaohua Li 		if (i == (cinfo->slot_number - 1)) {
8174ac7a65fSShaohua Li 			lockres_free(bm_lockres);
81896ae923aSGoldwyn Rodrigues 			continue;
8194ac7a65fSShaohua Li 		}
82096ae923aSGoldwyn Rodrigues 
82196ae923aSGoldwyn Rodrigues 		bm_lockres->flags |= DLM_LKF_NOQUEUE;
82296ae923aSGoldwyn Rodrigues 		ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
82396ae923aSGoldwyn Rodrigues 		if (ret == -EAGAIN) {
824ea89238cSGuoqing Jiang 			if (read_resync_info(mddev, bm_lockres)) {
82596ae923aSGoldwyn Rodrigues 				pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
82696ae923aSGoldwyn Rodrigues 						__func__, __LINE__,
827ea89238cSGuoqing Jiang 					(unsigned long long) cinfo->suspend_lo,
828ea89238cSGuoqing Jiang 					(unsigned long long) cinfo->suspend_hi,
829ea89238cSGuoqing Jiang 					i);
830ea89238cSGuoqing Jiang 				cinfo->suspend_from = i;
83196ae923aSGoldwyn Rodrigues 			}
83296ae923aSGoldwyn Rodrigues 			ret = 0;
83396ae923aSGoldwyn Rodrigues 			lockres_free(bm_lockres);
83496ae923aSGoldwyn Rodrigues 			continue;
83596ae923aSGoldwyn Rodrigues 		}
8366e6d9f2cSGuoqing Jiang 		if (ret) {
8376e6d9f2cSGuoqing Jiang 			lockres_free(bm_lockres);
83896ae923aSGoldwyn Rodrigues 			goto out;
8396e6d9f2cSGuoqing Jiang 		}
840abb9b22aSGuoqing Jiang 
841abb9b22aSGuoqing Jiang 		/* Read the disk bitmap sb and check if it needs recovery */
842e64e4018SAndy Shevchenko 		ret = md_bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
843abb9b22aSGuoqing Jiang 		if (ret) {
844abb9b22aSGuoqing Jiang 			pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
845abb9b22aSGuoqing Jiang 			lockres_free(bm_lockres);
846abb9b22aSGuoqing Jiang 			continue;
847abb9b22aSGuoqing Jiang 		}
848abb9b22aSGuoqing Jiang 		if ((hi > 0) && (lo < mddev->recovery_cp)) {
849abb9b22aSGuoqing Jiang 			set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
850abb9b22aSGuoqing Jiang 			mddev->recovery_cp = lo;
851abb9b22aSGuoqing Jiang 			md_check_recovery(mddev);
852abb9b22aSGuoqing Jiang 		}
853abb9b22aSGuoqing Jiang 
85496ae923aSGoldwyn Rodrigues 		lockres_free(bm_lockres);
85596ae923aSGoldwyn Rodrigues 	}
85696ae923aSGoldwyn Rodrigues out:
85796ae923aSGoldwyn Rodrigues 	return ret;
85896ae923aSGoldwyn Rodrigues }
85996ae923aSGoldwyn Rodrigues 
join(struct mddev * mddev,int nodes)860edb39c9dSGoldwyn Rodrigues static int join(struct mddev *mddev, int nodes)
861edb39c9dSGoldwyn Rodrigues {
862c4ce867fSGoldwyn Rodrigues 	struct md_cluster_info *cinfo;
863cf921cc1SGoldwyn Rodrigues 	int ret, ops_rv;
864c4ce867fSGoldwyn Rodrigues 	char str[64];
865c4ce867fSGoldwyn Rodrigues 
866c4ce867fSGoldwyn Rodrigues 	cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
867c4ce867fSGoldwyn Rodrigues 	if (!cinfo)
868c4ce867fSGoldwyn Rodrigues 		return -ENOMEM;
869c4ce867fSGoldwyn Rodrigues 
8709e3072e3SGuoqing Jiang 	INIT_LIST_HEAD(&cinfo->suspend_list);
8719e3072e3SGuoqing Jiang 	spin_lock_init(&cinfo->suspend_lock);
872cf921cc1SGoldwyn Rodrigues 	init_completion(&cinfo->completion);
873eece075cSGuoqing Jiang 	set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
8748b9277c8SGuoqing Jiang 	init_waitqueue_head(&cinfo->wait);
8758b9277c8SGuoqing Jiang 	mutex_init(&cinfo->recv_mutex);
876cf921cc1SGoldwyn Rodrigues 
877cf921cc1SGoldwyn Rodrigues 	mddev->cluster_info = cinfo;
8780ba95977SGuoqing Jiang 	cinfo->mddev = mddev;
879cf921cc1SGoldwyn Rodrigues 
880c4ce867fSGoldwyn Rodrigues 	memset(str, 0, 64);
881b89f704aSGuoqing Jiang 	sprintf(str, "%pU", mddev->uuid);
882cf921cc1SGoldwyn Rodrigues 	ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
88312cda13cSAlexander Aring 				0, LVB_SIZE, &md_ls_ops, mddev,
88412cda13cSAlexander Aring 				&ops_rv, &cinfo->lockspace);
885c4ce867fSGoldwyn Rodrigues 	if (ret)
886c4ce867fSGoldwyn Rodrigues 		goto err;
887cf921cc1SGoldwyn Rodrigues 	wait_for_completion(&cinfo->completion);
8888c58f02eSGuoqing Jiang 	if (nodes < cinfo->slot_number) {
8898c58f02eSGuoqing Jiang 		pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
8908c58f02eSGuoqing Jiang 			cinfo->slot_number, nodes);
891b97e9257SGoldwyn Rodrigues 		ret = -ERANGE;
892b97e9257SGoldwyn Rodrigues 		goto err;
893b97e9257SGoldwyn Rodrigues 	}
8944664680cSGoldwyn Rodrigues 	/* Initiate the communication resources */
8954664680cSGoldwyn Rodrigues 	ret = -ENOMEM;
89644693154SYu Kuai 	rcu_assign_pointer(cinfo->recv_thread,
89744693154SYu Kuai 			md_register_thread(recv_daemon, mddev, "cluster_recv"));
8984664680cSGoldwyn Rodrigues 	if (!cinfo->recv_thread) {
8994664680cSGoldwyn Rodrigues 		pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
9004664680cSGoldwyn Rodrigues 		goto err;
9014664680cSGoldwyn Rodrigues 	}
9024664680cSGoldwyn Rodrigues 	cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
9034664680cSGoldwyn Rodrigues 	if (!cinfo->message_lockres)
9044664680cSGoldwyn Rodrigues 		goto err;
9054664680cSGoldwyn Rodrigues 	cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
9064664680cSGoldwyn Rodrigues 	if (!cinfo->token_lockres)
9074664680cSGoldwyn Rodrigues 		goto err;
9081aee41f6SGoldwyn Rodrigues 	cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
9091aee41f6SGoldwyn Rodrigues 	if (!cinfo->no_new_dev_lockres)
9101aee41f6SGoldwyn Rodrigues 		goto err;
9111aee41f6SGoldwyn Rodrigues 
9121535212cSGuoqing Jiang 	ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
9131535212cSGuoqing Jiang 	if (ret) {
9141535212cSGuoqing Jiang 		ret = -EAGAIN;
9151535212cSGuoqing Jiang 		pr_err("md-cluster: can't join cluster to avoid lock issue\n");
9161535212cSGuoqing Jiang 		goto err;
9171535212cSGuoqing Jiang 	}
9181535212cSGuoqing Jiang 	cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
9190f6187dbSWei Yongjun 	if (!cinfo->ack_lockres) {
9200f6187dbSWei Yongjun 		ret = -ENOMEM;
9211535212cSGuoqing Jiang 		goto err;
9220f6187dbSWei Yongjun 	}
9234664680cSGoldwyn Rodrigues 	/* get sync CR lock on ACK. */
9244664680cSGoldwyn Rodrigues 	if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
9254664680cSGoldwyn Rodrigues 		pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
9264664680cSGoldwyn Rodrigues 				ret);
9271535212cSGuoqing Jiang 	dlm_unlock_sync(cinfo->token_lockres);
9281aee41f6SGoldwyn Rodrigues 	/* get sync CR lock on no-new-dev. */
9291aee41f6SGoldwyn Rodrigues 	if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
9301aee41f6SGoldwyn Rodrigues 		pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
9311aee41f6SGoldwyn Rodrigues 
93254519c5fSGoldwyn Rodrigues 
93354519c5fSGoldwyn Rodrigues 	pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
93454519c5fSGoldwyn Rodrigues 	snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
93554519c5fSGoldwyn Rodrigues 	cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
9360f6187dbSWei Yongjun 	if (!cinfo->bitmap_lockres) {
9370f6187dbSWei Yongjun 		ret = -ENOMEM;
93854519c5fSGoldwyn Rodrigues 		goto err;
9390f6187dbSWei Yongjun 	}
94054519c5fSGoldwyn Rodrigues 	if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
94154519c5fSGoldwyn Rodrigues 		pr_err("Failed to get bitmap lock\n");
94254519c5fSGoldwyn Rodrigues 		ret = -EINVAL;
94354519c5fSGoldwyn Rodrigues 		goto err;
94454519c5fSGoldwyn Rodrigues 	}
94554519c5fSGoldwyn Rodrigues 
946c186b128SGoldwyn Rodrigues 	cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
9470f6187dbSWei Yongjun 	if (!cinfo->resync_lockres) {
9480f6187dbSWei Yongjun 		ret = -ENOMEM;
949c186b128SGoldwyn Rodrigues 		goto err;
9500f6187dbSWei Yongjun 	}
951c186b128SGoldwyn Rodrigues 
952edb39c9dSGoldwyn Rodrigues 	return 0;
953c4ce867fSGoldwyn Rodrigues err:
9540ba95977SGuoqing Jiang 	set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
955*7eb8ff02SLi Lingfeng 	md_unregister_thread(mddev, &cinfo->recovery_thread);
956*7eb8ff02SLi Lingfeng 	md_unregister_thread(mddev, &cinfo->recv_thread);
9574664680cSGoldwyn Rodrigues 	lockres_free(cinfo->message_lockres);
9584664680cSGoldwyn Rodrigues 	lockres_free(cinfo->token_lockres);
9594664680cSGoldwyn Rodrigues 	lockres_free(cinfo->ack_lockres);
9601aee41f6SGoldwyn Rodrigues 	lockres_free(cinfo->no_new_dev_lockres);
961c186b128SGoldwyn Rodrigues 	lockres_free(cinfo->resync_lockres);
96296ae923aSGoldwyn Rodrigues 	lockres_free(cinfo->bitmap_lockres);
963c4ce867fSGoldwyn Rodrigues 	if (cinfo->lockspace)
964c4ce867fSGoldwyn Rodrigues 		dlm_release_lockspace(cinfo->lockspace, 2);
965cf921cc1SGoldwyn Rodrigues 	mddev->cluster_info = NULL;
966c4ce867fSGoldwyn Rodrigues 	kfree(cinfo);
967c4ce867fSGoldwyn Rodrigues 	return ret;
968edb39c9dSGoldwyn Rodrigues }
969edb39c9dSGoldwyn Rodrigues 
load_bitmaps(struct mddev * mddev,int total_slots)97051e453aeSGuoqing Jiang static void load_bitmaps(struct mddev *mddev, int total_slots)
97151e453aeSGuoqing Jiang {
97251e453aeSGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
97351e453aeSGuoqing Jiang 
97451e453aeSGuoqing Jiang 	/* load all the node's bitmap info for resync */
97551e453aeSGuoqing Jiang 	if (gather_all_resync_info(mddev, total_slots))
97651e453aeSGuoqing Jiang 		pr_err("md-cluster: failed to gather all resyn infos\n");
97751e453aeSGuoqing Jiang 	set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state);
97851e453aeSGuoqing Jiang 	/* wake up recv thread in case something need to be handled */
97951e453aeSGuoqing Jiang 	if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
98051e453aeSGuoqing Jiang 		md_wakeup_thread(cinfo->recv_thread);
98151e453aeSGuoqing Jiang }
98251e453aeSGuoqing Jiang 
resync_bitmap(struct mddev * mddev)98309995411SGuoqing Jiang static void resync_bitmap(struct mddev *mddev)
98409995411SGuoqing Jiang {
98509995411SGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
98609995411SGuoqing Jiang 	struct cluster_msg cmsg = {0};
98709995411SGuoqing Jiang 	int err;
98809995411SGuoqing Jiang 
98909995411SGuoqing Jiang 	cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
9900ba95977SGuoqing Jiang 	err = sendmsg(cinfo, &cmsg, 1);
99109995411SGuoqing Jiang 	if (err)
99209995411SGuoqing Jiang 		pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
99309995411SGuoqing Jiang 			__func__, __LINE__, err);
99409995411SGuoqing Jiang }
99509995411SGuoqing Jiang 
996f6a2dc64SGuoqing Jiang static void unlock_all_bitmaps(struct mddev *mddev);
leave(struct mddev * mddev)997edb39c9dSGoldwyn Rodrigues static int leave(struct mddev *mddev)
998edb39c9dSGoldwyn Rodrigues {
999c4ce867fSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1000c4ce867fSGoldwyn Rodrigues 
1001c4ce867fSGoldwyn Rodrigues 	if (!cinfo)
1002c4ce867fSGoldwyn Rodrigues 		return 0;
100309995411SGuoqing Jiang 
1004cb9ee154SGuoqing Jiang 	/*
1005cb9ee154SGuoqing Jiang 	 * BITMAP_NEEDS_SYNC message should be sent when node
100609995411SGuoqing Jiang 	 * is leaving the cluster with dirty bitmap, also we
1007cb9ee154SGuoqing Jiang 	 * can only deliver it when dlm connection is available.
1008cb9ee154SGuoqing Jiang 	 *
1009cb9ee154SGuoqing Jiang 	 * Also, we should send BITMAP_NEEDS_SYNC message in
1010cb9ee154SGuoqing Jiang 	 * case reshaping is interrupted.
1011cb9ee154SGuoqing Jiang 	 */
1012cb9ee154SGuoqing Jiang 	if ((cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector) ||
1013cb9ee154SGuoqing Jiang 	    (mddev->reshape_position != MaxSector &&
1014cb9ee154SGuoqing Jiang 	     test_bit(MD_CLOSING, &mddev->flags)))
101509995411SGuoqing Jiang 		resync_bitmap(mddev);
101609995411SGuoqing Jiang 
10170ba95977SGuoqing Jiang 	set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
1018*7eb8ff02SLi Lingfeng 	md_unregister_thread(mddev, &cinfo->recovery_thread);
1019*7eb8ff02SLi Lingfeng 	md_unregister_thread(mddev, &cinfo->recv_thread);
10204664680cSGoldwyn Rodrigues 	lockres_free(cinfo->message_lockres);
10214664680cSGoldwyn Rodrigues 	lockres_free(cinfo->token_lockres);
10224664680cSGoldwyn Rodrigues 	lockres_free(cinfo->ack_lockres);
10231aee41f6SGoldwyn Rodrigues 	lockres_free(cinfo->no_new_dev_lockres);
10244ac7a65fSShaohua Li 	lockres_free(cinfo->resync_lockres);
102554519c5fSGoldwyn Rodrigues 	lockres_free(cinfo->bitmap_lockres);
1026f6a2dc64SGuoqing Jiang 	unlock_all_bitmaps(mddev);
1027c4ce867fSGoldwyn Rodrigues 	dlm_release_lockspace(cinfo->lockspace, 2);
10289c8043f3SGuoqing Jiang 	kfree(cinfo);
1029edb39c9dSGoldwyn Rodrigues 	return 0;
1030edb39c9dSGoldwyn Rodrigues }
1031edb39c9dSGoldwyn Rodrigues 
1032cf921cc1SGoldwyn Rodrigues /* slot_number(): Returns the MD slot number to use
1033cf921cc1SGoldwyn Rodrigues  * DLM starts the slot numbers from 1, wheras cluster-md
1034cf921cc1SGoldwyn Rodrigues  * wants the number to be from zero, so we deduct one
1035cf921cc1SGoldwyn Rodrigues  */
slot_number(struct mddev * mddev)1036cf921cc1SGoldwyn Rodrigues static int slot_number(struct mddev *mddev)
1037cf921cc1SGoldwyn Rodrigues {
1038cf921cc1SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1039cf921cc1SGoldwyn Rodrigues 
1040cf921cc1SGoldwyn Rodrigues 	return cinfo->slot_number - 1;
1041cf921cc1SGoldwyn Rodrigues }
1042cf921cc1SGoldwyn Rodrigues 
10438b9277c8SGuoqing Jiang /*
10448b9277c8SGuoqing Jiang  * Check if the communication is already locked, else lock the communication
10458b9277c8SGuoqing Jiang  * channel.
10468b9277c8SGuoqing Jiang  * If it is already locked, token is in EX mode, and hence lock_token()
10478b9277c8SGuoqing Jiang  * should not be called.
10488b9277c8SGuoqing Jiang  */
metadata_update_start(struct mddev * mddev)1049293467aaSGoldwyn Rodrigues static int metadata_update_start(struct mddev *mddev)
1050293467aaSGoldwyn Rodrigues {
10518b9277c8SGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
10520ba95977SGuoqing Jiang 	int ret;
10530ba95977SGuoqing Jiang 
10540ba95977SGuoqing Jiang 	/*
10550ba95977SGuoqing Jiang 	 * metadata_update_start is always called with the protection of
10560ba95977SGuoqing Jiang 	 * reconfig_mutex, so set WAITING_FOR_TOKEN here.
10570ba95977SGuoqing Jiang 	 */
10580ba95977SGuoqing Jiang 	ret = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
10590ba95977SGuoqing Jiang 				    &cinfo->state);
10600ba95977SGuoqing Jiang 	WARN_ON_ONCE(ret);
10610ba95977SGuoqing Jiang 	md_wakeup_thread(mddev->thread);
10628b9277c8SGuoqing Jiang 
10638b9277c8SGuoqing Jiang 	wait_event(cinfo->wait,
10648b9277c8SGuoqing Jiang 		   !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
10658b9277c8SGuoqing Jiang 		   test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
10668b9277c8SGuoqing Jiang 
10678b9277c8SGuoqing Jiang 	/* If token is already locked, return 0 */
10680ba95977SGuoqing Jiang 	if (cinfo->token_lockres->mode == DLM_LOCK_EX) {
10690ba95977SGuoqing Jiang 		clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
10708b9277c8SGuoqing Jiang 		return 0;
10710ba95977SGuoqing Jiang 	}
10728b9277c8SGuoqing Jiang 
1073bca5b065SZhao Heming 	ret = lock_token(cinfo);
10740ba95977SGuoqing Jiang 	clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
10750ba95977SGuoqing Jiang 	return ret;
1076293467aaSGoldwyn Rodrigues }
1077293467aaSGoldwyn Rodrigues 
metadata_update_finish(struct mddev * mddev)1078293467aaSGoldwyn Rodrigues static int metadata_update_finish(struct mddev *mddev)
1079293467aaSGoldwyn Rodrigues {
1080293467aaSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1081293467aaSGoldwyn Rodrigues 	struct cluster_msg cmsg;
108270bcecdbSGoldwyn Rodrigues 	struct md_rdev *rdev;
108370bcecdbSGoldwyn Rodrigues 	int ret = 0;
1084ba2746b0SNeilBrown 	int raid_slot = -1;
1085293467aaSGoldwyn Rodrigues 
1086293467aaSGoldwyn Rodrigues 	memset(&cmsg, 0, sizeof(cmsg));
1087293467aaSGoldwyn Rodrigues 	cmsg.type = cpu_to_le32(METADATA_UPDATED);
108870bcecdbSGoldwyn Rodrigues 	/* Pick up a good active device number to send.
108970bcecdbSGoldwyn Rodrigues 	 */
109070bcecdbSGoldwyn Rodrigues 	rdev_for_each(rdev, mddev)
109170bcecdbSGoldwyn Rodrigues 		if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
1092ba2746b0SNeilBrown 			raid_slot = rdev->desc_nr;
109370bcecdbSGoldwyn Rodrigues 			break;
109470bcecdbSGoldwyn Rodrigues 		}
1095ba2746b0SNeilBrown 	if (raid_slot >= 0) {
1096ba2746b0SNeilBrown 		cmsg.raid_slot = cpu_to_le32(raid_slot);
1097293467aaSGoldwyn Rodrigues 		ret = __sendmsg(cinfo, &cmsg);
1098ba2746b0SNeilBrown 	} else
109970bcecdbSGoldwyn Rodrigues 		pr_warn("md-cluster: No good device id found to send\n");
11008b9277c8SGuoqing Jiang 	clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1101293467aaSGoldwyn Rodrigues 	unlock_comm(cinfo);
1102293467aaSGoldwyn Rodrigues 	return ret;
1103293467aaSGoldwyn Rodrigues }
1104293467aaSGoldwyn Rodrigues 
metadata_update_cancel(struct mddev * mddev)1105dbb64f86SGoldwyn Rodrigues static void metadata_update_cancel(struct mddev *mddev)
1106293467aaSGoldwyn Rodrigues {
1107293467aaSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
11088b9277c8SGuoqing Jiang 	clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1109dbb64f86SGoldwyn Rodrigues 	unlock_comm(cinfo);
1110293467aaSGoldwyn Rodrigues }
1111293467aaSGoldwyn Rodrigues 
update_bitmap_size(struct mddev * mddev,sector_t size)1112afd75628SGuoqing Jiang static int update_bitmap_size(struct mddev *mddev, sector_t size)
1113afd75628SGuoqing Jiang {
1114afd75628SGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
1115afd75628SGuoqing Jiang 	struct cluster_msg cmsg = {0};
1116afd75628SGuoqing Jiang 	int ret;
1117afd75628SGuoqing Jiang 
1118afd75628SGuoqing Jiang 	cmsg.type = cpu_to_le32(BITMAP_RESIZE);
1119afd75628SGuoqing Jiang 	cmsg.high = cpu_to_le64(size);
1120afd75628SGuoqing Jiang 	ret = sendmsg(cinfo, &cmsg, 0);
1121afd75628SGuoqing Jiang 	if (ret)
1122afd75628SGuoqing Jiang 		pr_err("%s:%d: failed to send BITMAP_RESIZE message (%d)\n",
1123afd75628SGuoqing Jiang 			__func__, __LINE__, ret);
1124afd75628SGuoqing Jiang 	return ret;
1125afd75628SGuoqing Jiang }
1126afd75628SGuoqing Jiang 
resize_bitmaps(struct mddev * mddev,sector_t newsize,sector_t oldsize)1127afd75628SGuoqing Jiang static int resize_bitmaps(struct mddev *mddev, sector_t newsize, sector_t oldsize)
1128afd75628SGuoqing Jiang {
1129afd75628SGuoqing Jiang 	struct bitmap_counts *counts;
1130afd75628SGuoqing Jiang 	char str[64];
1131afd75628SGuoqing Jiang 	struct dlm_lock_resource *bm_lockres;
1132afd75628SGuoqing Jiang 	struct bitmap *bitmap = mddev->bitmap;
1133afd75628SGuoqing Jiang 	unsigned long my_pages = bitmap->counts.pages;
1134afd75628SGuoqing Jiang 	int i, rv;
1135afd75628SGuoqing Jiang 
1136afd75628SGuoqing Jiang 	/*
1137afd75628SGuoqing Jiang 	 * We need to ensure all the nodes can grow to a larger
1138afd75628SGuoqing Jiang 	 * bitmap size before make the reshaping.
1139afd75628SGuoqing Jiang 	 */
1140afd75628SGuoqing Jiang 	rv = update_bitmap_size(mddev, newsize);
1141afd75628SGuoqing Jiang 	if (rv)
1142afd75628SGuoqing Jiang 		return rv;
1143afd75628SGuoqing Jiang 
1144afd75628SGuoqing Jiang 	for (i = 0; i < mddev->bitmap_info.nodes; i++) {
1145afd75628SGuoqing Jiang 		if (i == md_cluster_ops->slot_number(mddev))
1146afd75628SGuoqing Jiang 			continue;
1147afd75628SGuoqing Jiang 
1148afd75628SGuoqing Jiang 		bitmap = get_bitmap_from_slot(mddev, i);
1149afd75628SGuoqing Jiang 		if (IS_ERR(bitmap)) {
1150afd75628SGuoqing Jiang 			pr_err("can't get bitmap from slot %d\n", i);
1151e8abe1deSDan Carpenter 			bitmap = NULL;
1152afd75628SGuoqing Jiang 			goto out;
1153afd75628SGuoqing Jiang 		}
1154afd75628SGuoqing Jiang 		counts = &bitmap->counts;
1155afd75628SGuoqing Jiang 
1156afd75628SGuoqing Jiang 		/*
1157afd75628SGuoqing Jiang 		 * If we can hold the bitmap lock of one node then
1158afd75628SGuoqing Jiang 		 * the slot is not occupied, update the pages.
1159afd75628SGuoqing Jiang 		 */
1160afd75628SGuoqing Jiang 		snprintf(str, 64, "bitmap%04d", i);
1161afd75628SGuoqing Jiang 		bm_lockres = lockres_init(mddev, str, NULL, 1);
1162afd75628SGuoqing Jiang 		if (!bm_lockres) {
1163afd75628SGuoqing Jiang 			pr_err("Cannot initialize %s lock\n", str);
1164afd75628SGuoqing Jiang 			goto out;
1165afd75628SGuoqing Jiang 		}
1166afd75628SGuoqing Jiang 		bm_lockres->flags |= DLM_LKF_NOQUEUE;
1167afd75628SGuoqing Jiang 		rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
1168afd75628SGuoqing Jiang 		if (!rv)
1169afd75628SGuoqing Jiang 			counts->pages = my_pages;
1170afd75628SGuoqing Jiang 		lockres_free(bm_lockres);
1171afd75628SGuoqing Jiang 
1172afd75628SGuoqing Jiang 		if (my_pages != counts->pages)
1173afd75628SGuoqing Jiang 			/*
1174afd75628SGuoqing Jiang 			 * Let's revert the bitmap size if one node
1175afd75628SGuoqing Jiang 			 * can't resize bitmap
1176afd75628SGuoqing Jiang 			 */
1177afd75628SGuoqing Jiang 			goto out;
11781383b347SZhao Heming 		md_bitmap_free(bitmap);
1179afd75628SGuoqing Jiang 	}
1180afd75628SGuoqing Jiang 
1181afd75628SGuoqing Jiang 	return 0;
1182afd75628SGuoqing Jiang out:
1183afd75628SGuoqing Jiang 	md_bitmap_free(bitmap);
1184afd75628SGuoqing Jiang 	update_bitmap_size(mddev, oldsize);
1185afd75628SGuoqing Jiang 	return -1;
1186afd75628SGuoqing Jiang }
1187afd75628SGuoqing Jiang 
1188b98938d1SGuoqing Jiang /*
1189b98938d1SGuoqing Jiang  * return 0 if all the bitmaps have the same sync_size
1190b98938d1SGuoqing Jiang  */
cluster_check_sync_size(struct mddev * mddev)11917a57157aSColin Ian King static int cluster_check_sync_size(struct mddev *mddev)
1192b98938d1SGuoqing Jiang {
1193b98938d1SGuoqing Jiang 	int i, rv;
1194b98938d1SGuoqing Jiang 	bitmap_super_t *sb;
1195b98938d1SGuoqing Jiang 	unsigned long my_sync_size, sync_size = 0;
1196b98938d1SGuoqing Jiang 	int node_num = mddev->bitmap_info.nodes;
1197b98938d1SGuoqing Jiang 	int current_slot = md_cluster_ops->slot_number(mddev);
1198b98938d1SGuoqing Jiang 	struct bitmap *bitmap = mddev->bitmap;
1199b98938d1SGuoqing Jiang 	char str[64];
1200b98938d1SGuoqing Jiang 	struct dlm_lock_resource *bm_lockres;
1201b98938d1SGuoqing Jiang 
1202b98938d1SGuoqing Jiang 	sb = kmap_atomic(bitmap->storage.sb_page);
1203b98938d1SGuoqing Jiang 	my_sync_size = sb->sync_size;
1204b98938d1SGuoqing Jiang 	kunmap_atomic(sb);
1205b98938d1SGuoqing Jiang 
1206b98938d1SGuoqing Jiang 	for (i = 0; i < node_num; i++) {
1207b98938d1SGuoqing Jiang 		if (i == current_slot)
1208b98938d1SGuoqing Jiang 			continue;
1209b98938d1SGuoqing Jiang 
1210b98938d1SGuoqing Jiang 		bitmap = get_bitmap_from_slot(mddev, i);
1211b98938d1SGuoqing Jiang 		if (IS_ERR(bitmap)) {
1212b98938d1SGuoqing Jiang 			pr_err("can't get bitmap from slot %d\n", i);
1213b98938d1SGuoqing Jiang 			return -1;
1214b98938d1SGuoqing Jiang 		}
1215b98938d1SGuoqing Jiang 
1216b98938d1SGuoqing Jiang 		/*
1217b98938d1SGuoqing Jiang 		 * If we can hold the bitmap lock of one node then
1218b98938d1SGuoqing Jiang 		 * the slot is not occupied, update the sb.
1219b98938d1SGuoqing Jiang 		 */
1220b98938d1SGuoqing Jiang 		snprintf(str, 64, "bitmap%04d", i);
1221b98938d1SGuoqing Jiang 		bm_lockres = lockres_init(mddev, str, NULL, 1);
1222b98938d1SGuoqing Jiang 		if (!bm_lockres) {
1223b98938d1SGuoqing Jiang 			pr_err("md-cluster: Cannot initialize %s\n", str);
1224e64e4018SAndy Shevchenko 			md_bitmap_free(bitmap);
1225b98938d1SGuoqing Jiang 			return -1;
1226b98938d1SGuoqing Jiang 		}
1227b98938d1SGuoqing Jiang 		bm_lockres->flags |= DLM_LKF_NOQUEUE;
1228b98938d1SGuoqing Jiang 		rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
1229b98938d1SGuoqing Jiang 		if (!rv)
1230e64e4018SAndy Shevchenko 			md_bitmap_update_sb(bitmap);
1231b98938d1SGuoqing Jiang 		lockres_free(bm_lockres);
1232b98938d1SGuoqing Jiang 
1233b98938d1SGuoqing Jiang 		sb = kmap_atomic(bitmap->storage.sb_page);
1234b98938d1SGuoqing Jiang 		if (sync_size == 0)
1235b98938d1SGuoqing Jiang 			sync_size = sb->sync_size;
1236b98938d1SGuoqing Jiang 		else if (sync_size != sb->sync_size) {
1237b98938d1SGuoqing Jiang 			kunmap_atomic(sb);
1238e64e4018SAndy Shevchenko 			md_bitmap_free(bitmap);
1239b98938d1SGuoqing Jiang 			return -1;
1240b98938d1SGuoqing Jiang 		}
1241b98938d1SGuoqing Jiang 		kunmap_atomic(sb);
1242e64e4018SAndy Shevchenko 		md_bitmap_free(bitmap);
1243b98938d1SGuoqing Jiang 	}
1244b98938d1SGuoqing Jiang 
1245b98938d1SGuoqing Jiang 	return (my_sync_size == sync_size) ? 0 : -1;
1246b98938d1SGuoqing Jiang }
1247b98938d1SGuoqing Jiang 
1248818da59fSGuoqing Jiang /*
1249818da59fSGuoqing Jiang  * Update the size for cluster raid is a little more complex, we perform it
1250818da59fSGuoqing Jiang  * by the steps:
1251818da59fSGuoqing Jiang  * 1. hold token lock and update superblock in initiator node.
1252818da59fSGuoqing Jiang  * 2. send METADATA_UPDATED msg to other nodes.
1253818da59fSGuoqing Jiang  * 3. The initiator node continues to check each bitmap's sync_size, if all
1254818da59fSGuoqing Jiang  *    bitmaps have the same value of sync_size, then we can set capacity and
1255818da59fSGuoqing Jiang  *    let other nodes to perform it. If one node can't update sync_size
1256818da59fSGuoqing Jiang  *    accordingly, we need to revert to previous value.
1257818da59fSGuoqing Jiang  */
update_size(struct mddev * mddev,sector_t old_dev_sectors)1258818da59fSGuoqing Jiang static void update_size(struct mddev *mddev, sector_t old_dev_sectors)
1259818da59fSGuoqing Jiang {
1260818da59fSGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
1261818da59fSGuoqing Jiang 	struct cluster_msg cmsg;
1262818da59fSGuoqing Jiang 	struct md_rdev *rdev;
1263818da59fSGuoqing Jiang 	int ret = 0;
1264818da59fSGuoqing Jiang 	int raid_slot = -1;
1265818da59fSGuoqing Jiang 
1266818da59fSGuoqing Jiang 	md_update_sb(mddev, 1);
1267bca5b065SZhao Heming 	if (lock_comm(cinfo, 1)) {
1268bca5b065SZhao Heming 		pr_err("%s: lock_comm failed\n", __func__);
1269bca5b065SZhao Heming 		return;
1270bca5b065SZhao Heming 	}
1271818da59fSGuoqing Jiang 
1272818da59fSGuoqing Jiang 	memset(&cmsg, 0, sizeof(cmsg));
1273818da59fSGuoqing Jiang 	cmsg.type = cpu_to_le32(METADATA_UPDATED);
1274818da59fSGuoqing Jiang 	rdev_for_each(rdev, mddev)
1275818da59fSGuoqing Jiang 		if (rdev->raid_disk >= 0 && !test_bit(Faulty, &rdev->flags)) {
1276818da59fSGuoqing Jiang 			raid_slot = rdev->desc_nr;
1277818da59fSGuoqing Jiang 			break;
1278818da59fSGuoqing Jiang 		}
1279818da59fSGuoqing Jiang 	if (raid_slot >= 0) {
1280818da59fSGuoqing Jiang 		cmsg.raid_slot = cpu_to_le32(raid_slot);
1281818da59fSGuoqing Jiang 		/*
1282818da59fSGuoqing Jiang 		 * We can only change capiticy after all the nodes can do it,
1283818da59fSGuoqing Jiang 		 * so need to wait after other nodes already received the msg
1284818da59fSGuoqing Jiang 		 * and handled the change
1285818da59fSGuoqing Jiang 		 */
1286818da59fSGuoqing Jiang 		ret = __sendmsg(cinfo, &cmsg);
1287818da59fSGuoqing Jiang 		if (ret) {
1288818da59fSGuoqing Jiang 			pr_err("%s:%d: failed to send METADATA_UPDATED msg\n",
1289818da59fSGuoqing Jiang 			       __func__, __LINE__);
1290818da59fSGuoqing Jiang 			unlock_comm(cinfo);
1291818da59fSGuoqing Jiang 			return;
1292818da59fSGuoqing Jiang 		}
1293818da59fSGuoqing Jiang 	} else {
1294818da59fSGuoqing Jiang 		pr_err("md-cluster: No good device id found to send\n");
1295818da59fSGuoqing Jiang 		unlock_comm(cinfo);
1296818da59fSGuoqing Jiang 		return;
1297818da59fSGuoqing Jiang 	}
1298818da59fSGuoqing Jiang 
1299818da59fSGuoqing Jiang 	/*
1300818da59fSGuoqing Jiang 	 * check the sync_size from other node's bitmap, if sync_size
1301818da59fSGuoqing Jiang 	 * have already updated in other nodes as expected, send an
1302818da59fSGuoqing Jiang 	 * empty metadata msg to permit the change of capacity
1303818da59fSGuoqing Jiang 	 */
1304818da59fSGuoqing Jiang 	if (cluster_check_sync_size(mddev) == 0) {
1305818da59fSGuoqing Jiang 		memset(&cmsg, 0, sizeof(cmsg));
1306818da59fSGuoqing Jiang 		cmsg.type = cpu_to_le32(CHANGE_CAPACITY);
1307818da59fSGuoqing Jiang 		ret = __sendmsg(cinfo, &cmsg);
1308818da59fSGuoqing Jiang 		if (ret)
1309818da59fSGuoqing Jiang 			pr_err("%s:%d: failed to send CHANGE_CAPACITY msg\n",
1310818da59fSGuoqing Jiang 			       __func__, __LINE__);
13112c247c51SChristoph Hellwig 		set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
1312818da59fSGuoqing Jiang 	} else {
1313818da59fSGuoqing Jiang 		/* revert to previous sectors */
1314818da59fSGuoqing Jiang 		ret = mddev->pers->resize(mddev, old_dev_sectors);
1315818da59fSGuoqing Jiang 		ret = __sendmsg(cinfo, &cmsg);
1316818da59fSGuoqing Jiang 		if (ret)
1317818da59fSGuoqing Jiang 			pr_err("%s:%d: failed to send METADATA_UPDATED msg\n",
1318818da59fSGuoqing Jiang 			       __func__, __LINE__);
1319818da59fSGuoqing Jiang 	}
1320818da59fSGuoqing Jiang 	unlock_comm(cinfo);
1321818da59fSGuoqing Jiang }
1322818da59fSGuoqing Jiang 
resync_start(struct mddev * mddev)1323c186b128SGoldwyn Rodrigues static int resync_start(struct mddev *mddev)
1324c186b128SGoldwyn Rodrigues {
1325c186b128SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1326d6385db9SGuoqing Jiang 	return dlm_lock_sync_interruptible(cinfo->resync_lockres, DLM_LOCK_EX, mddev);
1327c186b128SGoldwyn Rodrigues }
1328c186b128SGoldwyn Rodrigues 
resync_info_get(struct mddev * mddev,sector_t * lo,sector_t * hi)13295ebaf80bSGuoqing Jiang static void resync_info_get(struct mddev *mddev, sector_t *lo, sector_t *hi)
13305ebaf80bSGuoqing Jiang {
13315ebaf80bSGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
13325ebaf80bSGuoqing Jiang 
13335ebaf80bSGuoqing Jiang 	spin_lock_irq(&cinfo->suspend_lock);
1334ea89238cSGuoqing Jiang 	*lo = cinfo->suspend_lo;
1335ea89238cSGuoqing Jiang 	*hi = cinfo->suspend_hi;
13365ebaf80bSGuoqing Jiang 	spin_unlock_irq(&cinfo->suspend_lock);
13375ebaf80bSGuoqing Jiang }
13385ebaf80bSGuoqing Jiang 
resync_info_update(struct mddev * mddev,sector_t lo,sector_t hi)1339c40f341fSGoldwyn Rodrigues static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
1340965400ebSGoldwyn Rodrigues {
1341965400ebSGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1342ac277c6aSGoldwyn Rodrigues 	struct resync_info ri;
1343aee177acSGuoqing Jiang 	struct cluster_msg cmsg = {0};
1344965400ebSGoldwyn Rodrigues 
1345ac277c6aSGoldwyn Rodrigues 	/* do not send zero again, if we have sent before */
1346ac277c6aSGoldwyn Rodrigues 	if (hi == 0) {
1347ac277c6aSGoldwyn Rodrigues 		memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
1348ac277c6aSGoldwyn Rodrigues 		if (le64_to_cpu(ri.hi) == 0)
1349ac277c6aSGoldwyn Rodrigues 			return 0;
1350ac277c6aSGoldwyn Rodrigues 	}
1351ac277c6aSGoldwyn Rodrigues 
135230661b49SNeilBrown 	add_resync_info(cinfo->bitmap_lockres, lo, hi);
1353c40f341fSGoldwyn Rodrigues 	/* Re-acquire the lock to refresh LVB */
1354c40f341fSGoldwyn Rodrigues 	dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
1355c40f341fSGoldwyn Rodrigues 	cmsg.type = cpu_to_le32(RESYNCING);
1356965400ebSGoldwyn Rodrigues 	cmsg.low = cpu_to_le64(lo);
1357965400ebSGoldwyn Rodrigues 	cmsg.high = cpu_to_le64(hi);
1358c186b128SGoldwyn Rodrigues 
13590ba95977SGuoqing Jiang 	/*
13600ba95977SGuoqing Jiang 	 * mddev_lock is held if resync_info_update is called from
13610ba95977SGuoqing Jiang 	 * resync_finish (md_reap_sync_thread -> resync_finish)
13620ba95977SGuoqing Jiang 	 */
13630ba95977SGuoqing Jiang 	if (lo == 0 && hi == 0)
13640ba95977SGuoqing Jiang 		return sendmsg(cinfo, &cmsg, 1);
13650ba95977SGuoqing Jiang 	else
13660ba95977SGuoqing Jiang 		return sendmsg(cinfo, &cmsg, 0);
1367965400ebSGoldwyn Rodrigues }
1368965400ebSGoldwyn Rodrigues 
resync_finish(struct mddev * mddev)1369c186b128SGoldwyn Rodrigues static int resync_finish(struct mddev *mddev)
1370c186b128SGoldwyn Rodrigues {
1371c186b128SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
137241a95041SGuoqing Jiang 	int ret = 0;
13730357ba27SGuoqing Jiang 
13740357ba27SGuoqing Jiang 	clear_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
1375df8c6764SGuoqing Jiang 
1376df8c6764SGuoqing Jiang 	/*
1377df8c6764SGuoqing Jiang 	 * If resync thread is interrupted so we can't say resync is finished,
1378df8c6764SGuoqing Jiang 	 * another node will launch resync thread to continue.
1379df8c6764SGuoqing Jiang 	 */
138041a95041SGuoqing Jiang 	if (!test_bit(MD_CLOSING, &mddev->flags))
138141a95041SGuoqing Jiang 		ret = resync_info_update(mddev, 0, 0);
138241a95041SGuoqing Jiang 	dlm_unlock_sync(cinfo->resync_lockres);
138341a95041SGuoqing Jiang 	return ret;
1384c186b128SGoldwyn Rodrigues }
1385c186b128SGoldwyn Rodrigues 
area_resyncing(struct mddev * mddev,int direction,sector_t lo,sector_t hi)138690382ed9SGoldwyn Rodrigues static int area_resyncing(struct mddev *mddev, int direction,
138790382ed9SGoldwyn Rodrigues 		sector_t lo, sector_t hi)
1388589a1c49SGoldwyn Rodrigues {
1389589a1c49SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1390589a1c49SGoldwyn Rodrigues 	int ret = 0;
1391589a1c49SGoldwyn Rodrigues 
139290382ed9SGoldwyn Rodrigues 	if ((direction == READ) &&
139390382ed9SGoldwyn Rodrigues 		test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
139490382ed9SGoldwyn Rodrigues 		return 1;
139590382ed9SGoldwyn Rodrigues 
1396589a1c49SGoldwyn Rodrigues 	spin_lock_irq(&cinfo->suspend_lock);
1397ea89238cSGuoqing Jiang 	if (hi > cinfo->suspend_lo && lo < cinfo->suspend_hi)
1398589a1c49SGoldwyn Rodrigues 		ret = 1;
1399589a1c49SGoldwyn Rodrigues 	spin_unlock_irq(&cinfo->suspend_lock);
1400589a1c49SGoldwyn Rodrigues 	return ret;
1401589a1c49SGoldwyn Rodrigues }
1402589a1c49SGoldwyn Rodrigues 
1403dbb64f86SGoldwyn Rodrigues /* add_new_disk() - initiates a disk add
1404dbb64f86SGoldwyn Rodrigues  * However, if this fails before writing md_update_sb(),
1405dbb64f86SGoldwyn Rodrigues  * add_new_disk_cancel() must be called to release token lock
1406dbb64f86SGoldwyn Rodrigues  */
add_new_disk(struct mddev * mddev,struct md_rdev * rdev)1407dbb64f86SGoldwyn Rodrigues static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
14081aee41f6SGoldwyn Rodrigues {
14091aee41f6SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
14101aee41f6SGoldwyn Rodrigues 	struct cluster_msg cmsg;
14111aee41f6SGoldwyn Rodrigues 	int ret = 0;
14121aee41f6SGoldwyn Rodrigues 	struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
14131aee41f6SGoldwyn Rodrigues 	char *uuid = sb->device_uuid;
14141aee41f6SGoldwyn Rodrigues 
14151aee41f6SGoldwyn Rodrigues 	memset(&cmsg, 0, sizeof(cmsg));
14161aee41f6SGoldwyn Rodrigues 	cmsg.type = cpu_to_le32(NEWDISK);
14171aee41f6SGoldwyn Rodrigues 	memcpy(cmsg.uuid, uuid, 16);
1418faeff83fSGuoqing Jiang 	cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1419bca5b065SZhao Heming 	if (lock_comm(cinfo, 1))
1420bca5b065SZhao Heming 		return -EAGAIN;
14211aee41f6SGoldwyn Rodrigues 	ret = __sendmsg(cinfo, &cmsg);
14222dffdc07SGuoqing Jiang 	if (ret) {
14232dffdc07SGuoqing Jiang 		unlock_comm(cinfo);
14241aee41f6SGoldwyn Rodrigues 		return ret;
14252dffdc07SGuoqing Jiang 	}
14261aee41f6SGoldwyn Rodrigues 	cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
14271aee41f6SGoldwyn Rodrigues 	ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
14281aee41f6SGoldwyn Rodrigues 	cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
14291aee41f6SGoldwyn Rodrigues 	/* Some node does not "see" the device */
14301aee41f6SGoldwyn Rodrigues 	if (ret == -EAGAIN)
14311aee41f6SGoldwyn Rodrigues 		ret = -ENOENT;
1432dbb64f86SGoldwyn Rodrigues 	if (ret)
1433dbb64f86SGoldwyn Rodrigues 		unlock_comm(cinfo);
14348b9277c8SGuoqing Jiang 	else {
14351aee41f6SGoldwyn Rodrigues 		dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
1436e19508faSGuoqing Jiang 		/* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
1437e19508faSGuoqing Jiang 		 * will run soon after add_new_disk, the below path will be
1438e19508faSGuoqing Jiang 		 * invoked:
1439e19508faSGuoqing Jiang 		 *   md_wakeup_thread(mddev->thread)
1440e19508faSGuoqing Jiang 		 *	-> conf->thread (raid1d)
1441e19508faSGuoqing Jiang 		 *	-> md_check_recovery -> md_update_sb
1442e19508faSGuoqing Jiang 		 *	-> metadata_update_start/finish
1443e19508faSGuoqing Jiang 		 * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
1444e19508faSGuoqing Jiang 		 *
1445e19508faSGuoqing Jiang 		 * For other failure cases, metadata_update_cancel and
1446e19508faSGuoqing Jiang 		 * add_new_disk_cancel also clear below bit as well.
1447e19508faSGuoqing Jiang 		 * */
14488b9277c8SGuoqing Jiang 		set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
14498b9277c8SGuoqing Jiang 		wake_up(&cinfo->wait);
14508b9277c8SGuoqing Jiang 	}
14511aee41f6SGoldwyn Rodrigues 	return ret;
14521aee41f6SGoldwyn Rodrigues }
14531aee41f6SGoldwyn Rodrigues 
add_new_disk_cancel(struct mddev * mddev)1454dbb64f86SGoldwyn Rodrigues static void add_new_disk_cancel(struct mddev *mddev)
14551aee41f6SGoldwyn Rodrigues {
1456dbb64f86SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
14578b9277c8SGuoqing Jiang 	clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1458dbb64f86SGoldwyn Rodrigues 	unlock_comm(cinfo);
14591aee41f6SGoldwyn Rodrigues }
14601aee41f6SGoldwyn Rodrigues 
new_disk_ack(struct mddev * mddev,bool ack)1461fa8259daSGoldwyn Rodrigues static int new_disk_ack(struct mddev *mddev, bool ack)
14621aee41f6SGoldwyn Rodrigues {
14631aee41f6SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
14641aee41f6SGoldwyn Rodrigues 
1465fa8259daSGoldwyn Rodrigues 	if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
1466fa8259daSGoldwyn Rodrigues 		pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
1467fa8259daSGoldwyn Rodrigues 		return -EINVAL;
1468fa8259daSGoldwyn Rodrigues 	}
1469fa8259daSGoldwyn Rodrigues 
14701aee41f6SGoldwyn Rodrigues 	if (ack)
14711aee41f6SGoldwyn Rodrigues 		dlm_unlock_sync(cinfo->no_new_dev_lockres);
14721aee41f6SGoldwyn Rodrigues 	complete(&cinfo->newdisk_completion);
1473fa8259daSGoldwyn Rodrigues 	return 0;
14741aee41f6SGoldwyn Rodrigues }
14751aee41f6SGoldwyn Rodrigues 
remove_disk(struct mddev * mddev,struct md_rdev * rdev)147688bcfef7SGoldwyn Rodrigues static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
147788bcfef7SGoldwyn Rodrigues {
1478aee177acSGuoqing Jiang 	struct cluster_msg cmsg = {0};
147988bcfef7SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
1480faeff83fSGuoqing Jiang 	cmsg.type = cpu_to_le32(REMOVE);
1481faeff83fSGuoqing Jiang 	cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
14820ba95977SGuoqing Jiang 	return sendmsg(cinfo, &cmsg, 1);
148388bcfef7SGoldwyn Rodrigues }
148488bcfef7SGoldwyn Rodrigues 
lock_all_bitmaps(struct mddev * mddev)1485f6a2dc64SGuoqing Jiang static int lock_all_bitmaps(struct mddev *mddev)
1486f6a2dc64SGuoqing Jiang {
1487f6a2dc64SGuoqing Jiang 	int slot, my_slot, ret, held = 1, i = 0;
1488f6a2dc64SGuoqing Jiang 	char str[64];
1489f6a2dc64SGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
1490f6a2dc64SGuoqing Jiang 
14916396bb22SKees Cook 	cinfo->other_bitmap_lockres =
14926396bb22SKees Cook 		kcalloc(mddev->bitmap_info.nodes - 1,
14936396bb22SKees Cook 			sizeof(struct dlm_lock_resource *), GFP_KERNEL);
1494f6a2dc64SGuoqing Jiang 	if (!cinfo->other_bitmap_lockres) {
1495f6a2dc64SGuoqing Jiang 		pr_err("md: can't alloc mem for other bitmap locks\n");
1496f6a2dc64SGuoqing Jiang 		return 0;
1497f6a2dc64SGuoqing Jiang 	}
1498f6a2dc64SGuoqing Jiang 
1499f6a2dc64SGuoqing Jiang 	my_slot = slot_number(mddev);
1500f6a2dc64SGuoqing Jiang 	for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
1501f6a2dc64SGuoqing Jiang 		if (slot == my_slot)
1502f6a2dc64SGuoqing Jiang 			continue;
1503f6a2dc64SGuoqing Jiang 
1504f6a2dc64SGuoqing Jiang 		memset(str, '\0', 64);
1505f6a2dc64SGuoqing Jiang 		snprintf(str, 64, "bitmap%04d", slot);
1506f6a2dc64SGuoqing Jiang 		cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
1507f6a2dc64SGuoqing Jiang 		if (!cinfo->other_bitmap_lockres[i])
1508f6a2dc64SGuoqing Jiang 			return -ENOMEM;
1509f6a2dc64SGuoqing Jiang 
1510f6a2dc64SGuoqing Jiang 		cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
1511f6a2dc64SGuoqing Jiang 		ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
1512f6a2dc64SGuoqing Jiang 		if (ret)
1513f6a2dc64SGuoqing Jiang 			held = -1;
1514f6a2dc64SGuoqing Jiang 		i++;
1515f6a2dc64SGuoqing Jiang 	}
1516f6a2dc64SGuoqing Jiang 
1517f6a2dc64SGuoqing Jiang 	return held;
1518f6a2dc64SGuoqing Jiang }
1519f6a2dc64SGuoqing Jiang 
unlock_all_bitmaps(struct mddev * mddev)1520f6a2dc64SGuoqing Jiang static void unlock_all_bitmaps(struct mddev *mddev)
1521f6a2dc64SGuoqing Jiang {
1522f6a2dc64SGuoqing Jiang 	struct md_cluster_info *cinfo = mddev->cluster_info;
1523f6a2dc64SGuoqing Jiang 	int i;
1524f6a2dc64SGuoqing Jiang 
1525f6a2dc64SGuoqing Jiang 	/* release other node's bitmap lock if they are existed */
1526f6a2dc64SGuoqing Jiang 	if (cinfo->other_bitmap_lockres) {
1527f6a2dc64SGuoqing Jiang 		for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
1528f6a2dc64SGuoqing Jiang 			if (cinfo->other_bitmap_lockres[i]) {
1529f6a2dc64SGuoqing Jiang 				lockres_free(cinfo->other_bitmap_lockres[i]);
1530f6a2dc64SGuoqing Jiang 			}
1531f6a2dc64SGuoqing Jiang 		}
1532f6a2dc64SGuoqing Jiang 		kfree(cinfo->other_bitmap_lockres);
153360f80d6fSZhao Heming 		cinfo->other_bitmap_lockres = NULL;
1534f6a2dc64SGuoqing Jiang 	}
1535f6a2dc64SGuoqing Jiang }
1536f6a2dc64SGuoqing Jiang 
gather_bitmaps(struct md_rdev * rdev)153797f6cd39SGoldwyn Rodrigues static int gather_bitmaps(struct md_rdev *rdev)
153897f6cd39SGoldwyn Rodrigues {
153997f6cd39SGoldwyn Rodrigues 	int sn, err;
154097f6cd39SGoldwyn Rodrigues 	sector_t lo, hi;
1541aee177acSGuoqing Jiang 	struct cluster_msg cmsg = {0};
154297f6cd39SGoldwyn Rodrigues 	struct mddev *mddev = rdev->mddev;
154397f6cd39SGoldwyn Rodrigues 	struct md_cluster_info *cinfo = mddev->cluster_info;
154497f6cd39SGoldwyn Rodrigues 
1545faeff83fSGuoqing Jiang 	cmsg.type = cpu_to_le32(RE_ADD);
1546faeff83fSGuoqing Jiang 	cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
15470ba95977SGuoqing Jiang 	err = sendmsg(cinfo, &cmsg, 1);
154897f6cd39SGoldwyn Rodrigues 	if (err)
154997f6cd39SGoldwyn Rodrigues 		goto out;
155097f6cd39SGoldwyn Rodrigues 
155197f6cd39SGoldwyn Rodrigues 	for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
155297f6cd39SGoldwyn Rodrigues 		if (sn == (cinfo->slot_number - 1))
155397f6cd39SGoldwyn Rodrigues 			continue;
1554e64e4018SAndy Shevchenko 		err = md_bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
155597f6cd39SGoldwyn Rodrigues 		if (err) {
155697f6cd39SGoldwyn Rodrigues 			pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
155797f6cd39SGoldwyn Rodrigues 			goto out;
155897f6cd39SGoldwyn Rodrigues 		}
155997f6cd39SGoldwyn Rodrigues 		if ((hi > 0) && (lo < mddev->recovery_cp))
156097f6cd39SGoldwyn Rodrigues 			mddev->recovery_cp = lo;
156197f6cd39SGoldwyn Rodrigues 	}
156297f6cd39SGoldwyn Rodrigues out:
156397f6cd39SGoldwyn Rodrigues 	return err;
156497f6cd39SGoldwyn Rodrigues }
156597f6cd39SGoldwyn Rodrigues 
1566edb39c9dSGoldwyn Rodrigues static struct md_cluster_operations cluster_ops = {
1567edb39c9dSGoldwyn Rodrigues 	.join   = join,
1568edb39c9dSGoldwyn Rodrigues 	.leave  = leave,
1569cf921cc1SGoldwyn Rodrigues 	.slot_number = slot_number,
1570c186b128SGoldwyn Rodrigues 	.resync_start = resync_start,
1571c186b128SGoldwyn Rodrigues 	.resync_finish = resync_finish,
157296ae923aSGoldwyn Rodrigues 	.resync_info_update = resync_info_update,
15735ebaf80bSGuoqing Jiang 	.resync_info_get = resync_info_get,
1574293467aaSGoldwyn Rodrigues 	.metadata_update_start = metadata_update_start,
1575293467aaSGoldwyn Rodrigues 	.metadata_update_finish = metadata_update_finish,
1576293467aaSGoldwyn Rodrigues 	.metadata_update_cancel = metadata_update_cancel,
1577589a1c49SGoldwyn Rodrigues 	.area_resyncing = area_resyncing,
1578dbb64f86SGoldwyn Rodrigues 	.add_new_disk = add_new_disk,
1579dbb64f86SGoldwyn Rodrigues 	.add_new_disk_cancel = add_new_disk_cancel,
15801aee41f6SGoldwyn Rodrigues 	.new_disk_ack = new_disk_ack,
158188bcfef7SGoldwyn Rodrigues 	.remove_disk = remove_disk,
158251e453aeSGuoqing Jiang 	.load_bitmaps = load_bitmaps,
158397f6cd39SGoldwyn Rodrigues 	.gather_bitmaps = gather_bitmaps,
1584afd75628SGuoqing Jiang 	.resize_bitmaps = resize_bitmaps,
1585f6a2dc64SGuoqing Jiang 	.lock_all_bitmaps = lock_all_bitmaps,
1586f6a2dc64SGuoqing Jiang 	.unlock_all_bitmaps = unlock_all_bitmaps,
1587818da59fSGuoqing Jiang 	.update_size = update_size,
1588edb39c9dSGoldwyn Rodrigues };
1589edb39c9dSGoldwyn Rodrigues 
cluster_init(void)15908e854e9cSGoldwyn Rodrigues static int __init cluster_init(void)
15918e854e9cSGoldwyn Rodrigues {
1592f0e230adSGuoqing Jiang 	pr_warn("md-cluster: support raid1 and raid10 (limited support)\n");
15938e854e9cSGoldwyn Rodrigues 	pr_info("Registering Cluster MD functions\n");
1594edb39c9dSGoldwyn Rodrigues 	register_md_cluster_operations(&cluster_ops, THIS_MODULE);
15958e854e9cSGoldwyn Rodrigues 	return 0;
15968e854e9cSGoldwyn Rodrigues }
15978e854e9cSGoldwyn Rodrigues 
cluster_exit(void)15988e854e9cSGoldwyn Rodrigues static void cluster_exit(void)
15998e854e9cSGoldwyn Rodrigues {
1600edb39c9dSGoldwyn Rodrigues 	unregister_md_cluster_operations();
16018e854e9cSGoldwyn Rodrigues }
16028e854e9cSGoldwyn Rodrigues 
16038e854e9cSGoldwyn Rodrigues module_init(cluster_init);
16048e854e9cSGoldwyn Rodrigues module_exit(cluster_exit);
160586b57277SGuoqing Jiang MODULE_AUTHOR("SUSE");
16068e854e9cSGoldwyn Rodrigues MODULE_LICENSE("GPL");
16078e854e9cSGoldwyn Rodrigues MODULE_DESCRIPTION("Clustering support for MD");
1608