blob: f42162c49e3bdc0a6bde8c4d36456294e6b77e3d [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. */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -050031 int mode;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050032};
33
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050034struct 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
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060046/* md_cluster_info flags */
47#define MD_CLUSTER_WAITING_FOR_NEWDISK 1
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -050048#define MD_CLUSTER_SUSPEND_READ_BALANCING 2
Guoqing Jiangeece0752015-07-10 17:01:21 +080049#define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060050
51
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050052struct md_cluster_info {
53 /* dlm lock space and resources for clustered raid. */
54 dlm_lockspace_t *lockspace;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -050055 int slot_number;
56 struct completion completion;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -050057 struct mutex sb_mutex;
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -050058 struct dlm_lock_resource *bitmap_lockres;
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -050059 struct dlm_lock_resource *resync_lockres;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -050060 struct list_head suspend_list;
61 spinlock_t suspend_lock;
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -050062 struct md_thread *recovery_thread;
63 unsigned long recovery_map;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050064 /* communication loc resources */
65 struct dlm_lock_resource *ack_lockres;
66 struct dlm_lock_resource *message_lockres;
67 struct dlm_lock_resource *token_lockres;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050068 struct dlm_lock_resource *no_new_dev_lockres;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050069 struct md_thread *recv_thread;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050070 struct completion newdisk_completion;
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -060071 unsigned long state;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050072};
73
74enum msg_type {
75 METADATA_UPDATED = 0,
76 RESYNCING,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050077 NEWDISK,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -050078 REMOVE,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -050079 RE_ADD,
Guoqing Jiangdc737d72015-07-10 16:54:04 +080080 BITMAP_NEEDS_SYNC,
Goldwyn Rodrigues46646802014-06-07 01:08:29 -050081};
82
83struct cluster_msg {
Guoqing Jiangcf97a342015-10-16 15:40:22 +080084 __le32 type;
85 __le32 slot;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050086 /* TODO: Unionize this for smaller footprint */
Guoqing Jiangcf97a342015-10-16 15:40:22 +080087 __le64 low;
88 __le64 high;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -050089 char uuid[16];
Guoqing Jiangcf97a342015-10-16 15:40:22 +080090 __le32 raid_slot;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -060091};
92
93static void sync_ast(void *arg)
94{
95 struct dlm_lock_resource *res;
96
97 res = (struct dlm_lock_resource *) arg;
98 complete(&res->completion);
99}
100
101static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
102{
103 int ret = 0;
104
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600105 ret = dlm_lock(res->ls, mode, &res->lksb,
106 res->flags, res->name, strlen(res->name),
107 0, sync_ast, res, res->bast);
108 if (ret)
109 return ret;
110 wait_for_completion(&res->completion);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500111 if (res->lksb.sb_status == 0)
112 res->mode = mode;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600113 return res->lksb.sb_status;
114}
115
116static int dlm_unlock_sync(struct dlm_lock_resource *res)
117{
118 return dlm_lock_sync(res, DLM_LOCK_NL);
119}
120
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500121static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600122 char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
123{
124 struct dlm_lock_resource *res = NULL;
125 int ret, namelen;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500126 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600127
128 res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
129 if (!res)
130 return NULL;
Guoqing Jiangb83d51c2015-07-10 17:01:16 +0800131 init_completion(&res->completion);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500132 res->ls = cinfo->lockspace;
133 res->mddev = mddev;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500134 res->mode = DLM_LOCK_IV;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600135 namelen = strlen(name);
136 res->name = kzalloc(namelen + 1, GFP_KERNEL);
137 if (!res->name) {
138 pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
139 goto out_err;
140 }
141 strlcpy(res->name, name, namelen + 1);
142 if (with_lvb) {
143 res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
144 if (!res->lksb.sb_lvbptr) {
145 pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
146 goto out_err;
147 }
148 res->flags = DLM_LKF_VALBLK;
149 }
150
151 if (bastfn)
152 res->bast = bastfn;
153
154 res->flags |= DLM_LKF_EXPEDITE;
155
156 ret = dlm_lock_sync(res, DLM_LOCK_NL);
157 if (ret) {
158 pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
159 goto out_err;
160 }
161 res->flags &= ~DLM_LKF_EXPEDITE;
162 res->flags |= DLM_LKF_CONVERT;
163
164 return res;
165out_err:
166 kfree(res->lksb.sb_lvbptr);
167 kfree(res->name);
168 kfree(res);
169 return NULL;
170}
171
172static void lockres_free(struct dlm_lock_resource *res)
173{
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800174 int ret;
175
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600176 if (!res)
177 return;
178
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800179 /* cancel a lock request or a conversion request that is blocked */
180 res->flags |= DLM_LKF_CANCEL;
181retry:
182 ret = dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res);
183 if (unlikely(ret != 0)) {
184 pr_info("%s: failed to unlock %s return %d\n", __func__, res->name, ret);
185
186 /* if a lock conversion is cancelled, then the lock is put
187 * back to grant queue, need to ensure it is unlocked */
188 if (ret == -DLM_ECANCEL)
189 goto retry;
190 }
191 res->flags &= ~DLM_LKF_CANCEL;
Goldwyn Rodrigues47741b72014-03-07 13:49:26 -0600192 wait_for_completion(&res->completion);
193
194 kfree(res->name);
195 kfree(res->lksb.sb_lvbptr);
196 kfree(res);
197}
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -0600198
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500199static void add_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres,
200 sector_t lo, sector_t hi)
201{
202 struct resync_info *ri;
203
204 ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
205 ri->lo = cpu_to_le64(lo);
206 ri->hi = cpu_to_le64(hi);
207}
208
209static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres)
210{
211 struct resync_info ri;
212 struct suspend_info *s = NULL;
213 sector_t hi = 0;
214
215 dlm_lock_sync(lockres, DLM_LOCK_CR);
216 memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
217 hi = le64_to_cpu(ri.hi);
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800218 if (hi > 0) {
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500219 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
220 if (!s)
221 goto out;
222 s->hi = hi;
223 s->lo = le64_to_cpu(ri.lo);
224 }
225 dlm_unlock_sync(lockres);
226out:
227 return s;
228}
229
kbuild test robot6dc69c9c2015-02-28 07:04:37 +0800230static void recover_bitmaps(struct md_thread *thread)
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500231{
232 struct mddev *mddev = thread->mddev;
233 struct md_cluster_info *cinfo = mddev->cluster_info;
234 struct dlm_lock_resource *bm_lockres;
235 char str[64];
236 int slot, ret;
237 struct suspend_info *s, *tmp;
238 sector_t lo, hi;
239
240 while (cinfo->recovery_map) {
241 slot = fls64((u64)cinfo->recovery_map) - 1;
242
243 /* Clear suspend_area associated with the bitmap */
244 spin_lock_irq(&cinfo->suspend_lock);
245 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
246 if (slot == s->slot) {
247 list_del(&s->list);
248 kfree(s);
249 }
250 spin_unlock_irq(&cinfo->suspend_lock);
251
252 snprintf(str, 64, "bitmap%04d", slot);
253 bm_lockres = lockres_init(mddev, str, NULL, 1);
254 if (!bm_lockres) {
255 pr_err("md-cluster: Cannot initialize bitmaps\n");
256 goto clear_bit;
257 }
258
259 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
260 if (ret) {
261 pr_err("md-cluster: Could not DLM lock %s: %d\n",
262 str, ret);
263 goto clear_bit;
264 }
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500265 ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500266 if (ret) {
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500267 pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
Goldwyn Rodrigues4b26a082014-06-07 00:52:29 -0500268 goto dlm_unlock;
269 }
270 if (hi > 0) {
271 /* TODO:Wait for current resync to get over */
272 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
273 if (lo < mddev->recovery_cp)
274 mddev->recovery_cp = lo;
275 md_check_recovery(mddev);
276 }
277dlm_unlock:
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500278 dlm_unlock_sync(bm_lockres);
279clear_bit:
280 clear_bit(slot, &cinfo->recovery_map);
281 }
282}
283
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500284static void recover_prep(void *arg)
285{
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500286 struct mddev *mddev = arg;
287 struct md_cluster_info *cinfo = mddev->cluster_info;
288 set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500289}
290
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800291static void __recover_slot(struct mddev *mddev, int slot)
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500292{
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500293 struct md_cluster_info *cinfo = mddev->cluster_info;
294
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800295 set_bit(slot, &cinfo->recovery_map);
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500296 if (!cinfo->recovery_thread) {
297 cinfo->recovery_thread = md_register_thread(recover_bitmaps,
298 mddev, "recover");
299 if (!cinfo->recovery_thread) {
300 pr_warn("md-cluster: Could not create recovery thread\n");
301 return;
302 }
303 }
304 md_wakeup_thread(cinfo->recovery_thread);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500305}
306
Guoqing Jiang05cd0e52015-07-10 16:54:03 +0800307static void recover_slot(void *arg, struct dlm_slot *slot)
308{
309 struct mddev *mddev = arg;
310 struct md_cluster_info *cinfo = mddev->cluster_info;
311
312 pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
313 mddev->bitmap_info.cluster_name,
314 slot->nodeid, slot->slot,
315 cinfo->slot_number);
316 /* deduct one since dlm slot starts from one while the num of
317 * cluster-md begins with 0 */
318 __recover_slot(mddev, slot->slot - 1);
319}
320
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500321static void recover_done(void *arg, struct dlm_slot *slots,
322 int num_slots, int our_slot,
323 uint32_t generation)
324{
325 struct mddev *mddev = arg;
326 struct md_cluster_info *cinfo = mddev->cluster_info;
327
328 cinfo->slot_number = our_slot;
Guoqing Jiangeece0752015-07-10 17:01:21 +0800329 /* completion is only need to be complete when node join cluster,
330 * it doesn't need to run during another node's failure */
331 if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
332 complete(&cinfo->completion);
333 clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
334 }
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500335 clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500336}
337
Guoqing Jiangeece0752015-07-10 17:01:21 +0800338/* the ops is called when node join the cluster, and do lock recovery
339 * if node failure occurs */
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500340static const struct dlm_lockspace_ops md_ls_ops = {
341 .recover_prep = recover_prep,
342 .recover_slot = recover_slot,
343 .recover_done = recover_done,
344};
345
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500346/*
347 * The BAST function for the ack lock resource
348 * This function wakes up the receive thread in
349 * order to receive and process the message.
350 */
351static void ack_bast(void *arg, int mode)
352{
353 struct dlm_lock_resource *res = (struct dlm_lock_resource *)arg;
354 struct md_cluster_info *cinfo = res->mddev->cluster_info;
355
356 if (mode == DLM_LOCK_EX)
357 md_wakeup_thread(cinfo->recv_thread);
358}
359
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500360static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot)
361{
362 struct suspend_info *s, *tmp;
363
364 list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list)
365 if (slot == s->slot) {
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500366 list_del(&s->list);
367 kfree(s);
368 break;
369 }
370}
371
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500372static void remove_suspend_info(struct mddev *mddev, int slot)
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500373{
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500374 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500375 spin_lock_irq(&cinfo->suspend_lock);
376 __remove_suspend_info(cinfo, slot);
377 spin_unlock_irq(&cinfo->suspend_lock);
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500378 mddev->pers->quiesce(mddev, 2);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500379}
380
381
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500382static void process_suspend_info(struct mddev *mddev,
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500383 int slot, sector_t lo, sector_t hi)
384{
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500385 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500386 struct suspend_info *s;
387
388 if (!hi) {
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500389 remove_suspend_info(mddev, slot);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500390 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
391 md_wakeup_thread(mddev->thread);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500392 return;
393 }
394 s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL);
395 if (!s)
396 return;
397 s->slot = slot;
398 s->lo = lo;
399 s->hi = hi;
Goldwyn Rodrigues9ed38ff52015-08-14 12:19:40 -0500400 mddev->pers->quiesce(mddev, 1);
401 mddev->pers->quiesce(mddev, 0);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500402 spin_lock_irq(&cinfo->suspend_lock);
403 /* Remove existing entry (if exists) before adding */
404 __remove_suspend_info(cinfo, slot);
405 list_add(&s->list, &cinfo->suspend_list);
406 spin_unlock_irq(&cinfo->suspend_lock);
Goldwyn Rodriguesb8ca8462015-10-09 11:27:01 -0500407 mddev->pers->quiesce(mddev, 2);
Goldwyn Rodriguese59721c2014-06-07 02:30:30 -0500408}
409
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500410static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
411{
412 char disk_uuid[64];
413 struct md_cluster_info *cinfo = mddev->cluster_info;
414 char event_name[] = "EVENT=ADD_DEVICE";
415 char raid_slot[16];
416 char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
417 int len;
418
419 len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800420 sprintf(disk_uuid + len, "%pU", cmsg->uuid);
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800421 snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500422 pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
423 init_completion(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600424 set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500425 kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
426 wait_for_completion_timeout(&cinfo->newdisk_completion,
427 NEW_DEV_TIMEOUT);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600428 clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500429}
430
431
432static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
433{
434 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500435 md_reload_sb(mddev, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500436 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
437}
438
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500439static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
440{
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800441 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
442 le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500443
444 if (rdev)
445 md_kick_rdev_from_array(rdev);
446 else
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800447 pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
448 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500449}
450
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500451static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
452{
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800453 struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev,
454 le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500455
456 if (rdev && test_bit(Faulty, &rdev->flags))
457 clear_bit(Faulty, &rdev->flags);
458 else
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800459 pr_warn("%s: %d Could not find disk(%d) which is faulty",
460 __func__, __LINE__, le32_to_cpu(msg->raid_slot));
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500461}
462
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500463static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
464{
Guoqing Jiang256f5b22015-10-12 17:21:23 +0800465 if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
466 "node %d received it's own msg\n", le32_to_cpu(msg->slot)))
467 return;
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800468 switch (le32_to_cpu(msg->type)) {
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500469 case METADATA_UPDATED:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500470 process_metadata_update(mddev, msg);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500471 break;
472 case RESYNCING:
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800473 process_suspend_info(mddev, le32_to_cpu(msg->slot),
474 le64_to_cpu(msg->low),
475 le64_to_cpu(msg->high));
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500476 break;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500477 case NEWDISK:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500478 process_add_new_disk(mddev, msg);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500479 break;
480 case REMOVE:
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500481 process_remove_disk(mddev, msg);
482 break;
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500483 case RE_ADD:
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500484 process_readd_disk(mddev, msg);
485 break;
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800486 case BITMAP_NEEDS_SYNC:
Guoqing Jiangcf97a342015-10-16 15:40:22 +0800487 __recover_slot(mddev, le32_to_cpu(msg->slot));
Guoqing Jiangdc737d72015-07-10 16:54:04 +0800488 break;
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500489 default:
490 pr_warn("%s:%d Received unknown message from %d\n",
491 __func__, __LINE__, msg->slot);
kbuild test robot09dd1af2015-02-28 09:16:08 +0800492 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500493}
494
495/*
496 * thread for receiving message
497 */
498static void recv_daemon(struct md_thread *thread)
499{
500 struct md_cluster_info *cinfo = thread->mddev->cluster_info;
501 struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
502 struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
503 struct cluster_msg msg;
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800504 int ret;
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500505
506 /*get CR on Message*/
507 if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
508 pr_err("md/raid1:failed to get CR on MESSAGE\n");
509 return;
510 }
511
512 /* read lvb and wake up thread to process this message_lockres */
513 memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
514 process_recvd_msg(thread->mddev, &msg);
515
516 /*release CR on ack_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800517 ret = dlm_unlock_sync(ack_lockres);
518 if (unlikely(ret != 0))
519 pr_info("unlock ack failed return %d\n", ret);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800520 /*up-convert to PR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800521 ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
522 if (unlikely(ret != 0))
523 pr_info("lock PR on msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500524 /*get CR on ack_lockres again*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800525 ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
526 if (unlikely(ret != 0))
527 pr_info("lock CR on ack failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500528 /*release CR on message_lockres*/
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800529 ret = dlm_unlock_sync(message_lockres);
530 if (unlikely(ret != 0))
531 pr_info("unlock msg failed return %d\n", ret);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500532}
533
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500534/* lock_comm()
535 * Takes the lock on the TOKEN lock resource so no other
536 * node can communicate while the operation is underway.
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500537 * If called again, and the TOKEN lock is alread in EX mode
538 * return success. However, care must be taken that unlock_comm()
539 * is called only once.
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500540 */
541static int lock_comm(struct md_cluster_info *cinfo)
542{
543 int error;
544
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500545 if (cinfo->token_lockres->mode == DLM_LOCK_EX)
546 return 0;
547
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500548 error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
549 if (error)
550 pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
551 __func__, __LINE__, error);
552 return error;
553}
554
555static void unlock_comm(struct md_cluster_info *cinfo)
556{
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500557 WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500558 dlm_unlock_sync(cinfo->token_lockres);
559}
560
561/* __sendmsg()
562 * This function performs the actual sending of the message. This function is
563 * usually called after performing the encompassing operation
564 * The function:
565 * 1. Grabs the message lockresource in EX mode
566 * 2. Copies the message to the message LVB
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800567 * 3. Downconverts message lockresource to CW
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500568 * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
569 * and the other nodes read the message. The thread will wait here until all other
570 * nodes have released ack lock resource.
571 * 5. Downconvert ack lockresource to CR
572 */
573static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
574{
575 int error;
576 int slot = cinfo->slot_number - 1;
577
578 cmsg->slot = cpu_to_le32(slot);
579 /*get EX on Message*/
580 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
581 if (error) {
582 pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
583 goto failed_message;
584 }
585
586 memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
587 sizeof(struct cluster_msg));
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800588 /*down-convert EX to CW on Message*/
589 error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500590 if (error) {
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800591 pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500592 error);
Guoqing Jiang66099bb2015-07-10 17:01:15 +0800593 goto failed_ack;
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500594 }
595
596 /*up-convert CR to EX on Ack*/
597 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
598 if (error) {
599 pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
600 error);
601 goto failed_ack;
602 }
603
604 /*down-convert EX to CR on Ack*/
605 error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
606 if (error) {
607 pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
608 error);
609 goto failed_ack;
610 }
611
612failed_ack:
Guoqing Jiangb5ef5672015-07-10 17:01:17 +0800613 error = dlm_unlock_sync(cinfo->message_lockres);
614 if (unlikely(error != 0)) {
615 pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
616 error);
617 /* in case the message can't be released due to some reason */
618 goto failed_ack;
619 }
Goldwyn Rodrigues601b5152014-06-07 01:28:53 -0500620failed_message:
621 return error;
622}
623
624static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
625{
626 int ret;
627
628 lock_comm(cinfo);
629 ret = __sendmsg(cinfo, cmsg);
630 unlock_comm(cinfo);
631 return ret;
632}
633
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500634static int gather_all_resync_info(struct mddev *mddev, int total_slots)
635{
636 struct md_cluster_info *cinfo = mddev->cluster_info;
637 int i, ret = 0;
638 struct dlm_lock_resource *bm_lockres;
639 struct suspend_info *s;
640 char str[64];
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800641 sector_t lo, hi;
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500642
643
644 for (i = 0; i < total_slots; i++) {
645 memset(str, '\0', 64);
646 snprintf(str, 64, "bitmap%04d", i);
647 bm_lockres = lockres_init(mddev, str, NULL, 1);
648 if (!bm_lockres)
649 return -ENOMEM;
650 if (i == (cinfo->slot_number - 1))
651 continue;
652
653 bm_lockres->flags |= DLM_LKF_NOQUEUE;
654 ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
655 if (ret == -EAGAIN) {
656 memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE);
657 s = read_resync_info(mddev, bm_lockres);
658 if (s) {
659 pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
660 __func__, __LINE__,
661 (unsigned long long) s->lo,
662 (unsigned long long) s->hi, i);
663 spin_lock_irq(&cinfo->suspend_lock);
664 s->slot = i;
665 list_add(&s->list, &cinfo->suspend_list);
666 spin_unlock_irq(&cinfo->suspend_lock);
667 }
668 ret = 0;
669 lockres_free(bm_lockres);
670 continue;
671 }
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800672 if (ret) {
673 lockres_free(bm_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500674 goto out;
Guoqing Jiang6e6d9f22015-07-10 17:01:20 +0800675 }
Guoqing Jiangabb9b222015-07-10 17:01:22 +0800676
677 /* Read the disk bitmap sb and check if it needs recovery */
678 ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false);
679 if (ret) {
680 pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
681 lockres_free(bm_lockres);
682 continue;
683 }
684 if ((hi > 0) && (lo < mddev->recovery_cp)) {
685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
686 mddev->recovery_cp = lo;
687 md_check_recovery(mddev);
688 }
689
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500690 dlm_unlock_sync(bm_lockres);
691 lockres_free(bm_lockres);
692 }
693out:
694 return ret;
695}
696
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500697static int join(struct mddev *mddev, int nodes)
698{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500699 struct md_cluster_info *cinfo;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500700 int ret, ops_rv;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500701 char str[64];
702
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500703 cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
704 if (!cinfo)
705 return -ENOMEM;
706
Guoqing Jiang9e3072e2015-07-10 17:01:18 +0800707 INIT_LIST_HEAD(&cinfo->suspend_list);
708 spin_lock_init(&cinfo->suspend_lock);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500709 init_completion(&cinfo->completion);
Guoqing Jiangeece0752015-07-10 17:01:21 +0800710 set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500711
712 mutex_init(&cinfo->sb_mutex);
713 mddev->cluster_info = cinfo;
714
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500715 memset(str, 0, 64);
Guoqing Jiangb89f7042015-07-10 16:54:02 +0800716 sprintf(str, "%pU", mddev->uuid);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500717 ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
718 DLM_LSFL_FS, LVB_SIZE,
719 &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500720 if (ret)
721 goto err;
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500722 wait_for_completion(&cinfo->completion);
Guoqing Jiang8c58f022015-04-21 11:25:52 -0500723 if (nodes < cinfo->slot_number) {
724 pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
725 cinfo->slot_number, nodes);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500726 ret = -ERANGE;
727 goto err;
728 }
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500729 /* Initiate the communication resources */
730 ret = -ENOMEM;
731 cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv");
732 if (!cinfo->recv_thread) {
733 pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
734 goto err;
735 }
736 cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
737 if (!cinfo->message_lockres)
738 goto err;
739 cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
740 if (!cinfo->token_lockres)
741 goto err;
742 cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
743 if (!cinfo->ack_lockres)
744 goto err;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500745 cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
746 if (!cinfo->no_new_dev_lockres)
747 goto err;
748
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500749 /* get sync CR lock on ACK. */
750 if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
751 pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
752 ret);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500753 /* get sync CR lock on no-new-dev. */
754 if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
755 pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
756
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500757
758 pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
759 snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
760 cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
761 if (!cinfo->bitmap_lockres)
762 goto err;
763 if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
764 pr_err("Failed to get bitmap lock\n");
765 ret = -EINVAL;
766 goto err;
767 }
768
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500769 cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
770 if (!cinfo->resync_lockres)
771 goto err;
772
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500773 ret = gather_all_resync_info(mddev, nodes);
774 if (ret)
775 goto err;
776
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500777 return 0;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500778err:
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500779 lockres_free(cinfo->message_lockres);
780 lockres_free(cinfo->token_lockres);
781 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500782 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500783 lockres_free(cinfo->resync_lockres);
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -0500784 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500785 if (cinfo->lockspace)
786 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500787 mddev->cluster_info = NULL;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500788 kfree(cinfo);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500789 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500790}
791
Guoqing Jiang09995412015-10-01 00:09:18 +0800792static void resync_bitmap(struct mddev *mddev)
793{
794 struct md_cluster_info *cinfo = mddev->cluster_info;
795 struct cluster_msg cmsg = {0};
796 int err;
797
798 cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
799 err = sendmsg(cinfo, &cmsg);
800 if (err)
801 pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
802 __func__, __LINE__, err);
803}
804
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500805static int leave(struct mddev *mddev)
806{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500807 struct md_cluster_info *cinfo = mddev->cluster_info;
808
809 if (!cinfo)
810 return 0;
Guoqing Jiang09995412015-10-01 00:09:18 +0800811
812 /* BITMAP_NEEDS_SYNC message should be sent when node
813 * is leaving the cluster with dirty bitmap, also we
814 * can only deliver it when dlm connection is available */
815 if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector)
816 resync_bitmap(mddev);
817
Goldwyn Rodriguese94987d2014-06-07 00:45:22 -0500818 md_unregister_thread(&cinfo->recovery_thread);
Goldwyn Rodrigues46646802014-06-07 01:08:29 -0500819 md_unregister_thread(&cinfo->recv_thread);
820 lockres_free(cinfo->message_lockres);
821 lockres_free(cinfo->token_lockres);
822 lockres_free(cinfo->ack_lockres);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500823 lockres_free(cinfo->no_new_dev_lockres);
Goldwyn Rodrigues54519c52014-06-06 12:12:32 -0500824 lockres_free(cinfo->bitmap_lockres);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500825 dlm_release_lockspace(cinfo->lockspace, 2);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -0500826 return 0;
827}
828
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500829/* slot_number(): Returns the MD slot number to use
830 * DLM starts the slot numbers from 1, wheras cluster-md
831 * wants the number to be from zero, so we deduct one
832 */
833static int slot_number(struct mddev *mddev)
834{
835 struct md_cluster_info *cinfo = mddev->cluster_info;
836
837 return cinfo->slot_number - 1;
838}
839
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500840static int metadata_update_start(struct mddev *mddev)
841{
842 return lock_comm(mddev->cluster_info);
843}
844
845static int metadata_update_finish(struct mddev *mddev)
846{
847 struct md_cluster_info *cinfo = mddev->cluster_info;
848 struct cluster_msg cmsg;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500849 struct md_rdev *rdev;
850 int ret = 0;
NeilBrownba2746b2015-10-16 13:48:35 +1100851 int raid_slot = -1;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500852
853 memset(&cmsg, 0, sizeof(cmsg));
854 cmsg.type = cpu_to_le32(METADATA_UPDATED);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500855 /* Pick up a good active device number to send.
856 */
857 rdev_for_each(rdev, mddev)
858 if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
NeilBrownba2746b2015-10-16 13:48:35 +1100859 raid_slot = rdev->desc_nr;
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500860 break;
861 }
NeilBrownba2746b2015-10-16 13:48:35 +1100862 if (raid_slot >= 0) {
863 cmsg.raid_slot = cpu_to_le32(raid_slot);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500864 ret = __sendmsg(cinfo, &cmsg);
NeilBrownba2746b2015-10-16 13:48:35 +1100865 } else
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -0500866 pr_warn("md-cluster: No good device id found to send\n");
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500867 unlock_comm(cinfo);
868 return ret;
869}
870
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500871static void metadata_update_cancel(struct mddev *mddev)
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500872{
873 struct md_cluster_info *cinfo = mddev->cluster_info;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500874 unlock_comm(cinfo);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -0500875}
876
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500877static int resync_start(struct mddev *mddev)
878{
879 struct md_cluster_info *cinfo = mddev->cluster_info;
880 cinfo->resync_lockres->flags |= DLM_LKF_NOQUEUE;
881 return dlm_lock_sync(cinfo->resync_lockres, DLM_LOCK_EX);
882}
883
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000884static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500885{
886 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiangaee177a2015-10-12 17:21:24 +0800887 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500888
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000889 add_resync_info(mddev, cinfo->bitmap_lockres, lo, hi);
890 /* Re-acquire the lock to refresh LVB */
891 dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000892 cmsg.type = cpu_to_le32(RESYNCING);
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500893 cmsg.low = cpu_to_le64(lo);
894 cmsg.high = cpu_to_le64(hi);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500895
Goldwyn Rodrigues965400e2014-06-07 02:16:58 -0500896 return sendmsg(cinfo, &cmsg);
897}
898
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -0500899static int resync_finish(struct mddev *mddev)
900{
901 struct md_cluster_info *cinfo = mddev->cluster_info;
902 cinfo->resync_lockres->flags &= ~DLM_LKF_NOQUEUE;
903 dlm_unlock_sync(cinfo->resync_lockres);
904 return resync_info_update(mddev, 0, 0);
905}
906
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500907static int area_resyncing(struct mddev *mddev, int direction,
908 sector_t lo, sector_t hi)
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500909{
910 struct md_cluster_info *cinfo = mddev->cluster_info;
911 int ret = 0;
912 struct suspend_info *s;
913
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500914 if ((direction == READ) &&
915 test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
916 return 1;
917
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -0500918 spin_lock_irq(&cinfo->suspend_lock);
919 if (list_empty(&cinfo->suspend_list))
920 goto out;
921 list_for_each_entry(s, &cinfo->suspend_list, list)
922 if (hi > s->lo && lo < s->hi) {
923 ret = 1;
924 break;
925 }
926out:
927 spin_unlock_irq(&cinfo->suspend_lock);
928 return ret;
929}
930
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500931/* add_new_disk() - initiates a disk add
932 * However, if this fails before writing md_update_sb(),
933 * add_new_disk_cancel() must be called to release token lock
934 */
935static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500936{
937 struct md_cluster_info *cinfo = mddev->cluster_info;
938 struct cluster_msg cmsg;
939 int ret = 0;
940 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
941 char *uuid = sb->device_uuid;
942
943 memset(&cmsg, 0, sizeof(cmsg));
944 cmsg.type = cpu_to_le32(NEWDISK);
945 memcpy(cmsg.uuid, uuid, 16);
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800946 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500947 lock_comm(cinfo);
948 ret = __sendmsg(cinfo, &cmsg);
949 if (ret)
950 return ret;
951 cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
952 ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
953 cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
954 /* Some node does not "see" the device */
955 if (ret == -EAGAIN)
956 ret = -ENOENT;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500957 if (ret)
958 unlock_comm(cinfo);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500959 else
960 dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
961 return ret;
962}
963
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500964static void add_new_disk_cancel(struct mddev *mddev)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500965{
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -0500966 struct md_cluster_info *cinfo = mddev->cluster_info;
967 unlock_comm(cinfo);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500968}
969
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600970static int new_disk_ack(struct mddev *mddev, bool ack)
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500971{
972 struct md_cluster_info *cinfo = mddev->cluster_info;
973
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600974 if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
975 pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
976 return -EINVAL;
977 }
978
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500979 if (ack)
980 dlm_unlock_sync(cinfo->no_new_dev_lockres);
981 complete(&cinfo->newdisk_completion);
Goldwyn Rodriguesfa8259d2015-03-02 10:55:49 -0600982 return 0;
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -0500983}
984
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500985static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
986{
Guoqing Jiangaee177a2015-10-12 17:21:24 +0800987 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500988 struct md_cluster_info *cinfo = mddev->cluster_info;
Guoqing Jiangfaeff832015-10-12 17:21:21 +0800989 cmsg.type = cpu_to_le32(REMOVE);
990 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -0500991 return __sendmsg(cinfo, &cmsg);
992}
993
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500994static int gather_bitmaps(struct md_rdev *rdev)
995{
996 int sn, err;
997 sector_t lo, hi;
Guoqing Jiangaee177a2015-10-12 17:21:24 +0800998 struct cluster_msg cmsg = {0};
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -0500999 struct mddev *mddev = rdev->mddev;
1000 struct md_cluster_info *cinfo = mddev->cluster_info;
1001
Guoqing Jiangfaeff832015-10-12 17:21:21 +08001002 cmsg.type = cpu_to_le32(RE_ADD);
1003 cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001004 err = sendmsg(cinfo, &cmsg);
1005 if (err)
1006 goto out;
1007
1008 for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
1009 if (sn == (cinfo->slot_number - 1))
1010 continue;
1011 err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
1012 if (err) {
1013 pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
1014 goto out;
1015 }
1016 if ((hi > 0) && (lo < mddev->recovery_cp))
1017 mddev->recovery_cp = lo;
1018 }
1019out:
1020 return err;
1021}
1022
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001023static struct md_cluster_operations cluster_ops = {
1024 .join = join,
1025 .leave = leave,
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05001026 .slot_number = slot_number,
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05001027 .resync_start = resync_start,
1028 .resync_finish = resync_finish,
Goldwyn Rodrigues96ae9232014-06-06 12:35:34 -05001029 .resync_info_update = resync_info_update,
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05001030 .metadata_update_start = metadata_update_start,
1031 .metadata_update_finish = metadata_update_finish,
1032 .metadata_update_cancel = metadata_update_cancel,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001033 .area_resyncing = area_resyncing,
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05001034 .add_new_disk = add_new_disk,
1035 .add_new_disk_cancel = add_new_disk_cancel,
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001036 .new_disk_ack = new_disk_ack,
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05001037 .remove_disk = remove_disk,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001038 .gather_bitmaps = gather_bitmaps,
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001039};
1040
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001041static int __init cluster_init(void)
1042{
1043 pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
1044 pr_info("Registering Cluster MD functions\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001045 register_md_cluster_operations(&cluster_ops, THIS_MODULE);
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001046 return 0;
1047}
1048
1049static void cluster_exit(void)
1050{
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05001051 unregister_md_cluster_operations();
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001052}
1053
1054module_init(cluster_init);
1055module_exit(cluster_exit);
Guoqing Jiang86b57272015-10-12 17:21:25 +08001056MODULE_AUTHOR("SUSE");
Goldwyn Rodrigues8e854e92014-03-07 11:21:15 -06001057MODULE_LICENSE("GPL");
1058MODULE_DESCRIPTION("Clustering support for MD");