md-cluster: Perform a lazy update
[linux-block.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. */
31};
32
96ae923a
GR
33struct suspend_info {
34 int slot;
35 sector_t lo;
36 sector_t hi;
37 struct list_head list;
38};
39
40struct resync_info {
41 __le64 lo;
42 __le64 hi;
43};
44
fa8259da
GR
45/* md_cluster_info flags */
46#define MD_CLUSTER_WAITING_FOR_NEWDISK 1
90382ed9 47#define MD_CLUSTER_SUSPEND_READ_BALANCING 2
eece075c 48#define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
fa8259da
GR
49
50
c4ce867f
GR
51struct md_cluster_info {
52 /* dlm lock space and resources for clustered raid. */
53 dlm_lockspace_t *lockspace;
cf921cc1
GR
54 int slot_number;
55 struct completion completion;
c4ce867f 56 struct mutex sb_mutex;
54519c5f 57 struct dlm_lock_resource *bitmap_lockres;
96ae923a
GR
58 struct list_head suspend_list;
59 spinlock_t suspend_lock;
e94987db
GR
60 struct md_thread *recovery_thread;
61 unsigned long recovery_map;
4664680c
GR
62 /* communication loc resources */
63 struct dlm_lock_resource *ack_lockres;
64 struct dlm_lock_resource *message_lockres;
65 struct dlm_lock_resource *token_lockres;
1aee41f6 66 struct dlm_lock_resource *no_new_dev_lockres;
4664680c 67 struct md_thread *recv_thread;
1aee41f6 68 struct completion newdisk_completion;
fa8259da 69 unsigned long state;
4664680c
GR
70};
71
72enum msg_type {
73 METADATA_UPDATED = 0,
74 RESYNCING,
1aee41f6 75 NEWDISK,
88bcfef7 76 REMOVE,
97f6cd39 77 RE_ADD,
dc737d7c 78 BITMAP_NEEDS_SYNC,
4664680c
GR
79};
80
81struct cluster_msg {
82 int type;
83 int slot;
1aee41f6 84 /* TODO: Unionize this for smaller footprint */
4664680c
GR
85 sector_t low;
86 sector_t high;
1aee41f6
GR
87 char uuid[16];
88 int raid_slot;
47741b7c
GR
89};
90
91static void sync_ast(void *arg)
92{
93 struct dlm_lock_resource *res;
94
95 res = (struct dlm_lock_resource *) arg;
96 complete(&res->completion);
97}
98
99static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
100{
101 int ret = 0;
102
47741b7c
GR
103 ret = dlm_lock(res->ls, mode, &res->lksb,
104 res->flags, res->name, strlen(res->name),
105 0, sync_ast, res, res->bast);
106 if (ret)
107 return ret;
108 wait_for_completion(&res->completion);
109 return res->lksb.sb_status;
110}
111
112static int dlm_unlock_sync(struct dlm_lock_resource *res)
113{
114 return dlm_lock_sync(res, DLM_LOCK_NL);
115}
116
c4ce867f 117static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
47741b7c
GR
118 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
119{
120 struct dlm_lock_resource *res = NULL;
121 int ret, namelen;
c4ce867f 122 struct md_cluster_info *cinfo = mddev->cluster_info;
47741b7c
GR
123
124 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
125 if (!res)
126 return NULL;
b83d51c0 127 init_completion(&res->completion);
c4ce867f
GR
128 res->ls = cinfo->lockspace;
129 res->mddev = mddev;
47741b7c
GR
130 namelen = strlen(name);
131 res->name = kzalloc(namelen + 1, GFP_KERNEL);
132 if (!res->name) {
133 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
134 goto out_err;
135 }
136 strlcpy(res->name, name, namelen + 1);
137 if (with_lvb) {
138 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
139 if (!res->lksb.sb_lvbptr) {
140 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
141 goto out_err;
142 }
143 res->flags = DLM_LKF_VALBLK;
144 }
145
146 if (bastfn)
147 res->bast = bastfn;
148
149 res->flags |= DLM_LKF_EXPEDITE;
150
151 ret = dlm_lock_sync(res, DLM_LOCK_NL);
152 if (ret) {
153 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
154 goto out_err;
155 }
156 res->flags &= ~DLM_LKF_EXPEDITE;
157 res->flags |= DLM_LKF_CONVERT;
158
159 return res;
160out_err:
161 kfree(res->lksb.sb_lvbptr);
162 kfree(res->name);
163 kfree(res);
164 return NULL;
165}
166
167static void lockres_free(struct dlm_lock_resource *res)
168{
b5ef5678
GJ
169 int ret;
170
47741b7c
GR
171 if (!res)
172 return;
173
b5ef5678
GJ
174 /* cancel a lock request or a conversion request that is blocked */
175 res->flags |= DLM_LKF_CANCEL;
176retry:
177 ret = dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res);
178 if (unlikely(ret != 0)) {
179 pr_info("%s: failed to unlock %s return %d\n", __func__, res->name, ret);
180
181 /* if a lock conversion is cancelled, then the lock is put
182 * back to grant queue, need to ensure it is unlocked */
183 if (ret == -DLM_ECANCEL)
184 goto retry;
185 }
186 res->flags &= ~DLM_LKF_CANCEL;
47741b7c
GR
187 wait_for_completion(&res->completion);
188
189 kfree(res->name);
190 kfree(res->lksb.sb_lvbptr);
191 kfree(res);
192}
8e854e9c 193
96ae923a
GR
194static void add_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres,
195 sector_t lo, sector_t hi)
196{
197 struct resync_info *ri;
198
199 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
200 ri->lo = cpu_to_le64(lo);
201 ri->hi = cpu_to_le64(hi);
202}
203
204static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
205{
206 struct resync_info ri;
207 struct suspend_info *s = NULL;
208 sector_t hi = 0;
209
210 dlm_lock_sync(lockres, DLM_LOCK_CR);
211 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
212 hi = le64_to_cpu(ri.hi);
213 if (ri.hi > 0) {
214 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
215 if (!s)
216 goto out;
217 s->hi = hi;
218 s->lo = le64_to_cpu(ri.lo);
219 }
220 dlm_unlock_sync(lockres);
221out:
222 return s;
223}
224
6dc69c9c 225static void recover_bitmaps(struct md_thread *thread)
e94987db
GR
226{
227 struct mddev *mddev = thread->mddev;
228 struct md_cluster_info *cinfo = mddev->cluster_info;
229 struct dlm_lock_resource *bm_lockres;
230 char str[64];
231 int slot, ret;
232 struct suspend_info *s, *tmp;
233 sector_t lo, hi;
234
235 while (cinfo->recovery_map) {
236 slot = fls64((u64)cinfo->recovery_map) - 1;
237
238 /* Clear suspend_area associated with the bitmap */
239 spin_lock_irq(&cinfo->suspend_lock);
240 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
241 if (slot == s->slot) {
242 list_del(&s->list);
243 kfree(s);
244 }
245 spin_unlock_irq(&cinfo->suspend_lock);
246
247 snprintf(str, 64, "bitmap%04d", slot);
248 bm_lockres = lockres_init(mddev, str, NULL, 1);
249 if (!bm_lockres) {
250 pr_err("md-cluster: Cannot initialize bitmaps\n");
251 goto clear_bit;
252 }
253
254 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
255 if (ret) {
256 pr_err("md-cluster: Could not DLM lock %s: %d\n",
257 str, ret);
258 goto clear_bit;
259 }
97f6cd39 260 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
4b26a08a 261 if (ret) {
e94987db 262 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
4b26a08a
GR
263 goto dlm_unlock;
264 }
265 if (hi > 0) {
266 /* TODO:Wait for current resync to get over */
267 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
268 if (lo < mddev->recovery_cp)
269 mddev->recovery_cp = lo;
270 md_check_recovery(mddev);
271 }
272dlm_unlock:
e94987db
GR
273 dlm_unlock_sync(bm_lockres);
274clear_bit:
275 clear_bit(slot, &cinfo->recovery_map);
276 }
277}
278
cf921cc1
GR
279static void recover_prep(void *arg)
280{
90382ed9
GR
281 struct mddev *mddev = arg;
282 struct md_cluster_info *cinfo = mddev->cluster_info;
283 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
cf921cc1
GR
284}
285
05cd0e51 286static void __recover_slot(struct mddev *mddev, int slot)
cf921cc1 287{
cf921cc1
GR
288 struct md_cluster_info *cinfo = mddev->cluster_info;
289
05cd0e51 290 set_bit(slot, &cinfo->recovery_map);
e94987db
GR
291 if (!cinfo->recovery_thread) {
292 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
293 mddev, "recover");
294 if (!cinfo->recovery_thread) {
295 pr_warn("md-cluster: Could not create recovery thread\n");
296 return;
297 }
298 }
299 md_wakeup_thread(cinfo->recovery_thread);
cf921cc1
GR
300}
301
05cd0e51
GJ
302static void recover_slot(void *arg, struct dlm_slot *slot)
303{
304 struct mddev *mddev = arg;
305 struct md_cluster_info *cinfo = mddev->cluster_info;
306
307 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
308 mddev->bitmap_info.cluster_name,
309 slot->nodeid, slot->slot,
310 cinfo->slot_number);
311 /* deduct one since dlm slot starts from one while the num of
312 * cluster-md begins with 0 */
313 __recover_slot(mddev, slot->slot - 1);
314}
315
cf921cc1
GR
316static void recover_done(void *arg, struct dlm_slot *slots,
317 int num_slots, int our_slot,
318 uint32_t generation)
319{
320 struct mddev *mddev = arg;
321 struct md_cluster_info *cinfo = mddev->cluster_info;
322
323 cinfo->slot_number = our_slot;
eece075c
GJ
324 /* completion is only need to be complete when node join cluster,
325 * it doesn't need to run during another node's failure */
326 if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
327 complete(&cinfo->completion);
328 clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
329 }
90382ed9 330 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
cf921cc1
GR
331}
332
eece075c
GJ
333/* the ops is called when node join the cluster, and do lock recovery
334 * if node failure occurs */
cf921cc1
GR
335static const struct dlm_lockspace_ops md_ls_ops = {
336 .recover_prep = recover_prep,
337 .recover_slot = recover_slot,
338 .recover_done = recover_done,
339};
340
4664680c
GR
341/*
342 * The BAST function for the ack lock resource
343 * This function wakes up the receive thread in
344 * order to receive and process the message.
345 */
346static void ack_bast(void *arg, int mode)
347{
348 struct dlm_lock_resource *res = (struct dlm_lock_resource *)arg;
349 struct md_cluster_info *cinfo = res->mddev->cluster_info;
350
351 if (mode == DLM_LOCK_EX)
352 md_wakeup_thread(cinfo->recv_thread);
353}
354
e59721cc
GR
355static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
356{
357 struct suspend_info *s, *tmp;
358
359 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
360 if (slot == s->slot) {
361 pr_info("%s:%d Deleting suspend_info: %d\n",
362 __func__, __LINE__, slot);
363 list_del(&s->list);
364 kfree(s);
365 break;
366 }
367}
368
b8ca846e 369static void remove_suspend_info(struct mddev *mddev, int slot)
e59721cc 370{
b8ca846e 371 struct md_cluster_info *cinfo = mddev->cluster_info;
e59721cc
GR
372 spin_lock_irq(&cinfo->suspend_lock);
373 __remove_suspend_info(cinfo, slot);
374 spin_unlock_irq(&cinfo->suspend_lock);
b8ca846e 375 mddev->pers->quiesce(mddev, 2);
e59721cc
GR
376}
377
378
9ed38ff5 379static void process_suspend_info(struct mddev *mddev,
e59721cc
GR
380 int slot, sector_t lo, sector_t hi)
381{
9ed38ff5 382 struct md_cluster_info *cinfo = mddev->cluster_info;
e59721cc
GR
383 struct suspend_info *s;
384
385 if (!hi) {
b8ca846e 386 remove_suspend_info(mddev, slot);
e59721cc
GR
387 return;
388 }
389 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
390 if (!s)
391 return;
392 s->slot = slot;
393 s->lo = lo;
394 s->hi = hi;
9ed38ff5
GR
395 mddev->pers->quiesce(mddev, 1);
396 mddev->pers->quiesce(mddev, 0);
e59721cc
GR
397 spin_lock_irq(&cinfo->suspend_lock);
398 /* Remove existing entry (if exists) before adding */
399 __remove_suspend_info(cinfo, slot);
400 list_add(&s->list, &cinfo->suspend_list);
401 spin_unlock_irq(&cinfo->suspend_lock);
b8ca846e 402 mddev->pers->quiesce(mddev, 2);
e59721cc
GR
403}
404
1aee41f6
GR
405static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
406{
407 char disk_uuid[64];
408 struct md_cluster_info *cinfo = mddev->cluster_info;
409 char event_name[] = "EVENT=ADD_DEVICE";
410 char raid_slot[16];
411 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
412 int len;
413
414 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
b89f704a 415 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
1aee41f6
GR
416 snprintf(raid_slot, 16, "RAID_DISK=%d", cmsg->raid_slot);
417 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
418 init_completion(&cinfo->newdisk_completion);
fa8259da 419 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
1aee41f6
GR
420 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
421 wait_for_completion_timeout(&cinfo->newdisk_completion,
422 NEW_DEV_TIMEOUT);
fa8259da 423 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
1aee41f6
GR
424}
425
426
427static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
428{
429 struct md_cluster_info *cinfo = mddev->cluster_info;
70bcecdb 430 md_reload_sb(mddev, le32_to_cpu(msg->raid_slot));
1aee41f6
GR
431 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
432}
433
88bcfef7
GR
434static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
435{
436 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev, msg->raid_slot);
437
438 if (rdev)
439 md_kick_rdev_from_array(rdev);
440 else
441 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n", __func__, __LINE__, msg->raid_slot);
442}
443
97f6cd39
GR
444static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
445{
446 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev, msg->raid_slot);
447
448 if (rdev && test_bit(Faulty, &rdev->flags))
449 clear_bit(Faulty, &rdev->flags);
450 else
451 pr_warn("%s: %d Could not find disk(%d) which is faulty", __func__, __LINE__, msg->raid_slot);
452}
453
4664680c
GR
454static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
455{
456 switch (msg->type) {
457 case METADATA_UPDATED:
458 pr_info("%s: %d Received message: METADATA_UPDATE from %d\n",
459 __func__, __LINE__, msg->slot);
1aee41f6 460 process_metadata_update(mddev, msg);
4664680c
GR
461 break;
462 case RESYNCING:
463 pr_info("%s: %d Received message: RESYNCING from %d\n",
464 __func__, __LINE__, msg->slot);
9ed38ff5 465 process_suspend_info(mddev, msg->slot,
e59721cc 466 msg->low, msg->high);
4664680c 467 break;
1aee41f6
GR
468 case NEWDISK:
469 pr_info("%s: %d Received message: NEWDISK from %d\n",
470 __func__, __LINE__, msg->slot);
471 process_add_new_disk(mddev, msg);
88bcfef7
GR
472 break;
473 case REMOVE:
474 pr_info("%s: %d Received REMOVE from %d\n",
475 __func__, __LINE__, msg->slot);
476 process_remove_disk(mddev, msg);
477 break;
97f6cd39
GR
478 case RE_ADD:
479 pr_info("%s: %d Received RE_ADD from %d\n",
480 __func__, __LINE__, msg->slot);
481 process_readd_disk(mddev, msg);
482 break;
dc737d7c
GJ
483 case BITMAP_NEEDS_SYNC:
484 pr_info("%s: %d Received BITMAP_NEEDS_SYNC from %d\n",
485 __func__, __LINE__, msg->slot);
486 __recover_slot(mddev, msg->slot);
487 break;
88bcfef7
GR
488 default:
489 pr_warn("%s:%d Received unknown message from %d\n",
490 __func__, __LINE__, msg->slot);
09dd1af2 491 }
4664680c
GR
492}
493
494/*
495 * thread for receiving message
496 */
497static void recv_daemon(struct md_thread *thread)
498{
499 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
500 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
501 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
502 struct cluster_msg msg;
b5ef5678 503 int ret;
4664680c
GR
504
505 /*get CR on Message*/
506 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
507 pr_err("md/raid1:failed to get CR on MESSAGE\n");
508 return;
509 }
510
511 /* read lvb and wake up thread to process this message_lockres */
512 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
513 process_recvd_msg(thread->mddev, &msg);
514
515 /*release CR on ack_lockres*/
b5ef5678
GJ
516 ret = dlm_unlock_sync(ack_lockres);
517 if (unlikely(ret != 0))
518 pr_info("unlock ack failed return %d\n", ret);
66099bb0 519 /*up-convert to PR on message_lockres*/
b5ef5678
GJ
520 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
521 if (unlikely(ret != 0))
522 pr_info("lock PR on msg failed return %d\n", ret);
4664680c 523 /*get CR on ack_lockres again*/
b5ef5678
GJ
524 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
525 if (unlikely(ret != 0))
526 pr_info("lock CR on ack failed return %d\n", ret);
4664680c 527 /*release CR on message_lockres*/
b5ef5678
GJ
528 ret = dlm_unlock_sync(message_lockres);
529 if (unlikely(ret != 0))
530 pr_info("unlock msg failed return %d\n", ret);
4664680c
GR
531}
532
601b515c
GR
533/* lock_comm()
534 * Takes the lock on the TOKEN lock resource so no other
535 * node can communicate while the operation is underway.
536 */
537static int lock_comm(struct md_cluster_info *cinfo)
538{
539 int error;
540
541 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
542 if (error)
543 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
544 __func__, __LINE__, error);
545 return error;
546}
547
548static void unlock_comm(struct md_cluster_info *cinfo)
549{
550 dlm_unlock_sync(cinfo->token_lockres);
551}
552
553/* __sendmsg()
554 * This function performs the actual sending of the message. This function is
555 * usually called after performing the encompassing operation
556 * The function:
557 * 1. Grabs the message lockresource in EX mode
558 * 2. Copies the message to the message LVB
66099bb0 559 * 3. Downconverts message lockresource to CW
601b515c
GR
560 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
561 * and the other nodes read the message. The thread will wait here until all other
562 * nodes have released ack lock resource.
563 * 5. Downconvert ack lockresource to CR
564 */
565static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
566{
567 int error;
568 int slot = cinfo->slot_number - 1;
569
570 cmsg->slot = cpu_to_le32(slot);
571 /*get EX on Message*/
572 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
573 if (error) {
574 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
575 goto failed_message;
576 }
577
578 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
579 sizeof(struct cluster_msg));
66099bb0
GJ
580 /*down-convert EX to CW on Message*/
581 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
601b515c 582 if (error) {
66099bb0 583 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
601b515c 584 error);
66099bb0 585 goto failed_ack;
601b515c
GR
586 }
587
588 /*up-convert CR to EX on Ack*/
589 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
590 if (error) {
591 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
592 error);
593 goto failed_ack;
594 }
595
596 /*down-convert EX to CR on Ack*/
597 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
598 if (error) {
599 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
600 error);
601 goto failed_ack;
602 }
603
604failed_ack:
b5ef5678
GJ
605 error = dlm_unlock_sync(cinfo->message_lockres);
606 if (unlikely(error != 0)) {
607 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
608 error);
609 /* in case the message can't be released due to some reason */
610 goto failed_ack;
611 }
601b515c
GR
612failed_message:
613 return error;
614}
615
616static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
617{
618 int ret;
619
620 lock_comm(cinfo);
621 ret = __sendmsg(cinfo, cmsg);
622 unlock_comm(cinfo);
623 return ret;
624}
625
96ae923a
GR
626static int gather_all_resync_info(struct mddev *mddev, int total_slots)
627{
628 struct md_cluster_info *cinfo = mddev->cluster_info;
629 int i, ret = 0;
630 struct dlm_lock_resource *bm_lockres;
631 struct suspend_info *s;
632 char str[64];
abb9b22a 633 sector_t lo, hi;
96ae923a
GR
634
635
636 for (i = 0; i < total_slots; i++) {
637 memset(str, '\0', 64);
638 snprintf(str, 64, "bitmap%04d", i);
639 bm_lockres = lockres_init(mddev, str, NULL, 1);
640 if (!bm_lockres)
641 return -ENOMEM;
642 if (i == (cinfo->slot_number - 1))
643 continue;
644
645 bm_lockres->flags |= DLM_LKF_NOQUEUE;
646 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
647 if (ret == -EAGAIN) {
648 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
649 s = read_resync_info(mddev, bm_lockres);
650 if (s) {
651 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
652 __func__, __LINE__,
653 (unsigned long long) s->lo,
654 (unsigned long long) s->hi, i);
655 spin_lock_irq(&cinfo->suspend_lock);
656 s->slot = i;
657 list_add(&s->list, &cinfo->suspend_list);
658 spin_unlock_irq(&cinfo->suspend_lock);
659 }
660 ret = 0;
661 lockres_free(bm_lockres);
662 continue;
663 }
6e6d9f2c
GJ
664 if (ret) {
665 lockres_free(bm_lockres);
96ae923a 666 goto out;
6e6d9f2c 667 }
abb9b22a
GJ
668
669 /* Read the disk bitmap sb and check if it needs recovery */
670 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
671 if (ret) {
672 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
673 lockres_free(bm_lockres);
674 continue;
675 }
676 if ((hi > 0) && (lo < mddev->recovery_cp)) {
677 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
678 mddev->recovery_cp = lo;
679 md_check_recovery(mddev);
680 }
681
96ae923a
GR
682 dlm_unlock_sync(bm_lockres);
683 lockres_free(bm_lockres);
684 }
685out:
686 return ret;
687}
688
edb39c9d
GR
689static int join(struct mddev *mddev, int nodes)
690{
c4ce867f 691 struct md_cluster_info *cinfo;
cf921cc1 692 int ret, ops_rv;
c4ce867f
GR
693 char str[64];
694
c4ce867f
GR
695 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
696 if (!cinfo)
697 return -ENOMEM;
698
9e3072e3
GJ
699 INIT_LIST_HEAD(&cinfo->suspend_list);
700 spin_lock_init(&cinfo->suspend_lock);
cf921cc1 701 init_completion(&cinfo->completion);
eece075c 702 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
cf921cc1
GR
703
704 mutex_init(&cinfo->sb_mutex);
705 mddev->cluster_info = cinfo;
706
c4ce867f 707 memset(str, 0, 64);
b89f704a 708 sprintf(str, "%pU", mddev->uuid);
cf921cc1
GR
709 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
710 DLM_LSFL_FS, LVB_SIZE,
711 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
c4ce867f
GR
712 if (ret)
713 goto err;
cf921cc1 714 wait_for_completion(&cinfo->completion);
8c58f02e
GJ
715 if (nodes < cinfo->slot_number) {
716 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
717 cinfo->slot_number, nodes);
b97e9257
GR
718 ret = -ERANGE;
719 goto err;
720 }
4664680c
GR
721 /* Initiate the communication resources */
722 ret = -ENOMEM;
723 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
724 if (!cinfo->recv_thread) {
725 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
726 goto err;
727 }
728 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
729 if (!cinfo->message_lockres)
730 goto err;
731 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
732 if (!cinfo->token_lockres)
733 goto err;
734 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
735 if (!cinfo->ack_lockres)
736 goto err;
1aee41f6
GR
737 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
738 if (!cinfo->no_new_dev_lockres)
739 goto err;
740
4664680c
GR
741 /* get sync CR lock on ACK. */
742 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
743 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
744 ret);
1aee41f6
GR
745 /* get sync CR lock on no-new-dev. */
746 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
747 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
748
54519c5f
GR
749
750 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
751 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
752 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
753 if (!cinfo->bitmap_lockres)
754 goto err;
755 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
756 pr_err("Failed to get bitmap lock\n");
757 ret = -EINVAL;
758 goto err;
759 }
760
96ae923a
GR
761 ret = gather_all_resync_info(mddev, nodes);
762 if (ret)
763 goto err;
764
edb39c9d 765 return 0;
c4ce867f 766err:
4664680c
GR
767 lockres_free(cinfo->message_lockres);
768 lockres_free(cinfo->token_lockres);
769 lockres_free(cinfo->ack_lockres);
1aee41f6 770 lockres_free(cinfo->no_new_dev_lockres);
96ae923a 771 lockres_free(cinfo->bitmap_lockres);
c4ce867f
GR
772 if (cinfo->lockspace)
773 dlm_release_lockspace(cinfo->lockspace, 2);
cf921cc1 774 mddev->cluster_info = NULL;
c4ce867f 775 kfree(cinfo);
c4ce867f 776 return ret;
edb39c9d
GR
777}
778
09995411
GJ
779static void resync_bitmap(struct mddev *mddev)
780{
781 struct md_cluster_info *cinfo = mddev->cluster_info;
782 struct cluster_msg cmsg = {0};
783 int err;
784
785 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
786 err = sendmsg(cinfo, &cmsg);
787 if (err)
788 pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
789 __func__, __LINE__, err);
790}
791
edb39c9d
GR
792static int leave(struct mddev *mddev)
793{
c4ce867f
GR
794 struct md_cluster_info *cinfo = mddev->cluster_info;
795
796 if (!cinfo)
797 return 0;
09995411
GJ
798
799 /* BITMAP_NEEDS_SYNC message should be sent when node
800 * is leaving the cluster with dirty bitmap, also we
801 * can only deliver it when dlm connection is available */
802 if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
803 resync_bitmap(mddev);
804
e94987db 805 md_unregister_thread(&cinfo->recovery_thread);
4664680c
GR
806 md_unregister_thread(&cinfo->recv_thread);
807 lockres_free(cinfo->message_lockres);
808 lockres_free(cinfo->token_lockres);
809 lockres_free(cinfo->ack_lockres);
1aee41f6 810 lockres_free(cinfo->no_new_dev_lockres);
54519c5f 811 lockres_free(cinfo->bitmap_lockres);
c4ce867f 812 dlm_release_lockspace(cinfo->lockspace, 2);
edb39c9d
GR
813 return 0;
814}
815
cf921cc1
GR
816/* slot_number(): Returns the MD slot number to use
817 * DLM starts the slot numbers from 1, wheras cluster-md
818 * wants the number to be from zero, so we deduct one
819 */
820static int slot_number(struct mddev *mddev)
821{
822 struct md_cluster_info *cinfo = mddev->cluster_info;
823
824 return cinfo->slot_number - 1;
825}
826
293467aa
GR
827static int metadata_update_start(struct mddev *mddev)
828{
829 return lock_comm(mddev->cluster_info);
830}
831
832static int metadata_update_finish(struct mddev *mddev)
833{
834 struct md_cluster_info *cinfo = mddev->cluster_info;
835 struct cluster_msg cmsg;
70bcecdb
GR
836 struct md_rdev *rdev;
837 int ret = 0;
293467aa
GR
838
839 memset(&cmsg, 0, sizeof(cmsg));
840 cmsg.type = cpu_to_le32(METADATA_UPDATED);
70bcecdb
GR
841 cmsg.raid_slot = -1;
842 /* Pick up a good active device number to send.
843 */
844 rdev_for_each(rdev, mddev)
845 if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
846 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
847 break;
848 }
849 if (cmsg.raid_slot >= 0)
850 ret = __sendmsg(cinfo, &cmsg);
851 else
852 pr_warn("md-cluster: No good device id found to send\n");
293467aa
GR
853 unlock_comm(cinfo);
854 return ret;
855}
856
857static int metadata_update_cancel(struct mddev *mddev)
858{
859 struct md_cluster_info *cinfo = mddev->cluster_info;
860
861 return dlm_unlock_sync(cinfo->token_lockres);
862}
863
c40f341f 864static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
965400eb
GR
865{
866 struct md_cluster_info *cinfo = mddev->cluster_info;
867 struct cluster_msg cmsg;
868 int slot = cinfo->slot_number - 1;
869
c40f341f
GR
870 add_resync_info(mddev, cinfo->bitmap_lockres, lo, hi);
871 /* Re-acquire the lock to refresh LVB */
872 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
965400eb
GR
873 pr_info("%s:%d lo: %llu hi: %llu\n", __func__, __LINE__,
874 (unsigned long long)lo,
875 (unsigned long long)hi);
c40f341f 876 cmsg.type = cpu_to_le32(RESYNCING);
965400eb
GR
877 cmsg.slot = cpu_to_le32(slot);
878 cmsg.low = cpu_to_le64(lo);
879 cmsg.high = cpu_to_le64(hi);
880 return sendmsg(cinfo, &cmsg);
881}
882
90382ed9
GR
883static int area_resyncing(struct mddev *mddev, int direction,
884 sector_t lo, sector_t hi)
589a1c49
GR
885{
886 struct md_cluster_info *cinfo = mddev->cluster_info;
887 int ret = 0;
888 struct suspend_info *s;
889
90382ed9
GR
890 if ((direction == READ) &&
891 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
892 return 1;
893
589a1c49
GR
894 spin_lock_irq(&cinfo->suspend_lock);
895 if (list_empty(&cinfo->suspend_list))
896 goto out;
897 list_for_each_entry(s, &cinfo->suspend_list, list)
898 if (hi > s->lo && lo < s->hi) {
899 ret = 1;
900 break;
901 }
902out:
903 spin_unlock_irq(&cinfo->suspend_lock);
904 return ret;
905}
906
1aee41f6
GR
907static int add_new_disk_start(struct mddev *mddev, struct md_rdev *rdev)
908{
909 struct md_cluster_info *cinfo = mddev->cluster_info;
910 struct cluster_msg cmsg;
911 int ret = 0;
912 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
913 char *uuid = sb->device_uuid;
914
915 memset(&cmsg, 0, sizeof(cmsg));
916 cmsg.type = cpu_to_le32(NEWDISK);
917 memcpy(cmsg.uuid, uuid, 16);
918 cmsg.raid_slot = rdev->desc_nr;
919 lock_comm(cinfo);
920 ret = __sendmsg(cinfo, &cmsg);
921 if (ret)
922 return ret;
923 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
924 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
925 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
926 /* Some node does not "see" the device */
927 if (ret == -EAGAIN)
928 ret = -ENOENT;
929 else
930 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
931 return ret;
932}
933
934static int add_new_disk_finish(struct mddev *mddev)
935{
1aee41f6
GR
936 /* Write sb and inform others */
937 md_update_sb(mddev, 1);
70bcecdb 938 return metadata_update_finish(mddev);
1aee41f6
GR
939}
940
fa8259da 941static int new_disk_ack(struct mddev *mddev, bool ack)
1aee41f6
GR
942{
943 struct md_cluster_info *cinfo = mddev->cluster_info;
944
fa8259da
GR
945 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
946 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
947 return -EINVAL;
948 }
949
1aee41f6
GR
950 if (ack)
951 dlm_unlock_sync(cinfo->no_new_dev_lockres);
952 complete(&cinfo->newdisk_completion);
fa8259da 953 return 0;
1aee41f6
GR
954}
955
88bcfef7
GR
956static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
957{
958 struct cluster_msg cmsg;
959 struct md_cluster_info *cinfo = mddev->cluster_info;
960 cmsg.type = REMOVE;
961 cmsg.raid_slot = rdev->desc_nr;
962 return __sendmsg(cinfo, &cmsg);
963}
964
97f6cd39
GR
965static int gather_bitmaps(struct md_rdev *rdev)
966{
967 int sn, err;
968 sector_t lo, hi;
969 struct cluster_msg cmsg;
970 struct mddev *mddev = rdev->mddev;
971 struct md_cluster_info *cinfo = mddev->cluster_info;
972
973 cmsg.type = RE_ADD;
974 cmsg.raid_slot = rdev->desc_nr;
975 err = sendmsg(cinfo, &cmsg);
976 if (err)
977 goto out;
978
979 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
980 if (sn == (cinfo->slot_number - 1))
981 continue;
982 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
983 if (err) {
984 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
985 goto out;
986 }
987 if ((hi > 0) && (lo < mddev->recovery_cp))
988 mddev->recovery_cp = lo;
989 }
990out:
991 return err;
992}
993
edb39c9d
GR
994static struct md_cluster_operations cluster_ops = {
995 .join = join,
996 .leave = leave,
cf921cc1 997 .slot_number = slot_number,
96ae923a 998 .resync_info_update = resync_info_update,
293467aa
GR
999 .metadata_update_start = metadata_update_start,
1000 .metadata_update_finish = metadata_update_finish,
1001 .metadata_update_cancel = metadata_update_cancel,
589a1c49 1002 .area_resyncing = area_resyncing,
1aee41f6
GR
1003 .add_new_disk_start = add_new_disk_start,
1004 .add_new_disk_finish = add_new_disk_finish,
1005 .new_disk_ack = new_disk_ack,
88bcfef7 1006 .remove_disk = remove_disk,
97f6cd39 1007 .gather_bitmaps = gather_bitmaps,
edb39c9d
GR
1008};
1009
8e854e9c
GR
1010static int __init cluster_init(void)
1011{
1012 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1013 pr_info("Registering Cluster MD functions\n");
edb39c9d 1014 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
8e854e9c
GR
1015 return 0;
1016}
1017
1018static void cluster_exit(void)
1019{
edb39c9d 1020 unregister_md_cluster_operations();
8e854e9c
GR
1021}
1022
1023module_init(cluster_init);
1024module_exit(cluster_exit);
1025MODULE_LICENSE("GPL");
1026MODULE_DESCRIPTION("Clustering support for MD");