blob: 11e3bc9d2a4b8377a940dde49fffd2ce36bec9ad [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>
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050015#include <linux/raid/md_p.h>
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060016#include "md.h"
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050017#include "bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050018#include "md-cluster.h"
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060019
20#define LVB_SIZE 64
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050021#define NEW_DEV_TIMEOUT 5000
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060022
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() */
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060028 struct completion completion; /* completion for synchronized locking */
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050029 void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
30 struct mddev *mddev; /* pointing back to mddev. */
31};
32
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050033struct 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
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060045/* md_cluster_info flags */
46#define MD_CLUSTER_WAITING_FOR_NEWDISK 1
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -050047#define MD_CLUSTER_SUSPEND_READ_BALANCING 2
Guoqing Jiangeece0752015-07-10 17:01:21 +080048#define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060049
50
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050051struct md_cluster_info {
52 /* dlm lock space and resources for clustered raid. */
53 dlm_lockspace_t *lockspace;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -050054 int slot_number;
55 struct completion completion;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050056 struct mutex sb_mutex;
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -050057 struct dlm_lock_resource *bitmap_lockres;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050058 struct list_head suspend_list;
59 spinlock_t suspend_lock;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050060 struct md_thread *recovery_thread;
61 unsigned long recovery_map;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050062 /* communication loc resources */
63 struct dlm_lock_resource *ack_lockres;
64 struct dlm_lock_resource *message_lockres;
65 struct dlm_lock_resource *token_lockres;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050066 struct dlm_lock_resource *no_new_dev_lockres;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050067 struct md_thread *recv_thread;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050068 struct completion newdisk_completion;
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060069 unsigned long state;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050070};
71
72enum msg_type {
73 METADATA_UPDATED = 0,
74 RESYNCING,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050075 NEWDISK,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -050076 REMOVE,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -050077 RE_ADD,
Guoqing Jiangdc737d72015-07-10 16:54:04 +080078 BITMAP_NEEDS_SYNC,
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050079};
80
81struct cluster_msg {
82 int type;
83 int slot;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050084 /* TODO: Unionize this for smaller footprint */
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050085 sector_t low;
86 sector_t high;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050087 char uuid[16];
88 int raid_slot;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060089};
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
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600103 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
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500117static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600118 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
119{
120 struct dlm_lock_resource *res = NULL;
121 int ret, namelen;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500122 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600123
124 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
125 if (!res)
126 return NULL;
Guoqing Jiangb83d51c2015-07-10 17:01:16 +0800127 init_completion(&res->completion);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500128 res->ls = cinfo->lockspace;
129 res->mddev = mddev;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600130 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{
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800169 int ret;
170
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600171 if (!res)
172 return;
173
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800174 /* 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;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600187 wait_for_completion(&res->completion);
188
189 kfree(res->name);
190 kfree(res->lksb.sb_lvbptr);
191 kfree(res);
192}
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600193
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500194static 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
kbuild test robot6dc69c92015-02-28 07:04:37 +0800225static void recover_bitmaps(struct md_thread *thread)
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500226{
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 }
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500260 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500261 if (ret) {
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500262 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500263 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:
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500273 dlm_unlock_sync(bm_lockres);
274clear_bit:
275 clear_bit(slot, &cinfo->recovery_map);
276 }
277}
278
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500279static void recover_prep(void *arg)
280{
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500281 struct mddev *mddev = arg;
282 struct md_cluster_info *cinfo = mddev->cluster_info;
283 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500284}
285
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800286static void __recover_slot(struct mddev *mddev, int slot)
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500287{
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500288 struct md_cluster_info *cinfo = mddev->cluster_info;
289
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800290 set_bit(slot, &cinfo->recovery_map);
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500291 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);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500300}
301
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800302static 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
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500316static 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;
Guoqing Jiangeece0752015-07-10 17:01:21 +0800324 /* 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 }
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500330 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500331}
332
Guoqing Jiangeece0752015-07-10 17:01:21 +0800333/* the ops is called when node join the cluster, and do lock recovery
334 * if node failure occurs */
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500335static 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
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500341/*
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
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500355static 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
369static void remove_suspend_info(struct md_cluster_info *cinfo, int slot)
370{
371 spin_lock_irq(&cinfo->suspend_lock);
372 __remove_suspend_info(cinfo, slot);
373 spin_unlock_irq(&cinfo->suspend_lock);
374}
375
376
377static void process_suspend_info(struct md_cluster_info *cinfo,
378 int slot, sector_t lo, sector_t hi)
379{
380 struct suspend_info *s;
381
382 if (!hi) {
383 remove_suspend_info(cinfo, slot);
384 return;
385 }
386 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
387 if (!s)
388 return;
389 s->slot = slot;
390 s->lo = lo;
391 s->hi = hi;
392 spin_lock_irq(&cinfo->suspend_lock);
393 /* Remove existing entry (if exists) before adding */
394 __remove_suspend_info(cinfo, slot);
395 list_add(&s->list, &cinfo->suspend_list);
396 spin_unlock_irq(&cinfo->suspend_lock);
397}
398
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500399static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
400{
401 char disk_uuid[64];
402 struct md_cluster_info *cinfo = mddev->cluster_info;
403 char event_name[] = "EVENT=ADD_DEVICE";
404 char raid_slot[16];
405 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
406 int len;
407
408 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800409 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500410 snprintf(raid_slot, 16, "RAID_DISK=%d", cmsg->raid_slot);
411 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
412 init_completion(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600413 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500414 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
415 wait_for_completion_timeout(&cinfo->newdisk_completion,
416 NEW_DEV_TIMEOUT);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600417 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500418}
419
420
421static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
422{
423 struct md_cluster_info *cinfo = mddev->cluster_info;
424
425 md_reload_sb(mddev);
426 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
427}
428
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500429static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
430{
431 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev, msg->raid_slot);
432
433 if (rdev)
434 md_kick_rdev_from_array(rdev);
435 else
436 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n", __func__, __LINE__, msg->raid_slot);
437}
438
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500439static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
440{
441 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev, msg->raid_slot);
442
443 if (rdev && test_bit(Faulty, &rdev->flags))
444 clear_bit(Faulty, &rdev->flags);
445 else
446 pr_warn("%s: %d Could not find disk(%d) which is faulty", __func__, __LINE__, msg->raid_slot);
447}
448
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500449static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
450{
451 switch (msg->type) {
452 case METADATA_UPDATED:
453 pr_info("%s: %d Received message: METADATA_UPDATE from %d\n",
454 __func__, __LINE__, msg->slot);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500455 process_metadata_update(mddev, msg);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500456 break;
457 case RESYNCING:
458 pr_info("%s: %d Received message: RESYNCING from %d\n",
459 __func__, __LINE__, msg->slot);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500460 process_suspend_info(mddev->cluster_info, msg->slot,
461 msg->low, msg->high);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500462 break;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500463 case NEWDISK:
464 pr_info("%s: %d Received message: NEWDISK from %d\n",
465 __func__, __LINE__, msg->slot);
466 process_add_new_disk(mddev, msg);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500467 break;
468 case REMOVE:
469 pr_info("%s: %d Received REMOVE from %d\n",
470 __func__, __LINE__, msg->slot);
471 process_remove_disk(mddev, msg);
472 break;
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500473 case RE_ADD:
474 pr_info("%s: %d Received RE_ADD from %d\n",
475 __func__, __LINE__, msg->slot);
476 process_readd_disk(mddev, msg);
477 break;
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800478 case BITMAP_NEEDS_SYNC:
479 pr_info("%s: %d Received BITMAP_NEEDS_SYNC from %d\n",
480 __func__, __LINE__, msg->slot);
481 __recover_slot(mddev, msg->slot);
482 break;
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500483 default:
484 pr_warn("%s:%d Received unknown message from %d\n",
485 __func__, __LINE__, msg->slot);
kbuild test robot09dd1af2015-02-28 09:16:08 +0800486 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500487}
488
489/*
490 * thread for receiving message
491 */
492static void recv_daemon(struct md_thread *thread)
493{
494 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
495 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
496 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
497 struct cluster_msg msg;
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800498 int ret;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500499
500 /*get CR on Message*/
501 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
502 pr_err("md/raid1:failed to get CR on MESSAGE\n");
503 return;
504 }
505
506 /* read lvb and wake up thread to process this message_lockres */
507 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
508 process_recvd_msg(thread->mddev, &msg);
509
510 /*release CR on ack_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800511 ret = dlm_unlock_sync(ack_lockres);
512 if (unlikely(ret != 0))
513 pr_info("unlock ack failed return %d\n", ret);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800514 /*up-convert to PR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800515 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
516 if (unlikely(ret != 0))
517 pr_info("lock PR on msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500518 /*get CR on ack_lockres again*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800519 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
520 if (unlikely(ret != 0))
521 pr_info("lock CR on ack failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500522 /*release CR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800523 ret = dlm_unlock_sync(message_lockres);
524 if (unlikely(ret != 0))
525 pr_info("unlock msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500526}
527
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500528/* lock_comm()
529 * Takes the lock on the TOKEN lock resource so no other
530 * node can communicate while the operation is underway.
531 */
532static int lock_comm(struct md_cluster_info *cinfo)
533{
534 int error;
535
536 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
537 if (error)
538 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
539 __func__, __LINE__, error);
540 return error;
541}
542
543static void unlock_comm(struct md_cluster_info *cinfo)
544{
545 dlm_unlock_sync(cinfo->token_lockres);
546}
547
548/* __sendmsg()
549 * This function performs the actual sending of the message. This function is
550 * usually called after performing the encompassing operation
551 * The function:
552 * 1. Grabs the message lockresource in EX mode
553 * 2. Copies the message to the message LVB
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800554 * 3. Downconverts message lockresource to CW
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500555 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
556 * and the other nodes read the message. The thread will wait here until all other
557 * nodes have released ack lock resource.
558 * 5. Downconvert ack lockresource to CR
559 */
560static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
561{
562 int error;
563 int slot = cinfo->slot_number - 1;
564
565 cmsg->slot = cpu_to_le32(slot);
566 /*get EX on Message*/
567 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
568 if (error) {
569 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
570 goto failed_message;
571 }
572
573 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
574 sizeof(struct cluster_msg));
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800575 /*down-convert EX to CW on Message*/
576 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500577 if (error) {
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800578 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500579 error);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800580 goto failed_ack;
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500581 }
582
583 /*up-convert CR to EX on Ack*/
584 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
585 if (error) {
586 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
587 error);
588 goto failed_ack;
589 }
590
591 /*down-convert EX to CR on Ack*/
592 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
593 if (error) {
594 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
595 error);
596 goto failed_ack;
597 }
598
599failed_ack:
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800600 error = dlm_unlock_sync(cinfo->message_lockres);
601 if (unlikely(error != 0)) {
602 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
603 error);
604 /* in case the message can't be released due to some reason */
605 goto failed_ack;
606 }
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500607failed_message:
608 return error;
609}
610
611static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
612{
613 int ret;
614
615 lock_comm(cinfo);
616 ret = __sendmsg(cinfo, cmsg);
617 unlock_comm(cinfo);
618 return ret;
619}
620
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500621static int gather_all_resync_info(struct mddev *mddev, int total_slots)
622{
623 struct md_cluster_info *cinfo = mddev->cluster_info;
624 int i, ret = 0;
625 struct dlm_lock_resource *bm_lockres;
626 struct suspend_info *s;
627 char str[64];
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800628 sector_t lo, hi;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500629
630
631 for (i = 0; i < total_slots; i++) {
632 memset(str, '\0', 64);
633 snprintf(str, 64, "bitmap%04d", i);
634 bm_lockres = lockres_init(mddev, str, NULL, 1);
635 if (!bm_lockres)
636 return -ENOMEM;
637 if (i == (cinfo->slot_number - 1))
638 continue;
639
640 bm_lockres->flags |= DLM_LKF_NOQUEUE;
641 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
642 if (ret == -EAGAIN) {
643 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
644 s = read_resync_info(mddev, bm_lockres);
645 if (s) {
646 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
647 __func__, __LINE__,
648 (unsigned long long) s->lo,
649 (unsigned long long) s->hi, i);
650 spin_lock_irq(&cinfo->suspend_lock);
651 s->slot = i;
652 list_add(&s->list, &cinfo->suspend_list);
653 spin_unlock_irq(&cinfo->suspend_lock);
654 }
655 ret = 0;
656 lockres_free(bm_lockres);
657 continue;
658 }
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800659 if (ret) {
660 lockres_free(bm_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500661 goto out;
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800662 }
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800663
664 /* Read the disk bitmap sb and check if it needs recovery */
665 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
666 if (ret) {
667 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
668 lockres_free(bm_lockres);
669 continue;
670 }
671 if ((hi > 0) && (lo < mddev->recovery_cp)) {
672 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
673 mddev->recovery_cp = lo;
674 md_check_recovery(mddev);
675 }
676
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500677 dlm_unlock_sync(bm_lockres);
678 lockres_free(bm_lockres);
679 }
680out:
681 return ret;
682}
683
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500684static int join(struct mddev *mddev, int nodes)
685{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500686 struct md_cluster_info *cinfo;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500687 int ret, ops_rv;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500688 char str[64];
689
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500690 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
691 if (!cinfo)
692 return -ENOMEM;
693
Guoqing Jiang9e3072e2015-07-10 17:01:18 +0800694 INIT_LIST_HEAD(&cinfo->suspend_list);
695 spin_lock_init(&cinfo->suspend_lock);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500696 init_completion(&cinfo->completion);
Guoqing Jiangeece0752015-07-10 17:01:21 +0800697 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500698
699 mutex_init(&cinfo->sb_mutex);
700 mddev->cluster_info = cinfo;
701
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500702 memset(str, 0, 64);
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800703 sprintf(str, "%pU", mddev->uuid);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500704 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
705 DLM_LSFL_FS, LVB_SIZE,
706 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500707 if (ret)
708 goto err;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500709 wait_for_completion(&cinfo->completion);
Guoqing Jiang8c58f022015-04-21 11:25:52 -0500710 if (nodes < cinfo->slot_number) {
711 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
712 cinfo->slot_number, nodes);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500713 ret = -ERANGE;
714 goto err;
715 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500716 /* Initiate the communication resources */
717 ret = -ENOMEM;
718 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
719 if (!cinfo->recv_thread) {
720 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
721 goto err;
722 }
723 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
724 if (!cinfo->message_lockres)
725 goto err;
726 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
727 if (!cinfo->token_lockres)
728 goto err;
729 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
730 if (!cinfo->ack_lockres)
731 goto err;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500732 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
733 if (!cinfo->no_new_dev_lockres)
734 goto err;
735
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500736 /* get sync CR lock on ACK. */
737 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
738 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
739 ret);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500740 /* get sync CR lock on no-new-dev. */
741 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
742 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
743
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500744
745 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
746 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
747 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
748 if (!cinfo->bitmap_lockres)
749 goto err;
750 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
751 pr_err("Failed to get bitmap lock\n");
752 ret = -EINVAL;
753 goto err;
754 }
755
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500756 ret = gather_all_resync_info(mddev, nodes);
757 if (ret)
758 goto err;
759
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500760 return 0;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500761err:
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500762 lockres_free(cinfo->message_lockres);
763 lockres_free(cinfo->token_lockres);
764 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500765 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500766 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500767 if (cinfo->lockspace)
768 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500769 mddev->cluster_info = NULL;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500770 kfree(cinfo);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500771 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500772}
773
774static int leave(struct mddev *mddev)
775{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500776 struct md_cluster_info *cinfo = mddev->cluster_info;
777
778 if (!cinfo)
779 return 0;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500780 md_unregister_thread(&cinfo->recovery_thread);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500781 md_unregister_thread(&cinfo->recv_thread);
782 lockres_free(cinfo->message_lockres);
783 lockres_free(cinfo->token_lockres);
784 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500785 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500786 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500787 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500788 return 0;
789}
790
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500791/* slot_number(): Returns the MD slot number to use
792 * DLM starts the slot numbers from 1, wheras cluster-md
793 * wants the number to be from zero, so we deduct one
794 */
795static int slot_number(struct mddev *mddev)
796{
797 struct md_cluster_info *cinfo = mddev->cluster_info;
798
799 return cinfo->slot_number - 1;
800}
801
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500802static void resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
803{
804 struct md_cluster_info *cinfo = mddev->cluster_info;
805
806 add_resync_info(mddev, cinfo->bitmap_lockres, lo, hi);
807 /* Re-acquire the lock to refresh LVB */
808 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
809}
810
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500811static int metadata_update_start(struct mddev *mddev)
812{
813 return lock_comm(mddev->cluster_info);
814}
815
816static int metadata_update_finish(struct mddev *mddev)
817{
818 struct md_cluster_info *cinfo = mddev->cluster_info;
819 struct cluster_msg cmsg;
820 int ret;
821
822 memset(&cmsg, 0, sizeof(cmsg));
823 cmsg.type = cpu_to_le32(METADATA_UPDATED);
824 ret = __sendmsg(cinfo, &cmsg);
825 unlock_comm(cinfo);
826 return ret;
827}
828
829static int metadata_update_cancel(struct mddev *mddev)
830{
831 struct md_cluster_info *cinfo = mddev->cluster_info;
832
833 return dlm_unlock_sync(cinfo->token_lockres);
834}
835
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500836static int resync_send(struct mddev *mddev, enum msg_type type,
837 sector_t lo, sector_t hi)
838{
839 struct md_cluster_info *cinfo = mddev->cluster_info;
840 struct cluster_msg cmsg;
841 int slot = cinfo->slot_number - 1;
842
843 pr_info("%s:%d lo: %llu hi: %llu\n", __func__, __LINE__,
844 (unsigned long long)lo,
845 (unsigned long long)hi);
846 resync_info_update(mddev, lo, hi);
847 cmsg.type = cpu_to_le32(type);
848 cmsg.slot = cpu_to_le32(slot);
849 cmsg.low = cpu_to_le64(lo);
850 cmsg.high = cpu_to_le64(hi);
851 return sendmsg(cinfo, &cmsg);
852}
853
854static int resync_start(struct mddev *mddev, sector_t lo, sector_t hi)
855{
856 pr_info("%s:%d\n", __func__, __LINE__);
857 return resync_send(mddev, RESYNCING, lo, hi);
858}
859
860static void resync_finish(struct mddev *mddev)
861{
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800862 struct md_cluster_info *cinfo = mddev->cluster_info;
863 struct cluster_msg cmsg;
864 int slot = cinfo->slot_number - 1;
865
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500866 pr_info("%s:%d\n", __func__, __LINE__);
867 resync_send(mddev, RESYNCING, 0, 0);
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800868 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
869 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
870 cmsg.slot = cpu_to_le32(slot);
871 sendmsg(cinfo, &cmsg);
872 }
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500873}
874
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500875static int area_resyncing(struct mddev *mddev, int direction,
876 sector_t lo, sector_t hi)
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500877{
878 struct md_cluster_info *cinfo = mddev->cluster_info;
879 int ret = 0;
880 struct suspend_info *s;
881
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500882 if ((direction == READ) &&
883 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
884 return 1;
885
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500886 spin_lock_irq(&cinfo->suspend_lock);
887 if (list_empty(&cinfo->suspend_list))
888 goto out;
889 list_for_each_entry(s, &cinfo->suspend_list, list)
890 if (hi > s->lo && lo < s->hi) {
891 ret = 1;
892 break;
893 }
894out:
895 spin_unlock_irq(&cinfo->suspend_lock);
896 return ret;
897}
898
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500899static int add_new_disk_start(struct mddev *mddev, struct md_rdev *rdev)
900{
901 struct md_cluster_info *cinfo = mddev->cluster_info;
902 struct cluster_msg cmsg;
903 int ret = 0;
904 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
905 char *uuid = sb->device_uuid;
906
907 memset(&cmsg, 0, sizeof(cmsg));
908 cmsg.type = cpu_to_le32(NEWDISK);
909 memcpy(cmsg.uuid, uuid, 16);
910 cmsg.raid_slot = rdev->desc_nr;
911 lock_comm(cinfo);
912 ret = __sendmsg(cinfo, &cmsg);
913 if (ret)
914 return ret;
915 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
916 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
917 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
918 /* Some node does not "see" the device */
919 if (ret == -EAGAIN)
920 ret = -ENOENT;
921 else
922 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
923 return ret;
924}
925
926static int add_new_disk_finish(struct mddev *mddev)
927{
928 struct cluster_msg cmsg;
929 struct md_cluster_info *cinfo = mddev->cluster_info;
930 int ret;
931 /* Write sb and inform others */
932 md_update_sb(mddev, 1);
933 cmsg.type = METADATA_UPDATED;
934 ret = __sendmsg(cinfo, &cmsg);
935 unlock_comm(cinfo);
936 return ret;
937}
938
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600939static int new_disk_ack(struct mddev *mddev, bool ack)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500940{
941 struct md_cluster_info *cinfo = mddev->cluster_info;
942
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600943 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
944 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
945 return -EINVAL;
946 }
947
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500948 if (ack)
949 dlm_unlock_sync(cinfo->no_new_dev_lockres);
950 complete(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600951 return 0;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500952}
953
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500954static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
955{
956 struct cluster_msg cmsg;
957 struct md_cluster_info *cinfo = mddev->cluster_info;
958 cmsg.type = REMOVE;
959 cmsg.raid_slot = rdev->desc_nr;
960 return __sendmsg(cinfo, &cmsg);
961}
962
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500963static int gather_bitmaps(struct md_rdev *rdev)
964{
965 int sn, err;
966 sector_t lo, hi;
967 struct cluster_msg cmsg;
968 struct mddev *mddev = rdev->mddev;
969 struct md_cluster_info *cinfo = mddev->cluster_info;
970
971 cmsg.type = RE_ADD;
972 cmsg.raid_slot = rdev->desc_nr;
973 err = sendmsg(cinfo, &cmsg);
974 if (err)
975 goto out;
976
977 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
978 if (sn == (cinfo->slot_number - 1))
979 continue;
980 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
981 if (err) {
982 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
983 goto out;
984 }
985 if ((hi > 0) && (lo < mddev->recovery_cp))
986 mddev->recovery_cp = lo;
987 }
988out:
989 return err;
990}
991
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500992static struct md_cluster_operations cluster_ops = {
993 .join = join,
994 .leave = leave,
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500995 .slot_number = slot_number,
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500996 .resync_info_update = resync_info_update,
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500997 .resync_start = resync_start,
998 .resync_finish = resync_finish,
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500999 .metadata_update_start = metadata_update_start,
1000 .metadata_update_finish = metadata_update_finish,
1001 .metadata_update_cancel = metadata_update_cancel,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001002 .area_resyncing = area_resyncing,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001003 .add_new_disk_start = add_new_disk_start,
1004 .add_new_disk_finish = add_new_disk_finish,
1005 .new_disk_ack = new_disk_ack,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001006 .remove_disk = remove_disk,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001007 .gather_bitmaps = gather_bitmaps,
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001008};
1009
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001010static int __init cluster_init(void)
1011{
1012 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1013 pr_info("Registering Cluster MD functions\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001014 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001015 return 0;
1016}
1017
1018static void cluster_exit(void)
1019{
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001020 unregister_md_cluster_operations();
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001021}
1022
1023module_init(cluster_init);
1024module_exit(cluster_exit);
1025MODULE_LICENSE("GPL");
1026MODULE_DESCRIPTION("Clustering support for MD");