blob: d85a6ca4443ee58ce70d54d78cabb27eb1053e78 [file] [log] [blame]
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001/*
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>
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060013#include <linux/dlm.h>
14#include <linux/sched.h>
15#include "md.h"
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050016#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050017#include "md-cluster.h"
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060018
19#define LVB_SIZE 64
20
21struct dlm_lock_resource {
22 dlm_lockspace_t *ls;
23 struct dlm_lksb lksb;
24 char *name; /* lock name. */
25 uint32_t flags; /* flags to pass to dlm_lock() */
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060026 struct completion completion; /* completion for synchronized locking */
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050027 void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
28 struct mddev *mddev; /* pointing back to mddev. */
29};
30
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050031struct suspend_info {
32 int slot;
33 sector_t lo;
34 sector_t hi;
35 struct list_head list;
36};
37
38struct resync_info {
39 __le64 lo;
40 __le64 hi;
41};
42
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050043struct md_cluster_info {
44 /* dlm lock space and resources for clustered raid. */
45 dlm_lockspace_t *lockspace;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -050046 int slot_number;
47 struct completion completion;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050048 struct dlm_lock_resource *sb_lock;
49 struct mutex sb_mutex;
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -050050 struct dlm_lock_resource *bitmap_lockres;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050051 struct list_head suspend_list;
52 spinlock_t suspend_lock;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050053 struct md_thread *recovery_thread;
54 unsigned long recovery_map;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050055 /* communication loc resources */
56 struct dlm_lock_resource *ack_lockres;
57 struct dlm_lock_resource *message_lockres;
58 struct dlm_lock_resource *token_lockres;
59 struct md_thread *recv_thread;
60};
61
62enum msg_type {
63 METADATA_UPDATED = 0,
64 RESYNCING,
65};
66
67struct cluster_msg {
68 int type;
69 int slot;
70 sector_t low;
71 sector_t high;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060072};
73
74static void sync_ast(void *arg)
75{
76 struct dlm_lock_resource *res;
77
78 res = (struct dlm_lock_resource *) arg;
79 complete(&res->completion);
80}
81
82static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
83{
84 int ret = 0;
85
86 init_completion(&res->completion);
87 ret = dlm_lock(res->ls, mode, &res->lksb,
88 res->flags, res->name, strlen(res->name),
89 0, sync_ast, res, res->bast);
90 if (ret)
91 return ret;
92 wait_for_completion(&res->completion);
93 return res->lksb.sb_status;
94}
95
96static int dlm_unlock_sync(struct dlm_lock_resource *res)
97{
98 return dlm_lock_sync(res, DLM_LOCK_NL);
99}
100
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500101static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600102 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
103{
104 struct dlm_lock_resource *res = NULL;
105 int ret, namelen;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500106 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600107
108 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
109 if (!res)
110 return NULL;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500111 res->ls = cinfo->lockspace;
112 res->mddev = mddev;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600113 namelen = strlen(name);
114 res->name = kzalloc(namelen + 1, GFP_KERNEL);
115 if (!res->name) {
116 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
117 goto out_err;
118 }
119 strlcpy(res->name, name, namelen + 1);
120 if (with_lvb) {
121 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
122 if (!res->lksb.sb_lvbptr) {
123 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
124 goto out_err;
125 }
126 res->flags = DLM_LKF_VALBLK;
127 }
128
129 if (bastfn)
130 res->bast = bastfn;
131
132 res->flags |= DLM_LKF_EXPEDITE;
133
134 ret = dlm_lock_sync(res, DLM_LOCK_NL);
135 if (ret) {
136 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
137 goto out_err;
138 }
139 res->flags &= ~DLM_LKF_EXPEDITE;
140 res->flags |= DLM_LKF_CONVERT;
141
142 return res;
143out_err:
144 kfree(res->lksb.sb_lvbptr);
145 kfree(res->name);
146 kfree(res);
147 return NULL;
148}
149
150static void lockres_free(struct dlm_lock_resource *res)
151{
152 if (!res)
153 return;
154
155 init_completion(&res->completion);
156 dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res);
157 wait_for_completion(&res->completion);
158
159 kfree(res->name);
160 kfree(res->lksb.sb_lvbptr);
161 kfree(res);
162}
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600163
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500164static char *pretty_uuid(char *dest, char *src)
165{
166 int i, len = 0;
167
168 for (i = 0; i < 16; i++) {
169 if (i == 4 || i == 6 || i == 8 || i == 10)
170 len += sprintf(dest + len, "-");
171 len += sprintf(dest + len, "%02x", (__u8)src[i]);
172 }
173 return dest;
174}
175
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500176static void add_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres,
177 sector_t lo, sector_t hi)
178{
179 struct resync_info *ri;
180
181 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
182 ri->lo = cpu_to_le64(lo);
183 ri->hi = cpu_to_le64(hi);
184}
185
186static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
187{
188 struct resync_info ri;
189 struct suspend_info *s = NULL;
190 sector_t hi = 0;
191
192 dlm_lock_sync(lockres, DLM_LOCK_CR);
193 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
194 hi = le64_to_cpu(ri.hi);
195 if (ri.hi > 0) {
196 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
197 if (!s)
198 goto out;
199 s->hi = hi;
200 s->lo = le64_to_cpu(ri.lo);
201 }
202 dlm_unlock_sync(lockres);
203out:
204 return s;
205}
206
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500207void recover_bitmaps(struct md_thread *thread)
208{
209 struct mddev *mddev = thread->mddev;
210 struct md_cluster_info *cinfo = mddev->cluster_info;
211 struct dlm_lock_resource *bm_lockres;
212 char str[64];
213 int slot, ret;
214 struct suspend_info *s, *tmp;
215 sector_t lo, hi;
216
217 while (cinfo->recovery_map) {
218 slot = fls64((u64)cinfo->recovery_map) - 1;
219
220 /* Clear suspend_area associated with the bitmap */
221 spin_lock_irq(&cinfo->suspend_lock);
222 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
223 if (slot == s->slot) {
224 list_del(&s->list);
225 kfree(s);
226 }
227 spin_unlock_irq(&cinfo->suspend_lock);
228
229 snprintf(str, 64, "bitmap%04d", slot);
230 bm_lockres = lockres_init(mddev, str, NULL, 1);
231 if (!bm_lockres) {
232 pr_err("md-cluster: Cannot initialize bitmaps\n");
233 goto clear_bit;
234 }
235
236 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
237 if (ret) {
238 pr_err("md-cluster: Could not DLM lock %s: %d\n",
239 str, ret);
240 goto clear_bit;
241 }
242 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500243 if (ret) {
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500244 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500245 goto dlm_unlock;
246 }
247 if (hi > 0) {
248 /* TODO:Wait for current resync to get over */
249 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
250 if (lo < mddev->recovery_cp)
251 mddev->recovery_cp = lo;
252 md_check_recovery(mddev);
253 }
254dlm_unlock:
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500255 dlm_unlock_sync(bm_lockres);
256clear_bit:
257 clear_bit(slot, &cinfo->recovery_map);
258 }
259}
260
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500261static void recover_prep(void *arg)
262{
263}
264
265static void recover_slot(void *arg, struct dlm_slot *slot)
266{
267 struct mddev *mddev = arg;
268 struct md_cluster_info *cinfo = mddev->cluster_info;
269
270 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
271 mddev->bitmap_info.cluster_name,
272 slot->nodeid, slot->slot,
273 cinfo->slot_number);
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500274 set_bit(slot->slot - 1, &cinfo->recovery_map);
275 if (!cinfo->recovery_thread) {
276 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
277 mddev, "recover");
278 if (!cinfo->recovery_thread) {
279 pr_warn("md-cluster: Could not create recovery thread\n");
280 return;
281 }
282 }
283 md_wakeup_thread(cinfo->recovery_thread);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500284}
285
286static void recover_done(void *arg, struct dlm_slot *slots,
287 int num_slots, int our_slot,
288 uint32_t generation)
289{
290 struct mddev *mddev = arg;
291 struct md_cluster_info *cinfo = mddev->cluster_info;
292
293 cinfo->slot_number = our_slot;
294 complete(&cinfo->completion);
295}
296
297static const struct dlm_lockspace_ops md_ls_ops = {
298 .recover_prep = recover_prep,
299 .recover_slot = recover_slot,
300 .recover_done = recover_done,
301};
302
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500303/*
304 * The BAST function for the ack lock resource
305 * This function wakes up the receive thread in
306 * order to receive and process the message.
307 */
308static void ack_bast(void *arg, int mode)
309{
310 struct dlm_lock_resource *res = (struct dlm_lock_resource *)arg;
311 struct md_cluster_info *cinfo = res->mddev->cluster_info;
312
313 if (mode == DLM_LOCK_EX)
314 md_wakeup_thread(cinfo->recv_thread);
315}
316
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500317static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
318{
319 struct suspend_info *s, *tmp;
320
321 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
322 if (slot == s->slot) {
323 pr_info("%s:%d Deleting suspend_info: %d\n",
324 __func__, __LINE__, slot);
325 list_del(&s->list);
326 kfree(s);
327 break;
328 }
329}
330
331static void remove_suspend_info(struct md_cluster_info *cinfo, int slot)
332{
333 spin_lock_irq(&cinfo->suspend_lock);
334 __remove_suspend_info(cinfo, slot);
335 spin_unlock_irq(&cinfo->suspend_lock);
336}
337
338
339static void process_suspend_info(struct md_cluster_info *cinfo,
340 int slot, sector_t lo, sector_t hi)
341{
342 struct suspend_info *s;
343
344 if (!hi) {
345 remove_suspend_info(cinfo, slot);
346 return;
347 }
348 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
349 if (!s)
350 return;
351 s->slot = slot;
352 s->lo = lo;
353 s->hi = hi;
354 spin_lock_irq(&cinfo->suspend_lock);
355 /* Remove existing entry (if exists) before adding */
356 __remove_suspend_info(cinfo, slot);
357 list_add(&s->list, &cinfo->suspend_list);
358 spin_unlock_irq(&cinfo->suspend_lock);
359}
360
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500361static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
362{
363 switch (msg->type) {
364 case METADATA_UPDATED:
365 pr_info("%s: %d Received message: METADATA_UPDATE from %d\n",
366 __func__, __LINE__, msg->slot);
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -0500367 md_reload_sb(mddev);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500368 break;
369 case RESYNCING:
370 pr_info("%s: %d Received message: RESYNCING from %d\n",
371 __func__, __LINE__, msg->slot);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500372 process_suspend_info(mddev->cluster_info, msg->slot,
373 msg->low, msg->high);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500374 break;
375 };
376}
377
378/*
379 * thread for receiving message
380 */
381static void recv_daemon(struct md_thread *thread)
382{
383 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
384 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
385 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
386 struct cluster_msg msg;
387
388 /*get CR on Message*/
389 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
390 pr_err("md/raid1:failed to get CR on MESSAGE\n");
391 return;
392 }
393
394 /* read lvb and wake up thread to process this message_lockres */
395 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
396 process_recvd_msg(thread->mddev, &msg);
397
398 /*release CR on ack_lockres*/
399 dlm_unlock_sync(ack_lockres);
400 /*up-convert to EX on message_lockres*/
401 dlm_lock_sync(message_lockres, DLM_LOCK_EX);
402 /*get CR on ack_lockres again*/
403 dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
404 /*release CR on message_lockres*/
405 dlm_unlock_sync(message_lockres);
406}
407
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500408/* lock_comm()
409 * Takes the lock on the TOKEN lock resource so no other
410 * node can communicate while the operation is underway.
411 */
412static int lock_comm(struct md_cluster_info *cinfo)
413{
414 int error;
415
416 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
417 if (error)
418 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
419 __func__, __LINE__, error);
420 return error;
421}
422
423static void unlock_comm(struct md_cluster_info *cinfo)
424{
425 dlm_unlock_sync(cinfo->token_lockres);
426}
427
428/* __sendmsg()
429 * This function performs the actual sending of the message. This function is
430 * usually called after performing the encompassing operation
431 * The function:
432 * 1. Grabs the message lockresource in EX mode
433 * 2. Copies the message to the message LVB
434 * 3. Downconverts message lockresource to CR
435 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
436 * and the other nodes read the message. The thread will wait here until all other
437 * nodes have released ack lock resource.
438 * 5. Downconvert ack lockresource to CR
439 */
440static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
441{
442 int error;
443 int slot = cinfo->slot_number - 1;
444
445 cmsg->slot = cpu_to_le32(slot);
446 /*get EX on Message*/
447 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
448 if (error) {
449 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
450 goto failed_message;
451 }
452
453 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
454 sizeof(struct cluster_msg));
455 /*down-convert EX to CR on Message*/
456 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CR);
457 if (error) {
458 pr_err("md-cluster: failed to convert EX to CR on MESSAGE(%d)\n",
459 error);
460 goto failed_message;
461 }
462
463 /*up-convert CR to EX on Ack*/
464 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
465 if (error) {
466 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
467 error);
468 goto failed_ack;
469 }
470
471 /*down-convert EX to CR on Ack*/
472 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
473 if (error) {
474 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
475 error);
476 goto failed_ack;
477 }
478
479failed_ack:
480 dlm_unlock_sync(cinfo->message_lockres);
481failed_message:
482 return error;
483}
484
485static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
486{
487 int ret;
488
489 lock_comm(cinfo);
490 ret = __sendmsg(cinfo, cmsg);
491 unlock_comm(cinfo);
492 return ret;
493}
494
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500495static int gather_all_resync_info(struct mddev *mddev, int total_slots)
496{
497 struct md_cluster_info *cinfo = mddev->cluster_info;
498 int i, ret = 0;
499 struct dlm_lock_resource *bm_lockres;
500 struct suspend_info *s;
501 char str[64];
502
503
504 for (i = 0; i < total_slots; i++) {
505 memset(str, '\0', 64);
506 snprintf(str, 64, "bitmap%04d", i);
507 bm_lockres = lockres_init(mddev, str, NULL, 1);
508 if (!bm_lockres)
509 return -ENOMEM;
510 if (i == (cinfo->slot_number - 1))
511 continue;
512
513 bm_lockres->flags |= DLM_LKF_NOQUEUE;
514 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
515 if (ret == -EAGAIN) {
516 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
517 s = read_resync_info(mddev, bm_lockres);
518 if (s) {
519 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
520 __func__, __LINE__,
521 (unsigned long long) s->lo,
522 (unsigned long long) s->hi, i);
523 spin_lock_irq(&cinfo->suspend_lock);
524 s->slot = i;
525 list_add(&s->list, &cinfo->suspend_list);
526 spin_unlock_irq(&cinfo->suspend_lock);
527 }
528 ret = 0;
529 lockres_free(bm_lockres);
530 continue;
531 }
532 if (ret)
533 goto out;
534 /* TODO: Read the disk bitmap sb and check if it needs recovery */
535 dlm_unlock_sync(bm_lockres);
536 lockres_free(bm_lockres);
537 }
538out:
539 return ret;
540}
541
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500542static int join(struct mddev *mddev, int nodes)
543{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500544 struct md_cluster_info *cinfo;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500545 int ret, ops_rv;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500546 char str[64];
547
548 if (!try_module_get(THIS_MODULE))
549 return -ENOENT;
550
551 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
552 if (!cinfo)
553 return -ENOMEM;
554
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500555 init_completion(&cinfo->completion);
556
557 mutex_init(&cinfo->sb_mutex);
558 mddev->cluster_info = cinfo;
559
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500560 memset(str, 0, 64);
561 pretty_uuid(str, mddev->uuid);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500562 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
563 DLM_LSFL_FS, LVB_SIZE,
564 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500565 if (ret)
566 goto err;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500567 wait_for_completion(&cinfo->completion);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500568 if (nodes <= cinfo->slot_number) {
569 pr_err("md-cluster: Slot allotted(%d) greater than available slots(%d)", cinfo->slot_number - 1,
570 nodes);
571 ret = -ERANGE;
572 goto err;
573 }
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500574 cinfo->sb_lock = lockres_init(mddev, "cmd-super",
575 NULL, 0);
576 if (!cinfo->sb_lock) {
577 ret = -ENOMEM;
578 goto err;
579 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500580 /* Initiate the communication resources */
581 ret = -ENOMEM;
582 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
583 if (!cinfo->recv_thread) {
584 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
585 goto err;
586 }
587 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
588 if (!cinfo->message_lockres)
589 goto err;
590 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
591 if (!cinfo->token_lockres)
592 goto err;
593 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
594 if (!cinfo->ack_lockres)
595 goto err;
596 /* get sync CR lock on ACK. */
597 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
598 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
599 ret);
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500600
601 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
602 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
603 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
604 if (!cinfo->bitmap_lockres)
605 goto err;
606 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
607 pr_err("Failed to get bitmap lock\n");
608 ret = -EINVAL;
609 goto err;
610 }
611
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500612 INIT_LIST_HEAD(&cinfo->suspend_list);
613 spin_lock_init(&cinfo->suspend_lock);
614
615 ret = gather_all_resync_info(mddev, nodes);
616 if (ret)
617 goto err;
618
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500619 return 0;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500620err:
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500621 lockres_free(cinfo->message_lockres);
622 lockres_free(cinfo->token_lockres);
623 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500624 lockres_free(cinfo->bitmap_lockres);
625 lockres_free(cinfo->sb_lock);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500626 if (cinfo->lockspace)
627 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500628 mddev->cluster_info = NULL;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500629 kfree(cinfo);
630 module_put(THIS_MODULE);
631 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500632}
633
634static int leave(struct mddev *mddev)
635{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500636 struct md_cluster_info *cinfo = mddev->cluster_info;
637
638 if (!cinfo)
639 return 0;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500640 md_unregister_thread(&cinfo->recovery_thread);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500641 md_unregister_thread(&cinfo->recv_thread);
642 lockres_free(cinfo->message_lockres);
643 lockres_free(cinfo->token_lockres);
644 lockres_free(cinfo->ack_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500645 lockres_free(cinfo->sb_lock);
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500646 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500647 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500648 return 0;
649}
650
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500651/* slot_number(): Returns the MD slot number to use
652 * DLM starts the slot numbers from 1, wheras cluster-md
653 * wants the number to be from zero, so we deduct one
654 */
655static int slot_number(struct mddev *mddev)
656{
657 struct md_cluster_info *cinfo = mddev->cluster_info;
658
659 return cinfo->slot_number - 1;
660}
661
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500662static void resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
663{
664 struct md_cluster_info *cinfo = mddev->cluster_info;
665
666 add_resync_info(mddev, cinfo->bitmap_lockres, lo, hi);
667 /* Re-acquire the lock to refresh LVB */
668 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
669}
670
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500671static int metadata_update_start(struct mddev *mddev)
672{
673 return lock_comm(mddev->cluster_info);
674}
675
676static int metadata_update_finish(struct mddev *mddev)
677{
678 struct md_cluster_info *cinfo = mddev->cluster_info;
679 struct cluster_msg cmsg;
680 int ret;
681
682 memset(&cmsg, 0, sizeof(cmsg));
683 cmsg.type = cpu_to_le32(METADATA_UPDATED);
684 ret = __sendmsg(cinfo, &cmsg);
685 unlock_comm(cinfo);
686 return ret;
687}
688
689static int metadata_update_cancel(struct mddev *mddev)
690{
691 struct md_cluster_info *cinfo = mddev->cluster_info;
692
693 return dlm_unlock_sync(cinfo->token_lockres);
694}
695
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500696static int resync_send(struct mddev *mddev, enum msg_type type,
697 sector_t lo, sector_t hi)
698{
699 struct md_cluster_info *cinfo = mddev->cluster_info;
700 struct cluster_msg cmsg;
701 int slot = cinfo->slot_number - 1;
702
703 pr_info("%s:%d lo: %llu hi: %llu\n", __func__, __LINE__,
704 (unsigned long long)lo,
705 (unsigned long long)hi);
706 resync_info_update(mddev, lo, hi);
707 cmsg.type = cpu_to_le32(type);
708 cmsg.slot = cpu_to_le32(slot);
709 cmsg.low = cpu_to_le64(lo);
710 cmsg.high = cpu_to_le64(hi);
711 return sendmsg(cinfo, &cmsg);
712}
713
714static int resync_start(struct mddev *mddev, sector_t lo, sector_t hi)
715{
716 pr_info("%s:%d\n", __func__, __LINE__);
717 return resync_send(mddev, RESYNCING, lo, hi);
718}
719
720static void resync_finish(struct mddev *mddev)
721{
722 pr_info("%s:%d\n", __func__, __LINE__);
723 resync_send(mddev, RESYNCING, 0, 0);
724}
725
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500726static int area_resyncing(struct mddev *mddev, sector_t lo, sector_t hi)
727{
728 struct md_cluster_info *cinfo = mddev->cluster_info;
729 int ret = 0;
730 struct suspend_info *s;
731
732 spin_lock_irq(&cinfo->suspend_lock);
733 if (list_empty(&cinfo->suspend_list))
734 goto out;
735 list_for_each_entry(s, &cinfo->suspend_list, list)
736 if (hi > s->lo && lo < s->hi) {
737 ret = 1;
738 break;
739 }
740out:
741 spin_unlock_irq(&cinfo->suspend_lock);
742 return ret;
743}
744
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500745static struct md_cluster_operations cluster_ops = {
746 .join = join,
747 .leave = leave,
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500748 .slot_number = slot_number,
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500749 .resync_info_update = resync_info_update,
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500750 .resync_start = resync_start,
751 .resync_finish = resync_finish,
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500752 .metadata_update_start = metadata_update_start,
753 .metadata_update_finish = metadata_update_finish,
754 .metadata_update_cancel = metadata_update_cancel,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500755 .area_resyncing = area_resyncing,
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500756};
757
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600758static int __init cluster_init(void)
759{
760 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
761 pr_info("Registering Cluster MD functions\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500762 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600763 return 0;
764}
765
766static void cluster_exit(void)
767{
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500768 unregister_md_cluster_operations();
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600769}
770
771module_init(cluster_init);
772module_exit(cluster_exit);
773MODULE_LICENSE("GPL");
774MODULE_DESCRIPTION("Clustering support for MD");