2 * Copyright (C) 2015, SUSE
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)
12 #include <linux/module.h>
13 #include <linux/dlm.h>
14 #include <linux/sched.h>
15 #include <linux/raid/md_p.h>
18 #include "md-cluster.h"
21 #define NEW_DEV_TIMEOUT 5000
23 struct dlm_lock_resource {
26 char *name; /* lock name. */
27 uint32_t flags; /* flags to pass to dlm_lock() */
28 struct completion completion; /* completion for synchronized locking */
29 void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
30 struct mddev *mddev; /* pointing back to mddev. */
38 struct list_head list;
46 /* md_cluster_info flags */
47 #define MD_CLUSTER_WAITING_FOR_NEWDISK 1
48 #define MD_CLUSTER_SUSPEND_READ_BALANCING 2
49 #define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
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.
55 #define MD_CLUSTER_SEND_LOCK 4
56 /* If cluster operations must lock the communication channel,
57 * so as to perform extra operations (and no other operation
58 * is allowed on the MD, such as adding a disk. Token needs
59 * to be locked and held until the operation completes with
60 * a md_update_sb(), which would eventually release the lock.
62 #define MD_CLUSTER_SEND_LOCKED_ALREADY 5
65 struct md_cluster_info {
66 /* dlm lock space and resources for clustered raid. */
67 dlm_lockspace_t *lockspace;
69 struct completion completion;
70 struct mutex recv_mutex;
71 struct dlm_lock_resource *bitmap_lockres;
72 struct dlm_lock_resource **other_bitmap_lockres;
73 struct dlm_lock_resource *resync_lockres;
74 struct list_head suspend_list;
75 spinlock_t suspend_lock;
76 struct md_thread *recovery_thread;
77 unsigned long recovery_map;
78 /* communication loc resources */
79 struct dlm_lock_resource *ack_lockres;
80 struct dlm_lock_resource *message_lockres;
81 struct dlm_lock_resource *token_lockres;
82 struct dlm_lock_resource *no_new_dev_lockres;
83 struct md_thread *recv_thread;
84 struct completion newdisk_completion;
85 wait_queue_head_t wait;
101 /* TODO: Unionize this for smaller footprint */
108 static void sync_ast(void *arg)
110 struct dlm_lock_resource *res;
113 complete(&res->completion);
116 static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
120 ret = dlm_lock(res->ls, mode, &res->lksb,
121 res->flags, res->name, strlen(res->name),
122 0, sync_ast, res, res->bast);
125 wait_for_completion(&res->completion);
126 if (res->lksb.sb_status == 0)
128 return res->lksb.sb_status;
131 static int dlm_unlock_sync(struct dlm_lock_resource *res)
133 return dlm_lock_sync(res, DLM_LOCK_NL);
136 static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
137 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
139 struct dlm_lock_resource *res = NULL;
141 struct md_cluster_info *cinfo = mddev->cluster_info;
143 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
146 init_completion(&res->completion);
147 res->ls = cinfo->lockspace;
149 res->mode = DLM_LOCK_IV;
150 namelen = strlen(name);
151 res->name = kzalloc(namelen + 1, GFP_KERNEL);
153 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
156 strlcpy(res->name, name, namelen + 1);
158 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
159 if (!res->lksb.sb_lvbptr) {
160 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
163 res->flags = DLM_LKF_VALBLK;
169 res->flags |= DLM_LKF_EXPEDITE;
171 ret = dlm_lock_sync(res, DLM_LOCK_NL);
173 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
176 res->flags &= ~DLM_LKF_EXPEDITE;
177 res->flags |= DLM_LKF_CONVERT;
181 kfree(res->lksb.sb_lvbptr);
187 static void lockres_free(struct dlm_lock_resource *res)
194 /* cancel a lock request or a conversion request that is blocked */
195 res->flags |= DLM_LKF_CANCEL;
197 ret = dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res);
198 if (unlikely(ret != 0)) {
199 pr_info("%s: failed to unlock %s return %d\n", __func__, res->name, ret);
201 /* if a lock conversion is cancelled, then the lock is put
202 * back to grant queue, need to ensure it is unlocked */
203 if (ret == -DLM_ECANCEL)
206 res->flags &= ~DLM_LKF_CANCEL;
207 wait_for_completion(&res->completion);
210 kfree(res->lksb.sb_lvbptr);
214 static void add_resync_info(struct dlm_lock_resource *lockres,
215 sector_t lo, sector_t hi)
217 struct resync_info *ri;
219 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
220 ri->lo = cpu_to_le64(lo);
221 ri->hi = cpu_to_le64(hi);
224 static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
226 struct resync_info ri;
227 struct suspend_info *s = NULL;
230 dlm_lock_sync(lockres, DLM_LOCK_CR);
231 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
232 hi = le64_to_cpu(ri.hi);
234 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
238 s->lo = le64_to_cpu(ri.lo);
240 dlm_unlock_sync(lockres);
245 static void recover_bitmaps(struct md_thread *thread)
247 struct mddev *mddev = thread->mddev;
248 struct md_cluster_info *cinfo = mddev->cluster_info;
249 struct dlm_lock_resource *bm_lockres;
252 struct suspend_info *s, *tmp;
255 while (cinfo->recovery_map) {
256 slot = fls64((u64)cinfo->recovery_map) - 1;
258 /* Clear suspend_area associated with the bitmap */
259 spin_lock_irq(&cinfo->suspend_lock);
260 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
261 if (slot == s->slot) {
265 spin_unlock_irq(&cinfo->suspend_lock);
267 snprintf(str, 64, "bitmap%04d", slot);
268 bm_lockres = lockres_init(mddev, str, NULL, 1);
270 pr_err("md-cluster: Cannot initialize bitmaps\n");
274 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
276 pr_err("md-cluster: Could not DLM lock %s: %d\n",
280 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
282 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
286 /* TODO:Wait for current resync to get over */
287 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
288 if (lo < mddev->recovery_cp)
289 mddev->recovery_cp = lo;
290 md_check_recovery(mddev);
293 dlm_unlock_sync(bm_lockres);
295 clear_bit(slot, &cinfo->recovery_map);
299 static void recover_prep(void *arg)
301 struct mddev *mddev = arg;
302 struct md_cluster_info *cinfo = mddev->cluster_info;
303 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
306 static void __recover_slot(struct mddev *mddev, int slot)
308 struct md_cluster_info *cinfo = mddev->cluster_info;
310 set_bit(slot, &cinfo->recovery_map);
311 if (!cinfo->recovery_thread) {
312 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
314 if (!cinfo->recovery_thread) {
315 pr_warn("md-cluster: Could not create recovery thread\n");
319 md_wakeup_thread(cinfo->recovery_thread);
322 static void recover_slot(void *arg, struct dlm_slot *slot)
324 struct mddev *mddev = arg;
325 struct md_cluster_info *cinfo = mddev->cluster_info;
327 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
328 mddev->bitmap_info.cluster_name,
329 slot->nodeid, slot->slot,
331 /* deduct one since dlm slot starts from one while the num of
332 * cluster-md begins with 0 */
333 __recover_slot(mddev, slot->slot - 1);
336 static void recover_done(void *arg, struct dlm_slot *slots,
337 int num_slots, int our_slot,
340 struct mddev *mddev = arg;
341 struct md_cluster_info *cinfo = mddev->cluster_info;
343 cinfo->slot_number = our_slot;
344 /* completion is only need to be complete when node join cluster,
345 * it doesn't need to run during another node's failure */
346 if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
347 complete(&cinfo->completion);
348 clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
350 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
353 /* the ops is called when node join the cluster, and do lock recovery
354 * if node failure occurs */
355 static const struct dlm_lockspace_ops md_ls_ops = {
356 .recover_prep = recover_prep,
357 .recover_slot = recover_slot,
358 .recover_done = recover_done,
362 * The BAST function for the ack lock resource
363 * This function wakes up the receive thread in
364 * order to receive and process the message.
366 static void ack_bast(void *arg, int mode)
368 struct dlm_lock_resource *res = arg;
369 struct md_cluster_info *cinfo = res->mddev->cluster_info;
371 if (mode == DLM_LOCK_EX)
372 md_wakeup_thread(cinfo->recv_thread);
375 static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
377 struct suspend_info *s, *tmp;
379 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
380 if (slot == s->slot) {
387 static void remove_suspend_info(struct mddev *mddev, int slot)
389 struct md_cluster_info *cinfo = mddev->cluster_info;
390 spin_lock_irq(&cinfo->suspend_lock);
391 __remove_suspend_info(cinfo, slot);
392 spin_unlock_irq(&cinfo->suspend_lock);
393 mddev->pers->quiesce(mddev, 2);
397 static void process_suspend_info(struct mddev *mddev,
398 int slot, sector_t lo, sector_t hi)
400 struct md_cluster_info *cinfo = mddev->cluster_info;
401 struct suspend_info *s;
404 remove_suspend_info(mddev, slot);
405 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
406 md_wakeup_thread(mddev->thread);
409 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
415 mddev->pers->quiesce(mddev, 1);
416 mddev->pers->quiesce(mddev, 0);
417 spin_lock_irq(&cinfo->suspend_lock);
418 /* Remove existing entry (if exists) before adding */
419 __remove_suspend_info(cinfo, slot);
420 list_add(&s->list, &cinfo->suspend_list);
421 spin_unlock_irq(&cinfo->suspend_lock);
422 mddev->pers->quiesce(mddev, 2);
425 static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
428 struct md_cluster_info *cinfo = mddev->cluster_info;
429 char event_name[] = "EVENT=ADD_DEVICE";
431 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
434 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
435 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
436 snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
437 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
438 init_completion(&cinfo->newdisk_completion);
439 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
440 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
441 wait_for_completion_timeout(&cinfo->newdisk_completion,
443 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
447 static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
449 struct md_cluster_info *cinfo = mddev->cluster_info;
450 mddev->good_device_nr = le32_to_cpu(msg->raid_slot);
451 set_bit(MD_RELOAD_SB, &mddev->flags);
452 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
453 md_wakeup_thread(mddev->thread);
456 static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
458 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
459 le32_to_cpu(msg->raid_slot));
462 set_bit(ClusterRemove, &rdev->flags);
463 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
464 md_wakeup_thread(mddev->thread);
467 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
468 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
471 static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
473 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
474 le32_to_cpu(msg->raid_slot));
476 if (rdev && test_bit(Faulty, &rdev->flags))
477 clear_bit(Faulty, &rdev->flags);
479 pr_warn("%s: %d Could not find disk(%d) which is faulty",
480 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
483 static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
485 if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
486 "node %d received it's own msg\n", le32_to_cpu(msg->slot)))
488 switch (le32_to_cpu(msg->type)) {
489 case METADATA_UPDATED:
490 process_metadata_update(mddev, msg);
493 process_suspend_info(mddev, le32_to_cpu(msg->slot),
494 le64_to_cpu(msg->low),
495 le64_to_cpu(msg->high));
498 process_add_new_disk(mddev, msg);
501 process_remove_disk(mddev, msg);
504 process_readd_disk(mddev, msg);
506 case BITMAP_NEEDS_SYNC:
507 __recover_slot(mddev, le32_to_cpu(msg->slot));
510 pr_warn("%s:%d Received unknown message from %d\n",
511 __func__, __LINE__, msg->slot);
516 * thread for receiving message
518 static void recv_daemon(struct md_thread *thread)
520 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
521 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
522 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
523 struct cluster_msg msg;
526 mutex_lock(&cinfo->recv_mutex);
527 /*get CR on Message*/
528 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
529 pr_err("md/raid1:failed to get CR on MESSAGE\n");
530 mutex_unlock(&cinfo->recv_mutex);
534 /* read lvb and wake up thread to process this message_lockres */
535 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
536 process_recvd_msg(thread->mddev, &msg);
538 /*release CR on ack_lockres*/
539 ret = dlm_unlock_sync(ack_lockres);
540 if (unlikely(ret != 0))
541 pr_info("unlock ack failed return %d\n", ret);
542 /*up-convert to PR on message_lockres*/
543 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
544 if (unlikely(ret != 0))
545 pr_info("lock PR on msg failed return %d\n", ret);
546 /*get CR on ack_lockres again*/
547 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
548 if (unlikely(ret != 0))
549 pr_info("lock CR on ack failed return %d\n", ret);
550 /*release CR on message_lockres*/
551 ret = dlm_unlock_sync(message_lockres);
552 if (unlikely(ret != 0))
553 pr_info("unlock msg failed return %d\n", ret);
554 mutex_unlock(&cinfo->recv_mutex);
558 * Takes the lock on the TOKEN lock resource so no other
559 * node can communicate while the operation is underway.
561 static int lock_token(struct md_cluster_info *cinfo)
565 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
567 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
568 __func__, __LINE__, error);
570 /* Lock the receive sequence */
571 mutex_lock(&cinfo->recv_mutex);
576 * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
578 static int lock_comm(struct md_cluster_info *cinfo)
580 wait_event(cinfo->wait,
581 !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));
583 return lock_token(cinfo);
586 static void unlock_comm(struct md_cluster_info *cinfo)
588 WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
589 mutex_unlock(&cinfo->recv_mutex);
590 dlm_unlock_sync(cinfo->token_lockres);
591 clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
592 wake_up(&cinfo->wait);
596 * This function performs the actual sending of the message. This function is
597 * usually called after performing the encompassing operation
599 * 1. Grabs the message lockresource in EX mode
600 * 2. Copies the message to the message LVB
601 * 3. Downconverts message lockresource to CW
602 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
603 * and the other nodes read the message. The thread will wait here until all other
604 * nodes have released ack lock resource.
605 * 5. Downconvert ack lockresource to CR
607 static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
610 int slot = cinfo->slot_number - 1;
612 cmsg->slot = cpu_to_le32(slot);
613 /*get EX on Message*/
614 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
616 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
620 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
621 sizeof(struct cluster_msg));
622 /*down-convert EX to CW on Message*/
623 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
625 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
630 /*up-convert CR to EX on Ack*/
631 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
633 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
638 /*down-convert EX to CR on Ack*/
639 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
641 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
647 error = dlm_unlock_sync(cinfo->message_lockres);
648 if (unlikely(error != 0)) {
649 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
651 /* in case the message can't be released due to some reason */
658 static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
663 ret = __sendmsg(cinfo, cmsg);
668 static int gather_all_resync_info(struct mddev *mddev, int total_slots)
670 struct md_cluster_info *cinfo = mddev->cluster_info;
672 struct dlm_lock_resource *bm_lockres;
673 struct suspend_info *s;
678 for (i = 0; i < total_slots; i++) {
679 memset(str, '\0', 64);
680 snprintf(str, 64, "bitmap%04d", i);
681 bm_lockres = lockres_init(mddev, str, NULL, 1);
684 if (i == (cinfo->slot_number - 1))
687 bm_lockres->flags |= DLM_LKF_NOQUEUE;
688 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
689 if (ret == -EAGAIN) {
690 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
691 s = read_resync_info(mddev, bm_lockres);
693 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
695 (unsigned long long) s->lo,
696 (unsigned long long) s->hi, i);
697 spin_lock_irq(&cinfo->suspend_lock);
699 list_add(&s->list, &cinfo->suspend_list);
700 spin_unlock_irq(&cinfo->suspend_lock);
703 lockres_free(bm_lockres);
707 lockres_free(bm_lockres);
711 /* Read the disk bitmap sb and check if it needs recovery */
712 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
714 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
715 lockres_free(bm_lockres);
718 if ((hi > 0) && (lo < mddev->recovery_cp)) {
719 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
720 mddev->recovery_cp = lo;
721 md_check_recovery(mddev);
724 dlm_unlock_sync(bm_lockres);
725 lockres_free(bm_lockres);
731 static int join(struct mddev *mddev, int nodes)
733 struct md_cluster_info *cinfo;
737 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
741 INIT_LIST_HEAD(&cinfo->suspend_list);
742 spin_lock_init(&cinfo->suspend_lock);
743 init_completion(&cinfo->completion);
744 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
745 init_waitqueue_head(&cinfo->wait);
746 mutex_init(&cinfo->recv_mutex);
748 mddev->cluster_info = cinfo;
751 sprintf(str, "%pU", mddev->uuid);
752 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
753 DLM_LSFL_FS, LVB_SIZE,
754 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
757 wait_for_completion(&cinfo->completion);
758 if (nodes < cinfo->slot_number) {
759 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
760 cinfo->slot_number, nodes);
764 /* Initiate the communication resources */
766 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
767 if (!cinfo->recv_thread) {
768 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
771 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
772 if (!cinfo->message_lockres)
774 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
775 if (!cinfo->token_lockres)
777 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
778 if (!cinfo->ack_lockres)
780 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
781 if (!cinfo->no_new_dev_lockres)
784 /* get sync CR lock on ACK. */
785 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
786 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
788 /* get sync CR lock on no-new-dev. */
789 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
790 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
793 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
794 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
795 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
796 if (!cinfo->bitmap_lockres)
798 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
799 pr_err("Failed to get bitmap lock\n");
804 cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
805 if (!cinfo->resync_lockres)
808 ret = gather_all_resync_info(mddev, nodes);
814 lockres_free(cinfo->message_lockres);
815 lockres_free(cinfo->token_lockres);
816 lockres_free(cinfo->ack_lockres);
817 lockres_free(cinfo->no_new_dev_lockres);
818 lockres_free(cinfo->resync_lockres);
819 lockres_free(cinfo->bitmap_lockres);
820 if (cinfo->lockspace)
821 dlm_release_lockspace(cinfo->lockspace, 2);
822 mddev->cluster_info = NULL;
827 static void resync_bitmap(struct mddev *mddev)
829 struct md_cluster_info *cinfo = mddev->cluster_info;
830 struct cluster_msg cmsg = {0};
833 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
834 err = sendmsg(cinfo, &cmsg);
836 pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
837 __func__, __LINE__, err);
840 static void unlock_all_bitmaps(struct mddev *mddev);
841 static int leave(struct mddev *mddev)
843 struct md_cluster_info *cinfo = mddev->cluster_info;
848 /* BITMAP_NEEDS_SYNC message should be sent when node
849 * is leaving the cluster with dirty bitmap, also we
850 * can only deliver it when dlm connection is available */
851 if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
852 resync_bitmap(mddev);
854 md_unregister_thread(&cinfo->recovery_thread);
855 md_unregister_thread(&cinfo->recv_thread);
856 lockres_free(cinfo->message_lockres);
857 lockres_free(cinfo->token_lockres);
858 lockres_free(cinfo->ack_lockres);
859 lockres_free(cinfo->no_new_dev_lockres);
860 lockres_free(cinfo->bitmap_lockres);
861 unlock_all_bitmaps(mddev);
862 dlm_release_lockspace(cinfo->lockspace, 2);
866 /* slot_number(): Returns the MD slot number to use
867 * DLM starts the slot numbers from 1, wheras cluster-md
868 * wants the number to be from zero, so we deduct one
870 static int slot_number(struct mddev *mddev)
872 struct md_cluster_info *cinfo = mddev->cluster_info;
874 return cinfo->slot_number - 1;
878 * Check if the communication is already locked, else lock the communication
880 * If it is already locked, token is in EX mode, and hence lock_token()
881 * should not be called.
883 static int metadata_update_start(struct mddev *mddev)
885 struct md_cluster_info *cinfo = mddev->cluster_info;
887 wait_event(cinfo->wait,
888 !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
889 test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
891 /* If token is already locked, return 0 */
892 if (cinfo->token_lockres->mode == DLM_LOCK_EX)
895 return lock_token(cinfo);
898 static int metadata_update_finish(struct mddev *mddev)
900 struct md_cluster_info *cinfo = mddev->cluster_info;
901 struct cluster_msg cmsg;
902 struct md_rdev *rdev;
906 memset(&cmsg, 0, sizeof(cmsg));
907 cmsg.type = cpu_to_le32(METADATA_UPDATED);
908 /* Pick up a good active device number to send.
910 rdev_for_each(rdev, mddev)
911 if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
912 raid_slot = rdev->desc_nr;
915 if (raid_slot >= 0) {
916 cmsg.raid_slot = cpu_to_le32(raid_slot);
917 ret = __sendmsg(cinfo, &cmsg);
919 pr_warn("md-cluster: No good device id found to send\n");
920 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
925 static void metadata_update_cancel(struct mddev *mddev)
927 struct md_cluster_info *cinfo = mddev->cluster_info;
928 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
932 static int resync_start(struct mddev *mddev)
934 struct md_cluster_info *cinfo = mddev->cluster_info;
935 cinfo->resync_lockres->flags |= DLM_LKF_NOQUEUE;
936 return dlm_lock_sync(cinfo->resync_lockres, DLM_LOCK_EX);
939 static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
941 struct md_cluster_info *cinfo = mddev->cluster_info;
942 struct resync_info ri;
943 struct cluster_msg cmsg = {0};
945 /* do not send zero again, if we have sent before */
947 memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
948 if (le64_to_cpu(ri.hi) == 0)
952 add_resync_info(cinfo->bitmap_lockres, lo, hi);
953 /* Re-acquire the lock to refresh LVB */
954 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
955 cmsg.type = cpu_to_le32(RESYNCING);
956 cmsg.low = cpu_to_le64(lo);
957 cmsg.high = cpu_to_le64(hi);
959 return sendmsg(cinfo, &cmsg);
962 static int resync_finish(struct mddev *mddev)
964 struct md_cluster_info *cinfo = mddev->cluster_info;
965 cinfo->resync_lockres->flags &= ~DLM_LKF_NOQUEUE;
966 dlm_unlock_sync(cinfo->resync_lockres);
967 return resync_info_update(mddev, 0, 0);
970 static int area_resyncing(struct mddev *mddev, int direction,
971 sector_t lo, sector_t hi)
973 struct md_cluster_info *cinfo = mddev->cluster_info;
975 struct suspend_info *s;
977 if ((direction == READ) &&
978 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
981 spin_lock_irq(&cinfo->suspend_lock);
982 if (list_empty(&cinfo->suspend_list))
984 list_for_each_entry(s, &cinfo->suspend_list, list)
985 if (hi > s->lo && lo < s->hi) {
990 spin_unlock_irq(&cinfo->suspend_lock);
994 /* add_new_disk() - initiates a disk add
995 * However, if this fails before writing md_update_sb(),
996 * add_new_disk_cancel() must be called to release token lock
998 static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
1000 struct md_cluster_info *cinfo = mddev->cluster_info;
1001 struct cluster_msg cmsg;
1003 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1004 char *uuid = sb->device_uuid;
1006 memset(&cmsg, 0, sizeof(cmsg));
1007 cmsg.type = cpu_to_le32(NEWDISK);
1008 memcpy(cmsg.uuid, uuid, 16);
1009 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1011 ret = __sendmsg(cinfo, &cmsg);
1014 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
1015 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
1016 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
1017 /* Some node does not "see" the device */
1023 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
1024 set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1025 wake_up(&cinfo->wait);
1030 static void add_new_disk_cancel(struct mddev *mddev)
1032 struct md_cluster_info *cinfo = mddev->cluster_info;
1033 clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
1037 static int new_disk_ack(struct mddev *mddev, bool ack)
1039 struct md_cluster_info *cinfo = mddev->cluster_info;
1041 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
1042 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
1047 dlm_unlock_sync(cinfo->no_new_dev_lockres);
1048 complete(&cinfo->newdisk_completion);
1052 static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1054 struct cluster_msg cmsg = {0};
1055 struct md_cluster_info *cinfo = mddev->cluster_info;
1056 cmsg.type = cpu_to_le32(REMOVE);
1057 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1058 return sendmsg(cinfo, &cmsg);
1061 static int lock_all_bitmaps(struct mddev *mddev)
1063 int slot, my_slot, ret, held = 1, i = 0;
1065 struct md_cluster_info *cinfo = mddev->cluster_info;
1067 cinfo->other_bitmap_lockres = kzalloc((mddev->bitmap_info.nodes - 1) *
1068 sizeof(struct dlm_lock_resource *),
1070 if (!cinfo->other_bitmap_lockres) {
1071 pr_err("md: can't alloc mem for other bitmap locks\n");
1075 my_slot = slot_number(mddev);
1076 for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
1077 if (slot == my_slot)
1080 memset(str, '\0', 64);
1081 snprintf(str, 64, "bitmap%04d", slot);
1082 cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
1083 if (!cinfo->other_bitmap_lockres[i])
1086 cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
1087 ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
1096 static void unlock_all_bitmaps(struct mddev *mddev)
1098 struct md_cluster_info *cinfo = mddev->cluster_info;
1101 /* release other node's bitmap lock if they are existed */
1102 if (cinfo->other_bitmap_lockres) {
1103 for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
1104 if (cinfo->other_bitmap_lockres[i]) {
1105 dlm_unlock_sync(cinfo->other_bitmap_lockres[i]);
1106 lockres_free(cinfo->other_bitmap_lockres[i]);
1109 kfree(cinfo->other_bitmap_lockres);
1113 static int gather_bitmaps(struct md_rdev *rdev)
1117 struct cluster_msg cmsg = {0};
1118 struct mddev *mddev = rdev->mddev;
1119 struct md_cluster_info *cinfo = mddev->cluster_info;
1121 cmsg.type = cpu_to_le32(RE_ADD);
1122 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
1123 err = sendmsg(cinfo, &cmsg);
1127 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
1128 if (sn == (cinfo->slot_number - 1))
1130 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
1132 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
1135 if ((hi > 0) && (lo < mddev->recovery_cp))
1136 mddev->recovery_cp = lo;
1142 static struct md_cluster_operations cluster_ops = {
1145 .slot_number = slot_number,
1146 .resync_start = resync_start,
1147 .resync_finish = resync_finish,
1148 .resync_info_update = resync_info_update,
1149 .metadata_update_start = metadata_update_start,
1150 .metadata_update_finish = metadata_update_finish,
1151 .metadata_update_cancel = metadata_update_cancel,
1152 .area_resyncing = area_resyncing,
1153 .add_new_disk = add_new_disk,
1154 .add_new_disk_cancel = add_new_disk_cancel,
1155 .new_disk_ack = new_disk_ack,
1156 .remove_disk = remove_disk,
1157 .gather_bitmaps = gather_bitmaps,
1158 .lock_all_bitmaps = lock_all_bitmaps,
1159 .unlock_all_bitmaps = unlock_all_bitmaps,
1162 static int __init cluster_init(void)
1164 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1165 pr_info("Registering Cluster MD functions\n");
1166 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
1170 static void cluster_exit(void)
1172 unregister_md_cluster_operations();
1175 module_init(cluster_init);
1176 module_exit(cluster_exit);
1177 MODULE_AUTHOR("SUSE");
1178 MODULE_LICENSE("GPL");
1179 MODULE_DESCRIPTION("Clustering support for MD");