]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/md/md-cluster.c
Merge remote-tracking branches 'asoc/topic/sgtl5000', 'asoc/topic/simple', 'asoc...
[mirror_ubuntu-zesty-kernel.git] / drivers / md / md-cluster.c
CommitLineData
8e854e9c
GR
1/*
2 * Copyright (C) 2015, SUSE
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 */
10
11
12#include <linux/module.h>
47741b7c
GR
13#include <linux/dlm.h>
14#include <linux/sched.h>
1aee41f6 15#include <linux/raid/md_p.h>
47741b7c 16#include "md.h"
e94987db 17#include "bitmap.h"
edb39c9d 18#include "md-cluster.h"
47741b7c
GR
19
20#define LVB_SIZE 64
1aee41f6 21#define NEW_DEV_TIMEOUT 5000
47741b7c
GR
22
23struct dlm_lock_resource {
24 dlm_lockspace_t *ls;
25 struct dlm_lksb lksb;
26 char *name; /* lock name. */
27 uint32_t flags; /* flags to pass to dlm_lock() */
47741b7c 28 struct completion completion; /* completion for synchronized locking */
c4ce867f
GR
29 void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
30 struct mddev *mddev; /* pointing back to mddev. */
dbb64f86 31 int mode;
c4ce867f
GR
32};
33
96ae923a
GR
34struct suspend_info {
35 int slot;
36 sector_t lo;
37 sector_t hi;
38 struct list_head list;
39};
40
41struct resync_info {
42 __le64 lo;
43 __le64 hi;
44};
45
fa8259da
GR
46/* md_cluster_info flags */
47#define MD_CLUSTER_WAITING_FOR_NEWDISK 1
90382ed9 48#define MD_CLUSTER_SUSPEND_READ_BALANCING 2
eece075c 49#define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
fa8259da 50
8b9277c8
GJ
51/* Lock the send communication. This is done through
52 * bit manipulation as opposed to a mutex in order to
53 * accomodate lock and hold. See next comment.
54 */
55#define MD_CLUSTER_SEND_LOCK 4
e19508fa
GJ
56/* If cluster operations (such as adding a disk) must lock the
57 * communication channel, so as to perform extra operations
58 * (update metadata) and no other operation is allowed on the
59 * MD. Token needs to be locked and held until the operation
60 * completes witha md_update_sb(), which would eventually release
61 * the lock.
8b9277c8
GJ
62 */
63#define MD_CLUSTER_SEND_LOCKED_ALREADY 5
51e453ae
GJ
64/* We should receive message after node joined cluster and
65 * set up all the related infos such as bitmap and personality */
66#define MD_CLUSTER_ALREADY_IN_CLUSTER 6
67#define MD_CLUSTER_PENDING_RECV_EVENT 7
8b9277c8 68
fa8259da 69
c4ce867f
GR
70struct md_cluster_info {
71 /* dlm lock space and resources for clustered raid. */
72 dlm_lockspace_t *lockspace;
cf921cc1
GR
73 int slot_number;
74 struct completion completion;
8b9277c8 75 struct mutex recv_mutex;
54519c5f 76 struct dlm_lock_resource *bitmap_lockres;
f6a2dc64 77 struct dlm_lock_resource **other_bitmap_lockres;
c186b128 78 struct dlm_lock_resource *resync_lockres;
96ae923a
GR
79 struct list_head suspend_list;
80 spinlock_t suspend_lock;
e94987db
GR
81 struct md_thread *recovery_thread;
82 unsigned long recovery_map;
4664680c
GR
83 /* communication loc resources */
84 struct dlm_lock_resource *ack_lockres;
85 struct dlm_lock_resource *message_lockres;
86 struct dlm_lock_resource *token_lockres;
1aee41f6 87 struct dlm_lock_resource *no_new_dev_lockres;
4664680c 88 struct md_thread *recv_thread;
1aee41f6 89 struct completion newdisk_completion;
8b9277c8 90 wait_queue_head_t wait;
fa8259da 91 unsigned long state;
18c9ff7f
GJ
92 /* record the region in RESYNCING message */
93 sector_t sync_low;
94 sector_t sync_hi;
4664680c
GR
95};
96
97enum msg_type {
98 METADATA_UPDATED = 0,
99 RESYNCING,
1aee41f6 100 NEWDISK,
88bcfef7 101 REMOVE,
97f6cd39 102 RE_ADD,
dc737d7c 103 BITMAP_NEEDS_SYNC,
4664680c
GR
104};
105
106struct cluster_msg {
cf97a348
GJ
107 __le32 type;
108 __le32 slot;
1aee41f6 109 /* TODO: Unionize this for smaller footprint */
cf97a348
GJ
110 __le64 low;
111 __le64 high;
1aee41f6 112 char uuid[16];
cf97a348 113 __le32 raid_slot;
47741b7c
GR
114};
115
116static void sync_ast(void *arg)
117{
118 struct dlm_lock_resource *res;
119
2e2a7cd9 120 res = arg;
47741b7c
GR
121 complete(&res->completion);
122}
123
124static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
125{
126 int ret = 0;
127
47741b7c
GR
128 ret = dlm_lock(res->ls, mode, &res->lksb,
129 res->flags, res->name, strlen(res->name),
130 0, sync_ast, res, res->bast);
131 if (ret)
132 return ret;
133 wait_for_completion(&res->completion);
dbb64f86
GR
134 if (res->lksb.sb_status == 0)
135 res->mode = mode;
47741b7c
GR
136 return res->lksb.sb_status;
137}
138
139static int dlm_unlock_sync(struct dlm_lock_resource *res)
140{
141 return dlm_lock_sync(res, DLM_LOCK_NL);
142}
143
c4ce867f 144static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
47741b7c
GR
145 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
146{
147 struct dlm_lock_resource *res = NULL;
148 int ret, namelen;
c4ce867f 149 struct md_cluster_info *cinfo = mddev->cluster_info;
47741b7c
GR
150
151 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
152 if (!res)
153 return NULL;
b83d51c0 154 init_completion(&res->completion);
c4ce867f
GR
155 res->ls = cinfo->lockspace;
156 res->mddev = mddev;
dbb64f86 157 res->mode = DLM_LOCK_IV;
47741b7c
GR
158 namelen = strlen(name);
159 res->name = kzalloc(namelen + 1, GFP_KERNEL);
160 if (!res->name) {
161 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
162 goto out_err;
163 }
164 strlcpy(res->name, name, namelen + 1);
165 if (with_lvb) {
166 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
167 if (!res->lksb.sb_lvbptr) {
168 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
169 goto out_err;
170 }
171 res->flags = DLM_LKF_VALBLK;
172 }
173
174 if (bastfn)
175 res->bast = bastfn;
176
177 res->flags |= DLM_LKF_EXPEDITE;
178
179 ret = dlm_lock_sync(res, DLM_LOCK_NL);
180 if (ret) {
181 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
182 goto out_err;
183 }
184 res->flags &= ~DLM_LKF_EXPEDITE;
185 res->flags |= DLM_LKF_CONVERT;
186
187 return res;
188out_err:
189 kfree(res->lksb.sb_lvbptr);
190 kfree(res->name);
191 kfree(res);
192 return NULL;
193}
194
195static void lockres_free(struct dlm_lock_resource *res)
196{
b5ef5678
GJ
197 int ret;
198
47741b7c
GR
199 if (!res)
200 return;
201
b5ef5678
GJ
202 /* cancel a lock request or a conversion request that is blocked */
203 res->flags |= DLM_LKF_CANCEL;
204retry:
205 ret = dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res);
206 if (unlikely(ret != 0)) {
207 pr_info("%s: failed to unlock %s return %d\n", __func__, res->name, ret);
208
209 /* if a lock conversion is cancelled, then the lock is put
210 * back to grant queue, need to ensure it is unlocked */
211 if (ret == -DLM_ECANCEL)
212 goto retry;
213 }
214 res->flags &= ~DLM_LKF_CANCEL;
47741b7c
GR
215 wait_for_completion(&res->completion);
216
217 kfree(res->name);
218 kfree(res->lksb.sb_lvbptr);
219 kfree(res);
220}
8e854e9c 221
30661b49
N
222static void add_resync_info(struct dlm_lock_resource *lockres,
223 sector_t lo, sector_t hi)
96ae923a
GR
224{
225 struct resync_info *ri;
226
227 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
228 ri->lo = cpu_to_le64(lo);
229 ri->hi = cpu_to_le64(hi);
230}
231
232static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
233{
234 struct resync_info ri;
235 struct suspend_info *s = NULL;
236 sector_t hi = 0;
237
238 dlm_lock_sync(lockres, DLM_LOCK_CR);
239 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
240 hi = le64_to_cpu(ri.hi);
cf97a348 241 if (hi > 0) {
96ae923a
GR
242 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
243 if (!s)
244 goto out;
245 s->hi = hi;
246 s->lo = le64_to_cpu(ri.lo);
247 }
248 dlm_unlock_sync(lockres);
249out:
250 return s;
251}
252
6dc69c9c 253static void recover_bitmaps(struct md_thread *thread)
e94987db
GR
254{
255 struct mddev *mddev = thread->mddev;
256 struct md_cluster_info *cinfo = mddev->cluster_info;
257 struct dlm_lock_resource *bm_lockres;
258 char str[64];
259 int slot, ret;
260 struct suspend_info *s, *tmp;
261 sector_t lo, hi;
262
263 while (cinfo->recovery_map) {
264 slot = fls64((u64)cinfo->recovery_map) - 1;
265
266 /* Clear suspend_area associated with the bitmap */
267 spin_lock_irq(&cinfo->suspend_lock);
268 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
269 if (slot == s->slot) {
270 list_del(&s->list);
271 kfree(s);
272 }
273 spin_unlock_irq(&cinfo->suspend_lock);
274
275 snprintf(str, 64, "bitmap%04d", slot);
276 bm_lockres = lockres_init(mddev, str, NULL, 1);
277 if (!bm_lockres) {
278 pr_err("md-cluster: Cannot initialize bitmaps\n");
279 goto clear_bit;
280 }
281
282 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
283 if (ret) {
284 pr_err("md-cluster: Could not DLM lock %s: %d\n",
285 str, ret);
286 goto clear_bit;
287 }
97f6cd39 288 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
4b26a08a 289 if (ret) {
e94987db 290 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
4b26a08a
GR
291 goto dlm_unlock;
292 }
293 if (hi > 0) {
4b26a08a
GR
294 if (lo < mddev->recovery_cp)
295 mddev->recovery_cp = lo;
eb315cd0
GJ
296 /* wake up thread to continue resync in case resync
297 * is not finished */
298 if (mddev->recovery_cp != MaxSector) {
299 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
300 md_wakeup_thread(mddev->thread);
301 }
4b26a08a
GR
302 }
303dlm_unlock:
e94987db
GR
304 dlm_unlock_sync(bm_lockres);
305clear_bit:
4ac7a65f 306 lockres_free(bm_lockres);
e94987db
GR
307 clear_bit(slot, &cinfo->recovery_map);
308 }
309}
310
cf921cc1
GR
311static void recover_prep(void *arg)
312{
90382ed9
GR
313 struct mddev *mddev = arg;
314 struct md_cluster_info *cinfo = mddev->cluster_info;
315 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
cf921cc1
GR
316}
317
05cd0e51 318static void __recover_slot(struct mddev *mddev, int slot)
cf921cc1 319{
cf921cc1
GR
320 struct md_cluster_info *cinfo = mddev->cluster_info;
321
05cd0e51 322 set_bit(slot, &cinfo->recovery_map);
e94987db
GR
323 if (!cinfo->recovery_thread) {
324 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
325 mddev, "recover");
326 if (!cinfo->recovery_thread) {
327 pr_warn("md-cluster: Could not create recovery thread\n");
328 return;
329 }
330 }
331 md_wakeup_thread(cinfo->recovery_thread);
cf921cc1
GR
332}
333
05cd0e51
GJ
334static void recover_slot(void *arg, struct dlm_slot *slot)
335{
336 struct mddev *mddev = arg;
337 struct md_cluster_info *cinfo = mddev->cluster_info;
338
339 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
340 mddev->bitmap_info.cluster_name,
341 slot->nodeid, slot->slot,
342 cinfo->slot_number);
343 /* deduct one since dlm slot starts from one while the num of
344 * cluster-md begins with 0 */
345 __recover_slot(mddev, slot->slot - 1);
346}
347
cf921cc1
GR
348static void recover_done(void *arg, struct dlm_slot *slots,
349 int num_slots, int our_slot,
350 uint32_t generation)
351{
352 struct mddev *mddev = arg;
353 struct md_cluster_info *cinfo = mddev->cluster_info;
354
355 cinfo->slot_number = our_slot;
eece075c
GJ
356 /* completion is only need to be complete when node join cluster,
357 * it doesn't need to run during another node's failure */
358 if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
359 complete(&cinfo->completion);
360 clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
361 }
90382ed9 362 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
cf921cc1
GR
363}
364
eece075c
GJ
365/* the ops is called when node join the cluster, and do lock recovery
366 * if node failure occurs */
cf921cc1
GR
367static const struct dlm_lockspace_ops md_ls_ops = {
368 .recover_prep = recover_prep,
369 .recover_slot = recover_slot,
370 .recover_done = recover_done,
371};
372
4664680c
GR
373/*
374 * The BAST function for the ack lock resource
375 * This function wakes up the receive thread in
376 * order to receive and process the message.
377 */
378static void ack_bast(void *arg, int mode)
379{
2e2a7cd9 380 struct dlm_lock_resource *res = arg;
4664680c
GR
381 struct md_cluster_info *cinfo = res->mddev->cluster_info;
382
51e453ae
GJ
383 if (mode == DLM_LOCK_EX) {
384 if (test_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state))
385 md_wakeup_thread(cinfo->recv_thread);
386 else
387 set_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state);
388 }
4664680c
GR
389}
390
e59721cc
GR
391static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
392{
393 struct suspend_info *s, *tmp;
394
395 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
396 if (slot == s->slot) {
e59721cc
GR
397 list_del(&s->list);
398 kfree(s);
399 break;
400 }
401}
402
b8ca846e 403static void remove_suspend_info(struct mddev *mddev, int slot)
e59721cc 404{
b8ca846e 405 struct md_cluster_info *cinfo = mddev->cluster_info;
e59721cc
GR
406 spin_lock_irq(&cinfo->suspend_lock);
407 __remove_suspend_info(cinfo, slot);
408 spin_unlock_irq(&cinfo->suspend_lock);
b8ca846e 409 mddev->pers->quiesce(mddev, 2);
e59721cc
GR
410}
411
412
9ed38ff5 413static void process_suspend_info(struct mddev *mddev,
e59721cc
GR
414 int slot, sector_t lo, sector_t hi)
415{
9ed38ff5 416 struct md_cluster_info *cinfo = mddev->cluster_info;
e59721cc
GR
417 struct suspend_info *s;
418
419 if (!hi) {
b8ca846e 420 remove_suspend_info(mddev, slot);
c186b128
GR
421 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
422 md_wakeup_thread(mddev->thread);
e59721cc
GR
423 return;
424 }
18c9ff7f
GJ
425
426 /*
427 * The bitmaps are not same for different nodes
428 * if RESYNCING is happening in one node, then
429 * the node which received the RESYNCING message
430 * probably will perform resync with the region
431 * [lo, hi] again, so we could reduce resync time
432 * a lot if we can ensure that the bitmaps among
433 * different nodes are match up well.
434 *
435 * sync_low/hi is used to record the region which
436 * arrived in the previous RESYNCING message,
437 *
438 * Call bitmap_sync_with_cluster to clear
439 * NEEDED_MASK and set RESYNC_MASK since
440 * resync thread is running in another node,
441 * so we don't need to do the resync again
442 * with the same section */
443 bitmap_sync_with_cluster(mddev, cinfo->sync_low,
444 cinfo->sync_hi,
445 lo, hi);
446 cinfo->sync_low = lo;
447 cinfo->sync_hi = hi;
448
e59721cc
GR
449 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
450 if (!s)
451 return;
452 s->slot = slot;
453 s->lo = lo;
454 s->hi = hi;
9ed38ff5
GR
455 mddev->pers->quiesce(mddev, 1);
456 mddev->pers->quiesce(mddev, 0);
e59721cc
GR
457 spin_lock_irq(&cinfo->suspend_lock);
458 /* Remove existing entry (if exists) before adding */
459 __remove_suspend_info(cinfo, slot);
460 list_add(&s->list, &cinfo->suspend_list);
461 spin_unlock_irq(&cinfo->suspend_lock);
b8ca846e 462 mddev->pers->quiesce(mddev, 2);
e59721cc
GR
463}
464
1aee41f6
GR
465static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
466{
467 char disk_uuid[64];
468 struct md_cluster_info *cinfo = mddev->cluster_info;
469 char event_name[] = "EVENT=ADD_DEVICE";
470 char raid_slot[16];
471 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
472 int len;
473
474 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
b89f704a 475 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
faeff83f 476 snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
1aee41f6
GR
477 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
478 init_completion(&cinfo->newdisk_completion);
fa8259da 479 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
1aee41f6
GR
480 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
481 wait_for_completion_timeout(&cinfo->newdisk_completion,
482 NEW_DEV_TIMEOUT);
fa8259da 483 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
1aee41f6
GR
484}
485
486
487static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
488{
489 struct md_cluster_info *cinfo = mddev->cluster_info;
15858fa5
GJ
490 mddev->good_device_nr = le32_to_cpu(msg->raid_slot);
491 set_bit(MD_RELOAD_SB, &mddev->flags);
1aee41f6 492 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
15858fa5 493 md_wakeup_thread(mddev->thread);
1aee41f6
GR
494}
495
88bcfef7
GR
496static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
497{
faeff83f
GJ
498 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
499 le32_to_cpu(msg->raid_slot));
88bcfef7 500
659b254f
GJ
501 if (rdev) {
502 set_bit(ClusterRemove, &rdev->flags);
503 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
504 md_wakeup_thread(mddev->thread);
505 }
88bcfef7 506 else
faeff83f
GJ
507 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
508 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
88bcfef7
GR
509}
510
97f6cd39
GR
511static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
512{
faeff83f
GJ
513 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
514 le32_to_cpu(msg->raid_slot));
97f6cd39
GR
515
516 if (rdev && test_bit(Faulty, &rdev->flags))
517 clear_bit(Faulty, &rdev->flags);
518 else
faeff83f
GJ
519 pr_warn("%s: %d Could not find disk(%d) which is faulty",
520 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
97f6cd39
GR
521}
522
1fa9a1ad 523static int process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
4664680c 524{
1fa9a1ad
GJ
525 int ret = 0;
526
256f5b24
GJ
527 if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
528 "node %d received it's own msg\n", le32_to_cpu(msg->slot)))
1fa9a1ad 529 return -1;
cf97a348 530 switch (le32_to_cpu(msg->type)) {
4664680c 531 case METADATA_UPDATED:
1aee41f6 532 process_metadata_update(mddev, msg);
4664680c
GR
533 break;
534 case RESYNCING:
cf97a348
GJ
535 process_suspend_info(mddev, le32_to_cpu(msg->slot),
536 le64_to_cpu(msg->low),
537 le64_to_cpu(msg->high));
4664680c 538 break;
1aee41f6 539 case NEWDISK:
1aee41f6 540 process_add_new_disk(mddev, msg);
88bcfef7
GR
541 break;
542 case REMOVE:
88bcfef7
GR
543 process_remove_disk(mddev, msg);
544 break;
97f6cd39 545 case RE_ADD:
97f6cd39
GR
546 process_readd_disk(mddev, msg);
547 break;
dc737d7c 548 case BITMAP_NEEDS_SYNC:
cf97a348 549 __recover_slot(mddev, le32_to_cpu(msg->slot));
dc737d7c 550 break;
88bcfef7 551 default:
1fa9a1ad 552 ret = -1;
88bcfef7
GR
553 pr_warn("%s:%d Received unknown message from %d\n",
554 __func__, __LINE__, msg->slot);
09dd1af2 555 }
1fa9a1ad 556 return ret;
4664680c
GR
557}
558
559/*
560 * thread for receiving message
561 */
562static void recv_daemon(struct md_thread *thread)
563{
564 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
565 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
566 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
567 struct cluster_msg msg;
b5ef5678 568 int ret;
4664680c 569
8b9277c8 570 mutex_lock(&cinfo->recv_mutex);
4664680c
GR
571 /*get CR on Message*/
572 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
573 pr_err("md/raid1:failed to get CR on MESSAGE\n");
8b9277c8 574 mutex_unlock(&cinfo->recv_mutex);
4664680c
GR
575 return;
576 }
577
578 /* read lvb and wake up thread to process this message_lockres */
579 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
1fa9a1ad
GJ
580 ret = process_recvd_msg(thread->mddev, &msg);
581 if (ret)
582 goto out;
4664680c
GR
583
584 /*release CR on ack_lockres*/
b5ef5678
GJ
585 ret = dlm_unlock_sync(ack_lockres);
586 if (unlikely(ret != 0))
587 pr_info("unlock ack failed return %d\n", ret);
66099bb0 588 /*up-convert to PR on message_lockres*/
b5ef5678
GJ
589 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
590 if (unlikely(ret != 0))
591 pr_info("lock PR on msg failed return %d\n", ret);
4664680c 592 /*get CR on ack_lockres again*/
b5ef5678
GJ
593 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
594 if (unlikely(ret != 0))
595 pr_info("lock CR on ack failed return %d\n", ret);
1fa9a1ad 596out:
4664680c 597 /*release CR on message_lockres*/
b5ef5678
GJ
598 ret = dlm_unlock_sync(message_lockres);
599 if (unlikely(ret != 0))
600 pr_info("unlock msg failed return %d\n", ret);
8b9277c8 601 mutex_unlock(&cinfo->recv_mutex);
4664680c
GR
602}
603
8b9277c8 604/* lock_token()
601b515c
GR
605 * Takes the lock on the TOKEN lock resource so no other
606 * node can communicate while the operation is underway.
607 */
8b9277c8 608static int lock_token(struct md_cluster_info *cinfo)
601b515c
GR
609{
610 int error;
611
612 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
613 if (error)
614 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
615 __func__, __LINE__, error);
8b9277c8
GJ
616
617 /* Lock the receive sequence */
618 mutex_lock(&cinfo->recv_mutex);
601b515c
GR
619 return error;
620}
621
8b9277c8
GJ
622/* lock_comm()
623 * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
624 */
625static int lock_comm(struct md_cluster_info *cinfo)
626{
627 wait_event(cinfo->wait,
628 !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));
629
630 return lock_token(cinfo);
631}
632
601b515c
GR
633static void unlock_comm(struct md_cluster_info *cinfo)
634{
dbb64f86 635 WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
8b9277c8 636 mutex_unlock(&cinfo->recv_mutex);
601b515c 637 dlm_unlock_sync(cinfo->token_lockres);
8b9277c8
GJ
638 clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
639 wake_up(&cinfo->wait);
601b515c
GR
640}
641
642/* __sendmsg()
643 * This function performs the actual sending of the message. This function is
644 * usually called after performing the encompassing operation
645 * The function:
646 * 1. Grabs the message lockresource in EX mode
647 * 2. Copies the message to the message LVB
66099bb0 648 * 3. Downconverts message lockresource to CW
601b515c
GR
649 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
650 * and the other nodes read the message. The thread will wait here until all other
651 * nodes have released ack lock resource.
652 * 5. Downconvert ack lockresource to CR
653 */
654static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
655{
656 int error;
657 int slot = cinfo->slot_number - 1;
658
659 cmsg->slot = cpu_to_le32(slot);
660 /*get EX on Message*/
661 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
662 if (error) {
663 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
664 goto failed_message;
665 }
666
667 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
668 sizeof(struct cluster_msg));
66099bb0
GJ
669 /*down-convert EX to CW on Message*/
670 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
601b515c 671 if (error) {
66099bb0 672 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
601b515c 673 error);
66099bb0 674 goto failed_ack;
601b515c
GR
675 }
676
677 /*up-convert CR to EX on Ack*/
678 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
679 if (error) {
680 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
681 error);
682 goto failed_ack;
683 }
684
685 /*down-convert EX to CR on Ack*/
686 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
687 if (error) {
688 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
689 error);
690 goto failed_ack;
691 }
692
693failed_ack:
b5ef5678
GJ
694 error = dlm_unlock_sync(cinfo->message_lockres);
695 if (unlikely(error != 0)) {
696 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
697 error);
698 /* in case the message can't be released due to some reason */
699 goto failed_ack;
700 }
601b515c
GR
701failed_message:
702 return error;
703}
704
705static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
706{
707 int ret;
708
709 lock_comm(cinfo);
710 ret = __sendmsg(cinfo, cmsg);
711 unlock_comm(cinfo);
712 return ret;
713}
714
96ae923a
GR
715static int gather_all_resync_info(struct mddev *mddev, int total_slots)
716{
717 struct md_cluster_info *cinfo = mddev->cluster_info;
718 int i, ret = 0;
719 struct dlm_lock_resource *bm_lockres;
720 struct suspend_info *s;
721 char str[64];
abb9b22a 722 sector_t lo, hi;
96ae923a
GR
723
724
725 for (i = 0; i < total_slots; i++) {
726 memset(str, '\0', 64);
727 snprintf(str, 64, "bitmap%04d", i);
728 bm_lockres = lockres_init(mddev, str, NULL, 1);
729 if (!bm_lockres)
730 return -ENOMEM;
4ac7a65f
SL
731 if (i == (cinfo->slot_number - 1)) {
732 lockres_free(bm_lockres);
96ae923a 733 continue;
4ac7a65f 734 }
96ae923a
GR
735
736 bm_lockres->flags |= DLM_LKF_NOQUEUE;
737 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
738 if (ret == -EAGAIN) {
739 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
740 s = read_resync_info(mddev, bm_lockres);
741 if (s) {
742 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
743 __func__, __LINE__,
744 (unsigned long long) s->lo,
745 (unsigned long long) s->hi, i);
746 spin_lock_irq(&cinfo->suspend_lock);
747 s->slot = i;
748 list_add(&s->list, &cinfo->suspend_list);
749 spin_unlock_irq(&cinfo->suspend_lock);
750 }
751 ret = 0;
752 lockres_free(bm_lockres);
753 continue;
754 }
6e6d9f2c
GJ
755 if (ret) {
756 lockres_free(bm_lockres);
96ae923a 757 goto out;
6e6d9f2c 758 }
abb9b22a
GJ
759
760 /* Read the disk bitmap sb and check if it needs recovery */
761 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
762 if (ret) {
763 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
764 lockres_free(bm_lockres);
765 continue;
766 }
767 if ((hi > 0) && (lo < mddev->recovery_cp)) {
768 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
769 mddev->recovery_cp = lo;
770 md_check_recovery(mddev);
771 }
772
96ae923a
GR
773 dlm_unlock_sync(bm_lockres);
774 lockres_free(bm_lockres);
775 }
776out:
777 return ret;
778}
779
edb39c9d
GR
780static int join(struct mddev *mddev, int nodes)
781{
c4ce867f 782 struct md_cluster_info *cinfo;
cf921cc1 783 int ret, ops_rv;
c4ce867f
GR
784 char str[64];
785
c4ce867f
GR
786 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
787 if (!cinfo)
788 return -ENOMEM;
789
9e3072e3
GJ
790 INIT_LIST_HEAD(&cinfo->suspend_list);
791 spin_lock_init(&cinfo->suspend_lock);
cf921cc1 792 init_completion(&cinfo->completion);
eece075c 793 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
8b9277c8
GJ
794 init_waitqueue_head(&cinfo->wait);
795 mutex_init(&cinfo->recv_mutex);
cf921cc1 796
cf921cc1
GR
797 mddev->cluster_info = cinfo;
798
c4ce867f 799 memset(str, 0, 64);
b89f704a 800 sprintf(str, "%pU", mddev->uuid);
cf921cc1
GR
801 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
802 DLM_LSFL_FS, LVB_SIZE,
803 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
c4ce867f
GR
804 if (ret)
805 goto err;
cf921cc1 806 wait_for_completion(&cinfo->completion);
8c58f02e
GJ
807 if (nodes < cinfo->slot_number) {
808 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
809 cinfo->slot_number, nodes);
b97e9257
GR
810 ret = -ERANGE;
811 goto err;
812 }
4664680c
GR
813 /* Initiate the communication resources */
814 ret = -ENOMEM;
815 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
816 if (!cinfo->recv_thread) {
817 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
818 goto err;
819 }
820 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
821 if (!cinfo->message_lockres)
822 goto err;
823 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
824 if (!cinfo->token_lockres)
825 goto err;
1aee41f6
GR
826 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
827 if (!cinfo->no_new_dev_lockres)
828 goto err;
829
1535212c
GJ
830 ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
831 if (ret) {
832 ret = -EAGAIN;
833 pr_err("md-cluster: can't join cluster to avoid lock issue\n");
834 goto err;
835 }
836 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
0f6187db
WY
837 if (!cinfo->ack_lockres) {
838 ret = -ENOMEM;
1535212c 839 goto err;
0f6187db 840 }
4664680c
GR
841 /* get sync CR lock on ACK. */
842 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
843 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
844 ret);
1535212c 845 dlm_unlock_sync(cinfo->token_lockres);
1aee41f6
GR
846 /* get sync CR lock on no-new-dev. */
847 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
848 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
849
54519c5f
GR
850
851 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
852 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
853 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
0f6187db
WY
854 if (!cinfo->bitmap_lockres) {
855 ret = -ENOMEM;
54519c5f 856 goto err;
0f6187db 857 }
54519c5f
GR
858 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
859 pr_err("Failed to get bitmap lock\n");
860 ret = -EINVAL;
861 goto err;
862 }
863
c186b128 864 cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
0f6187db
WY
865 if (!cinfo->resync_lockres) {
866 ret = -ENOMEM;
c186b128 867 goto err;
0f6187db 868 }
c186b128 869
edb39c9d 870 return 0;
c4ce867f 871err:
5b0fb33e
GJ
872 md_unregister_thread(&cinfo->recovery_thread);
873 md_unregister_thread(&cinfo->recv_thread);
4664680c
GR
874 lockres_free(cinfo->message_lockres);
875 lockres_free(cinfo->token_lockres);
876 lockres_free(cinfo->ack_lockres);
1aee41f6 877 lockres_free(cinfo->no_new_dev_lockres);
c186b128 878 lockres_free(cinfo->resync_lockres);
96ae923a 879 lockres_free(cinfo->bitmap_lockres);
c4ce867f
GR
880 if (cinfo->lockspace)
881 dlm_release_lockspace(cinfo->lockspace, 2);
cf921cc1 882 mddev->cluster_info = NULL;
c4ce867f 883 kfree(cinfo);
c4ce867f 884 return ret;
edb39c9d
GR
885}
886
51e453ae
GJ
887static void load_bitmaps(struct mddev *mddev, int total_slots)
888{
889 struct md_cluster_info *cinfo = mddev->cluster_info;
890
891 /* load all the node's bitmap info for resync */
892 if (gather_all_resync_info(mddev, total_slots))
893 pr_err("md-cluster: failed to gather all resyn infos\n");
894 set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state);
895 /* wake up recv thread in case something need to be handled */
896 if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
897 md_wakeup_thread(cinfo->recv_thread);
898}
899
09995411
GJ
900static void resync_bitmap(struct mddev *mddev)
901{
902 struct md_cluster_info *cinfo = mddev->cluster_info;
903 struct cluster_msg cmsg = {0};
904 int err;
905
906 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
907 err = sendmsg(cinfo, &cmsg);
908 if (err)
909 pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
910 __func__, __LINE__, err);
911}
912
f6a2dc64 913static void unlock_all_bitmaps(struct mddev *mddev);
edb39c9d
GR
914static int leave(struct mddev *mddev)
915{
c4ce867f
GR
916 struct md_cluster_info *cinfo = mddev->cluster_info;
917
918 if (!cinfo)
919 return 0;
09995411
GJ
920
921 /* BITMAP_NEEDS_SYNC message should be sent when node
922 * is leaving the cluster with dirty bitmap, also we
923 * can only deliver it when dlm connection is available */
924 if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
925 resync_bitmap(mddev);
926
e94987db 927 md_unregister_thread(&cinfo->recovery_thread);
4664680c
GR
928 md_unregister_thread(&cinfo->recv_thread);
929 lockres_free(cinfo->message_lockres);
930 lockres_free(cinfo->token_lockres);
931 lockres_free(cinfo->ack_lockres);
1aee41f6 932 lockres_free(cinfo->no_new_dev_lockres);
4ac7a65f 933 lockres_free(cinfo->resync_lockres);
54519c5f 934 lockres_free(cinfo->bitmap_lockres);
f6a2dc64 935 unlock_all_bitmaps(mddev);
c4ce867f 936 dlm_release_lockspace(cinfo->lockspace, 2);
edb39c9d
GR
937 return 0;
938}
939
cf921cc1
GR
940/* slot_number(): Returns the MD slot number to use
941 * DLM starts the slot numbers from 1, wheras cluster-md
942 * wants the number to be from zero, so we deduct one
943 */
944static int slot_number(struct mddev *mddev)
945{
946 struct md_cluster_info *cinfo = mddev->cluster_info;
947
948 return cinfo->slot_number - 1;
949}
950
8b9277c8
GJ
951/*
952 * Check if the communication is already locked, else lock the communication
953 * channel.
954 * If it is already locked, token is in EX mode, and hence lock_token()
955 * should not be called.
956 */
293467aa
GR
957static int metadata_update_start(struct mddev *mddev)
958{
8b9277c8
GJ
959 struct md_cluster_info *cinfo = mddev->cluster_info;
960
961 wait_event(cinfo->wait,
962 !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
963 test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
964
965 /* If token is already locked, return 0 */
966 if (cinfo->token_lockres->mode == DLM_LOCK_EX)
967 return 0;
968
969 return lock_token(cinfo);
293467aa
GR
970}
971
972static int metadata_update_finish(struct mddev *mddev)
973{
974 struct md_cluster_info *cinfo = mddev->cluster_info;
975 struct cluster_msg cmsg;
70bcecdb
GR
976 struct md_rdev *rdev;
977 int ret = 0;
ba2746b0 978 int raid_slot = -1;
293467aa
GR
979
980 memset(&cmsg, 0, sizeof(cmsg));
981 cmsg.type = cpu_to_le32(METADATA_UPDATED);
70bcecdb
GR
982 /* Pick up a good active device number to send.
983 */
984 rdev_for_each(rdev, mddev)
985 if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
ba2746b0 986 raid_slot = rdev->desc_nr;
70bcecdb
GR
987 break;
988 }
ba2746b0
N
989 if (raid_slot >= 0) {
990 cmsg.raid_slot = cpu_to_le32(raid_slot);
70bcecdb 991 ret = __sendmsg(cinfo, &cmsg);
ba2746b0 992 } else
70bcecdb 993 pr_warn("md-cluster: No good device id found to send\n");
8b9277c8 994 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
293467aa
GR
995 unlock_comm(cinfo);
996 return ret;
997}
998
dbb64f86 999static void metadata_update_cancel(struct mddev *mddev)
293467aa
GR
1000{
1001 struct md_cluster_info *cinfo = mddev->cluster_info;
8b9277c8 1002 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
dbb64f86 1003 unlock_comm(cinfo);
293467aa
GR
1004}
1005
c186b128
GR
1006static int resync_start(struct mddev *mddev)
1007{
1008 struct md_cluster_info *cinfo = mddev->cluster_info;
c186b128
GR
1009 return dlm_lock_sync(cinfo->resync_lockres, DLM_LOCK_EX);
1010}
1011
c40f341f 1012static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
965400eb
GR
1013{
1014 struct md_cluster_info *cinfo = mddev->cluster_info;
ac277c6a 1015 struct resync_info ri;
aee177ac 1016 struct cluster_msg cmsg = {0};
965400eb 1017
ac277c6a
GR
1018 /* do not send zero again, if we have sent before */
1019 if (hi == 0) {
1020 memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
1021 if (le64_to_cpu(ri.hi) == 0)
1022 return 0;
1023 }
1024
30661b49 1025 add_resync_info(cinfo->bitmap_lockres, lo, hi);
c40f341f
GR
1026 /* Re-acquire the lock to refresh LVB */
1027 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
c40f341f 1028 cmsg.type = cpu_to_le32(RESYNCING);
965400eb
GR
1029 cmsg.low = cpu_to_le64(lo);
1030 cmsg.high = cpu_to_le64(hi);
c186b128 1031
965400eb
GR
1032 return sendmsg(cinfo, &cmsg);
1033}
1034
c186b128
GR
1035static int resync_finish(struct mddev *mddev)
1036{
1037 struct md_cluster_info *cinfo = mddev->cluster_info;
c186b128
GR
1038 dlm_unlock_sync(cinfo->resync_lockres);
1039 return resync_info_update(mddev, 0, 0);
1040}
1041
90382ed9
GR
1042static int area_resyncing(struct mddev *mddev, int direction,
1043 sector_t lo, sector_t hi)
589a1c49
GR
1044{
1045 struct md_cluster_info *cinfo = mddev->cluster_info;
1046 int ret = 0;
1047 struct suspend_info *s;
1048
90382ed9
GR
1049 if ((direction == READ) &&
1050 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
1051 return 1;
1052
589a1c49
GR
1053 spin_lock_irq(&cinfo->suspend_lock);
1054 if (list_empty(&cinfo->suspend_list))
1055 goto out;
1056 list_for_each_entry(s, &cinfo->suspend_list, list)
1057 if (hi > s->lo && lo < s->hi) {
1058 ret = 1;
1059 break;
1060 }
1061out:
1062 spin_unlock_irq(&cinfo->suspend_lock);
1063 return ret;
1064}
1065
dbb64f86
GR
1066/* add_new_disk() - initiates a disk add
1067 * However, if this fails before writing md_update_sb(),
1068 * add_new_disk_cancel() must be called to release token lock
1069 */
1070static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
1aee41f6
GR
1071{
1072 struct md_cluster_info *cinfo = mddev->cluster_info;
1073 struct cluster_msg cmsg;
1074 int ret = 0;
1075 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1076 char *uuid = sb->device_uuid;
1077
1078 memset(&cmsg, 0, sizeof(cmsg));
1079 cmsg.type = cpu_to_le32(NEWDISK);
1080 memcpy(cmsg.uuid, uuid, 16);
faeff83f 1081 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1aee41f6
GR
1082 lock_comm(cinfo);
1083 ret = __sendmsg(cinfo, &cmsg);
1084 if (ret)
1085 return ret;
1086 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
1087 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
1088 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
1089 /* Some node does not "see" the device */
1090 if (ret == -EAGAIN)
1091 ret = -ENOENT;
dbb64f86
GR
1092 if (ret)
1093 unlock_comm(cinfo);
8b9277c8 1094 else {
1aee41f6 1095 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
e19508fa
GJ
1096 /* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
1097 * will run soon after add_new_disk, the below path will be
1098 * invoked:
1099 * md_wakeup_thread(mddev->thread)
1100 * -> conf->thread (raid1d)
1101 * -> md_check_recovery -> md_update_sb
1102 * -> metadata_update_start/finish
1103 * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
1104 *
1105 * For other failure cases, metadata_update_cancel and
1106 * add_new_disk_cancel also clear below bit as well.
1107 * */
8b9277c8
GJ
1108 set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1109 wake_up(&cinfo->wait);
1110 }
1aee41f6
GR
1111 return ret;
1112}
1113
dbb64f86 1114static void add_new_disk_cancel(struct mddev *mddev)
1aee41f6 1115{
dbb64f86 1116 struct md_cluster_info *cinfo = mddev->cluster_info;
8b9277c8 1117 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
dbb64f86 1118 unlock_comm(cinfo);
1aee41f6
GR
1119}
1120
fa8259da 1121static int new_disk_ack(struct mddev *mddev, bool ack)
1aee41f6
GR
1122{
1123 struct md_cluster_info *cinfo = mddev->cluster_info;
1124
fa8259da
GR
1125 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
1126 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
1127 return -EINVAL;
1128 }
1129
1aee41f6
GR
1130 if (ack)
1131 dlm_unlock_sync(cinfo->no_new_dev_lockres);
1132 complete(&cinfo->newdisk_completion);
fa8259da 1133 return 0;
1aee41f6
GR
1134}
1135
88bcfef7
GR
1136static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1137{
aee177ac 1138 struct cluster_msg cmsg = {0};
88bcfef7 1139 struct md_cluster_info *cinfo = mddev->cluster_info;
faeff83f
GJ
1140 cmsg.type = cpu_to_le32(REMOVE);
1141 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
54a88392 1142 return sendmsg(cinfo, &cmsg);
88bcfef7
GR
1143}
1144
f6a2dc64
GJ
1145static int lock_all_bitmaps(struct mddev *mddev)
1146{
1147 int slot, my_slot, ret, held = 1, i = 0;
1148 char str[64];
1149 struct md_cluster_info *cinfo = mddev->cluster_info;
1150
1151 cinfo->other_bitmap_lockres = kzalloc((mddev->bitmap_info.nodes - 1) *
1152 sizeof(struct dlm_lock_resource *),
1153 GFP_KERNEL);
1154 if (!cinfo->other_bitmap_lockres) {
1155 pr_err("md: can't alloc mem for other bitmap locks\n");
1156 return 0;
1157 }
1158
1159 my_slot = slot_number(mddev);
1160 for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
1161 if (slot == my_slot)
1162 continue;
1163
1164 memset(str, '\0', 64);
1165 snprintf(str, 64, "bitmap%04d", slot);
1166 cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
1167 if (!cinfo->other_bitmap_lockres[i])
1168 return -ENOMEM;
1169
1170 cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
1171 ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
1172 if (ret)
1173 held = -1;
1174 i++;
1175 }
1176
1177 return held;
1178}
1179
1180static void unlock_all_bitmaps(struct mddev *mddev)
1181{
1182 struct md_cluster_info *cinfo = mddev->cluster_info;
1183 int i;
1184
1185 /* release other node's bitmap lock if they are existed */
1186 if (cinfo->other_bitmap_lockres) {
1187 for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
1188 if (cinfo->other_bitmap_lockres[i]) {
1189 dlm_unlock_sync(cinfo->other_bitmap_lockres[i]);
1190 lockres_free(cinfo->other_bitmap_lockres[i]);
1191 }
1192 }
1193 kfree(cinfo->other_bitmap_lockres);
1194 }
1195}
1196
97f6cd39
GR
1197static int gather_bitmaps(struct md_rdev *rdev)
1198{
1199 int sn, err;
1200 sector_t lo, hi;
aee177ac 1201 struct cluster_msg cmsg = {0};
97f6cd39
GR
1202 struct mddev *mddev = rdev->mddev;
1203 struct md_cluster_info *cinfo = mddev->cluster_info;
1204
faeff83f
GJ
1205 cmsg.type = cpu_to_le32(RE_ADD);
1206 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
97f6cd39
GR
1207 err = sendmsg(cinfo, &cmsg);
1208 if (err)
1209 goto out;
1210
1211 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
1212 if (sn == (cinfo->slot_number - 1))
1213 continue;
1214 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
1215 if (err) {
1216 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
1217 goto out;
1218 }
1219 if ((hi > 0) && (lo < mddev->recovery_cp))
1220 mddev->recovery_cp = lo;
1221 }
1222out:
1223 return err;
1224}
1225
edb39c9d
GR
1226static struct md_cluster_operations cluster_ops = {
1227 .join = join,
1228 .leave = leave,
cf921cc1 1229 .slot_number = slot_number,
c186b128
GR
1230 .resync_start = resync_start,
1231 .resync_finish = resync_finish,
96ae923a 1232 .resync_info_update = resync_info_update,
293467aa
GR
1233 .metadata_update_start = metadata_update_start,
1234 .metadata_update_finish = metadata_update_finish,
1235 .metadata_update_cancel = metadata_update_cancel,
589a1c49 1236 .area_resyncing = area_resyncing,
dbb64f86
GR
1237 .add_new_disk = add_new_disk,
1238 .add_new_disk_cancel = add_new_disk_cancel,
1aee41f6 1239 .new_disk_ack = new_disk_ack,
88bcfef7 1240 .remove_disk = remove_disk,
51e453ae 1241 .load_bitmaps = load_bitmaps,
97f6cd39 1242 .gather_bitmaps = gather_bitmaps,
f6a2dc64
GJ
1243 .lock_all_bitmaps = lock_all_bitmaps,
1244 .unlock_all_bitmaps = unlock_all_bitmaps,
edb39c9d
GR
1245};
1246
8e854e9c
GR
1247static int __init cluster_init(void)
1248{
1249 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1250 pr_info("Registering Cluster MD functions\n");
edb39c9d 1251 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
8e854e9c
GR
1252 return 0;
1253}
1254
1255static void cluster_exit(void)
1256{
edb39c9d 1257 unregister_md_cluster_operations();
8e854e9c
GR
1258}
1259
1260module_init(cluster_init);
1261module_exit(cluster_exit);
86b57277 1262MODULE_AUTHOR("SUSE");
8e854e9c
GR
1263MODULE_LICENSE("GPL");
1264MODULE_DESCRIPTION("Clustering support for MD");