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